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/slab.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/highmem.h>
28 #include <linux/interrupt.h>
29 #include <linux/delay.h>
30 #include <linux/idr.h>
31 #include <linux/platform_device.h>
32 #include <linux/mfd/core.h>
33 #include <linux/mfd/rtsx_pci.h>
34 #include <asm/unaligned.h>
38 static bool msi_en
= true;
39 module_param(msi_en
, bool, S_IRUGO
| S_IWUSR
);
40 MODULE_PARM_DESC(msi_en
, "Enable MSI");
42 static DEFINE_IDR(rtsx_pci_idr
);
43 static DEFINE_SPINLOCK(rtsx_pci_lock
);
45 static struct mfd_cell rtsx_pcr_cells
[] = {
47 .name
= DRV_NAME_RTSX_PCI_SDMMC
,
50 .name
= DRV_NAME_RTSX_PCI_MS
,
54 static DEFINE_PCI_DEVICE_TABLE(rtsx_pci_ids
) = {
55 { PCI_DEVICE(0x10EC, 0x5209), PCI_CLASS_OTHERS
<< 16, 0xFF0000 },
56 { PCI_DEVICE(0x10EC, 0x5229), PCI_CLASS_OTHERS
<< 16, 0xFF0000 },
57 { PCI_DEVICE(0x10EC, 0x5289), PCI_CLASS_OTHERS
<< 16, 0xFF0000 },
58 { PCI_DEVICE(0x10EC, 0x5227), PCI_CLASS_OTHERS
<< 16, 0xFF0000 },
59 { PCI_DEVICE(0x10EC, 0x5249), PCI_CLASS_OTHERS
<< 16, 0xFF0000 },
60 { PCI_DEVICE(0x10EC, 0x5287), PCI_CLASS_OTHERS
<< 16, 0xFF0000 },
64 MODULE_DEVICE_TABLE(pci
, rtsx_pci_ids
);
66 void rtsx_pci_start_run(struct rtsx_pcr
*pcr
)
68 /* If pci device removed, don't queue idle work any more */
72 if (pcr
->state
!= PDEV_STAT_RUN
) {
73 pcr
->state
= PDEV_STAT_RUN
;
74 if (pcr
->ops
->enable_auto_blink
)
75 pcr
->ops
->enable_auto_blink(pcr
);
78 mod_delayed_work(system_wq
, &pcr
->idle_work
, msecs_to_jiffies(200));
80 EXPORT_SYMBOL_GPL(rtsx_pci_start_run
);
82 int rtsx_pci_write_register(struct rtsx_pcr
*pcr
, u16 addr
, u8 mask
, u8 data
)
85 u32 val
= HAIMR_WRITE_START
;
87 val
|= (u32
)(addr
& 0x3FFF) << 16;
88 val
|= (u32
)mask
<< 8;
91 rtsx_pci_writel(pcr
, RTSX_HAIMR
, val
);
93 for (i
= 0; i
< MAX_RW_REG_CNT
; i
++) {
94 val
= rtsx_pci_readl(pcr
, RTSX_HAIMR
);
95 if ((val
& HAIMR_TRANS_END
) == 0) {
104 EXPORT_SYMBOL_GPL(rtsx_pci_write_register
);
106 int rtsx_pci_read_register(struct rtsx_pcr
*pcr
, u16 addr
, u8
*data
)
108 u32 val
= HAIMR_READ_START
;
111 val
|= (u32
)(addr
& 0x3FFF) << 16;
112 rtsx_pci_writel(pcr
, RTSX_HAIMR
, val
);
114 for (i
= 0; i
< MAX_RW_REG_CNT
; i
++) {
115 val
= rtsx_pci_readl(pcr
, RTSX_HAIMR
);
116 if ((val
& HAIMR_TRANS_END
) == 0)
120 if (i
>= MAX_RW_REG_CNT
)
124 *data
= (u8
)(val
& 0xFF);
128 EXPORT_SYMBOL_GPL(rtsx_pci_read_register
);
130 int rtsx_pci_write_phy_register(struct rtsx_pcr
*pcr
, u8 addr
, u16 val
)
132 int err
, i
, finished
= 0;
135 rtsx_pci_init_cmd(pcr
);
137 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, PHYDATA0
, 0xFF, (u8
)val
);
138 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, PHYDATA1
, 0xFF, (u8
)(val
>> 8));
139 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, PHYADDR
, 0xFF, addr
);
140 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, PHYRWCTL
, 0xFF, 0x81);
142 err
= rtsx_pci_send_cmd(pcr
, 100);
146 for (i
= 0; i
< 100000; i
++) {
147 err
= rtsx_pci_read_register(pcr
, PHYRWCTL
, &tmp
);
162 EXPORT_SYMBOL_GPL(rtsx_pci_write_phy_register
);
164 int rtsx_pci_read_phy_register(struct rtsx_pcr
*pcr
, u8 addr
, u16
*val
)
166 int err
, i
, finished
= 0;
170 rtsx_pci_init_cmd(pcr
);
172 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, PHYADDR
, 0xFF, addr
);
173 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, PHYRWCTL
, 0xFF, 0x80);
175 err
= rtsx_pci_send_cmd(pcr
, 100);
179 for (i
= 0; i
< 100000; i
++) {
180 err
= rtsx_pci_read_register(pcr
, PHYRWCTL
, &tmp
);
193 rtsx_pci_init_cmd(pcr
);
195 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, PHYDATA0
, 0, 0);
196 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, PHYDATA1
, 0, 0);
198 err
= rtsx_pci_send_cmd(pcr
, 100);
202 ptr
= rtsx_pci_get_cmd_data(pcr
);
203 data
= ((u16
)ptr
[1] << 8) | ptr
[0];
210 EXPORT_SYMBOL_GPL(rtsx_pci_read_phy_register
);
212 void rtsx_pci_stop_cmd(struct rtsx_pcr
*pcr
)
214 rtsx_pci_writel(pcr
, RTSX_HCBCTLR
, STOP_CMD
);
215 rtsx_pci_writel(pcr
, RTSX_HDBCTLR
, STOP_DMA
);
217 rtsx_pci_write_register(pcr
, DMACTL
, 0x80, 0x80);
218 rtsx_pci_write_register(pcr
, RBCTL
, 0x80, 0x80);
220 EXPORT_SYMBOL_GPL(rtsx_pci_stop_cmd
);
222 void rtsx_pci_add_cmd(struct rtsx_pcr
*pcr
,
223 u8 cmd_type
, u16 reg_addr
, u8 mask
, u8 data
)
227 u32
*ptr
= (u32
*)(pcr
->host_cmds_ptr
);
229 val
|= (u32
)(cmd_type
& 0x03) << 30;
230 val
|= (u32
)(reg_addr
& 0x3FFF) << 16;
231 val
|= (u32
)mask
<< 8;
234 spin_lock_irqsave(&pcr
->lock
, flags
);
236 if (pcr
->ci
< (HOST_CMDS_BUF_LEN
/ 4)) {
237 put_unaligned_le32(val
, ptr
);
241 spin_unlock_irqrestore(&pcr
->lock
, flags
);
243 EXPORT_SYMBOL_GPL(rtsx_pci_add_cmd
);
245 void rtsx_pci_send_cmd_no_wait(struct rtsx_pcr
*pcr
)
249 rtsx_pci_writel(pcr
, RTSX_HCBAR
, pcr
->host_cmds_addr
);
251 val
|= (u32
)(pcr
->ci
* 4) & 0x00FFFFFF;
252 /* Hardware Auto Response */
254 rtsx_pci_writel(pcr
, RTSX_HCBCTLR
, val
);
256 EXPORT_SYMBOL_GPL(rtsx_pci_send_cmd_no_wait
);
258 int rtsx_pci_send_cmd(struct rtsx_pcr
*pcr
, int timeout
)
260 struct completion trans_done
;
266 spin_lock_irqsave(&pcr
->lock
, flags
);
268 /* set up data structures for the wakeup system */
269 pcr
->done
= &trans_done
;
270 pcr
->trans_result
= TRANS_NOT_READY
;
271 init_completion(&trans_done
);
273 rtsx_pci_writel(pcr
, RTSX_HCBAR
, pcr
->host_cmds_addr
);
275 val
|= (u32
)(pcr
->ci
* 4) & 0x00FFFFFF;
276 /* Hardware Auto Response */
278 rtsx_pci_writel(pcr
, RTSX_HCBCTLR
, val
);
280 spin_unlock_irqrestore(&pcr
->lock
, flags
);
282 /* Wait for TRANS_OK_INT */
283 timeleft
= wait_for_completion_interruptible_timeout(
284 &trans_done
, msecs_to_jiffies(timeout
));
286 dev_dbg(&(pcr
->pci
->dev
), "Timeout (%s %d)\n",
289 goto finish_send_cmd
;
292 spin_lock_irqsave(&pcr
->lock
, flags
);
293 if (pcr
->trans_result
== TRANS_RESULT_FAIL
)
295 else if (pcr
->trans_result
== TRANS_RESULT_OK
)
297 else if (pcr
->trans_result
== TRANS_NO_DEVICE
)
299 spin_unlock_irqrestore(&pcr
->lock
, flags
);
302 spin_lock_irqsave(&pcr
->lock
, flags
);
304 spin_unlock_irqrestore(&pcr
->lock
, flags
);
306 if ((err
< 0) && (err
!= -ENODEV
))
307 rtsx_pci_stop_cmd(pcr
);
310 complete(pcr
->finish_me
);
314 EXPORT_SYMBOL_GPL(rtsx_pci_send_cmd
);
316 static void rtsx_pci_add_sg_tbl(struct rtsx_pcr
*pcr
,
317 dma_addr_t addr
, unsigned int len
, int end
)
319 u64
*ptr
= (u64
*)(pcr
->host_sg_tbl_ptr
) + pcr
->sgi
;
321 u8 option
= SG_VALID
| SG_TRANS_DATA
;
323 dev_dbg(&(pcr
->pci
->dev
), "DMA addr: 0x%x, Len: 0x%x\n",
324 (unsigned int)addr
, len
);
328 val
= ((u64
)addr
<< 32) | ((u64
)len
<< 12) | option
;
330 put_unaligned_le64(val
, ptr
);
334 int rtsx_pci_transfer_data(struct rtsx_pcr
*pcr
, struct scatterlist
*sglist
,
335 int num_sg
, bool read
, int timeout
)
337 struct completion trans_done
;
339 int err
= 0, i
, count
;
342 struct scatterlist
*sg
;
343 enum dma_data_direction dma_dir
;
348 dev_dbg(&(pcr
->pci
->dev
), "--> %s: num_sg = %d\n", __func__
, num_sg
);
350 /* don't transfer data during abort processing */
354 if ((sglist
== NULL
) || (num_sg
<= 0))
358 dir
= DEVICE_TO_HOST
;
359 dma_dir
= DMA_FROM_DEVICE
;
361 dir
= HOST_TO_DEVICE
;
362 dma_dir
= DMA_TO_DEVICE
;
365 count
= dma_map_sg(&(pcr
->pci
->dev
), sglist
, num_sg
, dma_dir
);
367 dev_err(&(pcr
->pci
->dev
), "scatterlist map failed\n");
370 dev_dbg(&(pcr
->pci
->dev
), "DMA mapping count: %d\n", count
);
372 val
= ((u32
)(dir
& 0x01) << 29) | TRIG_DMA
| ADMA_MODE
;
374 for_each_sg(sglist
, sg
, count
, i
) {
375 addr
= sg_dma_address(sg
);
376 len
= sg_dma_len(sg
);
377 rtsx_pci_add_sg_tbl(pcr
, addr
, len
, i
== count
- 1);
380 spin_lock_irqsave(&pcr
->lock
, flags
);
382 pcr
->done
= &trans_done
;
383 pcr
->trans_result
= TRANS_NOT_READY
;
384 init_completion(&trans_done
);
385 rtsx_pci_writel(pcr
, RTSX_HDBAR
, pcr
->host_sg_tbl_addr
);
386 rtsx_pci_writel(pcr
, RTSX_HDBCTLR
, val
);
388 spin_unlock_irqrestore(&pcr
->lock
, flags
);
390 timeleft
= wait_for_completion_interruptible_timeout(
391 &trans_done
, msecs_to_jiffies(timeout
));
393 dev_dbg(&(pcr
->pci
->dev
), "Timeout (%s %d)\n",
399 spin_lock_irqsave(&pcr
->lock
, flags
);
401 if (pcr
->trans_result
== TRANS_RESULT_FAIL
)
403 else if (pcr
->trans_result
== TRANS_NO_DEVICE
)
406 spin_unlock_irqrestore(&pcr
->lock
, flags
);
409 spin_lock_irqsave(&pcr
->lock
, flags
);
411 spin_unlock_irqrestore(&pcr
->lock
, flags
);
413 dma_unmap_sg(&(pcr
->pci
->dev
), sglist
, num_sg
, dma_dir
);
415 if ((err
< 0) && (err
!= -ENODEV
))
416 rtsx_pci_stop_cmd(pcr
);
419 complete(pcr
->finish_me
);
423 EXPORT_SYMBOL_GPL(rtsx_pci_transfer_data
);
425 int rtsx_pci_read_ppbuf(struct rtsx_pcr
*pcr
, u8
*buf
, int buf_len
)
437 for (i
= 0; i
< buf_len
/ 256; i
++) {
438 rtsx_pci_init_cmd(pcr
);
440 for (j
= 0; j
< 256; j
++)
441 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, reg
++, 0, 0);
443 err
= rtsx_pci_send_cmd(pcr
, 250);
447 memcpy(ptr
, rtsx_pci_get_cmd_data(pcr
), 256);
452 rtsx_pci_init_cmd(pcr
);
454 for (j
= 0; j
< buf_len
% 256; j
++)
455 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, reg
++, 0, 0);
457 err
= rtsx_pci_send_cmd(pcr
, 250);
462 memcpy(ptr
, rtsx_pci_get_cmd_data(pcr
), buf_len
% 256);
466 EXPORT_SYMBOL_GPL(rtsx_pci_read_ppbuf
);
468 int rtsx_pci_write_ppbuf(struct rtsx_pcr
*pcr
, u8
*buf
, int buf_len
)
480 for (i
= 0; i
< buf_len
/ 256; i
++) {
481 rtsx_pci_init_cmd(pcr
);
483 for (j
= 0; j
< 256; j
++) {
484 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
489 err
= rtsx_pci_send_cmd(pcr
, 250);
495 rtsx_pci_init_cmd(pcr
);
497 for (j
= 0; j
< buf_len
% 256; j
++) {
498 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
503 err
= rtsx_pci_send_cmd(pcr
, 250);
510 EXPORT_SYMBOL_GPL(rtsx_pci_write_ppbuf
);
512 static int rtsx_pci_set_pull_ctl(struct rtsx_pcr
*pcr
, const u32
*tbl
)
516 rtsx_pci_init_cmd(pcr
);
518 while (*tbl
& 0xFFFF0000) {
519 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
520 (u16
)(*tbl
>> 16), 0xFF, (u8
)(*tbl
));
524 err
= rtsx_pci_send_cmd(pcr
, 100);
531 int rtsx_pci_card_pull_ctl_enable(struct rtsx_pcr
*pcr
, int card
)
535 if (card
== RTSX_SD_CARD
)
536 tbl
= pcr
->sd_pull_ctl_enable_tbl
;
537 else if (card
== RTSX_MS_CARD
)
538 tbl
= pcr
->ms_pull_ctl_enable_tbl
;
542 return rtsx_pci_set_pull_ctl(pcr
, tbl
);
544 EXPORT_SYMBOL_GPL(rtsx_pci_card_pull_ctl_enable
);
546 int rtsx_pci_card_pull_ctl_disable(struct rtsx_pcr
*pcr
, int card
)
550 if (card
== RTSX_SD_CARD
)
551 tbl
= pcr
->sd_pull_ctl_disable_tbl
;
552 else if (card
== RTSX_MS_CARD
)
553 tbl
= pcr
->ms_pull_ctl_disable_tbl
;
558 return rtsx_pci_set_pull_ctl(pcr
, tbl
);
560 EXPORT_SYMBOL_GPL(rtsx_pci_card_pull_ctl_disable
);
562 static void rtsx_pci_enable_bus_int(struct rtsx_pcr
*pcr
)
564 pcr
->bier
= TRANS_OK_INT_EN
| TRANS_FAIL_INT_EN
| SD_INT_EN
;
566 if (pcr
->num_slots
> 1)
567 pcr
->bier
|= MS_INT_EN
;
569 /* Enable Bus Interrupt */
570 rtsx_pci_writel(pcr
, RTSX_BIER
, pcr
->bier
);
572 dev_dbg(&(pcr
->pci
->dev
), "RTSX_BIER: 0x%08x\n", pcr
->bier
);
575 static inline u8
double_ssc_depth(u8 depth
)
577 return ((depth
> 1) ? (depth
- 1) : depth
);
580 static u8
revise_ssc_depth(u8 ssc_depth
, u8 div
)
582 if (div
> CLK_DIV_1
) {
583 if (ssc_depth
> (div
- 1))
584 ssc_depth
-= (div
- 1);
586 ssc_depth
= SSC_DEPTH_4M
;
592 int rtsx_pci_switch_clock(struct rtsx_pcr
*pcr
, unsigned int card_clock
,
593 u8 ssc_depth
, bool initial_mode
, bool double_clk
, bool vpclk
)
596 u8 n
, clk_divider
, mcu_cnt
, div
;
598 [RTSX_SSC_DEPTH_4M
] = SSC_DEPTH_4M
,
599 [RTSX_SSC_DEPTH_2M
] = SSC_DEPTH_2M
,
600 [RTSX_SSC_DEPTH_1M
] = SSC_DEPTH_1M
,
601 [RTSX_SSC_DEPTH_500K
] = SSC_DEPTH_500K
,
602 [RTSX_SSC_DEPTH_250K
] = SSC_DEPTH_250K
,
606 /* We use 250k(around) here, in initial stage */
607 clk_divider
= SD_CLK_DIVIDE_128
;
608 card_clock
= 30000000;
610 clk_divider
= SD_CLK_DIVIDE_0
;
612 err
= rtsx_pci_write_register(pcr
, SD_CFG1
,
613 SD_CLK_DIVIDE_MASK
, clk_divider
);
617 card_clock
/= 1000000;
618 dev_dbg(&(pcr
->pci
->dev
), "Switch card clock to %dMHz\n", card_clock
);
621 if (!initial_mode
&& double_clk
)
622 clk
= card_clock
* 2;
623 dev_dbg(&(pcr
->pci
->dev
),
624 "Internal SSC clock: %dMHz (cur_clock = %d)\n",
625 clk
, pcr
->cur_clock
);
627 if (clk
== pcr
->cur_clock
)
630 if (pcr
->ops
->conv_clk_and_div_n
)
631 n
= (u8
)pcr
->ops
->conv_clk_and_div_n(clk
, CLK_TO_DIV_N
);
634 if ((clk
<= 2) || (n
> MAX_DIV_N_PCR
))
637 mcu_cnt
= (u8
)(125/clk
+ 3);
641 /* Make sure that the SSC clock div_n is not less than MIN_DIV_N_PCR */
643 while ((n
< MIN_DIV_N_PCR
) && (div
< CLK_DIV_8
)) {
644 if (pcr
->ops
->conv_clk_and_div_n
) {
645 int dbl_clk
= pcr
->ops
->conv_clk_and_div_n(n
,
647 n
= (u8
)pcr
->ops
->conv_clk_and_div_n(dbl_clk
,
654 dev_dbg(&(pcr
->pci
->dev
), "n = %d, div = %d\n", n
, div
);
656 ssc_depth
= depth
[ssc_depth
];
658 ssc_depth
= double_ssc_depth(ssc_depth
);
660 ssc_depth
= revise_ssc_depth(ssc_depth
, div
);
661 dev_dbg(&(pcr
->pci
->dev
), "ssc_depth = %d\n", ssc_depth
);
663 rtsx_pci_init_cmd(pcr
);
664 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
665 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
666 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_DIV
,
667 0xFF, (div
<< 4) | mcu_cnt
);
668 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_CTL1
, SSC_RSTB
, 0);
669 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_CTL2
,
670 SSC_DEPTH_MASK
, ssc_depth
);
671 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_DIV_N_0
, 0xFF, n
);
672 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_CTL1
, SSC_RSTB
, SSC_RSTB
);
674 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
,
676 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
,
677 PHASE_NOT_RESET
, PHASE_NOT_RESET
);
680 err
= rtsx_pci_send_cmd(pcr
, 2000);
684 /* Wait SSC clock stable */
686 err
= rtsx_pci_write_register(pcr
, CLK_CTL
, CLK_LOW_FREQ
, 0);
690 pcr
->cur_clock
= clk
;
693 EXPORT_SYMBOL_GPL(rtsx_pci_switch_clock
);
695 int rtsx_pci_card_power_on(struct rtsx_pcr
*pcr
, int card
)
697 if (pcr
->ops
->card_power_on
)
698 return pcr
->ops
->card_power_on(pcr
, card
);
702 EXPORT_SYMBOL_GPL(rtsx_pci_card_power_on
);
704 int rtsx_pci_card_power_off(struct rtsx_pcr
*pcr
, int card
)
706 if (pcr
->ops
->card_power_off
)
707 return pcr
->ops
->card_power_off(pcr
, card
);
711 EXPORT_SYMBOL_GPL(rtsx_pci_card_power_off
);
713 int rtsx_pci_card_exclusive_check(struct rtsx_pcr
*pcr
, int card
)
715 unsigned int cd_mask
[] = {
716 [RTSX_SD_CARD
] = SD_EXIST
,
717 [RTSX_MS_CARD
] = MS_EXIST
721 /* When using single PMOS, accessing card is not permitted
722 * if the existing card is not the designated one.
724 if (pcr
->card_exist
& (~cd_mask
[card
]))
730 EXPORT_SYMBOL_GPL(rtsx_pci_card_exclusive_check
);
732 int rtsx_pci_switch_output_voltage(struct rtsx_pcr
*pcr
, u8 voltage
)
734 if (pcr
->ops
->switch_output_voltage
)
735 return pcr
->ops
->switch_output_voltage(pcr
, voltage
);
739 EXPORT_SYMBOL_GPL(rtsx_pci_switch_output_voltage
);
741 unsigned int rtsx_pci_card_exist(struct rtsx_pcr
*pcr
)
745 val
= rtsx_pci_readl(pcr
, RTSX_BIPR
);
746 if (pcr
->ops
->cd_deglitch
)
747 val
= pcr
->ops
->cd_deglitch(pcr
);
751 EXPORT_SYMBOL_GPL(rtsx_pci_card_exist
);
753 void rtsx_pci_complete_unfinished_transfer(struct rtsx_pcr
*pcr
)
755 struct completion finish
;
757 pcr
->finish_me
= &finish
;
758 init_completion(&finish
);
763 if (!pcr
->remove_pci
)
764 rtsx_pci_stop_cmd(pcr
);
766 wait_for_completion_interruptible_timeout(&finish
,
767 msecs_to_jiffies(2));
768 pcr
->finish_me
= NULL
;
770 EXPORT_SYMBOL_GPL(rtsx_pci_complete_unfinished_transfer
);
772 static void rtsx_pci_card_detect(struct work_struct
*work
)
774 struct delayed_work
*dwork
;
775 struct rtsx_pcr
*pcr
;
777 unsigned int card_detect
= 0, card_inserted
, card_removed
;
780 dwork
= to_delayed_work(work
);
781 pcr
= container_of(dwork
, struct rtsx_pcr
, carddet_work
);
783 dev_dbg(&(pcr
->pci
->dev
), "--> %s\n", __func__
);
785 mutex_lock(&pcr
->pcr_mutex
);
786 spin_lock_irqsave(&pcr
->lock
, flags
);
788 irq_status
= rtsx_pci_readl(pcr
, RTSX_BIPR
);
789 dev_dbg(&(pcr
->pci
->dev
), "irq_status: 0x%08x\n", irq_status
);
791 irq_status
&= CARD_EXIST
;
792 card_inserted
= pcr
->card_inserted
& irq_status
;
793 card_removed
= pcr
->card_removed
;
794 pcr
->card_inserted
= 0;
795 pcr
->card_removed
= 0;
797 spin_unlock_irqrestore(&pcr
->lock
, flags
);
799 if (card_inserted
|| card_removed
) {
800 dev_dbg(&(pcr
->pci
->dev
),
801 "card_inserted: 0x%x, card_removed: 0x%x\n",
802 card_inserted
, card_removed
);
804 if (pcr
->ops
->cd_deglitch
)
805 card_inserted
= pcr
->ops
->cd_deglitch(pcr
);
807 card_detect
= card_inserted
| card_removed
;
809 pcr
->card_exist
|= card_inserted
;
810 pcr
->card_exist
&= ~card_removed
;
813 mutex_unlock(&pcr
->pcr_mutex
);
815 if ((card_detect
& SD_EXIST
) && pcr
->slots
[RTSX_SD_CARD
].card_event
)
816 pcr
->slots
[RTSX_SD_CARD
].card_event(
817 pcr
->slots
[RTSX_SD_CARD
].p_dev
);
818 if ((card_detect
& MS_EXIST
) && pcr
->slots
[RTSX_MS_CARD
].card_event
)
819 pcr
->slots
[RTSX_MS_CARD
].card_event(
820 pcr
->slots
[RTSX_MS_CARD
].p_dev
);
823 static irqreturn_t
rtsx_pci_isr(int irq
, void *dev_id
)
825 struct rtsx_pcr
*pcr
= dev_id
;
831 spin_lock(&pcr
->lock
);
833 int_reg
= rtsx_pci_readl(pcr
, RTSX_BIPR
);
834 /* Clear interrupt flag */
835 rtsx_pci_writel(pcr
, RTSX_BIPR
, int_reg
);
836 if ((int_reg
& pcr
->bier
) == 0) {
837 spin_unlock(&pcr
->lock
);
840 if (int_reg
== 0xFFFFFFFF) {
841 spin_unlock(&pcr
->lock
);
845 int_reg
&= (pcr
->bier
| 0x7FFFFF);
847 if (int_reg
& SD_INT
) {
848 if (int_reg
& SD_EXIST
) {
849 pcr
->card_inserted
|= SD_EXIST
;
851 pcr
->card_removed
|= SD_EXIST
;
852 pcr
->card_inserted
&= ~SD_EXIST
;
856 if (int_reg
& MS_INT
) {
857 if (int_reg
& MS_EXIST
) {
858 pcr
->card_inserted
|= MS_EXIST
;
860 pcr
->card_removed
|= MS_EXIST
;
861 pcr
->card_inserted
&= ~MS_EXIST
;
865 if (int_reg
& (NEED_COMPLETE_INT
| DELINK_INT
)) {
866 if (int_reg
& (TRANS_FAIL_INT
| DELINK_INT
)) {
867 pcr
->trans_result
= TRANS_RESULT_FAIL
;
870 } else if (int_reg
& TRANS_OK_INT
) {
871 pcr
->trans_result
= TRANS_RESULT_OK
;
877 if (pcr
->card_inserted
|| pcr
->card_removed
)
878 schedule_delayed_work(&pcr
->carddet_work
,
879 msecs_to_jiffies(200));
881 spin_unlock(&pcr
->lock
);
885 static int rtsx_pci_acquire_irq(struct rtsx_pcr
*pcr
)
887 dev_info(&(pcr
->pci
->dev
), "%s: pcr->msi_en = %d, pci->irq = %d\n",
888 __func__
, pcr
->msi_en
, pcr
->pci
->irq
);
890 if (request_irq(pcr
->pci
->irq
, rtsx_pci_isr
,
891 pcr
->msi_en
? 0 : IRQF_SHARED
,
892 DRV_NAME_RTSX_PCI
, pcr
)) {
893 dev_err(&(pcr
->pci
->dev
),
894 "rtsx_sdmmc: unable to grab IRQ %d, disabling device\n",
899 pcr
->irq
= pcr
->pci
->irq
;
900 pci_intx(pcr
->pci
, !pcr
->msi_en
);
905 static void rtsx_pci_idle_work(struct work_struct
*work
)
907 struct delayed_work
*dwork
= to_delayed_work(work
);
908 struct rtsx_pcr
*pcr
= container_of(dwork
, struct rtsx_pcr
, idle_work
);
910 dev_dbg(&(pcr
->pci
->dev
), "--> %s\n", __func__
);
912 mutex_lock(&pcr
->pcr_mutex
);
914 pcr
->state
= PDEV_STAT_IDLE
;
916 if (pcr
->ops
->disable_auto_blink
)
917 pcr
->ops
->disable_auto_blink(pcr
);
918 if (pcr
->ops
->turn_off_led
)
919 pcr
->ops
->turn_off_led(pcr
);
921 mutex_unlock(&pcr
->pcr_mutex
);
924 static int rtsx_pci_init_hw(struct rtsx_pcr
*pcr
)
928 rtsx_pci_writel(pcr
, RTSX_HCBAR
, pcr
->host_cmds_addr
);
930 rtsx_pci_enable_bus_int(pcr
);
933 err
= rtsx_pci_write_register(pcr
, FPDCTL
, SSC_POWER_DOWN
, 0);
937 /* Wait SSC power stable */
940 if (pcr
->ops
->optimize_phy
) {
941 err
= pcr
->ops
->optimize_phy(pcr
);
946 rtsx_pci_init_cmd(pcr
);
948 /* Set mcu_cnt to 7 to ensure data can be sampled properly */
949 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_DIV
, 0x07, 0x07);
951 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, HOST_SLEEP_STATE
, 0x03, 0x00);
952 /* Disable card clock */
953 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_EN
, 0x1E, 0);
954 /* Reset ASPM state to default value */
955 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, ASPM_FORCE_CTL
, 0x3F, 0);
956 /* Reset delink mode */
957 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CHANGE_LINK_STATE
, 0x0A, 0);
958 /* Card driving select */
959 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD30_DRIVE_SEL
,
960 0x07, DRIVER_TYPE_D
);
961 /* Enable SSC Clock */
962 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_CTL1
,
963 0xFF, SSC_8X_EN
| SSC_SEL_4M
);
964 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_CTL2
, 0xFF, 0x12);
965 /* Disable cd_pwr_save */
966 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CHANGE_LINK_STATE
, 0x16, 0x10);
967 /* Clear Link Ready Interrupt */
968 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, IRQSTAT0
,
969 LINK_RDY_INT
, LINK_RDY_INT
);
970 /* Enlarge the estimation window of PERST# glitch
971 * to reduce the chance of invalid card interrupt
973 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, PERST_GLITCH_WIDTH
, 0xFF, 0x80);
974 /* Update RC oscillator to 400k
975 * bit[0] F_HIGH: for RC oscillator, Rst_value is 1'b1
978 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, RCCTL
, 0x01, 0x00);
979 /* Set interrupt write clear
980 * bit 1: U_elbi_if_rd_clr_en
981 * 1: Enable ELBI interrupt[31:22] & [7:0] flag read clear
982 * 0: ELBI interrupt flag[31:22] & [7:0] only can be write clear
984 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, NFTS_TX_CTRL
, 0x02, 0);
985 /* Force CLKREQ# PIN to drive 0 to request clock */
986 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, PETXCFG
, 0x08, 0x08);
988 err
= rtsx_pci_send_cmd(pcr
, 100);
992 /* Enable clk_request_n to enable clock power management */
993 rtsx_pci_write_config_byte(pcr
, 0x81, 1);
994 /* Enter L1 when host tx idle */
995 rtsx_pci_write_config_byte(pcr
, 0x70F, 0x5B);
997 if (pcr
->ops
->extra_init_hw
) {
998 err
= pcr
->ops
->extra_init_hw(pcr
);
1003 /* No CD interrupt if probing driver with card inserted.
1004 * So we need to initialize pcr->card_exist here.
1006 if (pcr
->ops
->cd_deglitch
)
1007 pcr
->card_exist
= pcr
->ops
->cd_deglitch(pcr
);
1009 pcr
->card_exist
= rtsx_pci_readl(pcr
, RTSX_BIPR
) & CARD_EXIST
;
1014 static int rtsx_pci_init_chip(struct rtsx_pcr
*pcr
)
1018 spin_lock_init(&pcr
->lock
);
1019 mutex_init(&pcr
->pcr_mutex
);
1021 switch (PCI_PID(pcr
)) {
1024 rts5209_init_params(pcr
);
1028 rts5229_init_params(pcr
);
1032 rtl8411_init_params(pcr
);
1036 rts5227_init_params(pcr
);
1040 rts5249_init_params(pcr
);
1044 rtl8411b_init_params(pcr
);
1048 dev_dbg(&(pcr
->pci
->dev
), "PID: 0x%04x, IC version: 0x%02x\n",
1049 PCI_PID(pcr
), pcr
->ic_version
);
1051 pcr
->slots
= kcalloc(pcr
->num_slots
, sizeof(struct rtsx_slot
),
1056 pcr
->state
= PDEV_STAT_IDLE
;
1057 err
= rtsx_pci_init_hw(pcr
);
1066 static int rtsx_pci_probe(struct pci_dev
*pcidev
,
1067 const struct pci_device_id
*id
)
1069 struct rtsx_pcr
*pcr
;
1070 struct pcr_handle
*handle
;
1074 dev_dbg(&(pcidev
->dev
),
1075 ": Realtek PCI-E Card Reader found at %s [%04x:%04x] (rev %x)\n",
1076 pci_name(pcidev
), (int)pcidev
->vendor
, (int)pcidev
->device
,
1077 (int)pcidev
->revision
);
1079 ret
= pci_set_dma_mask(pcidev
, DMA_BIT_MASK(32));
1083 ret
= pci_enable_device(pcidev
);
1087 ret
= pci_request_regions(pcidev
, DRV_NAME_RTSX_PCI
);
1091 pcr
= kzalloc(sizeof(*pcr
), GFP_KERNEL
);
1097 handle
= kzalloc(sizeof(*handle
), GFP_KERNEL
);
1104 idr_preload(GFP_KERNEL
);
1105 spin_lock(&rtsx_pci_lock
);
1106 ret
= idr_alloc(&rtsx_pci_idr
, pcr
, 0, 0, GFP_NOWAIT
);
1109 spin_unlock(&rtsx_pci_lock
);
1115 dev_set_drvdata(&pcidev
->dev
, handle
);
1117 len
= pci_resource_len(pcidev
, 0);
1118 base
= pci_resource_start(pcidev
, 0);
1119 pcr
->remap_addr
= ioremap_nocache(base
, len
);
1120 if (!pcr
->remap_addr
) {
1125 pcr
->rtsx_resv_buf
= dma_alloc_coherent(&(pcidev
->dev
),
1126 RTSX_RESV_BUF_LEN
, &(pcr
->rtsx_resv_buf_addr
),
1128 if (pcr
->rtsx_resv_buf
== NULL
) {
1132 pcr
->host_cmds_ptr
= pcr
->rtsx_resv_buf
;
1133 pcr
->host_cmds_addr
= pcr
->rtsx_resv_buf_addr
;
1134 pcr
->host_sg_tbl_ptr
= pcr
->rtsx_resv_buf
+ HOST_CMDS_BUF_LEN
;
1135 pcr
->host_sg_tbl_addr
= pcr
->rtsx_resv_buf_addr
+ HOST_CMDS_BUF_LEN
;
1137 pcr
->card_inserted
= 0;
1138 pcr
->card_removed
= 0;
1139 INIT_DELAYED_WORK(&pcr
->carddet_work
, rtsx_pci_card_detect
);
1140 INIT_DELAYED_WORK(&pcr
->idle_work
, rtsx_pci_idle_work
);
1142 pcr
->msi_en
= msi_en
;
1144 ret
= pci_enable_msi(pcidev
);
1146 pcr
->msi_en
= false;
1149 ret
= rtsx_pci_acquire_irq(pcr
);
1153 pci_set_master(pcidev
);
1154 synchronize_irq(pcr
->irq
);
1156 ret
= rtsx_pci_init_chip(pcr
);
1160 for (i
= 0; i
< ARRAY_SIZE(rtsx_pcr_cells
); i
++) {
1161 rtsx_pcr_cells
[i
].platform_data
= handle
;
1162 rtsx_pcr_cells
[i
].pdata_size
= sizeof(*handle
);
1164 ret
= mfd_add_devices(&pcidev
->dev
, pcr
->id
, rtsx_pcr_cells
,
1165 ARRAY_SIZE(rtsx_pcr_cells
), NULL
, 0, NULL
);
1169 schedule_delayed_work(&pcr
->idle_work
, msecs_to_jiffies(200));
1174 free_irq(pcr
->irq
, (void *)pcr
);
1177 pci_disable_msi(pcr
->pci
);
1178 dma_free_coherent(&(pcr
->pci
->dev
), RTSX_RESV_BUF_LEN
,
1179 pcr
->rtsx_resv_buf
, pcr
->rtsx_resv_buf_addr
);
1181 iounmap(pcr
->remap_addr
);
1183 dev_set_drvdata(&pcidev
->dev
, NULL
);
1189 pci_release_regions(pcidev
);
1191 pci_disable_device(pcidev
);
1196 static void rtsx_pci_remove(struct pci_dev
*pcidev
)
1198 struct pcr_handle
*handle
= pci_get_drvdata(pcidev
);
1199 struct rtsx_pcr
*pcr
= handle
->pcr
;
1201 pcr
->remove_pci
= true;
1203 cancel_delayed_work(&pcr
->carddet_work
);
1204 cancel_delayed_work(&pcr
->idle_work
);
1206 mfd_remove_devices(&pcidev
->dev
);
1208 dma_free_coherent(&(pcr
->pci
->dev
), RTSX_RESV_BUF_LEN
,
1209 pcr
->rtsx_resv_buf
, pcr
->rtsx_resv_buf_addr
);
1210 free_irq(pcr
->irq
, (void *)pcr
);
1212 pci_disable_msi(pcr
->pci
);
1213 iounmap(pcr
->remap_addr
);
1215 dev_set_drvdata(&pcidev
->dev
, NULL
);
1216 pci_release_regions(pcidev
);
1217 pci_disable_device(pcidev
);
1219 spin_lock(&rtsx_pci_lock
);
1220 idr_remove(&rtsx_pci_idr
, pcr
->id
);
1221 spin_unlock(&rtsx_pci_lock
);
1227 dev_dbg(&(pcidev
->dev
),
1228 ": Realtek PCI-E Card Reader at %s [%04x:%04x] has been removed\n",
1229 pci_name(pcidev
), (int)pcidev
->vendor
, (int)pcidev
->device
);
1234 static int rtsx_pci_suspend(struct pci_dev
*pcidev
, pm_message_t state
)
1236 struct pcr_handle
*handle
;
1237 struct rtsx_pcr
*pcr
;
1240 dev_dbg(&(pcidev
->dev
), "--> %s\n", __func__
);
1242 handle
= pci_get_drvdata(pcidev
);
1245 cancel_delayed_work(&pcr
->carddet_work
);
1246 cancel_delayed_work(&pcr
->idle_work
);
1248 mutex_lock(&pcr
->pcr_mutex
);
1250 if (pcr
->ops
->turn_off_led
)
1251 pcr
->ops
->turn_off_led(pcr
);
1253 rtsx_pci_writel(pcr
, RTSX_BIER
, 0);
1256 rtsx_pci_write_register(pcr
, PETXCFG
, 0x08, 0x08);
1257 rtsx_pci_write_register(pcr
, HOST_SLEEP_STATE
, 0x03, 0x02);
1259 pci_save_state(pcidev
);
1260 pci_enable_wake(pcidev
, pci_choose_state(pcidev
, state
), 0);
1261 pci_disable_device(pcidev
);
1262 pci_set_power_state(pcidev
, pci_choose_state(pcidev
, state
));
1264 mutex_unlock(&pcr
->pcr_mutex
);
1268 static int rtsx_pci_resume(struct pci_dev
*pcidev
)
1270 struct pcr_handle
*handle
;
1271 struct rtsx_pcr
*pcr
;
1274 dev_dbg(&(pcidev
->dev
), "--> %s\n", __func__
);
1276 handle
= pci_get_drvdata(pcidev
);
1279 mutex_lock(&pcr
->pcr_mutex
);
1281 pci_set_power_state(pcidev
, PCI_D0
);
1282 pci_restore_state(pcidev
);
1283 ret
= pci_enable_device(pcidev
);
1286 pci_set_master(pcidev
);
1288 ret
= rtsx_pci_write_register(pcr
, HOST_SLEEP_STATE
, 0x03, 0x00);
1292 ret
= rtsx_pci_init_hw(pcr
);
1296 schedule_delayed_work(&pcr
->idle_work
, msecs_to_jiffies(200));
1299 mutex_unlock(&pcr
->pcr_mutex
);
1303 #else /* CONFIG_PM */
1305 #define rtsx_pci_suspend NULL
1306 #define rtsx_pci_resume NULL
1308 #endif /* CONFIG_PM */
1310 static struct pci_driver rtsx_pci_driver
= {
1311 .name
= DRV_NAME_RTSX_PCI
,
1312 .id_table
= rtsx_pci_ids
,
1313 .probe
= rtsx_pci_probe
,
1314 .remove
= rtsx_pci_remove
,
1315 .suspend
= rtsx_pci_suspend
,
1316 .resume
= rtsx_pci_resume
,
1318 module_pci_driver(rtsx_pci_driver
);
1320 MODULE_LICENSE("GPL");
1321 MODULE_AUTHOR("Wei WANG <wei_wang@realsil.com.cn>");
1322 MODULE_DESCRIPTION("Realtek PCI-E Card Reader Driver");