1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Driver for Realtek PCI-Express card reader 4 * 5 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved. 6 * 7 * Author: 8 * Wei WANG (wei_wang@realsil.com.cn) 9 * Micky Ching (micky_ching@realsil.com.cn) 10 */ 11 12 #include <linux/blkdev.h> 13 #include <linux/kthread.h> 14 #include <linux/sched.h> 15 #include <linux/workqueue.h> 16 17 #include "rtsx.h" 18 #include "ms.h" 19 #include "sd.h" 20 #include "xd.h" 21 22 MODULE_DESCRIPTION("Realtek PCI-Express card reader rts5208/rts5288 driver"); 23 MODULE_LICENSE("GPL"); 24 25 static unsigned int delay_use = 1; 26 module_param(delay_use, uint, 0644); 27 MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device"); 28 29 static int ss_en; 30 module_param(ss_en, int, 0644); 31 MODULE_PARM_DESC(ss_en, "enable selective suspend"); 32 33 static int ss_interval = 50; 34 module_param(ss_interval, int, 0644); 35 MODULE_PARM_DESC(ss_interval, "Interval to enter ss state in seconds"); 36 37 static int auto_delink_en; 38 module_param(auto_delink_en, int, 0644); 39 MODULE_PARM_DESC(auto_delink_en, "enable auto delink"); 40 41 static unsigned char aspm_l0s_l1_en; 42 module_param(aspm_l0s_l1_en, byte, 0644); 43 MODULE_PARM_DESC(aspm_l0s_l1_en, "enable device aspm"); 44 45 static int msi_en; 46 module_param(msi_en, int, 0644); 47 MODULE_PARM_DESC(msi_en, "enable msi"); 48 49 static irqreturn_t rtsx_interrupt(int irq, void *dev_id); 50 51 /*********************************************************************** 52 * Host functions 53 ***********************************************************************/ 54 55 static const char *host_info(struct Scsi_Host *host) 56 { 57 return "SCSI emulation for PCI-Express Mass Storage devices"; 58 } 59 60 static int slave_alloc(struct scsi_device *sdev) 61 { 62 /* 63 * Set the INQUIRY transfer length to 36. We don't use any of 64 * the extra data and many devices choke if asked for more or 65 * less than 36 bytes. 66 */ 67 sdev->inquiry_len = 36; 68 return 0; 69 } 70 71 static int slave_configure(struct scsi_device *sdev) 72 { 73 /* 74 * Scatter-gather buffers (all but the last) must have a length 75 * divisible by the bulk maxpacket size. Otherwise a data packet 76 * would end up being short, causing a premature end to the data 77 * transfer. Since high-speed bulk pipes have a maxpacket size 78 * of 512, we'll use that as the scsi device queue's DMA alignment 79 * mask. Guaranteeing proper alignment of the first buffer will 80 * have the desired effect because, except at the beginning and 81 * the end, scatter-gather buffers follow page boundaries. 82 */ 83 blk_queue_dma_alignment(sdev->request_queue, (512 - 1)); 84 85 /* Set the SCSI level to at least 2. We'll leave it at 3 if that's 86 * what is originally reported. We need this to avoid confusing 87 * the SCSI layer with devices that report 0 or 1, but need 10-byte 88 * commands (ala ATAPI devices behind certain bridges, or devices 89 * which simply have broken INQUIRY data). 90 * 91 * NOTE: This means /dev/sg programs (ala cdrecord) will get the 92 * actual information. This seems to be the preference for 93 * programs like that. 94 * 95 * NOTE: This also means that /proc/scsi/scsi and sysfs may report 96 * the actual value or the modified one, depending on where the 97 * data comes from. 98 */ 99 if (sdev->scsi_level < SCSI_2) { 100 sdev->scsi_level = SCSI_2; 101 sdev->sdev_target->scsi_level = SCSI_2; 102 } 103 104 return 0; 105 } 106 107 /*********************************************************************** 108 * /proc/scsi/ functions 109 ***********************************************************************/ 110 111 /* we use this macro to help us write into the buffer */ 112 #undef SPRINTF 113 #define SPRINTF(args...) \ 114 do { \ 115 if (pos < buffer + length) \ 116 pos += sprintf(pos, ## args); \ 117 } while (0) 118 119 /* queue a command */ 120 /* This is always called with scsi_lock(host) held */ 121 static int queuecommand_lck(struct scsi_cmnd *srb, 122 void (*done)(struct scsi_cmnd *)) 123 { 124 struct rtsx_dev *dev = host_to_rtsx(srb->device->host); 125 struct rtsx_chip *chip = dev->chip; 126 127 /* check for state-transition errors */ 128 if (chip->srb) { 129 dev_err(&dev->pci->dev, "Error: chip->srb = %p\n", 130 chip->srb); 131 return SCSI_MLQUEUE_HOST_BUSY; 132 } 133 134 /* fail the command if we are disconnecting */ 135 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) { 136 dev_info(&dev->pci->dev, "Fail command during disconnect\n"); 137 srb->result = DID_NO_CONNECT << 16; 138 done(srb); 139 return 0; 140 } 141 142 /* enqueue the command and wake up the control thread */ 143 srb->scsi_done = done; 144 chip->srb = srb; 145 complete(&dev->cmnd_ready); 146 147 return 0; 148 } 149 150 static DEF_SCSI_QCMD(queuecommand) 151 152 /*********************************************************************** 153 * Error handling functions 154 ***********************************************************************/ 155 156 /* Command timeout and abort */ 157 static int command_abort(struct scsi_cmnd *srb) 158 { 159 struct Scsi_Host *host = srb->device->host; 160 struct rtsx_dev *dev = host_to_rtsx(host); 161 struct rtsx_chip *chip = dev->chip; 162 163 dev_info(&dev->pci->dev, "%s called\n", __func__); 164 165 scsi_lock(host); 166 167 /* Is this command still active? */ 168 if (chip->srb != srb) { 169 scsi_unlock(host); 170 dev_info(&dev->pci->dev, "-- nothing to abort\n"); 171 return FAILED; 172 } 173 174 rtsx_set_stat(chip, RTSX_STAT_ABORT); 175 176 scsi_unlock(host); 177 178 /* Wait for the aborted command to finish */ 179 wait_for_completion(&dev->notify); 180 181 return SUCCESS; 182 } 183 184 /* 185 * This invokes the transport reset mechanism to reset the state of the 186 * device 187 */ 188 static int device_reset(struct scsi_cmnd *srb) 189 { 190 struct rtsx_dev *dev = host_to_rtsx(srb->device->host); 191 192 dev_info(&dev->pci->dev, "%s called\n", __func__); 193 194 return SUCCESS; 195 } 196 197 /* 198 * this defines our host template, with which we'll allocate hosts 199 */ 200 201 static struct scsi_host_template rtsx_host_template = { 202 /* basic userland interface stuff */ 203 .name = CR_DRIVER_NAME, 204 .proc_name = CR_DRIVER_NAME, 205 .info = host_info, 206 207 /* command interface -- queued only */ 208 .queuecommand = queuecommand, 209 210 /* error and abort handlers */ 211 .eh_abort_handler = command_abort, 212 .eh_device_reset_handler = device_reset, 213 214 /* queue commands only, only one command per LUN */ 215 .can_queue = 1, 216 217 /* unknown initiator id */ 218 .this_id = -1, 219 220 .slave_alloc = slave_alloc, 221 .slave_configure = slave_configure, 222 223 /* lots of sg segments can be handled */ 224 .sg_tablesize = SG_ALL, 225 226 /* limit the total size of a transfer to 120 KB */ 227 .max_sectors = 240, 228 229 /* emulated HBA */ 230 .emulated = 1, 231 232 /* we do our own delay after a device or bus reset */ 233 .skip_settle_delay = 1, 234 235 /* module management */ 236 .module = THIS_MODULE 237 }; 238 239 static int rtsx_acquire_irq(struct rtsx_dev *dev) 240 { 241 struct rtsx_chip *chip = dev->chip; 242 243 dev_info(&dev->pci->dev, "%s: chip->msi_en = %d, pci->irq = %d\n", 244 __func__, chip->msi_en, dev->pci->irq); 245 246 if (request_irq(dev->pci->irq, rtsx_interrupt, 247 chip->msi_en ? 0 : IRQF_SHARED, 248 CR_DRIVER_NAME, dev)) { 249 dev_err(&dev->pci->dev, 250 "rtsx: unable to grab IRQ %d, disabling device\n", 251 dev->pci->irq); 252 return -1; 253 } 254 255 dev->irq = dev->pci->irq; 256 pci_intx(dev->pci, !chip->msi_en); 257 258 return 0; 259 } 260 261 #ifdef CONFIG_PM 262 /* 263 * power management 264 */ 265 static int rtsx_suspend(struct pci_dev *pci, pm_message_t state) 266 { 267 struct rtsx_dev *dev = pci_get_drvdata(pci); 268 struct rtsx_chip *chip; 269 270 if (!dev) 271 return 0; 272 273 /* lock the device pointers */ 274 mutex_lock(&dev->dev_mutex); 275 276 chip = dev->chip; 277 278 rtsx_do_before_power_down(chip, PM_S3); 279 280 if (dev->irq >= 0) { 281 free_irq(dev->irq, (void *)dev); 282 dev->irq = -1; 283 } 284 285 if (chip->msi_en) 286 pci_disable_msi(pci); 287 288 pci_save_state(pci); 289 pci_enable_wake(pci, pci_choose_state(pci, state), 1); 290 pci_disable_device(pci); 291 pci_set_power_state(pci, pci_choose_state(pci, state)); 292 293 /* unlock the device pointers */ 294 mutex_unlock(&dev->dev_mutex); 295 296 return 0; 297 } 298 299 static int rtsx_resume(struct pci_dev *pci) 300 { 301 struct rtsx_dev *dev = pci_get_drvdata(pci); 302 struct rtsx_chip *chip; 303 304 if (!dev) 305 return 0; 306 307 chip = dev->chip; 308 309 /* lock the device pointers */ 310 mutex_lock(&dev->dev_mutex); 311 312 pci_set_power_state(pci, PCI_D0); 313 pci_restore_state(pci); 314 if (pci_enable_device(pci) < 0) { 315 dev_err(&dev->pci->dev, 316 "%s: pci_enable_device failed, disabling device\n", 317 CR_DRIVER_NAME); 318 /* unlock the device pointers */ 319 mutex_unlock(&dev->dev_mutex); 320 return -EIO; 321 } 322 pci_set_master(pci); 323 324 if (chip->msi_en) { 325 if (pci_enable_msi(pci) < 0) 326 chip->msi_en = 0; 327 } 328 329 if (rtsx_acquire_irq(dev) < 0) { 330 /* unlock the device pointers */ 331 mutex_unlock(&dev->dev_mutex); 332 return -EIO; 333 } 334 335 rtsx_write_register(chip, HOST_SLEEP_STATE, 0x03, 0x00); 336 rtsx_init_chip(chip); 337 338 /* unlock the device pointers */ 339 mutex_unlock(&dev->dev_mutex); 340 341 return 0; 342 } 343 #endif /* CONFIG_PM */ 344 345 static void rtsx_shutdown(struct pci_dev *pci) 346 { 347 struct rtsx_dev *dev = pci_get_drvdata(pci); 348 struct rtsx_chip *chip; 349 350 if (!dev) 351 return; 352 353 chip = dev->chip; 354 355 rtsx_do_before_power_down(chip, PM_S1); 356 357 if (dev->irq >= 0) { 358 free_irq(dev->irq, (void *)dev); 359 dev->irq = -1; 360 } 361 362 if (chip->msi_en) 363 pci_disable_msi(pci); 364 365 pci_disable_device(pci); 366 } 367 368 static int rtsx_control_thread(void *__dev) 369 { 370 struct rtsx_dev *dev = __dev; 371 struct rtsx_chip *chip = dev->chip; 372 struct Scsi_Host *host = rtsx_to_host(dev); 373 374 for (;;) { 375 if (wait_for_completion_interruptible(&dev->cmnd_ready)) 376 break; 377 378 /* lock the device pointers */ 379 mutex_lock(&dev->dev_mutex); 380 381 /* if the device has disconnected, we are free to exit */ 382 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) { 383 dev_info(&dev->pci->dev, "-- rtsx-control exiting\n"); 384 mutex_unlock(&dev->dev_mutex); 385 break; 386 } 387 388 /* lock access to the state */ 389 scsi_lock(host); 390 391 /* has the command aborted ? */ 392 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) { 393 chip->srb->result = DID_ABORT << 16; 394 goto skip_for_abort; 395 } 396 397 scsi_unlock(host); 398 399 /* reject the command if the direction indicator 400 * is UNKNOWN 401 */ 402 if (chip->srb->sc_data_direction == DMA_BIDIRECTIONAL) { 403 dev_err(&dev->pci->dev, "UNKNOWN data direction\n"); 404 chip->srb->result = DID_ERROR << 16; 405 } 406 407 /* reject if target != 0 or if LUN is higher than 408 * the maximum known LUN 409 */ 410 else if (chip->srb->device->id) { 411 dev_err(&dev->pci->dev, "Bad target number (%d:%d)\n", 412 chip->srb->device->id, 413 (u8)chip->srb->device->lun); 414 chip->srb->result = DID_BAD_TARGET << 16; 415 } 416 417 else if (chip->srb->device->lun > chip->max_lun) { 418 dev_err(&dev->pci->dev, "Bad LUN (%d:%d)\n", 419 chip->srb->device->id, 420 (u8)chip->srb->device->lun); 421 chip->srb->result = DID_BAD_TARGET << 16; 422 } 423 424 /* we've got a command, let's do it! */ 425 else { 426 scsi_show_command(chip); 427 rtsx_invoke_transport(chip->srb, chip); 428 } 429 430 /* lock access to the state */ 431 scsi_lock(host); 432 433 /* did the command already complete because of a disconnect? */ 434 if (!chip->srb) 435 ; /* nothing to do */ 436 437 /* indicate that the command is done */ 438 else if (chip->srb->result != DID_ABORT << 16) { 439 chip->srb->scsi_done(chip->srb); 440 } else { 441 skip_for_abort: 442 dev_err(&dev->pci->dev, "scsi command aborted\n"); 443 } 444 445 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) { 446 complete(&dev->notify); 447 448 rtsx_set_stat(chip, RTSX_STAT_IDLE); 449 } 450 451 /* finished working on this command */ 452 chip->srb = NULL; 453 scsi_unlock(host); 454 455 /* unlock the device pointers */ 456 mutex_unlock(&dev->dev_mutex); 457 } /* for (;;) */ 458 459 /* notify the exit routine that we're actually exiting now 460 * 461 * complete()/wait_for_completion() is similar to up()/down(), 462 * except that complete() is safe in the case where the structure 463 * is getting deleted in a parallel mode of execution (i.e. just 464 * after the down() -- that's necessary for the thread-shutdown 465 * case. 466 * 467 * complete_and_exit() goes even further than this -- it is safe in 468 * the case that the thread of the caller is going away (not just 469 * the structure) -- this is necessary for the module-remove case. 470 * This is important in preemption kernels, which transfer the flow 471 * of execution immediately upon a complete(). 472 */ 473 complete_and_exit(&dev->control_exit, 0); 474 } 475 476 static int rtsx_polling_thread(void *__dev) 477 { 478 struct rtsx_dev *dev = __dev; 479 struct rtsx_chip *chip = dev->chip; 480 struct sd_info *sd_card = &chip->sd_card; 481 struct xd_info *xd_card = &chip->xd_card; 482 struct ms_info *ms_card = &chip->ms_card; 483 484 sd_card->cleanup_counter = 0; 485 xd_card->cleanup_counter = 0; 486 ms_card->cleanup_counter = 0; 487 488 /* Wait until SCSI scan finished */ 489 wait_timeout((delay_use + 5) * 1000); 490 491 for (;;) { 492 set_current_state(TASK_INTERRUPTIBLE); 493 schedule_timeout(msecs_to_jiffies(POLLING_INTERVAL)); 494 495 /* lock the device pointers */ 496 mutex_lock(&dev->dev_mutex); 497 498 /* if the device has disconnected, we are free to exit */ 499 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) { 500 dev_info(&dev->pci->dev, "-- rtsx-polling exiting\n"); 501 mutex_unlock(&dev->dev_mutex); 502 break; 503 } 504 505 mutex_unlock(&dev->dev_mutex); 506 507 mspro_polling_format_status(chip); 508 509 /* lock the device pointers */ 510 mutex_lock(&dev->dev_mutex); 511 512 rtsx_polling_func(chip); 513 514 /* unlock the device pointers */ 515 mutex_unlock(&dev->dev_mutex); 516 } 517 518 complete_and_exit(&dev->polling_exit, 0); 519 } 520 521 /* 522 * interrupt handler 523 */ 524 static irqreturn_t rtsx_interrupt(int irq, void *dev_id) 525 { 526 struct rtsx_dev *dev = dev_id; 527 struct rtsx_chip *chip; 528 int retval; 529 u32 status; 530 531 if (dev) 532 chip = dev->chip; 533 else 534 return IRQ_NONE; 535 536 if (!chip) 537 return IRQ_NONE; 538 539 spin_lock(&dev->reg_lock); 540 541 retval = rtsx_pre_handle_interrupt(chip); 542 if (retval == STATUS_FAIL) { 543 spin_unlock(&dev->reg_lock); 544 if (chip->int_reg == 0xFFFFFFFF) 545 return IRQ_HANDLED; 546 return IRQ_NONE; 547 } 548 549 status = chip->int_reg; 550 551 if (dev->check_card_cd) { 552 if (!(dev->check_card_cd & status)) { 553 /* card not exist, return TRANS_RESULT_FAIL */ 554 dev->trans_result = TRANS_RESULT_FAIL; 555 if (dev->done) 556 complete(dev->done); 557 goto exit; 558 } 559 } 560 561 if (status & (NEED_COMPLETE_INT | DELINK_INT)) { 562 if (status & (TRANS_FAIL_INT | DELINK_INT)) { 563 if (status & DELINK_INT) 564 RTSX_SET_DELINK(chip); 565 dev->trans_result = TRANS_RESULT_FAIL; 566 if (dev->done) 567 complete(dev->done); 568 } else if (status & TRANS_OK_INT) { 569 dev->trans_result = TRANS_RESULT_OK; 570 if (dev->done) 571 complete(dev->done); 572 } else if (status & DATA_DONE_INT) { 573 dev->trans_result = TRANS_NOT_READY; 574 if (dev->done && (dev->trans_state == STATE_TRANS_SG)) 575 complete(dev->done); 576 } 577 } 578 579 exit: 580 spin_unlock(&dev->reg_lock); 581 return IRQ_HANDLED; 582 } 583 584 /* Release all our dynamic resources */ 585 static void rtsx_release_resources(struct rtsx_dev *dev) 586 { 587 dev_info(&dev->pci->dev, "-- %s\n", __func__); 588 589 /* Tell the control thread to exit. The SCSI host must 590 * already have been removed so it won't try to queue 591 * any more commands. 592 */ 593 dev_info(&dev->pci->dev, "-- sending exit command to thread\n"); 594 complete(&dev->cmnd_ready); 595 if (dev->ctl_thread) 596 wait_for_completion(&dev->control_exit); 597 if (dev->polling_thread) 598 wait_for_completion(&dev->polling_exit); 599 600 wait_timeout(200); 601 602 if (dev->rtsx_resv_buf) { 603 dev->chip->host_cmds_ptr = NULL; 604 dev->chip->host_sg_tbl_ptr = NULL; 605 } 606 607 if (dev->irq > 0) 608 free_irq(dev->irq, (void *)dev); 609 if (dev->chip->msi_en) 610 pci_disable_msi(dev->pci); 611 if (dev->remap_addr) 612 iounmap(dev->remap_addr); 613 614 rtsx_release_chip(dev->chip); 615 kfree(dev->chip); 616 } 617 618 /* 619 * First stage of disconnect processing: stop all commands and remove 620 * the host 621 */ 622 static void quiesce_and_remove_host(struct rtsx_dev *dev) 623 { 624 struct Scsi_Host *host = rtsx_to_host(dev); 625 struct rtsx_chip *chip = dev->chip; 626 627 /* 628 * Prevent new transfers, stop the current command, and 629 * interrupt a SCSI-scan or device-reset delay 630 */ 631 mutex_lock(&dev->dev_mutex); 632 scsi_lock(host); 633 rtsx_set_stat(chip, RTSX_STAT_DISCONNECT); 634 scsi_unlock(host); 635 mutex_unlock(&dev->dev_mutex); 636 wake_up(&dev->delay_wait); 637 wait_for_completion(&dev->scanning_done); 638 639 /* Wait some time to let other threads exist */ 640 wait_timeout(100); 641 642 /* 643 * queuecommand won't accept any new commands and the control 644 * thread won't execute a previously-queued command. If there 645 * is such a command pending, complete it with an error. 646 */ 647 mutex_lock(&dev->dev_mutex); 648 if (chip->srb) { 649 chip->srb->result = DID_NO_CONNECT << 16; 650 scsi_lock(host); 651 chip->srb->scsi_done(dev->chip->srb); 652 chip->srb = NULL; 653 scsi_unlock(host); 654 } 655 mutex_unlock(&dev->dev_mutex); 656 657 /* Now we own no commands so it's safe to remove the SCSI host */ 658 scsi_remove_host(host); 659 } 660 661 /* Second stage of disconnect processing: deallocate all resources */ 662 static void release_everything(struct rtsx_dev *dev) 663 { 664 rtsx_release_resources(dev); 665 666 /* 667 * Drop our reference to the host; the SCSI core will free it 668 * when the refcount becomes 0. 669 */ 670 scsi_host_put(rtsx_to_host(dev)); 671 } 672 673 /* Thread to carry out delayed SCSI-device scanning */ 674 static int rtsx_scan_thread(void *__dev) 675 { 676 struct rtsx_dev *dev = __dev; 677 struct rtsx_chip *chip = dev->chip; 678 679 /* Wait for the timeout to expire or for a disconnect */ 680 if (delay_use > 0) { 681 dev_info(&dev->pci->dev, 682 "%s: waiting for device to settle before scanning\n", 683 CR_DRIVER_NAME); 684 wait_event_interruptible_timeout 685 (dev->delay_wait, 686 rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT), 687 delay_use * HZ); 688 } 689 690 /* If the device is still connected, perform the scanning */ 691 if (!rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) { 692 scsi_scan_host(rtsx_to_host(dev)); 693 dev_info(&dev->pci->dev, "%s: device scan complete\n", 694 CR_DRIVER_NAME); 695 696 /* Should we unbind if no devices were detected? */ 697 } 698 699 complete_and_exit(&dev->scanning_done, 0); 700 } 701 702 static void rtsx_init_options(struct rtsx_chip *chip) 703 { 704 chip->vendor_id = chip->rtsx->pci->vendor; 705 chip->product_id = chip->rtsx->pci->device; 706 chip->adma_mode = 1; 707 chip->lun_mc = 0; 708 chip->driver_first_load = 1; 709 #ifdef HW_AUTO_SWITCH_SD_BUS 710 chip->sdio_in_charge = 0; 711 #endif 712 713 chip->mspro_formatter_enable = 1; 714 chip->ignore_sd = 0; 715 chip->use_hw_setting = 0; 716 chip->lun_mode = DEFAULT_SINGLE; 717 chip->auto_delink_en = auto_delink_en; 718 chip->ss_en = ss_en; 719 chip->ss_idle_period = ss_interval * 1000; 720 chip->remote_wakeup_en = 0; 721 chip->aspm_l0s_l1_en = aspm_l0s_l1_en; 722 chip->dynamic_aspm = 1; 723 chip->fpga_sd_sdr104_clk = CLK_200; 724 chip->fpga_sd_ddr50_clk = CLK_100; 725 chip->fpga_sd_sdr50_clk = CLK_100; 726 chip->fpga_sd_hs_clk = CLK_100; 727 chip->fpga_mmc_52m_clk = CLK_80; 728 chip->fpga_ms_hg_clk = CLK_80; 729 chip->fpga_ms_4bit_clk = CLK_80; 730 chip->fpga_ms_1bit_clk = CLK_40; 731 chip->asic_sd_sdr104_clk = 203; 732 chip->asic_sd_sdr50_clk = 98; 733 chip->asic_sd_ddr50_clk = 98; 734 chip->asic_sd_hs_clk = 98; 735 chip->asic_mmc_52m_clk = 98; 736 chip->asic_ms_hg_clk = 117; 737 chip->asic_ms_4bit_clk = 78; 738 chip->asic_ms_1bit_clk = 39; 739 chip->ssc_depth_sd_sdr104 = SSC_DEPTH_2M; 740 chip->ssc_depth_sd_sdr50 = SSC_DEPTH_2M; 741 chip->ssc_depth_sd_ddr50 = SSC_DEPTH_1M; 742 chip->ssc_depth_sd_hs = SSC_DEPTH_1M; 743 chip->ssc_depth_mmc_52m = SSC_DEPTH_1M; 744 chip->ssc_depth_ms_hg = SSC_DEPTH_1M; 745 chip->ssc_depth_ms_4bit = SSC_DEPTH_512K; 746 chip->ssc_depth_low_speed = SSC_DEPTH_512K; 747 chip->ssc_en = 1; 748 chip->sd_speed_prior = 0x01040203; 749 chip->sd_current_prior = 0x00010203; 750 chip->sd_ctl = SD_PUSH_POINT_AUTO | 751 SD_SAMPLE_POINT_AUTO | 752 SUPPORT_MMC_DDR_MODE; 753 chip->sd_ddr_tx_phase = 0; 754 chip->mmc_ddr_tx_phase = 1; 755 chip->sd_default_tx_phase = 15; 756 chip->sd_default_rx_phase = 15; 757 chip->pmos_pwr_on_interval = 200; 758 chip->sd_voltage_switch_delay = 1000; 759 chip->ms_power_class_en = 3; 760 761 chip->sd_400mA_ocp_thd = 1; 762 chip->sd_800mA_ocp_thd = 5; 763 chip->ms_ocp_thd = 2; 764 765 chip->card_drive_sel = 0x55; 766 chip->sd30_drive_sel_1v8 = 0x03; 767 chip->sd30_drive_sel_3v3 = 0x01; 768 769 chip->do_delink_before_power_down = 1; 770 chip->auto_power_down = 1; 771 chip->polling_config = 0; 772 773 chip->force_clkreq_0 = 1; 774 chip->ft2_fast_mode = 0; 775 776 chip->sdio_retry_cnt = 1; 777 778 chip->xd_timeout = 2000; 779 chip->sd_timeout = 10000; 780 chip->ms_timeout = 2000; 781 chip->mspro_timeout = 15000; 782 783 chip->power_down_in_ss = 1; 784 785 chip->sdr104_en = 1; 786 chip->sdr50_en = 1; 787 chip->ddr50_en = 1; 788 789 chip->delink_stage1_step = 100; 790 chip->delink_stage2_step = 40; 791 chip->delink_stage3_step = 20; 792 793 chip->auto_delink_in_L1 = 1; 794 chip->blink_led = 1; 795 chip->msi_en = msi_en; 796 chip->hp_watch_bios_hotplug = 0; 797 chip->max_payload = 0; 798 chip->phy_voltage = 0; 799 800 chip->support_ms_8bit = 1; 801 chip->s3_pwr_off_delay = 1000; 802 } 803 804 static int rtsx_probe(struct pci_dev *pci, 805 const struct pci_device_id *pci_id) 806 { 807 struct Scsi_Host *host; 808 struct rtsx_dev *dev; 809 int err = 0; 810 struct task_struct *th; 811 812 dev_dbg(&pci->dev, "Realtek PCI-E card reader detected\n"); 813 814 err = pcim_enable_device(pci); 815 if (err < 0) { 816 dev_err(&pci->dev, "PCI enable device failed!\n"); 817 return err; 818 } 819 820 err = pci_request_regions(pci, CR_DRIVER_NAME); 821 if (err < 0) { 822 dev_err(&pci->dev, "PCI request regions for %s failed!\n", 823 CR_DRIVER_NAME); 824 return err; 825 } 826 827 /* 828 * Ask the SCSI layer to allocate a host structure, with extra 829 * space at the end for our private rtsx_dev structure. 830 */ 831 host = scsi_host_alloc(&rtsx_host_template, sizeof(*dev)); 832 if (!host) { 833 dev_err(&pci->dev, "Unable to allocate the scsi host\n"); 834 err = -ENOMEM; 835 goto scsi_host_alloc_fail; 836 } 837 838 dev = host_to_rtsx(host); 839 memset(dev, 0, sizeof(struct rtsx_dev)); 840 841 dev->chip = kzalloc(sizeof(*dev->chip), GFP_KERNEL); 842 if (!dev->chip) { 843 err = -ENOMEM; 844 goto chip_alloc_fail; 845 } 846 847 spin_lock_init(&dev->reg_lock); 848 mutex_init(&dev->dev_mutex); 849 init_completion(&dev->cmnd_ready); 850 init_completion(&dev->control_exit); 851 init_completion(&dev->polling_exit); 852 init_completion(&dev->notify); 853 init_completion(&dev->scanning_done); 854 init_waitqueue_head(&dev->delay_wait); 855 856 dev->pci = pci; 857 dev->irq = -1; 858 859 dev_info(&pci->dev, "Resource length: 0x%x\n", 860 (unsigned int)pci_resource_len(pci, 0)); 861 dev->addr = pci_resource_start(pci, 0); 862 dev->remap_addr = ioremap(dev->addr, pci_resource_len(pci, 0)); 863 if (!dev->remap_addr) { 864 dev_err(&pci->dev, "ioremap error\n"); 865 err = -ENXIO; 866 goto ioremap_fail; 867 } 868 869 /* 870 * Using "unsigned long" cast here to eliminate gcc warning in 871 * 64-bit system 872 */ 873 dev_info(&pci->dev, "Original address: 0x%lx, remapped address: 0x%lx\n", 874 (unsigned long)(dev->addr), (unsigned long)(dev->remap_addr)); 875 876 dev->rtsx_resv_buf = dmam_alloc_coherent(&pci->dev, RTSX_RESV_BUF_LEN, 877 &dev->rtsx_resv_buf_addr, 878 GFP_KERNEL); 879 if (!dev->rtsx_resv_buf) { 880 dev_err(&pci->dev, "alloc dma buffer fail\n"); 881 err = -ENXIO; 882 goto dma_alloc_fail; 883 } 884 dev->chip->host_cmds_ptr = dev->rtsx_resv_buf; 885 dev->chip->host_cmds_addr = dev->rtsx_resv_buf_addr; 886 dev->chip->host_sg_tbl_ptr = dev->rtsx_resv_buf + HOST_CMDS_BUF_LEN; 887 dev->chip->host_sg_tbl_addr = dev->rtsx_resv_buf_addr + 888 HOST_CMDS_BUF_LEN; 889 890 dev->chip->rtsx = dev; 891 892 rtsx_init_options(dev->chip); 893 894 dev_info(&pci->dev, "pci->irq = %d\n", pci->irq); 895 896 if (dev->chip->msi_en) { 897 if (pci_enable_msi(pci) < 0) 898 dev->chip->msi_en = 0; 899 } 900 901 if (rtsx_acquire_irq(dev) < 0) { 902 err = -EBUSY; 903 goto irq_acquire_fail; 904 } 905 906 pci_set_master(pci); 907 synchronize_irq(dev->irq); 908 909 rtsx_init_chip(dev->chip); 910 911 /* 912 * set the supported max_lun and max_id for the scsi host 913 * NOTE: the minimal value of max_id is 1 914 */ 915 host->max_id = 1; 916 host->max_lun = dev->chip->max_lun; 917 918 /* Start up our control thread */ 919 th = kthread_run(rtsx_control_thread, dev, CR_DRIVER_NAME); 920 if (IS_ERR(th)) { 921 dev_err(&pci->dev, "Unable to start control thread\n"); 922 err = PTR_ERR(th); 923 goto control_thread_fail; 924 } 925 dev->ctl_thread = th; 926 927 err = scsi_add_host(host, &pci->dev); 928 if (err) { 929 dev_err(&pci->dev, "Unable to add the scsi host\n"); 930 goto scsi_add_host_fail; 931 } 932 933 /* Start up the thread for delayed SCSI-device scanning */ 934 th = kthread_run(rtsx_scan_thread, dev, "rtsx-scan"); 935 if (IS_ERR(th)) { 936 dev_err(&pci->dev, "Unable to start the device-scanning thread\n"); 937 complete(&dev->scanning_done); 938 err = PTR_ERR(th); 939 goto scan_thread_fail; 940 } 941 942 /* Start up the thread for polling thread */ 943 th = kthread_run(rtsx_polling_thread, dev, "rtsx-polling"); 944 if (IS_ERR(th)) { 945 dev_err(&pci->dev, "Unable to start the device-polling thread\n"); 946 err = PTR_ERR(th); 947 goto scan_thread_fail; 948 } 949 dev->polling_thread = th; 950 951 pci_set_drvdata(pci, dev); 952 953 return 0; 954 955 /* We come here if there are any problems */ 956 scan_thread_fail: 957 quiesce_and_remove_host(dev); 958 scsi_add_host_fail: 959 complete(&dev->cmnd_ready); 960 wait_for_completion(&dev->control_exit); 961 control_thread_fail: 962 free_irq(dev->irq, (void *)dev); 963 rtsx_release_chip(dev->chip); 964 irq_acquire_fail: 965 dev->chip->host_cmds_ptr = NULL; 966 dev->chip->host_sg_tbl_ptr = NULL; 967 if (dev->chip->msi_en) 968 pci_disable_msi(dev->pci); 969 dma_alloc_fail: 970 iounmap(dev->remap_addr); 971 ioremap_fail: 972 kfree(dev->chip); 973 chip_alloc_fail: 974 dev_err(&pci->dev, "%s failed\n", __func__); 975 scsi_host_alloc_fail: 976 pci_release_regions(pci); 977 return err; 978 } 979 980 static void rtsx_remove(struct pci_dev *pci) 981 { 982 struct rtsx_dev *dev = pci_get_drvdata(pci); 983 984 dev_info(&pci->dev, "%s called\n", __func__); 985 986 quiesce_and_remove_host(dev); 987 release_everything(dev); 988 pci_release_regions(pci); 989 } 990 991 /* PCI IDs */ 992 static const struct pci_device_id rtsx_ids[] = { 993 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5208), 994 PCI_CLASS_OTHERS << 16, 0xFF0000 }, 995 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5288), 996 PCI_CLASS_OTHERS << 16, 0xFF0000 }, 997 { 0, }, 998 }; 999 1000 MODULE_DEVICE_TABLE(pci, rtsx_ids); 1001 1002 /* pci_driver definition */ 1003 static struct pci_driver rtsx_driver = { 1004 .name = CR_DRIVER_NAME, 1005 .id_table = rtsx_ids, 1006 .probe = rtsx_probe, 1007 .remove = rtsx_remove, 1008 #ifdef CONFIG_PM 1009 .suspend = rtsx_suspend, 1010 .resume = rtsx_resume, 1011 #endif 1012 .shutdown = rtsx_shutdown, 1013 }; 1014 1015 module_pci_driver(rtsx_driver); 1016