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