• Home
  • Raw
  • Download

Lines Matching +full:spi +full:- +full:tx +full:- +full:delay +full:- +full:us

2  * Driver for Broadcom BCM2835 SPI Controllers
9 * spi-ath79.c, Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
10 * spi-atmel.c, Copyright (C) 2006 Atmel Corporation
26 #include <linux/delay.h>
27 #include <linux/dma-mapping.h>
39 #include <linux/spi/spi.h>
41 /* SPI register offsets */
81 #define DRV_NAME "spi-bcm2835"
96 return readl(bs->regs + reg); in bcm2835_rd()
101 writel(val, bs->regs + reg); in bcm2835_wr()
108 while ((bs->rx_len) && in bcm2835_rd_fifo()
111 if (bs->rx_buf) in bcm2835_rd_fifo()
112 *bs->rx_buf++ = byte; in bcm2835_rd_fifo()
113 bs->rx_len--; in bcm2835_rd_fifo()
121 while ((bs->tx_len) && in bcm2835_wr_fifo()
123 byte = bs->tx_buf ? *bs->tx_buf++ : 0; in bcm2835_wr_fifo()
125 bs->tx_len--; in bcm2835_wr_fifo()
134 /* Disable SPI interrupts and transfer */ in bcm2835_spi_reset_hw()
139 /* and reset RX/TX FIFOS */ in bcm2835_spi_reset_hw()
158 if (!bs->rx_len) { in bcm2835_spi_interrupt()
159 /* Transfer complete - reset SPI HW */ in bcm2835_spi_interrupt()
162 complete(&master->xfer_completion); in bcm2835_spi_interrupt()
169 struct spi_device *spi, in bcm2835_spi_transfer_one_irq() argument
175 /* fill in fifo if we have gpio-cs in bcm2835_spi_transfer_one_irq()
176 * note that there have been rare events where the native-CS in bcm2835_spi_transfer_one_irq()
177 * flapped for <1us which may change the behaviour in bcm2835_spi_transfer_one_irq()
178 * with gpio-cs this does not happen, so it is implemented in bcm2835_spi_transfer_one_irq()
181 if (gpio_is_valid(spi->cs_gpio)) { in bcm2835_spi_transfer_one_irq()
187 /* fill in tx fifo as much as possible */ in bcm2835_spi_transfer_one_irq()
192 * Enable the HW block. This will immediately trigger a DONE (TX in bcm2835_spi_transfer_one_irq()
193 * empty) interrupt, upon which we will fill the TX FIFO with the in bcm2835_spi_transfer_one_irq()
194 * first TX bytes. Pre-filling the TX FIFO here to avoid the in bcm2835_spi_transfer_one_irq()
195 * interrupt doesn't work:-( in bcm2835_spi_transfer_one_irq()
211 * (so 0 to 65535 bytes) by the SPI HW due to BCM2835_SPI_DLEN
213 * also we currently assume that the scatter-gather fragments are
214 * all multiple of 4 (except the last) - otherwise we would need
216 * this also means that tx/rx transfers sg's need to be of equal size!
218 * there may be a few more border-cases we may need to address as well
219 * but unfortunately this would mean splitting up the scatter-gather
230 /* and terminate tx-dma as we do not have an irq for it in bcm2835_spi_dma_done()
232 * is called the tx-dma must have finished - can't get to this in bcm2835_spi_dma_done()
235 if (cmpxchg(&bs->dma_pending, true, false)) { in bcm2835_spi_dma_done()
236 dmaengine_terminate_all(master->dma_tx); in bcm2835_spi_dma_done()
240 complete(&master->xfer_completion); in bcm2835_spi_dma_done()
258 chan = master->dma_tx; in bcm2835_spi_prepare_sg()
259 nents = tfr->tx_sg.nents; in bcm2835_spi_prepare_sg()
260 sgl = tfr->tx_sg.sgl; in bcm2835_spi_prepare_sg()
261 flags = 0 /* no tx interrupt */; in bcm2835_spi_prepare_sg()
265 chan = master->dma_rx; in bcm2835_spi_prepare_sg()
266 nents = tfr->rx_sg.nents; in bcm2835_spi_prepare_sg()
267 sgl = tfr->rx_sg.sgl; in bcm2835_spi_prepare_sg()
273 return -EINVAL; in bcm2835_spi_prepare_sg()
277 desc->callback = bcm2835_spi_dma_done; in bcm2835_spi_prepare_sg()
278 desc->callback_param = master; in bcm2835_spi_prepare_sg()
281 /* submit it to DMA-engine */ in bcm2835_spi_prepare_sg()
292 /* check that the sg entries are word-sized (except for last) */ in bcm2835_check_sg_length()
293 for_each_sg(sgt->sgl, sgl, (int)sgt->nents - 1, i) { in bcm2835_check_sg_length()
295 return -EFAULT; in bcm2835_check_sg_length()
302 struct spi_device *spi, in bcm2835_spi_transfer_one_dma() argument
310 if (bcm2835_check_sg_length(&tfr->tx_sg) || in bcm2835_spi_transfer_one_dma()
311 bcm2835_check_sg_length(&tfr->rx_sg)) { in bcm2835_spi_transfer_one_dma()
312 dev_warn_once(&spi->dev, in bcm2835_spi_transfer_one_dma()
313 "scatter gather segment length is not a multiple of 4 - falling back to interrupt mode\n"); in bcm2835_spi_transfer_one_dma()
314 return bcm2835_spi_transfer_one_irq(master, spi, tfr, cs); in bcm2835_spi_transfer_one_dma()
317 /* setup tx-DMA */ in bcm2835_spi_transfer_one_dma()
322 /* start TX early */ in bcm2835_spi_transfer_one_dma()
323 dma_async_issue_pending(master->dma_tx); in bcm2835_spi_transfer_one_dma()
326 bs->dma_pending = 1; in bcm2835_spi_transfer_one_dma()
329 bcm2835_wr(bs, BCM2835_SPI_DLEN, tfr->len); in bcm2835_spi_transfer_one_dma()
335 /* setup rx-DMA late - to run transfers while in bcm2835_spi_transfer_one_dma()
337 * this saves 10us or more. in bcm2835_spi_transfer_one_dma()
342 dmaengine_terminate_all(master->dma_tx); in bcm2835_spi_transfer_one_dma()
343 bs->dma_pending = false; in bcm2835_spi_transfer_one_dma()
349 dma_async_issue_pending(master->dma_rx); in bcm2835_spi_transfer_one_dma()
356 struct spi_device *spi, in bcm2835_spi_can_dma() argument
360 if (!gpio_is_valid(spi->cs_gpio)) in bcm2835_spi_can_dma()
364 if (tfr->len < BCM2835_SPI_DMA_MIN_LENGTH) in bcm2835_spi_can_dma()
370 * method - ideally this would get done without any more in bcm2835_spi_can_dma()
373 if (tfr->len > 65535) { in bcm2835_spi_can_dma()
374 dev_warn_once(&spi->dev, in bcm2835_spi_can_dma()
375 "transfer size of %d too big for dma-transfer\n", in bcm2835_spi_can_dma()
376 tfr->len); in bcm2835_spi_can_dma()
381 if ((((size_t)tfr->rx_buf & 3) == 0) && in bcm2835_spi_can_dma()
382 (((size_t)tfr->tx_buf & 3) == 0)) in bcm2835_spi_can_dma()
388 if (((size_t)tfr->tx_buf & (PAGE_SIZE - 1)) + tfr->len > PAGE_SIZE) { in bcm2835_spi_can_dma()
389 dev_warn_once(&spi->dev, in bcm2835_spi_can_dma()
390 "Unaligned spi tx-transfer bridging page\n"); in bcm2835_spi_can_dma()
393 if (((size_t)tfr->rx_buf & (PAGE_SIZE - 1)) + tfr->len > PAGE_SIZE) { in bcm2835_spi_can_dma()
394 dev_warn_once(&spi->dev, in bcm2835_spi_can_dma()
395 "Unaligned spi rx-transfer bridging page\n"); in bcm2835_spi_can_dma()
405 if (master->dma_tx) { in bcm2835_dma_release()
406 dmaengine_terminate_all(master->dma_tx); in bcm2835_dma_release()
407 dma_release_channel(master->dma_tx); in bcm2835_dma_release()
408 master->dma_tx = NULL; in bcm2835_dma_release()
410 if (master->dma_rx) { in bcm2835_dma_release()
411 dmaengine_terminate_all(master->dma_rx); in bcm2835_dma_release()
412 dma_release_channel(master->dma_rx); in bcm2835_dma_release()
413 master->dma_rx = NULL; in bcm2835_dma_release()
424 /* base address in dma-space */ in bcm2835_dma_init()
425 addr = of_get_address(master->dev.of_node, 0, NULL, NULL); in bcm2835_dma_init()
427 dev_err(dev, "could not get DMA-register address - not using dma mode\n"); in bcm2835_dma_init()
432 /* get tx/rx dma */ in bcm2835_dma_init()
433 master->dma_tx = dma_request_slave_channel(dev, "tx"); in bcm2835_dma_init()
434 if (!master->dma_tx) { in bcm2835_dma_init()
435 dev_err(dev, "no tx-dma configuration found - not using dma mode\n"); in bcm2835_dma_init()
438 master->dma_rx = dma_request_slave_channel(dev, "rx"); in bcm2835_dma_init()
439 if (!master->dma_rx) { in bcm2835_dma_init()
440 dev_err(dev, "no rx-dma configuration found - not using dma mode\n"); in bcm2835_dma_init()
449 ret = dmaengine_slave_config(master->dma_tx, &slave_config); in bcm2835_dma_init()
457 ret = dmaengine_slave_config(master->dma_rx, &slave_config); in bcm2835_dma_init()
462 master->can_dma = bcm2835_spi_can_dma; in bcm2835_dma_init()
463 master->max_dma_len = 65535; /* limitation by BCM2835_SPI_DLEN */ in bcm2835_dma_init()
464 /* need to do TX AND RX DMA, so we need dummy buffers */ in bcm2835_dma_init()
465 master->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX; in bcm2835_dma_init()
470 dev_err(dev, "issue configuring dma: %d - not using DMA mode\n", in bcm2835_dma_init()
479 struct spi_device *spi, in bcm2835_spi_transfer_one_poll() argument
500 while (bs->rx_len) { in bcm2835_spi_transfer_one_poll()
501 /* fill in tx fifo with remaining data */ in bcm2835_spi_transfer_one_poll()
510 if (bs->rx_len && time_after(jiffies, timeout)) { in bcm2835_spi_transfer_one_poll()
511 dev_dbg_ratelimited(&spi->dev, in bcm2835_spi_transfer_one_poll()
512 … "timeout period reached: jiffies: %lu remaining tx/rx: %d/%d - falling back to interrupt mode\n", in bcm2835_spi_transfer_one_poll()
513 jiffies - timeout, in bcm2835_spi_transfer_one_poll()
514 bs->tx_len, bs->rx_len); in bcm2835_spi_transfer_one_poll()
516 return bcm2835_spi_transfer_one_irq(master, spi, in bcm2835_spi_transfer_one_poll()
521 /* Transfer complete - reset SPI HW */ in bcm2835_spi_transfer_one_poll()
528 struct spi_device *spi, in bcm2835_spi_transfer_one() argument
538 spi_hz = tfr->speed_hz; in bcm2835_spi_transfer_one()
539 clk_hz = clk_get_rate(bs->clk); in bcm2835_spi_transfer_one()
556 /* handle all the 3-wire mode */ in bcm2835_spi_transfer_one()
557 if (spi->mode & SPI_3WIRE && tfr->rx_buf && in bcm2835_spi_transfer_one()
558 tfr->rx_buf != master->dummy_rx) in bcm2835_spi_transfer_one()
563 /* for gpio_cs set dummy CS so that no HW-CS get changed in bcm2835_spi_transfer_one()
567 if (gpio_is_valid(spi->cs_gpio) || (spi->mode & SPI_NO_CS)) in bcm2835_spi_transfer_one()
571 bs->tx_buf = tfr->tx_buf; in bcm2835_spi_transfer_one()
572 bs->rx_buf = tfr->rx_buf; in bcm2835_spi_transfer_one()
573 bs->tx_len = tfr->len; in bcm2835_spi_transfer_one()
574 bs->rx_len = tfr->len; in bcm2835_spi_transfer_one()
576 /* calculate the estimated time in us the transfer runs */ in bcm2835_spi_transfer_one()
577 xfer_time_us = (unsigned long long)tfr->len in bcm2835_spi_transfer_one()
578 * 9 /* clocks/byte - SPI-HW waits 1 clock after each byte */ in bcm2835_spi_transfer_one()
584 return bcm2835_spi_transfer_one_poll(master, spi, tfr, in bcm2835_spi_transfer_one()
588 if (master->can_dma && bcm2835_spi_can_dma(master, spi, tfr)) in bcm2835_spi_transfer_one()
589 return bcm2835_spi_transfer_one_dma(master, spi, tfr, cs); in bcm2835_spi_transfer_one()
591 /* run in interrupt-mode */ in bcm2835_spi_transfer_one()
592 return bcm2835_spi_transfer_one_irq(master, spi, tfr, cs); in bcm2835_spi_transfer_one()
598 struct spi_device *spi = msg->spi; in bcm2835_spi_prepare_message() local
604 if (spi->mode & SPI_CPOL) in bcm2835_spi_prepare_message()
606 if (spi->mode & SPI_CPHA) in bcm2835_spi_prepare_message()
620 if (cmpxchg(&bs->dma_pending, true, false)) { in bcm2835_spi_handle_err()
621 dmaengine_terminate_all(master->dma_tx); in bcm2835_spi_handle_err()
622 dmaengine_terminate_all(master->dma_rx); in bcm2835_spi_handle_err()
628 static void bcm2835_spi_set_cs(struct spi_device *spi, bool gpio_level) in bcm2835_spi_set_cs() argument
632 * calling us ONLY when cs_gpio is not set in bcm2835_spi_set_cs()
637 * see spi_set_cs in spi.c for the implementation in bcm2835_spi_set_cs()
640 struct spi_master *master = spi->master; in bcm2835_spi_set_cs()
646 enable = (spi->mode & SPI_CS_HIGH) ? gpio_level : !gpio_level; in bcm2835_spi_set_cs()
649 if (spi->mode & SPI_CS_HIGH) { in bcm2835_spi_set_cs()
650 /* set the correct CS-bits */ in bcm2835_spi_set_cs()
652 cs |= BCM2835_SPI_CS_CSPOL0 << spi->chip_select; in bcm2835_spi_set_cs()
654 /* clean the CS-bits */ in bcm2835_spi_set_cs()
656 cs &= ~(BCM2835_SPI_CS_CSPOL0 << spi->chip_select); in bcm2835_spi_set_cs()
662 if (spi->mode & SPI_NO_CS) { in bcm2835_spi_set_cs()
663 /* use the "undefined" chip-select */ in bcm2835_spi_set_cs()
668 cs |= spi->chip_select; in bcm2835_spi_set_cs()
671 /* disable CSPOL which puts HW-CS into deselected state */ in bcm2835_spi_set_cs()
673 /* use the "undefined" chip-select as precaution */ in bcm2835_spi_set_cs()
683 return !strcmp(chip->label, data); in chip_match_name()
686 static int bcm2835_spi_setup(struct spi_device *spi) in bcm2835_spi_setup() argument
691 * sanity checking the native-chipselects in bcm2835_spi_setup()
693 if (spi->mode & SPI_NO_CS) in bcm2835_spi_setup()
695 if (gpio_is_valid(spi->cs_gpio)) in bcm2835_spi_setup()
697 if (spi->chip_select > 1) { in bcm2835_spi_setup()
702 dev_err(&spi->dev, in bcm2835_spi_setup()
703 "setup: only two native chip-selects are supported\n"); in bcm2835_spi_setup()
704 return -EINVAL; in bcm2835_spi_setup()
709 chip = gpiochip_find("pinctrl-bcm2835", chip_match_name); in bcm2835_spi_setup()
714 spi->cs_gpio = chip->base + 8 - spi->chip_select; in bcm2835_spi_setup()
717 dev_info(&spi->dev, "setting up native-CS%i as GPIO %i\n", in bcm2835_spi_setup()
718 spi->chip_select, spi->cs_gpio); in bcm2835_spi_setup()
721 err = gpio_direction_output(spi->cs_gpio, in bcm2835_spi_setup()
722 (spi->mode & SPI_CS_HIGH) ? 0 : 1); in bcm2835_spi_setup()
724 dev_err(&spi->dev, in bcm2835_spi_setup()
726 spi->chip_select, spi->cs_gpio, err); in bcm2835_spi_setup()
740 master = spi_alloc_master(&pdev->dev, sizeof(*bs)); in bcm2835_spi_probe()
742 dev_err(&pdev->dev, "spi_alloc_master() failed\n"); in bcm2835_spi_probe()
743 return -ENOMEM; in bcm2835_spi_probe()
748 master->mode_bits = BCM2835_SPI_MODE_BITS; in bcm2835_spi_probe()
749 master->bits_per_word_mask = SPI_BPW_MASK(8); in bcm2835_spi_probe()
750 master->num_chipselect = 3; in bcm2835_spi_probe()
751 master->setup = bcm2835_spi_setup; in bcm2835_spi_probe()
752 master->set_cs = bcm2835_spi_set_cs; in bcm2835_spi_probe()
753 master->transfer_one = bcm2835_spi_transfer_one; in bcm2835_spi_probe()
754 master->handle_err = bcm2835_spi_handle_err; in bcm2835_spi_probe()
755 master->prepare_message = bcm2835_spi_prepare_message; in bcm2835_spi_probe()
756 master->dev.of_node = pdev->dev.of_node; in bcm2835_spi_probe()
761 bs->regs = devm_ioremap_resource(&pdev->dev, res); in bcm2835_spi_probe()
762 if (IS_ERR(bs->regs)) { in bcm2835_spi_probe()
763 err = PTR_ERR(bs->regs); in bcm2835_spi_probe()
767 bs->clk = devm_clk_get(&pdev->dev, NULL); in bcm2835_spi_probe()
768 if (IS_ERR(bs->clk)) { in bcm2835_spi_probe()
769 err = PTR_ERR(bs->clk); in bcm2835_spi_probe()
770 dev_err(&pdev->dev, "could not get clk: %d\n", err); in bcm2835_spi_probe()
774 bs->irq = platform_get_irq(pdev, 0); in bcm2835_spi_probe()
775 if (bs->irq <= 0) { in bcm2835_spi_probe()
776 dev_err(&pdev->dev, "could not get IRQ: %d\n", bs->irq); in bcm2835_spi_probe()
777 err = bs->irq ? bs->irq : -ENODEV; in bcm2835_spi_probe()
781 clk_prepare_enable(bs->clk); in bcm2835_spi_probe()
783 bcm2835_dma_init(master, &pdev->dev); in bcm2835_spi_probe()
789 err = devm_request_irq(&pdev->dev, bs->irq, bcm2835_spi_interrupt, 0, in bcm2835_spi_probe()
790 dev_name(&pdev->dev), master); in bcm2835_spi_probe()
792 dev_err(&pdev->dev, "could not request IRQ: %d\n", err); in bcm2835_spi_probe()
798 dev_err(&pdev->dev, "could not register SPI master: %d\n", err); in bcm2835_spi_probe()
805 clk_disable_unprepare(bs->clk); in bcm2835_spi_probe()
822 clk_disable_unprepare(bs->clk); in bcm2835_spi_remove()
830 { .compatible = "brcm,bcm2835-spi", },
845 MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835");