blob: 5f05d519fbbd09188032e40f9626af7e24b5a498 [file] [log] [blame]
Thomas Gleixner75a6faf2019-06-01 10:08:37 +02001// SPDX-License-Identifier: GPL-2.0-only
Andrew Brestickerdeba25802014-11-14 10:48:32 -08002/*
3 * IMG SPFI controller driver
4 *
5 * Copyright (C) 2007,2008,2013 Imagination Technologies Ltd.
6 * Copyright (C) 2014 Google, Inc.
Andrew Brestickerdeba25802014-11-14 10:48:32 -08007 */
8
9#include <linux/clk.h>
10#include <linux/delay.h>
11#include <linux/dmaengine.h>
12#include <linux/interrupt.h>
13#include <linux/io.h>
14#include <linux/irq.h>
15#include <linux/module.h>
16#include <linux/of.h>
17#include <linux/platform_device.h>
18#include <linux/pm_runtime.h>
19#include <linux/scatterlist.h>
20#include <linux/slab.h>
21#include <linux/spi/spi.h>
22#include <linux/spinlock.h>
23
24#define SPFI_DEVICE_PARAMETER(x) (0x00 + 0x4 * (x))
25#define SPFI_DEVICE_PARAMETER_BITCLK_SHIFT 24
26#define SPFI_DEVICE_PARAMETER_BITCLK_MASK 0xff
27#define SPFI_DEVICE_PARAMETER_CSSETUP_SHIFT 16
28#define SPFI_DEVICE_PARAMETER_CSSETUP_MASK 0xff
29#define SPFI_DEVICE_PARAMETER_CSHOLD_SHIFT 8
30#define SPFI_DEVICE_PARAMETER_CSHOLD_MASK 0xff
31#define SPFI_DEVICE_PARAMETER_CSDELAY_SHIFT 0
32#define SPFI_DEVICE_PARAMETER_CSDELAY_MASK 0xff
33
34#define SPFI_CONTROL 0x14
35#define SPFI_CONTROL_CONTINUE BIT(12)
36#define SPFI_CONTROL_SOFT_RESET BIT(11)
37#define SPFI_CONTROL_SEND_DMA BIT(10)
38#define SPFI_CONTROL_GET_DMA BIT(9)
Sifan Naeem6a806a22015-06-18 13:50:54 +010039#define SPFI_CONTROL_SE BIT(8)
Andrew Brestickerdeba25802014-11-14 10:48:32 -080040#define SPFI_CONTROL_TMODE_SHIFT 5
41#define SPFI_CONTROL_TMODE_MASK 0x7
42#define SPFI_CONTROL_TMODE_SINGLE 0
43#define SPFI_CONTROL_TMODE_DUAL 1
44#define SPFI_CONTROL_TMODE_QUAD 2
45#define SPFI_CONTROL_SPFI_EN BIT(0)
46
47#define SPFI_TRANSACTION 0x18
48#define SPFI_TRANSACTION_TSIZE_SHIFT 16
49#define SPFI_TRANSACTION_TSIZE_MASK 0xffff
50
51#define SPFI_PORT_STATE 0x1c
52#define SPFI_PORT_STATE_DEV_SEL_SHIFT 20
53#define SPFI_PORT_STATE_DEV_SEL_MASK 0x7
54#define SPFI_PORT_STATE_CK_POL(x) BIT(19 - (x))
55#define SPFI_PORT_STATE_CK_PHASE(x) BIT(14 - (x))
56
57#define SPFI_TX_32BIT_VALID_DATA 0x20
58#define SPFI_TX_8BIT_VALID_DATA 0x24
59#define SPFI_RX_32BIT_VALID_DATA 0x28
60#define SPFI_RX_8BIT_VALID_DATA 0x2c
61
62#define SPFI_INTERRUPT_STATUS 0x30
63#define SPFI_INTERRUPT_ENABLE 0x34
64#define SPFI_INTERRUPT_CLEAR 0x38
65#define SPFI_INTERRUPT_IACCESS BIT(12)
66#define SPFI_INTERRUPT_GDEX8BIT BIT(11)
67#define SPFI_INTERRUPT_ALLDONETRIG BIT(9)
68#define SPFI_INTERRUPT_GDFUL BIT(8)
69#define SPFI_INTERRUPT_GDHF BIT(7)
70#define SPFI_INTERRUPT_GDEX32BIT BIT(6)
71#define SPFI_INTERRUPT_GDTRIG BIT(5)
72#define SPFI_INTERRUPT_SDFUL BIT(3)
73#define SPFI_INTERRUPT_SDHF BIT(2)
74#define SPFI_INTERRUPT_SDE BIT(1)
75#define SPFI_INTERRUPT_SDTRIG BIT(0)
76
77/*
78 * There are four parallel FIFOs of 16 bytes each. The word buffer
79 * (*_32BIT_VALID_DATA) accesses all four FIFOs at once, resulting in an
80 * effective FIFO size of 64 bytes. The byte buffer (*_8BIT_VALID_DATA)
81 * accesses only a single FIFO, resulting in an effective FIFO size of
82 * 16 bytes.
83 */
84#define SPFI_32BIT_FIFO_SIZE 64
85#define SPFI_8BIT_FIFO_SIZE 16
86
87struct img_spfi {
88 struct device *dev;
89 struct spi_master *master;
90 spinlock_t lock;
91
92 void __iomem *regs;
93 phys_addr_t phys;
94 int irq;
95 struct clk *spfi_clk;
96 struct clk *sys_clk;
97
98 struct dma_chan *rx_ch;
99 struct dma_chan *tx_ch;
100 bool tx_dma_busy;
101 bool rx_dma_busy;
102};
103
104static inline u32 spfi_readl(struct img_spfi *spfi, u32 reg)
105{
106 return readl(spfi->regs + reg);
107}
108
109static inline void spfi_writel(struct img_spfi *spfi, u32 val, u32 reg)
110{
111 writel(val, spfi->regs + reg);
112}
113
114static inline void spfi_start(struct img_spfi *spfi)
115{
116 u32 val;
117
118 val = spfi_readl(spfi, SPFI_CONTROL);
119 val |= SPFI_CONTROL_SPFI_EN;
120 spfi_writel(spfi, val, SPFI_CONTROL);
121}
122
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800123static inline void spfi_reset(struct img_spfi *spfi)
124{
125 spfi_writel(spfi, SPFI_CONTROL_SOFT_RESET, SPFI_CONTROL);
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800126 spfi_writel(spfi, 0, SPFI_CONTROL);
127}
128
Ezequiel Garcia8c2c8c02015-04-08 10:03:16 -0700129static int spfi_wait_all_done(struct img_spfi *spfi)
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800130{
Ezequiel Garcia8c2c8c02015-04-08 10:03:16 -0700131 unsigned long timeout = jiffies + msecs_to_jiffies(50);
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800132
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800133 while (time_before(jiffies, timeout)) {
Ezequiel Garcia8c2c8c02015-04-08 10:03:16 -0700134 u32 status = spfi_readl(spfi, SPFI_INTERRUPT_STATUS);
135
136 if (status & SPFI_INTERRUPT_ALLDONETRIG) {
137 spfi_writel(spfi, SPFI_INTERRUPT_ALLDONETRIG,
138 SPFI_INTERRUPT_CLEAR);
139 return 0;
140 }
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800141 cpu_relax();
142 }
143
Ezequiel Garcia8c2c8c02015-04-08 10:03:16 -0700144 dev_err(spfi->dev, "Timed out waiting for transaction to complete\n");
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800145 spfi_reset(spfi);
Ezequiel Garcia8c2c8c02015-04-08 10:03:16 -0700146
147 return -ETIMEDOUT;
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800148}
149
150static unsigned int spfi_pio_write32(struct img_spfi *spfi, const u32 *buf,
151 unsigned int max)
152{
153 unsigned int count = 0;
154 u32 status;
155
Andrew Bresticker549858c2014-12-22 11:35:16 -0800156 while (count < max / 4) {
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800157 spfi_writel(spfi, SPFI_INTERRUPT_SDFUL, SPFI_INTERRUPT_CLEAR);
158 status = spfi_readl(spfi, SPFI_INTERRUPT_STATUS);
159 if (status & SPFI_INTERRUPT_SDFUL)
160 break;
Andrew Bresticker549858c2014-12-22 11:35:16 -0800161 spfi_writel(spfi, buf[count], SPFI_TX_32BIT_VALID_DATA);
162 count++;
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800163 }
164
Andrew Bresticker549858c2014-12-22 11:35:16 -0800165 return count * 4;
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800166}
167
168static unsigned int spfi_pio_write8(struct img_spfi *spfi, const u8 *buf,
169 unsigned int max)
170{
171 unsigned int count = 0;
172 u32 status;
173
174 while (count < max) {
175 spfi_writel(spfi, SPFI_INTERRUPT_SDFUL, SPFI_INTERRUPT_CLEAR);
176 status = spfi_readl(spfi, SPFI_INTERRUPT_STATUS);
177 if (status & SPFI_INTERRUPT_SDFUL)
178 break;
179 spfi_writel(spfi, buf[count], SPFI_TX_8BIT_VALID_DATA);
180 count++;
181 }
182
183 return count;
184}
185
186static unsigned int spfi_pio_read32(struct img_spfi *spfi, u32 *buf,
187 unsigned int max)
188{
189 unsigned int count = 0;
190 u32 status;
191
Andrew Bresticker549858c2014-12-22 11:35:16 -0800192 while (count < max / 4) {
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800193 spfi_writel(spfi, SPFI_INTERRUPT_GDEX32BIT,
194 SPFI_INTERRUPT_CLEAR);
195 status = spfi_readl(spfi, SPFI_INTERRUPT_STATUS);
196 if (!(status & SPFI_INTERRUPT_GDEX32BIT))
197 break;
Andrew Bresticker549858c2014-12-22 11:35:16 -0800198 buf[count] = spfi_readl(spfi, SPFI_RX_32BIT_VALID_DATA);
199 count++;
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800200 }
201
Andrew Bresticker549858c2014-12-22 11:35:16 -0800202 return count * 4;
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800203}
204
205static unsigned int spfi_pio_read8(struct img_spfi *spfi, u8 *buf,
206 unsigned int max)
207{
208 unsigned int count = 0;
209 u32 status;
210
211 while (count < max) {
212 spfi_writel(spfi, SPFI_INTERRUPT_GDEX8BIT,
213 SPFI_INTERRUPT_CLEAR);
214 status = spfi_readl(spfi, SPFI_INTERRUPT_STATUS);
215 if (!(status & SPFI_INTERRUPT_GDEX8BIT))
216 break;
217 buf[count] = spfi_readl(spfi, SPFI_RX_8BIT_VALID_DATA);
218 count++;
219 }
220
221 return count;
222}
223
224static int img_spfi_start_pio(struct spi_master *master,
225 struct spi_device *spi,
226 struct spi_transfer *xfer)
227{
228 struct img_spfi *spfi = spi_master_get_devdata(spi->master);
229 unsigned int tx_bytes = 0, rx_bytes = 0;
230 const void *tx_buf = xfer->tx_buf;
231 void *rx_buf = xfer->rx_buf;
232 unsigned long timeout;
Ezequiel Garcia8c2c8c02015-04-08 10:03:16 -0700233 int ret;
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800234
235 if (tx_buf)
236 tx_bytes = xfer->len;
237 if (rx_buf)
238 rx_bytes = xfer->len;
239
240 spfi_start(spfi);
241
242 timeout = jiffies +
243 msecs_to_jiffies(xfer->len * 8 * 1000 / xfer->speed_hz + 100);
244 while ((tx_bytes > 0 || rx_bytes > 0) &&
245 time_before(jiffies, timeout)) {
246 unsigned int tx_count, rx_count;
247
Andrew Bresticker549858c2014-12-22 11:35:16 -0800248 if (tx_bytes >= 4)
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800249 tx_count = spfi_pio_write32(spfi, tx_buf, tx_bytes);
Andrew Bresticker549858c2014-12-22 11:35:16 -0800250 else
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800251 tx_count = spfi_pio_write8(spfi, tx_buf, tx_bytes);
Andrew Bresticker549858c2014-12-22 11:35:16 -0800252
253 if (rx_bytes >= 4)
254 rx_count = spfi_pio_read32(spfi, rx_buf, rx_bytes);
255 else
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800256 rx_count = spfi_pio_read8(spfi, rx_buf, rx_bytes);
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800257
258 tx_buf += tx_count;
259 rx_buf += rx_count;
260 tx_bytes -= tx_count;
261 rx_bytes -= rx_count;
262
263 cpu_relax();
264 }
265
266 if (rx_bytes > 0 || tx_bytes > 0) {
267 dev_err(spfi->dev, "PIO transfer timed out\n");
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800268 return -ETIMEDOUT;
269 }
270
Sifan Naeem011710e2015-07-27 13:11:15 +0100271 ret = spfi_wait_all_done(spfi);
272 if (ret < 0)
273 return ret;
274
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800275 return 0;
276}
277
278static void img_spfi_dma_rx_cb(void *data)
279{
280 struct img_spfi *spfi = data;
281 unsigned long flags;
282
Ezequiel Garcia8c2c8c02015-04-08 10:03:16 -0700283 spfi_wait_all_done(spfi);
284
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800285 spin_lock_irqsave(&spfi->lock, flags);
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800286 spfi->rx_dma_busy = false;
Ezequiel Garcia8c2c8c02015-04-08 10:03:16 -0700287 if (!spfi->tx_dma_busy)
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800288 spi_finalize_current_transfer(spfi->master);
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800289 spin_unlock_irqrestore(&spfi->lock, flags);
290}
291
292static void img_spfi_dma_tx_cb(void *data)
293{
294 struct img_spfi *spfi = data;
295 unsigned long flags;
296
Ezequiel Garcia8c2c8c02015-04-08 10:03:16 -0700297 spfi_wait_all_done(spfi);
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800298
299 spin_lock_irqsave(&spfi->lock, flags);
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800300 spfi->tx_dma_busy = false;
Ezequiel Garcia8c2c8c02015-04-08 10:03:16 -0700301 if (!spfi->rx_dma_busy)
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800302 spi_finalize_current_transfer(spfi->master);
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800303 spin_unlock_irqrestore(&spfi->lock, flags);
304}
305
306static int img_spfi_start_dma(struct spi_master *master,
307 struct spi_device *spi,
308 struct spi_transfer *xfer)
309{
310 struct img_spfi *spfi = spi_master_get_devdata(spi->master);
311 struct dma_async_tx_descriptor *rxdesc = NULL, *txdesc = NULL;
312 struct dma_slave_config rxconf, txconf;
313
314 spfi->rx_dma_busy = false;
315 spfi->tx_dma_busy = false;
316
317 if (xfer->rx_buf) {
318 rxconf.direction = DMA_DEV_TO_MEM;
Andrew Bresticker549858c2014-12-22 11:35:16 -0800319 if (xfer->len % 4 == 0) {
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800320 rxconf.src_addr = spfi->phys + SPFI_RX_32BIT_VALID_DATA;
321 rxconf.src_addr_width = 4;
322 rxconf.src_maxburst = 4;
Andrew Bresticker549858c2014-12-22 11:35:16 -0800323 } else {
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800324 rxconf.src_addr = spfi->phys + SPFI_RX_8BIT_VALID_DATA;
325 rxconf.src_addr_width = 1;
Andrew Bresticker76fe5e92014-12-22 11:35:15 -0800326 rxconf.src_maxburst = 4;
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800327 }
328 dmaengine_slave_config(spfi->rx_ch, &rxconf);
329
330 rxdesc = dmaengine_prep_slave_sg(spfi->rx_ch, xfer->rx_sg.sgl,
331 xfer->rx_sg.nents,
332 DMA_DEV_TO_MEM,
333 DMA_PREP_INTERRUPT);
334 if (!rxdesc)
335 goto stop_dma;
336
337 rxdesc->callback = img_spfi_dma_rx_cb;
338 rxdesc->callback_param = spfi;
339 }
340
341 if (xfer->tx_buf) {
342 txconf.direction = DMA_MEM_TO_DEV;
Andrew Bresticker549858c2014-12-22 11:35:16 -0800343 if (xfer->len % 4 == 0) {
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800344 txconf.dst_addr = spfi->phys + SPFI_TX_32BIT_VALID_DATA;
345 txconf.dst_addr_width = 4;
346 txconf.dst_maxburst = 4;
Andrew Bresticker549858c2014-12-22 11:35:16 -0800347 } else {
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800348 txconf.dst_addr = spfi->phys + SPFI_TX_8BIT_VALID_DATA;
349 txconf.dst_addr_width = 1;
Andrew Bresticker76fe5e92014-12-22 11:35:15 -0800350 txconf.dst_maxburst = 4;
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800351 }
352 dmaengine_slave_config(spfi->tx_ch, &txconf);
353
354 txdesc = dmaengine_prep_slave_sg(spfi->tx_ch, xfer->tx_sg.sgl,
355 xfer->tx_sg.nents,
356 DMA_MEM_TO_DEV,
357 DMA_PREP_INTERRUPT);
358 if (!txdesc)
359 goto stop_dma;
360
361 txdesc->callback = img_spfi_dma_tx_cb;
362 txdesc->callback_param = spfi;
363 }
364
365 if (xfer->rx_buf) {
366 spfi->rx_dma_busy = true;
367 dmaengine_submit(rxdesc);
368 dma_async_issue_pending(spfi->rx_ch);
369 }
370
Andrew Brestickerc0e7dc22014-12-16 13:05:44 -0800371 spfi_start(spfi);
372
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800373 if (xfer->tx_buf) {
374 spfi->tx_dma_busy = true;
375 dmaengine_submit(txdesc);
376 dma_async_issue_pending(spfi->tx_ch);
377 }
378
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800379 return 1;
380
381stop_dma:
382 dmaengine_terminate_all(spfi->rx_ch);
383 dmaengine_terminate_all(spfi->tx_ch);
384 return -EIO;
385}
386
Ezequiel Garcia824ab372015-04-08 10:03:14 -0700387static void img_spfi_handle_err(struct spi_master *master,
388 struct spi_message *msg)
389{
390 struct img_spfi *spfi = spi_master_get_devdata(master);
391 unsigned long flags;
392
393 /*
394 * Stop all DMA and reset the controller if the previous transaction
395 * timed-out and never completed it's DMA.
396 */
397 spin_lock_irqsave(&spfi->lock, flags);
398 if (spfi->tx_dma_busy || spfi->rx_dma_busy) {
399 spfi->tx_dma_busy = false;
400 spfi->rx_dma_busy = false;
401
402 dmaengine_terminate_all(spfi->tx_ch);
403 dmaengine_terminate_all(spfi->rx_ch);
404 }
405 spin_unlock_irqrestore(&spfi->lock, flags);
Ezequiel Garcia824ab372015-04-08 10:03:14 -0700406}
407
Ezequiel Garciab6fe3972015-04-06 14:29:04 -0700408static int img_spfi_prepare(struct spi_master *master, struct spi_message *msg)
409{
410 struct img_spfi *spfi = spi_master_get_devdata(master);
411 u32 val;
412
413 val = spfi_readl(spfi, SPFI_PORT_STATE);
Ionela Voinescubaec8eb2018-07-22 23:20:06 +0200414 val &= ~(SPFI_PORT_STATE_DEV_SEL_MASK <<
415 SPFI_PORT_STATE_DEV_SEL_SHIFT);
416 val |= msg->spi->chip_select << SPFI_PORT_STATE_DEV_SEL_SHIFT;
Ezequiel Garciab6fe3972015-04-06 14:29:04 -0700417 if (msg->spi->mode & SPI_CPHA)
418 val |= SPFI_PORT_STATE_CK_PHASE(msg->spi->chip_select);
419 else
420 val &= ~SPFI_PORT_STATE_CK_PHASE(msg->spi->chip_select);
421 if (msg->spi->mode & SPI_CPOL)
422 val |= SPFI_PORT_STATE_CK_POL(msg->spi->chip_select);
423 else
424 val &= ~SPFI_PORT_STATE_CK_POL(msg->spi->chip_select);
425 spfi_writel(spfi, val, SPFI_PORT_STATE);
426
427 return 0;
428}
429
Andrew Brestickerba33d8a2015-04-08 10:03:15 -0700430static int img_spfi_unprepare(struct spi_master *master,
431 struct spi_message *msg)
432{
433 struct img_spfi *spfi = spi_master_get_devdata(master);
434
435 spfi_reset(spfi);
436
437 return 0;
438}
439
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800440static void img_spfi_config(struct spi_master *master, struct spi_device *spi,
441 struct spi_transfer *xfer)
442{
443 struct img_spfi *spfi = spi_master_get_devdata(spi->master);
444 u32 val, div;
445
446 /*
447 * output = spfi_clk * (BITCLK / 512), where BITCLK must be a
Andrew Bresticker8543d0e2015-04-06 14:29:03 -0700448 * power of 2 up to 128
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800449 */
Andrew Bresticker8543d0e2015-04-06 14:29:03 -0700450 div = DIV_ROUND_UP(clk_get_rate(spfi->spfi_clk), xfer->speed_hz);
451 div = clamp(512 / (1 << get_count_order(div)), 1, 128);
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800452
453 val = spfi_readl(spfi, SPFI_DEVICE_PARAMETER(spi->chip_select));
454 val &= ~(SPFI_DEVICE_PARAMETER_BITCLK_MASK <<
455 SPFI_DEVICE_PARAMETER_BITCLK_SHIFT);
456 val |= div << SPFI_DEVICE_PARAMETER_BITCLK_SHIFT;
457 spfi_writel(spfi, val, SPFI_DEVICE_PARAMETER(spi->chip_select));
458
Sifan Naeemede8342b2015-04-06 14:29:06 -0700459 spfi_writel(spfi, xfer->len << SPFI_TRANSACTION_TSIZE_SHIFT,
460 SPFI_TRANSACTION);
461
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800462 val = spfi_readl(spfi, SPFI_CONTROL);
463 val &= ~(SPFI_CONTROL_SEND_DMA | SPFI_CONTROL_GET_DMA);
464 if (xfer->tx_buf)
465 val |= SPFI_CONTROL_SEND_DMA;
466 if (xfer->rx_buf)
467 val |= SPFI_CONTROL_GET_DMA;
468 val &= ~(SPFI_CONTROL_TMODE_MASK << SPFI_CONTROL_TMODE_SHIFT);
469 if (xfer->tx_nbits == SPI_NBITS_DUAL &&
470 xfer->rx_nbits == SPI_NBITS_DUAL)
471 val |= SPFI_CONTROL_TMODE_DUAL << SPFI_CONTROL_TMODE_SHIFT;
472 else if (xfer->tx_nbits == SPI_NBITS_QUAD &&
473 xfer->rx_nbits == SPI_NBITS_QUAD)
474 val |= SPFI_CONTROL_TMODE_QUAD << SPFI_CONTROL_TMODE_SHIFT;
Sifan Naeem6a806a22015-06-18 13:50:54 +0100475 val |= SPFI_CONTROL_SE;
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800476 spfi_writel(spfi, val, SPFI_CONTROL);
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800477}
478
479static int img_spfi_transfer_one(struct spi_master *master,
480 struct spi_device *spi,
481 struct spi_transfer *xfer)
482{
483 struct img_spfi *spfi = spi_master_get_devdata(spi->master);
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800484 int ret;
485
Sifan Naeemf165ed62015-03-02 16:06:46 +0000486 if (xfer->len > SPFI_TRANSACTION_TSIZE_MASK) {
487 dev_err(spfi->dev,
488 "Transfer length (%d) is greater than the max supported (%d)",
489 xfer->len, SPFI_TRANSACTION_TSIZE_MASK);
490 return -EINVAL;
491 }
492
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800493 img_spfi_config(master, spi, xfer);
494 if (master->can_dma && master->can_dma(master, spi, xfer))
495 ret = img_spfi_start_dma(master, spi, xfer);
496 else
497 ret = img_spfi_start_pio(master, spi, xfer);
498
499 return ret;
500}
501
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800502static bool img_spfi_can_dma(struct spi_master *master, struct spi_device *spi,
503 struct spi_transfer *xfer)
504{
Andrew Bresticker549858c2014-12-22 11:35:16 -0800505 if (xfer->len > SPFI_32BIT_FIFO_SIZE)
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800506 return true;
507 return false;
508}
509
510static irqreturn_t img_spfi_irq(int irq, void *dev_id)
511{
512 struct img_spfi *spfi = (struct img_spfi *)dev_id;
513 u32 status;
514
515 status = spfi_readl(spfi, SPFI_INTERRUPT_STATUS);
516 if (status & SPFI_INTERRUPT_IACCESS) {
517 spfi_writel(spfi, SPFI_INTERRUPT_IACCESS, SPFI_INTERRUPT_CLEAR);
518 dev_err(spfi->dev, "Illegal access interrupt");
519 return IRQ_HANDLED;
520 }
521
522 return IRQ_NONE;
523}
524
525static int img_spfi_probe(struct platform_device *pdev)
526{
527 struct spi_master *master;
528 struct img_spfi *spfi;
529 struct resource *res;
530 int ret;
Sifan Naeem93e3a9e2015-06-18 13:27:12 +0100531 u32 max_speed_hz;
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800532
533 master = spi_alloc_master(&pdev->dev, sizeof(*spfi));
534 if (!master)
535 return -ENOMEM;
536 platform_set_drvdata(pdev, master);
537
538 spfi = spi_master_get_devdata(master);
539 spfi->dev = &pdev->dev;
540 spfi->master = master;
541 spin_lock_init(&spfi->lock);
542
543 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
544 spfi->regs = devm_ioremap_resource(spfi->dev, res);
545 if (IS_ERR(spfi->regs)) {
546 ret = PTR_ERR(spfi->regs);
547 goto put_spi;
548 }
549 spfi->phys = res->start;
550
551 spfi->irq = platform_get_irq(pdev, 0);
552 if (spfi->irq < 0) {
553 ret = spfi->irq;
554 goto put_spi;
555 }
556 ret = devm_request_irq(spfi->dev, spfi->irq, img_spfi_irq,
557 IRQ_TYPE_LEVEL_HIGH, dev_name(spfi->dev), spfi);
558 if (ret)
559 goto put_spi;
560
561 spfi->sys_clk = devm_clk_get(spfi->dev, "sys");
562 if (IS_ERR(spfi->sys_clk)) {
563 ret = PTR_ERR(spfi->sys_clk);
564 goto put_spi;
565 }
566 spfi->spfi_clk = devm_clk_get(spfi->dev, "spfi");
567 if (IS_ERR(spfi->spfi_clk)) {
568 ret = PTR_ERR(spfi->spfi_clk);
569 goto put_spi;
570 }
571
572 ret = clk_prepare_enable(spfi->sys_clk);
573 if (ret)
574 goto put_spi;
575 ret = clk_prepare_enable(spfi->spfi_clk);
576 if (ret)
577 goto disable_pclk;
578
579 spfi_reset(spfi);
580 /*
581 * Only enable the error (IACCESS) interrupt. In PIO mode we'll
582 * poll the status of the FIFOs.
583 */
584 spfi_writel(spfi, SPFI_INTERRUPT_IACCESS, SPFI_INTERRUPT_ENABLE);
585
586 master->auto_runtime_pm = true;
587 master->bus_num = pdev->id;
588 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_TX_DUAL | SPI_RX_DUAL;
589 if (of_property_read_bool(spfi->dev->of_node, "img,supports-quad-mode"))
590 master->mode_bits |= SPI_TX_QUAD | SPI_RX_QUAD;
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800591 master->dev.of_node = pdev->dev.of_node;
592 master->bits_per_word_mask = SPI_BPW_MASK(32) | SPI_BPW_MASK(8);
Andrew Bresticker8543d0e2015-04-06 14:29:03 -0700593 master->max_speed_hz = clk_get_rate(spfi->spfi_clk) / 4;
594 master->min_speed_hz = clk_get_rate(spfi->spfi_clk) / 512;
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800595
Sifan Naeem93e3a9e2015-06-18 13:27:12 +0100596 /*
597 * Maximum speed supported by spfi is limited to the lower value
598 * between 1/4 of the SPFI clock or to "spfi-max-frequency"
599 * defined in the device tree.
600 * If no value is defined in the device tree assume the maximum
601 * speed supported to be 1/4 of the SPFI clock.
602 */
603 if (!of_property_read_u32(spfi->dev->of_node, "spfi-max-frequency",
604 &max_speed_hz)) {
605 if (master->max_speed_hz > max_speed_hz)
606 master->max_speed_hz = max_speed_hz;
607 }
608
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800609 master->transfer_one = img_spfi_transfer_one;
Ezequiel Garciab6fe3972015-04-06 14:29:04 -0700610 master->prepare_message = img_spfi_prepare;
Andrew Brestickerba33d8a2015-04-08 10:03:15 -0700611 master->unprepare_message = img_spfi_unprepare;
Ezequiel Garcia824ab372015-04-08 10:03:14 -0700612 master->handle_err = img_spfi_handle_err;
Linus Walleij27e23ca2020-06-25 22:14:22 +0200613 master->use_gpio_descriptors = true;
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800614
Peter Ujfalusi6bfbf4d2019-12-12 15:55:45 +0200615 spfi->tx_ch = dma_request_chan(spfi->dev, "tx");
616 if (IS_ERR(spfi->tx_ch)) {
617 ret = PTR_ERR(spfi->tx_ch);
618 spfi->tx_ch = NULL;
619 if (ret == -EPROBE_DEFER)
620 goto disable_pm;
621 }
622
623 spfi->rx_ch = dma_request_chan(spfi->dev, "rx");
624 if (IS_ERR(spfi->rx_ch)) {
625 ret = PTR_ERR(spfi->rx_ch);
626 spfi->rx_ch = NULL;
627 if (ret == -EPROBE_DEFER)
628 goto disable_pm;
629 }
630
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800631 if (!spfi->tx_ch || !spfi->rx_ch) {
632 if (spfi->tx_ch)
633 dma_release_channel(spfi->tx_ch);
634 if (spfi->rx_ch)
635 dma_release_channel(spfi->rx_ch);
Pan Biane9a8ba92019-11-06 10:36:09 +0800636 spfi->tx_ch = NULL;
637 spfi->rx_ch = NULL;
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800638 dev_warn(spfi->dev, "Failed to get DMA channels, falling back to PIO mode\n");
639 } else {
640 master->dma_tx = spfi->tx_ch;
641 master->dma_rx = spfi->rx_ch;
642 master->can_dma = img_spfi_can_dma;
643 }
644
645 pm_runtime_set_active(spfi->dev);
646 pm_runtime_enable(spfi->dev);
647
648 ret = devm_spi_register_master(spfi->dev, master);
649 if (ret)
650 goto disable_pm;
651
652 return 0;
653
654disable_pm:
655 pm_runtime_disable(spfi->dev);
656 if (spfi->rx_ch)
657 dma_release_channel(spfi->rx_ch);
658 if (spfi->tx_ch)
659 dma_release_channel(spfi->tx_ch);
660 clk_disable_unprepare(spfi->spfi_clk);
661disable_pclk:
662 clk_disable_unprepare(spfi->sys_clk);
663put_spi:
664 spi_master_put(master);
665
666 return ret;
667}
668
669static int img_spfi_remove(struct platform_device *pdev)
670{
671 struct spi_master *master = platform_get_drvdata(pdev);
672 struct img_spfi *spfi = spi_master_get_devdata(master);
673
674 if (spfi->tx_ch)
675 dma_release_channel(spfi->tx_ch);
676 if (spfi->rx_ch)
677 dma_release_channel(spfi->rx_ch);
678
679 pm_runtime_disable(spfi->dev);
680 if (!pm_runtime_status_suspended(spfi->dev)) {
681 clk_disable_unprepare(spfi->spfi_clk);
682 clk_disable_unprepare(spfi->sys_clk);
683 }
684
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800685 return 0;
686}
687
Rafael J. Wysocki47164fd2014-12-19 15:25:31 +0100688#ifdef CONFIG_PM
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800689static int img_spfi_runtime_suspend(struct device *dev)
690{
691 struct spi_master *master = dev_get_drvdata(dev);
692 struct img_spfi *spfi = spi_master_get_devdata(master);
693
694 clk_disable_unprepare(spfi->spfi_clk);
695 clk_disable_unprepare(spfi->sys_clk);
696
697 return 0;
698}
699
700static int img_spfi_runtime_resume(struct device *dev)
701{
702 struct spi_master *master = dev_get_drvdata(dev);
703 struct img_spfi *spfi = spi_master_get_devdata(master);
704 int ret;
705
706 ret = clk_prepare_enable(spfi->sys_clk);
707 if (ret)
708 return ret;
709 ret = clk_prepare_enable(spfi->spfi_clk);
710 if (ret) {
711 clk_disable_unprepare(spfi->sys_clk);
712 return ret;
713 }
714
715 return 0;
716}
Rafael J. Wysocki47164fd2014-12-19 15:25:31 +0100717#endif /* CONFIG_PM */
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800718
719#ifdef CONFIG_PM_SLEEP
720static int img_spfi_suspend(struct device *dev)
721{
722 struct spi_master *master = dev_get_drvdata(dev);
723
724 return spi_master_suspend(master);
725}
726
727static int img_spfi_resume(struct device *dev)
728{
729 struct spi_master *master = dev_get_drvdata(dev);
730 struct img_spfi *spfi = spi_master_get_devdata(master);
731 int ret;
732
733 ret = pm_runtime_get_sync(dev);
Zhang Qilongee5558a2020-11-02 22:56:51 +0800734 if (ret) {
735 pm_runtime_put_noidle(dev);
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800736 return ret;
Zhang Qilongee5558a2020-11-02 22:56:51 +0800737 }
Andrew Brestickerdeba25802014-11-14 10:48:32 -0800738 spfi_reset(spfi);
739 pm_runtime_put(dev);
740
741 return spi_master_resume(master);
742}
743#endif /* CONFIG_PM_SLEEP */
744
745static const struct dev_pm_ops img_spfi_pm_ops = {
746 SET_RUNTIME_PM_OPS(img_spfi_runtime_suspend, img_spfi_runtime_resume,
747 NULL)
748 SET_SYSTEM_SLEEP_PM_OPS(img_spfi_suspend, img_spfi_resume)
749};
750
751static const struct of_device_id img_spfi_of_match[] = {
752 { .compatible = "img,spfi", },
753 { },
754};
755MODULE_DEVICE_TABLE(of, img_spfi_of_match);
756
757static struct platform_driver img_spfi_driver = {
758 .driver = {
759 .name = "img-spfi",
760 .pm = &img_spfi_pm_ops,
761 .of_match_table = of_match_ptr(img_spfi_of_match),
762 },
763 .probe = img_spfi_probe,
764 .remove = img_spfi_remove,
765};
766module_platform_driver(img_spfi_driver);
767
768MODULE_DESCRIPTION("IMG SPFI controller driver");
769MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>");
770MODULE_LICENSE("GPL v2");