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