]> git.sur5r.net Git - u-boot/blob - drivers/spi/designware_spi.c
DW SPI: add option to use external gpio for chip select
[u-boot] / drivers / spi / designware_spi.c
1 /*
2  * Designware master SPI core controller driver
3  *
4  * Copyright (C) 2014 Stefan Roese <sr@denx.de>
5  *
6  * Very loosely based on the Linux driver:
7  * drivers/spi/spi-dw.c, which is:
8  * Copyright (c) 2009, Intel Corporation.
9  *
10  * SPDX-License-Identifier:     GPL-2.0
11  */
12
13 #include <asm-generic/gpio.h>
14 #include <common.h>
15 #include <clk.h>
16 #include <dm.h>
17 #include <errno.h>
18 #include <malloc.h>
19 #include <spi.h>
20 #include <fdtdec.h>
21 #include <linux/compat.h>
22 #include <linux/iopoll.h>
23 #include <asm/io.h>
24
25 DECLARE_GLOBAL_DATA_PTR;
26
27 /* Register offsets */
28 #define DW_SPI_CTRL0                    0x00
29 #define DW_SPI_CTRL1                    0x04
30 #define DW_SPI_SSIENR                   0x08
31 #define DW_SPI_MWCR                     0x0c
32 #define DW_SPI_SER                      0x10
33 #define DW_SPI_BAUDR                    0x14
34 #define DW_SPI_TXFLTR                   0x18
35 #define DW_SPI_RXFLTR                   0x1c
36 #define DW_SPI_TXFLR                    0x20
37 #define DW_SPI_RXFLR                    0x24
38 #define DW_SPI_SR                       0x28
39 #define DW_SPI_IMR                      0x2c
40 #define DW_SPI_ISR                      0x30
41 #define DW_SPI_RISR                     0x34
42 #define DW_SPI_TXOICR                   0x38
43 #define DW_SPI_RXOICR                   0x3c
44 #define DW_SPI_RXUICR                   0x40
45 #define DW_SPI_MSTICR                   0x44
46 #define DW_SPI_ICR                      0x48
47 #define DW_SPI_DMACR                    0x4c
48 #define DW_SPI_DMATDLR                  0x50
49 #define DW_SPI_DMARDLR                  0x54
50 #define DW_SPI_IDR                      0x58
51 #define DW_SPI_VERSION                  0x5c
52 #define DW_SPI_DR                       0x60
53
54 /* Bit fields in CTRLR0 */
55 #define SPI_DFS_OFFSET                  0
56
57 #define SPI_FRF_OFFSET                  4
58 #define SPI_FRF_SPI                     0x0
59 #define SPI_FRF_SSP                     0x1
60 #define SPI_FRF_MICROWIRE               0x2
61 #define SPI_FRF_RESV                    0x3
62
63 #define SPI_MODE_OFFSET                 6
64 #define SPI_SCPH_OFFSET                 6
65 #define SPI_SCOL_OFFSET                 7
66
67 #define SPI_TMOD_OFFSET                 8
68 #define SPI_TMOD_MASK                   (0x3 << SPI_TMOD_OFFSET)
69 #define SPI_TMOD_TR                     0x0             /* xmit & recv */
70 #define SPI_TMOD_TO                     0x1             /* xmit only */
71 #define SPI_TMOD_RO                     0x2             /* recv only */
72 #define SPI_TMOD_EPROMREAD              0x3             /* eeprom read mode */
73
74 #define SPI_SLVOE_OFFSET                10
75 #define SPI_SRL_OFFSET                  11
76 #define SPI_CFS_OFFSET                  12
77
78 /* Bit fields in SR, 7 bits */
79 #define SR_MASK                         GENMASK(6, 0)   /* cover 7 bits */
80 #define SR_BUSY                         BIT(0)
81 #define SR_TF_NOT_FULL                  BIT(1)
82 #define SR_TF_EMPT                      BIT(2)
83 #define SR_RF_NOT_EMPT                  BIT(3)
84 #define SR_RF_FULL                      BIT(4)
85 #define SR_TX_ERR                       BIT(5)
86 #define SR_DCOL                         BIT(6)
87
88 #define RX_TIMEOUT                      1000            /* timeout in ms */
89
90 struct dw_spi_platdata {
91         s32 frequency;          /* Default clock frequency, -1 for none */
92         void __iomem *regs;
93 };
94
95 struct dw_spi_priv {
96         void __iomem *regs;
97         unsigned int freq;              /* Default frequency */
98         unsigned int mode;
99         struct clk clk;
100         unsigned long bus_clk_rate;
101
102         struct gpio_desc cs_gpio;       /* External chip-select gpio */
103
104         int bits_per_word;
105         u8 cs;                  /* chip select pin */
106         u8 tmode;               /* TR/TO/RO/EEPROM */
107         u8 type;                /* SPI/SSP/MicroWire */
108         int len;
109
110         u32 fifo_len;           /* depth of the FIFO buffer */
111         void *tx;
112         void *tx_end;
113         void *rx;
114         void *rx_end;
115 };
116
117 static inline u32 dw_readl(struct dw_spi_priv *priv, u32 offset)
118 {
119         return __raw_readl(priv->regs + offset);
120 }
121
122 static inline void dw_writel(struct dw_spi_priv *priv, u32 offset, u32 val)
123 {
124         __raw_writel(val, priv->regs + offset);
125 }
126
127 static inline u16 dw_readw(struct dw_spi_priv *priv, u32 offset)
128 {
129         return __raw_readw(priv->regs + offset);
130 }
131
132 static inline void dw_writew(struct dw_spi_priv *priv, u32 offset, u16 val)
133 {
134         __raw_writew(val, priv->regs + offset);
135 }
136
137 static int request_gpio_cs(struct udevice *bus)
138 {
139 #if defined(CONFIG_DM_GPIO) && !defined(CONFIG_SPL_BUILD)
140         struct dw_spi_priv *priv = dev_get_priv(bus);
141         int ret;
142
143         /* External chip select gpio line is optional */
144         ret = gpio_request_by_name(bus, "cs-gpio", 0, &priv->cs_gpio, 0);
145         if (ret == -ENOENT)
146                 return 0;
147
148         if (ret < 0) {
149                 printf("Error: %d: Can't get %s gpio!\n", ret, bus->name);
150                 return ret;
151         }
152
153         if (dm_gpio_is_valid(&priv->cs_gpio)) {
154                 dm_gpio_set_dir_flags(&priv->cs_gpio,
155                                       GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
156         }
157
158         debug("%s: used external gpio for CS management\n", __func__);
159 #endif
160         return 0;
161 }
162
163 static int dw_spi_ofdata_to_platdata(struct udevice *bus)
164 {
165         struct dw_spi_platdata *plat = bus->platdata;
166         const void *blob = gd->fdt_blob;
167         int node = dev_of_offset(bus);
168
169         plat->regs = (struct dw_spi *)devfdt_get_addr(bus);
170
171         /* Use 500KHz as a suitable default */
172         plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
173                                         500000);
174         debug("%s: regs=%p max-frequency=%d\n", __func__, plat->regs,
175               plat->frequency);
176
177         return request_gpio_cs(bus);
178 }
179
180 static inline void spi_enable_chip(struct dw_spi_priv *priv, int enable)
181 {
182         dw_writel(priv, DW_SPI_SSIENR, (enable ? 1 : 0));
183 }
184
185 /* Restart the controller, disable all interrupts, clean rx fifo */
186 static void spi_hw_init(struct dw_spi_priv *priv)
187 {
188         spi_enable_chip(priv, 0);
189         dw_writel(priv, DW_SPI_IMR, 0xff);
190         spi_enable_chip(priv, 1);
191
192         /*
193          * Try to detect the FIFO depth if not set by interface driver,
194          * the depth could be from 2 to 256 from HW spec
195          */
196         if (!priv->fifo_len) {
197                 u32 fifo;
198
199                 for (fifo = 1; fifo < 256; fifo++) {
200                         dw_writew(priv, DW_SPI_TXFLTR, fifo);
201                         if (fifo != dw_readw(priv, DW_SPI_TXFLTR))
202                                 break;
203                 }
204
205                 priv->fifo_len = (fifo == 1) ? 0 : fifo;
206                 dw_writew(priv, DW_SPI_TXFLTR, 0);
207         }
208         debug("%s: fifo_len=%d\n", __func__, priv->fifo_len);
209 }
210
211 /*
212  * We define dw_spi_get_clk function as 'weak' as some targets
213  * (like SOCFPGA_GEN5 and SOCFPGA_ARRIA10) don't use standard clock API
214  * and implement dw_spi_get_clk their own way in their clock manager.
215  */
216 __weak int dw_spi_get_clk(struct udevice *bus, ulong *rate)
217 {
218         struct dw_spi_priv *priv = dev_get_priv(bus);
219         int ret;
220
221         ret = clk_get_by_index(bus, 0, &priv->clk);
222         if (ret)
223                 return ret;
224
225         ret = clk_enable(&priv->clk);
226         if (ret && ret != -ENOSYS && ret != -ENOTSUPP)
227                 return ret;
228
229         *rate = clk_get_rate(&priv->clk);
230         if (!*rate)
231                 goto err_rate;
232
233         debug("%s: get spi controller clk via device tree: %lu Hz\n",
234               __func__, *rate);
235
236         return 0;
237
238 err_rate:
239         clk_disable(&priv->clk);
240         clk_free(&priv->clk);
241
242         return -EINVAL;
243 }
244
245 static int dw_spi_probe(struct udevice *bus)
246 {
247         struct dw_spi_platdata *plat = dev_get_platdata(bus);
248         struct dw_spi_priv *priv = dev_get_priv(bus);
249         int ret;
250
251         priv->regs = plat->regs;
252         priv->freq = plat->frequency;
253
254         ret = dw_spi_get_clk(bus, &priv->bus_clk_rate);
255         if (ret)
256                 return ret;
257
258         /* Currently only bits_per_word == 8 supported */
259         priv->bits_per_word = 8;
260
261         priv->tmode = 0; /* Tx & Rx */
262
263         /* Basic HW init */
264         spi_hw_init(priv);
265
266         return 0;
267 }
268
269 /* Return the max entries we can fill into tx fifo */
270 static inline u32 tx_max(struct dw_spi_priv *priv)
271 {
272         u32 tx_left, tx_room, rxtx_gap;
273
274         tx_left = (priv->tx_end - priv->tx) / (priv->bits_per_word >> 3);
275         tx_room = priv->fifo_len - dw_readw(priv, DW_SPI_TXFLR);
276
277         /*
278          * Another concern is about the tx/rx mismatch, we
279          * thought about using (priv->fifo_len - rxflr - txflr) as
280          * one maximum value for tx, but it doesn't cover the
281          * data which is out of tx/rx fifo and inside the
282          * shift registers. So a control from sw point of
283          * view is taken.
284          */
285         rxtx_gap = ((priv->rx_end - priv->rx) - (priv->tx_end - priv->tx)) /
286                 (priv->bits_per_word >> 3);
287
288         return min3(tx_left, tx_room, (u32)(priv->fifo_len - rxtx_gap));
289 }
290
291 /* Return the max entries we should read out of rx fifo */
292 static inline u32 rx_max(struct dw_spi_priv *priv)
293 {
294         u32 rx_left = (priv->rx_end - priv->rx) / (priv->bits_per_word >> 3);
295
296         return min_t(u32, rx_left, dw_readw(priv, DW_SPI_RXFLR));
297 }
298
299 static void dw_writer(struct dw_spi_priv *priv)
300 {
301         u32 max = tx_max(priv);
302         u16 txw = 0;
303
304         while (max--) {
305                 /* Set the tx word if the transfer's original "tx" is not null */
306                 if (priv->tx_end - priv->len) {
307                         if (priv->bits_per_word == 8)
308                                 txw = *(u8 *)(priv->tx);
309                         else
310                                 txw = *(u16 *)(priv->tx);
311                 }
312                 dw_writew(priv, DW_SPI_DR, txw);
313                 debug("%s: tx=0x%02x\n", __func__, txw);
314                 priv->tx += priv->bits_per_word >> 3;
315         }
316 }
317
318 static void dw_reader(struct dw_spi_priv *priv)
319 {
320         u32 max = rx_max(priv);
321         u16 rxw;
322
323         while (max--) {
324                 rxw = dw_readw(priv, DW_SPI_DR);
325                 debug("%s: rx=0x%02x\n", __func__, rxw);
326
327                 /* Care about rx if the transfer's original "rx" is not null */
328                 if (priv->rx_end - priv->len) {
329                         if (priv->bits_per_word == 8)
330                                 *(u8 *)(priv->rx) = rxw;
331                         else
332                                 *(u16 *)(priv->rx) = rxw;
333                 }
334                 priv->rx += priv->bits_per_word >> 3;
335         }
336 }
337
338 static int poll_transfer(struct dw_spi_priv *priv)
339 {
340         do {
341                 dw_writer(priv);
342                 dw_reader(priv);
343         } while (priv->rx_end > priv->rx);
344
345         return 0;
346 }
347
348 static void external_cs_manage(struct udevice *dev, bool on)
349 {
350 #if defined(CONFIG_DM_GPIO) && !defined(CONFIG_SPL_BUILD)
351         struct dw_spi_priv *priv = dev_get_priv(dev->parent);
352
353         if (!dm_gpio_is_valid(&priv->cs_gpio))
354                 return;
355
356         dm_gpio_set_value(&priv->cs_gpio, on ? 1 : 0);
357 #endif
358 }
359
360 static int dw_spi_xfer(struct udevice *dev, unsigned int bitlen,
361                        const void *dout, void *din, unsigned long flags)
362 {
363         struct udevice *bus = dev->parent;
364         struct dw_spi_priv *priv = dev_get_priv(bus);
365         const u8 *tx = dout;
366         u8 *rx = din;
367         int ret = 0;
368         u32 cr0 = 0;
369         u32 val;
370         u32 cs;
371
372         /* spi core configured to do 8 bit transfers */
373         if (bitlen % 8) {
374                 debug("Non byte aligned SPI transfer.\n");
375                 return -1;
376         }
377
378         /* Start the transaction if necessary. */
379         if (flags & SPI_XFER_BEGIN)
380                 external_cs_manage(dev, false);
381
382         cr0 = (priv->bits_per_word - 1) | (priv->type << SPI_FRF_OFFSET) |
383                 (priv->mode << SPI_MODE_OFFSET) |
384                 (priv->tmode << SPI_TMOD_OFFSET);
385
386         if (rx && tx)
387                 priv->tmode = SPI_TMOD_TR;
388         else if (rx)
389                 priv->tmode = SPI_TMOD_RO;
390         else
391                 /*
392                  * In transmit only mode (SPI_TMOD_TO) input FIFO never gets
393                  * any data which breaks our logic in poll_transfer() above.
394                  */
395                 priv->tmode = SPI_TMOD_TR;
396
397         cr0 &= ~SPI_TMOD_MASK;
398         cr0 |= (priv->tmode << SPI_TMOD_OFFSET);
399
400         priv->len = bitlen >> 3;
401         debug("%s: rx=%p tx=%p len=%d [bytes]\n", __func__, rx, tx, priv->len);
402
403         priv->tx = (void *)tx;
404         priv->tx_end = priv->tx + priv->len;
405         priv->rx = rx;
406         priv->rx_end = priv->rx + priv->len;
407
408         /* Disable controller before writing control registers */
409         spi_enable_chip(priv, 0);
410
411         debug("%s: cr0=%08x\n", __func__, cr0);
412         /* Reprogram cr0 only if changed */
413         if (dw_readw(priv, DW_SPI_CTRL0) != cr0)
414                 dw_writew(priv, DW_SPI_CTRL0, cr0);
415
416         /*
417          * Configure the desired SS (slave select 0...3) in the controller
418          * The DW SPI controller will activate and deactivate this CS
419          * automatically. So no cs_activate() etc is needed in this driver.
420          */
421         cs = spi_chip_select(dev);
422         dw_writel(priv, DW_SPI_SER, 1 << cs);
423
424         /* Enable controller after writing control registers */
425         spi_enable_chip(priv, 1);
426
427         /* Start transfer in a polling loop */
428         ret = poll_transfer(priv);
429
430         /*
431          * Wait for current transmit operation to complete.
432          * Otherwise if some data still exists in Tx FIFO it can be
433          * silently flushed, i.e. dropped on disabling of the controller,
434          * which happens when writing 0 to DW_SPI_SSIENR which happens
435          * in the beginning of new transfer.
436          */
437         if (readl_poll_timeout(priv->regs + DW_SPI_SR, val,
438                                !(val & SR_TF_EMPT) || (val & SR_BUSY),
439                                RX_TIMEOUT * 1000)) {
440                 ret = -ETIMEDOUT;
441         }
442
443         /* Stop the transaction if necessary */
444         if (flags & SPI_XFER_END)
445                 external_cs_manage(dev, true);
446
447         return ret;
448 }
449
450 static int dw_spi_set_speed(struct udevice *bus, uint speed)
451 {
452         struct dw_spi_platdata *plat = bus->platdata;
453         struct dw_spi_priv *priv = dev_get_priv(bus);
454         u16 clk_div;
455
456         if (speed > plat->frequency)
457                 speed = plat->frequency;
458
459         /* Disable controller before writing control registers */
460         spi_enable_chip(priv, 0);
461
462         /* clk_div doesn't support odd number */
463         clk_div = priv->bus_clk_rate / speed;
464         clk_div = (clk_div + 1) & 0xfffe;
465         dw_writel(priv, DW_SPI_BAUDR, clk_div);
466
467         /* Enable controller after writing control registers */
468         spi_enable_chip(priv, 1);
469
470         priv->freq = speed;
471         debug("%s: regs=%p speed=%d clk_div=%d\n", __func__, priv->regs,
472               priv->freq, clk_div);
473
474         return 0;
475 }
476
477 static int dw_spi_set_mode(struct udevice *bus, uint mode)
478 {
479         struct dw_spi_priv *priv = dev_get_priv(bus);
480
481         /*
482          * Can't set mode yet. Since this depends on if rx, tx, or
483          * rx & tx is requested. So we have to defer this to the
484          * real transfer function.
485          */
486         priv->mode = mode;
487         debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
488
489         return 0;
490 }
491
492 static const struct dm_spi_ops dw_spi_ops = {
493         .xfer           = dw_spi_xfer,
494         .set_speed      = dw_spi_set_speed,
495         .set_mode       = dw_spi_set_mode,
496         /*
497          * cs_info is not needed, since we require all chip selects to be
498          * in the device tree explicitly
499          */
500 };
501
502 static const struct udevice_id dw_spi_ids[] = {
503         { .compatible = "snps,dw-apb-ssi" },
504         { }
505 };
506
507 U_BOOT_DRIVER(dw_spi) = {
508         .name = "dw_spi",
509         .id = UCLASS_SPI,
510         .of_match = dw_spi_ids,
511         .ops = &dw_spi_ops,
512         .ofdata_to_platdata = dw_spi_ofdata_to_platdata,
513         .platdata_auto_alloc_size = sizeof(struct dw_spi_platdata),
514         .priv_auto_alloc_size = sizeof(struct dw_spi_priv),
515         .probe = dw_spi_probe,
516 };