1 /* Driver for Realtek PCI-Express card reader
3 * Copyright(c) 2009-2013 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>
22 #include <linux/pci.h>
23 #include <linux/module.h>
24 #include <linux/slab.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/rtsx_pci.h>
33 #include <linux/mmc/card.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 const struct pci_device_id 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, 0x522A), PCI_CLASS_OTHERS
<< 16, 0xFF0000 },
60 { PCI_DEVICE(0x10EC, 0x5249), PCI_CLASS_OTHERS
<< 16, 0xFF0000 },
61 { PCI_DEVICE(0x10EC, 0x5287), PCI_CLASS_OTHERS
<< 16, 0xFF0000 },
62 { PCI_DEVICE(0x10EC, 0x5286), PCI_CLASS_OTHERS
<< 16, 0xFF0000 },
63 { PCI_DEVICE(0x10EC, 0x524A), PCI_CLASS_OTHERS
<< 16, 0xFF0000 },
64 { PCI_DEVICE(0x10EC, 0x525A), PCI_CLASS_OTHERS
<< 16, 0xFF0000 },
65 { PCI_DEVICE(0x10EC, 0x5260), PCI_CLASS_OTHERS
<< 16, 0xFF0000 },
69 MODULE_DEVICE_TABLE(pci
, rtsx_pci_ids
);
71 static inline void rtsx_pci_enable_aspm(struct rtsx_pcr
*pcr
)
73 rtsx_pci_update_cfg_byte(pcr
, pcr
->pcie_cap
+ PCI_EXP_LNKCTL
,
77 static inline void rtsx_pci_disable_aspm(struct rtsx_pcr
*pcr
)
79 rtsx_pci_update_cfg_byte(pcr
, pcr
->pcie_cap
+ PCI_EXP_LNKCTL
,
83 static int rtsx_comm_set_ltr_latency(struct rtsx_pcr
*pcr
, u32 latency
)
85 rtsx_pci_write_register(pcr
, MSGTXDATA0
,
86 MASK_8_BIT_DEF
, (u8
) (latency
& 0xFF));
87 rtsx_pci_write_register(pcr
, MSGTXDATA1
,
88 MASK_8_BIT_DEF
, (u8
)((latency
>> 8) & 0xFF));
89 rtsx_pci_write_register(pcr
, MSGTXDATA2
,
90 MASK_8_BIT_DEF
, (u8
)((latency
>> 16) & 0xFF));
91 rtsx_pci_write_register(pcr
, MSGTXDATA3
,
92 MASK_8_BIT_DEF
, (u8
)((latency
>> 24) & 0xFF));
93 rtsx_pci_write_register(pcr
, LTR_CTL
, LTR_TX_EN_MASK
|
94 LTR_LATENCY_MODE_MASK
, LTR_TX_EN_1
| LTR_LATENCY_MODE_SW
);
99 int rtsx_set_ltr_latency(struct rtsx_pcr
*pcr
, u32 latency
)
101 if (pcr
->ops
->set_ltr_latency
)
102 return pcr
->ops
->set_ltr_latency(pcr
, latency
);
104 return rtsx_comm_set_ltr_latency(pcr
, latency
);
107 static void rtsx_comm_set_aspm(struct rtsx_pcr
*pcr
, bool enable
)
109 struct rtsx_cr_option
*option
= &pcr
->option
;
111 if (pcr
->aspm_enabled
== enable
)
114 if (option
->dev_aspm_mode
== DEV_ASPM_DYNAMIC
) {
116 rtsx_pci_enable_aspm(pcr
);
118 rtsx_pci_disable_aspm(pcr
);
119 } else if (option
->dev_aspm_mode
== DEV_ASPM_BACKDOOR
) {
120 u8 mask
= FORCE_ASPM_VAL_MASK
;
125 rtsx_pci_write_register(pcr
, ASPM_FORCE_CTL
, mask
, val
);
128 pcr
->aspm_enabled
= enable
;
131 static void rtsx_disable_aspm(struct rtsx_pcr
*pcr
)
133 if (pcr
->ops
->set_aspm
)
134 pcr
->ops
->set_aspm(pcr
, false);
136 rtsx_comm_set_aspm(pcr
, false);
139 int rtsx_set_l1off_sub(struct rtsx_pcr
*pcr
, u8 val
)
141 rtsx_pci_write_register(pcr
, L1SUB_CONFIG3
, 0xFF, val
);
146 static void rtsx_set_l1off_sub_cfg_d0(struct rtsx_pcr
*pcr
, int active
)
148 if (pcr
->ops
->set_l1off_cfg_sub_d0
)
149 pcr
->ops
->set_l1off_cfg_sub_d0(pcr
, active
);
152 static void rtsx_comm_pm_full_on(struct rtsx_pcr
*pcr
)
154 struct rtsx_cr_option
*option
= &pcr
->option
;
156 rtsx_disable_aspm(pcr
);
158 if (option
->ltr_enabled
)
159 rtsx_set_ltr_latency(pcr
, option
->ltr_active_latency
);
161 if (rtsx_check_dev_flag(pcr
, LTR_L1SS_PWR_GATE_EN
))
162 rtsx_set_l1off_sub_cfg_d0(pcr
, 1);
165 static void rtsx_pm_full_on(struct rtsx_pcr
*pcr
)
167 if (pcr
->ops
->full_on
)
168 pcr
->ops
->full_on(pcr
);
170 rtsx_comm_pm_full_on(pcr
);
173 void rtsx_pci_start_run(struct rtsx_pcr
*pcr
)
175 /* If pci device removed, don't queue idle work any more */
179 if (pcr
->state
!= PDEV_STAT_RUN
) {
180 pcr
->state
= PDEV_STAT_RUN
;
181 if (pcr
->ops
->enable_auto_blink
)
182 pcr
->ops
->enable_auto_blink(pcr
);
183 rtsx_pm_full_on(pcr
);
186 mod_delayed_work(system_wq
, &pcr
->idle_work
, msecs_to_jiffies(200));
188 EXPORT_SYMBOL_GPL(rtsx_pci_start_run
);
190 int rtsx_pci_write_register(struct rtsx_pcr
*pcr
, u16 addr
, u8 mask
, u8 data
)
193 u32 val
= HAIMR_WRITE_START
;
195 val
|= (u32
)(addr
& 0x3FFF) << 16;
196 val
|= (u32
)mask
<< 8;
199 rtsx_pci_writel(pcr
, RTSX_HAIMR
, val
);
201 for (i
= 0; i
< MAX_RW_REG_CNT
; i
++) {
202 val
= rtsx_pci_readl(pcr
, RTSX_HAIMR
);
203 if ((val
& HAIMR_TRANS_END
) == 0) {
212 EXPORT_SYMBOL_GPL(rtsx_pci_write_register
);
214 int rtsx_pci_read_register(struct rtsx_pcr
*pcr
, u16 addr
, u8
*data
)
216 u32 val
= HAIMR_READ_START
;
219 val
|= (u32
)(addr
& 0x3FFF) << 16;
220 rtsx_pci_writel(pcr
, RTSX_HAIMR
, val
);
222 for (i
= 0; i
< MAX_RW_REG_CNT
; i
++) {
223 val
= rtsx_pci_readl(pcr
, RTSX_HAIMR
);
224 if ((val
& HAIMR_TRANS_END
) == 0)
228 if (i
>= MAX_RW_REG_CNT
)
232 *data
= (u8
)(val
& 0xFF);
236 EXPORT_SYMBOL_GPL(rtsx_pci_read_register
);
238 int __rtsx_pci_write_phy_register(struct rtsx_pcr
*pcr
, u8 addr
, u16 val
)
240 int err
, i
, finished
= 0;
243 rtsx_pci_init_cmd(pcr
);
245 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, PHYDATA0
, 0xFF, (u8
)val
);
246 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, PHYDATA1
, 0xFF, (u8
)(val
>> 8));
247 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, PHYADDR
, 0xFF, addr
);
248 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, PHYRWCTL
, 0xFF, 0x81);
250 err
= rtsx_pci_send_cmd(pcr
, 100);
254 for (i
= 0; i
< 100000; i
++) {
255 err
= rtsx_pci_read_register(pcr
, PHYRWCTL
, &tmp
);
271 int rtsx_pci_write_phy_register(struct rtsx_pcr
*pcr
, u8 addr
, u16 val
)
273 if (pcr
->ops
->write_phy
)
274 return pcr
->ops
->write_phy(pcr
, addr
, val
);
276 return __rtsx_pci_write_phy_register(pcr
, addr
, val
);
278 EXPORT_SYMBOL_GPL(rtsx_pci_write_phy_register
);
280 int __rtsx_pci_read_phy_register(struct rtsx_pcr
*pcr
, u8 addr
, u16
*val
)
282 int err
, i
, finished
= 0;
286 rtsx_pci_init_cmd(pcr
);
288 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, PHYADDR
, 0xFF, addr
);
289 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, PHYRWCTL
, 0xFF, 0x80);
291 err
= rtsx_pci_send_cmd(pcr
, 100);
295 for (i
= 0; i
< 100000; i
++) {
296 err
= rtsx_pci_read_register(pcr
, PHYRWCTL
, &tmp
);
309 rtsx_pci_init_cmd(pcr
);
311 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, PHYDATA0
, 0, 0);
312 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, PHYDATA1
, 0, 0);
314 err
= rtsx_pci_send_cmd(pcr
, 100);
318 ptr
= rtsx_pci_get_cmd_data(pcr
);
319 data
= ((u16
)ptr
[1] << 8) | ptr
[0];
327 int rtsx_pci_read_phy_register(struct rtsx_pcr
*pcr
, u8 addr
, u16
*val
)
329 if (pcr
->ops
->read_phy
)
330 return pcr
->ops
->read_phy(pcr
, addr
, val
);
332 return __rtsx_pci_read_phy_register(pcr
, addr
, val
);
334 EXPORT_SYMBOL_GPL(rtsx_pci_read_phy_register
);
336 void rtsx_pci_stop_cmd(struct rtsx_pcr
*pcr
)
338 if (pcr
->ops
->stop_cmd
)
339 return pcr
->ops
->stop_cmd(pcr
);
341 rtsx_pci_writel(pcr
, RTSX_HCBCTLR
, STOP_CMD
);
342 rtsx_pci_writel(pcr
, RTSX_HDBCTLR
, STOP_DMA
);
344 rtsx_pci_write_register(pcr
, DMACTL
, 0x80, 0x80);
345 rtsx_pci_write_register(pcr
, RBCTL
, 0x80, 0x80);
347 EXPORT_SYMBOL_GPL(rtsx_pci_stop_cmd
);
349 void rtsx_pci_add_cmd(struct rtsx_pcr
*pcr
,
350 u8 cmd_type
, u16 reg_addr
, u8 mask
, u8 data
)
354 u32
*ptr
= (u32
*)(pcr
->host_cmds_ptr
);
356 val
|= (u32
)(cmd_type
& 0x03) << 30;
357 val
|= (u32
)(reg_addr
& 0x3FFF) << 16;
358 val
|= (u32
)mask
<< 8;
361 spin_lock_irqsave(&pcr
->lock
, flags
);
363 if (pcr
->ci
< (HOST_CMDS_BUF_LEN
/ 4)) {
364 put_unaligned_le32(val
, ptr
);
368 spin_unlock_irqrestore(&pcr
->lock
, flags
);
370 EXPORT_SYMBOL_GPL(rtsx_pci_add_cmd
);
372 void rtsx_pci_send_cmd_no_wait(struct rtsx_pcr
*pcr
)
376 rtsx_pci_writel(pcr
, RTSX_HCBAR
, pcr
->host_cmds_addr
);
378 val
|= (u32
)(pcr
->ci
* 4) & 0x00FFFFFF;
379 /* Hardware Auto Response */
381 rtsx_pci_writel(pcr
, RTSX_HCBCTLR
, val
);
383 EXPORT_SYMBOL_GPL(rtsx_pci_send_cmd_no_wait
);
385 int rtsx_pci_send_cmd(struct rtsx_pcr
*pcr
, int timeout
)
387 struct completion trans_done
;
393 spin_lock_irqsave(&pcr
->lock
, flags
);
395 /* set up data structures for the wakeup system */
396 pcr
->done
= &trans_done
;
397 pcr
->trans_result
= TRANS_NOT_READY
;
398 init_completion(&trans_done
);
400 rtsx_pci_writel(pcr
, RTSX_HCBAR
, pcr
->host_cmds_addr
);
402 val
|= (u32
)(pcr
->ci
* 4) & 0x00FFFFFF;
403 /* Hardware Auto Response */
405 rtsx_pci_writel(pcr
, RTSX_HCBCTLR
, val
);
407 spin_unlock_irqrestore(&pcr
->lock
, flags
);
409 /* Wait for TRANS_OK_INT */
410 timeleft
= wait_for_completion_interruptible_timeout(
411 &trans_done
, msecs_to_jiffies(timeout
));
413 pcr_dbg(pcr
, "Timeout (%s %d)\n", __func__
, __LINE__
);
415 goto finish_send_cmd
;
418 spin_lock_irqsave(&pcr
->lock
, flags
);
419 if (pcr
->trans_result
== TRANS_RESULT_FAIL
)
421 else if (pcr
->trans_result
== TRANS_RESULT_OK
)
423 else if (pcr
->trans_result
== TRANS_NO_DEVICE
)
425 spin_unlock_irqrestore(&pcr
->lock
, flags
);
428 spin_lock_irqsave(&pcr
->lock
, flags
);
430 spin_unlock_irqrestore(&pcr
->lock
, flags
);
432 if ((err
< 0) && (err
!= -ENODEV
))
433 rtsx_pci_stop_cmd(pcr
);
436 complete(pcr
->finish_me
);
440 EXPORT_SYMBOL_GPL(rtsx_pci_send_cmd
);
442 static void rtsx_pci_add_sg_tbl(struct rtsx_pcr
*pcr
,
443 dma_addr_t addr
, unsigned int len
, int end
)
445 u64
*ptr
= (u64
*)(pcr
->host_sg_tbl_ptr
) + pcr
->sgi
;
447 u8 option
= RTSX_SG_VALID
| RTSX_SG_TRANS_DATA
;
449 pcr_dbg(pcr
, "DMA addr: 0x%x, Len: 0x%x\n", (unsigned int)addr
, len
);
452 option
|= RTSX_SG_END
;
453 val
= ((u64
)addr
<< 32) | ((u64
)len
<< 12) | option
;
455 put_unaligned_le64(val
, ptr
);
459 int rtsx_pci_transfer_data(struct rtsx_pcr
*pcr
, struct scatterlist
*sglist
,
460 int num_sg
, bool read
, int timeout
)
464 pcr_dbg(pcr
, "--> %s: num_sg = %d\n", __func__
, num_sg
);
465 count
= rtsx_pci_dma_map_sg(pcr
, sglist
, num_sg
, read
);
468 pcr_dbg(pcr
, "DMA mapping count: %d\n", count
);
470 err
= rtsx_pci_dma_transfer(pcr
, sglist
, count
, read
, timeout
);
472 rtsx_pci_dma_unmap_sg(pcr
, sglist
, num_sg
, read
);
476 EXPORT_SYMBOL_GPL(rtsx_pci_transfer_data
);
478 int rtsx_pci_dma_map_sg(struct rtsx_pcr
*pcr
, struct scatterlist
*sglist
,
479 int num_sg
, bool read
)
481 enum dma_data_direction dir
= read
? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
486 if ((sglist
== NULL
) || (num_sg
<= 0))
489 return dma_map_sg(&(pcr
->pci
->dev
), sglist
, num_sg
, dir
);
491 EXPORT_SYMBOL_GPL(rtsx_pci_dma_map_sg
);
493 void rtsx_pci_dma_unmap_sg(struct rtsx_pcr
*pcr
, struct scatterlist
*sglist
,
494 int num_sg
, bool read
)
496 enum dma_data_direction dir
= read
? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
498 dma_unmap_sg(&(pcr
->pci
->dev
), sglist
, num_sg
, dir
);
500 EXPORT_SYMBOL_GPL(rtsx_pci_dma_unmap_sg
);
502 int rtsx_pci_dma_transfer(struct rtsx_pcr
*pcr
, struct scatterlist
*sglist
,
503 int count
, bool read
, int timeout
)
505 struct completion trans_done
;
506 struct scatterlist
*sg
;
513 u8 dir
= read
? DEVICE_TO_HOST
: HOST_TO_DEVICE
;
518 if ((sglist
== NULL
) || (count
< 1))
521 val
= ((u32
)(dir
& 0x01) << 29) | TRIG_DMA
| ADMA_MODE
;
523 for_each_sg(sglist
, sg
, count
, i
) {
524 addr
= sg_dma_address(sg
);
525 len
= sg_dma_len(sg
);
526 rtsx_pci_add_sg_tbl(pcr
, addr
, len
, i
== count
- 1);
529 spin_lock_irqsave(&pcr
->lock
, flags
);
531 pcr
->done
= &trans_done
;
532 pcr
->trans_result
= TRANS_NOT_READY
;
533 init_completion(&trans_done
);
534 rtsx_pci_writel(pcr
, RTSX_HDBAR
, pcr
->host_sg_tbl_addr
);
535 rtsx_pci_writel(pcr
, RTSX_HDBCTLR
, val
);
537 spin_unlock_irqrestore(&pcr
->lock
, flags
);
539 timeleft
= wait_for_completion_interruptible_timeout(
540 &trans_done
, msecs_to_jiffies(timeout
));
542 pcr_dbg(pcr
, "Timeout (%s %d)\n", __func__
, __LINE__
);
547 spin_lock_irqsave(&pcr
->lock
, flags
);
548 if (pcr
->trans_result
== TRANS_RESULT_FAIL
) {
550 if (pcr
->dma_error_count
< RTS_MAX_TIMES_FREQ_REDUCTION
)
551 pcr
->dma_error_count
++;
554 else if (pcr
->trans_result
== TRANS_NO_DEVICE
)
556 spin_unlock_irqrestore(&pcr
->lock
, flags
);
559 spin_lock_irqsave(&pcr
->lock
, flags
);
561 spin_unlock_irqrestore(&pcr
->lock
, flags
);
563 if ((err
< 0) && (err
!= -ENODEV
))
564 rtsx_pci_stop_cmd(pcr
);
567 complete(pcr
->finish_me
);
571 EXPORT_SYMBOL_GPL(rtsx_pci_dma_transfer
);
573 int rtsx_pci_read_ppbuf(struct rtsx_pcr
*pcr
, u8
*buf
, int buf_len
)
585 for (i
= 0; i
< buf_len
/ 256; i
++) {
586 rtsx_pci_init_cmd(pcr
);
588 for (j
= 0; j
< 256; j
++)
589 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, reg
++, 0, 0);
591 err
= rtsx_pci_send_cmd(pcr
, 250);
595 memcpy(ptr
, rtsx_pci_get_cmd_data(pcr
), 256);
600 rtsx_pci_init_cmd(pcr
);
602 for (j
= 0; j
< buf_len
% 256; j
++)
603 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, reg
++, 0, 0);
605 err
= rtsx_pci_send_cmd(pcr
, 250);
610 memcpy(ptr
, rtsx_pci_get_cmd_data(pcr
), buf_len
% 256);
614 EXPORT_SYMBOL_GPL(rtsx_pci_read_ppbuf
);
616 int rtsx_pci_write_ppbuf(struct rtsx_pcr
*pcr
, u8
*buf
, int buf_len
)
628 for (i
= 0; i
< buf_len
/ 256; i
++) {
629 rtsx_pci_init_cmd(pcr
);
631 for (j
= 0; j
< 256; j
++) {
632 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
637 err
= rtsx_pci_send_cmd(pcr
, 250);
643 rtsx_pci_init_cmd(pcr
);
645 for (j
= 0; j
< buf_len
% 256; j
++) {
646 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
651 err
= rtsx_pci_send_cmd(pcr
, 250);
658 EXPORT_SYMBOL_GPL(rtsx_pci_write_ppbuf
);
660 static int rtsx_pci_set_pull_ctl(struct rtsx_pcr
*pcr
, const u32
*tbl
)
662 rtsx_pci_init_cmd(pcr
);
664 while (*tbl
& 0xFFFF0000) {
665 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
666 (u16
)(*tbl
>> 16), 0xFF, (u8
)(*tbl
));
670 return rtsx_pci_send_cmd(pcr
, 100);
673 int rtsx_pci_card_pull_ctl_enable(struct rtsx_pcr
*pcr
, int card
)
677 if (card
== RTSX_SD_CARD
)
678 tbl
= pcr
->sd_pull_ctl_enable_tbl
;
679 else if (card
== RTSX_MS_CARD
)
680 tbl
= pcr
->ms_pull_ctl_enable_tbl
;
684 return rtsx_pci_set_pull_ctl(pcr
, tbl
);
686 EXPORT_SYMBOL_GPL(rtsx_pci_card_pull_ctl_enable
);
688 int rtsx_pci_card_pull_ctl_disable(struct rtsx_pcr
*pcr
, int card
)
692 if (card
== RTSX_SD_CARD
)
693 tbl
= pcr
->sd_pull_ctl_disable_tbl
;
694 else if (card
== RTSX_MS_CARD
)
695 tbl
= pcr
->ms_pull_ctl_disable_tbl
;
700 return rtsx_pci_set_pull_ctl(pcr
, tbl
);
702 EXPORT_SYMBOL_GPL(rtsx_pci_card_pull_ctl_disable
);
704 static void rtsx_pci_enable_bus_int(struct rtsx_pcr
*pcr
)
706 pcr
->bier
= TRANS_OK_INT_EN
| TRANS_FAIL_INT_EN
| SD_INT_EN
;
708 if (pcr
->num_slots
> 1)
709 pcr
->bier
|= MS_INT_EN
;
711 /* Enable Bus Interrupt */
712 rtsx_pci_writel(pcr
, RTSX_BIER
, pcr
->bier
);
714 pcr_dbg(pcr
, "RTSX_BIER: 0x%08x\n", pcr
->bier
);
717 static inline u8
double_ssc_depth(u8 depth
)
719 return ((depth
> 1) ? (depth
- 1) : depth
);
722 static u8
revise_ssc_depth(u8 ssc_depth
, u8 div
)
724 if (div
> CLK_DIV_1
) {
725 if (ssc_depth
> (div
- 1))
726 ssc_depth
-= (div
- 1);
728 ssc_depth
= SSC_DEPTH_4M
;
734 int rtsx_pci_switch_clock(struct rtsx_pcr
*pcr
, unsigned int card_clock
,
735 u8 ssc_depth
, bool initial_mode
, bool double_clk
, bool vpclk
)
738 u8 n
, clk_divider
, mcu_cnt
, div
;
739 static const u8 depth
[] = {
740 [RTSX_SSC_DEPTH_4M
] = SSC_DEPTH_4M
,
741 [RTSX_SSC_DEPTH_2M
] = SSC_DEPTH_2M
,
742 [RTSX_SSC_DEPTH_1M
] = SSC_DEPTH_1M
,
743 [RTSX_SSC_DEPTH_500K
] = SSC_DEPTH_500K
,
744 [RTSX_SSC_DEPTH_250K
] = SSC_DEPTH_250K
,
748 /* We use 250k(around) here, in initial stage */
749 clk_divider
= SD_CLK_DIVIDE_128
;
750 card_clock
= 30000000;
752 clk_divider
= SD_CLK_DIVIDE_0
;
754 err
= rtsx_pci_write_register(pcr
, SD_CFG1
,
755 SD_CLK_DIVIDE_MASK
, clk_divider
);
759 /* Reduce card clock by 20MHz each time a DMA transfer error occurs */
760 if (card_clock
== UHS_SDR104_MAX_DTR
&&
761 pcr
->dma_error_count
&&
762 PCI_PID(pcr
) == RTS5227_DEVICE_ID
)
763 card_clock
= UHS_SDR104_MAX_DTR
-
764 (pcr
->dma_error_count
* 20000000);
766 card_clock
/= 1000000;
767 pcr_dbg(pcr
, "Switch card clock to %dMHz\n", card_clock
);
770 if (!initial_mode
&& double_clk
)
771 clk
= card_clock
* 2;
772 pcr_dbg(pcr
, "Internal SSC clock: %dMHz (cur_clock = %d)\n",
773 clk
, pcr
->cur_clock
);
775 if (clk
== pcr
->cur_clock
)
778 if (pcr
->ops
->conv_clk_and_div_n
)
779 n
= (u8
)pcr
->ops
->conv_clk_and_div_n(clk
, CLK_TO_DIV_N
);
782 if ((clk
<= 2) || (n
> MAX_DIV_N_PCR
))
785 mcu_cnt
= (u8
)(125/clk
+ 3);
789 /* Make sure that the SSC clock div_n is not less than MIN_DIV_N_PCR */
791 while ((n
< MIN_DIV_N_PCR
) && (div
< CLK_DIV_8
)) {
792 if (pcr
->ops
->conv_clk_and_div_n
) {
793 int dbl_clk
= pcr
->ops
->conv_clk_and_div_n(n
,
795 n
= (u8
)pcr
->ops
->conv_clk_and_div_n(dbl_clk
,
802 pcr_dbg(pcr
, "n = %d, div = %d\n", n
, div
);
804 ssc_depth
= depth
[ssc_depth
];
806 ssc_depth
= double_ssc_depth(ssc_depth
);
808 ssc_depth
= revise_ssc_depth(ssc_depth
, div
);
809 pcr_dbg(pcr
, "ssc_depth = %d\n", ssc_depth
);
811 rtsx_pci_init_cmd(pcr
);
812 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
813 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
814 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_DIV
,
815 0xFF, (div
<< 4) | mcu_cnt
);
816 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_CTL1
, SSC_RSTB
, 0);
817 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_CTL2
,
818 SSC_DEPTH_MASK
, ssc_depth
);
819 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_DIV_N_0
, 0xFF, n
);
820 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_CTL1
, SSC_RSTB
, SSC_RSTB
);
822 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
,
824 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
,
825 PHASE_NOT_RESET
, PHASE_NOT_RESET
);
828 err
= rtsx_pci_send_cmd(pcr
, 2000);
832 /* Wait SSC clock stable */
833 udelay(SSC_CLOCK_STABLE_WAIT
);
834 err
= rtsx_pci_write_register(pcr
, CLK_CTL
, CLK_LOW_FREQ
, 0);
838 pcr
->cur_clock
= clk
;
841 EXPORT_SYMBOL_GPL(rtsx_pci_switch_clock
);
843 int rtsx_pci_card_power_on(struct rtsx_pcr
*pcr
, int card
)
845 if (pcr
->ops
->card_power_on
)
846 return pcr
->ops
->card_power_on(pcr
, card
);
850 EXPORT_SYMBOL_GPL(rtsx_pci_card_power_on
);
852 int rtsx_pci_card_power_off(struct rtsx_pcr
*pcr
, int card
)
854 if (pcr
->ops
->card_power_off
)
855 return pcr
->ops
->card_power_off(pcr
, card
);
859 EXPORT_SYMBOL_GPL(rtsx_pci_card_power_off
);
861 int rtsx_pci_card_exclusive_check(struct rtsx_pcr
*pcr
, int card
)
863 static const unsigned int cd_mask
[] = {
864 [RTSX_SD_CARD
] = SD_EXIST
,
865 [RTSX_MS_CARD
] = MS_EXIST
868 if (!(pcr
->flags
& PCR_MS_PMOS
)) {
869 /* When using single PMOS, accessing card is not permitted
870 * if the existing card is not the designated one.
872 if (pcr
->card_exist
& (~cd_mask
[card
]))
878 EXPORT_SYMBOL_GPL(rtsx_pci_card_exclusive_check
);
880 int rtsx_pci_switch_output_voltage(struct rtsx_pcr
*pcr
, u8 voltage
)
882 if (pcr
->ops
->switch_output_voltage
)
883 return pcr
->ops
->switch_output_voltage(pcr
, voltage
);
887 EXPORT_SYMBOL_GPL(rtsx_pci_switch_output_voltage
);
889 unsigned int rtsx_pci_card_exist(struct rtsx_pcr
*pcr
)
893 val
= rtsx_pci_readl(pcr
, RTSX_BIPR
);
894 if (pcr
->ops
->cd_deglitch
)
895 val
= pcr
->ops
->cd_deglitch(pcr
);
899 EXPORT_SYMBOL_GPL(rtsx_pci_card_exist
);
901 void rtsx_pci_complete_unfinished_transfer(struct rtsx_pcr
*pcr
)
903 struct completion finish
;
905 pcr
->finish_me
= &finish
;
906 init_completion(&finish
);
911 if (!pcr
->remove_pci
)
912 rtsx_pci_stop_cmd(pcr
);
914 wait_for_completion_interruptible_timeout(&finish
,
915 msecs_to_jiffies(2));
916 pcr
->finish_me
= NULL
;
918 EXPORT_SYMBOL_GPL(rtsx_pci_complete_unfinished_transfer
);
920 static void rtsx_pci_card_detect(struct work_struct
*work
)
922 struct delayed_work
*dwork
;
923 struct rtsx_pcr
*pcr
;
925 unsigned int card_detect
= 0, card_inserted
, card_removed
;
928 dwork
= to_delayed_work(work
);
929 pcr
= container_of(dwork
, struct rtsx_pcr
, carddet_work
);
931 pcr_dbg(pcr
, "--> %s\n", __func__
);
933 mutex_lock(&pcr
->pcr_mutex
);
934 spin_lock_irqsave(&pcr
->lock
, flags
);
936 irq_status
= rtsx_pci_readl(pcr
, RTSX_BIPR
);
937 pcr_dbg(pcr
, "irq_status: 0x%08x\n", irq_status
);
939 irq_status
&= CARD_EXIST
;
940 card_inserted
= pcr
->card_inserted
& irq_status
;
941 card_removed
= pcr
->card_removed
;
942 pcr
->card_inserted
= 0;
943 pcr
->card_removed
= 0;
945 spin_unlock_irqrestore(&pcr
->lock
, flags
);
947 if (card_inserted
|| card_removed
) {
948 pcr_dbg(pcr
, "card_inserted: 0x%x, card_removed: 0x%x\n",
949 card_inserted
, card_removed
);
951 if (pcr
->ops
->cd_deglitch
)
952 card_inserted
= pcr
->ops
->cd_deglitch(pcr
);
954 card_detect
= card_inserted
| card_removed
;
956 pcr
->card_exist
|= card_inserted
;
957 pcr
->card_exist
&= ~card_removed
;
960 mutex_unlock(&pcr
->pcr_mutex
);
962 if ((card_detect
& SD_EXIST
) && pcr
->slots
[RTSX_SD_CARD
].card_event
)
963 pcr
->slots
[RTSX_SD_CARD
].card_event(
964 pcr
->slots
[RTSX_SD_CARD
].p_dev
);
965 if ((card_detect
& MS_EXIST
) && pcr
->slots
[RTSX_MS_CARD
].card_event
)
966 pcr
->slots
[RTSX_MS_CARD
].card_event(
967 pcr
->slots
[RTSX_MS_CARD
].p_dev
);
970 static void rtsx_pci_process_ocp(struct rtsx_pcr
*pcr
)
972 if (pcr
->ops
->process_ocp
)
973 pcr
->ops
->process_ocp(pcr
);
976 static int rtsx_pci_process_ocp_interrupt(struct rtsx_pcr
*pcr
)
978 if (pcr
->option
.ocp_en
)
979 rtsx_pci_process_ocp(pcr
);
984 static irqreturn_t
rtsx_pci_isr(int irq
, void *dev_id
)
986 struct rtsx_pcr
*pcr
= dev_id
;
992 spin_lock(&pcr
->lock
);
994 int_reg
= rtsx_pci_readl(pcr
, RTSX_BIPR
);
995 /* Clear interrupt flag */
996 rtsx_pci_writel(pcr
, RTSX_BIPR
, int_reg
);
997 if ((int_reg
& pcr
->bier
) == 0) {
998 spin_unlock(&pcr
->lock
);
1001 if (int_reg
== 0xFFFFFFFF) {
1002 spin_unlock(&pcr
->lock
);
1006 int_reg
&= (pcr
->bier
| 0x7FFFFF);
1008 if (int_reg
& SD_OC_INT
)
1009 rtsx_pci_process_ocp_interrupt(pcr
);
1011 if (int_reg
& SD_INT
) {
1012 if (int_reg
& SD_EXIST
) {
1013 pcr
->card_inserted
|= SD_EXIST
;
1015 pcr
->card_removed
|= SD_EXIST
;
1016 pcr
->card_inserted
&= ~SD_EXIST
;
1018 pcr
->dma_error_count
= 0;
1021 if (int_reg
& MS_INT
) {
1022 if (int_reg
& MS_EXIST
) {
1023 pcr
->card_inserted
|= MS_EXIST
;
1025 pcr
->card_removed
|= MS_EXIST
;
1026 pcr
->card_inserted
&= ~MS_EXIST
;
1030 if (int_reg
& (NEED_COMPLETE_INT
| DELINK_INT
)) {
1031 if (int_reg
& (TRANS_FAIL_INT
| DELINK_INT
)) {
1032 pcr
->trans_result
= TRANS_RESULT_FAIL
;
1034 complete(pcr
->done
);
1035 } else if (int_reg
& TRANS_OK_INT
) {
1036 pcr
->trans_result
= TRANS_RESULT_OK
;
1038 complete(pcr
->done
);
1042 if (pcr
->card_inserted
|| pcr
->card_removed
)
1043 schedule_delayed_work(&pcr
->carddet_work
,
1044 msecs_to_jiffies(200));
1046 spin_unlock(&pcr
->lock
);
1050 static int rtsx_pci_acquire_irq(struct rtsx_pcr
*pcr
)
1052 pcr_dbg(pcr
, "%s: pcr->msi_en = %d, pci->irq = %d\n",
1053 __func__
, pcr
->msi_en
, pcr
->pci
->irq
);
1055 if (request_irq(pcr
->pci
->irq
, rtsx_pci_isr
,
1056 pcr
->msi_en
? 0 : IRQF_SHARED
,
1057 DRV_NAME_RTSX_PCI
, pcr
)) {
1058 dev_err(&(pcr
->pci
->dev
),
1059 "rtsx_sdmmc: unable to grab IRQ %d, disabling device\n",
1064 pcr
->irq
= pcr
->pci
->irq
;
1065 pci_intx(pcr
->pci
, !pcr
->msi_en
);
1070 static void rtsx_enable_aspm(struct rtsx_pcr
*pcr
)
1072 if (pcr
->ops
->set_aspm
)
1073 pcr
->ops
->set_aspm(pcr
, true);
1075 rtsx_comm_set_aspm(pcr
, true);
1078 static void rtsx_comm_pm_power_saving(struct rtsx_pcr
*pcr
)
1080 struct rtsx_cr_option
*option
= &pcr
->option
;
1082 if (option
->ltr_enabled
) {
1083 u32 latency
= option
->ltr_l1off_latency
;
1085 if (rtsx_check_dev_flag(pcr
, L1_SNOOZE_TEST_EN
))
1086 mdelay(option
->l1_snooze_delay
);
1088 rtsx_set_ltr_latency(pcr
, latency
);
1091 if (rtsx_check_dev_flag(pcr
, LTR_L1SS_PWR_GATE_EN
))
1092 rtsx_set_l1off_sub_cfg_d0(pcr
, 0);
1094 rtsx_enable_aspm(pcr
);
1097 static void rtsx_pm_power_saving(struct rtsx_pcr
*pcr
)
1099 if (pcr
->ops
->power_saving
)
1100 pcr
->ops
->power_saving(pcr
);
1102 rtsx_comm_pm_power_saving(pcr
);
1105 static void rtsx_pci_idle_work(struct work_struct
*work
)
1107 struct delayed_work
*dwork
= to_delayed_work(work
);
1108 struct rtsx_pcr
*pcr
= container_of(dwork
, struct rtsx_pcr
, idle_work
);
1110 pcr_dbg(pcr
, "--> %s\n", __func__
);
1112 mutex_lock(&pcr
->pcr_mutex
);
1114 pcr
->state
= PDEV_STAT_IDLE
;
1116 if (pcr
->ops
->disable_auto_blink
)
1117 pcr
->ops
->disable_auto_blink(pcr
);
1118 if (pcr
->ops
->turn_off_led
)
1119 pcr
->ops
->turn_off_led(pcr
);
1121 rtsx_pm_power_saving(pcr
);
1123 mutex_unlock(&pcr
->pcr_mutex
);
1127 static void rtsx_pci_power_off(struct rtsx_pcr
*pcr
, u8 pm_state
)
1129 if (pcr
->ops
->turn_off_led
)
1130 pcr
->ops
->turn_off_led(pcr
);
1132 rtsx_pci_writel(pcr
, RTSX_BIER
, 0);
1135 rtsx_pci_write_register(pcr
, PETXCFG
, 0x08, 0x08);
1136 rtsx_pci_write_register(pcr
, HOST_SLEEP_STATE
, 0x03, pm_state
);
1138 if (pcr
->ops
->force_power_down
)
1139 pcr
->ops
->force_power_down(pcr
, pm_state
);
1143 void rtsx_pci_enable_ocp(struct rtsx_pcr
*pcr
)
1145 u8 val
= SD_OCP_INT_EN
| SD_DETECT_EN
;
1147 if (pcr
->ops
->enable_ocp
)
1148 pcr
->ops
->enable_ocp(pcr
);
1150 rtsx_pci_write_register(pcr
, REG_OCPCTL
, 0xFF, val
);
1154 void rtsx_pci_disable_ocp(struct rtsx_pcr
*pcr
)
1156 u8 mask
= SD_OCP_INT_EN
| SD_DETECT_EN
;
1158 if (pcr
->ops
->disable_ocp
)
1159 pcr
->ops
->disable_ocp(pcr
);
1161 rtsx_pci_write_register(pcr
, REG_OCPCTL
, mask
, 0);
1164 void rtsx_pci_init_ocp(struct rtsx_pcr
*pcr
)
1166 if (pcr
->ops
->init_ocp
) {
1167 pcr
->ops
->init_ocp(pcr
);
1169 struct rtsx_cr_option
*option
= &(pcr
->option
);
1171 if (option
->ocp_en
) {
1172 u8 val
= option
->sd_400mA_ocp_thd
;
1174 rtsx_pci_write_register(pcr
, FPDCTL
, OC_POWER_DOWN
, 0);
1175 rtsx_pci_write_register(pcr
, REG_OCPPARA1
,
1176 SD_OCP_TIME_MASK
, SD_OCP_TIME_800
);
1177 rtsx_pci_write_register(pcr
, REG_OCPPARA2
,
1178 SD_OCP_THD_MASK
, val
);
1179 rtsx_pci_write_register(pcr
, REG_OCPGLITCH
,
1180 SD_OCP_GLITCH_MASK
, pcr
->hw_param
.ocp_glitch
);
1181 rtsx_pci_enable_ocp(pcr
);
1184 rtsx_pci_write_register(pcr
, FPDCTL
, OC_POWER_DOWN
,
1190 int rtsx_pci_get_ocpstat(struct rtsx_pcr
*pcr
, u8
*val
)
1192 if (pcr
->ops
->get_ocpstat
)
1193 return pcr
->ops
->get_ocpstat(pcr
, val
);
1195 return rtsx_pci_read_register(pcr
, REG_OCPSTAT
, val
);
1198 void rtsx_pci_clear_ocpstat(struct rtsx_pcr
*pcr
)
1200 if (pcr
->ops
->clear_ocpstat
) {
1201 pcr
->ops
->clear_ocpstat(pcr
);
1203 u8 mask
= SD_OCP_INT_CLR
| SD_OC_CLR
;
1204 u8 val
= SD_OCP_INT_CLR
| SD_OC_CLR
;
1206 rtsx_pci_write_register(pcr
, REG_OCPCTL
, mask
, val
);
1207 rtsx_pci_write_register(pcr
, REG_OCPCTL
, mask
, 0);
1211 int rtsx_sd_power_off_card3v3(struct rtsx_pcr
*pcr
)
1213 rtsx_pci_write_register(pcr
, CARD_CLK_EN
, SD_CLK_EN
|
1214 MS_CLK_EN
| SD40_CLK_EN
, 0);
1215 rtsx_pci_write_register(pcr
, CARD_OE
, SD_OUTPUT_EN
, 0);
1217 rtsx_pci_card_power_off(pcr
, RTSX_SD_CARD
);
1221 rtsx_pci_card_pull_ctl_disable(pcr
, RTSX_SD_CARD
);
1226 int rtsx_ms_power_off_card3v3(struct rtsx_pcr
*pcr
)
1228 rtsx_pci_write_register(pcr
, CARD_CLK_EN
, SD_CLK_EN
|
1229 MS_CLK_EN
| SD40_CLK_EN
, 0);
1231 rtsx_pci_card_pull_ctl_disable(pcr
, RTSX_MS_CARD
);
1233 rtsx_pci_write_register(pcr
, CARD_OE
, MS_OUTPUT_EN
, 0);
1234 rtsx_pci_card_power_off(pcr
, RTSX_MS_CARD
);
1239 static int rtsx_pci_init_hw(struct rtsx_pcr
*pcr
)
1243 pcr
->pcie_cap
= pci_find_capability(pcr
->pci
, PCI_CAP_ID_EXP
);
1244 rtsx_pci_writel(pcr
, RTSX_HCBAR
, pcr
->host_cmds_addr
);
1246 rtsx_pci_enable_bus_int(pcr
);
1249 err
= rtsx_pci_write_register(pcr
, FPDCTL
, SSC_POWER_DOWN
, 0);
1253 /* Wait SSC power stable */
1256 rtsx_pci_disable_aspm(pcr
);
1257 if (pcr
->ops
->optimize_phy
) {
1258 err
= pcr
->ops
->optimize_phy(pcr
);
1263 rtsx_pci_init_cmd(pcr
);
1265 /* Set mcu_cnt to 7 to ensure data can be sampled properly */
1266 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_DIV
, 0x07, 0x07);
1268 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, HOST_SLEEP_STATE
, 0x03, 0x00);
1269 /* Disable card clock */
1270 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_EN
, 0x1E, 0);
1271 /* Reset delink mode */
1272 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CHANGE_LINK_STATE
, 0x0A, 0);
1273 /* Card driving select */
1274 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_DRIVE_SEL
,
1275 0xFF, pcr
->card_drive_sel
);
1276 /* Enable SSC Clock */
1277 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_CTL1
,
1278 0xFF, SSC_8X_EN
| SSC_SEL_4M
);
1279 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_CTL2
, 0xFF, 0x12);
1280 /* Disable cd_pwr_save */
1281 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CHANGE_LINK_STATE
, 0x16, 0x10);
1282 /* Clear Link Ready Interrupt */
1283 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, IRQSTAT0
,
1284 LINK_RDY_INT
, LINK_RDY_INT
);
1285 /* Enlarge the estimation window of PERST# glitch
1286 * to reduce the chance of invalid card interrupt
1288 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, PERST_GLITCH_WIDTH
, 0xFF, 0x80);
1289 /* Update RC oscillator to 400k
1290 * bit[0] F_HIGH: for RC oscillator, Rst_value is 1'b1
1293 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, RCCTL
, 0x01, 0x00);
1294 /* Set interrupt write clear
1295 * bit 1: U_elbi_if_rd_clr_en
1296 * 1: Enable ELBI interrupt[31:22] & [7:0] flag read clear
1297 * 0: ELBI interrupt flag[31:22] & [7:0] only can be write clear
1299 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, NFTS_TX_CTRL
, 0x02, 0);
1301 err
= rtsx_pci_send_cmd(pcr
, 100);
1305 switch (PCI_PID(pcr
)) {
1310 rtsx_pci_write_register(pcr
, PM_CLK_FORCE_CTL
, 1, 1);
1316 /* Enable clk_request_n to enable clock power management */
1317 rtsx_pci_write_config_byte(pcr
, pcr
->pcie_cap
+ PCI_EXP_LNKCTL
+ 1, 1);
1318 /* Enter L1 when host tx idle */
1319 rtsx_pci_write_config_byte(pcr
, 0x70F, 0x5B);
1321 if (pcr
->ops
->extra_init_hw
) {
1322 err
= pcr
->ops
->extra_init_hw(pcr
);
1327 /* No CD interrupt if probing driver with card inserted.
1328 * So we need to initialize pcr->card_exist here.
1330 if (pcr
->ops
->cd_deglitch
)
1331 pcr
->card_exist
= pcr
->ops
->cd_deglitch(pcr
);
1333 pcr
->card_exist
= rtsx_pci_readl(pcr
, RTSX_BIPR
) & CARD_EXIST
;
1338 static int rtsx_pci_init_chip(struct rtsx_pcr
*pcr
)
1342 spin_lock_init(&pcr
->lock
);
1343 mutex_init(&pcr
->pcr_mutex
);
1345 switch (PCI_PID(pcr
)) {
1348 rts5209_init_params(pcr
);
1352 rts5229_init_params(pcr
);
1356 rtl8411_init_params(pcr
);
1360 rts5227_init_params(pcr
);
1364 rts522a_init_params(pcr
);
1368 rts5249_init_params(pcr
);
1372 rts524a_init_params(pcr
);
1376 rts525a_init_params(pcr
);
1380 rtl8411b_init_params(pcr
);
1384 rtl8402_init_params(pcr
);
1387 rts5260_init_params(pcr
);
1391 pcr_dbg(pcr
, "PID: 0x%04x, IC version: 0x%02x\n",
1392 PCI_PID(pcr
), pcr
->ic_version
);
1394 pcr
->slots
= kcalloc(pcr
->num_slots
, sizeof(struct rtsx_slot
),
1399 if (pcr
->ops
->fetch_vendor_settings
)
1400 pcr
->ops
->fetch_vendor_settings(pcr
);
1402 pcr_dbg(pcr
, "pcr->aspm_en = 0x%x\n", pcr
->aspm_en
);
1403 pcr_dbg(pcr
, "pcr->sd30_drive_sel_1v8 = 0x%x\n",
1404 pcr
->sd30_drive_sel_1v8
);
1405 pcr_dbg(pcr
, "pcr->sd30_drive_sel_3v3 = 0x%x\n",
1406 pcr
->sd30_drive_sel_3v3
);
1407 pcr_dbg(pcr
, "pcr->card_drive_sel = 0x%x\n",
1408 pcr
->card_drive_sel
);
1409 pcr_dbg(pcr
, "pcr->flags = 0x%x\n", pcr
->flags
);
1411 pcr
->state
= PDEV_STAT_IDLE
;
1412 err
= rtsx_pci_init_hw(pcr
);
1421 static int rtsx_pci_probe(struct pci_dev
*pcidev
,
1422 const struct pci_device_id
*id
)
1424 struct rtsx_pcr
*pcr
;
1425 struct pcr_handle
*handle
;
1427 int ret
, i
, bar
= 0;
1429 dev_dbg(&(pcidev
->dev
),
1430 ": Realtek PCI-E Card Reader found at %s [%04x:%04x] (rev %x)\n",
1431 pci_name(pcidev
), (int)pcidev
->vendor
, (int)pcidev
->device
,
1432 (int)pcidev
->revision
);
1434 ret
= pci_set_dma_mask(pcidev
, DMA_BIT_MASK(32));
1438 ret
= pci_enable_device(pcidev
);
1442 ret
= pci_request_regions(pcidev
, DRV_NAME_RTSX_PCI
);
1446 pcr
= kzalloc(sizeof(*pcr
), GFP_KERNEL
);
1452 handle
= kzalloc(sizeof(*handle
), GFP_KERNEL
);
1459 idr_preload(GFP_KERNEL
);
1460 spin_lock(&rtsx_pci_lock
);
1461 ret
= idr_alloc(&rtsx_pci_idr
, pcr
, 0, 0, GFP_NOWAIT
);
1464 spin_unlock(&rtsx_pci_lock
);
1470 dev_set_drvdata(&pcidev
->dev
, handle
);
1472 if (CHK_PCI_PID(pcr
, 0x525A))
1474 len
= pci_resource_len(pcidev
, bar
);
1475 base
= pci_resource_start(pcidev
, bar
);
1476 pcr
->remap_addr
= ioremap_nocache(base
, len
);
1477 if (!pcr
->remap_addr
) {
1482 pcr
->rtsx_resv_buf
= dma_alloc_coherent(&(pcidev
->dev
),
1483 RTSX_RESV_BUF_LEN
, &(pcr
->rtsx_resv_buf_addr
),
1485 if (pcr
->rtsx_resv_buf
== NULL
) {
1489 pcr
->host_cmds_ptr
= pcr
->rtsx_resv_buf
;
1490 pcr
->host_cmds_addr
= pcr
->rtsx_resv_buf_addr
;
1491 pcr
->host_sg_tbl_ptr
= pcr
->rtsx_resv_buf
+ HOST_CMDS_BUF_LEN
;
1492 pcr
->host_sg_tbl_addr
= pcr
->rtsx_resv_buf_addr
+ HOST_CMDS_BUF_LEN
;
1494 pcr
->card_inserted
= 0;
1495 pcr
->card_removed
= 0;
1496 INIT_DELAYED_WORK(&pcr
->carddet_work
, rtsx_pci_card_detect
);
1497 INIT_DELAYED_WORK(&pcr
->idle_work
, rtsx_pci_idle_work
);
1499 pcr
->msi_en
= msi_en
;
1501 ret
= pci_enable_msi(pcidev
);
1503 pcr
->msi_en
= false;
1506 ret
= rtsx_pci_acquire_irq(pcr
);
1510 pci_set_master(pcidev
);
1511 synchronize_irq(pcr
->irq
);
1513 ret
= rtsx_pci_init_chip(pcr
);
1517 for (i
= 0; i
< ARRAY_SIZE(rtsx_pcr_cells
); i
++) {
1518 rtsx_pcr_cells
[i
].platform_data
= handle
;
1519 rtsx_pcr_cells
[i
].pdata_size
= sizeof(*handle
);
1521 ret
= mfd_add_devices(&pcidev
->dev
, pcr
->id
, rtsx_pcr_cells
,
1522 ARRAY_SIZE(rtsx_pcr_cells
), NULL
, 0, NULL
);
1526 schedule_delayed_work(&pcr
->idle_work
, msecs_to_jiffies(200));
1531 free_irq(pcr
->irq
, (void *)pcr
);
1534 pci_disable_msi(pcr
->pci
);
1535 dma_free_coherent(&(pcr
->pci
->dev
), RTSX_RESV_BUF_LEN
,
1536 pcr
->rtsx_resv_buf
, pcr
->rtsx_resv_buf_addr
);
1538 iounmap(pcr
->remap_addr
);
1544 pci_release_regions(pcidev
);
1546 pci_disable_device(pcidev
);
1551 static void rtsx_pci_remove(struct pci_dev
*pcidev
)
1553 struct pcr_handle
*handle
= pci_get_drvdata(pcidev
);
1554 struct rtsx_pcr
*pcr
= handle
->pcr
;
1556 pcr
->remove_pci
= true;
1558 /* Disable interrupts at the pcr level */
1559 spin_lock_irq(&pcr
->lock
);
1560 rtsx_pci_writel(pcr
, RTSX_BIER
, 0);
1562 spin_unlock_irq(&pcr
->lock
);
1564 cancel_delayed_work_sync(&pcr
->carddet_work
);
1565 cancel_delayed_work_sync(&pcr
->idle_work
);
1567 mfd_remove_devices(&pcidev
->dev
);
1569 dma_free_coherent(&(pcr
->pci
->dev
), RTSX_RESV_BUF_LEN
,
1570 pcr
->rtsx_resv_buf
, pcr
->rtsx_resv_buf_addr
);
1571 free_irq(pcr
->irq
, (void *)pcr
);
1573 pci_disable_msi(pcr
->pci
);
1574 iounmap(pcr
->remap_addr
);
1576 pci_release_regions(pcidev
);
1577 pci_disable_device(pcidev
);
1579 spin_lock(&rtsx_pci_lock
);
1580 idr_remove(&rtsx_pci_idr
, pcr
->id
);
1581 spin_unlock(&rtsx_pci_lock
);
1587 dev_dbg(&(pcidev
->dev
),
1588 ": Realtek PCI-E Card Reader at %s [%04x:%04x] has been removed\n",
1589 pci_name(pcidev
), (int)pcidev
->vendor
, (int)pcidev
->device
);
1594 static int rtsx_pci_suspend(struct pci_dev
*pcidev
, pm_message_t state
)
1596 struct pcr_handle
*handle
;
1597 struct rtsx_pcr
*pcr
;
1599 dev_dbg(&(pcidev
->dev
), "--> %s\n", __func__
);
1601 handle
= pci_get_drvdata(pcidev
);
1604 cancel_delayed_work(&pcr
->carddet_work
);
1605 cancel_delayed_work(&pcr
->idle_work
);
1607 mutex_lock(&pcr
->pcr_mutex
);
1609 rtsx_pci_power_off(pcr
, HOST_ENTER_S3
);
1611 pci_save_state(pcidev
);
1612 pci_enable_wake(pcidev
, pci_choose_state(pcidev
, state
), 0);
1613 pci_disable_device(pcidev
);
1614 pci_set_power_state(pcidev
, pci_choose_state(pcidev
, state
));
1616 mutex_unlock(&pcr
->pcr_mutex
);
1620 static int rtsx_pci_resume(struct pci_dev
*pcidev
)
1622 struct pcr_handle
*handle
;
1623 struct rtsx_pcr
*pcr
;
1626 dev_dbg(&(pcidev
->dev
), "--> %s\n", __func__
);
1628 handle
= pci_get_drvdata(pcidev
);
1631 mutex_lock(&pcr
->pcr_mutex
);
1633 pci_set_power_state(pcidev
, PCI_D0
);
1634 pci_restore_state(pcidev
);
1635 ret
= pci_enable_device(pcidev
);
1638 pci_set_master(pcidev
);
1640 ret
= rtsx_pci_write_register(pcr
, HOST_SLEEP_STATE
, 0x03, 0x00);
1644 ret
= rtsx_pci_init_hw(pcr
);
1648 schedule_delayed_work(&pcr
->idle_work
, msecs_to_jiffies(200));
1651 mutex_unlock(&pcr
->pcr_mutex
);
1655 static void rtsx_pci_shutdown(struct pci_dev
*pcidev
)
1657 struct pcr_handle
*handle
;
1658 struct rtsx_pcr
*pcr
;
1660 dev_dbg(&(pcidev
->dev
), "--> %s\n", __func__
);
1662 handle
= pci_get_drvdata(pcidev
);
1664 rtsx_pci_power_off(pcr
, HOST_ENTER_S1
);
1666 pci_disable_device(pcidev
);
1667 free_irq(pcr
->irq
, (void *)pcr
);
1669 pci_disable_msi(pcr
->pci
);
1672 #else /* CONFIG_PM */
1674 #define rtsx_pci_suspend NULL
1675 #define rtsx_pci_resume NULL
1676 #define rtsx_pci_shutdown NULL
1678 #endif /* CONFIG_PM */
1680 static struct pci_driver rtsx_pci_driver
= {
1681 .name
= DRV_NAME_RTSX_PCI
,
1682 .id_table
= rtsx_pci_ids
,
1683 .probe
= rtsx_pci_probe
,
1684 .remove
= rtsx_pci_remove
,
1685 .suspend
= rtsx_pci_suspend
,
1686 .resume
= rtsx_pci_resume
,
1687 .shutdown
= rtsx_pci_shutdown
,
1689 module_pci_driver(rtsx_pci_driver
);
1691 MODULE_LICENSE("GPL");
1692 MODULE_AUTHOR("Wei WANG <wei_wang@realsil.com.cn>");
1693 MODULE_DESCRIPTION("Realtek PCI-E Card Reader Driver");