0001
0002
0003
0004
0005
0006
0007 #include <linux/acpi.h>
0008 #include <linux/bitops.h>
0009 #include <linux/clk.h>
0010 #include <linux/delay.h>
0011 #include <linux/device.h>
0012 #include <linux/dmaengine.h>
0013 #include <linux/err.h>
0014 #include <linux/errno.h>
0015 #include <linux/gpio/consumer.h>
0016 #include <linux/init.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/ioport.h>
0019 #include <linux/kernel.h>
0020 #include <linux/module.h>
0021 #include <linux/mod_devicetable.h>
0022 #include <linux/of.h>
0023 #include <linux/pci.h>
0024 #include <linux/platform_device.h>
0025 #include <linux/pm_runtime.h>
0026 #include <linux/property.h>
0027 #include <linux/slab.h>
0028
0029 #include <linux/spi/pxa2xx_spi.h>
0030 #include <linux/spi/spi.h>
0031
0032 #include "spi-pxa2xx.h"
0033
0034 MODULE_AUTHOR("Stephen Street");
0035 MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
0036 MODULE_LICENSE("GPL");
0037 MODULE_ALIAS("platform:pxa2xx-spi");
0038
0039 #define TIMOUT_DFLT 1000
0040
0041
0042
0043
0044
0045
0046
0047
0048 #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
0049 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
0050 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
0051 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
0052 | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
0053 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
0054
0055 #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
0056 | QUARK_X1000_SSCR1_EFWR \
0057 | QUARK_X1000_SSCR1_RFT \
0058 | QUARK_X1000_SSCR1_TFT \
0059 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
0060
0061 #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
0062 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
0063 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
0064 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
0065 | CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
0066 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
0067
0068 #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
0069 #define LPSS_CS_CONTROL_SW_MODE BIT(0)
0070 #define LPSS_CS_CONTROL_CS_HIGH BIT(1)
0071 #define LPSS_CAPS_CS_EN_SHIFT 9
0072 #define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT)
0073
0074 #define LPSS_PRIV_CLOCK_GATE 0x38
0075 #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
0076 #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
0077
0078 struct lpss_config {
0079
0080 unsigned offset;
0081
0082 int reg_general;
0083 int reg_ssp;
0084 int reg_cs_ctrl;
0085 int reg_capabilities;
0086
0087 u32 rx_threshold;
0088 u32 tx_threshold_lo;
0089 u32 tx_threshold_hi;
0090
0091 unsigned cs_sel_shift;
0092 unsigned cs_sel_mask;
0093 unsigned cs_num;
0094
0095 unsigned cs_clk_stays_gated : 1;
0096 };
0097
0098
0099 static const struct lpss_config lpss_platforms[] = {
0100 {
0101 .offset = 0x800,
0102 .reg_general = 0x08,
0103 .reg_ssp = 0x0c,
0104 .reg_cs_ctrl = 0x18,
0105 .reg_capabilities = -1,
0106 .rx_threshold = 64,
0107 .tx_threshold_lo = 160,
0108 .tx_threshold_hi = 224,
0109 },
0110 {
0111 .offset = 0x400,
0112 .reg_general = 0x08,
0113 .reg_ssp = 0x0c,
0114 .reg_cs_ctrl = 0x18,
0115 .reg_capabilities = -1,
0116 .rx_threshold = 64,
0117 .tx_threshold_lo = 160,
0118 .tx_threshold_hi = 224,
0119 },
0120 {
0121 .offset = 0x400,
0122 .reg_general = 0x08,
0123 .reg_ssp = 0x0c,
0124 .reg_cs_ctrl = 0x18,
0125 .reg_capabilities = -1,
0126 .rx_threshold = 64,
0127 .tx_threshold_lo = 160,
0128 .tx_threshold_hi = 224,
0129 .cs_sel_shift = 2,
0130 .cs_sel_mask = 1 << 2,
0131 .cs_num = 2,
0132 },
0133 {
0134 .offset = 0x200,
0135 .reg_general = -1,
0136 .reg_ssp = 0x20,
0137 .reg_cs_ctrl = 0x24,
0138 .reg_capabilities = -1,
0139 .rx_threshold = 1,
0140 .tx_threshold_lo = 32,
0141 .tx_threshold_hi = 56,
0142 },
0143 {
0144 .offset = 0x200,
0145 .reg_general = -1,
0146 .reg_ssp = 0x20,
0147 .reg_cs_ctrl = 0x24,
0148 .reg_capabilities = 0xfc,
0149 .rx_threshold = 1,
0150 .tx_threshold_lo = 16,
0151 .tx_threshold_hi = 48,
0152 .cs_sel_shift = 8,
0153 .cs_sel_mask = 3 << 8,
0154 .cs_clk_stays_gated = true,
0155 },
0156 {
0157 .offset = 0x200,
0158 .reg_general = -1,
0159 .reg_ssp = 0x20,
0160 .reg_cs_ctrl = 0x24,
0161 .reg_capabilities = 0xfc,
0162 .rx_threshold = 1,
0163 .tx_threshold_lo = 32,
0164 .tx_threshold_hi = 56,
0165 .cs_sel_shift = 8,
0166 .cs_sel_mask = 3 << 8,
0167 .cs_clk_stays_gated = true,
0168 },
0169 };
0170
0171 static inline const struct lpss_config
0172 *lpss_get_config(const struct driver_data *drv_data)
0173 {
0174 return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
0175 }
0176
0177 static bool is_lpss_ssp(const struct driver_data *drv_data)
0178 {
0179 switch (drv_data->ssp_type) {
0180 case LPSS_LPT_SSP:
0181 case LPSS_BYT_SSP:
0182 case LPSS_BSW_SSP:
0183 case LPSS_SPT_SSP:
0184 case LPSS_BXT_SSP:
0185 case LPSS_CNL_SSP:
0186 return true;
0187 default:
0188 return false;
0189 }
0190 }
0191
0192 static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
0193 {
0194 return drv_data->ssp_type == QUARK_X1000_SSP;
0195 }
0196
0197 static bool is_mmp2_ssp(const struct driver_data *drv_data)
0198 {
0199 return drv_data->ssp_type == MMP2_SSP;
0200 }
0201
0202 static bool is_mrfld_ssp(const struct driver_data *drv_data)
0203 {
0204 return drv_data->ssp_type == MRFLD_SSP;
0205 }
0206
0207 static void pxa2xx_spi_update(const struct driver_data *drv_data, u32 reg, u32 mask, u32 value)
0208 {
0209 if ((pxa2xx_spi_read(drv_data, reg) & mask) != value)
0210 pxa2xx_spi_write(drv_data, reg, value & mask);
0211 }
0212
0213 static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
0214 {
0215 switch (drv_data->ssp_type) {
0216 case QUARK_X1000_SSP:
0217 return QUARK_X1000_SSCR1_CHANGE_MASK;
0218 case CE4100_SSP:
0219 return CE4100_SSCR1_CHANGE_MASK;
0220 default:
0221 return SSCR1_CHANGE_MASK;
0222 }
0223 }
0224
0225 static u32
0226 pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
0227 {
0228 switch (drv_data->ssp_type) {
0229 case QUARK_X1000_SSP:
0230 return RX_THRESH_QUARK_X1000_DFLT;
0231 case CE4100_SSP:
0232 return RX_THRESH_CE4100_DFLT;
0233 default:
0234 return RX_THRESH_DFLT;
0235 }
0236 }
0237
0238 static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
0239 {
0240 u32 mask;
0241
0242 switch (drv_data->ssp_type) {
0243 case QUARK_X1000_SSP:
0244 mask = QUARK_X1000_SSSR_TFL_MASK;
0245 break;
0246 case CE4100_SSP:
0247 mask = CE4100_SSSR_TFL_MASK;
0248 break;
0249 default:
0250 mask = SSSR_TFL_MASK;
0251 break;
0252 }
0253
0254 return read_SSSR_bits(drv_data, mask) == mask;
0255 }
0256
0257 static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
0258 u32 *sccr1_reg)
0259 {
0260 u32 mask;
0261
0262 switch (drv_data->ssp_type) {
0263 case QUARK_X1000_SSP:
0264 mask = QUARK_X1000_SSCR1_RFT;
0265 break;
0266 case CE4100_SSP:
0267 mask = CE4100_SSCR1_RFT;
0268 break;
0269 default:
0270 mask = SSCR1_RFT;
0271 break;
0272 }
0273 *sccr1_reg &= ~mask;
0274 }
0275
0276 static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
0277 u32 *sccr1_reg, u32 threshold)
0278 {
0279 switch (drv_data->ssp_type) {
0280 case QUARK_X1000_SSP:
0281 *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
0282 break;
0283 case CE4100_SSP:
0284 *sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
0285 break;
0286 default:
0287 *sccr1_reg |= SSCR1_RxTresh(threshold);
0288 break;
0289 }
0290 }
0291
0292 static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
0293 u32 clk_div, u8 bits)
0294 {
0295 switch (drv_data->ssp_type) {
0296 case QUARK_X1000_SSP:
0297 return clk_div
0298 | QUARK_X1000_SSCR0_Motorola
0299 | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits);
0300 default:
0301 return clk_div
0302 | SSCR0_Motorola
0303 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
0304 | (bits > 16 ? SSCR0_EDSS : 0);
0305 }
0306 }
0307
0308
0309
0310
0311
0312 static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
0313 {
0314 WARN_ON(!drv_data->lpss_base);
0315 return readl(drv_data->lpss_base + offset);
0316 }
0317
0318 static void __lpss_ssp_write_priv(struct driver_data *drv_data,
0319 unsigned offset, u32 value)
0320 {
0321 WARN_ON(!drv_data->lpss_base);
0322 writel(value, drv_data->lpss_base + offset);
0323 }
0324
0325
0326
0327
0328
0329
0330
0331
0332 static void lpss_ssp_setup(struct driver_data *drv_data)
0333 {
0334 const struct lpss_config *config;
0335 u32 value;
0336
0337 config = lpss_get_config(drv_data);
0338 drv_data->lpss_base = drv_data->ssp->mmio_base + config->offset;
0339
0340
0341 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
0342 value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
0343 value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
0344 __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
0345
0346
0347 if (drv_data->controller_info->enable_dma) {
0348 __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
0349
0350 if (config->reg_general >= 0) {
0351 value = __lpss_ssp_read_priv(drv_data,
0352 config->reg_general);
0353 value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
0354 __lpss_ssp_write_priv(drv_data,
0355 config->reg_general, value);
0356 }
0357 }
0358 }
0359
0360 static void lpss_ssp_select_cs(struct spi_device *spi,
0361 const struct lpss_config *config)
0362 {
0363 struct driver_data *drv_data =
0364 spi_controller_get_devdata(spi->controller);
0365 u32 value, cs;
0366
0367 if (!config->cs_sel_mask)
0368 return;
0369
0370 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
0371
0372 cs = spi->chip_select;
0373 cs <<= config->cs_sel_shift;
0374 if (cs != (value & config->cs_sel_mask)) {
0375
0376
0377
0378
0379
0380
0381
0382 value &= ~config->cs_sel_mask;
0383 value |= cs;
0384 __lpss_ssp_write_priv(drv_data,
0385 config->reg_cs_ctrl, value);
0386 ndelay(1000000000 /
0387 (drv_data->controller->max_speed_hz / 2));
0388 }
0389 }
0390
0391 static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
0392 {
0393 struct driver_data *drv_data =
0394 spi_controller_get_devdata(spi->controller);
0395 const struct lpss_config *config;
0396 u32 value;
0397
0398 config = lpss_get_config(drv_data);
0399
0400 if (enable)
0401 lpss_ssp_select_cs(spi, config);
0402
0403 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
0404 if (enable)
0405 value &= ~LPSS_CS_CONTROL_CS_HIGH;
0406 else
0407 value |= LPSS_CS_CONTROL_CS_HIGH;
0408 __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
0409 if (config->cs_clk_stays_gated) {
0410 u32 clkgate;
0411
0412
0413
0414
0415
0416
0417
0418 clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
0419 value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
0420 LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
0421
0422 __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
0423 __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
0424 }
0425 }
0426
0427 static void cs_assert(struct spi_device *spi)
0428 {
0429 struct driver_data *drv_data =
0430 spi_controller_get_devdata(spi->controller);
0431
0432 if (drv_data->ssp_type == CE4100_SSP) {
0433 pxa2xx_spi_write(drv_data, SSSR, spi->chip_select);
0434 return;
0435 }
0436
0437 if (is_lpss_ssp(drv_data))
0438 lpss_ssp_cs_control(spi, true);
0439 }
0440
0441 static void cs_deassert(struct spi_device *spi)
0442 {
0443 struct driver_data *drv_data =
0444 spi_controller_get_devdata(spi->controller);
0445 unsigned long timeout;
0446
0447 if (drv_data->ssp_type == CE4100_SSP)
0448 return;
0449
0450
0451 timeout = jiffies + msecs_to_jiffies(10);
0452 while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
0453 !time_after(jiffies, timeout))
0454 cpu_relax();
0455
0456 if (is_lpss_ssp(drv_data))
0457 lpss_ssp_cs_control(spi, false);
0458 }
0459
0460 static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
0461 {
0462 if (level)
0463 cs_deassert(spi);
0464 else
0465 cs_assert(spi);
0466 }
0467
0468 int pxa2xx_spi_flush(struct driver_data *drv_data)
0469 {
0470 unsigned long limit = loops_per_jiffy << 1;
0471
0472 do {
0473 while (read_SSSR_bits(drv_data, SSSR_RNE))
0474 pxa2xx_spi_read(drv_data, SSDR);
0475 } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
0476 write_SSSR_CS(drv_data, SSSR_ROR);
0477
0478 return limit;
0479 }
0480
0481 static void pxa2xx_spi_off(struct driver_data *drv_data)
0482 {
0483
0484 if (is_mmp2_ssp(drv_data))
0485 return;
0486
0487 pxa_ssp_disable(drv_data->ssp);
0488 }
0489
0490 static int null_writer(struct driver_data *drv_data)
0491 {
0492 u8 n_bytes = drv_data->n_bytes;
0493
0494 if (pxa2xx_spi_txfifo_full(drv_data)
0495 || (drv_data->tx == drv_data->tx_end))
0496 return 0;
0497
0498 pxa2xx_spi_write(drv_data, SSDR, 0);
0499 drv_data->tx += n_bytes;
0500
0501 return 1;
0502 }
0503
0504 static int null_reader(struct driver_data *drv_data)
0505 {
0506 u8 n_bytes = drv_data->n_bytes;
0507
0508 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
0509 pxa2xx_spi_read(drv_data, SSDR);
0510 drv_data->rx += n_bytes;
0511 }
0512
0513 return drv_data->rx == drv_data->rx_end;
0514 }
0515
0516 static int u8_writer(struct driver_data *drv_data)
0517 {
0518 if (pxa2xx_spi_txfifo_full(drv_data)
0519 || (drv_data->tx == drv_data->tx_end))
0520 return 0;
0521
0522 pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
0523 ++drv_data->tx;
0524
0525 return 1;
0526 }
0527
0528 static int u8_reader(struct driver_data *drv_data)
0529 {
0530 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
0531 *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
0532 ++drv_data->rx;
0533 }
0534
0535 return drv_data->rx == drv_data->rx_end;
0536 }
0537
0538 static int u16_writer(struct driver_data *drv_data)
0539 {
0540 if (pxa2xx_spi_txfifo_full(drv_data)
0541 || (drv_data->tx == drv_data->tx_end))
0542 return 0;
0543
0544 pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
0545 drv_data->tx += 2;
0546
0547 return 1;
0548 }
0549
0550 static int u16_reader(struct driver_data *drv_data)
0551 {
0552 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
0553 *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
0554 drv_data->rx += 2;
0555 }
0556
0557 return drv_data->rx == drv_data->rx_end;
0558 }
0559
0560 static int u32_writer(struct driver_data *drv_data)
0561 {
0562 if (pxa2xx_spi_txfifo_full(drv_data)
0563 || (drv_data->tx == drv_data->tx_end))
0564 return 0;
0565
0566 pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
0567 drv_data->tx += 4;
0568
0569 return 1;
0570 }
0571
0572 static int u32_reader(struct driver_data *drv_data)
0573 {
0574 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
0575 *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
0576 drv_data->rx += 4;
0577 }
0578
0579 return drv_data->rx == drv_data->rx_end;
0580 }
0581
0582 static void reset_sccr1(struct driver_data *drv_data)
0583 {
0584 u32 mask = drv_data->int_cr1 | drv_data->dma_cr1, threshold;
0585 struct chip_data *chip;
0586
0587 if (drv_data->controller->cur_msg) {
0588 chip = spi_get_ctldata(drv_data->controller->cur_msg->spi);
0589 threshold = chip->threshold;
0590 } else {
0591 threshold = 0;
0592 }
0593
0594 switch (drv_data->ssp_type) {
0595 case QUARK_X1000_SSP:
0596 mask |= QUARK_X1000_SSCR1_RFT;
0597 break;
0598 case CE4100_SSP:
0599 mask |= CE4100_SSCR1_RFT;
0600 break;
0601 default:
0602 mask |= SSCR1_RFT;
0603 break;
0604 }
0605
0606 pxa2xx_spi_update(drv_data, SSCR1, mask, threshold);
0607 }
0608
0609 static void int_stop_and_reset(struct driver_data *drv_data)
0610 {
0611
0612 write_SSSR_CS(drv_data, drv_data->clear_sr);
0613 reset_sccr1(drv_data);
0614 if (pxa25x_ssp_comp(drv_data))
0615 return;
0616
0617 pxa2xx_spi_write(drv_data, SSTO, 0);
0618 }
0619
0620 static void int_error_stop(struct driver_data *drv_data, const char *msg, int err)
0621 {
0622 int_stop_and_reset(drv_data);
0623 pxa2xx_spi_flush(drv_data);
0624 pxa2xx_spi_off(drv_data);
0625
0626 dev_err(drv_data->ssp->dev, "%s\n", msg);
0627
0628 drv_data->controller->cur_msg->status = err;
0629 spi_finalize_current_transfer(drv_data->controller);
0630 }
0631
0632 static void int_transfer_complete(struct driver_data *drv_data)
0633 {
0634 int_stop_and_reset(drv_data);
0635
0636 spi_finalize_current_transfer(drv_data->controller);
0637 }
0638
0639 static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
0640 {
0641 u32 irq_status;
0642
0643 irq_status = read_SSSR_bits(drv_data, drv_data->mask_sr);
0644 if (!(pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE))
0645 irq_status &= ~SSSR_TFS;
0646
0647 if (irq_status & SSSR_ROR) {
0648 int_error_stop(drv_data, "interrupt_transfer: FIFO overrun", -EIO);
0649 return IRQ_HANDLED;
0650 }
0651
0652 if (irq_status & SSSR_TUR) {
0653 int_error_stop(drv_data, "interrupt_transfer: FIFO underrun", -EIO);
0654 return IRQ_HANDLED;
0655 }
0656
0657 if (irq_status & SSSR_TINT) {
0658 pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
0659 if (drv_data->read(drv_data)) {
0660 int_transfer_complete(drv_data);
0661 return IRQ_HANDLED;
0662 }
0663 }
0664
0665
0666 do {
0667 if (drv_data->read(drv_data)) {
0668 int_transfer_complete(drv_data);
0669 return IRQ_HANDLED;
0670 }
0671 } while (drv_data->write(drv_data));
0672
0673 if (drv_data->read(drv_data)) {
0674 int_transfer_complete(drv_data);
0675 return IRQ_HANDLED;
0676 }
0677
0678 if (drv_data->tx == drv_data->tx_end) {
0679 u32 bytes_left;
0680 u32 sccr1_reg;
0681
0682 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
0683 sccr1_reg &= ~SSCR1_TIE;
0684
0685
0686
0687
0688
0689 if (pxa25x_ssp_comp(drv_data)) {
0690 u32 rx_thre;
0691
0692 pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
0693
0694 bytes_left = drv_data->rx_end - drv_data->rx;
0695 switch (drv_data->n_bytes) {
0696 case 4:
0697 bytes_left >>= 2;
0698 break;
0699 case 2:
0700 bytes_left >>= 1;
0701 break;
0702 }
0703
0704 rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
0705 if (rx_thre > bytes_left)
0706 rx_thre = bytes_left;
0707
0708 pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
0709 }
0710 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
0711 }
0712
0713
0714 return IRQ_HANDLED;
0715 }
0716
0717 static void handle_bad_msg(struct driver_data *drv_data)
0718 {
0719 int_stop_and_reset(drv_data);
0720 pxa2xx_spi_off(drv_data);
0721
0722 dev_err(drv_data->ssp->dev, "bad message state in interrupt handler\n");
0723 }
0724
0725 static irqreturn_t ssp_int(int irq, void *dev_id)
0726 {
0727 struct driver_data *drv_data = dev_id;
0728 u32 sccr1_reg;
0729 u32 mask = drv_data->mask_sr;
0730 u32 status;
0731
0732
0733
0734
0735
0736
0737
0738 if (pm_runtime_suspended(drv_data->ssp->dev))
0739 return IRQ_NONE;
0740
0741
0742
0743
0744
0745
0746
0747 status = pxa2xx_spi_read(drv_data, SSSR);
0748 if (status == ~0)
0749 return IRQ_NONE;
0750
0751 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
0752
0753
0754 if (!(sccr1_reg & SSCR1_TIE))
0755 mask &= ~SSSR_TFS;
0756
0757
0758 if (!(sccr1_reg & SSCR1_TINTE))
0759 mask &= ~SSSR_TINT;
0760
0761 if (!(status & mask))
0762 return IRQ_NONE;
0763
0764 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
0765 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
0766
0767 if (!drv_data->controller->cur_msg) {
0768 handle_bad_msg(drv_data);
0769
0770 return IRQ_HANDLED;
0771 }
0772
0773 return drv_data->transfer_handler(drv_data);
0774 }
0775
0776
0777
0778
0779
0780
0781
0782
0783
0784
0785
0786
0787
0788
0789
0790
0791
0792
0793
0794
0795
0796
0797
0798
0799
0800
0801
0802
0803
0804
0805
0806
0807
0808 static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
0809 {
0810 unsigned long xtal = 200000000;
0811 unsigned long fref = xtal / 2;
0812
0813
0814 unsigned long fref1 = fref / 2;
0815 unsigned long fref2 = fref * 2 / 5;
0816 unsigned long scale;
0817 unsigned long q, q1, q2;
0818 long r, r1, r2;
0819 u32 mul;
0820
0821
0822
0823
0824 mul = (1 << 24) >> 1;
0825
0826
0827 q1 = DIV_ROUND_UP(fref1, rate);
0828
0829
0830 if (q1 > 256) {
0831
0832 scale = fls_long(q1 - 1);
0833 if (scale > 9) {
0834 q1 >>= scale - 9;
0835 mul >>= scale - 9;
0836 }
0837
0838
0839 q1 += q1 & 1;
0840 }
0841
0842
0843 scale = __ffs(q1);
0844 q1 >>= scale;
0845 mul >>= scale;
0846
0847
0848 r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
0849
0850
0851
0852 q2 = DIV_ROUND_UP(fref2, rate);
0853 r2 = abs(fref2 / q2 - rate);
0854
0855
0856
0857
0858
0859
0860 if (r2 >= r1 || q2 > 256) {
0861
0862 r = r1;
0863 q = q1;
0864 } else {
0865
0866 r = r2;
0867 q = q2;
0868 mul = (1 << 24) * 2 / 5;
0869 }
0870
0871
0872 if (fref / rate >= 80) {
0873 u64 fssp;
0874 u32 m;
0875
0876
0877 q1 = DIV_ROUND_UP(fref, rate);
0878 m = (1 << 24) / q1;
0879
0880
0881 fssp = (u64)fref * m;
0882 do_div(fssp, 1 << 24);
0883 r1 = abs(fssp - rate);
0884
0885
0886 if (r1 < r) {
0887
0888 q = 1;
0889 mul = m;
0890 }
0891 }
0892
0893 *dds = mul;
0894 return q - 1;
0895 }
0896
0897 static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
0898 {
0899 unsigned long ssp_clk = drv_data->controller->max_speed_hz;
0900 const struct ssp_device *ssp = drv_data->ssp;
0901
0902 rate = min_t(int, ssp_clk, rate);
0903
0904
0905
0906
0907
0908 if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
0909 return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
0910 else
0911 return (DIV_ROUND_UP(ssp_clk, rate) - 1) & 0xfff;
0912 }
0913
0914 static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
0915 int rate)
0916 {
0917 struct chip_data *chip =
0918 spi_get_ctldata(drv_data->controller->cur_msg->spi);
0919 unsigned int clk_div;
0920
0921 switch (drv_data->ssp_type) {
0922 case QUARK_X1000_SSP:
0923 clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
0924 break;
0925 default:
0926 clk_div = ssp_get_clk_div(drv_data, rate);
0927 break;
0928 }
0929 return clk_div << 8;
0930 }
0931
0932 static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
0933 struct spi_device *spi,
0934 struct spi_transfer *xfer)
0935 {
0936 struct chip_data *chip = spi_get_ctldata(spi);
0937
0938 return chip->enable_dma &&
0939 xfer->len <= MAX_DMA_LEN &&
0940 xfer->len >= chip->dma_burst_size;
0941 }
0942
0943 static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
0944 struct spi_device *spi,
0945 struct spi_transfer *transfer)
0946 {
0947 struct driver_data *drv_data = spi_controller_get_devdata(controller);
0948 struct spi_message *message = controller->cur_msg;
0949 struct chip_data *chip = spi_get_ctldata(spi);
0950 u32 dma_thresh = chip->dma_threshold;
0951 u32 dma_burst = chip->dma_burst_size;
0952 u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
0953 u32 clk_div;
0954 u8 bits;
0955 u32 speed;
0956 u32 cr0;
0957 u32 cr1;
0958 int err;
0959 int dma_mapped;
0960
0961
0962 if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
0963
0964
0965 if (message->is_dma_mapped
0966 || transfer->rx_dma || transfer->tx_dma) {
0967 dev_err(&spi->dev,
0968 "Mapped transfer length of %u is greater than %d\n",
0969 transfer->len, MAX_DMA_LEN);
0970 return -EINVAL;
0971 }
0972
0973
0974 dev_warn_ratelimited(&spi->dev,
0975 "DMA disabled for transfer length %u greater than %d\n",
0976 transfer->len, MAX_DMA_LEN);
0977 }
0978
0979
0980 if (pxa2xx_spi_flush(drv_data) == 0) {
0981 dev_err(&spi->dev, "Flush failed\n");
0982 return -EIO;
0983 }
0984 drv_data->tx = (void *)transfer->tx_buf;
0985 drv_data->tx_end = drv_data->tx + transfer->len;
0986 drv_data->rx = transfer->rx_buf;
0987 drv_data->rx_end = drv_data->rx + transfer->len;
0988
0989
0990 bits = transfer->bits_per_word;
0991 speed = transfer->speed_hz;
0992
0993 clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
0994
0995 if (bits <= 8) {
0996 drv_data->n_bytes = 1;
0997 drv_data->read = drv_data->rx ? u8_reader : null_reader;
0998 drv_data->write = drv_data->tx ? u8_writer : null_writer;
0999 } else if (bits <= 16) {
1000 drv_data->n_bytes = 2;
1001 drv_data->read = drv_data->rx ? u16_reader : null_reader;
1002 drv_data->write = drv_data->tx ? u16_writer : null_writer;
1003 } else if (bits <= 32) {
1004 drv_data->n_bytes = 4;
1005 drv_data->read = drv_data->rx ? u32_reader : null_reader;
1006 drv_data->write = drv_data->tx ? u32_writer : null_writer;
1007 }
1008
1009
1010
1011
1012 if (chip->enable_dma) {
1013 if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
1014 spi,
1015 bits, &dma_burst,
1016 &dma_thresh))
1017 dev_warn_ratelimited(&spi->dev,
1018 "DMA burst size reduced to match bits_per_word\n");
1019 }
1020
1021 dma_mapped = controller->can_dma &&
1022 controller->can_dma(controller, spi, transfer) &&
1023 controller->cur_msg_mapped;
1024 if (dma_mapped) {
1025
1026
1027 drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
1028
1029 err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1030 if (err)
1031 return err;
1032
1033
1034 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1035 pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1036
1037 pxa2xx_spi_dma_start(drv_data);
1038 } else {
1039
1040 drv_data->transfer_handler = interrupt_transfer;
1041
1042
1043 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1044 write_SSSR_CS(drv_data, drv_data->clear_sr);
1045 }
1046
1047
1048 cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1049 if (!pxa25x_ssp_comp(drv_data))
1050 dev_dbg(&spi->dev, "%u Hz actual, %s\n",
1051 controller->max_speed_hz
1052 / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
1053 dma_mapped ? "DMA" : "PIO");
1054 else
1055 dev_dbg(&spi->dev, "%u Hz actual, %s\n",
1056 controller->max_speed_hz / 2
1057 / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1058 dma_mapped ? "DMA" : "PIO");
1059
1060 if (is_lpss_ssp(drv_data)) {
1061 pxa2xx_spi_update(drv_data, SSIRF, GENMASK(7, 0), chip->lpss_rx_threshold);
1062 pxa2xx_spi_update(drv_data, SSITF, GENMASK(15, 0), chip->lpss_tx_threshold);
1063 }
1064
1065 if (is_mrfld_ssp(drv_data)) {
1066 u32 mask = SFIFOTT_RFT | SFIFOTT_TFT;
1067 u32 thresh = 0;
1068
1069 thresh |= SFIFOTT_RxThresh(chip->lpss_rx_threshold);
1070 thresh |= SFIFOTT_TxThresh(chip->lpss_tx_threshold);
1071
1072 pxa2xx_spi_update(drv_data, SFIFOTT, mask, thresh);
1073 }
1074
1075 if (is_quark_x1000_ssp(drv_data))
1076 pxa2xx_spi_update(drv_data, DDS_RATE, GENMASK(23, 0), chip->dds_rate);
1077
1078
1079 if (!is_mmp2_ssp(drv_data))
1080 pxa_ssp_disable(drv_data->ssp);
1081
1082 if (!pxa25x_ssp_comp(drv_data))
1083 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1084
1085
1086 pxa2xx_spi_update(drv_data, SSCR1, change_mask, cr1);
1087
1088
1089 pxa2xx_spi_update(drv_data, SSCR0, GENMASK(31, 0), cr0);
1090
1091
1092 pxa_ssp_enable(drv_data->ssp);
1093
1094 if (is_mmp2_ssp(drv_data)) {
1095 u8 tx_level = read_SSSR_bits(drv_data, SSSR_TFL_MASK) >> 8;
1096
1097 if (tx_level) {
1098
1099 dev_warn(&spi->dev, "%u bytes of garbage in Tx FIFO!\n", tx_level);
1100 if (tx_level > transfer->len)
1101 tx_level = transfer->len;
1102 drv_data->tx += tx_level;
1103 }
1104 }
1105
1106 if (spi_controller_is_slave(controller)) {
1107 while (drv_data->write(drv_data))
1108 ;
1109 if (drv_data->gpiod_ready) {
1110 gpiod_set_value(drv_data->gpiod_ready, 1);
1111 udelay(1);
1112 gpiod_set_value(drv_data->gpiod_ready, 0);
1113 }
1114 }
1115
1116
1117
1118
1119
1120 pxa2xx_spi_write(drv_data, SSCR1, cr1);
1121
1122 return 1;
1123 }
1124
1125 static int pxa2xx_spi_slave_abort(struct spi_controller *controller)
1126 {
1127 struct driver_data *drv_data = spi_controller_get_devdata(controller);
1128
1129 int_error_stop(drv_data, "transfer aborted", -EINTR);
1130
1131 return 0;
1132 }
1133
1134 static void pxa2xx_spi_handle_err(struct spi_controller *controller,
1135 struct spi_message *msg)
1136 {
1137 struct driver_data *drv_data = spi_controller_get_devdata(controller);
1138
1139 int_stop_and_reset(drv_data);
1140
1141
1142 pxa2xx_spi_off(drv_data);
1143
1144
1145
1146
1147
1148
1149
1150
1151 if (atomic_read(&drv_data->dma_running))
1152 pxa2xx_spi_dma_stop(drv_data);
1153 }
1154
1155 static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
1156 {
1157 struct driver_data *drv_data = spi_controller_get_devdata(controller);
1158
1159
1160 pxa2xx_spi_off(drv_data);
1161
1162 return 0;
1163 }
1164
1165 static int setup(struct spi_device *spi)
1166 {
1167 struct pxa2xx_spi_chip *chip_info;
1168 struct chip_data *chip;
1169 const struct lpss_config *config;
1170 struct driver_data *drv_data =
1171 spi_controller_get_devdata(spi->controller);
1172 uint tx_thres, tx_hi_thres, rx_thres;
1173
1174 switch (drv_data->ssp_type) {
1175 case QUARK_X1000_SSP:
1176 tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1177 tx_hi_thres = 0;
1178 rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1179 break;
1180 case MRFLD_SSP:
1181 tx_thres = TX_THRESH_MRFLD_DFLT;
1182 tx_hi_thres = 0;
1183 rx_thres = RX_THRESH_MRFLD_DFLT;
1184 break;
1185 case CE4100_SSP:
1186 tx_thres = TX_THRESH_CE4100_DFLT;
1187 tx_hi_thres = 0;
1188 rx_thres = RX_THRESH_CE4100_DFLT;
1189 break;
1190 case LPSS_LPT_SSP:
1191 case LPSS_BYT_SSP:
1192 case LPSS_BSW_SSP:
1193 case LPSS_SPT_SSP:
1194 case LPSS_BXT_SSP:
1195 case LPSS_CNL_SSP:
1196 config = lpss_get_config(drv_data);
1197 tx_thres = config->tx_threshold_lo;
1198 tx_hi_thres = config->tx_threshold_hi;
1199 rx_thres = config->rx_threshold;
1200 break;
1201 default:
1202 tx_hi_thres = 0;
1203 if (spi_controller_is_slave(drv_data->controller)) {
1204 tx_thres = 1;
1205 rx_thres = 2;
1206 } else {
1207 tx_thres = TX_THRESH_DFLT;
1208 rx_thres = RX_THRESH_DFLT;
1209 }
1210 break;
1211 }
1212
1213
1214 chip = spi_get_ctldata(spi);
1215 if (!chip) {
1216 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1217 if (!chip)
1218 return -ENOMEM;
1219
1220 if (drv_data->ssp_type == CE4100_SSP) {
1221 if (spi->chip_select > 4) {
1222 dev_err(&spi->dev,
1223 "failed setup: cs number must not be > 4.\n");
1224 kfree(chip);
1225 return -EINVAL;
1226 }
1227 }
1228 chip->enable_dma = drv_data->controller_info->enable_dma;
1229 chip->timeout = TIMOUT_DFLT;
1230 }
1231
1232
1233
1234
1235
1236 chip_info = spi->controller_data;
1237
1238
1239 if (chip_info) {
1240 if (chip_info->timeout)
1241 chip->timeout = chip_info->timeout;
1242 if (chip_info->tx_threshold)
1243 tx_thres = chip_info->tx_threshold;
1244 if (chip_info->tx_hi_threshold)
1245 tx_hi_thres = chip_info->tx_hi_threshold;
1246 if (chip_info->rx_threshold)
1247 rx_thres = chip_info->rx_threshold;
1248 chip->dma_threshold = 0;
1249 }
1250
1251 chip->cr1 = 0;
1252 if (spi_controller_is_slave(drv_data->controller)) {
1253 chip->cr1 |= SSCR1_SCFR;
1254 chip->cr1 |= SSCR1_SCLKDIR;
1255 chip->cr1 |= SSCR1_SFRMDIR;
1256 chip->cr1 |= SSCR1_SPH;
1257 }
1258
1259 if (is_lpss_ssp(drv_data)) {
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
1265 if (is_mrfld_ssp(drv_data)) {
1266 chip->lpss_rx_threshold = rx_thres;
1267 chip->lpss_tx_threshold = tx_thres;
1268 }
1269
1270
1271
1272
1273
1274
1275 if (chip->enable_dma) {
1276
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 dev_dbg(&spi->dev,
1285 "in setup: DMA burst size set to %u\n",
1286 chip->dma_burst_size);
1287 }
1288
1289 switch (drv_data->ssp_type) {
1290 case QUARK_X1000_SSP:
1291 chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1292 & QUARK_X1000_SSCR1_RFT)
1293 | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1294 & QUARK_X1000_SSCR1_TFT);
1295 break;
1296 case CE4100_SSP:
1297 chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
1298 (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
1299 break;
1300 default:
1301 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1302 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1303 break;
1304 }
1305
1306 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1307 chip->cr1 |= ((spi->mode & SPI_CPHA) ? SSCR1_SPH : 0) |
1308 ((spi->mode & SPI_CPOL) ? SSCR1_SPO : 0);
1309
1310 if (spi->mode & SPI_LOOP)
1311 chip->cr1 |= SSCR1_LBM;
1312
1313 spi_set_ctldata(spi, chip);
1314
1315 return 0;
1316 }
1317
1318 static void cleanup(struct spi_device *spi)
1319 {
1320 struct chip_data *chip = spi_get_ctldata(spi);
1321
1322 kfree(chip);
1323 }
1324
1325 #ifdef CONFIG_ACPI
1326 static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
1327 { "INT33C0", LPSS_LPT_SSP },
1328 { "INT33C1", LPSS_LPT_SSP },
1329 { "INT3430", LPSS_LPT_SSP },
1330 { "INT3431", LPSS_LPT_SSP },
1331 { "80860F0E", LPSS_BYT_SSP },
1332 { "8086228E", LPSS_BSW_SSP },
1333 { },
1334 };
1335 MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
1336 #endif
1337
1338
1339
1340
1341
1342
1343 static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
1344
1345 { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
1346 { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
1347
1348 { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
1349 { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
1350
1351 { PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
1352 { PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
1353
1354 { PCI_VDEVICE(INTEL, 0xa3a9), LPSS_SPT_SSP },
1355 { PCI_VDEVICE(INTEL, 0xa3aa), LPSS_SPT_SSP },
1356
1357 { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1358 { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1359 { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
1360
1361 { PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1362 { PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1363 { PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
1364
1365 { PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
1366 { PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
1367 { PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
1368
1369 { PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
1370 { PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
1371 { PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
1372
1373 { PCI_VDEVICE(INTEL, 0x4b2a), LPSS_BXT_SSP },
1374 { PCI_VDEVICE(INTEL, 0x4b2b), LPSS_BXT_SSP },
1375 { PCI_VDEVICE(INTEL, 0x4b37), LPSS_BXT_SSP },
1376
1377 { PCI_VDEVICE(INTEL, 0x4daa), LPSS_CNL_SSP },
1378 { PCI_VDEVICE(INTEL, 0x4dab), LPSS_CNL_SSP },
1379 { PCI_VDEVICE(INTEL, 0x4dfb), LPSS_CNL_SSP },
1380
1381 { PCI_VDEVICE(INTEL, 0x43aa), LPSS_CNL_SSP },
1382 { PCI_VDEVICE(INTEL, 0x43ab), LPSS_CNL_SSP },
1383 { PCI_VDEVICE(INTEL, 0x43fb), LPSS_CNL_SSP },
1384 { PCI_VDEVICE(INTEL, 0x43fd), LPSS_CNL_SSP },
1385
1386 { PCI_VDEVICE(INTEL, 0x51aa), LPSS_CNL_SSP },
1387 { PCI_VDEVICE(INTEL, 0x51ab), LPSS_CNL_SSP },
1388 { PCI_VDEVICE(INTEL, 0x51fb), LPSS_CNL_SSP },
1389
1390 { PCI_VDEVICE(INTEL, 0x54aa), LPSS_CNL_SSP },
1391 { PCI_VDEVICE(INTEL, 0x54ab), LPSS_CNL_SSP },
1392 { PCI_VDEVICE(INTEL, 0x54fb), LPSS_CNL_SSP },
1393
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 { PCI_VDEVICE(INTEL, 0x7a2a), LPSS_CNL_SSP },
1399 { PCI_VDEVICE(INTEL, 0x7a2b), LPSS_CNL_SSP },
1400 { PCI_VDEVICE(INTEL, 0x7a79), LPSS_CNL_SSP },
1401 { PCI_VDEVICE(INTEL, 0x7a7b), LPSS_CNL_SSP },
1402
1403 { PCI_VDEVICE(INTEL, 0x7aaa), LPSS_CNL_SSP },
1404 { PCI_VDEVICE(INTEL, 0x7aab), LPSS_CNL_SSP },
1405 { PCI_VDEVICE(INTEL, 0x7af9), LPSS_CNL_SSP },
1406 { PCI_VDEVICE(INTEL, 0x7afb), LPSS_CNL_SSP },
1407
1408 { PCI_VDEVICE(INTEL, 0x7e27), LPSS_CNL_SSP },
1409 { PCI_VDEVICE(INTEL, 0x7e30), LPSS_CNL_SSP },
1410 { PCI_VDEVICE(INTEL, 0x7e46), LPSS_CNL_SSP },
1411
1412 { PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
1413 { PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
1414 { PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
1415
1416 { PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
1417 { PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
1418 { PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
1419
1420 { PCI_VDEVICE(INTEL, 0x02aa), LPSS_CNL_SSP },
1421 { PCI_VDEVICE(INTEL, 0x02ab), LPSS_CNL_SSP },
1422 { PCI_VDEVICE(INTEL, 0x02fb), LPSS_CNL_SSP },
1423
1424 { PCI_VDEVICE(INTEL, 0x06aa), LPSS_CNL_SSP },
1425 { PCI_VDEVICE(INTEL, 0x06ab), LPSS_CNL_SSP },
1426 { PCI_VDEVICE(INTEL, 0x06fb), LPSS_CNL_SSP },
1427
1428 { PCI_VDEVICE(INTEL, 0xa0aa), LPSS_CNL_SSP },
1429 { PCI_VDEVICE(INTEL, 0xa0ab), LPSS_CNL_SSP },
1430 { PCI_VDEVICE(INTEL, 0xa0de), LPSS_CNL_SSP },
1431 { PCI_VDEVICE(INTEL, 0xa0df), LPSS_CNL_SSP },
1432 { PCI_VDEVICE(INTEL, 0xa0fb), LPSS_CNL_SSP },
1433 { PCI_VDEVICE(INTEL, 0xa0fd), LPSS_CNL_SSP },
1434 { PCI_VDEVICE(INTEL, 0xa0fe), LPSS_CNL_SSP },
1435 { },
1436 };
1437
1438 static const struct of_device_id pxa2xx_spi_of_match[] = {
1439 { .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
1440 {},
1441 };
1442 MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
1443
1444 #ifdef CONFIG_ACPI
1445
1446 static int pxa2xx_spi_get_port_id(struct device *dev)
1447 {
1448 struct acpi_device *adev;
1449 unsigned int devid;
1450 int port_id = -1;
1451
1452 adev = ACPI_COMPANION(dev);
1453 if (adev && adev->pnp.unique_id &&
1454 !kstrtouint(adev->pnp.unique_id, 0, &devid))
1455 port_id = devid;
1456 return port_id;
1457 }
1458
1459 #else
1460
1461 static int pxa2xx_spi_get_port_id(struct device *dev)
1462 {
1463 return -1;
1464 }
1465
1466 #endif
1467
1468
1469 #ifdef CONFIG_PCI
1470
1471 static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
1472 {
1473 return param == chan->device->dev;
1474 }
1475
1476 #endif
1477
1478 static struct pxa2xx_spi_controller *
1479 pxa2xx_spi_init_pdata(struct platform_device *pdev)
1480 {
1481 struct pxa2xx_spi_controller *pdata;
1482 struct ssp_device *ssp;
1483 struct resource *res;
1484 struct device *parent = pdev->dev.parent;
1485 struct pci_dev *pcidev = dev_is_pci(parent) ? to_pci_dev(parent) : NULL;
1486 const struct pci_device_id *pcidev_id = NULL;
1487 enum pxa_ssp_type type;
1488 const void *match;
1489
1490 if (pcidev)
1491 pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, pcidev);
1492
1493 match = device_get_match_data(&pdev->dev);
1494 if (match)
1495 type = (enum pxa_ssp_type)match;
1496 else if (pcidev_id)
1497 type = (enum pxa_ssp_type)pcidev_id->driver_data;
1498 else
1499 return ERR_PTR(-EINVAL);
1500
1501 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1502 if (!pdata)
1503 return ERR_PTR(-ENOMEM);
1504
1505 ssp = &pdata->ssp;
1506
1507 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1508 ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1509 if (IS_ERR(ssp->mmio_base))
1510 return ERR_CAST(ssp->mmio_base);
1511
1512 ssp->phys_base = res->start;
1513
1514 #ifdef CONFIG_PCI
1515 if (pcidev_id) {
1516 pdata->tx_param = parent;
1517 pdata->rx_param = parent;
1518 pdata->dma_filter = pxa2xx_spi_idma_filter;
1519 }
1520 #endif
1521
1522 ssp->clk = devm_clk_get(&pdev->dev, NULL);
1523 if (IS_ERR(ssp->clk))
1524 return ERR_CAST(ssp->clk);
1525
1526 ssp->irq = platform_get_irq(pdev, 0);
1527 if (ssp->irq < 0)
1528 return ERR_PTR(ssp->irq);
1529
1530 ssp->type = type;
1531 ssp->dev = &pdev->dev;
1532 ssp->port_id = pxa2xx_spi_get_port_id(&pdev->dev);
1533
1534 pdata->is_slave = device_property_read_bool(&pdev->dev, "spi-slave");
1535 pdata->num_chipselect = 1;
1536 pdata->enable_dma = true;
1537 pdata->dma_burst_size = 1;
1538
1539 return pdata;
1540 }
1541
1542 static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
1543 unsigned int cs)
1544 {
1545 struct driver_data *drv_data = spi_controller_get_devdata(controller);
1546
1547 if (has_acpi_companion(drv_data->ssp->dev)) {
1548 switch (drv_data->ssp_type) {
1549
1550
1551
1552
1553
1554 case LPSS_BYT_SSP:
1555 case LPSS_BSW_SSP:
1556 return cs - 1;
1557
1558 default:
1559 break;
1560 }
1561 }
1562
1563 return cs;
1564 }
1565
1566 static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi)
1567 {
1568 return MAX_DMA_LEN;
1569 }
1570
1571 static int pxa2xx_spi_probe(struct platform_device *pdev)
1572 {
1573 struct device *dev = &pdev->dev;
1574 struct pxa2xx_spi_controller *platform_info;
1575 struct spi_controller *controller;
1576 struct driver_data *drv_data;
1577 struct ssp_device *ssp;
1578 const struct lpss_config *config;
1579 int status;
1580 u32 tmp;
1581
1582 platform_info = dev_get_platdata(dev);
1583 if (!platform_info) {
1584 platform_info = pxa2xx_spi_init_pdata(pdev);
1585 if (IS_ERR(platform_info)) {
1586 dev_err(&pdev->dev, "missing platform data\n");
1587 return PTR_ERR(platform_info);
1588 }
1589 }
1590
1591 ssp = pxa_ssp_request(pdev->id, pdev->name);
1592 if (!ssp)
1593 ssp = &platform_info->ssp;
1594
1595 if (!ssp->mmio_base) {
1596 dev_err(&pdev->dev, "failed to get SSP\n");
1597 return -ENODEV;
1598 }
1599
1600 if (platform_info->is_slave)
1601 controller = devm_spi_alloc_slave(dev, sizeof(*drv_data));
1602 else
1603 controller = devm_spi_alloc_master(dev, sizeof(*drv_data));
1604
1605 if (!controller) {
1606 dev_err(&pdev->dev, "cannot alloc spi_controller\n");
1607 status = -ENOMEM;
1608 goto out_error_controller_alloc;
1609 }
1610 drv_data = spi_controller_get_devdata(controller);
1611 drv_data->controller = controller;
1612 drv_data->controller_info = platform_info;
1613 drv_data->ssp = ssp;
1614
1615 device_set_node(&controller->dev, dev_fwnode(dev));
1616
1617
1618 controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1619
1620 controller->bus_num = ssp->port_id;
1621 controller->dma_alignment = DMA_ALIGNMENT;
1622 controller->cleanup = cleanup;
1623 controller->setup = setup;
1624 controller->set_cs = pxa2xx_spi_set_cs;
1625 controller->transfer_one = pxa2xx_spi_transfer_one;
1626 controller->slave_abort = pxa2xx_spi_slave_abort;
1627 controller->handle_err = pxa2xx_spi_handle_err;
1628 controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
1629 controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
1630 controller->auto_runtime_pm = true;
1631 controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
1632
1633 drv_data->ssp_type = ssp->type;
1634
1635 if (pxa25x_ssp_comp(drv_data)) {
1636 switch (drv_data->ssp_type) {
1637 case QUARK_X1000_SSP:
1638 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1639 break;
1640 default:
1641 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1642 break;
1643 }
1644
1645 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1646 drv_data->dma_cr1 = 0;
1647 drv_data->clear_sr = SSSR_ROR;
1648 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1649 } else {
1650 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1651 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
1652 drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1653 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1654 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1655 | SSSR_ROR | SSSR_TUR;
1656 }
1657
1658 status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1659 drv_data);
1660 if (status < 0) {
1661 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
1662 goto out_error_controller_alloc;
1663 }
1664
1665
1666 if (platform_info->enable_dma) {
1667 status = pxa2xx_spi_dma_setup(drv_data);
1668 if (status) {
1669 dev_warn(dev, "no DMA channels available, using PIO\n");
1670 platform_info->enable_dma = false;
1671 } else {
1672 controller->can_dma = pxa2xx_spi_can_dma;
1673 controller->max_dma_len = MAX_DMA_LEN;
1674 controller->max_transfer_size =
1675 pxa2xx_spi_max_dma_transfer_size;
1676 }
1677 }
1678
1679
1680 status = clk_prepare_enable(ssp->clk);
1681 if (status)
1682 goto out_error_dma_irq_alloc;
1683
1684 controller->max_speed_hz = clk_get_rate(ssp->clk);
1685
1686
1687
1688
1689 if (!pxa25x_ssp_comp(drv_data))
1690 controller->min_speed_hz =
1691 DIV_ROUND_UP(controller->max_speed_hz, 4096);
1692 else if (!is_quark_x1000_ssp(drv_data))
1693 controller->min_speed_hz =
1694 DIV_ROUND_UP(controller->max_speed_hz, 512);
1695
1696 pxa_ssp_disable(ssp);
1697
1698
1699 switch (drv_data->ssp_type) {
1700 case QUARK_X1000_SSP:
1701 tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
1702 QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1703 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1704
1705
1706 tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
1707 pxa2xx_spi_write(drv_data, SSCR0, tmp);
1708 break;
1709 case CE4100_SSP:
1710 tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
1711 CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
1712 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1713 tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
1714 pxa2xx_spi_write(drv_data, SSCR0, tmp);
1715 break;
1716 default:
1717
1718 if (spi_controller_is_slave(controller)) {
1719 tmp = SSCR1_SCFR |
1720 SSCR1_SCLKDIR |
1721 SSCR1_SFRMDIR |
1722 SSCR1_RxTresh(2) |
1723 SSCR1_TxTresh(1) |
1724 SSCR1_SPH;
1725 } else {
1726 tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1727 SSCR1_TxTresh(TX_THRESH_DFLT);
1728 }
1729 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1730 tmp = SSCR0_Motorola | SSCR0_DataSize(8);
1731 if (!spi_controller_is_slave(controller))
1732 tmp |= SSCR0_SCR(2);
1733 pxa2xx_spi_write(drv_data, SSCR0, tmp);
1734 break;
1735 }
1736
1737 if (!pxa25x_ssp_comp(drv_data))
1738 pxa2xx_spi_write(drv_data, SSTO, 0);
1739
1740 if (!is_quark_x1000_ssp(drv_data))
1741 pxa2xx_spi_write(drv_data, SSPSP, 0);
1742
1743 if (is_lpss_ssp(drv_data)) {
1744 lpss_ssp_setup(drv_data);
1745 config = lpss_get_config(drv_data);
1746 if (config->reg_capabilities >= 0) {
1747 tmp = __lpss_ssp_read_priv(drv_data,
1748 config->reg_capabilities);
1749 tmp &= LPSS_CAPS_CS_EN_MASK;
1750 tmp >>= LPSS_CAPS_CS_EN_SHIFT;
1751 platform_info->num_chipselect = ffz(tmp);
1752 } else if (config->cs_num) {
1753 platform_info->num_chipselect = config->cs_num;
1754 }
1755 }
1756 controller->num_chipselect = platform_info->num_chipselect;
1757 controller->use_gpio_descriptors = true;
1758
1759 if (platform_info->is_slave) {
1760 drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
1761 "ready", GPIOD_OUT_LOW);
1762 if (IS_ERR(drv_data->gpiod_ready)) {
1763 status = PTR_ERR(drv_data->gpiod_ready);
1764 goto out_error_clock_enabled;
1765 }
1766 }
1767
1768 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1769 pm_runtime_use_autosuspend(&pdev->dev);
1770 pm_runtime_set_active(&pdev->dev);
1771 pm_runtime_enable(&pdev->dev);
1772
1773
1774 platform_set_drvdata(pdev, drv_data);
1775 status = spi_register_controller(controller);
1776 if (status) {
1777 dev_err(&pdev->dev, "problem registering SPI controller\n");
1778 goto out_error_pm_runtime_enabled;
1779 }
1780
1781 return status;
1782
1783 out_error_pm_runtime_enabled:
1784 pm_runtime_disable(&pdev->dev);
1785
1786 out_error_clock_enabled:
1787 clk_disable_unprepare(ssp->clk);
1788
1789 out_error_dma_irq_alloc:
1790 pxa2xx_spi_dma_release(drv_data);
1791 free_irq(ssp->irq, drv_data);
1792
1793 out_error_controller_alloc:
1794 pxa_ssp_free(ssp);
1795 return status;
1796 }
1797
1798 static int pxa2xx_spi_remove(struct platform_device *pdev)
1799 {
1800 struct driver_data *drv_data = platform_get_drvdata(pdev);
1801 struct ssp_device *ssp = drv_data->ssp;
1802
1803 pm_runtime_get_sync(&pdev->dev);
1804
1805 spi_unregister_controller(drv_data->controller);
1806
1807
1808 pxa_ssp_disable(ssp);
1809 clk_disable_unprepare(ssp->clk);
1810
1811
1812 if (drv_data->controller_info->enable_dma)
1813 pxa2xx_spi_dma_release(drv_data);
1814
1815 pm_runtime_put_noidle(&pdev->dev);
1816 pm_runtime_disable(&pdev->dev);
1817
1818
1819 free_irq(ssp->irq, drv_data);
1820
1821
1822 pxa_ssp_free(ssp);
1823
1824 return 0;
1825 }
1826
1827 #ifdef CONFIG_PM_SLEEP
1828 static int pxa2xx_spi_suspend(struct device *dev)
1829 {
1830 struct driver_data *drv_data = dev_get_drvdata(dev);
1831 struct ssp_device *ssp = drv_data->ssp;
1832 int status;
1833
1834 status = spi_controller_suspend(drv_data->controller);
1835 if (status)
1836 return status;
1837
1838 pxa_ssp_disable(ssp);
1839
1840 if (!pm_runtime_suspended(dev))
1841 clk_disable_unprepare(ssp->clk);
1842
1843 return 0;
1844 }
1845
1846 static int pxa2xx_spi_resume(struct device *dev)
1847 {
1848 struct driver_data *drv_data = dev_get_drvdata(dev);
1849 struct ssp_device *ssp = drv_data->ssp;
1850 int status;
1851
1852
1853 if (!pm_runtime_suspended(dev)) {
1854 status = clk_prepare_enable(ssp->clk);
1855 if (status)
1856 return status;
1857 }
1858
1859
1860 return spi_controller_resume(drv_data->controller);
1861 }
1862 #endif
1863
1864 #ifdef CONFIG_PM
1865 static int pxa2xx_spi_runtime_suspend(struct device *dev)
1866 {
1867 struct driver_data *drv_data = dev_get_drvdata(dev);
1868
1869 clk_disable_unprepare(drv_data->ssp->clk);
1870 return 0;
1871 }
1872
1873 static int pxa2xx_spi_runtime_resume(struct device *dev)
1874 {
1875 struct driver_data *drv_data = dev_get_drvdata(dev);
1876 int status;
1877
1878 status = clk_prepare_enable(drv_data->ssp->clk);
1879 return status;
1880 }
1881 #endif
1882
1883 static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
1884 SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
1885 SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
1886 pxa2xx_spi_runtime_resume, NULL)
1887 };
1888
1889 static struct platform_driver driver = {
1890 .driver = {
1891 .name = "pxa2xx-spi",
1892 .pm = &pxa2xx_spi_pm_ops,
1893 .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
1894 .of_match_table = of_match_ptr(pxa2xx_spi_of_match),
1895 },
1896 .probe = pxa2xx_spi_probe,
1897 .remove = pxa2xx_spi_remove,
1898 };
1899
1900 static int __init pxa2xx_spi_init(void)
1901 {
1902 return platform_driver_register(&driver);
1903 }
1904 subsys_initcall(pxa2xx_spi_init);
1905
1906 static void __exit pxa2xx_spi_exit(void)
1907 {
1908 platform_driver_unregister(&driver);
1909 }
1910 module_exit(pxa2xx_spi_exit);
1911
1912 MODULE_SOFTDEP("pre: dw_dmac");