blob: cf47802f00c982518e6cf4358e29783a8bb7ee3e [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>
Linus Walleijb43d65f2009-06-09 08:11:42 +010032#include <linux/delay.h>
33#include <linux/clk.h>
34#include <linux/err.h>
35#include <linux/amba/bus.h>
36#include <linux/amba/pl022.h>
37#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Linus Walleijb1b6b9a2010-09-29 17:31:35 +090039#include <linux/dmaengine.h>
40#include <linux/dma-mapping.h>
41#include <linux/scatterlist.h>
Rabin Vincentbcda6ff2011-06-16 10:14:40 +020042#include <linux/pm_runtime.h>
Roland Stiggef6f46de2012-08-22 15:49:17 +020043#include <linux/gpio.h>
Linus Walleijb43d65f2009-06-09 08:11:42 +010044
45/*
46 * This macro is used to define some register default values.
47 * reg is masked with mask, the OR:ed with an (again masked)
48 * val shifted sb steps to the left.
49 */
50#define SSP_WRITE_BITS(reg, val, mask, sb) \
51 ((reg) = (((reg) & ~(mask)) | (((val)<<(sb)) & (mask))))
52
53/*
54 * This macro is also used to define some default values.
55 * It will just shift val by sb steps to the left and mask
56 * the result with mask.
57 */
58#define GEN_MASK_BITS(val, mask, sb) \
59 (((val)<<(sb)) & (mask))
60
61#define DRIVE_TX 0
62#define DO_NOT_DRIVE_TX 1
63
64#define DO_NOT_QUEUE_DMA 0
65#define QUEUE_DMA 1
66
67#define RX_TRANSFER 1
68#define TX_TRANSFER 2
69
70/*
71 * Macros to access SSP Registers with their offsets
72 */
73#define SSP_CR0(r) (r + 0x000)
74#define SSP_CR1(r) (r + 0x004)
75#define SSP_DR(r) (r + 0x008)
76#define SSP_SR(r) (r + 0x00C)
77#define SSP_CPSR(r) (r + 0x010)
78#define SSP_IMSC(r) (r + 0x014)
79#define SSP_RIS(r) (r + 0x018)
80#define SSP_MIS(r) (r + 0x01C)
81#define SSP_ICR(r) (r + 0x020)
82#define SSP_DMACR(r) (r + 0x024)
83#define SSP_ITCR(r) (r + 0x080)
84#define SSP_ITIP(r) (r + 0x084)
85#define SSP_ITOP(r) (r + 0x088)
86#define SSP_TDR(r) (r + 0x08C)
87
88#define SSP_PID0(r) (r + 0xFE0)
89#define SSP_PID1(r) (r + 0xFE4)
90#define SSP_PID2(r) (r + 0xFE8)
91#define SSP_PID3(r) (r + 0xFEC)
92
93#define SSP_CID0(r) (r + 0xFF0)
94#define SSP_CID1(r) (r + 0xFF4)
95#define SSP_CID2(r) (r + 0xFF8)
96#define SSP_CID3(r) (r + 0xFFC)
97
98/*
99 * SSP Control Register 0 - SSP_CR0
100 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000101#define SSP_CR0_MASK_DSS (0x0FUL << 0)
102#define SSP_CR0_MASK_FRF (0x3UL << 4)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100103#define SSP_CR0_MASK_SPO (0x1UL << 6)
104#define SSP_CR0_MASK_SPH (0x1UL << 7)
105#define SSP_CR0_MASK_SCR (0xFFUL << 8)
Linus Walleij556f4ae2010-05-05 09:28:15 +0000106
107/*
108 * The ST version of this block moves som bits
109 * in SSP_CR0 and extends it to 32 bits
110 */
111#define SSP_CR0_MASK_DSS_ST (0x1FUL << 0)
112#define SSP_CR0_MASK_HALFDUP_ST (0x1UL << 5)
113#define SSP_CR0_MASK_CSS_ST (0x1FUL << 16)
114#define SSP_CR0_MASK_FRF_ST (0x3UL << 21)
115
Linus Walleijb43d65f2009-06-09 08:11:42 +0100116/*
117 * SSP Control Register 0 - SSP_CR1
118 */
119#define SSP_CR1_MASK_LBM (0x1UL << 0)
120#define SSP_CR1_MASK_SSE (0x1UL << 1)
121#define SSP_CR1_MASK_MS (0x1UL << 2)
122#define SSP_CR1_MASK_SOD (0x1UL << 3)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100123
124/*
Linus Walleij556f4ae2010-05-05 09:28:15 +0000125 * The ST version of this block adds some bits
126 * in SSP_CR1
Linus Walleijb43d65f2009-06-09 08:11:42 +0100127 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000128#define SSP_CR1_MASK_RENDN_ST (0x1UL << 4)
129#define SSP_CR1_MASK_TENDN_ST (0x1UL << 5)
130#define SSP_CR1_MASK_MWAIT_ST (0x1UL << 6)
131#define SSP_CR1_MASK_RXIFLSEL_ST (0x7UL << 7)
132#define SSP_CR1_MASK_TXIFLSEL_ST (0x7UL << 10)
Linus Walleij781c7b12010-05-07 08:40:53 +0000133/* This one is only in the PL023 variant */
134#define SSP_CR1_MASK_FBCLKDEL_ST (0x7UL << 13)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100135
136/*
137 * SSP Status Register - SSP_SR
138 */
139#define SSP_SR_MASK_TFE (0x1UL << 0) /* Transmit FIFO empty */
140#define SSP_SR_MASK_TNF (0x1UL << 1) /* Transmit FIFO not full */
141#define SSP_SR_MASK_RNE (0x1UL << 2) /* Receive FIFO not empty */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000142#define SSP_SR_MASK_RFF (0x1UL << 3) /* Receive FIFO full */
Linus Walleijb43d65f2009-06-09 08:11:42 +0100143#define SSP_SR_MASK_BSY (0x1UL << 4) /* Busy Flag */
144
145/*
146 * SSP Clock Prescale Register - SSP_CPSR
147 */
148#define SSP_CPSR_MASK_CPSDVSR (0xFFUL << 0)
149
150/*
151 * SSP Interrupt Mask Set/Clear Register - SSP_IMSC
152 */
153#define SSP_IMSC_MASK_RORIM (0x1UL << 0) /* Receive Overrun Interrupt mask */
154#define SSP_IMSC_MASK_RTIM (0x1UL << 1) /* Receive timeout Interrupt mask */
155#define SSP_IMSC_MASK_RXIM (0x1UL << 2) /* Receive FIFO Interrupt mask */
156#define SSP_IMSC_MASK_TXIM (0x1UL << 3) /* Transmit FIFO Interrupt mask */
157
158/*
159 * SSP Raw Interrupt Status Register - SSP_RIS
160 */
161/* Receive Overrun Raw Interrupt status */
162#define SSP_RIS_MASK_RORRIS (0x1UL << 0)
163/* Receive Timeout Raw Interrupt status */
164#define SSP_RIS_MASK_RTRIS (0x1UL << 1)
165/* Receive FIFO Raw Interrupt status */
166#define SSP_RIS_MASK_RXRIS (0x1UL << 2)
167/* Transmit FIFO Raw Interrupt status */
168#define SSP_RIS_MASK_TXRIS (0x1UL << 3)
169
170/*
171 * SSP Masked Interrupt Status Register - SSP_MIS
172 */
173/* Receive Overrun Masked Interrupt status */
174#define SSP_MIS_MASK_RORMIS (0x1UL << 0)
175/* Receive Timeout Masked Interrupt status */
176#define SSP_MIS_MASK_RTMIS (0x1UL << 1)
177/* Receive FIFO Masked Interrupt status */
178#define SSP_MIS_MASK_RXMIS (0x1UL << 2)
179/* Transmit FIFO Masked Interrupt status */
180#define SSP_MIS_MASK_TXMIS (0x1UL << 3)
181
182/*
183 * SSP Interrupt Clear Register - SSP_ICR
184 */
185/* Receive Overrun Raw Clear Interrupt bit */
186#define SSP_ICR_MASK_RORIC (0x1UL << 0)
187/* Receive Timeout Clear Interrupt bit */
188#define SSP_ICR_MASK_RTIC (0x1UL << 1)
189
190/*
191 * SSP DMA Control Register - SSP_DMACR
192 */
193/* Receive DMA Enable bit */
194#define SSP_DMACR_MASK_RXDMAE (0x1UL << 0)
195/* Transmit DMA Enable bit */
196#define SSP_DMACR_MASK_TXDMAE (0x1UL << 1)
197
198/*
199 * SSP Integration Test control Register - SSP_ITCR
200 */
201#define SSP_ITCR_MASK_ITEN (0x1UL << 0)
202#define SSP_ITCR_MASK_TESTFIFO (0x1UL << 1)
203
204/*
205 * SSP Integration Test Input Register - SSP_ITIP
206 */
207#define ITIP_MASK_SSPRXD (0x1UL << 0)
208#define ITIP_MASK_SSPFSSIN (0x1UL << 1)
209#define ITIP_MASK_SSPCLKIN (0x1UL << 2)
210#define ITIP_MASK_RXDMAC (0x1UL << 3)
211#define ITIP_MASK_TXDMAC (0x1UL << 4)
212#define ITIP_MASK_SSPTXDIN (0x1UL << 5)
213
214/*
215 * SSP Integration Test output Register - SSP_ITOP
216 */
217#define ITOP_MASK_SSPTXD (0x1UL << 0)
218#define ITOP_MASK_SSPFSSOUT (0x1UL << 1)
219#define ITOP_MASK_SSPCLKOUT (0x1UL << 2)
220#define ITOP_MASK_SSPOEn (0x1UL << 3)
221#define ITOP_MASK_SSPCTLOEn (0x1UL << 4)
222#define ITOP_MASK_RORINTR (0x1UL << 5)
223#define ITOP_MASK_RTINTR (0x1UL << 6)
224#define ITOP_MASK_RXINTR (0x1UL << 7)
225#define ITOP_MASK_TXINTR (0x1UL << 8)
226#define ITOP_MASK_INTR (0x1UL << 9)
227#define ITOP_MASK_RXDMABREQ (0x1UL << 10)
228#define ITOP_MASK_RXDMASREQ (0x1UL << 11)
229#define ITOP_MASK_TXDMABREQ (0x1UL << 12)
230#define ITOP_MASK_TXDMASREQ (0x1UL << 13)
231
232/*
233 * SSP Test Data Register - SSP_TDR
234 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000235#define TDR_MASK_TESTDATA (0xFFFFFFFF)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100236
237/*
238 * Message State
239 * we use the spi_message.state (void *) pointer to
240 * hold a single state value, that's why all this
241 * (void *) casting is done here.
242 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000243#define STATE_START ((void *) 0)
244#define STATE_RUNNING ((void *) 1)
245#define STATE_DONE ((void *) 2)
246#define STATE_ERROR ((void *) -1)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100247
248/*
Linus Walleijb43d65f2009-06-09 08:11:42 +0100249 * SSP State - Whether Enabled or Disabled
250 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000251#define SSP_DISABLED (0)
252#define SSP_ENABLED (1)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100253
254/*
255 * SSP DMA State - Whether DMA Enabled or Disabled
256 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000257#define SSP_DMA_DISABLED (0)
258#define SSP_DMA_ENABLED (1)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100259
260/*
261 * SSP Clock Defaults
262 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000263#define SSP_DEFAULT_CLKRATE 0x2
264#define SSP_DEFAULT_PRESCALE 0x40
Linus Walleijb43d65f2009-06-09 08:11:42 +0100265
266/*
267 * SSP Clock Parameter ranges
268 */
269#define CPSDVR_MIN 0x02
270#define CPSDVR_MAX 0xFE
271#define SCR_MIN 0x00
272#define SCR_MAX 0xFF
273
274/*
275 * SSP Interrupt related Macros
276 */
277#define DEFAULT_SSP_REG_IMSC 0x0UL
278#define DISABLE_ALL_INTERRUPTS DEFAULT_SSP_REG_IMSC
279#define ENABLE_ALL_INTERRUPTS (~DEFAULT_SSP_REG_IMSC)
280
281#define CLEAR_ALL_INTERRUPTS 0x3
282
Magnus Templinga18c2662011-05-19 18:05:34 +0200283#define SPI_POLLING_TIMEOUT 1000
284
Linus Walleijb43d65f2009-06-09 08:11:42 +0100285/*
286 * The type of reading going on on this chip
287 */
288enum ssp_reading {
289 READING_NULL,
290 READING_U8,
291 READING_U16,
292 READING_U32
293};
294
295/**
296 * The type of writing going on on this chip
297 */
298enum ssp_writing {
299 WRITING_NULL,
300 WRITING_U8,
301 WRITING_U16,
302 WRITING_U32
303};
304
305/**
306 * struct vendor_data - vendor-specific config parameters
307 * for PL022 derivates
308 * @fifodepth: depth of FIFOs (both)
309 * @max_bpw: maximum number of bits per word
310 * @unidir: supports unidirection transfers
Linus Walleij556f4ae2010-05-05 09:28:15 +0000311 * @extended_cr: 32 bit wide control register 0 with extra
312 * features and extra features in CR1 as found in the ST variants
Linus Walleij781c7b12010-05-07 08:40:53 +0000313 * @pl023: supports a subset of the ST extensions called "PL023"
Linus Walleijb43d65f2009-06-09 08:11:42 +0100314 */
315struct vendor_data {
316 int fifodepth;
317 int max_bpw;
318 bool unidir;
Linus Walleij556f4ae2010-05-05 09:28:15 +0000319 bool extended_cr;
Linus Walleij781c7b12010-05-07 08:40:53 +0000320 bool pl023;
Philippe Langlais06fb01f2011-03-23 11:05:16 +0100321 bool loopback;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100322};
323
324/**
325 * struct pl022 - This is the private SSP driver data structure
326 * @adev: AMBA device model hookup
Linus Walleij12e8b322011-02-08 13:03:55 +0100327 * @vendor: vendor data for the IP block
328 * @phybase: the physical memory where the SSP device resides
329 * @virtbase: the virtual memory where the SSP is mapped
330 * @clk: outgoing clock "SPICLK" for the SPI bus
Linus Walleijb43d65f2009-06-09 08:11:42 +0100331 * @master: SPI framework hookup
332 * @master_info: controller-specific data from machine setup
Chris Blair14af60b2012-02-02 13:59:34 +0100333 * @kworker: thread struct for message pump
334 * @kworker_task: pointer to task for message pump kworker thread
335 * @pump_messages: work struct for scheduling work to the message pump
Linus Walleij12e8b322011-02-08 13:03:55 +0100336 * @queue_lock: spinlock to syncronise access to message queue
337 * @queue: message queue
Chris Blair14af60b2012-02-02 13:59:34 +0100338 * @busy: message pump is busy
339 * @running: message pump is running
Linus Walleijb43d65f2009-06-09 08:11:42 +0100340 * @pump_transfers: Tasklet used in Interrupt Transfer mode
341 * @cur_msg: Pointer to current spi_message being processed
342 * @cur_transfer: Pointer to current spi_transfer
343 * @cur_chip: pointer to current clients chip(assigned from controller_state)
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530344 * @next_msg_cs_active: the next message in the queue has been examined
345 * and it was found that it uses the same chip select as the previous
346 * message, so we left it active after the previous transfer, and it's
347 * active already.
Linus Walleijb43d65f2009-06-09 08:11:42 +0100348 * @tx: current position in TX buffer to be read
349 * @tx_end: end position in TX buffer to be read
350 * @rx: current position in RX buffer to be written
351 * @rx_end: end position in RX buffer to be written
Linus Walleij12e8b322011-02-08 13:03:55 +0100352 * @read: the type of read currently going on
353 * @write: the type of write currently going on
354 * @exp_fifo_level: expected FIFO level
355 * @dma_rx_channel: optional channel for RX DMA
356 * @dma_tx_channel: optional channel for TX DMA
357 * @sgt_rx: scattertable for the RX transfer
358 * @sgt_tx: scattertable for the TX transfer
359 * @dummypage: a dummy page used for driving data on the bus with DMA
Roland Stiggef6f46de2012-08-22 15:49:17 +0200360 * @cur_cs: current chip select (gpio)
361 * @chipselects: list of chipselects (gpios)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100362 */
363struct pl022 {
364 struct amba_device *adev;
365 struct vendor_data *vendor;
366 resource_size_t phybase;
367 void __iomem *virtbase;
368 struct clk *clk;
369 struct spi_master *master;
370 struct pl022_ssp_controller *master_info;
Linus Walleijffbbdd22012-02-22 10:05:38 +0100371 /* Message per-transfer pump */
Linus Walleijb43d65f2009-06-09 08:11:42 +0100372 struct tasklet_struct pump_transfers;
373 struct spi_message *cur_msg;
374 struct spi_transfer *cur_transfer;
375 struct chip_data *cur_chip;
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530376 bool next_msg_cs_active;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100377 void *tx;
378 void *tx_end;
379 void *rx;
380 void *rx_end;
381 enum ssp_reading read;
382 enum ssp_writing write;
Linus Walleijfc054752010-01-22 13:53:30 +0100383 u32 exp_fifo_level;
Linus Walleij083be3f2011-06-16 10:14:28 +0200384 enum ssp_rx_level_trig rx_lev_trig;
385 enum ssp_tx_level_trig tx_lev_trig;
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900386 /* DMA settings */
387#ifdef CONFIG_DMA_ENGINE
388 struct dma_chan *dma_rx_channel;
389 struct dma_chan *dma_tx_channel;
390 struct sg_table sgt_rx;
391 struct sg_table sgt_tx;
392 char *dummypage;
Linus Walleijffbbdd22012-02-22 10:05:38 +0100393 bool dma_running;
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900394#endif
Roland Stiggef6f46de2012-08-22 15:49:17 +0200395 int cur_cs;
396 int *chipselects;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100397};
398
399/**
400 * struct chip_data - To maintain runtime state of SSP for each client chip
Linus Walleij556f4ae2010-05-05 09:28:15 +0000401 * @cr0: Value of control register CR0 of SSP - on later ST variants this
402 * register is 32 bits wide rather than just 16
Linus Walleijb43d65f2009-06-09 08:11:42 +0100403 * @cr1: Value of control register CR1 of SSP
404 * @dmacr: Value of DMA control Register of SSP
405 * @cpsr: Value of Clock prescale register
406 * @n_bytes: how many bytes(power of 2) reqd for a given data width of client
407 * @enable_dma: Whether to enable DMA or not
Linus Walleijb43d65f2009-06-09 08:11:42 +0100408 * @read: function ptr to be used to read when doing xfer for this chip
Linus Walleij12e8b322011-02-08 13:03:55 +0100409 * @write: function ptr to be used to write when doing xfer for this chip
Linus Walleijb43d65f2009-06-09 08:11:42 +0100410 * @cs_control: chip select callback provided by chip
411 * @xfer_type: polling/interrupt/DMA
412 *
413 * Runtime state of the SSP controller, maintained per chip,
414 * This would be set according to the current message that would be served
415 */
416struct chip_data {
Linus Walleij556f4ae2010-05-05 09:28:15 +0000417 u32 cr0;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100418 u16 cr1;
419 u16 dmacr;
420 u16 cpsr;
421 u8 n_bytes;
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900422 bool enable_dma;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100423 enum ssp_reading read;
424 enum ssp_writing write;
425 void (*cs_control) (u32 command);
426 int xfer_type;
427};
428
429/**
430 * null_cs_control - Dummy chip select function
431 * @command: select/delect the chip
432 *
433 * If no chip select function is provided by client this is used as dummy
434 * chip select
435 */
436static void null_cs_control(u32 command)
437{
438 pr_debug("pl022: dummy chip select control, CS=0x%x\n", command);
439}
440
Roland Stiggef6f46de2012-08-22 15:49:17 +0200441static void pl022_cs_control(struct pl022 *pl022, u32 command)
442{
443 if (gpio_is_valid(pl022->cur_cs))
444 gpio_set_value(pl022->cur_cs, command);
445 else
446 pl022->cur_chip->cs_control(command);
447}
448
Linus Walleijb43d65f2009-06-09 08:11:42 +0100449/**
450 * giveback - current spi_message is over, schedule next message and call
451 * callback of this message. Assumes that caller already
452 * set message->status; dma and pio irqs are blocked
453 * @pl022: SSP driver private data structure
454 */
455static void giveback(struct pl022 *pl022)
456{
457 struct spi_transfer *last_transfer;
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530458 pl022->next_msg_cs_active = false;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100459
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530460 last_transfer = list_entry(pl022->cur_msg->transfers.prev,
Linus Walleijb43d65f2009-06-09 08:11:42 +0100461 struct spi_transfer,
462 transfer_list);
463
464 /* Delay if requested before any change in chip select */
465 if (last_transfer->delay_usecs)
466 /*
467 * FIXME: This runs in interrupt context.
468 * Is this really smart?
469 */
470 udelay(last_transfer->delay_usecs);
471
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530472 if (!last_transfer->cs_change) {
Linus Walleijb43d65f2009-06-09 08:11:42 +0100473 struct spi_message *next_msg;
474
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530475 /*
476 * cs_change was not set. We can keep the chip select
477 * enabled if there is message in the queue and it is
478 * for the same spi device.
Linus Walleijb43d65f2009-06-09 08:11:42 +0100479 *
480 * We cannot postpone this until pump_messages, because
481 * after calling msg->complete (below) the driver that
482 * sent the current message could be unloaded, which
483 * could invalidate the cs_control() callback...
484 */
Linus Walleijb43d65f2009-06-09 08:11:42 +0100485 /* get a pointer to the next message, if any */
Linus Walleijffbbdd22012-02-22 10:05:38 +0100486 next_msg = spi_get_next_queued_message(pl022->master);
Linus Walleijb43d65f2009-06-09 08:11:42 +0100487
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530488 /*
489 * see if the next and current messages point
490 * to the same spi device.
Linus Walleijb43d65f2009-06-09 08:11:42 +0100491 */
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530492 if (next_msg && next_msg->spi != pl022->cur_msg->spi)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100493 next_msg = NULL;
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530494 if (!next_msg || pl022->cur_msg->state == STATE_ERROR)
Roland Stiggef6f46de2012-08-22 15:49:17 +0200495 pl022_cs_control(pl022, SSP_CHIP_DESELECT);
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530496 else
497 pl022->next_msg_cs_active = true;
Linus Walleijffbbdd22012-02-22 10:05:38 +0100498
Linus Walleijb43d65f2009-06-09 08:11:42 +0100499 }
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530500
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +0530501 pl022->cur_msg = NULL;
502 pl022->cur_transfer = NULL;
503 pl022->cur_chip = NULL;
Linus Walleijffbbdd22012-02-22 10:05:38 +0100504 spi_finalize_current_message(pl022->master);
Virupax Sadashivpetimathfd316942012-06-12 15:10:58 +0200505
506 /* disable the SPI/SSP operation */
507 writew((readw(SSP_CR1(pl022->virtbase)) &
508 (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
509
Linus Walleijb43d65f2009-06-09 08:11:42 +0100510}
511
512/**
513 * flush - flush the FIFO to reach a clean state
514 * @pl022: SSP driver private data structure
515 */
516static int flush(struct pl022 *pl022)
517{
518 unsigned long limit = loops_per_jiffy << 1;
519
520 dev_dbg(&pl022->adev->dev, "flush\n");
521 do {
522 while (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
523 readw(SSP_DR(pl022->virtbase));
524 } while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_BSY) && limit--);
Linus Walleijfc054752010-01-22 13:53:30 +0100525
526 pl022->exp_fifo_level = 0;
527
Linus Walleijb43d65f2009-06-09 08:11:42 +0100528 return limit;
529}
530
531/**
532 * restore_state - Load configuration of current chip
533 * @pl022: SSP driver private data structure
534 */
535static void restore_state(struct pl022 *pl022)
536{
537 struct chip_data *chip = pl022->cur_chip;
538
Linus Walleij556f4ae2010-05-05 09:28:15 +0000539 if (pl022->vendor->extended_cr)
540 writel(chip->cr0, SSP_CR0(pl022->virtbase));
541 else
542 writew(chip->cr0, SSP_CR0(pl022->virtbase));
Linus Walleijb43d65f2009-06-09 08:11:42 +0100543 writew(chip->cr1, SSP_CR1(pl022->virtbase));
544 writew(chip->dmacr, SSP_DMACR(pl022->virtbase));
545 writew(chip->cpsr, SSP_CPSR(pl022->virtbase));
546 writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
547 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
548}
549
Linus Walleijb43d65f2009-06-09 08:11:42 +0100550/*
551 * Default SSP Register Values
552 */
553#define DEFAULT_SSP_REG_CR0 ( \
554 GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS, 0) | \
Linus Walleij556f4ae2010-05-05 09:28:15 +0000555 GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF, 4) | \
Linus Walleijb43d65f2009-06-09 08:11:42 +0100556 GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
Linus Walleijee2b8052009-08-15 15:12:05 +0100557 GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
Linus Walleij556f4ae2010-05-05 09:28:15 +0000558 GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
559)
560
561/* ST versions have slightly different bit layout */
562#define DEFAULT_SSP_REG_CR0_ST ( \
563 GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
564 GEN_MASK_BITS(SSP_MICROWIRE_CHANNEL_FULL_DUPLEX, SSP_CR0_MASK_HALFDUP_ST, 5) | \
565 GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
566 GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
567 GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) | \
568 GEN_MASK_BITS(SSP_BITS_8, SSP_CR0_MASK_CSS_ST, 16) | \
569 GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF_ST, 21) \
Linus Walleijb43d65f2009-06-09 08:11:42 +0100570)
571
Linus Walleij781c7b12010-05-07 08:40:53 +0000572/* The PL023 version is slightly different again */
573#define DEFAULT_SSP_REG_CR0_ST_PL023 ( \
574 GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
575 GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
576 GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
577 GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
578)
579
Linus Walleijb43d65f2009-06-09 08:11:42 +0100580#define DEFAULT_SSP_REG_CR1 ( \
581 GEN_MASK_BITS(LOOPBACK_DISABLED, SSP_CR1_MASK_LBM, 0) | \
582 GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
583 GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
Linus Walleij556f4ae2010-05-05 09:28:15 +0000584 GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) \
Linus Walleijb43d65f2009-06-09 08:11:42 +0100585)
586
Linus Walleij556f4ae2010-05-05 09:28:15 +0000587/* ST versions extend this register to use all 16 bits */
588#define DEFAULT_SSP_REG_CR1_ST ( \
589 DEFAULT_SSP_REG_CR1 | \
590 GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
591 GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
592 GEN_MASK_BITS(SSP_MWIRE_WAIT_ZERO, SSP_CR1_MASK_MWAIT_ST, 6) |\
593 GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
594 GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) \
595)
596
Linus Walleij781c7b12010-05-07 08:40:53 +0000597/*
598 * The PL023 variant has further differences: no loopback mode, no microwire
599 * support, and a new clock feedback delay setting.
600 */
601#define DEFAULT_SSP_REG_CR1_ST_PL023 ( \
602 GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
603 GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
604 GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) | \
605 GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
606 GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
607 GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
608 GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) | \
609 GEN_MASK_BITS(SSP_FEEDBACK_CLK_DELAY_NONE, SSP_CR1_MASK_FBCLKDEL_ST, 13) \
610)
Linus Walleij556f4ae2010-05-05 09:28:15 +0000611
Linus Walleijb43d65f2009-06-09 08:11:42 +0100612#define DEFAULT_SSP_REG_CPSR ( \
Linus Walleij556f4ae2010-05-05 09:28:15 +0000613 GEN_MASK_BITS(SSP_DEFAULT_PRESCALE, SSP_CPSR_MASK_CPSDVSR, 0) \
Linus Walleijb43d65f2009-06-09 08:11:42 +0100614)
615
616#define DEFAULT_SSP_REG_DMACR (\
617 GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_RXDMAE, 0) | \
618 GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_TXDMAE, 1) \
619)
620
Linus Walleij781c7b12010-05-07 08:40:53 +0000621/**
622 * load_ssp_default_config - Load default configuration for SSP
623 * @pl022: SSP driver private data structure
624 */
Linus Walleijb43d65f2009-06-09 08:11:42 +0100625static void load_ssp_default_config(struct pl022 *pl022)
626{
Linus Walleij781c7b12010-05-07 08:40:53 +0000627 if (pl022->vendor->pl023) {
628 writel(DEFAULT_SSP_REG_CR0_ST_PL023, SSP_CR0(pl022->virtbase));
629 writew(DEFAULT_SSP_REG_CR1_ST_PL023, SSP_CR1(pl022->virtbase));
630 } else if (pl022->vendor->extended_cr) {
Linus Walleij556f4ae2010-05-05 09:28:15 +0000631 writel(DEFAULT_SSP_REG_CR0_ST, SSP_CR0(pl022->virtbase));
632 writew(DEFAULT_SSP_REG_CR1_ST, SSP_CR1(pl022->virtbase));
633 } else {
634 writew(DEFAULT_SSP_REG_CR0, SSP_CR0(pl022->virtbase));
635 writew(DEFAULT_SSP_REG_CR1, SSP_CR1(pl022->virtbase));
636 }
Linus Walleijb43d65f2009-06-09 08:11:42 +0100637 writew(DEFAULT_SSP_REG_DMACR, SSP_DMACR(pl022->virtbase));
638 writew(DEFAULT_SSP_REG_CPSR, SSP_CPSR(pl022->virtbase));
639 writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
640 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
641}
642
643/**
644 * This will write to TX and read from RX according to the parameters
645 * set in pl022.
646 */
647static void readwriter(struct pl022 *pl022)
648{
649
650 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300651 * The FIFO depth is different between primecell variants.
Linus Walleijb43d65f2009-06-09 08:11:42 +0100652 * I believe filling in too much in the FIFO might cause
653 * errons in 8bit wide transfers on ARM variants (just 8 words
654 * FIFO, means only 8x8 = 64 bits in FIFO) at least.
655 *
Linus Walleijfc054752010-01-22 13:53:30 +0100656 * To prevent this issue, the TX FIFO is only filled to the
657 * unused RX FIFO fill length, regardless of what the TX
658 * FIFO status flag indicates.
Linus Walleijb43d65f2009-06-09 08:11:42 +0100659 */
660 dev_dbg(&pl022->adev->dev,
661 "%s, rx: %p, rxend: %p, tx: %p, txend: %p\n",
662 __func__, pl022->rx, pl022->rx_end, pl022->tx, pl022->tx_end);
663
664 /* Read as much as you can */
665 while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
666 && (pl022->rx < pl022->rx_end)) {
667 switch (pl022->read) {
668 case READING_NULL:
669 readw(SSP_DR(pl022->virtbase));
670 break;
671 case READING_U8:
672 *(u8 *) (pl022->rx) =
673 readw(SSP_DR(pl022->virtbase)) & 0xFFU;
674 break;
675 case READING_U16:
676 *(u16 *) (pl022->rx) =
677 (u16) readw(SSP_DR(pl022->virtbase));
678 break;
679 case READING_U32:
680 *(u32 *) (pl022->rx) =
681 readl(SSP_DR(pl022->virtbase));
682 break;
683 }
684 pl022->rx += (pl022->cur_chip->n_bytes);
Linus Walleijfc054752010-01-22 13:53:30 +0100685 pl022->exp_fifo_level--;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100686 }
687 /*
Linus Walleijfc054752010-01-22 13:53:30 +0100688 * Write as much as possible up to the RX FIFO size
Linus Walleijb43d65f2009-06-09 08:11:42 +0100689 */
Linus Walleijfc054752010-01-22 13:53:30 +0100690 while ((pl022->exp_fifo_level < pl022->vendor->fifodepth)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100691 && (pl022->tx < pl022->tx_end)) {
692 switch (pl022->write) {
693 case WRITING_NULL:
694 writew(0x0, SSP_DR(pl022->virtbase));
695 break;
696 case WRITING_U8:
697 writew(*(u8 *) (pl022->tx), SSP_DR(pl022->virtbase));
698 break;
699 case WRITING_U16:
700 writew((*(u16 *) (pl022->tx)), SSP_DR(pl022->virtbase));
701 break;
702 case WRITING_U32:
703 writel(*(u32 *) (pl022->tx), SSP_DR(pl022->virtbase));
704 break;
705 }
706 pl022->tx += (pl022->cur_chip->n_bytes);
Linus Walleijfc054752010-01-22 13:53:30 +0100707 pl022->exp_fifo_level++;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100708 /*
709 * This inner reader takes care of things appearing in the RX
710 * FIFO as we're transmitting. This will happen a lot since the
711 * clock starts running when you put things into the TX FIFO,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300712 * and then things are continuously clocked into the RX FIFO.
Linus Walleijb43d65f2009-06-09 08:11:42 +0100713 */
714 while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
715 && (pl022->rx < pl022->rx_end)) {
716 switch (pl022->read) {
717 case READING_NULL:
718 readw(SSP_DR(pl022->virtbase));
719 break;
720 case READING_U8:
721 *(u8 *) (pl022->rx) =
722 readw(SSP_DR(pl022->virtbase)) & 0xFFU;
723 break;
724 case READING_U16:
725 *(u16 *) (pl022->rx) =
726 (u16) readw(SSP_DR(pl022->virtbase));
727 break;
728 case READING_U32:
729 *(u32 *) (pl022->rx) =
730 readl(SSP_DR(pl022->virtbase));
731 break;
732 }
733 pl022->rx += (pl022->cur_chip->n_bytes);
Linus Walleijfc054752010-01-22 13:53:30 +0100734 pl022->exp_fifo_level--;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100735 }
736 }
737 /*
738 * When we exit here the TX FIFO should be full and the RX FIFO
739 * should be empty
740 */
741}
742
Linus Walleijb43d65f2009-06-09 08:11:42 +0100743/**
744 * next_transfer - Move to the Next transfer in the current spi message
745 * @pl022: SSP driver private data structure
746 *
747 * This function moves though the linked list of spi transfers in the
748 * current spi message and returns with the state of current spi
749 * message i.e whether its last transfer is done(STATE_DONE) or
750 * Next transfer is ready(STATE_RUNNING)
751 */
752static void *next_transfer(struct pl022 *pl022)
753{
754 struct spi_message *msg = pl022->cur_msg;
755 struct spi_transfer *trans = pl022->cur_transfer;
756
757 /* Move to next transfer */
758 if (trans->transfer_list.next != &msg->transfers) {
759 pl022->cur_transfer =
760 list_entry(trans->transfer_list.next,
761 struct spi_transfer, transfer_list);
762 return STATE_RUNNING;
763 }
764 return STATE_DONE;
765}
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900766
767/*
768 * This DMA functionality is only compiled in if we have
769 * access to the generic DMA devices/DMA engine.
770 */
771#ifdef CONFIG_DMA_ENGINE
772static void unmap_free_dma_scatter(struct pl022 *pl022)
773{
774 /* Unmap and free the SG tables */
Linus Walleijb7298892010-12-22 23:13:07 +0100775 dma_unmap_sg(pl022->dma_tx_channel->device->dev, pl022->sgt_tx.sgl,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900776 pl022->sgt_tx.nents, DMA_TO_DEVICE);
Linus Walleijb7298892010-12-22 23:13:07 +0100777 dma_unmap_sg(pl022->dma_rx_channel->device->dev, pl022->sgt_rx.sgl,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900778 pl022->sgt_rx.nents, DMA_FROM_DEVICE);
779 sg_free_table(&pl022->sgt_rx);
780 sg_free_table(&pl022->sgt_tx);
781}
782
783static void dma_callback(void *data)
784{
785 struct pl022 *pl022 = data;
786 struct spi_message *msg = pl022->cur_msg;
787
788 BUG_ON(!pl022->sgt_rx.sgl);
789
790#ifdef VERBOSE_DEBUG
791 /*
792 * Optionally dump out buffers to inspect contents, this is
793 * good if you want to convince yourself that the loopback
794 * read/write contents are the same, when adopting to a new
795 * DMA engine.
796 */
797 {
798 struct scatterlist *sg;
799 unsigned int i;
800
801 dma_sync_sg_for_cpu(&pl022->adev->dev,
802 pl022->sgt_rx.sgl,
803 pl022->sgt_rx.nents,
804 DMA_FROM_DEVICE);
805
806 for_each_sg(pl022->sgt_rx.sgl, sg, pl022->sgt_rx.nents, i) {
807 dev_dbg(&pl022->adev->dev, "SPI RX SG ENTRY: %d", i);
808 print_hex_dump(KERN_ERR, "SPI RX: ",
809 DUMP_PREFIX_OFFSET,
810 16,
811 1,
812 sg_virt(sg),
813 sg_dma_len(sg),
814 1);
815 }
816 for_each_sg(pl022->sgt_tx.sgl, sg, pl022->sgt_tx.nents, i) {
817 dev_dbg(&pl022->adev->dev, "SPI TX SG ENTRY: %d", i);
818 print_hex_dump(KERN_ERR, "SPI TX: ",
819 DUMP_PREFIX_OFFSET,
820 16,
821 1,
822 sg_virt(sg),
823 sg_dma_len(sg),
824 1);
825 }
826 }
827#endif
828
829 unmap_free_dma_scatter(pl022);
830
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300831 /* Update total bytes transferred */
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900832 msg->actual_length += pl022->cur_transfer->len;
833 if (pl022->cur_transfer->cs_change)
Roland Stiggef6f46de2012-08-22 15:49:17 +0200834 pl022_cs_control(pl022, SSP_CHIP_DESELECT);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900835
836 /* Move to next transfer */
837 msg->state = next_transfer(pl022);
838 tasklet_schedule(&pl022->pump_transfers);
839}
840
841static void setup_dma_scatter(struct pl022 *pl022,
842 void *buffer,
843 unsigned int length,
844 struct sg_table *sgtab)
845{
846 struct scatterlist *sg;
847 int bytesleft = length;
848 void *bufp = buffer;
849 int mapbytes;
850 int i;
851
852 if (buffer) {
853 for_each_sg(sgtab->sgl, sg, sgtab->nents, i) {
854 /*
855 * If there are less bytes left than what fits
856 * in the current page (plus page alignment offset)
857 * we just feed in this, else we stuff in as much
858 * as we can.
859 */
860 if (bytesleft < (PAGE_SIZE - offset_in_page(bufp)))
861 mapbytes = bytesleft;
862 else
863 mapbytes = PAGE_SIZE - offset_in_page(bufp);
864 sg_set_page(sg, virt_to_page(bufp),
865 mapbytes, offset_in_page(bufp));
866 bufp += mapbytes;
867 bytesleft -= mapbytes;
868 dev_dbg(&pl022->adev->dev,
869 "set RX/TX target page @ %p, %d bytes, %d left\n",
870 bufp, mapbytes, bytesleft);
871 }
872 } else {
873 /* Map the dummy buffer on every page */
874 for_each_sg(sgtab->sgl, sg, sgtab->nents, i) {
875 if (bytesleft < PAGE_SIZE)
876 mapbytes = bytesleft;
877 else
878 mapbytes = PAGE_SIZE;
879 sg_set_page(sg, virt_to_page(pl022->dummypage),
880 mapbytes, 0);
881 bytesleft -= mapbytes;
882 dev_dbg(&pl022->adev->dev,
883 "set RX/TX to dummy page %d bytes, %d left\n",
884 mapbytes, bytesleft);
885
886 }
887 }
888 BUG_ON(bytesleft);
889}
890
891/**
892 * configure_dma - configures the channels for the next transfer
893 * @pl022: SSP driver's private data structure
894 */
895static int configure_dma(struct pl022 *pl022)
896{
897 struct dma_slave_config rx_conf = {
898 .src_addr = SSP_DR(pl022->phybase),
Vinod Koula485df42011-10-14 10:47:38 +0530899 .direction = DMA_DEV_TO_MEM,
Viresh Kumar258aea72012-02-01 16:12:19 +0530900 .device_fc = false,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900901 };
902 struct dma_slave_config tx_conf = {
903 .dst_addr = SSP_DR(pl022->phybase),
Vinod Koula485df42011-10-14 10:47:38 +0530904 .direction = DMA_MEM_TO_DEV,
Viresh Kumar258aea72012-02-01 16:12:19 +0530905 .device_fc = false,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900906 };
907 unsigned int pages;
908 int ret;
Linus Walleij082086f2010-12-22 23:13:37 +0100909 int rx_sglen, tx_sglen;
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900910 struct dma_chan *rxchan = pl022->dma_rx_channel;
911 struct dma_chan *txchan = pl022->dma_tx_channel;
912 struct dma_async_tx_descriptor *rxdesc;
913 struct dma_async_tx_descriptor *txdesc;
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900914
915 /* Check that the channels are available */
916 if (!rxchan || !txchan)
917 return -ENODEV;
918
Linus Walleij083be3f2011-06-16 10:14:28 +0200919 /*
920 * If supplied, the DMA burstsize should equal the FIFO trigger level.
921 * Notice that the DMA engine uses one-to-one mapping. Since we can
922 * not trigger on 2 elements this needs explicit mapping rather than
923 * calculation.
924 */
925 switch (pl022->rx_lev_trig) {
926 case SSP_RX_1_OR_MORE_ELEM:
927 rx_conf.src_maxburst = 1;
928 break;
929 case SSP_RX_4_OR_MORE_ELEM:
930 rx_conf.src_maxburst = 4;
931 break;
932 case SSP_RX_8_OR_MORE_ELEM:
933 rx_conf.src_maxburst = 8;
934 break;
935 case SSP_RX_16_OR_MORE_ELEM:
936 rx_conf.src_maxburst = 16;
937 break;
938 case SSP_RX_32_OR_MORE_ELEM:
939 rx_conf.src_maxburst = 32;
940 break;
941 default:
942 rx_conf.src_maxburst = pl022->vendor->fifodepth >> 1;
943 break;
944 }
945
946 switch (pl022->tx_lev_trig) {
947 case SSP_TX_1_OR_MORE_EMPTY_LOC:
948 tx_conf.dst_maxburst = 1;
949 break;
950 case SSP_TX_4_OR_MORE_EMPTY_LOC:
951 tx_conf.dst_maxburst = 4;
952 break;
953 case SSP_TX_8_OR_MORE_EMPTY_LOC:
954 tx_conf.dst_maxburst = 8;
955 break;
956 case SSP_TX_16_OR_MORE_EMPTY_LOC:
957 tx_conf.dst_maxburst = 16;
958 break;
959 case SSP_TX_32_OR_MORE_EMPTY_LOC:
960 tx_conf.dst_maxburst = 32;
961 break;
962 default:
963 tx_conf.dst_maxburst = pl022->vendor->fifodepth >> 1;
964 break;
965 }
966
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900967 switch (pl022->read) {
968 case READING_NULL:
969 /* Use the same as for writing */
970 rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
971 break;
972 case READING_U8:
973 rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
974 break;
975 case READING_U16:
976 rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
977 break;
978 case READING_U32:
979 rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
980 break;
981 }
982
983 switch (pl022->write) {
984 case WRITING_NULL:
985 /* Use the same as for reading */
986 tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
987 break;
988 case WRITING_U8:
989 tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
990 break;
991 case WRITING_U16:
992 tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
993 break;
994 case WRITING_U32:
Joe Perchesbc3f67a2010-11-14 19:04:47 -0800995 tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900996 break;
997 }
998
999 /* SPI pecularity: we need to read and write the same width */
1000 if (rx_conf.src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
1001 rx_conf.src_addr_width = tx_conf.dst_addr_width;
1002 if (tx_conf.dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
1003 tx_conf.dst_addr_width = rx_conf.src_addr_width;
1004 BUG_ON(rx_conf.src_addr_width != tx_conf.dst_addr_width);
1005
Linus Walleijecd442f2011-02-08 13:03:12 +01001006 dmaengine_slave_config(rxchan, &rx_conf);
1007 dmaengine_slave_config(txchan, &tx_conf);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001008
1009 /* Create sglists for the transfers */
Viresh Kumarb1815652011-08-10 17:12:11 +05301010 pages = DIV_ROUND_UP(pl022->cur_transfer->len, PAGE_SIZE);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001011 dev_dbg(&pl022->adev->dev, "using %d pages for transfer\n", pages);
1012
Viresh Kumar538a18d2011-08-10 14:20:55 +05301013 ret = sg_alloc_table(&pl022->sgt_rx, pages, GFP_ATOMIC);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001014 if (ret)
1015 goto err_alloc_rx_sg;
1016
Viresh Kumar538a18d2011-08-10 14:20:55 +05301017 ret = sg_alloc_table(&pl022->sgt_tx, pages, GFP_ATOMIC);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001018 if (ret)
1019 goto err_alloc_tx_sg;
1020
1021 /* Fill in the scatterlists for the RX+TX buffers */
1022 setup_dma_scatter(pl022, pl022->rx,
1023 pl022->cur_transfer->len, &pl022->sgt_rx);
1024 setup_dma_scatter(pl022, pl022->tx,
1025 pl022->cur_transfer->len, &pl022->sgt_tx);
1026
1027 /* Map DMA buffers */
Linus Walleij082086f2010-12-22 23:13:37 +01001028 rx_sglen = dma_map_sg(rxchan->device->dev, pl022->sgt_rx.sgl,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001029 pl022->sgt_rx.nents, DMA_FROM_DEVICE);
Linus Walleij082086f2010-12-22 23:13:37 +01001030 if (!rx_sglen)
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001031 goto err_rx_sgmap;
1032
Linus Walleij082086f2010-12-22 23:13:37 +01001033 tx_sglen = dma_map_sg(txchan->device->dev, pl022->sgt_tx.sgl,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001034 pl022->sgt_tx.nents, DMA_TO_DEVICE);
Linus Walleij082086f2010-12-22 23:13:37 +01001035 if (!tx_sglen)
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001036 goto err_tx_sgmap;
1037
1038 /* Send both scatterlists */
Alexandre Bounine16052822012-03-08 16:11:18 -05001039 rxdesc = dmaengine_prep_slave_sg(rxchan,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001040 pl022->sgt_rx.sgl,
Linus Walleij082086f2010-12-22 23:13:37 +01001041 rx_sglen,
Vinod Koula485df42011-10-14 10:47:38 +05301042 DMA_DEV_TO_MEM,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001043 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1044 if (!rxdesc)
1045 goto err_rxdesc;
1046
Alexandre Bounine16052822012-03-08 16:11:18 -05001047 txdesc = dmaengine_prep_slave_sg(txchan,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001048 pl022->sgt_tx.sgl,
Linus Walleij082086f2010-12-22 23:13:37 +01001049 tx_sglen,
Vinod Koula485df42011-10-14 10:47:38 +05301050 DMA_MEM_TO_DEV,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001051 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1052 if (!txdesc)
1053 goto err_txdesc;
1054
1055 /* Put the callback on the RX transfer only, that should finish last */
1056 rxdesc->callback = dma_callback;
1057 rxdesc->callback_param = pl022;
1058
1059 /* Submit and fire RX and TX with TX last so we're ready to read! */
Linus Walleijecd442f2011-02-08 13:03:12 +01001060 dmaengine_submit(rxdesc);
1061 dmaengine_submit(txdesc);
1062 dma_async_issue_pending(rxchan);
1063 dma_async_issue_pending(txchan);
Linus Walleijffbbdd22012-02-22 10:05:38 +01001064 pl022->dma_running = true;
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001065
1066 return 0;
1067
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001068err_txdesc:
Linus Walleijecd442f2011-02-08 13:03:12 +01001069 dmaengine_terminate_all(txchan);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001070err_rxdesc:
Linus Walleijecd442f2011-02-08 13:03:12 +01001071 dmaengine_terminate_all(rxchan);
Linus Walleijb7298892010-12-22 23:13:07 +01001072 dma_unmap_sg(txchan->device->dev, pl022->sgt_tx.sgl,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001073 pl022->sgt_tx.nents, DMA_TO_DEVICE);
1074err_tx_sgmap:
Linus Walleijb7298892010-12-22 23:13:07 +01001075 dma_unmap_sg(rxchan->device->dev, pl022->sgt_rx.sgl,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001076 pl022->sgt_tx.nents, DMA_FROM_DEVICE);
1077err_rx_sgmap:
1078 sg_free_table(&pl022->sgt_tx);
1079err_alloc_tx_sg:
1080 sg_free_table(&pl022->sgt_rx);
1081err_alloc_rx_sg:
1082 return -ENOMEM;
1083}
1084
Russell Kinga5ab6292012-02-13 09:52:29 +00001085static int __devinit pl022_dma_probe(struct pl022 *pl022)
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001086{
1087 dma_cap_mask_t mask;
1088
1089 /* Try to acquire a generic DMA engine slave channel */
1090 dma_cap_zero(mask);
1091 dma_cap_set(DMA_SLAVE, mask);
1092 /*
1093 * We need both RX and TX channels to do DMA, else do none
1094 * of them.
1095 */
1096 pl022->dma_rx_channel = dma_request_channel(mask,
1097 pl022->master_info->dma_filter,
1098 pl022->master_info->dma_rx_param);
1099 if (!pl022->dma_rx_channel) {
Viresh Kumar43c64012011-05-16 09:40:10 +05301100 dev_dbg(&pl022->adev->dev, "no RX DMA channel!\n");
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001101 goto err_no_rxchan;
1102 }
1103
1104 pl022->dma_tx_channel = dma_request_channel(mask,
1105 pl022->master_info->dma_filter,
1106 pl022->master_info->dma_tx_param);
1107 if (!pl022->dma_tx_channel) {
Viresh Kumar43c64012011-05-16 09:40:10 +05301108 dev_dbg(&pl022->adev->dev, "no TX DMA channel!\n");
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001109 goto err_no_txchan;
1110 }
1111
1112 pl022->dummypage = kmalloc(PAGE_SIZE, GFP_KERNEL);
1113 if (!pl022->dummypage) {
Viresh Kumar43c64012011-05-16 09:40:10 +05301114 dev_dbg(&pl022->adev->dev, "no DMA dummypage!\n");
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001115 goto err_no_dummypage;
1116 }
1117
1118 dev_info(&pl022->adev->dev, "setup for DMA on RX %s, TX %s\n",
1119 dma_chan_name(pl022->dma_rx_channel),
1120 dma_chan_name(pl022->dma_tx_channel));
1121
1122 return 0;
1123
1124err_no_dummypage:
1125 dma_release_channel(pl022->dma_tx_channel);
1126err_no_txchan:
1127 dma_release_channel(pl022->dma_rx_channel);
1128 pl022->dma_rx_channel = NULL;
1129err_no_rxchan:
Viresh Kumar43c64012011-05-16 09:40:10 +05301130 dev_err(&pl022->adev->dev,
1131 "Failed to work in dma mode, work without dma!\n");
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001132 return -ENODEV;
1133}
1134
1135static void terminate_dma(struct pl022 *pl022)
1136{
1137 struct dma_chan *rxchan = pl022->dma_rx_channel;
1138 struct dma_chan *txchan = pl022->dma_tx_channel;
1139
Linus Walleijecd442f2011-02-08 13:03:12 +01001140 dmaengine_terminate_all(rxchan);
1141 dmaengine_terminate_all(txchan);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001142 unmap_free_dma_scatter(pl022);
Linus Walleijffbbdd22012-02-22 10:05:38 +01001143 pl022->dma_running = false;
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001144}
1145
1146static void pl022_dma_remove(struct pl022 *pl022)
1147{
Linus Walleijffbbdd22012-02-22 10:05:38 +01001148 if (pl022->dma_running)
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001149 terminate_dma(pl022);
1150 if (pl022->dma_tx_channel)
1151 dma_release_channel(pl022->dma_tx_channel);
1152 if (pl022->dma_rx_channel)
1153 dma_release_channel(pl022->dma_rx_channel);
1154 kfree(pl022->dummypage);
1155}
1156
1157#else
1158static inline int configure_dma(struct pl022 *pl022)
1159{
1160 return -ENODEV;
1161}
1162
1163static inline int pl022_dma_probe(struct pl022 *pl022)
1164{
1165 return 0;
1166}
1167
1168static inline void pl022_dma_remove(struct pl022 *pl022)
1169{
1170}
1171#endif
1172
Linus Walleijb43d65f2009-06-09 08:11:42 +01001173/**
1174 * pl022_interrupt_handler - Interrupt handler for SSP controller
1175 *
1176 * This function handles interrupts generated for an interrupt based transfer.
1177 * If a receive overrun (ROR) interrupt is there then we disable SSP, flag the
1178 * current message's state as STATE_ERROR and schedule the tasklet
1179 * pump_transfers which will do the postprocessing of the current message by
1180 * calling giveback(). Otherwise it reads data from RX FIFO till there is no
1181 * more data, and writes data in TX FIFO till it is not full. If we complete
1182 * the transfer we move to the next transfer and schedule the tasklet.
1183 */
1184static irqreturn_t pl022_interrupt_handler(int irq, void *dev_id)
1185{
1186 struct pl022 *pl022 = dev_id;
1187 struct spi_message *msg = pl022->cur_msg;
1188 u16 irq_status = 0;
1189 u16 flag = 0;
1190
1191 if (unlikely(!msg)) {
1192 dev_err(&pl022->adev->dev,
1193 "bad message state in interrupt handler");
1194 /* Never fail */
1195 return IRQ_HANDLED;
1196 }
1197
1198 /* Read the Interrupt Status Register */
1199 irq_status = readw(SSP_MIS(pl022->virtbase));
1200
1201 if (unlikely(!irq_status))
1202 return IRQ_NONE;
1203
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001204 /*
1205 * This handles the FIFO interrupts, the timeout
1206 * interrupts are flatly ignored, they cannot be
1207 * trusted.
1208 */
Linus Walleijb43d65f2009-06-09 08:11:42 +01001209 if (unlikely(irq_status & SSP_MIS_MASK_RORMIS)) {
1210 /*
1211 * Overrun interrupt - bail out since our Data has been
1212 * corrupted
1213 */
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001214 dev_err(&pl022->adev->dev, "FIFO overrun\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01001215 if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RFF)
1216 dev_err(&pl022->adev->dev,
1217 "RXFIFO is full\n");
1218 if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_TNF)
1219 dev_err(&pl022->adev->dev,
1220 "TXFIFO is full\n");
1221
1222 /*
1223 * Disable and clear interrupts, disable SSP,
1224 * mark message with bad status so it can be
1225 * retried.
1226 */
1227 writew(DISABLE_ALL_INTERRUPTS,
1228 SSP_IMSC(pl022->virtbase));
1229 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
1230 writew((readw(SSP_CR1(pl022->virtbase)) &
1231 (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
1232 msg->state = STATE_ERROR;
1233
1234 /* Schedule message queue handler */
1235 tasklet_schedule(&pl022->pump_transfers);
1236 return IRQ_HANDLED;
1237 }
1238
1239 readwriter(pl022);
1240
1241 if ((pl022->tx == pl022->tx_end) && (flag == 0)) {
1242 flag = 1;
Chris Blair172289d2011-06-04 07:57:47 +01001243 /* Disable Transmit interrupt, enable receive interrupt */
1244 writew((readw(SSP_IMSC(pl022->virtbase)) &
1245 ~SSP_IMSC_MASK_TXIM) | SSP_IMSC_MASK_RXIM,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001246 SSP_IMSC(pl022->virtbase));
1247 }
1248
1249 /*
1250 * Since all transactions must write as much as shall be read,
1251 * we can conclude the entire transaction once RX is complete.
1252 * At this point, all TX will always be finished.
1253 */
1254 if (pl022->rx >= pl022->rx_end) {
1255 writew(DISABLE_ALL_INTERRUPTS,
1256 SSP_IMSC(pl022->virtbase));
1257 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
1258 if (unlikely(pl022->rx > pl022->rx_end)) {
1259 dev_warn(&pl022->adev->dev, "read %u surplus "
1260 "bytes (did you request an odd "
1261 "number of bytes on a 16bit bus?)\n",
1262 (u32) (pl022->rx - pl022->rx_end));
1263 }
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001264 /* Update total bytes transferred */
Linus Walleijb43d65f2009-06-09 08:11:42 +01001265 msg->actual_length += pl022->cur_transfer->len;
1266 if (pl022->cur_transfer->cs_change)
Roland Stiggef6f46de2012-08-22 15:49:17 +02001267 pl022_cs_control(pl022, SSP_CHIP_DESELECT);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001268 /* Move to next transfer */
1269 msg->state = next_transfer(pl022);
1270 tasklet_schedule(&pl022->pump_transfers);
1271 return IRQ_HANDLED;
1272 }
1273
1274 return IRQ_HANDLED;
1275}
1276
1277/**
1278 * This sets up the pointers to memory for the next message to
1279 * send out on the SPI bus.
1280 */
1281static int set_up_next_transfer(struct pl022 *pl022,
1282 struct spi_transfer *transfer)
1283{
1284 int residue;
1285
1286 /* Sanity check the message for this bus width */
1287 residue = pl022->cur_transfer->len % pl022->cur_chip->n_bytes;
1288 if (unlikely(residue != 0)) {
1289 dev_err(&pl022->adev->dev,
1290 "message of %u bytes to transmit but the current "
1291 "chip bus has a data width of %u bytes!\n",
1292 pl022->cur_transfer->len,
1293 pl022->cur_chip->n_bytes);
1294 dev_err(&pl022->adev->dev, "skipping this message\n");
1295 return -EIO;
1296 }
1297 pl022->tx = (void *)transfer->tx_buf;
1298 pl022->tx_end = pl022->tx + pl022->cur_transfer->len;
1299 pl022->rx = (void *)transfer->rx_buf;
1300 pl022->rx_end = pl022->rx + pl022->cur_transfer->len;
1301 pl022->write =
1302 pl022->tx ? pl022->cur_chip->write : WRITING_NULL;
1303 pl022->read = pl022->rx ? pl022->cur_chip->read : READING_NULL;
1304 return 0;
1305}
1306
1307/**
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001308 * pump_transfers - Tasklet function which schedules next transfer
1309 * when running in interrupt or DMA transfer mode.
Linus Walleijb43d65f2009-06-09 08:11:42 +01001310 * @data: SSP driver private data structure
1311 *
1312 */
1313static void pump_transfers(unsigned long data)
1314{
1315 struct pl022 *pl022 = (struct pl022 *) data;
1316 struct spi_message *message = NULL;
1317 struct spi_transfer *transfer = NULL;
1318 struct spi_transfer *previous = NULL;
1319
1320 /* Get current state information */
1321 message = pl022->cur_msg;
1322 transfer = pl022->cur_transfer;
1323
1324 /* Handle for abort */
1325 if (message->state == STATE_ERROR) {
1326 message->status = -EIO;
1327 giveback(pl022);
1328 return;
1329 }
1330
1331 /* Handle end of message */
1332 if (message->state == STATE_DONE) {
1333 message->status = 0;
1334 giveback(pl022);
1335 return;
1336 }
1337
1338 /* Delay if requested at end of transfer before CS change */
1339 if (message->state == STATE_RUNNING) {
1340 previous = list_entry(transfer->transfer_list.prev,
1341 struct spi_transfer,
1342 transfer_list);
1343 if (previous->delay_usecs)
1344 /*
1345 * FIXME: This runs in interrupt context.
1346 * Is this really smart?
1347 */
1348 udelay(previous->delay_usecs);
1349
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +05301350 /* Reselect chip select only if cs_change was requested */
Linus Walleijb43d65f2009-06-09 08:11:42 +01001351 if (previous->cs_change)
Roland Stiggef6f46de2012-08-22 15:49:17 +02001352 pl022_cs_control(pl022, SSP_CHIP_SELECT);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001353 } else {
1354 /* STATE_START */
1355 message->state = STATE_RUNNING;
1356 }
1357
1358 if (set_up_next_transfer(pl022, transfer)) {
1359 message->state = STATE_ERROR;
1360 message->status = -EIO;
1361 giveback(pl022);
1362 return;
1363 }
1364 /* Flush the FIFOs and let's go! */
1365 flush(pl022);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001366
1367 if (pl022->cur_chip->enable_dma) {
1368 if (configure_dma(pl022)) {
1369 dev_dbg(&pl022->adev->dev,
1370 "configuration of DMA failed, fall back to interrupt mode\n");
1371 goto err_config_dma;
1372 }
1373 return;
1374 }
1375
1376err_config_dma:
Chris Blair172289d2011-06-04 07:57:47 +01001377 /* enable all interrupts except RX */
1378 writew(ENABLE_ALL_INTERRUPTS & ~SSP_IMSC_MASK_RXIM, SSP_IMSC(pl022->virtbase));
Linus Walleijb43d65f2009-06-09 08:11:42 +01001379}
1380
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001381static void do_interrupt_dma_transfer(struct pl022 *pl022)
Linus Walleijb43d65f2009-06-09 08:11:42 +01001382{
Chris Blair172289d2011-06-04 07:57:47 +01001383 /*
1384 * Default is to enable all interrupts except RX -
1385 * this will be enabled once TX is complete
1386 */
1387 u32 irqflags = ENABLE_ALL_INTERRUPTS & ~SSP_IMSC_MASK_RXIM;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001388
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +05301389 /* Enable target chip, if not already active */
1390 if (!pl022->next_msg_cs_active)
Roland Stiggef6f46de2012-08-22 15:49:17 +02001391 pl022_cs_control(pl022, SSP_CHIP_SELECT);
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +05301392
Linus Walleijb43d65f2009-06-09 08:11:42 +01001393 if (set_up_next_transfer(pl022, pl022->cur_transfer)) {
1394 /* Error path */
1395 pl022->cur_msg->state = STATE_ERROR;
1396 pl022->cur_msg->status = -EIO;
1397 giveback(pl022);
1398 return;
1399 }
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001400 /* If we're using DMA, set up DMA here */
1401 if (pl022->cur_chip->enable_dma) {
1402 /* Configure DMA transfer */
1403 if (configure_dma(pl022)) {
1404 dev_dbg(&pl022->adev->dev,
1405 "configuration of DMA failed, fall back to interrupt mode\n");
1406 goto err_config_dma;
1407 }
1408 /* Disable interrupts in DMA mode, IRQ from DMA controller */
1409 irqflags = DISABLE_ALL_INTERRUPTS;
1410 }
1411err_config_dma:
Linus Walleijb43d65f2009-06-09 08:11:42 +01001412 /* Enable SSP, turn on interrupts */
1413 writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
1414 SSP_CR1(pl022->virtbase));
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001415 writew(irqflags, SSP_IMSC(pl022->virtbase));
Linus Walleijb43d65f2009-06-09 08:11:42 +01001416}
1417
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001418static void do_polling_transfer(struct pl022 *pl022)
Linus Walleijb43d65f2009-06-09 08:11:42 +01001419{
Linus Walleijb43d65f2009-06-09 08:11:42 +01001420 struct spi_message *message = NULL;
1421 struct spi_transfer *transfer = NULL;
1422 struct spi_transfer *previous = NULL;
1423 struct chip_data *chip;
Magnus Templinga18c2662011-05-19 18:05:34 +02001424 unsigned long time, timeout;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001425
1426 chip = pl022->cur_chip;
1427 message = pl022->cur_msg;
1428
1429 while (message->state != STATE_DONE) {
1430 /* Handle for abort */
1431 if (message->state == STATE_ERROR)
1432 break;
1433 transfer = pl022->cur_transfer;
1434
1435 /* Delay if requested at end of transfer */
1436 if (message->state == STATE_RUNNING) {
1437 previous =
1438 list_entry(transfer->transfer_list.prev,
1439 struct spi_transfer, transfer_list);
1440 if (previous->delay_usecs)
1441 udelay(previous->delay_usecs);
1442 if (previous->cs_change)
Roland Stiggef6f46de2012-08-22 15:49:17 +02001443 pl022_cs_control(pl022, SSP_CHIP_SELECT);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001444 } else {
1445 /* STATE_START */
1446 message->state = STATE_RUNNING;
Virupax Sadashivpetimath8b8d7192011-11-10 12:43:24 +05301447 if (!pl022->next_msg_cs_active)
Roland Stiggef6f46de2012-08-22 15:49:17 +02001448 pl022_cs_control(pl022, SSP_CHIP_SELECT);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001449 }
1450
1451 /* Configuration Changing Per Transfer */
1452 if (set_up_next_transfer(pl022, transfer)) {
1453 /* Error path */
1454 message->state = STATE_ERROR;
1455 break;
1456 }
1457 /* Flush FIFOs and enable SSP */
1458 flush(pl022);
1459 writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
1460 SSP_CR1(pl022->virtbase));
1461
Linus Walleij556f4ae2010-05-05 09:28:15 +00001462 dev_dbg(&pl022->adev->dev, "polling transfer ongoing ...\n");
Magnus Templinga18c2662011-05-19 18:05:34 +02001463
1464 timeout = jiffies + msecs_to_jiffies(SPI_POLLING_TIMEOUT);
1465 while (pl022->tx < pl022->tx_end || pl022->rx < pl022->rx_end) {
1466 time = jiffies;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001467 readwriter(pl022);
Magnus Templinga18c2662011-05-19 18:05:34 +02001468 if (time_after(time, timeout)) {
1469 dev_warn(&pl022->adev->dev,
1470 "%s: timeout!\n", __func__);
1471 message->state = STATE_ERROR;
1472 goto out;
1473 }
Linus Walleij521999b2011-05-19 20:01:25 +02001474 cpu_relax();
Magnus Templinga18c2662011-05-19 18:05:34 +02001475 }
Linus Walleijb43d65f2009-06-09 08:11:42 +01001476
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001477 /* Update total byte transferred */
Linus Walleijb43d65f2009-06-09 08:11:42 +01001478 message->actual_length += pl022->cur_transfer->len;
1479 if (pl022->cur_transfer->cs_change)
Roland Stiggef6f46de2012-08-22 15:49:17 +02001480 pl022_cs_control(pl022, SSP_CHIP_DESELECT);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001481 /* Move to next transfer */
1482 message->state = next_transfer(pl022);
1483 }
Magnus Templinga18c2662011-05-19 18:05:34 +02001484out:
Linus Walleijb43d65f2009-06-09 08:11:42 +01001485 /* Handle end of message */
1486 if (message->state == STATE_DONE)
1487 message->status = 0;
1488 else
1489 message->status = -EIO;
1490
1491 giveback(pl022);
1492 return;
1493}
1494
Linus Walleijffbbdd22012-02-22 10:05:38 +01001495static int pl022_transfer_one_message(struct spi_master *master,
1496 struct spi_message *msg)
Linus Walleijb43d65f2009-06-09 08:11:42 +01001497{
Linus Walleijffbbdd22012-02-22 10:05:38 +01001498 struct pl022 *pl022 = spi_master_get_devdata(master);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001499
1500 /* Initial message state */
Linus Walleijffbbdd22012-02-22 10:05:38 +01001501 pl022->cur_msg = msg;
1502 msg->state = STATE_START;
1503
1504 pl022->cur_transfer = list_entry(msg->transfers.next,
1505 struct spi_transfer, transfer_list);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001506
1507 /* Setup the SPI using the per chip configuration */
Linus Walleijffbbdd22012-02-22 10:05:38 +01001508 pl022->cur_chip = spi_get_ctldata(msg->spi);
Roland Stiggef6f46de2012-08-22 15:49:17 +02001509 pl022->cur_cs = pl022->chipselects[msg->spi->chip_select];
Chris Blaird4b6af22011-11-04 07:43:41 +00001510
Linus Walleijb43d65f2009-06-09 08:11:42 +01001511 restore_state(pl022);
1512 flush(pl022);
1513
1514 if (pl022->cur_chip->xfer_type == POLLING_TRANSFER)
1515 do_polling_transfer(pl022);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001516 else
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001517 do_interrupt_dma_transfer(pl022);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001518
1519 return 0;
1520}
1521
Linus Walleijffbbdd22012-02-22 10:05:38 +01001522static int pl022_prepare_transfer_hardware(struct spi_master *master)
Linus Walleijb43d65f2009-06-09 08:11:42 +01001523{
Linus Walleijffbbdd22012-02-22 10:05:38 +01001524 struct pl022 *pl022 = spi_master_get_devdata(master);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001525
Linus Walleijffbbdd22012-02-22 10:05:38 +01001526 /*
1527 * Just make sure we have all we need to run the transfer by syncing
1528 * with the runtime PM framework.
1529 */
1530 pm_runtime_get_sync(&pl022->adev->dev);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001531 return 0;
1532}
1533
Linus Walleijffbbdd22012-02-22 10:05:38 +01001534static int pl022_unprepare_transfer_hardware(struct spi_master *master)
Linus Walleijb43d65f2009-06-09 08:11:42 +01001535{
Linus Walleijffbbdd22012-02-22 10:05:38 +01001536 struct pl022 *pl022 = spi_master_get_devdata(master);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001537
Linus Walleijffbbdd22012-02-22 10:05:38 +01001538 /* nothing more to do - disable spi/ssp and power off */
1539 writew((readw(SSP_CR1(pl022->virtbase)) &
1540 (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
Linus Walleijb43d65f2009-06-09 08:11:42 +01001541
Linus Walleijffbbdd22012-02-22 10:05:38 +01001542 if (pl022->master_info->autosuspend_delay > 0) {
1543 pm_runtime_mark_last_busy(&pl022->adev->dev);
1544 pm_runtime_put_autosuspend(&pl022->adev->dev);
1545 } else {
1546 pm_runtime_put(&pl022->adev->dev);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001547 }
1548
Linus Walleijb43d65f2009-06-09 08:11:42 +01001549 return 0;
1550}
1551
1552static int verify_controller_parameters(struct pl022 *pl022,
Linus Walleijf9d629c2010-10-01 13:33:13 +02001553 struct pl022_config_chip const *chip_info)
Linus Walleijb43d65f2009-06-09 08:11:42 +01001554{
Linus Walleijb43d65f2009-06-09 08:11:42 +01001555 if ((chip_info->iface < SSP_INTERFACE_MOTOROLA_SPI)
1556 || (chip_info->iface > SSP_INTERFACE_UNIDIRECTIONAL)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001557 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001558 "interface is configured incorrectly\n");
1559 return -EINVAL;
1560 }
1561 if ((chip_info->iface == SSP_INTERFACE_UNIDIRECTIONAL) &&
1562 (!pl022->vendor->unidir)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001563 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001564 "unidirectional mode not supported in this "
1565 "hardware version\n");
1566 return -EINVAL;
1567 }
1568 if ((chip_info->hierarchy != SSP_MASTER)
1569 && (chip_info->hierarchy != SSP_SLAVE)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001570 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001571 "hierarchy is configured incorrectly\n");
1572 return -EINVAL;
1573 }
Linus Walleijb43d65f2009-06-09 08:11:42 +01001574 if ((chip_info->com_mode != INTERRUPT_TRANSFER)
1575 && (chip_info->com_mode != DMA_TRANSFER)
1576 && (chip_info->com_mode != POLLING_TRANSFER)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001577 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001578 "Communication mode is configured incorrectly\n");
1579 return -EINVAL;
1580 }
Linus Walleij78b2b912011-06-16 10:14:46 +02001581 switch (chip_info->rx_lev_trig) {
1582 case SSP_RX_1_OR_MORE_ELEM:
1583 case SSP_RX_4_OR_MORE_ELEM:
1584 case SSP_RX_8_OR_MORE_ELEM:
1585 /* These are always OK, all variants can handle this */
1586 break;
1587 case SSP_RX_16_OR_MORE_ELEM:
1588 if (pl022->vendor->fifodepth < 16) {
1589 dev_err(&pl022->adev->dev,
1590 "RX FIFO Trigger Level is configured incorrectly\n");
1591 return -EINVAL;
1592 }
1593 break;
1594 case SSP_RX_32_OR_MORE_ELEM:
1595 if (pl022->vendor->fifodepth < 32) {
1596 dev_err(&pl022->adev->dev,
1597 "RX FIFO Trigger Level is configured incorrectly\n");
1598 return -EINVAL;
1599 }
1600 break;
1601 default:
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001602 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001603 "RX FIFO Trigger Level is configured incorrectly\n");
1604 return -EINVAL;
Linus Walleij78b2b912011-06-16 10:14:46 +02001605 break;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001606 }
Linus Walleij78b2b912011-06-16 10:14:46 +02001607 switch (chip_info->tx_lev_trig) {
1608 case SSP_TX_1_OR_MORE_EMPTY_LOC:
1609 case SSP_TX_4_OR_MORE_EMPTY_LOC:
1610 case SSP_TX_8_OR_MORE_EMPTY_LOC:
1611 /* These are always OK, all variants can handle this */
1612 break;
1613 case SSP_TX_16_OR_MORE_EMPTY_LOC:
1614 if (pl022->vendor->fifodepth < 16) {
1615 dev_err(&pl022->adev->dev,
1616 "TX FIFO Trigger Level is configured incorrectly\n");
1617 return -EINVAL;
1618 }
1619 break;
1620 case SSP_TX_32_OR_MORE_EMPTY_LOC:
1621 if (pl022->vendor->fifodepth < 32) {
1622 dev_err(&pl022->adev->dev,
1623 "TX FIFO Trigger Level is configured incorrectly\n");
1624 return -EINVAL;
1625 }
1626 break;
1627 default:
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001628 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001629 "TX FIFO Trigger Level is configured incorrectly\n");
1630 return -EINVAL;
Linus Walleij78b2b912011-06-16 10:14:46 +02001631 break;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001632 }
Linus Walleijb43d65f2009-06-09 08:11:42 +01001633 if (chip_info->iface == SSP_INTERFACE_NATIONAL_MICROWIRE) {
1634 if ((chip_info->ctrl_len < SSP_BITS_4)
1635 || (chip_info->ctrl_len > SSP_BITS_32)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001636 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001637 "CTRL LEN is configured incorrectly\n");
1638 return -EINVAL;
1639 }
1640 if ((chip_info->wait_state != SSP_MWIRE_WAIT_ZERO)
1641 && (chip_info->wait_state != SSP_MWIRE_WAIT_ONE)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001642 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001643 "Wait State is configured incorrectly\n");
1644 return -EINVAL;
1645 }
Linus Walleij556f4ae2010-05-05 09:28:15 +00001646 /* Half duplex is only available in the ST Micro version */
1647 if (pl022->vendor->extended_cr) {
1648 if ((chip_info->duplex !=
1649 SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
1650 && (chip_info->duplex !=
Julia Lawall4a4fd472010-09-29 17:31:30 +09001651 SSP_MICROWIRE_CHANNEL_HALF_DUPLEX)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001652 dev_err(&pl022->adev->dev,
Linus Walleij556f4ae2010-05-05 09:28:15 +00001653 "Microwire duplex mode is configured incorrectly\n");
1654 return -EINVAL;
Julia Lawall4a4fd472010-09-29 17:31:30 +09001655 }
Linus Walleij556f4ae2010-05-05 09:28:15 +00001656 } else {
1657 if (chip_info->duplex != SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001658 dev_err(&pl022->adev->dev,
Linus Walleij556f4ae2010-05-05 09:28:15 +00001659 "Microwire half duplex mode requested,"
1660 " but this is only available in the"
1661 " ST version of PL022\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01001662 return -EINVAL;
1663 }
1664 }
Linus Walleijb43d65f2009-06-09 08:11:42 +01001665 return 0;
1666}
1667
Viresh Kumar0379b2a2011-08-10 14:20:57 +05301668static inline u32 spi_rate(u32 rate, u16 cpsdvsr, u16 scr)
1669{
1670 return rate / (cpsdvsr * (1 + scr));
1671}
1672
1673static int calculate_effective_freq(struct pl022 *pl022, int freq, struct
1674 ssp_clock_params * clk_freq)
Linus Walleijb43d65f2009-06-09 08:11:42 +01001675{
1676 /* Lets calculate the frequency parameters */
Viresh Kumar0379b2a2011-08-10 14:20:57 +05301677 u16 cpsdvsr = CPSDVR_MIN, scr = SCR_MIN;
1678 u32 rate, max_tclk, min_tclk, best_freq = 0, best_cpsdvsr = 0,
1679 best_scr = 0, tmp, found = 0;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001680
1681 rate = clk_get_rate(pl022->clk);
1682 /* cpsdvscr = 2 & scr 0 */
Viresh Kumar0379b2a2011-08-10 14:20:57 +05301683 max_tclk = spi_rate(rate, CPSDVR_MIN, SCR_MIN);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001684 /* cpsdvsr = 254 & scr = 255 */
Viresh Kumar0379b2a2011-08-10 14:20:57 +05301685 min_tclk = spi_rate(rate, CPSDVR_MAX, SCR_MAX);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001686
Viresh Kumarea505bc2012-04-19 11:48:15 +05301687 if (freq > max_tclk)
1688 dev_warn(&pl022->adev->dev,
1689 "Max speed that can be programmed is %d Hz, you requested %d\n",
1690 max_tclk, freq);
1691
1692 if (freq < min_tclk) {
Linus Walleijb43d65f2009-06-09 08:11:42 +01001693 dev_err(&pl022->adev->dev,
Viresh Kumarea505bc2012-04-19 11:48:15 +05301694 "Requested frequency: %d Hz is less than minimum possible %d Hz\n",
1695 freq, min_tclk);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001696 return -EINVAL;
1697 }
Viresh Kumar0379b2a2011-08-10 14:20:57 +05301698
1699 /*
1700 * best_freq will give closest possible available rate (<= requested
1701 * freq) for all values of scr & cpsdvsr.
1702 */
1703 while ((cpsdvsr <= CPSDVR_MAX) && !found) {
1704 while (scr <= SCR_MAX) {
1705 tmp = spi_rate(rate, cpsdvsr, scr);
1706
Viresh Kumar5eb806a2012-04-19 14:44:21 +05301707 if (tmp > freq) {
1708 /* we need lower freq */
Viresh Kumar0379b2a2011-08-10 14:20:57 +05301709 scr++;
Viresh Kumar5eb806a2012-04-19 14:44:21 +05301710 continue;
1711 }
1712
Viresh Kumar0379b2a2011-08-10 14:20:57 +05301713 /*
Viresh Kumar5eb806a2012-04-19 14:44:21 +05301714 * If found exact value, mark found and break.
1715 * If found more closer value, update and break.
Viresh Kumar0379b2a2011-08-10 14:20:57 +05301716 */
Viresh Kumar5eb806a2012-04-19 14:44:21 +05301717 if (tmp > best_freq) {
Viresh Kumar0379b2a2011-08-10 14:20:57 +05301718 best_freq = tmp;
1719 best_cpsdvsr = cpsdvsr;
1720 best_scr = scr;
1721
1722 if (tmp == freq)
Viresh Kumar5eb806a2012-04-19 14:44:21 +05301723 found = 1;
Viresh Kumar0379b2a2011-08-10 14:20:57 +05301724 }
Viresh Kumar5eb806a2012-04-19 14:44:21 +05301725 /*
1726 * increased scr will give lower rates, which are not
1727 * required
1728 */
1729 break;
Viresh Kumar0379b2a2011-08-10 14:20:57 +05301730 }
1731 cpsdvsr += 2;
1732 scr = SCR_MIN;
1733 }
1734
Viresh Kumar5eb806a2012-04-19 14:44:21 +05301735 WARN(!best_freq, "pl022: Matching cpsdvsr and scr not found for %d Hz rate \n",
1736 freq);
1737
Viresh Kumar0379b2a2011-08-10 14:20:57 +05301738 clk_freq->cpsdvsr = (u8) (best_cpsdvsr & 0xFF);
1739 clk_freq->scr = (u8) (best_scr & 0xFF);
1740 dev_dbg(&pl022->adev->dev,
1741 "SSP Target Frequency is: %u, Effective Frequency is %u\n",
1742 freq, best_freq);
1743 dev_dbg(&pl022->adev->dev, "SSP cpsdvsr = %d, scr = %d\n",
1744 clk_freq->cpsdvsr, clk_freq->scr);
1745
Linus Walleijb43d65f2009-06-09 08:11:42 +01001746 return 0;
1747}
1748
Linus Walleijf9d629c2010-10-01 13:33:13 +02001749/*
1750 * A piece of default chip info unless the platform
1751 * supplies it.
1752 */
1753static const struct pl022_config_chip pl022_default_chip_info = {
1754 .com_mode = POLLING_TRANSFER,
1755 .iface = SSP_INTERFACE_MOTOROLA_SPI,
1756 .hierarchy = SSP_SLAVE,
1757 .slave_tx_disable = DO_NOT_DRIVE_TX,
1758 .rx_lev_trig = SSP_RX_1_OR_MORE_ELEM,
1759 .tx_lev_trig = SSP_TX_1_OR_MORE_EMPTY_LOC,
1760 .ctrl_len = SSP_BITS_8,
1761 .wait_state = SSP_MWIRE_WAIT_ZERO,
1762 .duplex = SSP_MICROWIRE_CHANNEL_FULL_DUPLEX,
1763 .cs_control = null_cs_control,
1764};
1765
Linus Walleijb43d65f2009-06-09 08:11:42 +01001766/**
Linus Walleijb43d65f2009-06-09 08:11:42 +01001767 * pl022_setup - setup function registered to SPI master framework
1768 * @spi: spi device which is requesting setup
1769 *
1770 * This function is registered to the SPI framework for this SPI master
1771 * controller. If it is the first time when setup is called by this device,
1772 * this function will initialize the runtime state for this chip and save
1773 * the same in the device structure. Else it will update the runtime info
1774 * with the updated chip info. Nothing is really being written to the
1775 * controller hardware here, that is not done until the actual transfer
1776 * commence.
1777 */
Linus Walleijb43d65f2009-06-09 08:11:42 +01001778static int pl022_setup(struct spi_device *spi)
1779{
Linus Walleijf9d629c2010-10-01 13:33:13 +02001780 struct pl022_config_chip const *chip_info;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001781 struct chip_data *chip;
Jonas Aabergc4a47842011-02-28 16:42:41 +01001782 struct ssp_clock_params clk_freq = { .cpsdvsr = 0, .scr = 0};
Linus Walleijb43d65f2009-06-09 08:11:42 +01001783 int status = 0;
1784 struct pl022 *pl022 = spi_master_get_devdata(spi->master);
Kevin Wellsbde435a2010-09-16 06:18:50 -07001785 unsigned int bits = spi->bits_per_word;
1786 u32 tmp;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001787
1788 if (!spi->max_speed_hz)
1789 return -EINVAL;
1790
1791 /* Get controller_state if one is supplied */
1792 chip = spi_get_ctldata(spi);
1793
1794 if (chip == NULL) {
1795 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1796 if (!chip) {
1797 dev_err(&spi->dev,
1798 "cannot allocate controller state\n");
1799 return -ENOMEM;
1800 }
1801 dev_dbg(&spi->dev,
1802 "allocated memory for controller's runtime state\n");
1803 }
1804
1805 /* Get controller data if one is supplied */
1806 chip_info = spi->controller_data;
1807
1808 if (chip_info == NULL) {
Linus Walleijf9d629c2010-10-01 13:33:13 +02001809 chip_info = &pl022_default_chip_info;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001810 /* spi_board_info.controller_data not is supplied */
1811 dev_dbg(&spi->dev,
1812 "using default controller_data settings\n");
Linus Walleijf9d629c2010-10-01 13:33:13 +02001813 } else
Linus Walleijb43d65f2009-06-09 08:11:42 +01001814 dev_dbg(&spi->dev,
1815 "using user supplied controller_data settings\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01001816
1817 /*
1818 * We can override with custom divisors, else we use the board
1819 * frequency setting
1820 */
1821 if ((0 == chip_info->clk_freq.cpsdvsr)
1822 && (0 == chip_info->clk_freq.scr)) {
1823 status = calculate_effective_freq(pl022,
1824 spi->max_speed_hz,
Linus Walleijf9d629c2010-10-01 13:33:13 +02001825 &clk_freq);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001826 if (status < 0)
1827 goto err_config_params;
1828 } else {
Linus Walleijf9d629c2010-10-01 13:33:13 +02001829 memcpy(&clk_freq, &chip_info->clk_freq, sizeof(clk_freq));
1830 if ((clk_freq.cpsdvsr % 2) != 0)
1831 clk_freq.cpsdvsr =
1832 clk_freq.cpsdvsr - 1;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001833 }
Linus Walleijf9d629c2010-10-01 13:33:13 +02001834 if ((clk_freq.cpsdvsr < CPSDVR_MIN)
1835 || (clk_freq.cpsdvsr > CPSDVR_MAX)) {
Virupax Sadashivpetimathe3f88ae2011-06-13 16:23:46 +05301836 status = -EINVAL;
Linus Walleijf9d629c2010-10-01 13:33:13 +02001837 dev_err(&spi->dev,
1838 "cpsdvsr is configured incorrectly\n");
1839 goto err_config_params;
1840 }
1841
Linus Walleijb43d65f2009-06-09 08:11:42 +01001842 status = verify_controller_parameters(pl022, chip_info);
1843 if (status) {
1844 dev_err(&spi->dev, "controller data is incorrect");
1845 goto err_config_params;
1846 }
Linus Walleijf9d629c2010-10-01 13:33:13 +02001847
Linus Walleij083be3f2011-06-16 10:14:28 +02001848 pl022->rx_lev_trig = chip_info->rx_lev_trig;
1849 pl022->tx_lev_trig = chip_info->tx_lev_trig;
1850
Linus Walleijb43d65f2009-06-09 08:11:42 +01001851 /* Now set controller state based on controller data */
1852 chip->xfer_type = chip_info->com_mode;
Linus Walleijf9d629c2010-10-01 13:33:13 +02001853 if (!chip_info->cs_control) {
1854 chip->cs_control = null_cs_control;
Roland Stiggef6f46de2012-08-22 15:49:17 +02001855 if (!gpio_is_valid(pl022->chipselects[spi->chip_select]))
1856 dev_warn(&spi->dev,
1857 "invalid chip select\n");
Linus Walleijf9d629c2010-10-01 13:33:13 +02001858 } else
1859 chip->cs_control = chip_info->cs_control;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001860
Vinit Shenoyeb798c62012-04-17 12:40:13 +05301861 /* Check bits per word with vendor specific range */
1862 if ((bits <= 3) || (bits > pl022->vendor->max_bpw)) {
Kevin Wellsbde435a2010-09-16 06:18:50 -07001863 status = -ENOTSUPP;
Vinit Shenoyeb798c62012-04-17 12:40:13 +05301864 dev_err(&spi->dev, "illegal data size for this controller!\n");
1865 dev_err(&spi->dev, "This controller can only handle 4 <= n <= %d bit words\n",
1866 pl022->vendor->max_bpw);
Kevin Wellsbde435a2010-09-16 06:18:50 -07001867 goto err_config_params;
1868 } else if (bits <= 8) {
1869 dev_dbg(&spi->dev, "4 <= n <=8 bits per word\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01001870 chip->n_bytes = 1;
1871 chip->read = READING_U8;
1872 chip->write = WRITING_U8;
Kevin Wellsbde435a2010-09-16 06:18:50 -07001873 } else if (bits <= 16) {
Linus Walleijb43d65f2009-06-09 08:11:42 +01001874 dev_dbg(&spi->dev, "9 <= n <= 16 bits per word\n");
1875 chip->n_bytes = 2;
1876 chip->read = READING_U16;
1877 chip->write = WRITING_U16;
1878 } else {
Vinit Shenoyeb798c62012-04-17 12:40:13 +05301879 dev_dbg(&spi->dev, "17 <= n <= 32 bits per word\n");
1880 chip->n_bytes = 4;
1881 chip->read = READING_U32;
1882 chip->write = WRITING_U32;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001883 }
1884
1885 /* Now Initialize all register settings required for this chip */
1886 chip->cr0 = 0;
1887 chip->cr1 = 0;
1888 chip->dmacr = 0;
1889 chip->cpsr = 0;
1890 if ((chip_info->com_mode == DMA_TRANSFER)
1891 && ((pl022->master_info)->enable_dma)) {
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001892 chip->enable_dma = true;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001893 dev_dbg(&spi->dev, "DMA mode set in controller state\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01001894 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
1895 SSP_DMACR_MASK_RXDMAE, 0);
1896 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
1897 SSP_DMACR_MASK_TXDMAE, 1);
1898 } else {
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001899 chip->enable_dma = false;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001900 dev_dbg(&spi->dev, "DMA mode NOT set in controller state\n");
1901 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
1902 SSP_DMACR_MASK_RXDMAE, 0);
1903 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
1904 SSP_DMACR_MASK_TXDMAE, 1);
1905 }
1906
Linus Walleijf9d629c2010-10-01 13:33:13 +02001907 chip->cpsr = clk_freq.cpsdvsr;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001908
Linus Walleij556f4ae2010-05-05 09:28:15 +00001909 /* Special setup for the ST micro extended control registers */
1910 if (pl022->vendor->extended_cr) {
Kevin Wellsbde435a2010-09-16 06:18:50 -07001911 u32 etx;
1912
Linus Walleij781c7b12010-05-07 08:40:53 +00001913 if (pl022->vendor->pl023) {
1914 /* These bits are only in the PL023 */
1915 SSP_WRITE_BITS(chip->cr1, chip_info->clkdelay,
1916 SSP_CR1_MASK_FBCLKDEL_ST, 13);
1917 } else {
1918 /* These bits are in the PL022 but not PL023 */
1919 SSP_WRITE_BITS(chip->cr0, chip_info->duplex,
1920 SSP_CR0_MASK_HALFDUP_ST, 5);
1921 SSP_WRITE_BITS(chip->cr0, chip_info->ctrl_len,
1922 SSP_CR0_MASK_CSS_ST, 16);
1923 SSP_WRITE_BITS(chip->cr0, chip_info->iface,
1924 SSP_CR0_MASK_FRF_ST, 21);
1925 SSP_WRITE_BITS(chip->cr1, chip_info->wait_state,
1926 SSP_CR1_MASK_MWAIT_ST, 6);
1927 }
Kevin Wellsbde435a2010-09-16 06:18:50 -07001928 SSP_WRITE_BITS(chip->cr0, bits - 1,
Linus Walleij556f4ae2010-05-05 09:28:15 +00001929 SSP_CR0_MASK_DSS_ST, 0);
Kevin Wellsbde435a2010-09-16 06:18:50 -07001930
1931 if (spi->mode & SPI_LSB_FIRST) {
1932 tmp = SSP_RX_LSB;
1933 etx = SSP_TX_LSB;
1934 } else {
1935 tmp = SSP_RX_MSB;
1936 etx = SSP_TX_MSB;
1937 }
1938 SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_RENDN_ST, 4);
1939 SSP_WRITE_BITS(chip->cr1, etx, SSP_CR1_MASK_TENDN_ST, 5);
Linus Walleij556f4ae2010-05-05 09:28:15 +00001940 SSP_WRITE_BITS(chip->cr1, chip_info->rx_lev_trig,
1941 SSP_CR1_MASK_RXIFLSEL_ST, 7);
1942 SSP_WRITE_BITS(chip->cr1, chip_info->tx_lev_trig,
1943 SSP_CR1_MASK_TXIFLSEL_ST, 10);
1944 } else {
Kevin Wellsbde435a2010-09-16 06:18:50 -07001945 SSP_WRITE_BITS(chip->cr0, bits - 1,
Linus Walleij556f4ae2010-05-05 09:28:15 +00001946 SSP_CR0_MASK_DSS, 0);
1947 SSP_WRITE_BITS(chip->cr0, chip_info->iface,
1948 SSP_CR0_MASK_FRF, 4);
1949 }
Kevin Wellsbde435a2010-09-16 06:18:50 -07001950
Linus Walleij556f4ae2010-05-05 09:28:15 +00001951 /* Stuff that is common for all versions */
Kevin Wellsbde435a2010-09-16 06:18:50 -07001952 if (spi->mode & SPI_CPOL)
1953 tmp = SSP_CLK_POL_IDLE_HIGH;
1954 else
1955 tmp = SSP_CLK_POL_IDLE_LOW;
1956 SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPO, 6);
1957
1958 if (spi->mode & SPI_CPHA)
1959 tmp = SSP_CLK_SECOND_EDGE;
1960 else
1961 tmp = SSP_CLK_FIRST_EDGE;
1962 SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPH, 7);
1963
Linus Walleijf9d629c2010-10-01 13:33:13 +02001964 SSP_WRITE_BITS(chip->cr0, clk_freq.scr, SSP_CR0_MASK_SCR, 8);
Linus Walleij781c7b12010-05-07 08:40:53 +00001965 /* Loopback is available on all versions except PL023 */
Philippe Langlais06fb01f2011-03-23 11:05:16 +01001966 if (pl022->vendor->loopback) {
Kevin Wellsbde435a2010-09-16 06:18:50 -07001967 if (spi->mode & SPI_LOOP)
1968 tmp = LOOPBACK_ENABLED;
1969 else
1970 tmp = LOOPBACK_DISABLED;
1971 SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_LBM, 0);
1972 }
Linus Walleijb43d65f2009-06-09 08:11:42 +01001973 SSP_WRITE_BITS(chip->cr1, SSP_DISABLED, SSP_CR1_MASK_SSE, 1);
1974 SSP_WRITE_BITS(chip->cr1, chip_info->hierarchy, SSP_CR1_MASK_MS, 2);
Viresh Kumarf1e45f82011-08-10 14:20:54 +05301975 SSP_WRITE_BITS(chip->cr1, chip_info->slave_tx_disable, SSP_CR1_MASK_SOD,
1976 3);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001977
1978 /* Save controller_state */
1979 spi_set_ctldata(spi, chip);
1980 return status;
1981 err_config_params:
Kevin Wellsbde435a2010-09-16 06:18:50 -07001982 spi_set_ctldata(spi, NULL);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001983 kfree(chip);
1984 return status;
1985}
1986
1987/**
1988 * pl022_cleanup - cleanup function registered to SPI master framework
1989 * @spi: spi device which is requesting cleanup
1990 *
1991 * This function is registered to the SPI framework for this SPI master
1992 * controller. It will free the runtime state of chip.
1993 */
1994static void pl022_cleanup(struct spi_device *spi)
1995{
1996 struct chip_data *chip = spi_get_ctldata(spi);
1997
1998 spi_set_ctldata(spi, NULL);
1999 kfree(chip);
2000}
2001
Kevin Wellsb4225882010-07-27 16:39:30 +00002002static int __devinit
Russell Kingaa25afa2011-02-19 15:55:00 +00002003pl022_probe(struct amba_device *adev, const struct amba_id *id)
Linus Walleijb43d65f2009-06-09 08:11:42 +01002004{
2005 struct device *dev = &adev->dev;
2006 struct pl022_ssp_controller *platform_info = adev->dev.platform_data;
2007 struct spi_master *master;
2008 struct pl022 *pl022 = NULL; /*Data for this driver */
Roland Stiggef6f46de2012-08-22 15:49:17 +02002009 int status = 0, i;
Linus Walleijb43d65f2009-06-09 08:11:42 +01002010
2011 dev_info(&adev->dev,
2012 "ARM PL022 driver, device ID: 0x%08x\n", adev->periphid);
2013 if (platform_info == NULL) {
2014 dev_err(&adev->dev, "probe - no platform data supplied\n");
2015 status = -ENODEV;
2016 goto err_no_pdata;
2017 }
2018
2019 /* Allocate master with space for data */
Roland Stiggef6f46de2012-08-22 15:49:17 +02002020 master = spi_alloc_master(dev, sizeof(struct pl022) + sizeof(int) *
2021 platform_info->num_chipselect);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002022 if (master == NULL) {
2023 dev_err(&adev->dev, "probe - cannot alloc SPI master\n");
2024 status = -ENOMEM;
2025 goto err_no_master;
2026 }
2027
2028 pl022 = spi_master_get_devdata(master);
2029 pl022->master = master;
2030 pl022->master_info = platform_info;
2031 pl022->adev = adev;
2032 pl022->vendor = id->data;
Roland Stiggef6f46de2012-08-22 15:49:17 +02002033 /* Point chipselects to allocated memory beyond the main struct */
2034 pl022->chipselects = (int *) pl022 + sizeof(struct pl022);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002035
2036 /*
2037 * Bus Number Which has been Assigned to this SSP controller
2038 * on this board
2039 */
2040 master->bus_num = platform_info->bus_id;
2041 master->num_chipselect = platform_info->num_chipselect;
2042 master->cleanup = pl022_cleanup;
2043 master->setup = pl022_setup;
Linus Walleijffbbdd22012-02-22 10:05:38 +01002044 master->prepare_transfer_hardware = pl022_prepare_transfer_hardware;
2045 master->transfer_one_message = pl022_transfer_one_message;
2046 master->unprepare_transfer_hardware = pl022_unprepare_transfer_hardware;
2047 master->rt = platform_info->rt;
Linus Walleijb43d65f2009-06-09 08:11:42 +01002048
Roland Stiggef6f46de2012-08-22 15:49:17 +02002049 if (platform_info->num_chipselect && platform_info->chipselects)
2050 for (i = 0; i < platform_info->num_chipselect; i++)
2051 pl022->chipselects[i] = platform_info->chipselects[i];
2052
Kevin Wellsbde435a2010-09-16 06:18:50 -07002053 /*
2054 * Supports mode 0-3, loopback, and active low CS. Transfers are
2055 * always MS bit first on the original pl022.
2056 */
2057 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
2058 if (pl022->vendor->extended_cr)
2059 master->mode_bits |= SPI_LSB_FIRST;
2060
Linus Walleijb43d65f2009-06-09 08:11:42 +01002061 dev_dbg(&adev->dev, "BUSNO: %d\n", master->bus_num);
2062
2063 status = amba_request_regions(adev, NULL);
2064 if (status)
2065 goto err_no_ioregion;
2066
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09002067 pl022->phybase = adev->res.start;
Linus Walleijb43d65f2009-06-09 08:11:42 +01002068 pl022->virtbase = ioremap(adev->res.start, resource_size(&adev->res));
2069 if (pl022->virtbase == NULL) {
2070 status = -ENOMEM;
2071 goto err_no_ioremap;
2072 }
2073 printk(KERN_INFO "pl022: mapped registers from 0x%08x to %p\n",
2074 adev->res.start, pl022->virtbase);
2075
Linus Walleij2fb30d12012-06-12 16:14:51 +02002076 pm_runtime_enable(dev);
2077 pm_runtime_resume(dev);
2078
Linus Walleijb43d65f2009-06-09 08:11:42 +01002079 pl022->clk = clk_get(&adev->dev, NULL);
2080 if (IS_ERR(pl022->clk)) {
2081 status = PTR_ERR(pl022->clk);
2082 dev_err(&adev->dev, "could not retrieve SSP/SPI bus clock\n");
2083 goto err_no_clk;
2084 }
Russell King7ff6bcf2011-09-22 14:27:11 +01002085
2086 status = clk_prepare(pl022->clk);
2087 if (status) {
2088 dev_err(&adev->dev, "could not prepare SSP/SPI bus clock\n");
2089 goto err_clk_prep;
2090 }
2091
Ulf Hansson71e63e72011-11-04 08:10:09 +01002092 status = clk_enable(pl022->clk);
2093 if (status) {
2094 dev_err(&adev->dev, "could not enable SSP/SPI bus clock\n");
2095 goto err_no_clk_en;
2096 }
2097
Linus Walleijffbbdd22012-02-22 10:05:38 +01002098 /* Initialize transfer pump */
2099 tasklet_init(&pl022->pump_transfers, pump_transfers,
2100 (unsigned long)pl022);
2101
Linus Walleijb43d65f2009-06-09 08:11:42 +01002102 /* Disable SSP */
Linus Walleijb43d65f2009-06-09 08:11:42 +01002103 writew((readw(SSP_CR1(pl022->virtbase)) & (~SSP_CR1_MASK_SSE)),
2104 SSP_CR1(pl022->virtbase));
2105 load_ssp_default_config(pl022);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002106
2107 status = request_irq(adev->irq[0], pl022_interrupt_handler, 0, "pl022",
2108 pl022);
2109 if (status < 0) {
2110 dev_err(&adev->dev, "probe - cannot get IRQ (%d)\n", status);
2111 goto err_no_irq;
2112 }
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09002113
2114 /* Get DMA channels */
2115 if (platform_info->enable_dma) {
2116 status = pl022_dma_probe(pl022);
2117 if (status != 0)
Viresh Kumar43c64012011-05-16 09:40:10 +05302118 platform_info->enable_dma = 0;
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09002119 }
2120
Linus Walleijb43d65f2009-06-09 08:11:42 +01002121 /* Register with the SPI framework */
2122 amba_set_drvdata(adev, pl022);
2123 status = spi_register_master(master);
2124 if (status != 0) {
2125 dev_err(&adev->dev,
2126 "probe - problem registering spi master\n");
2127 goto err_spi_register;
2128 }
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002129 dev_dbg(dev, "probe succeeded\n");
Russell King92b97f02011-08-14 09:13:48 +01002130
2131 /* let runtime pm put suspend */
Chris Blair53e4ace2011-11-08 08:54:46 +00002132 if (platform_info->autosuspend_delay > 0) {
2133 dev_info(&adev->dev,
2134 "will use autosuspend for runtime pm, delay %dms\n",
2135 platform_info->autosuspend_delay);
2136 pm_runtime_set_autosuspend_delay(dev,
2137 platform_info->autosuspend_delay);
2138 pm_runtime_use_autosuspend(dev);
2139 pm_runtime_put_autosuspend(dev);
2140 } else {
2141 pm_runtime_put(dev);
2142 }
Linus Walleijb43d65f2009-06-09 08:11:42 +01002143 return 0;
2144
2145 err_spi_register:
Viresh Kumar3e3ea712011-08-10 14:20:58 +05302146 if (platform_info->enable_dma)
2147 pl022_dma_remove(pl022);
2148
Linus Walleijb43d65f2009-06-09 08:11:42 +01002149 free_irq(adev->irq[0], pl022);
2150 err_no_irq:
Ulf Hansson71e63e72011-11-04 08:10:09 +01002151 clk_disable(pl022->clk);
2152 err_no_clk_en:
Russell King7ff6bcf2011-09-22 14:27:11 +01002153 clk_unprepare(pl022->clk);
2154 err_clk_prep:
Linus Walleijb43d65f2009-06-09 08:11:42 +01002155 clk_put(pl022->clk);
2156 err_no_clk:
2157 iounmap(pl022->virtbase);
2158 err_no_ioremap:
2159 amba_release_regions(adev);
2160 err_no_ioregion:
2161 spi_master_put(master);
2162 err_no_master:
2163 err_no_pdata:
2164 return status;
2165}
2166
Kevin Wellsb4225882010-07-27 16:39:30 +00002167static int __devexit
Linus Walleijb43d65f2009-06-09 08:11:42 +01002168pl022_remove(struct amba_device *adev)
2169{
2170 struct pl022 *pl022 = amba_get_drvdata(adev);
Linus Walleij50658b62011-08-02 11:29:24 +02002171
Linus Walleijb43d65f2009-06-09 08:11:42 +01002172 if (!pl022)
2173 return 0;
2174
Russell King92b97f02011-08-14 09:13:48 +01002175 /*
2176 * undo pm_runtime_put() in probe. I assume that we're not
2177 * accessing the primecell here.
2178 */
2179 pm_runtime_get_noresume(&adev->dev);
2180
Linus Walleijb43d65f2009-06-09 08:11:42 +01002181 load_ssp_default_config(pl022);
Viresh Kumar3e3ea712011-08-10 14:20:58 +05302182 if (pl022->master_info->enable_dma)
2183 pl022_dma_remove(pl022);
2184
Linus Walleijb43d65f2009-06-09 08:11:42 +01002185 free_irq(adev->irq[0], pl022);
2186 clk_disable(pl022->clk);
Russell King7ff6bcf2011-09-22 14:27:11 +01002187 clk_unprepare(pl022->clk);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002188 clk_put(pl022->clk);
Linus Walleij2fb30d12012-06-12 16:14:51 +02002189 pm_runtime_disable(&adev->dev);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002190 iounmap(pl022->virtbase);
2191 amba_release_regions(adev);
2192 tasklet_disable(&pl022->pump_transfers);
2193 spi_unregister_master(pl022->master);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002194 amba_set_drvdata(adev, NULL);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002195 return 0;
2196}
2197
Russell King92b97f02011-08-14 09:13:48 +01002198#ifdef CONFIG_SUSPEND
Peter Hüwe6cfa6272011-09-05 21:07:23 +01002199static int pl022_suspend(struct device *dev)
Linus Walleijb43d65f2009-06-09 08:11:42 +01002200{
Russell King92b97f02011-08-14 09:13:48 +01002201 struct pl022 *pl022 = dev_get_drvdata(dev);
Linus Walleijffbbdd22012-02-22 10:05:38 +01002202 int ret;
Linus Walleijb43d65f2009-06-09 08:11:42 +01002203
Linus Walleijffbbdd22012-02-22 10:05:38 +01002204 ret = spi_master_suspend(pl022->master);
2205 if (ret) {
2206 dev_warn(dev, "cannot suspend master\n");
2207 return ret;
Linus Walleijb43d65f2009-06-09 08:11:42 +01002208 }
2209
Peter Hüwe6cfa6272011-09-05 21:07:23 +01002210 dev_dbg(dev, "suspended\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01002211 return 0;
2212}
2213
Russell King92b97f02011-08-14 09:13:48 +01002214static int pl022_resume(struct device *dev)
Linus Walleijb43d65f2009-06-09 08:11:42 +01002215{
Russell King92b97f02011-08-14 09:13:48 +01002216 struct pl022 *pl022 = dev_get_drvdata(dev);
Linus Walleijffbbdd22012-02-22 10:05:38 +01002217 int ret;
Linus Walleijb43d65f2009-06-09 08:11:42 +01002218
2219 /* Start the queue running */
Linus Walleijffbbdd22012-02-22 10:05:38 +01002220 ret = spi_master_resume(pl022->master);
2221 if (ret)
2222 dev_err(dev, "problem starting queue (%d)\n", ret);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002223 else
Russell King92b97f02011-08-14 09:13:48 +01002224 dev_dbg(dev, "resumed\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01002225
Linus Walleijffbbdd22012-02-22 10:05:38 +01002226 return ret;
Linus Walleijb43d65f2009-06-09 08:11:42 +01002227}
Linus Walleijb43d65f2009-06-09 08:11:42 +01002228#endif /* CONFIG_PM */
2229
Russell King92b97f02011-08-14 09:13:48 +01002230#ifdef CONFIG_PM_RUNTIME
2231static int pl022_runtime_suspend(struct device *dev)
2232{
2233 struct pl022 *pl022 = dev_get_drvdata(dev);
2234
2235 clk_disable(pl022->clk);
Russell King92b97f02011-08-14 09:13:48 +01002236
2237 return 0;
2238}
2239
2240static int pl022_runtime_resume(struct device *dev)
2241{
2242 struct pl022 *pl022 = dev_get_drvdata(dev);
2243
Russell King92b97f02011-08-14 09:13:48 +01002244 clk_enable(pl022->clk);
2245
2246 return 0;
2247}
2248#endif
2249
2250static const struct dev_pm_ops pl022_dev_pm_ops = {
2251 SET_SYSTEM_SLEEP_PM_OPS(pl022_suspend, pl022_resume)
2252 SET_RUNTIME_PM_OPS(pl022_runtime_suspend, pl022_runtime_resume, NULL)
2253};
2254
Linus Walleijb43d65f2009-06-09 08:11:42 +01002255static struct vendor_data vendor_arm = {
2256 .fifodepth = 8,
2257 .max_bpw = 16,
2258 .unidir = false,
Linus Walleij556f4ae2010-05-05 09:28:15 +00002259 .extended_cr = false,
Linus Walleij781c7b12010-05-07 08:40:53 +00002260 .pl023 = false,
Philippe Langlais06fb01f2011-03-23 11:05:16 +01002261 .loopback = true,
Linus Walleijb43d65f2009-06-09 08:11:42 +01002262};
2263
Linus Walleijb43d65f2009-06-09 08:11:42 +01002264static struct vendor_data vendor_st = {
2265 .fifodepth = 32,
2266 .max_bpw = 32,
2267 .unidir = false,
Linus Walleij556f4ae2010-05-05 09:28:15 +00002268 .extended_cr = true,
Linus Walleij781c7b12010-05-07 08:40:53 +00002269 .pl023 = false,
Philippe Langlais06fb01f2011-03-23 11:05:16 +01002270 .loopback = true,
Linus Walleij781c7b12010-05-07 08:40:53 +00002271};
2272
2273static struct vendor_data vendor_st_pl023 = {
2274 .fifodepth = 32,
2275 .max_bpw = 32,
2276 .unidir = false,
2277 .extended_cr = true,
2278 .pl023 = true,
Philippe Langlais06fb01f2011-03-23 11:05:16 +01002279 .loopback = false,
2280};
2281
Linus Walleijb43d65f2009-06-09 08:11:42 +01002282static struct amba_id pl022_ids[] = {
2283 {
2284 /*
2285 * ARM PL022 variant, this has a 16bit wide
2286 * and 8 locations deep TX/RX FIFO
2287 */
2288 .id = 0x00041022,
2289 .mask = 0x000fffff,
2290 .data = &vendor_arm,
2291 },
2292 {
2293 /*
2294 * ST Micro derivative, this has 32bit wide
2295 * and 32 locations deep TX/RX FIFO
2296 */
Srinidhi Kasagare89e04f2009-10-05 06:13:53 +01002297 .id = 0x01080022,
Linus Walleijb43d65f2009-06-09 08:11:42 +01002298 .mask = 0xffffffff,
2299 .data = &vendor_st,
2300 },
Linus Walleij781c7b12010-05-07 08:40:53 +00002301 {
2302 /*
2303 * ST-Ericsson derivative "PL023" (this is not
2304 * an official ARM number), this is a PL022 SSP block
2305 * stripped to SPI mode only, it has 32bit wide
2306 * and 32 locations deep TX/RX FIFO but no extended
2307 * CR0/CR1 register
2308 */
Viresh Kumarf1e45f82011-08-10 14:20:54 +05302309 .id = 0x00080023,
2310 .mask = 0xffffffff,
2311 .data = &vendor_st_pl023,
Linus Walleij781c7b12010-05-07 08:40:53 +00002312 },
Linus Walleijb43d65f2009-06-09 08:11:42 +01002313 { 0, 0 },
2314};
2315
Dave Martin7eeac712011-10-05 15:15:22 +01002316MODULE_DEVICE_TABLE(amba, pl022_ids);
2317
Linus Walleijb43d65f2009-06-09 08:11:42 +01002318static struct amba_driver pl022_driver = {
2319 .drv = {
2320 .name = "ssp-pl022",
Russell King92b97f02011-08-14 09:13:48 +01002321 .pm = &pl022_dev_pm_ops,
Linus Walleijb43d65f2009-06-09 08:11:42 +01002322 },
2323 .id_table = pl022_ids,
2324 .probe = pl022_probe,
Kevin Wellsb4225882010-07-27 16:39:30 +00002325 .remove = __devexit_p(pl022_remove),
Linus Walleijb43d65f2009-06-09 08:11:42 +01002326};
2327
Linus Walleijb43d65f2009-06-09 08:11:42 +01002328static int __init pl022_init(void)
2329{
2330 return amba_driver_register(&pl022_driver);
2331}
Linus Walleij25c8e032010-09-06 11:02:12 +02002332subsys_initcall(pl022_init);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002333
2334static void __exit pl022_exit(void)
2335{
2336 amba_driver_unregister(&pl022_driver);
2337}
Linus Walleijb43d65f2009-06-09 08:11:42 +01002338module_exit(pl022_exit);
2339
2340MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
2341MODULE_DESCRIPTION("PL022 SSP Controller Driver");
2342MODULE_LICENSE("GPL");