1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2003
6 * Copyright (C) 2004-2009, 2015 Freescale Semiconductor, Inc.
12 #include <linux/math64.h>
23 #include <asm/arch/clock.h>
26 #include <linux/bitops.h>
27 #include <linux/delay.h>
29 /* linux/include/time.h */
30 #define NSEC_PER_SEC 1000000000L
32 DECLARE_GLOBAL_DATA_PTR;
34 /* fsl_dspi_platdata flags */
35 #define DSPI_FLAG_REGMAP_ENDIAN_BIG BIT(0)
38 #define DSPI_IDLE_VAL 0x0
40 /* max chipselect signals number */
41 #define FSL_DSPI_MAX_CHIPSELECT 6
43 /* default SCK frequency, unit: HZ */
44 #define FSL_DSPI_DEFAULT_SCK_FREQ 10000000
46 /* tx/rx data wait timeout value, unit: us */
47 #define DSPI_TXRX_WAIT_TIMEOUT 1000000
49 /* CTAR register pre-configure value */
50 #define DSPI_CTAR_DEFAULT_VALUE (DSPI_CTAR_TRSZ(7) | \
51 DSPI_CTAR_PCSSCK_1CLK | \
54 DSPI_CTAR_CSSCK(0) | \
58 /* CTAR register pre-configure mask */
59 #define DSPI_CTAR_SET_MODE_MASK (DSPI_CTAR_TRSZ(15) | \
60 DSPI_CTAR_PCSSCK(3) | \
63 DSPI_CTAR_CSSCK(15) | \
68 * struct fsl_dspi_platdata - platform data for Freescale DSPI
70 * @flags: Flags for DSPI DSPI_FLAG_...
71 * @speed_hz: Default SCK frequency
72 * @num_chipselect: Number of DSPI chipselect signals
73 * @regs_addr: Base address of DSPI registers
75 struct fsl_dspi_platdata {
83 * struct fsl_dspi_priv - private data for Freescale DSPI
85 * @flags: Flags for DSPI DSPI_FLAG_...
86 * @mode: SPI mode to use for slave device (see SPI mode flags)
87 * @mcr_val: MCR register configure value
88 * @bus_clk: DSPI input clk frequency
89 * @speed_hz: Default SCK frequency
90 * @charbit: How many bits in every transfer
91 * @num_chipselect: Number of DSPI chipselect signals
92 * @ctar_val: CTAR register configure value of per chipselect slave device
93 * @regs: Point to DSPI register structure for I/O access
95 struct fsl_dspi_priv {
103 uint ctar_val[FSL_DSPI_MAX_CHIPSELECT];
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;
276 if ((dout != NULL) && (din != NULL)) {
277 if (priv->charbit == 16) {
278 dspi_tx(priv, ctrl, *spi_wr16++);
279 *spi_rd16++ = dspi_rx(priv);
282 dspi_tx(priv, ctrl, *spi_wr++);
283 *spi_rd++ = dspi_rx(priv);
287 else if (dout != NULL) {
288 if (priv->charbit == 16)
289 dspi_tx(priv, ctrl, *spi_wr16++);
291 dspi_tx(priv, ctrl, *spi_wr++);
295 else if (din != NULL) {
296 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
297 if (priv->charbit == 16)
298 *spi_rd16++ = dspi_rx(priv);
300 *spi_rd++ = dspi_rx(priv);
304 len = 1; /* remaining byte */
307 if ((flags & SPI_XFER_END) == SPI_XFER_END)
308 ctrl &= ~DSPI_TFR_CONT;
311 if ((dout != NULL) && (din != NULL)) {
312 if (priv->charbit == 16) {
313 dspi_tx(priv, ctrl, *spi_wr16++);
314 *spi_rd16++ = dspi_rx(priv);
317 dspi_tx(priv, ctrl, *spi_wr++);
318 *spi_rd++ = dspi_rx(priv);
322 else if (dout != NULL) {
323 if (priv->charbit == 16)
324 dspi_tx(priv, ctrl, *spi_wr16);
326 dspi_tx(priv, ctrl, *spi_wr);
330 else if (din != NULL) {
331 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
332 if (priv->charbit == 16)
333 *spi_rd16 = dspi_rx(priv);
335 *spi_rd = dspi_rx(priv);
339 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
347 * Calculate the divide value between input clk frequency and expected SCK frequency
348 * Formula: SCK = (clkrate/pbr) x ((1+dbr)/br)
349 * Dbr: use default value 0
351 * @pbr: return Baud Rate Prescaler value
352 * @br: return Baud Rate Scaler value
353 * @speed_hz: expected SCK frequency
354 * @clkrate: input clk frequency
356 static int fsl_dspi_hz_to_spi_baud(int *pbr, int *br,
357 int speed_hz, uint clkrate)
359 /* Valid baud rate pre-scaler values */
360 int pbr_tbl[4] = {2, 3, 5, 7};
361 int brs[16] = {2, 4, 6, 8,
363 256, 512, 1024, 2048,
364 4096, 8192, 16384, 32768};
365 int temp, i = 0, j = 0;
367 temp = clkrate / speed_hz;
369 for (i = 0; i < ARRAY_SIZE(pbr_tbl); i++)
370 for (j = 0; j < ARRAY_SIZE(brs); j++) {
371 if (pbr_tbl[i] * brs[j] >= temp) {
378 debug("Can not find valid baud rate,speed_hz is %d, ", speed_hz);
379 debug("clkrate is %d, we use the max prescaler value.\n", clkrate);
381 *pbr = ARRAY_SIZE(pbr_tbl) - 1;
382 *br = ARRAY_SIZE(brs) - 1;
386 static void ns_delay_scale(unsigned char *psc, unsigned char *sc, int delay_ns,
387 unsigned long clkrate)
389 int scale_needed, scale, minscale = INT_MAX;
390 int pscale_tbl[4] = {1, 3, 5, 7};
394 scale_needed = div_u64_rem((u64)delay_ns * clkrate, NSEC_PER_SEC,
399 for (i = 0; i < ARRAY_SIZE(pscale_tbl); i++)
400 for (j = 0; j <= DSPI_CTAR_SCALE_BITS; j++) {
401 scale = pscale_tbl[i] * (2 << j);
402 if (scale >= scale_needed) {
403 if (scale < minscale) {
412 if (minscale == INT_MAX) {
413 pr_warn("Cannot find correct scale values for %dns delay at clkrate %ld, using max prescaler value",
415 *psc = ARRAY_SIZE(pscale_tbl) - 1;
416 *sc = DSPI_CTAR_SCALE_BITS;
420 static int fsl_dspi_cfg_speed(struct fsl_dspi_priv *priv, uint speed)
424 int best_i, best_j, bus_clk;
426 bus_clk = priv->bus_clk;
428 debug("DSPI set_speed: expected SCK speed %u, bus_clk %u.\n",
431 bus_setup = dspi_read32(priv->flags, &priv->regs->ctar[0]);
432 bus_setup &= ~(DSPI_CTAR_DBR | DSPI_CTAR_PBR(0x3) | DSPI_CTAR_BR(0xf));
434 ret = fsl_dspi_hz_to_spi_baud(&best_i, &best_j, speed, bus_clk);
436 speed = priv->speed_hz;
437 debug("DSPI set_speed use default SCK rate %u.\n", speed);
438 fsl_dspi_hz_to_spi_baud(&best_i, &best_j, speed, bus_clk);
441 bus_setup |= (DSPI_CTAR_PBR(best_i) | DSPI_CTAR_BR(best_j));
442 dspi_write32(priv->flags, &priv->regs->ctar[0], bus_setup);
444 priv->speed_hz = speed;
449 static int fsl_dspi_child_pre_probe(struct udevice *dev)
451 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
452 struct fsl_dspi_priv *priv = dev_get_priv(dev->parent);
453 u32 cs_sck_delay = 0, sck_cs_delay = 0;
454 unsigned char pcssck = 0, cssck = 0;
455 unsigned char pasc = 0, asc = 0;
457 if (slave_plat->cs >= priv->num_chipselect) {
458 debug("DSPI invalid chipselect number %d(max %d)!\n",
459 slave_plat->cs, priv->num_chipselect - 1);
463 ofnode_read_u32(dev->node, "fsl,spi-cs-sck-delay", &cs_sck_delay);
464 ofnode_read_u32(dev->node, "fsl,spi-sck-cs-delay", &sck_cs_delay);
466 /* Set PCS to SCK delay scale values */
467 ns_delay_scale(&pcssck, &cssck, cs_sck_delay, priv->bus_clk);
469 /* Set After SCK delay scale values */
470 ns_delay_scale(&pasc, &asc, sck_cs_delay, priv->bus_clk);
472 priv->ctar_val[slave_plat->cs] = DSPI_CTAR_DEFAULT_VALUE |
473 DSPI_CTAR_PCSSCK(pcssck) |
474 DSPI_CTAR_PASC(pasc);
476 debug("DSPI pre_probe slave device on CS %u, max_hz %u, mode 0x%x.\n",
477 slave_plat->cs, slave_plat->max_hz, slave_plat->mode);
482 static int fsl_dspi_probe(struct udevice *bus)
484 struct fsl_dspi_platdata *plat = dev_get_platdata(bus);
485 struct fsl_dspi_priv *priv = dev_get_priv(bus);
486 struct dm_spi_bus *dm_spi_bus;
489 dm_spi_bus = bus->uclass_priv;
491 /* cpu speical pin muxing configure */
492 cpu_dspi_port_conf();
494 /* get input clk frequency */
495 priv->regs = (struct dspi *)plat->regs_addr;
496 priv->flags = plat->flags;
498 priv->bus_clk = gd->bus_clk;
500 priv->bus_clk = mxc_get_clock(MXC_DSPI_CLK);
502 priv->num_chipselect = plat->num_chipselect;
503 priv->speed_hz = plat->speed_hz;
504 /* frame data length in bits, default 8bits */
507 dm_spi_bus->max_hz = plat->speed_hz;
509 /* default: all CS signals inactive state is high */
510 mcr_cfg_val = DSPI_MCR_MSTR | DSPI_MCR_PCSIS_MASK |
511 DSPI_MCR_CRXF | DSPI_MCR_CTXF;
512 fsl_dspi_init_mcr(priv, mcr_cfg_val);
514 debug("%s probe done, bus-num %d.\n", bus->name, bus->seq);
519 static int fsl_dspi_claim_bus(struct udevice *dev)
522 struct fsl_dspi_priv *priv;
523 struct udevice *bus = dev->parent;
524 struct dm_spi_slave_platdata *slave_plat =
525 dev_get_parent_platdata(dev);
527 priv = dev_get_priv(bus);
529 /* processor special preparation work */
530 cpu_dspi_claim_bus(bus->seq, slave_plat->cs);
532 /* configure transfer mode */
533 fsl_dspi_cfg_ctar_mode(priv, slave_plat->cs, priv->mode);
535 /* configure active state of CSX */
536 fsl_dspi_cfg_cs_active_state(priv, slave_plat->cs,
539 fsl_dspi_clr_fifo(priv);
541 /* check module TX and RX status */
542 sr_val = dspi_read32(priv->flags, &priv->regs->sr);
543 if ((sr_val & DSPI_SR_TXRXS) != DSPI_SR_TXRXS) {
544 debug("DSPI RX/TX not ready!\n");
551 static int fsl_dspi_release_bus(struct udevice *dev)
553 struct udevice *bus = dev->parent;
554 struct fsl_dspi_priv *priv = dev_get_priv(bus);
555 struct dm_spi_slave_platdata *slave_plat =
556 dev_get_parent_platdata(dev);
561 /* processor special release work */
562 cpu_dspi_release_bus(bus->seq, slave_plat->cs);
568 * This function doesn't do anything except help with debugging
570 static int fsl_dspi_bind(struct udevice *bus)
572 debug("%s assigned req_seq %d.\n", bus->name, bus->req_seq);
576 static int fsl_dspi_ofdata_to_platdata(struct udevice *bus)
579 struct fsl_dspi_platdata *plat = bus->platdata;
580 const void *blob = gd->fdt_blob;
581 int node = dev_of_offset(bus);
583 if (fdtdec_get_bool(blob, node, "big-endian"))
584 plat->flags |= DSPI_FLAG_REGMAP_ENDIAN_BIG;
586 plat->num_chipselect =
587 fdtdec_get_int(blob, node, "num-cs", FSL_DSPI_MAX_CHIPSELECT);
589 addr = dev_read_addr(bus);
590 if (addr == FDT_ADDR_T_NONE) {
591 debug("DSPI: Can't get base address or size\n");
594 plat->regs_addr = addr;
596 plat->speed_hz = fdtdec_get_int(blob,
597 node, "spi-max-frequency", FSL_DSPI_DEFAULT_SCK_FREQ);
599 debug("DSPI: regs=%pa, max-frequency=%d, endianess=%s, num-cs=%d\n",
600 &plat->regs_addr, plat->speed_hz,
601 plat->flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ? "be" : "le",
602 plat->num_chipselect);
607 static int fsl_dspi_xfer(struct udevice *dev, unsigned int bitlen,
608 const void *dout, void *din, unsigned long flags)
610 struct fsl_dspi_priv *priv;
611 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
615 priv = dev_get_priv(bus);
617 return dspi_xfer(priv, slave_plat->cs, bitlen, dout, din, flags);
620 static int fsl_dspi_set_speed(struct udevice *bus, uint speed)
622 struct fsl_dspi_priv *priv = dev_get_priv(bus);
624 return fsl_dspi_cfg_speed(priv, speed);
627 static int fsl_dspi_set_mode(struct udevice *bus, uint mode)
629 struct fsl_dspi_priv *priv = dev_get_priv(bus);
631 debug("DSPI set_mode: mode 0x%x.\n", mode);
634 * We store some chipselect special configure value in priv->ctar_val,
635 * and we can't get the correct chipselect number here,
636 * so just store mode value.
637 * Do really configuration when claim_bus.
644 static const struct dm_spi_ops fsl_dspi_ops = {
645 .claim_bus = fsl_dspi_claim_bus,
646 .release_bus = fsl_dspi_release_bus,
647 .xfer = fsl_dspi_xfer,
648 .set_speed = fsl_dspi_set_speed,
649 .set_mode = fsl_dspi_set_mode,
652 static const struct udevice_id fsl_dspi_ids[] = {
653 { .compatible = "fsl,vf610-dspi" },
657 U_BOOT_DRIVER(fsl_dspi) = {
660 .of_match = fsl_dspi_ids,
661 .ops = &fsl_dspi_ops,
662 .ofdata_to_platdata = fsl_dspi_ofdata_to_platdata,
663 .platdata_auto = sizeof(struct fsl_dspi_platdata),
664 .priv_auto = sizeof(struct fsl_dspi_priv),
665 .probe = fsl_dspi_probe,
666 .child_pre_probe = fsl_dspi_child_pre_probe,
667 .bind = fsl_dspi_bind,