1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2f8043872SChris Boot /* 3f8043872SChris Boot * Driver for Broadcom BCM2835 SPI Controllers 4f8043872SChris Boot * 5f8043872SChris Boot * Copyright (C) 2012 Chris Boot 6f8043872SChris Boot * Copyright (C) 2013 Stephen Warren 7e34ff011SMartin Sperl * Copyright (C) 2015 Martin Sperl 8f8043872SChris Boot * 9f8043872SChris Boot * This driver is inspired by: 10f8043872SChris Boot * spi-ath79.c, Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org> 11f8043872SChris Boot * spi-atmel.c, Copyright (C) 2006 Atmel Corporation 12f8043872SChris Boot */ 13f8043872SChris Boot 14f8043872SChris Boot #include <linux/clk.h> 15f8043872SChris Boot #include <linux/completion.h> 16f8043872SChris Boot #include <linux/delay.h> 173ecd37edSMartin Sperl #include <linux/dma-mapping.h> 183ecd37edSMartin Sperl #include <linux/dmaengine.h> 19f8043872SChris Boot #include <linux/err.h> 20f8043872SChris Boot #include <linux/interrupt.h> 21f8043872SChris Boot #include <linux/io.h> 22f8043872SChris Boot #include <linux/kernel.h> 23f8043872SChris Boot #include <linux/module.h> 24f8043872SChris Boot #include <linux/of.h> 253ecd37edSMartin Sperl #include <linux/of_address.h> 26f8043872SChris Boot #include <linux/of_device.h> 273ecd37edSMartin Sperl #include <linux/of_gpio.h> 283ecd37edSMartin Sperl #include <linux/of_irq.h> 29f8043872SChris Boot #include <linux/spi/spi.h> 30f8043872SChris Boot 31f8043872SChris Boot /* SPI register offsets */ 32f8043872SChris Boot #define BCM2835_SPI_CS 0x00 33f8043872SChris Boot #define BCM2835_SPI_FIFO 0x04 34f8043872SChris Boot #define BCM2835_SPI_CLK 0x08 35f8043872SChris Boot #define BCM2835_SPI_DLEN 0x0c 36f8043872SChris Boot #define BCM2835_SPI_LTOH 0x10 37f8043872SChris Boot #define BCM2835_SPI_DC 0x14 38f8043872SChris Boot 39f8043872SChris Boot /* Bitfields in CS */ 40f8043872SChris Boot #define BCM2835_SPI_CS_LEN_LONG 0x02000000 41f8043872SChris Boot #define BCM2835_SPI_CS_DMA_LEN 0x01000000 42f8043872SChris Boot #define BCM2835_SPI_CS_CSPOL2 0x00800000 43f8043872SChris Boot #define BCM2835_SPI_CS_CSPOL1 0x00400000 44f8043872SChris Boot #define BCM2835_SPI_CS_CSPOL0 0x00200000 45f8043872SChris Boot #define BCM2835_SPI_CS_RXF 0x00100000 46f8043872SChris Boot #define BCM2835_SPI_CS_RXR 0x00080000 47f8043872SChris Boot #define BCM2835_SPI_CS_TXD 0x00040000 48f8043872SChris Boot #define BCM2835_SPI_CS_RXD 0x00020000 49f8043872SChris Boot #define BCM2835_SPI_CS_DONE 0x00010000 50f8043872SChris Boot #define BCM2835_SPI_CS_LEN 0x00002000 51f8043872SChris Boot #define BCM2835_SPI_CS_REN 0x00001000 52f8043872SChris Boot #define BCM2835_SPI_CS_ADCS 0x00000800 53f8043872SChris Boot #define BCM2835_SPI_CS_INTR 0x00000400 54f8043872SChris Boot #define BCM2835_SPI_CS_INTD 0x00000200 55f8043872SChris Boot #define BCM2835_SPI_CS_DMAEN 0x00000100 56f8043872SChris Boot #define BCM2835_SPI_CS_TA 0x00000080 57f8043872SChris Boot #define BCM2835_SPI_CS_CSPOL 0x00000040 58f8043872SChris Boot #define BCM2835_SPI_CS_CLEAR_RX 0x00000020 59f8043872SChris Boot #define BCM2835_SPI_CS_CLEAR_TX 0x00000010 60f8043872SChris Boot #define BCM2835_SPI_CS_CPOL 0x00000008 61f8043872SChris Boot #define BCM2835_SPI_CS_CPHA 0x00000004 62f8043872SChris Boot #define BCM2835_SPI_CS_CS_10 0x00000002 63f8043872SChris Boot #define BCM2835_SPI_CS_CS_01 0x00000001 64f8043872SChris Boot 652e0733bcSLukas Wunner #define BCM2835_SPI_FIFO_SIZE 64 662e0733bcSLukas Wunner #define BCM2835_SPI_FIFO_SIZE_3_4 48 67704f32d4SMartin Sperl #define BCM2835_SPI_POLLING_LIMIT_US 30 68a750b124SMartin Sperl #define BCM2835_SPI_POLLING_JIFFIES 2 693ecd37edSMartin Sperl #define BCM2835_SPI_DMA_MIN_LENGTH 96 706935224dSMartin Sperl #define BCM2835_SPI_MODE_BITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \ 716935224dSMartin Sperl | SPI_NO_CS | SPI_3WIRE) 72f8043872SChris Boot 73f8043872SChris Boot #define DRV_NAME "spi-bcm2835" 74f8043872SChris Boot 75acf0f856SLukas Wunner /** 76acf0f856SLukas Wunner * struct bcm2835_spi - BCM2835 SPI controller 77acf0f856SLukas Wunner * @regs: base address of register map 78acf0f856SLukas Wunner * @clk: core clock, divided to calculate serial clock 79acf0f856SLukas Wunner * @irq: interrupt, signals TX FIFO empty or RX FIFO ¾ full 803bd7f658SLukas Wunner * @tfr: SPI transfer currently processed 81acf0f856SLukas Wunner * @tx_buf: pointer whence next transmitted byte is read 82acf0f856SLukas Wunner * @rx_buf: pointer where next received byte is written 83acf0f856SLukas Wunner * @tx_len: remaining bytes to transmit 84acf0f856SLukas Wunner * @rx_len: remaining bytes to receive 853bd7f658SLukas Wunner * @tx_prologue: bytes transmitted without DMA if first TX sglist entry's 863bd7f658SLukas Wunner * length is not a multiple of 4 (to overcome hardware limitation) 873bd7f658SLukas Wunner * @rx_prologue: bytes received without DMA if first RX sglist entry's 883bd7f658SLukas Wunner * length is not a multiple of 4 (to overcome hardware limitation) 893bd7f658SLukas Wunner * @tx_spillover: whether @tx_prologue spills over to second TX sglist entry 90acf0f856SLukas Wunner * @dma_pending: whether a DMA transfer is in progress 91acf0f856SLukas Wunner */ 92f8043872SChris Boot struct bcm2835_spi { 93f8043872SChris Boot void __iomem *regs; 94f8043872SChris Boot struct clk *clk; 95f8043872SChris Boot int irq; 963bd7f658SLukas Wunner struct spi_transfer *tfr; 97f8043872SChris Boot const u8 *tx_buf; 98f8043872SChris Boot u8 *rx_buf; 99e34ff011SMartin Sperl int tx_len; 100e34ff011SMartin Sperl int rx_len; 1013bd7f658SLukas Wunner int tx_prologue; 1023bd7f658SLukas Wunner int rx_prologue; 103b31a9299SLukas Wunner unsigned int tx_spillover; 10429bdedfdSLukas Wunner unsigned int dma_pending; 105f8043872SChris Boot }; 106f8043872SChris Boot 107f8043872SChris Boot static inline u32 bcm2835_rd(struct bcm2835_spi *bs, unsigned reg) 108f8043872SChris Boot { 109f8043872SChris Boot return readl(bs->regs + reg); 110f8043872SChris Boot } 111f8043872SChris Boot 112f8043872SChris Boot static inline void bcm2835_wr(struct bcm2835_spi *bs, unsigned reg, u32 val) 113f8043872SChris Boot { 114f8043872SChris Boot writel(val, bs->regs + reg); 115f8043872SChris Boot } 116f8043872SChris Boot 1174adf3129SMartin Sperl static inline void bcm2835_rd_fifo(struct bcm2835_spi *bs) 118f8043872SChris Boot { 119f8043872SChris Boot u8 byte; 120f8043872SChris Boot 121e34ff011SMartin Sperl while ((bs->rx_len) && 122e34ff011SMartin Sperl (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_RXD)) { 123f8043872SChris Boot byte = bcm2835_rd(bs, BCM2835_SPI_FIFO); 124f8043872SChris Boot if (bs->rx_buf) 125f8043872SChris Boot *bs->rx_buf++ = byte; 126e34ff011SMartin Sperl bs->rx_len--; 127f8043872SChris Boot } 128f8043872SChris Boot } 129f8043872SChris Boot 1304adf3129SMartin Sperl static inline void bcm2835_wr_fifo(struct bcm2835_spi *bs) 131f8043872SChris Boot { 132f8043872SChris Boot u8 byte; 133f8043872SChris Boot 134e34ff011SMartin Sperl while ((bs->tx_len) && 1354adf3129SMartin Sperl (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_TXD)) { 136f8043872SChris Boot byte = bs->tx_buf ? *bs->tx_buf++ : 0; 137f8043872SChris Boot bcm2835_wr(bs, BCM2835_SPI_FIFO, byte); 138e34ff011SMartin Sperl bs->tx_len--; 139f8043872SChris Boot } 140f8043872SChris Boot } 141f8043872SChris Boot 1423bd7f658SLukas Wunner /** 1433bd7f658SLukas Wunner * bcm2835_rd_fifo_count() - blindly read exactly @count bytes from RX FIFO 1443bd7f658SLukas Wunner * @bs: BCM2835 SPI controller 1453bd7f658SLukas Wunner * @count: bytes to read from RX FIFO 1463bd7f658SLukas Wunner * 1473bd7f658SLukas Wunner * The caller must ensure that @bs->rx_len is greater than or equal to @count, 1483bd7f658SLukas Wunner * that the RX FIFO contains at least @count bytes and that the DMA Enable flag 1493bd7f658SLukas Wunner * in the CS register is set (such that a read from the FIFO register receives 150b31a9299SLukas Wunner * 32-bit instead of just 8-bit). Moreover @bs->rx_buf must not be %NULL. 1513bd7f658SLukas Wunner */ 1523bd7f658SLukas Wunner static inline void bcm2835_rd_fifo_count(struct bcm2835_spi *bs, int count) 1533bd7f658SLukas Wunner { 1543bd7f658SLukas Wunner u32 val; 155b31a9299SLukas Wunner int len; 1563bd7f658SLukas Wunner 1573bd7f658SLukas Wunner bs->rx_len -= count; 1583bd7f658SLukas Wunner 1593bd7f658SLukas Wunner while (count > 0) { 1603bd7f658SLukas Wunner val = bcm2835_rd(bs, BCM2835_SPI_FIFO); 161b31a9299SLukas Wunner len = min(count, 4); 1623bd7f658SLukas Wunner memcpy(bs->rx_buf, &val, len); 1633bd7f658SLukas Wunner bs->rx_buf += len; 1643bd7f658SLukas Wunner count -= 4; 1653bd7f658SLukas Wunner } 1663bd7f658SLukas Wunner } 1673bd7f658SLukas Wunner 1683bd7f658SLukas Wunner /** 1693bd7f658SLukas Wunner * bcm2835_wr_fifo_count() - blindly write exactly @count bytes to TX FIFO 1703bd7f658SLukas Wunner * @bs: BCM2835 SPI controller 1713bd7f658SLukas Wunner * @count: bytes to write to TX FIFO 1723bd7f658SLukas Wunner * 1733bd7f658SLukas Wunner * The caller must ensure that @bs->tx_len is greater than or equal to @count, 1743bd7f658SLukas Wunner * that the TX FIFO can accommodate @count bytes and that the DMA Enable flag 1753bd7f658SLukas Wunner * in the CS register is set (such that a write to the FIFO register transmits 1763bd7f658SLukas Wunner * 32-bit instead of just 8-bit). 1773bd7f658SLukas Wunner */ 1783bd7f658SLukas Wunner static inline void bcm2835_wr_fifo_count(struct bcm2835_spi *bs, int count) 1793bd7f658SLukas Wunner { 1803bd7f658SLukas Wunner u32 val; 181b31a9299SLukas Wunner int len; 1823bd7f658SLukas Wunner 1833bd7f658SLukas Wunner bs->tx_len -= count; 1843bd7f658SLukas Wunner 1853bd7f658SLukas Wunner while (count > 0) { 1863bd7f658SLukas Wunner if (bs->tx_buf) { 187b31a9299SLukas Wunner len = min(count, 4); 1883bd7f658SLukas Wunner memcpy(&val, bs->tx_buf, len); 1893bd7f658SLukas Wunner bs->tx_buf += len; 1903bd7f658SLukas Wunner } else { 1913bd7f658SLukas Wunner val = 0; 1923bd7f658SLukas Wunner } 1933bd7f658SLukas Wunner bcm2835_wr(bs, BCM2835_SPI_FIFO, val); 1943bd7f658SLukas Wunner count -= 4; 1953bd7f658SLukas Wunner } 1963bd7f658SLukas Wunner } 1973bd7f658SLukas Wunner 1983bd7f658SLukas Wunner /** 1993bd7f658SLukas Wunner * bcm2835_wait_tx_fifo_empty() - busy-wait for TX FIFO to empty 2003bd7f658SLukas Wunner * @bs: BCM2835 SPI controller 201b31a9299SLukas Wunner * 202b31a9299SLukas Wunner * The caller must ensure that the RX FIFO can accommodate as many bytes 203b31a9299SLukas Wunner * as have been written to the TX FIFO: Transmission is halted once the 204b31a9299SLukas Wunner * RX FIFO is full, causing this function to spin forever. 2053bd7f658SLukas Wunner */ 2063bd7f658SLukas Wunner static inline void bcm2835_wait_tx_fifo_empty(struct bcm2835_spi *bs) 2073bd7f658SLukas Wunner { 2083bd7f658SLukas Wunner while (!(bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_DONE)) 2093bd7f658SLukas Wunner cpu_relax(); 2103bd7f658SLukas Wunner } 2113bd7f658SLukas Wunner 2122e0733bcSLukas Wunner /** 2132e0733bcSLukas Wunner * bcm2835_rd_fifo_blind() - blindly read up to @count bytes from RX FIFO 2142e0733bcSLukas Wunner * @bs: BCM2835 SPI controller 2152e0733bcSLukas Wunner * @count: bytes available for reading in RX FIFO 2162e0733bcSLukas Wunner */ 2172e0733bcSLukas Wunner static inline void bcm2835_rd_fifo_blind(struct bcm2835_spi *bs, int count) 2182e0733bcSLukas Wunner { 2192e0733bcSLukas Wunner u8 val; 2202e0733bcSLukas Wunner 2212e0733bcSLukas Wunner count = min(count, bs->rx_len); 2222e0733bcSLukas Wunner bs->rx_len -= count; 2232e0733bcSLukas Wunner 2242e0733bcSLukas Wunner while (count) { 2252e0733bcSLukas Wunner val = bcm2835_rd(bs, BCM2835_SPI_FIFO); 2262e0733bcSLukas Wunner if (bs->rx_buf) 2272e0733bcSLukas Wunner *bs->rx_buf++ = val; 2282e0733bcSLukas Wunner count--; 2292e0733bcSLukas Wunner } 2302e0733bcSLukas Wunner } 2312e0733bcSLukas Wunner 2322e0733bcSLukas Wunner /** 2332e0733bcSLukas Wunner * bcm2835_wr_fifo_blind() - blindly write up to @count bytes to TX FIFO 2342e0733bcSLukas Wunner * @bs: BCM2835 SPI controller 2352e0733bcSLukas Wunner * @count: bytes available for writing in TX FIFO 2362e0733bcSLukas Wunner */ 2372e0733bcSLukas Wunner static inline void bcm2835_wr_fifo_blind(struct bcm2835_spi *bs, int count) 2382e0733bcSLukas Wunner { 2392e0733bcSLukas Wunner u8 val; 2402e0733bcSLukas Wunner 2412e0733bcSLukas Wunner count = min(count, bs->tx_len); 2422e0733bcSLukas Wunner bs->tx_len -= count; 2432e0733bcSLukas Wunner 2442e0733bcSLukas Wunner while (count) { 2452e0733bcSLukas Wunner val = bs->tx_buf ? *bs->tx_buf++ : 0; 2462e0733bcSLukas Wunner bcm2835_wr(bs, BCM2835_SPI_FIFO, val); 2472e0733bcSLukas Wunner count--; 2482e0733bcSLukas Wunner } 2492e0733bcSLukas Wunner } 2502e0733bcSLukas Wunner 251e34ff011SMartin Sperl static void bcm2835_spi_reset_hw(struct spi_master *master) 252e34ff011SMartin Sperl { 253e34ff011SMartin Sperl struct bcm2835_spi *bs = spi_master_get_devdata(master); 254e34ff011SMartin Sperl u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS); 255e34ff011SMartin Sperl 256e34ff011SMartin Sperl /* Disable SPI interrupts and transfer */ 257e34ff011SMartin Sperl cs &= ~(BCM2835_SPI_CS_INTR | 258e34ff011SMartin Sperl BCM2835_SPI_CS_INTD | 2593ecd37edSMartin Sperl BCM2835_SPI_CS_DMAEN | 260e34ff011SMartin Sperl BCM2835_SPI_CS_TA); 261e34ff011SMartin Sperl /* and reset RX/TX FIFOS */ 262e34ff011SMartin Sperl cs |= BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX; 263e34ff011SMartin Sperl 264e34ff011SMartin Sperl /* and reset the SPI_HW */ 265e34ff011SMartin Sperl bcm2835_wr(bs, BCM2835_SPI_CS, cs); 2663ecd37edSMartin Sperl /* as well as DLEN */ 2673ecd37edSMartin Sperl bcm2835_wr(bs, BCM2835_SPI_DLEN, 0); 268e34ff011SMartin Sperl } 269e34ff011SMartin Sperl 270f8043872SChris Boot static irqreturn_t bcm2835_spi_interrupt(int irq, void *dev_id) 271f8043872SChris Boot { 272f8043872SChris Boot struct spi_master *master = dev_id; 273f8043872SChris Boot struct bcm2835_spi *bs = spi_master_get_devdata(master); 2742e0733bcSLukas Wunner u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS); 2752e0733bcSLukas Wunner 2762e0733bcSLukas Wunner /* 2772e0733bcSLukas Wunner * An interrupt is signaled either if DONE is set (TX FIFO empty) 2782e0733bcSLukas Wunner * or if RXR is set (RX FIFO >= ¾ full). 2792e0733bcSLukas Wunner */ 2802e0733bcSLukas Wunner if (cs & BCM2835_SPI_CS_RXF) 2812e0733bcSLukas Wunner bcm2835_rd_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE); 2822e0733bcSLukas Wunner else if (cs & BCM2835_SPI_CS_RXR) 2832e0733bcSLukas Wunner bcm2835_rd_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE_3_4); 2842e0733bcSLukas Wunner 2852e0733bcSLukas Wunner if (bs->tx_len && cs & BCM2835_SPI_CS_DONE) 2862e0733bcSLukas Wunner bcm2835_wr_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE); 287f8043872SChris Boot 2884adf3129SMartin Sperl /* Read as many bytes as possible from FIFO */ 2894adf3129SMartin Sperl bcm2835_rd_fifo(bs); 290e34ff011SMartin Sperl /* Write as many bytes as possible to FIFO */ 2914adf3129SMartin Sperl bcm2835_wr_fifo(bs); 292f8043872SChris Boot 29356c17234SLukas Wunner if (!bs->rx_len) { 294e34ff011SMartin Sperl /* Transfer complete - reset SPI HW */ 295e34ff011SMartin Sperl bcm2835_spi_reset_hw(master); 296e34ff011SMartin Sperl /* wake up the framework */ 297e34ff011SMartin Sperl complete(&master->xfer_completion); 298f8043872SChris Boot } 299f8043872SChris Boot 300f8043872SChris Boot return IRQ_HANDLED; 301f8043872SChris Boot } 302f8043872SChris Boot 303704f32d4SMartin Sperl static int bcm2835_spi_transfer_one_irq(struct spi_master *master, 304704f32d4SMartin Sperl struct spi_device *spi, 305704f32d4SMartin Sperl struct spi_transfer *tfr, 3062e0733bcSLukas Wunner u32 cs, bool fifo_empty) 307704f32d4SMartin Sperl { 308704f32d4SMartin Sperl struct bcm2835_spi *bs = spi_master_get_devdata(master); 309f8043872SChris Boot 310f8043872SChris Boot /* 3115c09e42fSLukas Wunner * Enable HW block, but with interrupts still disabled. 3125c09e42fSLukas Wunner * Otherwise the empty TX FIFO would immediately trigger an interrupt. 313f8043872SChris Boot */ 3145c09e42fSLukas Wunner bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA); 3155c09e42fSLukas Wunner 3165c09e42fSLukas Wunner /* fill TX FIFO as much as possible */ 3172e0733bcSLukas Wunner if (fifo_empty) 3182e0733bcSLukas Wunner bcm2835_wr_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE); 3195c09e42fSLukas Wunner bcm2835_wr_fifo(bs); 3205c09e42fSLukas Wunner 3215c09e42fSLukas Wunner /* enable interrupts */ 322e34ff011SMartin Sperl cs |= BCM2835_SPI_CS_INTR | BCM2835_SPI_CS_INTD | BCM2835_SPI_CS_TA; 323f8043872SChris Boot bcm2835_wr(bs, BCM2835_SPI_CS, cs); 324f8043872SChris Boot 325e34ff011SMartin Sperl /* signal that we need to wait for completion */ 326e34ff011SMartin Sperl return 1; 327f8043872SChris Boot } 328f8043872SChris Boot 3293bd7f658SLukas Wunner /** 3303bd7f658SLukas Wunner * bcm2835_spi_transfer_prologue() - transfer first few bytes without DMA 3313bd7f658SLukas Wunner * @master: SPI master 3323bd7f658SLukas Wunner * @tfr: SPI transfer 3333bd7f658SLukas Wunner * @bs: BCM2835 SPI controller 3343bd7f658SLukas Wunner * @cs: CS register 3353bd7f658SLukas Wunner * 3363bd7f658SLukas Wunner * A limitation in DMA mode is that the FIFO must be accessed in 4 byte chunks. 3373bd7f658SLukas Wunner * Only the final write access is permitted to transmit less than 4 bytes, the 3383bd7f658SLukas Wunner * SPI controller deduces its intended size from the DLEN register. 3393bd7f658SLukas Wunner * 3403bd7f658SLukas Wunner * If a TX or RX sglist contains multiple entries, one per page, and the first 3413bd7f658SLukas Wunner * entry starts in the middle of a page, that first entry's length may not be 3423bd7f658SLukas Wunner * a multiple of 4. Subsequent entries are fine because they span an entire 3433bd7f658SLukas Wunner * page, hence do have a length that's a multiple of 4. 3443bd7f658SLukas Wunner * 3453bd7f658SLukas Wunner * This cannot happen with kmalloc'ed buffers (which is what most clients use) 3463bd7f658SLukas Wunner * because they are contiguous in physical memory and therefore not split on 3473bd7f658SLukas Wunner * page boundaries by spi_map_buf(). But it *can* happen with vmalloc'ed 3483bd7f658SLukas Wunner * buffers. 3493bd7f658SLukas Wunner * 3503bd7f658SLukas Wunner * The DMA engine is incapable of combining sglist entries into a continuous 3513bd7f658SLukas Wunner * stream of 4 byte chunks, it treats every entry separately: A TX entry is 3523bd7f658SLukas Wunner * rounded up a to a multiple of 4 bytes by transmitting surplus bytes, an RX 3533bd7f658SLukas Wunner * entry is rounded up by throwing away received bytes. 3543bd7f658SLukas Wunner * 3553bd7f658SLukas Wunner * Overcome this limitation by transferring the first few bytes without DMA: 3563bd7f658SLukas Wunner * E.g. if the first TX sglist entry's length is 23 and the first RX's is 42, 3573bd7f658SLukas Wunner * write 3 bytes to the TX FIFO but read only 2 bytes from the RX FIFO. 3583bd7f658SLukas Wunner * The residue of 1 byte in the RX FIFO is picked up by DMA. Together with 3593bd7f658SLukas Wunner * the rest of the first RX sglist entry it makes up a multiple of 4 bytes. 3603bd7f658SLukas Wunner * 3613bd7f658SLukas Wunner * Should the RX prologue be larger, say, 3 vis-à-vis a TX prologue of 1, 3623bd7f658SLukas Wunner * write 1 + 4 = 5 bytes to the TX FIFO and read 3 bytes from the RX FIFO. 3633bd7f658SLukas Wunner * Caution, the additional 4 bytes spill over to the second TX sglist entry 3643bd7f658SLukas Wunner * if the length of the first is *exactly* 1. 3653bd7f658SLukas Wunner * 3663bd7f658SLukas Wunner * At most 6 bytes are written and at most 3 bytes read. Do we know the 3673bd7f658SLukas Wunner * transfer has this many bytes? Yes, see BCM2835_SPI_DMA_MIN_LENGTH. 3683bd7f658SLukas Wunner * 3693bd7f658SLukas Wunner * The FIFO is normally accessed with 8-bit width by the CPU and 32-bit width 3703bd7f658SLukas Wunner * by the DMA engine. Toggling the DMA Enable flag in the CS register switches 3713bd7f658SLukas Wunner * the width but also garbles the FIFO's contents. The prologue must therefore 3723bd7f658SLukas Wunner * be transmitted in 32-bit width to ensure that the following DMA transfer can 3733bd7f658SLukas Wunner * pick up the residue in the RX FIFO in ungarbled form. 3743bd7f658SLukas Wunner */ 3753bd7f658SLukas Wunner static void bcm2835_spi_transfer_prologue(struct spi_master *master, 3763bd7f658SLukas Wunner struct spi_transfer *tfr, 3773bd7f658SLukas Wunner struct bcm2835_spi *bs, 3783bd7f658SLukas Wunner u32 cs) 3793bd7f658SLukas Wunner { 3803bd7f658SLukas Wunner int tx_remaining; 3813bd7f658SLukas Wunner 3823bd7f658SLukas Wunner bs->tfr = tfr; 3833bd7f658SLukas Wunner bs->tx_prologue = 0; 3843bd7f658SLukas Wunner bs->rx_prologue = 0; 3853bd7f658SLukas Wunner bs->tx_spillover = false; 3863bd7f658SLukas Wunner 3873bd7f658SLukas Wunner if (!sg_is_last(&tfr->tx_sg.sgl[0])) 3883bd7f658SLukas Wunner bs->tx_prologue = sg_dma_len(&tfr->tx_sg.sgl[0]) & 3; 3893bd7f658SLukas Wunner 3903bd7f658SLukas Wunner if (!sg_is_last(&tfr->rx_sg.sgl[0])) { 3913bd7f658SLukas Wunner bs->rx_prologue = sg_dma_len(&tfr->rx_sg.sgl[0]) & 3; 3923bd7f658SLukas Wunner 3933bd7f658SLukas Wunner if (bs->rx_prologue > bs->tx_prologue) { 3943bd7f658SLukas Wunner if (sg_is_last(&tfr->tx_sg.sgl[0])) { 3953bd7f658SLukas Wunner bs->tx_prologue = bs->rx_prologue; 3963bd7f658SLukas Wunner } else { 3973bd7f658SLukas Wunner bs->tx_prologue += 4; 3983bd7f658SLukas Wunner bs->tx_spillover = 3993bd7f658SLukas Wunner !(sg_dma_len(&tfr->tx_sg.sgl[0]) & ~3); 4003bd7f658SLukas Wunner } 4013bd7f658SLukas Wunner } 4023bd7f658SLukas Wunner } 4033bd7f658SLukas Wunner 4043bd7f658SLukas Wunner /* rx_prologue > 0 implies tx_prologue > 0, so check only the latter */ 4053bd7f658SLukas Wunner if (!bs->tx_prologue) 4063bd7f658SLukas Wunner return; 4073bd7f658SLukas Wunner 4083bd7f658SLukas Wunner /* Write and read RX prologue. Adjust first entry in RX sglist. */ 4093bd7f658SLukas Wunner if (bs->rx_prologue) { 4103bd7f658SLukas Wunner bcm2835_wr(bs, BCM2835_SPI_DLEN, bs->rx_prologue); 4113bd7f658SLukas Wunner bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA 4123bd7f658SLukas Wunner | BCM2835_SPI_CS_DMAEN); 4133bd7f658SLukas Wunner bcm2835_wr_fifo_count(bs, bs->rx_prologue); 4143bd7f658SLukas Wunner bcm2835_wait_tx_fifo_empty(bs); 4153bd7f658SLukas Wunner bcm2835_rd_fifo_count(bs, bs->rx_prologue); 4163bd7f658SLukas Wunner bcm2835_spi_reset_hw(master); 4173bd7f658SLukas Wunner 418b31a9299SLukas Wunner dma_sync_single_for_device(master->dma_rx->device->dev, 419b31a9299SLukas Wunner sg_dma_address(&tfr->rx_sg.sgl[0]), 420b31a9299SLukas Wunner bs->rx_prologue, DMA_FROM_DEVICE); 4213bd7f658SLukas Wunner 422b31a9299SLukas Wunner sg_dma_address(&tfr->rx_sg.sgl[0]) += bs->rx_prologue; 423b31a9299SLukas Wunner sg_dma_len(&tfr->rx_sg.sgl[0]) -= bs->rx_prologue; 4243bd7f658SLukas Wunner } 4253bd7f658SLukas Wunner 4263bd7f658SLukas Wunner /* 4273bd7f658SLukas Wunner * Write remaining TX prologue. Adjust first entry in TX sglist. 4283bd7f658SLukas Wunner * Also adjust second entry if prologue spills over to it. 4293bd7f658SLukas Wunner */ 4303bd7f658SLukas Wunner tx_remaining = bs->tx_prologue - bs->rx_prologue; 4313bd7f658SLukas Wunner if (tx_remaining) { 4323bd7f658SLukas Wunner bcm2835_wr(bs, BCM2835_SPI_DLEN, tx_remaining); 4333bd7f658SLukas Wunner bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA 4343bd7f658SLukas Wunner | BCM2835_SPI_CS_DMAEN); 4353bd7f658SLukas Wunner bcm2835_wr_fifo_count(bs, tx_remaining); 4363bd7f658SLukas Wunner bcm2835_wait_tx_fifo_empty(bs); 4373bd7f658SLukas Wunner bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_CLEAR_TX); 4383bd7f658SLukas Wunner } 4393bd7f658SLukas Wunner 4403bd7f658SLukas Wunner if (likely(!bs->tx_spillover)) { 441b31a9299SLukas Wunner sg_dma_address(&tfr->tx_sg.sgl[0]) += bs->tx_prologue; 442b31a9299SLukas Wunner sg_dma_len(&tfr->tx_sg.sgl[0]) -= bs->tx_prologue; 4433bd7f658SLukas Wunner } else { 444b31a9299SLukas Wunner sg_dma_len(&tfr->tx_sg.sgl[0]) = 0; 445b31a9299SLukas Wunner sg_dma_address(&tfr->tx_sg.sgl[1]) += 4; 446b31a9299SLukas Wunner sg_dma_len(&tfr->tx_sg.sgl[1]) -= 4; 4473bd7f658SLukas Wunner } 4483bd7f658SLukas Wunner } 4493bd7f658SLukas Wunner 4503bd7f658SLukas Wunner /** 4513bd7f658SLukas Wunner * bcm2835_spi_undo_prologue() - reconstruct original sglist state 4523bd7f658SLukas Wunner * @bs: BCM2835 SPI controller 4533bd7f658SLukas Wunner * 4543bd7f658SLukas Wunner * Undo changes which were made to an SPI transfer's sglist when transmitting 4553bd7f658SLukas Wunner * the prologue. This is necessary to ensure the same memory ranges are 4563bd7f658SLukas Wunner * unmapped that were originally mapped. 4573bd7f658SLukas Wunner */ 4583bd7f658SLukas Wunner static void bcm2835_spi_undo_prologue(struct bcm2835_spi *bs) 4593bd7f658SLukas Wunner { 4603bd7f658SLukas Wunner struct spi_transfer *tfr = bs->tfr; 4613bd7f658SLukas Wunner 4623bd7f658SLukas Wunner if (!bs->tx_prologue) 4633bd7f658SLukas Wunner return; 4643bd7f658SLukas Wunner 4653bd7f658SLukas Wunner if (bs->rx_prologue) { 466b31a9299SLukas Wunner sg_dma_address(&tfr->rx_sg.sgl[0]) -= bs->rx_prologue; 467b31a9299SLukas Wunner sg_dma_len(&tfr->rx_sg.sgl[0]) += bs->rx_prologue; 4683bd7f658SLukas Wunner } 4693bd7f658SLukas Wunner 4703bd7f658SLukas Wunner if (likely(!bs->tx_spillover)) { 471b31a9299SLukas Wunner sg_dma_address(&tfr->tx_sg.sgl[0]) -= bs->tx_prologue; 472b31a9299SLukas Wunner sg_dma_len(&tfr->tx_sg.sgl[0]) += bs->tx_prologue; 4733bd7f658SLukas Wunner } else { 474b31a9299SLukas Wunner sg_dma_len(&tfr->tx_sg.sgl[0]) = bs->tx_prologue - 4; 475b31a9299SLukas Wunner sg_dma_address(&tfr->tx_sg.sgl[1]) -= 4; 476b31a9299SLukas Wunner sg_dma_len(&tfr->tx_sg.sgl[1]) += 4; 4773bd7f658SLukas Wunner } 4783bd7f658SLukas Wunner } 4793bd7f658SLukas Wunner 4803ecd37edSMartin Sperl static void bcm2835_spi_dma_done(void *data) 4813ecd37edSMartin Sperl { 4823ecd37edSMartin Sperl struct spi_master *master = data; 4833ecd37edSMartin Sperl struct bcm2835_spi *bs = spi_master_get_devdata(master); 4843ecd37edSMartin Sperl 4853ecd37edSMartin Sperl /* reset fifo and HW */ 4863ecd37edSMartin Sperl bcm2835_spi_reset_hw(master); 4873ecd37edSMartin Sperl 4883ecd37edSMartin Sperl /* and terminate tx-dma as we do not have an irq for it 4893ecd37edSMartin Sperl * because when the rx dma will terminate and this callback 4903ecd37edSMartin Sperl * is called the tx-dma must have finished - can't get to this 4913ecd37edSMartin Sperl * situation otherwise... 4923ecd37edSMartin Sperl */ 493e82b0b38SLukas Wunner if (cmpxchg(&bs->dma_pending, true, false)) { 4942527704dSLukas Wunner dmaengine_terminate_async(master->dma_tx); 4953bd7f658SLukas Wunner bcm2835_spi_undo_prologue(bs); 496e82b0b38SLukas Wunner } 4973ecd37edSMartin Sperl 4983ecd37edSMartin Sperl /* and mark as completed */; 4993ecd37edSMartin Sperl complete(&master->xfer_completion); 5003ecd37edSMartin Sperl } 5013ecd37edSMartin Sperl 5023ecd37edSMartin Sperl static int bcm2835_spi_prepare_sg(struct spi_master *master, 5033ecd37edSMartin Sperl struct spi_transfer *tfr, 5043ecd37edSMartin Sperl bool is_tx) 5053ecd37edSMartin Sperl { 5063ecd37edSMartin Sperl struct dma_chan *chan; 5073ecd37edSMartin Sperl struct scatterlist *sgl; 5083ecd37edSMartin Sperl unsigned int nents; 5093ecd37edSMartin Sperl enum dma_transfer_direction dir; 5103ecd37edSMartin Sperl unsigned long flags; 5113ecd37edSMartin Sperl 5123ecd37edSMartin Sperl struct dma_async_tx_descriptor *desc; 5133ecd37edSMartin Sperl dma_cookie_t cookie; 5143ecd37edSMartin Sperl 5153ecd37edSMartin Sperl if (is_tx) { 5163ecd37edSMartin Sperl dir = DMA_MEM_TO_DEV; 5173ecd37edSMartin Sperl chan = master->dma_tx; 5183ecd37edSMartin Sperl nents = tfr->tx_sg.nents; 5193ecd37edSMartin Sperl sgl = tfr->tx_sg.sgl; 5203ecd37edSMartin Sperl flags = 0 /* no tx interrupt */; 5213ecd37edSMartin Sperl 5223ecd37edSMartin Sperl } else { 5233ecd37edSMartin Sperl dir = DMA_DEV_TO_MEM; 5243ecd37edSMartin Sperl chan = master->dma_rx; 5253ecd37edSMartin Sperl nents = tfr->rx_sg.nents; 5263ecd37edSMartin Sperl sgl = tfr->rx_sg.sgl; 5273ecd37edSMartin Sperl flags = DMA_PREP_INTERRUPT; 5283ecd37edSMartin Sperl } 5293ecd37edSMartin Sperl /* prepare the channel */ 5303ecd37edSMartin Sperl desc = dmaengine_prep_slave_sg(chan, sgl, nents, dir, flags); 5313ecd37edSMartin Sperl if (!desc) 5323ecd37edSMartin Sperl return -EINVAL; 5333ecd37edSMartin Sperl 5343ecd37edSMartin Sperl /* set callback for rx */ 5353ecd37edSMartin Sperl if (!is_tx) { 5363ecd37edSMartin Sperl desc->callback = bcm2835_spi_dma_done; 5373ecd37edSMartin Sperl desc->callback_param = master; 5383ecd37edSMartin Sperl } 5393ecd37edSMartin Sperl 5403ecd37edSMartin Sperl /* submit it to DMA-engine */ 5413ecd37edSMartin Sperl cookie = dmaengine_submit(desc); 5423ecd37edSMartin Sperl 5433ecd37edSMartin Sperl return dma_submit_error(cookie); 5443ecd37edSMartin Sperl } 5453ecd37edSMartin Sperl 5463ecd37edSMartin Sperl static int bcm2835_spi_transfer_one_dma(struct spi_master *master, 5473ecd37edSMartin Sperl struct spi_device *spi, 5483ecd37edSMartin Sperl struct spi_transfer *tfr, 5493ecd37edSMartin Sperl u32 cs) 5503ecd37edSMartin Sperl { 5513ecd37edSMartin Sperl struct bcm2835_spi *bs = spi_master_get_devdata(master); 5523ecd37edSMartin Sperl int ret; 5533ecd37edSMartin Sperl 5543bd7f658SLukas Wunner /* 5553bd7f658SLukas Wunner * Transfer first few bytes without DMA if length of first TX or RX 5563bd7f658SLukas Wunner * sglist entry is not a multiple of 4 bytes (hardware limitation). 5573bd7f658SLukas Wunner */ 5583bd7f658SLukas Wunner bcm2835_spi_transfer_prologue(master, tfr, bs, cs); 5593ecd37edSMartin Sperl 5603ecd37edSMartin Sperl /* setup tx-DMA */ 5613ecd37edSMartin Sperl ret = bcm2835_spi_prepare_sg(master, tfr, true); 5623ecd37edSMartin Sperl if (ret) 5633bd7f658SLukas Wunner goto err_reset_hw; 5643ecd37edSMartin Sperl 5653ecd37edSMartin Sperl /* start TX early */ 5663ecd37edSMartin Sperl dma_async_issue_pending(master->dma_tx); 5673ecd37edSMartin Sperl 5683ecd37edSMartin Sperl /* mark as dma pending */ 5693ecd37edSMartin Sperl bs->dma_pending = 1; 5703ecd37edSMartin Sperl 5713ecd37edSMartin Sperl /* set the DMA length */ 5723bd7f658SLukas Wunner bcm2835_wr(bs, BCM2835_SPI_DLEN, bs->tx_len); 5733ecd37edSMartin Sperl 5743ecd37edSMartin Sperl /* start the HW */ 5753ecd37edSMartin Sperl bcm2835_wr(bs, BCM2835_SPI_CS, 5763ecd37edSMartin Sperl cs | BCM2835_SPI_CS_TA | BCM2835_SPI_CS_DMAEN); 5773ecd37edSMartin Sperl 5783ecd37edSMartin Sperl /* setup rx-DMA late - to run transfers while 5793ecd37edSMartin Sperl * mapping of the rx buffers still takes place 5803ecd37edSMartin Sperl * this saves 10us or more. 5813ecd37edSMartin Sperl */ 5823ecd37edSMartin Sperl ret = bcm2835_spi_prepare_sg(master, tfr, false); 5833ecd37edSMartin Sperl if (ret) { 5843ecd37edSMartin Sperl /* need to reset on errors */ 5852527704dSLukas Wunner dmaengine_terminate_sync(master->dma_tx); 586dbc94411SLukas Wunner bs->dma_pending = false; 5873bd7f658SLukas Wunner goto err_reset_hw; 5883ecd37edSMartin Sperl } 5893ecd37edSMartin Sperl 5903ecd37edSMartin Sperl /* start rx dma late */ 5913ecd37edSMartin Sperl dma_async_issue_pending(master->dma_rx); 5923ecd37edSMartin Sperl 5933ecd37edSMartin Sperl /* wait for wakeup in framework */ 5943ecd37edSMartin Sperl return 1; 5953bd7f658SLukas Wunner 5963bd7f658SLukas Wunner err_reset_hw: 5973bd7f658SLukas Wunner bcm2835_spi_reset_hw(master); 5983bd7f658SLukas Wunner bcm2835_spi_undo_prologue(bs); 5993bd7f658SLukas Wunner return ret; 6003ecd37edSMartin Sperl } 6013ecd37edSMartin Sperl 6023ecd37edSMartin Sperl static bool bcm2835_spi_can_dma(struct spi_master *master, 6033ecd37edSMartin Sperl struct spi_device *spi, 6043ecd37edSMartin Sperl struct spi_transfer *tfr) 6053ecd37edSMartin Sperl { 6063ecd37edSMartin Sperl /* we start DMA efforts only on bigger transfers */ 6073ecd37edSMartin Sperl if (tfr->len < BCM2835_SPI_DMA_MIN_LENGTH) 6083ecd37edSMartin Sperl return false; 6093ecd37edSMartin Sperl 6103ecd37edSMartin Sperl /* return OK */ 6113ecd37edSMartin Sperl return true; 6123ecd37edSMartin Sperl } 6133ecd37edSMartin Sperl 61429ad1a7aSkbuild test robot static void bcm2835_dma_release(struct spi_master *master) 6153ecd37edSMartin Sperl { 6163ecd37edSMartin Sperl if (master->dma_tx) { 6172527704dSLukas Wunner dmaengine_terminate_sync(master->dma_tx); 6183ecd37edSMartin Sperl dma_release_channel(master->dma_tx); 6193ecd37edSMartin Sperl master->dma_tx = NULL; 6203ecd37edSMartin Sperl } 6213ecd37edSMartin Sperl if (master->dma_rx) { 6222527704dSLukas Wunner dmaengine_terminate_sync(master->dma_rx); 6233ecd37edSMartin Sperl dma_release_channel(master->dma_rx); 6243ecd37edSMartin Sperl master->dma_rx = NULL; 6253ecd37edSMartin Sperl } 6263ecd37edSMartin Sperl } 6273ecd37edSMartin Sperl 62829ad1a7aSkbuild test robot static void bcm2835_dma_init(struct spi_master *master, struct device *dev) 6293ecd37edSMartin Sperl { 6303ecd37edSMartin Sperl struct dma_slave_config slave_config; 6313ecd37edSMartin Sperl const __be32 *addr; 6323ecd37edSMartin Sperl dma_addr_t dma_reg_base; 6333ecd37edSMartin Sperl int ret; 6343ecd37edSMartin Sperl 6353ecd37edSMartin Sperl /* base address in dma-space */ 6363ecd37edSMartin Sperl addr = of_get_address(master->dev.of_node, 0, NULL, NULL); 6373ecd37edSMartin Sperl if (!addr) { 6383ecd37edSMartin Sperl dev_err(dev, "could not get DMA-register address - not using dma mode\n"); 6393ecd37edSMartin Sperl goto err; 6403ecd37edSMartin Sperl } 6413ecd37edSMartin Sperl dma_reg_base = be32_to_cpup(addr); 6423ecd37edSMartin Sperl 6433ecd37edSMartin Sperl /* get tx/rx dma */ 6443ecd37edSMartin Sperl master->dma_tx = dma_request_slave_channel(dev, "tx"); 6453ecd37edSMartin Sperl if (!master->dma_tx) { 6463ecd37edSMartin Sperl dev_err(dev, "no tx-dma configuration found - not using dma mode\n"); 6473ecd37edSMartin Sperl goto err; 6483ecd37edSMartin Sperl } 6493ecd37edSMartin Sperl master->dma_rx = dma_request_slave_channel(dev, "rx"); 6503ecd37edSMartin Sperl if (!master->dma_rx) { 6513ecd37edSMartin Sperl dev_err(dev, "no rx-dma configuration found - not using dma mode\n"); 6523ecd37edSMartin Sperl goto err_release; 6533ecd37edSMartin Sperl } 6543ecd37edSMartin Sperl 6553ecd37edSMartin Sperl /* configure DMAs */ 6563ecd37edSMartin Sperl slave_config.direction = DMA_MEM_TO_DEV; 6573ecd37edSMartin Sperl slave_config.dst_addr = (u32)(dma_reg_base + BCM2835_SPI_FIFO); 6583ecd37edSMartin Sperl slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 6593ecd37edSMartin Sperl 6603ecd37edSMartin Sperl ret = dmaengine_slave_config(master->dma_tx, &slave_config); 6613ecd37edSMartin Sperl if (ret) 6623ecd37edSMartin Sperl goto err_config; 6633ecd37edSMartin Sperl 6643ecd37edSMartin Sperl slave_config.direction = DMA_DEV_TO_MEM; 6653ecd37edSMartin Sperl slave_config.src_addr = (u32)(dma_reg_base + BCM2835_SPI_FIFO); 6663ecd37edSMartin Sperl slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 6673ecd37edSMartin Sperl 6683ecd37edSMartin Sperl ret = dmaengine_slave_config(master->dma_rx, &slave_config); 6693ecd37edSMartin Sperl if (ret) 6703ecd37edSMartin Sperl goto err_config; 6713ecd37edSMartin Sperl 6723ecd37edSMartin Sperl /* all went well, so set can_dma */ 6733ecd37edSMartin Sperl master->can_dma = bcm2835_spi_can_dma; 6743ecd37edSMartin Sperl /* need to do TX AND RX DMA, so we need dummy buffers */ 6753ecd37edSMartin Sperl master->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX; 6763ecd37edSMartin Sperl 6773ecd37edSMartin Sperl return; 6783ecd37edSMartin Sperl 6793ecd37edSMartin Sperl err_config: 6803ecd37edSMartin Sperl dev_err(dev, "issue configuring dma: %d - not using DMA mode\n", 6813ecd37edSMartin Sperl ret); 6823ecd37edSMartin Sperl err_release: 6833ecd37edSMartin Sperl bcm2835_dma_release(master); 6843ecd37edSMartin Sperl err: 6853ecd37edSMartin Sperl return; 6863ecd37edSMartin Sperl } 6873ecd37edSMartin Sperl 688a750b124SMartin Sperl static int bcm2835_spi_transfer_one_poll(struct spi_master *master, 689a750b124SMartin Sperl struct spi_device *spi, 690a750b124SMartin Sperl struct spi_transfer *tfr, 691a750b124SMartin Sperl u32 cs, 6920122a518SMartin Sperl unsigned long long xfer_time_us) 693a750b124SMartin Sperl { 694a750b124SMartin Sperl struct bcm2835_spi *bs = spi_master_get_devdata(master); 695a750b124SMartin Sperl unsigned long timeout; 696a750b124SMartin Sperl 697a750b124SMartin Sperl /* enable HW block without interrupts */ 698a750b124SMartin Sperl bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA); 699a750b124SMartin Sperl 700a750b124SMartin Sperl /* fill in the fifo before timeout calculations 701a750b124SMartin Sperl * if we are interrupted here, then the data is 702a750b124SMartin Sperl * getting transferred by the HW while we are interrupted 703a750b124SMartin Sperl */ 7042e0733bcSLukas Wunner bcm2835_wr_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE); 705a750b124SMartin Sperl 706a750b124SMartin Sperl /* set the timeout */ 707a750b124SMartin Sperl timeout = jiffies + BCM2835_SPI_POLLING_JIFFIES; 708a750b124SMartin Sperl 709a750b124SMartin Sperl /* loop until finished the transfer */ 710a750b124SMartin Sperl while (bs->rx_len) { 711a750b124SMartin Sperl /* fill in tx fifo with remaining data */ 712a750b124SMartin Sperl bcm2835_wr_fifo(bs); 713a750b124SMartin Sperl 714a750b124SMartin Sperl /* read from fifo as much as possible */ 715a750b124SMartin Sperl bcm2835_rd_fifo(bs); 716a750b124SMartin Sperl 717a750b124SMartin Sperl /* if there is still data pending to read 718a750b124SMartin Sperl * then check the timeout 719a750b124SMartin Sperl */ 720a750b124SMartin Sperl if (bs->rx_len && time_after(jiffies, timeout)) { 721a750b124SMartin Sperl dev_dbg_ratelimited(&spi->dev, 722a750b124SMartin Sperl "timeout period reached: jiffies: %lu remaining tx/rx: %d/%d - falling back to interrupt mode\n", 723a750b124SMartin Sperl jiffies - timeout, 724a750b124SMartin Sperl bs->tx_len, bs->rx_len); 725a750b124SMartin Sperl /* fall back to interrupt mode */ 726a750b124SMartin Sperl return bcm2835_spi_transfer_one_irq(master, spi, 7272e0733bcSLukas Wunner tfr, cs, false); 728a750b124SMartin Sperl } 729a750b124SMartin Sperl } 730a750b124SMartin Sperl 731a750b124SMartin Sperl /* Transfer complete - reset SPI HW */ 732a750b124SMartin Sperl bcm2835_spi_reset_hw(master); 733a750b124SMartin Sperl /* and return without waiting for completion */ 734a750b124SMartin Sperl return 0; 735a750b124SMartin Sperl } 736a750b124SMartin Sperl 737704f32d4SMartin Sperl static int bcm2835_spi_transfer_one(struct spi_master *master, 738704f32d4SMartin Sperl struct spi_device *spi, 739704f32d4SMartin Sperl struct spi_transfer *tfr) 740704f32d4SMartin Sperl { 741704f32d4SMartin Sperl struct bcm2835_spi *bs = spi_master_get_devdata(master); 742704f32d4SMartin Sperl unsigned long spi_hz, clk_hz, cdiv; 7430122a518SMartin Sperl unsigned long spi_used_hz; 7440122a518SMartin Sperl unsigned long long xfer_time_us; 745704f32d4SMartin Sperl u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS); 746704f32d4SMartin Sperl 747704f32d4SMartin Sperl /* set clock */ 748704f32d4SMartin Sperl spi_hz = tfr->speed_hz; 749704f32d4SMartin Sperl clk_hz = clk_get_rate(bs->clk); 750704f32d4SMartin Sperl 751704f32d4SMartin Sperl if (spi_hz >= clk_hz / 2) { 752704f32d4SMartin Sperl cdiv = 2; /* clk_hz/2 is the fastest we can go */ 753704f32d4SMartin Sperl } else if (spi_hz) { 754704f32d4SMartin Sperl /* CDIV must be a multiple of two */ 755704f32d4SMartin Sperl cdiv = DIV_ROUND_UP(clk_hz, spi_hz); 756704f32d4SMartin Sperl cdiv += (cdiv % 2); 757704f32d4SMartin Sperl 758704f32d4SMartin Sperl if (cdiv >= 65536) 759704f32d4SMartin Sperl cdiv = 0; /* 0 is the slowest we can go */ 760704f32d4SMartin Sperl } else { 761704f32d4SMartin Sperl cdiv = 0; /* 0 is the slowest we can go */ 762704f32d4SMartin Sperl } 763704f32d4SMartin Sperl spi_used_hz = cdiv ? (clk_hz / cdiv) : (clk_hz / 65536); 764704f32d4SMartin Sperl bcm2835_wr(bs, BCM2835_SPI_CLK, cdiv); 765704f32d4SMartin Sperl 766acace73dSMartin Sperl /* handle all the 3-wire mode */ 767704f32d4SMartin Sperl if ((spi->mode & SPI_3WIRE) && (tfr->rx_buf)) 768704f32d4SMartin Sperl cs |= BCM2835_SPI_CS_REN; 769acace73dSMartin Sperl else 770acace73dSMartin Sperl cs &= ~BCM2835_SPI_CS_REN; 771704f32d4SMartin Sperl 7725c09e42fSLukas Wunner /* 7735c09e42fSLukas Wunner * The driver always uses software-controlled GPIO Chip Select. 7745c09e42fSLukas Wunner * Set the hardware-controlled native Chip Select to an invalid 7755c09e42fSLukas Wunner * value to prevent it from interfering. 776704f32d4SMartin Sperl */ 777704f32d4SMartin Sperl cs |= BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01; 778704f32d4SMartin Sperl 779704f32d4SMartin Sperl /* set transmit buffers and length */ 780704f32d4SMartin Sperl bs->tx_buf = tfr->tx_buf; 781704f32d4SMartin Sperl bs->rx_buf = tfr->rx_buf; 782704f32d4SMartin Sperl bs->tx_len = tfr->len; 783704f32d4SMartin Sperl bs->rx_len = tfr->len; 784704f32d4SMartin Sperl 785704f32d4SMartin Sperl /* calculate the estimated time in us the transfer runs */ 7860122a518SMartin Sperl xfer_time_us = (unsigned long long)tfr->len 787704f32d4SMartin Sperl * 9 /* clocks/byte - SPI-HW waits 1 clock after each byte */ 7880122a518SMartin Sperl * 1000000; 7890122a518SMartin Sperl do_div(xfer_time_us, spi_used_hz); 790704f32d4SMartin Sperl 791704f32d4SMartin Sperl /* for short requests run polling*/ 792704f32d4SMartin Sperl if (xfer_time_us <= BCM2835_SPI_POLLING_LIMIT_US) 793704f32d4SMartin Sperl return bcm2835_spi_transfer_one_poll(master, spi, tfr, 794704f32d4SMartin Sperl cs, xfer_time_us); 795704f32d4SMartin Sperl 7963ecd37edSMartin Sperl /* run in dma mode if conditions are right */ 7973ecd37edSMartin Sperl if (master->can_dma && bcm2835_spi_can_dma(master, spi, tfr)) 7983ecd37edSMartin Sperl return bcm2835_spi_transfer_one_dma(master, spi, tfr, cs); 7993ecd37edSMartin Sperl 8003ecd37edSMartin Sperl /* run in interrupt-mode */ 8012e0733bcSLukas Wunner return bcm2835_spi_transfer_one_irq(master, spi, tfr, cs, true); 802704f32d4SMartin Sperl } 803704f32d4SMartin Sperl 804acace73dSMartin Sperl static int bcm2835_spi_prepare_message(struct spi_master *master, 805acace73dSMartin Sperl struct spi_message *msg) 806acace73dSMartin Sperl { 807acace73dSMartin Sperl struct spi_device *spi = msg->spi; 808acace73dSMartin Sperl struct bcm2835_spi *bs = spi_master_get_devdata(master); 809acace73dSMartin Sperl u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS); 8108b7bd10eSMeghana Madhyastha int ret; 8118b7bd10eSMeghana Madhyastha 8128b7bd10eSMeghana Madhyastha /* 8138b7bd10eSMeghana Madhyastha * DMA transfers are limited to 16 bit (0 to 65535 bytes) by the SPI HW 8148b7bd10eSMeghana Madhyastha * due to DLEN. Split up transfers (32-bit FIFO aligned) if the limit is 8158b7bd10eSMeghana Madhyastha * exceeded. 8168b7bd10eSMeghana Madhyastha */ 8178b7bd10eSMeghana Madhyastha ret = spi_split_transfers_maxsize(master, msg, 65532, 8188b7bd10eSMeghana Madhyastha GFP_KERNEL | GFP_DMA); 8198b7bd10eSMeghana Madhyastha if (ret) 8208b7bd10eSMeghana Madhyastha return ret; 821acace73dSMartin Sperl 822acace73dSMartin Sperl cs &= ~(BCM2835_SPI_CS_CPOL | BCM2835_SPI_CS_CPHA); 823acace73dSMartin Sperl 824acace73dSMartin Sperl if (spi->mode & SPI_CPOL) 825acace73dSMartin Sperl cs |= BCM2835_SPI_CS_CPOL; 826acace73dSMartin Sperl if (spi->mode & SPI_CPHA) 827acace73dSMartin Sperl cs |= BCM2835_SPI_CS_CPHA; 828acace73dSMartin Sperl 829acace73dSMartin Sperl bcm2835_wr(bs, BCM2835_SPI_CS, cs); 830acace73dSMartin Sperl 831acace73dSMartin Sperl return 0; 832acace73dSMartin Sperl } 833acace73dSMartin Sperl 834e34ff011SMartin Sperl static void bcm2835_spi_handle_err(struct spi_master *master, 835e34ff011SMartin Sperl struct spi_message *msg) 836f8043872SChris Boot { 8373ecd37edSMartin Sperl struct bcm2835_spi *bs = spi_master_get_devdata(master); 8383ecd37edSMartin Sperl 8393ecd37edSMartin Sperl /* if an error occurred and we have an active dma, then terminate */ 840e82b0b38SLukas Wunner if (cmpxchg(&bs->dma_pending, true, false)) { 8412527704dSLukas Wunner dmaengine_terminate_sync(master->dma_tx); 8422527704dSLukas Wunner dmaengine_terminate_sync(master->dma_rx); 8433bd7f658SLukas Wunner bcm2835_spi_undo_prologue(bs); 8443ecd37edSMartin Sperl } 8453ecd37edSMartin Sperl /* and reset */ 846e34ff011SMartin Sperl bcm2835_spi_reset_hw(master); 847f8043872SChris Boot } 848f8043872SChris Boot 849a30a555dSMartin Sperl static int chip_match_name(struct gpio_chip *chip, void *data) 850a30a555dSMartin Sperl { 851a30a555dSMartin Sperl return !strcmp(chip->label, data); 852a30a555dSMartin Sperl } 853a30a555dSMartin Sperl 854e34ff011SMartin Sperl static int bcm2835_spi_setup(struct spi_device *spi) 855e34ff011SMartin Sperl { 856a30a555dSMartin Sperl int err; 857a30a555dSMartin Sperl struct gpio_chip *chip; 858e34ff011SMartin Sperl /* 859e34ff011SMartin Sperl * sanity checking the native-chipselects 860e34ff011SMartin Sperl */ 861e34ff011SMartin Sperl if (spi->mode & SPI_NO_CS) 862f8043872SChris Boot return 0; 863e34ff011SMartin Sperl if (gpio_is_valid(spi->cs_gpio)) 864e34ff011SMartin Sperl return 0; 865a30a555dSMartin Sperl if (spi->chip_select > 1) { 866a30a555dSMartin Sperl /* error in the case of native CS requested with CS > 1 867a30a555dSMartin Sperl * officially there is a CS2, but it is not documented 868a30a555dSMartin Sperl * which GPIO is connected with that... 869a30a555dSMartin Sperl */ 870a30a555dSMartin Sperl dev_err(&spi->dev, 871a30a555dSMartin Sperl "setup: only two native chip-selects are supported\n"); 872a30a555dSMartin Sperl return -EINVAL; 873a30a555dSMartin Sperl } 874a30a555dSMartin Sperl /* now translate native cs to GPIO */ 875a30a555dSMartin Sperl 876a30a555dSMartin Sperl /* get the gpio chip for the base */ 877a30a555dSMartin Sperl chip = gpiochip_find("pinctrl-bcm2835", chip_match_name); 878a30a555dSMartin Sperl if (!chip) 879e34ff011SMartin Sperl return 0; 880e34ff011SMartin Sperl 881a30a555dSMartin Sperl /* and calculate the real CS */ 882a30a555dSMartin Sperl spi->cs_gpio = chip->base + 8 - spi->chip_select; 883a30a555dSMartin Sperl 884a30a555dSMartin Sperl /* and set up the "mode" and level */ 885a30a555dSMartin Sperl dev_info(&spi->dev, "setting up native-CS%i as GPIO %i\n", 886a30a555dSMartin Sperl spi->chip_select, spi->cs_gpio); 887a30a555dSMartin Sperl 888a30a555dSMartin Sperl /* set up GPIO as output and pull to the correct level */ 889a30a555dSMartin Sperl err = gpio_direction_output(spi->cs_gpio, 890a30a555dSMartin Sperl (spi->mode & SPI_CS_HIGH) ? 0 : 1); 891a30a555dSMartin Sperl if (err) { 892e34ff011SMartin Sperl dev_err(&spi->dev, 893a30a555dSMartin Sperl "could not set CS%i gpio %i as output: %i", 894a30a555dSMartin Sperl spi->chip_select, spi->cs_gpio, err); 895a30a555dSMartin Sperl return err; 896a30a555dSMartin Sperl } 897a30a555dSMartin Sperl 898a30a555dSMartin Sperl return 0; 899f8043872SChris Boot } 900f8043872SChris Boot 901f8043872SChris Boot static int bcm2835_spi_probe(struct platform_device *pdev) 902f8043872SChris Boot { 903f8043872SChris Boot struct spi_master *master; 904f8043872SChris Boot struct bcm2835_spi *bs; 905f8043872SChris Boot struct resource *res; 906f8043872SChris Boot int err; 907f8043872SChris Boot 908f8043872SChris Boot master = spi_alloc_master(&pdev->dev, sizeof(*bs)); 909f8043872SChris Boot if (!master) { 910f8043872SChris Boot dev_err(&pdev->dev, "spi_alloc_master() failed\n"); 911f8043872SChris Boot return -ENOMEM; 912f8043872SChris Boot } 913f8043872SChris Boot 914f8043872SChris Boot platform_set_drvdata(pdev, master); 915f8043872SChris Boot 916f8043872SChris Boot master->mode_bits = BCM2835_SPI_MODE_BITS; 917c2b6a3a8SAxel Lin master->bits_per_word_mask = SPI_BPW_MASK(8); 918f8043872SChris Boot master->num_chipselect = 3; 919e34ff011SMartin Sperl master->setup = bcm2835_spi_setup; 920e34ff011SMartin Sperl master->transfer_one = bcm2835_spi_transfer_one; 921e34ff011SMartin Sperl master->handle_err = bcm2835_spi_handle_err; 922acace73dSMartin Sperl master->prepare_message = bcm2835_spi_prepare_message; 923f8043872SChris Boot master->dev.of_node = pdev->dev.of_node; 924f8043872SChris Boot 925f8043872SChris Boot bs = spi_master_get_devdata(master); 926f8043872SChris Boot 927f8043872SChris Boot res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 9282d6e75e8SLaurent Navet bs->regs = devm_ioremap_resource(&pdev->dev, res); 9292d6e75e8SLaurent Navet if (IS_ERR(bs->regs)) { 9302d6e75e8SLaurent Navet err = PTR_ERR(bs->regs); 931f8043872SChris Boot goto out_master_put; 932f8043872SChris Boot } 933f8043872SChris Boot 934f8043872SChris Boot bs->clk = devm_clk_get(&pdev->dev, NULL); 935f8043872SChris Boot if (IS_ERR(bs->clk)) { 936f8043872SChris Boot err = PTR_ERR(bs->clk); 937f8043872SChris Boot dev_err(&pdev->dev, "could not get clk: %d\n", err); 938f8043872SChris Boot goto out_master_put; 939f8043872SChris Boot } 940f8043872SChris Boot 941ddf0e1c2SMartin Sperl bs->irq = platform_get_irq(pdev, 0); 942f8043872SChris Boot if (bs->irq <= 0) { 943f8043872SChris Boot dev_err(&pdev->dev, "could not get IRQ: %d\n", bs->irq); 944f8043872SChris Boot err = bs->irq ? bs->irq : -ENODEV; 945f8043872SChris Boot goto out_master_put; 946f8043872SChris Boot } 947f8043872SChris Boot 948f8043872SChris Boot clk_prepare_enable(bs->clk); 949f8043872SChris Boot 950ddf0e1c2SMartin Sperl bcm2835_dma_init(master, &pdev->dev); 951ddf0e1c2SMartin Sperl 952ddf0e1c2SMartin Sperl /* initialise the hardware with the default polarities */ 953ddf0e1c2SMartin Sperl bcm2835_wr(bs, BCM2835_SPI_CS, 954ddf0e1c2SMartin Sperl BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX); 955ddf0e1c2SMartin Sperl 95608bc0544SJingoo Han err = devm_request_irq(&pdev->dev, bs->irq, bcm2835_spi_interrupt, 0, 957f8043872SChris Boot dev_name(&pdev->dev), master); 958f8043872SChris Boot if (err) { 959f8043872SChris Boot dev_err(&pdev->dev, "could not request IRQ: %d\n", err); 960f8043872SChris Boot goto out_clk_disable; 961f8043872SChris Boot } 962f8043872SChris Boot 963247263dbSJingoo Han err = devm_spi_register_master(&pdev->dev, master); 964f8043872SChris Boot if (err) { 965f8043872SChris Boot dev_err(&pdev->dev, "could not register SPI master: %d\n", err); 96608bc0544SJingoo Han goto out_clk_disable; 967f8043872SChris Boot } 968f8043872SChris Boot 969f8043872SChris Boot return 0; 970f8043872SChris Boot 971f8043872SChris Boot out_clk_disable: 972f8043872SChris Boot clk_disable_unprepare(bs->clk); 973f8043872SChris Boot out_master_put: 974f8043872SChris Boot spi_master_put(master); 975f8043872SChris Boot return err; 976f8043872SChris Boot } 977f8043872SChris Boot 978f8043872SChris Boot static int bcm2835_spi_remove(struct platform_device *pdev) 979f8043872SChris Boot { 980e0b35b89SWei Yongjun struct spi_master *master = platform_get_drvdata(pdev); 981f8043872SChris Boot struct bcm2835_spi *bs = spi_master_get_devdata(master); 982f8043872SChris Boot 983f8043872SChris Boot /* Clear FIFOs, and disable the HW block */ 984f8043872SChris Boot bcm2835_wr(bs, BCM2835_SPI_CS, 985f8043872SChris Boot BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX); 986f8043872SChris Boot 987f8043872SChris Boot clk_disable_unprepare(bs->clk); 988f8043872SChris Boot 9893ecd37edSMartin Sperl bcm2835_dma_release(master); 9903ecd37edSMartin Sperl 991f8043872SChris Boot return 0; 992f8043872SChris Boot } 993f8043872SChris Boot 994f8043872SChris Boot static const struct of_device_id bcm2835_spi_match[] = { 995f8043872SChris Boot { .compatible = "brcm,bcm2835-spi", }, 996f8043872SChris Boot {} 997f8043872SChris Boot }; 998f8043872SChris Boot MODULE_DEVICE_TABLE(of, bcm2835_spi_match); 999f8043872SChris Boot 1000f8043872SChris Boot static struct platform_driver bcm2835_spi_driver = { 1001f8043872SChris Boot .driver = { 1002f8043872SChris Boot .name = DRV_NAME, 1003f8043872SChris Boot .of_match_table = bcm2835_spi_match, 1004f8043872SChris Boot }, 1005f8043872SChris Boot .probe = bcm2835_spi_probe, 1006f8043872SChris Boot .remove = bcm2835_spi_remove, 1007f8043872SChris Boot }; 1008f8043872SChris Boot module_platform_driver(bcm2835_spi_driver); 1009f8043872SChris Boot 1010f8043872SChris Boot MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835"); 1011f8043872SChris Boot MODULE_AUTHOR("Chris Boot <bootc@bootc.net>"); 101222bf6cd2SStefan Wahren MODULE_LICENSE("GPL"); 1013