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