1 From d345c53b941a3d791c26f900af6e85aa1bcaf8b6 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 24/33] SPI: ralink: add Ralink SoC spi driver
6 Add the driver needed to make SPI work on Ralink SoC.
8 Signed-off-by: John Crispin <blogic@openwrt.org>
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
16 diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
17 index 92a9345..30e73ea 100644
18 --- a/drivers/spi/Kconfig
19 +++ b/drivers/spi/Kconfig
20 @@ -345,6 +345,12 @@ config SPI_RSPI
22 SPI driver for Renesas RSPI blocks.
25 + tristate "Ralink RT288x/RT305x/RT3662 SPI Controller"
26 + depends on (SOC_RT288X || SOC_RT305X || SOC_RT3883 || SOC_MT7620)
28 + This selects a driver for the Ralink RT288x/RT305x SPI Controller.
31 tristate "Samsung S3C24XX series SPI"
32 depends on ARCH_S3C24XX
33 diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
34 index 33f9c09..724e8de1 100644
35 --- a/drivers/spi/Makefile
36 +++ b/drivers/spi/Makefile
37 @@ -55,6 +55,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
47 index 0000000..b07cbaa
49 +++ b/drivers/spi/spi-ralink.c
52 + * spi-ralink.c -- Ralink RT288x/RT305x SPI controller driver
54 + * Copyright (C) 2011 Sergiy <piratfm@gmail.com>
55 + * Copyright (C) 2011-2013 Gabor Juhos <juhosg@openwrt.org>
57 + * Some parts are based on spi-orion.c:
58 + * Author: Shadi Ammouri <shadi@marvell.com>
59 + * Copyright (C) 2007-2008 Marvell Ltd.
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.
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>
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 */
80 +#define RAMIPS_SPI_STAT 0x00
81 +#define RAMIPS_SPI_CFG 0x10
82 +#define RAMIPS_SPI_CTL 0x14
83 +#define RAMIPS_SPI_DATA 0x20
85 +/* SPISTAT register bit field */
86 +#define SPISTAT_BUSY BIT(0)
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
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)
111 +#define spi_debug(args...) printk(args)
113 +#define spi_debug(args...)
117 + struct spi_master *master;
118 + void __iomem *base;
119 + unsigned int sys_freq;
120 + unsigned int speed;
124 +static inline struct ralink_spi *spidev_to_ralink_spi(struct spi_device *spi)
126 + return spi_master_get_devdata(spi->master);
129 +static inline u32 ralink_spi_read(struct ralink_spi *rs, u32 reg)
131 + return ioread32(rs->base + reg);
134 +static inline void ralink_spi_write(struct ralink_spi *rs, u32 reg, u32 val)
136 + iowrite32(val, rs->base + reg);
139 +static inline void ralink_spi_setbits(struct ralink_spi *rs, u32 reg, u32 mask)
141 + void __iomem *addr = rs->base + reg;
144 + val = ioread32(addr);
146 + iowrite32(val, addr);
149 +static inline void ralink_spi_clrbits(struct ralink_spi *rs, u32 reg, u32 mask)
151 + void __iomem *addr = rs->base + reg;
154 + val = ioread32(addr);
156 + iowrite32(val, addr);
159 +static int ralink_spi_baudrate_set(struct spi_device *spi, unsigned int speed)
161 + struct ralink_spi *rs = spidev_to_ralink_spi(spi);
166 + spi_debug("%s: speed:%u\n", __func__, speed);
169 + * the supported rates are: 2, 4, 8, ... 128
170 + * round up as we look for equal or less speed
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);
177 + /* check if requested speed is too small */
184 + /* Convert the rate to SPI clock divisor value. */
185 + prescale = ilog2(rate/2);
186 + spi_debug("%s: prescale:%u\n", __func__, prescale);
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);
196 + * called only when no transfer is active on the bus
199 +ralink_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
201 + struct ralink_spi *rs = spidev_to_ralink_spi(spi);
202 + unsigned int speed = spi->max_speed_hz;
204 + unsigned int bits_per_word = 8;
206 + if ((t != NULL) && t->speed_hz)
207 + speed = t->speed_hz;
209 + if ((t != NULL) && t->bits_per_word)
210 + bits_per_word = t->bits_per_word;
212 + if (rs->speed != speed) {
213 + spi_debug("%s: speed_hz:%u\n", __func__, speed);
214 + rc = ralink_spi_baudrate_set(spi, speed);
219 + if (bits_per_word != 8) {
220 + spi_debug("%s: bad bits_per_word: %u\n", __func__,
228 +static void ralink_spi_set_cs(struct ralink_spi *rs, int enable)
231 + ralink_spi_clrbits(rs, RAMIPS_SPI_CTL, SPICTL_SPIENA);
233 + ralink_spi_setbits(rs, RAMIPS_SPI_CTL, SPICTL_SPIENA);
236 +static inline int ralink_spi_wait_till_ready(struct ralink_spi *rs)
240 + for (i = 0; i < RALINK_SPI_WAIT_RDY_MAX_LOOP; i++) {
243 + status = ralink_spi_read(rs, RAMIPS_SPI_STAT);
244 + if ((status & SPISTAT_BUSY) == 0)
254 +ralink_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
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;
262 + spi_debug("%s(%d): %s %s\n", __func__, xfer->len,
263 + (tx != NULL) ? "tx" : " ",
264 + (rx != NULL) ? "rx" : " ");
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);
272 + dev_err(&spi->dev, "TX failed, err=%d\n", err);
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);
283 + dev_err(&spi->dev, "RX failed, err=%d\n", err);
286 + rx[count] = (u8) ralink_spi_read(rs, RAMIPS_SPI_DATA);
294 +static int ralink_spi_transfer_one_message(struct spi_master *master,
295 + struct spi_message *m)
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;
304 + /* Load defaults */
305 + status = ralink_spi_setup_transfer(spi, NULL);
309 + list_for_each_entry(t, &m->transfers, transfer_list) {
310 + unsigned int bits_per_word = spi->bits_per_word;
312 + if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
314 + "message rejected: invalid transfer data buffers\n");
319 + if (t->bits_per_word)
320 + bits_per_word = t->bits_per_word;
322 + if (bits_per_word != 8) {
324 + "message rejected: invalid transfer bits_per_word (%d bits)\n",
330 + if (t->speed_hz && t->speed_hz < (rs->sys_freq / 128)) {
332 + "message rejected: device min speed (%d Hz) exceeds required transfer speed (%d Hz)\n",
333 + (rs->sys_freq / 128), t->speed_hz);
338 + if (par_override || t->speed_hz || t->bits_per_word) {
340 + status = ralink_spi_setup_transfer(spi, t);
343 + if (!t->speed_hz && !t->bits_per_word)
348 + ralink_spi_set_cs(rs, 1);
353 + m->actual_length += ralink_spi_write_read(spi, t);
355 + if (t->delay_usecs)
356 + udelay(t->delay_usecs);
358 + if (t->cs_change) {
359 + ralink_spi_set_cs(rs, 0);
366 + ralink_spi_set_cs(rs, 0);
368 + m->status = status;
369 + spi_finalize_current_message(master);
374 +static int ralink_spi_setup(struct spi_device *spi)
376 + struct ralink_spi *rs = spidev_to_ralink_spi(spi);
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);
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);
388 + if (spi->bits_per_word != 0 && spi->bits_per_word != 8) {
390 + "setup: requested bits per words - os wrong %d bpw\n",
391 + spi->bits_per_word);
395 + if (spi->bits_per_word == 0)
396 + spi->bits_per_word = 8;
399 + * baudrate & width will be set ralink_spi_setup_transfer
404 +static void ralink_spi_reset(struct ralink_spi *rs)
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);
412 +static int ralink_spi_probe(struct platform_device *pdev)
414 + struct spi_master *master;
415 + struct ralink_spi *rs;
416 + struct resource *r;
419 + master = spi_alloc_master(&pdev->dev, sizeof(*rs));
420 + if (master == NULL) {
421 + dev_dbg(&pdev->dev, "master allocation failed\n");
425 + //if (pdev->id != -1)
426 + master->bus_num = 0;
428 + /* we support only mode 0, and no options */
429 + master->mode_bits = 0;
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;
436 + dev_set_drvdata(&pdev->dev, master);
438 + rs = spi_master_get_devdata(master);
439 + rs->master = master;
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",
446 + goto out_put_master;
449 + status = clk_enable(rs->clk);
453 + rs->sys_freq = clk_get_rate(rs->clk);
454 + spi_debug("%s: sys_freq: %u\n", __func__, rs->sys_freq);
456 + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
459 + goto out_disable_clk;
462 + rs->base = devm_request_and_ioremap(&pdev->dev, r);
464 + status = -EADDRNOTAVAIL;
465 + goto out_disable_clk;
468 + device_reset(&pdev->dev);
470 + ralink_spi_reset(rs);
472 + status = spi_register_master(master);
474 + goto out_disable_clk;
479 + clk_disable(rs->clk);
483 + spi_master_put(master);
487 +static int ralink_spi_remove(struct platform_device *pdev)
489 + struct spi_master *master;
490 + struct ralink_spi *rs;
492 + master = dev_get_drvdata(&pdev->dev);
493 + rs = spi_master_get_devdata(master);
495 + clk_disable(rs->clk);
497 + spi_unregister_master(master);
502 +MODULE_ALIAS("platform:" DRIVER_NAME);
504 +static const struct of_device_id ralink_spi_match[] = {
505 + { .compatible = "ralink,rt2880-spi" },
508 +MODULE_DEVICE_TABLE(of, ralink_spi_match);
510 +static struct platform_driver ralink_spi_driver = {
512 + .name = DRIVER_NAME,
513 + .owner = THIS_MODULE,
514 + .of_match_table = ralink_spi_match,
516 + .probe = ralink_spi_probe,
517 + .remove = ralink_spi_remove,
520 +module_platform_driver(ralink_spi_driver);
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");