1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Broadcom BRCMSTB, NSP, NS2, Cygnus SPI Controllers
5 * Copyright 2016 Broadcom
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
14 #include <linux/ioport.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/of_irq.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21 #include <linux/spi/spi.h>
22 #include <linux/spi/spi-mem.h>
23 #include <linux/sysfs.h>
24 #include <linux/types.h>
25 #include "spi-bcm-qspi.h"
27 #define DRIVER_NAME "bcm_qspi"
30 /* BSPI register offsets */
31 #define BSPI_REVISION_ID 0x000
32 #define BSPI_SCRATCH 0x004
33 #define BSPI_MAST_N_BOOT_CTRL 0x008
34 #define BSPI_BUSY_STATUS 0x00c
35 #define BSPI_INTR_STATUS 0x010
36 #define BSPI_B0_STATUS 0x014
37 #define BSPI_B0_CTRL 0x018
38 #define BSPI_B1_STATUS 0x01c
39 #define BSPI_B1_CTRL 0x020
40 #define BSPI_STRAP_OVERRIDE_CTRL 0x024
41 #define BSPI_FLEX_MODE_ENABLE 0x028
42 #define BSPI_BITS_PER_CYCLE 0x02c
43 #define BSPI_BITS_PER_PHASE 0x030
44 #define BSPI_CMD_AND_MODE_BYTE 0x034
45 #define BSPI_BSPI_FLASH_UPPER_ADDR_BYTE 0x038
46 #define BSPI_BSPI_XOR_VALUE 0x03c
47 #define BSPI_BSPI_XOR_ENABLE 0x040
48 #define BSPI_BSPI_PIO_MODE_ENABLE 0x044
49 #define BSPI_BSPI_PIO_IODIR 0x048
50 #define BSPI_BSPI_PIO_DATA 0x04c
52 /* RAF register offsets */
53 #define BSPI_RAF_START_ADDR 0x100
54 #define BSPI_RAF_NUM_WORDS 0x104
55 #define BSPI_RAF_CTRL 0x108
56 #define BSPI_RAF_FULLNESS 0x10c
57 #define BSPI_RAF_WATERMARK 0x110
58 #define BSPI_RAF_STATUS 0x114
59 #define BSPI_RAF_READ_DATA 0x118
60 #define BSPI_RAF_WORD_CNT 0x11c
61 #define BSPI_RAF_CURR_ADDR 0x120
63 /* Override mode masks */
64 #define BSPI_STRAP_OVERRIDE_CTRL_OVERRIDE BIT(0)
65 #define BSPI_STRAP_OVERRIDE_CTRL_DATA_DUAL BIT(1)
66 #define BSPI_STRAP_OVERRIDE_CTRL_ADDR_4BYTE BIT(2)
67 #define BSPI_STRAP_OVERRIDE_CTRL_DATA_QUAD BIT(3)
68 #define BSPI_STRAP_OVERRIDE_CTRL_ENDAIN_MODE BIT(4)
70 #define BSPI_ADDRLEN_3BYTES 3
71 #define BSPI_ADDRLEN_4BYTES 4
73 #define BSPI_RAF_STATUS_FIFO_EMPTY_MASK BIT(1)
75 #define BSPI_RAF_CTRL_START_MASK BIT(0)
76 #define BSPI_RAF_CTRL_CLEAR_MASK BIT(1)
78 #define BSPI_BPP_MODE_SELECT_MASK BIT(8)
79 #define BSPI_BPP_ADDR_SELECT_MASK BIT(16)
81 #define BSPI_READ_LENGTH 256
83 /* MSPI register offsets */
84 #define MSPI_SPCR0_LSB 0x000
85 #define MSPI_SPCR0_MSB 0x004
86 #define MSPI_SPCR0_MSB_CPHA BIT(0)
87 #define MSPI_SPCR0_MSB_CPOL BIT(1)
88 #define MSPI_SPCR0_MSB_BITS_SHIFT 0x2
89 #define MSPI_SPCR1_LSB 0x008
90 #define MSPI_SPCR1_MSB 0x00c
91 #define MSPI_NEWQP 0x010
92 #define MSPI_ENDQP 0x014
93 #define MSPI_SPCR2 0x018
94 #define MSPI_MSPI_STATUS 0x020
95 #define MSPI_CPTQP 0x024
96 #define MSPI_SPCR3 0x028
97 #define MSPI_REV 0x02c
98 #define MSPI_TXRAM 0x040
99 #define MSPI_RXRAM 0x0c0
100 #define MSPI_CDRAM 0x140
101 #define MSPI_WRITE_LOCK 0x180
103 #define MSPI_MASTER_BIT BIT(7)
105 #define MSPI_NUM_CDRAM 16
106 #define MSPI_CDRAM_OUTP BIT(8)
107 #define MSPI_CDRAM_CONT_BIT BIT(7)
108 #define MSPI_CDRAM_BITSE_BIT BIT(6)
109 #define MSPI_CDRAM_DT_BIT BIT(5)
110 #define MSPI_CDRAM_PCS 0xf
112 #define MSPI_SPCR2_SPE BIT(6)
113 #define MSPI_SPCR2_CONT_AFTER_CMD BIT(7)
115 #define MSPI_SPCR3_FASTBR BIT(0)
116 #define MSPI_SPCR3_FASTDT BIT(1)
117 #define MSPI_SPCR3_SYSCLKSEL_MASK GENMASK(11, 10)
118 #define MSPI_SPCR3_SYSCLKSEL_27 (MSPI_SPCR3_SYSCLKSEL_MASK & \
119 ~(BIT(10) | BIT(11)))
120 #define MSPI_SPCR3_SYSCLKSEL_108 (MSPI_SPCR3_SYSCLKSEL_MASK & \
122 #define MSPI_SPCR3_TXRXDAM_MASK GENMASK(4, 2)
123 #define MSPI_SPCR3_DAM_8BYTE 0
124 #define MSPI_SPCR3_DAM_16BYTE (BIT(2) | BIT(4))
125 #define MSPI_SPCR3_DAM_32BYTE (BIT(3) | BIT(5))
126 #define MSPI_SPCR3_HALFDUPLEX BIT(6)
127 #define MSPI_SPCR3_HDOUTTYPE BIT(7)
128 #define MSPI_SPCR3_DATA_REG_SZ BIT(8)
129 #define MSPI_SPCR3_CPHARX BIT(9)
131 #define MSPI_MSPI_STATUS_SPIF BIT(0)
133 #define INTR_BASE_BIT_SHIFT 0x02
134 #define INTR_COUNT 0x07
136 #define NUM_CHIPSELECT 4
137 #define QSPI_SPBR_MAX 255U
138 #define MSPI_BASE_FREQ 27000000UL
140 #define OPCODE_DIOR 0xBB
141 #define OPCODE_QIOR 0xEB
142 #define OPCODE_DIOR_4B 0xBC
143 #define OPCODE_QIOR_4B 0xEC
145 #define MAX_CMD_SIZE 6
147 #define ADDR_4MB_MASK GENMASK(22, 0)
149 /* stop at end of transfer, no other reason */
150 #define TRANS_STATUS_BREAK_NONE 0
151 /* stop at end of spi_message */
152 #define TRANS_STATUS_BREAK_EOM 1
153 /* stop at end of spi_transfer if delay */
154 #define TRANS_STATUS_BREAK_DELAY 2
155 /* stop at end of spi_transfer if cs_change */
156 #define TRANS_STATUS_BREAK_CS_CHANGE 4
157 /* stop if we run out of bytes */
158 #define TRANS_STATUS_BREAK_NO_BYTES 8
160 /* events that make us stop filling TX slots */
161 #define TRANS_STATUS_BREAK_TX (TRANS_STATUS_BREAK_EOM | \
162 TRANS_STATUS_BREAK_DELAY | \
163 TRANS_STATUS_BREAK_CS_CHANGE)
165 /* events that make us deassert CS */
166 #define TRANS_STATUS_BREAK_DESELECT (TRANS_STATUS_BREAK_EOM | \
167 TRANS_STATUS_BREAK_CS_CHANGE)
170 * Used for writing and reading data in the right order
171 * to TXRAM and RXRAM when used as 32-bit registers respectively
173 #define swap4bytes(__val) \
174 ((((__val) >> 24) & 0x000000FF) | (((__val) >> 8) & 0x0000FF00) | \
175 (((__val) << 8) & 0x00FF0000) | (((__val) << 24) & 0xFF000000))
177 struct bcm_qspi_parms {
183 struct bcm_xfer_mode {
186 unsigned int addrlen;
202 struct bcm_qspi_irq {
203 const char *irq_name;
204 const irq_handler_t irq_handler;
209 struct bcm_qspi_dev_id {
210 const struct bcm_qspi_irq *irqp;
216 struct spi_transfer *trans;
218 bool mspi_last_trans;
222 struct platform_device *pdev;
223 struct spi_controller *host;
227 void __iomem *base[BASEMAX];
229 /* Some SoCs provide custom interrupt status register(s) */
230 struct bcm_qspi_soc_intc *soc_intc;
232 struct bcm_qspi_parms last_parms;
233 struct qspi_trans trans_pos;
238 const struct spi_mem_op *bspi_rf_op;
241 u32 bspi_rf_op_status;
242 struct bcm_xfer_mode xfer_mode;
243 u32 s3_strap_override_ctrl;
247 struct bcm_qspi_dev_id *dev_ids;
248 struct completion mspi_done;
249 struct completion bspi_done;
252 bool mspi_spcr3_sysclk;
255 static inline bool has_bspi(struct bcm_qspi *qspi)
257 return qspi->bspi_mode;
260 /* hardware supports spcr3 and fast baud-rate */
261 static inline bool bcm_qspi_has_fastbr(struct bcm_qspi *qspi)
263 if (!has_bspi(qspi) &&
264 ((qspi->mspi_maj_rev >= 1) &&
265 (qspi->mspi_min_rev >= 5)))
271 /* hardware supports sys clk 108Mhz */
272 static inline bool bcm_qspi_has_sysclk_108(struct bcm_qspi *qspi)
274 if (!has_bspi(qspi) && (qspi->mspi_spcr3_sysclk ||
275 ((qspi->mspi_maj_rev >= 1) &&
276 (qspi->mspi_min_rev >= 6))))
282 static inline int bcm_qspi_spbr_min(struct bcm_qspi *qspi)
284 if (bcm_qspi_has_fastbr(qspi))
285 return (bcm_qspi_has_sysclk_108(qspi) ? 4 : 1);
290 static u32 bcm_qspi_calc_spbr(u32 clk_speed_hz,
291 const struct bcm_qspi_parms *xp)
295 /* SPBR = System Clock/(2 * SCK Baud Rate) */
297 spbr = clk_speed_hz / (xp->speed_hz * 2);
302 /* Read qspi controller register*/
303 static inline u32 bcm_qspi_read(struct bcm_qspi *qspi, enum base_type type,
306 return bcm_qspi_readl(qspi->big_endian, qspi->base[type] + offset);
309 /* Write qspi controller register*/
310 static inline void bcm_qspi_write(struct bcm_qspi *qspi, enum base_type type,
311 unsigned int offset, unsigned int data)
313 bcm_qspi_writel(qspi->big_endian, data, qspi->base[type] + offset);
317 static int bcm_qspi_bspi_busy_poll(struct bcm_qspi *qspi)
321 /* this should normally finish within 10us */
322 for (i = 0; i < 1000; i++) {
323 if (!(bcm_qspi_read(qspi, BSPI, BSPI_BUSY_STATUS) & 1))
327 dev_warn(&qspi->pdev->dev, "timeout waiting for !busy_status\n");
331 static inline bool bcm_qspi_bspi_ver_three(struct bcm_qspi *qspi)
333 if (qspi->bspi_maj_rev < 4)
338 static void bcm_qspi_bspi_flush_prefetch_buffers(struct bcm_qspi *qspi)
340 bcm_qspi_bspi_busy_poll(qspi);
341 /* Force rising edge for the b0/b1 'flush' field */
342 bcm_qspi_write(qspi, BSPI, BSPI_B0_CTRL, 1);
343 bcm_qspi_write(qspi, BSPI, BSPI_B1_CTRL, 1);
344 bcm_qspi_write(qspi, BSPI, BSPI_B0_CTRL, 0);
345 bcm_qspi_write(qspi, BSPI, BSPI_B1_CTRL, 0);
348 static int bcm_qspi_bspi_lr_is_fifo_empty(struct bcm_qspi *qspi)
350 return (bcm_qspi_read(qspi, BSPI, BSPI_RAF_STATUS) &
351 BSPI_RAF_STATUS_FIFO_EMPTY_MASK);
354 static inline u32 bcm_qspi_bspi_lr_read_fifo(struct bcm_qspi *qspi)
356 u32 data = bcm_qspi_read(qspi, BSPI, BSPI_RAF_READ_DATA);
358 /* BSPI v3 LR is LE only, convert data to host endianness */
359 if (bcm_qspi_bspi_ver_three(qspi))
360 data = le32_to_cpu(data);
365 static inline void bcm_qspi_bspi_lr_start(struct bcm_qspi *qspi)
367 bcm_qspi_bspi_busy_poll(qspi);
368 bcm_qspi_write(qspi, BSPI, BSPI_RAF_CTRL,
369 BSPI_RAF_CTRL_START_MASK);
372 static inline void bcm_qspi_bspi_lr_clear(struct bcm_qspi *qspi)
374 bcm_qspi_write(qspi, BSPI, BSPI_RAF_CTRL,
375 BSPI_RAF_CTRL_CLEAR_MASK);
376 bcm_qspi_bspi_flush_prefetch_buffers(qspi);
379 static void bcm_qspi_bspi_lr_data_read(struct bcm_qspi *qspi)
381 u32 *buf = (u32 *)qspi->bspi_rf_op->data.buf.in;
384 dev_dbg(&qspi->pdev->dev, "xfer %p rx %p rxlen %d\n", qspi->bspi_rf_op,
385 qspi->bspi_rf_op->data.buf.in, qspi->bspi_rf_op_len);
386 while (!bcm_qspi_bspi_lr_is_fifo_empty(qspi)) {
387 data = bcm_qspi_bspi_lr_read_fifo(qspi);
388 if (likely(qspi->bspi_rf_op_len >= 4) &&
389 IS_ALIGNED((uintptr_t)buf, 4)) {
390 buf[qspi->bspi_rf_op_idx++] = data;
391 qspi->bspi_rf_op_len -= 4;
393 /* Read out remaining bytes, make sure*/
394 u8 *cbuf = (u8 *)&buf[qspi->bspi_rf_op_idx];
396 data = cpu_to_le32(data);
397 while (qspi->bspi_rf_op_len) {
400 qspi->bspi_rf_op_len--;
406 static void bcm_qspi_bspi_set_xfer_params(struct bcm_qspi *qspi, u8 cmd_byte,
407 int bpp, int bpc, int flex_mode)
409 bcm_qspi_write(qspi, BSPI, BSPI_FLEX_MODE_ENABLE, 0);
410 bcm_qspi_write(qspi, BSPI, BSPI_BITS_PER_CYCLE, bpc);
411 bcm_qspi_write(qspi, BSPI, BSPI_BITS_PER_PHASE, bpp);
412 bcm_qspi_write(qspi, BSPI, BSPI_CMD_AND_MODE_BYTE, cmd_byte);
413 bcm_qspi_write(qspi, BSPI, BSPI_FLEX_MODE_ENABLE, flex_mode);
416 static int bcm_qspi_bspi_set_flex_mode(struct bcm_qspi *qspi,
417 const struct spi_mem_op *op, int hp)
419 int bpc = 0, bpp = 0;
420 u8 command = op->cmd.opcode;
421 int width = op->data.buswidth ? op->data.buswidth : SPI_NBITS_SINGLE;
422 int addrlen = op->addr.nbytes;
425 dev_dbg(&qspi->pdev->dev, "set flex mode w %x addrlen %x hp %d\n",
428 if (addrlen == BSPI_ADDRLEN_4BYTES)
429 bpp = BSPI_BPP_ADDR_SELECT_MASK;
431 if (op->dummy.nbytes)
432 bpp |= (op->dummy.nbytes * 8) / op->dummy.buswidth;
435 case SPI_NBITS_SINGLE:
436 if (addrlen == BSPI_ADDRLEN_3BYTES)
437 /* default mode, does not need flex_cmd */
443 bpc |= 0x00010100; /* address and mode are 2-bit */
444 bpp = BSPI_BPP_MODE_SELECT_MASK;
450 bpc |= 0x00020200; /* address and mode are 4-bit */
451 bpp |= BSPI_BPP_MODE_SELECT_MASK;
458 bcm_qspi_bspi_set_xfer_params(qspi, command, bpp, bpc, flex_mode);
463 static int bcm_qspi_bspi_set_override(struct bcm_qspi *qspi,
464 const struct spi_mem_op *op, int hp)
466 int width = op->data.buswidth ? op->data.buswidth : SPI_NBITS_SINGLE;
467 int addrlen = op->addr.nbytes;
468 u32 data = bcm_qspi_read(qspi, BSPI, BSPI_STRAP_OVERRIDE_CTRL);
470 dev_dbg(&qspi->pdev->dev, "set override mode w %x addrlen %x hp %d\n",
474 case SPI_NBITS_SINGLE:
475 /* clear quad/dual mode */
476 data &= ~(BSPI_STRAP_OVERRIDE_CTRL_DATA_QUAD |
477 BSPI_STRAP_OVERRIDE_CTRL_DATA_DUAL);
480 /* clear dual mode and set quad mode */
481 data &= ~BSPI_STRAP_OVERRIDE_CTRL_DATA_DUAL;
482 data |= BSPI_STRAP_OVERRIDE_CTRL_DATA_QUAD;
485 /* clear quad mode set dual mode */
486 data &= ~BSPI_STRAP_OVERRIDE_CTRL_DATA_QUAD;
487 data |= BSPI_STRAP_OVERRIDE_CTRL_DATA_DUAL;
493 if (addrlen == BSPI_ADDRLEN_4BYTES)
495 data |= BSPI_STRAP_OVERRIDE_CTRL_ADDR_4BYTE;
497 /* clear 4 byte mode */
498 data &= ~BSPI_STRAP_OVERRIDE_CTRL_ADDR_4BYTE;
500 /* set the override mode */
501 data |= BSPI_STRAP_OVERRIDE_CTRL_OVERRIDE;
502 bcm_qspi_write(qspi, BSPI, BSPI_STRAP_OVERRIDE_CTRL, data);
503 bcm_qspi_bspi_set_xfer_params(qspi, op->cmd.opcode, 0, 0, 0);
508 static int bcm_qspi_bspi_set_mode(struct bcm_qspi *qspi,
509 const struct spi_mem_op *op, int hp)
512 int width = op->data.buswidth ? op->data.buswidth : SPI_NBITS_SINGLE;
513 int addrlen = op->addr.nbytes;
516 qspi->xfer_mode.flex_mode = true;
518 if (!bcm_qspi_bspi_ver_three(qspi)) {
521 val = bcm_qspi_read(qspi, BSPI, BSPI_STRAP_OVERRIDE_CTRL);
522 mask = BSPI_STRAP_OVERRIDE_CTRL_OVERRIDE;
523 if (val & mask || qspi->s3_strap_override_ctrl & mask) {
524 qspi->xfer_mode.flex_mode = false;
525 bcm_qspi_write(qspi, BSPI, BSPI_FLEX_MODE_ENABLE, 0);
526 error = bcm_qspi_bspi_set_override(qspi, op, hp);
530 if (qspi->xfer_mode.flex_mode)
531 error = bcm_qspi_bspi_set_flex_mode(qspi, op, hp);
534 dev_warn(&qspi->pdev->dev,
535 "INVALID COMBINATION: width=%d addrlen=%d hp=%d\n",
537 } else if (qspi->xfer_mode.width != width ||
538 qspi->xfer_mode.addrlen != addrlen ||
539 qspi->xfer_mode.hp != hp) {
540 qspi->xfer_mode.width = width;
541 qspi->xfer_mode.addrlen = addrlen;
542 qspi->xfer_mode.hp = hp;
543 dev_dbg(&qspi->pdev->dev,
544 "cs:%d %d-lane output, %d-byte address%s\n",
546 qspi->xfer_mode.width,
547 qspi->xfer_mode.addrlen,
548 qspi->xfer_mode.hp != -1 ? ", hp mode" : "");
554 static void bcm_qspi_enable_bspi(struct bcm_qspi *qspi)
559 qspi->bspi_enabled = 1;
560 if ((bcm_qspi_read(qspi, BSPI, BSPI_MAST_N_BOOT_CTRL) & 1) == 0)
563 bcm_qspi_bspi_flush_prefetch_buffers(qspi);
565 bcm_qspi_write(qspi, BSPI, BSPI_MAST_N_BOOT_CTRL, 0);
569 static void bcm_qspi_disable_bspi(struct bcm_qspi *qspi)
574 qspi->bspi_enabled = 0;
575 if ((bcm_qspi_read(qspi, BSPI, BSPI_MAST_N_BOOT_CTRL) & 1))
578 bcm_qspi_bspi_busy_poll(qspi);
579 bcm_qspi_write(qspi, BSPI, BSPI_MAST_N_BOOT_CTRL, 1);
583 static void bcm_qspi_chip_select(struct bcm_qspi *qspi, int cs)
588 if (cs >= 0 && qspi->base[CHIP_SELECT]) {
589 rd = bcm_qspi_read(qspi, CHIP_SELECT, 0);
590 wr = (rd & ~0xff) | (1 << cs);
593 bcm_qspi_write(qspi, CHIP_SELECT, 0, wr);
594 usleep_range(10, 20);
597 dev_dbg(&qspi->pdev->dev, "using cs:%d\n", cs);
601 static bool bcmspi_parms_did_change(const struct bcm_qspi_parms * const cur,
602 const struct bcm_qspi_parms * const prev)
604 return (cur->speed_hz != prev->speed_hz) ||
605 (cur->mode != prev->mode) ||
606 (cur->bits_per_word != prev->bits_per_word);
611 static void bcm_qspi_hw_set_parms(struct bcm_qspi *qspi,
612 const struct bcm_qspi_parms *xp)
616 if (!bcmspi_parms_did_change(xp, &qspi->last_parms))
619 if (!qspi->mspi_maj_rev)
620 /* legacy controller */
621 spcr = MSPI_MASTER_BIT;
626 * Bits per transfer. BITS determines the number of data bits
627 * transferred if the command control bit (BITSE of a
628 * CDRAM Register) is equal to 1.
629 * If CDRAM BITSE is equal to 0, 8 data bits are transferred
632 if (xp->bits_per_word != 16 && xp->bits_per_word != 64)
633 spcr |= xp->bits_per_word << MSPI_SPCR0_MSB_BITS_SHIFT;
635 spcr |= xp->mode & (MSPI_SPCR0_MSB_CPHA | MSPI_SPCR0_MSB_CPOL);
636 bcm_qspi_write(qspi, MSPI, MSPI_SPCR0_MSB, spcr);
638 if (bcm_qspi_has_fastbr(qspi)) {
642 spcr |= MSPI_SPCR3_FASTBR;
644 if (xp->mode & SPI_3WIRE)
645 spcr |= MSPI_SPCR3_HALFDUPLEX | MSPI_SPCR3_HDOUTTYPE;
647 if (bcm_qspi_has_sysclk_108(qspi)) {
648 /* check requested baud rate before moving to 108Mhz */
649 spbr = bcm_qspi_calc_spbr(MSPI_BASE_FREQ * 4, xp);
650 if (spbr > QSPI_SPBR_MAX) {
651 /* use SYSCLK_27Mhz for slower baud rates */
652 spcr &= ~MSPI_SPCR3_SYSCLKSEL_MASK;
653 qspi->base_clk = MSPI_BASE_FREQ;
656 spcr |= MSPI_SPCR3_SYSCLKSEL_108;
657 qspi->base_clk = MSPI_BASE_FREQ * 4;
661 if (xp->bits_per_word > 16) {
662 /* data_reg_size 1 (64bit) */
663 spcr |= MSPI_SPCR3_DATA_REG_SZ;
664 /* TxRx RAM data access mode 2 for 32B and set fastdt */
665 spcr |= MSPI_SPCR3_DAM_32BYTE | MSPI_SPCR3_FASTDT;
667 * Set length of delay after transfer
668 * DTL from 0(256) to 1
670 bcm_qspi_write(qspi, MSPI, MSPI_SPCR1_LSB, 1);
672 /* data_reg_size[8] = 0 */
673 spcr &= ~(MSPI_SPCR3_DATA_REG_SZ);
676 * TxRx RAM access mode 8B
679 spcr &= ~(MSPI_SPCR3_DAM_32BYTE);
681 bcm_qspi_write(qspi, MSPI, MSPI_SPCR3, spcr);
684 /* SCK Baud Rate = System Clock/(2 * SPBR) */
685 qspi->max_speed_hz = qspi->base_clk / (bcm_qspi_spbr_min(qspi) * 2);
686 spbr = bcm_qspi_calc_spbr(qspi->base_clk, xp);
687 spbr = clamp_val(spbr, bcm_qspi_spbr_min(qspi), QSPI_SPBR_MAX);
688 bcm_qspi_write(qspi, MSPI, MSPI_SPCR0_LSB, spbr);
690 qspi->last_parms = *xp;
693 static void bcm_qspi_update_parms(struct bcm_qspi *qspi,
694 struct spi_device *spi,
695 struct spi_transfer *trans)
697 struct bcm_qspi_parms xp;
699 xp.speed_hz = trans->speed_hz;
700 xp.bits_per_word = trans->bits_per_word;
703 bcm_qspi_hw_set_parms(qspi, &xp);
706 static int bcm_qspi_setup(struct spi_device *spi)
708 struct bcm_qspi_parms *xp;
710 if (spi->bits_per_word > 64)
713 xp = spi_get_ctldata(spi);
715 xp = kzalloc(sizeof(*xp), GFP_KERNEL);
718 spi_set_ctldata(spi, xp);
720 xp->speed_hz = spi->max_speed_hz;
721 xp->mode = spi->mode;
723 if (spi->bits_per_word)
724 xp->bits_per_word = spi->bits_per_word;
726 xp->bits_per_word = 8;
731 static bool bcm_qspi_mspi_transfer_is_last(struct bcm_qspi *qspi,
732 struct qspi_trans *qt)
734 if (qt->mspi_last_trans &&
735 spi_transfer_is_last(qspi->host, qt->trans))
741 static int update_qspi_trans_byte_count(struct bcm_qspi *qspi,
742 struct qspi_trans *qt, int flags)
744 int ret = TRANS_STATUS_BREAK_NONE;
746 /* count the last transferred bytes */
747 if (qt->trans->bits_per_word <= 8)
749 else if (qt->trans->bits_per_word <= 16)
751 else if (qt->trans->bits_per_word <= 32)
753 else if (qt->trans->bits_per_word <= 64)
756 if (qt->byte >= qt->trans->len) {
757 /* we're at the end of the spi_transfer */
758 /* in TX mode, need to pause for a delay or CS change */
759 if (qt->trans->delay.value &&
760 (flags & TRANS_STATUS_BREAK_DELAY))
761 ret |= TRANS_STATUS_BREAK_DELAY;
762 if (qt->trans->cs_change &&
763 (flags & TRANS_STATUS_BREAK_CS_CHANGE))
764 ret |= TRANS_STATUS_BREAK_CS_CHANGE;
766 if (bcm_qspi_mspi_transfer_is_last(qspi, qt))
767 ret |= TRANS_STATUS_BREAK_EOM;
769 ret |= TRANS_STATUS_BREAK_NO_BYTES;
774 dev_dbg(&qspi->pdev->dev, "trans %p len %d byte %d ret %x\n",
775 qt->trans, qt->trans ? qt->trans->len : 0, qt->byte, ret);
779 static inline u8 read_rxram_slot_u8(struct bcm_qspi *qspi, int slot)
781 u32 slot_offset = MSPI_RXRAM + (slot << 3) + 0x4;
783 /* mask out reserved bits */
784 return bcm_qspi_read(qspi, MSPI, slot_offset) & 0xff;
787 static inline u16 read_rxram_slot_u16(struct bcm_qspi *qspi, int slot)
789 u32 reg_offset = MSPI_RXRAM;
790 u32 lsb_offset = reg_offset + (slot << 3) + 0x4;
791 u32 msb_offset = reg_offset + (slot << 3);
793 return (bcm_qspi_read(qspi, MSPI, lsb_offset) & 0xff) |
794 ((bcm_qspi_read(qspi, MSPI, msb_offset) & 0xff) << 8);
797 static inline u32 read_rxram_slot_u32(struct bcm_qspi *qspi, int slot)
799 u32 reg_offset = MSPI_RXRAM;
800 u32 offset = reg_offset + (slot << 3);
803 val = bcm_qspi_read(qspi, MSPI, offset);
804 val = swap4bytes(val);
809 static inline u64 read_rxram_slot_u64(struct bcm_qspi *qspi, int slot)
811 u32 reg_offset = MSPI_RXRAM;
812 u32 lsb_offset = reg_offset + (slot << 3) + 0x4;
813 u32 msb_offset = reg_offset + (slot << 3);
816 msb = bcm_qspi_read(qspi, MSPI, msb_offset);
817 msb = swap4bytes(msb);
818 lsb = bcm_qspi_read(qspi, MSPI, lsb_offset);
819 lsb = swap4bytes(lsb);
821 return ((u64)msb << 32 | lsb);
824 static void read_from_hw(struct bcm_qspi *qspi, int slots)
826 struct qspi_trans tp;
829 bcm_qspi_disable_bspi(qspi);
831 if (slots > MSPI_NUM_CDRAM) {
832 /* should never happen */
833 dev_err(&qspi->pdev->dev, "%s: too many slots!\n", __func__);
837 tp = qspi->trans_pos;
839 for (slot = 0; slot < slots; slot++) {
840 if (tp.trans->bits_per_word <= 8) {
841 u8 *buf = tp.trans->rx_buf;
844 buf[tp.byte] = read_rxram_slot_u8(qspi, slot);
845 dev_dbg(&qspi->pdev->dev, "RD %02x\n",
846 buf ? buf[tp.byte] : 0x0);
847 } else if (tp.trans->bits_per_word <= 16) {
848 u16 *buf = tp.trans->rx_buf;
851 buf[tp.byte / 2] = read_rxram_slot_u16(qspi,
853 dev_dbg(&qspi->pdev->dev, "RD %04x\n",
854 buf ? buf[tp.byte / 2] : 0x0);
855 } else if (tp.trans->bits_per_word <= 32) {
856 u32 *buf = tp.trans->rx_buf;
859 buf[tp.byte / 4] = read_rxram_slot_u32(qspi,
861 dev_dbg(&qspi->pdev->dev, "RD %08x\n",
862 buf ? buf[tp.byte / 4] : 0x0);
864 } else if (tp.trans->bits_per_word <= 64) {
865 u64 *buf = tp.trans->rx_buf;
868 buf[tp.byte / 8] = read_rxram_slot_u64(qspi,
870 dev_dbg(&qspi->pdev->dev, "RD %llx\n",
871 buf ? buf[tp.byte / 8] : 0x0);
876 update_qspi_trans_byte_count(qspi, &tp,
877 TRANS_STATUS_BREAK_NONE);
880 qspi->trans_pos = tp;
883 static inline void write_txram_slot_u8(struct bcm_qspi *qspi, int slot,
886 u32 reg_offset = MSPI_TXRAM + (slot << 3);
888 /* mask out reserved bits */
889 bcm_qspi_write(qspi, MSPI, reg_offset, val);
892 static inline void write_txram_slot_u16(struct bcm_qspi *qspi, int slot,
895 u32 reg_offset = MSPI_TXRAM;
896 u32 msb_offset = reg_offset + (slot << 3);
897 u32 lsb_offset = reg_offset + (slot << 3) + 0x4;
899 bcm_qspi_write(qspi, MSPI, msb_offset, (val >> 8));
900 bcm_qspi_write(qspi, MSPI, lsb_offset, (val & 0xff));
903 static inline void write_txram_slot_u32(struct bcm_qspi *qspi, int slot,
906 u32 reg_offset = MSPI_TXRAM;
907 u32 msb_offset = reg_offset + (slot << 3);
909 bcm_qspi_write(qspi, MSPI, msb_offset, swap4bytes(val));
912 static inline void write_txram_slot_u64(struct bcm_qspi *qspi, int slot,
915 u32 reg_offset = MSPI_TXRAM;
916 u32 msb_offset = reg_offset + (slot << 3);
917 u32 lsb_offset = reg_offset + (slot << 3) + 0x4;
918 u32 msb = upper_32_bits(val);
919 u32 lsb = lower_32_bits(val);
921 bcm_qspi_write(qspi, MSPI, msb_offset, swap4bytes(msb));
922 bcm_qspi_write(qspi, MSPI, lsb_offset, swap4bytes(lsb));
925 static inline u32 read_cdram_slot(struct bcm_qspi *qspi, int slot)
927 return bcm_qspi_read(qspi, MSPI, MSPI_CDRAM + (slot << 2));
930 static inline void write_cdram_slot(struct bcm_qspi *qspi, int slot, u32 val)
932 bcm_qspi_write(qspi, MSPI, (MSPI_CDRAM + (slot << 2)), val);
935 /* Return number of slots written */
936 static int write_to_hw(struct bcm_qspi *qspi, struct spi_device *spi)
938 struct qspi_trans tp;
939 int slot = 0, tstatus = 0;
942 bcm_qspi_disable_bspi(qspi);
943 tp = qspi->trans_pos;
944 bcm_qspi_update_parms(qspi, spi, tp.trans);
946 /* Run until end of transfer or reached the max data */
947 while (!tstatus && slot < MSPI_NUM_CDRAM) {
948 mspi_cdram = MSPI_CDRAM_CONT_BIT;
949 if (tp.trans->bits_per_word <= 8) {
950 const u8 *buf = tp.trans->tx_buf;
951 u8 val = buf ? buf[tp.byte] : 0x00;
953 write_txram_slot_u8(qspi, slot, val);
954 dev_dbg(&qspi->pdev->dev, "WR %02x\n", val);
955 } else if (tp.trans->bits_per_word <= 16) {
956 const u16 *buf = tp.trans->tx_buf;
957 u16 val = buf ? buf[tp.byte / 2] : 0x0000;
959 write_txram_slot_u16(qspi, slot, val);
960 dev_dbg(&qspi->pdev->dev, "WR %04x\n", val);
961 } else if (tp.trans->bits_per_word <= 32) {
962 const u32 *buf = tp.trans->tx_buf;
963 u32 val = buf ? buf[tp.byte/4] : 0x0;
965 write_txram_slot_u32(qspi, slot, val);
966 dev_dbg(&qspi->pdev->dev, "WR %08x\n", val);
967 } else if (tp.trans->bits_per_word <= 64) {
968 const u64 *buf = tp.trans->tx_buf;
969 u64 val = (buf ? buf[tp.byte/8] : 0x0);
971 /* use the length of delay from SPCR1_LSB */
972 if (bcm_qspi_has_fastbr(qspi))
973 mspi_cdram |= MSPI_CDRAM_DT_BIT;
975 write_txram_slot_u64(qspi, slot, val);
976 dev_dbg(&qspi->pdev->dev, "WR %llx\n", val);
979 mspi_cdram |= ((tp.trans->bits_per_word <= 8) ? 0 :
980 MSPI_CDRAM_BITSE_BIT);
982 /* set 3wrire halfduplex mode data from host to target */
983 if ((spi->mode & SPI_3WIRE) && tp.trans->tx_buf)
984 mspi_cdram |= MSPI_CDRAM_OUTP;
989 mspi_cdram |= (~(1 << spi_get_chipselect(spi, 0)) &
992 write_cdram_slot(qspi, slot, mspi_cdram);
994 tstatus = update_qspi_trans_byte_count(qspi, &tp,
995 TRANS_STATUS_BREAK_TX);
1000 dev_err(&qspi->pdev->dev, "%s: no data to send?", __func__);
1004 dev_dbg(&qspi->pdev->dev, "submitting %d slots\n", slot);
1005 bcm_qspi_write(qspi, MSPI, MSPI_NEWQP, 0);
1006 bcm_qspi_write(qspi, MSPI, MSPI_ENDQP, slot - 1);
1009 * case 1) EOM =1, cs_change =0: SSb inactive
1010 * case 2) EOM =1, cs_change =1: SSb stay active
1011 * case 3) EOM =0, cs_change =0: SSb stay active
1012 * case 4) EOM =0, cs_change =1: SSb inactive
1014 if (((tstatus & TRANS_STATUS_BREAK_DESELECT)
1015 == TRANS_STATUS_BREAK_CS_CHANGE) ||
1016 ((tstatus & TRANS_STATUS_BREAK_DESELECT)
1017 == TRANS_STATUS_BREAK_EOM)) {
1018 mspi_cdram = read_cdram_slot(qspi, slot - 1) &
1019 ~MSPI_CDRAM_CONT_BIT;
1020 write_cdram_slot(qspi, slot - 1, mspi_cdram);
1024 bcm_qspi_write(qspi, MSPI, MSPI_WRITE_LOCK, 1);
1026 /* Must flush previous writes before starting MSPI operation */
1028 /* Set cont | spe | spifie */
1029 bcm_qspi_write(qspi, MSPI, MSPI_SPCR2, 0xe0);
1035 static int bcm_qspi_bspi_exec_mem_op(struct spi_device *spi,
1036 const struct spi_mem_op *op)
1038 struct bcm_qspi *qspi = spi_controller_get_devdata(spi->controller);
1039 u32 addr = 0, len, rdlen, len_words, from = 0;
1041 unsigned long timeo = msecs_to_jiffies(100);
1042 struct bcm_qspi_soc_intc *soc_intc = qspi->soc_intc;
1044 if (bcm_qspi_bspi_ver_three(qspi))
1045 if (op->addr.nbytes == BSPI_ADDRLEN_4BYTES)
1048 from = op->addr.val;
1049 if (!spi_get_csgpiod(spi, 0))
1050 bcm_qspi_chip_select(qspi, spi_get_chipselect(spi, 0));
1051 bcm_qspi_write(qspi, MSPI, MSPI_WRITE_LOCK, 0);
1054 * when using flex mode we need to send
1055 * the upper address byte to bspi
1057 if (!bcm_qspi_bspi_ver_three(qspi)) {
1058 addr = from & 0xff000000;
1059 bcm_qspi_write(qspi, BSPI,
1060 BSPI_BSPI_FLASH_UPPER_ADDR_BYTE, addr);
1063 if (!qspi->xfer_mode.flex_mode)
1066 addr = from & 0x00ffffff;
1068 if (bcm_qspi_bspi_ver_three(qspi) == true)
1069 addr = (addr + 0xc00000) & 0xffffff;
1072 * read into the entire buffer by breaking the reads
1073 * into RAF buffer read lengths
1075 len = op->data.nbytes;
1076 qspi->bspi_rf_op_idx = 0;
1079 if (len > BSPI_READ_LENGTH)
1080 rdlen = BSPI_READ_LENGTH;
1084 reinit_completion(&qspi->bspi_done);
1085 bcm_qspi_enable_bspi(qspi);
1086 len_words = (rdlen + 3) >> 2;
1087 qspi->bspi_rf_op = op;
1088 qspi->bspi_rf_op_status = 0;
1089 qspi->bspi_rf_op_len = rdlen;
1090 dev_dbg(&qspi->pdev->dev,
1091 "bspi xfr addr 0x%x len 0x%x", addr, rdlen);
1092 bcm_qspi_write(qspi, BSPI, BSPI_RAF_START_ADDR, addr);
1093 bcm_qspi_write(qspi, BSPI, BSPI_RAF_NUM_WORDS, len_words);
1094 bcm_qspi_write(qspi, BSPI, BSPI_RAF_WATERMARK, 0);
1095 if (qspi->soc_intc) {
1097 * clear soc MSPI and BSPI interrupts and enable
1100 soc_intc->bcm_qspi_int_ack(soc_intc, MSPI_BSPI_DONE);
1101 soc_intc->bcm_qspi_int_set(soc_intc, BSPI_DONE, true);
1104 /* Must flush previous writes before starting BSPI operation */
1106 bcm_qspi_bspi_lr_start(qspi);
1107 if (!wait_for_completion_timeout(&qspi->bspi_done, timeo)) {
1108 dev_err(&qspi->pdev->dev, "timeout waiting for BSPI\n");
1113 /* set msg return length */
1121 static int bcm_qspi_transfer_one(struct spi_controller *host,
1122 struct spi_device *spi,
1123 struct spi_transfer *trans)
1125 struct bcm_qspi *qspi = spi_controller_get_devdata(host);
1127 unsigned long timeo = msecs_to_jiffies(100);
1129 if (!spi_get_csgpiod(spi, 0))
1130 bcm_qspi_chip_select(qspi, spi_get_chipselect(spi, 0));
1131 qspi->trans_pos.trans = trans;
1132 qspi->trans_pos.byte = 0;
1134 while (qspi->trans_pos.byte < trans->len) {
1135 reinit_completion(&qspi->mspi_done);
1137 slots = write_to_hw(qspi, spi);
1138 if (!wait_for_completion_timeout(&qspi->mspi_done, timeo)) {
1139 dev_err(&qspi->pdev->dev, "timeout waiting for MSPI\n");
1143 read_from_hw(qspi, slots);
1145 bcm_qspi_enable_bspi(qspi);
1150 static int bcm_qspi_mspi_exec_mem_op(struct spi_device *spi,
1151 const struct spi_mem_op *op)
1153 struct spi_controller *host = spi->controller;
1154 struct bcm_qspi *qspi = spi_controller_get_devdata(host);
1155 struct spi_transfer t[2];
1159 memset(cmd, 0, sizeof(cmd));
1160 memset(t, 0, sizeof(t));
1163 /* opcode is in cmd[0] */
1164 cmd[0] = op->cmd.opcode;
1165 for (i = 0; i < op->addr.nbytes; i++)
1166 cmd[1 + i] = op->addr.val >> (8 * (op->addr.nbytes - i - 1));
1169 t[0].len = op->addr.nbytes + op->dummy.nbytes + 1;
1170 t[0].bits_per_word = spi->bits_per_word;
1171 t[0].tx_nbits = op->cmd.buswidth;
1172 /* lets mspi know that this is not last transfer */
1173 qspi->trans_pos.mspi_last_trans = false;
1174 ret = bcm_qspi_transfer_one(host, spi, &t[0]);
1177 qspi->trans_pos.mspi_last_trans = true;
1180 t[1].rx_buf = op->data.buf.in;
1181 t[1].len = op->data.nbytes;
1182 t[1].rx_nbits = op->data.buswidth;
1183 t[1].bits_per_word = spi->bits_per_word;
1184 ret = bcm_qspi_transfer_one(host, spi, &t[1]);
1190 static int bcm_qspi_exec_mem_op(struct spi_mem *mem,
1191 const struct spi_mem_op *op)
1193 struct spi_device *spi = mem->spi;
1194 struct bcm_qspi *qspi = spi_controller_get_devdata(spi->controller);
1196 bool mspi_read = false;
1200 if (!op->data.nbytes || !op->addr.nbytes || op->addr.nbytes > 4 ||
1201 op->data.dir != SPI_MEM_DATA_IN)
1204 buf = op->data.buf.in;
1205 addr = op->addr.val;
1206 len = op->data.nbytes;
1208 if (has_bspi(qspi) && bcm_qspi_bspi_ver_three(qspi) == true) {
1210 * The address coming into this function is a raw flash offset.
1211 * But for BSPI <= V3, we need to convert it to a remapped BSPI
1212 * address. If it crosses a 4MB boundary, just revert back to
1215 addr = (addr + 0xc00000) & 0xffffff;
1217 if ((~ADDR_4MB_MASK & addr) ^
1218 (~ADDR_4MB_MASK & (addr + len - 1)))
1222 /* non-aligned and very short transfers are handled by MSPI */
1223 if (!IS_ALIGNED((uintptr_t)addr, 4) || !IS_ALIGNED((uintptr_t)buf, 4) ||
1227 if (!has_bspi(qspi) || mspi_read)
1228 return bcm_qspi_mspi_exec_mem_op(spi, op);
1230 ret = bcm_qspi_bspi_set_mode(qspi, op, 0);
1233 ret = bcm_qspi_bspi_exec_mem_op(spi, op);
1238 static void bcm_qspi_cleanup(struct spi_device *spi)
1240 struct bcm_qspi_parms *xp = spi_get_ctldata(spi);
1245 static irqreturn_t bcm_qspi_mspi_l2_isr(int irq, void *dev_id)
1247 struct bcm_qspi_dev_id *qspi_dev_id = dev_id;
1248 struct bcm_qspi *qspi = qspi_dev_id->dev;
1249 u32 status = bcm_qspi_read(qspi, MSPI, MSPI_MSPI_STATUS);
1251 if (status & MSPI_MSPI_STATUS_SPIF) {
1252 struct bcm_qspi_soc_intc *soc_intc = qspi->soc_intc;
1253 /* clear interrupt */
1254 status &= ~MSPI_MSPI_STATUS_SPIF;
1255 bcm_qspi_write(qspi, MSPI, MSPI_MSPI_STATUS, status);
1257 soc_intc->bcm_qspi_int_ack(soc_intc, MSPI_DONE);
1258 complete(&qspi->mspi_done);
1265 static irqreturn_t bcm_qspi_bspi_lr_l2_isr(int irq, void *dev_id)
1267 struct bcm_qspi_dev_id *qspi_dev_id = dev_id;
1268 struct bcm_qspi *qspi = qspi_dev_id->dev;
1269 struct bcm_qspi_soc_intc *soc_intc = qspi->soc_intc;
1270 u32 status = qspi_dev_id->irqp->mask;
1272 if (qspi->bspi_enabled && qspi->bspi_rf_op) {
1273 bcm_qspi_bspi_lr_data_read(qspi);
1274 if (qspi->bspi_rf_op_len == 0) {
1275 qspi->bspi_rf_op = NULL;
1276 if (qspi->soc_intc) {
1277 /* disable soc BSPI interrupt */
1278 soc_intc->bcm_qspi_int_set(soc_intc, BSPI_DONE,
1281 status = INTR_BSPI_LR_SESSION_DONE_MASK;
1284 if (qspi->bspi_rf_op_status)
1285 bcm_qspi_bspi_lr_clear(qspi);
1287 bcm_qspi_bspi_flush_prefetch_buffers(qspi);
1291 /* clear soc BSPI interrupt */
1292 soc_intc->bcm_qspi_int_ack(soc_intc, BSPI_DONE);
1295 status &= INTR_BSPI_LR_SESSION_DONE_MASK;
1296 if (qspi->bspi_enabled && status && qspi->bspi_rf_op_len == 0)
1297 complete(&qspi->bspi_done);
1302 static irqreturn_t bcm_qspi_bspi_lr_err_l2_isr(int irq, void *dev_id)
1304 struct bcm_qspi_dev_id *qspi_dev_id = dev_id;
1305 struct bcm_qspi *qspi = qspi_dev_id->dev;
1306 struct bcm_qspi_soc_intc *soc_intc = qspi->soc_intc;
1308 dev_err(&qspi->pdev->dev, "BSPI INT error\n");
1309 qspi->bspi_rf_op_status = -EIO;
1311 /* clear soc interrupt */
1312 soc_intc->bcm_qspi_int_ack(soc_intc, BSPI_ERR);
1314 complete(&qspi->bspi_done);
1318 static irqreturn_t bcm_qspi_l1_isr(int irq, void *dev_id)
1320 struct bcm_qspi_dev_id *qspi_dev_id = dev_id;
1321 struct bcm_qspi *qspi = qspi_dev_id->dev;
1322 struct bcm_qspi_soc_intc *soc_intc = qspi->soc_intc;
1323 irqreturn_t ret = IRQ_NONE;
1326 u32 status = soc_intc->bcm_qspi_get_int_status(soc_intc);
1328 if (status & MSPI_DONE)
1329 ret = bcm_qspi_mspi_l2_isr(irq, dev_id);
1330 else if (status & BSPI_DONE)
1331 ret = bcm_qspi_bspi_lr_l2_isr(irq, dev_id);
1332 else if (status & BSPI_ERR)
1333 ret = bcm_qspi_bspi_lr_err_l2_isr(irq, dev_id);
1339 static const struct bcm_qspi_irq qspi_irq_tab[] = {
1341 .irq_name = "spi_lr_fullness_reached",
1342 .irq_handler = bcm_qspi_bspi_lr_l2_isr,
1343 .mask = INTR_BSPI_LR_FULLNESS_REACHED_MASK,
1346 .irq_name = "spi_lr_session_aborted",
1347 .irq_handler = bcm_qspi_bspi_lr_err_l2_isr,
1348 .mask = INTR_BSPI_LR_SESSION_ABORTED_MASK,
1351 .irq_name = "spi_lr_impatient",
1352 .irq_handler = bcm_qspi_bspi_lr_err_l2_isr,
1353 .mask = INTR_BSPI_LR_IMPATIENT_MASK,
1356 .irq_name = "spi_lr_session_done",
1357 .irq_handler = bcm_qspi_bspi_lr_l2_isr,
1358 .mask = INTR_BSPI_LR_SESSION_DONE_MASK,
1360 #ifdef QSPI_INT_DEBUG
1361 /* this interrupt is for debug purposes only, dont request irq */
1363 .irq_name = "spi_lr_overread",
1364 .irq_handler = bcm_qspi_bspi_lr_err_l2_isr,
1365 .mask = INTR_BSPI_LR_OVERREAD_MASK,
1369 .irq_name = "mspi_done",
1370 .irq_handler = bcm_qspi_mspi_l2_isr,
1371 .mask = INTR_MSPI_DONE_MASK,
1374 .irq_name = "mspi_halted",
1375 .irq_handler = bcm_qspi_mspi_l2_isr,
1376 .mask = INTR_MSPI_HALTED_MASK,
1379 /* single muxed L1 interrupt source */
1380 .irq_name = "spi_l1_intr",
1381 .irq_handler = bcm_qspi_l1_isr,
1382 .irq_source = MUXED_L1,
1383 .mask = QSPI_INTERRUPTS_ALL,
1387 static void bcm_qspi_bspi_init(struct bcm_qspi *qspi)
1391 val = bcm_qspi_read(qspi, BSPI, BSPI_REVISION_ID);
1392 qspi->bspi_maj_rev = (val >> 8) & 0xff;
1393 qspi->bspi_min_rev = val & 0xff;
1394 if (!(bcm_qspi_bspi_ver_three(qspi))) {
1395 /* Force mapping of BSPI address -> flash offset */
1396 bcm_qspi_write(qspi, BSPI, BSPI_BSPI_XOR_VALUE, 0);
1397 bcm_qspi_write(qspi, BSPI, BSPI_BSPI_XOR_ENABLE, 1);
1399 qspi->bspi_enabled = 1;
1400 bcm_qspi_disable_bspi(qspi);
1401 bcm_qspi_write(qspi, BSPI, BSPI_B0_CTRL, 0);
1402 bcm_qspi_write(qspi, BSPI, BSPI_B1_CTRL, 0);
1405 static void bcm_qspi_hw_init(struct bcm_qspi *qspi)
1407 struct bcm_qspi_parms parms;
1409 bcm_qspi_write(qspi, MSPI, MSPI_SPCR1_LSB, 0);
1410 bcm_qspi_write(qspi, MSPI, MSPI_SPCR1_MSB, 0);
1411 bcm_qspi_write(qspi, MSPI, MSPI_NEWQP, 0);
1412 bcm_qspi_write(qspi, MSPI, MSPI_ENDQP, 0);
1413 bcm_qspi_write(qspi, MSPI, MSPI_SPCR2, 0x20);
1415 parms.mode = SPI_MODE_3;
1416 parms.bits_per_word = 8;
1417 parms.speed_hz = qspi->max_speed_hz;
1418 bcm_qspi_hw_set_parms(qspi, &parms);
1421 bcm_qspi_bspi_init(qspi);
1424 static void bcm_qspi_hw_uninit(struct bcm_qspi *qspi)
1426 u32 status = bcm_qspi_read(qspi, MSPI, MSPI_MSPI_STATUS);
1428 bcm_qspi_write(qspi, MSPI, MSPI_SPCR2, 0);
1430 bcm_qspi_write(qspi, MSPI, MSPI_WRITE_LOCK, 0);
1432 /* clear interrupt */
1433 bcm_qspi_write(qspi, MSPI, MSPI_MSPI_STATUS, status & ~1);
1436 static const struct spi_controller_mem_ops bcm_qspi_mem_ops = {
1437 .exec_op = bcm_qspi_exec_mem_op,
1440 struct bcm_qspi_data {
1442 bool has_spcr3_sysclk;
1445 static const struct bcm_qspi_data bcm_qspi_no_rev_data = {
1446 .has_mspi_rev = false,
1447 .has_spcr3_sysclk = false,
1450 static const struct bcm_qspi_data bcm_qspi_rev_data = {
1451 .has_mspi_rev = true,
1452 .has_spcr3_sysclk = false,
1455 static const struct bcm_qspi_data bcm_qspi_spcr3_data = {
1456 .has_mspi_rev = true,
1457 .has_spcr3_sysclk = true,
1460 static const struct of_device_id bcm_qspi_of_match[] __maybe_unused = {
1462 .compatible = "brcm,spi-bcm7445-qspi",
1463 .data = &bcm_qspi_rev_data,
1467 .compatible = "brcm,spi-bcm-qspi",
1468 .data = &bcm_qspi_no_rev_data,
1471 .compatible = "brcm,spi-bcm7216-qspi",
1472 .data = &bcm_qspi_spcr3_data,
1475 .compatible = "brcm,spi-bcm7278-qspi",
1476 .data = &bcm_qspi_spcr3_data,
1480 MODULE_DEVICE_TABLE(of, bcm_qspi_of_match);
1482 int bcm_qspi_probe(struct platform_device *pdev,
1483 struct bcm_qspi_soc_intc *soc_intc)
1485 const struct of_device_id *of_id = NULL;
1486 const struct bcm_qspi_data *data;
1487 struct device *dev = &pdev->dev;
1488 struct bcm_qspi *qspi;
1489 struct spi_controller *host;
1490 struct resource *res;
1491 int irq, ret = 0, num_ints = 0;
1494 const char *name = NULL;
1495 int num_irqs = ARRAY_SIZE(qspi_irq_tab);
1497 /* We only support device-tree instantiation */
1501 of_id = of_match_node(bcm_qspi_of_match, dev->of_node);
1507 host = devm_spi_alloc_host(dev, sizeof(struct bcm_qspi));
1509 dev_err(dev, "error allocating spi_controller\n");
1513 qspi = spi_controller_get_devdata(host);
1515 qspi->clk = devm_clk_get_optional(&pdev->dev, NULL);
1516 if (IS_ERR(qspi->clk))
1517 return PTR_ERR(qspi->clk);
1520 qspi->trans_pos.trans = NULL;
1521 qspi->trans_pos.byte = 0;
1522 qspi->trans_pos.mspi_last_trans = true;
1526 host->mode_bits = SPI_CPHA | SPI_CPOL | SPI_RX_DUAL | SPI_RX_QUAD |
1528 host->setup = bcm_qspi_setup;
1529 host->transfer_one = bcm_qspi_transfer_one;
1530 host->mem_ops = &bcm_qspi_mem_ops;
1531 host->cleanup = bcm_qspi_cleanup;
1532 host->dev.of_node = dev->of_node;
1533 host->num_chipselect = NUM_CHIPSELECT;
1534 host->use_gpio_descriptors = true;
1536 qspi->big_endian = of_device_is_big_endian(dev->of_node);
1538 if (!of_property_read_u32(dev->of_node, "num-cs", &val))
1539 host->num_chipselect = val;
1541 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hif_mspi");
1543 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1546 qspi->base[MSPI] = devm_ioremap_resource(dev, res);
1547 if (IS_ERR(qspi->base[MSPI]))
1548 return PTR_ERR(qspi->base[MSPI]);
1550 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "bspi");
1552 qspi->base[BSPI] = devm_ioremap_resource(dev, res);
1553 if (IS_ERR(qspi->base[BSPI]))
1554 return PTR_ERR(qspi->base[BSPI]);
1555 qspi->bspi_mode = true;
1557 qspi->bspi_mode = false;
1560 dev_info(dev, "using %smspi mode\n", qspi->bspi_mode ? "bspi-" : "");
1562 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs_reg");
1564 qspi->base[CHIP_SELECT] = devm_ioremap_resource(dev, res);
1565 if (IS_ERR(qspi->base[CHIP_SELECT]))
1566 return PTR_ERR(qspi->base[CHIP_SELECT]);
1569 qspi->dev_ids = kcalloc(num_irqs, sizeof(struct bcm_qspi_dev_id),
1575 * Some SoCs integrate spi controller (e.g., its interrupt bits)
1579 qspi->soc_intc = soc_intc;
1580 soc_intc->bcm_qspi_int_set(soc_intc, MSPI_DONE, true);
1582 qspi->soc_intc = NULL;
1586 ret = clk_prepare_enable(qspi->clk);
1588 dev_err(dev, "failed to prepare clock\n");
1589 goto qspi_probe_err;
1591 qspi->base_clk = clk_get_rate(qspi->clk);
1593 qspi->base_clk = MSPI_BASE_FREQ;
1596 if (data->has_mspi_rev) {
1597 rev = bcm_qspi_read(qspi, MSPI, MSPI_REV);
1598 /* some older revs do not have a MSPI_REV register */
1599 if ((rev & 0xff) == 0xff)
1603 qspi->mspi_maj_rev = (rev >> 4) & 0xf;
1604 qspi->mspi_min_rev = rev & 0xf;
1605 qspi->mspi_spcr3_sysclk = data->has_spcr3_sysclk;
1607 qspi->max_speed_hz = qspi->base_clk / (bcm_qspi_spbr_min(qspi) * 2);
1610 * On SW resets it is possible to have the mask still enabled
1611 * Need to disable the mask and clear the status while we init
1613 bcm_qspi_hw_uninit(qspi);
1615 for (val = 0; val < num_irqs; val++) {
1617 name = qspi_irq_tab[val].irq_name;
1618 if (qspi_irq_tab[val].irq_source == SINGLE_L2) {
1619 /* get the l2 interrupts */
1620 irq = platform_get_irq_byname_optional(pdev, name);
1621 } else if (!num_ints && soc_intc) {
1622 /* all mspi, bspi intrs muxed to one L1 intr */
1623 irq = platform_get_irq(pdev, 0);
1627 ret = devm_request_irq(&pdev->dev, irq,
1628 qspi_irq_tab[val].irq_handler, 0,
1630 &qspi->dev_ids[val]);
1632 dev_err(&pdev->dev, "IRQ %s not found\n", name);
1633 goto qspi_unprepare_err;
1636 qspi->dev_ids[val].dev = qspi;
1637 qspi->dev_ids[val].irqp = &qspi_irq_tab[val];
1639 dev_dbg(&pdev->dev, "registered IRQ %s %d\n",
1640 qspi_irq_tab[val].irq_name,
1646 dev_err(&pdev->dev, "no IRQs registered, cannot init driver\n");
1648 goto qspi_unprepare_err;
1651 bcm_qspi_hw_init(qspi);
1652 init_completion(&qspi->mspi_done);
1653 init_completion(&qspi->bspi_done);
1656 platform_set_drvdata(pdev, qspi);
1658 qspi->xfer_mode.width = -1;
1659 qspi->xfer_mode.addrlen = -1;
1660 qspi->xfer_mode.hp = -1;
1662 ret = spi_register_controller(host);
1664 dev_err(dev, "can't register host\n");
1671 bcm_qspi_hw_uninit(qspi);
1673 clk_disable_unprepare(qspi->clk);
1675 kfree(qspi->dev_ids);
1678 /* probe function to be called by SoC specific platform driver probe */
1679 EXPORT_SYMBOL_GPL(bcm_qspi_probe);
1681 void bcm_qspi_remove(struct platform_device *pdev)
1683 struct bcm_qspi *qspi = platform_get_drvdata(pdev);
1685 spi_unregister_controller(qspi->host);
1686 bcm_qspi_hw_uninit(qspi);
1687 clk_disable_unprepare(qspi->clk);
1688 kfree(qspi->dev_ids);
1691 /* function to be called by SoC specific platform driver remove() */
1692 EXPORT_SYMBOL_GPL(bcm_qspi_remove);
1694 static int __maybe_unused bcm_qspi_suspend(struct device *dev)
1696 struct bcm_qspi *qspi = dev_get_drvdata(dev);
1698 /* store the override strap value */
1699 if (!bcm_qspi_bspi_ver_three(qspi))
1700 qspi->s3_strap_override_ctrl =
1701 bcm_qspi_read(qspi, BSPI, BSPI_STRAP_OVERRIDE_CTRL);
1703 spi_controller_suspend(qspi->host);
1704 clk_disable_unprepare(qspi->clk);
1705 bcm_qspi_hw_uninit(qspi);
1710 static int __maybe_unused bcm_qspi_resume(struct device *dev)
1712 struct bcm_qspi *qspi = dev_get_drvdata(dev);
1715 bcm_qspi_hw_init(qspi);
1716 bcm_qspi_chip_select(qspi, qspi->curr_cs);
1718 /* enable MSPI interrupt */
1719 qspi->soc_intc->bcm_qspi_int_set(qspi->soc_intc, MSPI_DONE,
1722 ret = clk_prepare_enable(qspi->clk);
1724 spi_controller_resume(qspi->host);
1729 SIMPLE_DEV_PM_OPS(bcm_qspi_pm_ops, bcm_qspi_suspend, bcm_qspi_resume);
1731 /* pm_ops to be called by SoC specific platform driver */
1732 EXPORT_SYMBOL_GPL(bcm_qspi_pm_ops);
1734 MODULE_AUTHOR("Kamal Dasu");
1735 MODULE_DESCRIPTION("Broadcom QSPI driver");
1736 MODULE_LICENSE("GPL v2");
1737 MODULE_ALIAS("platform:" DRIVER_NAME);