x86, ioapic: Fix io_apic_redir_entries to return the number of entries.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / spi / amba-pl022.c
blobe9aeee16d922aba598f731e3cbd61b48e6d5b63c
1 /*
2 * drivers/spi/amba-pl022.c
4 * A driver for the ARM PL022 PrimeCell SSP/SPI bus master.
6 * Copyright (C) 2008-2009 ST-Ericsson AB
7 * Copyright (C) 2006 STMicroelectronics Pvt. Ltd.
9 * Author: Linus Walleij <linus.walleij@stericsson.com>
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>
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.
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.
28 * TODO:
29 * - add timeout on polled transfers
30 * - add generic DMA framework support
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>
41 #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>
47 #include <linux/slab.h>
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.
54 #define SSP_WRITE_BITS(reg, val, mask, sb) \
55 ((reg) = (((reg) & ~(mask)) | (((val)<<(sb)) & (mask))))
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.
62 #define GEN_MASK_BITS(val, mask, sb) \
63 (((val)<<(sb)) & (mask))
65 #define DRIVE_TX 0
66 #define DO_NOT_DRIVE_TX 1
68 #define DO_NOT_QUEUE_DMA 0
69 #define QUEUE_DMA 1
71 #define RX_TRANSFER 1
72 #define TX_TRANSFER 2
75 * Macros to access SSP Registers with their offsets
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)
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)
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)
103 * SSP Control Register 0 - SSP_CR0
105 #define SSP_CR0_MASK_DSS (0x1FUL << 0)
106 #define SSP_CR0_MASK_HALFDUP (0x1UL << 5)
107 #define SSP_CR0_MASK_SPO (0x1UL << 6)
108 #define SSP_CR0_MASK_SPH (0x1UL << 7)
109 #define SSP_CR0_MASK_SCR (0xFFUL << 8)
110 #define SSP_CR0_MASK_CSS (0x1FUL << 16)
111 #define SSP_CR0_MASK_FRF (0x3UL << 21)
114 * SSP Control Register 0 - SSP_CR1
116 #define SSP_CR1_MASK_LBM (0x1UL << 0)
117 #define SSP_CR1_MASK_SSE (0x1UL << 1)
118 #define SSP_CR1_MASK_MS (0x1UL << 2)
119 #define SSP_CR1_MASK_SOD (0x1UL << 3)
120 #define SSP_CR1_MASK_RENDN (0x1UL << 4)
121 #define SSP_CR1_MASK_TENDN (0x1UL << 5)
122 #define SSP_CR1_MASK_MWAIT (0x1UL << 6)
123 #define SSP_CR1_MASK_RXIFLSEL (0x7UL << 7)
124 #define SSP_CR1_MASK_TXIFLSEL (0x7UL << 10)
127 * SSP Data Register - SSP_DR
129 #define SSP_DR_MASK_DATA 0xFFFFFFFF
132 * SSP Status Register - SSP_SR
134 #define SSP_SR_MASK_TFE (0x1UL << 0) /* Transmit FIFO empty */
135 #define SSP_SR_MASK_TNF (0x1UL << 1) /* Transmit FIFO not full */
136 #define SSP_SR_MASK_RNE (0x1UL << 2) /* Receive FIFO not empty */
137 #define SSP_SR_MASK_RFF (0x1UL << 3) /* Receive FIFO full */
138 #define SSP_SR_MASK_BSY (0x1UL << 4) /* Busy Flag */
141 * SSP Clock Prescale Register - SSP_CPSR
143 #define SSP_CPSR_MASK_CPSDVSR (0xFFUL << 0)
146 * SSP Interrupt Mask Set/Clear Register - SSP_IMSC
148 #define SSP_IMSC_MASK_RORIM (0x1UL << 0) /* Receive Overrun Interrupt mask */
149 #define SSP_IMSC_MASK_RTIM (0x1UL << 1) /* Receive timeout Interrupt mask */
150 #define SSP_IMSC_MASK_RXIM (0x1UL << 2) /* Receive FIFO Interrupt mask */
151 #define SSP_IMSC_MASK_TXIM (0x1UL << 3) /* Transmit FIFO Interrupt mask */
154 * SSP Raw Interrupt Status Register - SSP_RIS
156 /* Receive Overrun Raw Interrupt status */
157 #define SSP_RIS_MASK_RORRIS (0x1UL << 0)
158 /* Receive Timeout Raw Interrupt status */
159 #define SSP_RIS_MASK_RTRIS (0x1UL << 1)
160 /* Receive FIFO Raw Interrupt status */
161 #define SSP_RIS_MASK_RXRIS (0x1UL << 2)
162 /* Transmit FIFO Raw Interrupt status */
163 #define SSP_RIS_MASK_TXRIS (0x1UL << 3)
166 * SSP Masked Interrupt Status Register - SSP_MIS
168 /* Receive Overrun Masked Interrupt status */
169 #define SSP_MIS_MASK_RORMIS (0x1UL << 0)
170 /* Receive Timeout Masked Interrupt status */
171 #define SSP_MIS_MASK_RTMIS (0x1UL << 1)
172 /* Receive FIFO Masked Interrupt status */
173 #define SSP_MIS_MASK_RXMIS (0x1UL << 2)
174 /* Transmit FIFO Masked Interrupt status */
175 #define SSP_MIS_MASK_TXMIS (0x1UL << 3)
178 * SSP Interrupt Clear Register - SSP_ICR
180 /* Receive Overrun Raw Clear Interrupt bit */
181 #define SSP_ICR_MASK_RORIC (0x1UL << 0)
182 /* Receive Timeout Clear Interrupt bit */
183 #define SSP_ICR_MASK_RTIC (0x1UL << 1)
186 * SSP DMA Control Register - SSP_DMACR
188 /* Receive DMA Enable bit */
189 #define SSP_DMACR_MASK_RXDMAE (0x1UL << 0)
190 /* Transmit DMA Enable bit */
191 #define SSP_DMACR_MASK_TXDMAE (0x1UL << 1)
194 * SSP Integration Test control Register - SSP_ITCR
196 #define SSP_ITCR_MASK_ITEN (0x1UL << 0)
197 #define SSP_ITCR_MASK_TESTFIFO (0x1UL << 1)
200 * SSP Integration Test Input Register - SSP_ITIP
202 #define ITIP_MASK_SSPRXD (0x1UL << 0)
203 #define ITIP_MASK_SSPFSSIN (0x1UL << 1)
204 #define ITIP_MASK_SSPCLKIN (0x1UL << 2)
205 #define ITIP_MASK_RXDMAC (0x1UL << 3)
206 #define ITIP_MASK_TXDMAC (0x1UL << 4)
207 #define ITIP_MASK_SSPTXDIN (0x1UL << 5)
210 * SSP Integration Test output Register - SSP_ITOP
212 #define ITOP_MASK_SSPTXD (0x1UL << 0)
213 #define ITOP_MASK_SSPFSSOUT (0x1UL << 1)
214 #define ITOP_MASK_SSPCLKOUT (0x1UL << 2)
215 #define ITOP_MASK_SSPOEn (0x1UL << 3)
216 #define ITOP_MASK_SSPCTLOEn (0x1UL << 4)
217 #define ITOP_MASK_RORINTR (0x1UL << 5)
218 #define ITOP_MASK_RTINTR (0x1UL << 6)
219 #define ITOP_MASK_RXINTR (0x1UL << 7)
220 #define ITOP_MASK_TXINTR (0x1UL << 8)
221 #define ITOP_MASK_INTR (0x1UL << 9)
222 #define ITOP_MASK_RXDMABREQ (0x1UL << 10)
223 #define ITOP_MASK_RXDMASREQ (0x1UL << 11)
224 #define ITOP_MASK_TXDMABREQ (0x1UL << 12)
225 #define ITOP_MASK_TXDMASREQ (0x1UL << 13)
228 * SSP Test Data Register - SSP_TDR
230 #define TDR_MASK_TESTDATA (0xFFFFFFFF)
233 * Message State
234 * we use the spi_message.state (void *) pointer to
235 * hold a single state value, that's why all this
236 * (void *) casting is done here.
238 #define STATE_START ((void *) 0)
239 #define STATE_RUNNING ((void *) 1)
240 #define STATE_DONE ((void *) 2)
241 #define STATE_ERROR ((void *) -1)
244 * Queue State
246 #define QUEUE_RUNNING (0)
247 #define QUEUE_STOPPED (1)
249 * SSP State - Whether Enabled or Disabled
251 #define SSP_DISABLED (0)
252 #define SSP_ENABLED (1)
255 * SSP DMA State - Whether DMA Enabled or Disabled
257 #define SSP_DMA_DISABLED (0)
258 #define SSP_DMA_ENABLED (1)
261 * SSP Clock Defaults
263 #define NMDK_SSP_DEFAULT_CLKRATE 0x2
264 #define NMDK_SSP_DEFAULT_PRESCALE 0x40
267 * SSP Clock Parameter ranges
269 #define CPSDVR_MIN 0x02
270 #define CPSDVR_MAX 0xFE
271 #define SCR_MIN 0x00
272 #define SCR_MAX 0xFF
275 * SSP Interrupt related Macros
277 #define DEFAULT_SSP_REG_IMSC 0x0UL
278 #define DISABLE_ALL_INTERRUPTS DEFAULT_SSP_REG_IMSC
279 #define ENABLE_ALL_INTERRUPTS (~DEFAULT_SSP_REG_IMSC)
281 #define CLEAR_ALL_INTERRUPTS 0x3
285 * The type of reading going on on this chip
287 enum ssp_reading {
288 READING_NULL,
289 READING_U8,
290 READING_U16,
291 READING_U32
295 * The type of writing going on on this chip
297 enum ssp_writing {
298 WRITING_NULL,
299 WRITING_U8,
300 WRITING_U16,
301 WRITING_U32
305 * struct vendor_data - vendor-specific config parameters
306 * for PL022 derivates
307 * @fifodepth: depth of FIFOs (both)
308 * @max_bpw: maximum number of bits per word
309 * @unidir: supports unidirection transfers
311 struct vendor_data {
312 int fifodepth;
313 int max_bpw;
314 bool unidir;
318 * struct pl022 - This is the private SSP driver data structure
319 * @adev: AMBA device model hookup
320 * @phybase: The physical memory where the SSP device resides
321 * @virtbase: The virtual memory where the SSP is mapped
322 * @master: SPI framework hookup
323 * @master_info: controller-specific data from machine setup
324 * @regs: SSP controller register's virtual address
325 * @pump_messages: Work struct for scheduling work to the workqueue
326 * @lock: spinlock to syncronise access to driver data
327 * @workqueue: a workqueue on which any spi_message request is queued
328 * @busy: workqueue is busy
329 * @run: workqueue is running
330 * @pump_transfers: Tasklet used in Interrupt Transfer mode
331 * @cur_msg: Pointer to current spi_message being processed
332 * @cur_transfer: Pointer to current spi_transfer
333 * @cur_chip: pointer to current clients chip(assigned from controller_state)
334 * @tx: current position in TX buffer to be read
335 * @tx_end: end position in TX buffer to be read
336 * @rx: current position in RX buffer to be written
337 * @rx_end: end position in RX buffer to be written
338 * @readingtype: the type of read currently going on
339 * @writingtype: the type or write currently going on
341 struct pl022 {
342 struct amba_device *adev;
343 struct vendor_data *vendor;
344 resource_size_t phybase;
345 void __iomem *virtbase;
346 struct clk *clk;
347 struct spi_master *master;
348 struct pl022_ssp_controller *master_info;
349 /* Driver message queue */
350 struct workqueue_struct *workqueue;
351 struct work_struct pump_messages;
352 spinlock_t queue_lock;
353 struct list_head queue;
354 int busy;
355 int run;
356 /* Message transfer pump */
357 struct tasklet_struct pump_transfers;
358 struct spi_message *cur_msg;
359 struct spi_transfer *cur_transfer;
360 struct chip_data *cur_chip;
361 void *tx;
362 void *tx_end;
363 void *rx;
364 void *rx_end;
365 enum ssp_reading read;
366 enum ssp_writing write;
367 u32 exp_fifo_level;
371 * struct chip_data - To maintain runtime state of SSP for each client chip
372 * @cr0: Value of control register CR0 of SSP
373 * @cr1: Value of control register CR1 of SSP
374 * @dmacr: Value of DMA control Register of SSP
375 * @cpsr: Value of Clock prescale register
376 * @n_bytes: how many bytes(power of 2) reqd for a given data width of client
377 * @enable_dma: Whether to enable DMA or not
378 * @write: function ptr to be used to write when doing xfer for this chip
379 * @read: function ptr to be used to read when doing xfer for this chip
380 * @cs_control: chip select callback provided by chip
381 * @xfer_type: polling/interrupt/DMA
383 * Runtime state of the SSP controller, maintained per chip,
384 * This would be set according to the current message that would be served
386 struct chip_data {
387 u16 cr0;
388 u16 cr1;
389 u16 dmacr;
390 u16 cpsr;
391 u8 n_bytes;
392 u8 enable_dma:1;
393 enum ssp_reading read;
394 enum ssp_writing write;
395 void (*cs_control) (u32 command);
396 int xfer_type;
400 * null_cs_control - Dummy chip select function
401 * @command: select/delect the chip
403 * If no chip select function is provided by client this is used as dummy
404 * chip select
406 static void null_cs_control(u32 command)
408 pr_debug("pl022: dummy chip select control, CS=0x%x\n", command);
412 * giveback - current spi_message is over, schedule next message and call
413 * callback of this message. Assumes that caller already
414 * set message->status; dma and pio irqs are blocked
415 * @pl022: SSP driver private data structure
417 static void giveback(struct pl022 *pl022)
419 struct spi_transfer *last_transfer;
420 unsigned long flags;
421 struct spi_message *msg;
422 void (*curr_cs_control) (u32 command);
425 * This local reference to the chip select function
426 * is needed because we set curr_chip to NULL
427 * as a step toward termininating the message.
429 curr_cs_control = pl022->cur_chip->cs_control;
430 spin_lock_irqsave(&pl022->queue_lock, flags);
431 msg = pl022->cur_msg;
432 pl022->cur_msg = NULL;
433 pl022->cur_transfer = NULL;
434 pl022->cur_chip = NULL;
435 queue_work(pl022->workqueue, &pl022->pump_messages);
436 spin_unlock_irqrestore(&pl022->queue_lock, flags);
438 last_transfer = list_entry(msg->transfers.prev,
439 struct spi_transfer,
440 transfer_list);
442 /* Delay if requested before any change in chip select */
443 if (last_transfer->delay_usecs)
445 * FIXME: This runs in interrupt context.
446 * Is this really smart?
448 udelay(last_transfer->delay_usecs);
451 * Drop chip select UNLESS cs_change is true or we are returning
452 * a message with an error, or next message is for another chip
454 if (!last_transfer->cs_change)
455 curr_cs_control(SSP_CHIP_DESELECT);
456 else {
457 struct spi_message *next_msg;
459 /* Holding of cs was hinted, but we need to make sure
460 * the next message is for the same chip. Don't waste
461 * time with the following tests unless this was hinted.
463 * We cannot postpone this until pump_messages, because
464 * after calling msg->complete (below) the driver that
465 * sent the current message could be unloaded, which
466 * could invalidate the cs_control() callback...
469 /* get a pointer to the next message, if any */
470 spin_lock_irqsave(&pl022->queue_lock, flags);
471 if (list_empty(&pl022->queue))
472 next_msg = NULL;
473 else
474 next_msg = list_entry(pl022->queue.next,
475 struct spi_message, queue);
476 spin_unlock_irqrestore(&pl022->queue_lock, flags);
478 /* see if the next and current messages point
479 * to the same chip
481 if (next_msg && next_msg->spi != msg->spi)
482 next_msg = NULL;
483 if (!next_msg || msg->state == STATE_ERROR)
484 curr_cs_control(SSP_CHIP_DESELECT);
486 msg->state = NULL;
487 if (msg->complete)
488 msg->complete(msg->context);
489 /* This message is completed, so let's turn off the clock! */
490 clk_disable(pl022->clk);
494 * flush - flush the FIFO to reach a clean state
495 * @pl022: SSP driver private data structure
497 static int flush(struct pl022 *pl022)
499 unsigned long limit = loops_per_jiffy << 1;
501 dev_dbg(&pl022->adev->dev, "flush\n");
502 do {
503 while (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
504 readw(SSP_DR(pl022->virtbase));
505 } while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_BSY) && limit--);
507 pl022->exp_fifo_level = 0;
509 return limit;
513 * restore_state - Load configuration of current chip
514 * @pl022: SSP driver private data structure
516 static void restore_state(struct pl022 *pl022)
518 struct chip_data *chip = pl022->cur_chip;
520 writew(chip->cr0, SSP_CR0(pl022->virtbase));
521 writew(chip->cr1, SSP_CR1(pl022->virtbase));
522 writew(chip->dmacr, SSP_DMACR(pl022->virtbase));
523 writew(chip->cpsr, SSP_CPSR(pl022->virtbase));
524 writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
525 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
529 * load_ssp_default_config - Load default configuration for SSP
530 * @pl022: SSP driver private data structure
534 * Default SSP Register Values
536 #define DEFAULT_SSP_REG_CR0 ( \
537 GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS, 0) | \
538 GEN_MASK_BITS(SSP_MICROWIRE_CHANNEL_FULL_DUPLEX, SSP_CR0_MASK_HALFDUP, 5) | \
539 GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
540 GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
541 GEN_MASK_BITS(NMDK_SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) | \
542 GEN_MASK_BITS(SSP_BITS_8, SSP_CR0_MASK_CSS, 16) | \
543 GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF, 21) \
546 #define DEFAULT_SSP_REG_CR1 ( \
547 GEN_MASK_BITS(LOOPBACK_DISABLED, SSP_CR1_MASK_LBM, 0) | \
548 GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
549 GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
550 GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) | \
551 GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN, 4) | \
552 GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN, 5) | \
553 GEN_MASK_BITS(SSP_MWIRE_WAIT_ZERO, SSP_CR1_MASK_MWAIT, 6) |\
554 GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL, 7) | \
555 GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL, 10) \
558 #define DEFAULT_SSP_REG_CPSR ( \
559 GEN_MASK_BITS(NMDK_SSP_DEFAULT_PRESCALE, SSP_CPSR_MASK_CPSDVSR, 0) \
562 #define DEFAULT_SSP_REG_DMACR (\
563 GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_RXDMAE, 0) | \
564 GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_TXDMAE, 1) \
568 static void load_ssp_default_config(struct pl022 *pl022)
570 writew(DEFAULT_SSP_REG_CR0, SSP_CR0(pl022->virtbase));
571 writew(DEFAULT_SSP_REG_CR1, SSP_CR1(pl022->virtbase));
572 writew(DEFAULT_SSP_REG_DMACR, SSP_DMACR(pl022->virtbase));
573 writew(DEFAULT_SSP_REG_CPSR, SSP_CPSR(pl022->virtbase));
574 writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
575 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
579 * This will write to TX and read from RX according to the parameters
580 * set in pl022.
582 static void readwriter(struct pl022 *pl022)
586 * The FIFO depth is different inbetween primecell variants.
587 * I believe filling in too much in the FIFO might cause
588 * errons in 8bit wide transfers on ARM variants (just 8 words
589 * FIFO, means only 8x8 = 64 bits in FIFO) at least.
591 * To prevent this issue, the TX FIFO is only filled to the
592 * unused RX FIFO fill length, regardless of what the TX
593 * FIFO status flag indicates.
595 dev_dbg(&pl022->adev->dev,
596 "%s, rx: %p, rxend: %p, tx: %p, txend: %p\n",
597 __func__, pl022->rx, pl022->rx_end, pl022->tx, pl022->tx_end);
599 /* Read as much as you can */
600 while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
601 && (pl022->rx < pl022->rx_end)) {
602 switch (pl022->read) {
603 case READING_NULL:
604 readw(SSP_DR(pl022->virtbase));
605 break;
606 case READING_U8:
607 *(u8 *) (pl022->rx) =
608 readw(SSP_DR(pl022->virtbase)) & 0xFFU;
609 break;
610 case READING_U16:
611 *(u16 *) (pl022->rx) =
612 (u16) readw(SSP_DR(pl022->virtbase));
613 break;
614 case READING_U32:
615 *(u32 *) (pl022->rx) =
616 readl(SSP_DR(pl022->virtbase));
617 break;
619 pl022->rx += (pl022->cur_chip->n_bytes);
620 pl022->exp_fifo_level--;
623 * Write as much as possible up to the RX FIFO size
625 while ((pl022->exp_fifo_level < pl022->vendor->fifodepth)
626 && (pl022->tx < pl022->tx_end)) {
627 switch (pl022->write) {
628 case WRITING_NULL:
629 writew(0x0, SSP_DR(pl022->virtbase));
630 break;
631 case WRITING_U8:
632 writew(*(u8 *) (pl022->tx), SSP_DR(pl022->virtbase));
633 break;
634 case WRITING_U16:
635 writew((*(u16 *) (pl022->tx)), SSP_DR(pl022->virtbase));
636 break;
637 case WRITING_U32:
638 writel(*(u32 *) (pl022->tx), SSP_DR(pl022->virtbase));
639 break;
641 pl022->tx += (pl022->cur_chip->n_bytes);
642 pl022->exp_fifo_level++;
644 * This inner reader takes care of things appearing in the RX
645 * FIFO as we're transmitting. This will happen a lot since the
646 * clock starts running when you put things into the TX FIFO,
647 * and then things are continously clocked into the RX FIFO.
649 while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
650 && (pl022->rx < pl022->rx_end)) {
651 switch (pl022->read) {
652 case READING_NULL:
653 readw(SSP_DR(pl022->virtbase));
654 break;
655 case READING_U8:
656 *(u8 *) (pl022->rx) =
657 readw(SSP_DR(pl022->virtbase)) & 0xFFU;
658 break;
659 case READING_U16:
660 *(u16 *) (pl022->rx) =
661 (u16) readw(SSP_DR(pl022->virtbase));
662 break;
663 case READING_U32:
664 *(u32 *) (pl022->rx) =
665 readl(SSP_DR(pl022->virtbase));
666 break;
668 pl022->rx += (pl022->cur_chip->n_bytes);
669 pl022->exp_fifo_level--;
673 * When we exit here the TX FIFO should be full and the RX FIFO
674 * should be empty
680 * next_transfer - Move to the Next transfer in the current spi message
681 * @pl022: SSP driver private data structure
683 * This function moves though the linked list of spi transfers in the
684 * current spi message and returns with the state of current spi
685 * message i.e whether its last transfer is done(STATE_DONE) or
686 * Next transfer is ready(STATE_RUNNING)
688 static void *next_transfer(struct pl022 *pl022)
690 struct spi_message *msg = pl022->cur_msg;
691 struct spi_transfer *trans = pl022->cur_transfer;
693 /* Move to next transfer */
694 if (trans->transfer_list.next != &msg->transfers) {
695 pl022->cur_transfer =
696 list_entry(trans->transfer_list.next,
697 struct spi_transfer, transfer_list);
698 return STATE_RUNNING;
700 return STATE_DONE;
703 * pl022_interrupt_handler - Interrupt handler for SSP controller
705 * This function handles interrupts generated for an interrupt based transfer.
706 * If a receive overrun (ROR) interrupt is there then we disable SSP, flag the
707 * current message's state as STATE_ERROR and schedule the tasklet
708 * pump_transfers which will do the postprocessing of the current message by
709 * calling giveback(). Otherwise it reads data from RX FIFO till there is no
710 * more data, and writes data in TX FIFO till it is not full. If we complete
711 * the transfer we move to the next transfer and schedule the tasklet.
713 static irqreturn_t pl022_interrupt_handler(int irq, void *dev_id)
715 struct pl022 *pl022 = dev_id;
716 struct spi_message *msg = pl022->cur_msg;
717 u16 irq_status = 0;
718 u16 flag = 0;
720 if (unlikely(!msg)) {
721 dev_err(&pl022->adev->dev,
722 "bad message state in interrupt handler");
723 /* Never fail */
724 return IRQ_HANDLED;
727 /* Read the Interrupt Status Register */
728 irq_status = readw(SSP_MIS(pl022->virtbase));
730 if (unlikely(!irq_status))
731 return IRQ_NONE;
733 /* This handles the error code interrupts */
734 if (unlikely(irq_status & SSP_MIS_MASK_RORMIS)) {
736 * Overrun interrupt - bail out since our Data has been
737 * corrupted
739 dev_err(&pl022->adev->dev,
740 "FIFO overrun\n");
741 if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RFF)
742 dev_err(&pl022->adev->dev,
743 "RXFIFO is full\n");
744 if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_TNF)
745 dev_err(&pl022->adev->dev,
746 "TXFIFO is full\n");
749 * Disable and clear interrupts, disable SSP,
750 * mark message with bad status so it can be
751 * retried.
753 writew(DISABLE_ALL_INTERRUPTS,
754 SSP_IMSC(pl022->virtbase));
755 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
756 writew((readw(SSP_CR1(pl022->virtbase)) &
757 (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
758 msg->state = STATE_ERROR;
760 /* Schedule message queue handler */
761 tasklet_schedule(&pl022->pump_transfers);
762 return IRQ_HANDLED;
765 readwriter(pl022);
767 if ((pl022->tx == pl022->tx_end) && (flag == 0)) {
768 flag = 1;
769 /* Disable Transmit interrupt */
770 writew(readw(SSP_IMSC(pl022->virtbase)) &
771 (~SSP_IMSC_MASK_TXIM),
772 SSP_IMSC(pl022->virtbase));
776 * Since all transactions must write as much as shall be read,
777 * we can conclude the entire transaction once RX is complete.
778 * At this point, all TX will always be finished.
780 if (pl022->rx >= pl022->rx_end) {
781 writew(DISABLE_ALL_INTERRUPTS,
782 SSP_IMSC(pl022->virtbase));
783 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
784 if (unlikely(pl022->rx > pl022->rx_end)) {
785 dev_warn(&pl022->adev->dev, "read %u surplus "
786 "bytes (did you request an odd "
787 "number of bytes on a 16bit bus?)\n",
788 (u32) (pl022->rx - pl022->rx_end));
790 /* Update total bytes transfered */
791 msg->actual_length += pl022->cur_transfer->len;
792 if (pl022->cur_transfer->cs_change)
793 pl022->cur_chip->
794 cs_control(SSP_CHIP_DESELECT);
795 /* Move to next transfer */
796 msg->state = next_transfer(pl022);
797 tasklet_schedule(&pl022->pump_transfers);
798 return IRQ_HANDLED;
801 return IRQ_HANDLED;
805 * This sets up the pointers to memory for the next message to
806 * send out on the SPI bus.
808 static int set_up_next_transfer(struct pl022 *pl022,
809 struct spi_transfer *transfer)
811 int residue;
813 /* Sanity check the message for this bus width */
814 residue = pl022->cur_transfer->len % pl022->cur_chip->n_bytes;
815 if (unlikely(residue != 0)) {
816 dev_err(&pl022->adev->dev,
817 "message of %u bytes to transmit but the current "
818 "chip bus has a data width of %u bytes!\n",
819 pl022->cur_transfer->len,
820 pl022->cur_chip->n_bytes);
821 dev_err(&pl022->adev->dev, "skipping this message\n");
822 return -EIO;
824 pl022->tx = (void *)transfer->tx_buf;
825 pl022->tx_end = pl022->tx + pl022->cur_transfer->len;
826 pl022->rx = (void *)transfer->rx_buf;
827 pl022->rx_end = pl022->rx + pl022->cur_transfer->len;
828 pl022->write =
829 pl022->tx ? pl022->cur_chip->write : WRITING_NULL;
830 pl022->read = pl022->rx ? pl022->cur_chip->read : READING_NULL;
831 return 0;
835 * pump_transfers - Tasklet function which schedules next interrupt transfer
836 * when running in interrupt transfer mode.
837 * @data: SSP driver private data structure
840 static void pump_transfers(unsigned long data)
842 struct pl022 *pl022 = (struct pl022 *) data;
843 struct spi_message *message = NULL;
844 struct spi_transfer *transfer = NULL;
845 struct spi_transfer *previous = NULL;
847 /* Get current state information */
848 message = pl022->cur_msg;
849 transfer = pl022->cur_transfer;
851 /* Handle for abort */
852 if (message->state == STATE_ERROR) {
853 message->status = -EIO;
854 giveback(pl022);
855 return;
858 /* Handle end of message */
859 if (message->state == STATE_DONE) {
860 message->status = 0;
861 giveback(pl022);
862 return;
865 /* Delay if requested at end of transfer before CS change */
866 if (message->state == STATE_RUNNING) {
867 previous = list_entry(transfer->transfer_list.prev,
868 struct spi_transfer,
869 transfer_list);
870 if (previous->delay_usecs)
872 * FIXME: This runs in interrupt context.
873 * Is this really smart?
875 udelay(previous->delay_usecs);
877 /* Drop chip select only if cs_change is requested */
878 if (previous->cs_change)
879 pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
880 } else {
881 /* STATE_START */
882 message->state = STATE_RUNNING;
885 if (set_up_next_transfer(pl022, transfer)) {
886 message->state = STATE_ERROR;
887 message->status = -EIO;
888 giveback(pl022);
889 return;
891 /* Flush the FIFOs and let's go! */
892 flush(pl022);
893 writew(ENABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
897 * NOT IMPLEMENTED
898 * configure_dma - It configures the DMA pipes for DMA transfers
899 * @data: SSP driver's private data structure
902 static int configure_dma(void *data)
904 struct pl022 *pl022 = data;
905 dev_dbg(&pl022->adev->dev, "configure DMA\n");
906 return -ENOTSUPP;
910 * do_dma_transfer - It handles transfers of the current message
911 * if it is DMA xfer.
912 * NOT FULLY IMPLEMENTED
913 * @data: SSP driver's private data structure
915 static void do_dma_transfer(void *data)
917 struct pl022 *pl022 = data;
919 if (configure_dma(data)) {
920 dev_dbg(&pl022->adev->dev, "configuration of DMA Failed!\n");
921 goto err_config_dma;
924 /* TODO: Implememt DMA setup of pipes here */
926 /* Enable target chip, set up transfer */
927 pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
928 if (set_up_next_transfer(pl022, pl022->cur_transfer)) {
929 /* Error path */
930 pl022->cur_msg->state = STATE_ERROR;
931 pl022->cur_msg->status = -EIO;
932 giveback(pl022);
933 return;
935 /* Enable SSP */
936 writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
937 SSP_CR1(pl022->virtbase));
939 /* TODO: Enable the DMA transfer here */
940 return;
942 err_config_dma:
943 pl022->cur_msg->state = STATE_ERROR;
944 pl022->cur_msg->status = -EIO;
945 giveback(pl022);
946 return;
949 static void do_interrupt_transfer(void *data)
951 struct pl022 *pl022 = data;
953 /* Enable target chip */
954 pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
955 if (set_up_next_transfer(pl022, pl022->cur_transfer)) {
956 /* Error path */
957 pl022->cur_msg->state = STATE_ERROR;
958 pl022->cur_msg->status = -EIO;
959 giveback(pl022);
960 return;
962 /* Enable SSP, turn on interrupts */
963 writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
964 SSP_CR1(pl022->virtbase));
965 writew(ENABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
968 static void do_polling_transfer(void *data)
970 struct pl022 *pl022 = data;
971 struct spi_message *message = NULL;
972 struct spi_transfer *transfer = NULL;
973 struct spi_transfer *previous = NULL;
974 struct chip_data *chip;
976 chip = pl022->cur_chip;
977 message = pl022->cur_msg;
979 while (message->state != STATE_DONE) {
980 /* Handle for abort */
981 if (message->state == STATE_ERROR)
982 break;
983 transfer = pl022->cur_transfer;
985 /* Delay if requested at end of transfer */
986 if (message->state == STATE_RUNNING) {
987 previous =
988 list_entry(transfer->transfer_list.prev,
989 struct spi_transfer, transfer_list);
990 if (previous->delay_usecs)
991 udelay(previous->delay_usecs);
992 if (previous->cs_change)
993 pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
994 } else {
995 /* STATE_START */
996 message->state = STATE_RUNNING;
997 pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
1000 /* Configuration Changing Per Transfer */
1001 if (set_up_next_transfer(pl022, transfer)) {
1002 /* Error path */
1003 message->state = STATE_ERROR;
1004 break;
1006 /* Flush FIFOs and enable SSP */
1007 flush(pl022);
1008 writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
1009 SSP_CR1(pl022->virtbase));
1011 dev_dbg(&pl022->adev->dev, "POLLING TRANSFER ONGOING ... \n");
1012 /* FIXME: insert a timeout so we don't hang here indefinately */
1013 while (pl022->tx < pl022->tx_end || pl022->rx < pl022->rx_end)
1014 readwriter(pl022);
1016 /* Update total byte transfered */
1017 message->actual_length += pl022->cur_transfer->len;
1018 if (pl022->cur_transfer->cs_change)
1019 pl022->cur_chip->cs_control(SSP_CHIP_DESELECT);
1020 /* Move to next transfer */
1021 message->state = next_transfer(pl022);
1024 /* Handle end of message */
1025 if (message->state == STATE_DONE)
1026 message->status = 0;
1027 else
1028 message->status = -EIO;
1030 giveback(pl022);
1031 return;
1035 * pump_messages - Workqueue function which processes spi message queue
1036 * @data: pointer to private data of SSP driver
1038 * This function checks if there is any spi message in the queue that
1039 * needs processing and delegate control to appropriate function
1040 * do_polling_transfer()/do_interrupt_transfer()/do_dma_transfer()
1041 * based on the kind of the transfer
1044 static void pump_messages(struct work_struct *work)
1046 struct pl022 *pl022 =
1047 container_of(work, struct pl022, pump_messages);
1048 unsigned long flags;
1050 /* Lock queue and check for queue work */
1051 spin_lock_irqsave(&pl022->queue_lock, flags);
1052 if (list_empty(&pl022->queue) || pl022->run == QUEUE_STOPPED) {
1053 pl022->busy = 0;
1054 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1055 return;
1057 /* Make sure we are not already running a message */
1058 if (pl022->cur_msg) {
1059 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1060 return;
1062 /* Extract head of queue */
1063 pl022->cur_msg =
1064 list_entry(pl022->queue.next, struct spi_message, queue);
1066 list_del_init(&pl022->cur_msg->queue);
1067 pl022->busy = 1;
1068 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1070 /* Initial message state */
1071 pl022->cur_msg->state = STATE_START;
1072 pl022->cur_transfer = list_entry(pl022->cur_msg->transfers.next,
1073 struct spi_transfer,
1074 transfer_list);
1076 /* Setup the SPI using the per chip configuration */
1077 pl022->cur_chip = spi_get_ctldata(pl022->cur_msg->spi);
1079 * We enable the clock here, then the clock will be disabled when
1080 * giveback() is called in each method (poll/interrupt/DMA)
1082 clk_enable(pl022->clk);
1083 restore_state(pl022);
1084 flush(pl022);
1086 if (pl022->cur_chip->xfer_type == POLLING_TRANSFER)
1087 do_polling_transfer(pl022);
1088 else if (pl022->cur_chip->xfer_type == INTERRUPT_TRANSFER)
1089 do_interrupt_transfer(pl022);
1090 else
1091 do_dma_transfer(pl022);
1095 static int __init init_queue(struct pl022 *pl022)
1097 INIT_LIST_HEAD(&pl022->queue);
1098 spin_lock_init(&pl022->queue_lock);
1100 pl022->run = QUEUE_STOPPED;
1101 pl022->busy = 0;
1103 tasklet_init(&pl022->pump_transfers,
1104 pump_transfers, (unsigned long)pl022);
1106 INIT_WORK(&pl022->pump_messages, pump_messages);
1107 pl022->workqueue = create_singlethread_workqueue(
1108 dev_name(pl022->master->dev.parent));
1109 if (pl022->workqueue == NULL)
1110 return -EBUSY;
1112 return 0;
1116 static int start_queue(struct pl022 *pl022)
1118 unsigned long flags;
1120 spin_lock_irqsave(&pl022->queue_lock, flags);
1122 if (pl022->run == QUEUE_RUNNING || pl022->busy) {
1123 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1124 return -EBUSY;
1127 pl022->run = QUEUE_RUNNING;
1128 pl022->cur_msg = NULL;
1129 pl022->cur_transfer = NULL;
1130 pl022->cur_chip = NULL;
1131 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1133 queue_work(pl022->workqueue, &pl022->pump_messages);
1135 return 0;
1139 static int stop_queue(struct pl022 *pl022)
1141 unsigned long flags;
1142 unsigned limit = 500;
1143 int status = 0;
1145 spin_lock_irqsave(&pl022->queue_lock, flags);
1147 /* This is a bit lame, but is optimized for the common execution path.
1148 * A wait_queue on the pl022->busy could be used, but then the common
1149 * execution path (pump_messages) would be required to call wake_up or
1150 * friends on every SPI message. Do this instead */
1151 pl022->run = QUEUE_STOPPED;
1152 while (!list_empty(&pl022->queue) && pl022->busy && limit--) {
1153 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1154 msleep(10);
1155 spin_lock_irqsave(&pl022->queue_lock, flags);
1158 if (!list_empty(&pl022->queue) || pl022->busy)
1159 status = -EBUSY;
1161 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1163 return status;
1166 static int destroy_queue(struct pl022 *pl022)
1168 int status;
1170 status = stop_queue(pl022);
1171 /* we are unloading the module or failing to load (only two calls
1172 * to this routine), and neither call can handle a return value.
1173 * However, destroy_workqueue calls flush_workqueue, and that will
1174 * block until all work is done. If the reason that stop_queue
1175 * timed out is that the work will never finish, then it does no
1176 * good to call destroy_workqueue, so return anyway. */
1177 if (status != 0)
1178 return status;
1180 destroy_workqueue(pl022->workqueue);
1182 return 0;
1185 static int verify_controller_parameters(struct pl022 *pl022,
1186 struct pl022_config_chip *chip_info)
1188 if ((chip_info->lbm != LOOPBACK_ENABLED)
1189 && (chip_info->lbm != LOOPBACK_DISABLED)) {
1190 dev_err(chip_info->dev,
1191 "loopback Mode is configured incorrectly\n");
1192 return -EINVAL;
1194 if ((chip_info->iface < SSP_INTERFACE_MOTOROLA_SPI)
1195 || (chip_info->iface > SSP_INTERFACE_UNIDIRECTIONAL)) {
1196 dev_err(chip_info->dev,
1197 "interface is configured incorrectly\n");
1198 return -EINVAL;
1200 if ((chip_info->iface == SSP_INTERFACE_UNIDIRECTIONAL) &&
1201 (!pl022->vendor->unidir)) {
1202 dev_err(chip_info->dev,
1203 "unidirectional mode not supported in this "
1204 "hardware version\n");
1205 return -EINVAL;
1207 if ((chip_info->hierarchy != SSP_MASTER)
1208 && (chip_info->hierarchy != SSP_SLAVE)) {
1209 dev_err(chip_info->dev,
1210 "hierarchy is configured incorrectly\n");
1211 return -EINVAL;
1213 if (((chip_info->clk_freq).cpsdvsr < CPSDVR_MIN)
1214 || ((chip_info->clk_freq).cpsdvsr > CPSDVR_MAX)) {
1215 dev_err(chip_info->dev,
1216 "cpsdvsr is configured incorrectly\n");
1217 return -EINVAL;
1219 if ((chip_info->endian_rx != SSP_RX_MSB)
1220 && (chip_info->endian_rx != SSP_RX_LSB)) {
1221 dev_err(chip_info->dev,
1222 "RX FIFO endianess is configured incorrectly\n");
1223 return -EINVAL;
1225 if ((chip_info->endian_tx != SSP_TX_MSB)
1226 && (chip_info->endian_tx != SSP_TX_LSB)) {
1227 dev_err(chip_info->dev,
1228 "TX FIFO endianess is configured incorrectly\n");
1229 return -EINVAL;
1231 if ((chip_info->data_size < SSP_DATA_BITS_4)
1232 || (chip_info->data_size > SSP_DATA_BITS_32)) {
1233 dev_err(chip_info->dev,
1234 "DATA Size is configured incorrectly\n");
1235 return -EINVAL;
1237 if ((chip_info->com_mode != INTERRUPT_TRANSFER)
1238 && (chip_info->com_mode != DMA_TRANSFER)
1239 && (chip_info->com_mode != POLLING_TRANSFER)) {
1240 dev_err(chip_info->dev,
1241 "Communication mode is configured incorrectly\n");
1242 return -EINVAL;
1244 if ((chip_info->rx_lev_trig < SSP_RX_1_OR_MORE_ELEM)
1245 || (chip_info->rx_lev_trig > SSP_RX_32_OR_MORE_ELEM)) {
1246 dev_err(chip_info->dev,
1247 "RX FIFO Trigger Level is configured incorrectly\n");
1248 return -EINVAL;
1250 if ((chip_info->tx_lev_trig < SSP_TX_1_OR_MORE_EMPTY_LOC)
1251 || (chip_info->tx_lev_trig > SSP_TX_32_OR_MORE_EMPTY_LOC)) {
1252 dev_err(chip_info->dev,
1253 "TX FIFO Trigger Level is configured incorrectly\n");
1254 return -EINVAL;
1256 if (chip_info->iface == SSP_INTERFACE_MOTOROLA_SPI) {
1257 if ((chip_info->clk_phase != SSP_CLK_FIRST_EDGE)
1258 && (chip_info->clk_phase != SSP_CLK_SECOND_EDGE)) {
1259 dev_err(chip_info->dev,
1260 "Clock Phase is configured incorrectly\n");
1261 return -EINVAL;
1263 if ((chip_info->clk_pol != SSP_CLK_POL_IDLE_LOW)
1264 && (chip_info->clk_pol != SSP_CLK_POL_IDLE_HIGH)) {
1265 dev_err(chip_info->dev,
1266 "Clock Polarity is configured incorrectly\n");
1267 return -EINVAL;
1270 if (chip_info->iface == SSP_INTERFACE_NATIONAL_MICROWIRE) {
1271 if ((chip_info->ctrl_len < SSP_BITS_4)
1272 || (chip_info->ctrl_len > SSP_BITS_32)) {
1273 dev_err(chip_info->dev,
1274 "CTRL LEN is configured incorrectly\n");
1275 return -EINVAL;
1277 if ((chip_info->wait_state != SSP_MWIRE_WAIT_ZERO)
1278 && (chip_info->wait_state != SSP_MWIRE_WAIT_ONE)) {
1279 dev_err(chip_info->dev,
1280 "Wait State is configured incorrectly\n");
1281 return -EINVAL;
1283 if ((chip_info->duplex != SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
1284 && (chip_info->duplex !=
1285 SSP_MICROWIRE_CHANNEL_HALF_DUPLEX)) {
1286 dev_err(chip_info->dev,
1287 "DUPLEX is configured incorrectly\n");
1288 return -EINVAL;
1291 if (chip_info->cs_control == NULL) {
1292 dev_warn(chip_info->dev,
1293 "Chip Select Function is NULL for this chip\n");
1294 chip_info->cs_control = null_cs_control;
1296 return 0;
1300 * pl022_transfer - transfer function registered to SPI master framework
1301 * @spi: spi device which is requesting transfer
1302 * @msg: spi message which is to handled is queued to driver queue
1304 * This function is registered to the SPI framework for this SPI master
1305 * controller. It will queue the spi_message in the queue of driver if
1306 * the queue is not stopped and return.
1308 static int pl022_transfer(struct spi_device *spi, struct spi_message *msg)
1310 struct pl022 *pl022 = spi_master_get_devdata(spi->master);
1311 unsigned long flags;
1313 spin_lock_irqsave(&pl022->queue_lock, flags);
1315 if (pl022->run == QUEUE_STOPPED) {
1316 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1317 return -ESHUTDOWN;
1319 msg->actual_length = 0;
1320 msg->status = -EINPROGRESS;
1321 msg->state = STATE_START;
1323 list_add_tail(&msg->queue, &pl022->queue);
1324 if (pl022->run == QUEUE_RUNNING && !pl022->busy)
1325 queue_work(pl022->workqueue, &pl022->pump_messages);
1327 spin_unlock_irqrestore(&pl022->queue_lock, flags);
1328 return 0;
1331 static int calculate_effective_freq(struct pl022 *pl022,
1332 int freq,
1333 struct ssp_clock_params *clk_freq)
1335 /* Lets calculate the frequency parameters */
1336 u16 cpsdvsr = 2;
1337 u16 scr = 0;
1338 bool freq_found = false;
1339 u32 rate;
1340 u32 max_tclk;
1341 u32 min_tclk;
1343 rate = clk_get_rate(pl022->clk);
1344 /* cpsdvscr = 2 & scr 0 */
1345 max_tclk = (rate / (CPSDVR_MIN * (1 + SCR_MIN)));
1346 /* cpsdvsr = 254 & scr = 255 */
1347 min_tclk = (rate / (CPSDVR_MAX * (1 + SCR_MAX)));
1349 if ((freq <= max_tclk) && (freq >= min_tclk)) {
1350 while (cpsdvsr <= CPSDVR_MAX && !freq_found) {
1351 while (scr <= SCR_MAX && !freq_found) {
1352 if ((rate /
1353 (cpsdvsr * (1 + scr))) > freq)
1354 scr += 1;
1355 else {
1357 * This bool is made true when
1358 * effective frequency >=
1359 * target frequency is found
1361 freq_found = true;
1362 if ((rate /
1363 (cpsdvsr * (1 + scr))) != freq) {
1364 if (scr == SCR_MIN) {
1365 cpsdvsr -= 2;
1366 scr = SCR_MAX;
1367 } else
1368 scr -= 1;
1372 if (!freq_found) {
1373 cpsdvsr += 2;
1374 scr = SCR_MIN;
1377 if (cpsdvsr != 0) {
1378 dev_dbg(&pl022->adev->dev,
1379 "SSP Effective Frequency is %u\n",
1380 (rate / (cpsdvsr * (1 + scr))));
1381 clk_freq->cpsdvsr = (u8) (cpsdvsr & 0xFF);
1382 clk_freq->scr = (u8) (scr & 0xFF);
1383 dev_dbg(&pl022->adev->dev,
1384 "SSP cpsdvsr = %d, scr = %d\n",
1385 clk_freq->cpsdvsr, clk_freq->scr);
1387 } else {
1388 dev_err(&pl022->adev->dev,
1389 "controller data is incorrect: out of range frequency");
1390 return -EINVAL;
1392 return 0;
1396 * NOT IMPLEMENTED
1397 * process_dma_info - Processes the DMA info provided by client drivers
1398 * @chip_info: chip info provided by client device
1399 * @chip: Runtime state maintained by the SSP controller for each spi device
1401 * This function processes and stores DMA config provided by client driver
1402 * into the runtime state maintained by the SSP controller driver
1404 static int process_dma_info(struct pl022_config_chip *chip_info,
1405 struct chip_data *chip)
1407 dev_err(chip_info->dev,
1408 "cannot process DMA info, DMA not implemented!\n");
1409 return -ENOTSUPP;
1413 * pl022_setup - setup function registered to SPI master framework
1414 * @spi: spi device which is requesting setup
1416 * This function is registered to the SPI framework for this SPI master
1417 * controller. If it is the first time when setup is called by this device,
1418 * this function will initialize the runtime state for this chip and save
1419 * the same in the device structure. Else it will update the runtime info
1420 * with the updated chip info. Nothing is really being written to the
1421 * controller hardware here, that is not done until the actual transfer
1422 * commence.
1425 /* FIXME: JUST GUESSING the spi->mode bits understood by this driver */
1426 #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
1427 | SPI_LSB_FIRST | SPI_LOOP)
1429 static int pl022_setup(struct spi_device *spi)
1431 struct pl022_config_chip *chip_info;
1432 struct chip_data *chip;
1433 int status = 0;
1434 struct pl022 *pl022 = spi_master_get_devdata(spi->master);
1436 if (spi->mode & ~MODEBITS) {
1437 dev_dbg(&spi->dev, "unsupported mode bits %x\n",
1438 spi->mode & ~MODEBITS);
1439 return -EINVAL;
1442 if (!spi->max_speed_hz)
1443 return -EINVAL;
1445 /* Get controller_state if one is supplied */
1446 chip = spi_get_ctldata(spi);
1448 if (chip == NULL) {
1449 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1450 if (!chip) {
1451 dev_err(&spi->dev,
1452 "cannot allocate controller state\n");
1453 return -ENOMEM;
1455 dev_dbg(&spi->dev,
1456 "allocated memory for controller's runtime state\n");
1459 /* Get controller data if one is supplied */
1460 chip_info = spi->controller_data;
1462 if (chip_info == NULL) {
1463 /* spi_board_info.controller_data not is supplied */
1464 dev_dbg(&spi->dev,
1465 "using default controller_data settings\n");
1467 chip_info =
1468 kzalloc(sizeof(struct pl022_config_chip), GFP_KERNEL);
1470 if (!chip_info) {
1471 dev_err(&spi->dev,
1472 "cannot allocate controller data\n");
1473 status = -ENOMEM;
1474 goto err_first_setup;
1477 dev_dbg(&spi->dev, "allocated memory for controller data\n");
1479 /* Pointer back to the SPI device */
1480 chip_info->dev = &spi->dev;
1482 * Set controller data default values:
1483 * Polling is supported by default
1485 chip_info->lbm = LOOPBACK_DISABLED;
1486 chip_info->com_mode = POLLING_TRANSFER;
1487 chip_info->iface = SSP_INTERFACE_MOTOROLA_SPI;
1488 chip_info->hierarchy = SSP_SLAVE;
1489 chip_info->slave_tx_disable = DO_NOT_DRIVE_TX;
1490 chip_info->endian_tx = SSP_TX_LSB;
1491 chip_info->endian_rx = SSP_RX_LSB;
1492 chip_info->data_size = SSP_DATA_BITS_12;
1493 chip_info->rx_lev_trig = SSP_RX_1_OR_MORE_ELEM;
1494 chip_info->tx_lev_trig = SSP_TX_1_OR_MORE_EMPTY_LOC;
1495 chip_info->clk_phase = SSP_CLK_SECOND_EDGE;
1496 chip_info->clk_pol = SSP_CLK_POL_IDLE_LOW;
1497 chip_info->ctrl_len = SSP_BITS_8;
1498 chip_info->wait_state = SSP_MWIRE_WAIT_ZERO;
1499 chip_info->duplex = SSP_MICROWIRE_CHANNEL_FULL_DUPLEX;
1500 chip_info->cs_control = null_cs_control;
1501 } else {
1502 dev_dbg(&spi->dev,
1503 "using user supplied controller_data settings\n");
1507 * We can override with custom divisors, else we use the board
1508 * frequency setting
1510 if ((0 == chip_info->clk_freq.cpsdvsr)
1511 && (0 == chip_info->clk_freq.scr)) {
1512 status = calculate_effective_freq(pl022,
1513 spi->max_speed_hz,
1514 &chip_info->clk_freq);
1515 if (status < 0)
1516 goto err_config_params;
1517 } else {
1518 if ((chip_info->clk_freq.cpsdvsr % 2) != 0)
1519 chip_info->clk_freq.cpsdvsr =
1520 chip_info->clk_freq.cpsdvsr - 1;
1522 status = verify_controller_parameters(pl022, chip_info);
1523 if (status) {
1524 dev_err(&spi->dev, "controller data is incorrect");
1525 goto err_config_params;
1527 /* Now set controller state based on controller data */
1528 chip->xfer_type = chip_info->com_mode;
1529 chip->cs_control = chip_info->cs_control;
1531 if (chip_info->data_size <= 8) {
1532 dev_dbg(&spi->dev, "1 <= n <=8 bits per word\n");
1533 chip->n_bytes = 1;
1534 chip->read = READING_U8;
1535 chip->write = WRITING_U8;
1536 } else if (chip_info->data_size <= 16) {
1537 dev_dbg(&spi->dev, "9 <= n <= 16 bits per word\n");
1538 chip->n_bytes = 2;
1539 chip->read = READING_U16;
1540 chip->write = WRITING_U16;
1541 } else {
1542 if (pl022->vendor->max_bpw >= 32) {
1543 dev_dbg(&spi->dev, "17 <= n <= 32 bits per word\n");
1544 chip->n_bytes = 4;
1545 chip->read = READING_U32;
1546 chip->write = WRITING_U32;
1547 } else {
1548 dev_err(&spi->dev,
1549 "illegal data size for this controller!\n");
1550 dev_err(&spi->dev,
1551 "a standard pl022 can only handle "
1552 "1 <= n <= 16 bit words\n");
1553 goto err_config_params;
1557 /* Now Initialize all register settings required for this chip */
1558 chip->cr0 = 0;
1559 chip->cr1 = 0;
1560 chip->dmacr = 0;
1561 chip->cpsr = 0;
1562 if ((chip_info->com_mode == DMA_TRANSFER)
1563 && ((pl022->master_info)->enable_dma)) {
1564 chip->enable_dma = 1;
1565 dev_dbg(&spi->dev, "DMA mode set in controller state\n");
1566 status = process_dma_info(chip_info, chip);
1567 if (status < 0)
1568 goto err_config_params;
1569 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
1570 SSP_DMACR_MASK_RXDMAE, 0);
1571 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
1572 SSP_DMACR_MASK_TXDMAE, 1);
1573 } else {
1574 chip->enable_dma = 0;
1575 dev_dbg(&spi->dev, "DMA mode NOT set in controller state\n");
1576 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
1577 SSP_DMACR_MASK_RXDMAE, 0);
1578 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
1579 SSP_DMACR_MASK_TXDMAE, 1);
1582 chip->cpsr = chip_info->clk_freq.cpsdvsr;
1584 SSP_WRITE_BITS(chip->cr0, chip_info->data_size, SSP_CR0_MASK_DSS, 0);
1585 SSP_WRITE_BITS(chip->cr0, chip_info->duplex, SSP_CR0_MASK_HALFDUP, 5);
1586 SSP_WRITE_BITS(chip->cr0, chip_info->clk_pol, SSP_CR0_MASK_SPO, 6);
1587 SSP_WRITE_BITS(chip->cr0, chip_info->clk_phase, SSP_CR0_MASK_SPH, 7);
1588 SSP_WRITE_BITS(chip->cr0, chip_info->clk_freq.scr, SSP_CR0_MASK_SCR, 8);
1589 SSP_WRITE_BITS(chip->cr0, chip_info->ctrl_len, SSP_CR0_MASK_CSS, 16);
1590 SSP_WRITE_BITS(chip->cr0, chip_info->iface, SSP_CR0_MASK_FRF, 21);
1591 SSP_WRITE_BITS(chip->cr1, chip_info->lbm, SSP_CR1_MASK_LBM, 0);
1592 SSP_WRITE_BITS(chip->cr1, SSP_DISABLED, SSP_CR1_MASK_SSE, 1);
1593 SSP_WRITE_BITS(chip->cr1, chip_info->hierarchy, SSP_CR1_MASK_MS, 2);
1594 SSP_WRITE_BITS(chip->cr1, chip_info->slave_tx_disable, SSP_CR1_MASK_SOD, 3);
1595 SSP_WRITE_BITS(chip->cr1, chip_info->endian_rx, SSP_CR1_MASK_RENDN, 4);
1596 SSP_WRITE_BITS(chip->cr1, chip_info->endian_tx, SSP_CR1_MASK_TENDN, 5);
1597 SSP_WRITE_BITS(chip->cr1, chip_info->wait_state, SSP_CR1_MASK_MWAIT, 6);
1598 SSP_WRITE_BITS(chip->cr1, chip_info->rx_lev_trig, SSP_CR1_MASK_RXIFLSEL, 7);
1599 SSP_WRITE_BITS(chip->cr1, chip_info->tx_lev_trig, SSP_CR1_MASK_TXIFLSEL, 10);
1601 /* Save controller_state */
1602 spi_set_ctldata(spi, chip);
1603 return status;
1604 err_config_params:
1605 err_first_setup:
1606 kfree(chip);
1607 return status;
1611 * pl022_cleanup - cleanup function registered to SPI master framework
1612 * @spi: spi device which is requesting cleanup
1614 * This function is registered to the SPI framework for this SPI master
1615 * controller. It will free the runtime state of chip.
1617 static void pl022_cleanup(struct spi_device *spi)
1619 struct chip_data *chip = spi_get_ctldata(spi);
1621 spi_set_ctldata(spi, NULL);
1622 kfree(chip);
1626 static int __init
1627 pl022_probe(struct amba_device *adev, struct amba_id *id)
1629 struct device *dev = &adev->dev;
1630 struct pl022_ssp_controller *platform_info = adev->dev.platform_data;
1631 struct spi_master *master;
1632 struct pl022 *pl022 = NULL; /*Data for this driver */
1633 int status = 0;
1635 dev_info(&adev->dev,
1636 "ARM PL022 driver, device ID: 0x%08x\n", adev->periphid);
1637 if (platform_info == NULL) {
1638 dev_err(&adev->dev, "probe - no platform data supplied\n");
1639 status = -ENODEV;
1640 goto err_no_pdata;
1643 /* Allocate master with space for data */
1644 master = spi_alloc_master(dev, sizeof(struct pl022));
1645 if (master == NULL) {
1646 dev_err(&adev->dev, "probe - cannot alloc SPI master\n");
1647 status = -ENOMEM;
1648 goto err_no_master;
1651 pl022 = spi_master_get_devdata(master);
1652 pl022->master = master;
1653 pl022->master_info = platform_info;
1654 pl022->adev = adev;
1655 pl022->vendor = id->data;
1658 * Bus Number Which has been Assigned to this SSP controller
1659 * on this board
1661 master->bus_num = platform_info->bus_id;
1662 master->num_chipselect = platform_info->num_chipselect;
1663 master->cleanup = pl022_cleanup;
1664 master->setup = pl022_setup;
1665 master->transfer = pl022_transfer;
1667 dev_dbg(&adev->dev, "BUSNO: %d\n", master->bus_num);
1669 status = amba_request_regions(adev, NULL);
1670 if (status)
1671 goto err_no_ioregion;
1673 pl022->virtbase = ioremap(adev->res.start, resource_size(&adev->res));
1674 if (pl022->virtbase == NULL) {
1675 status = -ENOMEM;
1676 goto err_no_ioremap;
1678 printk(KERN_INFO "pl022: mapped registers from 0x%08x to %p\n",
1679 adev->res.start, pl022->virtbase);
1681 pl022->clk = clk_get(&adev->dev, NULL);
1682 if (IS_ERR(pl022->clk)) {
1683 status = PTR_ERR(pl022->clk);
1684 dev_err(&adev->dev, "could not retrieve SSP/SPI bus clock\n");
1685 goto err_no_clk;
1688 /* Disable SSP */
1689 clk_enable(pl022->clk);
1690 writew((readw(SSP_CR1(pl022->virtbase)) & (~SSP_CR1_MASK_SSE)),
1691 SSP_CR1(pl022->virtbase));
1692 load_ssp_default_config(pl022);
1693 clk_disable(pl022->clk);
1695 status = request_irq(adev->irq[0], pl022_interrupt_handler, 0, "pl022",
1696 pl022);
1697 if (status < 0) {
1698 dev_err(&adev->dev, "probe - cannot get IRQ (%d)\n", status);
1699 goto err_no_irq;
1701 /* Initialize and start queue */
1702 status = init_queue(pl022);
1703 if (status != 0) {
1704 dev_err(&adev->dev, "probe - problem initializing queue\n");
1705 goto err_init_queue;
1707 status = start_queue(pl022);
1708 if (status != 0) {
1709 dev_err(&adev->dev, "probe - problem starting queue\n");
1710 goto err_start_queue;
1712 /* Register with the SPI framework */
1713 amba_set_drvdata(adev, pl022);
1714 status = spi_register_master(master);
1715 if (status != 0) {
1716 dev_err(&adev->dev,
1717 "probe - problem registering spi master\n");
1718 goto err_spi_register;
1720 dev_dbg(dev, "probe succeded\n");
1721 return 0;
1723 err_spi_register:
1724 err_start_queue:
1725 err_init_queue:
1726 destroy_queue(pl022);
1727 free_irq(adev->irq[0], pl022);
1728 err_no_irq:
1729 clk_put(pl022->clk);
1730 err_no_clk:
1731 iounmap(pl022->virtbase);
1732 err_no_ioremap:
1733 amba_release_regions(adev);
1734 err_no_ioregion:
1735 spi_master_put(master);
1736 err_no_master:
1737 err_no_pdata:
1738 return status;
1741 static int __exit
1742 pl022_remove(struct amba_device *adev)
1744 struct pl022 *pl022 = amba_get_drvdata(adev);
1745 int status = 0;
1746 if (!pl022)
1747 return 0;
1749 /* Remove the queue */
1750 status = destroy_queue(pl022);
1751 if (status != 0) {
1752 dev_err(&adev->dev,
1753 "queue remove failed (%d)\n", status);
1754 return status;
1756 load_ssp_default_config(pl022);
1757 free_irq(adev->irq[0], pl022);
1758 clk_disable(pl022->clk);
1759 clk_put(pl022->clk);
1760 iounmap(pl022->virtbase);
1761 amba_release_regions(adev);
1762 tasklet_disable(&pl022->pump_transfers);
1763 spi_unregister_master(pl022->master);
1764 spi_master_put(pl022->master);
1765 amba_set_drvdata(adev, NULL);
1766 dev_dbg(&adev->dev, "remove succeded\n");
1767 return 0;
1770 #ifdef CONFIG_PM
1771 static int pl022_suspend(struct amba_device *adev, pm_message_t state)
1773 struct pl022 *pl022 = amba_get_drvdata(adev);
1774 int status = 0;
1776 status = stop_queue(pl022);
1777 if (status) {
1778 dev_warn(&adev->dev, "suspend cannot stop queue\n");
1779 return status;
1782 clk_enable(pl022->clk);
1783 load_ssp_default_config(pl022);
1784 clk_disable(pl022->clk);
1785 dev_dbg(&adev->dev, "suspended\n");
1786 return 0;
1789 static int pl022_resume(struct amba_device *adev)
1791 struct pl022 *pl022 = amba_get_drvdata(adev);
1792 int status = 0;
1794 /* Start the queue running */
1795 status = start_queue(pl022);
1796 if (status)
1797 dev_err(&adev->dev, "problem starting queue (%d)\n", status);
1798 else
1799 dev_dbg(&adev->dev, "resumed\n");
1801 return status;
1803 #else
1804 #define pl022_suspend NULL
1805 #define pl022_resume NULL
1806 #endif /* CONFIG_PM */
1808 static struct vendor_data vendor_arm = {
1809 .fifodepth = 8,
1810 .max_bpw = 16,
1811 .unidir = false,
1815 static struct vendor_data vendor_st = {
1816 .fifodepth = 32,
1817 .max_bpw = 32,
1818 .unidir = false,
1821 static struct amba_id pl022_ids[] = {
1824 * ARM PL022 variant, this has a 16bit wide
1825 * and 8 locations deep TX/RX FIFO
1827 .id = 0x00041022,
1828 .mask = 0x000fffff,
1829 .data = &vendor_arm,
1833 * ST Micro derivative, this has 32bit wide
1834 * and 32 locations deep TX/RX FIFO
1836 .id = 0x01080022,
1837 .mask = 0xffffffff,
1838 .data = &vendor_st,
1840 { 0, 0 },
1843 static struct amba_driver pl022_driver = {
1844 .drv = {
1845 .name = "ssp-pl022",
1847 .id_table = pl022_ids,
1848 .probe = pl022_probe,
1849 .remove = __exit_p(pl022_remove),
1850 .suspend = pl022_suspend,
1851 .resume = pl022_resume,
1855 static int __init pl022_init(void)
1857 return amba_driver_register(&pl022_driver);
1860 module_init(pl022_init);
1862 static void __exit pl022_exit(void)
1864 amba_driver_unregister(&pl022_driver);
1867 module_exit(pl022_exit);
1869 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
1870 MODULE_DESCRIPTION("PL022 SSP Controller Driver");
1871 MODULE_LICENSE("GPL");