2 * Copyright (C) 2013 BayHub Technology Ltd.
4 * Authors: Peter Guo <peter.guo@bayhubtech.com>
5 * Adam Lee <adam.lee@canonical.com>
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/pci.h>
21 #include "sdhci-pci.h"
22 #include "sdhci-pci-o2micro.h"
24 static void o2_pci_set_baseclk(struct sdhci_pci_chip *chip, u32 value)
27 pci_read_config_dword(chip->pdev,
28 O2_SD_PLL_SETTING, &scratch_32);
30 scratch_32 &= 0x0000FFFF;
33 pci_write_config_dword(chip->pdev,
34 O2_SD_PLL_SETTING, scratch_32);
37 static void o2_pci_led_enable(struct sdhci_pci_chip *chip)
42 /* Set led of SD host function enable */
43 ret = pci_read_config_dword(chip->pdev,
44 O2_SD_FUNC_REG0, &scratch_32);
48 scratch_32 &= ~O2_SD_FREG0_LEDOFF;
49 pci_write_config_dword(chip->pdev,
50 O2_SD_FUNC_REG0, scratch_32);
52 ret = pci_read_config_dword(chip->pdev,
53 O2_SD_TEST_REG, &scratch_32);
57 scratch_32 |= O2_SD_LED_ENABLE;
58 pci_write_config_dword(chip->pdev,
59 O2_SD_TEST_REG, scratch_32);
63 void sdhci_pci_o2_fujin2_pci_init(struct sdhci_pci_chip *chip)
67 /* Improve write performance for SD3.0 */
68 ret = pci_read_config_dword(chip->pdev, O2_SD_DEV_CTRL, &scratch_32);
71 scratch_32 &= ~((1 << 12) | (1 << 13) | (1 << 14));
72 pci_write_config_dword(chip->pdev, O2_SD_DEV_CTRL, scratch_32);
74 /* Enable Link abnormal reset generating Reset */
75 ret = pci_read_config_dword(chip->pdev, O2_SD_MISC_REG5, &scratch_32);
78 scratch_32 &= ~((1 << 19) | (1 << 11));
79 scratch_32 |= (1 << 10);
80 pci_write_config_dword(chip->pdev, O2_SD_MISC_REG5, scratch_32);
82 /* set card power over current protection */
83 ret = pci_read_config_dword(chip->pdev, O2_SD_TEST_REG, &scratch_32);
86 scratch_32 |= (1 << 4);
87 pci_write_config_dword(chip->pdev, O2_SD_TEST_REG, scratch_32);
89 /* adjust the output delay for SD mode */
90 pci_write_config_dword(chip->pdev, O2_SD_DELAY_CTRL, 0x00002492);
92 /* Set the output voltage setting of Aux 1.2v LDO */
93 ret = pci_read_config_dword(chip->pdev, O2_SD_LD0_CTRL, &scratch_32);
96 scratch_32 &= ~(3 << 12);
97 pci_write_config_dword(chip->pdev, O2_SD_LD0_CTRL, scratch_32);
99 /* Set Max power supply capability of SD host */
100 ret = pci_read_config_dword(chip->pdev, O2_SD_CAP_REG0, &scratch_32);
103 scratch_32 &= ~(0x01FE);
104 scratch_32 |= 0x00CC;
105 pci_write_config_dword(chip->pdev, O2_SD_CAP_REG0, scratch_32);
106 /* Set DLL Tuning Window */
107 ret = pci_read_config_dword(chip->pdev,
108 O2_SD_TUNING_CTRL, &scratch_32);
111 scratch_32 &= ~(0x000000FF);
112 scratch_32 |= 0x00000066;
113 pci_write_config_dword(chip->pdev, O2_SD_TUNING_CTRL, scratch_32);
115 /* Set UHS2 T_EIDLE */
116 ret = pci_read_config_dword(chip->pdev,
117 O2_SD_UHS2_L1_CTRL, &scratch_32);
120 scratch_32 &= ~(0x000000FC);
121 scratch_32 |= 0x00000084;
122 pci_write_config_dword(chip->pdev, O2_SD_UHS2_L1_CTRL, scratch_32);
124 /* Set UHS2 Termination */
125 ret = pci_read_config_dword(chip->pdev, O2_SD_FUNC_REG3, &scratch_32);
128 scratch_32 &= ~((1 << 21) | (1 << 30));
130 /* Set RTD3 function disabled */
131 scratch_32 |= ((1 << 29) | (1 << 28));
132 pci_write_config_dword(chip->pdev, O2_SD_FUNC_REG3, scratch_32);
134 /* Set L1 Entrance Timer */
135 ret = pci_read_config_dword(chip->pdev, O2_SD_CAPS, &scratch_32);
138 scratch_32 &= ~(0xf0000000);
139 scratch_32 |= 0x30000000;
140 pci_write_config_dword(chip->pdev, O2_SD_CAPS, scratch_32);
142 ret = pci_read_config_dword(chip->pdev,
143 O2_SD_MISC_CTRL4, &scratch_32);
146 scratch_32 &= ~(0x000f0000);
147 scratch_32 |= 0x00080000;
148 pci_write_config_dword(chip->pdev, O2_SD_MISC_CTRL4, scratch_32);
150 EXPORT_SYMBOL_GPL(sdhci_pci_o2_fujin2_pci_init);
152 int sdhci_pci_o2_probe_slot(struct sdhci_pci_slot *slot)
154 struct sdhci_pci_chip *chip;
155 struct sdhci_host *host;
160 switch (chip->pdev->device) {
161 case PCI_DEVICE_ID_O2_SDS0:
162 case PCI_DEVICE_ID_O2_SEABIRD0:
163 case PCI_DEVICE_ID_O2_SEABIRD1:
164 case PCI_DEVICE_ID_O2_SDS1:
165 case PCI_DEVICE_ID_O2_FUJIN2:
166 reg = sdhci_readl(host, O2_SD_VENDOR_SETTING);
168 host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
170 if (chip->pdev->device != PCI_DEVICE_ID_O2_FUJIN2)
172 /* set dll watch dog timer */
173 reg = sdhci_readl(host, O2_SD_VENDOR_SETTING2);
175 sdhci_writel(host, reg, O2_SD_VENDOR_SETTING2);
184 EXPORT_SYMBOL_GPL(sdhci_pci_o2_probe_slot);
186 int sdhci_pci_o2_probe(struct sdhci_pci_chip *chip)
192 switch (chip->pdev->device) {
193 case PCI_DEVICE_ID_O2_8220:
194 case PCI_DEVICE_ID_O2_8221:
195 case PCI_DEVICE_ID_O2_8320:
196 case PCI_DEVICE_ID_O2_8321:
197 /* This extra setup is required due to broken ADMA. */
198 ret = pci_read_config_byte(chip->pdev,
199 O2_SD_LOCK_WP, &scratch);
203 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
205 /* Set Multi 3 to VCC3V# */
206 pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
208 /* Disable CLK_REQ# support after media DET */
209 ret = pci_read_config_byte(chip->pdev,
210 O2_SD_CLKREQ, &scratch);
214 pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
216 /* Choose capabilities, enable SDMA. We have to write 0x01
217 * to the capabilities register first to unlock it.
219 ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
223 pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
224 pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
226 /* Disable ADMA1/2 */
227 pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
228 pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
230 /* Disable the infinite transfer mode */
231 ret = pci_read_config_byte(chip->pdev,
232 O2_SD_INF_MOD, &scratch);
236 pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
239 ret = pci_read_config_byte(chip->pdev,
240 O2_SD_LOCK_WP, &scratch);
244 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
246 case PCI_DEVICE_ID_O2_SDS0:
247 case PCI_DEVICE_ID_O2_SDS1:
248 case PCI_DEVICE_ID_O2_FUJIN2:
250 ret = pci_read_config_byte(chip->pdev,
251 O2_SD_LOCK_WP, &scratch);
256 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
258 /* DevId=8520 subId= 0x11 or 0x12 Type Chip support */
259 if (chip->pdev->device == PCI_DEVICE_ID_O2_FUJIN2) {
260 ret = pci_read_config_dword(chip->pdev,
263 scratch_32 = ((scratch_32 & 0xFF000000) >> 24);
265 /* Check Whether subId is 0x11 or 0x12 */
266 if ((scratch_32 == 0x11) || (scratch_32 == 0x12)) {
267 scratch_32 = 0x2c280000;
269 /* Set Base Clock to 208MZ */
270 o2_pci_set_baseclk(chip, scratch_32);
271 ret = pci_read_config_dword(chip->pdev,
275 /* Enable Base Clk setting change */
276 scratch_32 |= O2_SD_FREG4_ENABLE_CLK_SET;
277 pci_write_config_dword(chip->pdev,
281 /* Set Tuning Window to 4 */
282 pci_write_config_byte(chip->pdev,
283 O2_SD_TUNING_CTRL, 0x44);
289 /* Enable 8520 led function */
290 o2_pci_led_enable(chip);
292 /* Set timeout CLK */
293 ret = pci_read_config_dword(chip->pdev,
294 O2_SD_CLK_SETTING, &scratch_32);
298 scratch_32 &= ~(0xFF00);
299 scratch_32 |= 0x07E0C800;
300 pci_write_config_dword(chip->pdev,
301 O2_SD_CLK_SETTING, scratch_32);
303 ret = pci_read_config_dword(chip->pdev,
304 O2_SD_CLKREQ, &scratch_32);
308 pci_write_config_dword(chip->pdev, O2_SD_CLKREQ, scratch_32);
310 ret = pci_read_config_dword(chip->pdev,
311 O2_SD_PLL_SETTING, &scratch_32);
315 scratch_32 &= ~(0x1F3F070E);
316 scratch_32 |= 0x18270106;
317 pci_write_config_dword(chip->pdev,
318 O2_SD_PLL_SETTING, scratch_32);
320 /* Disable UHS1 funciton */
321 ret = pci_read_config_dword(chip->pdev,
322 O2_SD_CAP_REG2, &scratch_32);
325 scratch_32 &= ~(0xE0);
326 pci_write_config_dword(chip->pdev,
327 O2_SD_CAP_REG2, scratch_32);
329 if (chip->pdev->device == PCI_DEVICE_ID_O2_FUJIN2)
330 sdhci_pci_o2_fujin2_pci_init(chip);
333 ret = pci_read_config_byte(chip->pdev,
334 O2_SD_LOCK_WP, &scratch);
338 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
340 case PCI_DEVICE_ID_O2_SEABIRD0:
341 case PCI_DEVICE_ID_O2_SEABIRD1:
343 ret = pci_read_config_byte(chip->pdev,
344 O2_SD_LOCK_WP, &scratch);
349 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
351 ret = pci_read_config_dword(chip->pdev,
352 O2_SD_PLL_SETTING, &scratch_32);
354 if ((scratch_32 & 0xff000000) == 0x01000000) {
355 scratch_32 &= 0x0000FFFF;
356 scratch_32 |= 0x1F340000;
358 pci_write_config_dword(chip->pdev,
359 O2_SD_PLL_SETTING, scratch_32);
361 scratch_32 &= 0x0000FFFF;
362 scratch_32 |= 0x2c280000;
364 pci_write_config_dword(chip->pdev,
365 O2_SD_PLL_SETTING, scratch_32);
367 ret = pci_read_config_dword(chip->pdev,
370 scratch_32 |= (1 << 22);
371 pci_write_config_dword(chip->pdev,
372 O2_SD_FUNC_REG4, scratch_32);
375 /* Set Tuning Windows to 5 */
376 pci_write_config_byte(chip->pdev,
377 O2_SD_TUNING_CTRL, 0x55);
379 ret = pci_read_config_byte(chip->pdev,
380 O2_SD_LOCK_WP, &scratch);
384 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
390 EXPORT_SYMBOL_GPL(sdhci_pci_o2_probe);
392 int sdhci_pci_o2_resume(struct sdhci_pci_chip *chip)
394 sdhci_pci_o2_probe(chip);
397 EXPORT_SYMBOL_GPL(sdhci_pci_o2_resume);