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