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