blob: f5b646e35cc4d1b57e67b9ed29eb3cb8abff2513 [file] [log] [blame]
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001/*
2 * Copyright (c) 2008-2014, The Linux foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License rev 2 and
6 * only rev 2 as published by the free Software foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or fITNESS fOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/list.h>
20#include <linux/module.h>
21#include <linux/of.h>
22#include <linux/platform_device.h>
23#include <linux/pm_runtime.h>
24#include <linux/spi/spi.h>
25
26#define QUP_CONFIG 0x0000
27#define QUP_STATE 0x0004
28#define QUP_IO_M_MODES 0x0008
29#define QUP_SW_RESET 0x000c
30#define QUP_OPERATIONAL 0x0018
31#define QUP_ERROR_FLAGS 0x001c
32#define QUP_ERROR_FLAGS_EN 0x0020
33#define QUP_OPERATIONAL_MASK 0x0028
34#define QUP_HW_VERSION 0x0030
35#define QUP_MX_OUTPUT_CNT 0x0100
36#define QUP_OUTPUT_FIFO 0x0110
37#define QUP_MX_WRITE_CNT 0x0150
38#define QUP_MX_INPUT_CNT 0x0200
39#define QUP_MX_READ_CNT 0x0208
40#define QUP_INPUT_FIFO 0x0218
41
42#define SPI_CONFIG 0x0300
43#define SPI_IO_CONTROL 0x0304
44#define SPI_ERROR_FLAGS 0x0308
45#define SPI_ERROR_FLAGS_EN 0x030c
46
47/* QUP_CONFIG fields */
48#define QUP_CONFIG_SPI_MODE (1 << 8)
49#define QUP_CONFIG_CLOCK_AUTO_GATE BIT(13)
50#define QUP_CONFIG_NO_INPUT BIT(7)
51#define QUP_CONFIG_NO_OUTPUT BIT(6)
52#define QUP_CONFIG_N 0x001f
53
54/* QUP_STATE fields */
55#define QUP_STATE_VALID BIT(2)
56#define QUP_STATE_RESET 0
57#define QUP_STATE_RUN 1
58#define QUP_STATE_PAUSE 3
59#define QUP_STATE_MASK 3
60#define QUP_STATE_CLEAR 2
61
62#define QUP_HW_VERSION_2_1_1 0x20010001
63
64/* QUP_IO_M_MODES fields */
65#define QUP_IO_M_PACK_EN BIT(15)
66#define QUP_IO_M_UNPACK_EN BIT(14)
67#define QUP_IO_M_INPUT_MODE_MASK_SHIFT 12
68#define QUP_IO_M_OUTPUT_MODE_MASK_SHIFT 10
69#define QUP_IO_M_INPUT_MODE_MASK (3 << QUP_IO_M_INPUT_MODE_MASK_SHIFT)
70#define QUP_IO_M_OUTPUT_MODE_MASK (3 << QUP_IO_M_OUTPUT_MODE_MASK_SHIFT)
71
72#define QUP_IO_M_OUTPUT_BLOCK_SIZE(x) (((x) & (0x03 << 0)) >> 0)
73#define QUP_IO_M_OUTPUT_FIFO_SIZE(x) (((x) & (0x07 << 2)) >> 2)
74#define QUP_IO_M_INPUT_BLOCK_SIZE(x) (((x) & (0x03 << 5)) >> 5)
75#define QUP_IO_M_INPUT_FIFO_SIZE(x) (((x) & (0x07 << 7)) >> 7)
76
77#define QUP_IO_M_MODE_FIFO 0
78#define QUP_IO_M_MODE_BLOCK 1
79#define QUP_IO_M_MODE_DMOV 2
80#define QUP_IO_M_MODE_BAM 3
81
82/* QUP_OPERATIONAL fields */
83#define QUP_OP_MAX_INPUT_DONE_FLAG BIT(11)
84#define QUP_OP_MAX_OUTPUT_DONE_FLAG BIT(10)
85#define QUP_OP_IN_SERVICE_FLAG BIT(9)
86#define QUP_OP_OUT_SERVICE_FLAG BIT(8)
87#define QUP_OP_IN_FIFO_FULL BIT(7)
88#define QUP_OP_OUT_FIFO_FULL BIT(6)
89#define QUP_OP_IN_FIFO_NOT_EMPTY BIT(5)
90#define QUP_OP_OUT_FIFO_NOT_EMPTY BIT(4)
91
92/* QUP_ERROR_FLAGS and QUP_ERROR_FLAGS_EN fields */
93#define QUP_ERROR_OUTPUT_OVER_RUN BIT(5)
94#define QUP_ERROR_INPUT_UNDER_RUN BIT(4)
95#define QUP_ERROR_OUTPUT_UNDER_RUN BIT(3)
96#define QUP_ERROR_INPUT_OVER_RUN BIT(2)
97
98/* SPI_CONFIG fields */
99#define SPI_CONFIG_HS_MODE BIT(10)
100#define SPI_CONFIG_INPUT_FIRST BIT(9)
101#define SPI_CONFIG_LOOPBACK BIT(8)
102
103/* SPI_IO_CONTROL fields */
104#define SPI_IO_C_FORCE_CS BIT(11)
105#define SPI_IO_C_CLK_IDLE_HIGH BIT(10)
106#define SPI_IO_C_MX_CS_MODE BIT(8)
107#define SPI_IO_C_CS_N_POLARITY_0 BIT(4)
108#define SPI_IO_C_CS_SELECT(x) (((x) & 3) << 2)
109#define SPI_IO_C_CS_SELECT_MASK 0x000c
110#define SPI_IO_C_TRISTATE_CS BIT(1)
111#define SPI_IO_C_NO_TRI_STATE BIT(0)
112
113/* SPI_ERROR_FLAGS and SPI_ERROR_FLAGS_EN fields */
114#define SPI_ERROR_CLK_OVER_RUN BIT(1)
115#define SPI_ERROR_CLK_UNDER_RUN BIT(0)
116
117#define SPI_NUM_CHIPSELECTS 4
118
119/* high speed mode is when bus rate is greater then 26MHz */
120#define SPI_HS_MIN_RATE 26000000
121#define SPI_MAX_RATE 50000000
122
123#define SPI_DELAY_THRESHOLD 1
124#define SPI_DELAY_RETRY 10
125
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200126struct spi_qup {
127 void __iomem *base;
128 struct device *dev;
129 struct clk *cclk; /* core clock */
130 struct clk *iclk; /* interface clock */
131 int irq;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200132 spinlock_t lock;
133
134 int in_fifo_sz;
135 int out_fifo_sz;
136 int in_blk_sz;
137 int out_blk_sz;
138
139 struct spi_transfer *xfer;
140 struct completion done;
141 int error;
142 int w_size; /* bytes per SPI word */
143 int tx_bytes;
144 int rx_bytes;
Andy Gross70cea0a2014-06-12 14:34:12 -0500145 int qup_v1;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200146};
147
148
149static inline bool spi_qup_is_valid_state(struct spi_qup *controller)
150{
151 u32 opstate = readl_relaxed(controller->base + QUP_STATE);
152
153 return opstate & QUP_STATE_VALID;
154}
155
156static int spi_qup_set_state(struct spi_qup *controller, u32 state)
157{
158 unsigned long loop;
159 u32 cur_state;
160
161 loop = 0;
162 while (!spi_qup_is_valid_state(controller)) {
163
164 usleep_range(SPI_DELAY_THRESHOLD, SPI_DELAY_THRESHOLD * 2);
165
166 if (++loop > SPI_DELAY_RETRY)
167 return -EIO;
168 }
169
170 if (loop)
171 dev_dbg(controller->dev, "invalid state for %ld,us %d\n",
172 loop, state);
173
174 cur_state = readl_relaxed(controller->base + QUP_STATE);
175 /*
176 * Per spec: for PAUSE_STATE to RESET_STATE, two writes
177 * of (b10) are required
178 */
179 if (((cur_state & QUP_STATE_MASK) == QUP_STATE_PAUSE) &&
180 (state == QUP_STATE_RESET)) {
181 writel_relaxed(QUP_STATE_CLEAR, controller->base + QUP_STATE);
182 writel_relaxed(QUP_STATE_CLEAR, controller->base + QUP_STATE);
183 } else {
184 cur_state &= ~QUP_STATE_MASK;
185 cur_state |= state;
186 writel_relaxed(cur_state, controller->base + QUP_STATE);
187 }
188
189 loop = 0;
190 while (!spi_qup_is_valid_state(controller)) {
191
192 usleep_range(SPI_DELAY_THRESHOLD, SPI_DELAY_THRESHOLD * 2);
193
194 if (++loop > SPI_DELAY_RETRY)
195 return -EIO;
196 }
197
198 return 0;
199}
200
201
202static void spi_qup_fifo_read(struct spi_qup *controller,
203 struct spi_transfer *xfer)
204{
205 u8 *rx_buf = xfer->rx_buf;
206 u32 word, state;
207 int idx, shift, w_size;
208
209 w_size = controller->w_size;
210
211 while (controller->rx_bytes < xfer->len) {
212
213 state = readl_relaxed(controller->base + QUP_OPERATIONAL);
214 if (0 == (state & QUP_OP_IN_FIFO_NOT_EMPTY))
215 break;
216
217 word = readl_relaxed(controller->base + QUP_INPUT_FIFO);
218
219 if (!rx_buf) {
220 controller->rx_bytes += w_size;
221 continue;
222 }
223
224 for (idx = 0; idx < w_size; idx++, controller->rx_bytes++) {
225 /*
226 * The data format depends on bytes per SPI word:
227 * 4 bytes: 0x12345678
228 * 2 bytes: 0x00001234
229 * 1 byte : 0x00000012
230 */
231 shift = BITS_PER_BYTE;
232 shift *= (w_size - idx - 1);
233 rx_buf[controller->rx_bytes] = word >> shift;
234 }
235 }
236}
237
238static void spi_qup_fifo_write(struct spi_qup *controller,
239 struct spi_transfer *xfer)
240{
241 const u8 *tx_buf = xfer->tx_buf;
242 u32 word, state, data;
243 int idx, w_size;
244
245 w_size = controller->w_size;
246
247 while (controller->tx_bytes < xfer->len) {
248
249 state = readl_relaxed(controller->base + QUP_OPERATIONAL);
250 if (state & QUP_OP_OUT_FIFO_FULL)
251 break;
252
253 word = 0;
254 for (idx = 0; idx < w_size; idx++, controller->tx_bytes++) {
255
256 if (!tx_buf) {
257 controller->tx_bytes += w_size;
258 break;
259 }
260
261 data = tx_buf[controller->tx_bytes];
262 word |= data << (BITS_PER_BYTE * (3 - idx));
263 }
264
265 writel_relaxed(word, controller->base + QUP_OUTPUT_FIFO);
266 }
267}
268
269static irqreturn_t spi_qup_qup_irq(int irq, void *dev_id)
270{
271 struct spi_qup *controller = dev_id;
272 struct spi_transfer *xfer;
273 u32 opflags, qup_err, spi_err;
274 unsigned long flags;
275 int error = 0;
276
277 spin_lock_irqsave(&controller->lock, flags);
278 xfer = controller->xfer;
279 controller->xfer = NULL;
280 spin_unlock_irqrestore(&controller->lock, flags);
281
282 qup_err = readl_relaxed(controller->base + QUP_ERROR_FLAGS);
283 spi_err = readl_relaxed(controller->base + SPI_ERROR_FLAGS);
284 opflags = readl_relaxed(controller->base + QUP_OPERATIONAL);
285
286 writel_relaxed(qup_err, controller->base + QUP_ERROR_FLAGS);
287 writel_relaxed(spi_err, controller->base + SPI_ERROR_FLAGS);
288 writel_relaxed(opflags, controller->base + QUP_OPERATIONAL);
289
290 if (!xfer) {
Andy Gross8f391222014-05-13 16:34:42 -0500291 dev_err_ratelimited(controller->dev, "unexpected irq %08x %08x %08x\n",
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200292 qup_err, spi_err, opflags);
293 return IRQ_HANDLED;
294 }
295
296 if (qup_err) {
297 if (qup_err & QUP_ERROR_OUTPUT_OVER_RUN)
298 dev_warn(controller->dev, "OUTPUT_OVER_RUN\n");
299 if (qup_err & QUP_ERROR_INPUT_UNDER_RUN)
300 dev_warn(controller->dev, "INPUT_UNDER_RUN\n");
301 if (qup_err & QUP_ERROR_OUTPUT_UNDER_RUN)
302 dev_warn(controller->dev, "OUTPUT_UNDER_RUN\n");
303 if (qup_err & QUP_ERROR_INPUT_OVER_RUN)
304 dev_warn(controller->dev, "INPUT_OVER_RUN\n");
305
306 error = -EIO;
307 }
308
309 if (spi_err) {
310 if (spi_err & SPI_ERROR_CLK_OVER_RUN)
311 dev_warn(controller->dev, "CLK_OVER_RUN\n");
312 if (spi_err & SPI_ERROR_CLK_UNDER_RUN)
313 dev_warn(controller->dev, "CLK_UNDER_RUN\n");
314
315 error = -EIO;
316 }
317
318 if (opflags & QUP_OP_IN_SERVICE_FLAG)
319 spi_qup_fifo_read(controller, xfer);
320
321 if (opflags & QUP_OP_OUT_SERVICE_FLAG)
322 spi_qup_fifo_write(controller, xfer);
323
324 spin_lock_irqsave(&controller->lock, flags);
325 controller->error = error;
326 controller->xfer = xfer;
327 spin_unlock_irqrestore(&controller->lock, flags);
328
329 if (controller->rx_bytes == xfer->len || error)
330 complete(&controller->done);
331
332 return IRQ_HANDLED;
333}
334
335
336/* set clock freq ... bits per word */
Axel Lin00cce742014-02-24 23:07:36 +0800337static int spi_qup_io_config(struct spi_device *spi, struct spi_transfer *xfer)
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200338{
Axel Lin00cce742014-02-24 23:07:36 +0800339 struct spi_qup *controller = spi_master_get_devdata(spi->master);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200340 u32 config, iomode, mode;
341 int ret, n_words, w_size;
342
Axel Lin00cce742014-02-24 23:07:36 +0800343 if (spi->mode & SPI_LOOP && xfer->len > controller->in_fifo_sz) {
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200344 dev_err(controller->dev, "too big size for loopback %d > %d\n",
345 xfer->len, controller->in_fifo_sz);
346 return -EIO;
347 }
348
349 ret = clk_set_rate(controller->cclk, xfer->speed_hz);
350 if (ret) {
351 dev_err(controller->dev, "fail to set frequency %d",
352 xfer->speed_hz);
353 return -EIO;
354 }
355
356 if (spi_qup_set_state(controller, QUP_STATE_RESET)) {
357 dev_err(controller->dev, "cannot set RESET state\n");
358 return -EIO;
359 }
360
361 w_size = 4;
362 if (xfer->bits_per_word <= 8)
363 w_size = 1;
364 else if (xfer->bits_per_word <= 16)
365 w_size = 2;
366
367 n_words = xfer->len / w_size;
368 controller->w_size = w_size;
369
Andy Gross8f391222014-05-13 16:34:42 -0500370 if (n_words <= (controller->in_fifo_sz / sizeof(u32))) {
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200371 mode = QUP_IO_M_MODE_FIFO;
372 writel_relaxed(n_words, controller->base + QUP_MX_READ_CNT);
373 writel_relaxed(n_words, controller->base + QUP_MX_WRITE_CNT);
374 /* must be zero for FIFO */
375 writel_relaxed(0, controller->base + QUP_MX_INPUT_CNT);
376 writel_relaxed(0, controller->base + QUP_MX_OUTPUT_CNT);
377 } else {
378 mode = QUP_IO_M_MODE_BLOCK;
379 writel_relaxed(n_words, controller->base + QUP_MX_INPUT_CNT);
380 writel_relaxed(n_words, controller->base + QUP_MX_OUTPUT_CNT);
381 /* must be zero for BLOCK and BAM */
382 writel_relaxed(0, controller->base + QUP_MX_READ_CNT);
383 writel_relaxed(0, controller->base + QUP_MX_WRITE_CNT);
384 }
385
386 iomode = readl_relaxed(controller->base + QUP_IO_M_MODES);
387 /* Set input and output transfer mode */
388 iomode &= ~(QUP_IO_M_INPUT_MODE_MASK | QUP_IO_M_OUTPUT_MODE_MASK);
389 iomode &= ~(QUP_IO_M_PACK_EN | QUP_IO_M_UNPACK_EN);
390 iomode |= (mode << QUP_IO_M_OUTPUT_MODE_MASK_SHIFT);
391 iomode |= (mode << QUP_IO_M_INPUT_MODE_MASK_SHIFT);
392
393 writel_relaxed(iomode, controller->base + QUP_IO_M_MODES);
394
395 config = readl_relaxed(controller->base + SPI_CONFIG);
396
Axel Lin00cce742014-02-24 23:07:36 +0800397 if (spi->mode & SPI_LOOP)
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200398 config |= SPI_CONFIG_LOOPBACK;
399 else
400 config &= ~SPI_CONFIG_LOOPBACK;
401
Axel Lin00cce742014-02-24 23:07:36 +0800402 if (spi->mode & SPI_CPHA)
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200403 config &= ~SPI_CONFIG_INPUT_FIRST;
404 else
405 config |= SPI_CONFIG_INPUT_FIRST;
406
407 /*
408 * HS_MODE improves signal stability for spi-clk high rates,
409 * but is invalid in loop back mode.
410 */
Axel Lin00cce742014-02-24 23:07:36 +0800411 if ((xfer->speed_hz >= SPI_HS_MIN_RATE) && !(spi->mode & SPI_LOOP))
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200412 config |= SPI_CONFIG_HS_MODE;
413 else
414 config &= ~SPI_CONFIG_HS_MODE;
415
416 writel_relaxed(config, controller->base + SPI_CONFIG);
417
418 config = readl_relaxed(controller->base + QUP_CONFIG);
419 config &= ~(QUP_CONFIG_NO_INPUT | QUP_CONFIG_NO_OUTPUT | QUP_CONFIG_N);
420 config |= xfer->bits_per_word - 1;
421 config |= QUP_CONFIG_SPI_MODE;
422 writel_relaxed(config, controller->base + QUP_CONFIG);
423
Andy Gross70cea0a2014-06-12 14:34:12 -0500424 /* only write to OPERATIONAL_MASK when register is present */
425 if (!controller->qup_v1)
426 writel_relaxed(0, controller->base + QUP_OPERATIONAL_MASK);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200427 return 0;
428}
429
430static void spi_qup_set_cs(struct spi_device *spi, bool enable)
431{
432 struct spi_qup *controller = spi_master_get_devdata(spi->master);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200433
434 u32 iocontol, mask;
435
436 iocontol = readl_relaxed(controller->base + SPI_IO_CONTROL);
437
438 /* Disable auto CS toggle and use manual */
439 iocontol &= ~SPI_IO_C_MX_CS_MODE;
440 iocontol |= SPI_IO_C_FORCE_CS;
441
442 iocontol &= ~SPI_IO_C_CS_SELECT_MASK;
Axel Lin00cce742014-02-24 23:07:36 +0800443 iocontol |= SPI_IO_C_CS_SELECT(spi->chip_select);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200444
Axel Lin00cce742014-02-24 23:07:36 +0800445 mask = SPI_IO_C_CS_N_POLARITY_0 << spi->chip_select;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200446
447 if (enable)
448 iocontol |= mask;
449 else
450 iocontol &= ~mask;
451
452 writel_relaxed(iocontol, controller->base + SPI_IO_CONTROL);
453}
454
455static int spi_qup_transfer_one(struct spi_master *master,
456 struct spi_device *spi,
457 struct spi_transfer *xfer)
458{
459 struct spi_qup *controller = spi_master_get_devdata(master);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200460 unsigned long timeout, flags;
461 int ret = -EIO;
462
Axel Lin00cce742014-02-24 23:07:36 +0800463 ret = spi_qup_io_config(spi, xfer);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200464 if (ret)
465 return ret;
466
467 timeout = DIV_ROUND_UP(xfer->speed_hz, MSEC_PER_SEC);
468 timeout = DIV_ROUND_UP(xfer->len * 8, timeout);
469 timeout = 100 * msecs_to_jiffies(timeout);
470
471 reinit_completion(&controller->done);
472
473 spin_lock_irqsave(&controller->lock, flags);
474 controller->xfer = xfer;
475 controller->error = 0;
476 controller->rx_bytes = 0;
477 controller->tx_bytes = 0;
478 spin_unlock_irqrestore(&controller->lock, flags);
479
480 if (spi_qup_set_state(controller, QUP_STATE_RUN)) {
481 dev_warn(controller->dev, "cannot set RUN state\n");
482 goto exit;
483 }
484
485 if (spi_qup_set_state(controller, QUP_STATE_PAUSE)) {
486 dev_warn(controller->dev, "cannot set PAUSE state\n");
487 goto exit;
488 }
489
490 spi_qup_fifo_write(controller, xfer);
491
492 if (spi_qup_set_state(controller, QUP_STATE_RUN)) {
493 dev_warn(controller->dev, "cannot set EXECUTE state\n");
494 goto exit;
495 }
496
497 if (!wait_for_completion_timeout(&controller->done, timeout))
498 ret = -ETIMEDOUT;
499exit:
500 spi_qup_set_state(controller, QUP_STATE_RESET);
501 spin_lock_irqsave(&controller->lock, flags);
502 controller->xfer = NULL;
503 if (!ret)
504 ret = controller->error;
505 spin_unlock_irqrestore(&controller->lock, flags);
506 return ret;
507}
508
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200509static int spi_qup_probe(struct platform_device *pdev)
510{
511 struct spi_master *master;
512 struct clk *iclk, *cclk;
513 struct spi_qup *controller;
514 struct resource *res;
515 struct device *dev;
516 void __iomem *base;
Andy Gross70cea0a2014-06-12 14:34:12 -0500517 u32 max_freq, iomode;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200518 int ret, irq, size;
519
520 dev = &pdev->dev;
521 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
522 base = devm_ioremap_resource(dev, res);
523 if (IS_ERR(base))
524 return PTR_ERR(base);
525
526 irq = platform_get_irq(pdev, 0);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200527 if (irq < 0)
528 return irq;
529
530 cclk = devm_clk_get(dev, "core");
531 if (IS_ERR(cclk))
532 return PTR_ERR(cclk);
533
534 iclk = devm_clk_get(dev, "iface");
535 if (IS_ERR(iclk))
536 return PTR_ERR(iclk);
537
538 /* This is optional parameter */
539 if (of_property_read_u32(dev->of_node, "spi-max-frequency", &max_freq))
540 max_freq = SPI_MAX_RATE;
541
542 if (!max_freq || max_freq > SPI_MAX_RATE) {
543 dev_err(dev, "invalid clock frequency %d\n", max_freq);
544 return -ENXIO;
545 }
546
547 ret = clk_prepare_enable(cclk);
548 if (ret) {
549 dev_err(dev, "cannot enable core clock\n");
550 return ret;
551 }
552
553 ret = clk_prepare_enable(iclk);
554 if (ret) {
555 clk_disable_unprepare(cclk);
556 dev_err(dev, "cannot enable iface clock\n");
557 return ret;
558 }
559
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200560 master = spi_alloc_master(dev, sizeof(struct spi_qup));
561 if (!master) {
562 clk_disable_unprepare(cclk);
563 clk_disable_unprepare(iclk);
564 dev_err(dev, "cannot allocate master\n");
565 return -ENOMEM;
566 }
567
568 master->bus_num = pdev->id;
569 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
570 master->num_chipselect = SPI_NUM_CHIPSELECTS;
571 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Axel Lincb64ca52014-02-21 09:34:16 +0800572 master->max_speed_hz = max_freq;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200573 master->set_cs = spi_qup_set_cs;
574 master->transfer_one = spi_qup_transfer_one;
575 master->dev.of_node = pdev->dev.of_node;
576 master->auto_runtime_pm = true;
577
578 platform_set_drvdata(pdev, master);
579
580 controller = spi_master_get_devdata(master);
581
582 controller->dev = dev;
583 controller->base = base;
584 controller->iclk = iclk;
585 controller->cclk = cclk;
586 controller->irq = irq;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200587
Andy Gross70cea0a2014-06-12 14:34:12 -0500588 /* set v1 flag if device is version 1 */
589 if (of_device_is_compatible(dev->of_node, "qcom,spi-qup-v1.1.1"))
590 controller->qup_v1 = 1;
591
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200592 spin_lock_init(&controller->lock);
593 init_completion(&controller->done);
594
595 iomode = readl_relaxed(base + QUP_IO_M_MODES);
596
597 size = QUP_IO_M_OUTPUT_BLOCK_SIZE(iomode);
598 if (size)
599 controller->out_blk_sz = size * 16;
600 else
601 controller->out_blk_sz = 4;
602
603 size = QUP_IO_M_INPUT_BLOCK_SIZE(iomode);
604 if (size)
605 controller->in_blk_sz = size * 16;
606 else
607 controller->in_blk_sz = 4;
608
609 size = QUP_IO_M_OUTPUT_FIFO_SIZE(iomode);
610 controller->out_fifo_sz = controller->out_blk_sz * (2 << size);
611
612 size = QUP_IO_M_INPUT_FIFO_SIZE(iomode);
613 controller->in_fifo_sz = controller->in_blk_sz * (2 << size);
614
Andy Gross70cea0a2014-06-12 14:34:12 -0500615 dev_info(dev, "IN:block:%d, fifo:%d, OUT:block:%d, fifo:%d\n",
616 controller->in_blk_sz, controller->in_fifo_sz,
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200617 controller->out_blk_sz, controller->out_fifo_sz);
618
619 writel_relaxed(1, base + QUP_SW_RESET);
620
621 ret = spi_qup_set_state(controller, QUP_STATE_RESET);
622 if (ret) {
623 dev_err(dev, "cannot set RESET state\n");
624 goto error;
625 }
626
627 writel_relaxed(0, base + QUP_OPERATIONAL);
628 writel_relaxed(0, base + QUP_IO_M_MODES);
Andy Gross70cea0a2014-06-12 14:34:12 -0500629
630 if (!controller->qup_v1)
631 writel_relaxed(0, base + QUP_OPERATIONAL_MASK);
632
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200633 writel_relaxed(SPI_ERROR_CLK_UNDER_RUN | SPI_ERROR_CLK_OVER_RUN,
634 base + SPI_ERROR_FLAGS_EN);
635
Andy Gross70cea0a2014-06-12 14:34:12 -0500636 /* if earlier version of the QUP, disable INPUT_OVERRUN */
637 if (controller->qup_v1)
638 writel_relaxed(QUP_ERROR_OUTPUT_OVER_RUN |
639 QUP_ERROR_INPUT_UNDER_RUN | QUP_ERROR_OUTPUT_UNDER_RUN,
640 base + QUP_ERROR_FLAGS_EN);
641
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200642 writel_relaxed(0, base + SPI_CONFIG);
643 writel_relaxed(SPI_IO_C_NO_TRI_STATE, base + SPI_IO_CONTROL);
644
645 ret = devm_request_irq(dev, irq, spi_qup_qup_irq,
646 IRQF_TRIGGER_HIGH, pdev->name, controller);
647 if (ret)
648 goto error;
649
650 ret = devm_spi_register_master(dev, master);
651 if (ret)
652 goto error;
653
654 pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC);
655 pm_runtime_use_autosuspend(dev);
656 pm_runtime_set_active(dev);
657 pm_runtime_enable(dev);
658 return 0;
659
660error:
661 clk_disable_unprepare(cclk);
662 clk_disable_unprepare(iclk);
663 spi_master_put(master);
664 return ret;
665}
666
667#ifdef CONFIG_PM_RUNTIME
668static int spi_qup_pm_suspend_runtime(struct device *device)
669{
670 struct spi_master *master = dev_get_drvdata(device);
671 struct spi_qup *controller = spi_master_get_devdata(master);
672 u32 config;
673
674 /* Enable clocks auto gaiting */
675 config = readl(controller->base + QUP_CONFIG);
Axel Linf0ceb112014-02-23 13:27:16 +0800676 config |= QUP_CONFIG_CLOCK_AUTO_GATE;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200677 writel_relaxed(config, controller->base + QUP_CONFIG);
678 return 0;
679}
680
681static int spi_qup_pm_resume_runtime(struct device *device)
682{
683 struct spi_master *master = dev_get_drvdata(device);
684 struct spi_qup *controller = spi_master_get_devdata(master);
685 u32 config;
686
687 /* Disable clocks auto gaiting */
688 config = readl_relaxed(controller->base + QUP_CONFIG);
Axel Linf0ceb112014-02-23 13:27:16 +0800689 config &= ~QUP_CONFIG_CLOCK_AUTO_GATE;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200690 writel_relaxed(config, controller->base + QUP_CONFIG);
691 return 0;
692}
693#endif /* CONFIG_PM_RUNTIME */
694
695#ifdef CONFIG_PM_SLEEP
696static int spi_qup_suspend(struct device *device)
697{
698 struct spi_master *master = dev_get_drvdata(device);
699 struct spi_qup *controller = spi_master_get_devdata(master);
700 int ret;
701
702 ret = spi_master_suspend(master);
703 if (ret)
704 return ret;
705
706 ret = spi_qup_set_state(controller, QUP_STATE_RESET);
707 if (ret)
708 return ret;
709
710 clk_disable_unprepare(controller->cclk);
711 clk_disable_unprepare(controller->iclk);
712 return 0;
713}
714
715static int spi_qup_resume(struct device *device)
716{
717 struct spi_master *master = dev_get_drvdata(device);
718 struct spi_qup *controller = spi_master_get_devdata(master);
719 int ret;
720
721 ret = clk_prepare_enable(controller->iclk);
722 if (ret)
723 return ret;
724
725 ret = clk_prepare_enable(controller->cclk);
726 if (ret)
727 return ret;
728
729 ret = spi_qup_set_state(controller, QUP_STATE_RESET);
730 if (ret)
731 return ret;
732
733 return spi_master_resume(master);
734}
735#endif /* CONFIG_PM_SLEEP */
736
737static int spi_qup_remove(struct platform_device *pdev)
738{
739 struct spi_master *master = dev_get_drvdata(&pdev->dev);
740 struct spi_qup *controller = spi_master_get_devdata(master);
741 int ret;
742
743 ret = pm_runtime_get_sync(&pdev->dev);
Axel Lin3d89e142014-05-03 10:57:57 +0800744 if (ret < 0)
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200745 return ret;
746
747 ret = spi_qup_set_state(controller, QUP_STATE_RESET);
748 if (ret)
749 return ret;
750
751 clk_disable_unprepare(controller->cclk);
752 clk_disable_unprepare(controller->iclk);
753
754 pm_runtime_put_noidle(&pdev->dev);
755 pm_runtime_disable(&pdev->dev);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200756 return 0;
757}
758
Jingoo Han113b1a02014-05-07 16:50:04 +0900759static const struct of_device_id spi_qup_dt_match[] = {
Andy Gross70cea0a2014-06-12 14:34:12 -0500760 { .compatible = "qcom,spi-qup-v1.1.1", },
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200761 { .compatible = "qcom,spi-qup-v2.1.1", },
762 { .compatible = "qcom,spi-qup-v2.2.1", },
763 { }
764};
765MODULE_DEVICE_TABLE(of, spi_qup_dt_match);
766
767static const struct dev_pm_ops spi_qup_dev_pm_ops = {
768 SET_SYSTEM_SLEEP_PM_OPS(spi_qup_suspend, spi_qup_resume)
769 SET_RUNTIME_PM_OPS(spi_qup_pm_suspend_runtime,
770 spi_qup_pm_resume_runtime,
771 NULL)
772};
773
774static struct platform_driver spi_qup_driver = {
775 .driver = {
776 .name = "spi_qup",
777 .owner = THIS_MODULE,
778 .pm = &spi_qup_dev_pm_ops,
779 .of_match_table = spi_qup_dt_match,
780 },
781 .probe = spi_qup_probe,
782 .remove = spi_qup_remove,
783};
784module_platform_driver(spi_qup_driver);
785
786MODULE_LICENSE("GPL v2");
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200787MODULE_ALIAS("platform:spi_qup");