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>
19 #include <linux/bitfield.h>
20 #include <linux/iopoll.h>
22 /* STM32 SPI registers */
23 #define STM32_SPI_CR1 0x00
24 #define STM32_SPI_CR2 0x04
25 #define STM32_SPI_CFG1 0x08
26 #define STM32_SPI_CFG2 0x0C
27 #define STM32_SPI_SR 0x14
28 #define STM32_SPI_IFCR 0x18
29 #define STM32_SPI_TXDR 0x20
30 #define STM32_SPI_RXDR 0x30
31 #define STM32_SPI_I2SCFGR 0x50
33 /* STM32_SPI_CR1 bit fields */
34 #define SPI_CR1_SPE BIT(0)
35 #define SPI_CR1_MASRX BIT(8)
36 #define SPI_CR1_CSTART BIT(9)
37 #define SPI_CR1_CSUSP BIT(10)
38 #define SPI_CR1_HDDIR BIT(11)
39 #define SPI_CR1_SSI BIT(12)
41 /* STM32_SPI_CR2 bit fields */
42 #define SPI_CR2_TSIZE GENMASK(15, 0)
44 /* STM32_SPI_CFG1 bit fields */
45 #define SPI_CFG1_DSIZE GENMASK(4, 0)
46 #define SPI_CFG1_DSIZE_MIN 3
47 #define SPI_CFG1_FTHLV_SHIFT 5
48 #define SPI_CFG1_FTHLV GENMASK(8, 5)
49 #define SPI_CFG1_MBR_SHIFT 28
50 #define SPI_CFG1_MBR GENMASK(30, 28)
51 #define SPI_CFG1_MBR_MIN 0
52 #define SPI_CFG1_MBR_MAX FIELD_GET(SPI_CFG1_MBR, SPI_CFG1_MBR)
54 /* STM32_SPI_CFG2 bit fields */
55 #define SPI_CFG2_COMM_SHIFT 17
56 #define SPI_CFG2_COMM GENMASK(18, 17)
57 #define SPI_CFG2_MASTER BIT(22)
58 #define SPI_CFG2_LSBFRST BIT(23)
59 #define SPI_CFG2_CPHA BIT(24)
60 #define SPI_CFG2_CPOL BIT(25)
61 #define SPI_CFG2_SSM BIT(26)
62 #define SPI_CFG2_AFCNTR BIT(31)
64 /* STM32_SPI_SR bit fields */
65 #define SPI_SR_RXP BIT(0)
66 #define SPI_SR_TXP BIT(1)
67 #define SPI_SR_EOT BIT(3)
68 #define SPI_SR_TXTF BIT(4)
69 #define SPI_SR_OVR BIT(6)
70 #define SPI_SR_SUSP BIT(11)
71 #define SPI_SR_RXPLVL_SHIFT 13
72 #define SPI_SR_RXPLVL GENMASK(14, 13)
73 #define SPI_SR_RXWNE BIT(15)
75 /* STM32_SPI_IFCR bit fields */
76 #define SPI_IFCR_ALL GENMASK(11, 3)
78 /* STM32_SPI_I2SCFGR bit fields */
79 #define SPI_I2SCFGR_I2SMOD BIT(0)
81 #define MAX_CS_COUNT 4
83 /* SPI Master Baud Rate min/max divisor */
84 #define STM32_MBR_DIV_MIN (2 << SPI_CFG1_MBR_MIN)
85 #define STM32_MBR_DIV_MAX (2 << SPI_CFG1_MBR_MAX)
87 #define STM32_SPI_TIMEOUT_US 100000
89 /* SPI Communication mode */
90 #define SPI_FULL_DUPLEX 0
91 #define SPI_SIMPLEX_TX 1
92 #define SPI_SIMPLEX_RX 2
93 #define SPI_HALF_DUPLEX 3
95 struct stm32_spi_priv {
98 struct reset_ctl rst_ctl;
99 struct gpio_desc cs_gpios[MAX_CS_COUNT];
101 unsigned int fifo_size;
102 unsigned int cur_bpw;
104 unsigned int cur_xferlen; /* current transfer length in bytes */
105 unsigned int tx_len; /* number of data to be written in bytes */
106 unsigned int rx_len; /* number of data to be read in bytes */
107 const void *tx_buf; /* data to be written, or NULL */
108 void *rx_buf; /* data to be read, or NULL */
113 static void stm32_spi_write_txfifo(struct stm32_spi_priv *priv)
115 while ((priv->tx_len > 0) &&
116 (readl(priv->base + STM32_SPI_SR) & SPI_SR_TXP)) {
117 u32 offs = priv->cur_xferlen - priv->tx_len;
119 if (priv->tx_len >= sizeof(u32) &&
120 IS_ALIGNED((uintptr_t)(priv->tx_buf + offs), sizeof(u32))) {
121 const u32 *tx_buf32 = (const u32 *)(priv->tx_buf + offs);
123 writel(*tx_buf32, priv->base + STM32_SPI_TXDR);
124 priv->tx_len -= sizeof(u32);
125 } else if (priv->tx_len >= sizeof(u16) &&
126 IS_ALIGNED((uintptr_t)(priv->tx_buf + offs), sizeof(u16))) {
127 const u16 *tx_buf16 = (const u16 *)(priv->tx_buf + offs);
129 writew(*tx_buf16, priv->base + STM32_SPI_TXDR);
130 priv->tx_len -= sizeof(u16);
132 const u8 *tx_buf8 = (const u8 *)(priv->tx_buf + offs);
134 writeb(*tx_buf8, priv->base + STM32_SPI_TXDR);
135 priv->tx_len -= sizeof(u8);
139 debug("%s: %d bytes left\n", __func__, priv->tx_len);
142 static void stm32_spi_read_rxfifo(struct stm32_spi_priv *priv)
144 u32 sr = readl(priv->base + STM32_SPI_SR);
145 u32 rxplvl = (sr & SPI_SR_RXPLVL) >> SPI_SR_RXPLVL_SHIFT;
147 while ((priv->rx_len > 0) &&
148 ((sr & SPI_SR_RXP) ||
149 ((sr & SPI_SR_EOT) && ((sr & SPI_SR_RXWNE) || (rxplvl > 0))))) {
150 u32 offs = priv->cur_xferlen - priv->rx_len;
152 if (IS_ALIGNED((uintptr_t)(priv->rx_buf + offs), sizeof(u32)) &&
153 (priv->rx_len >= sizeof(u32) || (sr & SPI_SR_RXWNE))) {
154 u32 *rx_buf32 = (u32 *)(priv->rx_buf + offs);
156 *rx_buf32 = readl(priv->base + STM32_SPI_RXDR);
157 priv->rx_len -= sizeof(u32);
158 } else if (IS_ALIGNED((uintptr_t)(priv->rx_buf + offs), sizeof(u16)) &&
159 (priv->rx_len >= sizeof(u16) ||
160 (!(sr & SPI_SR_RXWNE) &&
161 (rxplvl >= 2 || priv->cur_bpw > 8)))) {
162 u16 *rx_buf16 = (u16 *)(priv->rx_buf + offs);
164 *rx_buf16 = readw(priv->base + STM32_SPI_RXDR);
165 priv->rx_len -= sizeof(u16);
167 u8 *rx_buf8 = (u8 *)(priv->rx_buf + offs);
169 *rx_buf8 = readb(priv->base + STM32_SPI_RXDR);
170 priv->rx_len -= sizeof(u8);
173 sr = readl(priv->base + STM32_SPI_SR);
174 rxplvl = (sr & SPI_SR_RXPLVL) >> SPI_SR_RXPLVL_SHIFT;
177 debug("%s: %d bytes left\n", __func__, priv->rx_len);
180 static int stm32_spi_enable(struct stm32_spi_priv *priv)
182 debug("%s\n", __func__);
184 /* Enable the SPI hardware */
185 setbits_le32(priv->base + STM32_SPI_CR1, SPI_CR1_SPE);
190 static int stm32_spi_disable(struct stm32_spi_priv *priv)
192 debug("%s\n", __func__);
194 /* Disable the SPI hardware */
195 clrbits_le32(priv->base + STM32_SPI_CR1, SPI_CR1_SPE);
200 static int stm32_spi_claim_bus(struct udevice *slave)
202 struct udevice *bus = dev_get_parent(slave);
203 struct stm32_spi_priv *priv = dev_get_priv(bus);
205 debug("%s\n", __func__);
207 /* Enable the SPI hardware */
208 return stm32_spi_enable(priv);
211 static int stm32_spi_release_bus(struct udevice *slave)
213 struct udevice *bus = dev_get_parent(slave);
214 struct stm32_spi_priv *priv = dev_get_priv(bus);
216 debug("%s\n", __func__);
218 /* Disable the SPI hardware */
219 return stm32_spi_disable(priv);
222 static void stm32_spi_stopxfer(struct udevice *dev)
224 struct stm32_spi_priv *priv = dev_get_priv(dev);
228 debug("%s\n", __func__);
230 cr1 = readl(priv->base + STM32_SPI_CR1);
232 if (!(cr1 & SPI_CR1_SPE))
235 /* Wait on EOT or suspend the flow */
236 ret = readl_poll_timeout(priv->base + STM32_SPI_SR, sr,
237 !(sr & SPI_SR_EOT), 100000);
239 if (cr1 & SPI_CR1_CSTART) {
240 writel(cr1 | SPI_CR1_CSUSP, priv->base + STM32_SPI_CR1);
241 if (readl_poll_timeout(priv->base + STM32_SPI_SR,
242 sr, !(sr & SPI_SR_SUSP),
244 dev_err(dev, "Suspend request timeout\n");
248 /* clear status flags */
249 setbits_le32(priv->base + STM32_SPI_IFCR, SPI_IFCR_ALL);
252 static int stm32_spi_set_cs(struct udevice *dev, unsigned int cs, bool enable)
254 struct stm32_spi_priv *priv = dev_get_priv(dev);
256 debug("%s: cs=%d enable=%d\n", __func__, cs, enable);
258 if (cs >= MAX_CS_COUNT)
261 if (!dm_gpio_is_valid(&priv->cs_gpios[cs]))
267 return dm_gpio_set_value(&priv->cs_gpios[cs], enable ? 1 : 0);
270 static int stm32_spi_set_mode(struct udevice *bus, uint mode)
272 struct stm32_spi_priv *priv = dev_get_priv(bus);
273 u32 cfg2_clrb = 0, cfg2_setb = 0;
275 debug("%s: mode=%d\n", __func__, mode);
278 cfg2_setb |= SPI_CFG2_CPOL;
280 cfg2_clrb |= SPI_CFG2_CPOL;
283 cfg2_setb |= SPI_CFG2_CPHA;
285 cfg2_clrb |= SPI_CFG2_CPHA;
287 if (mode & SPI_LSB_FIRST)
288 cfg2_setb |= SPI_CFG2_LSBFRST;
290 cfg2_clrb |= SPI_CFG2_LSBFRST;
292 if (cfg2_clrb || cfg2_setb)
293 clrsetbits_le32(priv->base + STM32_SPI_CFG2,
294 cfg2_clrb, cfg2_setb);
296 if (mode & SPI_CS_HIGH)
297 priv->cs_high = true;
299 priv->cs_high = false;
303 static int stm32_spi_set_fthlv(struct udevice *dev, u32 xfer_len)
305 struct stm32_spi_priv *priv = dev_get_priv(dev);
306 u32 fthlv, half_fifo;
308 /* data packet should not exceed 1/2 of fifo space */
309 half_fifo = (priv->fifo_size / 2);
311 /* data_packet should not exceed transfer length */
312 fthlv = (half_fifo > xfer_len) ? xfer_len : half_fifo;
314 /* align packet size with data registers access */
315 fthlv -= (fthlv % 4);
319 clrsetbits_le32(priv->base + STM32_SPI_CFG1, SPI_CFG1_FTHLV,
320 (fthlv - 1) << SPI_CFG1_FTHLV_SHIFT);
325 static int stm32_spi_set_speed(struct udevice *bus, uint hz)
327 struct stm32_spi_priv *priv = dev_get_priv(bus);
331 debug("%s: hz=%d\n", __func__, hz);
333 if (priv->cur_hz == hz)
336 div = DIV_ROUND_UP(priv->bus_clk_rate, hz);
338 if (div < STM32_MBR_DIV_MIN ||
339 div > STM32_MBR_DIV_MAX)
342 /* Determine the first power of 2 greater than or equal to div */
346 mbrdiv = fls(div) - 1;
351 clrsetbits_le32(priv->base + STM32_SPI_CFG1, SPI_CFG1_MBR,
352 (mbrdiv - 1) << SPI_CFG1_MBR_SHIFT);
359 static int stm32_spi_xfer(struct udevice *slave, unsigned int bitlen,
360 const void *dout, void *din, unsigned long flags)
362 struct udevice *bus = dev_get_parent(slave);
363 struct dm_spi_slave_platdata *slave_plat;
364 struct stm32_spi_priv *priv = dev_get_priv(bus);
371 xferlen = bitlen / 8;
373 if (xferlen <= SPI_CR2_TSIZE)
374 writel(xferlen, priv->base + STM32_SPI_CR2);
380 priv->tx_len = priv->tx_buf ? bitlen / 8 : 0;
381 priv->rx_len = priv->rx_buf ? bitlen / 8 : 0;
383 mode = SPI_FULL_DUPLEX;
385 mode = SPI_SIMPLEX_RX;
386 else if (!priv->rx_buf)
387 mode = SPI_SIMPLEX_TX;
389 if (priv->cur_xferlen != xferlen || priv->cur_mode != mode) {
390 priv->cur_mode = mode;
391 priv->cur_xferlen = xferlen;
393 /* Disable the SPI hardware to unlock CFG1/CFG2 registers */
394 stm32_spi_disable(priv);
396 clrsetbits_le32(priv->base + STM32_SPI_CFG2, SPI_CFG2_COMM,
397 mode << SPI_CFG2_COMM_SHIFT);
399 stm32_spi_set_fthlv(bus, xferlen);
401 /* Enable the SPI hardware */
402 stm32_spi_enable(priv);
405 debug("%s: priv->tx_len=%d priv->rx_len=%d\n", __func__,
406 priv->tx_len, priv->rx_len);
408 slave_plat = dev_get_parent_platdata(slave);
409 if (flags & SPI_XFER_BEGIN)
410 stm32_spi_set_cs(bus, slave_plat->cs, false);
412 /* Be sure to have data in fifo before starting data transfer */
414 stm32_spi_write_txfifo(priv);
416 setbits_le32(priv->base + STM32_SPI_CR1, SPI_CR1_CSTART);
419 sr = readl(priv->base + STM32_SPI_SR);
421 if (sr & SPI_SR_OVR) {
422 dev_err(bus, "Overrun: RX data lost\n");
427 if (sr & SPI_SR_SUSP) {
428 dev_warn(bus, "System too slow is limiting data throughput\n");
430 if (priv->rx_buf && priv->rx_len > 0)
431 stm32_spi_read_rxfifo(priv);
436 if (sr & SPI_SR_TXTF)
440 if (priv->tx_buf && priv->tx_len > 0)
441 stm32_spi_write_txfifo(priv);
444 if (priv->rx_buf && priv->rx_len > 0)
445 stm32_spi_read_rxfifo(priv);
447 if (sr & SPI_SR_EOT) {
448 if (priv->rx_buf && priv->rx_len > 0)
449 stm32_spi_read_rxfifo(priv);
453 writel(ifcr, priv->base + STM32_SPI_IFCR);
456 /* clear status flags */
457 setbits_le32(priv->base + STM32_SPI_IFCR, SPI_IFCR_ALL);
458 stm32_spi_stopxfer(bus);
460 if (flags & SPI_XFER_END)
461 stm32_spi_set_cs(bus, slave_plat->cs, true);
466 static int stm32_spi_get_fifo_size(struct udevice *dev)
468 struct stm32_spi_priv *priv = dev_get_priv(dev);
471 stm32_spi_enable(priv);
473 while (readl(priv->base + STM32_SPI_SR) & SPI_SR_TXP)
474 writeb(++count, priv->base + STM32_SPI_TXDR);
476 stm32_spi_disable(priv);
478 debug("%s %d x 8-bit fifo size\n", __func__, count);
483 static int stm32_spi_probe(struct udevice *dev)
485 struct stm32_spi_priv *priv = dev_get_priv(dev);
486 unsigned long clk_rate;
490 priv->base = dev_remap_addr(dev);
495 ret = clk_get_by_index(dev, 0, &priv->clk);
499 ret = clk_enable(&priv->clk);
503 clk_rate = clk_get_rate(&priv->clk);
509 priv->bus_clk_rate = clk_rate;
512 ret = reset_get_by_index(dev, 0, &priv->rst_ctl);
516 reset_assert(&priv->rst_ctl);
518 reset_deassert(&priv->rst_ctl);
520 ret = gpio_request_list_by_name(dev, "cs-gpios", priv->cs_gpios,
521 ARRAY_SIZE(priv->cs_gpios), 0);
523 pr_err("Can't get %s cs gpios: %d", dev->name, ret);
527 priv->fifo_size = stm32_spi_get_fifo_size(dev);
529 priv->cur_mode = SPI_FULL_DUPLEX;
530 priv->cur_xferlen = 0;
531 priv->cur_bpw = SPI_DEFAULT_WORDLEN;
532 clrsetbits_le32(priv->base + STM32_SPI_CFG1, SPI_CFG1_DSIZE,
535 for (i = 0; i < ARRAY_SIZE(priv->cs_gpios); i++) {
536 if (!dm_gpio_is_valid(&priv->cs_gpios[i]))
539 dm_gpio_set_dir_flags(&priv->cs_gpios[i],
540 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
543 /* Ensure I2SMOD bit is kept cleared */
544 clrbits_le32(priv->base + STM32_SPI_I2SCFGR, SPI_I2SCFGR_I2SMOD);
547 * - SS input value high
548 * - transmitter half duplex direction
549 * - automatic communication suspend when RX-Fifo is full
551 setbits_le32(priv->base + STM32_SPI_CR1,
552 SPI_CR1_SSI | SPI_CR1_HDDIR | SPI_CR1_MASRX);
555 * - Set the master mode (default Motorola mode)
556 * - Consider 1 master/n slaves configuration and
557 * SS input value is determined by the SSI bit
558 * - keep control of all associated GPIOs
560 setbits_le32(priv->base + STM32_SPI_CFG2,
561 SPI_CFG2_MASTER | SPI_CFG2_SSM | SPI_CFG2_AFCNTR);
566 reset_free(&priv->rst_ctl);
569 clk_disable(&priv->clk);
570 clk_free(&priv->clk);
575 static int stm32_spi_remove(struct udevice *dev)
577 struct stm32_spi_priv *priv = dev_get_priv(dev);
580 stm32_spi_stopxfer(dev);
581 stm32_spi_disable(priv);
583 ret = reset_assert(&priv->rst_ctl);
587 reset_free(&priv->rst_ctl);
589 ret = clk_disable(&priv->clk);
593 clk_free(&priv->clk);
598 static const struct dm_spi_ops stm32_spi_ops = {
599 .claim_bus = stm32_spi_claim_bus,
600 .release_bus = stm32_spi_release_bus,
601 .set_mode = stm32_spi_set_mode,
602 .set_speed = stm32_spi_set_speed,
603 .xfer = stm32_spi_xfer,
606 static const struct udevice_id stm32_spi_ids[] = {
607 { .compatible = "st,stm32h7-spi", },
611 U_BOOT_DRIVER(stm32_spi) = {
614 .of_match = stm32_spi_ids,
615 .ops = &stm32_spi_ops,
616 .priv_auto_alloc_size = sizeof(struct stm32_spi_priv),
617 .probe = stm32_spi_probe,
618 .remove = stm32_spi_remove,