1 --- a/drivers/spi/Kconfig
2 +++ b/drivers/spi/Kconfig
3 @@ -439,6 +439,12 @@ config SPI_RT2880
5 This selects a driver for the Ralink RT288x/RT305x SPI Controller.
8 + tristate "MediaTek MT7621 SPI Controller"
11 + This selects a driver for the MediaTek MT7621 SPI Controller.
14 tristate "Samsung S3C24XX series SPI"
15 depends on ARCH_S3C24XX
16 --- a/drivers/spi/Makefile
17 +++ b/drivers/spi/Makefile
18 @@ -46,6 +46,7 @@ obj-$(CONFIG_SPI_LM70_LLP) += spi-lm70l
19 obj-$(CONFIG_SPI_MPC512x_PSC) += spi-mpc512x-psc.o
20 obj-$(CONFIG_SPI_MPC52xx_PSC) += spi-mpc52xx-psc.o
21 obj-$(CONFIG_SPI_MPC52xx) += spi-mpc52xx.o
22 +obj-$(CONFIG_SPI_MT7621) += spi-mt7621.o
23 obj-$(CONFIG_SPI_MXS) += spi-mxs.o
24 obj-$(CONFIG_SPI_NUC900) += spi-nuc900.o
25 obj-$(CONFIG_SPI_OC_TINY) += spi-oc-tiny.o
27 +++ b/drivers/spi/spi-mt7621.c
30 + * spi-mt7621.c -- MediaTek MT7621 SPI controller driver
32 + * Copyright (C) 2011 Sergiy <piratfm@gmail.com>
33 + * Copyright (C) 2011-2013 Gabor Juhos <juhosg@openwrt.org>
34 + * Copyright (C) 2014-2015 Felix Fietkau <nbd@openwrt.org>
36 + * Some parts are based on spi-orion.c:
37 + * Author: Shadi Ammouri <shadi@marvell.com>
38 + * Copyright (C) 2007-2008 Marvell Ltd.
40 + * This program is free software; you can redistribute it and/or modify
41 + * it under the terms of the GNU General Public License version 2 as
42 + * published by the Free Software Foundation.
45 +#include <linux/init.h>
46 +#include <linux/module.h>
47 +#include <linux/clk.h>
48 +#include <linux/err.h>
49 +#include <linux/delay.h>
50 +#include <linux/io.h>
51 +#include <linux/reset.h>
52 +#include <linux/spi/spi.h>
53 +#include <linux/of_device.h>
54 +#include <linux/platform_device.h>
55 +#include <linux/swab.h>
57 +#include <ralink_regs.h>
59 +#define SPI_BPW_MASK(bits) BIT((bits) - 1)
61 +#define DRIVER_NAME "spi-mt7621"
63 +#define RALINK_SPI_WAIT_MAX_LOOP 2000
65 +/* SPISTAT register bit field */
66 +#define SPISTAT_BUSY BIT(0)
68 +#define MT7621_SPI_TRANS 0x00
69 +#define SPITRANS_BUSY BIT(16)
71 +#define MT7621_SPI_OPCODE 0x04
72 +#define MT7621_SPI_DATA0 0x08
73 +#define MT7621_SPI_DATA4 0x18
74 +#define SPI_CTL_TX_RX_CNT_MASK 0xff
75 +#define SPI_CTL_START BIT(8)
77 +#define MT7621_SPI_POLAR 0x38
78 +#define MT7621_SPI_MASTER 0x28
79 +#define MT7621_SPI_MOREBUF 0x2c
80 +#define MT7621_SPI_SPACE 0x3c
82 +#define MT7621_CPHA BIT(5)
83 +#define MT7621_CPOL BIT(4)
84 +#define MT7621_LSB_FIRST BIT(3)
86 +#define RT2880_SPI_MODE_BITS (SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_CS_HIGH)
91 + struct spi_master *master;
93 + unsigned int sys_freq;
98 + struct mt7621_spi_ops *ops;
101 +static inline struct mt7621_spi *spidev_to_mt7621_spi(struct spi_device *spi)
103 + return spi_master_get_devdata(spi->master);
106 +static inline u32 mt7621_spi_read(struct mt7621_spi *rs, u32 reg)
108 + return ioread32(rs->base + reg);
111 +static inline void mt7621_spi_write(struct mt7621_spi *rs, u32 reg, u32 val)
113 + iowrite32(val, rs->base + reg);
116 +static void mt7621_spi_reset(struct mt7621_spi *rs, int duplex)
118 + u32 master = mt7621_spi_read(rs, MT7621_SPI_MASTER);
125 + master &= ~(1 << 10);
127 + mt7621_spi_write(rs, MT7621_SPI_MASTER, master);
130 +static void mt7621_spi_set_cs(struct spi_device *spi, int enable)
132 + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
133 + int cs = spi->chip_select;
136 + mt7621_spi_reset(rs, cs);
139 + mt7621_spi_write(rs, MT7621_SPI_POLAR, polar);
142 +static int mt7621_spi_prepare(struct spi_device *spi, unsigned int speed)
144 + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
148 + dev_dbg(&spi->dev, "speed:%u\n", speed);
150 + rate = DIV_ROUND_UP(rs->sys_freq, speed);
151 + dev_dbg(&spi->dev, "rate-1:%u\n", rate);
159 + reg = mt7621_spi_read(rs, MT7621_SPI_MASTER);
160 + reg &= ~(0xfff << 16);
161 + reg |= (rate - 2) << 16;
164 + reg &= ~MT7621_LSB_FIRST;
165 + if (spi->mode & SPI_LSB_FIRST)
166 + reg |= MT7621_LSB_FIRST;
168 + reg &= ~(MT7621_CPHA | MT7621_CPOL);
169 + switch(spi->mode & (SPI_CPOL | SPI_CPHA)) {
173 + reg |= MT7621_CPHA;
176 + reg |= MT7621_CPOL;
179 + reg |= MT7621_CPOL | MT7621_CPHA;
182 + mt7621_spi_write(rs, MT7621_SPI_MASTER, reg);
187 +static inline int mt7621_spi_wait_till_ready(struct spi_device *spi)
189 + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
192 + for (i = 0; i < RALINK_SPI_WAIT_MAX_LOOP; i++) {
195 + status = mt7621_spi_read(rs, MT7621_SPI_TRANS);
196 + if ((status & SPITRANS_BUSY) == 0) {
206 +static int mt7621_spi_transfer_half_duplex(struct spi_master *master,
207 + struct spi_message *m)
209 + struct mt7621_spi *rs = spi_master_get_devdata(master);
210 + struct spi_device *spi = m->spi;
211 + unsigned int speed = spi->max_speed_hz;
212 + struct spi_transfer *t = NULL;
216 + u32 data[9] = { 0 };
219 + mt7621_spi_wait_till_ready(spi);
221 + list_for_each_entry(t, &m->transfers, transfer_list) {
222 + const u8 *buf = t->tx_buf;
230 + if (WARN_ON(len + t->len > 36)) {
235 + for (i = 0; i < t->len; i++, len++)
236 + data[len / 4] |= buf[i] << (8 * (len & 3));
239 + if (WARN_ON(rx_len > 32)) {
244 + if (mt7621_spi_prepare(spi, speed)) {
248 + data[0] = swab32(data[0]);
250 + data[0] >>= (4 - len) * 8;
252 + for (i = 0; i < len; i += 4)
253 + mt7621_spi_write(rs, MT7621_SPI_OPCODE + i, data[i / 4]);
255 + val = (min_t(int, len, 4) * 8) << 24;
257 + val |= (len - 4) * 8;
258 + val |= (rx_len * 8) << 12;
259 + mt7621_spi_write(rs, MT7621_SPI_MOREBUF, val);
261 + mt7621_spi_set_cs(spi, 1);
263 + val = mt7621_spi_read(rs, MT7621_SPI_TRANS);
264 + val |= SPI_CTL_START;
265 + mt7621_spi_write(rs, MT7621_SPI_TRANS, val);
267 + mt7621_spi_wait_till_ready(spi);
269 + mt7621_spi_set_cs(spi, 0);
271 + for (i = 0; i < rx_len; i += 4)
272 + data[i / 4] = mt7621_spi_read(rs, MT7621_SPI_DATA0 + i);
274 + m->actual_length = len + rx_len;
277 + list_for_each_entry(t, &m->transfers, transfer_list) {
278 + u8 *buf = t->rx_buf;
283 + for (i = 0; i < t->len; i++, len++)
284 + buf[i] = data[len / 4] >> (8 * (len & 3));
288 + m->status = status;
289 + spi_finalize_current_message(master);
294 +static int mt7621_spi_transfer_full_duplex(struct spi_master *master,
295 + struct spi_message *m)
297 + struct mt7621_spi *rs = spi_master_get_devdata(master);
298 + struct spi_device *spi = m->spi;
299 + unsigned int speed = spi->max_speed_hz;
300 + struct spi_transfer *t = NULL;
304 + u32 data[9] = { 0 };
307 + mt7621_spi_wait_till_ready(spi);
309 + list_for_each_entry(t, &m->transfers, transfer_list) {
310 + const u8 *buf = t->tx_buf;
318 + if (WARN_ON(len + t->len > 16)) {
323 + for (i = 0; i < t->len; i++, len++)
324 + data[len / 4] |= buf[i] << (8 * (len & 3));
325 + if (speed > t->speed_hz)
326 + speed = t->speed_hz;
329 + if (WARN_ON(rx_len > 16)) {
334 + if (mt7621_spi_prepare(spi, speed)) {
339 + for (i = 0; i < len; i += 4)
340 + mt7621_spi_write(rs, MT7621_SPI_DATA0 + i, data[i / 4]);
343 + val |= (rx_len * 8) << 12;
344 + mt7621_spi_write(rs, MT7621_SPI_MOREBUF, val);
346 + mt7621_spi_set_cs(spi, 1);
348 + val = mt7621_spi_read(rs, MT7621_SPI_TRANS);
349 + val |= SPI_CTL_START;
350 + mt7621_spi_write(rs, MT7621_SPI_TRANS, val);
352 + mt7621_spi_wait_till_ready(spi);
354 + mt7621_spi_set_cs(spi, 0);
356 + for (i = 0; i < rx_len; i += 4)
357 + data[i / 4] = mt7621_spi_read(rs, MT7621_SPI_DATA4 + i);
359 + m->actual_length = rx_len;
362 + list_for_each_entry(t, &m->transfers, transfer_list) {
363 + u8 *buf = t->rx_buf;
368 + for (i = 0; i < t->len; i++, len++)
369 + buf[i] = data[len / 4] >> (8 * (len & 3));
373 + m->status = status;
374 + spi_finalize_current_message(master);
379 +static int mt7621_spi_transfer_one_message(struct spi_master *master,
380 + struct spi_message *m)
382 + struct spi_device *spi = m->spi;
383 + int cs = spi->chip_select;
386 + return mt7621_spi_transfer_full_duplex(master, m);
387 + return mt7621_spi_transfer_half_duplex(master, m);
390 +static int mt7621_spi_setup(struct spi_device *spi)
392 + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
394 + if ((spi->max_speed_hz == 0) ||
395 + (spi->max_speed_hz > (rs->sys_freq / 2)))
396 + spi->max_speed_hz = (rs->sys_freq / 2);
398 + if (spi->max_speed_hz < (rs->sys_freq / 4097)) {
399 + dev_err(&spi->dev, "setup: requested speed is too low %d Hz\n",
400 + spi->max_speed_hz);
407 +static const struct of_device_id mt7621_spi_match[] = {
408 + { .compatible = "ralink,mt7621-spi" },
411 +MODULE_DEVICE_TABLE(of, mt7621_spi_match);
413 +static int mt7621_spi_probe(struct platform_device *pdev)
415 + const struct of_device_id *match;
416 + struct spi_master *master;
417 + struct mt7621_spi *rs;
418 + unsigned long flags;
419 + void __iomem *base;
420 + struct resource *r;
423 + struct mt7621_spi_ops *ops;
425 + match = of_match_device(mt7621_spi_match, &pdev->dev);
428 + ops = (struct mt7621_spi_ops *)match->data;
430 + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
431 + base = devm_ioremap_resource(&pdev->dev, r);
433 + return PTR_ERR(base);
435 + clk = devm_clk_get(&pdev->dev, NULL);
437 + dev_err(&pdev->dev, "unable to get SYS clock, err=%d\n",
439 + return PTR_ERR(clk);
442 + status = clk_prepare_enable(clk);
446 + master = spi_alloc_master(&pdev->dev, sizeof(*rs));
447 + if (master == NULL) {
448 + dev_info(&pdev->dev, "master allocation failed\n");
452 + master->mode_bits = RT2880_SPI_MODE_BITS;
454 + master->setup = mt7621_spi_setup;
455 + master->transfer_one_message = mt7621_spi_transfer_one_message;
456 + master->bits_per_word_mask = SPI_BPW_MASK(8);
457 + master->dev.of_node = pdev->dev.of_node;
458 + master->num_chipselect = 2;
460 + dev_set_drvdata(&pdev->dev, master);
462 + rs = spi_master_get_devdata(master);
465 + rs->master = master;
466 + rs->sys_freq = clk_get_rate(rs->clk);
468 + dev_info(&pdev->dev, "sys_freq: %u\n", rs->sys_freq);
469 + spin_lock_irqsave(&rs->lock, flags);
471 + device_reset(&pdev->dev);
473 + mt7621_spi_reset(rs, 0);
475 + return spi_register_master(master);
478 +static int mt7621_spi_remove(struct platform_device *pdev)
480 + struct spi_master *master;
481 + struct mt7621_spi *rs;
483 + master = dev_get_drvdata(&pdev->dev);
484 + rs = spi_master_get_devdata(master);
486 + clk_disable(rs->clk);
487 + spi_unregister_master(master);
492 +MODULE_ALIAS("platform:" DRIVER_NAME);
494 +static struct platform_driver mt7621_spi_driver = {
496 + .name = DRIVER_NAME,
497 + .owner = THIS_MODULE,
498 + .of_match_table = mt7621_spi_match,
500 + .probe = mt7621_spi_probe,
501 + .remove = mt7621_spi_remove,
504 +module_platform_driver(mt7621_spi_driver);
506 +MODULE_DESCRIPTION("MT7621 SPI driver");
507 +MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
508 +MODULE_LICENSE("GPL");