blob: 0ea4930a0a31ebc6d483388c5c22fbaa7cc1b5d0 [file] [log] [blame]
Bhargav Shah3c34f752019-07-17 04:23:43 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2018 SiFive, Inc.
4 * Copyright 2019 Bhargav Shah <bhargavshah1988@gmail.com>
5 *
6 * SiFive SPI controller driver (master mode only)
7 */
8
9#include <common.h>
10#include <dm.h>
Jagan Teki294b2912020-04-23 22:30:53 +053011#include <dm/device_compat.h>
Bhargav Shah3c34f752019-07-17 04:23:43 +000012#include <malloc.h>
Jagan Teki294b2912020-04-23 22:30:53 +053013#include <spi-mem.h>
Bhargav Shah3c34f752019-07-17 04:23:43 +000014#include <asm/io.h>
15#include <linux/log2.h>
16#include <clk.h>
17
18#define SIFIVE_SPI_MAX_CS 32
19
20#define SIFIVE_SPI_DEFAULT_DEPTH 8
21#define SIFIVE_SPI_DEFAULT_BITS 8
22
23/* register offsets */
24#define SIFIVE_SPI_REG_SCKDIV 0x00 /* Serial clock divisor */
25#define SIFIVE_SPI_REG_SCKMODE 0x04 /* Serial clock mode */
26#define SIFIVE_SPI_REG_CSID 0x10 /* Chip select ID */
27#define SIFIVE_SPI_REG_CSDEF 0x14 /* Chip select default */
28#define SIFIVE_SPI_REG_CSMODE 0x18 /* Chip select mode */
29#define SIFIVE_SPI_REG_DELAY0 0x28 /* Delay control 0 */
30#define SIFIVE_SPI_REG_DELAY1 0x2c /* Delay control 1 */
31#define SIFIVE_SPI_REG_FMT 0x40 /* Frame format */
32#define SIFIVE_SPI_REG_TXDATA 0x48 /* Tx FIFO data */
33#define SIFIVE_SPI_REG_RXDATA 0x4c /* Rx FIFO data */
34#define SIFIVE_SPI_REG_TXMARK 0x50 /* Tx FIFO watermark */
35#define SIFIVE_SPI_REG_RXMARK 0x54 /* Rx FIFO watermark */
36#define SIFIVE_SPI_REG_FCTRL 0x60 /* SPI flash interface control */
37#define SIFIVE_SPI_REG_FFMT 0x64 /* SPI flash instruction format */
38#define SIFIVE_SPI_REG_IE 0x70 /* Interrupt Enable Register */
39#define SIFIVE_SPI_REG_IP 0x74 /* Interrupt Pendings Register */
40
41/* sckdiv bits */
42#define SIFIVE_SPI_SCKDIV_DIV_MASK 0xfffU
43
44/* sckmode bits */
45#define SIFIVE_SPI_SCKMODE_PHA BIT(0)
46#define SIFIVE_SPI_SCKMODE_POL BIT(1)
47#define SIFIVE_SPI_SCKMODE_MODE_MASK (SIFIVE_SPI_SCKMODE_PHA | \
48 SIFIVE_SPI_SCKMODE_POL)
49
50/* csmode bits */
51#define SIFIVE_SPI_CSMODE_MODE_AUTO 0U
52#define SIFIVE_SPI_CSMODE_MODE_HOLD 2U
53#define SIFIVE_SPI_CSMODE_MODE_OFF 3U
54
55/* delay0 bits */
56#define SIFIVE_SPI_DELAY0_CSSCK(x) ((u32)(x))
57#define SIFIVE_SPI_DELAY0_CSSCK_MASK 0xffU
58#define SIFIVE_SPI_DELAY0_SCKCS(x) ((u32)(x) << 16)
59#define SIFIVE_SPI_DELAY0_SCKCS_MASK (0xffU << 16)
60
61/* delay1 bits */
62#define SIFIVE_SPI_DELAY1_INTERCS(x) ((u32)(x))
63#define SIFIVE_SPI_DELAY1_INTERCS_MASK 0xffU
64#define SIFIVE_SPI_DELAY1_INTERXFR(x) ((u32)(x) << 16)
65#define SIFIVE_SPI_DELAY1_INTERXFR_MASK (0xffU << 16)
66
67/* fmt bits */
68#define SIFIVE_SPI_FMT_PROTO_SINGLE 0U
69#define SIFIVE_SPI_FMT_PROTO_DUAL 1U
70#define SIFIVE_SPI_FMT_PROTO_QUAD 2U
71#define SIFIVE_SPI_FMT_PROTO_MASK 3U
72#define SIFIVE_SPI_FMT_ENDIAN BIT(2)
73#define SIFIVE_SPI_FMT_DIR BIT(3)
74#define SIFIVE_SPI_FMT_LEN(x) ((u32)(x) << 16)
75#define SIFIVE_SPI_FMT_LEN_MASK (0xfU << 16)
76
77/* txdata bits */
78#define SIFIVE_SPI_TXDATA_DATA_MASK 0xffU
79#define SIFIVE_SPI_TXDATA_FULL BIT(31)
80
81/* rxdata bits */
82#define SIFIVE_SPI_RXDATA_DATA_MASK 0xffU
83#define SIFIVE_SPI_RXDATA_EMPTY BIT(31)
84
85/* ie and ip bits */
86#define SIFIVE_SPI_IP_TXWM BIT(0)
87#define SIFIVE_SPI_IP_RXWM BIT(1)
88
Jagan Tekiebca2c32020-04-23 22:30:54 +053089/* format protocol */
90#define SIFIVE_SPI_PROTO_QUAD 4 /* 4 lines I/O protocol transfer */
91#define SIFIVE_SPI_PROTO_DUAL 2 /* 2 lines I/O protocol transfer */
92#define SIFIVE_SPI_PROTO_SINGLE 1 /* 1 line I/O protocol transfer */
93
Bhargav Shah3c34f752019-07-17 04:23:43 +000094struct sifive_spi {
95 void *regs; /* base address of the registers */
96 u32 fifo_depth;
97 u32 bits_per_word;
98 u32 cs_inactive; /* Level of the CS pins when inactive*/
99 u32 freq;
100 u32 num_cs;
Jagan Tekiebca2c32020-04-23 22:30:54 +0530101 u8 fmt_proto;
Bhargav Shah3c34f752019-07-17 04:23:43 +0000102};
103
104static void sifive_spi_prep_device(struct sifive_spi *spi,
Jagan Teki351ce7c2020-04-20 16:03:46 +0530105 struct dm_spi_slave_platdata *slave_plat)
Bhargav Shah3c34f752019-07-17 04:23:43 +0000106{
107 /* Update the chip select polarity */
Jagan Teki351ce7c2020-04-20 16:03:46 +0530108 if (slave_plat->mode & SPI_CS_HIGH)
109 spi->cs_inactive &= ~BIT(slave_plat->cs);
Bhargav Shah3c34f752019-07-17 04:23:43 +0000110 else
Jagan Teki351ce7c2020-04-20 16:03:46 +0530111 spi->cs_inactive |= BIT(slave_plat->cs);
Bhargav Shah3c34f752019-07-17 04:23:43 +0000112 writel(spi->cs_inactive, spi->regs + SIFIVE_SPI_REG_CSDEF);
113
114 /* Select the correct device */
Jagan Teki351ce7c2020-04-20 16:03:46 +0530115 writel(slave_plat->cs, spi->regs + SIFIVE_SPI_REG_CSID);
Bhargav Shah3c34f752019-07-17 04:23:43 +0000116}
117
118static int sifive_spi_set_cs(struct sifive_spi *spi,
Jagan Teki351ce7c2020-04-20 16:03:46 +0530119 struct dm_spi_slave_platdata *slave_plat)
Bhargav Shah3c34f752019-07-17 04:23:43 +0000120{
121 u32 cs_mode = SIFIVE_SPI_CSMODE_MODE_HOLD;
122
Jagan Teki351ce7c2020-04-20 16:03:46 +0530123 if (slave_plat->mode & SPI_CS_HIGH)
Bhargav Shah3c34f752019-07-17 04:23:43 +0000124 cs_mode = SIFIVE_SPI_CSMODE_MODE_AUTO;
125
126 writel(cs_mode, spi->regs + SIFIVE_SPI_REG_CSMODE);
127
128 return 0;
129}
130
131static void sifive_spi_clear_cs(struct sifive_spi *spi)
132{
133 writel(SIFIVE_SPI_CSMODE_MODE_AUTO, spi->regs + SIFIVE_SPI_REG_CSMODE);
134}
135
136static void sifive_spi_prep_transfer(struct sifive_spi *spi,
137 bool is_rx_xfer,
Jagan Teki351ce7c2020-04-20 16:03:46 +0530138 struct dm_spi_slave_platdata *slave_plat)
Bhargav Shah3c34f752019-07-17 04:23:43 +0000139{
140 u32 cr;
141
142 /* Modify the SPI protocol mode */
143 cr = readl(spi->regs + SIFIVE_SPI_REG_FMT);
144
145 /* Bits per word ? */
146 cr &= ~SIFIVE_SPI_FMT_LEN_MASK;
147 cr |= SIFIVE_SPI_FMT_LEN(spi->bits_per_word);
148
149 /* LSB first? */
150 cr &= ~SIFIVE_SPI_FMT_ENDIAN;
Jagan Teki351ce7c2020-04-20 16:03:46 +0530151 if (slave_plat->mode & SPI_LSB_FIRST)
Bhargav Shah3c34f752019-07-17 04:23:43 +0000152 cr |= SIFIVE_SPI_FMT_ENDIAN;
153
154 /* Number of wires ? */
155 cr &= ~SIFIVE_SPI_FMT_PROTO_MASK;
Jagan Tekiebca2c32020-04-23 22:30:54 +0530156 switch (spi->fmt_proto) {
157 case SIFIVE_SPI_PROTO_QUAD:
Bhargav Shah3c34f752019-07-17 04:23:43 +0000158 cr |= SIFIVE_SPI_FMT_PROTO_QUAD;
Jagan Tekiebca2c32020-04-23 22:30:54 +0530159 break;
160 case SIFIVE_SPI_PROTO_DUAL:
Bhargav Shah3c34f752019-07-17 04:23:43 +0000161 cr |= SIFIVE_SPI_FMT_PROTO_DUAL;
Jagan Tekiebca2c32020-04-23 22:30:54 +0530162 break;
163 default:
Bhargav Shah3c34f752019-07-17 04:23:43 +0000164 cr |= SIFIVE_SPI_FMT_PROTO_SINGLE;
Jagan Tekiebca2c32020-04-23 22:30:54 +0530165 break;
166 }
Bhargav Shah3c34f752019-07-17 04:23:43 +0000167
168 /* SPI direction in/out ? */
169 cr &= ~SIFIVE_SPI_FMT_DIR;
170 if (!is_rx_xfer)
171 cr |= SIFIVE_SPI_FMT_DIR;
172
173 writel(cr, spi->regs + SIFIVE_SPI_REG_FMT);
174}
175
176static void sifive_spi_rx(struct sifive_spi *spi, u8 *rx_ptr)
177{
178 u32 data;
179
180 do {
181 data = readl(spi->regs + SIFIVE_SPI_REG_RXDATA);
182 } while (data & SIFIVE_SPI_RXDATA_EMPTY);
183
184 if (rx_ptr)
185 *rx_ptr = data & SIFIVE_SPI_RXDATA_DATA_MASK;
186}
187
188static void sifive_spi_tx(struct sifive_spi *spi, const u8 *tx_ptr)
189{
190 u32 data;
191 u8 tx_data = (tx_ptr) ? *tx_ptr & SIFIVE_SPI_TXDATA_DATA_MASK :
192 SIFIVE_SPI_TXDATA_DATA_MASK;
193
194 do {
195 data = readl(spi->regs + SIFIVE_SPI_REG_TXDATA);
196 } while (data & SIFIVE_SPI_TXDATA_FULL);
197
198 writel(tx_data, spi->regs + SIFIVE_SPI_REG_TXDATA);
199}
200
201static int sifive_spi_xfer(struct udevice *dev, unsigned int bitlen,
202 const void *dout, void *din, unsigned long flags)
203{
204 struct udevice *bus = dev->parent;
205 struct sifive_spi *spi = dev_get_priv(bus);
Jagan Teki351ce7c2020-04-20 16:03:46 +0530206 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
Bhargav Shah3c34f752019-07-17 04:23:43 +0000207 const unsigned char *tx_ptr = dout;
208 u8 *rx_ptr = din;
209 u32 remaining_len;
210 int ret;
211
212 if (flags & SPI_XFER_BEGIN) {
Jagan Teki351ce7c2020-04-20 16:03:46 +0530213 sifive_spi_prep_device(spi, slave_plat);
Bhargav Shah3c34f752019-07-17 04:23:43 +0000214
Jagan Teki351ce7c2020-04-20 16:03:46 +0530215 ret = sifive_spi_set_cs(spi, slave_plat);
Bhargav Shah3c34f752019-07-17 04:23:43 +0000216 if (ret)
217 return ret;
218 }
219
Jagan Teki351ce7c2020-04-20 16:03:46 +0530220 sifive_spi_prep_transfer(spi, true, slave_plat);
Bhargav Shah3c34f752019-07-17 04:23:43 +0000221
222 remaining_len = bitlen / 8;
223
224 while (remaining_len) {
225 int n_words, tx_words, rx_words;
226
227 n_words = min(remaining_len, spi->fifo_depth);
228
229 /* Enqueue n_words for transmission */
230 if (tx_ptr) {
231 for (tx_words = 0; tx_words < n_words; ++tx_words) {
232 sifive_spi_tx(spi, tx_ptr);
233 sifive_spi_rx(spi, NULL);
234 tx_ptr++;
235 }
236 }
237
238 /* Read out all the data from the RX FIFO */
239 if (rx_ptr) {
240 for (rx_words = 0; rx_words < n_words; ++rx_words) {
241 sifive_spi_tx(spi, NULL);
242 sifive_spi_rx(spi, rx_ptr);
243 rx_ptr++;
244 }
245 }
246
247 remaining_len -= n_words;
248 }
249
250 if (flags & SPI_XFER_END)
251 sifive_spi_clear_cs(spi);
252
253 return 0;
254}
255
Jagan Teki294b2912020-04-23 22:30:53 +0530256static int sifive_spi_exec_op(struct spi_slave *slave,
257 const struct spi_mem_op *op)
258{
259 struct udevice *dev = slave->dev;
Jagan Tekiebca2c32020-04-23 22:30:54 +0530260 struct sifive_spi *spi = dev_get_priv(dev->parent);
Jagan Teki294b2912020-04-23 22:30:53 +0530261 unsigned long flags = SPI_XFER_BEGIN;
262 u8 opcode = op->cmd.opcode;
263 unsigned int pos = 0;
264 const void *tx_buf = NULL;
265 void *rx_buf = NULL;
266 int op_len, i;
267 int ret;
268
269 if (!op->addr.nbytes && !op->dummy.nbytes && !op->data.nbytes)
270 flags |= SPI_XFER_END;
271
Jagan Tekiebca2c32020-04-23 22:30:54 +0530272 spi->fmt_proto = op->cmd.buswidth;
273
Jagan Teki294b2912020-04-23 22:30:53 +0530274 /* send the opcode */
275 ret = sifive_spi_xfer(dev, 8, (void *)&opcode, NULL, flags);
276 if (ret < 0) {
277 dev_err(dev, "failed to xfer opcode\n");
278 return ret;
279 }
280
281 op_len = op->addr.nbytes + op->dummy.nbytes;
282 u8 op_buf[op_len];
283
284 /* send the addr + dummy */
285 if (op->addr.nbytes) {
286 /* fill address */
287 for (i = 0; i < op->addr.nbytes; i++)
288 op_buf[pos + i] = op->addr.val >>
289 (8 * (op->addr.nbytes - i - 1));
290
291 pos += op->addr.nbytes;
292
293 /* fill dummy */
294 if (op->dummy.nbytes)
295 memset(op_buf + pos, 0xff, op->dummy.nbytes);
296
297 /* make sure to set end flag, if no data bytes */
298 if (!op->data.nbytes)
299 flags |= SPI_XFER_END;
300
Jagan Tekiebca2c32020-04-23 22:30:54 +0530301 spi->fmt_proto = op->addr.buswidth;
302
Jagan Teki294b2912020-04-23 22:30:53 +0530303 ret = sifive_spi_xfer(dev, op_len * 8, op_buf, NULL, flags);
304 if (ret < 0) {
305 dev_err(dev, "failed to xfer addr + dummy\n");
306 return ret;
307 }
308 }
309
310 /* send/received the data */
311 if (op->data.nbytes) {
312 if (op->data.dir == SPI_MEM_DATA_IN)
313 rx_buf = op->data.buf.in;
314 else
315 tx_buf = op->data.buf.out;
316
Jagan Tekiebca2c32020-04-23 22:30:54 +0530317 spi->fmt_proto = op->data.buswidth;
318
Jagan Teki294b2912020-04-23 22:30:53 +0530319 ret = sifive_spi_xfer(dev, op->data.nbytes * 8,
320 tx_buf, rx_buf, SPI_XFER_END);
321 if (ret) {
322 dev_err(dev, "failed to xfer data\n");
323 return ret;
324 }
325 }
326
327 return 0;
328}
329
Bhargav Shah3c34f752019-07-17 04:23:43 +0000330static int sifive_spi_set_speed(struct udevice *bus, uint speed)
331{
332 struct sifive_spi *spi = dev_get_priv(bus);
333 u32 scale;
334
335 if (speed > spi->freq)
336 speed = spi->freq;
337
338 /* Cofigure max speed */
339 scale = (DIV_ROUND_UP(spi->freq >> 1, speed) - 1)
340 & SIFIVE_SPI_SCKDIV_DIV_MASK;
341 writel(scale, spi->regs + SIFIVE_SPI_REG_SCKDIV);
342
343 return 0;
344}
345
346static int sifive_spi_set_mode(struct udevice *bus, uint mode)
347{
348 struct sifive_spi *spi = dev_get_priv(bus);
349 u32 cr;
350
351 /* Switch clock mode bits */
352 cr = readl(spi->regs + SIFIVE_SPI_REG_SCKMODE) &
353 ~SIFIVE_SPI_SCKMODE_MODE_MASK;
354 if (mode & SPI_CPHA)
355 cr |= SIFIVE_SPI_SCKMODE_PHA;
356 if (mode & SPI_CPOL)
357 cr |= SIFIVE_SPI_SCKMODE_POL;
358
359 writel(cr, spi->regs + SIFIVE_SPI_REG_SCKMODE);
360
361 return 0;
362}
363
364static int sifive_spi_cs_info(struct udevice *bus, uint cs,
365 struct spi_cs_info *info)
366{
367 struct sifive_spi *spi = dev_get_priv(bus);
368
369 if (cs >= spi->num_cs)
370 return -EINVAL;
371
372 return 0;
373}
374
375static void sifive_spi_init_hw(struct sifive_spi *spi)
376{
377 u32 cs_bits;
378
379 /* probe the number of CS lines */
380 spi->cs_inactive = readl(spi->regs + SIFIVE_SPI_REG_CSDEF);
381 writel(0xffffffffU, spi->regs + SIFIVE_SPI_REG_CSDEF);
382 cs_bits = readl(spi->regs + SIFIVE_SPI_REG_CSDEF);
383 writel(spi->cs_inactive, spi->regs + SIFIVE_SPI_REG_CSDEF);
384 if (!cs_bits) {
385 printf("Could not auto probe CS lines\n");
386 return;
387 }
388
389 spi->num_cs = ilog2(cs_bits) + 1;
390 if (spi->num_cs > SIFIVE_SPI_MAX_CS) {
391 printf("Invalid number of spi slaves\n");
392 return;
393 }
394
395 /* Watermark interrupts are disabled by default */
396 writel(0, spi->regs + SIFIVE_SPI_REG_IE);
397
398 /* Set CS/SCK Delays and Inactive Time to defaults */
399 writel(SIFIVE_SPI_DELAY0_CSSCK(1) | SIFIVE_SPI_DELAY0_SCKCS(1),
400 spi->regs + SIFIVE_SPI_REG_DELAY0);
401 writel(SIFIVE_SPI_DELAY1_INTERCS(1) | SIFIVE_SPI_DELAY1_INTERXFR(0),
402 spi->regs + SIFIVE_SPI_REG_DELAY1);
403
404 /* Exit specialized memory-mapped SPI flash mode */
405 writel(0, spi->regs + SIFIVE_SPI_REG_FCTRL);
406}
407
408static int sifive_spi_probe(struct udevice *bus)
409{
410 struct sifive_spi *spi = dev_get_priv(bus);
411 struct clk clkdev;
412 int ret;
413
414 spi->regs = (void *)(ulong)dev_remap_addr(bus);
415 if (!spi->regs)
416 return -ENODEV;
417
418 spi->fifo_depth = dev_read_u32_default(bus,
419 "sifive,fifo-depth",
420 SIFIVE_SPI_DEFAULT_DEPTH);
421
422 spi->bits_per_word = dev_read_u32_default(bus,
423 "sifive,max-bits-per-word",
424 SIFIVE_SPI_DEFAULT_BITS);
425
426 ret = clk_get_by_index(bus, 0, &clkdev);
427 if (ret)
428 return ret;
429 spi->freq = clk_get_rate(&clkdev);
430
431 /* init the sifive spi hw */
432 sifive_spi_init_hw(spi);
433
434 return 0;
435}
436
Jagan Teki294b2912020-04-23 22:30:53 +0530437static const struct spi_controller_mem_ops sifive_spi_mem_ops = {
438 .exec_op = sifive_spi_exec_op,
439};
440
Bhargav Shah3c34f752019-07-17 04:23:43 +0000441static const struct dm_spi_ops sifive_spi_ops = {
442 .xfer = sifive_spi_xfer,
443 .set_speed = sifive_spi_set_speed,
444 .set_mode = sifive_spi_set_mode,
445 .cs_info = sifive_spi_cs_info,
Jagan Teki294b2912020-04-23 22:30:53 +0530446 .mem_ops = &sifive_spi_mem_ops,
Bhargav Shah3c34f752019-07-17 04:23:43 +0000447};
448
449static const struct udevice_id sifive_spi_ids[] = {
450 { .compatible = "sifive,spi0" },
451 { }
452};
453
454U_BOOT_DRIVER(sifive_spi) = {
455 .name = "sifive_spi",
456 .id = UCLASS_SPI,
457 .of_match = sifive_spi_ids,
458 .ops = &sifive_spi_ops,
459 .priv_auto_alloc_size = sizeof(struct sifive_spi),
460 .probe = sifive_spi_probe,
461};