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