]>
Commit | Line | Data |
---|---|---|
deba2580 AB |
1 | /* |
2 | * IMG SPFI controller driver | |
3 | * | |
4 | * Copyright (C) 2007,2008,2013 Imagination Technologies Ltd. | |
5 | * Copyright (C) 2014 Google, Inc. | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify it | |
8 | * under the terms and conditions of the GNU General Public License, | |
9 | * version 2, as published by the Free Software Foundation. | |
10 | */ | |
11 | ||
12 | #include <linux/clk.h> | |
13 | #include <linux/delay.h> | |
14 | #include <linux/dmaengine.h> | |
15 | #include <linux/interrupt.h> | |
16 | #include <linux/io.h> | |
17 | #include <linux/irq.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/of.h> | |
20 | #include <linux/platform_device.h> | |
21 | #include <linux/pm_runtime.h> | |
22 | #include <linux/scatterlist.h> | |
23 | #include <linux/slab.h> | |
24 | #include <linux/spi/spi.h> | |
25 | #include <linux/spinlock.h> | |
26 | ||
27 | #define SPFI_DEVICE_PARAMETER(x) (0x00 + 0x4 * (x)) | |
28 | #define SPFI_DEVICE_PARAMETER_BITCLK_SHIFT 24 | |
29 | #define SPFI_DEVICE_PARAMETER_BITCLK_MASK 0xff | |
30 | #define SPFI_DEVICE_PARAMETER_CSSETUP_SHIFT 16 | |
31 | #define SPFI_DEVICE_PARAMETER_CSSETUP_MASK 0xff | |
32 | #define SPFI_DEVICE_PARAMETER_CSHOLD_SHIFT 8 | |
33 | #define SPFI_DEVICE_PARAMETER_CSHOLD_MASK 0xff | |
34 | #define SPFI_DEVICE_PARAMETER_CSDELAY_SHIFT 0 | |
35 | #define SPFI_DEVICE_PARAMETER_CSDELAY_MASK 0xff | |
36 | ||
37 | #define SPFI_CONTROL 0x14 | |
38 | #define SPFI_CONTROL_CONTINUE BIT(12) | |
39 | #define SPFI_CONTROL_SOFT_RESET BIT(11) | |
40 | #define SPFI_CONTROL_SEND_DMA BIT(10) | |
41 | #define SPFI_CONTROL_GET_DMA BIT(9) | |
42 | #define SPFI_CONTROL_TMODE_SHIFT 5 | |
43 | #define SPFI_CONTROL_TMODE_MASK 0x7 | |
44 | #define SPFI_CONTROL_TMODE_SINGLE 0 | |
45 | #define SPFI_CONTROL_TMODE_DUAL 1 | |
46 | #define SPFI_CONTROL_TMODE_QUAD 2 | |
47 | #define SPFI_CONTROL_SPFI_EN BIT(0) | |
48 | ||
49 | #define SPFI_TRANSACTION 0x18 | |
50 | #define SPFI_TRANSACTION_TSIZE_SHIFT 16 | |
51 | #define SPFI_TRANSACTION_TSIZE_MASK 0xffff | |
52 | ||
53 | #define SPFI_PORT_STATE 0x1c | |
54 | #define SPFI_PORT_STATE_DEV_SEL_SHIFT 20 | |
55 | #define SPFI_PORT_STATE_DEV_SEL_MASK 0x7 | |
56 | #define SPFI_PORT_STATE_CK_POL(x) BIT(19 - (x)) | |
57 | #define SPFI_PORT_STATE_CK_PHASE(x) BIT(14 - (x)) | |
58 | ||
59 | #define SPFI_TX_32BIT_VALID_DATA 0x20 | |
60 | #define SPFI_TX_8BIT_VALID_DATA 0x24 | |
61 | #define SPFI_RX_32BIT_VALID_DATA 0x28 | |
62 | #define SPFI_RX_8BIT_VALID_DATA 0x2c | |
63 | ||
64 | #define SPFI_INTERRUPT_STATUS 0x30 | |
65 | #define SPFI_INTERRUPT_ENABLE 0x34 | |
66 | #define SPFI_INTERRUPT_CLEAR 0x38 | |
67 | #define SPFI_INTERRUPT_IACCESS BIT(12) | |
68 | #define SPFI_INTERRUPT_GDEX8BIT BIT(11) | |
69 | #define SPFI_INTERRUPT_ALLDONETRIG BIT(9) | |
70 | #define SPFI_INTERRUPT_GDFUL BIT(8) | |
71 | #define SPFI_INTERRUPT_GDHF BIT(7) | |
72 | #define SPFI_INTERRUPT_GDEX32BIT BIT(6) | |
73 | #define SPFI_INTERRUPT_GDTRIG BIT(5) | |
74 | #define SPFI_INTERRUPT_SDFUL BIT(3) | |
75 | #define SPFI_INTERRUPT_SDHF BIT(2) | |
76 | #define SPFI_INTERRUPT_SDE BIT(1) | |
77 | #define SPFI_INTERRUPT_SDTRIG BIT(0) | |
78 | ||
79 | /* | |
80 | * There are four parallel FIFOs of 16 bytes each. The word buffer | |
81 | * (*_32BIT_VALID_DATA) accesses all four FIFOs at once, resulting in an | |
82 | * effective FIFO size of 64 bytes. The byte buffer (*_8BIT_VALID_DATA) | |
83 | * accesses only a single FIFO, resulting in an effective FIFO size of | |
84 | * 16 bytes. | |
85 | */ | |
86 | #define SPFI_32BIT_FIFO_SIZE 64 | |
87 | #define SPFI_8BIT_FIFO_SIZE 16 | |
88 | ||
89 | struct img_spfi { | |
90 | struct device *dev; | |
91 | struct spi_master *master; | |
92 | spinlock_t lock; | |
93 | ||
94 | void __iomem *regs; | |
95 | phys_addr_t phys; | |
96 | int irq; | |
97 | struct clk *spfi_clk; | |
98 | struct clk *sys_clk; | |
99 | ||
100 | struct dma_chan *rx_ch; | |
101 | struct dma_chan *tx_ch; | |
102 | bool tx_dma_busy; | |
103 | bool rx_dma_busy; | |
104 | }; | |
105 | ||
106 | static inline u32 spfi_readl(struct img_spfi *spfi, u32 reg) | |
107 | { | |
108 | return readl(spfi->regs + reg); | |
109 | } | |
110 | ||
111 | static inline void spfi_writel(struct img_spfi *spfi, u32 val, u32 reg) | |
112 | { | |
113 | writel(val, spfi->regs + reg); | |
114 | } | |
115 | ||
116 | static inline void spfi_start(struct img_spfi *spfi) | |
117 | { | |
118 | u32 val; | |
119 | ||
120 | val = spfi_readl(spfi, SPFI_CONTROL); | |
121 | val |= SPFI_CONTROL_SPFI_EN; | |
122 | spfi_writel(spfi, val, SPFI_CONTROL); | |
123 | } | |
124 | ||
125 | static inline void spfi_stop(struct img_spfi *spfi) | |
126 | { | |
127 | u32 val; | |
128 | ||
129 | val = spfi_readl(spfi, SPFI_CONTROL); | |
130 | val &= ~SPFI_CONTROL_SPFI_EN; | |
131 | spfi_writel(spfi, val, SPFI_CONTROL); | |
132 | } | |
133 | ||
134 | static inline void spfi_reset(struct img_spfi *spfi) | |
135 | { | |
136 | spfi_writel(spfi, SPFI_CONTROL_SOFT_RESET, SPFI_CONTROL); | |
deba2580 AB |
137 | spfi_writel(spfi, 0, SPFI_CONTROL); |
138 | } | |
139 | ||
140 | static void spfi_flush_tx_fifo(struct img_spfi *spfi) | |
141 | { | |
142 | unsigned long timeout = jiffies + msecs_to_jiffies(10); | |
143 | ||
144 | spfi_writel(spfi, SPFI_INTERRUPT_SDE, SPFI_INTERRUPT_CLEAR); | |
145 | while (time_before(jiffies, timeout)) { | |
146 | if (spfi_readl(spfi, SPFI_INTERRUPT_STATUS) & | |
147 | SPFI_INTERRUPT_SDE) | |
148 | return; | |
149 | cpu_relax(); | |
150 | } | |
151 | ||
152 | dev_err(spfi->dev, "Timed out waiting for FIFO to drain\n"); | |
153 | spfi_reset(spfi); | |
154 | } | |
155 | ||
156 | static unsigned int spfi_pio_write32(struct img_spfi *spfi, const u32 *buf, | |
157 | unsigned int max) | |
158 | { | |
159 | unsigned int count = 0; | |
160 | u32 status; | |
161 | ||
549858ce | 162 | while (count < max / 4) { |
deba2580 AB |
163 | spfi_writel(spfi, SPFI_INTERRUPT_SDFUL, SPFI_INTERRUPT_CLEAR); |
164 | status = spfi_readl(spfi, SPFI_INTERRUPT_STATUS); | |
165 | if (status & SPFI_INTERRUPT_SDFUL) | |
166 | break; | |
549858ce AB |
167 | spfi_writel(spfi, buf[count], SPFI_TX_32BIT_VALID_DATA); |
168 | count++; | |
deba2580 AB |
169 | } |
170 | ||
549858ce | 171 | return count * 4; |
deba2580 AB |
172 | } |
173 | ||
174 | static unsigned int spfi_pio_write8(struct img_spfi *spfi, const u8 *buf, | |
175 | unsigned int max) | |
176 | { | |
177 | unsigned int count = 0; | |
178 | u32 status; | |
179 | ||
180 | while (count < max) { | |
181 | spfi_writel(spfi, SPFI_INTERRUPT_SDFUL, SPFI_INTERRUPT_CLEAR); | |
182 | status = spfi_readl(spfi, SPFI_INTERRUPT_STATUS); | |
183 | if (status & SPFI_INTERRUPT_SDFUL) | |
184 | break; | |
185 | spfi_writel(spfi, buf[count], SPFI_TX_8BIT_VALID_DATA); | |
186 | count++; | |
187 | } | |
188 | ||
189 | return count; | |
190 | } | |
191 | ||
192 | static unsigned int spfi_pio_read32(struct img_spfi *spfi, u32 *buf, | |
193 | unsigned int max) | |
194 | { | |
195 | unsigned int count = 0; | |
196 | u32 status; | |
197 | ||
549858ce | 198 | while (count < max / 4) { |
deba2580 AB |
199 | spfi_writel(spfi, SPFI_INTERRUPT_GDEX32BIT, |
200 | SPFI_INTERRUPT_CLEAR); | |
201 | status = spfi_readl(spfi, SPFI_INTERRUPT_STATUS); | |
202 | if (!(status & SPFI_INTERRUPT_GDEX32BIT)) | |
203 | break; | |
549858ce AB |
204 | buf[count] = spfi_readl(spfi, SPFI_RX_32BIT_VALID_DATA); |
205 | count++; | |
deba2580 AB |
206 | } |
207 | ||
549858ce | 208 | return count * 4; |
deba2580 AB |
209 | } |
210 | ||
211 | static unsigned int spfi_pio_read8(struct img_spfi *spfi, u8 *buf, | |
212 | unsigned int max) | |
213 | { | |
214 | unsigned int count = 0; | |
215 | u32 status; | |
216 | ||
217 | while (count < max) { | |
218 | spfi_writel(spfi, SPFI_INTERRUPT_GDEX8BIT, | |
219 | SPFI_INTERRUPT_CLEAR); | |
220 | status = spfi_readl(spfi, SPFI_INTERRUPT_STATUS); | |
221 | if (!(status & SPFI_INTERRUPT_GDEX8BIT)) | |
222 | break; | |
223 | buf[count] = spfi_readl(spfi, SPFI_RX_8BIT_VALID_DATA); | |
224 | count++; | |
225 | } | |
226 | ||
227 | return count; | |
228 | } | |
229 | ||
230 | static int img_spfi_start_pio(struct spi_master *master, | |
231 | struct spi_device *spi, | |
232 | struct spi_transfer *xfer) | |
233 | { | |
234 | struct img_spfi *spfi = spi_master_get_devdata(spi->master); | |
235 | unsigned int tx_bytes = 0, rx_bytes = 0; | |
236 | const void *tx_buf = xfer->tx_buf; | |
237 | void *rx_buf = xfer->rx_buf; | |
238 | unsigned long timeout; | |
239 | ||
240 | if (tx_buf) | |
241 | tx_bytes = xfer->len; | |
242 | if (rx_buf) | |
243 | rx_bytes = xfer->len; | |
244 | ||
245 | spfi_start(spfi); | |
246 | ||
247 | timeout = jiffies + | |
248 | msecs_to_jiffies(xfer->len * 8 * 1000 / xfer->speed_hz + 100); | |
249 | while ((tx_bytes > 0 || rx_bytes > 0) && | |
250 | time_before(jiffies, timeout)) { | |
251 | unsigned int tx_count, rx_count; | |
252 | ||
549858ce | 253 | if (tx_bytes >= 4) |
deba2580 | 254 | tx_count = spfi_pio_write32(spfi, tx_buf, tx_bytes); |
549858ce | 255 | else |
deba2580 | 256 | tx_count = spfi_pio_write8(spfi, tx_buf, tx_bytes); |
549858ce AB |
257 | |
258 | if (rx_bytes >= 4) | |
259 | rx_count = spfi_pio_read32(spfi, rx_buf, rx_bytes); | |
260 | else | |
deba2580 | 261 | rx_count = spfi_pio_read8(spfi, rx_buf, rx_bytes); |
deba2580 AB |
262 | |
263 | tx_buf += tx_count; | |
264 | rx_buf += rx_count; | |
265 | tx_bytes -= tx_count; | |
266 | rx_bytes -= rx_count; | |
267 | ||
268 | cpu_relax(); | |
269 | } | |
270 | ||
271 | if (rx_bytes > 0 || tx_bytes > 0) { | |
272 | dev_err(spfi->dev, "PIO transfer timed out\n"); | |
273 | spfi_reset(spfi); | |
274 | return -ETIMEDOUT; | |
275 | } | |
276 | ||
277 | if (tx_buf) | |
278 | spfi_flush_tx_fifo(spfi); | |
279 | spfi_stop(spfi); | |
280 | ||
281 | return 0; | |
282 | } | |
283 | ||
284 | static void img_spfi_dma_rx_cb(void *data) | |
285 | { | |
286 | struct img_spfi *spfi = data; | |
287 | unsigned long flags; | |
288 | ||
289 | spin_lock_irqsave(&spfi->lock, flags); | |
290 | ||
291 | spfi->rx_dma_busy = false; | |
292 | if (!spfi->tx_dma_busy) { | |
293 | spfi_stop(spfi); | |
294 | spi_finalize_current_transfer(spfi->master); | |
295 | } | |
296 | ||
297 | spin_unlock_irqrestore(&spfi->lock, flags); | |
298 | } | |
299 | ||
300 | static void img_spfi_dma_tx_cb(void *data) | |
301 | { | |
302 | struct img_spfi *spfi = data; | |
303 | unsigned long flags; | |
304 | ||
305 | spfi_flush_tx_fifo(spfi); | |
306 | ||
307 | spin_lock_irqsave(&spfi->lock, flags); | |
308 | ||
309 | spfi->tx_dma_busy = false; | |
310 | if (!spfi->rx_dma_busy) { | |
311 | spfi_stop(spfi); | |
312 | spi_finalize_current_transfer(spfi->master); | |
313 | } | |
314 | ||
315 | spin_unlock_irqrestore(&spfi->lock, flags); | |
316 | } | |
317 | ||
318 | static int img_spfi_start_dma(struct spi_master *master, | |
319 | struct spi_device *spi, | |
320 | struct spi_transfer *xfer) | |
321 | { | |
322 | struct img_spfi *spfi = spi_master_get_devdata(spi->master); | |
323 | struct dma_async_tx_descriptor *rxdesc = NULL, *txdesc = NULL; | |
324 | struct dma_slave_config rxconf, txconf; | |
325 | ||
326 | spfi->rx_dma_busy = false; | |
327 | spfi->tx_dma_busy = false; | |
328 | ||
329 | if (xfer->rx_buf) { | |
330 | rxconf.direction = DMA_DEV_TO_MEM; | |
549858ce | 331 | if (xfer->len % 4 == 0) { |
deba2580 AB |
332 | rxconf.src_addr = spfi->phys + SPFI_RX_32BIT_VALID_DATA; |
333 | rxconf.src_addr_width = 4; | |
334 | rxconf.src_maxburst = 4; | |
549858ce | 335 | } else { |
deba2580 AB |
336 | rxconf.src_addr = spfi->phys + SPFI_RX_8BIT_VALID_DATA; |
337 | rxconf.src_addr_width = 1; | |
76fe5e95 | 338 | rxconf.src_maxburst = 4; |
deba2580 AB |
339 | } |
340 | dmaengine_slave_config(spfi->rx_ch, &rxconf); | |
341 | ||
342 | rxdesc = dmaengine_prep_slave_sg(spfi->rx_ch, xfer->rx_sg.sgl, | |
343 | xfer->rx_sg.nents, | |
344 | DMA_DEV_TO_MEM, | |
345 | DMA_PREP_INTERRUPT); | |
346 | if (!rxdesc) | |
347 | goto stop_dma; | |
348 | ||
349 | rxdesc->callback = img_spfi_dma_rx_cb; | |
350 | rxdesc->callback_param = spfi; | |
351 | } | |
352 | ||
353 | if (xfer->tx_buf) { | |
354 | txconf.direction = DMA_MEM_TO_DEV; | |
549858ce | 355 | if (xfer->len % 4 == 0) { |
deba2580 AB |
356 | txconf.dst_addr = spfi->phys + SPFI_TX_32BIT_VALID_DATA; |
357 | txconf.dst_addr_width = 4; | |
358 | txconf.dst_maxburst = 4; | |
549858ce | 359 | } else { |
deba2580 AB |
360 | txconf.dst_addr = spfi->phys + SPFI_TX_8BIT_VALID_DATA; |
361 | txconf.dst_addr_width = 1; | |
76fe5e95 | 362 | txconf.dst_maxburst = 4; |
deba2580 AB |
363 | } |
364 | dmaengine_slave_config(spfi->tx_ch, &txconf); | |
365 | ||
366 | txdesc = dmaengine_prep_slave_sg(spfi->tx_ch, xfer->tx_sg.sgl, | |
367 | xfer->tx_sg.nents, | |
368 | DMA_MEM_TO_DEV, | |
369 | DMA_PREP_INTERRUPT); | |
370 | if (!txdesc) | |
371 | goto stop_dma; | |
372 | ||
373 | txdesc->callback = img_spfi_dma_tx_cb; | |
374 | txdesc->callback_param = spfi; | |
375 | } | |
376 | ||
377 | if (xfer->rx_buf) { | |
378 | spfi->rx_dma_busy = true; | |
379 | dmaengine_submit(rxdesc); | |
380 | dma_async_issue_pending(spfi->rx_ch); | |
381 | } | |
382 | ||
c0e7dc21 AB |
383 | spfi_start(spfi); |
384 | ||
deba2580 AB |
385 | if (xfer->tx_buf) { |
386 | spfi->tx_dma_busy = true; | |
387 | dmaengine_submit(txdesc); | |
388 | dma_async_issue_pending(spfi->tx_ch); | |
389 | } | |
390 | ||
deba2580 AB |
391 | return 1; |
392 | ||
393 | stop_dma: | |
394 | dmaengine_terminate_all(spfi->rx_ch); | |
395 | dmaengine_terminate_all(spfi->tx_ch); | |
396 | return -EIO; | |
397 | } | |
398 | ||
399 | static void img_spfi_config(struct spi_master *master, struct spi_device *spi, | |
400 | struct spi_transfer *xfer) | |
401 | { | |
402 | struct img_spfi *spfi = spi_master_get_devdata(spi->master); | |
403 | u32 val, div; | |
404 | ||
405 | /* | |
406 | * output = spfi_clk * (BITCLK / 512), where BITCLK must be a | |
407 | * power of 2 up to 256 (where 255 == 256 since BITCLK is 8 bits) | |
408 | */ | |
409 | div = DIV_ROUND_UP(master->max_speed_hz, xfer->speed_hz); | |
410 | div = clamp(512 / (1 << get_count_order(div)), 1, 255); | |
411 | ||
412 | val = spfi_readl(spfi, SPFI_DEVICE_PARAMETER(spi->chip_select)); | |
413 | val &= ~(SPFI_DEVICE_PARAMETER_BITCLK_MASK << | |
414 | SPFI_DEVICE_PARAMETER_BITCLK_SHIFT); | |
415 | val |= div << SPFI_DEVICE_PARAMETER_BITCLK_SHIFT; | |
416 | spfi_writel(spfi, val, SPFI_DEVICE_PARAMETER(spi->chip_select)); | |
417 | ||
418 | val = spfi_readl(spfi, SPFI_CONTROL); | |
419 | val &= ~(SPFI_CONTROL_SEND_DMA | SPFI_CONTROL_GET_DMA); | |
420 | if (xfer->tx_buf) | |
421 | val |= SPFI_CONTROL_SEND_DMA; | |
422 | if (xfer->rx_buf) | |
423 | val |= SPFI_CONTROL_GET_DMA; | |
424 | val &= ~(SPFI_CONTROL_TMODE_MASK << SPFI_CONTROL_TMODE_SHIFT); | |
425 | if (xfer->tx_nbits == SPI_NBITS_DUAL && | |
426 | xfer->rx_nbits == SPI_NBITS_DUAL) | |
427 | val |= SPFI_CONTROL_TMODE_DUAL << SPFI_CONTROL_TMODE_SHIFT; | |
428 | else if (xfer->tx_nbits == SPI_NBITS_QUAD && | |
429 | xfer->rx_nbits == SPI_NBITS_QUAD) | |
430 | val |= SPFI_CONTROL_TMODE_QUAD << SPFI_CONTROL_TMODE_SHIFT; | |
431 | val &= ~SPFI_CONTROL_CONTINUE; | |
432 | if (!xfer->cs_change && !list_is_last(&xfer->transfer_list, | |
433 | &master->cur_msg->transfers)) | |
434 | val |= SPFI_CONTROL_CONTINUE; | |
435 | spfi_writel(spfi, val, SPFI_CONTROL); | |
436 | ||
437 | val = spfi_readl(spfi, SPFI_PORT_STATE); | |
438 | if (spi->mode & SPI_CPHA) | |
439 | val |= SPFI_PORT_STATE_CK_PHASE(spi->chip_select); | |
440 | else | |
441 | val &= ~SPFI_PORT_STATE_CK_PHASE(spi->chip_select); | |
442 | if (spi->mode & SPI_CPOL) | |
443 | val |= SPFI_PORT_STATE_CK_POL(spi->chip_select); | |
444 | else | |
445 | val &= ~SPFI_PORT_STATE_CK_POL(spi->chip_select); | |
446 | spfi_writel(spfi, val, SPFI_PORT_STATE); | |
447 | ||
448 | spfi_writel(spfi, xfer->len << SPFI_TRANSACTION_TSIZE_SHIFT, | |
449 | SPFI_TRANSACTION); | |
450 | } | |
451 | ||
452 | static int img_spfi_transfer_one(struct spi_master *master, | |
453 | struct spi_device *spi, | |
454 | struct spi_transfer *xfer) | |
455 | { | |
456 | struct img_spfi *spfi = spi_master_get_devdata(spi->master); | |
457 | bool dma_reset = false; | |
458 | unsigned long flags; | |
459 | int ret; | |
460 | ||
461 | /* | |
462 | * Stop all DMA and reset the controller if the previous transaction | |
463 | * timed-out and never completed it's DMA. | |
464 | */ | |
465 | spin_lock_irqsave(&spfi->lock, flags); | |
466 | if (spfi->tx_dma_busy || spfi->rx_dma_busy) { | |
467 | dev_err(spfi->dev, "SPI DMA still busy\n"); | |
468 | dma_reset = true; | |
469 | } | |
470 | spin_unlock_irqrestore(&spfi->lock, flags); | |
471 | ||
472 | if (dma_reset) { | |
473 | dmaengine_terminate_all(spfi->tx_ch); | |
474 | dmaengine_terminate_all(spfi->rx_ch); | |
475 | spfi_reset(spfi); | |
476 | } | |
477 | ||
478 | img_spfi_config(master, spi, xfer); | |
479 | if (master->can_dma && master->can_dma(master, spi, xfer)) | |
480 | ret = img_spfi_start_dma(master, spi, xfer); | |
481 | else | |
482 | ret = img_spfi_start_pio(master, spi, xfer); | |
483 | ||
484 | return ret; | |
485 | } | |
486 | ||
487 | static void img_spfi_set_cs(struct spi_device *spi, bool enable) | |
488 | { | |
489 | struct img_spfi *spfi = spi_master_get_devdata(spi->master); | |
490 | u32 val; | |
491 | ||
492 | val = spfi_readl(spfi, SPFI_PORT_STATE); | |
493 | val &= ~(SPFI_PORT_STATE_DEV_SEL_MASK << SPFI_PORT_STATE_DEV_SEL_SHIFT); | |
494 | val |= spi->chip_select << SPFI_PORT_STATE_DEV_SEL_SHIFT; | |
495 | spfi_writel(spfi, val, SPFI_PORT_STATE); | |
496 | } | |
497 | ||
498 | static bool img_spfi_can_dma(struct spi_master *master, struct spi_device *spi, | |
499 | struct spi_transfer *xfer) | |
500 | { | |
549858ce | 501 | if (xfer->len > SPFI_32BIT_FIFO_SIZE) |
deba2580 AB |
502 | return true; |
503 | return false; | |
504 | } | |
505 | ||
506 | static irqreturn_t img_spfi_irq(int irq, void *dev_id) | |
507 | { | |
508 | struct img_spfi *spfi = (struct img_spfi *)dev_id; | |
509 | u32 status; | |
510 | ||
511 | status = spfi_readl(spfi, SPFI_INTERRUPT_STATUS); | |
512 | if (status & SPFI_INTERRUPT_IACCESS) { | |
513 | spfi_writel(spfi, SPFI_INTERRUPT_IACCESS, SPFI_INTERRUPT_CLEAR); | |
514 | dev_err(spfi->dev, "Illegal access interrupt"); | |
515 | return IRQ_HANDLED; | |
516 | } | |
517 | ||
518 | return IRQ_NONE; | |
519 | } | |
520 | ||
521 | static int img_spfi_probe(struct platform_device *pdev) | |
522 | { | |
523 | struct spi_master *master; | |
524 | struct img_spfi *spfi; | |
525 | struct resource *res; | |
526 | int ret; | |
527 | ||
528 | master = spi_alloc_master(&pdev->dev, sizeof(*spfi)); | |
529 | if (!master) | |
530 | return -ENOMEM; | |
531 | platform_set_drvdata(pdev, master); | |
532 | ||
533 | spfi = spi_master_get_devdata(master); | |
534 | spfi->dev = &pdev->dev; | |
535 | spfi->master = master; | |
536 | spin_lock_init(&spfi->lock); | |
537 | ||
538 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
539 | spfi->regs = devm_ioremap_resource(spfi->dev, res); | |
540 | if (IS_ERR(spfi->regs)) { | |
541 | ret = PTR_ERR(spfi->regs); | |
542 | goto put_spi; | |
543 | } | |
544 | spfi->phys = res->start; | |
545 | ||
546 | spfi->irq = platform_get_irq(pdev, 0); | |
547 | if (spfi->irq < 0) { | |
548 | ret = spfi->irq; | |
549 | goto put_spi; | |
550 | } | |
551 | ret = devm_request_irq(spfi->dev, spfi->irq, img_spfi_irq, | |
552 | IRQ_TYPE_LEVEL_HIGH, dev_name(spfi->dev), spfi); | |
553 | if (ret) | |
554 | goto put_spi; | |
555 | ||
556 | spfi->sys_clk = devm_clk_get(spfi->dev, "sys"); | |
557 | if (IS_ERR(spfi->sys_clk)) { | |
558 | ret = PTR_ERR(spfi->sys_clk); | |
559 | goto put_spi; | |
560 | } | |
561 | spfi->spfi_clk = devm_clk_get(spfi->dev, "spfi"); | |
562 | if (IS_ERR(spfi->spfi_clk)) { | |
563 | ret = PTR_ERR(spfi->spfi_clk); | |
564 | goto put_spi; | |
565 | } | |
566 | ||
567 | ret = clk_prepare_enable(spfi->sys_clk); | |
568 | if (ret) | |
569 | goto put_spi; | |
570 | ret = clk_prepare_enable(spfi->spfi_clk); | |
571 | if (ret) | |
572 | goto disable_pclk; | |
573 | ||
574 | spfi_reset(spfi); | |
575 | /* | |
576 | * Only enable the error (IACCESS) interrupt. In PIO mode we'll | |
577 | * poll the status of the FIFOs. | |
578 | */ | |
579 | spfi_writel(spfi, SPFI_INTERRUPT_IACCESS, SPFI_INTERRUPT_ENABLE); | |
580 | ||
581 | master->auto_runtime_pm = true; | |
582 | master->bus_num = pdev->id; | |
583 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_TX_DUAL | SPI_RX_DUAL; | |
584 | if (of_property_read_bool(spfi->dev->of_node, "img,supports-quad-mode")) | |
585 | master->mode_bits |= SPI_TX_QUAD | SPI_RX_QUAD; | |
586 | master->num_chipselect = 5; | |
587 | master->dev.of_node = pdev->dev.of_node; | |
588 | master->bits_per_word_mask = SPI_BPW_MASK(32) | SPI_BPW_MASK(8); | |
589 | master->max_speed_hz = clk_get_rate(spfi->spfi_clk); | |
590 | master->min_speed_hz = master->max_speed_hz / 512; | |
591 | ||
592 | master->set_cs = img_spfi_set_cs; | |
593 | master->transfer_one = img_spfi_transfer_one; | |
594 | ||
595 | spfi->tx_ch = dma_request_slave_channel(spfi->dev, "tx"); | |
596 | spfi->rx_ch = dma_request_slave_channel(spfi->dev, "rx"); | |
597 | if (!spfi->tx_ch || !spfi->rx_ch) { | |
598 | if (spfi->tx_ch) | |
599 | dma_release_channel(spfi->tx_ch); | |
600 | if (spfi->rx_ch) | |
601 | dma_release_channel(spfi->rx_ch); | |
602 | dev_warn(spfi->dev, "Failed to get DMA channels, falling back to PIO mode\n"); | |
603 | } else { | |
604 | master->dma_tx = spfi->tx_ch; | |
605 | master->dma_rx = spfi->rx_ch; | |
606 | master->can_dma = img_spfi_can_dma; | |
607 | } | |
608 | ||
609 | pm_runtime_set_active(spfi->dev); | |
610 | pm_runtime_enable(spfi->dev); | |
611 | ||
612 | ret = devm_spi_register_master(spfi->dev, master); | |
613 | if (ret) | |
614 | goto disable_pm; | |
615 | ||
616 | return 0; | |
617 | ||
618 | disable_pm: | |
619 | pm_runtime_disable(spfi->dev); | |
620 | if (spfi->rx_ch) | |
621 | dma_release_channel(spfi->rx_ch); | |
622 | if (spfi->tx_ch) | |
623 | dma_release_channel(spfi->tx_ch); | |
624 | clk_disable_unprepare(spfi->spfi_clk); | |
625 | disable_pclk: | |
626 | clk_disable_unprepare(spfi->sys_clk); | |
627 | put_spi: | |
628 | spi_master_put(master); | |
629 | ||
630 | return ret; | |
631 | } | |
632 | ||
633 | static int img_spfi_remove(struct platform_device *pdev) | |
634 | { | |
635 | struct spi_master *master = platform_get_drvdata(pdev); | |
636 | struct img_spfi *spfi = spi_master_get_devdata(master); | |
637 | ||
638 | if (spfi->tx_ch) | |
639 | dma_release_channel(spfi->tx_ch); | |
640 | if (spfi->rx_ch) | |
641 | dma_release_channel(spfi->rx_ch); | |
642 | ||
643 | pm_runtime_disable(spfi->dev); | |
644 | if (!pm_runtime_status_suspended(spfi->dev)) { | |
645 | clk_disable_unprepare(spfi->spfi_clk); | |
646 | clk_disable_unprepare(spfi->sys_clk); | |
647 | } | |
648 | ||
649 | spi_master_put(master); | |
650 | ||
651 | return 0; | |
652 | } | |
653 | ||
47164fdb | 654 | #ifdef CONFIG_PM |
deba2580 AB |
655 | static int img_spfi_runtime_suspend(struct device *dev) |
656 | { | |
657 | struct spi_master *master = dev_get_drvdata(dev); | |
658 | struct img_spfi *spfi = spi_master_get_devdata(master); | |
659 | ||
660 | clk_disable_unprepare(spfi->spfi_clk); | |
661 | clk_disable_unprepare(spfi->sys_clk); | |
662 | ||
663 | return 0; | |
664 | } | |
665 | ||
666 | static int img_spfi_runtime_resume(struct device *dev) | |
667 | { | |
668 | struct spi_master *master = dev_get_drvdata(dev); | |
669 | struct img_spfi *spfi = spi_master_get_devdata(master); | |
670 | int ret; | |
671 | ||
672 | ret = clk_prepare_enable(spfi->sys_clk); | |
673 | if (ret) | |
674 | return ret; | |
675 | ret = clk_prepare_enable(spfi->spfi_clk); | |
676 | if (ret) { | |
677 | clk_disable_unprepare(spfi->sys_clk); | |
678 | return ret; | |
679 | } | |
680 | ||
681 | return 0; | |
682 | } | |
47164fdb | 683 | #endif /* CONFIG_PM */ |
deba2580 AB |
684 | |
685 | #ifdef CONFIG_PM_SLEEP | |
686 | static int img_spfi_suspend(struct device *dev) | |
687 | { | |
688 | struct spi_master *master = dev_get_drvdata(dev); | |
689 | ||
690 | return spi_master_suspend(master); | |
691 | } | |
692 | ||
693 | static int img_spfi_resume(struct device *dev) | |
694 | { | |
695 | struct spi_master *master = dev_get_drvdata(dev); | |
696 | struct img_spfi *spfi = spi_master_get_devdata(master); | |
697 | int ret; | |
698 | ||
699 | ret = pm_runtime_get_sync(dev); | |
700 | if (ret) | |
701 | return ret; | |
702 | spfi_reset(spfi); | |
703 | pm_runtime_put(dev); | |
704 | ||
705 | return spi_master_resume(master); | |
706 | } | |
707 | #endif /* CONFIG_PM_SLEEP */ | |
708 | ||
709 | static const struct dev_pm_ops img_spfi_pm_ops = { | |
710 | SET_RUNTIME_PM_OPS(img_spfi_runtime_suspend, img_spfi_runtime_resume, | |
711 | NULL) | |
712 | SET_SYSTEM_SLEEP_PM_OPS(img_spfi_suspend, img_spfi_resume) | |
713 | }; | |
714 | ||
715 | static const struct of_device_id img_spfi_of_match[] = { | |
716 | { .compatible = "img,spfi", }, | |
717 | { }, | |
718 | }; | |
719 | MODULE_DEVICE_TABLE(of, img_spfi_of_match); | |
720 | ||
721 | static struct platform_driver img_spfi_driver = { | |
722 | .driver = { | |
723 | .name = "img-spfi", | |
724 | .pm = &img_spfi_pm_ops, | |
725 | .of_match_table = of_match_ptr(img_spfi_of_match), | |
726 | }, | |
727 | .probe = img_spfi_probe, | |
728 | .remove = img_spfi_remove, | |
729 | }; | |
730 | module_platform_driver(img_spfi_driver); | |
731 | ||
732 | MODULE_DESCRIPTION("IMG SPFI controller driver"); | |
733 | MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>"); | |
734 | MODULE_LICENSE("GPL v2"); |