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