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, ®); 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, ®); 75e455b69dSRui Feng pcr_dbg(pcr, "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG2, reg); 765b4258f6SRicky Wu 7771732e24SKai-Heng Feng if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A)) 785b4258f6SRicky Wu pcr->rtd3_en = rtsx_reg_to_rtd3_uhsii(reg); 795b4258f6SRicky Wu 807c33e3c4SRicky Wu if (rtsx_check_mmc_support(reg)) 817c33e3c4SRicky Wu pcr->extra_caps |= EXTRA_CAPS_NO_MMC; 82e455b69dSRui Feng pcr->sd30_drive_sel_3v3 = rtsx_reg_to_sd30_drive_sel_3v3(reg); 83e455b69dSRui Feng if (rtsx_reg_check_reverse_socket(reg)) 84e455b69dSRui Feng pcr->flags |= PCR_REVERSE_SOCKET; 85e455b69dSRui Feng } 86e455b69dSRui Feng 87e455b69dSRui Feng static void rts5249_init_from_cfg(struct rtsx_pcr *pcr) 88e455b69dSRui Feng { 89e455b69dSRui Feng struct rtsx_cr_option *option = &(pcr->option); 90e455b69dSRui Feng 917c33e3c4SRicky Wu if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A)) { 92e455b69dSRui Feng if (rtsx_check_dev_flag(pcr, ASPM_L1_1_EN | ASPM_L1_2_EN 93e455b69dSRui Feng | PM_L1_1_EN | PM_L1_2_EN)) 94*0e4cac55SRicky WU rtsx_pci_disable_oobs_polling(pcr); 95e455b69dSRui Feng else 96*0e4cac55SRicky WU rtsx_pci_enable_oobs_polling(pcr); 97*0e4cac55SRicky WU } 98e455b69dSRui Feng 99*0e4cac55SRicky WU if (option->ltr_en) { 100*0e4cac55SRicky WU if (option->ltr_enabled) 101*0e4cac55SRicky WU rtsx_set_ltr_latency(pcr, option->ltr_active_latency); 102*0e4cac55SRicky WU } 103e455b69dSRui Feng } 104e455b69dSRui Feng 10571732e24SKai-Heng Feng static void rts52xa_force_power_down(struct rtsx_pcr *pcr, u8 pm_state, bool runtime) 10671732e24SKai-Heng Feng { 10771732e24SKai-Heng Feng /* Set relink_time to 0 */ 10871732e24SKai-Heng Feng rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 1, MASK_8_BIT_DEF, 0); 10971732e24SKai-Heng Feng rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 2, MASK_8_BIT_DEF, 0); 11071732e24SKai-Heng Feng rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 3, 11171732e24SKai-Heng Feng RELINK_TIME_MASK, 0); 11271732e24SKai-Heng Feng 11371732e24SKai-Heng Feng rtsx_pci_write_register(pcr, RTS524A_PM_CTRL3, 11471732e24SKai-Heng Feng D3_DELINK_MODE_EN, D3_DELINK_MODE_EN); 11571732e24SKai-Heng Feng 11671732e24SKai-Heng Feng if (!runtime) { 11771732e24SKai-Heng Feng rtsx_pci_write_register(pcr, RTS524A_AUTOLOAD_CFG1, 11871732e24SKai-Heng Feng CD_RESUME_EN_MASK, 0); 11971732e24SKai-Heng Feng rtsx_pci_write_register(pcr, RTS524A_PM_CTRL3, 0x01, 0x00); 12071732e24SKai-Heng Feng rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL, 0x30, 0x20); 12171732e24SKai-Heng Feng } 12271732e24SKai-Heng Feng 12371732e24SKai-Heng Feng rtsx_pci_write_register(pcr, FPDCTL, ALL_POWER_DOWN, ALL_POWER_DOWN); 12471732e24SKai-Heng Feng } 12571732e24SKai-Heng Feng 1267c33e3c4SRicky Wu static void rts52xa_save_content_from_efuse(struct rtsx_pcr *pcr) 1277c33e3c4SRicky Wu { 1287c33e3c4SRicky Wu u8 cnt, sv; 1297c33e3c4SRicky Wu u16 j = 0; 1307c33e3c4SRicky Wu u8 tmp; 1317c33e3c4SRicky Wu u8 val; 1327c33e3c4SRicky Wu int i; 1337c33e3c4SRicky Wu 1347c33e3c4SRicky Wu rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL, 1357c33e3c4SRicky Wu REG_EFUSE_BYPASS | REG_EFUSE_POR, REG_EFUSE_POR); 1367c33e3c4SRicky Wu udelay(1); 1377c33e3c4SRicky Wu 1387c33e3c4SRicky Wu pcr_dbg(pcr, "Enable efuse por!"); 1397c33e3c4SRicky Wu pcr_dbg(pcr, "save efuse to autoload"); 1407c33e3c4SRicky Wu 1417c33e3c4SRicky Wu rtsx_pci_write_register(pcr, RTS525A_EFUSE_ADD, REG_EFUSE_ADD_MASK, 0x00); 1427c33e3c4SRicky Wu rtsx_pci_write_register(pcr, RTS525A_EFUSE_CTL, 1437c33e3c4SRicky Wu REG_EFUSE_ENABLE | REG_EFUSE_MODE, REG_EFUSE_ENABLE); 1447c33e3c4SRicky Wu /* Wait transfer end */ 1457c33e3c4SRicky Wu for (j = 0; j < 1024; j++) { 1467c33e3c4SRicky Wu rtsx_pci_read_register(pcr, RTS525A_EFUSE_CTL, &tmp); 1477c33e3c4SRicky Wu if ((tmp & 0x80) == 0) 1487c33e3c4SRicky Wu break; 1497c33e3c4SRicky Wu } 1507c33e3c4SRicky Wu rtsx_pci_read_register(pcr, RTS525A_EFUSE_DATA, &val); 1517c33e3c4SRicky Wu cnt = val & 0x0F; 1527c33e3c4SRicky Wu sv = val & 0x10; 1537c33e3c4SRicky Wu 1547c33e3c4SRicky Wu if (sv) { 1557c33e3c4SRicky Wu for (i = 0; i < 4; i++) { 1567c33e3c4SRicky Wu rtsx_pci_write_register(pcr, RTS525A_EFUSE_ADD, 1577c33e3c4SRicky Wu REG_EFUSE_ADD_MASK, 0x04 + i); 1587c33e3c4SRicky Wu rtsx_pci_write_register(pcr, RTS525A_EFUSE_CTL, 1597c33e3c4SRicky Wu REG_EFUSE_ENABLE | REG_EFUSE_MODE, REG_EFUSE_ENABLE); 1607c33e3c4SRicky Wu /* Wait transfer end */ 1617c33e3c4SRicky Wu for (j = 0; j < 1024; j++) { 1627c33e3c4SRicky Wu rtsx_pci_read_register(pcr, RTS525A_EFUSE_CTL, &tmp); 1637c33e3c4SRicky Wu if ((tmp & 0x80) == 0) 1647c33e3c4SRicky Wu break; 1657c33e3c4SRicky Wu } 1667c33e3c4SRicky Wu rtsx_pci_read_register(pcr, RTS525A_EFUSE_DATA, &val); 1677c33e3c4SRicky Wu rtsx_pci_write_register(pcr, 0xFF04 + i, 0xFF, val); 1687c33e3c4SRicky Wu } 1697c33e3c4SRicky Wu } else { 1707c33e3c4SRicky Wu rtsx_pci_write_register(pcr, 0xFF04, 0xFF, (u8)PCI_VID(pcr)); 1717c33e3c4SRicky Wu rtsx_pci_write_register(pcr, 0xFF05, 0xFF, (u8)(PCI_VID(pcr) >> 8)); 1727c33e3c4SRicky Wu rtsx_pci_write_register(pcr, 0xFF06, 0xFF, (u8)PCI_PID(pcr)); 1737c33e3c4SRicky Wu rtsx_pci_write_register(pcr, 0xFF07, 0xFF, (u8)(PCI_PID(pcr) >> 8)); 1747c33e3c4SRicky Wu } 1757c33e3c4SRicky Wu 1767c33e3c4SRicky Wu for (i = 0; i < cnt * 4; i++) { 1777c33e3c4SRicky Wu if (sv) 1787c33e3c4SRicky Wu rtsx_pci_write_register(pcr, RTS525A_EFUSE_ADD, 1797c33e3c4SRicky Wu REG_EFUSE_ADD_MASK, 0x08 + i); 1807c33e3c4SRicky Wu else 1817c33e3c4SRicky Wu rtsx_pci_write_register(pcr, RTS525A_EFUSE_ADD, 1827c33e3c4SRicky Wu REG_EFUSE_ADD_MASK, 0x04 + i); 1837c33e3c4SRicky Wu rtsx_pci_write_register(pcr, RTS525A_EFUSE_CTL, 1847c33e3c4SRicky Wu REG_EFUSE_ENABLE | REG_EFUSE_MODE, REG_EFUSE_ENABLE); 1857c33e3c4SRicky Wu /* Wait transfer end */ 1867c33e3c4SRicky Wu for (j = 0; j < 1024; j++) { 1877c33e3c4SRicky Wu rtsx_pci_read_register(pcr, RTS525A_EFUSE_CTL, &tmp); 1887c33e3c4SRicky Wu if ((tmp & 0x80) == 0) 1897c33e3c4SRicky Wu break; 1907c33e3c4SRicky Wu } 1917c33e3c4SRicky Wu rtsx_pci_read_register(pcr, RTS525A_EFUSE_DATA, &val); 1927c33e3c4SRicky Wu rtsx_pci_write_register(pcr, 0xFF08 + i, 0xFF, val); 1937c33e3c4SRicky Wu } 1947c33e3c4SRicky Wu rtsx_pci_write_register(pcr, 0xFF00, 0xFF, (cnt & 0x7F) | 0x80); 1957c33e3c4SRicky Wu rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL, 1967c33e3c4SRicky Wu REG_EFUSE_BYPASS | REG_EFUSE_POR, REG_EFUSE_BYPASS); 1977c33e3c4SRicky Wu pcr_dbg(pcr, "Disable efuse por!"); 1987c33e3c4SRicky Wu } 1997c33e3c4SRicky Wu 2007c33e3c4SRicky Wu static void rts52xa_save_content_to_autoload_space(struct rtsx_pcr *pcr) 2017c33e3c4SRicky Wu { 2027c33e3c4SRicky Wu u8 val; 2037c33e3c4SRicky Wu 2047c33e3c4SRicky Wu rtsx_pci_read_register(pcr, RESET_LOAD_REG, &val); 2057c33e3c4SRicky Wu if (val & 0x02) { 2067c33e3c4SRicky Wu rtsx_pci_read_register(pcr, RTS525A_BIOS_CFG, &val); 2077c33e3c4SRicky Wu if (val & RTS525A_LOAD_BIOS_FLAG) { 2087c33e3c4SRicky Wu rtsx_pci_write_register(pcr, RTS525A_BIOS_CFG, 2097c33e3c4SRicky Wu RTS525A_LOAD_BIOS_FLAG, RTS525A_CLEAR_BIOS_FLAG); 2107c33e3c4SRicky Wu 2117c33e3c4SRicky Wu rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL, 2127c33e3c4SRicky Wu REG_EFUSE_POWER_MASK, REG_EFUSE_POWERON); 2137c33e3c4SRicky Wu 2147c33e3c4SRicky Wu pcr_dbg(pcr, "Power ON efuse!"); 2157c33e3c4SRicky Wu mdelay(1); 2167c33e3c4SRicky Wu rts52xa_save_content_from_efuse(pcr); 2177c33e3c4SRicky Wu } else { 2187c33e3c4SRicky Wu rtsx_pci_read_register(pcr, RTS524A_PME_FORCE_CTL, &val); 2197c33e3c4SRicky Wu if (!(val & 0x08)) 2207c33e3c4SRicky Wu rts52xa_save_content_from_efuse(pcr); 2217c33e3c4SRicky Wu } 2227c33e3c4SRicky Wu } else { 2237c33e3c4SRicky Wu pcr_dbg(pcr, "Load from autoload"); 2247c33e3c4SRicky Wu rtsx_pci_write_register(pcr, 0xFF00, 0xFF, 0x80); 2257c33e3c4SRicky Wu rtsx_pci_write_register(pcr, 0xFF04, 0xFF, (u8)PCI_VID(pcr)); 2267c33e3c4SRicky Wu rtsx_pci_write_register(pcr, 0xFF05, 0xFF, (u8)(PCI_VID(pcr) >> 8)); 2277c33e3c4SRicky Wu rtsx_pci_write_register(pcr, 0xFF06, 0xFF, (u8)PCI_PID(pcr)); 2287c33e3c4SRicky Wu rtsx_pci_write_register(pcr, 0xFF07, 0xFF, (u8)(PCI_PID(pcr) >> 8)); 2297c33e3c4SRicky Wu } 2307c33e3c4SRicky Wu } 2317c33e3c4SRicky Wu 232e455b69dSRui Feng static int rts5249_extra_init_hw(struct rtsx_pcr *pcr) 233e455b69dSRui Feng { 234e455b69dSRui Feng struct rtsx_cr_option *option = &(pcr->option); 235e455b69dSRui Feng 236e455b69dSRui Feng rts5249_init_from_cfg(pcr); 237e455b69dSRui Feng 238e455b69dSRui Feng rtsx_pci_init_cmd(pcr); 239e455b69dSRui Feng 2407c33e3c4SRicky Wu if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A)) 2417c33e3c4SRicky Wu rts52xa_save_content_to_autoload_space(pcr); 2427c33e3c4SRicky Wu 243e455b69dSRui Feng /* Rest L1SUB Config */ 244e455b69dSRui Feng rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, L1SUB_CONFIG3, 0xFF, 0x00); 245e455b69dSRui Feng /* Configure GPIO as output */ 246e455b69dSRui Feng rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, GPIO_CTL, 0x02, 0x02); 247e455b69dSRui Feng /* Reset ASPM state to default value */ 248e455b69dSRui Feng rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, ASPM_FORCE_CTL, 0x3F, 0); 249e455b69dSRui Feng /* Switch LDO3318 source from DV33 to card_3v3 */ 250e455b69dSRui Feng rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x00); 251e455b69dSRui Feng rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x01); 252e455b69dSRui Feng /* LED shine disabled, set initial shine cycle period */ 253e455b69dSRui Feng rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OLT_LED_CTL, 0x0F, 0x02); 254e455b69dSRui Feng /* Configure driving */ 255e455b69dSRui Feng rts5249_fill_driving(pcr, OUTPUT_3V3); 256e455b69dSRui Feng if (pcr->flags & PCR_REVERSE_SOCKET) 257e455b69dSRui Feng rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0xB0, 0xB0); 258e455b69dSRui Feng else 259e455b69dSRui Feng rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0xB0, 0x80); 260e455b69dSRui Feng 2617c33e3c4SRicky Wu rtsx_pci_send_cmd(pcr, CMD_TIMEOUT_DEF); 2627c33e3c4SRicky Wu 26371732e24SKai-Heng Feng if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A)) { 2647c33e3c4SRicky Wu rtsx_pci_write_register(pcr, REG_VREF, PWD_SUSPND_EN, PWD_SUSPND_EN); 26571732e24SKai-Heng Feng rtsx_pci_write_register(pcr, RTS524A_AUTOLOAD_CFG1, 26671732e24SKai-Heng Feng CD_RESUME_EN_MASK, CD_RESUME_EN_MASK); 26771732e24SKai-Heng Feng } 2685b4258f6SRicky Wu 2695b4258f6SRicky Wu if (pcr->rtd3_en) { 2705b4258f6SRicky Wu if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A)) { 2715b4258f6SRicky Wu rtsx_pci_write_register(pcr, RTS524A_PM_CTRL3, 0x01, 0x01); 2725b4258f6SRicky Wu rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL, 0x30, 0x30); 2735b4258f6SRicky Wu } else { 2745b4258f6SRicky Wu rtsx_pci_write_register(pcr, PM_CTRL3, 0x01, 0x01); 2755b4258f6SRicky Wu rtsx_pci_write_register(pcr, PME_FORCE_CTL, 0xFF, 0x33); 2765b4258f6SRicky Wu } 2775b4258f6SRicky Wu } else { 2785b4258f6SRicky Wu if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A)) { 2797c33e3c4SRicky Wu rtsx_pci_write_register(pcr, RTS524A_PM_CTRL3, 0x01, 0x00); 2807c33e3c4SRicky Wu rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL, 0x30, 0x20); 2817c33e3c4SRicky Wu } else { 2827c33e3c4SRicky Wu rtsx_pci_write_register(pcr, PME_FORCE_CTL, 0xFF, 0x30); 2837c33e3c4SRicky Wu rtsx_pci_write_register(pcr, PM_CTRL3, 0x01, 0x00); 2847c33e3c4SRicky Wu } 2855b4258f6SRicky Wu } 2865b4258f6SRicky Wu 287*0e4cac55SRicky WU 288e455b69dSRui Feng /* 289e455b69dSRui Feng * If u_force_clkreq_0 is enabled, CLKREQ# PIN will be forced 290e455b69dSRui Feng * to drive low, and we forcibly request clock. 291e455b69dSRui Feng */ 292*0e4cac55SRicky WU if (option->force_clkreq_0) 2937c33e3c4SRicky Wu rtsx_pci_write_register(pcr, PETXCFG, 294e455b69dSRui Feng FORCE_CLKREQ_DELINK_MASK, FORCE_CLKREQ_LOW); 295e455b69dSRui Feng else 2967c33e3c4SRicky Wu rtsx_pci_write_register(pcr, PETXCFG, 297e455b69dSRui Feng FORCE_CLKREQ_DELINK_MASK, FORCE_CLKREQ_HIGH); 298e455b69dSRui Feng 2997c33e3c4SRicky Wu rtsx_pci_write_register(pcr, pcr->reg_pm_ctrl3, 0x10, 0x00); 3007c33e3c4SRicky Wu if (CHK_PCI_PID(pcr, PID_524A) || CHK_PCI_PID(pcr, PID_525A)) { 3017c33e3c4SRicky Wu rtsx_pci_write_register(pcr, RTS524A_PME_FORCE_CTL, 3027c33e3c4SRicky Wu REG_EFUSE_POWER_MASK, REG_EFUSE_POWEROFF); 3037c33e3c4SRicky Wu pcr_dbg(pcr, "Power OFF efuse!"); 3047c33e3c4SRicky Wu } 3057c33e3c4SRicky Wu 3067c33e3c4SRicky Wu return 0; 307e455b69dSRui Feng } 308e455b69dSRui Feng 309e455b69dSRui Feng static int rts5249_optimize_phy(struct rtsx_pcr *pcr) 310e455b69dSRui Feng { 311e455b69dSRui Feng int err; 312e455b69dSRui Feng 313e455b69dSRui Feng err = rtsx_pci_write_register(pcr, PM_CTRL3, D3_DELINK_MODE_EN, 0x00); 314e455b69dSRui Feng if (err < 0) 315e455b69dSRui Feng return err; 316e455b69dSRui Feng 317e455b69dSRui Feng err = rtsx_pci_write_phy_register(pcr, PHY_REV, 318e455b69dSRui Feng PHY_REV_RESV | PHY_REV_RXIDLE_LATCHED | 319e455b69dSRui Feng PHY_REV_P1_EN | PHY_REV_RXIDLE_EN | 320e455b69dSRui Feng PHY_REV_CLKREQ_TX_EN | PHY_REV_RX_PWST | 321e455b69dSRui Feng PHY_REV_CLKREQ_DT_1_0 | PHY_REV_STOP_CLKRD | 322e455b69dSRui Feng PHY_REV_STOP_CLKWR); 323e455b69dSRui Feng if (err < 0) 324e455b69dSRui Feng return err; 325e455b69dSRui Feng 326e455b69dSRui Feng msleep(1); 327e455b69dSRui Feng 328e455b69dSRui Feng err = rtsx_pci_write_phy_register(pcr, PHY_BPCR, 329e455b69dSRui Feng PHY_BPCR_IBRXSEL | PHY_BPCR_IBTXSEL | 330e455b69dSRui Feng PHY_BPCR_IB_FILTER | PHY_BPCR_CMIRROR_EN); 331e455b69dSRui Feng if (err < 0) 332e455b69dSRui Feng return err; 333e455b69dSRui Feng 334e455b69dSRui Feng err = rtsx_pci_write_phy_register(pcr, PHY_PCR, 335e455b69dSRui Feng PHY_PCR_FORCE_CODE | PHY_PCR_OOBS_CALI_50 | 336e455b69dSRui Feng PHY_PCR_OOBS_VCM_08 | PHY_PCR_OOBS_SEN_90 | 337e455b69dSRui Feng PHY_PCR_RSSI_EN | PHY_PCR_RX10K); 338e455b69dSRui Feng if (err < 0) 339e455b69dSRui Feng return err; 340e455b69dSRui Feng 341e455b69dSRui Feng err = rtsx_pci_write_phy_register(pcr, PHY_RCR2, 342e455b69dSRui Feng PHY_RCR2_EMPHASE_EN | PHY_RCR2_NADJR | 343e455b69dSRui Feng PHY_RCR2_CDR_SR_2 | PHY_RCR2_FREQSEL_12 | 344e455b69dSRui Feng PHY_RCR2_CDR_SC_12P | PHY_RCR2_CALIB_LATE); 345e455b69dSRui Feng if (err < 0) 346e455b69dSRui Feng return err; 347e455b69dSRui Feng 348e455b69dSRui Feng err = rtsx_pci_write_phy_register(pcr, PHY_FLD4, 349e455b69dSRui Feng PHY_FLD4_FLDEN_SEL | PHY_FLD4_REQ_REF | 350e455b69dSRui Feng PHY_FLD4_RXAMP_OFF | PHY_FLD4_REQ_ADDA | 351e455b69dSRui Feng PHY_FLD4_BER_COUNT | PHY_FLD4_BER_TIMER | 352e455b69dSRui Feng PHY_FLD4_BER_CHK_EN); 353e455b69dSRui Feng if (err < 0) 354e455b69dSRui Feng return err; 355e455b69dSRui Feng err = rtsx_pci_write_phy_register(pcr, PHY_RDR, 356e455b69dSRui Feng PHY_RDR_RXDSEL_1_9 | PHY_SSC_AUTO_PWD); 357e455b69dSRui Feng if (err < 0) 358e455b69dSRui Feng return err; 359e455b69dSRui Feng err = rtsx_pci_write_phy_register(pcr, PHY_RCR1, 360e455b69dSRui Feng PHY_RCR1_ADP_TIME_4 | PHY_RCR1_VCO_COARSE); 361e455b69dSRui Feng if (err < 0) 362e455b69dSRui Feng return err; 363e455b69dSRui Feng err = rtsx_pci_write_phy_register(pcr, PHY_FLD3, 364e455b69dSRui Feng PHY_FLD3_TIMER_4 | PHY_FLD3_TIMER_6 | 365e455b69dSRui Feng PHY_FLD3_RXDELINK); 366e455b69dSRui Feng if (err < 0) 367e455b69dSRui Feng return err; 368e455b69dSRui Feng 369e455b69dSRui Feng return rtsx_pci_write_phy_register(pcr, PHY_TUNE, 370e455b69dSRui Feng PHY_TUNE_TUNEREF_1_0 | PHY_TUNE_VBGSEL_1252 | 371e455b69dSRui Feng PHY_TUNE_SDBUS_33 | PHY_TUNE_TUNED18 | 372e455b69dSRui Feng PHY_TUNE_TUNED12 | PHY_TUNE_TUNEA12); 373e455b69dSRui Feng } 374e455b69dSRui Feng 375e455b69dSRui Feng static int rtsx_base_turn_on_led(struct rtsx_pcr *pcr) 376e455b69dSRui Feng { 377e455b69dSRui Feng return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x02); 378e455b69dSRui Feng } 379e455b69dSRui Feng 380e455b69dSRui Feng static int rtsx_base_turn_off_led(struct rtsx_pcr *pcr) 381e455b69dSRui Feng { 382e455b69dSRui Feng return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x00); 383e455b69dSRui Feng } 384e455b69dSRui Feng 385e455b69dSRui Feng static int rtsx_base_enable_auto_blink(struct rtsx_pcr *pcr) 386e455b69dSRui Feng { 387e455b69dSRui Feng return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x08); 388e455b69dSRui Feng } 389e455b69dSRui Feng 390e455b69dSRui Feng static int rtsx_base_disable_auto_blink(struct rtsx_pcr *pcr) 391e455b69dSRui Feng { 392e455b69dSRui Feng return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x00); 393e455b69dSRui Feng } 394e455b69dSRui Feng 395e455b69dSRui Feng static int rtsx_base_card_power_on(struct rtsx_pcr *pcr, int card) 396e455b69dSRui Feng { 397e455b69dSRui Feng int err; 398bede03a5SRickyWu struct rtsx_cr_option *option = &pcr->option; 399bede03a5SRickyWu 400bede03a5SRickyWu if (option->ocp_en) 401bede03a5SRickyWu rtsx_pci_enable_ocp(pcr); 402e455b69dSRui Feng 403e455b69dSRui Feng rtsx_pci_init_cmd(pcr); 404e455b69dSRui Feng rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL, 405e455b69dSRui Feng SD_POWER_MASK, SD_VCC_PARTIAL_POWER_ON); 406e455b69dSRui Feng rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL, 407e455b69dSRui Feng LDO3318_PWR_MASK, 0x02); 408e455b69dSRui Feng err = rtsx_pci_send_cmd(pcr, 100); 409e455b69dSRui Feng if (err < 0) 410e455b69dSRui Feng return err; 411e455b69dSRui Feng 412e455b69dSRui Feng msleep(5); 413e455b69dSRui Feng 414e455b69dSRui Feng rtsx_pci_init_cmd(pcr); 415e455b69dSRui Feng rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL, 416e455b69dSRui Feng SD_POWER_MASK, SD_VCC_POWER_ON); 417e455b69dSRui Feng rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL, 418e455b69dSRui Feng LDO3318_PWR_MASK, 0x06); 419e455b69dSRui Feng return rtsx_pci_send_cmd(pcr, 100); 420e455b69dSRui Feng } 421e455b69dSRui Feng 422e455b69dSRui Feng static int rtsx_base_card_power_off(struct rtsx_pcr *pcr, int card) 423e455b69dSRui Feng { 424bede03a5SRickyWu struct rtsx_cr_option *option = &pcr->option; 425bede03a5SRickyWu 426bede03a5SRickyWu if (option->ocp_en) 427bede03a5SRickyWu rtsx_pci_disable_ocp(pcr); 428bede03a5SRickyWu 429bede03a5SRickyWu rtsx_pci_write_register(pcr, CARD_PWR_CTL, SD_POWER_MASK, SD_POWER_OFF); 430bede03a5SRickyWu 431bede03a5SRickyWu rtsx_pci_write_register(pcr, PWR_GATE_CTRL, LDO3318_PWR_MASK, 0x00); 432bede03a5SRickyWu return 0; 433e455b69dSRui Feng } 434e455b69dSRui Feng 435e455b69dSRui Feng static int rtsx_base_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage) 436e455b69dSRui Feng { 437e455b69dSRui Feng int err; 438e455b69dSRui Feng u16 append; 439e455b69dSRui Feng 440e455b69dSRui Feng switch (voltage) { 441e455b69dSRui Feng case OUTPUT_3V3: 442e455b69dSRui Feng err = rtsx_pci_update_phy(pcr, PHY_TUNE, PHY_TUNE_VOLTAGE_MASK, 443e455b69dSRui Feng PHY_TUNE_VOLTAGE_3V3); 444e455b69dSRui Feng if (err < 0) 445e455b69dSRui Feng return err; 446e455b69dSRui Feng break; 447e455b69dSRui Feng case OUTPUT_1V8: 448e455b69dSRui Feng append = PHY_TUNE_D18_1V8; 449e455b69dSRui Feng if (CHK_PCI_PID(pcr, 0x5249)) { 450e455b69dSRui Feng err = rtsx_pci_update_phy(pcr, PHY_BACR, 451e455b69dSRui Feng PHY_BACR_BASIC_MASK, 0); 452e455b69dSRui Feng if (err < 0) 453e455b69dSRui Feng return err; 454e455b69dSRui Feng append = PHY_TUNE_D18_1V7; 455e455b69dSRui Feng } 456e455b69dSRui Feng 457e455b69dSRui Feng err = rtsx_pci_update_phy(pcr, PHY_TUNE, PHY_TUNE_VOLTAGE_MASK, 458e455b69dSRui Feng append); 459e455b69dSRui Feng if (err < 0) 460e455b69dSRui Feng return err; 461e455b69dSRui Feng break; 462e455b69dSRui Feng default: 463e455b69dSRui Feng pcr_dbg(pcr, "unknown output voltage %d\n", voltage); 464e455b69dSRui Feng return -EINVAL; 465e455b69dSRui Feng } 466e455b69dSRui Feng 467e455b69dSRui Feng /* set pad drive */ 468e455b69dSRui Feng rtsx_pci_init_cmd(pcr); 469e455b69dSRui Feng rts5249_fill_driving(pcr, voltage); 470e455b69dSRui Feng return rtsx_pci_send_cmd(pcr, 100); 471e455b69dSRui Feng } 472e455b69dSRui Feng 473e455b69dSRui Feng static const struct pcr_ops rts5249_pcr_ops = { 474e455b69dSRui Feng .fetch_vendor_settings = rtsx_base_fetch_vendor_settings, 475e455b69dSRui Feng .extra_init_hw = rts5249_extra_init_hw, 476e455b69dSRui Feng .optimize_phy = rts5249_optimize_phy, 477e455b69dSRui Feng .turn_on_led = rtsx_base_turn_on_led, 478e455b69dSRui Feng .turn_off_led = rtsx_base_turn_off_led, 479e455b69dSRui Feng .enable_auto_blink = rtsx_base_enable_auto_blink, 480e455b69dSRui Feng .disable_auto_blink = rtsx_base_disable_auto_blink, 481e455b69dSRui Feng .card_power_on = rtsx_base_card_power_on, 482e455b69dSRui Feng .card_power_off = rtsx_base_card_power_off, 483e455b69dSRui Feng .switch_output_voltage = rtsx_base_switch_output_voltage, 484e455b69dSRui Feng }; 485e455b69dSRui Feng 486e455b69dSRui Feng /* SD Pull Control Enable: 487e455b69dSRui Feng * SD_DAT[3:0] ==> pull up 488e455b69dSRui Feng * SD_CD ==> pull up 489e455b69dSRui Feng * SD_WP ==> pull up 490e455b69dSRui Feng * SD_CMD ==> pull up 491e455b69dSRui Feng * SD_CLK ==> pull down 492e455b69dSRui Feng */ 493e455b69dSRui Feng static const u32 rts5249_sd_pull_ctl_enable_tbl[] = { 494e455b69dSRui Feng RTSX_REG_PAIR(CARD_PULL_CTL1, 0x66), 495e455b69dSRui Feng RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA), 496e455b69dSRui Feng RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9), 497e455b69dSRui Feng RTSX_REG_PAIR(CARD_PULL_CTL4, 0xAA), 498e455b69dSRui Feng 0, 499e455b69dSRui Feng }; 500e455b69dSRui Feng 501e455b69dSRui Feng /* SD Pull Control Disable: 502e455b69dSRui Feng * SD_DAT[3:0] ==> pull down 503e455b69dSRui Feng * SD_CD ==> pull up 504e455b69dSRui Feng * SD_WP ==> pull down 505e455b69dSRui Feng * SD_CMD ==> pull down 506e455b69dSRui Feng * SD_CLK ==> pull down 507e455b69dSRui Feng */ 508e455b69dSRui Feng static const u32 rts5249_sd_pull_ctl_disable_tbl[] = { 509e455b69dSRui Feng RTSX_REG_PAIR(CARD_PULL_CTL1, 0x66), 510e455b69dSRui Feng RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55), 511e455b69dSRui Feng RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5), 512e455b69dSRui Feng RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55), 513e455b69dSRui Feng 0, 514e455b69dSRui Feng }; 515e455b69dSRui Feng 516e455b69dSRui Feng /* MS Pull Control Enable: 517e455b69dSRui Feng * MS CD ==> pull up 518e455b69dSRui Feng * others ==> pull down 519e455b69dSRui Feng */ 520e455b69dSRui Feng static const u32 rts5249_ms_pull_ctl_enable_tbl[] = { 521e455b69dSRui Feng RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55), 522e455b69dSRui Feng RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55), 523e455b69dSRui Feng RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15), 524e455b69dSRui Feng 0, 525e455b69dSRui Feng }; 526e455b69dSRui Feng 527e455b69dSRui Feng /* MS Pull Control Disable: 528e455b69dSRui Feng * MS CD ==> pull up 529e455b69dSRui Feng * others ==> pull down 530e455b69dSRui Feng */ 531e455b69dSRui Feng static const u32 rts5249_ms_pull_ctl_disable_tbl[] = { 532e455b69dSRui Feng RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55), 533e455b69dSRui Feng RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55), 534e455b69dSRui Feng RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15), 535e455b69dSRui Feng 0, 536e455b69dSRui Feng }; 537e455b69dSRui Feng 538e455b69dSRui Feng void rts5249_init_params(struct rtsx_pcr *pcr) 539e455b69dSRui Feng { 540e455b69dSRui Feng struct rtsx_cr_option *option = &(pcr->option); 541e455b69dSRui Feng 542e455b69dSRui Feng pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104; 543e455b69dSRui Feng pcr->num_slots = 2; 544e455b69dSRui Feng pcr->ops = &rts5249_pcr_ops; 545e455b69dSRui Feng 546e455b69dSRui Feng pcr->flags = 0; 547e455b69dSRui Feng pcr->card_drive_sel = RTSX_CARD_DRIVE_DEFAULT; 548e455b69dSRui Feng pcr->sd30_drive_sel_1v8 = CFG_DRIVER_TYPE_B; 549e455b69dSRui Feng pcr->sd30_drive_sel_3v3 = CFG_DRIVER_TYPE_B; 550e455b69dSRui Feng pcr->aspm_en = ASPM_L1_EN; 5513df4fce7SRicky Wu pcr->aspm_mode = ASPM_MODE_CFG; 552e455b69dSRui Feng pcr->tx_initial_phase = SET_CLOCK_PHASE(1, 29, 16); 553e455b69dSRui Feng pcr->rx_initial_phase = SET_CLOCK_PHASE(24, 6, 5); 554e455b69dSRui Feng 555e455b69dSRui Feng pcr->ic_version = rts5249_get_ic_version(pcr); 556e455b69dSRui Feng pcr->sd_pull_ctl_enable_tbl = rts5249_sd_pull_ctl_enable_tbl; 557e455b69dSRui Feng pcr->sd_pull_ctl_disable_tbl = rts5249_sd_pull_ctl_disable_tbl; 558e455b69dSRui Feng pcr->ms_pull_ctl_enable_tbl = rts5249_ms_pull_ctl_enable_tbl; 559e455b69dSRui Feng pcr->ms_pull_ctl_disable_tbl = rts5249_ms_pull_ctl_disable_tbl; 560e455b69dSRui Feng 561e455b69dSRui Feng pcr->reg_pm_ctrl3 = PM_CTRL3; 562e455b69dSRui Feng 563e455b69dSRui Feng option->dev_flags = (LTR_L1SS_PWR_GATE_CHECK_CARD_EN 564e455b69dSRui Feng | LTR_L1SS_PWR_GATE_EN); 565e455b69dSRui Feng option->ltr_en = true; 566e455b69dSRui Feng 567e455b69dSRui Feng /* Init latency of active, idle, L1OFF to 60us, 300us, 3ms */ 568e455b69dSRui Feng option->ltr_active_latency = LTR_ACTIVE_LATENCY_DEF; 569e455b69dSRui Feng option->ltr_idle_latency = LTR_IDLE_LATENCY_DEF; 570e455b69dSRui Feng option->ltr_l1off_latency = LTR_L1OFF_LATENCY_DEF; 571e455b69dSRui Feng option->l1_snooze_delay = L1_SNOOZE_DELAY_DEF; 572e455b69dSRui Feng option->ltr_l1off_sspwrgate = LTR_L1OFF_SSPWRGATE_5249_DEF; 573e455b69dSRui Feng option->ltr_l1off_snooze_sspwrgate = 574e455b69dSRui Feng LTR_L1OFF_SNOOZE_SSPWRGATE_5249_DEF; 575e455b69dSRui Feng } 576e455b69dSRui Feng 577e455b69dSRui Feng static int rts524a_write_phy(struct rtsx_pcr *pcr, u8 addr, u16 val) 578e455b69dSRui Feng { 579e455b69dSRui Feng addr = addr & 0x80 ? (addr & 0x7F) | 0x40 : addr; 580e455b69dSRui Feng 581e455b69dSRui Feng return __rtsx_pci_write_phy_register(pcr, addr, val); 582e455b69dSRui Feng } 583e455b69dSRui Feng 584e455b69dSRui Feng static int rts524a_read_phy(struct rtsx_pcr *pcr, u8 addr, u16 *val) 585e455b69dSRui Feng { 586e455b69dSRui Feng addr = addr & 0x80 ? (addr & 0x7F) | 0x40 : addr; 587e455b69dSRui Feng 588e455b69dSRui Feng return __rtsx_pci_read_phy_register(pcr, addr, val); 589e455b69dSRui Feng } 590e455b69dSRui Feng 591e455b69dSRui Feng static int rts524a_optimize_phy(struct rtsx_pcr *pcr) 592e455b69dSRui Feng { 593e455b69dSRui Feng int err; 594e455b69dSRui Feng 595e455b69dSRui Feng err = rtsx_pci_write_register(pcr, RTS524A_PM_CTRL3, 596e455b69dSRui Feng D3_DELINK_MODE_EN, 0x00); 597e455b69dSRui Feng if (err < 0) 598e455b69dSRui Feng return err; 599e455b69dSRui Feng 600e455b69dSRui Feng rtsx_pci_write_phy_register(pcr, PHY_PCR, 601e455b69dSRui Feng PHY_PCR_FORCE_CODE | PHY_PCR_OOBS_CALI_50 | 602e455b69dSRui Feng PHY_PCR_OOBS_VCM_08 | PHY_PCR_OOBS_SEN_90 | PHY_PCR_RSSI_EN); 603e455b69dSRui Feng rtsx_pci_write_phy_register(pcr, PHY_SSCCR3, 604e455b69dSRui Feng PHY_SSCCR3_STEP_IN | PHY_SSCCR3_CHECK_DELAY); 605e455b69dSRui Feng 606e455b69dSRui Feng if (is_version(pcr, 0x524A, IC_VER_A)) { 607e455b69dSRui Feng rtsx_pci_write_phy_register(pcr, PHY_SSCCR3, 608e455b69dSRui Feng PHY_SSCCR3_STEP_IN | PHY_SSCCR3_CHECK_DELAY); 609e455b69dSRui Feng rtsx_pci_write_phy_register(pcr, PHY_SSCCR2, 610e455b69dSRui Feng PHY_SSCCR2_PLL_NCODE | PHY_SSCCR2_TIME0 | 611e455b69dSRui Feng PHY_SSCCR2_TIME2_WIDTH); 612e455b69dSRui Feng rtsx_pci_write_phy_register(pcr, PHY_ANA1A, 613e455b69dSRui Feng PHY_ANA1A_TXR_LOOPBACK | PHY_ANA1A_RXT_BIST | 614e455b69dSRui Feng PHY_ANA1A_TXR_BIST | PHY_ANA1A_REV); 615e455b69dSRui Feng rtsx_pci_write_phy_register(pcr, PHY_ANA1D, 616e455b69dSRui Feng PHY_ANA1D_DEBUG_ADDR); 617e455b69dSRui Feng rtsx_pci_write_phy_register(pcr, PHY_DIG1E, 618e455b69dSRui Feng PHY_DIG1E_REV | PHY_DIG1E_D0_X_D1 | 619e455b69dSRui Feng PHY_DIG1E_RX_ON_HOST | PHY_DIG1E_RCLK_REF_HOST | 620e455b69dSRui Feng PHY_DIG1E_RCLK_TX_EN_KEEP | 621e455b69dSRui Feng PHY_DIG1E_RCLK_TX_TERM_KEEP | 622e455b69dSRui Feng PHY_DIG1E_RCLK_RX_EIDLE_ON | PHY_DIG1E_TX_TERM_KEEP | 623e455b69dSRui Feng PHY_DIG1E_RX_TERM_KEEP | PHY_DIG1E_TX_EN_KEEP | 624e455b69dSRui Feng PHY_DIG1E_RX_EN_KEEP); 625e455b69dSRui Feng } 626e455b69dSRui Feng 627e455b69dSRui Feng rtsx_pci_write_phy_register(pcr, PHY_ANA08, 628e455b69dSRui Feng PHY_ANA08_RX_EQ_DCGAIN | PHY_ANA08_SEL_RX_EN | 629e455b69dSRui Feng PHY_ANA08_RX_EQ_VAL | PHY_ANA08_SCP | PHY_ANA08_SEL_IPI); 630e455b69dSRui Feng 631e455b69dSRui Feng return 0; 632e455b69dSRui Feng } 633e455b69dSRui Feng 634e455b69dSRui Feng static int rts524a_extra_init_hw(struct rtsx_pcr *pcr) 635e455b69dSRui Feng { 636e455b69dSRui Feng rts5249_extra_init_hw(pcr); 637e455b69dSRui Feng 638e455b69dSRui Feng rtsx_pci_write_register(pcr, FUNC_FORCE_CTL, 639e455b69dSRui Feng FORCE_ASPM_L1_EN, FORCE_ASPM_L1_EN); 640e455b69dSRui Feng rtsx_pci_write_register(pcr, PM_EVENT_DEBUG, PME_DEBUG_0, PME_DEBUG_0); 641e455b69dSRui Feng rtsx_pci_write_register(pcr, LDO_VCC_CFG1, LDO_VCC_LMT_EN, 642e455b69dSRui Feng LDO_VCC_LMT_EN); 643e455b69dSRui Feng rtsx_pci_write_register(pcr, PCLK_CTL, PCLK_MODE_SEL, PCLK_MODE_SEL); 644e455b69dSRui Feng if (is_version(pcr, 0x524A, IC_VER_A)) { 645e455b69dSRui Feng rtsx_pci_write_register(pcr, LDO_DV18_CFG, 646e455b69dSRui Feng LDO_DV18_SR_MASK, LDO_DV18_SR_DF); 647e455b69dSRui Feng rtsx_pci_write_register(pcr, LDO_VCC_CFG1, 648e455b69dSRui Feng LDO_VCC_REF_TUNE_MASK, LDO_VCC_REF_1V2); 649e455b69dSRui Feng rtsx_pci_write_register(pcr, LDO_VIO_CFG, 650e455b69dSRui Feng LDO_VIO_REF_TUNE_MASK, LDO_VIO_REF_1V2); 651e455b69dSRui Feng rtsx_pci_write_register(pcr, LDO_VIO_CFG, 652e455b69dSRui Feng LDO_VIO_SR_MASK, LDO_VIO_SR_DF); 653e455b69dSRui Feng rtsx_pci_write_register(pcr, LDO_DV12S_CFG, 654e455b69dSRui Feng LDO_REF12_TUNE_MASK, LDO_REF12_TUNE_DF); 655e455b69dSRui Feng rtsx_pci_write_register(pcr, SD40_LDO_CTL1, 656e455b69dSRui Feng SD40_VIO_TUNE_MASK, SD40_VIO_TUNE_1V7); 657e455b69dSRui Feng } 658e455b69dSRui Feng 659e455b69dSRui Feng return 0; 660e455b69dSRui Feng } 661e455b69dSRui Feng 662e455b69dSRui Feng static void rts5250_set_l1off_cfg_sub_d0(struct rtsx_pcr *pcr, int active) 663e455b69dSRui Feng { 664e455b69dSRui Feng struct rtsx_cr_option *option = &(pcr->option); 665e455b69dSRui Feng 666e455b69dSRui Feng u32 interrupt = rtsx_pci_readl(pcr, RTSX_BIPR); 667e455b69dSRui Feng int card_exist = (interrupt & SD_EXIST) | (interrupt & MS_EXIST); 668e455b69dSRui Feng int aspm_L1_1, aspm_L1_2; 669e455b69dSRui Feng u8 val = 0; 670e455b69dSRui Feng 671e455b69dSRui Feng aspm_L1_1 = rtsx_check_dev_flag(pcr, ASPM_L1_1_EN); 672e455b69dSRui Feng aspm_L1_2 = rtsx_check_dev_flag(pcr, ASPM_L1_2_EN); 673e455b69dSRui Feng 674e455b69dSRui Feng if (active) { 675e455b69dSRui Feng /* Run, latency: 60us */ 676e455b69dSRui Feng if (aspm_L1_1) 677e455b69dSRui Feng val = option->ltr_l1off_snooze_sspwrgate; 678e455b69dSRui Feng } else { 679e455b69dSRui Feng /* L1off, latency: 300us */ 680e455b69dSRui Feng if (aspm_L1_2) 681e455b69dSRui Feng val = option->ltr_l1off_sspwrgate; 682e455b69dSRui Feng } 683e455b69dSRui Feng 684e455b69dSRui Feng if (aspm_L1_1 || aspm_L1_2) { 685e455b69dSRui Feng if (rtsx_check_dev_flag(pcr, 686e455b69dSRui Feng LTR_L1SS_PWR_GATE_CHECK_CARD_EN)) { 687e455b69dSRui Feng if (card_exist) 688e455b69dSRui Feng val &= ~L1OFF_MBIAS2_EN_5250; 689e455b69dSRui Feng else 690e455b69dSRui Feng val |= L1OFF_MBIAS2_EN_5250; 691e455b69dSRui Feng } 692e455b69dSRui Feng } 693e455b69dSRui Feng rtsx_set_l1off_sub(pcr, val); 694e455b69dSRui Feng } 695e455b69dSRui Feng 696e455b69dSRui Feng static const struct pcr_ops rts524a_pcr_ops = { 697e455b69dSRui Feng .write_phy = rts524a_write_phy, 698e455b69dSRui Feng .read_phy = rts524a_read_phy, 699e455b69dSRui Feng .fetch_vendor_settings = rtsx_base_fetch_vendor_settings, 700e455b69dSRui Feng .extra_init_hw = rts524a_extra_init_hw, 701e455b69dSRui Feng .optimize_phy = rts524a_optimize_phy, 702e455b69dSRui Feng .turn_on_led = rtsx_base_turn_on_led, 703e455b69dSRui Feng .turn_off_led = rtsx_base_turn_off_led, 704e455b69dSRui Feng .enable_auto_blink = rtsx_base_enable_auto_blink, 705e455b69dSRui Feng .disable_auto_blink = rtsx_base_disable_auto_blink, 706e455b69dSRui Feng .card_power_on = rtsx_base_card_power_on, 707e455b69dSRui Feng .card_power_off = rtsx_base_card_power_off, 708e455b69dSRui Feng .switch_output_voltage = rtsx_base_switch_output_voltage, 70971732e24SKai-Heng Feng .force_power_down = rts52xa_force_power_down, 710e455b69dSRui Feng .set_l1off_cfg_sub_d0 = rts5250_set_l1off_cfg_sub_d0, 711e455b69dSRui Feng }; 712e455b69dSRui Feng 713e455b69dSRui Feng void rts524a_init_params(struct rtsx_pcr *pcr) 714e455b69dSRui Feng { 715e455b69dSRui Feng rts5249_init_params(pcr); 7163df4fce7SRicky Wu pcr->aspm_mode = ASPM_MODE_REG; 7174686392cSRicky Wu pcr->tx_initial_phase = SET_CLOCK_PHASE(27, 29, 11); 718e455b69dSRui Feng pcr->option.ltr_l1off_sspwrgate = LTR_L1OFF_SSPWRGATE_5250_DEF; 719e455b69dSRui Feng pcr->option.ltr_l1off_snooze_sspwrgate = 720e455b69dSRui Feng LTR_L1OFF_SNOOZE_SSPWRGATE_5250_DEF; 721e455b69dSRui Feng 722e455b69dSRui Feng pcr->reg_pm_ctrl3 = RTS524A_PM_CTRL3; 723e455b69dSRui Feng pcr->ops = &rts524a_pcr_ops; 724bede03a5SRickyWu 725bede03a5SRickyWu pcr->option.ocp_en = 1; 726bede03a5SRickyWu if (pcr->option.ocp_en) 727bede03a5SRickyWu pcr->hw_param.interrupt_en |= SD_OC_INT_EN; 728bede03a5SRickyWu pcr->hw_param.ocp_glitch = SD_OCP_GLITCH_10M; 729bede03a5SRickyWu pcr->option.sd_800mA_ocp_thd = RTS524A_OCP_THD_800; 730bede03a5SRickyWu 731e455b69dSRui Feng } 732e455b69dSRui Feng 733e455b69dSRui Feng static int rts525a_card_power_on(struct rtsx_pcr *pcr, int card) 734e455b69dSRui Feng { 735e455b69dSRui Feng rtsx_pci_write_register(pcr, LDO_VCC_CFG1, 736e455b69dSRui Feng LDO_VCC_TUNE_MASK, LDO_VCC_3V3); 737e455b69dSRui Feng return rtsx_base_card_power_on(pcr, card); 738e455b69dSRui Feng } 739e455b69dSRui Feng 740e455b69dSRui Feng static int rts525a_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage) 741e455b69dSRui Feng { 742e455b69dSRui Feng switch (voltage) { 743e455b69dSRui Feng case OUTPUT_3V3: 744e455b69dSRui Feng rtsx_pci_write_register(pcr, LDO_CONFIG2, 745e455b69dSRui Feng LDO_D3318_MASK, LDO_D3318_33V); 746e455b69dSRui Feng rtsx_pci_write_register(pcr, SD_PAD_CTL, SD_IO_USING_1V8, 0); 747e455b69dSRui Feng break; 748e455b69dSRui Feng case OUTPUT_1V8: 749e455b69dSRui Feng rtsx_pci_write_register(pcr, LDO_CONFIG2, 750e455b69dSRui Feng LDO_D3318_MASK, LDO_D3318_18V); 751e455b69dSRui Feng rtsx_pci_write_register(pcr, SD_PAD_CTL, SD_IO_USING_1V8, 752e455b69dSRui Feng SD_IO_USING_1V8); 753e455b69dSRui Feng break; 754e455b69dSRui Feng default: 755e455b69dSRui Feng return -EINVAL; 756e455b69dSRui Feng } 757e455b69dSRui Feng 758e455b69dSRui Feng rtsx_pci_init_cmd(pcr); 759e455b69dSRui Feng rts5249_fill_driving(pcr, voltage); 760e455b69dSRui Feng return rtsx_pci_send_cmd(pcr, 100); 761e455b69dSRui Feng } 762e455b69dSRui Feng 763e455b69dSRui Feng static int rts525a_optimize_phy(struct rtsx_pcr *pcr) 764e455b69dSRui Feng { 765e455b69dSRui Feng int err; 766e455b69dSRui Feng 767e455b69dSRui Feng err = rtsx_pci_write_register(pcr, RTS524A_PM_CTRL3, 768e455b69dSRui Feng D3_DELINK_MODE_EN, 0x00); 769e455b69dSRui Feng if (err < 0) 770e455b69dSRui Feng return err; 771e455b69dSRui Feng 772e455b69dSRui Feng rtsx_pci_write_phy_register(pcr, _PHY_FLD0, 773e455b69dSRui Feng _PHY_FLD0_CLK_REQ_20C | _PHY_FLD0_RX_IDLE_EN | 774e455b69dSRui Feng _PHY_FLD0_BIT_ERR_RSTN | _PHY_FLD0_BER_COUNT | 775e455b69dSRui Feng _PHY_FLD0_BER_TIMER | _PHY_FLD0_CHECK_EN); 776e455b69dSRui Feng 777e455b69dSRui Feng rtsx_pci_write_phy_register(pcr, _PHY_ANA03, 778e455b69dSRui Feng _PHY_ANA03_TIMER_MAX | _PHY_ANA03_OOBS_DEB_EN | 779e455b69dSRui Feng _PHY_CMU_DEBUG_EN); 780e455b69dSRui Feng 781e455b69dSRui Feng if (is_version(pcr, 0x525A, IC_VER_A)) 782e455b69dSRui Feng rtsx_pci_write_phy_register(pcr, _PHY_REV0, 783e455b69dSRui Feng _PHY_REV0_FILTER_OUT | _PHY_REV0_CDR_BYPASS_PFD | 784e455b69dSRui Feng _PHY_REV0_CDR_RX_IDLE_BYPASS); 785e455b69dSRui Feng 786e455b69dSRui Feng return 0; 787e455b69dSRui Feng } 788e455b69dSRui Feng 789e455b69dSRui Feng static int rts525a_extra_init_hw(struct rtsx_pcr *pcr) 790e455b69dSRui Feng { 791e455b69dSRui Feng rts5249_extra_init_hw(pcr); 792e455b69dSRui Feng 7937c33e3c4SRicky Wu rtsx_pci_write_register(pcr, RTS5250_CLK_CFG3, RTS525A_CFG_MEM_PD, RTS525A_CFG_MEM_PD); 7947c33e3c4SRicky Wu 795e455b69dSRui Feng rtsx_pci_write_register(pcr, PCLK_CTL, PCLK_MODE_SEL, PCLK_MODE_SEL); 796e455b69dSRui Feng if (is_version(pcr, 0x525A, IC_VER_A)) { 797e455b69dSRui Feng rtsx_pci_write_register(pcr, L1SUB_CONFIG2, 798e455b69dSRui Feng L1SUB_AUTO_CFG, L1SUB_AUTO_CFG); 799e455b69dSRui Feng rtsx_pci_write_register(pcr, RREF_CFG, 800e455b69dSRui Feng RREF_VBGSEL_MASK, RREF_VBGSEL_1V25); 801e455b69dSRui Feng rtsx_pci_write_register(pcr, LDO_VIO_CFG, 802e455b69dSRui Feng LDO_VIO_TUNE_MASK, LDO_VIO_1V7); 803e455b69dSRui Feng rtsx_pci_write_register(pcr, LDO_DV12S_CFG, 804e455b69dSRui Feng LDO_D12_TUNE_MASK, LDO_D12_TUNE_DF); 805e455b69dSRui Feng rtsx_pci_write_register(pcr, LDO_AV12S_CFG, 806e455b69dSRui Feng LDO_AV12S_TUNE_MASK, LDO_AV12S_TUNE_DF); 807e455b69dSRui Feng rtsx_pci_write_register(pcr, LDO_VCC_CFG0, 808e455b69dSRui Feng LDO_VCC_LMTVTH_MASK, LDO_VCC_LMTVTH_2A); 809e455b69dSRui Feng rtsx_pci_write_register(pcr, OOBS_CONFIG, 810e455b69dSRui Feng OOBS_AUTOK_DIS | OOBS_VAL_MASK, 0x89); 811e455b69dSRui Feng } 812e455b69dSRui Feng 813e455b69dSRui Feng return 0; 814e455b69dSRui Feng } 815e455b69dSRui Feng 816e455b69dSRui Feng static const struct pcr_ops rts525a_pcr_ops = { 817e455b69dSRui Feng .fetch_vendor_settings = rtsx_base_fetch_vendor_settings, 818e455b69dSRui Feng .extra_init_hw = rts525a_extra_init_hw, 819e455b69dSRui Feng .optimize_phy = rts525a_optimize_phy, 820e455b69dSRui Feng .turn_on_led = rtsx_base_turn_on_led, 821e455b69dSRui Feng .turn_off_led = rtsx_base_turn_off_led, 822e455b69dSRui Feng .enable_auto_blink = rtsx_base_enable_auto_blink, 823e455b69dSRui Feng .disable_auto_blink = rtsx_base_disable_auto_blink, 824e455b69dSRui Feng .card_power_on = rts525a_card_power_on, 825e455b69dSRui Feng .card_power_off = rtsx_base_card_power_off, 826e455b69dSRui Feng .switch_output_voltage = rts525a_switch_output_voltage, 82771732e24SKai-Heng Feng .force_power_down = rts52xa_force_power_down, 828e455b69dSRui Feng .set_l1off_cfg_sub_d0 = rts5250_set_l1off_cfg_sub_d0, 829e455b69dSRui Feng }; 830e455b69dSRui Feng 831e455b69dSRui Feng void rts525a_init_params(struct rtsx_pcr *pcr) 832e455b69dSRui Feng { 833e455b69dSRui Feng rts5249_init_params(pcr); 8343df4fce7SRicky Wu pcr->aspm_mode = ASPM_MODE_REG; 8354686392cSRicky Wu pcr->tx_initial_phase = SET_CLOCK_PHASE(25, 29, 11); 836e455b69dSRui Feng pcr->option.ltr_l1off_sspwrgate = LTR_L1OFF_SSPWRGATE_5250_DEF; 837e455b69dSRui Feng pcr->option.ltr_l1off_snooze_sspwrgate = 838e455b69dSRui Feng LTR_L1OFF_SNOOZE_SSPWRGATE_5250_DEF; 839e455b69dSRui Feng 840e455b69dSRui Feng pcr->reg_pm_ctrl3 = RTS524A_PM_CTRL3; 841e455b69dSRui Feng pcr->ops = &rts525a_pcr_ops; 842bede03a5SRickyWu 843bede03a5SRickyWu pcr->option.ocp_en = 1; 844bede03a5SRickyWu if (pcr->option.ocp_en) 845bede03a5SRickyWu pcr->hw_param.interrupt_en |= SD_OC_INT_EN; 846bede03a5SRickyWu pcr->hw_param.ocp_glitch = SD_OCP_GLITCH_10M; 847bede03a5SRickyWu pcr->option.sd_800mA_ocp_thd = RTS525A_OCP_THD_800; 848e455b69dSRui Feng } 849