1 // SPDX-License-Identifier: GPL-2.0
3 // spi-mt7621.c -- MediaTek MT7621 SPI controller driver
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>
9 // Some parts are based on spi-orion.c:
10 // Author: Shadi Ammouri <shadi@marvell.com>
11 // Copyright (C) 2007-2008 Marvell Ltd.
13 #include <linux/clk.h>
14 #include <linux/delay.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/reset.h>
19 #include <linux/spi/spi.h>
21 #define DRIVER_NAME "spi-mt7621"
24 #define RALINK_SPI_WAIT_MAX_LOOP 2000
26 /* SPISTAT register bit field */
27 #define SPISTAT_BUSY BIT(0)
29 #define MT7621_SPI_TRANS 0x00
30 #define SPITRANS_BUSY BIT(16)
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)
38 #define MT7621_SPI_MASTER 0x28
39 #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)
45 #define MT7621_SPI_MOREBUF 0x2c
46 #define MT7621_SPI_POLAR 0x38
47 #define MT7621_SPI_SPACE 0x3c
49 #define MT7621_CPHA BIT(5)
50 #define MT7621_CPOL BIT(4)
51 #define MT7621_LSB_FIRST BIT(3)
54 struct spi_controller *master;
56 unsigned int sys_freq;
61 static inline struct mt7621_spi *spidev_to_mt7621_spi(struct spi_device *spi)
63 return spi_controller_get_devdata(spi->master);
66 static inline u32 mt7621_spi_read(struct mt7621_spi *rs, u32 reg)
68 return ioread32(rs->base + reg);
71 static inline void mt7621_spi_write(struct mt7621_spi *rs, u32 reg, u32 val)
73 iowrite32(val, rs->base + reg);
76 static void mt7621_spi_set_cs(struct spi_device *spi, int enable)
78 struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
79 int cs = spi->chip_select;
84 * Select SPI device 7, enable "more buffer mode" and disable
85 * full-duplex (only half-duplex really works on this chip
88 master = mt7621_spi_read(rs, MT7621_SPI_MASTER);
89 master |= MASTER_RS_SLAVE_SEL | MASTER_MORE_BUFMODE;
90 master &= ~MASTER_FULL_DUPLEX;
91 mt7621_spi_write(rs, MT7621_SPI_MASTER, master);
93 rs->pending_write = 0;
97 mt7621_spi_write(rs, MT7621_SPI_POLAR, polar);
100 static int mt7621_spi_prepare(struct spi_device *spi, unsigned int speed)
102 struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
106 dev_dbg(&spi->dev, "speed:%u\n", speed);
108 rate = DIV_ROUND_UP(rs->sys_freq, speed);
109 dev_dbg(&spi->dev, "rate-1:%u\n", rate);
117 reg = mt7621_spi_read(rs, MT7621_SPI_MASTER);
118 reg &= ~MASTER_RS_CLK_SEL;
119 reg |= (rate - 2) << MASTER_RS_CLK_SEL_SHIFT;
122 reg &= ~MT7621_LSB_FIRST;
123 if (spi->mode & SPI_LSB_FIRST)
124 reg |= MT7621_LSB_FIRST;
127 * This SPI controller seems to be tested on SPI flash only and some
128 * bits are swizzled under other SPI modes probably due to incorrect
129 * wiring inside the silicon. Only mode 0 works correctly.
131 reg &= ~(MT7621_CPHA | MT7621_CPOL);
133 mt7621_spi_write(rs, MT7621_SPI_MASTER, reg);
138 static inline int mt7621_spi_wait_till_ready(struct mt7621_spi *rs)
142 for (i = 0; i < RALINK_SPI_WAIT_MAX_LOOP; i++) {
145 status = mt7621_spi_read(rs, MT7621_SPI_TRANS);
146 if ((status & SPITRANS_BUSY) == 0)
155 static void mt7621_spi_read_half_duplex(struct mt7621_spi *rs,
161 * Combine with any pending write, and perform one or more half-duplex
162 * transactions reading 'len' bytes. Data to be written is already in
165 tx_len = rs->pending_write;
166 rs->pending_write = 0;
168 while (rx_len || tx_len) {
170 u32 val = (min(tx_len, 4) * 8) << 24;
171 int rx = min(rx_len, 32);
174 val |= (tx_len - 4) * 8;
175 val |= (rx * 8) << 12;
176 mt7621_spi_write(rs, MT7621_SPI_MOREBUF, val);
180 val = mt7621_spi_read(rs, MT7621_SPI_TRANS);
181 val |= SPI_CTL_START;
182 mt7621_spi_write(rs, MT7621_SPI_TRANS, val);
184 mt7621_spi_wait_till_ready(rs);
186 for (i = 0; i < rx; i++) {
188 val = mt7621_spi_read(rs, MT7621_SPI_DATA0 + i);
197 static inline void mt7621_spi_flush(struct mt7621_spi *rs)
199 mt7621_spi_read_half_duplex(rs, 0, NULL);
202 static void mt7621_spi_write_half_duplex(struct mt7621_spi *rs,
203 int tx_len, const u8 *buf)
205 int len = rs->pending_write;
209 val = mt7621_spi_read(rs, MT7621_SPI_OPCODE + (len & ~3));
211 val <<= (4 - len) * 8;
218 rs->pending_write = len;
219 mt7621_spi_flush(rs);
223 val |= *buf++ << (8 * (len & 3));
225 if ((len & 3) == 0) {
227 /* The byte-order of the opcode is weird! */
229 mt7621_spi_write(rs, MT7621_SPI_OPCODE + len - 4, val);
238 val >>= (4 - len) * 8;
240 mt7621_spi_write(rs, MT7621_SPI_OPCODE + (len & ~3), val);
243 rs->pending_write = len;
246 static int mt7621_spi_transfer_one_message(struct spi_controller *master,
247 struct spi_message *m)
249 struct mt7621_spi *rs = spi_controller_get_devdata(master);
250 struct spi_device *spi = m->spi;
251 unsigned int speed = spi->max_speed_hz;
252 struct spi_transfer *t = NULL;
255 mt7621_spi_wait_till_ready(rs);
257 list_for_each_entry(t, &m->transfers, transfer_list)
258 if (t->speed_hz < speed)
261 if (mt7621_spi_prepare(spi, speed)) {
267 mt7621_spi_set_cs(spi, 1);
269 m->actual_length = 0;
270 list_for_each_entry(t, &m->transfers, transfer_list) {
271 if ((t->rx_buf) && (t->tx_buf)) {
273 * This controller will shift some extra data out
274 * of spi_opcode if (mosi_bit_cnt > 0) &&
275 * (cmd_bit_cnt == 0). So the claimed full-duplex
276 * support is broken since we have no way to read
277 * the MISO value during that bit.
281 } else if (t->rx_buf) {
282 mt7621_spi_read_half_duplex(rs, t->len, t->rx_buf);
283 } else if (t->tx_buf) {
284 mt7621_spi_write_half_duplex(rs, t->len, t->tx_buf);
286 m->actual_length += t->len;
289 /* Flush data and deassert CS */
290 mt7621_spi_flush(rs);
291 mt7621_spi_set_cs(spi, 0);
295 spi_finalize_current_message(master);
300 static int mt7621_spi_setup(struct spi_device *spi)
302 struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
304 if ((spi->max_speed_hz == 0) ||
305 (spi->max_speed_hz > (rs->sys_freq / 2)))
306 spi->max_speed_hz = rs->sys_freq / 2;
308 if (spi->max_speed_hz < (rs->sys_freq / 4097)) {
309 dev_err(&spi->dev, "setup: requested speed is too low %d Hz\n",
317 static const struct of_device_id mt7621_spi_match[] = {
318 { .compatible = "ralink,mt7621-spi" },
321 MODULE_DEVICE_TABLE(of, mt7621_spi_match);
323 static int mt7621_spi_probe(struct platform_device *pdev)
325 const struct of_device_id *match;
326 struct spi_controller *master;
327 struct mt7621_spi *rs;
332 match = of_match_device(mt7621_spi_match, &pdev->dev);
336 base = devm_platform_ioremap_resource(pdev, 0);
338 return PTR_ERR(base);
340 clk = devm_clk_get_enabled(&pdev->dev, NULL);
342 return dev_err_probe(&pdev->dev, PTR_ERR(clk),
343 "unable to get SYS clock\n");
345 master = devm_spi_alloc_master(&pdev->dev, sizeof(*rs));
347 dev_info(&pdev->dev, "master allocation failed\n");
351 master->mode_bits = SPI_LSB_FIRST;
352 master->flags = SPI_CONTROLLER_HALF_DUPLEX;
353 master->setup = mt7621_spi_setup;
354 master->transfer_one_message = mt7621_spi_transfer_one_message;
355 master->bits_per_word_mask = SPI_BPW_MASK(8);
356 master->dev.of_node = pdev->dev.of_node;
357 master->num_chipselect = 2;
359 dev_set_drvdata(&pdev->dev, master);
361 rs = spi_controller_get_devdata(master);
364 rs->sys_freq = clk_get_rate(clk);
365 rs->pending_write = 0;
366 dev_info(&pdev->dev, "sys_freq: %u\n", rs->sys_freq);
368 ret = device_reset(&pdev->dev);
370 dev_err(&pdev->dev, "SPI reset failed!\n");
374 return devm_spi_register_controller(&pdev->dev, master);
377 MODULE_ALIAS("platform:" DRIVER_NAME);
379 static struct platform_driver mt7621_spi_driver = {
382 .of_match_table = mt7621_spi_match,
384 .probe = mt7621_spi_probe,
387 module_platform_driver(mt7621_spi_driver);
389 MODULE_DESCRIPTION("MT7621 SPI driver");
390 MODULE_AUTHOR("Felix Fietkau <nbd@nbd.name>");
391 MODULE_LICENSE("GPL");