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_MAX_RX_SIZE 8
29 #define SPI_FSI_MAX_TX_SIZE 40
31 #define SPI_FSI_ERROR 0x0
32 #define SPI_FSI_COUNTER_CFG 0x1
33 #define SPI_FSI_CFG1 0x2
34 #define SPI_FSI_CLOCK_CFG 0x3
35 #define SPI_FSI_CLOCK_CFG_MM_ENABLE BIT_ULL(32)
36 #define SPI_FSI_CLOCK_CFG_ECC_DISABLE (BIT_ULL(35) | BIT_ULL(33))
37 #define SPI_FSI_CLOCK_CFG_RESET1 (BIT_ULL(36) | BIT_ULL(38))
38 #define SPI_FSI_CLOCK_CFG_RESET2 (BIT_ULL(37) | BIT_ULL(39))
39 #define SPI_FSI_CLOCK_CFG_MODE (BIT_ULL(41) | BIT_ULL(42))
40 #define SPI_FSI_CLOCK_CFG_SCK_RECV_DEL GENMASK_ULL(51, 44)
41 #define SPI_FSI_CLOCK_CFG_SCK_NO_DEL BIT_ULL(51)
42 #define SPI_FSI_CLOCK_CFG_SCK_DIV GENMASK_ULL(63, 52)
43 #define SPI_FSI_MMAP 0x4
44 #define SPI_FSI_DATA_TX 0x5
45 #define SPI_FSI_DATA_RX 0x6
46 #define SPI_FSI_SEQUENCE 0x7
47 #define SPI_FSI_SEQUENCE_STOP 0x00
48 #define SPI_FSI_SEQUENCE_SEL_SLAVE(x) (0x10 | ((x) & 0xf))
49 #define SPI_FSI_SEQUENCE_SHIFT_OUT(x) (0x30 | ((x) & 0xf))
50 #define SPI_FSI_SEQUENCE_SHIFT_IN(x) (0x40 | ((x) & 0xf))
51 #define SPI_FSI_SEQUENCE_COPY_DATA_TX 0xc0
52 #define SPI_FSI_SEQUENCE_BRANCH(x) (0xe0 | ((x) & 0xf))
53 #define SPI_FSI_STATUS 0x8
54 #define SPI_FSI_STATUS_ERROR \
55 (GENMASK_ULL(31, 21) | GENMASK_ULL(15, 12))
56 #define SPI_FSI_STATUS_SEQ_STATE GENMASK_ULL(55, 48)
57 #define SPI_FSI_STATUS_SEQ_STATE_IDLE BIT_ULL(48)
58 #define SPI_FSI_STATUS_TDR_UNDERRUN BIT_ULL(57)
59 #define SPI_FSI_STATUS_TDR_OVERRUN BIT_ULL(58)
60 #define SPI_FSI_STATUS_TDR_FULL BIT_ULL(59)
61 #define SPI_FSI_STATUS_RDR_UNDERRUN BIT_ULL(61)
62 #define SPI_FSI_STATUS_RDR_OVERRUN BIT_ULL(62)
63 #define SPI_FSI_STATUS_RDR_FULL BIT_ULL(63)
64 #define SPI_FSI_STATUS_ANY_ERROR \
65 (SPI_FSI_STATUS_ERROR | \
66 SPI_FSI_STATUS_TDR_OVERRUN | SPI_FSI_STATUS_RDR_UNDERRUN | \
67 SPI_FSI_STATUS_RDR_OVERRUN)
68 #define SPI_FSI_PORT_CTRL 0x9
71 struct device *dev; /* SPI controller device */
72 struct fsi_device *fsi; /* FSI2SPI CFAM engine device */
76 struct fsi_spi_sequence {
81 static int fsi_spi_check_mux(struct fsi_device *fsi, struct device *dev)
85 __be32 root_ctrl_8_be;
87 rc = fsi_slave_read(fsi->slave, FSI_MBOX_ROOT_CTRL_8, &root_ctrl_8_be,
88 sizeof(root_ctrl_8_be));
92 root_ctrl_8 = be32_to_cpu(root_ctrl_8_be);
93 dev_dbg(dev, "Root control register 8: %08x\n", root_ctrl_8);
94 if ((root_ctrl_8 & FSI_MBOX_ROOT_CTRL_8_SPI_MUX) ==
95 FSI_MBOX_ROOT_CTRL_8_SPI_MUX)
101 static int fsi_spi_check_status(struct fsi_spi *ctx)
107 rc = fsi_device_read(ctx->fsi, FSI2SPI_STATUS, &sts_be,
112 sts = be32_to_cpu(sts_be);
113 if (sts & FSI2SPI_STATUS_ANY_ERROR) {
114 dev_err(ctx->dev, "Error with FSI2SPI interface: %08x.\n", sts);
121 static int fsi_spi_read_reg(struct fsi_spi *ctx, u32 offset, u64 *value)
126 u32 cmd = offset + ctx->base;
130 if (cmd & FSI2SPI_CMD_WRITE)
133 cmd_be = cpu_to_be32(cmd);
134 rc = fsi_device_write(ctx->fsi, FSI2SPI_CMD, &cmd_be, sizeof(cmd_be));
138 rc = fsi_spi_check_status(ctx);
142 rc = fsi_device_read(ctx->fsi, FSI2SPI_DATA0, &data_be,
147 *value |= (u64)be32_to_cpu(data_be) << 32;
149 rc = fsi_device_read(ctx->fsi, FSI2SPI_DATA1, &data_be,
154 *value |= (u64)be32_to_cpu(data_be);
155 dev_dbg(ctx->dev, "Read %02x[%016llx].\n", offset, *value);
160 static int fsi_spi_write_reg(struct fsi_spi *ctx, u32 offset, u64 value)
165 u32 cmd = offset + ctx->base;
167 if (cmd & FSI2SPI_CMD_WRITE)
170 dev_dbg(ctx->dev, "Write %02x[%016llx].\n", offset, value);
172 data_be = cpu_to_be32(upper_32_bits(value));
173 rc = fsi_device_write(ctx->fsi, FSI2SPI_DATA0, &data_be,
178 data_be = cpu_to_be32(lower_32_bits(value));
179 rc = fsi_device_write(ctx->fsi, FSI2SPI_DATA1, &data_be,
184 cmd_be = cpu_to_be32(cmd | FSI2SPI_CMD_WRITE);
185 rc = fsi_device_write(ctx->fsi, FSI2SPI_CMD, &cmd_be, sizeof(cmd_be));
189 return fsi_spi_check_status(ctx);
192 static int fsi_spi_data_in(u64 in, u8 *rx, int len)
195 int num_bytes = min(len, 8);
197 for (i = 0; i < num_bytes; ++i)
198 rx[i] = (u8)(in >> (8 * ((num_bytes - 1) - i)));
203 static int fsi_spi_data_out(u64 *out, const u8 *tx, int len)
206 int num_bytes = min(len, 8);
207 u8 *out_bytes = (u8 *)out;
209 /* Unused bytes of the tx data should be 0. */
212 for (i = 0; i < num_bytes; ++i)
213 out_bytes[8 - (i + 1)] = tx[i];
218 static int fsi_spi_reset(struct fsi_spi *ctx)
222 dev_dbg(ctx->dev, "Resetting SPI controller.\n");
224 rc = fsi_spi_write_reg(ctx, SPI_FSI_CLOCK_CFG,
225 SPI_FSI_CLOCK_CFG_RESET1);
229 rc = fsi_spi_write_reg(ctx, SPI_FSI_CLOCK_CFG,
230 SPI_FSI_CLOCK_CFG_RESET2);
234 return fsi_spi_write_reg(ctx, SPI_FSI_STATUS, 0ULL);
237 static void fsi_spi_sequence_add(struct fsi_spi_sequence *seq, u8 val)
240 * Add the next byte of instruction to the 8-byte sequence register.
241 * Then decrement the counter so that the next instruction will go in
242 * the right place. Return the index of the slot we just filled in the
245 seq->data |= (u64)val << seq->bit;
249 static void fsi_spi_sequence_init(struct fsi_spi_sequence *seq)
255 static int fsi_spi_transfer_data(struct fsi_spi *ctx,
256 struct spi_transfer *transfer)
261 if (transfer->tx_buf) {
265 const u8 *tx = transfer->tx_buf;
267 while (transfer->len > sent) {
268 nb = fsi_spi_data_out(&out, &tx[sent],
269 (int)transfer->len - sent);
271 rc = fsi_spi_write_reg(ctx, SPI_FSI_DATA_TX, out);
276 rc = fsi_spi_read_reg(ctx, SPI_FSI_STATUS,
281 if (status & SPI_FSI_STATUS_ANY_ERROR) {
282 rc = fsi_spi_reset(ctx);
288 } while (status & SPI_FSI_STATUS_TDR_FULL);
292 } else if (transfer->rx_buf) {
295 u8 *rx = transfer->rx_buf;
297 while (transfer->len > recv) {
299 rc = fsi_spi_read_reg(ctx, SPI_FSI_STATUS,
304 if (status & SPI_FSI_STATUS_ANY_ERROR) {
305 rc = fsi_spi_reset(ctx);
311 } while (!(status & SPI_FSI_STATUS_RDR_FULL));
313 rc = fsi_spi_read_reg(ctx, SPI_FSI_DATA_RX, &in);
317 recv += fsi_spi_data_in(in, &rx[recv],
318 (int)transfer->len - recv);
325 static int fsi_spi_transfer_init(struct fsi_spi *ctx)
331 u64 clock_cfg = 0ULL;
333 u64 wanted_clock_cfg = SPI_FSI_CLOCK_CFG_ECC_DISABLE |
334 SPI_FSI_CLOCK_CFG_SCK_NO_DEL |
335 FIELD_PREP(SPI_FSI_CLOCK_CFG_SCK_DIV, 19);
337 end = jiffies + msecs_to_jiffies(SPI_FSI_INIT_TIMEOUT_MS);
339 if (time_after(jiffies, end))
342 rc = fsi_spi_read_reg(ctx, SPI_FSI_STATUS, &status);
346 seq_state = status & SPI_FSI_STATUS_SEQ_STATE;
348 if (status & (SPI_FSI_STATUS_ANY_ERROR |
349 SPI_FSI_STATUS_TDR_FULL |
350 SPI_FSI_STATUS_RDR_FULL)) {
354 rc = fsi_spi_reset(ctx);
361 } while (seq_state && (seq_state != SPI_FSI_STATUS_SEQ_STATE_IDLE));
363 rc = fsi_spi_write_reg(ctx, SPI_FSI_COUNTER_CFG, 0ULL);
367 rc = fsi_spi_read_reg(ctx, SPI_FSI_CLOCK_CFG, &clock_cfg);
371 if ((clock_cfg & (SPI_FSI_CLOCK_CFG_MM_ENABLE |
372 SPI_FSI_CLOCK_CFG_ECC_DISABLE |
373 SPI_FSI_CLOCK_CFG_MODE |
374 SPI_FSI_CLOCK_CFG_SCK_RECV_DEL |
375 SPI_FSI_CLOCK_CFG_SCK_DIV)) != wanted_clock_cfg)
376 rc = fsi_spi_write_reg(ctx, SPI_FSI_CLOCK_CFG,
382 static int fsi_spi_transfer_one_message(struct spi_controller *ctlr,
383 struct spi_message *mesg)
386 u8 seq_slave = SPI_FSI_SEQUENCE_SEL_SLAVE(mesg->spi->chip_select + 1);
388 struct spi_transfer *transfer;
389 struct fsi_spi *ctx = spi_controller_get_devdata(ctlr);
391 rc = fsi_spi_check_mux(ctx->fsi, ctx->dev);
395 list_for_each_entry(transfer, &mesg->transfers, transfer_list) {
396 struct fsi_spi_sequence seq;
397 struct spi_transfer *next = NULL;
399 /* Sequencer must do shift out (tx) first. */
400 if (!transfer->tx_buf || transfer->len > SPI_FSI_MAX_TX_SIZE) {
405 dev_dbg(ctx->dev, "Start tx of %d bytes.\n", transfer->len);
407 rc = fsi_spi_transfer_init(ctx);
411 fsi_spi_sequence_init(&seq);
412 fsi_spi_sequence_add(&seq, seq_slave);
416 fsi_spi_sequence_add(&seq,
417 SPI_FSI_SEQUENCE_SHIFT_OUT(8));
420 fsi_spi_sequence_add(&seq, SPI_FSI_SEQUENCE_SHIFT_OUT(len));
422 if (!list_is_last(&transfer->transfer_list,
424 next = list_next_entry(transfer, transfer_list);
426 /* Sequencer can only do shift in (rx) after tx. */
430 if (next->len > SPI_FSI_MAX_RX_SIZE) {
435 dev_dbg(ctx->dev, "Sequence rx of %d bytes.\n",
438 shift = SPI_FSI_SEQUENCE_SHIFT_IN(next->len);
439 fsi_spi_sequence_add(&seq, shift);
445 fsi_spi_sequence_add(&seq, SPI_FSI_SEQUENCE_SEL_SLAVE(0));
447 rc = fsi_spi_write_reg(ctx, SPI_FSI_SEQUENCE, seq.data);
451 rc = fsi_spi_transfer_data(ctx, transfer);
456 rc = fsi_spi_transfer_data(ctx, next);
466 spi_finalize_current_message(ctlr);
471 static size_t fsi_spi_max_transfer_size(struct spi_device *spi)
473 return SPI_FSI_MAX_RX_SIZE;
476 static int fsi_spi_probe(struct device *dev)
479 struct device_node *np;
480 int num_controllers_registered = 0;
481 struct fsi_device *fsi = to_fsi_dev(dev);
483 rc = fsi_spi_check_mux(fsi, dev);
487 for_each_available_child_of_node(dev->of_node, np) {
490 struct spi_controller *ctlr;
492 if (of_property_read_u32(np, "reg", &base))
495 ctlr = spi_alloc_master(dev, sizeof(*ctx));
501 ctlr->dev.of_node = np;
502 ctlr->num_chipselect = of_get_available_child_count(np) ?: 1;
503 ctlr->flags = SPI_CONTROLLER_HALF_DUPLEX;
504 ctlr->max_transfer_size = fsi_spi_max_transfer_size;
505 ctlr->transfer_one_message = fsi_spi_transfer_one_message;
507 ctx = spi_controller_get_devdata(ctlr);
508 ctx->dev = &ctlr->dev;
510 ctx->base = base + SPI_FSI_BASE;
512 rc = devm_spi_register_controller(dev, ctlr);
514 spi_controller_put(ctlr);
516 num_controllers_registered++;
519 if (!num_controllers_registered)
525 static const struct fsi_device_id fsi_spi_ids[] = {
526 { FSI_ENGID_SPI, FSI_VERSION_ANY },
529 MODULE_DEVICE_TABLE(fsi, fsi_spi_ids);
531 static struct fsi_driver fsi_spi_driver = {
532 .id_table = fsi_spi_ids,
535 .bus = &fsi_bus_type,
536 .probe = fsi_spi_probe,
539 module_fsi_driver(fsi_spi_driver);
541 MODULE_AUTHOR("Eddie James <eajames@linux.ibm.com>");
542 MODULE_DESCRIPTION("FSI attached SPI controller");
543 MODULE_LICENSE("GPL");