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.
9 * Copyright (C) 2011 Ruggedcom, Inc.
10 * Richard Retanubun (richardretanubun@freescale.com)
12 * See file CREDITS for list of people who contributed to this project.
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.
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.
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,
33 #include <asm/immap.h>
36 DECLARE_GLOBAL_DATA_PTR;
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)
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 */
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; }
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);
67 int spi_claim_bus(struct spi_slave *slave)
71 void spi_release_bus(struct spi_slave *slave)
82 void spi_cs_activate(struct spi_slave *slave)
84 struct cf_qspi_slave *dev = to_cf_qspi_slave(slave);
86 cfspi_cs_activate(slave->bus, slave->cs, !(dev->qwr & QSPI_QWR_CSIV));
90 void spi_cs_deactivate(struct spi_slave *slave)
92 struct cf_qspi_slave *dev = to_cf_qspi_slave(slave);
94 cfspi_cs_deactivate(slave->bus, slave->cs, !(dev->qwr & QSPI_QWR_CSIV));
98 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
100 /* Only 1 bus and 4 chipselect per controller */
101 if (bus == 0 && (cs >= 0 && cs < 4))
107 void spi_free_slave(struct spi_slave *slave)
109 struct cf_qspi_slave *dev = to_cf_qspi_slave(slave);
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)
118 struct cf_qspi_slave *dev = NULL;
120 if (!spi_cs_is_valid(bus, cs))
123 dev = spi_alloc_slave(struct cf_qspi_slave, bus, cs);
127 /* Initialize to known value */
128 dev->regs = (qspi_t *)MMAP_QSPI;
134 /* Map max_hz to QMR[BAUD] */
135 if (max_hz == 0) /* Go as fast as possible */
137 else /* Get the closest baud rate */
138 dev->qmr = clamp(((gd->bus_clk >> 2) + max_hz - 1)/max_hz,
141 /* Map mode to QMR[CPOL] and QMR[CPHA] */
143 dev->qmr |= QSPI_QMR_CPOL;
146 dev->qmr |= QSPI_QMR_CPHA;
148 /* Hardcode bit length to 8 bit per transter */
149 dev->qmr |= QSPI_QMR_BITS_8;
151 /* Set QMR[MSTR] to enable QSPI as master */
152 dev->qmr |= QSPI_QMR_MSTR;
155 * Set QCR and QWR to default values for spi flash operation.
156 * If more custom QCR and QRW are needed, overload mode variable
158 dev->qcr = (QSPI_QDR_CONT | QSPI_QDR_BITSE);
160 if (!(mode & SPI_CS_HIGH))
161 dev->qwr |= QSPI_QWR_CSIV;
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)
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;
174 u32 count = DIV_ROUND_UP(bitlen, 8);
177 /* Sanitize arguments */
179 printf("%s: NULL slave ptr\n", __func__);
183 if (flags & SPI_XFER_BEGIN)
184 spi_cs_activate(slave);
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 */
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.
198 /* Write the slave device's settings for the controller.*/
199 write_qmr(qspi, dev->qmr);
200 write_qwr(qspi, dev->qwr);
202 /* Limit transfer to 16 at a time */
205 /* Setup queue end point */
206 write_qwr(qspi, ((read_qwr(qspi) & QSPI_QWR_ENDQP_MASK)
207 | QSPI_QWR_ENDQP((n-1))));
209 /* Write Command RAM */
210 write_qar(qspi, QSPI_QAR_CMD);
211 for (i = 0; i < n; ++i)
212 write_qdr(qspi, dev->qcr);
214 /* Write TxBuf, if none given, fill with ZEROes */
215 write_qar(qspi, QSPI_QAR_TRANS);
217 for (i = 0; i < n; ++i)
218 write_qdr(qspi, *txbuf++);
220 for (i = 0; i < n; ++i)
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);
229 /* Poll QIR[SPIF] for transfer completion */
230 while ((read_qir(qspi) & QSPI_QIR_SPIF) != 1)
233 /* If given read RxBuf, load data to it */
235 write_qar(qspi, QSPI_QAR_RECV);
236 for (i = 0; i < n; ++i)
237 *rxbuf++ = read_qdr(qspi);
240 /* Decrement count */
245 if (flags & SPI_XFER_END)
246 spi_cs_deactivate(slave);
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)
256 debug("%s: bus %d cs %d cs_active_high %d\n",
257 __func__, bus, cs, cs_active_high);
260 case 0: /* QSPI_CS[0] = PQSPI[3] */
262 mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x08);
264 mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xF7);
266 mbar_writeByte(MCF_GPIO_PDDR_QSPI,
267 mbar_readByte(MCF_GPIO_PDDR_QSPI) | 0x08);
269 mbar_writeByte(MCF_GPIO_PAR_QSPI,
270 mbar_readByte(MCF_GPIO_PAR_QSPI) & 0xDF);
272 case 1: /* QSPI_CS[1] = PQSPI[4] */
274 mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x10);
276 mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xEF);
278 mbar_writeByte(MCF_GPIO_PDDR_QSPI,
279 mbar_readByte(MCF_GPIO_PDDR_QSPI) | 0x10);
281 mbar_writeByte(MCF_GPIO_PAR_QSPI,
282 mbar_readByte(MCF_GPIO_PAR_QSPI) & 0x3F);
284 case 2: /* QSPI_CS[2] = PTIMER[7] */
286 mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x80);
288 mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0x7F);
290 mbar_writeByte(MCF_GPIO_PDDR_TIMER,
291 mbar_readByte(MCF_GPIO_PDDR_TIMER) | 0x80);
293 mbar_writeShort(MCF_GPIO_PAR_TIMER,
294 mbar_readShort(MCF_GPIO_PAR_TIMER) & 0x3FFF);
296 case 3: /* QSPI_CS[3] = PTIMER[3] */
298 mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x08);
300 mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0xF7);
302 mbar_writeByte(MCF_GPIO_PDDR_TIMER,
303 mbar_readByte(MCF_GPIO_PDDR_TIMER) | 0x08);
305 mbar_writeShort(MCF_GPIO_PAR_TIMER,
306 mbar_readShort(MCF_GPIO_PAR_TIMER) & 0xFF3F);
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.
315 void cfspi_cs_deactivate(uint bus, uint cs, uint cs_active_high)
317 debug("%s: bus %d cs %d cs_active_high %d\n",
318 __func__, bus, cs, cs_active_high);
321 case 0: /* QSPI_CS[0] = PQSPI[3] */
323 mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xF7);
325 mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x08);
327 mbar_writeByte(MCF_GPIO_PDDR_QSPI,
328 mbar_readByte(MCF_GPIO_PDDR_QSPI) & 0xF7);
330 mbar_writeByte(MCF_GPIO_PAR_QSPI,
331 mbar_readByte(MCF_GPIO_PAR_QSPI) & 0xDF);
333 case 1: /* QSPI_CS[1] = PQSPI[4] */
335 mbar_writeByte(MCF_GPIO_PCLRR_QSPI, 0xEF);
337 mbar_writeByte(MCF_GPIO_PPDSDR_QSPI, 0x10);
339 mbar_writeByte(MCF_GPIO_PDDR_QSPI,
340 mbar_readByte(MCF_GPIO_PDDR_QSPI) & 0xEF);
342 mbar_writeByte(MCF_GPIO_PAR_QSPI,
343 mbar_readByte(MCF_GPIO_PAR_QSPI) & 0x3F);
345 case 2: /* QSPI_CS[2] = PTIMER[7] */
347 mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0x7F);
349 mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x80);
351 mbar_writeByte(MCF_GPIO_PDDR_TIMER,
352 mbar_readByte(MCF_GPIO_PDDR_TIMER) & 0x7F);
354 mbar_writeShort(MCF_GPIO_PAR_TIMER,
355 mbar_readShort(MCF_GPIO_PAR_TIMER) & 0x3FFF);
357 case 3: /* QSPI_CS[3] = PTIMER[3] */
359 mbar_writeByte(MCF_GPIO_PCLRR_TIMER, 0xF7);
361 mbar_writeByte(MCF_GPIO_PPDSDR_TIMER, 0x08);
363 mbar_writeByte(MCF_GPIO_PDDR_TIMER,
364 mbar_readByte(MCF_GPIO_PDDR_TIMER) & 0xF7);
366 mbar_writeShort(MCF_GPIO_PAR_TIMER,
367 mbar_readShort(MCF_GPIO_PAR_TIMER) & 0xFF3F);
371 #endif /* CONFIG_M5271 */