common: Drop linux/delay.h from common header
[oweals/u-boot.git] / drivers / spi / davinci_spi.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
4  *
5  * Driver for SPI controller on DaVinci. Based on atmel_spi.c
6  * by Atmel Corporation
7  *
8  * Copyright (C) 2007 Atmel Corporation
9  */
10
11 #include <common.h>
12 #include <log.h>
13 #include <spi.h>
14 #include <malloc.h>
15 #include <asm/io.h>
16 #include <asm/arch/hardware.h>
17 #include <dm.h>
18 #include <dm/platform_data/spi_davinci.h>
19 #include <linux/delay.h>
20
21 /* SPIGCR0 */
22 #define SPIGCR0_SPIENA_MASK     0x1
23 #define SPIGCR0_SPIRST_MASK     0x0
24
25 /* SPIGCR0 */
26 #define SPIGCR1_CLKMOD_MASK     BIT(1)
27 #define SPIGCR1_MASTER_MASK     BIT(0)
28 #define SPIGCR1_SPIENA_MASK     BIT(24)
29
30 /* SPIPC0 */
31 #define SPIPC0_DIFUN_MASK       BIT(11)         /* SIMO */
32 #define SPIPC0_DOFUN_MASK       BIT(10)         /* SOMI */
33 #define SPIPC0_CLKFUN_MASK      BIT(9)          /* CLK */
34 #define SPIPC0_EN0FUN_MASK      BIT(0)
35
36 /* SPIFMT0 */
37 #define SPIFMT_SHIFTDIR_SHIFT   20
38 #define SPIFMT_POLARITY_SHIFT   17
39 #define SPIFMT_PHASE_SHIFT      16
40 #define SPIFMT_PRESCALE_SHIFT   8
41
42 /* SPIDAT1 */
43 #define SPIDAT1_CSHOLD_SHIFT    28
44 #define SPIDAT1_CSNR_SHIFT      16
45
46 /* SPIDELAY */
47 #define SPI_C2TDELAY_SHIFT      24
48 #define SPI_T2CDELAY_SHIFT      16
49
50 /* SPIBUF */
51 #define SPIBUF_RXEMPTY_MASK     BIT(31)
52 #define SPIBUF_TXFULL_MASK      BIT(29)
53
54 /* SPIDEF */
55 #define SPIDEF_CSDEF0_MASK      BIT(0)
56
57 #ifndef CONFIG_DM_SPI
58 #define SPI0_BUS                0
59 #define SPI0_BASE               CONFIG_SYS_SPI_BASE
60 /*
61  * Define default SPI0_NUM_CS as 1 for existing platforms that uses this
62  * driver. Platform can configure number of CS using CONFIG_SYS_SPI0_NUM_CS
63  * if more than one CS is supported and by defining CONFIG_SYS_SPI0.
64  */
65 #ifndef CONFIG_SYS_SPI0
66 #define SPI0_NUM_CS             1
67 #else
68 #define SPI0_NUM_CS             CONFIG_SYS_SPI0_NUM_CS
69 #endif
70
71 /*
72  * define CONFIG_SYS_SPI1 when platform has spi-1 device (bus #1) and
73  * CONFIG_SYS_SPI1_NUM_CS defines number of CS on this bus
74  */
75 #ifdef CONFIG_SYS_SPI1
76 #define SPI1_BUS                1
77 #define SPI1_NUM_CS             CONFIG_SYS_SPI1_NUM_CS
78 #define SPI1_BASE               CONFIG_SYS_SPI1_BASE
79 #endif
80
81 /*
82  * define CONFIG_SYS_SPI2 when platform has spi-2 device (bus #2) and
83  * CONFIG_SYS_SPI2_NUM_CS defines number of CS on this bus
84  */
85 #ifdef CONFIG_SYS_SPI2
86 #define SPI2_BUS                2
87 #define SPI2_NUM_CS             CONFIG_SYS_SPI2_NUM_CS
88 #define SPI2_BASE               CONFIG_SYS_SPI2_BASE
89 #endif
90 #endif
91
92 DECLARE_GLOBAL_DATA_PTR;
93
94 /* davinci spi register set */
95 struct davinci_spi_regs {
96         dv_reg  gcr0;           /* 0x00 */
97         dv_reg  gcr1;           /* 0x04 */
98         dv_reg  int0;           /* 0x08 */
99         dv_reg  lvl;            /* 0x0c */
100         dv_reg  flg;            /* 0x10 */
101         dv_reg  pc0;            /* 0x14 */
102         dv_reg  pc1;            /* 0x18 */
103         dv_reg  pc2;            /* 0x1c */
104         dv_reg  pc3;            /* 0x20 */
105         dv_reg  pc4;            /* 0x24 */
106         dv_reg  pc5;            /* 0x28 */
107         dv_reg  rsvd[3];
108         dv_reg  dat0;           /* 0x38 */
109         dv_reg  dat1;           /* 0x3c */
110         dv_reg  buf;            /* 0x40 */
111         dv_reg  emu;            /* 0x44 */
112         dv_reg  delay;          /* 0x48 */
113         dv_reg  def;            /* 0x4c */
114         dv_reg  fmt0;           /* 0x50 */
115         dv_reg  fmt1;           /* 0x54 */
116         dv_reg  fmt2;           /* 0x58 */
117         dv_reg  fmt3;           /* 0x5c */
118         dv_reg  intvec0;        /* 0x60 */
119         dv_reg  intvec1;        /* 0x64 */
120 };
121
122 /* davinci spi slave */
123 struct davinci_spi_slave {
124 #ifndef CONFIG_DM_SPI
125         struct spi_slave slave;
126 #endif
127         struct davinci_spi_regs *regs;
128         unsigned int freq; /* current SPI bus frequency */
129         unsigned int mode; /* current SPI mode used */
130         u8 num_cs;         /* total no. of CS available */
131         u8 cur_cs;         /* CS of current slave */
132         bool half_duplex;  /* true, if master is half-duplex only */
133 };
134
135 /*
136  * This functions needs to act like a macro to avoid pipeline reloads in the
137  * loops below. Use always_inline. This gains us about 160KiB/s and the bloat
138  * appears to be zero bytes (da830).
139  */
140 __attribute__((always_inline))
141 static inline u32 davinci_spi_xfer_data(struct davinci_spi_slave *ds, u32 data)
142 {
143         u32     buf_reg_val;
144
145         /* send out data */
146         writel(data, &ds->regs->dat1);
147
148         /* wait for the data to clock in/out */
149         while ((buf_reg_val = readl(&ds->regs->buf)) & SPIBUF_RXEMPTY_MASK)
150                 ;
151
152         return buf_reg_val;
153 }
154
155 static int davinci_spi_read(struct davinci_spi_slave *ds, unsigned int len,
156                             u8 *rxp, unsigned long flags)
157 {
158         unsigned int data1_reg_val;
159
160         /* enable CS hold, CS[n] and clear the data bits */
161         data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
162                          (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
163
164         /* wait till TXFULL is deasserted */
165         while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
166                 ;
167
168         /* preload the TX buffer to avoid clock starvation */
169         writel(data1_reg_val, &ds->regs->dat1);
170
171         /* keep reading 1 byte until only 1 byte left */
172         while ((len--) > 1)
173                 *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val);
174
175         /* clear CS hold when we reach the end */
176         if (flags & SPI_XFER_END)
177                 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
178
179         /* read the last byte */
180         *rxp = davinci_spi_xfer_data(ds, data1_reg_val);
181
182         return 0;
183 }
184
185 static int davinci_spi_write(struct davinci_spi_slave *ds, unsigned int len,
186                              const u8 *txp, unsigned long flags)
187 {
188         unsigned int data1_reg_val;
189
190         /* enable CS hold and clear the data bits */
191         data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
192                          (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
193
194         /* wait till TXFULL is deasserted */
195         while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
196                 ;
197
198         /* preload the TX buffer to avoid clock starvation */
199         if (len > 2) {
200                 writel(data1_reg_val | *txp++, &ds->regs->dat1);
201                 len--;
202         }
203
204         /* keep writing 1 byte until only 1 byte left */
205         while ((len--) > 1)
206                 davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
207
208         /* clear CS hold when we reach the end */
209         if (flags & SPI_XFER_END)
210                 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
211
212         /* write the last byte */
213         davinci_spi_xfer_data(ds, data1_reg_val | *txp);
214
215         return 0;
216 }
217
218 static int davinci_spi_read_write(struct davinci_spi_slave *ds, unsigned
219                                   int len, u8 *rxp, const u8 *txp,
220                                   unsigned long flags)
221 {
222         unsigned int data1_reg_val;
223
224         /* enable CS hold and clear the data bits */
225         data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
226                          (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
227
228         /* wait till TXFULL is deasserted */
229         while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
230                 ;
231
232         /* keep reading and writing 1 byte until only 1 byte left */
233         while ((len--) > 1)
234                 *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
235
236         /* clear CS hold when we reach the end */
237         if (flags & SPI_XFER_END)
238                 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
239
240         /* read and write the last byte */
241         *rxp = davinci_spi_xfer_data(ds, data1_reg_val | *txp);
242
243         return 0;
244 }
245
246
247 static int __davinci_spi_claim_bus(struct davinci_spi_slave *ds, int cs)
248 {
249         unsigned int mode = 0, scalar;
250
251         /* Enable the SPI hardware */
252         writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
253         udelay(1000);
254         writel(SPIGCR0_SPIENA_MASK, &ds->regs->gcr0);
255
256         /* Set master mode, powered up and not activated */
257         writel(SPIGCR1_MASTER_MASK | SPIGCR1_CLKMOD_MASK, &ds->regs->gcr1);
258
259         /* CS, CLK, SIMO and SOMI are functional pins */
260         writel(((1 << cs) | SPIPC0_CLKFUN_MASK |
261                 SPIPC0_DOFUN_MASK | SPIPC0_DIFUN_MASK), &ds->regs->pc0);
262
263         /* setup format */
264         scalar = ((CONFIG_SYS_SPI_CLK / ds->freq) - 1) & 0xFF;
265
266         /*
267          * Use following format:
268          *   character length = 8,
269          *   MSB shifted out first
270          */
271         if (ds->mode & SPI_CPOL)
272                 mode |= SPI_CPOL;
273         if (!(ds->mode & SPI_CPHA))
274                 mode |= SPI_CPHA;
275         writel(8 | (scalar << SPIFMT_PRESCALE_SHIFT) |
276                 (mode << SPIFMT_PHASE_SHIFT), &ds->regs->fmt0);
277
278         /*
279          * Including a minor delay. No science here. Should be good even with
280          * no delay
281          */
282         writel((50 << SPI_C2TDELAY_SHIFT) |
283                 (50 << SPI_T2CDELAY_SHIFT), &ds->regs->delay);
284
285         /* default chip select register */
286         writel(SPIDEF_CSDEF0_MASK, &ds->regs->def);
287
288         /* no interrupts */
289         writel(0, &ds->regs->int0);
290         writel(0, &ds->regs->lvl);
291
292         /* enable SPI */
293         writel((readl(&ds->regs->gcr1) | SPIGCR1_SPIENA_MASK), &ds->regs->gcr1);
294
295         return 0;
296 }
297
298 static int __davinci_spi_release_bus(struct davinci_spi_slave *ds)
299 {
300         /* Disable the SPI hardware */
301         writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
302
303         return 0;
304 }
305
306 static int __davinci_spi_xfer(struct davinci_spi_slave *ds,
307                 unsigned int bitlen,  const void *dout, void *din,
308                 unsigned long flags)
309 {
310         unsigned int len;
311
312         if (bitlen == 0)
313                 /* Finish any previously submitted transfers */
314                 goto out;
315
316         /*
317          * It's not clear how non-8-bit-aligned transfers are supposed to be
318          * represented as a stream of bytes...this is a limitation of
319          * the current SPI interface - here we terminate on receiving such a
320          * transfer request.
321          */
322         if (bitlen % 8) {
323                 /* Errors always terminate an ongoing transfer */
324                 flags |= SPI_XFER_END;
325                 goto out;
326         }
327
328         len = bitlen / 8;
329
330         if (!dout)
331                 return davinci_spi_read(ds, len, din, flags);
332         if (!din)
333                 return davinci_spi_write(ds, len, dout, flags);
334         if (!ds->half_duplex)
335                 return davinci_spi_read_write(ds, len, din, dout, flags);
336
337         printf("SPI full duplex not supported\n");
338         flags |= SPI_XFER_END;
339
340 out:
341         if (flags & SPI_XFER_END) {
342                 u8 dummy = 0;
343                 davinci_spi_write(ds, 1, &dummy, flags);
344         }
345         return 0;
346 }
347
348 #ifndef CONFIG_DM_SPI
349
350 static inline struct davinci_spi_slave *to_davinci_spi(struct spi_slave *slave)
351 {
352         return container_of(slave, struct davinci_spi_slave, slave);
353 }
354
355 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
356 {
357         int ret = 0;
358
359         switch (bus) {
360         case SPI0_BUS:
361                 if (cs < SPI0_NUM_CS)
362                         ret = 1;
363                 break;
364 #ifdef CONFIG_SYS_SPI1
365         case SPI1_BUS:
366                 if (cs < SPI1_NUM_CS)
367                         ret = 1;
368                 break;
369 #endif
370 #ifdef CONFIG_SYS_SPI2
371         case SPI2_BUS:
372                 if (cs < SPI2_NUM_CS)
373                         ret = 1;
374                 break;
375 #endif
376         default:
377                 /* Invalid bus number. Do nothing */
378                 break;
379         }
380         return ret;
381 }
382
383 void spi_cs_activate(struct spi_slave *slave)
384 {
385         /* do nothing */
386 }
387
388 void spi_cs_deactivate(struct spi_slave *slave)
389 {
390         /* do nothing */
391 }
392
393 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
394                         unsigned int max_hz, unsigned int mode)
395 {
396         struct davinci_spi_slave        *ds;
397
398         if (!spi_cs_is_valid(bus, cs))
399                 return NULL;
400
401         ds = spi_alloc_slave(struct davinci_spi_slave, bus, cs);
402         if (!ds)
403                 return NULL;
404
405         switch (bus) {
406         case SPI0_BUS:
407                 ds->regs = (struct davinci_spi_regs *)SPI0_BASE;
408                 break;
409 #ifdef CONFIG_SYS_SPI1
410         case SPI1_BUS:
411                 ds->regs = (struct davinci_spi_regs *)SPI1_BASE;
412                 break;
413 #endif
414 #ifdef CONFIG_SYS_SPI2
415         case SPI2_BUS:
416                 ds->regs = (struct davinci_spi_regs *)SPI2_BASE;
417                 break;
418 #endif
419         default: /* Invalid bus number */
420                 return NULL;
421         }
422
423         ds->freq = max_hz;
424         ds->mode = mode;
425
426         return &ds->slave;
427 }
428
429 void spi_free_slave(struct spi_slave *slave)
430 {
431         struct davinci_spi_slave *ds = to_davinci_spi(slave);
432
433         free(ds);
434 }
435
436 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
437              const void *dout, void *din, unsigned long flags)
438 {
439         struct davinci_spi_slave *ds = to_davinci_spi(slave);
440
441         ds->cur_cs = slave->cs;
442
443         return __davinci_spi_xfer(ds, bitlen, dout, din, flags);
444 }
445
446 int spi_claim_bus(struct spi_slave *slave)
447 {
448         struct davinci_spi_slave *ds = to_davinci_spi(slave);
449
450 #ifdef CONFIG_SPI_HALF_DUPLEX
451         ds->half_duplex = true;
452 #else
453         ds->half_duplex = false;
454 #endif
455         return __davinci_spi_claim_bus(ds, ds->slave.cs);
456 }
457
458 void spi_release_bus(struct spi_slave *slave)
459 {
460         struct davinci_spi_slave *ds = to_davinci_spi(slave);
461
462         __davinci_spi_release_bus(ds);
463 }
464
465 #else
466 static int davinci_spi_set_speed(struct udevice *bus, uint max_hz)
467 {
468         struct davinci_spi_slave *ds = dev_get_priv(bus);
469
470         debug("%s speed %u\n", __func__, max_hz);
471         if (max_hz > CONFIG_SYS_SPI_CLK / 2)
472                 return -EINVAL;
473
474         ds->freq = max_hz;
475
476         return 0;
477 }
478
479 static int davinci_spi_set_mode(struct udevice *bus, uint mode)
480 {
481         struct davinci_spi_slave *ds = dev_get_priv(bus);
482
483         debug("%s mode %u\n", __func__, mode);
484         ds->mode = mode;
485
486         return 0;
487 }
488
489 static int davinci_spi_claim_bus(struct udevice *dev)
490 {
491         struct dm_spi_slave_platdata *slave_plat =
492                 dev_get_parent_platdata(dev);
493         struct udevice *bus = dev->parent;
494         struct davinci_spi_slave *ds = dev_get_priv(bus);
495
496         if (slave_plat->cs >= ds->num_cs) {
497                 printf("Invalid SPI chipselect\n");
498                 return -EINVAL;
499         }
500         ds->half_duplex = slave_plat->mode & SPI_PREAMBLE;
501
502         return __davinci_spi_claim_bus(ds, slave_plat->cs);
503 }
504
505 static int davinci_spi_release_bus(struct udevice *dev)
506 {
507         struct davinci_spi_slave *ds = dev_get_priv(dev->parent);
508
509         return __davinci_spi_release_bus(ds);
510 }
511
512 static int davinci_spi_xfer(struct udevice *dev, unsigned int bitlen,
513                             const void *dout, void *din,
514                             unsigned long flags)
515 {
516         struct dm_spi_slave_platdata *slave =
517                 dev_get_parent_platdata(dev);
518         struct udevice *bus = dev->parent;
519         struct davinci_spi_slave *ds = dev_get_priv(bus);
520
521         if (slave->cs >= ds->num_cs) {
522                 printf("Invalid SPI chipselect\n");
523                 return -EINVAL;
524         }
525         ds->cur_cs = slave->cs;
526
527         return __davinci_spi_xfer(ds, bitlen, dout, din, flags);
528 }
529
530 static const struct dm_spi_ops davinci_spi_ops = {
531         .claim_bus      = davinci_spi_claim_bus,
532         .release_bus    = davinci_spi_release_bus,
533         .xfer           = davinci_spi_xfer,
534         .set_speed      = davinci_spi_set_speed,
535         .set_mode       = davinci_spi_set_mode,
536 };
537
538 static int davinci_spi_probe(struct udevice *bus)
539 {
540         struct davinci_spi_slave *ds = dev_get_priv(bus);
541         struct davinci_spi_platdata *plat = bus->platdata;
542         ds->regs = plat->regs;
543         ds->num_cs = plat->num_cs;
544
545         return 0;
546 }
547
548 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
549 static int davinci_ofdata_to_platadata(struct udevice *bus)
550 {
551         struct davinci_spi_platdata *plat = bus->platdata;
552         fdt_addr_t addr;
553
554         addr = devfdt_get_addr(bus);
555         if (addr == FDT_ADDR_T_NONE)
556                 return -EINVAL;
557
558         plat->regs = (struct davinci_spi_regs *)addr;
559         plat->num_cs = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus), "num-cs", 4);
560
561         return 0;
562 }
563
564 static const struct udevice_id davinci_spi_ids[] = {
565         { .compatible = "ti,keystone-spi" },
566         { .compatible = "ti,dm6441-spi" },
567         { .compatible = "ti,da830-spi" },
568         { }
569 };
570 #endif
571
572 U_BOOT_DRIVER(davinci_spi) = {
573         .name = "davinci_spi",
574         .id = UCLASS_SPI,
575 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
576         .of_match = davinci_spi_ids,
577         .ofdata_to_platdata = davinci_ofdata_to_platadata,
578         .platdata_auto_alloc_size = sizeof(struct davinci_spi_platdata),
579 #endif
580         .probe = davinci_spi_probe,
581         .ops = &davinci_spi_ops,
582         .priv_auto_alloc_size = sizeof(struct davinci_spi_slave),
583 };
584 #endif