common: Drop linux/delay.h from common header
[oweals/u-boot.git] / drivers / spi / stm32_spi.c
1 // 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  */
7 #include <common.h>
8 #include <clk.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <log.h>
12 #include <malloc.h>
13 #include <reset.h>
14 #include <spi.h>
15 #include <dm/device_compat.h>
16 #include <linux/delay.h>
17
18 #include <asm/io.h>
19 #include <asm/gpio.h>
20 #include <linux/bitfield.h>
21 #include <linux/iopoll.h>
22
23 /* STM32 SPI registers */
24 #define STM32_SPI_CR1           0x00
25 #define STM32_SPI_CR2           0x04
26 #define STM32_SPI_CFG1          0x08
27 #define STM32_SPI_CFG2          0x0C
28 #define STM32_SPI_SR            0x14
29 #define STM32_SPI_IFCR          0x18
30 #define STM32_SPI_TXDR          0x20
31 #define STM32_SPI_RXDR          0x30
32 #define STM32_SPI_I2SCFGR       0x50
33
34 /* STM32_SPI_CR1 bit fields */
35 #define SPI_CR1_SPE             BIT(0)
36 #define SPI_CR1_MASRX           BIT(8)
37 #define SPI_CR1_CSTART          BIT(9)
38 #define SPI_CR1_CSUSP           BIT(10)
39 #define SPI_CR1_HDDIR           BIT(11)
40 #define SPI_CR1_SSI             BIT(12)
41
42 /* STM32_SPI_CR2 bit fields */
43 #define SPI_CR2_TSIZE           GENMASK(15, 0)
44
45 /* STM32_SPI_CFG1 bit fields */
46 #define SPI_CFG1_DSIZE          GENMASK(4, 0)
47 #define SPI_CFG1_DSIZE_MIN      3
48 #define SPI_CFG1_FTHLV_SHIFT    5
49 #define SPI_CFG1_FTHLV          GENMASK(8, 5)
50 #define SPI_CFG1_MBR_SHIFT      28
51 #define SPI_CFG1_MBR            GENMASK(30, 28)
52 #define SPI_CFG1_MBR_MIN        0
53 #define SPI_CFG1_MBR_MAX        FIELD_GET(SPI_CFG1_MBR, SPI_CFG1_MBR)
54
55 /* STM32_SPI_CFG2 bit fields */
56 #define SPI_CFG2_COMM_SHIFT     17
57 #define SPI_CFG2_COMM           GENMASK(18, 17)
58 #define SPI_CFG2_MASTER         BIT(22)
59 #define SPI_CFG2_LSBFRST        BIT(23)
60 #define SPI_CFG2_CPHA           BIT(24)
61 #define SPI_CFG2_CPOL           BIT(25)
62 #define SPI_CFG2_SSM            BIT(26)
63 #define SPI_CFG2_AFCNTR         BIT(31)
64
65 /* STM32_SPI_SR bit fields */
66 #define SPI_SR_RXP              BIT(0)
67 #define SPI_SR_TXP              BIT(1)
68 #define SPI_SR_EOT              BIT(3)
69 #define SPI_SR_TXTF             BIT(4)
70 #define SPI_SR_OVR              BIT(6)
71 #define SPI_SR_SUSP             BIT(11)
72 #define SPI_SR_RXPLVL_SHIFT     13
73 #define SPI_SR_RXPLVL           GENMASK(14, 13)
74 #define SPI_SR_RXWNE            BIT(15)
75
76 /* STM32_SPI_IFCR bit fields */
77 #define SPI_IFCR_ALL            GENMASK(11, 3)
78
79 /* STM32_SPI_I2SCFGR bit fields */
80 #define SPI_I2SCFGR_I2SMOD      BIT(0)
81
82 #define MAX_CS_COUNT    4
83
84 /* SPI Master Baud Rate min/max divisor */
85 #define STM32_MBR_DIV_MIN       (2 << SPI_CFG1_MBR_MIN)
86 #define STM32_MBR_DIV_MAX       (2 << SPI_CFG1_MBR_MAX)
87
88 #define STM32_SPI_TIMEOUT_US    100000
89
90 /* SPI Communication mode */
91 #define SPI_FULL_DUPLEX         0
92 #define SPI_SIMPLEX_TX          1
93 #define SPI_SIMPLEX_RX          2
94 #define SPI_HALF_DUPLEX         3
95
96 struct stm32_spi_priv {
97         void __iomem *base;
98         struct clk clk;
99         struct reset_ctl rst_ctl;
100         struct gpio_desc cs_gpios[MAX_CS_COUNT];
101         ulong bus_clk_rate;
102         unsigned int fifo_size;
103         unsigned int cur_bpw;
104         unsigned int cur_hz;
105         unsigned int cur_xferlen; /* current transfer length in bytes */
106         unsigned int tx_len;      /* number of data to be written in bytes */
107         unsigned int rx_len;      /* number of data to be read in bytes */
108         const void *tx_buf;       /* data to be written, or NULL */
109         void *rx_buf;             /* data to be read, or NULL */
110         u32 cur_mode;
111         bool cs_high;
112 };
113
114 static void stm32_spi_write_txfifo(struct stm32_spi_priv *priv)
115 {
116         while ((priv->tx_len > 0) &&
117                (readl(priv->base + STM32_SPI_SR) & SPI_SR_TXP)) {
118                 u32 offs = priv->cur_xferlen - priv->tx_len;
119
120                 if (priv->tx_len >= sizeof(u32) &&
121                     IS_ALIGNED((uintptr_t)(priv->tx_buf + offs), sizeof(u32))) {
122                         const u32 *tx_buf32 = (const u32 *)(priv->tx_buf + offs);
123
124                         writel(*tx_buf32, priv->base + STM32_SPI_TXDR);
125                         priv->tx_len -= sizeof(u32);
126                 } else if (priv->tx_len >= sizeof(u16) &&
127                            IS_ALIGNED((uintptr_t)(priv->tx_buf + offs), sizeof(u16))) {
128                         const u16 *tx_buf16 = (const u16 *)(priv->tx_buf + offs);
129
130                         writew(*tx_buf16, priv->base + STM32_SPI_TXDR);
131                         priv->tx_len -= sizeof(u16);
132                 } else {
133                         const u8 *tx_buf8 = (const u8 *)(priv->tx_buf + offs);
134
135                         writeb(*tx_buf8, priv->base + STM32_SPI_TXDR);
136                         priv->tx_len -= sizeof(u8);
137                 }
138         }
139
140         debug("%s: %d bytes left\n", __func__, priv->tx_len);
141 }
142
143 static void stm32_spi_read_rxfifo(struct stm32_spi_priv *priv)
144 {
145         u32 sr = readl(priv->base + STM32_SPI_SR);
146         u32 rxplvl = (sr & SPI_SR_RXPLVL) >> SPI_SR_RXPLVL_SHIFT;
147
148         while ((priv->rx_len > 0) &&
149                ((sr & SPI_SR_RXP) ||
150                ((sr & SPI_SR_EOT) && ((sr & SPI_SR_RXWNE) || (rxplvl > 0))))) {
151                 u32 offs = priv->cur_xferlen - priv->rx_len;
152
153                 if (IS_ALIGNED((uintptr_t)(priv->rx_buf + offs), sizeof(u32)) &&
154                     (priv->rx_len >= sizeof(u32) || (sr & SPI_SR_RXWNE))) {
155                         u32 *rx_buf32 = (u32 *)(priv->rx_buf + offs);
156
157                         *rx_buf32 = readl(priv->base + STM32_SPI_RXDR);
158                         priv->rx_len -= sizeof(u32);
159                 } else if (IS_ALIGNED((uintptr_t)(priv->rx_buf + offs), sizeof(u16)) &&
160                            (priv->rx_len >= sizeof(u16) ||
161                             (!(sr & SPI_SR_RXWNE) &&
162                             (rxplvl >= 2 || priv->cur_bpw > 8)))) {
163                         u16 *rx_buf16 = (u16 *)(priv->rx_buf + offs);
164
165                         *rx_buf16 = readw(priv->base + STM32_SPI_RXDR);
166                         priv->rx_len -= sizeof(u16);
167                 } else {
168                         u8 *rx_buf8 = (u8 *)(priv->rx_buf + offs);
169
170                         *rx_buf8 = readb(priv->base + STM32_SPI_RXDR);
171                         priv->rx_len -= sizeof(u8);
172                 }
173
174                 sr = readl(priv->base + STM32_SPI_SR);
175                 rxplvl = (sr & SPI_SR_RXPLVL) >> SPI_SR_RXPLVL_SHIFT;
176         }
177
178         debug("%s: %d bytes left\n", __func__, priv->rx_len);
179 }
180
181 static int stm32_spi_enable(struct stm32_spi_priv *priv)
182 {
183         debug("%s\n", __func__);
184
185         /* Enable the SPI hardware */
186         setbits_le32(priv->base + STM32_SPI_CR1, SPI_CR1_SPE);
187
188         return 0;
189 }
190
191 static int stm32_spi_disable(struct stm32_spi_priv *priv)
192 {
193         debug("%s\n", __func__);
194
195         /* Disable the SPI hardware */
196         clrbits_le32(priv->base + STM32_SPI_CR1, SPI_CR1_SPE);
197
198         return 0;
199 }
200
201 static int stm32_spi_claim_bus(struct udevice *slave)
202 {
203         struct udevice *bus = dev_get_parent(slave);
204         struct stm32_spi_priv *priv = dev_get_priv(bus);
205
206         debug("%s\n", __func__);
207
208         /* Enable the SPI hardware */
209         return stm32_spi_enable(priv);
210 }
211
212 static int stm32_spi_release_bus(struct udevice *slave)
213 {
214         struct udevice *bus = dev_get_parent(slave);
215         struct stm32_spi_priv *priv = dev_get_priv(bus);
216
217         debug("%s\n", __func__);
218
219         /* Disable the SPI hardware */
220         return stm32_spi_disable(priv);
221 }
222
223 static void stm32_spi_stopxfer(struct udevice *dev)
224 {
225         struct stm32_spi_priv *priv = dev_get_priv(dev);
226         u32 cr1, sr;
227         int ret;
228
229         debug("%s\n", __func__);
230
231         cr1 = readl(priv->base + STM32_SPI_CR1);
232
233         if (!(cr1 & SPI_CR1_SPE))
234                 return;
235
236         /* Wait on EOT or suspend the flow */
237         ret = readl_poll_timeout(priv->base + STM32_SPI_SR, sr,
238                                  !(sr & SPI_SR_EOT), 100000);
239         if (ret < 0) {
240                 if (cr1 & SPI_CR1_CSTART) {
241                         writel(cr1 | SPI_CR1_CSUSP, priv->base + STM32_SPI_CR1);
242                         if (readl_poll_timeout(priv->base + STM32_SPI_SR,
243                                                sr, !(sr & SPI_SR_SUSP),
244                                                100000) < 0)
245                                 dev_err(dev, "Suspend request timeout\n");
246                 }
247         }
248
249         /* clear status flags */
250         setbits_le32(priv->base + STM32_SPI_IFCR, SPI_IFCR_ALL);
251 }
252
253 static int stm32_spi_set_cs(struct udevice *dev, unsigned int cs, bool enable)
254 {
255         struct stm32_spi_priv *priv = dev_get_priv(dev);
256
257         debug("%s: cs=%d enable=%d\n", __func__, cs, enable);
258
259         if (cs >= MAX_CS_COUNT)
260                 return -ENODEV;
261
262         if (!dm_gpio_is_valid(&priv->cs_gpios[cs]))
263                 return -EINVAL;
264
265         if (priv->cs_high)
266                 enable = !enable;
267
268         return dm_gpio_set_value(&priv->cs_gpios[cs], enable ? 1 : 0);
269 }
270
271 static int stm32_spi_set_mode(struct udevice *bus, uint mode)
272 {
273         struct stm32_spi_priv *priv = dev_get_priv(bus);
274         u32 cfg2_clrb = 0, cfg2_setb = 0;
275
276         debug("%s: mode=%d\n", __func__, mode);
277
278         if (mode & SPI_CPOL)
279                 cfg2_setb |= SPI_CFG2_CPOL;
280         else
281                 cfg2_clrb |= SPI_CFG2_CPOL;
282
283         if (mode & SPI_CPHA)
284                 cfg2_setb |= SPI_CFG2_CPHA;
285         else
286                 cfg2_clrb |= SPI_CFG2_CPHA;
287
288         if (mode & SPI_LSB_FIRST)
289                 cfg2_setb |= SPI_CFG2_LSBFRST;
290         else
291                 cfg2_clrb |= SPI_CFG2_LSBFRST;
292
293         if (cfg2_clrb || cfg2_setb)
294                 clrsetbits_le32(priv->base + STM32_SPI_CFG2,
295                                 cfg2_clrb, cfg2_setb);
296
297         if (mode & SPI_CS_HIGH)
298                 priv->cs_high = true;
299         else
300                 priv->cs_high = false;
301         return 0;
302 }
303
304 static int stm32_spi_set_fthlv(struct udevice *dev, u32 xfer_len)
305 {
306         struct stm32_spi_priv *priv = dev_get_priv(dev);
307         u32 fthlv, half_fifo;
308
309         /* data packet should not exceed 1/2 of fifo space */
310         half_fifo = (priv->fifo_size / 2);
311
312         /* data_packet should not exceed transfer length */
313         fthlv = (half_fifo > xfer_len) ? xfer_len : half_fifo;
314
315         /* align packet size with data registers access */
316         fthlv -= (fthlv % 4);
317
318         if (!fthlv)
319                 fthlv = 1;
320         clrsetbits_le32(priv->base + STM32_SPI_CFG1, SPI_CFG1_FTHLV,
321                         (fthlv - 1) << SPI_CFG1_FTHLV_SHIFT);
322
323         return 0;
324 }
325
326 static int stm32_spi_set_speed(struct udevice *bus, uint hz)
327 {
328         struct stm32_spi_priv *priv = dev_get_priv(bus);
329         u32 mbrdiv;
330         long div;
331
332         debug("%s: hz=%d\n", __func__, hz);
333
334         if (priv->cur_hz == hz)
335                 return 0;
336
337         div = DIV_ROUND_UP(priv->bus_clk_rate, hz);
338
339         if (div < STM32_MBR_DIV_MIN ||
340             div > STM32_MBR_DIV_MAX)
341                 return -EINVAL;
342
343         /* Determine the first power of 2 greater than or equal to div */
344         if (div & (div - 1))
345                 mbrdiv = fls(div);
346         else
347                 mbrdiv = fls(div) - 1;
348
349         if (!mbrdiv)
350                 return -EINVAL;
351
352         clrsetbits_le32(priv->base + STM32_SPI_CFG1, SPI_CFG1_MBR,
353                         (mbrdiv - 1) << SPI_CFG1_MBR_SHIFT);
354
355         priv->cur_hz = hz;
356
357         return 0;
358 }
359
360 static int stm32_spi_xfer(struct udevice *slave, unsigned int bitlen,
361                           const void *dout, void *din, unsigned long flags)
362 {
363         struct udevice *bus = dev_get_parent(slave);
364         struct dm_spi_slave_platdata *slave_plat;
365         struct stm32_spi_priv *priv = dev_get_priv(bus);
366         u32 sr;
367         u32 ifcr = 0;
368         u32 xferlen;
369         u32 mode;
370         int xfer_status = 0;
371
372         xferlen = bitlen / 8;
373
374         if (xferlen <= SPI_CR2_TSIZE)
375                 writel(xferlen, priv->base + STM32_SPI_CR2);
376         else
377                 return -EMSGSIZE;
378
379         priv->tx_buf = dout;
380         priv->rx_buf = din;
381         priv->tx_len = priv->tx_buf ? bitlen / 8 : 0;
382         priv->rx_len = priv->rx_buf ? bitlen / 8 : 0;
383
384         mode = SPI_FULL_DUPLEX;
385         if (!priv->tx_buf)
386                 mode = SPI_SIMPLEX_RX;
387         else if (!priv->rx_buf)
388                 mode = SPI_SIMPLEX_TX;
389
390         if (priv->cur_xferlen != xferlen || priv->cur_mode != mode) {
391                 priv->cur_mode = mode;
392                 priv->cur_xferlen = xferlen;
393
394                 /* Disable the SPI hardware to unlock CFG1/CFG2 registers */
395                 stm32_spi_disable(priv);
396
397                 clrsetbits_le32(priv->base + STM32_SPI_CFG2, SPI_CFG2_COMM,
398                                 mode << SPI_CFG2_COMM_SHIFT);
399
400                 stm32_spi_set_fthlv(bus, xferlen);
401
402                 /* Enable the SPI hardware */
403                 stm32_spi_enable(priv);
404         }
405
406         debug("%s: priv->tx_len=%d priv->rx_len=%d\n", __func__,
407               priv->tx_len, priv->rx_len);
408
409         slave_plat = dev_get_parent_platdata(slave);
410         if (flags & SPI_XFER_BEGIN)
411                 stm32_spi_set_cs(bus, slave_plat->cs, false);
412
413         /* Be sure to have data in fifo before starting data transfer */
414         if (priv->tx_buf)
415                 stm32_spi_write_txfifo(priv);
416
417         setbits_le32(priv->base + STM32_SPI_CR1, SPI_CR1_CSTART);
418
419         while (1) {
420                 sr = readl(priv->base + STM32_SPI_SR);
421
422                 if (sr & SPI_SR_OVR) {
423                         dev_err(bus, "Overrun: RX data lost\n");
424                         xfer_status = -EIO;
425                         break;
426                 }
427
428                 if (sr & SPI_SR_SUSP) {
429                         dev_warn(bus, "System too slow is limiting data throughput\n");
430
431                         if (priv->rx_buf && priv->rx_len > 0)
432                                 stm32_spi_read_rxfifo(priv);
433
434                         ifcr |= SPI_SR_SUSP;
435                 }
436
437                 if (sr & SPI_SR_TXTF)
438                         ifcr |= SPI_SR_TXTF;
439
440                 if (sr & SPI_SR_TXP)
441                         if (priv->tx_buf && priv->tx_len > 0)
442                                 stm32_spi_write_txfifo(priv);
443
444                 if (sr & SPI_SR_RXP)
445                         if (priv->rx_buf && priv->rx_len > 0)
446                                 stm32_spi_read_rxfifo(priv);
447
448                 if (sr & SPI_SR_EOT) {
449                         if (priv->rx_buf && priv->rx_len > 0)
450                                 stm32_spi_read_rxfifo(priv);
451                         break;
452                 }
453
454                 writel(ifcr, priv->base + STM32_SPI_IFCR);
455         }
456
457         /* clear status flags */
458         setbits_le32(priv->base + STM32_SPI_IFCR, SPI_IFCR_ALL);
459         stm32_spi_stopxfer(bus);
460
461         if (flags & SPI_XFER_END)
462                 stm32_spi_set_cs(bus, slave_plat->cs, true);
463
464         return xfer_status;
465 }
466
467 static int stm32_spi_get_fifo_size(struct udevice *dev)
468 {
469         struct stm32_spi_priv *priv = dev_get_priv(dev);
470         u32 count = 0;
471
472         stm32_spi_enable(priv);
473
474         while (readl(priv->base + STM32_SPI_SR) & SPI_SR_TXP)
475                 writeb(++count, priv->base + STM32_SPI_TXDR);
476
477         stm32_spi_disable(priv);
478
479         debug("%s %d x 8-bit fifo size\n", __func__, count);
480
481         return count;
482 }
483
484 static int stm32_spi_probe(struct udevice *dev)
485 {
486         struct stm32_spi_priv *priv = dev_get_priv(dev);
487         unsigned long clk_rate;
488         int ret;
489         unsigned int i;
490
491         priv->base = dev_remap_addr(dev);
492         if (!priv->base)
493                 return -EINVAL;
494
495         /* enable clock */
496         ret = clk_get_by_index(dev, 0, &priv->clk);
497         if (ret < 0)
498                 return ret;
499
500         ret = clk_enable(&priv->clk);
501         if (ret < 0)
502                 return ret;
503
504         clk_rate = clk_get_rate(&priv->clk);
505         if (!clk_rate) {
506                 ret = -EINVAL;
507                 goto clk_err;
508         }
509
510         priv->bus_clk_rate = clk_rate;
511
512         /* perform reset */
513         ret = reset_get_by_index(dev, 0, &priv->rst_ctl);
514         if (ret < 0)
515                 goto clk_err;
516
517         reset_assert(&priv->rst_ctl);
518         udelay(2);
519         reset_deassert(&priv->rst_ctl);
520
521         ret = gpio_request_list_by_name(dev, "cs-gpios", priv->cs_gpios,
522                                         ARRAY_SIZE(priv->cs_gpios), 0);
523         if (ret < 0) {
524                 pr_err("Can't get %s cs gpios: %d", dev->name, ret);
525                 goto reset_err;
526         }
527
528         priv->fifo_size = stm32_spi_get_fifo_size(dev);
529
530         priv->cur_mode = SPI_FULL_DUPLEX;
531         priv->cur_xferlen = 0;
532         priv->cur_bpw = SPI_DEFAULT_WORDLEN;
533         clrsetbits_le32(priv->base + STM32_SPI_CFG1, SPI_CFG1_DSIZE,
534                         priv->cur_bpw - 1);
535
536         for (i = 0; i < ARRAY_SIZE(priv->cs_gpios); i++) {
537                 if (!dm_gpio_is_valid(&priv->cs_gpios[i]))
538                         continue;
539
540                 dm_gpio_set_dir_flags(&priv->cs_gpios[i],
541                                       GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
542         }
543
544         /* Ensure I2SMOD bit is kept cleared */
545         clrbits_le32(priv->base + STM32_SPI_I2SCFGR, SPI_I2SCFGR_I2SMOD);
546
547         /*
548          * - SS input value high
549          * - transmitter half duplex direction
550          * - automatic communication suspend when RX-Fifo is full
551          */
552         setbits_le32(priv->base + STM32_SPI_CR1,
553                      SPI_CR1_SSI | SPI_CR1_HDDIR | SPI_CR1_MASRX);
554
555         /*
556          * - Set the master mode (default Motorola mode)
557          * - Consider 1 master/n slaves configuration and
558          *   SS input value is determined by the SSI bit
559          * - keep control of all associated GPIOs
560          */
561         setbits_le32(priv->base + STM32_SPI_CFG2,
562                      SPI_CFG2_MASTER | SPI_CFG2_SSM | SPI_CFG2_AFCNTR);
563
564         return 0;
565
566 reset_err:
567         reset_free(&priv->rst_ctl);
568
569 clk_err:
570         clk_disable(&priv->clk);
571         clk_free(&priv->clk);
572
573         return ret;
574 };
575
576 static int stm32_spi_remove(struct udevice *dev)
577 {
578         struct stm32_spi_priv *priv = dev_get_priv(dev);
579         int ret;
580
581         stm32_spi_stopxfer(dev);
582         stm32_spi_disable(priv);
583
584         ret = reset_assert(&priv->rst_ctl);
585         if (ret < 0)
586                 return ret;
587
588         reset_free(&priv->rst_ctl);
589
590         ret = clk_disable(&priv->clk);
591         if (ret < 0)
592                 return ret;
593
594         clk_free(&priv->clk);
595
596         return ret;
597 };
598
599 static const struct dm_spi_ops stm32_spi_ops = {
600         .claim_bus      = stm32_spi_claim_bus,
601         .release_bus    = stm32_spi_release_bus,
602         .set_mode       = stm32_spi_set_mode,
603         .set_speed      = stm32_spi_set_speed,
604         .xfer           = stm32_spi_xfer,
605 };
606
607 static const struct udevice_id stm32_spi_ids[] = {
608         { .compatible = "st,stm32h7-spi", },
609         { }
610 };
611
612 U_BOOT_DRIVER(stm32_spi) = {
613         .name                   = "stm32_spi",
614         .id                     = UCLASS_SPI,
615         .of_match               = stm32_spi_ids,
616         .ops                    = &stm32_spi_ops,
617         .priv_auto_alloc_size   = sizeof(struct stm32_spi_priv),
618         .probe                  = stm32_spi_probe,
619         .remove                 = stm32_spi_remove,
620 };