blob: df9d9da1a9bb920f1c1f2cffce4b31ec9e5ca430 [file] [log] [blame]
Stefan Roese8adb8cb2018-08-16 10:48:48 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 Stefan Roese <sr@denx.de>
4 *
5 * Derived from the Linux driver version drivers/spi/spi-mt7621.c
6 * Copyright (C) 2011 Sergiy <piratfm@gmail.com>
7 * Copyright (C) 2011-2013 Gabor Juhos <juhosg@openwrt.org>
8 * Copyright (C) 2014-2015 Felix Fietkau <nbd@nbd.name>
9 */
10
11#include <common.h>
developer7ad7bee2019-09-25 17:45:23 +080012#include <clk.h>
Stefan Roese8adb8cb2018-08-16 10:48:48 +020013#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Stefan Roese8adb8cb2018-08-16 10:48:48 +020015#include <spi.h>
16#include <wait_bit.h>
17#include <linux/io.h>
18
developerfe93f592019-09-25 17:45:24 +080019#define MT7621_RX_FIFO_LEN 32
20#define MT7621_TX_FIFO_LEN 36
Stefan Roese8adb8cb2018-08-16 10:48:48 +020021
22#define MT7621_SPI_TRANS 0x00
23#define MT7621_SPI_TRANS_START BIT(8)
24#define MT7621_SPI_TRANS_BUSY BIT(16)
developera546d152019-09-25 17:45:25 +080025#define TRANS_ADDR_SZ GENMASK(20, 19)
26#define TRANS_ADDR_SZ_SHIFT 19
27#define TRANS_MOSI_BCNT GENMASK(3, 0)
28#define TRANS_MOSI_BCNT_SHIFT 0
Stefan Roese8adb8cb2018-08-16 10:48:48 +020029
30#define MT7621_SPI_OPCODE 0x04
31#define MT7621_SPI_DATA0 0x08
32#define MT7621_SPI_DATA4 0x18
33#define MT7621_SPI_MASTER 0x28
34#define MT7621_SPI_MOREBUF 0x2c
35#define MT7621_SPI_POLAR 0x38
36
37#define MT7621_LSB_FIRST BIT(3)
38#define MT7621_CPOL BIT(4)
39#define MT7621_CPHA BIT(5)
40
41#define MASTER_MORE_BUFMODE BIT(2)
42#define MASTER_RS_CLK_SEL GENMASK(27, 16)
43#define MASTER_RS_CLK_SEL_SHIFT 16
44#define MASTER_RS_SLAVE_SEL GENMASK(31, 29)
45
developerfe93f592019-09-25 17:45:24 +080046#define MOREBUF_CMD_CNT GENMASK(29, 24)
47#define MOREBUF_CMD_CNT_SHIFT 24
48#define MOREBUF_MISO_CNT GENMASK(20, 12)
49#define MOREBUF_MISO_CNT_SHIFT 12
50#define MOREBUF_MOSI_CNT GENMASK(8, 0)
51#define MOREBUF_MOSI_CNT_SHIFT 0
52
Stefan Roese8adb8cb2018-08-16 10:48:48 +020053struct mt7621_spi {
54 void __iomem *base;
55 unsigned int sys_freq;
Stefan Roese8adb8cb2018-08-16 10:48:48 +020056};
57
Stefan Roese8adb8cb2018-08-16 10:48:48 +020058static void mt7621_spi_set_cs(struct mt7621_spi *rs, int cs, int enable)
59{
Stefan Roese8adb8cb2018-08-16 10:48:48 +020060 debug("%s: cs#%d -> %s\n", __func__, cs, enable ? "enable" : "disable");
developera546d152019-09-25 17:45:25 +080061
62 if (enable) {
63 setbits_le32(rs->base + MT7621_SPI_MASTER,
64 MASTER_RS_SLAVE_SEL | MASTER_MORE_BUFMODE);
65 iowrite32(BIT(cs), rs->base + MT7621_SPI_POLAR);
66 } else {
67 iowrite32(0, rs->base + MT7621_SPI_POLAR);
68 iowrite32((2 << TRANS_ADDR_SZ_SHIFT) |
69 (1 << TRANS_MOSI_BCNT_SHIFT),
70 rs->base + MT7621_SPI_TRANS);
71 clrbits_le32(rs->base + MT7621_SPI_MASTER,
72 MASTER_RS_SLAVE_SEL | MASTER_MORE_BUFMODE);
73 }
Stefan Roese8adb8cb2018-08-16 10:48:48 +020074}
75
76static int mt7621_spi_set_mode(struct udevice *bus, uint mode)
77{
78 struct mt7621_spi *rs = dev_get_priv(bus);
79 u32 reg;
80
81 debug("%s: mode=0x%08x\n", __func__, mode);
82 reg = ioread32(rs->base + MT7621_SPI_MASTER);
83
84 reg &= ~MT7621_LSB_FIRST;
85 if (mode & SPI_LSB_FIRST)
86 reg |= MT7621_LSB_FIRST;
87
88 reg &= ~(MT7621_CPHA | MT7621_CPOL);
89 switch (mode & (SPI_CPOL | SPI_CPHA)) {
90 case SPI_MODE_0:
91 break;
92 case SPI_MODE_1:
93 reg |= MT7621_CPHA;
94 break;
95 case SPI_MODE_2:
96 reg |= MT7621_CPOL;
97 break;
98 case SPI_MODE_3:
99 reg |= MT7621_CPOL | MT7621_CPHA;
100 break;
101 }
102 iowrite32(reg, rs->base + MT7621_SPI_MASTER);
103
104 return 0;
105}
106
107static int mt7621_spi_set_speed(struct udevice *bus, uint speed)
108{
109 struct mt7621_spi *rs = dev_get_priv(bus);
110 u32 rate;
111 u32 reg;
112
113 debug("%s: speed=%d\n", __func__, speed);
114 rate = DIV_ROUND_UP(rs->sys_freq, speed);
115 debug("rate:%u\n", rate);
116
117 if (rate > 4097)
118 return -EINVAL;
119
120 if (rate < 2)
121 rate = 2;
122
123 reg = ioread32(rs->base + MT7621_SPI_MASTER);
124 reg &= ~MASTER_RS_CLK_SEL;
125 reg |= (rate - 2) << MASTER_RS_CLK_SEL_SHIFT;
126 iowrite32(reg, rs->base + MT7621_SPI_MASTER);
127
128 return 0;
129}
130
131static inline int mt7621_spi_wait_till_ready(struct mt7621_spi *rs)
132{
133 int ret;
134
135 ret = wait_for_bit_le32(rs->base + MT7621_SPI_TRANS,
136 MT7621_SPI_TRANS_BUSY, 0, 10, 0);
137 if (ret)
138 pr_err("Timeout in %s!\n", __func__);
139
140 return ret;
141}
142
developerfe93f592019-09-25 17:45:24 +0800143static int mt7621_spi_read(struct mt7621_spi *rs, u8 *buf, size_t len)
144{
145 size_t rx_len;
146 int i, ret;
147 u32 val = 0;
148
149 while (len) {
150 rx_len = min_t(size_t, len, MT7621_RX_FIFO_LEN);
151
152 iowrite32((rx_len * 8) << MOREBUF_MISO_CNT_SHIFT,
153 rs->base + MT7621_SPI_MOREBUF);
154 iowrite32(MT7621_SPI_TRANS_START, rs->base + MT7621_SPI_TRANS);
155
156 ret = mt7621_spi_wait_till_ready(rs);
157 if (ret)
158 return ret;
159
160 for (i = 0; i < rx_len; i++) {
161 if ((i % 4) == 0)
162 val = ioread32(rs->base + MT7621_SPI_DATA0 + i);
163 *buf++ = val & 0xff;
164 val >>= 8;
165 }
166
167 len -= rx_len;
168 }
169
170 return ret;
171}
172
173static int mt7621_spi_write(struct mt7621_spi *rs, const u8 *buf, size_t len)
174{
175 size_t tx_len, opcode_len, dido_len;
176 int i, ret;
177 u32 val;
178
179 while (len) {
180 tx_len = min_t(size_t, len, MT7621_TX_FIFO_LEN);
181
182 opcode_len = min_t(size_t, tx_len, 4);
183 dido_len = tx_len - opcode_len;
184
185 val = 0;
186 for (i = 0; i < opcode_len; i++) {
187 val <<= 8;
188 val |= *buf++;
189 }
190
191 iowrite32(val, rs->base + MT7621_SPI_OPCODE);
192
193 val = 0;
194 for (i = 0; i < dido_len; i++) {
195 val |= (*buf++) << ((i % 4) * 8);
196
197 if ((i % 4 == 3) || (i == dido_len - 1)) {
198 iowrite32(val, rs->base + MT7621_SPI_DATA0 +
199 (i & ~3));
200 val = 0;
201 }
202 }
203
204 iowrite32(((opcode_len * 8) << MOREBUF_CMD_CNT_SHIFT) |
205 ((dido_len * 8) << MOREBUF_MOSI_CNT_SHIFT),
206 rs->base + MT7621_SPI_MOREBUF);
207 iowrite32(MT7621_SPI_TRANS_START, rs->base + MT7621_SPI_TRANS);
208
209 ret = mt7621_spi_wait_till_ready(rs);
210 if (ret)
211 return ret;
212
213 len -= tx_len;
214 }
215
216 return 0;
217}
218
Stefan Roese8adb8cb2018-08-16 10:48:48 +0200219static int mt7621_spi_xfer(struct udevice *dev, unsigned int bitlen,
220 const void *dout, void *din, unsigned long flags)
221{
222 struct udevice *bus = dev->parent;
223 struct mt7621_spi *rs = dev_get_priv(bus);
Stefan Roese8adb8cb2018-08-16 10:48:48 +0200224 int total_size = bitlen >> 3;
developerfe93f592019-09-25 17:45:24 +0800225 int ret = 0;
Stefan Roese8adb8cb2018-08-16 10:48:48 +0200226
227 debug("%s: dout=%p, din=%p, len=%x, flags=%lx\n", __func__, dout, din,
228 total_size, flags);
229
230 /*
231 * This driver only supports half-duplex, so complain and bail out
232 * upon full-duplex messages
233 */
234 if (dout && din) {
235 printf("Only half-duplex SPI transfer supported\n");
236 return -EIO;
237 }
238
Stefan Roese8adb8cb2018-08-16 10:48:48 +0200239 mt7621_spi_wait_till_ready(rs);
240
241 /*
242 * Set CS active upon start of SPI message. This message can
243 * be split upon multiple calls to this xfer function
244 */
245 if (flags & SPI_XFER_BEGIN)
246 mt7621_spi_set_cs(rs, spi_chip_select(dev), 1);
247
developerfe93f592019-09-25 17:45:24 +0800248 if (din)
249 ret = mt7621_spi_read(rs, din, total_size);
250 else if (dout)
251 ret = mt7621_spi_write(rs, dout, total_size);
Stefan Roese8adb8cb2018-08-16 10:48:48 +0200252
Stefan Roese8adb8cb2018-08-16 10:48:48 +0200253 if (flags & SPI_XFER_END)
254 mt7621_spi_set_cs(rs, spi_chip_select(dev), 0);
255
developerfe93f592019-09-25 17:45:24 +0800256 return ret;
Stefan Roese8adb8cb2018-08-16 10:48:48 +0200257}
258
259static int mt7621_spi_probe(struct udevice *dev)
260{
261 struct mt7621_spi *rs = dev_get_priv(dev);
developer7ad7bee2019-09-25 17:45:23 +0800262 struct clk clk;
263 int ret;
Stefan Roese8adb8cb2018-08-16 10:48:48 +0200264
265 rs->base = dev_remap_addr(dev);
266 if (!rs->base)
267 return -EINVAL;
268
developer7ad7bee2019-09-25 17:45:23 +0800269 ret = clk_get_by_index(dev, 0, &clk);
270 if (ret < 0) {
271 printf("Please provide a clock!\n");
272 return ret;
273 }
274
275 clk_enable(&clk);
276
277 rs->sys_freq = clk_get_rate(&clk);
Stefan Roese8adb8cb2018-08-16 10:48:48 +0200278 if (!rs->sys_freq) {
developer7ad7bee2019-09-25 17:45:23 +0800279 printf("Please provide a valid clock!\n");
Stefan Roese8adb8cb2018-08-16 10:48:48 +0200280 return -EINVAL;
281 }
282
Stefan Roese8adb8cb2018-08-16 10:48:48 +0200283 return 0;
284}
285
286static const struct dm_spi_ops mt7621_spi_ops = {
287 .set_mode = mt7621_spi_set_mode,
288 .set_speed = mt7621_spi_set_speed,
289 .xfer = mt7621_spi_xfer,
290 /*
291 * cs_info is not needed, since we require all chip selects to be
292 * in the device tree explicitly
293 */
294};
295
296static const struct udevice_id mt7621_spi_ids[] = {
297 { .compatible = "ralink,mt7621-spi" },
298 { }
299};
300
301U_BOOT_DRIVER(mt7621_spi) = {
302 .name = "mt7621_spi",
303 .id = UCLASS_SPI,
304 .of_match = mt7621_spi_ids,
305 .ops = &mt7621_spi_ops,
306 .priv_auto_alloc_size = sizeof(struct mt7621_spi),
307 .probe = mt7621_spi_probe,
308};