blob: a0b695af85618bbcc6413f6de98d90958d3bc64d [file] [log] [blame]
Wei WANG67d16a42012-11-09 20:53:33 +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
23#include <linux/module.h>
24#include <linux/delay.h>
25#include <linux/mfd/rtsx_pci.h>
26
27#include "rtsx_pcr.h"
28
29static u8 rts5229_get_ic_version(struct rtsx_pcr *pcr)
30{
31 u8 val;
32
33 rtsx_pci_read_register(pcr, DUMMY_REG_RESET_0, &val);
34 return val & 0x0F;
35}
36
Wei WANG773ccdf2013-08-20 14:18:51 +080037static void rts5229_fetch_vendor_settings(struct rtsx_pcr *pcr)
38{
39 u32 reg;
40
41 rtsx_pci_read_config_dword(pcr, PCR_SETTING_REG1, &reg);
42 dev_dbg(&(pcr->pci->dev), "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG1, reg);
43
44 if (!rtsx_vendor_setting_valid(reg))
45 return;
46
47 pcr->aspm_en = rtsx_reg_to_aspm(reg);
48 pcr->sd30_drive_sel_1v8 =
49 map_sd_drive(rtsx_reg_to_sd30_drive_sel_1v8(reg));
50 pcr->card_drive_sel &= 0x3F;
51 pcr->card_drive_sel |= rtsx_reg_to_card_drive_sel(reg);
52
53 rtsx_pci_read_config_dword(pcr, PCR_SETTING_REG2, &reg);
54 dev_dbg(&(pcr->pci->dev), "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG2, reg);
55 pcr->sd30_drive_sel_3v3 =
56 map_sd_drive(rtsx_reg_to_sd30_drive_sel_3v3(reg));
57}
58
Wei WANG5947c162013-08-20 14:18:52 +080059static void rts5229_force_power_down(struct rtsx_pcr *pcr)
60{
61 rtsx_pci_write_register(pcr, FPDCTL, 0x03, 0x03);
62}
63
Wei WANG67d16a42012-11-09 20:53:33 +080064static int rts5229_extra_init_hw(struct rtsx_pcr *pcr)
65{
66 rtsx_pci_init_cmd(pcr);
67
68 /* Configure GPIO as output */
69 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, GPIO_CTL, 0x02, 0x02);
70 /* Switch LDO3318 source from DV33 to card_3v3 */
71 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x00);
72 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x01);
73 /* LED shine disabled, set initial shine cycle period */
74 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OLT_LED_CTL, 0x0F, 0x02);
Wei WANG773ccdf2013-08-20 14:18:51 +080075 /* Configure driving */
76 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_DRIVE_SEL,
77 0xFF, pcr->sd30_drive_sel_3v3);
Wei WANG67d16a42012-11-09 20:53:33 +080078
79 return rtsx_pci_send_cmd(pcr, 100);
80}
81
82static int rts5229_optimize_phy(struct rtsx_pcr *pcr)
83{
84 /* Optimize RX sensitivity */
85 return rtsx_pci_write_phy_register(pcr, 0x00, 0xBA42);
86}
87
88static int rts5229_turn_on_led(struct rtsx_pcr *pcr)
89{
90 return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x02);
91}
92
93static int rts5229_turn_off_led(struct rtsx_pcr *pcr)
94{
95 return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x00);
96}
97
98static int rts5229_enable_auto_blink(struct rtsx_pcr *pcr)
99{
100 return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x08);
101}
102
103static int rts5229_disable_auto_blink(struct rtsx_pcr *pcr)
104{
105 return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x00);
106}
107
108static int rts5229_card_power_on(struct rtsx_pcr *pcr, int card)
109{
110 int err;
111
112 rtsx_pci_init_cmd(pcr);
113 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
114 SD_POWER_MASK, SD_PARTIAL_POWER_ON);
115 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
116 LDO3318_PWR_MASK, 0x02);
117 err = rtsx_pci_send_cmd(pcr, 100);
118 if (err < 0)
119 return err;
120
121 /* To avoid too large in-rush current */
122 udelay(150);
123
124 rtsx_pci_init_cmd(pcr);
125 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
126 SD_POWER_MASK, SD_POWER_ON);
127 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
128 LDO3318_PWR_MASK, 0x06);
129 err = rtsx_pci_send_cmd(pcr, 100);
130 if (err < 0)
131 return err;
132
133 return 0;
134}
135
136static int rts5229_card_power_off(struct rtsx_pcr *pcr, int card)
137{
138 rtsx_pci_init_cmd(pcr);
139 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
140 SD_POWER_MASK | PMOS_STRG_MASK,
141 SD_POWER_OFF | PMOS_STRG_400mA);
142 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
Wei WANG773ccdf2013-08-20 14:18:51 +0800143 LDO3318_PWR_MASK, 0x00);
Wei WANG67d16a42012-11-09 20:53:33 +0800144 return rtsx_pci_send_cmd(pcr, 100);
145}
146
Wei WANGd817ac42013-01-23 09:51:04 +0800147static int rts5229_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
148{
149 int err;
150
151 if (voltage == OUTPUT_3V3) {
Roger Tseng88a7ee32013-02-04 15:45:58 +0800152 err = rtsx_pci_write_register(pcr,
Wei WANG773ccdf2013-08-20 14:18:51 +0800153 SD30_DRIVE_SEL, 0x07, pcr->sd30_drive_sel_3v3);
Roger Tseng88a7ee32013-02-04 15:45:58 +0800154 if (err < 0)
155 return err;
Wei WANGd817ac42013-01-23 09:51:04 +0800156 err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4FC0 | 0x24);
157 if (err < 0)
158 return err;
159 } else if (voltage == OUTPUT_1V8) {
Roger Tseng88a7ee32013-02-04 15:45:58 +0800160 err = rtsx_pci_write_register(pcr,
Wei WANG773ccdf2013-08-20 14:18:51 +0800161 SD30_DRIVE_SEL, 0x07, pcr->sd30_drive_sel_1v8);
Roger Tseng88a7ee32013-02-04 15:45:58 +0800162 if (err < 0)
163 return err;
Wei WANGd817ac42013-01-23 09:51:04 +0800164 err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4C40 | 0x24);
165 if (err < 0)
166 return err;
167 } else {
168 return -EINVAL;
169 }
170
171 return 0;
172}
173
Wei WANG67d16a42012-11-09 20:53:33 +0800174static const struct pcr_ops rts5229_pcr_ops = {
Wei WANG773ccdf2013-08-20 14:18:51 +0800175 .fetch_vendor_settings = rts5229_fetch_vendor_settings,
Wei WANG67d16a42012-11-09 20:53:33 +0800176 .extra_init_hw = rts5229_extra_init_hw,
177 .optimize_phy = rts5229_optimize_phy,
178 .turn_on_led = rts5229_turn_on_led,
179 .turn_off_led = rts5229_turn_off_led,
180 .enable_auto_blink = rts5229_enable_auto_blink,
181 .disable_auto_blink = rts5229_disable_auto_blink,
182 .card_power_on = rts5229_card_power_on,
183 .card_power_off = rts5229_card_power_off,
Wei WANGd817ac42013-01-23 09:51:04 +0800184 .switch_output_voltage = rts5229_switch_output_voltage,
Wei WANG67d16a42012-11-09 20:53:33 +0800185 .cd_deglitch = NULL,
Wei WANGab4e8f82013-01-23 09:51:06 +0800186 .conv_clk_and_div_n = NULL,
Wei WANG5947c162013-08-20 14:18:52 +0800187 .force_power_down = rts5229_force_power_down,
Wei WANG67d16a42012-11-09 20:53:33 +0800188};
189
190/* SD Pull Control Enable:
191 * SD_DAT[3:0] ==> pull up
192 * SD_CD ==> pull up
193 * SD_WP ==> pull up
194 * SD_CMD ==> pull up
195 * SD_CLK ==> pull down
196 */
197static const u32 rts5229_sd_pull_ctl_enable_tbl1[] = {
198 RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
199 RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9),
200 0,
201};
202
203/* For RTS5229 version C */
204static const u32 rts5229_sd_pull_ctl_enable_tbl2[] = {
205 RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
206 RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD9),
207 0,
208};
209
210/* SD Pull Control Disable:
211 * SD_DAT[3:0] ==> pull down
212 * SD_CD ==> pull up
213 * SD_WP ==> pull down
214 * SD_CMD ==> pull down
215 * SD_CLK ==> pull down
216 */
217static const u32 rts5229_sd_pull_ctl_disable_tbl1[] = {
218 RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
219 RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5),
220 0,
221};
222
223/* For RTS5229 version C */
224static const u32 rts5229_sd_pull_ctl_disable_tbl2[] = {
225 RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
226 RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE5),
227 0,
228};
229
230/* MS Pull Control Enable:
231 * MS CD ==> pull up
232 * others ==> pull down
233 */
234static const u32 rts5229_ms_pull_ctl_enable_tbl[] = {
235 RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
236 RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
237 0,
238};
239
240/* MS Pull Control Disable:
241 * MS CD ==> pull up
242 * others ==> pull down
243 */
244static const u32 rts5229_ms_pull_ctl_disable_tbl[] = {
245 RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
246 RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
247 0,
248};
249
250void rts5229_init_params(struct rtsx_pcr *pcr)
251{
252 pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104;
253 pcr->num_slots = 2;
254 pcr->ops = &rts5229_pcr_ops;
255
Wei WANG773ccdf2013-08-20 14:18:51 +0800256 pcr->flags = 0;
257 pcr->card_drive_sel = RTSX_CARD_DRIVE_DEFAULT;
258 pcr->sd30_drive_sel_1v8 = DRIVER_TYPE_B;
259 pcr->sd30_drive_sel_3v3 = DRIVER_TYPE_D;
260 pcr->aspm_en = ASPM_L1_EN;
261
Wei WANG67d16a42012-11-09 20:53:33 +0800262 pcr->ic_version = rts5229_get_ic_version(pcr);
263 if (pcr->ic_version == IC_VER_C) {
264 pcr->sd_pull_ctl_enable_tbl = rts5229_sd_pull_ctl_enable_tbl2;
265 pcr->sd_pull_ctl_disable_tbl = rts5229_sd_pull_ctl_disable_tbl2;
266 } else {
267 pcr->sd_pull_ctl_enable_tbl = rts5229_sd_pull_ctl_enable_tbl1;
268 pcr->sd_pull_ctl_disable_tbl = rts5229_sd_pull_ctl_disable_tbl1;
269 }
270 pcr->ms_pull_ctl_enable_tbl = rts5229_ms_pull_ctl_enable_tbl;
271 pcr->ms_pull_ctl_disable_tbl = rts5229_ms_pull_ctl_disable_tbl;
272}