blob: bbb1a275f718132b59caa77e03cb64a559f3f60d [file] [log] [blame]
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001/*
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 Lic908ef32014-04-15 15:24:59 +080013#include <linux/completion.h>
Zhiwu Song1cc2df92012-02-13 17:45:38 +080014#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 Songde39f5f2013-08-06 14:21:21 +080023#include <linux/dmaengine.h>
24#include <linux/dma-direction.h>
25#include <linux/dma-mapping.h>
Qipan Li8509c552014-11-20 22:33:07 +080026#include <linux/reset.h>
Zhiwu Song1cc2df92012-02-13 17:45:38 +080027
28#define DRIVER_NAME "sirfsoc_spi"
Zhiwu Song1cc2df92012-02-13 17:45:38 +080029/* SPI CTRL register defines */
30#define SIRFSOC_SPI_SLV_MODE BIT(16)
31#define SIRFSOC_SPI_CMD_MODE BIT(17)
32#define SIRFSOC_SPI_CS_IO_OUT BIT(18)
33#define SIRFSOC_SPI_CS_IO_MODE BIT(19)
34#define SIRFSOC_SPI_CLK_IDLE_STAT BIT(20)
35#define SIRFSOC_SPI_CS_IDLE_STAT BIT(21)
36#define SIRFSOC_SPI_TRAN_MSB BIT(22)
37#define SIRFSOC_SPI_DRV_POS_EDGE BIT(23)
38#define SIRFSOC_SPI_CS_HOLD_TIME BIT(24)
39#define SIRFSOC_SPI_CLK_SAMPLE_MODE BIT(25)
40#define SIRFSOC_SPI_TRAN_DAT_FORMAT_8 (0 << 26)
41#define SIRFSOC_SPI_TRAN_DAT_FORMAT_12 (1 << 26)
42#define SIRFSOC_SPI_TRAN_DAT_FORMAT_16 (2 << 26)
43#define SIRFSOC_SPI_TRAN_DAT_FORMAT_32 (3 << 26)
Qipan Li9593e612014-09-02 17:02:36 +080044#define SIRFSOC_SPI_CMD_BYTE_NUM(x) ((x & 3) << 28)
45#define SIRFSOC_SPI_ENA_AUTO_CLR BIT(30)
46#define SIRFSOC_SPI_MUL_DAT_MODE BIT(31)
Zhiwu Song1cc2df92012-02-13 17:45:38 +080047
48/* Interrupt Enable */
Qipan Li9593e612014-09-02 17:02:36 +080049#define SIRFSOC_SPI_RX_DONE_INT_EN BIT(0)
50#define SIRFSOC_SPI_TX_DONE_INT_EN BIT(1)
51#define SIRFSOC_SPI_RX_OFLOW_INT_EN BIT(2)
52#define SIRFSOC_SPI_TX_UFLOW_INT_EN BIT(3)
Zhiwu Song1cc2df92012-02-13 17:45:38 +080053#define SIRFSOC_SPI_RX_IO_DMA_INT_EN BIT(4)
54#define SIRFSOC_SPI_TX_IO_DMA_INT_EN BIT(5)
55#define SIRFSOC_SPI_RXFIFO_FULL_INT_EN BIT(6)
56#define SIRFSOC_SPI_TXFIFO_EMPTY_INT_EN BIT(7)
57#define SIRFSOC_SPI_RXFIFO_THD_INT_EN BIT(8)
58#define SIRFSOC_SPI_TXFIFO_THD_INT_EN BIT(9)
59#define SIRFSOC_SPI_FRM_END_INT_EN BIT(10)
60
Zhiwu Song1cc2df92012-02-13 17:45:38 +080061/* Interrupt status */
62#define SIRFSOC_SPI_RX_DONE BIT(0)
63#define SIRFSOC_SPI_TX_DONE BIT(1)
64#define SIRFSOC_SPI_RX_OFLOW BIT(2)
65#define SIRFSOC_SPI_TX_UFLOW BIT(3)
Qipan Li41148c32014-05-04 14:32:36 +080066#define SIRFSOC_SPI_RX_IO_DMA BIT(4)
Zhiwu Song1cc2df92012-02-13 17:45:38 +080067#define SIRFSOC_SPI_RX_FIFO_FULL BIT(6)
68#define SIRFSOC_SPI_TXFIFO_EMPTY BIT(7)
69#define SIRFSOC_SPI_RXFIFO_THD_REACH BIT(8)
70#define SIRFSOC_SPI_TXFIFO_THD_REACH BIT(9)
71#define SIRFSOC_SPI_FRM_END BIT(10)
72
73/* TX RX enable */
74#define SIRFSOC_SPI_RX_EN BIT(0)
75#define SIRFSOC_SPI_TX_EN BIT(1)
76#define SIRFSOC_SPI_CMD_TX_EN BIT(2)
77
78#define SIRFSOC_SPI_IO_MODE_SEL BIT(0)
79#define SIRFSOC_SPI_RX_DMA_FLUSH BIT(2)
80
81/* FIFO OPs */
82#define SIRFSOC_SPI_FIFO_RESET BIT(0)
83#define SIRFSOC_SPI_FIFO_START BIT(1)
84
85/* FIFO CTRL */
86#define SIRFSOC_SPI_FIFO_WIDTH_BYTE (0 << 0)
87#define SIRFSOC_SPI_FIFO_WIDTH_WORD (1 << 0)
88#define SIRFSOC_SPI_FIFO_WIDTH_DWORD (2 << 0)
Qipan Lie3fb57c2015-05-19 14:41:12 +000089/* USP related */
90#define SIRFSOC_USP_SYNC_MODE BIT(0)
91#define SIRFSOC_USP_SLV_MODE BIT(1)
92#define SIRFSOC_USP_LSB BIT(4)
93#define SIRFSOC_USP_EN BIT(5)
94#define SIRFSOC_USP_RXD_FALLING_EDGE BIT(6)
95#define SIRFSOC_USP_TXD_FALLING_EDGE BIT(7)
96#define SIRFSOC_USP_CS_HIGH_VALID BIT(9)
97#define SIRFSOC_USP_SCLK_IDLE_STAT BIT(11)
98#define SIRFSOC_USP_TFS_IO_MODE BIT(14)
99#define SIRFSOC_USP_TFS_IO_INPUT BIT(19)
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800100
Qipan Lie3fb57c2015-05-19 14:41:12 +0000101#define SIRFSOC_USP_RXD_DELAY_LEN_MASK 0xFF
102#define SIRFSOC_USP_TXD_DELAY_LEN_MASK 0xFF
103#define SIRFSOC_USP_RXD_DELAY_OFFSET 0
104#define SIRFSOC_USP_TXD_DELAY_OFFSET 8
105#define SIRFSOC_USP_RXD_DELAY_LEN 1
106#define SIRFSOC_USP_TXD_DELAY_LEN 1
107#define SIRFSOC_USP_CLK_DIVISOR_OFFSET 21
108#define SIRFSOC_USP_CLK_DIVISOR_MASK 0x3FF
109#define SIRFSOC_USP_CLK_10_11_MASK 0x3
110#define SIRFSOC_USP_CLK_10_11_OFFSET 30
111#define SIRFSOC_USP_CLK_12_15_MASK 0xF
112#define SIRFSOC_USP_CLK_12_15_OFFSET 24
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800113
Qipan Lie3fb57c2015-05-19 14:41:12 +0000114#define SIRFSOC_USP_TX_DATA_OFFSET 0
115#define SIRFSOC_USP_TX_SYNC_OFFSET 8
116#define SIRFSOC_USP_TX_FRAME_OFFSET 16
117#define SIRFSOC_USP_TX_SHIFTER_OFFSET 24
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800118
Qipan Lie3fb57c2015-05-19 14:41:12 +0000119#define SIRFSOC_USP_TX_DATA_MASK 0xFF
120#define SIRFSOC_USP_TX_SYNC_MASK 0xFF
121#define SIRFSOC_USP_TX_FRAME_MASK 0xFF
122#define SIRFSOC_USP_TX_SHIFTER_MASK 0x1F
123
124#define SIRFSOC_USP_RX_DATA_OFFSET 0
125#define SIRFSOC_USP_RX_FRAME_OFFSET 8
126#define SIRFSOC_USP_RX_SHIFTER_OFFSET 16
127
128#define SIRFSOC_USP_RX_DATA_MASK 0xFF
129#define SIRFSOC_USP_RX_FRAME_MASK 0xFF
130#define SIRFSOC_USP_RX_SHIFTER_MASK 0x1F
131#define SIRFSOC_USP_CS_HIGH_VALUE BIT(1)
132
133#define SIRFSOC_SPI_FIFO_SC_OFFSET 0
134#define SIRFSOC_SPI_FIFO_LC_OFFSET 10
135#define SIRFSOC_SPI_FIFO_HC_OFFSET 20
136
137#define SIRFSOC_SPI_FIFO_FULL_MASK(s) (1 << ((s)->fifo_full_offset))
138#define SIRFSOC_SPI_FIFO_EMPTY_MASK(s) (1 << ((s)->fifo_full_offset + 1))
139#define SIRFSOC_SPI_FIFO_THD_MASK(s) ((s)->fifo_size - 1)
140#define SIRFSOC_SPI_FIFO_THD_OFFSET 2
141#define SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(s, val) \
142 ((val) & (s)->fifo_level_chk_mask)
143
144enum sirf_spi_type {
145 SIRF_REAL_SPI,
146 SIRF_USP_SPI_P2,
147 SIRF_USP_SPI_A7,
148};
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800149
Barry Songde39f5f2013-08-06 14:21:21 +0800150/*
151 * only if the rx/tx buffer and transfer size are 4-bytes aligned, we use dma
152 * due to the limitation of dma controller
153 */
154
155#define ALIGNED(x) (!((u32)x & 0x3))
156#define IS_DMA_VALID(x) (x && ALIGNED(x->tx_buf) && ALIGNED(x->rx_buf) && \
Qipan Li692fb0f2013-08-25 21:42:50 +0800157 ALIGNED(x->len) && (x->len < 2 * PAGE_SIZE))
Barry Songde39f5f2013-08-06 14:21:21 +0800158
Qipan Lieeb713952014-03-01 12:38:17 +0800159#define SIRFSOC_MAX_CMD_BYTES 4
Qipan Lifcc50e52014-11-17 23:17:03 +0800160#define SIRFSOC_SPI_DEFAULT_FRQ 1000000
Qipan Lieeb713952014-03-01 12:38:17 +0800161
Qipan Lie3fb57c2015-05-19 14:41:12 +0000162struct sirf_spi_register {
163 /*SPI and USP-SPI common*/
164 u32 tx_rx_en;
165 u32 int_en;
166 u32 int_st;
167 u32 tx_dma_io_ctrl;
168 u32 tx_dma_io_len;
169 u32 txfifo_ctrl;
170 u32 txfifo_level_chk;
171 u32 txfifo_op;
172 u32 txfifo_st;
173 u32 txfifo_data;
174 u32 rx_dma_io_ctrl;
175 u32 rx_dma_io_len;
176 u32 rxfifo_ctrl;
177 u32 rxfifo_level_chk;
178 u32 rxfifo_op;
179 u32 rxfifo_st;
180 u32 rxfifo_data;
181 /*SPI self*/
182 u32 spi_ctrl;
183 u32 spi_cmd;
184 u32 spi_dummy_delay_ctrl;
185 /*USP-SPI self*/
186 u32 usp_mode1;
187 u32 usp_mode2;
188 u32 usp_tx_frame_ctrl;
189 u32 usp_rx_frame_ctrl;
190 u32 usp_pin_io_data;
191 u32 usp_risc_dsp_mode;
192 u32 usp_async_param_reg;
193 u32 usp_irda_x_mode_div;
194 u32 usp_sm_cfg;
195 u32 usp_int_en_clr;
196};
197
198static const struct sirf_spi_register real_spi_register = {
199 .tx_rx_en = 0x8,
200 .int_en = 0xc,
201 .int_st = 0x10,
202 .tx_dma_io_ctrl = 0x100,
203 .tx_dma_io_len = 0x104,
204 .txfifo_ctrl = 0x108,
205 .txfifo_level_chk = 0x10c,
206 .txfifo_op = 0x110,
207 .txfifo_st = 0x114,
208 .txfifo_data = 0x118,
209 .rx_dma_io_ctrl = 0x120,
210 .rx_dma_io_len = 0x124,
211 .rxfifo_ctrl = 0x128,
212 .rxfifo_level_chk = 0x12c,
213 .rxfifo_op = 0x130,
214 .rxfifo_st = 0x134,
215 .rxfifo_data = 0x138,
216 .spi_ctrl = 0x0,
217 .spi_cmd = 0x4,
218 .spi_dummy_delay_ctrl = 0x144,
219};
220
221static const struct sirf_spi_register usp_spi_register = {
222 .tx_rx_en = 0x10,
223 .int_en = 0x14,
224 .int_st = 0x18,
225 .tx_dma_io_ctrl = 0x100,
226 .tx_dma_io_len = 0x104,
227 .txfifo_ctrl = 0x108,
228 .txfifo_level_chk = 0x10c,
229 .txfifo_op = 0x110,
230 .txfifo_st = 0x114,
231 .txfifo_data = 0x118,
232 .rx_dma_io_ctrl = 0x120,
233 .rx_dma_io_len = 0x124,
234 .rxfifo_ctrl = 0x128,
235 .rxfifo_level_chk = 0x12c,
236 .rxfifo_op = 0x130,
237 .rxfifo_st = 0x134,
238 .rxfifo_data = 0x138,
239 .usp_mode1 = 0x0,
240 .usp_mode2 = 0x4,
241 .usp_tx_frame_ctrl = 0x8,
242 .usp_rx_frame_ctrl = 0xc,
243 .usp_pin_io_data = 0x1c,
244 .usp_risc_dsp_mode = 0x20,
245 .usp_async_param_reg = 0x24,
246 .usp_irda_x_mode_div = 0x28,
247 .usp_sm_cfg = 0x2c,
248 .usp_int_en_clr = 0x140,
249};
250
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800251struct sirfsoc_spi {
252 struct spi_bitbang bitbang;
Barry Songde39f5f2013-08-06 14:21:21 +0800253 struct completion rx_done;
254 struct completion tx_done;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800255
256 void __iomem *base;
257 u32 ctrl_freq; /* SPI controller clock speed */
258 struct clk *clk;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800259
260 /* rx & tx bufs from the spi_transfer */
261 const void *tx;
262 void *rx;
263
264 /* place received word into rx buffer */
265 void (*rx_word) (struct sirfsoc_spi *);
266 /* get word from tx buffer for sending */
267 void (*tx_word) (struct sirfsoc_spi *);
268
269 /* number of words left to be tranmitted/received */
Qipan Li692fb0f2013-08-25 21:42:50 +0800270 unsigned int left_tx_word;
271 unsigned int left_rx_word;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800272
Barry Songde39f5f2013-08-06 14:21:21 +0800273 /* rx & tx DMA channels */
274 struct dma_chan *rx_chan;
275 struct dma_chan *tx_chan;
276 dma_addr_t src_start;
277 dma_addr_t dst_start;
Barry Songde39f5f2013-08-06 14:21:21 +0800278 int word_width; /* in bytes */
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800279
Qipan Lieeb713952014-03-01 12:38:17 +0800280 /*
281 * if tx size is not more than 4 and rx size is NULL, use
282 * command model
283 */
284 bool tx_by_cmd;
Qipan Li7850cdf2014-09-02 17:01:01 +0800285 bool hw_cs;
Qipan Lie3fb57c2015-05-19 14:41:12 +0000286 enum sirf_spi_type type;
287 const struct sirf_spi_register *regs;
288 unsigned int fifo_size;
289 /* fifo empty offset is (fifo full offset + 1)*/
290 unsigned int fifo_full_offset;
291 /* fifo_level_chk_mask is (fifo_size/4 - 1) */
292 unsigned int fifo_level_chk_mask;
293 unsigned int dat_max_frm_len;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800294};
295
Qipan Li5bcc3b02015-05-26 05:21:34 +0000296struct sirf_spi_comp_data {
297 const struct sirf_spi_register *regs;
298 enum sirf_spi_type type;
299 unsigned int dat_max_frm_len;
300 unsigned int fifo_size;
301 void (*hwinit)(struct sirfsoc_spi *sspi);
302};
303
304static void sirfsoc_usp_hwinit(struct sirfsoc_spi *sspi)
305{
306 /* reset USP and let USP can operate */
307 writel(readl(sspi->base + sspi->regs->usp_mode1) &
308 ~SIRFSOC_USP_EN, sspi->base + sspi->regs->usp_mode1);
309 writel(readl(sspi->base + sspi->regs->usp_mode1) |
310 SIRFSOC_USP_EN, sspi->base + sspi->regs->usp_mode1);
311}
312
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800313static void spi_sirfsoc_rx_word_u8(struct sirfsoc_spi *sspi)
314{
315 u32 data;
316 u8 *rx = sspi->rx;
317
Qipan Lie3fb57c2015-05-19 14:41:12 +0000318 data = readl(sspi->base + sspi->regs->rxfifo_data);
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800319
320 if (rx) {
321 *rx++ = (u8) data;
322 sspi->rx = rx;
323 }
324
Qipan Li692fb0f2013-08-25 21:42:50 +0800325 sspi->left_rx_word--;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800326}
327
328static void spi_sirfsoc_tx_word_u8(struct sirfsoc_spi *sspi)
329{
330 u32 data = 0;
331 const u8 *tx = sspi->tx;
332
333 if (tx) {
334 data = *tx++;
335 sspi->tx = tx;
336 }
Qipan Lie3fb57c2015-05-19 14:41:12 +0000337 writel(data, sspi->base + sspi->regs->txfifo_data);
Qipan Li692fb0f2013-08-25 21:42:50 +0800338 sspi->left_tx_word--;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800339}
340
341static void spi_sirfsoc_rx_word_u16(struct sirfsoc_spi *sspi)
342{
343 u32 data;
344 u16 *rx = sspi->rx;
345
Qipan Lie3fb57c2015-05-19 14:41:12 +0000346 data = readl(sspi->base + sspi->regs->rxfifo_data);
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800347
348 if (rx) {
349 *rx++ = (u16) data;
350 sspi->rx = rx;
351 }
352
Qipan Li692fb0f2013-08-25 21:42:50 +0800353 sspi->left_rx_word--;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800354}
355
356static void spi_sirfsoc_tx_word_u16(struct sirfsoc_spi *sspi)
357{
358 u32 data = 0;
359 const u16 *tx = sspi->tx;
360
361 if (tx) {
362 data = *tx++;
363 sspi->tx = tx;
364 }
365
Qipan Lie3fb57c2015-05-19 14:41:12 +0000366 writel(data, sspi->base + sspi->regs->txfifo_data);
Qipan Li692fb0f2013-08-25 21:42:50 +0800367 sspi->left_tx_word--;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800368}
369
370static void spi_sirfsoc_rx_word_u32(struct sirfsoc_spi *sspi)
371{
372 u32 data;
373 u32 *rx = sspi->rx;
374
Qipan Lie3fb57c2015-05-19 14:41:12 +0000375 data = readl(sspi->base + sspi->regs->rxfifo_data);
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800376
377 if (rx) {
378 *rx++ = (u32) data;
379 sspi->rx = rx;
380 }
381
Qipan Li692fb0f2013-08-25 21:42:50 +0800382 sspi->left_rx_word--;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800383
384}
385
386static void spi_sirfsoc_tx_word_u32(struct sirfsoc_spi *sspi)
387{
388 u32 data = 0;
389 const u32 *tx = sspi->tx;
390
391 if (tx) {
392 data = *tx++;
393 sspi->tx = tx;
394 }
395
Qipan Lie3fb57c2015-05-19 14:41:12 +0000396 writel(data, sspi->base + sspi->regs->txfifo_data);
Qipan Li692fb0f2013-08-25 21:42:50 +0800397 sspi->left_tx_word--;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800398}
399
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800400static irqreturn_t spi_sirfsoc_irq(int irq, void *dev_id)
401{
402 struct sirfsoc_spi *sspi = dev_id;
Qipan Lie3fb57c2015-05-19 14:41:12 +0000403 u32 spi_stat;
404
405 spi_stat = readl(sspi->base + sspi->regs->int_st);
406 if (sspi->tx_by_cmd && sspi->type == SIRF_REAL_SPI
407 && (spi_stat & SIRFSOC_SPI_FRM_END)) {
Qipan Lieeb713952014-03-01 12:38:17 +0800408 complete(&sspi->tx_done);
Qipan Lie3fb57c2015-05-19 14:41:12 +0000409 writel(0x0, sspi->base + sspi->regs->int_en);
410 writel(readl(sspi->base + sspi->regs->int_st),
411 sspi->base + sspi->regs->int_st);
Qipan Lieeb713952014-03-01 12:38:17 +0800412 return IRQ_HANDLED;
413 }
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800414 /* Error Conditions */
415 if (spi_stat & SIRFSOC_SPI_RX_OFLOW ||
416 spi_stat & SIRFSOC_SPI_TX_UFLOW) {
Qipan Li41148c32014-05-04 14:32:36 +0800417 complete(&sspi->tx_done);
Barry Songde39f5f2013-08-06 14:21:21 +0800418 complete(&sspi->rx_done);
Qipan Lie3fb57c2015-05-19 14:41:12 +0000419 switch (sspi->type) {
420 case SIRF_REAL_SPI:
421 case SIRF_USP_SPI_P2:
422 writel(0x0, sspi->base + sspi->regs->int_en);
423 break;
424 case SIRF_USP_SPI_A7:
425 writel(~0UL, sspi->base + sspi->regs->usp_int_en_clr);
426 break;
427 }
428 writel(readl(sspi->base + sspi->regs->int_st),
429 sspi->base + sspi->regs->int_st);
Qipan Li41148c32014-05-04 14:32:36 +0800430 return IRQ_HANDLED;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800431 }
Qipan Li41148c32014-05-04 14:32:36 +0800432 if (spi_stat & SIRFSOC_SPI_TXFIFO_EMPTY)
433 complete(&sspi->tx_done);
Qipan Lie3fb57c2015-05-19 14:41:12 +0000434 while (!(readl(sspi->base + sspi->regs->int_st) &
Qipan Li41148c32014-05-04 14:32:36 +0800435 SIRFSOC_SPI_RX_IO_DMA))
436 cpu_relax();
437 complete(&sspi->rx_done);
Qipan Lie3fb57c2015-05-19 14:41:12 +0000438 switch (sspi->type) {
439 case SIRF_REAL_SPI:
440 case SIRF_USP_SPI_P2:
441 writel(0x0, sspi->base + sspi->regs->int_en);
442 break;
443 case SIRF_USP_SPI_A7:
444 writel(~0UL, sspi->base + sspi->regs->usp_int_en_clr);
445 break;
446 }
447 writel(readl(sspi->base + sspi->regs->int_st),
448 sspi->base + sspi->regs->int_st);
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800449
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800450 return IRQ_HANDLED;
451}
452
Barry Songde39f5f2013-08-06 14:21:21 +0800453static void spi_sirfsoc_dma_fini_callback(void *data)
454{
455 struct completion *dma_complete = data;
456
457 complete(dma_complete);
458}
459
Qipan Li0021d972014-09-02 17:01:04 +0800460static void spi_sirfsoc_cmd_transfer(struct spi_device *spi,
Qipan Lic908ef32014-04-15 15:24:59 +0800461 struct spi_transfer *t)
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800462{
463 struct sirfsoc_spi *sspi;
464 int timeout = t->len * 10;
Qipan Lic908ef32014-04-15 15:24:59 +0800465 u32 cmd;
466
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800467 sspi = spi_master_get_devdata(spi->master);
Qipan Lie3fb57c2015-05-19 14:41:12 +0000468 writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + sspi->regs->txfifo_op);
469 writel(SIRFSOC_SPI_FIFO_START, sspi->base + sspi->regs->txfifo_op);
Qipan Lic908ef32014-04-15 15:24:59 +0800470 memcpy(&cmd, sspi->tx, t->len);
471 if (sspi->word_width == 1 && !(spi->mode & SPI_LSB_FIRST))
472 cmd = cpu_to_be32(cmd) >>
473 ((SIRFSOC_MAX_CMD_BYTES - t->len) * 8);
474 if (sspi->word_width == 2 && t->len == 4 &&
475 (!(spi->mode & SPI_LSB_FIRST)))
476 cmd = ((cmd & 0xffff) << 16) | (cmd >> 16);
Qipan Lie3fb57c2015-05-19 14:41:12 +0000477 writel(cmd, sspi->base + sspi->regs->spi_cmd);
Qipan Lic908ef32014-04-15 15:24:59 +0800478 writel(SIRFSOC_SPI_FRM_END_INT_EN,
Qipan Lie3fb57c2015-05-19 14:41:12 +0000479 sspi->base + sspi->regs->int_en);
Qipan Lic908ef32014-04-15 15:24:59 +0800480 writel(SIRFSOC_SPI_CMD_TX_EN,
Qipan Lie3fb57c2015-05-19 14:41:12 +0000481 sspi->base + sspi->regs->tx_rx_en);
Qipan Lic908ef32014-04-15 15:24:59 +0800482 if (wait_for_completion_timeout(&sspi->tx_done, timeout) == 0) {
483 dev_err(&spi->dev, "cmd transfer timeout\n");
Qipan Li0021d972014-09-02 17:01:04 +0800484 return;
Qipan Lieeb713952014-03-01 12:38:17 +0800485 }
Qipan Li0021d972014-09-02 17:01:04 +0800486 sspi->left_rx_word -= t->len;
Qipan Lic908ef32014-04-15 15:24:59 +0800487}
488
489static void spi_sirfsoc_dma_transfer(struct spi_device *spi,
490 struct spi_transfer *t)
491{
492 struct sirfsoc_spi *sspi;
493 struct dma_async_tx_descriptor *rx_desc, *tx_desc;
494 int timeout = t->len * 10;
495
496 sspi = spi_master_get_devdata(spi->master);
Qipan Lie3fb57c2015-05-19 14:41:12 +0000497 writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + sspi->regs->rxfifo_op);
498 writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + sspi->regs->txfifo_op);
499 switch (sspi->type) {
500 case SIRF_REAL_SPI:
501 writel(SIRFSOC_SPI_FIFO_START,
502 sspi->base + sspi->regs->rxfifo_op);
503 writel(SIRFSOC_SPI_FIFO_START,
504 sspi->base + sspi->regs->txfifo_op);
505 writel(0, sspi->base + sspi->regs->int_en);
506 break;
507 case SIRF_USP_SPI_P2:
508 writel(0x0, sspi->base + sspi->regs->rxfifo_op);
509 writel(0x0, sspi->base + sspi->regs->txfifo_op);
510 writel(0, sspi->base + sspi->regs->int_en);
511 break;
512 case SIRF_USP_SPI_A7:
513 writel(0x0, sspi->base + sspi->regs->rxfifo_op);
514 writel(0x0, sspi->base + sspi->regs->txfifo_op);
515 writel(~0UL, sspi->base + sspi->regs->usp_int_en_clr);
516 break;
517 }
518 writel(readl(sspi->base + sspi->regs->int_st),
519 sspi->base + sspi->regs->int_st);
520 if (sspi->left_tx_word < sspi->dat_max_frm_len) {
521 switch (sspi->type) {
522 case SIRF_REAL_SPI:
523 writel(readl(sspi->base + sspi->regs->spi_ctrl) |
524 SIRFSOC_SPI_ENA_AUTO_CLR |
525 SIRFSOC_SPI_MUL_DAT_MODE,
526 sspi->base + sspi->regs->spi_ctrl);
527 writel(sspi->left_tx_word - 1,
528 sspi->base + sspi->regs->tx_dma_io_len);
529 writel(sspi->left_tx_word - 1,
530 sspi->base + sspi->regs->rx_dma_io_len);
531 break;
532 case SIRF_USP_SPI_P2:
533 case SIRF_USP_SPI_A7:
534 /*USP simulate SPI, tx/rx_dma_io_len indicates bytes*/
535 writel(sspi->left_tx_word * sspi->word_width,
536 sspi->base + sspi->regs->tx_dma_io_len);
537 writel(sspi->left_tx_word * sspi->word_width,
538 sspi->base + sspi->regs->rx_dma_io_len);
539 break;
540 }
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800541 } else {
Qipan Lie3fb57c2015-05-19 14:41:12 +0000542 if (sspi->type == SIRF_REAL_SPI)
543 writel(readl(sspi->base + sspi->regs->spi_ctrl),
544 sspi->base + sspi->regs->spi_ctrl);
545 writel(0, sspi->base + sspi->regs->tx_dma_io_len);
546 writel(0, sspi->base + sspi->regs->rx_dma_io_len);
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800547 }
Qipan Lic908ef32014-04-15 15:24:59 +0800548 sspi->dst_start = dma_map_single(&spi->dev, sspi->rx, t->len,
549 (t->tx_buf != t->rx_buf) ?
550 DMA_FROM_DEVICE : DMA_BIDIRECTIONAL);
551 rx_desc = dmaengine_prep_slave_single(sspi->rx_chan,
552 sspi->dst_start, t->len, DMA_DEV_TO_MEM,
553 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
554 rx_desc->callback = spi_sirfsoc_dma_fini_callback;
555 rx_desc->callback_param = &sspi->rx_done;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800556
Qipan Lic908ef32014-04-15 15:24:59 +0800557 sspi->src_start = dma_map_single(&spi->dev, (void *)sspi->tx, t->len,
558 (t->tx_buf != t->rx_buf) ?
559 DMA_TO_DEVICE : DMA_BIDIRECTIONAL);
560 tx_desc = dmaengine_prep_slave_single(sspi->tx_chan,
561 sspi->src_start, t->len, DMA_MEM_TO_DEV,
562 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
563 tx_desc->callback = spi_sirfsoc_dma_fini_callback;
564 tx_desc->callback_param = &sspi->tx_done;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800565
Qipan Lic908ef32014-04-15 15:24:59 +0800566 dmaengine_submit(tx_desc);
567 dmaengine_submit(rx_desc);
568 dma_async_issue_pending(sspi->tx_chan);
569 dma_async_issue_pending(sspi->rx_chan);
Qipan Lid77ec5d2014-04-14 14:30:00 +0800570 writel(SIRFSOC_SPI_RX_EN | SIRFSOC_SPI_TX_EN,
Qipan Lie3fb57c2015-05-19 14:41:12 +0000571 sspi->base + sspi->regs->tx_rx_en);
572 if (sspi->type == SIRF_USP_SPI_P2 ||
573 sspi->type == SIRF_USP_SPI_A7) {
574 writel(SIRFSOC_SPI_FIFO_START,
575 sspi->base + sspi->regs->rxfifo_op);
576 writel(SIRFSOC_SPI_FIFO_START,
577 sspi->base + sspi->regs->txfifo_op);
578 }
Qipan Lic908ef32014-04-15 15:24:59 +0800579 if (wait_for_completion_timeout(&sspi->rx_done, timeout) == 0) {
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800580 dev_err(&spi->dev, "transfer timeout\n");
Barry Songde39f5f2013-08-06 14:21:21 +0800581 dmaengine_terminate_all(sspi->rx_chan);
582 } else
Qipan Li692fb0f2013-08-25 21:42:50 +0800583 sspi->left_rx_word = 0;
Barry Songde39f5f2013-08-06 14:21:21 +0800584 /*
585 * we only wait tx-done event if transferring by DMA. for PIO,
586 * we get rx data by writing tx data, so if rx is done, tx has
587 * done earlier
588 */
Qipan Lic908ef32014-04-15 15:24:59 +0800589 if (wait_for_completion_timeout(&sspi->tx_done, timeout) == 0) {
590 dev_err(&spi->dev, "transfer timeout\n");
Qipan Lie3fb57c2015-05-19 14:41:12 +0000591 if (sspi->type == SIRF_USP_SPI_P2 ||
592 sspi->type == SIRF_USP_SPI_A7)
593 writel(0, sspi->base + sspi->regs->tx_rx_en);
Qipan Lic908ef32014-04-15 15:24:59 +0800594 dmaengine_terminate_all(sspi->tx_chan);
Barry Songde39f5f2013-08-06 14:21:21 +0800595 }
Qipan Lic908ef32014-04-15 15:24:59 +0800596 dma_unmap_single(&spi->dev, sspi->src_start, t->len, DMA_TO_DEVICE);
597 dma_unmap_single(&spi->dev, sspi->dst_start, t->len, DMA_FROM_DEVICE);
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800598 /* TX, RX FIFO stop */
Qipan Lie3fb57c2015-05-19 14:41:12 +0000599 writel(0, sspi->base + sspi->regs->rxfifo_op);
600 writel(0, sspi->base + sspi->regs->txfifo_op);
601 if (sspi->left_tx_word >= sspi->dat_max_frm_len)
602 writel(0, sspi->base + sspi->regs->tx_rx_en);
603 if (sspi->type == SIRF_USP_SPI_P2 ||
604 sspi->type == SIRF_USP_SPI_A7)
605 writel(0, sspi->base + sspi->regs->tx_rx_en);
Qipan Lic908ef32014-04-15 15:24:59 +0800606}
607
608static void spi_sirfsoc_pio_transfer(struct spi_device *spi,
609 struct spi_transfer *t)
610{
611 struct sirfsoc_spi *sspi;
612 int timeout = t->len * 10;
Qipan Lie3fb57c2015-05-19 14:41:12 +0000613 unsigned int data_units;
Qipan Lic908ef32014-04-15 15:24:59 +0800614
615 sspi = spi_master_get_devdata(spi->master);
Qipan Li41148c32014-05-04 14:32:36 +0800616 do {
617 writel(SIRFSOC_SPI_FIFO_RESET,
Qipan Lie3fb57c2015-05-19 14:41:12 +0000618 sspi->base + sspi->regs->rxfifo_op);
Qipan Li41148c32014-05-04 14:32:36 +0800619 writel(SIRFSOC_SPI_FIFO_RESET,
Qipan Lie3fb57c2015-05-19 14:41:12 +0000620 sspi->base + sspi->regs->txfifo_op);
621 switch (sspi->type) {
622 case SIRF_USP_SPI_P2:
623 writel(0x0, sspi->base + sspi->regs->rxfifo_op);
624 writel(0x0, sspi->base + sspi->regs->txfifo_op);
625 writel(0, sspi->base + sspi->regs->int_en);
626 writel(readl(sspi->base + sspi->regs->int_st),
627 sspi->base + sspi->regs->int_st);
628 writel(min((sspi->left_tx_word * sspi->word_width),
629 sspi->fifo_size),
630 sspi->base + sspi->regs->tx_dma_io_len);
631 writel(min((sspi->left_rx_word * sspi->word_width),
632 sspi->fifo_size),
633 sspi->base + sspi->regs->rx_dma_io_len);
634 break;
635 case SIRF_USP_SPI_A7:
636 writel(0x0, sspi->base + sspi->regs->rxfifo_op);
637 writel(0x0, sspi->base + sspi->regs->txfifo_op);
638 writel(~0UL, sspi->base + sspi->regs->usp_int_en_clr);
639 writel(readl(sspi->base + sspi->regs->int_st),
640 sspi->base + sspi->regs->int_st);
641 writel(min((sspi->left_tx_word * sspi->word_width),
642 sspi->fifo_size),
643 sspi->base + sspi->regs->tx_dma_io_len);
644 writel(min((sspi->left_rx_word * sspi->word_width),
645 sspi->fifo_size),
646 sspi->base + sspi->regs->rx_dma_io_len);
647 break;
648 case SIRF_REAL_SPI:
649 writel(SIRFSOC_SPI_FIFO_START,
650 sspi->base + sspi->regs->rxfifo_op);
651 writel(SIRFSOC_SPI_FIFO_START,
652 sspi->base + sspi->regs->txfifo_op);
653 writel(0, sspi->base + sspi->regs->int_en);
654 writel(readl(sspi->base + sspi->regs->int_st),
655 sspi->base + sspi->regs->int_st);
656 writel(readl(sspi->base + sspi->regs->spi_ctrl) |
657 SIRFSOC_SPI_MUL_DAT_MODE |
658 SIRFSOC_SPI_ENA_AUTO_CLR,
659 sspi->base + sspi->regs->spi_ctrl);
660 data_units = sspi->fifo_size / sspi->word_width;
661 writel(min(sspi->left_tx_word, data_units) - 1,
662 sspi->base + sspi->regs->tx_dma_io_len);
663 writel(min(sspi->left_rx_word, data_units) - 1,
664 sspi->base + sspi->regs->rx_dma_io_len);
665 break;
666 }
667 while (!((readl(sspi->base + sspi->regs->txfifo_st)
668 & SIRFSOC_SPI_FIFO_FULL_MASK(sspi))) &&
669 sspi->left_tx_word)
Qipan Li41148c32014-05-04 14:32:36 +0800670 sspi->tx_word(sspi);
671 writel(SIRFSOC_SPI_TXFIFO_EMPTY_INT_EN |
672 SIRFSOC_SPI_TX_UFLOW_INT_EN |
Qipan Lif2a08b42014-09-02 17:01:03 +0800673 SIRFSOC_SPI_RX_OFLOW_INT_EN |
674 SIRFSOC_SPI_RX_IO_DMA_INT_EN,
Qipan Lie3fb57c2015-05-19 14:41:12 +0000675 sspi->base + sspi->regs->int_en);
Qipan Li41148c32014-05-04 14:32:36 +0800676 writel(SIRFSOC_SPI_RX_EN | SIRFSOC_SPI_TX_EN,
Qipan Lie3fb57c2015-05-19 14:41:12 +0000677 sspi->base + sspi->regs->tx_rx_en);
678 if (sspi->type == SIRF_USP_SPI_P2 ||
679 sspi->type == SIRF_USP_SPI_A7) {
680 writel(SIRFSOC_SPI_FIFO_START,
681 sspi->base + sspi->regs->rxfifo_op);
682 writel(SIRFSOC_SPI_FIFO_START,
683 sspi->base + sspi->regs->txfifo_op);
684 }
Qipan Li41148c32014-05-04 14:32:36 +0800685 if (!wait_for_completion_timeout(&sspi->tx_done, timeout) ||
686 !wait_for_completion_timeout(&sspi->rx_done, timeout)) {
687 dev_err(&spi->dev, "transfer timeout\n");
Qipan Lie3fb57c2015-05-19 14:41:12 +0000688 if (sspi->type == SIRF_USP_SPI_P2 ||
689 sspi->type == SIRF_USP_SPI_A7)
690 writel(0, sspi->base + sspi->regs->tx_rx_en);
Qipan Li41148c32014-05-04 14:32:36 +0800691 break;
692 }
Qipan Lie3fb57c2015-05-19 14:41:12 +0000693 while (!((readl(sspi->base + sspi->regs->rxfifo_st)
694 & SIRFSOC_SPI_FIFO_EMPTY_MASK(sspi))) &&
695 sspi->left_rx_word)
Qipan Li41148c32014-05-04 14:32:36 +0800696 sspi->rx_word(sspi);
Qipan Lie3fb57c2015-05-19 14:41:12 +0000697 if (sspi->type == SIRF_USP_SPI_P2 ||
698 sspi->type == SIRF_USP_SPI_A7)
699 writel(0, sspi->base + sspi->regs->tx_rx_en);
700 writel(0, sspi->base + sspi->regs->rxfifo_op);
701 writel(0, sspi->base + sspi->regs->txfifo_op);
Qipan Li41148c32014-05-04 14:32:36 +0800702 } while (sspi->left_tx_word != 0 || sspi->left_rx_word != 0);
Qipan Lic908ef32014-04-15 15:24:59 +0800703}
704
705static int spi_sirfsoc_transfer(struct spi_device *spi, struct spi_transfer *t)
706{
707 struct sirfsoc_spi *sspi;
Qipan Lic908ef32014-04-15 15:24:59 +0800708
Qipan Lie3fb57c2015-05-19 14:41:12 +0000709 sspi = spi_master_get_devdata(spi->master);
Qipan Li3aa7b1d2015-05-26 05:21:01 +0000710 sspi->tx = t->tx_buf;
711 sspi->rx = t->rx_buf;
Qipan Lic908ef32014-04-15 15:24:59 +0800712 sspi->left_tx_word = sspi->left_rx_word = t->len / sspi->word_width;
713 reinit_completion(&sspi->rx_done);
714 reinit_completion(&sspi->tx_done);
715 /*
716 * in the transfer, if transfer data using command register with rx_buf
717 * null, just fill command data into command register and wait for its
718 * completion.
719 */
Qipan Lie3fb57c2015-05-19 14:41:12 +0000720 if (sspi->type == SIRF_REAL_SPI && sspi->tx_by_cmd)
Qipan Lic908ef32014-04-15 15:24:59 +0800721 spi_sirfsoc_cmd_transfer(spi, t);
722 else if (IS_DMA_VALID(t))
723 spi_sirfsoc_dma_transfer(spi, t);
724 else
725 spi_sirfsoc_pio_transfer(spi, t);
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800726
Qipan Li692fb0f2013-08-25 21:42:50 +0800727 return t->len - sspi->left_rx_word * sspi->word_width;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800728}
729
730static void spi_sirfsoc_chipselect(struct spi_device *spi, int value)
731{
732 struct sirfsoc_spi *sspi = spi_master_get_devdata(spi->master);
733
Qipan Li7850cdf2014-09-02 17:01:01 +0800734 if (sspi->hw_cs) {
Qipan Lie3fb57c2015-05-19 14:41:12 +0000735 u32 regval;
736
737 switch (sspi->type) {
738 case SIRF_REAL_SPI:
739 regval = readl(sspi->base + sspi->regs->spi_ctrl);
740 switch (value) {
741 case BITBANG_CS_ACTIVE:
742 if (spi->mode & SPI_CS_HIGH)
743 regval |= SIRFSOC_SPI_CS_IO_OUT;
744 else
745 regval &= ~SIRFSOC_SPI_CS_IO_OUT;
746 break;
747 case BITBANG_CS_INACTIVE:
748 if (spi->mode & SPI_CS_HIGH)
749 regval &= ~SIRFSOC_SPI_CS_IO_OUT;
750 else
751 regval |= SIRFSOC_SPI_CS_IO_OUT;
752 break;
753 }
754 writel(regval, sspi->base + sspi->regs->spi_ctrl);
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800755 break;
Qipan Lie3fb57c2015-05-19 14:41:12 +0000756 case SIRF_USP_SPI_P2:
757 case SIRF_USP_SPI_A7:
758 regval = readl(sspi->base +
759 sspi->regs->usp_pin_io_data);
760 switch (value) {
761 case BITBANG_CS_ACTIVE:
762 if (spi->mode & SPI_CS_HIGH)
763 regval |= SIRFSOC_USP_CS_HIGH_VALUE;
764 else
765 regval &= ~(SIRFSOC_USP_CS_HIGH_VALUE);
766 break;
767 case BITBANG_CS_INACTIVE:
768 if (spi->mode & SPI_CS_HIGH)
769 regval &= ~(SIRFSOC_USP_CS_HIGH_VALUE);
770 else
771 regval |= SIRFSOC_USP_CS_HIGH_VALUE;
772 break;
773 }
774 writel(regval,
775 sspi->base + sspi->regs->usp_pin_io_data);
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800776 break;
777 }
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800778 } else {
Qipan Li6ee8a2f2014-04-14 14:29:59 +0800779 switch (value) {
780 case BITBANG_CS_ACTIVE:
Qipan Li7850cdf2014-09-02 17:01:01 +0800781 gpio_direction_output(spi->cs_gpio,
Qipan Li6ee8a2f2014-04-14 14:29:59 +0800782 spi->mode & SPI_CS_HIGH ? 1 : 0);
783 break;
784 case BITBANG_CS_INACTIVE:
Qipan Li7850cdf2014-09-02 17:01:01 +0800785 gpio_direction_output(spi->cs_gpio,
Qipan Li6ee8a2f2014-04-14 14:29:59 +0800786 spi->mode & SPI_CS_HIGH ? 0 : 1);
787 break;
788 }
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800789 }
790}
791
Qipan Lie3fb57c2015-05-19 14:41:12 +0000792static int spi_sirfsoc_config_mode(struct spi_device *spi)
793{
794 struct sirfsoc_spi *sspi;
795 u32 regval, usp_mode1;
796
797 sspi = spi_master_get_devdata(spi->master);
798 regval = readl(sspi->base + sspi->regs->spi_ctrl);
799 usp_mode1 = readl(sspi->base + sspi->regs->usp_mode1);
800 if (!(spi->mode & SPI_CS_HIGH)) {
801 regval |= SIRFSOC_SPI_CS_IDLE_STAT;
802 usp_mode1 &= ~SIRFSOC_USP_CS_HIGH_VALID;
803 } else {
804 regval &= ~SIRFSOC_SPI_CS_IDLE_STAT;
805 usp_mode1 |= SIRFSOC_USP_CS_HIGH_VALID;
806 }
807 if (!(spi->mode & SPI_LSB_FIRST)) {
808 regval |= SIRFSOC_SPI_TRAN_MSB;
809 usp_mode1 &= ~SIRFSOC_USP_LSB;
810 } else {
811 regval &= ~SIRFSOC_SPI_TRAN_MSB;
812 usp_mode1 |= SIRFSOC_USP_LSB;
813 }
814 if (spi->mode & SPI_CPOL) {
815 regval |= SIRFSOC_SPI_CLK_IDLE_STAT;
816 usp_mode1 |= SIRFSOC_USP_SCLK_IDLE_STAT;
817 } else {
818 regval &= ~SIRFSOC_SPI_CLK_IDLE_STAT;
819 usp_mode1 &= ~SIRFSOC_USP_SCLK_IDLE_STAT;
820 }
821 /*
822 * Data should be driven at least 1/2 cycle before the fetch edge
823 * to make sure that data gets stable at the fetch edge.
824 */
825 if (((spi->mode & SPI_CPOL) && (spi->mode & SPI_CPHA)) ||
826 (!(spi->mode & SPI_CPOL) && !(spi->mode & SPI_CPHA))) {
827 regval &= ~SIRFSOC_SPI_DRV_POS_EDGE;
828 usp_mode1 |= (SIRFSOC_USP_TXD_FALLING_EDGE |
829 SIRFSOC_USP_RXD_FALLING_EDGE);
830 } else {
831 regval |= SIRFSOC_SPI_DRV_POS_EDGE;
832 usp_mode1 &= ~(SIRFSOC_USP_RXD_FALLING_EDGE |
833 SIRFSOC_USP_TXD_FALLING_EDGE);
834 }
835 writel((SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(sspi, sspi->fifo_size - 2) <<
836 SIRFSOC_SPI_FIFO_SC_OFFSET) |
837 (SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(sspi, sspi->fifo_size / 2) <<
838 SIRFSOC_SPI_FIFO_LC_OFFSET) |
839 (SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(sspi, 2) <<
840 SIRFSOC_SPI_FIFO_HC_OFFSET),
841 sspi->base + sspi->regs->txfifo_level_chk);
842 writel((SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(sspi, 2) <<
843 SIRFSOC_SPI_FIFO_SC_OFFSET) |
844 (SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(sspi, sspi->fifo_size / 2) <<
845 SIRFSOC_SPI_FIFO_LC_OFFSET) |
846 (SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(sspi, sspi->fifo_size - 2) <<
847 SIRFSOC_SPI_FIFO_HC_OFFSET),
848 sspi->base + sspi->regs->rxfifo_level_chk);
849 /*
850 * it should never set to hardware cs mode because in hardware cs mode,
851 * cs signal can't controlled by driver.
852 */
853 switch (sspi->type) {
854 case SIRF_REAL_SPI:
855 regval |= SIRFSOC_SPI_CS_IO_MODE;
856 writel(regval, sspi->base + sspi->regs->spi_ctrl);
857 break;
858 case SIRF_USP_SPI_P2:
859 case SIRF_USP_SPI_A7:
860 usp_mode1 |= SIRFSOC_USP_SYNC_MODE;
861 usp_mode1 |= SIRFSOC_USP_TFS_IO_MODE;
862 usp_mode1 &= ~SIRFSOC_USP_TFS_IO_INPUT;
863 writel(usp_mode1, sspi->base + sspi->regs->usp_mode1);
864 break;
865 }
866
867 return 0;
868}
869
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800870static int
871spi_sirfsoc_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
872{
873 struct sirfsoc_spi *sspi;
874 u8 bits_per_word = 0;
875 int hz = 0;
Qipan Lie3fb57c2015-05-19 14:41:12 +0000876 u32 regval, txfifo_ctrl, rxfifo_ctrl, tx_frm_ctl, rx_frm_ctl, usp_mode2;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800877
878 sspi = spi_master_get_devdata(spi->master);
879
Laxman Dewangan766ed702012-12-18 14:25:43 +0530880 bits_per_word = (t) ? t->bits_per_word : spi->bits_per_word;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800881 hz = t && t->speed_hz ? t->speed_hz : spi->max_speed_hz;
882
Qipan Lie3fb57c2015-05-19 14:41:12 +0000883 usp_mode2 = regval = (sspi->ctrl_freq / (2 * hz)) - 1;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800884 if (regval > 0xFFFF || regval < 0) {
885 dev_err(&spi->dev, "Speed %d not supported\n", hz);
886 return -EINVAL;
887 }
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800888 switch (bits_per_word) {
889 case 8:
890 regval |= SIRFSOC_SPI_TRAN_DAT_FORMAT_8;
891 sspi->rx_word = spi_sirfsoc_rx_word_u8;
892 sspi->tx_word = spi_sirfsoc_tx_word_u8;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800893 break;
894 case 12:
895 case 16:
Qipan Lid77ec5d2014-04-14 14:30:00 +0800896 regval |= (bits_per_word == 12) ?
897 SIRFSOC_SPI_TRAN_DAT_FORMAT_12 :
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800898 SIRFSOC_SPI_TRAN_DAT_FORMAT_16;
899 sspi->rx_word = spi_sirfsoc_rx_word_u16;
900 sspi->tx_word = spi_sirfsoc_tx_word_u16;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800901 break;
902 case 32:
903 regval |= SIRFSOC_SPI_TRAN_DAT_FORMAT_32;
904 sspi->rx_word = spi_sirfsoc_rx_word_u32;
905 sspi->tx_word = spi_sirfsoc_tx_word_u32;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800906 break;
Arnd Bergmann804ae432013-06-03 15:24:53 +0200907 default:
Qipan Lif08654a2015-04-27 09:22:28 +0000908 dev_err(&spi->dev, "bpw %d not supported\n", bits_per_word);
909 return -EINVAL;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800910 }
Axel Lin8c328a22014-01-15 17:07:43 +0800911 sspi->word_width = DIV_ROUND_UP(bits_per_word, 8);
Qipan Lie3fb57c2015-05-19 14:41:12 +0000912 txfifo_ctrl = (((sspi->fifo_size / 2) &
913 SIRFSOC_SPI_FIFO_THD_MASK(sspi))
914 << SIRFSOC_SPI_FIFO_THD_OFFSET) |
915 (sspi->word_width >> 1);
916 rxfifo_ctrl = (((sspi->fifo_size / 2) &
917 SIRFSOC_SPI_FIFO_THD_MASK(sspi))
918 << SIRFSOC_SPI_FIFO_THD_OFFSET) |
919 (sspi->word_width >> 1);
920 writel(txfifo_ctrl, sspi->base + sspi->regs->txfifo_ctrl);
921 writel(rxfifo_ctrl, sspi->base + sspi->regs->rxfifo_ctrl);
922 if (sspi->type == SIRF_USP_SPI_P2 ||
923 sspi->type == SIRF_USP_SPI_A7) {
924 tx_frm_ctl = 0;
925 tx_frm_ctl |= ((bits_per_word - 1) & SIRFSOC_USP_TX_DATA_MASK)
926 << SIRFSOC_USP_TX_DATA_OFFSET;
927 tx_frm_ctl |= ((bits_per_word + 1 + SIRFSOC_USP_TXD_DELAY_LEN
928 - 1) & SIRFSOC_USP_TX_SYNC_MASK) <<
929 SIRFSOC_USP_TX_SYNC_OFFSET;
930 tx_frm_ctl |= ((bits_per_word + 1 + SIRFSOC_USP_TXD_DELAY_LEN
931 + 2 - 1) & SIRFSOC_USP_TX_FRAME_MASK) <<
932 SIRFSOC_USP_TX_FRAME_OFFSET;
933 tx_frm_ctl |= ((bits_per_word - 1) &
934 SIRFSOC_USP_TX_SHIFTER_MASK) <<
935 SIRFSOC_USP_TX_SHIFTER_OFFSET;
936 rx_frm_ctl = 0;
937 rx_frm_ctl |= ((bits_per_word - 1) & SIRFSOC_USP_RX_DATA_MASK)
938 << SIRFSOC_USP_RX_DATA_OFFSET;
939 rx_frm_ctl |= ((bits_per_word + 1 + SIRFSOC_USP_RXD_DELAY_LEN
940 + 2 - 1) & SIRFSOC_USP_RX_FRAME_MASK) <<
941 SIRFSOC_USP_RX_FRAME_OFFSET;
942 rx_frm_ctl |= ((bits_per_word - 1)
943 & SIRFSOC_USP_RX_SHIFTER_MASK) <<
944 SIRFSOC_USP_RX_SHIFTER_OFFSET;
945 writel(tx_frm_ctl | (((usp_mode2 >> 10) &
946 SIRFSOC_USP_CLK_10_11_MASK) <<
947 SIRFSOC_USP_CLK_10_11_OFFSET),
948 sspi->base + sspi->regs->usp_tx_frame_ctrl);
949 writel(rx_frm_ctl | (((usp_mode2 >> 12) &
950 SIRFSOC_USP_CLK_12_15_MASK) <<
951 SIRFSOC_USP_CLK_12_15_OFFSET),
952 sspi->base + sspi->regs->usp_rx_frame_ctrl);
953 writel(readl(sspi->base + sspi->regs->usp_mode2) |
954 ((usp_mode2 & SIRFSOC_USP_CLK_DIVISOR_MASK) <<
955 SIRFSOC_USP_CLK_DIVISOR_OFFSET) |
956 (SIRFSOC_USP_RXD_DELAY_LEN <<
957 SIRFSOC_USP_RXD_DELAY_OFFSET) |
958 (SIRFSOC_USP_TXD_DELAY_LEN <<
959 SIRFSOC_USP_TXD_DELAY_OFFSET),
960 sspi->base + sspi->regs->usp_mode2);
Qipan Lieeb713952014-03-01 12:38:17 +0800961 }
Qipan Lie3fb57c2015-05-19 14:41:12 +0000962 if (sspi->type == SIRF_REAL_SPI)
963 writel(regval, sspi->base + sspi->regs->spi_ctrl);
964 spi_sirfsoc_config_mode(spi);
965 if (sspi->type == SIRF_REAL_SPI) {
966 if (t && t->tx_buf && !t->rx_buf &&
967 (t->len <= SIRFSOC_MAX_CMD_BYTES)) {
968 sspi->tx_by_cmd = true;
969 writel(readl(sspi->base + sspi->regs->spi_ctrl) |
970 (SIRFSOC_SPI_CMD_BYTE_NUM((t->len - 1)) |
971 SIRFSOC_SPI_CMD_MODE),
972 sspi->base + sspi->regs->spi_ctrl);
973 } else {
974 sspi->tx_by_cmd = false;
975 writel(readl(sspi->base + sspi->regs->spi_ctrl) &
976 ~SIRFSOC_SPI_CMD_MODE,
977 sspi->base + sspi->regs->spi_ctrl);
978 }
979 }
Barry Songde39f5f2013-08-06 14:21:21 +0800980 if (IS_DMA_VALID(t)) {
981 /* Enable DMA mode for RX, TX */
Qipan Lie3fb57c2015-05-19 14:41:12 +0000982 writel(0, sspi->base + sspi->regs->tx_dma_io_ctrl);
Qipan Lid77ec5d2014-04-14 14:30:00 +0800983 writel(SIRFSOC_SPI_RX_DMA_FLUSH,
Qipan Lie3fb57c2015-05-19 14:41:12 +0000984 sspi->base + sspi->regs->rx_dma_io_ctrl);
Barry Songde39f5f2013-08-06 14:21:21 +0800985 } else {
986 /* Enable IO mode for RX, TX */
Qipan Lid77ec5d2014-04-14 14:30:00 +0800987 writel(SIRFSOC_SPI_IO_MODE_SEL,
Qipan Lie3fb57c2015-05-19 14:41:12 +0000988 sspi->base + sspi->regs->tx_dma_io_ctrl);
Qipan Lid77ec5d2014-04-14 14:30:00 +0800989 writel(SIRFSOC_SPI_IO_MODE_SEL,
Qipan Lie3fb57c2015-05-19 14:41:12 +0000990 sspi->base + sspi->regs->rx_dma_io_ctrl);
Barry Songde39f5f2013-08-06 14:21:21 +0800991 }
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800992 return 0;
993}
994
995static int spi_sirfsoc_setup(struct spi_device *spi)
996{
Qipan Li7850cdf2014-09-02 17:01:01 +0800997 struct sirfsoc_spi *sspi;
Qipan Li96bf4012015-05-03 10:30:12 +0000998 int ret = 0;
Qipan Li7850cdf2014-09-02 17:01:01 +0800999
Qipan Li7850cdf2014-09-02 17:01:01 +08001000 sspi = spi_master_get_devdata(spi->master);
Qipan Li7850cdf2014-09-02 17:01:01 +08001001 if (spi->cs_gpio == -ENOENT)
1002 sspi->hw_cs = true;
Qipan Li96bf4012015-05-03 10:30:12 +00001003 else {
Qipan Li7850cdf2014-09-02 17:01:01 +08001004 sspi->hw_cs = false;
Qipan Li96bf4012015-05-03 10:30:12 +00001005 if (!spi_get_ctldata(spi)) {
1006 void *cs = kmalloc(sizeof(int), GFP_KERNEL);
1007 if (!cs) {
1008 ret = -ENOMEM;
1009 goto exit;
1010 }
1011 ret = gpio_is_valid(spi->cs_gpio);
1012 if (!ret) {
1013 dev_err(&spi->dev, "no valid gpio\n");
1014 ret = -ENOENT;
1015 goto exit;
1016 }
1017 ret = gpio_request(spi->cs_gpio, DRIVER_NAME);
1018 if (ret) {
1019 dev_err(&spi->dev, "failed to request gpio\n");
1020 goto exit;
1021 }
1022 spi_set_ctldata(spi, cs);
1023 }
1024 }
Qipan Lie3fb57c2015-05-19 14:41:12 +00001025 spi_sirfsoc_config_mode(spi);
Qipan Li96bf4012015-05-03 10:30:12 +00001026 spi_sirfsoc_chipselect(spi, BITBANG_CS_INACTIVE);
1027exit:
1028 return ret;
1029}
1030
1031static void spi_sirfsoc_cleanup(struct spi_device *spi)
1032{
1033 if (spi_get_ctldata(spi)) {
1034 gpio_free(spi->cs_gpio);
1035 kfree(spi_get_ctldata(spi));
1036 }
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001037}
1038
Qipan Li5bcc3b02015-05-26 05:21:34 +00001039static const struct sirf_spi_comp_data sirf_real_spi = {
1040 .regs = &real_spi_register,
1041 .type = SIRF_REAL_SPI,
1042 .dat_max_frm_len = 64 * 1024,
1043 .fifo_size = 256,
1044};
1045
1046static const struct sirf_spi_comp_data sirf_usp_spi_p2 = {
1047 .regs = &usp_spi_register,
1048 .type = SIRF_USP_SPI_P2,
1049 .dat_max_frm_len = 1024 * 1024,
1050 .fifo_size = 128,
1051 .hwinit = sirfsoc_usp_hwinit,
1052};
1053
1054static const struct sirf_spi_comp_data sirf_usp_spi_a7 = {
1055 .regs = &usp_spi_register,
1056 .type = SIRF_USP_SPI_A7,
1057 .dat_max_frm_len = 1024 * 1024,
1058 .fifo_size = 512,
1059 .hwinit = sirfsoc_usp_hwinit,
1060};
1061
Qipan Lie3fb57c2015-05-19 14:41:12 +00001062static const struct of_device_id spi_sirfsoc_of_match[] = {
1063 { .compatible = "sirf,prima2-spi", .data = &sirf_real_spi},
1064 { .compatible = "sirf,prima2-usp-spi", .data = &sirf_usp_spi_p2},
1065 { .compatible = "sirf,atlas7-usp-spi", .data = &sirf_usp_spi_a7},
1066 {}
1067};
1068MODULE_DEVICE_TABLE(of, spi_sirfsoc_of_match);
1069
Grant Likelyfd4a3192012-12-07 16:57:14 +00001070static int spi_sirfsoc_probe(struct platform_device *pdev)
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001071{
1072 struct sirfsoc_spi *sspi;
1073 struct spi_master *master;
1074 struct resource *mem_res;
Qipan Lie3fb57c2015-05-19 14:41:12 +00001075 struct sirf_spi_comp_data *spi_comp_data;
Qipan Li7850cdf2014-09-02 17:01:01 +08001076 int irq;
Qipan Li96bf4012015-05-03 10:30:12 +00001077 int ret;
Qipan Lie3fb57c2015-05-19 14:41:12 +00001078 const struct of_device_id *match;
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001079
Qipan Li8509c552014-11-20 22:33:07 +08001080 ret = device_reset(&pdev->dev);
1081 if (ret) {
1082 dev_err(&pdev->dev, "SPI reset failed!\n");
1083 return ret;
1084 }
1085
Qipan Li7850cdf2014-09-02 17:01:01 +08001086 master = spi_alloc_master(&pdev->dev, sizeof(*sspi));
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001087 if (!master) {
1088 dev_err(&pdev->dev, "Unable to allocate SPI master\n");
1089 return -ENOMEM;
1090 }
Qipan Lie3fb57c2015-05-19 14:41:12 +00001091 match = of_match_node(spi_sirfsoc_of_match, pdev->dev.of_node);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001092 platform_set_drvdata(pdev, master);
1093 sspi = spi_master_get_devdata(master);
Qipan Lie3fb57c2015-05-19 14:41:12 +00001094 sspi->fifo_full_offset = ilog2(sspi->fifo_size);
1095 spi_comp_data = (struct sirf_spi_comp_data *)match->data;
1096 sspi->regs = spi_comp_data->regs;
1097 sspi->type = spi_comp_data->type;
1098 sspi->fifo_level_chk_mask = (sspi->fifo_size / 4) - 1;
1099 sspi->dat_max_frm_len = spi_comp_data->dat_max_frm_len;
1100 sspi->fifo_size = spi_comp_data->fifo_size;
Julia Lawall24797902013-08-14 11:11:29 +02001101 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingb0ee5602013-01-21 11:09:18 +01001102 sspi->base = devm_ioremap_resource(&pdev->dev, mem_res);
1103 if (IS_ERR(sspi->base)) {
1104 ret = PTR_ERR(sspi->base);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001105 goto free_master;
1106 }
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001107 irq = platform_get_irq(pdev, 0);
1108 if (irq < 0) {
1109 ret = -ENXIO;
1110 goto free_master;
1111 }
1112 ret = devm_request_irq(&pdev->dev, irq, spi_sirfsoc_irq, 0,
1113 DRIVER_NAME, sspi);
1114 if (ret)
1115 goto free_master;
1116
Axel Lin94c69f72013-09-10 15:43:41 +08001117 sspi->bitbang.master = master;
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001118 sspi->bitbang.chipselect = spi_sirfsoc_chipselect;
1119 sspi->bitbang.setup_transfer = spi_sirfsoc_setup_transfer;
1120 sspi->bitbang.txrx_bufs = spi_sirfsoc_transfer;
1121 sspi->bitbang.master->setup = spi_sirfsoc_setup;
Qipan Li96bf4012015-05-03 10:30:12 +00001122 sspi->bitbang.master->cleanup = spi_sirfsoc_cleanup;
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001123 master->bus_num = pdev->id;
Qipan Li94b1f0d2013-06-25 19:45:29 +08001124 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_CS_HIGH;
Stephen Warren24778be2013-05-21 20:36:35 -06001125 master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(12) |
1126 SPI_BPW_MASK(16) | SPI_BPW_MASK(32);
Qipan Lifcc50e52014-11-17 23:17:03 +08001127 master->max_speed_hz = SIRFSOC_SPI_DEFAULT_FRQ;
Qipan Li3aa7b1d2015-05-26 05:21:01 +00001128 master->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX;
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001129 sspi->bitbang.master->dev.of_node = pdev->dev.of_node;
1130
Barry Songde39f5f2013-08-06 14:21:21 +08001131 /* request DMA channels */
Barry Songdd7243d2014-02-13 00:30:19 +08001132 sspi->rx_chan = dma_request_slave_channel(&pdev->dev, "rx");
Barry Songde39f5f2013-08-06 14:21:21 +08001133 if (!sspi->rx_chan) {
1134 dev_err(&pdev->dev, "can not allocate rx dma channel\n");
Wei Yongjun6cca9e22013-08-23 08:33:39 +08001135 ret = -ENODEV;
Barry Songde39f5f2013-08-06 14:21:21 +08001136 goto free_master;
1137 }
Barry Songdd7243d2014-02-13 00:30:19 +08001138 sspi->tx_chan = dma_request_slave_channel(&pdev->dev, "tx");
Barry Songde39f5f2013-08-06 14:21:21 +08001139 if (!sspi->tx_chan) {
1140 dev_err(&pdev->dev, "can not allocate tx dma channel\n");
Wei Yongjun6cca9e22013-08-23 08:33:39 +08001141 ret = -ENODEV;
Barry Songde39f5f2013-08-06 14:21:21 +08001142 goto free_rx_dma;
1143 }
1144
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001145 sspi->clk = clk_get(&pdev->dev, NULL);
1146 if (IS_ERR(sspi->clk)) {
Barry Songde39f5f2013-08-06 14:21:21 +08001147 ret = PTR_ERR(sspi->clk);
1148 goto free_tx_dma;
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001149 }
Barry Songe5118cd2012-12-26 10:48:33 +08001150 clk_prepare_enable(sspi->clk);
Qipan Li5bcc3b02015-05-26 05:21:34 +00001151 if (spi_comp_data->hwinit)
1152 spi_comp_data->hwinit(sspi);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001153 sspi->ctrl_freq = clk_get_rate(sspi->clk);
1154
Barry Songde39f5f2013-08-06 14:21:21 +08001155 init_completion(&sspi->rx_done);
1156 init_completion(&sspi->tx_done);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001157
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001158 ret = spi_bitbang_start(&sspi->bitbang);
1159 if (ret)
Qipan Lia34bcbe2015-05-07 07:13:10 +00001160 goto free_clk;
Colin Ian King2d781e82017-06-26 14:39:12 +01001161 dev_info(&pdev->dev, "registered, bus number = %d\n", master->bus_num);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001162
1163 return 0;
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001164free_clk:
Barry Songe5118cd2012-12-26 10:48:33 +08001165 clk_disable_unprepare(sspi->clk);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001166 clk_put(sspi->clk);
Barry Songde39f5f2013-08-06 14:21:21 +08001167free_tx_dma:
1168 dma_release_channel(sspi->tx_chan);
1169free_rx_dma:
1170 dma_release_channel(sspi->rx_chan);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001171free_master:
1172 spi_master_put(master);
Qipan Li7850cdf2014-09-02 17:01:01 +08001173
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001174 return ret;
1175}
1176
Grant Likelyfd4a3192012-12-07 16:57:14 +00001177static int spi_sirfsoc_remove(struct platform_device *pdev)
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001178{
1179 struct spi_master *master;
1180 struct sirfsoc_spi *sspi;
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001181
1182 master = platform_get_drvdata(pdev);
1183 sspi = spi_master_get_devdata(master);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001184 spi_bitbang_stop(&sspi->bitbang);
Barry Songe5118cd2012-12-26 10:48:33 +08001185 clk_disable_unprepare(sspi->clk);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001186 clk_put(sspi->clk);
Barry Songde39f5f2013-08-06 14:21:21 +08001187 dma_release_channel(sspi->rx_chan);
1188 dma_release_channel(sspi->tx_chan);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001189 spi_master_put(master);
1190 return 0;
1191}
1192
Qipan Lifacffed2014-02-13 00:30:20 +08001193#ifdef CONFIG_PM_SLEEP
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001194static int spi_sirfsoc_suspend(struct device *dev)
1195{
Axel Lina12163942013-08-09 15:35:16 +08001196 struct spi_master *master = dev_get_drvdata(dev);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001197 struct sirfsoc_spi *sspi = spi_master_get_devdata(master);
Axel Lina82ba3a2014-03-05 15:19:09 +08001198 int ret;
1199
1200 ret = spi_master_suspend(master);
1201 if (ret)
1202 return ret;
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001203
1204 clk_disable(sspi->clk);
1205 return 0;
1206}
1207
1208static int spi_sirfsoc_resume(struct device *dev)
1209{
Axel Lina12163942013-08-09 15:35:16 +08001210 struct spi_master *master = dev_get_drvdata(dev);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001211 struct sirfsoc_spi *sspi = spi_master_get_devdata(master);
1212
1213 clk_enable(sspi->clk);
Qipan Lie3fb57c2015-05-19 14:41:12 +00001214 writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + sspi->regs->txfifo_op);
1215 writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + sspi->regs->rxfifo_op);
1216 writel(SIRFSOC_SPI_FIFO_START, sspi->base + sspi->regs->txfifo_op);
1217 writel(SIRFSOC_SPI_FIFO_START, sspi->base + sspi->regs->rxfifo_op);
1218 return 0;
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001219}
Qipan Lifacffed2014-02-13 00:30:20 +08001220#endif
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001221
Jingoo Han71aa2e32014-02-26 10:32:48 +09001222static SIMPLE_DEV_PM_OPS(spi_sirfsoc_pm_ops, spi_sirfsoc_suspend,
1223 spi_sirfsoc_resume);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001224
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001225static struct platform_driver spi_sirfsoc_driver = {
1226 .driver = {
1227 .name = DRIVER_NAME,
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001228 .pm = &spi_sirfsoc_pm_ops,
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001229 .of_match_table = spi_sirfsoc_of_match,
1230 },
1231 .probe = spi_sirfsoc_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001232 .remove = spi_sirfsoc_remove,
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001233};
1234module_platform_driver(spi_sirfsoc_driver);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001235MODULE_DESCRIPTION("SiRF SoC SPI master driver");
Qipan Lid77ec5d2014-04-14 14:30:00 +08001236MODULE_AUTHOR("Zhiwu Song <Zhiwu.Song@csr.com>");
1237MODULE_AUTHOR("Barry Song <Baohua.Song@csr.com>");
Qipan Lie3fb57c2015-05-19 14:41:12 +00001238MODULE_AUTHOR("Qipan Li <Qipan.Li@csr.com>");
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001239MODULE_LICENSE("GPL v2");