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 void rts5227_fill_driving(struct rtsx_pcr *pcr, u8 voltage)
31 u8 driving_3v3[4][3] = {
37 u8 driving_1v8[4][3] = {
43 u8 (*driving)[3], drive_sel;
45 if (voltage == OUTPUT_3V3) {
46 driving = driving_3v3;
47 drive_sel = pcr->sd30_drive_sel_3v3;
49 driving = driving_1v8;
50 drive_sel = pcr->sd30_drive_sel_1v8;
53 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_CLK_DRIVE_SEL,
54 0xFF, driving[drive_sel][0]);
55 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_CMD_DRIVE_SEL,
56 0xFF, driving[drive_sel][1]);
57 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_DAT_DRIVE_SEL,
58 0xFF, driving[drive_sel][2]);
61 static void rts5227_fetch_vendor_settings(struct rtsx_pcr *pcr)
65 rtsx_pci_read_config_dword(pcr, PCR_SETTING_REG1, ®);
66 dev_dbg(&(pcr->pci->dev), "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG1, reg);
68 if (!rtsx_vendor_setting_valid(reg))
71 pcr->aspm_en = rtsx_reg_to_aspm(reg);
72 pcr->sd30_drive_sel_1v8 = rtsx_reg_to_sd30_drive_sel_1v8(reg);
73 pcr->card_drive_sel &= 0x3F;
74 pcr->card_drive_sel |= rtsx_reg_to_card_drive_sel(reg);
76 rtsx_pci_read_config_dword(pcr, PCR_SETTING_REG2, ®);
77 dev_dbg(&(pcr->pci->dev), "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG2, reg);
78 pcr->sd30_drive_sel_3v3 = rtsx_reg_to_sd30_drive_sel_3v3(reg);
79 if (rtsx_reg_check_reverse_socket(reg))
80 pcr->flags |= PCR_REVERSE_SOCKET;
83 static void rts5227_force_power_down(struct rtsx_pcr *pcr, u8 pm_state)
85 /* Set relink_time to 0 */
86 rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 1, 0xFF, 0);
87 rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 2, 0xFF, 0);
88 rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 3, 0x01, 0);
90 if (pm_state == HOST_ENTER_S3)
91 rtsx_pci_write_register(pcr, PM_CTRL3, 0x10, 0x10);
93 rtsx_pci_write_register(pcr, FPDCTL, 0x03, 0x03);
96 static int rts5227_extra_init_hw(struct rtsx_pcr *pcr)
100 rtsx_pci_init_cmd(pcr);
102 /* Configure GPIO as output */
103 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, GPIO_CTL, 0x02, 0x02);
104 /* Reset ASPM state to default value */
105 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, ASPM_FORCE_CTL, 0x3F, 0);
106 /* Switch LDO3318 source from DV33 to card_3v3 */
107 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x00);
108 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x01);
109 /* LED shine disabled, set initial shine cycle period */
110 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OLT_LED_CTL, 0x0F, 0x02);
112 pcie_capability_read_word(pcr->pci, PCI_EXP_DEVCTL2, &cap);
113 if (cap & PCI_EXP_DEVCTL2_LTR_EN)
114 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LTR_CTL, 0xFF, 0xA3);
116 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OBFF_CFG, 0x03, 0x03);
117 /* Configure driving */
118 rts5227_fill_driving(pcr, OUTPUT_3V3);
119 /* Configure force_clock_req */
120 if (pcr->flags & PCR_REVERSE_SOCKET)
121 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
122 AUTOLOAD_CFG_BASE + 3, 0xB8, 0xB8);
124 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
125 AUTOLOAD_CFG_BASE + 3, 0xB8, 0x88);
126 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PM_CTRL3, 0x10, 0x00);
128 return rtsx_pci_send_cmd(pcr, 100);
131 static int rts5227_optimize_phy(struct rtsx_pcr *pcr)
133 /* Optimize RX sensitivity */
134 return rtsx_pci_write_phy_register(pcr, 0x00, 0xBA42);
137 static int rts5227_turn_on_led(struct rtsx_pcr *pcr)
139 return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x02);
142 static int rts5227_turn_off_led(struct rtsx_pcr *pcr)
144 return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x00);
147 static int rts5227_enable_auto_blink(struct rtsx_pcr *pcr)
149 return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x08);
152 static int rts5227_disable_auto_blink(struct rtsx_pcr *pcr)
154 return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x00);
157 static int rts5227_card_power_on(struct rtsx_pcr *pcr, int card)
161 rtsx_pci_init_cmd(pcr);
162 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
163 SD_POWER_MASK, SD_PARTIAL_POWER_ON);
164 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
165 LDO3318_PWR_MASK, 0x02);
166 err = rtsx_pci_send_cmd(pcr, 100);
170 /* To avoid too large in-rush current */
173 rtsx_pci_init_cmd(pcr);
174 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
175 SD_POWER_MASK, SD_POWER_ON);
176 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
177 LDO3318_PWR_MASK, 0x06);
178 err = rtsx_pci_send_cmd(pcr, 100);
185 static int rts5227_card_power_off(struct rtsx_pcr *pcr, int card)
187 rtsx_pci_init_cmd(pcr);
188 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
189 SD_POWER_MASK | PMOS_STRG_MASK,
190 SD_POWER_OFF | PMOS_STRG_400mA);
191 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
192 LDO3318_PWR_MASK, 0X00);
193 return rtsx_pci_send_cmd(pcr, 100);
196 static int rts5227_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
200 if (voltage == OUTPUT_3V3) {
201 err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4FC0 | 0x24);
204 } else if (voltage == OUTPUT_1V8) {
205 err = rtsx_pci_write_phy_register(pcr, 0x11, 0x3C02);
208 err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4C80 | 0x24);
216 rtsx_pci_init_cmd(pcr);
217 rts5227_fill_driving(pcr, voltage);
218 return rtsx_pci_send_cmd(pcr, 100);
221 static const struct pcr_ops rts5227_pcr_ops = {
222 .fetch_vendor_settings = rts5227_fetch_vendor_settings,
223 .extra_init_hw = rts5227_extra_init_hw,
224 .optimize_phy = rts5227_optimize_phy,
225 .turn_on_led = rts5227_turn_on_led,
226 .turn_off_led = rts5227_turn_off_led,
227 .enable_auto_blink = rts5227_enable_auto_blink,
228 .disable_auto_blink = rts5227_disable_auto_blink,
229 .card_power_on = rts5227_card_power_on,
230 .card_power_off = rts5227_card_power_off,
231 .switch_output_voltage = rts5227_switch_output_voltage,
233 .conv_clk_and_div_n = NULL,
234 .force_power_down = rts5227_force_power_down,
237 /* SD Pull Control Enable:
238 * SD_DAT[3:0] ==> pull up
242 * SD_CLK ==> pull down
244 static const u32 rts5227_sd_pull_ctl_enable_tbl[] = {
245 RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
246 RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9),
250 /* SD Pull Control Disable:
251 * SD_DAT[3:0] ==> pull down
253 * SD_WP ==> pull down
254 * SD_CMD ==> pull down
255 * SD_CLK ==> pull down
257 static const u32 rts5227_sd_pull_ctl_disable_tbl[] = {
258 RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
259 RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5),
263 /* MS Pull Control Enable:
265 * others ==> pull down
267 static const u32 rts5227_ms_pull_ctl_enable_tbl[] = {
268 RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
269 RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
273 /* MS Pull Control Disable:
275 * others ==> pull down
277 static const u32 rts5227_ms_pull_ctl_disable_tbl[] = {
278 RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
279 RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
283 void rts5227_init_params(struct rtsx_pcr *pcr)
285 pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104;
287 pcr->ops = &rts5227_pcr_ops;
290 pcr->card_drive_sel = RTSX_CARD_DRIVE_DEFAULT;
291 pcr->sd30_drive_sel_1v8 = CFG_DRIVER_TYPE_B;
292 pcr->sd30_drive_sel_3v3 = CFG_DRIVER_TYPE_B;
293 pcr->aspm_en = ASPM_L1_EN;
294 pcr->tx_initial_phase = SET_CLOCK_PHASE(27, 27, 15);
295 pcr->rx_initial_phase = SET_CLOCK_PHASE(30, 7, 7);
297 pcr->sd_pull_ctl_enable_tbl = rts5227_sd_pull_ctl_enable_tbl;
298 pcr->sd_pull_ctl_disable_tbl = rts5227_sd_pull_ctl_disable_tbl;
299 pcr->ms_pull_ctl_enable_tbl = rts5227_ms_pull_ctl_enable_tbl;
300 pcr->ms_pull_ctl_disable_tbl = rts5227_ms_pull_ctl_disable_tbl;