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