blob: 0e0ce25abb91a3c38cb8c1280e9a205b4493a14e [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>
Jagan Teki59798822020-04-23 22:30:55 +053014#include <wait_bit.h>
Bhargav Shah3c34f752019-07-17 04:23:43 +000015#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060016#include <linux/bitops.h>
Bhargav Shah3c34f752019-07-17 04:23:43 +000017#include <linux/log2.h>
18#include <clk.h>
19
20#define SIFIVE_SPI_MAX_CS 32
21
22#define SIFIVE_SPI_DEFAULT_DEPTH 8
23#define SIFIVE_SPI_DEFAULT_BITS 8
24
25/* register offsets */
26#define SIFIVE_SPI_REG_SCKDIV 0x00 /* Serial clock divisor */
27#define SIFIVE_SPI_REG_SCKMODE 0x04 /* Serial clock mode */
28#define SIFIVE_SPI_REG_CSID 0x10 /* Chip select ID */
29#define SIFIVE_SPI_REG_CSDEF 0x14 /* Chip select default */
30#define SIFIVE_SPI_REG_CSMODE 0x18 /* Chip select mode */
31#define SIFIVE_SPI_REG_DELAY0 0x28 /* Delay control 0 */
32#define SIFIVE_SPI_REG_DELAY1 0x2c /* Delay control 1 */
33#define SIFIVE_SPI_REG_FMT 0x40 /* Frame format */
34#define SIFIVE_SPI_REG_TXDATA 0x48 /* Tx FIFO data */
35#define SIFIVE_SPI_REG_RXDATA 0x4c /* Rx FIFO data */
36#define SIFIVE_SPI_REG_TXMARK 0x50 /* Tx FIFO watermark */
37#define SIFIVE_SPI_REG_RXMARK 0x54 /* Rx FIFO watermark */
38#define SIFIVE_SPI_REG_FCTRL 0x60 /* SPI flash interface control */
39#define SIFIVE_SPI_REG_FFMT 0x64 /* SPI flash instruction format */
40#define SIFIVE_SPI_REG_IE 0x70 /* Interrupt Enable Register */
41#define SIFIVE_SPI_REG_IP 0x74 /* Interrupt Pendings Register */
42
43/* sckdiv bits */
44#define SIFIVE_SPI_SCKDIV_DIV_MASK 0xfffU
45
46/* sckmode bits */
47#define SIFIVE_SPI_SCKMODE_PHA BIT(0)
48#define SIFIVE_SPI_SCKMODE_POL BIT(1)
49#define SIFIVE_SPI_SCKMODE_MODE_MASK (SIFIVE_SPI_SCKMODE_PHA | \
50 SIFIVE_SPI_SCKMODE_POL)
51
52/* csmode bits */
53#define SIFIVE_SPI_CSMODE_MODE_AUTO 0U
54#define SIFIVE_SPI_CSMODE_MODE_HOLD 2U
55#define SIFIVE_SPI_CSMODE_MODE_OFF 3U
56
57/* delay0 bits */
58#define SIFIVE_SPI_DELAY0_CSSCK(x) ((u32)(x))
59#define SIFIVE_SPI_DELAY0_CSSCK_MASK 0xffU
60#define SIFIVE_SPI_DELAY0_SCKCS(x) ((u32)(x) << 16)
61#define SIFIVE_SPI_DELAY0_SCKCS_MASK (0xffU << 16)
62
63/* delay1 bits */
64#define SIFIVE_SPI_DELAY1_INTERCS(x) ((u32)(x))
65#define SIFIVE_SPI_DELAY1_INTERCS_MASK 0xffU
66#define SIFIVE_SPI_DELAY1_INTERXFR(x) ((u32)(x) << 16)
67#define SIFIVE_SPI_DELAY1_INTERXFR_MASK (0xffU << 16)
68
69/* fmt bits */
70#define SIFIVE_SPI_FMT_PROTO_SINGLE 0U
71#define SIFIVE_SPI_FMT_PROTO_DUAL 1U
72#define SIFIVE_SPI_FMT_PROTO_QUAD 2U
73#define SIFIVE_SPI_FMT_PROTO_MASK 3U
74#define SIFIVE_SPI_FMT_ENDIAN BIT(2)
75#define SIFIVE_SPI_FMT_DIR BIT(3)
76#define SIFIVE_SPI_FMT_LEN(x) ((u32)(x) << 16)
77#define SIFIVE_SPI_FMT_LEN_MASK (0xfU << 16)
78
79/* txdata bits */
80#define SIFIVE_SPI_TXDATA_DATA_MASK 0xffU
81#define SIFIVE_SPI_TXDATA_FULL BIT(31)
82
83/* rxdata bits */
84#define SIFIVE_SPI_RXDATA_DATA_MASK 0xffU
85#define SIFIVE_SPI_RXDATA_EMPTY BIT(31)
86
87/* ie and ip bits */
88#define SIFIVE_SPI_IP_TXWM BIT(0)
89#define SIFIVE_SPI_IP_RXWM BIT(1)
90
Jagan Tekiebca2c32020-04-23 22:30:54 +053091/* format protocol */
92#define SIFIVE_SPI_PROTO_QUAD 4 /* 4 lines I/O protocol transfer */
93#define SIFIVE_SPI_PROTO_DUAL 2 /* 2 lines I/O protocol transfer */
94#define SIFIVE_SPI_PROTO_SINGLE 1 /* 1 line I/O protocol transfer */
95
Bhargav Shah3c34f752019-07-17 04:23:43 +000096struct sifive_spi {
97 void *regs; /* base address of the registers */
98 u32 fifo_depth;
99 u32 bits_per_word;
100 u32 cs_inactive; /* Level of the CS pins when inactive*/
101 u32 freq;
102 u32 num_cs;
Jagan Tekiebca2c32020-04-23 22:30:54 +0530103 u8 fmt_proto;
Bhargav Shah3c34f752019-07-17 04:23:43 +0000104};
105
106static void sifive_spi_prep_device(struct sifive_spi *spi,
Jagan Teki351ce7c2020-04-20 16:03:46 +0530107 struct dm_spi_slave_platdata *slave_plat)
Bhargav Shah3c34f752019-07-17 04:23:43 +0000108{
109 /* Update the chip select polarity */
Jagan Teki351ce7c2020-04-20 16:03:46 +0530110 if (slave_plat->mode & SPI_CS_HIGH)
111 spi->cs_inactive &= ~BIT(slave_plat->cs);
Bhargav Shah3c34f752019-07-17 04:23:43 +0000112 else
Jagan Teki351ce7c2020-04-20 16:03:46 +0530113 spi->cs_inactive |= BIT(slave_plat->cs);
Bhargav Shah3c34f752019-07-17 04:23:43 +0000114 writel(spi->cs_inactive, spi->regs + SIFIVE_SPI_REG_CSDEF);
115
116 /* Select the correct device */
Jagan Teki351ce7c2020-04-20 16:03:46 +0530117 writel(slave_plat->cs, spi->regs + SIFIVE_SPI_REG_CSID);
Bhargav Shah3c34f752019-07-17 04:23:43 +0000118}
119
120static int sifive_spi_set_cs(struct sifive_spi *spi,
Jagan Teki351ce7c2020-04-20 16:03:46 +0530121 struct dm_spi_slave_platdata *slave_plat)
Bhargav Shah3c34f752019-07-17 04:23:43 +0000122{
123 u32 cs_mode = SIFIVE_SPI_CSMODE_MODE_HOLD;
124
Jagan Teki351ce7c2020-04-20 16:03:46 +0530125 if (slave_plat->mode & SPI_CS_HIGH)
Bhargav Shah3c34f752019-07-17 04:23:43 +0000126 cs_mode = SIFIVE_SPI_CSMODE_MODE_AUTO;
127
128 writel(cs_mode, spi->regs + SIFIVE_SPI_REG_CSMODE);
129
130 return 0;
131}
132
133static void sifive_spi_clear_cs(struct sifive_spi *spi)
134{
135 writel(SIFIVE_SPI_CSMODE_MODE_AUTO, spi->regs + SIFIVE_SPI_REG_CSMODE);
136}
137
138static void sifive_spi_prep_transfer(struct sifive_spi *spi,
Jagan Teki59798822020-04-23 22:30:55 +0530139 struct dm_spi_slave_platdata *slave_plat,
140 u8 *rx_ptr)
Bhargav Shah3c34f752019-07-17 04:23:43 +0000141{
142 u32 cr;
143
144 /* Modify the SPI protocol mode */
145 cr = readl(spi->regs + SIFIVE_SPI_REG_FMT);
146
147 /* Bits per word ? */
148 cr &= ~SIFIVE_SPI_FMT_LEN_MASK;
149 cr |= SIFIVE_SPI_FMT_LEN(spi->bits_per_word);
150
151 /* LSB first? */
152 cr &= ~SIFIVE_SPI_FMT_ENDIAN;
Jagan Teki351ce7c2020-04-20 16:03:46 +0530153 if (slave_plat->mode & SPI_LSB_FIRST)
Bhargav Shah3c34f752019-07-17 04:23:43 +0000154 cr |= SIFIVE_SPI_FMT_ENDIAN;
155
156 /* Number of wires ? */
157 cr &= ~SIFIVE_SPI_FMT_PROTO_MASK;
Jagan Tekiebca2c32020-04-23 22:30:54 +0530158 switch (spi->fmt_proto) {
159 case SIFIVE_SPI_PROTO_QUAD:
Bhargav Shah3c34f752019-07-17 04:23:43 +0000160 cr |= SIFIVE_SPI_FMT_PROTO_QUAD;
Jagan Tekiebca2c32020-04-23 22:30:54 +0530161 break;
162 case SIFIVE_SPI_PROTO_DUAL:
Bhargav Shah3c34f752019-07-17 04:23:43 +0000163 cr |= SIFIVE_SPI_FMT_PROTO_DUAL;
Jagan Tekiebca2c32020-04-23 22:30:54 +0530164 break;
165 default:
Bhargav Shah3c34f752019-07-17 04:23:43 +0000166 cr |= SIFIVE_SPI_FMT_PROTO_SINGLE;
Jagan Tekiebca2c32020-04-23 22:30:54 +0530167 break;
168 }
Bhargav Shah3c34f752019-07-17 04:23:43 +0000169
170 /* SPI direction in/out ? */
171 cr &= ~SIFIVE_SPI_FMT_DIR;
Jagan Teki59798822020-04-23 22:30:55 +0530172 if (!rx_ptr)
Bhargav Shah3c34f752019-07-17 04:23:43 +0000173 cr |= SIFIVE_SPI_FMT_DIR;
174
175 writel(cr, spi->regs + SIFIVE_SPI_REG_FMT);
176}
177
178static void sifive_spi_rx(struct sifive_spi *spi, u8 *rx_ptr)
179{
180 u32 data;
181
182 do {
183 data = readl(spi->regs + SIFIVE_SPI_REG_RXDATA);
184 } while (data & SIFIVE_SPI_RXDATA_EMPTY);
185
186 if (rx_ptr)
187 *rx_ptr = data & SIFIVE_SPI_RXDATA_DATA_MASK;
188}
189
190static void sifive_spi_tx(struct sifive_spi *spi, const u8 *tx_ptr)
191{
192 u32 data;
193 u8 tx_data = (tx_ptr) ? *tx_ptr & SIFIVE_SPI_TXDATA_DATA_MASK :
194 SIFIVE_SPI_TXDATA_DATA_MASK;
195
196 do {
197 data = readl(spi->regs + SIFIVE_SPI_REG_TXDATA);
198 } while (data & SIFIVE_SPI_TXDATA_FULL);
199
200 writel(tx_data, spi->regs + SIFIVE_SPI_REG_TXDATA);
201}
202
Jagan Teki59798822020-04-23 22:30:55 +0530203static int sifive_spi_wait(struct sifive_spi *spi, u32 bit)
204{
205 return wait_for_bit_le32(spi->regs + SIFIVE_SPI_REG_IP,
206 bit, true, 100, false);
207}
208
Bhargav Shah3c34f752019-07-17 04:23:43 +0000209static int sifive_spi_xfer(struct udevice *dev, unsigned int bitlen,
210 const void *dout, void *din, unsigned long flags)
211{
212 struct udevice *bus = dev->parent;
213 struct sifive_spi *spi = dev_get_priv(bus);
Jagan Teki351ce7c2020-04-20 16:03:46 +0530214 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
Jagan Teki59798822020-04-23 22:30:55 +0530215 const u8 *tx_ptr = dout;
Bhargav Shah3c34f752019-07-17 04:23:43 +0000216 u8 *rx_ptr = din;
217 u32 remaining_len;
218 int ret;
219
220 if (flags & SPI_XFER_BEGIN) {
Jagan Teki351ce7c2020-04-20 16:03:46 +0530221 sifive_spi_prep_device(spi, slave_plat);
Bhargav Shah3c34f752019-07-17 04:23:43 +0000222
Jagan Teki351ce7c2020-04-20 16:03:46 +0530223 ret = sifive_spi_set_cs(spi, slave_plat);
Bhargav Shah3c34f752019-07-17 04:23:43 +0000224 if (ret)
225 return ret;
226 }
227
Jagan Teki59798822020-04-23 22:30:55 +0530228 sifive_spi_prep_transfer(spi, slave_plat, rx_ptr);
Bhargav Shah3c34f752019-07-17 04:23:43 +0000229
230 remaining_len = bitlen / 8;
231
232 while (remaining_len) {
Jagan Teki59798822020-04-23 22:30:55 +0530233 unsigned int n_words = min(remaining_len, spi->fifo_depth);
234 unsigned int tx_words, rx_words;
Bhargav Shah3c34f752019-07-17 04:23:43 +0000235
236 /* Enqueue n_words for transmission */
Jagan Teki59798822020-04-23 22:30:55 +0530237 for (tx_words = 0; tx_words < n_words; tx_words++) {
238 if (!tx_ptr)
239 sifive_spi_tx(spi, NULL);
240 else
241 sifive_spi_tx(spi, tx_ptr++);
Bhargav Shah3c34f752019-07-17 04:23:43 +0000242 }
243
Bhargav Shah3c34f752019-07-17 04:23:43 +0000244 if (rx_ptr) {
Jagan Teki59798822020-04-23 22:30:55 +0530245 /* Wait for transmission + reception to complete */
246 writel(n_words - 1, spi->regs + SIFIVE_SPI_REG_RXMARK);
247 ret = sifive_spi_wait(spi, SIFIVE_SPI_IP_RXWM);
248 if (ret)
249 return ret;
250
251 /* Read out all the data from the RX FIFO */
252 for (rx_words = 0; rx_words < n_words; rx_words++)
253 sifive_spi_rx(spi, rx_ptr++);
254 } else {
255 /* Wait for transmission to complete */
256 ret = sifive_spi_wait(spi, SIFIVE_SPI_IP_TXWM);
257 if (ret)
258 return ret;
Bhargav Shah3c34f752019-07-17 04:23:43 +0000259 }
260
261 remaining_len -= n_words;
262 }
263
264 if (flags & SPI_XFER_END)
265 sifive_spi_clear_cs(spi);
266
267 return 0;
268}
269
Jagan Teki294b2912020-04-23 22:30:53 +0530270static int sifive_spi_exec_op(struct spi_slave *slave,
271 const struct spi_mem_op *op)
272{
273 struct udevice *dev = slave->dev;
Jagan Tekiebca2c32020-04-23 22:30:54 +0530274 struct sifive_spi *spi = dev_get_priv(dev->parent);
Jagan Teki294b2912020-04-23 22:30:53 +0530275 unsigned long flags = SPI_XFER_BEGIN;
276 u8 opcode = op->cmd.opcode;
277 unsigned int pos = 0;
278 const void *tx_buf = NULL;
279 void *rx_buf = NULL;
280 int op_len, i;
281 int ret;
282
283 if (!op->addr.nbytes && !op->dummy.nbytes && !op->data.nbytes)
284 flags |= SPI_XFER_END;
285
Jagan Tekiebca2c32020-04-23 22:30:54 +0530286 spi->fmt_proto = op->cmd.buswidth;
287
Jagan Teki294b2912020-04-23 22:30:53 +0530288 /* send the opcode */
289 ret = sifive_spi_xfer(dev, 8, (void *)&opcode, NULL, flags);
290 if (ret < 0) {
291 dev_err(dev, "failed to xfer opcode\n");
292 return ret;
293 }
294
295 op_len = op->addr.nbytes + op->dummy.nbytes;
296 u8 op_buf[op_len];
297
298 /* send the addr + dummy */
299 if (op->addr.nbytes) {
300 /* fill address */
301 for (i = 0; i < op->addr.nbytes; i++)
302 op_buf[pos + i] = op->addr.val >>
303 (8 * (op->addr.nbytes - i - 1));
304
305 pos += op->addr.nbytes;
306
307 /* fill dummy */
308 if (op->dummy.nbytes)
309 memset(op_buf + pos, 0xff, op->dummy.nbytes);
310
311 /* make sure to set end flag, if no data bytes */
312 if (!op->data.nbytes)
313 flags |= SPI_XFER_END;
314
Jagan Tekiebca2c32020-04-23 22:30:54 +0530315 spi->fmt_proto = op->addr.buswidth;
316
Jagan Teki294b2912020-04-23 22:30:53 +0530317 ret = sifive_spi_xfer(dev, op_len * 8, op_buf, NULL, flags);
318 if (ret < 0) {
319 dev_err(dev, "failed to xfer addr + dummy\n");
320 return ret;
321 }
322 }
323
324 /* send/received the data */
325 if (op->data.nbytes) {
326 if (op->data.dir == SPI_MEM_DATA_IN)
327 rx_buf = op->data.buf.in;
328 else
329 tx_buf = op->data.buf.out;
330
Jagan Tekiebca2c32020-04-23 22:30:54 +0530331 spi->fmt_proto = op->data.buswidth;
332
Jagan Teki294b2912020-04-23 22:30:53 +0530333 ret = sifive_spi_xfer(dev, op->data.nbytes * 8,
334 tx_buf, rx_buf, SPI_XFER_END);
335 if (ret) {
336 dev_err(dev, "failed to xfer data\n");
337 return ret;
338 }
339 }
340
341 return 0;
342}
343
Bhargav Shah3c34f752019-07-17 04:23:43 +0000344static int sifive_spi_set_speed(struct udevice *bus, uint speed)
345{
346 struct sifive_spi *spi = dev_get_priv(bus);
347 u32 scale;
348
349 if (speed > spi->freq)
350 speed = spi->freq;
351
352 /* Cofigure max speed */
353 scale = (DIV_ROUND_UP(spi->freq >> 1, speed) - 1)
354 & SIFIVE_SPI_SCKDIV_DIV_MASK;
355 writel(scale, spi->regs + SIFIVE_SPI_REG_SCKDIV);
356
357 return 0;
358}
359
360static int sifive_spi_set_mode(struct udevice *bus, uint mode)
361{
362 struct sifive_spi *spi = dev_get_priv(bus);
363 u32 cr;
364
365 /* Switch clock mode bits */
366 cr = readl(spi->regs + SIFIVE_SPI_REG_SCKMODE) &
367 ~SIFIVE_SPI_SCKMODE_MODE_MASK;
368 if (mode & SPI_CPHA)
369 cr |= SIFIVE_SPI_SCKMODE_PHA;
370 if (mode & SPI_CPOL)
371 cr |= SIFIVE_SPI_SCKMODE_POL;
372
373 writel(cr, spi->regs + SIFIVE_SPI_REG_SCKMODE);
374
375 return 0;
376}
377
378static int sifive_spi_cs_info(struct udevice *bus, uint cs,
379 struct spi_cs_info *info)
380{
381 struct sifive_spi *spi = dev_get_priv(bus);
382
383 if (cs >= spi->num_cs)
384 return -EINVAL;
385
386 return 0;
387}
388
389static void sifive_spi_init_hw(struct sifive_spi *spi)
390{
391 u32 cs_bits;
392
393 /* probe the number of CS lines */
394 spi->cs_inactive = readl(spi->regs + SIFIVE_SPI_REG_CSDEF);
395 writel(0xffffffffU, spi->regs + SIFIVE_SPI_REG_CSDEF);
396 cs_bits = readl(spi->regs + SIFIVE_SPI_REG_CSDEF);
397 writel(spi->cs_inactive, spi->regs + SIFIVE_SPI_REG_CSDEF);
398 if (!cs_bits) {
399 printf("Could not auto probe CS lines\n");
400 return;
401 }
402
403 spi->num_cs = ilog2(cs_bits) + 1;
404 if (spi->num_cs > SIFIVE_SPI_MAX_CS) {
405 printf("Invalid number of spi slaves\n");
406 return;
407 }
408
409 /* Watermark interrupts are disabled by default */
410 writel(0, spi->regs + SIFIVE_SPI_REG_IE);
411
Jagan Teki59798822020-04-23 22:30:55 +0530412 /* Default watermark FIFO threshold values */
413 writel(1, spi->regs + SIFIVE_SPI_REG_TXMARK);
414 writel(0, spi->regs + SIFIVE_SPI_REG_RXMARK);
415
Bhargav Shah3c34f752019-07-17 04:23:43 +0000416 /* Set CS/SCK Delays and Inactive Time to defaults */
417 writel(SIFIVE_SPI_DELAY0_CSSCK(1) | SIFIVE_SPI_DELAY0_SCKCS(1),
418 spi->regs + SIFIVE_SPI_REG_DELAY0);
419 writel(SIFIVE_SPI_DELAY1_INTERCS(1) | SIFIVE_SPI_DELAY1_INTERXFR(0),
420 spi->regs + SIFIVE_SPI_REG_DELAY1);
421
422 /* Exit specialized memory-mapped SPI flash mode */
423 writel(0, spi->regs + SIFIVE_SPI_REG_FCTRL);
424}
425
426static int sifive_spi_probe(struct udevice *bus)
427{
428 struct sifive_spi *spi = dev_get_priv(bus);
429 struct clk clkdev;
430 int ret;
431
432 spi->regs = (void *)(ulong)dev_remap_addr(bus);
433 if (!spi->regs)
434 return -ENODEV;
435
436 spi->fifo_depth = dev_read_u32_default(bus,
437 "sifive,fifo-depth",
438 SIFIVE_SPI_DEFAULT_DEPTH);
439
440 spi->bits_per_word = dev_read_u32_default(bus,
441 "sifive,max-bits-per-word",
442 SIFIVE_SPI_DEFAULT_BITS);
443
444 ret = clk_get_by_index(bus, 0, &clkdev);
445 if (ret)
446 return ret;
447 spi->freq = clk_get_rate(&clkdev);
448
449 /* init the sifive spi hw */
450 sifive_spi_init_hw(spi);
451
452 return 0;
453}
454
Jagan Teki294b2912020-04-23 22:30:53 +0530455static const struct spi_controller_mem_ops sifive_spi_mem_ops = {
456 .exec_op = sifive_spi_exec_op,
457};
458
Bhargav Shah3c34f752019-07-17 04:23:43 +0000459static const struct dm_spi_ops sifive_spi_ops = {
460 .xfer = sifive_spi_xfer,
461 .set_speed = sifive_spi_set_speed,
462 .set_mode = sifive_spi_set_mode,
463 .cs_info = sifive_spi_cs_info,
Jagan Teki294b2912020-04-23 22:30:53 +0530464 .mem_ops = &sifive_spi_mem_ops,
Bhargav Shah3c34f752019-07-17 04:23:43 +0000465};
466
467static const struct udevice_id sifive_spi_ids[] = {
468 { .compatible = "sifive,spi0" },
469 { }
470};
471
472U_BOOT_DRIVER(sifive_spi) = {
473 .name = "sifive_spi",
474 .id = UCLASS_SPI,
475 .of_match = sifive_spi_ids,
476 .ops = &sifive_spi_ops,
477 .priv_auto_alloc_size = sizeof(struct sifive_spi),
478 .probe = sifive_spi_probe,
479};