1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Renesas RZ/V2M Clocked Serial Interface (CSI) driver
5 * Copyright (C) 2023 Renesas Electronics Corporation
8 #include <linux/bits.h>
10 #include <linux/count_zeros.h>
11 #include <linux/interrupt.h>
12 #include <linux/iopoll.h>
13 #include <linux/log2.h>
14 #include <linux/platform_device.h>
15 #include <linux/property.h>
16 #include <linux/reset.h>
17 #include <linux/spi/spi.h>
18 #include <linux/units.h>
21 #define CSI_MODE 0x00 /* CSI mode control */
22 #define CSI_CLKSEL 0x04 /* CSI clock select */
23 #define CSI_CNT 0x08 /* CSI control */
24 #define CSI_INT 0x0C /* CSI interrupt status */
25 #define CSI_IFIFOL 0x10 /* CSI receive FIFO level display */
26 #define CSI_OFIFOL 0x14 /* CSI transmit FIFO level display */
27 #define CSI_IFIFO 0x18 /* CSI receive window */
28 #define CSI_OFIFO 0x1C /* CSI transmit window */
29 #define CSI_FIFOTRG 0x20 /* CSI FIFO trigger level */
32 #define CSI_MODE_CSIE BIT(7)
33 #define CSI_MODE_TRMD BIT(6)
34 #define CSI_MODE_CCL BIT(5)
35 #define CSI_MODE_DIR BIT(4)
36 #define CSI_MODE_CSOT BIT(0)
38 #define CSI_MODE_SETUP 0x00000040
41 #define CSI_CLKSEL_CKP BIT(17)
42 #define CSI_CLKSEL_DAP BIT(16)
43 #define CSI_CLKSEL_MODE (CSI_CLKSEL_CKP|CSI_CLKSEL_DAP)
44 #define CSI_CLKSEL_SLAVE BIT(15)
45 #define CSI_CLKSEL_CKS GENMASK(14, 1)
48 #define CSI_CNT_CSIRST BIT(28)
49 #define CSI_CNT_R_TRGEN BIT(19)
50 #define CSI_CNT_UNDER_E BIT(13)
51 #define CSI_CNT_OVERF_E BIT(12)
52 #define CSI_CNT_TREND_E BIT(9)
53 #define CSI_CNT_CSIEND_E BIT(8)
54 #define CSI_CNT_T_TRGR_E BIT(4)
55 #define CSI_CNT_R_TRGR_E BIT(0)
58 #define CSI_INT_UNDER BIT(13)
59 #define CSI_INT_OVERF BIT(12)
60 #define CSI_INT_TREND BIT(9)
61 #define CSI_INT_CSIEND BIT(8)
62 #define CSI_INT_T_TRGR BIT(4)
63 #define CSI_INT_R_TRGR BIT(0)
66 #define CSI_FIFOTRG_R_TRG GENMASK(2, 0)
68 #define CSI_FIFO_SIZE_BYTES 32U
69 #define CSI_FIFO_HALF_SIZE 16U
70 #define CSI_EN_DIS_TIMEOUT_US 100
72 * Clock "csiclk" gets divided by 2 * CSI_CLKSEL_CKS in order to generate the
73 * serial clock (output from master), with CSI_CLKSEL_CKS ranging from 0x1 (that
74 * means "csiclk" is divided by 2) to 0x3FFF ("csiclk" is divided by 32766).
76 #define CSI_CKS_MAX GENMASK(13, 0)
78 #define UNDERRUN_ERROR BIT(0)
79 #define OVERFLOW_ERROR BIT(1)
80 #define TX_TIMEOUT_ERROR BIT(2)
81 #define RX_TIMEOUT_ERROR BIT(3)
83 #define CSI_MAX_SPI_SCKO (8 * HZ_PER_MHZ)
85 struct rzv2m_csi_priv {
90 struct spi_controller *controller;
93 unsigned int buffer_len;
94 unsigned int bytes_sent;
95 unsigned int bytes_received;
96 unsigned int bytes_to_transfer;
97 unsigned int words_to_transfer;
98 unsigned int bytes_per_word;
99 wait_queue_head_t wait;
104 static void rzv2m_csi_reg_write_bit(const struct rzv2m_csi_priv *csi,
105 int reg_offs, int bit_mask, u32 value)
110 nr_zeros = count_trailing_zeros(bit_mask);
113 tmp = (readl(csi->base + reg_offs) & ~bit_mask) | value;
114 writel(tmp, csi->base + reg_offs);
117 static int rzv2m_csi_sw_reset(struct rzv2m_csi_priv *csi, int assert)
121 rzv2m_csi_reg_write_bit(csi, CSI_CNT, CSI_CNT_CSIRST, assert);
126 return readl_poll_timeout(csi->base + CSI_MODE, reg,
127 !(reg & CSI_MODE_CSOT), 0,
128 CSI_EN_DIS_TIMEOUT_US);
131 static int rzv2m_csi_start_stop_operation(const struct rzv2m_csi_priv *csi,
132 int enable, bool wait)
136 rzv2m_csi_reg_write_bit(csi, CSI_MODE, CSI_MODE_CSIE, enable);
141 return readl_poll_timeout(csi->base + CSI_MODE, reg,
142 !(reg & CSI_MODE_CSOT), 0,
143 CSI_EN_DIS_TIMEOUT_US);
146 static int rzv2m_csi_fill_txfifo(struct rzv2m_csi_priv *csi)
150 if (readl(csi->base + CSI_OFIFOL))
153 if (csi->bytes_per_word == 2) {
154 const u16 *buf = csi->txbuf;
156 for (i = 0; i < csi->words_to_transfer; i++)
157 writel(buf[i], csi->base + CSI_OFIFO);
159 const u8 *buf = csi->txbuf;
161 for (i = 0; i < csi->words_to_transfer; i++)
162 writel(buf[i], csi->base + CSI_OFIFO);
165 csi->txbuf += csi->bytes_to_transfer;
166 csi->bytes_sent += csi->bytes_to_transfer;
171 static int rzv2m_csi_read_rxfifo(struct rzv2m_csi_priv *csi)
175 if (readl(csi->base + CSI_IFIFOL) != csi->bytes_to_transfer)
178 if (csi->bytes_per_word == 2) {
179 u16 *buf = csi->rxbuf;
181 for (i = 0; i < csi->words_to_transfer; i++)
182 buf[i] = (u16)readl(csi->base + CSI_IFIFO);
184 u8 *buf = csi->rxbuf;
186 for (i = 0; i < csi->words_to_transfer; i++)
187 buf[i] = (u8)readl(csi->base + CSI_IFIFO);
190 csi->rxbuf += csi->bytes_to_transfer;
191 csi->bytes_received += csi->bytes_to_transfer;
196 static inline void rzv2m_csi_calc_current_transfer(struct rzv2m_csi_priv *csi)
198 unsigned int bytes_transferred = max(csi->bytes_received, csi->bytes_sent);
199 unsigned int bytes_remaining = csi->buffer_len - bytes_transferred;
200 unsigned int to_transfer;
204 * Leaving a little bit of headroom in the FIFOs makes it very
205 * hard to raise an overflow error (which is only possible
206 * when IP transmits and receives at the same time).
208 to_transfer = min(CSI_FIFO_HALF_SIZE, bytes_remaining);
210 to_transfer = min(CSI_FIFO_SIZE_BYTES, bytes_remaining);
212 if (csi->bytes_per_word == 2)
216 * We can only choose a trigger level from a predefined set of values.
217 * This will pick a value that is the greatest possible integer that's
218 * less than or equal to the number of bytes we need to transfer.
219 * This may result in multiple smaller transfers.
221 csi->words_to_transfer = rounddown_pow_of_two(to_transfer);
223 if (csi->bytes_per_word == 2)
224 csi->bytes_to_transfer = csi->words_to_transfer << 1;
226 csi->bytes_to_transfer = csi->words_to_transfer;
229 static inline void rzv2m_csi_set_rx_fifo_trigger_level(struct rzv2m_csi_priv *csi)
231 rzv2m_csi_reg_write_bit(csi, CSI_FIFOTRG, CSI_FIFOTRG_R_TRG,
232 ilog2(csi->words_to_transfer));
235 static inline void rzv2m_csi_enable_rx_trigger(struct rzv2m_csi_priv *csi,
238 rzv2m_csi_reg_write_bit(csi, CSI_CNT, CSI_CNT_R_TRGEN, enable);
241 static void rzv2m_csi_disable_irqs(const struct rzv2m_csi_priv *csi,
244 u32 cnt = readl(csi->base + CSI_CNT);
246 writel(cnt & ~enable_bits, csi->base + CSI_CNT);
249 static void rzv2m_csi_disable_all_irqs(struct rzv2m_csi_priv *csi)
251 rzv2m_csi_disable_irqs(csi, CSI_CNT_R_TRGR_E | CSI_CNT_T_TRGR_E |
252 CSI_CNT_CSIEND_E | CSI_CNT_TREND_E |
253 CSI_CNT_OVERF_E | CSI_CNT_UNDER_E);
256 static inline void rzv2m_csi_clear_irqs(struct rzv2m_csi_priv *csi, u32 irqs)
258 writel(irqs, csi->base + CSI_INT);
261 static void rzv2m_csi_clear_all_irqs(struct rzv2m_csi_priv *csi)
263 rzv2m_csi_clear_irqs(csi, CSI_INT_UNDER | CSI_INT_OVERF |
264 CSI_INT_TREND | CSI_INT_CSIEND | CSI_INT_T_TRGR |
268 static void rzv2m_csi_enable_irqs(struct rzv2m_csi_priv *csi, u32 enable_bits)
270 u32 cnt = readl(csi->base + CSI_CNT);
272 writel(cnt | enable_bits, csi->base + CSI_CNT);
275 static int rzv2m_csi_wait_for_interrupt(struct rzv2m_csi_priv *csi,
276 u32 wait_mask, u32 enable_bits)
280 rzv2m_csi_enable_irqs(csi, enable_bits);
282 ret = wait_event_timeout(csi->wait,
283 ((csi->status & wait_mask) == wait_mask) ||
286 rzv2m_csi_disable_irqs(csi, enable_bits);
297 static int rzv2m_csi_wait_for_tx_empty(struct rzv2m_csi_priv *csi)
301 if (readl(csi->base + CSI_OFIFOL) == 0)
304 ret = rzv2m_csi_wait_for_interrupt(csi, CSI_INT_TREND, CSI_CNT_TREND_E);
305 if (ret == -ETIMEDOUT)
306 csi->errors |= TX_TIMEOUT_ERROR;
311 static inline int rzv2m_csi_wait_for_rx_ready(struct rzv2m_csi_priv *csi)
315 if (readl(csi->base + CSI_IFIFOL) == csi->bytes_to_transfer)
318 ret = rzv2m_csi_wait_for_interrupt(csi, CSI_INT_R_TRGR,
320 if (ret == -ETIMEDOUT)
321 csi->errors |= RX_TIMEOUT_ERROR;
326 static irqreturn_t rzv2m_csi_irq_handler(int irq, void *data)
328 struct rzv2m_csi_priv *csi = data;
330 csi->status = readl(csi->base + CSI_INT);
331 rzv2m_csi_disable_irqs(csi, csi->status);
333 if (csi->status & CSI_INT_OVERF)
334 csi->errors |= OVERFLOW_ERROR;
335 if (csi->status & CSI_INT_UNDER)
336 csi->errors |= UNDERRUN_ERROR;
343 static void rzv2m_csi_setup_clock(struct rzv2m_csi_priv *csi, u32 spi_hz)
345 unsigned long csiclk_rate = clk_get_rate(csi->csiclk);
346 unsigned long pclk_rate = clk_get_rate(csi->pclk);
347 unsigned long csiclk_rate_limit = pclk_rate >> 1;
351 * There is a restriction on the frequency of CSICLK, it has to be <=
354 if (csiclk_rate > csiclk_rate_limit) {
355 clk_set_rate(csi->csiclk, csiclk_rate >> 1);
356 csiclk_rate = clk_get_rate(csi->csiclk);
357 } else if ((csiclk_rate << 1) <= csiclk_rate_limit) {
358 clk_set_rate(csi->csiclk, csiclk_rate << 1);
359 csiclk_rate = clk_get_rate(csi->csiclk);
362 spi_hz = spi_hz > CSI_MAX_SPI_SCKO ? CSI_MAX_SPI_SCKO : spi_hz;
364 cks = DIV_ROUND_UP(csiclk_rate, spi_hz << 1);
365 if (cks > CSI_CKS_MAX)
368 dev_dbg(csi->dev, "SPI clk rate is %ldHz\n", csiclk_rate / (cks << 1));
370 rzv2m_csi_reg_write_bit(csi, CSI_CLKSEL, CSI_CLKSEL_CKS, cks);
373 static void rzv2m_csi_setup_operating_mode(struct rzv2m_csi_priv *csi,
374 struct spi_transfer *t)
376 if (t->rx_buf && !t->tx_buf)
377 /* Reception-only mode */
378 rzv2m_csi_reg_write_bit(csi, CSI_MODE, CSI_MODE_TRMD, 0);
380 /* Send and receive mode */
381 rzv2m_csi_reg_write_bit(csi, CSI_MODE, CSI_MODE_TRMD, 1);
383 csi->bytes_per_word = t->bits_per_word / 8;
384 rzv2m_csi_reg_write_bit(csi, CSI_MODE, CSI_MODE_CCL,
385 csi->bytes_per_word == 2);
388 static int rzv2m_csi_setup(struct spi_device *spi)
390 struct rzv2m_csi_priv *csi = spi_controller_get_devdata(spi->controller);
393 rzv2m_csi_sw_reset(csi, 0);
395 writel(CSI_MODE_SETUP, csi->base + CSI_MODE);
397 /* Setup clock polarity and phase timing */
398 rzv2m_csi_reg_write_bit(csi, CSI_CLKSEL, CSI_CLKSEL_MODE,
399 ~spi->mode & SPI_MODE_X_MASK);
401 /* Setup serial data order */
402 rzv2m_csi_reg_write_bit(csi, CSI_MODE, CSI_MODE_DIR,
403 !!(spi->mode & SPI_LSB_FIRST));
405 /* Set the operation mode as master */
406 rzv2m_csi_reg_write_bit(csi, CSI_CLKSEL, CSI_CLKSEL_SLAVE, 0);
408 /* Give the IP a SW reset */
409 ret = rzv2m_csi_sw_reset(csi, 1);
412 rzv2m_csi_sw_reset(csi, 0);
415 * We need to enable the communication so that the clock will settle
416 * for the right polarity before enabling the CS.
418 rzv2m_csi_start_stop_operation(csi, 1, false);
420 rzv2m_csi_start_stop_operation(csi, 0, false);
425 static int rzv2m_csi_pio_transfer(struct rzv2m_csi_priv *csi)
427 bool tx_completed = !csi->txbuf;
428 bool rx_completed = !csi->rxbuf;
431 /* Make sure the TX FIFO is empty */
432 writel(0, csi->base + CSI_OFIFOL);
435 csi->bytes_received = 0;
438 rzv2m_csi_disable_all_irqs(csi);
439 rzv2m_csi_clear_all_irqs(csi);
440 rzv2m_csi_enable_rx_trigger(csi, true);
442 while (!tx_completed || !rx_completed) {
444 * Decide how many words we are going to transfer during
445 * this cycle (for both TX and RX), then set the RX FIFO trigger
446 * level accordingly. No need to set a trigger level for the
447 * TX FIFO, as this IP comes with an interrupt that fires when
448 * the TX FIFO is empty.
450 rzv2m_csi_calc_current_transfer(csi);
451 rzv2m_csi_set_rx_fifo_trigger_level(csi);
453 rzv2m_csi_enable_irqs(csi, CSI_INT_OVERF | CSI_INT_UNDER);
455 /* Make sure the RX FIFO is empty */
456 writel(0, csi->base + CSI_IFIFOL);
458 writel(readl(csi->base + CSI_INT), csi->base + CSI_INT);
461 rzv2m_csi_start_stop_operation(csi, 1, false);
465 ret = rzv2m_csi_fill_txfifo(csi);
469 ret = rzv2m_csi_wait_for_tx_empty(csi);
473 if (csi->bytes_sent == csi->buffer_len)
478 * Make sure the RX FIFO contains the desired number of words.
479 * We then either flush its content, or we copy it onto
482 ret = rzv2m_csi_wait_for_rx_ready(csi);
488 rzv2m_csi_start_stop_operation(csi, 0, false);
490 ret = rzv2m_csi_read_rxfifo(csi);
494 if (csi->bytes_received == csi->buffer_len)
498 ret = rzv2m_csi_start_stop_operation(csi, 0, true);
508 rzv2m_csi_start_stop_operation(csi, 0, true);
511 rzv2m_csi_disable_all_irqs(csi);
512 rzv2m_csi_enable_rx_trigger(csi, false);
513 rzv2m_csi_clear_all_irqs(csi);
518 static int rzv2m_csi_transfer_one(struct spi_controller *controller,
519 struct spi_device *spi,
520 struct spi_transfer *transfer)
522 struct rzv2m_csi_priv *csi = spi_controller_get_devdata(controller);
523 struct device *dev = csi->dev;
526 csi->txbuf = transfer->tx_buf;
527 csi->rxbuf = transfer->rx_buf;
528 csi->buffer_len = transfer->len;
530 rzv2m_csi_setup_operating_mode(csi, transfer);
532 rzv2m_csi_setup_clock(csi, transfer->speed_hz);
534 ret = rzv2m_csi_pio_transfer(csi);
536 if (csi->errors & UNDERRUN_ERROR)
537 dev_err(dev, "Underrun error\n");
538 if (csi->errors & OVERFLOW_ERROR)
539 dev_err(dev, "Overflow error\n");
540 if (csi->errors & TX_TIMEOUT_ERROR)
541 dev_err(dev, "TX timeout error\n");
542 if (csi->errors & RX_TIMEOUT_ERROR)
543 dev_err(dev, "RX timeout error\n");
549 static int rzv2m_csi_probe(struct platform_device *pdev)
551 struct spi_controller *controller;
552 struct device *dev = &pdev->dev;
553 struct rzv2m_csi_priv *csi;
554 struct reset_control *rstc;
558 controller = devm_spi_alloc_host(dev, sizeof(*csi));
562 csi = spi_controller_get_devdata(controller);
563 platform_set_drvdata(pdev, csi);
566 csi->controller = controller;
568 csi->base = devm_platform_ioremap_resource(pdev, 0);
569 if (IS_ERR(csi->base))
570 return PTR_ERR(csi->base);
572 irq = platform_get_irq(pdev, 0);
576 csi->csiclk = devm_clk_get(dev, "csiclk");
577 if (IS_ERR(csi->csiclk))
578 return dev_err_probe(dev, PTR_ERR(csi->csiclk),
579 "could not get csiclk\n");
581 csi->pclk = devm_clk_get(dev, "pclk");
582 if (IS_ERR(csi->pclk))
583 return dev_err_probe(dev, PTR_ERR(csi->pclk),
584 "could not get pclk\n");
586 rstc = devm_reset_control_get_shared(dev, NULL);
588 return dev_err_probe(dev, PTR_ERR(rstc), "Missing reset ctrl\n");
590 init_waitqueue_head(&csi->wait);
592 controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
593 controller->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8);
594 controller->setup = rzv2m_csi_setup;
595 controller->transfer_one = rzv2m_csi_transfer_one;
596 controller->use_gpio_descriptors = true;
598 device_set_node(&controller->dev, dev_fwnode(dev));
600 ret = devm_request_irq(dev, irq, rzv2m_csi_irq_handler, 0,
603 return dev_err_probe(dev, ret, "cannot request IRQ\n");
606 * The reset also affects other HW that is not under the control
607 * of Linux. Therefore, all we can do is make sure the reset is
610 reset_control_deassert(rstc);
612 /* Make sure the IP is in SW reset state */
613 ret = rzv2m_csi_sw_reset(csi, 1);
617 ret = clk_prepare_enable(csi->csiclk);
619 return dev_err_probe(dev, ret, "could not enable csiclk\n");
621 ret = spi_register_controller(controller);
623 clk_disable_unprepare(csi->csiclk);
624 return dev_err_probe(dev, ret, "register controller failed\n");
630 static void rzv2m_csi_remove(struct platform_device *pdev)
632 struct rzv2m_csi_priv *csi = platform_get_drvdata(pdev);
634 spi_unregister_controller(csi->controller);
635 rzv2m_csi_sw_reset(csi, 1);
636 clk_disable_unprepare(csi->csiclk);
639 static const struct of_device_id rzv2m_csi_match[] = {
640 { .compatible = "renesas,rzv2m-csi" },
643 MODULE_DEVICE_TABLE(of, rzv2m_csi_match);
645 static struct platform_driver rzv2m_csi_drv = {
646 .probe = rzv2m_csi_probe,
647 .remove_new = rzv2m_csi_remove,
650 .of_match_table = rzv2m_csi_match,
653 module_platform_driver(rzv2m_csi_drv);
655 MODULE_LICENSE("GPL");
656 MODULE_AUTHOR("Fabrizio Castro <castro.fabrizio.jz@renesas.com>");
657 MODULE_DESCRIPTION("Clocked Serial Interface Driver");