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