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>
25 #include <linux/bitops.h>
26 #include <linux/delay.h>
28 DECLARE_GLOBAL_DATA_PTR;
30 /* fsl_dspi_platdata flags */
31 #define DSPI_FLAG_REGMAP_ENDIAN_BIG BIT(0)
34 #define DSPI_IDLE_VAL 0x0
36 /* max chipselect signals number */
37 #define FSL_DSPI_MAX_CHIPSELECT 6
39 /* default SCK frequency, unit: HZ */
40 #define FSL_DSPI_DEFAULT_SCK_FREQ 10000000
42 /* tx/rx data wait timeout value, unit: us */
43 #define DSPI_TXRX_WAIT_TIMEOUT 1000000
45 /* CTAR register pre-configure value */
46 #define DSPI_CTAR_DEFAULT_VALUE (DSPI_CTAR_TRSZ(7) | \
47 DSPI_CTAR_PCSSCK_1CLK | \
50 DSPI_CTAR_CSSCK(0) | \
54 /* CTAR register pre-configure mask */
55 #define DSPI_CTAR_SET_MODE_MASK (DSPI_CTAR_TRSZ(15) | \
56 DSPI_CTAR_PCSSCK(3) | \
59 DSPI_CTAR_CSSCK(15) | \
64 * struct fsl_dspi_platdata - platform data for Freescale DSPI
66 * @flags: Flags for DSPI DSPI_FLAG_...
67 * @speed_hz: Default SCK frequency
68 * @num_chipselect: Number of DSPI chipselect signals
69 * @regs_addr: Base address of DSPI registers
71 struct fsl_dspi_platdata {
79 * struct fsl_dspi_priv - private data for Freescale DSPI
81 * @flags: Flags for DSPI DSPI_FLAG_...
82 * @mode: SPI mode to use for slave device (see SPI mode flags)
83 * @mcr_val: MCR register configure value
84 * @bus_clk: DSPI input clk frequency
85 * @speed_hz: Default SCK frequency
86 * @charbit: How many bits in every transfer
87 * @num_chipselect: Number of DSPI chipselect signals
88 * @ctar_val: CTAR register configure value of per chipselect slave device
89 * @regs: Point to DSPI register structure for I/O access
91 struct fsl_dspi_priv {
99 uint ctar_val[FSL_DSPI_MAX_CHIPSELECT];
103 __weak void cpu_dspi_port_conf(void)
107 __weak int cpu_dspi_claim_bus(uint bus, uint cs)
112 __weak void cpu_dspi_release_bus(uint bus, uint cs)
116 static uint dspi_read32(uint flags, uint *addr)
118 return flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ?
119 in_be32(addr) : in_le32(addr);
122 static void dspi_write32(uint flags, uint *addr, uint val)
124 flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ?
125 out_be32(addr, val) : out_le32(addr, val);
128 static void dspi_halt(struct fsl_dspi_priv *priv, u8 halt)
132 mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
135 mcr_val |= DSPI_MCR_HALT;
137 mcr_val &= ~DSPI_MCR_HALT;
139 dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
142 static void fsl_dspi_init_mcr(struct fsl_dspi_priv *priv, uint cfg_val)
144 /* halt DSPI module */
147 dspi_write32(priv->flags, &priv->regs->mcr, cfg_val);
152 priv->mcr_val = cfg_val;
155 static void fsl_dspi_cfg_cs_active_state(struct fsl_dspi_priv *priv,
162 mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
163 if (state & SPI_CS_HIGH)
164 /* CSx inactive state is low */
165 mcr_val &= ~DSPI_MCR_PCSIS(cs);
167 /* CSx inactive state is high */
168 mcr_val |= DSPI_MCR_PCSIS(cs);
169 dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
174 static int fsl_dspi_cfg_ctar_mode(struct fsl_dspi_priv *priv,
179 bus_setup = dspi_read32(priv->flags, &priv->regs->ctar[0]);
181 bus_setup &= ~DSPI_CTAR_SET_MODE_MASK;
182 bus_setup |= priv->ctar_val[cs];
183 bus_setup &= ~(DSPI_CTAR_CPOL | DSPI_CTAR_CPHA | DSPI_CTAR_LSBFE);
186 bus_setup |= DSPI_CTAR_CPOL;
188 bus_setup |= DSPI_CTAR_CPHA;
189 if (mode & SPI_LSB_FIRST)
190 bus_setup |= DSPI_CTAR_LSBFE;
192 dspi_write32(priv->flags, &priv->regs->ctar[0], bus_setup);
195 ((dspi_read32(priv->flags, &priv->regs->ctar[0]) &
196 DSPI_CTAR_TRSZ(15)) == DSPI_CTAR_TRSZ(15)) ? 16 : 8;
201 static void fsl_dspi_clr_fifo(struct fsl_dspi_priv *priv)
206 mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
207 /* flush RX and TX FIFO */
208 mcr_val |= (DSPI_MCR_CTXF | DSPI_MCR_CRXF);
209 dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
213 static void dspi_tx(struct fsl_dspi_priv *priv, u32 ctrl, u16 data)
215 int timeout = DSPI_TXRX_WAIT_TIMEOUT;
217 /* wait for empty entries in TXFIFO or timeout */
218 while (DSPI_SR_TXCTR(dspi_read32(priv->flags, &priv->regs->sr)) >= 4 &&
223 dspi_write32(priv->flags, &priv->regs->tfr, (ctrl | data));
225 debug("dspi_tx: waiting timeout!\n");
228 static u16 dspi_rx(struct fsl_dspi_priv *priv)
230 int timeout = DSPI_TXRX_WAIT_TIMEOUT;
232 /* wait for valid entries in RXFIFO or timeout */
233 while (DSPI_SR_RXCTR(dspi_read32(priv->flags, &priv->regs->sr)) == 0 &&
238 return (u16)DSPI_RFR_RXDATA(
239 dspi_read32(priv->flags, &priv->regs->rfr));
241 debug("dspi_rx: waiting timeout!\n");
246 static int dspi_xfer(struct fsl_dspi_priv *priv, uint cs, unsigned int bitlen,
247 const void *dout, void *din, unsigned long flags)
249 u16 *spi_rd16 = NULL, *spi_wr16 = NULL;
250 u8 *spi_rd = NULL, *spi_wr = NULL;
252 uint len = bitlen >> 3;
254 if (priv->charbit == 16) {
256 spi_wr16 = (u16 *)dout;
257 spi_rd16 = (u16 *)din;
263 if ((flags & SPI_XFER_BEGIN) == SPI_XFER_BEGIN)
264 ctrl |= DSPI_TFR_CONT;
266 ctrl = ctrl & DSPI_TFR_CONT;
267 ctrl = ctrl | DSPI_TFR_CTAS(0) | DSPI_TFR_PCS(cs);
270 int tmp_len = len - 1;
272 if ((dout != NULL) && (din != NULL)) {
273 if (priv->charbit == 16) {
274 dspi_tx(priv, ctrl, *spi_wr16++);
275 *spi_rd16++ = dspi_rx(priv);
278 dspi_tx(priv, ctrl, *spi_wr++);
279 *spi_rd++ = dspi_rx(priv);
283 else if (dout != NULL) {
284 if (priv->charbit == 16)
285 dspi_tx(priv, ctrl, *spi_wr16++);
287 dspi_tx(priv, ctrl, *spi_wr++);
291 else if (din != NULL) {
292 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
293 if (priv->charbit == 16)
294 *spi_rd16++ = dspi_rx(priv);
296 *spi_rd++ = dspi_rx(priv);
300 len = 1; /* remaining byte */
303 if ((flags & SPI_XFER_END) == SPI_XFER_END)
304 ctrl &= ~DSPI_TFR_CONT;
307 if ((dout != NULL) && (din != NULL)) {
308 if (priv->charbit == 16) {
309 dspi_tx(priv, ctrl, *spi_wr16++);
310 *spi_rd16++ = dspi_rx(priv);
313 dspi_tx(priv, ctrl, *spi_wr++);
314 *spi_rd++ = dspi_rx(priv);
318 else if (dout != NULL) {
319 if (priv->charbit == 16)
320 dspi_tx(priv, ctrl, *spi_wr16);
322 dspi_tx(priv, ctrl, *spi_wr);
326 else if (din != NULL) {
327 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
328 if (priv->charbit == 16)
329 *spi_rd16 = dspi_rx(priv);
331 *spi_rd = dspi_rx(priv);
335 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
343 * Calculate the divide value between input clk frequency and expected SCK frequency
344 * Formula: SCK = (clkrate/pbr) x ((1+dbr)/br)
345 * Dbr: use default value 0
347 * @pbr: return Baud Rate Prescaler value
348 * @br: return Baud Rate Scaler value
349 * @speed_hz: expected SCK frequency
350 * @clkrate: input clk frequency
352 static int fsl_dspi_hz_to_spi_baud(int *pbr, int *br,
353 int speed_hz, uint clkrate)
355 /* Valid baud rate pre-scaler values */
356 int pbr_tbl[4] = {2, 3, 5, 7};
357 int brs[16] = {2, 4, 6, 8,
359 256, 512, 1024, 2048,
360 4096, 8192, 16384, 32768};
361 int temp, i = 0, j = 0;
363 temp = clkrate / speed_hz;
365 for (i = 0; i < ARRAY_SIZE(pbr_tbl); i++)
366 for (j = 0; j < ARRAY_SIZE(brs); j++) {
367 if (pbr_tbl[i] * brs[j] >= temp) {
374 debug("Can not find valid baud rate,speed_hz is %d, ", speed_hz);
375 debug("clkrate is %d, we use the max prescaler value.\n", clkrate);
377 *pbr = ARRAY_SIZE(pbr_tbl) - 1;
378 *br = ARRAY_SIZE(brs) - 1;
382 static int fsl_dspi_cfg_speed(struct fsl_dspi_priv *priv, uint speed)
386 int best_i, best_j, bus_clk;
388 bus_clk = priv->bus_clk;
390 debug("DSPI set_speed: expected SCK speed %u, bus_clk %u.\n",
393 bus_setup = dspi_read32(priv->flags, &priv->regs->ctar[0]);
394 bus_setup &= ~(DSPI_CTAR_DBR | DSPI_CTAR_PBR(0x3) | DSPI_CTAR_BR(0xf));
396 ret = fsl_dspi_hz_to_spi_baud(&best_i, &best_j, speed, bus_clk);
398 speed = priv->speed_hz;
399 debug("DSPI set_speed use default SCK rate %u.\n", speed);
400 fsl_dspi_hz_to_spi_baud(&best_i, &best_j, speed, bus_clk);
403 bus_setup |= (DSPI_CTAR_PBR(best_i) | DSPI_CTAR_BR(best_j));
404 dspi_write32(priv->flags, &priv->regs->ctar[0], bus_setup);
406 priv->speed_hz = speed;
411 static int fsl_dspi_child_pre_probe(struct udevice *dev)
413 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
414 struct fsl_dspi_priv *priv = dev_get_priv(dev->parent);
416 if (slave_plat->cs >= priv->num_chipselect) {
417 debug("DSPI invalid chipselect number %d(max %d)!\n",
418 slave_plat->cs, priv->num_chipselect - 1);
422 priv->ctar_val[slave_plat->cs] = DSPI_CTAR_DEFAULT_VALUE;
424 debug("DSPI pre_probe slave device on CS %u, max_hz %u, mode 0x%x.\n",
425 slave_plat->cs, slave_plat->max_hz, slave_plat->mode);
430 static int fsl_dspi_probe(struct udevice *bus)
432 struct fsl_dspi_platdata *plat = dev_get_platdata(bus);
433 struct fsl_dspi_priv *priv = dev_get_priv(bus);
434 struct dm_spi_bus *dm_spi_bus;
437 dm_spi_bus = bus->uclass_priv;
439 /* cpu speical pin muxing configure */
440 cpu_dspi_port_conf();
442 /* get input clk frequency */
443 priv->regs = (struct dspi *)plat->regs_addr;
444 priv->flags = plat->flags;
446 priv->bus_clk = gd->bus_clk;
448 priv->bus_clk = mxc_get_clock(MXC_DSPI_CLK);
450 priv->num_chipselect = plat->num_chipselect;
451 priv->speed_hz = plat->speed_hz;
452 /* frame data length in bits, default 8bits */
455 dm_spi_bus->max_hz = plat->speed_hz;
457 /* default: all CS signals inactive state is high */
458 mcr_cfg_val = DSPI_MCR_MSTR | DSPI_MCR_PCSIS_MASK |
459 DSPI_MCR_CRXF | DSPI_MCR_CTXF;
460 fsl_dspi_init_mcr(priv, mcr_cfg_val);
462 debug("%s probe done, bus-num %d.\n", bus->name, bus->seq);
467 static int fsl_dspi_claim_bus(struct udevice *dev)
470 struct fsl_dspi_priv *priv;
471 struct udevice *bus = dev->parent;
472 struct dm_spi_slave_platdata *slave_plat =
473 dev_get_parent_platdata(dev);
475 priv = dev_get_priv(bus);
477 /* processor special preparation work */
478 cpu_dspi_claim_bus(bus->seq, slave_plat->cs);
480 /* configure transfer mode */
481 fsl_dspi_cfg_ctar_mode(priv, slave_plat->cs, priv->mode);
483 /* configure active state of CSX */
484 fsl_dspi_cfg_cs_active_state(priv, slave_plat->cs,
487 fsl_dspi_clr_fifo(priv);
489 /* check module TX and RX status */
490 sr_val = dspi_read32(priv->flags, &priv->regs->sr);
491 if ((sr_val & DSPI_SR_TXRXS) != DSPI_SR_TXRXS) {
492 debug("DSPI RX/TX not ready!\n");
499 static int fsl_dspi_release_bus(struct udevice *dev)
501 struct udevice *bus = dev->parent;
502 struct fsl_dspi_priv *priv = dev_get_priv(bus);
503 struct dm_spi_slave_platdata *slave_plat =
504 dev_get_parent_platdata(dev);
509 /* processor special release work */
510 cpu_dspi_release_bus(bus->seq, slave_plat->cs);
516 * This function doesn't do anything except help with debugging
518 static int fsl_dspi_bind(struct udevice *bus)
520 debug("%s assigned req_seq %d.\n", bus->name, bus->req_seq);
524 static int fsl_dspi_ofdata_to_platdata(struct udevice *bus)
527 struct fsl_dspi_platdata *plat = bus->platdata;
528 const void *blob = gd->fdt_blob;
529 int node = dev_of_offset(bus);
531 if (fdtdec_get_bool(blob, node, "big-endian"))
532 plat->flags |= DSPI_FLAG_REGMAP_ENDIAN_BIG;
534 plat->num_chipselect =
535 fdtdec_get_int(blob, node, "num-cs", FSL_DSPI_MAX_CHIPSELECT);
537 addr = devfdt_get_addr(bus);
538 if (addr == FDT_ADDR_T_NONE) {
539 debug("DSPI: Can't get base address or size\n");
542 plat->regs_addr = addr;
544 plat->speed_hz = fdtdec_get_int(blob,
545 node, "spi-max-frequency", FSL_DSPI_DEFAULT_SCK_FREQ);
547 debug("DSPI: regs=%pa, max-frequency=%d, endianess=%s, num-cs=%d\n",
548 &plat->regs_addr, plat->speed_hz,
549 plat->flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ? "be" : "le",
550 plat->num_chipselect);
555 static int fsl_dspi_xfer(struct udevice *dev, unsigned int bitlen,
556 const void *dout, void *din, unsigned long flags)
558 struct fsl_dspi_priv *priv;
559 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
563 priv = dev_get_priv(bus);
565 return dspi_xfer(priv, slave_plat->cs, bitlen, dout, din, flags);
568 static int fsl_dspi_set_speed(struct udevice *bus, uint speed)
570 struct fsl_dspi_priv *priv = dev_get_priv(bus);
572 return fsl_dspi_cfg_speed(priv, speed);
575 static int fsl_dspi_set_mode(struct udevice *bus, uint mode)
577 struct fsl_dspi_priv *priv = dev_get_priv(bus);
579 debug("DSPI set_mode: mode 0x%x.\n", mode);
582 * We store some chipselect special configure value in priv->ctar_val,
583 * and we can't get the correct chipselect number here,
584 * so just store mode value.
585 * Do really configuration when claim_bus.
592 static const struct dm_spi_ops fsl_dspi_ops = {
593 .claim_bus = fsl_dspi_claim_bus,
594 .release_bus = fsl_dspi_release_bus,
595 .xfer = fsl_dspi_xfer,
596 .set_speed = fsl_dspi_set_speed,
597 .set_mode = fsl_dspi_set_mode,
600 static const struct udevice_id fsl_dspi_ids[] = {
601 { .compatible = "fsl,vf610-dspi" },
605 U_BOOT_DRIVER(fsl_dspi) = {
608 .of_match = fsl_dspi_ids,
609 .ops = &fsl_dspi_ops,
610 .ofdata_to_platdata = fsl_dspi_ofdata_to_platdata,
611 .platdata_auto_alloc_size = sizeof(struct fsl_dspi_platdata),
612 .priv_auto_alloc_size = sizeof(struct fsl_dspi_priv),
613 .probe = fsl_dspi_probe,
614 .child_pre_probe = fsl_dspi_child_pre_probe,
615 .bind = fsl_dspi_bind,