f32b83c7209f2efb6c90ff5af53da14eedbd6b5f
[muen/linux.git] / drivers / spi / spi-armada-3700.c
1 /*
2  * Marvell Armada-3700 SPI controller driver
3  *
4  * Copyright (C) 2016 Marvell Ltd.
5  *
6  * Author: Wilson Ding <dingwei@marvell.com>
7  * Author: Romain Perier <romain.perier@free-electrons.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include <linux/clk.h>
15 #include <linux/completion.h>
16 #include <linux/delay.h>
17 #include <linux/err.h>
18 #include <linux/interrupt.h>
19 #include <linux/io.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/of_irq.h>
24 #include <linux/of_device.h>
25 #include <linux/pinctrl/consumer.h>
26 #include <linux/spi/spi.h>
27
28 #define DRIVER_NAME                     "armada_3700_spi"
29
30 #define A3700_SPI_MAX_SPEED_HZ          100000000
31 #define A3700_SPI_MAX_PRESCALE          30
32 #define A3700_SPI_TIMEOUT               10
33
34 /* SPI Register Offest */
35 #define A3700_SPI_IF_CTRL_REG           0x00
36 #define A3700_SPI_IF_CFG_REG            0x04
37 #define A3700_SPI_DATA_OUT_REG          0x08
38 #define A3700_SPI_DATA_IN_REG           0x0C
39 #define A3700_SPI_IF_INST_REG           0x10
40 #define A3700_SPI_IF_ADDR_REG           0x14
41 #define A3700_SPI_IF_RMODE_REG          0x18
42 #define A3700_SPI_IF_HDR_CNT_REG        0x1C
43 #define A3700_SPI_IF_DIN_CNT_REG        0x20
44 #define A3700_SPI_IF_TIME_REG           0x24
45 #define A3700_SPI_INT_STAT_REG          0x28
46 #define A3700_SPI_INT_MASK_REG          0x2C
47
48 /* A3700_SPI_IF_CTRL_REG */
49 #define A3700_SPI_EN                    BIT(16)
50 #define A3700_SPI_ADDR_NOT_CONFIG       BIT(12)
51 #define A3700_SPI_WFIFO_OVERFLOW        BIT(11)
52 #define A3700_SPI_WFIFO_UNDERFLOW       BIT(10)
53 #define A3700_SPI_RFIFO_OVERFLOW        BIT(9)
54 #define A3700_SPI_RFIFO_UNDERFLOW       BIT(8)
55 #define A3700_SPI_WFIFO_FULL            BIT(7)
56 #define A3700_SPI_WFIFO_EMPTY           BIT(6)
57 #define A3700_SPI_RFIFO_FULL            BIT(5)
58 #define A3700_SPI_RFIFO_EMPTY           BIT(4)
59 #define A3700_SPI_WFIFO_RDY             BIT(3)
60 #define A3700_SPI_RFIFO_RDY             BIT(2)
61 #define A3700_SPI_XFER_RDY              BIT(1)
62 #define A3700_SPI_XFER_DONE             BIT(0)
63
64 /* A3700_SPI_IF_CFG_REG */
65 #define A3700_SPI_WFIFO_THRS            BIT(28)
66 #define A3700_SPI_RFIFO_THRS            BIT(24)
67 #define A3700_SPI_AUTO_CS               BIT(20)
68 #define A3700_SPI_DMA_RD_EN             BIT(18)
69 #define A3700_SPI_FIFO_MODE             BIT(17)
70 #define A3700_SPI_SRST                  BIT(16)
71 #define A3700_SPI_XFER_START            BIT(15)
72 #define A3700_SPI_XFER_STOP             BIT(14)
73 #define A3700_SPI_INST_PIN              BIT(13)
74 #define A3700_SPI_ADDR_PIN              BIT(12)
75 #define A3700_SPI_DATA_PIN1             BIT(11)
76 #define A3700_SPI_DATA_PIN0             BIT(10)
77 #define A3700_SPI_FIFO_FLUSH            BIT(9)
78 #define A3700_SPI_RW_EN                 BIT(8)
79 #define A3700_SPI_CLK_POL               BIT(7)
80 #define A3700_SPI_CLK_PHA               BIT(6)
81 #define A3700_SPI_BYTE_LEN              BIT(5)
82 #define A3700_SPI_CLK_PRESCALE          BIT(0)
83 #define A3700_SPI_CLK_PRESCALE_MASK     (0x1f)
84
85 #define A3700_SPI_WFIFO_THRS_BIT        28
86 #define A3700_SPI_RFIFO_THRS_BIT        24
87 #define A3700_SPI_FIFO_THRS_MASK        0x7
88
89 #define A3700_SPI_DATA_PIN_MASK         0x3
90
91 /* A3700_SPI_IF_HDR_CNT_REG */
92 #define A3700_SPI_DUMMY_CNT_BIT         12
93 #define A3700_SPI_DUMMY_CNT_MASK        0x7
94 #define A3700_SPI_RMODE_CNT_BIT         8
95 #define A3700_SPI_RMODE_CNT_MASK        0x3
96 #define A3700_SPI_ADDR_CNT_BIT          4
97 #define A3700_SPI_ADDR_CNT_MASK         0x7
98 #define A3700_SPI_INSTR_CNT_BIT         0
99 #define A3700_SPI_INSTR_CNT_MASK        0x3
100
101 /* A3700_SPI_IF_TIME_REG */
102 #define A3700_SPI_CLK_CAPT_EDGE         BIT(7)
103
104 struct a3700_spi {
105         struct spi_master *master;
106         void __iomem *base;
107         struct clk *clk;
108         unsigned int irq;
109         unsigned int flags;
110         bool xmit_data;
111         const u8 *tx_buf;
112         u8 *rx_buf;
113         size_t buf_len;
114         u8 byte_len;
115         u32 wait_mask;
116         struct completion done;
117 };
118
119 static u32 spireg_read(struct a3700_spi *a3700_spi, u32 offset)
120 {
121         return readl(a3700_spi->base + offset);
122 }
123
124 static void spireg_write(struct a3700_spi *a3700_spi, u32 offset, u32 data)
125 {
126         writel(data, a3700_spi->base + offset);
127 }
128
129 static void a3700_spi_auto_cs_unset(struct a3700_spi *a3700_spi)
130 {
131         u32 val;
132
133         val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
134         val &= ~A3700_SPI_AUTO_CS;
135         spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
136 }
137
138 static void a3700_spi_activate_cs(struct a3700_spi *a3700_spi, unsigned int cs)
139 {
140         u32 val;
141
142         val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
143         val |= (A3700_SPI_EN << cs);
144         spireg_write(a3700_spi, A3700_SPI_IF_CTRL_REG, val);
145 }
146
147 static void a3700_spi_deactivate_cs(struct a3700_spi *a3700_spi,
148                                     unsigned int cs)
149 {
150         u32 val;
151
152         val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
153         val &= ~(A3700_SPI_EN << cs);
154         spireg_write(a3700_spi, A3700_SPI_IF_CTRL_REG, val);
155 }
156
157 static int a3700_spi_pin_mode_set(struct a3700_spi *a3700_spi,
158                                   unsigned int pin_mode, bool receiving)
159 {
160         u32 val;
161
162         val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
163         val &= ~(A3700_SPI_INST_PIN | A3700_SPI_ADDR_PIN);
164         val &= ~(A3700_SPI_DATA_PIN0 | A3700_SPI_DATA_PIN1);
165
166         switch (pin_mode) {
167         case SPI_NBITS_SINGLE:
168                 break;
169         case SPI_NBITS_DUAL:
170                 val |= A3700_SPI_DATA_PIN0;
171                 break;
172         case SPI_NBITS_QUAD:
173                 val |= A3700_SPI_DATA_PIN1;
174                 /* RX during address reception uses 4-pin */
175                 if (receiving)
176                         val |= A3700_SPI_ADDR_PIN;
177                 break;
178         default:
179                 dev_err(&a3700_spi->master->dev, "wrong pin mode %u", pin_mode);
180                 return -EINVAL;
181         }
182
183         spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
184
185         return 0;
186 }
187
188 static void a3700_spi_fifo_mode_set(struct a3700_spi *a3700_spi, bool enable)
189 {
190         u32 val;
191
192         val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
193         if (enable)
194                 val |= A3700_SPI_FIFO_MODE;
195         else
196                 val &= ~A3700_SPI_FIFO_MODE;
197         spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
198 }
199
200 static void a3700_spi_mode_set(struct a3700_spi *a3700_spi,
201                                unsigned int mode_bits)
202 {
203         u32 val;
204
205         val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
206
207         if (mode_bits & SPI_CPOL)
208                 val |= A3700_SPI_CLK_POL;
209         else
210                 val &= ~A3700_SPI_CLK_POL;
211
212         if (mode_bits & SPI_CPHA)
213                 val |= A3700_SPI_CLK_PHA;
214         else
215                 val &= ~A3700_SPI_CLK_PHA;
216
217         spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
218 }
219
220 static void a3700_spi_clock_set(struct a3700_spi *a3700_spi,
221                                 unsigned int speed_hz)
222 {
223         u32 val;
224         u32 prescale;
225
226         prescale = DIV_ROUND_UP(clk_get_rate(a3700_spi->clk), speed_hz);
227
228         val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
229         val = val & ~A3700_SPI_CLK_PRESCALE_MASK;
230
231         val = val | (prescale & A3700_SPI_CLK_PRESCALE_MASK);
232         spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
233
234         if (prescale <= 2) {
235                 val = spireg_read(a3700_spi, A3700_SPI_IF_TIME_REG);
236                 val |= A3700_SPI_CLK_CAPT_EDGE;
237                 spireg_write(a3700_spi, A3700_SPI_IF_TIME_REG, val);
238         }
239 }
240
241 static void a3700_spi_bytelen_set(struct a3700_spi *a3700_spi, unsigned int len)
242 {
243         u32 val;
244
245         val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
246         if (len == 4)
247                 val |= A3700_SPI_BYTE_LEN;
248         else
249                 val &= ~A3700_SPI_BYTE_LEN;
250         spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
251
252         a3700_spi->byte_len = len;
253 }
254
255 static int a3700_spi_fifo_flush(struct a3700_spi *a3700_spi)
256 {
257         int timeout = A3700_SPI_TIMEOUT;
258         u32 val;
259
260         val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
261         val |= A3700_SPI_FIFO_FLUSH;
262         spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
263
264         while (--timeout) {
265                 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
266                 if (!(val & A3700_SPI_FIFO_FLUSH))
267                         return 0;
268                 udelay(1);
269         }
270
271         return -ETIMEDOUT;
272 }
273
274 static int a3700_spi_init(struct a3700_spi *a3700_spi)
275 {
276         struct spi_master *master = a3700_spi->master;
277         u32 val;
278         int i, ret = 0;
279
280         /* Reset SPI unit */
281         val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
282         val |= A3700_SPI_SRST;
283         spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
284
285         udelay(A3700_SPI_TIMEOUT);
286
287         val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
288         val &= ~A3700_SPI_SRST;
289         spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
290
291         /* Disable AUTO_CS and deactivate all chip-selects */
292         a3700_spi_auto_cs_unset(a3700_spi);
293         for (i = 0; i < master->num_chipselect; i++)
294                 a3700_spi_deactivate_cs(a3700_spi, i);
295
296         /* Enable FIFO mode */
297         a3700_spi_fifo_mode_set(a3700_spi, true);
298
299         /* Set SPI mode */
300         a3700_spi_mode_set(a3700_spi, master->mode_bits);
301
302         /* Reset counters */
303         spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, 0);
304         spireg_write(a3700_spi, A3700_SPI_IF_DIN_CNT_REG, 0);
305
306         /* Mask the interrupts and clear cause bits */
307         spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0);
308         spireg_write(a3700_spi, A3700_SPI_INT_STAT_REG, ~0U);
309
310         return ret;
311 }
312
313 static irqreturn_t a3700_spi_interrupt(int irq, void *dev_id)
314 {
315         struct spi_master *master = dev_id;
316         struct a3700_spi *a3700_spi;
317         u32 cause;
318
319         a3700_spi = spi_master_get_devdata(master);
320
321         /* Get interrupt causes */
322         cause = spireg_read(a3700_spi, A3700_SPI_INT_STAT_REG);
323
324         if (!cause || !(a3700_spi->wait_mask & cause))
325                 return IRQ_NONE;
326
327         /* mask and acknowledge the SPI interrupts */
328         spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0);
329         spireg_write(a3700_spi, A3700_SPI_INT_STAT_REG, cause);
330
331         /* Wake up the transfer */
332         complete(&a3700_spi->done);
333
334         return IRQ_HANDLED;
335 }
336
337 static bool a3700_spi_wait_completion(struct spi_device *spi)
338 {
339         struct a3700_spi *a3700_spi;
340         unsigned int timeout;
341         unsigned int ctrl_reg;
342         unsigned long timeout_jiffies;
343
344         a3700_spi = spi_master_get_devdata(spi->master);
345
346         /* SPI interrupt is edge-triggered, which means an interrupt will
347          * be generated only when detecting a specific status bit changed
348          * from '0' to '1'. So when we start waiting for a interrupt, we
349          * need to check status bit in control reg first, if it is already 1,
350          * then we do not need to wait for interrupt
351          */
352         ctrl_reg = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
353         if (a3700_spi->wait_mask & ctrl_reg)
354                 return true;
355
356         reinit_completion(&a3700_spi->done);
357
358         spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG,
359                      a3700_spi->wait_mask);
360
361         timeout_jiffies = msecs_to_jiffies(A3700_SPI_TIMEOUT);
362         timeout = wait_for_completion_timeout(&a3700_spi->done,
363                                               timeout_jiffies);
364
365         a3700_spi->wait_mask = 0;
366
367         if (timeout)
368                 return true;
369
370         /* there might be the case that right after we checked the
371          * status bits in this routine and before start to wait for
372          * interrupt by wait_for_completion_timeout, the interrupt
373          * happens, to avoid missing it we need to double check
374          * status bits in control reg, if it is already 1, then
375          * consider that we have the interrupt successfully and
376          * return true.
377          */
378         ctrl_reg = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
379         if (a3700_spi->wait_mask & ctrl_reg)
380                 return true;
381
382         spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0);
383
384         /* Timeout was reached */
385         return false;
386 }
387
388 static bool a3700_spi_transfer_wait(struct spi_device *spi,
389                                     unsigned int bit_mask)
390 {
391         struct a3700_spi *a3700_spi;
392
393         a3700_spi = spi_master_get_devdata(spi->master);
394         a3700_spi->wait_mask = bit_mask;
395
396         return a3700_spi_wait_completion(spi);
397 }
398
399 static void a3700_spi_fifo_thres_set(struct a3700_spi *a3700_spi,
400                                      unsigned int bytes)
401 {
402         u32 val;
403
404         val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
405         val &= ~(A3700_SPI_FIFO_THRS_MASK << A3700_SPI_RFIFO_THRS_BIT);
406         val |= (bytes - 1) << A3700_SPI_RFIFO_THRS_BIT;
407         val &= ~(A3700_SPI_FIFO_THRS_MASK << A3700_SPI_WFIFO_THRS_BIT);
408         val |= (7 - bytes) << A3700_SPI_WFIFO_THRS_BIT;
409         spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
410 }
411
412 static void a3700_spi_transfer_setup(struct spi_device *spi,
413                                      struct spi_transfer *xfer)
414 {
415         struct a3700_spi *a3700_spi;
416
417         a3700_spi = spi_master_get_devdata(spi->master);
418
419         a3700_spi_clock_set(a3700_spi, xfer->speed_hz);
420
421         /* Use 4 bytes long transfers. Each transfer method has its way to deal
422          * with the remaining bytes for non 4-bytes aligned transfers.
423          */
424         a3700_spi_bytelen_set(a3700_spi, 4);
425
426         /* Initialize the working buffers */
427         a3700_spi->tx_buf  = xfer->tx_buf;
428         a3700_spi->rx_buf  = xfer->rx_buf;
429         a3700_spi->buf_len = xfer->len;
430 }
431
432 static void a3700_spi_set_cs(struct spi_device *spi, bool enable)
433 {
434         struct a3700_spi *a3700_spi = spi_master_get_devdata(spi->master);
435
436         if (!enable)
437                 a3700_spi_activate_cs(a3700_spi, spi->chip_select);
438         else
439                 a3700_spi_deactivate_cs(a3700_spi, spi->chip_select);
440 }
441
442 static void a3700_spi_header_set(struct a3700_spi *a3700_spi)
443 {
444         unsigned int addr_cnt;
445         u32 val = 0;
446
447         /* Clear the header registers */
448         spireg_write(a3700_spi, A3700_SPI_IF_INST_REG, 0);
449         spireg_write(a3700_spi, A3700_SPI_IF_ADDR_REG, 0);
450         spireg_write(a3700_spi, A3700_SPI_IF_RMODE_REG, 0);
451         spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, 0);
452
453         /* Set header counters */
454         if (a3700_spi->tx_buf) {
455                 /*
456                  * when tx data is not 4 bytes aligned, there will be unexpected
457                  * bytes out of SPI output register, since it always shifts out
458                  * as whole 4 bytes. This might cause incorrect transaction with
459                  * some devices. To avoid that, use SPI header count feature to
460                  * transfer up to 3 bytes of data first, and then make the rest
461                  * of data 4-byte aligned.
462                  */
463                 addr_cnt = a3700_spi->buf_len % 4;
464                 if (addr_cnt) {
465                         val = (addr_cnt & A3700_SPI_ADDR_CNT_MASK)
466                                 << A3700_SPI_ADDR_CNT_BIT;
467                         spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, val);
468
469                         /* Update the buffer length to be transferred */
470                         a3700_spi->buf_len -= addr_cnt;
471
472                         /* transfer 1~3 bytes through address count */
473                         val = 0;
474                         while (addr_cnt--) {
475                                 val = (val << 8) | a3700_spi->tx_buf[0];
476                                 a3700_spi->tx_buf++;
477                         }
478                         spireg_write(a3700_spi, A3700_SPI_IF_ADDR_REG, val);
479                 }
480         }
481 }
482
483 static int a3700_is_wfifo_full(struct a3700_spi *a3700_spi)
484 {
485         u32 val;
486
487         val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
488         return (val & A3700_SPI_WFIFO_FULL);
489 }
490
491 static int a3700_spi_fifo_write(struct a3700_spi *a3700_spi)
492 {
493         u32 val;
494
495         while (!a3700_is_wfifo_full(a3700_spi) && a3700_spi->buf_len) {
496                 val = *(u32 *)a3700_spi->tx_buf;
497                 spireg_write(a3700_spi, A3700_SPI_DATA_OUT_REG, val);
498                 a3700_spi->buf_len -= 4;
499                 a3700_spi->tx_buf += 4;
500         }
501
502         return 0;
503 }
504
505 static int a3700_is_rfifo_empty(struct a3700_spi *a3700_spi)
506 {
507         u32 val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
508
509         return (val & A3700_SPI_RFIFO_EMPTY);
510 }
511
512 static int a3700_spi_fifo_read(struct a3700_spi *a3700_spi)
513 {
514         u32 val;
515
516         while (!a3700_is_rfifo_empty(a3700_spi) && a3700_spi->buf_len) {
517                 val = spireg_read(a3700_spi, A3700_SPI_DATA_IN_REG);
518                 if (a3700_spi->buf_len >= 4) {
519
520                         memcpy(a3700_spi->rx_buf, &val, 4);
521
522                         a3700_spi->buf_len -= 4;
523                         a3700_spi->rx_buf += 4;
524                 } else {
525                         /*
526                          * When remain bytes is not larger than 4, we should
527                          * avoid memory overwriting and just write the left rx
528                          * buffer bytes.
529                          */
530                         while (a3700_spi->buf_len) {
531                                 *a3700_spi->rx_buf = val & 0xff;
532                                 val >>= 8;
533
534                                 a3700_spi->buf_len--;
535                                 a3700_spi->rx_buf++;
536                         }
537                 }
538         }
539
540         return 0;
541 }
542
543 static void a3700_spi_transfer_abort_fifo(struct a3700_spi *a3700_spi)
544 {
545         int timeout = A3700_SPI_TIMEOUT;
546         u32 val;
547
548         val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
549         val |= A3700_SPI_XFER_STOP;
550         spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
551
552         while (--timeout) {
553                 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
554                 if (!(val & A3700_SPI_XFER_START))
555                         break;
556                 udelay(1);
557         }
558
559         a3700_spi_fifo_flush(a3700_spi);
560
561         val &= ~A3700_SPI_XFER_STOP;
562         spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
563 }
564
565 static int a3700_spi_prepare_message(struct spi_master *master,
566                                      struct spi_message *message)
567 {
568         struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
569         struct spi_device *spi = message->spi;
570         int ret;
571
572         ret = clk_enable(a3700_spi->clk);
573         if (ret) {
574                 dev_err(&spi->dev, "failed to enable clk with error %d\n", ret);
575                 return ret;
576         }
577
578         /* Flush the FIFOs */
579         ret = a3700_spi_fifo_flush(a3700_spi);
580         if (ret)
581                 return ret;
582
583         a3700_spi_mode_set(a3700_spi, spi->mode);
584
585         return 0;
586 }
587
588 static int a3700_spi_transfer_one_fifo(struct spi_master *master,
589                                   struct spi_device *spi,
590                                   struct spi_transfer *xfer)
591 {
592         struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
593         int ret = 0, timeout = A3700_SPI_TIMEOUT;
594         unsigned int nbits = 0, byte_len;
595         u32 val;
596
597         /* Make sure we use FIFO mode */
598         a3700_spi_fifo_mode_set(a3700_spi, true);
599
600         /* Configure FIFO thresholds */
601         byte_len = xfer->bits_per_word >> 3;
602         a3700_spi_fifo_thres_set(a3700_spi, byte_len);
603
604         if (xfer->tx_buf)
605                 nbits = xfer->tx_nbits;
606         else if (xfer->rx_buf)
607                 nbits = xfer->rx_nbits;
608
609         a3700_spi_pin_mode_set(a3700_spi, nbits, xfer->rx_buf ? true : false);
610
611         /* Flush the FIFOs */
612         a3700_spi_fifo_flush(a3700_spi);
613
614         /* Transfer first bytes of data when buffer is not 4-byte aligned */
615         a3700_spi_header_set(a3700_spi);
616
617         if (xfer->rx_buf) {
618                 /* Clear WFIFO, since it's last 2 bytes are shifted out during
619                  * a read operation
620                  */
621                 spireg_write(a3700_spi, A3700_SPI_DATA_OUT_REG, 0);
622
623                 /* Set read data length */
624                 spireg_write(a3700_spi, A3700_SPI_IF_DIN_CNT_REG,
625                              a3700_spi->buf_len);
626                 /* Start READ transfer */
627                 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
628                 val &= ~A3700_SPI_RW_EN;
629                 val |= A3700_SPI_XFER_START;
630                 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
631         } else if (xfer->tx_buf) {
632                 /* Start Write transfer */
633                 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
634                 val |= (A3700_SPI_XFER_START | A3700_SPI_RW_EN);
635                 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
636
637                 /*
638                  * If there are data to be written to the SPI device, xmit_data
639                  * flag is set true; otherwise the instruction in SPI_INSTR does
640                  * not require data to be written to the SPI device, then
641                  * xmit_data flag is set false.
642                  */
643                 a3700_spi->xmit_data = (a3700_spi->buf_len != 0);
644         }
645
646         while (a3700_spi->buf_len) {
647                 if (a3700_spi->tx_buf) {
648                         /* Wait wfifo ready */
649                         if (!a3700_spi_transfer_wait(spi,
650                                                      A3700_SPI_WFIFO_RDY)) {
651                                 dev_err(&spi->dev,
652                                         "wait wfifo ready timed out\n");
653                                 ret = -ETIMEDOUT;
654                                 goto error;
655                         }
656                         /* Fill up the wfifo */
657                         ret = a3700_spi_fifo_write(a3700_spi);
658                         if (ret)
659                                 goto error;
660                 } else if (a3700_spi->rx_buf) {
661                         /* Wait rfifo ready */
662                         if (!a3700_spi_transfer_wait(spi,
663                                                      A3700_SPI_RFIFO_RDY)) {
664                                 dev_err(&spi->dev,
665                                         "wait rfifo ready timed out\n");
666                                 ret = -ETIMEDOUT;
667                                 goto error;
668                         }
669                         /* Drain out the rfifo */
670                         ret = a3700_spi_fifo_read(a3700_spi);
671                         if (ret)
672                                 goto error;
673                 }
674         }
675
676         /*
677          * Stop a write transfer in fifo mode:
678          *      - wait all the bytes in wfifo to be shifted out
679          *       - set XFER_STOP bit
680          *      - wait XFER_START bit clear
681          *      - clear XFER_STOP bit
682          * Stop a read transfer in fifo mode:
683          *      - the hardware is to reset the XFER_START bit
684          *         after the number of bytes indicated in DIN_CNT
685          *         register
686          *      - just wait XFER_START bit clear
687          */
688         if (a3700_spi->tx_buf) {
689                 if (a3700_spi->xmit_data) {
690                         /*
691                          * If there are data written to the SPI device, wait
692                          * until SPI_WFIFO_EMPTY is 1 to wait for all data to
693                          * transfer out of write FIFO.
694                          */
695                         if (!a3700_spi_transfer_wait(spi,
696                                                      A3700_SPI_WFIFO_EMPTY)) {
697                                 dev_err(&spi->dev, "wait wfifo empty timed out\n");
698                                 return -ETIMEDOUT;
699                         }
700                 }
701
702                 if (!a3700_spi_transfer_wait(spi, A3700_SPI_XFER_RDY)) {
703                         dev_err(&spi->dev, "wait xfer ready timed out\n");
704                         return -ETIMEDOUT;
705                 }
706
707                 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
708                 val |= A3700_SPI_XFER_STOP;
709                 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
710         }
711
712         while (--timeout) {
713                 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
714                 if (!(val & A3700_SPI_XFER_START))
715                         break;
716                 udelay(1);
717         }
718
719         if (timeout == 0) {
720                 dev_err(&spi->dev, "wait transfer start clear timed out\n");
721                 ret = -ETIMEDOUT;
722                 goto error;
723         }
724
725         val &= ~A3700_SPI_XFER_STOP;
726         spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
727         goto out;
728
729 error:
730         a3700_spi_transfer_abort_fifo(a3700_spi);
731 out:
732         spi_finalize_current_transfer(master);
733
734         return ret;
735 }
736
737 static int a3700_spi_transfer_one_full_duplex(struct spi_master *master,
738                                   struct spi_device *spi,
739                                   struct spi_transfer *xfer)
740 {
741         struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
742         u32 val_in, val_out;
743
744         /* Disable FIFO mode */
745         a3700_spi_fifo_mode_set(a3700_spi, false);
746
747         while (a3700_spi->buf_len) {
748
749                 /* When we have less than 4 bytes to transfer, switch to 1 byte
750                  * mode. This is reset after each transfer
751                  */
752                 if (a3700_spi->buf_len < 4)
753                         a3700_spi_bytelen_set(a3700_spi, 1);
754
755                 if (a3700_spi->byte_len == 1)
756                         val_out = *a3700_spi->tx_buf;
757                 else
758                         val_out = cpu_to_le32(*(u32 *)a3700_spi->tx_buf);
759
760                 spireg_write(a3700_spi, A3700_SPI_DATA_OUT_REG, val_out);
761
762                 /* Wait for all the data to be shifted in / out */
763                 while (!(spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG) &
764                                 A3700_SPI_XFER_DONE))
765                         cpu_relax();
766
767                 val_in = le32_to_cpu(spireg_read(a3700_spi,
768                                                  A3700_SPI_DATA_IN_REG));
769
770                 memcpy(a3700_spi->rx_buf, &val_in, a3700_spi->byte_len);
771
772                 a3700_spi->buf_len -= a3700_spi->byte_len;
773                 a3700_spi->tx_buf += a3700_spi->byte_len;
774                 a3700_spi->rx_buf += a3700_spi->byte_len;
775
776         }
777
778         spi_finalize_current_transfer(master);
779
780         return 0;
781 }
782
783 static int a3700_spi_transfer_one(struct spi_master *master,
784                                   struct spi_device *spi,
785                                   struct spi_transfer *xfer)
786 {
787         a3700_spi_transfer_setup(spi, xfer);
788
789         if (xfer->tx_buf && xfer->rx_buf)
790                 return a3700_spi_transfer_one_full_duplex(master, spi, xfer);
791
792         return a3700_spi_transfer_one_fifo(master, spi, xfer);
793 }
794
795 static int a3700_spi_unprepare_message(struct spi_master *master,
796                                        struct spi_message *message)
797 {
798         struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
799
800         clk_disable(a3700_spi->clk);
801
802         return 0;
803 }
804
805 static const struct of_device_id a3700_spi_dt_ids[] = {
806         { .compatible = "marvell,armada-3700-spi", .data = NULL },
807         {},
808 };
809
810 MODULE_DEVICE_TABLE(of, a3700_spi_dt_ids);
811
812 static int a3700_spi_probe(struct platform_device *pdev)
813 {
814         struct device *dev = &pdev->dev;
815         struct device_node *of_node = dev->of_node;
816         struct resource *res;
817         struct spi_master *master;
818         struct a3700_spi *spi;
819         u32 num_cs = 0;
820         int irq, ret = 0;
821
822         master = spi_alloc_master(dev, sizeof(*spi));
823         if (!master) {
824                 dev_err(dev, "master allocation failed\n");
825                 ret = -ENOMEM;
826                 goto out;
827         }
828
829         if (of_property_read_u32(of_node, "num-cs", &num_cs)) {
830                 dev_err(dev, "could not find num-cs\n");
831                 ret = -ENXIO;
832                 goto error;
833         }
834
835         master->bus_num = pdev->id;
836         master->dev.of_node = of_node;
837         master->mode_bits = SPI_MODE_3;
838         master->num_chipselect = num_cs;
839         master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(32);
840         master->prepare_message =  a3700_spi_prepare_message;
841         master->transfer_one = a3700_spi_transfer_one;
842         master->unprepare_message = a3700_spi_unprepare_message;
843         master->set_cs = a3700_spi_set_cs;
844         master->mode_bits |= (SPI_RX_DUAL | SPI_TX_DUAL |
845                               SPI_RX_QUAD | SPI_TX_QUAD);
846
847         platform_set_drvdata(pdev, master);
848
849         spi = spi_master_get_devdata(master);
850         memset(spi, 0, sizeof(struct a3700_spi));
851
852         spi->master = master;
853
854         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
855         spi->base = devm_ioremap_resource(dev, res);
856         if (IS_ERR(spi->base)) {
857                 ret = PTR_ERR(spi->base);
858                 goto error;
859         }
860
861         irq = platform_get_irq(pdev, 0);
862         if (irq < 0) {
863                 dev_err(dev, "could not get irq: %d\n", irq);
864                 ret = -ENXIO;
865                 goto error;
866         }
867         spi->irq = irq;
868
869         init_completion(&spi->done);
870
871         spi->clk = devm_clk_get(dev, NULL);
872         if (IS_ERR(spi->clk)) {
873                 dev_err(dev, "could not find clk: %ld\n", PTR_ERR(spi->clk));
874                 goto error;
875         }
876
877         ret = clk_prepare(spi->clk);
878         if (ret) {
879                 dev_err(dev, "could not prepare clk: %d\n", ret);
880                 goto error;
881         }
882
883         master->max_speed_hz = min_t(unsigned long, A3700_SPI_MAX_SPEED_HZ,
884                                         clk_get_rate(spi->clk));
885         master->min_speed_hz = DIV_ROUND_UP(clk_get_rate(spi->clk),
886                                                 A3700_SPI_MAX_PRESCALE);
887
888         ret = a3700_spi_init(spi);
889         if (ret)
890                 goto error_clk;
891
892         ret = devm_request_irq(dev, spi->irq, a3700_spi_interrupt, 0,
893                                dev_name(dev), master);
894         if (ret) {
895                 dev_err(dev, "could not request IRQ: %d\n", ret);
896                 goto error_clk;
897         }
898
899         ret = devm_spi_register_master(dev, master);
900         if (ret) {
901                 dev_err(dev, "Failed to register master\n");
902                 goto error_clk;
903         }
904
905         return 0;
906
907 error_clk:
908         clk_disable_unprepare(spi->clk);
909 error:
910         spi_master_put(master);
911 out:
912         return ret;
913 }
914
915 static int a3700_spi_remove(struct platform_device *pdev)
916 {
917         struct spi_master *master = platform_get_drvdata(pdev);
918         struct a3700_spi *spi = spi_master_get_devdata(master);
919
920         clk_unprepare(spi->clk);
921
922         return 0;
923 }
924
925 static struct platform_driver a3700_spi_driver = {
926         .driver = {
927                 .name   = DRIVER_NAME,
928                 .of_match_table = of_match_ptr(a3700_spi_dt_ids),
929         },
930         .probe          = a3700_spi_probe,
931         .remove         = a3700_spi_remove,
932 };
933
934 module_platform_driver(a3700_spi_driver);
935
936 MODULE_DESCRIPTION("Armada-3700 SPI driver");
937 MODULE_AUTHOR("Wilson Ding <dingwei@marvell.com>");
938 MODULE_LICENSE("GPL");
939 MODULE_ALIAS("platform:" DRIVER_NAME);