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