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 * Ricky WU <ricky_wu@realtek.com>
8 * Rui FENG <rui_feng@realsil.com.cn>
9 * Wei WANG <wei_wang@realsil.com.cn>
12 #include <linux/module.h>
13 #include <linux/delay.h>
14 #include <linux/rtsx_pci.h>
19 static u8
rts5228_get_ic_version(struct rtsx_pcr
*pcr
)
23 rtsx_pci_read_register(pcr
, DUMMY_REG_RESET_0
, &val
);
24 return val
& IC_VERSION_MASK
;
27 static void rts5228_fill_driving(struct rtsx_pcr
*pcr
, u8 voltage
)
29 u8 driving_3v3
[4][3] = {
35 u8 driving_1v8
[4][3] = {
41 u8 (*driving
)[3], drive_sel
;
43 if (voltage
== OUTPUT_3V3
) {
44 driving
= driving_3v3
;
45 drive_sel
= pcr
->sd30_drive_sel_3v3
;
47 driving
= driving_1v8
;
48 drive_sel
= pcr
->sd30_drive_sel_1v8
;
51 rtsx_pci_write_register(pcr
, SD30_CLK_DRIVE_SEL
,
52 0xFF, driving
[drive_sel
][0]);
54 rtsx_pci_write_register(pcr
, SD30_CMD_DRIVE_SEL
,
55 0xFF, driving
[drive_sel
][1]);
57 rtsx_pci_write_register(pcr
, SD30_DAT_DRIVE_SEL
,
58 0xFF, driving
[drive_sel
][2]);
61 static void rtsx5228_fetch_vendor_settings(struct rtsx_pcr
*pcr
)
63 struct pci_dev
*pdev
= pcr
->pci
;
67 pci_read_config_dword(pdev
, PCR_SETTING_REG1
, ®
);
68 pcr_dbg(pcr
, "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG1
, reg
);
70 if (!rtsx_vendor_setting_valid(reg
)) {
71 pcr_dbg(pcr
, "skip fetch vendor setting\n");
74 pcr
->sd30_drive_sel_1v8
= rtsx_reg_to_sd30_drive_sel_1v8(reg
);
75 pcr
->aspm_en
= rtsx_reg_to_aspm(reg
);
78 pci_read_config_dword(pdev
, PCR_SETTING_REG2
, ®
);
79 pcr_dbg(pcr
, "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG2
, reg
);
81 pcr
->rtd3_en
= rtsx_reg_to_rtd3(reg
);
82 if (rtsx_check_mmc_support(reg
))
83 pcr
->extra_caps
|= EXTRA_CAPS_NO_MMC
;
84 pcr
->sd30_drive_sel_3v3
= rtsx_reg_to_sd30_drive_sel_3v3(reg
);
85 if (rtsx_reg_check_reverse_socket(reg
))
86 pcr
->flags
|= PCR_REVERSE_SOCKET
;
89 static int rts5228_optimize_phy(struct rtsx_pcr
*pcr
)
91 return rtsx_pci_write_phy_register(pcr
, 0x07, 0x8F40);
94 static void rts5228_force_power_down(struct rtsx_pcr
*pcr
, u8 pm_state
)
96 /* Set relink_time to 0 */
97 rtsx_pci_write_register(pcr
, AUTOLOAD_CFG_BASE
+ 1, MASK_8_BIT_DEF
, 0);
98 rtsx_pci_write_register(pcr
, AUTOLOAD_CFG_BASE
+ 2, MASK_8_BIT_DEF
, 0);
99 rtsx_pci_write_register(pcr
, AUTOLOAD_CFG_BASE
+ 3,
100 RELINK_TIME_MASK
, 0);
102 rtsx_pci_write_register(pcr
, pcr
->reg_pm_ctrl3
,
103 D3_DELINK_MODE_EN
, D3_DELINK_MODE_EN
);
105 rtsx_pci_write_register(pcr
, FPDCTL
,
106 SSC_POWER_DOWN
, SSC_POWER_DOWN
);
109 static int rts5228_enable_auto_blink(struct rtsx_pcr
*pcr
)
111 return rtsx_pci_write_register(pcr
, OLT_LED_CTL
,
112 LED_SHINE_MASK
, LED_SHINE_EN
);
115 static int rts5228_disable_auto_blink(struct rtsx_pcr
*pcr
)
117 return rtsx_pci_write_register(pcr
, OLT_LED_CTL
,
118 LED_SHINE_MASK
, LED_SHINE_DISABLE
);
121 static int rts5228_turn_on_led(struct rtsx_pcr
*pcr
)
123 return rtsx_pci_write_register(pcr
, GPIO_CTL
,
127 static int rts5228_turn_off_led(struct rtsx_pcr
*pcr
)
129 return rtsx_pci_write_register(pcr
, GPIO_CTL
,
133 /* SD Pull Control Enable:
134 * SD_DAT[3:0] ==> pull up
138 * SD_CLK ==> pull down
140 static const u32 rts5228_sd_pull_ctl_enable_tbl
[] = {
141 RTSX_REG_PAIR(CARD_PULL_CTL2
, 0xAA),
142 RTSX_REG_PAIR(CARD_PULL_CTL3
, 0xE9),
146 /* SD Pull Control Disable:
147 * SD_DAT[3:0] ==> pull down
149 * SD_WP ==> pull down
150 * SD_CMD ==> pull down
151 * SD_CLK ==> pull down
153 static const u32 rts5228_sd_pull_ctl_disable_tbl
[] = {
154 RTSX_REG_PAIR(CARD_PULL_CTL2
, 0x55),
155 RTSX_REG_PAIR(CARD_PULL_CTL3
, 0xD5),
159 static int rts5228_sd_set_sample_push_timing_sd30(struct rtsx_pcr
*pcr
)
161 rtsx_pci_write_register(pcr
, SD_CFG1
, SD_MODE_SELECT_MASK
162 | SD_ASYNC_FIFO_NOT_RST
, SD_30_MODE
| SD_ASYNC_FIFO_NOT_RST
);
163 rtsx_pci_write_register(pcr
, CLK_CTL
, CLK_LOW_FREQ
, CLK_LOW_FREQ
);
164 rtsx_pci_write_register(pcr
, CARD_CLK_SOURCE
, 0xFF,
165 CRC_VAR_CLK0
| SD30_FIX_CLK
| SAMPLE_VAR_CLK1
);
166 rtsx_pci_write_register(pcr
, CLK_CTL
, CLK_LOW_FREQ
, 0);
171 static int rts5228_card_power_on(struct rtsx_pcr
*pcr
, int card
)
173 struct rtsx_cr_option
*option
= &pcr
->option
;
176 rtsx_pci_enable_ocp(pcr
);
178 rtsx_pci_write_register(pcr
, REG_CRC_DUMMY_0
,
179 CFG_SD_POW_AUTO_PD
, CFG_SD_POW_AUTO_PD
);
181 rtsx_pci_write_register(pcr
, RTS5228_LDO1_CFG1
,
182 RTS5228_LDO1_TUNE_MASK
, RTS5228_LDO1_33
);
184 rtsx_pci_write_register(pcr
, RTS5228_LDO1233318_POW_CTL
,
185 RTS5228_LDO1_POWERON_MASK
, RTS5228_LDO1_SOFTSTART
);
187 rtsx_pci_write_register(pcr
, RTS5228_LDO1233318_POW_CTL
,
188 RTS5228_LDO1_POWERON_MASK
, RTS5228_LDO1_FULLON
);
191 rtsx_pci_write_register(pcr
, RTS5228_LDO1233318_POW_CTL
,
192 RTS5228_LDO3318_POWERON
, RTS5228_LDO3318_POWERON
);
196 rtsx_pci_write_register(pcr
, CARD_OE
, SD_OUTPUT_EN
, SD_OUTPUT_EN
);
198 /* Initialize SD_CFG1 register */
199 rtsx_pci_write_register(pcr
, SD_CFG1
, 0xFF,
200 SD_CLK_DIVIDE_128
| SD_20_MODE
| SD_BUS_WIDTH_1BIT
);
202 rtsx_pci_write_register(pcr
, SD_SAMPLE_POINT_CTL
,
203 0xFF, SD20_RX_POS_EDGE
);
204 rtsx_pci_write_register(pcr
, SD_PUSH_POINT_CTL
, 0xFF, 0);
205 rtsx_pci_write_register(pcr
, CARD_STOP
, SD_STOP
| SD_CLR_ERR
,
206 SD_STOP
| SD_CLR_ERR
);
208 /* Reset SD_CFG3 register */
209 rtsx_pci_write_register(pcr
, SD_CFG3
, SD30_CLK_END_EN
, 0);
210 rtsx_pci_write_register(pcr
, REG_SD_STOP_SDCLK_CFG
,
211 SD30_CLK_STOP_CFG_EN
| SD30_CLK_STOP_CFG1
|
212 SD30_CLK_STOP_CFG0
, 0);
214 if (pcr
->extra_caps
& EXTRA_CAPS_SD_SDR50
||
215 pcr
->extra_caps
& EXTRA_CAPS_SD_SDR104
)
216 rts5228_sd_set_sample_push_timing_sd30(pcr
);
221 static int rts5228_switch_output_voltage(struct rtsx_pcr
*pcr
, u8 voltage
)
226 rtsx_pci_write_register(pcr
, RTS5228_CARD_PWR_CTL
,
227 RTS5228_PUPDC
, RTS5228_PUPDC
);
231 rtsx_pci_read_phy_register(pcr
, PHY_TUNE
, &val
);
232 val
|= PHY_TUNE_SDBUS_33
;
233 err
= rtsx_pci_write_phy_register(pcr
, PHY_TUNE
, val
);
237 rtsx_pci_write_register(pcr
, RTS5228_DV3318_CFG
,
238 RTS5228_DV3318_TUNE_MASK
, RTS5228_DV3318_33
);
239 rtsx_pci_write_register(pcr
, SD_PAD_CTL
,
243 rtsx_pci_read_phy_register(pcr
, PHY_TUNE
, &val
);
244 val
&= ~PHY_TUNE_SDBUS_33
;
245 err
= rtsx_pci_write_phy_register(pcr
, PHY_TUNE
, val
);
249 rtsx_pci_write_register(pcr
, RTS5228_DV3318_CFG
,
250 RTS5228_DV3318_TUNE_MASK
, RTS5228_DV3318_18
);
251 rtsx_pci_write_register(pcr
, SD_PAD_CTL
,
252 SD_IO_USING_1V8
, SD_IO_USING_1V8
);
259 rts5228_fill_driving(pcr
, voltage
);
264 static void rts5228_stop_cmd(struct rtsx_pcr
*pcr
)
266 rtsx_pci_writel(pcr
, RTSX_HCBCTLR
, STOP_CMD
);
267 rtsx_pci_writel(pcr
, RTSX_HDBCTLR
, STOP_DMA
);
268 rtsx_pci_write_register(pcr
, RTS5260_DMA_RST_CTL_0
,
269 RTS5260_DMA_RST
| RTS5260_ADMA3_RST
,
270 RTS5260_DMA_RST
| RTS5260_ADMA3_RST
);
271 rtsx_pci_write_register(pcr
, RBCTL
, RB_FLUSH
, RB_FLUSH
);
274 static void rts5228_card_before_power_off(struct rtsx_pcr
*pcr
)
276 rts5228_stop_cmd(pcr
);
277 rts5228_switch_output_voltage(pcr
, OUTPUT_3V3
);
280 static void rts5228_enable_ocp(struct rtsx_pcr
*pcr
)
284 val
= SD_OCP_INT_EN
| SD_DETECT_EN
;
285 rtsx_pci_write_register(pcr
, REG_OCPCTL
, 0xFF, val
);
286 rtsx_pci_write_register(pcr
, RTS5228_LDO1_CFG0
,
287 RTS5228_LDO1_OCP_EN
| RTS5228_LDO1_OCP_LMT_EN
,
288 RTS5228_LDO1_OCP_EN
| RTS5228_LDO1_OCP_LMT_EN
);
291 static void rts5228_disable_ocp(struct rtsx_pcr
*pcr
)
295 mask
= SD_OCP_INT_EN
| SD_DETECT_EN
;
296 rtsx_pci_write_register(pcr
, REG_OCPCTL
, mask
, 0);
297 rtsx_pci_write_register(pcr
, RTS5228_LDO1_CFG0
,
298 RTS5228_LDO1_OCP_EN
| RTS5228_LDO1_OCP_LMT_EN
, 0);
301 static int rts5228_card_power_off(struct rtsx_pcr
*pcr
, int card
)
305 rts5228_card_before_power_off(pcr
);
306 err
= rtsx_pci_write_register(pcr
, RTS5228_LDO1233318_POW_CTL
,
307 RTS5228_LDO_POWERON_MASK
, 0);
308 rtsx_pci_write_register(pcr
, REG_CRC_DUMMY_0
, CFG_SD_POW_AUTO_PD
, 0);
310 if (pcr
->option
.ocp_en
)
311 rtsx_pci_disable_ocp(pcr
);
316 static void rts5228_init_ocp(struct rtsx_pcr
*pcr
)
318 struct rtsx_cr_option
*option
= &pcr
->option
;
320 if (option
->ocp_en
) {
323 rtsx_pci_write_register(pcr
, RTS5228_LDO1_CFG0
,
324 RTS5228_LDO1_OCP_EN
| RTS5228_LDO1_OCP_LMT_EN
,
325 RTS5228_LDO1_OCP_EN
| RTS5228_LDO1_OCP_LMT_EN
);
327 rtsx_pci_write_register(pcr
, RTS5228_LDO1_CFG0
,
328 RTS5228_LDO1_OCP_THD_MASK
, option
->sd_800mA_ocp_thd
);
330 rtsx_pci_write_register(pcr
, RTS5228_LDO1_CFG0
,
331 RTS5228_LDO1_OCP_LMT_THD_MASK
,
332 RTS5228_LDO1_LMT_THD_1500
);
334 rtsx_pci_read_register(pcr
, RTS5228_LDO1_CFG0
, &val
);
336 mask
= SD_OCP_GLITCH_MASK
;
337 val
= pcr
->hw_param
.ocp_glitch
;
338 rtsx_pci_write_register(pcr
, REG_OCPGLITCH
, mask
, val
);
340 rts5228_enable_ocp(pcr
);
343 rtsx_pci_write_register(pcr
, RTS5228_LDO1_CFG0
,
344 RTS5228_LDO1_OCP_EN
| RTS5228_LDO1_OCP_LMT_EN
, 0);
348 static void rts5228_clear_ocpstat(struct rtsx_pcr
*pcr
)
353 mask
= SD_OCP_INT_CLR
| SD_OC_CLR
;
354 val
= SD_OCP_INT_CLR
| SD_OC_CLR
;
356 rtsx_pci_write_register(pcr
, REG_OCPCTL
, mask
, val
);
359 rtsx_pci_write_register(pcr
, REG_OCPCTL
, mask
, 0);
363 static void rts5228_process_ocp(struct rtsx_pcr
*pcr
)
365 if (!pcr
->option
.ocp_en
)
368 rtsx_pci_get_ocpstat(pcr
, &pcr
->ocp_stat
);
370 if (pcr
->ocp_stat
& (SD_OC_NOW
| SD_OC_EVER
)) {
371 rts5228_clear_ocpstat(pcr
);
372 rts5228_card_power_off(pcr
, RTSX_SD_CARD
);
373 rtsx_pci_write_register(pcr
, CARD_OE
, SD_OUTPUT_EN
, 0);
379 static void rts5228_init_from_cfg(struct rtsx_pcr
*pcr
)
381 struct pci_dev
*pdev
= pcr
->pci
;
384 struct rtsx_cr_option
*option
= &pcr
->option
;
386 l1ss
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_L1SS
);
390 pci_read_config_dword(pdev
, l1ss
+ PCI_L1SS_CTL1
, &lval
);
392 if (0 == (lval
& 0x0F))
393 rtsx_pci_enable_oobs_polling(pcr
);
395 rtsx_pci_disable_oobs_polling(pcr
);
397 if (lval
& PCI_L1SS_CTL1_ASPM_L1_1
)
398 rtsx_set_dev_flag(pcr
, ASPM_L1_1_EN
);
400 rtsx_clear_dev_flag(pcr
, ASPM_L1_1_EN
);
402 if (lval
& PCI_L1SS_CTL1_ASPM_L1_2
)
403 rtsx_set_dev_flag(pcr
, ASPM_L1_2_EN
);
405 rtsx_clear_dev_flag(pcr
, ASPM_L1_2_EN
);
407 if (lval
& PCI_L1SS_CTL1_PCIPM_L1_1
)
408 rtsx_set_dev_flag(pcr
, PM_L1_1_EN
);
410 rtsx_clear_dev_flag(pcr
, PM_L1_1_EN
);
412 if (lval
& PCI_L1SS_CTL1_PCIPM_L1_2
)
413 rtsx_set_dev_flag(pcr
, PM_L1_2_EN
);
415 rtsx_clear_dev_flag(pcr
, PM_L1_2_EN
);
417 rtsx_pci_write_register(pcr
, ASPM_FORCE_CTL
, 0xFF, 0);
418 if (option
->ltr_en
) {
421 pcie_capability_read_word(pcr
->pci
, PCI_EXP_DEVCTL2
, &val
);
422 if (val
& PCI_EXP_DEVCTL2_LTR_EN
) {
423 option
->ltr_enabled
= true;
424 option
->ltr_active
= true;
425 rtsx_set_ltr_latency(pcr
, option
->ltr_active_latency
);
427 option
->ltr_enabled
= false;
431 if (rtsx_check_dev_flag(pcr
, ASPM_L1_1_EN
| ASPM_L1_2_EN
432 | PM_L1_1_EN
| PM_L1_2_EN
))
433 option
->force_clkreq_0
= false;
435 option
->force_clkreq_0
= true;
438 static int rts5228_extra_init_hw(struct rtsx_pcr
*pcr
)
440 struct rtsx_cr_option
*option
= &pcr
->option
;
442 rtsx_pci_write_register(pcr
, RTS5228_AUTOLOAD_CFG1
,
443 CD_RESUME_EN_MASK
, CD_RESUME_EN_MASK
);
445 rts5228_init_from_cfg(pcr
);
447 rtsx_pci_write_register(pcr
, L1SUB_CONFIG1
,
448 AUX_CLK_ACTIVE_SEL_MASK
, MAC_CKSW_DONE
);
449 rtsx_pci_write_register(pcr
, L1SUB_CONFIG3
, 0xFF, 0);
451 rtsx_pci_write_register(pcr
, FUNC_FORCE_CTL
,
452 FUNC_FORCE_UPME_XMT_DBG
, FUNC_FORCE_UPME_XMT_DBG
);
454 rtsx_pci_write_register(pcr
, PCLK_CTL
,
455 PCLK_MODE_SEL
, PCLK_MODE_SEL
);
457 rtsx_pci_write_register(pcr
, PM_EVENT_DEBUG
, PME_DEBUG_0
, PME_DEBUG_0
);
458 rtsx_pci_write_register(pcr
, PM_CLK_FORCE_CTL
, CLK_PM_EN
, CLK_PM_EN
);
460 /* LED shine disabled, set initial shine cycle period */
461 rtsx_pci_write_register(pcr
, OLT_LED_CTL
, 0x0F, 0x02);
463 /* Configure driving */
464 rts5228_fill_driving(pcr
, OUTPUT_3V3
);
466 if (pcr
->flags
& PCR_REVERSE_SOCKET
)
467 rtsx_pci_write_register(pcr
, PETXCFG
, 0x30, 0x30);
469 rtsx_pci_write_register(pcr
, PETXCFG
, 0x30, 0x00);
472 * If u_force_clkreq_0 is enabled, CLKREQ# PIN will be forced
473 * to drive low, and we forcibly request clock.
475 if (option
->force_clkreq_0
)
476 rtsx_pci_write_register(pcr
, PETXCFG
,
477 FORCE_CLKREQ_DELINK_MASK
, FORCE_CLKREQ_LOW
);
479 rtsx_pci_write_register(pcr
, PETXCFG
,
480 FORCE_CLKREQ_DELINK_MASK
, FORCE_CLKREQ_HIGH
);
482 rtsx_pci_write_register(pcr
, PWD_SUSPEND_EN
, 0xFF, 0xFB);
483 rtsx_pci_write_register(pcr
, pcr
->reg_pm_ctrl3
, 0x10, 0x00);
484 rtsx_pci_write_register(pcr
, RTS5228_REG_PME_FORCE_CTL
,
485 FORCE_PM_CONTROL
| FORCE_PM_VALUE
, FORCE_PM_CONTROL
);
490 static void rts5228_enable_aspm(struct rtsx_pcr
*pcr
, bool enable
)
494 if (pcr
->aspm_enabled
== enable
)
497 mask
= FORCE_ASPM_VAL_MASK
| FORCE_ASPM_CTL0
| FORCE_ASPM_CTL1
;
498 val
= FORCE_ASPM_CTL0
| FORCE_ASPM_CTL1
;
499 val
|= (pcr
->aspm_en
& 0x02);
500 rtsx_pci_write_register(pcr
, ASPM_FORCE_CTL
, mask
, val
);
501 pcie_capability_clear_and_set_word(pcr
->pci
, PCI_EXP_LNKCTL
,
502 PCI_EXP_LNKCTL_ASPMC
, pcr
->aspm_en
);
503 pcr
->aspm_enabled
= enable
;
506 static void rts5228_disable_aspm(struct rtsx_pcr
*pcr
, bool enable
)
510 if (pcr
->aspm_enabled
== enable
)
513 pcie_capability_clear_and_set_word(pcr
->pci
, PCI_EXP_LNKCTL
,
514 PCI_EXP_LNKCTL_ASPMC
, 0);
515 mask
= FORCE_ASPM_VAL_MASK
| FORCE_ASPM_CTL0
| FORCE_ASPM_CTL1
;
516 val
= FORCE_ASPM_CTL0
| FORCE_ASPM_CTL1
;
517 rtsx_pci_write_register(pcr
, ASPM_FORCE_CTL
, mask
, val
);
518 rtsx_pci_write_register(pcr
, SD_CFG1
, SD_ASYNC_FIFO_NOT_RST
, 0);
520 pcr
->aspm_enabled
= enable
;
523 static void rts5228_set_aspm(struct rtsx_pcr
*pcr
, bool enable
)
526 rts5228_enable_aspm(pcr
, true);
528 rts5228_disable_aspm(pcr
, false);
531 static void rts5228_set_l1off_cfg_sub_d0(struct rtsx_pcr
*pcr
, int active
)
533 struct rtsx_cr_option
*option
= &pcr
->option
;
534 int aspm_L1_1
, aspm_L1_2
;
537 aspm_L1_1
= rtsx_check_dev_flag(pcr
, ASPM_L1_1_EN
);
538 aspm_L1_2
= rtsx_check_dev_flag(pcr
, ASPM_L1_2_EN
);
541 /* run, latency: 60us */
543 val
= option
->ltr_l1off_snooze_sspwrgate
;
545 /* l1off, latency: 300us */
547 val
= option
->ltr_l1off_sspwrgate
;
550 rtsx_set_l1off_sub(pcr
, val
);
553 static const struct pcr_ops rts5228_pcr_ops
= {
554 .fetch_vendor_settings
= rtsx5228_fetch_vendor_settings
,
555 .turn_on_led
= rts5228_turn_on_led
,
556 .turn_off_led
= rts5228_turn_off_led
,
557 .extra_init_hw
= rts5228_extra_init_hw
,
558 .enable_auto_blink
= rts5228_enable_auto_blink
,
559 .disable_auto_blink
= rts5228_disable_auto_blink
,
560 .card_power_on
= rts5228_card_power_on
,
561 .card_power_off
= rts5228_card_power_off
,
562 .switch_output_voltage
= rts5228_switch_output_voltage
,
563 .force_power_down
= rts5228_force_power_down
,
564 .stop_cmd
= rts5228_stop_cmd
,
565 .set_aspm
= rts5228_set_aspm
,
566 .set_l1off_cfg_sub_d0
= rts5228_set_l1off_cfg_sub_d0
,
567 .enable_ocp
= rts5228_enable_ocp
,
568 .disable_ocp
= rts5228_disable_ocp
,
569 .init_ocp
= rts5228_init_ocp
,
570 .process_ocp
= rts5228_process_ocp
,
571 .clear_ocpstat
= rts5228_clear_ocpstat
,
572 .optimize_phy
= rts5228_optimize_phy
,
576 static inline u8
double_ssc_depth(u8 depth
)
578 return ((depth
> 1) ? (depth
- 1) : depth
);
581 int rts5228_pci_switch_clock(struct rtsx_pcr
*pcr
, unsigned int card_clock
,
582 u8 ssc_depth
, bool initial_mode
, bool double_clk
, bool vpclk
)
586 u8 clk_divider
, mcu_cnt
, div
;
587 static const u8 depth
[] = {
588 [RTSX_SSC_DEPTH_4M
] = RTS5228_SSC_DEPTH_4M
,
589 [RTSX_SSC_DEPTH_2M
] = RTS5228_SSC_DEPTH_2M
,
590 [RTSX_SSC_DEPTH_1M
] = RTS5228_SSC_DEPTH_1M
,
591 [RTSX_SSC_DEPTH_500K
] = RTS5228_SSC_DEPTH_512K
,
595 /* We use 250k(around) here, in initial stage */
596 clk_divider
= SD_CLK_DIVIDE_128
;
597 card_clock
= 30000000;
599 clk_divider
= SD_CLK_DIVIDE_0
;
601 err
= rtsx_pci_write_register(pcr
, SD_CFG1
,
602 SD_CLK_DIVIDE_MASK
, clk_divider
);
606 card_clock
/= 1000000;
607 pcr_dbg(pcr
, "Switch card clock to %dMHz\n", card_clock
);
610 if (!initial_mode
&& double_clk
)
611 clk
= card_clock
* 2;
612 pcr_dbg(pcr
, "Internal SSC clock: %dMHz (cur_clock = %d)\n",
613 clk
, pcr
->cur_clock
);
615 if (clk
== pcr
->cur_clock
)
618 if (pcr
->ops
->conv_clk_and_div_n
)
619 n
= pcr
->ops
->conv_clk_and_div_n(clk
, CLK_TO_DIV_N
);
622 if ((clk
<= 4) || (n
> 396))
625 mcu_cnt
= 125/clk
+ 3;
630 while ((n
< MIN_DIV_N_PCR
- 4) && (div
< CLK_DIV_8
)) {
631 if (pcr
->ops
->conv_clk_and_div_n
) {
632 int dbl_clk
= pcr
->ops
->conv_clk_and_div_n(n
,
634 n
= pcr
->ops
->conv_clk_and_div_n(dbl_clk
,
643 pcr_dbg(pcr
, "n = %d, div = %d\n", n
, div
);
645 ssc_depth
= depth
[ssc_depth
];
647 ssc_depth
= double_ssc_depth(ssc_depth
);
650 if (div
== CLK_DIV_2
) {
654 ssc_depth
= RTS5228_SSC_DEPTH_8M
;
655 } else if (div
== CLK_DIV_4
) {
659 ssc_depth
= RTS5228_SSC_DEPTH_8M
;
660 } else if (div
== CLK_DIV_8
) {
664 ssc_depth
= RTS5228_SSC_DEPTH_8M
;
669 pcr_dbg(pcr
, "ssc_depth = %d\n", ssc_depth
);
671 rtsx_pci_init_cmd(pcr
);
672 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
673 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
674 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_DIV
,
675 0xFF, (div
<< 4) | mcu_cnt
);
676 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_CTL1
, SSC_RSTB
, 0);
677 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_CTL2
,
678 SSC_DEPTH_MASK
, ssc_depth
);
679 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_DIV_N_0
, 0xFF, n
);
680 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SSC_CTL1
, SSC_RSTB
, SSC_RSTB
);
682 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
,
684 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK1_CTL
,
686 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
,
687 PHASE_NOT_RESET
, PHASE_NOT_RESET
);
688 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK1_CTL
,
689 PHASE_NOT_RESET
, PHASE_NOT_RESET
);
692 err
= rtsx_pci_send_cmd(pcr
, 2000);
696 /* Wait SSC clock stable */
697 udelay(SSC_CLOCK_STABLE_WAIT
);
698 err
= rtsx_pci_write_register(pcr
, CLK_CTL
, CLK_LOW_FREQ
, 0);
702 pcr
->cur_clock
= clk
;
707 void rts5228_init_params(struct rtsx_pcr
*pcr
)
709 struct rtsx_cr_option
*option
= &pcr
->option
;
710 struct rtsx_hw_param
*hw_param
= &pcr
->hw_param
;
712 pcr
->extra_caps
= EXTRA_CAPS_SD_SDR50
| EXTRA_CAPS_SD_SDR104
;
714 pcr
->ops
= &rts5228_pcr_ops
;
717 pcr
->card_drive_sel
= RTSX_CARD_DRIVE_DEFAULT
;
718 pcr
->sd30_drive_sel_1v8
= CFG_DRIVER_TYPE_B
;
719 pcr
->sd30_drive_sel_3v3
= CFG_DRIVER_TYPE_B
;
720 pcr
->aspm_en
= ASPM_L1_EN
;
721 pcr
->tx_initial_phase
= SET_CLOCK_PHASE(28, 27, 11);
722 pcr
->rx_initial_phase
= SET_CLOCK_PHASE(24, 6, 5);
724 pcr
->ic_version
= rts5228_get_ic_version(pcr
);
725 pcr
->sd_pull_ctl_enable_tbl
= rts5228_sd_pull_ctl_enable_tbl
;
726 pcr
->sd_pull_ctl_disable_tbl
= rts5228_sd_pull_ctl_disable_tbl
;
728 pcr
->reg_pm_ctrl3
= RTS5228_AUTOLOAD_CFG3
;
730 option
->dev_flags
= (LTR_L1SS_PWR_GATE_CHECK_CARD_EN
731 | LTR_L1SS_PWR_GATE_EN
);
732 option
->ltr_en
= true;
734 /* init latency of active, idle, L1OFF to 60us, 300us, 3ms */
735 option
->ltr_active_latency
= LTR_ACTIVE_LATENCY_DEF
;
736 option
->ltr_idle_latency
= LTR_IDLE_LATENCY_DEF
;
737 option
->ltr_l1off_latency
= LTR_L1OFF_LATENCY_DEF
;
738 option
->l1_snooze_delay
= L1_SNOOZE_DELAY_DEF
;
739 option
->ltr_l1off_sspwrgate
= 0x7F;
740 option
->ltr_l1off_snooze_sspwrgate
= 0x78;
743 hw_param
->interrupt_en
|= SD_OC_INT_EN
;
744 hw_param
->ocp_glitch
= SD_OCP_GLITCH_800U
;
745 option
->sd_800mA_ocp_thd
= RTS5228_LDO1_OCP_THD_930
;