blob: 7a1ad6dd29179ddb522b287b5a2165c6f8a39705 [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 WANG67d16a42012-11-09 20:53:33 +080059static int rts5229_extra_init_hw(struct rtsx_pcr *pcr)
60{
61 rtsx_pci_init_cmd(pcr);
62
63 /* Configure GPIO as output */
64 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, GPIO_CTL, 0x02, 0x02);
65 /* Switch LDO3318 source from DV33 to card_3v3 */
66 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x00);
67 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x01);
68 /* LED shine disabled, set initial shine cycle period */
69 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OLT_LED_CTL, 0x0F, 0x02);
Wei WANG773ccdf2013-08-20 14:18:51 +080070 /* Configure driving */
71 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_DRIVE_SEL,
72 0xFF, pcr->sd30_drive_sel_3v3);
Wei WANG67d16a42012-11-09 20:53:33 +080073
74 return rtsx_pci_send_cmd(pcr, 100);
75}
76
77static int rts5229_optimize_phy(struct rtsx_pcr *pcr)
78{
79 /* Optimize RX sensitivity */
80 return rtsx_pci_write_phy_register(pcr, 0x00, 0xBA42);
81}
82
83static int rts5229_turn_on_led(struct rtsx_pcr *pcr)
84{
85 return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x02);
86}
87
88static int rts5229_turn_off_led(struct rtsx_pcr *pcr)
89{
90 return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x00);
91}
92
93static int rts5229_enable_auto_blink(struct rtsx_pcr *pcr)
94{
95 return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x08);
96}
97
98static int rts5229_disable_auto_blink(struct rtsx_pcr *pcr)
99{
100 return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x00);
101}
102
103static int rts5229_card_power_on(struct rtsx_pcr *pcr, int card)
104{
105 int err;
106
107 rtsx_pci_init_cmd(pcr);
108 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
109 SD_POWER_MASK, SD_PARTIAL_POWER_ON);
110 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
111 LDO3318_PWR_MASK, 0x02);
112 err = rtsx_pci_send_cmd(pcr, 100);
113 if (err < 0)
114 return err;
115
116 /* To avoid too large in-rush current */
117 udelay(150);
118
119 rtsx_pci_init_cmd(pcr);
120 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
121 SD_POWER_MASK, SD_POWER_ON);
122 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
123 LDO3318_PWR_MASK, 0x06);
124 err = rtsx_pci_send_cmd(pcr, 100);
125 if (err < 0)
126 return err;
127
128 return 0;
129}
130
131static int rts5229_card_power_off(struct rtsx_pcr *pcr, int card)
132{
133 rtsx_pci_init_cmd(pcr);
134 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
135 SD_POWER_MASK | PMOS_STRG_MASK,
136 SD_POWER_OFF | PMOS_STRG_400mA);
137 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
Wei WANG773ccdf2013-08-20 14:18:51 +0800138 LDO3318_PWR_MASK, 0x00);
Wei WANG67d16a42012-11-09 20:53:33 +0800139 return rtsx_pci_send_cmd(pcr, 100);
140}
141
Wei WANGd817ac42013-01-23 09:51:04 +0800142static int rts5229_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
143{
144 int err;
145
146 if (voltage == OUTPUT_3V3) {
Roger Tseng88a7ee32013-02-04 15:45:58 +0800147 err = rtsx_pci_write_register(pcr,
Wei WANG773ccdf2013-08-20 14:18:51 +0800148 SD30_DRIVE_SEL, 0x07, pcr->sd30_drive_sel_3v3);
Roger Tseng88a7ee32013-02-04 15:45:58 +0800149 if (err < 0)
150 return err;
Wei WANGd817ac42013-01-23 09:51:04 +0800151 err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4FC0 | 0x24);
152 if (err < 0)
153 return err;
154 } else if (voltage == OUTPUT_1V8) {
Roger Tseng88a7ee32013-02-04 15:45:58 +0800155 err = rtsx_pci_write_register(pcr,
Wei WANG773ccdf2013-08-20 14:18:51 +0800156 SD30_DRIVE_SEL, 0x07, pcr->sd30_drive_sel_1v8);
Roger Tseng88a7ee32013-02-04 15:45:58 +0800157 if (err < 0)
158 return err;
Wei WANGd817ac42013-01-23 09:51:04 +0800159 err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4C40 | 0x24);
160 if (err < 0)
161 return err;
162 } else {
163 return -EINVAL;
164 }
165
166 return 0;
167}
168
Wei WANG67d16a42012-11-09 20:53:33 +0800169static const struct pcr_ops rts5229_pcr_ops = {
Wei WANG773ccdf2013-08-20 14:18:51 +0800170 .fetch_vendor_settings = rts5229_fetch_vendor_settings,
Wei WANG67d16a42012-11-09 20:53:33 +0800171 .extra_init_hw = rts5229_extra_init_hw,
172 .optimize_phy = rts5229_optimize_phy,
173 .turn_on_led = rts5229_turn_on_led,
174 .turn_off_led = rts5229_turn_off_led,
175 .enable_auto_blink = rts5229_enable_auto_blink,
176 .disable_auto_blink = rts5229_disable_auto_blink,
177 .card_power_on = rts5229_card_power_on,
178 .card_power_off = rts5229_card_power_off,
Wei WANGd817ac42013-01-23 09:51:04 +0800179 .switch_output_voltage = rts5229_switch_output_voltage,
Wei WANG67d16a42012-11-09 20:53:33 +0800180 .cd_deglitch = NULL,
Wei WANGab4e8f82013-01-23 09:51:06 +0800181 .conv_clk_and_div_n = NULL,
Wei WANG67d16a42012-11-09 20:53:33 +0800182};
183
184/* SD Pull Control Enable:
185 * SD_DAT[3:0] ==> pull up
186 * SD_CD ==> pull up
187 * SD_WP ==> pull up
188 * SD_CMD ==> pull up
189 * SD_CLK ==> pull down
190 */
191static const u32 rts5229_sd_pull_ctl_enable_tbl1[] = {
192 RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
193 RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9),
194 0,
195};
196
197/* For RTS5229 version C */
198static const u32 rts5229_sd_pull_ctl_enable_tbl2[] = {
199 RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
200 RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD9),
201 0,
202};
203
204/* SD Pull Control Disable:
205 * SD_DAT[3:0] ==> pull down
206 * SD_CD ==> pull up
207 * SD_WP ==> pull down
208 * SD_CMD ==> pull down
209 * SD_CLK ==> pull down
210 */
211static const u32 rts5229_sd_pull_ctl_disable_tbl1[] = {
212 RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
213 RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5),
214 0,
215};
216
217/* For RTS5229 version C */
218static const u32 rts5229_sd_pull_ctl_disable_tbl2[] = {
219 RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
220 RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE5),
221 0,
222};
223
224/* MS Pull Control Enable:
225 * MS CD ==> pull up
226 * others ==> pull down
227 */
228static const u32 rts5229_ms_pull_ctl_enable_tbl[] = {
229 RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
230 RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
231 0,
232};
233
234/* MS Pull Control Disable:
235 * MS CD ==> pull up
236 * others ==> pull down
237 */
238static const u32 rts5229_ms_pull_ctl_disable_tbl[] = {
239 RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
240 RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
241 0,
242};
243
244void rts5229_init_params(struct rtsx_pcr *pcr)
245{
246 pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104;
247 pcr->num_slots = 2;
248 pcr->ops = &rts5229_pcr_ops;
249
Wei WANG773ccdf2013-08-20 14:18:51 +0800250 pcr->flags = 0;
251 pcr->card_drive_sel = RTSX_CARD_DRIVE_DEFAULT;
252 pcr->sd30_drive_sel_1v8 = DRIVER_TYPE_B;
253 pcr->sd30_drive_sel_3v3 = DRIVER_TYPE_D;
254 pcr->aspm_en = ASPM_L1_EN;
255
Wei WANG67d16a42012-11-09 20:53:33 +0800256 pcr->ic_version = rts5229_get_ic_version(pcr);
257 if (pcr->ic_version == IC_VER_C) {
258 pcr->sd_pull_ctl_enable_tbl = rts5229_sd_pull_ctl_enable_tbl2;
259 pcr->sd_pull_ctl_disable_tbl = rts5229_sd_pull_ctl_disable_tbl2;
260 } else {
261 pcr->sd_pull_ctl_enable_tbl = rts5229_sd_pull_ctl_enable_tbl1;
262 pcr->sd_pull_ctl_disable_tbl = rts5229_sd_pull_ctl_disable_tbl1;
263 }
264 pcr->ms_pull_ctl_enable_tbl = rts5229_ms_pull_ctl_enable_tbl;
265 pcr->ms_pull_ctl_disable_tbl = rts5229_ms_pull_ctl_disable_tbl;
266}