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