1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
5 * Driver for STMicroelectronics Serial peripheral interface (SPI)
15 #include <dm/device_compat.h>
16 #include <linux/delay.h>
20 #include <linux/bitfield.h>
21 #include <linux/iopoll.h>
23 /* STM32 SPI registers */
24 #define STM32_SPI_CR1 0x00
25 #define STM32_SPI_CR2 0x04
26 #define STM32_SPI_CFG1 0x08
27 #define STM32_SPI_CFG2 0x0C
28 #define STM32_SPI_SR 0x14
29 #define STM32_SPI_IFCR 0x18
30 #define STM32_SPI_TXDR 0x20
31 #define STM32_SPI_RXDR 0x30
32 #define STM32_SPI_I2SCFGR 0x50
34 /* STM32_SPI_CR1 bit fields */
35 #define SPI_CR1_SPE BIT(0)
36 #define SPI_CR1_MASRX BIT(8)
37 #define SPI_CR1_CSTART BIT(9)
38 #define SPI_CR1_CSUSP BIT(10)
39 #define SPI_CR1_HDDIR BIT(11)
40 #define SPI_CR1_SSI BIT(12)
42 /* STM32_SPI_CR2 bit fields */
43 #define SPI_CR2_TSIZE GENMASK(15, 0)
45 /* STM32_SPI_CFG1 bit fields */
46 #define SPI_CFG1_DSIZE GENMASK(4, 0)
47 #define SPI_CFG1_DSIZE_MIN 3
48 #define SPI_CFG1_FTHLV_SHIFT 5
49 #define SPI_CFG1_FTHLV GENMASK(8, 5)
50 #define SPI_CFG1_MBR_SHIFT 28
51 #define SPI_CFG1_MBR GENMASK(30, 28)
52 #define SPI_CFG1_MBR_MIN 0
53 #define SPI_CFG1_MBR_MAX FIELD_GET(SPI_CFG1_MBR, SPI_CFG1_MBR)
55 /* STM32_SPI_CFG2 bit fields */
56 #define SPI_CFG2_COMM_SHIFT 17
57 #define SPI_CFG2_COMM GENMASK(18, 17)
58 #define SPI_CFG2_MASTER BIT(22)
59 #define SPI_CFG2_LSBFRST BIT(23)
60 #define SPI_CFG2_CPHA BIT(24)
61 #define SPI_CFG2_CPOL BIT(25)
62 #define SPI_CFG2_SSM BIT(26)
63 #define SPI_CFG2_AFCNTR BIT(31)
65 /* STM32_SPI_SR bit fields */
66 #define SPI_SR_RXP BIT(0)
67 #define SPI_SR_TXP BIT(1)
68 #define SPI_SR_EOT BIT(3)
69 #define SPI_SR_TXTF BIT(4)
70 #define SPI_SR_OVR BIT(6)
71 #define SPI_SR_SUSP BIT(11)
72 #define SPI_SR_RXPLVL_SHIFT 13
73 #define SPI_SR_RXPLVL GENMASK(14, 13)
74 #define SPI_SR_RXWNE BIT(15)
76 /* STM32_SPI_IFCR bit fields */
77 #define SPI_IFCR_ALL GENMASK(11, 3)
79 /* STM32_SPI_I2SCFGR bit fields */
80 #define SPI_I2SCFGR_I2SMOD BIT(0)
82 #define MAX_CS_COUNT 4
84 /* SPI Master Baud Rate min/max divisor */
85 #define STM32_MBR_DIV_MIN (2 << SPI_CFG1_MBR_MIN)
86 #define STM32_MBR_DIV_MAX (2 << SPI_CFG1_MBR_MAX)
88 #define STM32_SPI_TIMEOUT_US 100000
90 /* SPI Communication mode */
91 #define SPI_FULL_DUPLEX 0
92 #define SPI_SIMPLEX_TX 1
93 #define SPI_SIMPLEX_RX 2
94 #define SPI_HALF_DUPLEX 3
96 struct stm32_spi_priv {
99 struct reset_ctl rst_ctl;
100 struct gpio_desc cs_gpios[MAX_CS_COUNT];
102 unsigned int fifo_size;
103 unsigned int cur_bpw;
105 unsigned int cur_xferlen; /* current transfer length in bytes */
106 unsigned int tx_len; /* number of data to be written in bytes */
107 unsigned int rx_len; /* number of data to be read in bytes */
108 const void *tx_buf; /* data to be written, or NULL */
109 void *rx_buf; /* data to be read, or NULL */
114 static void stm32_spi_write_txfifo(struct stm32_spi_priv *priv)
116 while ((priv->tx_len > 0) &&
117 (readl(priv->base + STM32_SPI_SR) & SPI_SR_TXP)) {
118 u32 offs = priv->cur_xferlen - priv->tx_len;
120 if (priv->tx_len >= sizeof(u32) &&
121 IS_ALIGNED((uintptr_t)(priv->tx_buf + offs), sizeof(u32))) {
122 const u32 *tx_buf32 = (const u32 *)(priv->tx_buf + offs);
124 writel(*tx_buf32, priv->base + STM32_SPI_TXDR);
125 priv->tx_len -= sizeof(u32);
126 } else if (priv->tx_len >= sizeof(u16) &&
127 IS_ALIGNED((uintptr_t)(priv->tx_buf + offs), sizeof(u16))) {
128 const u16 *tx_buf16 = (const u16 *)(priv->tx_buf + offs);
130 writew(*tx_buf16, priv->base + STM32_SPI_TXDR);
131 priv->tx_len -= sizeof(u16);
133 const u8 *tx_buf8 = (const u8 *)(priv->tx_buf + offs);
135 writeb(*tx_buf8, priv->base + STM32_SPI_TXDR);
136 priv->tx_len -= sizeof(u8);
140 debug("%s: %d bytes left\n", __func__, priv->tx_len);
143 static void stm32_spi_read_rxfifo(struct stm32_spi_priv *priv)
145 u32 sr = readl(priv->base + STM32_SPI_SR);
146 u32 rxplvl = (sr & SPI_SR_RXPLVL) >> SPI_SR_RXPLVL_SHIFT;
148 while ((priv->rx_len > 0) &&
149 ((sr & SPI_SR_RXP) ||
150 ((sr & SPI_SR_EOT) && ((sr & SPI_SR_RXWNE) || (rxplvl > 0))))) {
151 u32 offs = priv->cur_xferlen - priv->rx_len;
153 if (IS_ALIGNED((uintptr_t)(priv->rx_buf + offs), sizeof(u32)) &&
154 (priv->rx_len >= sizeof(u32) || (sr & SPI_SR_RXWNE))) {
155 u32 *rx_buf32 = (u32 *)(priv->rx_buf + offs);
157 *rx_buf32 = readl(priv->base + STM32_SPI_RXDR);
158 priv->rx_len -= sizeof(u32);
159 } else if (IS_ALIGNED((uintptr_t)(priv->rx_buf + offs), sizeof(u16)) &&
160 (priv->rx_len >= sizeof(u16) ||
161 (!(sr & SPI_SR_RXWNE) &&
162 (rxplvl >= 2 || priv->cur_bpw > 8)))) {
163 u16 *rx_buf16 = (u16 *)(priv->rx_buf + offs);
165 *rx_buf16 = readw(priv->base + STM32_SPI_RXDR);
166 priv->rx_len -= sizeof(u16);
168 u8 *rx_buf8 = (u8 *)(priv->rx_buf + offs);
170 *rx_buf8 = readb(priv->base + STM32_SPI_RXDR);
171 priv->rx_len -= sizeof(u8);
174 sr = readl(priv->base + STM32_SPI_SR);
175 rxplvl = (sr & SPI_SR_RXPLVL) >> SPI_SR_RXPLVL_SHIFT;
178 debug("%s: %d bytes left\n", __func__, priv->rx_len);
181 static int stm32_spi_enable(struct stm32_spi_priv *priv)
183 debug("%s\n", __func__);
185 /* Enable the SPI hardware */
186 setbits_le32(priv->base + STM32_SPI_CR1, SPI_CR1_SPE);
191 static int stm32_spi_disable(struct stm32_spi_priv *priv)
193 debug("%s\n", __func__);
195 /* Disable the SPI hardware */
196 clrbits_le32(priv->base + STM32_SPI_CR1, SPI_CR1_SPE);
201 static int stm32_spi_claim_bus(struct udevice *slave)
203 struct udevice *bus = dev_get_parent(slave);
204 struct stm32_spi_priv *priv = dev_get_priv(bus);
206 debug("%s\n", __func__);
208 /* Enable the SPI hardware */
209 return stm32_spi_enable(priv);
212 static int stm32_spi_release_bus(struct udevice *slave)
214 struct udevice *bus = dev_get_parent(slave);
215 struct stm32_spi_priv *priv = dev_get_priv(bus);
217 debug("%s\n", __func__);
219 /* Disable the SPI hardware */
220 return stm32_spi_disable(priv);
223 static void stm32_spi_stopxfer(struct udevice *dev)
225 struct stm32_spi_priv *priv = dev_get_priv(dev);
229 debug("%s\n", __func__);
231 cr1 = readl(priv->base + STM32_SPI_CR1);
233 if (!(cr1 & SPI_CR1_SPE))
236 /* Wait on EOT or suspend the flow */
237 ret = readl_poll_timeout(priv->base + STM32_SPI_SR, sr,
238 !(sr & SPI_SR_EOT), 100000);
240 if (cr1 & SPI_CR1_CSTART) {
241 writel(cr1 | SPI_CR1_CSUSP, priv->base + STM32_SPI_CR1);
242 if (readl_poll_timeout(priv->base + STM32_SPI_SR,
243 sr, !(sr & SPI_SR_SUSP),
245 dev_err(dev, "Suspend request timeout\n");
249 /* clear status flags */
250 setbits_le32(priv->base + STM32_SPI_IFCR, SPI_IFCR_ALL);
253 static int stm32_spi_set_cs(struct udevice *dev, unsigned int cs, bool enable)
255 struct stm32_spi_priv *priv = dev_get_priv(dev);
257 debug("%s: cs=%d enable=%d\n", __func__, cs, enable);
259 if (cs >= MAX_CS_COUNT)
262 if (!dm_gpio_is_valid(&priv->cs_gpios[cs]))
268 return dm_gpio_set_value(&priv->cs_gpios[cs], enable ? 1 : 0);
271 static int stm32_spi_set_mode(struct udevice *bus, uint mode)
273 struct stm32_spi_priv *priv = dev_get_priv(bus);
274 u32 cfg2_clrb = 0, cfg2_setb = 0;
276 debug("%s: mode=%d\n", __func__, mode);
279 cfg2_setb |= SPI_CFG2_CPOL;
281 cfg2_clrb |= SPI_CFG2_CPOL;
284 cfg2_setb |= SPI_CFG2_CPHA;
286 cfg2_clrb |= SPI_CFG2_CPHA;
288 if (mode & SPI_LSB_FIRST)
289 cfg2_setb |= SPI_CFG2_LSBFRST;
291 cfg2_clrb |= SPI_CFG2_LSBFRST;
293 if (cfg2_clrb || cfg2_setb)
294 clrsetbits_le32(priv->base + STM32_SPI_CFG2,
295 cfg2_clrb, cfg2_setb);
297 if (mode & SPI_CS_HIGH)
298 priv->cs_high = true;
300 priv->cs_high = false;
304 static int stm32_spi_set_fthlv(struct udevice *dev, u32 xfer_len)
306 struct stm32_spi_priv *priv = dev_get_priv(dev);
307 u32 fthlv, half_fifo;
309 /* data packet should not exceed 1/2 of fifo space */
310 half_fifo = (priv->fifo_size / 2);
312 /* data_packet should not exceed transfer length */
313 fthlv = (half_fifo > xfer_len) ? xfer_len : half_fifo;
315 /* align packet size with data registers access */
316 fthlv -= (fthlv % 4);
320 clrsetbits_le32(priv->base + STM32_SPI_CFG1, SPI_CFG1_FTHLV,
321 (fthlv - 1) << SPI_CFG1_FTHLV_SHIFT);
326 static int stm32_spi_set_speed(struct udevice *bus, uint hz)
328 struct stm32_spi_priv *priv = dev_get_priv(bus);
332 debug("%s: hz=%d\n", __func__, hz);
334 if (priv->cur_hz == hz)
337 div = DIV_ROUND_UP(priv->bus_clk_rate, hz);
339 if (div < STM32_MBR_DIV_MIN ||
340 div > STM32_MBR_DIV_MAX)
343 /* Determine the first power of 2 greater than or equal to div */
347 mbrdiv = fls(div) - 1;
352 clrsetbits_le32(priv->base + STM32_SPI_CFG1, SPI_CFG1_MBR,
353 (mbrdiv - 1) << SPI_CFG1_MBR_SHIFT);
360 static int stm32_spi_xfer(struct udevice *slave, unsigned int bitlen,
361 const void *dout, void *din, unsigned long flags)
363 struct udevice *bus = dev_get_parent(slave);
364 struct dm_spi_slave_platdata *slave_plat;
365 struct stm32_spi_priv *priv = dev_get_priv(bus);
372 xferlen = bitlen / 8;
374 if (xferlen <= SPI_CR2_TSIZE)
375 writel(xferlen, priv->base + STM32_SPI_CR2);
381 priv->tx_len = priv->tx_buf ? bitlen / 8 : 0;
382 priv->rx_len = priv->rx_buf ? bitlen / 8 : 0;
384 mode = SPI_FULL_DUPLEX;
386 mode = SPI_SIMPLEX_RX;
387 else if (!priv->rx_buf)
388 mode = SPI_SIMPLEX_TX;
390 if (priv->cur_xferlen != xferlen || priv->cur_mode != mode) {
391 priv->cur_mode = mode;
392 priv->cur_xferlen = xferlen;
394 /* Disable the SPI hardware to unlock CFG1/CFG2 registers */
395 stm32_spi_disable(priv);
397 clrsetbits_le32(priv->base + STM32_SPI_CFG2, SPI_CFG2_COMM,
398 mode << SPI_CFG2_COMM_SHIFT);
400 stm32_spi_set_fthlv(bus, xferlen);
402 /* Enable the SPI hardware */
403 stm32_spi_enable(priv);
406 debug("%s: priv->tx_len=%d priv->rx_len=%d\n", __func__,
407 priv->tx_len, priv->rx_len);
409 slave_plat = dev_get_parent_platdata(slave);
410 if (flags & SPI_XFER_BEGIN)
411 stm32_spi_set_cs(bus, slave_plat->cs, false);
413 /* Be sure to have data in fifo before starting data transfer */
415 stm32_spi_write_txfifo(priv);
417 setbits_le32(priv->base + STM32_SPI_CR1, SPI_CR1_CSTART);
420 sr = readl(priv->base + STM32_SPI_SR);
422 if (sr & SPI_SR_OVR) {
423 dev_err(bus, "Overrun: RX data lost\n");
428 if (sr & SPI_SR_SUSP) {
429 dev_warn(bus, "System too slow is limiting data throughput\n");
431 if (priv->rx_buf && priv->rx_len > 0)
432 stm32_spi_read_rxfifo(priv);
437 if (sr & SPI_SR_TXTF)
441 if (priv->tx_buf && priv->tx_len > 0)
442 stm32_spi_write_txfifo(priv);
445 if (priv->rx_buf && priv->rx_len > 0)
446 stm32_spi_read_rxfifo(priv);
448 if (sr & SPI_SR_EOT) {
449 if (priv->rx_buf && priv->rx_len > 0)
450 stm32_spi_read_rxfifo(priv);
454 writel(ifcr, priv->base + STM32_SPI_IFCR);
457 /* clear status flags */
458 setbits_le32(priv->base + STM32_SPI_IFCR, SPI_IFCR_ALL);
459 stm32_spi_stopxfer(bus);
461 if (flags & SPI_XFER_END)
462 stm32_spi_set_cs(bus, slave_plat->cs, true);
467 static int stm32_spi_get_fifo_size(struct udevice *dev)
469 struct stm32_spi_priv *priv = dev_get_priv(dev);
472 stm32_spi_enable(priv);
474 while (readl(priv->base + STM32_SPI_SR) & SPI_SR_TXP)
475 writeb(++count, priv->base + STM32_SPI_TXDR);
477 stm32_spi_disable(priv);
479 debug("%s %d x 8-bit fifo size\n", __func__, count);
484 static int stm32_spi_probe(struct udevice *dev)
486 struct stm32_spi_priv *priv = dev_get_priv(dev);
487 unsigned long clk_rate;
491 priv->base = dev_remap_addr(dev);
496 ret = clk_get_by_index(dev, 0, &priv->clk);
500 ret = clk_enable(&priv->clk);
504 clk_rate = clk_get_rate(&priv->clk);
510 priv->bus_clk_rate = clk_rate;
513 ret = reset_get_by_index(dev, 0, &priv->rst_ctl);
517 reset_assert(&priv->rst_ctl);
519 reset_deassert(&priv->rst_ctl);
521 ret = gpio_request_list_by_name(dev, "cs-gpios", priv->cs_gpios,
522 ARRAY_SIZE(priv->cs_gpios), 0);
524 pr_err("Can't get %s cs gpios: %d", dev->name, ret);
528 priv->fifo_size = stm32_spi_get_fifo_size(dev);
530 priv->cur_mode = SPI_FULL_DUPLEX;
531 priv->cur_xferlen = 0;
532 priv->cur_bpw = SPI_DEFAULT_WORDLEN;
533 clrsetbits_le32(priv->base + STM32_SPI_CFG1, SPI_CFG1_DSIZE,
536 for (i = 0; i < ARRAY_SIZE(priv->cs_gpios); i++) {
537 if (!dm_gpio_is_valid(&priv->cs_gpios[i]))
540 dm_gpio_set_dir_flags(&priv->cs_gpios[i],
541 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
544 /* Ensure I2SMOD bit is kept cleared */
545 clrbits_le32(priv->base + STM32_SPI_I2SCFGR, SPI_I2SCFGR_I2SMOD);
548 * - SS input value high
549 * - transmitter half duplex direction
550 * - automatic communication suspend when RX-Fifo is full
552 setbits_le32(priv->base + STM32_SPI_CR1,
553 SPI_CR1_SSI | SPI_CR1_HDDIR | SPI_CR1_MASRX);
556 * - Set the master mode (default Motorola mode)
557 * - Consider 1 master/n slaves configuration and
558 * SS input value is determined by the SSI bit
559 * - keep control of all associated GPIOs
561 setbits_le32(priv->base + STM32_SPI_CFG2,
562 SPI_CFG2_MASTER | SPI_CFG2_SSM | SPI_CFG2_AFCNTR);
567 reset_free(&priv->rst_ctl);
570 clk_disable(&priv->clk);
571 clk_free(&priv->clk);
576 static int stm32_spi_remove(struct udevice *dev)
578 struct stm32_spi_priv *priv = dev_get_priv(dev);
581 stm32_spi_stopxfer(dev);
582 stm32_spi_disable(priv);
584 ret = reset_assert(&priv->rst_ctl);
588 reset_free(&priv->rst_ctl);
590 ret = clk_disable(&priv->clk);
594 clk_free(&priv->clk);
599 static const struct dm_spi_ops stm32_spi_ops = {
600 .claim_bus = stm32_spi_claim_bus,
601 .release_bus = stm32_spi_release_bus,
602 .set_mode = stm32_spi_set_mode,
603 .set_speed = stm32_spi_set_speed,
604 .xfer = stm32_spi_xfer,
607 static const struct udevice_id stm32_spi_ids[] = {
608 { .compatible = "st,stm32h7-spi", },
612 U_BOOT_DRIVER(stm32_spi) = {
615 .of_match = stm32_spi_ids,
616 .ops = &stm32_spi_ops,
617 .priv_auto_alloc_size = sizeof(struct stm32_spi_priv),
618 .probe = stm32_spi_probe,
619 .remove = stm32_spi_remove,