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