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