Commit | Line | Data |
---|---|---|
b43d65f7 LW |
1 | /* |
2 | * drivers/spi/amba-pl022.c | |
3 | * | |
4 | * A driver for the ARM PL022 PrimeCell SSP/SPI bus master. | |
5 | * | |
6 | * Copyright (C) 2008-2009 ST-Ericsson AB | |
7 | * Copyright (C) 2006 STMicroelectronics Pvt. Ltd. | |
8 | * | |
9 | * Author: Linus Walleij <linus.walleij@stericsson.com> | |
10 | * | |
11 | * Initial version inspired by: | |
12 | * linux-2.6.17-rc3-mm1/drivers/spi/pxa2xx_spi.c | |
13 | * Initial adoption to PL022 by: | |
14 | * Sachin Verma <sachin.verma@st.com> | |
15 | * | |
16 | * This program is free software; you can redistribute it and/or modify | |
17 | * it under the terms of the GNU General Public License as published by | |
18 | * the Free Software Foundation; either version 2 of the License, or | |
19 | * (at your option) any later version. | |
20 | * | |
21 | * This program is distributed in the hope that it will be useful, | |
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | * GNU General Public License for more details. | |
25 | */ | |
26 | ||
b43d65f7 LW |
27 | #include <linux/init.h> |
28 | #include <linux/module.h> | |
29 | #include <linux/device.h> | |
30 | #include <linux/ioport.h> | |
31 | #include <linux/errno.h> | |
32 | #include <linux/interrupt.h> | |
33 | #include <linux/spi/spi.h> | |
34 | #include <linux/workqueue.h> | |
b43d65f7 LW |
35 | #include <linux/delay.h> |
36 | #include <linux/clk.h> | |
37 | #include <linux/err.h> | |
38 | #include <linux/amba/bus.h> | |
39 | #include <linux/amba/pl022.h> | |
40 | #include <linux/io.h> | |
5a0e3ad6 | 41 | #include <linux/slab.h> |
b1b6b9aa LW |
42 | #include <linux/dmaengine.h> |
43 | #include <linux/dma-mapping.h> | |
44 | #include <linux/scatterlist.h> | |
b43d65f7 LW |
45 | |
46 | /* | |
47 | * This macro is used to define some register default values. | |
48 | * reg is masked with mask, the OR:ed with an (again masked) | |
49 | * val shifted sb steps to the left. | |
50 | */ | |
51 | #define SSP_WRITE_BITS(reg, val, mask, sb) \ | |
52 | ((reg) = (((reg) & ~(mask)) | (((val)<<(sb)) & (mask)))) | |
53 | ||
54 | /* | |
55 | * This macro is also used to define some default values. | |
56 | * It will just shift val by sb steps to the left and mask | |
57 | * the result with mask. | |
58 | */ | |
59 | #define GEN_MASK_BITS(val, mask, sb) \ | |
60 | (((val)<<(sb)) & (mask)) | |
61 | ||
62 | #define DRIVE_TX 0 | |
63 | #define DO_NOT_DRIVE_TX 1 | |
64 | ||
65 | #define DO_NOT_QUEUE_DMA 0 | |
66 | #define QUEUE_DMA 1 | |
67 | ||
68 | #define RX_TRANSFER 1 | |
69 | #define TX_TRANSFER 2 | |
70 | ||
71 | /* | |
72 | * Macros to access SSP Registers with their offsets | |
73 | */ | |
74 | #define SSP_CR0(r) (r + 0x000) | |
75 | #define SSP_CR1(r) (r + 0x004) | |
76 | #define SSP_DR(r) (r + 0x008) | |
77 | #define SSP_SR(r) (r + 0x00C) | |
78 | #define SSP_CPSR(r) (r + 0x010) | |
79 | #define SSP_IMSC(r) (r + 0x014) | |
80 | #define SSP_RIS(r) (r + 0x018) | |
81 | #define SSP_MIS(r) (r + 0x01C) | |
82 | #define SSP_ICR(r) (r + 0x020) | |
83 | #define SSP_DMACR(r) (r + 0x024) | |
84 | #define SSP_ITCR(r) (r + 0x080) | |
85 | #define SSP_ITIP(r) (r + 0x084) | |
86 | #define SSP_ITOP(r) (r + 0x088) | |
87 | #define SSP_TDR(r) (r + 0x08C) | |
88 | ||
89 | #define SSP_PID0(r) (r + 0xFE0) | |
90 | #define SSP_PID1(r) (r + 0xFE4) | |
91 | #define SSP_PID2(r) (r + 0xFE8) | |
92 | #define SSP_PID3(r) (r + 0xFEC) | |
93 | ||
94 | #define SSP_CID0(r) (r + 0xFF0) | |
95 | #define SSP_CID1(r) (r + 0xFF4) | |
96 | #define SSP_CID2(r) (r + 0xFF8) | |
97 | #define SSP_CID3(r) (r + 0xFFC) | |
98 | ||
99 | /* | |
100 | * SSP Control Register 0 - SSP_CR0 | |
101 | */ | |
556f4aeb LW |
102 | #define SSP_CR0_MASK_DSS (0x0FUL << 0) |
103 | #define SSP_CR0_MASK_FRF (0x3UL << 4) | |
b43d65f7 LW |
104 | #define SSP_CR0_MASK_SPO (0x1UL << 6) |
105 | #define SSP_CR0_MASK_SPH (0x1UL << 7) | |
106 | #define SSP_CR0_MASK_SCR (0xFFUL << 8) | |
556f4aeb LW |
107 | |
108 | /* | |
109 | * The ST version of this block moves som bits | |
110 | * in SSP_CR0 and extends it to 32 bits | |
111 | */ | |
112 | #define SSP_CR0_MASK_DSS_ST (0x1FUL << 0) | |
113 | #define SSP_CR0_MASK_HALFDUP_ST (0x1UL << 5) | |
114 | #define SSP_CR0_MASK_CSS_ST (0x1FUL << 16) | |
115 | #define SSP_CR0_MASK_FRF_ST (0x3UL << 21) | |
116 | ||
b43d65f7 LW |
117 | |
118 | /* | |
119 | * SSP Control Register 0 - SSP_CR1 | |
120 | */ | |
121 | #define SSP_CR1_MASK_LBM (0x1UL << 0) | |
122 | #define SSP_CR1_MASK_SSE (0x1UL << 1) | |
123 | #define SSP_CR1_MASK_MS (0x1UL << 2) | |
124 | #define SSP_CR1_MASK_SOD (0x1UL << 3) | |
b43d65f7 LW |
125 | |
126 | /* | |
556f4aeb LW |
127 | * The ST version of this block adds some bits |
128 | * in SSP_CR1 | |
b43d65f7 | 129 | */ |
556f4aeb LW |
130 | #define SSP_CR1_MASK_RENDN_ST (0x1UL << 4) |
131 | #define SSP_CR1_MASK_TENDN_ST (0x1UL << 5) | |
132 | #define SSP_CR1_MASK_MWAIT_ST (0x1UL << 6) | |
133 | #define SSP_CR1_MASK_RXIFLSEL_ST (0x7UL << 7) | |
134 | #define SSP_CR1_MASK_TXIFLSEL_ST (0x7UL << 10) | |
781c7b12 LW |
135 | /* This one is only in the PL023 variant */ |
136 | #define SSP_CR1_MASK_FBCLKDEL_ST (0x7UL << 13) | |
b43d65f7 LW |
137 | |
138 | /* | |
139 | * SSP Status Register - SSP_SR | |
140 | */ | |
141 | #define SSP_SR_MASK_TFE (0x1UL << 0) /* Transmit FIFO empty */ | |
142 | #define SSP_SR_MASK_TNF (0x1UL << 1) /* Transmit FIFO not full */ | |
143 | #define SSP_SR_MASK_RNE (0x1UL << 2) /* Receive FIFO not empty */ | |
556f4aeb | 144 | #define SSP_SR_MASK_RFF (0x1UL << 3) /* Receive FIFO full */ |
b43d65f7 LW |
145 | #define SSP_SR_MASK_BSY (0x1UL << 4) /* Busy Flag */ |
146 | ||
147 | /* | |
148 | * SSP Clock Prescale Register - SSP_CPSR | |
149 | */ | |
150 | #define SSP_CPSR_MASK_CPSDVSR (0xFFUL << 0) | |
151 | ||
152 | /* | |
153 | * SSP Interrupt Mask Set/Clear Register - SSP_IMSC | |
154 | */ | |
155 | #define SSP_IMSC_MASK_RORIM (0x1UL << 0) /* Receive Overrun Interrupt mask */ | |
156 | #define SSP_IMSC_MASK_RTIM (0x1UL << 1) /* Receive timeout Interrupt mask */ | |
157 | #define SSP_IMSC_MASK_RXIM (0x1UL << 2) /* Receive FIFO Interrupt mask */ | |
158 | #define SSP_IMSC_MASK_TXIM (0x1UL << 3) /* Transmit FIFO Interrupt mask */ | |
159 | ||
160 | /* | |
161 | * SSP Raw Interrupt Status Register - SSP_RIS | |
162 | */ | |
163 | /* Receive Overrun Raw Interrupt status */ | |
164 | #define SSP_RIS_MASK_RORRIS (0x1UL << 0) | |
165 | /* Receive Timeout Raw Interrupt status */ | |
166 | #define SSP_RIS_MASK_RTRIS (0x1UL << 1) | |
167 | /* Receive FIFO Raw Interrupt status */ | |
168 | #define SSP_RIS_MASK_RXRIS (0x1UL << 2) | |
169 | /* Transmit FIFO Raw Interrupt status */ | |
170 | #define SSP_RIS_MASK_TXRIS (0x1UL << 3) | |
171 | ||
172 | /* | |
173 | * SSP Masked Interrupt Status Register - SSP_MIS | |
174 | */ | |
175 | /* Receive Overrun Masked Interrupt status */ | |
176 | #define SSP_MIS_MASK_RORMIS (0x1UL << 0) | |
177 | /* Receive Timeout Masked Interrupt status */ | |
178 | #define SSP_MIS_MASK_RTMIS (0x1UL << 1) | |
179 | /* Receive FIFO Masked Interrupt status */ | |
180 | #define SSP_MIS_MASK_RXMIS (0x1UL << 2) | |
181 | /* Transmit FIFO Masked Interrupt status */ | |
182 | #define SSP_MIS_MASK_TXMIS (0x1UL << 3) | |
183 | ||
184 | /* | |
185 | * SSP Interrupt Clear Register - SSP_ICR | |
186 | */ | |
187 | /* Receive Overrun Raw Clear Interrupt bit */ | |
188 | #define SSP_ICR_MASK_RORIC (0x1UL << 0) | |
189 | /* Receive Timeout Clear Interrupt bit */ | |
190 | #define SSP_ICR_MASK_RTIC (0x1UL << 1) | |
191 | ||
192 | /* | |
193 | * SSP DMA Control Register - SSP_DMACR | |
194 | */ | |
195 | /* Receive DMA Enable bit */ | |
196 | #define SSP_DMACR_MASK_RXDMAE (0x1UL << 0) | |
197 | /* Transmit DMA Enable bit */ | |
198 | #define SSP_DMACR_MASK_TXDMAE (0x1UL << 1) | |
199 | ||
200 | /* | |
201 | * SSP Integration Test control Register - SSP_ITCR | |
202 | */ | |
203 | #define SSP_ITCR_MASK_ITEN (0x1UL << 0) | |
204 | #define SSP_ITCR_MASK_TESTFIFO (0x1UL << 1) | |
205 | ||
206 | /* | |
207 | * SSP Integration Test Input Register - SSP_ITIP | |
208 | */ | |
209 | #define ITIP_MASK_SSPRXD (0x1UL << 0) | |
210 | #define ITIP_MASK_SSPFSSIN (0x1UL << 1) | |
211 | #define ITIP_MASK_SSPCLKIN (0x1UL << 2) | |
212 | #define ITIP_MASK_RXDMAC (0x1UL << 3) | |
213 | #define ITIP_MASK_TXDMAC (0x1UL << 4) | |
214 | #define ITIP_MASK_SSPTXDIN (0x1UL << 5) | |
215 | ||
216 | /* | |
217 | * SSP Integration Test output Register - SSP_ITOP | |
218 | */ | |
219 | #define ITOP_MASK_SSPTXD (0x1UL << 0) | |
220 | #define ITOP_MASK_SSPFSSOUT (0x1UL << 1) | |
221 | #define ITOP_MASK_SSPCLKOUT (0x1UL << 2) | |
222 | #define ITOP_MASK_SSPOEn (0x1UL << 3) | |
223 | #define ITOP_MASK_SSPCTLOEn (0x1UL << 4) | |
224 | #define ITOP_MASK_RORINTR (0x1UL << 5) | |
225 | #define ITOP_MASK_RTINTR (0x1UL << 6) | |
226 | #define ITOP_MASK_RXINTR (0x1UL << 7) | |
227 | #define ITOP_MASK_TXINTR (0x1UL << 8) | |
228 | #define ITOP_MASK_INTR (0x1UL << 9) | |
229 | #define ITOP_MASK_RXDMABREQ (0x1UL << 10) | |
230 | #define ITOP_MASK_RXDMASREQ (0x1UL << 11) | |
231 | #define ITOP_MASK_TXDMABREQ (0x1UL << 12) | |
232 | #define ITOP_MASK_TXDMASREQ (0x1UL << 13) | |
233 | ||
234 | /* | |
235 | * SSP Test Data Register - SSP_TDR | |
236 | */ | |
556f4aeb | 237 | #define TDR_MASK_TESTDATA (0xFFFFFFFF) |
b43d65f7 LW |
238 | |
239 | /* | |
240 | * Message State | |
241 | * we use the spi_message.state (void *) pointer to | |
242 | * hold a single state value, that's why all this | |
243 | * (void *) casting is done here. | |
244 | */ | |
556f4aeb LW |
245 | #define STATE_START ((void *) 0) |
246 | #define STATE_RUNNING ((void *) 1) | |
247 | #define STATE_DONE ((void *) 2) | |
248 | #define STATE_ERROR ((void *) -1) | |
b43d65f7 | 249 | |
b43d65f7 LW |
250 | /* |
251 | * SSP State - Whether Enabled or Disabled | |
252 | */ | |
556f4aeb LW |
253 | #define SSP_DISABLED (0) |
254 | #define SSP_ENABLED (1) | |
b43d65f7 LW |
255 | |
256 | /* | |
257 | * SSP DMA State - Whether DMA Enabled or Disabled | |
258 | */ | |
556f4aeb LW |
259 | #define SSP_DMA_DISABLED (0) |
260 | #define SSP_DMA_ENABLED (1) | |
b43d65f7 LW |
261 | |
262 | /* | |
263 | * SSP Clock Defaults | |
264 | */ | |
556f4aeb LW |
265 | #define SSP_DEFAULT_CLKRATE 0x2 |
266 | #define SSP_DEFAULT_PRESCALE 0x40 | |
b43d65f7 LW |
267 | |
268 | /* | |
269 | * SSP Clock Parameter ranges | |
270 | */ | |
271 | #define CPSDVR_MIN 0x02 | |
272 | #define CPSDVR_MAX 0xFE | |
273 | #define SCR_MIN 0x00 | |
274 | #define SCR_MAX 0xFF | |
275 | ||
276 | /* | |
277 | * SSP Interrupt related Macros | |
278 | */ | |
279 | #define DEFAULT_SSP_REG_IMSC 0x0UL | |
280 | #define DISABLE_ALL_INTERRUPTS DEFAULT_SSP_REG_IMSC | |
281 | #define ENABLE_ALL_INTERRUPTS (~DEFAULT_SSP_REG_IMSC) | |
282 | ||
283 | #define CLEAR_ALL_INTERRUPTS 0x3 | |
284 | ||
a18c266f MT |
285 | #define SPI_POLLING_TIMEOUT 1000 |
286 | ||
b43d65f7 LW |
287 | |
288 | /* | |
289 | * The type of reading going on on this chip | |
290 | */ | |
291 | enum ssp_reading { | |
292 | READING_NULL, | |
293 | READING_U8, | |
294 | READING_U16, | |
295 | READING_U32 | |
296 | }; | |
297 | ||
298 | /** | |
299 | * The type of writing going on on this chip | |
300 | */ | |
301 | enum ssp_writing { | |
302 | WRITING_NULL, | |
303 | WRITING_U8, | |
304 | WRITING_U16, | |
305 | WRITING_U32 | |
306 | }; | |
307 | ||
308 | /** | |
309 | * struct vendor_data - vendor-specific config parameters | |
310 | * for PL022 derivates | |
311 | * @fifodepth: depth of FIFOs (both) | |
312 | * @max_bpw: maximum number of bits per word | |
313 | * @unidir: supports unidirection transfers | |
556f4aeb LW |
314 | * @extended_cr: 32 bit wide control register 0 with extra |
315 | * features and extra features in CR1 as found in the ST variants | |
781c7b12 | 316 | * @pl023: supports a subset of the ST extensions called "PL023" |
b43d65f7 LW |
317 | */ |
318 | struct vendor_data { | |
319 | int fifodepth; | |
320 | int max_bpw; | |
321 | bool unidir; | |
556f4aeb | 322 | bool extended_cr; |
781c7b12 | 323 | bool pl023; |
06fb01fd | 324 | bool loopback; |
b43d65f7 LW |
325 | }; |
326 | ||
327 | /** | |
328 | * struct pl022 - This is the private SSP driver data structure | |
329 | * @adev: AMBA device model hookup | |
12e8b325 LW |
330 | * @vendor: vendor data for the IP block |
331 | * @phybase: the physical memory where the SSP device resides | |
332 | * @virtbase: the virtual memory where the SSP is mapped | |
333 | * @clk: outgoing clock "SPICLK" for the SPI bus | |
b43d65f7 LW |
334 | * @master: SPI framework hookup |
335 | * @master_info: controller-specific data from machine setup | |
b43d65f7 | 336 | * @workqueue: a workqueue on which any spi_message request is queued |
12e8b325 LW |
337 | * @pump_messages: work struct for scheduling work to the workqueue |
338 | * @queue_lock: spinlock to syncronise access to message queue | |
339 | * @queue: message queue | |
b43d65f7 | 340 | * @busy: workqueue is busy |
5e8b821d | 341 | * @running: workqueue is running |
b43d65f7 LW |
342 | * @pump_transfers: Tasklet used in Interrupt Transfer mode |
343 | * @cur_msg: Pointer to current spi_message being processed | |
344 | * @cur_transfer: Pointer to current spi_transfer | |
345 | * @cur_chip: pointer to current clients chip(assigned from controller_state) | |
346 | * @tx: current position in TX buffer to be read | |
347 | * @tx_end: end position in TX buffer to be read | |
348 | * @rx: current position in RX buffer to be written | |
349 | * @rx_end: end position in RX buffer to be written | |
12e8b325 LW |
350 | * @read: the type of read currently going on |
351 | * @write: the type of write currently going on | |
352 | * @exp_fifo_level: expected FIFO level | |
353 | * @dma_rx_channel: optional channel for RX DMA | |
354 | * @dma_tx_channel: optional channel for TX DMA | |
355 | * @sgt_rx: scattertable for the RX transfer | |
356 | * @sgt_tx: scattertable for the TX transfer | |
357 | * @dummypage: a dummy page used for driving data on the bus with DMA | |
b43d65f7 LW |
358 | */ |
359 | struct pl022 { | |
360 | struct amba_device *adev; | |
361 | struct vendor_data *vendor; | |
362 | resource_size_t phybase; | |
363 | void __iomem *virtbase; | |
364 | struct clk *clk; | |
365 | struct spi_master *master; | |
366 | struct pl022_ssp_controller *master_info; | |
367 | /* Driver message queue */ | |
368 | struct workqueue_struct *workqueue; | |
369 | struct work_struct pump_messages; | |
370 | spinlock_t queue_lock; | |
371 | struct list_head queue; | |
dec5a581 | 372 | bool busy; |
5e8b821d | 373 | bool running; |
b43d65f7 LW |
374 | /* Message transfer pump */ |
375 | struct tasklet_struct pump_transfers; | |
376 | struct spi_message *cur_msg; | |
377 | struct spi_transfer *cur_transfer; | |
378 | struct chip_data *cur_chip; | |
379 | void *tx; | |
380 | void *tx_end; | |
381 | void *rx; | |
382 | void *rx_end; | |
383 | enum ssp_reading read; | |
384 | enum ssp_writing write; | |
fc05475f | 385 | u32 exp_fifo_level; |
b1b6b9aa LW |
386 | /* DMA settings */ |
387 | #ifdef CONFIG_DMA_ENGINE | |
388 | struct dma_chan *dma_rx_channel; | |
389 | struct dma_chan *dma_tx_channel; | |
390 | struct sg_table sgt_rx; | |
391 | struct sg_table sgt_tx; | |
392 | char *dummypage; | |
393 | #endif | |
b43d65f7 LW |
394 | }; |
395 | ||
396 | /** | |
397 | * struct chip_data - To maintain runtime state of SSP for each client chip | |
556f4aeb LW |
398 | * @cr0: Value of control register CR0 of SSP - on later ST variants this |
399 | * register is 32 bits wide rather than just 16 | |
b43d65f7 LW |
400 | * @cr1: Value of control register CR1 of SSP |
401 | * @dmacr: Value of DMA control Register of SSP | |
402 | * @cpsr: Value of Clock prescale register | |
403 | * @n_bytes: how many bytes(power of 2) reqd for a given data width of client | |
404 | * @enable_dma: Whether to enable DMA or not | |
b43d65f7 | 405 | * @read: function ptr to be used to read when doing xfer for this chip |
12e8b325 | 406 | * @write: function ptr to be used to write when doing xfer for this chip |
b43d65f7 LW |
407 | * @cs_control: chip select callback provided by chip |
408 | * @xfer_type: polling/interrupt/DMA | |
409 | * | |
410 | * Runtime state of the SSP controller, maintained per chip, | |
411 | * This would be set according to the current message that would be served | |
412 | */ | |
413 | struct chip_data { | |
556f4aeb | 414 | u32 cr0; |
b43d65f7 LW |
415 | u16 cr1; |
416 | u16 dmacr; | |
417 | u16 cpsr; | |
418 | u8 n_bytes; | |
b1b6b9aa | 419 | bool enable_dma; |
b43d65f7 LW |
420 | enum ssp_reading read; |
421 | enum ssp_writing write; | |
422 | void (*cs_control) (u32 command); | |
423 | int xfer_type; | |
424 | }; | |
425 | ||
426 | /** | |
427 | * null_cs_control - Dummy chip select function | |
428 | * @command: select/delect the chip | |
429 | * | |
430 | * If no chip select function is provided by client this is used as dummy | |
431 | * chip select | |
432 | */ | |
433 | static void null_cs_control(u32 command) | |
434 | { | |
435 | pr_debug("pl022: dummy chip select control, CS=0x%x\n", command); | |
436 | } | |
437 | ||
438 | /** | |
439 | * giveback - current spi_message is over, schedule next message and call | |
440 | * callback of this message. Assumes that caller already | |
441 | * set message->status; dma and pio irqs are blocked | |
442 | * @pl022: SSP driver private data structure | |
443 | */ | |
444 | static void giveback(struct pl022 *pl022) | |
445 | { | |
446 | struct spi_transfer *last_transfer; | |
447 | unsigned long flags; | |
448 | struct spi_message *msg; | |
449 | void (*curr_cs_control) (u32 command); | |
450 | ||
451 | /* | |
452 | * This local reference to the chip select function | |
453 | * is needed because we set curr_chip to NULL | |
454 | * as a step toward termininating the message. | |
455 | */ | |
456 | curr_cs_control = pl022->cur_chip->cs_control; | |
457 | spin_lock_irqsave(&pl022->queue_lock, flags); | |
458 | msg = pl022->cur_msg; | |
459 | pl022->cur_msg = NULL; | |
460 | pl022->cur_transfer = NULL; | |
461 | pl022->cur_chip = NULL; | |
462 | queue_work(pl022->workqueue, &pl022->pump_messages); | |
463 | spin_unlock_irqrestore(&pl022->queue_lock, flags); | |
464 | ||
465 | last_transfer = list_entry(msg->transfers.prev, | |
466 | struct spi_transfer, | |
467 | transfer_list); | |
468 | ||
469 | /* Delay if requested before any change in chip select */ | |
470 | if (last_transfer->delay_usecs) | |
471 | /* | |
472 | * FIXME: This runs in interrupt context. | |
473 | * Is this really smart? | |
474 | */ | |
475 | udelay(last_transfer->delay_usecs); | |
476 | ||
477 | /* | |
478 | * Drop chip select UNLESS cs_change is true or we are returning | |
479 | * a message with an error, or next message is for another chip | |
480 | */ | |
481 | if (!last_transfer->cs_change) | |
482 | curr_cs_control(SSP_CHIP_DESELECT); | |
483 | else { | |
484 | struct spi_message *next_msg; | |
485 | ||
486 | /* Holding of cs was hinted, but we need to make sure | |
487 | * the next message is for the same chip. Don't waste | |
488 | * time with the following tests unless this was hinted. | |
489 | * | |
490 | * We cannot postpone this until pump_messages, because | |
491 | * after calling msg->complete (below) the driver that | |
492 | * sent the current message could be unloaded, which | |
493 | * could invalidate the cs_control() callback... | |
494 | */ | |
495 | ||
496 | /* get a pointer to the next message, if any */ | |
497 | spin_lock_irqsave(&pl022->queue_lock, flags); | |
498 | if (list_empty(&pl022->queue)) | |
499 | next_msg = NULL; | |
500 | else | |
501 | next_msg = list_entry(pl022->queue.next, | |
502 | struct spi_message, queue); | |
503 | spin_unlock_irqrestore(&pl022->queue_lock, flags); | |
504 | ||
505 | /* see if the next and current messages point | |
506 | * to the same chip | |
507 | */ | |
508 | if (next_msg && next_msg->spi != msg->spi) | |
509 | next_msg = NULL; | |
510 | if (!next_msg || msg->state == STATE_ERROR) | |
511 | curr_cs_control(SSP_CHIP_DESELECT); | |
512 | } | |
513 | msg->state = NULL; | |
514 | if (msg->complete) | |
515 | msg->complete(msg->context); | |
808f1037 | 516 | /* This message is completed, so let's turn off the clocks & power */ |
b43d65f7 | 517 | clk_disable(pl022->clk); |
545074fb | 518 | amba_pclk_disable(pl022->adev); |
808f1037 | 519 | amba_vcore_disable(pl022->adev); |
b43d65f7 LW |
520 | } |
521 | ||
522 | /** | |
523 | * flush - flush the FIFO to reach a clean state | |
524 | * @pl022: SSP driver private data structure | |
525 | */ | |
526 | static int flush(struct pl022 *pl022) | |
527 | { | |
528 | unsigned long limit = loops_per_jiffy << 1; | |
529 | ||
530 | dev_dbg(&pl022->adev->dev, "flush\n"); | |
531 | do { | |
532 | while (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE) | |
533 | readw(SSP_DR(pl022->virtbase)); | |
534 | } while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_BSY) && limit--); | |
fc05475f LW |
535 | |
536 | pl022->exp_fifo_level = 0; | |
537 | ||
b43d65f7 LW |
538 | return limit; |
539 | } | |
540 | ||
541 | /** | |
542 | * restore_state - Load configuration of current chip | |
543 | * @pl022: SSP driver private data structure | |
544 | */ | |
545 | static void restore_state(struct pl022 *pl022) | |
546 | { | |
547 | struct chip_data *chip = pl022->cur_chip; | |
548 | ||
556f4aeb LW |
549 | if (pl022->vendor->extended_cr) |
550 | writel(chip->cr0, SSP_CR0(pl022->virtbase)); | |
551 | else | |
552 | writew(chip->cr0, SSP_CR0(pl022->virtbase)); | |
b43d65f7 LW |
553 | writew(chip->cr1, SSP_CR1(pl022->virtbase)); |
554 | writew(chip->dmacr, SSP_DMACR(pl022->virtbase)); | |
555 | writew(chip->cpsr, SSP_CPSR(pl022->virtbase)); | |
556 | writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase)); | |
557 | writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase)); | |
558 | } | |
559 | ||
b43d65f7 LW |
560 | /* |
561 | * Default SSP Register Values | |
562 | */ | |
563 | #define DEFAULT_SSP_REG_CR0 ( \ | |
564 | GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS, 0) | \ | |
556f4aeb LW |
565 | GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF, 4) | \ |
566 | GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \ | |
567 | GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \ | |
568 | GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \ | |
569 | ) | |
570 | ||
571 | /* ST versions have slightly different bit layout */ | |
572 | #define DEFAULT_SSP_REG_CR0_ST ( \ | |
573 | GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \ | |
574 | GEN_MASK_BITS(SSP_MICROWIRE_CHANNEL_FULL_DUPLEX, SSP_CR0_MASK_HALFDUP_ST, 5) | \ | |
b43d65f7 | 575 | GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \ |
ee2b805c | 576 | GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \ |
556f4aeb LW |
577 | GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) | \ |
578 | GEN_MASK_BITS(SSP_BITS_8, SSP_CR0_MASK_CSS_ST, 16) | \ | |
579 | GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF_ST, 21) \ | |
b43d65f7 LW |
580 | ) |
581 | ||
781c7b12 LW |
582 | /* The PL023 version is slightly different again */ |
583 | #define DEFAULT_SSP_REG_CR0_ST_PL023 ( \ | |
584 | GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \ | |
585 | GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \ | |
586 | GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \ | |
587 | GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \ | |
588 | ) | |
589 | ||
b43d65f7 LW |
590 | #define DEFAULT_SSP_REG_CR1 ( \ |
591 | GEN_MASK_BITS(LOOPBACK_DISABLED, SSP_CR1_MASK_LBM, 0) | \ | |
592 | GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \ | |
593 | GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \ | |
556f4aeb | 594 | GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) \ |
b43d65f7 LW |
595 | ) |
596 | ||
556f4aeb LW |
597 | /* ST versions extend this register to use all 16 bits */ |
598 | #define DEFAULT_SSP_REG_CR1_ST ( \ | |
599 | DEFAULT_SSP_REG_CR1 | \ | |
600 | GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \ | |
601 | GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \ | |
602 | GEN_MASK_BITS(SSP_MWIRE_WAIT_ZERO, SSP_CR1_MASK_MWAIT_ST, 6) |\ | |
603 | GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \ | |
604 | GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) \ | |
605 | ) | |
606 | ||
781c7b12 LW |
607 | /* |
608 | * The PL023 variant has further differences: no loopback mode, no microwire | |
609 | * support, and a new clock feedback delay setting. | |
610 | */ | |
611 | #define DEFAULT_SSP_REG_CR1_ST_PL023 ( \ | |
612 | GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \ | |
613 | GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \ | |
614 | GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) | \ | |
615 | GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \ | |
616 | GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \ | |
617 | GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \ | |
618 | GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) | \ | |
619 | GEN_MASK_BITS(SSP_FEEDBACK_CLK_DELAY_NONE, SSP_CR1_MASK_FBCLKDEL_ST, 13) \ | |
620 | ) | |
556f4aeb | 621 | |
b43d65f7 | 622 | #define DEFAULT_SSP_REG_CPSR ( \ |
556f4aeb | 623 | GEN_MASK_BITS(SSP_DEFAULT_PRESCALE, SSP_CPSR_MASK_CPSDVSR, 0) \ |
b43d65f7 LW |
624 | ) |
625 | ||
626 | #define DEFAULT_SSP_REG_DMACR (\ | |
627 | GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_RXDMAE, 0) | \ | |
628 | GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_TXDMAE, 1) \ | |
629 | ) | |
630 | ||
781c7b12 LW |
631 | /** |
632 | * load_ssp_default_config - Load default configuration for SSP | |
633 | * @pl022: SSP driver private data structure | |
634 | */ | |
b43d65f7 LW |
635 | static void load_ssp_default_config(struct pl022 *pl022) |
636 | { | |
781c7b12 LW |
637 | if (pl022->vendor->pl023) { |
638 | writel(DEFAULT_SSP_REG_CR0_ST_PL023, SSP_CR0(pl022->virtbase)); | |
639 | writew(DEFAULT_SSP_REG_CR1_ST_PL023, SSP_CR1(pl022->virtbase)); | |
640 | } else if (pl022->vendor->extended_cr) { | |
556f4aeb LW |
641 | writel(DEFAULT_SSP_REG_CR0_ST, SSP_CR0(pl022->virtbase)); |
642 | writew(DEFAULT_SSP_REG_CR1_ST, SSP_CR1(pl022->virtbase)); | |
643 | } else { | |
644 | writew(DEFAULT_SSP_REG_CR0, SSP_CR0(pl022->virtbase)); | |
645 | writew(DEFAULT_SSP_REG_CR1, SSP_CR1(pl022->virtbase)); | |
646 | } | |
b43d65f7 LW |
647 | writew(DEFAULT_SSP_REG_DMACR, SSP_DMACR(pl022->virtbase)); |
648 | writew(DEFAULT_SSP_REG_CPSR, SSP_CPSR(pl022->virtbase)); | |
649 | writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase)); | |
650 | writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase)); | |
651 | } | |
652 | ||
653 | /** | |
654 | * This will write to TX and read from RX according to the parameters | |
655 | * set in pl022. | |
656 | */ | |
657 | static void readwriter(struct pl022 *pl022) | |
658 | { | |
659 | ||
660 | /* | |
25985edc | 661 | * The FIFO depth is different between primecell variants. |
b43d65f7 LW |
662 | * I believe filling in too much in the FIFO might cause |
663 | * errons in 8bit wide transfers on ARM variants (just 8 words | |
664 | * FIFO, means only 8x8 = 64 bits in FIFO) at least. | |
665 | * | |
fc05475f LW |
666 | * To prevent this issue, the TX FIFO is only filled to the |
667 | * unused RX FIFO fill length, regardless of what the TX | |
668 | * FIFO status flag indicates. | |
b43d65f7 LW |
669 | */ |
670 | dev_dbg(&pl022->adev->dev, | |
671 | "%s, rx: %p, rxend: %p, tx: %p, txend: %p\n", | |
672 | __func__, pl022->rx, pl022->rx_end, pl022->tx, pl022->tx_end); | |
673 | ||
674 | /* Read as much as you can */ | |
675 | while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE) | |
676 | && (pl022->rx < pl022->rx_end)) { | |
677 | switch (pl022->read) { | |
678 | case READING_NULL: | |
679 | readw(SSP_DR(pl022->virtbase)); | |
680 | break; | |
681 | case READING_U8: | |
682 | *(u8 *) (pl022->rx) = | |
683 | readw(SSP_DR(pl022->virtbase)) & 0xFFU; | |
684 | break; | |
685 | case READING_U16: | |
686 | *(u16 *) (pl022->rx) = | |
687 | (u16) readw(SSP_DR(pl022->virtbase)); | |
688 | break; | |
689 | case READING_U32: | |
690 | *(u32 *) (pl022->rx) = | |
691 | readl(SSP_DR(pl022->virtbase)); | |
692 | break; | |
693 | } | |
694 | pl022->rx += (pl022->cur_chip->n_bytes); | |
fc05475f | 695 | pl022->exp_fifo_level--; |
b43d65f7 LW |
696 | } |
697 | /* | |
fc05475f | 698 | * Write as much as possible up to the RX FIFO size |
b43d65f7 | 699 | */ |
fc05475f | 700 | while ((pl022->exp_fifo_level < pl022->vendor->fifodepth) |
b43d65f7 LW |
701 | && (pl022->tx < pl022->tx_end)) { |
702 | switch (pl022->write) { | |
703 | case WRITING_NULL: | |
704 | writew(0x0, SSP_DR(pl022->virtbase)); | |
705 | break; | |
706 | case WRITING_U8: | |
707 | writew(*(u8 *) (pl022->tx), SSP_DR(pl022->virtbase)); | |
708 | break; | |
709 | case WRITING_U16: | |
710 | writew((*(u16 *) (pl022->tx)), SSP_DR(pl022->virtbase)); | |
711 | break; | |
712 | case WRITING_U32: | |
713 | writel(*(u32 *) (pl022->tx), SSP_DR(pl022->virtbase)); | |
714 | break; | |
715 | } | |
716 | pl022->tx += (pl022->cur_chip->n_bytes); | |
fc05475f | 717 | pl022->exp_fifo_level++; |
b43d65f7 LW |
718 | /* |
719 | * This inner reader takes care of things appearing in the RX | |
720 | * FIFO as we're transmitting. This will happen a lot since the | |
721 | * clock starts running when you put things into the TX FIFO, | |
25985edc | 722 | * and then things are continuously clocked into the RX FIFO. |
b43d65f7 LW |
723 | */ |
724 | while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE) | |
725 | && (pl022->rx < pl022->rx_end)) { | |
726 | switch (pl022->read) { | |
727 | case READING_NULL: | |
728 | readw(SSP_DR(pl022->virtbase)); | |
729 | break; | |
730 | case READING_U8: | |
731 | *(u8 *) (pl022->rx) = | |
732 | readw(SSP_DR(pl022->virtbase)) & 0xFFU; | |
733 | break; | |
734 | case READING_U16: | |
735 | *(u16 *) (pl022->rx) = | |
736 | (u16) readw(SSP_DR(pl022->virtbase)); | |
737 | break; | |
738 | case READING_U32: | |
739 | *(u32 *) (pl022->rx) = | |
740 | readl(SSP_DR(pl022->virtbase)); | |
741 | break; | |
742 | } | |
743 | pl022->rx += (pl022->cur_chip->n_bytes); | |
fc05475f | 744 | pl022->exp_fifo_level--; |
b43d65f7 LW |
745 | } |
746 | } | |
747 | /* | |
748 | * When we exit here the TX FIFO should be full and the RX FIFO | |
749 | * should be empty | |
750 | */ | |
751 | } | |
752 | ||
753 | ||
754 | /** | |
755 | * next_transfer - Move to the Next transfer in the current spi message | |
756 | * @pl022: SSP driver private data structure | |
757 | * | |
758 | * This function moves though the linked list of spi transfers in the | |
759 | * current spi message and returns with the state of current spi | |
760 | * message i.e whether its last transfer is done(STATE_DONE) or | |
761 | * Next transfer is ready(STATE_RUNNING) | |
762 | */ | |
763 | static void *next_transfer(struct pl022 *pl022) | |
764 | { | |
765 | struct spi_message *msg = pl022->cur_msg; | |
766 | struct spi_transfer *trans = pl022->cur_transfer; | |
767 | ||
768 | /* Move to next transfer */ | |
769 | if (trans->transfer_list.next != &msg->transfers) { | |
770 | pl022->cur_transfer = | |
771 | list_entry(trans->transfer_list.next, | |
772 | struct spi_transfer, transfer_list); | |
773 | return STATE_RUNNING; | |
774 | } | |
775 | return STATE_DONE; | |
776 | } | |
b1b6b9aa LW |
777 | |
778 | /* | |
779 | * This DMA functionality is only compiled in if we have | |
780 | * access to the generic DMA devices/DMA engine. | |
781 | */ | |
782 | #ifdef CONFIG_DMA_ENGINE | |
783 | static void unmap_free_dma_scatter(struct pl022 *pl022) | |
784 | { | |
785 | /* Unmap and free the SG tables */ | |
b7298896 | 786 | dma_unmap_sg(pl022->dma_tx_channel->device->dev, pl022->sgt_tx.sgl, |
b1b6b9aa | 787 | pl022->sgt_tx.nents, DMA_TO_DEVICE); |
b7298896 | 788 | dma_unmap_sg(pl022->dma_rx_channel->device->dev, pl022->sgt_rx.sgl, |
b1b6b9aa LW |
789 | pl022->sgt_rx.nents, DMA_FROM_DEVICE); |
790 | sg_free_table(&pl022->sgt_rx); | |
791 | sg_free_table(&pl022->sgt_tx); | |
792 | } | |
793 | ||
794 | static void dma_callback(void *data) | |
795 | { | |
796 | struct pl022 *pl022 = data; | |
797 | struct spi_message *msg = pl022->cur_msg; | |
798 | ||
799 | BUG_ON(!pl022->sgt_rx.sgl); | |
800 | ||
801 | #ifdef VERBOSE_DEBUG | |
802 | /* | |
803 | * Optionally dump out buffers to inspect contents, this is | |
804 | * good if you want to convince yourself that the loopback | |
805 | * read/write contents are the same, when adopting to a new | |
806 | * DMA engine. | |
807 | */ | |
808 | { | |
809 | struct scatterlist *sg; | |
810 | unsigned int i; | |
811 | ||
812 | dma_sync_sg_for_cpu(&pl022->adev->dev, | |
813 | pl022->sgt_rx.sgl, | |
814 | pl022->sgt_rx.nents, | |
815 | DMA_FROM_DEVICE); | |
816 | ||
817 | for_each_sg(pl022->sgt_rx.sgl, sg, pl022->sgt_rx.nents, i) { | |
818 | dev_dbg(&pl022->adev->dev, "SPI RX SG ENTRY: %d", i); | |
819 | print_hex_dump(KERN_ERR, "SPI RX: ", | |
820 | DUMP_PREFIX_OFFSET, | |
821 | 16, | |
822 | 1, | |
823 | sg_virt(sg), | |
824 | sg_dma_len(sg), | |
825 | 1); | |
826 | } | |
827 | for_each_sg(pl022->sgt_tx.sgl, sg, pl022->sgt_tx.nents, i) { | |
828 | dev_dbg(&pl022->adev->dev, "SPI TX SG ENTRY: %d", i); | |
829 | print_hex_dump(KERN_ERR, "SPI TX: ", | |
830 | DUMP_PREFIX_OFFSET, | |
831 | 16, | |
832 | 1, | |
833 | sg_virt(sg), | |
834 | sg_dma_len(sg), | |
835 | 1); | |
836 | } | |
837 | } | |
838 | #endif | |
839 | ||
840 | unmap_free_dma_scatter(pl022); | |
841 | ||
25985edc | 842 | /* Update total bytes transferred */ |
b1b6b9aa LW |
843 | msg->actual_length += pl022->cur_transfer->len; |
844 | if (pl022->cur_transfer->cs_change) | |
845 | pl022->cur_chip-> | |
846 | cs_control(SSP_CHIP_DESELECT); | |
847 | ||
848 | /* Move to next transfer */ | |
849 | msg->state = next_transfer(pl022); | |
850 | tasklet_schedule(&pl022->pump_transfers); | |
851 | } | |
852 | ||
853 | static void setup_dma_scatter(struct pl022 *pl022, | |
854 | void *buffer, | |
855 | unsigned int length, | |
856 | struct sg_table *sgtab) | |
857 | { | |
858 | struct scatterlist *sg; | |
859 | int bytesleft = length; | |
860 | void *bufp = buffer; | |
861 | int mapbytes; | |
862 | int i; | |
863 | ||
864 | if (buffer) { | |
865 | for_each_sg(sgtab->sgl, sg, sgtab->nents, i) { | |
866 | /* | |
867 | * If there are less bytes left than what fits | |
868 | * in the current page (plus page alignment offset) | |
869 | * we just feed in this, else we stuff in as much | |
870 | * as we can. | |
871 | */ | |
872 | if (bytesleft < (PAGE_SIZE - offset_in_page(bufp))) | |
873 | mapbytes = bytesleft; | |
874 | else | |
875 | mapbytes = PAGE_SIZE - offset_in_page(bufp); | |
876 | sg_set_page(sg, virt_to_page(bufp), | |
877 | mapbytes, offset_in_page(bufp)); | |
878 | bufp += mapbytes; | |
879 | bytesleft -= mapbytes; | |
880 | dev_dbg(&pl022->adev->dev, | |
881 | "set RX/TX target page @ %p, %d bytes, %d left\n", | |
882 | bufp, mapbytes, bytesleft); | |
883 | } | |
884 | } else { | |
885 | /* Map the dummy buffer on every page */ | |
886 | for_each_sg(sgtab->sgl, sg, sgtab->nents, i) { | |
887 | if (bytesleft < PAGE_SIZE) | |
888 | mapbytes = bytesleft; | |
889 | else | |
890 | mapbytes = PAGE_SIZE; | |
891 | sg_set_page(sg, virt_to_page(pl022->dummypage), | |
892 | mapbytes, 0); | |
893 | bytesleft -= mapbytes; | |
894 | dev_dbg(&pl022->adev->dev, | |
895 | "set RX/TX to dummy page %d bytes, %d left\n", | |
896 | mapbytes, bytesleft); | |
897 | ||
898 | } | |
899 | } | |
900 | BUG_ON(bytesleft); | |
901 | } | |
902 | ||
903 | /** | |
904 | * configure_dma - configures the channels for the next transfer | |
905 | * @pl022: SSP driver's private data structure | |
906 | */ | |
907 | static int configure_dma(struct pl022 *pl022) | |
908 | { | |
909 | struct dma_slave_config rx_conf = { | |
910 | .src_addr = SSP_DR(pl022->phybase), | |
911 | .direction = DMA_FROM_DEVICE, | |
912 | .src_maxburst = pl022->vendor->fifodepth >> 1, | |
913 | }; | |
914 | struct dma_slave_config tx_conf = { | |
915 | .dst_addr = SSP_DR(pl022->phybase), | |
916 | .direction = DMA_TO_DEVICE, | |
917 | .dst_maxburst = pl022->vendor->fifodepth >> 1, | |
918 | }; | |
919 | unsigned int pages; | |
920 | int ret; | |
082086f2 | 921 | int rx_sglen, tx_sglen; |
b1b6b9aa LW |
922 | struct dma_chan *rxchan = pl022->dma_rx_channel; |
923 | struct dma_chan *txchan = pl022->dma_tx_channel; | |
924 | struct dma_async_tx_descriptor *rxdesc; | |
925 | struct dma_async_tx_descriptor *txdesc; | |
b1b6b9aa LW |
926 | |
927 | /* Check that the channels are available */ | |
928 | if (!rxchan || !txchan) | |
929 | return -ENODEV; | |
930 | ||
931 | switch (pl022->read) { | |
932 | case READING_NULL: | |
933 | /* Use the same as for writing */ | |
934 | rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED; | |
935 | break; | |
936 | case READING_U8: | |
937 | rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; | |
938 | break; | |
939 | case READING_U16: | |
940 | rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; | |
941 | break; | |
942 | case READING_U32: | |
943 | rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; | |
944 | break; | |
945 | } | |
946 | ||
947 | switch (pl022->write) { | |
948 | case WRITING_NULL: | |
949 | /* Use the same as for reading */ | |
950 | tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED; | |
951 | break; | |
952 | case WRITING_U8: | |
953 | tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; | |
954 | break; | |
955 | case WRITING_U16: | |
956 | tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; | |
957 | break; | |
958 | case WRITING_U32: | |
bc3f67a3 | 959 | tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
b1b6b9aa LW |
960 | break; |
961 | } | |
962 | ||
963 | /* SPI pecularity: we need to read and write the same width */ | |
964 | if (rx_conf.src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) | |
965 | rx_conf.src_addr_width = tx_conf.dst_addr_width; | |
966 | if (tx_conf.dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) | |
967 | tx_conf.dst_addr_width = rx_conf.src_addr_width; | |
968 | BUG_ON(rx_conf.src_addr_width != tx_conf.dst_addr_width); | |
969 | ||
ecd442fd LW |
970 | dmaengine_slave_config(rxchan, &rx_conf); |
971 | dmaengine_slave_config(txchan, &tx_conf); | |
b1b6b9aa LW |
972 | |
973 | /* Create sglists for the transfers */ | |
974 | pages = (pl022->cur_transfer->len >> PAGE_SHIFT) + 1; | |
975 | dev_dbg(&pl022->adev->dev, "using %d pages for transfer\n", pages); | |
976 | ||
977 | ret = sg_alloc_table(&pl022->sgt_rx, pages, GFP_KERNEL); | |
978 | if (ret) | |
979 | goto err_alloc_rx_sg; | |
980 | ||
981 | ret = sg_alloc_table(&pl022->sgt_tx, pages, GFP_KERNEL); | |
982 | if (ret) | |
983 | goto err_alloc_tx_sg; | |
984 | ||
985 | /* Fill in the scatterlists for the RX+TX buffers */ | |
986 | setup_dma_scatter(pl022, pl022->rx, | |
987 | pl022->cur_transfer->len, &pl022->sgt_rx); | |
988 | setup_dma_scatter(pl022, pl022->tx, | |
989 | pl022->cur_transfer->len, &pl022->sgt_tx); | |
990 | ||
991 | /* Map DMA buffers */ | |
082086f2 | 992 | rx_sglen = dma_map_sg(rxchan->device->dev, pl022->sgt_rx.sgl, |
b1b6b9aa | 993 | pl022->sgt_rx.nents, DMA_FROM_DEVICE); |
082086f2 | 994 | if (!rx_sglen) |
b1b6b9aa LW |
995 | goto err_rx_sgmap; |
996 | ||
082086f2 | 997 | tx_sglen = dma_map_sg(txchan->device->dev, pl022->sgt_tx.sgl, |
b1b6b9aa | 998 | pl022->sgt_tx.nents, DMA_TO_DEVICE); |
082086f2 | 999 | if (!tx_sglen) |
b1b6b9aa LW |
1000 | goto err_tx_sgmap; |
1001 | ||
1002 | /* Send both scatterlists */ | |
1003 | rxdesc = rxchan->device->device_prep_slave_sg(rxchan, | |
1004 | pl022->sgt_rx.sgl, | |
082086f2 | 1005 | rx_sglen, |
b1b6b9aa LW |
1006 | DMA_FROM_DEVICE, |
1007 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); | |
1008 | if (!rxdesc) | |
1009 | goto err_rxdesc; | |
1010 | ||
1011 | txdesc = txchan->device->device_prep_slave_sg(txchan, | |
1012 | pl022->sgt_tx.sgl, | |
082086f2 | 1013 | tx_sglen, |
b1b6b9aa LW |
1014 | DMA_TO_DEVICE, |
1015 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); | |
1016 | if (!txdesc) | |
1017 | goto err_txdesc; | |
1018 | ||
1019 | /* Put the callback on the RX transfer only, that should finish last */ | |
1020 | rxdesc->callback = dma_callback; | |
1021 | rxdesc->callback_param = pl022; | |
1022 | ||
1023 | /* Submit and fire RX and TX with TX last so we're ready to read! */ | |
ecd442fd LW |
1024 | dmaengine_submit(rxdesc); |
1025 | dmaengine_submit(txdesc); | |
1026 | dma_async_issue_pending(rxchan); | |
1027 | dma_async_issue_pending(txchan); | |
b1b6b9aa LW |
1028 | |
1029 | return 0; | |
1030 | ||
b1b6b9aa | 1031 | err_txdesc: |
ecd442fd | 1032 | dmaengine_terminate_all(txchan); |
b1b6b9aa | 1033 | err_rxdesc: |
ecd442fd | 1034 | dmaengine_terminate_all(rxchan); |
b7298896 | 1035 | dma_unmap_sg(txchan->device->dev, pl022->sgt_tx.sgl, |
b1b6b9aa LW |
1036 | pl022->sgt_tx.nents, DMA_TO_DEVICE); |
1037 | err_tx_sgmap: | |
b7298896 | 1038 | dma_unmap_sg(rxchan->device->dev, pl022->sgt_rx.sgl, |
b1b6b9aa LW |
1039 | pl022->sgt_tx.nents, DMA_FROM_DEVICE); |
1040 | err_rx_sgmap: | |
1041 | sg_free_table(&pl022->sgt_tx); | |
1042 | err_alloc_tx_sg: | |
1043 | sg_free_table(&pl022->sgt_rx); | |
1044 | err_alloc_rx_sg: | |
1045 | return -ENOMEM; | |
1046 | } | |
1047 | ||
1048 | static int __init pl022_dma_probe(struct pl022 *pl022) | |
1049 | { | |
1050 | dma_cap_mask_t mask; | |
1051 | ||
1052 | /* Try to acquire a generic DMA engine slave channel */ | |
1053 | dma_cap_zero(mask); | |
1054 | dma_cap_set(DMA_SLAVE, mask); | |
1055 | /* | |
1056 | * We need both RX and TX channels to do DMA, else do none | |
1057 | * of them. | |
1058 | */ | |
1059 | pl022->dma_rx_channel = dma_request_channel(mask, | |
1060 | pl022->master_info->dma_filter, | |
1061 | pl022->master_info->dma_rx_param); | |
1062 | if (!pl022->dma_rx_channel) { | |
43c64015 | 1063 | dev_dbg(&pl022->adev->dev, "no RX DMA channel!\n"); |
b1b6b9aa LW |
1064 | goto err_no_rxchan; |
1065 | } | |
1066 | ||
1067 | pl022->dma_tx_channel = dma_request_channel(mask, | |
1068 | pl022->master_info->dma_filter, | |
1069 | pl022->master_info->dma_tx_param); | |
1070 | if (!pl022->dma_tx_channel) { | |
43c64015 | 1071 | dev_dbg(&pl022->adev->dev, "no TX DMA channel!\n"); |
b1b6b9aa LW |
1072 | goto err_no_txchan; |
1073 | } | |
1074 | ||
1075 | pl022->dummypage = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
1076 | if (!pl022->dummypage) { | |
43c64015 | 1077 | dev_dbg(&pl022->adev->dev, "no DMA dummypage!\n"); |
b1b6b9aa LW |
1078 | goto err_no_dummypage; |
1079 | } | |
1080 | ||
1081 | dev_info(&pl022->adev->dev, "setup for DMA on RX %s, TX %s\n", | |
1082 | dma_chan_name(pl022->dma_rx_channel), | |
1083 | dma_chan_name(pl022->dma_tx_channel)); | |
1084 | ||
1085 | return 0; | |
1086 | ||
1087 | err_no_dummypage: | |
1088 | dma_release_channel(pl022->dma_tx_channel); | |
1089 | err_no_txchan: | |
1090 | dma_release_channel(pl022->dma_rx_channel); | |
1091 | pl022->dma_rx_channel = NULL; | |
1092 | err_no_rxchan: | |
43c64015 VK |
1093 | dev_err(&pl022->adev->dev, |
1094 | "Failed to work in dma mode, work without dma!\n"); | |
b1b6b9aa LW |
1095 | return -ENODEV; |
1096 | } | |
1097 | ||
1098 | static void terminate_dma(struct pl022 *pl022) | |
1099 | { | |
1100 | struct dma_chan *rxchan = pl022->dma_rx_channel; | |
1101 | struct dma_chan *txchan = pl022->dma_tx_channel; | |
1102 | ||
ecd442fd LW |
1103 | dmaengine_terminate_all(rxchan); |
1104 | dmaengine_terminate_all(txchan); | |
b1b6b9aa LW |
1105 | unmap_free_dma_scatter(pl022); |
1106 | } | |
1107 | ||
1108 | static void pl022_dma_remove(struct pl022 *pl022) | |
1109 | { | |
1110 | if (pl022->busy) | |
1111 | terminate_dma(pl022); | |
1112 | if (pl022->dma_tx_channel) | |
1113 | dma_release_channel(pl022->dma_tx_channel); | |
1114 | if (pl022->dma_rx_channel) | |
1115 | dma_release_channel(pl022->dma_rx_channel); | |
1116 | kfree(pl022->dummypage); | |
1117 | } | |
1118 | ||
1119 | #else | |
1120 | static inline int configure_dma(struct pl022 *pl022) | |
1121 | { | |
1122 | return -ENODEV; | |
1123 | } | |
1124 | ||
1125 | static inline int pl022_dma_probe(struct pl022 *pl022) | |
1126 | { | |
1127 | return 0; | |
1128 | } | |
1129 | ||
1130 | static inline void pl022_dma_remove(struct pl022 *pl022) | |
1131 | { | |
1132 | } | |
1133 | #endif | |
1134 | ||
b43d65f7 LW |
1135 | /** |
1136 | * pl022_interrupt_handler - Interrupt handler for SSP controller | |
1137 | * | |
1138 | * This function handles interrupts generated for an interrupt based transfer. | |
1139 | * If a receive overrun (ROR) interrupt is there then we disable SSP, flag the | |
1140 | * current message's state as STATE_ERROR and schedule the tasklet | |
1141 | * pump_transfers which will do the postprocessing of the current message by | |
1142 | * calling giveback(). Otherwise it reads data from RX FIFO till there is no | |
1143 | * more data, and writes data in TX FIFO till it is not full. If we complete | |
1144 | * the transfer we move to the next transfer and schedule the tasklet. | |
1145 | */ | |
1146 | static irqreturn_t pl022_interrupt_handler(int irq, void *dev_id) | |
1147 | { | |
1148 | struct pl022 *pl022 = dev_id; | |
1149 | struct spi_message *msg = pl022->cur_msg; | |
1150 | u16 irq_status = 0; | |
1151 | u16 flag = 0; | |
1152 | ||
1153 | if (unlikely(!msg)) { | |
1154 | dev_err(&pl022->adev->dev, | |
1155 | "bad message state in interrupt handler"); | |
1156 | /* Never fail */ | |
1157 | return IRQ_HANDLED; | |
1158 | } | |
1159 | ||
1160 | /* Read the Interrupt Status Register */ | |
1161 | irq_status = readw(SSP_MIS(pl022->virtbase)); | |
1162 | ||
1163 | if (unlikely(!irq_status)) | |
1164 | return IRQ_NONE; | |
1165 | ||
b1b6b9aa LW |
1166 | /* |
1167 | * This handles the FIFO interrupts, the timeout | |
1168 | * interrupts are flatly ignored, they cannot be | |
1169 | * trusted. | |
1170 | */ | |
b43d65f7 LW |
1171 | if (unlikely(irq_status & SSP_MIS_MASK_RORMIS)) { |
1172 | /* | |
1173 | * Overrun interrupt - bail out since our Data has been | |
1174 | * corrupted | |
1175 | */ | |
b1b6b9aa | 1176 | dev_err(&pl022->adev->dev, "FIFO overrun\n"); |
b43d65f7 LW |
1177 | if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RFF) |
1178 | dev_err(&pl022->adev->dev, | |
1179 | "RXFIFO is full\n"); | |
1180 | if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_TNF) | |
1181 | dev_err(&pl022->adev->dev, | |
1182 | "TXFIFO is full\n"); | |
1183 | ||
1184 | /* | |
1185 | * Disable and clear interrupts, disable SSP, | |
1186 | * mark message with bad status so it can be | |
1187 | * retried. | |
1188 | */ | |
1189 | writew(DISABLE_ALL_INTERRUPTS, | |
1190 | SSP_IMSC(pl022->virtbase)); | |
1191 | writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase)); | |
1192 | writew((readw(SSP_CR1(pl022->virtbase)) & | |
1193 | (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase)); | |
1194 | msg->state = STATE_ERROR; | |
1195 | ||
1196 | /* Schedule message queue handler */ | |
1197 | tasklet_schedule(&pl022->pump_transfers); | |
1198 | return IRQ_HANDLED; | |
1199 | } | |
1200 | ||
1201 | readwriter(pl022); | |
1202 | ||
1203 | if ((pl022->tx == pl022->tx_end) && (flag == 0)) { | |
1204 | flag = 1; | |
1205 | /* Disable Transmit interrupt */ | |
1206 | writew(readw(SSP_IMSC(pl022->virtbase)) & | |
1207 | (~SSP_IMSC_MASK_TXIM), | |
1208 | SSP_IMSC(pl022->virtbase)); | |
1209 | } | |
1210 | ||
1211 | /* | |
1212 | * Since all transactions must write as much as shall be read, | |
1213 | * we can conclude the entire transaction once RX is complete. | |
1214 | * At this point, all TX will always be finished. | |
1215 | */ | |
1216 | if (pl022->rx >= pl022->rx_end) { | |
1217 | writew(DISABLE_ALL_INTERRUPTS, | |
1218 | SSP_IMSC(pl022->virtbase)); | |
1219 | writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase)); | |
1220 | if (unlikely(pl022->rx > pl022->rx_end)) { | |
1221 | dev_warn(&pl022->adev->dev, "read %u surplus " | |
1222 | "bytes (did you request an odd " | |
1223 | "number of bytes on a 16bit bus?)\n", | |
1224 | (u32) (pl022->rx - pl022->rx_end)); | |
1225 | } | |
25985edc | 1226 | /* Update total bytes transferred */ |
b43d65f7 LW |
1227 | msg->actual_length += pl022->cur_transfer->len; |
1228 | if (pl022->cur_transfer->cs_change) | |
1229 | pl022->cur_chip-> | |
1230 | cs_control(SSP_CHIP_DESELECT); | |
1231 | /* Move to next transfer */ | |
1232 | msg->state = next_transfer(pl022); | |
1233 | tasklet_schedule(&pl022->pump_transfers); | |
1234 | return IRQ_HANDLED; | |
1235 | } | |
1236 | ||
1237 | return IRQ_HANDLED; | |
1238 | } | |
1239 | ||
1240 | /** | |
1241 | * This sets up the pointers to memory for the next message to | |
1242 | * send out on the SPI bus. | |
1243 | */ | |
1244 | static int set_up_next_transfer(struct pl022 *pl022, | |
1245 | struct spi_transfer *transfer) | |
1246 | { | |
1247 | int residue; | |
1248 | ||
1249 | /* Sanity check the message for this bus width */ | |
1250 | residue = pl022->cur_transfer->len % pl022->cur_chip->n_bytes; | |
1251 | if (unlikely(residue != 0)) { | |
1252 | dev_err(&pl022->adev->dev, | |
1253 | "message of %u bytes to transmit but the current " | |
1254 | "chip bus has a data width of %u bytes!\n", | |
1255 | pl022->cur_transfer->len, | |
1256 | pl022->cur_chip->n_bytes); | |
1257 | dev_err(&pl022->adev->dev, "skipping this message\n"); | |
1258 | return -EIO; | |
1259 | } | |
1260 | pl022->tx = (void *)transfer->tx_buf; | |
1261 | pl022->tx_end = pl022->tx + pl022->cur_transfer->len; | |
1262 | pl022->rx = (void *)transfer->rx_buf; | |
1263 | pl022->rx_end = pl022->rx + pl022->cur_transfer->len; | |
1264 | pl022->write = | |
1265 | pl022->tx ? pl022->cur_chip->write : WRITING_NULL; | |
1266 | pl022->read = pl022->rx ? pl022->cur_chip->read : READING_NULL; | |
1267 | return 0; | |
1268 | } | |
1269 | ||
1270 | /** | |
b1b6b9aa LW |
1271 | * pump_transfers - Tasklet function which schedules next transfer |
1272 | * when running in interrupt or DMA transfer mode. | |
b43d65f7 LW |
1273 | * @data: SSP driver private data structure |
1274 | * | |
1275 | */ | |
1276 | static void pump_transfers(unsigned long data) | |
1277 | { | |
1278 | struct pl022 *pl022 = (struct pl022 *) data; | |
1279 | struct spi_message *message = NULL; | |
1280 | struct spi_transfer *transfer = NULL; | |
1281 | struct spi_transfer *previous = NULL; | |
1282 | ||
1283 | /* Get current state information */ | |
1284 | message = pl022->cur_msg; | |
1285 | transfer = pl022->cur_transfer; | |
1286 | ||
1287 | /* Handle for abort */ | |
1288 | if (message->state == STATE_ERROR) { | |
1289 | message->status = -EIO; | |
1290 | giveback(pl022); | |
1291 | return; | |
1292 | } | |
1293 | ||
1294 | /* Handle end of message */ | |
1295 | if (message->state == STATE_DONE) { | |
1296 | message->status = 0; | |
1297 | giveback(pl022); | |
1298 | return; | |
1299 | } | |
1300 | ||
1301 | /* Delay if requested at end of transfer before CS change */ | |
1302 | if (message->state == STATE_RUNNING) { | |
1303 | previous = list_entry(transfer->transfer_list.prev, | |
1304 | struct spi_transfer, | |
1305 | transfer_list); | |
1306 | if (previous->delay_usecs) | |
1307 | /* | |
1308 | * FIXME: This runs in interrupt context. | |
1309 | * Is this really smart? | |
1310 | */ | |
1311 | udelay(previous->delay_usecs); | |
1312 | ||
1313 | /* Drop chip select only if cs_change is requested */ | |
1314 | if (previous->cs_change) | |
1315 | pl022->cur_chip->cs_control(SSP_CHIP_SELECT); | |
1316 | } else { | |
1317 | /* STATE_START */ | |
1318 | message->state = STATE_RUNNING; | |
1319 | } | |
1320 | ||
1321 | if (set_up_next_transfer(pl022, transfer)) { | |
1322 | message->state = STATE_ERROR; | |
1323 | message->status = -EIO; | |
1324 | giveback(pl022); | |
1325 | return; | |
1326 | } | |
1327 | /* Flush the FIFOs and let's go! */ | |
1328 | flush(pl022); | |
b43d65f7 | 1329 | |
b1b6b9aa LW |
1330 | if (pl022->cur_chip->enable_dma) { |
1331 | if (configure_dma(pl022)) { | |
1332 | dev_dbg(&pl022->adev->dev, | |
1333 | "configuration of DMA failed, fall back to interrupt mode\n"); | |
1334 | goto err_config_dma; | |
1335 | } | |
b43d65f7 LW |
1336 | return; |
1337 | } | |
b43d65f7 | 1338 | |
b1b6b9aa LW |
1339 | err_config_dma: |
1340 | writew(ENABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase)); | |
b43d65f7 LW |
1341 | } |
1342 | ||
b1b6b9aa | 1343 | static void do_interrupt_dma_transfer(struct pl022 *pl022) |
b43d65f7 | 1344 | { |
b1b6b9aa | 1345 | u32 irqflags = ENABLE_ALL_INTERRUPTS; |
b43d65f7 LW |
1346 | |
1347 | /* Enable target chip */ | |
1348 | pl022->cur_chip->cs_control(SSP_CHIP_SELECT); | |
1349 | if (set_up_next_transfer(pl022, pl022->cur_transfer)) { | |
1350 | /* Error path */ | |
1351 | pl022->cur_msg->state = STATE_ERROR; | |
1352 | pl022->cur_msg->status = -EIO; | |
1353 | giveback(pl022); | |
1354 | return; | |
1355 | } | |
b1b6b9aa LW |
1356 | /* If we're using DMA, set up DMA here */ |
1357 | if (pl022->cur_chip->enable_dma) { | |
1358 | /* Configure DMA transfer */ | |
1359 | if (configure_dma(pl022)) { | |
1360 | dev_dbg(&pl022->adev->dev, | |
1361 | "configuration of DMA failed, fall back to interrupt mode\n"); | |
1362 | goto err_config_dma; | |
1363 | } | |
1364 | /* Disable interrupts in DMA mode, IRQ from DMA controller */ | |
1365 | irqflags = DISABLE_ALL_INTERRUPTS; | |
1366 | } | |
1367 | err_config_dma: | |
b43d65f7 LW |
1368 | /* Enable SSP, turn on interrupts */ |
1369 | writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE), | |
1370 | SSP_CR1(pl022->virtbase)); | |
b1b6b9aa | 1371 | writew(irqflags, SSP_IMSC(pl022->virtbase)); |
b43d65f7 LW |
1372 | } |
1373 | ||
b1b6b9aa | 1374 | static void do_polling_transfer(struct pl022 *pl022) |
b43d65f7 | 1375 | { |
b43d65f7 LW |
1376 | struct spi_message *message = NULL; |
1377 | struct spi_transfer *transfer = NULL; | |
1378 | struct spi_transfer *previous = NULL; | |
1379 | struct chip_data *chip; | |
a18c266f | 1380 | unsigned long time, timeout; |
b43d65f7 LW |
1381 | |
1382 | chip = pl022->cur_chip; | |
1383 | message = pl022->cur_msg; | |
1384 | ||
1385 | while (message->state != STATE_DONE) { | |
1386 | /* Handle for abort */ | |
1387 | if (message->state == STATE_ERROR) | |
1388 | break; | |
1389 | transfer = pl022->cur_transfer; | |
1390 | ||
1391 | /* Delay if requested at end of transfer */ | |
1392 | if (message->state == STATE_RUNNING) { | |
1393 | previous = | |
1394 | list_entry(transfer->transfer_list.prev, | |
1395 | struct spi_transfer, transfer_list); | |
1396 | if (previous->delay_usecs) | |
1397 | udelay(previous->delay_usecs); | |
1398 | if (previous->cs_change) | |
1399 | pl022->cur_chip->cs_control(SSP_CHIP_SELECT); | |
1400 | } else { | |
1401 | /* STATE_START */ | |
1402 | message->state = STATE_RUNNING; | |
1403 | pl022->cur_chip->cs_control(SSP_CHIP_SELECT); | |
1404 | } | |
1405 | ||
1406 | /* Configuration Changing Per Transfer */ | |
1407 | if (set_up_next_transfer(pl022, transfer)) { | |
1408 | /* Error path */ | |
1409 | message->state = STATE_ERROR; | |
1410 | break; | |
1411 | } | |
1412 | /* Flush FIFOs and enable SSP */ | |
1413 | flush(pl022); | |
1414 | writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE), | |
1415 | SSP_CR1(pl022->virtbase)); | |
1416 | ||
556f4aeb | 1417 | dev_dbg(&pl022->adev->dev, "polling transfer ongoing ...\n"); |
a18c266f MT |
1418 | |
1419 | timeout = jiffies + msecs_to_jiffies(SPI_POLLING_TIMEOUT); | |
1420 | while (pl022->tx < pl022->tx_end || pl022->rx < pl022->rx_end) { | |
1421 | time = jiffies; | |
b43d65f7 | 1422 | readwriter(pl022); |
a18c266f MT |
1423 | if (time_after(time, timeout)) { |
1424 | dev_warn(&pl022->adev->dev, | |
1425 | "%s: timeout!\n", __func__); | |
1426 | message->state = STATE_ERROR; | |
1427 | goto out; | |
1428 | } | |
521999bd | 1429 | cpu_relax(); |
a18c266f | 1430 | } |
b43d65f7 | 1431 | |
25985edc | 1432 | /* Update total byte transferred */ |
b43d65f7 LW |
1433 | message->actual_length += pl022->cur_transfer->len; |
1434 | if (pl022->cur_transfer->cs_change) | |
1435 | pl022->cur_chip->cs_control(SSP_CHIP_DESELECT); | |
1436 | /* Move to next transfer */ | |
1437 | message->state = next_transfer(pl022); | |
1438 | } | |
a18c266f | 1439 | out: |
b43d65f7 LW |
1440 | /* Handle end of message */ |
1441 | if (message->state == STATE_DONE) | |
1442 | message->status = 0; | |
1443 | else | |
1444 | message->status = -EIO; | |
1445 | ||
1446 | giveback(pl022); | |
1447 | return; | |
1448 | } | |
1449 | ||
1450 | /** | |
1451 | * pump_messages - Workqueue function which processes spi message queue | |
1452 | * @data: pointer to private data of SSP driver | |
1453 | * | |
1454 | * This function checks if there is any spi message in the queue that | |
1455 | * needs processing and delegate control to appropriate function | |
b1b6b9aa | 1456 | * do_polling_transfer()/do_interrupt_dma_transfer() |
b43d65f7 LW |
1457 | * based on the kind of the transfer |
1458 | * | |
1459 | */ | |
1460 | static void pump_messages(struct work_struct *work) | |
1461 | { | |
1462 | struct pl022 *pl022 = | |
1463 | container_of(work, struct pl022, pump_messages); | |
1464 | unsigned long flags; | |
1465 | ||
1466 | /* Lock queue and check for queue work */ | |
1467 | spin_lock_irqsave(&pl022->queue_lock, flags); | |
5e8b821d | 1468 | if (list_empty(&pl022->queue) || !pl022->running) { |
dec5a581 | 1469 | pl022->busy = false; |
b43d65f7 LW |
1470 | spin_unlock_irqrestore(&pl022->queue_lock, flags); |
1471 | return; | |
1472 | } | |
1473 | /* Make sure we are not already running a message */ | |
1474 | if (pl022->cur_msg) { | |
1475 | spin_unlock_irqrestore(&pl022->queue_lock, flags); | |
1476 | return; | |
1477 | } | |
1478 | /* Extract head of queue */ | |
1479 | pl022->cur_msg = | |
1480 | list_entry(pl022->queue.next, struct spi_message, queue); | |
1481 | ||
1482 | list_del_init(&pl022->cur_msg->queue); | |
dec5a581 | 1483 | pl022->busy = true; |
b43d65f7 LW |
1484 | spin_unlock_irqrestore(&pl022->queue_lock, flags); |
1485 | ||
1486 | /* Initial message state */ | |
1487 | pl022->cur_msg->state = STATE_START; | |
1488 | pl022->cur_transfer = list_entry(pl022->cur_msg->transfers.next, | |
1489 | struct spi_transfer, | |
1490 | transfer_list); | |
1491 | ||
1492 | /* Setup the SPI using the per chip configuration */ | |
1493 | pl022->cur_chip = spi_get_ctldata(pl022->cur_msg->spi); | |
1494 | /* | |
808f1037 LW |
1495 | * We enable the core voltage and clocks here, then the clocks |
1496 | * and core will be disabled when giveback() is called in each method | |
1497 | * (poll/interrupt/DMA) | |
b43d65f7 | 1498 | */ |
808f1037 | 1499 | amba_vcore_enable(pl022->adev); |
545074fb | 1500 | amba_pclk_enable(pl022->adev); |
b43d65f7 LW |
1501 | clk_enable(pl022->clk); |
1502 | restore_state(pl022); | |
1503 | flush(pl022); | |
1504 | ||
1505 | if (pl022->cur_chip->xfer_type == POLLING_TRANSFER) | |
1506 | do_polling_transfer(pl022); | |
b43d65f7 | 1507 | else |
b1b6b9aa | 1508 | do_interrupt_dma_transfer(pl022); |
b43d65f7 LW |
1509 | } |
1510 | ||
1511 | ||
1512 | static int __init init_queue(struct pl022 *pl022) | |
1513 | { | |
1514 | INIT_LIST_HEAD(&pl022->queue); | |
1515 | spin_lock_init(&pl022->queue_lock); | |
1516 | ||
5e8b821d | 1517 | pl022->running = false; |
dec5a581 | 1518 | pl022->busy = false; |
b43d65f7 LW |
1519 | |
1520 | tasklet_init(&pl022->pump_transfers, | |
1521 | pump_transfers, (unsigned long)pl022); | |
1522 | ||
1523 | INIT_WORK(&pl022->pump_messages, pump_messages); | |
1524 | pl022->workqueue = create_singlethread_workqueue( | |
1525 | dev_name(pl022->master->dev.parent)); | |
1526 | if (pl022->workqueue == NULL) | |
1527 | return -EBUSY; | |
1528 | ||
1529 | return 0; | |
1530 | } | |
1531 | ||
1532 | ||
1533 | static int start_queue(struct pl022 *pl022) | |
1534 | { | |
1535 | unsigned long flags; | |
1536 | ||
1537 | spin_lock_irqsave(&pl022->queue_lock, flags); | |
1538 | ||
5e8b821d | 1539 | if (pl022->running || pl022->busy) { |
b43d65f7 LW |
1540 | spin_unlock_irqrestore(&pl022->queue_lock, flags); |
1541 | return -EBUSY; | |
1542 | } | |
1543 | ||
5e8b821d | 1544 | pl022->running = true; |
b43d65f7 LW |
1545 | pl022->cur_msg = NULL; |
1546 | pl022->cur_transfer = NULL; | |
1547 | pl022->cur_chip = NULL; | |
1548 | spin_unlock_irqrestore(&pl022->queue_lock, flags); | |
1549 | ||
1550 | queue_work(pl022->workqueue, &pl022->pump_messages); | |
1551 | ||
1552 | return 0; | |
1553 | } | |
1554 | ||
1555 | ||
1556 | static int stop_queue(struct pl022 *pl022) | |
1557 | { | |
1558 | unsigned long flags; | |
1559 | unsigned limit = 500; | |
1560 | int status = 0; | |
1561 | ||
1562 | spin_lock_irqsave(&pl022->queue_lock, flags); | |
1563 | ||
1564 | /* This is a bit lame, but is optimized for the common execution path. | |
1565 | * A wait_queue on the pl022->busy could be used, but then the common | |
1566 | * execution path (pump_messages) would be required to call wake_up or | |
1567 | * friends on every SPI message. Do this instead */ | |
850a28ec | 1568 | while ((!list_empty(&pl022->queue) || pl022->busy) && limit--) { |
b43d65f7 LW |
1569 | spin_unlock_irqrestore(&pl022->queue_lock, flags); |
1570 | msleep(10); | |
1571 | spin_lock_irqsave(&pl022->queue_lock, flags); | |
1572 | } | |
1573 | ||
1574 | if (!list_empty(&pl022->queue) || pl022->busy) | |
1575 | status = -EBUSY; | |
5e8b821d LW |
1576 | else |
1577 | pl022->running = false; | |
b43d65f7 LW |
1578 | |
1579 | spin_unlock_irqrestore(&pl022->queue_lock, flags); | |
1580 | ||
1581 | return status; | |
1582 | } | |
1583 | ||
1584 | static int destroy_queue(struct pl022 *pl022) | |
1585 | { | |
1586 | int status; | |
1587 | ||
1588 | status = stop_queue(pl022); | |
1589 | /* we are unloading the module or failing to load (only two calls | |
1590 | * to this routine), and neither call can handle a return value. | |
1591 | * However, destroy_workqueue calls flush_workqueue, and that will | |
1592 | * block until all work is done. If the reason that stop_queue | |
1593 | * timed out is that the work will never finish, then it does no | |
1594 | * good to call destroy_workqueue, so return anyway. */ | |
1595 | if (status != 0) | |
1596 | return status; | |
1597 | ||
1598 | destroy_workqueue(pl022->workqueue); | |
1599 | ||
1600 | return 0; | |
1601 | } | |
1602 | ||
1603 | static int verify_controller_parameters(struct pl022 *pl022, | |
f9d629c7 | 1604 | struct pl022_config_chip const *chip_info) |
b43d65f7 | 1605 | { |
b43d65f7 LW |
1606 | if ((chip_info->iface < SSP_INTERFACE_MOTOROLA_SPI) |
1607 | || (chip_info->iface > SSP_INTERFACE_UNIDIRECTIONAL)) { | |
5a1c98be | 1608 | dev_err(&pl022->adev->dev, |
b43d65f7 LW |
1609 | "interface is configured incorrectly\n"); |
1610 | return -EINVAL; | |
1611 | } | |
1612 | if ((chip_info->iface == SSP_INTERFACE_UNIDIRECTIONAL) && | |
1613 | (!pl022->vendor->unidir)) { | |
5a1c98be | 1614 | dev_err(&pl022->adev->dev, |
b43d65f7 LW |
1615 | "unidirectional mode not supported in this " |
1616 | "hardware version\n"); | |
1617 | return -EINVAL; | |
1618 | } | |
1619 | if ((chip_info->hierarchy != SSP_MASTER) | |
1620 | && (chip_info->hierarchy != SSP_SLAVE)) { | |
5a1c98be | 1621 | dev_err(&pl022->adev->dev, |
b43d65f7 LW |
1622 | "hierarchy is configured incorrectly\n"); |
1623 | return -EINVAL; | |
1624 | } | |
b43d65f7 LW |
1625 | if ((chip_info->com_mode != INTERRUPT_TRANSFER) |
1626 | && (chip_info->com_mode != DMA_TRANSFER) | |
1627 | && (chip_info->com_mode != POLLING_TRANSFER)) { | |
5a1c98be | 1628 | dev_err(&pl022->adev->dev, |
b43d65f7 LW |
1629 | "Communication mode is configured incorrectly\n"); |
1630 | return -EINVAL; | |
1631 | } | |
1632 | if ((chip_info->rx_lev_trig < SSP_RX_1_OR_MORE_ELEM) | |
1633 | || (chip_info->rx_lev_trig > SSP_RX_32_OR_MORE_ELEM)) { | |
5a1c98be | 1634 | dev_err(&pl022->adev->dev, |
b43d65f7 LW |
1635 | "RX FIFO Trigger Level is configured incorrectly\n"); |
1636 | return -EINVAL; | |
1637 | } | |
1638 | if ((chip_info->tx_lev_trig < SSP_TX_1_OR_MORE_EMPTY_LOC) | |
1639 | || (chip_info->tx_lev_trig > SSP_TX_32_OR_MORE_EMPTY_LOC)) { | |
5a1c98be | 1640 | dev_err(&pl022->adev->dev, |
b43d65f7 LW |
1641 | "TX FIFO Trigger Level is configured incorrectly\n"); |
1642 | return -EINVAL; | |
1643 | } | |
b43d65f7 LW |
1644 | if (chip_info->iface == SSP_INTERFACE_NATIONAL_MICROWIRE) { |
1645 | if ((chip_info->ctrl_len < SSP_BITS_4) | |
1646 | || (chip_info->ctrl_len > SSP_BITS_32)) { | |
5a1c98be | 1647 | dev_err(&pl022->adev->dev, |
b43d65f7 LW |
1648 | "CTRL LEN is configured incorrectly\n"); |
1649 | return -EINVAL; | |
1650 | } | |
1651 | if ((chip_info->wait_state != SSP_MWIRE_WAIT_ZERO) | |
1652 | && (chip_info->wait_state != SSP_MWIRE_WAIT_ONE)) { | |
5a1c98be | 1653 | dev_err(&pl022->adev->dev, |
b43d65f7 LW |
1654 | "Wait State is configured incorrectly\n"); |
1655 | return -EINVAL; | |
1656 | } | |
556f4aeb LW |
1657 | /* Half duplex is only available in the ST Micro version */ |
1658 | if (pl022->vendor->extended_cr) { | |
1659 | if ((chip_info->duplex != | |
1660 | SSP_MICROWIRE_CHANNEL_FULL_DUPLEX) | |
1661 | && (chip_info->duplex != | |
4a4fd471 | 1662 | SSP_MICROWIRE_CHANNEL_HALF_DUPLEX)) { |
5a1c98be | 1663 | dev_err(&pl022->adev->dev, |
556f4aeb LW |
1664 | "Microwire duplex mode is configured incorrectly\n"); |
1665 | return -EINVAL; | |
4a4fd471 | 1666 | } |
556f4aeb LW |
1667 | } else { |
1668 | if (chip_info->duplex != SSP_MICROWIRE_CHANNEL_FULL_DUPLEX) | |
5a1c98be | 1669 | dev_err(&pl022->adev->dev, |
556f4aeb LW |
1670 | "Microwire half duplex mode requested," |
1671 | " but this is only available in the" | |
1672 | " ST version of PL022\n"); | |
b43d65f7 LW |
1673 | return -EINVAL; |
1674 | } | |
1675 | } | |
b43d65f7 LW |
1676 | return 0; |
1677 | } | |
1678 | ||
1679 | /** | |
1680 | * pl022_transfer - transfer function registered to SPI master framework | |
1681 | * @spi: spi device which is requesting transfer | |
1682 | * @msg: spi message which is to handled is queued to driver queue | |
1683 | * | |
1684 | * This function is registered to the SPI framework for this SPI master | |
1685 | * controller. It will queue the spi_message in the queue of driver if | |
1686 | * the queue is not stopped and return. | |
1687 | */ | |
1688 | static int pl022_transfer(struct spi_device *spi, struct spi_message *msg) | |
1689 | { | |
1690 | struct pl022 *pl022 = spi_master_get_devdata(spi->master); | |
1691 | unsigned long flags; | |
1692 | ||
1693 | spin_lock_irqsave(&pl022->queue_lock, flags); | |
1694 | ||
5e8b821d | 1695 | if (!pl022->running) { |
b43d65f7 LW |
1696 | spin_unlock_irqrestore(&pl022->queue_lock, flags); |
1697 | return -ESHUTDOWN; | |
1698 | } | |
1699 | msg->actual_length = 0; | |
1700 | msg->status = -EINPROGRESS; | |
1701 | msg->state = STATE_START; | |
1702 | ||
1703 | list_add_tail(&msg->queue, &pl022->queue); | |
5e8b821d | 1704 | if (pl022->running && !pl022->busy) |
b43d65f7 LW |
1705 | queue_work(pl022->workqueue, &pl022->pump_messages); |
1706 | ||
1707 | spin_unlock_irqrestore(&pl022->queue_lock, flags); | |
1708 | return 0; | |
1709 | } | |
1710 | ||
1711 | static int calculate_effective_freq(struct pl022 *pl022, | |
1712 | int freq, | |
1713 | struct ssp_clock_params *clk_freq) | |
1714 | { | |
1715 | /* Lets calculate the frequency parameters */ | |
1716 | u16 cpsdvsr = 2; | |
1717 | u16 scr = 0; | |
1718 | bool freq_found = false; | |
1719 | u32 rate; | |
1720 | u32 max_tclk; | |
1721 | u32 min_tclk; | |
1722 | ||
1723 | rate = clk_get_rate(pl022->clk); | |
1724 | /* cpsdvscr = 2 & scr 0 */ | |
1725 | max_tclk = (rate / (CPSDVR_MIN * (1 + SCR_MIN))); | |
1726 | /* cpsdvsr = 254 & scr = 255 */ | |
1727 | min_tclk = (rate / (CPSDVR_MAX * (1 + SCR_MAX))); | |
1728 | ||
1729 | if ((freq <= max_tclk) && (freq >= min_tclk)) { | |
1730 | while (cpsdvsr <= CPSDVR_MAX && !freq_found) { | |
1731 | while (scr <= SCR_MAX && !freq_found) { | |
1732 | if ((rate / | |
1733 | (cpsdvsr * (1 + scr))) > freq) | |
1734 | scr += 1; | |
1735 | else { | |
1736 | /* | |
1737 | * This bool is made true when | |
1738 | * effective frequency >= | |
1739 | * target frequency is found | |
1740 | */ | |
1741 | freq_found = true; | |
1742 | if ((rate / | |
1743 | (cpsdvsr * (1 + scr))) != freq) { | |
1744 | if (scr == SCR_MIN) { | |
1745 | cpsdvsr -= 2; | |
1746 | scr = SCR_MAX; | |
1747 | } else | |
1748 | scr -= 1; | |
1749 | } | |
1750 | } | |
1751 | } | |
1752 | if (!freq_found) { | |
1753 | cpsdvsr += 2; | |
1754 | scr = SCR_MIN; | |
1755 | } | |
1756 | } | |
1757 | if (cpsdvsr != 0) { | |
1758 | dev_dbg(&pl022->adev->dev, | |
1759 | "SSP Effective Frequency is %u\n", | |
1760 | (rate / (cpsdvsr * (1 + scr)))); | |
1761 | clk_freq->cpsdvsr = (u8) (cpsdvsr & 0xFF); | |
1762 | clk_freq->scr = (u8) (scr & 0xFF); | |
1763 | dev_dbg(&pl022->adev->dev, | |
1764 | "SSP cpsdvsr = %d, scr = %d\n", | |
1765 | clk_freq->cpsdvsr, clk_freq->scr); | |
1766 | } | |
1767 | } else { | |
1768 | dev_err(&pl022->adev->dev, | |
1769 | "controller data is incorrect: out of range frequency"); | |
1770 | return -EINVAL; | |
1771 | } | |
1772 | return 0; | |
1773 | } | |
1774 | ||
f9d629c7 LW |
1775 | |
1776 | /* | |
1777 | * A piece of default chip info unless the platform | |
1778 | * supplies it. | |
1779 | */ | |
1780 | static const struct pl022_config_chip pl022_default_chip_info = { | |
1781 | .com_mode = POLLING_TRANSFER, | |
1782 | .iface = SSP_INTERFACE_MOTOROLA_SPI, | |
1783 | .hierarchy = SSP_SLAVE, | |
1784 | .slave_tx_disable = DO_NOT_DRIVE_TX, | |
1785 | .rx_lev_trig = SSP_RX_1_OR_MORE_ELEM, | |
1786 | .tx_lev_trig = SSP_TX_1_OR_MORE_EMPTY_LOC, | |
1787 | .ctrl_len = SSP_BITS_8, | |
1788 | .wait_state = SSP_MWIRE_WAIT_ZERO, | |
1789 | .duplex = SSP_MICROWIRE_CHANNEL_FULL_DUPLEX, | |
1790 | .cs_control = null_cs_control, | |
1791 | }; | |
1792 | ||
1793 | ||
b43d65f7 LW |
1794 | /** |
1795 | * pl022_setup - setup function registered to SPI master framework | |
1796 | * @spi: spi device which is requesting setup | |
1797 | * | |
1798 | * This function is registered to the SPI framework for this SPI master | |
1799 | * controller. If it is the first time when setup is called by this device, | |
1800 | * this function will initialize the runtime state for this chip and save | |
1801 | * the same in the device structure. Else it will update the runtime info | |
1802 | * with the updated chip info. Nothing is really being written to the | |
1803 | * controller hardware here, that is not done until the actual transfer | |
1804 | * commence. | |
1805 | */ | |
b43d65f7 LW |
1806 | static int pl022_setup(struct spi_device *spi) |
1807 | { | |
f9d629c7 | 1808 | struct pl022_config_chip const *chip_info; |
b43d65f7 | 1809 | struct chip_data *chip; |
94a1b6d8 | 1810 | struct ssp_clock_params clk_freq = {0, }; |
b43d65f7 LW |
1811 | int status = 0; |
1812 | struct pl022 *pl022 = spi_master_get_devdata(spi->master); | |
bde435a9 KW |
1813 | unsigned int bits = spi->bits_per_word; |
1814 | u32 tmp; | |
b43d65f7 LW |
1815 | |
1816 | if (!spi->max_speed_hz) | |
1817 | return -EINVAL; | |
1818 | ||
1819 | /* Get controller_state if one is supplied */ | |
1820 | chip = spi_get_ctldata(spi); | |
1821 | ||
1822 | if (chip == NULL) { | |
1823 | chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); | |
1824 | if (!chip) { | |
1825 | dev_err(&spi->dev, | |
1826 | "cannot allocate controller state\n"); | |
1827 | return -ENOMEM; | |
1828 | } | |
1829 | dev_dbg(&spi->dev, | |
1830 | "allocated memory for controller's runtime state\n"); | |
1831 | } | |
1832 | ||
1833 | /* Get controller data if one is supplied */ | |
1834 | chip_info = spi->controller_data; | |
1835 | ||
1836 | if (chip_info == NULL) { | |
f9d629c7 | 1837 | chip_info = &pl022_default_chip_info; |
b43d65f7 LW |
1838 | /* spi_board_info.controller_data not is supplied */ |
1839 | dev_dbg(&spi->dev, | |
1840 | "using default controller_data settings\n"); | |
f9d629c7 | 1841 | } else |
b43d65f7 LW |
1842 | dev_dbg(&spi->dev, |
1843 | "using user supplied controller_data settings\n"); | |
b43d65f7 LW |
1844 | |
1845 | /* | |
1846 | * We can override with custom divisors, else we use the board | |
1847 | * frequency setting | |
1848 | */ | |
1849 | if ((0 == chip_info->clk_freq.cpsdvsr) | |
1850 | && (0 == chip_info->clk_freq.scr)) { | |
1851 | status = calculate_effective_freq(pl022, | |
1852 | spi->max_speed_hz, | |
f9d629c7 | 1853 | &clk_freq); |
b43d65f7 LW |
1854 | if (status < 0) |
1855 | goto err_config_params; | |
1856 | } else { | |
f9d629c7 LW |
1857 | memcpy(&clk_freq, &chip_info->clk_freq, sizeof(clk_freq)); |
1858 | if ((clk_freq.cpsdvsr % 2) != 0) | |
1859 | clk_freq.cpsdvsr = | |
1860 | clk_freq.cpsdvsr - 1; | |
b43d65f7 | 1861 | } |
f9d629c7 LW |
1862 | if ((clk_freq.cpsdvsr < CPSDVR_MIN) |
1863 | || (clk_freq.cpsdvsr > CPSDVR_MAX)) { | |
1864 | dev_err(&spi->dev, | |
1865 | "cpsdvsr is configured incorrectly\n"); | |
1866 | goto err_config_params; | |
1867 | } | |
1868 | ||
1869 | ||
b43d65f7 LW |
1870 | status = verify_controller_parameters(pl022, chip_info); |
1871 | if (status) { | |
1872 | dev_err(&spi->dev, "controller data is incorrect"); | |
1873 | goto err_config_params; | |
1874 | } | |
f9d629c7 | 1875 | |
b43d65f7 LW |
1876 | /* Now set controller state based on controller data */ |
1877 | chip->xfer_type = chip_info->com_mode; | |
f9d629c7 LW |
1878 | if (!chip_info->cs_control) { |
1879 | chip->cs_control = null_cs_control; | |
1880 | dev_warn(&spi->dev, | |
1881 | "chip select function is NULL for this chip\n"); | |
1882 | } else | |
1883 | chip->cs_control = chip_info->cs_control; | |
b43d65f7 | 1884 | |
bde435a9 KW |
1885 | if (bits <= 3) { |
1886 | /* PL022 doesn't support less than 4-bits */ | |
1887 | status = -ENOTSUPP; | |
1888 | goto err_config_params; | |
1889 | } else if (bits <= 8) { | |
1890 | dev_dbg(&spi->dev, "4 <= n <=8 bits per word\n"); | |
b43d65f7 LW |
1891 | chip->n_bytes = 1; |
1892 | chip->read = READING_U8; | |
1893 | chip->write = WRITING_U8; | |
bde435a9 | 1894 | } else if (bits <= 16) { |
b43d65f7 LW |
1895 | dev_dbg(&spi->dev, "9 <= n <= 16 bits per word\n"); |
1896 | chip->n_bytes = 2; | |
1897 | chip->read = READING_U16; | |
1898 | chip->write = WRITING_U16; | |
1899 | } else { | |
1900 | if (pl022->vendor->max_bpw >= 32) { | |
1901 | dev_dbg(&spi->dev, "17 <= n <= 32 bits per word\n"); | |
1902 | chip->n_bytes = 4; | |
1903 | chip->read = READING_U32; | |
1904 | chip->write = WRITING_U32; | |
1905 | } else { | |
1906 | dev_err(&spi->dev, | |
1907 | "illegal data size for this controller!\n"); | |
1908 | dev_err(&spi->dev, | |
1909 | "a standard pl022 can only handle " | |
1910 | "1 <= n <= 16 bit words\n"); | |
bde435a9 | 1911 | status = -ENOTSUPP; |
b43d65f7 LW |
1912 | goto err_config_params; |
1913 | } | |
1914 | } | |
1915 | ||
1916 | /* Now Initialize all register settings required for this chip */ | |
1917 | chip->cr0 = 0; | |
1918 | chip->cr1 = 0; | |
1919 | chip->dmacr = 0; | |
1920 | chip->cpsr = 0; | |
1921 | if ((chip_info->com_mode == DMA_TRANSFER) | |
1922 | && ((pl022->master_info)->enable_dma)) { | |
b1b6b9aa | 1923 | chip->enable_dma = true; |
b43d65f7 | 1924 | dev_dbg(&spi->dev, "DMA mode set in controller state\n"); |
b43d65f7 LW |
1925 | SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED, |
1926 | SSP_DMACR_MASK_RXDMAE, 0); | |
1927 | SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED, | |
1928 | SSP_DMACR_MASK_TXDMAE, 1); | |
1929 | } else { | |
b1b6b9aa | 1930 | chip->enable_dma = false; |
b43d65f7 LW |
1931 | dev_dbg(&spi->dev, "DMA mode NOT set in controller state\n"); |
1932 | SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED, | |
1933 | SSP_DMACR_MASK_RXDMAE, 0); | |
1934 | SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED, | |
1935 | SSP_DMACR_MASK_TXDMAE, 1); | |
1936 | } | |
1937 | ||
f9d629c7 | 1938 | chip->cpsr = clk_freq.cpsdvsr; |
b43d65f7 | 1939 | |
556f4aeb LW |
1940 | /* Special setup for the ST micro extended control registers */ |
1941 | if (pl022->vendor->extended_cr) { | |
bde435a9 KW |
1942 | u32 etx; |
1943 | ||
781c7b12 LW |
1944 | if (pl022->vendor->pl023) { |
1945 | /* These bits are only in the PL023 */ | |
1946 | SSP_WRITE_BITS(chip->cr1, chip_info->clkdelay, | |
1947 | SSP_CR1_MASK_FBCLKDEL_ST, 13); | |
1948 | } else { | |
1949 | /* These bits are in the PL022 but not PL023 */ | |
1950 | SSP_WRITE_BITS(chip->cr0, chip_info->duplex, | |
1951 | SSP_CR0_MASK_HALFDUP_ST, 5); | |
1952 | SSP_WRITE_BITS(chip->cr0, chip_info->ctrl_len, | |
1953 | SSP_CR0_MASK_CSS_ST, 16); | |
1954 | SSP_WRITE_BITS(chip->cr0, chip_info->iface, | |
1955 | SSP_CR0_MASK_FRF_ST, 21); | |
1956 | SSP_WRITE_BITS(chip->cr1, chip_info->wait_state, | |
1957 | SSP_CR1_MASK_MWAIT_ST, 6); | |
1958 | } | |
bde435a9 | 1959 | SSP_WRITE_BITS(chip->cr0, bits - 1, |
556f4aeb | 1960 | SSP_CR0_MASK_DSS_ST, 0); |
bde435a9 KW |
1961 | |
1962 | if (spi->mode & SPI_LSB_FIRST) { | |
1963 | tmp = SSP_RX_LSB; | |
1964 | etx = SSP_TX_LSB; | |
1965 | } else { | |
1966 | tmp = SSP_RX_MSB; | |
1967 | etx = SSP_TX_MSB; | |
1968 | } | |
1969 | SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_RENDN_ST, 4); | |
1970 | SSP_WRITE_BITS(chip->cr1, etx, SSP_CR1_MASK_TENDN_ST, 5); | |
556f4aeb LW |
1971 | SSP_WRITE_BITS(chip->cr1, chip_info->rx_lev_trig, |
1972 | SSP_CR1_MASK_RXIFLSEL_ST, 7); | |
1973 | SSP_WRITE_BITS(chip->cr1, chip_info->tx_lev_trig, | |
1974 | SSP_CR1_MASK_TXIFLSEL_ST, 10); | |
1975 | } else { | |
bde435a9 | 1976 | SSP_WRITE_BITS(chip->cr0, bits - 1, |
556f4aeb LW |
1977 | SSP_CR0_MASK_DSS, 0); |
1978 | SSP_WRITE_BITS(chip->cr0, chip_info->iface, | |
1979 | SSP_CR0_MASK_FRF, 4); | |
1980 | } | |
bde435a9 | 1981 | |
556f4aeb | 1982 | /* Stuff that is common for all versions */ |
bde435a9 KW |
1983 | if (spi->mode & SPI_CPOL) |
1984 | tmp = SSP_CLK_POL_IDLE_HIGH; | |
1985 | else | |
1986 | tmp = SSP_CLK_POL_IDLE_LOW; | |
1987 | SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPO, 6); | |
1988 | ||
1989 | if (spi->mode & SPI_CPHA) | |
1990 | tmp = SSP_CLK_SECOND_EDGE; | |
1991 | else | |
1992 | tmp = SSP_CLK_FIRST_EDGE; | |
1993 | SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPH, 7); | |
1994 | ||
f9d629c7 | 1995 | SSP_WRITE_BITS(chip->cr0, clk_freq.scr, SSP_CR0_MASK_SCR, 8); |
781c7b12 | 1996 | /* Loopback is available on all versions except PL023 */ |
06fb01fd | 1997 | if (pl022->vendor->loopback) { |
bde435a9 KW |
1998 | if (spi->mode & SPI_LOOP) |
1999 | tmp = LOOPBACK_ENABLED; | |
2000 | else | |
2001 | tmp = LOOPBACK_DISABLED; | |
2002 | SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_LBM, 0); | |
2003 | } | |
b43d65f7 LW |
2004 | SSP_WRITE_BITS(chip->cr1, SSP_DISABLED, SSP_CR1_MASK_SSE, 1); |
2005 | SSP_WRITE_BITS(chip->cr1, chip_info->hierarchy, SSP_CR1_MASK_MS, 2); | |
2006 | SSP_WRITE_BITS(chip->cr1, chip_info->slave_tx_disable, SSP_CR1_MASK_SOD, 3); | |
b43d65f7 LW |
2007 | |
2008 | /* Save controller_state */ | |
2009 | spi_set_ctldata(spi, chip); | |
2010 | return status; | |
2011 | err_config_params: | |
bde435a9 | 2012 | spi_set_ctldata(spi, NULL); |
b43d65f7 LW |
2013 | kfree(chip); |
2014 | return status; | |
2015 | } | |
2016 | ||
2017 | /** | |
2018 | * pl022_cleanup - cleanup function registered to SPI master framework | |
2019 | * @spi: spi device which is requesting cleanup | |
2020 | * | |
2021 | * This function is registered to the SPI framework for this SPI master | |
2022 | * controller. It will free the runtime state of chip. | |
2023 | */ | |
2024 | static void pl022_cleanup(struct spi_device *spi) | |
2025 | { | |
2026 | struct chip_data *chip = spi_get_ctldata(spi); | |
2027 | ||
2028 | spi_set_ctldata(spi, NULL); | |
2029 | kfree(chip); | |
2030 | } | |
2031 | ||
2032 | ||
b4225885 | 2033 | static int __devinit |
aa25afad | 2034 | pl022_probe(struct amba_device *adev, const struct amba_id *id) |
b43d65f7 LW |
2035 | { |
2036 | struct device *dev = &adev->dev; | |
2037 | struct pl022_ssp_controller *platform_info = adev->dev.platform_data; | |
2038 | struct spi_master *master; | |
2039 | struct pl022 *pl022 = NULL; /*Data for this driver */ | |
2040 | int status = 0; | |
2041 | ||
2042 | dev_info(&adev->dev, | |
2043 | "ARM PL022 driver, device ID: 0x%08x\n", adev->periphid); | |
2044 | if (platform_info == NULL) { | |
2045 | dev_err(&adev->dev, "probe - no platform data supplied\n"); | |
2046 | status = -ENODEV; | |
2047 | goto err_no_pdata; | |
2048 | } | |
2049 | ||
2050 | /* Allocate master with space for data */ | |
2051 | master = spi_alloc_master(dev, sizeof(struct pl022)); | |
2052 | if (master == NULL) { | |
2053 | dev_err(&adev->dev, "probe - cannot alloc SPI master\n"); | |
2054 | status = -ENOMEM; | |
2055 | goto err_no_master; | |
2056 | } | |
2057 | ||
2058 | pl022 = spi_master_get_devdata(master); | |
2059 | pl022->master = master; | |
2060 | pl022->master_info = platform_info; | |
2061 | pl022->adev = adev; | |
2062 | pl022->vendor = id->data; | |
2063 | ||
2064 | /* | |
2065 | * Bus Number Which has been Assigned to this SSP controller | |
2066 | * on this board | |
2067 | */ | |
2068 | master->bus_num = platform_info->bus_id; | |
2069 | master->num_chipselect = platform_info->num_chipselect; | |
2070 | master->cleanup = pl022_cleanup; | |
2071 | master->setup = pl022_setup; | |
2072 | master->transfer = pl022_transfer; | |
2073 | ||
bde435a9 KW |
2074 | /* |
2075 | * Supports mode 0-3, loopback, and active low CS. Transfers are | |
2076 | * always MS bit first on the original pl022. | |
2077 | */ | |
2078 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP; | |
2079 | if (pl022->vendor->extended_cr) | |
2080 | master->mode_bits |= SPI_LSB_FIRST; | |
2081 | ||
b43d65f7 LW |
2082 | dev_dbg(&adev->dev, "BUSNO: %d\n", master->bus_num); |
2083 | ||
2084 | status = amba_request_regions(adev, NULL); | |
2085 | if (status) | |
2086 | goto err_no_ioregion; | |
2087 | ||
b1b6b9aa | 2088 | pl022->phybase = adev->res.start; |
b43d65f7 LW |
2089 | pl022->virtbase = ioremap(adev->res.start, resource_size(&adev->res)); |
2090 | if (pl022->virtbase == NULL) { | |
2091 | status = -ENOMEM; | |
2092 | goto err_no_ioremap; | |
2093 | } | |
2094 | printk(KERN_INFO "pl022: mapped registers from 0x%08x to %p\n", | |
2095 | adev->res.start, pl022->virtbase); | |
2096 | ||
2097 | pl022->clk = clk_get(&adev->dev, NULL); | |
2098 | if (IS_ERR(pl022->clk)) { | |
2099 | status = PTR_ERR(pl022->clk); | |
2100 | dev_err(&adev->dev, "could not retrieve SSP/SPI bus clock\n"); | |
2101 | goto err_no_clk; | |
2102 | } | |
2103 | ||
2104 | /* Disable SSP */ | |
b43d65f7 LW |
2105 | writew((readw(SSP_CR1(pl022->virtbase)) & (~SSP_CR1_MASK_SSE)), |
2106 | SSP_CR1(pl022->virtbase)); | |
2107 | load_ssp_default_config(pl022); | |
b43d65f7 LW |
2108 | |
2109 | status = request_irq(adev->irq[0], pl022_interrupt_handler, 0, "pl022", | |
2110 | pl022); | |
2111 | if (status < 0) { | |
2112 | dev_err(&adev->dev, "probe - cannot get IRQ (%d)\n", status); | |
2113 | goto err_no_irq; | |
2114 | } | |
b1b6b9aa LW |
2115 | |
2116 | /* Get DMA channels */ | |
2117 | if (platform_info->enable_dma) { | |
2118 | status = pl022_dma_probe(pl022); | |
2119 | if (status != 0) | |
43c64015 | 2120 | platform_info->enable_dma = 0; |
b1b6b9aa LW |
2121 | } |
2122 | ||
b43d65f7 LW |
2123 | /* Initialize and start queue */ |
2124 | status = init_queue(pl022); | |
2125 | if (status != 0) { | |
2126 | dev_err(&adev->dev, "probe - problem initializing queue\n"); | |
2127 | goto err_init_queue; | |
2128 | } | |
2129 | status = start_queue(pl022); | |
2130 | if (status != 0) { | |
2131 | dev_err(&adev->dev, "probe - problem starting queue\n"); | |
2132 | goto err_start_queue; | |
2133 | } | |
2134 | /* Register with the SPI framework */ | |
2135 | amba_set_drvdata(adev, pl022); | |
2136 | status = spi_register_master(master); | |
2137 | if (status != 0) { | |
2138 | dev_err(&adev->dev, | |
2139 | "probe - problem registering spi master\n"); | |
2140 | goto err_spi_register; | |
2141 | } | |
25985edc | 2142 | dev_dbg(dev, "probe succeeded\n"); |
808f1037 LW |
2143 | /* |
2144 | * Disable the silicon block pclk and any voltage domain and just | |
2145 | * power it up and clock it when it's needed | |
2146 | */ | |
545074fb | 2147 | amba_pclk_disable(adev); |
808f1037 | 2148 | amba_vcore_disable(adev); |
b43d65f7 LW |
2149 | return 0; |
2150 | ||
2151 | err_spi_register: | |
2152 | err_start_queue: | |
2153 | err_init_queue: | |
2154 | destroy_queue(pl022); | |
b1b6b9aa | 2155 | pl022_dma_remove(pl022); |
b43d65f7 LW |
2156 | free_irq(adev->irq[0], pl022); |
2157 | err_no_irq: | |
2158 | clk_put(pl022->clk); | |
2159 | err_no_clk: | |
2160 | iounmap(pl022->virtbase); | |
2161 | err_no_ioremap: | |
2162 | amba_release_regions(adev); | |
2163 | err_no_ioregion: | |
2164 | spi_master_put(master); | |
2165 | err_no_master: | |
2166 | err_no_pdata: | |
2167 | return status; | |
2168 | } | |
2169 | ||
b4225885 | 2170 | static int __devexit |
b43d65f7 LW |
2171 | pl022_remove(struct amba_device *adev) |
2172 | { | |
2173 | struct pl022 *pl022 = amba_get_drvdata(adev); | |
2174 | int status = 0; | |
2175 | if (!pl022) | |
2176 | return 0; | |
2177 | ||
2178 | /* Remove the queue */ | |
2179 | status = destroy_queue(pl022); | |
2180 | if (status != 0) { | |
2181 | dev_err(&adev->dev, | |
2182 | "queue remove failed (%d)\n", status); | |
2183 | return status; | |
2184 | } | |
2185 | load_ssp_default_config(pl022); | |
b1b6b9aa | 2186 | pl022_dma_remove(pl022); |
b43d65f7 LW |
2187 | free_irq(adev->irq[0], pl022); |
2188 | clk_disable(pl022->clk); | |
2189 | clk_put(pl022->clk); | |
2190 | iounmap(pl022->virtbase); | |
2191 | amba_release_regions(adev); | |
2192 | tasklet_disable(&pl022->pump_transfers); | |
2193 | spi_unregister_master(pl022->master); | |
2194 | spi_master_put(pl022->master); | |
2195 | amba_set_drvdata(adev, NULL); | |
25985edc | 2196 | dev_dbg(&adev->dev, "remove succeeded\n"); |
b43d65f7 LW |
2197 | return 0; |
2198 | } | |
2199 | ||
2200 | #ifdef CONFIG_PM | |
2201 | static int pl022_suspend(struct amba_device *adev, pm_message_t state) | |
2202 | { | |
2203 | struct pl022 *pl022 = amba_get_drvdata(adev); | |
2204 | int status = 0; | |
2205 | ||
2206 | status = stop_queue(pl022); | |
2207 | if (status) { | |
2208 | dev_warn(&adev->dev, "suspend cannot stop queue\n"); | |
2209 | return status; | |
2210 | } | |
2211 | ||
808f1037 | 2212 | amba_vcore_enable(adev); |
545074fb | 2213 | amba_pclk_enable(adev); |
b43d65f7 | 2214 | load_ssp_default_config(pl022); |
545074fb | 2215 | amba_pclk_disable(adev); |
808f1037 | 2216 | amba_vcore_disable(adev); |
b43d65f7 LW |
2217 | dev_dbg(&adev->dev, "suspended\n"); |
2218 | return 0; | |
2219 | } | |
2220 | ||
2221 | static int pl022_resume(struct amba_device *adev) | |
2222 | { | |
2223 | struct pl022 *pl022 = amba_get_drvdata(adev); | |
2224 | int status = 0; | |
2225 | ||
2226 | /* Start the queue running */ | |
2227 | status = start_queue(pl022); | |
2228 | if (status) | |
2229 | dev_err(&adev->dev, "problem starting queue (%d)\n", status); | |
2230 | else | |
2231 | dev_dbg(&adev->dev, "resumed\n"); | |
2232 | ||
2233 | return status; | |
2234 | } | |
2235 | #else | |
2236 | #define pl022_suspend NULL | |
2237 | #define pl022_resume NULL | |
2238 | #endif /* CONFIG_PM */ | |
2239 | ||
2240 | static struct vendor_data vendor_arm = { | |
2241 | .fifodepth = 8, | |
2242 | .max_bpw = 16, | |
2243 | .unidir = false, | |
556f4aeb | 2244 | .extended_cr = false, |
781c7b12 | 2245 | .pl023 = false, |
06fb01fd | 2246 | .loopback = true, |
b43d65f7 LW |
2247 | }; |
2248 | ||
2249 | ||
2250 | static struct vendor_data vendor_st = { | |
2251 | .fifodepth = 32, | |
2252 | .max_bpw = 32, | |
2253 | .unidir = false, | |
556f4aeb | 2254 | .extended_cr = true, |
781c7b12 | 2255 | .pl023 = false, |
06fb01fd | 2256 | .loopback = true, |
781c7b12 LW |
2257 | }; |
2258 | ||
2259 | static struct vendor_data vendor_st_pl023 = { | |
2260 | .fifodepth = 32, | |
2261 | .max_bpw = 32, | |
2262 | .unidir = false, | |
2263 | .extended_cr = true, | |
2264 | .pl023 = true, | |
06fb01fd PL |
2265 | .loopback = false, |
2266 | }; | |
2267 | ||
2268 | static struct vendor_data vendor_db5500_pl023 = { | |
2269 | .fifodepth = 32, | |
2270 | .max_bpw = 32, | |
2271 | .unidir = false, | |
2272 | .extended_cr = true, | |
2273 | .pl023 = true, | |
2274 | .loopback = true, | |
b43d65f7 LW |
2275 | }; |
2276 | ||
2277 | static struct amba_id pl022_ids[] = { | |
2278 | { | |
2279 | /* | |
2280 | * ARM PL022 variant, this has a 16bit wide | |
2281 | * and 8 locations deep TX/RX FIFO | |
2282 | */ | |
2283 | .id = 0x00041022, | |
2284 | .mask = 0x000fffff, | |
2285 | .data = &vendor_arm, | |
2286 | }, | |
2287 | { | |
2288 | /* | |
2289 | * ST Micro derivative, this has 32bit wide | |
2290 | * and 32 locations deep TX/RX FIFO | |
2291 | */ | |
e89e04fc | 2292 | .id = 0x01080022, |
b43d65f7 LW |
2293 | .mask = 0xffffffff, |
2294 | .data = &vendor_st, | |
2295 | }, | |
781c7b12 LW |
2296 | { |
2297 | /* | |
2298 | * ST-Ericsson derivative "PL023" (this is not | |
2299 | * an official ARM number), this is a PL022 SSP block | |
2300 | * stripped to SPI mode only, it has 32bit wide | |
2301 | * and 32 locations deep TX/RX FIFO but no extended | |
2302 | * CR0/CR1 register | |
2303 | */ | |
2304 | .id = 0x00080023, | |
2305 | .mask = 0xffffffff, | |
2306 | .data = &vendor_st_pl023, | |
2307 | }, | |
06fb01fd PL |
2308 | { |
2309 | .id = 0x10080023, | |
2310 | .mask = 0xffffffff, | |
2311 | .data = &vendor_db5500_pl023, | |
2312 | }, | |
b43d65f7 LW |
2313 | { 0, 0 }, |
2314 | }; | |
2315 | ||
2316 | static struct amba_driver pl022_driver = { | |
2317 | .drv = { | |
2318 | .name = "ssp-pl022", | |
2319 | }, | |
2320 | .id_table = pl022_ids, | |
2321 | .probe = pl022_probe, | |
b4225885 | 2322 | .remove = __devexit_p(pl022_remove), |
b43d65f7 LW |
2323 | .suspend = pl022_suspend, |
2324 | .resume = pl022_resume, | |
2325 | }; | |
2326 | ||
2327 | ||
2328 | static int __init pl022_init(void) | |
2329 | { | |
2330 | return amba_driver_register(&pl022_driver); | |
2331 | } | |
2332 | ||
25c8e03b | 2333 | subsys_initcall(pl022_init); |
b43d65f7 LW |
2334 | |
2335 | static void __exit pl022_exit(void) | |
2336 | { | |
2337 | amba_driver_unregister(&pl022_driver); | |
2338 | } | |
2339 | ||
2340 | module_exit(pl022_exit); | |
2341 | ||
2342 | MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>"); | |
2343 | MODULE_DESCRIPTION("PL022 SSP Controller Driver"); | |
2344 | MODULE_LICENSE("GPL"); |