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