1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2003
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Copyright (C) 2004-2009, 2015 Freescale Semiconductor, Inc.
7 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
8 * Chao Fu (B44548@freescale.com)
9 * Haikun Wang (B53464@freescale.com)
22 #include <asm/arch/clock.h>
26 DECLARE_GLOBAL_DATA_PTR;
28 /* fsl_dspi_platdata flags */
29 #define DSPI_FLAG_REGMAP_ENDIAN_BIG BIT(0)
32 #define DSPI_IDLE_VAL 0x0
34 /* max chipselect signals number */
35 #define FSL_DSPI_MAX_CHIPSELECT 6
37 /* default SCK frequency, unit: HZ */
38 #define FSL_DSPI_DEFAULT_SCK_FREQ 10000000
40 /* tx/rx data wait timeout value, unit: us */
41 #define DSPI_TXRX_WAIT_TIMEOUT 1000000
43 /* CTAR register pre-configure value */
44 #define DSPI_CTAR_DEFAULT_VALUE (DSPI_CTAR_TRSZ(7) | \
45 DSPI_CTAR_PCSSCK_1CLK | \
48 DSPI_CTAR_CSSCK(0) | \
52 /* CTAR register pre-configure mask */
53 #define DSPI_CTAR_SET_MODE_MASK (DSPI_CTAR_TRSZ(15) | \
54 DSPI_CTAR_PCSSCK(3) | \
57 DSPI_CTAR_CSSCK(15) | \
62 * struct fsl_dspi_platdata - platform data for Freescale DSPI
64 * @flags: Flags for DSPI DSPI_FLAG_...
65 * @speed_hz: Default SCK frequency
66 * @num_chipselect: Number of DSPI chipselect signals
67 * @regs_addr: Base address of DSPI registers
69 struct fsl_dspi_platdata {
77 * struct fsl_dspi_priv - private data for Freescale DSPI
79 * @flags: Flags for DSPI DSPI_FLAG_...
80 * @mode: SPI mode to use for slave device (see SPI mode flags)
81 * @mcr_val: MCR register configure value
82 * @bus_clk: DSPI input clk frequency
83 * @speed_hz: Default SCK frequency
84 * @charbit: How many bits in every transfer
85 * @num_chipselect: Number of DSPI chipselect signals
86 * @ctar_val: CTAR register configure value of per chipselect slave device
87 * @regs: Point to DSPI register structure for I/O access
89 struct fsl_dspi_priv {
97 uint ctar_val[FSL_DSPI_MAX_CHIPSELECT];
101 #ifndef CONFIG_DM_SPI
103 struct spi_slave slave;
104 struct fsl_dspi_priv priv;
108 __weak void cpu_dspi_port_conf(void)
112 __weak int cpu_dspi_claim_bus(uint bus, uint cs)
117 __weak void cpu_dspi_release_bus(uint bus, uint cs)
121 static uint dspi_read32(uint flags, uint *addr)
123 return flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ?
124 in_be32(addr) : in_le32(addr);
127 static void dspi_write32(uint flags, uint *addr, uint val)
129 flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ?
130 out_be32(addr, val) : out_le32(addr, val);
133 static void dspi_halt(struct fsl_dspi_priv *priv, u8 halt)
137 mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
140 mcr_val |= DSPI_MCR_HALT;
142 mcr_val &= ~DSPI_MCR_HALT;
144 dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
147 static void fsl_dspi_init_mcr(struct fsl_dspi_priv *priv, uint cfg_val)
149 /* halt DSPI module */
152 dspi_write32(priv->flags, &priv->regs->mcr, cfg_val);
157 priv->mcr_val = cfg_val;
160 static void fsl_dspi_cfg_cs_active_state(struct fsl_dspi_priv *priv,
167 mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
168 if (state & SPI_CS_HIGH)
169 /* CSx inactive state is low */
170 mcr_val &= ~DSPI_MCR_PCSIS(cs);
172 /* CSx inactive state is high */
173 mcr_val |= DSPI_MCR_PCSIS(cs);
174 dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
179 static int fsl_dspi_cfg_ctar_mode(struct fsl_dspi_priv *priv,
184 bus_setup = dspi_read32(priv->flags, &priv->regs->ctar[0]);
186 bus_setup &= ~DSPI_CTAR_SET_MODE_MASK;
187 bus_setup |= priv->ctar_val[cs];
188 bus_setup &= ~(DSPI_CTAR_CPOL | DSPI_CTAR_CPHA | DSPI_CTAR_LSBFE);
191 bus_setup |= DSPI_CTAR_CPOL;
193 bus_setup |= DSPI_CTAR_CPHA;
194 if (mode & SPI_LSB_FIRST)
195 bus_setup |= DSPI_CTAR_LSBFE;
197 dspi_write32(priv->flags, &priv->regs->ctar[0], bus_setup);
200 ((dspi_read32(priv->flags, &priv->regs->ctar[0]) &
201 DSPI_CTAR_TRSZ(15)) == DSPI_CTAR_TRSZ(15)) ? 16 : 8;
206 static void fsl_dspi_clr_fifo(struct fsl_dspi_priv *priv)
211 mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
212 /* flush RX and TX FIFO */
213 mcr_val |= (DSPI_MCR_CTXF | DSPI_MCR_CRXF);
214 dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
218 static void dspi_tx(struct fsl_dspi_priv *priv, u32 ctrl, u16 data)
220 int timeout = DSPI_TXRX_WAIT_TIMEOUT;
222 /* wait for empty entries in TXFIFO or timeout */
223 while (DSPI_SR_TXCTR(dspi_read32(priv->flags, &priv->regs->sr)) >= 4 &&
228 dspi_write32(priv->flags, &priv->regs->tfr, (ctrl | data));
230 debug("dspi_tx: waiting timeout!\n");
233 static u16 dspi_rx(struct fsl_dspi_priv *priv)
235 int timeout = DSPI_TXRX_WAIT_TIMEOUT;
237 /* wait for valid entries in RXFIFO or timeout */
238 while (DSPI_SR_RXCTR(dspi_read32(priv->flags, &priv->regs->sr)) == 0 &&
243 return (u16)DSPI_RFR_RXDATA(
244 dspi_read32(priv->flags, &priv->regs->rfr));
246 debug("dspi_rx: waiting timeout!\n");
251 static int dspi_xfer(struct fsl_dspi_priv *priv, uint cs, unsigned int bitlen,
252 const void *dout, void *din, unsigned long flags)
254 u16 *spi_rd16 = NULL, *spi_wr16 = NULL;
255 u8 *spi_rd = NULL, *spi_wr = NULL;
257 uint len = bitlen >> 3;
259 if (priv->charbit == 16) {
261 spi_wr16 = (u16 *)dout;
262 spi_rd16 = (u16 *)din;
268 if ((flags & SPI_XFER_BEGIN) == SPI_XFER_BEGIN)
269 ctrl |= DSPI_TFR_CONT;
271 ctrl = ctrl & DSPI_TFR_CONT;
272 ctrl = ctrl | DSPI_TFR_CTAS(0) | DSPI_TFR_PCS(cs);
275 int tmp_len = len - 1;
277 if ((dout != NULL) && (din != NULL)) {
278 if (priv->charbit == 16) {
279 dspi_tx(priv, ctrl, *spi_wr16++);
280 *spi_rd16++ = dspi_rx(priv);
283 dspi_tx(priv, ctrl, *spi_wr++);
284 *spi_rd++ = dspi_rx(priv);
288 else if (dout != NULL) {
289 if (priv->charbit == 16)
290 dspi_tx(priv, ctrl, *spi_wr16++);
292 dspi_tx(priv, ctrl, *spi_wr++);
296 else if (din != NULL) {
297 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
298 if (priv->charbit == 16)
299 *spi_rd16++ = dspi_rx(priv);
301 *spi_rd++ = dspi_rx(priv);
305 len = 1; /* remaining byte */
308 if ((flags & SPI_XFER_END) == SPI_XFER_END)
309 ctrl &= ~DSPI_TFR_CONT;
312 if ((dout != NULL) && (din != NULL)) {
313 if (priv->charbit == 16) {
314 dspi_tx(priv, ctrl, *spi_wr16++);
315 *spi_rd16++ = dspi_rx(priv);
318 dspi_tx(priv, ctrl, *spi_wr++);
319 *spi_rd++ = dspi_rx(priv);
323 else if (dout != NULL) {
324 if (priv->charbit == 16)
325 dspi_tx(priv, ctrl, *spi_wr16);
327 dspi_tx(priv, ctrl, *spi_wr);
331 else if (din != NULL) {
332 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
333 if (priv->charbit == 16)
334 *spi_rd16 = dspi_rx(priv);
336 *spi_rd = dspi_rx(priv);
340 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
348 * Calculate the divide value between input clk frequency and expected SCK frequency
349 * Formula: SCK = (clkrate/pbr) x ((1+dbr)/br)
350 * Dbr: use default value 0
352 * @pbr: return Baud Rate Prescaler value
353 * @br: return Baud Rate Scaler value
354 * @speed_hz: expected SCK frequency
355 * @clkrate: input clk frequency
357 static int fsl_dspi_hz_to_spi_baud(int *pbr, int *br,
358 int speed_hz, uint clkrate)
360 /* Valid baud rate pre-scaler values */
361 int pbr_tbl[4] = {2, 3, 5, 7};
362 int brs[16] = {2, 4, 6, 8,
364 256, 512, 1024, 2048,
365 4096, 8192, 16384, 32768};
366 int temp, i = 0, j = 0;
368 temp = clkrate / speed_hz;
370 for (i = 0; i < ARRAY_SIZE(pbr_tbl); i++)
371 for (j = 0; j < ARRAY_SIZE(brs); j++) {
372 if (pbr_tbl[i] * brs[j] >= temp) {
379 debug("Can not find valid baud rate,speed_hz is %d, ", speed_hz);
380 debug("clkrate is %d, we use the max prescaler value.\n", clkrate);
382 *pbr = ARRAY_SIZE(pbr_tbl) - 1;
383 *br = ARRAY_SIZE(brs) - 1;
387 static int fsl_dspi_cfg_speed(struct fsl_dspi_priv *priv, uint speed)
391 int best_i, best_j, bus_clk;
393 bus_clk = priv->bus_clk;
395 debug("DSPI set_speed: expected SCK speed %u, bus_clk %u.\n",
398 bus_setup = dspi_read32(priv->flags, &priv->regs->ctar[0]);
399 bus_setup &= ~(DSPI_CTAR_DBR | DSPI_CTAR_PBR(0x3) | DSPI_CTAR_BR(0xf));
401 ret = fsl_dspi_hz_to_spi_baud(&best_i, &best_j, speed, bus_clk);
403 speed = priv->speed_hz;
404 debug("DSPI set_speed use default SCK rate %u.\n", speed);
405 fsl_dspi_hz_to_spi_baud(&best_i, &best_j, speed, bus_clk);
408 bus_setup |= (DSPI_CTAR_PBR(best_i) | DSPI_CTAR_BR(best_j));
409 dspi_write32(priv->flags, &priv->regs->ctar[0], bus_setup);
411 priv->speed_hz = speed;
415 #ifndef CONFIG_DM_SPI
416 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
418 if (((cs >= 0) && (cs < 8)) && ((bus >= 0) && (bus < 8)))
424 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
425 unsigned int max_hz, unsigned int mode)
427 struct fsl_dspi *dspi;
430 dspi = spi_alloc_slave(struct fsl_dspi, bus, cs);
434 cpu_dspi_port_conf();
436 #ifdef CONFIG_SYS_FSL_DSPI_BE
437 dspi->priv.flags |= DSPI_FLAG_REGMAP_ENDIAN_BIG;
440 dspi->priv.regs = (struct dspi *)MMAP_DSPI;
443 dspi->priv.bus_clk = gd->bus_clk;
445 dspi->priv.bus_clk = mxc_get_clock(MXC_DSPI_CLK);
447 dspi->priv.speed_hz = FSL_DSPI_DEFAULT_SCK_FREQ;
449 /* default: all CS signals inactive state is high */
450 mcr_cfg_val = DSPI_MCR_MSTR | DSPI_MCR_PCSIS_MASK |
451 DSPI_MCR_CRXF | DSPI_MCR_CTXF;
452 fsl_dspi_init_mcr(&dspi->priv, mcr_cfg_val);
454 for (i = 0; i < FSL_DSPI_MAX_CHIPSELECT; i++)
455 dspi->priv.ctar_val[i] = DSPI_CTAR_DEFAULT_VALUE;
457 #ifdef CONFIG_SYS_DSPI_CTAR0
458 if (FSL_DSPI_MAX_CHIPSELECT > 0)
459 dspi->priv.ctar_val[0] = CONFIG_SYS_DSPI_CTAR0;
461 #ifdef CONFIG_SYS_DSPI_CTAR1
462 if (FSL_DSPI_MAX_CHIPSELECT > 1)
463 dspi->priv.ctar_val[1] = CONFIG_SYS_DSPI_CTAR1;
465 #ifdef CONFIG_SYS_DSPI_CTAR2
466 if (FSL_DSPI_MAX_CHIPSELECT > 2)
467 dspi->priv.ctar_val[2] = CONFIG_SYS_DSPI_CTAR2;
469 #ifdef CONFIG_SYS_DSPI_CTAR3
470 if (FSL_DSPI_MAX_CHIPSELECT > 3)
471 dspi->priv.ctar_val[3] = CONFIG_SYS_DSPI_CTAR3;
473 #ifdef CONFIG_SYS_DSPI_CTAR4
474 if (FSL_DSPI_MAX_CHIPSELECT > 4)
475 dspi->priv.ctar_val[4] = CONFIG_SYS_DSPI_CTAR4;
477 #ifdef CONFIG_SYS_DSPI_CTAR5
478 if (FSL_DSPI_MAX_CHIPSELECT > 5)
479 dspi->priv.ctar_val[5] = CONFIG_SYS_DSPI_CTAR5;
481 #ifdef CONFIG_SYS_DSPI_CTAR6
482 if (FSL_DSPI_MAX_CHIPSELECT > 6)
483 dspi->priv.ctar_val[6] = CONFIG_SYS_DSPI_CTAR6;
485 #ifdef CONFIG_SYS_DSPI_CTAR7
486 if (FSL_DSPI_MAX_CHIPSELECT > 7)
487 dspi->priv.ctar_val[7] = CONFIG_SYS_DSPI_CTAR7;
490 fsl_dspi_cfg_speed(&dspi->priv, max_hz);
492 /* configure transfer mode */
493 fsl_dspi_cfg_ctar_mode(&dspi->priv, cs, mode);
495 /* configure active state of CSX */
496 fsl_dspi_cfg_cs_active_state(&dspi->priv, cs, mode);
501 void spi_free_slave(struct spi_slave *slave)
506 int spi_claim_bus(struct spi_slave *slave)
509 struct fsl_dspi *dspi = (struct fsl_dspi *)slave;
511 cpu_dspi_claim_bus(slave->bus, slave->cs);
513 fsl_dspi_clr_fifo(&dspi->priv);
515 /* check module TX and RX status */
516 sr_val = dspi_read32(dspi->priv.flags, &dspi->priv.regs->sr);
517 if ((sr_val & DSPI_SR_TXRXS) != DSPI_SR_TXRXS) {
518 debug("DSPI RX/TX not ready!\n");
525 void spi_release_bus(struct spi_slave *slave)
527 struct fsl_dspi *dspi = (struct fsl_dspi *)slave;
529 dspi_halt(&dspi->priv, 1);
530 cpu_dspi_release_bus(slave->bus.slave->cs);
533 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
534 void *din, unsigned long flags)
536 struct fsl_dspi *dspi = (struct fsl_dspi *)slave;
537 return dspi_xfer(&dspi->priv, slave->cs, bitlen, dout, din, flags);
540 static int fsl_dspi_child_pre_probe(struct udevice *dev)
542 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
543 struct fsl_dspi_priv *priv = dev_get_priv(dev->parent);
545 if (slave_plat->cs >= priv->num_chipselect) {
546 debug("DSPI invalid chipselect number %d(max %d)!\n",
547 slave_plat->cs, priv->num_chipselect - 1);
551 priv->ctar_val[slave_plat->cs] = DSPI_CTAR_DEFAULT_VALUE;
553 debug("DSPI pre_probe slave device on CS %u, max_hz %u, mode 0x%x.\n",
554 slave_plat->cs, slave_plat->max_hz, slave_plat->mode);
559 static int fsl_dspi_probe(struct udevice *bus)
561 struct fsl_dspi_platdata *plat = dev_get_platdata(bus);
562 struct fsl_dspi_priv *priv = dev_get_priv(bus);
563 struct dm_spi_bus *dm_spi_bus;
566 dm_spi_bus = bus->uclass_priv;
568 /* cpu speical pin muxing configure */
569 cpu_dspi_port_conf();
571 /* get input clk frequency */
572 priv->regs = (struct dspi *)plat->regs_addr;
573 priv->flags = plat->flags;
575 priv->bus_clk = gd->bus_clk;
577 priv->bus_clk = mxc_get_clock(MXC_DSPI_CLK);
579 priv->num_chipselect = plat->num_chipselect;
580 priv->speed_hz = plat->speed_hz;
581 /* frame data length in bits, default 8bits */
584 dm_spi_bus->max_hz = plat->speed_hz;
586 /* default: all CS signals inactive state is high */
587 mcr_cfg_val = DSPI_MCR_MSTR | DSPI_MCR_PCSIS_MASK |
588 DSPI_MCR_CRXF | DSPI_MCR_CTXF;
589 fsl_dspi_init_mcr(priv, mcr_cfg_val);
591 debug("%s probe done, bus-num %d.\n", bus->name, bus->seq);
596 static int fsl_dspi_claim_bus(struct udevice *dev)
599 struct fsl_dspi_priv *priv;
600 struct udevice *bus = dev->parent;
601 struct dm_spi_slave_platdata *slave_plat =
602 dev_get_parent_platdata(dev);
604 priv = dev_get_priv(bus);
606 /* processor special preparation work */
607 cpu_dspi_claim_bus(bus->seq, slave_plat->cs);
609 /* configure transfer mode */
610 fsl_dspi_cfg_ctar_mode(priv, slave_plat->cs, priv->mode);
612 /* configure active state of CSX */
613 fsl_dspi_cfg_cs_active_state(priv, slave_plat->cs,
616 fsl_dspi_clr_fifo(priv);
618 /* check module TX and RX status */
619 sr_val = dspi_read32(priv->flags, &priv->regs->sr);
620 if ((sr_val & DSPI_SR_TXRXS) != DSPI_SR_TXRXS) {
621 debug("DSPI RX/TX not ready!\n");
628 static int fsl_dspi_release_bus(struct udevice *dev)
630 struct udevice *bus = dev->parent;
631 struct fsl_dspi_priv *priv = dev_get_priv(bus);
632 struct dm_spi_slave_platdata *slave_plat =
633 dev_get_parent_platdata(dev);
638 /* processor special release work */
639 cpu_dspi_release_bus(bus->seq, slave_plat->cs);
645 * This function doesn't do anything except help with debugging
647 static int fsl_dspi_bind(struct udevice *bus)
649 debug("%s assigned req_seq %d.\n", bus->name, bus->req_seq);
653 static int fsl_dspi_ofdata_to_platdata(struct udevice *bus)
656 struct fsl_dspi_platdata *plat = bus->platdata;
657 const void *blob = gd->fdt_blob;
658 int node = dev_of_offset(bus);
660 if (fdtdec_get_bool(blob, node, "big-endian"))
661 plat->flags |= DSPI_FLAG_REGMAP_ENDIAN_BIG;
663 plat->num_chipselect =
664 fdtdec_get_int(blob, node, "num-cs", FSL_DSPI_MAX_CHIPSELECT);
666 addr = devfdt_get_addr(bus);
667 if (addr == FDT_ADDR_T_NONE) {
668 debug("DSPI: Can't get base address or size\n");
671 plat->regs_addr = addr;
673 plat->speed_hz = fdtdec_get_int(blob,
674 node, "spi-max-frequency", FSL_DSPI_DEFAULT_SCK_FREQ);
676 debug("DSPI: regs=%pa, max-frequency=%d, endianess=%s, num-cs=%d\n",
677 &plat->regs_addr, plat->speed_hz,
678 plat->flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ? "be" : "le",
679 plat->num_chipselect);
684 static int fsl_dspi_xfer(struct udevice *dev, unsigned int bitlen,
685 const void *dout, void *din, unsigned long flags)
687 struct fsl_dspi_priv *priv;
688 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
692 priv = dev_get_priv(bus);
694 return dspi_xfer(priv, slave_plat->cs, bitlen, dout, din, flags);
697 static int fsl_dspi_set_speed(struct udevice *bus, uint speed)
699 struct fsl_dspi_priv *priv = dev_get_priv(bus);
701 return fsl_dspi_cfg_speed(priv, speed);
704 static int fsl_dspi_set_mode(struct udevice *bus, uint mode)
706 struct fsl_dspi_priv *priv = dev_get_priv(bus);
708 debug("DSPI set_mode: mode 0x%x.\n", mode);
711 * We store some chipselect special configure value in priv->ctar_val,
712 * and we can't get the correct chipselect number here,
713 * so just store mode value.
714 * Do really configuration when claim_bus.
721 static const struct dm_spi_ops fsl_dspi_ops = {
722 .claim_bus = fsl_dspi_claim_bus,
723 .release_bus = fsl_dspi_release_bus,
724 .xfer = fsl_dspi_xfer,
725 .set_speed = fsl_dspi_set_speed,
726 .set_mode = fsl_dspi_set_mode,
729 static const struct udevice_id fsl_dspi_ids[] = {
730 { .compatible = "fsl,vf610-dspi" },
734 U_BOOT_DRIVER(fsl_dspi) = {
737 .of_match = fsl_dspi_ids,
738 .ops = &fsl_dspi_ops,
739 .ofdata_to_platdata = fsl_dspi_ofdata_to_platdata,
740 .platdata_auto_alloc_size = sizeof(struct fsl_dspi_platdata),
741 .priv_auto_alloc_size = sizeof(struct fsl_dspi_priv),
742 .probe = fsl_dspi_probe,
743 .child_pre_probe = fsl_dspi_child_pre_probe,
744 .bind = fsl_dspi_bind,