xref: /linux/drivers/misc/cardreader/rts5249.c (revision 5b4258f6721f41b092c63f6ee71be76e9616718b)
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);
76*5b4258f6SRicky Wu 
77*5b4258f6SRicky Wu 	pcr->rtd3_en = rtsx_reg_to_rtd3_uhsii(reg);
78*5b4258f6SRicky Wu 
797c33e3c4SRicky Wu 	if (rtsx_check_mmc_support(reg))
807c33e3c4SRicky Wu 		pcr->extra_caps |= EXTRA_CAPS_NO_MMC;
81e455b69dSRui Feng 	pcr->sd30_drive_sel_3v3 = rtsx_reg_to_sd30_drive_sel_3v3(reg);
82e455b69dSRui Feng 	if (rtsx_reg_check_reverse_socket(reg))
83e455b69dSRui Feng 		pcr->flags |= PCR_REVERSE_SOCKET;
84e455b69dSRui Feng }
85e455b69dSRui Feng 
86e455b69dSRui Feng static void rts5249_init_from_cfg(struct rtsx_pcr *pcr)
87e455b69dSRui Feng {
8822bf3251SBjorn Helgaas 	struct pci_dev *pdev = pcr->pci;
89ed86a987SBjorn Helgaas 	int l1ss;
90e455b69dSRui Feng 	struct rtsx_cr_option *option = &(pcr->option);
91e455b69dSRui Feng 	u32 lval;
92e455b69dSRui Feng 
93ed86a987SBjorn Helgaas 	l1ss = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_L1SS);
94ed86a987SBjorn Helgaas 	if (!l1ss)
95ed86a987SBjorn Helgaas 		return;
96ed86a987SBjorn Helgaas 
97ed86a987SBjorn Helgaas 	pci_read_config_dword(pdev, l1ss + PCI_L1SS_CTL1, &lval);
98e455b69dSRui Feng 
997c33e3c4SRicky Wu 	if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A)) {
1007c33e3c4SRicky Wu 		if (0 == (lval & 0x0F))
1017c33e3c4SRicky Wu 			rtsx_pci_enable_oobs_polling(pcr);
1027c33e3c4SRicky Wu 		else
1037c33e3c4SRicky Wu 			rtsx_pci_disable_oobs_polling(pcr);
1047c33e3c4SRicky Wu 	}
1057c33e3c4SRicky Wu 
1067c33e3c4SRicky Wu 
1077a4462a9SBjorn Helgaas 	if (lval & PCI_L1SS_CTL1_ASPM_L1_1)
108e455b69dSRui Feng 		rtsx_set_dev_flag(pcr, ASPM_L1_1_EN);
109e455b69dSRui Feng 
1107a4462a9SBjorn Helgaas 	if (lval & PCI_L1SS_CTL1_ASPM_L1_2)
111e455b69dSRui Feng 		rtsx_set_dev_flag(pcr, ASPM_L1_2_EN);
112e455b69dSRui Feng 
1137a4462a9SBjorn Helgaas 	if (lval & PCI_L1SS_CTL1_PCIPM_L1_1)
114e455b69dSRui Feng 		rtsx_set_dev_flag(pcr, PM_L1_1_EN);
115e455b69dSRui Feng 
1167a4462a9SBjorn Helgaas 	if (lval & PCI_L1SS_CTL1_PCIPM_L1_2)
117e455b69dSRui Feng 		rtsx_set_dev_flag(pcr, PM_L1_2_EN);
118e455b69dSRui Feng 
119e455b69dSRui Feng 	if (option->ltr_en) {
120e455b69dSRui Feng 		u16 val;
121e455b69dSRui Feng 
12222bf3251SBjorn Helgaas 		pcie_capability_read_word(pdev, PCI_EXP_DEVCTL2, &val);
123e455b69dSRui Feng 		if (val & PCI_EXP_DEVCTL2_LTR_EN) {
124e455b69dSRui Feng 			option->ltr_enabled = true;
125e455b69dSRui Feng 			option->ltr_active = true;
126e455b69dSRui Feng 			rtsx_set_ltr_latency(pcr, option->ltr_active_latency);
127e455b69dSRui Feng 		} else {
128e455b69dSRui Feng 			option->ltr_enabled = false;
129e455b69dSRui Feng 		}
130e455b69dSRui Feng 	}
131e455b69dSRui Feng }
132e455b69dSRui Feng 
133e455b69dSRui Feng static int rts5249_init_from_hw(struct rtsx_pcr *pcr)
134e455b69dSRui Feng {
135e455b69dSRui Feng 	struct rtsx_cr_option *option = &(pcr->option);
136e455b69dSRui Feng 
137e455b69dSRui Feng 	if (rtsx_check_dev_flag(pcr, ASPM_L1_1_EN | ASPM_L1_2_EN
138e455b69dSRui Feng 				| PM_L1_1_EN | PM_L1_2_EN))
139e455b69dSRui Feng 		option->force_clkreq_0 = false;
140e455b69dSRui Feng 	else
141e455b69dSRui Feng 		option->force_clkreq_0 = true;
142e455b69dSRui Feng 
143e455b69dSRui Feng 	return 0;
144e455b69dSRui Feng }
145e455b69dSRui Feng 
1467c33e3c4SRicky Wu static void rts52xa_save_content_from_efuse(struct rtsx_pcr *pcr)
1477c33e3c4SRicky Wu {
1487c33e3c4SRicky Wu 	u8 cnt, sv;
1497c33e3c4SRicky Wu 	u16 j = 0;
1507c33e3c4SRicky Wu 	u8 tmp;
1517c33e3c4SRicky Wu 	u8 val;
1527c33e3c4SRicky Wu 	int i;
1537c33e3c4SRicky Wu 
1547c33e3c4SRicky Wu 	rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL,
1557c33e3c4SRicky Wu 				REG_EFUSE_BYPASS | REG_EFUSE_POR, REG_EFUSE_POR);
1567c33e3c4SRicky Wu 	udelay(1);
1577c33e3c4SRicky Wu 
1587c33e3c4SRicky Wu 	pcr_dbg(pcr, "Enable efuse por!");
1597c33e3c4SRicky Wu 	pcr_dbg(pcr, "save efuse to autoload");
1607c33e3c4SRicky Wu 
1617c33e3c4SRicky Wu 	rtsx_pci_write_register(pcr, RTS525A_EFUSE_ADD, REG_EFUSE_ADD_MASK, 0x00);
1627c33e3c4SRicky Wu 	rtsx_pci_write_register(pcr, RTS525A_EFUSE_CTL,
1637c33e3c4SRicky Wu 				REG_EFUSE_ENABLE | REG_EFUSE_MODE, REG_EFUSE_ENABLE);
1647c33e3c4SRicky Wu 	/* Wait transfer end */
1657c33e3c4SRicky Wu 	for (j = 0; j < 1024; j++) {
1667c33e3c4SRicky Wu 		rtsx_pci_read_register(pcr, RTS525A_EFUSE_CTL, &tmp);
1677c33e3c4SRicky Wu 		if ((tmp & 0x80) == 0)
1687c33e3c4SRicky Wu 			break;
1697c33e3c4SRicky Wu 	}
1707c33e3c4SRicky Wu 	rtsx_pci_read_register(pcr, RTS525A_EFUSE_DATA, &val);
1717c33e3c4SRicky Wu 	cnt = val & 0x0F;
1727c33e3c4SRicky Wu 	sv = val & 0x10;
1737c33e3c4SRicky Wu 
1747c33e3c4SRicky Wu 	if (sv) {
1757c33e3c4SRicky Wu 		for (i = 0; i < 4; i++) {
1767c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, RTS525A_EFUSE_ADD,
1777c33e3c4SRicky Wu 				REG_EFUSE_ADD_MASK, 0x04 + i);
1787c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, RTS525A_EFUSE_CTL,
1797c33e3c4SRicky Wu 				REG_EFUSE_ENABLE | REG_EFUSE_MODE, REG_EFUSE_ENABLE);
1807c33e3c4SRicky Wu 			/* Wait transfer end */
1817c33e3c4SRicky Wu 			for (j = 0; j < 1024; j++) {
1827c33e3c4SRicky Wu 				rtsx_pci_read_register(pcr, RTS525A_EFUSE_CTL, &tmp);
1837c33e3c4SRicky Wu 				if ((tmp & 0x80) == 0)
1847c33e3c4SRicky Wu 					break;
1857c33e3c4SRicky Wu 			}
1867c33e3c4SRicky Wu 			rtsx_pci_read_register(pcr, RTS525A_EFUSE_DATA, &val);
1877c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, 0xFF04 + i, 0xFF, val);
1887c33e3c4SRicky Wu 		}
1897c33e3c4SRicky Wu 	} else {
1907c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF04, 0xFF, (u8)PCI_VID(pcr));
1917c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF05, 0xFF, (u8)(PCI_VID(pcr) >> 8));
1927c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF06, 0xFF, (u8)PCI_PID(pcr));
1937c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF07, 0xFF, (u8)(PCI_PID(pcr) >> 8));
1947c33e3c4SRicky Wu 	}
1957c33e3c4SRicky Wu 
1967c33e3c4SRicky Wu 	for (i = 0; i < cnt * 4; i++) {
1977c33e3c4SRicky Wu 		if (sv)
1987c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, RTS525A_EFUSE_ADD,
1997c33e3c4SRicky Wu 				REG_EFUSE_ADD_MASK, 0x08 + i);
2007c33e3c4SRicky Wu 		else
2017c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, RTS525A_EFUSE_ADD,
2027c33e3c4SRicky Wu 				REG_EFUSE_ADD_MASK, 0x04 + i);
2037c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, RTS525A_EFUSE_CTL,
2047c33e3c4SRicky Wu 				REG_EFUSE_ENABLE | REG_EFUSE_MODE, REG_EFUSE_ENABLE);
2057c33e3c4SRicky Wu 		/* Wait transfer end */
2067c33e3c4SRicky Wu 		for (j = 0; j < 1024; j++) {
2077c33e3c4SRicky Wu 			rtsx_pci_read_register(pcr, RTS525A_EFUSE_CTL, &tmp);
2087c33e3c4SRicky Wu 			if ((tmp & 0x80) == 0)
2097c33e3c4SRicky Wu 				break;
2107c33e3c4SRicky Wu 		}
2117c33e3c4SRicky Wu 		rtsx_pci_read_register(pcr, RTS525A_EFUSE_DATA, &val);
2127c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF08 + i, 0xFF, val);
2137c33e3c4SRicky Wu 	}
2147c33e3c4SRicky Wu 	rtsx_pci_write_register(pcr, 0xFF00, 0xFF, (cnt & 0x7F) | 0x80);
2157c33e3c4SRicky Wu 	rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL,
2167c33e3c4SRicky Wu 		REG_EFUSE_BYPASS | REG_EFUSE_POR, REG_EFUSE_BYPASS);
2177c33e3c4SRicky Wu 	pcr_dbg(pcr, "Disable efuse por!");
2187c33e3c4SRicky Wu }
2197c33e3c4SRicky Wu 
2207c33e3c4SRicky Wu static void rts52xa_save_content_to_autoload_space(struct rtsx_pcr *pcr)
2217c33e3c4SRicky Wu {
2227c33e3c4SRicky Wu 	u8 val;
2237c33e3c4SRicky Wu 
2247c33e3c4SRicky Wu 	rtsx_pci_read_register(pcr, RESET_LOAD_REG, &val);
2257c33e3c4SRicky Wu 	if (val & 0x02) {
2267c33e3c4SRicky Wu 		rtsx_pci_read_register(pcr, RTS525A_BIOS_CFG, &val);
2277c33e3c4SRicky Wu 		if (val & RTS525A_LOAD_BIOS_FLAG) {
2287c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, RTS525A_BIOS_CFG,
2297c33e3c4SRicky Wu 				RTS525A_LOAD_BIOS_FLAG, RTS525A_CLEAR_BIOS_FLAG);
2307c33e3c4SRicky Wu 
2317c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL,
2327c33e3c4SRicky Wu 				REG_EFUSE_POWER_MASK, REG_EFUSE_POWERON);
2337c33e3c4SRicky Wu 
2347c33e3c4SRicky Wu 			pcr_dbg(pcr, "Power ON efuse!");
2357c33e3c4SRicky Wu 			mdelay(1);
2367c33e3c4SRicky Wu 			rts52xa_save_content_from_efuse(pcr);
2377c33e3c4SRicky Wu 		} else {
2387c33e3c4SRicky Wu 			rtsx_pci_read_register(pcr, RTS524A_PME_FORCE_CTL, &val);
2397c33e3c4SRicky Wu 			if (!(val & 0x08))
2407c33e3c4SRicky Wu 				rts52xa_save_content_from_efuse(pcr);
2417c33e3c4SRicky Wu 		}
2427c33e3c4SRicky Wu 	} else {
2437c33e3c4SRicky Wu 		pcr_dbg(pcr, "Load from autoload");
2447c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF00, 0xFF, 0x80);
2457c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF04, 0xFF, (u8)PCI_VID(pcr));
2467c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF05, 0xFF, (u8)(PCI_VID(pcr) >> 8));
2477c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF06, 0xFF, (u8)PCI_PID(pcr));
2487c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, 0xFF07, 0xFF, (u8)(PCI_PID(pcr) >> 8));
2497c33e3c4SRicky Wu 	}
2507c33e3c4SRicky Wu }
2517c33e3c4SRicky Wu 
252e455b69dSRui Feng static int rts5249_extra_init_hw(struct rtsx_pcr *pcr)
253e455b69dSRui Feng {
254e455b69dSRui Feng 	struct rtsx_cr_option *option = &(pcr->option);
255e455b69dSRui Feng 
256e455b69dSRui Feng 	rts5249_init_from_cfg(pcr);
257e455b69dSRui Feng 	rts5249_init_from_hw(pcr);
258e455b69dSRui Feng 
259e455b69dSRui Feng 	rtsx_pci_init_cmd(pcr);
260e455b69dSRui Feng 
2617c33e3c4SRicky Wu 	if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A))
2627c33e3c4SRicky Wu 		rts52xa_save_content_to_autoload_space(pcr);
2637c33e3c4SRicky Wu 
264e455b69dSRui Feng 	/* Rest L1SUB Config */
265e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, L1SUB_CONFIG3, 0xFF, 0x00);
266e455b69dSRui Feng 	/* Configure GPIO as output */
267e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, GPIO_CTL, 0x02, 0x02);
268e455b69dSRui Feng 	/* Reset ASPM state to default value */
269e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, ASPM_FORCE_CTL, 0x3F, 0);
270e455b69dSRui Feng 	/* Switch LDO3318 source from DV33 to card_3v3 */
271e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x00);
272e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x01);
273e455b69dSRui Feng 	/* LED shine disabled, set initial shine cycle period */
274e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OLT_LED_CTL, 0x0F, 0x02);
275e455b69dSRui Feng 	/* Configure driving */
276e455b69dSRui Feng 	rts5249_fill_driving(pcr, OUTPUT_3V3);
277e455b69dSRui Feng 	if (pcr->flags & PCR_REVERSE_SOCKET)
278e455b69dSRui Feng 		rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0xB0, 0xB0);
279e455b69dSRui Feng 	else
280e455b69dSRui Feng 		rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0xB0, 0x80);
281e455b69dSRui Feng 
2827c33e3c4SRicky Wu 	rtsx_pci_send_cmd(pcr, CMD_TIMEOUT_DEF);
2837c33e3c4SRicky Wu 
284*5b4258f6SRicky Wu 	if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A))
2857c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, REG_VREF, PWD_SUSPND_EN, PWD_SUSPND_EN);
286*5b4258f6SRicky Wu 
287*5b4258f6SRicky Wu 	if (pcr->rtd3_en) {
288*5b4258f6SRicky Wu 		if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A)) {
289*5b4258f6SRicky Wu 			rtsx_pci_write_register(pcr, RTS524A_PM_CTRL3, 0x01, 0x01);
290*5b4258f6SRicky Wu 			rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL, 0x30, 0x30);
291*5b4258f6SRicky Wu 		} else {
292*5b4258f6SRicky Wu 			rtsx_pci_write_register(pcr, PM_CTRL3, 0x01, 0x01);
293*5b4258f6SRicky Wu 			rtsx_pci_write_register(pcr, PME_FORCE_CTL, 0xFF, 0x33);
294*5b4258f6SRicky Wu 		}
295*5b4258f6SRicky Wu 	} else {
296*5b4258f6SRicky Wu 		if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A)) {
2977c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, RTS524A_PM_CTRL3, 0x01, 0x00);
2987c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL, 0x30, 0x20);
2997c33e3c4SRicky Wu 		} else {
3007c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, PME_FORCE_CTL, 0xFF, 0x30);
3017c33e3c4SRicky Wu 			rtsx_pci_write_register(pcr, PM_CTRL3, 0x01, 0x00);
3027c33e3c4SRicky Wu 		}
303*5b4258f6SRicky Wu 	}
304*5b4258f6SRicky Wu 
3057c33e3c4SRicky Wu 
306e455b69dSRui Feng 	/*
307e455b69dSRui Feng 	 * If u_force_clkreq_0 is enabled, CLKREQ# PIN will be forced
308e455b69dSRui Feng 	 * to drive low, and we forcibly request clock.
309e455b69dSRui Feng 	 */
310e455b69dSRui Feng 	if (option->force_clkreq_0)
3117c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, PETXCFG,
312e455b69dSRui Feng 			FORCE_CLKREQ_DELINK_MASK, FORCE_CLKREQ_LOW);
313e455b69dSRui Feng 	else
3147c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, PETXCFG,
315e455b69dSRui Feng 			FORCE_CLKREQ_DELINK_MASK, FORCE_CLKREQ_HIGH);
316e455b69dSRui Feng 
3177c33e3c4SRicky Wu 	rtsx_pci_write_register(pcr, pcr->reg_pm_ctrl3, 0x10, 0x00);
3187c33e3c4SRicky Wu 	if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A)) {
3197c33e3c4SRicky Wu 		rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL,
3207c33e3c4SRicky Wu 				REG_EFUSE_POWER_MASK, REG_EFUSE_POWEROFF);
3217c33e3c4SRicky Wu 		pcr_dbg(pcr, "Power OFF efuse!");
3227c33e3c4SRicky Wu 	}
3237c33e3c4SRicky Wu 
3247c33e3c4SRicky Wu 	return 0;
325e455b69dSRui Feng }
326e455b69dSRui Feng 
327e455b69dSRui Feng static int rts5249_optimize_phy(struct rtsx_pcr *pcr)
328e455b69dSRui Feng {
329e455b69dSRui Feng 	int err;
330e455b69dSRui Feng 
331e455b69dSRui Feng 	err = rtsx_pci_write_register(pcr, PM_CTRL3, D3_DELINK_MODE_EN, 0x00);
332e455b69dSRui Feng 	if (err < 0)
333e455b69dSRui Feng 		return err;
334e455b69dSRui Feng 
335e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_REV,
336e455b69dSRui Feng 			PHY_REV_RESV | PHY_REV_RXIDLE_LATCHED |
337e455b69dSRui Feng 			PHY_REV_P1_EN | PHY_REV_RXIDLE_EN |
338e455b69dSRui Feng 			PHY_REV_CLKREQ_TX_EN | PHY_REV_RX_PWST |
339e455b69dSRui Feng 			PHY_REV_CLKREQ_DT_1_0 | PHY_REV_STOP_CLKRD |
340e455b69dSRui Feng 			PHY_REV_STOP_CLKWR);
341e455b69dSRui Feng 	if (err < 0)
342e455b69dSRui Feng 		return err;
343e455b69dSRui Feng 
344e455b69dSRui Feng 	msleep(1);
345e455b69dSRui Feng 
346e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_BPCR,
347e455b69dSRui Feng 			PHY_BPCR_IBRXSEL | PHY_BPCR_IBTXSEL |
348e455b69dSRui Feng 			PHY_BPCR_IB_FILTER | PHY_BPCR_CMIRROR_EN);
349e455b69dSRui Feng 	if (err < 0)
350e455b69dSRui Feng 		return err;
351e455b69dSRui Feng 
352e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_PCR,
353e455b69dSRui Feng 			PHY_PCR_FORCE_CODE | PHY_PCR_OOBS_CALI_50 |
354e455b69dSRui Feng 			PHY_PCR_OOBS_VCM_08 | PHY_PCR_OOBS_SEN_90 |
355e455b69dSRui Feng 			PHY_PCR_RSSI_EN | PHY_PCR_RX10K);
356e455b69dSRui Feng 	if (err < 0)
357e455b69dSRui Feng 		return err;
358e455b69dSRui Feng 
359e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_RCR2,
360e455b69dSRui Feng 			PHY_RCR2_EMPHASE_EN | PHY_RCR2_NADJR |
361e455b69dSRui Feng 			PHY_RCR2_CDR_SR_2 | PHY_RCR2_FREQSEL_12 |
362e455b69dSRui Feng 			PHY_RCR2_CDR_SC_12P | PHY_RCR2_CALIB_LATE);
363e455b69dSRui Feng 	if (err < 0)
364e455b69dSRui Feng 		return err;
365e455b69dSRui Feng 
366e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_FLD4,
367e455b69dSRui Feng 			PHY_FLD4_FLDEN_SEL | PHY_FLD4_REQ_REF |
368e455b69dSRui Feng 			PHY_FLD4_RXAMP_OFF | PHY_FLD4_REQ_ADDA |
369e455b69dSRui Feng 			PHY_FLD4_BER_COUNT | PHY_FLD4_BER_TIMER |
370e455b69dSRui Feng 			PHY_FLD4_BER_CHK_EN);
371e455b69dSRui Feng 	if (err < 0)
372e455b69dSRui Feng 		return err;
373e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_RDR,
374e455b69dSRui Feng 			PHY_RDR_RXDSEL_1_9 | PHY_SSC_AUTO_PWD);
375e455b69dSRui Feng 	if (err < 0)
376e455b69dSRui Feng 		return err;
377e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_RCR1,
378e455b69dSRui Feng 			PHY_RCR1_ADP_TIME_4 | PHY_RCR1_VCO_COARSE);
379e455b69dSRui Feng 	if (err < 0)
380e455b69dSRui Feng 		return err;
381e455b69dSRui Feng 	err = rtsx_pci_write_phy_register(pcr, PHY_FLD3,
382e455b69dSRui Feng 			PHY_FLD3_TIMER_4 | PHY_FLD3_TIMER_6 |
383e455b69dSRui Feng 			PHY_FLD3_RXDELINK);
384e455b69dSRui Feng 	if (err < 0)
385e455b69dSRui Feng 		return err;
386e455b69dSRui Feng 
387e455b69dSRui Feng 	return rtsx_pci_write_phy_register(pcr, PHY_TUNE,
388e455b69dSRui Feng 			PHY_TUNE_TUNEREF_1_0 | PHY_TUNE_VBGSEL_1252 |
389e455b69dSRui Feng 			PHY_TUNE_SDBUS_33 | PHY_TUNE_TUNED18 |
390e455b69dSRui Feng 			PHY_TUNE_TUNED12 | PHY_TUNE_TUNEA12);
391e455b69dSRui Feng }
392e455b69dSRui Feng 
393e455b69dSRui Feng static int rtsx_base_turn_on_led(struct rtsx_pcr *pcr)
394e455b69dSRui Feng {
395e455b69dSRui Feng 	return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x02);
396e455b69dSRui Feng }
397e455b69dSRui Feng 
398e455b69dSRui Feng static int rtsx_base_turn_off_led(struct rtsx_pcr *pcr)
399e455b69dSRui Feng {
400e455b69dSRui Feng 	return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x00);
401e455b69dSRui Feng }
402e455b69dSRui Feng 
403e455b69dSRui Feng static int rtsx_base_enable_auto_blink(struct rtsx_pcr *pcr)
404e455b69dSRui Feng {
405e455b69dSRui Feng 	return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x08);
406e455b69dSRui Feng }
407e455b69dSRui Feng 
408e455b69dSRui Feng static int rtsx_base_disable_auto_blink(struct rtsx_pcr *pcr)
409e455b69dSRui Feng {
410e455b69dSRui Feng 	return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x00);
411e455b69dSRui Feng }
412e455b69dSRui Feng 
413e455b69dSRui Feng static int rtsx_base_card_power_on(struct rtsx_pcr *pcr, int card)
414e455b69dSRui Feng {
415e455b69dSRui Feng 	int err;
416bede03a5SRickyWu 	struct rtsx_cr_option *option = &pcr->option;
417bede03a5SRickyWu 
418bede03a5SRickyWu 	if (option->ocp_en)
419bede03a5SRickyWu 		rtsx_pci_enable_ocp(pcr);
420e455b69dSRui Feng 
421e455b69dSRui Feng 	rtsx_pci_init_cmd(pcr);
422e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
423e455b69dSRui Feng 			SD_POWER_MASK, SD_VCC_PARTIAL_POWER_ON);
424e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
425e455b69dSRui Feng 			LDO3318_PWR_MASK, 0x02);
426e455b69dSRui Feng 	err = rtsx_pci_send_cmd(pcr, 100);
427e455b69dSRui Feng 	if (err < 0)
428e455b69dSRui Feng 		return err;
429e455b69dSRui Feng 
430e455b69dSRui Feng 	msleep(5);
431e455b69dSRui Feng 
432e455b69dSRui Feng 	rtsx_pci_init_cmd(pcr);
433e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
434e455b69dSRui Feng 			SD_POWER_MASK, SD_VCC_POWER_ON);
435e455b69dSRui Feng 	rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
436e455b69dSRui Feng 			LDO3318_PWR_MASK, 0x06);
437e455b69dSRui Feng 	return rtsx_pci_send_cmd(pcr, 100);
438e455b69dSRui Feng }
439e455b69dSRui Feng 
440e455b69dSRui Feng static int rtsx_base_card_power_off(struct rtsx_pcr *pcr, int card)
441e455b69dSRui Feng {
442bede03a5SRickyWu 	struct rtsx_cr_option *option = &pcr->option;
443bede03a5SRickyWu 
444bede03a5SRickyWu 	if (option->ocp_en)
445bede03a5SRickyWu 		rtsx_pci_disable_ocp(pcr);
446bede03a5SRickyWu 
447bede03a5SRickyWu 	rtsx_pci_write_register(pcr, CARD_PWR_CTL, SD_POWER_MASK, SD_POWER_OFF);
448bede03a5SRickyWu 
449bede03a5SRickyWu 	rtsx_pci_write_register(pcr, PWR_GATE_CTRL, LDO3318_PWR_MASK, 0x00);
450bede03a5SRickyWu 	return 0;
451e455b69dSRui Feng }
452e455b69dSRui Feng 
453e455b69dSRui Feng static int rtsx_base_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
454e455b69dSRui Feng {
455e455b69dSRui Feng 	int err;
456e455b69dSRui Feng 	u16 append;
457e455b69dSRui Feng 
458e455b69dSRui Feng 	switch (voltage) {
459e455b69dSRui Feng 	case OUTPUT_3V3:
460e455b69dSRui Feng 		err = rtsx_pci_update_phy(pcr, PHY_TUNE, PHY_TUNE_VOLTAGE_MASK,
461e455b69dSRui Feng 			PHY_TUNE_VOLTAGE_3V3);
462e455b69dSRui Feng 		if (err < 0)
463e455b69dSRui Feng 			return err;
464e455b69dSRui Feng 		break;
465e455b69dSRui Feng 	case OUTPUT_1V8:
466e455b69dSRui Feng 		append = PHY_TUNE_D18_1V8;
467e455b69dSRui Feng 		if (CHK_PCI_PID(pcr, 0x5249)) {
468e455b69dSRui Feng 			err = rtsx_pci_update_phy(pcr, PHY_BACR,
469e455b69dSRui Feng 				PHY_BACR_BASIC_MASK, 0);
470e455b69dSRui Feng 			if (err < 0)
471e455b69dSRui Feng 				return err;
472e455b69dSRui Feng 			append = PHY_TUNE_D18_1V7;
473e455b69dSRui Feng 		}
474e455b69dSRui Feng 
475e455b69dSRui Feng 		err = rtsx_pci_update_phy(pcr, PHY_TUNE, PHY_TUNE_VOLTAGE_MASK,
476e455b69dSRui Feng 			append);
477e455b69dSRui Feng 		if (err < 0)
478e455b69dSRui Feng 			return err;
479e455b69dSRui Feng 		break;
480e455b69dSRui Feng 	default:
481e455b69dSRui Feng 		pcr_dbg(pcr, "unknown output voltage %d\n", voltage);
482e455b69dSRui Feng 		return -EINVAL;
483e455b69dSRui Feng 	}
484e455b69dSRui Feng 
485e455b69dSRui Feng 	/* set pad drive */
486e455b69dSRui Feng 	rtsx_pci_init_cmd(pcr);
487e455b69dSRui Feng 	rts5249_fill_driving(pcr, voltage);
488e455b69dSRui Feng 	return rtsx_pci_send_cmd(pcr, 100);
489e455b69dSRui Feng }
490e455b69dSRui Feng 
491e455b69dSRui Feng static const struct pcr_ops rts5249_pcr_ops = {
492e455b69dSRui Feng 	.fetch_vendor_settings = rtsx_base_fetch_vendor_settings,
493e455b69dSRui Feng 	.extra_init_hw = rts5249_extra_init_hw,
494e455b69dSRui Feng 	.optimize_phy = rts5249_optimize_phy,
495e455b69dSRui Feng 	.turn_on_led = rtsx_base_turn_on_led,
496e455b69dSRui Feng 	.turn_off_led = rtsx_base_turn_off_led,
497e455b69dSRui Feng 	.enable_auto_blink = rtsx_base_enable_auto_blink,
498e455b69dSRui Feng 	.disable_auto_blink = rtsx_base_disable_auto_blink,
499e455b69dSRui Feng 	.card_power_on = rtsx_base_card_power_on,
500e455b69dSRui Feng 	.card_power_off = rtsx_base_card_power_off,
501e455b69dSRui Feng 	.switch_output_voltage = rtsx_base_switch_output_voltage,
502e455b69dSRui Feng };
503e455b69dSRui Feng 
504e455b69dSRui Feng /* SD Pull Control Enable:
505e455b69dSRui Feng  *     SD_DAT[3:0] ==> pull up
506e455b69dSRui Feng  *     SD_CD       ==> pull up
507e455b69dSRui Feng  *     SD_WP       ==> pull up
508e455b69dSRui Feng  *     SD_CMD      ==> pull up
509e455b69dSRui Feng  *     SD_CLK      ==> pull down
510e455b69dSRui Feng  */
511e455b69dSRui Feng static const u32 rts5249_sd_pull_ctl_enable_tbl[] = {
512e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL1, 0x66),
513e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
514e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9),
515e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL4, 0xAA),
516e455b69dSRui Feng 	0,
517e455b69dSRui Feng };
518e455b69dSRui Feng 
519e455b69dSRui Feng /* SD Pull Control Disable:
520e455b69dSRui Feng  *     SD_DAT[3:0] ==> pull down
521e455b69dSRui Feng  *     SD_CD       ==> pull up
522e455b69dSRui Feng  *     SD_WP       ==> pull down
523e455b69dSRui Feng  *     SD_CMD      ==> pull down
524e455b69dSRui Feng  *     SD_CLK      ==> pull down
525e455b69dSRui Feng  */
526e455b69dSRui Feng static const u32 rts5249_sd_pull_ctl_disable_tbl[] = {
527e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL1, 0x66),
528e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
529e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5),
530e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
531e455b69dSRui Feng 	0,
532e455b69dSRui Feng };
533e455b69dSRui Feng 
534e455b69dSRui Feng /* MS Pull Control Enable:
535e455b69dSRui Feng  *     MS CD       ==> pull up
536e455b69dSRui Feng  *     others      ==> pull down
537e455b69dSRui Feng  */
538e455b69dSRui Feng static const u32 rts5249_ms_pull_ctl_enable_tbl[] = {
539e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
540e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
541e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
542e455b69dSRui Feng 	0,
543e455b69dSRui Feng };
544e455b69dSRui Feng 
545e455b69dSRui Feng /* MS Pull Control Disable:
546e455b69dSRui Feng  *     MS CD       ==> pull up
547e455b69dSRui Feng  *     others      ==> pull down
548e455b69dSRui Feng  */
549e455b69dSRui Feng static const u32 rts5249_ms_pull_ctl_disable_tbl[] = {
550e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
551e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
552e455b69dSRui Feng 	RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
553e455b69dSRui Feng 	0,
554e455b69dSRui Feng };
555e455b69dSRui Feng 
556e455b69dSRui Feng void rts5249_init_params(struct rtsx_pcr *pcr)
557e455b69dSRui Feng {
558e455b69dSRui Feng 	struct rtsx_cr_option *option = &(pcr->option);
559e455b69dSRui Feng 
560e455b69dSRui Feng 	pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104;
561e455b69dSRui Feng 	pcr->num_slots = 2;
562e455b69dSRui Feng 	pcr->ops = &rts5249_pcr_ops;
563e455b69dSRui Feng 
564e455b69dSRui Feng 	pcr->flags = 0;
565e455b69dSRui Feng 	pcr->card_drive_sel = RTSX_CARD_DRIVE_DEFAULT;
566e455b69dSRui Feng 	pcr->sd30_drive_sel_1v8 = CFG_DRIVER_TYPE_B;
567e455b69dSRui Feng 	pcr->sd30_drive_sel_3v3 = CFG_DRIVER_TYPE_B;
568e455b69dSRui Feng 	pcr->aspm_en = ASPM_L1_EN;
569e455b69dSRui Feng 	pcr->tx_initial_phase = SET_CLOCK_PHASE(1, 29, 16);
570e455b69dSRui Feng 	pcr->rx_initial_phase = SET_CLOCK_PHASE(24, 6, 5);
571e455b69dSRui Feng 
572e455b69dSRui Feng 	pcr->ic_version = rts5249_get_ic_version(pcr);
573e455b69dSRui Feng 	pcr->sd_pull_ctl_enable_tbl = rts5249_sd_pull_ctl_enable_tbl;
574e455b69dSRui Feng 	pcr->sd_pull_ctl_disable_tbl = rts5249_sd_pull_ctl_disable_tbl;
575e455b69dSRui Feng 	pcr->ms_pull_ctl_enable_tbl = rts5249_ms_pull_ctl_enable_tbl;
576e455b69dSRui Feng 	pcr->ms_pull_ctl_disable_tbl = rts5249_ms_pull_ctl_disable_tbl;
577e455b69dSRui Feng 
578e455b69dSRui Feng 	pcr->reg_pm_ctrl3 = PM_CTRL3;
579e455b69dSRui Feng 
580e455b69dSRui Feng 	option->dev_flags = (LTR_L1SS_PWR_GATE_CHECK_CARD_EN
581e455b69dSRui Feng 				| LTR_L1SS_PWR_GATE_EN);
582e455b69dSRui Feng 	option->ltr_en = true;
583e455b69dSRui Feng 
584e455b69dSRui Feng 	/* Init latency of active, idle, L1OFF to 60us, 300us, 3ms */
585e455b69dSRui Feng 	option->ltr_active_latency = LTR_ACTIVE_LATENCY_DEF;
586e455b69dSRui Feng 	option->ltr_idle_latency = LTR_IDLE_LATENCY_DEF;
587e455b69dSRui Feng 	option->ltr_l1off_latency = LTR_L1OFF_LATENCY_DEF;
588e455b69dSRui Feng 	option->l1_snooze_delay = L1_SNOOZE_DELAY_DEF;
589e455b69dSRui Feng 	option->ltr_l1off_sspwrgate = LTR_L1OFF_SSPWRGATE_5249_DEF;
590e455b69dSRui Feng 	option->ltr_l1off_snooze_sspwrgate =
591e455b69dSRui Feng 		LTR_L1OFF_SNOOZE_SSPWRGATE_5249_DEF;
592e455b69dSRui Feng }
593e455b69dSRui Feng 
594e455b69dSRui Feng static int rts524a_write_phy(struct rtsx_pcr *pcr, u8 addr, u16 val)
595e455b69dSRui Feng {
596e455b69dSRui Feng 	addr = addr & 0x80 ? (addr & 0x7F) | 0x40 : addr;
597e455b69dSRui Feng 
598e455b69dSRui Feng 	return __rtsx_pci_write_phy_register(pcr, addr, val);
599e455b69dSRui Feng }
600e455b69dSRui Feng 
601e455b69dSRui Feng static int rts524a_read_phy(struct rtsx_pcr *pcr, u8 addr, u16 *val)
602e455b69dSRui Feng {
603e455b69dSRui Feng 	addr = addr & 0x80 ? (addr & 0x7F) | 0x40 : addr;
604e455b69dSRui Feng 
605e455b69dSRui Feng 	return __rtsx_pci_read_phy_register(pcr, addr, val);
606e455b69dSRui Feng }
607e455b69dSRui Feng 
608e455b69dSRui Feng static int rts524a_optimize_phy(struct rtsx_pcr *pcr)
609e455b69dSRui Feng {
610e455b69dSRui Feng 	int err;
611e455b69dSRui Feng 
612e455b69dSRui Feng 	err = rtsx_pci_write_register(pcr, RTS524A_PM_CTRL3,
613e455b69dSRui Feng 		D3_DELINK_MODE_EN, 0x00);
614e455b69dSRui Feng 	if (err < 0)
615e455b69dSRui Feng 		return err;
616e455b69dSRui Feng 
617e455b69dSRui Feng 	rtsx_pci_write_phy_register(pcr, PHY_PCR,
618e455b69dSRui Feng 		PHY_PCR_FORCE_CODE | PHY_PCR_OOBS_CALI_50 |
619e455b69dSRui Feng 		PHY_PCR_OOBS_VCM_08 | PHY_PCR_OOBS_SEN_90 | PHY_PCR_RSSI_EN);
620e455b69dSRui Feng 	rtsx_pci_write_phy_register(pcr, PHY_SSCCR3,
621e455b69dSRui Feng 		PHY_SSCCR3_STEP_IN | PHY_SSCCR3_CHECK_DELAY);
622e455b69dSRui Feng 
623e455b69dSRui Feng 	if (is_version(pcr, 0x524A, IC_VER_A)) {
624e455b69dSRui Feng 		rtsx_pci_write_phy_register(pcr, PHY_SSCCR3,
625e455b69dSRui Feng 			PHY_SSCCR3_STEP_IN | PHY_SSCCR3_CHECK_DELAY);
626e455b69dSRui Feng 		rtsx_pci_write_phy_register(pcr, PHY_SSCCR2,
627e455b69dSRui Feng 			PHY_SSCCR2_PLL_NCODE | PHY_SSCCR2_TIME0 |
628e455b69dSRui Feng 			PHY_SSCCR2_TIME2_WIDTH);
629e455b69dSRui Feng 		rtsx_pci_write_phy_register(pcr, PHY_ANA1A,
630e455b69dSRui Feng 			PHY_ANA1A_TXR_LOOPBACK | PHY_ANA1A_RXT_BIST |
631e455b69dSRui Feng 			PHY_ANA1A_TXR_BIST | PHY_ANA1A_REV);
632e455b69dSRui Feng 		rtsx_pci_write_phy_register(pcr, PHY_ANA1D,
633e455b69dSRui Feng 			PHY_ANA1D_DEBUG_ADDR);
634e455b69dSRui Feng 		rtsx_pci_write_phy_register(pcr, PHY_DIG1E,
635e455b69dSRui Feng 			PHY_DIG1E_REV | PHY_DIG1E_D0_X_D1 |
636e455b69dSRui Feng 			PHY_DIG1E_RX_ON_HOST | PHY_DIG1E_RCLK_REF_HOST |
637e455b69dSRui Feng 			PHY_DIG1E_RCLK_TX_EN_KEEP |
638e455b69dSRui Feng 			PHY_DIG1E_RCLK_TX_TERM_KEEP |
639e455b69dSRui Feng 			PHY_DIG1E_RCLK_RX_EIDLE_ON | PHY_DIG1E_TX_TERM_KEEP |
640e455b69dSRui Feng 			PHY_DIG1E_RX_TERM_KEEP | PHY_DIG1E_TX_EN_KEEP |
641e455b69dSRui Feng 			PHY_DIG1E_RX_EN_KEEP);
642e455b69dSRui Feng 	}
643e455b69dSRui Feng 
644e455b69dSRui Feng 	rtsx_pci_write_phy_register(pcr, PHY_ANA08,
645e455b69dSRui Feng 		PHY_ANA08_RX_EQ_DCGAIN | PHY_ANA08_SEL_RX_EN |
646e455b69dSRui Feng 		PHY_ANA08_RX_EQ_VAL | PHY_ANA08_SCP | PHY_ANA08_SEL_IPI);
647e455b69dSRui Feng 
648e455b69dSRui Feng 	return 0;
649e455b69dSRui Feng }
650e455b69dSRui Feng 
651e455b69dSRui Feng static int rts524a_extra_init_hw(struct rtsx_pcr *pcr)
652e455b69dSRui Feng {
653e455b69dSRui Feng 	rts5249_extra_init_hw(pcr);
654e455b69dSRui Feng 
655e455b69dSRui Feng 	rtsx_pci_write_register(pcr, FUNC_FORCE_CTL,
656e455b69dSRui Feng 		FORCE_ASPM_L1_EN, FORCE_ASPM_L1_EN);
657e455b69dSRui Feng 	rtsx_pci_write_register(pcr, PM_EVENT_DEBUG, PME_DEBUG_0, PME_DEBUG_0);
658e455b69dSRui Feng 	rtsx_pci_write_register(pcr, LDO_VCC_CFG1, LDO_VCC_LMT_EN,
659e455b69dSRui Feng 		LDO_VCC_LMT_EN);
660e455b69dSRui Feng 	rtsx_pci_write_register(pcr, PCLK_CTL, PCLK_MODE_SEL, PCLK_MODE_SEL);
661e455b69dSRui Feng 	if (is_version(pcr, 0x524A, IC_VER_A)) {
662e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_DV18_CFG,
663e455b69dSRui Feng 			LDO_DV18_SR_MASK, LDO_DV18_SR_DF);
664e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_VCC_CFG1,
665e455b69dSRui Feng 			LDO_VCC_REF_TUNE_MASK, LDO_VCC_REF_1V2);
666e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_VIO_CFG,
667e455b69dSRui Feng 			LDO_VIO_REF_TUNE_MASK, LDO_VIO_REF_1V2);
668e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_VIO_CFG,
669e455b69dSRui Feng 			LDO_VIO_SR_MASK, LDO_VIO_SR_DF);
670e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_DV12S_CFG,
671e455b69dSRui Feng 			LDO_REF12_TUNE_MASK, LDO_REF12_TUNE_DF);
672e455b69dSRui Feng 		rtsx_pci_write_register(pcr, SD40_LDO_CTL1,
673e455b69dSRui Feng 			SD40_VIO_TUNE_MASK, SD40_VIO_TUNE_1V7);
674e455b69dSRui Feng 	}
675e455b69dSRui Feng 
676e455b69dSRui Feng 	return 0;
677e455b69dSRui Feng }
678e455b69dSRui Feng 
679e455b69dSRui Feng static void rts5250_set_l1off_cfg_sub_d0(struct rtsx_pcr *pcr, int active)
680e455b69dSRui Feng {
681e455b69dSRui Feng 	struct rtsx_cr_option *option = &(pcr->option);
682e455b69dSRui Feng 
683e455b69dSRui Feng 	u32 interrupt = rtsx_pci_readl(pcr, RTSX_BIPR);
684e455b69dSRui Feng 	int card_exist = (interrupt & SD_EXIST) | (interrupt & MS_EXIST);
685e455b69dSRui Feng 	int aspm_L1_1, aspm_L1_2;
686e455b69dSRui Feng 	u8 val = 0;
687e455b69dSRui Feng 
688e455b69dSRui Feng 	aspm_L1_1 = rtsx_check_dev_flag(pcr, ASPM_L1_1_EN);
689e455b69dSRui Feng 	aspm_L1_2 = rtsx_check_dev_flag(pcr, ASPM_L1_2_EN);
690e455b69dSRui Feng 
691e455b69dSRui Feng 	if (active) {
692e455b69dSRui Feng 		/* Run, latency: 60us */
693e455b69dSRui Feng 		if (aspm_L1_1)
694e455b69dSRui Feng 			val = option->ltr_l1off_snooze_sspwrgate;
695e455b69dSRui Feng 	} else {
696e455b69dSRui Feng 		/* L1off, latency: 300us */
697e455b69dSRui Feng 		if (aspm_L1_2)
698e455b69dSRui Feng 			val = option->ltr_l1off_sspwrgate;
699e455b69dSRui Feng 	}
700e455b69dSRui Feng 
701e455b69dSRui Feng 	if (aspm_L1_1 || aspm_L1_2) {
702e455b69dSRui Feng 		if (rtsx_check_dev_flag(pcr,
703e455b69dSRui Feng 					LTR_L1SS_PWR_GATE_CHECK_CARD_EN)) {
704e455b69dSRui Feng 			if (card_exist)
705e455b69dSRui Feng 				val &= ~L1OFF_MBIAS2_EN_5250;
706e455b69dSRui Feng 			else
707e455b69dSRui Feng 				val |= L1OFF_MBIAS2_EN_5250;
708e455b69dSRui Feng 		}
709e455b69dSRui Feng 	}
710e455b69dSRui Feng 	rtsx_set_l1off_sub(pcr, val);
711e455b69dSRui Feng }
712e455b69dSRui Feng 
713e455b69dSRui Feng static const struct pcr_ops rts524a_pcr_ops = {
714e455b69dSRui Feng 	.write_phy = rts524a_write_phy,
715e455b69dSRui Feng 	.read_phy = rts524a_read_phy,
716e455b69dSRui Feng 	.fetch_vendor_settings = rtsx_base_fetch_vendor_settings,
717e455b69dSRui Feng 	.extra_init_hw = rts524a_extra_init_hw,
718e455b69dSRui Feng 	.optimize_phy = rts524a_optimize_phy,
719e455b69dSRui Feng 	.turn_on_led = rtsx_base_turn_on_led,
720e455b69dSRui Feng 	.turn_off_led = rtsx_base_turn_off_led,
721e455b69dSRui Feng 	.enable_auto_blink = rtsx_base_enable_auto_blink,
722e455b69dSRui Feng 	.disable_auto_blink = rtsx_base_disable_auto_blink,
723e455b69dSRui Feng 	.card_power_on = rtsx_base_card_power_on,
724e455b69dSRui Feng 	.card_power_off = rtsx_base_card_power_off,
725e455b69dSRui Feng 	.switch_output_voltage = rtsx_base_switch_output_voltage,
726e455b69dSRui Feng 	.set_l1off_cfg_sub_d0 = rts5250_set_l1off_cfg_sub_d0,
727e455b69dSRui Feng };
728e455b69dSRui Feng 
729e455b69dSRui Feng void rts524a_init_params(struct rtsx_pcr *pcr)
730e455b69dSRui Feng {
731e455b69dSRui Feng 	rts5249_init_params(pcr);
7324686392cSRicky Wu 	pcr->tx_initial_phase = SET_CLOCK_PHASE(27, 29, 11);
733e455b69dSRui Feng 	pcr->option.ltr_l1off_sspwrgate = LTR_L1OFF_SSPWRGATE_5250_DEF;
734e455b69dSRui Feng 	pcr->option.ltr_l1off_snooze_sspwrgate =
735e455b69dSRui Feng 		LTR_L1OFF_SNOOZE_SSPWRGATE_5250_DEF;
736e455b69dSRui Feng 
737e455b69dSRui Feng 	pcr->reg_pm_ctrl3 = RTS524A_PM_CTRL3;
738e455b69dSRui Feng 	pcr->ops = &rts524a_pcr_ops;
739bede03a5SRickyWu 
740bede03a5SRickyWu 	pcr->option.ocp_en = 1;
741bede03a5SRickyWu 	if (pcr->option.ocp_en)
742bede03a5SRickyWu 		pcr->hw_param.interrupt_en |= SD_OC_INT_EN;
743bede03a5SRickyWu 	pcr->hw_param.ocp_glitch = SD_OCP_GLITCH_10M;
744bede03a5SRickyWu 	pcr->option.sd_800mA_ocp_thd = RTS524A_OCP_THD_800;
745bede03a5SRickyWu 
746e455b69dSRui Feng }
747e455b69dSRui Feng 
748e455b69dSRui Feng static int rts525a_card_power_on(struct rtsx_pcr *pcr, int card)
749e455b69dSRui Feng {
750e455b69dSRui Feng 	rtsx_pci_write_register(pcr, LDO_VCC_CFG1,
751e455b69dSRui Feng 		LDO_VCC_TUNE_MASK, LDO_VCC_3V3);
752e455b69dSRui Feng 	return rtsx_base_card_power_on(pcr, card);
753e455b69dSRui Feng }
754e455b69dSRui Feng 
755e455b69dSRui Feng static int rts525a_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
756e455b69dSRui Feng {
757e455b69dSRui Feng 	switch (voltage) {
758e455b69dSRui Feng 	case OUTPUT_3V3:
759e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_CONFIG2,
760e455b69dSRui Feng 			LDO_D3318_MASK, LDO_D3318_33V);
761e455b69dSRui Feng 		rtsx_pci_write_register(pcr, SD_PAD_CTL, SD_IO_USING_1V8, 0);
762e455b69dSRui Feng 		break;
763e455b69dSRui Feng 	case OUTPUT_1V8:
764e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_CONFIG2,
765e455b69dSRui Feng 			LDO_D3318_MASK, LDO_D3318_18V);
766e455b69dSRui Feng 		rtsx_pci_write_register(pcr, SD_PAD_CTL, SD_IO_USING_1V8,
767e455b69dSRui Feng 			SD_IO_USING_1V8);
768e455b69dSRui Feng 		break;
769e455b69dSRui Feng 	default:
770e455b69dSRui Feng 		return -EINVAL;
771e455b69dSRui Feng 	}
772e455b69dSRui Feng 
773e455b69dSRui Feng 	rtsx_pci_init_cmd(pcr);
774e455b69dSRui Feng 	rts5249_fill_driving(pcr, voltage);
775e455b69dSRui Feng 	return rtsx_pci_send_cmd(pcr, 100);
776e455b69dSRui Feng }
777e455b69dSRui Feng 
778e455b69dSRui Feng static int rts525a_optimize_phy(struct rtsx_pcr *pcr)
779e455b69dSRui Feng {
780e455b69dSRui Feng 	int err;
781e455b69dSRui Feng 
782e455b69dSRui Feng 	err = rtsx_pci_write_register(pcr, RTS524A_PM_CTRL3,
783e455b69dSRui Feng 		D3_DELINK_MODE_EN, 0x00);
784e455b69dSRui Feng 	if (err < 0)
785e455b69dSRui Feng 		return err;
786e455b69dSRui Feng 
787e455b69dSRui Feng 	rtsx_pci_write_phy_register(pcr, _PHY_FLD0,
788e455b69dSRui Feng 		_PHY_FLD0_CLK_REQ_20C | _PHY_FLD0_RX_IDLE_EN |
789e455b69dSRui Feng 		_PHY_FLD0_BIT_ERR_RSTN | _PHY_FLD0_BER_COUNT |
790e455b69dSRui Feng 		_PHY_FLD0_BER_TIMER | _PHY_FLD0_CHECK_EN);
791e455b69dSRui Feng 
792e455b69dSRui Feng 	rtsx_pci_write_phy_register(pcr, _PHY_ANA03,
793e455b69dSRui Feng 		_PHY_ANA03_TIMER_MAX | _PHY_ANA03_OOBS_DEB_EN |
794e455b69dSRui Feng 		_PHY_CMU_DEBUG_EN);
795e455b69dSRui Feng 
796e455b69dSRui Feng 	if (is_version(pcr, 0x525A, IC_VER_A))
797e455b69dSRui Feng 		rtsx_pci_write_phy_register(pcr, _PHY_REV0,
798e455b69dSRui Feng 			_PHY_REV0_FILTER_OUT | _PHY_REV0_CDR_BYPASS_PFD |
799e455b69dSRui Feng 			_PHY_REV0_CDR_RX_IDLE_BYPASS);
800e455b69dSRui Feng 
801e455b69dSRui Feng 	return 0;
802e455b69dSRui Feng }
803e455b69dSRui Feng 
804e455b69dSRui Feng static int rts525a_extra_init_hw(struct rtsx_pcr *pcr)
805e455b69dSRui Feng {
806e455b69dSRui Feng 	rts5249_extra_init_hw(pcr);
807e455b69dSRui Feng 
8087c33e3c4SRicky Wu 	rtsx_pci_write_register(pcr, RTS5250_CLK_CFG3, RTS525A_CFG_MEM_PD, RTS525A_CFG_MEM_PD);
8097c33e3c4SRicky Wu 
810e455b69dSRui Feng 	rtsx_pci_write_register(pcr, PCLK_CTL, PCLK_MODE_SEL, PCLK_MODE_SEL);
811e455b69dSRui Feng 	if (is_version(pcr, 0x525A, IC_VER_A)) {
812e455b69dSRui Feng 		rtsx_pci_write_register(pcr, L1SUB_CONFIG2,
813e455b69dSRui Feng 			L1SUB_AUTO_CFG, L1SUB_AUTO_CFG);
814e455b69dSRui Feng 		rtsx_pci_write_register(pcr, RREF_CFG,
815e455b69dSRui Feng 			RREF_VBGSEL_MASK, RREF_VBGSEL_1V25);
816e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_VIO_CFG,
817e455b69dSRui Feng 			LDO_VIO_TUNE_MASK, LDO_VIO_1V7);
818e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_DV12S_CFG,
819e455b69dSRui Feng 			LDO_D12_TUNE_MASK, LDO_D12_TUNE_DF);
820e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_AV12S_CFG,
821e455b69dSRui Feng 			LDO_AV12S_TUNE_MASK, LDO_AV12S_TUNE_DF);
822e455b69dSRui Feng 		rtsx_pci_write_register(pcr, LDO_VCC_CFG0,
823e455b69dSRui Feng 			LDO_VCC_LMTVTH_MASK, LDO_VCC_LMTVTH_2A);
824e455b69dSRui Feng 		rtsx_pci_write_register(pcr, OOBS_CONFIG,
825e455b69dSRui Feng 			OOBS_AUTOK_DIS | OOBS_VAL_MASK, 0x89);
826e455b69dSRui Feng 	}
827e455b69dSRui Feng 
828e455b69dSRui Feng 	return 0;
829e455b69dSRui Feng }
830e455b69dSRui Feng 
831e455b69dSRui Feng static const struct pcr_ops rts525a_pcr_ops = {
832e455b69dSRui Feng 	.fetch_vendor_settings = rtsx_base_fetch_vendor_settings,
833e455b69dSRui Feng 	.extra_init_hw = rts525a_extra_init_hw,
834e455b69dSRui Feng 	.optimize_phy = rts525a_optimize_phy,
835e455b69dSRui Feng 	.turn_on_led = rtsx_base_turn_on_led,
836e455b69dSRui Feng 	.turn_off_led = rtsx_base_turn_off_led,
837e455b69dSRui Feng 	.enable_auto_blink = rtsx_base_enable_auto_blink,
838e455b69dSRui Feng 	.disable_auto_blink = rtsx_base_disable_auto_blink,
839e455b69dSRui Feng 	.card_power_on = rts525a_card_power_on,
840e455b69dSRui Feng 	.card_power_off = rtsx_base_card_power_off,
841e455b69dSRui Feng 	.switch_output_voltage = rts525a_switch_output_voltage,
842e455b69dSRui Feng 	.set_l1off_cfg_sub_d0 = rts5250_set_l1off_cfg_sub_d0,
843e455b69dSRui Feng };
844e455b69dSRui Feng 
845e455b69dSRui Feng void rts525a_init_params(struct rtsx_pcr *pcr)
846e455b69dSRui Feng {
847e455b69dSRui Feng 	rts5249_init_params(pcr);
8484686392cSRicky Wu 	pcr->tx_initial_phase = SET_CLOCK_PHASE(25, 29, 11);
849e455b69dSRui Feng 	pcr->option.ltr_l1off_sspwrgate = LTR_L1OFF_SSPWRGATE_5250_DEF;
850e455b69dSRui Feng 	pcr->option.ltr_l1off_snooze_sspwrgate =
851e455b69dSRui Feng 		LTR_L1OFF_SNOOZE_SSPWRGATE_5250_DEF;
852e455b69dSRui Feng 
853e455b69dSRui Feng 	pcr->reg_pm_ctrl3 = RTS524A_PM_CTRL3;
854e455b69dSRui Feng 	pcr->ops = &rts525a_pcr_ops;
855bede03a5SRickyWu 
856bede03a5SRickyWu 	pcr->option.ocp_en = 1;
857bede03a5SRickyWu 	if (pcr->option.ocp_en)
858bede03a5SRickyWu 		pcr->hw_param.interrupt_en |= SD_OC_INT_EN;
859bede03a5SRickyWu 	pcr->hw_param.ocp_glitch = SD_OCP_GLITCH_10M;
860bede03a5SRickyWu 	pcr->option.sd_800mA_ocp_thd = RTS525A_OCP_THD_800;
861e455b69dSRui Feng }
862