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.
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>
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))
66 #define DO_NOT_DRIVE_TX 1
68 #define DO_NOT_QUEUE_DMA 0
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 (0x0FUL << 0)
106 #define SSP_CR0_MASK_FRF (0x3UL << 4)
107 #define SSP_CR0_MASK_SPO (0x1UL << 6)
108 #define SSP_CR0_MASK_SPH (0x1UL << 7)
109 #define SSP_CR0_MASK_SCR (0xFFUL << 8)
112 * The ST version of this block moves som bits
113 * in SSP_CR0 and extends it to 32 bits
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)
122 * SSP Control Register 0 - SSP_CR1
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)
130 * The ST version of this block adds some bits
133 #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)
138 /* This one is only in the PL023 variant */
139 #define SSP_CR1_MASK_FBCLKDEL_ST (0x7UL << 13)
142 * SSP Status Register - SSP_SR
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 */
147 #define SSP_SR_MASK_RFF (0x1UL << 3) /* Receive FIFO full */
148 #define SSP_SR_MASK_BSY (0x1UL << 4) /* Busy Flag */
151 * SSP Clock Prescale Register - SSP_CPSR
153 #define SSP_CPSR_MASK_CPSDVSR (0xFFUL << 0)
156 * SSP Interrupt Mask Set/Clear Register - SSP_IMSC
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 */
164 * SSP Raw Interrupt Status Register - SSP_RIS
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)
176 * SSP Masked Interrupt Status Register - SSP_MIS
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)
188 * SSP Interrupt Clear Register - SSP_ICR
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)
196 * SSP DMA Control Register - SSP_DMACR
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)
204 * SSP Integration Test control Register - SSP_ITCR
206 #define SSP_ITCR_MASK_ITEN (0x1UL << 0)
207 #define SSP_ITCR_MASK_TESTFIFO (0x1UL << 1)
210 * SSP Integration Test Input Register - SSP_ITIP
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)
220 * SSP Integration Test output Register - SSP_ITOP
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)
238 * SSP Test Data Register - SSP_TDR
240 #define TDR_MASK_TESTDATA (0xFFFFFFFF)
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.
248 #define STATE_START ((void *) 0)
249 #define STATE_RUNNING ((void *) 1)
250 #define STATE_DONE ((void *) 2)
251 #define STATE_ERROR ((void *) -1)
256 #define QUEUE_RUNNING (0)
257 #define QUEUE_STOPPED (1)
259 * SSP State - Whether Enabled or Disabled
261 #define SSP_DISABLED (0)
262 #define SSP_ENABLED (1)
265 * SSP DMA State - Whether DMA Enabled or Disabled
267 #define SSP_DMA_DISABLED (0)
268 #define SSP_DMA_ENABLED (1)
273 #define SSP_DEFAULT_CLKRATE 0x2
274 #define SSP_DEFAULT_PRESCALE 0x40
277 * SSP Clock Parameter ranges
279 #define CPSDVR_MIN 0x02
280 #define CPSDVR_MAX 0xFE
285 * SSP Interrupt related Macros
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)
291 #define CLEAR_ALL_INTERRUPTS 0x3
295 * The type of reading going on on this chip
305 * The type of writing going on on this chip
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
320 * @extended_cr: 32 bit wide control register 0 with extra
321 * features and extra features in CR1 as found in the ST variants
322 * @pl023: supports a subset of the ST extensions called "PL023"
333 * struct pl022 - This is the private SSP driver data structure
334 * @adev: AMBA device model hookup
335 * @vendor: Vendor data for the IP block
336 * @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
358 struct amba_device
*adev
;
359 struct vendor_data
*vendor
;
360 resource_size_t phybase
;
361 void __iomem
*virtbase
;
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
;
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
;
381 enum ssp_reading read
;
382 enum ssp_writing write
;
387 * struct chip_data - To maintain runtime state of SSP for each client chip
388 * @cr0: Value of control register CR0 of SSP - on later ST variants this
389 * register is 32 bits wide rather than just 16
390 * @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
400 * Runtime state of the SSP controller, maintained per chip,
401 * This would be set according to the current message that would be served
410 enum ssp_reading read
;
411 enum ssp_writing write
;
412 void (*cs_control
) (u32 command
);
417 * null_cs_control - Dummy chip select function
418 * @command: select/delect the chip
420 * If no chip select function is provided by client this is used as dummy
423 static void null_cs_control(u32 command
)
425 pr_debug("pl022: dummy chip select control, CS=0x%x\n", command
);
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
434 static void giveback(struct pl022
*pl022
)
436 struct spi_transfer
*last_transfer
;
438 struct spi_message
*msg
;
439 void (*curr_cs_control
) (u32 command
);
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.
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
);
455 last_transfer
= list_entry(msg
->transfers
.prev
,
459 /* Delay if requested before any change in chip select */
460 if (last_transfer
->delay_usecs
)
462 * FIXME: This runs in interrupt context.
463 * Is this really smart?
465 udelay(last_transfer
->delay_usecs
);
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
471 if (!last_transfer
->cs_change
)
472 curr_cs_control(SSP_CHIP_DESELECT
);
474 struct spi_message
*next_msg
;
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.
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...
486 /* get a pointer to the next message, if any */
487 spin_lock_irqsave(&pl022
->queue_lock
, flags
);
488 if (list_empty(&pl022
->queue
))
491 next_msg
= list_entry(pl022
->queue
.next
,
492 struct spi_message
, queue
);
493 spin_unlock_irqrestore(&pl022
->queue_lock
, flags
);
495 /* see if the next and current messages point
498 if (next_msg
&& next_msg
->spi
!= msg
->spi
)
500 if (!next_msg
|| msg
->state
== STATE_ERROR
)
501 curr_cs_control(SSP_CHIP_DESELECT
);
505 msg
->complete(msg
->context
);
506 /* This message is completed, so let's turn off the clocks! */
507 clk_disable(pl022
->clk
);
508 amba_pclk_disable(pl022
->adev
);
512 * flush - flush the FIFO to reach a clean state
513 * @pl022: SSP driver private data structure
515 static int flush(struct pl022
*pl022
)
517 unsigned long limit
= loops_per_jiffy
<< 1;
519 dev_dbg(&pl022
->adev
->dev
, "flush\n");
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
--);
525 pl022
->exp_fifo_level
= 0;
531 * restore_state - Load configuration of current chip
532 * @pl022: SSP driver private data structure
534 static void restore_state(struct pl022
*pl022
)
536 struct chip_data
*chip
= pl022
->cur_chip
;
538 if (pl022
->vendor
->extended_cr
)
539 writel(chip
->cr0
, SSP_CR0(pl022
->virtbase
));
541 writew(chip
->cr0
, SSP_CR0(pl022
->virtbase
));
542 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
));
550 * Default SSP Register Values
552 #define DEFAULT_SSP_REG_CR0 ( \
553 GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS, 0) | \
554 GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF, 4) | \
555 GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
556 GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
557 GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
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) \
571 /* 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) \
579 #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) | \
583 GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) \
586 /* 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) \
597 * The PL023 variant has further differences: no loopback mode, no microwire
598 * support, and a new clock feedback delay setting.
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) \
611 #define DEFAULT_SSP_REG_CPSR ( \
612 GEN_MASK_BITS(SSP_DEFAULT_PRESCALE, SSP_CPSR_MASK_CPSDVSR, 0) \
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) \
621 * load_ssp_default_config - Load default configuration for SSP
622 * @pl022: SSP driver private data structure
624 static void load_ssp_default_config(struct pl022
*pl022
)
626 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
) {
630 writel(DEFAULT_SSP_REG_CR0_ST
, SSP_CR0(pl022
->virtbase
));
631 writew(DEFAULT_SSP_REG_CR1_ST
, SSP_CR1(pl022
->virtbase
));
633 writew(DEFAULT_SSP_REG_CR0
, SSP_CR0(pl022
->virtbase
));
634 writew(DEFAULT_SSP_REG_CR1
, SSP_CR1(pl022
->virtbase
));
636 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
));
643 * This will write to TX and read from RX according to the parameters
646 static void readwriter(struct pl022
*pl022
)
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.
655 * 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.
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
);
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
) {
668 readw(SSP_DR(pl022
->virtbase
));
671 *(u8
*) (pl022
->rx
) =
672 readw(SSP_DR(pl022
->virtbase
)) & 0xFFU
;
675 *(u16
*) (pl022
->rx
) =
676 (u16
) readw(SSP_DR(pl022
->virtbase
));
679 *(u32
*) (pl022
->rx
) =
680 readl(SSP_DR(pl022
->virtbase
));
683 pl022
->rx
+= (pl022
->cur_chip
->n_bytes
);
684 pl022
->exp_fifo_level
--;
687 * Write as much as possible up to the RX FIFO size
689 while ((pl022
->exp_fifo_level
< pl022
->vendor
->fifodepth
)
690 && (pl022
->tx
< pl022
->tx_end
)) {
691 switch (pl022
->write
) {
693 writew(0x0, SSP_DR(pl022
->virtbase
));
696 writew(*(u8
*) (pl022
->tx
), SSP_DR(pl022
->virtbase
));
699 writew((*(u16
*) (pl022
->tx
)), SSP_DR(pl022
->virtbase
));
702 writel(*(u32
*) (pl022
->tx
), SSP_DR(pl022
->virtbase
));
705 pl022
->tx
+= (pl022
->cur_chip
->n_bytes
);
706 pl022
->exp_fifo_level
++;
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.
713 while ((readw(SSP_SR(pl022
->virtbase
)) & SSP_SR_MASK_RNE
)
714 && (pl022
->rx
< pl022
->rx_end
)) {
715 switch (pl022
->read
) {
717 readw(SSP_DR(pl022
->virtbase
));
720 *(u8
*) (pl022
->rx
) =
721 readw(SSP_DR(pl022
->virtbase
)) & 0xFFU
;
724 *(u16
*) (pl022
->rx
) =
725 (u16
) readw(SSP_DR(pl022
->virtbase
));
728 *(u32
*) (pl022
->rx
) =
729 readl(SSP_DR(pl022
->virtbase
));
732 pl022
->rx
+= (pl022
->cur_chip
->n_bytes
);
733 pl022
->exp_fifo_level
--;
737 * When we exit here the TX FIFO should be full and the RX FIFO
744 * next_transfer - Move to the Next transfer in the current spi message
745 * @pl022: SSP driver private data structure
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)
752 static void *next_transfer(struct pl022
*pl022
)
754 struct spi_message
*msg
= pl022
->cur_msg
;
755 struct spi_transfer
*trans
= pl022
->cur_transfer
;
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
;
767 * pl022_interrupt_handler - Interrupt handler for SSP controller
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.
777 static irqreturn_t
pl022_interrupt_handler(int irq
, void *dev_id
)
779 struct pl022
*pl022
= dev_id
;
780 struct spi_message
*msg
= pl022
->cur_msg
;
784 if (unlikely(!msg
)) {
785 dev_err(&pl022
->adev
->dev
,
786 "bad message state in interrupt handler");
791 /* Read the Interrupt Status Register */
792 irq_status
= readw(SSP_MIS(pl022
->virtbase
));
794 if (unlikely(!irq_status
))
797 /* This handles the error code interrupts */
798 if (unlikely(irq_status
& SSP_MIS_MASK_RORMIS
)) {
800 * Overrun interrupt - bail out since our Data has been
803 dev_err(&pl022
->adev
->dev
,
805 if (readw(SSP_SR(pl022
->virtbase
)) & SSP_SR_MASK_RFF
)
806 dev_err(&pl022
->adev
->dev
,
808 if (readw(SSP_SR(pl022
->virtbase
)) & SSP_SR_MASK_TNF
)
809 dev_err(&pl022
->adev
->dev
,
813 * Disable and clear interrupts, disable SSP,
814 * mark message with bad status so it can be
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
;
824 /* Schedule message queue handler */
825 tasklet_schedule(&pl022
->pump_transfers
);
831 if ((pl022
->tx
== pl022
->tx_end
) && (flag
== 0)) {
833 /* Disable Transmit interrupt */
834 writew(readw(SSP_IMSC(pl022
->virtbase
)) &
835 (~SSP_IMSC_MASK_TXIM
),
836 SSP_IMSC(pl022
->virtbase
));
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.
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
));
854 /* Update total bytes transfered */
855 msg
->actual_length
+= pl022
->cur_transfer
->len
;
856 if (pl022
->cur_transfer
->cs_change
)
858 cs_control(SSP_CHIP_DESELECT
);
859 /* Move to next transfer */
860 msg
->state
= next_transfer(pl022
);
861 tasklet_schedule(&pl022
->pump_transfers
);
869 * This sets up the pointers to memory for the next message to
870 * send out on the SPI bus.
872 static int set_up_next_transfer(struct pl022
*pl022
,
873 struct spi_transfer
*transfer
)
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");
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
;
893 pl022
->tx
? pl022
->cur_chip
->write
: WRITING_NULL
;
894 pl022
->read
= pl022
->rx
? pl022
->cur_chip
->read
: READING_NULL
;
899 * pump_transfers - Tasklet function which schedules next interrupt transfer
900 * when running in interrupt transfer mode.
901 * @data: SSP driver private data structure
904 static void pump_transfers(unsigned long data
)
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
;
911 /* Get current state information */
912 message
= pl022
->cur_msg
;
913 transfer
= pl022
->cur_transfer
;
915 /* Handle for abort */
916 if (message
->state
== STATE_ERROR
) {
917 message
->status
= -EIO
;
922 /* Handle end of message */
923 if (message
->state
== STATE_DONE
) {
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
,
934 if (previous
->delay_usecs
)
936 * FIXME: This runs in interrupt context.
937 * Is this really smart?
939 udelay(previous
->delay_usecs
);
941 /* Drop chip select only if cs_change is requested */
942 if (previous
->cs_change
)
943 pl022
->cur_chip
->cs_control(SSP_CHIP_SELECT
);
946 message
->state
= STATE_RUNNING
;
949 if (set_up_next_transfer(pl022
, transfer
)) {
950 message
->state
= STATE_ERROR
;
951 message
->status
= -EIO
;
955 /* Flush the FIFOs and let's go! */
957 writew(ENABLE_ALL_INTERRUPTS
, SSP_IMSC(pl022
->virtbase
));
962 * configure_dma - It configures the DMA pipes for DMA transfers
963 * @data: SSP driver's private data structure
966 static int configure_dma(void *data
)
968 struct pl022
*pl022
= data
;
969 dev_dbg(&pl022
->adev
->dev
, "configure DMA\n");
974 * do_dma_transfer - It handles transfers of the current message
976 * NOT FULLY IMPLEMENTED
977 * @data: SSP driver's private data structure
979 static void do_dma_transfer(void *data
)
981 struct pl022
*pl022
= data
;
983 if (configure_dma(data
)) {
984 dev_dbg(&pl022
->adev
->dev
, "configuration of DMA Failed!\n");
988 /* TODO: Implememt DMA setup of pipes here */
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
)) {
994 pl022
->cur_msg
->state
= STATE_ERROR
;
995 pl022
->cur_msg
->status
= -EIO
;
1000 writew((readw(SSP_CR1(pl022
->virtbase
)) | SSP_CR1_MASK_SSE
),
1001 SSP_CR1(pl022
->virtbase
));
1003 /* TODO: Enable the DMA transfer here */
1007 pl022
->cur_msg
->state
= STATE_ERROR
;
1008 pl022
->cur_msg
->status
= -EIO
;
1013 static void do_interrupt_transfer(void *data
)
1015 struct pl022
*pl022
= data
;
1017 /* Enable target chip */
1018 pl022
->cur_chip
->cs_control(SSP_CHIP_SELECT
);
1019 if (set_up_next_transfer(pl022
, pl022
->cur_transfer
)) {
1021 pl022
->cur_msg
->state
= STATE_ERROR
;
1022 pl022
->cur_msg
->status
= -EIO
;
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
));
1032 static void do_polling_transfer(void *data
)
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
;
1040 chip
= pl022
->cur_chip
;
1041 message
= pl022
->cur_msg
;
1043 while (message
->state
!= STATE_DONE
) {
1044 /* Handle for abort */
1045 if (message
->state
== STATE_ERROR
)
1047 transfer
= pl022
->cur_transfer
;
1049 /* Delay if requested at end of transfer */
1050 if (message
->state
== STATE_RUNNING
) {
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
);
1060 message
->state
= STATE_RUNNING
;
1061 pl022
->cur_chip
->cs_control(SSP_CHIP_SELECT
);
1064 /* Configuration Changing Per Transfer */
1065 if (set_up_next_transfer(pl022
, transfer
)) {
1067 message
->state
= STATE_ERROR
;
1070 /* Flush FIFOs and enable SSP */
1072 writew((readw(SSP_CR1(pl022
->virtbase
)) | SSP_CR1_MASK_SSE
),
1073 SSP_CR1(pl022
->virtbase
));
1075 dev_dbg(&pl022
->adev
->dev
, "polling transfer ongoing ...\n");
1076 /* FIXME: insert a timeout so we don't hang here indefinately */
1077 while (pl022
->tx
< pl022
->tx_end
|| pl022
->rx
< pl022
->rx_end
)
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
);
1088 /* Handle end of message */
1089 if (message
->state
== STATE_DONE
)
1090 message
->status
= 0;
1092 message
->status
= -EIO
;
1099 * pump_messages - Workqueue function which processes spi message queue
1100 * @data: pointer to private data of SSP driver
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
1108 static void pump_messages(struct work_struct
*work
)
1110 struct pl022
*pl022
=
1111 container_of(work
, struct pl022
, pump_messages
);
1112 unsigned long flags
;
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
) {
1118 spin_unlock_irqrestore(&pl022
->queue_lock
, flags
);
1121 /* Make sure we are not already running a message */
1122 if (pl022
->cur_msg
) {
1123 spin_unlock_irqrestore(&pl022
->queue_lock
, flags
);
1126 /* Extract head of queue */
1128 list_entry(pl022
->queue
.next
, struct spi_message
, queue
);
1130 list_del_init(&pl022
->cur_msg
->queue
);
1132 spin_unlock_irqrestore(&pl022
->queue_lock
, flags
);
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
,
1140 /* Setup the SPI using the per chip configuration */
1141 pl022
->cur_chip
= spi_get_ctldata(pl022
->cur_msg
->spi
);
1143 * We enable the clocks here, then the clocks will be disabled when
1144 * giveback() is called in each method (poll/interrupt/DMA)
1146 amba_pclk_enable(pl022
->adev
);
1147 clk_enable(pl022
->clk
);
1148 restore_state(pl022
);
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
);
1156 do_dma_transfer(pl022
);
1160 static int __init
init_queue(struct pl022
*pl022
)
1162 INIT_LIST_HEAD(&pl022
->queue
);
1163 spin_lock_init(&pl022
->queue_lock
);
1165 pl022
->run
= QUEUE_STOPPED
;
1168 tasklet_init(&pl022
->pump_transfers
,
1169 pump_transfers
, (unsigned long)pl022
);
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
)
1181 static int start_queue(struct pl022
*pl022
)
1183 unsigned long flags
;
1185 spin_lock_irqsave(&pl022
->queue_lock
, flags
);
1187 if (pl022
->run
== QUEUE_RUNNING
|| pl022
->busy
) {
1188 spin_unlock_irqrestore(&pl022
->queue_lock
, flags
);
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
);
1198 queue_work(pl022
->workqueue
, &pl022
->pump_messages
);
1204 static int stop_queue(struct pl022
*pl022
)
1206 unsigned long flags
;
1207 unsigned limit
= 500;
1210 spin_lock_irqsave(&pl022
->queue_lock
, flags
);
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 */
1216 while (!list_empty(&pl022
->queue
) && pl022
->busy
&& limit
--) {
1217 spin_unlock_irqrestore(&pl022
->queue_lock
, flags
);
1219 spin_lock_irqsave(&pl022
->queue_lock
, flags
);
1222 if (!list_empty(&pl022
->queue
) || pl022
->busy
)
1224 else pl022
->run
= QUEUE_STOPPED
;
1226 spin_unlock_irqrestore(&pl022
->queue_lock
, flags
);
1231 static int destroy_queue(struct pl022
*pl022
)
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. */
1245 destroy_workqueue(pl022
->workqueue
);
1250 static int verify_controller_parameters(struct pl022
*pl022
,
1251 struct pl022_config_chip
*chip_info
)
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
1321 if (chip_info
->iface
== SSP_INTERFACE_MOTOROLA_SPI
) {
1322 if ((chip_info
->clk_phase
!= SSP_CLK_FIRST_EDGE
)
1323 && (chip_info
->clk_phase
!= SSP_CLK_SECOND_EDGE
)) {
1324 dev_err(chip_info
->dev
,
1325 "Clock Phase is configured incorrectly\n");
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");
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");
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");
1348 /* 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
!=
1353 SSP_MICROWIRE_CHANNEL_HALF_DUPLEX
))
1354 dev_err(chip_info
->dev
,
1355 "Microwire duplex mode is configured incorrectly\n");
1358 if (chip_info
->duplex
!= SSP_MICROWIRE_CHANNEL_FULL_DUPLEX
)
1359 dev_err(chip_info
->dev
,
1360 "Microwire half duplex mode requested,"
1361 " but this is only available in the"
1362 " ST version of PL022\n");
1366 if (chip_info
->cs_control
== NULL
) {
1367 dev_warn(chip_info
->dev
,
1368 "Chip Select Function is NULL for this chip\n");
1369 chip_info
->cs_control
= null_cs_control
;
1375 * pl022_transfer - transfer function registered to SPI master framework
1376 * @spi: spi device which is requesting transfer
1377 * @msg: spi message which is to handled is queued to driver queue
1379 * This function is registered to the SPI framework for this SPI master
1380 * controller. It will queue the spi_message in the queue of driver if
1381 * the queue is not stopped and return.
1383 static int pl022_transfer(struct spi_device
*spi
, struct spi_message
*msg
)
1385 struct pl022
*pl022
= spi_master_get_devdata(spi
->master
);
1386 unsigned long flags
;
1388 spin_lock_irqsave(&pl022
->queue_lock
, flags
);
1390 if (pl022
->run
== QUEUE_STOPPED
) {
1391 spin_unlock_irqrestore(&pl022
->queue_lock
, flags
);
1394 msg
->actual_length
= 0;
1395 msg
->status
= -EINPROGRESS
;
1396 msg
->state
= STATE_START
;
1398 list_add_tail(&msg
->queue
, &pl022
->queue
);
1399 if (pl022
->run
== QUEUE_RUNNING
&& !pl022
->busy
)
1400 queue_work(pl022
->workqueue
, &pl022
->pump_messages
);
1402 spin_unlock_irqrestore(&pl022
->queue_lock
, flags
);
1406 static int calculate_effective_freq(struct pl022
*pl022
,
1408 struct ssp_clock_params
*clk_freq
)
1410 /* Lets calculate the frequency parameters */
1413 bool freq_found
= false;
1418 rate
= clk_get_rate(pl022
->clk
);
1419 /* cpsdvscr = 2 & scr 0 */
1420 max_tclk
= (rate
/ (CPSDVR_MIN
* (1 + SCR_MIN
)));
1421 /* cpsdvsr = 254 & scr = 255 */
1422 min_tclk
= (rate
/ (CPSDVR_MAX
* (1 + SCR_MAX
)));
1424 if ((freq
<= max_tclk
) && (freq
>= min_tclk
)) {
1425 while (cpsdvsr
<= CPSDVR_MAX
&& !freq_found
) {
1426 while (scr
<= SCR_MAX
&& !freq_found
) {
1428 (cpsdvsr
* (1 + scr
))) > freq
)
1432 * This bool is made true when
1433 * effective frequency >=
1434 * target frequency is found
1438 (cpsdvsr
* (1 + scr
))) != freq
) {
1439 if (scr
== SCR_MIN
) {
1453 dev_dbg(&pl022
->adev
->dev
,
1454 "SSP Effective Frequency is %u\n",
1455 (rate
/ (cpsdvsr
* (1 + scr
))));
1456 clk_freq
->cpsdvsr
= (u8
) (cpsdvsr
& 0xFF);
1457 clk_freq
->scr
= (u8
) (scr
& 0xFF);
1458 dev_dbg(&pl022
->adev
->dev
,
1459 "SSP cpsdvsr = %d, scr = %d\n",
1460 clk_freq
->cpsdvsr
, clk_freq
->scr
);
1463 dev_err(&pl022
->adev
->dev
,
1464 "controller data is incorrect: out of range frequency");
1472 * process_dma_info - Processes the DMA info provided by client drivers
1473 * @chip_info: chip info provided by client device
1474 * @chip: Runtime state maintained by the SSP controller for each spi device
1476 * This function processes and stores DMA config provided by client driver
1477 * into the runtime state maintained by the SSP controller driver
1479 static int process_dma_info(struct pl022_config_chip
*chip_info
,
1480 struct chip_data
*chip
)
1482 dev_err(chip_info
->dev
,
1483 "cannot process DMA info, DMA not implemented!\n");
1488 * pl022_setup - setup function registered to SPI master framework
1489 * @spi: spi device which is requesting setup
1491 * This function is registered to the SPI framework for this SPI master
1492 * controller. If it is the first time when setup is called by this device,
1493 * this function will initialize the runtime state for this chip and save
1494 * the same in the device structure. Else it will update the runtime info
1495 * with the updated chip info. Nothing is really being written to the
1496 * controller hardware here, that is not done until the actual transfer
1500 /* FIXME: JUST GUESSING the spi->mode bits understood by this driver */
1501 #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
1502 | SPI_LSB_FIRST | SPI_LOOP)
1504 static int pl022_setup(struct spi_device
*spi
)
1506 struct pl022_config_chip
*chip_info
;
1507 struct chip_data
*chip
;
1509 struct pl022
*pl022
= spi_master_get_devdata(spi
->master
);
1511 if (spi
->mode
& ~MODEBITS
) {
1512 dev_dbg(&spi
->dev
, "unsupported mode bits %x\n",
1513 spi
->mode
& ~MODEBITS
);
1517 if (!spi
->max_speed_hz
)
1520 /* Get controller_state if one is supplied */
1521 chip
= spi_get_ctldata(spi
);
1524 chip
= kzalloc(sizeof(struct chip_data
), GFP_KERNEL
);
1527 "cannot allocate controller state\n");
1531 "allocated memory for controller's runtime state\n");
1534 /* Get controller data if one is supplied */
1535 chip_info
= spi
->controller_data
;
1537 if (chip_info
== NULL
) {
1538 /* spi_board_info.controller_data not is supplied */
1540 "using default controller_data settings\n");
1543 kzalloc(sizeof(struct pl022_config_chip
), GFP_KERNEL
);
1547 "cannot allocate controller data\n");
1549 goto err_first_setup
;
1552 dev_dbg(&spi
->dev
, "allocated memory for controller data\n");
1554 /* Pointer back to the SPI device */
1555 chip_info
->dev
= &spi
->dev
;
1557 * Set controller data default values:
1558 * Polling is supported by default
1560 chip_info
->lbm
= LOOPBACK_DISABLED
;
1561 chip_info
->com_mode
= POLLING_TRANSFER
;
1562 chip_info
->iface
= SSP_INTERFACE_MOTOROLA_SPI
;
1563 chip_info
->hierarchy
= SSP_SLAVE
;
1564 chip_info
->slave_tx_disable
= DO_NOT_DRIVE_TX
;
1565 chip_info
->endian_tx
= SSP_TX_LSB
;
1566 chip_info
->endian_rx
= SSP_RX_LSB
;
1567 chip_info
->data_size
= SSP_DATA_BITS_12
;
1568 chip_info
->rx_lev_trig
= SSP_RX_1_OR_MORE_ELEM
;
1569 chip_info
->tx_lev_trig
= SSP_TX_1_OR_MORE_EMPTY_LOC
;
1570 chip_info
->clk_phase
= SSP_CLK_SECOND_EDGE
;
1571 chip_info
->clk_pol
= SSP_CLK_POL_IDLE_LOW
;
1572 chip_info
->ctrl_len
= SSP_BITS_8
;
1573 chip_info
->wait_state
= SSP_MWIRE_WAIT_ZERO
;
1574 chip_info
->duplex
= SSP_MICROWIRE_CHANNEL_FULL_DUPLEX
;
1575 chip_info
->cs_control
= null_cs_control
;
1578 "using user supplied controller_data settings\n");
1582 * We can override with custom divisors, else we use the board
1585 if ((0 == chip_info
->clk_freq
.cpsdvsr
)
1586 && (0 == chip_info
->clk_freq
.scr
)) {
1587 status
= calculate_effective_freq(pl022
,
1589 &chip_info
->clk_freq
);
1591 goto err_config_params
;
1593 if ((chip_info
->clk_freq
.cpsdvsr
% 2) != 0)
1594 chip_info
->clk_freq
.cpsdvsr
=
1595 chip_info
->clk_freq
.cpsdvsr
- 1;
1597 status
= verify_controller_parameters(pl022
, chip_info
);
1599 dev_err(&spi
->dev
, "controller data is incorrect");
1600 goto err_config_params
;
1602 /* Now set controller state based on controller data */
1603 chip
->xfer_type
= chip_info
->com_mode
;
1604 chip
->cs_control
= chip_info
->cs_control
;
1606 if (chip_info
->data_size
<= 8) {
1607 dev_dbg(&spi
->dev
, "1 <= n <=8 bits per word\n");
1609 chip
->read
= READING_U8
;
1610 chip
->write
= WRITING_U8
;
1611 } else if (chip_info
->data_size
<= 16) {
1612 dev_dbg(&spi
->dev
, "9 <= n <= 16 bits per word\n");
1614 chip
->read
= READING_U16
;
1615 chip
->write
= WRITING_U16
;
1617 if (pl022
->vendor
->max_bpw
>= 32) {
1618 dev_dbg(&spi
->dev
, "17 <= n <= 32 bits per word\n");
1620 chip
->read
= READING_U32
;
1621 chip
->write
= WRITING_U32
;
1624 "illegal data size for this controller!\n");
1626 "a standard pl022 can only handle "
1627 "1 <= n <= 16 bit words\n");
1628 goto err_config_params
;
1632 /* Now Initialize all register settings required for this chip */
1637 if ((chip_info
->com_mode
== DMA_TRANSFER
)
1638 && ((pl022
->master_info
)->enable_dma
)) {
1639 chip
->enable_dma
= 1;
1640 dev_dbg(&spi
->dev
, "DMA mode set in controller state\n");
1641 status
= process_dma_info(chip_info
, chip
);
1643 goto err_config_params
;
1644 SSP_WRITE_BITS(chip
->dmacr
, SSP_DMA_ENABLED
,
1645 SSP_DMACR_MASK_RXDMAE
, 0);
1646 SSP_WRITE_BITS(chip
->dmacr
, SSP_DMA_ENABLED
,
1647 SSP_DMACR_MASK_TXDMAE
, 1);
1649 chip
->enable_dma
= 0;
1650 dev_dbg(&spi
->dev
, "DMA mode NOT set in controller state\n");
1651 SSP_WRITE_BITS(chip
->dmacr
, SSP_DMA_DISABLED
,
1652 SSP_DMACR_MASK_RXDMAE
, 0);
1653 SSP_WRITE_BITS(chip
->dmacr
, SSP_DMA_DISABLED
,
1654 SSP_DMACR_MASK_TXDMAE
, 1);
1657 chip
->cpsr
= chip_info
->clk_freq
.cpsdvsr
;
1659 /* Special setup for the ST micro extended control registers */
1660 if (pl022
->vendor
->extended_cr
) {
1661 if (pl022
->vendor
->pl023
) {
1662 /* These bits are only in the PL023 */
1663 SSP_WRITE_BITS(chip
->cr1
, chip_info
->clkdelay
,
1664 SSP_CR1_MASK_FBCLKDEL_ST
, 13);
1666 /* These bits are in the PL022 but not PL023 */
1667 SSP_WRITE_BITS(chip
->cr0
, chip_info
->duplex
,
1668 SSP_CR0_MASK_HALFDUP_ST
, 5);
1669 SSP_WRITE_BITS(chip
->cr0
, chip_info
->ctrl_len
,
1670 SSP_CR0_MASK_CSS_ST
, 16);
1671 SSP_WRITE_BITS(chip
->cr0
, chip_info
->iface
,
1672 SSP_CR0_MASK_FRF_ST
, 21);
1673 SSP_WRITE_BITS(chip
->cr1
, chip_info
->wait_state
,
1674 SSP_CR1_MASK_MWAIT_ST
, 6);
1676 SSP_WRITE_BITS(chip
->cr0
, chip_info
->data_size
,
1677 SSP_CR0_MASK_DSS_ST
, 0);
1678 SSP_WRITE_BITS(chip
->cr1
, chip_info
->endian_rx
,
1679 SSP_CR1_MASK_RENDN_ST
, 4);
1680 SSP_WRITE_BITS(chip
->cr1
, chip_info
->endian_tx
,
1681 SSP_CR1_MASK_TENDN_ST
, 5);
1682 SSP_WRITE_BITS(chip
->cr1
, chip_info
->rx_lev_trig
,
1683 SSP_CR1_MASK_RXIFLSEL_ST
, 7);
1684 SSP_WRITE_BITS(chip
->cr1
, chip_info
->tx_lev_trig
,
1685 SSP_CR1_MASK_TXIFLSEL_ST
, 10);
1687 SSP_WRITE_BITS(chip
->cr0
, chip_info
->data_size
,
1688 SSP_CR0_MASK_DSS
, 0);
1689 SSP_WRITE_BITS(chip
->cr0
, chip_info
->iface
,
1690 SSP_CR0_MASK_FRF
, 4);
1692 /* Stuff that is common for all versions */
1693 SSP_WRITE_BITS(chip
->cr0
, chip_info
->clk_pol
, SSP_CR0_MASK_SPO
, 6);
1694 SSP_WRITE_BITS(chip
->cr0
, chip_info
->clk_phase
, SSP_CR0_MASK_SPH
, 7);
1695 SSP_WRITE_BITS(chip
->cr0
, chip_info
->clk_freq
.scr
, SSP_CR0_MASK_SCR
, 8);
1696 /* Loopback is available on all versions except PL023 */
1697 if (!pl022
->vendor
->pl023
)
1698 SSP_WRITE_BITS(chip
->cr1
, chip_info
->lbm
, SSP_CR1_MASK_LBM
, 0);
1699 SSP_WRITE_BITS(chip
->cr1
, SSP_DISABLED
, SSP_CR1_MASK_SSE
, 1);
1700 SSP_WRITE_BITS(chip
->cr1
, chip_info
->hierarchy
, SSP_CR1_MASK_MS
, 2);
1701 SSP_WRITE_BITS(chip
->cr1
, chip_info
->slave_tx_disable
, SSP_CR1_MASK_SOD
, 3);
1703 /* Save controller_state */
1704 spi_set_ctldata(spi
, chip
);
1713 * pl022_cleanup - cleanup function registered to SPI master framework
1714 * @spi: spi device which is requesting cleanup
1716 * This function is registered to the SPI framework for this SPI master
1717 * controller. It will free the runtime state of chip.
1719 static void pl022_cleanup(struct spi_device
*spi
)
1721 struct chip_data
*chip
= spi_get_ctldata(spi
);
1723 spi_set_ctldata(spi
, NULL
);
1728 static int __devinit
1729 pl022_probe(struct amba_device
*adev
, struct amba_id
*id
)
1731 struct device
*dev
= &adev
->dev
;
1732 struct pl022_ssp_controller
*platform_info
= adev
->dev
.platform_data
;
1733 struct spi_master
*master
;
1734 struct pl022
*pl022
= NULL
; /*Data for this driver */
1737 dev_info(&adev
->dev
,
1738 "ARM PL022 driver, device ID: 0x%08x\n", adev
->periphid
);
1739 if (platform_info
== NULL
) {
1740 dev_err(&adev
->dev
, "probe - no platform data supplied\n");
1745 /* Allocate master with space for data */
1746 master
= spi_alloc_master(dev
, sizeof(struct pl022
));
1747 if (master
== NULL
) {
1748 dev_err(&adev
->dev
, "probe - cannot alloc SPI master\n");
1753 pl022
= spi_master_get_devdata(master
);
1754 pl022
->master
= master
;
1755 pl022
->master_info
= platform_info
;
1757 pl022
->vendor
= id
->data
;
1760 * Bus Number Which has been Assigned to this SSP controller
1763 master
->bus_num
= platform_info
->bus_id
;
1764 master
->num_chipselect
= platform_info
->num_chipselect
;
1765 master
->cleanup
= pl022_cleanup
;
1766 master
->setup
= pl022_setup
;
1767 master
->transfer
= pl022_transfer
;
1769 dev_dbg(&adev
->dev
, "BUSNO: %d\n", master
->bus_num
);
1771 status
= amba_request_regions(adev
, NULL
);
1773 goto err_no_ioregion
;
1775 pl022
->virtbase
= ioremap(adev
->res
.start
, resource_size(&adev
->res
));
1776 if (pl022
->virtbase
== NULL
) {
1778 goto err_no_ioremap
;
1780 printk(KERN_INFO
"pl022: mapped registers from 0x%08x to %p\n",
1781 adev
->res
.start
, pl022
->virtbase
);
1783 pl022
->clk
= clk_get(&adev
->dev
, NULL
);
1784 if (IS_ERR(pl022
->clk
)) {
1785 status
= PTR_ERR(pl022
->clk
);
1786 dev_err(&adev
->dev
, "could not retrieve SSP/SPI bus clock\n");
1791 writew((readw(SSP_CR1(pl022
->virtbase
)) & (~SSP_CR1_MASK_SSE
)),
1792 SSP_CR1(pl022
->virtbase
));
1793 load_ssp_default_config(pl022
);
1795 status
= request_irq(adev
->irq
[0], pl022_interrupt_handler
, 0, "pl022",
1798 dev_err(&adev
->dev
, "probe - cannot get IRQ (%d)\n", status
);
1801 /* Initialize and start queue */
1802 status
= init_queue(pl022
);
1804 dev_err(&adev
->dev
, "probe - problem initializing queue\n");
1805 goto err_init_queue
;
1807 status
= start_queue(pl022
);
1809 dev_err(&adev
->dev
, "probe - problem starting queue\n");
1810 goto err_start_queue
;
1812 /* Register with the SPI framework */
1813 amba_set_drvdata(adev
, pl022
);
1814 status
= spi_register_master(master
);
1817 "probe - problem registering spi master\n");
1818 goto err_spi_register
;
1820 dev_dbg(dev
, "probe succeded\n");
1821 /* Disable the silicon block pclk and clock it when needed */
1822 amba_pclk_disable(adev
);
1828 destroy_queue(pl022
);
1829 free_irq(adev
->irq
[0], pl022
);
1831 clk_put(pl022
->clk
);
1833 iounmap(pl022
->virtbase
);
1835 amba_release_regions(adev
);
1837 spi_master_put(master
);
1843 static int __devexit
1844 pl022_remove(struct amba_device
*adev
)
1846 struct pl022
*pl022
= amba_get_drvdata(adev
);
1851 /* Remove the queue */
1852 status
= destroy_queue(pl022
);
1855 "queue remove failed (%d)\n", status
);
1858 load_ssp_default_config(pl022
);
1859 free_irq(adev
->irq
[0], pl022
);
1860 clk_disable(pl022
->clk
);
1861 clk_put(pl022
->clk
);
1862 iounmap(pl022
->virtbase
);
1863 amba_release_regions(adev
);
1864 tasklet_disable(&pl022
->pump_transfers
);
1865 spi_unregister_master(pl022
->master
);
1866 spi_master_put(pl022
->master
);
1867 amba_set_drvdata(adev
, NULL
);
1868 dev_dbg(&adev
->dev
, "remove succeded\n");
1873 static int pl022_suspend(struct amba_device
*adev
, pm_message_t state
)
1875 struct pl022
*pl022
= amba_get_drvdata(adev
);
1878 status
= stop_queue(pl022
);
1880 dev_warn(&adev
->dev
, "suspend cannot stop queue\n");
1884 amba_pclk_enable(adev
);
1885 load_ssp_default_config(pl022
);
1886 amba_pclk_disable(adev
);
1887 dev_dbg(&adev
->dev
, "suspended\n");
1891 static int pl022_resume(struct amba_device
*adev
)
1893 struct pl022
*pl022
= amba_get_drvdata(adev
);
1896 /* Start the queue running */
1897 status
= start_queue(pl022
);
1899 dev_err(&adev
->dev
, "problem starting queue (%d)\n", status
);
1901 dev_dbg(&adev
->dev
, "resumed\n");
1906 #define pl022_suspend NULL
1907 #define pl022_resume NULL
1908 #endif /* CONFIG_PM */
1910 static struct vendor_data vendor_arm
= {
1914 .extended_cr
= false,
1919 static struct vendor_data vendor_st
= {
1923 .extended_cr
= true,
1927 static struct vendor_data vendor_st_pl023
= {
1931 .extended_cr
= true,
1935 static struct amba_id pl022_ids
[] = {
1938 * ARM PL022 variant, this has a 16bit wide
1939 * and 8 locations deep TX/RX FIFO
1943 .data
= &vendor_arm
,
1947 * ST Micro derivative, this has 32bit wide
1948 * and 32 locations deep TX/RX FIFO
1956 * ST-Ericsson derivative "PL023" (this is not
1957 * an official ARM number), this is a PL022 SSP block
1958 * stripped to SPI mode only, it has 32bit wide
1959 * and 32 locations deep TX/RX FIFO but no extended
1964 .data
= &vendor_st_pl023
,
1969 static struct amba_driver pl022_driver
= {
1971 .name
= "ssp-pl022",
1973 .id_table
= pl022_ids
,
1974 .probe
= pl022_probe
,
1975 .remove
= __devexit_p(pl022_remove
),
1976 .suspend
= pl022_suspend
,
1977 .resume
= pl022_resume
,
1981 static int __init
pl022_init(void)
1983 return amba_driver_register(&pl022_driver
);
1986 subsys_initcall(pl022_init
);
1988 static void __exit
pl022_exit(void)
1990 amba_driver_unregister(&pl022_driver
);
1993 module_exit(pl022_exit
);
1995 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
1996 MODULE_DESCRIPTION("PL022 SSP Controller Driver");
1997 MODULE_LICENSE("GPL");