c615601e55e03ce015b2637f0fb4c9692351d1f7
[openwrt/staging/blogic.git] / target / linux / ramips / patches-4.14 / 0043-spi-add-mt7621-support.patch
1 From 87a5fcd57c577cd94b5b080deb98885077c13a42 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Sun, 27 Jul 2014 09:49:07 +0100
4 Subject: [PATCH 43/53] spi: add mt7621 support
5
6 Signed-off-by: John Crispin <blogic@openwrt.org>
7 ---
8 drivers/spi/Kconfig | 6 +
9 drivers/spi/Makefile | 1 +
10 drivers/spi/spi-mt7621.c | 480 ++++++++++++++++++++++++++++++++++++++++++++++
11 3 files changed, 487 insertions(+)
12 create mode 100644 drivers/spi/spi-mt7621.c
13
14 --- a/drivers/spi/Kconfig
15 +++ b/drivers/spi/Kconfig
16 @@ -569,6 +569,12 @@ config SPI_RT2880
17 help
18 This selects a driver for the Ralink RT288x/RT305x SPI Controller.
19
20 +config SPI_MT7621
21 + tristate "MediaTek MT7621 SPI Controller"
22 + depends on RALINK
23 + help
24 + This selects a driver for the MediaTek MT7621 SPI Controller.
25 +
26 config SPI_S3C24XX
27 tristate "Samsung S3C24XX series SPI"
28 depends on ARCH_S3C24XX
29 --- a/drivers/spi/Makefile
30 +++ b/drivers/spi/Makefile
31 @@ -60,6 +60,7 @@ obj-$(CONFIG_SPI_MPC512x_PSC) += spi-mp
32 obj-$(CONFIG_SPI_MPC52xx_PSC) += spi-mpc52xx-psc.o
33 obj-$(CONFIG_SPI_MPC52xx) += spi-mpc52xx.o
34 obj-$(CONFIG_SPI_MT65XX) += spi-mt65xx.o
35 +obj-$(CONFIG_SPI_MT7621) += spi-mt7621.o
36 obj-$(CONFIG_SPI_MXS) += spi-mxs.o
37 obj-$(CONFIG_SPI_NUC900) += spi-nuc900.o
38 obj-$(CONFIG_SPI_OC_TINY) += spi-oc-tiny.o
39 --- /dev/null
40 +++ b/drivers/spi/spi-mt7621.c
41 @@ -0,0 +1,488 @@
42 +/*
43 + * spi-mt7621.c -- MediaTek MT7621 SPI controller driver
44 + *
45 + * Copyright (C) 2011 Sergiy <piratfm@gmail.com>
46 + * Copyright (C) 2011-2013 Gabor Juhos <juhosg@openwrt.org>
47 + * Copyright (C) 2014-2015 Felix Fietkau <nbd@nbd.name>
48 + *
49 + * Some parts are based on spi-orion.c:
50 + * Author: Shadi Ammouri <shadi@marvell.com>
51 + * Copyright (C) 2007-2008 Marvell Ltd.
52 + *
53 + * This program is free software; you can redistribute it and/or modify
54 + * it under the terms of the GNU General Public License version 2 as
55 + * published by the Free Software Foundation.
56 + */
57 +
58 +#include <linux/init.h>
59 +#include <linux/module.h>
60 +#include <linux/clk.h>
61 +#include <linux/err.h>
62 +#include <linux/delay.h>
63 +#include <linux/io.h>
64 +#include <linux/reset.h>
65 +#include <linux/spi/spi.h>
66 +#include <linux/of_device.h>
67 +#include <linux/platform_device.h>
68 +#include <linux/swab.h>
69 +
70 +#include <ralink_regs.h>
71 +
72 +#define SPI_BPW_MASK(bits) BIT((bits) - 1)
73 +
74 +#define DRIVER_NAME "spi-mt7621"
75 +/* in usec */
76 +#define RALINK_SPI_WAIT_MAX_LOOP 2000
77 +
78 +/* SPISTAT register bit field */
79 +#define SPISTAT_BUSY BIT(0)
80 +
81 +#define MT7621_SPI_TRANS 0x00
82 +#define SPITRANS_BUSY BIT(16)
83 +
84 +#define MT7621_SPI_OPCODE 0x04
85 +#define MT7621_SPI_DATA0 0x08
86 +#define MT7621_SPI_DATA4 0x18
87 +#define SPI_CTL_TX_RX_CNT_MASK 0xff
88 +#define SPI_CTL_START BIT(8)
89 +
90 +#define MT7621_SPI_POLAR 0x38
91 +#define MT7621_SPI_MASTER 0x28
92 +#define MT7621_SPI_MOREBUF 0x2c
93 +#define MT7621_SPI_SPACE 0x3c
94 +
95 +#define MT7621_CPHA BIT(5)
96 +#define MT7621_CPOL BIT(4)
97 +#define MT7621_LSB_FIRST BIT(3)
98 +
99 +#define RT2880_SPI_MODE_BITS (SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_CS_HIGH)
100 +
101 +struct mt7621_spi;
102 +
103 +struct mt7621_spi {
104 + struct spi_master *master;
105 + void __iomem *base;
106 + unsigned int sys_freq;
107 + unsigned int speed;
108 + struct clk *clk;
109 +
110 + struct mt7621_spi_ops *ops;
111 +};
112 +
113 +static inline struct mt7621_spi *spidev_to_mt7621_spi(struct spi_device *spi)
114 +{
115 + return spi_master_get_devdata(spi->master);
116 +}
117 +
118 +static inline u32 mt7621_spi_read(struct mt7621_spi *rs, u32 reg)
119 +{
120 + return ioread32(rs->base + reg);
121 +}
122 +
123 +static inline void mt7621_spi_write(struct mt7621_spi *rs, u32 reg, u32 val)
124 +{
125 + iowrite32(val, rs->base + reg);
126 +}
127 +
128 +static void mt7621_spi_reset(struct mt7621_spi *rs, int duplex)
129 +{
130 + u32 master = mt7621_spi_read(rs, MT7621_SPI_MASTER);
131 +
132 + master |= 7 << 29;
133 + master |= 1 << 2;
134 + if (duplex)
135 + master |= 1 << 10;
136 + else
137 + master &= ~(1 << 10);
138 +
139 + mt7621_spi_write(rs, MT7621_SPI_MASTER, master);
140 +}
141 +
142 +static void mt7621_spi_set_cs(struct spi_device *spi, int enable)
143 +{
144 + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
145 + int cs = spi->chip_select;
146 + u32 polar = 0;
147 +
148 + mt7621_spi_reset(rs, cs);
149 + if (enable)
150 + polar = BIT(cs);
151 + mt7621_spi_write(rs, MT7621_SPI_POLAR, polar);
152 +}
153 +
154 +static int mt7621_spi_prepare(struct spi_device *spi, unsigned int speed)
155 +{
156 + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
157 + u32 rate;
158 + u32 reg;
159 +
160 + dev_dbg(&spi->dev, "speed:%u\n", speed);
161 +
162 + rate = DIV_ROUND_UP(rs->sys_freq, speed);
163 + dev_dbg(&spi->dev, "rate-1:%u\n", rate);
164 +
165 + if (rate > 4097)
166 + return -EINVAL;
167 +
168 + if (rate < 2)
169 + rate = 2;
170 +
171 + reg = mt7621_spi_read(rs, MT7621_SPI_MASTER);
172 + reg &= ~(0xfff << 16);
173 + reg |= (rate - 2) << 16;
174 + rs->speed = speed;
175 +
176 + reg &= ~MT7621_LSB_FIRST;
177 + if (spi->mode & SPI_LSB_FIRST)
178 + reg |= MT7621_LSB_FIRST;
179 +
180 + reg &= ~(MT7621_CPHA | MT7621_CPOL);
181 + switch(spi->mode & (SPI_CPOL | SPI_CPHA)) {
182 + case SPI_MODE_0:
183 + break;
184 + case SPI_MODE_1:
185 + reg |= MT7621_CPHA;
186 + break;
187 + case SPI_MODE_2:
188 + reg |= MT7621_CPOL;
189 + break;
190 + case SPI_MODE_3:
191 + reg |= MT7621_CPOL | MT7621_CPHA;
192 + break;
193 + }
194 + mt7621_spi_write(rs, MT7621_SPI_MASTER, reg);
195 +
196 + return 0;
197 +}
198 +
199 +static inline int mt7621_spi_wait_till_ready(struct spi_device *spi)
200 +{
201 + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
202 + int i;
203 +
204 + for (i = 0; i < RALINK_SPI_WAIT_MAX_LOOP; i++) {
205 + u32 status;
206 +
207 + status = mt7621_spi_read(rs, MT7621_SPI_TRANS);
208 + if ((status & SPITRANS_BUSY) == 0) {
209 + return 0;
210 + }
211 + cpu_relax();
212 + udelay(1);
213 + }
214 +
215 + return -ETIMEDOUT;
216 +}
217 +
218 +static int mt7621_spi_transfer_half_duplex(struct spi_master *master,
219 + struct spi_message *m)
220 +{
221 + struct mt7621_spi *rs = spi_master_get_devdata(master);
222 + struct spi_device *spi = m->spi;
223 + unsigned int speed = spi->max_speed_hz;
224 + struct spi_transfer *t = NULL;
225 + int status = 0;
226 + int i, len = 0;
227 + int rx_len = 0;
228 + u32 data[9] = { 0 };
229 + u32 val;
230 +
231 + mt7621_spi_wait_till_ready(spi);
232 +
233 + list_for_each_entry(t, &m->transfers, transfer_list) {
234 + const u8 *buf = t->tx_buf;
235 +
236 + if (t->rx_buf)
237 + rx_len += t->len;
238 +
239 + if (!buf)
240 + continue;
241 +
242 + if (t->speed_hz < speed)
243 + speed = t->speed_hz;
244 +
245 + /*
246 + * m25p80 might attempt to write more data than we can handle.
247 + * truncate the message to what we can fit into the registers
248 + */
249 + if (len + t->len > 36)
250 + t->len = 36 - len;
251 +
252 + for (i = 0; i < t->len; i++, len++)
253 + data[len / 4] |= buf[i] << (8 * (len & 3));
254 + }
255 +
256 + if (WARN_ON(rx_len > 32)) {
257 + status = -EIO;
258 + goto msg_done;
259 + }
260 +
261 + if (mt7621_spi_prepare(spi, speed)) {
262 + status = -EIO;
263 + goto msg_done;
264 + }
265 + data[0] = swab32(data[0]);
266 + if (len < 4)
267 + data[0] >>= (4 - len) * 8;
268 +
269 + for (i = 0; i < len; i += 4)
270 + mt7621_spi_write(rs, MT7621_SPI_OPCODE + i, data[i / 4]);
271 +
272 + val = (min_t(int, len, 4) * 8) << 24;
273 + if (len > 4)
274 + val |= (len - 4) * 8;
275 + val |= (rx_len * 8) << 12;
276 + mt7621_spi_write(rs, MT7621_SPI_MOREBUF, val);
277 +
278 + mt7621_spi_set_cs(spi, 1);
279 +
280 + val = mt7621_spi_read(rs, MT7621_SPI_TRANS);
281 + val |= SPI_CTL_START;
282 + mt7621_spi_write(rs, MT7621_SPI_TRANS, val);
283 +
284 + mt7621_spi_wait_till_ready(spi);
285 +
286 + mt7621_spi_set_cs(spi, 0);
287 +
288 + for (i = 0; i < rx_len; i += 4)
289 + data[i / 4] = mt7621_spi_read(rs, MT7621_SPI_DATA0 + i);
290 +
291 + m->actual_length = len + rx_len;
292 +
293 + len = 0;
294 + list_for_each_entry(t, &m->transfers, transfer_list) {
295 + u8 *buf = t->rx_buf;
296 +
297 + if (!buf)
298 + continue;
299 +
300 + for (i = 0; i < t->len; i++, len++)
301 + buf[i] = data[len / 4] >> (8 * (len & 3));
302 + }
303 +
304 +msg_done:
305 + m->status = status;
306 + spi_finalize_current_message(master);
307 +
308 + return 0;
309 +}
310 +
311 +static int mt7621_spi_transfer_full_duplex(struct spi_master *master,
312 + struct spi_message *m)
313 +{
314 + struct mt7621_spi *rs = spi_master_get_devdata(master);
315 + struct spi_device *spi = m->spi;
316 + unsigned int speed = spi->max_speed_hz;
317 + struct spi_transfer *t = NULL;
318 + int status = 0;
319 + int i, len = 0;
320 + int rx_len = 0;
321 + u32 data[9] = { 0 };
322 + u32 val = 0;
323 +
324 + mt7621_spi_wait_till_ready(spi);
325 +
326 + list_for_each_entry(t, &m->transfers, transfer_list) {
327 + const u8 *buf = t->tx_buf;
328 +
329 + if (t->rx_buf)
330 + rx_len += t->len;
331 +
332 + if (!buf)
333 + continue;
334 +
335 + if (WARN_ON(len + t->len > 16)) {
336 + status = -EIO;
337 + goto msg_done;
338 + }
339 +
340 + for (i = 0; i < t->len; i++, len++)
341 + data[len / 4] |= buf[i] << (8 * (len & 3));
342 + if (speed > t->speed_hz)
343 + speed = t->speed_hz;
344 + }
345 +
346 + if (WARN_ON(rx_len > 16)) {
347 + status = -EIO;
348 + goto msg_done;
349 + }
350 +
351 + if (mt7621_spi_prepare(spi, speed)) {
352 + status = -EIO;
353 + goto msg_done;
354 + }
355 +
356 + for (i = 0; i < len; i += 4)
357 + mt7621_spi_write(rs, MT7621_SPI_DATA0 + i, data[i / 4]);
358 +
359 + val |= len * 8;
360 + val |= (rx_len * 8) << 12;
361 + mt7621_spi_write(rs, MT7621_SPI_MOREBUF, val);
362 +
363 + mt7621_spi_set_cs(spi, 1);
364 +
365 + val = mt7621_spi_read(rs, MT7621_SPI_TRANS);
366 + val |= SPI_CTL_START;
367 + mt7621_spi_write(rs, MT7621_SPI_TRANS, val);
368 +
369 + mt7621_spi_wait_till_ready(spi);
370 +
371 + mt7621_spi_set_cs(spi, 0);
372 +
373 + for (i = 0; i < rx_len; i += 4)
374 + data[i / 4] = mt7621_spi_read(rs, MT7621_SPI_DATA4 + i);
375 +
376 + m->actual_length = rx_len;
377 +
378 + len = 0;
379 + list_for_each_entry(t, &m->transfers, transfer_list) {
380 + u8 *buf = t->rx_buf;
381 +
382 + if (!buf)
383 + continue;
384 +
385 + for (i = 0; i < t->len; i++, len++)
386 + buf[i] = data[len / 4] >> (8 * (len & 3));
387 + }
388 +
389 +msg_done:
390 + m->status = status;
391 + spi_finalize_current_message(master);
392 +
393 + return 0;
394 +}
395 +
396 +static int mt7621_spi_transfer_one_message(struct spi_master *master,
397 + struct spi_message *m)
398 +{
399 + struct spi_device *spi = m->spi;
400 + int cs = spi->chip_select;
401 +
402 + if (cs)
403 + return mt7621_spi_transfer_full_duplex(master, m);
404 + return mt7621_spi_transfer_half_duplex(master, m);
405 +}
406 +
407 +static int mt7621_spi_setup(struct spi_device *spi)
408 +{
409 + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
410 +
411 + if ((spi->max_speed_hz == 0) ||
412 + (spi->max_speed_hz > (rs->sys_freq / 2)))
413 + spi->max_speed_hz = (rs->sys_freq / 2);
414 +
415 + if (spi->max_speed_hz < (rs->sys_freq / 4097)) {
416 + dev_err(&spi->dev, "setup: requested speed is too low %d Hz\n",
417 + spi->max_speed_hz);
418 + return -EINVAL;
419 + }
420 +
421 + return 0;
422 +}
423 +
424 +static const struct of_device_id mt7621_spi_match[] = {
425 + { .compatible = "ralink,mt7621-spi" },
426 + {},
427 +};
428 +MODULE_DEVICE_TABLE(of, mt7621_spi_match);
429 +
430 +static size_t mt7621_max_transfer_size(struct spi_device *spi)
431 +{
432 + return 32;
433 +}
434 +
435 +static int mt7621_spi_probe(struct platform_device *pdev)
436 +{
437 + const struct of_device_id *match;
438 + struct spi_master *master;
439 + struct mt7621_spi *rs;
440 + void __iomem *base;
441 + struct resource *r;
442 + int status = 0;
443 + struct clk *clk;
444 + struct mt7621_spi_ops *ops;
445 +
446 + match = of_match_device(mt7621_spi_match, &pdev->dev);
447 + if (!match)
448 + return -EINVAL;
449 + ops = (struct mt7621_spi_ops *)match->data;
450 +
451 + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
452 + base = devm_ioremap_resource(&pdev->dev, r);
453 + if (IS_ERR(base))
454 + return PTR_ERR(base);
455 +
456 + clk = devm_clk_get(&pdev->dev, NULL);
457 + if (IS_ERR(clk)) {
458 + dev_err(&pdev->dev, "unable to get SYS clock, err=%d\n",
459 + status);
460 + return PTR_ERR(clk);
461 + }
462 +
463 + status = clk_prepare_enable(clk);
464 + if (status)
465 + return status;
466 +
467 + master = spi_alloc_master(&pdev->dev, sizeof(*rs));
468 + if (master == NULL) {
469 + dev_info(&pdev->dev, "master allocation failed\n");
470 + return -ENOMEM;
471 + }
472 +
473 + master->mode_bits = RT2880_SPI_MODE_BITS;
474 +
475 + master->setup = mt7621_spi_setup;
476 + master->transfer_one_message = mt7621_spi_transfer_one_message;
477 + master->bits_per_word_mask = SPI_BPW_MASK(8);
478 + master->dev.of_node = pdev->dev.of_node;
479 + master->num_chipselect = 2;
480 + master->max_transfer_size = mt7621_max_transfer_size;
481 +
482 + dev_set_drvdata(&pdev->dev, master);
483 +
484 + rs = spi_master_get_devdata(master);
485 + rs->base = base;
486 + rs->clk = clk;
487 + rs->master = master;
488 + rs->sys_freq = clk_get_rate(rs->clk);
489 + rs->ops = ops;
490 + dev_info(&pdev->dev, "sys_freq: %u\n", rs->sys_freq);
491 +
492 + device_reset(&pdev->dev);
493 +
494 + mt7621_spi_reset(rs, 0);
495 +
496 + return spi_register_master(master);
497 +}
498 +
499 +static int mt7621_spi_remove(struct platform_device *pdev)
500 +{
501 + struct spi_master *master;
502 + struct mt7621_spi *rs;
503 +
504 + master = dev_get_drvdata(&pdev->dev);
505 + rs = spi_master_get_devdata(master);
506 +
507 + clk_disable(rs->clk);
508 + spi_unregister_master(master);
509 +
510 + return 0;
511 +}
512 +
513 +MODULE_ALIAS("platform:" DRIVER_NAME);
514 +
515 +static struct platform_driver mt7621_spi_driver = {
516 + .driver = {
517 + .name = DRIVER_NAME,
518 + .owner = THIS_MODULE,
519 + .of_match_table = mt7621_spi_match,
520 + },
521 + .probe = mt7621_spi_probe,
522 + .remove = mt7621_spi_remove,
523 +};
524 +
525 +module_platform_driver(mt7621_spi_driver);
526 +
527 +MODULE_DESCRIPTION("MT7621 SPI driver");
528 +MODULE_AUTHOR("Felix Fietkau <nbd@nbd.name>");
529 +MODULE_LICENSE("GPL");