Lines Matching full:rs

204 static inline void spi_enable_chip(struct rockchip_spi *rs, bool enable)  in spi_enable_chip()  argument
206 writel_relaxed((enable ? 1U : 0U), rs->regs + ROCKCHIP_SPI_SSIENR); in spi_enable_chip()
209 static inline void wait_for_tx_idle(struct rockchip_spi *rs, bool slave_mode) in wait_for_tx_idle() argument
215 if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_SLAVE_TX_BUSY) && in wait_for_tx_idle()
216 !((readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY))) in wait_for_tx_idle()
219 if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY)) in wait_for_tx_idle()
224 dev_warn(rs->dev, "spi controller is in busy state!\n"); in wait_for_tx_idle()
227 static u32 get_fifo_len(struct rockchip_spi *rs) in get_fifo_len() argument
231 ver = readl_relaxed(rs->regs + ROCKCHIP_SPI_VERSION); in get_fifo_len()
245 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); in rockchip_spi_set_cs() local
249 if (cs_asserted == rs->cs_asserted[spi->chip_select]) in rockchip_spi_set_cs()
254 pm_runtime_get_sync(rs->dev); in rockchip_spi_set_cs()
257 ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER, 1); in rockchip_spi_set_cs()
259 ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER, BIT(spi->chip_select)); in rockchip_spi_set_cs()
262 ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER, 1); in rockchip_spi_set_cs()
264 ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER, BIT(spi->chip_select)); in rockchip_spi_set_cs()
267 pm_runtime_put(rs->dev); in rockchip_spi_set_cs()
270 rs->cs_asserted[spi->chip_select] = cs_asserted; in rockchip_spi_set_cs()
276 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); in rockchip_spi_handle_err() local
281 spi_enable_chip(rs, false); in rockchip_spi_handle_err()
284 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR); in rockchip_spi_handle_err()
285 writel_relaxed(0xffffffff, rs->regs + ROCKCHIP_SPI_ICR); in rockchip_spi_handle_err()
287 if (atomic_read(&rs->state) & TXDMA) in rockchip_spi_handle_err()
290 if (atomic_read(&rs->state) & RXDMA) in rockchip_spi_handle_err()
294 static void rockchip_spi_pio_writer(struct rockchip_spi *rs) in rockchip_spi_pio_writer() argument
296 u32 tx_free = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR); in rockchip_spi_pio_writer()
297 u32 words = min(rs->tx_left, tx_free); in rockchip_spi_pio_writer()
299 rs->tx_left -= words; in rockchip_spi_pio_writer()
303 if (rs->n_bytes == 1) in rockchip_spi_pio_writer()
304 txw = *(u8 *)rs->tx; in rockchip_spi_pio_writer()
306 txw = *(u16 *)rs->tx; in rockchip_spi_pio_writer()
308 writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR); in rockchip_spi_pio_writer()
309 rs->tx += rs->n_bytes; in rockchip_spi_pio_writer()
313 static void rockchip_spi_pio_reader(struct rockchip_spi *rs) in rockchip_spi_pio_reader() argument
315 u32 words = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR); in rockchip_spi_pio_reader()
316 u32 rx_left = (rs->rx_left > words) ? rs->rx_left - words : 0; in rockchip_spi_pio_reader()
324 u32 ftl = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFTLR) + 1; in rockchip_spi_pio_reader()
328 words = rs->rx_left - rx_left; in rockchip_spi_pio_reader()
332 rs->rx_left = rx_left; in rockchip_spi_pio_reader()
334 u32 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR); in rockchip_spi_pio_reader()
336 if (!rs->rx) in rockchip_spi_pio_reader()
339 if (rs->n_bytes == 1) in rockchip_spi_pio_reader()
340 *(u8 *)rs->rx = (u8)rxw; in rockchip_spi_pio_reader()
342 *(u16 *)rs->rx = (u16)rxw; in rockchip_spi_pio_reader()
343 rs->rx += rs->n_bytes; in rockchip_spi_pio_reader()
350 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); in rockchip_spi_isr() local
353 if (rs->cs_inactive && readl_relaxed(rs->regs + ROCKCHIP_SPI_IMR) & INT_CS_INACTIVE) { in rockchip_spi_isr()
355 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR); in rockchip_spi_isr()
356 writel_relaxed(0xffffffff, rs->regs + ROCKCHIP_SPI_ICR); in rockchip_spi_isr()
361 if (rs->tx_left) in rockchip_spi_isr()
362 rockchip_spi_pio_writer(rs); in rockchip_spi_isr()
364 rockchip_spi_pio_reader(rs); in rockchip_spi_isr()
365 if (!rs->rx_left) { in rockchip_spi_isr()
366 spi_enable_chip(rs, false); in rockchip_spi_isr()
367 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR); in rockchip_spi_isr()
368 writel_relaxed(0xffffffff, rs->regs + ROCKCHIP_SPI_ICR); in rockchip_spi_isr()
375 static int rockchip_spi_prepare_irq(struct rockchip_spi *rs, in rockchip_spi_prepare_irq() argument
379 rs->tx = xfer->tx_buf; in rockchip_spi_prepare_irq()
380 rs->rx = xfer->rx_buf; in rockchip_spi_prepare_irq()
381 rs->tx_left = rs->tx ? xfer->len / rs->n_bytes : 0; in rockchip_spi_prepare_irq()
382 rs->rx_left = xfer->len / rs->n_bytes; in rockchip_spi_prepare_irq()
384 writel_relaxed(0xffffffff, rs->regs + ROCKCHIP_SPI_ICR); in rockchip_spi_prepare_irq()
386 spi_enable_chip(rs, true); in rockchip_spi_prepare_irq()
388 if (rs->tx_left) in rockchip_spi_prepare_irq()
389 rockchip_spi_pio_writer(rs); in rockchip_spi_prepare_irq()
391 if (rs->cs_inactive) in rockchip_spi_prepare_irq()
392 writel_relaxed(INT_RF_FULL | INT_CS_INACTIVE, rs->regs + ROCKCHIP_SPI_IMR); in rockchip_spi_prepare_irq()
394 writel_relaxed(INT_RF_FULL, rs->regs + ROCKCHIP_SPI_IMR); in rockchip_spi_prepare_irq()
403 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); in rockchip_spi_dma_rxcb() local
404 int state = atomic_fetch_andnot(RXDMA, &rs->state); in rockchip_spi_dma_rxcb()
406 if (state & TXDMA && !rs->slave_abort) in rockchip_spi_dma_rxcb()
409 if (rs->cs_inactive) in rockchip_spi_dma_rxcb()
410 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR); in rockchip_spi_dma_rxcb()
412 spi_enable_chip(rs, false); in rockchip_spi_dma_rxcb()
419 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); in rockchip_spi_dma_txcb() local
420 int state = atomic_fetch_andnot(TXDMA, &rs->state); in rockchip_spi_dma_txcb()
422 if (state & RXDMA && !rs->slave_abort) in rockchip_spi_dma_txcb()
426 wait_for_tx_idle(rs, ctlr->slave); in rockchip_spi_dma_txcb()
428 spi_enable_chip(rs, false); in rockchip_spi_dma_txcb()
445 static int rockchip_spi_prepare_dma(struct rockchip_spi *rs, in rockchip_spi_prepare_dma() argument
450 atomic_set(&rs->state, 0); in rockchip_spi_prepare_dma()
452 rs->tx = xfer->tx_buf; in rockchip_spi_prepare_dma()
453 rs->rx = xfer->rx_buf; in rockchip_spi_prepare_dma()
459 .src_addr = rs->dma_addr_rx, in rockchip_spi_prepare_dma()
460 .src_addr_width = rs->n_bytes, in rockchip_spi_prepare_dma()
461 .src_maxburst = rockchip_spi_calc_burst_size(xfer->len / rs->n_bytes), in rockchip_spi_prepare_dma()
481 .dst_addr = rs->dma_addr_tx, in rockchip_spi_prepare_dma()
482 .dst_addr_width = rs->n_bytes, in rockchip_spi_prepare_dma()
483 .dst_maxburst = rs->fifo_len / 4, in rockchip_spi_prepare_dma()
504 atomic_or(RXDMA, &rs->state); in rockchip_spi_prepare_dma()
509 if (rs->cs_inactive) in rockchip_spi_prepare_dma()
510 writel_relaxed(INT_CS_INACTIVE, rs->regs + ROCKCHIP_SPI_IMR); in rockchip_spi_prepare_dma()
512 spi_enable_chip(rs, true); in rockchip_spi_prepare_dma()
515 atomic_or(TXDMA, &rs->state); in rockchip_spi_prepare_dma()
524 static int rockchip_spi_config(struct rockchip_spi *rs, in rockchip_spi_config() argument
537 rs->slave_abort = false; in rockchip_spi_config()
539 cr0 |= rs->rsd << CR0_RSD_OFFSET; in rockchip_spi_config()
571 dev_err(rs->dev, "unknown bits per word: %d\n", in rockchip_spi_config()
583 writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0); in rockchip_spi_config()
584 writel_relaxed(cr1, rs->regs + ROCKCHIP_SPI_CTRLR1); in rockchip_spi_config()
590 if ((xfer->len / rs->n_bytes) < rs->fifo_len) in rockchip_spi_config()
591 writel_relaxed(xfer->len / rs->n_bytes - 1, rs->regs + ROCKCHIP_SPI_RXFTLR); in rockchip_spi_config()
593 writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR); in rockchip_spi_config()
595 writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_DMATDLR); in rockchip_spi_config()
596 writel_relaxed(rockchip_spi_calc_burst_size(xfer->len / rs->n_bytes) - 1, in rockchip_spi_config()
597 rs->regs + ROCKCHIP_SPI_DMARDLR); in rockchip_spi_config()
598 writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR); in rockchip_spi_config()
604 writel_relaxed(2 * DIV_ROUND_UP(rs->freq, 2 * xfer->speed_hz), in rockchip_spi_config()
605 rs->regs + ROCKCHIP_SPI_BAUDR); in rockchip_spi_config()
617 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); in rockchip_spi_slave_abort() local
623 if (atomic_read(&rs->state) & RXDMA) { in rockchip_spi_slave_abort()
627 rs->rx = rs->xfer->rx_buf; in rockchip_spi_slave_abort()
628 rs->xfer->len = 0; in rockchip_spi_slave_abort()
629 rx_fifo_left = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR); in rockchip_spi_slave_abort()
631 readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR); in rockchip_spi_slave_abort()
634 rs->rx += rs->xfer->len - rs->n_bytes * state.residue; in rockchip_spi_slave_abort()
638 /* Get the valid data left in rx fifo and set rs->xfer->len real rx size */ in rockchip_spi_slave_abort()
639 if (rs->rx) { in rockchip_spi_slave_abort()
640 rx_fifo_left = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR); in rockchip_spi_slave_abort()
642 u32 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR); in rockchip_spi_slave_abort()
644 if (rs->n_bytes == 1) in rockchip_spi_slave_abort()
645 *(u8 *)rs->rx = (u8)rxw; in rockchip_spi_slave_abort()
647 *(u16 *)rs->rx = (u16)rxw; in rockchip_spi_slave_abort()
648 rs->rx += rs->n_bytes; in rockchip_spi_slave_abort()
650 rs->xfer->len = (unsigned int)(rs->rx - rs->xfer->rx_buf); in rockchip_spi_slave_abort()
654 if (atomic_read(&rs->state) & RXDMA) in rockchip_spi_slave_abort()
656 if (atomic_read(&rs->state) & TXDMA) in rockchip_spi_slave_abort()
658 atomic_set(&rs->state, 0); in rockchip_spi_slave_abort()
659 spi_enable_chip(rs, false); in rockchip_spi_slave_abort()
660 rs->slave_abort = true; in rockchip_spi_slave_abort()
671 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); in rockchip_spi_transfer_one() local
681 WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) && in rockchip_spi_transfer_one()
682 (readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY)); in rockchip_spi_transfer_one()
685 dev_err(rs->dev, "No buffer for transfer\n"); in rockchip_spi_transfer_one()
690 dev_err(rs->dev, "Transfer is too long (%d)\n", xfer->len); in rockchip_spi_transfer_one()
694 rs->n_bytes = xfer->bits_per_word <= 8 ? 1 : 2; in rockchip_spi_transfer_one()
695 rs->xfer = xfer; in rockchip_spi_transfer_one()
698 ret = rockchip_spi_config(rs, spi, xfer, use_dma, ctlr->slave); in rockchip_spi_transfer_one()
703 return rockchip_spi_prepare_dma(rs, ctlr, xfer); in rockchip_spi_transfer_one()
705 return rockchip_spi_prepare_irq(rs, ctlr, xfer); in rockchip_spi_transfer_one()
712 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); in rockchip_spi_can_dma() local
719 return xfer->len / bytes_per_word >= rs->fifo_len; in rockchip_spi_can_dma()
724 struct rockchip_spi *rs = spi_controller_get_devdata(spi->controller); in rockchip_spi_setup() local
727 if (!spi->cs_gpiod && (spi->mode & SPI_CS_HIGH) && !rs->cs_high_supported) { in rockchip_spi_setup()
732 pm_runtime_get_sync(rs->dev); in rockchip_spi_setup()
734 cr0 = readl_relaxed(rs->regs + ROCKCHIP_SPI_CTRLR0); in rockchip_spi_setup()
743 writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0); in rockchip_spi_setup()
745 pm_runtime_put(rs->dev); in rockchip_spi_setup()
753 struct rockchip_spi *rs; in rockchip_spi_probe() local
774 rs = spi_controller_get_devdata(ctlr); in rockchip_spi_probe()
779 rs->regs = devm_ioremap_resource(&pdev->dev, mem); in rockchip_spi_probe()
780 if (IS_ERR(rs->regs)) { in rockchip_spi_probe()
781 ret = PTR_ERR(rs->regs); in rockchip_spi_probe()
785 rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk"); in rockchip_spi_probe()
786 if (IS_ERR(rs->apb_pclk)) { in rockchip_spi_probe()
788 ret = PTR_ERR(rs->apb_pclk); in rockchip_spi_probe()
792 rs->spiclk = devm_clk_get(&pdev->dev, "spiclk"); in rockchip_spi_probe()
793 if (IS_ERR(rs->spiclk)) { in rockchip_spi_probe()
795 ret = PTR_ERR(rs->spiclk); in rockchip_spi_probe()
799 ret = clk_prepare_enable(rs->apb_pclk); in rockchip_spi_probe()
805 ret = clk_prepare_enable(rs->spiclk); in rockchip_spi_probe()
811 spi_enable_chip(rs, false); in rockchip_spi_probe()
822 rs->dev = &pdev->dev; in rockchip_spi_probe()
823 rs->freq = clk_get_rate(rs->spiclk); in rockchip_spi_probe()
828 u32 rsd = DIV_ROUND_CLOSEST(rsd_nsecs * (rs->freq >> 8), in rockchip_spi_probe()
831 dev_warn(rs->dev, "%u Hz are too slow to express %u ns delay\n", in rockchip_spi_probe()
832 rs->freq, rsd_nsecs); in rockchip_spi_probe()
835 dev_warn(rs->dev, "%u Hz are too fast to express %u ns delay, clamping at %u ns\n", in rockchip_spi_probe()
836 rs->freq, rsd_nsecs, in rockchip_spi_probe()
837 CR0_RSD_MAX * 1000000000U / rs->freq); in rockchip_spi_probe()
839 rs->rsd = rsd; in rockchip_spi_probe()
842 rs->fifo_len = get_fifo_len(rs); in rockchip_spi_probe()
843 if (!rs->fifo_len) { in rockchip_spi_probe()
874 ctlr->min_speed_hz = rs->freq / BAUDR_SCKDV_MAX; in rockchip_spi_probe()
875 ctlr->max_speed_hz = min(rs->freq / BAUDR_SCKDV_MIN, MAX_SCLK_OUT); in rockchip_spi_probe()
883 ctlr->dma_tx = dma_request_chan(rs->dev, "tx"); in rockchip_spi_probe()
890 dev_warn(rs->dev, "Failed to request TX DMA channel\n"); in rockchip_spi_probe()
894 ctlr->dma_rx = dma_request_chan(rs->dev, "rx"); in rockchip_spi_probe()
900 dev_warn(rs->dev, "Failed to request RX DMA channel\n"); in rockchip_spi_probe()
905 rs->dma_addr_tx = mem->start + ROCKCHIP_SPI_TXDR; in rockchip_spi_probe()
906 rs->dma_addr_rx = mem->start + ROCKCHIP_SPI_RXDR; in rockchip_spi_probe()
910 switch (readl_relaxed(rs->regs + ROCKCHIP_SPI_VERSION)) { in rockchip_spi_probe()
912 rs->cs_high_supported = true; in rockchip_spi_probe()
915 rs->cs_inactive = true; in rockchip_spi_probe()
917 rs->cs_inactive = false; in rockchip_spi_probe()
920 rs->cs_inactive = false; in rockchip_spi_probe()
941 clk_disable_unprepare(rs->spiclk); in rockchip_spi_probe()
943 clk_disable_unprepare(rs->apb_pclk); in rockchip_spi_probe()
953 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); in rockchip_spi_remove() local
957 clk_disable_unprepare(rs->spiclk); in rockchip_spi_remove()
958 clk_disable_unprepare(rs->apb_pclk); in rockchip_spi_remove()
979 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); in rockchip_spi_suspend() local
985 clk_disable_unprepare(rs->spiclk); in rockchip_spi_suspend()
986 clk_disable_unprepare(rs->apb_pclk); in rockchip_spi_suspend()
997 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); in rockchip_spi_resume() local
1001 ret = clk_prepare_enable(rs->apb_pclk); in rockchip_spi_resume()
1005 ret = clk_prepare_enable(rs->spiclk); in rockchip_spi_resume()
1007 clk_disable_unprepare(rs->apb_pclk); in rockchip_spi_resume()
1011 clk_disable_unprepare(rs->spiclk); in rockchip_spi_resume()
1012 clk_disable_unprepare(rs->apb_pclk); in rockchip_spi_resume()
1023 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); in rockchip_spi_runtime_suspend() local
1025 clk_disable_unprepare(rs->spiclk); in rockchip_spi_runtime_suspend()
1026 clk_disable_unprepare(rs->apb_pclk); in rockchip_spi_runtime_suspend()
1035 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); in rockchip_spi_runtime_resume() local
1037 ret = clk_prepare_enable(rs->apb_pclk); in rockchip_spi_runtime_resume()
1041 ret = clk_prepare_enable(rs->spiclk); in rockchip_spi_runtime_resume()
1043 clk_disable_unprepare(rs->apb_pclk); in rockchip_spi_runtime_resume()