Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 1 | /* |
| 2 | * SPI bus driver for CSR SiRFprimaII |
| 3 | * |
| 4 | * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company. |
| 5 | * |
| 6 | * Licensed under GPLv2 or later. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/clk.h> |
Qipan Li | c908ef3 | 2014-04-15 15:24:59 +0800 | [diff] [blame] | 13 | #include <linux/completion.h> |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/io.h> |
| 16 | #include <linux/of.h> |
| 17 | #include <linux/bitops.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/of_gpio.h> |
| 21 | #include <linux/spi/spi.h> |
| 22 | #include <linux/spi/spi_bitbang.h> |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 23 | #include <linux/dmaengine.h> |
| 24 | #include <linux/dma-direction.h> |
| 25 | #include <linux/dma-mapping.h> |
Qipan Li | 8509c55 | 2014-11-20 22:33:07 +0800 | [diff] [blame] | 26 | #include <linux/reset.h> |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 27 | |
| 28 | #define DRIVER_NAME "sirfsoc_spi" |
| 29 | |
| 30 | #define SIRFSOC_SPI_CTRL 0x0000 |
| 31 | #define SIRFSOC_SPI_CMD 0x0004 |
| 32 | #define SIRFSOC_SPI_TX_RX_EN 0x0008 |
| 33 | #define SIRFSOC_SPI_INT_EN 0x000C |
| 34 | #define SIRFSOC_SPI_INT_STATUS 0x0010 |
| 35 | #define SIRFSOC_SPI_TX_DMA_IO_CTRL 0x0100 |
| 36 | #define SIRFSOC_SPI_TX_DMA_IO_LEN 0x0104 |
| 37 | #define SIRFSOC_SPI_TXFIFO_CTRL 0x0108 |
| 38 | #define SIRFSOC_SPI_TXFIFO_LEVEL_CHK 0x010C |
| 39 | #define SIRFSOC_SPI_TXFIFO_OP 0x0110 |
| 40 | #define SIRFSOC_SPI_TXFIFO_STATUS 0x0114 |
| 41 | #define SIRFSOC_SPI_TXFIFO_DATA 0x0118 |
| 42 | #define SIRFSOC_SPI_RX_DMA_IO_CTRL 0x0120 |
| 43 | #define SIRFSOC_SPI_RX_DMA_IO_LEN 0x0124 |
| 44 | #define SIRFSOC_SPI_RXFIFO_CTRL 0x0128 |
| 45 | #define SIRFSOC_SPI_RXFIFO_LEVEL_CHK 0x012C |
| 46 | #define SIRFSOC_SPI_RXFIFO_OP 0x0130 |
| 47 | #define SIRFSOC_SPI_RXFIFO_STATUS 0x0134 |
| 48 | #define SIRFSOC_SPI_RXFIFO_DATA 0x0138 |
| 49 | #define SIRFSOC_SPI_DUMMY_DELAY_CTL 0x0144 |
| 50 | |
| 51 | /* SPI CTRL register defines */ |
| 52 | #define SIRFSOC_SPI_SLV_MODE BIT(16) |
| 53 | #define SIRFSOC_SPI_CMD_MODE BIT(17) |
| 54 | #define SIRFSOC_SPI_CS_IO_OUT BIT(18) |
| 55 | #define SIRFSOC_SPI_CS_IO_MODE BIT(19) |
| 56 | #define SIRFSOC_SPI_CLK_IDLE_STAT BIT(20) |
| 57 | #define SIRFSOC_SPI_CS_IDLE_STAT BIT(21) |
| 58 | #define SIRFSOC_SPI_TRAN_MSB BIT(22) |
| 59 | #define SIRFSOC_SPI_DRV_POS_EDGE BIT(23) |
| 60 | #define SIRFSOC_SPI_CS_HOLD_TIME BIT(24) |
| 61 | #define SIRFSOC_SPI_CLK_SAMPLE_MODE BIT(25) |
| 62 | #define SIRFSOC_SPI_TRAN_DAT_FORMAT_8 (0 << 26) |
| 63 | #define SIRFSOC_SPI_TRAN_DAT_FORMAT_12 (1 << 26) |
| 64 | #define SIRFSOC_SPI_TRAN_DAT_FORMAT_16 (2 << 26) |
| 65 | #define SIRFSOC_SPI_TRAN_DAT_FORMAT_32 (3 << 26) |
Qipan Li | 9593e61 | 2014-09-02 17:02:36 +0800 | [diff] [blame] | 66 | #define SIRFSOC_SPI_CMD_BYTE_NUM(x) ((x & 3) << 28) |
| 67 | #define SIRFSOC_SPI_ENA_AUTO_CLR BIT(30) |
| 68 | #define SIRFSOC_SPI_MUL_DAT_MODE BIT(31) |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 69 | |
| 70 | /* Interrupt Enable */ |
Qipan Li | 9593e61 | 2014-09-02 17:02:36 +0800 | [diff] [blame] | 71 | #define SIRFSOC_SPI_RX_DONE_INT_EN BIT(0) |
| 72 | #define SIRFSOC_SPI_TX_DONE_INT_EN BIT(1) |
| 73 | #define SIRFSOC_SPI_RX_OFLOW_INT_EN BIT(2) |
| 74 | #define SIRFSOC_SPI_TX_UFLOW_INT_EN BIT(3) |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 75 | #define SIRFSOC_SPI_RX_IO_DMA_INT_EN BIT(4) |
| 76 | #define SIRFSOC_SPI_TX_IO_DMA_INT_EN BIT(5) |
| 77 | #define SIRFSOC_SPI_RXFIFO_FULL_INT_EN BIT(6) |
| 78 | #define SIRFSOC_SPI_TXFIFO_EMPTY_INT_EN BIT(7) |
| 79 | #define SIRFSOC_SPI_RXFIFO_THD_INT_EN BIT(8) |
| 80 | #define SIRFSOC_SPI_TXFIFO_THD_INT_EN BIT(9) |
| 81 | #define SIRFSOC_SPI_FRM_END_INT_EN BIT(10) |
| 82 | |
Qipan Li | 9593e61 | 2014-09-02 17:02:36 +0800 | [diff] [blame] | 83 | #define SIRFSOC_SPI_INT_MASK_ALL 0x1FFF |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 84 | |
| 85 | /* Interrupt status */ |
| 86 | #define SIRFSOC_SPI_RX_DONE BIT(0) |
| 87 | #define SIRFSOC_SPI_TX_DONE BIT(1) |
| 88 | #define SIRFSOC_SPI_RX_OFLOW BIT(2) |
| 89 | #define SIRFSOC_SPI_TX_UFLOW BIT(3) |
Qipan Li | 41148c3 | 2014-05-04 14:32:36 +0800 | [diff] [blame] | 90 | #define SIRFSOC_SPI_RX_IO_DMA BIT(4) |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 91 | #define SIRFSOC_SPI_RX_FIFO_FULL BIT(6) |
| 92 | #define SIRFSOC_SPI_TXFIFO_EMPTY BIT(7) |
| 93 | #define SIRFSOC_SPI_RXFIFO_THD_REACH BIT(8) |
| 94 | #define SIRFSOC_SPI_TXFIFO_THD_REACH BIT(9) |
| 95 | #define SIRFSOC_SPI_FRM_END BIT(10) |
| 96 | |
| 97 | /* TX RX enable */ |
| 98 | #define SIRFSOC_SPI_RX_EN BIT(0) |
| 99 | #define SIRFSOC_SPI_TX_EN BIT(1) |
| 100 | #define SIRFSOC_SPI_CMD_TX_EN BIT(2) |
| 101 | |
| 102 | #define SIRFSOC_SPI_IO_MODE_SEL BIT(0) |
| 103 | #define SIRFSOC_SPI_RX_DMA_FLUSH BIT(2) |
| 104 | |
| 105 | /* FIFO OPs */ |
| 106 | #define SIRFSOC_SPI_FIFO_RESET BIT(0) |
| 107 | #define SIRFSOC_SPI_FIFO_START BIT(1) |
| 108 | |
| 109 | /* FIFO CTRL */ |
| 110 | #define SIRFSOC_SPI_FIFO_WIDTH_BYTE (0 << 0) |
| 111 | #define SIRFSOC_SPI_FIFO_WIDTH_WORD (1 << 0) |
| 112 | #define SIRFSOC_SPI_FIFO_WIDTH_DWORD (2 << 0) |
| 113 | |
| 114 | /* FIFO Status */ |
| 115 | #define SIRFSOC_SPI_FIFO_LEVEL_MASK 0xFF |
| 116 | #define SIRFSOC_SPI_FIFO_FULL BIT(8) |
| 117 | #define SIRFSOC_SPI_FIFO_EMPTY BIT(9) |
| 118 | |
| 119 | /* 256 bytes rx/tx FIFO */ |
| 120 | #define SIRFSOC_SPI_FIFO_SIZE 256 |
| 121 | #define SIRFSOC_SPI_DAT_FRM_LEN_MAX (64 * 1024) |
| 122 | |
| 123 | #define SIRFSOC_SPI_FIFO_SC(x) ((x) & 0x3F) |
| 124 | #define SIRFSOC_SPI_FIFO_LC(x) (((x) & 0x3F) << 10) |
| 125 | #define SIRFSOC_SPI_FIFO_HC(x) (((x) & 0x3F) << 20) |
| 126 | #define SIRFSOC_SPI_FIFO_THD(x) (((x) & 0xFF) << 2) |
| 127 | |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 128 | /* |
| 129 | * only if the rx/tx buffer and transfer size are 4-bytes aligned, we use dma |
| 130 | * due to the limitation of dma controller |
| 131 | */ |
| 132 | |
| 133 | #define ALIGNED(x) (!((u32)x & 0x3)) |
| 134 | #define IS_DMA_VALID(x) (x && ALIGNED(x->tx_buf) && ALIGNED(x->rx_buf) && \ |
Qipan Li | 692fb0f | 2013-08-25 21:42:50 +0800 | [diff] [blame] | 135 | ALIGNED(x->len) && (x->len < 2 * PAGE_SIZE)) |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 136 | |
Qipan Li | eeb71395 | 2014-03-01 12:38:17 +0800 | [diff] [blame] | 137 | #define SIRFSOC_MAX_CMD_BYTES 4 |
Qipan Li | fcc50e5 | 2014-11-17 23:17:03 +0800 | [diff] [blame] | 138 | #define SIRFSOC_SPI_DEFAULT_FRQ 1000000 |
Qipan Li | eeb71395 | 2014-03-01 12:38:17 +0800 | [diff] [blame] | 139 | |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 140 | struct sirfsoc_spi { |
| 141 | struct spi_bitbang bitbang; |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 142 | struct completion rx_done; |
| 143 | struct completion tx_done; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 144 | |
| 145 | void __iomem *base; |
| 146 | u32 ctrl_freq; /* SPI controller clock speed */ |
| 147 | struct clk *clk; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 148 | |
| 149 | /* rx & tx bufs from the spi_transfer */ |
| 150 | const void *tx; |
| 151 | void *rx; |
| 152 | |
| 153 | /* place received word into rx buffer */ |
| 154 | void (*rx_word) (struct sirfsoc_spi *); |
| 155 | /* get word from tx buffer for sending */ |
| 156 | void (*tx_word) (struct sirfsoc_spi *); |
| 157 | |
| 158 | /* number of words left to be tranmitted/received */ |
Qipan Li | 692fb0f | 2013-08-25 21:42:50 +0800 | [diff] [blame] | 159 | unsigned int left_tx_word; |
| 160 | unsigned int left_rx_word; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 161 | |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 162 | /* rx & tx DMA channels */ |
| 163 | struct dma_chan *rx_chan; |
| 164 | struct dma_chan *tx_chan; |
| 165 | dma_addr_t src_start; |
| 166 | dma_addr_t dst_start; |
| 167 | void *dummypage; |
| 168 | int word_width; /* in bytes */ |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 169 | |
Qipan Li | eeb71395 | 2014-03-01 12:38:17 +0800 | [diff] [blame] | 170 | /* |
| 171 | * if tx size is not more than 4 and rx size is NULL, use |
| 172 | * command model |
| 173 | */ |
| 174 | bool tx_by_cmd; |
Qipan Li | 7850cdf | 2014-09-02 17:01:01 +0800 | [diff] [blame] | 175 | bool hw_cs; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | static void spi_sirfsoc_rx_word_u8(struct sirfsoc_spi *sspi) |
| 179 | { |
| 180 | u32 data; |
| 181 | u8 *rx = sspi->rx; |
| 182 | |
| 183 | data = readl(sspi->base + SIRFSOC_SPI_RXFIFO_DATA); |
| 184 | |
| 185 | if (rx) { |
| 186 | *rx++ = (u8) data; |
| 187 | sspi->rx = rx; |
| 188 | } |
| 189 | |
Qipan Li | 692fb0f | 2013-08-25 21:42:50 +0800 | [diff] [blame] | 190 | sspi->left_rx_word--; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static void spi_sirfsoc_tx_word_u8(struct sirfsoc_spi *sspi) |
| 194 | { |
| 195 | u32 data = 0; |
| 196 | const u8 *tx = sspi->tx; |
| 197 | |
| 198 | if (tx) { |
| 199 | data = *tx++; |
| 200 | sspi->tx = tx; |
| 201 | } |
| 202 | |
| 203 | writel(data, sspi->base + SIRFSOC_SPI_TXFIFO_DATA); |
Qipan Li | 692fb0f | 2013-08-25 21:42:50 +0800 | [diff] [blame] | 204 | sspi->left_tx_word--; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static void spi_sirfsoc_rx_word_u16(struct sirfsoc_spi *sspi) |
| 208 | { |
| 209 | u32 data; |
| 210 | u16 *rx = sspi->rx; |
| 211 | |
| 212 | data = readl(sspi->base + SIRFSOC_SPI_RXFIFO_DATA); |
| 213 | |
| 214 | if (rx) { |
| 215 | *rx++ = (u16) data; |
| 216 | sspi->rx = rx; |
| 217 | } |
| 218 | |
Qipan Li | 692fb0f | 2013-08-25 21:42:50 +0800 | [diff] [blame] | 219 | sspi->left_rx_word--; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static void spi_sirfsoc_tx_word_u16(struct sirfsoc_spi *sspi) |
| 223 | { |
| 224 | u32 data = 0; |
| 225 | const u16 *tx = sspi->tx; |
| 226 | |
| 227 | if (tx) { |
| 228 | data = *tx++; |
| 229 | sspi->tx = tx; |
| 230 | } |
| 231 | |
| 232 | writel(data, sspi->base + SIRFSOC_SPI_TXFIFO_DATA); |
Qipan Li | 692fb0f | 2013-08-25 21:42:50 +0800 | [diff] [blame] | 233 | sspi->left_tx_word--; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static void spi_sirfsoc_rx_word_u32(struct sirfsoc_spi *sspi) |
| 237 | { |
| 238 | u32 data; |
| 239 | u32 *rx = sspi->rx; |
| 240 | |
| 241 | data = readl(sspi->base + SIRFSOC_SPI_RXFIFO_DATA); |
| 242 | |
| 243 | if (rx) { |
| 244 | *rx++ = (u32) data; |
| 245 | sspi->rx = rx; |
| 246 | } |
| 247 | |
Qipan Li | 692fb0f | 2013-08-25 21:42:50 +0800 | [diff] [blame] | 248 | sspi->left_rx_word--; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 249 | |
| 250 | } |
| 251 | |
| 252 | static void spi_sirfsoc_tx_word_u32(struct sirfsoc_spi *sspi) |
| 253 | { |
| 254 | u32 data = 0; |
| 255 | const u32 *tx = sspi->tx; |
| 256 | |
| 257 | if (tx) { |
| 258 | data = *tx++; |
| 259 | sspi->tx = tx; |
| 260 | } |
| 261 | |
| 262 | writel(data, sspi->base + SIRFSOC_SPI_TXFIFO_DATA); |
Qipan Li | 692fb0f | 2013-08-25 21:42:50 +0800 | [diff] [blame] | 263 | sspi->left_tx_word--; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 264 | } |
| 265 | |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 266 | static irqreturn_t spi_sirfsoc_irq(int irq, void *dev_id) |
| 267 | { |
| 268 | struct sirfsoc_spi *sspi = dev_id; |
| 269 | u32 spi_stat = readl(sspi->base + SIRFSOC_SPI_INT_STATUS); |
Qipan Li | eeb71395 | 2014-03-01 12:38:17 +0800 | [diff] [blame] | 270 | if (sspi->tx_by_cmd && (spi_stat & SIRFSOC_SPI_FRM_END)) { |
| 271 | complete(&sspi->tx_done); |
| 272 | writel(0x0, sspi->base + SIRFSOC_SPI_INT_EN); |
Qipan Li | 41148c3 | 2014-05-04 14:32:36 +0800 | [diff] [blame] | 273 | writel(SIRFSOC_SPI_INT_MASK_ALL, |
| 274 | sspi->base + SIRFSOC_SPI_INT_STATUS); |
Qipan Li | eeb71395 | 2014-03-01 12:38:17 +0800 | [diff] [blame] | 275 | return IRQ_HANDLED; |
| 276 | } |
| 277 | |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 278 | /* Error Conditions */ |
| 279 | if (spi_stat & SIRFSOC_SPI_RX_OFLOW || |
| 280 | spi_stat & SIRFSOC_SPI_TX_UFLOW) { |
Qipan Li | 41148c3 | 2014-05-04 14:32:36 +0800 | [diff] [blame] | 281 | complete(&sspi->tx_done); |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 282 | complete(&sspi->rx_done); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 283 | writel(0x0, sspi->base + SIRFSOC_SPI_INT_EN); |
Qipan Li | 41148c3 | 2014-05-04 14:32:36 +0800 | [diff] [blame] | 284 | writel(SIRFSOC_SPI_INT_MASK_ALL, |
| 285 | sspi->base + SIRFSOC_SPI_INT_STATUS); |
| 286 | return IRQ_HANDLED; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 287 | } |
Qipan Li | 41148c3 | 2014-05-04 14:32:36 +0800 | [diff] [blame] | 288 | if (spi_stat & SIRFSOC_SPI_TXFIFO_EMPTY) |
| 289 | complete(&sspi->tx_done); |
| 290 | while (!(readl(sspi->base + SIRFSOC_SPI_INT_STATUS) & |
| 291 | SIRFSOC_SPI_RX_IO_DMA)) |
| 292 | cpu_relax(); |
| 293 | complete(&sspi->rx_done); |
| 294 | writel(0x0, sspi->base + SIRFSOC_SPI_INT_EN); |
| 295 | writel(SIRFSOC_SPI_INT_MASK_ALL, |
| 296 | sspi->base + SIRFSOC_SPI_INT_STATUS); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 297 | |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 298 | return IRQ_HANDLED; |
| 299 | } |
| 300 | |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 301 | static void spi_sirfsoc_dma_fini_callback(void *data) |
| 302 | { |
| 303 | struct completion *dma_complete = data; |
| 304 | |
| 305 | complete(dma_complete); |
| 306 | } |
| 307 | |
Qipan Li | 0021d97 | 2014-09-02 17:01:04 +0800 | [diff] [blame] | 308 | static void spi_sirfsoc_cmd_transfer(struct spi_device *spi, |
Qipan Li | c908ef3 | 2014-04-15 15:24:59 +0800 | [diff] [blame] | 309 | struct spi_transfer *t) |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 310 | { |
| 311 | struct sirfsoc_spi *sspi; |
| 312 | int timeout = t->len * 10; |
Qipan Li | c908ef3 | 2014-04-15 15:24:59 +0800 | [diff] [blame] | 313 | u32 cmd; |
| 314 | |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 315 | sspi = spi_master_get_devdata(spi->master); |
Qipan Li | 810a58b | 2014-09-02 17:02:34 +0800 | [diff] [blame] | 316 | writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_TXFIFO_OP); |
| 317 | writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_TXFIFO_OP); |
Qipan Li | c908ef3 | 2014-04-15 15:24:59 +0800 | [diff] [blame] | 318 | memcpy(&cmd, sspi->tx, t->len); |
| 319 | if (sspi->word_width == 1 && !(spi->mode & SPI_LSB_FIRST)) |
| 320 | cmd = cpu_to_be32(cmd) >> |
| 321 | ((SIRFSOC_MAX_CMD_BYTES - t->len) * 8); |
| 322 | if (sspi->word_width == 2 && t->len == 4 && |
| 323 | (!(spi->mode & SPI_LSB_FIRST))) |
| 324 | cmd = ((cmd & 0xffff) << 16) | (cmd >> 16); |
| 325 | writel(cmd, sspi->base + SIRFSOC_SPI_CMD); |
| 326 | writel(SIRFSOC_SPI_FRM_END_INT_EN, |
| 327 | sspi->base + SIRFSOC_SPI_INT_EN); |
| 328 | writel(SIRFSOC_SPI_CMD_TX_EN, |
| 329 | sspi->base + SIRFSOC_SPI_TX_RX_EN); |
| 330 | if (wait_for_completion_timeout(&sspi->tx_done, timeout) == 0) { |
| 331 | dev_err(&spi->dev, "cmd transfer timeout\n"); |
Qipan Li | 0021d97 | 2014-09-02 17:01:04 +0800 | [diff] [blame] | 332 | return; |
Qipan Li | eeb71395 | 2014-03-01 12:38:17 +0800 | [diff] [blame] | 333 | } |
Qipan Li | 0021d97 | 2014-09-02 17:01:04 +0800 | [diff] [blame] | 334 | sspi->left_rx_word -= t->len; |
Qipan Li | c908ef3 | 2014-04-15 15:24:59 +0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | static void spi_sirfsoc_dma_transfer(struct spi_device *spi, |
| 338 | struct spi_transfer *t) |
| 339 | { |
| 340 | struct sirfsoc_spi *sspi; |
| 341 | struct dma_async_tx_descriptor *rx_desc, *tx_desc; |
| 342 | int timeout = t->len * 10; |
| 343 | |
| 344 | sspi = spi_master_get_devdata(spi->master); |
| 345 | writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_RXFIFO_OP); |
| 346 | writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_TXFIFO_OP); |
| 347 | writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_RXFIFO_OP); |
| 348 | writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_TXFIFO_OP); |
| 349 | writel(0, sspi->base + SIRFSOC_SPI_INT_EN); |
| 350 | writel(SIRFSOC_SPI_INT_MASK_ALL, sspi->base + SIRFSOC_SPI_INT_STATUS); |
| 351 | if (sspi->left_tx_word < SIRFSOC_SPI_DAT_FRM_LEN_MAX) { |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 352 | writel(readl(sspi->base + SIRFSOC_SPI_CTRL) | |
Qipan Li | c908ef3 | 2014-04-15 15:24:59 +0800 | [diff] [blame] | 353 | SIRFSOC_SPI_ENA_AUTO_CLR | SIRFSOC_SPI_MUL_DAT_MODE, |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 354 | sspi->base + SIRFSOC_SPI_CTRL); |
Qipan Li | 692fb0f | 2013-08-25 21:42:50 +0800 | [diff] [blame] | 355 | writel(sspi->left_tx_word - 1, |
| 356 | sspi->base + SIRFSOC_SPI_TX_DMA_IO_LEN); |
| 357 | writel(sspi->left_tx_word - 1, |
| 358 | sspi->base + SIRFSOC_SPI_RX_DMA_IO_LEN); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 359 | } else { |
| 360 | writel(readl(sspi->base + SIRFSOC_SPI_CTRL), |
| 361 | sspi->base + SIRFSOC_SPI_CTRL); |
| 362 | writel(0, sspi->base + SIRFSOC_SPI_TX_DMA_IO_LEN); |
| 363 | writel(0, sspi->base + SIRFSOC_SPI_RX_DMA_IO_LEN); |
| 364 | } |
Qipan Li | c908ef3 | 2014-04-15 15:24:59 +0800 | [diff] [blame] | 365 | sspi->dst_start = dma_map_single(&spi->dev, sspi->rx, t->len, |
| 366 | (t->tx_buf != t->rx_buf) ? |
| 367 | DMA_FROM_DEVICE : DMA_BIDIRECTIONAL); |
| 368 | rx_desc = dmaengine_prep_slave_single(sspi->rx_chan, |
| 369 | sspi->dst_start, t->len, DMA_DEV_TO_MEM, |
| 370 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 371 | rx_desc->callback = spi_sirfsoc_dma_fini_callback; |
| 372 | rx_desc->callback_param = &sspi->rx_done; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 373 | |
Qipan Li | c908ef3 | 2014-04-15 15:24:59 +0800 | [diff] [blame] | 374 | sspi->src_start = dma_map_single(&spi->dev, (void *)sspi->tx, t->len, |
| 375 | (t->tx_buf != t->rx_buf) ? |
| 376 | DMA_TO_DEVICE : DMA_BIDIRECTIONAL); |
| 377 | tx_desc = dmaengine_prep_slave_single(sspi->tx_chan, |
| 378 | sspi->src_start, t->len, DMA_MEM_TO_DEV, |
| 379 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 380 | tx_desc->callback = spi_sirfsoc_dma_fini_callback; |
| 381 | tx_desc->callback_param = &sspi->tx_done; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 382 | |
Qipan Li | c908ef3 | 2014-04-15 15:24:59 +0800 | [diff] [blame] | 383 | dmaengine_submit(tx_desc); |
| 384 | dmaengine_submit(rx_desc); |
| 385 | dma_async_issue_pending(sspi->tx_chan); |
| 386 | dma_async_issue_pending(sspi->rx_chan); |
Qipan Li | d77ec5d | 2014-04-14 14:30:00 +0800 | [diff] [blame] | 387 | writel(SIRFSOC_SPI_RX_EN | SIRFSOC_SPI_TX_EN, |
| 388 | sspi->base + SIRFSOC_SPI_TX_RX_EN); |
Qipan Li | c908ef3 | 2014-04-15 15:24:59 +0800 | [diff] [blame] | 389 | if (wait_for_completion_timeout(&sspi->rx_done, timeout) == 0) { |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 390 | dev_err(&spi->dev, "transfer timeout\n"); |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 391 | dmaengine_terminate_all(sspi->rx_chan); |
| 392 | } else |
Qipan Li | 692fb0f | 2013-08-25 21:42:50 +0800 | [diff] [blame] | 393 | sspi->left_rx_word = 0; |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 394 | /* |
| 395 | * we only wait tx-done event if transferring by DMA. for PIO, |
| 396 | * we get rx data by writing tx data, so if rx is done, tx has |
| 397 | * done earlier |
| 398 | */ |
Qipan Li | c908ef3 | 2014-04-15 15:24:59 +0800 | [diff] [blame] | 399 | if (wait_for_completion_timeout(&sspi->tx_done, timeout) == 0) { |
| 400 | dev_err(&spi->dev, "transfer timeout\n"); |
| 401 | dmaengine_terminate_all(sspi->tx_chan); |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 402 | } |
Qipan Li | c908ef3 | 2014-04-15 15:24:59 +0800 | [diff] [blame] | 403 | dma_unmap_single(&spi->dev, sspi->src_start, t->len, DMA_TO_DEVICE); |
| 404 | dma_unmap_single(&spi->dev, sspi->dst_start, t->len, DMA_FROM_DEVICE); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 405 | /* TX, RX FIFO stop */ |
| 406 | writel(0, sspi->base + SIRFSOC_SPI_RXFIFO_OP); |
| 407 | writel(0, sspi->base + SIRFSOC_SPI_TXFIFO_OP); |
Qipan Li | c908ef3 | 2014-04-15 15:24:59 +0800 | [diff] [blame] | 408 | if (sspi->left_tx_word >= SIRFSOC_SPI_DAT_FRM_LEN_MAX) |
| 409 | writel(0, sspi->base + SIRFSOC_SPI_TX_RX_EN); |
| 410 | } |
| 411 | |
| 412 | static void spi_sirfsoc_pio_transfer(struct spi_device *spi, |
| 413 | struct spi_transfer *t) |
| 414 | { |
| 415 | struct sirfsoc_spi *sspi; |
| 416 | int timeout = t->len * 10; |
| 417 | |
| 418 | sspi = spi_master_get_devdata(spi->master); |
Qipan Li | 41148c3 | 2014-05-04 14:32:36 +0800 | [diff] [blame] | 419 | do { |
| 420 | writel(SIRFSOC_SPI_FIFO_RESET, |
| 421 | sspi->base + SIRFSOC_SPI_RXFIFO_OP); |
| 422 | writel(SIRFSOC_SPI_FIFO_RESET, |
| 423 | sspi->base + SIRFSOC_SPI_TXFIFO_OP); |
| 424 | writel(SIRFSOC_SPI_FIFO_START, |
| 425 | sspi->base + SIRFSOC_SPI_RXFIFO_OP); |
| 426 | writel(SIRFSOC_SPI_FIFO_START, |
| 427 | sspi->base + SIRFSOC_SPI_TXFIFO_OP); |
| 428 | writel(0, sspi->base + SIRFSOC_SPI_INT_EN); |
| 429 | writel(SIRFSOC_SPI_INT_MASK_ALL, |
| 430 | sspi->base + SIRFSOC_SPI_INT_STATUS); |
| 431 | writel(readl(sspi->base + SIRFSOC_SPI_CTRL) | |
| 432 | SIRFSOC_SPI_MUL_DAT_MODE | SIRFSOC_SPI_ENA_AUTO_CLR, |
| 433 | sspi->base + SIRFSOC_SPI_CTRL); |
| 434 | writel(min(sspi->left_tx_word, (u32)(256 / sspi->word_width)) |
| 435 | - 1, sspi->base + SIRFSOC_SPI_TX_DMA_IO_LEN); |
| 436 | writel(min(sspi->left_rx_word, (u32)(256 / sspi->word_width)) |
| 437 | - 1, sspi->base + SIRFSOC_SPI_RX_DMA_IO_LEN); |
| 438 | while (!((readl(sspi->base + SIRFSOC_SPI_TXFIFO_STATUS) |
| 439 | & SIRFSOC_SPI_FIFO_FULL)) && sspi->left_tx_word) |
| 440 | sspi->tx_word(sspi); |
| 441 | writel(SIRFSOC_SPI_TXFIFO_EMPTY_INT_EN | |
| 442 | SIRFSOC_SPI_TX_UFLOW_INT_EN | |
Qipan Li | f2a08b4 | 2014-09-02 17:01:03 +0800 | [diff] [blame] | 443 | SIRFSOC_SPI_RX_OFLOW_INT_EN | |
| 444 | SIRFSOC_SPI_RX_IO_DMA_INT_EN, |
Qipan Li | 41148c3 | 2014-05-04 14:32:36 +0800 | [diff] [blame] | 445 | sspi->base + SIRFSOC_SPI_INT_EN); |
| 446 | writel(SIRFSOC_SPI_RX_EN | SIRFSOC_SPI_TX_EN, |
Qipan Li | c908ef3 | 2014-04-15 15:24:59 +0800 | [diff] [blame] | 447 | sspi->base + SIRFSOC_SPI_TX_RX_EN); |
Qipan Li | 41148c3 | 2014-05-04 14:32:36 +0800 | [diff] [blame] | 448 | if (!wait_for_completion_timeout(&sspi->tx_done, timeout) || |
| 449 | !wait_for_completion_timeout(&sspi->rx_done, timeout)) { |
| 450 | dev_err(&spi->dev, "transfer timeout\n"); |
| 451 | break; |
| 452 | } |
| 453 | while (!((readl(sspi->base + SIRFSOC_SPI_RXFIFO_STATUS) |
| 454 | & SIRFSOC_SPI_FIFO_EMPTY)) && sspi->left_rx_word) |
| 455 | sspi->rx_word(sspi); |
| 456 | writel(0, sspi->base + SIRFSOC_SPI_RXFIFO_OP); |
| 457 | writel(0, sspi->base + SIRFSOC_SPI_TXFIFO_OP); |
| 458 | } while (sspi->left_tx_word != 0 || sspi->left_rx_word != 0); |
Qipan Li | c908ef3 | 2014-04-15 15:24:59 +0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | static int spi_sirfsoc_transfer(struct spi_device *spi, struct spi_transfer *t) |
| 462 | { |
| 463 | struct sirfsoc_spi *sspi; |
| 464 | sspi = spi_master_get_devdata(spi->master); |
| 465 | |
| 466 | sspi->tx = t->tx_buf ? t->tx_buf : sspi->dummypage; |
| 467 | sspi->rx = t->rx_buf ? t->rx_buf : sspi->dummypage; |
| 468 | sspi->left_tx_word = sspi->left_rx_word = t->len / sspi->word_width; |
| 469 | reinit_completion(&sspi->rx_done); |
| 470 | reinit_completion(&sspi->tx_done); |
| 471 | /* |
| 472 | * in the transfer, if transfer data using command register with rx_buf |
| 473 | * null, just fill command data into command register and wait for its |
| 474 | * completion. |
| 475 | */ |
| 476 | if (sspi->tx_by_cmd) |
| 477 | spi_sirfsoc_cmd_transfer(spi, t); |
| 478 | else if (IS_DMA_VALID(t)) |
| 479 | spi_sirfsoc_dma_transfer(spi, t); |
| 480 | else |
| 481 | spi_sirfsoc_pio_transfer(spi, t); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 482 | |
Qipan Li | 692fb0f | 2013-08-25 21:42:50 +0800 | [diff] [blame] | 483 | return t->len - sspi->left_rx_word * sspi->word_width; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | static void spi_sirfsoc_chipselect(struct spi_device *spi, int value) |
| 487 | { |
| 488 | struct sirfsoc_spi *sspi = spi_master_get_devdata(spi->master); |
| 489 | |
Qipan Li | 7850cdf | 2014-09-02 17:01:01 +0800 | [diff] [blame] | 490 | if (sspi->hw_cs) { |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 491 | u32 regval = readl(sspi->base + SIRFSOC_SPI_CTRL); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 492 | switch (value) { |
| 493 | case BITBANG_CS_ACTIVE: |
| 494 | if (spi->mode & SPI_CS_HIGH) |
| 495 | regval |= SIRFSOC_SPI_CS_IO_OUT; |
| 496 | else |
| 497 | regval &= ~SIRFSOC_SPI_CS_IO_OUT; |
| 498 | break; |
| 499 | case BITBANG_CS_INACTIVE: |
| 500 | if (spi->mode & SPI_CS_HIGH) |
| 501 | regval &= ~SIRFSOC_SPI_CS_IO_OUT; |
| 502 | else |
| 503 | regval |= SIRFSOC_SPI_CS_IO_OUT; |
| 504 | break; |
| 505 | } |
| 506 | writel(regval, sspi->base + SIRFSOC_SPI_CTRL); |
| 507 | } else { |
Qipan Li | 6ee8a2f | 2014-04-14 14:29:59 +0800 | [diff] [blame] | 508 | switch (value) { |
| 509 | case BITBANG_CS_ACTIVE: |
Qipan Li | 7850cdf | 2014-09-02 17:01:01 +0800 | [diff] [blame] | 510 | gpio_direction_output(spi->cs_gpio, |
Qipan Li | 6ee8a2f | 2014-04-14 14:29:59 +0800 | [diff] [blame] | 511 | spi->mode & SPI_CS_HIGH ? 1 : 0); |
| 512 | break; |
| 513 | case BITBANG_CS_INACTIVE: |
Qipan Li | 7850cdf | 2014-09-02 17:01:01 +0800 | [diff] [blame] | 514 | gpio_direction_output(spi->cs_gpio, |
Qipan Li | 6ee8a2f | 2014-04-14 14:29:59 +0800 | [diff] [blame] | 515 | spi->mode & SPI_CS_HIGH ? 0 : 1); |
| 516 | break; |
| 517 | } |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
| 521 | static int |
| 522 | spi_sirfsoc_setup_transfer(struct spi_device *spi, struct spi_transfer *t) |
| 523 | { |
| 524 | struct sirfsoc_spi *sspi; |
| 525 | u8 bits_per_word = 0; |
| 526 | int hz = 0; |
| 527 | u32 regval; |
| 528 | u32 txfifo_ctrl, rxfifo_ctrl; |
| 529 | u32 fifo_size = SIRFSOC_SPI_FIFO_SIZE / 4; |
| 530 | |
| 531 | sspi = spi_master_get_devdata(spi->master); |
| 532 | |
Laxman Dewangan | 766ed70 | 2012-12-18 14:25:43 +0530 | [diff] [blame] | 533 | bits_per_word = (t) ? t->bits_per_word : spi->bits_per_word; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 534 | hz = t && t->speed_hz ? t->speed_hz : spi->max_speed_hz; |
| 535 | |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 536 | regval = (sspi->ctrl_freq / (2 * hz)) - 1; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 537 | if (regval > 0xFFFF || regval < 0) { |
| 538 | dev_err(&spi->dev, "Speed %d not supported\n", hz); |
| 539 | return -EINVAL; |
| 540 | } |
| 541 | |
| 542 | switch (bits_per_word) { |
| 543 | case 8: |
| 544 | regval |= SIRFSOC_SPI_TRAN_DAT_FORMAT_8; |
| 545 | sspi->rx_word = spi_sirfsoc_rx_word_u8; |
| 546 | sspi->tx_word = spi_sirfsoc_tx_word_u8; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 547 | break; |
| 548 | case 12: |
| 549 | case 16: |
Qipan Li | d77ec5d | 2014-04-14 14:30:00 +0800 | [diff] [blame] | 550 | regval |= (bits_per_word == 12) ? |
| 551 | SIRFSOC_SPI_TRAN_DAT_FORMAT_12 : |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 552 | SIRFSOC_SPI_TRAN_DAT_FORMAT_16; |
| 553 | sspi->rx_word = spi_sirfsoc_rx_word_u16; |
| 554 | sspi->tx_word = spi_sirfsoc_tx_word_u16; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 555 | break; |
| 556 | case 32: |
| 557 | regval |= SIRFSOC_SPI_TRAN_DAT_FORMAT_32; |
| 558 | sspi->rx_word = spi_sirfsoc_rx_word_u32; |
| 559 | sspi->tx_word = spi_sirfsoc_tx_word_u32; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 560 | break; |
Arnd Bergmann | 804ae43 | 2013-06-03 15:24:53 +0200 | [diff] [blame] | 561 | default: |
| 562 | BUG(); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 563 | } |
| 564 | |
Axel Lin | 8c328a2 | 2014-01-15 17:07:43 +0800 | [diff] [blame] | 565 | sspi->word_width = DIV_ROUND_UP(bits_per_word, 8); |
| 566 | txfifo_ctrl = SIRFSOC_SPI_FIFO_THD(SIRFSOC_SPI_FIFO_SIZE / 2) | |
Qipan Li | 9c4b19a | 2014-11-17 23:17:02 +0800 | [diff] [blame] | 567 | (sspi->word_width >> 1); |
Axel Lin | 8c328a2 | 2014-01-15 17:07:43 +0800 | [diff] [blame] | 568 | rxfifo_ctrl = SIRFSOC_SPI_FIFO_THD(SIRFSOC_SPI_FIFO_SIZE / 2) | |
Qipan Li | 9c4b19a | 2014-11-17 23:17:02 +0800 | [diff] [blame] | 569 | (sspi->word_width >> 1); |
Axel Lin | 8c328a2 | 2014-01-15 17:07:43 +0800 | [diff] [blame] | 570 | |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 571 | if (!(spi->mode & SPI_CS_HIGH)) |
| 572 | regval |= SIRFSOC_SPI_CS_IDLE_STAT; |
| 573 | if (!(spi->mode & SPI_LSB_FIRST)) |
| 574 | regval |= SIRFSOC_SPI_TRAN_MSB; |
| 575 | if (spi->mode & SPI_CPOL) |
| 576 | regval |= SIRFSOC_SPI_CLK_IDLE_STAT; |
| 577 | |
| 578 | /* |
Qipan Li | d77ec5d | 2014-04-14 14:30:00 +0800 | [diff] [blame] | 579 | * Data should be driven at least 1/2 cycle before the fetch edge |
| 580 | * to make sure that data gets stable at the fetch edge. |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 581 | */ |
| 582 | if (((spi->mode & SPI_CPOL) && (spi->mode & SPI_CPHA)) || |
| 583 | (!(spi->mode & SPI_CPOL) && !(spi->mode & SPI_CPHA))) |
| 584 | regval &= ~SIRFSOC_SPI_DRV_POS_EDGE; |
| 585 | else |
| 586 | regval |= SIRFSOC_SPI_DRV_POS_EDGE; |
| 587 | |
| 588 | writel(SIRFSOC_SPI_FIFO_SC(fifo_size - 2) | |
| 589 | SIRFSOC_SPI_FIFO_LC(fifo_size / 2) | |
| 590 | SIRFSOC_SPI_FIFO_HC(2), |
| 591 | sspi->base + SIRFSOC_SPI_TXFIFO_LEVEL_CHK); |
| 592 | writel(SIRFSOC_SPI_FIFO_SC(2) | |
| 593 | SIRFSOC_SPI_FIFO_LC(fifo_size / 2) | |
| 594 | SIRFSOC_SPI_FIFO_HC(fifo_size - 2), |
| 595 | sspi->base + SIRFSOC_SPI_RXFIFO_LEVEL_CHK); |
| 596 | writel(txfifo_ctrl, sspi->base + SIRFSOC_SPI_TXFIFO_CTRL); |
| 597 | writel(rxfifo_ctrl, sspi->base + SIRFSOC_SPI_RXFIFO_CTRL); |
| 598 | |
Qipan Li | eeb71395 | 2014-03-01 12:38:17 +0800 | [diff] [blame] | 599 | if (t && t->tx_buf && !t->rx_buf && (t->len <= SIRFSOC_MAX_CMD_BYTES)) { |
| 600 | regval |= (SIRFSOC_SPI_CMD_BYTE_NUM((t->len - 1)) | |
| 601 | SIRFSOC_SPI_CMD_MODE); |
| 602 | sspi->tx_by_cmd = true; |
| 603 | } else { |
| 604 | regval &= ~SIRFSOC_SPI_CMD_MODE; |
| 605 | sspi->tx_by_cmd = false; |
| 606 | } |
Qipan Li | 625227a4 | 2014-04-14 14:29:58 +0800 | [diff] [blame] | 607 | /* |
Qipan Li | 7850cdf | 2014-09-02 17:01:01 +0800 | [diff] [blame] | 608 | * it should never set to hardware cs mode because in hardware cs mode, |
| 609 | * cs signal can't controlled by driver. |
Qipan Li | 625227a4 | 2014-04-14 14:29:58 +0800 | [diff] [blame] | 610 | */ |
| 611 | regval |= SIRFSOC_SPI_CS_IO_MODE; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 612 | writel(regval, sspi->base + SIRFSOC_SPI_CTRL); |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 613 | |
| 614 | if (IS_DMA_VALID(t)) { |
| 615 | /* Enable DMA mode for RX, TX */ |
| 616 | writel(0, sspi->base + SIRFSOC_SPI_TX_DMA_IO_CTRL); |
Qipan Li | d77ec5d | 2014-04-14 14:30:00 +0800 | [diff] [blame] | 617 | writel(SIRFSOC_SPI_RX_DMA_FLUSH, |
| 618 | sspi->base + SIRFSOC_SPI_RX_DMA_IO_CTRL); |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 619 | } else { |
| 620 | /* Enable IO mode for RX, TX */ |
Qipan Li | d77ec5d | 2014-04-14 14:30:00 +0800 | [diff] [blame] | 621 | writel(SIRFSOC_SPI_IO_MODE_SEL, |
| 622 | sspi->base + SIRFSOC_SPI_TX_DMA_IO_CTRL); |
| 623 | writel(SIRFSOC_SPI_IO_MODE_SEL, |
| 624 | sspi->base + SIRFSOC_SPI_RX_DMA_IO_CTRL); |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 625 | } |
| 626 | |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | static int spi_sirfsoc_setup(struct spi_device *spi) |
| 631 | { |
Qipan Li | 7850cdf | 2014-09-02 17:01:01 +0800 | [diff] [blame] | 632 | struct sirfsoc_spi *sspi; |
| 633 | |
Qipan Li | 7850cdf | 2014-09-02 17:01:01 +0800 | [diff] [blame] | 634 | sspi = spi_master_get_devdata(spi->master); |
| 635 | |
| 636 | if (spi->cs_gpio == -ENOENT) |
| 637 | sspi->hw_cs = true; |
| 638 | else |
| 639 | sspi->hw_cs = false; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 640 | return spi_sirfsoc_setup_transfer(spi, NULL); |
| 641 | } |
| 642 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 643 | static int spi_sirfsoc_probe(struct platform_device *pdev) |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 644 | { |
| 645 | struct sirfsoc_spi *sspi; |
| 646 | struct spi_master *master; |
| 647 | struct resource *mem_res; |
Qipan Li | 7850cdf | 2014-09-02 17:01:01 +0800 | [diff] [blame] | 648 | int irq; |
| 649 | int i, ret; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 650 | |
Qipan Li | 8509c55 | 2014-11-20 22:33:07 +0800 | [diff] [blame] | 651 | ret = device_reset(&pdev->dev); |
| 652 | if (ret) { |
| 653 | dev_err(&pdev->dev, "SPI reset failed!\n"); |
| 654 | return ret; |
| 655 | } |
| 656 | |
Qipan Li | 7850cdf | 2014-09-02 17:01:01 +0800 | [diff] [blame] | 657 | master = spi_alloc_master(&pdev->dev, sizeof(*sspi)); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 658 | if (!master) { |
| 659 | dev_err(&pdev->dev, "Unable to allocate SPI master\n"); |
| 660 | return -ENOMEM; |
| 661 | } |
| 662 | platform_set_drvdata(pdev, master); |
| 663 | sspi = spi_master_get_devdata(master); |
| 664 | |
Julia Lawall | 2479790 | 2013-08-14 11:11:29 +0200 | [diff] [blame] | 665 | mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Thierry Reding | b0ee560 | 2013-01-21 11:09:18 +0100 | [diff] [blame] | 666 | sspi->base = devm_ioremap_resource(&pdev->dev, mem_res); |
| 667 | if (IS_ERR(sspi->base)) { |
| 668 | ret = PTR_ERR(sspi->base); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 669 | goto free_master; |
| 670 | } |
| 671 | |
| 672 | irq = platform_get_irq(pdev, 0); |
| 673 | if (irq < 0) { |
| 674 | ret = -ENXIO; |
| 675 | goto free_master; |
| 676 | } |
| 677 | ret = devm_request_irq(&pdev->dev, irq, spi_sirfsoc_irq, 0, |
| 678 | DRIVER_NAME, sspi); |
| 679 | if (ret) |
| 680 | goto free_master; |
| 681 | |
Axel Lin | 94c69f7 | 2013-09-10 15:43:41 +0800 | [diff] [blame] | 682 | sspi->bitbang.master = master; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 683 | sspi->bitbang.chipselect = spi_sirfsoc_chipselect; |
| 684 | sspi->bitbang.setup_transfer = spi_sirfsoc_setup_transfer; |
| 685 | sspi->bitbang.txrx_bufs = spi_sirfsoc_transfer; |
| 686 | sspi->bitbang.master->setup = spi_sirfsoc_setup; |
| 687 | master->bus_num = pdev->id; |
Qipan Li | 94b1f0d | 2013-06-25 19:45:29 +0800 | [diff] [blame] | 688 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_CS_HIGH; |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 689 | master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(12) | |
| 690 | SPI_BPW_MASK(16) | SPI_BPW_MASK(32); |
Qipan Li | fcc50e5 | 2014-11-17 23:17:03 +0800 | [diff] [blame] | 691 | master->max_speed_hz = SIRFSOC_SPI_DEFAULT_FRQ; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 692 | sspi->bitbang.master->dev.of_node = pdev->dev.of_node; |
| 693 | |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 694 | /* request DMA channels */ |
Barry Song | dd7243d | 2014-02-13 00:30:19 +0800 | [diff] [blame] | 695 | sspi->rx_chan = dma_request_slave_channel(&pdev->dev, "rx"); |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 696 | if (!sspi->rx_chan) { |
| 697 | dev_err(&pdev->dev, "can not allocate rx dma channel\n"); |
Wei Yongjun | 6cca9e2 | 2013-08-23 08:33:39 +0800 | [diff] [blame] | 698 | ret = -ENODEV; |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 699 | goto free_master; |
| 700 | } |
Barry Song | dd7243d | 2014-02-13 00:30:19 +0800 | [diff] [blame] | 701 | sspi->tx_chan = dma_request_slave_channel(&pdev->dev, "tx"); |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 702 | if (!sspi->tx_chan) { |
| 703 | dev_err(&pdev->dev, "can not allocate tx dma channel\n"); |
Wei Yongjun | 6cca9e2 | 2013-08-23 08:33:39 +0800 | [diff] [blame] | 704 | ret = -ENODEV; |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 705 | goto free_rx_dma; |
| 706 | } |
| 707 | |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 708 | sspi->clk = clk_get(&pdev->dev, NULL); |
| 709 | if (IS_ERR(sspi->clk)) { |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 710 | ret = PTR_ERR(sspi->clk); |
| 711 | goto free_tx_dma; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 712 | } |
Barry Song | e5118cd | 2012-12-26 10:48:33 +0800 | [diff] [blame] | 713 | clk_prepare_enable(sspi->clk); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 714 | sspi->ctrl_freq = clk_get_rate(sspi->clk); |
| 715 | |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 716 | init_completion(&sspi->rx_done); |
| 717 | init_completion(&sspi->tx_done); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 718 | |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 719 | writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_RXFIFO_OP); |
| 720 | writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_TXFIFO_OP); |
| 721 | writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_RXFIFO_OP); |
| 722 | writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_TXFIFO_OP); |
| 723 | /* We are not using dummy delay between command and data */ |
| 724 | writel(0, sspi->base + SIRFSOC_SPI_DUMMY_DELAY_CTL); |
| 725 | |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 726 | sspi->dummypage = kmalloc(2 * PAGE_SIZE, GFP_KERNEL); |
Wei Yongjun | 6cca9e2 | 2013-08-23 08:33:39 +0800 | [diff] [blame] | 727 | if (!sspi->dummypage) { |
| 728 | ret = -ENOMEM; |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 729 | goto free_clk; |
Wei Yongjun | 6cca9e2 | 2013-08-23 08:33:39 +0800 | [diff] [blame] | 730 | } |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 731 | |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 732 | ret = spi_bitbang_start(&sspi->bitbang); |
| 733 | if (ret) |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 734 | goto free_dummypage; |
Qipan Li | 7850cdf | 2014-09-02 17:01:01 +0800 | [diff] [blame] | 735 | for (i = 0; master->cs_gpios && i < master->num_chipselect; i++) { |
| 736 | if (master->cs_gpios[i] == -ENOENT) |
| 737 | continue; |
| 738 | if (!gpio_is_valid(master->cs_gpios[i])) { |
| 739 | dev_err(&pdev->dev, "no valid gpio\n"); |
| 740 | ret = -EINVAL; |
| 741 | goto free_dummypage; |
| 742 | } |
| 743 | ret = devm_gpio_request(&pdev->dev, |
| 744 | master->cs_gpios[i], DRIVER_NAME); |
| 745 | if (ret) { |
| 746 | dev_err(&pdev->dev, "failed to request gpio\n"); |
| 747 | goto free_dummypage; |
| 748 | } |
| 749 | } |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 750 | dev_info(&pdev->dev, "registerred, bus number = %d\n", master->bus_num); |
| 751 | |
| 752 | return 0; |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 753 | free_dummypage: |
| 754 | kfree(sspi->dummypage); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 755 | free_clk: |
Barry Song | e5118cd | 2012-12-26 10:48:33 +0800 | [diff] [blame] | 756 | clk_disable_unprepare(sspi->clk); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 757 | clk_put(sspi->clk); |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 758 | free_tx_dma: |
| 759 | dma_release_channel(sspi->tx_chan); |
| 760 | free_rx_dma: |
| 761 | dma_release_channel(sspi->rx_chan); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 762 | free_master: |
| 763 | spi_master_put(master); |
Qipan Li | 7850cdf | 2014-09-02 17:01:01 +0800 | [diff] [blame] | 764 | |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 765 | return ret; |
| 766 | } |
| 767 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 768 | static int spi_sirfsoc_remove(struct platform_device *pdev) |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 769 | { |
| 770 | struct spi_master *master; |
| 771 | struct sirfsoc_spi *sspi; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 772 | |
| 773 | master = platform_get_drvdata(pdev); |
| 774 | sspi = spi_master_get_devdata(master); |
| 775 | |
| 776 | spi_bitbang_stop(&sspi->bitbang); |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 777 | kfree(sspi->dummypage); |
Barry Song | e5118cd | 2012-12-26 10:48:33 +0800 | [diff] [blame] | 778 | clk_disable_unprepare(sspi->clk); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 779 | clk_put(sspi->clk); |
Barry Song | de39f5f | 2013-08-06 14:21:21 +0800 | [diff] [blame] | 780 | dma_release_channel(sspi->rx_chan); |
| 781 | dma_release_channel(sspi->tx_chan); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 782 | spi_master_put(master); |
| 783 | return 0; |
| 784 | } |
| 785 | |
Qipan Li | facffed | 2014-02-13 00:30:20 +0800 | [diff] [blame] | 786 | #ifdef CONFIG_PM_SLEEP |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 787 | static int spi_sirfsoc_suspend(struct device *dev) |
| 788 | { |
Axel Lin | a1216394 | 2013-08-09 15:35:16 +0800 | [diff] [blame] | 789 | struct spi_master *master = dev_get_drvdata(dev); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 790 | struct sirfsoc_spi *sspi = spi_master_get_devdata(master); |
Axel Lin | a82ba3a | 2014-03-05 15:19:09 +0800 | [diff] [blame] | 791 | int ret; |
| 792 | |
| 793 | ret = spi_master_suspend(master); |
| 794 | if (ret) |
| 795 | return ret; |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 796 | |
| 797 | clk_disable(sspi->clk); |
| 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | static int spi_sirfsoc_resume(struct device *dev) |
| 802 | { |
Axel Lin | a1216394 | 2013-08-09 15:35:16 +0800 | [diff] [blame] | 803 | struct spi_master *master = dev_get_drvdata(dev); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 804 | struct sirfsoc_spi *sspi = spi_master_get_devdata(master); |
| 805 | |
| 806 | clk_enable(sspi->clk); |
| 807 | writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_RXFIFO_OP); |
| 808 | writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_TXFIFO_OP); |
| 809 | writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_RXFIFO_OP); |
| 810 | writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_TXFIFO_OP); |
| 811 | |
Axel Lin | a82ba3a | 2014-03-05 15:19:09 +0800 | [diff] [blame] | 812 | return spi_master_resume(master); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 813 | } |
Qipan Li | facffed | 2014-02-13 00:30:20 +0800 | [diff] [blame] | 814 | #endif |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 815 | |
Jingoo Han | 71aa2e3 | 2014-02-26 10:32:48 +0900 | [diff] [blame] | 816 | static SIMPLE_DEV_PM_OPS(spi_sirfsoc_pm_ops, spi_sirfsoc_suspend, |
| 817 | spi_sirfsoc_resume); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 818 | |
| 819 | static const struct of_device_id spi_sirfsoc_of_match[] = { |
| 820 | { .compatible = "sirf,prima2-spi", }, |
| 821 | {} |
| 822 | }; |
Arnd Bergmann | 3af4ed7 | 2013-04-23 18:30:41 +0200 | [diff] [blame] | 823 | MODULE_DEVICE_TABLE(of, spi_sirfsoc_of_match); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 824 | |
| 825 | static struct platform_driver spi_sirfsoc_driver = { |
| 826 | .driver = { |
| 827 | .name = DRIVER_NAME, |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 828 | .pm = &spi_sirfsoc_pm_ops, |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 829 | .of_match_table = spi_sirfsoc_of_match, |
| 830 | }, |
| 831 | .probe = spi_sirfsoc_probe, |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 832 | .remove = spi_sirfsoc_remove, |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 833 | }; |
| 834 | module_platform_driver(spi_sirfsoc_driver); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 835 | MODULE_DESCRIPTION("SiRF SoC SPI master driver"); |
Qipan Li | d77ec5d | 2014-04-14 14:30:00 +0800 | [diff] [blame] | 836 | MODULE_AUTHOR("Zhiwu Song <Zhiwu.Song@csr.com>"); |
| 837 | MODULE_AUTHOR("Barry Song <Baohua.Song@csr.com>"); |
Zhiwu Song | 1cc2df9 | 2012-02-13 17:45:38 +0800 | [diff] [blame] | 838 | MODULE_LICENSE("GPL v2"); |