19c92ab61SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
201acf691SAdam Lee /*
301acf691SAdam Lee  * Copyright (C) 2013 BayHub Technology Ltd.
401acf691SAdam Lee  *
501acf691SAdam Lee  * Authors: Peter Guo <peter.guo@bayhubtech.com>
601acf691SAdam Lee  *          Adam Lee <adam.lee@canonical.com>
757322d54Sernest.zhang  *          Ernest Zhang <ernest.zhang@bayhubtech.com>
801acf691SAdam Lee  */
901acf691SAdam Lee 
1001acf691SAdam Lee #include <linux/pci.h>
110086fc21Sernest.zhang #include <linux/mmc/host.h>
120086fc21Sernest.zhang #include <linux/mmc/mmc.h>
130086fc21Sernest.zhang #include <linux/delay.h>
147d440617SShirley Her (SC) #include <linux/iopoll.h>
154be33cf1SFred Ai #include <linux/bitfield.h>
1601acf691SAdam Lee 
1701acf691SAdam Lee #include "sdhci.h"
1801acf691SAdam Lee #include "sdhci-pci.h"
19361eeda0SAdrian Hunter 
20361eeda0SAdrian Hunter /*
21361eeda0SAdrian Hunter  * O2Micro device registers
22361eeda0SAdrian Hunter  */
23361eeda0SAdrian Hunter 
24da2a69cfSChevron Li #define O2_SD_PCIE_SWITCH	0x54
25361eeda0SAdrian Hunter #define O2_SD_MISC_REG5		0x64
26361eeda0SAdrian Hunter #define O2_SD_LD0_CTRL		0x68
27361eeda0SAdrian Hunter #define O2_SD_DEV_CTRL		0x88
28361eeda0SAdrian Hunter #define O2_SD_LOCK_WP		0xD3
29361eeda0SAdrian Hunter #define O2_SD_TEST_REG		0xD4
30361eeda0SAdrian Hunter #define O2_SD_FUNC_REG0		0xDC
31361eeda0SAdrian Hunter #define O2_SD_MULTI_VCC3V	0xEE
32361eeda0SAdrian Hunter #define O2_SD_CLKREQ		0xEC
33361eeda0SAdrian Hunter #define O2_SD_CAPS		0xE0
34361eeda0SAdrian Hunter #define O2_SD_ADMA1		0xE2
35361eeda0SAdrian Hunter #define O2_SD_ADMA2		0xE7
36096cc0cdSChevron Li #define O2_SD_MISC_CTRL2	0xF0
37361eeda0SAdrian Hunter #define O2_SD_INF_MOD		0xF1
38361eeda0SAdrian Hunter #define O2_SD_MISC_CTRL4	0xFC
391ad9f880SShirley Her #define O2_SD_MISC_CTRL		0x1C0
403d757ddbSChevron Li #define O2_SD_EXP_INT_REG	0x1E0
411ad9f880SShirley Her #define O2_SD_PWR_FORCE_L0	0x0002
42361eeda0SAdrian Hunter #define O2_SD_TUNING_CTRL	0x300
43361eeda0SAdrian Hunter #define O2_SD_PLL_SETTING	0x304
4457322d54Sernest.zhang #define O2_SD_MISC_SETTING	0x308
45361eeda0SAdrian Hunter #define O2_SD_CLK_SETTING	0x328
46361eeda0SAdrian Hunter #define O2_SD_CAP_REG2		0x330
47361eeda0SAdrian Hunter #define O2_SD_CAP_REG0		0x334
48361eeda0SAdrian Hunter #define O2_SD_UHS1_CAP_SETTING	0x33C
49361eeda0SAdrian Hunter #define O2_SD_DELAY_CTRL	0x350
504be33cf1SFred Ai #define O2_SD_OUTPUT_CLK_SOURCE_SWITCH	0x354
51361eeda0SAdrian Hunter #define O2_SD_UHS2_L1_CTRL	0x35C
52361eeda0SAdrian Hunter #define O2_SD_FUNC_REG3		0x3E0
53361eeda0SAdrian Hunter #define O2_SD_FUNC_REG4		0x3E4
543d757ddbSChevron Li #define O2_SD_PARA_SET_REG1	0x444
553d757ddbSChevron Li #define O2_SD_VDDX_CTRL_REG	0x508
563d757ddbSChevron Li #define O2_SD_GPIO_CTRL_REG1	0x510
57361eeda0SAdrian Hunter #define O2_SD_LED_ENABLE	BIT(6)
58361eeda0SAdrian Hunter #define O2_SD_FREG0_LEDOFF	BIT(13)
594be33cf1SFred Ai #define O2_SD_SEL_DLL		BIT(16)
60361eeda0SAdrian Hunter #define O2_SD_FREG4_ENABLE_CLK_SET	BIT(22)
614be33cf1SFred Ai #define O2_SD_PHASE_MASK	GENMASK(23, 20)
624be33cf1SFred Ai #define O2_SD_FIX_PHASE		FIELD_PREP(O2_SD_PHASE_MASK, 0x9)
63361eeda0SAdrian Hunter 
64361eeda0SAdrian Hunter #define O2_SD_VENDOR_SETTING	0x110
65361eeda0SAdrian Hunter #define O2_SD_VENDOR_SETTING2	0x1C8
660086fc21Sernest.zhang #define O2_SD_HW_TUNING_DISABLE	BIT(4)
670086fc21Sernest.zhang 
689674bab4SShirley Her (SC) #define O2_PLL_DLL_WDT_CONTROL1	0x1CC
6969d91ed1SErnest Zhang(WH) #define  O2_PLL_FORCE_ACTIVE	BIT(18)
7069d91ed1SErnest Zhang(WH) #define  O2_PLL_LOCK_STATUS	BIT(14)
7169d91ed1SErnest Zhang(WH) #define  O2_PLL_SOFT_RESET	BIT(12)
727d440617SShirley Her (SC) #define  O2_DLL_LOCK_STATUS	BIT(11)
7369d91ed1SErnest Zhang(WH) 
7469d91ed1SErnest Zhang(WH) #define O2_SD_DETECT_SETTING 0x324
7569d91ed1SErnest Zhang(WH) 
767d440617SShirley Her (SC) static const u32 dmdn_table[] = {0x2B1C0000,
777d440617SShirley Her (SC) 	0x2C1A0000, 0x371B0000, 0x35100000};
787d440617SShirley Her (SC) #define DMDN_SZ ARRAY_SIZE(dmdn_table)
797d440617SShirley Her (SC) 
807d440617SShirley Her (SC) struct o2_host {
817d440617SShirley Her (SC) 	u8 dll_adjust_count;
827d440617SShirley Her (SC) };
837d440617SShirley Her (SC) 
sdhci_o2_wait_card_detect_stable(struct sdhci_host * host)84908fd508SShirley Her (SC) static void sdhci_o2_wait_card_detect_stable(struct sdhci_host *host)
85908fd508SShirley Her (SC) {
86908fd508SShirley Her (SC) 	ktime_t timeout;
87908fd508SShirley Her (SC) 	u32 scratch32;
88908fd508SShirley Her (SC) 
89908fd508SShirley Her (SC) 	/* Wait max 50 ms */
90908fd508SShirley Her (SC) 	timeout = ktime_add_ms(ktime_get(), 50);
91908fd508SShirley Her (SC) 	while (1) {
92908fd508SShirley Her (SC) 		bool timedout = ktime_after(ktime_get(), timeout);
93908fd508SShirley Her (SC) 
94908fd508SShirley Her (SC) 		scratch32 = sdhci_readl(host, SDHCI_PRESENT_STATE);
95908fd508SShirley Her (SC) 		if ((scratch32 & SDHCI_CARD_PRESENT) >> SDHCI_CARD_PRES_SHIFT
96908fd508SShirley Her (SC) 		    == (scratch32 & SDHCI_CD_LVL) >> SDHCI_CD_LVL_SHIFT)
97908fd508SShirley Her (SC) 			break;
98908fd508SShirley Her (SC) 
99908fd508SShirley Her (SC) 		if (timedout) {
100908fd508SShirley Her (SC) 			pr_err("%s: Card Detect debounce never finished.\n",
101908fd508SShirley Her (SC) 			       mmc_hostname(host->mmc));
102908fd508SShirley Her (SC) 			sdhci_dumpregs(host);
103908fd508SShirley Her (SC) 			return;
104908fd508SShirley Her (SC) 		}
105908fd508SShirley Her (SC) 		udelay(10);
106908fd508SShirley Her (SC) 	}
107908fd508SShirley Her (SC) }
108908fd508SShirley Her (SC) 
sdhci_o2_enable_internal_clock(struct sdhci_host * host)109908fd508SShirley Her (SC) static void sdhci_o2_enable_internal_clock(struct sdhci_host *host)
110908fd508SShirley Her (SC) {
111908fd508SShirley Her (SC) 	ktime_t timeout;
112908fd508SShirley Her (SC) 	u16 scratch;
113908fd508SShirley Her (SC) 	u32 scratch32;
114908fd508SShirley Her (SC) 
115908fd508SShirley Her (SC) 	/* PLL software reset */
116908fd508SShirley Her (SC) 	scratch32 = sdhci_readl(host, O2_PLL_DLL_WDT_CONTROL1);
117908fd508SShirley Her (SC) 	scratch32 |= O2_PLL_SOFT_RESET;
118908fd508SShirley Her (SC) 	sdhci_writel(host, scratch32, O2_PLL_DLL_WDT_CONTROL1);
119908fd508SShirley Her (SC) 	udelay(1);
120908fd508SShirley Her (SC) 	scratch32 &= ~(O2_PLL_SOFT_RESET);
121908fd508SShirley Her (SC) 	sdhci_writel(host, scratch32, O2_PLL_DLL_WDT_CONTROL1);
122908fd508SShirley Her (SC) 
123908fd508SShirley Her (SC) 	/* PLL force active */
124908fd508SShirley Her (SC) 	scratch32 |= O2_PLL_FORCE_ACTIVE;
125908fd508SShirley Her (SC) 	sdhci_writel(host, scratch32, O2_PLL_DLL_WDT_CONTROL1);
126908fd508SShirley Her (SC) 
127908fd508SShirley Her (SC) 	/* Wait max 20 ms */
128908fd508SShirley Her (SC) 	timeout = ktime_add_ms(ktime_get(), 20);
129908fd508SShirley Her (SC) 	while (1) {
130908fd508SShirley Her (SC) 		bool timedout = ktime_after(ktime_get(), timeout);
131908fd508SShirley Her (SC) 
132908fd508SShirley Her (SC) 		scratch = sdhci_readw(host, O2_PLL_DLL_WDT_CONTROL1);
133908fd508SShirley Her (SC) 		if (scratch & O2_PLL_LOCK_STATUS)
134908fd508SShirley Her (SC) 			break;
135908fd508SShirley Her (SC) 		if (timedout) {
136908fd508SShirley Her (SC) 			pr_err("%s: Internal clock never stabilised.\n",
137908fd508SShirley Her (SC) 			       mmc_hostname(host->mmc));
138908fd508SShirley Her (SC) 			sdhci_dumpregs(host);
139908fd508SShirley Her (SC) 			goto out;
140908fd508SShirley Her (SC) 		}
141908fd508SShirley Her (SC) 		udelay(10);
142908fd508SShirley Her (SC) 	}
143908fd508SShirley Her (SC) 
144908fd508SShirley Her (SC) 	/* Wait for card detect finish */
145908fd508SShirley Her (SC) 	udelay(1);
146908fd508SShirley Her (SC) 	sdhci_o2_wait_card_detect_stable(host);
147908fd508SShirley Her (SC) 
148908fd508SShirley Her (SC) out:
149908fd508SShirley Her (SC) 	/* Cancel PLL force active */
150908fd508SShirley Her (SC) 	scratch32 = sdhci_readl(host, O2_PLL_DLL_WDT_CONTROL1);
151908fd508SShirley Her (SC) 	scratch32 &= ~O2_PLL_FORCE_ACTIVE;
152908fd508SShirley Her (SC) 	sdhci_writel(host, scratch32, O2_PLL_DLL_WDT_CONTROL1);
153908fd508SShirley Her (SC) }
154908fd508SShirley Her (SC) 
sdhci_o2_get_cd(struct mmc_host * mmc)155908fd508SShirley Her (SC) static int sdhci_o2_get_cd(struct mmc_host *mmc)
156908fd508SShirley Her (SC) {
157908fd508SShirley Her (SC) 	struct sdhci_host *host = mmc_priv(mmc);
158908fd508SShirley Her (SC) 
1597d440617SShirley Her (SC) 	if (!(sdhci_readw(host, O2_PLL_DLL_WDT_CONTROL1) & O2_PLL_LOCK_STATUS))
160908fd508SShirley Her (SC) 		sdhci_o2_enable_internal_clock(host);
161e591fcf6SChevron Li 	else
162e591fcf6SChevron Li 		sdhci_o2_wait_card_detect_stable(host);
163908fd508SShirley Her (SC) 
164908fd508SShirley Her (SC) 	return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
165908fd508SShirley Her (SC) }
166908fd508SShirley Her (SC) 
o2_pci_set_baseclk(struct sdhci_pci_chip * chip,u32 value)167908fd508SShirley Her (SC) static void o2_pci_set_baseclk(struct sdhci_pci_chip *chip, u32 value)
168908fd508SShirley Her (SC) {
169908fd508SShirley Her (SC) 	u32 scratch_32;
170908fd508SShirley Her (SC) 
171908fd508SShirley Her (SC) 	pci_read_config_dword(chip->pdev,
172908fd508SShirley Her (SC) 			      O2_SD_PLL_SETTING, &scratch_32);
173908fd508SShirley Her (SC) 
174908fd508SShirley Her (SC) 	scratch_32 &= 0x0000FFFF;
175908fd508SShirley Her (SC) 	scratch_32 |= value;
176908fd508SShirley Her (SC) 
177908fd508SShirley Her (SC) 	pci_write_config_dword(chip->pdev,
178908fd508SShirley Her (SC) 			       O2_SD_PLL_SETTING, scratch_32);
179908fd508SShirley Her (SC) }
180908fd508SShirley Her (SC) 
sdhci_o2_pll_dll_wdt_control(struct sdhci_host * host)1817d440617SShirley Her (SC) static u32 sdhci_o2_pll_dll_wdt_control(struct sdhci_host *host)
1827d440617SShirley Her (SC) {
1837d440617SShirley Her (SC) 	return sdhci_readl(host, O2_PLL_DLL_WDT_CONTROL1);
1847d440617SShirley Her (SC) }
1857d440617SShirley Her (SC) 
1867d440617SShirley Her (SC) /*
1877d440617SShirley Her (SC)  * This function is used to detect dll lock status.
1887d440617SShirley Her (SC)  * Since the dll lock status bit will toggle randomly
1897d440617SShirley Her (SC)  * with very short interval which needs to be polled
1907d440617SShirley Her (SC)  * as fast as possible. Set sleep_us as 1 microsecond.
1917d440617SShirley Her (SC)  */
sdhci_o2_wait_dll_detect_lock(struct sdhci_host * host)1927d440617SShirley Her (SC) static int sdhci_o2_wait_dll_detect_lock(struct sdhci_host *host)
1937d440617SShirley Her (SC) {
1947d440617SShirley Her (SC) 	u32	scratch32 = 0;
1957d440617SShirley Her (SC) 
1967d440617SShirley Her (SC) 	return readx_poll_timeout(sdhci_o2_pll_dll_wdt_control, host,
1977d440617SShirley Her (SC) 		scratch32, !(scratch32 & O2_DLL_LOCK_STATUS), 1, 1000000);
1987d440617SShirley Her (SC) }
1997d440617SShirley Her (SC) 
sdhci_o2_set_tuning_mode(struct sdhci_host * host)2000086fc21Sernest.zhang static void sdhci_o2_set_tuning_mode(struct sdhci_host *host)
2010086fc21Sernest.zhang {
2020086fc21Sernest.zhang 	u16 reg;
2030086fc21Sernest.zhang 
2040086fc21Sernest.zhang 	/* enable hardware tuning */
2050086fc21Sernest.zhang 	reg = sdhci_readw(host, O2_SD_VENDOR_SETTING);
2060086fc21Sernest.zhang 	reg &= ~O2_SD_HW_TUNING_DISABLE;
2070086fc21Sernest.zhang 	sdhci_writew(host, reg, O2_SD_VENDOR_SETTING);
2080086fc21Sernest.zhang }
2090086fc21Sernest.zhang 
__sdhci_o2_execute_tuning(struct sdhci_host * host,u32 opcode)2100086fc21Sernest.zhang static void __sdhci_o2_execute_tuning(struct sdhci_host *host, u32 opcode)
2110086fc21Sernest.zhang {
2120086fc21Sernest.zhang 	int i;
2130086fc21Sernest.zhang 
2147b7d897eSshirley her 	sdhci_send_tuning(host, opcode);
2150086fc21Sernest.zhang 
2160086fc21Sernest.zhang 	for (i = 0; i < 150; i++) {
2170086fc21Sernest.zhang 		u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2180086fc21Sernest.zhang 
2190086fc21Sernest.zhang 		if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
2200086fc21Sernest.zhang 			if (ctrl & SDHCI_CTRL_TUNED_CLK) {
2210086fc21Sernest.zhang 				host->tuning_done = true;
2220086fc21Sernest.zhang 				return;
2230086fc21Sernest.zhang 			}
2240086fc21Sernest.zhang 			pr_warn("%s: HW tuning failed !\n",
2250086fc21Sernest.zhang 				mmc_hostname(host->mmc));
2260086fc21Sernest.zhang 			break;
2270086fc21Sernest.zhang 		}
2280086fc21Sernest.zhang 
2290086fc21Sernest.zhang 		mdelay(1);
2300086fc21Sernest.zhang 	}
2310086fc21Sernest.zhang 
2320086fc21Sernest.zhang 	pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
2330086fc21Sernest.zhang 		mmc_hostname(host->mmc));
2340086fc21Sernest.zhang 	sdhci_reset_tuning(host);
2350086fc21Sernest.zhang }
2360086fc21Sernest.zhang 
2377d440617SShirley Her (SC) /*
2387d440617SShirley Her (SC)  * This function is used to fix o2 dll shift issue.
2397d440617SShirley Her (SC)  * It isn't necessary to detect card present before recovery.
2407d440617SShirley Her (SC)  * Firstly, it is used by bht emmc card, which is embedded.
2417d440617SShirley Her (SC)  * Second, before call recovery card present will be detected
2427d440617SShirley Her (SC)  * outside of the execute tuning function.
2437d440617SShirley Her (SC)  */
sdhci_o2_dll_recovery(struct sdhci_host * host)2447d440617SShirley Her (SC) static int sdhci_o2_dll_recovery(struct sdhci_host *host)
2457d440617SShirley Her (SC) {
2467d440617SShirley Her (SC) 	int ret = 0;
2477d440617SShirley Her (SC) 	u8 scratch_8 = 0;
2487d440617SShirley Her (SC) 	u32 scratch_32 = 0;
2497d440617SShirley Her (SC) 	struct sdhci_pci_slot *slot = sdhci_priv(host);
2507d440617SShirley Her (SC) 	struct sdhci_pci_chip *chip = slot->chip;
2517d440617SShirley Her (SC) 	struct o2_host *o2_host = sdhci_pci_priv(slot);
2527d440617SShirley Her (SC) 
2537d440617SShirley Her (SC) 	/* UnLock WP */
2547d440617SShirley Her (SC) 	pci_read_config_byte(chip->pdev,
2557d440617SShirley Her (SC) 			O2_SD_LOCK_WP, &scratch_8);
2567d440617SShirley Her (SC) 	scratch_8 &= 0x7f;
2577d440617SShirley Her (SC) 	pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
2587d440617SShirley Her (SC) 	while (o2_host->dll_adjust_count < DMDN_SZ && !ret) {
2597d440617SShirley Her (SC) 		/* Disable clock */
2607d440617SShirley Her (SC) 		sdhci_writeb(host, 0, SDHCI_CLOCK_CONTROL);
2617d440617SShirley Her (SC) 
2627d440617SShirley Her (SC) 		/* PLL software reset */
2637d440617SShirley Her (SC) 		scratch_32 = sdhci_readl(host, O2_PLL_DLL_WDT_CONTROL1);
2647d440617SShirley Her (SC) 		scratch_32 |= O2_PLL_SOFT_RESET;
2657d440617SShirley Her (SC) 		sdhci_writel(host, scratch_32, O2_PLL_DLL_WDT_CONTROL1);
2667d440617SShirley Her (SC) 
2677d440617SShirley Her (SC) 		pci_read_config_dword(chip->pdev,
2687d440617SShirley Her (SC) 					    O2_SD_FUNC_REG4,
2697d440617SShirley Her (SC) 					    &scratch_32);
2707d440617SShirley Her (SC) 		/* Enable Base Clk setting change */
2717d440617SShirley Her (SC) 		scratch_32 |= O2_SD_FREG4_ENABLE_CLK_SET;
2727d440617SShirley Her (SC) 		pci_write_config_dword(chip->pdev, O2_SD_FUNC_REG4, scratch_32);
2737d440617SShirley Her (SC) 		o2_pci_set_baseclk(chip, dmdn_table[o2_host->dll_adjust_count]);
2747d440617SShirley Her (SC) 
2757d440617SShirley Her (SC) 		/* Enable internal clock */
2767d440617SShirley Her (SC) 		scratch_8 = SDHCI_CLOCK_INT_EN;
2777d440617SShirley Her (SC) 		sdhci_writeb(host, scratch_8, SDHCI_CLOCK_CONTROL);
2787d440617SShirley Her (SC) 
2797d440617SShirley Her (SC) 		if (sdhci_o2_get_cd(host->mmc)) {
2807d440617SShirley Her (SC) 			/*
2817d440617SShirley Her (SC) 			 * need wait at least 5ms for dll status stable,
2827d440617SShirley Her (SC) 			 * after enable internal clock
2837d440617SShirley Her (SC) 			 */
2847d440617SShirley Her (SC) 			usleep_range(5000, 6000);
2857d440617SShirley Her (SC) 			if (sdhci_o2_wait_dll_detect_lock(host)) {
2867d440617SShirley Her (SC) 				scratch_8 |= SDHCI_CLOCK_CARD_EN;
2877d440617SShirley Her (SC) 				sdhci_writeb(host, scratch_8,
2887d440617SShirley Her (SC) 					SDHCI_CLOCK_CONTROL);
2897d440617SShirley Her (SC) 				ret = 1;
2907d440617SShirley Her (SC) 			} else {
2917d440617SShirley Her (SC) 				pr_warn("%s: DLL unlocked when dll_adjust_count is %d.\n",
2927d440617SShirley Her (SC) 					mmc_hostname(host->mmc),
2937d440617SShirley Her (SC) 					o2_host->dll_adjust_count);
2947d440617SShirley Her (SC) 			}
2957d440617SShirley Her (SC) 		} else {
2967d440617SShirley Her (SC) 			pr_err("%s: card present detect failed.\n",
2977d440617SShirley Her (SC) 				mmc_hostname(host->mmc));
2987d440617SShirley Her (SC) 			break;
2997d440617SShirley Her (SC) 		}
3007d440617SShirley Her (SC) 
3017d440617SShirley Her (SC) 		o2_host->dll_adjust_count++;
3027d440617SShirley Her (SC) 	}
3037d440617SShirley Her (SC) 	if (!ret && o2_host->dll_adjust_count == DMDN_SZ)
3047d440617SShirley Her (SC) 		pr_err("%s: DLL adjust over max times\n",
3057d440617SShirley Her (SC) 		mmc_hostname(host->mmc));
3067d440617SShirley Her (SC) 	/* Lock WP */
3077d440617SShirley Her (SC) 	pci_read_config_byte(chip->pdev,
3087d440617SShirley Her (SC) 				   O2_SD_LOCK_WP, &scratch_8);
3097d440617SShirley Her (SC) 	scratch_8 |= 0x80;
3107d440617SShirley Her (SC) 	pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
3117d440617SShirley Her (SC) 	return ret;
3127d440617SShirley Her (SC) }
3137d440617SShirley Her (SC) 
sdhci_o2_execute_tuning(struct mmc_host * mmc,u32 opcode)3140086fc21Sernest.zhang static int sdhci_o2_execute_tuning(struct mmc_host *mmc, u32 opcode)
3150086fc21Sernest.zhang {
3160086fc21Sernest.zhang 	struct sdhci_host *host = mmc_priv(mmc);
3174be33cf1SFred Ai 	struct sdhci_pci_slot *slot = sdhci_priv(host);
3184be33cf1SFred Ai 	struct sdhci_pci_chip *chip = slot->chip;
3190086fc21Sernest.zhang 	int current_bus_width = 0;
3201ad9f880SShirley Her 	u32 scratch32 = 0;
3211ad9f880SShirley Her 	u16 scratch = 0;
3224be33cf1SFred Ai 	u8  scratch_8 = 0;
3234be33cf1SFred Ai 	u32 reg_val;
3240086fc21Sernest.zhang 
3250086fc21Sernest.zhang 	/*
32650aeccccSChevron Li 	 * This handler implements the hardware tuning that is specific to
3270086fc21Sernest.zhang 	 * this controller.  Fall back to the standard method for other TIMING.
3280086fc21Sernest.zhang 	 */
3297b7d897eSshirley her 	if ((host->timing != MMC_TIMING_MMC_HS200) &&
33050aeccccSChevron Li 		(host->timing != MMC_TIMING_UHS_SDR104) &&
33150aeccccSChevron Li 		(host->timing != MMC_TIMING_UHS_SDR50))
3320086fc21Sernest.zhang 		return sdhci_execute_tuning(mmc, opcode);
3330086fc21Sernest.zhang 
334b98e7e8dSChanWoo Lee 	if (WARN_ON(!mmc_op_tuning(opcode)))
3350086fc21Sernest.zhang 		return -EINVAL;
3361ad9f880SShirley Her 
3371ad9f880SShirley Her 	/* Force power mode enter L0 */
3381ad9f880SShirley Her 	scratch = sdhci_readw(host, O2_SD_MISC_CTRL);
3391ad9f880SShirley Her 	scratch |= O2_SD_PWR_FORCE_L0;
3401ad9f880SShirley Her 	sdhci_writew(host, scratch, O2_SD_MISC_CTRL);
3411ad9f880SShirley Her 
3423d757ddbSChevron Li 	/* Update output phase */
3433d757ddbSChevron Li 	switch (chip->pdev->device) {
3443d757ddbSChevron Li 	case PCI_DEVICE_ID_O2_SDS0:
3453d757ddbSChevron Li 	case PCI_DEVICE_ID_O2_SEABIRD0:
3463d757ddbSChevron Li 	case PCI_DEVICE_ID_O2_SEABIRD1:
3473d757ddbSChevron Li 	case PCI_DEVICE_ID_O2_SDS1:
3483d757ddbSChevron Li 	case PCI_DEVICE_ID_O2_FUJIN2:
3494be33cf1SFred Ai 		/* Stop clk */
3504be33cf1SFred Ai 		reg_val = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
3514be33cf1SFred Ai 		reg_val &= ~SDHCI_CLOCK_CARD_EN;
3524be33cf1SFred Ai 		sdhci_writew(host, reg_val, SDHCI_CLOCK_CONTROL);
3534be33cf1SFred Ai 
3543d757ddbSChevron Li 		if (host->timing == MMC_TIMING_MMC_HS200 ||
3553d757ddbSChevron Li 		    host->timing == MMC_TIMING_UHS_SDR104) {
3564be33cf1SFred Ai 			/* UnLock WP */
3574be33cf1SFred Ai 			pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch_8);
3584be33cf1SFred Ai 			scratch_8 &= 0x7f;
3594be33cf1SFred Ai 			pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
3604be33cf1SFred Ai 
3614be33cf1SFred Ai 			/* Set pcr 0x354[16] to choose dll clock, and set the default phase */
3624be33cf1SFred Ai 			pci_read_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, &reg_val);
3634be33cf1SFred Ai 			reg_val &= ~(O2_SD_SEL_DLL | O2_SD_PHASE_MASK);
3644be33cf1SFred Ai 			reg_val |= (O2_SD_SEL_DLL | O2_SD_FIX_PHASE);
3654be33cf1SFred Ai 			pci_write_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, reg_val);
3664be33cf1SFred Ai 
3674be33cf1SFred Ai 			/* Lock WP */
3684be33cf1SFred Ai 			pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch_8);
3694be33cf1SFred Ai 			scratch_8 |= 0x80;
3704be33cf1SFred Ai 			pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
37151dfc614SFred 		}
3723d757ddbSChevron Li 
3734be33cf1SFred Ai 		/* Start clk */
3744be33cf1SFred Ai 		reg_val = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
3754be33cf1SFred Ai 		reg_val |= SDHCI_CLOCK_CARD_EN;
3764be33cf1SFred Ai 		sdhci_writew(host, reg_val, SDHCI_CLOCK_CONTROL);
3773d757ddbSChevron Li 		break;
3783d757ddbSChevron Li 	default:
3793d757ddbSChevron Li 		break;
3803d757ddbSChevron Li 	}
3814be33cf1SFred Ai 
3821ad9f880SShirley Her 	/* wait DLL lock, timeout value 5ms */
3831ad9f880SShirley Her 	if (readx_poll_timeout(sdhci_o2_pll_dll_wdt_control, host,
3841ad9f880SShirley Her 		scratch32, (scratch32 & O2_DLL_LOCK_STATUS), 1, 5000))
3851ad9f880SShirley Her 		pr_warn("%s: DLL can't lock in 5ms after force L0 during tuning.\n",
3861ad9f880SShirley Her 				mmc_hostname(host->mmc));
3877d440617SShirley Her (SC) 	/*
3887d440617SShirley Her (SC) 	 * Judge the tuning reason, whether caused by dll shift
3897d440617SShirley Her (SC) 	 * If cause by dll shift, should call sdhci_o2_dll_recovery
3907d440617SShirley Her (SC) 	 */
3917d440617SShirley Her (SC) 	if (!sdhci_o2_wait_dll_detect_lock(host))
3927d440617SShirley Her (SC) 		if (!sdhci_o2_dll_recovery(host)) {
3937d440617SShirley Her (SC) 			pr_err("%s: o2 dll recovery failed\n",
3947d440617SShirley Her (SC) 				mmc_hostname(host->mmc));
3957d440617SShirley Her (SC) 			return -EINVAL;
3967d440617SShirley Her (SC) 		}
3970086fc21Sernest.zhang 	/*
3980086fc21Sernest.zhang 	 * o2 sdhci host didn't support 8bit emmc tuning
3990086fc21Sernest.zhang 	 */
4000086fc21Sernest.zhang 	if (mmc->ios.bus_width == MMC_BUS_WIDTH_8) {
4010086fc21Sernest.zhang 		current_bus_width = mmc->ios.bus_width;
4020f7b79a4SRaul E Rangel 		mmc->ios.bus_width = MMC_BUS_WIDTH_4;
4030086fc21Sernest.zhang 		sdhci_set_bus_width(host, MMC_BUS_WIDTH_4);
4040086fc21Sernest.zhang 	}
4050086fc21Sernest.zhang 
4060086fc21Sernest.zhang 	sdhci_o2_set_tuning_mode(host);
4070086fc21Sernest.zhang 
4080086fc21Sernest.zhang 	sdhci_start_tuning(host);
4090086fc21Sernest.zhang 
4100086fc21Sernest.zhang 	__sdhci_o2_execute_tuning(host, opcode);
4110086fc21Sernest.zhang 
4120086fc21Sernest.zhang 	sdhci_end_tuning(host);
4130086fc21Sernest.zhang 
4140f7b79a4SRaul E Rangel 	if (current_bus_width == MMC_BUS_WIDTH_8) {
4150f7b79a4SRaul E Rangel 		mmc->ios.bus_width = MMC_BUS_WIDTH_8;
4160086fc21Sernest.zhang 		sdhci_set_bus_width(host, current_bus_width);
4170f7b79a4SRaul E Rangel 	}
4180086fc21Sernest.zhang 
4191ad9f880SShirley Her 	/* Cancel force power mode enter L0 */
4201ad9f880SShirley Her 	scratch = sdhci_readw(host, O2_SD_MISC_CTRL);
4211ad9f880SShirley Her 	scratch &= ~(O2_SD_PWR_FORCE_L0);
4221ad9f880SShirley Her 	sdhci_writew(host, scratch, O2_SD_MISC_CTRL);
4231ad9f880SShirley Her 
4247b7d897eSshirley her 	sdhci_reset(host, SDHCI_RESET_CMD);
4257b7d897eSshirley her 	sdhci_reset(host, SDHCI_RESET_DATA);
4267b7d897eSshirley her 
4270086fc21Sernest.zhang 	host->flags &= ~SDHCI_HS400_TUNING;
4280086fc21Sernest.zhang 	return 0;
4290086fc21Sernest.zhang }
43001acf691SAdam Lee 
o2_pci_led_enable(struct sdhci_pci_chip * chip)431706adf6bSPeter Guo static void o2_pci_led_enable(struct sdhci_pci_chip *chip)
432706adf6bSPeter Guo {
433706adf6bSPeter Guo 	int ret;
434706adf6bSPeter Guo 	u32 scratch_32;
435706adf6bSPeter Guo 
436706adf6bSPeter Guo 	/* Set led of SD host function enable */
437706adf6bSPeter Guo 	ret = pci_read_config_dword(chip->pdev,
438706adf6bSPeter Guo 				    O2_SD_FUNC_REG0, &scratch_32);
439706adf6bSPeter Guo 	if (ret)
440706adf6bSPeter Guo 		return;
441706adf6bSPeter Guo 
442706adf6bSPeter Guo 	scratch_32 &= ~O2_SD_FREG0_LEDOFF;
443706adf6bSPeter Guo 	pci_write_config_dword(chip->pdev,
444706adf6bSPeter Guo 			       O2_SD_FUNC_REG0, scratch_32);
445706adf6bSPeter Guo 
446706adf6bSPeter Guo 	ret = pci_read_config_dword(chip->pdev,
447706adf6bSPeter Guo 				    O2_SD_TEST_REG, &scratch_32);
448706adf6bSPeter Guo 	if (ret)
449706adf6bSPeter Guo 		return;
450706adf6bSPeter Guo 
451706adf6bSPeter Guo 	scratch_32 |= O2_SD_LED_ENABLE;
452706adf6bSPeter Guo 	pci_write_config_dword(chip->pdev,
453706adf6bSPeter Guo 			       O2_SD_TEST_REG, scratch_32);
454706adf6bSPeter Guo }
455706adf6bSPeter Guo 
sdhci_pci_o2_fujin2_pci_init(struct sdhci_pci_chip * chip)456f0cbd780SBen Hutchings static void sdhci_pci_o2_fujin2_pci_init(struct sdhci_pci_chip *chip)
45701acf691SAdam Lee {
45801acf691SAdam Lee 	u32 scratch_32;
45901acf691SAdam Lee 	int ret;
46001acf691SAdam Lee 	/* Improve write performance for SD3.0 */
46101acf691SAdam Lee 	ret = pci_read_config_dword(chip->pdev, O2_SD_DEV_CTRL, &scratch_32);
46201acf691SAdam Lee 	if (ret)
46301acf691SAdam Lee 		return;
46401acf691SAdam Lee 	scratch_32 &= ~((1 << 12) | (1 << 13) | (1 << 14));
46501acf691SAdam Lee 	pci_write_config_dword(chip->pdev, O2_SD_DEV_CTRL, scratch_32);
46601acf691SAdam Lee 
46701acf691SAdam Lee 	/* Enable Link abnormal reset generating Reset */
46801acf691SAdam Lee 	ret = pci_read_config_dword(chip->pdev, O2_SD_MISC_REG5, &scratch_32);
46901acf691SAdam Lee 	if (ret)
47001acf691SAdam Lee 		return;
47101acf691SAdam Lee 	scratch_32 &= ~((1 << 19) | (1 << 11));
47201acf691SAdam Lee 	scratch_32 |= (1 << 10);
47301acf691SAdam Lee 	pci_write_config_dword(chip->pdev, O2_SD_MISC_REG5, scratch_32);
47401acf691SAdam Lee 
47501acf691SAdam Lee 	/* set card power over current protection */
47601acf691SAdam Lee 	ret = pci_read_config_dword(chip->pdev, O2_SD_TEST_REG, &scratch_32);
47701acf691SAdam Lee 	if (ret)
47801acf691SAdam Lee 		return;
47901acf691SAdam Lee 	scratch_32 |= (1 << 4);
48001acf691SAdam Lee 	pci_write_config_dword(chip->pdev, O2_SD_TEST_REG, scratch_32);
48101acf691SAdam Lee 
48201acf691SAdam Lee 	/* adjust the output delay for SD mode */
48301acf691SAdam Lee 	pci_write_config_dword(chip->pdev, O2_SD_DELAY_CTRL, 0x00002492);
48401acf691SAdam Lee 
48501acf691SAdam Lee 	/* Set the output voltage setting of Aux 1.2v LDO */
48601acf691SAdam Lee 	ret = pci_read_config_dword(chip->pdev, O2_SD_LD0_CTRL, &scratch_32);
48701acf691SAdam Lee 	if (ret)
48801acf691SAdam Lee 		return;
48901acf691SAdam Lee 	scratch_32 &= ~(3 << 12);
49001acf691SAdam Lee 	pci_write_config_dword(chip->pdev, O2_SD_LD0_CTRL, scratch_32);
49101acf691SAdam Lee 
49201acf691SAdam Lee 	/* Set Max power supply capability of SD host */
49301acf691SAdam Lee 	ret = pci_read_config_dword(chip->pdev, O2_SD_CAP_REG0, &scratch_32);
49401acf691SAdam Lee 	if (ret)
49501acf691SAdam Lee 		return;
49601acf691SAdam Lee 	scratch_32 &= ~(0x01FE);
49701acf691SAdam Lee 	scratch_32 |= 0x00CC;
49801acf691SAdam Lee 	pci_write_config_dword(chip->pdev, O2_SD_CAP_REG0, scratch_32);
49901acf691SAdam Lee 	/* Set DLL Tuning Window */
50001acf691SAdam Lee 	ret = pci_read_config_dword(chip->pdev,
50101acf691SAdam Lee 				    O2_SD_TUNING_CTRL, &scratch_32);
50201acf691SAdam Lee 	if (ret)
50301acf691SAdam Lee 		return;
50401acf691SAdam Lee 	scratch_32 &= ~(0x000000FF);
50501acf691SAdam Lee 	scratch_32 |= 0x00000066;
50601acf691SAdam Lee 	pci_write_config_dword(chip->pdev, O2_SD_TUNING_CTRL, scratch_32);
50701acf691SAdam Lee 
50801acf691SAdam Lee 	/* Set UHS2 T_EIDLE */
50901acf691SAdam Lee 	ret = pci_read_config_dword(chip->pdev,
51001acf691SAdam Lee 				    O2_SD_UHS2_L1_CTRL, &scratch_32);
51101acf691SAdam Lee 	if (ret)
51201acf691SAdam Lee 		return;
51301acf691SAdam Lee 	scratch_32 &= ~(0x000000FC);
51401acf691SAdam Lee 	scratch_32 |= 0x00000084;
51501acf691SAdam Lee 	pci_write_config_dword(chip->pdev, O2_SD_UHS2_L1_CTRL, scratch_32);
51601acf691SAdam Lee 
51701acf691SAdam Lee 	/* Set UHS2 Termination */
51801acf691SAdam Lee 	ret = pci_read_config_dword(chip->pdev, O2_SD_FUNC_REG3, &scratch_32);
51901acf691SAdam Lee 	if (ret)
52001acf691SAdam Lee 		return;
52101acf691SAdam Lee 	scratch_32 &= ~((1 << 21) | (1 << 30));
52201acf691SAdam Lee 
52301acf691SAdam Lee 	pci_write_config_dword(chip->pdev, O2_SD_FUNC_REG3, scratch_32);
52401acf691SAdam Lee 
52501acf691SAdam Lee 	/* Set L1 Entrance Timer */
52601acf691SAdam Lee 	ret = pci_read_config_dword(chip->pdev, O2_SD_CAPS, &scratch_32);
52701acf691SAdam Lee 	if (ret)
52801acf691SAdam Lee 		return;
52901acf691SAdam Lee 	scratch_32 &= ~(0xf0000000);
53001acf691SAdam Lee 	scratch_32 |= 0x30000000;
53101acf691SAdam Lee 	pci_write_config_dword(chip->pdev, O2_SD_CAPS, scratch_32);
53201acf691SAdam Lee 
53301acf691SAdam Lee 	ret = pci_read_config_dword(chip->pdev,
53401acf691SAdam Lee 				    O2_SD_MISC_CTRL4, &scratch_32);
53501acf691SAdam Lee 	if (ret)
53601acf691SAdam Lee 		return;
53701acf691SAdam Lee 	scratch_32 &= ~(0x000f0000);
53801acf691SAdam Lee 	scratch_32 |= 0x00080000;
53901acf691SAdam Lee 	pci_write_config_dword(chip->pdev, O2_SD_MISC_CTRL4, scratch_32);
54001acf691SAdam Lee }
54101acf691SAdam Lee 
sdhci_pci_o2_enable_msi(struct sdhci_pci_chip * chip,struct sdhci_host * host)54202a3c0bdSernest.zhang static void sdhci_pci_o2_enable_msi(struct sdhci_pci_chip *chip,
54302a3c0bdSernest.zhang 				    struct sdhci_host *host)
54402a3c0bdSernest.zhang {
54502a3c0bdSernest.zhang 	int ret;
54602a3c0bdSernest.zhang 
54702a3c0bdSernest.zhang 	ret = pci_find_capability(chip->pdev, PCI_CAP_ID_MSI);
54802a3c0bdSernest.zhang 	if (!ret) {
5490818d197SColin Ian King 		pr_info("%s: unsupported MSI, use INTx irq\n",
55002a3c0bdSernest.zhang 			mmc_hostname(host->mmc));
55102a3c0bdSernest.zhang 		return;
55202a3c0bdSernest.zhang 	}
55302a3c0bdSernest.zhang 
55402a3c0bdSernest.zhang 	ret = pci_alloc_irq_vectors(chip->pdev, 1, 1,
55502a3c0bdSernest.zhang 				    PCI_IRQ_MSI | PCI_IRQ_MSIX);
55602a3c0bdSernest.zhang 	if (ret < 0) {
55702a3c0bdSernest.zhang 		pr_err("%s: enable PCI MSI failed, err=%d\n",
55802a3c0bdSernest.zhang 		       mmc_hostname(host->mmc), ret);
55902a3c0bdSernest.zhang 		return;
56002a3c0bdSernest.zhang 	}
56102a3c0bdSernest.zhang 
56202a3c0bdSernest.zhang 	host->irq = pci_irq_vector(chip->pdev, 0);
56302a3c0bdSernest.zhang }
56402a3c0bdSernest.zhang 
sdhci_o2_enable_clk(struct sdhci_host * host,u16 clk)56569d91ed1SErnest Zhang(WH) static void sdhci_o2_enable_clk(struct sdhci_host *host, u16 clk)
56669d91ed1SErnest Zhang(WH) {
56769d91ed1SErnest Zhang(WH) 	/* Enable internal clock */
56869d91ed1SErnest Zhang(WH) 	clk |= SDHCI_CLOCK_INT_EN;
56969d91ed1SErnest Zhang(WH) 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
57069d91ed1SErnest Zhang(WH) 
5717d440617SShirley Her (SC) 	sdhci_o2_enable_internal_clock(host);
57269d91ed1SErnest Zhang(WH) 	if (sdhci_o2_get_cd(host->mmc)) {
57369d91ed1SErnest Zhang(WH) 		clk |= SDHCI_CLOCK_CARD_EN;
57469d91ed1SErnest Zhang(WH) 		sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
57569d91ed1SErnest Zhang(WH) 	}
57669d91ed1SErnest Zhang(WH) }
57769d91ed1SErnest Zhang(WH) 
sdhci_pci_o2_set_clock(struct sdhci_host * host,unsigned int clock)578580b946eSZou Wei static void sdhci_pci_o2_set_clock(struct sdhci_host *host, unsigned int clock)
57969d91ed1SErnest Zhang(WH) {
58069d91ed1SErnest Zhang(WH) 	u16 clk;
5817b7d897eSshirley her 	u8 scratch;
5827b7d897eSshirley her 	u32 scratch_32;
5833d757ddbSChevron Li 	u32 dmdn_208m, dmdn_200m;
5847b7d897eSshirley her 	struct sdhci_pci_slot *slot = sdhci_priv(host);
5857b7d897eSshirley her 	struct sdhci_pci_chip *chip = slot->chip;
58669d91ed1SErnest Zhang(WH) 
58769d91ed1SErnest Zhang(WH) 	host->mmc->actual_clock = 0;
58869d91ed1SErnest Zhang(WH) 
58969d91ed1SErnest Zhang(WH) 	sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
59069d91ed1SErnest Zhang(WH) 
59169d91ed1SErnest Zhang(WH) 	if (clock == 0)
59269d91ed1SErnest Zhang(WH) 		return;
59369d91ed1SErnest Zhang(WH) 
5944be33cf1SFred Ai 	/* UnLock WP */
5957b7d897eSshirley her 	pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
5967b7d897eSshirley her 	scratch &= 0x7f;
5977b7d897eSshirley her 	pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
5987b7d897eSshirley her 
5993d757ddbSChevron Li 	if (chip->pdev->device == PCI_DEVICE_ID_O2_GG8_9860 ||
6003d757ddbSChevron Li 	    chip->pdev->device == PCI_DEVICE_ID_O2_GG8_9861 ||
6013d757ddbSChevron Li 	    chip->pdev->device == PCI_DEVICE_ID_O2_GG8_9862 ||
6023d757ddbSChevron Li 	    chip->pdev->device == PCI_DEVICE_ID_O2_GG8_9863) {
6033d757ddbSChevron Li 		dmdn_208m = 0x2c500000;
6043d757ddbSChevron Li 		dmdn_200m = 0x25200000;
6053d757ddbSChevron Li 	} else {
6063d757ddbSChevron Li 		dmdn_208m = 0x2c280000;
6073d757ddbSChevron Li 		dmdn_200m = 0x25100000;
6083d757ddbSChevron Li 	}
6093d757ddbSChevron Li 
6104be33cf1SFred Ai 	if ((host->timing == MMC_TIMING_UHS_SDR104) && (clock == 200000000)) {
6117b7d897eSshirley her 		pci_read_config_dword(chip->pdev, O2_SD_PLL_SETTING, &scratch_32);
6127b7d897eSshirley her 
6133d757ddbSChevron Li 		if ((scratch_32 & 0xFFFF0000) != dmdn_208m)
6143d757ddbSChevron Li 			o2_pci_set_baseclk(chip, dmdn_208m);
615a7c18e5cSFred Ai 	} else {
616a7c18e5cSFred Ai 		pci_read_config_dword(chip->pdev, O2_SD_PLL_SETTING, &scratch_32);
617a7c18e5cSFred Ai 
6183d757ddbSChevron Li 		if ((scratch_32 & 0xFFFF0000) != dmdn_200m)
6193d757ddbSChevron Li 			o2_pci_set_baseclk(chip, dmdn_200m);
6204be33cf1SFred Ai 	}
6217b7d897eSshirley her 
6224be33cf1SFred Ai 	pci_read_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, &scratch_32);
6234be33cf1SFred Ai 	scratch_32 &= ~(O2_SD_SEL_DLL | O2_SD_PHASE_MASK);
6244be33cf1SFred Ai 	pci_write_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, scratch_32);
6254be33cf1SFred Ai 
6264be33cf1SFred Ai 	/* Lock WP */
6277b7d897eSshirley her 	pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
6287b7d897eSshirley her 	scratch |= 0x80;
6297b7d897eSshirley her 	pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
6307b7d897eSshirley her 
63169d91ed1SErnest Zhang(WH) 	clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
63269d91ed1SErnest Zhang(WH) 	sdhci_o2_enable_clk(host, clk);
63369d91ed1SErnest Zhang(WH) }
63469d91ed1SErnest Zhang(WH) 
sdhci_pci_o2_init_sd_express(struct mmc_host * mmc,struct mmc_ios * ios)635da2a69cfSChevron Li static int sdhci_pci_o2_init_sd_express(struct mmc_host *mmc, struct mmc_ios *ios)
636da2a69cfSChevron Li {
637da2a69cfSChevron Li 	struct sdhci_host *host = mmc_priv(mmc);
638da2a69cfSChevron Li 	struct sdhci_pci_slot *slot = sdhci_priv(host);
639da2a69cfSChevron Li 	struct sdhci_pci_chip *chip = slot->chip;
640da2a69cfSChevron Li 	u8 scratch8;
641da2a69cfSChevron Li 	u16 scratch16;
642da2a69cfSChevron Li 	int ret;
643da2a69cfSChevron Li 
644da2a69cfSChevron Li 	/* Disable clock */
645da2a69cfSChevron Li 	sdhci_writeb(host, 0, SDHCI_CLOCK_CONTROL);
646da2a69cfSChevron Li 
647da2a69cfSChevron Li 	/* Set VDD2 voltage*/
648da2a69cfSChevron Li 	scratch8 = sdhci_readb(host, SDHCI_POWER_CONTROL);
649da2a69cfSChevron Li 	scratch8 &= 0x0F;
650da2a69cfSChevron Li 	if (host->mmc->ios.timing == MMC_TIMING_SD_EXP_1_2V &&
651da2a69cfSChevron Li 	    host->mmc->caps2 & MMC_CAP2_SD_EXP_1_2V) {
652da2a69cfSChevron Li 		scratch8 |= SDHCI_VDD2_POWER_ON | SDHCI_VDD2_POWER_120;
653da2a69cfSChevron Li 	} else {
654da2a69cfSChevron Li 		scratch8 |= SDHCI_VDD2_POWER_ON | SDHCI_VDD2_POWER_180;
655da2a69cfSChevron Li 	}
656da2a69cfSChevron Li 
657da2a69cfSChevron Li 	sdhci_writeb(host, scratch8, SDHCI_POWER_CONTROL);
658da2a69cfSChevron Li 
659da2a69cfSChevron Li 	/* UnLock WP */
660da2a69cfSChevron Li 	pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch8);
661da2a69cfSChevron Li 	scratch8 &= 0x7f;
662da2a69cfSChevron Li 	pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch8);
663da2a69cfSChevron Li 
664da2a69cfSChevron Li 	/* Wait for express card clkreqn assert */
665da2a69cfSChevron Li 	ret = read_poll_timeout(sdhci_readb, scratch8, !(scratch8 & BIT(0)),
666da2a69cfSChevron Li 				1, 30000, false, host, O2_SD_EXP_INT_REG);
667da2a69cfSChevron Li 
668da2a69cfSChevron Li 	if (!ret) {
669da2a69cfSChevron Li 		/* Switch to PCIe mode */
670da2a69cfSChevron Li 		scratch16 = sdhci_readw(host, O2_SD_PCIE_SWITCH);
671da2a69cfSChevron Li 		scratch16 |= BIT(8);
672da2a69cfSChevron Li 		sdhci_writew(host, scratch16, O2_SD_PCIE_SWITCH);
673da2a69cfSChevron Li 	} else {
674da2a69cfSChevron Li 		/* Power off VDD2 voltage*/
675da2a69cfSChevron Li 		scratch8 = sdhci_readb(host, SDHCI_POWER_CONTROL);
676da2a69cfSChevron Li 		scratch8 &= 0x0F;
677da2a69cfSChevron Li 		sdhci_writeb(host, scratch8, SDHCI_POWER_CONTROL);
678da2a69cfSChevron Li 
679da2a69cfSChevron Li 		/* Keep mode as UHSI */
680da2a69cfSChevron Li 		pci_read_config_word(chip->pdev, O2_SD_PARA_SET_REG1, &scratch16);
681da2a69cfSChevron Li 		scratch16 &= ~BIT(11);
682da2a69cfSChevron Li 		pci_write_config_word(chip->pdev, O2_SD_PARA_SET_REG1, scratch16);
683da2a69cfSChevron Li 
684da2a69cfSChevron Li 		host->mmc->ios.timing = MMC_TIMING_LEGACY;
685da2a69cfSChevron Li 		pr_info("%s: Express card initialization failed, falling back to Legacy\n",
686da2a69cfSChevron Li 			mmc_hostname(host->mmc));
687da2a69cfSChevron Li 	}
688da2a69cfSChevron Li 	/* Lock WP */
689da2a69cfSChevron Li 	pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch8);
690da2a69cfSChevron Li 	scratch8 |= 0x80;
691da2a69cfSChevron Li 	pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch8);
692da2a69cfSChevron Li 
693da2a69cfSChevron Li 	return 0;
694da2a69cfSChevron Li }
695da2a69cfSChevron Li 
sdhci_pci_o2_set_power(struct sdhci_host * host,unsigned char mode,unsigned short vdd)696*4796a1a4SFred Ai static void sdhci_pci_o2_set_power(struct sdhci_host *host, unsigned char mode,  unsigned short vdd)
697*4796a1a4SFred Ai {
698*4796a1a4SFred Ai 	struct sdhci_pci_chip *chip;
699*4796a1a4SFred Ai 	struct sdhci_pci_slot *slot = sdhci_priv(host);
700*4796a1a4SFred Ai 	u32 scratch_32 = 0;
701*4796a1a4SFred Ai 	u8 scratch_8 = 0;
702*4796a1a4SFred Ai 
703*4796a1a4SFred Ai 	chip = slot->chip;
704*4796a1a4SFred Ai 
705*4796a1a4SFred Ai 	if (mode == MMC_POWER_OFF) {
706*4796a1a4SFred Ai 		/* UnLock WP */
707*4796a1a4SFred Ai 		pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch_8);
708*4796a1a4SFred Ai 		scratch_8 &= 0x7f;
709*4796a1a4SFred Ai 		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
710*4796a1a4SFred Ai 
711*4796a1a4SFred Ai 		/* Set PCR 0x354[16] to switch Clock Source back to OPE Clock */
712*4796a1a4SFred Ai 		pci_read_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, &scratch_32);
713*4796a1a4SFred Ai 		scratch_32 &= ~(O2_SD_SEL_DLL);
714*4796a1a4SFred Ai 		pci_write_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, scratch_32);
715*4796a1a4SFred Ai 
716*4796a1a4SFred Ai 		/* Lock WP */
717*4796a1a4SFred Ai 		pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch_8);
718*4796a1a4SFred Ai 		scratch_8 |= 0x80;
719*4796a1a4SFred Ai 		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
720*4796a1a4SFred Ai 	}
721*4796a1a4SFred Ai 
722*4796a1a4SFred Ai 	sdhci_set_power(host, mode, vdd);
723*4796a1a4SFred Ai }
724*4796a1a4SFred Ai 
sdhci_pci_o2_probe_slot(struct sdhci_pci_slot * slot)725580b946eSZou Wei static int sdhci_pci_o2_probe_slot(struct sdhci_pci_slot *slot)
72601acf691SAdam Lee {
72701acf691SAdam Lee 	struct sdhci_pci_chip *chip;
72801acf691SAdam Lee 	struct sdhci_host *host;
7297d440617SShirley Her (SC) 	struct o2_host *o2_host = sdhci_pci_priv(slot);
730de23f0b7SRaul E Rangel 	u32 reg, caps;
73157322d54Sernest.zhang 	int ret;
73201acf691SAdam Lee 
73301acf691SAdam Lee 	chip = slot->chip;
73401acf691SAdam Lee 	host = slot->host;
735de23f0b7SRaul E Rangel 
7367d440617SShirley Her (SC) 	o2_host->dll_adjust_count = 0;
737de23f0b7SRaul E Rangel 	caps = sdhci_readl(host, SDHCI_CAPABILITIES);
738de23f0b7SRaul E Rangel 
739de23f0b7SRaul E Rangel 	/*
740de23f0b7SRaul E Rangel 	 * mmc_select_bus_width() will test the bus to determine the actual bus
741de23f0b7SRaul E Rangel 	 * width.
742de23f0b7SRaul E Rangel 	 */
743de23f0b7SRaul E Rangel 	if (caps & SDHCI_CAN_DO_8BIT)
744de23f0b7SRaul E Rangel 		host->mmc->caps |= MMC_CAP_8_BIT_DATA;
745de23f0b7SRaul E Rangel 
7463d757ddbSChevron Li 	host->quirks2 |= SDHCI_QUIRK2_BROKEN_DDR50;
7473d757ddbSChevron Li 
7483d757ddbSChevron Li 	sdhci_pci_o2_enable_msi(chip, host);
7493d757ddbSChevron Li 
7503d757ddbSChevron Li 	host->mmc_host_ops.execute_tuning = sdhci_o2_execute_tuning;
75101acf691SAdam Lee 	switch (chip->pdev->device) {
75201acf691SAdam Lee 	case PCI_DEVICE_ID_O2_SDS0:
75301acf691SAdam Lee 	case PCI_DEVICE_ID_O2_SEABIRD0:
75401acf691SAdam Lee 	case PCI_DEVICE_ID_O2_SEABIRD1:
75501acf691SAdam Lee 	case PCI_DEVICE_ID_O2_SDS1:
75601acf691SAdam Lee 	case PCI_DEVICE_ID_O2_FUJIN2:
75701acf691SAdam Lee 		reg = sdhci_readl(host, O2_SD_VENDOR_SETTING);
75801acf691SAdam Lee 		if (reg & 0x1)
75901acf691SAdam Lee 			host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
76001acf691SAdam Lee 
76157322d54Sernest.zhang 		if (chip->pdev->device == PCI_DEVICE_ID_O2_SEABIRD0) {
76257322d54Sernest.zhang 			ret = pci_read_config_dword(chip->pdev,
76357322d54Sernest.zhang 						    O2_SD_MISC_SETTING, &reg);
76457322d54Sernest.zhang 			if (ret)
76557322d54Sernest.zhang 				return -EIO;
76657322d54Sernest.zhang 			if (reg & (1 << 4)) {
76757322d54Sernest.zhang 				pr_info("%s: emmc 1.8v flag is set, force 1.8v signaling voltage\n",
76857322d54Sernest.zhang 					mmc_hostname(host->mmc));
76957322d54Sernest.zhang 				host->flags &= ~SDHCI_SIGNALING_330;
77057322d54Sernest.zhang 				host->flags |= SDHCI_SIGNALING_180;
77157322d54Sernest.zhang 				host->mmc->caps2 |= MMC_CAP2_NO_SD;
77257322d54Sernest.zhang 				host->mmc->caps2 |= MMC_CAP2_NO_SDIO;
77369d91ed1SErnest Zhang(WH) 				pci_write_config_dword(chip->pdev,
77469d91ed1SErnest Zhang(WH) 						       O2_SD_DETECT_SETTING, 3);
77557322d54Sernest.zhang 			}
77669d91ed1SErnest Zhang(WH) 
77769d91ed1SErnest Zhang(WH) 			slot->host->mmc_host_ops.get_cd = sdhci_o2_get_cd;
77857322d54Sernest.zhang 		}
77957322d54Sernest.zhang 
780cdd2b769Sshirley her 		if (chip->pdev->device == PCI_DEVICE_ID_O2_SEABIRD1) {
781cdd2b769Sshirley her 			slot->host->mmc_host_ops.get_cd = sdhci_o2_get_cd;
782cdd2b769Sshirley her 			host->mmc->caps2 |= MMC_CAP2_NO_SDIO;
783cdd2b769Sshirley her 			host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
784cdd2b769Sshirley her 		}
785cdd2b769Sshirley her 
78601acf691SAdam Lee 		if (chip->pdev->device != PCI_DEVICE_ID_O2_FUJIN2)
78701acf691SAdam Lee 			break;
78801acf691SAdam Lee 		/* set dll watch dog timer */
78901acf691SAdam Lee 		reg = sdhci_readl(host, O2_SD_VENDOR_SETTING2);
79001acf691SAdam Lee 		reg |= (1 << 12);
79101acf691SAdam Lee 		sdhci_writel(host, reg, O2_SD_VENDOR_SETTING2);
7923d757ddbSChevron Li 		break;
7933d757ddbSChevron Li 	case PCI_DEVICE_ID_O2_GG8_9860:
7943d757ddbSChevron Li 	case PCI_DEVICE_ID_O2_GG8_9861:
7953d757ddbSChevron Li 	case PCI_DEVICE_ID_O2_GG8_9862:
7963d757ddbSChevron Li 	case PCI_DEVICE_ID_O2_GG8_9863:
797da2a69cfSChevron Li 		host->mmc->caps2 |= MMC_CAP2_NO_SDIO | MMC_CAP2_SD_EXP | MMC_CAP2_SD_EXP_1_2V;
7983d757ddbSChevron Li 		host->mmc->caps |= MMC_CAP_HW_RESET;
7993d757ddbSChevron Li 		host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
8003d757ddbSChevron Li 		slot->host->mmc_host_ops.get_cd = sdhci_o2_get_cd;
801da2a69cfSChevron Li 		host->mmc_host_ops.init_sd_express = sdhci_pci_o2_init_sd_express;
80201acf691SAdam Lee 		break;
80301acf691SAdam Lee 	default:
80401acf691SAdam Lee 		break;
80501acf691SAdam Lee 	}
80601acf691SAdam Lee 
80701acf691SAdam Lee 	return 0;
80801acf691SAdam Lee }
80901acf691SAdam Lee 
sdhci_pci_o2_probe(struct sdhci_pci_chip * chip)810580b946eSZou Wei static int sdhci_pci_o2_probe(struct sdhci_pci_chip *chip)
81101acf691SAdam Lee {
81201acf691SAdam Lee 	int ret;
81301acf691SAdam Lee 	u8 scratch;
8143d757ddbSChevron Li 	u16 scratch16;
81501acf691SAdam Lee 	u32 scratch_32;
81601acf691SAdam Lee 
81701acf691SAdam Lee 	switch (chip->pdev->device) {
81801acf691SAdam Lee 	case PCI_DEVICE_ID_O2_8220:
81901acf691SAdam Lee 	case PCI_DEVICE_ID_O2_8221:
82001acf691SAdam Lee 	case PCI_DEVICE_ID_O2_8320:
82101acf691SAdam Lee 	case PCI_DEVICE_ID_O2_8321:
82201acf691SAdam Lee 		/* This extra setup is required due to broken ADMA. */
82301acf691SAdam Lee 		ret = pci_read_config_byte(chip->pdev,
82401acf691SAdam Lee 				O2_SD_LOCK_WP, &scratch);
82501acf691SAdam Lee 		if (ret)
82601acf691SAdam Lee 			return ret;
82701acf691SAdam Lee 		scratch &= 0x7f;
82801acf691SAdam Lee 		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
82901acf691SAdam Lee 
83001acf691SAdam Lee 		/* Set Multi 3 to VCC3V# */
83101acf691SAdam Lee 		pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
83201acf691SAdam Lee 
83301acf691SAdam Lee 		/* Disable CLK_REQ# support after media DET */
83401acf691SAdam Lee 		ret = pci_read_config_byte(chip->pdev,
83501acf691SAdam Lee 				O2_SD_CLKREQ, &scratch);
83601acf691SAdam Lee 		if (ret)
83701acf691SAdam Lee 			return ret;
83801acf691SAdam Lee 		scratch |= 0x20;
83901acf691SAdam Lee 		pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
84001acf691SAdam Lee 
84101acf691SAdam Lee 		/* Choose capabilities, enable SDMA.  We have to write 0x01
84201acf691SAdam Lee 		 * to the capabilities register first to unlock it.
84301acf691SAdam Lee 		 */
84401acf691SAdam Lee 		ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
84501acf691SAdam Lee 		if (ret)
84601acf691SAdam Lee 			return ret;
84701acf691SAdam Lee 		scratch |= 0x01;
84801acf691SAdam Lee 		pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
84901acf691SAdam Lee 		pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
85001acf691SAdam Lee 
85101acf691SAdam Lee 		/* Disable ADMA1/2 */
85201acf691SAdam Lee 		pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
85301acf691SAdam Lee 		pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
85401acf691SAdam Lee 
85501acf691SAdam Lee 		/* Disable the infinite transfer mode */
85601acf691SAdam Lee 		ret = pci_read_config_byte(chip->pdev,
85701acf691SAdam Lee 				O2_SD_INF_MOD, &scratch);
85801acf691SAdam Lee 		if (ret)
85901acf691SAdam Lee 			return ret;
86001acf691SAdam Lee 		scratch |= 0x08;
86101acf691SAdam Lee 		pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
86201acf691SAdam Lee 
86301acf691SAdam Lee 		/* Lock WP */
86401acf691SAdam Lee 		ret = pci_read_config_byte(chip->pdev,
86501acf691SAdam Lee 				O2_SD_LOCK_WP, &scratch);
86601acf691SAdam Lee 		if (ret)
86701acf691SAdam Lee 			return ret;
86801acf691SAdam Lee 		scratch |= 0x80;
86901acf691SAdam Lee 		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
87001acf691SAdam Lee 		break;
87101acf691SAdam Lee 	case PCI_DEVICE_ID_O2_SDS0:
87201acf691SAdam Lee 	case PCI_DEVICE_ID_O2_SDS1:
87301acf691SAdam Lee 	case PCI_DEVICE_ID_O2_FUJIN2:
87401acf691SAdam Lee 		/* UnLock WP */
87501acf691SAdam Lee 		ret = pci_read_config_byte(chip->pdev,
87601acf691SAdam Lee 				O2_SD_LOCK_WP, &scratch);
87701acf691SAdam Lee 		if (ret)
87801acf691SAdam Lee 			return ret;
87901acf691SAdam Lee 
88001acf691SAdam Lee 		scratch &= 0x7f;
88101acf691SAdam Lee 		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
88201acf691SAdam Lee 
883706adf6bSPeter Guo 		/* DevId=8520 subId= 0x11 or 0x12  Type Chip support */
884706adf6bSPeter Guo 		if (chip->pdev->device == PCI_DEVICE_ID_O2_FUJIN2) {
885706adf6bSPeter Guo 			ret = pci_read_config_dword(chip->pdev,
886706adf6bSPeter Guo 						    O2_SD_FUNC_REG0,
887706adf6bSPeter Guo 						    &scratch_32);
888d599005aSDinghao Liu 			if (ret)
889d599005aSDinghao Liu 				return ret;
890706adf6bSPeter Guo 			scratch_32 = ((scratch_32 & 0xFF000000) >> 24);
891706adf6bSPeter Guo 
892706adf6bSPeter Guo 			/* Check Whether subId is 0x11 or 0x12 */
893706adf6bSPeter Guo 			if ((scratch_32 == 0x11) || (scratch_32 == 0x12)) {
8943665ff03Sernest.zhang 				scratch_32 = 0x25100000;
895706adf6bSPeter Guo 
896706adf6bSPeter Guo 				o2_pci_set_baseclk(chip, scratch_32);
897706adf6bSPeter Guo 				ret = pci_read_config_dword(chip->pdev,
898706adf6bSPeter Guo 							    O2_SD_FUNC_REG4,
899706adf6bSPeter Guo 							    &scratch_32);
900d599005aSDinghao Liu 				if (ret)
901d599005aSDinghao Liu 					return ret;
902706adf6bSPeter Guo 
903706adf6bSPeter Guo 				/* Enable Base Clk setting change */
904706adf6bSPeter Guo 				scratch_32 |= O2_SD_FREG4_ENABLE_CLK_SET;
905706adf6bSPeter Guo 				pci_write_config_dword(chip->pdev,
906706adf6bSPeter Guo 						       O2_SD_FUNC_REG4,
907706adf6bSPeter Guo 						       scratch_32);
908706adf6bSPeter Guo 
909706adf6bSPeter Guo 				/* Set Tuning Window to 4 */
910706adf6bSPeter Guo 				pci_write_config_byte(chip->pdev,
911706adf6bSPeter Guo 						      O2_SD_TUNING_CTRL, 0x44);
912706adf6bSPeter Guo 
913706adf6bSPeter Guo 				break;
914706adf6bSPeter Guo 			}
915706adf6bSPeter Guo 		}
916706adf6bSPeter Guo 
917706adf6bSPeter Guo 		/* Enable 8520 led function */
918706adf6bSPeter Guo 		o2_pci_led_enable(chip);
919706adf6bSPeter Guo 
92001acf691SAdam Lee 		/* Set timeout CLK */
92101acf691SAdam Lee 		ret = pci_read_config_dword(chip->pdev,
92201acf691SAdam Lee 					    O2_SD_CLK_SETTING, &scratch_32);
92301acf691SAdam Lee 		if (ret)
92401acf691SAdam Lee 			return ret;
92501acf691SAdam Lee 
92601acf691SAdam Lee 		scratch_32 &= ~(0xFF00);
92701acf691SAdam Lee 		scratch_32 |= 0x07E0C800;
92801acf691SAdam Lee 		pci_write_config_dword(chip->pdev,
92901acf691SAdam Lee 				       O2_SD_CLK_SETTING, scratch_32);
93001acf691SAdam Lee 
93101acf691SAdam Lee 		ret = pci_read_config_dword(chip->pdev,
93201acf691SAdam Lee 					    O2_SD_CLKREQ, &scratch_32);
93301acf691SAdam Lee 		if (ret)
93401acf691SAdam Lee 			return ret;
93501acf691SAdam Lee 		scratch_32 |= 0x3;
93601acf691SAdam Lee 		pci_write_config_dword(chip->pdev, O2_SD_CLKREQ, scratch_32);
93701acf691SAdam Lee 
93801acf691SAdam Lee 		ret = pci_read_config_dword(chip->pdev,
93901acf691SAdam Lee 					    O2_SD_PLL_SETTING, &scratch_32);
94001acf691SAdam Lee 		if (ret)
94101acf691SAdam Lee 			return ret;
94201acf691SAdam Lee 
94301acf691SAdam Lee 		scratch_32 &= ~(0x1F3F070E);
94401acf691SAdam Lee 		scratch_32 |= 0x18270106;
94501acf691SAdam Lee 		pci_write_config_dword(chip->pdev,
94601acf691SAdam Lee 				       O2_SD_PLL_SETTING, scratch_32);
94701acf691SAdam Lee 
94801acf691SAdam Lee 		/* Disable UHS1 funciton */
94901acf691SAdam Lee 		ret = pci_read_config_dword(chip->pdev,
95001acf691SAdam Lee 					    O2_SD_CAP_REG2, &scratch_32);
95101acf691SAdam Lee 		if (ret)
95201acf691SAdam Lee 			return ret;
95301acf691SAdam Lee 		scratch_32 &= ~(0xE0);
95401acf691SAdam Lee 		pci_write_config_dword(chip->pdev,
95501acf691SAdam Lee 				       O2_SD_CAP_REG2, scratch_32);
95601acf691SAdam Lee 
95701acf691SAdam Lee 		if (chip->pdev->device == PCI_DEVICE_ID_O2_FUJIN2)
95801acf691SAdam Lee 			sdhci_pci_o2_fujin2_pci_init(chip);
95901acf691SAdam Lee 
96001acf691SAdam Lee 		/* Lock WP */
96101acf691SAdam Lee 		ret = pci_read_config_byte(chip->pdev,
96201acf691SAdam Lee 					   O2_SD_LOCK_WP, &scratch);
96301acf691SAdam Lee 		if (ret)
96401acf691SAdam Lee 			return ret;
96501acf691SAdam Lee 		scratch |= 0x80;
96601acf691SAdam Lee 		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
96701acf691SAdam Lee 		break;
96801acf691SAdam Lee 	case PCI_DEVICE_ID_O2_SEABIRD0:
96901acf691SAdam Lee 	case PCI_DEVICE_ID_O2_SEABIRD1:
97001acf691SAdam Lee 		/* UnLock WP */
97101acf691SAdam Lee 		ret = pci_read_config_byte(chip->pdev,
97201acf691SAdam Lee 				O2_SD_LOCK_WP, &scratch);
97301acf691SAdam Lee 		if (ret)
97401acf691SAdam Lee 			return ret;
97501acf691SAdam Lee 
97601acf691SAdam Lee 		scratch &= 0x7f;
97701acf691SAdam Lee 		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
97801acf691SAdam Lee 
97901acf691SAdam Lee 		ret = pci_read_config_dword(chip->pdev,
980706adf6bSPeter Guo 					    O2_SD_PLL_SETTING, &scratch_32);
981d599005aSDinghao Liu 		if (ret)
982d599005aSDinghao Liu 			return ret;
98301acf691SAdam Lee 
98401acf691SAdam Lee 		if ((scratch_32 & 0xff000000) == 0x01000000) {
98501acf691SAdam Lee 			scratch_32 &= 0x0000FFFF;
98601acf691SAdam Lee 			scratch_32 |= 0x1F340000;
98701acf691SAdam Lee 
98801acf691SAdam Lee 			pci_write_config_dword(chip->pdev,
98901acf691SAdam Lee 					       O2_SD_PLL_SETTING, scratch_32);
99001acf691SAdam Lee 		} else {
99101acf691SAdam Lee 			scratch_32 &= 0x0000FFFF;
9923665ff03Sernest.zhang 			scratch_32 |= 0x25100000;
99301acf691SAdam Lee 
99401acf691SAdam Lee 			pci_write_config_dword(chip->pdev,
99501acf691SAdam Lee 					       O2_SD_PLL_SETTING, scratch_32);
99601acf691SAdam Lee 
99701acf691SAdam Lee 			ret = pci_read_config_dword(chip->pdev,
99801acf691SAdam Lee 						    O2_SD_FUNC_REG4,
99901acf691SAdam Lee 						    &scratch_32);
1000d599005aSDinghao Liu 			if (ret)
1001d599005aSDinghao Liu 				return ret;
100201acf691SAdam Lee 			scratch_32 |= (1 << 22);
100301acf691SAdam Lee 			pci_write_config_dword(chip->pdev,
100401acf691SAdam Lee 					       O2_SD_FUNC_REG4, scratch_32);
100501acf691SAdam Lee 		}
100601acf691SAdam Lee 
1007706adf6bSPeter Guo 		/* Set Tuning Windows to 5 */
1008706adf6bSPeter Guo 		pci_write_config_byte(chip->pdev,
1009706adf6bSPeter Guo 				O2_SD_TUNING_CTRL, 0x55);
1010096cc0cdSChevron Li 		//Adjust 1st and 2nd CD debounce time
1011096cc0cdSChevron Li 		pci_read_config_dword(chip->pdev, O2_SD_MISC_CTRL2, &scratch_32);
1012096cc0cdSChevron Li 		scratch_32 &= 0xFFE7FFFF;
1013096cc0cdSChevron Li 		scratch_32 |= 0x00180000;
1014096cc0cdSChevron Li 		pci_write_config_dword(chip->pdev, O2_SD_MISC_CTRL2, scratch_32);
1015096cc0cdSChevron Li 		pci_write_config_dword(chip->pdev, O2_SD_DETECT_SETTING, 1);
101601acf691SAdam Lee 		/* Lock WP */
101701acf691SAdam Lee 		ret = pci_read_config_byte(chip->pdev,
101801acf691SAdam Lee 					   O2_SD_LOCK_WP, &scratch);
101901acf691SAdam Lee 		if (ret)
102001acf691SAdam Lee 			return ret;
102101acf691SAdam Lee 		scratch |= 0x80;
102201acf691SAdam Lee 		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
102301acf691SAdam Lee 		break;
10243d757ddbSChevron Li 	case PCI_DEVICE_ID_O2_GG8_9860:
10253d757ddbSChevron Li 	case PCI_DEVICE_ID_O2_GG8_9861:
10263d757ddbSChevron Li 	case PCI_DEVICE_ID_O2_GG8_9862:
10273d757ddbSChevron Li 	case PCI_DEVICE_ID_O2_GG8_9863:
10283d757ddbSChevron Li 		/* UnLock WP */
10293d757ddbSChevron Li 		ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
10303d757ddbSChevron Li 		if (ret)
10313d757ddbSChevron Li 			return ret;
10323d757ddbSChevron Li 		scratch &= 0x7f;
10333d757ddbSChevron Li 		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
10343d757ddbSChevron Li 
10353d757ddbSChevron Li 		/* Select mode switch source as software control */
10363d757ddbSChevron Li 		pci_read_config_word(chip->pdev, O2_SD_PARA_SET_REG1, &scratch16);
10373d757ddbSChevron Li 		scratch16 &= 0xF8FF;
10383d757ddbSChevron Li 		scratch16 |= BIT(9);
10393d757ddbSChevron Li 		pci_write_config_word(chip->pdev, O2_SD_PARA_SET_REG1, scratch16);
10403d757ddbSChevron Li 
10413d757ddbSChevron Li 		/* set VDD1 supply source */
10423d757ddbSChevron Li 		pci_read_config_word(chip->pdev, O2_SD_VDDX_CTRL_REG, &scratch16);
10433d757ddbSChevron Li 		scratch16 &= 0xFFE3;
10443d757ddbSChevron Li 		scratch16 |= BIT(3);
10453d757ddbSChevron Li 		pci_write_config_word(chip->pdev, O2_SD_VDDX_CTRL_REG, scratch16);
10463d757ddbSChevron Li 
10473d757ddbSChevron Li 		/* Set host drive strength*/
10483d757ddbSChevron Li 		scratch16 = 0x0025;
10493d757ddbSChevron Li 		pci_write_config_word(chip->pdev, O2_SD_PLL_SETTING, scratch16);
10503d757ddbSChevron Li 
10513d757ddbSChevron Li 		/* Set output delay*/
10523d757ddbSChevron Li 		pci_read_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, &scratch_32);
10533d757ddbSChevron Li 		scratch_32 &= 0xFF0FFF00;
10543d757ddbSChevron Li 		scratch_32 |= 0x00B0003B;
10553d757ddbSChevron Li 		pci_write_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, scratch_32);
10563d757ddbSChevron Li 
10573d757ddbSChevron Li 		/* Lock WP */
10583d757ddbSChevron Li 		ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
10593d757ddbSChevron Li 		if (ret)
10603d757ddbSChevron Li 			return ret;
10613d757ddbSChevron Li 		scratch |= 0x80;
10623d757ddbSChevron Li 		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
10633d757ddbSChevron Li 		break;
106401acf691SAdam Lee 	}
106501acf691SAdam Lee 
106601acf691SAdam Lee 	return 0;
106701acf691SAdam Lee }
106801acf691SAdam Lee 
1069b7813f0fSAdrian Hunter #ifdef CONFIG_PM_SLEEP
sdhci_pci_o2_resume(struct sdhci_pci_chip * chip)1070580b946eSZou Wei static int sdhci_pci_o2_resume(struct sdhci_pci_chip *chip)
107101acf691SAdam Lee {
107201acf691SAdam Lee 	sdhci_pci_o2_probe(chip);
107330cf2803SAdrian Hunter 	return sdhci_pci_resume_host(chip);
107401acf691SAdam Lee }
1075b7813f0fSAdrian Hunter #endif
1076328be8beSErnest Zhang(WH) 
107769d91ed1SErnest Zhang(WH) static const struct sdhci_ops sdhci_pci_o2_ops = {
107869d91ed1SErnest Zhang(WH) 	.set_clock = sdhci_pci_o2_set_clock,
107969d91ed1SErnest Zhang(WH) 	.enable_dma = sdhci_pci_enable_dma,
108069d91ed1SErnest Zhang(WH) 	.set_bus_width = sdhci_set_bus_width,
108169d91ed1SErnest Zhang(WH) 	.reset = sdhci_reset,
108269d91ed1SErnest Zhang(WH) 	.set_uhs_signaling = sdhci_set_uhs_signaling,
1083*4796a1a4SFred Ai 	.set_power = sdhci_pci_o2_set_power,
108469d91ed1SErnest Zhang(WH) };
108569d91ed1SErnest Zhang(WH) 
1086328be8beSErnest Zhang(WH) const struct sdhci_pci_fixes sdhci_o2 = {
1087328be8beSErnest Zhang(WH) 	.probe = sdhci_pci_o2_probe,
1088328be8beSErnest Zhang(WH) 	.quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
108949baa01cSDaniel Drake 	.quirks2 = SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD,
1090328be8beSErnest Zhang(WH) 	.probe_slot = sdhci_pci_o2_probe_slot,
1091328be8beSErnest Zhang(WH) #ifdef CONFIG_PM_SLEEP
1092328be8beSErnest Zhang(WH) 	.resume = sdhci_pci_o2_resume,
1093328be8beSErnest Zhang(WH) #endif
109469d91ed1SErnest Zhang(WH) 	.ops = &sdhci_pci_o2_ops,
10957d440617SShirley Her (SC) 	.priv_size = sizeof(struct o2_host),
1096328be8beSErnest Zhang(WH) };
1097