Richard Retanubun | 9324138 | 2011-03-24 08:58:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Freescale Coldfire Queued SPI driver |
| 3 | * |
| 4 | * NOTE: |
| 5 | * This driver is written to transfer 8 bit at-a-time and uses the dedicated |
| 6 | * SPI slave select pins as bit-banged GPIO to work with spi_flash subsystem. |
| 7 | * |
| 8 | * |
| 9 | * Copyright (C) 2011 Ruggedcom, Inc. |
| 10 | * Richard Retanubun (richardretanubun@freescale.com) |
| 11 | * |
| 12 | * See file CREDITS for list of people who contributed to this project. |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of |
| 17 | * the License, or (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 27 | * MA 02111-1307 USA |
| 28 | */ |
| 29 | |
| 30 | #include <common.h> |
| 31 | #include <malloc.h> |
| 32 | #include <spi.h> |
| 33 | #include <asm/immap.h> |
| 34 | #include <asm/io.h> |
| 35 | |
| 36 | DECLARE_GLOBAL_DATA_PTR; |
| 37 | |
| 38 | #define clamp(x, low, high) (min(max(low, x), high)) |
| 39 | #define to_cf_qspi_slave(s) container_of(s, struct cf_qspi_slave, s) |
| 40 | |
| 41 | struct cf_qspi_slave { |
| 42 | struct spi_slave slave; /* Specific bus:cs ID for each device */ |
| 43 | qspi_t *regs; /* Pointer to SPI controller registers */ |
| 44 | u16 qmr; /* QMR: Queued Mode Register */ |
| 45 | u16 qwr; /* QWR: Queued Wrap Register */ |
| 46 | u16 qcr; /* QCR: Queued Command Ram */ |
| 47 | }; |
| 48 | |
| 49 | /* Register write wrapper functions */ |
| 50 | static void write_qmr(volatile qspi_t *qspi, u16 val) { qspi->mr = val; } |
| 51 | static void write_qdlyr(volatile qspi_t *qspi, u16 val) { qspi->dlyr = val; } |
| 52 | static void write_qwr(volatile qspi_t *qspi, u16 val) { qspi->wr = val; } |
| 53 | static void write_qir(volatile qspi_t *qspi, u16 val) { qspi->ir = val; } |
| 54 | static void write_qar(volatile qspi_t *qspi, u16 val) { qspi->ar = val; } |
| 55 | static void write_qdr(volatile qspi_t *qspi, u16 val) { qspi->dr = val; } |
| 56 | /* Register read wrapper functions */ |
| 57 | static u16 read_qdlyr(volatile qspi_t *qspi) { return qspi->dlyr; } |
| 58 | static u16 read_qwr(volatile qspi_t *qspi) { return qspi->wr; } |
| 59 | static u16 read_qir(volatile qspi_t *qspi) { return qspi->ir; } |
| 60 | static u16 read_qdr(volatile qspi_t *qspi) { return qspi->dr; } |
| 61 | |
| 62 | /* These call points may be different for each ColdFire CPU */ |
| 63 | extern void cfspi_port_conf(void); |
| 64 | static void cfspi_cs_activate(uint bus, uint cs, uint cs_active_high); |
| 65 | static void cfspi_cs_deactivate(uint bus, uint cs, uint cs_active_high); |
| 66 | |
| 67 | int spi_claim_bus(struct spi_slave *slave) |
| 68 | { |
| 69 | return 0; |
| 70 | } |
| 71 | void spi_release_bus(struct spi_slave *slave) |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | __attribute__((weak)) |
| 76 | void spi_init(void) |
| 77 | { |
| 78 | cfspi_port_conf(); |
| 79 | } |
| 80 | |
| 81 | __attribute__((weak)) |
| 82 | void spi_cs_activate(struct spi_slave *slave) |
| 83 | { |
| 84 | struct cf_qspi_slave *dev = to_cf_qspi_slave(slave); |
| 85 | |
| 86 | cfspi_cs_activate(slave->bus, slave->cs, !(dev->qwr & QSPI_QWR_CSIV)); |
| 87 | } |
| 88 | |
| 89 | __attribute__((weak)) |
| 90 | void spi_cs_deactivate(struct spi_slave *slave) |
| 91 | { |
| 92 | struct cf_qspi_slave *dev = to_cf_qspi_slave(slave); |
| 93 | |
| 94 | cfspi_cs_deactivate(slave->bus, slave->cs, !(dev->qwr & QSPI_QWR_CSIV)); |
| 95 | } |
| 96 | |
| 97 | __attribute__((weak)) |
| 98 | int spi_cs_is_valid(unsigned int bus, unsigned int cs) |
| 99 | { |
| 100 | /* Only 1 bus and 4 chipselect per controller */ |
| 101 | if (bus == 0 && (cs >= 0 && cs < 4)) |
| 102 | return 1; |
| 103 | else |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | void spi_free_slave(struct spi_slave *slave) |
| 108 | { |
| 109 | struct cf_qspi_slave *dev = to_cf_qspi_slave(slave); |
| 110 | |
| 111 | free(dev); |
| 112 | } |
| 113 | |
| 114 | /* Translate information given by spi_setup_slave to members of cf_qspi_slave */ |
| 115 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 116 | unsigned int max_hz, unsigned int mode) |
| 117 | { |
| 118 | struct cf_qspi_slave *dev = NULL; |
| 119 | |
| 120 | if (!spi_cs_is_valid(bus, cs)) |
| 121 | return NULL; |
| 122 | |
Simon Glass | d034a95 | 2013-03-18 19:23:40 +0000 | [diff] [blame] | 123 | dev = spi_alloc_slave(struct cf_qspi_slave, bus, cs); |
Richard Retanubun | 9324138 | 2011-03-24 08:58:11 +0000 | [diff] [blame] | 124 | if (!dev) |
| 125 | return NULL; |
| 126 | |
| 127 | /* Initialize to known value */ |
Richard Retanubun | 9324138 | 2011-03-24 08:58:11 +0000 | [diff] [blame] | 128 | dev->regs = (qspi_t *)MMAP_QSPI; |
| 129 | dev->qmr = 0; |
| 130 | dev->qwr = 0; |
| 131 | dev->qcr = 0; |
| 132 | |
| 133 | |
| 134 | /* Map max_hz to QMR[BAUD] */ |
| 135 | if (max_hz == 0) /* Go as fast as possible */ |
| 136 | dev->qmr = 2u; |
| 137 | else /* Get the closest baud rate */ |
| 138 | dev->qmr = clamp(((gd->bus_clk >> 2) + max_hz - 1)/max_hz, |
| 139 | 2u, 255u); |
| 140 | |
| 141 | /* Map mode to QMR[CPOL] and QMR[CPHA] */ |
| 142 | if (mode & SPI_CPOL) |
| 143 | dev->qmr |= QSPI_QMR_CPOL; |
| 144 | |
| 145 | if (mode & SPI_CPHA) |
| 146 | dev->qmr |= QSPI_QMR_CPHA; |
| 147 | |
| 148 | /* Hardcode bit length to 8 bit per transter */ |
| 149 | dev->qmr |= QSPI_QMR_BITS_8; |
| 150 | |
| 151 | /* Set QMR[MSTR] to enable QSPI as master */ |
| 152 | dev->qmr |= QSPI_QMR_MSTR; |
| 153 | |
| 154 | /* |
| 155 | * Set QCR and QWR to default values for spi flash operation. |
| 156 | * If more custom QCR and QRW are needed, overload mode variable |
| 157 | */ |
| 158 | dev->qcr = (QSPI_QDR_CONT | QSPI_QDR_BITSE); |
| 159 | |
| 160 | if (!(mode & SPI_CS_HIGH)) |
| 161 | dev->qwr |= QSPI_QWR_CSIV; |
| 162 | |
| 163 | return &dev->slave; |
| 164 | } |
| 165 | |
| 166 | /* Transfer 8 bit at a time */ |
| 167 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, |
| 168 | void *din, unsigned long flags) |
| 169 | { |
| 170 | struct cf_qspi_slave *dev = to_cf_qspi_slave(slave); |
| 171 | volatile qspi_t *qspi = dev->regs; |
| 172 | u8 *txbuf = (u8 *)dout; |
| 173 | u8 *rxbuf = (u8 *)din; |
Axel Lin | 29180ba | 2013-06-14 21:12:19 +0800 | [diff] [blame] | 174 | u32 count = DIV_ROUND_UP(bitlen, 8); |
Richard Retanubun | 9324138 | 2011-03-24 08:58:11 +0000 | [diff] [blame] | 175 | u32 n, i = 0; |
| 176 | |
| 177 | /* Sanitize arguments */ |
| 178 | if (slave == NULL) { |
| 179 | printf("%s: NULL slave ptr\n", __func__); |
| 180 | return -1; |
| 181 | } |
| 182 | |
| 183 | if (flags & SPI_XFER_BEGIN) |
| 184 | spi_cs_activate(slave); |
| 185 | |
| 186 | /* There is something to send, lets process it. spi_xfer is also called |
| 187 | * just to toggle chip select, so bitlen of 0 is valid */ |
| 188 | if (count > 0) { |
| 189 | /* |
| 190 | * NOTE: Since chip select is driven as a bit-bang-ed GPIO |
| 191 | * using spi_cs_activate() and spi_cs_deactivate(), |
| 192 | * the chip select settings inside the controller |
| 193 | * (i.e. QCR[CONT] and QWR[CSIV]) are moot. The bits are set to |
| 194 | * keep the controller settings consistent with the actual |
| 195 | * operation of the bus. |
| 196 | */ |
| 197 | |
| 198 | /* Write the slave device's settings for the controller.*/ |
| 199 | write_qmr(qspi, dev->qmr); |
| 200 | write_qwr(qspi, dev->qwr); |
| 201 | |
| 202 | /* Limit transfer to 16 at a time */ |
| 203 | n = min(count, 16u); |
| 204 | do { |
| 205 | /* Setup queue end point */ |
| 206 | write_qwr(qspi, ((read_qwr(qspi) & QSPI_QWR_ENDQP_MASK) |
| 207 | | QSPI_QWR_ENDQP((n-1)))); |
| 208 | |
| 209 | /* Write Command RAM */ |
| 210 | write_qar(qspi, QSPI_QAR_CMD); |
| 211 | for (i = 0; i < n; ++i) |
| 212 | write_qdr(qspi, dev->qcr); |
| 213 | |
| 214 | /* Write TxBuf, if none given, fill with ZEROes */ |
| 215 | write_qar(qspi, QSPI_QAR_TRANS); |
| 216 | if (txbuf) { |
| 217 | for (i = 0; i < n; ++i) |
| 218 | write_qdr(qspi, *txbuf++); |
| 219 | } else { |
| 220 | for (i = 0; i < n; ++i) |
| 221 | write_qdr(qspi, 0); |
| 222 | } |
| 223 | |
| 224 | /* Clear QIR[SPIF] by writing a 1 to it */ |
| 225 | write_qir(qspi, read_qir(qspi) | QSPI_QIR_SPIF); |
| 226 | /* Set QDLYR[SPE] to start sending */ |
| 227 | write_qdlyr(qspi, read_qdlyr(qspi) | QSPI_QDLYR_SPE); |
| 228 | |
| 229 | /* Poll QIR[SPIF] for transfer completion */ |
| 230 | while ((read_qir(qspi) & QSPI_QIR_SPIF) != 1) |
| 231 | udelay(1); |
| 232 | |
| 233 | /* If given read RxBuf, load data to it */ |
| 234 | if (rxbuf) { |
| 235 | write_qar(qspi, QSPI_QAR_RECV); |
| 236 | for (i = 0; i < n; ++i) |
| 237 | *rxbuf++ = read_qdr(qspi); |
| 238 | } |
| 239 | |
| 240 | /* Decrement count */ |
| 241 | count -= n; |
| 242 | } while (count); |
| 243 | } |
| 244 | |
| 245 | if (flags & SPI_XFER_END) |
| 246 | spi_cs_deactivate(slave); |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | /* Each MCF CPU may have different pin assignments for chip selects. */ |
| 252 | #if defined(CONFIG_M5271) |
| 253 | /* Assert chip select, val = [1|0] , dir = out, mode = GPIO */ |
| 254 | void cfspi_cs_activate(uint bus, uint cs, uint cs_active_high) |
| 255 | { |
| 256 | debug("%s: bus %d cs %d cs_active_high %d\n", |
| 257 | __func__, bus, cs, cs_active_high); |
| 258 | |
| 259 | switch (cs) { |
| 260 | case 0: /* QSPI_CS[0] = PQSPI[3] */ |
| 261 | if (cs_active_high) |
| 262 | mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x08); |
| 263 | else |
| 264 | mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xF7); |
| 265 | |
| 266 | mbar_writeByte(MCF_GPIO_PDDR_QSPI, |
| 267 | mbar_readByte(MCF_GPIO_PDDR_QSPI) | 0x08); |
| 268 | |
| 269 | mbar_writeByte(MCF_GPIO_PAR_QSPI, |
| 270 | mbar_readByte(MCF_GPIO_PAR_QSPI) & 0xDF); |
| 271 | break; |
| 272 | case 1: /* QSPI_CS[1] = PQSPI[4] */ |
| 273 | if (cs_active_high) |
| 274 | mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x10); |
| 275 | else |
| 276 | mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xEF); |
| 277 | |
| 278 | mbar_writeByte(MCF_GPIO_PDDR_QSPI, |
| 279 | mbar_readByte(MCF_GPIO_PDDR_QSPI) | 0x10); |
| 280 | |
| 281 | mbar_writeByte(MCF_GPIO_PAR_QSPI, |
| 282 | mbar_readByte(MCF_GPIO_PAR_QSPI) & 0x3F); |
| 283 | break; |
| 284 | case 2: /* QSPI_CS[2] = PTIMER[7] */ |
| 285 | if (cs_active_high) |
| 286 | mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x80); |
| 287 | else |
| 288 | mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0x7F); |
| 289 | |
| 290 | mbar_writeByte(MCF_GPIO_PDDR_TIMER, |
| 291 | mbar_readByte(MCF_GPIO_PDDR_TIMER) | 0x80); |
| 292 | |
| 293 | mbar_writeShort(MCF_GPIO_PAR_TIMER, |
| 294 | mbar_readShort(MCF_GPIO_PAR_TIMER) & 0x3FFF); |
| 295 | break; |
| 296 | case 3: /* QSPI_CS[3] = PTIMER[3] */ |
| 297 | if (cs_active_high) |
| 298 | mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x08); |
| 299 | else |
| 300 | mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0xF7); |
| 301 | |
| 302 | mbar_writeByte(MCF_GPIO_PDDR_TIMER, |
| 303 | mbar_readByte(MCF_GPIO_PDDR_TIMER) | 0x08); |
| 304 | |
| 305 | mbar_writeShort(MCF_GPIO_PAR_TIMER, |
| 306 | mbar_readShort(MCF_GPIO_PAR_TIMER) & 0xFF3F); |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /* Deassert chip select, val = [1|0], dir = in, mode = GPIO |
| 312 | * direction set as IN to undrive the pin, external pullup/pulldown will bring |
| 313 | * bus to deassert state. |
| 314 | */ |
| 315 | void cfspi_cs_deactivate(uint bus, uint cs, uint cs_active_high) |
| 316 | { |
| 317 | debug("%s: bus %d cs %d cs_active_high %d\n", |
| 318 | __func__, bus, cs, cs_active_high); |
| 319 | |
| 320 | switch (cs) { |
| 321 | case 0: /* QSPI_CS[0] = PQSPI[3] */ |
| 322 | if (cs_active_high) |
| 323 | mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xF7); |
| 324 | else |
| 325 | mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x08); |
| 326 | |
| 327 | mbar_writeByte(MCF_GPIO_PDDR_QSPI, |
| 328 | mbar_readByte(MCF_GPIO_PDDR_QSPI) & 0xF7); |
| 329 | |
| 330 | mbar_writeByte(MCF_GPIO_PAR_QSPI, |
| 331 | mbar_readByte(MCF_GPIO_PAR_QSPI) & 0xDF); |
| 332 | break; |
| 333 | case 1: /* QSPI_CS[1] = PQSPI[4] */ |
| 334 | if (cs_active_high) |
| 335 | mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xEF); |
| 336 | else |
| 337 | mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x10); |
| 338 | |
| 339 | mbar_writeByte(MCF_GPIO_PDDR_QSPI, |
| 340 | mbar_readByte(MCF_GPIO_PDDR_QSPI) & 0xEF); |
| 341 | |
| 342 | mbar_writeByte(MCF_GPIO_PAR_QSPI, |
| 343 | mbar_readByte(MCF_GPIO_PAR_QSPI) & 0x3F); |
| 344 | break; |
| 345 | case 2: /* QSPI_CS[2] = PTIMER[7] */ |
| 346 | if (cs_active_high) |
| 347 | mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0x7F); |
| 348 | else |
| 349 | mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x80); |
| 350 | |
| 351 | mbar_writeByte(MCF_GPIO_PDDR_TIMER, |
| 352 | mbar_readByte(MCF_GPIO_PDDR_TIMER) & 0x7F); |
| 353 | |
| 354 | mbar_writeShort(MCF_GPIO_PAR_TIMER, |
| 355 | mbar_readShort(MCF_GPIO_PAR_TIMER) & 0x3FFF); |
| 356 | break; |
| 357 | case 3: /* QSPI_CS[3] = PTIMER[3] */ |
| 358 | if (cs_active_high) |
| 359 | mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0xF7); |
| 360 | else |
| 361 | mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x08); |
| 362 | |
| 363 | mbar_writeByte(MCF_GPIO_PDDR_TIMER, |
| 364 | mbar_readByte(MCF_GPIO_PDDR_TIMER) & 0xF7); |
| 365 | |
| 366 | mbar_writeShort(MCF_GPIO_PAR_TIMER, |
| 367 | mbar_readShort(MCF_GPIO_PAR_TIMER) & 0xFF3F); |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | #endif /* CONFIG_M5271 */ |