blob: 82f6ed783f9e8e71033dcf71e39de1dc1dfbb309 [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 <common.h>
11#include <clk.h>
12#include <dm.h>
13#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <malloc.h>
Patrice Chotard0b08bf82019-04-30 18:08:28 +020016#include <reset.h>
17#include <spi.h>
Simon Glass9bc15642020-02-03 07:36:16 -070018#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060019#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060020#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060021#include <linux/printk.h>
Patrice Chotard0b08bf82019-04-30 18:08:28 +020022
23#include <asm/io.h>
24#include <asm/gpio.h>
25#include <linux/bitfield.h>
26#include <linux/iopoll.h>
27
28/* STM32 SPI registers */
29#define STM32_SPI_CR1 0x00
30#define STM32_SPI_CR2 0x04
31#define STM32_SPI_CFG1 0x08
32#define STM32_SPI_CFG2 0x0C
33#define STM32_SPI_SR 0x14
34#define STM32_SPI_IFCR 0x18
35#define STM32_SPI_TXDR 0x20
36#define STM32_SPI_RXDR 0x30
37#define STM32_SPI_I2SCFGR 0x50
38
39/* STM32_SPI_CR1 bit fields */
40#define SPI_CR1_SPE BIT(0)
41#define SPI_CR1_MASRX BIT(8)
42#define SPI_CR1_CSTART BIT(9)
43#define SPI_CR1_CSUSP BIT(10)
44#define SPI_CR1_HDDIR BIT(11)
45#define SPI_CR1_SSI BIT(12)
46
47/* STM32_SPI_CR2 bit fields */
48#define SPI_CR2_TSIZE GENMASK(15, 0)
49
50/* STM32_SPI_CFG1 bit fields */
51#define SPI_CFG1_DSIZE GENMASK(4, 0)
52#define SPI_CFG1_DSIZE_MIN 3
53#define SPI_CFG1_FTHLV_SHIFT 5
54#define SPI_CFG1_FTHLV GENMASK(8, 5)
55#define SPI_CFG1_MBR_SHIFT 28
56#define SPI_CFG1_MBR GENMASK(30, 28)
57#define SPI_CFG1_MBR_MIN 0
58#define SPI_CFG1_MBR_MAX FIELD_GET(SPI_CFG1_MBR, SPI_CFG1_MBR)
59
60/* STM32_SPI_CFG2 bit fields */
61#define SPI_CFG2_COMM_SHIFT 17
62#define SPI_CFG2_COMM GENMASK(18, 17)
63#define SPI_CFG2_MASTER BIT(22)
64#define SPI_CFG2_LSBFRST BIT(23)
65#define SPI_CFG2_CPHA BIT(24)
66#define SPI_CFG2_CPOL BIT(25)
67#define SPI_CFG2_SSM BIT(26)
68#define SPI_CFG2_AFCNTR BIT(31)
69
70/* STM32_SPI_SR bit fields */
71#define SPI_SR_RXP BIT(0)
72#define SPI_SR_TXP BIT(1)
73#define SPI_SR_EOT BIT(3)
74#define SPI_SR_TXTF BIT(4)
75#define SPI_SR_OVR BIT(6)
76#define SPI_SR_SUSP BIT(11)
77#define SPI_SR_RXPLVL_SHIFT 13
78#define SPI_SR_RXPLVL GENMASK(14, 13)
79#define SPI_SR_RXWNE BIT(15)
80
81/* STM32_SPI_IFCR bit fields */
82#define SPI_IFCR_ALL GENMASK(11, 3)
83
84/* STM32_SPI_I2SCFGR bit fields */
85#define SPI_I2SCFGR_I2SMOD BIT(0)
86
87#define MAX_CS_COUNT 4
88
89/* SPI Master Baud Rate min/max divisor */
90#define STM32_MBR_DIV_MIN (2 << SPI_CFG1_MBR_MIN)
91#define STM32_MBR_DIV_MAX (2 << SPI_CFG1_MBR_MAX)
92
93#define STM32_SPI_TIMEOUT_US 100000
94
95/* SPI Communication mode */
96#define SPI_FULL_DUPLEX 0
97#define SPI_SIMPLEX_TX 1
98#define SPI_SIMPLEX_RX 2
99#define SPI_HALF_DUPLEX 3
100
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200101struct stm32_spi_plat {
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200102 void __iomem *base;
103 struct clk clk;
104 struct reset_ctl rst_ctl;
105 struct gpio_desc cs_gpios[MAX_CS_COUNT];
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200106};
107
108struct stm32_spi_priv {
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200109 ulong bus_clk_rate;
110 unsigned int fifo_size;
111 unsigned int cur_bpw;
112 unsigned int cur_hz;
113 unsigned int cur_xferlen; /* current transfer length in bytes */
Patrick Delaunay5043c4b2019-06-21 15:26:58 +0200114 unsigned int tx_len; /* number of data to be written in bytes */
115 unsigned int rx_len; /* number of data to be read in bytes */
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200116 const void *tx_buf; /* data to be written, or NULL */
117 void *rx_buf; /* data to be read, or NULL */
118 u32 cur_mode;
119 bool cs_high;
120};
121
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200122static void stm32_spi_write_txfifo(struct udevice *bus)
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200123{
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200124 struct stm32_spi_priv *priv = dev_get_priv(bus);
125 struct stm32_spi_plat *plat = dev_get_plat(bus);
126 void __iomem *base = plat->base;
127
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200128 while ((priv->tx_len > 0) &&
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200129 (readl(base + STM32_SPI_SR) & SPI_SR_TXP)) {
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200130 u32 offs = priv->cur_xferlen - priv->tx_len;
131
132 if (priv->tx_len >= sizeof(u32) &&
133 IS_ALIGNED((uintptr_t)(priv->tx_buf + offs), sizeof(u32))) {
134 const u32 *tx_buf32 = (const u32 *)(priv->tx_buf + offs);
135
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200136 writel(*tx_buf32, base + STM32_SPI_TXDR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200137 priv->tx_len -= sizeof(u32);
138 } else if (priv->tx_len >= sizeof(u16) &&
139 IS_ALIGNED((uintptr_t)(priv->tx_buf + offs), sizeof(u16))) {
140 const u16 *tx_buf16 = (const u16 *)(priv->tx_buf + offs);
141
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200142 writew(*tx_buf16, base + STM32_SPI_TXDR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200143 priv->tx_len -= sizeof(u16);
144 } else {
145 const u8 *tx_buf8 = (const u8 *)(priv->tx_buf + offs);
146
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200147 writeb(*tx_buf8, base + STM32_SPI_TXDR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200148 priv->tx_len -= sizeof(u8);
149 }
150 }
151
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100152 log_debug("%d bytes left\n", priv->tx_len);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200153}
154
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200155static void stm32_spi_read_rxfifo(struct udevice *bus)
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200156{
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200157 struct stm32_spi_priv *priv = dev_get_priv(bus);
158 struct stm32_spi_plat *plat = dev_get_plat(bus);
159 void __iomem *base = plat->base;
160 u32 sr = readl(base + STM32_SPI_SR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200161 u32 rxplvl = (sr & SPI_SR_RXPLVL) >> SPI_SR_RXPLVL_SHIFT;
162
163 while ((priv->rx_len > 0) &&
164 ((sr & SPI_SR_RXP) ||
165 ((sr & SPI_SR_EOT) && ((sr & SPI_SR_RXWNE) || (rxplvl > 0))))) {
166 u32 offs = priv->cur_xferlen - priv->rx_len;
167
168 if (IS_ALIGNED((uintptr_t)(priv->rx_buf + offs), sizeof(u32)) &&
169 (priv->rx_len >= sizeof(u32) || (sr & SPI_SR_RXWNE))) {
170 u32 *rx_buf32 = (u32 *)(priv->rx_buf + offs);
171
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200172 *rx_buf32 = readl(base + STM32_SPI_RXDR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200173 priv->rx_len -= sizeof(u32);
174 } else if (IS_ALIGNED((uintptr_t)(priv->rx_buf + offs), sizeof(u16)) &&
175 (priv->rx_len >= sizeof(u16) ||
176 (!(sr & SPI_SR_RXWNE) &&
177 (rxplvl >= 2 || priv->cur_bpw > 8)))) {
178 u16 *rx_buf16 = (u16 *)(priv->rx_buf + offs);
179
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200180 *rx_buf16 = readw(base + STM32_SPI_RXDR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200181 priv->rx_len -= sizeof(u16);
182 } else {
183 u8 *rx_buf8 = (u8 *)(priv->rx_buf + offs);
184
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200185 *rx_buf8 = readb(base + STM32_SPI_RXDR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200186 priv->rx_len -= sizeof(u8);
187 }
188
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200189 sr = readl(base + STM32_SPI_SR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200190 rxplvl = (sr & SPI_SR_RXPLVL) >> SPI_SR_RXPLVL_SHIFT;
191 }
192
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100193 log_debug("%d bytes left\n", priv->rx_len);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200194}
195
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200196static int stm32_spi_enable(void __iomem *base)
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200197{
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100198 log_debug("\n");
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200199
200 /* Enable the SPI hardware */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200201 setbits_le32(base + STM32_SPI_CR1, SPI_CR1_SPE);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200202
203 return 0;
204}
205
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200206static int stm32_spi_disable(void __iomem *base)
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200207{
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100208 log_debug("\n");
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200209
210 /* Disable the SPI hardware */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200211 clrbits_le32(base + STM32_SPI_CR1, SPI_CR1_SPE);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200212
213 return 0;
214}
215
216static int stm32_spi_claim_bus(struct udevice *slave)
217{
218 struct udevice *bus = dev_get_parent(slave);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200219 struct stm32_spi_plat *plat = dev_get_plat(bus);
220 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200221
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100222 dev_dbg(slave, "\n");
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200223
224 /* Enable the SPI hardware */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200225 return stm32_spi_enable(base);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200226}
227
228static int stm32_spi_release_bus(struct udevice *slave)
229{
230 struct udevice *bus = dev_get_parent(slave);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200231 struct stm32_spi_plat *plat = dev_get_plat(bus);
232 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200233
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100234 dev_dbg(slave, "\n");
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200235
236 /* Disable the SPI hardware */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200237 return stm32_spi_disable(base);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200238}
239
240static void stm32_spi_stopxfer(struct udevice *dev)
241{
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200242 struct stm32_spi_plat *plat = dev_get_plat(dev);
243 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200244 u32 cr1, sr;
245 int ret;
246
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100247 dev_dbg(dev, "\n");
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200248
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200249 cr1 = readl(base + STM32_SPI_CR1);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200250
251 if (!(cr1 & SPI_CR1_SPE))
252 return;
253
254 /* Wait on EOT or suspend the flow */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200255 ret = readl_poll_timeout(base + STM32_SPI_SR, sr,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200256 !(sr & SPI_SR_EOT), 100000);
257 if (ret < 0) {
258 if (cr1 & SPI_CR1_CSTART) {
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200259 writel(cr1 | SPI_CR1_CSUSP, base + STM32_SPI_CR1);
260 if (readl_poll_timeout(base + STM32_SPI_SR,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200261 sr, !(sr & SPI_SR_SUSP),
262 100000) < 0)
263 dev_err(dev, "Suspend request timeout\n");
264 }
265 }
266
267 /* clear status flags */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200268 setbits_le32(base + STM32_SPI_IFCR, SPI_IFCR_ALL);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200269}
270
271static int stm32_spi_set_cs(struct udevice *dev, unsigned int cs, bool enable)
272{
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200273 struct stm32_spi_plat *plat = dev_get_plat(dev);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200274 struct stm32_spi_priv *priv = dev_get_priv(dev);
275
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100276 dev_dbg(dev, "cs=%d enable=%d\n", cs, enable);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200277
278 if (cs >= MAX_CS_COUNT)
279 return -ENODEV;
280
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200281 if (!dm_gpio_is_valid(&plat->cs_gpios[cs]))
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200282 return -EINVAL;
283
284 if (priv->cs_high)
285 enable = !enable;
286
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200287 return dm_gpio_set_value(&plat->cs_gpios[cs], enable ? 1 : 0);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200288}
289
290static int stm32_spi_set_mode(struct udevice *bus, uint mode)
291{
292 struct stm32_spi_priv *priv = dev_get_priv(bus);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200293 struct stm32_spi_plat *plat = dev_get_plat(bus);
294 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200295 u32 cfg2_clrb = 0, cfg2_setb = 0;
296
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100297 dev_dbg(bus, "mode=%d\n", mode);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200298
299 if (mode & SPI_CPOL)
300 cfg2_setb |= SPI_CFG2_CPOL;
301 else
302 cfg2_clrb |= SPI_CFG2_CPOL;
303
304 if (mode & SPI_CPHA)
305 cfg2_setb |= SPI_CFG2_CPHA;
306 else
307 cfg2_clrb |= SPI_CFG2_CPHA;
308
309 if (mode & SPI_LSB_FIRST)
310 cfg2_setb |= SPI_CFG2_LSBFRST;
311 else
312 cfg2_clrb |= SPI_CFG2_LSBFRST;
313
314 if (cfg2_clrb || cfg2_setb)
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200315 clrsetbits_le32(base + STM32_SPI_CFG2,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200316 cfg2_clrb, cfg2_setb);
317
318 if (mode & SPI_CS_HIGH)
319 priv->cs_high = true;
320 else
321 priv->cs_high = false;
322 return 0;
323}
324
325static int stm32_spi_set_fthlv(struct udevice *dev, u32 xfer_len)
326{
327 struct stm32_spi_priv *priv = dev_get_priv(dev);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200328 struct stm32_spi_plat *plat = dev_get_plat(dev);
329 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200330 u32 fthlv, half_fifo;
331
332 /* data packet should not exceed 1/2 of fifo space */
333 half_fifo = (priv->fifo_size / 2);
334
335 /* data_packet should not exceed transfer length */
336 fthlv = (half_fifo > xfer_len) ? xfer_len : half_fifo;
337
338 /* align packet size with data registers access */
339 fthlv -= (fthlv % 4);
340
341 if (!fthlv)
342 fthlv = 1;
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200343 clrsetbits_le32(base + STM32_SPI_CFG1, SPI_CFG1_FTHLV,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200344 (fthlv - 1) << SPI_CFG1_FTHLV_SHIFT);
345
346 return 0;
347}
348
349static int stm32_spi_set_speed(struct udevice *bus, uint hz)
350{
351 struct stm32_spi_priv *priv = dev_get_priv(bus);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200352 struct stm32_spi_plat *plat = dev_get_plat(bus);
353 void __iomem *base = plat->base;
Patrick Delaunay5043c4b2019-06-21 15:26:58 +0200354 u32 mbrdiv;
355 long div;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200356
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100357 dev_dbg(bus, "hz=%d\n", hz);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200358
359 if (priv->cur_hz == hz)
360 return 0;
361
362 div = DIV_ROUND_UP(priv->bus_clk_rate, hz);
363
364 if (div < STM32_MBR_DIV_MIN ||
365 div > STM32_MBR_DIV_MAX)
366 return -EINVAL;
367
368 /* Determine the first power of 2 greater than or equal to div */
369 if (div & (div - 1))
370 mbrdiv = fls(div);
371 else
372 mbrdiv = fls(div) - 1;
373
Patrick Delaunay5043c4b2019-06-21 15:26:58 +0200374 if (!mbrdiv)
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200375 return -EINVAL;
376
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200377 clrsetbits_le32(base + STM32_SPI_CFG1, SPI_CFG1_MBR,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200378 (mbrdiv - 1) << SPI_CFG1_MBR_SHIFT);
379
380 priv->cur_hz = hz;
381
382 return 0;
383}
384
385static int stm32_spi_xfer(struct udevice *slave, unsigned int bitlen,
386 const void *dout, void *din, unsigned long flags)
387{
388 struct udevice *bus = dev_get_parent(slave);
Simon Glassb75b15b2020-12-03 16:55:23 -0700389 struct dm_spi_slave_plat *slave_plat;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200390 struct stm32_spi_priv *priv = dev_get_priv(bus);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200391 struct stm32_spi_plat *plat = dev_get_plat(bus);
392 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200393 u32 sr;
394 u32 ifcr = 0;
395 u32 xferlen;
396 u32 mode;
397 int xfer_status = 0;
398
399 xferlen = bitlen / 8;
400
401 if (xferlen <= SPI_CR2_TSIZE)
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200402 writel(xferlen, base + STM32_SPI_CR2);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200403 else
404 return -EMSGSIZE;
405
406 priv->tx_buf = dout;
407 priv->rx_buf = din;
408 priv->tx_len = priv->tx_buf ? bitlen / 8 : 0;
409 priv->rx_len = priv->rx_buf ? bitlen / 8 : 0;
410
411 mode = SPI_FULL_DUPLEX;
412 if (!priv->tx_buf)
413 mode = SPI_SIMPLEX_RX;
414 else if (!priv->rx_buf)
415 mode = SPI_SIMPLEX_TX;
416
417 if (priv->cur_xferlen != xferlen || priv->cur_mode != mode) {
418 priv->cur_mode = mode;
419 priv->cur_xferlen = xferlen;
420
421 /* Disable the SPI hardware to unlock CFG1/CFG2 registers */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200422 stm32_spi_disable(base);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200423
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200424 clrsetbits_le32(base + STM32_SPI_CFG2, SPI_CFG2_COMM,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200425 mode << SPI_CFG2_COMM_SHIFT);
426
427 stm32_spi_set_fthlv(bus, xferlen);
428
429 /* Enable the SPI hardware */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200430 stm32_spi_enable(base);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200431 }
432
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100433 dev_dbg(bus, "priv->tx_len=%d priv->rx_len=%d\n",
434 priv->tx_len, priv->rx_len);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200435
Simon Glass71fa5b42020-12-03 16:55:18 -0700436 slave_plat = dev_get_parent_plat(slave);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200437 if (flags & SPI_XFER_BEGIN)
438 stm32_spi_set_cs(bus, slave_plat->cs, false);
439
440 /* Be sure to have data in fifo before starting data transfer */
441 if (priv->tx_buf)
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200442 stm32_spi_write_txfifo(bus);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200443
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200444 setbits_le32(base + STM32_SPI_CR1, SPI_CR1_CSTART);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200445
446 while (1) {
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200447 sr = readl(base + STM32_SPI_SR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200448
449 if (sr & SPI_SR_OVR) {
450 dev_err(bus, "Overrun: RX data lost\n");
451 xfer_status = -EIO;
452 break;
453 }
454
455 if (sr & SPI_SR_SUSP) {
456 dev_warn(bus, "System too slow is limiting data throughput\n");
457
458 if (priv->rx_buf && priv->rx_len > 0)
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200459 stm32_spi_read_rxfifo(bus);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200460
461 ifcr |= SPI_SR_SUSP;
462 }
463
464 if (sr & SPI_SR_TXTF)
465 ifcr |= SPI_SR_TXTF;
466
467 if (sr & SPI_SR_TXP)
468 if (priv->tx_buf && priv->tx_len > 0)
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200469 stm32_spi_write_txfifo(bus);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200470
471 if (sr & SPI_SR_RXP)
472 if (priv->rx_buf && priv->rx_len > 0)
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200473 stm32_spi_read_rxfifo(bus);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200474
475 if (sr & SPI_SR_EOT) {
476 if (priv->rx_buf && priv->rx_len > 0)
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200477 stm32_spi_read_rxfifo(bus);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200478 break;
479 }
480
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200481 writel(ifcr, base + STM32_SPI_IFCR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200482 }
483
484 /* clear status flags */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200485 setbits_le32(base + STM32_SPI_IFCR, SPI_IFCR_ALL);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200486 stm32_spi_stopxfer(bus);
487
488 if (flags & SPI_XFER_END)
489 stm32_spi_set_cs(bus, slave_plat->cs, true);
490
491 return xfer_status;
492}
493
494static int stm32_spi_get_fifo_size(struct udevice *dev)
495{
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200496 struct stm32_spi_plat *plat = dev_get_plat(dev);
497 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200498 u32 count = 0;
499
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200500 stm32_spi_enable(base);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200501
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200502 while (readl(base + STM32_SPI_SR) & SPI_SR_TXP)
503 writeb(++count, base + STM32_SPI_TXDR);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200504
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200505 stm32_spi_disable(base);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200506
Patrick Delaunay7380f9e2020-11-06 19:01:52 +0100507 dev_dbg(dev, "%d x 8-bit fifo size\n", count);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200508
509 return count;
510}
511
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200512static int stm32_spi_of_to_plat(struct udevice *dev)
513{
514 struct stm32_spi_plat *plat = dev_get_plat(dev);
515 int ret;
516
517 plat->base = dev_read_addr_ptr(dev);
518 if (!plat->base) {
519 dev_err(dev, "can't get registers base address\n");
520 return -ENOENT;
521 }
522
523 ret = clk_get_by_index(dev, 0, &plat->clk);
524 if (ret < 0)
525 return ret;
526
527 ret = reset_get_by_index(dev, 0, &plat->rst_ctl);
528 if (ret < 0)
529 goto clk_err;
530
531 ret = gpio_request_list_by_name(dev, "cs-gpios", plat->cs_gpios,
532 ARRAY_SIZE(plat->cs_gpios), 0);
533 if (ret < 0) {
534 dev_err(dev, "Can't get %s cs gpios: %d", dev->name, ret);
535 ret = -ENOENT;
536 goto clk_err;
537 }
538
539 return 0;
540
541clk_err:
542 clk_free(&plat->clk);
543
544 return ret;
545}
546
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200547static int stm32_spi_probe(struct udevice *dev)
548{
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200549 struct stm32_spi_plat *plat = dev_get_plat(dev);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200550 struct stm32_spi_priv *priv = dev_get_priv(dev);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200551 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200552 unsigned long clk_rate;
553 int ret;
Patrick Delaunay5043c4b2019-06-21 15:26:58 +0200554 unsigned int i;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200555
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200556 /* enable clock */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200557 ret = clk_enable(&plat->clk);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200558 if (ret < 0)
559 return ret;
560
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200561 clk_rate = clk_get_rate(&plat->clk);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200562 if (!clk_rate) {
563 ret = -EINVAL;
564 goto clk_err;
565 }
566
567 priv->bus_clk_rate = clk_rate;
568
569 /* perform reset */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200570 reset_assert(&plat->rst_ctl);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200571 udelay(2);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200572 reset_deassert(&plat->rst_ctl);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200573
574 priv->fifo_size = stm32_spi_get_fifo_size(dev);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200575 priv->cur_mode = SPI_FULL_DUPLEX;
576 priv->cur_xferlen = 0;
577 priv->cur_bpw = SPI_DEFAULT_WORDLEN;
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200578 clrsetbits_le32(base + STM32_SPI_CFG1, SPI_CFG1_DSIZE,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200579 priv->cur_bpw - 1);
580
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200581 for (i = 0; i < ARRAY_SIZE(plat->cs_gpios); i++) {
582 if (!dm_gpio_is_valid(&plat->cs_gpios[i]))
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200583 continue;
584
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200585 dm_gpio_set_dir_flags(&plat->cs_gpios[i],
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200586 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
587 }
588
589 /* Ensure I2SMOD bit is kept cleared */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200590 clrbits_le32(base + STM32_SPI_I2SCFGR, SPI_I2SCFGR_I2SMOD);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200591
592 /*
593 * - SS input value high
594 * - transmitter half duplex direction
595 * - automatic communication suspend when RX-Fifo is full
596 */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200597 setbits_le32(base + STM32_SPI_CR1,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200598 SPI_CR1_SSI | SPI_CR1_HDDIR | SPI_CR1_MASRX);
599
600 /*
601 * - Set the master mode (default Motorola mode)
602 * - Consider 1 master/n slaves configuration and
603 * SS input value is determined by the SSI bit
604 * - keep control of all associated GPIOs
605 */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200606 setbits_le32(base + STM32_SPI_CFG2,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200607 SPI_CFG2_MASTER | SPI_CFG2_SSM | SPI_CFG2_AFCNTR);
608
609 return 0;
610
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200611clk_err:
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200612 clk_disable(&plat->clk);
613 clk_free(&plat->clk);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200614
615 return ret;
616};
617
618static int stm32_spi_remove(struct udevice *dev)
619{
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200620 struct stm32_spi_plat *plat = dev_get_plat(dev);
621 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200622 int ret;
623
624 stm32_spi_stopxfer(dev);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200625 stm32_spi_disable(base);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200626
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200627 ret = reset_assert(&plat->rst_ctl);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200628 if (ret < 0)
629 return ret;
630
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200631 reset_free(&plat->rst_ctl);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200632
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200633 ret = clk_disable(&plat->clk);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200634 if (ret < 0)
635 return ret;
636
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200637 clk_free(&plat->clk);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200638
639 return ret;
640};
641
642static const struct dm_spi_ops stm32_spi_ops = {
643 .claim_bus = stm32_spi_claim_bus,
644 .release_bus = stm32_spi_release_bus,
645 .set_mode = stm32_spi_set_mode,
646 .set_speed = stm32_spi_set_speed,
647 .xfer = stm32_spi_xfer,
648};
649
650static const struct udevice_id stm32_spi_ids[] = {
651 { .compatible = "st,stm32h7-spi", },
652 { }
653};
654
655U_BOOT_DRIVER(stm32_spi) = {
656 .name = "stm32_spi",
657 .id = UCLASS_SPI,
658 .of_match = stm32_spi_ids,
659 .ops = &stm32_spi_ops,
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200660 .of_to_plat = stm32_spi_of_to_plat,
661 .plat_auto = sizeof(struct stm32_spi_plat),
662 .priv_auto = sizeof(struct stm32_spi_priv),
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200663 .probe = stm32_spi_probe,
664 .remove = stm32_spi_remove,
665};