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 clamp(x, low, high) (min(max(low, x), high))
23 #define to_cf_qspi_slave(s) container_of(s, struct cf_qspi_slave, s)
25 struct cf_qspi_slave
{
26 struct spi_slave slave
; /* Specific bus:cs ID for each device */
27 qspi_t
*regs
; /* Pointer to SPI controller registers */
28 u16 qmr
; /* QMR: Queued Mode Register */
29 u16 qwr
; /* QWR: Queued Wrap Register */
30 u16 qcr
; /* QCR: Queued Command Ram */
33 /* Register write wrapper functions */
34 static void write_qmr(volatile qspi_t
*qspi
, u16 val
) { qspi
->mr
= val
; }
35 static void write_qdlyr(volatile qspi_t
*qspi
, u16 val
) { qspi
->dlyr
= val
; }
36 static void write_qwr(volatile qspi_t
*qspi
, u16 val
) { qspi
->wr
= val
; }
37 static void write_qir(volatile qspi_t
*qspi
, u16 val
) { qspi
->ir
= val
; }
38 static void write_qar(volatile qspi_t
*qspi
, u16 val
) { qspi
->ar
= val
; }
39 static void write_qdr(volatile qspi_t
*qspi
, u16 val
) { qspi
->dr
= val
; }
40 /* Register read wrapper functions */
41 static u16
read_qdlyr(volatile qspi_t
*qspi
) { return qspi
->dlyr
; }
42 static u16
read_qwr(volatile qspi_t
*qspi
) { return qspi
->wr
; }
43 static u16
read_qir(volatile qspi_t
*qspi
) { return qspi
->ir
; }
44 static u16
read_qdr(volatile qspi_t
*qspi
) { return qspi
->dr
; }
46 /* These call points may be different for each ColdFire CPU */
47 extern void cfspi_port_conf(void);
48 static void cfspi_cs_activate(uint bus
, uint cs
, uint cs_active_high
);
49 static void cfspi_cs_deactivate(uint bus
, uint cs
, uint cs_active_high
);
51 int spi_claim_bus(struct spi_slave
*slave
)
55 void spi_release_bus(struct spi_slave
*slave
)
66 void spi_cs_activate(struct spi_slave
*slave
)
68 struct cf_qspi_slave
*dev
= to_cf_qspi_slave(slave
);
70 cfspi_cs_activate(slave
->bus
, slave
->cs
, !(dev
->qwr
& QSPI_QWR_CSIV
));
74 void spi_cs_deactivate(struct spi_slave
*slave
)
76 struct cf_qspi_slave
*dev
= to_cf_qspi_slave(slave
);
78 cfspi_cs_deactivate(slave
->bus
, slave
->cs
, !(dev
->qwr
& QSPI_QWR_CSIV
));
82 int spi_cs_is_valid(unsigned int bus
, unsigned int cs
)
84 /* Only 1 bus and 4 chipselect per controller */
85 if (bus
== 0 && (cs
>= 0 && cs
< 4))
91 void spi_free_slave(struct spi_slave
*slave
)
93 struct cf_qspi_slave
*dev
= to_cf_qspi_slave(slave
);
98 /* Translate information given by spi_setup_slave to members of cf_qspi_slave */
99 struct spi_slave
*spi_setup_slave(unsigned int bus
, unsigned int cs
,
100 unsigned int max_hz
, unsigned int mode
)
102 struct cf_qspi_slave
*dev
= NULL
;
104 if (!spi_cs_is_valid(bus
, cs
))
107 dev
= spi_alloc_slave(struct cf_qspi_slave
, bus
, cs
);
111 /* Initialize to known value */
112 dev
->regs
= (qspi_t
*)MMAP_QSPI
;
118 /* Map max_hz to QMR[BAUD] */
119 if (max_hz
== 0) /* Go as fast as possible */
121 else /* Get the closest baud rate */
122 dev
->qmr
= clamp(((gd
->bus_clk
>> 2) + max_hz
- 1)/max_hz
,
125 /* Map mode to QMR[CPOL] and QMR[CPHA] */
127 dev
->qmr
|= QSPI_QMR_CPOL
;
130 dev
->qmr
|= QSPI_QMR_CPHA
;
132 /* Hardcode bit length to 8 bit per transter */
133 dev
->qmr
|= QSPI_QMR_BITS_8
;
135 /* Set QMR[MSTR] to enable QSPI as master */
136 dev
->qmr
|= QSPI_QMR_MSTR
;
139 * Set QCR and QWR to default values for spi flash operation.
140 * If more custom QCR and QRW are needed, overload mode variable
142 dev
->qcr
= (QSPI_QDR_CONT
| QSPI_QDR_BITSE
);
144 if (!(mode
& SPI_CS_HIGH
))
145 dev
->qwr
|= QSPI_QWR_CSIV
;
150 /* Transfer 8 bit at a time */
151 int spi_xfer(struct spi_slave
*slave
, unsigned int bitlen
, const void *dout
,
152 void *din
, unsigned long flags
)
154 struct cf_qspi_slave
*dev
= to_cf_qspi_slave(slave
);
155 volatile qspi_t
*qspi
= dev
->regs
;
156 u8
*txbuf
= (u8
*)dout
;
157 u8
*rxbuf
= (u8
*)din
;
158 u32 count
= DIV_ROUND_UP(bitlen
, 8);
161 /* Sanitize arguments */
163 printf("%s: NULL slave ptr\n", __func__
);
167 if (flags
& SPI_XFER_BEGIN
)
168 spi_cs_activate(slave
);
170 /* There is something to send, lets process it. spi_xfer is also called
171 * just to toggle chip select, so bitlen of 0 is valid */
174 * NOTE: Since chip select is driven as a bit-bang-ed GPIO
175 * using spi_cs_activate() and spi_cs_deactivate(),
176 * the chip select settings inside the controller
177 * (i.e. QCR[CONT] and QWR[CSIV]) are moot. The bits are set to
178 * keep the controller settings consistent with the actual
179 * operation of the bus.
182 /* Write the slave device's settings for the controller.*/
183 write_qmr(qspi
, dev
->qmr
);
184 write_qwr(qspi
, dev
->qwr
);
186 /* Limit transfer to 16 at a time */
189 /* Setup queue end point */
190 write_qwr(qspi
, ((read_qwr(qspi
) & QSPI_QWR_ENDQP_MASK
)
191 | QSPI_QWR_ENDQP((n
-1))));
193 /* Write Command RAM */
194 write_qar(qspi
, QSPI_QAR_CMD
);
195 for (i
= 0; i
< n
; ++i
)
196 write_qdr(qspi
, dev
->qcr
);
198 /* Write TxBuf, if none given, fill with ZEROes */
199 write_qar(qspi
, QSPI_QAR_TRANS
);
201 for (i
= 0; i
< n
; ++i
)
202 write_qdr(qspi
, *txbuf
++);
204 for (i
= 0; i
< n
; ++i
)
208 /* Clear QIR[SPIF] by writing a 1 to it */
209 write_qir(qspi
, read_qir(qspi
) | QSPI_QIR_SPIF
);
210 /* Set QDLYR[SPE] to start sending */
211 write_qdlyr(qspi
, read_qdlyr(qspi
) | QSPI_QDLYR_SPE
);
213 /* Poll QIR[SPIF] for transfer completion */
214 while ((read_qir(qspi
) & QSPI_QIR_SPIF
) != 1)
217 /* If given read RxBuf, load data to it */
219 write_qar(qspi
, QSPI_QAR_RECV
);
220 for (i
= 0; i
< n
; ++i
)
221 *rxbuf
++ = read_qdr(qspi
);
224 /* Decrement count */
229 if (flags
& SPI_XFER_END
)
230 spi_cs_deactivate(slave
);
235 /* Each MCF CPU may have different pin assignments for chip selects. */
236 #if defined(CONFIG_M5271)
237 /* Assert chip select, val = [1|0] , dir = out, mode = GPIO */
238 void cfspi_cs_activate(uint bus
, uint cs
, uint cs_active_high
)
240 debug("%s: bus %d cs %d cs_active_high %d\n",
241 __func__
, bus
, cs
, cs_active_high
);
244 case 0: /* QSPI_CS[0] = PQSPI[3] */
246 mbar_writeByte(MCF_GPIO_PPDSDR_QSPI
, 0x08);
248 mbar_writeByte(MCF_GPIO_PCLRR_QSPI
, 0xF7);
250 mbar_writeByte(MCF_GPIO_PDDR_QSPI
,
251 mbar_readByte(MCF_GPIO_PDDR_QSPI
) | 0x08);
253 mbar_writeByte(MCF_GPIO_PAR_QSPI
,
254 mbar_readByte(MCF_GPIO_PAR_QSPI
) & 0xDF);
256 case 1: /* QSPI_CS[1] = PQSPI[4] */
258 mbar_writeByte(MCF_GPIO_PPDSDR_QSPI
, 0x10);
260 mbar_writeByte(MCF_GPIO_PCLRR_QSPI
, 0xEF);
262 mbar_writeByte(MCF_GPIO_PDDR_QSPI
,
263 mbar_readByte(MCF_GPIO_PDDR_QSPI
) | 0x10);
265 mbar_writeByte(MCF_GPIO_PAR_QSPI
,
266 mbar_readByte(MCF_GPIO_PAR_QSPI
) & 0x3F);
268 case 2: /* QSPI_CS[2] = PTIMER[7] */
270 mbar_writeByte(MCF_GPIO_PPDSDR_TIMER
, 0x80);
272 mbar_writeByte(MCF_GPIO_PCLRR_TIMER
, 0x7F);
274 mbar_writeByte(MCF_GPIO_PDDR_TIMER
,
275 mbar_readByte(MCF_GPIO_PDDR_TIMER
) | 0x80);
277 mbar_writeShort(MCF_GPIO_PAR_TIMER
,
278 mbar_readShort(MCF_GPIO_PAR_TIMER
) & 0x3FFF);
280 case 3: /* QSPI_CS[3] = PTIMER[3] */
282 mbar_writeByte(MCF_GPIO_PPDSDR_TIMER
, 0x08);
284 mbar_writeByte(MCF_GPIO_PCLRR_TIMER
, 0xF7);
286 mbar_writeByte(MCF_GPIO_PDDR_TIMER
,
287 mbar_readByte(MCF_GPIO_PDDR_TIMER
) | 0x08);
289 mbar_writeShort(MCF_GPIO_PAR_TIMER
,
290 mbar_readShort(MCF_GPIO_PAR_TIMER
) & 0xFF3F);
295 /* Deassert chip select, val = [1|0], dir = in, mode = GPIO
296 * direction set as IN to undrive the pin, external pullup/pulldown will bring
297 * bus to deassert state.
299 void cfspi_cs_deactivate(uint bus
, uint cs
, uint cs_active_high
)
301 debug("%s: bus %d cs %d cs_active_high %d\n",
302 __func__
, bus
, cs
, cs_active_high
);
305 case 0: /* QSPI_CS[0] = PQSPI[3] */
307 mbar_writeByte(MCF_GPIO_PCLRR_QSPI
, 0xF7);
309 mbar_writeByte(MCF_GPIO_PPDSDR_QSPI
, 0x08);
311 mbar_writeByte(MCF_GPIO_PDDR_QSPI
,
312 mbar_readByte(MCF_GPIO_PDDR_QSPI
) & 0xF7);
314 mbar_writeByte(MCF_GPIO_PAR_QSPI
,
315 mbar_readByte(MCF_GPIO_PAR_QSPI
) & 0xDF);
317 case 1: /* QSPI_CS[1] = PQSPI[4] */
319 mbar_writeByte(MCF_GPIO_PCLRR_QSPI
, 0xEF);
321 mbar_writeByte(MCF_GPIO_PPDSDR_QSPI
, 0x10);
323 mbar_writeByte(MCF_GPIO_PDDR_QSPI
,
324 mbar_readByte(MCF_GPIO_PDDR_QSPI
) & 0xEF);
326 mbar_writeByte(MCF_GPIO_PAR_QSPI
,
327 mbar_readByte(MCF_GPIO_PAR_QSPI
) & 0x3F);
329 case 2: /* QSPI_CS[2] = PTIMER[7] */
331 mbar_writeByte(MCF_GPIO_PCLRR_TIMER
, 0x7F);
333 mbar_writeByte(MCF_GPIO_PPDSDR_TIMER
, 0x80);
335 mbar_writeByte(MCF_GPIO_PDDR_TIMER
,
336 mbar_readByte(MCF_GPIO_PDDR_TIMER
) & 0x7F);
338 mbar_writeShort(MCF_GPIO_PAR_TIMER
,
339 mbar_readShort(MCF_GPIO_PAR_TIMER
) & 0x3FFF);
341 case 3: /* QSPI_CS[3] = PTIMER[3] */
343 mbar_writeByte(MCF_GPIO_PCLRR_TIMER
, 0xF7);
345 mbar_writeByte(MCF_GPIO_PPDSDR_TIMER
, 0x08);
347 mbar_writeByte(MCF_GPIO_PDDR_TIMER
,
348 mbar_readByte(MCF_GPIO_PDDR_TIMER
) & 0xF7);
350 mbar_writeShort(MCF_GPIO_PAR_TIMER
,
351 mbar_readShort(MCF_GPIO_PAR_TIMER
) & 0xFF3F);
355 #endif /* CONFIG_M5271 */