Lines Matching +full:at91rm9200 +full:- +full:spi
1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Atmel AT32 and AT91 SPI Controllers
13 #include <linux/dma-mapping.h>
17 #include <linux/spi/spi.h>
26 #include <trace/events/spi.h>
28 /* SPI register offsets */
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)
247 * The core SPI transfer engine just talks to a register bank to set up
285 /* Controller-specific per-slave state */
299 * Atmel-11121F-ATARM-SAMA5D3-Series-Datasheet_02-Feb-16, page 1283)
305 * This message can be anything as it should not be treated by any SPI device.
310 * Version 2 of the SPI controller has
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()
328 static void atmel_spi_send_dummy(struct atmel_spi *as, struct spi_device *spi, int chip_select) in atmel_spi_send_dummy() argument
334 * Set a clock frequency to allow sending message on SPI bus. in atmel_spi_send_dummy()
343 * Read all data coming from SPI bus, needed to be able to send in atmel_spi_send_dummy()
354 readl_poll_timeout_atomic(as->regs + SPI_SR, status, in atmel_spi_send_dummy()
360 * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby
361 * they assume that spi slave device state will not change on deselect, so
367 * it lets us support active-high chipselects despite the controller's
368 * belief that only active-low devices/systems exists.
370 * However, at91rm9200 has a second erratum whereby nCS0 doesn't work
376 * When changing the clock polarity, the SPI controller waits for the next
382 static void cs_activate(struct atmel_spi *as, struct spi_device *spi) in cs_activate() argument
384 struct atmel_spi_device *asd = spi->controller_state; in cs_activate()
389 if (spi_get_csgpiod(spi, 0)) in cs_activate()
390 chip_select = as->native_cs_for_gpio; in cs_activate()
392 chip_select = spi_get_chipselect(spi, 0); in cs_activate()
395 spi_writel(as, CSR0 + 4 * chip_select, asd->csr); in cs_activate()
396 /* For the low SPI version, there is a issue that PDC transfer in cs_activate()
399 spi_writel(as, CSR0, asd->csr); in cs_activate()
400 if (as->caps.has_wdrbt) { in cs_activate()
418 * processed by the spi devices. in cs_activate()
420 if (spi_get_csgpiod(spi, 0)) { 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()
425 * message because it is already set by the spi core. in cs_activate()
427 gpiod_set_value_cansleep(spi_get_csgpiod(spi, 0), 0); in cs_activate()
428 atmel_spi_send_dummy(as, spi, chip_select); in cs_activate()
429 as->last_polarity = new_polarity; in cs_activate()
430 gpiod_set_value_cansleep(spi_get_csgpiod(spi, 0), 1); in cs_activate()
434 u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0; in cs_activate()
439 for (i = 0; i < spi->controller->num_chipselect; i++) { in cs_activate()
451 dev_dbg(&spi->dev, "activate NPCS, mr %08x\n", mr); in cs_activate()
454 static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi) in cs_deactivate() argument
459 if (spi_get_csgpiod(spi, 0)) in cs_deactivate()
460 chip_select = as->native_cs_for_gpio; in cs_deactivate()
462 chip_select = spi_get_chipselect(spi, 0); in cs_deactivate()
473 dev_dbg(&spi->dev, "DEactivate NPCS, mr %08x\n", mr); in cs_deactivate()
475 if (!spi_get_csgpiod(spi, 0)) 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()
501 struct spi_device *spi, in atmel_spi_can_dma() argument
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()
555 * This driver configures the spi controller for host mode (MSTR bit 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()
874 struct spi_device *spi, in atmel_spi_set_xfer_speed() argument
881 if (spi_get_csgpiod(spi, 0)) in atmel_spi_set_xfer_speed()
882 chip_select = as->native_cs_for_gpio; in atmel_spi_set_xfer_speed()
884 chip_select = spi_get_chipselect(spi, 0); 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()
916 xfer->effective_speed_hz = bus_hz / scbr; in atmel_spi_set_xfer_speed()
923 * lock is held, spi irq is blocked
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
991 * This driver manages the dma address unless message->is_dma_mapped.
996 struct device *dev = &as->pdev->dev; in atmel_spi_dma_map_xfer()
998 xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS; in atmel_spi_dma_map_xfer()
999 if (xfer->tx_buf) { in atmel_spi_dma_map_xfer()
1002 void *nonconst_tx = (void *)xfer->tx_buf; in atmel_spi_dma_map_xfer()
1004 xfer->tx_dma = dma_map_single(dev, in atmel_spi_dma_map_xfer()
1005 nonconst_tx, xfer->len, in atmel_spi_dma_map_xfer()
1007 if (dma_mapping_error(dev, xfer->tx_dma)) in atmel_spi_dma_map_xfer()
1008 return -ENOMEM; in atmel_spi_dma_map_xfer()
1010 if (xfer->rx_buf) { in atmel_spi_dma_map_xfer()
1011 xfer->rx_dma = dma_map_single(dev, in atmel_spi_dma_map_xfer()
1012 xfer->rx_buf, xfer->len, in atmel_spi_dma_map_xfer()
1014 if (dma_mapping_error(dev, xfer->rx_dma)) { in atmel_spi_dma_map_xfer()
1015 if (xfer->tx_buf) in atmel_spi_dma_map_xfer()
1017 xfer->tx_dma, xfer->len, in atmel_spi_dma_map_xfer()
1019 return -ENOMEM; in atmel_spi_dma_map_xfer()
1028 if (xfer->tx_dma != INVALID_DMA_ADDRESS) in atmel_spi_dma_unmap_xfer()
1029 dma_unmap_single(host->dev.parent, xfer->tx_dma, in atmel_spi_dma_unmap_xfer()
1030 xfer->len, DMA_TO_DEVICE); in atmel_spi_dma_unmap_xfer()
1031 if (xfer->rx_dma != INVALID_DMA_ADDRESS) in atmel_spi_dma_unmap_xfer()
1032 dma_unmap_single(host->dev.parent, xfer->rx_dma, in atmel_spi_dma_unmap_xfer()
1033 xfer->len, DMA_FROM_DEVICE); in atmel_spi_dma_unmap_xfer()
1046 unsigned long xfer_pos = xfer->len - as->current_remaining_bytes; in atmel_spi_pump_single_data()
1048 if (xfer->bits_per_word > 8) { in atmel_spi_pump_single_data()
1049 rxp16 = (u16 *)(((u8 *)xfer->rx_buf) + xfer_pos); in atmel_spi_pump_single_data()
1052 rxp = ((u8 *)xfer->rx_buf) + xfer_pos; in atmel_spi_pump_single_data()
1055 if (xfer->bits_per_word > 8) { in atmel_spi_pump_single_data()
1056 if (as->current_remaining_bytes > 2) in atmel_spi_pump_single_data()
1057 as->current_remaining_bytes -= 2; in atmel_spi_pump_single_data()
1059 as->current_remaining_bytes = 0; in atmel_spi_pump_single_data()
1061 as->current_remaining_bytes--; in atmel_spi_pump_single_data()
1070 u32 offset = xfer->len - as->current_remaining_bytes; in atmel_spi_pump_fifo_data()
1071 u16 *words = (u16 *)((u8 *)xfer->rx_buf + offset); in atmel_spi_pump_fifo_data()
1072 u8 *bytes = (u8 *)((u8 *)xfer->rx_buf + offset); in atmel_spi_pump_fifo_data()
1076 num_bytes = ((xfer->bits_per_word > 8) ? in atmel_spi_pump_fifo_data()
1080 if (as->current_remaining_bytes > num_bytes) in atmel_spi_pump_fifo_data()
1081 as->current_remaining_bytes -= num_bytes; in atmel_spi_pump_fifo_data()
1083 as->current_remaining_bytes = 0; in atmel_spi_pump_fifo_data()
1086 if (xfer->bits_per_word > 8) in atmel_spi_pump_fifo_data()
1087 as->current_remaining_bytes &= ~0x1; in atmel_spi_pump_fifo_data()
1092 if (xfer->bits_per_word > 8) in atmel_spi_pump_fifo_data()
1096 num_data--; in atmel_spi_pump_fifo_data()
1108 if (as->fifo_size) in atmel_spi_pump_pio_data()
1133 dev_warn(host->dev.parent, "overrun\n"); in atmel_spi_pio_interrupt()
1138 * bounce buffer and msg->actual_len will not be in atmel_spi_pio_interrupt()
1144 as->done_status = -EIO; in atmel_spi_pio_interrupt()
1150 complete(&as->xfer_completion); in atmel_spi_pio_interrupt()
1155 if (as->current_remaining_bytes) { in atmel_spi_pio_interrupt()
1157 xfer = as->current_transfer; in atmel_spi_pio_interrupt()
1159 if (!as->current_remaining_bytes) in atmel_spi_pio_interrupt()
1162 complete(&as->xfer_completion); in atmel_spi_pio_interrupt()
1197 as->done_status = -EIO; in atmel_spi_pdc_interrupt()
1199 complete(&as->xfer_completion); in atmel_spi_pdc_interrupt()
1206 complete(&as->xfer_completion); in atmel_spi_pdc_interrupt()
1212 static int atmel_word_delay_csr(struct spi_device *spi, struct atmel_spi *as) in atmel_word_delay_csr() argument
1214 struct spi_delay *delay = &spi->word_delay; in atmel_word_delay_csr()
1215 u32 value = delay->value; in atmel_word_delay_csr()
1217 switch (delay->unit) { in atmel_word_delay_csr()
1224 return -EINVAL; in atmel_word_delay_csr()
1227 return (as->spi_clk / 1000000 * value) >> 5; in atmel_word_delay_csr()
1233 struct spi_controller *host = platform_get_drvdata(as->pdev); in initialize_native_cs_for_gpio()
1235 if (!as->native_cs_free) in initialize_native_cs_for_gpio()
1238 if (!host->cs_gpiods) in initialize_native_cs_for_gpio()
1242 * On the first version of the controller (AT91RM9200), CS0 in initialize_native_cs_for_gpio()
1251 if (host->cs_gpiods[i]) in initialize_native_cs_for_gpio()
1252 as->native_cs_free |= BIT(i); in initialize_native_cs_for_gpio()
1254 if (as->native_cs_free) in initialize_native_cs_for_gpio()
1255 as->native_cs_for_gpio = ffs(as->native_cs_free); in initialize_native_cs_for_gpio()
1258 static int atmel_spi_setup(struct spi_device *spi) in atmel_spi_setup() argument
1263 unsigned int bits = spi->bits_per_word; in atmel_spi_setup()
1267 as = spi_controller_get_devdata(spi->controller); in atmel_spi_setup()
1270 if (!spi_get_csgpiod(spi, 0) && (spi->mode & SPI_CS_HIGH)) { in atmel_spi_setup()
1271 dev_warn(&spi->dev, "setup: non GPIO CS can't be active-high\n"); in atmel_spi_setup()
1272 return -EINVAL; in atmel_spi_setup()
1282 if (spi_get_csgpiod(spi, 0) && as->native_cs_free) { in atmel_spi_setup()
1283 dev_err(&spi->dev, in atmel_spi_setup()
1285 return -EBUSY; in atmel_spi_setup()
1288 if (spi_get_csgpiod(spi, 0)) in atmel_spi_setup()
1289 chip_select = as->native_cs_for_gpio; in atmel_spi_setup()
1291 chip_select = spi_get_chipselect(spi, 0); in atmel_spi_setup()
1293 csr = SPI_BF(BITS, bits - 8); in atmel_spi_setup()
1294 if (spi->mode & SPI_CPOL) in atmel_spi_setup()
1296 if (!(spi->mode & SPI_CPHA)) in atmel_spi_setup()
1299 if (!spi_get_csgpiod(spi, 0)) in atmel_spi_setup()
1303 word_delay_csr = atmel_word_delay_csr(spi, as); in atmel_spi_setup()
1312 asd = spi->controller_state; in atmel_spi_setup()
1316 return -ENOMEM; in atmel_spi_setup()
1318 spi->controller_state = asd; in atmel_spi_setup()
1321 asd->csr = csr; in atmel_spi_setup()
1323 dev_dbg(&spi->dev, in atmel_spi_setup()
1324 "setup: bpw %u mode 0x%x -> csr%d %08x\n", in atmel_spi_setup()
1325 bits, spi->mode, spi_get_chipselect(spi, 0), csr); in atmel_spi_setup()
1333 static void atmel_spi_set_cs(struct spi_device *spi, bool enable) in atmel_spi_set_cs() argument
1335 struct atmel_spi *as = spi_controller_get_devdata(spi->controller); in atmel_spi_set_cs()
1340 enable = (!!(spi->mode & SPI_CS_HIGH) == enable); in atmel_spi_set_cs()
1343 cs_activate(as, spi); in atmel_spi_set_cs()
1345 cs_deactivate(as, spi); in atmel_spi_set_cs()
1351 struct spi_device *spi, in atmel_spi_one_transfer() argument
1365 asd = spi->controller_state; in atmel_spi_one_transfer()
1366 bits = (asd->csr >> 4) & 0xf; in atmel_spi_one_transfer()
1367 if (bits != xfer->bits_per_word - 8) { in atmel_spi_one_transfer()
1368 dev_dbg(&spi->dev, in atmel_spi_one_transfer()
1370 return -ENOPROTOOPT; in atmel_spi_one_transfer()
1377 if ((!host->cur_msg->is_dma_mapped) in atmel_spi_one_transfer()
1378 && as->use_pdc) { in atmel_spi_one_transfer()
1380 return -ENOMEM; in atmel_spi_one_transfer()
1383 atmel_spi_set_xfer_speed(as, spi, xfer); in atmel_spi_one_transfer()
1385 as->done_status = 0; in atmel_spi_one_transfer()
1386 as->current_transfer = xfer; in atmel_spi_one_transfer()
1387 as->current_remaining_bytes = xfer->len; in atmel_spi_one_transfer()
1388 while (as->current_remaining_bytes) { in atmel_spi_one_transfer()
1389 reinit_completion(&as->xfer_completion); in atmel_spi_one_transfer()
1391 if (as->use_pdc) { in atmel_spi_one_transfer()
1396 len = as->current_remaining_bytes; in atmel_spi_one_transfer()
1400 dev_err(&spi->dev, in atmel_spi_one_transfer()
1402 as->done_status = ret; in atmel_spi_one_transfer()
1405 as->current_remaining_bytes -= len; in atmel_spi_one_transfer()
1406 if (as->current_remaining_bytes < 0) in atmel_spi_one_transfer()
1407 as->current_remaining_bytes = 0; in atmel_spi_one_transfer()
1416 ret_timeout = wait_for_completion_timeout(&as->xfer_completion, dma_timeout); in atmel_spi_one_transfer()
1418 dev_err(&spi->dev, "spi transfer timeout\n"); in atmel_spi_one_transfer()
1419 as->done_status = -EIO; in atmel_spi_one_transfer()
1422 if (as->done_status) in atmel_spi_one_transfer()
1426 if (as->done_status) { in atmel_spi_one_transfer()
1427 if (as->use_pdc) { in atmel_spi_one_transfer()
1428 dev_warn(host->dev.parent, in atmel_spi_one_transfer()
1440 for (timeout = 1000; timeout; timeout--) in atmel_spi_one_transfer()
1444 dev_warn(host->dev.parent, in atmel_spi_one_transfer()
1457 if (!host->cur_msg->is_dma_mapped in atmel_spi_one_transfer()
1458 && as->use_pdc) in atmel_spi_one_transfer()
1461 if (as->use_pdc) in atmel_spi_one_transfer()
1464 return as->done_status; in atmel_spi_one_transfer()
1467 static void atmel_spi_cleanup(struct spi_device *spi) in atmel_spi_cleanup() argument
1469 struct atmel_spi_device *asd = spi->controller_state; in atmel_spi_cleanup()
1474 spi->controller_state = NULL; in atmel_spi_cleanup()
1489 as->caps.is_spi2 = version > 0x121; in atmel_get_caps()
1490 as->caps.has_wdrbt = version >= 0x210; in atmel_get_caps()
1491 as->caps.has_dma_support = version >= 0x212; in atmel_get_caps()
1492 as->caps.has_pdc_support = version < 0x212; in atmel_get_caps()
1501 if (as->fifo_size) in atmel_spi_init()
1504 if (as->caps.has_wdrbt) { in atmel_spi_init()
1511 if (as->use_pdc) in atmel_spi_init()
1526 pinctrl_pm_select_default_state(&pdev->dev); in atmel_spi_probe()
1532 clk = devm_clk_get(&pdev->dev, "spi_clk"); in atmel_spi_probe()
1536 /* setup spi core then atmel-specific driver state */ in atmel_spi_probe()
1537 host = spi_alloc_host(&pdev->dev, sizeof(*as)); in atmel_spi_probe()
1539 return -ENOMEM; in atmel_spi_probe()
1541 /* the spi->mode bits understood by this driver: */ in atmel_spi_probe()
1542 host->use_gpio_descriptors = true; in atmel_spi_probe()
1543 host->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; in atmel_spi_probe()
1544 host->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 16); in atmel_spi_probe()
1545 host->dev.of_node = pdev->dev.of_node; in atmel_spi_probe()
1546 host->bus_num = pdev->id; in atmel_spi_probe()
1547 host->num_chipselect = 4; in atmel_spi_probe()
1548 host->setup = atmel_spi_setup; in atmel_spi_probe()
1549 host->flags = (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX | in atmel_spi_probe()
1551 host->transfer_one = atmel_spi_one_transfer; in atmel_spi_probe()
1552 host->set_cs = atmel_spi_set_cs; in atmel_spi_probe()
1553 host->cleanup = atmel_spi_cleanup; in atmel_spi_probe()
1554 host->auto_runtime_pm = true; in atmel_spi_probe()
1555 host->max_dma_len = SPI_MAX_DMA_XFER; in atmel_spi_probe()
1556 host->can_dma = atmel_spi_can_dma; in atmel_spi_probe()
1561 spin_lock_init(&as->lock); in atmel_spi_probe()
1563 as->pdev = pdev; in atmel_spi_probe()
1564 as->regs = devm_platform_get_and_ioremap_resource(pdev, 0, ®s); in atmel_spi_probe()
1565 if (IS_ERR(as->regs)) { in atmel_spi_probe()
1566 ret = PTR_ERR(as->regs); in atmel_spi_probe()
1569 as->phybase = regs->start; in atmel_spi_probe()
1570 as->irq = irq; in atmel_spi_probe()
1571 as->clk = clk; in atmel_spi_probe()
1573 init_completion(&as->xfer_completion); in atmel_spi_probe()
1577 as->use_dma = false; in atmel_spi_probe()
1578 as->use_pdc = false; in atmel_spi_probe()
1579 if (as->caps.has_dma_support) { in atmel_spi_probe()
1582 as->use_dma = true; in atmel_spi_probe()
1583 } else if (ret == -EPROBE_DEFER) { in atmel_spi_probe()
1586 } else if (as->caps.has_pdc_support) { in atmel_spi_probe()
1587 as->use_pdc = true; in atmel_spi_probe()
1591 as->addr_rx_bbuf = dma_alloc_coherent(&pdev->dev, in atmel_spi_probe()
1593 &as->dma_addr_rx_bbuf, in atmel_spi_probe()
1595 if (!as->addr_rx_bbuf) { in atmel_spi_probe()
1596 as->use_dma = false; in atmel_spi_probe()
1598 as->addr_tx_bbuf = dma_alloc_coherent(&pdev->dev, in atmel_spi_probe()
1600 &as->dma_addr_tx_bbuf, in atmel_spi_probe()
1602 if (!as->addr_tx_bbuf) { in atmel_spi_probe()
1603 as->use_dma = false; in atmel_spi_probe()
1604 dma_free_coherent(&pdev->dev, SPI_MAX_DMA_XFER, in atmel_spi_probe()
1605 as->addr_rx_bbuf, in atmel_spi_probe()
1606 as->dma_addr_rx_bbuf); in atmel_spi_probe()
1609 if (!as->use_dma) in atmel_spi_probe()
1610 dev_info(host->dev.parent, in atmel_spi_probe()
1614 if (as->caps.has_dma_support && !as->use_dma) in atmel_spi_probe()
1615 dev_info(&pdev->dev, "Atmel SPI Controller using PIO only\n"); in atmel_spi_probe()
1617 if (as->use_pdc) { in atmel_spi_probe()
1618 ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pdc_interrupt, in atmel_spi_probe()
1619 0, dev_name(&pdev->dev), host); in atmel_spi_probe()
1621 ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pio_interrupt, in atmel_spi_probe()
1622 0, dev_name(&pdev->dev), host); in atmel_spi_probe()
1632 as->spi_clk = clk_get_rate(clk); in atmel_spi_probe()
1634 as->fifo_size = 0; in atmel_spi_probe()
1635 if (!of_property_read_u32(pdev->dev.of_node, "atmel,fifo-size", in atmel_spi_probe()
1636 &as->fifo_size)) { in atmel_spi_probe()
1637 dev_info(&pdev->dev, "Using FIFO (%u data)\n", as->fifo_size); in atmel_spi_probe()
1642 pm_runtime_set_autosuspend_delay(&pdev->dev, AUTOSUSPEND_TIMEOUT); in atmel_spi_probe()
1643 pm_runtime_use_autosuspend(&pdev->dev); in atmel_spi_probe()
1644 pm_runtime_set_active(&pdev->dev); in atmel_spi_probe()
1645 pm_runtime_enable(&pdev->dev); in atmel_spi_probe()
1647 ret = devm_spi_register_controller(&pdev->dev, host); in atmel_spi_probe()
1652 dev_info(&pdev->dev, "Atmel SPI Controller version 0x%x at 0x%08lx (irq %d)\n", in atmel_spi_probe()
1653 atmel_get_version(as), (unsigned long)regs->start, in atmel_spi_probe()
1659 pm_runtime_disable(&pdev->dev); in atmel_spi_probe()
1660 pm_runtime_set_suspended(&pdev->dev); in atmel_spi_probe()
1662 if (as->use_dma) in atmel_spi_probe()
1679 pm_runtime_get_sync(&pdev->dev); in atmel_spi_remove()
1682 if (as->use_dma) { in atmel_spi_remove()
1686 dma_free_coherent(&pdev->dev, SPI_MAX_DMA_XFER, in atmel_spi_remove()
1687 as->addr_tx_bbuf, in atmel_spi_remove()
1688 as->dma_addr_tx_bbuf); in atmel_spi_remove()
1689 dma_free_coherent(&pdev->dev, SPI_MAX_DMA_XFER, in atmel_spi_remove()
1690 as->addr_rx_bbuf, in atmel_spi_remove()
1691 as->dma_addr_rx_bbuf); in atmel_spi_remove()
1695 spin_lock_irq(&as->lock); in atmel_spi_remove()
1699 spin_unlock_irq(&as->lock); in atmel_spi_remove()
1701 clk_disable_unprepare(as->clk); in atmel_spi_remove()
1703 pm_runtime_put_noidle(&pdev->dev); in atmel_spi_remove()
1704 pm_runtime_disable(&pdev->dev); in atmel_spi_remove()
1712 clk_disable_unprepare(as->clk); in atmel_spi_runtime_suspend()
1725 return clk_prepare_enable(as->clk); in atmel_spi_runtime_resume()
1750 ret = clk_prepare_enable(as->clk); in atmel_spi_resume()
1756 clk_disable_unprepare(as->clk); in atmel_spi_resume()
1775 { .compatible = "atmel,at91rm9200-spi" },
1792 MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver");