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