1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 BayHub Technology Ltd.
5 * Authors: Peter Guo <peter.guo@bayhubtech.com>
6 * Adam Lee <adam.lee@canonical.com>
7 * Ernest Zhang <ernest.zhang@bayhubtech.com>
10 #include <linux/pci.h>
11 #include <linux/mmc/host.h>
12 #include <linux/mmc/mmc.h>
13 #include <linux/delay.h>
14 #include <linux/iopoll.h>
15 #include <linux/bitfield.h>
18 #include "sdhci-pci.h"
21 * O2Micro device registers
24 #define O2_SD_MISC_REG5 0x64
25 #define O2_SD_LD0_CTRL 0x68
26 #define O2_SD_DEV_CTRL 0x88
27 #define O2_SD_LOCK_WP 0xD3
28 #define O2_SD_TEST_REG 0xD4
29 #define O2_SD_FUNC_REG0 0xDC
30 #define O2_SD_MULTI_VCC3V 0xEE
31 #define O2_SD_CLKREQ 0xEC
32 #define O2_SD_CAPS 0xE0
33 #define O2_SD_ADMA1 0xE2
34 #define O2_SD_ADMA2 0xE7
35 #define O2_SD_INF_MOD 0xF1
36 #define O2_SD_MISC_CTRL4 0xFC
37 #define O2_SD_MISC_CTRL 0x1C0
38 #define O2_SD_PWR_FORCE_L0 0x0002
39 #define O2_SD_TUNING_CTRL 0x300
40 #define O2_SD_PLL_SETTING 0x304
41 #define O2_SD_MISC_SETTING 0x308
42 #define O2_SD_CLK_SETTING 0x328
43 #define O2_SD_CAP_REG2 0x330
44 #define O2_SD_CAP_REG0 0x334
45 #define O2_SD_UHS1_CAP_SETTING 0x33C
46 #define O2_SD_DELAY_CTRL 0x350
47 #define O2_SD_OUTPUT_CLK_SOURCE_SWITCH 0x354
48 #define O2_SD_UHS2_L1_CTRL 0x35C
49 #define O2_SD_FUNC_REG3 0x3E0
50 #define O2_SD_FUNC_REG4 0x3E4
51 #define O2_SD_LED_ENABLE BIT(6)
52 #define O2_SD_FREG0_LEDOFF BIT(13)
53 #define O2_SD_SEL_DLL BIT(16)
54 #define O2_SD_FREG4_ENABLE_CLK_SET BIT(22)
55 #define O2_SD_PHASE_MASK GENMASK(23, 20)
56 #define O2_SD_FIX_PHASE FIELD_PREP(O2_SD_PHASE_MASK, 0x9)
58 #define O2_SD_VENDOR_SETTING 0x110
59 #define O2_SD_VENDOR_SETTING2 0x1C8
60 #define O2_SD_HW_TUNING_DISABLE BIT(4)
62 #define O2_PLL_DLL_WDT_CONTROL1 0x1CC
63 #define O2_PLL_FORCE_ACTIVE BIT(18)
64 #define O2_PLL_LOCK_STATUS BIT(14)
65 #define O2_PLL_SOFT_RESET BIT(12)
66 #define O2_DLL_LOCK_STATUS BIT(11)
68 #define O2_SD_DETECT_SETTING 0x324
70 static const u32 dmdn_table[] = {0x2B1C0000,
71 0x2C1A0000, 0x371B0000, 0x35100000};
72 #define DMDN_SZ ARRAY_SIZE(dmdn_table)
78 static void sdhci_o2_wait_card_detect_stable(struct sdhci_host *host)
84 timeout = ktime_add_ms(ktime_get(), 50);
86 bool timedout = ktime_after(ktime_get(), timeout);
88 scratch32 = sdhci_readl(host, SDHCI_PRESENT_STATE);
89 if ((scratch32 & SDHCI_CARD_PRESENT) >> SDHCI_CARD_PRES_SHIFT
90 == (scratch32 & SDHCI_CD_LVL) >> SDHCI_CD_LVL_SHIFT)
94 pr_err("%s: Card Detect debounce never finished.\n",
95 mmc_hostname(host->mmc));
103 static void sdhci_o2_enable_internal_clock(struct sdhci_host *host)
109 /* PLL software reset */
110 scratch32 = sdhci_readl(host, O2_PLL_DLL_WDT_CONTROL1);
111 scratch32 |= O2_PLL_SOFT_RESET;
112 sdhci_writel(host, scratch32, O2_PLL_DLL_WDT_CONTROL1);
114 scratch32 &= ~(O2_PLL_SOFT_RESET);
115 sdhci_writel(host, scratch32, O2_PLL_DLL_WDT_CONTROL1);
117 /* PLL force active */
118 scratch32 |= O2_PLL_FORCE_ACTIVE;
119 sdhci_writel(host, scratch32, O2_PLL_DLL_WDT_CONTROL1);
122 timeout = ktime_add_ms(ktime_get(), 20);
124 bool timedout = ktime_after(ktime_get(), timeout);
126 scratch = sdhci_readw(host, O2_PLL_DLL_WDT_CONTROL1);
127 if (scratch & O2_PLL_LOCK_STATUS)
130 pr_err("%s: Internal clock never stabilised.\n",
131 mmc_hostname(host->mmc));
132 sdhci_dumpregs(host);
138 /* Wait for card detect finish */
140 sdhci_o2_wait_card_detect_stable(host);
143 /* Cancel PLL force active */
144 scratch32 = sdhci_readl(host, O2_PLL_DLL_WDT_CONTROL1);
145 scratch32 &= ~O2_PLL_FORCE_ACTIVE;
146 sdhci_writel(host, scratch32, O2_PLL_DLL_WDT_CONTROL1);
149 static int sdhci_o2_get_cd(struct mmc_host *mmc)
151 struct sdhci_host *host = mmc_priv(mmc);
153 if (!(sdhci_readw(host, O2_PLL_DLL_WDT_CONTROL1) & O2_PLL_LOCK_STATUS))
154 sdhci_o2_enable_internal_clock(host);
156 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
159 static void o2_pci_set_baseclk(struct sdhci_pci_chip *chip, u32 value)
163 pci_read_config_dword(chip->pdev,
164 O2_SD_PLL_SETTING, &scratch_32);
166 scratch_32 &= 0x0000FFFF;
169 pci_write_config_dword(chip->pdev,
170 O2_SD_PLL_SETTING, scratch_32);
173 static u32 sdhci_o2_pll_dll_wdt_control(struct sdhci_host *host)
175 return sdhci_readl(host, O2_PLL_DLL_WDT_CONTROL1);
179 * This function is used to detect dll lock status.
180 * Since the dll lock status bit will toggle randomly
181 * with very short interval which needs to be polled
182 * as fast as possible. Set sleep_us as 1 microsecond.
184 static int sdhci_o2_wait_dll_detect_lock(struct sdhci_host *host)
188 return readx_poll_timeout(sdhci_o2_pll_dll_wdt_control, host,
189 scratch32, !(scratch32 & O2_DLL_LOCK_STATUS), 1, 1000000);
192 static void sdhci_o2_set_tuning_mode(struct sdhci_host *host)
196 /* enable hardware tuning */
197 reg = sdhci_readw(host, O2_SD_VENDOR_SETTING);
198 reg &= ~O2_SD_HW_TUNING_DISABLE;
199 sdhci_writew(host, reg, O2_SD_VENDOR_SETTING);
202 static void __sdhci_o2_execute_tuning(struct sdhci_host *host, u32 opcode)
206 sdhci_send_tuning(host, opcode);
208 for (i = 0; i < 150; i++) {
209 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
211 if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
212 if (ctrl & SDHCI_CTRL_TUNED_CLK) {
213 host->tuning_done = true;
216 pr_warn("%s: HW tuning failed !\n",
217 mmc_hostname(host->mmc));
224 pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
225 mmc_hostname(host->mmc));
226 sdhci_reset_tuning(host);
230 * This function is used to fix o2 dll shift issue.
231 * It isn't necessary to detect card present before recovery.
232 * Firstly, it is used by bht emmc card, which is embedded.
233 * Second, before call recovery card present will be detected
234 * outside of the execute tuning function.
236 static int sdhci_o2_dll_recovery(struct sdhci_host *host)
241 struct sdhci_pci_slot *slot = sdhci_priv(host);
242 struct sdhci_pci_chip *chip = slot->chip;
243 struct o2_host *o2_host = sdhci_pci_priv(slot);
246 pci_read_config_byte(chip->pdev,
247 O2_SD_LOCK_WP, &scratch_8);
249 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
250 while (o2_host->dll_adjust_count < DMDN_SZ && !ret) {
252 sdhci_writeb(host, 0, SDHCI_CLOCK_CONTROL);
254 /* PLL software reset */
255 scratch_32 = sdhci_readl(host, O2_PLL_DLL_WDT_CONTROL1);
256 scratch_32 |= O2_PLL_SOFT_RESET;
257 sdhci_writel(host, scratch_32, O2_PLL_DLL_WDT_CONTROL1);
259 pci_read_config_dword(chip->pdev,
262 /* Enable Base Clk setting change */
263 scratch_32 |= O2_SD_FREG4_ENABLE_CLK_SET;
264 pci_write_config_dword(chip->pdev, O2_SD_FUNC_REG4, scratch_32);
265 o2_pci_set_baseclk(chip, dmdn_table[o2_host->dll_adjust_count]);
267 /* Enable internal clock */
268 scratch_8 = SDHCI_CLOCK_INT_EN;
269 sdhci_writeb(host, scratch_8, SDHCI_CLOCK_CONTROL);
271 if (sdhci_o2_get_cd(host->mmc)) {
273 * need wait at least 5ms for dll status stable,
274 * after enable internal clock
276 usleep_range(5000, 6000);
277 if (sdhci_o2_wait_dll_detect_lock(host)) {
278 scratch_8 |= SDHCI_CLOCK_CARD_EN;
279 sdhci_writeb(host, scratch_8,
280 SDHCI_CLOCK_CONTROL);
283 pr_warn("%s: DLL unlocked when dll_adjust_count is %d.\n",
284 mmc_hostname(host->mmc),
285 o2_host->dll_adjust_count);
288 pr_err("%s: card present detect failed.\n",
289 mmc_hostname(host->mmc));
293 o2_host->dll_adjust_count++;
295 if (!ret && o2_host->dll_adjust_count == DMDN_SZ)
296 pr_err("%s: DLL adjust over max times\n",
297 mmc_hostname(host->mmc));
299 pci_read_config_byte(chip->pdev,
300 O2_SD_LOCK_WP, &scratch_8);
302 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
306 static int sdhci_o2_execute_tuning(struct mmc_host *mmc, u32 opcode)
308 struct sdhci_host *host = mmc_priv(mmc);
309 struct sdhci_pci_slot *slot = sdhci_priv(host);
310 struct sdhci_pci_chip *chip = slot->chip;
311 int current_bus_width = 0;
318 * This handler only implements the eMMC tuning that is specific to
319 * this controller. Fall back to the standard method for other TIMING.
321 if ((host->timing != MMC_TIMING_MMC_HS200) &&
322 (host->timing != MMC_TIMING_UHS_SDR104))
323 return sdhci_execute_tuning(mmc, opcode);
325 if (WARN_ON((opcode != MMC_SEND_TUNING_BLOCK_HS200) &&
326 (opcode != MMC_SEND_TUNING_BLOCK)))
329 /* Force power mode enter L0 */
330 scratch = sdhci_readw(host, O2_SD_MISC_CTRL);
331 scratch |= O2_SD_PWR_FORCE_L0;
332 sdhci_writew(host, scratch, O2_SD_MISC_CTRL);
335 reg_val = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
336 reg_val &= ~SDHCI_CLOCK_CARD_EN;
337 sdhci_writew(host, reg_val, SDHCI_CLOCK_CONTROL);
340 pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch_8);
342 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
344 /* Set pcr 0x354[16] to choose dll clock, and set the default phase */
345 pci_read_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, ®_val);
346 reg_val &= ~(O2_SD_SEL_DLL | O2_SD_PHASE_MASK);
347 reg_val |= (O2_SD_SEL_DLL | O2_SD_FIX_PHASE);
348 pci_write_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, reg_val);
351 pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch_8);
353 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
356 reg_val = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
357 reg_val |= SDHCI_CLOCK_CARD_EN;
358 sdhci_writew(host, reg_val, SDHCI_CLOCK_CONTROL);
360 /* wait DLL lock, timeout value 5ms */
361 if (readx_poll_timeout(sdhci_o2_pll_dll_wdt_control, host,
362 scratch32, (scratch32 & O2_DLL_LOCK_STATUS), 1, 5000))
363 pr_warn("%s: DLL can't lock in 5ms after force L0 during tuning.\n",
364 mmc_hostname(host->mmc));
366 * Judge the tuning reason, whether caused by dll shift
367 * If cause by dll shift, should call sdhci_o2_dll_recovery
369 if (!sdhci_o2_wait_dll_detect_lock(host))
370 if (!sdhci_o2_dll_recovery(host)) {
371 pr_err("%s: o2 dll recovery failed\n",
372 mmc_hostname(host->mmc));
376 * o2 sdhci host didn't support 8bit emmc tuning
378 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8) {
379 current_bus_width = mmc->ios.bus_width;
380 mmc->ios.bus_width = MMC_BUS_WIDTH_4;
381 sdhci_set_bus_width(host, MMC_BUS_WIDTH_4);
384 sdhci_o2_set_tuning_mode(host);
386 sdhci_start_tuning(host);
388 __sdhci_o2_execute_tuning(host, opcode);
390 sdhci_end_tuning(host);
392 if (current_bus_width == MMC_BUS_WIDTH_8) {
393 mmc->ios.bus_width = MMC_BUS_WIDTH_8;
394 sdhci_set_bus_width(host, current_bus_width);
397 /* Cancel force power mode enter L0 */
398 scratch = sdhci_readw(host, O2_SD_MISC_CTRL);
399 scratch &= ~(O2_SD_PWR_FORCE_L0);
400 sdhci_writew(host, scratch, O2_SD_MISC_CTRL);
402 sdhci_reset(host, SDHCI_RESET_CMD);
403 sdhci_reset(host, SDHCI_RESET_DATA);
405 host->flags &= ~SDHCI_HS400_TUNING;
409 static void o2_pci_led_enable(struct sdhci_pci_chip *chip)
414 /* Set led of SD host function enable */
415 ret = pci_read_config_dword(chip->pdev,
416 O2_SD_FUNC_REG0, &scratch_32);
420 scratch_32 &= ~O2_SD_FREG0_LEDOFF;
421 pci_write_config_dword(chip->pdev,
422 O2_SD_FUNC_REG0, scratch_32);
424 ret = pci_read_config_dword(chip->pdev,
425 O2_SD_TEST_REG, &scratch_32);
429 scratch_32 |= O2_SD_LED_ENABLE;
430 pci_write_config_dword(chip->pdev,
431 O2_SD_TEST_REG, scratch_32);
434 static void sdhci_pci_o2_fujin2_pci_init(struct sdhci_pci_chip *chip)
438 /* Improve write performance for SD3.0 */
439 ret = pci_read_config_dword(chip->pdev, O2_SD_DEV_CTRL, &scratch_32);
442 scratch_32 &= ~((1 << 12) | (1 << 13) | (1 << 14));
443 pci_write_config_dword(chip->pdev, O2_SD_DEV_CTRL, scratch_32);
445 /* Enable Link abnormal reset generating Reset */
446 ret = pci_read_config_dword(chip->pdev, O2_SD_MISC_REG5, &scratch_32);
449 scratch_32 &= ~((1 << 19) | (1 << 11));
450 scratch_32 |= (1 << 10);
451 pci_write_config_dword(chip->pdev, O2_SD_MISC_REG5, scratch_32);
453 /* set card power over current protection */
454 ret = pci_read_config_dword(chip->pdev, O2_SD_TEST_REG, &scratch_32);
457 scratch_32 |= (1 << 4);
458 pci_write_config_dword(chip->pdev, O2_SD_TEST_REG, scratch_32);
460 /* adjust the output delay for SD mode */
461 pci_write_config_dword(chip->pdev, O2_SD_DELAY_CTRL, 0x00002492);
463 /* Set the output voltage setting of Aux 1.2v LDO */
464 ret = pci_read_config_dword(chip->pdev, O2_SD_LD0_CTRL, &scratch_32);
467 scratch_32 &= ~(3 << 12);
468 pci_write_config_dword(chip->pdev, O2_SD_LD0_CTRL, scratch_32);
470 /* Set Max power supply capability of SD host */
471 ret = pci_read_config_dword(chip->pdev, O2_SD_CAP_REG0, &scratch_32);
474 scratch_32 &= ~(0x01FE);
475 scratch_32 |= 0x00CC;
476 pci_write_config_dword(chip->pdev, O2_SD_CAP_REG0, scratch_32);
477 /* Set DLL Tuning Window */
478 ret = pci_read_config_dword(chip->pdev,
479 O2_SD_TUNING_CTRL, &scratch_32);
482 scratch_32 &= ~(0x000000FF);
483 scratch_32 |= 0x00000066;
484 pci_write_config_dword(chip->pdev, O2_SD_TUNING_CTRL, scratch_32);
486 /* Set UHS2 T_EIDLE */
487 ret = pci_read_config_dword(chip->pdev,
488 O2_SD_UHS2_L1_CTRL, &scratch_32);
491 scratch_32 &= ~(0x000000FC);
492 scratch_32 |= 0x00000084;
493 pci_write_config_dword(chip->pdev, O2_SD_UHS2_L1_CTRL, scratch_32);
495 /* Set UHS2 Termination */
496 ret = pci_read_config_dword(chip->pdev, O2_SD_FUNC_REG3, &scratch_32);
499 scratch_32 &= ~((1 << 21) | (1 << 30));
501 pci_write_config_dword(chip->pdev, O2_SD_FUNC_REG3, scratch_32);
503 /* Set L1 Entrance Timer */
504 ret = pci_read_config_dword(chip->pdev, O2_SD_CAPS, &scratch_32);
507 scratch_32 &= ~(0xf0000000);
508 scratch_32 |= 0x30000000;
509 pci_write_config_dword(chip->pdev, O2_SD_CAPS, scratch_32);
511 ret = pci_read_config_dword(chip->pdev,
512 O2_SD_MISC_CTRL4, &scratch_32);
515 scratch_32 &= ~(0x000f0000);
516 scratch_32 |= 0x00080000;
517 pci_write_config_dword(chip->pdev, O2_SD_MISC_CTRL4, scratch_32);
520 static void sdhci_pci_o2_enable_msi(struct sdhci_pci_chip *chip,
521 struct sdhci_host *host)
525 ret = pci_find_capability(chip->pdev, PCI_CAP_ID_MSI);
527 pr_info("%s: unsupported MSI, use INTx irq\n",
528 mmc_hostname(host->mmc));
532 ret = pci_alloc_irq_vectors(chip->pdev, 1, 1,
533 PCI_IRQ_MSI | PCI_IRQ_MSIX);
535 pr_err("%s: enable PCI MSI failed, err=%d\n",
536 mmc_hostname(host->mmc), ret);
540 host->irq = pci_irq_vector(chip->pdev, 0);
543 static void sdhci_o2_enable_clk(struct sdhci_host *host, u16 clk)
545 /* Enable internal clock */
546 clk |= SDHCI_CLOCK_INT_EN;
547 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
549 sdhci_o2_enable_internal_clock(host);
550 if (sdhci_o2_get_cd(host->mmc)) {
551 clk |= SDHCI_CLOCK_CARD_EN;
552 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
556 static void sdhci_pci_o2_set_clock(struct sdhci_host *host, unsigned int clock)
561 struct sdhci_pci_slot *slot = sdhci_priv(host);
562 struct sdhci_pci_chip *chip = slot->chip;
564 host->mmc->actual_clock = 0;
566 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
572 pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
574 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
576 if ((host->timing == MMC_TIMING_UHS_SDR104) && (clock == 200000000)) {
577 pci_read_config_dword(chip->pdev, O2_SD_PLL_SETTING, &scratch_32);
579 if ((scratch_32 & 0xFFFF0000) != 0x2c280000)
580 o2_pci_set_baseclk(chip, 0x2c280000);
582 pci_read_config_dword(chip->pdev, O2_SD_PLL_SETTING, &scratch_32);
584 if ((scratch_32 & 0xFFFF0000) != 0x25100000)
585 o2_pci_set_baseclk(chip, 0x25100000);
588 pci_read_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, &scratch_32);
589 scratch_32 &= ~(O2_SD_SEL_DLL | O2_SD_PHASE_MASK);
590 pci_write_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, scratch_32);
593 pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
595 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
597 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
598 sdhci_o2_enable_clk(host, clk);
601 static int sdhci_pci_o2_probe_slot(struct sdhci_pci_slot *slot)
603 struct sdhci_pci_chip *chip;
604 struct sdhci_host *host;
605 struct o2_host *o2_host = sdhci_pci_priv(slot);
612 o2_host->dll_adjust_count = 0;
613 caps = sdhci_readl(host, SDHCI_CAPABILITIES);
616 * mmc_select_bus_width() will test the bus to determine the actual bus
619 if (caps & SDHCI_CAN_DO_8BIT)
620 host->mmc->caps |= MMC_CAP_8_BIT_DATA;
622 switch (chip->pdev->device) {
623 case PCI_DEVICE_ID_O2_SDS0:
624 case PCI_DEVICE_ID_O2_SEABIRD0:
625 case PCI_DEVICE_ID_O2_SEABIRD1:
626 case PCI_DEVICE_ID_O2_SDS1:
627 case PCI_DEVICE_ID_O2_FUJIN2:
628 reg = sdhci_readl(host, O2_SD_VENDOR_SETTING);
630 host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
632 sdhci_pci_o2_enable_msi(chip, host);
634 if (chip->pdev->device == PCI_DEVICE_ID_O2_SEABIRD0) {
635 ret = pci_read_config_dword(chip->pdev,
636 O2_SD_MISC_SETTING, ®);
639 if (reg & (1 << 4)) {
640 pr_info("%s: emmc 1.8v flag is set, force 1.8v signaling voltage\n",
641 mmc_hostname(host->mmc));
642 host->flags &= ~SDHCI_SIGNALING_330;
643 host->flags |= SDHCI_SIGNALING_180;
644 host->mmc->caps2 |= MMC_CAP2_NO_SD;
645 host->mmc->caps2 |= MMC_CAP2_NO_SDIO;
646 pci_write_config_dword(chip->pdev,
647 O2_SD_DETECT_SETTING, 3);
650 slot->host->mmc_host_ops.get_cd = sdhci_o2_get_cd;
653 if (chip->pdev->device == PCI_DEVICE_ID_O2_SEABIRD1) {
654 slot->host->mmc_host_ops.get_cd = sdhci_o2_get_cd;
655 host->mmc->caps2 |= MMC_CAP2_NO_SDIO;
656 host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
659 host->mmc_host_ops.execute_tuning = sdhci_o2_execute_tuning;
661 if (chip->pdev->device != PCI_DEVICE_ID_O2_FUJIN2)
663 /* set dll watch dog timer */
664 reg = sdhci_readl(host, O2_SD_VENDOR_SETTING2);
666 sdhci_writel(host, reg, O2_SD_VENDOR_SETTING2);
676 static int sdhci_pci_o2_probe(struct sdhci_pci_chip *chip)
682 switch (chip->pdev->device) {
683 case PCI_DEVICE_ID_O2_8220:
684 case PCI_DEVICE_ID_O2_8221:
685 case PCI_DEVICE_ID_O2_8320:
686 case PCI_DEVICE_ID_O2_8321:
687 /* This extra setup is required due to broken ADMA. */
688 ret = pci_read_config_byte(chip->pdev,
689 O2_SD_LOCK_WP, &scratch);
693 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
695 /* Set Multi 3 to VCC3V# */
696 pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
698 /* Disable CLK_REQ# support after media DET */
699 ret = pci_read_config_byte(chip->pdev,
700 O2_SD_CLKREQ, &scratch);
704 pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
706 /* Choose capabilities, enable SDMA. We have to write 0x01
707 * to the capabilities register first to unlock it.
709 ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
713 pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
714 pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
716 /* Disable ADMA1/2 */
717 pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
718 pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
720 /* Disable the infinite transfer mode */
721 ret = pci_read_config_byte(chip->pdev,
722 O2_SD_INF_MOD, &scratch);
726 pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
729 ret = pci_read_config_byte(chip->pdev,
730 O2_SD_LOCK_WP, &scratch);
734 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
736 case PCI_DEVICE_ID_O2_SDS0:
737 case PCI_DEVICE_ID_O2_SDS1:
738 case PCI_DEVICE_ID_O2_FUJIN2:
740 ret = pci_read_config_byte(chip->pdev,
741 O2_SD_LOCK_WP, &scratch);
746 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
748 /* DevId=8520 subId= 0x11 or 0x12 Type Chip support */
749 if (chip->pdev->device == PCI_DEVICE_ID_O2_FUJIN2) {
750 ret = pci_read_config_dword(chip->pdev,
755 scratch_32 = ((scratch_32 & 0xFF000000) >> 24);
757 /* Check Whether subId is 0x11 or 0x12 */
758 if ((scratch_32 == 0x11) || (scratch_32 == 0x12)) {
759 scratch_32 = 0x25100000;
761 o2_pci_set_baseclk(chip, scratch_32);
762 ret = pci_read_config_dword(chip->pdev,
768 /* Enable Base Clk setting change */
769 scratch_32 |= O2_SD_FREG4_ENABLE_CLK_SET;
770 pci_write_config_dword(chip->pdev,
774 /* Set Tuning Window to 4 */
775 pci_write_config_byte(chip->pdev,
776 O2_SD_TUNING_CTRL, 0x44);
782 /* Enable 8520 led function */
783 o2_pci_led_enable(chip);
785 /* Set timeout CLK */
786 ret = pci_read_config_dword(chip->pdev,
787 O2_SD_CLK_SETTING, &scratch_32);
791 scratch_32 &= ~(0xFF00);
792 scratch_32 |= 0x07E0C800;
793 pci_write_config_dword(chip->pdev,
794 O2_SD_CLK_SETTING, scratch_32);
796 ret = pci_read_config_dword(chip->pdev,
797 O2_SD_CLKREQ, &scratch_32);
801 pci_write_config_dword(chip->pdev, O2_SD_CLKREQ, scratch_32);
803 ret = pci_read_config_dword(chip->pdev,
804 O2_SD_PLL_SETTING, &scratch_32);
808 scratch_32 &= ~(0x1F3F070E);
809 scratch_32 |= 0x18270106;
810 pci_write_config_dword(chip->pdev,
811 O2_SD_PLL_SETTING, scratch_32);
813 /* Disable UHS1 funciton */
814 ret = pci_read_config_dword(chip->pdev,
815 O2_SD_CAP_REG2, &scratch_32);
818 scratch_32 &= ~(0xE0);
819 pci_write_config_dword(chip->pdev,
820 O2_SD_CAP_REG2, scratch_32);
822 if (chip->pdev->device == PCI_DEVICE_ID_O2_FUJIN2)
823 sdhci_pci_o2_fujin2_pci_init(chip);
826 ret = pci_read_config_byte(chip->pdev,
827 O2_SD_LOCK_WP, &scratch);
831 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
833 case PCI_DEVICE_ID_O2_SEABIRD0:
834 case PCI_DEVICE_ID_O2_SEABIRD1:
836 ret = pci_read_config_byte(chip->pdev,
837 O2_SD_LOCK_WP, &scratch);
842 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
844 ret = pci_read_config_dword(chip->pdev,
845 O2_SD_PLL_SETTING, &scratch_32);
849 if ((scratch_32 & 0xff000000) == 0x01000000) {
850 scratch_32 &= 0x0000FFFF;
851 scratch_32 |= 0x1F340000;
853 pci_write_config_dword(chip->pdev,
854 O2_SD_PLL_SETTING, scratch_32);
856 scratch_32 &= 0x0000FFFF;
857 scratch_32 |= 0x25100000;
859 pci_write_config_dword(chip->pdev,
860 O2_SD_PLL_SETTING, scratch_32);
862 ret = pci_read_config_dword(chip->pdev,
867 scratch_32 |= (1 << 22);
868 pci_write_config_dword(chip->pdev,
869 O2_SD_FUNC_REG4, scratch_32);
872 /* Set Tuning Windows to 5 */
873 pci_write_config_byte(chip->pdev,
874 O2_SD_TUNING_CTRL, 0x55);
876 ret = pci_read_config_byte(chip->pdev,
877 O2_SD_LOCK_WP, &scratch);
881 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
888 #ifdef CONFIG_PM_SLEEP
889 static int sdhci_pci_o2_resume(struct sdhci_pci_chip *chip)
891 sdhci_pci_o2_probe(chip);
892 return sdhci_pci_resume_host(chip);
896 static const struct sdhci_ops sdhci_pci_o2_ops = {
897 .set_clock = sdhci_pci_o2_set_clock,
898 .enable_dma = sdhci_pci_enable_dma,
899 .set_bus_width = sdhci_set_bus_width,
900 .reset = sdhci_reset,
901 .set_uhs_signaling = sdhci_set_uhs_signaling,
904 const struct sdhci_pci_fixes sdhci_o2 = {
905 .probe = sdhci_pci_o2_probe,
906 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
907 .quirks2 = SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD,
908 .probe_slot = sdhci_pci_o2_probe_slot,
909 #ifdef CONFIG_PM_SLEEP
910 .resume = sdhci_pci_o2_resume,
912 .ops = &sdhci_pci_o2_ops,
913 .priv_size = sizeof(struct o2_host),