1 /* Driver for Realtek PCI-Express card reader
3 * Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved.
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
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.
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/>.
19 * Wei WANG <wei_wang@realsil.com.cn>
20 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
23 #include <linux/module.h>
24 #include <linux/bitops.h>
25 #include <linux/delay.h>
26 #include <linux/mfd/rtsx_pci.h>
30 static u8 rtl8411_get_ic_version(struct rtsx_pcr *pcr)
34 rtsx_pci_read_register(pcr, SYS_VER, &val);
38 static int rtl8411_extra_init_hw(struct rtsx_pcr *pcr)
40 return rtsx_pci_write_register(pcr, CD_PAD_CTL,
41 CD_DISABLE_MASK | CD_AUTO_DISABLE, CD_ENABLE);
44 static int rtl8411_turn_on_led(struct rtsx_pcr *pcr)
46 return rtsx_pci_write_register(pcr, CARD_GPIO, 0x01, 0x00);
49 static int rtl8411_turn_off_led(struct rtsx_pcr *pcr)
51 return rtsx_pci_write_register(pcr, CARD_GPIO, 0x01, 0x01);
54 static int rtl8411_enable_auto_blink(struct rtsx_pcr *pcr)
56 return rtsx_pci_write_register(pcr, CARD_AUTO_BLINK, 0xFF, 0x0D);
59 static int rtl8411_disable_auto_blink(struct rtsx_pcr *pcr)
61 return rtsx_pci_write_register(pcr, CARD_AUTO_BLINK, 0x08, 0x00);
64 static int rtl8411_card_power_on(struct rtsx_pcr *pcr, int card)
68 rtsx_pci_init_cmd(pcr);
69 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
70 BPP_POWER_MASK, BPP_POWER_5_PERCENT_ON);
71 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_CTL,
72 BPP_LDO_POWB, BPP_LDO_SUSPEND);
73 err = rtsx_pci_send_cmd(pcr, 100);
77 /* To avoid too large in-rush current */
80 err = rtsx_pci_write_register(pcr, CARD_PWR_CTL,
81 BPP_POWER_MASK, BPP_POWER_10_PERCENT_ON);
87 err = rtsx_pci_write_register(pcr, CARD_PWR_CTL,
88 BPP_POWER_MASK, BPP_POWER_15_PERCENT_ON);
94 err = rtsx_pci_write_register(pcr, CARD_PWR_CTL,
95 BPP_POWER_MASK, BPP_POWER_ON);
99 return rtsx_pci_write_register(pcr, LDO_CTL, BPP_LDO_POWB, BPP_LDO_ON);
102 static int rtl8411_card_power_off(struct rtsx_pcr *pcr, int card)
106 err = rtsx_pci_write_register(pcr, CARD_PWR_CTL,
107 BPP_POWER_MASK, BPP_POWER_OFF);
111 return rtsx_pci_write_register(pcr, LDO_CTL,
112 BPP_LDO_POWB, BPP_LDO_SUSPEND);
115 static int rtl8411_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
119 mask = (BPP_REG_TUNED18 << BPP_TUNED18_SHIFT_8411) | BPP_PAD_MASK;
120 if (voltage == OUTPUT_3V3)
121 val = (BPP_ASIC_3V3 << BPP_TUNED18_SHIFT_8411) | BPP_PAD_3V3;
122 else if (voltage == OUTPUT_1V8)
123 val = (BPP_ASIC_1V8 << BPP_TUNED18_SHIFT_8411) | BPP_PAD_1V8;
127 return rtsx_pci_write_register(pcr, LDO_CTL, mask, val);
130 static unsigned int rtl8411_cd_deglitch(struct rtsx_pcr *pcr)
132 unsigned int card_exist;
134 card_exist = rtsx_pci_readl(pcr, RTSX_BIPR);
135 card_exist &= CARD_EXIST;
138 rtsx_pci_write_register(pcr, CD_PAD_CTL,
139 CD_DISABLE_MASK, CD_ENABLE);
140 /* Enable card interrupt */
141 rtsx_pci_write_register(pcr, EFUSE_CONTENT, 0xe0, 0x00);
145 if (hweight32(card_exist) > 1) {
146 rtsx_pci_write_register(pcr, CARD_PWR_CTL,
147 BPP_POWER_MASK, BPP_POWER_5_PERCENT_ON);
150 card_exist = rtsx_pci_readl(pcr, RTSX_BIPR);
151 if (card_exist & MS_EXIST)
152 card_exist = MS_EXIST;
153 else if (card_exist & SD_EXIST)
154 card_exist = SD_EXIST;
158 rtsx_pci_write_register(pcr, CARD_PWR_CTL,
159 BPP_POWER_MASK, BPP_POWER_OFF);
161 dev_dbg(&(pcr->pci->dev),
162 "After CD deglitch, card_exist = 0x%x\n",
166 if (card_exist & MS_EXIST) {
167 /* Disable SD interrupt */
168 rtsx_pci_write_register(pcr, EFUSE_CONTENT, 0xe0, 0x40);
169 rtsx_pci_write_register(pcr, CD_PAD_CTL,
170 CD_DISABLE_MASK, MS_CD_EN_ONLY);
171 } else if (card_exist & SD_EXIST) {
172 /* Disable MS interrupt */
173 rtsx_pci_write_register(pcr, EFUSE_CONTENT, 0xe0, 0x80);
174 rtsx_pci_write_register(pcr, CD_PAD_CTL,
175 CD_DISABLE_MASK, SD_CD_EN_ONLY);
181 static int rtl8411_conv_clk_and_div_n(int input, int dir)
185 if (dir == CLK_TO_DIV_N)
186 output = input * 4 / 5 - 2;
188 output = (input + 2) * 5 / 4;
193 static const struct pcr_ops rtl8411_pcr_ops = {
194 .extra_init_hw = rtl8411_extra_init_hw,
195 .optimize_phy = NULL,
196 .turn_on_led = rtl8411_turn_on_led,
197 .turn_off_led = rtl8411_turn_off_led,
198 .enable_auto_blink = rtl8411_enable_auto_blink,
199 .disable_auto_blink = rtl8411_disable_auto_blink,
200 .card_power_on = rtl8411_card_power_on,
201 .card_power_off = rtl8411_card_power_off,
202 .switch_output_voltage = rtl8411_switch_output_voltage,
203 .cd_deglitch = rtl8411_cd_deglitch,
204 .conv_clk_and_div_n = rtl8411_conv_clk_and_div_n,
207 /* SD Pull Control Enable:
208 * SD_DAT[3:0] ==> pull up
212 * SD_CLK ==> pull down
214 static const u32 rtl8411_sd_pull_ctl_enable_tbl[] = {
215 RTSX_REG_PAIR(CARD_PULL_CTL1, 0xAA),
216 RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
217 RTSX_REG_PAIR(CARD_PULL_CTL3, 0xA9),
218 RTSX_REG_PAIR(CARD_PULL_CTL4, 0x09),
219 RTSX_REG_PAIR(CARD_PULL_CTL5, 0x09),
220 RTSX_REG_PAIR(CARD_PULL_CTL6, 0x04),
224 /* SD Pull Control Disable:
225 * SD_DAT[3:0] ==> pull down
227 * SD_WP ==> pull down
228 * SD_CMD ==> pull down
229 * SD_CLK ==> pull down
231 static const u32 rtl8411_sd_pull_ctl_disable_tbl[] = {
232 RTSX_REG_PAIR(CARD_PULL_CTL1, 0x65),
233 RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
234 RTSX_REG_PAIR(CARD_PULL_CTL3, 0x95),
235 RTSX_REG_PAIR(CARD_PULL_CTL4, 0x09),
236 RTSX_REG_PAIR(CARD_PULL_CTL5, 0x05),
237 RTSX_REG_PAIR(CARD_PULL_CTL6, 0x04),
241 /* MS Pull Control Enable:
243 * others ==> pull down
245 static const u32 rtl8411_ms_pull_ctl_enable_tbl[] = {
246 RTSX_REG_PAIR(CARD_PULL_CTL1, 0x65),
247 RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
248 RTSX_REG_PAIR(CARD_PULL_CTL3, 0x95),
249 RTSX_REG_PAIR(CARD_PULL_CTL4, 0x05),
250 RTSX_REG_PAIR(CARD_PULL_CTL5, 0x05),
251 RTSX_REG_PAIR(CARD_PULL_CTL6, 0x04),
255 /* MS Pull Control Disable:
257 * others ==> pull down
259 static const u32 rtl8411_ms_pull_ctl_disable_tbl[] = {
260 RTSX_REG_PAIR(CARD_PULL_CTL1, 0x65),
261 RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
262 RTSX_REG_PAIR(CARD_PULL_CTL3, 0x95),
263 RTSX_REG_PAIR(CARD_PULL_CTL4, 0x09),
264 RTSX_REG_PAIR(CARD_PULL_CTL5, 0x05),
265 RTSX_REG_PAIR(CARD_PULL_CTL6, 0x04),
269 void rtl8411_init_params(struct rtsx_pcr *pcr)
271 pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104;
273 pcr->ops = &rtl8411_pcr_ops;
275 pcr->ic_version = rtl8411_get_ic_version(pcr);
276 pcr->sd_pull_ctl_enable_tbl = rtl8411_sd_pull_ctl_enable_tbl;
277 pcr->sd_pull_ctl_disable_tbl = rtl8411_sd_pull_ctl_disable_tbl;
278 pcr->ms_pull_ctl_enable_tbl = rtl8411_ms_pull_ctl_enable_tbl;
279 pcr->ms_pull_ctl_disable_tbl = rtl8411_ms_pull_ctl_disable_tbl;