ramips: make rt3883 usb work properly
[openwrt.git] / target / linux / ramips / patches-3.9 / 0144-SPI-ralink-add-Ralink-SoC-spi-driver.patch
1 From 14620ea8ee17ba9642fbe42d73ff8ebfb42d6cf7 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Mon, 22 Apr 2013 23:16:18 +0200
4 Subject: [PATCH 144/164] SPI: ralink: add Ralink SoC spi driver
5
6 Add the driver needed to make SPI work on Ralink SoC.
7
8 Signed-off-by: John Crispin <blogic@openwrt.org>
9 ---
10  drivers/spi/Kconfig      |    6 +
11  drivers/spi/Makefile     |    1 +
12  drivers/spi/spi-ralink.c |  475 ++++++++++++++++++++++++++++++++++++++++++++++
13  3 files changed, 482 insertions(+)
14  create mode 100644 drivers/spi/spi-ralink.c
15
16 diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
17 index 2be0de9..a7de751 100644
18 --- a/drivers/spi/Kconfig
19 +++ b/drivers/spi/Kconfig
20 @@ -327,6 +327,12 @@ config SPI_RSPI
21         help
22           SPI driver for Renesas RSPI blocks.
23  
24 +config SPI_RALINK
25 +       tristate "Ralink RT288x/RT305x/RT3662 SPI Controller"
26 +       depends on (SOC_RT288X || SOC_RT305X || SOC_RT3883 || SOC_MT7620)
27 +       help
28 +         This selects a driver for the Ralink RT288x/RT305x SPI Controller.
29 +
30  config SPI_S3C24XX
31         tristate "Samsung S3C24XX series SPI"
32         depends on ARCH_S3C24XX
33 diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
34 index e53c309..a4b3c5b 100644
35 --- a/drivers/spi/Makefile
36 +++ b/drivers/spi/Makefile
37 @@ -53,6 +53,7 @@ spi-pxa2xx-platform-$(CONFIG_SPI_PXA2XX_DMA)  += spi-pxa2xx-dma.o
38  obj-$(CONFIG_SPI_PXA2XX)               += spi-pxa2xx-platform.o
39  obj-$(CONFIG_SPI_PXA2XX_PCI)           += spi-pxa2xx-pci.o
40  obj-$(CONFIG_SPI_RSPI)                 += spi-rspi.o
41 +obj-$(CONFIG_SPI_RALINK)               += spi-ralink.o
42  obj-$(CONFIG_SPI_S3C24XX)              += spi-s3c24xx-hw.o
43  spi-s3c24xx-hw-y                       := spi-s3c24xx.o
44  spi-s3c24xx-hw-$(CONFIG_SPI_S3C24XX_FIQ) += spi-s3c24xx-fiq.o
45 diff --git a/drivers/spi/spi-ralink.c b/drivers/spi/spi-ralink.c
46 new file mode 100644
47 index 0000000..b07cbaa
48 --- /dev/null
49 +++ b/drivers/spi/spi-ralink.c
50 @@ -0,0 +1,475 @@
51 +/*
52 + * spi-ralink.c -- Ralink RT288x/RT305x SPI controller driver
53 + *
54 + * Copyright (C) 2011 Sergiy <piratfm@gmail.com>
55 + * Copyright (C) 2011-2013 Gabor Juhos <juhosg@openwrt.org>
56 + *
57 + * Some parts are based on spi-orion.c:
58 + *   Author: Shadi Ammouri <shadi@marvell.com>
59 + *   Copyright (C) 2007-2008 Marvell Ltd.
60 + *
61 + * This program is free software; you can redistribute it and/or modify
62 + * it under the terms of the GNU General Public License version 2 as
63 + * published by the Free Software Foundation.
64 + */
65 +
66 +#include <linux/init.h>
67 +#include <linux/module.h>
68 +#include <linux/clk.h>
69 +#include <linux/err.h>
70 +#include <linux/delay.h>
71 +#include <linux/io.h>
72 +#include <linux/reset.h>
73 +#include <linux/spi/spi.h>
74 +#include <linux/platform_device.h>
75 +
76 +#define DRIVER_NAME                    "spi-ralink"
77 +#define RALINK_NUM_CHIPSELECTS         1 /* only one slave is supported*/
78 +#define RALINK_SPI_WAIT_RDY_MAX_LOOP   2000 /* in usec */
79 +
80 +#define RAMIPS_SPI_STAT                        0x00
81 +#define RAMIPS_SPI_CFG                 0x10
82 +#define RAMIPS_SPI_CTL                 0x14
83 +#define RAMIPS_SPI_DATA                        0x20
84 +
85 +/* SPISTAT register bit field */
86 +#define SPISTAT_BUSY                   BIT(0)
87 +
88 +/* SPICFG register bit field */
89 +#define SPICFG_LSBFIRST                        0
90 +#define SPICFG_MSBFIRST                        BIT(8)
91 +#define SPICFG_SPICLKPOL               BIT(6)
92 +#define SPICFG_RXCLKEDGE_FALLING       BIT(5)
93 +#define SPICFG_TXCLKEDGE_FALLING       BIT(4)
94 +#define SPICFG_SPICLK_PRESCALE_MASK    0x7
95 +#define SPICFG_SPICLK_DIV2             0
96 +#define SPICFG_SPICLK_DIV4             1
97 +#define SPICFG_SPICLK_DIV8             2
98 +#define SPICFG_SPICLK_DIV16            3
99 +#define SPICFG_SPICLK_DIV32            4
100 +#define SPICFG_SPICLK_DIV64            5
101 +#define SPICFG_SPICLK_DIV128           6
102 +#define SPICFG_SPICLK_DISABLE          7
103 +
104 +/* SPICTL register bit field */
105 +#define SPICTL_HIZSDO                  BIT(3)
106 +#define SPICTL_STARTWR                 BIT(2)
107 +#define SPICTL_STARTRD                 BIT(1)
108 +#define SPICTL_SPIENA                  BIT(0)
109 +
110 +#ifdef DEBUG
111 +#define spi_debug(args...) printk(args)
112 +#else
113 +#define spi_debug(args...)
114 +#endif
115 +
116 +struct ralink_spi {
117 +       struct spi_master       *master;
118 +       void __iomem            *base;
119 +       unsigned int            sys_freq;
120 +       unsigned int            speed;
121 +       struct clk              *clk;
122 +};
123 +
124 +static inline struct ralink_spi *spidev_to_ralink_spi(struct spi_device *spi)
125 +{
126 +       return spi_master_get_devdata(spi->master);
127 +}
128 +
129 +static inline u32 ralink_spi_read(struct ralink_spi *rs, u32 reg)
130 +{
131 +       return ioread32(rs->base + reg);
132 +}
133 +
134 +static inline void ralink_spi_write(struct ralink_spi *rs, u32 reg, u32 val)
135 +{
136 +       iowrite32(val, rs->base + reg);
137 +}
138 +
139 +static inline void ralink_spi_setbits(struct ralink_spi *rs, u32 reg, u32 mask)
140 +{
141 +       void __iomem *addr = rs->base + reg;
142 +       u32 val;
143 +
144 +       val = ioread32(addr);
145 +       val |= mask;
146 +       iowrite32(val, addr);
147 +}
148 +
149 +static inline void ralink_spi_clrbits(struct ralink_spi *rs, u32 reg, u32 mask)
150 +{
151 +       void __iomem *addr = rs->base + reg;
152 +       u32 val;
153 +
154 +       val = ioread32(addr);
155 +       val &= ~mask;
156 +       iowrite32(val, addr);
157 +}
158 +
159 +static int ralink_spi_baudrate_set(struct spi_device *spi, unsigned int speed)
160 +{
161 +       struct ralink_spi *rs = spidev_to_ralink_spi(spi);
162 +       u32 rate;
163 +       u32 prescale;
164 +       u32 reg;
165 +
166 +       spi_debug("%s: speed:%u\n", __func__, speed);
167 +
168 +       /*
169 +        * the supported rates are: 2, 4, 8, ... 128
170 +        * round up as we look for equal or less speed
171 +        */
172 +       rate = DIV_ROUND_UP(rs->sys_freq, speed);
173 +       spi_debug("%s: rate-1:%u\n", __func__, rate);
174 +       rate = roundup_pow_of_two(rate);
175 +       spi_debug("%s: rate-2:%u\n", __func__, rate);
176 +
177 +       /* check if requested speed is too small */
178 +       if (rate > 128)
179 +               return -EINVAL;
180 +
181 +       if (rate < 2)
182 +               rate = 2;
183 +
184 +       /* Convert the rate to SPI clock divisor value. */
185 +       prescale = ilog2(rate/2);
186 +       spi_debug("%s: prescale:%u\n", __func__, prescale);
187 +
188 +       reg = ralink_spi_read(rs, RAMIPS_SPI_CFG);
189 +       reg = ((reg & ~SPICFG_SPICLK_PRESCALE_MASK) | prescale);
190 +       ralink_spi_write(rs, RAMIPS_SPI_CFG, reg);
191 +       rs->speed = speed;
192 +       return 0;
193 +}
194 +
195 +/*
196 + * called only when no transfer is active on the bus
197 + */
198 +static int
199 +ralink_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
200 +{
201 +       struct ralink_spi *rs = spidev_to_ralink_spi(spi);
202 +       unsigned int speed = spi->max_speed_hz;
203 +       int rc;
204 +       unsigned int bits_per_word = 8;
205 +
206 +       if ((t != NULL) && t->speed_hz)
207 +               speed = t->speed_hz;
208 +
209 +       if ((t != NULL) && t->bits_per_word)
210 +               bits_per_word = t->bits_per_word;
211 +
212 +       if (rs->speed != speed) {
213 +               spi_debug("%s: speed_hz:%u\n", __func__, speed);
214 +               rc = ralink_spi_baudrate_set(spi, speed);
215 +               if (rc)
216 +                       return rc;
217 +       }
218 +
219 +       if (bits_per_word != 8) {
220 +               spi_debug("%s: bad bits_per_word: %u\n", __func__,
221 +                         bits_per_word);
222 +               return -EINVAL;
223 +       }
224 +
225 +       return 0;
226 +}
227 +
228 +static void ralink_spi_set_cs(struct ralink_spi *rs, int enable)
229 +{
230 +       if (enable)
231 +               ralink_spi_clrbits(rs, RAMIPS_SPI_CTL, SPICTL_SPIENA);
232 +       else
233 +               ralink_spi_setbits(rs, RAMIPS_SPI_CTL, SPICTL_SPIENA);
234 +}
235 +
236 +static inline int ralink_spi_wait_till_ready(struct ralink_spi *rs)
237 +{
238 +       int i;
239 +
240 +       for (i = 0; i < RALINK_SPI_WAIT_RDY_MAX_LOOP; i++) {
241 +               u32 status;
242 +
243 +               status = ralink_spi_read(rs, RAMIPS_SPI_STAT);
244 +               if ((status & SPISTAT_BUSY) == 0)
245 +                       return 0;
246 +
247 +               udelay(1);
248 +       }
249 +
250 +       return -ETIMEDOUT;
251 +}
252 +
253 +static unsigned int
254 +ralink_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
255 +{
256 +       struct ralink_spi *rs = spidev_to_ralink_spi(spi);
257 +       unsigned count = 0;
258 +       u8 *rx = xfer->rx_buf;
259 +       const u8 *tx = xfer->tx_buf;
260 +       int err;
261 +
262 +       spi_debug("%s(%d): %s %s\n", __func__, xfer->len,
263 +                 (tx != NULL) ? "tx" : "  ",
264 +                 (rx != NULL) ? "rx" : "  ");
265 +
266 +       if (tx) {
267 +               for (count = 0; count < xfer->len; count++) {
268 +                       ralink_spi_write(rs, RAMIPS_SPI_DATA, tx[count]);
269 +                       ralink_spi_setbits(rs, RAMIPS_SPI_CTL, SPICTL_STARTWR);
270 +                       err = ralink_spi_wait_till_ready(rs);
271 +                       if (err) {
272 +                               dev_err(&spi->dev, "TX failed, err=%d\n", err);
273 +                               goto out;
274 +                       }
275 +               }
276 +       }
277 +
278 +       if (rx) {
279 +               for (count = 0; count < xfer->len; count++) {
280 +                       ralink_spi_setbits(rs, RAMIPS_SPI_CTL, SPICTL_STARTRD);
281 +                       err = ralink_spi_wait_till_ready(rs);
282 +                       if (err) {
283 +                               dev_err(&spi->dev, "RX failed, err=%d\n", err);
284 +                               goto out;
285 +                       }
286 +                       rx[count] = (u8) ralink_spi_read(rs, RAMIPS_SPI_DATA);
287 +               }
288 +       }
289 +
290 +out:
291 +       return count;
292 +}
293 +
294 +static int ralink_spi_transfer_one_message(struct spi_master *master,
295 +                                          struct spi_message *m)
296 +{
297 +       struct ralink_spi *rs = spi_master_get_devdata(master);
298 +       struct spi_device *spi = m->spi;
299 +       struct spi_transfer *t = NULL;
300 +       int par_override = 0;
301 +       int status = 0;
302 +       int cs_active = 0;
303 +
304 +       /* Load defaults */
305 +       status = ralink_spi_setup_transfer(spi, NULL);
306 +       if (status < 0)
307 +               goto msg_done;
308 +
309 +       list_for_each_entry(t, &m->transfers, transfer_list) {
310 +               unsigned int bits_per_word = spi->bits_per_word;
311 +
312 +               if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
313 +                       dev_err(&spi->dev,
314 +                               "message rejected: invalid transfer data buffers\n");
315 +                       status = -EIO;
316 +                       goto msg_done;
317 +               }
318 +
319 +               if (t->bits_per_word)
320 +                       bits_per_word = t->bits_per_word;
321 +
322 +               if (bits_per_word != 8) {
323 +                       dev_err(&spi->dev,
324 +                               "message rejected: invalid transfer bits_per_word (%d bits)\n",
325 +                               bits_per_word);
326 +                       status = -EIO;
327 +                       goto msg_done;
328 +               }
329 +
330 +               if (t->speed_hz && t->speed_hz < (rs->sys_freq / 128)) {
331 +                       dev_err(&spi->dev,
332 +                               "message rejected: device min speed (%d Hz) exceeds required transfer speed (%d Hz)\n",
333 +                               (rs->sys_freq / 128), t->speed_hz);
334 +                       status = -EIO;
335 +                       goto msg_done;
336 +               }
337 +
338 +               if (par_override || t->speed_hz || t->bits_per_word) {
339 +                       par_override = 1;
340 +                       status = ralink_spi_setup_transfer(spi, t);
341 +                       if (status < 0)
342 +                               goto msg_done;
343 +                       if (!t->speed_hz && !t->bits_per_word)
344 +                               par_override = 0;
345 +               }
346 +
347 +               if (!cs_active) {
348 +                       ralink_spi_set_cs(rs, 1);
349 +                       cs_active = 1;
350 +               }
351 +
352 +               if (t->len)
353 +                       m->actual_length += ralink_spi_write_read(spi, t);
354 +
355 +               if (t->delay_usecs)
356 +                       udelay(t->delay_usecs);
357 +
358 +               if (t->cs_change) {
359 +                       ralink_spi_set_cs(rs, 0);
360 +                       cs_active = 0;
361 +               }
362 +       }
363 +
364 +msg_done:
365 +       if (cs_active)
366 +               ralink_spi_set_cs(rs, 0);
367 +
368 +       m->status = status;
369 +       spi_finalize_current_message(master);
370 +
371 +       return 0;
372 +}
373 +
374 +static int ralink_spi_setup(struct spi_device *spi)
375 +{
376 +       struct ralink_spi *rs = spidev_to_ralink_spi(spi);
377 +
378 +       if ((spi->max_speed_hz == 0) ||
379 +           (spi->max_speed_hz > (rs->sys_freq / 2)))
380 +               spi->max_speed_hz = (rs->sys_freq / 2);
381 +
382 +       if (spi->max_speed_hz < (rs->sys_freq / 128)) {
383 +               dev_err(&spi->dev, "setup: requested speed is too low %d Hz\n",
384 +                       spi->max_speed_hz);
385 +               return -EINVAL;
386 +       }
387 +
388 +       if (spi->bits_per_word != 0 && spi->bits_per_word != 8) {
389 +               dev_err(&spi->dev,
390 +                       "setup: requested bits per words - os wrong %d bpw\n",
391 +                       spi->bits_per_word);
392 +               return -EINVAL;
393 +       }
394 +
395 +       if (spi->bits_per_word == 0)
396 +               spi->bits_per_word = 8;
397 +
398 +       /*
399 +        * baudrate & width will be set ralink_spi_setup_transfer
400 +        */
401 +       return 0;
402 +}
403 +
404 +static void ralink_spi_reset(struct ralink_spi *rs)
405 +{
406 +       ralink_spi_write(rs, RAMIPS_SPI_CFG,
407 +                        SPICFG_MSBFIRST | SPICFG_TXCLKEDGE_FALLING |
408 +                        SPICFG_SPICLK_DIV16 | SPICFG_SPICLKPOL);
409 +       ralink_spi_write(rs, RAMIPS_SPI_CTL, SPICTL_HIZSDO | SPICTL_SPIENA);
410 +}
411 +
412 +static int ralink_spi_probe(struct platform_device *pdev)
413 +{
414 +       struct spi_master *master;
415 +       struct ralink_spi *rs;
416 +       struct resource *r;
417 +       int status = 0;
418 +
419 +       master = spi_alloc_master(&pdev->dev, sizeof(*rs));
420 +       if (master == NULL) {
421 +               dev_dbg(&pdev->dev, "master allocation failed\n");
422 +               return -ENOMEM;
423 +       }
424 +
425 +       //if (pdev->id != -1)
426 +               master->bus_num = 0;
427 +
428 +       /* we support only mode 0, and no options */
429 +       master->mode_bits = 0;
430 +
431 +       master->setup = ralink_spi_setup;
432 +       master->transfer_one_message = ralink_spi_transfer_one_message;
433 +       master->num_chipselect = RALINK_NUM_CHIPSELECTS;
434 +       master->dev.of_node = pdev->dev.of_node;
435 +
436 +       dev_set_drvdata(&pdev->dev, master);
437 +
438 +       rs = spi_master_get_devdata(master);
439 +       rs->master = master;
440 +
441 +       rs->clk = clk_get(&pdev->dev, NULL);
442 +       if (IS_ERR(rs->clk)) {
443 +               status = PTR_ERR(rs->clk);
444 +               dev_err(&pdev->dev, "unable to get SYS clock, err=%d\n",
445 +                       status);
446 +               goto out_put_master;
447 +       }
448 +
449 +       status = clk_enable(rs->clk);
450 +       if (status)
451 +               goto out_put_clk;
452 +
453 +       rs->sys_freq = clk_get_rate(rs->clk);
454 +       spi_debug("%s: sys_freq: %u\n", __func__, rs->sys_freq);
455 +
456 +       r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
457 +       if (r == NULL) {
458 +               status = -ENODEV;
459 +               goto out_disable_clk;
460 +       }
461 +
462 +       rs->base = devm_request_and_ioremap(&pdev->dev, r);
463 +       if (!rs->base) {
464 +               status = -EADDRNOTAVAIL;
465 +               goto out_disable_clk;
466 +       }
467 +
468 +       device_reset(&pdev->dev);
469 +
470 +       ralink_spi_reset(rs);
471 +
472 +       status = spi_register_master(master);
473 +       if (status)
474 +               goto out_disable_clk;
475 +
476 +       return 0;
477 +
478 +out_disable_clk:
479 +       clk_disable(rs->clk);
480 +out_put_clk:
481 +       clk_put(rs->clk);
482 +out_put_master:
483 +       spi_master_put(master);
484 +       return status;
485 +}
486 +
487 +static int ralink_spi_remove(struct platform_device *pdev)
488 +{
489 +       struct spi_master *master;
490 +       struct ralink_spi *rs;
491 +
492 +       master = dev_get_drvdata(&pdev->dev);
493 +       rs = spi_master_get_devdata(master);
494 +
495 +       clk_disable(rs->clk);
496 +       clk_put(rs->clk);
497 +       spi_unregister_master(master);
498 +
499 +       return 0;
500 +}
501 +
502 +MODULE_ALIAS("platform:" DRIVER_NAME);
503 +
504 +static const struct of_device_id ralink_spi_match[] = {
505 +       { .compatible = "ralink,rt2880-spi" },
506 +       {},
507 +};
508 +MODULE_DEVICE_TABLE(of, ralink_spi_match);
509 +
510 +static struct platform_driver ralink_spi_driver = {
511 +       .driver = {
512 +               .name = DRIVER_NAME,
513 +               .owner = THIS_MODULE,
514 +               .of_match_table = ralink_spi_match,
515 +       },
516 +       .probe = ralink_spi_probe,
517 +       .remove = ralink_spi_remove,
518 +};
519 +
520 +module_platform_driver(ralink_spi_driver);
521 +
522 +MODULE_DESCRIPTION("Ralink SPI driver");
523 +MODULE_AUTHOR("Sergiy <piratfm@gmail.com>");
524 +MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
525 +MODULE_LICENSE("GPL");
526 -- 
527 1.7.10.4
528