Merge https://gitlab.denx.de/u-boot/custodians/u-boot-spi into next
[platform/kernel/u-boot.git] / drivers / spi / fsl_dspi.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2003
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  *
6  * Copyright (C) 2004-2009, 2015 Freescale Semiconductor, Inc.
7  * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
8  * Chao Fu (B44548@freescale.com)
9  * Haikun Wang (B53464@freescale.com)
10  */
11
12 #include <common.h>
13 #include <dm.h>
14 #include <errno.h>
15 #include <common.h>
16 #include <log.h>
17 #include <spi.h>
18 #include <malloc.h>
19 #include <asm/io.h>
20 #include <fdtdec.h>
21 #ifndef CONFIG_M68K
22 #include <asm/arch/clock.h>
23 #endif
24 #include <fsl_dspi.h>
25 #include <linux/bitops.h>
26 #include <linux/delay.h>
27
28 DECLARE_GLOBAL_DATA_PTR;
29
30 /* fsl_dspi_platdata flags */
31 #define DSPI_FLAG_REGMAP_ENDIAN_BIG     BIT(0)
32
33 /* idle data value */
34 #define DSPI_IDLE_VAL                   0x0
35
36 /* max chipselect signals number */
37 #define FSL_DSPI_MAX_CHIPSELECT         6
38
39 /* default SCK frequency, unit: HZ */
40 #define FSL_DSPI_DEFAULT_SCK_FREQ       10000000
41
42 /* tx/rx data wait timeout value, unit: us */
43 #define DSPI_TXRX_WAIT_TIMEOUT          1000000
44
45 /* CTAR register pre-configure value */
46 #define DSPI_CTAR_DEFAULT_VALUE         (DSPI_CTAR_TRSZ(7) | \
47                                         DSPI_CTAR_PCSSCK_1CLK | \
48                                         DSPI_CTAR_PASC(0) | \
49                                         DSPI_CTAR_PDT(0) | \
50                                         DSPI_CTAR_CSSCK(0) | \
51                                         DSPI_CTAR_ASC(0) | \
52                                         DSPI_CTAR_DT(0))
53
54 /* CTAR register pre-configure mask */
55 #define DSPI_CTAR_SET_MODE_MASK         (DSPI_CTAR_TRSZ(15) | \
56                                         DSPI_CTAR_PCSSCK(3) | \
57                                         DSPI_CTAR_PASC(3) | \
58                                         DSPI_CTAR_PDT(3) | \
59                                         DSPI_CTAR_CSSCK(15) | \
60                                         DSPI_CTAR_ASC(15) | \
61                                         DSPI_CTAR_DT(15))
62
63 /**
64  * struct fsl_dspi_platdata - platform data for Freescale DSPI
65  *
66  * @flags: Flags for DSPI DSPI_FLAG_...
67  * @speed_hz: Default SCK frequency
68  * @num_chipselect: Number of DSPI chipselect signals
69  * @regs_addr: Base address of DSPI registers
70  */
71 struct fsl_dspi_platdata {
72         uint flags;
73         uint speed_hz;
74         uint num_chipselect;
75         fdt_addr_t regs_addr;
76 };
77
78 /**
79  * struct fsl_dspi_priv - private data for Freescale DSPI
80  *
81  * @flags: Flags for DSPI DSPI_FLAG_...
82  * @mode: SPI mode to use for slave device (see SPI mode flags)
83  * @mcr_val: MCR register configure value
84  * @bus_clk: DSPI input clk frequency
85  * @speed_hz: Default SCK frequency
86  * @charbit: How many bits in every transfer
87  * @num_chipselect: Number of DSPI chipselect signals
88  * @ctar_val: CTAR register configure value of per chipselect slave device
89  * @regs: Point to DSPI register structure for I/O access
90  */
91 struct fsl_dspi_priv {
92         uint flags;
93         uint mode;
94         uint mcr_val;
95         uint bus_clk;
96         uint speed_hz;
97         uint charbit;
98         uint num_chipselect;
99         uint ctar_val[FSL_DSPI_MAX_CHIPSELECT];
100         struct dspi *regs;
101 };
102
103 __weak void cpu_dspi_port_conf(void)
104 {
105 }
106
107 __weak int cpu_dspi_claim_bus(uint bus, uint cs)
108 {
109         return 0;
110 }
111
112 __weak void cpu_dspi_release_bus(uint bus, uint cs)
113 {
114 }
115
116 static uint dspi_read32(uint flags, uint *addr)
117 {
118         return flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ?
119                 in_be32(addr) : in_le32(addr);
120 }
121
122 static void dspi_write32(uint flags, uint *addr, uint val)
123 {
124         flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ?
125                 out_be32(addr, val) : out_le32(addr, val);
126 }
127
128 static void dspi_halt(struct fsl_dspi_priv *priv, u8 halt)
129 {
130         uint mcr_val;
131
132         mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
133
134         if (halt)
135                 mcr_val |= DSPI_MCR_HALT;
136         else
137                 mcr_val &= ~DSPI_MCR_HALT;
138
139         dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
140 }
141
142 static void fsl_dspi_init_mcr(struct fsl_dspi_priv *priv, uint cfg_val)
143 {
144         /* halt DSPI module */
145         dspi_halt(priv, 1);
146
147         dspi_write32(priv->flags, &priv->regs->mcr, cfg_val);
148
149         /* resume module */
150         dspi_halt(priv, 0);
151
152         priv->mcr_val = cfg_val;
153 }
154
155 static void fsl_dspi_cfg_cs_active_state(struct fsl_dspi_priv *priv,
156                 uint cs, uint state)
157 {
158         uint mcr_val;
159
160         dspi_halt(priv, 1);
161
162         mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
163         if (state & SPI_CS_HIGH)
164                 /* CSx inactive state is low */
165                 mcr_val &= ~DSPI_MCR_PCSIS(cs);
166         else
167                 /* CSx inactive state is high */
168                 mcr_val |= DSPI_MCR_PCSIS(cs);
169         dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
170
171         dspi_halt(priv, 0);
172 }
173
174 static int fsl_dspi_cfg_ctar_mode(struct fsl_dspi_priv *priv,
175                 uint cs, uint mode)
176 {
177         uint bus_setup;
178
179         bus_setup = dspi_read32(priv->flags, &priv->regs->ctar[0]);
180
181         bus_setup &= ~DSPI_CTAR_SET_MODE_MASK;
182         bus_setup |= priv->ctar_val[cs];
183         bus_setup &= ~(DSPI_CTAR_CPOL | DSPI_CTAR_CPHA | DSPI_CTAR_LSBFE);
184
185         if (mode & SPI_CPOL)
186                 bus_setup |= DSPI_CTAR_CPOL;
187         if (mode & SPI_CPHA)
188                 bus_setup |= DSPI_CTAR_CPHA;
189         if (mode & SPI_LSB_FIRST)
190                 bus_setup |= DSPI_CTAR_LSBFE;
191
192         dspi_write32(priv->flags, &priv->regs->ctar[0], bus_setup);
193
194         priv->charbit =
195                 ((dspi_read32(priv->flags, &priv->regs->ctar[0]) &
196                   DSPI_CTAR_TRSZ(15)) == DSPI_CTAR_TRSZ(15)) ? 16 : 8;
197
198         return 0;
199 }
200
201 static void fsl_dspi_clr_fifo(struct fsl_dspi_priv *priv)
202 {
203         uint mcr_val;
204
205         dspi_halt(priv, 1);
206         mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
207         /* flush RX and TX FIFO */
208         mcr_val |= (DSPI_MCR_CTXF | DSPI_MCR_CRXF);
209         dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
210         dspi_halt(priv, 0);
211 }
212
213 static void dspi_tx(struct fsl_dspi_priv *priv, u32 ctrl, u16 data)
214 {
215         int timeout = DSPI_TXRX_WAIT_TIMEOUT;
216
217         /* wait for empty entries in TXFIFO or timeout */
218         while (DSPI_SR_TXCTR(dspi_read32(priv->flags, &priv->regs->sr)) >= 4 &&
219                         timeout--)
220                 udelay(1);
221
222         if (timeout >= 0)
223                 dspi_write32(priv->flags, &priv->regs->tfr, (ctrl | data));
224         else
225                 debug("dspi_tx: waiting timeout!\n");
226 }
227
228 static u16 dspi_rx(struct fsl_dspi_priv *priv)
229 {
230         int timeout = DSPI_TXRX_WAIT_TIMEOUT;
231
232         /* wait for valid entries in RXFIFO or timeout */
233         while (DSPI_SR_RXCTR(dspi_read32(priv->flags, &priv->regs->sr)) == 0 &&
234                         timeout--)
235                 udelay(1);
236
237         if (timeout >= 0)
238                 return (u16)DSPI_RFR_RXDATA(
239                                 dspi_read32(priv->flags, &priv->regs->rfr));
240         else {
241                 debug("dspi_rx: waiting timeout!\n");
242                 return (u16)(~0);
243         }
244 }
245
246 static int dspi_xfer(struct fsl_dspi_priv *priv, uint cs, unsigned int bitlen,
247                 const void *dout, void *din, unsigned long flags)
248 {
249         u16 *spi_rd16 = NULL, *spi_wr16 = NULL;
250         u8 *spi_rd = NULL, *spi_wr = NULL;
251         static u32 ctrl;
252         uint len = bitlen >> 3;
253
254         if (priv->charbit == 16) {
255                 bitlen >>= 1;
256                 spi_wr16 = (u16 *)dout;
257                 spi_rd16 = (u16 *)din;
258         } else {
259                 spi_wr = (u8 *)dout;
260                 spi_rd = (u8 *)din;
261         }
262
263         if ((flags & SPI_XFER_BEGIN) == SPI_XFER_BEGIN)
264                 ctrl |= DSPI_TFR_CONT;
265
266         ctrl = ctrl & DSPI_TFR_CONT;
267         ctrl = ctrl | DSPI_TFR_CTAS(0) | DSPI_TFR_PCS(cs);
268
269         if (len > 1) {
270                 int tmp_len = len - 1;
271                 while (tmp_len--) {
272                         if ((dout != NULL) && (din != NULL)) {
273                                 if (priv->charbit == 16) {
274                                         dspi_tx(priv, ctrl, *spi_wr16++);
275                                         *spi_rd16++ = dspi_rx(priv);
276                                 }
277                                 else {
278                                         dspi_tx(priv, ctrl, *spi_wr++);
279                                         *spi_rd++ = dspi_rx(priv);
280                                 }
281                         }
282
283                         else if (dout != NULL) {
284                                 if (priv->charbit == 16)
285                                         dspi_tx(priv, ctrl, *spi_wr16++);
286                                 else
287                                         dspi_tx(priv, ctrl, *spi_wr++);
288                                 dspi_rx(priv);
289                         }
290
291                         else if (din != NULL) {
292                                 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
293                                 if (priv->charbit == 16)
294                                         *spi_rd16++ = dspi_rx(priv);
295                                 else
296                                         *spi_rd++ = dspi_rx(priv);
297                         }
298                 }
299
300                 len = 1;        /* remaining byte */
301         }
302
303         if ((flags & SPI_XFER_END) == SPI_XFER_END)
304                 ctrl &= ~DSPI_TFR_CONT;
305
306         if (len) {
307                 if ((dout != NULL) && (din != NULL)) {
308                         if (priv->charbit == 16) {
309                                 dspi_tx(priv, ctrl, *spi_wr16++);
310                                 *spi_rd16++ = dspi_rx(priv);
311                         }
312                         else {
313                                 dspi_tx(priv, ctrl, *spi_wr++);
314                                 *spi_rd++ = dspi_rx(priv);
315                         }
316                 }
317
318                 else if (dout != NULL) {
319                         if (priv->charbit == 16)
320                                 dspi_tx(priv, ctrl, *spi_wr16);
321                         else
322                                 dspi_tx(priv, ctrl, *spi_wr);
323                         dspi_rx(priv);
324                 }
325
326                 else if (din != NULL) {
327                         dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
328                         if (priv->charbit == 16)
329                                 *spi_rd16 = dspi_rx(priv);
330                         else
331                                 *spi_rd = dspi_rx(priv);
332                 }
333         } else {
334                 /* dummy read */
335                 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
336                 dspi_rx(priv);
337         }
338
339         return 0;
340 }
341
342 /**
343  * Calculate the divide value between input clk frequency and expected SCK frequency
344  * Formula: SCK = (clkrate/pbr) x ((1+dbr)/br)
345  * Dbr: use default value 0
346  *
347  * @pbr: return Baud Rate Prescaler value
348  * @br: return Baud Rate Scaler value
349  * @speed_hz: expected SCK frequency
350  * @clkrate: input clk frequency
351  */
352 static int fsl_dspi_hz_to_spi_baud(int *pbr, int *br,
353                 int speed_hz, uint clkrate)
354 {
355         /* Valid baud rate pre-scaler values */
356         int pbr_tbl[4] = {2, 3, 5, 7};
357         int brs[16] = {2, 4, 6, 8,
358                 16, 32, 64, 128,
359                 256, 512, 1024, 2048,
360                 4096, 8192, 16384, 32768};
361         int temp, i = 0, j = 0;
362
363         temp = clkrate / speed_hz;
364
365         for (i = 0; i < ARRAY_SIZE(pbr_tbl); i++)
366                 for (j = 0; j < ARRAY_SIZE(brs); j++) {
367                         if (pbr_tbl[i] * brs[j] >= temp) {
368                                 *pbr = i;
369                                 *br = j;
370                                 return 0;
371                         }
372                 }
373
374         debug("Can not find valid baud rate,speed_hz is %d, ", speed_hz);
375         debug("clkrate is %d, we use the max prescaler value.\n", clkrate);
376
377         *pbr = ARRAY_SIZE(pbr_tbl) - 1;
378         *br =  ARRAY_SIZE(brs) - 1;
379         return -EINVAL;
380 }
381
382 static int fsl_dspi_cfg_speed(struct fsl_dspi_priv *priv, uint speed)
383 {
384         int ret;
385         uint bus_setup;
386         int best_i, best_j, bus_clk;
387
388         bus_clk = priv->bus_clk;
389
390         debug("DSPI set_speed: expected SCK speed %u, bus_clk %u.\n",
391               speed, bus_clk);
392
393         bus_setup = dspi_read32(priv->flags, &priv->regs->ctar[0]);
394         bus_setup &= ~(DSPI_CTAR_DBR | DSPI_CTAR_PBR(0x3) | DSPI_CTAR_BR(0xf));
395
396         ret = fsl_dspi_hz_to_spi_baud(&best_i, &best_j, speed, bus_clk);
397         if (ret) {
398                 speed = priv->speed_hz;
399                 debug("DSPI set_speed use default SCK rate %u.\n", speed);
400                 fsl_dspi_hz_to_spi_baud(&best_i, &best_j, speed, bus_clk);
401         }
402
403         bus_setup |= (DSPI_CTAR_PBR(best_i) | DSPI_CTAR_BR(best_j));
404         dspi_write32(priv->flags, &priv->regs->ctar[0], bus_setup);
405
406         priv->speed_hz = speed;
407
408         return 0;
409 }
410
411 static int fsl_dspi_child_pre_probe(struct udevice *dev)
412 {
413         struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
414         struct fsl_dspi_priv *priv = dev_get_priv(dev->parent);
415
416         if (slave_plat->cs >= priv->num_chipselect) {
417                 debug("DSPI invalid chipselect number %d(max %d)!\n",
418                       slave_plat->cs, priv->num_chipselect - 1);
419                 return -EINVAL;
420         }
421
422         priv->ctar_val[slave_plat->cs] = DSPI_CTAR_DEFAULT_VALUE;
423
424         debug("DSPI pre_probe slave device on CS %u, max_hz %u, mode 0x%x.\n",
425               slave_plat->cs, slave_plat->max_hz, slave_plat->mode);
426
427         return 0;
428 }
429
430 static int fsl_dspi_probe(struct udevice *bus)
431 {
432         struct fsl_dspi_platdata *plat = dev_get_platdata(bus);
433         struct fsl_dspi_priv *priv = dev_get_priv(bus);
434         struct dm_spi_bus *dm_spi_bus;
435         uint mcr_cfg_val;
436
437         dm_spi_bus = bus->uclass_priv;
438
439         /* cpu speical pin muxing configure */
440         cpu_dspi_port_conf();
441
442         /* get input clk frequency */
443         priv->regs = (struct dspi *)plat->regs_addr;
444         priv->flags = plat->flags;
445 #ifdef CONFIG_M68K
446         priv->bus_clk = gd->bus_clk;
447 #else
448         priv->bus_clk = mxc_get_clock(MXC_DSPI_CLK);
449 #endif
450         priv->num_chipselect = plat->num_chipselect;
451         priv->speed_hz = plat->speed_hz;
452         /* frame data length in bits, default 8bits */
453         priv->charbit = 8;
454
455         dm_spi_bus->max_hz = plat->speed_hz;
456
457         /* default: all CS signals inactive state is high */
458         mcr_cfg_val = DSPI_MCR_MSTR | DSPI_MCR_PCSIS_MASK |
459                 DSPI_MCR_CRXF | DSPI_MCR_CTXF;
460         fsl_dspi_init_mcr(priv, mcr_cfg_val);
461
462         debug("%s probe done, bus-num %d.\n", bus->name, bus->seq);
463
464         return 0;
465 }
466
467 static int fsl_dspi_claim_bus(struct udevice *dev)
468 {
469         uint sr_val;
470         struct fsl_dspi_priv *priv;
471         struct udevice *bus = dev->parent;
472         struct dm_spi_slave_platdata *slave_plat =
473                 dev_get_parent_platdata(dev);
474
475         priv = dev_get_priv(bus);
476
477         /* processor special preparation work */
478         cpu_dspi_claim_bus(bus->seq, slave_plat->cs);
479
480         /* configure transfer mode */
481         fsl_dspi_cfg_ctar_mode(priv, slave_plat->cs, priv->mode);
482
483         /* configure active state of CSX */
484         fsl_dspi_cfg_cs_active_state(priv, slave_plat->cs,
485                                      priv->mode);
486
487         fsl_dspi_clr_fifo(priv);
488
489         /* check module TX and RX status */
490         sr_val = dspi_read32(priv->flags, &priv->regs->sr);
491         if ((sr_val & DSPI_SR_TXRXS) != DSPI_SR_TXRXS) {
492                 debug("DSPI RX/TX not ready!\n");
493                 return -EIO;
494         }
495
496         return 0;
497 }
498
499 static int fsl_dspi_release_bus(struct udevice *dev)
500 {
501         struct udevice *bus = dev->parent;
502         struct fsl_dspi_priv *priv = dev_get_priv(bus);
503         struct dm_spi_slave_platdata *slave_plat =
504                 dev_get_parent_platdata(dev);
505
506         /* halt module */
507         dspi_halt(priv, 1);
508
509         /* processor special release work */
510         cpu_dspi_release_bus(bus->seq, slave_plat->cs);
511
512         return 0;
513 }
514
515 /**
516  * This function doesn't do anything except help with debugging
517  */
518 static int fsl_dspi_bind(struct udevice *bus)
519 {
520         debug("%s assigned req_seq %d.\n", bus->name, bus->req_seq);
521         return 0;
522 }
523
524 static int fsl_dspi_ofdata_to_platdata(struct udevice *bus)
525 {
526         fdt_addr_t addr;
527         struct fsl_dspi_platdata *plat = bus->platdata;
528         const void *blob = gd->fdt_blob;
529         int node = dev_of_offset(bus);
530
531         if (fdtdec_get_bool(blob, node, "big-endian"))
532                 plat->flags |= DSPI_FLAG_REGMAP_ENDIAN_BIG;
533
534         plat->num_chipselect =
535                 fdtdec_get_int(blob, node, "num-cs", FSL_DSPI_MAX_CHIPSELECT);
536
537         addr = devfdt_get_addr(bus);
538         if (addr == FDT_ADDR_T_NONE) {
539                 debug("DSPI: Can't get base address or size\n");
540                 return -ENOMEM;
541         }
542         plat->regs_addr = addr;
543
544         plat->speed_hz = fdtdec_get_int(blob,
545                         node, "spi-max-frequency", FSL_DSPI_DEFAULT_SCK_FREQ);
546
547         debug("DSPI: regs=%pa, max-frequency=%d, endianess=%s, num-cs=%d\n",
548               &plat->regs_addr, plat->speed_hz,
549               plat->flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ? "be" : "le",
550               plat->num_chipselect);
551
552         return 0;
553 }
554
555 static int fsl_dspi_xfer(struct udevice *dev, unsigned int bitlen,
556                 const void *dout, void *din, unsigned long flags)
557 {
558         struct fsl_dspi_priv *priv;
559         struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
560         struct udevice *bus;
561
562         bus = dev->parent;
563         priv = dev_get_priv(bus);
564
565         return dspi_xfer(priv, slave_plat->cs, bitlen, dout, din, flags);
566 }
567
568 static int fsl_dspi_set_speed(struct udevice *bus, uint speed)
569 {
570         struct fsl_dspi_priv *priv = dev_get_priv(bus);
571
572         return fsl_dspi_cfg_speed(priv, speed);
573 }
574
575 static int fsl_dspi_set_mode(struct udevice *bus, uint mode)
576 {
577         struct fsl_dspi_priv *priv = dev_get_priv(bus);
578
579         debug("DSPI set_mode: mode 0x%x.\n", mode);
580
581         /*
582          * We store some chipselect special configure value in priv->ctar_val,
583          * and we can't get the correct chipselect number here,
584          * so just store mode value.
585          * Do really configuration when claim_bus.
586          */
587         priv->mode = mode;
588
589         return 0;
590 }
591
592 static const struct dm_spi_ops fsl_dspi_ops = {
593         .claim_bus      = fsl_dspi_claim_bus,
594         .release_bus    = fsl_dspi_release_bus,
595         .xfer           = fsl_dspi_xfer,
596         .set_speed      = fsl_dspi_set_speed,
597         .set_mode       = fsl_dspi_set_mode,
598 };
599
600 static const struct udevice_id fsl_dspi_ids[] = {
601         { .compatible = "fsl,vf610-dspi" },
602         { }
603 };
604
605 U_BOOT_DRIVER(fsl_dspi) = {
606         .name   = "fsl_dspi",
607         .id     = UCLASS_SPI,
608         .of_match = fsl_dspi_ids,
609         .ops    = &fsl_dspi_ops,
610         .ofdata_to_platdata = fsl_dspi_ofdata_to_platdata,
611         .platdata_auto_alloc_size = sizeof(struct fsl_dspi_platdata),
612         .priv_auto_alloc_size = sizeof(struct fsl_dspi_priv),
613         .probe  = fsl_dspi_probe,
614         .child_pre_probe = fsl_dspi_child_pre_probe,
615         .bind = fsl_dspi_bind,
616 };