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