blob: a28702b1fa0568ad9a10a8ca38250e6453e295da [file] [log] [blame]
Romain Perier5762ab72016-12-08 15:58:44 +01001/*
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
83#define A3700_SPI_WFIFO_THRS_BIT 28
84#define A3700_SPI_RFIFO_THRS_BIT 24
85#define A3700_SPI_FIFO_THRS_MASK 0x7
86
87#define A3700_SPI_DATA_PIN_MASK 0x3
88
89/* A3700_SPI_IF_HDR_CNT_REG */
90#define A3700_SPI_DUMMY_CNT_BIT 12
91#define A3700_SPI_DUMMY_CNT_MASK 0x7
92#define A3700_SPI_RMODE_CNT_BIT 8
93#define A3700_SPI_RMODE_CNT_MASK 0x3
94#define A3700_SPI_ADDR_CNT_BIT 4
95#define A3700_SPI_ADDR_CNT_MASK 0x7
96#define A3700_SPI_INSTR_CNT_BIT 0
97#define A3700_SPI_INSTR_CNT_MASK 0x3
98
99/* A3700_SPI_IF_TIME_REG */
100#define A3700_SPI_CLK_CAPT_EDGE BIT(7)
101
102/* Flags and macros for struct a3700_spi */
103#define A3700_INSTR_CNT 1
104#define A3700_ADDR_CNT 3
105#define A3700_DUMMY_CNT 1
106
107struct a3700_spi {
108 struct spi_master *master;
109 void __iomem *base;
110 struct clk *clk;
111 unsigned int irq;
112 unsigned int flags;
113 bool xmit_data;
114 const u8 *tx_buf;
115 u8 *rx_buf;
116 size_t buf_len;
117 u8 byte_len;
118 u32 wait_mask;
119 struct completion done;
120 u32 addr_cnt;
121 u32 instr_cnt;
122 size_t hdr_cnt;
123};
124
125static u32 spireg_read(struct a3700_spi *a3700_spi, u32 offset)
126{
127 return readl(a3700_spi->base + offset);
128}
129
130static void spireg_write(struct a3700_spi *a3700_spi, u32 offset, u32 data)
131{
132 writel(data, a3700_spi->base + offset);
133}
134
135static void a3700_spi_auto_cs_unset(struct a3700_spi *a3700_spi)
136{
137 u32 val;
138
139 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
140 val &= ~A3700_SPI_AUTO_CS;
141 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
142}
143
144static void a3700_spi_activate_cs(struct a3700_spi *a3700_spi, unsigned int cs)
145{
146 u32 val;
147
148 val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
149 val |= (A3700_SPI_EN << cs);
150 spireg_write(a3700_spi, A3700_SPI_IF_CTRL_REG, val);
151}
152
153static void a3700_spi_deactivate_cs(struct a3700_spi *a3700_spi,
154 unsigned int cs)
155{
156 u32 val;
157
158 val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
159 val &= ~(A3700_SPI_EN << cs);
160 spireg_write(a3700_spi, A3700_SPI_IF_CTRL_REG, val);
161}
162
163static int a3700_spi_pin_mode_set(struct a3700_spi *a3700_spi,
Miquel Raynal747e1f62017-09-13 18:21:38 +0200164 unsigned int pin_mode, bool receiving)
Romain Perier5762ab72016-12-08 15:58:44 +0100165{
166 u32 val;
167
168 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
169 val &= ~(A3700_SPI_INST_PIN | A3700_SPI_ADDR_PIN);
170 val &= ~(A3700_SPI_DATA_PIN0 | A3700_SPI_DATA_PIN1);
171
172 switch (pin_mode) {
Romain Periercfd66932016-12-21 11:10:30 +0100173 case SPI_NBITS_SINGLE:
Romain Perier5762ab72016-12-08 15:58:44 +0100174 break;
Romain Periercfd66932016-12-21 11:10:30 +0100175 case SPI_NBITS_DUAL:
Romain Perier5762ab72016-12-08 15:58:44 +0100176 val |= A3700_SPI_DATA_PIN0;
177 break;
Romain Periercfd66932016-12-21 11:10:30 +0100178 case SPI_NBITS_QUAD:
Romain Perier5762ab72016-12-08 15:58:44 +0100179 val |= A3700_SPI_DATA_PIN1;
Miquel Raynal747e1f62017-09-13 18:21:38 +0200180 /* RX during address reception uses 4-pin */
181 if (receiving)
182 val |= A3700_SPI_ADDR_PIN;
Romain Perier5762ab72016-12-08 15:58:44 +0100183 break;
184 default:
185 dev_err(&a3700_spi->master->dev, "wrong pin mode %u", pin_mode);
186 return -EINVAL;
187 }
188
189 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
190
191 return 0;
192}
193
194static void a3700_spi_fifo_mode_set(struct a3700_spi *a3700_spi)
195{
196 u32 val;
197
198 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
199 val |= A3700_SPI_FIFO_MODE;
200 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
201}
202
203static void a3700_spi_mode_set(struct a3700_spi *a3700_spi,
204 unsigned int mode_bits)
205{
206 u32 val;
207
208 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
209
210 if (mode_bits & SPI_CPOL)
211 val |= A3700_SPI_CLK_POL;
212 else
213 val &= ~A3700_SPI_CLK_POL;
214
215 if (mode_bits & SPI_CPHA)
216 val |= A3700_SPI_CLK_PHA;
217 else
218 val &= ~A3700_SPI_CLK_PHA;
219
220 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
221}
222
223static void a3700_spi_clock_set(struct a3700_spi *a3700_spi,
224 unsigned int speed_hz, u16 mode)
225{
226 u32 val;
227 u32 prescale;
228
229 prescale = DIV_ROUND_UP(clk_get_rate(a3700_spi->clk), speed_hz);
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
255static 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
269static 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
288static 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
327static 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 */
Dan Carpenter0cc059a2016-12-16 12:33:59 +0300346 complete(&a3700_spi->done);
Romain Perier5762ab72016-12-08 15:58:44 +0100347
348 return IRQ_HANDLED;
349}
350
351static 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 return true;
399}
400
401static bool a3700_spi_transfer_wait(struct spi_device *spi,
402 unsigned int bit_mask)
403{
404 struct a3700_spi *a3700_spi;
405
406 a3700_spi = spi_master_get_devdata(spi->master);
407 a3700_spi->wait_mask = bit_mask;
408
409 return a3700_spi_wait_completion(spi);
410}
411
412static void a3700_spi_fifo_thres_set(struct a3700_spi *a3700_spi,
413 unsigned int bytes)
414{
415 u32 val;
416
417 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
418 val &= ~(A3700_SPI_FIFO_THRS_MASK << A3700_SPI_RFIFO_THRS_BIT);
419 val |= (bytes - 1) << A3700_SPI_RFIFO_THRS_BIT;
420 val &= ~(A3700_SPI_FIFO_THRS_MASK << A3700_SPI_WFIFO_THRS_BIT);
421 val |= (7 - bytes) << A3700_SPI_WFIFO_THRS_BIT;
422 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
423}
424
425static void a3700_spi_transfer_setup(struct spi_device *spi,
Romain Perier85798e152016-12-21 11:10:29 +0100426 struct spi_transfer *xfer)
Romain Perier5762ab72016-12-08 15:58:44 +0100427{
428 struct a3700_spi *a3700_spi;
429 unsigned int byte_len;
430
431 a3700_spi = spi_master_get_devdata(spi->master);
432
433 a3700_spi_clock_set(a3700_spi, xfer->speed_hz, spi->mode);
434
435 byte_len = xfer->bits_per_word >> 3;
436
437 a3700_spi_fifo_thres_set(a3700_spi, byte_len);
438}
439
440static void a3700_spi_set_cs(struct spi_device *spi, bool enable)
441{
442 struct a3700_spi *a3700_spi = spi_master_get_devdata(spi->master);
443
444 if (!enable)
445 a3700_spi_activate_cs(a3700_spi, spi->chip_select);
446 else
447 a3700_spi_deactivate_cs(a3700_spi, spi->chip_select);
448}
449
450static void a3700_spi_header_set(struct a3700_spi *a3700_spi)
451{
452 u32 instr_cnt = 0, addr_cnt = 0, dummy_cnt = 0;
453 u32 val = 0;
454
455 /* Clear the header registers */
456 spireg_write(a3700_spi, A3700_SPI_IF_INST_REG, 0);
457 spireg_write(a3700_spi, A3700_SPI_IF_ADDR_REG, 0);
458 spireg_write(a3700_spi, A3700_SPI_IF_RMODE_REG, 0);
459
460 /* Set header counters */
461 if (a3700_spi->tx_buf) {
462 if (a3700_spi->buf_len <= a3700_spi->instr_cnt) {
463 instr_cnt = a3700_spi->buf_len;
464 } else if (a3700_spi->buf_len <= (a3700_spi->instr_cnt +
465 a3700_spi->addr_cnt)) {
466 instr_cnt = a3700_spi->instr_cnt;
467 addr_cnt = a3700_spi->buf_len - instr_cnt;
468 } else if (a3700_spi->buf_len <= a3700_spi->hdr_cnt) {
469 instr_cnt = a3700_spi->instr_cnt;
470 addr_cnt = a3700_spi->addr_cnt;
471 /* Need to handle the normal write case with 1 byte
472 * data
473 */
474 if (!a3700_spi->tx_buf[instr_cnt + addr_cnt])
475 dummy_cnt = a3700_spi->buf_len - instr_cnt -
476 addr_cnt;
477 }
478 val |= ((instr_cnt & A3700_SPI_INSTR_CNT_MASK)
479 << A3700_SPI_INSTR_CNT_BIT);
480 val |= ((addr_cnt & A3700_SPI_ADDR_CNT_MASK)
481 << A3700_SPI_ADDR_CNT_BIT);
482 val |= ((dummy_cnt & A3700_SPI_DUMMY_CNT_MASK)
483 << A3700_SPI_DUMMY_CNT_BIT);
484 }
485 spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, val);
486
487 /* Update the buffer length to be transferred */
488 a3700_spi->buf_len -= (instr_cnt + addr_cnt + dummy_cnt);
489
490 /* Set Instruction */
491 val = 0;
492 while (instr_cnt--) {
493 val = (val << 8) | a3700_spi->tx_buf[0];
494 a3700_spi->tx_buf++;
495 }
496 spireg_write(a3700_spi, A3700_SPI_IF_INST_REG, val);
497
498 /* Set Address */
499 val = 0;
500 while (addr_cnt--) {
501 val = (val << 8) | a3700_spi->tx_buf[0];
502 a3700_spi->tx_buf++;
503 }
504 spireg_write(a3700_spi, A3700_SPI_IF_ADDR_REG, val);
505}
506
507static int a3700_is_wfifo_full(struct a3700_spi *a3700_spi)
508{
509 u32 val;
510
511 val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
512 return (val & A3700_SPI_WFIFO_FULL);
513}
514
515static int a3700_spi_fifo_write(struct a3700_spi *a3700_spi)
516{
517 u32 val;
518 int i = 0;
519
520 while (!a3700_is_wfifo_full(a3700_spi) && a3700_spi->buf_len) {
521 val = 0;
522 if (a3700_spi->buf_len >= 4) {
523 val = cpu_to_le32(*(u32 *)a3700_spi->tx_buf);
524 spireg_write(a3700_spi, A3700_SPI_DATA_OUT_REG, val);
525
526 a3700_spi->buf_len -= 4;
527 a3700_spi->tx_buf += 4;
528 } else {
529 /*
530 * If the remained buffer length is less than 4-bytes,
531 * we should pad the write buffer with all ones. So that
532 * it avoids overwrite the unexpected bytes following
533 * the last one.
534 */
535 val = GENMASK(31, 0);
536 while (a3700_spi->buf_len) {
537 val &= ~(0xff << (8 * i));
538 val |= *a3700_spi->tx_buf++ << (8 * i);
539 i++;
540 a3700_spi->buf_len--;
541
542 spireg_write(a3700_spi, A3700_SPI_DATA_OUT_REG,
543 val);
544 }
545 break;
546 }
547 }
548
549 return 0;
550}
551
552static int a3700_is_rfifo_empty(struct a3700_spi *a3700_spi)
553{
554 u32 val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
555
556 return (val & A3700_SPI_RFIFO_EMPTY);
557}
558
559static int a3700_spi_fifo_read(struct a3700_spi *a3700_spi)
560{
561 u32 val;
562
563 while (!a3700_is_rfifo_empty(a3700_spi) && a3700_spi->buf_len) {
564 val = spireg_read(a3700_spi, A3700_SPI_DATA_IN_REG);
565 if (a3700_spi->buf_len >= 4) {
566 u32 data = le32_to_cpu(val);
Romain Perier85798e152016-12-21 11:10:29 +0100567
Romain Perier5762ab72016-12-08 15:58:44 +0100568 memcpy(a3700_spi->rx_buf, &data, 4);
569
570 a3700_spi->buf_len -= 4;
571 a3700_spi->rx_buf += 4;
572 } else {
573 /*
574 * When remain bytes is not larger than 4, we should
575 * avoid memory overwriting and just write the left rx
576 * buffer bytes.
577 */
578 while (a3700_spi->buf_len) {
579 *a3700_spi->rx_buf = val & 0xff;
580 val >>= 8;
581
582 a3700_spi->buf_len--;
583 a3700_spi->rx_buf++;
584 }
585 }
586 }
587
588 return 0;
589}
590
591static void a3700_spi_transfer_abort_fifo(struct a3700_spi *a3700_spi)
592{
593 int timeout = A3700_SPI_TIMEOUT;
594 u32 val;
595
596 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
597 val |= A3700_SPI_XFER_STOP;
598 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
599
600 while (--timeout) {
601 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
602 if (!(val & A3700_SPI_XFER_START))
603 break;
604 udelay(1);
605 }
606
607 a3700_spi_fifo_flush(a3700_spi);
608
609 val &= ~A3700_SPI_XFER_STOP;
610 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
611}
612
613static int a3700_spi_prepare_message(struct spi_master *master,
614 struct spi_message *message)
615{
616 struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
617 struct spi_device *spi = message->spi;
618 int ret;
619
620 ret = clk_enable(a3700_spi->clk);
621 if (ret) {
622 dev_err(&spi->dev, "failed to enable clk with error %d\n", ret);
623 return ret;
624 }
625
626 /* Flush the FIFOs */
627 ret = a3700_spi_fifo_flush(a3700_spi);
628 if (ret)
629 return ret;
630
631 a3700_spi_bytelen_set(a3700_spi, 4);
632
633 return 0;
634}
635
636static int a3700_spi_transfer_one(struct spi_master *master,
637 struct spi_device *spi,
638 struct spi_transfer *xfer)
639{
640 struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
641 int ret = 0, timeout = A3700_SPI_TIMEOUT;
642 unsigned int nbits = 0;
643 u32 val;
644
645 a3700_spi_transfer_setup(spi, xfer);
646
647 a3700_spi->tx_buf = xfer->tx_buf;
648 a3700_spi->rx_buf = xfer->rx_buf;
649 a3700_spi->buf_len = xfer->len;
650
651 /* SPI transfer headers */
652 a3700_spi_header_set(a3700_spi);
653
654 if (xfer->tx_buf)
655 nbits = xfer->tx_nbits;
656 else if (xfer->rx_buf)
657 nbits = xfer->rx_nbits;
658
Miquel Raynal747e1f62017-09-13 18:21:38 +0200659 a3700_spi_pin_mode_set(a3700_spi, nbits, xfer->rx_buf ? true : false);
Romain Perier5762ab72016-12-08 15:58:44 +0100660
661 if (xfer->rx_buf) {
662 /* Set read data length */
663 spireg_write(a3700_spi, A3700_SPI_IF_DIN_CNT_REG,
664 a3700_spi->buf_len);
665 /* Start READ transfer */
666 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
667 val &= ~A3700_SPI_RW_EN;
668 val |= A3700_SPI_XFER_START;
669 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
670 } else if (xfer->tx_buf) {
671 /* Start Write transfer */
672 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
673 val |= (A3700_SPI_XFER_START | A3700_SPI_RW_EN);
674 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
675
676 /*
677 * If there are data to be written to the SPI device, xmit_data
678 * flag is set true; otherwise the instruction in SPI_INSTR does
679 * not require data to be written to the SPI device, then
680 * xmit_data flag is set false.
681 */
682 a3700_spi->xmit_data = (a3700_spi->buf_len != 0);
683 }
684
685 while (a3700_spi->buf_len) {
686 if (a3700_spi->tx_buf) {
687 /* Wait wfifo ready */
688 if (!a3700_spi_transfer_wait(spi,
689 A3700_SPI_WFIFO_RDY)) {
690 dev_err(&spi->dev,
691 "wait wfifo ready timed out\n");
692 ret = -ETIMEDOUT;
693 goto error;
694 }
695 /* Fill up the wfifo */
696 ret = a3700_spi_fifo_write(a3700_spi);
697 if (ret)
698 goto error;
699 } else if (a3700_spi->rx_buf) {
700 /* Wait rfifo ready */
701 if (!a3700_spi_transfer_wait(spi,
702 A3700_SPI_RFIFO_RDY)) {
703 dev_err(&spi->dev,
704 "wait rfifo ready timed out\n");
705 ret = -ETIMEDOUT;
706 goto error;
707 }
708 /* Drain out the rfifo */
709 ret = a3700_spi_fifo_read(a3700_spi);
710 if (ret)
711 goto error;
712 }
713 }
714
715 /*
716 * Stop a write transfer in fifo mode:
717 * - wait all the bytes in wfifo to be shifted out
718 * - set XFER_STOP bit
719 * - wait XFER_START bit clear
720 * - clear XFER_STOP bit
721 * Stop a read transfer in fifo mode:
722 * - the hardware is to reset the XFER_START bit
723 * after the number of bytes indicated in DIN_CNT
724 * register
725 * - just wait XFER_START bit clear
726 */
727 if (a3700_spi->tx_buf) {
728 if (a3700_spi->xmit_data) {
729 /*
730 * If there are data written to the SPI device, wait
731 * until SPI_WFIFO_EMPTY is 1 to wait for all data to
732 * transfer out of write FIFO.
733 */
734 if (!a3700_spi_transfer_wait(spi,
735 A3700_SPI_WFIFO_EMPTY)) {
736 dev_err(&spi->dev, "wait wfifo empty timed out\n");
737 return -ETIMEDOUT;
738 }
739 } else {
740 /*
741 * If the instruction in SPI_INSTR does not require data
742 * to be written to the SPI device, wait until SPI_RDY
743 * is 1 for the SPI interface to be in idle.
744 */
745 if (!a3700_spi_transfer_wait(spi, A3700_SPI_XFER_RDY)) {
746 dev_err(&spi->dev, "wait xfer ready timed out\n");
747 return -ETIMEDOUT;
748 }
749 }
750
751 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
752 val |= A3700_SPI_XFER_STOP;
753 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
754 }
755
756 while (--timeout) {
757 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
758 if (!(val & A3700_SPI_XFER_START))
759 break;
760 udelay(1);
761 }
762
763 if (timeout == 0) {
764 dev_err(&spi->dev, "wait transfer start clear timed out\n");
765 ret = -ETIMEDOUT;
766 goto error;
767 }
768
769 val &= ~A3700_SPI_XFER_STOP;
770 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
771 goto out;
772
773error:
774 a3700_spi_transfer_abort_fifo(a3700_spi);
775out:
776 spi_finalize_current_transfer(master);
777
778 return ret;
779}
780
781static int a3700_spi_unprepare_message(struct spi_master *master,
782 struct spi_message *message)
783{
784 struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
785
786 clk_disable(a3700_spi->clk);
787
788 return 0;
789}
790
791static const struct of_device_id a3700_spi_dt_ids[] = {
792 { .compatible = "marvell,armada-3700-spi", .data = NULL },
793 {},
794};
795
796MODULE_DEVICE_TABLE(of, a3700_spi_dt_ids);
797
798static int a3700_spi_probe(struct platform_device *pdev)
799{
800 struct device *dev = &pdev->dev;
801 struct device_node *of_node = dev->of_node;
802 struct resource *res;
803 struct spi_master *master;
804 struct a3700_spi *spi;
805 u32 num_cs = 0;
Colin Ian Kingf6f00832016-12-13 10:28:12 +0000806 int irq, ret = 0;
Romain Perier5762ab72016-12-08 15:58:44 +0100807
808 master = spi_alloc_master(dev, sizeof(*spi));
809 if (!master) {
810 dev_err(dev, "master allocation failed\n");
811 ret = -ENOMEM;
812 goto out;
813 }
814
815 if (of_property_read_u32(of_node, "num-cs", &num_cs)) {
816 dev_err(dev, "could not find num-cs\n");
817 ret = -ENXIO;
818 goto error;
819 }
820
821 master->bus_num = pdev->id;
822 master->dev.of_node = of_node;
823 master->mode_bits = SPI_MODE_3;
824 master->num_chipselect = num_cs;
825 master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(32);
826 master->prepare_message = a3700_spi_prepare_message;
827 master->transfer_one = a3700_spi_transfer_one;
828 master->unprepare_message = a3700_spi_unprepare_message;
829 master->set_cs = a3700_spi_set_cs;
830 master->flags = SPI_MASTER_HALF_DUPLEX;
Dan Carpenter42cd4ed2016-12-16 12:33:25 +0300831 master->mode_bits |= (SPI_RX_DUAL | SPI_TX_DUAL |
Romain Perier5762ab72016-12-08 15:58:44 +0100832 SPI_RX_QUAD | SPI_TX_QUAD);
833
834 platform_set_drvdata(pdev, master);
835
836 spi = spi_master_get_devdata(master);
837 memset(spi, 0, sizeof(struct a3700_spi));
838
839 spi->master = master;
840 spi->instr_cnt = A3700_INSTR_CNT;
841 spi->addr_cnt = A3700_ADDR_CNT;
842 spi->hdr_cnt = A3700_INSTR_CNT + A3700_ADDR_CNT +
843 A3700_DUMMY_CNT;
844
845 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
846 spi->base = devm_ioremap_resource(dev, res);
847 if (IS_ERR(spi->base)) {
848 ret = PTR_ERR(spi->base);
849 goto error;
850 }
851
Colin Ian Kingf6f00832016-12-13 10:28:12 +0000852 irq = platform_get_irq(pdev, 0);
853 if (irq < 0) {
854 dev_err(dev, "could not get irq: %d\n", irq);
Romain Perier5762ab72016-12-08 15:58:44 +0100855 ret = -ENXIO;
856 goto error;
857 }
Colin Ian Kingf6f00832016-12-13 10:28:12 +0000858 spi->irq = irq;
Romain Perier5762ab72016-12-08 15:58:44 +0100859
860 init_completion(&spi->done);
861
862 spi->clk = devm_clk_get(dev, NULL);
863 if (IS_ERR(spi->clk)) {
864 dev_err(dev, "could not find clk: %ld\n", PTR_ERR(spi->clk));
865 goto error;
866 }
867
868 ret = clk_prepare(spi->clk);
869 if (ret) {
870 dev_err(dev, "could not prepare clk: %d\n", ret);
871 goto error;
872 }
873
874 ret = a3700_spi_init(spi);
875 if (ret)
876 goto error_clk;
877
878 ret = devm_request_irq(dev, spi->irq, a3700_spi_interrupt, 0,
879 dev_name(dev), master);
880 if (ret) {
881 dev_err(dev, "could not request IRQ: %d\n", ret);
882 goto error_clk;
883 }
884
885 ret = devm_spi_register_master(dev, master);
886 if (ret) {
887 dev_err(dev, "Failed to register master\n");
888 goto error_clk;
889 }
890
891 return 0;
892
893error_clk:
894 clk_disable_unprepare(spi->clk);
895error:
896 spi_master_put(master);
897out:
898 return ret;
899}
900
901static int a3700_spi_remove(struct platform_device *pdev)
902{
903 struct spi_master *master = platform_get_drvdata(pdev);
904 struct a3700_spi *spi = spi_master_get_devdata(master);
905
906 clk_unprepare(spi->clk);
Romain Perier5762ab72016-12-08 15:58:44 +0100907
908 return 0;
909}
910
911static struct platform_driver a3700_spi_driver = {
912 .driver = {
913 .name = DRIVER_NAME,
Romain Perier5762ab72016-12-08 15:58:44 +0100914 .of_match_table = of_match_ptr(a3700_spi_dt_ids),
915 },
916 .probe = a3700_spi_probe,
917 .remove = a3700_spi_remove,
918};
919
920module_platform_driver(a3700_spi_driver);
921
922MODULE_DESCRIPTION("Armada-3700 SPI driver");
923MODULE_AUTHOR("Wilson Ding <dingwei@marvell.com>");
924MODULE_LICENSE("GPL");
925MODULE_ALIAS("platform:" DRIVER_NAME);