blob: 42ebf5c050c145e0d1ce70212c4360b9d4aa3e0c [file] [log] [blame]
Roger Tsenge1237932013-02-04 15:45:59 +08001/* 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
Wei WANG773ccdf2013-08-20 14:18:51 +080032static 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
64static void rts5227_fetch_vendor_settings(struct rtsx_pcr *pcr)
65{
66 u32 reg;
67
68 rtsx_pci_read_config_dword(pcr, PCR_SETTING_REG1, &reg);
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, &reg);
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
Wei WANG5947c162013-08-20 14:18:52 +080086static void rts5227_force_power_down(struct rtsx_pcr *pcr)
87{
88 /* Set relink_time to 0 */
89 rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 1, 0xFF, 0);
90 rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 2, 0xFF, 0);
91 rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 3, 0x01, 0);
92
93 rtsx_pci_write_register(pcr, FPDCTL, 0x03, 0x03);
94}
95
Roger Tsenge1237932013-02-04 15:45:59 +080096static int rts5227_extra_init_hw(struct rtsx_pcr *pcr)
97{
98 u16 cap;
99
100 rtsx_pci_init_cmd(pcr);
101
102 /* Configure GPIO as output */
103 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, GPIO_CTL, 0x02, 0x02);
104 /* Switch LDO3318 source from DV33 to card_3v3 */
105 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x00);
106 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x01);
107 /* LED shine disabled, set initial shine cycle period */
108 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OLT_LED_CTL, 0x0F, 0x02);
109 /* Configure LTR */
110 pcie_capability_read_word(pcr->pci, PCI_EXP_DEVCTL2, &cap);
111 if (cap & PCI_EXP_LTR_EN)
112 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LTR_CTL, 0xFF, 0xA3);
113 /* Configure OBFF */
114 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OBFF_CFG, 0x03, 0x03);
Wei WANG773ccdf2013-08-20 14:18:51 +0800115 /* Configure driving */
116 rts5227_fill_driving(pcr, OUTPUT_3V3);
117 /* Configure force_clock_req */
118 if (pcr->flags & PCR_REVERSE_SOCKET)
119 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
120 AUTOLOAD_CFG_BASE + 3, 0xB8, 0xB8);
121 else
122 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
123 AUTOLOAD_CFG_BASE + 3, 0xB8, 0x88);
Roger Tsenge1237932013-02-04 15:45:59 +0800124
125 return rtsx_pci_send_cmd(pcr, 100);
126}
127
128static int rts5227_optimize_phy(struct rtsx_pcr *pcr)
129{
130 /* Optimize RX sensitivity */
131 return rtsx_pci_write_phy_register(pcr, 0x00, 0xBA42);
132}
133
134static int rts5227_turn_on_led(struct rtsx_pcr *pcr)
135{
136 return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x02);
137}
138
139static int rts5227_turn_off_led(struct rtsx_pcr *pcr)
140{
141 return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x00);
142}
143
144static int rts5227_enable_auto_blink(struct rtsx_pcr *pcr)
145{
146 return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x08);
147}
148
149static int rts5227_disable_auto_blink(struct rtsx_pcr *pcr)
150{
151 return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x00);
152}
153
154static int rts5227_card_power_on(struct rtsx_pcr *pcr, int card)
155{
156 int err;
157
158 rtsx_pci_init_cmd(pcr);
159 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
160 SD_POWER_MASK, SD_PARTIAL_POWER_ON);
161 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
162 LDO3318_PWR_MASK, 0x02);
163 err = rtsx_pci_send_cmd(pcr, 100);
164 if (err < 0)
165 return err;
166
167 /* To avoid too large in-rush current */
168 udelay(150);
169
170 rtsx_pci_init_cmd(pcr);
171 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
172 SD_POWER_MASK, SD_POWER_ON);
173 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
174 LDO3318_PWR_MASK, 0x06);
175 err = rtsx_pci_send_cmd(pcr, 100);
176 if (err < 0)
177 return err;
178
179 return 0;
180}
181
182static int rts5227_card_power_off(struct rtsx_pcr *pcr, int card)
183{
184 rtsx_pci_init_cmd(pcr);
185 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
186 SD_POWER_MASK | PMOS_STRG_MASK,
187 SD_POWER_OFF | PMOS_STRG_400mA);
188 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
189 LDO3318_PWR_MASK, 0X00);
190 return rtsx_pci_send_cmd(pcr, 100);
191}
192
193static int rts5227_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
194{
195 int err;
Roger Tsenge1237932013-02-04 15:45:59 +0800196
197 if (voltage == OUTPUT_3V3) {
198 err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4FC0 | 0x24);
199 if (err < 0)
200 return err;
Roger Tsenge1237932013-02-04 15:45:59 +0800201 } else if (voltage == OUTPUT_1V8) {
202 err = rtsx_pci_write_phy_register(pcr, 0x11, 0x3C02);
203 if (err < 0)
204 return err;
205 err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4C80 | 0x24);
206 if (err < 0)
207 return err;
Roger Tsenge1237932013-02-04 15:45:59 +0800208 } else {
209 return -EINVAL;
210 }
211
212 /* set pad drive */
213 rtsx_pci_init_cmd(pcr);
Wei WANG773ccdf2013-08-20 14:18:51 +0800214 rts5227_fill_driving(pcr, voltage);
Roger Tsenge1237932013-02-04 15:45:59 +0800215 return rtsx_pci_send_cmd(pcr, 100);
216}
217
218static const struct pcr_ops rts5227_pcr_ops = {
Wei WANG773ccdf2013-08-20 14:18:51 +0800219 .fetch_vendor_settings = rts5227_fetch_vendor_settings,
Roger Tsenge1237932013-02-04 15:45:59 +0800220 .extra_init_hw = rts5227_extra_init_hw,
221 .optimize_phy = rts5227_optimize_phy,
222 .turn_on_led = rts5227_turn_on_led,
223 .turn_off_led = rts5227_turn_off_led,
224 .enable_auto_blink = rts5227_enable_auto_blink,
225 .disable_auto_blink = rts5227_disable_auto_blink,
226 .card_power_on = rts5227_card_power_on,
227 .card_power_off = rts5227_card_power_off,
228 .switch_output_voltage = rts5227_switch_output_voltage,
229 .cd_deglitch = NULL,
230 .conv_clk_and_div_n = NULL,
Wei WANG5947c162013-08-20 14:18:52 +0800231 .force_power_down = rts5227_force_power_down,
Roger Tsenge1237932013-02-04 15:45:59 +0800232};
233
234/* SD Pull Control Enable:
235 * SD_DAT[3:0] ==> pull up
236 * SD_CD ==> pull up
237 * SD_WP ==> pull up
238 * SD_CMD ==> pull up
239 * SD_CLK ==> pull down
240 */
241static const u32 rts5227_sd_pull_ctl_enable_tbl[] = {
242 RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
243 RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9),
244 0,
245};
246
247/* SD Pull Control Disable:
248 * SD_DAT[3:0] ==> pull down
249 * SD_CD ==> pull up
250 * SD_WP ==> pull down
251 * SD_CMD ==> pull down
252 * SD_CLK ==> pull down
253 */
254static const u32 rts5227_sd_pull_ctl_disable_tbl[] = {
255 RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
256 RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5),
257 0,
258};
259
260/* MS Pull Control Enable:
261 * MS CD ==> pull up
262 * others ==> pull down
263 */
264static const u32 rts5227_ms_pull_ctl_enable_tbl[] = {
265 RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
266 RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
267 0,
268};
269
270/* MS Pull Control Disable:
271 * MS CD ==> pull up
272 * others ==> pull down
273 */
274static const u32 rts5227_ms_pull_ctl_disable_tbl[] = {
275 RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
276 RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
277 0,
278};
279
280void rts5227_init_params(struct rtsx_pcr *pcr)
281{
282 pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104;
283 pcr->num_slots = 2;
284 pcr->ops = &rts5227_pcr_ops;
285
Wei WANG773ccdf2013-08-20 14:18:51 +0800286 pcr->flags = 0;
287 pcr->card_drive_sel = RTSX_CARD_DRIVE_DEFAULT;
288 pcr->sd30_drive_sel_1v8 = CFG_DRIVER_TYPE_B;
289 pcr->sd30_drive_sel_3v3 = CFG_DRIVER_TYPE_B;
290 pcr->aspm_en = ASPM_L1_EN;
291
Roger Tsenge1237932013-02-04 15:45:59 +0800292 pcr->sd_pull_ctl_enable_tbl = rts5227_sd_pull_ctl_enable_tbl;
293 pcr->sd_pull_ctl_disable_tbl = rts5227_sd_pull_ctl_disable_tbl;
294 pcr->ms_pull_ctl_enable_tbl = rts5227_ms_pull_ctl_enable_tbl;
295 pcr->ms_pull_ctl_disable_tbl = rts5227_ms_pull_ctl_disable_tbl;
296}