1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // Copyright (C) IBM Corporation 2020
4 #include <linux/bitfield.h>
5 #include <linux/bits.h>
7 #include <linux/jiffies.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
11 #include <linux/spi/spi.h>
13 #define FSI_ENGID_SPI 0x23
14 #define FSI_MBOX_ROOT_CTRL_8 0x2860
15 #define FSI_MBOX_ROOT_CTRL_8_SPI_MUX 0xf0000000
17 #define FSI2SPI_DATA0 0x00
18 #define FSI2SPI_DATA1 0x04
19 #define FSI2SPI_CMD 0x08
20 #define FSI2SPI_CMD_WRITE BIT(31)
21 #define FSI2SPI_RESET 0x18
22 #define FSI2SPI_STATUS 0x1c
23 #define FSI2SPI_STATUS_ANY_ERROR BIT(31)
24 #define FSI2SPI_IRQ 0x20
26 #define SPI_FSI_BASE 0x70000
27 #define SPI_FSI_INIT_TIMEOUT_MS 1000
28 #define SPI_FSI_STATUS_TIMEOUT_MS 100
29 #define SPI_FSI_MAX_RX_SIZE 8
30 #define SPI_FSI_MAX_TX_SIZE 40
32 #define SPI_FSI_ERROR 0x0
33 #define SPI_FSI_COUNTER_CFG 0x1
34 #define SPI_FSI_CFG1 0x2
35 #define SPI_FSI_CLOCK_CFG 0x3
36 #define SPI_FSI_CLOCK_CFG_MM_ENABLE BIT_ULL(32)
37 #define SPI_FSI_CLOCK_CFG_ECC_DISABLE (BIT_ULL(35) | BIT_ULL(33))
38 #define SPI_FSI_CLOCK_CFG_RESET1 (BIT_ULL(36) | BIT_ULL(38))
39 #define SPI_FSI_CLOCK_CFG_RESET2 (BIT_ULL(37) | BIT_ULL(39))
40 #define SPI_FSI_CLOCK_CFG_MODE (BIT_ULL(41) | BIT_ULL(42))
41 #define SPI_FSI_CLOCK_CFG_SCK_RECV_DEL GENMASK_ULL(51, 44)
42 #define SPI_FSI_CLOCK_CFG_SCK_NO_DEL BIT_ULL(51)
43 #define SPI_FSI_CLOCK_CFG_SCK_DIV GENMASK_ULL(63, 52)
44 #define SPI_FSI_MMAP 0x4
45 #define SPI_FSI_DATA_TX 0x5
46 #define SPI_FSI_DATA_RX 0x6
47 #define SPI_FSI_SEQUENCE 0x7
48 #define SPI_FSI_SEQUENCE_STOP 0x00
49 #define SPI_FSI_SEQUENCE_SEL_SLAVE(x) (0x10 | ((x) & 0xf))
50 #define SPI_FSI_SEQUENCE_SHIFT_OUT(x) (0x30 | ((x) & 0xf))
51 #define SPI_FSI_SEQUENCE_SHIFT_IN(x) (0x40 | ((x) & 0xf))
52 #define SPI_FSI_SEQUENCE_COPY_DATA_TX 0xc0
53 #define SPI_FSI_SEQUENCE_BRANCH(x) (0xe0 | ((x) & 0xf))
54 #define SPI_FSI_STATUS 0x8
55 #define SPI_FSI_STATUS_ERROR \
56 (GENMASK_ULL(31, 21) | GENMASK_ULL(15, 12))
57 #define SPI_FSI_STATUS_SEQ_STATE GENMASK_ULL(55, 48)
58 #define SPI_FSI_STATUS_SEQ_STATE_IDLE BIT_ULL(48)
59 #define SPI_FSI_STATUS_TDR_UNDERRUN BIT_ULL(57)
60 #define SPI_FSI_STATUS_TDR_OVERRUN BIT_ULL(58)
61 #define SPI_FSI_STATUS_TDR_FULL BIT_ULL(59)
62 #define SPI_FSI_STATUS_RDR_UNDERRUN BIT_ULL(61)
63 #define SPI_FSI_STATUS_RDR_OVERRUN BIT_ULL(62)
64 #define SPI_FSI_STATUS_RDR_FULL BIT_ULL(63)
65 #define SPI_FSI_STATUS_ANY_ERROR \
66 (SPI_FSI_STATUS_ERROR | \
67 SPI_FSI_STATUS_TDR_OVERRUN | SPI_FSI_STATUS_RDR_UNDERRUN | \
68 SPI_FSI_STATUS_RDR_OVERRUN)
69 #define SPI_FSI_PORT_CTRL 0x9
72 struct fsi_device *fsi; /* FSI2SPI CFAM engine device */
73 struct mutex lock; /* lock access to the device */
77 struct device *dev; /* SPI controller device */
78 struct fsi2spi *bridge; /* FSI2SPI device */
82 struct fsi_spi_sequence {
87 static int fsi_spi_check_mux(struct fsi_device *fsi, struct device *dev)
91 __be32 root_ctrl_8_be;
93 rc = fsi_slave_read(fsi->slave, FSI_MBOX_ROOT_CTRL_8, &root_ctrl_8_be,
94 sizeof(root_ctrl_8_be));
98 root_ctrl_8 = be32_to_cpu(root_ctrl_8_be);
99 dev_dbg(dev, "Root control register 8: %08x\n", root_ctrl_8);
100 if ((root_ctrl_8 & FSI_MBOX_ROOT_CTRL_8_SPI_MUX) ==
101 FSI_MBOX_ROOT_CTRL_8_SPI_MUX)
107 static int fsi_spi_check_status(struct fsi_spi *ctx)
113 rc = fsi_device_read(ctx->bridge->fsi, FSI2SPI_STATUS, &sts_be,
118 sts = be32_to_cpu(sts_be);
119 if (sts & FSI2SPI_STATUS_ANY_ERROR) {
120 dev_err(ctx->dev, "Error with FSI2SPI interface: %08x.\n", sts);
127 static int fsi_spi_read_reg(struct fsi_spi *ctx, u32 offset, u64 *value)
132 u32 cmd = offset + ctx->base;
133 struct fsi2spi *bridge = ctx->bridge;
137 if (cmd & FSI2SPI_CMD_WRITE)
140 rc = mutex_lock_interruptible(&bridge->lock);
144 cmd_be = cpu_to_be32(cmd);
145 rc = fsi_device_write(bridge->fsi, FSI2SPI_CMD, &cmd_be,
150 rc = fsi_spi_check_status(ctx);
154 rc = fsi_device_read(bridge->fsi, FSI2SPI_DATA0, &data_be,
159 *value |= (u64)be32_to_cpu(data_be) << 32;
161 rc = fsi_device_read(bridge->fsi, FSI2SPI_DATA1, &data_be,
166 *value |= (u64)be32_to_cpu(data_be);
167 dev_dbg(ctx->dev, "Read %02x[%016llx].\n", offset, *value);
170 mutex_unlock(&bridge->lock);
174 static int fsi_spi_write_reg(struct fsi_spi *ctx, u32 offset, u64 value)
179 u32 cmd = offset + ctx->base;
180 struct fsi2spi *bridge = ctx->bridge;
182 if (cmd & FSI2SPI_CMD_WRITE)
185 rc = mutex_lock_interruptible(&bridge->lock);
189 dev_dbg(ctx->dev, "Write %02x[%016llx].\n", offset, value);
191 data_be = cpu_to_be32(upper_32_bits(value));
192 rc = fsi_device_write(bridge->fsi, FSI2SPI_DATA0, &data_be,
197 data_be = cpu_to_be32(lower_32_bits(value));
198 rc = fsi_device_write(bridge->fsi, FSI2SPI_DATA1, &data_be,
203 cmd_be = cpu_to_be32(cmd | FSI2SPI_CMD_WRITE);
204 rc = fsi_device_write(bridge->fsi, FSI2SPI_CMD, &cmd_be,
209 rc = fsi_spi_check_status(ctx);
212 mutex_unlock(&bridge->lock);
216 static int fsi_spi_data_in(u64 in, u8 *rx, int len)
219 int num_bytes = min(len, 8);
221 for (i = 0; i < num_bytes; ++i)
222 rx[i] = (u8)(in >> (8 * ((num_bytes - 1) - i)));
227 static int fsi_spi_data_out(u64 *out, const u8 *tx, int len)
230 int num_bytes = min(len, 8);
231 u8 *out_bytes = (u8 *)out;
233 /* Unused bytes of the tx data should be 0. */
236 for (i = 0; i < num_bytes; ++i)
237 out_bytes[8 - (i + 1)] = tx[i];
242 static int fsi_spi_reset(struct fsi_spi *ctx)
246 dev_dbg(ctx->dev, "Resetting SPI controller.\n");
248 rc = fsi_spi_write_reg(ctx, SPI_FSI_CLOCK_CFG,
249 SPI_FSI_CLOCK_CFG_RESET1);
253 rc = fsi_spi_write_reg(ctx, SPI_FSI_CLOCK_CFG,
254 SPI_FSI_CLOCK_CFG_RESET2);
258 return fsi_spi_write_reg(ctx, SPI_FSI_STATUS, 0ULL);
261 static int fsi_spi_status(struct fsi_spi *ctx, u64 *status, const char *dir)
263 int rc = fsi_spi_read_reg(ctx, SPI_FSI_STATUS, status);
268 if (*status & SPI_FSI_STATUS_ANY_ERROR) {
269 dev_err(ctx->dev, "%s error: %016llx\n", dir, *status);
271 rc = fsi_spi_reset(ctx);
281 static void fsi_spi_sequence_add(struct fsi_spi_sequence *seq, u8 val)
284 * Add the next byte of instruction to the 8-byte sequence register.
285 * Then decrement the counter so that the next instruction will go in
286 * the right place. Return the index of the slot we just filled in the
289 seq->data |= (u64)val << seq->bit;
293 static void fsi_spi_sequence_init(struct fsi_spi_sequence *seq)
299 static int fsi_spi_transfer_data(struct fsi_spi *ctx,
300 struct spi_transfer *transfer)
306 if (transfer->tx_buf) {
310 const u8 *tx = transfer->tx_buf;
312 while (transfer->len > sent) {
313 nb = fsi_spi_data_out(&out, &tx[sent],
314 (int)transfer->len - sent);
316 rc = fsi_spi_write_reg(ctx, SPI_FSI_DATA_TX, out);
320 end = jiffies + msecs_to_jiffies(SPI_FSI_STATUS_TIMEOUT_MS);
322 if (time_after(jiffies, end))
325 rc = fsi_spi_status(ctx, &status, "TX");
328 } while (status & SPI_FSI_STATUS_TDR_FULL);
332 } else if (transfer->rx_buf) {
335 u8 *rx = transfer->rx_buf;
337 while (transfer->len > recv) {
338 end = jiffies + msecs_to_jiffies(SPI_FSI_STATUS_TIMEOUT_MS);
340 if (time_after(jiffies, end))
343 rc = fsi_spi_status(ctx, &status, "RX");
346 } while (!(status & SPI_FSI_STATUS_RDR_FULL));
348 rc = fsi_spi_read_reg(ctx, SPI_FSI_DATA_RX, &in);
352 recv += fsi_spi_data_in(in, &rx[recv],
353 (int)transfer->len - recv);
360 static int fsi_spi_transfer_init(struct fsi_spi *ctx)
366 u64 clock_cfg = 0ULL;
368 u64 wanted_clock_cfg = SPI_FSI_CLOCK_CFG_ECC_DISABLE |
369 SPI_FSI_CLOCK_CFG_SCK_NO_DEL |
370 FIELD_PREP(SPI_FSI_CLOCK_CFG_SCK_DIV, 19);
372 end = jiffies + msecs_to_jiffies(SPI_FSI_INIT_TIMEOUT_MS);
374 if (time_after(jiffies, end))
377 rc = fsi_spi_read_reg(ctx, SPI_FSI_STATUS, &status);
381 seq_state = status & SPI_FSI_STATUS_SEQ_STATE;
383 if (status & (SPI_FSI_STATUS_ANY_ERROR |
384 SPI_FSI_STATUS_TDR_FULL |
385 SPI_FSI_STATUS_RDR_FULL)) {
388 "Initialization error: %08llx\n",
393 rc = fsi_spi_reset(ctx);
400 } while (seq_state && (seq_state != SPI_FSI_STATUS_SEQ_STATE_IDLE));
402 rc = fsi_spi_write_reg(ctx, SPI_FSI_COUNTER_CFG, 0ULL);
406 rc = fsi_spi_read_reg(ctx, SPI_FSI_CLOCK_CFG, &clock_cfg);
410 if ((clock_cfg & (SPI_FSI_CLOCK_CFG_MM_ENABLE |
411 SPI_FSI_CLOCK_CFG_ECC_DISABLE |
412 SPI_FSI_CLOCK_CFG_MODE |
413 SPI_FSI_CLOCK_CFG_SCK_RECV_DEL |
414 SPI_FSI_CLOCK_CFG_SCK_DIV)) != wanted_clock_cfg)
415 rc = fsi_spi_write_reg(ctx, SPI_FSI_CLOCK_CFG,
421 static int fsi_spi_transfer_one_message(struct spi_controller *ctlr,
422 struct spi_message *mesg)
425 u8 seq_slave = SPI_FSI_SEQUENCE_SEL_SLAVE(mesg->spi->chip_select + 1);
427 struct spi_transfer *transfer;
428 struct fsi_spi *ctx = spi_controller_get_devdata(ctlr);
430 rc = fsi_spi_check_mux(ctx->bridge->fsi, ctx->dev);
434 list_for_each_entry(transfer, &mesg->transfers, transfer_list) {
435 struct fsi_spi_sequence seq;
436 struct spi_transfer *next = NULL;
438 /* Sequencer must do shift out (tx) first. */
439 if (!transfer->tx_buf || transfer->len > SPI_FSI_MAX_TX_SIZE) {
444 dev_dbg(ctx->dev, "Start tx of %d bytes.\n", transfer->len);
446 rc = fsi_spi_transfer_init(ctx);
450 fsi_spi_sequence_init(&seq);
451 fsi_spi_sequence_add(&seq, seq_slave);
455 fsi_spi_sequence_add(&seq,
456 SPI_FSI_SEQUENCE_SHIFT_OUT(8));
459 fsi_spi_sequence_add(&seq, SPI_FSI_SEQUENCE_SHIFT_OUT(len));
461 if (!list_is_last(&transfer->transfer_list,
463 next = list_next_entry(transfer, transfer_list);
465 /* Sequencer can only do shift in (rx) after tx. */
469 if (next->len > SPI_FSI_MAX_RX_SIZE) {
474 dev_dbg(ctx->dev, "Sequence rx of %d bytes.\n",
477 shift = SPI_FSI_SEQUENCE_SHIFT_IN(next->len);
478 fsi_spi_sequence_add(&seq, shift);
484 fsi_spi_sequence_add(&seq, SPI_FSI_SEQUENCE_SEL_SLAVE(0));
486 rc = fsi_spi_write_reg(ctx, SPI_FSI_SEQUENCE, seq.data);
490 rc = fsi_spi_transfer_data(ctx, transfer);
495 rc = fsi_spi_transfer_data(ctx, next);
505 spi_finalize_current_message(ctlr);
510 static size_t fsi_spi_max_transfer_size(struct spi_device *spi)
512 return SPI_FSI_MAX_RX_SIZE;
515 static int fsi_spi_probe(struct device *dev)
518 struct device_node *np;
519 int num_controllers_registered = 0;
520 struct fsi2spi *bridge;
521 struct fsi_device *fsi = to_fsi_dev(dev);
523 rc = fsi_spi_check_mux(fsi, dev);
527 bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
532 mutex_init(&bridge->lock);
534 for_each_available_child_of_node(dev->of_node, np) {
537 struct spi_controller *ctlr;
539 if (of_property_read_u32(np, "reg", &base))
542 ctlr = spi_alloc_master(dev, sizeof(*ctx));
548 ctlr->dev.of_node = np;
549 ctlr->num_chipselect = of_get_available_child_count(np) ?: 1;
550 ctlr->flags = SPI_CONTROLLER_HALF_DUPLEX;
551 ctlr->max_transfer_size = fsi_spi_max_transfer_size;
552 ctlr->transfer_one_message = fsi_spi_transfer_one_message;
554 ctx = spi_controller_get_devdata(ctlr);
555 ctx->dev = &ctlr->dev;
556 ctx->bridge = bridge;
557 ctx->base = base + SPI_FSI_BASE;
559 rc = devm_spi_register_controller(dev, ctlr);
561 spi_controller_put(ctlr);
563 num_controllers_registered++;
566 if (!num_controllers_registered)
572 static const struct fsi_device_id fsi_spi_ids[] = {
573 { FSI_ENGID_SPI, FSI_VERSION_ANY },
576 MODULE_DEVICE_TABLE(fsi, fsi_spi_ids);
578 static struct fsi_driver fsi_spi_driver = {
579 .id_table = fsi_spi_ids,
582 .bus = &fsi_bus_type,
583 .probe = fsi_spi_probe,
586 module_fsi_driver(fsi_spi_driver);
588 MODULE_AUTHOR("Eddie James <eajames@linux.ibm.com>");
589 MODULE_DESCRIPTION("FSI attached SPI controller");
590 MODULE_LICENSE("GPL");