xref: /linux/drivers/misc/cardreader/rts5249.c (revision aaf4989bd4b1963493d7ee14eccff62d79555347)
1*aaf4989bSThomas 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 {
58e455b69dSRui Feng 	u32 reg;
59e455b69dSRui Feng 
60e455b69dSRui Feng 	rtsx_pci_read_config_dword(pcr, PCR_SETTING_REG1, &reg);
61e455b69dSRui Feng 	pcr_dbg(pcr, "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG1, reg);
62e455b69dSRui Feng 
63e455b69dSRui Feng 	if (!rtsx_vendor_setting_valid(reg)) {
64e455b69dSRui Feng 		pcr_dbg(pcr, "skip fetch vendor setting\n");
65e455b69dSRui Feng 		return;
66e455b69dSRui Feng 	}
67e455b69dSRui Feng 
68e455b69dSRui Feng 	pcr->aspm_en = rtsx_reg_to_aspm(reg);
69e455b69dSRui Feng 	pcr->sd30_drive_sel_1v8 = rtsx_reg_to_sd30_drive_sel_1v8(reg);
70e455b69dSRui Feng 	pcr->card_drive_sel &= 0x3F;
71e455b69dSRui Feng 	pcr->card_drive_sel |= rtsx_reg_to_card_drive_sel(reg);
72e455b69dSRui Feng 
73e455b69dSRui Feng 	rtsx_pci_read_config_dword(pcr, PCR_SETTING_REG2, &reg);
74e455b69dSRui Feng 	pcr_dbg(pcr, "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG2, reg);
75e455b69dSRui Feng 	pcr->sd30_drive_sel_3v3 = rtsx_reg_to_sd30_drive_sel_3v3(reg);
76e455b69dSRui Feng 	if (rtsx_reg_check_reverse_socket(reg))
77e455b69dSRui Feng 		pcr->flags |= PCR_REVERSE_SOCKET;
78e455b69dSRui Feng }
79e455b69dSRui Feng 
80e455b69dSRui Feng static void rtsx_base_force_power_down(struct rtsx_pcr *pcr, u8 pm_state)
81e455b69dSRui Feng {
82e455b69dSRui Feng 	/* Set relink_time to 0 */
83e455b69dSRui Feng 	rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 1, 0xFF, 0);
84e455b69dSRui Feng 	rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 2, 0xFF, 0);
85e455b69dSRui Feng 	rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 3, 0x01, 0);
86e455b69dSRui Feng 
87e455b69dSRui Feng 	if (pm_state == HOST_ENTER_S3)
88e455b69dSRui Feng 		rtsx_pci_write_register(pcr, pcr->reg_pm_ctrl3,
89e455b69dSRui Feng 			D3_DELINK_MODE_EN, D3_DELINK_MODE_EN);
90e455b69dSRui Feng 
91e455b69dSRui Feng 	rtsx_pci_write_register(pcr, FPDCTL, 0x03, 0x03);
92e455b69dSRui Feng }
93e455b69dSRui Feng 
94e455b69dSRui Feng static void rts5249_init_from_cfg(struct rtsx_pcr *pcr)
95e455b69dSRui Feng {
96e455b69dSRui Feng 	struct rtsx_cr_option *option = &(pcr->option);
97e455b69dSRui Feng 	u32 lval;
98e455b69dSRui Feng 
99e455b69dSRui Feng 	if (CHK_PCI_PID(pcr, PID_524A))
100e455b69dSRui Feng 		rtsx_pci_read_config_dword(pcr,
101e455b69dSRui Feng 			PCR_ASPM_SETTING_REG1, &lval);
102e455b69dSRui Feng 	else
103e455b69dSRui Feng 		rtsx_pci_read_config_dword(pcr,
104e455b69dSRui Feng 			PCR_ASPM_SETTING_REG2, &lval);
105e455b69dSRui Feng 
106e455b69dSRui Feng 	if (lval & ASPM_L1_1_EN_MASK)
107e455b69dSRui Feng 		rtsx_set_dev_flag(pcr, ASPM_L1_1_EN);
108e455b69dSRui Feng 
109e455b69dSRui Feng 	if (lval & ASPM_L1_2_EN_MASK)
110e455b69dSRui Feng 		rtsx_set_dev_flag(pcr, ASPM_L1_2_EN);
111e455b69dSRui Feng 
112e455b69dSRui Feng 	if (lval & PM_L1_1_EN_MASK)
113e455b69dSRui Feng 		rtsx_set_dev_flag(pcr, PM_L1_1_EN);
114e455b69dSRui Feng 
115e455b69dSRui Feng 	if (lval & PM_L1_2_EN_MASK)
116e455b69dSRui Feng 		rtsx_set_dev_flag(pcr, PM_L1_2_EN);
117e455b69dSRui Feng 
118e455b69dSRui Feng 	if (option->ltr_en) {
119e455b69dSRui Feng 		u16 val;
120e455b69dSRui Feng 
121e455b69dSRui Feng 		pcie_capability_read_word(pcr->pci, PCI_EXP_DEVCTL2, &val);
122e455b69dSRui Feng 		if (val & PCI_EXP_DEVCTL2_LTR_EN) {
123e455b69dSRui Feng 			option->ltr_enabled = true;
124e455b69dSRui Feng 			option->ltr_active = true;
125e455b69dSRui Feng 			rtsx_set_ltr_latency(pcr, option->ltr_active_latency);
126e455b69dSRui Feng 		} else {
127e455b69dSRui Feng 			option->ltr_enabled = false;
128e455b69dSRui Feng 		}
129e455b69dSRui Feng 	}
130e455b69dSRui Feng }
131e455b69dSRui Feng 
132e455b69dSRui Feng static int rts5249_init_from_hw(struct rtsx_pcr *pcr)
133e455b69dSRui Feng {
134e455b69dSRui Feng 	struct rtsx_cr_option *option = &(pcr->option);
135e455b69dSRui Feng 
136e455b69dSRui Feng 	if (rtsx_check_dev_flag(pcr, ASPM_L1_1_EN | ASPM_L1_2_EN
137e455b69dSRui Feng 				| PM_L1_1_EN | PM_L1_2_EN))
138e455b69dSRui Feng 		option->force_clkreq_0 = false;
139e455b69dSRui Feng 	else
140e455b69dSRui Feng 		option->force_clkreq_0 = true;
141e455b69dSRui Feng 
142e455b69dSRui Feng 	return 0;
143e455b69dSRui Feng }
144e455b69dSRui Feng 
145e455b69dSRui Feng static int rts5249_extra_init_hw(struct rtsx_pcr *pcr)
146e455b69dSRui Feng {
147e455b69dSRui Feng 	struct rtsx_cr_option *option = &(pcr->option);
148e455b69dSRui Feng 
149e455b69dSRui Feng 	rts5249_init_from_cfg(pcr);
150e455b69dSRui Feng 	rts5249_init_from_hw(pcr);
151e455b69dSRui Feng 
152e455b69dSRui Feng 	rtsx_pci_init_cmd(pcr);
153e455b69dSRui Feng 
154e455b69dSRui Feng 	/* Rest L1SUB Config */
155e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, L1SUB_CONFIG3, 0xFF, 0x00);
156e455b69dSRui Feng 	/* Configure GPIO as output */
157e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, GPIO_CTL, 0x02, 0x02);
158e455b69dSRui Feng 	/* Reset ASPM state to default value */
159e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, ASPM_FORCE_CTL, 0x3F, 0);
160e455b69dSRui Feng 	/* Switch LDO3318 source from DV33 to card_3v3 */
161e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x00);
162e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x01);
163e455b69dSRui Feng 	/* LED shine disabled, set initial shine cycle period */
164e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OLT_LED_CTL, 0x0F, 0x02);
165e455b69dSRui Feng 	/* Configure driving */
166e455b69dSRui Feng 	rts5249_fill_driving(pcr, OUTPUT_3V3);
167e455b69dSRui Feng 	if (pcr->flags & PCR_REVERSE_SOCKET)
168e455b69dSRui Feng 		rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0xB0, 0xB0);
169e455b69dSRui Feng 	else
170e455b69dSRui Feng 		rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0xB0, 0x80);
171e455b69dSRui Feng 
172e455b69dSRui Feng 	/*
173e455b69dSRui Feng 	 * If u_force_clkreq_0 is enabled, CLKREQ# PIN will be forced
174e455b69dSRui Feng 	 * to drive low, and we forcibly request clock.
175e455b69dSRui Feng 	 */
176e455b69dSRui Feng 	if (option->force_clkreq_0)
177e455b69dSRui Feng 		rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG,
178e455b69dSRui Feng 			FORCE_CLKREQ_DELINK_MASK, FORCE_CLKREQ_LOW);
179e455b69dSRui Feng 	else
180e455b69dSRui Feng 		rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG,
181e455b69dSRui Feng 			FORCE_CLKREQ_DELINK_MASK, FORCE_CLKREQ_HIGH);
182e455b69dSRui Feng 
183e455b69dSRui Feng 	return rtsx_pci_send_cmd(pcr, CMD_TIMEOUT_DEF);
184e455b69dSRui Feng }
185e455b69dSRui Feng 
186e455b69dSRui Feng static int rts5249_optimize_phy(struct rtsx_pcr *pcr)
187e455b69dSRui Feng {
188e455b69dSRui Feng 	int err;
189e455b69dSRui Feng 
190e455b69dSRui Feng 	err = rtsx_pci_write_register(pcr, PM_CTRL3, D3_DELINK_MODE_EN, 0x00);
191e455b69dSRui Feng 	if (err < 0)
192e455b69dSRui Feng 		return err;
193e455b69dSRui Feng 
194e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_REV,
195e455b69dSRui Feng 			PHY_REV_RESV | PHY_REV_RXIDLE_LATCHED |
196e455b69dSRui Feng 			PHY_REV_P1_EN | PHY_REV_RXIDLE_EN |
197e455b69dSRui Feng 			PHY_REV_CLKREQ_TX_EN | PHY_REV_RX_PWST |
198e455b69dSRui Feng 			PHY_REV_CLKREQ_DT_1_0 | PHY_REV_STOP_CLKRD |
199e455b69dSRui Feng 			PHY_REV_STOP_CLKWR);
200e455b69dSRui Feng 	if (err < 0)
201e455b69dSRui Feng 		return err;
202e455b69dSRui Feng 
203e455b69dSRui Feng 	msleep(1);
204e455b69dSRui Feng 
205e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_BPCR,
206e455b69dSRui Feng 			PHY_BPCR_IBRXSEL | PHY_BPCR_IBTXSEL |
207e455b69dSRui Feng 			PHY_BPCR_IB_FILTER | PHY_BPCR_CMIRROR_EN);
208e455b69dSRui Feng 	if (err < 0)
209e455b69dSRui Feng 		return err;
210e455b69dSRui Feng 
211e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_PCR,
212e455b69dSRui Feng 			PHY_PCR_FORCE_CODE | PHY_PCR_OOBS_CALI_50 |
213e455b69dSRui Feng 			PHY_PCR_OOBS_VCM_08 | PHY_PCR_OOBS_SEN_90 |
214e455b69dSRui Feng 			PHY_PCR_RSSI_EN | PHY_PCR_RX10K);
215e455b69dSRui Feng 	if (err < 0)
216e455b69dSRui Feng 		return err;
217e455b69dSRui Feng 
218e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_RCR2,
219e455b69dSRui Feng 			PHY_RCR2_EMPHASE_EN | PHY_RCR2_NADJR |
220e455b69dSRui Feng 			PHY_RCR2_CDR_SR_2 | PHY_RCR2_FREQSEL_12 |
221e455b69dSRui Feng 			PHY_RCR2_CDR_SC_12P | PHY_RCR2_CALIB_LATE);
222e455b69dSRui Feng 	if (err < 0)
223e455b69dSRui Feng 		return err;
224e455b69dSRui Feng 
225e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_FLD4,
226e455b69dSRui Feng 			PHY_FLD4_FLDEN_SEL | PHY_FLD4_REQ_REF |
227e455b69dSRui Feng 			PHY_FLD4_RXAMP_OFF | PHY_FLD4_REQ_ADDA |
228e455b69dSRui Feng 			PHY_FLD4_BER_COUNT | PHY_FLD4_BER_TIMER |
229e455b69dSRui Feng 			PHY_FLD4_BER_CHK_EN);
230e455b69dSRui Feng 	if (err < 0)
231e455b69dSRui Feng 		return err;
232e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_RDR,
233e455b69dSRui Feng 			PHY_RDR_RXDSEL_1_9 | PHY_SSC_AUTO_PWD);
234e455b69dSRui Feng 	if (err < 0)
235e455b69dSRui Feng 		return err;
236e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_RCR1,
237e455b69dSRui Feng 			PHY_RCR1_ADP_TIME_4 | PHY_RCR1_VCO_COARSE);
238e455b69dSRui Feng 	if (err < 0)
239e455b69dSRui Feng 		return err;
240e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_FLD3,
241e455b69dSRui Feng 			PHY_FLD3_TIMER_4 | PHY_FLD3_TIMER_6 |
242e455b69dSRui Feng 			PHY_FLD3_RXDELINK);
243e455b69dSRui Feng 	if (err < 0)
244e455b69dSRui Feng 		return err;
245e455b69dSRui Feng 
246e455b69dSRui Feng 	return rtsx_pci_write_phy_register(pcr, PHY_TUNE,
247e455b69dSRui Feng 			PHY_TUNE_TUNEREF_1_0 | PHY_TUNE_VBGSEL_1252 |
248e455b69dSRui Feng 			PHY_TUNE_SDBUS_33 | PHY_TUNE_TUNED18 |
249e455b69dSRui Feng 			PHY_TUNE_TUNED12 | PHY_TUNE_TUNEA12);
250e455b69dSRui Feng }
251e455b69dSRui Feng 
252e455b69dSRui Feng static int rtsx_base_turn_on_led(struct rtsx_pcr *pcr)
253e455b69dSRui Feng {
254e455b69dSRui Feng 	return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x02);
255e455b69dSRui Feng }
256e455b69dSRui Feng 
257e455b69dSRui Feng static int rtsx_base_turn_off_led(struct rtsx_pcr *pcr)
258e455b69dSRui Feng {
259e455b69dSRui Feng 	return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x00);
260e455b69dSRui Feng }
261e455b69dSRui Feng 
262e455b69dSRui Feng static int rtsx_base_enable_auto_blink(struct rtsx_pcr *pcr)
263e455b69dSRui Feng {
264e455b69dSRui Feng 	return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x08);
265e455b69dSRui Feng }
266e455b69dSRui Feng 
267e455b69dSRui Feng static int rtsx_base_disable_auto_blink(struct rtsx_pcr *pcr)
268e455b69dSRui Feng {
269e455b69dSRui Feng 	return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x00);
270e455b69dSRui Feng }
271e455b69dSRui Feng 
272e455b69dSRui Feng static int rtsx_base_card_power_on(struct rtsx_pcr *pcr, int card)
273e455b69dSRui Feng {
274e455b69dSRui Feng 	int err;
275bede03a5SRickyWu 	struct rtsx_cr_option *option = &pcr->option;
276bede03a5SRickyWu 
277bede03a5SRickyWu 	if (option->ocp_en)
278bede03a5SRickyWu 		rtsx_pci_enable_ocp(pcr);
279e455b69dSRui Feng 
280e455b69dSRui Feng 	rtsx_pci_init_cmd(pcr);
281e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
282e455b69dSRui Feng 			SD_POWER_MASK, SD_VCC_PARTIAL_POWER_ON);
283e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
284e455b69dSRui Feng 			LDO3318_PWR_MASK, 0x02);
285e455b69dSRui Feng 	err = rtsx_pci_send_cmd(pcr, 100);
286e455b69dSRui Feng 	if (err < 0)
287e455b69dSRui Feng 		return err;
288e455b69dSRui Feng 
289e455b69dSRui Feng 	msleep(5);
290e455b69dSRui Feng 
291e455b69dSRui Feng 	rtsx_pci_init_cmd(pcr);
292e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
293e455b69dSRui Feng 			SD_POWER_MASK, SD_VCC_POWER_ON);
294e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
295e455b69dSRui Feng 			LDO3318_PWR_MASK, 0x06);
296e455b69dSRui Feng 	return rtsx_pci_send_cmd(pcr, 100);
297e455b69dSRui Feng }
298e455b69dSRui Feng 
299e455b69dSRui Feng static int rtsx_base_card_power_off(struct rtsx_pcr *pcr, int card)
300e455b69dSRui Feng {
301bede03a5SRickyWu 	struct rtsx_cr_option *option = &pcr->option;
302bede03a5SRickyWu 
303bede03a5SRickyWu 	if (option->ocp_en)
304bede03a5SRickyWu 		rtsx_pci_disable_ocp(pcr);
305bede03a5SRickyWu 
306bede03a5SRickyWu 	rtsx_pci_write_register(pcr, CARD_PWR_CTL, SD_POWER_MASK, SD_POWER_OFF);
307bede03a5SRickyWu 
308bede03a5SRickyWu 	rtsx_pci_write_register(pcr, PWR_GATE_CTRL, LDO3318_PWR_MASK, 0x00);
309bede03a5SRickyWu 	return 0;
310e455b69dSRui Feng }
311e455b69dSRui Feng 
312e455b69dSRui Feng static int rtsx_base_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
313e455b69dSRui Feng {
314e455b69dSRui Feng 	int err;
315e455b69dSRui Feng 	u16 append;
316e455b69dSRui Feng 
317e455b69dSRui Feng 	switch (voltage) {
318e455b69dSRui Feng 	case OUTPUT_3V3:
319e455b69dSRui Feng 		err = rtsx_pci_update_phy(pcr, PHY_TUNE, PHY_TUNE_VOLTAGE_MASK,
320e455b69dSRui Feng 			PHY_TUNE_VOLTAGE_3V3);
321e455b69dSRui Feng 		if (err < 0)
322e455b69dSRui Feng 			return err;
323e455b69dSRui Feng 		break;
324e455b69dSRui Feng 	case OUTPUT_1V8:
325e455b69dSRui Feng 		append = PHY_TUNE_D18_1V8;
326e455b69dSRui Feng 		if (CHK_PCI_PID(pcr, 0x5249)) {
327e455b69dSRui Feng 			err = rtsx_pci_update_phy(pcr, PHY_BACR,
328e455b69dSRui Feng 				PHY_BACR_BASIC_MASK, 0);
329e455b69dSRui Feng 			if (err < 0)
330e455b69dSRui Feng 				return err;
331e455b69dSRui Feng 			append = PHY_TUNE_D18_1V7;
332e455b69dSRui Feng 		}
333e455b69dSRui Feng 
334e455b69dSRui Feng 		err = rtsx_pci_update_phy(pcr, PHY_TUNE, PHY_TUNE_VOLTAGE_MASK,
335e455b69dSRui Feng 			append);
336e455b69dSRui Feng 		if (err < 0)
337e455b69dSRui Feng 			return err;
338e455b69dSRui Feng 		break;
339e455b69dSRui Feng 	default:
340e455b69dSRui Feng 		pcr_dbg(pcr, "unknown output voltage %d\n", voltage);
341e455b69dSRui Feng 		return -EINVAL;
342e455b69dSRui Feng 	}
343e455b69dSRui Feng 
344e455b69dSRui Feng 	/* set pad drive */
345e455b69dSRui Feng 	rtsx_pci_init_cmd(pcr);
346e455b69dSRui Feng 	rts5249_fill_driving(pcr, voltage);
347e455b69dSRui Feng 	return rtsx_pci_send_cmd(pcr, 100);
348e455b69dSRui Feng }
349e455b69dSRui Feng 
350e455b69dSRui Feng static void rts5249_set_aspm(struct rtsx_pcr *pcr, bool enable)
351e455b69dSRui Feng {
352e455b69dSRui Feng 	struct rtsx_cr_option *option = &pcr->option;
353e455b69dSRui Feng 	u8 val = 0;
354e455b69dSRui Feng 
355e455b69dSRui Feng 	if (pcr->aspm_enabled == enable)
356e455b69dSRui Feng 		return;
357e455b69dSRui Feng 
358e455b69dSRui Feng 	if (option->dev_aspm_mode == DEV_ASPM_DYNAMIC) {
359e455b69dSRui Feng 		if (enable)
360e455b69dSRui Feng 			val = pcr->aspm_en;
361e455b69dSRui Feng 		rtsx_pci_update_cfg_byte(pcr,
362e455b69dSRui Feng 			pcr->pcie_cap + PCI_EXP_LNKCTL,
363e455b69dSRui Feng 			ASPM_MASK_NEG, val);
364e455b69dSRui Feng 	} else if (option->dev_aspm_mode == DEV_ASPM_BACKDOOR) {
365e455b69dSRui Feng 		u8 mask = FORCE_ASPM_VAL_MASK | FORCE_ASPM_CTL0;
366e455b69dSRui Feng 
367e455b69dSRui Feng 		if (!enable)
368e455b69dSRui Feng 			val = FORCE_ASPM_CTL0;
369e455b69dSRui Feng 		rtsx_pci_write_register(pcr, ASPM_FORCE_CTL, mask, val);
370e455b69dSRui Feng 	}
371e455b69dSRui Feng 
372e455b69dSRui Feng 	pcr->aspm_enabled = enable;
373e455b69dSRui Feng }
374e455b69dSRui Feng 
375e455b69dSRui Feng static const struct pcr_ops rts5249_pcr_ops = {
376e455b69dSRui Feng 	.fetch_vendor_settings = rtsx_base_fetch_vendor_settings,
377e455b69dSRui Feng 	.extra_init_hw = rts5249_extra_init_hw,
378e455b69dSRui Feng 	.optimize_phy = rts5249_optimize_phy,
379e455b69dSRui Feng 	.turn_on_led = rtsx_base_turn_on_led,
380e455b69dSRui Feng 	.turn_off_led = rtsx_base_turn_off_led,
381e455b69dSRui Feng 	.enable_auto_blink = rtsx_base_enable_auto_blink,
382e455b69dSRui Feng 	.disable_auto_blink = rtsx_base_disable_auto_blink,
383e455b69dSRui Feng 	.card_power_on = rtsx_base_card_power_on,
384e455b69dSRui Feng 	.card_power_off = rtsx_base_card_power_off,
385e455b69dSRui Feng 	.switch_output_voltage = rtsx_base_switch_output_voltage,
386e455b69dSRui Feng 	.force_power_down = rtsx_base_force_power_down,
387e455b69dSRui Feng 	.set_aspm = rts5249_set_aspm,
388e455b69dSRui Feng };
389e455b69dSRui Feng 
390e455b69dSRui Feng /* SD Pull Control Enable:
391e455b69dSRui Feng  *     SD_DAT[3:0] ==> pull up
392e455b69dSRui Feng  *     SD_CD       ==> pull up
393e455b69dSRui Feng  *     SD_WP       ==> pull up
394e455b69dSRui Feng  *     SD_CMD      ==> pull up
395e455b69dSRui Feng  *     SD_CLK      ==> pull down
396e455b69dSRui Feng  */
397e455b69dSRui Feng static const u32 rts5249_sd_pull_ctl_enable_tbl[] = {
398e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL1, 0x66),
399e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
400e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9),
401e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL4, 0xAA),
402e455b69dSRui Feng 	0,
403e455b69dSRui Feng };
404e455b69dSRui Feng 
405e455b69dSRui Feng /* SD Pull Control Disable:
406e455b69dSRui Feng  *     SD_DAT[3:0] ==> pull down
407e455b69dSRui Feng  *     SD_CD       ==> pull up
408e455b69dSRui Feng  *     SD_WP       ==> pull down
409e455b69dSRui Feng  *     SD_CMD      ==> pull down
410e455b69dSRui Feng  *     SD_CLK      ==> pull down
411e455b69dSRui Feng  */
412e455b69dSRui Feng static const u32 rts5249_sd_pull_ctl_disable_tbl[] = {
413e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL1, 0x66),
414e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
415e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5),
416e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
417e455b69dSRui Feng 	0,
418e455b69dSRui Feng };
419e455b69dSRui Feng 
420e455b69dSRui Feng /* MS Pull Control Enable:
421e455b69dSRui Feng  *     MS CD       ==> pull up
422e455b69dSRui Feng  *     others      ==> pull down
423e455b69dSRui Feng  */
424e455b69dSRui Feng static const u32 rts5249_ms_pull_ctl_enable_tbl[] = {
425e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
426e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
427e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
428e455b69dSRui Feng 	0,
429e455b69dSRui Feng };
430e455b69dSRui Feng 
431e455b69dSRui Feng /* MS Pull Control Disable:
432e455b69dSRui Feng  *     MS CD       ==> pull up
433e455b69dSRui Feng  *     others      ==> pull down
434e455b69dSRui Feng  */
435e455b69dSRui Feng static const u32 rts5249_ms_pull_ctl_disable_tbl[] = {
436e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
437e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
438e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
439e455b69dSRui Feng 	0,
440e455b69dSRui Feng };
441e455b69dSRui Feng 
442e455b69dSRui Feng void rts5249_init_params(struct rtsx_pcr *pcr)
443e455b69dSRui Feng {
444e455b69dSRui Feng 	struct rtsx_cr_option *option = &(pcr->option);
445e455b69dSRui Feng 
446e455b69dSRui Feng 	pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104;
447e455b69dSRui Feng 	pcr->num_slots = 2;
448e455b69dSRui Feng 	pcr->ops = &rts5249_pcr_ops;
449e455b69dSRui Feng 
450e455b69dSRui Feng 	pcr->flags = 0;
451e455b69dSRui Feng 	pcr->card_drive_sel = RTSX_CARD_DRIVE_DEFAULT;
452e455b69dSRui Feng 	pcr->sd30_drive_sel_1v8 = CFG_DRIVER_TYPE_B;
453e455b69dSRui Feng 	pcr->sd30_drive_sel_3v3 = CFG_DRIVER_TYPE_B;
454e455b69dSRui Feng 	pcr->aspm_en = ASPM_L1_EN;
455e455b69dSRui Feng 	pcr->tx_initial_phase = SET_CLOCK_PHASE(1, 29, 16);
456e455b69dSRui Feng 	pcr->rx_initial_phase = SET_CLOCK_PHASE(24, 6, 5);
457e455b69dSRui Feng 
458e455b69dSRui Feng 	pcr->ic_version = rts5249_get_ic_version(pcr);
459e455b69dSRui Feng 	pcr->sd_pull_ctl_enable_tbl = rts5249_sd_pull_ctl_enable_tbl;
460e455b69dSRui Feng 	pcr->sd_pull_ctl_disable_tbl = rts5249_sd_pull_ctl_disable_tbl;
461e455b69dSRui Feng 	pcr->ms_pull_ctl_enable_tbl = rts5249_ms_pull_ctl_enable_tbl;
462e455b69dSRui Feng 	pcr->ms_pull_ctl_disable_tbl = rts5249_ms_pull_ctl_disable_tbl;
463e455b69dSRui Feng 
464e455b69dSRui Feng 	pcr->reg_pm_ctrl3 = PM_CTRL3;
465e455b69dSRui Feng 
466e455b69dSRui Feng 	option->dev_flags = (LTR_L1SS_PWR_GATE_CHECK_CARD_EN
467e455b69dSRui Feng 				| LTR_L1SS_PWR_GATE_EN);
468e455b69dSRui Feng 	option->ltr_en = true;
469e455b69dSRui Feng 
470e455b69dSRui Feng 	/* Init latency of active, idle, L1OFF to 60us, 300us, 3ms */
471e455b69dSRui Feng 	option->ltr_active_latency = LTR_ACTIVE_LATENCY_DEF;
472e455b69dSRui Feng 	option->ltr_idle_latency = LTR_IDLE_LATENCY_DEF;
473e455b69dSRui Feng 	option->ltr_l1off_latency = LTR_L1OFF_LATENCY_DEF;
474e455b69dSRui Feng 	option->dev_aspm_mode = DEV_ASPM_DYNAMIC;
475e455b69dSRui Feng 	option->l1_snooze_delay = L1_SNOOZE_DELAY_DEF;
476e455b69dSRui Feng 	option->ltr_l1off_sspwrgate = LTR_L1OFF_SSPWRGATE_5249_DEF;
477e455b69dSRui Feng 	option->ltr_l1off_snooze_sspwrgate =
478e455b69dSRui Feng 		LTR_L1OFF_SNOOZE_SSPWRGATE_5249_DEF;
479e455b69dSRui Feng }
480e455b69dSRui Feng 
481e455b69dSRui Feng static int rts524a_write_phy(struct rtsx_pcr *pcr, u8 addr, u16 val)
482e455b69dSRui Feng {
483e455b69dSRui Feng 	addr = addr & 0x80 ? (addr & 0x7F) | 0x40 : addr;
484e455b69dSRui Feng 
485e455b69dSRui Feng 	return __rtsx_pci_write_phy_register(pcr, addr, val);
486e455b69dSRui Feng }
487e455b69dSRui Feng 
488e455b69dSRui Feng static int rts524a_read_phy(struct rtsx_pcr *pcr, u8 addr, u16 *val)
489e455b69dSRui Feng {
490e455b69dSRui Feng 	addr = addr & 0x80 ? (addr & 0x7F) | 0x40 : addr;
491e455b69dSRui Feng 
492e455b69dSRui Feng 	return __rtsx_pci_read_phy_register(pcr, addr, val);
493e455b69dSRui Feng }
494e455b69dSRui Feng 
495e455b69dSRui Feng static int rts524a_optimize_phy(struct rtsx_pcr *pcr)
496e455b69dSRui Feng {
497e455b69dSRui Feng 	int err;
498e455b69dSRui Feng 
499e455b69dSRui Feng 	err = rtsx_pci_write_register(pcr, RTS524A_PM_CTRL3,
500e455b69dSRui Feng 		D3_DELINK_MODE_EN, 0x00);
501e455b69dSRui Feng 	if (err < 0)
502e455b69dSRui Feng 		return err;
503e455b69dSRui Feng 
504e455b69dSRui Feng 	rtsx_pci_write_phy_register(pcr, PHY_PCR,
505e455b69dSRui Feng 		PHY_PCR_FORCE_CODE | PHY_PCR_OOBS_CALI_50 |
506e455b69dSRui Feng 		PHY_PCR_OOBS_VCM_08 | PHY_PCR_OOBS_SEN_90 | PHY_PCR_RSSI_EN);
507e455b69dSRui Feng 	rtsx_pci_write_phy_register(pcr, PHY_SSCCR3,
508e455b69dSRui Feng 		PHY_SSCCR3_STEP_IN | PHY_SSCCR3_CHECK_DELAY);
509e455b69dSRui Feng 
510e455b69dSRui Feng 	if (is_version(pcr, 0x524A, IC_VER_A)) {
511e455b69dSRui Feng 		rtsx_pci_write_phy_register(pcr, PHY_SSCCR3,
512e455b69dSRui Feng 			PHY_SSCCR3_STEP_IN | PHY_SSCCR3_CHECK_DELAY);
513e455b69dSRui Feng 		rtsx_pci_write_phy_register(pcr, PHY_SSCCR2,
514e455b69dSRui Feng 			PHY_SSCCR2_PLL_NCODE | PHY_SSCCR2_TIME0 |
515e455b69dSRui Feng 			PHY_SSCCR2_TIME2_WIDTH);
516e455b69dSRui Feng 		rtsx_pci_write_phy_register(pcr, PHY_ANA1A,
517e455b69dSRui Feng 			PHY_ANA1A_TXR_LOOPBACK | PHY_ANA1A_RXT_BIST |
518e455b69dSRui Feng 			PHY_ANA1A_TXR_BIST | PHY_ANA1A_REV);
519e455b69dSRui Feng 		rtsx_pci_write_phy_register(pcr, PHY_ANA1D,
520e455b69dSRui Feng 			PHY_ANA1D_DEBUG_ADDR);
521e455b69dSRui Feng 		rtsx_pci_write_phy_register(pcr, PHY_DIG1E,
522e455b69dSRui Feng 			PHY_DIG1E_REV | PHY_DIG1E_D0_X_D1 |
523e455b69dSRui Feng 			PHY_DIG1E_RX_ON_HOST | PHY_DIG1E_RCLK_REF_HOST |
524e455b69dSRui Feng 			PHY_DIG1E_RCLK_TX_EN_KEEP |
525e455b69dSRui Feng 			PHY_DIG1E_RCLK_TX_TERM_KEEP |
526e455b69dSRui Feng 			PHY_DIG1E_RCLK_RX_EIDLE_ON | PHY_DIG1E_TX_TERM_KEEP |
527e455b69dSRui Feng 			PHY_DIG1E_RX_TERM_KEEP | PHY_DIG1E_TX_EN_KEEP |
528e455b69dSRui Feng 			PHY_DIG1E_RX_EN_KEEP);
529e455b69dSRui Feng 	}
530e455b69dSRui Feng 
531e455b69dSRui Feng 	rtsx_pci_write_phy_register(pcr, PHY_ANA08,
532e455b69dSRui Feng 		PHY_ANA08_RX_EQ_DCGAIN | PHY_ANA08_SEL_RX_EN |
533e455b69dSRui Feng 		PHY_ANA08_RX_EQ_VAL | PHY_ANA08_SCP | PHY_ANA08_SEL_IPI);
534e455b69dSRui Feng 
535e455b69dSRui Feng 	return 0;
536e455b69dSRui Feng }
537e455b69dSRui Feng 
538e455b69dSRui Feng static int rts524a_extra_init_hw(struct rtsx_pcr *pcr)
539e455b69dSRui Feng {
540e455b69dSRui Feng 	rts5249_extra_init_hw(pcr);
541e455b69dSRui Feng 
542e455b69dSRui Feng 	rtsx_pci_write_register(pcr, FUNC_FORCE_CTL,
543e455b69dSRui Feng 		FORCE_ASPM_L1_EN, FORCE_ASPM_L1_EN);
544e455b69dSRui Feng 	rtsx_pci_write_register(pcr, PM_EVENT_DEBUG, PME_DEBUG_0, PME_DEBUG_0);
545e455b69dSRui Feng 	rtsx_pci_write_register(pcr, LDO_VCC_CFG1, LDO_VCC_LMT_EN,
546e455b69dSRui Feng 		LDO_VCC_LMT_EN);
547e455b69dSRui Feng 	rtsx_pci_write_register(pcr, PCLK_CTL, PCLK_MODE_SEL, PCLK_MODE_SEL);
548e455b69dSRui Feng 	if (is_version(pcr, 0x524A, IC_VER_A)) {
549e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_DV18_CFG,
550e455b69dSRui Feng 			LDO_DV18_SR_MASK, LDO_DV18_SR_DF);
551e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_VCC_CFG1,
552e455b69dSRui Feng 			LDO_VCC_REF_TUNE_MASK, LDO_VCC_REF_1V2);
553e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_VIO_CFG,
554e455b69dSRui Feng 			LDO_VIO_REF_TUNE_MASK, LDO_VIO_REF_1V2);
555e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_VIO_CFG,
556e455b69dSRui Feng 			LDO_VIO_SR_MASK, LDO_VIO_SR_DF);
557e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_DV12S_CFG,
558e455b69dSRui Feng 			LDO_REF12_TUNE_MASK, LDO_REF12_TUNE_DF);
559e455b69dSRui Feng 		rtsx_pci_write_register(pcr, SD40_LDO_CTL1,
560e455b69dSRui Feng 			SD40_VIO_TUNE_MASK, SD40_VIO_TUNE_1V7);
561e455b69dSRui Feng 	}
562e455b69dSRui Feng 
563e455b69dSRui Feng 	return 0;
564e455b69dSRui Feng }
565e455b69dSRui Feng 
566e455b69dSRui Feng static void rts5250_set_l1off_cfg_sub_d0(struct rtsx_pcr *pcr, int active)
567e455b69dSRui Feng {
568e455b69dSRui Feng 	struct rtsx_cr_option *option = &(pcr->option);
569e455b69dSRui Feng 
570e455b69dSRui Feng 	u32 interrupt = rtsx_pci_readl(pcr, RTSX_BIPR);
571e455b69dSRui Feng 	int card_exist = (interrupt & SD_EXIST) | (interrupt & MS_EXIST);
572e455b69dSRui Feng 	int aspm_L1_1, aspm_L1_2;
573e455b69dSRui Feng 	u8 val = 0;
574e455b69dSRui Feng 
575e455b69dSRui Feng 	aspm_L1_1 = rtsx_check_dev_flag(pcr, ASPM_L1_1_EN);
576e455b69dSRui Feng 	aspm_L1_2 = rtsx_check_dev_flag(pcr, ASPM_L1_2_EN);
577e455b69dSRui Feng 
578e455b69dSRui Feng 	if (active) {
579e455b69dSRui Feng 		/* Run, latency: 60us */
580e455b69dSRui Feng 		if (aspm_L1_1)
581e455b69dSRui Feng 			val = option->ltr_l1off_snooze_sspwrgate;
582e455b69dSRui Feng 	} else {
583e455b69dSRui Feng 		/* L1off, latency: 300us */
584e455b69dSRui Feng 		if (aspm_L1_2)
585e455b69dSRui Feng 			val = option->ltr_l1off_sspwrgate;
586e455b69dSRui Feng 	}
587e455b69dSRui Feng 
588e455b69dSRui Feng 	if (aspm_L1_1 || aspm_L1_2) {
589e455b69dSRui Feng 		if (rtsx_check_dev_flag(pcr,
590e455b69dSRui Feng 					LTR_L1SS_PWR_GATE_CHECK_CARD_EN)) {
591e455b69dSRui Feng 			if (card_exist)
592e455b69dSRui Feng 				val &= ~L1OFF_MBIAS2_EN_5250;
593e455b69dSRui Feng 			else
594e455b69dSRui Feng 				val |= L1OFF_MBIAS2_EN_5250;
595e455b69dSRui Feng 		}
596e455b69dSRui Feng 	}
597e455b69dSRui Feng 	rtsx_set_l1off_sub(pcr, val);
598e455b69dSRui Feng }
599e455b69dSRui Feng 
600e455b69dSRui Feng static const struct pcr_ops rts524a_pcr_ops = {
601e455b69dSRui Feng 	.write_phy = rts524a_write_phy,
602e455b69dSRui Feng 	.read_phy = rts524a_read_phy,
603e455b69dSRui Feng 	.fetch_vendor_settings = rtsx_base_fetch_vendor_settings,
604e455b69dSRui Feng 	.extra_init_hw = rts524a_extra_init_hw,
605e455b69dSRui Feng 	.optimize_phy = rts524a_optimize_phy,
606e455b69dSRui Feng 	.turn_on_led = rtsx_base_turn_on_led,
607e455b69dSRui Feng 	.turn_off_led = rtsx_base_turn_off_led,
608e455b69dSRui Feng 	.enable_auto_blink = rtsx_base_enable_auto_blink,
609e455b69dSRui Feng 	.disable_auto_blink = rtsx_base_disable_auto_blink,
610e455b69dSRui Feng 	.card_power_on = rtsx_base_card_power_on,
611e455b69dSRui Feng 	.card_power_off = rtsx_base_card_power_off,
612e455b69dSRui Feng 	.switch_output_voltage = rtsx_base_switch_output_voltage,
613e455b69dSRui Feng 	.force_power_down = rtsx_base_force_power_down,
614e455b69dSRui Feng 	.set_l1off_cfg_sub_d0 = rts5250_set_l1off_cfg_sub_d0,
615e455b69dSRui Feng 	.set_aspm = rts5249_set_aspm,
616e455b69dSRui Feng };
617e455b69dSRui Feng 
618e455b69dSRui Feng void rts524a_init_params(struct rtsx_pcr *pcr)
619e455b69dSRui Feng {
620e455b69dSRui Feng 	rts5249_init_params(pcr);
621e455b69dSRui Feng 	pcr->option.ltr_l1off_sspwrgate = LTR_L1OFF_SSPWRGATE_5250_DEF;
622e455b69dSRui Feng 	pcr->option.ltr_l1off_snooze_sspwrgate =
623e455b69dSRui Feng 		LTR_L1OFF_SNOOZE_SSPWRGATE_5250_DEF;
624e455b69dSRui Feng 
625e455b69dSRui Feng 	pcr->reg_pm_ctrl3 = RTS524A_PM_CTRL3;
626e455b69dSRui Feng 	pcr->ops = &rts524a_pcr_ops;
627bede03a5SRickyWu 
628bede03a5SRickyWu 	pcr->option.ocp_en = 1;
629bede03a5SRickyWu 	if (pcr->option.ocp_en)
630bede03a5SRickyWu 		pcr->hw_param.interrupt_en |= SD_OC_INT_EN;
631bede03a5SRickyWu 	pcr->hw_param.ocp_glitch = SD_OCP_GLITCH_10M;
632bede03a5SRickyWu 	pcr->option.sd_800mA_ocp_thd = RTS524A_OCP_THD_800;
633bede03a5SRickyWu 
634e455b69dSRui Feng }
635e455b69dSRui Feng 
636e455b69dSRui Feng static int rts525a_card_power_on(struct rtsx_pcr *pcr, int card)
637e455b69dSRui Feng {
638e455b69dSRui Feng 	rtsx_pci_write_register(pcr, LDO_VCC_CFG1,
639e455b69dSRui Feng 		LDO_VCC_TUNE_MASK, LDO_VCC_3V3);
640e455b69dSRui Feng 	return rtsx_base_card_power_on(pcr, card);
641e455b69dSRui Feng }
642e455b69dSRui Feng 
643e455b69dSRui Feng static int rts525a_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
644e455b69dSRui Feng {
645e455b69dSRui Feng 	switch (voltage) {
646e455b69dSRui Feng 	case OUTPUT_3V3:
647e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_CONFIG2,
648e455b69dSRui Feng 			LDO_D3318_MASK, LDO_D3318_33V);
649e455b69dSRui Feng 		rtsx_pci_write_register(pcr, SD_PAD_CTL, SD_IO_USING_1V8, 0);
650e455b69dSRui Feng 		break;
651e455b69dSRui Feng 	case OUTPUT_1V8:
652e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_CONFIG2,
653e455b69dSRui Feng 			LDO_D3318_MASK, LDO_D3318_18V);
654e455b69dSRui Feng 		rtsx_pci_write_register(pcr, SD_PAD_CTL, SD_IO_USING_1V8,
655e455b69dSRui Feng 			SD_IO_USING_1V8);
656e455b69dSRui Feng 		break;
657e455b69dSRui Feng 	default:
658e455b69dSRui Feng 		return -EINVAL;
659e455b69dSRui Feng 	}
660e455b69dSRui Feng 
661e455b69dSRui Feng 	rtsx_pci_init_cmd(pcr);
662e455b69dSRui Feng 	rts5249_fill_driving(pcr, voltage);
663e455b69dSRui Feng 	return rtsx_pci_send_cmd(pcr, 100);
664e455b69dSRui Feng }
665e455b69dSRui Feng 
666e455b69dSRui Feng static int rts525a_optimize_phy(struct rtsx_pcr *pcr)
667e455b69dSRui Feng {
668e455b69dSRui Feng 	int err;
669e455b69dSRui Feng 
670e455b69dSRui Feng 	err = rtsx_pci_write_register(pcr, RTS524A_PM_CTRL3,
671e455b69dSRui Feng 		D3_DELINK_MODE_EN, 0x00);
672e455b69dSRui Feng 	if (err < 0)
673e455b69dSRui Feng 		return err;
674e455b69dSRui Feng 
675e455b69dSRui Feng 	rtsx_pci_write_phy_register(pcr, _PHY_FLD0,
676e455b69dSRui Feng 		_PHY_FLD0_CLK_REQ_20C | _PHY_FLD0_RX_IDLE_EN |
677e455b69dSRui Feng 		_PHY_FLD0_BIT_ERR_RSTN | _PHY_FLD0_BER_COUNT |
678e455b69dSRui Feng 		_PHY_FLD0_BER_TIMER | _PHY_FLD0_CHECK_EN);
679e455b69dSRui Feng 
680e455b69dSRui Feng 	rtsx_pci_write_phy_register(pcr, _PHY_ANA03,
681e455b69dSRui Feng 		_PHY_ANA03_TIMER_MAX | _PHY_ANA03_OOBS_DEB_EN |
682e455b69dSRui Feng 		_PHY_CMU_DEBUG_EN);
683e455b69dSRui Feng 
684e455b69dSRui Feng 	if (is_version(pcr, 0x525A, IC_VER_A))
685e455b69dSRui Feng 		rtsx_pci_write_phy_register(pcr, _PHY_REV0,
686e455b69dSRui Feng 			_PHY_REV0_FILTER_OUT | _PHY_REV0_CDR_BYPASS_PFD |
687e455b69dSRui Feng 			_PHY_REV0_CDR_RX_IDLE_BYPASS);
688e455b69dSRui Feng 
689e455b69dSRui Feng 	return 0;
690e455b69dSRui Feng }
691e455b69dSRui Feng 
692e455b69dSRui Feng static int rts525a_extra_init_hw(struct rtsx_pcr *pcr)
693e455b69dSRui Feng {
694e455b69dSRui Feng 	rts5249_extra_init_hw(pcr);
695e455b69dSRui Feng 
696e455b69dSRui Feng 	rtsx_pci_write_register(pcr, PCLK_CTL, PCLK_MODE_SEL, PCLK_MODE_SEL);
697e455b69dSRui Feng 	if (is_version(pcr, 0x525A, IC_VER_A)) {
698e455b69dSRui Feng 		rtsx_pci_write_register(pcr, L1SUB_CONFIG2,
699e455b69dSRui Feng 			L1SUB_AUTO_CFG, L1SUB_AUTO_CFG);
700e455b69dSRui Feng 		rtsx_pci_write_register(pcr, RREF_CFG,
701e455b69dSRui Feng 			RREF_VBGSEL_MASK, RREF_VBGSEL_1V25);
702e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_VIO_CFG,
703e455b69dSRui Feng 			LDO_VIO_TUNE_MASK, LDO_VIO_1V7);
704e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_DV12S_CFG,
705e455b69dSRui Feng 			LDO_D12_TUNE_MASK, LDO_D12_TUNE_DF);
706e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_AV12S_CFG,
707e455b69dSRui Feng 			LDO_AV12S_TUNE_MASK, LDO_AV12S_TUNE_DF);
708e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_VCC_CFG0,
709e455b69dSRui Feng 			LDO_VCC_LMTVTH_MASK, LDO_VCC_LMTVTH_2A);
710e455b69dSRui Feng 		rtsx_pci_write_register(pcr, OOBS_CONFIG,
711e455b69dSRui Feng 			OOBS_AUTOK_DIS | OOBS_VAL_MASK, 0x89);
712e455b69dSRui Feng 	}
713e455b69dSRui Feng 
714e455b69dSRui Feng 	return 0;
715e455b69dSRui Feng }
716e455b69dSRui Feng 
717e455b69dSRui Feng static const struct pcr_ops rts525a_pcr_ops = {
718e455b69dSRui Feng 	.fetch_vendor_settings = rtsx_base_fetch_vendor_settings,
719e455b69dSRui Feng 	.extra_init_hw = rts525a_extra_init_hw,
720e455b69dSRui Feng 	.optimize_phy = rts525a_optimize_phy,
721e455b69dSRui Feng 	.turn_on_led = rtsx_base_turn_on_led,
722e455b69dSRui Feng 	.turn_off_led = rtsx_base_turn_off_led,
723e455b69dSRui Feng 	.enable_auto_blink = rtsx_base_enable_auto_blink,
724e455b69dSRui Feng 	.disable_auto_blink = rtsx_base_disable_auto_blink,
725e455b69dSRui Feng 	.card_power_on = rts525a_card_power_on,
726e455b69dSRui Feng 	.card_power_off = rtsx_base_card_power_off,
727e455b69dSRui Feng 	.switch_output_voltage = rts525a_switch_output_voltage,
728e455b69dSRui Feng 	.force_power_down = rtsx_base_force_power_down,
729e455b69dSRui Feng 	.set_l1off_cfg_sub_d0 = rts5250_set_l1off_cfg_sub_d0,
730e455b69dSRui Feng 	.set_aspm = rts5249_set_aspm,
731e455b69dSRui Feng };
732e455b69dSRui Feng 
733e455b69dSRui Feng void rts525a_init_params(struct rtsx_pcr *pcr)
734e455b69dSRui Feng {
735e455b69dSRui Feng 	rts5249_init_params(pcr);
736e455b69dSRui Feng 	pcr->option.ltr_l1off_sspwrgate = LTR_L1OFF_SSPWRGATE_5250_DEF;
737e455b69dSRui Feng 	pcr->option.ltr_l1off_snooze_sspwrgate =
738e455b69dSRui Feng 		LTR_L1OFF_SNOOZE_SSPWRGATE_5250_DEF;
739e455b69dSRui Feng 
740e455b69dSRui Feng 	pcr->reg_pm_ctrl3 = RTS524A_PM_CTRL3;
741e455b69dSRui Feng 	pcr->ops = &rts525a_pcr_ops;
742bede03a5SRickyWu 
743bede03a5SRickyWu 	pcr->option.ocp_en = 1;
744bede03a5SRickyWu 	if (pcr->option.ocp_en)
745bede03a5SRickyWu 		pcr->hw_param.interrupt_en |= SD_OC_INT_EN;
746bede03a5SRickyWu 	pcr->hw_param.ocp_glitch = SD_OCP_GLITCH_10M;
747bede03a5SRickyWu 	pcr->option.sd_800mA_ocp_thd = RTS525A_OCP_THD_800;
748e455b69dSRui Feng }
749