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 * Wei WANG <wei_wang@realsil.com.cn>
20 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
23 #include <linux/pci.h>
24 #include <linux/module.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/highmem.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <linux/idr.h>
30 #include <linux/platform_device.h>
31 #include <linux/mfd/core.h>
32 #include <linux/mfd/rtsx_pci.h>
33 #include <asm/unaligned.h>
37 static bool msi_en = true;
38 module_param(msi_en, bool, S_IRUGO | S_IWUSR);
39 MODULE_PARM_DESC(msi_en, "Enable MSI");
41 static DEFINE_IDR(rtsx_pci_idr);
42 static DEFINE_SPINLOCK(rtsx_pci_lock);
44 static struct mfd_cell rtsx_pcr_cells[] = {
46 .name = DRV_NAME_RTSX_PCI_SDMMC,
49 .name = DRV_NAME_RTSX_PCI_MS,
53 static DEFINE_PCI_DEVICE_TABLE(rtsx_pci_ids) = {
54 { PCI_DEVICE(0x10EC, 0x5209), PCI_CLASS_OTHERS << 16, 0xFF0000 },
55 { PCI_DEVICE(0x10EC, 0x5229), PCI_CLASS_OTHERS << 16, 0xFF0000 },
56 { PCI_DEVICE(0x10EC, 0x5289), PCI_CLASS_OTHERS << 16, 0xFF0000 },
60 MODULE_DEVICE_TABLE(pci, rtsx_pci_ids);
62 void rtsx_pci_start_run(struct rtsx_pcr *pcr)
64 /* If pci device removed, don't queue idle work any more */
68 if (pcr->state != PDEV_STAT_RUN) {
69 pcr->state = PDEV_STAT_RUN;
70 if (pcr->ops->enable_auto_blink)
71 pcr->ops->enable_auto_blink(pcr);
74 mod_delayed_work(system_wq, &pcr->idle_work, msecs_to_jiffies(200));
76 EXPORT_SYMBOL_GPL(rtsx_pci_start_run);
78 int rtsx_pci_write_register(struct rtsx_pcr *pcr, u16 addr, u8 mask, u8 data)
81 u32 val = HAIMR_WRITE_START;
83 val |= (u32)(addr & 0x3FFF) << 16;
84 val |= (u32)mask << 8;
87 rtsx_pci_writel(pcr, RTSX_HAIMR, val);
89 for (i = 0; i < MAX_RW_REG_CNT; i++) {
90 val = rtsx_pci_readl(pcr, RTSX_HAIMR);
91 if ((val & HAIMR_TRANS_END) == 0) {
100 EXPORT_SYMBOL_GPL(rtsx_pci_write_register);
102 int rtsx_pci_read_register(struct rtsx_pcr *pcr, u16 addr, u8 *data)
104 u32 val = HAIMR_READ_START;
107 val |= (u32)(addr & 0x3FFF) << 16;
108 rtsx_pci_writel(pcr, RTSX_HAIMR, val);
110 for (i = 0; i < MAX_RW_REG_CNT; i++) {
111 val = rtsx_pci_readl(pcr, RTSX_HAIMR);
112 if ((val & HAIMR_TRANS_END) == 0)
116 if (i >= MAX_RW_REG_CNT)
120 *data = (u8)(val & 0xFF);
124 EXPORT_SYMBOL_GPL(rtsx_pci_read_register);
126 int rtsx_pci_write_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 val)
128 int err, i, finished = 0;
131 rtsx_pci_init_cmd(pcr);
133 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYDATA0, 0xFF, (u8)val);
134 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYDATA1, 0xFF, (u8)(val >> 8));
135 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYADDR, 0xFF, addr);
136 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYRWCTL, 0xFF, 0x81);
138 err = rtsx_pci_send_cmd(pcr, 100);
142 for (i = 0; i < 100000; i++) {
143 err = rtsx_pci_read_register(pcr, PHYRWCTL, &tmp);
158 EXPORT_SYMBOL_GPL(rtsx_pci_write_phy_register);
160 int rtsx_pci_read_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 *val)
162 int err, i, finished = 0;
166 rtsx_pci_init_cmd(pcr);
168 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYADDR, 0xFF, addr);
169 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYRWCTL, 0xFF, 0x80);
171 err = rtsx_pci_send_cmd(pcr, 100);
175 for (i = 0; i < 100000; i++) {
176 err = rtsx_pci_read_register(pcr, PHYRWCTL, &tmp);
189 rtsx_pci_init_cmd(pcr);
191 rtsx_pci_add_cmd(pcr, READ_REG_CMD, PHYDATA0, 0, 0);
192 rtsx_pci_add_cmd(pcr, READ_REG_CMD, PHYDATA1, 0, 0);
194 err = rtsx_pci_send_cmd(pcr, 100);
198 ptr = rtsx_pci_get_cmd_data(pcr);
199 data = ((u16)ptr[1] << 8) | ptr[0];
206 EXPORT_SYMBOL_GPL(rtsx_pci_read_phy_register);
208 void rtsx_pci_stop_cmd(struct rtsx_pcr *pcr)
210 rtsx_pci_writel(pcr, RTSX_HCBCTLR, STOP_CMD);
211 rtsx_pci_writel(pcr, RTSX_HDBCTLR, STOP_DMA);
213 rtsx_pci_write_register(pcr, DMACTL, 0x80, 0x80);
214 rtsx_pci_write_register(pcr, RBCTL, 0x80, 0x80);
216 EXPORT_SYMBOL_GPL(rtsx_pci_stop_cmd);
218 void rtsx_pci_add_cmd(struct rtsx_pcr *pcr,
219 u8 cmd_type, u16 reg_addr, u8 mask, u8 data)
223 u32 *ptr = (u32 *)(pcr->host_cmds_ptr);
225 val |= (u32)(cmd_type & 0x03) << 30;
226 val |= (u32)(reg_addr & 0x3FFF) << 16;
227 val |= (u32)mask << 8;
230 spin_lock_irqsave(&pcr->lock, flags);
232 if (pcr->ci < (HOST_CMDS_BUF_LEN / 4)) {
233 put_unaligned_le32(val, ptr);
237 spin_unlock_irqrestore(&pcr->lock, flags);
239 EXPORT_SYMBOL_GPL(rtsx_pci_add_cmd);
241 void rtsx_pci_send_cmd_no_wait(struct rtsx_pcr *pcr)
245 rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr);
247 val |= (u32)(pcr->ci * 4) & 0x00FFFFFF;
248 /* Hardware Auto Response */
250 rtsx_pci_writel(pcr, RTSX_HCBCTLR, val);
252 EXPORT_SYMBOL_GPL(rtsx_pci_send_cmd_no_wait);
254 int rtsx_pci_send_cmd(struct rtsx_pcr *pcr, int timeout)
256 struct completion trans_done;
262 spin_lock_irqsave(&pcr->lock, flags);
264 /* set up data structures for the wakeup system */
265 pcr->done = &trans_done;
266 pcr->trans_result = TRANS_NOT_READY;
267 init_completion(&trans_done);
269 rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr);
271 val |= (u32)(pcr->ci * 4) & 0x00FFFFFF;
272 /* Hardware Auto Response */
274 rtsx_pci_writel(pcr, RTSX_HCBCTLR, val);
276 spin_unlock_irqrestore(&pcr->lock, flags);
278 /* Wait for TRANS_OK_INT */
279 timeleft = wait_for_completion_interruptible_timeout(
280 &trans_done, msecs_to_jiffies(timeout));
282 dev_dbg(&(pcr->pci->dev), "Timeout (%s %d)\n",
285 goto finish_send_cmd;
288 spin_lock_irqsave(&pcr->lock, flags);
289 if (pcr->trans_result == TRANS_RESULT_FAIL)
291 else if (pcr->trans_result == TRANS_RESULT_OK)
293 else if (pcr->trans_result == TRANS_NO_DEVICE)
295 spin_unlock_irqrestore(&pcr->lock, flags);
298 spin_lock_irqsave(&pcr->lock, flags);
300 spin_unlock_irqrestore(&pcr->lock, flags);
302 if ((err < 0) && (err != -ENODEV))
303 rtsx_pci_stop_cmd(pcr);
306 complete(pcr->finish_me);
310 EXPORT_SYMBOL_GPL(rtsx_pci_send_cmd);
312 static void rtsx_pci_add_sg_tbl(struct rtsx_pcr *pcr,
313 dma_addr_t addr, unsigned int len, int end)
315 u64 *ptr = (u64 *)(pcr->host_sg_tbl_ptr) + pcr->sgi;
317 u8 option = SG_VALID | SG_TRANS_DATA;
319 dev_dbg(&(pcr->pci->dev), "DMA addr: 0x%x, Len: 0x%x\n",
320 (unsigned int)addr, len);
324 val = ((u64)addr << 32) | ((u64)len << 12) | option;
326 put_unaligned_le64(val, ptr);
331 int rtsx_pci_transfer_data(struct rtsx_pcr *pcr, struct scatterlist *sglist,
332 int num_sg, bool read, int timeout)
334 struct completion trans_done;
336 int err = 0, i, count;
339 struct scatterlist *sg;
340 enum dma_data_direction dma_dir;
345 dev_dbg(&(pcr->pci->dev), "--> %s: num_sg = %d\n", __func__, num_sg);
347 /* don't transfer data during abort processing */
351 if ((sglist == NULL) || (num_sg <= 0))
355 dir = DEVICE_TO_HOST;
356 dma_dir = DMA_FROM_DEVICE;
358 dir = HOST_TO_DEVICE;
359 dma_dir = DMA_TO_DEVICE;
362 count = dma_map_sg(&(pcr->pci->dev), sglist, num_sg, dma_dir);
364 dev_err(&(pcr->pci->dev), "scatterlist map failed\n");
367 dev_dbg(&(pcr->pci->dev), "DMA mapping count: %d\n", count);
369 val = ((u32)(dir & 0x01) << 29) | TRIG_DMA | ADMA_MODE;
371 for_each_sg(sglist, sg, count, i) {
372 addr = sg_dma_address(sg);
373 len = sg_dma_len(sg);
374 rtsx_pci_add_sg_tbl(pcr, addr, len, i == count - 1);
377 spin_lock_irqsave(&pcr->lock, flags);
379 pcr->done = &trans_done;
380 pcr->trans_result = TRANS_NOT_READY;
381 init_completion(&trans_done);
382 rtsx_pci_writel(pcr, RTSX_HDBAR, pcr->host_sg_tbl_addr);
383 rtsx_pci_writel(pcr, RTSX_HDBCTLR, val);
385 spin_unlock_irqrestore(&pcr->lock, flags);
387 timeleft = wait_for_completion_interruptible_timeout(
388 &trans_done, msecs_to_jiffies(timeout));
390 dev_dbg(&(pcr->pci->dev), "Timeout (%s %d)\n",
396 spin_lock_irqsave(&pcr->lock, flags);
398 if (pcr->trans_result == TRANS_RESULT_FAIL)
400 else if (pcr->trans_result == TRANS_NO_DEVICE)
403 spin_unlock_irqrestore(&pcr->lock, flags);
406 spin_lock_irqsave(&pcr->lock, flags);
408 spin_unlock_irqrestore(&pcr->lock, flags);
410 dma_unmap_sg(&(pcr->pci->dev), sglist, num_sg, dma_dir);
412 if ((err < 0) && (err != -ENODEV))
413 rtsx_pci_stop_cmd(pcr);
416 complete(pcr->finish_me);
420 EXPORT_SYMBOL_GPL(rtsx_pci_transfer_data);
422 int rtsx_pci_read_ppbuf(struct rtsx_pcr *pcr, u8 *buf, int buf_len)
434 for (i = 0; i < buf_len / 256; i++) {
435 rtsx_pci_init_cmd(pcr);
437 for (j = 0; j < 256; j++)
438 rtsx_pci_add_cmd(pcr, READ_REG_CMD, reg++, 0, 0);
440 err = rtsx_pci_send_cmd(pcr, 250);
444 memcpy(ptr, rtsx_pci_get_cmd_data(pcr), 256);
449 rtsx_pci_init_cmd(pcr);
451 for (j = 0; j < buf_len % 256; j++)
452 rtsx_pci_add_cmd(pcr, READ_REG_CMD, reg++, 0, 0);
454 err = rtsx_pci_send_cmd(pcr, 250);
459 memcpy(ptr, rtsx_pci_get_cmd_data(pcr), buf_len % 256);
463 EXPORT_SYMBOL_GPL(rtsx_pci_read_ppbuf);
465 int rtsx_pci_write_ppbuf(struct rtsx_pcr *pcr, u8 *buf, int buf_len)
477 for (i = 0; i < buf_len / 256; i++) {
478 rtsx_pci_init_cmd(pcr);
480 for (j = 0; j < 256; j++) {
481 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
486 err = rtsx_pci_send_cmd(pcr, 250);
492 rtsx_pci_init_cmd(pcr);
494 for (j = 0; j < buf_len % 256; j++) {
495 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
500 err = rtsx_pci_send_cmd(pcr, 250);
507 EXPORT_SYMBOL_GPL(rtsx_pci_write_ppbuf);
509 static int rtsx_pci_set_pull_ctl(struct rtsx_pcr *pcr, const u32 *tbl)
513 rtsx_pci_init_cmd(pcr);
515 while (*tbl & 0xFFFF0000) {
516 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
517 (u16)(*tbl >> 16), 0xFF, (u8)(*tbl));
521 err = rtsx_pci_send_cmd(pcr, 100);
528 int rtsx_pci_card_pull_ctl_enable(struct rtsx_pcr *pcr, int card)
532 if (card == RTSX_SD_CARD)
533 tbl = pcr->sd_pull_ctl_enable_tbl;
534 else if (card == RTSX_MS_CARD)
535 tbl = pcr->ms_pull_ctl_enable_tbl;
539 return rtsx_pci_set_pull_ctl(pcr, tbl);
541 EXPORT_SYMBOL_GPL(rtsx_pci_card_pull_ctl_enable);
543 int rtsx_pci_card_pull_ctl_disable(struct rtsx_pcr *pcr, int card)
547 if (card == RTSX_SD_CARD)
548 tbl = pcr->sd_pull_ctl_disable_tbl;
549 else if (card == RTSX_MS_CARD)
550 tbl = pcr->ms_pull_ctl_disable_tbl;
555 return rtsx_pci_set_pull_ctl(pcr, tbl);
557 EXPORT_SYMBOL_GPL(rtsx_pci_card_pull_ctl_disable);
559 static void rtsx_pci_enable_bus_int(struct rtsx_pcr *pcr)
561 pcr->bier = TRANS_OK_INT_EN | TRANS_FAIL_INT_EN | SD_INT_EN;
563 if (pcr->num_slots > 1)
564 pcr->bier |= MS_INT_EN;
566 /* Enable Bus Interrupt */
567 rtsx_pci_writel(pcr, RTSX_BIER, pcr->bier);
569 dev_dbg(&(pcr->pci->dev), "RTSX_BIER: 0x%08x\n", pcr->bier);
572 static inline u8 double_ssc_depth(u8 depth)
574 return ((depth > 1) ? (depth - 1) : depth);
577 static u8 revise_ssc_depth(u8 ssc_depth, u8 div)
579 if (div > CLK_DIV_1) {
580 if (ssc_depth > (div - 1))
581 ssc_depth -= (div - 1);
583 ssc_depth = SSC_DEPTH_4M;
589 int rtsx_pci_switch_clock(struct rtsx_pcr *pcr, unsigned int card_clock,
590 u8 ssc_depth, bool initial_mode, bool double_clk, bool vpclk)
593 u8 N, min_N, max_N, clk_divider;
594 u8 mcu_cnt, div, max_div;
596 [RTSX_SSC_DEPTH_4M] = SSC_DEPTH_4M,
597 [RTSX_SSC_DEPTH_2M] = SSC_DEPTH_2M,
598 [RTSX_SSC_DEPTH_1M] = SSC_DEPTH_1M,
599 [RTSX_SSC_DEPTH_500K] = SSC_DEPTH_500K,
600 [RTSX_SSC_DEPTH_250K] = SSC_DEPTH_250K,
604 /* We use 250k(around) here, in initial stage */
605 clk_divider = SD_CLK_DIVIDE_128;
606 card_clock = 30000000;
608 clk_divider = SD_CLK_DIVIDE_0;
610 err = rtsx_pci_write_register(pcr, SD_CFG1,
611 SD_CLK_DIVIDE_MASK, clk_divider);
615 card_clock /= 1000000;
616 dev_dbg(&(pcr->pci->dev), "Switch card clock to %dMHz\n", card_clock);
623 if (!initial_mode && double_clk)
624 clk = card_clock * 2;
625 dev_dbg(&(pcr->pci->dev),
626 "Internal SSC clock: %dMHz (cur_clock = %d)\n",
627 clk, pcr->cur_clock);
629 if (clk == pcr->cur_clock)
633 if ((clk <= 2) || (N > max_N))
636 mcu_cnt = (u8)(125/clk + 3);
640 /* Make sure that the SSC clock div_n is equal or greater than min_N */
642 while ((N < min_N) && (div < max_div)) {
646 dev_dbg(&(pcr->pci->dev), "N = %d, div = %d\n", N, div);
648 ssc_depth = depth[ssc_depth];
650 ssc_depth = double_ssc_depth(ssc_depth);
652 ssc_depth = revise_ssc_depth(ssc_depth, div);
653 dev_dbg(&(pcr->pci->dev), "ssc_depth = %d\n", ssc_depth);
655 rtsx_pci_init_cmd(pcr);
656 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL,
657 CLK_LOW_FREQ, CLK_LOW_FREQ);
658 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_DIV,
659 0xFF, (div << 4) | mcu_cnt);
660 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, 0);
661 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL2,
662 SSC_DEPTH_MASK, ssc_depth);
663 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_DIV_N_0, 0xFF, N);
664 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, SSC_RSTB);
666 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPCLK0_CTL,
668 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPCLK0_CTL,
669 PHASE_NOT_RESET, PHASE_NOT_RESET);
672 err = rtsx_pci_send_cmd(pcr, 2000);
676 /* Wait SSC clock stable */
678 err = rtsx_pci_write_register(pcr, CLK_CTL, CLK_LOW_FREQ, 0);
682 pcr->cur_clock = clk;
685 EXPORT_SYMBOL_GPL(rtsx_pci_switch_clock);
687 int rtsx_pci_card_power_on(struct rtsx_pcr *pcr, int card)
689 if (pcr->ops->card_power_on)
690 return pcr->ops->card_power_on(pcr, card);
694 EXPORT_SYMBOL_GPL(rtsx_pci_card_power_on);
696 int rtsx_pci_card_power_off(struct rtsx_pcr *pcr, int card)
698 if (pcr->ops->card_power_off)
699 return pcr->ops->card_power_off(pcr, card);
703 EXPORT_SYMBOL_GPL(rtsx_pci_card_power_off);
705 unsigned int rtsx_pci_card_exist(struct rtsx_pcr *pcr)
709 val = rtsx_pci_readl(pcr, RTSX_BIPR);
710 if (pcr->ops->cd_deglitch)
711 val = pcr->ops->cd_deglitch(pcr);
715 EXPORT_SYMBOL_GPL(rtsx_pci_card_exist);
717 void rtsx_pci_complete_unfinished_transfer(struct rtsx_pcr *pcr)
719 struct completion finish;
721 pcr->finish_me = &finish;
722 init_completion(&finish);
727 if (!pcr->remove_pci)
728 rtsx_pci_stop_cmd(pcr);
730 wait_for_completion_interruptible_timeout(&finish,
731 msecs_to_jiffies(2));
732 pcr->finish_me = NULL;
734 EXPORT_SYMBOL_GPL(rtsx_pci_complete_unfinished_transfer);
736 static void rtsx_pci_card_detect(struct work_struct *work)
738 struct delayed_work *dwork;
739 struct rtsx_pcr *pcr;
741 unsigned int card_detect = 0;
744 dwork = to_delayed_work(work);
745 pcr = container_of(dwork, struct rtsx_pcr, carddet_work);
747 dev_dbg(&(pcr->pci->dev), "--> %s\n", __func__);
749 spin_lock_irqsave(&pcr->lock, flags);
751 irq_status = rtsx_pci_readl(pcr, RTSX_BIPR);
752 dev_dbg(&(pcr->pci->dev), "irq_status: 0x%08x\n", irq_status);
754 if (pcr->card_inserted || pcr->card_removed) {
755 dev_dbg(&(pcr->pci->dev),
756 "card_inserted: 0x%x, card_removed: 0x%x\n",
757 pcr->card_inserted, pcr->card_removed);
759 if (pcr->ops->cd_deglitch)
760 pcr->card_inserted = pcr->ops->cd_deglitch(pcr);
762 card_detect = pcr->card_inserted | pcr->card_removed;
763 pcr->card_inserted = 0;
764 pcr->card_removed = 0;
767 spin_unlock_irqrestore(&pcr->lock, flags);
769 if (card_detect & SD_EXIST)
770 pcr->slots[RTSX_SD_CARD].card_event(
771 pcr->slots[RTSX_SD_CARD].p_dev);
772 if (card_detect & MS_EXIST)
773 pcr->slots[RTSX_MS_CARD].card_event(
774 pcr->slots[RTSX_MS_CARD].p_dev);
777 static irqreturn_t rtsx_pci_isr(int irq, void *dev_id)
779 struct rtsx_pcr *pcr = dev_id;
785 spin_lock(&pcr->lock);
787 int_reg = rtsx_pci_readl(pcr, RTSX_BIPR);
788 /* Clear interrupt flag */
789 rtsx_pci_writel(pcr, RTSX_BIPR, int_reg);
790 if ((int_reg & pcr->bier) == 0) {
791 spin_unlock(&pcr->lock);
794 if (int_reg == 0xFFFFFFFF) {
795 spin_unlock(&pcr->lock);
799 int_reg &= (pcr->bier | 0x7FFFFF);
801 if (int_reg & SD_INT) {
802 if (int_reg & SD_EXIST) {
803 pcr->card_inserted |= SD_EXIST;
805 pcr->card_removed |= SD_EXIST;
806 pcr->card_inserted &= ~SD_EXIST;
810 if (int_reg & MS_INT) {
811 if (int_reg & MS_EXIST) {
812 pcr->card_inserted |= MS_EXIST;
814 pcr->card_removed |= MS_EXIST;
815 pcr->card_inserted &= ~MS_EXIST;
819 if (pcr->card_inserted || pcr->card_removed)
820 schedule_delayed_work(&pcr->carddet_work,
821 msecs_to_jiffies(200));
823 if (int_reg & (NEED_COMPLETE_INT | DELINK_INT)) {
824 if (int_reg & (TRANS_FAIL_INT | DELINK_INT)) {
825 pcr->trans_result = TRANS_RESULT_FAIL;
828 } else if (int_reg & TRANS_OK_INT) {
829 pcr->trans_result = TRANS_RESULT_OK;
835 spin_unlock(&pcr->lock);
839 static int rtsx_pci_acquire_irq(struct rtsx_pcr *pcr)
841 dev_info(&(pcr->pci->dev), "%s: pcr->msi_en = %d, pci->irq = %d\n",
842 __func__, pcr->msi_en, pcr->pci->irq);
844 if (request_irq(pcr->pci->irq, rtsx_pci_isr,
845 pcr->msi_en ? 0 : IRQF_SHARED,
846 DRV_NAME_RTSX_PCI, pcr)) {
847 dev_err(&(pcr->pci->dev),
848 "rtsx_sdmmc: unable to grab IRQ %d, disabling device\n",
853 pcr->irq = pcr->pci->irq;
854 pci_intx(pcr->pci, !pcr->msi_en);
859 static void rtsx_pci_idle_work(struct work_struct *work)
861 struct delayed_work *dwork = to_delayed_work(work);
862 struct rtsx_pcr *pcr = container_of(dwork, struct rtsx_pcr, idle_work);
864 dev_dbg(&(pcr->pci->dev), "--> %s\n", __func__);
866 mutex_lock(&pcr->pcr_mutex);
868 pcr->state = PDEV_STAT_IDLE;
870 if (pcr->ops->disable_auto_blink)
871 pcr->ops->disable_auto_blink(pcr);
872 if (pcr->ops->turn_off_led)
873 pcr->ops->turn_off_led(pcr);
875 mutex_unlock(&pcr->pcr_mutex);
878 static int rtsx_pci_init_hw(struct rtsx_pcr *pcr)
882 rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr);
884 rtsx_pci_enable_bus_int(pcr);
887 err = rtsx_pci_write_register(pcr, FPDCTL, SSC_POWER_DOWN, 0);
891 /* Wait SSC power stable */
894 if (pcr->ops->optimize_phy) {
895 err = pcr->ops->optimize_phy(pcr);
900 rtsx_pci_init_cmd(pcr);
902 /* Set mcu_cnt to 7 to ensure data can be sampled properly */
903 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_DIV, 0x07, 0x07);
905 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, HOST_SLEEP_STATE, 0x03, 0x00);
906 /* Disable card clock */
907 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_EN, 0x1E, 0);
908 /* Reset ASPM state to default value */
909 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, ASPM_FORCE_CTL, 0x3F, 0);
910 /* Reset delink mode */
911 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CHANGE_LINK_STATE, 0x0A, 0);
912 /* Card driving select */
913 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_DRIVE_SEL,
914 0x07, DRIVER_TYPE_D);
915 /* Enable SSC Clock */
916 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1,
917 0xFF, SSC_8X_EN | SSC_SEL_4M);
918 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL2, 0xFF, 0x12);
919 /* Disable cd_pwr_save */
920 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CHANGE_LINK_STATE, 0x16, 0x10);
921 /* Clear Link Ready Interrupt */
922 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, IRQSTAT0,
923 LINK_RDY_INT, LINK_RDY_INT);
924 /* Enlarge the estimation window of PERST# glitch
925 * to reduce the chance of invalid card interrupt
927 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PERST_GLITCH_WIDTH, 0xFF, 0x80);
928 /* Update RC oscillator to 400k
929 * bit[0] F_HIGH: for RC oscillator, Rst_value is 1'b1
932 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, RCCTL, 0x01, 0x00);
933 /* Set interrupt write clear
934 * bit 1: U_elbi_if_rd_clr_en
935 * 1: Enable ELBI interrupt[31:22] & [7:0] flag read clear
936 * 0: ELBI interrupt flag[31:22] & [7:0] only can be write clear
938 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, NFTS_TX_CTRL, 0x02, 0);
939 /* Force CLKREQ# PIN to drive 0 to request clock */
940 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0x08, 0x08);
942 err = rtsx_pci_send_cmd(pcr, 100);
946 /* Enable clk_request_n to enable clock power management */
947 rtsx_pci_write_config_byte(pcr, 0x81, 1);
948 /* Enter L1 when host tx idle */
949 rtsx_pci_write_config_byte(pcr, 0x70F, 0x5B);
951 if (pcr->ops->extra_init_hw) {
952 err = pcr->ops->extra_init_hw(pcr);
960 static int rtsx_pci_init_chip(struct rtsx_pcr *pcr)
964 spin_lock_init(&pcr->lock);
965 mutex_init(&pcr->pcr_mutex);
967 switch (PCI_PID(pcr)) {
970 rts5209_init_params(pcr);
974 rts5229_init_params(pcr);
978 rtl8411_init_params(pcr);
982 dev_dbg(&(pcr->pci->dev), "PID: 0x%04x, IC version: 0x%02x\n",
983 PCI_PID(pcr), pcr->ic_version);
985 pcr->slots = kcalloc(pcr->num_slots, sizeof(struct rtsx_slot),
990 pcr->state = PDEV_STAT_IDLE;
991 err = rtsx_pci_init_hw(pcr);
1000 static int __devinit rtsx_pci_probe(struct pci_dev *pcidev,
1001 const struct pci_device_id *id)
1003 struct rtsx_pcr *pcr;
1004 struct pcr_handle *handle;
1008 dev_dbg(&(pcidev->dev),
1009 ": Realtek PCI-E Card Reader found at %s [%04x:%04x] (rev %x)\n",
1010 pci_name(pcidev), (int)pcidev->vendor, (int)pcidev->device,
1011 (int)pcidev->revision);
1013 ret = pci_enable_device(pcidev);
1017 ret = pci_request_regions(pcidev, DRV_NAME_RTSX_PCI);
1021 pcr = kzalloc(sizeof(*pcr), GFP_KERNEL);
1027 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1034 if (!idr_pre_get(&rtsx_pci_idr, GFP_KERNEL)) {
1039 spin_lock(&rtsx_pci_lock);
1040 ret = idr_get_new(&rtsx_pci_idr, pcr, &pcr->id);
1041 spin_unlock(&rtsx_pci_lock);
1046 dev_set_drvdata(&pcidev->dev, handle);
1048 len = pci_resource_len(pcidev, 0);
1049 base = pci_resource_start(pcidev, 0);
1050 pcr->remap_addr = ioremap_nocache(base, len);
1051 if (!pcr->remap_addr) {
1056 pcr->rtsx_resv_buf = dma_alloc_coherent(&(pcidev->dev),
1057 RTSX_RESV_BUF_LEN, &(pcr->rtsx_resv_buf_addr),
1059 if (pcr->rtsx_resv_buf == NULL) {
1063 pcr->host_cmds_ptr = pcr->rtsx_resv_buf;
1064 pcr->host_cmds_addr = pcr->rtsx_resv_buf_addr;
1065 pcr->host_sg_tbl_ptr = pcr->rtsx_resv_buf + HOST_CMDS_BUF_LEN;
1066 pcr->host_sg_tbl_addr = pcr->rtsx_resv_buf_addr + HOST_CMDS_BUF_LEN;
1068 pcr->card_inserted = 0;
1069 pcr->card_removed = 0;
1070 INIT_DELAYED_WORK(&pcr->carddet_work, rtsx_pci_card_detect);
1071 INIT_DELAYED_WORK(&pcr->idle_work, rtsx_pci_idle_work);
1073 pcr->msi_en = msi_en;
1075 ret = pci_enable_msi(pcidev);
1077 pcr->msi_en = false;
1080 ret = rtsx_pci_acquire_irq(pcr);
1084 pci_set_master(pcidev);
1085 synchronize_irq(pcr->irq);
1087 ret = rtsx_pci_init_chip(pcr);
1091 for (i = 0; i < ARRAY_SIZE(rtsx_pcr_cells); i++) {
1092 rtsx_pcr_cells[i].platform_data = handle;
1093 rtsx_pcr_cells[i].pdata_size = sizeof(*handle);
1095 ret = mfd_add_devices(&pcidev->dev, pcr->id, rtsx_pcr_cells,
1096 ARRAY_SIZE(rtsx_pcr_cells), NULL, 0, NULL);
1100 schedule_delayed_work(&pcr->idle_work, msecs_to_jiffies(200));
1105 free_irq(pcr->irq, (void *)pcr);
1107 dma_free_coherent(&(pcr->pci->dev), RTSX_RESV_BUF_LEN,
1108 pcr->rtsx_resv_buf, pcr->rtsx_resv_buf_addr);
1110 iounmap(pcr->remap_addr);
1112 dev_set_drvdata(&pcidev->dev, NULL);
1118 pci_release_regions(pcidev);
1120 pci_disable_device(pcidev);
1125 static void __devexit rtsx_pci_remove(struct pci_dev *pcidev)
1127 struct pcr_handle *handle = pci_get_drvdata(pcidev);
1128 struct rtsx_pcr *pcr = handle->pcr;
1130 pcr->remove_pci = true;
1132 cancel_delayed_work(&pcr->carddet_work);
1133 cancel_delayed_work(&pcr->idle_work);
1135 mfd_remove_devices(&pcidev->dev);
1137 dma_free_coherent(&(pcr->pci->dev), RTSX_RESV_BUF_LEN,
1138 pcr->rtsx_resv_buf, pcr->rtsx_resv_buf_addr);
1139 free_irq(pcr->irq, (void *)pcr);
1141 pci_disable_msi(pcr->pci);
1142 iounmap(pcr->remap_addr);
1144 dev_set_drvdata(&pcidev->dev, NULL);
1145 pci_release_regions(pcidev);
1146 pci_disable_device(pcidev);
1148 spin_lock(&rtsx_pci_lock);
1149 idr_remove(&rtsx_pci_idr, pcr->id);
1150 spin_unlock(&rtsx_pci_lock);
1156 dev_dbg(&(pcidev->dev),
1157 ": Realtek PCI-E Card Reader at %s [%04x:%04x] has been removed\n",
1158 pci_name(pcidev), (int)pcidev->vendor, (int)pcidev->device);
1163 static int rtsx_pci_suspend(struct pci_dev *pcidev, pm_message_t state)
1165 struct pcr_handle *handle;
1166 struct rtsx_pcr *pcr;
1169 dev_dbg(&(pcidev->dev), "--> %s\n", __func__);
1171 handle = pci_get_drvdata(pcidev);
1174 cancel_delayed_work(&pcr->carddet_work);
1175 cancel_delayed_work(&pcr->idle_work);
1177 mutex_lock(&pcr->pcr_mutex);
1179 if (pcr->ops->turn_off_led)
1180 pcr->ops->turn_off_led(pcr);
1182 rtsx_pci_writel(pcr, RTSX_BIER, 0);
1185 rtsx_pci_write_register(pcr, PETXCFG, 0x08, 0x08);
1186 rtsx_pci_write_register(pcr, HOST_SLEEP_STATE, 0x03, 0x02);
1188 pci_save_state(pcidev);
1189 pci_enable_wake(pcidev, pci_choose_state(pcidev, state), 0);
1190 pci_disable_device(pcidev);
1191 pci_set_power_state(pcidev, pci_choose_state(pcidev, state));
1193 mutex_unlock(&pcr->pcr_mutex);
1197 static int rtsx_pci_resume(struct pci_dev *pcidev)
1199 struct pcr_handle *handle;
1200 struct rtsx_pcr *pcr;
1203 dev_dbg(&(pcidev->dev), "--> %s\n", __func__);
1205 handle = pci_get_drvdata(pcidev);
1208 mutex_lock(&pcr->pcr_mutex);
1210 pci_set_power_state(pcidev, PCI_D0);
1211 pci_restore_state(pcidev);
1212 ret = pci_enable_device(pcidev);
1215 pci_set_master(pcidev);
1217 ret = rtsx_pci_write_register(pcr, HOST_SLEEP_STATE, 0x03, 0x00);
1221 ret = rtsx_pci_init_hw(pcr);
1225 schedule_delayed_work(&pcr->idle_work, msecs_to_jiffies(200));
1228 mutex_unlock(&pcr->pcr_mutex);
1232 #else /* CONFIG_PM */
1234 #define rtsx_pci_suspend NULL
1235 #define rtsx_pci_resume NULL
1237 #endif /* CONFIG_PM */
1239 static struct pci_driver rtsx_pci_driver = {
1240 .name = DRV_NAME_RTSX_PCI,
1241 .id_table = rtsx_pci_ids,
1242 .probe = rtsx_pci_probe,
1243 .remove = __devexit_p(rtsx_pci_remove),
1244 .suspend = rtsx_pci_suspend,
1245 .resume = rtsx_pci_resume,
1247 module_pci_driver(rtsx_pci_driver);
1249 MODULE_LICENSE("GPL");
1250 MODULE_AUTHOR("Wei WANG <wei_wang@realsil.com.cn>");
1251 MODULE_DESCRIPTION("Realtek PCI-E Card Reader Driver");