blob: 2c3b7a2a1ec77a0f68733ec62ba1695abca6c5b7 [file] [log] [blame]
Stefan Roese9ad67a12019-02-01 11:17:07 +01001// SPDX-License-Identifier: GPL-2.0
Stefan Roesecbd66c62019-03-25 09:29:25 +01002//
3// spi-mt7621.c -- MediaTek MT7621 SPI controller driver
4//
5// Copyright (C) 2011 Sergiy <piratfm@gmail.com>
6// Copyright (C) 2011-2013 Gabor Juhos <juhosg@openwrt.org>
7// Copyright (C) 2014-2015 Felix Fietkau <nbd@nbd.name>
8//
9// Some parts are based on spi-orion.c:
10// Author: Shadi Ammouri <shadi@marvell.com>
11// Copyright (C) 2007-2008 Marvell Ltd.
John Crispin1ab7f2a2018-03-15 07:22:35 +110012
John Crispin1ab7f2a2018-03-15 07:22:35 +110013#include <linux/clk.h>
John Crispin1ab7f2a2018-03-15 07:22:35 +110014#include <linux/delay.h>
15#include <linux/io.h>
Stefan Roesedab7ed42019-02-01 11:17:08 +010016#include <linux/module.h>
17#include <linux/of_device.h>
John Crispin1ab7f2a2018-03-15 07:22:35 +110018#include <linux/reset.h>
19#include <linux/spi/spi.h>
John Crispin1ab7f2a2018-03-15 07:22:35 +110020
Stefan Roese718a4912019-02-01 11:17:11 +010021#define DRIVER_NAME "spi-mt7621"
22
John Crispin1ab7f2a2018-03-15 07:22:35 +110023/* in usec */
Stefan Roese718a4912019-02-01 11:17:11 +010024#define RALINK_SPI_WAIT_MAX_LOOP 2000
John Crispin1ab7f2a2018-03-15 07:22:35 +110025
26/* SPISTAT register bit field */
Stefan Roese718a4912019-02-01 11:17:11 +010027#define SPISTAT_BUSY BIT(0)
John Crispin1ab7f2a2018-03-15 07:22:35 +110028
29#define MT7621_SPI_TRANS 0x00
30#define SPITRANS_BUSY BIT(16)
31
32#define MT7621_SPI_OPCODE 0x04
33#define MT7621_SPI_DATA0 0x08
34#define MT7621_SPI_DATA4 0x18
35#define SPI_CTL_TX_RX_CNT_MASK 0xff
36#define SPI_CTL_START BIT(8)
37
John Crispin1ab7f2a2018-03-15 07:22:35 +110038#define MT7621_SPI_MASTER 0x28
Stefan Roese5220dd42019-02-01 11:17:14 +010039#define MASTER_MORE_BUFMODE BIT(2)
40#define MASTER_FULL_DUPLEX BIT(10)
41#define MASTER_RS_CLK_SEL GENMASK(27, 16)
42#define MASTER_RS_CLK_SEL_SHIFT 16
43#define MASTER_RS_SLAVE_SEL GENMASK(31, 29)
44
John Crispin1ab7f2a2018-03-15 07:22:35 +110045#define MT7621_SPI_MOREBUF 0x2c
Stefan Roese376a6222019-02-01 11:17:13 +010046#define MT7621_SPI_POLAR 0x38
John Crispin1ab7f2a2018-03-15 07:22:35 +110047#define MT7621_SPI_SPACE 0x3c
48
49#define MT7621_CPHA BIT(5)
50#define MT7621_CPOL BIT(4)
51#define MT7621_LSB_FIRST BIT(3)
52
John Crispin1ab7f2a2018-03-15 07:22:35 +110053struct mt7621_spi {
Stefan Roesecbd66c62019-03-25 09:29:25 +010054 struct spi_controller *master;
John Crispin1ab7f2a2018-03-15 07:22:35 +110055 void __iomem *base;
56 unsigned int sys_freq;
57 unsigned int speed;
58 struct clk *clk;
NeilBrownbf732c62018-06-07 08:04:21 +100059 int pending_write;
John Crispin1ab7f2a2018-03-15 07:22:35 +110060};
61
62static inline struct mt7621_spi *spidev_to_mt7621_spi(struct spi_device *spi)
63{
Stefan Roesecbd66c62019-03-25 09:29:25 +010064 return spi_controller_get_devdata(spi->master);
John Crispin1ab7f2a2018-03-15 07:22:35 +110065}
66
67static inline u32 mt7621_spi_read(struct mt7621_spi *rs, u32 reg)
68{
69 return ioread32(rs->base + reg);
70}
71
72static inline void mt7621_spi_write(struct mt7621_spi *rs, u32 reg, u32 val)
73{
74 iowrite32(val, rs->base + reg);
75}
76
Stefan Roesecbd66c62019-03-25 09:29:25 +010077static void mt7621_spi_set_cs(struct spi_device *spi, int enable)
John Crispin1ab7f2a2018-03-15 07:22:35 +110078{
Stefan Roesecbd66c62019-03-25 09:29:25 +010079 struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
80 int cs = spi->chip_select;
81 u32 polar = 0;
82 u32 master;
John Crispin1ab7f2a2018-03-15 07:22:35 +110083
Stefan Roese5220dd42019-02-01 11:17:14 +010084 /*
85 * Select SPI device 7, enable "more buffer mode" and disable
86 * full-duplex (only half-duplex really works on this chip
87 * reliably)
88 */
Stefan Roesecbd66c62019-03-25 09:29:25 +010089 master = mt7621_spi_read(rs, MT7621_SPI_MASTER);
Stefan Roese5220dd42019-02-01 11:17:14 +010090 master |= MASTER_RS_SLAVE_SEL | MASTER_MORE_BUFMODE;
91 master &= ~MASTER_FULL_DUPLEX;
John Crispin1ab7f2a2018-03-15 07:22:35 +110092 mt7621_spi_write(rs, MT7621_SPI_MASTER, master);
Stefan Roesecbd66c62019-03-25 09:29:25 +010093
NeilBrownbf732c62018-06-07 08:04:21 +100094 rs->pending_write = 0;
John Crispin1ab7f2a2018-03-15 07:22:35 +110095
John Crispin1ab7f2a2018-03-15 07:22:35 +110096 if (enable)
97 polar = BIT(cs);
98 mt7621_spi_write(rs, MT7621_SPI_POLAR, polar);
99}
100
101static int mt7621_spi_prepare(struct spi_device *spi, unsigned int speed)
102{
103 struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
104 u32 rate;
105 u32 reg;
106
107 dev_dbg(&spi->dev, "speed:%u\n", speed);
108
109 rate = DIV_ROUND_UP(rs->sys_freq, speed);
110 dev_dbg(&spi->dev, "rate-1:%u\n", rate);
111
112 if (rate > 4097)
113 return -EINVAL;
114
115 if (rate < 2)
116 rate = 2;
117
118 reg = mt7621_spi_read(rs, MT7621_SPI_MASTER);
Stefan Roese5220dd42019-02-01 11:17:14 +0100119 reg &= ~MASTER_RS_CLK_SEL;
120 reg |= (rate - 2) << MASTER_RS_CLK_SEL_SHIFT;
John Crispin1ab7f2a2018-03-15 07:22:35 +1100121 rs->speed = speed;
122
123 reg &= ~MT7621_LSB_FIRST;
124 if (spi->mode & SPI_LSB_FIRST)
125 reg |= MT7621_LSB_FIRST;
126
Stefan Roesefeec6672019-02-01 11:17:12 +0100127 /*
128 * This SPI controller seems to be tested on SPI flash only and some
129 * bits are swizzled under other SPI modes probably due to incorrect
130 * wiring inside the silicon. Only mode 0 works correctly.
Chuanhong Guo354ea2e2018-12-06 21:15:09 +0800131 */
John Crispin1ab7f2a2018-03-15 07:22:35 +1100132 reg &= ~(MT7621_CPHA | MT7621_CPOL);
Chuanhong Guo354ea2e2018-12-06 21:15:09 +0800133
John Crispin1ab7f2a2018-03-15 07:22:35 +1100134 mt7621_spi_write(rs, MT7621_SPI_MASTER, reg);
135
136 return 0;
137}
138
NeilBrowna83834c2018-06-07 08:04:21 +1000139static inline int mt7621_spi_wait_till_ready(struct mt7621_spi *rs)
John Crispin1ab7f2a2018-03-15 07:22:35 +1100140{
John Crispin1ab7f2a2018-03-15 07:22:35 +1100141 int i;
142
143 for (i = 0; i < RALINK_SPI_WAIT_MAX_LOOP; i++) {
144 u32 status;
145
146 status = mt7621_spi_read(rs, MT7621_SPI_TRANS);
Sankalp Negi9c562d82018-06-03 00:07:31 +0530147 if ((status & SPITRANS_BUSY) == 0)
John Crispin1ab7f2a2018-03-15 07:22:35 +1100148 return 0;
John Crispin1ab7f2a2018-03-15 07:22:35 +1100149 cpu_relax();
150 udelay(1);
151 }
152
153 return -ETIMEDOUT;
154}
155
NeilBrownbf732c62018-06-07 08:04:21 +1000156static void mt7621_spi_read_half_duplex(struct mt7621_spi *rs,
157 int rx_len, u8 *buf)
158{
Stefan Roesecbd66c62019-03-25 09:29:25 +0100159 int tx_len;
160
Stefan Roesefeec6672019-02-01 11:17:12 +0100161 /*
162 * Combine with any pending write, and perform one or more half-duplex
163 * transactions reading 'len' bytes. Data to be written is already in
164 * MT7621_SPI_DATA.
NeilBrownbf732c62018-06-07 08:04:21 +1000165 */
Stefan Roesecbd66c62019-03-25 09:29:25 +0100166 tx_len = rs->pending_write;
NeilBrownbf732c62018-06-07 08:04:21 +1000167 rs->pending_write = 0;
168
169 while (rx_len || tx_len) {
170 int i;
171 u32 val = (min(tx_len, 4) * 8) << 24;
172 int rx = min(rx_len, 32);
173
174 if (tx_len > 4)
175 val |= (tx_len - 4) * 8;
176 val |= (rx * 8) << 12;
177 mt7621_spi_write(rs, MT7621_SPI_MOREBUF, val);
178
179 tx_len = 0;
180
181 val = mt7621_spi_read(rs, MT7621_SPI_TRANS);
182 val |= SPI_CTL_START;
183 mt7621_spi_write(rs, MT7621_SPI_TRANS, val);
184
185 mt7621_spi_wait_till_ready(rs);
186
187 for (i = 0; i < rx; i++) {
188 if ((i % 4) == 0)
189 val = mt7621_spi_read(rs, MT7621_SPI_DATA0 + i);
190 *buf++ = val & 0xff;
191 val >>= 8;
192 }
Stefan Roese718a4912019-02-01 11:17:11 +0100193
NeilBrownbf732c62018-06-07 08:04:21 +1000194 rx_len -= i;
195 }
196}
197
198static inline void mt7621_spi_flush(struct mt7621_spi *rs)
199{
200 mt7621_spi_read_half_duplex(rs, 0, NULL);
201}
202
203static void mt7621_spi_write_half_duplex(struct mt7621_spi *rs,
204 int tx_len, const u8 *buf)
205{
NeilBrownbf732c62018-06-07 08:04:21 +1000206 int len = rs->pending_write;
Stefan Roesecbd66c62019-03-25 09:29:25 +0100207 int val = 0;
NeilBrownbf732c62018-06-07 08:04:21 +1000208
209 if (len & 3) {
210 val = mt7621_spi_read(rs, MT7621_SPI_OPCODE + (len & ~3));
211 if (len < 4) {
212 val <<= (4 - len) * 8;
213 val = swab32(val);
214 }
215 }
216
217 while (tx_len > 0) {
218 if (len >= 36) {
219 rs->pending_write = len;
220 mt7621_spi_flush(rs);
221 len = 0;
222 }
223
224 val |= *buf++ << (8 * (len & 3));
225 len++;
226 if ((len & 3) == 0) {
227 if (len == 4)
228 /* The byte-order of the opcode is weird! */
229 val = swab32(val);
230 mt7621_spi_write(rs, MT7621_SPI_OPCODE + len - 4, val);
231 val = 0;
232 }
233 tx_len -= 1;
234 }
Stefan Roesecbd66c62019-03-25 09:29:25 +0100235
NeilBrownbf732c62018-06-07 08:04:21 +1000236 if (len & 3) {
237 if (len < 4) {
238 val = swab32(val);
239 val >>= (4 - len) * 8;
240 }
241 mt7621_spi_write(rs, MT7621_SPI_OPCODE + (len & ~3), val);
242 }
Stefan Roesecbd66c62019-03-25 09:29:25 +0100243
NeilBrownbf732c62018-06-07 08:04:21 +1000244 rs->pending_write = len;
245}
246
Stefan Roesecbd66c62019-03-25 09:29:25 +0100247static int mt7621_spi_transfer_one_message(struct spi_controller *master,
John Crispin1ab7f2a2018-03-15 07:22:35 +1100248 struct spi_message *m)
249{
Stefan Roesecbd66c62019-03-25 09:29:25 +0100250 struct mt7621_spi *rs = spi_controller_get_devdata(master);
John Crispin1ab7f2a2018-03-15 07:22:35 +1100251 struct spi_device *spi = m->spi;
252 unsigned int speed = spi->max_speed_hz;
253 struct spi_transfer *t = NULL;
254 int status = 0;
John Crispin1ab7f2a2018-03-15 07:22:35 +1100255
NeilBrowna83834c2018-06-07 08:04:21 +1000256 mt7621_spi_wait_till_ready(rs);
John Crispin1ab7f2a2018-03-15 07:22:35 +1100257
NeilBrownbf732c62018-06-07 08:04:21 +1000258 list_for_each_entry(t, &m->transfers, transfer_list)
John Crispin1ab7f2a2018-03-15 07:22:35 +1100259 if (t->speed_hz < speed)
260 speed = t->speed_hz;
261
John Crispin1ab7f2a2018-03-15 07:22:35 +1100262 if (mt7621_spi_prepare(spi, speed)) {
263 status = -EIO;
264 goto msg_done;
265 }
John Crispin1ab7f2a2018-03-15 07:22:35 +1100266
Stefan Roesecbd66c62019-03-25 09:29:25 +0100267 /* Assert CS */
John Crispin1ab7f2a2018-03-15 07:22:35 +1100268 mt7621_spi_set_cs(spi, 1);
Stefan Roesecbd66c62019-03-25 09:29:25 +0100269
NeilBrownbf732c62018-06-07 08:04:21 +1000270 m->actual_length = 0;
271 list_for_each_entry(t, &m->transfers, transfer_list) {
Chuanhong Guo108d9dd2018-12-06 21:15:08 +0800272 if ((t->rx_buf) && (t->tx_buf)) {
Stefan Roesecbd66c62019-03-25 09:29:25 +0100273 /*
274 * This controller will shift some extra data out
Chuanhong Guo108d9dd2018-12-06 21:15:08 +0800275 * of spi_opcode if (mosi_bit_cnt > 0) &&
276 * (cmd_bit_cnt == 0). So the claimed full-duplex
277 * support is broken since we have no way to read
278 * the MISO value during that bit.
279 */
280 status = -EIO;
281 goto msg_done;
282 } else if (t->rx_buf) {
NeilBrownbf732c62018-06-07 08:04:21 +1000283 mt7621_spi_read_half_duplex(rs, t->len, t->rx_buf);
Chuanhong Guo108d9dd2018-12-06 21:15:08 +0800284 } else if (t->tx_buf) {
NeilBrownbf732c62018-06-07 08:04:21 +1000285 mt7621_spi_write_half_duplex(rs, t->len, t->tx_buf);
Chuanhong Guo108d9dd2018-12-06 21:15:08 +0800286 }
NeilBrownbf732c62018-06-07 08:04:21 +1000287 m->actual_length += t->len;
288 }
John Crispin1ab7f2a2018-03-15 07:22:35 +1100289
Stefan Roesecbd66c62019-03-25 09:29:25 +0100290 /* Flush data and deassert CS */
291 mt7621_spi_flush(rs);
John Crispin1ab7f2a2018-03-15 07:22:35 +1100292 mt7621_spi_set_cs(spi, 0);
Stefan Roese718a4912019-02-01 11:17:11 +0100293
John Crispin1ab7f2a2018-03-15 07:22:35 +1100294msg_done:
295 m->status = status;
296 spi_finalize_current_message(master);
297
298 return 0;
299}
300
John Crispin1ab7f2a2018-03-15 07:22:35 +1100301static int mt7621_spi_setup(struct spi_device *spi)
302{
303 struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
304
305 if ((spi->max_speed_hz == 0) ||
Stefan Roesecbd66c62019-03-25 09:29:25 +0100306 (spi->max_speed_hz > (rs->sys_freq / 2)))
Nishka Dasgupta18f0e242019-04-02 10:41:56 +0530307 spi->max_speed_hz = rs->sys_freq / 2;
John Crispin1ab7f2a2018-03-15 07:22:35 +1100308
309 if (spi->max_speed_hz < (rs->sys_freq / 4097)) {
310 dev_err(&spi->dev, "setup: requested speed is too low %d Hz\n",
311 spi->max_speed_hz);
312 return -EINVAL;
313 }
314
315 return 0;
316}
317
318static const struct of_device_id mt7621_spi_match[] = {
319 { .compatible = "ralink,mt7621-spi" },
320 {},
321};
322MODULE_DEVICE_TABLE(of, mt7621_spi_match);
323
John Crispin1ab7f2a2018-03-15 07:22:35 +1100324static int mt7621_spi_probe(struct platform_device *pdev)
325{
326 const struct of_device_id *match;
Stefan Roesecbd66c62019-03-25 09:29:25 +0100327 struct spi_controller *master;
John Crispin1ab7f2a2018-03-15 07:22:35 +1100328 struct mt7621_spi *rs;
John Crispin1ab7f2a2018-03-15 07:22:35 +1100329 void __iomem *base;
John Crispin1ab7f2a2018-03-15 07:22:35 +1100330 int status = 0;
331 struct clk *clk;
Stefan Roese46c33782019-02-01 11:17:09 +0100332 int ret;
John Crispin1ab7f2a2018-03-15 07:22:35 +1100333
334 match = of_match_device(mt7621_spi_match, &pdev->dev);
335 if (!match)
336 return -EINVAL;
John Crispin1ab7f2a2018-03-15 07:22:35 +1100337
YueHaibingf88771c2019-09-04 21:58:59 +0800338 base = devm_platform_ioremap_resource(pdev, 0);
John Crispin1ab7f2a2018-03-15 07:22:35 +1100339 if (IS_ERR(base))
340 return PTR_ERR(base);
341
342 clk = devm_clk_get(&pdev->dev, NULL);
343 if (IS_ERR(clk)) {
344 dev_err(&pdev->dev, "unable to get SYS clock, err=%d\n",
345 status);
346 return PTR_ERR(clk);
347 }
348
349 status = clk_prepare_enable(clk);
350 if (status)
351 return status;
352
353 master = spi_alloc_master(&pdev->dev, sizeof(*rs));
Jasminko Dedic5ccbce32019-02-04 02:44:42 +0700354 if (!master) {
John Crispin1ab7f2a2018-03-15 07:22:35 +1100355 dev_info(&pdev->dev, "master allocation failed\n");
356 return -ENOMEM;
357 }
358
Chuanhong Guo354ea2e2018-12-06 21:15:09 +0800359 master->mode_bits = SPI_LSB_FIRST;
Stefan Roesecbd66c62019-03-25 09:29:25 +0100360 master->flags = SPI_CONTROLLER_HALF_DUPLEX;
John Crispin1ab7f2a2018-03-15 07:22:35 +1100361 master->setup = mt7621_spi_setup;
362 master->transfer_one_message = mt7621_spi_transfer_one_message;
363 master->bits_per_word_mask = SPI_BPW_MASK(8);
364 master->dev.of_node = pdev->dev.of_node;
365 master->num_chipselect = 2;
John Crispin1ab7f2a2018-03-15 07:22:35 +1100366
367 dev_set_drvdata(&pdev->dev, master);
368
Stefan Roesecbd66c62019-03-25 09:29:25 +0100369 rs = spi_controller_get_devdata(master);
John Crispin1ab7f2a2018-03-15 07:22:35 +1100370 rs->base = base;
371 rs->clk = clk;
372 rs->master = master;
373 rs->sys_freq = clk_get_rate(rs->clk);
NeilBrownbf732c62018-06-07 08:04:21 +1000374 rs->pending_write = 0;
John Crispin1ab7f2a2018-03-15 07:22:35 +1100375 dev_info(&pdev->dev, "sys_freq: %u\n", rs->sys_freq);
John Crispin1ab7f2a2018-03-15 07:22:35 +1100376
Stefan Roese46c33782019-02-01 11:17:09 +0100377 ret = device_reset(&pdev->dev);
378 if (ret) {
379 dev_err(&pdev->dev, "SPI reset failed!\n");
380 return ret;
381 }
John Crispin1ab7f2a2018-03-15 07:22:35 +1100382
Stefan Roesecbd66c62019-03-25 09:29:25 +0100383 return devm_spi_register_controller(&pdev->dev, master);
John Crispin1ab7f2a2018-03-15 07:22:35 +1100384}
385
386static int mt7621_spi_remove(struct platform_device *pdev)
387{
Stefan Roesecbd66c62019-03-25 09:29:25 +0100388 struct spi_controller *master;
John Crispin1ab7f2a2018-03-15 07:22:35 +1100389 struct mt7621_spi *rs;
390
391 master = dev_get_drvdata(&pdev->dev);
Stefan Roesecbd66c62019-03-25 09:29:25 +0100392 rs = spi_controller_get_devdata(master);
John Crispin1ab7f2a2018-03-15 07:22:35 +1100393
Stefan Roesecbd66c62019-03-25 09:29:25 +0100394 clk_disable_unprepare(rs->clk);
John Crispin1ab7f2a2018-03-15 07:22:35 +1100395
396 return 0;
397}
398
399MODULE_ALIAS("platform:" DRIVER_NAME);
400
401static struct platform_driver mt7621_spi_driver = {
402 .driver = {
403 .name = DRIVER_NAME,
John Crispin1ab7f2a2018-03-15 07:22:35 +1100404 .of_match_table = mt7621_spi_match,
405 },
406 .probe = mt7621_spi_probe,
407 .remove = mt7621_spi_remove,
408};
409
410module_platform_driver(mt7621_spi_driver);
411
412MODULE_DESCRIPTION("MT7621 SPI driver");
413MODULE_AUTHOR("Felix Fietkau <nbd@nbd.name>");
414MODULE_LICENSE("GPL");