blob: 8fdf530f9f50d961c21040c03834741f872b50db [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 Walleij808f1032011-02-08 13:03:32 +0100511 /* This message is completed, so let's turn off the clocks & power */
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 Walleij808f1032011-02-08 13:03:32 +0100514 amba_vcore_disable(pl022->adev);
Linus Walleijb43d65f2009-06-09 08:11:42 +0100515}
516
517/**
518 * flush - flush the FIFO to reach a clean state
519 * @pl022: SSP driver private data structure
520 */
521static int flush(struct pl022 *pl022)
522{
523 unsigned long limit = loops_per_jiffy << 1;
524
525 dev_dbg(&pl022->adev->dev, "flush\n");
526 do {
527 while (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
528 readw(SSP_DR(pl022->virtbase));
529 } while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_BSY) && limit--);
Linus Walleijfc054752010-01-22 13:53:30 +0100530
531 pl022->exp_fifo_level = 0;
532
Linus Walleijb43d65f2009-06-09 08:11:42 +0100533 return limit;
534}
535
536/**
537 * restore_state - Load configuration of current chip
538 * @pl022: SSP driver private data structure
539 */
540static void restore_state(struct pl022 *pl022)
541{
542 struct chip_data *chip = pl022->cur_chip;
543
Linus Walleij556f4ae2010-05-05 09:28:15 +0000544 if (pl022->vendor->extended_cr)
545 writel(chip->cr0, SSP_CR0(pl022->virtbase));
546 else
547 writew(chip->cr0, SSP_CR0(pl022->virtbase));
Linus Walleijb43d65f2009-06-09 08:11:42 +0100548 writew(chip->cr1, SSP_CR1(pl022->virtbase));
549 writew(chip->dmacr, SSP_DMACR(pl022->virtbase));
550 writew(chip->cpsr, SSP_CPSR(pl022->virtbase));
551 writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
552 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
553}
554
Linus Walleijb43d65f2009-06-09 08:11:42 +0100555/*
556 * Default SSP Register Values
557 */
558#define DEFAULT_SSP_REG_CR0 ( \
559 GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS, 0) | \
Linus Walleij556f4ae2010-05-05 09:28:15 +0000560 GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF, 4) | \
Linus Walleijb43d65f2009-06-09 08:11:42 +0100561 GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
Linus Walleijee2b8052009-08-15 15:12:05 +0100562 GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
Linus Walleij556f4ae2010-05-05 09:28:15 +0000563 GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
564)
565
566/* ST versions have slightly different bit layout */
567#define DEFAULT_SSP_REG_CR0_ST ( \
568 GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
569 GEN_MASK_BITS(SSP_MICROWIRE_CHANNEL_FULL_DUPLEX, SSP_CR0_MASK_HALFDUP_ST, 5) | \
570 GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
571 GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
572 GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) | \
573 GEN_MASK_BITS(SSP_BITS_8, SSP_CR0_MASK_CSS_ST, 16) | \
574 GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF_ST, 21) \
Linus Walleijb43d65f2009-06-09 08:11:42 +0100575)
576
Linus Walleij781c7b12010-05-07 08:40:53 +0000577/* The PL023 version is slightly different again */
578#define DEFAULT_SSP_REG_CR0_ST_PL023 ( \
579 GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
580 GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
581 GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
582 GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
583)
584
Linus Walleijb43d65f2009-06-09 08:11:42 +0100585#define DEFAULT_SSP_REG_CR1 ( \
586 GEN_MASK_BITS(LOOPBACK_DISABLED, SSP_CR1_MASK_LBM, 0) | \
587 GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
588 GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
Linus Walleij556f4ae2010-05-05 09:28:15 +0000589 GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) \
Linus Walleijb43d65f2009-06-09 08:11:42 +0100590)
591
Linus Walleij556f4ae2010-05-05 09:28:15 +0000592/* ST versions extend this register to use all 16 bits */
593#define DEFAULT_SSP_REG_CR1_ST ( \
594 DEFAULT_SSP_REG_CR1 | \
595 GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
596 GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
597 GEN_MASK_BITS(SSP_MWIRE_WAIT_ZERO, SSP_CR1_MASK_MWAIT_ST, 6) |\
598 GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
599 GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) \
600)
601
Linus Walleij781c7b12010-05-07 08:40:53 +0000602/*
603 * The PL023 variant has further differences: no loopback mode, no microwire
604 * support, and a new clock feedback delay setting.
605 */
606#define DEFAULT_SSP_REG_CR1_ST_PL023 ( \
607 GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
608 GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
609 GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) | \
610 GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
611 GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
612 GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
613 GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) | \
614 GEN_MASK_BITS(SSP_FEEDBACK_CLK_DELAY_NONE, SSP_CR1_MASK_FBCLKDEL_ST, 13) \
615)
Linus Walleij556f4ae2010-05-05 09:28:15 +0000616
Linus Walleijb43d65f2009-06-09 08:11:42 +0100617#define DEFAULT_SSP_REG_CPSR ( \
Linus Walleij556f4ae2010-05-05 09:28:15 +0000618 GEN_MASK_BITS(SSP_DEFAULT_PRESCALE, SSP_CPSR_MASK_CPSDVSR, 0) \
Linus Walleijb43d65f2009-06-09 08:11:42 +0100619)
620
621#define DEFAULT_SSP_REG_DMACR (\
622 GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_RXDMAE, 0) | \
623 GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_TXDMAE, 1) \
624)
625
Linus Walleij781c7b12010-05-07 08:40:53 +0000626/**
627 * load_ssp_default_config - Load default configuration for SSP
628 * @pl022: SSP driver private data structure
629 */
Linus Walleijb43d65f2009-06-09 08:11:42 +0100630static void load_ssp_default_config(struct pl022 *pl022)
631{
Linus Walleij781c7b12010-05-07 08:40:53 +0000632 if (pl022->vendor->pl023) {
633 writel(DEFAULT_SSP_REG_CR0_ST_PL023, SSP_CR0(pl022->virtbase));
634 writew(DEFAULT_SSP_REG_CR1_ST_PL023, SSP_CR1(pl022->virtbase));
635 } else if (pl022->vendor->extended_cr) {
Linus Walleij556f4ae2010-05-05 09:28:15 +0000636 writel(DEFAULT_SSP_REG_CR0_ST, SSP_CR0(pl022->virtbase));
637 writew(DEFAULT_SSP_REG_CR1_ST, SSP_CR1(pl022->virtbase));
638 } else {
639 writew(DEFAULT_SSP_REG_CR0, SSP_CR0(pl022->virtbase));
640 writew(DEFAULT_SSP_REG_CR1, SSP_CR1(pl022->virtbase));
641 }
Linus Walleijb43d65f2009-06-09 08:11:42 +0100642 writew(DEFAULT_SSP_REG_DMACR, SSP_DMACR(pl022->virtbase));
643 writew(DEFAULT_SSP_REG_CPSR, SSP_CPSR(pl022->virtbase));
644 writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
645 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
646}
647
648/**
649 * This will write to TX and read from RX according to the parameters
650 * set in pl022.
651 */
652static void readwriter(struct pl022 *pl022)
653{
654
655 /*
656 * The FIFO depth is different inbetween primecell variants.
657 * I believe filling in too much in the FIFO might cause
658 * errons in 8bit wide transfers on ARM variants (just 8 words
659 * FIFO, means only 8x8 = 64 bits in FIFO) at least.
660 *
Linus Walleijfc054752010-01-22 13:53:30 +0100661 * To prevent this issue, the TX FIFO is only filled to the
662 * unused RX FIFO fill length, regardless of what the TX
663 * FIFO status flag indicates.
Linus Walleijb43d65f2009-06-09 08:11:42 +0100664 */
665 dev_dbg(&pl022->adev->dev,
666 "%s, rx: %p, rxend: %p, tx: %p, txend: %p\n",
667 __func__, pl022->rx, pl022->rx_end, pl022->tx, pl022->tx_end);
668
669 /* Read as much as you can */
670 while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
671 && (pl022->rx < pl022->rx_end)) {
672 switch (pl022->read) {
673 case READING_NULL:
674 readw(SSP_DR(pl022->virtbase));
675 break;
676 case READING_U8:
677 *(u8 *) (pl022->rx) =
678 readw(SSP_DR(pl022->virtbase)) & 0xFFU;
679 break;
680 case READING_U16:
681 *(u16 *) (pl022->rx) =
682 (u16) readw(SSP_DR(pl022->virtbase));
683 break;
684 case READING_U32:
685 *(u32 *) (pl022->rx) =
686 readl(SSP_DR(pl022->virtbase));
687 break;
688 }
689 pl022->rx += (pl022->cur_chip->n_bytes);
Linus Walleijfc054752010-01-22 13:53:30 +0100690 pl022->exp_fifo_level--;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100691 }
692 /*
Linus Walleijfc054752010-01-22 13:53:30 +0100693 * Write as much as possible up to the RX FIFO size
Linus Walleijb43d65f2009-06-09 08:11:42 +0100694 */
Linus Walleijfc054752010-01-22 13:53:30 +0100695 while ((pl022->exp_fifo_level < pl022->vendor->fifodepth)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100696 && (pl022->tx < pl022->tx_end)) {
697 switch (pl022->write) {
698 case WRITING_NULL:
699 writew(0x0, SSP_DR(pl022->virtbase));
700 break;
701 case WRITING_U8:
702 writew(*(u8 *) (pl022->tx), SSP_DR(pl022->virtbase));
703 break;
704 case WRITING_U16:
705 writew((*(u16 *) (pl022->tx)), SSP_DR(pl022->virtbase));
706 break;
707 case WRITING_U32:
708 writel(*(u32 *) (pl022->tx), SSP_DR(pl022->virtbase));
709 break;
710 }
711 pl022->tx += (pl022->cur_chip->n_bytes);
Linus Walleijfc054752010-01-22 13:53:30 +0100712 pl022->exp_fifo_level++;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100713 /*
714 * This inner reader takes care of things appearing in the RX
715 * FIFO as we're transmitting. This will happen a lot since the
716 * clock starts running when you put things into the TX FIFO,
717 * and then things are continously clocked into the RX FIFO.
718 */
719 while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
720 && (pl022->rx < pl022->rx_end)) {
721 switch (pl022->read) {
722 case READING_NULL:
723 readw(SSP_DR(pl022->virtbase));
724 break;
725 case READING_U8:
726 *(u8 *) (pl022->rx) =
727 readw(SSP_DR(pl022->virtbase)) & 0xFFU;
728 break;
729 case READING_U16:
730 *(u16 *) (pl022->rx) =
731 (u16) readw(SSP_DR(pl022->virtbase));
732 break;
733 case READING_U32:
734 *(u32 *) (pl022->rx) =
735 readl(SSP_DR(pl022->virtbase));
736 break;
737 }
738 pl022->rx += (pl022->cur_chip->n_bytes);
Linus Walleijfc054752010-01-22 13:53:30 +0100739 pl022->exp_fifo_level--;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100740 }
741 }
742 /*
743 * When we exit here the TX FIFO should be full and the RX FIFO
744 * should be empty
745 */
746}
747
748
749/**
750 * next_transfer - Move to the Next transfer in the current spi message
751 * @pl022: SSP driver private data structure
752 *
753 * This function moves though the linked list of spi transfers in the
754 * current spi message and returns with the state of current spi
755 * message i.e whether its last transfer is done(STATE_DONE) or
756 * Next transfer is ready(STATE_RUNNING)
757 */
758static void *next_transfer(struct pl022 *pl022)
759{
760 struct spi_message *msg = pl022->cur_msg;
761 struct spi_transfer *trans = pl022->cur_transfer;
762
763 /* Move to next transfer */
764 if (trans->transfer_list.next != &msg->transfers) {
765 pl022->cur_transfer =
766 list_entry(trans->transfer_list.next,
767 struct spi_transfer, transfer_list);
768 return STATE_RUNNING;
769 }
770 return STATE_DONE;
771}
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900772
773/*
774 * This DMA functionality is only compiled in if we have
775 * access to the generic DMA devices/DMA engine.
776 */
777#ifdef CONFIG_DMA_ENGINE
778static void unmap_free_dma_scatter(struct pl022 *pl022)
779{
780 /* Unmap and free the SG tables */
Linus Walleijb7298892010-12-22 23:13:07 +0100781 dma_unmap_sg(pl022->dma_tx_channel->device->dev, pl022->sgt_tx.sgl,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900782 pl022->sgt_tx.nents, DMA_TO_DEVICE);
Linus Walleijb7298892010-12-22 23:13:07 +0100783 dma_unmap_sg(pl022->dma_rx_channel->device->dev, pl022->sgt_rx.sgl,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900784 pl022->sgt_rx.nents, DMA_FROM_DEVICE);
785 sg_free_table(&pl022->sgt_rx);
786 sg_free_table(&pl022->sgt_tx);
787}
788
789static void dma_callback(void *data)
790{
791 struct pl022 *pl022 = data;
792 struct spi_message *msg = pl022->cur_msg;
793
794 BUG_ON(!pl022->sgt_rx.sgl);
795
796#ifdef VERBOSE_DEBUG
797 /*
798 * Optionally dump out buffers to inspect contents, this is
799 * good if you want to convince yourself that the loopback
800 * read/write contents are the same, when adopting to a new
801 * DMA engine.
802 */
803 {
804 struct scatterlist *sg;
805 unsigned int i;
806
807 dma_sync_sg_for_cpu(&pl022->adev->dev,
808 pl022->sgt_rx.sgl,
809 pl022->sgt_rx.nents,
810 DMA_FROM_DEVICE);
811
812 for_each_sg(pl022->sgt_rx.sgl, sg, pl022->sgt_rx.nents, i) {
813 dev_dbg(&pl022->adev->dev, "SPI RX SG ENTRY: %d", i);
814 print_hex_dump(KERN_ERR, "SPI RX: ",
815 DUMP_PREFIX_OFFSET,
816 16,
817 1,
818 sg_virt(sg),
819 sg_dma_len(sg),
820 1);
821 }
822 for_each_sg(pl022->sgt_tx.sgl, sg, pl022->sgt_tx.nents, i) {
823 dev_dbg(&pl022->adev->dev, "SPI TX SG ENTRY: %d", i);
824 print_hex_dump(KERN_ERR, "SPI TX: ",
825 DUMP_PREFIX_OFFSET,
826 16,
827 1,
828 sg_virt(sg),
829 sg_dma_len(sg),
830 1);
831 }
832 }
833#endif
834
835 unmap_free_dma_scatter(pl022);
836
837 /* Update total bytes transfered */
838 msg->actual_length += pl022->cur_transfer->len;
839 if (pl022->cur_transfer->cs_change)
840 pl022->cur_chip->
841 cs_control(SSP_CHIP_DESELECT);
842
843 /* Move to next transfer */
844 msg->state = next_transfer(pl022);
845 tasklet_schedule(&pl022->pump_transfers);
846}
847
848static void setup_dma_scatter(struct pl022 *pl022,
849 void *buffer,
850 unsigned int length,
851 struct sg_table *sgtab)
852{
853 struct scatterlist *sg;
854 int bytesleft = length;
855 void *bufp = buffer;
856 int mapbytes;
857 int i;
858
859 if (buffer) {
860 for_each_sg(sgtab->sgl, sg, sgtab->nents, i) {
861 /*
862 * If there are less bytes left than what fits
863 * in the current page (plus page alignment offset)
864 * we just feed in this, else we stuff in as much
865 * as we can.
866 */
867 if (bytesleft < (PAGE_SIZE - offset_in_page(bufp)))
868 mapbytes = bytesleft;
869 else
870 mapbytes = PAGE_SIZE - offset_in_page(bufp);
871 sg_set_page(sg, virt_to_page(bufp),
872 mapbytes, offset_in_page(bufp));
873 bufp += mapbytes;
874 bytesleft -= mapbytes;
875 dev_dbg(&pl022->adev->dev,
876 "set RX/TX target page @ %p, %d bytes, %d left\n",
877 bufp, mapbytes, bytesleft);
878 }
879 } else {
880 /* Map the dummy buffer on every page */
881 for_each_sg(sgtab->sgl, sg, sgtab->nents, i) {
882 if (bytesleft < PAGE_SIZE)
883 mapbytes = bytesleft;
884 else
885 mapbytes = PAGE_SIZE;
886 sg_set_page(sg, virt_to_page(pl022->dummypage),
887 mapbytes, 0);
888 bytesleft -= mapbytes;
889 dev_dbg(&pl022->adev->dev,
890 "set RX/TX to dummy page %d bytes, %d left\n",
891 mapbytes, bytesleft);
892
893 }
894 }
895 BUG_ON(bytesleft);
896}
897
898/**
899 * configure_dma - configures the channels for the next transfer
900 * @pl022: SSP driver's private data structure
901 */
902static int configure_dma(struct pl022 *pl022)
903{
904 struct dma_slave_config rx_conf = {
905 .src_addr = SSP_DR(pl022->phybase),
906 .direction = DMA_FROM_DEVICE,
907 .src_maxburst = pl022->vendor->fifodepth >> 1,
908 };
909 struct dma_slave_config tx_conf = {
910 .dst_addr = SSP_DR(pl022->phybase),
911 .direction = DMA_TO_DEVICE,
912 .dst_maxburst = pl022->vendor->fifodepth >> 1,
913 };
914 unsigned int pages;
915 int ret;
Linus Walleij082086f2010-12-22 23:13:37 +0100916 int rx_sglen, tx_sglen;
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900917 struct dma_chan *rxchan = pl022->dma_rx_channel;
918 struct dma_chan *txchan = pl022->dma_tx_channel;
919 struct dma_async_tx_descriptor *rxdesc;
920 struct dma_async_tx_descriptor *txdesc;
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900921
922 /* Check that the channels are available */
923 if (!rxchan || !txchan)
924 return -ENODEV;
925
926 switch (pl022->read) {
927 case READING_NULL:
928 /* Use the same as for writing */
929 rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
930 break;
931 case READING_U8:
932 rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
933 break;
934 case READING_U16:
935 rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
936 break;
937 case READING_U32:
938 rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
939 break;
940 }
941
942 switch (pl022->write) {
943 case WRITING_NULL:
944 /* Use the same as for reading */
945 tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
946 break;
947 case WRITING_U8:
948 tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
949 break;
950 case WRITING_U16:
951 tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
952 break;
953 case WRITING_U32:
Joe Perchesbc3f67a2010-11-14 19:04:47 -0800954 tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900955 break;
956 }
957
958 /* SPI pecularity: we need to read and write the same width */
959 if (rx_conf.src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
960 rx_conf.src_addr_width = tx_conf.dst_addr_width;
961 if (tx_conf.dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
962 tx_conf.dst_addr_width = rx_conf.src_addr_width;
963 BUG_ON(rx_conf.src_addr_width != tx_conf.dst_addr_width);
964
Linus Walleijecd442f2011-02-08 13:03:12 +0100965 dmaengine_slave_config(rxchan, &rx_conf);
966 dmaengine_slave_config(txchan, &tx_conf);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900967
968 /* Create sglists for the transfers */
969 pages = (pl022->cur_transfer->len >> PAGE_SHIFT) + 1;
970 dev_dbg(&pl022->adev->dev, "using %d pages for transfer\n", pages);
971
972 ret = sg_alloc_table(&pl022->sgt_rx, pages, GFP_KERNEL);
973 if (ret)
974 goto err_alloc_rx_sg;
975
976 ret = sg_alloc_table(&pl022->sgt_tx, pages, GFP_KERNEL);
977 if (ret)
978 goto err_alloc_tx_sg;
979
980 /* Fill in the scatterlists for the RX+TX buffers */
981 setup_dma_scatter(pl022, pl022->rx,
982 pl022->cur_transfer->len, &pl022->sgt_rx);
983 setup_dma_scatter(pl022, pl022->tx,
984 pl022->cur_transfer->len, &pl022->sgt_tx);
985
986 /* Map DMA buffers */
Linus Walleij082086f2010-12-22 23:13:37 +0100987 rx_sglen = dma_map_sg(rxchan->device->dev, pl022->sgt_rx.sgl,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900988 pl022->sgt_rx.nents, DMA_FROM_DEVICE);
Linus Walleij082086f2010-12-22 23:13:37 +0100989 if (!rx_sglen)
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900990 goto err_rx_sgmap;
991
Linus Walleij082086f2010-12-22 23:13:37 +0100992 tx_sglen = dma_map_sg(txchan->device->dev, pl022->sgt_tx.sgl,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900993 pl022->sgt_tx.nents, DMA_TO_DEVICE);
Linus Walleij082086f2010-12-22 23:13:37 +0100994 if (!tx_sglen)
Linus Walleijb1b6b9a2010-09-29 17:31:35 +0900995 goto err_tx_sgmap;
996
997 /* Send both scatterlists */
998 rxdesc = rxchan->device->device_prep_slave_sg(rxchan,
999 pl022->sgt_rx.sgl,
Linus Walleij082086f2010-12-22 23:13:37 +01001000 rx_sglen,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001001 DMA_FROM_DEVICE,
1002 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1003 if (!rxdesc)
1004 goto err_rxdesc;
1005
1006 txdesc = txchan->device->device_prep_slave_sg(txchan,
1007 pl022->sgt_tx.sgl,
Linus Walleij082086f2010-12-22 23:13:37 +01001008 tx_sglen,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001009 DMA_TO_DEVICE,
1010 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1011 if (!txdesc)
1012 goto err_txdesc;
1013
1014 /* Put the callback on the RX transfer only, that should finish last */
1015 rxdesc->callback = dma_callback;
1016 rxdesc->callback_param = pl022;
1017
1018 /* Submit and fire RX and TX with TX last so we're ready to read! */
Linus Walleijecd442f2011-02-08 13:03:12 +01001019 dmaengine_submit(rxdesc);
1020 dmaengine_submit(txdesc);
1021 dma_async_issue_pending(rxchan);
1022 dma_async_issue_pending(txchan);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001023
1024 return 0;
1025
1026err_submit_tx:
1027err_submit_rx:
1028err_txdesc:
Linus Walleijecd442f2011-02-08 13:03:12 +01001029 dmaengine_terminate_all(txchan);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001030err_rxdesc:
Linus Walleijecd442f2011-02-08 13:03:12 +01001031 dmaengine_terminate_all(rxchan);
Linus Walleijb7298892010-12-22 23:13:07 +01001032 dma_unmap_sg(txchan->device->dev, pl022->sgt_tx.sgl,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001033 pl022->sgt_tx.nents, DMA_TO_DEVICE);
1034err_tx_sgmap:
Linus Walleijb7298892010-12-22 23:13:07 +01001035 dma_unmap_sg(rxchan->device->dev, pl022->sgt_rx.sgl,
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001036 pl022->sgt_tx.nents, DMA_FROM_DEVICE);
1037err_rx_sgmap:
1038 sg_free_table(&pl022->sgt_tx);
1039err_alloc_tx_sg:
1040 sg_free_table(&pl022->sgt_rx);
1041err_alloc_rx_sg:
1042 return -ENOMEM;
1043}
1044
1045static int __init pl022_dma_probe(struct pl022 *pl022)
1046{
1047 dma_cap_mask_t mask;
1048
1049 /* Try to acquire a generic DMA engine slave channel */
1050 dma_cap_zero(mask);
1051 dma_cap_set(DMA_SLAVE, mask);
1052 /*
1053 * We need both RX and TX channels to do DMA, else do none
1054 * of them.
1055 */
1056 pl022->dma_rx_channel = dma_request_channel(mask,
1057 pl022->master_info->dma_filter,
1058 pl022->master_info->dma_rx_param);
1059 if (!pl022->dma_rx_channel) {
1060 dev_err(&pl022->adev->dev, "no RX DMA channel!\n");
1061 goto err_no_rxchan;
1062 }
1063
1064 pl022->dma_tx_channel = dma_request_channel(mask,
1065 pl022->master_info->dma_filter,
1066 pl022->master_info->dma_tx_param);
1067 if (!pl022->dma_tx_channel) {
1068 dev_err(&pl022->adev->dev, "no TX DMA channel!\n");
1069 goto err_no_txchan;
1070 }
1071
1072 pl022->dummypage = kmalloc(PAGE_SIZE, GFP_KERNEL);
1073 if (!pl022->dummypage) {
1074 dev_err(&pl022->adev->dev, "no DMA dummypage!\n");
1075 goto err_no_dummypage;
1076 }
1077
1078 dev_info(&pl022->adev->dev, "setup for DMA on RX %s, TX %s\n",
1079 dma_chan_name(pl022->dma_rx_channel),
1080 dma_chan_name(pl022->dma_tx_channel));
1081
1082 return 0;
1083
1084err_no_dummypage:
1085 dma_release_channel(pl022->dma_tx_channel);
1086err_no_txchan:
1087 dma_release_channel(pl022->dma_rx_channel);
1088 pl022->dma_rx_channel = NULL;
1089err_no_rxchan:
1090 return -ENODEV;
1091}
1092
1093static void terminate_dma(struct pl022 *pl022)
1094{
1095 struct dma_chan *rxchan = pl022->dma_rx_channel;
1096 struct dma_chan *txchan = pl022->dma_tx_channel;
1097
Linus Walleijecd442f2011-02-08 13:03:12 +01001098 dmaengine_terminate_all(rxchan);
1099 dmaengine_terminate_all(txchan);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001100 unmap_free_dma_scatter(pl022);
1101}
1102
1103static void pl022_dma_remove(struct pl022 *pl022)
1104{
1105 if (pl022->busy)
1106 terminate_dma(pl022);
1107 if (pl022->dma_tx_channel)
1108 dma_release_channel(pl022->dma_tx_channel);
1109 if (pl022->dma_rx_channel)
1110 dma_release_channel(pl022->dma_rx_channel);
1111 kfree(pl022->dummypage);
1112}
1113
1114#else
1115static inline int configure_dma(struct pl022 *pl022)
1116{
1117 return -ENODEV;
1118}
1119
1120static inline int pl022_dma_probe(struct pl022 *pl022)
1121{
1122 return 0;
1123}
1124
1125static inline void pl022_dma_remove(struct pl022 *pl022)
1126{
1127}
1128#endif
1129
Linus Walleijb43d65f2009-06-09 08:11:42 +01001130/**
1131 * pl022_interrupt_handler - Interrupt handler for SSP controller
1132 *
1133 * This function handles interrupts generated for an interrupt based transfer.
1134 * If a receive overrun (ROR) interrupt is there then we disable SSP, flag the
1135 * current message's state as STATE_ERROR and schedule the tasklet
1136 * pump_transfers which will do the postprocessing of the current message by
1137 * calling giveback(). Otherwise it reads data from RX FIFO till there is no
1138 * more data, and writes data in TX FIFO till it is not full. If we complete
1139 * the transfer we move to the next transfer and schedule the tasklet.
1140 */
1141static irqreturn_t pl022_interrupt_handler(int irq, void *dev_id)
1142{
1143 struct pl022 *pl022 = dev_id;
1144 struct spi_message *msg = pl022->cur_msg;
1145 u16 irq_status = 0;
1146 u16 flag = 0;
1147
1148 if (unlikely(!msg)) {
1149 dev_err(&pl022->adev->dev,
1150 "bad message state in interrupt handler");
1151 /* Never fail */
1152 return IRQ_HANDLED;
1153 }
1154
1155 /* Read the Interrupt Status Register */
1156 irq_status = readw(SSP_MIS(pl022->virtbase));
1157
1158 if (unlikely(!irq_status))
1159 return IRQ_NONE;
1160
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001161 /*
1162 * This handles the FIFO interrupts, the timeout
1163 * interrupts are flatly ignored, they cannot be
1164 * trusted.
1165 */
Linus Walleijb43d65f2009-06-09 08:11:42 +01001166 if (unlikely(irq_status & SSP_MIS_MASK_RORMIS)) {
1167 /*
1168 * Overrun interrupt - bail out since our Data has been
1169 * corrupted
1170 */
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001171 dev_err(&pl022->adev->dev, "FIFO overrun\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01001172 if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RFF)
1173 dev_err(&pl022->adev->dev,
1174 "RXFIFO is full\n");
1175 if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_TNF)
1176 dev_err(&pl022->adev->dev,
1177 "TXFIFO is full\n");
1178
1179 /*
1180 * Disable and clear interrupts, disable SSP,
1181 * mark message with bad status so it can be
1182 * retried.
1183 */
1184 writew(DISABLE_ALL_INTERRUPTS,
1185 SSP_IMSC(pl022->virtbase));
1186 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
1187 writew((readw(SSP_CR1(pl022->virtbase)) &
1188 (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
1189 msg->state = STATE_ERROR;
1190
1191 /* Schedule message queue handler */
1192 tasklet_schedule(&pl022->pump_transfers);
1193 return IRQ_HANDLED;
1194 }
1195
1196 readwriter(pl022);
1197
1198 if ((pl022->tx == pl022->tx_end) && (flag == 0)) {
1199 flag = 1;
1200 /* Disable Transmit interrupt */
1201 writew(readw(SSP_IMSC(pl022->virtbase)) &
1202 (~SSP_IMSC_MASK_TXIM),
1203 SSP_IMSC(pl022->virtbase));
1204 }
1205
1206 /*
1207 * Since all transactions must write as much as shall be read,
1208 * we can conclude the entire transaction once RX is complete.
1209 * At this point, all TX will always be finished.
1210 */
1211 if (pl022->rx >= pl022->rx_end) {
1212 writew(DISABLE_ALL_INTERRUPTS,
1213 SSP_IMSC(pl022->virtbase));
1214 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
1215 if (unlikely(pl022->rx > pl022->rx_end)) {
1216 dev_warn(&pl022->adev->dev, "read %u surplus "
1217 "bytes (did you request an odd "
1218 "number of bytes on a 16bit bus?)\n",
1219 (u32) (pl022->rx - pl022->rx_end));
1220 }
1221 /* Update total bytes transfered */
1222 msg->actual_length += pl022->cur_transfer->len;
1223 if (pl022->cur_transfer->cs_change)
1224 pl022->cur_chip->
1225 cs_control(SSP_CHIP_DESELECT);
1226 /* Move to next transfer */
1227 msg->state = next_transfer(pl022);
1228 tasklet_schedule(&pl022->pump_transfers);
1229 return IRQ_HANDLED;
1230 }
1231
1232 return IRQ_HANDLED;
1233}
1234
1235/**
1236 * This sets up the pointers to memory for the next message to
1237 * send out on the SPI bus.
1238 */
1239static int set_up_next_transfer(struct pl022 *pl022,
1240 struct spi_transfer *transfer)
1241{
1242 int residue;
1243
1244 /* Sanity check the message for this bus width */
1245 residue = pl022->cur_transfer->len % pl022->cur_chip->n_bytes;
1246 if (unlikely(residue != 0)) {
1247 dev_err(&pl022->adev->dev,
1248 "message of %u bytes to transmit but the current "
1249 "chip bus has a data width of %u bytes!\n",
1250 pl022->cur_transfer->len,
1251 pl022->cur_chip->n_bytes);
1252 dev_err(&pl022->adev->dev, "skipping this message\n");
1253 return -EIO;
1254 }
1255 pl022->tx = (void *)transfer->tx_buf;
1256 pl022->tx_end = pl022->tx + pl022->cur_transfer->len;
1257 pl022->rx = (void *)transfer->rx_buf;
1258 pl022->rx_end = pl022->rx + pl022->cur_transfer->len;
1259 pl022->write =
1260 pl022->tx ? pl022->cur_chip->write : WRITING_NULL;
1261 pl022->read = pl022->rx ? pl022->cur_chip->read : READING_NULL;
1262 return 0;
1263}
1264
1265/**
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001266 * pump_transfers - Tasklet function which schedules next transfer
1267 * when running in interrupt or DMA transfer mode.
Linus Walleijb43d65f2009-06-09 08:11:42 +01001268 * @data: SSP driver private data structure
1269 *
1270 */
1271static void pump_transfers(unsigned long data)
1272{
1273 struct pl022 *pl022 = (struct pl022 *) data;
1274 struct spi_message *message = NULL;
1275 struct spi_transfer *transfer = NULL;
1276 struct spi_transfer *previous = NULL;
1277
1278 /* Get current state information */
1279 message = pl022->cur_msg;
1280 transfer = pl022->cur_transfer;
1281
1282 /* Handle for abort */
1283 if (message->state == STATE_ERROR) {
1284 message->status = -EIO;
1285 giveback(pl022);
1286 return;
1287 }
1288
1289 /* Handle end of message */
1290 if (message->state == STATE_DONE) {
1291 message->status = 0;
1292 giveback(pl022);
1293 return;
1294 }
1295
1296 /* Delay if requested at end of transfer before CS change */
1297 if (message->state == STATE_RUNNING) {
1298 previous = list_entry(transfer->transfer_list.prev,
1299 struct spi_transfer,
1300 transfer_list);
1301 if (previous->delay_usecs)
1302 /*
1303 * FIXME: This runs in interrupt context.
1304 * Is this really smart?
1305 */
1306 udelay(previous->delay_usecs);
1307
1308 /* Drop chip select only if cs_change is requested */
1309 if (previous->cs_change)
1310 pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
1311 } else {
1312 /* STATE_START */
1313 message->state = STATE_RUNNING;
1314 }
1315
1316 if (set_up_next_transfer(pl022, transfer)) {
1317 message->state = STATE_ERROR;
1318 message->status = -EIO;
1319 giveback(pl022);
1320 return;
1321 }
1322 /* Flush the FIFOs and let's go! */
1323 flush(pl022);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001324
1325 if (pl022->cur_chip->enable_dma) {
1326 if (configure_dma(pl022)) {
1327 dev_dbg(&pl022->adev->dev,
1328 "configuration of DMA failed, fall back to interrupt mode\n");
1329 goto err_config_dma;
1330 }
1331 return;
1332 }
1333
1334err_config_dma:
Linus Walleijb43d65f2009-06-09 08:11:42 +01001335 writew(ENABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
1336}
1337
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001338static void do_interrupt_dma_transfer(struct pl022 *pl022)
Linus Walleijb43d65f2009-06-09 08:11:42 +01001339{
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001340 u32 irqflags = ENABLE_ALL_INTERRUPTS;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001341
1342 /* Enable target chip */
1343 pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
1344 if (set_up_next_transfer(pl022, pl022->cur_transfer)) {
1345 /* Error path */
1346 pl022->cur_msg->state = STATE_ERROR;
1347 pl022->cur_msg->status = -EIO;
1348 giveback(pl022);
1349 return;
1350 }
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001351 /* If we're using DMA, set up DMA here */
1352 if (pl022->cur_chip->enable_dma) {
1353 /* Configure DMA transfer */
1354 if (configure_dma(pl022)) {
1355 dev_dbg(&pl022->adev->dev,
1356 "configuration of DMA failed, fall back to interrupt mode\n");
1357 goto err_config_dma;
1358 }
1359 /* Disable interrupts in DMA mode, IRQ from DMA controller */
1360 irqflags = DISABLE_ALL_INTERRUPTS;
1361 }
1362err_config_dma:
Linus Walleijb43d65f2009-06-09 08:11:42 +01001363 /* Enable SSP, turn on interrupts */
1364 writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
1365 SSP_CR1(pl022->virtbase));
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001366 writew(irqflags, SSP_IMSC(pl022->virtbase));
Linus Walleijb43d65f2009-06-09 08:11:42 +01001367}
1368
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001369static void do_polling_transfer(struct pl022 *pl022)
Linus Walleijb43d65f2009-06-09 08:11:42 +01001370{
Linus Walleijb43d65f2009-06-09 08:11:42 +01001371 struct spi_message *message = NULL;
1372 struct spi_transfer *transfer = NULL;
1373 struct spi_transfer *previous = NULL;
1374 struct chip_data *chip;
1375
1376 chip = pl022->cur_chip;
1377 message = pl022->cur_msg;
1378
1379 while (message->state != STATE_DONE) {
1380 /* Handle for abort */
1381 if (message->state == STATE_ERROR)
1382 break;
1383 transfer = pl022->cur_transfer;
1384
1385 /* Delay if requested at end of transfer */
1386 if (message->state == STATE_RUNNING) {
1387 previous =
1388 list_entry(transfer->transfer_list.prev,
1389 struct spi_transfer, transfer_list);
1390 if (previous->delay_usecs)
1391 udelay(previous->delay_usecs);
1392 if (previous->cs_change)
1393 pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
1394 } else {
1395 /* STATE_START */
1396 message->state = STATE_RUNNING;
1397 pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
1398 }
1399
1400 /* Configuration Changing Per Transfer */
1401 if (set_up_next_transfer(pl022, transfer)) {
1402 /* Error path */
1403 message->state = STATE_ERROR;
1404 break;
1405 }
1406 /* Flush FIFOs and enable SSP */
1407 flush(pl022);
1408 writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
1409 SSP_CR1(pl022->virtbase));
1410
Linus Walleij556f4ae2010-05-05 09:28:15 +00001411 dev_dbg(&pl022->adev->dev, "polling transfer ongoing ...\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01001412 /* FIXME: insert a timeout so we don't hang here indefinately */
1413 while (pl022->tx < pl022->tx_end || pl022->rx < pl022->rx_end)
1414 readwriter(pl022);
1415
1416 /* Update total byte transfered */
1417 message->actual_length += pl022->cur_transfer->len;
1418 if (pl022->cur_transfer->cs_change)
1419 pl022->cur_chip->cs_control(SSP_CHIP_DESELECT);
1420 /* Move to next transfer */
1421 message->state = next_transfer(pl022);
1422 }
1423
1424 /* Handle end of message */
1425 if (message->state == STATE_DONE)
1426 message->status = 0;
1427 else
1428 message->status = -EIO;
1429
1430 giveback(pl022);
1431 return;
1432}
1433
1434/**
1435 * pump_messages - Workqueue function which processes spi message queue
1436 * @data: pointer to private data of SSP driver
1437 *
1438 * This function checks if there is any spi message in the queue that
1439 * needs processing and delegate control to appropriate function
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001440 * do_polling_transfer()/do_interrupt_dma_transfer()
Linus Walleijb43d65f2009-06-09 08:11:42 +01001441 * based on the kind of the transfer
1442 *
1443 */
1444static void pump_messages(struct work_struct *work)
1445{
1446 struct pl022 *pl022 =
1447 container_of(work, struct pl022, pump_messages);
1448 unsigned long flags;
1449
1450 /* Lock queue and check for queue work */
1451 spin_lock_irqsave(&pl022->queue_lock, flags);
Linus Walleij5e8b8212010-12-22 23:13:59 +01001452 if (list_empty(&pl022->queue) || !pl022->running) {
Linus Walleijdec5a582010-12-22 23:13:48 +01001453 pl022->busy = false;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001454 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1455 return;
1456 }
1457 /* Make sure we are not already running a message */
1458 if (pl022->cur_msg) {
1459 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1460 return;
1461 }
1462 /* Extract head of queue */
1463 pl022->cur_msg =
1464 list_entry(pl022->queue.next, struct spi_message, queue);
1465
1466 list_del_init(&pl022->cur_msg->queue);
Linus Walleijdec5a582010-12-22 23:13:48 +01001467 pl022->busy = true;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001468 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1469
1470 /* Initial message state */
1471 pl022->cur_msg->state = STATE_START;
1472 pl022->cur_transfer = list_entry(pl022->cur_msg->transfers.next,
1473 struct spi_transfer,
1474 transfer_list);
1475
1476 /* Setup the SPI using the per chip configuration */
1477 pl022->cur_chip = spi_get_ctldata(pl022->cur_msg->spi);
1478 /*
Linus Walleij808f1032011-02-08 13:03:32 +01001479 * We enable the core voltage and clocks here, then the clocks
1480 * and core will be disabled when giveback() is called in each method
1481 * (poll/interrupt/DMA)
Linus Walleijb43d65f2009-06-09 08:11:42 +01001482 */
Linus Walleij808f1032011-02-08 13:03:32 +01001483 amba_vcore_enable(pl022->adev);
Linus Walleij545074f2010-08-21 11:07:36 +02001484 amba_pclk_enable(pl022->adev);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001485 clk_enable(pl022->clk);
1486 restore_state(pl022);
1487 flush(pl022);
1488
1489 if (pl022->cur_chip->xfer_type == POLLING_TRANSFER)
1490 do_polling_transfer(pl022);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001491 else
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001492 do_interrupt_dma_transfer(pl022);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001493}
1494
1495
1496static int __init init_queue(struct pl022 *pl022)
1497{
1498 INIT_LIST_HEAD(&pl022->queue);
1499 spin_lock_init(&pl022->queue_lock);
1500
Linus Walleij5e8b8212010-12-22 23:13:59 +01001501 pl022->running = false;
Linus Walleijdec5a582010-12-22 23:13:48 +01001502 pl022->busy = false;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001503
1504 tasklet_init(&pl022->pump_transfers,
1505 pump_transfers, (unsigned long)pl022);
1506
1507 INIT_WORK(&pl022->pump_messages, pump_messages);
1508 pl022->workqueue = create_singlethread_workqueue(
1509 dev_name(pl022->master->dev.parent));
1510 if (pl022->workqueue == NULL)
1511 return -EBUSY;
1512
1513 return 0;
1514}
1515
1516
1517static int start_queue(struct pl022 *pl022)
1518{
1519 unsigned long flags;
1520
1521 spin_lock_irqsave(&pl022->queue_lock, flags);
1522
Linus Walleij5e8b8212010-12-22 23:13:59 +01001523 if (pl022->running || pl022->busy) {
Linus Walleijb43d65f2009-06-09 08:11:42 +01001524 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1525 return -EBUSY;
1526 }
1527
Linus Walleij5e8b8212010-12-22 23:13:59 +01001528 pl022->running = true;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001529 pl022->cur_msg = NULL;
1530 pl022->cur_transfer = NULL;
1531 pl022->cur_chip = NULL;
1532 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1533
1534 queue_work(pl022->workqueue, &pl022->pump_messages);
1535
1536 return 0;
1537}
1538
1539
1540static int stop_queue(struct pl022 *pl022)
1541{
1542 unsigned long flags;
1543 unsigned limit = 500;
1544 int status = 0;
1545
1546 spin_lock_irqsave(&pl022->queue_lock, flags);
1547
1548 /* This is a bit lame, but is optimized for the common execution path.
1549 * A wait_queue on the pl022->busy could be used, but then the common
1550 * execution path (pump_messages) would be required to call wake_up or
1551 * friends on every SPI message. Do this instead */
Linus Walleijb43d65f2009-06-09 08:11:42 +01001552 while (!list_empty(&pl022->queue) && pl022->busy && limit--) {
1553 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1554 msleep(10);
1555 spin_lock_irqsave(&pl022->queue_lock, flags);
1556 }
1557
1558 if (!list_empty(&pl022->queue) || pl022->busy)
1559 status = -EBUSY;
Linus Walleij5e8b8212010-12-22 23:13:59 +01001560 else
1561 pl022->running = false;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001562
1563 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1564
1565 return status;
1566}
1567
1568static int destroy_queue(struct pl022 *pl022)
1569{
1570 int status;
1571
1572 status = stop_queue(pl022);
1573 /* we are unloading the module or failing to load (only two calls
1574 * to this routine), and neither call can handle a return value.
1575 * However, destroy_workqueue calls flush_workqueue, and that will
1576 * block until all work is done. If the reason that stop_queue
1577 * timed out is that the work will never finish, then it does no
1578 * good to call destroy_workqueue, so return anyway. */
1579 if (status != 0)
1580 return status;
1581
1582 destroy_workqueue(pl022->workqueue);
1583
1584 return 0;
1585}
1586
1587static int verify_controller_parameters(struct pl022 *pl022,
Linus Walleijf9d629c2010-10-01 13:33:13 +02001588 struct pl022_config_chip const *chip_info)
Linus Walleijb43d65f2009-06-09 08:11:42 +01001589{
Linus Walleijb43d65f2009-06-09 08:11:42 +01001590 if ((chip_info->iface < SSP_INTERFACE_MOTOROLA_SPI)
1591 || (chip_info->iface > SSP_INTERFACE_UNIDIRECTIONAL)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001592 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001593 "interface is configured incorrectly\n");
1594 return -EINVAL;
1595 }
1596 if ((chip_info->iface == SSP_INTERFACE_UNIDIRECTIONAL) &&
1597 (!pl022->vendor->unidir)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001598 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001599 "unidirectional mode not supported in this "
1600 "hardware version\n");
1601 return -EINVAL;
1602 }
1603 if ((chip_info->hierarchy != SSP_MASTER)
1604 && (chip_info->hierarchy != SSP_SLAVE)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001605 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001606 "hierarchy is configured incorrectly\n");
1607 return -EINVAL;
1608 }
Linus Walleijb43d65f2009-06-09 08:11:42 +01001609 if ((chip_info->com_mode != INTERRUPT_TRANSFER)
1610 && (chip_info->com_mode != DMA_TRANSFER)
1611 && (chip_info->com_mode != POLLING_TRANSFER)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001612 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001613 "Communication mode is configured incorrectly\n");
1614 return -EINVAL;
1615 }
1616 if ((chip_info->rx_lev_trig < SSP_RX_1_OR_MORE_ELEM)
1617 || (chip_info->rx_lev_trig > SSP_RX_32_OR_MORE_ELEM)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001618 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001619 "RX FIFO Trigger Level is configured incorrectly\n");
1620 return -EINVAL;
1621 }
1622 if ((chip_info->tx_lev_trig < SSP_TX_1_OR_MORE_EMPTY_LOC)
1623 || (chip_info->tx_lev_trig > SSP_TX_32_OR_MORE_EMPTY_LOC)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001624 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001625 "TX FIFO Trigger Level is configured incorrectly\n");
1626 return -EINVAL;
1627 }
Linus Walleijb43d65f2009-06-09 08:11:42 +01001628 if (chip_info->iface == SSP_INTERFACE_NATIONAL_MICROWIRE) {
1629 if ((chip_info->ctrl_len < SSP_BITS_4)
1630 || (chip_info->ctrl_len > SSP_BITS_32)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001631 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001632 "CTRL LEN is configured incorrectly\n");
1633 return -EINVAL;
1634 }
1635 if ((chip_info->wait_state != SSP_MWIRE_WAIT_ZERO)
1636 && (chip_info->wait_state != SSP_MWIRE_WAIT_ONE)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001637 dev_err(&pl022->adev->dev,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001638 "Wait State is configured incorrectly\n");
1639 return -EINVAL;
1640 }
Linus Walleij556f4ae2010-05-05 09:28:15 +00001641 /* Half duplex is only available in the ST Micro version */
1642 if (pl022->vendor->extended_cr) {
1643 if ((chip_info->duplex !=
1644 SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
1645 && (chip_info->duplex !=
Julia Lawall4a4fd472010-09-29 17:31:30 +09001646 SSP_MICROWIRE_CHANNEL_HALF_DUPLEX)) {
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001647 dev_err(&pl022->adev->dev,
Linus Walleij556f4ae2010-05-05 09:28:15 +00001648 "Microwire duplex mode is configured incorrectly\n");
1649 return -EINVAL;
Julia Lawall4a4fd472010-09-29 17:31:30 +09001650 }
Linus Walleij556f4ae2010-05-05 09:28:15 +00001651 } else {
1652 if (chip_info->duplex != SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
Linus Walleij5a1c98b2010-10-01 11:47:32 +02001653 dev_err(&pl022->adev->dev,
Linus Walleij556f4ae2010-05-05 09:28:15 +00001654 "Microwire half duplex mode requested,"
1655 " but this is only available in the"
1656 " ST version of PL022\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01001657 return -EINVAL;
1658 }
1659 }
Linus Walleijb43d65f2009-06-09 08:11:42 +01001660 return 0;
1661}
1662
1663/**
1664 * pl022_transfer - transfer function registered to SPI master framework
1665 * @spi: spi device which is requesting transfer
1666 * @msg: spi message which is to handled is queued to driver queue
1667 *
1668 * This function is registered to the SPI framework for this SPI master
1669 * controller. It will queue the spi_message in the queue of driver if
1670 * the queue is not stopped and return.
1671 */
1672static int pl022_transfer(struct spi_device *spi, struct spi_message *msg)
1673{
1674 struct pl022 *pl022 = spi_master_get_devdata(spi->master);
1675 unsigned long flags;
1676
1677 spin_lock_irqsave(&pl022->queue_lock, flags);
1678
Linus Walleij5e8b8212010-12-22 23:13:59 +01001679 if (!pl022->running) {
Linus Walleijb43d65f2009-06-09 08:11:42 +01001680 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1681 return -ESHUTDOWN;
1682 }
1683 msg->actual_length = 0;
1684 msg->status = -EINPROGRESS;
1685 msg->state = STATE_START;
1686
1687 list_add_tail(&msg->queue, &pl022->queue);
Linus Walleij5e8b8212010-12-22 23:13:59 +01001688 if (pl022->running && !pl022->busy)
Linus Walleijb43d65f2009-06-09 08:11:42 +01001689 queue_work(pl022->workqueue, &pl022->pump_messages);
1690
1691 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1692 return 0;
1693}
1694
1695static int calculate_effective_freq(struct pl022 *pl022,
1696 int freq,
1697 struct ssp_clock_params *clk_freq)
1698{
1699 /* Lets calculate the frequency parameters */
1700 u16 cpsdvsr = 2;
1701 u16 scr = 0;
1702 bool freq_found = false;
1703 u32 rate;
1704 u32 max_tclk;
1705 u32 min_tclk;
1706
1707 rate = clk_get_rate(pl022->clk);
1708 /* cpsdvscr = 2 & scr 0 */
1709 max_tclk = (rate / (CPSDVR_MIN * (1 + SCR_MIN)));
1710 /* cpsdvsr = 254 & scr = 255 */
1711 min_tclk = (rate / (CPSDVR_MAX * (1 + SCR_MAX)));
1712
1713 if ((freq <= max_tclk) && (freq >= min_tclk)) {
1714 while (cpsdvsr <= CPSDVR_MAX && !freq_found) {
1715 while (scr <= SCR_MAX && !freq_found) {
1716 if ((rate /
1717 (cpsdvsr * (1 + scr))) > freq)
1718 scr += 1;
1719 else {
1720 /*
1721 * This bool is made true when
1722 * effective frequency >=
1723 * target frequency is found
1724 */
1725 freq_found = true;
1726 if ((rate /
1727 (cpsdvsr * (1 + scr))) != freq) {
1728 if (scr == SCR_MIN) {
1729 cpsdvsr -= 2;
1730 scr = SCR_MAX;
1731 } else
1732 scr -= 1;
1733 }
1734 }
1735 }
1736 if (!freq_found) {
1737 cpsdvsr += 2;
1738 scr = SCR_MIN;
1739 }
1740 }
1741 if (cpsdvsr != 0) {
1742 dev_dbg(&pl022->adev->dev,
1743 "SSP Effective Frequency is %u\n",
1744 (rate / (cpsdvsr * (1 + scr))));
1745 clk_freq->cpsdvsr = (u8) (cpsdvsr & 0xFF);
1746 clk_freq->scr = (u8) (scr & 0xFF);
1747 dev_dbg(&pl022->adev->dev,
1748 "SSP cpsdvsr = %d, scr = %d\n",
1749 clk_freq->cpsdvsr, clk_freq->scr);
1750 }
1751 } else {
1752 dev_err(&pl022->adev->dev,
1753 "controller data is incorrect: out of range frequency");
1754 return -EINVAL;
1755 }
1756 return 0;
1757}
1758
Linus Walleijf9d629c2010-10-01 13:33:13 +02001759
1760/*
1761 * A piece of default chip info unless the platform
1762 * supplies it.
1763 */
1764static const struct pl022_config_chip pl022_default_chip_info = {
1765 .com_mode = POLLING_TRANSFER,
1766 .iface = SSP_INTERFACE_MOTOROLA_SPI,
1767 .hierarchy = SSP_SLAVE,
1768 .slave_tx_disable = DO_NOT_DRIVE_TX,
1769 .rx_lev_trig = SSP_RX_1_OR_MORE_ELEM,
1770 .tx_lev_trig = SSP_TX_1_OR_MORE_EMPTY_LOC,
1771 .ctrl_len = SSP_BITS_8,
1772 .wait_state = SSP_MWIRE_WAIT_ZERO,
1773 .duplex = SSP_MICROWIRE_CHANNEL_FULL_DUPLEX,
1774 .cs_control = null_cs_control,
1775};
1776
1777
Linus Walleijb43d65f2009-06-09 08:11:42 +01001778/**
Linus Walleijb43d65f2009-06-09 08:11:42 +01001779 * pl022_setup - setup function registered to SPI master framework
1780 * @spi: spi device which is requesting setup
1781 *
1782 * This function is registered to the SPI framework for this SPI master
1783 * controller. If it is the first time when setup is called by this device,
1784 * this function will initialize the runtime state for this chip and save
1785 * the same in the device structure. Else it will update the runtime info
1786 * with the updated chip info. Nothing is really being written to the
1787 * controller hardware here, that is not done until the actual transfer
1788 * commence.
1789 */
Linus Walleijb43d65f2009-06-09 08:11:42 +01001790static int pl022_setup(struct spi_device *spi)
1791{
Linus Walleijf9d629c2010-10-01 13:33:13 +02001792 struct pl022_config_chip const *chip_info;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001793 struct chip_data *chip;
Viresh Kumar94a1b6d2011-01-13 17:24:22 +05301794 struct ssp_clock_params clk_freq = {0, };
Linus Walleijb43d65f2009-06-09 08:11:42 +01001795 int status = 0;
1796 struct pl022 *pl022 = spi_master_get_devdata(spi->master);
Kevin Wellsbde435a2010-09-16 06:18:50 -07001797 unsigned int bits = spi->bits_per_word;
1798 u32 tmp;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001799
1800 if (!spi->max_speed_hz)
1801 return -EINVAL;
1802
1803 /* Get controller_state if one is supplied */
1804 chip = spi_get_ctldata(spi);
1805
1806 if (chip == NULL) {
1807 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1808 if (!chip) {
1809 dev_err(&spi->dev,
1810 "cannot allocate controller state\n");
1811 return -ENOMEM;
1812 }
1813 dev_dbg(&spi->dev,
1814 "allocated memory for controller's runtime state\n");
1815 }
1816
1817 /* Get controller data if one is supplied */
1818 chip_info = spi->controller_data;
1819
1820 if (chip_info == NULL) {
Linus Walleijf9d629c2010-10-01 13:33:13 +02001821 chip_info = &pl022_default_chip_info;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001822 /* spi_board_info.controller_data not is supplied */
1823 dev_dbg(&spi->dev,
1824 "using default controller_data settings\n");
Linus Walleijf9d629c2010-10-01 13:33:13 +02001825 } else
Linus Walleijb43d65f2009-06-09 08:11:42 +01001826 dev_dbg(&spi->dev,
1827 "using user supplied controller_data settings\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01001828
1829 /*
1830 * We can override with custom divisors, else we use the board
1831 * frequency setting
1832 */
1833 if ((0 == chip_info->clk_freq.cpsdvsr)
1834 && (0 == chip_info->clk_freq.scr)) {
1835 status = calculate_effective_freq(pl022,
1836 spi->max_speed_hz,
Linus Walleijf9d629c2010-10-01 13:33:13 +02001837 &clk_freq);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001838 if (status < 0)
1839 goto err_config_params;
1840 } else {
Linus Walleijf9d629c2010-10-01 13:33:13 +02001841 memcpy(&clk_freq, &chip_info->clk_freq, sizeof(clk_freq));
1842 if ((clk_freq.cpsdvsr % 2) != 0)
1843 clk_freq.cpsdvsr =
1844 clk_freq.cpsdvsr - 1;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001845 }
Linus Walleijf9d629c2010-10-01 13:33:13 +02001846 if ((clk_freq.cpsdvsr < CPSDVR_MIN)
1847 || (clk_freq.cpsdvsr > CPSDVR_MAX)) {
1848 dev_err(&spi->dev,
1849 "cpsdvsr is configured incorrectly\n");
1850 goto err_config_params;
1851 }
1852
1853
Linus Walleijb43d65f2009-06-09 08:11:42 +01001854 status = verify_controller_parameters(pl022, chip_info);
1855 if (status) {
1856 dev_err(&spi->dev, "controller data is incorrect");
1857 goto err_config_params;
1858 }
Linus Walleijf9d629c2010-10-01 13:33:13 +02001859
Linus Walleijb43d65f2009-06-09 08:11:42 +01001860 /* Now set controller state based on controller data */
1861 chip->xfer_type = chip_info->com_mode;
Linus Walleijf9d629c2010-10-01 13:33:13 +02001862 if (!chip_info->cs_control) {
1863 chip->cs_control = null_cs_control;
1864 dev_warn(&spi->dev,
1865 "chip select function is NULL for this chip\n");
1866 } else
1867 chip->cs_control = chip_info->cs_control;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001868
Kevin Wellsbde435a2010-09-16 06:18:50 -07001869 if (bits <= 3) {
1870 /* PL022 doesn't support less than 4-bits */
1871 status = -ENOTSUPP;
1872 goto err_config_params;
1873 } else if (bits <= 8) {
1874 dev_dbg(&spi->dev, "4 <= n <=8 bits per word\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01001875 chip->n_bytes = 1;
1876 chip->read = READING_U8;
1877 chip->write = WRITING_U8;
Kevin Wellsbde435a2010-09-16 06:18:50 -07001878 } else if (bits <= 16) {
Linus Walleijb43d65f2009-06-09 08:11:42 +01001879 dev_dbg(&spi->dev, "9 <= n <= 16 bits per word\n");
1880 chip->n_bytes = 2;
1881 chip->read = READING_U16;
1882 chip->write = WRITING_U16;
1883 } else {
1884 if (pl022->vendor->max_bpw >= 32) {
1885 dev_dbg(&spi->dev, "17 <= n <= 32 bits per word\n");
1886 chip->n_bytes = 4;
1887 chip->read = READING_U32;
1888 chip->write = WRITING_U32;
1889 } else {
1890 dev_err(&spi->dev,
1891 "illegal data size for this controller!\n");
1892 dev_err(&spi->dev,
1893 "a standard pl022 can only handle "
1894 "1 <= n <= 16 bit words\n");
Kevin Wellsbde435a2010-09-16 06:18:50 -07001895 status = -ENOTSUPP;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001896 goto err_config_params;
1897 }
1898 }
1899
1900 /* Now Initialize all register settings required for this chip */
1901 chip->cr0 = 0;
1902 chip->cr1 = 0;
1903 chip->dmacr = 0;
1904 chip->cpsr = 0;
1905 if ((chip_info->com_mode == DMA_TRANSFER)
1906 && ((pl022->master_info)->enable_dma)) {
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001907 chip->enable_dma = true;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001908 dev_dbg(&spi->dev, "DMA mode set in controller state\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01001909 if (status < 0)
1910 goto err_config_params;
1911 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
1912 SSP_DMACR_MASK_RXDMAE, 0);
1913 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
1914 SSP_DMACR_MASK_TXDMAE, 1);
1915 } else {
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09001916 chip->enable_dma = false;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001917 dev_dbg(&spi->dev, "DMA mode NOT set in controller state\n");
1918 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
1919 SSP_DMACR_MASK_RXDMAE, 0);
1920 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
1921 SSP_DMACR_MASK_TXDMAE, 1);
1922 }
1923
Linus Walleijf9d629c2010-10-01 13:33:13 +02001924 chip->cpsr = clk_freq.cpsdvsr;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001925
Linus Walleij556f4ae2010-05-05 09:28:15 +00001926 /* Special setup for the ST micro extended control registers */
1927 if (pl022->vendor->extended_cr) {
Kevin Wellsbde435a2010-09-16 06:18:50 -07001928 u32 etx;
1929
Linus Walleij781c7b12010-05-07 08:40:53 +00001930 if (pl022->vendor->pl023) {
1931 /* These bits are only in the PL023 */
1932 SSP_WRITE_BITS(chip->cr1, chip_info->clkdelay,
1933 SSP_CR1_MASK_FBCLKDEL_ST, 13);
1934 } else {
1935 /* These bits are in the PL022 but not PL023 */
1936 SSP_WRITE_BITS(chip->cr0, chip_info->duplex,
1937 SSP_CR0_MASK_HALFDUP_ST, 5);
1938 SSP_WRITE_BITS(chip->cr0, chip_info->ctrl_len,
1939 SSP_CR0_MASK_CSS_ST, 16);
1940 SSP_WRITE_BITS(chip->cr0, chip_info->iface,
1941 SSP_CR0_MASK_FRF_ST, 21);
1942 SSP_WRITE_BITS(chip->cr1, chip_info->wait_state,
1943 SSP_CR1_MASK_MWAIT_ST, 6);
1944 }
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_ST, 0);
Kevin Wellsbde435a2010-09-16 06:18:50 -07001947
1948 if (spi->mode & SPI_LSB_FIRST) {
1949 tmp = SSP_RX_LSB;
1950 etx = SSP_TX_LSB;
1951 } else {
1952 tmp = SSP_RX_MSB;
1953 etx = SSP_TX_MSB;
1954 }
1955 SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_RENDN_ST, 4);
1956 SSP_WRITE_BITS(chip->cr1, etx, SSP_CR1_MASK_TENDN_ST, 5);
Linus Walleij556f4ae2010-05-05 09:28:15 +00001957 SSP_WRITE_BITS(chip->cr1, chip_info->rx_lev_trig,
1958 SSP_CR1_MASK_RXIFLSEL_ST, 7);
1959 SSP_WRITE_BITS(chip->cr1, chip_info->tx_lev_trig,
1960 SSP_CR1_MASK_TXIFLSEL_ST, 10);
1961 } else {
Kevin Wellsbde435a2010-09-16 06:18:50 -07001962 SSP_WRITE_BITS(chip->cr0, bits - 1,
Linus Walleij556f4ae2010-05-05 09:28:15 +00001963 SSP_CR0_MASK_DSS, 0);
1964 SSP_WRITE_BITS(chip->cr0, chip_info->iface,
1965 SSP_CR0_MASK_FRF, 4);
1966 }
Kevin Wellsbde435a2010-09-16 06:18:50 -07001967
Linus Walleij556f4ae2010-05-05 09:28:15 +00001968 /* Stuff that is common for all versions */
Kevin Wellsbde435a2010-09-16 06:18:50 -07001969 if (spi->mode & SPI_CPOL)
1970 tmp = SSP_CLK_POL_IDLE_HIGH;
1971 else
1972 tmp = SSP_CLK_POL_IDLE_LOW;
1973 SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPO, 6);
1974
1975 if (spi->mode & SPI_CPHA)
1976 tmp = SSP_CLK_SECOND_EDGE;
1977 else
1978 tmp = SSP_CLK_FIRST_EDGE;
1979 SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPH, 7);
1980
Linus Walleijf9d629c2010-10-01 13:33:13 +02001981 SSP_WRITE_BITS(chip->cr0, clk_freq.scr, SSP_CR0_MASK_SCR, 8);
Linus Walleij781c7b12010-05-07 08:40:53 +00001982 /* Loopback is available on all versions except PL023 */
Kevin Wellsbde435a2010-09-16 06:18:50 -07001983 if (!pl022->vendor->pl023) {
1984 if (spi->mode & SPI_LOOP)
1985 tmp = LOOPBACK_ENABLED;
1986 else
1987 tmp = LOOPBACK_DISABLED;
1988 SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_LBM, 0);
1989 }
Linus Walleijb43d65f2009-06-09 08:11:42 +01001990 SSP_WRITE_BITS(chip->cr1, SSP_DISABLED, SSP_CR1_MASK_SSE, 1);
1991 SSP_WRITE_BITS(chip->cr1, chip_info->hierarchy, SSP_CR1_MASK_MS, 2);
1992 SSP_WRITE_BITS(chip->cr1, chip_info->slave_tx_disable, SSP_CR1_MASK_SOD, 3);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001993
1994 /* Save controller_state */
1995 spi_set_ctldata(spi, chip);
1996 return status;
1997 err_config_params:
Kevin Wellsbde435a2010-09-16 06:18:50 -07001998 spi_set_ctldata(spi, NULL);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001999 kfree(chip);
2000 return status;
2001}
2002
2003/**
2004 * pl022_cleanup - cleanup function registered to SPI master framework
2005 * @spi: spi device which is requesting cleanup
2006 *
2007 * This function is registered to the SPI framework for this SPI master
2008 * controller. It will free the runtime state of chip.
2009 */
2010static void pl022_cleanup(struct spi_device *spi)
2011{
2012 struct chip_data *chip = spi_get_ctldata(spi);
2013
2014 spi_set_ctldata(spi, NULL);
2015 kfree(chip);
2016}
2017
2018
Kevin Wellsb4225882010-07-27 16:39:30 +00002019static int __devinit
Linus Walleijb43d65f2009-06-09 08:11:42 +01002020pl022_probe(struct amba_device *adev, struct amba_id *id)
2021{
2022 struct device *dev = &adev->dev;
2023 struct pl022_ssp_controller *platform_info = adev->dev.platform_data;
2024 struct spi_master *master;
2025 struct pl022 *pl022 = NULL; /*Data for this driver */
2026 int status = 0;
2027
2028 dev_info(&adev->dev,
2029 "ARM PL022 driver, device ID: 0x%08x\n", adev->periphid);
2030 if (platform_info == NULL) {
2031 dev_err(&adev->dev, "probe - no platform data supplied\n");
2032 status = -ENODEV;
2033 goto err_no_pdata;
2034 }
2035
2036 /* Allocate master with space for data */
2037 master = spi_alloc_master(dev, sizeof(struct pl022));
2038 if (master == NULL) {
2039 dev_err(&adev->dev, "probe - cannot alloc SPI master\n");
2040 status = -ENOMEM;
2041 goto err_no_master;
2042 }
2043
2044 pl022 = spi_master_get_devdata(master);
2045 pl022->master = master;
2046 pl022->master_info = platform_info;
2047 pl022->adev = adev;
2048 pl022->vendor = id->data;
2049
2050 /*
2051 * Bus Number Which has been Assigned to this SSP controller
2052 * on this board
2053 */
2054 master->bus_num = platform_info->bus_id;
2055 master->num_chipselect = platform_info->num_chipselect;
2056 master->cleanup = pl022_cleanup;
2057 master->setup = pl022_setup;
2058 master->transfer = pl022_transfer;
2059
Kevin Wellsbde435a2010-09-16 06:18:50 -07002060 /*
2061 * Supports mode 0-3, loopback, and active low CS. Transfers are
2062 * always MS bit first on the original pl022.
2063 */
2064 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
2065 if (pl022->vendor->extended_cr)
2066 master->mode_bits |= SPI_LSB_FIRST;
2067
Linus Walleijb43d65f2009-06-09 08:11:42 +01002068 dev_dbg(&adev->dev, "BUSNO: %d\n", master->bus_num);
2069
2070 status = amba_request_regions(adev, NULL);
2071 if (status)
2072 goto err_no_ioregion;
2073
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09002074 pl022->phybase = adev->res.start;
Linus Walleijb43d65f2009-06-09 08:11:42 +01002075 pl022->virtbase = ioremap(adev->res.start, resource_size(&adev->res));
2076 if (pl022->virtbase == NULL) {
2077 status = -ENOMEM;
2078 goto err_no_ioremap;
2079 }
2080 printk(KERN_INFO "pl022: mapped registers from 0x%08x to %p\n",
2081 adev->res.start, pl022->virtbase);
2082
2083 pl022->clk = clk_get(&adev->dev, NULL);
2084 if (IS_ERR(pl022->clk)) {
2085 status = PTR_ERR(pl022->clk);
2086 dev_err(&adev->dev, "could not retrieve SSP/SPI bus clock\n");
2087 goto err_no_clk;
2088 }
2089
2090 /* Disable SSP */
Linus Walleijb43d65f2009-06-09 08:11:42 +01002091 writew((readw(SSP_CR1(pl022->virtbase)) & (~SSP_CR1_MASK_SSE)),
2092 SSP_CR1(pl022->virtbase));
2093 load_ssp_default_config(pl022);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002094
2095 status = request_irq(adev->irq[0], pl022_interrupt_handler, 0, "pl022",
2096 pl022);
2097 if (status < 0) {
2098 dev_err(&adev->dev, "probe - cannot get IRQ (%d)\n", status);
2099 goto err_no_irq;
2100 }
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09002101
2102 /* Get DMA channels */
2103 if (platform_info->enable_dma) {
2104 status = pl022_dma_probe(pl022);
2105 if (status != 0)
2106 goto err_no_dma;
2107 }
2108
Linus Walleijb43d65f2009-06-09 08:11:42 +01002109 /* Initialize and start queue */
2110 status = init_queue(pl022);
2111 if (status != 0) {
2112 dev_err(&adev->dev, "probe - problem initializing queue\n");
2113 goto err_init_queue;
2114 }
2115 status = start_queue(pl022);
2116 if (status != 0) {
2117 dev_err(&adev->dev, "probe - problem starting queue\n");
2118 goto err_start_queue;
2119 }
2120 /* Register with the SPI framework */
2121 amba_set_drvdata(adev, pl022);
2122 status = spi_register_master(master);
2123 if (status != 0) {
2124 dev_err(&adev->dev,
2125 "probe - problem registering spi master\n");
2126 goto err_spi_register;
2127 }
2128 dev_dbg(dev, "probe succeded\n");
Linus Walleij808f1032011-02-08 13:03:32 +01002129 /*
2130 * Disable the silicon block pclk and any voltage domain and just
2131 * power it up and clock it when it's needed
2132 */
Linus Walleij545074f2010-08-21 11:07:36 +02002133 amba_pclk_disable(adev);
Linus Walleij808f1032011-02-08 13:03:32 +01002134 amba_vcore_disable(adev);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002135 return 0;
2136
2137 err_spi_register:
2138 err_start_queue:
2139 err_init_queue:
2140 destroy_queue(pl022);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09002141 pl022_dma_remove(pl022);
2142 err_no_dma:
Linus Walleijb43d65f2009-06-09 08:11:42 +01002143 free_irq(adev->irq[0], pl022);
2144 err_no_irq:
2145 clk_put(pl022->clk);
2146 err_no_clk:
2147 iounmap(pl022->virtbase);
2148 err_no_ioremap:
2149 amba_release_regions(adev);
2150 err_no_ioregion:
2151 spi_master_put(master);
2152 err_no_master:
2153 err_no_pdata:
2154 return status;
2155}
2156
Kevin Wellsb4225882010-07-27 16:39:30 +00002157static int __devexit
Linus Walleijb43d65f2009-06-09 08:11:42 +01002158pl022_remove(struct amba_device *adev)
2159{
2160 struct pl022 *pl022 = amba_get_drvdata(adev);
2161 int status = 0;
2162 if (!pl022)
2163 return 0;
2164
2165 /* Remove the queue */
2166 status = destroy_queue(pl022);
2167 if (status != 0) {
2168 dev_err(&adev->dev,
2169 "queue remove failed (%d)\n", status);
2170 return status;
2171 }
2172 load_ssp_default_config(pl022);
Linus Walleijb1b6b9a2010-09-29 17:31:35 +09002173 pl022_dma_remove(pl022);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002174 free_irq(adev->irq[0], pl022);
2175 clk_disable(pl022->clk);
2176 clk_put(pl022->clk);
2177 iounmap(pl022->virtbase);
2178 amba_release_regions(adev);
2179 tasklet_disable(&pl022->pump_transfers);
2180 spi_unregister_master(pl022->master);
2181 spi_master_put(pl022->master);
2182 amba_set_drvdata(adev, NULL);
2183 dev_dbg(&adev->dev, "remove succeded\n");
2184 return 0;
2185}
2186
2187#ifdef CONFIG_PM
2188static int pl022_suspend(struct amba_device *adev, pm_message_t state)
2189{
2190 struct pl022 *pl022 = amba_get_drvdata(adev);
2191 int status = 0;
2192
2193 status = stop_queue(pl022);
2194 if (status) {
2195 dev_warn(&adev->dev, "suspend cannot stop queue\n");
2196 return status;
2197 }
2198
Linus Walleij808f1032011-02-08 13:03:32 +01002199 amba_vcore_enable(adev);
Linus Walleij545074f2010-08-21 11:07:36 +02002200 amba_pclk_enable(adev);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002201 load_ssp_default_config(pl022);
Linus Walleij545074f2010-08-21 11:07:36 +02002202 amba_pclk_disable(adev);
Linus Walleij808f1032011-02-08 13:03:32 +01002203 amba_vcore_disable(adev);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002204 dev_dbg(&adev->dev, "suspended\n");
2205 return 0;
2206}
2207
2208static int pl022_resume(struct amba_device *adev)
2209{
2210 struct pl022 *pl022 = amba_get_drvdata(adev);
2211 int status = 0;
2212
2213 /* Start the queue running */
2214 status = start_queue(pl022);
2215 if (status)
2216 dev_err(&adev->dev, "problem starting queue (%d)\n", status);
2217 else
2218 dev_dbg(&adev->dev, "resumed\n");
2219
2220 return status;
2221}
2222#else
2223#define pl022_suspend NULL
2224#define pl022_resume NULL
2225#endif /* CONFIG_PM */
2226
2227static struct vendor_data vendor_arm = {
2228 .fifodepth = 8,
2229 .max_bpw = 16,
2230 .unidir = false,
Linus Walleij556f4ae2010-05-05 09:28:15 +00002231 .extended_cr = false,
Linus Walleij781c7b12010-05-07 08:40:53 +00002232 .pl023 = false,
Linus Walleijb43d65f2009-06-09 08:11:42 +01002233};
2234
2235
2236static struct vendor_data vendor_st = {
2237 .fifodepth = 32,
2238 .max_bpw = 32,
2239 .unidir = false,
Linus Walleij556f4ae2010-05-05 09:28:15 +00002240 .extended_cr = true,
Linus Walleij781c7b12010-05-07 08:40:53 +00002241 .pl023 = false,
2242};
2243
2244static struct vendor_data vendor_st_pl023 = {
2245 .fifodepth = 32,
2246 .max_bpw = 32,
2247 .unidir = false,
2248 .extended_cr = true,
2249 .pl023 = true,
Linus Walleijb43d65f2009-06-09 08:11:42 +01002250};
2251
2252static struct amba_id pl022_ids[] = {
2253 {
2254 /*
2255 * ARM PL022 variant, this has a 16bit wide
2256 * and 8 locations deep TX/RX FIFO
2257 */
2258 .id = 0x00041022,
2259 .mask = 0x000fffff,
2260 .data = &vendor_arm,
2261 },
2262 {
2263 /*
2264 * ST Micro derivative, this has 32bit wide
2265 * and 32 locations deep TX/RX FIFO
2266 */
Srinidhi Kasagare89e04f2009-10-05 06:13:53 +01002267 .id = 0x01080022,
Linus Walleijb43d65f2009-06-09 08:11:42 +01002268 .mask = 0xffffffff,
2269 .data = &vendor_st,
2270 },
Linus Walleij781c7b12010-05-07 08:40:53 +00002271 {
2272 /*
2273 * ST-Ericsson derivative "PL023" (this is not
2274 * an official ARM number), this is a PL022 SSP block
2275 * stripped to SPI mode only, it has 32bit wide
2276 * and 32 locations deep TX/RX FIFO but no extended
2277 * CR0/CR1 register
2278 */
2279 .id = 0x00080023,
2280 .mask = 0xffffffff,
2281 .data = &vendor_st_pl023,
2282 },
Linus Walleijb43d65f2009-06-09 08:11:42 +01002283 { 0, 0 },
2284};
2285
2286static struct amba_driver pl022_driver = {
2287 .drv = {
2288 .name = "ssp-pl022",
2289 },
2290 .id_table = pl022_ids,
2291 .probe = pl022_probe,
Kevin Wellsb4225882010-07-27 16:39:30 +00002292 .remove = __devexit_p(pl022_remove),
Linus Walleijb43d65f2009-06-09 08:11:42 +01002293 .suspend = pl022_suspend,
2294 .resume = pl022_resume,
2295};
2296
2297
2298static int __init pl022_init(void)
2299{
2300 return amba_driver_register(&pl022_driver);
2301}
2302
Linus Walleij25c8e032010-09-06 11:02:12 +02002303subsys_initcall(pl022_init);
Linus Walleijb43d65f2009-06-09 08:11:42 +01002304
2305static void __exit pl022_exit(void)
2306{
2307 amba_driver_unregister(&pl022_driver);
2308}
2309
2310module_exit(pl022_exit);
2311
2312MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
2313MODULE_DESCRIPTION("PL022 SSP Controller Driver");
2314MODULE_LICENSE("GPL");