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)
16 #include <linux/bitfield.h>
17 #include <linux/iopoll.h>
19 /* STM32 SPI registers */
20 #define STM32_SPI_CR1 0x00
21 #define STM32_SPI_CR2 0x04
22 #define STM32_SPI_CFG1 0x08
23 #define STM32_SPI_CFG2 0x0C
24 #define STM32_SPI_SR 0x14
25 #define STM32_SPI_IFCR 0x18
26 #define STM32_SPI_TXDR 0x20
27 #define STM32_SPI_RXDR 0x30
28 #define STM32_SPI_I2SCFGR 0x50
30 /* STM32_SPI_CR1 bit fields */
31 #define SPI_CR1_SPE BIT(0)
32 #define SPI_CR1_MASRX BIT(8)
33 #define SPI_CR1_CSTART BIT(9)
34 #define SPI_CR1_CSUSP BIT(10)
35 #define SPI_CR1_HDDIR BIT(11)
36 #define SPI_CR1_SSI BIT(12)
38 /* STM32_SPI_CR2 bit fields */
39 #define SPI_CR2_TSIZE GENMASK(15, 0)
41 /* STM32_SPI_CFG1 bit fields */
42 #define SPI_CFG1_DSIZE GENMASK(4, 0)
43 #define SPI_CFG1_DSIZE_MIN 3
44 #define SPI_CFG1_FTHLV_SHIFT 5
45 #define SPI_CFG1_FTHLV GENMASK(8, 5)
46 #define SPI_CFG1_MBR_SHIFT 28
47 #define SPI_CFG1_MBR GENMASK(30, 28)
48 #define SPI_CFG1_MBR_MIN 0
49 #define SPI_CFG1_MBR_MAX FIELD_GET(SPI_CFG1_MBR, SPI_CFG1_MBR)
51 /* STM32_SPI_CFG2 bit fields */
52 #define SPI_CFG2_COMM_SHIFT 17
53 #define SPI_CFG2_COMM GENMASK(18, 17)
54 #define SPI_CFG2_MASTER BIT(22)
55 #define SPI_CFG2_LSBFRST BIT(23)
56 #define SPI_CFG2_CPHA BIT(24)
57 #define SPI_CFG2_CPOL BIT(25)
58 #define SPI_CFG2_SSM BIT(26)
59 #define SPI_CFG2_AFCNTR BIT(31)
61 /* STM32_SPI_SR bit fields */
62 #define SPI_SR_RXP BIT(0)
63 #define SPI_SR_TXP BIT(1)
64 #define SPI_SR_EOT BIT(3)
65 #define SPI_SR_TXTF BIT(4)
66 #define SPI_SR_OVR BIT(6)
67 #define SPI_SR_SUSP BIT(11)
68 #define SPI_SR_RXPLVL_SHIFT 13
69 #define SPI_SR_RXPLVL GENMASK(14, 13)
70 #define SPI_SR_RXWNE BIT(15)
72 /* STM32_SPI_IFCR bit fields */
73 #define SPI_IFCR_ALL GENMASK(11, 3)
75 /* STM32_SPI_I2SCFGR bit fields */
76 #define SPI_I2SCFGR_I2SMOD BIT(0)
78 #define MAX_CS_COUNT 4
80 /* SPI Master Baud Rate min/max divisor */
81 #define STM32_MBR_DIV_MIN (2 << SPI_CFG1_MBR_MIN)
82 #define STM32_MBR_DIV_MAX (2 << SPI_CFG1_MBR_MAX)
84 #define STM32_SPI_TIMEOUT_US 100000
86 /* SPI Communication mode */
87 #define SPI_FULL_DUPLEX 0
88 #define SPI_SIMPLEX_TX 1
89 #define SPI_SIMPLEX_RX 2
90 #define SPI_HALF_DUPLEX 3
92 struct stm32_spi_priv {
95 struct reset_ctl rst_ctl;
96 struct gpio_desc cs_gpios[MAX_CS_COUNT];
98 unsigned int fifo_size;
101 unsigned int cur_xferlen; /* current transfer length in bytes */
102 unsigned int tx_len; /* number of data to be written in bytes */
103 unsigned int rx_len; /* number of data to be read in bytes */
104 const void *tx_buf; /* data to be written, or NULL */
105 void *rx_buf; /* data to be read, or NULL */
110 static void stm32_spi_write_txfifo(struct stm32_spi_priv *priv)
112 while ((priv->tx_len > 0) &&
113 (readl(priv->base + STM32_SPI_SR) & SPI_SR_TXP)) {
114 u32 offs = priv->cur_xferlen - priv->tx_len;
116 if (priv->tx_len >= sizeof(u32) &&
117 IS_ALIGNED((uintptr_t)(priv->tx_buf + offs), sizeof(u32))) {
118 const u32 *tx_buf32 = (const u32 *)(priv->tx_buf + offs);
120 writel(*tx_buf32, priv->base + STM32_SPI_TXDR);
121 priv->tx_len -= sizeof(u32);
122 } else if (priv->tx_len >= sizeof(u16) &&
123 IS_ALIGNED((uintptr_t)(priv->tx_buf + offs), sizeof(u16))) {
124 const u16 *tx_buf16 = (const u16 *)(priv->tx_buf + offs);
126 writew(*tx_buf16, priv->base + STM32_SPI_TXDR);
127 priv->tx_len -= sizeof(u16);
129 const u8 *tx_buf8 = (const u8 *)(priv->tx_buf + offs);
131 writeb(*tx_buf8, priv->base + STM32_SPI_TXDR);
132 priv->tx_len -= sizeof(u8);
136 debug("%s: %d bytes left\n", __func__, priv->tx_len);
139 static void stm32_spi_read_rxfifo(struct stm32_spi_priv *priv)
141 u32 sr = readl(priv->base + STM32_SPI_SR);
142 u32 rxplvl = (sr & SPI_SR_RXPLVL) >> SPI_SR_RXPLVL_SHIFT;
144 while ((priv->rx_len > 0) &&
145 ((sr & SPI_SR_RXP) ||
146 ((sr & SPI_SR_EOT) && ((sr & SPI_SR_RXWNE) || (rxplvl > 0))))) {
147 u32 offs = priv->cur_xferlen - priv->rx_len;
149 if (IS_ALIGNED((uintptr_t)(priv->rx_buf + offs), sizeof(u32)) &&
150 (priv->rx_len >= sizeof(u32) || (sr & SPI_SR_RXWNE))) {
151 u32 *rx_buf32 = (u32 *)(priv->rx_buf + offs);
153 *rx_buf32 = readl(priv->base + STM32_SPI_RXDR);
154 priv->rx_len -= sizeof(u32);
155 } else if (IS_ALIGNED((uintptr_t)(priv->rx_buf + offs), sizeof(u16)) &&
156 (priv->rx_len >= sizeof(u16) ||
157 (!(sr & SPI_SR_RXWNE) &&
158 (rxplvl >= 2 || priv->cur_bpw > 8)))) {
159 u16 *rx_buf16 = (u16 *)(priv->rx_buf + offs);
161 *rx_buf16 = readw(priv->base + STM32_SPI_RXDR);
162 priv->rx_len -= sizeof(u16);
164 u8 *rx_buf8 = (u8 *)(priv->rx_buf + offs);
166 *rx_buf8 = readb(priv->base + STM32_SPI_RXDR);
167 priv->rx_len -= sizeof(u8);
170 sr = readl(priv->base + STM32_SPI_SR);
171 rxplvl = (sr & SPI_SR_RXPLVL) >> SPI_SR_RXPLVL_SHIFT;
174 debug("%s: %d bytes left\n", __func__, priv->rx_len);
177 static int stm32_spi_enable(struct stm32_spi_priv *priv)
179 debug("%s\n", __func__);
181 /* Enable the SPI hardware */
182 setbits_le32(priv->base + STM32_SPI_CR1, SPI_CR1_SPE);
187 static int stm32_spi_disable(struct stm32_spi_priv *priv)
189 debug("%s\n", __func__);
191 /* Disable the SPI hardware */
192 clrbits_le32(priv->base + STM32_SPI_CR1, SPI_CR1_SPE);
197 static int stm32_spi_claim_bus(struct udevice *slave)
199 struct udevice *bus = dev_get_parent(slave);
200 struct stm32_spi_priv *priv = dev_get_priv(bus);
202 debug("%s\n", __func__);
204 /* Enable the SPI hardware */
205 return stm32_spi_enable(priv);
208 static int stm32_spi_release_bus(struct udevice *slave)
210 struct udevice *bus = dev_get_parent(slave);
211 struct stm32_spi_priv *priv = dev_get_priv(bus);
213 debug("%s\n", __func__);
215 /* Disable the SPI hardware */
216 return stm32_spi_disable(priv);
219 static void stm32_spi_stopxfer(struct udevice *dev)
221 struct stm32_spi_priv *priv = dev_get_priv(dev);
225 debug("%s\n", __func__);
227 cr1 = readl(priv->base + STM32_SPI_CR1);
229 if (!(cr1 & SPI_CR1_SPE))
232 /* Wait on EOT or suspend the flow */
233 ret = readl_poll_timeout(priv->base + STM32_SPI_SR, sr,
234 !(sr & SPI_SR_EOT), 100000);
236 if (cr1 & SPI_CR1_CSTART) {
237 writel(cr1 | SPI_CR1_CSUSP, priv->base + STM32_SPI_CR1);
238 if (readl_poll_timeout(priv->base + STM32_SPI_SR,
239 sr, !(sr & SPI_SR_SUSP),
241 dev_err(dev, "Suspend request timeout\n");
245 /* clear status flags */
246 setbits_le32(priv->base + STM32_SPI_IFCR, SPI_IFCR_ALL);
249 static int stm32_spi_set_cs(struct udevice *dev, unsigned int cs, bool enable)
251 struct stm32_spi_priv *priv = dev_get_priv(dev);
253 debug("%s: cs=%d enable=%d\n", __func__, cs, enable);
255 if (cs >= MAX_CS_COUNT)
258 if (!dm_gpio_is_valid(&priv->cs_gpios[cs]))
264 return dm_gpio_set_value(&priv->cs_gpios[cs], enable ? 1 : 0);
267 static int stm32_spi_set_mode(struct udevice *bus, uint mode)
269 struct stm32_spi_priv *priv = dev_get_priv(bus);
270 u32 cfg2_clrb = 0, cfg2_setb = 0;
272 debug("%s: mode=%d\n", __func__, mode);
275 cfg2_setb |= SPI_CFG2_CPOL;
277 cfg2_clrb |= SPI_CFG2_CPOL;
280 cfg2_setb |= SPI_CFG2_CPHA;
282 cfg2_clrb |= SPI_CFG2_CPHA;
284 if (mode & SPI_LSB_FIRST)
285 cfg2_setb |= SPI_CFG2_LSBFRST;
287 cfg2_clrb |= SPI_CFG2_LSBFRST;
289 if (cfg2_clrb || cfg2_setb)
290 clrsetbits_le32(priv->base + STM32_SPI_CFG2,
291 cfg2_clrb, cfg2_setb);
293 if (mode & SPI_CS_HIGH)
294 priv->cs_high = true;
296 priv->cs_high = false;
300 static int stm32_spi_set_fthlv(struct udevice *dev, u32 xfer_len)
302 struct stm32_spi_priv *priv = dev_get_priv(dev);
303 u32 fthlv, half_fifo;
305 /* data packet should not exceed 1/2 of fifo space */
306 half_fifo = (priv->fifo_size / 2);
308 /* data_packet should not exceed transfer length */
309 fthlv = (half_fifo > xfer_len) ? xfer_len : half_fifo;
311 /* align packet size with data registers access */
312 fthlv -= (fthlv % 4);
316 clrsetbits_le32(priv->base + STM32_SPI_CFG1, SPI_CFG1_FTHLV,
317 (fthlv - 1) << SPI_CFG1_FTHLV_SHIFT);
322 static int stm32_spi_set_speed(struct udevice *bus, uint hz)
324 struct stm32_spi_priv *priv = dev_get_priv(bus);
328 debug("%s: hz=%d\n", __func__, hz);
330 if (priv->cur_hz == hz)
333 div = DIV_ROUND_UP(priv->bus_clk_rate, hz);
335 if (div < STM32_MBR_DIV_MIN ||
336 div > STM32_MBR_DIV_MAX)
339 /* Determine the first power of 2 greater than or equal to div */
343 mbrdiv = fls(div) - 1;
348 clrsetbits_le32(priv->base + STM32_SPI_CFG1, SPI_CFG1_MBR,
349 (mbrdiv - 1) << SPI_CFG1_MBR_SHIFT);
356 static int stm32_spi_xfer(struct udevice *slave, unsigned int bitlen,
357 const void *dout, void *din, unsigned long flags)
359 struct udevice *bus = dev_get_parent(slave);
360 struct dm_spi_slave_platdata *slave_plat;
361 struct stm32_spi_priv *priv = dev_get_priv(bus);
368 xferlen = bitlen / 8;
370 if (xferlen <= SPI_CR2_TSIZE)
371 writel(xferlen, priv->base + STM32_SPI_CR2);
377 priv->tx_len = priv->tx_buf ? bitlen / 8 : 0;
378 priv->rx_len = priv->rx_buf ? bitlen / 8 : 0;
380 mode = SPI_FULL_DUPLEX;
382 mode = SPI_SIMPLEX_RX;
383 else if (!priv->rx_buf)
384 mode = SPI_SIMPLEX_TX;
386 if (priv->cur_xferlen != xferlen || priv->cur_mode != mode) {
387 priv->cur_mode = mode;
388 priv->cur_xferlen = xferlen;
390 /* Disable the SPI hardware to unlock CFG1/CFG2 registers */
391 stm32_spi_disable(priv);
393 clrsetbits_le32(priv->base + STM32_SPI_CFG2, SPI_CFG2_COMM,
394 mode << SPI_CFG2_COMM_SHIFT);
396 stm32_spi_set_fthlv(bus, xferlen);
398 /* Enable the SPI hardware */
399 stm32_spi_enable(priv);
402 debug("%s: priv->tx_len=%d priv->rx_len=%d\n", __func__,
403 priv->tx_len, priv->rx_len);
405 slave_plat = dev_get_parent_platdata(slave);
406 if (flags & SPI_XFER_BEGIN)
407 stm32_spi_set_cs(bus, slave_plat->cs, false);
409 /* Be sure to have data in fifo before starting data transfer */
411 stm32_spi_write_txfifo(priv);
413 setbits_le32(priv->base + STM32_SPI_CR1, SPI_CR1_CSTART);
416 sr = readl(priv->base + STM32_SPI_SR);
418 if (sr & SPI_SR_OVR) {
419 dev_err(bus, "Overrun: RX data lost\n");
424 if (sr & SPI_SR_SUSP) {
425 dev_warn(bus, "System too slow is limiting data throughput\n");
427 if (priv->rx_buf && priv->rx_len > 0)
428 stm32_spi_read_rxfifo(priv);
433 if (sr & SPI_SR_TXTF)
437 if (priv->tx_buf && priv->tx_len > 0)
438 stm32_spi_write_txfifo(priv);
441 if (priv->rx_buf && priv->rx_len > 0)
442 stm32_spi_read_rxfifo(priv);
444 if (sr & SPI_SR_EOT) {
445 if (priv->rx_buf && priv->rx_len > 0)
446 stm32_spi_read_rxfifo(priv);
450 writel(ifcr, priv->base + STM32_SPI_IFCR);
453 /* clear status flags */
454 setbits_le32(priv->base + STM32_SPI_IFCR, SPI_IFCR_ALL);
455 stm32_spi_stopxfer(bus);
457 if (flags & SPI_XFER_END)
458 stm32_spi_set_cs(bus, slave_plat->cs, true);
463 static int stm32_spi_get_fifo_size(struct udevice *dev)
465 struct stm32_spi_priv *priv = dev_get_priv(dev);
468 stm32_spi_enable(priv);
470 while (readl(priv->base + STM32_SPI_SR) & SPI_SR_TXP)
471 writeb(++count, priv->base + STM32_SPI_TXDR);
473 stm32_spi_disable(priv);
475 debug("%s %d x 8-bit fifo size\n", __func__, count);
480 static int stm32_spi_probe(struct udevice *dev)
482 struct stm32_spi_priv *priv = dev_get_priv(dev);
483 unsigned long clk_rate;
487 priv->base = dev_remap_addr(dev);
492 ret = clk_get_by_index(dev, 0, &priv->clk);
496 ret = clk_enable(&priv->clk);
500 clk_rate = clk_get_rate(&priv->clk);
506 priv->bus_clk_rate = clk_rate;
509 ret = reset_get_by_index(dev, 0, &priv->rst_ctl);
513 reset_assert(&priv->rst_ctl);
515 reset_deassert(&priv->rst_ctl);
517 ret = gpio_request_list_by_name(dev, "cs-gpios", priv->cs_gpios,
518 ARRAY_SIZE(priv->cs_gpios), 0);
520 pr_err("Can't get %s cs gpios: %d", dev->name, ret);
524 priv->fifo_size = stm32_spi_get_fifo_size(dev);
526 priv->cur_mode = SPI_FULL_DUPLEX;
527 priv->cur_xferlen = 0;
528 priv->cur_bpw = SPI_DEFAULT_WORDLEN;
529 clrsetbits_le32(priv->base + STM32_SPI_CFG1, SPI_CFG1_DSIZE,
532 for (i = 0; i < ARRAY_SIZE(priv->cs_gpios); i++) {
533 if (!dm_gpio_is_valid(&priv->cs_gpios[i]))
536 dm_gpio_set_dir_flags(&priv->cs_gpios[i],
537 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
540 /* Ensure I2SMOD bit is kept cleared */
541 clrbits_le32(priv->base + STM32_SPI_I2SCFGR, SPI_I2SCFGR_I2SMOD);
544 * - SS input value high
545 * - transmitter half duplex direction
546 * - automatic communication suspend when RX-Fifo is full
548 setbits_le32(priv->base + STM32_SPI_CR1,
549 SPI_CR1_SSI | SPI_CR1_HDDIR | SPI_CR1_MASRX);
552 * - Set the master mode (default Motorola mode)
553 * - Consider 1 master/n slaves configuration and
554 * SS input value is determined by the SSI bit
555 * - keep control of all associated GPIOs
557 setbits_le32(priv->base + STM32_SPI_CFG2,
558 SPI_CFG2_MASTER | SPI_CFG2_SSM | SPI_CFG2_AFCNTR);
563 reset_free(&priv->rst_ctl);
566 clk_disable(&priv->clk);
567 clk_free(&priv->clk);
572 static int stm32_spi_remove(struct udevice *dev)
574 struct stm32_spi_priv *priv = dev_get_priv(dev);
577 stm32_spi_stopxfer(dev);
578 stm32_spi_disable(priv);
580 ret = reset_assert(&priv->rst_ctl);
584 reset_free(&priv->rst_ctl);
586 ret = clk_disable(&priv->clk);
590 clk_free(&priv->clk);
595 static const struct dm_spi_ops stm32_spi_ops = {
596 .claim_bus = stm32_spi_claim_bus,
597 .release_bus = stm32_spi_release_bus,
598 .set_mode = stm32_spi_set_mode,
599 .set_speed = stm32_spi_set_speed,
600 .xfer = stm32_spi_xfer,
603 static const struct udevice_id stm32_spi_ids[] = {
604 { .compatible = "st,stm32h7-spi", },
608 U_BOOT_DRIVER(stm32_spi) = {
611 .of_match = stm32_spi_ids,
612 .ops = &stm32_spi_ops,
613 .priv_auto_alloc_size = sizeof(struct stm32_spi_priv),
614 .probe = stm32_spi_probe,
615 .remove = stm32_spi_remove,