1 /* Driver for Realtek PCI-Express card reader
3 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
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
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.
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/>.
19 * Wei WANG <wei_wang@realsil.com.cn>
20 * Roger Tseng <rogerable@realtek.com>
23 #include <linux/module.h>
24 #include <linux/delay.h>
25 #include <linux/mfd/rtsx_pci.h>
29 static u8 rts5227_get_ic_version(struct rtsx_pcr *pcr)
33 rtsx_pci_read_register(pcr, DUMMY_REG_RESET_0, &val);
37 static void rts5227_fill_driving(struct rtsx_pcr *pcr, u8 voltage)
39 u8 driving_3v3[4][3] = {
45 u8 driving_1v8[4][3] = {
51 u8 (*driving)[3], drive_sel;
53 if (voltage == OUTPUT_3V3) {
54 driving = driving_3v3;
55 drive_sel = pcr->sd30_drive_sel_3v3;
57 driving = driving_1v8;
58 drive_sel = pcr->sd30_drive_sel_1v8;
61 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_CLK_DRIVE_SEL,
62 0xFF, driving[drive_sel][0]);
63 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_CMD_DRIVE_SEL,
64 0xFF, driving[drive_sel][1]);
65 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_DAT_DRIVE_SEL,
66 0xFF, driving[drive_sel][2]);
69 static void rts5227_fetch_vendor_settings(struct rtsx_pcr *pcr)
73 rtsx_pci_read_config_dword(pcr, PCR_SETTING_REG1, ®);
74 pcr_dbg(pcr, "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG1, reg);
76 if (!rtsx_vendor_setting_valid(reg))
79 pcr->aspm_en = rtsx_reg_to_aspm(reg);
80 pcr->sd30_drive_sel_1v8 = rtsx_reg_to_sd30_drive_sel_1v8(reg);
81 pcr->card_drive_sel &= 0x3F;
82 pcr->card_drive_sel |= rtsx_reg_to_card_drive_sel(reg);
84 rtsx_pci_read_config_dword(pcr, PCR_SETTING_REG2, ®);
85 pcr_dbg(pcr, "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG2, reg);
86 pcr->sd30_drive_sel_3v3 = rtsx_reg_to_sd30_drive_sel_3v3(reg);
87 if (rtsx_reg_check_reverse_socket(reg))
88 pcr->flags |= PCR_REVERSE_SOCKET;
91 static void rts5227_force_power_down(struct rtsx_pcr *pcr, u8 pm_state)
93 /* Set relink_time to 0 */
94 rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 1, 0xFF, 0);
95 rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 2, 0xFF, 0);
96 rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 3, 0x01, 0);
98 if (pm_state == HOST_ENTER_S3)
99 rtsx_pci_write_register(pcr, pcr->reg_pm_ctrl3, 0x10, 0x10);
101 rtsx_pci_write_register(pcr, FPDCTL, 0x03, 0x03);
104 static int rts5227_extra_init_hw(struct rtsx_pcr *pcr)
108 rtsx_pci_init_cmd(pcr);
110 /* Configure GPIO as output */
111 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, GPIO_CTL, 0x02, 0x02);
112 /* Reset ASPM state to default value */
113 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, ASPM_FORCE_CTL, 0x3F, 0);
114 /* Switch LDO3318 source from DV33 to card_3v3 */
115 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x00);
116 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x01);
117 /* LED shine disabled, set initial shine cycle period */
118 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OLT_LED_CTL, 0x0F, 0x02);
120 pcie_capability_read_word(pcr->pci, PCI_EXP_DEVCTL2, &cap);
121 if (cap & PCI_EXP_DEVCTL2_LTR_EN)
122 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LTR_CTL, 0xFF, 0xA3);
124 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OBFF_CFG, 0x03, 0x03);
125 /* Configure driving */
126 rts5227_fill_driving(pcr, OUTPUT_3V3);
127 /* Configure force_clock_req */
128 if (pcr->flags & PCR_REVERSE_SOCKET)
129 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0xB8, 0xB8);
131 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0xB8, 0x88);
132 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, pcr->reg_pm_ctrl3, 0x10, 0x00);
134 return rtsx_pci_send_cmd(pcr, 100);
137 static int rts5227_optimize_phy(struct rtsx_pcr *pcr)
141 err = rtsx_pci_write_register(pcr, PM_CTRL3, D3_DELINK_MODE_EN, 0x00);
145 /* Optimize RX sensitivity */
146 return rtsx_pci_write_phy_register(pcr, 0x00, 0xBA42);
149 static int rts5227_turn_on_led(struct rtsx_pcr *pcr)
151 return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x02);
154 static int rts5227_turn_off_led(struct rtsx_pcr *pcr)
156 return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x00);
159 static int rts5227_enable_auto_blink(struct rtsx_pcr *pcr)
161 return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x08);
164 static int rts5227_disable_auto_blink(struct rtsx_pcr *pcr)
166 return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x00);
169 static int rts5227_card_power_on(struct rtsx_pcr *pcr, int card)
173 rtsx_pci_init_cmd(pcr);
174 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
175 SD_POWER_MASK, SD_PARTIAL_POWER_ON);
176 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
177 LDO3318_PWR_MASK, 0x02);
178 err = rtsx_pci_send_cmd(pcr, 100);
182 /* To avoid too large in-rush current */
185 rtsx_pci_init_cmd(pcr);
186 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
187 SD_POWER_MASK, SD_POWER_ON);
188 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
189 LDO3318_PWR_MASK, 0x06);
190 return rtsx_pci_send_cmd(pcr, 100);
193 static int rts5227_card_power_off(struct rtsx_pcr *pcr, int card)
195 rtsx_pci_init_cmd(pcr);
196 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
197 SD_POWER_MASK | PMOS_STRG_MASK,
198 SD_POWER_OFF | PMOS_STRG_400mA);
199 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
200 LDO3318_PWR_MASK, 0X00);
201 return rtsx_pci_send_cmd(pcr, 100);
204 static int rts5227_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
208 if (voltage == OUTPUT_3V3) {
209 err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4FC0 | 0x24);
212 } else if (voltage == OUTPUT_1V8) {
213 err = rtsx_pci_write_phy_register(pcr, 0x11, 0x3C02);
216 err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4C80 | 0x24);
224 rtsx_pci_init_cmd(pcr);
225 rts5227_fill_driving(pcr, voltage);
226 return rtsx_pci_send_cmd(pcr, 100);
229 static const struct pcr_ops rts5227_pcr_ops = {
230 .fetch_vendor_settings = rts5227_fetch_vendor_settings,
231 .extra_init_hw = rts5227_extra_init_hw,
232 .optimize_phy = rts5227_optimize_phy,
233 .turn_on_led = rts5227_turn_on_led,
234 .turn_off_led = rts5227_turn_off_led,
235 .enable_auto_blink = rts5227_enable_auto_blink,
236 .disable_auto_blink = rts5227_disable_auto_blink,
237 .card_power_on = rts5227_card_power_on,
238 .card_power_off = rts5227_card_power_off,
239 .switch_output_voltage = rts5227_switch_output_voltage,
241 .conv_clk_and_div_n = NULL,
242 .force_power_down = rts5227_force_power_down,
245 /* SD Pull Control Enable:
246 * SD_DAT[3:0] ==> pull up
250 * SD_CLK ==> pull down
252 static const u32 rts5227_sd_pull_ctl_enable_tbl[] = {
253 RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
254 RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9),
258 /* SD Pull Control Disable:
259 * SD_DAT[3:0] ==> pull down
261 * SD_WP ==> pull down
262 * SD_CMD ==> pull down
263 * SD_CLK ==> pull down
265 static const u32 rts5227_sd_pull_ctl_disable_tbl[] = {
266 RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
267 RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5),
271 /* MS Pull Control Enable:
273 * others ==> pull down
275 static const u32 rts5227_ms_pull_ctl_enable_tbl[] = {
276 RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
277 RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
281 /* MS Pull Control Disable:
283 * others ==> pull down
285 static const u32 rts5227_ms_pull_ctl_disable_tbl[] = {
286 RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
287 RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
291 void rts5227_init_params(struct rtsx_pcr *pcr)
293 pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104;
295 pcr->ops = &rts5227_pcr_ops;
298 pcr->card_drive_sel = RTSX_CARD_DRIVE_DEFAULT;
299 pcr->sd30_drive_sel_1v8 = CFG_DRIVER_TYPE_B;
300 pcr->sd30_drive_sel_3v3 = CFG_DRIVER_TYPE_B;
301 pcr->aspm_en = ASPM_L1_EN;
302 pcr->tx_initial_phase = SET_CLOCK_PHASE(27, 27, 15);
303 pcr->rx_initial_phase = SET_CLOCK_PHASE(30, 7, 7);
305 pcr->ic_version = rts5227_get_ic_version(pcr);
306 pcr->sd_pull_ctl_enable_tbl = rts5227_sd_pull_ctl_enable_tbl;
307 pcr->sd_pull_ctl_disable_tbl = rts5227_sd_pull_ctl_disable_tbl;
308 pcr->ms_pull_ctl_enable_tbl = rts5227_ms_pull_ctl_enable_tbl;
309 pcr->ms_pull_ctl_disable_tbl = rts5227_ms_pull_ctl_disable_tbl;
311 pcr->reg_pm_ctrl3 = PM_CTRL3;
314 static int rts522a_optimize_phy(struct rtsx_pcr *pcr)
318 err = rtsx_pci_write_register(pcr, RTS522A_PM_CTRL3, D3_DELINK_MODE_EN,
323 if (is_version(pcr, 0x522A, IC_VER_A)) {
324 err = rtsx_pci_write_phy_register(pcr, PHY_RCR2,
329 rtsx_pci_write_phy_register(pcr, PHY_RCR1, PHY_RCR1_INIT_27S);
330 rtsx_pci_write_phy_register(pcr, PHY_FLD0, PHY_FLD0_INIT_27S);
331 rtsx_pci_write_phy_register(pcr, PHY_FLD3, PHY_FLD3_INIT_27S);
332 rtsx_pci_write_phy_register(pcr, PHY_FLD4, PHY_FLD4_INIT_27S);
338 static int rts522a_extra_init_hw(struct rtsx_pcr *pcr)
340 rts5227_extra_init_hw(pcr);
342 rtsx_pci_write_register(pcr, FUNC_FORCE_CTL, FUNC_FORCE_UPME_XMT_DBG,
343 FUNC_FORCE_UPME_XMT_DBG);
344 rtsx_pci_write_register(pcr, PCLK_CTL, 0x04, 0x04);
345 rtsx_pci_write_register(pcr, PM_EVENT_DEBUG, PME_DEBUG_0, PME_DEBUG_0);
346 rtsx_pci_write_register(pcr, PM_CLK_FORCE_CTL, 0xFF, 0x11);
351 /* rts522a operations mainly derived from rts5227, except phy/hw init setting.
353 static const struct pcr_ops rts522a_pcr_ops = {
354 .fetch_vendor_settings = rts5227_fetch_vendor_settings,
355 .extra_init_hw = rts522a_extra_init_hw,
356 .optimize_phy = rts522a_optimize_phy,
357 .turn_on_led = rts5227_turn_on_led,
358 .turn_off_led = rts5227_turn_off_led,
359 .enable_auto_blink = rts5227_enable_auto_blink,
360 .disable_auto_blink = rts5227_disable_auto_blink,
361 .card_power_on = rts5227_card_power_on,
362 .card_power_off = rts5227_card_power_off,
363 .switch_output_voltage = rts5227_switch_output_voltage,
365 .conv_clk_and_div_n = NULL,
366 .force_power_down = rts5227_force_power_down,
369 void rts522a_init_params(struct rtsx_pcr *pcr)
371 rts5227_init_params(pcr);
372 pcr->ops = &rts522a_pcr_ops;
374 pcr->reg_pm_ctrl3 = RTS522A_PM_CTRL3;