2 * Freescale Coldfire Queued SPI driver
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.
8 * Copyright (C) 2011 Ruggedcom, Inc.
9 * Richard Retanubun (richardretanubun@freescale.com)
11 * SPDX-License-Identifier: GPL-2.0+
17 #include <asm/immap.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 #define to_cf_qspi_slave(s) container_of(s, struct cf_qspi_slave, slave)
24 struct cf_qspi_slave {
25 struct spi_slave slave; /* Specific bus:cs ID for each device */
26 qspi_t *regs; /* Pointer to SPI controller registers */
27 u16 qmr; /* QMR: Queued Mode Register */
28 u16 qwr; /* QWR: Queued Wrap Register */
29 u16 qcr; /* QCR: Queued Command Ram */
32 /* Register write wrapper functions */
33 static void write_qmr(volatile qspi_t *qspi, u16 val) { qspi->mr = val; }
34 static void write_qdlyr(volatile qspi_t *qspi, u16 val) { qspi->dlyr = val; }
35 static void write_qwr(volatile qspi_t *qspi, u16 val) { qspi->wr = val; }
36 static void write_qir(volatile qspi_t *qspi, u16 val) { qspi->ir = val; }
37 static void write_qar(volatile qspi_t *qspi, u16 val) { qspi->ar = val; }
38 static void write_qdr(volatile qspi_t *qspi, u16 val) { qspi->dr = val; }
39 /* Register read wrapper functions */
40 static u16 read_qdlyr(volatile qspi_t *qspi) { return qspi->dlyr; }
41 static u16 read_qwr(volatile qspi_t *qspi) { return qspi->wr; }
42 static u16 read_qir(volatile qspi_t *qspi) { return qspi->ir; }
43 static u16 read_qdr(volatile qspi_t *qspi) { return qspi->dr; }
45 /* These call points may be different for each ColdFire CPU */
46 extern void cfspi_port_conf(void);
47 static void cfspi_cs_activate(uint bus, uint cs, uint cs_active_high);
48 static void cfspi_cs_deactivate(uint bus, uint cs, uint cs_active_high);
50 int spi_claim_bus(struct spi_slave *slave)
54 void spi_release_bus(struct spi_slave *slave)
65 void spi_cs_activate(struct spi_slave *slave)
67 struct cf_qspi_slave *dev = to_cf_qspi_slave(slave);
69 cfspi_cs_activate(slave->bus, slave->cs, !(dev->qwr & QSPI_QWR_CSIV));
73 void spi_cs_deactivate(struct spi_slave *slave)
75 struct cf_qspi_slave *dev = to_cf_qspi_slave(slave);
77 cfspi_cs_deactivate(slave->bus, slave->cs, !(dev->qwr & QSPI_QWR_CSIV));
81 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
83 /* Only 1 bus and 4 chipselect per controller */
84 if (bus == 0 && (cs >= 0 && cs < 4))
90 void spi_free_slave(struct spi_slave *slave)
92 struct cf_qspi_slave *dev = to_cf_qspi_slave(slave);
97 /* Translate information given by spi_setup_slave to members of cf_qspi_slave */
98 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
99 unsigned int max_hz, unsigned int mode)
101 struct cf_qspi_slave *dev = NULL;
103 if (!spi_cs_is_valid(bus, cs))
106 dev = spi_alloc_slave(struct cf_qspi_slave, bus, cs);
110 /* Initialize to known value */
111 dev->regs = (qspi_t *)MMAP_QSPI;
117 /* Map max_hz to QMR[BAUD] */
118 if (max_hz == 0) /* Go as fast as possible */
120 else /* Get the closest baud rate */
121 dev->qmr = clamp(((gd->bus_clk >> 2) + max_hz - 1)/max_hz,
124 /* Map mode to QMR[CPOL] and QMR[CPHA] */
126 dev->qmr |= QSPI_QMR_CPOL;
129 dev->qmr |= QSPI_QMR_CPHA;
131 /* Hardcode bit length to 8 bit per transter */
132 dev->qmr |= QSPI_QMR_BITS_8;
134 /* Set QMR[MSTR] to enable QSPI as master */
135 dev->qmr |= QSPI_QMR_MSTR;
138 * Set QCR and QWR to default values for spi flash operation.
139 * If more custom QCR and QRW are needed, overload mode variable
141 dev->qcr = (QSPI_QDR_CONT | QSPI_QDR_BITSE);
143 if (!(mode & SPI_CS_HIGH))
144 dev->qwr |= QSPI_QWR_CSIV;
149 /* Transfer 8 bit at a time */
150 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
151 void *din, unsigned long flags)
153 struct cf_qspi_slave *dev = to_cf_qspi_slave(slave);
154 volatile qspi_t *qspi = dev->regs;
155 u8 *txbuf = (u8 *)dout;
156 u8 *rxbuf = (u8 *)din;
157 u32 count = DIV_ROUND_UP(bitlen, 8);
160 /* Sanitize arguments */
162 printf("%s: NULL slave ptr\n", __func__);
166 if (flags & SPI_XFER_BEGIN)
167 spi_cs_activate(slave);
169 /* There is something to send, lets process it. spi_xfer is also called
170 * just to toggle chip select, so bitlen of 0 is valid */
173 * NOTE: Since chip select is driven as a bit-bang-ed GPIO
174 * using spi_cs_activate() and spi_cs_deactivate(),
175 * the chip select settings inside the controller
176 * (i.e. QCR[CONT] and QWR[CSIV]) are moot. The bits are set to
177 * keep the controller settings consistent with the actual
178 * operation of the bus.
181 /* Write the slave device's settings for the controller.*/
182 write_qmr(qspi, dev->qmr);
183 write_qwr(qspi, dev->qwr);
185 /* Limit transfer to 16 at a time */
188 /* Setup queue end point */
189 write_qwr(qspi, ((read_qwr(qspi) & QSPI_QWR_ENDQP_MASK)
190 | QSPI_QWR_ENDQP((n-1))));
192 /* Write Command RAM */
193 write_qar(qspi, QSPI_QAR_CMD);
194 for (i = 0; i < n; ++i)
195 write_qdr(qspi, dev->qcr);
197 /* Write TxBuf, if none given, fill with ZEROes */
198 write_qar(qspi, QSPI_QAR_TRANS);
200 for (i = 0; i < n; ++i)
201 write_qdr(qspi, *txbuf++);
203 for (i = 0; i < n; ++i)
207 /* Clear QIR[SPIF] by writing a 1 to it */
208 write_qir(qspi, read_qir(qspi) | QSPI_QIR_SPIF);
209 /* Set QDLYR[SPE] to start sending */
210 write_qdlyr(qspi, read_qdlyr(qspi) | QSPI_QDLYR_SPE);
212 /* Poll QIR[SPIF] for transfer completion */
213 while ((read_qir(qspi) & QSPI_QIR_SPIF) != 1)
216 /* If given read RxBuf, load data to it */
218 write_qar(qspi, QSPI_QAR_RECV);
219 for (i = 0; i < n; ++i)
220 *rxbuf++ = read_qdr(qspi);
223 /* Decrement count */
228 if (flags & SPI_XFER_END)
229 spi_cs_deactivate(slave);
234 /* Each MCF CPU may have different pin assignments for chip selects. */
235 #if defined(CONFIG_M5271)
236 /* Assert chip select, val = [1|0] , dir = out, mode = GPIO */
237 void cfspi_cs_activate(uint bus, uint cs, uint cs_active_high)
239 debug("%s: bus %d cs %d cs_active_high %d\n",
240 __func__, bus, cs, cs_active_high);
243 case 0: /* QSPI_CS[0] = PQSPI[3] */
245 mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x08);
247 mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xF7);
249 mbar_writeByte(MCF_GPIO_PDDR_QSPI,
250 mbar_readByte(MCF_GPIO_PDDR_QSPI) | 0x08);
252 mbar_writeByte(MCF_GPIO_PAR_QSPI,
253 mbar_readByte(MCF_GPIO_PAR_QSPI) & 0xDF);
255 case 1: /* QSPI_CS[1] = PQSPI[4] */
257 mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x10);
259 mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xEF);
261 mbar_writeByte(MCF_GPIO_PDDR_QSPI,
262 mbar_readByte(MCF_GPIO_PDDR_QSPI) | 0x10);
264 mbar_writeByte(MCF_GPIO_PAR_QSPI,
265 mbar_readByte(MCF_GPIO_PAR_QSPI) & 0x3F);
267 case 2: /* QSPI_CS[2] = PTIMER[7] */
269 mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x80);
271 mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0x7F);
273 mbar_writeByte(MCF_GPIO_PDDR_TIMER,
274 mbar_readByte(MCF_GPIO_PDDR_TIMER) | 0x80);
276 mbar_writeShort(MCF_GPIO_PAR_TIMER,
277 mbar_readShort(MCF_GPIO_PAR_TIMER) & 0x3FFF);
279 case 3: /* QSPI_CS[3] = PTIMER[3] */
281 mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x08);
283 mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0xF7);
285 mbar_writeByte(MCF_GPIO_PDDR_TIMER,
286 mbar_readByte(MCF_GPIO_PDDR_TIMER) | 0x08);
288 mbar_writeShort(MCF_GPIO_PAR_TIMER,
289 mbar_readShort(MCF_GPIO_PAR_TIMER) & 0xFF3F);
294 /* Deassert chip select, val = [1|0], dir = in, mode = GPIO
295 * direction set as IN to undrive the pin, external pullup/pulldown will bring
296 * bus to deassert state.
298 void cfspi_cs_deactivate(uint bus, uint cs, uint cs_active_high)
300 debug("%s: bus %d cs %d cs_active_high %d\n",
301 __func__, bus, cs, cs_active_high);
304 case 0: /* QSPI_CS[0] = PQSPI[3] */
306 mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xF7);
308 mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x08);
310 mbar_writeByte(MCF_GPIO_PDDR_QSPI,
311 mbar_readByte(MCF_GPIO_PDDR_QSPI) & 0xF7);
313 mbar_writeByte(MCF_GPIO_PAR_QSPI,
314 mbar_readByte(MCF_GPIO_PAR_QSPI) & 0xDF);
316 case 1: /* QSPI_CS[1] = PQSPI[4] */
318 mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xEF);
320 mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x10);
322 mbar_writeByte(MCF_GPIO_PDDR_QSPI,
323 mbar_readByte(MCF_GPIO_PDDR_QSPI) & 0xEF);
325 mbar_writeByte(MCF_GPIO_PAR_QSPI,
326 mbar_readByte(MCF_GPIO_PAR_QSPI) & 0x3F);
328 case 2: /* QSPI_CS[2] = PTIMER[7] */
330 mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0x7F);
332 mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x80);
334 mbar_writeByte(MCF_GPIO_PDDR_TIMER,
335 mbar_readByte(MCF_GPIO_PDDR_TIMER) & 0x7F);
337 mbar_writeShort(MCF_GPIO_PAR_TIMER,
338 mbar_readShort(MCF_GPIO_PAR_TIMER) & 0x3FFF);
340 case 3: /* QSPI_CS[3] = PTIMER[3] */
342 mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0xF7);
344 mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x08);
346 mbar_writeByte(MCF_GPIO_PDDR_TIMER,
347 mbar_readByte(MCF_GPIO_PDDR_TIMER) & 0xF7);
349 mbar_writeShort(MCF_GPIO_PAR_TIMER,
350 mbar_readShort(MCF_GPIO_PAR_TIMER) & 0xFF3F);
354 #endif /* CONFIG_M5271 */