blob: 97b83b171670b34804a417e02bca5799a15856c5 [file] [log] [blame]
Patrice Chotard0b08bf82019-04-30 18:08:28 +02001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
4 *
5 * Driver for STMicroelectronics Serial peripheral interface (SPI)
6 */
Patrick Delaunay7380f9e2020-11-06 19:01:52 +01007
8#define LOG_CATEGORY UCLASS_SPI
9
Patrice Chotard0b08bf82019-04-30 18:08:28 +020010#include <clk.h>
11#include <dm.h>
12#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <malloc.h>
Patrice Chotard0b08bf82019-04-30 18:08:28 +020015#include <reset.h>
16#include <spi.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060018#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060020#include <linux/printk.h>
Patrice Chotard0b08bf82019-04-30 18:08:28 +020021
22#include <asm/io.h>
23#include <asm/gpio.h>
24#include <linux/bitfield.h>
25#include <linux/iopoll.h>
26
27/* STM32 SPI registers */
28#define STM32_SPI_CR1 0x00
29#define STM32_SPI_CR2 0x04
30#define STM32_SPI_CFG1 0x08
31#define STM32_SPI_CFG2 0x0C
32#define STM32_SPI_SR 0x14
33#define STM32_SPI_IFCR 0x18
34#define STM32_SPI_TXDR 0x20
35#define STM32_SPI_RXDR 0x30
36#define STM32_SPI_I2SCFGR 0x50
37
38/* STM32_SPI_CR1 bit fields */
39#define SPI_CR1_SPE BIT(0)
40#define SPI_CR1_MASRX BIT(8)
41#define SPI_CR1_CSTART BIT(9)
42#define SPI_CR1_CSUSP BIT(10)
43#define SPI_CR1_HDDIR BIT(11)
44#define SPI_CR1_SSI BIT(12)
45
46/* STM32_SPI_CR2 bit fields */
47#define SPI_CR2_TSIZE GENMASK(15, 0)
48
49/* STM32_SPI_CFG1 bit fields */
50#define SPI_CFG1_DSIZE GENMASK(4, 0)
51#define SPI_CFG1_DSIZE_MIN 3
52#define SPI_CFG1_FTHLV_SHIFT 5
53#define SPI_CFG1_FTHLV GENMASK(8, 5)
54#define SPI_CFG1_MBR_SHIFT 28
55#define SPI_CFG1_MBR GENMASK(30, 28)
56#define SPI_CFG1_MBR_MIN 0
57#define SPI_CFG1_MBR_MAX FIELD_GET(SPI_CFG1_MBR, SPI_CFG1_MBR)
58
59/* STM32_SPI_CFG2 bit fields */
60#define SPI_CFG2_COMM_SHIFT 17
61#define SPI_CFG2_COMM GENMASK(18, 17)
62#define SPI_CFG2_MASTER BIT(22)
63#define SPI_CFG2_LSBFRST BIT(23)
64#define SPI_CFG2_CPHA BIT(24)
65#define SPI_CFG2_CPOL BIT(25)
66#define SPI_CFG2_SSM BIT(26)
67#define SPI_CFG2_AFCNTR BIT(31)
68
69/* STM32_SPI_SR bit fields */
70#define SPI_SR_RXP BIT(0)
71#define SPI_SR_TXP BIT(1)
72#define SPI_SR_EOT BIT(3)
73#define SPI_SR_TXTF BIT(4)
74#define SPI_SR_OVR BIT(6)
75#define SPI_SR_SUSP BIT(11)
76#define SPI_SR_RXPLVL_SHIFT 13
77#define SPI_SR_RXPLVL GENMASK(14, 13)
78#define SPI_SR_RXWNE BIT(15)
79
80/* STM32_SPI_IFCR bit fields */
81#define SPI_IFCR_ALL GENMASK(11, 3)
82
83/* STM32_SPI_I2SCFGR bit fields */
84#define SPI_I2SCFGR_I2SMOD BIT(0)
85
86#define MAX_CS_COUNT 4
87
88/* SPI Master Baud Rate min/max divisor */
89#define STM32_MBR_DIV_MIN (2 << SPI_CFG1_MBR_MIN)
90#define STM32_MBR_DIV_MAX (2 << SPI_CFG1_MBR_MAX)
91
92#define STM32_SPI_TIMEOUT_US 100000
93
94/* SPI Communication mode */
95#define SPI_FULL_DUPLEX 0
96#define SPI_SIMPLEX_TX 1
97#define SPI_SIMPLEX_RX 2
98#define SPI_HALF_DUPLEX 3
99
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200100struct stm32_spi_plat {
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200101 void __iomem *base;
102 struct clk clk;
103 struct reset_ctl rst_ctl;
104 struct gpio_desc cs_gpios[MAX_CS_COUNT];
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200105};
106
107struct stm32_spi_priv {
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200108 ulong bus_clk_rate;
109 unsigned int fifo_size;
110 unsigned int cur_bpw;
111 unsigned int cur_hz;
112 unsigned int cur_xferlen; /* current transfer length in bytes */
Patrick Delaunay5043c4b2019-06-21 15:26:58 +0200113 unsigned int tx_len; /* number of data to be written in bytes */
114 unsigned int rx_len; /* number of data to be read in bytes */
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200115 const void *tx_buf; /* data to be written, or NULL */
116 void *rx_buf; /* data to be read, or NULL */
117 u32 cur_mode;
118 bool cs_high;
119};
120
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200121static void stm32_spi_write_txfifo(struct udevice *bus)
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200122{
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200123 struct stm32_spi_priv *priv = dev_get_priv(bus);
124 struct stm32_spi_plat *plat = dev_get_plat(bus);
125 void __iomem *base = plat->base;
126
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200127 while ((priv->tx_len > 0) &&
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200128 (readl(base + STM32_SPI_SR) & SPI_SR_TXP)) {
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200129 u32 offs = priv->cur_xferlen - priv->tx_len;
130
131 if (priv->tx_len >= sizeof(u32) &&
132 IS_ALIGNED((uintptr_t)(priv->tx_buf + offs), sizeof(u32))) {
133 const u32 *tx_buf32 = (const u32 *)(priv->tx_buf + offs);
134
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200135 writel(*tx_buf32, base + STM32_SPI_TXDR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200136 priv->tx_len -= sizeof(u32);
137 } else if (priv->tx_len >= sizeof(u16) &&
138 IS_ALIGNED((uintptr_t)(priv->tx_buf + offs), sizeof(u16))) {
139 const u16 *tx_buf16 = (const u16 *)(priv->tx_buf + offs);
140
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200141 writew(*tx_buf16, base + STM32_SPI_TXDR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200142 priv->tx_len -= sizeof(u16);
143 } else {
144 const u8 *tx_buf8 = (const u8 *)(priv->tx_buf + offs);
145
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200146 writeb(*tx_buf8, base + STM32_SPI_TXDR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200147 priv->tx_len -= sizeof(u8);
148 }
149 }
150
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100151 log_debug("%d bytes left\n", priv->tx_len);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200152}
153
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200154static void stm32_spi_read_rxfifo(struct udevice *bus)
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200155{
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200156 struct stm32_spi_priv *priv = dev_get_priv(bus);
157 struct stm32_spi_plat *plat = dev_get_plat(bus);
158 void __iomem *base = plat->base;
159 u32 sr = readl(base + STM32_SPI_SR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200160 u32 rxplvl = (sr & SPI_SR_RXPLVL) >> SPI_SR_RXPLVL_SHIFT;
161
162 while ((priv->rx_len > 0) &&
163 ((sr & SPI_SR_RXP) ||
164 ((sr & SPI_SR_EOT) && ((sr & SPI_SR_RXWNE) || (rxplvl > 0))))) {
165 u32 offs = priv->cur_xferlen - priv->rx_len;
166
167 if (IS_ALIGNED((uintptr_t)(priv->rx_buf + offs), sizeof(u32)) &&
168 (priv->rx_len >= sizeof(u32) || (sr & SPI_SR_RXWNE))) {
169 u32 *rx_buf32 = (u32 *)(priv->rx_buf + offs);
170
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200171 *rx_buf32 = readl(base + STM32_SPI_RXDR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200172 priv->rx_len -= sizeof(u32);
173 } else if (IS_ALIGNED((uintptr_t)(priv->rx_buf + offs), sizeof(u16)) &&
174 (priv->rx_len >= sizeof(u16) ||
175 (!(sr & SPI_SR_RXWNE) &&
176 (rxplvl >= 2 || priv->cur_bpw > 8)))) {
177 u16 *rx_buf16 = (u16 *)(priv->rx_buf + offs);
178
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200179 *rx_buf16 = readw(base + STM32_SPI_RXDR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200180 priv->rx_len -= sizeof(u16);
181 } else {
182 u8 *rx_buf8 = (u8 *)(priv->rx_buf + offs);
183
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200184 *rx_buf8 = readb(base + STM32_SPI_RXDR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200185 priv->rx_len -= sizeof(u8);
186 }
187
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200188 sr = readl(base + STM32_SPI_SR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200189 rxplvl = (sr & SPI_SR_RXPLVL) >> SPI_SR_RXPLVL_SHIFT;
190 }
191
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100192 log_debug("%d bytes left\n", priv->rx_len);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200193}
194
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200195static int stm32_spi_enable(void __iomem *base)
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200196{
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100197 log_debug("\n");
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200198
199 /* Enable the SPI hardware */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200200 setbits_le32(base + STM32_SPI_CR1, SPI_CR1_SPE);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200201
202 return 0;
203}
204
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200205static int stm32_spi_disable(void __iomem *base)
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200206{
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100207 log_debug("\n");
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200208
209 /* Disable the SPI hardware */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200210 clrbits_le32(base + STM32_SPI_CR1, SPI_CR1_SPE);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200211
212 return 0;
213}
214
215static int stm32_spi_claim_bus(struct udevice *slave)
216{
217 struct udevice *bus = dev_get_parent(slave);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200218 struct stm32_spi_plat *plat = dev_get_plat(bus);
219 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200220
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100221 dev_dbg(slave, "\n");
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200222
223 /* Enable the SPI hardware */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200224 return stm32_spi_enable(base);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200225}
226
227static int stm32_spi_release_bus(struct udevice *slave)
228{
229 struct udevice *bus = dev_get_parent(slave);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200230 struct stm32_spi_plat *plat = dev_get_plat(bus);
231 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200232
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100233 dev_dbg(slave, "\n");
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200234
235 /* Disable the SPI hardware */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200236 return stm32_spi_disable(base);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200237}
238
239static void stm32_spi_stopxfer(struct udevice *dev)
240{
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200241 struct stm32_spi_plat *plat = dev_get_plat(dev);
242 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200243 u32 cr1, sr;
244 int ret;
245
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100246 dev_dbg(dev, "\n");
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200247
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200248 cr1 = readl(base + STM32_SPI_CR1);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200249
250 if (!(cr1 & SPI_CR1_SPE))
251 return;
252
253 /* Wait on EOT or suspend the flow */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200254 ret = readl_poll_timeout(base + STM32_SPI_SR, sr,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200255 !(sr & SPI_SR_EOT), 100000);
256 if (ret < 0) {
257 if (cr1 & SPI_CR1_CSTART) {
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200258 writel(cr1 | SPI_CR1_CSUSP, base + STM32_SPI_CR1);
259 if (readl_poll_timeout(base + STM32_SPI_SR,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200260 sr, !(sr & SPI_SR_SUSP),
261 100000) < 0)
262 dev_err(dev, "Suspend request timeout\n");
263 }
264 }
265
266 /* clear status flags */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200267 setbits_le32(base + STM32_SPI_IFCR, SPI_IFCR_ALL);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200268}
269
270static int stm32_spi_set_cs(struct udevice *dev, unsigned int cs, bool enable)
271{
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200272 struct stm32_spi_plat *plat = dev_get_plat(dev);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200273 struct stm32_spi_priv *priv = dev_get_priv(dev);
274
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100275 dev_dbg(dev, "cs=%d enable=%d\n", cs, enable);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200276
277 if (cs >= MAX_CS_COUNT)
278 return -ENODEV;
279
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200280 if (!dm_gpio_is_valid(&plat->cs_gpios[cs]))
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200281 return -EINVAL;
282
283 if (priv->cs_high)
284 enable = !enable;
285
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200286 return dm_gpio_set_value(&plat->cs_gpios[cs], enable ? 1 : 0);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200287}
288
289static int stm32_spi_set_mode(struct udevice *bus, uint mode)
290{
291 struct stm32_spi_priv *priv = dev_get_priv(bus);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200292 struct stm32_spi_plat *plat = dev_get_plat(bus);
293 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200294 u32 cfg2_clrb = 0, cfg2_setb = 0;
295
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100296 dev_dbg(bus, "mode=%d\n", mode);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200297
298 if (mode & SPI_CPOL)
299 cfg2_setb |= SPI_CFG2_CPOL;
300 else
301 cfg2_clrb |= SPI_CFG2_CPOL;
302
303 if (mode & SPI_CPHA)
304 cfg2_setb |= SPI_CFG2_CPHA;
305 else
306 cfg2_clrb |= SPI_CFG2_CPHA;
307
308 if (mode & SPI_LSB_FIRST)
309 cfg2_setb |= SPI_CFG2_LSBFRST;
310 else
311 cfg2_clrb |= SPI_CFG2_LSBFRST;
312
313 if (cfg2_clrb || cfg2_setb)
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200314 clrsetbits_le32(base + STM32_SPI_CFG2,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200315 cfg2_clrb, cfg2_setb);
316
317 if (mode & SPI_CS_HIGH)
318 priv->cs_high = true;
319 else
320 priv->cs_high = false;
321 return 0;
322}
323
324static int stm32_spi_set_fthlv(struct udevice *dev, u32 xfer_len)
325{
326 struct stm32_spi_priv *priv = dev_get_priv(dev);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200327 struct stm32_spi_plat *plat = dev_get_plat(dev);
328 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200329 u32 fthlv, half_fifo;
330
331 /* data packet should not exceed 1/2 of fifo space */
332 half_fifo = (priv->fifo_size / 2);
333
334 /* data_packet should not exceed transfer length */
335 fthlv = (half_fifo > xfer_len) ? xfer_len : half_fifo;
336
337 /* align packet size with data registers access */
338 fthlv -= (fthlv % 4);
339
340 if (!fthlv)
341 fthlv = 1;
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200342 clrsetbits_le32(base + STM32_SPI_CFG1, SPI_CFG1_FTHLV,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200343 (fthlv - 1) << SPI_CFG1_FTHLV_SHIFT);
344
345 return 0;
346}
347
348static int stm32_spi_set_speed(struct udevice *bus, uint hz)
349{
350 struct stm32_spi_priv *priv = dev_get_priv(bus);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200351 struct stm32_spi_plat *plat = dev_get_plat(bus);
352 void __iomem *base = plat->base;
Patrick Delaunay5043c4b2019-06-21 15:26:58 +0200353 u32 mbrdiv;
354 long div;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200355
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100356 dev_dbg(bus, "hz=%d\n", hz);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200357
358 if (priv->cur_hz == hz)
359 return 0;
360
361 div = DIV_ROUND_UP(priv->bus_clk_rate, hz);
362
363 if (div < STM32_MBR_DIV_MIN ||
364 div > STM32_MBR_DIV_MAX)
365 return -EINVAL;
366
367 /* Determine the first power of 2 greater than or equal to div */
368 if (div & (div - 1))
369 mbrdiv = fls(div);
370 else
371 mbrdiv = fls(div) - 1;
372
Patrick Delaunay5043c4b2019-06-21 15:26:58 +0200373 if (!mbrdiv)
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200374 return -EINVAL;
375
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200376 clrsetbits_le32(base + STM32_SPI_CFG1, SPI_CFG1_MBR,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200377 (mbrdiv - 1) << SPI_CFG1_MBR_SHIFT);
378
379 priv->cur_hz = hz;
380
381 return 0;
382}
383
384static int stm32_spi_xfer(struct udevice *slave, unsigned int bitlen,
385 const void *dout, void *din, unsigned long flags)
386{
387 struct udevice *bus = dev_get_parent(slave);
Simon Glassb75b15b2020-12-03 16:55:23 -0700388 struct dm_spi_slave_plat *slave_plat;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200389 struct stm32_spi_priv *priv = dev_get_priv(bus);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200390 struct stm32_spi_plat *plat = dev_get_plat(bus);
391 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200392 u32 sr;
393 u32 ifcr = 0;
394 u32 xferlen;
395 u32 mode;
396 int xfer_status = 0;
397
398 xferlen = bitlen / 8;
399
400 if (xferlen <= SPI_CR2_TSIZE)
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200401 writel(xferlen, base + STM32_SPI_CR2);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200402 else
403 return -EMSGSIZE;
404
405 priv->tx_buf = dout;
406 priv->rx_buf = din;
407 priv->tx_len = priv->tx_buf ? bitlen / 8 : 0;
408 priv->rx_len = priv->rx_buf ? bitlen / 8 : 0;
409
410 mode = SPI_FULL_DUPLEX;
411 if (!priv->tx_buf)
412 mode = SPI_SIMPLEX_RX;
413 else if (!priv->rx_buf)
414 mode = SPI_SIMPLEX_TX;
415
416 if (priv->cur_xferlen != xferlen || priv->cur_mode != mode) {
417 priv->cur_mode = mode;
418 priv->cur_xferlen = xferlen;
419
420 /* Disable the SPI hardware to unlock CFG1/CFG2 registers */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200421 stm32_spi_disable(base);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200422
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200423 clrsetbits_le32(base + STM32_SPI_CFG2, SPI_CFG2_COMM,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200424 mode << SPI_CFG2_COMM_SHIFT);
425
426 stm32_spi_set_fthlv(bus, xferlen);
427
428 /* Enable the SPI hardware */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200429 stm32_spi_enable(base);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200430 }
431
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100432 dev_dbg(bus, "priv->tx_len=%d priv->rx_len=%d\n",
433 priv->tx_len, priv->rx_len);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200434
Simon Glass71fa5b42020-12-03 16:55:18 -0700435 slave_plat = dev_get_parent_plat(slave);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200436 if (flags & SPI_XFER_BEGIN)
437 stm32_spi_set_cs(bus, slave_plat->cs, false);
438
439 /* Be sure to have data in fifo before starting data transfer */
440 if (priv->tx_buf)
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200441 stm32_spi_write_txfifo(bus);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200442
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200443 setbits_le32(base + STM32_SPI_CR1, SPI_CR1_CSTART);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200444
445 while (1) {
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200446 sr = readl(base + STM32_SPI_SR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200447
448 if (sr & SPI_SR_OVR) {
449 dev_err(bus, "Overrun: RX data lost\n");
450 xfer_status = -EIO;
451 break;
452 }
453
454 if (sr & SPI_SR_SUSP) {
455 dev_warn(bus, "System too slow is limiting data throughput\n");
456
457 if (priv->rx_buf && priv->rx_len > 0)
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200458 stm32_spi_read_rxfifo(bus);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200459
460 ifcr |= SPI_SR_SUSP;
461 }
462
463 if (sr & SPI_SR_TXTF)
464 ifcr |= SPI_SR_TXTF;
465
466 if (sr & SPI_SR_TXP)
467 if (priv->tx_buf && priv->tx_len > 0)
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200468 stm32_spi_write_txfifo(bus);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200469
470 if (sr & SPI_SR_RXP)
471 if (priv->rx_buf && priv->rx_len > 0)
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200472 stm32_spi_read_rxfifo(bus);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200473
474 if (sr & SPI_SR_EOT) {
475 if (priv->rx_buf && priv->rx_len > 0)
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200476 stm32_spi_read_rxfifo(bus);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200477 break;
478 }
479
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200480 writel(ifcr, base + STM32_SPI_IFCR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200481 }
482
483 /* clear status flags */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200484 setbits_le32(base + STM32_SPI_IFCR, SPI_IFCR_ALL);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200485 stm32_spi_stopxfer(bus);
486
487 if (flags & SPI_XFER_END)
488 stm32_spi_set_cs(bus, slave_plat->cs, true);
489
490 return xfer_status;
491}
492
493static int stm32_spi_get_fifo_size(struct udevice *dev)
494{
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200495 struct stm32_spi_plat *plat = dev_get_plat(dev);
496 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200497 u32 count = 0;
498
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200499 stm32_spi_enable(base);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200500
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200501 while (readl(base + STM32_SPI_SR) & SPI_SR_TXP)
502 writeb(++count, base + STM32_SPI_TXDR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200503
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200504 stm32_spi_disable(base);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200505
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100506 dev_dbg(dev, "%d x 8-bit fifo size\n", count);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200507
508 return count;
509}
510
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200511static int stm32_spi_of_to_plat(struct udevice *dev)
512{
513 struct stm32_spi_plat *plat = dev_get_plat(dev);
514 int ret;
515
516 plat->base = dev_read_addr_ptr(dev);
517 if (!plat->base) {
518 dev_err(dev, "can't get registers base address\n");
519 return -ENOENT;
520 }
521
522 ret = clk_get_by_index(dev, 0, &plat->clk);
523 if (ret < 0)
524 return ret;
525
526 ret = reset_get_by_index(dev, 0, &plat->rst_ctl);
527 if (ret < 0)
Sean Andersond318eb32023-12-16 14:38:42 -0500528 return ret;
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200529
530 ret = gpio_request_list_by_name(dev, "cs-gpios", plat->cs_gpios,
531 ARRAY_SIZE(plat->cs_gpios), 0);
532 if (ret < 0) {
533 dev_err(dev, "Can't get %s cs gpios: %d", dev->name, ret);
Sean Andersond318eb32023-12-16 14:38:42 -0500534 return -ENOENT;
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200535 }
536
537 return 0;
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200538}
539
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200540static int stm32_spi_probe(struct udevice *dev)
541{
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200542 struct stm32_spi_plat *plat = dev_get_plat(dev);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200543 struct stm32_spi_priv *priv = dev_get_priv(dev);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200544 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200545 unsigned long clk_rate;
546 int ret;
Patrick Delaunay5043c4b2019-06-21 15:26:58 +0200547 unsigned int i;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200548
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200549 /* enable clock */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200550 ret = clk_enable(&plat->clk);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200551 if (ret < 0)
552 return ret;
553
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200554 clk_rate = clk_get_rate(&plat->clk);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200555 if (!clk_rate) {
556 ret = -EINVAL;
557 goto clk_err;
558 }
559
560 priv->bus_clk_rate = clk_rate;
561
562 /* perform reset */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200563 reset_assert(&plat->rst_ctl);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200564 udelay(2);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200565 reset_deassert(&plat->rst_ctl);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200566
567 priv->fifo_size = stm32_spi_get_fifo_size(dev);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200568 priv->cur_mode = SPI_FULL_DUPLEX;
569 priv->cur_xferlen = 0;
570 priv->cur_bpw = SPI_DEFAULT_WORDLEN;
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200571 clrsetbits_le32(base + STM32_SPI_CFG1, SPI_CFG1_DSIZE,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200572 priv->cur_bpw - 1);
573
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200574 for (i = 0; i < ARRAY_SIZE(plat->cs_gpios); i++) {
575 if (!dm_gpio_is_valid(&plat->cs_gpios[i]))
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200576 continue;
577
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200578 dm_gpio_set_dir_flags(&plat->cs_gpios[i],
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200579 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
580 }
581
582 /* Ensure I2SMOD bit is kept cleared */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200583 clrbits_le32(base + STM32_SPI_I2SCFGR, SPI_I2SCFGR_I2SMOD);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200584
585 /*
586 * - SS input value high
587 * - transmitter half duplex direction
588 * - automatic communication suspend when RX-Fifo is full
589 */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200590 setbits_le32(base + STM32_SPI_CR1,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200591 SPI_CR1_SSI | SPI_CR1_HDDIR | SPI_CR1_MASRX);
592
593 /*
594 * - Set the master mode (default Motorola mode)
595 * - Consider 1 master/n slaves configuration and
596 * SS input value is determined by the SSI bit
597 * - keep control of all associated GPIOs
598 */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200599 setbits_le32(base + STM32_SPI_CFG2,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200600 SPI_CFG2_MASTER | SPI_CFG2_SSM | SPI_CFG2_AFCNTR);
601
602 return 0;
603
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200604clk_err:
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200605 clk_disable(&plat->clk);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200606
607 return ret;
608};
609
610static int stm32_spi_remove(struct udevice *dev)
611{
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200612 struct stm32_spi_plat *plat = dev_get_plat(dev);
613 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200614 int ret;
615
616 stm32_spi_stopxfer(dev);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200617 stm32_spi_disable(base);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200618
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200619 ret = reset_assert(&plat->rst_ctl);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200620 if (ret < 0)
621 return ret;
622
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200623 reset_free(&plat->rst_ctl);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200624
Sean Andersond318eb32023-12-16 14:38:42 -0500625 return clk_disable(&plat->clk);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200626};
627
628static const struct dm_spi_ops stm32_spi_ops = {
629 .claim_bus = stm32_spi_claim_bus,
630 .release_bus = stm32_spi_release_bus,
631 .set_mode = stm32_spi_set_mode,
632 .set_speed = stm32_spi_set_speed,
633 .xfer = stm32_spi_xfer,
634};
635
636static const struct udevice_id stm32_spi_ids[] = {
637 { .compatible = "st,stm32h7-spi", },
638 { }
639};
640
641U_BOOT_DRIVER(stm32_spi) = {
642 .name = "stm32_spi",
643 .id = UCLASS_SPI,
644 .of_match = stm32_spi_ids,
645 .ops = &stm32_spi_ops,
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200646 .of_to_plat = stm32_spi_of_to_plat,
647 .plat_auto = sizeof(struct stm32_spi_plat),
648 .priv_auto = sizeof(struct stm32_spi_priv),
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200649 .probe = stm32_spi_probe,
650 .remove = stm32_spi_remove,
651};