Macpaul Lin | fafc245 | 2011-04-24 22:01:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Driver of Andes SPI Controller |
| 3 | * |
| 4 | * (C) Copyright 2011 Andes Technology |
| 5 | * Macpaul Lin <macpaul@andestech.com> |
| 6 | * |
| 7 | * See file CREDITS for list of people who contributed to this |
| 8 | * project. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of |
| 13 | * the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 23 | * MA 02111-1307 USA |
| 24 | */ |
| 25 | |
| 26 | #include <common.h> |
| 27 | #include <malloc.h> |
| 28 | #include <spi.h> |
| 29 | |
| 30 | #include <asm/io.h> |
| 31 | #include "andes_spi.h" |
| 32 | |
| 33 | void spi_init(void) |
| 34 | { |
| 35 | /* do nothing */ |
| 36 | } |
| 37 | |
| 38 | static void andes_spi_spit_en(struct andes_spi_slave *ds) |
| 39 | { |
| 40 | unsigned int dcr = readl(&ds->regs->dcr); |
| 41 | |
| 42 | debug("%s: dcr: %x, write value: %x\n", |
| 43 | __func__, dcr, (dcr | ANDES_SPI_DCR_SPIT)); |
| 44 | |
| 45 | writel((dcr | ANDES_SPI_DCR_SPIT), &ds->regs->dcr); |
| 46 | } |
| 47 | |
| 48 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 49 | unsigned int max_hz, unsigned int mode) |
| 50 | { |
| 51 | struct andes_spi_slave *ds; |
| 52 | |
| 53 | if (!spi_cs_is_valid(bus, cs)) |
| 54 | return NULL; |
| 55 | |
| 56 | ds = malloc(sizeof(*ds)); |
| 57 | if (!ds) |
| 58 | return NULL; |
| 59 | |
| 60 | ds->slave.bus = bus; |
| 61 | ds->slave.cs = cs; |
| 62 | ds->regs = (struct andes_spi_regs *)CONFIG_SYS_SPI_BASE; |
| 63 | |
| 64 | /* |
| 65 | * The hardware of andes_spi will set its frequency according |
| 66 | * to APB/AHB bus clock. Hence the hardware doesn't allow changing of |
| 67 | * requency and so the user requested speed is always ignored. |
| 68 | */ |
| 69 | ds->freq = max_hz; |
| 70 | |
| 71 | return &ds->slave; |
| 72 | } |
| 73 | |
| 74 | void spi_free_slave(struct spi_slave *slave) |
| 75 | { |
| 76 | struct andes_spi_slave *ds = to_andes_spi(slave); |
| 77 | |
| 78 | free(ds); |
| 79 | } |
| 80 | |
| 81 | int spi_claim_bus(struct spi_slave *slave) |
| 82 | { |
| 83 | struct andes_spi_slave *ds = to_andes_spi(slave); |
| 84 | unsigned int apb; |
| 85 | unsigned int baud; |
| 86 | |
| 87 | /* Enable the SPI hardware */ |
| 88 | writel(ANDES_SPI_CR_SPIRST, &ds->regs->cr); |
| 89 | udelay(1000); |
| 90 | |
| 91 | /* setup format */ |
| 92 | baud = ((CONFIG_SYS_CLK_FREQ / CONFIG_SYS_SPI_CLK / 2) - 1) & 0xFF; |
| 93 | |
| 94 | /* |
| 95 | * SPI_CLK = AHB bus clock / ((BAUD + 1)*2) |
| 96 | * BAUD = AHB bus clock / SPI_CLK / 2) - 1 |
| 97 | */ |
| 98 | apb = (readl(&ds->regs->apb) & 0xffffff00) | baud; |
| 99 | writel(apb, &ds->regs->apb); |
| 100 | |
| 101 | /* no interrupts */ |
| 102 | writel(0, &ds->regs->ie); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | void spi_release_bus(struct spi_slave *slave) |
| 108 | { |
| 109 | struct andes_spi_slave *ds = to_andes_spi(slave); |
| 110 | |
| 111 | /* Disable the SPI hardware */ |
| 112 | writel(ANDES_SPI_CR_SPIRST, &ds->regs->cr); |
| 113 | } |
| 114 | |
| 115 | static int andes_spi_read(struct spi_slave *slave, unsigned int len, |
| 116 | u8 *rxp, unsigned long flags) |
| 117 | { |
| 118 | struct andes_spi_slave *ds = to_andes_spi(slave); |
| 119 | unsigned int i, left; |
| 120 | unsigned int data; |
| 121 | |
| 122 | debug("%s: slave: %x, len: %d, rxp: %x, flags: %d\n", |
| 123 | __func__, slave, len, rxp, flags); |
| 124 | |
| 125 | debug("%s: data: ", __func__); |
| 126 | while (len > 0) { |
| 127 | left = min(len, 4); |
| 128 | data = readl(&ds->regs->data); |
| 129 | |
| 130 | debug(" "); |
| 131 | for (i = 0; i < left; i++) { |
| 132 | debug("%02x ", data & 0xff); |
| 133 | *rxp++ = data; |
| 134 | data >>= 8; |
| 135 | len--; |
| 136 | } |
| 137 | } |
| 138 | debug("\n"); |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int andes_spi_write(struct spi_slave *slave, unsigned int wlen, |
| 144 | unsigned int rlen, const u8 *txp, unsigned long flags) |
| 145 | { |
| 146 | struct andes_spi_slave *ds = to_andes_spi(slave); |
| 147 | unsigned int data; |
| 148 | unsigned int i, left; |
| 149 | unsigned int spit_enabled = 0; |
| 150 | |
| 151 | debug("%s: slave: %x, wlen: %d, rlen: %d, txp: %x, flags: %x\n", |
| 152 | __func__, slave, wlen, rlen, txp, flags); |
| 153 | |
| 154 | /* The value of wlen and rlen wrote to register must minus 1 */ |
| 155 | if (rlen == 0) /* write only */ |
| 156 | writel(ANDES_SPI_DCR_MODE_WO | ANDES_SPI_DCR_WCNT(wlen-1) | |
| 157 | ANDES_SPI_DCR_RCNT(0), &ds->regs->dcr); |
| 158 | else /* write then read */ |
| 159 | writel(ANDES_SPI_DCR_MODE_WR | ANDES_SPI_DCR_WCNT(wlen-1) | |
| 160 | ANDES_SPI_DCR_RCNT(rlen-1), &ds->regs->dcr); |
| 161 | |
| 162 | /* wait till SPIBSY is cleared */ |
| 163 | while (readl(&ds->regs->st) & ANDES_SPI_ST_SPIBSY) |
| 164 | ; |
| 165 | |
| 166 | /* data write process */ |
| 167 | debug("%s: txp: ", __func__); |
| 168 | while (wlen > 0) { |
| 169 | /* clear the data */ |
| 170 | data = 0; |
| 171 | |
| 172 | /* data are usually be read 32bits once a time */ |
| 173 | left = min(wlen, 4); |
| 174 | |
| 175 | for (i = 0; i < left; i++) { |
| 176 | debug("%x ", *txp); |
| 177 | data |= *txp++ << (i * 8); |
| 178 | wlen--; |
| 179 | } |
| 180 | debug("\n"); |
| 181 | |
| 182 | debug("data: %08x\n", data); |
| 183 | debug("streg before write: %08x\n", readl(&ds->regs->st)); |
| 184 | /* wait till TXFULL is deasserted */ |
| 185 | while (readl(&ds->regs->st) & ANDES_SPI_ST_TXFEL) |
| 186 | ; |
| 187 | writel(data, &ds->regs->data); |
| 188 | debug("streg after write: %08x\n", readl(&ds->regs->st)); |
| 189 | |
| 190 | |
| 191 | if (spit_enabled == 0) { |
| 192 | /* enable SPIT bit - trigger the tx and rx progress */ |
| 193 | andes_spi_spit_en(ds); |
| 194 | spit_enabled = 1; |
| 195 | } |
| 196 | |
| 197 | } |
| 198 | debug("\n"); |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * spi_xfer: |
| 205 | * Since andes_spi doesn't support independent command transaction, |
| 206 | * that is, write and than read must be operated in continuous |
| 207 | * execution, there is no need to set dcr and trigger spit again in |
| 208 | * RX process. |
| 209 | */ |
| 210 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, |
| 211 | const void *dout, void *din, unsigned long flags) |
| 212 | { |
| 213 | unsigned int len; |
| 214 | static int op_nextime; |
| 215 | static u8 tmp_cmd[5]; |
| 216 | static int tmp_wlen; |
| 217 | unsigned int i; |
| 218 | |
| 219 | if (bitlen == 0) |
| 220 | /* Finish any previously submitted transfers */ |
| 221 | goto out; |
| 222 | |
| 223 | if (bitlen % 8) { |
| 224 | /* Errors always terminate an ongoing transfer */ |
| 225 | flags |= SPI_XFER_END; |
| 226 | goto out; |
| 227 | } |
| 228 | |
| 229 | len = bitlen / 8; |
| 230 | |
| 231 | debug("%s: slave: %08x, bitlen: %d, dout: " |
| 232 | "%08x, din: %08x, flags: %d, len: %d\n", |
| 233 | __func__, slave, bitlen, dout, din, flags, len); |
| 234 | |
| 235 | /* |
| 236 | * Important: |
| 237 | * andes_spi's hardware doesn't support 2 data channel. The read |
| 238 | * and write cmd/data share the same register (data register). |
| 239 | * |
| 240 | * If a command has write and read transaction, you cannot do write |
| 241 | * this time and then do read on next time. |
| 242 | * |
| 243 | * A command writes first with a read response must indicating |
| 244 | * the read length in write operation. Hence the write action must |
| 245 | * be stored temporary and wait until the next read action has been |
| 246 | * arrived. Then we flush the write and read action out together. |
| 247 | */ |
| 248 | if (!dout) { |
| 249 | if (op_nextime == 1) { |
| 250 | /* flags should be SPI_XFER_END, value is 2 */ |
| 251 | op_nextime = 0; |
| 252 | andes_spi_write(slave, tmp_wlen, len, tmp_cmd, flags); |
| 253 | } |
| 254 | return andes_spi_read(slave, len, din, flags); |
| 255 | } else if (!din) { |
| 256 | if (flags == SPI_XFER_BEGIN) { |
| 257 | /* store the write command and do operation next time */ |
| 258 | op_nextime = 1; |
| 259 | memset(tmp_cmd, 0, sizeof(tmp_cmd)); |
| 260 | memcpy(tmp_cmd, dout, len); |
| 261 | |
| 262 | debug("%s: tmp_cmd: ", __func__); |
| 263 | for (i = 0; i < len; i++) |
| 264 | debug("%x ", *(tmp_cmd + i)); |
| 265 | debug("\n"); |
| 266 | |
| 267 | tmp_wlen = len; |
| 268 | } else { |
| 269 | /* |
| 270 | * flags should be (SPI_XFER_BEGIN | SPI_XFER_END), |
| 271 | * the value is 3. |
| 272 | */ |
| 273 | if (op_nextime == 1) { |
| 274 | /* flags should be SPI_XFER_END, value is 2 */ |
| 275 | op_nextime = 0; |
| 276 | /* flags 3 implies write only */ |
| 277 | andes_spi_write(slave, tmp_wlen, 0, tmp_cmd, 3); |
| 278 | } |
| 279 | |
| 280 | debug("flags: %x\n", flags); |
| 281 | return andes_spi_write(slave, len, 0, dout, flags); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | out: |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | int spi_cs_is_valid(unsigned int bus, unsigned int cs) |
| 290 | { |
| 291 | return bus == 0 && cs == 0; |
| 292 | } |
| 293 | |
| 294 | void spi_cs_activate(struct spi_slave *slave) |
| 295 | { |
| 296 | /* do nothing */ |
| 297 | } |
| 298 | |
| 299 | void spi_cs_deactivate(struct spi_slave *slave) |
| 300 | { |
| 301 | /* do nothing */ |
| 302 | } |