Lines Matching +full:clk +full:- +full:csr

1 // SPDX-License-Identifier: GPL-2.0-only
9 #include <linux/clk.h>
13 #include <linux/dma-mapping.h>
217 (((value) & ((1 << SPI_##name##_SIZE) - 1)) << SPI_##name##_OFFSET)
219 (((value) >> SPI_##name##_OFFSET) & ((1 << SPI_##name##_SIZE) - 1))
221 (((old) & ~(((1 << SPI_##name##_SIZE) - 1) << SPI_##name##_OFFSET)) \
226 readl_relaxed((port)->regs + SPI_##reg)
228 writel_relaxed((value), (port)->regs + SPI_##reg)
230 writew_relaxed((value), (port)->regs + SPI_##reg)
258 struct clk *clk; member
285 /* Controller-specific per-slave state */
287 u32 csr; member
299 * Atmel-11121F-ATARM-SAMA5D3-Series-Datasheet_02-Feb-16, page 1283)
311 * - CR.LASTXFER
312 * - SPI_MR.DIV32 may become FDIV or must-be-zero (here: always zero)
313 * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs)
314 * - SPI_CSRx.CSAAT
315 * - SPI_CSRx.SBCR allows faster clocking
319 return as->caps.is_spi2; in atmel_spi_is_v2()
331 u32 csr; in atmel_spi_send_dummy() local
338 csr = spi_readl(as, CSR0 + 4 * chip_select); in atmel_spi_send_dummy()
339 csr = SPI_BFINS(SCBR, DUMMY_MSG_FREQUENCY, csr); in atmel_spi_send_dummy()
340 spi_writel(as, CSR0 + 4 * chip_select, csr); in atmel_spi_send_dummy()
354 readl_poll_timeout_atomic(as->regs + SPI_SR, status, in atmel_spi_send_dummy()
367 * it lets us support active-high chipselects despite the controller's
368 * belief that only active-low devices/systems exists.
384 struct atmel_spi_device *asd = spi->controller_state; in cs_activate()
390 chip_select = as->native_cs_for_gpio; in cs_activate()
395 spi_writel(as, CSR0 + 4 * chip_select, asd->csr); in cs_activate()
399 spi_writel(as, CSR0, asd->csr); in cs_activate()
400 if (as->caps.has_wdrbt) { in cs_activate()
421 new_polarity = (asd->csr & SPI_BIT(CPOL)) != 0; in cs_activate()
422 if (new_polarity != as->last_polarity) { in cs_activate()
429 as->last_polarity = new_polarity; in cs_activate()
434 u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0; in cs_activate()
436 u32 csr; in cs_activate() local
439 for (i = 0; i < spi->controller->num_chipselect; i++) { in cs_activate()
440 csr = spi_readl(as, CSR0 + 4 * i); in cs_activate()
441 if ((csr ^ cpol) & SPI_BIT(CPOL)) in cs_activate()
443 csr ^ SPI_BIT(CPOL)); in cs_activate()
451 dev_dbg(&spi->dev, "activate NPCS, mr %08x\n", mr); in cs_activate()
460 chip_select = as->native_cs_for_gpio; in cs_deactivate()
473 dev_dbg(&spi->dev, "DEactivate NPCS, mr %08x\n", mr); in cs_deactivate()
479 static void atmel_spi_lock(struct atmel_spi *as) __acquires(&as->lock) in atmel_spi_lock()
481 spin_lock_irqsave(&as->lock, as->flags); in atmel_spi_lock()
484 static void atmel_spi_unlock(struct atmel_spi *as) __releases(&as->lock) in atmel_spi_unlock()
486 spin_unlock_irqrestore(&as->lock, as->flags); in atmel_spi_unlock()
491 return is_vmalloc_addr(xfer->tx_buf) || is_vmalloc_addr(xfer->rx_buf); in atmel_spi_is_vmalloc_xfer()
497 return as->use_dma && xfer->len >= DMA_MIN_BYTES; in atmel_spi_use_dma()
516 struct spi_controller *host = platform_get_drvdata(as->pdev); in atmel_spi_dma_slave_config()
528 slave_config.dst_addr = (dma_addr_t)as->phybase + SPI_TDR; in atmel_spi_dma_slave_config()
529 slave_config.src_addr = (dma_addr_t)as->phybase + SPI_RDR; in atmel_spi_dma_slave_config()
548 if (dmaengine_slave_config(host->dma_tx, &slave_config)) { in atmel_spi_dma_slave_config()
549 dev_err(&as->pdev->dev, in atmel_spi_dma_slave_config()
551 err = -EINVAL; in atmel_spi_dma_slave_config()
562 if (dmaengine_slave_config(host->dma_rx, &slave_config)) { in atmel_spi_dma_slave_config()
563 dev_err(&as->pdev->dev, in atmel_spi_dma_slave_config()
565 err = -EINVAL; in atmel_spi_dma_slave_config()
574 struct device *dev = &as->pdev->dev; in atmel_spi_configure_dma()
577 host->dma_tx = dma_request_chan(dev, "tx"); in atmel_spi_configure_dma()
578 if (IS_ERR(host->dma_tx)) { in atmel_spi_configure_dma()
579 err = PTR_ERR(host->dma_tx); in atmel_spi_configure_dma()
584 host->dma_rx = dma_request_chan(dev, "rx"); in atmel_spi_configure_dma()
585 if (IS_ERR(host->dma_rx)) { in atmel_spi_configure_dma()
586 err = PTR_ERR(host->dma_rx); in atmel_spi_configure_dma()
599 dev_info(&as->pdev->dev, in atmel_spi_configure_dma()
601 dma_chan_name(host->dma_tx), in atmel_spi_configure_dma()
602 dma_chan_name(host->dma_rx)); in atmel_spi_configure_dma()
606 if (!IS_ERR(host->dma_rx)) in atmel_spi_configure_dma()
607 dma_release_channel(host->dma_rx); in atmel_spi_configure_dma()
608 if (!IS_ERR(host->dma_tx)) in atmel_spi_configure_dma()
609 dma_release_channel(host->dma_tx); in atmel_spi_configure_dma()
611 host->dma_tx = host->dma_rx = NULL; in atmel_spi_configure_dma()
617 if (host->dma_rx) in atmel_spi_stop_dma()
618 dmaengine_terminate_all(host->dma_rx); in atmel_spi_stop_dma()
619 if (host->dma_tx) in atmel_spi_stop_dma()
620 dmaengine_terminate_all(host->dma_tx); in atmel_spi_stop_dma()
625 if (host->dma_rx) { in atmel_spi_release_dma()
626 dma_release_channel(host->dma_rx); in atmel_spi_release_dma()
627 host->dma_rx = NULL; in atmel_spi_release_dma()
629 if (host->dma_tx) { in atmel_spi_release_dma()
630 dma_release_channel(host->dma_tx); in atmel_spi_release_dma()
631 host->dma_tx = NULL; in atmel_spi_release_dma()
641 if (is_vmalloc_addr(as->current_transfer->rx_buf) && in dma_callback()
643 memcpy(as->current_transfer->rx_buf, as->addr_rx_bbuf, in dma_callback()
644 as->current_transfer->len); in dma_callback()
646 complete(&as->xfer_completion); in dma_callback()
656 unsigned long xfer_pos = xfer->len - as->current_remaining_bytes; in atmel_spi_next_xfer_single()
658 dev_vdbg(host->dev.parent, "atmel_spi_next_xfer_pio\n"); in atmel_spi_next_xfer_single()
667 if (xfer->bits_per_word > 8) in atmel_spi_next_xfer_single()
668 spi_writel(as, TDR, *(u16 *)(xfer->tx_buf + xfer_pos)); in atmel_spi_next_xfer_single()
670 spi_writel(as, TDR, *(u8 *)(xfer->tx_buf + xfer_pos)); in atmel_spi_next_xfer_single()
672 dev_dbg(host->dev.parent, in atmel_spi_next_xfer_single()
674 xfer, xfer->len, xfer->tx_buf, xfer->rx_buf, in atmel_spi_next_xfer_single()
675 xfer->bits_per_word); in atmel_spi_next_xfer_single()
689 u32 offset = xfer->len - as->current_remaining_bytes; in atmel_spi_next_xfer_fifo()
690 const u16 *words = (const u16 *)((u8 *)xfer->tx_buf + offset); in atmel_spi_next_xfer_fifo()
691 const u8 *bytes = (const u8 *)((u8 *)xfer->tx_buf + offset); in atmel_spi_next_xfer_fifo()
695 dev_vdbg(host->dev.parent, "atmel_spi_next_xfer_fifo\n"); in atmel_spi_next_xfer_fifo()
698 current_remaining_data = ((xfer->bits_per_word > 8) ? in atmel_spi_next_xfer_fifo()
699 ((u32)as->current_remaining_bytes >> 1) : in atmel_spi_next_xfer_fifo()
700 (u32)as->current_remaining_bytes); in atmel_spi_next_xfer_fifo()
701 num_data = min(current_remaining_data, as->fifo_size); in atmel_spi_next_xfer_fifo()
717 if (xfer->bits_per_word > 8) { in atmel_spi_next_xfer_fifo()
726 num_data -= 2; in atmel_spi_next_xfer_fifo()
730 if (xfer->bits_per_word > 8) in atmel_spi_next_xfer_fifo()
736 num_data--; in atmel_spi_next_xfer_fifo()
739 dev_dbg(host->dev.parent, in atmel_spi_next_xfer_fifo()
741 xfer, xfer->len, xfer->tx_buf, xfer->rx_buf, in atmel_spi_next_xfer_fifo()
742 xfer->bits_per_word); in atmel_spi_next_xfer_fifo()
759 if (as->fifo_size) in atmel_spi_next_xfer_pio()
773 struct dma_chan *rxchan = host->dma_rx; in atmel_spi_next_xfer_dma_submit()
774 struct dma_chan *txchan = host->dma_tx; in atmel_spi_next_xfer_dma_submit()
779 dev_vdbg(host->dev.parent, "atmel_spi_next_xfer_dma_submit\n"); in atmel_spi_next_xfer_dma_submit()
783 return -ENODEV; in atmel_spi_next_xfer_dma_submit()
786 *plen = xfer->len; in atmel_spi_next_xfer_dma_submit()
788 if (atmel_spi_dma_slave_config(as, xfer->bits_per_word)) in atmel_spi_next_xfer_dma_submit()
795 as->dma_addr_rx_bbuf, in atmel_spi_next_xfer_dma_submit()
796 xfer->len, in atmel_spi_next_xfer_dma_submit()
802 xfer->rx_sg.sgl, in atmel_spi_next_xfer_dma_submit()
803 xfer->rx_sg.nents, in atmel_spi_next_xfer_dma_submit()
813 memcpy(as->addr_tx_bbuf, xfer->tx_buf, xfer->len); in atmel_spi_next_xfer_dma_submit()
815 as->dma_addr_tx_bbuf, in atmel_spi_next_xfer_dma_submit()
816 xfer->len, DMA_MEM_TO_DEV, in atmel_spi_next_xfer_dma_submit()
821 xfer->tx_sg.sgl, in atmel_spi_next_xfer_dma_submit()
822 xfer->tx_sg.nents, in atmel_spi_next_xfer_dma_submit()
830 dev_dbg(host->dev.parent, in atmel_spi_next_xfer_dma_submit()
832 xfer, xfer->len, xfer->tx_buf, (unsigned long long)xfer->tx_dma, in atmel_spi_next_xfer_dma_submit()
833 xfer->rx_buf, (unsigned long long)xfer->rx_dma); in atmel_spi_next_xfer_dma_submit()
839 rxdesc->callback = dma_callback; in atmel_spi_next_xfer_dma_submit()
840 rxdesc->callback_param = host; in atmel_spi_next_xfer_dma_submit()
843 cookie = rxdesc->tx_submit(rxdesc); in atmel_spi_next_xfer_dma_submit()
846 cookie = txdesc->tx_submit(txdesc); in atmel_spi_next_xfer_dma_submit()
849 rxchan->device->device_issue_pending(rxchan); in atmel_spi_next_xfer_dma_submit()
850 txchan->device->device_issue_pending(txchan); in atmel_spi_next_xfer_dma_submit()
858 return -ENOMEM; in atmel_spi_next_xfer_dma_submit()
867 *rx_dma = xfer->rx_dma + xfer->len - *plen; in atmel_spi_next_xfer_data()
868 *tx_dma = xfer->tx_dma + xfer->len - *plen; in atmel_spi_next_xfer_data()
869 if (*plen > host->max_dma_len) in atmel_spi_next_xfer_data()
870 *plen = host->max_dma_len; in atmel_spi_next_xfer_data()
877 u32 scbr, csr; in atmel_spi_set_xfer_speed() local
882 chip_select = as->native_cs_for_gpio; in atmel_spi_set_xfer_speed()
887 bus_hz = as->spi_clk; in atmel_spi_set_xfer_speed()
895 scbr = DIV_ROUND_UP(bus_hz, xfer->speed_hz); in atmel_spi_set_xfer_speed()
902 dev_err(&spi->dev, in atmel_spi_set_xfer_speed()
904 xfer->speed_hz, scbr, bus_hz/255); in atmel_spi_set_xfer_speed()
905 return -EINVAL; in atmel_spi_set_xfer_speed()
908 dev_err(&spi->dev, in atmel_spi_set_xfer_speed()
910 xfer->speed_hz, scbr, bus_hz); in atmel_spi_set_xfer_speed()
911 return -EINVAL; in atmel_spi_set_xfer_speed()
913 csr = spi_readl(as, CSR0 + 4 * chip_select); in atmel_spi_set_xfer_speed()
914 csr = SPI_BFINS(SCBR, scbr, csr); in atmel_spi_set_xfer_speed()
915 spi_writel(as, CSR0 + 4 * chip_select, csr); in atmel_spi_set_xfer_speed()
916 xfer->effective_speed_hz = bus_hz / scbr; in atmel_spi_set_xfer_speed()
934 len = as->current_remaining_bytes; in atmel_spi_pdc_next_xfer()
936 as->current_remaining_bytes -= len; in atmel_spi_pdc_next_xfer()
941 if (xfer->bits_per_word > 8) in atmel_spi_pdc_next_xfer()
946 dev_dbg(&host->dev, in atmel_spi_pdc_next_xfer()
948 xfer, xfer->len, xfer->tx_buf, in atmel_spi_pdc_next_xfer()
949 (unsigned long long)xfer->tx_dma, xfer->rx_buf, in atmel_spi_pdc_next_xfer()
950 (unsigned long long)xfer->rx_dma); in atmel_spi_pdc_next_xfer()
952 if (as->current_remaining_bytes) { in atmel_spi_pdc_next_xfer()
953 len = as->current_remaining_bytes; in atmel_spi_pdc_next_xfer()
955 as->current_remaining_bytes -= len; in atmel_spi_pdc_next_xfer()
960 if (xfer->bits_per_word > 8) in atmel_spi_pdc_next_xfer()
965 dev_dbg(&host->dev, in atmel_spi_pdc_next_xfer()
967 xfer, xfer->len, xfer->tx_buf, in atmel_spi_pdc_next_xfer()
968 (unsigned long long)xfer->tx_dma, xfer->rx_buf, in atmel_spi_pdc_next_xfer()
969 (unsigned long long)xfer->rx_dma); in atmel_spi_pdc_next_xfer()
988 * - The buffer is either valid for CPU access, else NULL
989 * - If the buffer is valid, so is its DMA address
994 struct device *dev = &as->pdev->dev; in atmel_spi_dma_map_xfer()
996 xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS; in atmel_spi_dma_map_xfer()
997 if (xfer->tx_buf) { in atmel_spi_dma_map_xfer()
1000 void *nonconst_tx = (void *)xfer->tx_buf; in atmel_spi_dma_map_xfer()
1002 xfer->tx_dma = dma_map_single(dev, in atmel_spi_dma_map_xfer()
1003 nonconst_tx, xfer->len, in atmel_spi_dma_map_xfer()
1005 if (dma_mapping_error(dev, xfer->tx_dma)) in atmel_spi_dma_map_xfer()
1006 return -ENOMEM; in atmel_spi_dma_map_xfer()
1008 if (xfer->rx_buf) { in atmel_spi_dma_map_xfer()
1009 xfer->rx_dma = dma_map_single(dev, in atmel_spi_dma_map_xfer()
1010 xfer->rx_buf, xfer->len, in atmel_spi_dma_map_xfer()
1012 if (dma_mapping_error(dev, xfer->rx_dma)) { in atmel_spi_dma_map_xfer()
1013 if (xfer->tx_buf) in atmel_spi_dma_map_xfer()
1015 xfer->tx_dma, xfer->len, in atmel_spi_dma_map_xfer()
1017 return -ENOMEM; in atmel_spi_dma_map_xfer()
1026 if (xfer->tx_dma != INVALID_DMA_ADDRESS) in atmel_spi_dma_unmap_xfer()
1027 dma_unmap_single(host->dev.parent, xfer->tx_dma, in atmel_spi_dma_unmap_xfer()
1028 xfer->len, DMA_TO_DEVICE); in atmel_spi_dma_unmap_xfer()
1029 if (xfer->rx_dma != INVALID_DMA_ADDRESS) in atmel_spi_dma_unmap_xfer()
1030 dma_unmap_single(host->dev.parent, xfer->rx_dma, in atmel_spi_dma_unmap_xfer()
1031 xfer->len, DMA_FROM_DEVICE); in atmel_spi_dma_unmap_xfer()
1044 unsigned long xfer_pos = xfer->len - as->current_remaining_bytes; in atmel_spi_pump_single_data()
1046 if (xfer->bits_per_word > 8) { in atmel_spi_pump_single_data()
1047 rxp16 = (u16 *)(((u8 *)xfer->rx_buf) + xfer_pos); in atmel_spi_pump_single_data()
1050 rxp = ((u8 *)xfer->rx_buf) + xfer_pos; in atmel_spi_pump_single_data()
1053 if (xfer->bits_per_word > 8) { in atmel_spi_pump_single_data()
1054 if (as->current_remaining_bytes > 2) in atmel_spi_pump_single_data()
1055 as->current_remaining_bytes -= 2; in atmel_spi_pump_single_data()
1057 as->current_remaining_bytes = 0; in atmel_spi_pump_single_data()
1059 as->current_remaining_bytes--; in atmel_spi_pump_single_data()
1068 u32 offset = xfer->len - as->current_remaining_bytes; in atmel_spi_pump_fifo_data()
1069 u16 *words = (u16 *)((u8 *)xfer->rx_buf + offset); in atmel_spi_pump_fifo_data()
1070 u8 *bytes = (u8 *)((u8 *)xfer->rx_buf + offset); in atmel_spi_pump_fifo_data()
1074 num_bytes = ((xfer->bits_per_word > 8) ? in atmel_spi_pump_fifo_data()
1078 if (as->current_remaining_bytes > num_bytes) in atmel_spi_pump_fifo_data()
1079 as->current_remaining_bytes -= num_bytes; in atmel_spi_pump_fifo_data()
1081 as->current_remaining_bytes = 0; in atmel_spi_pump_fifo_data()
1084 if (xfer->bits_per_word > 8) in atmel_spi_pump_fifo_data()
1085 as->current_remaining_bytes &= ~0x1; in atmel_spi_pump_fifo_data()
1090 if (xfer->bits_per_word > 8) in atmel_spi_pump_fifo_data()
1094 num_data--; in atmel_spi_pump_fifo_data()
1106 if (as->fifo_size) in atmel_spi_pump_pio_data()
1131 dev_warn(host->dev.parent, "overrun\n"); in atmel_spi_pio_interrupt()
1136 * bounce buffer and msg->actual_len will not be in atmel_spi_pio_interrupt()
1142 as->done_status = -EIO; in atmel_spi_pio_interrupt()
1148 complete(&as->xfer_completion); in atmel_spi_pio_interrupt()
1153 if (as->current_remaining_bytes) { in atmel_spi_pio_interrupt()
1155 xfer = as->current_transfer; in atmel_spi_pio_interrupt()
1157 if (!as->current_remaining_bytes) in atmel_spi_pio_interrupt()
1160 complete(&as->xfer_completion); in atmel_spi_pio_interrupt()
1195 as->done_status = -EIO; in atmel_spi_pdc_interrupt()
1197 complete(&as->xfer_completion); in atmel_spi_pdc_interrupt()
1204 complete(&as->xfer_completion); in atmel_spi_pdc_interrupt()
1212 struct spi_delay *delay = &spi->word_delay; in atmel_word_delay_csr()
1213 u32 value = delay->value; in atmel_word_delay_csr()
1215 switch (delay->unit) { in atmel_word_delay_csr()
1222 return -EINVAL; in atmel_word_delay_csr()
1225 return (as->spi_clk / 1000000 * value) >> 5; in atmel_word_delay_csr()
1231 struct spi_controller *host = platform_get_drvdata(as->pdev); in initialize_native_cs_for_gpio()
1233 if (!as->native_cs_free) in initialize_native_cs_for_gpio()
1236 if (!host->cs_gpiods) in initialize_native_cs_for_gpio()
1249 if (host->cs_gpiods[i]) in initialize_native_cs_for_gpio()
1250 as->native_cs_free |= BIT(i); in initialize_native_cs_for_gpio()
1252 if (as->native_cs_free) in initialize_native_cs_for_gpio()
1253 as->native_cs_for_gpio = ffs(as->native_cs_free); in initialize_native_cs_for_gpio()
1260 u32 csr; in atmel_spi_setup() local
1261 unsigned int bits = spi->bits_per_word; in atmel_spi_setup()
1265 as = spi_controller_get_devdata(spi->controller); in atmel_spi_setup()
1268 if (!spi_get_csgpiod(spi, 0) && (spi->mode & SPI_CS_HIGH)) { in atmel_spi_setup()
1269 dev_warn(&spi->dev, "setup: non GPIO CS can't be active-high\n"); in atmel_spi_setup()
1270 return -EINVAL; in atmel_spi_setup()
1280 if (spi_get_csgpiod(spi, 0) && as->native_cs_free) { in atmel_spi_setup()
1281 dev_err(&spi->dev, in atmel_spi_setup()
1283 return -EBUSY; in atmel_spi_setup()
1287 chip_select = as->native_cs_for_gpio; in atmel_spi_setup()
1291 csr = SPI_BF(BITS, bits - 8); in atmel_spi_setup()
1292 if (spi->mode & SPI_CPOL) in atmel_spi_setup()
1293 csr |= SPI_BIT(CPOL); in atmel_spi_setup()
1294 if (!(spi->mode & SPI_CPHA)) in atmel_spi_setup()
1295 csr |= SPI_BIT(NCPHA); in atmel_spi_setup()
1298 csr |= SPI_BIT(CSAAT); in atmel_spi_setup()
1299 csr |= SPI_BF(DLYBS, 0); in atmel_spi_setup()
1308 csr |= SPI_BF(DLYBCT, word_delay_csr); in atmel_spi_setup()
1310 asd = spi->controller_state; in atmel_spi_setup()
1314 return -ENOMEM; in atmel_spi_setup()
1316 spi->controller_state = asd; in atmel_spi_setup()
1319 asd->csr = csr; in atmel_spi_setup()
1321 dev_dbg(&spi->dev, in atmel_spi_setup()
1322 "setup: bpw %u mode 0x%x -> csr%d %08x\n", in atmel_spi_setup()
1323 bits, spi->mode, spi_get_chipselect(spi, 0), csr); in atmel_spi_setup()
1326 spi_writel(as, CSR0 + 4 * chip_select, csr); in atmel_spi_setup()
1333 struct atmel_spi *as = spi_controller_get_devdata(spi->controller); in atmel_spi_set_cs()
1338 enable = (!!(spi->mode & SPI_CS_HIGH) == enable); in atmel_spi_set_cs()
1363 asd = spi->controller_state; in atmel_spi_one_transfer()
1364 bits = (asd->csr >> 4) & 0xf; in atmel_spi_one_transfer()
1365 if (bits != xfer->bits_per_word - 8) { in atmel_spi_one_transfer()
1366 dev_dbg(&spi->dev, in atmel_spi_one_transfer()
1368 return -ENOPROTOOPT; in atmel_spi_one_transfer()
1375 if (as->use_pdc) { in atmel_spi_one_transfer()
1377 return -ENOMEM; in atmel_spi_one_transfer()
1382 as->done_status = 0; in atmel_spi_one_transfer()
1383 as->current_transfer = xfer; in atmel_spi_one_transfer()
1384 as->current_remaining_bytes = xfer->len; in atmel_spi_one_transfer()
1385 while (as->current_remaining_bytes) { in atmel_spi_one_transfer()
1386 reinit_completion(&as->xfer_completion); in atmel_spi_one_transfer()
1388 if (as->use_pdc) { in atmel_spi_one_transfer()
1393 len = as->current_remaining_bytes; in atmel_spi_one_transfer()
1397 dev_err(&spi->dev, in atmel_spi_one_transfer()
1399 as->done_status = ret; in atmel_spi_one_transfer()
1402 as->current_remaining_bytes -= len; in atmel_spi_one_transfer()
1403 if (as->current_remaining_bytes < 0) in atmel_spi_one_transfer()
1404 as->current_remaining_bytes = 0; in atmel_spi_one_transfer()
1413 ret_timeout = wait_for_completion_timeout(&as->xfer_completion, dma_timeout); in atmel_spi_one_transfer()
1415 dev_err(&spi->dev, "spi transfer timeout\n"); in atmel_spi_one_transfer()
1416 as->done_status = -EIO; in atmel_spi_one_transfer()
1419 if (as->done_status) in atmel_spi_one_transfer()
1423 if (as->done_status) { in atmel_spi_one_transfer()
1424 if (as->use_pdc) { in atmel_spi_one_transfer()
1425 dev_warn(host->dev.parent, in atmel_spi_one_transfer()
1437 for (timeout = 1000; timeout; timeout--) in atmel_spi_one_transfer()
1441 dev_warn(host->dev.parent, in atmel_spi_one_transfer()
1454 if (as->use_pdc) in atmel_spi_one_transfer()
1457 if (as->use_pdc) in atmel_spi_one_transfer()
1460 return as->done_status; in atmel_spi_one_transfer()
1465 struct atmel_spi_device *asd = spi->controller_state; in atmel_spi_cleanup()
1470 spi->controller_state = NULL; in atmel_spi_cleanup()
1485 as->caps.is_spi2 = version > 0x121; in atmel_get_caps()
1486 as->caps.has_wdrbt = version >= 0x210; in atmel_get_caps()
1487 as->caps.has_dma_support = version >= 0x212; in atmel_get_caps()
1488 as->caps.has_pdc_support = version < 0x212; in atmel_get_caps()
1497 if (as->fifo_size) in atmel_spi_init()
1500 if (as->caps.has_wdrbt) { in atmel_spi_init()
1507 if (as->use_pdc) in atmel_spi_init()
1516 struct clk *clk; in atmel_spi_probe() local
1522 pinctrl_pm_select_default_state(&pdev->dev); in atmel_spi_probe()
1528 clk = devm_clk_get(&pdev->dev, "spi_clk"); in atmel_spi_probe()
1529 if (IS_ERR(clk)) in atmel_spi_probe()
1530 return PTR_ERR(clk); in atmel_spi_probe()
1532 /* setup spi core then atmel-specific driver state */ in atmel_spi_probe()
1533 host = spi_alloc_host(&pdev->dev, sizeof(*as)); in atmel_spi_probe()
1535 return -ENOMEM; in atmel_spi_probe()
1537 /* the spi->mode bits understood by this driver: */ in atmel_spi_probe()
1538 host->use_gpio_descriptors = true; in atmel_spi_probe()
1539 host->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; in atmel_spi_probe()
1540 host->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 16); in atmel_spi_probe()
1541 host->dev.of_node = pdev->dev.of_node; in atmel_spi_probe()
1542 host->bus_num = pdev->id; in atmel_spi_probe()
1543 host->num_chipselect = 4; in atmel_spi_probe()
1544 host->setup = atmel_spi_setup; in atmel_spi_probe()
1545 host->flags = (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX | in atmel_spi_probe()
1547 host->transfer_one = atmel_spi_one_transfer; in atmel_spi_probe()
1548 host->set_cs = atmel_spi_set_cs; in atmel_spi_probe()
1549 host->cleanup = atmel_spi_cleanup; in atmel_spi_probe()
1550 host->auto_runtime_pm = true; in atmel_spi_probe()
1551 host->max_dma_len = SPI_MAX_DMA_XFER; in atmel_spi_probe()
1552 host->can_dma = atmel_spi_can_dma; in atmel_spi_probe()
1557 spin_lock_init(&as->lock); in atmel_spi_probe()
1559 as->pdev = pdev; in atmel_spi_probe()
1560 as->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &regs); in atmel_spi_probe()
1561 if (IS_ERR(as->regs)) { in atmel_spi_probe()
1562 ret = PTR_ERR(as->regs); in atmel_spi_probe()
1565 as->phybase = regs->start; in atmel_spi_probe()
1566 as->irq = irq; in atmel_spi_probe()
1567 as->clk = clk; in atmel_spi_probe()
1569 init_completion(&as->xfer_completion); in atmel_spi_probe()
1573 as->use_dma = false; in atmel_spi_probe()
1574 as->use_pdc = false; in atmel_spi_probe()
1575 if (as->caps.has_dma_support) { in atmel_spi_probe()
1578 as->use_dma = true; in atmel_spi_probe()
1579 } else if (ret == -EPROBE_DEFER) { in atmel_spi_probe()
1582 } else if (as->caps.has_pdc_support) { in atmel_spi_probe()
1583 as->use_pdc = true; in atmel_spi_probe()
1587 as->addr_rx_bbuf = dma_alloc_coherent(&pdev->dev, in atmel_spi_probe()
1589 &as->dma_addr_rx_bbuf, in atmel_spi_probe()
1591 if (!as->addr_rx_bbuf) { in atmel_spi_probe()
1592 as->use_dma = false; in atmel_spi_probe()
1594 as->addr_tx_bbuf = dma_alloc_coherent(&pdev->dev, in atmel_spi_probe()
1596 &as->dma_addr_tx_bbuf, in atmel_spi_probe()
1598 if (!as->addr_tx_bbuf) { in atmel_spi_probe()
1599 as->use_dma = false; in atmel_spi_probe()
1600 dma_free_coherent(&pdev->dev, SPI_MAX_DMA_XFER, in atmel_spi_probe()
1601 as->addr_rx_bbuf, in atmel_spi_probe()
1602 as->dma_addr_rx_bbuf); in atmel_spi_probe()
1605 if (!as->use_dma) in atmel_spi_probe()
1606 dev_info(host->dev.parent, in atmel_spi_probe()
1610 if (as->caps.has_dma_support && !as->use_dma) in atmel_spi_probe()
1611 dev_info(&pdev->dev, "Atmel SPI Controller using PIO only\n"); in atmel_spi_probe()
1613 if (as->use_pdc) { in atmel_spi_probe()
1614 ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pdc_interrupt, in atmel_spi_probe()
1615 0, dev_name(&pdev->dev), host); in atmel_spi_probe()
1617 ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pio_interrupt, in atmel_spi_probe()
1618 0, dev_name(&pdev->dev), host); in atmel_spi_probe()
1624 ret = clk_prepare_enable(clk); in atmel_spi_probe()
1628 as->spi_clk = clk_get_rate(clk); in atmel_spi_probe()
1630 as->fifo_size = 0; in atmel_spi_probe()
1631 if (!of_property_read_u32(pdev->dev.of_node, "atmel,fifo-size", in atmel_spi_probe()
1632 &as->fifo_size)) { in atmel_spi_probe()
1633 dev_info(&pdev->dev, "Using FIFO (%u data)\n", as->fifo_size); in atmel_spi_probe()
1638 pm_runtime_set_autosuspend_delay(&pdev->dev, AUTOSUSPEND_TIMEOUT); in atmel_spi_probe()
1639 pm_runtime_use_autosuspend(&pdev->dev); in atmel_spi_probe()
1640 pm_runtime_set_active(&pdev->dev); in atmel_spi_probe()
1641 pm_runtime_enable(&pdev->dev); in atmel_spi_probe()
1643 ret = devm_spi_register_controller(&pdev->dev, host); in atmel_spi_probe()
1648 dev_info(&pdev->dev, "Atmel SPI Controller version 0x%x at 0x%08lx (irq %d)\n", in atmel_spi_probe()
1649 atmel_get_version(as), (unsigned long)regs->start, in atmel_spi_probe()
1655 pm_runtime_disable(&pdev->dev); in atmel_spi_probe()
1656 pm_runtime_set_suspended(&pdev->dev); in atmel_spi_probe()
1658 if (as->use_dma) in atmel_spi_probe()
1663 clk_disable_unprepare(clk); in atmel_spi_probe()
1675 pm_runtime_get_sync(&pdev->dev); in atmel_spi_remove()
1678 if (as->use_dma) { in atmel_spi_remove()
1682 dma_free_coherent(&pdev->dev, SPI_MAX_DMA_XFER, in atmel_spi_remove()
1683 as->addr_tx_bbuf, in atmel_spi_remove()
1684 as->dma_addr_tx_bbuf); in atmel_spi_remove()
1685 dma_free_coherent(&pdev->dev, SPI_MAX_DMA_XFER, in atmel_spi_remove()
1686 as->addr_rx_bbuf, in atmel_spi_remove()
1687 as->dma_addr_rx_bbuf); in atmel_spi_remove()
1691 spin_lock_irq(&as->lock); in atmel_spi_remove()
1695 spin_unlock_irq(&as->lock); in atmel_spi_remove()
1697 clk_disable_unprepare(as->clk); in atmel_spi_remove()
1699 pm_runtime_put_noidle(&pdev->dev); in atmel_spi_remove()
1700 pm_runtime_disable(&pdev->dev); in atmel_spi_remove()
1708 clk_disable_unprepare(as->clk); in atmel_spi_runtime_suspend()
1721 return clk_prepare_enable(as->clk); in atmel_spi_runtime_resume()
1746 ret = clk_prepare_enable(as->clk); in atmel_spi_resume()
1752 clk_disable_unprepare(as->clk); in atmel_spi_resume()
1771 { .compatible = "atmel,at91rm9200-spi" },