xref: /linux/drivers/misc/cardreader/rts5249.c (revision 71732e24609b5a7af96efc89aebde55f76c1de3e)
1aaf4989bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e455b69dSRui Feng /* Driver for Realtek PCI-Express card reader
3e455b69dSRui Feng  *
4e455b69dSRui Feng  * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
5e455b69dSRui Feng  *
6e455b69dSRui Feng  * Author:
7e455b69dSRui Feng  *   Wei WANG <wei_wang@realsil.com.cn>
8e455b69dSRui Feng  */
9e455b69dSRui Feng 
10e455b69dSRui Feng #include <linux/module.h>
11e455b69dSRui Feng #include <linux/delay.h>
12e455b69dSRui Feng #include <linux/rtsx_pci.h>
13e455b69dSRui Feng 
14e455b69dSRui Feng #include "rtsx_pcr.h"
15e455b69dSRui Feng 
16e455b69dSRui Feng static u8 rts5249_get_ic_version(struct rtsx_pcr *pcr)
17e455b69dSRui Feng {
18e455b69dSRui Feng 	u8 val;
19e455b69dSRui Feng 
20e455b69dSRui Feng 	rtsx_pci_read_register(pcr, DUMMY_REG_RESET_0, &val);
21e455b69dSRui Feng 	return val & 0x0F;
22e455b69dSRui Feng }
23e455b69dSRui Feng 
24e455b69dSRui Feng static void rts5249_fill_driving(struct rtsx_pcr *pcr, u8 voltage)
25e455b69dSRui Feng {
26e455b69dSRui Feng 	u8 driving_3v3[4][3] = {
27e455b69dSRui Feng 		{0x11, 0x11, 0x18},
28e455b69dSRui Feng 		{0x55, 0x55, 0x5C},
29e455b69dSRui Feng 		{0xFF, 0xFF, 0xFF},
30e455b69dSRui Feng 		{0x96, 0x96, 0x96},
31e455b69dSRui Feng 	};
32e455b69dSRui Feng 	u8 driving_1v8[4][3] = {
33e455b69dSRui Feng 		{0xC4, 0xC4, 0xC4},
34e455b69dSRui Feng 		{0x3C, 0x3C, 0x3C},
35e455b69dSRui Feng 		{0xFE, 0xFE, 0xFE},
36e455b69dSRui Feng 		{0xB3, 0xB3, 0xB3},
37e455b69dSRui Feng 	};
38e455b69dSRui Feng 	u8 (*driving)[3], drive_sel;
39e455b69dSRui Feng 
40e455b69dSRui Feng 	if (voltage == OUTPUT_3V3) {
41e455b69dSRui Feng 		driving = driving_3v3;
42e455b69dSRui Feng 		drive_sel = pcr->sd30_drive_sel_3v3;
43e455b69dSRui Feng 	} else {
44e455b69dSRui Feng 		driving = driving_1v8;
45e455b69dSRui Feng 		drive_sel = pcr->sd30_drive_sel_1v8;
46e455b69dSRui Feng 	}
47e455b69dSRui Feng 
48e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_CLK_DRIVE_SEL,
49e455b69dSRui Feng 			0xFF, driving[drive_sel][0]);
50e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_CMD_DRIVE_SEL,
51e455b69dSRui Feng 			0xFF, driving[drive_sel][1]);
52e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_DAT_DRIVE_SEL,
53e455b69dSRui Feng 			0xFF, driving[drive_sel][2]);
54e455b69dSRui Feng }
55e455b69dSRui Feng 
56e455b69dSRui Feng static void rtsx_base_fetch_vendor_settings(struct rtsx_pcr *pcr)
57e455b69dSRui Feng {
5822bf3251SBjorn Helgaas 	struct pci_dev *pdev = pcr->pci;
59e455b69dSRui Feng 	u32 reg;
60e455b69dSRui Feng 
6122bf3251SBjorn Helgaas 	pci_read_config_dword(pdev, PCR_SETTING_REG1, &reg);
62e455b69dSRui Feng 	pcr_dbg(pcr, "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG1, reg);
63e455b69dSRui Feng 
64e455b69dSRui Feng 	if (!rtsx_vendor_setting_valid(reg)) {
65e455b69dSRui Feng 		pcr_dbg(pcr, "skip fetch vendor setting\n");
66e455b69dSRui Feng 		return;
67e455b69dSRui Feng 	}
68e455b69dSRui Feng 
69e455b69dSRui Feng 	pcr->aspm_en = rtsx_reg_to_aspm(reg);
70e455b69dSRui Feng 	pcr->sd30_drive_sel_1v8 = rtsx_reg_to_sd30_drive_sel_1v8(reg);
71e455b69dSRui Feng 	pcr->card_drive_sel &= 0x3F;
72e455b69dSRui Feng 	pcr->card_drive_sel |= rtsx_reg_to_card_drive_sel(reg);
73e455b69dSRui Feng 
7422bf3251SBjorn Helgaas 	pci_read_config_dword(pdev, PCR_SETTING_REG2, &reg);
75e455b69dSRui Feng 	pcr_dbg(pcr, "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG2, reg);
765b4258f6SRicky Wu 
77*71732e24SKai-Heng Feng 	if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A))
785b4258f6SRicky Wu 		pcr->rtd3_en = rtsx_reg_to_rtd3_uhsii(reg);
795b4258f6SRicky Wu 
807c33e3c4SRicky Wu 	if (rtsx_check_mmc_support(reg))
817c33e3c4SRicky Wu 		pcr->extra_caps |= EXTRA_CAPS_NO_MMC;
82e455b69dSRui Feng 	pcr->sd30_drive_sel_3v3 = rtsx_reg_to_sd30_drive_sel_3v3(reg);
83e455b69dSRui Feng 	if (rtsx_reg_check_reverse_socket(reg))
84e455b69dSRui Feng 		pcr->flags |= PCR_REVERSE_SOCKET;
85e455b69dSRui Feng }
86e455b69dSRui Feng 
87e455b69dSRui Feng static void rts5249_init_from_cfg(struct rtsx_pcr *pcr)
88e455b69dSRui Feng {
8922bf3251SBjorn Helgaas 	struct pci_dev *pdev = pcr->pci;
90ed86a987SBjorn Helgaas 	int l1ss;
91e455b69dSRui Feng 	struct rtsx_cr_option *option = &(pcr->option);
92e455b69dSRui Feng 	u32 lval;
93e455b69dSRui Feng 
94ed86a987SBjorn Helgaas 	l1ss = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_L1SS);
95ed86a987SBjorn Helgaas 	if (!l1ss)
96ed86a987SBjorn Helgaas 		return;
97ed86a987SBjorn Helgaas 
98ed86a987SBjorn Helgaas 	pci_read_config_dword(pdev, l1ss + PCI_L1SS_CTL1, &lval);
99e455b69dSRui Feng 
1007c33e3c4SRicky Wu 	if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A)) {
1017c33e3c4SRicky Wu 		if (0 == (lval & 0x0F))
1027c33e3c4SRicky Wu 			rtsx_pci_enable_oobs_polling(pcr);
1037c33e3c4SRicky Wu 		else
1047c33e3c4SRicky Wu 			rtsx_pci_disable_oobs_polling(pcr);
1057c33e3c4SRicky Wu 	}
1067c33e3c4SRicky Wu 
1077c33e3c4SRicky Wu 
1087a4462a9SBjorn Helgaas 	if (lval & PCI_L1SS_CTL1_ASPM_L1_1)
109e455b69dSRui Feng 		rtsx_set_dev_flag(pcr, ASPM_L1_1_EN);
110e455b69dSRui Feng 
1117a4462a9SBjorn Helgaas 	if (lval & PCI_L1SS_CTL1_ASPM_L1_2)
112e455b69dSRui Feng 		rtsx_set_dev_flag(pcr, ASPM_L1_2_EN);
113e455b69dSRui Feng 
1147a4462a9SBjorn Helgaas 	if (lval & PCI_L1SS_CTL1_PCIPM_L1_1)
115e455b69dSRui Feng 		rtsx_set_dev_flag(pcr, PM_L1_1_EN);
116e455b69dSRui Feng 
1177a4462a9SBjorn Helgaas 	if (lval & PCI_L1SS_CTL1_PCIPM_L1_2)
118e455b69dSRui Feng 		rtsx_set_dev_flag(pcr, PM_L1_2_EN);
119e455b69dSRui Feng 
120e455b69dSRui Feng 	if (option->ltr_en) {
121e455b69dSRui Feng 		u16 val;
122e455b69dSRui Feng 
12322bf3251SBjorn Helgaas 		pcie_capability_read_word(pdev, PCI_EXP_DEVCTL2, &val);
124e455b69dSRui Feng 		if (val & PCI_EXP_DEVCTL2_LTR_EN) {
125e455b69dSRui Feng 			option->ltr_enabled = true;
126e455b69dSRui Feng 			option->ltr_active = true;
127e455b69dSRui Feng 			rtsx_set_ltr_latency(pcr, option->ltr_active_latency);
128e455b69dSRui Feng 		} else {
129e455b69dSRui Feng 			option->ltr_enabled = false;
130e455b69dSRui Feng 		}
131e455b69dSRui Feng 	}
132e455b69dSRui Feng }
133e455b69dSRui Feng 
134e455b69dSRui Feng static int rts5249_init_from_hw(struct rtsx_pcr *pcr)
135e455b69dSRui Feng {
136e455b69dSRui Feng 	struct rtsx_cr_option *option = &(pcr->option);
137e455b69dSRui Feng 
138e455b69dSRui Feng 	if (rtsx_check_dev_flag(pcr, ASPM_L1_1_EN | ASPM_L1_2_EN
139e455b69dSRui Feng 				| PM_L1_1_EN | PM_L1_2_EN))
140e455b69dSRui Feng 		option->force_clkreq_0 = false;
141e455b69dSRui Feng 	else
142e455b69dSRui Feng 		option->force_clkreq_0 = true;
143e455b69dSRui Feng 
144e455b69dSRui Feng 	return 0;
145e455b69dSRui Feng }
146e455b69dSRui Feng 
147*71732e24SKai-Heng Feng static void rts52xa_force_power_down(struct rtsx_pcr *pcr, u8 pm_state, bool runtime)
148*71732e24SKai-Heng Feng {
149*71732e24SKai-Heng Feng 	/* Set relink_time to 0 */
150*71732e24SKai-Heng Feng 	rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 1, MASK_8_BIT_DEF, 0);
151*71732e24SKai-Heng Feng 	rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 2, MASK_8_BIT_DEF, 0);
152*71732e24SKai-Heng Feng 	rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 3,
153*71732e24SKai-Heng Feng 				RELINK_TIME_MASK, 0);
154*71732e24SKai-Heng Feng 
155*71732e24SKai-Heng Feng 	rtsx_pci_write_register(pcr, RTS524A_PM_CTRL3,
156*71732e24SKai-Heng Feng 			D3_DELINK_MODE_EN, D3_DELINK_MODE_EN);
157*71732e24SKai-Heng Feng 
158*71732e24SKai-Heng Feng 	if (!runtime) {
159*71732e24SKai-Heng Feng 		rtsx_pci_write_register(pcr, RTS524A_AUTOLOAD_CFG1,
160*71732e24SKai-Heng Feng 				CD_RESUME_EN_MASK, 0);
161*71732e24SKai-Heng Feng 		rtsx_pci_write_register(pcr, RTS524A_PM_CTRL3, 0x01, 0x00);
162*71732e24SKai-Heng Feng 		rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL, 0x30, 0x20);
163*71732e24SKai-Heng Feng 	}
164*71732e24SKai-Heng Feng 
165*71732e24SKai-Heng Feng 	rtsx_pci_write_register(pcr, FPDCTL, ALL_POWER_DOWN, ALL_POWER_DOWN);
166*71732e24SKai-Heng Feng }
167*71732e24SKai-Heng Feng 
1687c33e3c4SRicky Wu static void rts52xa_save_content_from_efuse(struct rtsx_pcr *pcr)
1697c33e3c4SRicky Wu {
1707c33e3c4SRicky Wu 	u8 cnt, sv;
1717c33e3c4SRicky Wu 	u16 j = 0;
1727c33e3c4SRicky Wu 	u8 tmp;
1737c33e3c4SRicky Wu 	u8 val;
1747c33e3c4SRicky Wu 	int i;
1757c33e3c4SRicky Wu 
1767c33e3c4SRicky Wu 	rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL,
1777c33e3c4SRicky Wu 				REG_EFUSE_BYPASS | REG_EFUSE_POR, REG_EFUSE_POR);
1787c33e3c4SRicky Wu 	udelay(1);
1797c33e3c4SRicky Wu 
1807c33e3c4SRicky Wu 	pcr_dbg(pcr, "Enable efuse por!");
1817c33e3c4SRicky Wu 	pcr_dbg(pcr, "save efuse to autoload");
1827c33e3c4SRicky Wu 
1837c33e3c4SRicky Wu 	rtsx_pci_write_register(pcr, RTS525A_EFUSE_ADD, REG_EFUSE_ADD_MASK, 0x00);
1847c33e3c4SRicky Wu 	rtsx_pci_write_register(pcr, RTS525A_EFUSE_CTL,
1857c33e3c4SRicky Wu 				REG_EFUSE_ENABLE | REG_EFUSE_MODE, REG_EFUSE_ENABLE);
1867c33e3c4SRicky Wu 	/* Wait transfer end */
1877c33e3c4SRicky Wu 	for (j = 0; j < 1024; j++) {
1887c33e3c4SRicky Wu 		rtsx_pci_read_register(pcr, RTS525A_EFUSE_CTL, &tmp);
1897c33e3c4SRicky Wu 		if ((tmp & 0x80) == 0)
1907c33e3c4SRicky Wu 			break;
1917c33e3c4SRicky Wu 	}
1927c33e3c4SRicky Wu 	rtsx_pci_read_register(pcr, RTS525A_EFUSE_DATA, &val);
1937c33e3c4SRicky Wu 	cnt = val & 0x0F;
1947c33e3c4SRicky Wu 	sv = val & 0x10;
1957c33e3c4SRicky Wu 
1967c33e3c4SRicky Wu 	if (sv) {
1977c33e3c4SRicky Wu 		for (i = 0; i < 4; i++) {
1987c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, RTS525A_EFUSE_ADD,
1997c33e3c4SRicky Wu 				REG_EFUSE_ADD_MASK, 0x04 + i);
2007c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, RTS525A_EFUSE_CTL,
2017c33e3c4SRicky Wu 				REG_EFUSE_ENABLE | REG_EFUSE_MODE, REG_EFUSE_ENABLE);
2027c33e3c4SRicky Wu 			/* Wait transfer end */
2037c33e3c4SRicky Wu 			for (j = 0; j < 1024; j++) {
2047c33e3c4SRicky Wu 				rtsx_pci_read_register(pcr, RTS525A_EFUSE_CTL, &tmp);
2057c33e3c4SRicky Wu 				if ((tmp & 0x80) == 0)
2067c33e3c4SRicky Wu 					break;
2077c33e3c4SRicky Wu 			}
2087c33e3c4SRicky Wu 			rtsx_pci_read_register(pcr, RTS525A_EFUSE_DATA, &val);
2097c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, 0xFF04 + i, 0xFF, val);
2107c33e3c4SRicky Wu 		}
2117c33e3c4SRicky Wu 	} else {
2127c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF04, 0xFF, (u8)PCI_VID(pcr));
2137c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF05, 0xFF, (u8)(PCI_VID(pcr) >> 8));
2147c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF06, 0xFF, (u8)PCI_PID(pcr));
2157c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF07, 0xFF, (u8)(PCI_PID(pcr) >> 8));
2167c33e3c4SRicky Wu 	}
2177c33e3c4SRicky Wu 
2187c33e3c4SRicky Wu 	for (i = 0; i < cnt * 4; i++) {
2197c33e3c4SRicky Wu 		if (sv)
2207c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, RTS525A_EFUSE_ADD,
2217c33e3c4SRicky Wu 				REG_EFUSE_ADD_MASK, 0x08 + i);
2227c33e3c4SRicky Wu 		else
2237c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, RTS525A_EFUSE_ADD,
2247c33e3c4SRicky Wu 				REG_EFUSE_ADD_MASK, 0x04 + i);
2257c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, RTS525A_EFUSE_CTL,
2267c33e3c4SRicky Wu 				REG_EFUSE_ENABLE | REG_EFUSE_MODE, REG_EFUSE_ENABLE);
2277c33e3c4SRicky Wu 		/* Wait transfer end */
2287c33e3c4SRicky Wu 		for (j = 0; j < 1024; j++) {
2297c33e3c4SRicky Wu 			rtsx_pci_read_register(pcr, RTS525A_EFUSE_CTL, &tmp);
2307c33e3c4SRicky Wu 			if ((tmp & 0x80) == 0)
2317c33e3c4SRicky Wu 				break;
2327c33e3c4SRicky Wu 		}
2337c33e3c4SRicky Wu 		rtsx_pci_read_register(pcr, RTS525A_EFUSE_DATA, &val);
2347c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF08 + i, 0xFF, val);
2357c33e3c4SRicky Wu 	}
2367c33e3c4SRicky Wu 	rtsx_pci_write_register(pcr, 0xFF00, 0xFF, (cnt & 0x7F) | 0x80);
2377c33e3c4SRicky Wu 	rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL,
2387c33e3c4SRicky Wu 		REG_EFUSE_BYPASS | REG_EFUSE_POR, REG_EFUSE_BYPASS);
2397c33e3c4SRicky Wu 	pcr_dbg(pcr, "Disable efuse por!");
2407c33e3c4SRicky Wu }
2417c33e3c4SRicky Wu 
2427c33e3c4SRicky Wu static void rts52xa_save_content_to_autoload_space(struct rtsx_pcr *pcr)
2437c33e3c4SRicky Wu {
2447c33e3c4SRicky Wu 	u8 val;
2457c33e3c4SRicky Wu 
2467c33e3c4SRicky Wu 	rtsx_pci_read_register(pcr, RESET_LOAD_REG, &val);
2477c33e3c4SRicky Wu 	if (val & 0x02) {
2487c33e3c4SRicky Wu 		rtsx_pci_read_register(pcr, RTS525A_BIOS_CFG, &val);
2497c33e3c4SRicky Wu 		if (val & RTS525A_LOAD_BIOS_FLAG) {
2507c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, RTS525A_BIOS_CFG,
2517c33e3c4SRicky Wu 				RTS525A_LOAD_BIOS_FLAG, RTS525A_CLEAR_BIOS_FLAG);
2527c33e3c4SRicky Wu 
2537c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL,
2547c33e3c4SRicky Wu 				REG_EFUSE_POWER_MASK, REG_EFUSE_POWERON);
2557c33e3c4SRicky Wu 
2567c33e3c4SRicky Wu 			pcr_dbg(pcr, "Power ON efuse!");
2577c33e3c4SRicky Wu 			mdelay(1);
2587c33e3c4SRicky Wu 			rts52xa_save_content_from_efuse(pcr);
2597c33e3c4SRicky Wu 		} else {
2607c33e3c4SRicky Wu 			rtsx_pci_read_register(pcr, RTS524A_PME_FORCE_CTL, &val);
2617c33e3c4SRicky Wu 			if (!(val & 0x08))
2627c33e3c4SRicky Wu 				rts52xa_save_content_from_efuse(pcr);
2637c33e3c4SRicky Wu 		}
2647c33e3c4SRicky Wu 	} else {
2657c33e3c4SRicky Wu 		pcr_dbg(pcr, "Load from autoload");
2667c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF00, 0xFF, 0x80);
2677c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF04, 0xFF, (u8)PCI_VID(pcr));
2687c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF05, 0xFF, (u8)(PCI_VID(pcr) >> 8));
2697c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF06, 0xFF, (u8)PCI_PID(pcr));
2707c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF07, 0xFF, (u8)(PCI_PID(pcr) >> 8));
2717c33e3c4SRicky Wu 	}
2727c33e3c4SRicky Wu }
2737c33e3c4SRicky Wu 
274e455b69dSRui Feng static int rts5249_extra_init_hw(struct rtsx_pcr *pcr)
275e455b69dSRui Feng {
276e455b69dSRui Feng 	struct rtsx_cr_option *option = &(pcr->option);
277e455b69dSRui Feng 
278e455b69dSRui Feng 	rts5249_init_from_cfg(pcr);
279e455b69dSRui Feng 	rts5249_init_from_hw(pcr);
280e455b69dSRui Feng 
281e455b69dSRui Feng 	rtsx_pci_init_cmd(pcr);
282e455b69dSRui Feng 
2837c33e3c4SRicky Wu 	if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A))
2847c33e3c4SRicky Wu 		rts52xa_save_content_to_autoload_space(pcr);
2857c33e3c4SRicky Wu 
286e455b69dSRui Feng 	/* Rest L1SUB Config */
287e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, L1SUB_CONFIG3, 0xFF, 0x00);
288e455b69dSRui Feng 	/* Configure GPIO as output */
289e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, GPIO_CTL, 0x02, 0x02);
290e455b69dSRui Feng 	/* Reset ASPM state to default value */
291e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, ASPM_FORCE_CTL, 0x3F, 0);
292e455b69dSRui Feng 	/* Switch LDO3318 source from DV33 to card_3v3 */
293e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x00);
294e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x01);
295e455b69dSRui Feng 	/* LED shine disabled, set initial shine cycle period */
296e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OLT_LED_CTL, 0x0F, 0x02);
297e455b69dSRui Feng 	/* Configure driving */
298e455b69dSRui Feng 	rts5249_fill_driving(pcr, OUTPUT_3V3);
299e455b69dSRui Feng 	if (pcr->flags & PCR_REVERSE_SOCKET)
300e455b69dSRui Feng 		rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0xB0, 0xB0);
301e455b69dSRui Feng 	else
302e455b69dSRui Feng 		rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0xB0, 0x80);
303e455b69dSRui Feng 
3047c33e3c4SRicky Wu 	rtsx_pci_send_cmd(pcr, CMD_TIMEOUT_DEF);
3057c33e3c4SRicky Wu 
306*71732e24SKai-Heng Feng 	if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A)) {
3077c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, REG_VREF, PWD_SUSPND_EN, PWD_SUSPND_EN);
308*71732e24SKai-Heng Feng 		rtsx_pci_write_register(pcr, RTS524A_AUTOLOAD_CFG1,
309*71732e24SKai-Heng Feng 			CD_RESUME_EN_MASK, CD_RESUME_EN_MASK);
310*71732e24SKai-Heng Feng 	}
3115b4258f6SRicky Wu 
3125b4258f6SRicky Wu 	if (pcr->rtd3_en) {
3135b4258f6SRicky Wu 		if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A)) {
3145b4258f6SRicky Wu 			rtsx_pci_write_register(pcr, RTS524A_PM_CTRL3, 0x01, 0x01);
3155b4258f6SRicky Wu 			rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL, 0x30, 0x30);
3165b4258f6SRicky Wu 		} else {
3175b4258f6SRicky Wu 			rtsx_pci_write_register(pcr, PM_CTRL3, 0x01, 0x01);
3185b4258f6SRicky Wu 			rtsx_pci_write_register(pcr, PME_FORCE_CTL, 0xFF, 0x33);
3195b4258f6SRicky Wu 		}
3205b4258f6SRicky Wu 	} else {
3215b4258f6SRicky Wu 		if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A)) {
3227c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, RTS524A_PM_CTRL3, 0x01, 0x00);
3237c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL, 0x30, 0x20);
3247c33e3c4SRicky Wu 		} else {
3257c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, PME_FORCE_CTL, 0xFF, 0x30);
3267c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, PM_CTRL3, 0x01, 0x00);
3277c33e3c4SRicky Wu 		}
3285b4258f6SRicky Wu 	}
3295b4258f6SRicky Wu 
3307c33e3c4SRicky Wu 
331e455b69dSRui Feng 	/*
332e455b69dSRui Feng 	 * If u_force_clkreq_0 is enabled, CLKREQ# PIN will be forced
333e455b69dSRui Feng 	 * to drive low, and we forcibly request clock.
334e455b69dSRui Feng 	 */
335e455b69dSRui Feng 	if (option->force_clkreq_0)
3367c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, PETXCFG,
337e455b69dSRui Feng 			FORCE_CLKREQ_DELINK_MASK, FORCE_CLKREQ_LOW);
338e455b69dSRui Feng 	else
3397c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, PETXCFG,
340e455b69dSRui Feng 			FORCE_CLKREQ_DELINK_MASK, FORCE_CLKREQ_HIGH);
341e455b69dSRui Feng 
3427c33e3c4SRicky Wu 	rtsx_pci_write_register(pcr, pcr->reg_pm_ctrl3, 0x10, 0x00);
3437c33e3c4SRicky Wu 	if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A)) {
3447c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL,
3457c33e3c4SRicky Wu 				REG_EFUSE_POWER_MASK, REG_EFUSE_POWEROFF);
3467c33e3c4SRicky Wu 		pcr_dbg(pcr, "Power OFF efuse!");
3477c33e3c4SRicky Wu 	}
3487c33e3c4SRicky Wu 
3497c33e3c4SRicky Wu 	return 0;
350e455b69dSRui Feng }
351e455b69dSRui Feng 
352e455b69dSRui Feng static int rts5249_optimize_phy(struct rtsx_pcr *pcr)
353e455b69dSRui Feng {
354e455b69dSRui Feng 	int err;
355e455b69dSRui Feng 
356e455b69dSRui Feng 	err = rtsx_pci_write_register(pcr, PM_CTRL3, D3_DELINK_MODE_EN, 0x00);
357e455b69dSRui Feng 	if (err < 0)
358e455b69dSRui Feng 		return err;
359e455b69dSRui Feng 
360e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_REV,
361e455b69dSRui Feng 			PHY_REV_RESV | PHY_REV_RXIDLE_LATCHED |
362e455b69dSRui Feng 			PHY_REV_P1_EN | PHY_REV_RXIDLE_EN |
363e455b69dSRui Feng 			PHY_REV_CLKREQ_TX_EN | PHY_REV_RX_PWST |
364e455b69dSRui Feng 			PHY_REV_CLKREQ_DT_1_0 | PHY_REV_STOP_CLKRD |
365e455b69dSRui Feng 			PHY_REV_STOP_CLKWR);
366e455b69dSRui Feng 	if (err < 0)
367e455b69dSRui Feng 		return err;
368e455b69dSRui Feng 
369e455b69dSRui Feng 	msleep(1);
370e455b69dSRui Feng 
371e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_BPCR,
372e455b69dSRui Feng 			PHY_BPCR_IBRXSEL | PHY_BPCR_IBTXSEL |
373e455b69dSRui Feng 			PHY_BPCR_IB_FILTER | PHY_BPCR_CMIRROR_EN);
374e455b69dSRui Feng 	if (err < 0)
375e455b69dSRui Feng 		return err;
376e455b69dSRui Feng 
377e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_PCR,
378e455b69dSRui Feng 			PHY_PCR_FORCE_CODE | PHY_PCR_OOBS_CALI_50 |
379e455b69dSRui Feng 			PHY_PCR_OOBS_VCM_08 | PHY_PCR_OOBS_SEN_90 |
380e455b69dSRui Feng 			PHY_PCR_RSSI_EN | PHY_PCR_RX10K);
381e455b69dSRui Feng 	if (err < 0)
382e455b69dSRui Feng 		return err;
383e455b69dSRui Feng 
384e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_RCR2,
385e455b69dSRui Feng 			PHY_RCR2_EMPHASE_EN | PHY_RCR2_NADJR |
386e455b69dSRui Feng 			PHY_RCR2_CDR_SR_2 | PHY_RCR2_FREQSEL_12 |
387e455b69dSRui Feng 			PHY_RCR2_CDR_SC_12P | PHY_RCR2_CALIB_LATE);
388e455b69dSRui Feng 	if (err < 0)
389e455b69dSRui Feng 		return err;
390e455b69dSRui Feng 
391e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_FLD4,
392e455b69dSRui Feng 			PHY_FLD4_FLDEN_SEL | PHY_FLD4_REQ_REF |
393e455b69dSRui Feng 			PHY_FLD4_RXAMP_OFF | PHY_FLD4_REQ_ADDA |
394e455b69dSRui Feng 			PHY_FLD4_BER_COUNT | PHY_FLD4_BER_TIMER |
395e455b69dSRui Feng 			PHY_FLD4_BER_CHK_EN);
396e455b69dSRui Feng 	if (err < 0)
397e455b69dSRui Feng 		return err;
398e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_RDR,
399e455b69dSRui Feng 			PHY_RDR_RXDSEL_1_9 | PHY_SSC_AUTO_PWD);
400e455b69dSRui Feng 	if (err < 0)
401e455b69dSRui Feng 		return err;
402e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_RCR1,
403e455b69dSRui Feng 			PHY_RCR1_ADP_TIME_4 | PHY_RCR1_VCO_COARSE);
404e455b69dSRui Feng 	if (err < 0)
405e455b69dSRui Feng 		return err;
406e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_FLD3,
407e455b69dSRui Feng 			PHY_FLD3_TIMER_4 | PHY_FLD3_TIMER_6 |
408e455b69dSRui Feng 			PHY_FLD3_RXDELINK);
409e455b69dSRui Feng 	if (err < 0)
410e455b69dSRui Feng 		return err;
411e455b69dSRui Feng 
412e455b69dSRui Feng 	return rtsx_pci_write_phy_register(pcr, PHY_TUNE,
413e455b69dSRui Feng 			PHY_TUNE_TUNEREF_1_0 | PHY_TUNE_VBGSEL_1252 |
414e455b69dSRui Feng 			PHY_TUNE_SDBUS_33 | PHY_TUNE_TUNED18 |
415e455b69dSRui Feng 			PHY_TUNE_TUNED12 | PHY_TUNE_TUNEA12);
416e455b69dSRui Feng }
417e455b69dSRui Feng 
418e455b69dSRui Feng static int rtsx_base_turn_on_led(struct rtsx_pcr *pcr)
419e455b69dSRui Feng {
420e455b69dSRui Feng 	return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x02);
421e455b69dSRui Feng }
422e455b69dSRui Feng 
423e455b69dSRui Feng static int rtsx_base_turn_off_led(struct rtsx_pcr *pcr)
424e455b69dSRui Feng {
425e455b69dSRui Feng 	return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x00);
426e455b69dSRui Feng }
427e455b69dSRui Feng 
428e455b69dSRui Feng static int rtsx_base_enable_auto_blink(struct rtsx_pcr *pcr)
429e455b69dSRui Feng {
430e455b69dSRui Feng 	return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x08);
431e455b69dSRui Feng }
432e455b69dSRui Feng 
433e455b69dSRui Feng static int rtsx_base_disable_auto_blink(struct rtsx_pcr *pcr)
434e455b69dSRui Feng {
435e455b69dSRui Feng 	return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x00);
436e455b69dSRui Feng }
437e455b69dSRui Feng 
438e455b69dSRui Feng static int rtsx_base_card_power_on(struct rtsx_pcr *pcr, int card)
439e455b69dSRui Feng {
440e455b69dSRui Feng 	int err;
441bede03a5SRickyWu 	struct rtsx_cr_option *option = &pcr->option;
442bede03a5SRickyWu 
443bede03a5SRickyWu 	if (option->ocp_en)
444bede03a5SRickyWu 		rtsx_pci_enable_ocp(pcr);
445e455b69dSRui Feng 
446e455b69dSRui Feng 	rtsx_pci_init_cmd(pcr);
447e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
448e455b69dSRui Feng 			SD_POWER_MASK, SD_VCC_PARTIAL_POWER_ON);
449e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
450e455b69dSRui Feng 			LDO3318_PWR_MASK, 0x02);
451e455b69dSRui Feng 	err = rtsx_pci_send_cmd(pcr, 100);
452e455b69dSRui Feng 	if (err < 0)
453e455b69dSRui Feng 		return err;
454e455b69dSRui Feng 
455e455b69dSRui Feng 	msleep(5);
456e455b69dSRui Feng 
457e455b69dSRui Feng 	rtsx_pci_init_cmd(pcr);
458e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
459e455b69dSRui Feng 			SD_POWER_MASK, SD_VCC_POWER_ON);
460e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
461e455b69dSRui Feng 			LDO3318_PWR_MASK, 0x06);
462e455b69dSRui Feng 	return rtsx_pci_send_cmd(pcr, 100);
463e455b69dSRui Feng }
464e455b69dSRui Feng 
465e455b69dSRui Feng static int rtsx_base_card_power_off(struct rtsx_pcr *pcr, int card)
466e455b69dSRui Feng {
467bede03a5SRickyWu 	struct rtsx_cr_option *option = &pcr->option;
468bede03a5SRickyWu 
469bede03a5SRickyWu 	if (option->ocp_en)
470bede03a5SRickyWu 		rtsx_pci_disable_ocp(pcr);
471bede03a5SRickyWu 
472bede03a5SRickyWu 	rtsx_pci_write_register(pcr, CARD_PWR_CTL, SD_POWER_MASK, SD_POWER_OFF);
473bede03a5SRickyWu 
474bede03a5SRickyWu 	rtsx_pci_write_register(pcr, PWR_GATE_CTRL, LDO3318_PWR_MASK, 0x00);
475bede03a5SRickyWu 	return 0;
476e455b69dSRui Feng }
477e455b69dSRui Feng 
478e455b69dSRui Feng static int rtsx_base_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
479e455b69dSRui Feng {
480e455b69dSRui Feng 	int err;
481e455b69dSRui Feng 	u16 append;
482e455b69dSRui Feng 
483e455b69dSRui Feng 	switch (voltage) {
484e455b69dSRui Feng 	case OUTPUT_3V3:
485e455b69dSRui Feng 		err = rtsx_pci_update_phy(pcr, PHY_TUNE, PHY_TUNE_VOLTAGE_MASK,
486e455b69dSRui Feng 			PHY_TUNE_VOLTAGE_3V3);
487e455b69dSRui Feng 		if (err < 0)
488e455b69dSRui Feng 			return err;
489e455b69dSRui Feng 		break;
490e455b69dSRui Feng 	case OUTPUT_1V8:
491e455b69dSRui Feng 		append = PHY_TUNE_D18_1V8;
492e455b69dSRui Feng 		if (CHK_PCI_PID(pcr, 0x5249)) {
493e455b69dSRui Feng 			err = rtsx_pci_update_phy(pcr, PHY_BACR,
494e455b69dSRui Feng 				PHY_BACR_BASIC_MASK, 0);
495e455b69dSRui Feng 			if (err < 0)
496e455b69dSRui Feng 				return err;
497e455b69dSRui Feng 			append = PHY_TUNE_D18_1V7;
498e455b69dSRui Feng 		}
499e455b69dSRui Feng 
500e455b69dSRui Feng 		err = rtsx_pci_update_phy(pcr, PHY_TUNE, PHY_TUNE_VOLTAGE_MASK,
501e455b69dSRui Feng 			append);
502e455b69dSRui Feng 		if (err < 0)
503e455b69dSRui Feng 			return err;
504e455b69dSRui Feng 		break;
505e455b69dSRui Feng 	default:
506e455b69dSRui Feng 		pcr_dbg(pcr, "unknown output voltage %d\n", voltage);
507e455b69dSRui Feng 		return -EINVAL;
508e455b69dSRui Feng 	}
509e455b69dSRui Feng 
510e455b69dSRui Feng 	/* set pad drive */
511e455b69dSRui Feng 	rtsx_pci_init_cmd(pcr);
512e455b69dSRui Feng 	rts5249_fill_driving(pcr, voltage);
513e455b69dSRui Feng 	return rtsx_pci_send_cmd(pcr, 100);
514e455b69dSRui Feng }
515e455b69dSRui Feng 
516e455b69dSRui Feng static const struct pcr_ops rts5249_pcr_ops = {
517e455b69dSRui Feng 	.fetch_vendor_settings = rtsx_base_fetch_vendor_settings,
518e455b69dSRui Feng 	.extra_init_hw = rts5249_extra_init_hw,
519e455b69dSRui Feng 	.optimize_phy = rts5249_optimize_phy,
520e455b69dSRui Feng 	.turn_on_led = rtsx_base_turn_on_led,
521e455b69dSRui Feng 	.turn_off_led = rtsx_base_turn_off_led,
522e455b69dSRui Feng 	.enable_auto_blink = rtsx_base_enable_auto_blink,
523e455b69dSRui Feng 	.disable_auto_blink = rtsx_base_disable_auto_blink,
524e455b69dSRui Feng 	.card_power_on = rtsx_base_card_power_on,
525e455b69dSRui Feng 	.card_power_off = rtsx_base_card_power_off,
526e455b69dSRui Feng 	.switch_output_voltage = rtsx_base_switch_output_voltage,
527e455b69dSRui Feng };
528e455b69dSRui Feng 
529e455b69dSRui Feng /* SD Pull Control Enable:
530e455b69dSRui Feng  *     SD_DAT[3:0] ==> pull up
531e455b69dSRui Feng  *     SD_CD       ==> pull up
532e455b69dSRui Feng  *     SD_WP       ==> pull up
533e455b69dSRui Feng  *     SD_CMD      ==> pull up
534e455b69dSRui Feng  *     SD_CLK      ==> pull down
535e455b69dSRui Feng  */
536e455b69dSRui Feng static const u32 rts5249_sd_pull_ctl_enable_tbl[] = {
537e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL1, 0x66),
538e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
539e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9),
540e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL4, 0xAA),
541e455b69dSRui Feng 	0,
542e455b69dSRui Feng };
543e455b69dSRui Feng 
544e455b69dSRui Feng /* SD Pull Control Disable:
545e455b69dSRui Feng  *     SD_DAT[3:0] ==> pull down
546e455b69dSRui Feng  *     SD_CD       ==> pull up
547e455b69dSRui Feng  *     SD_WP       ==> pull down
548e455b69dSRui Feng  *     SD_CMD      ==> pull down
549e455b69dSRui Feng  *     SD_CLK      ==> pull down
550e455b69dSRui Feng  */
551e455b69dSRui Feng static const u32 rts5249_sd_pull_ctl_disable_tbl[] = {
552e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL1, 0x66),
553e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
554e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5),
555e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
556e455b69dSRui Feng 	0,
557e455b69dSRui Feng };
558e455b69dSRui Feng 
559e455b69dSRui Feng /* MS Pull Control Enable:
560e455b69dSRui Feng  *     MS CD       ==> pull up
561e455b69dSRui Feng  *     others      ==> pull down
562e455b69dSRui Feng  */
563e455b69dSRui Feng static const u32 rts5249_ms_pull_ctl_enable_tbl[] = {
564e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
565e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
566e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
567e455b69dSRui Feng 	0,
568e455b69dSRui Feng };
569e455b69dSRui Feng 
570e455b69dSRui Feng /* MS Pull Control Disable:
571e455b69dSRui Feng  *     MS CD       ==> pull up
572e455b69dSRui Feng  *     others      ==> pull down
573e455b69dSRui Feng  */
574e455b69dSRui Feng static const u32 rts5249_ms_pull_ctl_disable_tbl[] = {
575e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
576e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
577e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
578e455b69dSRui Feng 	0,
579e455b69dSRui Feng };
580e455b69dSRui Feng 
581e455b69dSRui Feng void rts5249_init_params(struct rtsx_pcr *pcr)
582e455b69dSRui Feng {
583e455b69dSRui Feng 	struct rtsx_cr_option *option = &(pcr->option);
584e455b69dSRui Feng 
585e455b69dSRui Feng 	pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104;
586e455b69dSRui Feng 	pcr->num_slots = 2;
587e455b69dSRui Feng 	pcr->ops = &rts5249_pcr_ops;
588e455b69dSRui Feng 
589e455b69dSRui Feng 	pcr->flags = 0;
590e455b69dSRui Feng 	pcr->card_drive_sel = RTSX_CARD_DRIVE_DEFAULT;
591e455b69dSRui Feng 	pcr->sd30_drive_sel_1v8 = CFG_DRIVER_TYPE_B;
592e455b69dSRui Feng 	pcr->sd30_drive_sel_3v3 = CFG_DRIVER_TYPE_B;
593e455b69dSRui Feng 	pcr->aspm_en = ASPM_L1_EN;
5943df4fce7SRicky Wu 	pcr->aspm_mode = ASPM_MODE_CFG;
595e455b69dSRui Feng 	pcr->tx_initial_phase = SET_CLOCK_PHASE(1, 29, 16);
596e455b69dSRui Feng 	pcr->rx_initial_phase = SET_CLOCK_PHASE(24, 6, 5);
597e455b69dSRui Feng 
598e455b69dSRui Feng 	pcr->ic_version = rts5249_get_ic_version(pcr);
599e455b69dSRui Feng 	pcr->sd_pull_ctl_enable_tbl = rts5249_sd_pull_ctl_enable_tbl;
600e455b69dSRui Feng 	pcr->sd_pull_ctl_disable_tbl = rts5249_sd_pull_ctl_disable_tbl;
601e455b69dSRui Feng 	pcr->ms_pull_ctl_enable_tbl = rts5249_ms_pull_ctl_enable_tbl;
602e455b69dSRui Feng 	pcr->ms_pull_ctl_disable_tbl = rts5249_ms_pull_ctl_disable_tbl;
603e455b69dSRui Feng 
604e455b69dSRui Feng 	pcr->reg_pm_ctrl3 = PM_CTRL3;
605e455b69dSRui Feng 
606e455b69dSRui Feng 	option->dev_flags = (LTR_L1SS_PWR_GATE_CHECK_CARD_EN
607e455b69dSRui Feng 				| LTR_L1SS_PWR_GATE_EN);
608e455b69dSRui Feng 	option->ltr_en = true;
609e455b69dSRui Feng 
610e455b69dSRui Feng 	/* Init latency of active, idle, L1OFF to 60us, 300us, 3ms */
611e455b69dSRui Feng 	option->ltr_active_latency = LTR_ACTIVE_LATENCY_DEF;
612e455b69dSRui Feng 	option->ltr_idle_latency = LTR_IDLE_LATENCY_DEF;
613e455b69dSRui Feng 	option->ltr_l1off_latency = LTR_L1OFF_LATENCY_DEF;
614e455b69dSRui Feng 	option->l1_snooze_delay = L1_SNOOZE_DELAY_DEF;
615e455b69dSRui Feng 	option->ltr_l1off_sspwrgate = LTR_L1OFF_SSPWRGATE_5249_DEF;
616e455b69dSRui Feng 	option->ltr_l1off_snooze_sspwrgate =
617e455b69dSRui Feng 		LTR_L1OFF_SNOOZE_SSPWRGATE_5249_DEF;
618e455b69dSRui Feng }
619e455b69dSRui Feng 
620e455b69dSRui Feng static int rts524a_write_phy(struct rtsx_pcr *pcr, u8 addr, u16 val)
621e455b69dSRui Feng {
622e455b69dSRui Feng 	addr = addr & 0x80 ? (addr & 0x7F) | 0x40 : addr;
623e455b69dSRui Feng 
624e455b69dSRui Feng 	return __rtsx_pci_write_phy_register(pcr, addr, val);
625e455b69dSRui Feng }
626e455b69dSRui Feng 
627e455b69dSRui Feng static int rts524a_read_phy(struct rtsx_pcr *pcr, u8 addr, u16 *val)
628e455b69dSRui Feng {
629e455b69dSRui Feng 	addr = addr & 0x80 ? (addr & 0x7F) | 0x40 : addr;
630e455b69dSRui Feng 
631e455b69dSRui Feng 	return __rtsx_pci_read_phy_register(pcr, addr, val);
632e455b69dSRui Feng }
633e455b69dSRui Feng 
634e455b69dSRui Feng static int rts524a_optimize_phy(struct rtsx_pcr *pcr)
635e455b69dSRui Feng {
636e455b69dSRui Feng 	int err;
637e455b69dSRui Feng 
638e455b69dSRui Feng 	err = rtsx_pci_write_register(pcr, RTS524A_PM_CTRL3,
639e455b69dSRui Feng 		D3_DELINK_MODE_EN, 0x00);
640e455b69dSRui Feng 	if (err < 0)
641e455b69dSRui Feng 		return err;
642e455b69dSRui Feng 
643e455b69dSRui Feng 	rtsx_pci_write_phy_register(pcr, PHY_PCR,
644e455b69dSRui Feng 		PHY_PCR_FORCE_CODE | PHY_PCR_OOBS_CALI_50 |
645e455b69dSRui Feng 		PHY_PCR_OOBS_VCM_08 | PHY_PCR_OOBS_SEN_90 | PHY_PCR_RSSI_EN);
646e455b69dSRui Feng 	rtsx_pci_write_phy_register(pcr, PHY_SSCCR3,
647e455b69dSRui Feng 		PHY_SSCCR3_STEP_IN | PHY_SSCCR3_CHECK_DELAY);
648e455b69dSRui Feng 
649e455b69dSRui Feng 	if (is_version(pcr, 0x524A, IC_VER_A)) {
650e455b69dSRui Feng 		rtsx_pci_write_phy_register(pcr, PHY_SSCCR3,
651e455b69dSRui Feng 			PHY_SSCCR3_STEP_IN | PHY_SSCCR3_CHECK_DELAY);
652e455b69dSRui Feng 		rtsx_pci_write_phy_register(pcr, PHY_SSCCR2,
653e455b69dSRui Feng 			PHY_SSCCR2_PLL_NCODE | PHY_SSCCR2_TIME0 |
654e455b69dSRui Feng 			PHY_SSCCR2_TIME2_WIDTH);
655e455b69dSRui Feng 		rtsx_pci_write_phy_register(pcr, PHY_ANA1A,
656e455b69dSRui Feng 			PHY_ANA1A_TXR_LOOPBACK | PHY_ANA1A_RXT_BIST |
657e455b69dSRui Feng 			PHY_ANA1A_TXR_BIST | PHY_ANA1A_REV);
658e455b69dSRui Feng 		rtsx_pci_write_phy_register(pcr, PHY_ANA1D,
659e455b69dSRui Feng 			PHY_ANA1D_DEBUG_ADDR);
660e455b69dSRui Feng 		rtsx_pci_write_phy_register(pcr, PHY_DIG1E,
661e455b69dSRui Feng 			PHY_DIG1E_REV | PHY_DIG1E_D0_X_D1 |
662e455b69dSRui Feng 			PHY_DIG1E_RX_ON_HOST | PHY_DIG1E_RCLK_REF_HOST |
663e455b69dSRui Feng 			PHY_DIG1E_RCLK_TX_EN_KEEP |
664e455b69dSRui Feng 			PHY_DIG1E_RCLK_TX_TERM_KEEP |
665e455b69dSRui Feng 			PHY_DIG1E_RCLK_RX_EIDLE_ON | PHY_DIG1E_TX_TERM_KEEP |
666e455b69dSRui Feng 			PHY_DIG1E_RX_TERM_KEEP | PHY_DIG1E_TX_EN_KEEP |
667e455b69dSRui Feng 			PHY_DIG1E_RX_EN_KEEP);
668e455b69dSRui Feng 	}
669e455b69dSRui Feng 
670e455b69dSRui Feng 	rtsx_pci_write_phy_register(pcr, PHY_ANA08,
671e455b69dSRui Feng 		PHY_ANA08_RX_EQ_DCGAIN | PHY_ANA08_SEL_RX_EN |
672e455b69dSRui Feng 		PHY_ANA08_RX_EQ_VAL | PHY_ANA08_SCP | PHY_ANA08_SEL_IPI);
673e455b69dSRui Feng 
674e455b69dSRui Feng 	return 0;
675e455b69dSRui Feng }
676e455b69dSRui Feng 
677e455b69dSRui Feng static int rts524a_extra_init_hw(struct rtsx_pcr *pcr)
678e455b69dSRui Feng {
679e455b69dSRui Feng 	rts5249_extra_init_hw(pcr);
680e455b69dSRui Feng 
681e455b69dSRui Feng 	rtsx_pci_write_register(pcr, FUNC_FORCE_CTL,
682e455b69dSRui Feng 		FORCE_ASPM_L1_EN, FORCE_ASPM_L1_EN);
683e455b69dSRui Feng 	rtsx_pci_write_register(pcr, PM_EVENT_DEBUG, PME_DEBUG_0, PME_DEBUG_0);
684e455b69dSRui Feng 	rtsx_pci_write_register(pcr, LDO_VCC_CFG1, LDO_VCC_LMT_EN,
685e455b69dSRui Feng 		LDO_VCC_LMT_EN);
686e455b69dSRui Feng 	rtsx_pci_write_register(pcr, PCLK_CTL, PCLK_MODE_SEL, PCLK_MODE_SEL);
687e455b69dSRui Feng 	if (is_version(pcr, 0x524A, IC_VER_A)) {
688e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_DV18_CFG,
689e455b69dSRui Feng 			LDO_DV18_SR_MASK, LDO_DV18_SR_DF);
690e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_VCC_CFG1,
691e455b69dSRui Feng 			LDO_VCC_REF_TUNE_MASK, LDO_VCC_REF_1V2);
692e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_VIO_CFG,
693e455b69dSRui Feng 			LDO_VIO_REF_TUNE_MASK, LDO_VIO_REF_1V2);
694e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_VIO_CFG,
695e455b69dSRui Feng 			LDO_VIO_SR_MASK, LDO_VIO_SR_DF);
696e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_DV12S_CFG,
697e455b69dSRui Feng 			LDO_REF12_TUNE_MASK, LDO_REF12_TUNE_DF);
698e455b69dSRui Feng 		rtsx_pci_write_register(pcr, SD40_LDO_CTL1,
699e455b69dSRui Feng 			SD40_VIO_TUNE_MASK, SD40_VIO_TUNE_1V7);
700e455b69dSRui Feng 	}
701e455b69dSRui Feng 
702e455b69dSRui Feng 	return 0;
703e455b69dSRui Feng }
704e455b69dSRui Feng 
705e455b69dSRui Feng static void rts5250_set_l1off_cfg_sub_d0(struct rtsx_pcr *pcr, int active)
706e455b69dSRui Feng {
707e455b69dSRui Feng 	struct rtsx_cr_option *option = &(pcr->option);
708e455b69dSRui Feng 
709e455b69dSRui Feng 	u32 interrupt = rtsx_pci_readl(pcr, RTSX_BIPR);
710e455b69dSRui Feng 	int card_exist = (interrupt & SD_EXIST) | (interrupt & MS_EXIST);
711e455b69dSRui Feng 	int aspm_L1_1, aspm_L1_2;
712e455b69dSRui Feng 	u8 val = 0;
713e455b69dSRui Feng 
714e455b69dSRui Feng 	aspm_L1_1 = rtsx_check_dev_flag(pcr, ASPM_L1_1_EN);
715e455b69dSRui Feng 	aspm_L1_2 = rtsx_check_dev_flag(pcr, ASPM_L1_2_EN);
716e455b69dSRui Feng 
717e455b69dSRui Feng 	if (active) {
718e455b69dSRui Feng 		/* Run, latency: 60us */
719e455b69dSRui Feng 		if (aspm_L1_1)
720e455b69dSRui Feng 			val = option->ltr_l1off_snooze_sspwrgate;
721e455b69dSRui Feng 	} else {
722e455b69dSRui Feng 		/* L1off, latency: 300us */
723e455b69dSRui Feng 		if (aspm_L1_2)
724e455b69dSRui Feng 			val = option->ltr_l1off_sspwrgate;
725e455b69dSRui Feng 	}
726e455b69dSRui Feng 
727e455b69dSRui Feng 	if (aspm_L1_1 || aspm_L1_2) {
728e455b69dSRui Feng 		if (rtsx_check_dev_flag(pcr,
729e455b69dSRui Feng 					LTR_L1SS_PWR_GATE_CHECK_CARD_EN)) {
730e455b69dSRui Feng 			if (card_exist)
731e455b69dSRui Feng 				val &= ~L1OFF_MBIAS2_EN_5250;
732e455b69dSRui Feng 			else
733e455b69dSRui Feng 				val |= L1OFF_MBIAS2_EN_5250;
734e455b69dSRui Feng 		}
735e455b69dSRui Feng 	}
736e455b69dSRui Feng 	rtsx_set_l1off_sub(pcr, val);
737e455b69dSRui Feng }
738e455b69dSRui Feng 
739e455b69dSRui Feng static const struct pcr_ops rts524a_pcr_ops = {
740e455b69dSRui Feng 	.write_phy = rts524a_write_phy,
741e455b69dSRui Feng 	.read_phy = rts524a_read_phy,
742e455b69dSRui Feng 	.fetch_vendor_settings = rtsx_base_fetch_vendor_settings,
743e455b69dSRui Feng 	.extra_init_hw = rts524a_extra_init_hw,
744e455b69dSRui Feng 	.optimize_phy = rts524a_optimize_phy,
745e455b69dSRui Feng 	.turn_on_led = rtsx_base_turn_on_led,
746e455b69dSRui Feng 	.turn_off_led = rtsx_base_turn_off_led,
747e455b69dSRui Feng 	.enable_auto_blink = rtsx_base_enable_auto_blink,
748e455b69dSRui Feng 	.disable_auto_blink = rtsx_base_disable_auto_blink,
749e455b69dSRui Feng 	.card_power_on = rtsx_base_card_power_on,
750e455b69dSRui Feng 	.card_power_off = rtsx_base_card_power_off,
751e455b69dSRui Feng 	.switch_output_voltage = rtsx_base_switch_output_voltage,
752*71732e24SKai-Heng Feng 	.force_power_down = rts52xa_force_power_down,
753e455b69dSRui Feng 	.set_l1off_cfg_sub_d0 = rts5250_set_l1off_cfg_sub_d0,
754e455b69dSRui Feng };
755e455b69dSRui Feng 
756e455b69dSRui Feng void rts524a_init_params(struct rtsx_pcr *pcr)
757e455b69dSRui Feng {
758e455b69dSRui Feng 	rts5249_init_params(pcr);
7593df4fce7SRicky Wu 	pcr->aspm_mode = ASPM_MODE_REG;
7604686392cSRicky Wu 	pcr->tx_initial_phase = SET_CLOCK_PHASE(27, 29, 11);
761e455b69dSRui Feng 	pcr->option.ltr_l1off_sspwrgate = LTR_L1OFF_SSPWRGATE_5250_DEF;
762e455b69dSRui Feng 	pcr->option.ltr_l1off_snooze_sspwrgate =
763e455b69dSRui Feng 		LTR_L1OFF_SNOOZE_SSPWRGATE_5250_DEF;
764e455b69dSRui Feng 
765e455b69dSRui Feng 	pcr->reg_pm_ctrl3 = RTS524A_PM_CTRL3;
766e455b69dSRui Feng 	pcr->ops = &rts524a_pcr_ops;
767bede03a5SRickyWu 
768bede03a5SRickyWu 	pcr->option.ocp_en = 1;
769bede03a5SRickyWu 	if (pcr->option.ocp_en)
770bede03a5SRickyWu 		pcr->hw_param.interrupt_en |= SD_OC_INT_EN;
771bede03a5SRickyWu 	pcr->hw_param.ocp_glitch = SD_OCP_GLITCH_10M;
772bede03a5SRickyWu 	pcr->option.sd_800mA_ocp_thd = RTS524A_OCP_THD_800;
773bede03a5SRickyWu 
774e455b69dSRui Feng }
775e455b69dSRui Feng 
776e455b69dSRui Feng static int rts525a_card_power_on(struct rtsx_pcr *pcr, int card)
777e455b69dSRui Feng {
778e455b69dSRui Feng 	rtsx_pci_write_register(pcr, LDO_VCC_CFG1,
779e455b69dSRui Feng 		LDO_VCC_TUNE_MASK, LDO_VCC_3V3);
780e455b69dSRui Feng 	return rtsx_base_card_power_on(pcr, card);
781e455b69dSRui Feng }
782e455b69dSRui Feng 
783e455b69dSRui Feng static int rts525a_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
784e455b69dSRui Feng {
785e455b69dSRui Feng 	switch (voltage) {
786e455b69dSRui Feng 	case OUTPUT_3V3:
787e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_CONFIG2,
788e455b69dSRui Feng 			LDO_D3318_MASK, LDO_D3318_33V);
789e455b69dSRui Feng 		rtsx_pci_write_register(pcr, SD_PAD_CTL, SD_IO_USING_1V8, 0);
790e455b69dSRui Feng 		break;
791e455b69dSRui Feng 	case OUTPUT_1V8:
792e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_CONFIG2,
793e455b69dSRui Feng 			LDO_D3318_MASK, LDO_D3318_18V);
794e455b69dSRui Feng 		rtsx_pci_write_register(pcr, SD_PAD_CTL, SD_IO_USING_1V8,
795e455b69dSRui Feng 			SD_IO_USING_1V8);
796e455b69dSRui Feng 		break;
797e455b69dSRui Feng 	default:
798e455b69dSRui Feng 		return -EINVAL;
799e455b69dSRui Feng 	}
800e455b69dSRui Feng 
801e455b69dSRui Feng 	rtsx_pci_init_cmd(pcr);
802e455b69dSRui Feng 	rts5249_fill_driving(pcr, voltage);
803e455b69dSRui Feng 	return rtsx_pci_send_cmd(pcr, 100);
804e455b69dSRui Feng }
805e455b69dSRui Feng 
806e455b69dSRui Feng static int rts525a_optimize_phy(struct rtsx_pcr *pcr)
807e455b69dSRui Feng {
808e455b69dSRui Feng 	int err;
809e455b69dSRui Feng 
810e455b69dSRui Feng 	err = rtsx_pci_write_register(pcr, RTS524A_PM_CTRL3,
811e455b69dSRui Feng 		D3_DELINK_MODE_EN, 0x00);
812e455b69dSRui Feng 	if (err < 0)
813e455b69dSRui Feng 		return err;
814e455b69dSRui Feng 
815e455b69dSRui Feng 	rtsx_pci_write_phy_register(pcr, _PHY_FLD0,
816e455b69dSRui Feng 		_PHY_FLD0_CLK_REQ_20C | _PHY_FLD0_RX_IDLE_EN |
817e455b69dSRui Feng 		_PHY_FLD0_BIT_ERR_RSTN | _PHY_FLD0_BER_COUNT |
818e455b69dSRui Feng 		_PHY_FLD0_BER_TIMER | _PHY_FLD0_CHECK_EN);
819e455b69dSRui Feng 
820e455b69dSRui Feng 	rtsx_pci_write_phy_register(pcr, _PHY_ANA03,
821e455b69dSRui Feng 		_PHY_ANA03_TIMER_MAX | _PHY_ANA03_OOBS_DEB_EN |
822e455b69dSRui Feng 		_PHY_CMU_DEBUG_EN);
823e455b69dSRui Feng 
824e455b69dSRui Feng 	if (is_version(pcr, 0x525A, IC_VER_A))
825e455b69dSRui Feng 		rtsx_pci_write_phy_register(pcr, _PHY_REV0,
826e455b69dSRui Feng 			_PHY_REV0_FILTER_OUT | _PHY_REV0_CDR_BYPASS_PFD |
827e455b69dSRui Feng 			_PHY_REV0_CDR_RX_IDLE_BYPASS);
828e455b69dSRui Feng 
829e455b69dSRui Feng 	return 0;
830e455b69dSRui Feng }
831e455b69dSRui Feng 
832e455b69dSRui Feng static int rts525a_extra_init_hw(struct rtsx_pcr *pcr)
833e455b69dSRui Feng {
834e455b69dSRui Feng 	rts5249_extra_init_hw(pcr);
835e455b69dSRui Feng 
8367c33e3c4SRicky Wu 	rtsx_pci_write_register(pcr, RTS5250_CLK_CFG3, RTS525A_CFG_MEM_PD, RTS525A_CFG_MEM_PD);
8377c33e3c4SRicky Wu 
838e455b69dSRui Feng 	rtsx_pci_write_register(pcr, PCLK_CTL, PCLK_MODE_SEL, PCLK_MODE_SEL);
839e455b69dSRui Feng 	if (is_version(pcr, 0x525A, IC_VER_A)) {
840e455b69dSRui Feng 		rtsx_pci_write_register(pcr, L1SUB_CONFIG2,
841e455b69dSRui Feng 			L1SUB_AUTO_CFG, L1SUB_AUTO_CFG);
842e455b69dSRui Feng 		rtsx_pci_write_register(pcr, RREF_CFG,
843e455b69dSRui Feng 			RREF_VBGSEL_MASK, RREF_VBGSEL_1V25);
844e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_VIO_CFG,
845e455b69dSRui Feng 			LDO_VIO_TUNE_MASK, LDO_VIO_1V7);
846e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_DV12S_CFG,
847e455b69dSRui Feng 			LDO_D12_TUNE_MASK, LDO_D12_TUNE_DF);
848e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_AV12S_CFG,
849e455b69dSRui Feng 			LDO_AV12S_TUNE_MASK, LDO_AV12S_TUNE_DF);
850e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_VCC_CFG0,
851e455b69dSRui Feng 			LDO_VCC_LMTVTH_MASK, LDO_VCC_LMTVTH_2A);
852e455b69dSRui Feng 		rtsx_pci_write_register(pcr, OOBS_CONFIG,
853e455b69dSRui Feng 			OOBS_AUTOK_DIS | OOBS_VAL_MASK, 0x89);
854e455b69dSRui Feng 	}
855e455b69dSRui Feng 
856e455b69dSRui Feng 	return 0;
857e455b69dSRui Feng }
858e455b69dSRui Feng 
859e455b69dSRui Feng static const struct pcr_ops rts525a_pcr_ops = {
860e455b69dSRui Feng 	.fetch_vendor_settings = rtsx_base_fetch_vendor_settings,
861e455b69dSRui Feng 	.extra_init_hw = rts525a_extra_init_hw,
862e455b69dSRui Feng 	.optimize_phy = rts525a_optimize_phy,
863e455b69dSRui Feng 	.turn_on_led = rtsx_base_turn_on_led,
864e455b69dSRui Feng 	.turn_off_led = rtsx_base_turn_off_led,
865e455b69dSRui Feng 	.enable_auto_blink = rtsx_base_enable_auto_blink,
866e455b69dSRui Feng 	.disable_auto_blink = rtsx_base_disable_auto_blink,
867e455b69dSRui Feng 	.card_power_on = rts525a_card_power_on,
868e455b69dSRui Feng 	.card_power_off = rtsx_base_card_power_off,
869e455b69dSRui Feng 	.switch_output_voltage = rts525a_switch_output_voltage,
870*71732e24SKai-Heng Feng 	.force_power_down = rts52xa_force_power_down,
871e455b69dSRui Feng 	.set_l1off_cfg_sub_d0 = rts5250_set_l1off_cfg_sub_d0,
872e455b69dSRui Feng };
873e455b69dSRui Feng 
874e455b69dSRui Feng void rts525a_init_params(struct rtsx_pcr *pcr)
875e455b69dSRui Feng {
876e455b69dSRui Feng 	rts5249_init_params(pcr);
8773df4fce7SRicky Wu 	pcr->aspm_mode = ASPM_MODE_REG;
8784686392cSRicky Wu 	pcr->tx_initial_phase = SET_CLOCK_PHASE(25, 29, 11);
879e455b69dSRui Feng 	pcr->option.ltr_l1off_sspwrgate = LTR_L1OFF_SSPWRGATE_5250_DEF;
880e455b69dSRui Feng 	pcr->option.ltr_l1off_snooze_sspwrgate =
881e455b69dSRui Feng 		LTR_L1OFF_SNOOZE_SSPWRGATE_5250_DEF;
882e455b69dSRui Feng 
883e455b69dSRui Feng 	pcr->reg_pm_ctrl3 = RTS524A_PM_CTRL3;
884e455b69dSRui Feng 	pcr->ops = &rts525a_pcr_ops;
885bede03a5SRickyWu 
886bede03a5SRickyWu 	pcr->option.ocp_en = 1;
887bede03a5SRickyWu 	if (pcr->option.ocp_en)
888bede03a5SRickyWu 		pcr->hw_param.interrupt_en |= SD_OC_INT_EN;
889bede03a5SRickyWu 	pcr->hw_param.ocp_glitch = SD_OCP_GLITCH_10M;
890bede03a5SRickyWu 	pcr->option.sd_800mA_ocp_thd = RTS525A_OCP_THD_800;
891e455b69dSRui Feng }
892