1 // SPDX-License-Identifier: GPL-2.0+
4 * (C) Copyright 2000-2003
7 * Copyright (C) 2004-2009 Freescale Semiconductor, Inc.
10 * Support for DM and DT, non-DM code removed.
13 * TODO: fsl_dspi.c should work as a driver for the DSPI module.
18 #include <asm/global_data.h>
19 #include <dm/platform_data/spi_coldfire.h>
22 #include <asm/coldfire/dspi.h>
25 struct coldfire_spi_priv {
32 DECLARE_GLOBAL_DATA_PTR;
35 #if defined(CONFIG_SPI_MMC)
36 #define SPI_IDLE_VAL 0xFFFF
38 #define SPI_IDLE_VAL 0x0
45 * bit 31 - 28: Transfer size 3 to 16 bits
46 * 27 - 26: PCS to SCK delay prescaler
47 * 25 - 24: After SCK delay prescaler
48 * 23 - 22: Delay after transfer prescaler
49 * 21 : Allow overwrite for bit 31-22 and bit 20-8
50 * 20 : Double baud rate
51 * 19 - 16: PCS to SCK delay scaler
52 * 15 - 12: After SCK delay scaler
53 * 11 - 8: Delay after transfer scaler
54 * 7 - 0: SPI_CPHA, SPI_CPOL, SPI_LSB_FIRST
56 #define SPI_MODE_MOD 0x00200000
57 #define SPI_MODE_DBLRATE 0x00100000
59 #define SPI_MODE_XFER_SZ_MASK 0xf0000000
60 #define SPI_MODE_DLY_PRE_MASK 0x0fc00000
61 #define SPI_MODE_DLY_SCA_MASK 0x000fff00
63 #define MCF_FRM_SZ_16BIT DSPI_CTAR_TRSZ(0xf)
64 #define MCF_DSPI_SPEED_BESTMATCH 0x7FFFFFFF
65 #define MCF_DSPI_MAX_CTAR_REGS 8
68 #define MCF_DSPI_DEFAULT_SCK_FREQ 10000000
69 #define MCF_DSPI_DEFAULT_MAX_CS 4
70 #define MCF_DSPI_DEFAULT_MODE 0
72 #define MCF_DSPI_DEFAULT_CTAR (DSPI_CTAR_TRSZ(7) | \
73 DSPI_CTAR_PCSSCK_1CLK | \
76 DSPI_CTAR_CSSCK(0) | \
81 #define MCF_CTAR_MODE_MASK (MCF_FRM_SZ_16BIT | \
82 DSPI_CTAR_PCSSCK(3) | \
83 DSPI_CTAR_PASC_7CLK | \
85 DSPI_CTAR_CSSCK(0x0f) | \
86 DSPI_CTAR_ASC(0x0f) | \
89 #define setup_ctrl(ctrl, cs) ((ctrl & 0xFF000000) | ((1 << cs) << 16))
91 static inline void cfspi_tx(struct coldfire_spi_priv *cfspi,
95 * Need to check fifo level here
97 while ((readl(&cfspi->regs->sr) & 0x0000F000) >= 0x4000)
100 writel(ctrl | data, &cfspi->regs->tfr);
103 static inline u16 cfspi_rx(struct coldfire_spi_priv *cfspi)
106 while ((readl(&cfspi->regs->sr) & 0x000000F0) == 0)
109 return readw(&cfspi->regs->rfr);
112 static int coldfire_spi_claim_bus(struct udevice *dev)
114 struct udevice *bus = dev->parent;
115 struct coldfire_spi_priv *cfspi = dev_get_priv(bus);
116 struct dspi *dspi = cfspi->regs;
117 struct dm_spi_slave_plat *slave_plat =
118 dev_get_parent_plat(dev);
120 if ((in_be32(&dspi->sr) & DSPI_SR_TXRXS) != DSPI_SR_TXRXS)
123 /* Clear FIFO and resume transfer */
124 clrbits_be32(&dspi->mcr, DSPI_MCR_CTXF | DSPI_MCR_CRXF);
126 dspi_chip_select(slave_plat->cs);
131 static int coldfire_spi_release_bus(struct udevice *dev)
133 struct udevice *bus = dev->parent;
134 struct coldfire_spi_priv *cfspi = dev_get_priv(bus);
135 struct dspi *dspi = cfspi->regs;
136 struct dm_spi_slave_plat *slave_plat =
137 dev_get_parent_plat(dev);
140 clrbits_be32(&dspi->mcr, DSPI_MCR_CTXF | DSPI_MCR_CRXF);
142 dspi_chip_unselect(slave_plat->cs);
147 static int coldfire_spi_xfer(struct udevice *dev, unsigned int bitlen,
148 const void *dout, void *din,
151 struct udevice *bus = dev_get_parent(dev);
152 struct coldfire_spi_priv *cfspi = dev_get_priv(bus);
153 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
154 u16 *spi_rd16 = NULL, *spi_wr16 = NULL;
155 u8 *spi_rd = NULL, *spi_wr = NULL;
157 uint len = bitlen >> 3;
159 if (cfspi->charbit == 16) {
161 spi_wr16 = (u16 *)dout;
162 spi_rd16 = (u16 *)din;
168 if ((flags & SPI_XFER_BEGIN) == SPI_XFER_BEGIN)
169 ctrl |= DSPI_TFR_CONT;
171 ctrl = setup_ctrl(ctrl, slave_plat->cs);
174 int tmp_len = len - 1;
178 if (cfspi->charbit == 16)
179 cfspi_tx(cfspi, ctrl, *spi_wr16++);
181 cfspi_tx(cfspi, ctrl, *spi_wr++);
186 cfspi_tx(cfspi, ctrl, SPI_IDLE_VAL);
187 if (cfspi->charbit == 16)
188 *spi_rd16++ = cfspi_rx(cfspi);
190 *spi_rd++ = cfspi_rx(cfspi);
194 len = 1; /* remaining byte */
197 if (flags & SPI_XFER_END)
198 ctrl &= ~DSPI_TFR_CONT;
202 if (cfspi->charbit == 16)
203 cfspi_tx(cfspi, ctrl, *spi_wr16);
205 cfspi_tx(cfspi, ctrl, *spi_wr);
210 cfspi_tx(cfspi, ctrl, SPI_IDLE_VAL);
211 if (cfspi->charbit == 16)
212 *spi_rd16 = cfspi_rx(cfspi);
214 *spi_rd = cfspi_rx(cfspi);
218 cfspi_tx(cfspi, ctrl, SPI_IDLE_VAL);
225 static int coldfire_spi_set_speed(struct udevice *bus, uint max_hz)
227 struct coldfire_spi_priv *cfspi = dev_get_priv(bus);
228 struct dspi *dspi = cfspi->regs;
229 int prescaler[] = { 2, 3, 5, 7 };
233 256, 512, 1024, 2048,
234 4096, 8192, 16384, 32768
236 int i, j, pbrcnt, brcnt, diff, tmp, dbr = 0;
237 int best_i, best_j, bestmatch = MCF_DSPI_SPEED_BESTMATCH, baud_speed;
240 cfspi->baudrate = max_hz;
242 /* Read current setup */
243 bus_setup = readl(&dspi->ctar[dev_seq(bus)]);
245 tmp = (prescaler[3] * scaler[15]);
246 /* Maximum and minimum baudrate it can handle */
247 if ((cfspi->baudrate > (gd->bus_clk >> 1)) ||
248 (cfspi->baudrate < (gd->bus_clk / tmp))) {
249 printf("Exceed baudrate limitation: Max %d - Min %d\n",
250 (int)(gd->bus_clk >> 1), (int)(gd->bus_clk / tmp));
254 /* Activate Double Baud when it exceed 1/4 the bus clk */
255 if ((bus_setup & DSPI_CTAR_DBR) ||
256 (cfspi->baudrate > (gd->bus_clk / (prescaler[0] * scaler[0])))) {
257 bus_setup |= DSPI_CTAR_DBR;
261 /* Overwrite default value set in platform configuration file */
262 if (cfspi->mode & SPI_MODE_MOD) {
264 * Check to see if it is enabled by default in platform
265 * config, or manual setting passed by mode parameter
267 if (cfspi->mode & SPI_MODE_DBLRATE) {
268 bus_setup |= DSPI_CTAR_DBR;
273 pbrcnt = sizeof(prescaler) / sizeof(int);
274 brcnt = sizeof(scaler) / sizeof(int);
276 /* baudrate calculation - to closer value, may not be exact match */
277 for (best_i = 0, best_j = 0, i = 0; i < pbrcnt; i++) {
278 baud_speed = gd->bus_clk / prescaler[i];
279 for (j = 0; j < brcnt; j++) {
280 tmp = (baud_speed / scaler[j]) * (1 + dbr);
282 if (tmp > cfspi->baudrate)
283 diff = tmp - cfspi->baudrate;
285 diff = cfspi->baudrate - tmp;
287 if (diff < bestmatch) {
295 bus_setup &= ~(DSPI_CTAR_PBR(0x03) | DSPI_CTAR_BR(0x0f));
296 bus_setup |= (DSPI_CTAR_PBR(best_i) | DSPI_CTAR_BR(best_j));
297 writel(bus_setup, &dspi->ctar[dev_seq(bus)]);
302 static int coldfire_spi_set_mode(struct udevice *bus, uint mode)
304 struct coldfire_spi_priv *cfspi = dev_get_priv(bus);
305 struct dspi *dspi = cfspi->regs;
310 if (cfspi->mode & SPI_CPOL)
311 bus_setup |= DSPI_CTAR_CPOL;
312 if (cfspi->mode & SPI_CPHA)
313 bus_setup |= DSPI_CTAR_CPHA;
314 if (cfspi->mode & SPI_LSB_FIRST)
315 bus_setup |= DSPI_CTAR_LSBFE;
317 /* Overwrite default value set in platform configuration file */
318 if (cfspi->mode & SPI_MODE_MOD) {
319 if ((cfspi->mode & SPI_MODE_XFER_SZ_MASK) == 0)
321 readl(&dspi->ctar[dev_seq(bus)]) & MCF_FRM_SZ_16BIT;
324 ((cfspi->mode & SPI_MODE_XFER_SZ_MASK) >> 1);
326 /* PSCSCK, PASC, PDT */
327 bus_setup |= (cfspi->mode & SPI_MODE_DLY_PRE_MASK) >> 4;
329 bus_setup |= (cfspi->mode & SPI_MODE_DLY_SCA_MASK) >> 4;
332 (readl(&dspi->ctar[dev_seq(bus)]) & MCF_CTAR_MODE_MASK);
336 ((readl(&dspi->ctar[dev_seq(bus)]) & MCF_FRM_SZ_16BIT) ==
337 MCF_FRM_SZ_16BIT) ? 16 : 8;
339 setbits_be32(&dspi->ctar[dev_seq(bus)], bus_setup);
344 static int coldfire_spi_probe(struct udevice *bus)
346 struct coldfire_spi_plat *plat = dev_get_plat(bus);
347 struct coldfire_spi_priv *cfspi = dev_get_priv(bus);
348 struct dspi *dspi = cfspi->regs;
351 cfspi->regs = (struct dspi *)plat->regs_addr;
353 cfspi->baudrate = plat->speed_hz;
354 cfspi->mode = plat->mode;
356 for (i = 0; i < MCF_DSPI_MAX_CTAR_REGS; i++) {
357 unsigned int ctar = 0;
359 if (plat->ctar[i][0] == 0)
362 ctar = DSPI_CTAR_TRSZ(plat->ctar[i][0]) |
363 DSPI_CTAR_PCSSCK(plat->ctar[i][1]) |
364 DSPI_CTAR_PASC(plat->ctar[i][2]) |
365 DSPI_CTAR_PDT(plat->ctar[i][3]) |
366 DSPI_CTAR_CSSCK(plat->ctar[i][4]) |
367 DSPI_CTAR_ASC(plat->ctar[i][5]) |
368 DSPI_CTAR_DT(plat->ctar[i][6]) |
369 DSPI_CTAR_BR(plat->ctar[i][7]);
371 writel(ctar, &cfspi->regs->ctar[i]);
375 for (i = 0; i < MCF_DSPI_MAX_CTAR_REGS; i++)
376 writel(MCF_DSPI_DEFAULT_CTAR, &dspi->ctar[i]);
378 dspi->mcr = DSPI_MCR_MSTR | DSPI_MCR_CSIS7 | DSPI_MCR_CSIS6 |
379 DSPI_MCR_CSIS5 | DSPI_MCR_CSIS4 | DSPI_MCR_CSIS3 |
380 DSPI_MCR_CSIS2 | DSPI_MCR_CSIS1 | DSPI_MCR_CSIS0 |
381 DSPI_MCR_CRXF | DSPI_MCR_CTXF;
386 #if CONFIG_IS_ENABLED(OF_REAL)
387 static int coldfire_dspi_of_to_plat(struct udevice *bus)
390 struct coldfire_spi_plat *plat = dev_get_plat(bus);
391 const void *blob = gd->fdt_blob;
392 int node = dev_of_offset(bus);
395 addr = dev_read_addr(bus);
396 if (addr == FDT_ADDR_T_NONE)
399 plat->regs_addr = addr;
401 plat->num_cs = fdtdec_get_int(blob, node, "num-cs",
402 MCF_DSPI_DEFAULT_MAX_CS);
404 plat->speed_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
405 MCF_DSPI_DEFAULT_SCK_FREQ);
407 plat->mode = fdtdec_get_int(blob, node, "spi-mode",
408 MCF_DSPI_DEFAULT_MODE);
410 memset(plat->ctar, 0, sizeof(plat->ctar));
412 ctar = (int *)fdt_getprop(blob, node, "ctar-params", &len);
417 ctar_regs = len / sizeof(unsigned int) / MAX_CTAR_FIELDS;
419 if (ctar_regs > MAX_CTAR_REGS)
420 ctar_regs = MAX_CTAR_REGS;
422 for (i = 0; i < ctar_regs; i++) {
423 for (q = 0; q < MAX_CTAR_FIELDS; q++)
424 plat->ctar[i][q] = *ctar++;
428 debug("DSPI: regs=%pa, max-frequency=%d, num-cs=%d, mode=%d\n",
429 (void *)plat->regs_addr,
430 plat->speed_hz, plat->num_cs, plat->mode);
435 static const struct udevice_id coldfire_spi_ids[] = {
436 { .compatible = "fsl,mcf-dspi" },
441 static const struct dm_spi_ops coldfire_spi_ops = {
442 .claim_bus = coldfire_spi_claim_bus,
443 .release_bus = coldfire_spi_release_bus,
444 .xfer = coldfire_spi_xfer,
445 .set_speed = coldfire_spi_set_speed,
446 .set_mode = coldfire_spi_set_mode,
449 U_BOOT_DRIVER(coldfire_spi) = {
450 .name = "spi_coldfire",
452 #if CONFIG_IS_ENABLED(OF_REAL)
453 .of_match = coldfire_spi_ids,
454 .of_to_plat = coldfire_dspi_of_to_plat,
455 .plat_auto = sizeof(struct coldfire_spi_plat),
457 .probe = coldfire_spi_probe,
458 .ops = &coldfire_spi_ops,
459 .priv_auto = sizeof(struct coldfire_spi_priv),