1 /* Driver for Realtek PCI-Express card reader
3 * Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2, or (at your option) any
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses/>.
19 * wwang (wei_wang@realsil.com.cn)
20 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
23 #include <linux/blkdev.h>
24 #include <linux/kthread.h>
25 #include <linux/sched.h>
26 #include <linux/workqueue.h>
29 #include "rtsx_chip.h"
30 #include "rtsx_transport.h"
31 #include "rtsx_scsi.h"
32 #include "rtsx_card.h"
39 #define DRIVER_VERSION "v1.10"
41 MODULE_DESCRIPTION("Realtek PCI-Express card reader driver");
42 MODULE_LICENSE("GPL");
43 MODULE_VERSION(DRIVER_VERSION);
45 static unsigned int delay_use = 1;
46 module_param(delay_use, uint, S_IRUGO | S_IWUSR);
47 MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
50 module_param(ss_en, int, S_IRUGO | S_IWUSR);
51 MODULE_PARM_DESC(ss_en, "enable selective suspend");
53 static int ss_interval = 50;
54 module_param(ss_interval, int, S_IRUGO | S_IWUSR);
55 MODULE_PARM_DESC(ss_interval, "Interval to enter ss state in seconds");
57 static int auto_delink_en;
58 module_param(auto_delink_en, int, S_IRUGO | S_IWUSR);
59 MODULE_PARM_DESC(auto_delink_en, "enable auto delink");
61 static unsigned char aspm_l0s_l1_en;
62 module_param(aspm_l0s_l1_en, byte, S_IRUGO | S_IWUSR);
63 MODULE_PARM_DESC(aspm_l0s_l1_en, "enable device aspm");
66 module_param(msi_en, int, S_IRUGO | S_IWUSR);
67 MODULE_PARM_DESC(msi_en, "enable msi");
69 static irqreturn_t rtsx_interrupt(int irq, void *dev_id);
71 /***********************************************************************
73 ***********************************************************************/
75 static const char *host_info(struct Scsi_Host *host)
77 return "SCSI emulation for PCI-Express Mass Storage devices";
80 static int slave_alloc (struct scsi_device *sdev)
83 * Set the INQUIRY transfer length to 36. We don't use any of
84 * the extra data and many devices choke if asked for more or
87 sdev->inquiry_len = 36;
91 static int slave_configure(struct scsi_device *sdev)
93 /* Scatter-gather buffers (all but the last) must have a length
94 * divisible by the bulk maxpacket size. Otherwise a data packet
95 * would end up being short, causing a premature end to the data
96 * transfer. Since high-speed bulk pipes have a maxpacket size
97 * of 512, we'll use that as the scsi device queue's DMA alignment
98 * mask. Guaranteeing proper alignment of the first buffer will
99 * have the desired effect because, except at the beginning and
100 * the end, scatter-gather buffers follow page boundaries. */
101 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
103 /* Set the SCSI level to at least 2. We'll leave it at 3 if that's
104 * what is originally reported. We need this to avoid confusing
105 * the SCSI layer with devices that report 0 or 1, but need 10-byte
106 * commands (ala ATAPI devices behind certain bridges, or devices
107 * which simply have broken INQUIRY data).
109 * NOTE: This means /dev/sg programs (ala cdrecord) will get the
110 * actual information. This seems to be the preference for
111 * programs like that.
113 * NOTE: This also means that /proc/scsi/scsi and sysfs may report
114 * the actual value or the modified one, depending on where the
117 if (sdev->scsi_level < SCSI_2)
118 sdev->scsi_level = sdev->sdev_target->scsi_level = SCSI_2;
124 /***********************************************************************
125 * /proc/scsi/ functions
126 ***********************************************************************/
128 /* we use this macro to help us write into the buffer */
130 #define SPRINTF(args...) \
131 do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
133 static int proc_info (struct Scsi_Host *host, char *buffer,
134 char **start, off_t offset, int length, int inout)
138 /* if someone is sending us data, just throw it away */
142 /* print the controller name */
143 SPRINTF(" Host scsi%d: %s\n", host->host_no, CR_DRIVER_NAME);
145 /* print product, vendor, and driver version strings */
146 SPRINTF(" Vendor: Realtek Corp.\n");
147 SPRINTF(" Product: PCIE Card Reader\n");
148 SPRINTF(" Version: %s\n", DRIVER_VERSION);
151 * Calculate start of next buffer, and return value.
153 *start = buffer + offset;
155 if ((pos - buffer) < offset)
157 else if ((pos - buffer - offset) < length)
158 return pos - buffer - offset;
163 /* queue a command */
164 /* This is always called with scsi_lock(host) held */
165 static int queuecommand_lck(struct scsi_cmnd *srb,
166 void (*done)(struct scsi_cmnd *))
168 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
169 struct rtsx_chip *chip = dev->chip;
171 /* check for state-transition errors */
172 if (chip->srb != NULL) {
173 printk(KERN_ERR "Error in %s: chip->srb = %p\n",
174 __func__, chip->srb);
175 return SCSI_MLQUEUE_HOST_BUSY;
178 /* fail the command if we are disconnecting */
179 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
180 printk(KERN_INFO "Fail command during disconnect\n");
181 srb->result = DID_NO_CONNECT << 16;
186 /* enqueue the command and wake up the control thread */
187 srb->scsi_done = done;
189 complete(&dev->cmnd_ready);
194 static DEF_SCSI_QCMD(queuecommand)
196 /***********************************************************************
197 * Error handling functions
198 ***********************************************************************/
200 /* Command timeout and abort */
201 static int command_abort(struct scsi_cmnd *srb)
203 struct Scsi_Host *host = srb->device->host;
204 struct rtsx_dev *dev = host_to_rtsx(host);
205 struct rtsx_chip *chip = dev->chip;
207 printk(KERN_INFO "%s called\n", __func__);
211 /* Is this command still active? */
212 if (chip->srb != srb) {
214 printk(KERN_INFO "-- nothing to abort\n");
218 rtsx_set_stat(chip, RTSX_STAT_ABORT);
222 /* Wait for the aborted command to finish */
223 wait_for_completion(&dev->notify);
228 /* This invokes the transport reset mechanism to reset the state of the
230 static int device_reset(struct scsi_cmnd *srb)
234 printk(KERN_INFO "%s called\n", __func__);
236 return result < 0 ? FAILED : SUCCESS;
239 /* Simulate a SCSI bus reset by resetting the device's USB port. */
240 static int bus_reset(struct scsi_cmnd *srb)
244 printk(KERN_INFO "%s called\n", __func__);
246 return result < 0 ? FAILED : SUCCESS;
251 * this defines our host template, with which we'll allocate hosts
254 static struct scsi_host_template rtsx_host_template = {
255 /* basic userland interface stuff */
256 .name = CR_DRIVER_NAME,
257 .proc_name = CR_DRIVER_NAME,
258 .proc_info = proc_info,
261 /* command interface -- queued only */
262 .queuecommand = queuecommand,
264 /* error and abort handlers */
265 .eh_abort_handler = command_abort,
266 .eh_device_reset_handler = device_reset,
267 .eh_bus_reset_handler = bus_reset,
269 /* queue commands only, only one command per LUN */
273 /* unknown initiator id */
276 .slave_alloc = slave_alloc,
277 .slave_configure = slave_configure,
279 /* lots of sg segments can be handled */
280 .sg_tablesize = SG_ALL,
282 /* limit the total size of a transfer to 120 KB */
285 /* merge commands... this seems to help performance, but
286 * periodically someone should test to see which setting is more
294 /* we do our own delay after a device or bus reset */
295 .skip_settle_delay = 1,
297 /* module management */
298 .module = THIS_MODULE
302 static int rtsx_acquire_irq(struct rtsx_dev *dev)
304 struct rtsx_chip *chip = dev->chip;
306 printk(KERN_INFO "%s: chip->msi_en = %d, pci->irq = %d\n",
307 __func__, chip->msi_en, dev->pci->irq);
309 if (request_irq(dev->pci->irq, rtsx_interrupt,
310 chip->msi_en ? 0 : IRQF_SHARED,
311 CR_DRIVER_NAME, dev)) {
312 printk(KERN_ERR "rtsx: unable to grab IRQ %d, "
313 "disabling device\n", dev->pci->irq);
317 dev->irq = dev->pci->irq;
318 pci_intx(dev->pci, !chip->msi_en);
324 int rtsx_read_pci_cfg_byte(u8 bus, u8 dev, u8 func, u8 offset, u8 *val)
326 struct pci_dev *pdev;
328 u8 devfn = (dev << 3) | func;
330 pdev = pci_get_bus_and_slot(bus, devfn);
334 pci_read_config_byte(pdev, offset, &data);
345 static int rtsx_suspend(struct pci_dev *pci, pm_message_t state)
347 struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
348 struct rtsx_chip *chip;
350 printk(KERN_INFO "Ready to suspend\n");
353 printk(KERN_ERR "Invalid memory\n");
357 /* lock the device pointers */
358 mutex_lock(&(dev->dev_mutex));
362 rtsx_do_before_power_down(chip, PM_S3);
365 synchronize_irq(dev->irq);
366 free_irq(dev->irq, (void *)dev);
371 pci_disable_msi(pci);
374 pci_enable_wake(pci, pci_choose_state(pci, state), 1);
375 pci_disable_device(pci);
376 pci_set_power_state(pci, pci_choose_state(pci, state));
378 /* unlock the device pointers */
379 mutex_unlock(&dev->dev_mutex);
384 static int rtsx_resume(struct pci_dev *pci)
386 struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
387 struct rtsx_chip *chip;
389 printk(KERN_INFO "Ready to resume\n");
392 printk(KERN_ERR "Invalid memory\n");
398 /* lock the device pointers */
399 mutex_lock(&(dev->dev_mutex));
401 pci_set_power_state(pci, PCI_D0);
402 pci_restore_state(pci);
403 if (pci_enable_device(pci) < 0) {
404 printk(KERN_ERR "%s: pci_enable_device failed, "
405 "disabling device\n", CR_DRIVER_NAME);
406 /* unlock the device pointers */
407 mutex_unlock(&dev->dev_mutex);
413 if (pci_enable_msi(pci) < 0)
417 if (rtsx_acquire_irq(dev) < 0) {
418 /* unlock the device pointers */
419 mutex_unlock(&dev->dev_mutex);
423 rtsx_write_register(chip, HOST_SLEEP_STATE, 0x03, 0x00);
424 rtsx_init_chip(chip);
426 /* unlock the device pointers */
427 mutex_unlock(&dev->dev_mutex);
431 #endif /* CONFIG_PM */
433 static void rtsx_shutdown(struct pci_dev *pci)
435 struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
436 struct rtsx_chip *chip;
438 printk(KERN_INFO "Ready to shutdown\n");
441 printk(KERN_ERR "Invalid memory\n");
447 rtsx_do_before_power_down(chip, PM_S1);
450 synchronize_irq(dev->irq);
451 free_irq(dev->irq, (void *)dev);
456 pci_disable_msi(pci);
458 pci_disable_device(pci);
463 static int rtsx_control_thread(void *__dev)
465 struct rtsx_dev *dev = (struct rtsx_dev *)__dev;
466 struct rtsx_chip *chip = dev->chip;
467 struct Scsi_Host *host = rtsx_to_host(dev);
469 current->flags |= PF_NOFREEZE;
472 if (wait_for_completion_interruptible(&dev->cmnd_ready))
475 /* lock the device pointers */
476 mutex_lock(&(dev->dev_mutex));
478 /* if the device has disconnected, we are free to exit */
479 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
480 printk(KERN_INFO "-- rtsx-control exiting\n");
481 mutex_unlock(&dev->dev_mutex);
485 /* lock access to the state */
488 /* has the command aborted ? */
489 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
490 chip->srb->result = DID_ABORT << 16;
496 /* reject the command if the direction indicator
499 if (chip->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
500 printk(KERN_ERR "UNKNOWN data direction\n");
501 chip->srb->result = DID_ERROR << 16;
504 /* reject if target != 0 or if LUN is higher than
505 * the maximum known LUN
507 else if (chip->srb->device->id) {
508 printk(KERN_ERR "Bad target number (%d:%d)\n",
509 chip->srb->device->id, chip->srb->device->lun);
510 chip->srb->result = DID_BAD_TARGET << 16;
513 else if (chip->srb->device->lun > chip->max_lun) {
514 printk(KERN_ERR "Bad LUN (%d:%d)\n",
515 chip->srb->device->id, chip->srb->device->lun);
516 chip->srb->result = DID_BAD_TARGET << 16;
519 /* we've got a command, let's do it! */
521 RTSX_DEBUG(scsi_show_command(chip->srb));
522 rtsx_invoke_transport(chip->srb, chip);
525 /* lock access to the state */
528 /* did the command already complete because of a disconnect? */
530 ; /* nothing to do */
532 /* indicate that the command is done */
533 else if (chip->srb->result != DID_ABORT << 16) {
534 chip->srb->scsi_done(chip->srb);
537 printk(KERN_ERR "scsi command aborted\n");
540 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
541 complete(&(dev->notify));
543 rtsx_set_stat(chip, RTSX_STAT_IDLE);
546 /* finished working on this command */
550 /* unlock the device pointers */
551 mutex_unlock(&dev->dev_mutex);
554 /* notify the exit routine that we're actually exiting now
556 * complete()/wait_for_completion() is similar to up()/down(),
557 * except that complete() is safe in the case where the structure
558 * is getting deleted in a parallel mode of execution (i.e. just
559 * after the down() -- that's necessary for the thread-shutdown
562 * complete_and_exit() goes even further than this -- it is safe in
563 * the case that the thread of the caller is going away (not just
564 * the structure) -- this is necessary for the module-remove case.
565 * This is important in preemption kernels, which transfer the flow
566 * of execution immediately upon a complete().
568 complete_and_exit(&dev->control_exit, 0);
572 static int rtsx_polling_thread(void *__dev)
574 struct rtsx_dev *dev = (struct rtsx_dev *)__dev;
575 struct rtsx_chip *chip = dev->chip;
576 struct sd_info *sd_card = &(chip->sd_card);
577 struct xd_info *xd_card = &(chip->xd_card);
578 struct ms_info *ms_card = &(chip->ms_card);
580 sd_card->cleanup_counter = 0;
581 xd_card->cleanup_counter = 0;
582 ms_card->cleanup_counter = 0;
584 /* Wait until SCSI scan finished */
585 wait_timeout((delay_use + 5) * 1000);
589 set_current_state(TASK_INTERRUPTIBLE);
590 schedule_timeout(POLLING_INTERVAL);
592 /* lock the device pointers */
593 mutex_lock(&(dev->dev_mutex));
595 /* if the device has disconnected, we are free to exit */
596 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
597 printk(KERN_INFO "-- rtsx-polling exiting\n");
598 mutex_unlock(&dev->dev_mutex);
602 mutex_unlock(&dev->dev_mutex);
604 mspro_polling_format_status(chip);
606 /* lock the device pointers */
607 mutex_lock(&(dev->dev_mutex));
609 rtsx_polling_func(chip);
611 /* unlock the device pointers */
612 mutex_unlock(&dev->dev_mutex);
615 complete_and_exit(&dev->polling_exit, 0);
621 static irqreturn_t rtsx_interrupt(int irq, void *dev_id)
623 struct rtsx_dev *dev = dev_id;
624 struct rtsx_chip *chip;
638 spin_lock(&dev->reg_lock);
640 retval = rtsx_pre_handle_interrupt(chip);
641 if (retval == STATUS_FAIL) {
642 spin_unlock(&dev->reg_lock);
643 if (chip->int_reg == 0xFFFFFFFF) {
650 status = chip->int_reg;
652 if (dev->check_card_cd) {
653 if (!(dev->check_card_cd & status)) {
654 /* card not exist, return TRANS_RESULT_FAIL */
655 dev->trans_result = TRANS_RESULT_FAIL;
662 if (status & (NEED_COMPLETE_INT | DELINK_INT)) {
663 if (status & (TRANS_FAIL_INT | DELINK_INT)) {
664 if (status & DELINK_INT) {
665 RTSX_SET_DELINK(chip);
667 dev->trans_result = TRANS_RESULT_FAIL;
670 } else if (status & TRANS_OK_INT) {
671 dev->trans_result = TRANS_RESULT_OK;
674 } else if (status & DATA_DONE_INT) {
675 dev->trans_result = TRANS_NOT_READY;
676 if (dev->done && (dev->trans_state == STATE_TRANS_SG))
682 spin_unlock(&dev->reg_lock);
687 /* Release all our dynamic resources */
688 static void rtsx_release_resources(struct rtsx_dev *dev)
690 printk(KERN_INFO "-- %s\n", __func__);
692 /* Tell the control thread to exit. The SCSI host must
693 * already have been removed so it won't try to queue
696 printk(KERN_INFO "-- sending exit command to thread\n");
697 complete(&dev->cmnd_ready);
699 wait_for_completion(&dev->control_exit);
700 if (dev->polling_thread)
701 wait_for_completion(&dev->polling_exit);
705 if (dev->rtsx_resv_buf) {
706 dma_free_coherent(&(dev->pci->dev), RTSX_RESV_BUF_LEN,
707 dev->rtsx_resv_buf, dev->rtsx_resv_buf_addr);
708 dev->chip->host_cmds_ptr = NULL;
709 dev->chip->host_sg_tbl_ptr = NULL;
713 free_irq(dev->irq, (void *)dev);
714 if (dev->chip->msi_en)
715 pci_disable_msi(dev->pci);
717 iounmap(dev->remap_addr);
719 pci_disable_device(dev->pci);
720 pci_release_regions(dev->pci);
722 rtsx_release_chip(dev->chip);
726 /* First stage of disconnect processing: stop all commands and remove
728 static void quiesce_and_remove_host(struct rtsx_dev *dev)
730 struct Scsi_Host *host = rtsx_to_host(dev);
731 struct rtsx_chip *chip = dev->chip;
733 /* Prevent new transfers, stop the current command, and
734 * interrupt a SCSI-scan or device-reset delay */
735 mutex_lock(&dev->dev_mutex);
737 rtsx_set_stat(chip, RTSX_STAT_DISCONNECT);
739 mutex_unlock(&dev->dev_mutex);
740 wake_up(&dev->delay_wait);
741 wait_for_completion(&dev->scanning_done);
743 /* Wait some time to let other threads exist */
746 /* queuecommand won't accept any new commands and the control
747 * thread won't execute a previously-queued command. If there
748 * is such a command pending, complete it with an error. */
749 mutex_lock(&dev->dev_mutex);
751 chip->srb->result = DID_NO_CONNECT << 16;
753 chip->srb->scsi_done(dev->chip->srb);
757 mutex_unlock(&dev->dev_mutex);
759 /* Now we own no commands so it's safe to remove the SCSI host */
760 scsi_remove_host(host);
763 /* Second stage of disconnect processing: deallocate all resources */
764 static void release_everything(struct rtsx_dev *dev)
766 rtsx_release_resources(dev);
768 /* Drop our reference to the host; the SCSI core will free it
769 * when the refcount becomes 0. */
770 scsi_host_put(rtsx_to_host(dev));
773 /* Thread to carry out delayed SCSI-device scanning */
774 static int rtsx_scan_thread(void *__dev)
776 struct rtsx_dev *dev = (struct rtsx_dev *)__dev;
777 struct rtsx_chip *chip = dev->chip;
779 /* Wait for the timeout to expire or for a disconnect */
781 printk(KERN_INFO "%s: waiting for device "
782 "to settle before scanning\n", CR_DRIVER_NAME);
783 wait_event_interruptible_timeout(dev->delay_wait,
784 rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT),
788 /* If the device is still connected, perform the scanning */
789 if (!rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
790 scsi_scan_host(rtsx_to_host(dev));
791 printk(KERN_INFO "%s: device scan complete\n", CR_DRIVER_NAME);
793 /* Should we unbind if no devices were detected? */
796 complete_and_exit(&dev->scanning_done, 0);
799 static void rtsx_init_options(struct rtsx_chip *chip)
801 chip->vendor_id = chip->rtsx->pci->vendor;
802 chip->product_id = chip->rtsx->pci->device;
805 chip->driver_first_load = 1;
806 #ifdef HW_AUTO_SWITCH_SD_BUS
807 chip->sdio_in_charge = 0;
810 chip->mspro_formatter_enable = 1;
812 chip->use_hw_setting = 0;
813 chip->lun_mode = DEFAULT_SINGLE;
814 chip->auto_delink_en = auto_delink_en;
816 chip->ss_idle_period = ss_interval * 1000;
817 chip->remote_wakeup_en = 0;
818 chip->aspm_l0s_l1_en = aspm_l0s_l1_en;
819 chip->dynamic_aspm = 1;
820 chip->fpga_sd_sdr104_clk = CLK_200;
821 chip->fpga_sd_ddr50_clk = CLK_100;
822 chip->fpga_sd_sdr50_clk = CLK_100;
823 chip->fpga_sd_hs_clk = CLK_100;
824 chip->fpga_mmc_52m_clk = CLK_80;
825 chip->fpga_ms_hg_clk = CLK_80;
826 chip->fpga_ms_4bit_clk = CLK_80;
827 chip->fpga_ms_1bit_clk = CLK_40;
828 chip->asic_sd_sdr104_clk = 203;
829 chip->asic_sd_sdr50_clk = 98;
830 chip->asic_sd_ddr50_clk = 98;
831 chip->asic_sd_hs_clk = 98;
832 chip->asic_mmc_52m_clk = 98;
833 chip->asic_ms_hg_clk = 117;
834 chip->asic_ms_4bit_clk = 78;
835 chip->asic_ms_1bit_clk = 39;
836 chip->ssc_depth_sd_sdr104 = SSC_DEPTH_2M;
837 chip->ssc_depth_sd_sdr50 = SSC_DEPTH_2M;
838 chip->ssc_depth_sd_ddr50 = SSC_DEPTH_1M;
839 chip->ssc_depth_sd_hs = SSC_DEPTH_1M;
840 chip->ssc_depth_mmc_52m = SSC_DEPTH_1M;
841 chip->ssc_depth_ms_hg = SSC_DEPTH_1M;
842 chip->ssc_depth_ms_4bit = SSC_DEPTH_512K;
843 chip->ssc_depth_low_speed = SSC_DEPTH_512K;
845 chip->sd_speed_prior = 0x01040203;
846 chip->sd_current_prior = 0x00010203;
847 chip->sd_ctl = SD_PUSH_POINT_AUTO | SD_SAMPLE_POINT_AUTO | SUPPORT_MMC_DDR_MODE;
848 chip->sd_ddr_tx_phase = 0;
849 chip->mmc_ddr_tx_phase = 1;
850 chip->sd_default_tx_phase = 15;
851 chip->sd_default_rx_phase = 15;
852 chip->pmos_pwr_on_interval = 200;
853 chip->sd_voltage_switch_delay = 1000;
854 chip->ms_power_class_en = 3;
856 chip->sd_400mA_ocp_thd = 1;
857 chip->sd_800mA_ocp_thd = 5;
858 chip->ms_ocp_thd = 2;
860 chip->card_drive_sel = 0x55;
861 chip->sd30_drive_sel_1v8 = 0x03;
862 chip->sd30_drive_sel_3v3 = 0x01;
864 chip->do_delink_before_power_down = 1;
865 chip->auto_power_down = 1;
866 chip->polling_config = 0;
868 chip->force_clkreq_0 = 1;
869 chip->ft2_fast_mode = 0;
871 chip->sdio_retry_cnt = 1;
873 chip->xd_timeout = 2000;
874 chip->sd_timeout = 10000;
875 chip->ms_timeout = 2000;
876 chip->mspro_timeout = 15000;
878 chip->power_down_in_ss = 1;
884 chip->delink_stage1_step = 100;
885 chip->delink_stage2_step = 40;
886 chip->delink_stage3_step = 20;
888 chip->auto_delink_in_L1 = 1;
890 chip->msi_en = msi_en;
891 chip->hp_watch_bios_hotplug = 0;
892 chip->max_payload = 0;
893 chip->phy_voltage = 0;
895 chip->support_ms_8bit = 1;
896 chip->s3_pwr_off_delay = 1000;
899 static int __devinit rtsx_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
901 struct Scsi_Host *host;
902 struct rtsx_dev *dev;
904 struct task_struct *th;
906 RTSX_DEBUGP("Realtek PCI-E card reader detected\n");
908 err = pci_enable_device(pci);
910 printk(KERN_ERR "PCI enable device failed!\n");
914 err = pci_request_regions(pci, CR_DRIVER_NAME);
916 printk(KERN_ERR "PCI request regions for %s failed!\n", CR_DRIVER_NAME);
917 pci_disable_device(pci);
922 * Ask the SCSI layer to allocate a host structure, with extra
923 * space at the end for our private rtsx_dev structure.
925 host = scsi_host_alloc(&rtsx_host_template, sizeof(*dev));
927 printk(KERN_ERR "Unable to allocate the scsi host\n");
928 pci_release_regions(pci);
929 pci_disable_device(pci);
933 dev = host_to_rtsx(host);
934 memset(dev, 0, sizeof(struct rtsx_dev));
936 dev->chip = kzalloc(sizeof(struct rtsx_chip), GFP_KERNEL);
937 if (dev->chip == NULL) {
941 spin_lock_init(&dev->reg_lock);
942 mutex_init(&(dev->dev_mutex));
943 init_completion(&dev->cmnd_ready);
944 init_completion(&dev->control_exit);
945 init_completion(&dev->polling_exit);
946 init_completion(&(dev->notify));
947 init_completion(&dev->scanning_done);
948 init_waitqueue_head(&dev->delay_wait);
953 printk(KERN_INFO "Resource length: 0x%x\n", (unsigned int)pci_resource_len(pci, 0));
954 dev->addr = pci_resource_start(pci, 0);
955 dev->remap_addr = ioremap_nocache(dev->addr, pci_resource_len(pci, 0));
956 if (dev->remap_addr == NULL) {
957 printk(KERN_ERR "ioremap error\n");
962 /* Using "unsigned long" cast here to eliminate gcc warning in 64-bit system */
963 printk(KERN_INFO "Original address: 0x%lx, remapped address: 0x%lx\n",
964 (unsigned long)(dev->addr), (unsigned long)(dev->remap_addr));
966 dev->rtsx_resv_buf = dma_alloc_coherent(&(pci->dev), RTSX_RESV_BUF_LEN,
967 &(dev->rtsx_resv_buf_addr), GFP_KERNEL);
968 if (dev->rtsx_resv_buf == NULL) {
969 printk(KERN_ERR "alloc dma buffer fail\n");
973 dev->chip->host_cmds_ptr = dev->rtsx_resv_buf;
974 dev->chip->host_cmds_addr = dev->rtsx_resv_buf_addr;
975 dev->chip->host_sg_tbl_ptr = dev->rtsx_resv_buf + HOST_CMDS_BUF_LEN;
976 dev->chip->host_sg_tbl_addr = dev->rtsx_resv_buf_addr + HOST_CMDS_BUF_LEN;
978 dev->chip->rtsx = dev;
980 rtsx_init_options(dev->chip);
982 printk(KERN_INFO "pci->irq = %d\n", pci->irq);
984 if (dev->chip->msi_en) {
985 if (pci_enable_msi(pci) < 0)
986 dev->chip->msi_en = 0;
989 if (rtsx_acquire_irq(dev) < 0) {
995 synchronize_irq(dev->irq);
997 rtsx_init_chip(dev->chip);
999 /* Start up our control thread */
1000 th = kthread_run(rtsx_control_thread, dev, CR_DRIVER_NAME);
1002 printk(KERN_ERR "Unable to start control thread\n");
1006 dev->ctl_thread = th;
1008 err = scsi_add_host(host, &pci->dev);
1010 printk(KERN_ERR "Unable to add the scsi host\n");
1014 /* Start up the thread for delayed SCSI-device scanning */
1015 th = kthread_create(rtsx_scan_thread, dev, "rtsx-scan");
1017 printk(KERN_ERR "Unable to start the device-scanning thread\n");
1018 quiesce_and_remove_host(dev);
1023 wake_up_process(th);
1025 /* Start up the thread for polling thread */
1026 th = kthread_run(rtsx_polling_thread, dev, "rtsx-polling");
1028 printk(KERN_ERR "Unable to start the device-polling thread\n");
1029 quiesce_and_remove_host(dev);
1033 dev->polling_thread = th;
1035 pci_set_drvdata(pci, dev);
1039 /* We come here if there are any problems */
1041 printk(KERN_ERR "rtsx_probe() failed\n");
1042 release_everything(dev);
1048 static void __devexit rtsx_remove(struct pci_dev *pci)
1050 struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
1052 printk(KERN_INFO "rtsx_remove() called\n");
1054 quiesce_and_remove_host(dev);
1055 release_everything(dev);
1057 pci_set_drvdata(pci, NULL);
1061 static struct pci_device_id rtsx_ids[] = {
1062 { 0x10EC, 0x5208, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_OTHERS << 16, 0xFF0000 },
1063 { 0x10EC, 0x5209, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_OTHERS << 16, 0xFF0000 },
1064 { 0x10EC, 0x5288, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_OTHERS << 16, 0xFF0000 },
1068 MODULE_DEVICE_TABLE(pci, rtsx_ids);
1070 /* pci_driver definition */
1071 static struct pci_driver driver = {
1072 .name = CR_DRIVER_NAME,
1073 .id_table = rtsx_ids,
1074 .probe = rtsx_probe,
1075 .remove = __devexit_p(rtsx_remove),
1077 .suspend = rtsx_suspend,
1078 .resume = rtsx_resume,
1080 .shutdown = rtsx_shutdown,
1083 static int __init rtsx_init(void)
1085 printk(KERN_INFO "Initializing Realtek PCIE storage driver...\n");
1087 return pci_register_driver(&driver);
1090 static void __exit rtsx_exit(void)
1092 printk(KERN_INFO "rtsx_exit() called\n");
1094 pci_unregister_driver(&driver);
1096 printk(KERN_INFO "%s module exit\n", CR_DRIVER_NAME);
1099 module_init(rtsx_init)
1100 module_exit(rtsx_exit)