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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/blkdev.h>
26 #include <linux/kthread.h>
27 #include <linux/sched.h>
28 #include <linux/workqueue.h>
31 #include "rtsx_chip.h"
32 #include "rtsx_transport.h"
33 #include "rtsx_scsi.h"
34 #include "rtsx_card.h"
41 #define DRIVER_VERSION "v1.10"
43 MODULE_DESCRIPTION("Realtek PCI-Express card reader driver");
44 MODULE_LICENSE("GPL");
45 MODULE_VERSION(DRIVER_VERSION);
47 static unsigned int delay_use = 1;
48 module_param(delay_use, uint, S_IRUGO | S_IWUSR);
49 MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
52 module_param(ss_en, int, S_IRUGO | S_IWUSR);
53 MODULE_PARM_DESC(ss_en, "enable selective suspend");
55 static int ss_interval = 50;
56 module_param(ss_interval, int, S_IRUGO | S_IWUSR);
57 MODULE_PARM_DESC(ss_interval, "Interval to enter ss state in seconds");
59 static int auto_delink_en;
60 module_param(auto_delink_en, int, S_IRUGO | S_IWUSR);
61 MODULE_PARM_DESC(auto_delink_en, "enable auto delink");
63 static unsigned char aspm_l0s_l1_en;
64 module_param(aspm_l0s_l1_en, byte, S_IRUGO | S_IWUSR);
65 MODULE_PARM_DESC(aspm_l0s_l1_en, "enable device aspm");
68 module_param(msi_en, int, S_IRUGO | S_IWUSR);
69 MODULE_PARM_DESC(msi_en, "enable msi");
71 static irqreturn_t rtsx_interrupt(int irq, void *dev_id);
73 /***********************************************************************
75 ***********************************************************************/
77 static const char *host_info(struct Scsi_Host *host)
79 return "SCSI emulation for PCI-Express Mass Storage devices";
82 static int slave_alloc(struct scsi_device *sdev)
85 * Set the INQUIRY transfer length to 36. We don't use any of
86 * the extra data and many devices choke if asked for more or
89 sdev->inquiry_len = 36;
93 static int slave_configure(struct scsi_device *sdev)
95 /* Scatter-gather buffers (all but the last) must have a length
96 * divisible by the bulk maxpacket size. Otherwise a data packet
97 * would end up being short, causing a premature end to the data
98 * transfer. Since high-speed bulk pipes have a maxpacket size
99 * of 512, we'll use that as the scsi device queue's DMA alignment
100 * mask. Guaranteeing proper alignment of the first buffer will
101 * have the desired effect because, except at the beginning and
102 * the end, scatter-gather buffers follow page boundaries. */
103 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
105 /* Set the SCSI level to at least 2. We'll leave it at 3 if that's
106 * what is originally reported. We need this to avoid confusing
107 * the SCSI layer with devices that report 0 or 1, but need 10-byte
108 * commands (ala ATAPI devices behind certain bridges, or devices
109 * which simply have broken INQUIRY data).
111 * NOTE: This means /dev/sg programs (ala cdrecord) will get the
112 * actual information. This seems to be the preference for
113 * programs like that.
115 * NOTE: This also means that /proc/scsi/scsi and sysfs may report
116 * the actual value or the modified one, depending on where the
119 if (sdev->scsi_level < SCSI_2)
120 sdev->scsi_level = sdev->sdev_target->scsi_level = SCSI_2;
126 /***********************************************************************
127 * /proc/scsi/ functions
128 ***********************************************************************/
130 /* we use this macro to help us write into the buffer */
132 #define SPRINTF(args...) \
133 do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
135 static int proc_info(struct Scsi_Host *host, char *buffer,
136 char **start, off_t offset, int length, int inout)
140 /* if someone is sending us data, just throw it away */
144 /* print the controller name */
145 SPRINTF(" Host scsi%d: %s\n", host->host_no, CR_DRIVER_NAME);
147 /* print product, vendor, and driver version strings */
148 SPRINTF(" Vendor: Realtek Corp.\n");
149 SPRINTF(" Product: PCIE Card Reader\n");
150 SPRINTF(" Version: %s\n", DRIVER_VERSION);
153 * Calculate start of next buffer, and return value.
155 *start = buffer + offset;
157 if ((pos - buffer) < offset)
159 else if ((pos - buffer - offset) < length)
160 return pos - buffer - offset;
165 /* queue a command */
166 /* This is always called with scsi_lock(host) held */
167 static int queuecommand_lck(struct scsi_cmnd *srb,
168 void (*done)(struct scsi_cmnd *))
170 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
171 struct rtsx_chip *chip = dev->chip;
173 /* check for state-transition errors */
174 if (chip->srb != NULL) {
175 dev_err(&dev->pci->dev, "Error in %s: chip->srb = %p\n",
176 __func__, chip->srb);
177 return SCSI_MLQUEUE_HOST_BUSY;
180 /* fail the command if we are disconnecting */
181 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
182 dev_info(&dev->pci->dev, "Fail command during disconnect\n");
183 srb->result = DID_NO_CONNECT << 16;
188 /* enqueue the command and wake up the control thread */
189 srb->scsi_done = done;
191 complete(&dev->cmnd_ready);
196 static DEF_SCSI_QCMD(queuecommand)
198 /***********************************************************************
199 * Error handling functions
200 ***********************************************************************/
202 /* Command timeout and abort */
203 static int command_abort(struct scsi_cmnd *srb)
205 struct Scsi_Host *host = srb->device->host;
206 struct rtsx_dev *dev = host_to_rtsx(host);
207 struct rtsx_chip *chip = dev->chip;
209 dev_info(&dev->pci->dev, "%s called\n", __func__);
213 /* Is this command still active? */
214 if (chip->srb != srb) {
216 dev_info(&dev->pci->dev, "-- nothing to abort\n");
220 rtsx_set_stat(chip, RTSX_STAT_ABORT);
224 /* Wait for the aborted command to finish */
225 wait_for_completion(&dev->notify);
230 /* This invokes the transport reset mechanism to reset the state of the
232 static int device_reset(struct scsi_cmnd *srb)
235 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
237 dev_info(&dev->pci->dev, "%s called\n", __func__);
239 return result < 0 ? FAILED : SUCCESS;
242 /* Simulate a SCSI bus reset by resetting the device's USB port. */
243 static int bus_reset(struct scsi_cmnd *srb)
246 struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
248 dev_info(&dev->pci->dev, "%s called\n", __func__);
250 return result < 0 ? FAILED : SUCCESS;
255 * this defines our host template, with which we'll allocate hosts
258 static struct scsi_host_template rtsx_host_template = {
259 /* basic userland interface stuff */
260 .name = CR_DRIVER_NAME,
261 .proc_name = CR_DRIVER_NAME,
262 .proc_info = proc_info,
265 /* command interface -- queued only */
266 .queuecommand = queuecommand,
268 /* error and abort handlers */
269 .eh_abort_handler = command_abort,
270 .eh_device_reset_handler = device_reset,
271 .eh_bus_reset_handler = bus_reset,
273 /* queue commands only, only one command per LUN */
277 /* unknown initiator id */
280 .slave_alloc = slave_alloc,
281 .slave_configure = slave_configure,
283 /* lots of sg segments can be handled */
284 .sg_tablesize = SG_ALL,
286 /* limit the total size of a transfer to 120 KB */
289 /* merge commands... this seems to help performance, but
290 * periodically someone should test to see which setting is more
298 /* we do our own delay after a device or bus reset */
299 .skip_settle_delay = 1,
301 /* module management */
302 .module = THIS_MODULE
306 static int rtsx_acquire_irq(struct rtsx_dev *dev)
308 struct rtsx_chip *chip = dev->chip;
310 dev_info(&dev->pci->dev, "%s: chip->msi_en = %d, pci->irq = %d\n",
311 __func__, chip->msi_en, dev->pci->irq);
313 if (request_irq(dev->pci->irq, rtsx_interrupt,
314 chip->msi_en ? 0 : IRQF_SHARED,
315 CR_DRIVER_NAME, dev)) {
316 dev_err(&dev->pci->dev,
317 "rtsx: unable to grab IRQ %d, disabling device\n",
322 dev->irq = dev->pci->irq;
323 pci_intx(dev->pci, !chip->msi_en);
329 int rtsx_read_pci_cfg_byte(u8 bus, u8 dev, u8 func, u8 offset, u8 *val)
331 struct pci_dev *pdev;
333 u8 devfn = (dev << 3) | func;
335 pdev = pci_get_bus_and_slot(bus, devfn);
339 pci_read_config_byte(pdev, offset, &data);
350 static int rtsx_suspend(struct pci_dev *pci, pm_message_t state)
352 struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
353 struct rtsx_chip *chip;
358 /* lock the device pointers */
359 mutex_lock(&(dev->dev_mutex));
363 rtsx_do_before_power_down(chip, PM_S3);
366 synchronize_irq(dev->irq);
367 free_irq(dev->irq, (void *)dev);
372 pci_disable_msi(pci);
375 pci_enable_wake(pci, pci_choose_state(pci, state), 1);
376 pci_disable_device(pci);
377 pci_set_power_state(pci, pci_choose_state(pci, state));
379 /* unlock the device pointers */
380 mutex_unlock(&dev->dev_mutex);
385 static int rtsx_resume(struct pci_dev *pci)
387 struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
388 struct rtsx_chip *chip;
395 /* lock the device pointers */
396 mutex_lock(&(dev->dev_mutex));
398 pci_set_power_state(pci, PCI_D0);
399 pci_restore_state(pci);
400 if (pci_enable_device(pci) < 0) {
401 dev_err(&dev->pci->dev,
402 "%s: pci_enable_device failed, disabling device\n",
404 /* unlock the device pointers */
405 mutex_unlock(&dev->dev_mutex);
411 if (pci_enable_msi(pci) < 0)
415 if (rtsx_acquire_irq(dev) < 0) {
416 /* unlock the device pointers */
417 mutex_unlock(&dev->dev_mutex);
421 rtsx_write_register(chip, HOST_SLEEP_STATE, 0x03, 0x00);
422 rtsx_init_chip(chip);
424 /* unlock the device pointers */
425 mutex_unlock(&dev->dev_mutex);
429 #endif /* CONFIG_PM */
431 static void rtsx_shutdown(struct pci_dev *pci)
433 struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
434 struct rtsx_chip *chip;
441 rtsx_do_before_power_down(chip, PM_S1);
444 synchronize_irq(dev->irq);
445 free_irq(dev->irq, (void *)dev);
450 pci_disable_msi(pci);
452 pci_disable_device(pci);
457 static int rtsx_control_thread(void *__dev)
459 struct rtsx_dev *dev = (struct rtsx_dev *)__dev;
460 struct rtsx_chip *chip = dev->chip;
461 struct Scsi_Host *host = rtsx_to_host(dev);
464 if (wait_for_completion_interruptible(&dev->cmnd_ready))
467 /* lock the device pointers */
468 mutex_lock(&(dev->dev_mutex));
470 /* if the device has disconnected, we are free to exit */
471 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
472 dev_info(&dev->pci->dev, "-- rtsx-control exiting\n");
473 mutex_unlock(&dev->dev_mutex);
477 /* lock access to the state */
480 /* has the command aborted ? */
481 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
482 chip->srb->result = DID_ABORT << 16;
488 /* reject the command if the direction indicator
491 if (chip->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
492 dev_err(&dev->pci->dev, "UNKNOWN data direction\n");
493 chip->srb->result = DID_ERROR << 16;
496 /* reject if target != 0 or if LUN is higher than
497 * the maximum known LUN
499 else if (chip->srb->device->id) {
500 dev_err(&dev->pci->dev, "Bad target number (%d:%d)\n",
501 chip->srb->device->id,
502 chip->srb->device->lun);
503 chip->srb->result = DID_BAD_TARGET << 16;
506 else if (chip->srb->device->lun > chip->max_lun) {
507 dev_err(&dev->pci->dev, "Bad LUN (%d:%d)\n",
508 chip->srb->device->id,
509 chip->srb->device->lun);
510 chip->srb->result = DID_BAD_TARGET << 16;
513 /* we've got a command, let's do it! */
515 RTSX_DEBUG(scsi_show_command(chip->srb));
516 rtsx_invoke_transport(chip->srb, chip);
519 /* lock access to the state */
522 /* did the command already complete because of a disconnect? */
524 ; /* nothing to do */
526 /* indicate that the command is done */
527 else if (chip->srb->result != DID_ABORT << 16) {
528 chip->srb->scsi_done(chip->srb);
531 dev_err(&dev->pci->dev, "scsi command aborted\n");
534 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
535 complete(&(dev->notify));
537 rtsx_set_stat(chip, RTSX_STAT_IDLE);
540 /* finished working on this command */
544 /* unlock the device pointers */
545 mutex_unlock(&dev->dev_mutex);
548 /* notify the exit routine that we're actually exiting now
550 * complete()/wait_for_completion() is similar to up()/down(),
551 * except that complete() is safe in the case where the structure
552 * is getting deleted in a parallel mode of execution (i.e. just
553 * after the down() -- that's necessary for the thread-shutdown
556 * complete_and_exit() goes even further than this -- it is safe in
557 * the case that the thread of the caller is going away (not just
558 * the structure) -- this is necessary for the module-remove case.
559 * This is important in preemption kernels, which transfer the flow
560 * of execution immediately upon a complete().
562 complete_and_exit(&dev->control_exit, 0);
566 static int rtsx_polling_thread(void *__dev)
568 struct rtsx_dev *dev = (struct rtsx_dev *)__dev;
569 struct rtsx_chip *chip = dev->chip;
570 struct sd_info *sd_card = &(chip->sd_card);
571 struct xd_info *xd_card = &(chip->xd_card);
572 struct ms_info *ms_card = &(chip->ms_card);
574 sd_card->cleanup_counter = 0;
575 xd_card->cleanup_counter = 0;
576 ms_card->cleanup_counter = 0;
578 /* Wait until SCSI scan finished */
579 wait_timeout((delay_use + 5) * 1000);
583 set_current_state(TASK_INTERRUPTIBLE);
584 schedule_timeout(POLLING_INTERVAL);
586 /* lock the device pointers */
587 mutex_lock(&(dev->dev_mutex));
589 /* if the device has disconnected, we are free to exit */
590 if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
591 dev_info(&dev->pci->dev, "-- rtsx-polling exiting\n");
592 mutex_unlock(&dev->dev_mutex);
596 mutex_unlock(&dev->dev_mutex);
598 mspro_polling_format_status(chip);
600 /* lock the device pointers */
601 mutex_lock(&(dev->dev_mutex));
603 rtsx_polling_func(chip);
605 /* unlock the device pointers */
606 mutex_unlock(&dev->dev_mutex);
609 complete_and_exit(&dev->polling_exit, 0);
615 static irqreturn_t rtsx_interrupt(int irq, void *dev_id)
617 struct rtsx_dev *dev = dev_id;
618 struct rtsx_chip *chip;
630 spin_lock(&dev->reg_lock);
632 retval = rtsx_pre_handle_interrupt(chip);
633 if (retval == STATUS_FAIL) {
634 spin_unlock(&dev->reg_lock);
635 if (chip->int_reg == 0xFFFFFFFF)
641 status = chip->int_reg;
643 if (dev->check_card_cd) {
644 if (!(dev->check_card_cd & status)) {
645 /* card not exist, return TRANS_RESULT_FAIL */
646 dev->trans_result = TRANS_RESULT_FAIL;
653 if (status & (NEED_COMPLETE_INT | DELINK_INT)) {
654 if (status & (TRANS_FAIL_INT | DELINK_INT)) {
655 if (status & DELINK_INT)
656 RTSX_SET_DELINK(chip);
657 dev->trans_result = TRANS_RESULT_FAIL;
660 } else if (status & TRANS_OK_INT) {
661 dev->trans_result = TRANS_RESULT_OK;
664 } else if (status & DATA_DONE_INT) {
665 dev->trans_result = TRANS_NOT_READY;
666 if (dev->done && (dev->trans_state == STATE_TRANS_SG))
672 spin_unlock(&dev->reg_lock);
677 /* Release all our dynamic resources */
678 static void rtsx_release_resources(struct rtsx_dev *dev)
680 dev_info(&dev->pci->dev, "-- %s\n", __func__);
682 /* Tell the control thread to exit. The SCSI host must
683 * already have been removed so it won't try to queue
686 dev_info(&dev->pci->dev, "-- sending exit command to thread\n");
687 complete(&dev->cmnd_ready);
689 wait_for_completion(&dev->control_exit);
690 if (dev->polling_thread)
691 wait_for_completion(&dev->polling_exit);
695 if (dev->rtsx_resv_buf) {
696 dma_free_coherent(&(dev->pci->dev), RTSX_RESV_BUF_LEN,
697 dev->rtsx_resv_buf, dev->rtsx_resv_buf_addr);
698 dev->chip->host_cmds_ptr = NULL;
699 dev->chip->host_sg_tbl_ptr = NULL;
703 free_irq(dev->irq, (void *)dev);
704 if (dev->chip->msi_en)
705 pci_disable_msi(dev->pci);
707 iounmap(dev->remap_addr);
709 pci_disable_device(dev->pci);
710 pci_release_regions(dev->pci);
712 rtsx_release_chip(dev->chip);
716 /* First stage of disconnect processing: stop all commands and remove
718 static void quiesce_and_remove_host(struct rtsx_dev *dev)
720 struct Scsi_Host *host = rtsx_to_host(dev);
721 struct rtsx_chip *chip = dev->chip;
723 /* Prevent new transfers, stop the current command, and
724 * interrupt a SCSI-scan or device-reset delay */
725 mutex_lock(&dev->dev_mutex);
727 rtsx_set_stat(chip, RTSX_STAT_DISCONNECT);
729 mutex_unlock(&dev->dev_mutex);
730 wake_up(&dev->delay_wait);
731 wait_for_completion(&dev->scanning_done);
733 /* Wait some time to let other threads exist */
736 /* queuecommand won't accept any new commands and the control
737 * thread won't execute a previously-queued command. If there
738 * is such a command pending, complete it with an error. */
739 mutex_lock(&dev->dev_mutex);
741 chip->srb->result = DID_NO_CONNECT << 16;
743 chip->srb->scsi_done(dev->chip->srb);
747 mutex_unlock(&dev->dev_mutex);
749 /* Now we own no commands so it's safe to remove the SCSI host */
750 scsi_remove_host(host);
753 /* Second stage of disconnect processing: deallocate all resources */
754 static void release_everything(struct rtsx_dev *dev)
756 rtsx_release_resources(dev);
758 /* Drop our reference to the host; the SCSI core will free it
759 * when the refcount becomes 0. */
760 scsi_host_put(rtsx_to_host(dev));
763 /* Thread to carry out delayed SCSI-device scanning */
764 static int rtsx_scan_thread(void *__dev)
766 struct rtsx_dev *dev = (struct rtsx_dev *)__dev;
767 struct rtsx_chip *chip = dev->chip;
769 /* Wait for the timeout to expire or for a disconnect */
771 dev_info(&dev->pci->dev,
772 "%s: waiting for device to settle before scanning\n",
774 wait_event_interruptible_timeout(dev->delay_wait,
775 rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT),
779 /* If the device is still connected, perform the scanning */
780 if (!rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
781 scsi_scan_host(rtsx_to_host(dev));
782 dev_info(&dev->pci->dev, "%s: device scan complete\n",
785 /* Should we unbind if no devices were detected? */
788 complete_and_exit(&dev->scanning_done, 0);
791 static void rtsx_init_options(struct rtsx_chip *chip)
793 chip->vendor_id = chip->rtsx->pci->vendor;
794 chip->product_id = chip->rtsx->pci->device;
797 chip->driver_first_load = 1;
798 #ifdef HW_AUTO_SWITCH_SD_BUS
799 chip->sdio_in_charge = 0;
802 chip->mspro_formatter_enable = 1;
804 chip->use_hw_setting = 0;
805 chip->lun_mode = DEFAULT_SINGLE;
806 chip->auto_delink_en = auto_delink_en;
808 chip->ss_idle_period = ss_interval * 1000;
809 chip->remote_wakeup_en = 0;
810 chip->aspm_l0s_l1_en = aspm_l0s_l1_en;
811 chip->dynamic_aspm = 1;
812 chip->fpga_sd_sdr104_clk = CLK_200;
813 chip->fpga_sd_ddr50_clk = CLK_100;
814 chip->fpga_sd_sdr50_clk = CLK_100;
815 chip->fpga_sd_hs_clk = CLK_100;
816 chip->fpga_mmc_52m_clk = CLK_80;
817 chip->fpga_ms_hg_clk = CLK_80;
818 chip->fpga_ms_4bit_clk = CLK_80;
819 chip->fpga_ms_1bit_clk = CLK_40;
820 chip->asic_sd_sdr104_clk = 203;
821 chip->asic_sd_sdr50_clk = 98;
822 chip->asic_sd_ddr50_clk = 98;
823 chip->asic_sd_hs_clk = 98;
824 chip->asic_mmc_52m_clk = 98;
825 chip->asic_ms_hg_clk = 117;
826 chip->asic_ms_4bit_clk = 78;
827 chip->asic_ms_1bit_clk = 39;
828 chip->ssc_depth_sd_sdr104 = SSC_DEPTH_2M;
829 chip->ssc_depth_sd_sdr50 = SSC_DEPTH_2M;
830 chip->ssc_depth_sd_ddr50 = SSC_DEPTH_1M;
831 chip->ssc_depth_sd_hs = SSC_DEPTH_1M;
832 chip->ssc_depth_mmc_52m = SSC_DEPTH_1M;
833 chip->ssc_depth_ms_hg = SSC_DEPTH_1M;
834 chip->ssc_depth_ms_4bit = SSC_DEPTH_512K;
835 chip->ssc_depth_low_speed = SSC_DEPTH_512K;
837 chip->sd_speed_prior = 0x01040203;
838 chip->sd_current_prior = 0x00010203;
839 chip->sd_ctl = SD_PUSH_POINT_AUTO |
840 SD_SAMPLE_POINT_AUTO |
841 SUPPORT_MMC_DDR_MODE;
842 chip->sd_ddr_tx_phase = 0;
843 chip->mmc_ddr_tx_phase = 1;
844 chip->sd_default_tx_phase = 15;
845 chip->sd_default_rx_phase = 15;
846 chip->pmos_pwr_on_interval = 200;
847 chip->sd_voltage_switch_delay = 1000;
848 chip->ms_power_class_en = 3;
850 chip->sd_400mA_ocp_thd = 1;
851 chip->sd_800mA_ocp_thd = 5;
852 chip->ms_ocp_thd = 2;
854 chip->card_drive_sel = 0x55;
855 chip->sd30_drive_sel_1v8 = 0x03;
856 chip->sd30_drive_sel_3v3 = 0x01;
858 chip->do_delink_before_power_down = 1;
859 chip->auto_power_down = 1;
860 chip->polling_config = 0;
862 chip->force_clkreq_0 = 1;
863 chip->ft2_fast_mode = 0;
865 chip->sdio_retry_cnt = 1;
867 chip->xd_timeout = 2000;
868 chip->sd_timeout = 10000;
869 chip->ms_timeout = 2000;
870 chip->mspro_timeout = 15000;
872 chip->power_down_in_ss = 1;
878 chip->delink_stage1_step = 100;
879 chip->delink_stage2_step = 40;
880 chip->delink_stage3_step = 20;
882 chip->auto_delink_in_L1 = 1;
884 chip->msi_en = msi_en;
885 chip->hp_watch_bios_hotplug = 0;
886 chip->max_payload = 0;
887 chip->phy_voltage = 0;
889 chip->support_ms_8bit = 1;
890 chip->s3_pwr_off_delay = 1000;
893 static int __devinit rtsx_probe(struct pci_dev *pci,
894 const struct pci_device_id *pci_id)
896 struct Scsi_Host *host;
897 struct rtsx_dev *dev;
899 struct task_struct *th;
901 RTSX_DEBUGP("Realtek PCI-E card reader detected\n");
903 err = pci_enable_device(pci);
905 dev_err(&pci->dev, "PCI enable device failed!\n");
909 err = pci_request_regions(pci, CR_DRIVER_NAME);
911 dev_err(&pci->dev, "PCI request regions for %s failed!\n",
913 pci_disable_device(pci);
918 * Ask the SCSI layer to allocate a host structure, with extra
919 * space at the end for our private rtsx_dev structure.
921 host = scsi_host_alloc(&rtsx_host_template, sizeof(*dev));
923 dev_err(&pci->dev, "Unable to allocate the scsi host\n");
924 pci_release_regions(pci);
925 pci_disable_device(pci);
929 dev = host_to_rtsx(host);
930 memset(dev, 0, sizeof(struct rtsx_dev));
932 dev->chip = kzalloc(sizeof(struct rtsx_chip), GFP_KERNEL);
933 if (dev->chip == NULL)
936 spin_lock_init(&dev->reg_lock);
937 mutex_init(&(dev->dev_mutex));
938 init_completion(&dev->cmnd_ready);
939 init_completion(&dev->control_exit);
940 init_completion(&dev->polling_exit);
941 init_completion(&(dev->notify));
942 init_completion(&dev->scanning_done);
943 init_waitqueue_head(&dev->delay_wait);
948 dev_info(&pci->dev, "Resource length: 0x%x\n",
949 (unsigned int)pci_resource_len(pci, 0));
950 dev->addr = pci_resource_start(pci, 0);
951 dev->remap_addr = ioremap_nocache(dev->addr, pci_resource_len(pci, 0));
952 if (dev->remap_addr == NULL) {
953 dev_err(&pci->dev, "ioremap error\n");
959 * Using "unsigned long" cast here to eliminate gcc warning in
962 dev_info(&pci->dev, "Original address: 0x%lx, remapped address: 0x%lx\n",
963 (unsigned long)(dev->addr), (unsigned long)(dev->remap_addr));
965 dev->rtsx_resv_buf = dma_alloc_coherent(&(pci->dev), RTSX_RESV_BUF_LEN,
966 &(dev->rtsx_resv_buf_addr), GFP_KERNEL);
967 if (dev->rtsx_resv_buf == NULL) {
968 dev_err(&pci->dev, "alloc dma buffer fail\n");
972 dev->chip->host_cmds_ptr = dev->rtsx_resv_buf;
973 dev->chip->host_cmds_addr = dev->rtsx_resv_buf_addr;
974 dev->chip->host_sg_tbl_ptr = dev->rtsx_resv_buf + HOST_CMDS_BUF_LEN;
975 dev->chip->host_sg_tbl_addr = dev->rtsx_resv_buf_addr +
978 dev->chip->rtsx = dev;
980 rtsx_init_options(dev->chip);
982 dev_info(&pci->dev, "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 /* set the supported max_lun and max_id for the scsi host
1000 * NOTE: the minimal value of max_id is 1 */
1002 host->max_lun = dev->chip->max_lun;
1004 /* Start up our control thread */
1005 th = kthread_run(rtsx_control_thread, dev, CR_DRIVER_NAME);
1007 dev_err(&pci->dev, "Unable to start control thread\n");
1011 dev->ctl_thread = th;
1013 err = scsi_add_host(host, &pci->dev);
1015 dev_err(&pci->dev, "Unable to add the scsi host\n");
1019 /* Start up the thread for delayed SCSI-device scanning */
1020 th = kthread_run(rtsx_scan_thread, dev, "rtsx-scan");
1022 dev_err(&pci->dev, "Unable to start the device-scanning thread\n");
1023 complete(&dev->scanning_done);
1024 quiesce_and_remove_host(dev);
1029 /* Start up the thread for polling thread */
1030 th = kthread_run(rtsx_polling_thread, dev, "rtsx-polling");
1032 dev_err(&pci->dev, "Unable to start the device-polling thread\n");
1033 quiesce_and_remove_host(dev);
1037 dev->polling_thread = th;
1039 pci_set_drvdata(pci, dev);
1043 /* We come here if there are any problems */
1045 dev_err(&pci->dev, "rtsx_probe() failed\n");
1046 release_everything(dev);
1052 static void __devexit rtsx_remove(struct pci_dev *pci)
1054 struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
1056 dev_info(&pci->dev, "rtsx_remove() called\n");
1058 quiesce_and_remove_host(dev);
1059 release_everything(dev);
1061 pci_set_drvdata(pci, NULL);
1065 static DEFINE_PCI_DEVICE_TABLE(rtsx_ids) = {
1066 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5208), PCI_CLASS_OTHERS << 16, 0xFF0000 },
1067 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5209), PCI_CLASS_OTHERS << 16, 0xFF0000 },
1068 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5288), PCI_CLASS_OTHERS << 16, 0xFF0000 },
1072 MODULE_DEVICE_TABLE(pci, rtsx_ids);
1074 /* pci_driver definition */
1075 static struct pci_driver driver = {
1076 .name = CR_DRIVER_NAME,
1077 .id_table = rtsx_ids,
1078 .probe = rtsx_probe,
1079 .remove = __devexit_p(rtsx_remove),
1081 .suspend = rtsx_suspend,
1082 .resume = rtsx_resume,
1084 .shutdown = rtsx_shutdown,
1087 static int __init rtsx_init(void)
1089 pr_info("Initializing Realtek PCIE storage driver...\n");
1091 return pci_register_driver(&driver);
1094 static void __exit rtsx_exit(void)
1096 pr_info("rtsx_exit() called\n");
1098 pci_unregister_driver(&driver);
1100 pr_info("%s module exit\n", CR_DRIVER_NAME);
1103 module_init(rtsx_init)
1104 module_exit(rtsx_exit)