blob: 8cdddc97325c7f26d9d737de48df4ff643ac1080 [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
30 * - add generic DMA framework support
31 */
32
33#include <linux/init.h>
34#include <linux/module.h>
35#include <linux/device.h>
36#include <linux/ioport.h>
37#include <linux/errno.h>
38#include <linux/interrupt.h>
39#include <linux/spi/spi.h>
40#include <linux/workqueue.h>
Linus Walleijb43d65f2009-06-09 08:11:42 +010041#include <linux/delay.h>
42#include <linux/clk.h>
43#include <linux/err.h>
44#include <linux/amba/bus.h>
45#include <linux/amba/pl022.h>
46#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Linus Walleijb43d65f2009-06-09 08:11:42 +010048
49/*
50 * This macro is used to define some register default values.
51 * reg is masked with mask, the OR:ed with an (again masked)
52 * val shifted sb steps to the left.
53 */
54#define SSP_WRITE_BITS(reg, val, mask, sb) \
55 ((reg) = (((reg) & ~(mask)) | (((val)<<(sb)) & (mask))))
56
57/*
58 * This macro is also used to define some default values.
59 * It will just shift val by sb steps to the left and mask
60 * the result with mask.
61 */
62#define GEN_MASK_BITS(val, mask, sb) \
63 (((val)<<(sb)) & (mask))
64
65#define DRIVE_TX 0
66#define DO_NOT_DRIVE_TX 1
67
68#define DO_NOT_QUEUE_DMA 0
69#define QUEUE_DMA 1
70
71#define RX_TRANSFER 1
72#define TX_TRANSFER 2
73
74/*
75 * Macros to access SSP Registers with their offsets
76 */
77#define SSP_CR0(r) (r + 0x000)
78#define SSP_CR1(r) (r + 0x004)
79#define SSP_DR(r) (r + 0x008)
80#define SSP_SR(r) (r + 0x00C)
81#define SSP_CPSR(r) (r + 0x010)
82#define SSP_IMSC(r) (r + 0x014)
83#define SSP_RIS(r) (r + 0x018)
84#define SSP_MIS(r) (r + 0x01C)
85#define SSP_ICR(r) (r + 0x020)
86#define SSP_DMACR(r) (r + 0x024)
87#define SSP_ITCR(r) (r + 0x080)
88#define SSP_ITIP(r) (r + 0x084)
89#define SSP_ITOP(r) (r + 0x088)
90#define SSP_TDR(r) (r + 0x08C)
91
92#define SSP_PID0(r) (r + 0xFE0)
93#define SSP_PID1(r) (r + 0xFE4)
94#define SSP_PID2(r) (r + 0xFE8)
95#define SSP_PID3(r) (r + 0xFEC)
96
97#define SSP_CID0(r) (r + 0xFF0)
98#define SSP_CID1(r) (r + 0xFF4)
99#define SSP_CID2(r) (r + 0xFF8)
100#define SSP_CID3(r) (r + 0xFFC)
101
102/*
103 * SSP Control Register 0 - SSP_CR0
104 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000105#define SSP_CR0_MASK_DSS (0x0FUL << 0)
106#define SSP_CR0_MASK_FRF (0x3UL << 4)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100107#define SSP_CR0_MASK_SPO (0x1UL << 6)
108#define SSP_CR0_MASK_SPH (0x1UL << 7)
109#define SSP_CR0_MASK_SCR (0xFFUL << 8)
Linus Walleij556f4ae2010-05-05 09:28:15 +0000110
111/*
112 * The ST version of this block moves som bits
113 * in SSP_CR0 and extends it to 32 bits
114 */
115#define SSP_CR0_MASK_DSS_ST (0x1FUL << 0)
116#define SSP_CR0_MASK_HALFDUP_ST (0x1UL << 5)
117#define SSP_CR0_MASK_CSS_ST (0x1FUL << 16)
118#define SSP_CR0_MASK_FRF_ST (0x3UL << 21)
119
Linus Walleijb43d65f2009-06-09 08:11:42 +0100120
121/*
122 * SSP Control Register 0 - SSP_CR1
123 */
124#define SSP_CR1_MASK_LBM (0x1UL << 0)
125#define SSP_CR1_MASK_SSE (0x1UL << 1)
126#define SSP_CR1_MASK_MS (0x1UL << 2)
127#define SSP_CR1_MASK_SOD (0x1UL << 3)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100128
129/*
Linus Walleij556f4ae2010-05-05 09:28:15 +0000130 * The ST version of this block adds some bits
131 * in SSP_CR1
Linus Walleijb43d65f2009-06-09 08:11:42 +0100132 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000133#define SSP_CR1_MASK_RENDN_ST (0x1UL << 4)
134#define SSP_CR1_MASK_TENDN_ST (0x1UL << 5)
135#define SSP_CR1_MASK_MWAIT_ST (0x1UL << 6)
136#define SSP_CR1_MASK_RXIFLSEL_ST (0x7UL << 7)
137#define SSP_CR1_MASK_TXIFLSEL_ST (0x7UL << 10)
Linus Walleij781c7b12010-05-07 08:40:53 +0000138/* This one is only in the PL023 variant */
139#define SSP_CR1_MASK_FBCLKDEL_ST (0x7UL << 13)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100140
141/*
142 * SSP Status Register - SSP_SR
143 */
144#define SSP_SR_MASK_TFE (0x1UL << 0) /* Transmit FIFO empty */
145#define SSP_SR_MASK_TNF (0x1UL << 1) /* Transmit FIFO not full */
146#define SSP_SR_MASK_RNE (0x1UL << 2) /* Receive FIFO not empty */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000147#define SSP_SR_MASK_RFF (0x1UL << 3) /* Receive FIFO full */
Linus Walleijb43d65f2009-06-09 08:11:42 +0100148#define SSP_SR_MASK_BSY (0x1UL << 4) /* Busy Flag */
149
150/*
151 * SSP Clock Prescale Register - SSP_CPSR
152 */
153#define SSP_CPSR_MASK_CPSDVSR (0xFFUL << 0)
154
155/*
156 * SSP Interrupt Mask Set/Clear Register - SSP_IMSC
157 */
158#define SSP_IMSC_MASK_RORIM (0x1UL << 0) /* Receive Overrun Interrupt mask */
159#define SSP_IMSC_MASK_RTIM (0x1UL << 1) /* Receive timeout Interrupt mask */
160#define SSP_IMSC_MASK_RXIM (0x1UL << 2) /* Receive FIFO Interrupt mask */
161#define SSP_IMSC_MASK_TXIM (0x1UL << 3) /* Transmit FIFO Interrupt mask */
162
163/*
164 * SSP Raw Interrupt Status Register - SSP_RIS
165 */
166/* Receive Overrun Raw Interrupt status */
167#define SSP_RIS_MASK_RORRIS (0x1UL << 0)
168/* Receive Timeout Raw Interrupt status */
169#define SSP_RIS_MASK_RTRIS (0x1UL << 1)
170/* Receive FIFO Raw Interrupt status */
171#define SSP_RIS_MASK_RXRIS (0x1UL << 2)
172/* Transmit FIFO Raw Interrupt status */
173#define SSP_RIS_MASK_TXRIS (0x1UL << 3)
174
175/*
176 * SSP Masked Interrupt Status Register - SSP_MIS
177 */
178/* Receive Overrun Masked Interrupt status */
179#define SSP_MIS_MASK_RORMIS (0x1UL << 0)
180/* Receive Timeout Masked Interrupt status */
181#define SSP_MIS_MASK_RTMIS (0x1UL << 1)
182/* Receive FIFO Masked Interrupt status */
183#define SSP_MIS_MASK_RXMIS (0x1UL << 2)
184/* Transmit FIFO Masked Interrupt status */
185#define SSP_MIS_MASK_TXMIS (0x1UL << 3)
186
187/*
188 * SSP Interrupt Clear Register - SSP_ICR
189 */
190/* Receive Overrun Raw Clear Interrupt bit */
191#define SSP_ICR_MASK_RORIC (0x1UL << 0)
192/* Receive Timeout Clear Interrupt bit */
193#define SSP_ICR_MASK_RTIC (0x1UL << 1)
194
195/*
196 * SSP DMA Control Register - SSP_DMACR
197 */
198/* Receive DMA Enable bit */
199#define SSP_DMACR_MASK_RXDMAE (0x1UL << 0)
200/* Transmit DMA Enable bit */
201#define SSP_DMACR_MASK_TXDMAE (0x1UL << 1)
202
203/*
204 * SSP Integration Test control Register - SSP_ITCR
205 */
206#define SSP_ITCR_MASK_ITEN (0x1UL << 0)
207#define SSP_ITCR_MASK_TESTFIFO (0x1UL << 1)
208
209/*
210 * SSP Integration Test Input Register - SSP_ITIP
211 */
212#define ITIP_MASK_SSPRXD (0x1UL << 0)
213#define ITIP_MASK_SSPFSSIN (0x1UL << 1)
214#define ITIP_MASK_SSPCLKIN (0x1UL << 2)
215#define ITIP_MASK_RXDMAC (0x1UL << 3)
216#define ITIP_MASK_TXDMAC (0x1UL << 4)
217#define ITIP_MASK_SSPTXDIN (0x1UL << 5)
218
219/*
220 * SSP Integration Test output Register - SSP_ITOP
221 */
222#define ITOP_MASK_SSPTXD (0x1UL << 0)
223#define ITOP_MASK_SSPFSSOUT (0x1UL << 1)
224#define ITOP_MASK_SSPCLKOUT (0x1UL << 2)
225#define ITOP_MASK_SSPOEn (0x1UL << 3)
226#define ITOP_MASK_SSPCTLOEn (0x1UL << 4)
227#define ITOP_MASK_RORINTR (0x1UL << 5)
228#define ITOP_MASK_RTINTR (0x1UL << 6)
229#define ITOP_MASK_RXINTR (0x1UL << 7)
230#define ITOP_MASK_TXINTR (0x1UL << 8)
231#define ITOP_MASK_INTR (0x1UL << 9)
232#define ITOP_MASK_RXDMABREQ (0x1UL << 10)
233#define ITOP_MASK_RXDMASREQ (0x1UL << 11)
234#define ITOP_MASK_TXDMABREQ (0x1UL << 12)
235#define ITOP_MASK_TXDMASREQ (0x1UL << 13)
236
237/*
238 * SSP Test Data Register - SSP_TDR
239 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000240#define TDR_MASK_TESTDATA (0xFFFFFFFF)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100241
242/*
243 * Message State
244 * we use the spi_message.state (void *) pointer to
245 * hold a single state value, that's why all this
246 * (void *) casting is done here.
247 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000248#define STATE_START ((void *) 0)
249#define STATE_RUNNING ((void *) 1)
250#define STATE_DONE ((void *) 2)
251#define STATE_ERROR ((void *) -1)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100252
253/*
254 * Queue State
255 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000256#define QUEUE_RUNNING (0)
257#define QUEUE_STOPPED (1)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100258/*
259 * SSP State - Whether Enabled or Disabled
260 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000261#define SSP_DISABLED (0)
262#define SSP_ENABLED (1)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100263
264/*
265 * SSP DMA State - Whether DMA Enabled or Disabled
266 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000267#define SSP_DMA_DISABLED (0)
268#define SSP_DMA_ENABLED (1)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100269
270/*
271 * SSP Clock Defaults
272 */
Linus Walleij556f4ae2010-05-05 09:28:15 +0000273#define SSP_DEFAULT_CLKRATE 0x2
274#define SSP_DEFAULT_PRESCALE 0x40
Linus Walleijb43d65f2009-06-09 08:11:42 +0100275
276/*
277 * SSP Clock Parameter ranges
278 */
279#define CPSDVR_MIN 0x02
280#define CPSDVR_MAX 0xFE
281#define SCR_MIN 0x00
282#define SCR_MAX 0xFF
283
284/*
285 * SSP Interrupt related Macros
286 */
287#define DEFAULT_SSP_REG_IMSC 0x0UL
288#define DISABLE_ALL_INTERRUPTS DEFAULT_SSP_REG_IMSC
289#define ENABLE_ALL_INTERRUPTS (~DEFAULT_SSP_REG_IMSC)
290
291#define CLEAR_ALL_INTERRUPTS 0x3
292
293
294/*
295 * The type of reading going on on this chip
296 */
297enum ssp_reading {
298 READING_NULL,
299 READING_U8,
300 READING_U16,
301 READING_U32
302};
303
304/**
305 * The type of writing going on on this chip
306 */
307enum ssp_writing {
308 WRITING_NULL,
309 WRITING_U8,
310 WRITING_U16,
311 WRITING_U32
312};
313
314/**
315 * struct vendor_data - vendor-specific config parameters
316 * for PL022 derivates
317 * @fifodepth: depth of FIFOs (both)
318 * @max_bpw: maximum number of bits per word
319 * @unidir: supports unidirection transfers
Linus Walleij556f4ae2010-05-05 09:28:15 +0000320 * @extended_cr: 32 bit wide control register 0 with extra
321 * features and extra features in CR1 as found in the ST variants
Linus Walleij781c7b12010-05-07 08:40:53 +0000322 * @pl023: supports a subset of the ST extensions called "PL023"
Linus Walleijb43d65f2009-06-09 08:11:42 +0100323 */
324struct vendor_data {
325 int fifodepth;
326 int max_bpw;
327 bool unidir;
Linus Walleij556f4ae2010-05-05 09:28:15 +0000328 bool extended_cr;
Linus Walleij781c7b12010-05-07 08:40:53 +0000329 bool pl023;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100330};
331
332/**
333 * struct pl022 - This is the private SSP driver data structure
334 * @adev: AMBA device model hookup
Linus Walleij556f4ae2010-05-05 09:28:15 +0000335 * @vendor: Vendor data for the IP block
Linus Walleijb43d65f2009-06-09 08:11:42 +0100336 * @phybase: The physical memory where the SSP device resides
337 * @virtbase: The virtual memory where the SSP is mapped
338 * @master: SPI framework hookup
339 * @master_info: controller-specific data from machine setup
340 * @regs: SSP controller register's virtual address
341 * @pump_messages: Work struct for scheduling work to the workqueue
342 * @lock: spinlock to syncronise access to driver data
343 * @workqueue: a workqueue on which any spi_message request is queued
344 * @busy: workqueue is busy
345 * @run: workqueue is running
346 * @pump_transfers: Tasklet used in Interrupt Transfer mode
347 * @cur_msg: Pointer to current spi_message being processed
348 * @cur_transfer: Pointer to current spi_transfer
349 * @cur_chip: pointer to current clients chip(assigned from controller_state)
350 * @tx: current position in TX buffer to be read
351 * @tx_end: end position in TX buffer to be read
352 * @rx: current position in RX buffer to be written
353 * @rx_end: end position in RX buffer to be written
354 * @readingtype: the type of read currently going on
355 * @writingtype: the type or write currently going on
356 */
357struct pl022 {
358 struct amba_device *adev;
359 struct vendor_data *vendor;
360 resource_size_t phybase;
361 void __iomem *virtbase;
362 struct clk *clk;
363 struct spi_master *master;
364 struct pl022_ssp_controller *master_info;
365 /* Driver message queue */
366 struct workqueue_struct *workqueue;
367 struct work_struct pump_messages;
368 spinlock_t queue_lock;
369 struct list_head queue;
370 int busy;
371 int run;
372 /* Message transfer pump */
373 struct tasklet_struct pump_transfers;
374 struct spi_message *cur_msg;
375 struct spi_transfer *cur_transfer;
376 struct chip_data *cur_chip;
377 void *tx;
378 void *tx_end;
379 void *rx;
380 void *rx_end;
381 enum ssp_reading read;
382 enum ssp_writing write;
Linus Walleijfc054752010-01-22 13:53:30 +0100383 u32 exp_fifo_level;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100384};
385
386/**
387 * struct chip_data - To maintain runtime state of SSP for each client chip
Linus Walleij556f4ae2010-05-05 09:28:15 +0000388 * @cr0: Value of control register CR0 of SSP - on later ST variants this
389 * register is 32 bits wide rather than just 16
Linus Walleijb43d65f2009-06-09 08:11:42 +0100390 * @cr1: Value of control register CR1 of SSP
391 * @dmacr: Value of DMA control Register of SSP
392 * @cpsr: Value of Clock prescale register
393 * @n_bytes: how many bytes(power of 2) reqd for a given data width of client
394 * @enable_dma: Whether to enable DMA or not
395 * @write: function ptr to be used to write when doing xfer for this chip
396 * @read: function ptr to be used to read when doing xfer for this chip
397 * @cs_control: chip select callback provided by chip
398 * @xfer_type: polling/interrupt/DMA
399 *
400 * Runtime state of the SSP controller, maintained per chip,
401 * This would be set according to the current message that would be served
402 */
403struct chip_data {
Linus Walleij556f4ae2010-05-05 09:28:15 +0000404 u32 cr0;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100405 u16 cr1;
406 u16 dmacr;
407 u16 cpsr;
408 u8 n_bytes;
409 u8 enable_dma:1;
410 enum ssp_reading read;
411 enum ssp_writing write;
412 void (*cs_control) (u32 command);
413 int xfer_type;
414};
415
416/**
417 * null_cs_control - Dummy chip select function
418 * @command: select/delect the chip
419 *
420 * If no chip select function is provided by client this is used as dummy
421 * chip select
422 */
423static void null_cs_control(u32 command)
424{
425 pr_debug("pl022: dummy chip select control, CS=0x%x\n", command);
426}
427
428/**
429 * giveback - current spi_message is over, schedule next message and call
430 * callback of this message. Assumes that caller already
431 * set message->status; dma and pio irqs are blocked
432 * @pl022: SSP driver private data structure
433 */
434static void giveback(struct pl022 *pl022)
435{
436 struct spi_transfer *last_transfer;
437 unsigned long flags;
438 struct spi_message *msg;
439 void (*curr_cs_control) (u32 command);
440
441 /*
442 * This local reference to the chip select function
443 * is needed because we set curr_chip to NULL
444 * as a step toward termininating the message.
445 */
446 curr_cs_control = pl022->cur_chip->cs_control;
447 spin_lock_irqsave(&pl022->queue_lock, flags);
448 msg = pl022->cur_msg;
449 pl022->cur_msg = NULL;
450 pl022->cur_transfer = NULL;
451 pl022->cur_chip = NULL;
452 queue_work(pl022->workqueue, &pl022->pump_messages);
453 spin_unlock_irqrestore(&pl022->queue_lock, flags);
454
455 last_transfer = list_entry(msg->transfers.prev,
456 struct spi_transfer,
457 transfer_list);
458
459 /* Delay if requested before any change in chip select */
460 if (last_transfer->delay_usecs)
461 /*
462 * FIXME: This runs in interrupt context.
463 * Is this really smart?
464 */
465 udelay(last_transfer->delay_usecs);
466
467 /*
468 * Drop chip select UNLESS cs_change is true or we are returning
469 * a message with an error, or next message is for another chip
470 */
471 if (!last_transfer->cs_change)
472 curr_cs_control(SSP_CHIP_DESELECT);
473 else {
474 struct spi_message *next_msg;
475
476 /* Holding of cs was hinted, but we need to make sure
477 * the next message is for the same chip. Don't waste
478 * time with the following tests unless this was hinted.
479 *
480 * We cannot postpone this until pump_messages, because
481 * after calling msg->complete (below) the driver that
482 * sent the current message could be unloaded, which
483 * could invalidate the cs_control() callback...
484 */
485
486 /* get a pointer to the next message, if any */
487 spin_lock_irqsave(&pl022->queue_lock, flags);
488 if (list_empty(&pl022->queue))
489 next_msg = NULL;
490 else
491 next_msg = list_entry(pl022->queue.next,
492 struct spi_message, queue);
493 spin_unlock_irqrestore(&pl022->queue_lock, flags);
494
495 /* see if the next and current messages point
496 * to the same chip
497 */
498 if (next_msg && next_msg->spi != msg->spi)
499 next_msg = NULL;
500 if (!next_msg || msg->state == STATE_ERROR)
501 curr_cs_control(SSP_CHIP_DESELECT);
502 }
503 msg->state = NULL;
504 if (msg->complete)
505 msg->complete(msg->context);
Linus Walleij545074f2010-08-21 11:07:36 +0200506 /* This message is completed, so let's turn off the clocks! */
Linus Walleijb43d65f2009-06-09 08:11:42 +0100507 clk_disable(pl022->clk);
Linus Walleij545074f2010-08-21 11:07:36 +0200508 amba_pclk_disable(pl022->adev);
Linus Walleijb43d65f2009-06-09 08:11:42 +0100509}
510
511/**
512 * flush - flush the FIFO to reach a clean state
513 * @pl022: SSP driver private data structure
514 */
515static int flush(struct pl022 *pl022)
516{
517 unsigned long limit = loops_per_jiffy << 1;
518
519 dev_dbg(&pl022->adev->dev, "flush\n");
520 do {
521 while (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
522 readw(SSP_DR(pl022->virtbase));
523 } while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_BSY) && limit--);
Linus Walleijfc054752010-01-22 13:53:30 +0100524
525 pl022->exp_fifo_level = 0;
526
Linus Walleijb43d65f2009-06-09 08:11:42 +0100527 return limit;
528}
529
530/**
531 * restore_state - Load configuration of current chip
532 * @pl022: SSP driver private data structure
533 */
534static void restore_state(struct pl022 *pl022)
535{
536 struct chip_data *chip = pl022->cur_chip;
537
Linus Walleij556f4ae2010-05-05 09:28:15 +0000538 if (pl022->vendor->extended_cr)
539 writel(chip->cr0, SSP_CR0(pl022->virtbase));
540 else
541 writew(chip->cr0, SSP_CR0(pl022->virtbase));
Linus Walleijb43d65f2009-06-09 08:11:42 +0100542 writew(chip->cr1, SSP_CR1(pl022->virtbase));
543 writew(chip->dmacr, SSP_DMACR(pl022->virtbase));
544 writew(chip->cpsr, SSP_CPSR(pl022->virtbase));
545 writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
546 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
547}
548
Linus Walleijb43d65f2009-06-09 08:11:42 +0100549/*
550 * Default SSP Register Values
551 */
552#define DEFAULT_SSP_REG_CR0 ( \
553 GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS, 0) | \
Linus Walleij556f4ae2010-05-05 09:28:15 +0000554 GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF, 4) | \
Linus Walleijb43d65f2009-06-09 08:11:42 +0100555 GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
Linus Walleijee2b8052009-08-15 15:12:05 +0100556 GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
Linus Walleij556f4ae2010-05-05 09:28:15 +0000557 GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
558)
559
560/* ST versions have slightly different bit layout */
561#define DEFAULT_SSP_REG_CR0_ST ( \
562 GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
563 GEN_MASK_BITS(SSP_MICROWIRE_CHANNEL_FULL_DUPLEX, SSP_CR0_MASK_HALFDUP_ST, 5) | \
564 GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
565 GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
566 GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) | \
567 GEN_MASK_BITS(SSP_BITS_8, SSP_CR0_MASK_CSS_ST, 16) | \
568 GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF_ST, 21) \
Linus Walleijb43d65f2009-06-09 08:11:42 +0100569)
570
Linus Walleij781c7b12010-05-07 08:40:53 +0000571/* The PL023 version is slightly different again */
572#define DEFAULT_SSP_REG_CR0_ST_PL023 ( \
573 GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
574 GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
575 GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
576 GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
577)
578
Linus Walleijb43d65f2009-06-09 08:11:42 +0100579#define DEFAULT_SSP_REG_CR1 ( \
580 GEN_MASK_BITS(LOOPBACK_DISABLED, SSP_CR1_MASK_LBM, 0) | \
581 GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
582 GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
Linus Walleij556f4ae2010-05-05 09:28:15 +0000583 GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) \
Linus Walleijb43d65f2009-06-09 08:11:42 +0100584)
585
Linus Walleij556f4ae2010-05-05 09:28:15 +0000586/* ST versions extend this register to use all 16 bits */
587#define DEFAULT_SSP_REG_CR1_ST ( \
588 DEFAULT_SSP_REG_CR1 | \
589 GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
590 GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
591 GEN_MASK_BITS(SSP_MWIRE_WAIT_ZERO, SSP_CR1_MASK_MWAIT_ST, 6) |\
592 GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
593 GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) \
594)
595
Linus Walleij781c7b12010-05-07 08:40:53 +0000596/*
597 * The PL023 variant has further differences: no loopback mode, no microwire
598 * support, and a new clock feedback delay setting.
599 */
600#define DEFAULT_SSP_REG_CR1_ST_PL023 ( \
601 GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
602 GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
603 GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) | \
604 GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
605 GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
606 GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
607 GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) | \
608 GEN_MASK_BITS(SSP_FEEDBACK_CLK_DELAY_NONE, SSP_CR1_MASK_FBCLKDEL_ST, 13) \
609)
Linus Walleij556f4ae2010-05-05 09:28:15 +0000610
Linus Walleijb43d65f2009-06-09 08:11:42 +0100611#define DEFAULT_SSP_REG_CPSR ( \
Linus Walleij556f4ae2010-05-05 09:28:15 +0000612 GEN_MASK_BITS(SSP_DEFAULT_PRESCALE, SSP_CPSR_MASK_CPSDVSR, 0) \
Linus Walleijb43d65f2009-06-09 08:11:42 +0100613)
614
615#define DEFAULT_SSP_REG_DMACR (\
616 GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_RXDMAE, 0) | \
617 GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_TXDMAE, 1) \
618)
619
Linus Walleij781c7b12010-05-07 08:40:53 +0000620/**
621 * load_ssp_default_config - Load default configuration for SSP
622 * @pl022: SSP driver private data structure
623 */
Linus Walleijb43d65f2009-06-09 08:11:42 +0100624static void load_ssp_default_config(struct pl022 *pl022)
625{
Linus Walleij781c7b12010-05-07 08:40:53 +0000626 if (pl022->vendor->pl023) {
627 writel(DEFAULT_SSP_REG_CR0_ST_PL023, SSP_CR0(pl022->virtbase));
628 writew(DEFAULT_SSP_REG_CR1_ST_PL023, SSP_CR1(pl022->virtbase));
629 } else if (pl022->vendor->extended_cr) {
Linus Walleij556f4ae2010-05-05 09:28:15 +0000630 writel(DEFAULT_SSP_REG_CR0_ST, SSP_CR0(pl022->virtbase));
631 writew(DEFAULT_SSP_REG_CR1_ST, SSP_CR1(pl022->virtbase));
632 } else {
633 writew(DEFAULT_SSP_REG_CR0, SSP_CR0(pl022->virtbase));
634 writew(DEFAULT_SSP_REG_CR1, SSP_CR1(pl022->virtbase));
635 }
Linus Walleijb43d65f2009-06-09 08:11:42 +0100636 writew(DEFAULT_SSP_REG_DMACR, SSP_DMACR(pl022->virtbase));
637 writew(DEFAULT_SSP_REG_CPSR, SSP_CPSR(pl022->virtbase));
638 writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
639 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
640}
641
642/**
643 * This will write to TX and read from RX according to the parameters
644 * set in pl022.
645 */
646static void readwriter(struct pl022 *pl022)
647{
648
649 /*
650 * The FIFO depth is different inbetween primecell variants.
651 * I believe filling in too much in the FIFO might cause
652 * errons in 8bit wide transfers on ARM variants (just 8 words
653 * FIFO, means only 8x8 = 64 bits in FIFO) at least.
654 *
Linus Walleijfc054752010-01-22 13:53:30 +0100655 * To prevent this issue, the TX FIFO is only filled to the
656 * unused RX FIFO fill length, regardless of what the TX
657 * FIFO status flag indicates.
Linus Walleijb43d65f2009-06-09 08:11:42 +0100658 */
659 dev_dbg(&pl022->adev->dev,
660 "%s, rx: %p, rxend: %p, tx: %p, txend: %p\n",
661 __func__, pl022->rx, pl022->rx_end, pl022->tx, pl022->tx_end);
662
663 /* Read as much as you can */
664 while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
665 && (pl022->rx < pl022->rx_end)) {
666 switch (pl022->read) {
667 case READING_NULL:
668 readw(SSP_DR(pl022->virtbase));
669 break;
670 case READING_U8:
671 *(u8 *) (pl022->rx) =
672 readw(SSP_DR(pl022->virtbase)) & 0xFFU;
673 break;
674 case READING_U16:
675 *(u16 *) (pl022->rx) =
676 (u16) readw(SSP_DR(pl022->virtbase));
677 break;
678 case READING_U32:
679 *(u32 *) (pl022->rx) =
680 readl(SSP_DR(pl022->virtbase));
681 break;
682 }
683 pl022->rx += (pl022->cur_chip->n_bytes);
Linus Walleijfc054752010-01-22 13:53:30 +0100684 pl022->exp_fifo_level--;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100685 }
686 /*
Linus Walleijfc054752010-01-22 13:53:30 +0100687 * Write as much as possible up to the RX FIFO size
Linus Walleijb43d65f2009-06-09 08:11:42 +0100688 */
Linus Walleijfc054752010-01-22 13:53:30 +0100689 while ((pl022->exp_fifo_level < pl022->vendor->fifodepth)
Linus Walleijb43d65f2009-06-09 08:11:42 +0100690 && (pl022->tx < pl022->tx_end)) {
691 switch (pl022->write) {
692 case WRITING_NULL:
693 writew(0x0, SSP_DR(pl022->virtbase));
694 break;
695 case WRITING_U8:
696 writew(*(u8 *) (pl022->tx), SSP_DR(pl022->virtbase));
697 break;
698 case WRITING_U16:
699 writew((*(u16 *) (pl022->tx)), SSP_DR(pl022->virtbase));
700 break;
701 case WRITING_U32:
702 writel(*(u32 *) (pl022->tx), SSP_DR(pl022->virtbase));
703 break;
704 }
705 pl022->tx += (pl022->cur_chip->n_bytes);
Linus Walleijfc054752010-01-22 13:53:30 +0100706 pl022->exp_fifo_level++;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100707 /*
708 * This inner reader takes care of things appearing in the RX
709 * FIFO as we're transmitting. This will happen a lot since the
710 * clock starts running when you put things into the TX FIFO,
711 * and then things are continously clocked into the RX FIFO.
712 */
713 while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
714 && (pl022->rx < pl022->rx_end)) {
715 switch (pl022->read) {
716 case READING_NULL:
717 readw(SSP_DR(pl022->virtbase));
718 break;
719 case READING_U8:
720 *(u8 *) (pl022->rx) =
721 readw(SSP_DR(pl022->virtbase)) & 0xFFU;
722 break;
723 case READING_U16:
724 *(u16 *) (pl022->rx) =
725 (u16) readw(SSP_DR(pl022->virtbase));
726 break;
727 case READING_U32:
728 *(u32 *) (pl022->rx) =
729 readl(SSP_DR(pl022->virtbase));
730 break;
731 }
732 pl022->rx += (pl022->cur_chip->n_bytes);
Linus Walleijfc054752010-01-22 13:53:30 +0100733 pl022->exp_fifo_level--;
Linus Walleijb43d65f2009-06-09 08:11:42 +0100734 }
735 }
736 /*
737 * When we exit here the TX FIFO should be full and the RX FIFO
738 * should be empty
739 */
740}
741
742
743/**
744 * next_transfer - Move to the Next transfer in the current spi message
745 * @pl022: SSP driver private data structure
746 *
747 * This function moves though the linked list of spi transfers in the
748 * current spi message and returns with the state of current spi
749 * message i.e whether its last transfer is done(STATE_DONE) or
750 * Next transfer is ready(STATE_RUNNING)
751 */
752static void *next_transfer(struct pl022 *pl022)
753{
754 struct spi_message *msg = pl022->cur_msg;
755 struct spi_transfer *trans = pl022->cur_transfer;
756
757 /* Move to next transfer */
758 if (trans->transfer_list.next != &msg->transfers) {
759 pl022->cur_transfer =
760 list_entry(trans->transfer_list.next,
761 struct spi_transfer, transfer_list);
762 return STATE_RUNNING;
763 }
764 return STATE_DONE;
765}
766/**
767 * pl022_interrupt_handler - Interrupt handler for SSP controller
768 *
769 * This function handles interrupts generated for an interrupt based transfer.
770 * If a receive overrun (ROR) interrupt is there then we disable SSP, flag the
771 * current message's state as STATE_ERROR and schedule the tasklet
772 * pump_transfers which will do the postprocessing of the current message by
773 * calling giveback(). Otherwise it reads data from RX FIFO till there is no
774 * more data, and writes data in TX FIFO till it is not full. If we complete
775 * the transfer we move to the next transfer and schedule the tasklet.
776 */
777static irqreturn_t pl022_interrupt_handler(int irq, void *dev_id)
778{
779 struct pl022 *pl022 = dev_id;
780 struct spi_message *msg = pl022->cur_msg;
781 u16 irq_status = 0;
782 u16 flag = 0;
783
784 if (unlikely(!msg)) {
785 dev_err(&pl022->adev->dev,
786 "bad message state in interrupt handler");
787 /* Never fail */
788 return IRQ_HANDLED;
789 }
790
791 /* Read the Interrupt Status Register */
792 irq_status = readw(SSP_MIS(pl022->virtbase));
793
794 if (unlikely(!irq_status))
795 return IRQ_NONE;
796
797 /* This handles the error code interrupts */
798 if (unlikely(irq_status & SSP_MIS_MASK_RORMIS)) {
799 /*
800 * Overrun interrupt - bail out since our Data has been
801 * corrupted
802 */
803 dev_err(&pl022->adev->dev,
804 "FIFO overrun\n");
805 if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RFF)
806 dev_err(&pl022->adev->dev,
807 "RXFIFO is full\n");
808 if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_TNF)
809 dev_err(&pl022->adev->dev,
810 "TXFIFO is full\n");
811
812 /*
813 * Disable and clear interrupts, disable SSP,
814 * mark message with bad status so it can be
815 * retried.
816 */
817 writew(DISABLE_ALL_INTERRUPTS,
818 SSP_IMSC(pl022->virtbase));
819 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
820 writew((readw(SSP_CR1(pl022->virtbase)) &
821 (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
822 msg->state = STATE_ERROR;
823
824 /* Schedule message queue handler */
825 tasklet_schedule(&pl022->pump_transfers);
826 return IRQ_HANDLED;
827 }
828
829 readwriter(pl022);
830
831 if ((pl022->tx == pl022->tx_end) && (flag == 0)) {
832 flag = 1;
833 /* Disable Transmit interrupt */
834 writew(readw(SSP_IMSC(pl022->virtbase)) &
835 (~SSP_IMSC_MASK_TXIM),
836 SSP_IMSC(pl022->virtbase));
837 }
838
839 /*
840 * Since all transactions must write as much as shall be read,
841 * we can conclude the entire transaction once RX is complete.
842 * At this point, all TX will always be finished.
843 */
844 if (pl022->rx >= pl022->rx_end) {
845 writew(DISABLE_ALL_INTERRUPTS,
846 SSP_IMSC(pl022->virtbase));
847 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
848 if (unlikely(pl022->rx > pl022->rx_end)) {
849 dev_warn(&pl022->adev->dev, "read %u surplus "
850 "bytes (did you request an odd "
851 "number of bytes on a 16bit bus?)\n",
852 (u32) (pl022->rx - pl022->rx_end));
853 }
854 /* Update total bytes transfered */
855 msg->actual_length += pl022->cur_transfer->len;
856 if (pl022->cur_transfer->cs_change)
857 pl022->cur_chip->
858 cs_control(SSP_CHIP_DESELECT);
859 /* Move to next transfer */
860 msg->state = next_transfer(pl022);
861 tasklet_schedule(&pl022->pump_transfers);
862 return IRQ_HANDLED;
863 }
864
865 return IRQ_HANDLED;
866}
867
868/**
869 * This sets up the pointers to memory for the next message to
870 * send out on the SPI bus.
871 */
872static int set_up_next_transfer(struct pl022 *pl022,
873 struct spi_transfer *transfer)
874{
875 int residue;
876
877 /* Sanity check the message for this bus width */
878 residue = pl022->cur_transfer->len % pl022->cur_chip->n_bytes;
879 if (unlikely(residue != 0)) {
880 dev_err(&pl022->adev->dev,
881 "message of %u bytes to transmit but the current "
882 "chip bus has a data width of %u bytes!\n",
883 pl022->cur_transfer->len,
884 pl022->cur_chip->n_bytes);
885 dev_err(&pl022->adev->dev, "skipping this message\n");
886 return -EIO;
887 }
888 pl022->tx = (void *)transfer->tx_buf;
889 pl022->tx_end = pl022->tx + pl022->cur_transfer->len;
890 pl022->rx = (void *)transfer->rx_buf;
891 pl022->rx_end = pl022->rx + pl022->cur_transfer->len;
892 pl022->write =
893 pl022->tx ? pl022->cur_chip->write : WRITING_NULL;
894 pl022->read = pl022->rx ? pl022->cur_chip->read : READING_NULL;
895 return 0;
896}
897
898/**
899 * pump_transfers - Tasklet function which schedules next interrupt transfer
900 * when running in interrupt transfer mode.
901 * @data: SSP driver private data structure
902 *
903 */
904static void pump_transfers(unsigned long data)
905{
906 struct pl022 *pl022 = (struct pl022 *) data;
907 struct spi_message *message = NULL;
908 struct spi_transfer *transfer = NULL;
909 struct spi_transfer *previous = NULL;
910
911 /* Get current state information */
912 message = pl022->cur_msg;
913 transfer = pl022->cur_transfer;
914
915 /* Handle for abort */
916 if (message->state == STATE_ERROR) {
917 message->status = -EIO;
918 giveback(pl022);
919 return;
920 }
921
922 /* Handle end of message */
923 if (message->state == STATE_DONE) {
924 message->status = 0;
925 giveback(pl022);
926 return;
927 }
928
929 /* Delay if requested at end of transfer before CS change */
930 if (message->state == STATE_RUNNING) {
931 previous = list_entry(transfer->transfer_list.prev,
932 struct spi_transfer,
933 transfer_list);
934 if (previous->delay_usecs)
935 /*
936 * FIXME: This runs in interrupt context.
937 * Is this really smart?
938 */
939 udelay(previous->delay_usecs);
940
941 /* Drop chip select only if cs_change is requested */
942 if (previous->cs_change)
943 pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
944 } else {
945 /* STATE_START */
946 message->state = STATE_RUNNING;
947 }
948
949 if (set_up_next_transfer(pl022, transfer)) {
950 message->state = STATE_ERROR;
951 message->status = -EIO;
952 giveback(pl022);
953 return;
954 }
955 /* Flush the FIFOs and let's go! */
956 flush(pl022);
957 writew(ENABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
958}
959
960/**
961 * NOT IMPLEMENTED
962 * configure_dma - It configures the DMA pipes for DMA transfers
963 * @data: SSP driver's private data structure
964 *
965 */
966static int configure_dma(void *data)
967{
968 struct pl022 *pl022 = data;
969 dev_dbg(&pl022->adev->dev, "configure DMA\n");
970 return -ENOTSUPP;
971}
972
973/**
974 * do_dma_transfer - It handles transfers of the current message
975 * if it is DMA xfer.
976 * NOT FULLY IMPLEMENTED
977 * @data: SSP driver's private data structure
978 */
979static void do_dma_transfer(void *data)
980{
981 struct pl022 *pl022 = data;
982
983 if (configure_dma(data)) {
984 dev_dbg(&pl022->adev->dev, "configuration of DMA Failed!\n");
985 goto err_config_dma;
986 }
987
988 /* TODO: Implememt DMA setup of pipes here */
989
990 /* Enable target chip, set up transfer */
991 pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
992 if (set_up_next_transfer(pl022, pl022->cur_transfer)) {
993 /* Error path */
994 pl022->cur_msg->state = STATE_ERROR;
995 pl022->cur_msg->status = -EIO;
996 giveback(pl022);
997 return;
998 }
999 /* Enable SSP */
1000 writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
1001 SSP_CR1(pl022->virtbase));
1002
1003 /* TODO: Enable the DMA transfer here */
1004 return;
1005
1006 err_config_dma:
1007 pl022->cur_msg->state = STATE_ERROR;
1008 pl022->cur_msg->status = -EIO;
1009 giveback(pl022);
1010 return;
1011}
1012
1013static void do_interrupt_transfer(void *data)
1014{
1015 struct pl022 *pl022 = data;
1016
1017 /* Enable target chip */
1018 pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
1019 if (set_up_next_transfer(pl022, pl022->cur_transfer)) {
1020 /* Error path */
1021 pl022->cur_msg->state = STATE_ERROR;
1022 pl022->cur_msg->status = -EIO;
1023 giveback(pl022);
1024 return;
1025 }
1026 /* Enable SSP, turn on interrupts */
1027 writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
1028 SSP_CR1(pl022->virtbase));
1029 writew(ENABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
1030}
1031
1032static void do_polling_transfer(void *data)
1033{
1034 struct pl022 *pl022 = data;
1035 struct spi_message *message = NULL;
1036 struct spi_transfer *transfer = NULL;
1037 struct spi_transfer *previous = NULL;
1038 struct chip_data *chip;
1039
1040 chip = pl022->cur_chip;
1041 message = pl022->cur_msg;
1042
1043 while (message->state != STATE_DONE) {
1044 /* Handle for abort */
1045 if (message->state == STATE_ERROR)
1046 break;
1047 transfer = pl022->cur_transfer;
1048
1049 /* Delay if requested at end of transfer */
1050 if (message->state == STATE_RUNNING) {
1051 previous =
1052 list_entry(transfer->transfer_list.prev,
1053 struct spi_transfer, transfer_list);
1054 if (previous->delay_usecs)
1055 udelay(previous->delay_usecs);
1056 if (previous->cs_change)
1057 pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
1058 } else {
1059 /* STATE_START */
1060 message->state = STATE_RUNNING;
1061 pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
1062 }
1063
1064 /* Configuration Changing Per Transfer */
1065 if (set_up_next_transfer(pl022, transfer)) {
1066 /* Error path */
1067 message->state = STATE_ERROR;
1068 break;
1069 }
1070 /* Flush FIFOs and enable SSP */
1071 flush(pl022);
1072 writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
1073 SSP_CR1(pl022->virtbase));
1074
Linus Walleij556f4ae2010-05-05 09:28:15 +00001075 dev_dbg(&pl022->adev->dev, "polling transfer ongoing ...\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01001076 /* FIXME: insert a timeout so we don't hang here indefinately */
1077 while (pl022->tx < pl022->tx_end || pl022->rx < pl022->rx_end)
1078 readwriter(pl022);
1079
1080 /* Update total byte transfered */
1081 message->actual_length += pl022->cur_transfer->len;
1082 if (pl022->cur_transfer->cs_change)
1083 pl022->cur_chip->cs_control(SSP_CHIP_DESELECT);
1084 /* Move to next transfer */
1085 message->state = next_transfer(pl022);
1086 }
1087
1088 /* Handle end of message */
1089 if (message->state == STATE_DONE)
1090 message->status = 0;
1091 else
1092 message->status = -EIO;
1093
1094 giveback(pl022);
1095 return;
1096}
1097
1098/**
1099 * pump_messages - Workqueue function which processes spi message queue
1100 * @data: pointer to private data of SSP driver
1101 *
1102 * This function checks if there is any spi message in the queue that
1103 * needs processing and delegate control to appropriate function
1104 * do_polling_transfer()/do_interrupt_transfer()/do_dma_transfer()
1105 * based on the kind of the transfer
1106 *
1107 */
1108static void pump_messages(struct work_struct *work)
1109{
1110 struct pl022 *pl022 =
1111 container_of(work, struct pl022, pump_messages);
1112 unsigned long flags;
1113
1114 /* Lock queue and check for queue work */
1115 spin_lock_irqsave(&pl022->queue_lock, flags);
1116 if (list_empty(&pl022->queue) || pl022->run == QUEUE_STOPPED) {
1117 pl022->busy = 0;
1118 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1119 return;
1120 }
1121 /* Make sure we are not already running a message */
1122 if (pl022->cur_msg) {
1123 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1124 return;
1125 }
1126 /* Extract head of queue */
1127 pl022->cur_msg =
1128 list_entry(pl022->queue.next, struct spi_message, queue);
1129
1130 list_del_init(&pl022->cur_msg->queue);
1131 pl022->busy = 1;
1132 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1133
1134 /* Initial message state */
1135 pl022->cur_msg->state = STATE_START;
1136 pl022->cur_transfer = list_entry(pl022->cur_msg->transfers.next,
1137 struct spi_transfer,
1138 transfer_list);
1139
1140 /* Setup the SPI using the per chip configuration */
1141 pl022->cur_chip = spi_get_ctldata(pl022->cur_msg->spi);
1142 /*
Linus Walleij545074f2010-08-21 11:07:36 +02001143 * We enable the clocks here, then the clocks will be disabled when
Linus Walleijb43d65f2009-06-09 08:11:42 +01001144 * giveback() is called in each method (poll/interrupt/DMA)
1145 */
Linus Walleij545074f2010-08-21 11:07:36 +02001146 amba_pclk_enable(pl022->adev);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001147 clk_enable(pl022->clk);
1148 restore_state(pl022);
1149 flush(pl022);
1150
1151 if (pl022->cur_chip->xfer_type == POLLING_TRANSFER)
1152 do_polling_transfer(pl022);
1153 else if (pl022->cur_chip->xfer_type == INTERRUPT_TRANSFER)
1154 do_interrupt_transfer(pl022);
1155 else
1156 do_dma_transfer(pl022);
1157}
1158
1159
1160static int __init init_queue(struct pl022 *pl022)
1161{
1162 INIT_LIST_HEAD(&pl022->queue);
1163 spin_lock_init(&pl022->queue_lock);
1164
1165 pl022->run = QUEUE_STOPPED;
1166 pl022->busy = 0;
1167
1168 tasklet_init(&pl022->pump_transfers,
1169 pump_transfers, (unsigned long)pl022);
1170
1171 INIT_WORK(&pl022->pump_messages, pump_messages);
1172 pl022->workqueue = create_singlethread_workqueue(
1173 dev_name(pl022->master->dev.parent));
1174 if (pl022->workqueue == NULL)
1175 return -EBUSY;
1176
1177 return 0;
1178}
1179
1180
1181static int start_queue(struct pl022 *pl022)
1182{
1183 unsigned long flags;
1184
1185 spin_lock_irqsave(&pl022->queue_lock, flags);
1186
1187 if (pl022->run == QUEUE_RUNNING || pl022->busy) {
1188 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1189 return -EBUSY;
1190 }
1191
1192 pl022->run = QUEUE_RUNNING;
1193 pl022->cur_msg = NULL;
1194 pl022->cur_transfer = NULL;
1195 pl022->cur_chip = NULL;
1196 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1197
1198 queue_work(pl022->workqueue, &pl022->pump_messages);
1199
1200 return 0;
1201}
1202
1203
1204static int stop_queue(struct pl022 *pl022)
1205{
1206 unsigned long flags;
1207 unsigned limit = 500;
1208 int status = 0;
1209
1210 spin_lock_irqsave(&pl022->queue_lock, flags);
1211
1212 /* This is a bit lame, but is optimized for the common execution path.
1213 * A wait_queue on the pl022->busy could be used, but then the common
1214 * execution path (pump_messages) would be required to call wake_up or
1215 * friends on every SPI message. Do this instead */
Linus Walleijb43d65f2009-06-09 08:11:42 +01001216 while (!list_empty(&pl022->queue) && pl022->busy && limit--) {
1217 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1218 msleep(10);
1219 spin_lock_irqsave(&pl022->queue_lock, flags);
1220 }
1221
1222 if (!list_empty(&pl022->queue) || pl022->busy)
1223 status = -EBUSY;
Grzegorz Sygieda4a124042010-05-20 22:28:30 +00001224 else pl022->run = QUEUE_STOPPED;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001225
1226 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1227
1228 return status;
1229}
1230
1231static int destroy_queue(struct pl022 *pl022)
1232{
1233 int status;
1234
1235 status = stop_queue(pl022);
1236 /* we are unloading the module or failing to load (only two calls
1237 * to this routine), and neither call can handle a return value.
1238 * However, destroy_workqueue calls flush_workqueue, and that will
1239 * block until all work is done. If the reason that stop_queue
1240 * timed out is that the work will never finish, then it does no
1241 * good to call destroy_workqueue, so return anyway. */
1242 if (status != 0)
1243 return status;
1244
1245 destroy_workqueue(pl022->workqueue);
1246
1247 return 0;
1248}
1249
1250static int verify_controller_parameters(struct pl022 *pl022,
1251 struct pl022_config_chip *chip_info)
1252{
1253 if ((chip_info->lbm != LOOPBACK_ENABLED)
1254 && (chip_info->lbm != LOOPBACK_DISABLED)) {
1255 dev_err(chip_info->dev,
1256 "loopback Mode is configured incorrectly\n");
1257 return -EINVAL;
1258 }
1259 if ((chip_info->iface < SSP_INTERFACE_MOTOROLA_SPI)
1260 || (chip_info->iface > SSP_INTERFACE_UNIDIRECTIONAL)) {
1261 dev_err(chip_info->dev,
1262 "interface is configured incorrectly\n");
1263 return -EINVAL;
1264 }
1265 if ((chip_info->iface == SSP_INTERFACE_UNIDIRECTIONAL) &&
1266 (!pl022->vendor->unidir)) {
1267 dev_err(chip_info->dev,
1268 "unidirectional mode not supported in this "
1269 "hardware version\n");
1270 return -EINVAL;
1271 }
1272 if ((chip_info->hierarchy != SSP_MASTER)
1273 && (chip_info->hierarchy != SSP_SLAVE)) {
1274 dev_err(chip_info->dev,
1275 "hierarchy is configured incorrectly\n");
1276 return -EINVAL;
1277 }
1278 if (((chip_info->clk_freq).cpsdvsr < CPSDVR_MIN)
1279 || ((chip_info->clk_freq).cpsdvsr > CPSDVR_MAX)) {
1280 dev_err(chip_info->dev,
1281 "cpsdvsr is configured incorrectly\n");
1282 return -EINVAL;
1283 }
1284 if ((chip_info->endian_rx != SSP_RX_MSB)
1285 && (chip_info->endian_rx != SSP_RX_LSB)) {
1286 dev_err(chip_info->dev,
1287 "RX FIFO endianess is configured incorrectly\n");
1288 return -EINVAL;
1289 }
1290 if ((chip_info->endian_tx != SSP_TX_MSB)
1291 && (chip_info->endian_tx != SSP_TX_LSB)) {
1292 dev_err(chip_info->dev,
1293 "TX FIFO endianess is configured incorrectly\n");
1294 return -EINVAL;
1295 }
1296 if ((chip_info->data_size < SSP_DATA_BITS_4)
1297 || (chip_info->data_size > SSP_DATA_BITS_32)) {
1298 dev_err(chip_info->dev,
1299 "DATA Size is configured incorrectly\n");
1300 return -EINVAL;
1301 }
1302 if ((chip_info->com_mode != INTERRUPT_TRANSFER)
1303 && (chip_info->com_mode != DMA_TRANSFER)
1304 && (chip_info->com_mode != POLLING_TRANSFER)) {
1305 dev_err(chip_info->dev,
1306 "Communication mode is configured incorrectly\n");
1307 return -EINVAL;
1308 }
1309 if ((chip_info->rx_lev_trig < SSP_RX_1_OR_MORE_ELEM)
1310 || (chip_info->rx_lev_trig > SSP_RX_32_OR_MORE_ELEM)) {
1311 dev_err(chip_info->dev,
1312 "RX FIFO Trigger Level is configured incorrectly\n");
1313 return -EINVAL;
1314 }
1315 if ((chip_info->tx_lev_trig < SSP_TX_1_OR_MORE_EMPTY_LOC)
1316 || (chip_info->tx_lev_trig > SSP_TX_32_OR_MORE_EMPTY_LOC)) {
1317 dev_err(chip_info->dev,
1318 "TX FIFO Trigger Level is configured incorrectly\n");
1319 return -EINVAL;
1320 }
1321 if (chip_info->iface == SSP_INTERFACE_MOTOROLA_SPI) {
Linus Walleijee2b8052009-08-15 15:12:05 +01001322 if ((chip_info->clk_phase != SSP_CLK_FIRST_EDGE)
1323 && (chip_info->clk_phase != SSP_CLK_SECOND_EDGE)) {
Linus Walleijb43d65f2009-06-09 08:11:42 +01001324 dev_err(chip_info->dev,
1325 "Clock Phase is configured incorrectly\n");
1326 return -EINVAL;
1327 }
1328 if ((chip_info->clk_pol != SSP_CLK_POL_IDLE_LOW)
1329 && (chip_info->clk_pol != SSP_CLK_POL_IDLE_HIGH)) {
1330 dev_err(chip_info->dev,
1331 "Clock Polarity is configured incorrectly\n");
1332 return -EINVAL;
1333 }
1334 }
1335 if (chip_info->iface == SSP_INTERFACE_NATIONAL_MICROWIRE) {
1336 if ((chip_info->ctrl_len < SSP_BITS_4)
1337 || (chip_info->ctrl_len > SSP_BITS_32)) {
1338 dev_err(chip_info->dev,
1339 "CTRL LEN is configured incorrectly\n");
1340 return -EINVAL;
1341 }
1342 if ((chip_info->wait_state != SSP_MWIRE_WAIT_ZERO)
1343 && (chip_info->wait_state != SSP_MWIRE_WAIT_ONE)) {
1344 dev_err(chip_info->dev,
1345 "Wait State is configured incorrectly\n");
1346 return -EINVAL;
1347 }
Linus Walleij556f4ae2010-05-05 09:28:15 +00001348 /* Half duplex is only available in the ST Micro version */
1349 if (pl022->vendor->extended_cr) {
1350 if ((chip_info->duplex !=
1351 SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
1352 && (chip_info->duplex !=
Julia Lawall4a4fd472010-09-29 17:31:30 +09001353 SSP_MICROWIRE_CHANNEL_HALF_DUPLEX)) {
Linus Walleij556f4ae2010-05-05 09:28:15 +00001354 dev_err(chip_info->dev,
1355 "Microwire duplex mode is configured incorrectly\n");
1356 return -EINVAL;
Julia Lawall4a4fd472010-09-29 17:31:30 +09001357 }
Linus Walleij556f4ae2010-05-05 09:28:15 +00001358 } else {
1359 if (chip_info->duplex != SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
1360 dev_err(chip_info->dev,
1361 "Microwire half duplex mode requested,"
1362 " but this is only available in the"
1363 " ST version of PL022\n");
Linus Walleijb43d65f2009-06-09 08:11:42 +01001364 return -EINVAL;
1365 }
1366 }
1367 if (chip_info->cs_control == NULL) {
1368 dev_warn(chip_info->dev,
1369 "Chip Select Function is NULL for this chip\n");
1370 chip_info->cs_control = null_cs_control;
1371 }
1372 return 0;
1373}
1374
1375/**
1376 * pl022_transfer - transfer function registered to SPI master framework
1377 * @spi: spi device which is requesting transfer
1378 * @msg: spi message which is to handled is queued to driver queue
1379 *
1380 * This function is registered to the SPI framework for this SPI master
1381 * controller. It will queue the spi_message in the queue of driver if
1382 * the queue is not stopped and return.
1383 */
1384static int pl022_transfer(struct spi_device *spi, struct spi_message *msg)
1385{
1386 struct pl022 *pl022 = spi_master_get_devdata(spi->master);
1387 unsigned long flags;
1388
1389 spin_lock_irqsave(&pl022->queue_lock, flags);
1390
1391 if (pl022->run == QUEUE_STOPPED) {
1392 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1393 return -ESHUTDOWN;
1394 }
1395 msg->actual_length = 0;
1396 msg->status = -EINPROGRESS;
1397 msg->state = STATE_START;
1398
1399 list_add_tail(&msg->queue, &pl022->queue);
1400 if (pl022->run == QUEUE_RUNNING && !pl022->busy)
1401 queue_work(pl022->workqueue, &pl022->pump_messages);
1402
1403 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1404 return 0;
1405}
1406
1407static int calculate_effective_freq(struct pl022 *pl022,
1408 int freq,
1409 struct ssp_clock_params *clk_freq)
1410{
1411 /* Lets calculate the frequency parameters */
1412 u16 cpsdvsr = 2;
1413 u16 scr = 0;
1414 bool freq_found = false;
1415 u32 rate;
1416 u32 max_tclk;
1417 u32 min_tclk;
1418
1419 rate = clk_get_rate(pl022->clk);
1420 /* cpsdvscr = 2 & scr 0 */
1421 max_tclk = (rate / (CPSDVR_MIN * (1 + SCR_MIN)));
1422 /* cpsdvsr = 254 & scr = 255 */
1423 min_tclk = (rate / (CPSDVR_MAX * (1 + SCR_MAX)));
1424
1425 if ((freq <= max_tclk) && (freq >= min_tclk)) {
1426 while (cpsdvsr <= CPSDVR_MAX && !freq_found) {
1427 while (scr <= SCR_MAX && !freq_found) {
1428 if ((rate /
1429 (cpsdvsr * (1 + scr))) > freq)
1430 scr += 1;
1431 else {
1432 /*
1433 * This bool is made true when
1434 * effective frequency >=
1435 * target frequency is found
1436 */
1437 freq_found = true;
1438 if ((rate /
1439 (cpsdvsr * (1 + scr))) != freq) {
1440 if (scr == SCR_MIN) {
1441 cpsdvsr -= 2;
1442 scr = SCR_MAX;
1443 } else
1444 scr -= 1;
1445 }
1446 }
1447 }
1448 if (!freq_found) {
1449 cpsdvsr += 2;
1450 scr = SCR_MIN;
1451 }
1452 }
1453 if (cpsdvsr != 0) {
1454 dev_dbg(&pl022->adev->dev,
1455 "SSP Effective Frequency is %u\n",
1456 (rate / (cpsdvsr * (1 + scr))));
1457 clk_freq->cpsdvsr = (u8) (cpsdvsr & 0xFF);
1458 clk_freq->scr = (u8) (scr & 0xFF);
1459 dev_dbg(&pl022->adev->dev,
1460 "SSP cpsdvsr = %d, scr = %d\n",
1461 clk_freq->cpsdvsr, clk_freq->scr);
1462 }
1463 } else {
1464 dev_err(&pl022->adev->dev,
1465 "controller data is incorrect: out of range frequency");
1466 return -EINVAL;
1467 }
1468 return 0;
1469}
1470
1471/**
1472 * NOT IMPLEMENTED
1473 * process_dma_info - Processes the DMA info provided by client drivers
1474 * @chip_info: chip info provided by client device
1475 * @chip: Runtime state maintained by the SSP controller for each spi device
1476 *
1477 * This function processes and stores DMA config provided by client driver
1478 * into the runtime state maintained by the SSP controller driver
1479 */
1480static int process_dma_info(struct pl022_config_chip *chip_info,
1481 struct chip_data *chip)
1482{
1483 dev_err(chip_info->dev,
1484 "cannot process DMA info, DMA not implemented!\n");
1485 return -ENOTSUPP;
1486}
1487
1488/**
1489 * pl022_setup - setup function registered to SPI master framework
1490 * @spi: spi device which is requesting setup
1491 *
1492 * This function is registered to the SPI framework for this SPI master
1493 * controller. If it is the first time when setup is called by this device,
1494 * this function will initialize the runtime state for this chip and save
1495 * the same in the device structure. Else it will update the runtime info
1496 * with the updated chip info. Nothing is really being written to the
1497 * controller hardware here, that is not done until the actual transfer
1498 * commence.
1499 */
1500
1501/* FIXME: JUST GUESSING the spi->mode bits understood by this driver */
1502#define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
1503 | SPI_LSB_FIRST | SPI_LOOP)
1504
1505static int pl022_setup(struct spi_device *spi)
1506{
1507 struct pl022_config_chip *chip_info;
1508 struct chip_data *chip;
1509 int status = 0;
1510 struct pl022 *pl022 = spi_master_get_devdata(spi->master);
1511
1512 if (spi->mode & ~MODEBITS) {
1513 dev_dbg(&spi->dev, "unsupported mode bits %x\n",
1514 spi->mode & ~MODEBITS);
1515 return -EINVAL;
1516 }
1517
1518 if (!spi->max_speed_hz)
1519 return -EINVAL;
1520
1521 /* Get controller_state if one is supplied */
1522 chip = spi_get_ctldata(spi);
1523
1524 if (chip == NULL) {
1525 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1526 if (!chip) {
1527 dev_err(&spi->dev,
1528 "cannot allocate controller state\n");
1529 return -ENOMEM;
1530 }
1531 dev_dbg(&spi->dev,
1532 "allocated memory for controller's runtime state\n");
1533 }
1534
1535 /* Get controller data if one is supplied */
1536 chip_info = spi->controller_data;
1537
1538 if (chip_info == NULL) {
1539 /* spi_board_info.controller_data not is supplied */
1540 dev_dbg(&spi->dev,
1541 "using default controller_data settings\n");
1542
1543 chip_info =
1544 kzalloc(sizeof(struct pl022_config_chip), GFP_KERNEL);
1545
1546 if (!chip_info) {
1547 dev_err(&spi->dev,
1548 "cannot allocate controller data\n");
1549 status = -ENOMEM;
1550 goto err_first_setup;
1551 }
1552
1553 dev_dbg(&spi->dev, "allocated memory for controller data\n");
1554
1555 /* Pointer back to the SPI device */
1556 chip_info->dev = &spi->dev;
1557 /*
1558 * Set controller data default values:
1559 * Polling is supported by default
1560 */
1561 chip_info->lbm = LOOPBACK_DISABLED;
1562 chip_info->com_mode = POLLING_TRANSFER;
1563 chip_info->iface = SSP_INTERFACE_MOTOROLA_SPI;
1564 chip_info->hierarchy = SSP_SLAVE;
1565 chip_info->slave_tx_disable = DO_NOT_DRIVE_TX;
1566 chip_info->endian_tx = SSP_TX_LSB;
1567 chip_info->endian_rx = SSP_RX_LSB;
1568 chip_info->data_size = SSP_DATA_BITS_12;
1569 chip_info->rx_lev_trig = SSP_RX_1_OR_MORE_ELEM;
1570 chip_info->tx_lev_trig = SSP_TX_1_OR_MORE_EMPTY_LOC;
Linus Walleijee2b8052009-08-15 15:12:05 +01001571 chip_info->clk_phase = SSP_CLK_SECOND_EDGE;
Linus Walleijb43d65f2009-06-09 08:11:42 +01001572 chip_info->clk_pol = SSP_CLK_POL_IDLE_LOW;
1573 chip_info->ctrl_len = SSP_BITS_8;
1574 chip_info->wait_state = SSP_MWIRE_WAIT_ZERO;
1575 chip_info->duplex = SSP_MICROWIRE_CHANNEL_FULL_DUPLEX;
1576 chip_info->cs_control = null_cs_control;
1577 } else {
1578 dev_dbg(&spi->dev,
1579 "using user supplied controller_data settings\n");
1580 }
1581
1582 /*
1583 * We can override with custom divisors, else we use the board
1584 * frequency setting
1585 */
1586 if ((0 == chip_info->clk_freq.cpsdvsr)
1587 && (0 == chip_info->clk_freq.scr)) {
1588 status = calculate_effective_freq(pl022,
1589 spi->max_speed_hz,
1590 &chip_info->clk_freq);
1591 if (status < 0)
1592 goto err_config_params;
1593 } else {
1594 if ((chip_info->clk_freq.cpsdvsr % 2) != 0)
1595 chip_info->clk_freq.cpsdvsr =
1596 chip_info->clk_freq.cpsdvsr - 1;
1597 }
1598 status = verify_controller_parameters(pl022, chip_info);
1599 if (status) {
1600 dev_err(&spi->dev, "controller data is incorrect");
1601 goto err_config_params;
1602 }
1603 /* Now set controller state based on controller data */
1604 chip->xfer_type = chip_info->com_mode;
1605 chip->cs_control = chip_info->cs_control;
1606
1607 if (chip_info->data_size <= 8) {
1608 dev_dbg(&spi->dev, "1 <= n <=8 bits per word\n");
1609 chip->n_bytes = 1;
1610 chip->read = READING_U8;
1611 chip->write = WRITING_U8;
1612 } else if (chip_info->data_size <= 16) {
1613 dev_dbg(&spi->dev, "9 <= n <= 16 bits per word\n");
1614 chip->n_bytes = 2;
1615 chip->read = READING_U16;
1616 chip->write = WRITING_U16;
1617 } else {
1618 if (pl022->vendor->max_bpw >= 32) {
1619 dev_dbg(&spi->dev, "17 <= n <= 32 bits per word\n");
1620 chip->n_bytes = 4;
1621 chip->read = READING_U32;
1622 chip->write = WRITING_U32;
1623 } else {
1624 dev_err(&spi->dev,
1625 "illegal data size for this controller!\n");
1626 dev_err(&spi->dev,
1627 "a standard pl022 can only handle "
1628 "1 <= n <= 16 bit words\n");
1629 goto err_config_params;
1630 }
1631 }
1632
1633 /* Now Initialize all register settings required for this chip */
1634 chip->cr0 = 0;
1635 chip->cr1 = 0;
1636 chip->dmacr = 0;
1637 chip->cpsr = 0;
1638 if ((chip_info->com_mode == DMA_TRANSFER)
1639 && ((pl022->master_info)->enable_dma)) {
1640 chip->enable_dma = 1;
1641 dev_dbg(&spi->dev, "DMA mode set in controller state\n");
1642 status = process_dma_info(chip_info, chip);
1643 if (status < 0)
1644 goto err_config_params;
1645 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
1646 SSP_DMACR_MASK_RXDMAE, 0);
1647 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
1648 SSP_DMACR_MASK_TXDMAE, 1);
1649 } else {
1650 chip->enable_dma = 0;
1651 dev_dbg(&spi->dev, "DMA mode NOT set in controller state\n");
1652 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
1653 SSP_DMACR_MASK_RXDMAE, 0);
1654 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
1655 SSP_DMACR_MASK_TXDMAE, 1);
1656 }
1657
1658 chip->cpsr = chip_info->clk_freq.cpsdvsr;
1659
Linus Walleij556f4ae2010-05-05 09:28:15 +00001660 /* Special setup for the ST micro extended control registers */
1661 if (pl022->vendor->extended_cr) {
Linus Walleij781c7b12010-05-07 08:40:53 +00001662 if (pl022->vendor->pl023) {
1663 /* These bits are only in the PL023 */
1664 SSP_WRITE_BITS(chip->cr1, chip_info->clkdelay,
1665 SSP_CR1_MASK_FBCLKDEL_ST, 13);
1666 } else {
1667 /* These bits are in the PL022 but not PL023 */
1668 SSP_WRITE_BITS(chip->cr0, chip_info->duplex,
1669 SSP_CR0_MASK_HALFDUP_ST, 5);
1670 SSP_WRITE_BITS(chip->cr0, chip_info->ctrl_len,
1671 SSP_CR0_MASK_CSS_ST, 16);
1672 SSP_WRITE_BITS(chip->cr0, chip_info->iface,
1673 SSP_CR0_MASK_FRF_ST, 21);
1674 SSP_WRITE_BITS(chip->cr1, chip_info->wait_state,
1675 SSP_CR1_MASK_MWAIT_ST, 6);
1676 }
Linus Walleij556f4ae2010-05-05 09:28:15 +00001677 SSP_WRITE_BITS(chip->cr0, chip_info->data_size,
1678 SSP_CR0_MASK_DSS_ST, 0);
Linus Walleij556f4ae2010-05-05 09:28:15 +00001679 SSP_WRITE_BITS(chip->cr1, chip_info->endian_rx,
1680 SSP_CR1_MASK_RENDN_ST, 4);
1681 SSP_WRITE_BITS(chip->cr1, chip_info->endian_tx,
1682 SSP_CR1_MASK_TENDN_ST, 5);
Linus Walleij556f4ae2010-05-05 09:28:15 +00001683 SSP_WRITE_BITS(chip->cr1, chip_info->rx_lev_trig,
1684 SSP_CR1_MASK_RXIFLSEL_ST, 7);
1685 SSP_WRITE_BITS(chip->cr1, chip_info->tx_lev_trig,
1686 SSP_CR1_MASK_TXIFLSEL_ST, 10);
1687 } else {
1688 SSP_WRITE_BITS(chip->cr0, chip_info->data_size,
1689 SSP_CR0_MASK_DSS, 0);
1690 SSP_WRITE_BITS(chip->cr0, chip_info->iface,
1691 SSP_CR0_MASK_FRF, 4);
1692 }
1693 /* Stuff that is common for all versions */
Linus Walleijb43d65f2009-06-09 08:11:42 +01001694 SSP_WRITE_BITS(chip->cr0, chip_info->clk_pol, SSP_CR0_MASK_SPO, 6);
1695 SSP_WRITE_BITS(chip->cr0, chip_info->clk_phase, SSP_CR0_MASK_SPH, 7);
1696 SSP_WRITE_BITS(chip->cr0, chip_info->clk_freq.scr, SSP_CR0_MASK_SCR, 8);
Linus Walleij781c7b12010-05-07 08:40:53 +00001697 /* Loopback is available on all versions except PL023 */
1698 if (!pl022->vendor->pl023)
1699 SSP_WRITE_BITS(chip->cr1, chip_info->lbm, SSP_CR1_MASK_LBM, 0);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001700 SSP_WRITE_BITS(chip->cr1, SSP_DISABLED, SSP_CR1_MASK_SSE, 1);
1701 SSP_WRITE_BITS(chip->cr1, chip_info->hierarchy, SSP_CR1_MASK_MS, 2);
1702 SSP_WRITE_BITS(chip->cr1, chip_info->slave_tx_disable, SSP_CR1_MASK_SOD, 3);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001703
1704 /* Save controller_state */
1705 spi_set_ctldata(spi, chip);
1706 return status;
1707 err_config_params:
1708 err_first_setup:
1709 kfree(chip);
1710 return status;
1711}
1712
1713/**
1714 * pl022_cleanup - cleanup function registered to SPI master framework
1715 * @spi: spi device which is requesting cleanup
1716 *
1717 * This function is registered to the SPI framework for this SPI master
1718 * controller. It will free the runtime state of chip.
1719 */
1720static void pl022_cleanup(struct spi_device *spi)
1721{
1722 struct chip_data *chip = spi_get_ctldata(spi);
1723
1724 spi_set_ctldata(spi, NULL);
1725 kfree(chip);
1726}
1727
1728
Kevin Wellsb4225882010-07-27 16:39:30 +00001729static int __devinit
Linus Walleijb43d65f2009-06-09 08:11:42 +01001730pl022_probe(struct amba_device *adev, struct amba_id *id)
1731{
1732 struct device *dev = &adev->dev;
1733 struct pl022_ssp_controller *platform_info = adev->dev.platform_data;
1734 struct spi_master *master;
1735 struct pl022 *pl022 = NULL; /*Data for this driver */
1736 int status = 0;
1737
1738 dev_info(&adev->dev,
1739 "ARM PL022 driver, device ID: 0x%08x\n", adev->periphid);
1740 if (platform_info == NULL) {
1741 dev_err(&adev->dev, "probe - no platform data supplied\n");
1742 status = -ENODEV;
1743 goto err_no_pdata;
1744 }
1745
1746 /* Allocate master with space for data */
1747 master = spi_alloc_master(dev, sizeof(struct pl022));
1748 if (master == NULL) {
1749 dev_err(&adev->dev, "probe - cannot alloc SPI master\n");
1750 status = -ENOMEM;
1751 goto err_no_master;
1752 }
1753
1754 pl022 = spi_master_get_devdata(master);
1755 pl022->master = master;
1756 pl022->master_info = platform_info;
1757 pl022->adev = adev;
1758 pl022->vendor = id->data;
1759
1760 /*
1761 * Bus Number Which has been Assigned to this SSP controller
1762 * on this board
1763 */
1764 master->bus_num = platform_info->bus_id;
1765 master->num_chipselect = platform_info->num_chipselect;
1766 master->cleanup = pl022_cleanup;
1767 master->setup = pl022_setup;
1768 master->transfer = pl022_transfer;
1769
1770 dev_dbg(&adev->dev, "BUSNO: %d\n", master->bus_num);
1771
1772 status = amba_request_regions(adev, NULL);
1773 if (status)
1774 goto err_no_ioregion;
1775
1776 pl022->virtbase = ioremap(adev->res.start, resource_size(&adev->res));
1777 if (pl022->virtbase == NULL) {
1778 status = -ENOMEM;
1779 goto err_no_ioremap;
1780 }
1781 printk(KERN_INFO "pl022: mapped registers from 0x%08x to %p\n",
1782 adev->res.start, pl022->virtbase);
1783
1784 pl022->clk = clk_get(&adev->dev, NULL);
1785 if (IS_ERR(pl022->clk)) {
1786 status = PTR_ERR(pl022->clk);
1787 dev_err(&adev->dev, "could not retrieve SSP/SPI bus clock\n");
1788 goto err_no_clk;
1789 }
1790
1791 /* Disable SSP */
Linus Walleijb43d65f2009-06-09 08:11:42 +01001792 writew((readw(SSP_CR1(pl022->virtbase)) & (~SSP_CR1_MASK_SSE)),
1793 SSP_CR1(pl022->virtbase));
1794 load_ssp_default_config(pl022);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001795
1796 status = request_irq(adev->irq[0], pl022_interrupt_handler, 0, "pl022",
1797 pl022);
1798 if (status < 0) {
1799 dev_err(&adev->dev, "probe - cannot get IRQ (%d)\n", status);
1800 goto err_no_irq;
1801 }
1802 /* Initialize and start queue */
1803 status = init_queue(pl022);
1804 if (status != 0) {
1805 dev_err(&adev->dev, "probe - problem initializing queue\n");
1806 goto err_init_queue;
1807 }
1808 status = start_queue(pl022);
1809 if (status != 0) {
1810 dev_err(&adev->dev, "probe - problem starting queue\n");
1811 goto err_start_queue;
1812 }
1813 /* Register with the SPI framework */
1814 amba_set_drvdata(adev, pl022);
1815 status = spi_register_master(master);
1816 if (status != 0) {
1817 dev_err(&adev->dev,
1818 "probe - problem registering spi master\n");
1819 goto err_spi_register;
1820 }
1821 dev_dbg(dev, "probe succeded\n");
Linus Walleij545074f2010-08-21 11:07:36 +02001822 /* Disable the silicon block pclk and clock it when needed */
1823 amba_pclk_disable(adev);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001824 return 0;
1825
1826 err_spi_register:
1827 err_start_queue:
1828 err_init_queue:
1829 destroy_queue(pl022);
1830 free_irq(adev->irq[0], pl022);
1831 err_no_irq:
1832 clk_put(pl022->clk);
1833 err_no_clk:
1834 iounmap(pl022->virtbase);
1835 err_no_ioremap:
1836 amba_release_regions(adev);
1837 err_no_ioregion:
1838 spi_master_put(master);
1839 err_no_master:
1840 err_no_pdata:
1841 return status;
1842}
1843
Kevin Wellsb4225882010-07-27 16:39:30 +00001844static int __devexit
Linus Walleijb43d65f2009-06-09 08:11:42 +01001845pl022_remove(struct amba_device *adev)
1846{
1847 struct pl022 *pl022 = amba_get_drvdata(adev);
1848 int status = 0;
1849 if (!pl022)
1850 return 0;
1851
1852 /* Remove the queue */
1853 status = destroy_queue(pl022);
1854 if (status != 0) {
1855 dev_err(&adev->dev,
1856 "queue remove failed (%d)\n", status);
1857 return status;
1858 }
1859 load_ssp_default_config(pl022);
1860 free_irq(adev->irq[0], pl022);
1861 clk_disable(pl022->clk);
1862 clk_put(pl022->clk);
1863 iounmap(pl022->virtbase);
1864 amba_release_regions(adev);
1865 tasklet_disable(&pl022->pump_transfers);
1866 spi_unregister_master(pl022->master);
1867 spi_master_put(pl022->master);
1868 amba_set_drvdata(adev, NULL);
1869 dev_dbg(&adev->dev, "remove succeded\n");
1870 return 0;
1871}
1872
1873#ifdef CONFIG_PM
1874static int pl022_suspend(struct amba_device *adev, pm_message_t state)
1875{
1876 struct pl022 *pl022 = amba_get_drvdata(adev);
1877 int status = 0;
1878
1879 status = stop_queue(pl022);
1880 if (status) {
1881 dev_warn(&adev->dev, "suspend cannot stop queue\n");
1882 return status;
1883 }
1884
Linus Walleij545074f2010-08-21 11:07:36 +02001885 amba_pclk_enable(adev);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001886 load_ssp_default_config(pl022);
Linus Walleij545074f2010-08-21 11:07:36 +02001887 amba_pclk_disable(adev);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001888 dev_dbg(&adev->dev, "suspended\n");
1889 return 0;
1890}
1891
1892static int pl022_resume(struct amba_device *adev)
1893{
1894 struct pl022 *pl022 = amba_get_drvdata(adev);
1895 int status = 0;
1896
1897 /* Start the queue running */
1898 status = start_queue(pl022);
1899 if (status)
1900 dev_err(&adev->dev, "problem starting queue (%d)\n", status);
1901 else
1902 dev_dbg(&adev->dev, "resumed\n");
1903
1904 return status;
1905}
1906#else
1907#define pl022_suspend NULL
1908#define pl022_resume NULL
1909#endif /* CONFIG_PM */
1910
1911static struct vendor_data vendor_arm = {
1912 .fifodepth = 8,
1913 .max_bpw = 16,
1914 .unidir = false,
Linus Walleij556f4ae2010-05-05 09:28:15 +00001915 .extended_cr = false,
Linus Walleij781c7b12010-05-07 08:40:53 +00001916 .pl023 = false,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001917};
1918
1919
1920static struct vendor_data vendor_st = {
1921 .fifodepth = 32,
1922 .max_bpw = 32,
1923 .unidir = false,
Linus Walleij556f4ae2010-05-05 09:28:15 +00001924 .extended_cr = true,
Linus Walleij781c7b12010-05-07 08:40:53 +00001925 .pl023 = false,
1926};
1927
1928static struct vendor_data vendor_st_pl023 = {
1929 .fifodepth = 32,
1930 .max_bpw = 32,
1931 .unidir = false,
1932 .extended_cr = true,
1933 .pl023 = true,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001934};
1935
1936static struct amba_id pl022_ids[] = {
1937 {
1938 /*
1939 * ARM PL022 variant, this has a 16bit wide
1940 * and 8 locations deep TX/RX FIFO
1941 */
1942 .id = 0x00041022,
1943 .mask = 0x000fffff,
1944 .data = &vendor_arm,
1945 },
1946 {
1947 /*
1948 * ST Micro derivative, this has 32bit wide
1949 * and 32 locations deep TX/RX FIFO
1950 */
Srinidhi Kasagare89e04f2009-10-05 06:13:53 +01001951 .id = 0x01080022,
Linus Walleijb43d65f2009-06-09 08:11:42 +01001952 .mask = 0xffffffff,
1953 .data = &vendor_st,
1954 },
Linus Walleij781c7b12010-05-07 08:40:53 +00001955 {
1956 /*
1957 * ST-Ericsson derivative "PL023" (this is not
1958 * an official ARM number), this is a PL022 SSP block
1959 * stripped to SPI mode only, it has 32bit wide
1960 * and 32 locations deep TX/RX FIFO but no extended
1961 * CR0/CR1 register
1962 */
1963 .id = 0x00080023,
1964 .mask = 0xffffffff,
1965 .data = &vendor_st_pl023,
1966 },
Linus Walleijb43d65f2009-06-09 08:11:42 +01001967 { 0, 0 },
1968};
1969
1970static struct amba_driver pl022_driver = {
1971 .drv = {
1972 .name = "ssp-pl022",
1973 },
1974 .id_table = pl022_ids,
1975 .probe = pl022_probe,
Kevin Wellsb4225882010-07-27 16:39:30 +00001976 .remove = __devexit_p(pl022_remove),
Linus Walleijb43d65f2009-06-09 08:11:42 +01001977 .suspend = pl022_suspend,
1978 .resume = pl022_resume,
1979};
1980
1981
1982static int __init pl022_init(void)
1983{
1984 return amba_driver_register(&pl022_driver);
1985}
1986
Linus Walleij25c8e032010-09-06 11:02:12 +02001987subsys_initcall(pl022_init);
Linus Walleijb43d65f2009-06-09 08:11:42 +01001988
1989static void __exit pl022_exit(void)
1990{
1991 amba_driver_unregister(&pl022_driver);
1992}
1993
1994module_exit(pl022_exit);
1995
1996MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
1997MODULE_DESCRIPTION("PL022 SSP Controller Driver");
1998MODULE_LICENSE("GPL");