1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Marvell International Ltd.
13 #include <asm/unaligned.h>
14 #include <linux/bitfield.h>
15 #include <linux/compat.h>
16 #include <linux/delay.h>
18 #define OCTEON_SPI_MAX_BYTES 9
19 #define OCTEON_SPI_MAX_CLOCK_HZ 50000000
21 #define OCTEON_SPI_NUM_CS 4
23 #define OCTEON_SPI_CS_VALID(cs) ((cs) < OCTEON_SPI_NUM_CS)
25 #define MPI_CFG 0x0000
26 #define MPI_STS 0x0008
28 #define MPI_XMIT 0x0018
29 #define MPI_WIDE_DAT 0x0040
30 #define MPI_IO_CTL 0x0048
31 #define MPI_DAT(X) (0x0080 + ((X) << 3))
32 #define MPI_WIDE_BUF(X) (0x0800 + ((X) << 3))
33 #define MPI_CYA_CFG 0x1000
34 #define MPI_CLKEN 0x1080
36 #define MPI_CFG_ENABLE BIT_ULL(0)
37 #define MPI_CFG_IDLELO BIT_ULL(1)
38 #define MPI_CFG_CLK_CONT BIT_ULL(2)
39 #define MPI_CFG_WIREOR BIT_ULL(3)
40 #define MPI_CFG_LSBFIRST BIT_ULL(4)
41 #define MPI_CFG_CS_STICKY BIT_ULL(5)
42 #define MPI_CFG_CSHI BIT_ULL(7)
43 #define MPI_CFG_IDLECLKS GENMASK_ULL(9, 8)
44 #define MPI_CFG_TRITX BIT_ULL(10)
45 #define MPI_CFG_CSLATE BIT_ULL(11)
46 #define MPI_CFG_CSENA0 BIT_ULL(12)
47 #define MPI_CFG_CSENA1 BIT_ULL(13)
48 #define MPI_CFG_CSENA2 BIT_ULL(14)
49 #define MPI_CFG_CSENA3 BIT_ULL(15)
50 #define MPI_CFG_CLKDIV GENMASK_ULL(28, 16)
51 #define MPI_CFG_LEGACY_DIS BIT_ULL(31)
52 #define MPI_CFG_IOMODE GENMASK_ULL(35, 34)
53 #define MPI_CFG_TB100_EN BIT_ULL(49)
55 #define MPI_DAT_DATA GENMASK_ULL(7, 0)
57 #define MPI_STS_BUSY BIT_ULL(0)
58 #define MPI_STS_MPI_INTR BIT_ULL(1)
59 #define MPI_STS_RXNUM GENMASK_ULL(12, 8)
61 #define MPI_TX_TOTNUM GENMASK_ULL(4, 0)
62 #define MPI_TX_TXNUM GENMASK_ULL(12, 8)
63 #define MPI_TX_LEAVECS BIT_ULL(16)
64 #define MPI_TX_CSID GENMASK_ULL(21, 20)
66 #define MPI_XMIT_TOTNUM GENMASK_ULL(10, 0)
67 #define MPI_XMIT_TXNUM GENMASK_ULL(30, 20)
68 #define MPI_XMIT_BUF_SEL BIT_ULL(59)
69 #define MPI_XMIT_LEAVECS BIT_ULL(60)
70 #define MPI_XMIT_CSID GENMASK_ULL(62, 61)
72 /* Used on Octeon TX2 */
73 void board_acquire_flash_arb(bool acquire);
75 /* Local driver data structure */
77 void __iomem *base; /* Register base address */
79 u32 clkdiv; /* Clock divisor for device speed */
82 static u64 octeon_spi_set_mpicfg(struct udevice *dev)
84 struct dm_spi_slave_plat *slave = dev_get_parent_plat(dev);
85 struct udevice *bus = dev_get_parent(dev);
86 struct octeon_spi *priv = dev_get_priv(bus);
88 uint max_speed = slave->max_hz;
93 if (max_speed > OCTEON_SPI_MAX_CLOCK_HZ)
94 max_speed = OCTEON_SPI_MAX_CLOCK_HZ;
96 debug("\n slave params %d %d %d\n", slave->cs,
97 slave->max_hz, slave->mode);
98 cpha = !!(slave->mode & SPI_CPHA);
99 cpol = !!(slave->mode & SPI_CPOL);
101 mpi_cfg = FIELD_PREP(MPI_CFG_CLKDIV, priv->clkdiv & 0x1fff) |
102 FIELD_PREP(MPI_CFG_CSHI, !!(slave->mode & SPI_CS_HIGH)) |
103 FIELD_PREP(MPI_CFG_LSBFIRST, !!(slave->mode & SPI_LSB_FIRST)) |
104 FIELD_PREP(MPI_CFG_WIREOR, !!(slave->mode & SPI_3WIRE)) |
105 FIELD_PREP(MPI_CFG_IDLELO, cpha != cpol) |
106 FIELD_PREP(MPI_CFG_CSLATE, cpha) |
107 MPI_CFG_CSENA0 | MPI_CFG_CSENA1 |
108 MPI_CFG_CSENA2 | MPI_CFG_CSENA1 |
111 debug("\n mpi_cfg %llx\n", mpi_cfg);
116 * Wait until the SPI bus is ready
118 * @param dev SPI device to wait for
120 static void octeon_spi_wait_ready(struct udevice *dev)
122 struct udevice *bus = dev_get_parent(dev);
123 struct octeon_spi *priv = dev_get_priv(bus);
124 void *base = priv->base;
128 mpi_sts = readq(base + MPI_STS);
130 } while (mpi_sts & MPI_STS_BUSY);
132 debug("%s(%s)\n", __func__, dev->name);
136 * Claim the bus for a slave device
140 * Return: 0 for success, -EINVAL if chip select is invalid
142 static int octeon_spi_claim_bus(struct udevice *dev)
144 struct udevice *bus = dev_get_parent(dev);
145 struct octeon_spi *priv = dev_get_priv(bus);
146 void *base = priv->base;
149 debug("\n\n%s(%s)\n", __func__, dev->name);
150 if (!OCTEON_SPI_CS_VALID(spi_chip_select(dev)))
153 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2))
154 board_acquire_flash_arb(true);
156 mpi_cfg = readq(base + MPI_CFG);
157 mpi_cfg &= ~MPI_CFG_TRITX;
158 mpi_cfg |= MPI_CFG_ENABLE;
159 writeq(mpi_cfg, base + MPI_CFG);
160 mpi_cfg = readq(base + MPI_CFG);
161 udelay(5); /** Wait for bus to settle */
167 * Release the bus to a slave device
171 * Return: 0 for success, -EINVAL if chip select is invalid
173 static int octeon_spi_release_bus(struct udevice *dev)
175 struct udevice *bus = dev_get_parent(dev);
176 struct octeon_spi *priv = dev_get_priv(bus);
177 void *base = priv->base;
180 debug("%s(%s)\n\n", __func__, dev->name);
181 if (!OCTEON_SPI_CS_VALID(spi_chip_select(dev)))
184 if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2))
185 board_acquire_flash_arb(false);
187 mpi_cfg = readq(base + MPI_CFG);
188 mpi_cfg &= ~MPI_CFG_ENABLE;
189 writeq(mpi_cfg, base + MPI_CFG);
190 mpi_cfg = readq(base + MPI_CFG);
196 static int octeon_spi_xfer(struct udevice *dev, unsigned int bitlen,
197 const void *dout, void *din, unsigned long flags)
199 struct udevice *bus = dev_get_parent(dev);
200 struct octeon_spi *priv = dev_get_priv(bus);
201 void *base = priv->base;
205 int len = bitlen / 8;
207 const u8 *tx_data = dout;
209 int cs = spi_chip_select(dev);
211 if (!OCTEON_SPI_CS_VALID(cs))
214 debug("\n %s(%s, %u, %p, %p, 0x%lx), cs: %d\n",
215 __func__, dev->name, bitlen, dout, din, flags, cs);
217 mpi_cfg = octeon_spi_set_mpicfg(dev);
218 if (mpi_cfg != readq(base + MPI_CFG)) {
219 writeq(mpi_cfg, base + MPI_CFG);
220 mpi_cfg = readq(base + MPI_CFG);
224 debug("\n mpi_cfg upd %llx\n", mpi_cfg);
227 * Start by writing and reading 8 bytes at a time. While we can support
228 * up to 10, it's easier to just use 8 with the MPI_WIDE_DAT register.
232 wide_dat = get_unaligned((u64 *)tx_data);
233 debug(" tx: %016llx \t", (unsigned long long)wide_dat);
235 writeq(wide_dat, base + MPI_WIDE_DAT);
238 mpi_tx = FIELD_PREP(MPI_TX_CSID, cs) |
239 FIELD_PREP(MPI_TX_LEAVECS, 1) |
240 FIELD_PREP(MPI_TX_TXNUM, tx_data ? 8 : 0) |
241 FIELD_PREP(MPI_TX_TOTNUM, 8);
242 writeq(mpi_tx, base + MPI_TX);
244 octeon_spi_wait_ready(dev);
249 wide_dat = readq(base + MPI_WIDE_DAT);
250 debug(" rx: %016llx\t", (unsigned long long)wide_dat);
251 *(u64 *)rx_data = wide_dat;
259 /* Write and read the rest of the data */
261 for (i = 0; i < len; i++) {
262 debug(" tx: %02x\n", *tx_data);
263 writeq(*tx_data++, base + MPI_DAT(i));
267 mpi_tx = FIELD_PREP(MPI_TX_CSID, cs) |
268 FIELD_PREP(MPI_TX_LEAVECS, !(flags & SPI_XFER_END)) |
269 FIELD_PREP(MPI_TX_TXNUM, tx_data ? len : 0) |
270 FIELD_PREP(MPI_TX_TOTNUM, len);
271 writeq(mpi_tx, base + MPI_TX);
273 octeon_spi_wait_ready(dev);
278 for (i = 0; i < len; i++) {
279 *rx_data = readq(base + MPI_DAT(i)) & 0xff;
280 debug(" rx: %02x\n", *rx_data);
288 static int octeontx2_spi_xfer(struct udevice *dev, unsigned int bitlen,
289 const void *dout, void *din, unsigned long flags)
291 struct udevice *bus = dev_get_parent(dev);
292 struct octeon_spi *priv = dev_get_priv(bus);
293 void *base = priv->base;
297 int len = bitlen / 8;
300 const u8 *tx_data = dout;
302 int cs = spi_chip_select(dev);
304 if (!OCTEON_SPI_CS_VALID(cs))
307 debug("\n %s(%s, %u, %p, %p, 0x%lx), cs: %d\n",
308 __func__, dev->name, bitlen, dout, din, flags, cs);
310 mpi_cfg = octeon_spi_set_mpicfg(dev);
312 mpi_cfg |= MPI_CFG_TRITX | MPI_CFG_LEGACY_DIS | MPI_CFG_CS_STICKY |
315 mpi_cfg &= ~MPI_CFG_IOMODE;
316 if (flags & (SPI_TX_DUAL | SPI_RX_DUAL))
317 mpi_cfg |= FIELD_PREP(MPI_CFG_IOMODE, 2);
318 if (flags & (SPI_TX_QUAD | SPI_RX_QUAD))
319 mpi_cfg |= FIELD_PREP(MPI_CFG_IOMODE, 3);
321 if (mpi_cfg != readq(base + MPI_CFG)) {
322 writeq(mpi_cfg, base + MPI_CFG);
323 mpi_cfg = readq(base + MPI_CFG);
327 debug("\n mpi_cfg upd %llx\n\n", mpi_cfg);
329 /* Start by writing or reading 1024 bytes at a time. */
332 /* 8 bytes per iteration */
333 for (i = 0; i < 128; i++) {
334 wide_dat = get_unaligned((u64 *)tx_data);
335 debug(" tx: %016llx \t",
336 (unsigned long long)wide_dat);
340 writeq(wide_dat, base + MPI_WIDE_BUF(i));
344 mpi_xmit = FIELD_PREP(MPI_XMIT_CSID, cs) | MPI_XMIT_LEAVECS |
345 FIELD_PREP(MPI_XMIT_TXNUM, tx_data ? 1024 : 0) |
346 FIELD_PREP(MPI_XMIT_TOTNUM, 1024);
347 writeq(mpi_xmit, base + MPI_XMIT);
349 octeon_spi_wait_ready(dev);
354 /* 8 bytes per iteration */
355 for (i = 0; i < 128; i++) {
356 wide_dat = readq(base + MPI_WIDE_BUF(i));
357 debug(" rx: %016llx\t",
358 (unsigned long long)wide_dat);
361 *(u64 *)rx_data = wide_dat;
370 /* 8 bytes per iteration */
371 for (i = 0; i < len / 8; i++) {
372 wide_dat = get_unaligned((u64 *)tx_data);
373 debug(" tx: %016llx \t",
374 (unsigned long long)wide_dat);
378 writeq(wide_dat, base + MPI_WIDE_BUF(i));
381 memcpy(&wide_dat, tx_data, rem);
382 debug(" rtx: %016llx\t", wide_dat);
383 writeq(wide_dat, base + MPI_WIDE_BUF(i));
387 mpi_xmit = FIELD_PREP(MPI_XMIT_CSID, cs) |
388 FIELD_PREP(MPI_XMIT_LEAVECS, !(flags & SPI_XFER_END)) |
389 FIELD_PREP(MPI_XMIT_TXNUM, tx_data ? len : 0) |
390 FIELD_PREP(MPI_XMIT_TOTNUM, len);
391 writeq(mpi_xmit, base + MPI_XMIT);
393 octeon_spi_wait_ready(dev);
399 /* 8 bytes per iteration */
400 for (i = 0; i < len / 8; i++) {
401 wide_dat = readq(base + MPI_WIDE_BUF(i));
402 debug(" rx: %016llx\t",
403 (unsigned long long)wide_dat);
406 *(u64 *)rx_data = wide_dat;
410 wide_dat = readq(base + MPI_WIDE_BUF(i));
411 debug(" rrx: %016llx\t",
412 (unsigned long long)wide_dat);
413 memcpy(rx_data, &wide_dat, rem);
421 static bool octeon_spi_supports_op(struct spi_slave *slave,
422 const struct spi_mem_op *op)
424 /* For now, support only below combinations
429 if (op->cmd.buswidth != 1)
434 static int octeon_spi_exec_op(struct spi_slave *slave,
435 const struct spi_mem_op *op)
437 unsigned long flags = SPI_XFER_BEGIN;
444 if (op->cmd.buswidth != 1)
449 opcode = op->cmd.opcode;
451 if (!op->data.nbytes && !op->addr.nbytes && !op->dummy.nbytes)
452 flags |= SPI_XFER_END;
454 ret = octeontx2_spi_xfer(slave->dev, 8, (void *)&opcode, NULL, flags);
458 /* Send Address and dummy */
459 if (op->addr.nbytes) {
460 /* Alloc buffer for address+dummy */
461 buf = (u8 *)calloc(1, op->addr.nbytes + op->dummy.nbytes);
463 printf("%s Out of memory\n", __func__);
466 addr = (u8 *)&op->addr.val;
467 for (temp = 0; temp < op->addr.nbytes; temp++)
468 buf[i++] = *(u8 *)(addr + op->addr.nbytes - 1 - temp);
469 for (temp = 0; temp < op->dummy.nbytes; temp++)
471 if (op->addr.buswidth == 2)
472 flags |= SPI_RX_DUAL;
473 if (op->addr.buswidth == 4)
474 flags |= SPI_RX_QUAD;
476 if (!op->data.nbytes)
477 flags |= SPI_XFER_END;
478 ret = octeontx2_spi_xfer(slave->dev, i * 8, (void *)buf, NULL,
484 if (!op->data.nbytes)
487 /* Send/Receive Data */
488 flags |= SPI_XFER_END;
489 if (op->data.buswidth == 2)
490 flags |= SPI_RX_DUAL;
491 if (op->data.buswidth == 4)
492 flags |= SPI_RX_QUAD;
494 rx = (op->data.dir == SPI_MEM_DATA_IN) ? op->data.buf.in : NULL;
495 tx = (op->data.dir == SPI_MEM_DATA_OUT) ? op->data.buf.out : NULL;
497 ret = octeontx2_spi_xfer(slave->dev, (op->data.nbytes * 8), tx, rx,
502 static const struct spi_controller_mem_ops octeontx2_spi_mem_ops = {
503 .supports_op = octeon_spi_supports_op,
504 .exec_op = octeon_spi_exec_op,
508 * Set the speed of the SPI bus
510 * @param bus bus to set
511 * @param max_hz maximum speed supported
513 static int octeon_spi_set_speed(struct udevice *bus, uint max_hz)
515 struct octeon_spi *priv = dev_get_priv(bus);
519 if (max_hz > OCTEON_SPI_MAX_CLOCK_HZ)
520 max_hz = OCTEON_SPI_MAX_CLOCK_HZ;
522 if (device_is_compatible(bus, "cavium,thunderx-spi"))
523 clk_rate = 100000000;
525 clk_rate = clk_get_rate(&priv->clk);
526 if (IS_ERR_VALUE(clk_rate))
529 debug("%s(%s, %u, %lu)\n", __func__, bus->name, max_hz, clk_rate);
531 priv->clkdiv = clk_rate / (2 * max_hz);
533 calc_hz = clk_rate / (2 * priv->clkdiv);
534 if (calc_hz <= max_hz)
539 if (priv->clkdiv > 8191)
542 debug("%s: clkdiv=%d\n", __func__, priv->clkdiv);
547 static int octeon_spi_set_mode(struct udevice *bus, uint mode)
549 /* We don't set it here */
553 static struct dm_spi_ops octeon_spi_ops = {
554 .claim_bus = octeon_spi_claim_bus,
555 .release_bus = octeon_spi_release_bus,
556 .set_speed = octeon_spi_set_speed,
557 .set_mode = octeon_spi_set_mode,
558 .xfer = octeon_spi_xfer,
561 static int octeon_spi_probe(struct udevice *dev)
563 struct octeon_spi *priv = dev_get_priv(dev);
566 /* Octeon TX & TX2 use PCI based probing */
567 if (device_is_compatible(dev, "cavium,thunder-8190-spi")) {
568 pci_dev_t bdf = dm_pci_get_bdf(dev);
570 debug("SPI PCI device: %x\n", bdf);
571 priv->base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0, 0, PCI_REGION_TYPE,
573 /* Add base offset */
574 priv->base += 0x1000;
577 * Octeon TX2 needs a different xfer function and supports
580 if (device_is_compatible(dev, "cavium,thunderx-spi")) {
581 octeon_spi_ops.xfer = octeontx2_spi_xfer;
582 octeon_spi_ops.mem_ops = &octeontx2_spi_mem_ops;
585 priv->base = dev_remap_addr(dev);
588 ret = clk_get_by_index(dev, 0, &priv->clk);
592 ret = clk_enable(&priv->clk);
596 debug("SPI bus %s %d at %p\n", dev->name, dev_seq(dev), priv->base);
601 static const struct udevice_id octeon_spi_ids[] = {
603 { .compatible = "cavium,octeon-3010-spi" },
604 /* ARM Octeon TX / TX2 */
605 { .compatible = "cavium,thunder-8190-spi" },
609 U_BOOT_DRIVER(octeon_spi) = {
610 .name = "spi_octeon",
612 .of_match = octeon_spi_ids,
613 .probe = octeon_spi_probe,
614 .priv_auto = sizeof(struct octeon_spi),
615 .ops = &octeon_spi_ops,