1 /* SPDX-License-Identifier: BSD-3-Clause */
3 #include <device/mmio.h>
4 #include <console/console.h>
10 #include <spi_flash.h>
13 static const struct blsp_spi spi_reg
[] = {
14 /* BLSP0 registers for SPI interface */
17 BLSP0_SPI_IO_CONTROL_REG
,
18 BLSP0_SPI_ERROR_FLAGS_REG
,
19 BLSP0_SPI_ERROR_FLAGS_EN_REG
,
21 BLSP0_QUP_ERROR_FLAGS_REG
,
22 BLSP0_QUP_ERROR_FLAGS_EN_REG
,
23 BLSP0_QUP_OPERATIONAL_REG
,
24 BLSP0_QUP_IO_MODES_REG
,
26 BLSP0_QUP_INPUT_FIFOc_REG(0),
27 BLSP0_QUP_OUTPUT_FIFOc_REG(0),
28 BLSP0_QUP_MX_INPUT_COUNT_REG
,
29 BLSP0_QUP_MX_OUTPUT_COUNT_REG
,
30 BLSP0_QUP_SW_RESET_REG
,
33 BLSP0_QUP_OPERATIONAL_MASK
,
34 BLSP0_SPI_DEASSERT_WAIT_REG
,
37 /* BLSP4 registers for SPI interface */
40 BLSP4_SPI_IO_CONTROL_REG
,
41 BLSP4_SPI_ERROR_FLAGS_REG
,
42 BLSP4_SPI_ERROR_FLAGS_EN_REG
,
44 BLSP4_QUP_ERROR_FLAGS_REG
,
45 BLSP4_QUP_ERROR_FLAGS_EN_REG
,
46 BLSP4_QUP_OPERATIONAL_REG
,
47 BLSP4_QUP_IO_MODES_REG
,
49 BLSP4_QUP_INPUT_FIFOc_REG(0),
50 BLSP4_QUP_OUTPUT_FIFOc_REG(0),
51 BLSP4_QUP_MX_INPUT_COUNT_REG
,
52 BLSP4_QUP_MX_OUTPUT_COUNT_REG
,
53 BLSP4_QUP_SW_RESET_REG
,
56 BLSP4_QUP_OPERATIONAL_MASK
,
57 BLSP4_SPI_DEASSERT_WAIT_REG
,
59 /* BLSP5 registers for SPI interface */
62 BLSP5_SPI_IO_CONTROL_REG
,
63 BLSP5_SPI_ERROR_FLAGS_REG
,
64 BLSP5_SPI_ERROR_FLAGS_EN_REG
,
66 BLSP5_QUP_ERROR_FLAGS_REG
,
67 BLSP5_QUP_ERROR_FLAGS_EN_REG
,
68 BLSP5_QUP_OPERATIONAL_REG
,
69 BLSP5_QUP_IO_MODES_REG
,
71 BLSP5_QUP_INPUT_FIFOc_REG(0),
72 BLSP5_QUP_OUTPUT_FIFOc_REG(0),
73 BLSP5_QUP_MX_INPUT_COUNT_REG
,
74 BLSP5_QUP_MX_OUTPUT_COUNT_REG
,
75 BLSP5_QUP_SW_RESET_REG
,
78 BLSP5_QUP_OPERATIONAL_MASK
,
79 BLSP5_SPI_DEASSERT_WAIT_REG
,
83 static int check_bit_state(void *reg_addr
, int mask
,
84 int val
, int us_delay
)
86 unsigned int count
= TIMEOUT_CNT
;
88 while ((read32(reg_addr
) & mask
) != val
) {
99 * Check whether QUPn State is valid
101 static int check_qup_state_valid(struct qcs_spi_slave
*ds
)
103 return check_bit_state(ds
->regs
->qup_state
, QUP_STATE_VALID_MASK
,
108 * Configure QUPn Core state
110 static int config_spi_state(struct qcs_spi_slave
*ds
, unsigned int state
)
115 ret
= check_qup_state_valid(ds
);
121 /* Set the state to RUN */
122 val
= ((read32(ds
->regs
->qup_state
) & ~QUP_STATE_MASK
)
124 write32(ds
->regs
->qup_state
, val
);
125 ret
= check_qup_state_valid(ds
);
127 case QUP_STATE_RESET
:
128 /* Set the state to RESET */
129 val
= ((read32(ds
->regs
->qup_state
) & ~QUP_STATE_MASK
)
131 write32(ds
->regs
->qup_state
, val
);
132 ret
= check_qup_state_valid(ds
);
135 printk(BIOS_ERR
, "unsupported QUP SPI state : %d\n", state
);
146 static void spi_set_mode(struct qcs_spi_slave
*ds
, unsigned int mode
)
148 unsigned int clk_idle_state
;
149 unsigned int input_first_mode
;
155 input_first_mode
= SPI_CONFIG_INPUT_FIRST
;
159 input_first_mode
= 0;
163 input_first_mode
= SPI_CONFIG_INPUT_FIRST
;
167 input_first_mode
= 0;
170 printk(BIOS_ERR
, "unsupported spi mode : %d\n", mode
);
174 val
= read32(ds
->regs
->spi_config
);
175 val
|= input_first_mode
;
176 write32(ds
->regs
->spi_config
, val
);
177 val
= read32(ds
->regs
->io_control
);
180 val
|= SPI_IO_CTRL_CLOCK_IDLE_HIGH
;
182 val
&= ~SPI_IO_CTRL_CLOCK_IDLE_HIGH
;
184 write32(ds
->regs
->io_control
, val
);
188 * Reset entire QUP and all mini cores
190 static void spi_reset(struct qcs_spi_slave
*ds
)
192 write32(ds
->regs
->qup_sw_reset
, 0x1);
194 check_qup_state_valid(ds
);
197 static struct qcs_spi_slave spi_slave_pool
[3];
199 static struct qcs_spi_slave
*to_qcs_spi(const struct spi_slave
*slave
)
201 struct qcs_spi_slave
*ds
;
204 for (i
= 0; i
< ARRAY_SIZE(spi_slave_pool
); i
++) {
205 ds
= spi_slave_pool
+ i
;
210 if ((ds
->slave
.bus
== slave
->bus
) &&
211 (ds
->slave
.cs
== slave
->cs
))
218 static void write_force_cs(const struct spi_slave
*slave
, int assert)
220 struct qcs_spi_slave
*ds
= to_qcs_spi(slave
);
222 clrsetbits32(ds
->regs
->io_control
,
223 SPI_IO_CTRL_FORCE_CS_MSK
, SPI_IO_CTRL_FORCE_CS_EN
);
225 clrsetbits32(ds
->regs
->io_control
,
226 SPI_IO_CTRL_FORCE_CS_MSK
, SPI_IO_CTRL_FORCE_CS_DIS
);
230 * BLSP QUPn SPI Hardware Initialisation
232 static int spi_hw_init(struct qcs_spi_slave
*ds
)
238 /* QUPn module configuration */
241 /* Set the QUPn state */
242 ret
= config_spi_state(ds
, QUP_STATE_RESET
);
247 * Configure Mini core to SPI core with Input Output enabled,
248 * SPI master, N = 8 bits
250 clrsetbits32(ds
->regs
->qup_config
, QUP_CONFIG_MINI_CORE_MSK
|
252 QUP_CONF_OUTPUT_MSK
|
254 QUP_CONFIG_MINI_CORE_SPI
|
256 QUP_CONF_OUTPUT_ENA
|
257 QUP_CONF_N_SPI_8_BIT_WORD
);
260 * Configure Input first SPI protocol,
261 * SPI master mode and no loopback
263 clrsetbits32(ds
->regs
->spi_config
, SPI_CONFIG_LOOP_BACK_MSK
|
264 SPI_CONFIG_NO_SLAVE_OPER_MSK
,
265 SPI_CONFIG_NO_LOOP_BACK
|
266 SPI_CONFIG_NO_SLAVE_OPER
);
269 * Configure SPI IO Control Register
274 write32(ds
->regs
->io_control
, SPI_IO_CTRL_CLK_ALWAYS_ON
|
275 SPI_IO_CTRL_NO_TRI_STATE
| SPI_IO_CTRL_MX_CS_MODE
);
278 * Configure SPI IO Modes.
279 * OUTPUT_BIT_SHIFT_EN = 1
280 * INPUT_MODE = Block Mode
281 * OUTPUT MODE = Block Mode
283 clrsetbits32(ds
->regs
->qup_io_modes
,
284 QUP_IO_MODES_OUTPUT_BIT_SHIFT_MSK
|
285 QUP_IO_MODES_INPUT_MODE_MSK
|
286 QUP_IO_MODES_OUTPUT_MODE_MSK
,
287 QUP_IO_MODES_OUTPUT_BIT_SHIFT_EN
|
288 QUP_IO_MODES_INPUT_BLOCK_MODE
|
289 QUP_IO_MODES_OUTPUT_BLOCK_MODE
);
291 spi_set_mode(ds
, ds
->mode
);
293 /* Disable Error mask */
294 write32(ds
->regs
->error_flags_en
, 0);
295 write32(ds
->regs
->qup_error_flags_en
, 0);
296 write32(ds
->regs
->qup_deassert_wait
, 0);
303 static int spi_ctrlr_claim_bus(const struct spi_slave
*slave
)
305 struct qcs_spi_slave
*ds
= to_qcs_spi(slave
);
308 ret
= spi_hw_init(ds
);
311 switch (slave
->bus
) {
314 (GPIO(37), 2, GPIO_PULL_DOWN
, GPIO_6MA
, GPIO_INPUT
); // MOSI
316 (GPIO(38), 2, GPIO_PULL_DOWN
, GPIO_6MA
, GPIO_OUTPUT
); // MISO
318 (GPIO(117), 2, GPIO_NO_PULL
, GPIO_6MA
, GPIO_OUTPUT
); // CS
320 (GPIO(118), 2, GPIO_PULL_DOWN
, GPIO_6MA
, GPIO_OUTPUT
);// CLK
324 (GPIO(26), 3, GPIO_NO_PULL
, GPIO_16MA
, GPIO_INPUT
); // MOSI
326 (GPIO(27), 3, GPIO_NO_PULL
, GPIO_16MA
, GPIO_INPUT
); // MISO
328 (GPIO(28), 4, GPIO_PULL_UP
, GPIO_16MA
, GPIO_INPUT
); // CS
330 (GPIO(29), 4, GPIO_NO_PULL
, GPIO_16MA
, GPIO_INPUT
); // CLK
333 printk(BIOS_ERR
, "SPI error: unsupported bus %d "
334 "(Supported buses 0, 1, 2, 3, 4, 5)\n", slave
->bus
);
337 write_force_cs(slave
, 1);
342 static void spi_ctrlr_release_bus(const struct spi_slave
*slave
)
344 struct qcs_spi_slave
*ds
= to_qcs_spi(slave
);
346 /* Reset the SPI hardware */
347 write_force_cs(slave
, 0);
353 * Function to write data to OUTPUT FIFO
355 static void spi_write_byte(struct qcs_spi_slave
*ds
, unsigned char data
)
357 /* Wait for space in the FIFO */
358 while ((read32(ds
->regs
->qup_operational
) & OUTPUT_FIFO_FULL
))
361 /* Write the byte of data */
362 write32(ds
->regs
->qup_output_fifo
, data
);
366 * Function to read data from Input FIFO
368 static unsigned char spi_read_byte(struct qcs_spi_slave
*ds
)
370 /* Wait for Data in FIFO */
371 while (!(read32(ds
->regs
->qup_operational
) & INPUT_FIFO_NOT_EMPTY
))
374 /* Read a byte of data */
375 return read32(ds
->regs
->qup_input_fifo
) & 0xff;
379 * Function to check whether Input or Output FIFO
380 * has data to be serviced
382 static int check_fifo_status(void *reg_addr
)
384 unsigned int count
= TIMEOUT_CNT
;
385 unsigned int status_flag
;
389 val
= read32(reg_addr
);
393 status_flag
= ((val
& OUTPUT_SERVICE_FLAG
) |
394 (val
& INPUT_SERVICE_FLAG
));
395 } while (!status_flag
);
401 * Function to configure Input and Output enable/disable
403 static void enable_io_config(struct qcs_spi_slave
*ds
,
404 uint32_t write_cnt
, uint32_t read_cnt
)
407 clrsetbits32(ds
->regs
->qup_config
,
408 QUP_CONF_OUTPUT_MSK
, QUP_CONF_OUTPUT_ENA
);
410 clrsetbits32(ds
->regs
->qup_config
,
411 QUP_CONF_OUTPUT_MSK
, QUP_CONF_NO_OUTPUT
);
415 clrsetbits32(ds
->regs
->qup_config
,
416 QUP_CONF_INPUT_MSK
, QUP_CONF_INPUT_ENA
);
418 clrsetbits32(ds
->regs
->qup_config
,
419 QUP_CONF_INPUT_MSK
, QUP_CONF_NO_INPUT
);
424 * Function to read bytes number of data from the Input FIFO
426 static int __blsp_spi_read(struct qcs_spi_slave
*ds
, u8
*data_buffer
,
431 unsigned int fifo_count
;
436 /* Configure no of bytes to read */
437 state_config
= config_spi_state(ds
, QUP_STATE_RESET
);
441 /* Configure input and output enable */
442 enable_io_config(ds
, 0, bytes
);
444 write32(ds
->regs
->qup_mx_input_count
, bytes
);
446 state_config
= config_spi_state(ds
, QUP_STATE_RUN
);
451 ret
= check_fifo_status(ds
->regs
->qup_operational
);
455 val
= read32(ds
->regs
->qup_operational
);
456 if (val
& INPUT_SERVICE_FLAG
) {
458 * acknowledge to hw that software will
461 val
&= INPUT_SERVICE_FLAG
;
462 write32(ds
->regs
->qup_operational
, val
);
464 fifo_count
= ((bytes
> SPI_INPUT_BLOCK_SIZE
) ?
465 SPI_INPUT_BLOCK_SIZE
: bytes
);
467 for (i
= 0; i
< fifo_count
; i
++) {
468 *data_buffer
= spi_read_byte(ds
);
475 stopwatch_init_msecs_expire(&sw
, 10);
478 val
= read32(ds
->regs
->qup_operational
);
479 if (stopwatch_expired(&sw
)) {
480 printk(BIOS_ERR
, "SPI FIFO read timeout\n");
484 } while (!(val
& MAX_INPUT_DONE_FLAG
));
488 * Put the SPI Core back in the Reset State
489 * to end the transfer
491 (void)config_spi_state(ds
, QUP_STATE_RESET
);
495 static int blsp_spi_read(struct qcs_spi_slave
*ds
, u8
*data_buffer
,
501 length
= (bytes
< MAX_COUNT_SIZE
) ? bytes
: MAX_COUNT_SIZE
;
503 ret
= __blsp_spi_read(ds
, data_buffer
, length
);
507 data_buffer
+= length
;
515 * Function to write data to the Output FIFO
517 static int __blsp_spi_write(struct qcs_spi_slave
*ds
, const u8
*cmd_buffer
,
522 unsigned int write_len
= bytes
;
523 unsigned int read_len
= bytes
;
524 unsigned int fifo_count
;
529 state_config
= config_spi_state(ds
, QUP_STATE_RESET
);
533 /* Configure input and output enable */
534 enable_io_config(ds
, write_len
, read_len
);
535 /* No of bytes to be written in Output FIFO */
536 write32(ds
->regs
->qup_mx_output_count
, bytes
);
537 write32(ds
->regs
->qup_mx_input_count
, bytes
);
538 state_config
= config_spi_state(ds
, QUP_STATE_RUN
);
544 * read_len considered to ensure that we read the dummy data for the
545 * write we performed. This is needed to ensure with WR-RD transaction
546 * to get the actual data on the subsequent read cycle that happens
548 while (write_len
|| read_len
) {
549 ret
= check_fifo_status(ds
->regs
->qup_operational
);
553 val
= read32(ds
->regs
->qup_operational
);
554 if (val
& OUTPUT_SERVICE_FLAG
) {
556 * acknowledge to hw that software will write
557 * expected output data
559 val
&= OUTPUT_SERVICE_FLAG
;
560 write32(ds
->regs
->qup_operational
, val
);
562 if (write_len
> SPI_OUTPUT_BLOCK_SIZE
)
563 fifo_count
= SPI_OUTPUT_BLOCK_SIZE
;
565 fifo_count
= write_len
;
567 for (i
= 0; i
< fifo_count
; i
++) {
568 /* Write actual data to output FIFO */
569 spi_write_byte(ds
, *cmd_buffer
);
575 if (val
& INPUT_SERVICE_FLAG
) {
577 * acknowledge to hw that software
578 * will read input data
580 val
&= INPUT_SERVICE_FLAG
;
581 write32(ds
->regs
->qup_operational
, val
);
583 if (read_len
> SPI_INPUT_BLOCK_SIZE
)
584 fifo_count
= SPI_INPUT_BLOCK_SIZE
;
586 fifo_count
= read_len
;
588 for (i
= 0; i
< fifo_count
; i
++) {
589 /* Read dummy data for the data written */
590 (void)spi_read_byte(ds
);
592 /* Decrement the read count after reading the
593 * dummy data from the device. This is to make
594 * sure we read dummy data before we write the
602 stopwatch_init_msecs_expire(&sw
, 10);
605 val
= read32(ds
->regs
->qup_operational
);
606 if (stopwatch_expired(&sw
)) {
607 printk(BIOS_ERR
, "SPI FIFO write timeout\n");
612 } while (!(val
& MAX_OUTPUT_DONE_FLAG
));
616 * Put the SPI Core back in the Reset State
617 * to end the transfer
619 (void)config_spi_state(ds
, QUP_STATE_RESET
);
624 static int blsp_spi_write(struct qcs_spi_slave
*ds
, u8
*cmd_buffer
,
630 length
= (bytes
< MAX_COUNT_SIZE
) ? bytes
: MAX_COUNT_SIZE
;
632 ret
= __blsp_spi_write(ds
, cmd_buffer
, length
);
633 if (ret
!= SUCCESS
) {
634 printk(BIOS_ERR
, "SPI:DBG write not success\n");
638 cmd_buffer
+= length
;
646 * This function is invoked with either tx_buf or rx_buf.
647 * Calling this function with both null does a chip select change.
649 static int spi_ctrlr_xfer(const struct spi_slave
*slave
, const void *dout
,
650 size_t out_bytes
, void *din
, size_t in_bytes
)
652 struct qcs_spi_slave
*ds
= to_qcs_spi(slave
);
653 u8
*txp
= (u8
*)dout
;
657 ret
= config_spi_state(ds
, QUP_STATE_RESET
);
662 ret
= blsp_spi_write(ds
, txp
, (unsigned int)out_bytes
);
668 ret
= blsp_spi_read(ds
, rxp
, in_bytes
);
675 * Put the SPI Core back in the Reset State
676 * to end the transfer
678 (void)config_spi_state(ds
, QUP_STATE_RESET
);
683 static int spi_ctrlr_setup(const struct spi_slave
*slave
)
685 struct qcs_spi_slave
*ds
= NULL
;
687 unsigned int bus
= slave
->bus
;
688 unsigned int cs
= slave
->cs
;
692 if (((bus
!= BLSP4_SPI
) && (bus
!= BLSP5_SPI
)) || cs
!= 0) {
694 "SPI error: unsupported bus %d or cs %d\n", bus
, cs
);
698 for (i
= 0; i
< ARRAY_SIZE(spi_slave_pool
); i
++) {
699 if (spi_slave_pool
[i
].allocated
)
701 ds
= spi_slave_pool
+ i
;
704 ds
->regs
= &spi_reg
[bus
];
705 ds
->mode
= SPI_MODE0
;
708 if (bus
== BLSP4_SPI
) {
714 clock_configure_spi(blsp
, qup
, ds
->freq
);
715 clock_enable_spi(blsp
, qup
);
722 printk(BIOS_ERR
, "SPI error: all %d pools busy\n", i
);
726 static int xfer_vectors(const struct spi_slave
*slave
,
727 struct spi_op vectors
[], size_t count
)
729 return spi_flash_vector_helper(slave
, vectors
, count
, spi_ctrlr_xfer
);
732 static const struct spi_ctrlr spi_ctrlr
= {
733 .setup
= spi_ctrlr_setup
,
734 .claim_bus
= spi_ctrlr_claim_bus
,
735 .release_bus
= spi_ctrlr_release_bus
,
736 .xfer
= spi_ctrlr_xfer
,
737 .xfer_vector
= xfer_vectors
,
738 .max_xfer_size
= MAX_PACKET_COUNT
,
741 const struct spi_ctrlr_buses spi_ctrlr_bus_map
[] = {
744 .bus_start
= BLSP5_SPI
,
745 .bus_end
= BLSP5_SPI
,
749 .bus_start
= BLSP4_SPI
,
750 .bus_end
= BLSP4_SPI
,
754 const size_t spi_ctrlr_bus_map_count
= ARRAY_SIZE(spi_ctrlr_bus_map
);