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>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/reset.h>
21 #include <linux/spi/spi.h>
23 #define DRIVER_NAME "spi-mt7621"
26 #define RALINK_SPI_WAIT_MAX_LOOP 2000
28 /* SPISTAT register bit field */
29 #define SPISTAT_BUSY BIT(0)
31 #define MT7621_SPI_TRANS 0x00
32 #define SPITRANS_BUSY BIT(16)
34 #define MT7621_SPI_OPCODE 0x04
35 #define MT7621_SPI_DATA0 0x08
36 #define MT7621_SPI_DATA4 0x18
37 #define SPI_CTL_TX_RX_CNT_MASK 0xff
38 #define SPI_CTL_START BIT(8)
40 #define MT7621_SPI_MASTER 0x28
41 #define MASTER_MORE_BUFMODE BIT(2)
42 #define MASTER_FULL_DUPLEX BIT(10)
43 #define MASTER_RS_CLK_SEL GENMASK(27, 16)
44 #define MASTER_RS_CLK_SEL_SHIFT 16
45 #define MASTER_RS_SLAVE_SEL GENMASK(31, 29)
47 #define MT7621_SPI_MOREBUF 0x2c
48 #define MT7621_SPI_POLAR 0x38
49 #define MT7621_SPI_SPACE 0x3c
51 #define MT7621_CPHA BIT(5)
52 #define MT7621_CPOL BIT(4)
53 #define MT7621_LSB_FIRST BIT(3)
55 #define MT7621_NATIVE_CS_COUNT 2
58 struct spi_controller
*host
;
60 unsigned int sys_freq
;
65 static inline struct mt7621_spi
*spidev_to_mt7621_spi(struct spi_device
*spi
)
67 return spi_controller_get_devdata(spi
->controller
);
70 static inline u32
mt7621_spi_read(struct mt7621_spi
*rs
, u32 reg
)
72 return ioread32(rs
->base
+ reg
);
75 static inline void mt7621_spi_write(struct mt7621_spi
*rs
, u32 reg
, u32 val
)
77 iowrite32(val
, rs
->base
+ reg
);
80 static void mt7621_spi_set_native_cs(struct spi_device
*spi
, bool enable
)
82 struct mt7621_spi
*rs
= spidev_to_mt7621_spi(spi
);
83 int cs
= spi_get_chipselect(spi
, 0);
84 bool active
= spi
->mode
& SPI_CS_HIGH
? enable
: !enable
;
89 * Select SPI device 7, enable "more buffer mode" and disable
90 * full-duplex (only half-duplex really works on this chip
93 host
= mt7621_spi_read(rs
, MT7621_SPI_MASTER
);
94 host
|= MASTER_RS_SLAVE_SEL
| MASTER_MORE_BUFMODE
;
95 host
&= ~MASTER_FULL_DUPLEX
;
96 mt7621_spi_write(rs
, MT7621_SPI_MASTER
, host
);
98 rs
->pending_write
= 0;
102 mt7621_spi_write(rs
, MT7621_SPI_POLAR
, polar
);
105 static int mt7621_spi_prepare(struct spi_device
*spi
, unsigned int speed
)
107 struct mt7621_spi
*rs
= spidev_to_mt7621_spi(spi
);
111 dev_dbg(&spi
->dev
, "speed:%u\n", speed
);
113 rate
= DIV_ROUND_UP(rs
->sys_freq
, speed
);
114 dev_dbg(&spi
->dev
, "rate-1:%u\n", rate
);
122 reg
= mt7621_spi_read(rs
, MT7621_SPI_MASTER
);
123 reg
&= ~MASTER_RS_CLK_SEL
;
124 reg
|= (rate
- 2) << MASTER_RS_CLK_SEL_SHIFT
;
127 reg
&= ~MT7621_LSB_FIRST
;
128 if (spi
->mode
& SPI_LSB_FIRST
)
129 reg
|= MT7621_LSB_FIRST
;
132 * This SPI controller seems to be tested on SPI flash only and some
133 * bits are swizzled under other SPI modes probably due to incorrect
134 * wiring inside the silicon. Only mode 0 works correctly.
136 reg
&= ~(MT7621_CPHA
| MT7621_CPOL
);
138 mt7621_spi_write(rs
, MT7621_SPI_MASTER
, reg
);
143 static inline int mt7621_spi_wait_till_ready(struct mt7621_spi
*rs
)
147 for (i
= 0; i
< RALINK_SPI_WAIT_MAX_LOOP
; i
++) {
150 status
= mt7621_spi_read(rs
, MT7621_SPI_TRANS
);
151 if ((status
& SPITRANS_BUSY
) == 0)
160 static int mt7621_spi_prepare_message(struct spi_controller
*host
,
161 struct spi_message
*m
)
163 struct mt7621_spi
*rs
= spi_controller_get_devdata(host
);
164 struct spi_device
*spi
= m
->spi
;
165 unsigned int speed
= spi
->max_speed_hz
;
166 struct spi_transfer
*t
= NULL
;
168 mt7621_spi_wait_till_ready(rs
);
170 list_for_each_entry(t
, &m
->transfers
, transfer_list
)
171 if (t
->speed_hz
< speed
)
174 return mt7621_spi_prepare(spi
, speed
);
177 static void mt7621_spi_read_half_duplex(struct mt7621_spi
*rs
,
183 * Combine with any pending write, and perform one or more half-duplex
184 * transactions reading 'len' bytes. Data to be written is already in
187 tx_len
= rs
->pending_write
;
188 rs
->pending_write
= 0;
190 while (rx_len
|| tx_len
) {
192 u32 val
= (min(tx_len
, 4) * 8) << 24;
193 int rx
= min(rx_len
, 32);
196 val
|= (tx_len
- 4) * 8;
197 val
|= (rx
* 8) << 12;
198 mt7621_spi_write(rs
, MT7621_SPI_MOREBUF
, val
);
202 val
= mt7621_spi_read(rs
, MT7621_SPI_TRANS
);
203 val
|= SPI_CTL_START
;
204 mt7621_spi_write(rs
, MT7621_SPI_TRANS
, val
);
206 mt7621_spi_wait_till_ready(rs
);
208 for (i
= 0; i
< rx
; i
++) {
210 val
= mt7621_spi_read(rs
, MT7621_SPI_DATA0
+ i
);
219 static inline void mt7621_spi_flush(struct mt7621_spi
*rs
)
221 mt7621_spi_read_half_duplex(rs
, 0, NULL
);
224 static void mt7621_spi_write_half_duplex(struct mt7621_spi
*rs
,
225 int tx_len
, const u8
*buf
)
227 int len
= rs
->pending_write
;
231 val
= mt7621_spi_read(rs
, MT7621_SPI_OPCODE
+ (len
& ~3));
233 val
<<= (4 - len
) * 8;
240 rs
->pending_write
= len
;
241 mt7621_spi_flush(rs
);
245 val
|= *buf
++ << (8 * (len
& 3));
247 if ((len
& 3) == 0) {
249 /* The byte-order of the opcode is weird! */
251 mt7621_spi_write(rs
, MT7621_SPI_OPCODE
+ len
- 4, val
);
260 val
>>= (4 - len
) * 8;
262 mt7621_spi_write(rs
, MT7621_SPI_OPCODE
+ (len
& ~3), val
);
265 rs
->pending_write
= len
;
266 mt7621_spi_flush(rs
);
269 static int mt7621_spi_transfer_one(struct spi_controller
*host
,
270 struct spi_device
*spi
,
271 struct spi_transfer
*t
)
273 struct mt7621_spi
*rs
= spi_controller_get_devdata(host
);
275 if ((t
->rx_buf
) && (t
->tx_buf
)) {
277 * This controller will shift some extra data out
278 * of spi_opcode if (mosi_bit_cnt > 0) &&
279 * (cmd_bit_cnt == 0). So the claimed full-duplex
280 * support is broken since we have no way to read
281 * the MISO value during that bit.
284 } else if (t
->rx_buf
) {
285 mt7621_spi_read_half_duplex(rs
, t
->len
, t
->rx_buf
);
286 } else if (t
->tx_buf
) {
287 mt7621_spi_write_half_duplex(rs
, t
->len
, t
->tx_buf
);
293 static int mt7621_spi_setup(struct spi_device
*spi
)
295 struct mt7621_spi
*rs
= spidev_to_mt7621_spi(spi
);
297 if ((spi
->max_speed_hz
== 0) ||
298 (spi
->max_speed_hz
> (rs
->sys_freq
/ 2)))
299 spi
->max_speed_hz
= rs
->sys_freq
/ 2;
301 if (spi
->max_speed_hz
< (rs
->sys_freq
/ 4097)) {
302 dev_err(&spi
->dev
, "setup: requested speed is too low %d Hz\n",
310 static const struct of_device_id mt7621_spi_match
[] = {
311 { .compatible
= "ralink,mt7621-spi" },
314 MODULE_DEVICE_TABLE(of
, mt7621_spi_match
);
316 static int mt7621_spi_probe(struct platform_device
*pdev
)
318 const struct of_device_id
*match
;
319 struct spi_controller
*host
;
320 struct mt7621_spi
*rs
;
325 match
= of_match_device(mt7621_spi_match
, &pdev
->dev
);
329 base
= devm_platform_ioremap_resource(pdev
, 0);
331 return PTR_ERR(base
);
333 clk
= devm_clk_get_enabled(&pdev
->dev
, NULL
);
335 return dev_err_probe(&pdev
->dev
, PTR_ERR(clk
),
336 "unable to get SYS clock\n");
338 host
= devm_spi_alloc_host(&pdev
->dev
, sizeof(*rs
));
340 dev_info(&pdev
->dev
, "host allocation failed\n");
344 host
->mode_bits
= SPI_LSB_FIRST
;
345 host
->flags
= SPI_CONTROLLER_HALF_DUPLEX
;
346 host
->setup
= mt7621_spi_setup
;
347 host
->prepare_message
= mt7621_spi_prepare_message
;
348 host
->set_cs
= mt7621_spi_set_native_cs
;
349 host
->transfer_one
= mt7621_spi_transfer_one
;
350 host
->bits_per_word_mask
= SPI_BPW_MASK(8);
351 host
->dev
.of_node
= pdev
->dev
.of_node
;
352 host
->max_native_cs
= MT7621_NATIVE_CS_COUNT
;
353 host
->num_chipselect
= MT7621_NATIVE_CS_COUNT
;
354 host
->use_gpio_descriptors
= true;
356 dev_set_drvdata(&pdev
->dev
, host
);
358 rs
= spi_controller_get_devdata(host
);
361 rs
->sys_freq
= clk_get_rate(clk
);
362 rs
->pending_write
= 0;
363 dev_info(&pdev
->dev
, "sys_freq: %u\n", rs
->sys_freq
);
365 ret
= device_reset(&pdev
->dev
);
367 dev_err(&pdev
->dev
, "SPI reset failed!\n");
371 return devm_spi_register_controller(&pdev
->dev
, host
);
374 MODULE_ALIAS("platform:" DRIVER_NAME
);
376 static struct platform_driver mt7621_spi_driver
= {
379 .of_match_table
= mt7621_spi_match
,
381 .probe
= mt7621_spi_probe
,
384 module_platform_driver(mt7621_spi_driver
);
386 MODULE_DESCRIPTION("MT7621 SPI driver");
387 MODULE_AUTHOR("Felix Fietkau <nbd@nbd.name>");
388 MODULE_LICENSE("GPL");