blob: dd87ba6fddabf0499161d93d74df93cfe1e1cd8d [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
251struct sirf_spi_comp_data {
252 const struct sirf_spi_register *regs;
253 enum sirf_spi_type type;
254 unsigned int dat_max_frm_len;
255 unsigned int fifo_size;
256};
257
258static const struct sirf_spi_comp_data sirf_real_spi = {
259 .regs = &real_spi_register,
260 .type = SIRF_REAL_SPI,
261 .dat_max_frm_len = 64 * 1024,
262 .fifo_size = 256,
263};
264
265static const struct sirf_spi_comp_data sirf_usp_spi_p2 = {
266 .regs = &usp_spi_register,
267 .type = SIRF_USP_SPI_P2,
268 .dat_max_frm_len = 1024 * 1024,
269 .fifo_size = 128,
270};
271
272static const struct sirf_spi_comp_data sirf_usp_spi_a7 = {
273 .regs = &usp_spi_register,
274 .type = SIRF_USP_SPI_A7,
275 .dat_max_frm_len = 1024 * 1024,
276 .fifo_size = 512,
277};
278
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800279struct sirfsoc_spi {
280 struct spi_bitbang bitbang;
Barry Songde39f5f2013-08-06 14:21:21 +0800281 struct completion rx_done;
282 struct completion tx_done;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800283
284 void __iomem *base;
285 u32 ctrl_freq; /* SPI controller clock speed */
286 struct clk *clk;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800287
288 /* rx & tx bufs from the spi_transfer */
289 const void *tx;
290 void *rx;
291
292 /* place received word into rx buffer */
293 void (*rx_word) (struct sirfsoc_spi *);
294 /* get word from tx buffer for sending */
295 void (*tx_word) (struct sirfsoc_spi *);
296
297 /* number of words left to be tranmitted/received */
Qipan Li692fb0f2013-08-25 21:42:50 +0800298 unsigned int left_tx_word;
299 unsigned int left_rx_word;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800300
Barry Songde39f5f2013-08-06 14:21:21 +0800301 /* rx & tx DMA channels */
302 struct dma_chan *rx_chan;
303 struct dma_chan *tx_chan;
304 dma_addr_t src_start;
305 dma_addr_t dst_start;
306 void *dummypage;
307 int word_width; /* in bytes */
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800308
Qipan Lieeb713952014-03-01 12:38:17 +0800309 /*
310 * if tx size is not more than 4 and rx size is NULL, use
311 * command model
312 */
313 bool tx_by_cmd;
Qipan Li7850cdf2014-09-02 17:01:01 +0800314 bool hw_cs;
Qipan Lie3fb57c2015-05-19 14:41:12 +0000315 enum sirf_spi_type type;
316 const struct sirf_spi_register *regs;
317 unsigned int fifo_size;
318 /* fifo empty offset is (fifo full offset + 1)*/
319 unsigned int fifo_full_offset;
320 /* fifo_level_chk_mask is (fifo_size/4 - 1) */
321 unsigned int fifo_level_chk_mask;
322 unsigned int dat_max_frm_len;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800323};
324
325static void spi_sirfsoc_rx_word_u8(struct sirfsoc_spi *sspi)
326{
327 u32 data;
328 u8 *rx = sspi->rx;
329
Qipan Lie3fb57c2015-05-19 14:41:12 +0000330 data = readl(sspi->base + sspi->regs->rxfifo_data);
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800331
332 if (rx) {
333 *rx++ = (u8) data;
334 sspi->rx = rx;
335 }
336
Qipan Li692fb0f2013-08-25 21:42:50 +0800337 sspi->left_rx_word--;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800338}
339
340static void spi_sirfsoc_tx_word_u8(struct sirfsoc_spi *sspi)
341{
342 u32 data = 0;
343 const u8 *tx = sspi->tx;
344
345 if (tx) {
346 data = *tx++;
347 sspi->tx = tx;
348 }
Qipan Lie3fb57c2015-05-19 14:41:12 +0000349 writel(data, sspi->base + sspi->regs->txfifo_data);
Qipan Li692fb0f2013-08-25 21:42:50 +0800350 sspi->left_tx_word--;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800351}
352
353static void spi_sirfsoc_rx_word_u16(struct sirfsoc_spi *sspi)
354{
355 u32 data;
356 u16 *rx = sspi->rx;
357
Qipan Lie3fb57c2015-05-19 14:41:12 +0000358 data = readl(sspi->base + sspi->regs->rxfifo_data);
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800359
360 if (rx) {
361 *rx++ = (u16) data;
362 sspi->rx = rx;
363 }
364
Qipan Li692fb0f2013-08-25 21:42:50 +0800365 sspi->left_rx_word--;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800366}
367
368static void spi_sirfsoc_tx_word_u16(struct sirfsoc_spi *sspi)
369{
370 u32 data = 0;
371 const u16 *tx = sspi->tx;
372
373 if (tx) {
374 data = *tx++;
375 sspi->tx = tx;
376 }
377
Qipan Lie3fb57c2015-05-19 14:41:12 +0000378 writel(data, sspi->base + sspi->regs->txfifo_data);
Qipan Li692fb0f2013-08-25 21:42:50 +0800379 sspi->left_tx_word--;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800380}
381
382static void spi_sirfsoc_rx_word_u32(struct sirfsoc_spi *sspi)
383{
384 u32 data;
385 u32 *rx = sspi->rx;
386
Qipan Lie3fb57c2015-05-19 14:41:12 +0000387 data = readl(sspi->base + sspi->regs->rxfifo_data);
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800388
389 if (rx) {
390 *rx++ = (u32) data;
391 sspi->rx = rx;
392 }
393
Qipan Li692fb0f2013-08-25 21:42:50 +0800394 sspi->left_rx_word--;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800395
396}
397
398static void spi_sirfsoc_tx_word_u32(struct sirfsoc_spi *sspi)
399{
400 u32 data = 0;
401 const u32 *tx = sspi->tx;
402
403 if (tx) {
404 data = *tx++;
405 sspi->tx = tx;
406 }
407
Qipan Lie3fb57c2015-05-19 14:41:12 +0000408 writel(data, sspi->base + sspi->regs->txfifo_data);
Qipan Li692fb0f2013-08-25 21:42:50 +0800409 sspi->left_tx_word--;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800410}
411
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800412static irqreturn_t spi_sirfsoc_irq(int irq, void *dev_id)
413{
414 struct sirfsoc_spi *sspi = dev_id;
Qipan Lie3fb57c2015-05-19 14:41:12 +0000415 u32 spi_stat;
416
417 spi_stat = readl(sspi->base + sspi->regs->int_st);
418 if (sspi->tx_by_cmd && sspi->type == SIRF_REAL_SPI
419 && (spi_stat & SIRFSOC_SPI_FRM_END)) {
Qipan Lieeb713952014-03-01 12:38:17 +0800420 complete(&sspi->tx_done);
Qipan Lie3fb57c2015-05-19 14:41:12 +0000421 writel(0x0, sspi->base + sspi->regs->int_en);
422 writel(readl(sspi->base + sspi->regs->int_st),
423 sspi->base + sspi->regs->int_st);
Qipan Lieeb713952014-03-01 12:38:17 +0800424 return IRQ_HANDLED;
425 }
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800426 /* Error Conditions */
427 if (spi_stat & SIRFSOC_SPI_RX_OFLOW ||
428 spi_stat & SIRFSOC_SPI_TX_UFLOW) {
Qipan Li41148c32014-05-04 14:32:36 +0800429 complete(&sspi->tx_done);
Barry Songde39f5f2013-08-06 14:21:21 +0800430 complete(&sspi->rx_done);
Qipan Lie3fb57c2015-05-19 14:41:12 +0000431 switch (sspi->type) {
432 case SIRF_REAL_SPI:
433 case SIRF_USP_SPI_P2:
434 writel(0x0, sspi->base + sspi->regs->int_en);
435 break;
436 case SIRF_USP_SPI_A7:
437 writel(~0UL, sspi->base + sspi->regs->usp_int_en_clr);
438 break;
439 }
440 writel(readl(sspi->base + sspi->regs->int_st),
441 sspi->base + sspi->regs->int_st);
Qipan Li41148c32014-05-04 14:32:36 +0800442 return IRQ_HANDLED;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800443 }
Qipan Li41148c32014-05-04 14:32:36 +0800444 if (spi_stat & SIRFSOC_SPI_TXFIFO_EMPTY)
445 complete(&sspi->tx_done);
Qipan Lie3fb57c2015-05-19 14:41:12 +0000446 while (!(readl(sspi->base + sspi->regs->int_st) &
Qipan Li41148c32014-05-04 14:32:36 +0800447 SIRFSOC_SPI_RX_IO_DMA))
448 cpu_relax();
449 complete(&sspi->rx_done);
Qipan Lie3fb57c2015-05-19 14:41:12 +0000450 switch (sspi->type) {
451 case SIRF_REAL_SPI:
452 case SIRF_USP_SPI_P2:
453 writel(0x0, sspi->base + sspi->regs->int_en);
454 break;
455 case SIRF_USP_SPI_A7:
456 writel(~0UL, sspi->base + sspi->regs->usp_int_en_clr);
457 break;
458 }
459 writel(readl(sspi->base + sspi->regs->int_st),
460 sspi->base + sspi->regs->int_st);
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800461
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800462 return IRQ_HANDLED;
463}
464
Barry Songde39f5f2013-08-06 14:21:21 +0800465static void spi_sirfsoc_dma_fini_callback(void *data)
466{
467 struct completion *dma_complete = data;
468
469 complete(dma_complete);
470}
471
Qipan Li0021d972014-09-02 17:01:04 +0800472static void spi_sirfsoc_cmd_transfer(struct spi_device *spi,
Qipan Lic908ef32014-04-15 15:24:59 +0800473 struct spi_transfer *t)
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800474{
475 struct sirfsoc_spi *sspi;
476 int timeout = t->len * 10;
Qipan Lic908ef32014-04-15 15:24:59 +0800477 u32 cmd;
478
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800479 sspi = spi_master_get_devdata(spi->master);
Qipan Lie3fb57c2015-05-19 14:41:12 +0000480 writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + sspi->regs->txfifo_op);
481 writel(SIRFSOC_SPI_FIFO_START, sspi->base + sspi->regs->txfifo_op);
Qipan Lic908ef32014-04-15 15:24:59 +0800482 memcpy(&cmd, sspi->tx, t->len);
483 if (sspi->word_width == 1 && !(spi->mode & SPI_LSB_FIRST))
484 cmd = cpu_to_be32(cmd) >>
485 ((SIRFSOC_MAX_CMD_BYTES - t->len) * 8);
486 if (sspi->word_width == 2 && t->len == 4 &&
487 (!(spi->mode & SPI_LSB_FIRST)))
488 cmd = ((cmd & 0xffff) << 16) | (cmd >> 16);
Qipan Lie3fb57c2015-05-19 14:41:12 +0000489 writel(cmd, sspi->base + sspi->regs->spi_cmd);
Qipan Lic908ef32014-04-15 15:24:59 +0800490 writel(SIRFSOC_SPI_FRM_END_INT_EN,
Qipan Lie3fb57c2015-05-19 14:41:12 +0000491 sspi->base + sspi->regs->int_en);
Qipan Lic908ef32014-04-15 15:24:59 +0800492 writel(SIRFSOC_SPI_CMD_TX_EN,
Qipan Lie3fb57c2015-05-19 14:41:12 +0000493 sspi->base + sspi->regs->tx_rx_en);
Qipan Lic908ef32014-04-15 15:24:59 +0800494 if (wait_for_completion_timeout(&sspi->tx_done, timeout) == 0) {
495 dev_err(&spi->dev, "cmd transfer timeout\n");
Qipan Li0021d972014-09-02 17:01:04 +0800496 return;
Qipan Lieeb713952014-03-01 12:38:17 +0800497 }
Qipan Li0021d972014-09-02 17:01:04 +0800498 sspi->left_rx_word -= t->len;
Qipan Lic908ef32014-04-15 15:24:59 +0800499}
500
501static void spi_sirfsoc_dma_transfer(struct spi_device *spi,
502 struct spi_transfer *t)
503{
504 struct sirfsoc_spi *sspi;
505 struct dma_async_tx_descriptor *rx_desc, *tx_desc;
506 int timeout = t->len * 10;
507
508 sspi = spi_master_get_devdata(spi->master);
Qipan Lie3fb57c2015-05-19 14:41:12 +0000509 writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + sspi->regs->rxfifo_op);
510 writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + sspi->regs->txfifo_op);
511 switch (sspi->type) {
512 case SIRF_REAL_SPI:
513 writel(SIRFSOC_SPI_FIFO_START,
514 sspi->base + sspi->regs->rxfifo_op);
515 writel(SIRFSOC_SPI_FIFO_START,
516 sspi->base + sspi->regs->txfifo_op);
517 writel(0, sspi->base + sspi->regs->int_en);
518 break;
519 case SIRF_USP_SPI_P2:
520 writel(0x0, sspi->base + sspi->regs->rxfifo_op);
521 writel(0x0, sspi->base + sspi->regs->txfifo_op);
522 writel(0, sspi->base + sspi->regs->int_en);
523 break;
524 case SIRF_USP_SPI_A7:
525 writel(0x0, sspi->base + sspi->regs->rxfifo_op);
526 writel(0x0, sspi->base + sspi->regs->txfifo_op);
527 writel(~0UL, sspi->base + sspi->regs->usp_int_en_clr);
528 break;
529 }
530 writel(readl(sspi->base + sspi->regs->int_st),
531 sspi->base + sspi->regs->int_st);
532 if (sspi->left_tx_word < sspi->dat_max_frm_len) {
533 switch (sspi->type) {
534 case SIRF_REAL_SPI:
535 writel(readl(sspi->base + sspi->regs->spi_ctrl) |
536 SIRFSOC_SPI_ENA_AUTO_CLR |
537 SIRFSOC_SPI_MUL_DAT_MODE,
538 sspi->base + sspi->regs->spi_ctrl);
539 writel(sspi->left_tx_word - 1,
540 sspi->base + sspi->regs->tx_dma_io_len);
541 writel(sspi->left_tx_word - 1,
542 sspi->base + sspi->regs->rx_dma_io_len);
543 break;
544 case SIRF_USP_SPI_P2:
545 case SIRF_USP_SPI_A7:
546 /*USP simulate SPI, tx/rx_dma_io_len indicates bytes*/
547 writel(sspi->left_tx_word * sspi->word_width,
548 sspi->base + sspi->regs->tx_dma_io_len);
549 writel(sspi->left_tx_word * sspi->word_width,
550 sspi->base + sspi->regs->rx_dma_io_len);
551 break;
552 }
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800553 } else {
Qipan Lie3fb57c2015-05-19 14:41:12 +0000554 if (sspi->type == SIRF_REAL_SPI)
555 writel(readl(sspi->base + sspi->regs->spi_ctrl),
556 sspi->base + sspi->regs->spi_ctrl);
557 writel(0, sspi->base + sspi->regs->tx_dma_io_len);
558 writel(0, sspi->base + sspi->regs->rx_dma_io_len);
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800559 }
Qipan Lic908ef32014-04-15 15:24:59 +0800560 sspi->dst_start = dma_map_single(&spi->dev, sspi->rx, t->len,
561 (t->tx_buf != t->rx_buf) ?
562 DMA_FROM_DEVICE : DMA_BIDIRECTIONAL);
563 rx_desc = dmaengine_prep_slave_single(sspi->rx_chan,
564 sspi->dst_start, t->len, DMA_DEV_TO_MEM,
565 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
566 rx_desc->callback = spi_sirfsoc_dma_fini_callback;
567 rx_desc->callback_param = &sspi->rx_done;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800568
Qipan Lic908ef32014-04-15 15:24:59 +0800569 sspi->src_start = dma_map_single(&spi->dev, (void *)sspi->tx, t->len,
570 (t->tx_buf != t->rx_buf) ?
571 DMA_TO_DEVICE : DMA_BIDIRECTIONAL);
572 tx_desc = dmaengine_prep_slave_single(sspi->tx_chan,
573 sspi->src_start, t->len, DMA_MEM_TO_DEV,
574 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
575 tx_desc->callback = spi_sirfsoc_dma_fini_callback;
576 tx_desc->callback_param = &sspi->tx_done;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800577
Qipan Lic908ef32014-04-15 15:24:59 +0800578 dmaengine_submit(tx_desc);
579 dmaengine_submit(rx_desc);
580 dma_async_issue_pending(sspi->tx_chan);
581 dma_async_issue_pending(sspi->rx_chan);
Qipan Lid77ec5d2014-04-14 14:30:00 +0800582 writel(SIRFSOC_SPI_RX_EN | SIRFSOC_SPI_TX_EN,
Qipan Lie3fb57c2015-05-19 14:41:12 +0000583 sspi->base + sspi->regs->tx_rx_en);
584 if (sspi->type == SIRF_USP_SPI_P2 ||
585 sspi->type == SIRF_USP_SPI_A7) {
586 writel(SIRFSOC_SPI_FIFO_START,
587 sspi->base + sspi->regs->rxfifo_op);
588 writel(SIRFSOC_SPI_FIFO_START,
589 sspi->base + sspi->regs->txfifo_op);
590 }
Qipan Lic908ef32014-04-15 15:24:59 +0800591 if (wait_for_completion_timeout(&sspi->rx_done, timeout) == 0) {
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800592 dev_err(&spi->dev, "transfer timeout\n");
Barry Songde39f5f2013-08-06 14:21:21 +0800593 dmaengine_terminate_all(sspi->rx_chan);
594 } else
Qipan Li692fb0f2013-08-25 21:42:50 +0800595 sspi->left_rx_word = 0;
Barry Songde39f5f2013-08-06 14:21:21 +0800596 /*
597 * we only wait tx-done event if transferring by DMA. for PIO,
598 * we get rx data by writing tx data, so if rx is done, tx has
599 * done earlier
600 */
Qipan Lic908ef32014-04-15 15:24:59 +0800601 if (wait_for_completion_timeout(&sspi->tx_done, timeout) == 0) {
602 dev_err(&spi->dev, "transfer timeout\n");
Qipan Lie3fb57c2015-05-19 14:41:12 +0000603 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 dmaengine_terminate_all(sspi->tx_chan);
Barry Songde39f5f2013-08-06 14:21:21 +0800607 }
Qipan Lic908ef32014-04-15 15:24:59 +0800608 dma_unmap_single(&spi->dev, sspi->src_start, t->len, DMA_TO_DEVICE);
609 dma_unmap_single(&spi->dev, sspi->dst_start, t->len, DMA_FROM_DEVICE);
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800610 /* TX, RX FIFO stop */
Qipan Lie3fb57c2015-05-19 14:41:12 +0000611 writel(0, sspi->base + sspi->regs->rxfifo_op);
612 writel(0, sspi->base + sspi->regs->txfifo_op);
613 if (sspi->left_tx_word >= sspi->dat_max_frm_len)
614 writel(0, sspi->base + sspi->regs->tx_rx_en);
615 if (sspi->type == SIRF_USP_SPI_P2 ||
616 sspi->type == SIRF_USP_SPI_A7)
617 writel(0, sspi->base + sspi->regs->tx_rx_en);
Qipan Lic908ef32014-04-15 15:24:59 +0800618}
619
620static void spi_sirfsoc_pio_transfer(struct spi_device *spi,
621 struct spi_transfer *t)
622{
623 struct sirfsoc_spi *sspi;
624 int timeout = t->len * 10;
Qipan Lie3fb57c2015-05-19 14:41:12 +0000625 unsigned int data_units;
Qipan Lic908ef32014-04-15 15:24:59 +0800626
627 sspi = spi_master_get_devdata(spi->master);
Qipan Li41148c32014-05-04 14:32:36 +0800628 do {
629 writel(SIRFSOC_SPI_FIFO_RESET,
Qipan Lie3fb57c2015-05-19 14:41:12 +0000630 sspi->base + sspi->regs->rxfifo_op);
Qipan Li41148c32014-05-04 14:32:36 +0800631 writel(SIRFSOC_SPI_FIFO_RESET,
Qipan Lie3fb57c2015-05-19 14:41:12 +0000632 sspi->base + sspi->regs->txfifo_op);
633 switch (sspi->type) {
634 case SIRF_USP_SPI_P2:
635 writel(0x0, sspi->base + sspi->regs->rxfifo_op);
636 writel(0x0, sspi->base + sspi->regs->txfifo_op);
637 writel(0, sspi->base + sspi->regs->int_en);
638 writel(readl(sspi->base + sspi->regs->int_st),
639 sspi->base + sspi->regs->int_st);
640 writel(min((sspi->left_tx_word * sspi->word_width),
641 sspi->fifo_size),
642 sspi->base + sspi->regs->tx_dma_io_len);
643 writel(min((sspi->left_rx_word * sspi->word_width),
644 sspi->fifo_size),
645 sspi->base + sspi->regs->rx_dma_io_len);
646 break;
647 case SIRF_USP_SPI_A7:
648 writel(0x0, sspi->base + sspi->regs->rxfifo_op);
649 writel(0x0, sspi->base + sspi->regs->txfifo_op);
650 writel(~0UL, sspi->base + sspi->regs->usp_int_en_clr);
651 writel(readl(sspi->base + sspi->regs->int_st),
652 sspi->base + sspi->regs->int_st);
653 writel(min((sspi->left_tx_word * sspi->word_width),
654 sspi->fifo_size),
655 sspi->base + sspi->regs->tx_dma_io_len);
656 writel(min((sspi->left_rx_word * sspi->word_width),
657 sspi->fifo_size),
658 sspi->base + sspi->regs->rx_dma_io_len);
659 break;
660 case SIRF_REAL_SPI:
661 writel(SIRFSOC_SPI_FIFO_START,
662 sspi->base + sspi->regs->rxfifo_op);
663 writel(SIRFSOC_SPI_FIFO_START,
664 sspi->base + sspi->regs->txfifo_op);
665 writel(0, sspi->base + sspi->regs->int_en);
666 writel(readl(sspi->base + sspi->regs->int_st),
667 sspi->base + sspi->regs->int_st);
668 writel(readl(sspi->base + sspi->regs->spi_ctrl) |
669 SIRFSOC_SPI_MUL_DAT_MODE |
670 SIRFSOC_SPI_ENA_AUTO_CLR,
671 sspi->base + sspi->regs->spi_ctrl);
672 data_units = sspi->fifo_size / sspi->word_width;
673 writel(min(sspi->left_tx_word, data_units) - 1,
674 sspi->base + sspi->regs->tx_dma_io_len);
675 writel(min(sspi->left_rx_word, data_units) - 1,
676 sspi->base + sspi->regs->rx_dma_io_len);
677 break;
678 }
679 while (!((readl(sspi->base + sspi->regs->txfifo_st)
680 & SIRFSOC_SPI_FIFO_FULL_MASK(sspi))) &&
681 sspi->left_tx_word)
Qipan Li41148c32014-05-04 14:32:36 +0800682 sspi->tx_word(sspi);
683 writel(SIRFSOC_SPI_TXFIFO_EMPTY_INT_EN |
684 SIRFSOC_SPI_TX_UFLOW_INT_EN |
Qipan Lif2a08b42014-09-02 17:01:03 +0800685 SIRFSOC_SPI_RX_OFLOW_INT_EN |
686 SIRFSOC_SPI_RX_IO_DMA_INT_EN,
Qipan Lie3fb57c2015-05-19 14:41:12 +0000687 sspi->base + sspi->regs->int_en);
Qipan Li41148c32014-05-04 14:32:36 +0800688 writel(SIRFSOC_SPI_RX_EN | SIRFSOC_SPI_TX_EN,
Qipan Lie3fb57c2015-05-19 14:41:12 +0000689 sspi->base + sspi->regs->tx_rx_en);
690 if (sspi->type == SIRF_USP_SPI_P2 ||
691 sspi->type == SIRF_USP_SPI_A7) {
692 writel(SIRFSOC_SPI_FIFO_START,
693 sspi->base + sspi->regs->rxfifo_op);
694 writel(SIRFSOC_SPI_FIFO_START,
695 sspi->base + sspi->regs->txfifo_op);
696 }
Qipan Li41148c32014-05-04 14:32:36 +0800697 if (!wait_for_completion_timeout(&sspi->tx_done, timeout) ||
698 !wait_for_completion_timeout(&sspi->rx_done, timeout)) {
699 dev_err(&spi->dev, "transfer timeout\n");
Qipan Lie3fb57c2015-05-19 14:41:12 +0000700 if (sspi->type == SIRF_USP_SPI_P2 ||
701 sspi->type == SIRF_USP_SPI_A7)
702 writel(0, sspi->base + sspi->regs->tx_rx_en);
Qipan Li41148c32014-05-04 14:32:36 +0800703 break;
704 }
Qipan Lie3fb57c2015-05-19 14:41:12 +0000705 while (!((readl(sspi->base + sspi->regs->rxfifo_st)
706 & SIRFSOC_SPI_FIFO_EMPTY_MASK(sspi))) &&
707 sspi->left_rx_word)
Qipan Li41148c32014-05-04 14:32:36 +0800708 sspi->rx_word(sspi);
Qipan Lie3fb57c2015-05-19 14:41:12 +0000709 if (sspi->type == SIRF_USP_SPI_P2 ||
710 sspi->type == SIRF_USP_SPI_A7)
711 writel(0, sspi->base + sspi->regs->tx_rx_en);
712 writel(0, sspi->base + sspi->regs->rxfifo_op);
713 writel(0, sspi->base + sspi->regs->txfifo_op);
Qipan Li41148c32014-05-04 14:32:36 +0800714 } while (sspi->left_tx_word != 0 || sspi->left_rx_word != 0);
Qipan Lic908ef32014-04-15 15:24:59 +0800715}
716
717static int spi_sirfsoc_transfer(struct spi_device *spi, struct spi_transfer *t)
718{
719 struct sirfsoc_spi *sspi;
Qipan Lic908ef32014-04-15 15:24:59 +0800720
Qipan Lie3fb57c2015-05-19 14:41:12 +0000721 sspi = spi_master_get_devdata(spi->master);
Qipan Lic908ef32014-04-15 15:24:59 +0800722 sspi->tx = t->tx_buf ? t->tx_buf : sspi->dummypage;
723 sspi->rx = t->rx_buf ? t->rx_buf : sspi->dummypage;
724 sspi->left_tx_word = sspi->left_rx_word = t->len / sspi->word_width;
725 reinit_completion(&sspi->rx_done);
726 reinit_completion(&sspi->tx_done);
727 /*
728 * in the transfer, if transfer data using command register with rx_buf
729 * null, just fill command data into command register and wait for its
730 * completion.
731 */
Qipan Lie3fb57c2015-05-19 14:41:12 +0000732 if (sspi->type == SIRF_REAL_SPI && sspi->tx_by_cmd)
Qipan Lic908ef32014-04-15 15:24:59 +0800733 spi_sirfsoc_cmd_transfer(spi, t);
734 else if (IS_DMA_VALID(t))
735 spi_sirfsoc_dma_transfer(spi, t);
736 else
737 spi_sirfsoc_pio_transfer(spi, t);
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800738
Qipan Li692fb0f2013-08-25 21:42:50 +0800739 return t->len - sspi->left_rx_word * sspi->word_width;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800740}
741
742static void spi_sirfsoc_chipselect(struct spi_device *spi, int value)
743{
744 struct sirfsoc_spi *sspi = spi_master_get_devdata(spi->master);
745
Qipan Li7850cdf2014-09-02 17:01:01 +0800746 if (sspi->hw_cs) {
Qipan Lie3fb57c2015-05-19 14:41:12 +0000747 u32 regval;
748
749 switch (sspi->type) {
750 case SIRF_REAL_SPI:
751 regval = readl(sspi->base + sspi->regs->spi_ctrl);
752 switch (value) {
753 case BITBANG_CS_ACTIVE:
754 if (spi->mode & SPI_CS_HIGH)
755 regval |= SIRFSOC_SPI_CS_IO_OUT;
756 else
757 regval &= ~SIRFSOC_SPI_CS_IO_OUT;
758 break;
759 case BITBANG_CS_INACTIVE:
760 if (spi->mode & SPI_CS_HIGH)
761 regval &= ~SIRFSOC_SPI_CS_IO_OUT;
762 else
763 regval |= SIRFSOC_SPI_CS_IO_OUT;
764 break;
765 }
766 writel(regval, sspi->base + sspi->regs->spi_ctrl);
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800767 break;
Qipan Lie3fb57c2015-05-19 14:41:12 +0000768 case SIRF_USP_SPI_P2:
769 case SIRF_USP_SPI_A7:
770 regval = readl(sspi->base +
771 sspi->regs->usp_pin_io_data);
772 switch (value) {
773 case BITBANG_CS_ACTIVE:
774 if (spi->mode & SPI_CS_HIGH)
775 regval |= SIRFSOC_USP_CS_HIGH_VALUE;
776 else
777 regval &= ~(SIRFSOC_USP_CS_HIGH_VALUE);
778 break;
779 case BITBANG_CS_INACTIVE:
780 if (spi->mode & SPI_CS_HIGH)
781 regval &= ~(SIRFSOC_USP_CS_HIGH_VALUE);
782 else
783 regval |= SIRFSOC_USP_CS_HIGH_VALUE;
784 break;
785 }
786 writel(regval,
787 sspi->base + sspi->regs->usp_pin_io_data);
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800788 break;
789 }
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800790 } else {
Qipan Li6ee8a2f2014-04-14 14:29:59 +0800791 switch (value) {
792 case BITBANG_CS_ACTIVE:
Qipan Li7850cdf2014-09-02 17:01:01 +0800793 gpio_direction_output(spi->cs_gpio,
Qipan Li6ee8a2f2014-04-14 14:29:59 +0800794 spi->mode & SPI_CS_HIGH ? 1 : 0);
795 break;
796 case BITBANG_CS_INACTIVE:
Qipan Li7850cdf2014-09-02 17:01:01 +0800797 gpio_direction_output(spi->cs_gpio,
Qipan Li6ee8a2f2014-04-14 14:29:59 +0800798 spi->mode & SPI_CS_HIGH ? 0 : 1);
799 break;
800 }
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800801 }
802}
803
Qipan Lie3fb57c2015-05-19 14:41:12 +0000804static int spi_sirfsoc_config_mode(struct spi_device *spi)
805{
806 struct sirfsoc_spi *sspi;
807 u32 regval, usp_mode1;
808
809 sspi = spi_master_get_devdata(spi->master);
810 regval = readl(sspi->base + sspi->regs->spi_ctrl);
811 usp_mode1 = readl(sspi->base + sspi->regs->usp_mode1);
812 if (!(spi->mode & SPI_CS_HIGH)) {
813 regval |= SIRFSOC_SPI_CS_IDLE_STAT;
814 usp_mode1 &= ~SIRFSOC_USP_CS_HIGH_VALID;
815 } else {
816 regval &= ~SIRFSOC_SPI_CS_IDLE_STAT;
817 usp_mode1 |= SIRFSOC_USP_CS_HIGH_VALID;
818 }
819 if (!(spi->mode & SPI_LSB_FIRST)) {
820 regval |= SIRFSOC_SPI_TRAN_MSB;
821 usp_mode1 &= ~SIRFSOC_USP_LSB;
822 } else {
823 regval &= ~SIRFSOC_SPI_TRAN_MSB;
824 usp_mode1 |= SIRFSOC_USP_LSB;
825 }
826 if (spi->mode & SPI_CPOL) {
827 regval |= SIRFSOC_SPI_CLK_IDLE_STAT;
828 usp_mode1 |= SIRFSOC_USP_SCLK_IDLE_STAT;
829 } else {
830 regval &= ~SIRFSOC_SPI_CLK_IDLE_STAT;
831 usp_mode1 &= ~SIRFSOC_USP_SCLK_IDLE_STAT;
832 }
833 /*
834 * Data should be driven at least 1/2 cycle before the fetch edge
835 * to make sure that data gets stable at the fetch edge.
836 */
837 if (((spi->mode & SPI_CPOL) && (spi->mode & SPI_CPHA)) ||
838 (!(spi->mode & SPI_CPOL) && !(spi->mode & SPI_CPHA))) {
839 regval &= ~SIRFSOC_SPI_DRV_POS_EDGE;
840 usp_mode1 |= (SIRFSOC_USP_TXD_FALLING_EDGE |
841 SIRFSOC_USP_RXD_FALLING_EDGE);
842 } else {
843 regval |= SIRFSOC_SPI_DRV_POS_EDGE;
844 usp_mode1 &= ~(SIRFSOC_USP_RXD_FALLING_EDGE |
845 SIRFSOC_USP_TXD_FALLING_EDGE);
846 }
847 writel((SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(sspi, sspi->fifo_size - 2) <<
848 SIRFSOC_SPI_FIFO_SC_OFFSET) |
849 (SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(sspi, sspi->fifo_size / 2) <<
850 SIRFSOC_SPI_FIFO_LC_OFFSET) |
851 (SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(sspi, 2) <<
852 SIRFSOC_SPI_FIFO_HC_OFFSET),
853 sspi->base + sspi->regs->txfifo_level_chk);
854 writel((SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(sspi, 2) <<
855 SIRFSOC_SPI_FIFO_SC_OFFSET) |
856 (SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(sspi, sspi->fifo_size / 2) <<
857 SIRFSOC_SPI_FIFO_LC_OFFSET) |
858 (SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(sspi, sspi->fifo_size - 2) <<
859 SIRFSOC_SPI_FIFO_HC_OFFSET),
860 sspi->base + sspi->regs->rxfifo_level_chk);
861 /*
862 * it should never set to hardware cs mode because in hardware cs mode,
863 * cs signal can't controlled by driver.
864 */
865 switch (sspi->type) {
866 case SIRF_REAL_SPI:
867 regval |= SIRFSOC_SPI_CS_IO_MODE;
868 writel(regval, sspi->base + sspi->regs->spi_ctrl);
869 break;
870 case SIRF_USP_SPI_P2:
871 case SIRF_USP_SPI_A7:
872 usp_mode1 |= SIRFSOC_USP_SYNC_MODE;
873 usp_mode1 |= SIRFSOC_USP_TFS_IO_MODE;
874 usp_mode1 &= ~SIRFSOC_USP_TFS_IO_INPUT;
875 writel(usp_mode1, sspi->base + sspi->regs->usp_mode1);
876 break;
877 }
878
879 return 0;
880}
881
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800882static int
883spi_sirfsoc_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
884{
885 struct sirfsoc_spi *sspi;
886 u8 bits_per_word = 0;
887 int hz = 0;
Qipan Lie3fb57c2015-05-19 14:41:12 +0000888 u32 regval, txfifo_ctrl, rxfifo_ctrl, tx_frm_ctl, rx_frm_ctl, usp_mode2;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800889
890 sspi = spi_master_get_devdata(spi->master);
891
Laxman Dewangan766ed702012-12-18 14:25:43 +0530892 bits_per_word = (t) ? t->bits_per_word : spi->bits_per_word;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800893 hz = t && t->speed_hz ? t->speed_hz : spi->max_speed_hz;
894
Qipan Lie3fb57c2015-05-19 14:41:12 +0000895 usp_mode2 = regval = (sspi->ctrl_freq / (2 * hz)) - 1;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800896 if (regval > 0xFFFF || regval < 0) {
897 dev_err(&spi->dev, "Speed %d not supported\n", hz);
898 return -EINVAL;
899 }
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800900 switch (bits_per_word) {
901 case 8:
902 regval |= SIRFSOC_SPI_TRAN_DAT_FORMAT_8;
903 sspi->rx_word = spi_sirfsoc_rx_word_u8;
904 sspi->tx_word = spi_sirfsoc_tx_word_u8;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800905 break;
906 case 12:
907 case 16:
Qipan Lid77ec5d2014-04-14 14:30:00 +0800908 regval |= (bits_per_word == 12) ?
909 SIRFSOC_SPI_TRAN_DAT_FORMAT_12 :
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800910 SIRFSOC_SPI_TRAN_DAT_FORMAT_16;
911 sspi->rx_word = spi_sirfsoc_rx_word_u16;
912 sspi->tx_word = spi_sirfsoc_tx_word_u16;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800913 break;
914 case 32:
915 regval |= SIRFSOC_SPI_TRAN_DAT_FORMAT_32;
916 sspi->rx_word = spi_sirfsoc_rx_word_u32;
917 sspi->tx_word = spi_sirfsoc_tx_word_u32;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800918 break;
Arnd Bergmann804ae432013-06-03 15:24:53 +0200919 default:
Qipan Lif08654a2015-04-27 09:22:28 +0000920 dev_err(&spi->dev, "bpw %d not supported\n", bits_per_word);
921 return -EINVAL;
Zhiwu Song1cc2df92012-02-13 17:45:38 +0800922 }
Axel Lin8c328a22014-01-15 17:07:43 +0800923 sspi->word_width = DIV_ROUND_UP(bits_per_word, 8);
Qipan Lie3fb57c2015-05-19 14:41:12 +0000924 txfifo_ctrl = (((sspi->fifo_size / 2) &
925 SIRFSOC_SPI_FIFO_THD_MASK(sspi))
926 << SIRFSOC_SPI_FIFO_THD_OFFSET) |
927 (sspi->word_width >> 1);
928 rxfifo_ctrl = (((sspi->fifo_size / 2) &
929 SIRFSOC_SPI_FIFO_THD_MASK(sspi))
930 << SIRFSOC_SPI_FIFO_THD_OFFSET) |
931 (sspi->word_width >> 1);
932 writel(txfifo_ctrl, sspi->base + sspi->regs->txfifo_ctrl);
933 writel(rxfifo_ctrl, sspi->base + sspi->regs->rxfifo_ctrl);
934 if (sspi->type == SIRF_USP_SPI_P2 ||
935 sspi->type == SIRF_USP_SPI_A7) {
936 tx_frm_ctl = 0;
937 tx_frm_ctl |= ((bits_per_word - 1) & SIRFSOC_USP_TX_DATA_MASK)
938 << SIRFSOC_USP_TX_DATA_OFFSET;
939 tx_frm_ctl |= ((bits_per_word + 1 + SIRFSOC_USP_TXD_DELAY_LEN
940 - 1) & SIRFSOC_USP_TX_SYNC_MASK) <<
941 SIRFSOC_USP_TX_SYNC_OFFSET;
942 tx_frm_ctl |= ((bits_per_word + 1 + SIRFSOC_USP_TXD_DELAY_LEN
943 + 2 - 1) & SIRFSOC_USP_TX_FRAME_MASK) <<
944 SIRFSOC_USP_TX_FRAME_OFFSET;
945 tx_frm_ctl |= ((bits_per_word - 1) &
946 SIRFSOC_USP_TX_SHIFTER_MASK) <<
947 SIRFSOC_USP_TX_SHIFTER_OFFSET;
948 rx_frm_ctl = 0;
949 rx_frm_ctl |= ((bits_per_word - 1) & SIRFSOC_USP_RX_DATA_MASK)
950 << SIRFSOC_USP_RX_DATA_OFFSET;
951 rx_frm_ctl |= ((bits_per_word + 1 + SIRFSOC_USP_RXD_DELAY_LEN
952 + 2 - 1) & SIRFSOC_USP_RX_FRAME_MASK) <<
953 SIRFSOC_USP_RX_FRAME_OFFSET;
954 rx_frm_ctl |= ((bits_per_word - 1)
955 & SIRFSOC_USP_RX_SHIFTER_MASK) <<
956 SIRFSOC_USP_RX_SHIFTER_OFFSET;
957 writel(tx_frm_ctl | (((usp_mode2 >> 10) &
958 SIRFSOC_USP_CLK_10_11_MASK) <<
959 SIRFSOC_USP_CLK_10_11_OFFSET),
960 sspi->base + sspi->regs->usp_tx_frame_ctrl);
961 writel(rx_frm_ctl | (((usp_mode2 >> 12) &
962 SIRFSOC_USP_CLK_12_15_MASK) <<
963 SIRFSOC_USP_CLK_12_15_OFFSET),
964 sspi->base + sspi->regs->usp_rx_frame_ctrl);
965 writel(readl(sspi->base + sspi->regs->usp_mode2) |
966 ((usp_mode2 & SIRFSOC_USP_CLK_DIVISOR_MASK) <<
967 SIRFSOC_USP_CLK_DIVISOR_OFFSET) |
968 (SIRFSOC_USP_RXD_DELAY_LEN <<
969 SIRFSOC_USP_RXD_DELAY_OFFSET) |
970 (SIRFSOC_USP_TXD_DELAY_LEN <<
971 SIRFSOC_USP_TXD_DELAY_OFFSET),
972 sspi->base + sspi->regs->usp_mode2);
Qipan Lieeb713952014-03-01 12:38:17 +0800973 }
Qipan Lie3fb57c2015-05-19 14:41:12 +0000974 if (sspi->type == SIRF_REAL_SPI)
975 writel(regval, sspi->base + sspi->regs->spi_ctrl);
976 spi_sirfsoc_config_mode(spi);
977 if (sspi->type == SIRF_REAL_SPI) {
978 if (t && t->tx_buf && !t->rx_buf &&
979 (t->len <= SIRFSOC_MAX_CMD_BYTES)) {
980 sspi->tx_by_cmd = true;
981 writel(readl(sspi->base + sspi->regs->spi_ctrl) |
982 (SIRFSOC_SPI_CMD_BYTE_NUM((t->len - 1)) |
983 SIRFSOC_SPI_CMD_MODE),
984 sspi->base + sspi->regs->spi_ctrl);
985 } else {
986 sspi->tx_by_cmd = false;
987 writel(readl(sspi->base + sspi->regs->spi_ctrl) &
988 ~SIRFSOC_SPI_CMD_MODE,
989 sspi->base + sspi->regs->spi_ctrl);
990 }
991 }
Barry Songde39f5f2013-08-06 14:21:21 +0800992 if (IS_DMA_VALID(t)) {
993 /* Enable DMA mode for RX, TX */
Qipan Lie3fb57c2015-05-19 14:41:12 +0000994 writel(0, sspi->base + sspi->regs->tx_dma_io_ctrl);
Qipan Lid77ec5d2014-04-14 14:30:00 +0800995 writel(SIRFSOC_SPI_RX_DMA_FLUSH,
Qipan Lie3fb57c2015-05-19 14:41:12 +0000996 sspi->base + sspi->regs->rx_dma_io_ctrl);
Barry Songde39f5f2013-08-06 14:21:21 +0800997 } else {
998 /* Enable IO mode for RX, TX */
Qipan Lid77ec5d2014-04-14 14:30:00 +0800999 writel(SIRFSOC_SPI_IO_MODE_SEL,
Qipan Lie3fb57c2015-05-19 14:41:12 +00001000 sspi->base + sspi->regs->tx_dma_io_ctrl);
Qipan Lid77ec5d2014-04-14 14:30:00 +08001001 writel(SIRFSOC_SPI_IO_MODE_SEL,
Qipan Lie3fb57c2015-05-19 14:41:12 +00001002 sspi->base + sspi->regs->rx_dma_io_ctrl);
Barry Songde39f5f2013-08-06 14:21:21 +08001003 }
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001004 return 0;
1005}
1006
1007static int spi_sirfsoc_setup(struct spi_device *spi)
1008{
Qipan Li7850cdf2014-09-02 17:01:01 +08001009 struct sirfsoc_spi *sspi;
Qipan Li96bf4012015-05-03 10:30:12 +00001010 int ret = 0;
Qipan Li7850cdf2014-09-02 17:01:01 +08001011
Qipan Li7850cdf2014-09-02 17:01:01 +08001012 sspi = spi_master_get_devdata(spi->master);
Qipan Li7850cdf2014-09-02 17:01:01 +08001013 if (spi->cs_gpio == -ENOENT)
1014 sspi->hw_cs = true;
Qipan Li96bf4012015-05-03 10:30:12 +00001015 else {
Qipan Li7850cdf2014-09-02 17:01:01 +08001016 sspi->hw_cs = false;
Qipan Li96bf4012015-05-03 10:30:12 +00001017 if (!spi_get_ctldata(spi)) {
1018 void *cs = kmalloc(sizeof(int), GFP_KERNEL);
1019 if (!cs) {
1020 ret = -ENOMEM;
1021 goto exit;
1022 }
1023 ret = gpio_is_valid(spi->cs_gpio);
1024 if (!ret) {
1025 dev_err(&spi->dev, "no valid gpio\n");
1026 ret = -ENOENT;
1027 goto exit;
1028 }
1029 ret = gpio_request(spi->cs_gpio, DRIVER_NAME);
1030 if (ret) {
1031 dev_err(&spi->dev, "failed to request gpio\n");
1032 goto exit;
1033 }
1034 spi_set_ctldata(spi, cs);
1035 }
1036 }
Qipan Lie3fb57c2015-05-19 14:41:12 +00001037 spi_sirfsoc_config_mode(spi);
Qipan Li96bf4012015-05-03 10:30:12 +00001038 spi_sirfsoc_chipselect(spi, BITBANG_CS_INACTIVE);
1039exit:
1040 return ret;
1041}
1042
1043static void spi_sirfsoc_cleanup(struct spi_device *spi)
1044{
1045 if (spi_get_ctldata(spi)) {
1046 gpio_free(spi->cs_gpio);
1047 kfree(spi_get_ctldata(spi));
1048 }
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001049}
1050
Qipan Lie3fb57c2015-05-19 14:41:12 +00001051static const struct of_device_id spi_sirfsoc_of_match[] = {
1052 { .compatible = "sirf,prima2-spi", .data = &sirf_real_spi},
1053 { .compatible = "sirf,prima2-usp-spi", .data = &sirf_usp_spi_p2},
1054 { .compatible = "sirf,atlas7-usp-spi", .data = &sirf_usp_spi_a7},
1055 {}
1056};
1057MODULE_DEVICE_TABLE(of, spi_sirfsoc_of_match);
1058
Grant Likelyfd4a3192012-12-07 16:57:14 +00001059static int spi_sirfsoc_probe(struct platform_device *pdev)
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001060{
1061 struct sirfsoc_spi *sspi;
1062 struct spi_master *master;
1063 struct resource *mem_res;
Qipan Lie3fb57c2015-05-19 14:41:12 +00001064 struct sirf_spi_comp_data *spi_comp_data;
Qipan Li7850cdf2014-09-02 17:01:01 +08001065 int irq;
Qipan Li96bf4012015-05-03 10:30:12 +00001066 int ret;
Qipan Lie3fb57c2015-05-19 14:41:12 +00001067 const struct of_device_id *match;
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001068
Qipan Li8509c552014-11-20 22:33:07 +08001069 ret = device_reset(&pdev->dev);
1070 if (ret) {
1071 dev_err(&pdev->dev, "SPI reset failed!\n");
1072 return ret;
1073 }
1074
Qipan Li7850cdf2014-09-02 17:01:01 +08001075 master = spi_alloc_master(&pdev->dev, sizeof(*sspi));
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001076 if (!master) {
1077 dev_err(&pdev->dev, "Unable to allocate SPI master\n");
1078 return -ENOMEM;
1079 }
Qipan Lie3fb57c2015-05-19 14:41:12 +00001080 match = of_match_node(spi_sirfsoc_of_match, pdev->dev.of_node);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001081 platform_set_drvdata(pdev, master);
1082 sspi = spi_master_get_devdata(master);
Qipan Lie3fb57c2015-05-19 14:41:12 +00001083 sspi->fifo_full_offset = ilog2(sspi->fifo_size);
1084 spi_comp_data = (struct sirf_spi_comp_data *)match->data;
1085 sspi->regs = spi_comp_data->regs;
1086 sspi->type = spi_comp_data->type;
1087 sspi->fifo_level_chk_mask = (sspi->fifo_size / 4) - 1;
1088 sspi->dat_max_frm_len = spi_comp_data->dat_max_frm_len;
1089 sspi->fifo_size = spi_comp_data->fifo_size;
Julia Lawall24797902013-08-14 11:11:29 +02001090 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingb0ee5602013-01-21 11:09:18 +01001091 sspi->base = devm_ioremap_resource(&pdev->dev, mem_res);
1092 if (IS_ERR(sspi->base)) {
1093 ret = PTR_ERR(sspi->base);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001094 goto free_master;
1095 }
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001096 irq = platform_get_irq(pdev, 0);
1097 if (irq < 0) {
1098 ret = -ENXIO;
1099 goto free_master;
1100 }
1101 ret = devm_request_irq(&pdev->dev, irq, spi_sirfsoc_irq, 0,
1102 DRIVER_NAME, sspi);
1103 if (ret)
1104 goto free_master;
1105
Axel Lin94c69f72013-09-10 15:43:41 +08001106 sspi->bitbang.master = master;
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001107 sspi->bitbang.chipselect = spi_sirfsoc_chipselect;
1108 sspi->bitbang.setup_transfer = spi_sirfsoc_setup_transfer;
1109 sspi->bitbang.txrx_bufs = spi_sirfsoc_transfer;
1110 sspi->bitbang.master->setup = spi_sirfsoc_setup;
Qipan Li96bf4012015-05-03 10:30:12 +00001111 sspi->bitbang.master->cleanup = spi_sirfsoc_cleanup;
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001112 master->bus_num = pdev->id;
Qipan Li94b1f0d2013-06-25 19:45:29 +08001113 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_CS_HIGH;
Stephen Warren24778be2013-05-21 20:36:35 -06001114 master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(12) |
1115 SPI_BPW_MASK(16) | SPI_BPW_MASK(32);
Qipan Lifcc50e52014-11-17 23:17:03 +08001116 master->max_speed_hz = SIRFSOC_SPI_DEFAULT_FRQ;
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001117 sspi->bitbang.master->dev.of_node = pdev->dev.of_node;
1118
Barry Songde39f5f2013-08-06 14:21:21 +08001119 /* request DMA channels */
Barry Songdd7243d2014-02-13 00:30:19 +08001120 sspi->rx_chan = dma_request_slave_channel(&pdev->dev, "rx");
Barry Songde39f5f2013-08-06 14:21:21 +08001121 if (!sspi->rx_chan) {
1122 dev_err(&pdev->dev, "can not allocate rx dma channel\n");
Wei Yongjun6cca9e22013-08-23 08:33:39 +08001123 ret = -ENODEV;
Barry Songde39f5f2013-08-06 14:21:21 +08001124 goto free_master;
1125 }
Barry Songdd7243d2014-02-13 00:30:19 +08001126 sspi->tx_chan = dma_request_slave_channel(&pdev->dev, "tx");
Barry Songde39f5f2013-08-06 14:21:21 +08001127 if (!sspi->tx_chan) {
1128 dev_err(&pdev->dev, "can not allocate tx dma channel\n");
Wei Yongjun6cca9e22013-08-23 08:33:39 +08001129 ret = -ENODEV;
Barry Songde39f5f2013-08-06 14:21:21 +08001130 goto free_rx_dma;
1131 }
1132
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001133 sspi->clk = clk_get(&pdev->dev, NULL);
1134 if (IS_ERR(sspi->clk)) {
Barry Songde39f5f2013-08-06 14:21:21 +08001135 ret = PTR_ERR(sspi->clk);
1136 goto free_tx_dma;
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001137 }
Barry Songe5118cd2012-12-26 10:48:33 +08001138 clk_prepare_enable(sspi->clk);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001139 sspi->ctrl_freq = clk_get_rate(sspi->clk);
1140
Barry Songde39f5f2013-08-06 14:21:21 +08001141 init_completion(&sspi->rx_done);
1142 init_completion(&sspi->tx_done);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001143
Qipan Lia34bcbe2015-05-07 07:13:10 +00001144 sspi->dummypage = devm_kzalloc(&pdev->dev, 2 * PAGE_SIZE, GFP_KERNEL);
Wei Yongjun6cca9e22013-08-23 08:33:39 +08001145 if (!sspi->dummypage) {
1146 ret = -ENOMEM;
Barry Songde39f5f2013-08-06 14:21:21 +08001147 goto free_clk;
Wei Yongjun6cca9e22013-08-23 08:33:39 +08001148 }
Barry Songde39f5f2013-08-06 14:21:21 +08001149
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001150 ret = spi_bitbang_start(&sspi->bitbang);
1151 if (ret)
Qipan Lia34bcbe2015-05-07 07:13:10 +00001152 goto free_clk;
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001153 dev_info(&pdev->dev, "registerred, bus number = %d\n", master->bus_num);
1154
1155 return 0;
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001156free_clk:
Barry Songe5118cd2012-12-26 10:48:33 +08001157 clk_disable_unprepare(sspi->clk);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001158 clk_put(sspi->clk);
Barry Songde39f5f2013-08-06 14:21:21 +08001159free_tx_dma:
1160 dma_release_channel(sspi->tx_chan);
1161free_rx_dma:
1162 dma_release_channel(sspi->rx_chan);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001163free_master:
1164 spi_master_put(master);
Qipan Li7850cdf2014-09-02 17:01:01 +08001165
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001166 return ret;
1167}
1168
Grant Likelyfd4a3192012-12-07 16:57:14 +00001169static int spi_sirfsoc_remove(struct platform_device *pdev)
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001170{
1171 struct spi_master *master;
1172 struct sirfsoc_spi *sspi;
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001173
1174 master = platform_get_drvdata(pdev);
1175 sspi = spi_master_get_devdata(master);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001176 spi_bitbang_stop(&sspi->bitbang);
Barry Songe5118cd2012-12-26 10:48:33 +08001177 clk_disable_unprepare(sspi->clk);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001178 clk_put(sspi->clk);
Barry Songde39f5f2013-08-06 14:21:21 +08001179 dma_release_channel(sspi->rx_chan);
1180 dma_release_channel(sspi->tx_chan);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001181 spi_master_put(master);
1182 return 0;
1183}
1184
Qipan Lifacffed2014-02-13 00:30:20 +08001185#ifdef CONFIG_PM_SLEEP
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001186static int spi_sirfsoc_suspend(struct device *dev)
1187{
Axel Lina12163942013-08-09 15:35:16 +08001188 struct spi_master *master = dev_get_drvdata(dev);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001189 struct sirfsoc_spi *sspi = spi_master_get_devdata(master);
Axel Lina82ba3a2014-03-05 15:19:09 +08001190 int ret;
1191
1192 ret = spi_master_suspend(master);
1193 if (ret)
1194 return ret;
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001195
1196 clk_disable(sspi->clk);
1197 return 0;
1198}
1199
1200static int spi_sirfsoc_resume(struct device *dev)
1201{
Axel Lina12163942013-08-09 15:35:16 +08001202 struct spi_master *master = dev_get_drvdata(dev);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001203 struct sirfsoc_spi *sspi = spi_master_get_devdata(master);
1204
1205 clk_enable(sspi->clk);
Qipan Lie3fb57c2015-05-19 14:41:12 +00001206 writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + sspi->regs->txfifo_op);
1207 writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + sspi->regs->rxfifo_op);
1208 writel(SIRFSOC_SPI_FIFO_START, sspi->base + sspi->regs->txfifo_op);
1209 writel(SIRFSOC_SPI_FIFO_START, sspi->base + sspi->regs->rxfifo_op);
1210 return 0;
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001211}
Qipan Lifacffed2014-02-13 00:30:20 +08001212#endif
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001213
Jingoo Han71aa2e32014-02-26 10:32:48 +09001214static SIMPLE_DEV_PM_OPS(spi_sirfsoc_pm_ops, spi_sirfsoc_suspend,
1215 spi_sirfsoc_resume);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001216
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001217static struct platform_driver spi_sirfsoc_driver = {
1218 .driver = {
1219 .name = DRIVER_NAME,
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001220 .pm = &spi_sirfsoc_pm_ops,
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001221 .of_match_table = spi_sirfsoc_of_match,
1222 },
1223 .probe = spi_sirfsoc_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001224 .remove = spi_sirfsoc_remove,
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001225};
1226module_platform_driver(spi_sirfsoc_driver);
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001227MODULE_DESCRIPTION("SiRF SoC SPI master driver");
Qipan Lid77ec5d2014-04-14 14:30:00 +08001228MODULE_AUTHOR("Zhiwu Song <Zhiwu.Song@csr.com>");
1229MODULE_AUTHOR("Barry Song <Baohua.Song@csr.com>");
Qipan Lie3fb57c2015-05-19 14:41:12 +00001230MODULE_AUTHOR("Qipan Li <Qipan.Li@csr.com>");
Zhiwu Song1cc2df92012-02-13 17:45:38 +08001231MODULE_LICENSE("GPL v2");