]>
Commit | Line | Data |
---|---|---|
e1237932 RT |
1 | /* Driver for Realtek PCI-Express card reader |
2 | * | |
3 | * Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved. | |
4 | * | |
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 | |
8 | * later version. | |
9 | * | |
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. | |
14 | * | |
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/>. | |
17 | * | |
18 | * Author: | |
19 | * Wei WANG <wei_wang@realsil.com.cn> | |
20 | * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China | |
21 | * | |
22 | * Roger Tseng <rogerable@realtek.com> | |
23 | * No. 2, Innovation Road II, Hsinchu Science Park, Hsinchu 300, Taiwan | |
24 | */ | |
25 | ||
26 | #include <linux/module.h> | |
27 | #include <linux/delay.h> | |
28 | #include <linux/mfd/rtsx_pci.h> | |
29 | ||
30 | #include "rtsx_pcr.h" | |
31 | ||
773ccdfd WW |
32 | static void rts5227_fill_driving(struct rtsx_pcr *pcr, u8 voltage) |
33 | { | |
34 | u8 driving_3v3[4][3] = { | |
35 | {0x13, 0x13, 0x13}, | |
36 | {0x96, 0x96, 0x96}, | |
37 | {0x7F, 0x7F, 0x7F}, | |
38 | {0x96, 0x96, 0x96}, | |
39 | }; | |
40 | u8 driving_1v8[4][3] = { | |
41 | {0x99, 0x99, 0x99}, | |
42 | {0xAA, 0xAA, 0xAA}, | |
43 | {0xFE, 0xFE, 0xFE}, | |
44 | {0xB3, 0xB3, 0xB3}, | |
45 | }; | |
46 | u8 (*driving)[3], drive_sel; | |
47 | ||
48 | if (voltage == OUTPUT_3V3) { | |
49 | driving = driving_3v3; | |
50 | drive_sel = pcr->sd30_drive_sel_3v3; | |
51 | } else { | |
52 | driving = driving_1v8; | |
53 | drive_sel = pcr->sd30_drive_sel_1v8; | |
54 | } | |
55 | ||
56 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_CLK_DRIVE_SEL, | |
57 | 0xFF, driving[drive_sel][0]); | |
58 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_CMD_DRIVE_SEL, | |
59 | 0xFF, driving[drive_sel][1]); | |
60 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_DAT_DRIVE_SEL, | |
61 | 0xFF, driving[drive_sel][2]); | |
62 | } | |
63 | ||
64 | static void rts5227_fetch_vendor_settings(struct rtsx_pcr *pcr) | |
65 | { | |
66 | u32 reg; | |
67 | ||
68 | rtsx_pci_read_config_dword(pcr, PCR_SETTING_REG1, ®); | |
69 | dev_dbg(&(pcr->pci->dev), "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG1, reg); | |
70 | ||
71 | if (!rtsx_vendor_setting_valid(reg)) | |
72 | return; | |
73 | ||
74 | pcr->aspm_en = rtsx_reg_to_aspm(reg); | |
75 | pcr->sd30_drive_sel_1v8 = rtsx_reg_to_sd30_drive_sel_1v8(reg); | |
76 | pcr->card_drive_sel &= 0x3F; | |
77 | pcr->card_drive_sel |= rtsx_reg_to_card_drive_sel(reg); | |
78 | ||
79 | rtsx_pci_read_config_dword(pcr, PCR_SETTING_REG2, ®); | |
80 | dev_dbg(&(pcr->pci->dev), "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG2, reg); | |
81 | pcr->sd30_drive_sel_3v3 = rtsx_reg_to_sd30_drive_sel_3v3(reg); | |
82 | if (rtsx_reg_check_reverse_socket(reg)) | |
83 | pcr->flags |= PCR_REVERSE_SOCKET; | |
84 | } | |
85 | ||
e1237932 RT |
86 | static int rts5227_extra_init_hw(struct rtsx_pcr *pcr) |
87 | { | |
88 | u16 cap; | |
89 | ||
90 | rtsx_pci_init_cmd(pcr); | |
91 | ||
92 | /* Configure GPIO as output */ | |
93 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, GPIO_CTL, 0x02, 0x02); | |
94 | /* Switch LDO3318 source from DV33 to card_3v3 */ | |
95 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x00); | |
96 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x01); | |
97 | /* LED shine disabled, set initial shine cycle period */ | |
98 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OLT_LED_CTL, 0x0F, 0x02); | |
99 | /* Configure LTR */ | |
100 | pcie_capability_read_word(pcr->pci, PCI_EXP_DEVCTL2, &cap); | |
101 | if (cap & PCI_EXP_LTR_EN) | |
102 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LTR_CTL, 0xFF, 0xA3); | |
103 | /* Configure OBFF */ | |
104 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OBFF_CFG, 0x03, 0x03); | |
773ccdfd WW |
105 | /* Configure driving */ |
106 | rts5227_fill_driving(pcr, OUTPUT_3V3); | |
107 | /* Configure force_clock_req */ | |
108 | if (pcr->flags & PCR_REVERSE_SOCKET) | |
109 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, | |
110 | AUTOLOAD_CFG_BASE + 3, 0xB8, 0xB8); | |
111 | else | |
112 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, | |
113 | AUTOLOAD_CFG_BASE + 3, 0xB8, 0x88); | |
e1237932 RT |
114 | |
115 | return rtsx_pci_send_cmd(pcr, 100); | |
116 | } | |
117 | ||
118 | static int rts5227_optimize_phy(struct rtsx_pcr *pcr) | |
119 | { | |
120 | /* Optimize RX sensitivity */ | |
121 | return rtsx_pci_write_phy_register(pcr, 0x00, 0xBA42); | |
122 | } | |
123 | ||
124 | static int rts5227_turn_on_led(struct rtsx_pcr *pcr) | |
125 | { | |
126 | return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x02); | |
127 | } | |
128 | ||
129 | static int rts5227_turn_off_led(struct rtsx_pcr *pcr) | |
130 | { | |
131 | return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x00); | |
132 | } | |
133 | ||
134 | static int rts5227_enable_auto_blink(struct rtsx_pcr *pcr) | |
135 | { | |
136 | return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x08); | |
137 | } | |
138 | ||
139 | static int rts5227_disable_auto_blink(struct rtsx_pcr *pcr) | |
140 | { | |
141 | return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x00); | |
142 | } | |
143 | ||
144 | static int rts5227_card_power_on(struct rtsx_pcr *pcr, int card) | |
145 | { | |
146 | int err; | |
147 | ||
148 | rtsx_pci_init_cmd(pcr); | |
149 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL, | |
150 | SD_POWER_MASK, SD_PARTIAL_POWER_ON); | |
151 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL, | |
152 | LDO3318_PWR_MASK, 0x02); | |
153 | err = rtsx_pci_send_cmd(pcr, 100); | |
154 | if (err < 0) | |
155 | return err; | |
156 | ||
157 | /* To avoid too large in-rush current */ | |
158 | udelay(150); | |
159 | ||
160 | rtsx_pci_init_cmd(pcr); | |
161 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL, | |
162 | SD_POWER_MASK, SD_POWER_ON); | |
163 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL, | |
164 | LDO3318_PWR_MASK, 0x06); | |
165 | err = rtsx_pci_send_cmd(pcr, 100); | |
166 | if (err < 0) | |
167 | return err; | |
168 | ||
169 | return 0; | |
170 | } | |
171 | ||
172 | static int rts5227_card_power_off(struct rtsx_pcr *pcr, int card) | |
173 | { | |
174 | rtsx_pci_init_cmd(pcr); | |
175 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL, | |
176 | SD_POWER_MASK | PMOS_STRG_MASK, | |
177 | SD_POWER_OFF | PMOS_STRG_400mA); | |
178 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL, | |
179 | LDO3318_PWR_MASK, 0X00); | |
180 | return rtsx_pci_send_cmd(pcr, 100); | |
181 | } | |
182 | ||
183 | static int rts5227_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage) | |
184 | { | |
185 | int err; | |
e1237932 RT |
186 | |
187 | if (voltage == OUTPUT_3V3) { | |
188 | err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4FC0 | 0x24); | |
189 | if (err < 0) | |
190 | return err; | |
e1237932 RT |
191 | } else if (voltage == OUTPUT_1V8) { |
192 | err = rtsx_pci_write_phy_register(pcr, 0x11, 0x3C02); | |
193 | if (err < 0) | |
194 | return err; | |
195 | err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4C80 | 0x24); | |
196 | if (err < 0) | |
197 | return err; | |
e1237932 RT |
198 | } else { |
199 | return -EINVAL; | |
200 | } | |
201 | ||
202 | /* set pad drive */ | |
203 | rtsx_pci_init_cmd(pcr); | |
773ccdfd | 204 | rts5227_fill_driving(pcr, voltage); |
e1237932 RT |
205 | return rtsx_pci_send_cmd(pcr, 100); |
206 | } | |
207 | ||
208 | static const struct pcr_ops rts5227_pcr_ops = { | |
773ccdfd | 209 | .fetch_vendor_settings = rts5227_fetch_vendor_settings, |
e1237932 RT |
210 | .extra_init_hw = rts5227_extra_init_hw, |
211 | .optimize_phy = rts5227_optimize_phy, | |
212 | .turn_on_led = rts5227_turn_on_led, | |
213 | .turn_off_led = rts5227_turn_off_led, | |
214 | .enable_auto_blink = rts5227_enable_auto_blink, | |
215 | .disable_auto_blink = rts5227_disable_auto_blink, | |
216 | .card_power_on = rts5227_card_power_on, | |
217 | .card_power_off = rts5227_card_power_off, | |
218 | .switch_output_voltage = rts5227_switch_output_voltage, | |
219 | .cd_deglitch = NULL, | |
220 | .conv_clk_and_div_n = NULL, | |
221 | }; | |
222 | ||
223 | /* SD Pull Control Enable: | |
224 | * SD_DAT[3:0] ==> pull up | |
225 | * SD_CD ==> pull up | |
226 | * SD_WP ==> pull up | |
227 | * SD_CMD ==> pull up | |
228 | * SD_CLK ==> pull down | |
229 | */ | |
230 | static const u32 rts5227_sd_pull_ctl_enable_tbl[] = { | |
231 | RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA), | |
232 | RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9), | |
233 | 0, | |
234 | }; | |
235 | ||
236 | /* SD Pull Control Disable: | |
237 | * SD_DAT[3:0] ==> pull down | |
238 | * SD_CD ==> pull up | |
239 | * SD_WP ==> pull down | |
240 | * SD_CMD ==> pull down | |
241 | * SD_CLK ==> pull down | |
242 | */ | |
243 | static const u32 rts5227_sd_pull_ctl_disable_tbl[] = { | |
244 | RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55), | |
245 | RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5), | |
246 | 0, | |
247 | }; | |
248 | ||
249 | /* MS Pull Control Enable: | |
250 | * MS CD ==> pull up | |
251 | * others ==> pull down | |
252 | */ | |
253 | static const u32 rts5227_ms_pull_ctl_enable_tbl[] = { | |
254 | RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55), | |
255 | RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15), | |
256 | 0, | |
257 | }; | |
258 | ||
259 | /* MS Pull Control Disable: | |
260 | * MS CD ==> pull up | |
261 | * others ==> pull down | |
262 | */ | |
263 | static const u32 rts5227_ms_pull_ctl_disable_tbl[] = { | |
264 | RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55), | |
265 | RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15), | |
266 | 0, | |
267 | }; | |
268 | ||
269 | void rts5227_init_params(struct rtsx_pcr *pcr) | |
270 | { | |
271 | pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104; | |
272 | pcr->num_slots = 2; | |
273 | pcr->ops = &rts5227_pcr_ops; | |
274 | ||
773ccdfd WW |
275 | pcr->flags = 0; |
276 | pcr->card_drive_sel = RTSX_CARD_DRIVE_DEFAULT; | |
277 | pcr->sd30_drive_sel_1v8 = CFG_DRIVER_TYPE_B; | |
278 | pcr->sd30_drive_sel_3v3 = CFG_DRIVER_TYPE_B; | |
279 | pcr->aspm_en = ASPM_L1_EN; | |
280 | ||
e1237932 RT |
281 | pcr->sd_pull_ctl_enable_tbl = rts5227_sd_pull_ctl_enable_tbl; |
282 | pcr->sd_pull_ctl_disable_tbl = rts5227_sd_pull_ctl_disable_tbl; | |
283 | pcr->ms_pull_ctl_enable_tbl = rts5227_ms_pull_ctl_enable_tbl; | |
284 | pcr->ms_pull_ctl_disable_tbl = rts5227_ms_pull_ctl_disable_tbl; | |
285 | } |