blob: 81847c9a7586f4b9acc6edfc2f099e9abab9532f [file] [log] [blame]
Linus Walleijb43d65f2009-06-09 08:11:42 +01001/*
Linus Walleijb43d65f2009-06-09 08:11:42 +01002 * A driver for the ARM PL022 PrimeCell SSP/SPI bus master.
3 *
4 * Copyright (C) 2008-2009 ST-Ericsson AB
5 * Copyright (C) 2006 STMicroelectronics Pvt. Ltd.
6 *
7 * Author: Linus Walleij <linus.walleij@stericsson.com>
8 *
9 * Initial version inspired by:
10 * linux-2.6.17-rc3-mm1/drivers/spi/pxa2xx_spi.c
11 * Initial adoption to PL022 by:
12 * Sachin Verma <sachin.verma@st.com>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 */
24
Linus Walleijb43d65f2009-06-09 08:11:42 +010025#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/device.h>
28#include <linux/ioport.h>
29#include <linux/errno.h>
30#include <linux/interrupt.h>
31#include <linux/spi/spi.h>
Chris Blair14af60b2012-02-02 13:59:34 +010032#include <linux/kthread.h>
Linus Walleijb43d65f2009-06-09 08:11:42 +010033#include <linux/delay.h>
34#include <linux/clk.h>
35#include <linux/err.h>
36#include <linux/amba/bus.h>
37#include <linux/amba/pl022.h>
38#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Linus Walleijb1b6b9a2010-09-29 17:31:35 +090040#include <linux/dmaengine.h>
41#include <linux/dma-mapping.h>
42#include <linux/scatterlist.h>
Rabin Vincentbcda6ff2011-06-16 10:14:40 +020043#include <linux/pm_runtime.h>
Chris Blair14af60b2012-02-02 13:59:34 +010044#include <linux/sched.h>
Linus Walleijb43d65f2009-06-09 08:11:42 +010045
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 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000102#define SSP_CR0_MASK_DSS (0x0FUL << 0)
103#define SSP_CR0_MASK_FRF (0x3UL << 4)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100104#define SSP_CR0_MASK_SPO (0x1UL << 6)
105#define SSP_CR0_MASK_SPH (0x1UL << 7)
106#define SSP_CR0_MASK_SCR (0xFFUL << 8)
Linus Walleij556f4ae2010-05-05 09:28:15 +0000107
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
Linus Walleijb43d65f2009-06-09 08:11:42 +0100117/*
118 * SSP Control Register 0 - SSP_CR1
119 */
120#define SSP_CR1_MASK_LBM (0x1UL << 0)
121#define SSP_CR1_MASK_SSE (0x1UL << 1)
122#define SSP_CR1_MASK_MS (0x1UL << 2)
123#define SSP_CR1_MASK_SOD (0x1UL << 3)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100124
125/*
Linus Walleij556f4ae2010-05-05 09:28:15 +0000126 * The ST version of this block adds some bits
127 * in SSP_CR1
Linus Walleijb43d65f2009-06-09 08:11:42 +0100128 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000129#define SSP_CR1_MASK_RENDN_ST (0x1UL << 4)
130#define SSP_CR1_MASK_TENDN_ST (0x1UL << 5)
131#define SSP_CR1_MASK_MWAIT_ST (0x1UL << 6)
132#define SSP_CR1_MASK_RXIFLSEL_ST (0x7UL << 7)
133#define SSP_CR1_MASK_TXIFLSEL_ST (0x7UL << 10)
Linus Walleij781c7b12010-05-07 08:40:53 +0000134/* This one is only in the PL023 variant */
135#define SSP_CR1_MASK_FBCLKDEL_ST (0x7UL << 13)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100136
137/*
138 * SSP Status Register - SSP_SR
139 */
140#define SSP_SR_MASK_TFE (0x1UL << 0) /* Transmit FIFO empty */
141#define SSP_SR_MASK_TNF (0x1UL << 1) /* Transmit FIFO not full */
142#define SSP_SR_MASK_RNE (0x1UL << 2) /* Receive FIFO not empty */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000143#define SSP_SR_MASK_RFF (0x1UL << 3) /* Receive FIFO full */
Linus Walleijb43d65f2009-06-09 08:11:42 +0100144#define SSP_SR_MASK_BSY (0x1UL << 4) /* Busy Flag */
145
146/*
147 * SSP Clock Prescale Register - SSP_CPSR
148 */
149#define SSP_CPSR_MASK_CPSDVSR (0xFFUL << 0)
150
151/*
152 * SSP Interrupt Mask Set/Clear Register - SSP_IMSC
153 */
154#define SSP_IMSC_MASK_RORIM (0x1UL << 0) /* Receive Overrun Interrupt mask */
155#define SSP_IMSC_MASK_RTIM (0x1UL << 1) /* Receive timeout Interrupt mask */
156#define SSP_IMSC_MASK_RXIM (0x1UL << 2) /* Receive FIFO Interrupt mask */
157#define SSP_IMSC_MASK_TXIM (0x1UL << 3) /* Transmit FIFO Interrupt mask */
158
159/*
160 * SSP Raw Interrupt Status Register - SSP_RIS
161 */
162/* Receive Overrun Raw Interrupt status */
163#define SSP_RIS_MASK_RORRIS (0x1UL << 0)
164/* Receive Timeout Raw Interrupt status */
165#define SSP_RIS_MASK_RTRIS (0x1UL << 1)
166/* Receive FIFO Raw Interrupt status */
167#define SSP_RIS_MASK_RXRIS (0x1UL << 2)
168/* Transmit FIFO Raw Interrupt status */
169#define SSP_RIS_MASK_TXRIS (0x1UL << 3)
170
171/*
172 * SSP Masked Interrupt Status Register - SSP_MIS
173 */
174/* Receive Overrun Masked Interrupt status */
175#define SSP_MIS_MASK_RORMIS (0x1UL << 0)
176/* Receive Timeout Masked Interrupt status */
177#define SSP_MIS_MASK_RTMIS (0x1UL << 1)
178/* Receive FIFO Masked Interrupt status */
179#define SSP_MIS_MASK_RXMIS (0x1UL << 2)
180/* Transmit FIFO Masked Interrupt status */
181#define SSP_MIS_MASK_TXMIS (0x1UL << 3)
182
183/*
184 * SSP Interrupt Clear Register - SSP_ICR
185 */
186/* Receive Overrun Raw Clear Interrupt bit */
187#define SSP_ICR_MASK_RORIC (0x1UL << 0)
188/* Receive Timeout Clear Interrupt bit */
189#define SSP_ICR_MASK_RTIC (0x1UL << 1)
190
191/*
192 * SSP DMA Control Register - SSP_DMACR
193 */
194/* Receive DMA Enable bit */
195#define SSP_DMACR_MASK_RXDMAE (0x1UL << 0)
196/* Transmit DMA Enable bit */
197#define SSP_DMACR_MASK_TXDMAE (0x1UL << 1)
198
199/*
200 * SSP Integration Test control Register - SSP_ITCR
201 */
202#define SSP_ITCR_MASK_ITEN (0x1UL << 0)
203#define SSP_ITCR_MASK_TESTFIFO (0x1UL << 1)
204
205/*
206 * SSP Integration Test Input Register - SSP_ITIP
207 */
208#define ITIP_MASK_SSPRXD (0x1UL << 0)
209#define ITIP_MASK_SSPFSSIN (0x1UL << 1)
210#define ITIP_MASK_SSPCLKIN (0x1UL << 2)
211#define ITIP_MASK_RXDMAC (0x1UL << 3)
212#define ITIP_MASK_TXDMAC (0x1UL << 4)
213#define ITIP_MASK_SSPTXDIN (0x1UL << 5)
214
215/*
216 * SSP Integration Test output Register - SSP_ITOP
217 */
218#define ITOP_MASK_SSPTXD (0x1UL << 0)
219#define ITOP_MASK_SSPFSSOUT (0x1UL << 1)
220#define ITOP_MASK_SSPCLKOUT (0x1UL << 2)
221#define ITOP_MASK_SSPOEn (0x1UL << 3)
222#define ITOP_MASK_SSPCTLOEn (0x1UL << 4)
223#define ITOP_MASK_RORINTR (0x1UL << 5)
224#define ITOP_MASK_RTINTR (0x1UL << 6)
225#define ITOP_MASK_RXINTR (0x1UL << 7)
226#define ITOP_MASK_TXINTR (0x1UL << 8)
227#define ITOP_MASK_INTR (0x1UL << 9)
228#define ITOP_MASK_RXDMABREQ (0x1UL << 10)
229#define ITOP_MASK_RXDMASREQ (0x1UL << 11)
230#define ITOP_MASK_TXDMABREQ (0x1UL << 12)
231#define ITOP_MASK_TXDMASREQ (0x1UL << 13)
232
233/*
234 * SSP Test Data Register - SSP_TDR
235 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000236#define TDR_MASK_TESTDATA (0xFFFFFFFF)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100237
238/*
239 * Message State
240 * we use the spi_message.state (void *) pointer to
241 * hold a single state value, that's why all this
242 * (void *) casting is done here.
243 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000244#define STATE_START ((void *) 0)
245#define STATE_RUNNING ((void *) 1)
246#define STATE_DONE ((void *) 2)
247#define STATE_ERROR ((void *) -1)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100248
249/*
Linus Walleijb43d65f2009-06-09 08:11:42 +0100250 * SSP State - Whether Enabled or Disabled
251 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000252#define SSP_DISABLED (0)
253#define SSP_ENABLED (1)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100254
255/*
256 * SSP DMA State - Whether DMA Enabled or Disabled
257 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000258#define SSP_DMA_DISABLED (0)
259#define SSP_DMA_ENABLED (1)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100260
261/*
262 * SSP Clock Defaults
263 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000264#define SSP_DEFAULT_CLKRATE 0x2
265#define SSP_DEFAULT_PRESCALE 0x40
Linus Walleijb43d65f2009-06-09 08:11:42 +0100266
267/*
268 * SSP Clock Parameter ranges
269 */
270#define CPSDVR_MIN 0x02
271#define CPSDVR_MAX 0xFE
272#define SCR_MIN 0x00
273#define SCR_MAX 0xFF
274
275/*
276 * SSP Interrupt related Macros
277 */
278#define DEFAULT_SSP_REG_IMSC 0x0UL
279#define DISABLE_ALL_INTERRUPTS DEFAULT_SSP_REG_IMSC
280#define ENABLE_ALL_INTERRUPTS (~DEFAULT_SSP_REG_IMSC)
281
282#define CLEAR_ALL_INTERRUPTS 0x3
283
Magnus Templinga18c2662011-05-19 18:05:34 +0200284#define SPI_POLLING_TIMEOUT 1000
285
Linus Walleijb43d65f2009-06-09 08:11:42 +0100286/*
287 * The type of reading going on on this chip
288 */
289enum ssp_reading {
290 READING_NULL,
291 READING_U8,
292 READING_U16,
293 READING_U32
294};
295
296/**
297 * The type of writing going on on this chip
298 */
299enum ssp_writing {
300 WRITING_NULL,
301 WRITING_U8,
302 WRITING_U16,
303 WRITING_U32
304};
305
306/**
307 * struct vendor_data - vendor-specific config parameters
308 * for PL022 derivates
309 * @fifodepth: depth of FIFOs (both)
310 * @max_bpw: maximum number of bits per word
311 * @unidir: supports unidirection transfers
Linus Walleij556f4ae2010-05-05 09:28:15 +0000312 * @extended_cr: 32 bit wide control register 0 with extra
313 * features and extra features in CR1 as found in the ST variants
Linus Walleij781c7b12010-05-07 08:40:53 +0000314 * @pl023: supports a subset of the ST extensions called "PL023"
Linus Walleijb43d65f2009-06-09 08:11:42 +0100315 */
316struct vendor_data {
317 int fifodepth;
318 int max_bpw;
319 bool unidir;
Linus Walleij556f4ae2010-05-05 09:28:15 +0000320 bool extended_cr;
Linus Walleij781c7b12010-05-07 08:40:53 +0000321 bool pl023;
Philippe Langlais06fb01f2011-03-23 11:05:16 +0100322 bool loopback;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100323};
324
325/**
326 * struct pl022 - This is the private SSP driver data structure
327 * @adev: AMBA device model hookup
Linus Walleij12e8b322011-02-08 13:03:55 +0100328 * @vendor: vendor data for the IP block
329 * @phybase: the physical memory where the SSP device resides
330 * @virtbase: the virtual memory where the SSP is mapped
331 * @clk: outgoing clock "SPICLK" for the SPI bus
Linus Walleijb43d65f2009-06-09 08:11:42 +0100332 * @master: SPI framework hookup
333 * @master_info: controller-specific data from machine setup
Chris Blair14af60b2012-02-02 13:59:34 +0100334 * @kworker: thread struct for message pump
335 * @kworker_task: pointer to task for message pump kworker thread
336 * @pump_messages: work struct for scheduling work to the message pump
Linus Walleij12e8b322011-02-08 13:03:55 +0100337 * @queue_lock: spinlock to syncronise access to message queue
338 * @queue: message queue
Chris Blair14af60b2012-02-02 13:59:34 +0100339 * @busy: message pump is busy
340 * @running: message pump is running
Linus Walleijb43d65f2009-06-09 08:11:42 +0100341 * @pump_transfers: Tasklet used in Interrupt Transfer mode
342 * @cur_msg: Pointer to current spi_message being processed
343 * @cur_transfer: Pointer to current spi_transfer
344 * @cur_chip: pointer to current clients chip(assigned from controller_state)
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530345 * @next_msg_cs_active: the next message in the queue has been examined
346 * and it was found that it uses the same chip select as the previous
347 * message, so we left it active after the previous transfer, and it's
348 * active already.
Linus Walleijb43d65f2009-06-09 08:11:42 +0100349 * @tx: current position in TX buffer to be read
350 * @tx_end: end position in TX buffer to be read
351 * @rx: current position in RX buffer to be written
352 * @rx_end: end position in RX buffer to be written
Linus Walleij12e8b322011-02-08 13:03:55 +0100353 * @read: the type of read currently going on
354 * @write: the type of write currently going on
355 * @exp_fifo_level: expected FIFO level
356 * @dma_rx_channel: optional channel for RX DMA
357 * @dma_tx_channel: optional channel for TX DMA
358 * @sgt_rx: scattertable for the RX transfer
359 * @sgt_tx: scattertable for the TX transfer
360 * @dummypage: a dummy page used for driving data on the bus with DMA
Linus Walleijb43d65f2009-06-09 08:11:42 +0100361 */
362struct pl022 {
363 struct amba_device *adev;
364 struct vendor_data *vendor;
365 resource_size_t phybase;
366 void __iomem *virtbase;
367 struct clk *clk;
368 struct spi_master *master;
369 struct pl022_ssp_controller *master_info;
Chris Blair14af60b2012-02-02 13:59:34 +0100370 /* Driver message pump */
371 struct kthread_worker kworker;
372 struct task_struct *kworker_task;
373 struct kthread_work pump_messages;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100374 spinlock_t queue_lock;
375 struct list_head queue;
Linus Walleijdec5a582010-12-22 23:13:48 +0100376 bool busy;
Linus Walleij5e8b8212010-12-22 23:13:59 +0100377 bool running;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100378 /* Message transfer pump */
379 struct tasklet_struct pump_transfers;
380 struct spi_message *cur_msg;
381 struct spi_transfer *cur_transfer;
382 struct chip_data *cur_chip;
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530383 bool next_msg_cs_active;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100384 void *tx;
385 void *tx_end;
386 void *rx;
387 void *rx_end;
388 enum ssp_reading read;
389 enum ssp_writing write;
Linus Walleijfc054752010-01-22 13:53:30 +0100390 u32 exp_fifo_level;
Linus Walleij083be3f2011-06-16 10:14:28 +0200391 enum ssp_rx_level_trig rx_lev_trig;
392 enum ssp_tx_level_trig tx_lev_trig;
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900393 /* DMA settings */
394#ifdef CONFIG_DMA_ENGINE
395 struct dma_chan *dma_rx_channel;
396 struct dma_chan *dma_tx_channel;
397 struct sg_table sgt_rx;
398 struct sg_table sgt_tx;
399 char *dummypage;
400#endif
Linus Walleijb43d65f2009-06-09 08:11:42 +0100401};
402
403/**
404 * struct chip_data - To maintain runtime state of SSP for each client chip
Linus Walleij556f4ae2010-05-05 09:28:15 +0000405 * @cr0: Value of control register CR0 of SSP - on later ST variants this
406 * register is 32 bits wide rather than just 16
Linus Walleijb43d65f2009-06-09 08:11:42 +0100407 * @cr1: Value of control register CR1 of SSP
408 * @dmacr: Value of DMA control Register of SSP
409 * @cpsr: Value of Clock prescale register
410 * @n_bytes: how many bytes(power of 2) reqd for a given data width of client
411 * @enable_dma: Whether to enable DMA or not
Linus Walleijb43d65f2009-06-09 08:11:42 +0100412 * @read: function ptr to be used to read when doing xfer for this chip
Linus Walleij12e8b322011-02-08 13:03:55 +0100413 * @write: function ptr to be used to write when doing xfer for this chip
Linus Walleijb43d65f2009-06-09 08:11:42 +0100414 * @cs_control: chip select callback provided by chip
415 * @xfer_type: polling/interrupt/DMA
416 *
417 * Runtime state of the SSP controller, maintained per chip,
418 * This would be set according to the current message that would be served
419 */
420struct chip_data {
Linus Walleij556f4ae2010-05-05 09:28:15 +0000421 u32 cr0;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100422 u16 cr1;
423 u16 dmacr;
424 u16 cpsr;
425 u8 n_bytes;
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900426 bool enable_dma;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100427 enum ssp_reading read;
428 enum ssp_writing write;
429 void (*cs_control) (u32 command);
430 int xfer_type;
431};
432
433/**
434 * null_cs_control - Dummy chip select function
435 * @command: select/delect the chip
436 *
437 * If no chip select function is provided by client this is used as dummy
438 * chip select
439 */
440static void null_cs_control(u32 command)
441{
442 pr_debug("pl022: dummy chip select control, CS=0x%x\n", command);
443}
444
445/**
446 * giveback - current spi_message is over, schedule next message and call
447 * callback of this message. Assumes that caller already
448 * set message->status; dma and pio irqs are blocked
449 * @pl022: SSP driver private data structure
450 */
451static void giveback(struct pl022 *pl022)
452{
453 struct spi_transfer *last_transfer;
454 unsigned long flags;
455 struct spi_message *msg;
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530456 pl022->next_msg_cs_active = false;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100457
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530458 last_transfer = list_entry(pl022->cur_msg->transfers.prev,
Linus Walleijb43d65f2009-06-09 08:11:42 +0100459 struct spi_transfer,
460 transfer_list);
461
462 /* Delay if requested before any change in chip select */
463 if (last_transfer->delay_usecs)
464 /*
465 * FIXME: This runs in interrupt context.
466 * Is this really smart?
467 */
468 udelay(last_transfer->delay_usecs);
469
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530470 if (!last_transfer->cs_change) {
Linus Walleijb43d65f2009-06-09 08:11:42 +0100471 struct spi_message *next_msg;
472
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530473 /*
474 * cs_change was not set. We can keep the chip select
475 * enabled if there is message in the queue and it is
476 * for the same spi device.
Linus Walleijb43d65f2009-06-09 08:11:42 +0100477 *
478 * We cannot postpone this until pump_messages, because
479 * after calling msg->complete (below) the driver that
480 * sent the current message could be unloaded, which
481 * could invalidate the cs_control() callback...
482 */
483
484 /* get a pointer to the next message, if any */
485 spin_lock_irqsave(&pl022->queue_lock, flags);
486 if (list_empty(&pl022->queue))
487 next_msg = NULL;
488 else
489 next_msg = list_entry(pl022->queue.next,
490 struct spi_message, queue);
491 spin_unlock_irqrestore(&pl022->queue_lock, flags);
492
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530493 /*
494 * see if the next and current messages point
495 * to the same spi device.
Linus Walleijb43d65f2009-06-09 08:11:42 +0100496 */
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530497 if (next_msg && next_msg->spi != pl022->cur_msg->spi)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100498 next_msg = NULL;
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530499 if (!next_msg || pl022->cur_msg->state == STATE_ERROR)
500 pl022->cur_chip->cs_control(SSP_CHIP_DESELECT);
501 else
502 pl022->next_msg_cs_active = true;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100503 }
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530504
505 spin_lock_irqsave(&pl022->queue_lock, flags);
506 msg = pl022->cur_msg;
507 pl022->cur_msg = NULL;
508 pl022->cur_transfer = NULL;
509 pl022->cur_chip = NULL;
Chris Blair14af60b2012-02-02 13:59:34 +0100510 queue_kthread_work(&pl022->kworker, &pl022->pump_messages);
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530511 spin_unlock_irqrestore(&pl022->queue_lock, flags);
512
Linus Walleijb43d65f2009-06-09 08:11:42 +0100513 msg->state = NULL;
514 if (msg->complete)
515 msg->complete(msg->context);
Linus Walleijb43d65f2009-06-09 08:11:42 +0100516}
517
518/**
519 * flush - flush the FIFO to reach a clean state
520 * @pl022: SSP driver private data structure
521 */
522static int flush(struct pl022 *pl022)
523{
524 unsigned long limit = loops_per_jiffy << 1;
525
526 dev_dbg(&pl022->adev->dev, "flush\n");
527 do {
528 while (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
529 readw(SSP_DR(pl022->virtbase));
530 } while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_BSY) && limit--);
Linus Walleijfc054752010-01-22 13:53:30 +0100531
532 pl022->exp_fifo_level = 0;
533
Linus Walleijb43d65f2009-06-09 08:11:42 +0100534 return limit;
535}
536
537/**
538 * restore_state - Load configuration of current chip
539 * @pl022: SSP driver private data structure
540 */
541static void restore_state(struct pl022 *pl022)
542{
543 struct chip_data *chip = pl022->cur_chip;
544
Linus Walleij556f4ae2010-05-05 09:28:15 +0000545 if (pl022->vendor->extended_cr)
546 writel(chip->cr0, SSP_CR0(pl022->virtbase));
547 else
548 writew(chip->cr0, SSP_CR0(pl022->virtbase));
Linus Walleijb43d65f2009-06-09 08:11:42 +0100549 writew(chip->cr1, SSP_CR1(pl022->virtbase));
550 writew(chip->dmacr, SSP_DMACR(pl022->virtbase));
551 writew(chip->cpsr, SSP_CPSR(pl022->virtbase));
552 writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
553 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
554}
555
Linus Walleijb43d65f2009-06-09 08:11:42 +0100556/*
557 * Default SSP Register Values
558 */
559#define DEFAULT_SSP_REG_CR0 ( \
560 GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS, 0) | \
Linus Walleij556f4ae2010-05-05 09:28:15 +0000561 GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF, 4) | \
Linus Walleijb43d65f2009-06-09 08:11:42 +0100562 GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
Linus Walleijee2b8052009-08-15 15:12:05 +0100563 GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
Linus Walleij556f4ae2010-05-05 09:28:15 +0000564 GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
565)
566
567/* ST versions have slightly different bit layout */
568#define DEFAULT_SSP_REG_CR0_ST ( \
569 GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
570 GEN_MASK_BITS(SSP_MICROWIRE_CHANNEL_FULL_DUPLEX, SSP_CR0_MASK_HALFDUP_ST, 5) | \
571 GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
572 GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
573 GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) | \
574 GEN_MASK_BITS(SSP_BITS_8, SSP_CR0_MASK_CSS_ST, 16) | \
575 GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF_ST, 21) \
Linus Walleijb43d65f2009-06-09 08:11:42 +0100576)
577
Linus Walleij781c7b12010-05-07 08:40:53 +0000578/* The PL023 version is slightly different again */
579#define DEFAULT_SSP_REG_CR0_ST_PL023 ( \
580 GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
581 GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
582 GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
583 GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
584)
585
Linus Walleijb43d65f2009-06-09 08:11:42 +0100586#define DEFAULT_SSP_REG_CR1 ( \
587 GEN_MASK_BITS(LOOPBACK_DISABLED, SSP_CR1_MASK_LBM, 0) | \
588 GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
589 GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
Linus Walleij556f4ae2010-05-05 09:28:15 +0000590 GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) \
Linus Walleijb43d65f2009-06-09 08:11:42 +0100591)
592
Linus Walleij556f4ae2010-05-05 09:28:15 +0000593/* ST versions extend this register to use all 16 bits */
594#define DEFAULT_SSP_REG_CR1_ST ( \
595 DEFAULT_SSP_REG_CR1 | \
596 GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
597 GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
598 GEN_MASK_BITS(SSP_MWIRE_WAIT_ZERO, SSP_CR1_MASK_MWAIT_ST, 6) |\
599 GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
600 GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) \
601)
602
Linus Walleij781c7b12010-05-07 08:40:53 +0000603/*
604 * The PL023 variant has further differences: no loopback mode, no microwire
605 * support, and a new clock feedback delay setting.
606 */
607#define DEFAULT_SSP_REG_CR1_ST_PL023 ( \
608 GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
609 GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
610 GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) | \
611 GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
612 GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
613 GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
614 GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) | \
615 GEN_MASK_BITS(SSP_FEEDBACK_CLK_DELAY_NONE, SSP_CR1_MASK_FBCLKDEL_ST, 13) \
616)
Linus Walleij556f4ae2010-05-05 09:28:15 +0000617
Linus Walleijb43d65f2009-06-09 08:11:42 +0100618#define DEFAULT_SSP_REG_CPSR ( \
Linus Walleij556f4ae2010-05-05 09:28:15 +0000619 GEN_MASK_BITS(SSP_DEFAULT_PRESCALE, SSP_CPSR_MASK_CPSDVSR, 0) \
Linus Walleijb43d65f2009-06-09 08:11:42 +0100620)
621
622#define DEFAULT_SSP_REG_DMACR (\
623 GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_RXDMAE, 0) | \
624 GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_TXDMAE, 1) \
625)
626
Linus Walleij781c7b12010-05-07 08:40:53 +0000627/**
628 * load_ssp_default_config - Load default configuration for SSP
629 * @pl022: SSP driver private data structure
630 */
Linus Walleijb43d65f2009-06-09 08:11:42 +0100631static void load_ssp_default_config(struct pl022 *pl022)
632{
Linus Walleij781c7b12010-05-07 08:40:53 +0000633 if (pl022->vendor->pl023) {
634 writel(DEFAULT_SSP_REG_CR0_ST_PL023, SSP_CR0(pl022->virtbase));
635 writew(DEFAULT_SSP_REG_CR1_ST_PL023, SSP_CR1(pl022->virtbase));
636 } else if (pl022->vendor->extended_cr) {
Linus Walleij556f4ae2010-05-05 09:28:15 +0000637 writel(DEFAULT_SSP_REG_CR0_ST, SSP_CR0(pl022->virtbase));
638 writew(DEFAULT_SSP_REG_CR1_ST, SSP_CR1(pl022->virtbase));
639 } else {
640 writew(DEFAULT_SSP_REG_CR0, SSP_CR0(pl022->virtbase));
641 writew(DEFAULT_SSP_REG_CR1, SSP_CR1(pl022->virtbase));
642 }
Linus Walleijb43d65f2009-06-09 08:11:42 +0100643 writew(DEFAULT_SSP_REG_DMACR, SSP_DMACR(pl022->virtbase));
644 writew(DEFAULT_SSP_REG_CPSR, SSP_CPSR(pl022->virtbase));
645 writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
646 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
647}
648
649/**
650 * This will write to TX and read from RX according to the parameters
651 * set in pl022.
652 */
653static void readwriter(struct pl022 *pl022)
654{
655
656 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300657 * The FIFO depth is different between primecell variants.
Linus Walleijb43d65f2009-06-09 08:11:42 +0100658 * I believe filling in too much in the FIFO might cause
659 * errons in 8bit wide transfers on ARM variants (just 8 words
660 * FIFO, means only 8x8 = 64 bits in FIFO) at least.
661 *
Linus Walleijfc054752010-01-22 13:53:30 +0100662 * To prevent this issue, the TX FIFO is only filled to the
663 * unused RX FIFO fill length, regardless of what the TX
664 * FIFO status flag indicates.
Linus Walleijb43d65f2009-06-09 08:11:42 +0100665 */
666 dev_dbg(&pl022->adev->dev,
667 "%s, rx: %p, rxend: %p, tx: %p, txend: %p\n",
668 __func__, pl022->rx, pl022->rx_end, pl022->tx, pl022->tx_end);
669
670 /* Read as much as you can */
671 while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
672 && (pl022->rx < pl022->rx_end)) {
673 switch (pl022->read) {
674 case READING_NULL:
675 readw(SSP_DR(pl022->virtbase));
676 break;
677 case READING_U8:
678 *(u8 *) (pl022->rx) =
679 readw(SSP_DR(pl022->virtbase)) & 0xFFU;
680 break;
681 case READING_U16:
682 *(u16 *) (pl022->rx) =
683 (u16) readw(SSP_DR(pl022->virtbase));
684 break;
685 case READING_U32:
686 *(u32 *) (pl022->rx) =
687 readl(SSP_DR(pl022->virtbase));
688 break;
689 }
690 pl022->rx += (pl022->cur_chip->n_bytes);
Linus Walleijfc054752010-01-22 13:53:30 +0100691 pl022->exp_fifo_level--;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100692 }
693 /*
Linus Walleijfc054752010-01-22 13:53:30 +0100694 * Write as much as possible up to the RX FIFO size
Linus Walleijb43d65f2009-06-09 08:11:42 +0100695 */
Linus Walleijfc054752010-01-22 13:53:30 +0100696 while ((pl022->exp_fifo_level < pl022->vendor->fifodepth)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100697 && (pl022->tx < pl022->tx_end)) {
698 switch (pl022->write) {
699 case WRITING_NULL:
700 writew(0x0, SSP_DR(pl022->virtbase));
701 break;
702 case WRITING_U8:
703 writew(*(u8 *) (pl022->tx), SSP_DR(pl022->virtbase));
704 break;
705 case WRITING_U16:
706 writew((*(u16 *) (pl022->tx)), SSP_DR(pl022->virtbase));
707 break;
708 case WRITING_U32:
709 writel(*(u32 *) (pl022->tx), SSP_DR(pl022->virtbase));
710 break;
711 }
712 pl022->tx += (pl022->cur_chip->n_bytes);
Linus Walleijfc054752010-01-22 13:53:30 +0100713 pl022->exp_fifo_level++;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100714 /*
715 * This inner reader takes care of things appearing in the RX
716 * FIFO as we're transmitting. This will happen a lot since the
717 * clock starts running when you put things into the TX FIFO,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300718 * and then things are continuously clocked into the RX FIFO.
Linus Walleijb43d65f2009-06-09 08:11:42 +0100719 */
720 while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
721 && (pl022->rx < pl022->rx_end)) {
722 switch (pl022->read) {
723 case READING_NULL:
724 readw(SSP_DR(pl022->virtbase));
725 break;
726 case READING_U8:
727 *(u8 *) (pl022->rx) =
728 readw(SSP_DR(pl022->virtbase)) & 0xFFU;
729 break;
730 case READING_U16:
731 *(u16 *) (pl022->rx) =
732 (u16) readw(SSP_DR(pl022->virtbase));
733 break;
734 case READING_U32:
735 *(u32 *) (pl022->rx) =
736 readl(SSP_DR(pl022->virtbase));
737 break;
738 }
739 pl022->rx += (pl022->cur_chip->n_bytes);
Linus Walleijfc054752010-01-22 13:53:30 +0100740 pl022->exp_fifo_level--;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100741 }
742 }
743 /*
744 * When we exit here the TX FIFO should be full and the RX FIFO
745 * should be empty
746 */
747}
748
Linus Walleijb43d65f2009-06-09 08:11:42 +0100749/**
750 * next_transfer - Move to the Next transfer in the current spi message
751 * @pl022: SSP driver private data structure
752 *
753 * This function moves though the linked list of spi transfers in the
754 * current spi message and returns with the state of current spi
755 * message i.e whether its last transfer is done(STATE_DONE) or
756 * Next transfer is ready(STATE_RUNNING)
757 */
758static void *next_transfer(struct pl022 *pl022)
759{
760 struct spi_message *msg = pl022->cur_msg;
761 struct spi_transfer *trans = pl022->cur_transfer;
762
763 /* Move to next transfer */
764 if (trans->transfer_list.next != &msg->transfers) {
765 pl022->cur_transfer =
766 list_entry(trans->transfer_list.next,
767 struct spi_transfer, transfer_list);
768 return STATE_RUNNING;
769 }
770 return STATE_DONE;
771}
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900772
773/*
774 * This DMA functionality is only compiled in if we have
775 * access to the generic DMA devices/DMA engine.
776 */
777#ifdef CONFIG_DMA_ENGINE
778static void unmap_free_dma_scatter(struct pl022 *pl022)
779{
780 /* Unmap and free the SG tables */
Linus Walleijb7298892010-12-22 23:13:07 +0100781 dma_unmap_sg(pl022->dma_tx_channel->device->dev, pl022->sgt_tx.sgl,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900782 pl022->sgt_tx.nents, DMA_TO_DEVICE);
Linus Walleijb7298892010-12-22 23:13:07 +0100783 dma_unmap_sg(pl022->dma_rx_channel->device->dev, pl022->sgt_rx.sgl,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900784 pl022->sgt_rx.nents, DMA_FROM_DEVICE);
785 sg_free_table(&pl022->sgt_rx);
786 sg_free_table(&pl022->sgt_tx);
787}
788
789static void dma_callback(void *data)
790{
791 struct pl022 *pl022 = data;
792 struct spi_message *msg = pl022->cur_msg;
793
794 BUG_ON(!pl022->sgt_rx.sgl);
795
796#ifdef VERBOSE_DEBUG
797 /*
798 * Optionally dump out buffers to inspect contents, this is
799 * good if you want to convince yourself that the loopback
800 * read/write contents are the same, when adopting to a new
801 * DMA engine.
802 */
803 {
804 struct scatterlist *sg;
805 unsigned int i;
806
807 dma_sync_sg_for_cpu(&pl022->adev->dev,
808 pl022->sgt_rx.sgl,
809 pl022->sgt_rx.nents,
810 DMA_FROM_DEVICE);
811
812 for_each_sg(pl022->sgt_rx.sgl, sg, pl022->sgt_rx.nents, i) {
813 dev_dbg(&pl022->adev->dev, "SPI RX SG ENTRY: %d", i);
814 print_hex_dump(KERN_ERR, "SPI RX: ",
815 DUMP_PREFIX_OFFSET,
816 16,
817 1,
818 sg_virt(sg),
819 sg_dma_len(sg),
820 1);
821 }
822 for_each_sg(pl022->sgt_tx.sgl, sg, pl022->sgt_tx.nents, i) {
823 dev_dbg(&pl022->adev->dev, "SPI TX SG ENTRY: %d", i);
824 print_hex_dump(KERN_ERR, "SPI TX: ",
825 DUMP_PREFIX_OFFSET,
826 16,
827 1,
828 sg_virt(sg),
829 sg_dma_len(sg),
830 1);
831 }
832 }
833#endif
834
835 unmap_free_dma_scatter(pl022);
836
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300837 /* Update total bytes transferred */
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900838 msg->actual_length += pl022->cur_transfer->len;
839 if (pl022->cur_transfer->cs_change)
840 pl022->cur_chip->
841 cs_control(SSP_CHIP_DESELECT);
842
843 /* Move to next transfer */
844 msg->state = next_transfer(pl022);
845 tasklet_schedule(&pl022->pump_transfers);
846}
847
848static void setup_dma_scatter(struct pl022 *pl022,
849 void *buffer,
850 unsigned int length,
851 struct sg_table *sgtab)
852{
853 struct scatterlist *sg;
854 int bytesleft = length;
855 void *bufp = buffer;
856 int mapbytes;
857 int i;
858
859 if (buffer) {
860 for_each_sg(sgtab->sgl, sg, sgtab->nents, i) {
861 /*
862 * If there are less bytes left than what fits
863 * in the current page (plus page alignment offset)
864 * we just feed in this, else we stuff in as much
865 * as we can.
866 */
867 if (bytesleft < (PAGE_SIZE - offset_in_page(bufp)))
868 mapbytes = bytesleft;
869 else
870 mapbytes = PAGE_SIZE - offset_in_page(bufp);
871 sg_set_page(sg, virt_to_page(bufp),
872 mapbytes, offset_in_page(bufp));
873 bufp += mapbytes;
874 bytesleft -= mapbytes;
875 dev_dbg(&pl022->adev->dev,
876 "set RX/TX target page @ %p, %d bytes, %d left\n",
877 bufp, mapbytes, bytesleft);
878 }
879 } else {
880 /* Map the dummy buffer on every page */
881 for_each_sg(sgtab->sgl, sg, sgtab->nents, i) {
882 if (bytesleft < PAGE_SIZE)
883 mapbytes = bytesleft;
884 else
885 mapbytes = PAGE_SIZE;
886 sg_set_page(sg, virt_to_page(pl022->dummypage),
887 mapbytes, 0);
888 bytesleft -= mapbytes;
889 dev_dbg(&pl022->adev->dev,
890 "set RX/TX to dummy page %d bytes, %d left\n",
891 mapbytes, bytesleft);
892
893 }
894 }
895 BUG_ON(bytesleft);
896}
897
898/**
899 * configure_dma - configures the channels for the next transfer
900 * @pl022: SSP driver's private data structure
901 */
902static int configure_dma(struct pl022 *pl022)
903{
904 struct dma_slave_config rx_conf = {
905 .src_addr = SSP_DR(pl022->phybase),
Vinod Koula485df42011-10-14 10:47:38 +0530906 .direction = DMA_DEV_TO_MEM,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900907 };
908 struct dma_slave_config tx_conf = {
909 .dst_addr = SSP_DR(pl022->phybase),
Vinod Koula485df42011-10-14 10:47:38 +0530910 .direction = DMA_MEM_TO_DEV,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900911 };
912 unsigned int pages;
913 int ret;
Linus Walleij082086f2010-12-22 23:13:37 +0100914 int rx_sglen, tx_sglen;
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900915 struct dma_chan *rxchan = pl022->dma_rx_channel;
916 struct dma_chan *txchan = pl022->dma_tx_channel;
917 struct dma_async_tx_descriptor *rxdesc;
918 struct dma_async_tx_descriptor *txdesc;
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900919
920 /* Check that the channels are available */
921 if (!rxchan || !txchan)
922 return -ENODEV;
923
Linus Walleij083be3f2011-06-16 10:14:28 +0200924 /*
925 * If supplied, the DMA burstsize should equal the FIFO trigger level.
926 * Notice that the DMA engine uses one-to-one mapping. Since we can
927 * not trigger on 2 elements this needs explicit mapping rather than
928 * calculation.
929 */
930 switch (pl022->rx_lev_trig) {
931 case SSP_RX_1_OR_MORE_ELEM:
932 rx_conf.src_maxburst = 1;
933 break;
934 case SSP_RX_4_OR_MORE_ELEM:
935 rx_conf.src_maxburst = 4;
936 break;
937 case SSP_RX_8_OR_MORE_ELEM:
938 rx_conf.src_maxburst = 8;
939 break;
940 case SSP_RX_16_OR_MORE_ELEM:
941 rx_conf.src_maxburst = 16;
942 break;
943 case SSP_RX_32_OR_MORE_ELEM:
944 rx_conf.src_maxburst = 32;
945 break;
946 default:
947 rx_conf.src_maxburst = pl022->vendor->fifodepth >> 1;
948 break;
949 }
950
951 switch (pl022->tx_lev_trig) {
952 case SSP_TX_1_OR_MORE_EMPTY_LOC:
953 tx_conf.dst_maxburst = 1;
954 break;
955 case SSP_TX_4_OR_MORE_EMPTY_LOC:
956 tx_conf.dst_maxburst = 4;
957 break;
958 case SSP_TX_8_OR_MORE_EMPTY_LOC:
959 tx_conf.dst_maxburst = 8;
960 break;
961 case SSP_TX_16_OR_MORE_EMPTY_LOC:
962 tx_conf.dst_maxburst = 16;
963 break;
964 case SSP_TX_32_OR_MORE_EMPTY_LOC:
965 tx_conf.dst_maxburst = 32;
966 break;
967 default:
968 tx_conf.dst_maxburst = pl022->vendor->fifodepth >> 1;
969 break;
970 }
971
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900972 switch (pl022->read) {
973 case READING_NULL:
974 /* Use the same as for writing */
975 rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
976 break;
977 case READING_U8:
978 rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
979 break;
980 case READING_U16:
981 rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
982 break;
983 case READING_U32:
984 rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
985 break;
986 }
987
988 switch (pl022->write) {
989 case WRITING_NULL:
990 /* Use the same as for reading */
991 tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
992 break;
993 case WRITING_U8:
994 tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
995 break;
996 case WRITING_U16:
997 tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
998 break;
999 case WRITING_U32:
Joe Perchesbc3f67a2010-11-14 19:04:47 -08001000 tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001001 break;
1002 }
1003
1004 /* SPI pecularity: we need to read and write the same width */
1005 if (rx_conf.src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
1006 rx_conf.src_addr_width = tx_conf.dst_addr_width;
1007 if (tx_conf.dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
1008 tx_conf.dst_addr_width = rx_conf.src_addr_width;
1009 BUG_ON(rx_conf.src_addr_width != tx_conf.dst_addr_width);
1010
Linus Walleijecd442f2011-02-08 13:03:12 +01001011 dmaengine_slave_config(rxchan, &rx_conf);
1012 dmaengine_slave_config(txchan, &tx_conf);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001013
1014 /* Create sglists for the transfers */
Viresh Kumarb1815652011-08-10 17:12:11 +05301015 pages = DIV_ROUND_UP(pl022->cur_transfer->len, PAGE_SIZE);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001016 dev_dbg(&pl022->adev->dev, "using %d pages for transfer\n", pages);
1017
Viresh Kumar538a18d2011-08-10 14:20:55 +05301018 ret = sg_alloc_table(&pl022->sgt_rx, pages, GFP_ATOMIC);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001019 if (ret)
1020 goto err_alloc_rx_sg;
1021
Viresh Kumar538a18d2011-08-10 14:20:55 +05301022 ret = sg_alloc_table(&pl022->sgt_tx, pages, GFP_ATOMIC);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001023 if (ret)
1024 goto err_alloc_tx_sg;
1025
1026 /* Fill in the scatterlists for the RX+TX buffers */
1027 setup_dma_scatter(pl022, pl022->rx,
1028 pl022->cur_transfer->len, &pl022->sgt_rx);
1029 setup_dma_scatter(pl022, pl022->tx,
1030 pl022->cur_transfer->len, &pl022->sgt_tx);
1031
1032 /* Map DMA buffers */
Linus Walleij082086f2010-12-22 23:13:37 +01001033 rx_sglen = dma_map_sg(rxchan->device->dev, pl022->sgt_rx.sgl,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001034 pl022->sgt_rx.nents, DMA_FROM_DEVICE);
Linus Walleij082086f2010-12-22 23:13:37 +01001035 if (!rx_sglen)
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001036 goto err_rx_sgmap;
1037
Linus Walleij082086f2010-12-22 23:13:37 +01001038 tx_sglen = dma_map_sg(txchan->device->dev, pl022->sgt_tx.sgl,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001039 pl022->sgt_tx.nents, DMA_TO_DEVICE);
Linus Walleij082086f2010-12-22 23:13:37 +01001040 if (!tx_sglen)
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001041 goto err_tx_sgmap;
1042
1043 /* Send both scatterlists */
1044 rxdesc = rxchan->device->device_prep_slave_sg(rxchan,
1045 pl022->sgt_rx.sgl,
Linus Walleij082086f2010-12-22 23:13:37 +01001046 rx_sglen,
Vinod Koula485df42011-10-14 10:47:38 +05301047 DMA_DEV_TO_MEM,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001048 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1049 if (!rxdesc)
1050 goto err_rxdesc;
1051
1052 txdesc = txchan->device->device_prep_slave_sg(txchan,
1053 pl022->sgt_tx.sgl,
Linus Walleij082086f2010-12-22 23:13:37 +01001054 tx_sglen,
Vinod Koula485df42011-10-14 10:47:38 +05301055 DMA_MEM_TO_DEV,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001056 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1057 if (!txdesc)
1058 goto err_txdesc;
1059
1060 /* Put the callback on the RX transfer only, that should finish last */
1061 rxdesc->callback = dma_callback;
1062 rxdesc->callback_param = pl022;
1063
1064 /* Submit and fire RX and TX with TX last so we're ready to read! */
Linus Walleijecd442f2011-02-08 13:03:12 +01001065 dmaengine_submit(rxdesc);
1066 dmaengine_submit(txdesc);
1067 dma_async_issue_pending(rxchan);
1068 dma_async_issue_pending(txchan);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001069
1070 return 0;
1071
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001072err_txdesc:
Linus Walleijecd442f2011-02-08 13:03:12 +01001073 dmaengine_terminate_all(txchan);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001074err_rxdesc:
Linus Walleijecd442f2011-02-08 13:03:12 +01001075 dmaengine_terminate_all(rxchan);
Linus Walleijb7298892010-12-22 23:13:07 +01001076 dma_unmap_sg(txchan->device->dev, pl022->sgt_tx.sgl,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001077 pl022->sgt_tx.nents, DMA_TO_DEVICE);
1078err_tx_sgmap:
Linus Walleijb7298892010-12-22 23:13:07 +01001079 dma_unmap_sg(rxchan->device->dev, pl022->sgt_rx.sgl,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001080 pl022->sgt_tx.nents, DMA_FROM_DEVICE);
1081err_rx_sgmap:
1082 sg_free_table(&pl022->sgt_tx);
1083err_alloc_tx_sg:
1084 sg_free_table(&pl022->sgt_rx);
1085err_alloc_rx_sg:
1086 return -ENOMEM;
1087}
1088
1089static int __init pl022_dma_probe(struct pl022 *pl022)
1090{
1091 dma_cap_mask_t mask;
1092
1093 /* Try to acquire a generic DMA engine slave channel */
1094 dma_cap_zero(mask);
1095 dma_cap_set(DMA_SLAVE, mask);
1096 /*
1097 * We need both RX and TX channels to do DMA, else do none
1098 * of them.
1099 */
1100 pl022->dma_rx_channel = dma_request_channel(mask,
1101 pl022->master_info->dma_filter,
1102 pl022->master_info->dma_rx_param);
1103 if (!pl022->dma_rx_channel) {
Viresh Kumar43c64012011-05-16 09:40:10 +05301104 dev_dbg(&pl022->adev->dev, "no RX DMA channel!\n");
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001105 goto err_no_rxchan;
1106 }
1107
1108 pl022->dma_tx_channel = dma_request_channel(mask,
1109 pl022->master_info->dma_filter,
1110 pl022->master_info->dma_tx_param);
1111 if (!pl022->dma_tx_channel) {
Viresh Kumar43c64012011-05-16 09:40:10 +05301112 dev_dbg(&pl022->adev->dev, "no TX DMA channel!\n");
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001113 goto err_no_txchan;
1114 }
1115
1116 pl022->dummypage = kmalloc(PAGE_SIZE, GFP_KERNEL);
1117 if (!pl022->dummypage) {
Viresh Kumar43c64012011-05-16 09:40:10 +05301118 dev_dbg(&pl022->adev->dev, "no DMA dummypage!\n");
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001119 goto err_no_dummypage;
1120 }
1121
1122 dev_info(&pl022->adev->dev, "setup for DMA on RX %s, TX %s\n",
1123 dma_chan_name(pl022->dma_rx_channel),
1124 dma_chan_name(pl022->dma_tx_channel));
1125
1126 return 0;
1127
1128err_no_dummypage:
1129 dma_release_channel(pl022->dma_tx_channel);
1130err_no_txchan:
1131 dma_release_channel(pl022->dma_rx_channel);
1132 pl022->dma_rx_channel = NULL;
1133err_no_rxchan:
Viresh Kumar43c64012011-05-16 09:40:10 +05301134 dev_err(&pl022->adev->dev,
1135 "Failed to work in dma mode, work without dma!\n");
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001136 return -ENODEV;
1137}
1138
1139static void terminate_dma(struct pl022 *pl022)
1140{
1141 struct dma_chan *rxchan = pl022->dma_rx_channel;
1142 struct dma_chan *txchan = pl022->dma_tx_channel;
1143
Linus Walleijecd442f2011-02-08 13:03:12 +01001144 dmaengine_terminate_all(rxchan);
1145 dmaengine_terminate_all(txchan);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001146 unmap_free_dma_scatter(pl022);
1147}
1148
1149static void pl022_dma_remove(struct pl022 *pl022)
1150{
1151 if (pl022->busy)
1152 terminate_dma(pl022);
1153 if (pl022->dma_tx_channel)
1154 dma_release_channel(pl022->dma_tx_channel);
1155 if (pl022->dma_rx_channel)
1156 dma_release_channel(pl022->dma_rx_channel);
1157 kfree(pl022->dummypage);
1158}
1159
1160#else
1161static inline int configure_dma(struct pl022 *pl022)
1162{
1163 return -ENODEV;
1164}
1165
1166static inline int pl022_dma_probe(struct pl022 *pl022)
1167{
1168 return 0;
1169}
1170
1171static inline void pl022_dma_remove(struct pl022 *pl022)
1172{
1173}
1174#endif
1175
Linus Walleijb43d65f2009-06-09 08:11:42 +01001176/**
1177 * pl022_interrupt_handler - Interrupt handler for SSP controller
1178 *
1179 * This function handles interrupts generated for an interrupt based transfer.
1180 * If a receive overrun (ROR) interrupt is there then we disable SSP, flag the
1181 * current message's state as STATE_ERROR and schedule the tasklet
1182 * pump_transfers which will do the postprocessing of the current message by
1183 * calling giveback(). Otherwise it reads data from RX FIFO till there is no
1184 * more data, and writes data in TX FIFO till it is not full. If we complete
1185 * the transfer we move to the next transfer and schedule the tasklet.
1186 */
1187static irqreturn_t pl022_interrupt_handler(int irq, void *dev_id)
1188{
1189 struct pl022 *pl022 = dev_id;
1190 struct spi_message *msg = pl022->cur_msg;
1191 u16 irq_status = 0;
1192 u16 flag = 0;
1193
1194 if (unlikely(!msg)) {
1195 dev_err(&pl022->adev->dev,
1196 "bad message state in interrupt handler");
1197 /* Never fail */
1198 return IRQ_HANDLED;
1199 }
1200
1201 /* Read the Interrupt Status Register */
1202 irq_status = readw(SSP_MIS(pl022->virtbase));
1203
1204 if (unlikely(!irq_status))
1205 return IRQ_NONE;
1206
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001207 /*
1208 * This handles the FIFO interrupts, the timeout
1209 * interrupts are flatly ignored, they cannot be
1210 * trusted.
1211 */
Linus Walleijb43d65f2009-06-09 08:11:42 +01001212 if (unlikely(irq_status & SSP_MIS_MASK_RORMIS)) {
1213 /*
1214 * Overrun interrupt - bail out since our Data has been
1215 * corrupted
1216 */
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001217 dev_err(&pl022->adev->dev, "FIFO overrun\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01001218 if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RFF)
1219 dev_err(&pl022->adev->dev,
1220 "RXFIFO is full\n");
1221 if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_TNF)
1222 dev_err(&pl022->adev->dev,
1223 "TXFIFO is full\n");
1224
1225 /*
1226 * Disable and clear interrupts, disable SSP,
1227 * mark message with bad status so it can be
1228 * retried.
1229 */
1230 writew(DISABLE_ALL_INTERRUPTS,
1231 SSP_IMSC(pl022->virtbase));
1232 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
1233 writew((readw(SSP_CR1(pl022->virtbase)) &
1234 (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
1235 msg->state = STATE_ERROR;
1236
1237 /* Schedule message queue handler */
1238 tasklet_schedule(&pl022->pump_transfers);
1239 return IRQ_HANDLED;
1240 }
1241
1242 readwriter(pl022);
1243
1244 if ((pl022->tx == pl022->tx_end) && (flag == 0)) {
1245 flag = 1;
Chris Blair172289df2011-06-04 07:57:47 +01001246 /* Disable Transmit interrupt, enable receive interrupt */
1247 writew((readw(SSP_IMSC(pl022->virtbase)) &
1248 ~SSP_IMSC_MASK_TXIM) | SSP_IMSC_MASK_RXIM,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001249 SSP_IMSC(pl022->virtbase));
1250 }
1251
1252 /*
1253 * Since all transactions must write as much as shall be read,
1254 * we can conclude the entire transaction once RX is complete.
1255 * At this point, all TX will always be finished.
1256 */
1257 if (pl022->rx >= pl022->rx_end) {
1258 writew(DISABLE_ALL_INTERRUPTS,
1259 SSP_IMSC(pl022->virtbase));
1260 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
1261 if (unlikely(pl022->rx > pl022->rx_end)) {
1262 dev_warn(&pl022->adev->dev, "read %u surplus "
1263 "bytes (did you request an odd "
1264 "number of bytes on a 16bit bus?)\n",
1265 (u32) (pl022->rx - pl022->rx_end));
1266 }
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001267 /* Update total bytes transferred */
Linus Walleijb43d65f2009-06-09 08:11:42 +01001268 msg->actual_length += pl022->cur_transfer->len;
1269 if (pl022->cur_transfer->cs_change)
1270 pl022->cur_chip->
1271 cs_control(SSP_CHIP_DESELECT);
1272 /* Move to next transfer */
1273 msg->state = next_transfer(pl022);
1274 tasklet_schedule(&pl022->pump_transfers);
1275 return IRQ_HANDLED;
1276 }
1277
1278 return IRQ_HANDLED;
1279}
1280
1281/**
1282 * This sets up the pointers to memory for the next message to
1283 * send out on the SPI bus.
1284 */
1285static int set_up_next_transfer(struct pl022 *pl022,
1286 struct spi_transfer *transfer)
1287{
1288 int residue;
1289
1290 /* Sanity check the message for this bus width */
1291 residue = pl022->cur_transfer->len % pl022->cur_chip->n_bytes;
1292 if (unlikely(residue != 0)) {
1293 dev_err(&pl022->adev->dev,
1294 "message of %u bytes to transmit but the current "
1295 "chip bus has a data width of %u bytes!\n",
1296 pl022->cur_transfer->len,
1297 pl022->cur_chip->n_bytes);
1298 dev_err(&pl022->adev->dev, "skipping this message\n");
1299 return -EIO;
1300 }
1301 pl022->tx = (void *)transfer->tx_buf;
1302 pl022->tx_end = pl022->tx + pl022->cur_transfer->len;
1303 pl022->rx = (void *)transfer->rx_buf;
1304 pl022->rx_end = pl022->rx + pl022->cur_transfer->len;
1305 pl022->write =
1306 pl022->tx ? pl022->cur_chip->write : WRITING_NULL;
1307 pl022->read = pl022->rx ? pl022->cur_chip->read : READING_NULL;
1308 return 0;
1309}
1310
1311/**
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001312 * pump_transfers - Tasklet function which schedules next transfer
1313 * when running in interrupt or DMA transfer mode.
Linus Walleijb43d65f2009-06-09 08:11:42 +01001314 * @data: SSP driver private data structure
1315 *
1316 */
1317static void pump_transfers(unsigned long data)
1318{
1319 struct pl022 *pl022 = (struct pl022 *) data;
1320 struct spi_message *message = NULL;
1321 struct spi_transfer *transfer = NULL;
1322 struct spi_transfer *previous = NULL;
1323
1324 /* Get current state information */
1325 message = pl022->cur_msg;
1326 transfer = pl022->cur_transfer;
1327
1328 /* Handle for abort */
1329 if (message->state == STATE_ERROR) {
1330 message->status = -EIO;
1331 giveback(pl022);
1332 return;
1333 }
1334
1335 /* Handle end of message */
1336 if (message->state == STATE_DONE) {
1337 message->status = 0;
1338 giveback(pl022);
1339 return;
1340 }
1341
1342 /* Delay if requested at end of transfer before CS change */
1343 if (message->state == STATE_RUNNING) {
1344 previous = list_entry(transfer->transfer_list.prev,
1345 struct spi_transfer,
1346 transfer_list);
1347 if (previous->delay_usecs)
1348 /*
1349 * FIXME: This runs in interrupt context.
1350 * Is this really smart?
1351 */
1352 udelay(previous->delay_usecs);
1353
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +05301354 /* Reselect chip select only if cs_change was requested */
Linus Walleijb43d65f2009-06-09 08:11:42 +01001355 if (previous->cs_change)
1356 pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
1357 } else {
1358 /* STATE_START */
1359 message->state = STATE_RUNNING;
1360 }
1361
1362 if (set_up_next_transfer(pl022, transfer)) {
1363 message->state = STATE_ERROR;
1364 message->status = -EIO;
1365 giveback(pl022);
1366 return;
1367 }
1368 /* Flush the FIFOs and let's go! */
1369 flush(pl022);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001370
1371 if (pl022->cur_chip->enable_dma) {
1372 if (configure_dma(pl022)) {
1373 dev_dbg(&pl022->adev->dev,
1374 "configuration of DMA failed, fall back to interrupt mode\n");
1375 goto err_config_dma;
1376 }
1377 return;
1378 }
1379
1380err_config_dma:
Chris Blair172289df2011-06-04 07:57:47 +01001381 /* enable all interrupts except RX */
1382 writew(ENABLE_ALL_INTERRUPTS & ~SSP_IMSC_MASK_RXIM, SSP_IMSC(pl022->virtbase));
Linus Walleijb43d65f2009-06-09 08:11:42 +01001383}
1384
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001385static void do_interrupt_dma_transfer(struct pl022 *pl022)
Linus Walleijb43d65f2009-06-09 08:11:42 +01001386{
Chris Blair172289df2011-06-04 07:57:47 +01001387 /*
1388 * Default is to enable all interrupts except RX -
1389 * this will be enabled once TX is complete
1390 */
1391 u32 irqflags = ENABLE_ALL_INTERRUPTS & ~SSP_IMSC_MASK_RXIM;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001392
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +05301393 /* Enable target chip, if not already active */
1394 if (!pl022->next_msg_cs_active)
1395 pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
1396
Linus Walleijb43d65f2009-06-09 08:11:42 +01001397 if (set_up_next_transfer(pl022, pl022->cur_transfer)) {
1398 /* Error path */
1399 pl022->cur_msg->state = STATE_ERROR;
1400 pl022->cur_msg->status = -EIO;
1401 giveback(pl022);
1402 return;
1403 }
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001404 /* If we're using DMA, set up DMA here */
1405 if (pl022->cur_chip->enable_dma) {
1406 /* Configure DMA transfer */
1407 if (configure_dma(pl022)) {
1408 dev_dbg(&pl022->adev->dev,
1409 "configuration of DMA failed, fall back to interrupt mode\n");
1410 goto err_config_dma;
1411 }
1412 /* Disable interrupts in DMA mode, IRQ from DMA controller */
1413 irqflags = DISABLE_ALL_INTERRUPTS;
1414 }
1415err_config_dma:
Linus Walleijb43d65f2009-06-09 08:11:42 +01001416 /* Enable SSP, turn on interrupts */
1417 writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
1418 SSP_CR1(pl022->virtbase));
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001419 writew(irqflags, SSP_IMSC(pl022->virtbase));
Linus Walleijb43d65f2009-06-09 08:11:42 +01001420}
1421
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001422static void do_polling_transfer(struct pl022 *pl022)
Linus Walleijb43d65f2009-06-09 08:11:42 +01001423{
Linus Walleijb43d65f2009-06-09 08:11:42 +01001424 struct spi_message *message = NULL;
1425 struct spi_transfer *transfer = NULL;
1426 struct spi_transfer *previous = NULL;
1427 struct chip_data *chip;
Magnus Templinga18c2662011-05-19 18:05:34 +02001428 unsigned long time, timeout;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001429
1430 chip = pl022->cur_chip;
1431 message = pl022->cur_msg;
1432
1433 while (message->state != STATE_DONE) {
1434 /* Handle for abort */
1435 if (message->state == STATE_ERROR)
1436 break;
1437 transfer = pl022->cur_transfer;
1438
1439 /* Delay if requested at end of transfer */
1440 if (message->state == STATE_RUNNING) {
1441 previous =
1442 list_entry(transfer->transfer_list.prev,
1443 struct spi_transfer, transfer_list);
1444 if (previous->delay_usecs)
1445 udelay(previous->delay_usecs);
1446 if (previous->cs_change)
1447 pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
1448 } else {
1449 /* STATE_START */
1450 message->state = STATE_RUNNING;
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +05301451 if (!pl022->next_msg_cs_active)
1452 pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001453 }
1454
1455 /* Configuration Changing Per Transfer */
1456 if (set_up_next_transfer(pl022, transfer)) {
1457 /* Error path */
1458 message->state = STATE_ERROR;
1459 break;
1460 }
1461 /* Flush FIFOs and enable SSP */
1462 flush(pl022);
1463 writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
1464 SSP_CR1(pl022->virtbase));
1465
Linus Walleij556f4ae2010-05-05 09:28:15 +00001466 dev_dbg(&pl022->adev->dev, "polling transfer ongoing ...\n");
Magnus Templinga18c2662011-05-19 18:05:34 +02001467
1468 timeout = jiffies + msecs_to_jiffies(SPI_POLLING_TIMEOUT);
1469 while (pl022->tx < pl022->tx_end || pl022->rx < pl022->rx_end) {
1470 time = jiffies;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001471 readwriter(pl022);
Magnus Templinga18c2662011-05-19 18:05:34 +02001472 if (time_after(time, timeout)) {
1473 dev_warn(&pl022->adev->dev,
1474 "%s: timeout!\n", __func__);
1475 message->state = STATE_ERROR;
1476 goto out;
1477 }
Linus Walleij521999b2011-05-19 20:01:25 +02001478 cpu_relax();
Magnus Templinga18c2662011-05-19 18:05:34 +02001479 }
Linus Walleijb43d65f2009-06-09 08:11:42 +01001480
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001481 /* Update total byte transferred */
Linus Walleijb43d65f2009-06-09 08:11:42 +01001482 message->actual_length += pl022->cur_transfer->len;
1483 if (pl022->cur_transfer->cs_change)
1484 pl022->cur_chip->cs_control(SSP_CHIP_DESELECT);
1485 /* Move to next transfer */
1486 message->state = next_transfer(pl022);
1487 }
Magnus Templinga18c2662011-05-19 18:05:34 +02001488out:
Linus Walleijb43d65f2009-06-09 08:11:42 +01001489 /* Handle end of message */
1490 if (message->state == STATE_DONE)
1491 message->status = 0;
1492 else
1493 message->status = -EIO;
1494
1495 giveback(pl022);
1496 return;
1497}
1498
1499/**
Chris Blair14af60b2012-02-02 13:59:34 +01001500 * pump_messages - kthread work function which processes spi message queue
1501 * @work: pointer to kthread work struct contained in the pl022 private struct
Linus Walleijb43d65f2009-06-09 08:11:42 +01001502 *
1503 * This function checks if there is any spi message in the queue that
1504 * needs processing and delegate control to appropriate function
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001505 * do_polling_transfer()/do_interrupt_dma_transfer()
Linus Walleijb43d65f2009-06-09 08:11:42 +01001506 * based on the kind of the transfer
1507 *
1508 */
Chris Blair14af60b2012-02-02 13:59:34 +01001509static void pump_messages(struct kthread_work *work)
Linus Walleijb43d65f2009-06-09 08:11:42 +01001510{
1511 struct pl022 *pl022 =
1512 container_of(work, struct pl022, pump_messages);
1513 unsigned long flags;
Chris Blaird4b6af22011-11-04 07:43:41 +00001514 bool was_busy = false;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001515
1516 /* Lock queue and check for queue work */
1517 spin_lock_irqsave(&pl022->queue_lock, flags);
Linus Walleij5e8b8212010-12-22 23:13:59 +01001518 if (list_empty(&pl022->queue) || !pl022->running) {
Virupax Sadashivpetimath0ad2dee2011-10-17 14:52:47 +02001519 if (pl022->busy) {
1520 /* nothing more to do - disable spi/ssp and power off */
1521 writew((readw(SSP_CR1(pl022->virtbase)) &
1522 (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
Chris Blair53e4ace2011-11-08 08:54:46 +00001523
1524 if (pl022->master_info->autosuspend_delay > 0) {
1525 pm_runtime_mark_last_busy(&pl022->adev->dev);
1526 pm_runtime_put_autosuspend(&pl022->adev->dev);
1527 } else {
1528 pm_runtime_put(&pl022->adev->dev);
1529 }
Virupax Sadashivpetimath0ad2dee2011-10-17 14:52:47 +02001530 }
Linus Walleijdec5a582010-12-22 23:13:48 +01001531 pl022->busy = false;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001532 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1533 return;
1534 }
Chris Blaird4b6af22011-11-04 07:43:41 +00001535
Linus Walleijb43d65f2009-06-09 08:11:42 +01001536 /* Make sure we are not already running a message */
1537 if (pl022->cur_msg) {
1538 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1539 return;
1540 }
1541 /* Extract head of queue */
1542 pl022->cur_msg =
1543 list_entry(pl022->queue.next, struct spi_message, queue);
1544
1545 list_del_init(&pl022->cur_msg->queue);
Chris Blaird4b6af22011-11-04 07:43:41 +00001546 if (pl022->busy)
1547 was_busy = true;
1548 else
1549 pl022->busy = true;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001550 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1551
1552 /* Initial message state */
1553 pl022->cur_msg->state = STATE_START;
1554 pl022->cur_transfer = list_entry(pl022->cur_msg->transfers.next,
Viresh Kumarf1e45f82011-08-10 14:20:54 +05301555 struct spi_transfer, transfer_list);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001556
1557 /* Setup the SPI using the per chip configuration */
1558 pl022->cur_chip = spi_get_ctldata(pl022->cur_msg->spi);
Chris Blaird4b6af22011-11-04 07:43:41 +00001559 if (!was_busy)
1560 /*
1561 * We enable the core voltage and clocks here, then the clocks
Chris Blair14af60b2012-02-02 13:59:34 +01001562 * and core will be disabled when this thread is run again
Chris Blaird4b6af22011-11-04 07:43:41 +00001563 * and there is no more work to be done.
1564 */
1565 pm_runtime_get_sync(&pl022->adev->dev);
1566
Linus Walleijb43d65f2009-06-09 08:11:42 +01001567 restore_state(pl022);
1568 flush(pl022);
1569
1570 if (pl022->cur_chip->xfer_type == POLLING_TRANSFER)
1571 do_polling_transfer(pl022);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001572 else
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001573 do_interrupt_dma_transfer(pl022);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001574}
1575
Linus Walleijb43d65f2009-06-09 08:11:42 +01001576static int __init init_queue(struct pl022 *pl022)
1577{
Chris Blair14af60b2012-02-02 13:59:34 +01001578 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
1579
Linus Walleijb43d65f2009-06-09 08:11:42 +01001580 INIT_LIST_HEAD(&pl022->queue);
1581 spin_lock_init(&pl022->queue_lock);
1582
Linus Walleij5e8b8212010-12-22 23:13:59 +01001583 pl022->running = false;
Linus Walleijdec5a582010-12-22 23:13:48 +01001584 pl022->busy = false;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001585
Viresh Kumarf1e45f82011-08-10 14:20:54 +05301586 tasklet_init(&pl022->pump_transfers, pump_transfers,
1587 (unsigned long)pl022);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001588
Chris Blair14af60b2012-02-02 13:59:34 +01001589 init_kthread_worker(&pl022->kworker);
1590 pl022->kworker_task = kthread_run(kthread_worker_fn,
1591 &pl022->kworker,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001592 dev_name(pl022->master->dev.parent));
Chris Blair14af60b2012-02-02 13:59:34 +01001593 if (IS_ERR(pl022->kworker_task)) {
1594 dev_err(&pl022->adev->dev,
1595 "failed to create message pump task\n");
1596 return -ENOMEM;
1597 }
1598 init_kthread_work(&pl022->pump_messages, pump_messages);
1599
1600 /*
1601 * Board config will indicate if this controller should run the
1602 * message pump with high (realtime) priority to reduce the transfer
1603 * latency on the bus by minimising the delay between a transfer
1604 * request and the scheduling of the message pump thread. Without this
1605 * setting the message pump thread will remain at default priority.
1606 */
1607 if (pl022->master_info->rt) {
1608 dev_info(&pl022->adev->dev,
1609 "will run message pump with realtime priority\n");
1610 sched_setscheduler(pl022->kworker_task, SCHED_FIFO, &param);
1611 }
Linus Walleijb43d65f2009-06-09 08:11:42 +01001612
1613 return 0;
1614}
1615
Linus Walleijb43d65f2009-06-09 08:11:42 +01001616static int start_queue(struct pl022 *pl022)
1617{
1618 unsigned long flags;
1619
1620 spin_lock_irqsave(&pl022->queue_lock, flags);
1621
Linus Walleij5e8b8212010-12-22 23:13:59 +01001622 if (pl022->running || pl022->busy) {
Linus Walleijb43d65f2009-06-09 08:11:42 +01001623 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1624 return -EBUSY;
1625 }
1626
Linus Walleij5e8b8212010-12-22 23:13:59 +01001627 pl022->running = true;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001628 pl022->cur_msg = NULL;
1629 pl022->cur_transfer = NULL;
1630 pl022->cur_chip = NULL;
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +05301631 pl022->next_msg_cs_active = false;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001632 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1633
Chris Blair14af60b2012-02-02 13:59:34 +01001634 queue_kthread_work(&pl022->kworker, &pl022->pump_messages);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001635
1636 return 0;
1637}
1638
Linus Walleijb43d65f2009-06-09 08:11:42 +01001639static int stop_queue(struct pl022 *pl022)
1640{
1641 unsigned long flags;
1642 unsigned limit = 500;
1643 int status = 0;
1644
1645 spin_lock_irqsave(&pl022->queue_lock, flags);
1646
1647 /* This is a bit lame, but is optimized for the common execution path.
1648 * A wait_queue on the pl022->busy could be used, but then the common
1649 * execution path (pump_messages) would be required to call wake_up or
1650 * friends on every SPI message. Do this instead */
Vasily Khoruzhick850a28e2011-04-06 17:49:15 +03001651 while ((!list_empty(&pl022->queue) || pl022->busy) && limit--) {
Linus Walleijb43d65f2009-06-09 08:11:42 +01001652 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1653 msleep(10);
1654 spin_lock_irqsave(&pl022->queue_lock, flags);
1655 }
1656
1657 if (!list_empty(&pl022->queue) || pl022->busy)
1658 status = -EBUSY;
Linus Walleij5e8b8212010-12-22 23:13:59 +01001659 else
1660 pl022->running = false;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001661
1662 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1663
1664 return status;
1665}
1666
1667static int destroy_queue(struct pl022 *pl022)
1668{
1669 int status;
1670
1671 status = stop_queue(pl022);
Chris Blair14af60b2012-02-02 13:59:34 +01001672
1673 /*
1674 * We are unloading the module or failing to load (only two calls
Linus Walleijb43d65f2009-06-09 08:11:42 +01001675 * to this routine), and neither call can handle a return value.
Chris Blair14af60b2012-02-02 13:59:34 +01001676 * However, flush_kthread_worker will block until all work is done.
1677 * If the reason that stop_queue timed out is that the work will never
1678 * finish, then it does no good to call flush/stop thread, so
1679 * return anyway.
1680 */
Linus Walleijb43d65f2009-06-09 08:11:42 +01001681 if (status != 0)
1682 return status;
1683
Chris Blair14af60b2012-02-02 13:59:34 +01001684 flush_kthread_worker(&pl022->kworker);
1685 kthread_stop(pl022->kworker_task);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001686
1687 return 0;
1688}
1689
1690static int verify_controller_parameters(struct pl022 *pl022,
Linus Walleijf9d629c2010-10-01 13:33:13 +02001691 struct pl022_config_chip const *chip_info)
Linus Walleijb43d65f2009-06-09 08:11:42 +01001692{
Linus Walleijb43d65f2009-06-09 08:11:42 +01001693 if ((chip_info->iface < SSP_INTERFACE_MOTOROLA_SPI)
1694 || (chip_info->iface > SSP_INTERFACE_UNIDIRECTIONAL)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001695 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001696 "interface is configured incorrectly\n");
1697 return -EINVAL;
1698 }
1699 if ((chip_info->iface == SSP_INTERFACE_UNIDIRECTIONAL) &&
1700 (!pl022->vendor->unidir)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001701 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001702 "unidirectional mode not supported in this "
1703 "hardware version\n");
1704 return -EINVAL;
1705 }
1706 if ((chip_info->hierarchy != SSP_MASTER)
1707 && (chip_info->hierarchy != SSP_SLAVE)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001708 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001709 "hierarchy is configured incorrectly\n");
1710 return -EINVAL;
1711 }
Linus Walleijb43d65f2009-06-09 08:11:42 +01001712 if ((chip_info->com_mode != INTERRUPT_TRANSFER)
1713 && (chip_info->com_mode != DMA_TRANSFER)
1714 && (chip_info->com_mode != POLLING_TRANSFER)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001715 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001716 "Communication mode is configured incorrectly\n");
1717 return -EINVAL;
1718 }
Linus Walleij78b2b912011-06-16 10:14:46 +02001719 switch (chip_info->rx_lev_trig) {
1720 case SSP_RX_1_OR_MORE_ELEM:
1721 case SSP_RX_4_OR_MORE_ELEM:
1722 case SSP_RX_8_OR_MORE_ELEM:
1723 /* These are always OK, all variants can handle this */
1724 break;
1725 case SSP_RX_16_OR_MORE_ELEM:
1726 if (pl022->vendor->fifodepth < 16) {
1727 dev_err(&pl022->adev->dev,
1728 "RX FIFO Trigger Level is configured incorrectly\n");
1729 return -EINVAL;
1730 }
1731 break;
1732 case SSP_RX_32_OR_MORE_ELEM:
1733 if (pl022->vendor->fifodepth < 32) {
1734 dev_err(&pl022->adev->dev,
1735 "RX FIFO Trigger Level is configured incorrectly\n");
1736 return -EINVAL;
1737 }
1738 break;
1739 default:
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001740 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001741 "RX FIFO Trigger Level is configured incorrectly\n");
1742 return -EINVAL;
Linus Walleij78b2b912011-06-16 10:14:46 +02001743 break;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001744 }
Linus Walleij78b2b912011-06-16 10:14:46 +02001745 switch (chip_info->tx_lev_trig) {
1746 case SSP_TX_1_OR_MORE_EMPTY_LOC:
1747 case SSP_TX_4_OR_MORE_EMPTY_LOC:
1748 case SSP_TX_8_OR_MORE_EMPTY_LOC:
1749 /* These are always OK, all variants can handle this */
1750 break;
1751 case SSP_TX_16_OR_MORE_EMPTY_LOC:
1752 if (pl022->vendor->fifodepth < 16) {
1753 dev_err(&pl022->adev->dev,
1754 "TX FIFO Trigger Level is configured incorrectly\n");
1755 return -EINVAL;
1756 }
1757 break;
1758 case SSP_TX_32_OR_MORE_EMPTY_LOC:
1759 if (pl022->vendor->fifodepth < 32) {
1760 dev_err(&pl022->adev->dev,
1761 "TX FIFO Trigger Level is configured incorrectly\n");
1762 return -EINVAL;
1763 }
1764 break;
1765 default:
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001766 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001767 "TX FIFO Trigger Level is configured incorrectly\n");
1768 return -EINVAL;
Linus Walleij78b2b912011-06-16 10:14:46 +02001769 break;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001770 }
Linus Walleijb43d65f2009-06-09 08:11:42 +01001771 if (chip_info->iface == SSP_INTERFACE_NATIONAL_MICROWIRE) {
1772 if ((chip_info->ctrl_len < SSP_BITS_4)
1773 || (chip_info->ctrl_len > SSP_BITS_32)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001774 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001775 "CTRL LEN is configured incorrectly\n");
1776 return -EINVAL;
1777 }
1778 if ((chip_info->wait_state != SSP_MWIRE_WAIT_ZERO)
1779 && (chip_info->wait_state != SSP_MWIRE_WAIT_ONE)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001780 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001781 "Wait State is configured incorrectly\n");
1782 return -EINVAL;
1783 }
Linus Walleij556f4ae2010-05-05 09:28:15 +00001784 /* Half duplex is only available in the ST Micro version */
1785 if (pl022->vendor->extended_cr) {
1786 if ((chip_info->duplex !=
1787 SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
1788 && (chip_info->duplex !=
Julia Lawall4a4fd472010-09-29 17:31:30 +09001789 SSP_MICROWIRE_CHANNEL_HALF_DUPLEX)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001790 dev_err(&pl022->adev->dev,
Linus Walleij556f4ae2010-05-05 09:28:15 +00001791 "Microwire duplex mode is configured incorrectly\n");
1792 return -EINVAL;
Julia Lawall4a4fd472010-09-29 17:31:30 +09001793 }
Linus Walleij556f4ae2010-05-05 09:28:15 +00001794 } else {
1795 if (chip_info->duplex != SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001796 dev_err(&pl022->adev->dev,
Linus Walleij556f4ae2010-05-05 09:28:15 +00001797 "Microwire half duplex mode requested,"
1798 " but this is only available in the"
1799 " ST version of PL022\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01001800 return -EINVAL;
1801 }
1802 }
Linus Walleijb43d65f2009-06-09 08:11:42 +01001803 return 0;
1804}
1805
1806/**
1807 * pl022_transfer - transfer function registered to SPI master framework
1808 * @spi: spi device which is requesting transfer
1809 * @msg: spi message which is to handled is queued to driver queue
1810 *
1811 * This function is registered to the SPI framework for this SPI master
1812 * controller. It will queue the spi_message in the queue of driver if
1813 * the queue is not stopped and return.
1814 */
1815static int pl022_transfer(struct spi_device *spi, struct spi_message *msg)
1816{
1817 struct pl022 *pl022 = spi_master_get_devdata(spi->master);
1818 unsigned long flags;
1819
1820 spin_lock_irqsave(&pl022->queue_lock, flags);
1821
Linus Walleij5e8b8212010-12-22 23:13:59 +01001822 if (!pl022->running) {
Linus Walleijb43d65f2009-06-09 08:11:42 +01001823 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1824 return -ESHUTDOWN;
1825 }
1826 msg->actual_length = 0;
1827 msg->status = -EINPROGRESS;
1828 msg->state = STATE_START;
1829
1830 list_add_tail(&msg->queue, &pl022->queue);
Linus Walleij5e8b8212010-12-22 23:13:59 +01001831 if (pl022->running && !pl022->busy)
Chris Blair14af60b2012-02-02 13:59:34 +01001832 queue_kthread_work(&pl022->kworker, &pl022->pump_messages);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001833
1834 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1835 return 0;
1836}
1837
Viresh Kumar0379b2a2011-08-10 14:20:57 +05301838static inline u32 spi_rate(u32 rate, u16 cpsdvsr, u16 scr)
1839{
1840 return rate / (cpsdvsr * (1 + scr));
1841}
1842
1843static int calculate_effective_freq(struct pl022 *pl022, int freq, struct
1844 ssp_clock_params * clk_freq)
Linus Walleijb43d65f2009-06-09 08:11:42 +01001845{
1846 /* Lets calculate the frequency parameters */
Viresh Kumar0379b2a2011-08-10 14:20:57 +05301847 u16 cpsdvsr = CPSDVR_MIN, scr = SCR_MIN;
1848 u32 rate, max_tclk, min_tclk, best_freq = 0, best_cpsdvsr = 0,
1849 best_scr = 0, tmp, found = 0;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001850
1851 rate = clk_get_rate(pl022->clk);
1852 /* cpsdvscr = 2 & scr 0 */
Viresh Kumar0379b2a2011-08-10 14:20:57 +05301853 max_tclk = spi_rate(rate, CPSDVR_MIN, SCR_MIN);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001854 /* cpsdvsr = 254 & scr = 255 */
Viresh Kumar0379b2a2011-08-10 14:20:57 +05301855 min_tclk = spi_rate(rate, CPSDVR_MAX, SCR_MAX);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001856
Viresh Kumar0379b2a2011-08-10 14:20:57 +05301857 if (!((freq <= max_tclk) && (freq >= min_tclk))) {
Linus Walleijb43d65f2009-06-09 08:11:42 +01001858 dev_err(&pl022->adev->dev,
1859 "controller data is incorrect: out of range frequency");
1860 return -EINVAL;
1861 }
Viresh Kumar0379b2a2011-08-10 14:20:57 +05301862
1863 /*
1864 * best_freq will give closest possible available rate (<= requested
1865 * freq) for all values of scr & cpsdvsr.
1866 */
1867 while ((cpsdvsr <= CPSDVR_MAX) && !found) {
1868 while (scr <= SCR_MAX) {
1869 tmp = spi_rate(rate, cpsdvsr, scr);
1870
1871 if (tmp > freq)
1872 scr++;
1873 /*
1874 * If found exact value, update and break.
1875 * If found more closer value, update and continue.
1876 */
1877 else if ((tmp == freq) || (tmp > best_freq)) {
1878 best_freq = tmp;
1879 best_cpsdvsr = cpsdvsr;
1880 best_scr = scr;
1881
1882 if (tmp == freq)
1883 break;
1884 }
1885 scr++;
1886 }
1887 cpsdvsr += 2;
1888 scr = SCR_MIN;
1889 }
1890
1891 clk_freq->cpsdvsr = (u8) (best_cpsdvsr & 0xFF);
1892 clk_freq->scr = (u8) (best_scr & 0xFF);
1893 dev_dbg(&pl022->adev->dev,
1894 "SSP Target Frequency is: %u, Effective Frequency is %u\n",
1895 freq, best_freq);
1896 dev_dbg(&pl022->adev->dev, "SSP cpsdvsr = %d, scr = %d\n",
1897 clk_freq->cpsdvsr, clk_freq->scr);
1898
Linus Walleijb43d65f2009-06-09 08:11:42 +01001899 return 0;
1900}
1901
Linus Walleijf9d629c2010-10-01 13:33:13 +02001902/*
1903 * A piece of default chip info unless the platform
1904 * supplies it.
1905 */
1906static const struct pl022_config_chip pl022_default_chip_info = {
1907 .com_mode = POLLING_TRANSFER,
1908 .iface = SSP_INTERFACE_MOTOROLA_SPI,
1909 .hierarchy = SSP_SLAVE,
1910 .slave_tx_disable = DO_NOT_DRIVE_TX,
1911 .rx_lev_trig = SSP_RX_1_OR_MORE_ELEM,
1912 .tx_lev_trig = SSP_TX_1_OR_MORE_EMPTY_LOC,
1913 .ctrl_len = SSP_BITS_8,
1914 .wait_state = SSP_MWIRE_WAIT_ZERO,
1915 .duplex = SSP_MICROWIRE_CHANNEL_FULL_DUPLEX,
1916 .cs_control = null_cs_control,
1917};
1918
Linus Walleijb43d65f2009-06-09 08:11:42 +01001919/**
Linus Walleijb43d65f2009-06-09 08:11:42 +01001920 * pl022_setup - setup function registered to SPI master framework
1921 * @spi: spi device which is requesting setup
1922 *
1923 * This function is registered to the SPI framework for this SPI master
1924 * controller. If it is the first time when setup is called by this device,
1925 * this function will initialize the runtime state for this chip and save
1926 * the same in the device structure. Else it will update the runtime info
1927 * with the updated chip info. Nothing is really being written to the
1928 * controller hardware here, that is not done until the actual transfer
1929 * commence.
1930 */
Linus Walleijb43d65f2009-06-09 08:11:42 +01001931static int pl022_setup(struct spi_device *spi)
1932{
Linus Walleijf9d629c2010-10-01 13:33:13 +02001933 struct pl022_config_chip const *chip_info;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001934 struct chip_data *chip;
Jonas Aabergc4a47842011-02-28 16:42:41 +01001935 struct ssp_clock_params clk_freq = { .cpsdvsr = 0, .scr = 0};
Linus Walleijb43d65f2009-06-09 08:11:42 +01001936 int status = 0;
1937 struct pl022 *pl022 = spi_master_get_devdata(spi->master);
Kevin Wellsbde435a2010-09-16 06:18:50 -07001938 unsigned int bits = spi->bits_per_word;
1939 u32 tmp;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001940
1941 if (!spi->max_speed_hz)
1942 return -EINVAL;
1943
1944 /* Get controller_state if one is supplied */
1945 chip = spi_get_ctldata(spi);
1946
1947 if (chip == NULL) {
1948 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1949 if (!chip) {
1950 dev_err(&spi->dev,
1951 "cannot allocate controller state\n");
1952 return -ENOMEM;
1953 }
1954 dev_dbg(&spi->dev,
1955 "allocated memory for controller's runtime state\n");
1956 }
1957
1958 /* Get controller data if one is supplied */
1959 chip_info = spi->controller_data;
1960
1961 if (chip_info == NULL) {
Linus Walleijf9d629c2010-10-01 13:33:13 +02001962 chip_info = &pl022_default_chip_info;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001963 /* spi_board_info.controller_data not is supplied */
1964 dev_dbg(&spi->dev,
1965 "using default controller_data settings\n");
Linus Walleijf9d629c2010-10-01 13:33:13 +02001966 } else
Linus Walleijb43d65f2009-06-09 08:11:42 +01001967 dev_dbg(&spi->dev,
1968 "using user supplied controller_data settings\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01001969
1970 /*
1971 * We can override with custom divisors, else we use the board
1972 * frequency setting
1973 */
1974 if ((0 == chip_info->clk_freq.cpsdvsr)
1975 && (0 == chip_info->clk_freq.scr)) {
1976 status = calculate_effective_freq(pl022,
1977 spi->max_speed_hz,
Linus Walleijf9d629c2010-10-01 13:33:13 +02001978 &clk_freq);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001979 if (status < 0)
1980 goto err_config_params;
1981 } else {
Linus Walleijf9d629c2010-10-01 13:33:13 +02001982 memcpy(&clk_freq, &chip_info->clk_freq, sizeof(clk_freq));
1983 if ((clk_freq.cpsdvsr % 2) != 0)
1984 clk_freq.cpsdvsr =
1985 clk_freq.cpsdvsr - 1;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001986 }
Linus Walleijf9d629c2010-10-01 13:33:13 +02001987 if ((clk_freq.cpsdvsr < CPSDVR_MIN)
1988 || (clk_freq.cpsdvsr > CPSDVR_MAX)) {
Virupax Sadashivpetimathe3f88ae2011-06-13 16:23:46 +05301989 status = -EINVAL;
Linus Walleijf9d629c2010-10-01 13:33:13 +02001990 dev_err(&spi->dev,
1991 "cpsdvsr is configured incorrectly\n");
1992 goto err_config_params;
1993 }
1994
Linus Walleijb43d65f2009-06-09 08:11:42 +01001995 status = verify_controller_parameters(pl022, chip_info);
1996 if (status) {
1997 dev_err(&spi->dev, "controller data is incorrect");
1998 goto err_config_params;
1999 }
Linus Walleijf9d629c2010-10-01 13:33:13 +02002000
Linus Walleij083be3f2011-06-16 10:14:28 +02002001 pl022->rx_lev_trig = chip_info->rx_lev_trig;
2002 pl022->tx_lev_trig = chip_info->tx_lev_trig;
2003
Linus Walleijb43d65f2009-06-09 08:11:42 +01002004 /* Now set controller state based on controller data */
2005 chip->xfer_type = chip_info->com_mode;
Linus Walleijf9d629c2010-10-01 13:33:13 +02002006 if (!chip_info->cs_control) {
2007 chip->cs_control = null_cs_control;
2008 dev_warn(&spi->dev,
2009 "chip select function is NULL for this chip\n");
2010 } else
2011 chip->cs_control = chip_info->cs_control;
Linus Walleijb43d65f2009-06-09 08:11:42 +01002012
Kevin Wellsbde435a2010-09-16 06:18:50 -07002013 if (bits <= 3) {
2014 /* PL022 doesn't support less than 4-bits */
2015 status = -ENOTSUPP;
2016 goto err_config_params;
2017 } else if (bits <= 8) {
2018 dev_dbg(&spi->dev, "4 <= n <=8 bits per word\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01002019 chip->n_bytes = 1;
2020 chip->read = READING_U8;
2021 chip->write = WRITING_U8;
Kevin Wellsbde435a2010-09-16 06:18:50 -07002022 } else if (bits <= 16) {
Linus Walleijb43d65f2009-06-09 08:11:42 +01002023 dev_dbg(&spi->dev, "9 <= n <= 16 bits per word\n");
2024 chip->n_bytes = 2;
2025 chip->read = READING_U16;
2026 chip->write = WRITING_U16;
2027 } else {
2028 if (pl022->vendor->max_bpw >= 32) {
2029 dev_dbg(&spi->dev, "17 <= n <= 32 bits per word\n");
2030 chip->n_bytes = 4;
2031 chip->read = READING_U32;
2032 chip->write = WRITING_U32;
2033 } else {
2034 dev_err(&spi->dev,
2035 "illegal data size for this controller!\n");
2036 dev_err(&spi->dev,
2037 "a standard pl022 can only handle "
2038 "1 <= n <= 16 bit words\n");
Kevin Wellsbde435a2010-09-16 06:18:50 -07002039 status = -ENOTSUPP;
Linus Walleijb43d65f2009-06-09 08:11:42 +01002040 goto err_config_params;
2041 }
2042 }
2043
2044 /* Now Initialize all register settings required for this chip */
2045 chip->cr0 = 0;
2046 chip->cr1 = 0;
2047 chip->dmacr = 0;
2048 chip->cpsr = 0;
2049 if ((chip_info->com_mode == DMA_TRANSFER)
2050 && ((pl022->master_info)->enable_dma)) {
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09002051 chip->enable_dma = true;
Linus Walleijb43d65f2009-06-09 08:11:42 +01002052 dev_dbg(&spi->dev, "DMA mode set in controller state\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01002053 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
2054 SSP_DMACR_MASK_RXDMAE, 0);
2055 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
2056 SSP_DMACR_MASK_TXDMAE, 1);
2057 } else {
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09002058 chip->enable_dma = false;
Linus Walleijb43d65f2009-06-09 08:11:42 +01002059 dev_dbg(&spi->dev, "DMA mode NOT set in controller state\n");
2060 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
2061 SSP_DMACR_MASK_RXDMAE, 0);
2062 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
2063 SSP_DMACR_MASK_TXDMAE, 1);
2064 }
2065
Linus Walleijf9d629c2010-10-01 13:33:13 +02002066 chip->cpsr = clk_freq.cpsdvsr;
Linus Walleijb43d65f2009-06-09 08:11:42 +01002067
Linus Walleij556f4ae2010-05-05 09:28:15 +00002068 /* Special setup for the ST micro extended control registers */
2069 if (pl022->vendor->extended_cr) {
Kevin Wellsbde435a2010-09-16 06:18:50 -07002070 u32 etx;
2071
Linus Walleij781c7b12010-05-07 08:40:53 +00002072 if (pl022->vendor->pl023) {
2073 /* These bits are only in the PL023 */
2074 SSP_WRITE_BITS(chip->cr1, chip_info->clkdelay,
2075 SSP_CR1_MASK_FBCLKDEL_ST, 13);
2076 } else {
2077 /* These bits are in the PL022 but not PL023 */
2078 SSP_WRITE_BITS(chip->cr0, chip_info->duplex,
2079 SSP_CR0_MASK_HALFDUP_ST, 5);
2080 SSP_WRITE_BITS(chip->cr0, chip_info->ctrl_len,
2081 SSP_CR0_MASK_CSS_ST, 16);
2082 SSP_WRITE_BITS(chip->cr0, chip_info->iface,
2083 SSP_CR0_MASK_FRF_ST, 21);
2084 SSP_WRITE_BITS(chip->cr1, chip_info->wait_state,
2085 SSP_CR1_MASK_MWAIT_ST, 6);
2086 }
Kevin Wellsbde435a2010-09-16 06:18:50 -07002087 SSP_WRITE_BITS(chip->cr0, bits - 1,
Linus Walleij556f4ae2010-05-05 09:28:15 +00002088 SSP_CR0_MASK_DSS_ST, 0);
Kevin Wellsbde435a2010-09-16 06:18:50 -07002089
2090 if (spi->mode & SPI_LSB_FIRST) {
2091 tmp = SSP_RX_LSB;
2092 etx = SSP_TX_LSB;
2093 } else {
2094 tmp = SSP_RX_MSB;
2095 etx = SSP_TX_MSB;
2096 }
2097 SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_RENDN_ST, 4);
2098 SSP_WRITE_BITS(chip->cr1, etx, SSP_CR1_MASK_TENDN_ST, 5);
Linus Walleij556f4ae2010-05-05 09:28:15 +00002099 SSP_WRITE_BITS(chip->cr1, chip_info->rx_lev_trig,
2100 SSP_CR1_MASK_RXIFLSEL_ST, 7);
2101 SSP_WRITE_BITS(chip->cr1, chip_info->tx_lev_trig,
2102 SSP_CR1_MASK_TXIFLSEL_ST, 10);
2103 } else {
Kevin Wellsbde435a2010-09-16 06:18:50 -07002104 SSP_WRITE_BITS(chip->cr0, bits - 1,
Linus Walleij556f4ae2010-05-05 09:28:15 +00002105 SSP_CR0_MASK_DSS, 0);
2106 SSP_WRITE_BITS(chip->cr0, chip_info->iface,
2107 SSP_CR0_MASK_FRF, 4);
2108 }
Kevin Wellsbde435a2010-09-16 06:18:50 -07002109
Linus Walleij556f4ae2010-05-05 09:28:15 +00002110 /* Stuff that is common for all versions */
Kevin Wellsbde435a2010-09-16 06:18:50 -07002111 if (spi->mode & SPI_CPOL)
2112 tmp = SSP_CLK_POL_IDLE_HIGH;
2113 else
2114 tmp = SSP_CLK_POL_IDLE_LOW;
2115 SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPO, 6);
2116
2117 if (spi->mode & SPI_CPHA)
2118 tmp = SSP_CLK_SECOND_EDGE;
2119 else
2120 tmp = SSP_CLK_FIRST_EDGE;
2121 SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPH, 7);
2122
Linus Walleijf9d629c2010-10-01 13:33:13 +02002123 SSP_WRITE_BITS(chip->cr0, clk_freq.scr, SSP_CR0_MASK_SCR, 8);
Linus Walleij781c7b12010-05-07 08:40:53 +00002124 /* Loopback is available on all versions except PL023 */
Philippe Langlais06fb01f2011-03-23 11:05:16 +01002125 if (pl022->vendor->loopback) {
Kevin Wellsbde435a2010-09-16 06:18:50 -07002126 if (spi->mode & SPI_LOOP)
2127 tmp = LOOPBACK_ENABLED;
2128 else
2129 tmp = LOOPBACK_DISABLED;
2130 SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_LBM, 0);
2131 }
Linus Walleijb43d65f2009-06-09 08:11:42 +01002132 SSP_WRITE_BITS(chip->cr1, SSP_DISABLED, SSP_CR1_MASK_SSE, 1);
2133 SSP_WRITE_BITS(chip->cr1, chip_info->hierarchy, SSP_CR1_MASK_MS, 2);
Viresh Kumarf1e45f82011-08-10 14:20:54 +05302134 SSP_WRITE_BITS(chip->cr1, chip_info->slave_tx_disable, SSP_CR1_MASK_SOD,
2135 3);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002136
2137 /* Save controller_state */
2138 spi_set_ctldata(spi, chip);
2139 return status;
2140 err_config_params:
Kevin Wellsbde435a2010-09-16 06:18:50 -07002141 spi_set_ctldata(spi, NULL);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002142 kfree(chip);
2143 return status;
2144}
2145
2146/**
2147 * pl022_cleanup - cleanup function registered to SPI master framework
2148 * @spi: spi device which is requesting cleanup
2149 *
2150 * This function is registered to the SPI framework for this SPI master
2151 * controller. It will free the runtime state of chip.
2152 */
2153static void pl022_cleanup(struct spi_device *spi)
2154{
2155 struct chip_data *chip = spi_get_ctldata(spi);
2156
2157 spi_set_ctldata(spi, NULL);
2158 kfree(chip);
2159}
2160
Kevin Wellsb4225882010-07-27 16:39:30 +00002161static int __devinit
Russell Kingaa25afa2011-02-19 15:55:00 +00002162pl022_probe(struct amba_device *adev, const struct amba_id *id)
Linus Walleijb43d65f2009-06-09 08:11:42 +01002163{
2164 struct device *dev = &adev->dev;
2165 struct pl022_ssp_controller *platform_info = adev->dev.platform_data;
2166 struct spi_master *master;
2167 struct pl022 *pl022 = NULL; /*Data for this driver */
2168 int status = 0;
2169
2170 dev_info(&adev->dev,
2171 "ARM PL022 driver, device ID: 0x%08x\n", adev->periphid);
2172 if (platform_info == NULL) {
2173 dev_err(&adev->dev, "probe - no platform data supplied\n");
2174 status = -ENODEV;
2175 goto err_no_pdata;
2176 }
2177
2178 /* Allocate master with space for data */
2179 master = spi_alloc_master(dev, sizeof(struct pl022));
2180 if (master == NULL) {
2181 dev_err(&adev->dev, "probe - cannot alloc SPI master\n");
2182 status = -ENOMEM;
2183 goto err_no_master;
2184 }
2185
2186 pl022 = spi_master_get_devdata(master);
2187 pl022->master = master;
2188 pl022->master_info = platform_info;
2189 pl022->adev = adev;
2190 pl022->vendor = id->data;
2191
2192 /*
2193 * Bus Number Which has been Assigned to this SSP controller
2194 * on this board
2195 */
2196 master->bus_num = platform_info->bus_id;
2197 master->num_chipselect = platform_info->num_chipselect;
2198 master->cleanup = pl022_cleanup;
2199 master->setup = pl022_setup;
2200 master->transfer = pl022_transfer;
2201
Kevin Wellsbde435a2010-09-16 06:18:50 -07002202 /*
2203 * Supports mode 0-3, loopback, and active low CS. Transfers are
2204 * always MS bit first on the original pl022.
2205 */
2206 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
2207 if (pl022->vendor->extended_cr)
2208 master->mode_bits |= SPI_LSB_FIRST;
2209
Linus Walleijb43d65f2009-06-09 08:11:42 +01002210 dev_dbg(&adev->dev, "BUSNO: %d\n", master->bus_num);
2211
2212 status = amba_request_regions(adev, NULL);
2213 if (status)
2214 goto err_no_ioregion;
2215
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09002216 pl022->phybase = adev->res.start;
Linus Walleijb43d65f2009-06-09 08:11:42 +01002217 pl022->virtbase = ioremap(adev->res.start, resource_size(&adev->res));
2218 if (pl022->virtbase == NULL) {
2219 status = -ENOMEM;
2220 goto err_no_ioremap;
2221 }
2222 printk(KERN_INFO "pl022: mapped registers from 0x%08x to %p\n",
2223 adev->res.start, pl022->virtbase);
2224
2225 pl022->clk = clk_get(&adev->dev, NULL);
2226 if (IS_ERR(pl022->clk)) {
2227 status = PTR_ERR(pl022->clk);
2228 dev_err(&adev->dev, "could not retrieve SSP/SPI bus clock\n");
2229 goto err_no_clk;
2230 }
Russell King7ff6bcf2011-09-22 14:27:11 +01002231
2232 status = clk_prepare(pl022->clk);
2233 if (status) {
2234 dev_err(&adev->dev, "could not prepare SSP/SPI bus clock\n");
2235 goto err_clk_prep;
2236 }
2237
Ulf Hansson71e63e72011-11-04 08:10:09 +01002238 status = clk_enable(pl022->clk);
2239 if (status) {
2240 dev_err(&adev->dev, "could not enable SSP/SPI bus clock\n");
2241 goto err_no_clk_en;
2242 }
2243
Linus Walleijb43d65f2009-06-09 08:11:42 +01002244 /* Disable SSP */
Linus Walleijb43d65f2009-06-09 08:11:42 +01002245 writew((readw(SSP_CR1(pl022->virtbase)) & (~SSP_CR1_MASK_SSE)),
2246 SSP_CR1(pl022->virtbase));
2247 load_ssp_default_config(pl022);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002248
2249 status = request_irq(adev->irq[0], pl022_interrupt_handler, 0, "pl022",
2250 pl022);
2251 if (status < 0) {
2252 dev_err(&adev->dev, "probe - cannot get IRQ (%d)\n", status);
2253 goto err_no_irq;
2254 }
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09002255
2256 /* Get DMA channels */
2257 if (platform_info->enable_dma) {
2258 status = pl022_dma_probe(pl022);
2259 if (status != 0)
Viresh Kumar43c64012011-05-16 09:40:10 +05302260 platform_info->enable_dma = 0;
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09002261 }
2262
Linus Walleijb43d65f2009-06-09 08:11:42 +01002263 /* Initialize and start queue */
2264 status = init_queue(pl022);
2265 if (status != 0) {
2266 dev_err(&adev->dev, "probe - problem initializing queue\n");
2267 goto err_init_queue;
2268 }
2269 status = start_queue(pl022);
2270 if (status != 0) {
2271 dev_err(&adev->dev, "probe - problem starting queue\n");
2272 goto err_start_queue;
2273 }
2274 /* Register with the SPI framework */
2275 amba_set_drvdata(adev, pl022);
2276 status = spi_register_master(master);
2277 if (status != 0) {
2278 dev_err(&adev->dev,
2279 "probe - problem registering spi master\n");
2280 goto err_spi_register;
2281 }
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002282 dev_dbg(dev, "probe succeeded\n");
Russell King92b97f02011-08-14 09:13:48 +01002283
2284 /* let runtime pm put suspend */
Chris Blair53e4ace2011-11-08 08:54:46 +00002285 if (platform_info->autosuspend_delay > 0) {
2286 dev_info(&adev->dev,
2287 "will use autosuspend for runtime pm, delay %dms\n",
2288 platform_info->autosuspend_delay);
2289 pm_runtime_set_autosuspend_delay(dev,
2290 platform_info->autosuspend_delay);
2291 pm_runtime_use_autosuspend(dev);
2292 pm_runtime_put_autosuspend(dev);
2293 } else {
2294 pm_runtime_put(dev);
2295 }
Linus Walleijb43d65f2009-06-09 08:11:42 +01002296 return 0;
2297
2298 err_spi_register:
2299 err_start_queue:
2300 err_init_queue:
2301 destroy_queue(pl022);
Viresh Kumar3e3ea712011-08-10 14:20:58 +05302302 if (platform_info->enable_dma)
2303 pl022_dma_remove(pl022);
2304
Linus Walleijb43d65f2009-06-09 08:11:42 +01002305 free_irq(adev->irq[0], pl022);
2306 err_no_irq:
Ulf Hansson71e63e72011-11-04 08:10:09 +01002307 clk_disable(pl022->clk);
2308 err_no_clk_en:
Russell King7ff6bcf2011-09-22 14:27:11 +01002309 clk_unprepare(pl022->clk);
2310 err_clk_prep:
Linus Walleijb43d65f2009-06-09 08:11:42 +01002311 clk_put(pl022->clk);
2312 err_no_clk:
2313 iounmap(pl022->virtbase);
2314 err_no_ioremap:
2315 amba_release_regions(adev);
2316 err_no_ioregion:
2317 spi_master_put(master);
2318 err_no_master:
2319 err_no_pdata:
2320 return status;
2321}
2322
Kevin Wellsb4225882010-07-27 16:39:30 +00002323static int __devexit
Linus Walleijb43d65f2009-06-09 08:11:42 +01002324pl022_remove(struct amba_device *adev)
2325{
2326 struct pl022 *pl022 = amba_get_drvdata(adev);
Linus Walleij50658b62011-08-02 11:29:24 +02002327
Linus Walleijb43d65f2009-06-09 08:11:42 +01002328 if (!pl022)
2329 return 0;
2330
Russell King92b97f02011-08-14 09:13:48 +01002331 /*
2332 * undo pm_runtime_put() in probe. I assume that we're not
2333 * accessing the primecell here.
2334 */
2335 pm_runtime_get_noresume(&adev->dev);
2336
Linus Walleijb43d65f2009-06-09 08:11:42 +01002337 /* Remove the queue */
Linus Walleij50658b62011-08-02 11:29:24 +02002338 if (destroy_queue(pl022) != 0)
2339 dev_err(&adev->dev, "queue remove failed\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01002340 load_ssp_default_config(pl022);
Viresh Kumar3e3ea712011-08-10 14:20:58 +05302341 if (pl022->master_info->enable_dma)
2342 pl022_dma_remove(pl022);
2343
Linus Walleijb43d65f2009-06-09 08:11:42 +01002344 free_irq(adev->irq[0], pl022);
2345 clk_disable(pl022->clk);
Russell King7ff6bcf2011-09-22 14:27:11 +01002346 clk_unprepare(pl022->clk);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002347 clk_put(pl022->clk);
2348 iounmap(pl022->virtbase);
2349 amba_release_regions(adev);
2350 tasklet_disable(&pl022->pump_transfers);
2351 spi_unregister_master(pl022->master);
2352 spi_master_put(pl022->master);
2353 amba_set_drvdata(adev, NULL);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002354 return 0;
2355}
2356
Russell King92b97f02011-08-14 09:13:48 +01002357#ifdef CONFIG_SUSPEND
Peter Hüwe6cfa6272011-09-05 21:07:23 +01002358static int pl022_suspend(struct device *dev)
Linus Walleijb43d65f2009-06-09 08:11:42 +01002359{
Russell King92b97f02011-08-14 09:13:48 +01002360 struct pl022 *pl022 = dev_get_drvdata(dev);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002361 int status = 0;
2362
2363 status = stop_queue(pl022);
2364 if (status) {
Peter Hüwe6cfa6272011-09-05 21:07:23 +01002365 dev_warn(dev, "suspend cannot stop queue\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01002366 return status;
2367 }
2368
Peter Hüwe6cfa6272011-09-05 21:07:23 +01002369 dev_dbg(dev, "suspended\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01002370 return 0;
2371}
2372
Russell King92b97f02011-08-14 09:13:48 +01002373static int pl022_resume(struct device *dev)
Linus Walleijb43d65f2009-06-09 08:11:42 +01002374{
Russell King92b97f02011-08-14 09:13:48 +01002375 struct pl022 *pl022 = dev_get_drvdata(dev);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002376 int status = 0;
2377
2378 /* Start the queue running */
2379 status = start_queue(pl022);
2380 if (status)
Russell King92b97f02011-08-14 09:13:48 +01002381 dev_err(dev, "problem starting queue (%d)\n", status);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002382 else
Russell King92b97f02011-08-14 09:13:48 +01002383 dev_dbg(dev, "resumed\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01002384
2385 return status;
2386}
Linus Walleijb43d65f2009-06-09 08:11:42 +01002387#endif /* CONFIG_PM */
2388
Russell King92b97f02011-08-14 09:13:48 +01002389#ifdef CONFIG_PM_RUNTIME
2390static int pl022_runtime_suspend(struct device *dev)
2391{
2392 struct pl022 *pl022 = dev_get_drvdata(dev);
2393
2394 clk_disable(pl022->clk);
2395 amba_vcore_disable(pl022->adev);
2396
2397 return 0;
2398}
2399
2400static int pl022_runtime_resume(struct device *dev)
2401{
2402 struct pl022 *pl022 = dev_get_drvdata(dev);
2403
2404 amba_vcore_enable(pl022->adev);
2405 clk_enable(pl022->clk);
2406
2407 return 0;
2408}
2409#endif
2410
2411static const struct dev_pm_ops pl022_dev_pm_ops = {
2412 SET_SYSTEM_SLEEP_PM_OPS(pl022_suspend, pl022_resume)
2413 SET_RUNTIME_PM_OPS(pl022_runtime_suspend, pl022_runtime_resume, NULL)
2414};
2415
Linus Walleijb43d65f2009-06-09 08:11:42 +01002416static struct vendor_data vendor_arm = {
2417 .fifodepth = 8,
2418 .max_bpw = 16,
2419 .unidir = false,
Linus Walleij556f4ae2010-05-05 09:28:15 +00002420 .extended_cr = false,
Linus Walleij781c7b12010-05-07 08:40:53 +00002421 .pl023 = false,
Philippe Langlais06fb01f2011-03-23 11:05:16 +01002422 .loopback = true,
Linus Walleijb43d65f2009-06-09 08:11:42 +01002423};
2424
Linus Walleijb43d65f2009-06-09 08:11:42 +01002425static struct vendor_data vendor_st = {
2426 .fifodepth = 32,
2427 .max_bpw = 32,
2428 .unidir = false,
Linus Walleij556f4ae2010-05-05 09:28:15 +00002429 .extended_cr = true,
Linus Walleij781c7b12010-05-07 08:40:53 +00002430 .pl023 = false,
Philippe Langlais06fb01f2011-03-23 11:05:16 +01002431 .loopback = true,
Linus Walleij781c7b12010-05-07 08:40:53 +00002432};
2433
2434static struct vendor_data vendor_st_pl023 = {
2435 .fifodepth = 32,
2436 .max_bpw = 32,
2437 .unidir = false,
2438 .extended_cr = true,
2439 .pl023 = true,
Philippe Langlais06fb01f2011-03-23 11:05:16 +01002440 .loopback = false,
2441};
2442
2443static struct vendor_data vendor_db5500_pl023 = {
2444 .fifodepth = 32,
2445 .max_bpw = 32,
2446 .unidir = false,
2447 .extended_cr = true,
2448 .pl023 = true,
2449 .loopback = true,
Linus Walleijb43d65f2009-06-09 08:11:42 +01002450};
2451
2452static struct amba_id pl022_ids[] = {
2453 {
2454 /*
2455 * ARM PL022 variant, this has a 16bit wide
2456 * and 8 locations deep TX/RX FIFO
2457 */
2458 .id = 0x00041022,
2459 .mask = 0x000fffff,
2460 .data = &vendor_arm,
2461 },
2462 {
2463 /*
2464 * ST Micro derivative, this has 32bit wide
2465 * and 32 locations deep TX/RX FIFO
2466 */
Srinidhi Kasagare89e04f2009-10-05 06:13:53 +01002467 .id = 0x01080022,
Linus Walleijb43d65f2009-06-09 08:11:42 +01002468 .mask = 0xffffffff,
2469 .data = &vendor_st,
2470 },
Linus Walleij781c7b12010-05-07 08:40:53 +00002471 {
2472 /*
2473 * ST-Ericsson derivative "PL023" (this is not
2474 * an official ARM number), this is a PL022 SSP block
2475 * stripped to SPI mode only, it has 32bit wide
2476 * and 32 locations deep TX/RX FIFO but no extended
2477 * CR0/CR1 register
2478 */
Viresh Kumarf1e45f82011-08-10 14:20:54 +05302479 .id = 0x00080023,
2480 .mask = 0xffffffff,
2481 .data = &vendor_st_pl023,
Linus Walleij781c7b12010-05-07 08:40:53 +00002482 },
Philippe Langlais06fb01f2011-03-23 11:05:16 +01002483 {
2484 .id = 0x10080023,
2485 .mask = 0xffffffff,
2486 .data = &vendor_db5500_pl023,
2487 },
Linus Walleijb43d65f2009-06-09 08:11:42 +01002488 { 0, 0 },
2489};
2490
Dave Martin7eeac712011-10-05 15:15:22 +01002491MODULE_DEVICE_TABLE(amba, pl022_ids);
2492
Linus Walleijb43d65f2009-06-09 08:11:42 +01002493static struct amba_driver pl022_driver = {
2494 .drv = {
2495 .name = "ssp-pl022",
Russell King92b97f02011-08-14 09:13:48 +01002496 .pm = &pl022_dev_pm_ops,
Linus Walleijb43d65f2009-06-09 08:11:42 +01002497 },
2498 .id_table = pl022_ids,
2499 .probe = pl022_probe,
Kevin Wellsb4225882010-07-27 16:39:30 +00002500 .remove = __devexit_p(pl022_remove),
Linus Walleijb43d65f2009-06-09 08:11:42 +01002501};
2502
Linus Walleijb43d65f2009-06-09 08:11:42 +01002503static int __init pl022_init(void)
2504{
2505 return amba_driver_register(&pl022_driver);
2506}
Linus Walleij25c8e032010-09-06 11:02:12 +02002507subsys_initcall(pl022_init);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002508
2509static void __exit pl022_exit(void)
2510{
2511 amba_driver_unregister(&pl022_driver);
2512}
Linus Walleijb43d65f2009-06-09 08:11:42 +01002513module_exit(pl022_exit);
2514
2515MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
2516MODULE_DESCRIPTION("PL022 SSP Controller Driver");
2517MODULE_LICENSE("GPL");