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)
21 #include <asm/arch/clock.h>
25 DECLARE_GLOBAL_DATA_PTR;
27 /* fsl_dspi_platdata flags */
28 #define DSPI_FLAG_REGMAP_ENDIAN_BIG BIT(0)
31 #define DSPI_IDLE_VAL 0x0
33 /* max chipselect signals number */
34 #define FSL_DSPI_MAX_CHIPSELECT 6
36 /* default SCK frequency, unit: HZ */
37 #define FSL_DSPI_DEFAULT_SCK_FREQ 10000000
39 /* tx/rx data wait timeout value, unit: us */
40 #define DSPI_TXRX_WAIT_TIMEOUT 1000000
42 /* CTAR register pre-configure value */
43 #define DSPI_CTAR_DEFAULT_VALUE (DSPI_CTAR_TRSZ(7) | \
44 DSPI_CTAR_PCSSCK_1CLK | \
47 DSPI_CTAR_CSSCK(0) | \
51 /* CTAR register pre-configure mask */
52 #define DSPI_CTAR_SET_MODE_MASK (DSPI_CTAR_TRSZ(15) | \
53 DSPI_CTAR_PCSSCK(3) | \
56 DSPI_CTAR_CSSCK(15) | \
61 * struct fsl_dspi_platdata - platform data for Freescale DSPI
63 * @flags: Flags for DSPI DSPI_FLAG_...
64 * @speed_hz: Default SCK frequency
65 * @num_chipselect: Number of DSPI chipselect signals
66 * @regs_addr: Base address of DSPI registers
68 struct fsl_dspi_platdata {
76 * struct fsl_dspi_priv - private data for Freescale DSPI
78 * @flags: Flags for DSPI DSPI_FLAG_...
79 * @mode: SPI mode to use for slave device (see SPI mode flags)
80 * @mcr_val: MCR register configure value
81 * @bus_clk: DSPI input clk frequency
82 * @speed_hz: Default SCK frequency
83 * @charbit: How many bits in every transfer
84 * @num_chipselect: Number of DSPI chipselect signals
85 * @ctar_val: CTAR register configure value of per chipselect slave device
86 * @regs: Point to DSPI register structure for I/O access
88 struct fsl_dspi_priv {
96 uint ctar_val[FSL_DSPI_MAX_CHIPSELECT];
100 #ifndef CONFIG_DM_SPI
102 struct spi_slave slave;
103 struct fsl_dspi_priv priv;
107 __weak void cpu_dspi_port_conf(void)
111 __weak int cpu_dspi_claim_bus(uint bus, uint cs)
116 __weak void cpu_dspi_release_bus(uint bus, uint cs)
120 static uint dspi_read32(uint flags, uint *addr)
122 return flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ?
123 in_be32(addr) : in_le32(addr);
126 static void dspi_write32(uint flags, uint *addr, uint val)
128 flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ?
129 out_be32(addr, val) : out_le32(addr, val);
132 static void dspi_halt(struct fsl_dspi_priv *priv, u8 halt)
136 mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
139 mcr_val |= DSPI_MCR_HALT;
141 mcr_val &= ~DSPI_MCR_HALT;
143 dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
146 static void fsl_dspi_init_mcr(struct fsl_dspi_priv *priv, uint cfg_val)
148 /* halt DSPI module */
151 dspi_write32(priv->flags, &priv->regs->mcr, cfg_val);
156 priv->mcr_val = cfg_val;
159 static void fsl_dspi_cfg_cs_active_state(struct fsl_dspi_priv *priv,
166 mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
167 if (state & SPI_CS_HIGH)
168 /* CSx inactive state is low */
169 mcr_val &= ~DSPI_MCR_PCSIS(cs);
171 /* CSx inactive state is high */
172 mcr_val |= DSPI_MCR_PCSIS(cs);
173 dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
178 static int fsl_dspi_cfg_ctar_mode(struct fsl_dspi_priv *priv,
183 bus_setup = dspi_read32(priv->flags, &priv->regs->ctar[0]);
185 bus_setup &= ~DSPI_CTAR_SET_MODE_MASK;
186 bus_setup |= priv->ctar_val[cs];
187 bus_setup &= ~(DSPI_CTAR_CPOL | DSPI_CTAR_CPHA | DSPI_CTAR_LSBFE);
190 bus_setup |= DSPI_CTAR_CPOL;
192 bus_setup |= DSPI_CTAR_CPHA;
193 if (mode & SPI_LSB_FIRST)
194 bus_setup |= DSPI_CTAR_LSBFE;
196 dspi_write32(priv->flags, &priv->regs->ctar[0], bus_setup);
199 ((dspi_read32(priv->flags, &priv->regs->ctar[0]) &
200 DSPI_CTAR_TRSZ(15)) == DSPI_CTAR_TRSZ(15)) ? 16 : 8;
205 static void fsl_dspi_clr_fifo(struct fsl_dspi_priv *priv)
210 mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
211 /* flush RX and TX FIFO */
212 mcr_val |= (DSPI_MCR_CTXF | DSPI_MCR_CRXF);
213 dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
217 static void dspi_tx(struct fsl_dspi_priv *priv, u32 ctrl, u16 data)
219 int timeout = DSPI_TXRX_WAIT_TIMEOUT;
221 /* wait for empty entries in TXFIFO or timeout */
222 while (DSPI_SR_TXCTR(dspi_read32(priv->flags, &priv->regs->sr)) >= 4 &&
227 dspi_write32(priv->flags, &priv->regs->tfr, (ctrl | data));
229 debug("dspi_tx: waiting timeout!\n");
232 static u16 dspi_rx(struct fsl_dspi_priv *priv)
234 int timeout = DSPI_TXRX_WAIT_TIMEOUT;
236 /* wait for valid entries in RXFIFO or timeout */
237 while (DSPI_SR_RXCTR(dspi_read32(priv->flags, &priv->regs->sr)) == 0 &&
242 return (u16)DSPI_RFR_RXDATA(
243 dspi_read32(priv->flags, &priv->regs->rfr));
245 debug("dspi_rx: waiting timeout!\n");
250 static int dspi_xfer(struct fsl_dspi_priv *priv, uint cs, unsigned int bitlen,
251 const void *dout, void *din, unsigned long flags)
253 u16 *spi_rd16 = NULL, *spi_wr16 = NULL;
254 u8 *spi_rd = NULL, *spi_wr = NULL;
256 uint len = bitlen >> 3;
258 if (priv->charbit == 16) {
260 spi_wr16 = (u16 *)dout;
261 spi_rd16 = (u16 *)din;
267 if ((flags & SPI_XFER_BEGIN) == SPI_XFER_BEGIN)
268 ctrl |= DSPI_TFR_CONT;
270 ctrl = ctrl & DSPI_TFR_CONT;
271 ctrl = ctrl | DSPI_TFR_CTAS(0) | DSPI_TFR_PCS(cs);
274 int tmp_len = len - 1;
277 if (priv->charbit == 16)
278 dspi_tx(priv, ctrl, *spi_wr16++);
280 dspi_tx(priv, ctrl, *spi_wr++);
285 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
286 if (priv->charbit == 16)
287 *spi_rd16++ = dspi_rx(priv);
289 *spi_rd++ = dspi_rx(priv);
293 len = 1; /* remaining byte */
296 if ((flags & SPI_XFER_END) == SPI_XFER_END)
297 ctrl &= ~DSPI_TFR_CONT;
301 if (priv->charbit == 16)
302 dspi_tx(priv, ctrl, *spi_wr16);
304 dspi_tx(priv, ctrl, *spi_wr);
309 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
310 if (priv->charbit == 16)
311 *spi_rd16 = dspi_rx(priv);
313 *spi_rd = dspi_rx(priv);
317 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
325 * Calculate the divide value between input clk frequency and expected SCK frequency
326 * Formula: SCK = (clkrate/pbr) x ((1+dbr)/br)
327 * Dbr: use default value 0
329 * @pbr: return Baud Rate Prescaler value
330 * @br: return Baud Rate Scaler value
331 * @speed_hz: expected SCK frequency
332 * @clkrate: input clk frequency
334 static int fsl_dspi_hz_to_spi_baud(int *pbr, int *br,
335 int speed_hz, uint clkrate)
337 /* Valid baud rate pre-scaler values */
338 int pbr_tbl[4] = {2, 3, 5, 7};
339 int brs[16] = {2, 4, 6, 8,
341 256, 512, 1024, 2048,
342 4096, 8192, 16384, 32768};
343 int temp, i = 0, j = 0;
345 temp = clkrate / speed_hz;
347 for (i = 0; i < ARRAY_SIZE(pbr_tbl); i++)
348 for (j = 0; j < ARRAY_SIZE(brs); j++) {
349 if (pbr_tbl[i] * brs[j] >= temp) {
356 debug("Can not find valid baud rate,speed_hz is %d, ", speed_hz);
357 debug("clkrate is %d, we use the max prescaler value.\n", clkrate);
359 *pbr = ARRAY_SIZE(pbr_tbl) - 1;
360 *br = ARRAY_SIZE(brs) - 1;
364 static int fsl_dspi_cfg_speed(struct fsl_dspi_priv *priv, uint speed)
368 int best_i, best_j, bus_clk;
370 bus_clk = priv->bus_clk;
372 debug("DSPI set_speed: expected SCK speed %u, bus_clk %u.\n",
375 bus_setup = dspi_read32(priv->flags, &priv->regs->ctar[0]);
376 bus_setup &= ~(DSPI_CTAR_DBR | DSPI_CTAR_PBR(0x3) | DSPI_CTAR_BR(0xf));
378 ret = fsl_dspi_hz_to_spi_baud(&best_i, &best_j, speed, bus_clk);
380 speed = priv->speed_hz;
381 debug("DSPI set_speed use default SCK rate %u.\n", speed);
382 fsl_dspi_hz_to_spi_baud(&best_i, &best_j, speed, bus_clk);
385 bus_setup |= (DSPI_CTAR_PBR(best_i) | DSPI_CTAR_BR(best_j));
386 dspi_write32(priv->flags, &priv->regs->ctar[0], bus_setup);
388 priv->speed_hz = speed;
392 #ifndef CONFIG_DM_SPI
393 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
395 if (((cs >= 0) && (cs < 8)) && ((bus >= 0) && (bus < 8)))
401 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
402 unsigned int max_hz, unsigned int mode)
404 struct fsl_dspi *dspi;
407 dspi = spi_alloc_slave(struct fsl_dspi, bus, cs);
411 cpu_dspi_port_conf();
413 #ifdef CONFIG_SYS_FSL_DSPI_BE
414 dspi->priv.flags |= DSPI_FLAG_REGMAP_ENDIAN_BIG;
417 dspi->priv.regs = (struct dspi *)MMAP_DSPI;
420 dspi->priv.bus_clk = gd->bus_clk;
422 dspi->priv.bus_clk = mxc_get_clock(MXC_DSPI_CLK);
424 dspi->priv.speed_hz = FSL_DSPI_DEFAULT_SCK_FREQ;
426 /* default: all CS signals inactive state is high */
427 mcr_cfg_val = DSPI_MCR_MSTR | DSPI_MCR_PCSIS_MASK |
428 DSPI_MCR_CRXF | DSPI_MCR_CTXF;
429 fsl_dspi_init_mcr(&dspi->priv, mcr_cfg_val);
431 for (i = 0; i < FSL_DSPI_MAX_CHIPSELECT; i++)
432 dspi->priv.ctar_val[i] = DSPI_CTAR_DEFAULT_VALUE;
434 #ifdef CONFIG_SYS_DSPI_CTAR0
435 if (FSL_DSPI_MAX_CHIPSELECT > 0)
436 dspi->priv.ctar_val[0] = CONFIG_SYS_DSPI_CTAR0;
438 #ifdef CONFIG_SYS_DSPI_CTAR1
439 if (FSL_DSPI_MAX_CHIPSELECT > 1)
440 dspi->priv.ctar_val[1] = CONFIG_SYS_DSPI_CTAR1;
442 #ifdef CONFIG_SYS_DSPI_CTAR2
443 if (FSL_DSPI_MAX_CHIPSELECT > 2)
444 dspi->priv.ctar_val[2] = CONFIG_SYS_DSPI_CTAR2;
446 #ifdef CONFIG_SYS_DSPI_CTAR3
447 if (FSL_DSPI_MAX_CHIPSELECT > 3)
448 dspi->priv.ctar_val[3] = CONFIG_SYS_DSPI_CTAR3;
450 #ifdef CONFIG_SYS_DSPI_CTAR4
451 if (FSL_DSPI_MAX_CHIPSELECT > 4)
452 dspi->priv.ctar_val[4] = CONFIG_SYS_DSPI_CTAR4;
454 #ifdef CONFIG_SYS_DSPI_CTAR5
455 if (FSL_DSPI_MAX_CHIPSELECT > 5)
456 dspi->priv.ctar_val[5] = CONFIG_SYS_DSPI_CTAR5;
458 #ifdef CONFIG_SYS_DSPI_CTAR6
459 if (FSL_DSPI_MAX_CHIPSELECT > 6)
460 dspi->priv.ctar_val[6] = CONFIG_SYS_DSPI_CTAR6;
462 #ifdef CONFIG_SYS_DSPI_CTAR7
463 if (FSL_DSPI_MAX_CHIPSELECT > 7)
464 dspi->priv.ctar_val[7] = CONFIG_SYS_DSPI_CTAR7;
467 fsl_dspi_cfg_speed(&dspi->priv, max_hz);
469 /* configure transfer mode */
470 fsl_dspi_cfg_ctar_mode(&dspi->priv, cs, mode);
472 /* configure active state of CSX */
473 fsl_dspi_cfg_cs_active_state(&dspi->priv, cs, mode);
478 void spi_free_slave(struct spi_slave *slave)
483 int spi_claim_bus(struct spi_slave *slave)
486 struct fsl_dspi *dspi = (struct fsl_dspi *)slave;
488 cpu_dspi_claim_bus(slave->bus, slave->cs);
490 fsl_dspi_clr_fifo(&dspi->priv);
492 /* check module TX and RX status */
493 sr_val = dspi_read32(dspi->priv.flags, &dspi->priv.regs->sr);
494 if ((sr_val & DSPI_SR_TXRXS) != DSPI_SR_TXRXS) {
495 debug("DSPI RX/TX not ready!\n");
502 void spi_release_bus(struct spi_slave *slave)
504 struct fsl_dspi *dspi = (struct fsl_dspi *)slave;
506 dspi_halt(&dspi->priv, 1);
507 cpu_dspi_release_bus(slave->bus.slave->cs);
510 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
511 void *din, unsigned long flags)
513 struct fsl_dspi *dspi = (struct fsl_dspi *)slave;
514 return dspi_xfer(&dspi->priv, slave->cs, bitlen, dout, din, flags);
517 static int fsl_dspi_child_pre_probe(struct udevice *dev)
519 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
520 struct fsl_dspi_priv *priv = dev_get_priv(dev->parent);
522 if (slave_plat->cs >= priv->num_chipselect) {
523 debug("DSPI invalid chipselect number %d(max %d)!\n",
524 slave_plat->cs, priv->num_chipselect - 1);
528 priv->ctar_val[slave_plat->cs] = DSPI_CTAR_DEFAULT_VALUE;
530 debug("DSPI pre_probe slave device on CS %u, max_hz %u, mode 0x%x.\n",
531 slave_plat->cs, slave_plat->max_hz, slave_plat->mode);
536 static int fsl_dspi_probe(struct udevice *bus)
538 struct fsl_dspi_platdata *plat = dev_get_platdata(bus);
539 struct fsl_dspi_priv *priv = dev_get_priv(bus);
540 struct dm_spi_bus *dm_spi_bus;
543 dm_spi_bus = bus->uclass_priv;
545 /* cpu speical pin muxing configure */
546 cpu_dspi_port_conf();
548 /* get input clk frequency */
549 priv->regs = (struct dspi *)plat->regs_addr;
550 priv->flags = plat->flags;
552 priv->bus_clk = gd->bus_clk;
554 priv->bus_clk = mxc_get_clock(MXC_DSPI_CLK);
556 priv->num_chipselect = plat->num_chipselect;
557 priv->speed_hz = plat->speed_hz;
558 /* frame data length in bits, default 8bits */
561 dm_spi_bus->max_hz = plat->speed_hz;
563 /* default: all CS signals inactive state is high */
564 mcr_cfg_val = DSPI_MCR_MSTR | DSPI_MCR_PCSIS_MASK |
565 DSPI_MCR_CRXF | DSPI_MCR_CTXF;
566 fsl_dspi_init_mcr(priv, mcr_cfg_val);
568 debug("%s probe done, bus-num %d.\n", bus->name, bus->seq);
573 static int fsl_dspi_claim_bus(struct udevice *dev)
576 struct fsl_dspi_priv *priv;
577 struct udevice *bus = dev->parent;
578 struct dm_spi_slave_platdata *slave_plat =
579 dev_get_parent_platdata(dev);
581 priv = dev_get_priv(bus);
583 /* processor special preparation work */
584 cpu_dspi_claim_bus(bus->seq, slave_plat->cs);
586 /* configure transfer mode */
587 fsl_dspi_cfg_ctar_mode(priv, slave_plat->cs, priv->mode);
589 /* configure active state of CSX */
590 fsl_dspi_cfg_cs_active_state(priv, slave_plat->cs,
593 fsl_dspi_clr_fifo(priv);
595 /* check module TX and RX status */
596 sr_val = dspi_read32(priv->flags, &priv->regs->sr);
597 if ((sr_val & DSPI_SR_TXRXS) != DSPI_SR_TXRXS) {
598 debug("DSPI RX/TX not ready!\n");
605 static int fsl_dspi_release_bus(struct udevice *dev)
607 struct udevice *bus = dev->parent;
608 struct fsl_dspi_priv *priv = dev_get_priv(bus);
609 struct dm_spi_slave_platdata *slave_plat =
610 dev_get_parent_platdata(dev);
615 /* processor special release work */
616 cpu_dspi_release_bus(bus->seq, slave_plat->cs);
622 * This function doesn't do anything except help with debugging
624 static int fsl_dspi_bind(struct udevice *bus)
626 debug("%s assigned req_seq %d.\n", bus->name, bus->req_seq);
630 static int fsl_dspi_ofdata_to_platdata(struct udevice *bus)
633 struct fsl_dspi_platdata *plat = bus->platdata;
634 const void *blob = gd->fdt_blob;
635 int node = dev_of_offset(bus);
637 if (fdtdec_get_bool(blob, node, "big-endian"))
638 plat->flags |= DSPI_FLAG_REGMAP_ENDIAN_BIG;
640 plat->num_chipselect =
641 fdtdec_get_int(blob, node, "num-cs", FSL_DSPI_MAX_CHIPSELECT);
643 addr = devfdt_get_addr(bus);
644 if (addr == FDT_ADDR_T_NONE) {
645 debug("DSPI: Can't get base address or size\n");
648 plat->regs_addr = addr;
650 plat->speed_hz = fdtdec_get_int(blob,
651 node, "spi-max-frequency", FSL_DSPI_DEFAULT_SCK_FREQ);
653 debug("DSPI: regs=%pa, max-frequency=%d, endianess=%s, num-cs=%d\n",
654 &plat->regs_addr, plat->speed_hz,
655 plat->flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ? "be" : "le",
656 plat->num_chipselect);
661 static int fsl_dspi_xfer(struct udevice *dev, unsigned int bitlen,
662 const void *dout, void *din, unsigned long flags)
664 struct fsl_dspi_priv *priv;
665 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
669 priv = dev_get_priv(bus);
671 return dspi_xfer(priv, slave_plat->cs, bitlen, dout, din, flags);
674 static int fsl_dspi_set_speed(struct udevice *bus, uint speed)
676 struct fsl_dspi_priv *priv = dev_get_priv(bus);
678 return fsl_dspi_cfg_speed(priv, speed);
681 static int fsl_dspi_set_mode(struct udevice *bus, uint mode)
683 struct fsl_dspi_priv *priv = dev_get_priv(bus);
685 debug("DSPI set_mode: mode 0x%x.\n", mode);
688 * We store some chipselect special configure value in priv->ctar_val,
689 * and we can't get the correct chipselect number here,
690 * so just store mode value.
691 * Do really configuration when claim_bus.
698 static const struct dm_spi_ops fsl_dspi_ops = {
699 .claim_bus = fsl_dspi_claim_bus,
700 .release_bus = fsl_dspi_release_bus,
701 .xfer = fsl_dspi_xfer,
702 .set_speed = fsl_dspi_set_speed,
703 .set_mode = fsl_dspi_set_mode,
706 static const struct udevice_id fsl_dspi_ids[] = {
707 { .compatible = "fsl,vf610-dspi" },
711 U_BOOT_DRIVER(fsl_dspi) = {
714 .of_match = fsl_dspi_ids,
715 .ops = &fsl_dspi_ops,
716 .ofdata_to_platdata = fsl_dspi_ofdata_to_platdata,
717 .platdata_auto_alloc_size = sizeof(struct fsl_dspi_platdata),
718 .priv_auto_alloc_size = sizeof(struct fsl_dspi_priv),
719 .probe = fsl_dspi_probe,
720 .child_pre_probe = fsl_dspi_child_pre_probe,
721 .bind = fsl_dspi_bind,