1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Driver for Realtek PCI-Express card reader
4 * Copyright(c) 2018-2019 Realtek Semiconductor Corp. All rights reserved.
7 * Rui FENG <rui_feng@realsil.com.cn>
8 * Wei WANG <wei_wang@realsil.com.cn>
11 #include <linux/module.h>
12 #include <linux/delay.h>
13 #include <linux/rtsx_pci.h>
18 static u8
rts5261_get_ic_version(struct rtsx_pcr
*pcr
)
22 rtsx_pci_read_register(pcr
, DUMMY_REG_RESET_0
, &val
);
23 return val
& IC_VERSION_MASK
;
26 static void rts5261_fill_driving(struct rtsx_pcr
*pcr
, u8 voltage
)
28 u8 driving_3v3
[4][3] = {
34 u8 driving_1v8
[4][3] = {
40 u8 (*driving
)[3], drive_sel
;
42 if (voltage
== OUTPUT_3V3
) {
43 driving
= driving_3v3
;
44 drive_sel
= pcr
->sd30_drive_sel_3v3
;
46 driving
= driving_1v8
;
47 drive_sel
= pcr
->sd30_drive_sel_1v8
;
50 rtsx_pci_write_register(pcr
, SD30_CLK_DRIVE_SEL
,
51 0xFF, driving
[drive_sel
][0]);
53 rtsx_pci_write_register(pcr
, SD30_CMD_DRIVE_SEL
,
54 0xFF, driving
[drive_sel
][1]);
56 rtsx_pci_write_register(pcr
, SD30_DAT_DRIVE_SEL
,
57 0xFF, driving
[drive_sel
][2]);
60 static void rtsx5261_fetch_vendor_settings(struct rtsx_pcr
*pcr
)
64 rtsx_pci_read_config_dword(pcr
, PCR_SETTING_REG2
, ®
);
65 pcr_dbg(pcr
, "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG2
, reg
);
67 if (!rts5261_vendor_setting_valid(reg
)) {
68 pcr_dbg(pcr
, "skip fetch vendor setting\n");
72 pcr
->card_drive_sel
&= 0x3F;
73 pcr
->card_drive_sel
|= rts5261_reg_to_card_drive_sel(reg
);
75 if (rts5261_reg_check_reverse_socket(reg
))
76 pcr
->flags
|= PCR_REVERSE_SOCKET
;
79 rtsx_pci_read_config_dword(pcr
, PCR_SETTING_REG1
, ®
);
80 pcr_dbg(pcr
, "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG1
, reg
);
82 pcr
->aspm_en
= rts5261_reg_to_aspm(reg
);
83 pcr
->sd30_drive_sel_1v8
= rts5261_reg_to_sd30_drive_sel_1v8(reg
);
84 pcr
->sd30_drive_sel_3v3
= rts5261_reg_to_sd30_drive_sel_3v3(reg
);
87 static void rts5261_force_power_down(struct rtsx_pcr
*pcr
, u8 pm_state
)
89 /* Set relink_time to 0 */
90 rtsx_pci_write_register(pcr
, AUTOLOAD_CFG_BASE
+ 1, MASK_8_BIT_DEF
, 0);
91 rtsx_pci_write_register(pcr
, AUTOLOAD_CFG_BASE
+ 2, MASK_8_BIT_DEF
, 0);
92 rtsx_pci_write_register(pcr
, AUTOLOAD_CFG_BASE
+ 3,
95 if (pm_state
== HOST_ENTER_S3
)
96 rtsx_pci_write_register(pcr
, pcr
->reg_pm_ctrl3
,
97 D3_DELINK_MODE_EN
, D3_DELINK_MODE_EN
);
99 rtsx_pci_write_register(pcr
, RTS5261_REG_FPDCTL
,
100 SSC_POWER_DOWN
, SSC_POWER_DOWN
);
103 static int rts5261_enable_auto_blink(struct rtsx_pcr
*pcr
)
105 return rtsx_pci_write_register(pcr
, OLT_LED_CTL
,
106 LED_SHINE_MASK
, LED_SHINE_EN
);
109 static int rts5261_disable_auto_blink(struct rtsx_pcr
*pcr
)
111 return rtsx_pci_write_register(pcr
, OLT_LED_CTL
,
112 LED_SHINE_MASK
, LED_SHINE_DISABLE
);
115 static int rts5261_turn_on_led(struct rtsx_pcr
*pcr
)
117 return rtsx_pci_write_register(pcr
, GPIO_CTL
,
121 static int rts5261_turn_off_led(struct rtsx_pcr
*pcr
)
123 return rtsx_pci_write_register(pcr
, GPIO_CTL
,
127 /* SD Pull Control Enable:
128 * SD_DAT[3:0] ==> pull up
132 * SD_CLK ==> pull down
134 static const u32 rts5261_sd_pull_ctl_enable_tbl
[] = {
135 RTSX_REG_PAIR(CARD_PULL_CTL2
, 0xAA),
136 RTSX_REG_PAIR(CARD_PULL_CTL3
, 0xE9),
140 /* SD Pull Control Disable:
141 * SD_DAT[3:0] ==> pull down
143 * SD_WP ==> pull down
144 * SD_CMD ==> pull down
145 * SD_CLK ==> pull down
147 static const u32 rts5261_sd_pull_ctl_disable_tbl
[] = {
148 RTSX_REG_PAIR(CARD_PULL_CTL2
, 0x55),
149 RTSX_REG_PAIR(CARD_PULL_CTL3
, 0xD5),
153 static int rts5261_sd_set_sample_push_timing_sd30(struct rtsx_pcr
*pcr
)
155 rtsx_pci_write_register(pcr
, SD_CFG1
, SD_MODE_SELECT_MASK
156 | SD_ASYNC_FIFO_NOT_RST
, SD_30_MODE
| SD_ASYNC_FIFO_NOT_RST
);
157 rtsx_pci_write_register(pcr
, CLK_CTL
, CLK_LOW_FREQ
, CLK_LOW_FREQ
);
158 rtsx_pci_write_register(pcr
, CARD_CLK_SOURCE
, 0xFF,
159 CRC_VAR_CLK0
| SD30_FIX_CLK
| SAMPLE_VAR_CLK1
);
160 rtsx_pci_write_register(pcr
, CLK_CTL
, CLK_LOW_FREQ
, 0);
165 static int rts5261_card_power_on(struct rtsx_pcr
*pcr
, int card
)
167 struct rtsx_cr_option
*option
= &pcr
->option
;
170 rtsx_pci_enable_ocp(pcr
);
173 rtsx_pci_write_register(pcr
, RTS5261_LDO1_CFG1
,
174 RTS5261_LDO1_TUNE_MASK
, RTS5261_LDO1_33
);
175 rtsx_pci_write_register(pcr
, RTS5261_LDO1233318_POW_CTL
,
176 RTS5261_LDO1_POWERON
, RTS5261_LDO1_POWERON
);
178 rtsx_pci_write_register(pcr
, RTS5261_LDO1233318_POW_CTL
,
179 RTS5261_LDO3318_POWERON
, RTS5261_LDO3318_POWERON
);
183 rtsx_pci_write_register(pcr
, CARD_OE
, SD_OUTPUT_EN
, SD_OUTPUT_EN
);
185 /* Initialize SD_CFG1 register */
186 rtsx_pci_write_register(pcr
, SD_CFG1
, 0xFF,
187 SD_CLK_DIVIDE_128
| SD_20_MODE
| SD_BUS_WIDTH_1BIT
);
189 rtsx_pci_write_register(pcr
, SD_SAMPLE_POINT_CTL
,
190 0xFF, SD20_RX_POS_EDGE
);
191 rtsx_pci_write_register(pcr
, SD_PUSH_POINT_CTL
, 0xFF, 0);
192 rtsx_pci_write_register(pcr
, CARD_STOP
, SD_STOP
| SD_CLR_ERR
,
193 SD_STOP
| SD_CLR_ERR
);
195 /* Reset SD_CFG3 register */
196 rtsx_pci_write_register(pcr
, SD_CFG3
, SD30_CLK_END_EN
, 0);
197 rtsx_pci_write_register(pcr
, REG_SD_STOP_SDCLK_CFG
,
198 SD30_CLK_STOP_CFG_EN
| SD30_CLK_STOP_CFG1
|
199 SD30_CLK_STOP_CFG0
, 0);
201 if (pcr
->extra_caps
& EXTRA_CAPS_SD_SDR50
||
202 pcr
->extra_caps
& EXTRA_CAPS_SD_SDR104
)
203 rts5261_sd_set_sample_push_timing_sd30(pcr
);
208 static int rts5261_switch_output_voltage(struct rtsx_pcr
*pcr
, u8 voltage
)
213 rtsx_pci_write_register(pcr
, RTS5261_CARD_PWR_CTL
,
214 RTS5261_PUPDC
, RTS5261_PUPDC
);
218 rtsx_pci_read_phy_register(pcr
, PHY_TUNE
, &val
);
219 val
|= PHY_TUNE_SDBUS_33
;
220 err
= rtsx_pci_write_phy_register(pcr
, PHY_TUNE
, val
);
224 rtsx_pci_write_register(pcr
, RTS5261_DV3318_CFG
,
225 RTS5261_DV3318_TUNE_MASK
, RTS5261_DV3318_33
);
226 rtsx_pci_write_register(pcr
, SD_PAD_CTL
,
230 rtsx_pci_read_phy_register(pcr
, PHY_TUNE
, &val
);
231 val
&= ~PHY_TUNE_SDBUS_33
;
232 err
= rtsx_pci_write_phy_register(pcr
, PHY_TUNE
, val
);
236 rtsx_pci_write_register(pcr
, RTS5261_DV3318_CFG
,
237 RTS5261_DV3318_TUNE_MASK
, RTS5261_DV3318_18
);
238 rtsx_pci_write_register(pcr
, SD_PAD_CTL
,
239 SD_IO_USING_1V8
, SD_IO_USING_1V8
);
246 rts5261_fill_driving(pcr
, voltage
);
251 static void rts5261_stop_cmd(struct rtsx_pcr
*pcr
)
253 rtsx_pci_writel(pcr
, RTSX_HCBCTLR
, STOP_CMD
);
254 rtsx_pci_writel(pcr
, RTSX_HDBCTLR
, STOP_DMA
);
255 rtsx_pci_write_register(pcr
, RTS5260_DMA_RST_CTL_0
,
256 RTS5260_DMA_RST
| RTS5260_ADMA3_RST
,
257 RTS5260_DMA_RST
| RTS5260_ADMA3_RST
);
258 rtsx_pci_write_register(pcr
, RBCTL
, RB_FLUSH
, RB_FLUSH
);
261 static void rts5261_card_before_power_off(struct rtsx_pcr
*pcr
)
263 rts5261_stop_cmd(pcr
);
264 rts5261_switch_output_voltage(pcr
, OUTPUT_3V3
);
268 static void rts5261_enable_ocp(struct rtsx_pcr
*pcr
)
272 val
= SD_OCP_INT_EN
| SD_DETECT_EN
;
273 rtsx_pci_write_register(pcr
, REG_OCPCTL
, 0xFF, val
);
277 static void rts5261_disable_ocp(struct rtsx_pcr
*pcr
)
281 mask
= SD_OCP_INT_EN
| SD_DETECT_EN
;
282 rtsx_pci_write_register(pcr
, REG_OCPCTL
, mask
, 0);
283 rtsx_pci_write_register(pcr
, RTS5261_LDO1_CFG0
,
284 RTS5261_LDO1_OCP_EN
| RTS5261_LDO1_OCP_LMT_EN
, 0);
288 static int rts5261_card_power_off(struct rtsx_pcr
*pcr
, int card
)
292 rts5261_card_before_power_off(pcr
);
293 err
= rtsx_pci_write_register(pcr
, RTS5261_LDO1233318_POW_CTL
,
294 RTS5261_LDO_POWERON_MASK
, 0);
296 if (pcr
->option
.ocp_en
)
297 rtsx_pci_disable_ocp(pcr
);
302 static void rts5261_init_ocp(struct rtsx_pcr
*pcr
)
304 struct rtsx_cr_option
*option
= &pcr
->option
;
306 if (option
->ocp_en
) {
309 rtsx_pci_write_register(pcr
, RTS5261_LDO1_CFG0
,
310 RTS5261_LDO1_OCP_EN
| RTS5261_LDO1_OCP_LMT_EN
,
311 RTS5261_LDO1_OCP_EN
| RTS5261_LDO1_OCP_LMT_EN
);
313 rtsx_pci_write_register(pcr
, RTS5261_LDO1_CFG0
,
314 RTS5261_LDO1_OCP_THD_MASK
, option
->sd_800mA_ocp_thd
);
316 rtsx_pci_write_register(pcr
, RTS5261_LDO1_CFG0
,
317 RTS5261_LDO1_OCP_LMT_THD_MASK
,
318 RTS5261_LDO1_LMT_THD_2000
);
320 mask
= SD_OCP_GLITCH_MASK
;
321 val
= pcr
->hw_param
.ocp_glitch
;
322 rtsx_pci_write_register(pcr
, REG_OCPGLITCH
, mask
, val
);
324 rts5261_enable_ocp(pcr
);
326 rtsx_pci_write_register(pcr
, RTS5261_LDO1_CFG0
,
327 RTS5261_LDO1_OCP_EN
| RTS5261_LDO1_OCP_LMT_EN
, 0);
331 static void rts5261_clear_ocpstat(struct rtsx_pcr
*pcr
)
336 mask
= SD_OCP_INT_CLR
| SD_OC_CLR
;
337 val
= SD_OCP_INT_CLR
| SD_OC_CLR
;
339 rtsx_pci_write_register(pcr
, REG_OCPCTL
, mask
, val
);
342 rtsx_pci_write_register(pcr
, REG_OCPCTL
, mask
, 0);
346 static void rts5261_process_ocp(struct rtsx_pcr
*pcr
)
348 if (!pcr
->option
.ocp_en
)
351 rtsx_pci_get_ocpstat(pcr
, &pcr
->ocp_stat
);
353 if (pcr
->ocp_stat
& (SD_OC_NOW
| SD_OC_EVER
)) {
354 rts5261_card_power_off(pcr
, RTSX_SD_CARD
);
355 rtsx_pci_write_register(pcr
, CARD_OE
, SD_OUTPUT_EN
, 0);
356 rts5261_clear_ocpstat(pcr
);
362 static int rts5261_init_from_hw(struct rtsx_pcr
*pcr
)
366 u8 valid
, efuse_valid
, tmp
;
368 rtsx_pci_write_register(pcr
, RTS5261_REG_PME_FORCE_CTL
,
369 REG_EFUSE_POR
| REG_EFUSE_POWER_MASK
,
370 REG_EFUSE_POR
| REG_EFUSE_POWERON
);
372 rtsx_pci_write_register(pcr
, RTS5261_EFUSE_ADDR
,
373 RTS5261_EFUSE_ADDR_MASK
, 0x00);
374 rtsx_pci_write_register(pcr
, RTS5261_EFUSE_CTL
,
375 RTS5261_EFUSE_ENABLE
| RTS5261_EFUSE_MODE_MASK
,
376 RTS5261_EFUSE_ENABLE
);
378 /* Wait transfer end */
379 for (i
= 0; i
< MAX_RW_REG_CNT
; i
++) {
380 rtsx_pci_read_register(pcr
, RTS5261_EFUSE_CTL
, &tmp
);
381 if ((tmp
& 0x80) == 0)
384 rtsx_pci_read_register(pcr
, RTS5261_EFUSE_READ_DATA
, &tmp
);
385 efuse_valid
= ((tmp
& 0x0C) >> 2);
386 pcr_dbg(pcr
, "Load efuse valid: 0x%x\n", efuse_valid
);
388 if (efuse_valid
== 0) {
389 retval
= rtsx_pci_read_config_dword(pcr
,
390 PCR_SETTING_REG2
, &lval
);
392 pcr_dbg(pcr
, "read 0x814 DW fail\n");
393 pcr_dbg(pcr
, "DW from 0x814: 0x%x\n", lval
);
395 valid
= (u8
)((lval
>> 16) & 0x03);
396 pcr_dbg(pcr
, "0x816: %d\n", valid
);
398 rtsx_pci_write_register(pcr
, RTS5261_REG_PME_FORCE_CTL
,
400 pcr_dbg(pcr
, "Disable efuse por!\n");
402 rtsx_pci_read_config_dword(pcr
, PCR_SETTING_REG2
, &lval
);
403 lval
= lval
& 0x00FFFFFF;
404 retval
= rtsx_pci_write_config_dword(pcr
, PCR_SETTING_REG2
, lval
);
406 pcr_dbg(pcr
, "write config fail\n");
411 static void rts5261_init_from_cfg(struct rtsx_pcr
*pcr
)
414 struct rtsx_cr_option
*option
= &pcr
->option
;
416 rtsx_pci_read_config_dword(pcr
, PCR_ASPM_SETTING_REG1
, &lval
);
418 if (lval
& ASPM_L1_1_EN_MASK
)
419 rtsx_set_dev_flag(pcr
, ASPM_L1_1_EN
);
421 rtsx_clear_dev_flag(pcr
, ASPM_L1_1_EN
);
423 if (lval
& ASPM_L1_2_EN_MASK
)
424 rtsx_set_dev_flag(pcr
, ASPM_L1_2_EN
);
426 rtsx_clear_dev_flag(pcr
, ASPM_L1_2_EN
);
428 if (lval
& PM_L1_1_EN_MASK
)
429 rtsx_set_dev_flag(pcr
, PM_L1_1_EN
);
431 rtsx_clear_dev_flag(pcr
, PM_L1_1_EN
);
433 if (lval
& PM_L1_2_EN_MASK
)
434 rtsx_set_dev_flag(pcr
, PM_L1_2_EN
);
436 rtsx_clear_dev_flag(pcr
, PM_L1_2_EN
);
438 rtsx_pci_write_register(pcr
, ASPM_FORCE_CTL
, 0xFF, 0);
439 if (option
->ltr_en
) {
442 pcie_capability_read_word(pcr
->pci
, PCI_EXP_DEVCTL2
, &val
);
443 if (val
& PCI_EXP_DEVCTL2_LTR_EN
) {
444 option
->ltr_enabled
= true;
445 option
->ltr_active
= true;
446 rtsx_set_ltr_latency(pcr
, option
->ltr_active_latency
);
448 option
->ltr_enabled
= false;
452 if (rtsx_check_dev_flag(pcr
, ASPM_L1_1_EN
| ASPM_L1_2_EN
453 | PM_L1_1_EN
| PM_L1_2_EN
))
454 option
->force_clkreq_0
= false;
456 option
->force_clkreq_0
= true;
459 static int rts5261_extra_init_hw(struct rtsx_pcr
*pcr
)
461 struct rtsx_cr_option
*option
= &pcr
->option
;
463 rtsx_pci_write_register(pcr
, RTS5261_AUTOLOAD_CFG1
,
464 CD_RESUME_EN_MASK
, CD_RESUME_EN_MASK
);
466 rts5261_init_from_cfg(pcr
);
467 rts5261_init_from_hw(pcr
);
469 /* power off efuse */
470 rtsx_pci_write_register(pcr
, RTS5261_REG_PME_FORCE_CTL
,
471 REG_EFUSE_POWER_MASK
, REG_EFUSE_POWEROFF
);
472 rtsx_pci_write_register(pcr
, L1SUB_CONFIG1
,
473 AUX_CLK_ACTIVE_SEL_MASK
, MAC_CKSW_DONE
);
474 rtsx_pci_write_register(pcr
, L1SUB_CONFIG3
, 0xFF, 0);
476 rtsx_pci_write_register(pcr
, RTS5261_AUTOLOAD_CFG4
,
477 RTS5261_AUX_CLK_16M_EN
, 0);
480 rtsx_pci_write_register(pcr
, RTS5261_AUTOLOAD_CFG4
,
481 RTS5261_FORCE_PRSNT_LOW
, 0);
482 rtsx_pci_write_register(pcr
, FUNC_FORCE_CTL
,
483 FUNC_FORCE_UPME_XMT_DBG
, FUNC_FORCE_UPME_XMT_DBG
);
485 rtsx_pci_write_register(pcr
, PCLK_CTL
,
486 PCLK_MODE_SEL
, PCLK_MODE_SEL
);
488 rtsx_pci_write_register(pcr
, PM_EVENT_DEBUG
, PME_DEBUG_0
, PME_DEBUG_0
);
489 rtsx_pci_write_register(pcr
, PM_CLK_FORCE_CTL
, CLK_PM_EN
, CLK_PM_EN
);
491 /* LED shine disabled, set initial shine cycle period */
492 rtsx_pci_write_register(pcr
, OLT_LED_CTL
, 0x0F, 0x02);
494 /* Configure driving */
495 rts5261_fill_driving(pcr
, OUTPUT_3V3
);
498 * If u_force_clkreq_0 is enabled, CLKREQ# PIN will be forced
499 * to drive low, and we forcibly request clock.
501 if (option
->force_clkreq_0
)
502 rtsx_pci_write_register(pcr
, PETXCFG
,
503 FORCE_CLKREQ_DELINK_MASK
, FORCE_CLKREQ_LOW
);
505 rtsx_pci_write_register(pcr
, PETXCFG
,
506 FORCE_CLKREQ_DELINK_MASK
, FORCE_CLKREQ_HIGH
);
508 rtsx_pci_write_register(pcr
, pcr
->reg_pm_ctrl3
, 0x10, 0x00);
509 rtsx_pci_write_register(pcr
, RTS5261_REG_PME_FORCE_CTL
,
510 FORCE_PM_CONTROL
| FORCE_PM_VALUE
, FORCE_PM_CONTROL
);
512 /* Clear Enter RTD3_cold Information*/
513 rtsx_pci_write_register(pcr
, RTS5261_FW_CTL
,
514 RTS5261_INFORM_RTD3_COLD
, 0);
519 static void rts5261_enable_aspm(struct rtsx_pcr
*pcr
, bool enable
)
521 struct rtsx_cr_option
*option
= &pcr
->option
;
524 if (pcr
->aspm_enabled
== enable
)
527 if (option
->dev_aspm_mode
== DEV_ASPM_DYNAMIC
) {
529 rtsx_pci_update_cfg_byte(pcr
, pcr
->pcie_cap
+ PCI_EXP_LNKCTL
,
531 } else if (option
->dev_aspm_mode
== DEV_ASPM_BACKDOOR
) {
532 u8 mask
= FORCE_ASPM_VAL_MASK
| FORCE_ASPM_CTL0
;
534 val
= FORCE_ASPM_CTL0
;
535 val
|= (pcr
->aspm_en
& 0x02);
536 rtsx_pci_write_register(pcr
, ASPM_FORCE_CTL
, mask
, val
);
538 rtsx_pci_update_cfg_byte(pcr
, pcr
->pcie_cap
+ PCI_EXP_LNKCTL
,
541 pcr
->aspm_enabled
= enable
;
545 static void rts5261_disable_aspm(struct rtsx_pcr
*pcr
, bool enable
)
547 struct rtsx_cr_option
*option
= &pcr
->option
;
550 if (pcr
->aspm_enabled
== enable
)
553 if (option
->dev_aspm_mode
== DEV_ASPM_DYNAMIC
) {
555 rtsx_pci_update_cfg_byte(pcr
, pcr
->pcie_cap
+ PCI_EXP_LNKCTL
,
557 } else if (option
->dev_aspm_mode
== DEV_ASPM_BACKDOOR
) {
558 u8 mask
= FORCE_ASPM_VAL_MASK
| FORCE_ASPM_CTL0
;
561 rtsx_pci_update_cfg_byte(pcr
, pcr
->pcie_cap
+ PCI_EXP_LNKCTL
,
563 val
= FORCE_ASPM_CTL0
;
564 rtsx_pci_write_register(pcr
, ASPM_FORCE_CTL
, mask
, val
);
566 rtsx_pci_write_register(pcr
, SD_CFG1
, SD_ASYNC_FIFO_NOT_RST
, 0);
568 pcr
->aspm_enabled
= enable
;
571 static void rts5261_set_aspm(struct rtsx_pcr
*pcr
, bool enable
)
574 rts5261_enable_aspm(pcr
, true);
576 rts5261_disable_aspm(pcr
, false);
579 static void rts5261_set_l1off_cfg_sub_d0(struct rtsx_pcr
*pcr
, int active
)
581 struct rtsx_cr_option
*option
= &pcr
->option
;
582 int aspm_L1_1
, aspm_L1_2
;
585 aspm_L1_1
= rtsx_check_dev_flag(pcr
, ASPM_L1_1_EN
);
586 aspm_L1_2
= rtsx_check_dev_flag(pcr
, ASPM_L1_2_EN
);
589 /* run, latency: 60us */
591 val
= option
->ltr_l1off_snooze_sspwrgate
;
593 /* l1off, latency: 300us */
595 val
= option
->ltr_l1off_sspwrgate
;
598 rtsx_set_l1off_sub(pcr
, val
);
601 static const struct pcr_ops rts5261_pcr_ops
= {
602 .fetch_vendor_settings
= rtsx5261_fetch_vendor_settings
,
603 .turn_on_led
= rts5261_turn_on_led
,
604 .turn_off_led
= rts5261_turn_off_led
,
605 .extra_init_hw
= rts5261_extra_init_hw
,
606 .enable_auto_blink
= rts5261_enable_auto_blink
,
607 .disable_auto_blink
= rts5261_disable_auto_blink
,
608 .card_power_on
= rts5261_card_power_on
,
609 .card_power_off
= rts5261_card_power_off
,
610 .switch_output_voltage
= rts5261_switch_output_voltage
,
611 .force_power_down
= rts5261_force_power_down
,
612 .stop_cmd
= rts5261_stop_cmd
,
613 .set_aspm
= rts5261_set_aspm
,
614 .set_l1off_cfg_sub_d0
= rts5261_set_l1off_cfg_sub_d0
,
615 .enable_ocp
= rts5261_enable_ocp
,
616 .disable_ocp
= rts5261_disable_ocp
,
617 .init_ocp
= rts5261_init_ocp
,
618 .process_ocp
= rts5261_process_ocp
,
619 .clear_ocpstat
= rts5261_clear_ocpstat
,
622 static inline u8
double_ssc_depth(u8 depth
)
624 return ((depth
> 1) ? (depth
- 1) : depth
);
627 int rts5261_pci_switch_clock(struct rtsx_pcr
*pcr
, unsigned int card_clock
,
628 u8 ssc_depth
, bool initial_mode
, bool double_clk
, bool vpclk
)
632 u8 clk_divider
, mcu_cnt
, div
;
633 static const u8 depth
[] = {
634 [RTSX_SSC_DEPTH_4M
] = RTS5261_SSC_DEPTH_4M
,
635 [RTSX_SSC_DEPTH_2M
] = RTS5261_SSC_DEPTH_2M
,
636 [RTSX_SSC_DEPTH_1M
] = RTS5261_SSC_DEPTH_1M
,
637 [RTSX_SSC_DEPTH_500K
] = RTS5261_SSC_DEPTH_512K
,
641 /* We use 250k(around) here, in initial stage */
642 clk_divider
= SD_CLK_DIVIDE_128
;
643 card_clock
= 30000000;
645 clk_divider
= SD_CLK_DIVIDE_0
;
647 err
= rtsx_pci_write_register(pcr
, SD_CFG1
,
648 SD_CLK_DIVIDE_MASK
, clk_divider
);
652 card_clock
/= 1000000;
653 pcr_dbg(pcr
, "Switch card clock to %dMHz\n", card_clock
);
656 if (!initial_mode
&& double_clk
)
657 clk
= card_clock
* 2;
658 pcr_dbg(pcr
, "Internal SSC clock: %dMHz (cur_clock = %d)\n",
659 clk
, pcr
->cur_clock
);
661 if (clk
== pcr
->cur_clock
)
664 if (pcr
->ops
->conv_clk_and_div_n
)
665 n
= pcr
->ops
->conv_clk_and_div_n(clk
, CLK_TO_DIV_N
);
668 if ((clk
<= 4) || (n
> 396))
671 mcu_cnt
= 125/clk
+ 3;
676 while ((n
< MIN_DIV_N_PCR
- 4) && (div
< CLK_DIV_8
)) {
677 if (pcr
->ops
->conv_clk_and_div_n
) {
678 int dbl_clk
= pcr
->ops
->conv_clk_and_div_n(n
,
680 n
= pcr
->ops
->conv_clk_and_div_n(dbl_clk
,
689 pcr_dbg(pcr
, "n = %d, div = %d\n", n
, div
);
691 ssc_depth
= depth
[ssc_depth
];
693 ssc_depth
= double_ssc_depth(ssc_depth
);
696 if (div
== CLK_DIV_2
) {
700 ssc_depth
= RTS5261_SSC_DEPTH_8M
;
701 } else if (div
== CLK_DIV_4
) {
705 ssc_depth
= RTS5261_SSC_DEPTH_8M
;
706 } else if (div
== CLK_DIV_8
) {
710 ssc_depth
= RTS5261_SSC_DEPTH_8M
;
715 pcr_dbg(pcr
, "ssc_depth = %d\n", ssc_depth
);
717 rtsx_pci_init_cmd(pcr
);
718 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
719 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
720 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_DIV
,
721 0xFF, (div
<< 4) | mcu_cnt
);
722 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_CTL1
, SSC_RSTB
, 0);
723 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_CTL2
,
724 SSC_DEPTH_MASK
, ssc_depth
);
725 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_DIV_N_0
, 0xFF, n
);
726 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_CTL1
, SSC_RSTB
, SSC_RSTB
);
728 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
,
730 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK1_CTL
,
732 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
,
733 PHASE_NOT_RESET
, PHASE_NOT_RESET
);
734 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK1_CTL
,
735 PHASE_NOT_RESET
, PHASE_NOT_RESET
);
738 err
= rtsx_pci_send_cmd(pcr
, 2000);
742 /* Wait SSC clock stable */
743 udelay(SSC_CLOCK_STABLE_WAIT
);
744 err
= rtsx_pci_write_register(pcr
, CLK_CTL
, CLK_LOW_FREQ
, 0);
748 pcr
->cur_clock
= clk
;
753 void rts5261_init_params(struct rtsx_pcr
*pcr
)
755 struct rtsx_cr_option
*option
= &pcr
->option
;
756 struct rtsx_hw_param
*hw_param
= &pcr
->hw_param
;
758 pcr
->extra_caps
= EXTRA_CAPS_SD_SDR50
| EXTRA_CAPS_SD_SDR104
;
760 pcr
->ops
= &rts5261_pcr_ops
;
763 pcr
->card_drive_sel
= RTSX_CARD_DRIVE_DEFAULT
;
764 pcr
->sd30_drive_sel_1v8
= CFG_DRIVER_TYPE_B
;
765 pcr
->sd30_drive_sel_3v3
= CFG_DRIVER_TYPE_B
;
766 pcr
->aspm_en
= ASPM_L1_EN
;
767 pcr
->tx_initial_phase
= SET_CLOCK_PHASE(20, 27, 16);
768 pcr
->rx_initial_phase
= SET_CLOCK_PHASE(24, 6, 5);
770 pcr
->ic_version
= rts5261_get_ic_version(pcr
);
771 pcr
->sd_pull_ctl_enable_tbl
= rts5261_sd_pull_ctl_enable_tbl
;
772 pcr
->sd_pull_ctl_disable_tbl
= rts5261_sd_pull_ctl_disable_tbl
;
774 pcr
->reg_pm_ctrl3
= RTS5261_AUTOLOAD_CFG3
;
776 option
->dev_flags
= (LTR_L1SS_PWR_GATE_CHECK_CARD_EN
777 | LTR_L1SS_PWR_GATE_EN
);
778 option
->ltr_en
= true;
780 /* init latency of active, idle, L1OFF to 60us, 300us, 3ms */
781 option
->ltr_active_latency
= LTR_ACTIVE_LATENCY_DEF
;
782 option
->ltr_idle_latency
= LTR_IDLE_LATENCY_DEF
;
783 option
->ltr_l1off_latency
= LTR_L1OFF_LATENCY_DEF
;
784 option
->l1_snooze_delay
= L1_SNOOZE_DELAY_DEF
;
785 option
->ltr_l1off_sspwrgate
= 0x7F;
786 option
->ltr_l1off_snooze_sspwrgate
= 0x78;
787 option
->dev_aspm_mode
= DEV_ASPM_DYNAMIC
;
790 hw_param
->interrupt_en
|= SD_OC_INT_EN
;
791 hw_param
->ocp_glitch
= SD_OCP_GLITCH_800U
;
792 option
->sd_800mA_ocp_thd
= RTS5261_LDO1_OCP_THD_1040
;