blob: b37f859b98210127a8fabf9a61f8c63042d9d437 [file] [log] [blame]
Stefan Roese5eee9de2018-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>
Weijie Gaof0997852019-09-25 17:45:23 +080012#include <clk.h>
Stefan Roese5eee9de2018-08-16 10:48:48 +020013#include <dm.h>
14#include <spi.h>
15#include <wait_bit.h>
16#include <linux/io.h>
17
18#define SPI_MSG_SIZE_MAX 32 /* SPI message chunk size */
19/* Enough for SPI NAND page read / write with page size 2048 bytes */
20#define SPI_MSG_SIZE_OVERALL (2048 + 16)
21
22#define MT7621_SPI_TRANS 0x00
23#define MT7621_SPI_TRANS_START BIT(8)
24#define MT7621_SPI_TRANS_BUSY BIT(16)
25
26#define MT7621_SPI_OPCODE 0x04
27#define MT7621_SPI_DATA0 0x08
28#define MT7621_SPI_DATA4 0x18
29#define MT7621_SPI_MASTER 0x28
30#define MT7621_SPI_MOREBUF 0x2c
31#define MT7621_SPI_POLAR 0x38
32
33#define MT7621_LSB_FIRST BIT(3)
34#define MT7621_CPOL BIT(4)
35#define MT7621_CPHA BIT(5)
36
37#define MASTER_MORE_BUFMODE BIT(2)
38#define MASTER_RS_CLK_SEL GENMASK(27, 16)
39#define MASTER_RS_CLK_SEL_SHIFT 16
40#define MASTER_RS_SLAVE_SEL GENMASK(31, 29)
41
42struct mt7621_spi {
43 void __iomem *base;
44 unsigned int sys_freq;
45 u32 data[(SPI_MSG_SIZE_OVERALL / 4) + 1];
46 int tx_len;
47};
48
49static void mt7621_spi_reset(struct mt7621_spi *rs, int duplex)
50{
51 setbits_le32(rs->base + MT7621_SPI_MASTER,
52 MASTER_RS_SLAVE_SEL | MASTER_MORE_BUFMODE);
53}
54
55static void mt7621_spi_set_cs(struct mt7621_spi *rs, int cs, int enable)
56{
57 u32 val = 0;
58
59 debug("%s: cs#%d -> %s\n", __func__, cs, enable ? "enable" : "disable");
60 if (enable)
61 val = BIT(cs);
62 iowrite32(val, rs->base + MT7621_SPI_POLAR);
63}
64
65static int mt7621_spi_set_mode(struct udevice *bus, uint mode)
66{
67 struct mt7621_spi *rs = dev_get_priv(bus);
68 u32 reg;
69
70 debug("%s: mode=0x%08x\n", __func__, mode);
71 reg = ioread32(rs->base + MT7621_SPI_MASTER);
72
73 reg &= ~MT7621_LSB_FIRST;
74 if (mode & SPI_LSB_FIRST)
75 reg |= MT7621_LSB_FIRST;
76
77 reg &= ~(MT7621_CPHA | MT7621_CPOL);
78 switch (mode & (SPI_CPOL | SPI_CPHA)) {
79 case SPI_MODE_0:
80 break;
81 case SPI_MODE_1:
82 reg |= MT7621_CPHA;
83 break;
84 case SPI_MODE_2:
85 reg |= MT7621_CPOL;
86 break;
87 case SPI_MODE_3:
88 reg |= MT7621_CPOL | MT7621_CPHA;
89 break;
90 }
91 iowrite32(reg, rs->base + MT7621_SPI_MASTER);
92
93 return 0;
94}
95
96static int mt7621_spi_set_speed(struct udevice *bus, uint speed)
97{
98 struct mt7621_spi *rs = dev_get_priv(bus);
99 u32 rate;
100 u32 reg;
101
102 debug("%s: speed=%d\n", __func__, speed);
103 rate = DIV_ROUND_UP(rs->sys_freq, speed);
104 debug("rate:%u\n", rate);
105
106 if (rate > 4097)
107 return -EINVAL;
108
109 if (rate < 2)
110 rate = 2;
111
112 reg = ioread32(rs->base + MT7621_SPI_MASTER);
113 reg &= ~MASTER_RS_CLK_SEL;
114 reg |= (rate - 2) << MASTER_RS_CLK_SEL_SHIFT;
115 iowrite32(reg, rs->base + MT7621_SPI_MASTER);
116
117 return 0;
118}
119
120static inline int mt7621_spi_wait_till_ready(struct mt7621_spi *rs)
121{
122 int ret;
123
124 ret = wait_for_bit_le32(rs->base + MT7621_SPI_TRANS,
125 MT7621_SPI_TRANS_BUSY, 0, 10, 0);
126 if (ret)
127 pr_err("Timeout in %s!\n", __func__);
128
129 return ret;
130}
131
132static int mt7621_spi_xfer(struct udevice *dev, unsigned int bitlen,
133 const void *dout, void *din, unsigned long flags)
134{
135 struct udevice *bus = dev->parent;
136 struct mt7621_spi *rs = dev_get_priv(bus);
137 const u8 *tx_buf = dout;
138 u8 *ptr = (u8 *)dout;
139 u8 *rx_buf = din;
140 int total_size = bitlen >> 3;
141 int chunk_size;
142 int rx_len = 0;
143 u32 data[(SPI_MSG_SIZE_MAX / 4) + 1] = { 0 };
144 u32 val;
145 int i;
146
147 debug("%s: dout=%p, din=%p, len=%x, flags=%lx\n", __func__, dout, din,
148 total_size, flags);
149
150 /*
151 * This driver only supports half-duplex, so complain and bail out
152 * upon full-duplex messages
153 */
154 if (dout && din) {
155 printf("Only half-duplex SPI transfer supported\n");
156 return -EIO;
157 }
158
159 if (dout) {
160 debug("TX-DATA: ");
161 for (i = 0; i < total_size; i++)
162 debug("%02x ", *ptr++);
163 debug("\n");
164 }
165
166 mt7621_spi_wait_till_ready(rs);
167
168 /*
169 * Set CS active upon start of SPI message. This message can
170 * be split upon multiple calls to this xfer function
171 */
172 if (flags & SPI_XFER_BEGIN)
173 mt7621_spi_set_cs(rs, spi_chip_select(dev), 1);
174
175 while (total_size > 0) {
176 /* Don't exceed the max xfer size */
177 chunk_size = min_t(int, total_size, SPI_MSG_SIZE_MAX);
178
179 /*
180 * We might have some TX data buffered from the last xfer
181 * message. Make sure, that this does not exceed the max
182 * xfer size
183 */
184 if (rs->tx_len > 4)
185 chunk_size -= rs->tx_len;
186 if (din)
187 rx_len = chunk_size;
188
189 if (tx_buf) {
190 /* Check if this message does not exceed the buffer */
191 if ((chunk_size + rs->tx_len) > SPI_MSG_SIZE_OVERALL) {
192 printf("TX message size too big (%d)\n",
193 chunk_size + rs->tx_len);
194 return -EMSGSIZE;
195 }
196
197 /*
198 * Write all TX data into internal buffer to collect
199 * all TX messages into one buffer (might be split into
200 * multiple calls to this function)
201 */
202 for (i = 0; i < chunk_size; i++, rs->tx_len++) {
203 rs->data[rs->tx_len / 4] |=
204 tx_buf[i] << (8 * (rs->tx_len & 3));
205 }
206 }
207
208 if (flags & SPI_XFER_END) {
209 /* Write TX data into controller */
210 if (rs->tx_len) {
211 rs->data[0] = swab32(rs->data[0]);
212 if (rs->tx_len < 4)
213 rs->data[0] >>= (4 - rs->tx_len) * 8;
214
215 for (i = 0; i < rs->tx_len; i += 4) {
216 iowrite32(rs->data[i / 4], rs->base +
217 MT7621_SPI_OPCODE + i);
218 }
219 }
220
221 /* Write length into controller */
222 val = (min_t(int, rs->tx_len, 4) * 8) << 24;
223 if (rs->tx_len > 4)
224 val |= (rs->tx_len - 4) * 8;
225 val |= (rx_len * 8) << 12;
226 iowrite32(val, rs->base + MT7621_SPI_MOREBUF);
227
228 /* Start the xfer */
229 setbits_le32(rs->base + MT7621_SPI_TRANS,
230 MT7621_SPI_TRANS_START);
231
232 /* Wait until xfer is finished on bus */
233 mt7621_spi_wait_till_ready(rs);
234
235 /* Reset TX length and TX buffer for next xfer */
236 rs->tx_len = 0;
237 memset(rs->data, 0, sizeof(rs->data));
238 }
239
240 for (i = 0; i < rx_len; i += 4)
241 data[i / 4] = ioread32(rs->base + MT7621_SPI_DATA0 + i);
242
243 if (rx_len) {
244 debug("RX-DATA: ");
245 for (i = 0; i < rx_len; i++) {
246 rx_buf[i] = data[i / 4] >> (8 * (i & 3));
247 debug("%02x ", rx_buf[i]);
248 }
249 debug("\n");
250 }
251
252 if (tx_buf)
253 tx_buf += chunk_size;
254 if (rx_buf)
255 rx_buf += chunk_size;
256 total_size -= chunk_size;
257 }
258
259 /* Wait until xfer is finished on bus and de-assert CS */
260 mt7621_spi_wait_till_ready(rs);
261 if (flags & SPI_XFER_END)
262 mt7621_spi_set_cs(rs, spi_chip_select(dev), 0);
263
264 return 0;
265}
266
267static int mt7621_spi_probe(struct udevice *dev)
268{
269 struct mt7621_spi *rs = dev_get_priv(dev);
Weijie Gaof0997852019-09-25 17:45:23 +0800270 struct clk clk;
271 int ret;
Stefan Roese5eee9de2018-08-16 10:48:48 +0200272
273 rs->base = dev_remap_addr(dev);
274 if (!rs->base)
275 return -EINVAL;
276
Weijie Gaof0997852019-09-25 17:45:23 +0800277 ret = clk_get_by_index(dev, 0, &clk);
278 if (ret < 0) {
279 printf("Please provide a clock!\n");
280 return ret;
281 }
282
283 clk_enable(&clk);
284
285 rs->sys_freq = clk_get_rate(&clk);
Stefan Roese5eee9de2018-08-16 10:48:48 +0200286 if (!rs->sys_freq) {
Weijie Gaof0997852019-09-25 17:45:23 +0800287 printf("Please provide a valid clock!\n");
Stefan Roese5eee9de2018-08-16 10:48:48 +0200288 return -EINVAL;
289 }
290
291 mt7621_spi_reset(rs, 0);
292
293 return 0;
294}
295
296static const struct dm_spi_ops mt7621_spi_ops = {
297 .set_mode = mt7621_spi_set_mode,
298 .set_speed = mt7621_spi_set_speed,
299 .xfer = mt7621_spi_xfer,
300 /*
301 * cs_info is not needed, since we require all chip selects to be
302 * in the device tree explicitly
303 */
304};
305
306static const struct udevice_id mt7621_spi_ids[] = {
307 { .compatible = "ralink,mt7621-spi" },
308 { }
309};
310
311U_BOOT_DRIVER(mt7621_spi) = {
312 .name = "mt7621_spi",
313 .id = UCLASS_SPI,
314 .of_match = mt7621_spi_ids,
315 .ops = &mt7621_spi_ops,
316 .priv_auto_alloc_size = sizeof(struct mt7621_spi),
317 .probe = mt7621_spi_probe,
318};