]> Git Repo - linux.git/blob - drivers/spi/spi-pxa2xx.c
Merge patch series "riscv: Extension parsing fixes"
[linux.git] / drivers / spi / spi-pxa2xx.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
4  * Copyright (C) 2013, 2021 Intel Corporation
5  */
6
7 #include <linux/acpi.h>
8 #include <linux/atomic.h>
9 #include <linux/bitops.h>
10 #include <linux/bug.h>
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/dmaengine.h>
15 #include <linux/err.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/io.h>
20 #include <linux/ioport.h>
21 #include <linux/math64.h>
22 #include <linux/minmax.h>
23 #include <linux/mod_devicetable.h>
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/property.h>
28 #include <linux/slab.h>
29 #include <linux/types.h>
30
31 #include <linux/spi/spi.h>
32
33 #include "spi-pxa2xx.h"
34
35 MODULE_AUTHOR("Stephen Street");
36 MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
37 MODULE_LICENSE("GPL");
38 MODULE_ALIAS("platform:pxa2xx-spi");
39
40 #define TIMOUT_DFLT             1000
41
42 /*
43  * For testing SSCR1 changes that require SSP restart, basically
44  * everything except the service and interrupt enables, the PXA270 developer
45  * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
46  * list, but the PXA255 developer manual says all bits without really meaning
47  * the service and interrupt enables.
48  */
49 #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
50                                 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
51                                 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
52                                 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
53                                 | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
54                                 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
55
56 #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF   \
57                                 | QUARK_X1000_SSCR1_EFWR        \
58                                 | QUARK_X1000_SSCR1_RFT         \
59                                 | QUARK_X1000_SSCR1_TFT         \
60                                 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
61
62 #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
63                                 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
64                                 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
65                                 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
66                                 | CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
67                                 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
68
69 struct chip_data {
70         u32 cr1;
71         u32 dds_rate;
72         u32 threshold;
73         u16 lpss_rx_threshold;
74         u16 lpss_tx_threshold;
75 };
76
77 #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE   BIT(24)
78 #define LPSS_CS_CONTROL_SW_MODE                 BIT(0)
79 #define LPSS_CS_CONTROL_CS_HIGH                 BIT(1)
80 #define LPSS_CAPS_CS_EN_SHIFT                   9
81 #define LPSS_CAPS_CS_EN_MASK                    (0xf << LPSS_CAPS_CS_EN_SHIFT)
82
83 #define LPSS_PRIV_CLOCK_GATE 0x38
84 #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
85 #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
86
87 struct lpss_config {
88         /* LPSS offset from drv_data->ioaddr */
89         unsigned offset;
90         /* Register offsets from drv_data->lpss_base or -1 */
91         int reg_general;
92         int reg_ssp;
93         int reg_cs_ctrl;
94         int reg_capabilities;
95         /* FIFO thresholds */
96         u32 rx_threshold;
97         u32 tx_threshold_lo;
98         u32 tx_threshold_hi;
99         /* Chip select control */
100         unsigned cs_sel_shift;
101         unsigned cs_sel_mask;
102         unsigned cs_num;
103         /* Quirks */
104         unsigned cs_clk_stays_gated : 1;
105 };
106
107 /* Keep these sorted with enum pxa_ssp_type */
108 static const struct lpss_config lpss_platforms[] = {
109         {       /* LPSS_LPT_SSP */
110                 .offset = 0x800,
111                 .reg_general = 0x08,
112                 .reg_ssp = 0x0c,
113                 .reg_cs_ctrl = 0x18,
114                 .reg_capabilities = -1,
115                 .rx_threshold = 64,
116                 .tx_threshold_lo = 160,
117                 .tx_threshold_hi = 224,
118         },
119         {       /* LPSS_BYT_SSP */
120                 .offset = 0x400,
121                 .reg_general = 0x08,
122                 .reg_ssp = 0x0c,
123                 .reg_cs_ctrl = 0x18,
124                 .reg_capabilities = -1,
125                 .rx_threshold = 64,
126                 .tx_threshold_lo = 160,
127                 .tx_threshold_hi = 224,
128         },
129         {       /* LPSS_BSW_SSP */
130                 .offset = 0x400,
131                 .reg_general = 0x08,
132                 .reg_ssp = 0x0c,
133                 .reg_cs_ctrl = 0x18,
134                 .reg_capabilities = -1,
135                 .rx_threshold = 64,
136                 .tx_threshold_lo = 160,
137                 .tx_threshold_hi = 224,
138                 .cs_sel_shift = 2,
139                 .cs_sel_mask = 1 << 2,
140                 .cs_num = 2,
141         },
142         {       /* LPSS_SPT_SSP */
143                 .offset = 0x200,
144                 .reg_general = -1,
145                 .reg_ssp = 0x20,
146                 .reg_cs_ctrl = 0x24,
147                 .reg_capabilities = -1,
148                 .rx_threshold = 1,
149                 .tx_threshold_lo = 32,
150                 .tx_threshold_hi = 56,
151         },
152         {       /* LPSS_BXT_SSP */
153                 .offset = 0x200,
154                 .reg_general = -1,
155                 .reg_ssp = 0x20,
156                 .reg_cs_ctrl = 0x24,
157                 .reg_capabilities = 0xfc,
158                 .rx_threshold = 1,
159                 .tx_threshold_lo = 16,
160                 .tx_threshold_hi = 48,
161                 .cs_sel_shift = 8,
162                 .cs_sel_mask = 3 << 8,
163                 .cs_clk_stays_gated = true,
164         },
165         {       /* LPSS_CNL_SSP */
166                 .offset = 0x200,
167                 .reg_general = -1,
168                 .reg_ssp = 0x20,
169                 .reg_cs_ctrl = 0x24,
170                 .reg_capabilities = 0xfc,
171                 .rx_threshold = 1,
172                 .tx_threshold_lo = 32,
173                 .tx_threshold_hi = 56,
174                 .cs_sel_shift = 8,
175                 .cs_sel_mask = 3 << 8,
176                 .cs_clk_stays_gated = true,
177         },
178 };
179
180 static inline const struct lpss_config
181 *lpss_get_config(const struct driver_data *drv_data)
182 {
183         return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
184 }
185
186 static bool is_lpss_ssp(const struct driver_data *drv_data)
187 {
188         switch (drv_data->ssp_type) {
189         case LPSS_LPT_SSP:
190         case LPSS_BYT_SSP:
191         case LPSS_BSW_SSP:
192         case LPSS_SPT_SSP:
193         case LPSS_BXT_SSP:
194         case LPSS_CNL_SSP:
195                 return true;
196         default:
197                 return false;
198         }
199 }
200
201 static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
202 {
203         return drv_data->ssp_type == QUARK_X1000_SSP;
204 }
205
206 static bool is_mmp2_ssp(const struct driver_data *drv_data)
207 {
208         return drv_data->ssp_type == MMP2_SSP;
209 }
210
211 static bool is_mrfld_ssp(const struct driver_data *drv_data)
212 {
213         return drv_data->ssp_type == MRFLD_SSP;
214 }
215
216 static void pxa2xx_spi_update(const struct driver_data *drv_data, u32 reg, u32 mask, u32 value)
217 {
218         if ((pxa2xx_spi_read(drv_data, reg) & mask) != value)
219                 pxa2xx_spi_write(drv_data, reg, value & mask);
220 }
221
222 static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
223 {
224         switch (drv_data->ssp_type) {
225         case QUARK_X1000_SSP:
226                 return QUARK_X1000_SSCR1_CHANGE_MASK;
227         case CE4100_SSP:
228                 return CE4100_SSCR1_CHANGE_MASK;
229         default:
230                 return SSCR1_CHANGE_MASK;
231         }
232 }
233
234 static u32
235 pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
236 {
237         switch (drv_data->ssp_type) {
238         case QUARK_X1000_SSP:
239                 return RX_THRESH_QUARK_X1000_DFLT;
240         case CE4100_SSP:
241                 return RX_THRESH_CE4100_DFLT;
242         default:
243                 return RX_THRESH_DFLT;
244         }
245 }
246
247 static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
248 {
249         u32 mask;
250
251         switch (drv_data->ssp_type) {
252         case QUARK_X1000_SSP:
253                 mask = QUARK_X1000_SSSR_TFL_MASK;
254                 break;
255         case CE4100_SSP:
256                 mask = CE4100_SSSR_TFL_MASK;
257                 break;
258         default:
259                 mask = SSSR_TFL_MASK;
260                 break;
261         }
262
263         return read_SSSR_bits(drv_data, mask) == mask;
264 }
265
266 static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
267                                      u32 *sccr1_reg)
268 {
269         u32 mask;
270
271         switch (drv_data->ssp_type) {
272         case QUARK_X1000_SSP:
273                 mask = QUARK_X1000_SSCR1_RFT;
274                 break;
275         case CE4100_SSP:
276                 mask = CE4100_SSCR1_RFT;
277                 break;
278         default:
279                 mask = SSCR1_RFT;
280                 break;
281         }
282         *sccr1_reg &= ~mask;
283 }
284
285 static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
286                                    u32 *sccr1_reg, u32 threshold)
287 {
288         switch (drv_data->ssp_type) {
289         case QUARK_X1000_SSP:
290                 *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
291                 break;
292         case CE4100_SSP:
293                 *sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
294                 break;
295         default:
296                 *sccr1_reg |= SSCR1_RxTresh(threshold);
297                 break;
298         }
299 }
300
301 static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
302                                   u32 clk_div, u8 bits)
303 {
304         switch (drv_data->ssp_type) {
305         case QUARK_X1000_SSP:
306                 return clk_div
307                         | QUARK_X1000_SSCR0_Motorola
308                         | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits);
309         default:
310                 return clk_div
311                         | SSCR0_Motorola
312                         | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
313                         | (bits > 16 ? SSCR0_EDSS : 0);
314         }
315 }
316
317 /*
318  * Read and write LPSS SSP private registers. Caller must first check that
319  * is_lpss_ssp() returns true before these can be called.
320  */
321 static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
322 {
323         WARN_ON(!drv_data->lpss_base);
324         return readl(drv_data->lpss_base + offset);
325 }
326
327 static void __lpss_ssp_write_priv(struct driver_data *drv_data,
328                                   unsigned offset, u32 value)
329 {
330         WARN_ON(!drv_data->lpss_base);
331         writel(value, drv_data->lpss_base + offset);
332 }
333
334 /*
335  * lpss_ssp_setup - perform LPSS SSP specific setup
336  * @drv_data: pointer to the driver private data
337  *
338  * Perform LPSS SSP specific setup. This function must be called first if
339  * one is going to use LPSS SSP private registers.
340  */
341 static void lpss_ssp_setup(struct driver_data *drv_data)
342 {
343         const struct lpss_config *config;
344         u32 value;
345
346         config = lpss_get_config(drv_data);
347         drv_data->lpss_base = drv_data->ssp->mmio_base + config->offset;
348
349         /* Enable software chip select control */
350         value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
351         value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
352         value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
353         __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
354
355         /* Enable multiblock DMA transfers */
356         if (drv_data->controller_info->enable_dma) {
357                 __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
358
359                 if (config->reg_general >= 0) {
360                         value = __lpss_ssp_read_priv(drv_data,
361                                                      config->reg_general);
362                         value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
363                         __lpss_ssp_write_priv(drv_data,
364                                               config->reg_general, value);
365                 }
366         }
367 }
368
369 static void lpss_ssp_select_cs(struct spi_device *spi,
370                                const struct lpss_config *config)
371 {
372         struct driver_data *drv_data =
373                 spi_controller_get_devdata(spi->controller);
374         u32 value, cs;
375
376         if (!config->cs_sel_mask)
377                 return;
378
379         value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
380
381         cs = spi_get_chipselect(spi, 0);
382         cs <<= config->cs_sel_shift;
383         if (cs != (value & config->cs_sel_mask)) {
384                 /*
385                  * When switching another chip select output active the
386                  * output must be selected first and wait 2 ssp_clk cycles
387                  * before changing state to active. Otherwise a short
388                  * glitch will occur on the previous chip select since
389                  * output select is latched but state control is not.
390                  */
391                 value &= ~config->cs_sel_mask;
392                 value |= cs;
393                 __lpss_ssp_write_priv(drv_data,
394                                       config->reg_cs_ctrl, value);
395                 ndelay(1000000000 /
396                        (drv_data->controller->max_speed_hz / 2));
397         }
398 }
399
400 static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
401 {
402         struct driver_data *drv_data =
403                 spi_controller_get_devdata(spi->controller);
404         const struct lpss_config *config;
405         u32 value;
406
407         config = lpss_get_config(drv_data);
408
409         if (enable)
410                 lpss_ssp_select_cs(spi, config);
411
412         value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
413         if (enable)
414                 value &= ~LPSS_CS_CONTROL_CS_HIGH;
415         else
416                 value |= LPSS_CS_CONTROL_CS_HIGH;
417         __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
418         if (config->cs_clk_stays_gated) {
419                 u32 clkgate;
420
421                 /*
422                  * Changing CS alone when dynamic clock gating is on won't
423                  * actually flip CS at that time. This ruins SPI transfers
424                  * that specify delays, or have no data. Toggle the clock mode
425                  * to force on briefly to poke the CS pin to move.
426                  */
427                 clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
428                 value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
429                         LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
430
431                 __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
432                 __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
433         }
434 }
435
436 static void cs_assert(struct spi_device *spi)
437 {
438         struct driver_data *drv_data =
439                 spi_controller_get_devdata(spi->controller);
440
441         if (drv_data->ssp_type == CE4100_SSP) {
442                 pxa2xx_spi_write(drv_data, SSSR, spi_get_chipselect(spi, 0));
443                 return;
444         }
445
446         if (is_lpss_ssp(drv_data))
447                 lpss_ssp_cs_control(spi, true);
448 }
449
450 static void cs_deassert(struct spi_device *spi)
451 {
452         struct driver_data *drv_data =
453                 spi_controller_get_devdata(spi->controller);
454         unsigned long timeout;
455
456         if (drv_data->ssp_type == CE4100_SSP)
457                 return;
458
459         /* Wait until SSP becomes idle before deasserting the CS */
460         timeout = jiffies + msecs_to_jiffies(10);
461         while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
462                !time_after(jiffies, timeout))
463                 cpu_relax();
464
465         if (is_lpss_ssp(drv_data))
466                 lpss_ssp_cs_control(spi, false);
467 }
468
469 static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
470 {
471         if (level)
472                 cs_deassert(spi);
473         else
474                 cs_assert(spi);
475 }
476
477 int pxa2xx_spi_flush(struct driver_data *drv_data)
478 {
479         unsigned long limit = loops_per_jiffy << 1;
480
481         do {
482                 while (read_SSSR_bits(drv_data, SSSR_RNE))
483                         pxa2xx_spi_read(drv_data, SSDR);
484         } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
485         write_SSSR_CS(drv_data, SSSR_ROR);
486
487         return limit;
488 }
489
490 static void pxa2xx_spi_off(struct driver_data *drv_data)
491 {
492         /* On MMP, disabling SSE seems to corrupt the Rx FIFO */
493         if (is_mmp2_ssp(drv_data))
494                 return;
495
496         pxa_ssp_disable(drv_data->ssp);
497 }
498
499 static int null_writer(struct driver_data *drv_data)
500 {
501         u8 n_bytes = drv_data->n_bytes;
502
503         if (pxa2xx_spi_txfifo_full(drv_data)
504                 || (drv_data->tx == drv_data->tx_end))
505                 return 0;
506
507         pxa2xx_spi_write(drv_data, SSDR, 0);
508         drv_data->tx += n_bytes;
509
510         return 1;
511 }
512
513 static int null_reader(struct driver_data *drv_data)
514 {
515         u8 n_bytes = drv_data->n_bytes;
516
517         while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
518                 pxa2xx_spi_read(drv_data, SSDR);
519                 drv_data->rx += n_bytes;
520         }
521
522         return drv_data->rx == drv_data->rx_end;
523 }
524
525 static int u8_writer(struct driver_data *drv_data)
526 {
527         if (pxa2xx_spi_txfifo_full(drv_data)
528                 || (drv_data->tx == drv_data->tx_end))
529                 return 0;
530
531         pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
532         ++drv_data->tx;
533
534         return 1;
535 }
536
537 static int u8_reader(struct driver_data *drv_data)
538 {
539         while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
540                 *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
541                 ++drv_data->rx;
542         }
543
544         return drv_data->rx == drv_data->rx_end;
545 }
546
547 static int u16_writer(struct driver_data *drv_data)
548 {
549         if (pxa2xx_spi_txfifo_full(drv_data)
550                 || (drv_data->tx == drv_data->tx_end))
551                 return 0;
552
553         pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
554         drv_data->tx += 2;
555
556         return 1;
557 }
558
559 static int u16_reader(struct driver_data *drv_data)
560 {
561         while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
562                 *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
563                 drv_data->rx += 2;
564         }
565
566         return drv_data->rx == drv_data->rx_end;
567 }
568
569 static int u32_writer(struct driver_data *drv_data)
570 {
571         if (pxa2xx_spi_txfifo_full(drv_data)
572                 || (drv_data->tx == drv_data->tx_end))
573                 return 0;
574
575         pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
576         drv_data->tx += 4;
577
578         return 1;
579 }
580
581 static int u32_reader(struct driver_data *drv_data)
582 {
583         while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
584                 *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
585                 drv_data->rx += 4;
586         }
587
588         return drv_data->rx == drv_data->rx_end;
589 }
590
591 static void reset_sccr1(struct driver_data *drv_data)
592 {
593         u32 mask = drv_data->int_cr1 | drv_data->dma_cr1, threshold;
594         struct chip_data *chip;
595
596         if (drv_data->controller->cur_msg) {
597                 chip = spi_get_ctldata(drv_data->controller->cur_msg->spi);
598                 threshold = chip->threshold;
599         } else {
600                 threshold = 0;
601         }
602
603         switch (drv_data->ssp_type) {
604         case QUARK_X1000_SSP:
605                 mask |= QUARK_X1000_SSCR1_RFT;
606                 break;
607         case CE4100_SSP:
608                 mask |= CE4100_SSCR1_RFT;
609                 break;
610         default:
611                 mask |= SSCR1_RFT;
612                 break;
613         }
614
615         pxa2xx_spi_update(drv_data, SSCR1, mask, threshold);
616 }
617
618 static void int_stop_and_reset(struct driver_data *drv_data)
619 {
620         /* Clear and disable interrupts */
621         write_SSSR_CS(drv_data, drv_data->clear_sr);
622         reset_sccr1(drv_data);
623         if (pxa25x_ssp_comp(drv_data))
624                 return;
625
626         pxa2xx_spi_write(drv_data, SSTO, 0);
627 }
628
629 static void int_error_stop(struct driver_data *drv_data, const char *msg, int err)
630 {
631         int_stop_and_reset(drv_data);
632         pxa2xx_spi_flush(drv_data);
633         pxa2xx_spi_off(drv_data);
634
635         dev_err(drv_data->ssp->dev, "%s\n", msg);
636
637         drv_data->controller->cur_msg->status = err;
638         spi_finalize_current_transfer(drv_data->controller);
639 }
640
641 static void int_transfer_complete(struct driver_data *drv_data)
642 {
643         int_stop_and_reset(drv_data);
644
645         spi_finalize_current_transfer(drv_data->controller);
646 }
647
648 static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
649 {
650         u32 irq_status;
651
652         irq_status = read_SSSR_bits(drv_data, drv_data->mask_sr);
653         if (!(pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE))
654                 irq_status &= ~SSSR_TFS;
655
656         if (irq_status & SSSR_ROR) {
657                 int_error_stop(drv_data, "interrupt_transfer: FIFO overrun", -EIO);
658                 return IRQ_HANDLED;
659         }
660
661         if (irq_status & SSSR_TUR) {
662                 int_error_stop(drv_data, "interrupt_transfer: FIFO underrun", -EIO);
663                 return IRQ_HANDLED;
664         }
665
666         if (irq_status & SSSR_TINT) {
667                 pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
668                 if (drv_data->read(drv_data)) {
669                         int_transfer_complete(drv_data);
670                         return IRQ_HANDLED;
671                 }
672         }
673
674         /* Drain Rx FIFO, Fill Tx FIFO and prevent overruns */
675         do {
676                 if (drv_data->read(drv_data)) {
677                         int_transfer_complete(drv_data);
678                         return IRQ_HANDLED;
679                 }
680         } while (drv_data->write(drv_data));
681
682         if (drv_data->read(drv_data)) {
683                 int_transfer_complete(drv_data);
684                 return IRQ_HANDLED;
685         }
686
687         if (drv_data->tx == drv_data->tx_end) {
688                 u32 bytes_left;
689                 u32 sccr1_reg;
690
691                 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
692                 sccr1_reg &= ~SSCR1_TIE;
693
694                 /*
695                  * PXA25x_SSP has no timeout, set up Rx threshold for
696                  * the remaining Rx bytes.
697                  */
698                 if (pxa25x_ssp_comp(drv_data)) {
699                         u32 rx_thre;
700
701                         pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
702
703                         bytes_left = drv_data->rx_end - drv_data->rx;
704                         switch (drv_data->n_bytes) {
705                         case 4:
706                                 bytes_left >>= 2;
707                                 break;
708                         case 2:
709                                 bytes_left >>= 1;
710                                 break;
711                         }
712
713                         rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
714                         if (rx_thre > bytes_left)
715                                 rx_thre = bytes_left;
716
717                         pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
718                 }
719                 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
720         }
721
722         /* We did something */
723         return IRQ_HANDLED;
724 }
725
726 static void handle_bad_msg(struct driver_data *drv_data)
727 {
728         int_stop_and_reset(drv_data);
729         pxa2xx_spi_off(drv_data);
730
731         dev_err(drv_data->ssp->dev, "bad message state in interrupt handler\n");
732 }
733
734 static irqreturn_t ssp_int(int irq, void *dev_id)
735 {
736         struct driver_data *drv_data = dev_id;
737         u32 sccr1_reg;
738         u32 mask = drv_data->mask_sr;
739         u32 status;
740
741         /*
742          * The IRQ might be shared with other peripherals so we must first
743          * check that are we RPM suspended or not. If we are we assume that
744          * the IRQ was not for us (we shouldn't be RPM suspended when the
745          * interrupt is enabled).
746          */
747         if (pm_runtime_suspended(drv_data->ssp->dev))
748                 return IRQ_NONE;
749
750         /*
751          * If the device is not yet in RPM suspended state and we get an
752          * interrupt that is meant for another device, check if status bits
753          * are all set to one. That means that the device is already
754          * powered off.
755          */
756         status = pxa2xx_spi_read(drv_data, SSSR);
757         if (status == ~0)
758                 return IRQ_NONE;
759
760         sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
761
762         /* Ignore possible writes if we don't need to write */
763         if (!(sccr1_reg & SSCR1_TIE))
764                 mask &= ~SSSR_TFS;
765
766         /* Ignore RX timeout interrupt if it is disabled */
767         if (!(sccr1_reg & SSCR1_TINTE))
768                 mask &= ~SSSR_TINT;
769
770         if (!(status & mask))
771                 return IRQ_NONE;
772
773         pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
774         pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
775
776         if (!drv_data->controller->cur_msg) {
777                 handle_bad_msg(drv_data);
778                 /* Never fail */
779                 return IRQ_HANDLED;
780         }
781
782         return drv_data->transfer_handler(drv_data);
783 }
784
785 /*
786  * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
787  * input frequency by fractions of 2^24. It also has a divider by 5.
788  *
789  * There are formulas to get baud rate value for given input frequency and
790  * divider parameters, such as DDS_CLK_RATE and SCR:
791  *
792  * Fsys = 200MHz
793  *
794  * Fssp = Fsys * DDS_CLK_RATE / 2^24                    (1)
795  * Baud rate = Fsclk = Fssp / (2 * (SCR + 1))           (2)
796  *
797  * DDS_CLK_RATE either 2^n or 2^n / 5.
798  * SCR is in range 0 .. 255
799  *
800  * Divisor = 5^i * 2^j * 2 * k
801  *       i = [0, 1]      i = 1 iff j = 0 or j > 3
802  *       j = [0, 23]     j = 0 iff i = 1
803  *       k = [1, 256]
804  * Special case: j = 0, i = 1: Divisor = 2 / 5
805  *
806  * Accordingly to the specification the recommended values for DDS_CLK_RATE
807  * are:
808  *      Case 1:         2^n, n = [0, 23]
809  *      Case 2:         2^24 * 2 / 5 (0x666666)
810  *      Case 3:         less than or equal to 2^24 / 5 / 16 (0x33333)
811  *
812  * In all cases the lowest possible value is better.
813  *
814  * The function calculates parameters for all cases and chooses the one closest
815  * to the asked baud rate.
816  */
817 static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
818 {
819         unsigned long xtal = 200000000;
820         unsigned long fref = xtal / 2;          /* mandatory division by 2,
821                                                    see (2) */
822                                                 /* case 3 */
823         unsigned long fref1 = fref / 2;         /* case 1 */
824         unsigned long fref2 = fref * 2 / 5;     /* case 2 */
825         unsigned long scale;
826         unsigned long q, q1, q2;
827         long r, r1, r2;
828         u32 mul;
829
830         /* Case 1 */
831
832         /* Set initial value for DDS_CLK_RATE */
833         mul = (1 << 24) >> 1;
834
835         /* Calculate initial quot */
836         q1 = DIV_ROUND_UP(fref1, rate);
837
838         /* Scale q1 if it's too big */
839         if (q1 > 256) {
840                 /* Scale q1 to range [1, 512] */
841                 scale = fls_long(q1 - 1);
842                 if (scale > 9) {
843                         q1 >>= scale - 9;
844                         mul >>= scale - 9;
845                 }
846
847                 /* Round the result if we have a remainder */
848                 q1 += q1 & 1;
849         }
850
851         /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
852         scale = __ffs(q1);
853         q1 >>= scale;
854         mul >>= scale;
855
856         /* Get the remainder */
857         r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
858
859         /* Case 2 */
860
861         q2 = DIV_ROUND_UP(fref2, rate);
862         r2 = abs(fref2 / q2 - rate);
863
864         /*
865          * Choose the best between two: less remainder we have the better. We
866          * can't go case 2 if q2 is greater than 256 since SCR register can
867          * hold only values 0 .. 255.
868          */
869         if (r2 >= r1 || q2 > 256) {
870                 /* case 1 is better */
871                 r = r1;
872                 q = q1;
873         } else {
874                 /* case 2 is better */
875                 r = r2;
876                 q = q2;
877                 mul = (1 << 24) * 2 / 5;
878         }
879
880         /* Check case 3 only if the divisor is big enough */
881         if (fref / rate >= 80) {
882                 u64 fssp;
883                 u32 m;
884
885                 /* Calculate initial quot */
886                 q1 = DIV_ROUND_UP(fref, rate);
887                 m = (1 << 24) / q1;
888
889                 /* Get the remainder */
890                 fssp = (u64)fref * m;
891                 do_div(fssp, 1 << 24);
892                 r1 = abs(fssp - rate);
893
894                 /* Choose this one if it suits better */
895                 if (r1 < r) {
896                         /* case 3 is better */
897                         q = 1;
898                         mul = m;
899                 }
900         }
901
902         *dds = mul;
903         return q - 1;
904 }
905
906 static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
907 {
908         unsigned long ssp_clk = drv_data->controller->max_speed_hz;
909         const struct ssp_device *ssp = drv_data->ssp;
910
911         rate = min_t(int, ssp_clk, rate);
912
913         /*
914          * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
915          * that the SSP transmission rate can be greater than the device rate.
916          */
917         if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
918                 return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
919         else
920                 return (DIV_ROUND_UP(ssp_clk, rate) - 1)  & 0xfff;
921 }
922
923 static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
924                                            int rate)
925 {
926         struct chip_data *chip =
927                 spi_get_ctldata(drv_data->controller->cur_msg->spi);
928         unsigned int clk_div;
929
930         switch (drv_data->ssp_type) {
931         case QUARK_X1000_SSP:
932                 clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
933                 break;
934         default:
935                 clk_div = ssp_get_clk_div(drv_data, rate);
936                 break;
937         }
938         return clk_div << 8;
939 }
940
941 static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
942                                struct spi_device *spi,
943                                struct spi_transfer *xfer)
944 {
945         struct driver_data *drv_data = spi_controller_get_devdata(controller);
946
947         return drv_data->controller_info->enable_dma &&
948                xfer->len <= MAX_DMA_LEN &&
949                xfer->len >= drv_data->controller_info->dma_burst_size;
950 }
951
952 static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
953                                    struct spi_device *spi,
954                                    struct spi_transfer *transfer)
955 {
956         struct driver_data *drv_data = spi_controller_get_devdata(controller);
957         struct chip_data *chip = spi_get_ctldata(spi);
958         u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
959         u32 dma_thresh;
960         u32 clk_div;
961         u8 bits;
962         u32 speed;
963         u32 cr0;
964         u32 cr1;
965         int err;
966         int dma_mapped;
967
968         /* Check if we can DMA this transfer */
969         if (transfer->len > MAX_DMA_LEN && drv_data->controller_info->enable_dma) {
970                 /* Warn ... we force this to PIO mode */
971                 dev_warn_ratelimited(&spi->dev,
972                                      "DMA disabled for transfer length %u greater than %d\n",
973                                      transfer->len, MAX_DMA_LEN);
974         }
975
976         /* Setup the transfer state based on the type of transfer */
977         if (pxa2xx_spi_flush(drv_data) == 0) {
978                 dev_err(&spi->dev, "Flush failed\n");
979                 return -EIO;
980         }
981         drv_data->tx = (void *)transfer->tx_buf;
982         drv_data->tx_end = drv_data->tx + transfer->len;
983         drv_data->rx = transfer->rx_buf;
984         drv_data->rx_end = drv_data->rx + transfer->len;
985
986         /* Change speed and bit per word on a per transfer */
987         bits = transfer->bits_per_word;
988         speed = transfer->speed_hz;
989
990         clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
991
992         if (bits <= 8) {
993                 drv_data->n_bytes = 1;
994                 drv_data->read = drv_data->rx ? u8_reader : null_reader;
995                 drv_data->write = drv_data->tx ? u8_writer : null_writer;
996         } else if (bits <= 16) {
997                 drv_data->n_bytes = 2;
998                 drv_data->read = drv_data->rx ? u16_reader : null_reader;
999                 drv_data->write = drv_data->tx ? u16_writer : null_writer;
1000         } else if (bits <= 32) {
1001                 drv_data->n_bytes = 4;
1002                 drv_data->read = drv_data->rx ? u32_reader : null_reader;
1003                 drv_data->write = drv_data->tx ? u32_writer : null_writer;
1004         }
1005
1006         dma_thresh = SSCR1_RxTresh(RX_THRESH_DFLT) | SSCR1_TxTresh(TX_THRESH_DFLT);
1007         dma_mapped = controller->can_dma &&
1008                      controller->can_dma(controller, spi, transfer) &&
1009                      controller->cur_msg_mapped;
1010         if (dma_mapped) {
1011
1012                 /* Ensure we have the correct interrupt handler */
1013                 drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
1014
1015                 err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1016                 if (err)
1017                         return err;
1018
1019                 /* Clear status and start DMA engine */
1020                 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1021                 pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1022
1023                 pxa2xx_spi_dma_start(drv_data);
1024         } else {
1025                 /* Ensure we have the correct interrupt handler */
1026                 drv_data->transfer_handler = interrupt_transfer;
1027
1028                 /* Clear status  */
1029                 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1030                 write_SSSR_CS(drv_data, drv_data->clear_sr);
1031         }
1032
1033         /* NOTE:  PXA25x_SSP _could_ use external clocking ... */
1034         cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1035         if (!pxa25x_ssp_comp(drv_data))
1036                 dev_dbg(&spi->dev, "%u Hz actual, %s\n",
1037                         controller->max_speed_hz
1038                                 / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
1039                         dma_mapped ? "DMA" : "PIO");
1040         else
1041                 dev_dbg(&spi->dev, "%u Hz actual, %s\n",
1042                         controller->max_speed_hz / 2
1043                                 / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1044                         dma_mapped ? "DMA" : "PIO");
1045
1046         if (is_lpss_ssp(drv_data)) {
1047                 pxa2xx_spi_update(drv_data, SSIRF, GENMASK(7, 0), chip->lpss_rx_threshold);
1048                 pxa2xx_spi_update(drv_data, SSITF, GENMASK(15, 0), chip->lpss_tx_threshold);
1049         }
1050
1051         if (is_mrfld_ssp(drv_data)) {
1052                 u32 mask = SFIFOTT_RFT | SFIFOTT_TFT;
1053                 u32 thresh = 0;
1054
1055                 thresh |= SFIFOTT_RxThresh(chip->lpss_rx_threshold);
1056                 thresh |= SFIFOTT_TxThresh(chip->lpss_tx_threshold);
1057
1058                 pxa2xx_spi_update(drv_data, SFIFOTT, mask, thresh);
1059         }
1060
1061         if (is_quark_x1000_ssp(drv_data))
1062                 pxa2xx_spi_update(drv_data, DDS_RATE, GENMASK(23, 0), chip->dds_rate);
1063
1064         /* Stop the SSP */
1065         if (!is_mmp2_ssp(drv_data))
1066                 pxa_ssp_disable(drv_data->ssp);
1067
1068         if (!pxa25x_ssp_comp(drv_data))
1069                 pxa2xx_spi_write(drv_data, SSTO, TIMOUT_DFLT);
1070
1071         /* First set CR1 without interrupt and service enables */
1072         pxa2xx_spi_update(drv_data, SSCR1, change_mask, cr1);
1073
1074         /* See if we need to reload the configuration registers */
1075         pxa2xx_spi_update(drv_data, SSCR0, GENMASK(31, 0), cr0);
1076
1077         /* Restart the SSP */
1078         pxa_ssp_enable(drv_data->ssp);
1079
1080         if (is_mmp2_ssp(drv_data)) {
1081                 u8 tx_level = read_SSSR_bits(drv_data, SSSR_TFL_MASK) >> 8;
1082
1083                 if (tx_level) {
1084                         /* On MMP2, flipping SSE doesn't to empty Tx FIFO. */
1085                         dev_warn(&spi->dev, "%u bytes of garbage in Tx FIFO!\n", tx_level);
1086                         if (tx_level > transfer->len)
1087                                 tx_level = transfer->len;
1088                         drv_data->tx += tx_level;
1089                 }
1090         }
1091
1092         if (spi_controller_is_target(controller)) {
1093                 while (drv_data->write(drv_data))
1094                         ;
1095                 if (drv_data->gpiod_ready) {
1096                         gpiod_set_value(drv_data->gpiod_ready, 1);
1097                         udelay(1);
1098                         gpiod_set_value(drv_data->gpiod_ready, 0);
1099                 }
1100         }
1101
1102         /*
1103          * Release the data by enabling service requests and interrupts,
1104          * without changing any mode bits.
1105          */
1106         pxa2xx_spi_write(drv_data, SSCR1, cr1);
1107
1108         return 1;
1109 }
1110
1111 static int pxa2xx_spi_target_abort(struct spi_controller *controller)
1112 {
1113         struct driver_data *drv_data = spi_controller_get_devdata(controller);
1114
1115         int_error_stop(drv_data, "transfer aborted", -EINTR);
1116
1117         return 0;
1118 }
1119
1120 static void pxa2xx_spi_handle_err(struct spi_controller *controller,
1121                                  struct spi_message *msg)
1122 {
1123         struct driver_data *drv_data = spi_controller_get_devdata(controller);
1124
1125         int_stop_and_reset(drv_data);
1126
1127         /* Disable the SSP */
1128         pxa2xx_spi_off(drv_data);
1129
1130         /*
1131          * Stop the DMA if running. Note DMA callback handler may have unset
1132          * the dma_running already, which is fine as stopping is not needed
1133          * then but we shouldn't rely this flag for anything else than
1134          * stopping. For instance to differentiate between PIO and DMA
1135          * transfers.
1136          */
1137         if (atomic_read(&drv_data->dma_running))
1138                 pxa2xx_spi_dma_stop(drv_data);
1139 }
1140
1141 static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
1142 {
1143         struct driver_data *drv_data = spi_controller_get_devdata(controller);
1144
1145         /* Disable the SSP now */
1146         pxa2xx_spi_off(drv_data);
1147
1148         return 0;
1149 }
1150
1151 static int setup(struct spi_device *spi)
1152 {
1153         struct chip_data *chip;
1154         const struct lpss_config *config;
1155         struct driver_data *drv_data =
1156                 spi_controller_get_devdata(spi->controller);
1157         uint tx_thres, tx_hi_thres, rx_thres;
1158
1159         switch (drv_data->ssp_type) {
1160         case QUARK_X1000_SSP:
1161                 tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1162                 tx_hi_thres = 0;
1163                 rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1164                 break;
1165         case MRFLD_SSP:
1166                 tx_thres = TX_THRESH_MRFLD_DFLT;
1167                 tx_hi_thres = 0;
1168                 rx_thres = RX_THRESH_MRFLD_DFLT;
1169                 break;
1170         case CE4100_SSP:
1171                 tx_thres = TX_THRESH_CE4100_DFLT;
1172                 tx_hi_thres = 0;
1173                 rx_thres = RX_THRESH_CE4100_DFLT;
1174                 break;
1175         case LPSS_LPT_SSP:
1176         case LPSS_BYT_SSP:
1177         case LPSS_BSW_SSP:
1178         case LPSS_SPT_SSP:
1179         case LPSS_BXT_SSP:
1180         case LPSS_CNL_SSP:
1181                 config = lpss_get_config(drv_data);
1182                 tx_thres = config->tx_threshold_lo;
1183                 tx_hi_thres = config->tx_threshold_hi;
1184                 rx_thres = config->rx_threshold;
1185                 break;
1186         default:
1187                 tx_hi_thres = 0;
1188                 if (spi_controller_is_target(drv_data->controller)) {
1189                         tx_thres = 1;
1190                         rx_thres = 2;
1191                 } else {
1192                         tx_thres = TX_THRESH_DFLT;
1193                         rx_thres = RX_THRESH_DFLT;
1194                 }
1195                 break;
1196         }
1197
1198         if (drv_data->ssp_type == CE4100_SSP) {
1199                 if (spi_get_chipselect(spi, 0) > 4) {
1200                         dev_err(&spi->dev, "failed setup: cs number must not be > 4.\n");
1201                         return -EINVAL;
1202                 }
1203         }
1204
1205         /* Only allocate on the first setup */
1206         chip = spi_get_ctldata(spi);
1207         if (!chip) {
1208                 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1209                 if (!chip)
1210                         return -ENOMEM;
1211         }
1212
1213         chip->cr1 = 0;
1214         if (spi_controller_is_target(drv_data->controller)) {
1215                 chip->cr1 |= SSCR1_SCFR;
1216                 chip->cr1 |= SSCR1_SCLKDIR;
1217                 chip->cr1 |= SSCR1_SFRMDIR;
1218                 chip->cr1 |= SSCR1_SPH;
1219         }
1220
1221         if (is_lpss_ssp(drv_data)) {
1222                 chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1223                 chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) |
1224                                           SSITF_TxHiThresh(tx_hi_thres);
1225         }
1226
1227         if (is_mrfld_ssp(drv_data)) {
1228                 chip->lpss_rx_threshold = rx_thres;
1229                 chip->lpss_tx_threshold = tx_thres;
1230         }
1231
1232         switch (drv_data->ssp_type) {
1233         case QUARK_X1000_SSP:
1234                 chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1235                                    & QUARK_X1000_SSCR1_RFT)
1236                                    | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1237                                    & QUARK_X1000_SSCR1_TFT);
1238                 break;
1239         case CE4100_SSP:
1240                 chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
1241                         (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
1242                 break;
1243         default:
1244                 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1245                         (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1246                 break;
1247         }
1248
1249         chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1250         chip->cr1 |= ((spi->mode & SPI_CPHA) ? SSCR1_SPH : 0) |
1251                      ((spi->mode & SPI_CPOL) ? SSCR1_SPO : 0);
1252
1253         if (spi->mode & SPI_LOOP)
1254                 chip->cr1 |= SSCR1_LBM;
1255
1256         spi_set_ctldata(spi, chip);
1257
1258         return 0;
1259 }
1260
1261 static void cleanup(struct spi_device *spi)
1262 {
1263         struct chip_data *chip = spi_get_ctldata(spi);
1264
1265         kfree(chip);
1266 }
1267
1268 static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
1269 {
1270         return param == chan->device->dev;
1271 }
1272
1273 static int
1274 pxa2xx_spi_init_ssp(struct platform_device *pdev, struct ssp_device *ssp, enum pxa_ssp_type type)
1275 {
1276         struct device *dev = &pdev->dev;
1277         struct resource *res;
1278         int status;
1279         u64 uid;
1280
1281         ssp->mmio_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1282         if (IS_ERR(ssp->mmio_base))
1283                 return PTR_ERR(ssp->mmio_base);
1284
1285         ssp->phys_base = res->start;
1286
1287         ssp->clk = devm_clk_get(dev, NULL);
1288         if (IS_ERR(ssp->clk))
1289                 return PTR_ERR(ssp->clk);
1290
1291         ssp->irq = platform_get_irq(pdev, 0);
1292         if (ssp->irq < 0)
1293                 return ssp->irq;
1294
1295         ssp->type = type;
1296         ssp->dev = dev;
1297
1298         status = acpi_dev_uid_to_integer(ACPI_COMPANION(dev), &uid);
1299         if (status)
1300                 ssp->port_id = -1;
1301         else
1302                 ssp->port_id = uid;
1303
1304         return 0;
1305 }
1306
1307 static struct pxa2xx_spi_controller *
1308 pxa2xx_spi_init_pdata(struct platform_device *pdev)
1309 {
1310         struct pxa2xx_spi_controller *pdata;
1311         struct device *dev = &pdev->dev;
1312         struct device *parent = dev->parent;
1313         enum pxa_ssp_type type = SSP_UNDEFINED;
1314         struct ssp_device *ssp = NULL;
1315         const void *match;
1316         bool is_lpss_priv;
1317         u32 num_cs = 1;
1318         int status;
1319
1320         is_lpss_priv = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lpss_priv");
1321
1322         match = device_get_match_data(dev);
1323         if (match)
1324                 type = (uintptr_t)match;
1325         else if (is_lpss_priv) {
1326                 u32 value;
1327
1328                 status = device_property_read_u32(dev, "intel,spi-pxa2xx-type", &value);
1329                 if (status)
1330                         return ERR_PTR(status);
1331
1332                 type = (enum pxa_ssp_type)value;
1333         } else {
1334                 ssp = pxa_ssp_request(pdev->id, pdev->name);
1335                 if (ssp) {
1336                         type = ssp->type;
1337                         pxa_ssp_free(ssp);
1338                 }
1339         }
1340
1341         /* Validate the SSP type correctness */
1342         if (!(type > SSP_UNDEFINED && type < SSP_MAX))
1343                 return ERR_PTR(-EINVAL);
1344
1345         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1346         if (!pdata)
1347                 return ERR_PTR(-ENOMEM);
1348
1349         /* Platforms with iDMA 64-bit */
1350         if (is_lpss_priv) {
1351                 pdata->tx_param = parent;
1352                 pdata->rx_param = parent;
1353                 pdata->dma_filter = pxa2xx_spi_idma_filter;
1354         }
1355
1356         /* Read number of chip select pins, if provided */
1357         device_property_read_u32(dev, "num-cs", &num_cs);
1358
1359         pdata->num_chipselect = num_cs;
1360         pdata->is_target = device_property_read_bool(dev, "spi-slave");
1361         pdata->enable_dma = true;
1362         pdata->dma_burst_size = 1;
1363
1364         /* If SSP has been already enumerated, use it */
1365         if (ssp)
1366                 return pdata;
1367
1368         status = pxa2xx_spi_init_ssp(pdev, &pdata->ssp, type);
1369         if (status)
1370                 return ERR_PTR(status);
1371
1372         return pdata;
1373 }
1374
1375 static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
1376                                       unsigned int cs)
1377 {
1378         struct driver_data *drv_data = spi_controller_get_devdata(controller);
1379
1380         if (has_acpi_companion(drv_data->ssp->dev)) {
1381                 switch (drv_data->ssp_type) {
1382                 /*
1383                  * For Atoms the ACPI DeviceSelection used by the Windows
1384                  * driver starts from 1 instead of 0 so translate it here
1385                  * to match what Linux expects.
1386                  */
1387                 case LPSS_BYT_SSP:
1388                 case LPSS_BSW_SSP:
1389                         return cs - 1;
1390
1391                 default:
1392                         break;
1393                 }
1394         }
1395
1396         return cs;
1397 }
1398
1399 static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi)
1400 {
1401         return MAX_DMA_LEN;
1402 }
1403
1404 static int pxa2xx_spi_probe(struct platform_device *pdev)
1405 {
1406         struct device *dev = &pdev->dev;
1407         struct pxa2xx_spi_controller *platform_info;
1408         struct spi_controller *controller;
1409         struct driver_data *drv_data;
1410         struct ssp_device *ssp;
1411         const struct lpss_config *config;
1412         int status;
1413         u32 tmp;
1414
1415         platform_info = dev_get_platdata(dev);
1416         if (!platform_info) {
1417                 platform_info = pxa2xx_spi_init_pdata(pdev);
1418                 if (IS_ERR(platform_info))
1419                         return dev_err_probe(dev, PTR_ERR(platform_info), "missing platform data\n");
1420         }
1421         dev_dbg(dev, "DMA burst size set to %u\n", platform_info->dma_burst_size);
1422
1423         ssp = pxa_ssp_request(pdev->id, pdev->name);
1424         if (!ssp)
1425                 ssp = &platform_info->ssp;
1426
1427         if (!ssp->mmio_base)
1428                 return dev_err_probe(dev, -ENODEV, "failed to get SSP\n");
1429
1430         if (platform_info->is_target)
1431                 controller = devm_spi_alloc_target(dev, sizeof(*drv_data));
1432         else
1433                 controller = devm_spi_alloc_host(dev, sizeof(*drv_data));
1434
1435         if (!controller) {
1436                 status = dev_err_probe(dev, -ENOMEM, "cannot alloc spi_controller\n");
1437                 goto out_error_controller_alloc;
1438         }
1439         drv_data = spi_controller_get_devdata(controller);
1440         drv_data->controller = controller;
1441         drv_data->controller_info = platform_info;
1442         drv_data->ssp = ssp;
1443
1444         device_set_node(&controller->dev, dev_fwnode(dev));
1445
1446         /* The spi->mode bits understood by this driver: */
1447         controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1448
1449         controller->bus_num = ssp->port_id;
1450         controller->dma_alignment = DMA_ALIGNMENT;
1451         controller->cleanup = cleanup;
1452         controller->setup = setup;
1453         controller->set_cs = pxa2xx_spi_set_cs;
1454         controller->transfer_one = pxa2xx_spi_transfer_one;
1455         controller->target_abort = pxa2xx_spi_target_abort;
1456         controller->handle_err = pxa2xx_spi_handle_err;
1457         controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
1458         controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
1459         controller->auto_runtime_pm = true;
1460         controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
1461
1462         drv_data->ssp_type = ssp->type;
1463
1464         if (pxa25x_ssp_comp(drv_data)) {
1465                 switch (drv_data->ssp_type) {
1466                 case QUARK_X1000_SSP:
1467                         controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1468                         break;
1469                 default:
1470                         controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1471                         break;
1472                 }
1473
1474                 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1475                 drv_data->dma_cr1 = 0;
1476                 drv_data->clear_sr = SSSR_ROR;
1477                 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1478         } else {
1479                 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1480                 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
1481                 drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1482                 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1483                 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1484                                                 | SSSR_ROR | SSSR_TUR;
1485         }
1486
1487         status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1488                         drv_data);
1489         if (status < 0) {
1490                 dev_err_probe(dev, status, "cannot get IRQ %d\n", ssp->irq);
1491                 goto out_error_controller_alloc;
1492         }
1493
1494         /* Setup DMA if requested */
1495         if (platform_info->enable_dma) {
1496                 status = pxa2xx_spi_dma_setup(drv_data);
1497                 if (status) {
1498                         dev_warn(dev, "no DMA channels available, using PIO\n");
1499                         platform_info->enable_dma = false;
1500                 } else {
1501                         controller->can_dma = pxa2xx_spi_can_dma;
1502                         controller->max_dma_len = MAX_DMA_LEN;
1503                         controller->max_transfer_size =
1504                                 pxa2xx_spi_max_dma_transfer_size;
1505                 }
1506         }
1507
1508         /* Enable SOC clock */
1509         status = clk_prepare_enable(ssp->clk);
1510         if (status)
1511                 goto out_error_dma_irq_alloc;
1512
1513         controller->max_speed_hz = clk_get_rate(ssp->clk);
1514         /*
1515          * Set minimum speed for all other platforms than Intel Quark which is
1516          * able do under 1 Hz transfers.
1517          */
1518         if (!pxa25x_ssp_comp(drv_data))
1519                 controller->min_speed_hz =
1520                         DIV_ROUND_UP(controller->max_speed_hz, 4096);
1521         else if (!is_quark_x1000_ssp(drv_data))
1522                 controller->min_speed_hz =
1523                         DIV_ROUND_UP(controller->max_speed_hz, 512);
1524
1525         pxa_ssp_disable(ssp);
1526
1527         /* Load default SSP configuration */
1528         switch (drv_data->ssp_type) {
1529         case QUARK_X1000_SSP:
1530                 tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
1531                       QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1532                 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1533
1534                 /* Using the Motorola SPI protocol and use 8 bit frame */
1535                 tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
1536                 pxa2xx_spi_write(drv_data, SSCR0, tmp);
1537                 break;
1538         case CE4100_SSP:
1539                 tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
1540                       CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
1541                 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1542                 tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
1543                 pxa2xx_spi_write(drv_data, SSCR0, tmp);
1544                 break;
1545         default:
1546
1547                 if (spi_controller_is_target(controller)) {
1548                         tmp = SSCR1_SCFR |
1549                               SSCR1_SCLKDIR |
1550                               SSCR1_SFRMDIR |
1551                               SSCR1_RxTresh(2) |
1552                               SSCR1_TxTresh(1) |
1553                               SSCR1_SPH;
1554                 } else {
1555                         tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1556                               SSCR1_TxTresh(TX_THRESH_DFLT);
1557                 }
1558                 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1559                 tmp = SSCR0_Motorola | SSCR0_DataSize(8);
1560                 if (!spi_controller_is_target(controller))
1561                         tmp |= SSCR0_SCR(2);
1562                 pxa2xx_spi_write(drv_data, SSCR0, tmp);
1563                 break;
1564         }
1565
1566         if (!pxa25x_ssp_comp(drv_data))
1567                 pxa2xx_spi_write(drv_data, SSTO, 0);
1568
1569         if (!is_quark_x1000_ssp(drv_data))
1570                 pxa2xx_spi_write(drv_data, SSPSP, 0);
1571
1572         if (is_lpss_ssp(drv_data)) {
1573                 lpss_ssp_setup(drv_data);
1574                 config = lpss_get_config(drv_data);
1575                 if (config->reg_capabilities >= 0) {
1576                         tmp = __lpss_ssp_read_priv(drv_data,
1577                                                    config->reg_capabilities);
1578                         tmp &= LPSS_CAPS_CS_EN_MASK;
1579                         tmp >>= LPSS_CAPS_CS_EN_SHIFT;
1580                         platform_info->num_chipselect = ffz(tmp);
1581                 } else if (config->cs_num) {
1582                         platform_info->num_chipselect = config->cs_num;
1583                 }
1584         }
1585         controller->num_chipselect = platform_info->num_chipselect;
1586         controller->use_gpio_descriptors = true;
1587
1588         if (platform_info->is_target) {
1589                 drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
1590                                                 "ready", GPIOD_OUT_LOW);
1591                 if (IS_ERR(drv_data->gpiod_ready)) {
1592                         status = PTR_ERR(drv_data->gpiod_ready);
1593                         goto out_error_clock_enabled;
1594                 }
1595         }
1596
1597         pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1598         pm_runtime_use_autosuspend(&pdev->dev);
1599         pm_runtime_set_active(&pdev->dev);
1600         pm_runtime_enable(&pdev->dev);
1601
1602         /* Register with the SPI framework */
1603         platform_set_drvdata(pdev, drv_data);
1604         status = spi_register_controller(controller);
1605         if (status) {
1606                 dev_err_probe(dev, status, "problem registering SPI controller\n");
1607                 goto out_error_pm_runtime_enabled;
1608         }
1609
1610         return status;
1611
1612 out_error_pm_runtime_enabled:
1613         pm_runtime_disable(&pdev->dev);
1614
1615 out_error_clock_enabled:
1616         clk_disable_unprepare(ssp->clk);
1617
1618 out_error_dma_irq_alloc:
1619         pxa2xx_spi_dma_release(drv_data);
1620         free_irq(ssp->irq, drv_data);
1621
1622 out_error_controller_alloc:
1623         pxa_ssp_free(ssp);
1624         return status;
1625 }
1626
1627 static void pxa2xx_spi_remove(struct platform_device *pdev)
1628 {
1629         struct driver_data *drv_data = platform_get_drvdata(pdev);
1630         struct ssp_device *ssp = drv_data->ssp;
1631
1632         pm_runtime_get_sync(&pdev->dev);
1633
1634         spi_unregister_controller(drv_data->controller);
1635
1636         /* Disable the SSP at the peripheral and SOC level */
1637         pxa_ssp_disable(ssp);
1638         clk_disable_unprepare(ssp->clk);
1639
1640         /* Release DMA */
1641         if (drv_data->controller_info->enable_dma)
1642                 pxa2xx_spi_dma_release(drv_data);
1643
1644         pm_runtime_put_noidle(&pdev->dev);
1645         pm_runtime_disable(&pdev->dev);
1646
1647         /* Release IRQ */
1648         free_irq(ssp->irq, drv_data);
1649
1650         /* Release SSP */
1651         pxa_ssp_free(ssp);
1652 }
1653
1654 static int pxa2xx_spi_suspend(struct device *dev)
1655 {
1656         struct driver_data *drv_data = dev_get_drvdata(dev);
1657         struct ssp_device *ssp = drv_data->ssp;
1658         int status;
1659
1660         status = spi_controller_suspend(drv_data->controller);
1661         if (status)
1662                 return status;
1663
1664         pxa_ssp_disable(ssp);
1665
1666         if (!pm_runtime_suspended(dev))
1667                 clk_disable_unprepare(ssp->clk);
1668
1669         return 0;
1670 }
1671
1672 static int pxa2xx_spi_resume(struct device *dev)
1673 {
1674         struct driver_data *drv_data = dev_get_drvdata(dev);
1675         struct ssp_device *ssp = drv_data->ssp;
1676         int status;
1677
1678         /* Enable the SSP clock */
1679         if (!pm_runtime_suspended(dev)) {
1680                 status = clk_prepare_enable(ssp->clk);
1681                 if (status)
1682                         return status;
1683         }
1684
1685         /* Start the queue running */
1686         return spi_controller_resume(drv_data->controller);
1687 }
1688
1689 static int pxa2xx_spi_runtime_suspend(struct device *dev)
1690 {
1691         struct driver_data *drv_data = dev_get_drvdata(dev);
1692
1693         clk_disable_unprepare(drv_data->ssp->clk);
1694         return 0;
1695 }
1696
1697 static int pxa2xx_spi_runtime_resume(struct device *dev)
1698 {
1699         struct driver_data *drv_data = dev_get_drvdata(dev);
1700
1701         return clk_prepare_enable(drv_data->ssp->clk);
1702 }
1703
1704 static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
1705         SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
1706         RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend, pxa2xx_spi_runtime_resume, NULL)
1707 };
1708
1709 static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
1710         { "80860F0E", LPSS_BYT_SSP },
1711         { "8086228E", LPSS_BSW_SSP },
1712         { "INT33C0", LPSS_LPT_SSP },
1713         { "INT33C1", LPSS_LPT_SSP },
1714         { "INT3430", LPSS_LPT_SSP },
1715         { "INT3431", LPSS_LPT_SSP },
1716         {}
1717 };
1718 MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
1719
1720 static const struct of_device_id pxa2xx_spi_of_match[] = {
1721         { .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
1722         {}
1723 };
1724 MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
1725
1726 static struct platform_driver driver = {
1727         .driver = {
1728                 .name   = "pxa2xx-spi",
1729                 .pm     = pm_ptr(&pxa2xx_spi_pm_ops),
1730                 .acpi_match_table = pxa2xx_spi_acpi_match,
1731                 .of_match_table = pxa2xx_spi_of_match,
1732         },
1733         .probe = pxa2xx_spi_probe,
1734         .remove_new = pxa2xx_spi_remove,
1735 };
1736
1737 static int __init pxa2xx_spi_init(void)
1738 {
1739         return platform_driver_register(&driver);
1740 }
1741 subsys_initcall(pxa2xx_spi_init);
1742
1743 static void __exit pxa2xx_spi_exit(void)
1744 {
1745         platform_driver_unregister(&driver);
1746 }
1747 module_exit(pxa2xx_spi_exit);
1748
1749 MODULE_SOFTDEP("pre: dw_dmac");
This page took 0.135075 seconds and 4 git commands to generate.