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