blob: fe5419e8518232aa88ae8759fb8116e0f0e63d47 [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>
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)
528 goto clk_err;
529
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);
534 ret = -ENOENT;
535 goto clk_err;
536 }
537
538 return 0;
539
540clk_err:
541 clk_free(&plat->clk);
542
543 return ret;
544}
545
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200546static int stm32_spi_probe(struct udevice *dev)
547{
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200548 struct stm32_spi_plat *plat = dev_get_plat(dev);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200549 struct stm32_spi_priv *priv = dev_get_priv(dev);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200550 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200551 unsigned long clk_rate;
552 int ret;
Patrick Delaunay5043c4b2019-06-21 15:26:58 +0200553 unsigned int i;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200554
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200555 /* enable clock */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200556 ret = clk_enable(&plat->clk);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200557 if (ret < 0)
558 return ret;
559
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200560 clk_rate = clk_get_rate(&plat->clk);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200561 if (!clk_rate) {
562 ret = -EINVAL;
563 goto clk_err;
564 }
565
566 priv->bus_clk_rate = clk_rate;
567
568 /* perform reset */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200569 reset_assert(&plat->rst_ctl);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200570 udelay(2);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200571 reset_deassert(&plat->rst_ctl);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200572
573 priv->fifo_size = stm32_spi_get_fifo_size(dev);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200574 priv->cur_mode = SPI_FULL_DUPLEX;
575 priv->cur_xferlen = 0;
576 priv->cur_bpw = SPI_DEFAULT_WORDLEN;
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200577 clrsetbits_le32(base + STM32_SPI_CFG1, SPI_CFG1_DSIZE,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200578 priv->cur_bpw - 1);
579
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200580 for (i = 0; i < ARRAY_SIZE(plat->cs_gpios); i++) {
581 if (!dm_gpio_is_valid(&plat->cs_gpios[i]))
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200582 continue;
583
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200584 dm_gpio_set_dir_flags(&plat->cs_gpios[i],
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200585 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
586 }
587
588 /* Ensure I2SMOD bit is kept cleared */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200589 clrbits_le32(base + STM32_SPI_I2SCFGR, SPI_I2SCFGR_I2SMOD);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200590
591 /*
592 * - SS input value high
593 * - transmitter half duplex direction
594 * - automatic communication suspend when RX-Fifo is full
595 */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200596 setbits_le32(base + STM32_SPI_CR1,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200597 SPI_CR1_SSI | SPI_CR1_HDDIR | SPI_CR1_MASRX);
598
599 /*
600 * - Set the master mode (default Motorola mode)
601 * - Consider 1 master/n slaves configuration and
602 * SS input value is determined by the SSI bit
603 * - keep control of all associated GPIOs
604 */
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200605 setbits_le32(base + STM32_SPI_CFG2,
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200606 SPI_CFG2_MASTER | SPI_CFG2_SSM | SPI_CFG2_AFCNTR);
607
608 return 0;
609
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200610clk_err:
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200611 clk_disable(&plat->clk);
612 clk_free(&plat->clk);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200613
614 return ret;
615};
616
617static int stm32_spi_remove(struct udevice *dev)
618{
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200619 struct stm32_spi_plat *plat = dev_get_plat(dev);
620 void __iomem *base = plat->base;
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200621 int ret;
622
623 stm32_spi_stopxfer(dev);
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200624 stm32_spi_disable(base);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200625
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200626 ret = reset_assert(&plat->rst_ctl);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200627 if (ret < 0)
628 return ret;
629
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200630 reset_free(&plat->rst_ctl);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200631
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200632 ret = clk_disable(&plat->clk);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200633 if (ret < 0)
634 return ret;
635
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200636 clk_free(&plat->clk);
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200637
638 return ret;
639};
640
641static const struct dm_spi_ops stm32_spi_ops = {
642 .claim_bus = stm32_spi_claim_bus,
643 .release_bus = stm32_spi_release_bus,
644 .set_mode = stm32_spi_set_mode,
645 .set_speed = stm32_spi_set_speed,
646 .xfer = stm32_spi_xfer,
647};
648
649static const struct udevice_id stm32_spi_ids[] = {
650 { .compatible = "st,stm32h7-spi", },
651 { }
652};
653
654U_BOOT_DRIVER(stm32_spi) = {
655 .name = "stm32_spi",
656 .id = UCLASS_SPI,
657 .of_match = stm32_spi_ids,
658 .ops = &stm32_spi_ops,
Patrice Chotarda9f8bbd2021-08-03 11:16:40 +0200659 .of_to_plat = stm32_spi_of_to_plat,
660 .plat_auto = sizeof(struct stm32_spi_plat),
661 .priv_auto = sizeof(struct stm32_spi_priv),
Patrice Chotard0b08bf82019-04-30 18:08:28 +0200662 .probe = stm32_spi_probe,
663 .remove = stm32_spi_remove,
664};