1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2018 SiFive, Inc.
6 * SiFive SPI controller driver (master mode only)
11 #include <dm/device_compat.h>
15 #include <linux/log2.h>
18 #define SIFIVE_SPI_MAX_CS 32
20 #define SIFIVE_SPI_DEFAULT_DEPTH 8
21 #define SIFIVE_SPI_DEFAULT_BITS 8
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 */
42 #define SIFIVE_SPI_SCKDIV_DIV_MASK 0xfffU
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)
51 #define SIFIVE_SPI_CSMODE_MODE_AUTO 0U
52 #define SIFIVE_SPI_CSMODE_MODE_HOLD 2U
53 #define SIFIVE_SPI_CSMODE_MODE_OFF 3U
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)
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)
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)
78 #define SIFIVE_SPI_TXDATA_DATA_MASK 0xffU
79 #define SIFIVE_SPI_TXDATA_FULL BIT(31)
82 #define SIFIVE_SPI_RXDATA_DATA_MASK 0xffU
83 #define SIFIVE_SPI_RXDATA_EMPTY BIT(31)
86 #define SIFIVE_SPI_IP_TXWM BIT(0)
87 #define SIFIVE_SPI_IP_RXWM BIT(1)
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 */
95 void *regs; /* base address of the registers */
98 u32 cs_inactive; /* Level of the CS pins when inactive*/
104 static void sifive_spi_prep_device(struct sifive_spi *spi,
105 struct dm_spi_slave_platdata *slave_plat)
107 /* Update the chip select polarity */
108 if (slave_plat->mode & SPI_CS_HIGH)
109 spi->cs_inactive &= ~BIT(slave_plat->cs);
111 spi->cs_inactive |= BIT(slave_plat->cs);
112 writel(spi->cs_inactive, spi->regs + SIFIVE_SPI_REG_CSDEF);
114 /* Select the correct device */
115 writel(slave_plat->cs, spi->regs + SIFIVE_SPI_REG_CSID);
118 static int sifive_spi_set_cs(struct sifive_spi *spi,
119 struct dm_spi_slave_platdata *slave_plat)
121 u32 cs_mode = SIFIVE_SPI_CSMODE_MODE_HOLD;
123 if (slave_plat->mode & SPI_CS_HIGH)
124 cs_mode = SIFIVE_SPI_CSMODE_MODE_AUTO;
126 writel(cs_mode, spi->regs + SIFIVE_SPI_REG_CSMODE);
131 static void sifive_spi_clear_cs(struct sifive_spi *spi)
133 writel(SIFIVE_SPI_CSMODE_MODE_AUTO, spi->regs + SIFIVE_SPI_REG_CSMODE);
136 static void sifive_spi_prep_transfer(struct sifive_spi *spi,
138 struct dm_spi_slave_platdata *slave_plat)
142 /* Modify the SPI protocol mode */
143 cr = readl(spi->regs + SIFIVE_SPI_REG_FMT);
145 /* Bits per word ? */
146 cr &= ~SIFIVE_SPI_FMT_LEN_MASK;
147 cr |= SIFIVE_SPI_FMT_LEN(spi->bits_per_word);
150 cr &= ~SIFIVE_SPI_FMT_ENDIAN;
151 if (slave_plat->mode & SPI_LSB_FIRST)
152 cr |= SIFIVE_SPI_FMT_ENDIAN;
154 /* Number of wires ? */
155 cr &= ~SIFIVE_SPI_FMT_PROTO_MASK;
156 switch (spi->fmt_proto) {
157 case SIFIVE_SPI_PROTO_QUAD:
158 cr |= SIFIVE_SPI_FMT_PROTO_QUAD;
160 case SIFIVE_SPI_PROTO_DUAL:
161 cr |= SIFIVE_SPI_FMT_PROTO_DUAL;
164 cr |= SIFIVE_SPI_FMT_PROTO_SINGLE;
168 /* SPI direction in/out ? */
169 cr &= ~SIFIVE_SPI_FMT_DIR;
171 cr |= SIFIVE_SPI_FMT_DIR;
173 writel(cr, spi->regs + SIFIVE_SPI_REG_FMT);
176 static void sifive_spi_rx(struct sifive_spi *spi, u8 *rx_ptr)
181 data = readl(spi->regs + SIFIVE_SPI_REG_RXDATA);
182 } while (data & SIFIVE_SPI_RXDATA_EMPTY);
185 *rx_ptr = data & SIFIVE_SPI_RXDATA_DATA_MASK;
188 static void sifive_spi_tx(struct sifive_spi *spi, const u8 *tx_ptr)
191 u8 tx_data = (tx_ptr) ? *tx_ptr & SIFIVE_SPI_TXDATA_DATA_MASK :
192 SIFIVE_SPI_TXDATA_DATA_MASK;
195 data = readl(spi->regs + SIFIVE_SPI_REG_TXDATA);
196 } while (data & SIFIVE_SPI_TXDATA_FULL);
198 writel(tx_data, spi->regs + SIFIVE_SPI_REG_TXDATA);
201 static int sifive_spi_xfer(struct udevice *dev, unsigned int bitlen,
202 const void *dout, void *din, unsigned long flags)
204 struct udevice *bus = dev->parent;
205 struct sifive_spi *spi = dev_get_priv(bus);
206 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
207 const unsigned char *tx_ptr = dout;
212 if (flags & SPI_XFER_BEGIN) {
213 sifive_spi_prep_device(spi, slave_plat);
215 ret = sifive_spi_set_cs(spi, slave_plat);
220 sifive_spi_prep_transfer(spi, true, slave_plat);
222 remaining_len = bitlen / 8;
224 while (remaining_len) {
225 int n_words, tx_words, rx_words;
227 n_words = min(remaining_len, spi->fifo_depth);
229 /* Enqueue n_words for transmission */
231 for (tx_words = 0; tx_words < n_words; ++tx_words) {
232 sifive_spi_tx(spi, tx_ptr);
233 sifive_spi_rx(spi, NULL);
238 /* Read out all the data from the RX FIFO */
240 for (rx_words = 0; rx_words < n_words; ++rx_words) {
241 sifive_spi_tx(spi, NULL);
242 sifive_spi_rx(spi, rx_ptr);
247 remaining_len -= n_words;
250 if (flags & SPI_XFER_END)
251 sifive_spi_clear_cs(spi);
256 static int sifive_spi_exec_op(struct spi_slave *slave,
257 const struct spi_mem_op *op)
259 struct udevice *dev = slave->dev;
260 struct sifive_spi *spi = dev_get_priv(dev->parent);
261 unsigned long flags = SPI_XFER_BEGIN;
262 u8 opcode = op->cmd.opcode;
263 unsigned int pos = 0;
264 const void *tx_buf = NULL;
269 if (!op->addr.nbytes && !op->dummy.nbytes && !op->data.nbytes)
270 flags |= SPI_XFER_END;
272 spi->fmt_proto = op->cmd.buswidth;
274 /* send the opcode */
275 ret = sifive_spi_xfer(dev, 8, (void *)&opcode, NULL, flags);
277 dev_err(dev, "failed to xfer opcode\n");
281 op_len = op->addr.nbytes + op->dummy.nbytes;
284 /* send the addr + dummy */
285 if (op->addr.nbytes) {
287 for (i = 0; i < op->addr.nbytes; i++)
288 op_buf[pos + i] = op->addr.val >>
289 (8 * (op->addr.nbytes - i - 1));
291 pos += op->addr.nbytes;
294 if (op->dummy.nbytes)
295 memset(op_buf + pos, 0xff, op->dummy.nbytes);
297 /* make sure to set end flag, if no data bytes */
298 if (!op->data.nbytes)
299 flags |= SPI_XFER_END;
301 spi->fmt_proto = op->addr.buswidth;
303 ret = sifive_spi_xfer(dev, op_len * 8, op_buf, NULL, flags);
305 dev_err(dev, "failed to xfer addr + dummy\n");
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;
315 tx_buf = op->data.buf.out;
317 spi->fmt_proto = op->data.buswidth;
319 ret = sifive_spi_xfer(dev, op->data.nbytes * 8,
320 tx_buf, rx_buf, SPI_XFER_END);
322 dev_err(dev, "failed to xfer data\n");
330 static int sifive_spi_set_speed(struct udevice *bus, uint speed)
332 struct sifive_spi *spi = dev_get_priv(bus);
335 if (speed > spi->freq)
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);
346 static int sifive_spi_set_mode(struct udevice *bus, uint mode)
348 struct sifive_spi *spi = dev_get_priv(bus);
351 /* Switch clock mode bits */
352 cr = readl(spi->regs + SIFIVE_SPI_REG_SCKMODE) &
353 ~SIFIVE_SPI_SCKMODE_MODE_MASK;
355 cr |= SIFIVE_SPI_SCKMODE_PHA;
357 cr |= SIFIVE_SPI_SCKMODE_POL;
359 writel(cr, spi->regs + SIFIVE_SPI_REG_SCKMODE);
364 static int sifive_spi_cs_info(struct udevice *bus, uint cs,
365 struct spi_cs_info *info)
367 struct sifive_spi *spi = dev_get_priv(bus);
369 if (cs >= spi->num_cs)
375 static void sifive_spi_init_hw(struct sifive_spi *spi)
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);
385 printf("Could not auto probe CS lines\n");
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");
395 /* Watermark interrupts are disabled by default */
396 writel(0, spi->regs + SIFIVE_SPI_REG_IE);
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);
404 /* Exit specialized memory-mapped SPI flash mode */
405 writel(0, spi->regs + SIFIVE_SPI_REG_FCTRL);
408 static int sifive_spi_probe(struct udevice *bus)
410 struct sifive_spi *spi = dev_get_priv(bus);
414 spi->regs = (void *)(ulong)dev_remap_addr(bus);
418 spi->fifo_depth = dev_read_u32_default(bus,
420 SIFIVE_SPI_DEFAULT_DEPTH);
422 spi->bits_per_word = dev_read_u32_default(bus,
423 "sifive,max-bits-per-word",
424 SIFIVE_SPI_DEFAULT_BITS);
426 ret = clk_get_by_index(bus, 0, &clkdev);
429 spi->freq = clk_get_rate(&clkdev);
431 /* init the sifive spi hw */
432 sifive_spi_init_hw(spi);
437 static const struct spi_controller_mem_ops sifive_spi_mem_ops = {
438 .exec_op = sifive_spi_exec_op,
441 static 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,
446 .mem_ops = &sifive_spi_mem_ops,
449 static const struct udevice_id sifive_spi_ids[] = {
450 { .compatible = "sifive,spi0" },
454 U_BOOT_DRIVER(sifive_spi) = {
455 .name = "sifive_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,