1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Driver for Realtek PCI-Express card reader
4 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
7 * Wei WANG <wei_wang@realsil.com.cn>
10 #include <linux/pci.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/highmem.h>
15 #include <linux/interrupt.h>
16 #include <linux/delay.h>
17 #include <linux/idr.h>
18 #include <linux/platform_device.h>
19 #include <linux/mfd/core.h>
20 #include <linux/rtsx_pci.h>
21 #include <linux/mmc/card.h>
22 #include <asm/unaligned.h>
24 #include <linux/pm_runtime.h>
30 static bool msi_en = true;
31 module_param(msi_en, bool, S_IRUGO | S_IWUSR);
32 MODULE_PARM_DESC(msi_en, "Enable MSI");
34 static DEFINE_IDR(rtsx_pci_idr);
35 static DEFINE_SPINLOCK(rtsx_pci_lock);
37 static struct mfd_cell rtsx_pcr_cells[] = {
39 .name = DRV_NAME_RTSX_PCI_SDMMC,
43 static const struct pci_device_id rtsx_pci_ids[] = {
44 { PCI_DEVICE(0x10EC, 0x5209), PCI_CLASS_OTHERS << 16, 0xFF0000 },
45 { PCI_DEVICE(0x10EC, 0x5229), PCI_CLASS_OTHERS << 16, 0xFF0000 },
46 { PCI_DEVICE(0x10EC, 0x5289), PCI_CLASS_OTHERS << 16, 0xFF0000 },
47 { PCI_DEVICE(0x10EC, 0x5227), PCI_CLASS_OTHERS << 16, 0xFF0000 },
48 { PCI_DEVICE(0x10EC, 0x522A), PCI_CLASS_OTHERS << 16, 0xFF0000 },
49 { PCI_DEVICE(0x10EC, 0x5249), PCI_CLASS_OTHERS << 16, 0xFF0000 },
50 { PCI_DEVICE(0x10EC, 0x5287), PCI_CLASS_OTHERS << 16, 0xFF0000 },
51 { PCI_DEVICE(0x10EC, 0x5286), PCI_CLASS_OTHERS << 16, 0xFF0000 },
52 { PCI_DEVICE(0x10EC, 0x524A), PCI_CLASS_OTHERS << 16, 0xFF0000 },
53 { PCI_DEVICE(0x10EC, 0x525A), PCI_CLASS_OTHERS << 16, 0xFF0000 },
54 { PCI_DEVICE(0x10EC, 0x5260), PCI_CLASS_OTHERS << 16, 0xFF0000 },
55 { PCI_DEVICE(0x10EC, 0x5261), PCI_CLASS_OTHERS << 16, 0xFF0000 },
56 { PCI_DEVICE(0x10EC, 0x5228), PCI_CLASS_OTHERS << 16, 0xFF0000 },
60 MODULE_DEVICE_TABLE(pci, rtsx_pci_ids);
62 static int rtsx_comm_set_ltr_latency(struct rtsx_pcr *pcr, u32 latency)
64 rtsx_pci_write_register(pcr, MSGTXDATA0,
65 MASK_8_BIT_DEF, (u8) (latency & 0xFF));
66 rtsx_pci_write_register(pcr, MSGTXDATA1,
67 MASK_8_BIT_DEF, (u8)((latency >> 8) & 0xFF));
68 rtsx_pci_write_register(pcr, MSGTXDATA2,
69 MASK_8_BIT_DEF, (u8)((latency >> 16) & 0xFF));
70 rtsx_pci_write_register(pcr, MSGTXDATA3,
71 MASK_8_BIT_DEF, (u8)((latency >> 24) & 0xFF));
72 rtsx_pci_write_register(pcr, LTR_CTL, LTR_TX_EN_MASK |
73 LTR_LATENCY_MODE_MASK, LTR_TX_EN_1 | LTR_LATENCY_MODE_SW);
78 int rtsx_set_ltr_latency(struct rtsx_pcr *pcr, u32 latency)
80 return rtsx_comm_set_ltr_latency(pcr, latency);
83 static void rtsx_comm_set_aspm(struct rtsx_pcr *pcr, bool enable)
85 if (pcr->aspm_enabled == enable)
88 if (pcr->aspm_mode == ASPM_MODE_CFG) {
89 pcie_capability_clear_and_set_word(pcr->pci, PCI_EXP_LNKCTL,
91 enable ? pcr->aspm_en : 0);
92 } else if (pcr->aspm_mode == ASPM_MODE_REG) {
93 if (pcr->aspm_en & 0x02)
94 rtsx_pci_write_register(pcr, ASPM_FORCE_CTL, FORCE_ASPM_CTL0 |
95 FORCE_ASPM_CTL1, enable ? 0 : FORCE_ASPM_CTL0 | FORCE_ASPM_CTL1);
97 rtsx_pci_write_register(pcr, ASPM_FORCE_CTL, FORCE_ASPM_CTL0 |
98 FORCE_ASPM_CTL1, FORCE_ASPM_CTL0 | FORCE_ASPM_CTL1);
101 if (!enable && (pcr->aspm_en & 0x02))
104 pcr->aspm_enabled = enable;
107 static void rtsx_disable_aspm(struct rtsx_pcr *pcr)
109 if (pcr->ops->set_aspm)
110 pcr->ops->set_aspm(pcr, false);
112 rtsx_comm_set_aspm(pcr, false);
115 int rtsx_set_l1off_sub(struct rtsx_pcr *pcr, u8 val)
117 rtsx_pci_write_register(pcr, L1SUB_CONFIG3, 0xFF, val);
122 static void rtsx_set_l1off_sub_cfg_d0(struct rtsx_pcr *pcr, int active)
124 if (pcr->ops->set_l1off_cfg_sub_d0)
125 pcr->ops->set_l1off_cfg_sub_d0(pcr, active);
128 static void rtsx_comm_pm_full_on(struct rtsx_pcr *pcr)
130 struct rtsx_cr_option *option = &pcr->option;
132 rtsx_disable_aspm(pcr);
134 /* Fixes DMA transfer timeout issue after disabling ASPM on RTS5260 */
137 if (option->ltr_enabled)
138 rtsx_set_ltr_latency(pcr, option->ltr_active_latency);
140 if (rtsx_check_dev_flag(pcr, LTR_L1SS_PWR_GATE_EN))
141 rtsx_set_l1off_sub_cfg_d0(pcr, 1);
144 static void rtsx_pm_full_on(struct rtsx_pcr *pcr)
146 rtsx_comm_pm_full_on(pcr);
149 void rtsx_pci_start_run(struct rtsx_pcr *pcr)
151 /* If pci device removed, don't queue idle work any more */
155 if (pcr->state != PDEV_STAT_RUN) {
156 pcr->state = PDEV_STAT_RUN;
157 if (pcr->ops->enable_auto_blink)
158 pcr->ops->enable_auto_blink(pcr);
159 rtsx_pm_full_on(pcr);
162 EXPORT_SYMBOL_GPL(rtsx_pci_start_run);
164 int rtsx_pci_write_register(struct rtsx_pcr *pcr, u16 addr, u8 mask, u8 data)
167 u32 val = HAIMR_WRITE_START;
169 val |= (u32)(addr & 0x3FFF) << 16;
170 val |= (u32)mask << 8;
173 rtsx_pci_writel(pcr, RTSX_HAIMR, val);
175 for (i = 0; i < MAX_RW_REG_CNT; i++) {
176 val = rtsx_pci_readl(pcr, RTSX_HAIMR);
177 if ((val & HAIMR_TRANS_END) == 0) {
186 EXPORT_SYMBOL_GPL(rtsx_pci_write_register);
188 int rtsx_pci_read_register(struct rtsx_pcr *pcr, u16 addr, u8 *data)
190 u32 val = HAIMR_READ_START;
193 val |= (u32)(addr & 0x3FFF) << 16;
194 rtsx_pci_writel(pcr, RTSX_HAIMR, val);
196 for (i = 0; i < MAX_RW_REG_CNT; i++) {
197 val = rtsx_pci_readl(pcr, RTSX_HAIMR);
198 if ((val & HAIMR_TRANS_END) == 0)
202 if (i >= MAX_RW_REG_CNT)
206 *data = (u8)(val & 0xFF);
210 EXPORT_SYMBOL_GPL(rtsx_pci_read_register);
212 int __rtsx_pci_write_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 val)
214 int err, i, finished = 0;
217 rtsx_pci_write_register(pcr, PHYDATA0, 0xFF, (u8)val);
218 rtsx_pci_write_register(pcr, PHYDATA1, 0xFF, (u8)(val >> 8));
219 rtsx_pci_write_register(pcr, PHYADDR, 0xFF, addr);
220 rtsx_pci_write_register(pcr, PHYRWCTL, 0xFF, 0x81);
222 for (i = 0; i < 100000; i++) {
223 err = rtsx_pci_read_register(pcr, PHYRWCTL, &tmp);
239 int rtsx_pci_write_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 val)
241 if (pcr->ops->write_phy)
242 return pcr->ops->write_phy(pcr, addr, val);
244 return __rtsx_pci_write_phy_register(pcr, addr, val);
246 EXPORT_SYMBOL_GPL(rtsx_pci_write_phy_register);
248 int __rtsx_pci_read_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 *val)
250 int err, i, finished = 0;
254 rtsx_pci_write_register(pcr, PHYADDR, 0xFF, addr);
255 rtsx_pci_write_register(pcr, PHYRWCTL, 0xFF, 0x80);
257 for (i = 0; i < 100000; i++) {
258 err = rtsx_pci_read_register(pcr, PHYRWCTL, &tmp);
271 rtsx_pci_read_register(pcr, PHYDATA0, &val1);
272 rtsx_pci_read_register(pcr, PHYDATA1, &val2);
273 data = val1 | (val2 << 8);
281 int rtsx_pci_read_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 *val)
283 if (pcr->ops->read_phy)
284 return pcr->ops->read_phy(pcr, addr, val);
286 return __rtsx_pci_read_phy_register(pcr, addr, val);
288 EXPORT_SYMBOL_GPL(rtsx_pci_read_phy_register);
290 void rtsx_pci_stop_cmd(struct rtsx_pcr *pcr)
292 if (pcr->ops->stop_cmd)
293 return pcr->ops->stop_cmd(pcr);
295 rtsx_pci_writel(pcr, RTSX_HCBCTLR, STOP_CMD);
296 rtsx_pci_writel(pcr, RTSX_HDBCTLR, STOP_DMA);
298 rtsx_pci_write_register(pcr, DMACTL, 0x80, 0x80);
299 rtsx_pci_write_register(pcr, RBCTL, 0x80, 0x80);
301 EXPORT_SYMBOL_GPL(rtsx_pci_stop_cmd);
303 void rtsx_pci_add_cmd(struct rtsx_pcr *pcr,
304 u8 cmd_type, u16 reg_addr, u8 mask, u8 data)
308 u32 *ptr = (u32 *)(pcr->host_cmds_ptr);
310 val |= (u32)(cmd_type & 0x03) << 30;
311 val |= (u32)(reg_addr & 0x3FFF) << 16;
312 val |= (u32)mask << 8;
315 spin_lock_irqsave(&pcr->lock, flags);
317 if (pcr->ci < (HOST_CMDS_BUF_LEN / 4)) {
318 put_unaligned_le32(val, ptr);
322 spin_unlock_irqrestore(&pcr->lock, flags);
324 EXPORT_SYMBOL_GPL(rtsx_pci_add_cmd);
326 void rtsx_pci_send_cmd_no_wait(struct rtsx_pcr *pcr)
330 rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr);
332 val |= (u32)(pcr->ci * 4) & 0x00FFFFFF;
333 /* Hardware Auto Response */
335 rtsx_pci_writel(pcr, RTSX_HCBCTLR, val);
337 EXPORT_SYMBOL_GPL(rtsx_pci_send_cmd_no_wait);
339 int rtsx_pci_send_cmd(struct rtsx_pcr *pcr, int timeout)
341 struct completion trans_done;
347 spin_lock_irqsave(&pcr->lock, flags);
349 /* set up data structures for the wakeup system */
350 pcr->done = &trans_done;
351 pcr->trans_result = TRANS_NOT_READY;
352 init_completion(&trans_done);
354 rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr);
356 val |= (u32)(pcr->ci * 4) & 0x00FFFFFF;
357 /* Hardware Auto Response */
359 rtsx_pci_writel(pcr, RTSX_HCBCTLR, val);
361 spin_unlock_irqrestore(&pcr->lock, flags);
363 /* Wait for TRANS_OK_INT */
364 timeleft = wait_for_completion_interruptible_timeout(
365 &trans_done, msecs_to_jiffies(timeout));
367 pcr_dbg(pcr, "Timeout (%s %d)\n", __func__, __LINE__);
369 goto finish_send_cmd;
372 spin_lock_irqsave(&pcr->lock, flags);
373 if (pcr->trans_result == TRANS_RESULT_FAIL)
375 else if (pcr->trans_result == TRANS_RESULT_OK)
377 else if (pcr->trans_result == TRANS_NO_DEVICE)
379 spin_unlock_irqrestore(&pcr->lock, flags);
382 spin_lock_irqsave(&pcr->lock, flags);
384 spin_unlock_irqrestore(&pcr->lock, flags);
386 if ((err < 0) && (err != -ENODEV))
387 rtsx_pci_stop_cmd(pcr);
390 complete(pcr->finish_me);
394 EXPORT_SYMBOL_GPL(rtsx_pci_send_cmd);
396 static void rtsx_pci_add_sg_tbl(struct rtsx_pcr *pcr,
397 dma_addr_t addr, unsigned int len, int end)
399 u64 *ptr = (u64 *)(pcr->host_sg_tbl_ptr) + pcr->sgi;
401 u8 option = RTSX_SG_VALID | RTSX_SG_TRANS_DATA;
403 pcr_dbg(pcr, "DMA addr: 0x%x, Len: 0x%x\n", (unsigned int)addr, len);
406 option |= RTSX_SG_END;
408 if ((PCI_PID(pcr) == PID_5261) || (PCI_PID(pcr) == PID_5228)) {
410 val = ((u64)addr << 32) | (((u64)len & 0xFFFF) << 16)
411 | (((u64)len >> 16) << 6) | option;
413 val = ((u64)addr << 32) | ((u64)len << 16) | option;
415 val = ((u64)addr << 32) | ((u64)len << 12) | option;
417 put_unaligned_le64(val, ptr);
421 int rtsx_pci_transfer_data(struct rtsx_pcr *pcr, struct scatterlist *sglist,
422 int num_sg, bool read, int timeout)
426 pcr_dbg(pcr, "--> %s: num_sg = %d\n", __func__, num_sg);
427 count = rtsx_pci_dma_map_sg(pcr, sglist, num_sg, read);
430 pcr_dbg(pcr, "DMA mapping count: %d\n", count);
432 err = rtsx_pci_dma_transfer(pcr, sglist, count, read, timeout);
434 rtsx_pci_dma_unmap_sg(pcr, sglist, num_sg, read);
438 EXPORT_SYMBOL_GPL(rtsx_pci_transfer_data);
440 int rtsx_pci_dma_map_sg(struct rtsx_pcr *pcr, struct scatterlist *sglist,
441 int num_sg, bool read)
443 enum dma_data_direction dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
448 if ((sglist == NULL) || (num_sg <= 0))
451 return dma_map_sg(&(pcr->pci->dev), sglist, num_sg, dir);
453 EXPORT_SYMBOL_GPL(rtsx_pci_dma_map_sg);
455 void rtsx_pci_dma_unmap_sg(struct rtsx_pcr *pcr, struct scatterlist *sglist,
456 int num_sg, bool read)
458 enum dma_data_direction dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
460 dma_unmap_sg(&(pcr->pci->dev), sglist, num_sg, dir);
462 EXPORT_SYMBOL_GPL(rtsx_pci_dma_unmap_sg);
464 int rtsx_pci_dma_transfer(struct rtsx_pcr *pcr, struct scatterlist *sglist,
465 int count, bool read, int timeout)
467 struct completion trans_done;
468 struct scatterlist *sg;
475 u8 dir = read ? DEVICE_TO_HOST : HOST_TO_DEVICE;
480 if ((sglist == NULL) || (count < 1))
483 val = ((u32)(dir & 0x01) << 29) | TRIG_DMA | ADMA_MODE;
485 for_each_sg(sglist, sg, count, i) {
486 addr = sg_dma_address(sg);
487 len = sg_dma_len(sg);
488 rtsx_pci_add_sg_tbl(pcr, addr, len, i == count - 1);
491 spin_lock_irqsave(&pcr->lock, flags);
493 pcr->done = &trans_done;
494 pcr->trans_result = TRANS_NOT_READY;
495 init_completion(&trans_done);
496 rtsx_pci_writel(pcr, RTSX_HDBAR, pcr->host_sg_tbl_addr);
497 rtsx_pci_writel(pcr, RTSX_HDBCTLR, val);
499 spin_unlock_irqrestore(&pcr->lock, flags);
501 timeleft = wait_for_completion_interruptible_timeout(
502 &trans_done, msecs_to_jiffies(timeout));
504 pcr_dbg(pcr, "Timeout (%s %d)\n", __func__, __LINE__);
509 spin_lock_irqsave(&pcr->lock, flags);
510 if (pcr->trans_result == TRANS_RESULT_FAIL) {
512 if (pcr->dma_error_count < RTS_MAX_TIMES_FREQ_REDUCTION)
513 pcr->dma_error_count++;
516 else if (pcr->trans_result == TRANS_NO_DEVICE)
518 spin_unlock_irqrestore(&pcr->lock, flags);
521 spin_lock_irqsave(&pcr->lock, flags);
523 spin_unlock_irqrestore(&pcr->lock, flags);
525 if ((err < 0) && (err != -ENODEV))
526 rtsx_pci_stop_cmd(pcr);
529 complete(pcr->finish_me);
533 EXPORT_SYMBOL_GPL(rtsx_pci_dma_transfer);
535 int rtsx_pci_read_ppbuf(struct rtsx_pcr *pcr, u8 *buf, int buf_len)
547 for (i = 0; i < buf_len / 256; i++) {
548 rtsx_pci_init_cmd(pcr);
550 for (j = 0; j < 256; j++)
551 rtsx_pci_add_cmd(pcr, READ_REG_CMD, reg++, 0, 0);
553 err = rtsx_pci_send_cmd(pcr, 250);
557 memcpy(ptr, rtsx_pci_get_cmd_data(pcr), 256);
562 rtsx_pci_init_cmd(pcr);
564 for (j = 0; j < buf_len % 256; j++)
565 rtsx_pci_add_cmd(pcr, READ_REG_CMD, reg++, 0, 0);
567 err = rtsx_pci_send_cmd(pcr, 250);
572 memcpy(ptr, rtsx_pci_get_cmd_data(pcr), buf_len % 256);
576 EXPORT_SYMBOL_GPL(rtsx_pci_read_ppbuf);
578 int rtsx_pci_write_ppbuf(struct rtsx_pcr *pcr, u8 *buf, int buf_len)
590 for (i = 0; i < buf_len / 256; i++) {
591 rtsx_pci_init_cmd(pcr);
593 for (j = 0; j < 256; j++) {
594 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
599 err = rtsx_pci_send_cmd(pcr, 250);
605 rtsx_pci_init_cmd(pcr);
607 for (j = 0; j < buf_len % 256; j++) {
608 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
613 err = rtsx_pci_send_cmd(pcr, 250);
620 EXPORT_SYMBOL_GPL(rtsx_pci_write_ppbuf);
622 static int rtsx_pci_set_pull_ctl(struct rtsx_pcr *pcr, const u32 *tbl)
624 rtsx_pci_init_cmd(pcr);
626 while (*tbl & 0xFFFF0000) {
627 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
628 (u16)(*tbl >> 16), 0xFF, (u8)(*tbl));
632 return rtsx_pci_send_cmd(pcr, 100);
635 int rtsx_pci_card_pull_ctl_enable(struct rtsx_pcr *pcr, int card)
639 if (card == RTSX_SD_CARD)
640 tbl = pcr->sd_pull_ctl_enable_tbl;
641 else if (card == RTSX_MS_CARD)
642 tbl = pcr->ms_pull_ctl_enable_tbl;
646 return rtsx_pci_set_pull_ctl(pcr, tbl);
648 EXPORT_SYMBOL_GPL(rtsx_pci_card_pull_ctl_enable);
650 int rtsx_pci_card_pull_ctl_disable(struct rtsx_pcr *pcr, int card)
654 if (card == RTSX_SD_CARD)
655 tbl = pcr->sd_pull_ctl_disable_tbl;
656 else if (card == RTSX_MS_CARD)
657 tbl = pcr->ms_pull_ctl_disable_tbl;
661 return rtsx_pci_set_pull_ctl(pcr, tbl);
663 EXPORT_SYMBOL_GPL(rtsx_pci_card_pull_ctl_disable);
665 static void rtsx_pci_enable_bus_int(struct rtsx_pcr *pcr)
667 struct rtsx_hw_param *hw_param = &pcr->hw_param;
669 pcr->bier = TRANS_OK_INT_EN | TRANS_FAIL_INT_EN | SD_INT_EN
670 | hw_param->interrupt_en;
672 if (pcr->num_slots > 1)
673 pcr->bier |= MS_INT_EN;
675 /* Enable Bus Interrupt */
676 rtsx_pci_writel(pcr, RTSX_BIER, pcr->bier);
678 pcr_dbg(pcr, "RTSX_BIER: 0x%08x\n", pcr->bier);
681 static inline u8 double_ssc_depth(u8 depth)
683 return ((depth > 1) ? (depth - 1) : depth);
686 static u8 revise_ssc_depth(u8 ssc_depth, u8 div)
688 if (div > CLK_DIV_1) {
689 if (ssc_depth > (div - 1))
690 ssc_depth -= (div - 1);
692 ssc_depth = SSC_DEPTH_4M;
698 int rtsx_pci_switch_clock(struct rtsx_pcr *pcr, unsigned int card_clock,
699 u8 ssc_depth, bool initial_mode, bool double_clk, bool vpclk)
702 u8 n, clk_divider, mcu_cnt, div;
703 static const u8 depth[] = {
704 [RTSX_SSC_DEPTH_4M] = SSC_DEPTH_4M,
705 [RTSX_SSC_DEPTH_2M] = SSC_DEPTH_2M,
706 [RTSX_SSC_DEPTH_1M] = SSC_DEPTH_1M,
707 [RTSX_SSC_DEPTH_500K] = SSC_DEPTH_500K,
708 [RTSX_SSC_DEPTH_250K] = SSC_DEPTH_250K,
711 if (PCI_PID(pcr) == PID_5261)
712 return rts5261_pci_switch_clock(pcr, card_clock,
713 ssc_depth, initial_mode, double_clk, vpclk);
714 if (PCI_PID(pcr) == PID_5228)
715 return rts5228_pci_switch_clock(pcr, card_clock,
716 ssc_depth, initial_mode, double_clk, vpclk);
719 /* We use 250k(around) here, in initial stage */
720 clk_divider = SD_CLK_DIVIDE_128;
721 card_clock = 30000000;
723 clk_divider = SD_CLK_DIVIDE_0;
725 err = rtsx_pci_write_register(pcr, SD_CFG1,
726 SD_CLK_DIVIDE_MASK, clk_divider);
730 /* Reduce card clock by 20MHz each time a DMA transfer error occurs */
731 if (card_clock == UHS_SDR104_MAX_DTR &&
732 pcr->dma_error_count &&
733 PCI_PID(pcr) == RTS5227_DEVICE_ID)
734 card_clock = UHS_SDR104_MAX_DTR -
735 (pcr->dma_error_count * 20000000);
737 card_clock /= 1000000;
738 pcr_dbg(pcr, "Switch card clock to %dMHz\n", card_clock);
741 if (!initial_mode && double_clk)
742 clk = card_clock * 2;
743 pcr_dbg(pcr, "Internal SSC clock: %dMHz (cur_clock = %d)\n",
744 clk, pcr->cur_clock);
746 if (clk == pcr->cur_clock)
749 if (pcr->ops->conv_clk_and_div_n)
750 n = (u8)pcr->ops->conv_clk_and_div_n(clk, CLK_TO_DIV_N);
753 if ((clk <= 2) || (n > MAX_DIV_N_PCR))
756 mcu_cnt = (u8)(125/clk + 3);
760 /* Make sure that the SSC clock div_n is not less than MIN_DIV_N_PCR */
762 while ((n < MIN_DIV_N_PCR) && (div < CLK_DIV_8)) {
763 if (pcr->ops->conv_clk_and_div_n) {
764 int dbl_clk = pcr->ops->conv_clk_and_div_n(n,
766 n = (u8)pcr->ops->conv_clk_and_div_n(dbl_clk,
773 pcr_dbg(pcr, "n = %d, div = %d\n", n, div);
775 ssc_depth = depth[ssc_depth];
777 ssc_depth = double_ssc_depth(ssc_depth);
779 ssc_depth = revise_ssc_depth(ssc_depth, div);
780 pcr_dbg(pcr, "ssc_depth = %d\n", ssc_depth);
782 rtsx_pci_init_cmd(pcr);
783 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL,
784 CLK_LOW_FREQ, CLK_LOW_FREQ);
785 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_DIV,
786 0xFF, (div << 4) | mcu_cnt);
787 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, 0);
788 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL2,
789 SSC_DEPTH_MASK, ssc_depth);
790 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_DIV_N_0, 0xFF, n);
791 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, SSC_RSTB);
793 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPCLK0_CTL,
795 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPCLK0_CTL,
796 PHASE_NOT_RESET, PHASE_NOT_RESET);
799 err = rtsx_pci_send_cmd(pcr, 2000);
803 /* Wait SSC clock stable */
804 udelay(SSC_CLOCK_STABLE_WAIT);
805 err = rtsx_pci_write_register(pcr, CLK_CTL, CLK_LOW_FREQ, 0);
809 pcr->cur_clock = clk;
812 EXPORT_SYMBOL_GPL(rtsx_pci_switch_clock);
814 int rtsx_pci_card_power_on(struct rtsx_pcr *pcr, int card)
816 if (pcr->ops->card_power_on)
817 return pcr->ops->card_power_on(pcr, card);
821 EXPORT_SYMBOL_GPL(rtsx_pci_card_power_on);
823 int rtsx_pci_card_power_off(struct rtsx_pcr *pcr, int card)
825 if (pcr->ops->card_power_off)
826 return pcr->ops->card_power_off(pcr, card);
830 EXPORT_SYMBOL_GPL(rtsx_pci_card_power_off);
832 int rtsx_pci_card_exclusive_check(struct rtsx_pcr *pcr, int card)
834 static const unsigned int cd_mask[] = {
835 [RTSX_SD_CARD] = SD_EXIST,
836 [RTSX_MS_CARD] = MS_EXIST
839 if (!(pcr->flags & PCR_MS_PMOS)) {
840 /* When using single PMOS, accessing card is not permitted
841 * if the existing card is not the designated one.
843 if (pcr->card_exist & (~cd_mask[card]))
849 EXPORT_SYMBOL_GPL(rtsx_pci_card_exclusive_check);
851 int rtsx_pci_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
853 if (pcr->ops->switch_output_voltage)
854 return pcr->ops->switch_output_voltage(pcr, voltage);
858 EXPORT_SYMBOL_GPL(rtsx_pci_switch_output_voltage);
860 unsigned int rtsx_pci_card_exist(struct rtsx_pcr *pcr)
864 val = rtsx_pci_readl(pcr, RTSX_BIPR);
865 if (pcr->ops->cd_deglitch)
866 val = pcr->ops->cd_deglitch(pcr);
870 EXPORT_SYMBOL_GPL(rtsx_pci_card_exist);
872 void rtsx_pci_complete_unfinished_transfer(struct rtsx_pcr *pcr)
874 struct completion finish;
876 pcr->finish_me = &finish;
877 init_completion(&finish);
882 if (!pcr->remove_pci)
883 rtsx_pci_stop_cmd(pcr);
885 wait_for_completion_interruptible_timeout(&finish,
886 msecs_to_jiffies(2));
887 pcr->finish_me = NULL;
889 EXPORT_SYMBOL_GPL(rtsx_pci_complete_unfinished_transfer);
891 static void rtsx_pci_card_detect(struct work_struct *work)
893 struct delayed_work *dwork;
894 struct rtsx_pcr *pcr;
896 unsigned int card_detect = 0, card_inserted, card_removed;
899 dwork = to_delayed_work(work);
900 pcr = container_of(dwork, struct rtsx_pcr, carddet_work);
902 pcr_dbg(pcr, "--> %s\n", __func__);
904 mutex_lock(&pcr->pcr_mutex);
905 spin_lock_irqsave(&pcr->lock, flags);
907 irq_status = rtsx_pci_readl(pcr, RTSX_BIPR);
908 pcr_dbg(pcr, "irq_status: 0x%08x\n", irq_status);
910 irq_status &= CARD_EXIST;
911 card_inserted = pcr->card_inserted & irq_status;
912 card_removed = pcr->card_removed;
913 pcr->card_inserted = 0;
914 pcr->card_removed = 0;
916 spin_unlock_irqrestore(&pcr->lock, flags);
918 if (card_inserted || card_removed) {
919 pcr_dbg(pcr, "card_inserted: 0x%x, card_removed: 0x%x\n",
920 card_inserted, card_removed);
922 if (pcr->ops->cd_deglitch)
923 card_inserted = pcr->ops->cd_deglitch(pcr);
925 card_detect = card_inserted | card_removed;
927 pcr->card_exist |= card_inserted;
928 pcr->card_exist &= ~card_removed;
931 mutex_unlock(&pcr->pcr_mutex);
933 if ((card_detect & SD_EXIST) && pcr->slots[RTSX_SD_CARD].card_event)
934 pcr->slots[RTSX_SD_CARD].card_event(
935 pcr->slots[RTSX_SD_CARD].p_dev);
936 if ((card_detect & MS_EXIST) && pcr->slots[RTSX_MS_CARD].card_event)
937 pcr->slots[RTSX_MS_CARD].card_event(
938 pcr->slots[RTSX_MS_CARD].p_dev);
941 static void rtsx_pci_process_ocp(struct rtsx_pcr *pcr)
943 if (pcr->ops->process_ocp) {
944 pcr->ops->process_ocp(pcr);
946 if (!pcr->option.ocp_en)
948 rtsx_pci_get_ocpstat(pcr, &pcr->ocp_stat);
949 if (pcr->ocp_stat & (SD_OC_NOW | SD_OC_EVER)) {
950 rtsx_pci_card_power_off(pcr, RTSX_SD_CARD);
951 rtsx_pci_write_register(pcr, CARD_OE, SD_OUTPUT_EN, 0);
952 rtsx_pci_clear_ocpstat(pcr);
958 static int rtsx_pci_process_ocp_interrupt(struct rtsx_pcr *pcr)
960 if (pcr->option.ocp_en)
961 rtsx_pci_process_ocp(pcr);
966 static irqreturn_t rtsx_pci_isr(int irq, void *dev_id)
968 struct rtsx_pcr *pcr = dev_id;
974 spin_lock(&pcr->lock);
976 int_reg = rtsx_pci_readl(pcr, RTSX_BIPR);
977 /* Clear interrupt flag */
978 rtsx_pci_writel(pcr, RTSX_BIPR, int_reg);
979 if ((int_reg & pcr->bier) == 0) {
980 spin_unlock(&pcr->lock);
983 if (int_reg == 0xFFFFFFFF) {
984 spin_unlock(&pcr->lock);
988 int_reg &= (pcr->bier | 0x7FFFFF);
990 if (int_reg & SD_OC_INT)
991 rtsx_pci_process_ocp_interrupt(pcr);
993 if (int_reg & SD_INT) {
994 if (int_reg & SD_EXIST) {
995 pcr->card_inserted |= SD_EXIST;
997 pcr->card_removed |= SD_EXIST;
998 pcr->card_inserted &= ~SD_EXIST;
999 if (PCI_PID(pcr) == PID_5261) {
1000 rtsx_pci_write_register(pcr, RTS5261_FW_STATUS,
1001 RTS5261_EXPRESS_LINK_FAIL_MASK, 0);
1002 pcr->extra_caps |= EXTRA_CAPS_SD_EXPRESS;
1005 pcr->dma_error_count = 0;
1008 if (int_reg & MS_INT) {
1009 if (int_reg & MS_EXIST) {
1010 pcr->card_inserted |= MS_EXIST;
1012 pcr->card_removed |= MS_EXIST;
1013 pcr->card_inserted &= ~MS_EXIST;
1017 if (int_reg & (NEED_COMPLETE_INT | DELINK_INT)) {
1018 if (int_reg & (TRANS_FAIL_INT | DELINK_INT)) {
1019 pcr->trans_result = TRANS_RESULT_FAIL;
1021 complete(pcr->done);
1022 } else if (int_reg & TRANS_OK_INT) {
1023 pcr->trans_result = TRANS_RESULT_OK;
1025 complete(pcr->done);
1029 if ((pcr->card_inserted || pcr->card_removed) && !(int_reg & SD_OC_INT))
1030 schedule_delayed_work(&pcr->carddet_work,
1031 msecs_to_jiffies(200));
1033 spin_unlock(&pcr->lock);
1037 static int rtsx_pci_acquire_irq(struct rtsx_pcr *pcr)
1039 pcr_dbg(pcr, "%s: pcr->msi_en = %d, pci->irq = %d\n",
1040 __func__, pcr->msi_en, pcr->pci->irq);
1042 if (request_irq(pcr->pci->irq, rtsx_pci_isr,
1043 pcr->msi_en ? 0 : IRQF_SHARED,
1044 DRV_NAME_RTSX_PCI, pcr)) {
1045 dev_err(&(pcr->pci->dev),
1046 "rtsx_sdmmc: unable to grab IRQ %d, disabling device\n",
1051 pcr->irq = pcr->pci->irq;
1052 pci_intx(pcr->pci, !pcr->msi_en);
1057 static void rtsx_base_force_power_down(struct rtsx_pcr *pcr)
1059 /* Set relink_time to 0 */
1060 rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 1, MASK_8_BIT_DEF, 0);
1061 rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 2, MASK_8_BIT_DEF, 0);
1062 rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 3,
1063 RELINK_TIME_MASK, 0);
1065 rtsx_pci_write_register(pcr, pcr->reg_pm_ctrl3,
1066 D3_DELINK_MODE_EN, D3_DELINK_MODE_EN);
1068 rtsx_pci_write_register(pcr, FPDCTL, ALL_POWER_DOWN, ALL_POWER_DOWN);
1071 static void __maybe_unused rtsx_pci_power_off(struct rtsx_pcr *pcr, u8 pm_state, bool runtime)
1073 if (pcr->ops->turn_off_led)
1074 pcr->ops->turn_off_led(pcr);
1076 rtsx_pci_writel(pcr, RTSX_BIER, 0);
1079 rtsx_pci_write_register(pcr, PETXCFG, 0x08, 0x08);
1080 rtsx_pci_write_register(pcr, HOST_SLEEP_STATE, 0x03, pm_state);
1082 if (pcr->ops->force_power_down)
1083 pcr->ops->force_power_down(pcr, pm_state, runtime);
1085 rtsx_base_force_power_down(pcr);
1088 void rtsx_pci_enable_ocp(struct rtsx_pcr *pcr)
1090 u8 val = SD_OCP_INT_EN | SD_DETECT_EN;
1092 if (pcr->ops->enable_ocp) {
1093 pcr->ops->enable_ocp(pcr);
1095 rtsx_pci_write_register(pcr, FPDCTL, OC_POWER_DOWN, 0);
1096 rtsx_pci_write_register(pcr, REG_OCPCTL, 0xFF, val);
1101 void rtsx_pci_disable_ocp(struct rtsx_pcr *pcr)
1103 u8 mask = SD_OCP_INT_EN | SD_DETECT_EN;
1105 if (pcr->ops->disable_ocp) {
1106 pcr->ops->disable_ocp(pcr);
1108 rtsx_pci_write_register(pcr, REG_OCPCTL, mask, 0);
1109 rtsx_pci_write_register(pcr, FPDCTL, OC_POWER_DOWN,
1114 void rtsx_pci_init_ocp(struct rtsx_pcr *pcr)
1116 if (pcr->ops->init_ocp) {
1117 pcr->ops->init_ocp(pcr);
1119 struct rtsx_cr_option *option = &(pcr->option);
1121 if (option->ocp_en) {
1122 u8 val = option->sd_800mA_ocp_thd;
1124 rtsx_pci_write_register(pcr, FPDCTL, OC_POWER_DOWN, 0);
1125 rtsx_pci_write_register(pcr, REG_OCPPARA1,
1126 SD_OCP_TIME_MASK, SD_OCP_TIME_800);
1127 rtsx_pci_write_register(pcr, REG_OCPPARA2,
1128 SD_OCP_THD_MASK, val);
1129 rtsx_pci_write_register(pcr, REG_OCPGLITCH,
1130 SD_OCP_GLITCH_MASK, pcr->hw_param.ocp_glitch);
1131 rtsx_pci_enable_ocp(pcr);
1136 int rtsx_pci_get_ocpstat(struct rtsx_pcr *pcr, u8 *val)
1138 if (pcr->ops->get_ocpstat)
1139 return pcr->ops->get_ocpstat(pcr, val);
1141 return rtsx_pci_read_register(pcr, REG_OCPSTAT, val);
1144 void rtsx_pci_clear_ocpstat(struct rtsx_pcr *pcr)
1146 if (pcr->ops->clear_ocpstat) {
1147 pcr->ops->clear_ocpstat(pcr);
1149 u8 mask = SD_OCP_INT_CLR | SD_OC_CLR;
1150 u8 val = SD_OCP_INT_CLR | SD_OC_CLR;
1152 rtsx_pci_write_register(pcr, REG_OCPCTL, mask, val);
1154 rtsx_pci_write_register(pcr, REG_OCPCTL, mask, 0);
1158 void rtsx_pci_enable_oobs_polling(struct rtsx_pcr *pcr)
1162 if ((PCI_PID(pcr) != PID_525A) && (PCI_PID(pcr) != PID_5260)) {
1163 rtsx_pci_read_phy_register(pcr, 0x01, &val);
1165 rtsx_pci_write_phy_register(pcr, 0x01, val);
1167 rtsx_pci_write_register(pcr, REG_CFG_OOBS_OFF_TIMER, 0xFF, 0x32);
1168 rtsx_pci_write_register(pcr, REG_CFG_OOBS_ON_TIMER, 0xFF, 0x05);
1169 rtsx_pci_write_register(pcr, REG_CFG_VCM_ON_TIMER, 0xFF, 0x83);
1170 rtsx_pci_write_register(pcr, REG_CFG_OOBS_POLLING, 0xFF, 0xDE);
1174 void rtsx_pci_disable_oobs_polling(struct rtsx_pcr *pcr)
1178 if ((PCI_PID(pcr) != PID_525A) && (PCI_PID(pcr) != PID_5260)) {
1179 rtsx_pci_read_phy_register(pcr, 0x01, &val);
1181 rtsx_pci_write_phy_register(pcr, 0x01, val);
1183 rtsx_pci_write_register(pcr, REG_CFG_VCM_ON_TIMER, 0xFF, 0x03);
1184 rtsx_pci_write_register(pcr, REG_CFG_OOBS_POLLING, 0xFF, 0x00);
1188 int rtsx_sd_power_off_card3v3(struct rtsx_pcr *pcr)
1190 rtsx_pci_write_register(pcr, CARD_CLK_EN, SD_CLK_EN |
1191 MS_CLK_EN | SD40_CLK_EN, 0);
1192 rtsx_pci_write_register(pcr, CARD_OE, SD_OUTPUT_EN, 0);
1193 rtsx_pci_card_power_off(pcr, RTSX_SD_CARD);
1197 rtsx_pci_card_pull_ctl_disable(pcr, RTSX_SD_CARD);
1202 int rtsx_ms_power_off_card3v3(struct rtsx_pcr *pcr)
1204 rtsx_pci_write_register(pcr, CARD_CLK_EN, SD_CLK_EN |
1205 MS_CLK_EN | SD40_CLK_EN, 0);
1207 rtsx_pci_card_pull_ctl_disable(pcr, RTSX_MS_CARD);
1209 rtsx_pci_write_register(pcr, CARD_OE, MS_OUTPUT_EN, 0);
1210 rtsx_pci_card_power_off(pcr, RTSX_MS_CARD);
1215 static int rtsx_pci_init_hw(struct rtsx_pcr *pcr)
1217 struct pci_dev *pdev = pcr->pci;
1220 if (PCI_PID(pcr) == PID_5228)
1221 rtsx_pci_write_register(pcr, RTS5228_LDO1_CFG1, RTS5228_LDO1_SR_TIME_MASK,
1222 RTS5228_LDO1_SR_0_5);
1224 rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr);
1226 rtsx_pci_enable_bus_int(pcr);
1229 if (PCI_PID(pcr) == PID_5261) {
1230 /* Gating real mcu clock */
1231 err = rtsx_pci_write_register(pcr, RTS5261_FW_CFG1,
1232 RTS5261_MCU_CLOCK_GATING, 0);
1233 err = rtsx_pci_write_register(pcr, RTS5261_REG_FPDCTL,
1236 err = rtsx_pci_write_register(pcr, FPDCTL, SSC_POWER_DOWN, 0);
1241 /* Wait SSC power stable */
1244 rtsx_disable_aspm(pcr);
1245 if (pcr->ops->optimize_phy) {
1246 err = pcr->ops->optimize_phy(pcr);
1251 rtsx_pci_init_cmd(pcr);
1253 /* Set mcu_cnt to 7 to ensure data can be sampled properly */
1254 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_DIV, 0x07, 0x07);
1256 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, HOST_SLEEP_STATE, 0x03, 0x00);
1257 /* Disable card clock */
1258 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_EN, 0x1E, 0);
1259 /* Reset delink mode */
1260 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CHANGE_LINK_STATE, 0x0A, 0);
1261 /* Card driving select */
1262 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DRIVE_SEL,
1263 0xFF, pcr->card_drive_sel);
1264 /* Enable SSC Clock */
1265 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1,
1266 0xFF, SSC_8X_EN | SSC_SEL_4M);
1267 if (PCI_PID(pcr) == PID_5261)
1268 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL2, 0xFF,
1269 RTS5261_SSC_DEPTH_2M);
1270 else if (PCI_PID(pcr) == PID_5228)
1271 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL2, 0xFF,
1272 RTS5228_SSC_DEPTH_2M);
1274 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL2, 0xFF, 0x12);
1276 /* Disable cd_pwr_save */
1277 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CHANGE_LINK_STATE, 0x16, 0x10);
1278 /* Clear Link Ready Interrupt */
1279 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, IRQSTAT0,
1280 LINK_RDY_INT, LINK_RDY_INT);
1281 /* Enlarge the estimation window of PERST# glitch
1282 * to reduce the chance of invalid card interrupt
1284 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PERST_GLITCH_WIDTH, 0xFF, 0x80);
1285 /* Update RC oscillator to 400k
1286 * bit[0] F_HIGH: for RC oscillator, Rst_value is 1'b1
1289 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, RCCTL, 0x01, 0x00);
1290 /* Set interrupt write clear
1291 * bit 1: U_elbi_if_rd_clr_en
1292 * 1: Enable ELBI interrupt[31:22] & [7:0] flag read clear
1293 * 0: ELBI interrupt flag[31:22] & [7:0] only can be write clear
1295 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, NFTS_TX_CTRL, 0x02, 0);
1297 err = rtsx_pci_send_cmd(pcr, 100);
1301 switch (PCI_PID(pcr)) {
1308 rtsx_pci_write_register(pcr, PM_CLK_FORCE_CTL, 1, 1);
1315 rtsx_pci_init_ocp(pcr);
1317 /* Enable clk_request_n to enable clock power management */
1318 pcie_capability_clear_and_set_word(pcr->pci, PCI_EXP_LNKCTL,
1319 0, PCI_EXP_LNKCTL_CLKREQ_EN);
1320 /* Enter L1 when host tx idle */
1321 pci_write_config_byte(pdev, 0x70F, 0x5B);
1323 if (pcr->ops->extra_init_hw) {
1324 err = pcr->ops->extra_init_hw(pcr);
1329 if (pcr->aspm_mode == ASPM_MODE_REG) {
1330 rtsx_pci_write_register(pcr, ASPM_FORCE_CTL, 0x30, 0x30);
1331 rtsx_pci_write_register(pcr, PETXCFG,
1332 FORCE_CLKREQ_DELINK_MASK, FORCE_CLKREQ_HIGH);
1335 /* No CD interrupt if probing driver with card inserted.
1336 * So we need to initialize pcr->card_exist here.
1338 if (pcr->ops->cd_deglitch)
1339 pcr->card_exist = pcr->ops->cd_deglitch(pcr);
1341 pcr->card_exist = rtsx_pci_readl(pcr, RTSX_BIPR) & CARD_EXIST;
1346 static int rtsx_pci_init_chip(struct rtsx_pcr *pcr)
1352 spin_lock_init(&pcr->lock);
1353 mutex_init(&pcr->pcr_mutex);
1355 switch (PCI_PID(pcr)) {
1358 rts5209_init_params(pcr);
1362 rts5229_init_params(pcr);
1366 rtl8411_init_params(pcr);
1370 rts5227_init_params(pcr);
1374 rts522a_init_params(pcr);
1378 rts5249_init_params(pcr);
1382 rts524a_init_params(pcr);
1386 rts525a_init_params(pcr);
1390 rtl8411b_init_params(pcr);
1394 rtl8402_init_params(pcr);
1398 rts5260_init_params(pcr);
1402 rts5261_init_params(pcr);
1406 rts5228_init_params(pcr);
1410 pcr_dbg(pcr, "PID: 0x%04x, IC version: 0x%02x\n",
1411 PCI_PID(pcr), pcr->ic_version);
1413 pcr->slots = kcalloc(pcr->num_slots, sizeof(struct rtsx_slot),
1418 if (pcr->aspm_mode == ASPM_MODE_CFG) {
1419 pcie_capability_read_word(pcr->pci, PCI_EXP_LNKCTL, &cfg_val);
1420 if (cfg_val & PCI_EXP_LNKCTL_ASPM_L1)
1421 pcr->aspm_enabled = true;
1423 pcr->aspm_enabled = false;
1425 } else if (pcr->aspm_mode == ASPM_MODE_REG) {
1426 rtsx_pci_read_register(pcr, ASPM_FORCE_CTL, &val);
1427 if (val & FORCE_ASPM_CTL0 && val & FORCE_ASPM_CTL1)
1428 pcr->aspm_enabled = false;
1430 pcr->aspm_enabled = true;
1433 if (pcr->ops->fetch_vendor_settings)
1434 pcr->ops->fetch_vendor_settings(pcr);
1436 pcr_dbg(pcr, "pcr->aspm_en = 0x%x\n", pcr->aspm_en);
1437 pcr_dbg(pcr, "pcr->sd30_drive_sel_1v8 = 0x%x\n",
1438 pcr->sd30_drive_sel_1v8);
1439 pcr_dbg(pcr, "pcr->sd30_drive_sel_3v3 = 0x%x\n",
1440 pcr->sd30_drive_sel_3v3);
1441 pcr_dbg(pcr, "pcr->card_drive_sel = 0x%x\n",
1442 pcr->card_drive_sel);
1443 pcr_dbg(pcr, "pcr->flags = 0x%x\n", pcr->flags);
1445 pcr->state = PDEV_STAT_IDLE;
1446 err = rtsx_pci_init_hw(pcr);
1455 static int rtsx_pci_probe(struct pci_dev *pcidev,
1456 const struct pci_device_id *id)
1458 struct rtsx_pcr *pcr;
1459 struct pcr_handle *handle;
1461 int ret, i, bar = 0;
1463 dev_dbg(&(pcidev->dev),
1464 ": Realtek PCI-E Card Reader found at %s [%04x:%04x] (rev %x)\n",
1465 pci_name(pcidev), (int)pcidev->vendor, (int)pcidev->device,
1466 (int)pcidev->revision);
1468 ret = dma_set_mask(&pcidev->dev, DMA_BIT_MASK(32));
1472 ret = pci_enable_device(pcidev);
1476 ret = pci_request_regions(pcidev, DRV_NAME_RTSX_PCI);
1480 pcr = kzalloc(sizeof(*pcr), GFP_KERNEL);
1486 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1493 idr_preload(GFP_KERNEL);
1494 spin_lock(&rtsx_pci_lock);
1495 ret = idr_alloc(&rtsx_pci_idr, pcr, 0, 0, GFP_NOWAIT);
1498 spin_unlock(&rtsx_pci_lock);
1504 dev_set_drvdata(&pcidev->dev, handle);
1506 if (CHK_PCI_PID(pcr, 0x525A))
1508 len = pci_resource_len(pcidev, bar);
1509 base = pci_resource_start(pcidev, bar);
1510 pcr->remap_addr = ioremap(base, len);
1511 if (!pcr->remap_addr) {
1516 pcr->rtsx_resv_buf = dma_alloc_coherent(&(pcidev->dev),
1517 RTSX_RESV_BUF_LEN, &(pcr->rtsx_resv_buf_addr),
1519 if (pcr->rtsx_resv_buf == NULL) {
1523 pcr->host_cmds_ptr = pcr->rtsx_resv_buf;
1524 pcr->host_cmds_addr = pcr->rtsx_resv_buf_addr;
1525 pcr->host_sg_tbl_ptr = pcr->rtsx_resv_buf + HOST_CMDS_BUF_LEN;
1526 pcr->host_sg_tbl_addr = pcr->rtsx_resv_buf_addr + HOST_CMDS_BUF_LEN;
1527 pcr->card_inserted = 0;
1528 pcr->card_removed = 0;
1529 INIT_DELAYED_WORK(&pcr->carddet_work, rtsx_pci_card_detect);
1531 pcr->msi_en = msi_en;
1533 ret = pci_enable_msi(pcidev);
1535 pcr->msi_en = false;
1538 ret = rtsx_pci_acquire_irq(pcr);
1542 pci_set_master(pcidev);
1543 synchronize_irq(pcr->irq);
1545 ret = rtsx_pci_init_chip(pcr);
1549 for (i = 0; i < ARRAY_SIZE(rtsx_pcr_cells); i++) {
1550 rtsx_pcr_cells[i].platform_data = handle;
1551 rtsx_pcr_cells[i].pdata_size = sizeof(*handle);
1555 ret = mfd_add_devices(&pcidev->dev, pcr->id, rtsx_pcr_cells,
1556 ARRAY_SIZE(rtsx_pcr_cells), NULL, 0, NULL);
1560 pm_runtime_allow(&pcidev->dev);
1561 pm_runtime_put(&pcidev->dev);
1568 free_irq(pcr->irq, (void *)pcr);
1571 pci_disable_msi(pcr->pci);
1572 dma_free_coherent(&(pcr->pci->dev), RTSX_RESV_BUF_LEN,
1573 pcr->rtsx_resv_buf, pcr->rtsx_resv_buf_addr);
1575 iounmap(pcr->remap_addr);
1577 spin_lock(&rtsx_pci_lock);
1578 idr_remove(&rtsx_pci_idr, pcr->id);
1579 spin_unlock(&rtsx_pci_lock);
1585 pci_release_regions(pcidev);
1587 pci_disable_device(pcidev);
1592 static void rtsx_pci_remove(struct pci_dev *pcidev)
1594 struct pcr_handle *handle = pci_get_drvdata(pcidev);
1595 struct rtsx_pcr *pcr = handle->pcr;
1597 pcr->remove_pci = true;
1599 pm_runtime_get_sync(&pcidev->dev);
1600 pm_runtime_forbid(&pcidev->dev);
1602 /* Disable interrupts at the pcr level */
1603 spin_lock_irq(&pcr->lock);
1604 rtsx_pci_writel(pcr, RTSX_BIER, 0);
1606 spin_unlock_irq(&pcr->lock);
1608 cancel_delayed_work_sync(&pcr->carddet_work);
1610 mfd_remove_devices(&pcidev->dev);
1612 dma_free_coherent(&(pcr->pci->dev), RTSX_RESV_BUF_LEN,
1613 pcr->rtsx_resv_buf, pcr->rtsx_resv_buf_addr);
1614 free_irq(pcr->irq, (void *)pcr);
1616 pci_disable_msi(pcr->pci);
1617 iounmap(pcr->remap_addr);
1619 pci_release_regions(pcidev);
1620 pci_disable_device(pcidev);
1622 spin_lock(&rtsx_pci_lock);
1623 idr_remove(&rtsx_pci_idr, pcr->id);
1624 spin_unlock(&rtsx_pci_lock);
1630 dev_dbg(&(pcidev->dev),
1631 ": Realtek PCI-E Card Reader at %s [%04x:%04x] has been removed\n",
1632 pci_name(pcidev), (int)pcidev->vendor, (int)pcidev->device);
1635 static int __maybe_unused rtsx_pci_suspend(struct device *dev_d)
1637 struct pci_dev *pcidev = to_pci_dev(dev_d);
1638 struct pcr_handle *handle = pci_get_drvdata(pcidev);
1639 struct rtsx_pcr *pcr = handle->pcr;
1641 dev_dbg(&(pcidev->dev), "--> %s\n", __func__);
1643 cancel_delayed_work_sync(&pcr->carddet_work);
1645 mutex_lock(&pcr->pcr_mutex);
1647 rtsx_pci_power_off(pcr, HOST_ENTER_S3, false);
1649 mutex_unlock(&pcr->pcr_mutex);
1653 static int __maybe_unused rtsx_pci_resume(struct device *dev_d)
1655 struct pci_dev *pcidev = to_pci_dev(dev_d);
1656 struct pcr_handle *handle = pci_get_drvdata(pcidev);
1657 struct rtsx_pcr *pcr = handle->pcr;
1660 dev_dbg(&(pcidev->dev), "--> %s\n", __func__);
1662 mutex_lock(&pcr->pcr_mutex);
1664 ret = rtsx_pci_write_register(pcr, HOST_SLEEP_STATE, 0x03, 0x00);
1668 ret = rtsx_pci_init_hw(pcr);
1673 mutex_unlock(&pcr->pcr_mutex);
1679 static void rtsx_enable_aspm(struct rtsx_pcr *pcr)
1681 if (pcr->ops->set_aspm)
1682 pcr->ops->set_aspm(pcr, true);
1684 rtsx_comm_set_aspm(pcr, true);
1687 static void rtsx_comm_pm_power_saving(struct rtsx_pcr *pcr)
1689 struct rtsx_cr_option *option = &pcr->option;
1691 if (option->ltr_enabled) {
1692 u32 latency = option->ltr_l1off_latency;
1694 if (rtsx_check_dev_flag(pcr, L1_SNOOZE_TEST_EN))
1695 mdelay(option->l1_snooze_delay);
1697 rtsx_set_ltr_latency(pcr, latency);
1700 if (rtsx_check_dev_flag(pcr, LTR_L1SS_PWR_GATE_EN))
1701 rtsx_set_l1off_sub_cfg_d0(pcr, 0);
1703 rtsx_enable_aspm(pcr);
1706 static void rtsx_pm_power_saving(struct rtsx_pcr *pcr)
1708 rtsx_comm_pm_power_saving(pcr);
1711 static void rtsx_pci_shutdown(struct pci_dev *pcidev)
1713 struct pcr_handle *handle = pci_get_drvdata(pcidev);
1714 struct rtsx_pcr *pcr = handle->pcr;
1716 dev_dbg(&(pcidev->dev), "--> %s\n", __func__);
1718 rtsx_pci_power_off(pcr, HOST_ENTER_S1, false);
1720 pci_disable_device(pcidev);
1721 free_irq(pcr->irq, (void *)pcr);
1723 pci_disable_msi(pcr->pci);
1726 static int rtsx_pci_runtime_idle(struct device *device)
1728 struct pci_dev *pcidev = to_pci_dev(device);
1729 struct pcr_handle *handle = pci_get_drvdata(pcidev);
1730 struct rtsx_pcr *pcr = handle->pcr;
1732 dev_dbg(device, "--> %s\n", __func__);
1734 mutex_lock(&pcr->pcr_mutex);
1736 pcr->state = PDEV_STAT_IDLE;
1738 if (pcr->ops->disable_auto_blink)
1739 pcr->ops->disable_auto_blink(pcr);
1740 if (pcr->ops->turn_off_led)
1741 pcr->ops->turn_off_led(pcr);
1743 rtsx_pm_power_saving(pcr);
1745 mutex_unlock(&pcr->pcr_mutex);
1748 pm_schedule_suspend(device, 10000);
1753 static int rtsx_pci_runtime_suspend(struct device *device)
1755 struct pci_dev *pcidev = to_pci_dev(device);
1756 struct pcr_handle *handle = pci_get_drvdata(pcidev);
1757 struct rtsx_pcr *pcr = handle->pcr;
1759 dev_dbg(device, "--> %s\n", __func__);
1761 cancel_delayed_work_sync(&pcr->carddet_work);
1763 mutex_lock(&pcr->pcr_mutex);
1764 rtsx_pci_power_off(pcr, HOST_ENTER_S3, true);
1766 mutex_unlock(&pcr->pcr_mutex);
1771 static int rtsx_pci_runtime_resume(struct device *device)
1773 struct pci_dev *pcidev = to_pci_dev(device);
1774 struct pcr_handle *handle = pci_get_drvdata(pcidev);
1775 struct rtsx_pcr *pcr = handle->pcr;
1777 dev_dbg(device, "--> %s\n", __func__);
1779 mutex_lock(&pcr->pcr_mutex);
1781 rtsx_pci_write_register(pcr, HOST_SLEEP_STATE, 0x03, 0x00);
1783 rtsx_pci_init_hw(pcr);
1785 if (pcr->slots[RTSX_SD_CARD].p_dev != NULL) {
1786 pcr->slots[RTSX_SD_CARD].card_event(
1787 pcr->slots[RTSX_SD_CARD].p_dev);
1790 mutex_unlock(&pcr->pcr_mutex);
1794 #else /* CONFIG_PM */
1796 #define rtsx_pci_shutdown NULL
1797 #define rtsx_pci_runtime_suspend NULL
1798 #define rtsx_pic_runtime_resume NULL
1800 #endif /* CONFIG_PM */
1802 static const struct dev_pm_ops rtsx_pci_pm_ops = {
1803 SET_SYSTEM_SLEEP_PM_OPS(rtsx_pci_suspend, rtsx_pci_resume)
1804 SET_RUNTIME_PM_OPS(rtsx_pci_runtime_suspend, rtsx_pci_runtime_resume, rtsx_pci_runtime_idle)
1807 static struct pci_driver rtsx_pci_driver = {
1808 .name = DRV_NAME_RTSX_PCI,
1809 .id_table = rtsx_pci_ids,
1810 .probe = rtsx_pci_probe,
1811 .remove = rtsx_pci_remove,
1812 .driver.pm = &rtsx_pci_pm_ops,
1813 .shutdown = rtsx_pci_shutdown,
1815 module_pci_driver(rtsx_pci_driver);
1817 MODULE_LICENSE("GPL");
1818 MODULE_AUTHOR("Wei WANG <wei_wang@realsil.com.cn>");
1819 MODULE_DESCRIPTION("Realtek PCI-E Card Reader Driver");