spi: pl022: Drop custom per-chip cs_control
[linux-2.6-microblaze.git] / drivers / spi / spi-pl022.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * A driver for the ARM PL022 PrimeCell SSP/SPI bus master.
4  *
5  * Copyright (C) 2008-2012 ST-Ericsson AB
6  * Copyright (C) 2006 STMicroelectronics Pvt. Ltd.
7  *
8  * Author: Linus Walleij <linus.walleij@stericsson.com>
9  *
10  * Initial version inspired by:
11  *      linux-2.6.17-rc3-mm1/drivers/spi/pxa2xx_spi.c
12  * Initial adoption to PL022 by:
13  *      Sachin Verma <sachin.verma@st.com>
14  */
15
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/device.h>
19 #include <linux/ioport.h>
20 #include <linux/errno.h>
21 #include <linux/interrupt.h>
22 #include <linux/spi/spi.h>
23 #include <linux/delay.h>
24 #include <linux/clk.h>
25 #include <linux/err.h>
26 #include <linux/amba/bus.h>
27 #include <linux/amba/pl022.h>
28 #include <linux/io.h>
29 #include <linux/slab.h>
30 #include <linux/dmaengine.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/scatterlist.h>
33 #include <linux/pm_runtime.h>
34 #include <linux/gpio.h>
35 #include <linux/of_gpio.h>
36 #include <linux/pinctrl/consumer.h>
37
38 /*
39  * This macro is used to define some register default values.
40  * reg is masked with mask, the OR:ed with an (again masked)
41  * val shifted sb steps to the left.
42  */
43 #define SSP_WRITE_BITS(reg, val, mask, sb) \
44  ((reg) = (((reg) & ~(mask)) | (((val)<<(sb)) & (mask))))
45
46 /*
47  * This macro is also used to define some default values.
48  * It will just shift val by sb steps to the left and mask
49  * the result with mask.
50  */
51 #define GEN_MASK_BITS(val, mask, sb) \
52  (((val)<<(sb)) & (mask))
53
54 #define DRIVE_TX                0
55 #define DO_NOT_DRIVE_TX         1
56
57 #define DO_NOT_QUEUE_DMA        0
58 #define QUEUE_DMA               1
59
60 #define RX_TRANSFER             1
61 #define TX_TRANSFER             2
62
63 /*
64  * Macros to access SSP Registers with their offsets
65  */
66 #define SSP_CR0(r)      (r + 0x000)
67 #define SSP_CR1(r)      (r + 0x004)
68 #define SSP_DR(r)       (r + 0x008)
69 #define SSP_SR(r)       (r + 0x00C)
70 #define SSP_CPSR(r)     (r + 0x010)
71 #define SSP_IMSC(r)     (r + 0x014)
72 #define SSP_RIS(r)      (r + 0x018)
73 #define SSP_MIS(r)      (r + 0x01C)
74 #define SSP_ICR(r)      (r + 0x020)
75 #define SSP_DMACR(r)    (r + 0x024)
76 #define SSP_CSR(r)      (r + 0x030) /* vendor extension */
77 #define SSP_ITCR(r)     (r + 0x080)
78 #define SSP_ITIP(r)     (r + 0x084)
79 #define SSP_ITOP(r)     (r + 0x088)
80 #define SSP_TDR(r)      (r + 0x08C)
81
82 #define SSP_PID0(r)     (r + 0xFE0)
83 #define SSP_PID1(r)     (r + 0xFE4)
84 #define SSP_PID2(r)     (r + 0xFE8)
85 #define SSP_PID3(r)     (r + 0xFEC)
86
87 #define SSP_CID0(r)     (r + 0xFF0)
88 #define SSP_CID1(r)     (r + 0xFF4)
89 #define SSP_CID2(r)     (r + 0xFF8)
90 #define SSP_CID3(r)     (r + 0xFFC)
91
92 /*
93  * SSP Control Register 0  - SSP_CR0
94  */
95 #define SSP_CR0_MASK_DSS        (0x0FUL << 0)
96 #define SSP_CR0_MASK_FRF        (0x3UL << 4)
97 #define SSP_CR0_MASK_SPO        (0x1UL << 6)
98 #define SSP_CR0_MASK_SPH        (0x1UL << 7)
99 #define SSP_CR0_MASK_SCR        (0xFFUL << 8)
100
101 /*
102  * The ST version of this block moves som bits
103  * in SSP_CR0 and extends it to 32 bits
104  */
105 #define SSP_CR0_MASK_DSS_ST     (0x1FUL << 0)
106 #define SSP_CR0_MASK_HALFDUP_ST (0x1UL << 5)
107 #define SSP_CR0_MASK_CSS_ST     (0x1FUL << 16)
108 #define SSP_CR0_MASK_FRF_ST     (0x3UL << 21)
109
110 /*
111  * SSP Control Register 0  - SSP_CR1
112  */
113 #define SSP_CR1_MASK_LBM        (0x1UL << 0)
114 #define SSP_CR1_MASK_SSE        (0x1UL << 1)
115 #define SSP_CR1_MASK_MS         (0x1UL << 2)
116 #define SSP_CR1_MASK_SOD        (0x1UL << 3)
117
118 /*
119  * The ST version of this block adds some bits
120  * in SSP_CR1
121  */
122 #define SSP_CR1_MASK_RENDN_ST   (0x1UL << 4)
123 #define SSP_CR1_MASK_TENDN_ST   (0x1UL << 5)
124 #define SSP_CR1_MASK_MWAIT_ST   (0x1UL << 6)
125 #define SSP_CR1_MASK_RXIFLSEL_ST (0x7UL << 7)
126 #define SSP_CR1_MASK_TXIFLSEL_ST (0x7UL << 10)
127 /* This one is only in the PL023 variant */
128 #define SSP_CR1_MASK_FBCLKDEL_ST (0x7UL << 13)
129
130 /*
131  * SSP Status Register - SSP_SR
132  */
133 #define SSP_SR_MASK_TFE         (0x1UL << 0) /* Transmit FIFO empty */
134 #define SSP_SR_MASK_TNF         (0x1UL << 1) /* Transmit FIFO not full */
135 #define SSP_SR_MASK_RNE         (0x1UL << 2) /* Receive FIFO not empty */
136 #define SSP_SR_MASK_RFF         (0x1UL << 3) /* Receive FIFO full */
137 #define SSP_SR_MASK_BSY         (0x1UL << 4) /* Busy Flag */
138
139 /*
140  * SSP Clock Prescale Register  - SSP_CPSR
141  */
142 #define SSP_CPSR_MASK_CPSDVSR   (0xFFUL << 0)
143
144 /*
145  * SSP Interrupt Mask Set/Clear Register - SSP_IMSC
146  */
147 #define SSP_IMSC_MASK_RORIM (0x1UL << 0) /* Receive Overrun Interrupt mask */
148 #define SSP_IMSC_MASK_RTIM  (0x1UL << 1) /* Receive timeout Interrupt mask */
149 #define SSP_IMSC_MASK_RXIM  (0x1UL << 2) /* Receive FIFO Interrupt mask */
150 #define SSP_IMSC_MASK_TXIM  (0x1UL << 3) /* Transmit FIFO Interrupt mask */
151
152 /*
153  * SSP Raw Interrupt Status Register - SSP_RIS
154  */
155 /* Receive Overrun Raw Interrupt status */
156 #define SSP_RIS_MASK_RORRIS             (0x1UL << 0)
157 /* Receive Timeout Raw Interrupt status */
158 #define SSP_RIS_MASK_RTRIS              (0x1UL << 1)
159 /* Receive FIFO Raw Interrupt status */
160 #define SSP_RIS_MASK_RXRIS              (0x1UL << 2)
161 /* Transmit FIFO Raw Interrupt status */
162 #define SSP_RIS_MASK_TXRIS              (0x1UL << 3)
163
164 /*
165  * SSP Masked Interrupt Status Register - SSP_MIS
166  */
167 /* Receive Overrun Masked Interrupt status */
168 #define SSP_MIS_MASK_RORMIS             (0x1UL << 0)
169 /* Receive Timeout Masked Interrupt status */
170 #define SSP_MIS_MASK_RTMIS              (0x1UL << 1)
171 /* Receive FIFO Masked Interrupt status */
172 #define SSP_MIS_MASK_RXMIS              (0x1UL << 2)
173 /* Transmit FIFO Masked Interrupt status */
174 #define SSP_MIS_MASK_TXMIS              (0x1UL << 3)
175
176 /*
177  * SSP Interrupt Clear Register - SSP_ICR
178  */
179 /* Receive Overrun Raw Clear Interrupt bit */
180 #define SSP_ICR_MASK_RORIC              (0x1UL << 0)
181 /* Receive Timeout Clear Interrupt bit */
182 #define SSP_ICR_MASK_RTIC               (0x1UL << 1)
183
184 /*
185  * SSP DMA Control Register - SSP_DMACR
186  */
187 /* Receive DMA Enable bit */
188 #define SSP_DMACR_MASK_RXDMAE           (0x1UL << 0)
189 /* Transmit DMA Enable bit */
190 #define SSP_DMACR_MASK_TXDMAE           (0x1UL << 1)
191
192 /*
193  * SSP Chip Select Control Register - SSP_CSR
194  * (vendor extension)
195  */
196 #define SSP_CSR_CSVALUE_MASK            (0x1FUL << 0)
197
198 /*
199  * SSP Integration Test control Register - SSP_ITCR
200  */
201 #define SSP_ITCR_MASK_ITEN              (0x1UL << 0)
202 #define SSP_ITCR_MASK_TESTFIFO          (0x1UL << 1)
203
204 /*
205  * SSP Integration Test Input Register - SSP_ITIP
206  */
207 #define ITIP_MASK_SSPRXD                 (0x1UL << 0)
208 #define ITIP_MASK_SSPFSSIN               (0x1UL << 1)
209 #define ITIP_MASK_SSPCLKIN               (0x1UL << 2)
210 #define ITIP_MASK_RXDMAC                 (0x1UL << 3)
211 #define ITIP_MASK_TXDMAC                 (0x1UL << 4)
212 #define ITIP_MASK_SSPTXDIN               (0x1UL << 5)
213
214 /*
215  * SSP Integration Test output Register - SSP_ITOP
216  */
217 #define ITOP_MASK_SSPTXD                 (0x1UL << 0)
218 #define ITOP_MASK_SSPFSSOUT              (0x1UL << 1)
219 #define ITOP_MASK_SSPCLKOUT              (0x1UL << 2)
220 #define ITOP_MASK_SSPOEn                 (0x1UL << 3)
221 #define ITOP_MASK_SSPCTLOEn              (0x1UL << 4)
222 #define ITOP_MASK_RORINTR                (0x1UL << 5)
223 #define ITOP_MASK_RTINTR                 (0x1UL << 6)
224 #define ITOP_MASK_RXINTR                 (0x1UL << 7)
225 #define ITOP_MASK_TXINTR                 (0x1UL << 8)
226 #define ITOP_MASK_INTR                   (0x1UL << 9)
227 #define ITOP_MASK_RXDMABREQ              (0x1UL << 10)
228 #define ITOP_MASK_RXDMASREQ              (0x1UL << 11)
229 #define ITOP_MASK_TXDMABREQ              (0x1UL << 12)
230 #define ITOP_MASK_TXDMASREQ              (0x1UL << 13)
231
232 /*
233  * SSP Test Data Register - SSP_TDR
234  */
235 #define TDR_MASK_TESTDATA               (0xFFFFFFFF)
236
237 /*
238  * Message State
239  * we use the spi_message.state (void *) pointer to
240  * hold a single state value, that's why all this
241  * (void *) casting is done here.
242  */
243 #define STATE_START                     ((void *) 0)
244 #define STATE_RUNNING                   ((void *) 1)
245 #define STATE_DONE                      ((void *) 2)
246 #define STATE_ERROR                     ((void *) -1)
247 #define STATE_TIMEOUT                   ((void *) -2)
248
249 /*
250  * SSP State - Whether Enabled or Disabled
251  */
252 #define SSP_DISABLED                    (0)
253 #define SSP_ENABLED                     (1)
254
255 /*
256  * SSP DMA State - Whether DMA Enabled or Disabled
257  */
258 #define SSP_DMA_DISABLED                (0)
259 #define SSP_DMA_ENABLED                 (1)
260
261 /*
262  * SSP Clock Defaults
263  */
264 #define SSP_DEFAULT_CLKRATE 0x2
265 #define SSP_DEFAULT_PRESCALE 0x40
266
267 /*
268  * SSP Clock Parameter ranges
269  */
270 #define CPSDVR_MIN 0x02
271 #define CPSDVR_MAX 0xFE
272 #define SCR_MIN 0x00
273 #define SCR_MAX 0xFF
274
275 /*
276  * SSP Interrupt related Macros
277  */
278 #define DEFAULT_SSP_REG_IMSC  0x0UL
279 #define DISABLE_ALL_INTERRUPTS DEFAULT_SSP_REG_IMSC
280 #define ENABLE_ALL_INTERRUPTS ( \
281         SSP_IMSC_MASK_RORIM | \
282         SSP_IMSC_MASK_RTIM | \
283         SSP_IMSC_MASK_RXIM | \
284         SSP_IMSC_MASK_TXIM \
285 )
286
287 #define CLEAR_ALL_INTERRUPTS  0x3
288
289 #define SPI_POLLING_TIMEOUT 1000
290
291 /*
292  * The type of reading going on on this chip
293  */
294 enum ssp_reading {
295         READING_NULL,
296         READING_U8,
297         READING_U16,
298         READING_U32
299 };
300
301 /*
302  * The type of writing going on on this chip
303  */
304 enum ssp_writing {
305         WRITING_NULL,
306         WRITING_U8,
307         WRITING_U16,
308         WRITING_U32
309 };
310
311 /**
312  * struct vendor_data - vendor-specific config parameters
313  * for PL022 derivates
314  * @fifodepth: depth of FIFOs (both)
315  * @max_bpw: maximum number of bits per word
316  * @unidir: supports unidirection transfers
317  * @extended_cr: 32 bit wide control register 0 with extra
318  * features and extra features in CR1 as found in the ST variants
319  * @pl023: supports a subset of the ST extensions called "PL023"
320  * @loopback: supports loopback mode
321  * @internal_cs_ctrl: supports chip select control register
322  */
323 struct vendor_data {
324         int fifodepth;
325         int max_bpw;
326         bool unidir;
327         bool extended_cr;
328         bool pl023;
329         bool loopback;
330         bool internal_cs_ctrl;
331 };
332
333 /**
334  * struct pl022 - This is the private SSP driver data structure
335  * @adev: AMBA device model hookup
336  * @vendor: vendor data for the IP block
337  * @phybase: the physical memory where the SSP device resides
338  * @virtbase: the virtual memory where the SSP is mapped
339  * @clk: outgoing clock "SPICLK" for the SPI bus
340  * @master: SPI framework hookup
341  * @master_info: controller-specific data from machine setup
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  * @next_msg_cs_active: the next message in the queue has been examined
347  *  and it was found that it uses the same chip select as the previous
348  *  message, so we left it active after the previous transfer, and it's
349  *  active already.
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  * @read: the type of read currently going on
355  * @write: the type of write currently going on
356  * @exp_fifo_level: expected FIFO level
357  * @rx_lev_trig: receive FIFO watermark level which triggers IRQ
358  * @tx_lev_trig: transmit FIFO watermark level which triggers IRQ
359  * @dma_rx_channel: optional channel for RX DMA
360  * @dma_tx_channel: optional channel for TX DMA
361  * @sgt_rx: scattertable for the RX transfer
362  * @sgt_tx: scattertable for the TX transfer
363  * @dummypage: a dummy page used for driving data on the bus with DMA
364  * @dma_running: indicates whether DMA is in operation
365  * @cur_cs: current chip select (gpio)
366  * @chipselects: list of chipselects (gpios)
367  */
368 struct pl022 {
369         struct amba_device              *adev;
370         struct vendor_data              *vendor;
371         resource_size_t                 phybase;
372         void __iomem                    *virtbase;
373         struct clk                      *clk;
374         struct spi_master               *master;
375         struct pl022_ssp_controller     *master_info;
376         /* Message per-transfer pump */
377         struct tasklet_struct           pump_transfers;
378         struct spi_message              *cur_msg;
379         struct spi_transfer             *cur_transfer;
380         struct chip_data                *cur_chip;
381         bool                            next_msg_cs_active;
382         void                            *tx;
383         void                            *tx_end;
384         void                            *rx;
385         void                            *rx_end;
386         enum ssp_reading                read;
387         enum ssp_writing                write;
388         u32                             exp_fifo_level;
389         enum ssp_rx_level_trig          rx_lev_trig;
390         enum ssp_tx_level_trig          tx_lev_trig;
391         /* DMA settings */
392 #ifdef CONFIG_DMA_ENGINE
393         struct dma_chan                 *dma_rx_channel;
394         struct dma_chan                 *dma_tx_channel;
395         struct sg_table                 sgt_rx;
396         struct sg_table                 sgt_tx;
397         char                            *dummypage;
398         bool                            dma_running;
399 #endif
400         int cur_cs;
401         int *chipselects;
402 };
403
404 /**
405  * struct chip_data - To maintain runtime state of SSP for each client chip
406  * @cr0: Value of control register CR0 of SSP - on later ST variants this
407  *       register is 32 bits wide rather than just 16
408  * @cr1: Value of control register CR1 of SSP
409  * @dmacr: Value of DMA control Register of SSP
410  * @cpsr: Value of Clock prescale register
411  * @n_bytes: how many bytes(power of 2) reqd for a given data width of client
412  * @enable_dma: Whether to enable DMA or not
413  * @read: function ptr to be used to read when doing xfer for this chip
414  * @write: function ptr to be used to write when doing xfer for this chip
415  * @xfer_type: polling/interrupt/DMA
416  *
417  * Runtime state of the SSP controller, maintained per chip,
418  * This would be set according to the current message that would be served
419  */
420 struct chip_data {
421         u32 cr0;
422         u16 cr1;
423         u16 dmacr;
424         u16 cpsr;
425         u8 n_bytes;
426         bool enable_dma;
427         enum ssp_reading read;
428         enum ssp_writing write;
429         int xfer_type;
430 };
431
432 /**
433  * internal_cs_control - Control chip select signals via SSP_CSR.
434  * @pl022: SSP driver private data structure
435  * @command: select/delect the chip
436  *
437  * Used on controller with internal chip select control via SSP_CSR register
438  * (vendor extension). Each of the 5 LSB in the register controls one chip
439  * select signal.
440  */
441 static void internal_cs_control(struct pl022 *pl022, u32 command)
442 {
443         u32 tmp;
444
445         tmp = readw(SSP_CSR(pl022->virtbase));
446         if (command == SSP_CHIP_SELECT)
447                 tmp &= ~BIT(pl022->cur_cs);
448         else
449                 tmp |= BIT(pl022->cur_cs);
450         writew(tmp, SSP_CSR(pl022->virtbase));
451 }
452
453 static void pl022_cs_control(struct pl022 *pl022, u32 command)
454 {
455         if (pl022->vendor->internal_cs_ctrl)
456                 internal_cs_control(pl022, command);
457         else if (gpio_is_valid(pl022->cur_cs))
458                 gpio_set_value(pl022->cur_cs, command);
459 }
460
461 /**
462  * giveback - current spi_message is over, schedule next message and call
463  * callback of this message. Assumes that caller already
464  * set message->status; dma and pio irqs are blocked
465  * @pl022: SSP driver private data structure
466  */
467 static void giveback(struct pl022 *pl022)
468 {
469         struct spi_transfer *last_transfer;
470         pl022->next_msg_cs_active = false;
471
472         last_transfer = list_last_entry(&pl022->cur_msg->transfers,
473                                         struct spi_transfer, transfer_list);
474
475         /* Delay if requested before any change in chip select */
476         /*
477          * FIXME: This runs in interrupt context.
478          * Is this really smart?
479          */
480         spi_transfer_delay_exec(last_transfer);
481
482         if (!last_transfer->cs_change) {
483                 struct spi_message *next_msg;
484
485                 /*
486                  * cs_change was not set. We can keep the chip select
487                  * enabled if there is message in the queue and it is
488                  * for the same spi device.
489                  *
490                  * We cannot postpone this until pump_messages, because
491                  * after calling msg->complete (below) the driver that
492                  * sent the current message could be unloaded, which
493                  * could invalidate the cs_control() callback...
494                  */
495                 /* get a pointer to the next message, if any */
496                 next_msg = spi_get_next_queued_message(pl022->master);
497
498                 /*
499                  * see if the next and current messages point
500                  * to the same spi device.
501                  */
502                 if (next_msg && next_msg->spi != pl022->cur_msg->spi)
503                         next_msg = NULL;
504                 if (!next_msg || pl022->cur_msg->state == STATE_ERROR)
505                         pl022_cs_control(pl022, SSP_CHIP_DESELECT);
506                 else
507                         pl022->next_msg_cs_active = true;
508
509         }
510
511         pl022->cur_msg = NULL;
512         pl022->cur_transfer = NULL;
513         pl022->cur_chip = NULL;
514
515         /* disable the SPI/SSP operation */
516         writew((readw(SSP_CR1(pl022->virtbase)) &
517                 (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
518
519         spi_finalize_current_message(pl022->master);
520 }
521
522 /**
523  * flush - flush the FIFO to reach a clean state
524  * @pl022: SSP driver private data structure
525  */
526 static int flush(struct pl022 *pl022)
527 {
528         unsigned long limit = loops_per_jiffy << 1;
529
530         dev_dbg(&pl022->adev->dev, "flush\n");
531         do {
532                 while (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
533                         readw(SSP_DR(pl022->virtbase));
534         } while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_BSY) && limit--);
535
536         pl022->exp_fifo_level = 0;
537
538         return limit;
539 }
540
541 /**
542  * restore_state - Load configuration of current chip
543  * @pl022: SSP driver private data structure
544  */
545 static void restore_state(struct pl022 *pl022)
546 {
547         struct chip_data *chip = pl022->cur_chip;
548
549         if (pl022->vendor->extended_cr)
550                 writel(chip->cr0, SSP_CR0(pl022->virtbase));
551         else
552                 writew(chip->cr0, SSP_CR0(pl022->virtbase));
553         writew(chip->cr1, SSP_CR1(pl022->virtbase));
554         writew(chip->dmacr, SSP_DMACR(pl022->virtbase));
555         writew(chip->cpsr, SSP_CPSR(pl022->virtbase));
556         writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
557         writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
558 }
559
560 /*
561  * Default SSP Register Values
562  */
563 #define DEFAULT_SSP_REG_CR0 ( \
564         GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS, 0)    | \
565         GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF, 4) | \
566         GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
567         GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
568         GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
569 )
570
571 /* ST versions have slightly different bit layout */
572 #define DEFAULT_SSP_REG_CR0_ST ( \
573         GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
574         GEN_MASK_BITS(SSP_MICROWIRE_CHANNEL_FULL_DUPLEX, SSP_CR0_MASK_HALFDUP_ST, 5) | \
575         GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
576         GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
577         GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) | \
578         GEN_MASK_BITS(SSP_BITS_8, SSP_CR0_MASK_CSS_ST, 16)      | \
579         GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF_ST, 21) \
580 )
581
582 /* The PL023 version is slightly different again */
583 #define DEFAULT_SSP_REG_CR0_ST_PL023 ( \
584         GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
585         GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
586         GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
587         GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
588 )
589
590 #define DEFAULT_SSP_REG_CR1 ( \
591         GEN_MASK_BITS(LOOPBACK_DISABLED, SSP_CR1_MASK_LBM, 0) | \
592         GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
593         GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
594         GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) \
595 )
596
597 /* ST versions extend this register to use all 16 bits */
598 #define DEFAULT_SSP_REG_CR1_ST ( \
599         DEFAULT_SSP_REG_CR1 | \
600         GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
601         GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
602         GEN_MASK_BITS(SSP_MWIRE_WAIT_ZERO, SSP_CR1_MASK_MWAIT_ST, 6) |\
603         GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
604         GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) \
605 )
606
607 /*
608  * The PL023 variant has further differences: no loopback mode, no microwire
609  * support, and a new clock feedback delay setting.
610  */
611 #define DEFAULT_SSP_REG_CR1_ST_PL023 ( \
612         GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
613         GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
614         GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) | \
615         GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
616         GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
617         GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
618         GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) | \
619         GEN_MASK_BITS(SSP_FEEDBACK_CLK_DELAY_NONE, SSP_CR1_MASK_FBCLKDEL_ST, 13) \
620 )
621
622 #define DEFAULT_SSP_REG_CPSR ( \
623         GEN_MASK_BITS(SSP_DEFAULT_PRESCALE, SSP_CPSR_MASK_CPSDVSR, 0) \
624 )
625
626 #define DEFAULT_SSP_REG_DMACR (\
627         GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_RXDMAE, 0) | \
628         GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_TXDMAE, 1) \
629 )
630
631 /**
632  * load_ssp_default_config - Load default configuration for SSP
633  * @pl022: SSP driver private data structure
634  */
635 static void load_ssp_default_config(struct pl022 *pl022)
636 {
637         if (pl022->vendor->pl023) {
638                 writel(DEFAULT_SSP_REG_CR0_ST_PL023, SSP_CR0(pl022->virtbase));
639                 writew(DEFAULT_SSP_REG_CR1_ST_PL023, SSP_CR1(pl022->virtbase));
640         } else if (pl022->vendor->extended_cr) {
641                 writel(DEFAULT_SSP_REG_CR0_ST, SSP_CR0(pl022->virtbase));
642                 writew(DEFAULT_SSP_REG_CR1_ST, SSP_CR1(pl022->virtbase));
643         } else {
644                 writew(DEFAULT_SSP_REG_CR0, SSP_CR0(pl022->virtbase));
645                 writew(DEFAULT_SSP_REG_CR1, SSP_CR1(pl022->virtbase));
646         }
647         writew(DEFAULT_SSP_REG_DMACR, SSP_DMACR(pl022->virtbase));
648         writew(DEFAULT_SSP_REG_CPSR, SSP_CPSR(pl022->virtbase));
649         writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
650         writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
651 }
652
653 /*
654  * This will write to TX and read from RX according to the parameters
655  * set in pl022.
656  */
657 static void readwriter(struct pl022 *pl022)
658 {
659
660         /*
661          * The FIFO depth is different between primecell variants.
662          * I believe filling in too much in the FIFO might cause
663          * errons in 8bit wide transfers on ARM variants (just 8 words
664          * FIFO, means only 8x8 = 64 bits in FIFO) at least.
665          *
666          * To prevent this issue, the TX FIFO is only filled to the
667          * unused RX FIFO fill length, regardless of what the TX
668          * FIFO status flag indicates.
669          */
670         dev_dbg(&pl022->adev->dev,
671                 "%s, rx: %p, rxend: %p, tx: %p, txend: %p\n",
672                 __func__, pl022->rx, pl022->rx_end, pl022->tx, pl022->tx_end);
673
674         /* Read as much as you can */
675         while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
676                && (pl022->rx < pl022->rx_end)) {
677                 switch (pl022->read) {
678                 case READING_NULL:
679                         readw(SSP_DR(pl022->virtbase));
680                         break;
681                 case READING_U8:
682                         *(u8 *) (pl022->rx) =
683                                 readw(SSP_DR(pl022->virtbase)) & 0xFFU;
684                         break;
685                 case READING_U16:
686                         *(u16 *) (pl022->rx) =
687                                 (u16) readw(SSP_DR(pl022->virtbase));
688                         break;
689                 case READING_U32:
690                         *(u32 *) (pl022->rx) =
691                                 readl(SSP_DR(pl022->virtbase));
692                         break;
693                 }
694                 pl022->rx += (pl022->cur_chip->n_bytes);
695                 pl022->exp_fifo_level--;
696         }
697         /*
698          * Write as much as possible up to the RX FIFO size
699          */
700         while ((pl022->exp_fifo_level < pl022->vendor->fifodepth)
701                && (pl022->tx < pl022->tx_end)) {
702                 switch (pl022->write) {
703                 case WRITING_NULL:
704                         writew(0x0, SSP_DR(pl022->virtbase));
705                         break;
706                 case WRITING_U8:
707                         writew(*(u8 *) (pl022->tx), SSP_DR(pl022->virtbase));
708                         break;
709                 case WRITING_U16:
710                         writew((*(u16 *) (pl022->tx)), SSP_DR(pl022->virtbase));
711                         break;
712                 case WRITING_U32:
713                         writel(*(u32 *) (pl022->tx), SSP_DR(pl022->virtbase));
714                         break;
715                 }
716                 pl022->tx += (pl022->cur_chip->n_bytes);
717                 pl022->exp_fifo_level++;
718                 /*
719                  * This inner reader takes care of things appearing in the RX
720                  * FIFO as we're transmitting. This will happen a lot since the
721                  * clock starts running when you put things into the TX FIFO,
722                  * and then things are continuously clocked into the RX FIFO.
723                  */
724                 while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
725                        && (pl022->rx < pl022->rx_end)) {
726                         switch (pl022->read) {
727                         case READING_NULL:
728                                 readw(SSP_DR(pl022->virtbase));
729                                 break;
730                         case READING_U8:
731                                 *(u8 *) (pl022->rx) =
732                                         readw(SSP_DR(pl022->virtbase)) & 0xFFU;
733                                 break;
734                         case READING_U16:
735                                 *(u16 *) (pl022->rx) =
736                                         (u16) readw(SSP_DR(pl022->virtbase));
737                                 break;
738                         case READING_U32:
739                                 *(u32 *) (pl022->rx) =
740                                         readl(SSP_DR(pl022->virtbase));
741                                 break;
742                         }
743                         pl022->rx += (pl022->cur_chip->n_bytes);
744                         pl022->exp_fifo_level--;
745                 }
746         }
747         /*
748          * When we exit here the TX FIFO should be full and the RX FIFO
749          * should be empty
750          */
751 }
752
753 /**
754  * next_transfer - Move to the Next transfer in the current spi message
755  * @pl022: SSP driver private data structure
756  *
757  * This function moves though the linked list of spi transfers in the
758  * current spi message and returns with the state of current spi
759  * message i.e whether its last transfer is done(STATE_DONE) or
760  * Next transfer is ready(STATE_RUNNING)
761  */
762 static void *next_transfer(struct pl022 *pl022)
763 {
764         struct spi_message *msg = pl022->cur_msg;
765         struct spi_transfer *trans = pl022->cur_transfer;
766
767         /* Move to next transfer */
768         if (trans->transfer_list.next != &msg->transfers) {
769                 pl022->cur_transfer =
770                     list_entry(trans->transfer_list.next,
771                                struct spi_transfer, transfer_list);
772                 return STATE_RUNNING;
773         }
774         return STATE_DONE;
775 }
776
777 /*
778  * This DMA functionality is only compiled in if we have
779  * access to the generic DMA devices/DMA engine.
780  */
781 #ifdef CONFIG_DMA_ENGINE
782 static void unmap_free_dma_scatter(struct pl022 *pl022)
783 {
784         /* Unmap and free the SG tables */
785         dma_unmap_sg(pl022->dma_tx_channel->device->dev, pl022->sgt_tx.sgl,
786                      pl022->sgt_tx.nents, DMA_TO_DEVICE);
787         dma_unmap_sg(pl022->dma_rx_channel->device->dev, pl022->sgt_rx.sgl,
788                      pl022->sgt_rx.nents, DMA_FROM_DEVICE);
789         sg_free_table(&pl022->sgt_rx);
790         sg_free_table(&pl022->sgt_tx);
791 }
792
793 static void dma_callback(void *data)
794 {
795         struct pl022 *pl022 = data;
796         struct spi_message *msg = pl022->cur_msg;
797
798         BUG_ON(!pl022->sgt_rx.sgl);
799
800 #ifdef VERBOSE_DEBUG
801         /*
802          * Optionally dump out buffers to inspect contents, this is
803          * good if you want to convince yourself that the loopback
804          * read/write contents are the same, when adopting to a new
805          * DMA engine.
806          */
807         {
808                 struct scatterlist *sg;
809                 unsigned int i;
810
811                 dma_sync_sg_for_cpu(&pl022->adev->dev,
812                                     pl022->sgt_rx.sgl,
813                                     pl022->sgt_rx.nents,
814                                     DMA_FROM_DEVICE);
815
816                 for_each_sg(pl022->sgt_rx.sgl, sg, pl022->sgt_rx.nents, i) {
817                         dev_dbg(&pl022->adev->dev, "SPI RX SG ENTRY: %d", i);
818                         print_hex_dump(KERN_ERR, "SPI RX: ",
819                                        DUMP_PREFIX_OFFSET,
820                                        16,
821                                        1,
822                                        sg_virt(sg),
823                                        sg_dma_len(sg),
824                                        1);
825                 }
826                 for_each_sg(pl022->sgt_tx.sgl, sg, pl022->sgt_tx.nents, i) {
827                         dev_dbg(&pl022->adev->dev, "SPI TX SG ENTRY: %d", i);
828                         print_hex_dump(KERN_ERR, "SPI TX: ",
829                                        DUMP_PREFIX_OFFSET,
830                                        16,
831                                        1,
832                                        sg_virt(sg),
833                                        sg_dma_len(sg),
834                                        1);
835                 }
836         }
837 #endif
838
839         unmap_free_dma_scatter(pl022);
840
841         /* Update total bytes transferred */
842         msg->actual_length += pl022->cur_transfer->len;
843         /* Move to next transfer */
844         msg->state = next_transfer(pl022);
845         if (msg->state != STATE_DONE && pl022->cur_transfer->cs_change)
846                 pl022_cs_control(pl022, SSP_CHIP_DESELECT);
847         tasklet_schedule(&pl022->pump_transfers);
848 }
849
850 static void setup_dma_scatter(struct pl022 *pl022,
851                               void *buffer,
852                               unsigned int length,
853                               struct sg_table *sgtab)
854 {
855         struct scatterlist *sg;
856         int bytesleft = length;
857         void *bufp = buffer;
858         int mapbytes;
859         int i;
860
861         if (buffer) {
862                 for_each_sg(sgtab->sgl, sg, sgtab->nents, i) {
863                         /*
864                          * If there are less bytes left than what fits
865                          * in the current page (plus page alignment offset)
866                          * we just feed in this, else we stuff in as much
867                          * as we can.
868                          */
869                         if (bytesleft < (PAGE_SIZE - offset_in_page(bufp)))
870                                 mapbytes = bytesleft;
871                         else
872                                 mapbytes = PAGE_SIZE - offset_in_page(bufp);
873                         sg_set_page(sg, virt_to_page(bufp),
874                                     mapbytes, offset_in_page(bufp));
875                         bufp += mapbytes;
876                         bytesleft -= mapbytes;
877                         dev_dbg(&pl022->adev->dev,
878                                 "set RX/TX target page @ %p, %d bytes, %d left\n",
879                                 bufp, mapbytes, bytesleft);
880                 }
881         } else {
882                 /* Map the dummy buffer on every page */
883                 for_each_sg(sgtab->sgl, sg, sgtab->nents, i) {
884                         if (bytesleft < PAGE_SIZE)
885                                 mapbytes = bytesleft;
886                         else
887                                 mapbytes = PAGE_SIZE;
888                         sg_set_page(sg, virt_to_page(pl022->dummypage),
889                                     mapbytes, 0);
890                         bytesleft -= mapbytes;
891                         dev_dbg(&pl022->adev->dev,
892                                 "set RX/TX to dummy page %d bytes, %d left\n",
893                                 mapbytes, bytesleft);
894
895                 }
896         }
897         BUG_ON(bytesleft);
898 }
899
900 /**
901  * configure_dma - configures the channels for the next transfer
902  * @pl022: SSP driver's private data structure
903  */
904 static int configure_dma(struct pl022 *pl022)
905 {
906         struct dma_slave_config rx_conf = {
907                 .src_addr = SSP_DR(pl022->phybase),
908                 .direction = DMA_DEV_TO_MEM,
909                 .device_fc = false,
910         };
911         struct dma_slave_config tx_conf = {
912                 .dst_addr = SSP_DR(pl022->phybase),
913                 .direction = DMA_MEM_TO_DEV,
914                 .device_fc = false,
915         };
916         unsigned int pages;
917         int ret;
918         int rx_sglen, tx_sglen;
919         struct dma_chan *rxchan = pl022->dma_rx_channel;
920         struct dma_chan *txchan = pl022->dma_tx_channel;
921         struct dma_async_tx_descriptor *rxdesc;
922         struct dma_async_tx_descriptor *txdesc;
923
924         /* Check that the channels are available */
925         if (!rxchan || !txchan)
926                 return -ENODEV;
927
928         /*
929          * If supplied, the DMA burstsize should equal the FIFO trigger level.
930          * Notice that the DMA engine uses one-to-one mapping. Since we can
931          * not trigger on 2 elements this needs explicit mapping rather than
932          * calculation.
933          */
934         switch (pl022->rx_lev_trig) {
935         case SSP_RX_1_OR_MORE_ELEM:
936                 rx_conf.src_maxburst = 1;
937                 break;
938         case SSP_RX_4_OR_MORE_ELEM:
939                 rx_conf.src_maxburst = 4;
940                 break;
941         case SSP_RX_8_OR_MORE_ELEM:
942                 rx_conf.src_maxburst = 8;
943                 break;
944         case SSP_RX_16_OR_MORE_ELEM:
945                 rx_conf.src_maxburst = 16;
946                 break;
947         case SSP_RX_32_OR_MORE_ELEM:
948                 rx_conf.src_maxburst = 32;
949                 break;
950         default:
951                 rx_conf.src_maxburst = pl022->vendor->fifodepth >> 1;
952                 break;
953         }
954
955         switch (pl022->tx_lev_trig) {
956         case SSP_TX_1_OR_MORE_EMPTY_LOC:
957                 tx_conf.dst_maxburst = 1;
958                 break;
959         case SSP_TX_4_OR_MORE_EMPTY_LOC:
960                 tx_conf.dst_maxburst = 4;
961                 break;
962         case SSP_TX_8_OR_MORE_EMPTY_LOC:
963                 tx_conf.dst_maxburst = 8;
964                 break;
965         case SSP_TX_16_OR_MORE_EMPTY_LOC:
966                 tx_conf.dst_maxburst = 16;
967                 break;
968         case SSP_TX_32_OR_MORE_EMPTY_LOC:
969                 tx_conf.dst_maxburst = 32;
970                 break;
971         default:
972                 tx_conf.dst_maxburst = pl022->vendor->fifodepth >> 1;
973                 break;
974         }
975
976         switch (pl022->read) {
977         case READING_NULL:
978                 /* Use the same as for writing */
979                 rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
980                 break;
981         case READING_U8:
982                 rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
983                 break;
984         case READING_U16:
985                 rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
986                 break;
987         case READING_U32:
988                 rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
989                 break;
990         }
991
992         switch (pl022->write) {
993         case WRITING_NULL:
994                 /* Use the same as for reading */
995                 tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
996                 break;
997         case WRITING_U8:
998                 tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
999                 break;
1000         case WRITING_U16:
1001                 tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1002                 break;
1003         case WRITING_U32:
1004                 tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1005                 break;
1006         }
1007
1008         /* SPI pecularity: we need to read and write the same width */
1009         if (rx_conf.src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
1010                 rx_conf.src_addr_width = tx_conf.dst_addr_width;
1011         if (tx_conf.dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
1012                 tx_conf.dst_addr_width = rx_conf.src_addr_width;
1013         BUG_ON(rx_conf.src_addr_width != tx_conf.dst_addr_width);
1014
1015         dmaengine_slave_config(rxchan, &rx_conf);
1016         dmaengine_slave_config(txchan, &tx_conf);
1017
1018         /* Create sglists for the transfers */
1019         pages = DIV_ROUND_UP(pl022->cur_transfer->len, PAGE_SIZE);
1020         dev_dbg(&pl022->adev->dev, "using %d pages for transfer\n", pages);
1021
1022         ret = sg_alloc_table(&pl022->sgt_rx, pages, GFP_ATOMIC);
1023         if (ret)
1024                 goto err_alloc_rx_sg;
1025
1026         ret = sg_alloc_table(&pl022->sgt_tx, pages, GFP_ATOMIC);
1027         if (ret)
1028                 goto err_alloc_tx_sg;
1029
1030         /* Fill in the scatterlists for the RX+TX buffers */
1031         setup_dma_scatter(pl022, pl022->rx,
1032                           pl022->cur_transfer->len, &pl022->sgt_rx);
1033         setup_dma_scatter(pl022, pl022->tx,
1034                           pl022->cur_transfer->len, &pl022->sgt_tx);
1035
1036         /* Map DMA buffers */
1037         rx_sglen = dma_map_sg(rxchan->device->dev, pl022->sgt_rx.sgl,
1038                            pl022->sgt_rx.nents, DMA_FROM_DEVICE);
1039         if (!rx_sglen)
1040                 goto err_rx_sgmap;
1041
1042         tx_sglen = dma_map_sg(txchan->device->dev, pl022->sgt_tx.sgl,
1043                            pl022->sgt_tx.nents, DMA_TO_DEVICE);
1044         if (!tx_sglen)
1045                 goto err_tx_sgmap;
1046
1047         /* Send both scatterlists */
1048         rxdesc = dmaengine_prep_slave_sg(rxchan,
1049                                       pl022->sgt_rx.sgl,
1050                                       rx_sglen,
1051                                       DMA_DEV_TO_MEM,
1052                                       DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1053         if (!rxdesc)
1054                 goto err_rxdesc;
1055
1056         txdesc = dmaengine_prep_slave_sg(txchan,
1057                                       pl022->sgt_tx.sgl,
1058                                       tx_sglen,
1059                                       DMA_MEM_TO_DEV,
1060                                       DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1061         if (!txdesc)
1062                 goto err_txdesc;
1063
1064         /* Put the callback on the RX transfer only, that should finish last */
1065         rxdesc->callback = dma_callback;
1066         rxdesc->callback_param = pl022;
1067
1068         /* Submit and fire RX and TX with TX last so we're ready to read! */
1069         dmaengine_submit(rxdesc);
1070         dmaengine_submit(txdesc);
1071         dma_async_issue_pending(rxchan);
1072         dma_async_issue_pending(txchan);
1073         pl022->dma_running = true;
1074
1075         return 0;
1076
1077 err_txdesc:
1078         dmaengine_terminate_all(txchan);
1079 err_rxdesc:
1080         dmaengine_terminate_all(rxchan);
1081         dma_unmap_sg(txchan->device->dev, pl022->sgt_tx.sgl,
1082                      pl022->sgt_tx.nents, DMA_TO_DEVICE);
1083 err_tx_sgmap:
1084         dma_unmap_sg(rxchan->device->dev, pl022->sgt_rx.sgl,
1085                      pl022->sgt_rx.nents, DMA_FROM_DEVICE);
1086 err_rx_sgmap:
1087         sg_free_table(&pl022->sgt_tx);
1088 err_alloc_tx_sg:
1089         sg_free_table(&pl022->sgt_rx);
1090 err_alloc_rx_sg:
1091         return -ENOMEM;
1092 }
1093
1094 static int pl022_dma_probe(struct pl022 *pl022)
1095 {
1096         dma_cap_mask_t mask;
1097
1098         /* Try to acquire a generic DMA engine slave channel */
1099         dma_cap_zero(mask);
1100         dma_cap_set(DMA_SLAVE, mask);
1101         /*
1102          * We need both RX and TX channels to do DMA, else do none
1103          * of them.
1104          */
1105         pl022->dma_rx_channel = dma_request_channel(mask,
1106                                             pl022->master_info->dma_filter,
1107                                             pl022->master_info->dma_rx_param);
1108         if (!pl022->dma_rx_channel) {
1109                 dev_dbg(&pl022->adev->dev, "no RX DMA channel!\n");
1110                 goto err_no_rxchan;
1111         }
1112
1113         pl022->dma_tx_channel = dma_request_channel(mask,
1114                                             pl022->master_info->dma_filter,
1115                                             pl022->master_info->dma_tx_param);
1116         if (!pl022->dma_tx_channel) {
1117                 dev_dbg(&pl022->adev->dev, "no TX DMA channel!\n");
1118                 goto err_no_txchan;
1119         }
1120
1121         pl022->dummypage = kmalloc(PAGE_SIZE, GFP_KERNEL);
1122         if (!pl022->dummypage)
1123                 goto err_no_dummypage;
1124
1125         dev_info(&pl022->adev->dev, "setup for DMA on RX %s, TX %s\n",
1126                  dma_chan_name(pl022->dma_rx_channel),
1127                  dma_chan_name(pl022->dma_tx_channel));
1128
1129         return 0;
1130
1131 err_no_dummypage:
1132         dma_release_channel(pl022->dma_tx_channel);
1133 err_no_txchan:
1134         dma_release_channel(pl022->dma_rx_channel);
1135         pl022->dma_rx_channel = NULL;
1136 err_no_rxchan:
1137         dev_err(&pl022->adev->dev,
1138                         "Failed to work in dma mode, work without dma!\n");
1139         return -ENODEV;
1140 }
1141
1142 static int pl022_dma_autoprobe(struct pl022 *pl022)
1143 {
1144         struct device *dev = &pl022->adev->dev;
1145         struct dma_chan *chan;
1146         int err;
1147
1148         /* automatically configure DMA channels from platform, normally using DT */
1149         chan = dma_request_chan(dev, "rx");
1150         if (IS_ERR(chan)) {
1151                 err = PTR_ERR(chan);
1152                 goto err_no_rxchan;
1153         }
1154
1155         pl022->dma_rx_channel = chan;
1156
1157         chan = dma_request_chan(dev, "tx");
1158         if (IS_ERR(chan)) {
1159                 err = PTR_ERR(chan);
1160                 goto err_no_txchan;
1161         }
1162
1163         pl022->dma_tx_channel = chan;
1164
1165         pl022->dummypage = kmalloc(PAGE_SIZE, GFP_KERNEL);
1166         if (!pl022->dummypage) {
1167                 err = -ENOMEM;
1168                 goto err_no_dummypage;
1169         }
1170
1171         return 0;
1172
1173 err_no_dummypage:
1174         dma_release_channel(pl022->dma_tx_channel);
1175         pl022->dma_tx_channel = NULL;
1176 err_no_txchan:
1177         dma_release_channel(pl022->dma_rx_channel);
1178         pl022->dma_rx_channel = NULL;
1179 err_no_rxchan:
1180         return err;
1181 }
1182
1183 static void terminate_dma(struct pl022 *pl022)
1184 {
1185         struct dma_chan *rxchan = pl022->dma_rx_channel;
1186         struct dma_chan *txchan = pl022->dma_tx_channel;
1187
1188         dmaengine_terminate_all(rxchan);
1189         dmaengine_terminate_all(txchan);
1190         unmap_free_dma_scatter(pl022);
1191         pl022->dma_running = false;
1192 }
1193
1194 static void pl022_dma_remove(struct pl022 *pl022)
1195 {
1196         if (pl022->dma_running)
1197                 terminate_dma(pl022);
1198         if (pl022->dma_tx_channel)
1199                 dma_release_channel(pl022->dma_tx_channel);
1200         if (pl022->dma_rx_channel)
1201                 dma_release_channel(pl022->dma_rx_channel);
1202         kfree(pl022->dummypage);
1203 }
1204
1205 #else
1206 static inline int configure_dma(struct pl022 *pl022)
1207 {
1208         return -ENODEV;
1209 }
1210
1211 static inline int pl022_dma_autoprobe(struct pl022 *pl022)
1212 {
1213         return 0;
1214 }
1215
1216 static inline int pl022_dma_probe(struct pl022 *pl022)
1217 {
1218         return 0;
1219 }
1220
1221 static inline void pl022_dma_remove(struct pl022 *pl022)
1222 {
1223 }
1224 #endif
1225
1226 /**
1227  * pl022_interrupt_handler - Interrupt handler for SSP controller
1228  * @irq: IRQ number
1229  * @dev_id: Local device data
1230  *
1231  * This function handles interrupts generated for an interrupt based transfer.
1232  * If a receive overrun (ROR) interrupt is there then we disable SSP, flag the
1233  * current message's state as STATE_ERROR and schedule the tasklet
1234  * pump_transfers which will do the postprocessing of the current message by
1235  * calling giveback(). Otherwise it reads data from RX FIFO till there is no
1236  * more data, and writes data in TX FIFO till it is not full. If we complete
1237  * the transfer we move to the next transfer and schedule the tasklet.
1238  */
1239 static irqreturn_t pl022_interrupt_handler(int irq, void *dev_id)
1240 {
1241         struct pl022 *pl022 = dev_id;
1242         struct spi_message *msg = pl022->cur_msg;
1243         u16 irq_status = 0;
1244
1245         if (unlikely(!msg)) {
1246                 dev_err(&pl022->adev->dev,
1247                         "bad message state in interrupt handler");
1248                 /* Never fail */
1249                 return IRQ_HANDLED;
1250         }
1251
1252         /* Read the Interrupt Status Register */
1253         irq_status = readw(SSP_MIS(pl022->virtbase));
1254
1255         if (unlikely(!irq_status))
1256                 return IRQ_NONE;
1257
1258         /*
1259          * This handles the FIFO interrupts, the timeout
1260          * interrupts are flatly ignored, they cannot be
1261          * trusted.
1262          */
1263         if (unlikely(irq_status & SSP_MIS_MASK_RORMIS)) {
1264                 /*
1265                  * Overrun interrupt - bail out since our Data has been
1266                  * corrupted
1267                  */
1268                 dev_err(&pl022->adev->dev, "FIFO overrun\n");
1269                 if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RFF)
1270                         dev_err(&pl022->adev->dev,
1271                                 "RXFIFO is full\n");
1272
1273                 /*
1274                  * Disable and clear interrupts, disable SSP,
1275                  * mark message with bad status so it can be
1276                  * retried.
1277                  */
1278                 writew(DISABLE_ALL_INTERRUPTS,
1279                        SSP_IMSC(pl022->virtbase));
1280                 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
1281                 writew((readw(SSP_CR1(pl022->virtbase)) &
1282                         (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
1283                 msg->state = STATE_ERROR;
1284
1285                 /* Schedule message queue handler */
1286                 tasklet_schedule(&pl022->pump_transfers);
1287                 return IRQ_HANDLED;
1288         }
1289
1290         readwriter(pl022);
1291
1292         if (pl022->tx == pl022->tx_end) {
1293                 /* Disable Transmit interrupt, enable receive interrupt */
1294                 writew((readw(SSP_IMSC(pl022->virtbase)) &
1295                        ~SSP_IMSC_MASK_TXIM) | SSP_IMSC_MASK_RXIM,
1296                        SSP_IMSC(pl022->virtbase));
1297         }
1298
1299         /*
1300          * Since all transactions must write as much as shall be read,
1301          * we can conclude the entire transaction once RX is complete.
1302          * At this point, all TX will always be finished.
1303          */
1304         if (pl022->rx >= pl022->rx_end) {
1305                 writew(DISABLE_ALL_INTERRUPTS,
1306                        SSP_IMSC(pl022->virtbase));
1307                 writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
1308                 if (unlikely(pl022->rx > pl022->rx_end)) {
1309                         dev_warn(&pl022->adev->dev, "read %u surplus "
1310                                  "bytes (did you request an odd "
1311                                  "number of bytes on a 16bit bus?)\n",
1312                                  (u32) (pl022->rx - pl022->rx_end));
1313                 }
1314                 /* Update total bytes transferred */
1315                 msg->actual_length += pl022->cur_transfer->len;
1316                 /* Move to next transfer */
1317                 msg->state = next_transfer(pl022);
1318                 if (msg->state != STATE_DONE && pl022->cur_transfer->cs_change)
1319                         pl022_cs_control(pl022, SSP_CHIP_DESELECT);
1320                 tasklet_schedule(&pl022->pump_transfers);
1321                 return IRQ_HANDLED;
1322         }
1323
1324         return IRQ_HANDLED;
1325 }
1326
1327 /*
1328  * This sets up the pointers to memory for the next message to
1329  * send out on the SPI bus.
1330  */
1331 static int set_up_next_transfer(struct pl022 *pl022,
1332                                 struct spi_transfer *transfer)
1333 {
1334         int residue;
1335
1336         /* Sanity check the message for this bus width */
1337         residue = pl022->cur_transfer->len % pl022->cur_chip->n_bytes;
1338         if (unlikely(residue != 0)) {
1339                 dev_err(&pl022->adev->dev,
1340                         "message of %u bytes to transmit but the current "
1341                         "chip bus has a data width of %u bytes!\n",
1342                         pl022->cur_transfer->len,
1343                         pl022->cur_chip->n_bytes);
1344                 dev_err(&pl022->adev->dev, "skipping this message\n");
1345                 return -EIO;
1346         }
1347         pl022->tx = (void *)transfer->tx_buf;
1348         pl022->tx_end = pl022->tx + pl022->cur_transfer->len;
1349         pl022->rx = (void *)transfer->rx_buf;
1350         pl022->rx_end = pl022->rx + pl022->cur_transfer->len;
1351         pl022->write =
1352             pl022->tx ? pl022->cur_chip->write : WRITING_NULL;
1353         pl022->read = pl022->rx ? pl022->cur_chip->read : READING_NULL;
1354         return 0;
1355 }
1356
1357 /**
1358  * pump_transfers - Tasklet function which schedules next transfer
1359  * when running in interrupt or DMA transfer mode.
1360  * @data: SSP driver private data structure
1361  *
1362  */
1363 static void pump_transfers(unsigned long data)
1364 {
1365         struct pl022 *pl022 = (struct pl022 *) data;
1366         struct spi_message *message = NULL;
1367         struct spi_transfer *transfer = NULL;
1368         struct spi_transfer *previous = NULL;
1369
1370         /* Get current state information */
1371         message = pl022->cur_msg;
1372         transfer = pl022->cur_transfer;
1373
1374         /* Handle for abort */
1375         if (message->state == STATE_ERROR) {
1376                 message->status = -EIO;
1377                 giveback(pl022);
1378                 return;
1379         }
1380
1381         /* Handle end of message */
1382         if (message->state == STATE_DONE) {
1383                 message->status = 0;
1384                 giveback(pl022);
1385                 return;
1386         }
1387
1388         /* Delay if requested at end of transfer before CS change */
1389         if (message->state == STATE_RUNNING) {
1390                 previous = list_entry(transfer->transfer_list.prev,
1391                                         struct spi_transfer,
1392                                         transfer_list);
1393                 /*
1394                  * FIXME: This runs in interrupt context.
1395                  * Is this really smart?
1396                  */
1397                 spi_transfer_delay_exec(previous);
1398
1399                 /* Reselect chip select only if cs_change was requested */
1400                 if (previous->cs_change)
1401                         pl022_cs_control(pl022, SSP_CHIP_SELECT);
1402         } else {
1403                 /* STATE_START */
1404                 message->state = STATE_RUNNING;
1405         }
1406
1407         if (set_up_next_transfer(pl022, transfer)) {
1408                 message->state = STATE_ERROR;
1409                 message->status = -EIO;
1410                 giveback(pl022);
1411                 return;
1412         }
1413         /* Flush the FIFOs and let's go! */
1414         flush(pl022);
1415
1416         if (pl022->cur_chip->enable_dma) {
1417                 if (configure_dma(pl022)) {
1418                         dev_dbg(&pl022->adev->dev,
1419                                 "configuration of DMA failed, fall back to interrupt mode\n");
1420                         goto err_config_dma;
1421                 }
1422                 return;
1423         }
1424
1425 err_config_dma:
1426         /* enable all interrupts except RX */
1427         writew(ENABLE_ALL_INTERRUPTS & ~SSP_IMSC_MASK_RXIM, SSP_IMSC(pl022->virtbase));
1428 }
1429
1430 static void do_interrupt_dma_transfer(struct pl022 *pl022)
1431 {
1432         /*
1433          * Default is to enable all interrupts except RX -
1434          * this will be enabled once TX is complete
1435          */
1436         u32 irqflags = (u32)(ENABLE_ALL_INTERRUPTS & ~SSP_IMSC_MASK_RXIM);
1437
1438         /* Enable target chip, if not already active */
1439         if (!pl022->next_msg_cs_active)
1440                 pl022_cs_control(pl022, SSP_CHIP_SELECT);
1441
1442         if (set_up_next_transfer(pl022, pl022->cur_transfer)) {
1443                 /* Error path */
1444                 pl022->cur_msg->state = STATE_ERROR;
1445                 pl022->cur_msg->status = -EIO;
1446                 giveback(pl022);
1447                 return;
1448         }
1449         /* If we're using DMA, set up DMA here */
1450         if (pl022->cur_chip->enable_dma) {
1451                 /* Configure DMA transfer */
1452                 if (configure_dma(pl022)) {
1453                         dev_dbg(&pl022->adev->dev,
1454                                 "configuration of DMA failed, fall back to interrupt mode\n");
1455                         goto err_config_dma;
1456                 }
1457                 /* Disable interrupts in DMA mode, IRQ from DMA controller */
1458                 irqflags = DISABLE_ALL_INTERRUPTS;
1459         }
1460 err_config_dma:
1461         /* Enable SSP, turn on interrupts */
1462         writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
1463                SSP_CR1(pl022->virtbase));
1464         writew(irqflags, SSP_IMSC(pl022->virtbase));
1465 }
1466
1467 static void print_current_status(struct pl022 *pl022)
1468 {
1469         u32 read_cr0;
1470         u16 read_cr1, read_dmacr, read_sr;
1471
1472         if (pl022->vendor->extended_cr)
1473                 read_cr0 = readl(SSP_CR0(pl022->virtbase));
1474         else
1475                 read_cr0 = readw(SSP_CR0(pl022->virtbase));
1476         read_cr1 = readw(SSP_CR1(pl022->virtbase));
1477         read_dmacr = readw(SSP_DMACR(pl022->virtbase));
1478         read_sr = readw(SSP_SR(pl022->virtbase));
1479
1480         dev_warn(&pl022->adev->dev, "spi-pl022 CR0: %x\n", read_cr0);
1481         dev_warn(&pl022->adev->dev, "spi-pl022 CR1: %x\n", read_cr1);
1482         dev_warn(&pl022->adev->dev, "spi-pl022 DMACR: %x\n", read_dmacr);
1483         dev_warn(&pl022->adev->dev, "spi-pl022 SR: %x\n", read_sr);
1484         dev_warn(&pl022->adev->dev,
1485                         "spi-pl022 exp_fifo_level/fifodepth: %u/%d\n",
1486                         pl022->exp_fifo_level,
1487                         pl022->vendor->fifodepth);
1488
1489 }
1490
1491 static void do_polling_transfer(struct pl022 *pl022)
1492 {
1493         struct spi_message *message = NULL;
1494         struct spi_transfer *transfer = NULL;
1495         struct spi_transfer *previous = NULL;
1496         unsigned long time, timeout;
1497
1498         message = pl022->cur_msg;
1499
1500         while (message->state != STATE_DONE) {
1501                 /* Handle for abort */
1502                 if (message->state == STATE_ERROR)
1503                         break;
1504                 transfer = pl022->cur_transfer;
1505
1506                 /* Delay if requested at end of transfer */
1507                 if (message->state == STATE_RUNNING) {
1508                         previous =
1509                             list_entry(transfer->transfer_list.prev,
1510                                        struct spi_transfer, transfer_list);
1511                         spi_transfer_delay_exec(previous);
1512                         if (previous->cs_change)
1513                                 pl022_cs_control(pl022, SSP_CHIP_SELECT);
1514                 } else {
1515                         /* STATE_START */
1516                         message->state = STATE_RUNNING;
1517                         if (!pl022->next_msg_cs_active)
1518                                 pl022_cs_control(pl022, SSP_CHIP_SELECT);
1519                 }
1520
1521                 /* Configuration Changing Per Transfer */
1522                 if (set_up_next_transfer(pl022, transfer)) {
1523                         /* Error path */
1524                         message->state = STATE_ERROR;
1525                         break;
1526                 }
1527                 /* Flush FIFOs and enable SSP */
1528                 flush(pl022);
1529                 writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
1530                        SSP_CR1(pl022->virtbase));
1531
1532                 dev_dbg(&pl022->adev->dev, "polling transfer ongoing ...\n");
1533
1534                 timeout = jiffies + msecs_to_jiffies(SPI_POLLING_TIMEOUT);
1535                 while (pl022->tx < pl022->tx_end || pl022->rx < pl022->rx_end) {
1536                         time = jiffies;
1537                         readwriter(pl022);
1538                         if (time_after(time, timeout)) {
1539                                 dev_warn(&pl022->adev->dev,
1540                                 "%s: timeout!\n", __func__);
1541                                 message->state = STATE_TIMEOUT;
1542                                 print_current_status(pl022);
1543                                 goto out;
1544                         }
1545                         cpu_relax();
1546                 }
1547
1548                 /* Update total byte transferred */
1549                 message->actual_length += pl022->cur_transfer->len;
1550                 /* Move to next transfer */
1551                 message->state = next_transfer(pl022);
1552                 if (message->state != STATE_DONE
1553                     && pl022->cur_transfer->cs_change)
1554                         pl022_cs_control(pl022, SSP_CHIP_DESELECT);
1555         }
1556 out:
1557         /* Handle end of message */
1558         if (message->state == STATE_DONE)
1559                 message->status = 0;
1560         else if (message->state == STATE_TIMEOUT)
1561                 message->status = -EAGAIN;
1562         else
1563                 message->status = -EIO;
1564
1565         giveback(pl022);
1566         return;
1567 }
1568
1569 static int pl022_transfer_one_message(struct spi_master *master,
1570                                       struct spi_message *msg)
1571 {
1572         struct pl022 *pl022 = spi_master_get_devdata(master);
1573
1574         /* Initial message state */
1575         pl022->cur_msg = msg;
1576         msg->state = STATE_START;
1577
1578         pl022->cur_transfer = list_entry(msg->transfers.next,
1579                                          struct spi_transfer, transfer_list);
1580
1581         /* Setup the SPI using the per chip configuration */
1582         pl022->cur_chip = spi_get_ctldata(msg->spi);
1583         pl022->cur_cs = pl022->chipselects[msg->spi->chip_select];
1584
1585         restore_state(pl022);
1586         flush(pl022);
1587
1588         if (pl022->cur_chip->xfer_type == POLLING_TRANSFER)
1589                 do_polling_transfer(pl022);
1590         else
1591                 do_interrupt_dma_transfer(pl022);
1592
1593         return 0;
1594 }
1595
1596 static int pl022_unprepare_transfer_hardware(struct spi_master *master)
1597 {
1598         struct pl022 *pl022 = spi_master_get_devdata(master);
1599
1600         /* nothing more to do - disable spi/ssp and power off */
1601         writew((readw(SSP_CR1(pl022->virtbase)) &
1602                 (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
1603
1604         return 0;
1605 }
1606
1607 static int verify_controller_parameters(struct pl022 *pl022,
1608                                 struct pl022_config_chip const *chip_info)
1609 {
1610         if ((chip_info->iface < SSP_INTERFACE_MOTOROLA_SPI)
1611             || (chip_info->iface > SSP_INTERFACE_UNIDIRECTIONAL)) {
1612                 dev_err(&pl022->adev->dev,
1613                         "interface is configured incorrectly\n");
1614                 return -EINVAL;
1615         }
1616         if ((chip_info->iface == SSP_INTERFACE_UNIDIRECTIONAL) &&
1617             (!pl022->vendor->unidir)) {
1618                 dev_err(&pl022->adev->dev,
1619                         "unidirectional mode not supported in this "
1620                         "hardware version\n");
1621                 return -EINVAL;
1622         }
1623         if ((chip_info->hierarchy != SSP_MASTER)
1624             && (chip_info->hierarchy != SSP_SLAVE)) {
1625                 dev_err(&pl022->adev->dev,
1626                         "hierarchy is configured incorrectly\n");
1627                 return -EINVAL;
1628         }
1629         if ((chip_info->com_mode != INTERRUPT_TRANSFER)
1630             && (chip_info->com_mode != DMA_TRANSFER)
1631             && (chip_info->com_mode != POLLING_TRANSFER)) {
1632                 dev_err(&pl022->adev->dev,
1633                         "Communication mode is configured incorrectly\n");
1634                 return -EINVAL;
1635         }
1636         switch (chip_info->rx_lev_trig) {
1637         case SSP_RX_1_OR_MORE_ELEM:
1638         case SSP_RX_4_OR_MORE_ELEM:
1639         case SSP_RX_8_OR_MORE_ELEM:
1640                 /* These are always OK, all variants can handle this */
1641                 break;
1642         case SSP_RX_16_OR_MORE_ELEM:
1643                 if (pl022->vendor->fifodepth < 16) {
1644                         dev_err(&pl022->adev->dev,
1645                         "RX FIFO Trigger Level is configured incorrectly\n");
1646                         return -EINVAL;
1647                 }
1648                 break;
1649         case SSP_RX_32_OR_MORE_ELEM:
1650                 if (pl022->vendor->fifodepth < 32) {
1651                         dev_err(&pl022->adev->dev,
1652                         "RX FIFO Trigger Level is configured incorrectly\n");
1653                         return -EINVAL;
1654                 }
1655                 break;
1656         default:
1657                 dev_err(&pl022->adev->dev,
1658                         "RX FIFO Trigger Level is configured incorrectly\n");
1659                 return -EINVAL;
1660         }
1661         switch (chip_info->tx_lev_trig) {
1662         case SSP_TX_1_OR_MORE_EMPTY_LOC:
1663         case SSP_TX_4_OR_MORE_EMPTY_LOC:
1664         case SSP_TX_8_OR_MORE_EMPTY_LOC:
1665                 /* These are always OK, all variants can handle this */
1666                 break;
1667         case SSP_TX_16_OR_MORE_EMPTY_LOC:
1668                 if (pl022->vendor->fifodepth < 16) {
1669                         dev_err(&pl022->adev->dev,
1670                         "TX FIFO Trigger Level is configured incorrectly\n");
1671                         return -EINVAL;
1672                 }
1673                 break;
1674         case SSP_TX_32_OR_MORE_EMPTY_LOC:
1675                 if (pl022->vendor->fifodepth < 32) {
1676                         dev_err(&pl022->adev->dev,
1677                         "TX FIFO Trigger Level is configured incorrectly\n");
1678                         return -EINVAL;
1679                 }
1680                 break;
1681         default:
1682                 dev_err(&pl022->adev->dev,
1683                         "TX FIFO Trigger Level is configured incorrectly\n");
1684                 return -EINVAL;
1685         }
1686         if (chip_info->iface == SSP_INTERFACE_NATIONAL_MICROWIRE) {
1687                 if ((chip_info->ctrl_len < SSP_BITS_4)
1688                     || (chip_info->ctrl_len > SSP_BITS_32)) {
1689                         dev_err(&pl022->adev->dev,
1690                                 "CTRL LEN is configured incorrectly\n");
1691                         return -EINVAL;
1692                 }
1693                 if ((chip_info->wait_state != SSP_MWIRE_WAIT_ZERO)
1694                     && (chip_info->wait_state != SSP_MWIRE_WAIT_ONE)) {
1695                         dev_err(&pl022->adev->dev,
1696                                 "Wait State is configured incorrectly\n");
1697                         return -EINVAL;
1698                 }
1699                 /* Half duplex is only available in the ST Micro version */
1700                 if (pl022->vendor->extended_cr) {
1701                         if ((chip_info->duplex !=
1702                              SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
1703                             && (chip_info->duplex !=
1704                                 SSP_MICROWIRE_CHANNEL_HALF_DUPLEX)) {
1705                                 dev_err(&pl022->adev->dev,
1706                                         "Microwire duplex mode is configured incorrectly\n");
1707                                 return -EINVAL;
1708                         }
1709                 } else {
1710                         if (chip_info->duplex != SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
1711                                 dev_err(&pl022->adev->dev,
1712                                         "Microwire half duplex mode requested,"
1713                                         " but this is only available in the"
1714                                         " ST version of PL022\n");
1715                         return -EINVAL;
1716                 }
1717         }
1718         return 0;
1719 }
1720
1721 static inline u32 spi_rate(u32 rate, u16 cpsdvsr, u16 scr)
1722 {
1723         return rate / (cpsdvsr * (1 + scr));
1724 }
1725
1726 static int calculate_effective_freq(struct pl022 *pl022, int freq, struct
1727                                     ssp_clock_params * clk_freq)
1728 {
1729         /* Lets calculate the frequency parameters */
1730         u16 cpsdvsr = CPSDVR_MIN, scr = SCR_MIN;
1731         u32 rate, max_tclk, min_tclk, best_freq = 0, best_cpsdvsr = 0,
1732                 best_scr = 0, tmp, found = 0;
1733
1734         rate = clk_get_rate(pl022->clk);
1735         /* cpsdvscr = 2 & scr 0 */
1736         max_tclk = spi_rate(rate, CPSDVR_MIN, SCR_MIN);
1737         /* cpsdvsr = 254 & scr = 255 */
1738         min_tclk = spi_rate(rate, CPSDVR_MAX, SCR_MAX);
1739
1740         if (freq > max_tclk)
1741                 dev_warn(&pl022->adev->dev,
1742                         "Max speed that can be programmed is %d Hz, you requested %d\n",
1743                         max_tclk, freq);
1744
1745         if (freq < min_tclk) {
1746                 dev_err(&pl022->adev->dev,
1747                         "Requested frequency: %d Hz is less than minimum possible %d Hz\n",
1748                         freq, min_tclk);
1749                 return -EINVAL;
1750         }
1751
1752         /*
1753          * best_freq will give closest possible available rate (<= requested
1754          * freq) for all values of scr & cpsdvsr.
1755          */
1756         while ((cpsdvsr <= CPSDVR_MAX) && !found) {
1757                 while (scr <= SCR_MAX) {
1758                         tmp = spi_rate(rate, cpsdvsr, scr);
1759
1760                         if (tmp > freq) {
1761                                 /* we need lower freq */
1762                                 scr++;
1763                                 continue;
1764                         }
1765
1766                         /*
1767                          * If found exact value, mark found and break.
1768                          * If found more closer value, update and break.
1769                          */
1770                         if (tmp > best_freq) {
1771                                 best_freq = tmp;
1772                                 best_cpsdvsr = cpsdvsr;
1773                                 best_scr = scr;
1774
1775                                 if (tmp == freq)
1776                                         found = 1;
1777                         }
1778                         /*
1779                          * increased scr will give lower rates, which are not
1780                          * required
1781                          */
1782                         break;
1783                 }
1784                 cpsdvsr += 2;
1785                 scr = SCR_MIN;
1786         }
1787
1788         WARN(!best_freq, "pl022: Matching cpsdvsr and scr not found for %d Hz rate \n",
1789                         freq);
1790
1791         clk_freq->cpsdvsr = (u8) (best_cpsdvsr & 0xFF);
1792         clk_freq->scr = (u8) (best_scr & 0xFF);
1793         dev_dbg(&pl022->adev->dev,
1794                 "SSP Target Frequency is: %u, Effective Frequency is %u\n",
1795                 freq, best_freq);
1796         dev_dbg(&pl022->adev->dev, "SSP cpsdvsr = %d, scr = %d\n",
1797                 clk_freq->cpsdvsr, clk_freq->scr);
1798
1799         return 0;
1800 }
1801
1802 /*
1803  * A piece of default chip info unless the platform
1804  * supplies it.
1805  */
1806 static const struct pl022_config_chip pl022_default_chip_info = {
1807         .com_mode = INTERRUPT_TRANSFER,
1808         .iface = SSP_INTERFACE_MOTOROLA_SPI,
1809         .hierarchy = SSP_MASTER,
1810         .slave_tx_disable = DO_NOT_DRIVE_TX,
1811         .rx_lev_trig = SSP_RX_1_OR_MORE_ELEM,
1812         .tx_lev_trig = SSP_TX_1_OR_MORE_EMPTY_LOC,
1813         .ctrl_len = SSP_BITS_8,
1814         .wait_state = SSP_MWIRE_WAIT_ZERO,
1815         .duplex = SSP_MICROWIRE_CHANNEL_FULL_DUPLEX,
1816 };
1817
1818 /**
1819  * pl022_setup - setup function registered to SPI master framework
1820  * @spi: spi device which is requesting setup
1821  *
1822  * This function is registered to the SPI framework for this SPI master
1823  * controller. If it is the first time when setup is called by this device,
1824  * this function will initialize the runtime state for this chip and save
1825  * the same in the device structure. Else it will update the runtime info
1826  * with the updated chip info. Nothing is really being written to the
1827  * controller hardware here, that is not done until the actual transfer
1828  * commence.
1829  */
1830 static int pl022_setup(struct spi_device *spi)
1831 {
1832         struct pl022_config_chip const *chip_info;
1833         struct pl022_config_chip chip_info_dt;
1834         struct chip_data *chip;
1835         struct ssp_clock_params clk_freq = { .cpsdvsr = 0, .scr = 0};
1836         int status = 0;
1837         struct pl022 *pl022 = spi_master_get_devdata(spi->master);
1838         unsigned int bits = spi->bits_per_word;
1839         u32 tmp;
1840         struct device_node *np = spi->dev.of_node;
1841
1842         if (!spi->max_speed_hz)
1843                 return -EINVAL;
1844
1845         /* Get controller_state if one is supplied */
1846         chip = spi_get_ctldata(spi);
1847
1848         if (chip == NULL) {
1849                 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1850                 if (!chip)
1851                         return -ENOMEM;
1852                 dev_dbg(&spi->dev,
1853                         "allocated memory for controller's runtime state\n");
1854         }
1855
1856         /* Get controller data if one is supplied */
1857         chip_info = spi->controller_data;
1858
1859         if (chip_info == NULL) {
1860                 if (np) {
1861                         chip_info_dt = pl022_default_chip_info;
1862
1863                         chip_info_dt.hierarchy = SSP_MASTER;
1864                         of_property_read_u32(np, "pl022,interface",
1865                                 &chip_info_dt.iface);
1866                         of_property_read_u32(np, "pl022,com-mode",
1867                                 &chip_info_dt.com_mode);
1868                         of_property_read_u32(np, "pl022,rx-level-trig",
1869                                 &chip_info_dt.rx_lev_trig);
1870                         of_property_read_u32(np, "pl022,tx-level-trig",
1871                                 &chip_info_dt.tx_lev_trig);
1872                         of_property_read_u32(np, "pl022,ctrl-len",
1873                                 &chip_info_dt.ctrl_len);
1874                         of_property_read_u32(np, "pl022,wait-state",
1875                                 &chip_info_dt.wait_state);
1876                         of_property_read_u32(np, "pl022,duplex",
1877                                 &chip_info_dt.duplex);
1878
1879                         chip_info = &chip_info_dt;
1880                 } else {
1881                         chip_info = &pl022_default_chip_info;
1882                         /* spi_board_info.controller_data not is supplied */
1883                         dev_dbg(&spi->dev,
1884                                 "using default controller_data settings\n");
1885                 }
1886         } else
1887                 dev_dbg(&spi->dev,
1888                         "using user supplied controller_data settings\n");
1889
1890         /*
1891          * We can override with custom divisors, else we use the board
1892          * frequency setting
1893          */
1894         if ((0 == chip_info->clk_freq.cpsdvsr)
1895             && (0 == chip_info->clk_freq.scr)) {
1896                 status = calculate_effective_freq(pl022,
1897                                                   spi->max_speed_hz,
1898                                                   &clk_freq);
1899                 if (status < 0)
1900                         goto err_config_params;
1901         } else {
1902                 memcpy(&clk_freq, &chip_info->clk_freq, sizeof(clk_freq));
1903                 if ((clk_freq.cpsdvsr % 2) != 0)
1904                         clk_freq.cpsdvsr =
1905                                 clk_freq.cpsdvsr - 1;
1906         }
1907         if ((clk_freq.cpsdvsr < CPSDVR_MIN)
1908             || (clk_freq.cpsdvsr > CPSDVR_MAX)) {
1909                 status = -EINVAL;
1910                 dev_err(&spi->dev,
1911                         "cpsdvsr is configured incorrectly\n");
1912                 goto err_config_params;
1913         }
1914
1915         status = verify_controller_parameters(pl022, chip_info);
1916         if (status) {
1917                 dev_err(&spi->dev, "controller data is incorrect");
1918                 goto err_config_params;
1919         }
1920
1921         pl022->rx_lev_trig = chip_info->rx_lev_trig;
1922         pl022->tx_lev_trig = chip_info->tx_lev_trig;
1923
1924         /* Now set controller state based on controller data */
1925         chip->xfer_type = chip_info->com_mode;
1926         if (!gpio_is_valid(pl022->chipselects[spi->chip_select]))
1927                 dev_warn(&spi->dev, "invalid chip select\n");
1928
1929         /* Check bits per word with vendor specific range */
1930         if ((bits <= 3) || (bits > pl022->vendor->max_bpw)) {
1931                 status = -ENOTSUPP;
1932                 dev_err(&spi->dev, "illegal data size for this controller!\n");
1933                 dev_err(&spi->dev, "This controller can only handle 4 <= n <= %d bit words\n",
1934                                 pl022->vendor->max_bpw);
1935                 goto err_config_params;
1936         } else if (bits <= 8) {
1937                 dev_dbg(&spi->dev, "4 <= n <=8 bits per word\n");
1938                 chip->n_bytes = 1;
1939                 chip->read = READING_U8;
1940                 chip->write = WRITING_U8;
1941         } else if (bits <= 16) {
1942                 dev_dbg(&spi->dev, "9 <= n <= 16 bits per word\n");
1943                 chip->n_bytes = 2;
1944                 chip->read = READING_U16;
1945                 chip->write = WRITING_U16;
1946         } else {
1947                 dev_dbg(&spi->dev, "17 <= n <= 32 bits per word\n");
1948                 chip->n_bytes = 4;
1949                 chip->read = READING_U32;
1950                 chip->write = WRITING_U32;
1951         }
1952
1953         /* Now Initialize all register settings required for this chip */
1954         chip->cr0 = 0;
1955         chip->cr1 = 0;
1956         chip->dmacr = 0;
1957         chip->cpsr = 0;
1958         if ((chip_info->com_mode == DMA_TRANSFER)
1959             && ((pl022->master_info)->enable_dma)) {
1960                 chip->enable_dma = true;
1961                 dev_dbg(&spi->dev, "DMA mode set in controller state\n");
1962                 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
1963                                SSP_DMACR_MASK_RXDMAE, 0);
1964                 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
1965                                SSP_DMACR_MASK_TXDMAE, 1);
1966         } else {
1967                 chip->enable_dma = false;
1968                 dev_dbg(&spi->dev, "DMA mode NOT set in controller state\n");
1969                 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
1970                                SSP_DMACR_MASK_RXDMAE, 0);
1971                 SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
1972                                SSP_DMACR_MASK_TXDMAE, 1);
1973         }
1974
1975         chip->cpsr = clk_freq.cpsdvsr;
1976
1977         /* Special setup for the ST micro extended control registers */
1978         if (pl022->vendor->extended_cr) {
1979                 u32 etx;
1980
1981                 if (pl022->vendor->pl023) {
1982                         /* These bits are only in the PL023 */
1983                         SSP_WRITE_BITS(chip->cr1, chip_info->clkdelay,
1984                                        SSP_CR1_MASK_FBCLKDEL_ST, 13);
1985                 } else {
1986                         /* These bits are in the PL022 but not PL023 */
1987                         SSP_WRITE_BITS(chip->cr0, chip_info->duplex,
1988                                        SSP_CR0_MASK_HALFDUP_ST, 5);
1989                         SSP_WRITE_BITS(chip->cr0, chip_info->ctrl_len,
1990                                        SSP_CR0_MASK_CSS_ST, 16);
1991                         SSP_WRITE_BITS(chip->cr0, chip_info->iface,
1992                                        SSP_CR0_MASK_FRF_ST, 21);
1993                         SSP_WRITE_BITS(chip->cr1, chip_info->wait_state,
1994                                        SSP_CR1_MASK_MWAIT_ST, 6);
1995                 }
1996                 SSP_WRITE_BITS(chip->cr0, bits - 1,
1997                                SSP_CR0_MASK_DSS_ST, 0);
1998
1999                 if (spi->mode & SPI_LSB_FIRST) {
2000                         tmp = SSP_RX_LSB;
2001                         etx = SSP_TX_LSB;
2002                 } else {
2003                         tmp = SSP_RX_MSB;
2004                         etx = SSP_TX_MSB;
2005                 }
2006                 SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_RENDN_ST, 4);
2007                 SSP_WRITE_BITS(chip->cr1, etx, SSP_CR1_MASK_TENDN_ST, 5);
2008                 SSP_WRITE_BITS(chip->cr1, chip_info->rx_lev_trig,
2009                                SSP_CR1_MASK_RXIFLSEL_ST, 7);
2010                 SSP_WRITE_BITS(chip->cr1, chip_info->tx_lev_trig,
2011                                SSP_CR1_MASK_TXIFLSEL_ST, 10);
2012         } else {
2013                 SSP_WRITE_BITS(chip->cr0, bits - 1,
2014                                SSP_CR0_MASK_DSS, 0);
2015                 SSP_WRITE_BITS(chip->cr0, chip_info->iface,
2016                                SSP_CR0_MASK_FRF, 4);
2017         }
2018
2019         /* Stuff that is common for all versions */
2020         if (spi->mode & SPI_CPOL)
2021                 tmp = SSP_CLK_POL_IDLE_HIGH;
2022         else
2023                 tmp = SSP_CLK_POL_IDLE_LOW;
2024         SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPO, 6);
2025
2026         if (spi->mode & SPI_CPHA)
2027                 tmp = SSP_CLK_SECOND_EDGE;
2028         else
2029                 tmp = SSP_CLK_FIRST_EDGE;
2030         SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPH, 7);
2031
2032         SSP_WRITE_BITS(chip->cr0, clk_freq.scr, SSP_CR0_MASK_SCR, 8);
2033         /* Loopback is available on all versions except PL023 */
2034         if (pl022->vendor->loopback) {
2035                 if (spi->mode & SPI_LOOP)
2036                         tmp = LOOPBACK_ENABLED;
2037                 else
2038                         tmp = LOOPBACK_DISABLED;
2039                 SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_LBM, 0);
2040         }
2041         SSP_WRITE_BITS(chip->cr1, SSP_DISABLED, SSP_CR1_MASK_SSE, 1);
2042         SSP_WRITE_BITS(chip->cr1, chip_info->hierarchy, SSP_CR1_MASK_MS, 2);
2043         SSP_WRITE_BITS(chip->cr1, chip_info->slave_tx_disable, SSP_CR1_MASK_SOD,
2044                 3);
2045
2046         /* Save controller_state */
2047         spi_set_ctldata(spi, chip);
2048         return status;
2049  err_config_params:
2050         spi_set_ctldata(spi, NULL);
2051         kfree(chip);
2052         return status;
2053 }
2054
2055 /**
2056  * pl022_cleanup - cleanup function registered to SPI master framework
2057  * @spi: spi device which is requesting cleanup
2058  *
2059  * This function is registered to the SPI framework for this SPI master
2060  * controller. It will free the runtime state of chip.
2061  */
2062 static void pl022_cleanup(struct spi_device *spi)
2063 {
2064         struct chip_data *chip = spi_get_ctldata(spi);
2065
2066         spi_set_ctldata(spi, NULL);
2067         kfree(chip);
2068 }
2069
2070 static struct pl022_ssp_controller *
2071 pl022_platform_data_dt_get(struct device *dev)
2072 {
2073         struct device_node *np = dev->of_node;
2074         struct pl022_ssp_controller *pd;
2075         u32 tmp = 0;
2076
2077         if (!np) {
2078                 dev_err(dev, "no dt node defined\n");
2079                 return NULL;
2080         }
2081
2082         pd = devm_kzalloc(dev, sizeof(struct pl022_ssp_controller), GFP_KERNEL);
2083         if (!pd)
2084                 return NULL;
2085
2086         pd->bus_id = -1;
2087         pd->enable_dma = 1;
2088         of_property_read_u32(np, "num-cs", &tmp);
2089         pd->num_chipselect = tmp;
2090         of_property_read_u32(np, "pl022,autosuspend-delay",
2091                              &pd->autosuspend_delay);
2092         pd->rt = of_property_read_bool(np, "pl022,rt");
2093
2094         return pd;
2095 }
2096
2097 static int pl022_probe(struct amba_device *adev, const struct amba_id *id)
2098 {
2099         struct device *dev = &adev->dev;
2100         struct pl022_ssp_controller *platform_info =
2101                         dev_get_platdata(&adev->dev);
2102         struct spi_master *master;
2103         struct pl022 *pl022 = NULL;     /*Data for this driver */
2104         struct device_node *np = adev->dev.of_node;
2105         int status = 0, i, num_cs;
2106
2107         dev_info(&adev->dev,
2108                  "ARM PL022 driver, device ID: 0x%08x\n", adev->periphid);
2109         if (!platform_info && IS_ENABLED(CONFIG_OF))
2110                 platform_info = pl022_platform_data_dt_get(dev);
2111
2112         if (!platform_info) {
2113                 dev_err(dev, "probe: no platform data defined\n");
2114                 return -ENODEV;
2115         }
2116
2117         if (platform_info->num_chipselect) {
2118                 num_cs = platform_info->num_chipselect;
2119         } else {
2120                 dev_err(dev, "probe: no chip select defined\n");
2121                 return -ENODEV;
2122         }
2123
2124         /* Allocate master with space for data */
2125         master = spi_alloc_master(dev, sizeof(struct pl022));
2126         if (master == NULL) {
2127                 dev_err(&adev->dev, "probe - cannot alloc SPI master\n");
2128                 return -ENOMEM;
2129         }
2130
2131         pl022 = spi_master_get_devdata(master);
2132         pl022->master = master;
2133         pl022->master_info = platform_info;
2134         pl022->adev = adev;
2135         pl022->vendor = id->data;
2136         pl022->chipselects = devm_kcalloc(dev, num_cs, sizeof(int),
2137                                           GFP_KERNEL);
2138         if (!pl022->chipselects) {
2139                 status = -ENOMEM;
2140                 goto err_no_mem;
2141         }
2142
2143         /*
2144          * Bus Number Which has been Assigned to this SSP controller
2145          * on this board
2146          */
2147         master->bus_num = platform_info->bus_id;
2148         master->num_chipselect = num_cs;
2149         master->cleanup = pl022_cleanup;
2150         master->setup = pl022_setup;
2151         master->auto_runtime_pm = true;
2152         master->transfer_one_message = pl022_transfer_one_message;
2153         master->unprepare_transfer_hardware = pl022_unprepare_transfer_hardware;
2154         master->rt = platform_info->rt;
2155         master->dev.of_node = dev->of_node;
2156
2157         if (platform_info->num_chipselect && platform_info->chipselects) {
2158                 for (i = 0; i < num_cs; i++)
2159                         pl022->chipselects[i] = platform_info->chipselects[i];
2160         } else if (pl022->vendor->internal_cs_ctrl) {
2161                 for (i = 0; i < num_cs; i++)
2162                         pl022->chipselects[i] = i;
2163         } else if (IS_ENABLED(CONFIG_OF)) {
2164                 for (i = 0; i < num_cs; i++) {
2165                         int cs_gpio = of_get_named_gpio(np, "cs-gpios", i);
2166
2167                         if (cs_gpio == -EPROBE_DEFER) {
2168                                 status = -EPROBE_DEFER;
2169                                 goto err_no_gpio;
2170                         }
2171
2172                         pl022->chipselects[i] = cs_gpio;
2173
2174                         if (gpio_is_valid(cs_gpio)) {
2175                                 if (devm_gpio_request(dev, cs_gpio, "ssp-pl022"))
2176                                         dev_err(&adev->dev,
2177                                                 "could not request %d gpio\n",
2178                                                 cs_gpio);
2179                                 else if (gpio_direction_output(cs_gpio, 1))
2180                                         dev_err(&adev->dev,
2181                                                 "could not set gpio %d as output\n",
2182                                                 cs_gpio);
2183                         }
2184                 }
2185         }
2186
2187         /*
2188          * Supports mode 0-3, loopback, and active low CS. Transfers are
2189          * always MS bit first on the original pl022.
2190          */
2191         master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
2192         if (pl022->vendor->extended_cr)
2193                 master->mode_bits |= SPI_LSB_FIRST;
2194
2195         dev_dbg(&adev->dev, "BUSNO: %d\n", master->bus_num);
2196
2197         status = amba_request_regions(adev, NULL);
2198         if (status)
2199                 goto err_no_ioregion;
2200
2201         pl022->phybase = adev->res.start;
2202         pl022->virtbase = devm_ioremap(dev, adev->res.start,
2203                                        resource_size(&adev->res));
2204         if (pl022->virtbase == NULL) {
2205                 status = -ENOMEM;
2206                 goto err_no_ioremap;
2207         }
2208         dev_info(&adev->dev, "mapped registers from %pa to %p\n",
2209                 &adev->res.start, pl022->virtbase);
2210
2211         pl022->clk = devm_clk_get(&adev->dev, NULL);
2212         if (IS_ERR(pl022->clk)) {
2213                 status = PTR_ERR(pl022->clk);
2214                 dev_err(&adev->dev, "could not retrieve SSP/SPI bus clock\n");
2215                 goto err_no_clk;
2216         }
2217
2218         status = clk_prepare_enable(pl022->clk);
2219         if (status) {
2220                 dev_err(&adev->dev, "could not enable SSP/SPI bus clock\n");
2221                 goto err_no_clk_en;
2222         }
2223
2224         /* Initialize transfer pump */
2225         tasklet_init(&pl022->pump_transfers, pump_transfers,
2226                      (unsigned long)pl022);
2227
2228         /* Disable SSP */
2229         writew((readw(SSP_CR1(pl022->virtbase)) & (~SSP_CR1_MASK_SSE)),
2230                SSP_CR1(pl022->virtbase));
2231         load_ssp_default_config(pl022);
2232
2233         status = devm_request_irq(dev, adev->irq[0], pl022_interrupt_handler,
2234                                   0, "pl022", pl022);
2235         if (status < 0) {
2236                 dev_err(&adev->dev, "probe - cannot get IRQ (%d)\n", status);
2237                 goto err_no_irq;
2238         }
2239
2240         /* Get DMA channels, try autoconfiguration first */
2241         status = pl022_dma_autoprobe(pl022);
2242         if (status == -EPROBE_DEFER) {
2243                 dev_dbg(dev, "deferring probe to get DMA channel\n");
2244                 goto err_no_irq;
2245         }
2246
2247         /* If that failed, use channels from platform_info */
2248         if (status == 0)
2249                 platform_info->enable_dma = 1;
2250         else if (platform_info->enable_dma) {
2251                 status = pl022_dma_probe(pl022);
2252                 if (status != 0)
2253                         platform_info->enable_dma = 0;
2254         }
2255
2256         /* Register with the SPI framework */
2257         amba_set_drvdata(adev, pl022);
2258         status = devm_spi_register_master(&adev->dev, master);
2259         if (status != 0) {
2260                 dev_err(&adev->dev,
2261                         "probe - problem registering spi master\n");
2262                 goto err_spi_register;
2263         }
2264         dev_dbg(dev, "probe succeeded\n");
2265
2266         /* let runtime pm put suspend */
2267         if (platform_info->autosuspend_delay > 0) {
2268                 dev_info(&adev->dev,
2269                         "will use autosuspend for runtime pm, delay %dms\n",
2270                         platform_info->autosuspend_delay);
2271                 pm_runtime_set_autosuspend_delay(dev,
2272                         platform_info->autosuspend_delay);
2273                 pm_runtime_use_autosuspend(dev);
2274         }
2275         pm_runtime_put(dev);
2276
2277         return 0;
2278
2279  err_spi_register:
2280         if (platform_info->enable_dma)
2281                 pl022_dma_remove(pl022);
2282  err_no_irq:
2283         clk_disable_unprepare(pl022->clk);
2284  err_no_clk_en:
2285  err_no_clk:
2286  err_no_ioremap:
2287         amba_release_regions(adev);
2288  err_no_ioregion:
2289  err_no_gpio:
2290  err_no_mem:
2291         spi_master_put(master);
2292         return status;
2293 }
2294
2295 static void
2296 pl022_remove(struct amba_device *adev)
2297 {
2298         struct pl022 *pl022 = amba_get_drvdata(adev);
2299
2300         if (!pl022)
2301                 return;
2302
2303         /*
2304          * undo pm_runtime_put() in probe.  I assume that we're not
2305          * accessing the primecell here.
2306          */
2307         pm_runtime_get_noresume(&adev->dev);
2308
2309         load_ssp_default_config(pl022);
2310         if (pl022->master_info->enable_dma)
2311                 pl022_dma_remove(pl022);
2312
2313         clk_disable_unprepare(pl022->clk);
2314         amba_release_regions(adev);
2315         tasklet_disable(&pl022->pump_transfers);
2316 }
2317
2318 #ifdef CONFIG_PM_SLEEP
2319 static int pl022_suspend(struct device *dev)
2320 {
2321         struct pl022 *pl022 = dev_get_drvdata(dev);
2322         int ret;
2323
2324         ret = spi_master_suspend(pl022->master);
2325         if (ret)
2326                 return ret;
2327
2328         ret = pm_runtime_force_suspend(dev);
2329         if (ret) {
2330                 spi_master_resume(pl022->master);
2331                 return ret;
2332         }
2333
2334         pinctrl_pm_select_sleep_state(dev);
2335
2336         dev_dbg(dev, "suspended\n");
2337         return 0;
2338 }
2339
2340 static int pl022_resume(struct device *dev)
2341 {
2342         struct pl022 *pl022 = dev_get_drvdata(dev);
2343         int ret;
2344
2345         ret = pm_runtime_force_resume(dev);
2346         if (ret)
2347                 dev_err(dev, "problem resuming\n");
2348
2349         /* Start the queue running */
2350         ret = spi_master_resume(pl022->master);
2351         if (!ret)
2352                 dev_dbg(dev, "resumed\n");
2353
2354         return ret;
2355 }
2356 #endif
2357
2358 #ifdef CONFIG_PM
2359 static int pl022_runtime_suspend(struct device *dev)
2360 {
2361         struct pl022 *pl022 = dev_get_drvdata(dev);
2362
2363         clk_disable_unprepare(pl022->clk);
2364         pinctrl_pm_select_idle_state(dev);
2365
2366         return 0;
2367 }
2368
2369 static int pl022_runtime_resume(struct device *dev)
2370 {
2371         struct pl022 *pl022 = dev_get_drvdata(dev);
2372
2373         pinctrl_pm_select_default_state(dev);
2374         clk_prepare_enable(pl022->clk);
2375
2376         return 0;
2377 }
2378 #endif
2379
2380 static const struct dev_pm_ops pl022_dev_pm_ops = {
2381         SET_SYSTEM_SLEEP_PM_OPS(pl022_suspend, pl022_resume)
2382         SET_RUNTIME_PM_OPS(pl022_runtime_suspend, pl022_runtime_resume, NULL)
2383 };
2384
2385 static struct vendor_data vendor_arm = {
2386         .fifodepth = 8,
2387         .max_bpw = 16,
2388         .unidir = false,
2389         .extended_cr = false,
2390         .pl023 = false,
2391         .loopback = true,
2392         .internal_cs_ctrl = false,
2393 };
2394
2395 static struct vendor_data vendor_st = {
2396         .fifodepth = 32,
2397         .max_bpw = 32,
2398         .unidir = false,
2399         .extended_cr = true,
2400         .pl023 = false,
2401         .loopback = true,
2402         .internal_cs_ctrl = false,
2403 };
2404
2405 static struct vendor_data vendor_st_pl023 = {
2406         .fifodepth = 32,
2407         .max_bpw = 32,
2408         .unidir = false,
2409         .extended_cr = true,
2410         .pl023 = true,
2411         .loopback = false,
2412         .internal_cs_ctrl = false,
2413 };
2414
2415 static struct vendor_data vendor_lsi = {
2416         .fifodepth = 8,
2417         .max_bpw = 16,
2418         .unidir = false,
2419         .extended_cr = false,
2420         .pl023 = false,
2421         .loopback = true,
2422         .internal_cs_ctrl = true,
2423 };
2424
2425 static const struct amba_id pl022_ids[] = {
2426         {
2427                 /*
2428                  * ARM PL022 variant, this has a 16bit wide
2429                  * and 8 locations deep TX/RX FIFO
2430                  */
2431                 .id     = 0x00041022,
2432                 .mask   = 0x000fffff,
2433                 .data   = &vendor_arm,
2434         },
2435         {
2436                 /*
2437                  * ST Micro derivative, this has 32bit wide
2438                  * and 32 locations deep TX/RX FIFO
2439                  */
2440                 .id     = 0x01080022,
2441                 .mask   = 0xffffffff,
2442                 .data   = &vendor_st,
2443         },
2444         {
2445                 /*
2446                  * ST-Ericsson derivative "PL023" (this is not
2447                  * an official ARM number), this is a PL022 SSP block
2448                  * stripped to SPI mode only, it has 32bit wide
2449                  * and 32 locations deep TX/RX FIFO but no extended
2450                  * CR0/CR1 register
2451                  */
2452                 .id     = 0x00080023,
2453                 .mask   = 0xffffffff,
2454                 .data   = &vendor_st_pl023,
2455         },
2456         {
2457                 /*
2458                  * PL022 variant that has a chip select control register whih
2459                  * allows control of 5 output signals nCS[0:4].
2460                  */
2461                 .id     = 0x000b6022,
2462                 .mask   = 0x000fffff,
2463                 .data   = &vendor_lsi,
2464         },
2465         { 0, 0 },
2466 };
2467
2468 MODULE_DEVICE_TABLE(amba, pl022_ids);
2469
2470 static struct amba_driver pl022_driver = {
2471         .drv = {
2472                 .name   = "ssp-pl022",
2473                 .pm     = &pl022_dev_pm_ops,
2474         },
2475         .id_table       = pl022_ids,
2476         .probe          = pl022_probe,
2477         .remove         = pl022_remove,
2478 };
2479
2480 static int __init pl022_init(void)
2481 {
2482         return amba_driver_register(&pl022_driver);
2483 }
2484 subsys_initcall(pl022_init);
2485
2486 static void __exit pl022_exit(void)
2487 {
2488         amba_driver_unregister(&pl022_driver);
2489 }
2490 module_exit(pl022_exit);
2491
2492 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
2493 MODULE_DESCRIPTION("PL022 SSP Controller Driver");
2494 MODULE_LICENSE("GPL");