blob: c67935efd4f5b5de9219fd8964039cf1f2afbadb [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 rts5209_get_ic_version(struct rtsx_pcr *pcr)
30{
31 u8 val;
32
33 val = rtsx_pci_readb(pcr, 0x1C);
34 return val & 0x0F;
35}
36
Wei WANG773ccdf2013-08-20 14:18:51 +080037static void rts5209_fetch_vendor_settings(struct rtsx_pcr *pcr)
Wei WANG67d16a42012-11-09 20:53:33 +080038{
Wei WANG773ccdf2013-08-20 14:18:51 +080039 u32 reg;
Wei WANG67d16a42012-11-09 20:53:33 +080040
Wei WANG773ccdf2013-08-20 14:18:51 +080041 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);
Wei WANG67d16a42012-11-09 20:53:33 +080043
Wei WANG773ccdf2013-08-20 14:18:51 +080044 if (rts5209_vendor_setting1_valid(reg)) {
45 if (rts5209_reg_check_ms_pmos(reg))
46 pcr->flags |= PCR_MS_PMOS;
47 pcr->aspm_en = rts5209_reg_to_aspm(reg);
48 }
49
50 rtsx_pci_read_config_dword(pcr, PCR_SETTING_REG2, &reg);
51 dev_dbg(&(pcr->pci->dev), "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG2, reg);
52
53 if (rts5209_vendor_setting2_valid(reg)) {
54 pcr->sd30_drive_sel_1v8 =
55 rts5209_reg_to_sd30_drive_sel_1v8(reg);
56 pcr->sd30_drive_sel_3v3 =
57 rts5209_reg_to_sd30_drive_sel_3v3(reg);
58 pcr->card_drive_sel = rts5209_reg_to_card_drive_sel(reg);
Wei WANG67d16a42012-11-09 20:53:33 +080059 }
60}
61
Wei WANG5947c162013-08-20 14:18:52 +080062static void rts5209_force_power_down(struct rtsx_pcr *pcr)
63{
64 rtsx_pci_write_register(pcr, FPDCTL, 0x07, 0x07);
65}
66
Wei WANG67d16a42012-11-09 20:53:33 +080067static int rts5209_extra_init_hw(struct rtsx_pcr *pcr)
68{
69 rtsx_pci_init_cmd(pcr);
70
71 /* Turn off LED */
72 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_GPIO, 0xFF, 0x03);
73 /* Configure GPIO as output */
74 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_GPIO_DIR, 0xFF, 0x03);
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 rts5209_optimize_phy(struct rtsx_pcr *pcr)
83{
84 return rtsx_pci_write_phy_register(pcr, 0x00, 0xB966);
85}
86
87static int rts5209_turn_on_led(struct rtsx_pcr *pcr)
88{
89 return rtsx_pci_write_register(pcr, CARD_GPIO, 0x01, 0x00);
90}
91
92static int rts5209_turn_off_led(struct rtsx_pcr *pcr)
93{
94 return rtsx_pci_write_register(pcr, CARD_GPIO, 0x01, 0x01);
95}
96
97static int rts5209_enable_auto_blink(struct rtsx_pcr *pcr)
98{
99 return rtsx_pci_write_register(pcr, CARD_AUTO_BLINK, 0xFF, 0x0D);
100}
101
102static int rts5209_disable_auto_blink(struct rtsx_pcr *pcr)
103{
104 return rtsx_pci_write_register(pcr, CARD_AUTO_BLINK, 0x08, 0x00);
105}
106
107static int rts5209_card_power_on(struct rtsx_pcr *pcr, int card)
108{
109 int err;
110 u8 pwr_mask, partial_pwr_on, pwr_on;
111
112 pwr_mask = SD_POWER_MASK;
113 partial_pwr_on = SD_PARTIAL_POWER_ON;
114 pwr_on = SD_POWER_ON;
115
Wei WANG773ccdf2013-08-20 14:18:51 +0800116 if ((pcr->flags & PCR_MS_PMOS) && (card == RTSX_MS_CARD)) {
Wei WANG67d16a42012-11-09 20:53:33 +0800117 pwr_mask = MS_POWER_MASK;
118 partial_pwr_on = MS_PARTIAL_POWER_ON;
119 pwr_on = MS_POWER_ON;
120 }
121
122 rtsx_pci_init_cmd(pcr);
123 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
124 pwr_mask, partial_pwr_on);
125 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
126 LDO3318_PWR_MASK, 0x04);
127 err = rtsx_pci_send_cmd(pcr, 100);
128 if (err < 0)
129 return err;
130
131 /* To avoid too large in-rush current */
132 udelay(150);
133
134 rtsx_pci_init_cmd(pcr);
135 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL, pwr_mask, pwr_on);
136 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
137 LDO3318_PWR_MASK, 0x00);
138 err = rtsx_pci_send_cmd(pcr, 100);
139 if (err < 0)
140 return err;
141
142 return 0;
143}
144
145static int rts5209_card_power_off(struct rtsx_pcr *pcr, int card)
146{
147 u8 pwr_mask, pwr_off;
148
149 pwr_mask = SD_POWER_MASK;
150 pwr_off = SD_POWER_OFF;
151
Wei WANG773ccdf2013-08-20 14:18:51 +0800152 if ((pcr->flags & PCR_MS_PMOS) && (card == RTSX_MS_CARD)) {
Wei WANG67d16a42012-11-09 20:53:33 +0800153 pwr_mask = MS_POWER_MASK;
154 pwr_off = MS_POWER_OFF;
155 }
156
157 rtsx_pci_init_cmd(pcr);
158 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
159 pwr_mask | PMOS_STRG_MASK, pwr_off | PMOS_STRG_400mA);
160 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
Wei WANG773ccdf2013-08-20 14:18:51 +0800161 LDO3318_PWR_MASK, 0x06);
Wei WANG67d16a42012-11-09 20:53:33 +0800162 return rtsx_pci_send_cmd(pcr, 100);
163}
164
Wei WANGd817ac42013-01-23 09:51:04 +0800165static int rts5209_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
166{
167 int err;
168
169 if (voltage == OUTPUT_3V3) {
Roger Tseng88a7ee32013-02-04 15:45:58 +0800170 err = rtsx_pci_write_register(pcr,
Wei WANG773ccdf2013-08-20 14:18:51 +0800171 SD30_DRIVE_SEL, 0x07, pcr->sd30_drive_sel_3v3);
Roger Tseng88a7ee32013-02-04 15:45:58 +0800172 if (err < 0)
173 return err;
Wei WANGd817ac42013-01-23 09:51:04 +0800174 err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4FC0 | 0x24);
175 if (err < 0)
176 return err;
177 } else if (voltage == OUTPUT_1V8) {
Roger Tseng88a7ee32013-02-04 15:45:58 +0800178 err = rtsx_pci_write_register(pcr,
Wei WANG773ccdf2013-08-20 14:18:51 +0800179 SD30_DRIVE_SEL, 0x07, pcr->sd30_drive_sel_1v8);
Roger Tseng88a7ee32013-02-04 15:45:58 +0800180 if (err < 0)
181 return err;
Wei WANGd817ac42013-01-23 09:51:04 +0800182 err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4C40 | 0x24);
183 if (err < 0)
184 return err;
185 } else {
186 return -EINVAL;
187 }
188
189 return 0;
190}
191
Wei WANG67d16a42012-11-09 20:53:33 +0800192static const struct pcr_ops rts5209_pcr_ops = {
Wei WANG773ccdf2013-08-20 14:18:51 +0800193 .fetch_vendor_settings = rts5209_fetch_vendor_settings,
Wei WANG67d16a42012-11-09 20:53:33 +0800194 .extra_init_hw = rts5209_extra_init_hw,
195 .optimize_phy = rts5209_optimize_phy,
196 .turn_on_led = rts5209_turn_on_led,
197 .turn_off_led = rts5209_turn_off_led,
198 .enable_auto_blink = rts5209_enable_auto_blink,
199 .disable_auto_blink = rts5209_disable_auto_blink,
200 .card_power_on = rts5209_card_power_on,
201 .card_power_off = rts5209_card_power_off,
Wei WANGd817ac42013-01-23 09:51:04 +0800202 .switch_output_voltage = rts5209_switch_output_voltage,
Wei WANG67d16a42012-11-09 20:53:33 +0800203 .cd_deglitch = NULL,
Wei WANGab4e8f82013-01-23 09:51:06 +0800204 .conv_clk_and_div_n = NULL,
Wei WANG5947c162013-08-20 14:18:52 +0800205 .force_power_down = rts5209_force_power_down,
Wei WANG67d16a42012-11-09 20:53:33 +0800206};
207
208/* SD Pull Control Enable:
209 * SD_DAT[3:0] ==> pull up
210 * SD_CD ==> pull up
211 * SD_WP ==> pull up
212 * SD_CMD ==> pull up
213 * SD_CLK ==> pull down
214 */
215static const u32 rts5209_sd_pull_ctl_enable_tbl[] = {
216 RTSX_REG_PAIR(CARD_PULL_CTL1, 0xAA),
217 RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
218 RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9),
219 0,
220};
221
222/* SD Pull Control Disable:
223 * SD_DAT[3:0] ==> pull down
224 * SD_CD ==> pull up
225 * SD_WP ==> pull down
226 * SD_CMD ==> pull down
227 * SD_CLK ==> pull down
228 */
229static const u32 rts5209_sd_pull_ctl_disable_tbl[] = {
230 RTSX_REG_PAIR(CARD_PULL_CTL1, 0x55),
231 RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
232 RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5),
233 0,
234};
235
236/* MS Pull Control Enable:
237 * MS CD ==> pull up
238 * others ==> pull down
239 */
240static const u32 rts5209_ms_pull_ctl_enable_tbl[] = {
241 RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
242 RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
243 RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
244 0,
245};
246
247/* MS Pull Control Disable:
248 * MS CD ==> pull up
249 * others ==> pull down
250 */
251static const u32 rts5209_ms_pull_ctl_disable_tbl[] = {
252 RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
253 RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
254 RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
255 0,
256};
257
258void rts5209_init_params(struct rtsx_pcr *pcr)
259{
260 pcr->extra_caps = EXTRA_CAPS_SD_SDR50 |
261 EXTRA_CAPS_SD_SDR104 | EXTRA_CAPS_MMC_8BIT;
262 pcr->num_slots = 2;
263 pcr->ops = &rts5209_pcr_ops;
264
Wei WANG773ccdf2013-08-20 14:18:51 +0800265 pcr->flags = 0;
266 pcr->card_drive_sel = RTS5209_CARD_DRIVE_DEFAULT;
267 pcr->sd30_drive_sel_1v8 = DRIVER_TYPE_B;
268 pcr->sd30_drive_sel_3v3 = DRIVER_TYPE_D;
269 pcr->aspm_en = ASPM_L1_EN;
Wei WANG67d16a42012-11-09 20:53:33 +0800270
271 pcr->ic_version = rts5209_get_ic_version(pcr);
272 pcr->sd_pull_ctl_enable_tbl = rts5209_sd_pull_ctl_enable_tbl;
273 pcr->sd_pull_ctl_disable_tbl = rts5209_sd_pull_ctl_disable_tbl;
274 pcr->ms_pull_ctl_enable_tbl = rts5209_ms_pull_ctl_enable_tbl;
275 pcr->ms_pull_ctl_disable_tbl = rts5209_ms_pull_ctl_disable_tbl;
276}