162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2013 BayHub Technology Ltd.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Authors: Peter Guo <peter.guo@bayhubtech.com>
662306a36Sopenharmony_ci *          Adam Lee <adam.lee@canonical.com>
762306a36Sopenharmony_ci *          Ernest Zhang <ernest.zhang@bayhubtech.com>
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/pci.h>
1162306a36Sopenharmony_ci#include <linux/mmc/host.h>
1262306a36Sopenharmony_ci#include <linux/mmc/mmc.h>
1362306a36Sopenharmony_ci#include <linux/delay.h>
1462306a36Sopenharmony_ci#include <linux/iopoll.h>
1562306a36Sopenharmony_ci#include <linux/bitfield.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include "sdhci.h"
1862306a36Sopenharmony_ci#include "sdhci-pci.h"
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci/*
2162306a36Sopenharmony_ci * O2Micro device registers
2262306a36Sopenharmony_ci */
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#define O2_SD_PCIE_SWITCH	0x54
2562306a36Sopenharmony_ci#define O2_SD_MISC_REG5		0x64
2662306a36Sopenharmony_ci#define O2_SD_LD0_CTRL		0x68
2762306a36Sopenharmony_ci#define O2_SD_DEV_CTRL		0x88
2862306a36Sopenharmony_ci#define O2_SD_LOCK_WP		0xD3
2962306a36Sopenharmony_ci#define O2_SD_TEST_REG		0xD4
3062306a36Sopenharmony_ci#define O2_SD_FUNC_REG0		0xDC
3162306a36Sopenharmony_ci#define O2_SD_MULTI_VCC3V	0xEE
3262306a36Sopenharmony_ci#define O2_SD_CLKREQ		0xEC
3362306a36Sopenharmony_ci#define O2_SD_CAPS		0xE0
3462306a36Sopenharmony_ci#define O2_SD_ADMA1		0xE2
3562306a36Sopenharmony_ci#define O2_SD_ADMA2		0xE7
3662306a36Sopenharmony_ci#define O2_SD_MISC_CTRL2	0xF0
3762306a36Sopenharmony_ci#define O2_SD_INF_MOD		0xF1
3862306a36Sopenharmony_ci#define O2_SD_MISC_CTRL4	0xFC
3962306a36Sopenharmony_ci#define O2_SD_MISC_CTRL		0x1C0
4062306a36Sopenharmony_ci#define O2_SD_EXP_INT_REG	0x1E0
4162306a36Sopenharmony_ci#define O2_SD_PWR_FORCE_L0	0x0002
4262306a36Sopenharmony_ci#define O2_SD_TUNING_CTRL	0x300
4362306a36Sopenharmony_ci#define O2_SD_PLL_SETTING	0x304
4462306a36Sopenharmony_ci#define O2_SD_MISC_SETTING	0x308
4562306a36Sopenharmony_ci#define O2_SD_CLK_SETTING	0x328
4662306a36Sopenharmony_ci#define O2_SD_CAP_REG2		0x330
4762306a36Sopenharmony_ci#define O2_SD_CAP_REG0		0x334
4862306a36Sopenharmony_ci#define O2_SD_UHS1_CAP_SETTING	0x33C
4962306a36Sopenharmony_ci#define O2_SD_DELAY_CTRL	0x350
5062306a36Sopenharmony_ci#define O2_SD_OUTPUT_CLK_SOURCE_SWITCH	0x354
5162306a36Sopenharmony_ci#define O2_SD_UHS2_L1_CTRL	0x35C
5262306a36Sopenharmony_ci#define O2_SD_FUNC_REG3		0x3E0
5362306a36Sopenharmony_ci#define O2_SD_FUNC_REG4		0x3E4
5462306a36Sopenharmony_ci#define O2_SD_PARA_SET_REG1	0x444
5562306a36Sopenharmony_ci#define O2_SD_VDDX_CTRL_REG	0x508
5662306a36Sopenharmony_ci#define O2_SD_GPIO_CTRL_REG1	0x510
5762306a36Sopenharmony_ci#define O2_SD_LED_ENABLE	BIT(6)
5862306a36Sopenharmony_ci#define O2_SD_FREG0_LEDOFF	BIT(13)
5962306a36Sopenharmony_ci#define O2_SD_SEL_DLL		BIT(16)
6062306a36Sopenharmony_ci#define O2_SD_FREG4_ENABLE_CLK_SET	BIT(22)
6162306a36Sopenharmony_ci#define O2_SD_PHASE_MASK	GENMASK(23, 20)
6262306a36Sopenharmony_ci#define O2_SD_FIX_PHASE		FIELD_PREP(O2_SD_PHASE_MASK, 0x9)
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci#define O2_SD_VENDOR_SETTING	0x110
6562306a36Sopenharmony_ci#define O2_SD_VENDOR_SETTING2	0x1C8
6662306a36Sopenharmony_ci#define O2_SD_HW_TUNING_DISABLE	BIT(4)
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#define O2_PLL_DLL_WDT_CONTROL1	0x1CC
6962306a36Sopenharmony_ci#define  O2_PLL_FORCE_ACTIVE	BIT(18)
7062306a36Sopenharmony_ci#define  O2_PLL_LOCK_STATUS	BIT(14)
7162306a36Sopenharmony_ci#define  O2_PLL_SOFT_RESET	BIT(12)
7262306a36Sopenharmony_ci#define  O2_DLL_LOCK_STATUS	BIT(11)
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci#define O2_SD_DETECT_SETTING 0x324
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_cistatic const u32 dmdn_table[] = {0x2B1C0000,
7762306a36Sopenharmony_ci	0x2C1A0000, 0x371B0000, 0x35100000};
7862306a36Sopenharmony_ci#define DMDN_SZ ARRAY_SIZE(dmdn_table)
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_cistruct o2_host {
8162306a36Sopenharmony_ci	u8 dll_adjust_count;
8262306a36Sopenharmony_ci};
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_cistatic void sdhci_o2_wait_card_detect_stable(struct sdhci_host *host)
8562306a36Sopenharmony_ci{
8662306a36Sopenharmony_ci	ktime_t timeout;
8762306a36Sopenharmony_ci	u32 scratch32;
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci	/* Wait max 50 ms */
9062306a36Sopenharmony_ci	timeout = ktime_add_ms(ktime_get(), 50);
9162306a36Sopenharmony_ci	while (1) {
9262306a36Sopenharmony_ci		bool timedout = ktime_after(ktime_get(), timeout);
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci		scratch32 = sdhci_readl(host, SDHCI_PRESENT_STATE);
9562306a36Sopenharmony_ci		if ((scratch32 & SDHCI_CARD_PRESENT) >> SDHCI_CARD_PRES_SHIFT
9662306a36Sopenharmony_ci		    == (scratch32 & SDHCI_CD_LVL) >> SDHCI_CD_LVL_SHIFT)
9762306a36Sopenharmony_ci			break;
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci		if (timedout) {
10062306a36Sopenharmony_ci			pr_err("%s: Card Detect debounce never finished.\n",
10162306a36Sopenharmony_ci			       mmc_hostname(host->mmc));
10262306a36Sopenharmony_ci			sdhci_dumpregs(host);
10362306a36Sopenharmony_ci			return;
10462306a36Sopenharmony_ci		}
10562306a36Sopenharmony_ci		udelay(10);
10662306a36Sopenharmony_ci	}
10762306a36Sopenharmony_ci}
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_cistatic void sdhci_o2_enable_internal_clock(struct sdhci_host *host)
11062306a36Sopenharmony_ci{
11162306a36Sopenharmony_ci	ktime_t timeout;
11262306a36Sopenharmony_ci	u16 scratch;
11362306a36Sopenharmony_ci	u32 scratch32;
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	/* PLL software reset */
11662306a36Sopenharmony_ci	scratch32 = sdhci_readl(host, O2_PLL_DLL_WDT_CONTROL1);
11762306a36Sopenharmony_ci	scratch32 |= O2_PLL_SOFT_RESET;
11862306a36Sopenharmony_ci	sdhci_writel(host, scratch32, O2_PLL_DLL_WDT_CONTROL1);
11962306a36Sopenharmony_ci	udelay(1);
12062306a36Sopenharmony_ci	scratch32 &= ~(O2_PLL_SOFT_RESET);
12162306a36Sopenharmony_ci	sdhci_writel(host, scratch32, O2_PLL_DLL_WDT_CONTROL1);
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	/* PLL force active */
12462306a36Sopenharmony_ci	scratch32 |= O2_PLL_FORCE_ACTIVE;
12562306a36Sopenharmony_ci	sdhci_writel(host, scratch32, O2_PLL_DLL_WDT_CONTROL1);
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci	/* Wait max 20 ms */
12862306a36Sopenharmony_ci	timeout = ktime_add_ms(ktime_get(), 20);
12962306a36Sopenharmony_ci	while (1) {
13062306a36Sopenharmony_ci		bool timedout = ktime_after(ktime_get(), timeout);
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci		scratch = sdhci_readw(host, O2_PLL_DLL_WDT_CONTROL1);
13362306a36Sopenharmony_ci		if (scratch & O2_PLL_LOCK_STATUS)
13462306a36Sopenharmony_ci			break;
13562306a36Sopenharmony_ci		if (timedout) {
13662306a36Sopenharmony_ci			pr_err("%s: Internal clock never stabilised.\n",
13762306a36Sopenharmony_ci			       mmc_hostname(host->mmc));
13862306a36Sopenharmony_ci			sdhci_dumpregs(host);
13962306a36Sopenharmony_ci			goto out;
14062306a36Sopenharmony_ci		}
14162306a36Sopenharmony_ci		udelay(10);
14262306a36Sopenharmony_ci	}
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	/* Wait for card detect finish */
14562306a36Sopenharmony_ci	udelay(1);
14662306a36Sopenharmony_ci	sdhci_o2_wait_card_detect_stable(host);
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ciout:
14962306a36Sopenharmony_ci	/* Cancel PLL force active */
15062306a36Sopenharmony_ci	scratch32 = sdhci_readl(host, O2_PLL_DLL_WDT_CONTROL1);
15162306a36Sopenharmony_ci	scratch32 &= ~O2_PLL_FORCE_ACTIVE;
15262306a36Sopenharmony_ci	sdhci_writel(host, scratch32, O2_PLL_DLL_WDT_CONTROL1);
15362306a36Sopenharmony_ci}
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_cistatic int sdhci_o2_get_cd(struct mmc_host *mmc)
15662306a36Sopenharmony_ci{
15762306a36Sopenharmony_ci	struct sdhci_host *host = mmc_priv(mmc);
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	if (!(sdhci_readw(host, O2_PLL_DLL_WDT_CONTROL1) & O2_PLL_LOCK_STATUS))
16062306a36Sopenharmony_ci		sdhci_o2_enable_internal_clock(host);
16162306a36Sopenharmony_ci	else
16262306a36Sopenharmony_ci		sdhci_o2_wait_card_detect_stable(host);
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
16562306a36Sopenharmony_ci}
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_cistatic void o2_pci_set_baseclk(struct sdhci_pci_chip *chip, u32 value)
16862306a36Sopenharmony_ci{
16962306a36Sopenharmony_ci	u32 scratch_32;
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci	pci_read_config_dword(chip->pdev,
17262306a36Sopenharmony_ci			      O2_SD_PLL_SETTING, &scratch_32);
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	scratch_32 &= 0x0000FFFF;
17562306a36Sopenharmony_ci	scratch_32 |= value;
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	pci_write_config_dword(chip->pdev,
17862306a36Sopenharmony_ci			       O2_SD_PLL_SETTING, scratch_32);
17962306a36Sopenharmony_ci}
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_cistatic u32 sdhci_o2_pll_dll_wdt_control(struct sdhci_host *host)
18262306a36Sopenharmony_ci{
18362306a36Sopenharmony_ci	return sdhci_readl(host, O2_PLL_DLL_WDT_CONTROL1);
18462306a36Sopenharmony_ci}
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci/*
18762306a36Sopenharmony_ci * This function is used to detect dll lock status.
18862306a36Sopenharmony_ci * Since the dll lock status bit will toggle randomly
18962306a36Sopenharmony_ci * with very short interval which needs to be polled
19062306a36Sopenharmony_ci * as fast as possible. Set sleep_us as 1 microsecond.
19162306a36Sopenharmony_ci */
19262306a36Sopenharmony_cistatic int sdhci_o2_wait_dll_detect_lock(struct sdhci_host *host)
19362306a36Sopenharmony_ci{
19462306a36Sopenharmony_ci	u32	scratch32 = 0;
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci	return readx_poll_timeout(sdhci_o2_pll_dll_wdt_control, host,
19762306a36Sopenharmony_ci		scratch32, !(scratch32 & O2_DLL_LOCK_STATUS), 1, 1000000);
19862306a36Sopenharmony_ci}
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_cistatic void sdhci_o2_set_tuning_mode(struct sdhci_host *host)
20162306a36Sopenharmony_ci{
20262306a36Sopenharmony_ci	u16 reg;
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci	/* enable hardware tuning */
20562306a36Sopenharmony_ci	reg = sdhci_readw(host, O2_SD_VENDOR_SETTING);
20662306a36Sopenharmony_ci	reg &= ~O2_SD_HW_TUNING_DISABLE;
20762306a36Sopenharmony_ci	sdhci_writew(host, reg, O2_SD_VENDOR_SETTING);
20862306a36Sopenharmony_ci}
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_cistatic void __sdhci_o2_execute_tuning(struct sdhci_host *host, u32 opcode)
21162306a36Sopenharmony_ci{
21262306a36Sopenharmony_ci	int i;
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci	sdhci_send_tuning(host, opcode);
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci	for (i = 0; i < 150; i++) {
21762306a36Sopenharmony_ci		u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci		if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
22062306a36Sopenharmony_ci			if (ctrl & SDHCI_CTRL_TUNED_CLK) {
22162306a36Sopenharmony_ci				host->tuning_done = true;
22262306a36Sopenharmony_ci				return;
22362306a36Sopenharmony_ci			}
22462306a36Sopenharmony_ci			pr_warn("%s: HW tuning failed !\n",
22562306a36Sopenharmony_ci				mmc_hostname(host->mmc));
22662306a36Sopenharmony_ci			break;
22762306a36Sopenharmony_ci		}
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci		mdelay(1);
23062306a36Sopenharmony_ci	}
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ci	pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
23362306a36Sopenharmony_ci		mmc_hostname(host->mmc));
23462306a36Sopenharmony_ci	sdhci_reset_tuning(host);
23562306a36Sopenharmony_ci}
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci/*
23862306a36Sopenharmony_ci * This function is used to fix o2 dll shift issue.
23962306a36Sopenharmony_ci * It isn't necessary to detect card present before recovery.
24062306a36Sopenharmony_ci * Firstly, it is used by bht emmc card, which is embedded.
24162306a36Sopenharmony_ci * Second, before call recovery card present will be detected
24262306a36Sopenharmony_ci * outside of the execute tuning function.
24362306a36Sopenharmony_ci */
24462306a36Sopenharmony_cistatic int sdhci_o2_dll_recovery(struct sdhci_host *host)
24562306a36Sopenharmony_ci{
24662306a36Sopenharmony_ci	int ret = 0;
24762306a36Sopenharmony_ci	u8 scratch_8 = 0;
24862306a36Sopenharmony_ci	u32 scratch_32 = 0;
24962306a36Sopenharmony_ci	struct sdhci_pci_slot *slot = sdhci_priv(host);
25062306a36Sopenharmony_ci	struct sdhci_pci_chip *chip = slot->chip;
25162306a36Sopenharmony_ci	struct o2_host *o2_host = sdhci_pci_priv(slot);
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci	/* UnLock WP */
25462306a36Sopenharmony_ci	pci_read_config_byte(chip->pdev,
25562306a36Sopenharmony_ci			O2_SD_LOCK_WP, &scratch_8);
25662306a36Sopenharmony_ci	scratch_8 &= 0x7f;
25762306a36Sopenharmony_ci	pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
25862306a36Sopenharmony_ci	while (o2_host->dll_adjust_count < DMDN_SZ && !ret) {
25962306a36Sopenharmony_ci		/* Disable clock */
26062306a36Sopenharmony_ci		sdhci_writeb(host, 0, SDHCI_CLOCK_CONTROL);
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci		/* PLL software reset */
26362306a36Sopenharmony_ci		scratch_32 = sdhci_readl(host, O2_PLL_DLL_WDT_CONTROL1);
26462306a36Sopenharmony_ci		scratch_32 |= O2_PLL_SOFT_RESET;
26562306a36Sopenharmony_ci		sdhci_writel(host, scratch_32, O2_PLL_DLL_WDT_CONTROL1);
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci		pci_read_config_dword(chip->pdev,
26862306a36Sopenharmony_ci					    O2_SD_FUNC_REG4,
26962306a36Sopenharmony_ci					    &scratch_32);
27062306a36Sopenharmony_ci		/* Enable Base Clk setting change */
27162306a36Sopenharmony_ci		scratch_32 |= O2_SD_FREG4_ENABLE_CLK_SET;
27262306a36Sopenharmony_ci		pci_write_config_dword(chip->pdev, O2_SD_FUNC_REG4, scratch_32);
27362306a36Sopenharmony_ci		o2_pci_set_baseclk(chip, dmdn_table[o2_host->dll_adjust_count]);
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci		/* Enable internal clock */
27662306a36Sopenharmony_ci		scratch_8 = SDHCI_CLOCK_INT_EN;
27762306a36Sopenharmony_ci		sdhci_writeb(host, scratch_8, SDHCI_CLOCK_CONTROL);
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci		if (sdhci_o2_get_cd(host->mmc)) {
28062306a36Sopenharmony_ci			/*
28162306a36Sopenharmony_ci			 * need wait at least 5ms for dll status stable,
28262306a36Sopenharmony_ci			 * after enable internal clock
28362306a36Sopenharmony_ci			 */
28462306a36Sopenharmony_ci			usleep_range(5000, 6000);
28562306a36Sopenharmony_ci			if (sdhci_o2_wait_dll_detect_lock(host)) {
28662306a36Sopenharmony_ci				scratch_8 |= SDHCI_CLOCK_CARD_EN;
28762306a36Sopenharmony_ci				sdhci_writeb(host, scratch_8,
28862306a36Sopenharmony_ci					SDHCI_CLOCK_CONTROL);
28962306a36Sopenharmony_ci				ret = 1;
29062306a36Sopenharmony_ci			} else {
29162306a36Sopenharmony_ci				pr_warn("%s: DLL unlocked when dll_adjust_count is %d.\n",
29262306a36Sopenharmony_ci					mmc_hostname(host->mmc),
29362306a36Sopenharmony_ci					o2_host->dll_adjust_count);
29462306a36Sopenharmony_ci			}
29562306a36Sopenharmony_ci		} else {
29662306a36Sopenharmony_ci			pr_err("%s: card present detect failed.\n",
29762306a36Sopenharmony_ci				mmc_hostname(host->mmc));
29862306a36Sopenharmony_ci			break;
29962306a36Sopenharmony_ci		}
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci		o2_host->dll_adjust_count++;
30262306a36Sopenharmony_ci	}
30362306a36Sopenharmony_ci	if (!ret && o2_host->dll_adjust_count == DMDN_SZ)
30462306a36Sopenharmony_ci		pr_err("%s: DLL adjust over max times\n",
30562306a36Sopenharmony_ci		mmc_hostname(host->mmc));
30662306a36Sopenharmony_ci	/* Lock WP */
30762306a36Sopenharmony_ci	pci_read_config_byte(chip->pdev,
30862306a36Sopenharmony_ci				   O2_SD_LOCK_WP, &scratch_8);
30962306a36Sopenharmony_ci	scratch_8 |= 0x80;
31062306a36Sopenharmony_ci	pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
31162306a36Sopenharmony_ci	return ret;
31262306a36Sopenharmony_ci}
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_cistatic int sdhci_o2_execute_tuning(struct mmc_host *mmc, u32 opcode)
31562306a36Sopenharmony_ci{
31662306a36Sopenharmony_ci	struct sdhci_host *host = mmc_priv(mmc);
31762306a36Sopenharmony_ci	struct sdhci_pci_slot *slot = sdhci_priv(host);
31862306a36Sopenharmony_ci	struct sdhci_pci_chip *chip = slot->chip;
31962306a36Sopenharmony_ci	int current_bus_width = 0;
32062306a36Sopenharmony_ci	u32 scratch32 = 0;
32162306a36Sopenharmony_ci	u16 scratch = 0;
32262306a36Sopenharmony_ci	u8  scratch_8 = 0;
32362306a36Sopenharmony_ci	u32 reg_val;
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci	/*
32662306a36Sopenharmony_ci	 * This handler implements the hardware tuning that is specific to
32762306a36Sopenharmony_ci	 * this controller.  Fall back to the standard method for other TIMING.
32862306a36Sopenharmony_ci	 */
32962306a36Sopenharmony_ci	if ((host->timing != MMC_TIMING_MMC_HS200) &&
33062306a36Sopenharmony_ci		(host->timing != MMC_TIMING_UHS_SDR104) &&
33162306a36Sopenharmony_ci		(host->timing != MMC_TIMING_UHS_SDR50))
33262306a36Sopenharmony_ci		return sdhci_execute_tuning(mmc, opcode);
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci	if (WARN_ON(!mmc_op_tuning(opcode)))
33562306a36Sopenharmony_ci		return -EINVAL;
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci	/* Force power mode enter L0 */
33862306a36Sopenharmony_ci	scratch = sdhci_readw(host, O2_SD_MISC_CTRL);
33962306a36Sopenharmony_ci	scratch |= O2_SD_PWR_FORCE_L0;
34062306a36Sopenharmony_ci	sdhci_writew(host, scratch, O2_SD_MISC_CTRL);
34162306a36Sopenharmony_ci
34262306a36Sopenharmony_ci	/* Update output phase */
34362306a36Sopenharmony_ci	switch (chip->pdev->device) {
34462306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_SDS0:
34562306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_SEABIRD0:
34662306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_SEABIRD1:
34762306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_SDS1:
34862306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_FUJIN2:
34962306a36Sopenharmony_ci		/* Stop clk */
35062306a36Sopenharmony_ci		reg_val = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
35162306a36Sopenharmony_ci		reg_val &= ~SDHCI_CLOCK_CARD_EN;
35262306a36Sopenharmony_ci		sdhci_writew(host, reg_val, SDHCI_CLOCK_CONTROL);
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_ci		if (host->timing == MMC_TIMING_MMC_HS200 ||
35562306a36Sopenharmony_ci		    host->timing == MMC_TIMING_UHS_SDR104) {
35662306a36Sopenharmony_ci			/* UnLock WP */
35762306a36Sopenharmony_ci			pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch_8);
35862306a36Sopenharmony_ci			scratch_8 &= 0x7f;
35962306a36Sopenharmony_ci			pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_ci			/* Set pcr 0x354[16] to choose dll clock, and set the default phase */
36262306a36Sopenharmony_ci			pci_read_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, &reg_val);
36362306a36Sopenharmony_ci			reg_val &= ~(O2_SD_SEL_DLL | O2_SD_PHASE_MASK);
36462306a36Sopenharmony_ci			reg_val |= (O2_SD_SEL_DLL | O2_SD_FIX_PHASE);
36562306a36Sopenharmony_ci			pci_write_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, reg_val);
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci			/* Lock WP */
36862306a36Sopenharmony_ci			pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch_8);
36962306a36Sopenharmony_ci			scratch_8 |= 0x80;
37062306a36Sopenharmony_ci			pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
37162306a36Sopenharmony_ci		}
37262306a36Sopenharmony_ci
37362306a36Sopenharmony_ci		/* Start clk */
37462306a36Sopenharmony_ci		reg_val = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
37562306a36Sopenharmony_ci		reg_val |= SDHCI_CLOCK_CARD_EN;
37662306a36Sopenharmony_ci		sdhci_writew(host, reg_val, SDHCI_CLOCK_CONTROL);
37762306a36Sopenharmony_ci		break;
37862306a36Sopenharmony_ci	default:
37962306a36Sopenharmony_ci		break;
38062306a36Sopenharmony_ci	}
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci	/* wait DLL lock, timeout value 5ms */
38362306a36Sopenharmony_ci	if (readx_poll_timeout(sdhci_o2_pll_dll_wdt_control, host,
38462306a36Sopenharmony_ci		scratch32, (scratch32 & O2_DLL_LOCK_STATUS), 1, 5000))
38562306a36Sopenharmony_ci		pr_warn("%s: DLL can't lock in 5ms after force L0 during tuning.\n",
38662306a36Sopenharmony_ci				mmc_hostname(host->mmc));
38762306a36Sopenharmony_ci	/*
38862306a36Sopenharmony_ci	 * Judge the tuning reason, whether caused by dll shift
38962306a36Sopenharmony_ci	 * If cause by dll shift, should call sdhci_o2_dll_recovery
39062306a36Sopenharmony_ci	 */
39162306a36Sopenharmony_ci	if (!sdhci_o2_wait_dll_detect_lock(host))
39262306a36Sopenharmony_ci		if (!sdhci_o2_dll_recovery(host)) {
39362306a36Sopenharmony_ci			pr_err("%s: o2 dll recovery failed\n",
39462306a36Sopenharmony_ci				mmc_hostname(host->mmc));
39562306a36Sopenharmony_ci			return -EINVAL;
39662306a36Sopenharmony_ci		}
39762306a36Sopenharmony_ci	/*
39862306a36Sopenharmony_ci	 * o2 sdhci host didn't support 8bit emmc tuning
39962306a36Sopenharmony_ci	 */
40062306a36Sopenharmony_ci	if (mmc->ios.bus_width == MMC_BUS_WIDTH_8) {
40162306a36Sopenharmony_ci		current_bus_width = mmc->ios.bus_width;
40262306a36Sopenharmony_ci		mmc->ios.bus_width = MMC_BUS_WIDTH_4;
40362306a36Sopenharmony_ci		sdhci_set_bus_width(host, MMC_BUS_WIDTH_4);
40462306a36Sopenharmony_ci	}
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_ci	sdhci_o2_set_tuning_mode(host);
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ci	sdhci_start_tuning(host);
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ci	__sdhci_o2_execute_tuning(host, opcode);
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ci	sdhci_end_tuning(host);
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ci	if (current_bus_width == MMC_BUS_WIDTH_8) {
41562306a36Sopenharmony_ci		mmc->ios.bus_width = MMC_BUS_WIDTH_8;
41662306a36Sopenharmony_ci		sdhci_set_bus_width(host, current_bus_width);
41762306a36Sopenharmony_ci	}
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_ci	/* Cancel force power mode enter L0 */
42062306a36Sopenharmony_ci	scratch = sdhci_readw(host, O2_SD_MISC_CTRL);
42162306a36Sopenharmony_ci	scratch &= ~(O2_SD_PWR_FORCE_L0);
42262306a36Sopenharmony_ci	sdhci_writew(host, scratch, O2_SD_MISC_CTRL);
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_ci	sdhci_reset(host, SDHCI_RESET_CMD);
42562306a36Sopenharmony_ci	sdhci_reset(host, SDHCI_RESET_DATA);
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ci	host->flags &= ~SDHCI_HS400_TUNING;
42862306a36Sopenharmony_ci	return 0;
42962306a36Sopenharmony_ci}
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_cistatic void o2_pci_led_enable(struct sdhci_pci_chip *chip)
43262306a36Sopenharmony_ci{
43362306a36Sopenharmony_ci	int ret;
43462306a36Sopenharmony_ci	u32 scratch_32;
43562306a36Sopenharmony_ci
43662306a36Sopenharmony_ci	/* Set led of SD host function enable */
43762306a36Sopenharmony_ci	ret = pci_read_config_dword(chip->pdev,
43862306a36Sopenharmony_ci				    O2_SD_FUNC_REG0, &scratch_32);
43962306a36Sopenharmony_ci	if (ret)
44062306a36Sopenharmony_ci		return;
44162306a36Sopenharmony_ci
44262306a36Sopenharmony_ci	scratch_32 &= ~O2_SD_FREG0_LEDOFF;
44362306a36Sopenharmony_ci	pci_write_config_dword(chip->pdev,
44462306a36Sopenharmony_ci			       O2_SD_FUNC_REG0, scratch_32);
44562306a36Sopenharmony_ci
44662306a36Sopenharmony_ci	ret = pci_read_config_dword(chip->pdev,
44762306a36Sopenharmony_ci				    O2_SD_TEST_REG, &scratch_32);
44862306a36Sopenharmony_ci	if (ret)
44962306a36Sopenharmony_ci		return;
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_ci	scratch_32 |= O2_SD_LED_ENABLE;
45262306a36Sopenharmony_ci	pci_write_config_dword(chip->pdev,
45362306a36Sopenharmony_ci			       O2_SD_TEST_REG, scratch_32);
45462306a36Sopenharmony_ci}
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_cistatic void sdhci_pci_o2_fujin2_pci_init(struct sdhci_pci_chip *chip)
45762306a36Sopenharmony_ci{
45862306a36Sopenharmony_ci	u32 scratch_32;
45962306a36Sopenharmony_ci	int ret;
46062306a36Sopenharmony_ci	/* Improve write performance for SD3.0 */
46162306a36Sopenharmony_ci	ret = pci_read_config_dword(chip->pdev, O2_SD_DEV_CTRL, &scratch_32);
46262306a36Sopenharmony_ci	if (ret)
46362306a36Sopenharmony_ci		return;
46462306a36Sopenharmony_ci	scratch_32 &= ~((1 << 12) | (1 << 13) | (1 << 14));
46562306a36Sopenharmony_ci	pci_write_config_dword(chip->pdev, O2_SD_DEV_CTRL, scratch_32);
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci	/* Enable Link abnormal reset generating Reset */
46862306a36Sopenharmony_ci	ret = pci_read_config_dword(chip->pdev, O2_SD_MISC_REG5, &scratch_32);
46962306a36Sopenharmony_ci	if (ret)
47062306a36Sopenharmony_ci		return;
47162306a36Sopenharmony_ci	scratch_32 &= ~((1 << 19) | (1 << 11));
47262306a36Sopenharmony_ci	scratch_32 |= (1 << 10);
47362306a36Sopenharmony_ci	pci_write_config_dword(chip->pdev, O2_SD_MISC_REG5, scratch_32);
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci	/* set card power over current protection */
47662306a36Sopenharmony_ci	ret = pci_read_config_dword(chip->pdev, O2_SD_TEST_REG, &scratch_32);
47762306a36Sopenharmony_ci	if (ret)
47862306a36Sopenharmony_ci		return;
47962306a36Sopenharmony_ci	scratch_32 |= (1 << 4);
48062306a36Sopenharmony_ci	pci_write_config_dword(chip->pdev, O2_SD_TEST_REG, scratch_32);
48162306a36Sopenharmony_ci
48262306a36Sopenharmony_ci	/* adjust the output delay for SD mode */
48362306a36Sopenharmony_ci	pci_write_config_dword(chip->pdev, O2_SD_DELAY_CTRL, 0x00002492);
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_ci	/* Set the output voltage setting of Aux 1.2v LDO */
48662306a36Sopenharmony_ci	ret = pci_read_config_dword(chip->pdev, O2_SD_LD0_CTRL, &scratch_32);
48762306a36Sopenharmony_ci	if (ret)
48862306a36Sopenharmony_ci		return;
48962306a36Sopenharmony_ci	scratch_32 &= ~(3 << 12);
49062306a36Sopenharmony_ci	pci_write_config_dword(chip->pdev, O2_SD_LD0_CTRL, scratch_32);
49162306a36Sopenharmony_ci
49262306a36Sopenharmony_ci	/* Set Max power supply capability of SD host */
49362306a36Sopenharmony_ci	ret = pci_read_config_dword(chip->pdev, O2_SD_CAP_REG0, &scratch_32);
49462306a36Sopenharmony_ci	if (ret)
49562306a36Sopenharmony_ci		return;
49662306a36Sopenharmony_ci	scratch_32 &= ~(0x01FE);
49762306a36Sopenharmony_ci	scratch_32 |= 0x00CC;
49862306a36Sopenharmony_ci	pci_write_config_dword(chip->pdev, O2_SD_CAP_REG0, scratch_32);
49962306a36Sopenharmony_ci	/* Set DLL Tuning Window */
50062306a36Sopenharmony_ci	ret = pci_read_config_dword(chip->pdev,
50162306a36Sopenharmony_ci				    O2_SD_TUNING_CTRL, &scratch_32);
50262306a36Sopenharmony_ci	if (ret)
50362306a36Sopenharmony_ci		return;
50462306a36Sopenharmony_ci	scratch_32 &= ~(0x000000FF);
50562306a36Sopenharmony_ci	scratch_32 |= 0x00000066;
50662306a36Sopenharmony_ci	pci_write_config_dword(chip->pdev, O2_SD_TUNING_CTRL, scratch_32);
50762306a36Sopenharmony_ci
50862306a36Sopenharmony_ci	/* Set UHS2 T_EIDLE */
50962306a36Sopenharmony_ci	ret = pci_read_config_dword(chip->pdev,
51062306a36Sopenharmony_ci				    O2_SD_UHS2_L1_CTRL, &scratch_32);
51162306a36Sopenharmony_ci	if (ret)
51262306a36Sopenharmony_ci		return;
51362306a36Sopenharmony_ci	scratch_32 &= ~(0x000000FC);
51462306a36Sopenharmony_ci	scratch_32 |= 0x00000084;
51562306a36Sopenharmony_ci	pci_write_config_dword(chip->pdev, O2_SD_UHS2_L1_CTRL, scratch_32);
51662306a36Sopenharmony_ci
51762306a36Sopenharmony_ci	/* Set UHS2 Termination */
51862306a36Sopenharmony_ci	ret = pci_read_config_dword(chip->pdev, O2_SD_FUNC_REG3, &scratch_32);
51962306a36Sopenharmony_ci	if (ret)
52062306a36Sopenharmony_ci		return;
52162306a36Sopenharmony_ci	scratch_32 &= ~((1 << 21) | (1 << 30));
52262306a36Sopenharmony_ci
52362306a36Sopenharmony_ci	pci_write_config_dword(chip->pdev, O2_SD_FUNC_REG3, scratch_32);
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci	/* Set L1 Entrance Timer */
52662306a36Sopenharmony_ci	ret = pci_read_config_dword(chip->pdev, O2_SD_CAPS, &scratch_32);
52762306a36Sopenharmony_ci	if (ret)
52862306a36Sopenharmony_ci		return;
52962306a36Sopenharmony_ci	scratch_32 &= ~(0xf0000000);
53062306a36Sopenharmony_ci	scratch_32 |= 0x30000000;
53162306a36Sopenharmony_ci	pci_write_config_dword(chip->pdev, O2_SD_CAPS, scratch_32);
53262306a36Sopenharmony_ci
53362306a36Sopenharmony_ci	ret = pci_read_config_dword(chip->pdev,
53462306a36Sopenharmony_ci				    O2_SD_MISC_CTRL4, &scratch_32);
53562306a36Sopenharmony_ci	if (ret)
53662306a36Sopenharmony_ci		return;
53762306a36Sopenharmony_ci	scratch_32 &= ~(0x000f0000);
53862306a36Sopenharmony_ci	scratch_32 |= 0x00080000;
53962306a36Sopenharmony_ci	pci_write_config_dword(chip->pdev, O2_SD_MISC_CTRL4, scratch_32);
54062306a36Sopenharmony_ci}
54162306a36Sopenharmony_ci
54262306a36Sopenharmony_cistatic void sdhci_pci_o2_enable_msi(struct sdhci_pci_chip *chip,
54362306a36Sopenharmony_ci				    struct sdhci_host *host)
54462306a36Sopenharmony_ci{
54562306a36Sopenharmony_ci	int ret;
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_ci	ret = pci_find_capability(chip->pdev, PCI_CAP_ID_MSI);
54862306a36Sopenharmony_ci	if (!ret) {
54962306a36Sopenharmony_ci		pr_info("%s: unsupported MSI, use INTx irq\n",
55062306a36Sopenharmony_ci			mmc_hostname(host->mmc));
55162306a36Sopenharmony_ci		return;
55262306a36Sopenharmony_ci	}
55362306a36Sopenharmony_ci
55462306a36Sopenharmony_ci	ret = pci_alloc_irq_vectors(chip->pdev, 1, 1,
55562306a36Sopenharmony_ci				    PCI_IRQ_MSI | PCI_IRQ_MSIX);
55662306a36Sopenharmony_ci	if (ret < 0) {
55762306a36Sopenharmony_ci		pr_err("%s: enable PCI MSI failed, err=%d\n",
55862306a36Sopenharmony_ci		       mmc_hostname(host->mmc), ret);
55962306a36Sopenharmony_ci		return;
56062306a36Sopenharmony_ci	}
56162306a36Sopenharmony_ci
56262306a36Sopenharmony_ci	host->irq = pci_irq_vector(chip->pdev, 0);
56362306a36Sopenharmony_ci}
56462306a36Sopenharmony_ci
56562306a36Sopenharmony_cistatic void sdhci_o2_enable_clk(struct sdhci_host *host, u16 clk)
56662306a36Sopenharmony_ci{
56762306a36Sopenharmony_ci	/* Enable internal clock */
56862306a36Sopenharmony_ci	clk |= SDHCI_CLOCK_INT_EN;
56962306a36Sopenharmony_ci	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
57062306a36Sopenharmony_ci
57162306a36Sopenharmony_ci	sdhci_o2_enable_internal_clock(host);
57262306a36Sopenharmony_ci	if (sdhci_o2_get_cd(host->mmc)) {
57362306a36Sopenharmony_ci		clk |= SDHCI_CLOCK_CARD_EN;
57462306a36Sopenharmony_ci		sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
57562306a36Sopenharmony_ci	}
57662306a36Sopenharmony_ci}
57762306a36Sopenharmony_ci
57862306a36Sopenharmony_cistatic void sdhci_pci_o2_set_clock(struct sdhci_host *host, unsigned int clock)
57962306a36Sopenharmony_ci{
58062306a36Sopenharmony_ci	u16 clk;
58162306a36Sopenharmony_ci	u8 scratch;
58262306a36Sopenharmony_ci	u32 scratch_32;
58362306a36Sopenharmony_ci	u32 dmdn_208m, dmdn_200m;
58462306a36Sopenharmony_ci	struct sdhci_pci_slot *slot = sdhci_priv(host);
58562306a36Sopenharmony_ci	struct sdhci_pci_chip *chip = slot->chip;
58662306a36Sopenharmony_ci
58762306a36Sopenharmony_ci	host->mmc->actual_clock = 0;
58862306a36Sopenharmony_ci
58962306a36Sopenharmony_ci	sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_ci	if (clock == 0)
59262306a36Sopenharmony_ci		return;
59362306a36Sopenharmony_ci
59462306a36Sopenharmony_ci	/* UnLock WP */
59562306a36Sopenharmony_ci	pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
59662306a36Sopenharmony_ci	scratch &= 0x7f;
59762306a36Sopenharmony_ci	pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
59862306a36Sopenharmony_ci
59962306a36Sopenharmony_ci	if (chip->pdev->device == PCI_DEVICE_ID_O2_GG8_9860 ||
60062306a36Sopenharmony_ci	    chip->pdev->device == PCI_DEVICE_ID_O2_GG8_9861 ||
60162306a36Sopenharmony_ci	    chip->pdev->device == PCI_DEVICE_ID_O2_GG8_9862 ||
60262306a36Sopenharmony_ci	    chip->pdev->device == PCI_DEVICE_ID_O2_GG8_9863) {
60362306a36Sopenharmony_ci		dmdn_208m = 0x2c500000;
60462306a36Sopenharmony_ci		dmdn_200m = 0x25200000;
60562306a36Sopenharmony_ci	} else {
60662306a36Sopenharmony_ci		dmdn_208m = 0x2c280000;
60762306a36Sopenharmony_ci		dmdn_200m = 0x25100000;
60862306a36Sopenharmony_ci	}
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_ci	if ((host->timing == MMC_TIMING_UHS_SDR104) && (clock == 200000000)) {
61162306a36Sopenharmony_ci		pci_read_config_dword(chip->pdev, O2_SD_PLL_SETTING, &scratch_32);
61262306a36Sopenharmony_ci
61362306a36Sopenharmony_ci		if ((scratch_32 & 0xFFFF0000) != dmdn_208m)
61462306a36Sopenharmony_ci			o2_pci_set_baseclk(chip, dmdn_208m);
61562306a36Sopenharmony_ci	} else {
61662306a36Sopenharmony_ci		pci_read_config_dword(chip->pdev, O2_SD_PLL_SETTING, &scratch_32);
61762306a36Sopenharmony_ci
61862306a36Sopenharmony_ci		if ((scratch_32 & 0xFFFF0000) != dmdn_200m)
61962306a36Sopenharmony_ci			o2_pci_set_baseclk(chip, dmdn_200m);
62062306a36Sopenharmony_ci	}
62162306a36Sopenharmony_ci
62262306a36Sopenharmony_ci	pci_read_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, &scratch_32);
62362306a36Sopenharmony_ci	scratch_32 &= ~(O2_SD_SEL_DLL | O2_SD_PHASE_MASK);
62462306a36Sopenharmony_ci	pci_write_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, scratch_32);
62562306a36Sopenharmony_ci
62662306a36Sopenharmony_ci	/* Lock WP */
62762306a36Sopenharmony_ci	pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
62862306a36Sopenharmony_ci	scratch |= 0x80;
62962306a36Sopenharmony_ci	pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
63062306a36Sopenharmony_ci
63162306a36Sopenharmony_ci	clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
63262306a36Sopenharmony_ci	sdhci_o2_enable_clk(host, clk);
63362306a36Sopenharmony_ci}
63462306a36Sopenharmony_ci
63562306a36Sopenharmony_cistatic int sdhci_pci_o2_init_sd_express(struct mmc_host *mmc, struct mmc_ios *ios)
63662306a36Sopenharmony_ci{
63762306a36Sopenharmony_ci	struct sdhci_host *host = mmc_priv(mmc);
63862306a36Sopenharmony_ci	struct sdhci_pci_slot *slot = sdhci_priv(host);
63962306a36Sopenharmony_ci	struct sdhci_pci_chip *chip = slot->chip;
64062306a36Sopenharmony_ci	u8 scratch8;
64162306a36Sopenharmony_ci	u16 scratch16;
64262306a36Sopenharmony_ci	int ret;
64362306a36Sopenharmony_ci
64462306a36Sopenharmony_ci	/* Disable clock */
64562306a36Sopenharmony_ci	sdhci_writeb(host, 0, SDHCI_CLOCK_CONTROL);
64662306a36Sopenharmony_ci
64762306a36Sopenharmony_ci	/* Set VDD2 voltage*/
64862306a36Sopenharmony_ci	scratch8 = sdhci_readb(host, SDHCI_POWER_CONTROL);
64962306a36Sopenharmony_ci	scratch8 &= 0x0F;
65062306a36Sopenharmony_ci	if (host->mmc->ios.timing == MMC_TIMING_SD_EXP_1_2V &&
65162306a36Sopenharmony_ci	    host->mmc->caps2 & MMC_CAP2_SD_EXP_1_2V) {
65262306a36Sopenharmony_ci		scratch8 |= SDHCI_VDD2_POWER_ON | SDHCI_VDD2_POWER_120;
65362306a36Sopenharmony_ci	} else {
65462306a36Sopenharmony_ci		scratch8 |= SDHCI_VDD2_POWER_ON | SDHCI_VDD2_POWER_180;
65562306a36Sopenharmony_ci	}
65662306a36Sopenharmony_ci
65762306a36Sopenharmony_ci	sdhci_writeb(host, scratch8, SDHCI_POWER_CONTROL);
65862306a36Sopenharmony_ci
65962306a36Sopenharmony_ci	/* UnLock WP */
66062306a36Sopenharmony_ci	pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch8);
66162306a36Sopenharmony_ci	scratch8 &= 0x7f;
66262306a36Sopenharmony_ci	pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch8);
66362306a36Sopenharmony_ci
66462306a36Sopenharmony_ci	/* Wait for express card clkreqn assert */
66562306a36Sopenharmony_ci	ret = read_poll_timeout(sdhci_readb, scratch8, !(scratch8 & BIT(0)),
66662306a36Sopenharmony_ci				1, 30000, false, host, O2_SD_EXP_INT_REG);
66762306a36Sopenharmony_ci
66862306a36Sopenharmony_ci	if (!ret) {
66962306a36Sopenharmony_ci		/* Switch to PCIe mode */
67062306a36Sopenharmony_ci		scratch16 = sdhci_readw(host, O2_SD_PCIE_SWITCH);
67162306a36Sopenharmony_ci		scratch16 |= BIT(8);
67262306a36Sopenharmony_ci		sdhci_writew(host, scratch16, O2_SD_PCIE_SWITCH);
67362306a36Sopenharmony_ci	} else {
67462306a36Sopenharmony_ci		/* Power off VDD2 voltage*/
67562306a36Sopenharmony_ci		scratch8 = sdhci_readb(host, SDHCI_POWER_CONTROL);
67662306a36Sopenharmony_ci		scratch8 &= 0x0F;
67762306a36Sopenharmony_ci		sdhci_writeb(host, scratch8, SDHCI_POWER_CONTROL);
67862306a36Sopenharmony_ci
67962306a36Sopenharmony_ci		/* Keep mode as UHSI */
68062306a36Sopenharmony_ci		pci_read_config_word(chip->pdev, O2_SD_PARA_SET_REG1, &scratch16);
68162306a36Sopenharmony_ci		scratch16 &= ~BIT(11);
68262306a36Sopenharmony_ci		pci_write_config_word(chip->pdev, O2_SD_PARA_SET_REG1, scratch16);
68362306a36Sopenharmony_ci
68462306a36Sopenharmony_ci		host->mmc->ios.timing = MMC_TIMING_LEGACY;
68562306a36Sopenharmony_ci		pr_info("%s: Express card initialization failed, falling back to Legacy\n",
68662306a36Sopenharmony_ci			mmc_hostname(host->mmc));
68762306a36Sopenharmony_ci	}
68862306a36Sopenharmony_ci	/* Lock WP */
68962306a36Sopenharmony_ci	pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch8);
69062306a36Sopenharmony_ci	scratch8 |= 0x80;
69162306a36Sopenharmony_ci	pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch8);
69262306a36Sopenharmony_ci
69362306a36Sopenharmony_ci	return 0;
69462306a36Sopenharmony_ci}
69562306a36Sopenharmony_ci
69662306a36Sopenharmony_cistatic void sdhci_pci_o2_set_power(struct sdhci_host *host, unsigned char mode,  unsigned short vdd)
69762306a36Sopenharmony_ci{
69862306a36Sopenharmony_ci	struct sdhci_pci_chip *chip;
69962306a36Sopenharmony_ci	struct sdhci_pci_slot *slot = sdhci_priv(host);
70062306a36Sopenharmony_ci	u32 scratch_32 = 0;
70162306a36Sopenharmony_ci	u8 scratch_8 = 0;
70262306a36Sopenharmony_ci
70362306a36Sopenharmony_ci	chip = slot->chip;
70462306a36Sopenharmony_ci
70562306a36Sopenharmony_ci	if (mode == MMC_POWER_OFF) {
70662306a36Sopenharmony_ci		/* UnLock WP */
70762306a36Sopenharmony_ci		pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch_8);
70862306a36Sopenharmony_ci		scratch_8 &= 0x7f;
70962306a36Sopenharmony_ci		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
71062306a36Sopenharmony_ci
71162306a36Sopenharmony_ci		/* Set PCR 0x354[16] to switch Clock Source back to OPE Clock */
71262306a36Sopenharmony_ci		pci_read_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, &scratch_32);
71362306a36Sopenharmony_ci		scratch_32 &= ~(O2_SD_SEL_DLL);
71462306a36Sopenharmony_ci		pci_write_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, scratch_32);
71562306a36Sopenharmony_ci
71662306a36Sopenharmony_ci		/* Lock WP */
71762306a36Sopenharmony_ci		pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch_8);
71862306a36Sopenharmony_ci		scratch_8 |= 0x80;
71962306a36Sopenharmony_ci		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
72062306a36Sopenharmony_ci	}
72162306a36Sopenharmony_ci
72262306a36Sopenharmony_ci	sdhci_set_power(host, mode, vdd);
72362306a36Sopenharmony_ci}
72462306a36Sopenharmony_ci
72562306a36Sopenharmony_cistatic int sdhci_pci_o2_probe_slot(struct sdhci_pci_slot *slot)
72662306a36Sopenharmony_ci{
72762306a36Sopenharmony_ci	struct sdhci_pci_chip *chip;
72862306a36Sopenharmony_ci	struct sdhci_host *host;
72962306a36Sopenharmony_ci	struct o2_host *o2_host = sdhci_pci_priv(slot);
73062306a36Sopenharmony_ci	u32 reg, caps;
73162306a36Sopenharmony_ci	int ret;
73262306a36Sopenharmony_ci
73362306a36Sopenharmony_ci	chip = slot->chip;
73462306a36Sopenharmony_ci	host = slot->host;
73562306a36Sopenharmony_ci
73662306a36Sopenharmony_ci	o2_host->dll_adjust_count = 0;
73762306a36Sopenharmony_ci	caps = sdhci_readl(host, SDHCI_CAPABILITIES);
73862306a36Sopenharmony_ci
73962306a36Sopenharmony_ci	/*
74062306a36Sopenharmony_ci	 * mmc_select_bus_width() will test the bus to determine the actual bus
74162306a36Sopenharmony_ci	 * width.
74262306a36Sopenharmony_ci	 */
74362306a36Sopenharmony_ci	if (caps & SDHCI_CAN_DO_8BIT)
74462306a36Sopenharmony_ci		host->mmc->caps |= MMC_CAP_8_BIT_DATA;
74562306a36Sopenharmony_ci
74662306a36Sopenharmony_ci	host->quirks2 |= SDHCI_QUIRK2_BROKEN_DDR50;
74762306a36Sopenharmony_ci
74862306a36Sopenharmony_ci	sdhci_pci_o2_enable_msi(chip, host);
74962306a36Sopenharmony_ci
75062306a36Sopenharmony_ci	host->mmc_host_ops.execute_tuning = sdhci_o2_execute_tuning;
75162306a36Sopenharmony_ci	switch (chip->pdev->device) {
75262306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_SDS0:
75362306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_SEABIRD0:
75462306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_SEABIRD1:
75562306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_SDS1:
75662306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_FUJIN2:
75762306a36Sopenharmony_ci		reg = sdhci_readl(host, O2_SD_VENDOR_SETTING);
75862306a36Sopenharmony_ci		if (reg & 0x1)
75962306a36Sopenharmony_ci			host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
76062306a36Sopenharmony_ci
76162306a36Sopenharmony_ci		if (chip->pdev->device == PCI_DEVICE_ID_O2_SEABIRD0) {
76262306a36Sopenharmony_ci			ret = pci_read_config_dword(chip->pdev,
76362306a36Sopenharmony_ci						    O2_SD_MISC_SETTING, &reg);
76462306a36Sopenharmony_ci			if (ret)
76562306a36Sopenharmony_ci				return -EIO;
76662306a36Sopenharmony_ci			if (reg & (1 << 4)) {
76762306a36Sopenharmony_ci				pr_info("%s: emmc 1.8v flag is set, force 1.8v signaling voltage\n",
76862306a36Sopenharmony_ci					mmc_hostname(host->mmc));
76962306a36Sopenharmony_ci				host->flags &= ~SDHCI_SIGNALING_330;
77062306a36Sopenharmony_ci				host->flags |= SDHCI_SIGNALING_180;
77162306a36Sopenharmony_ci				host->mmc->caps2 |= MMC_CAP2_NO_SD;
77262306a36Sopenharmony_ci				host->mmc->caps2 |= MMC_CAP2_NO_SDIO;
77362306a36Sopenharmony_ci				pci_write_config_dword(chip->pdev,
77462306a36Sopenharmony_ci						       O2_SD_DETECT_SETTING, 3);
77562306a36Sopenharmony_ci			}
77662306a36Sopenharmony_ci
77762306a36Sopenharmony_ci			slot->host->mmc_host_ops.get_cd = sdhci_o2_get_cd;
77862306a36Sopenharmony_ci		}
77962306a36Sopenharmony_ci
78062306a36Sopenharmony_ci		if (chip->pdev->device == PCI_DEVICE_ID_O2_SEABIRD1) {
78162306a36Sopenharmony_ci			slot->host->mmc_host_ops.get_cd = sdhci_o2_get_cd;
78262306a36Sopenharmony_ci			host->mmc->caps2 |= MMC_CAP2_NO_SDIO;
78362306a36Sopenharmony_ci			host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
78462306a36Sopenharmony_ci		}
78562306a36Sopenharmony_ci
78662306a36Sopenharmony_ci		if (chip->pdev->device != PCI_DEVICE_ID_O2_FUJIN2)
78762306a36Sopenharmony_ci			break;
78862306a36Sopenharmony_ci		/* set dll watch dog timer */
78962306a36Sopenharmony_ci		reg = sdhci_readl(host, O2_SD_VENDOR_SETTING2);
79062306a36Sopenharmony_ci		reg |= (1 << 12);
79162306a36Sopenharmony_ci		sdhci_writel(host, reg, O2_SD_VENDOR_SETTING2);
79262306a36Sopenharmony_ci		break;
79362306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_GG8_9860:
79462306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_GG8_9861:
79562306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_GG8_9862:
79662306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_GG8_9863:
79762306a36Sopenharmony_ci		host->mmc->caps2 |= MMC_CAP2_NO_SDIO | MMC_CAP2_SD_EXP | MMC_CAP2_SD_EXP_1_2V;
79862306a36Sopenharmony_ci		host->mmc->caps |= MMC_CAP_HW_RESET;
79962306a36Sopenharmony_ci		host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
80062306a36Sopenharmony_ci		slot->host->mmc_host_ops.get_cd = sdhci_o2_get_cd;
80162306a36Sopenharmony_ci		host->mmc_host_ops.init_sd_express = sdhci_pci_o2_init_sd_express;
80262306a36Sopenharmony_ci		break;
80362306a36Sopenharmony_ci	default:
80462306a36Sopenharmony_ci		break;
80562306a36Sopenharmony_ci	}
80662306a36Sopenharmony_ci
80762306a36Sopenharmony_ci	return 0;
80862306a36Sopenharmony_ci}
80962306a36Sopenharmony_ci
81062306a36Sopenharmony_cistatic int sdhci_pci_o2_probe(struct sdhci_pci_chip *chip)
81162306a36Sopenharmony_ci{
81262306a36Sopenharmony_ci	int ret;
81362306a36Sopenharmony_ci	u8 scratch;
81462306a36Sopenharmony_ci	u16 scratch16;
81562306a36Sopenharmony_ci	u32 scratch_32;
81662306a36Sopenharmony_ci
81762306a36Sopenharmony_ci	switch (chip->pdev->device) {
81862306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_8220:
81962306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_8221:
82062306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_8320:
82162306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_8321:
82262306a36Sopenharmony_ci		/* This extra setup is required due to broken ADMA. */
82362306a36Sopenharmony_ci		ret = pci_read_config_byte(chip->pdev,
82462306a36Sopenharmony_ci				O2_SD_LOCK_WP, &scratch);
82562306a36Sopenharmony_ci		if (ret)
82662306a36Sopenharmony_ci			return ret;
82762306a36Sopenharmony_ci		scratch &= 0x7f;
82862306a36Sopenharmony_ci		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
82962306a36Sopenharmony_ci
83062306a36Sopenharmony_ci		/* Set Multi 3 to VCC3V# */
83162306a36Sopenharmony_ci		pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
83262306a36Sopenharmony_ci
83362306a36Sopenharmony_ci		/* Disable CLK_REQ# support after media DET */
83462306a36Sopenharmony_ci		ret = pci_read_config_byte(chip->pdev,
83562306a36Sopenharmony_ci				O2_SD_CLKREQ, &scratch);
83662306a36Sopenharmony_ci		if (ret)
83762306a36Sopenharmony_ci			return ret;
83862306a36Sopenharmony_ci		scratch |= 0x20;
83962306a36Sopenharmony_ci		pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
84062306a36Sopenharmony_ci
84162306a36Sopenharmony_ci		/* Choose capabilities, enable SDMA.  We have to write 0x01
84262306a36Sopenharmony_ci		 * to the capabilities register first to unlock it.
84362306a36Sopenharmony_ci		 */
84462306a36Sopenharmony_ci		ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
84562306a36Sopenharmony_ci		if (ret)
84662306a36Sopenharmony_ci			return ret;
84762306a36Sopenharmony_ci		scratch |= 0x01;
84862306a36Sopenharmony_ci		pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
84962306a36Sopenharmony_ci		pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
85062306a36Sopenharmony_ci
85162306a36Sopenharmony_ci		/* Disable ADMA1/2 */
85262306a36Sopenharmony_ci		pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
85362306a36Sopenharmony_ci		pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
85462306a36Sopenharmony_ci
85562306a36Sopenharmony_ci		/* Disable the infinite transfer mode */
85662306a36Sopenharmony_ci		ret = pci_read_config_byte(chip->pdev,
85762306a36Sopenharmony_ci				O2_SD_INF_MOD, &scratch);
85862306a36Sopenharmony_ci		if (ret)
85962306a36Sopenharmony_ci			return ret;
86062306a36Sopenharmony_ci		scratch |= 0x08;
86162306a36Sopenharmony_ci		pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
86262306a36Sopenharmony_ci
86362306a36Sopenharmony_ci		/* Lock WP */
86462306a36Sopenharmony_ci		ret = pci_read_config_byte(chip->pdev,
86562306a36Sopenharmony_ci				O2_SD_LOCK_WP, &scratch);
86662306a36Sopenharmony_ci		if (ret)
86762306a36Sopenharmony_ci			return ret;
86862306a36Sopenharmony_ci		scratch |= 0x80;
86962306a36Sopenharmony_ci		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
87062306a36Sopenharmony_ci		break;
87162306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_SDS0:
87262306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_SDS1:
87362306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_FUJIN2:
87462306a36Sopenharmony_ci		/* UnLock WP */
87562306a36Sopenharmony_ci		ret = pci_read_config_byte(chip->pdev,
87662306a36Sopenharmony_ci				O2_SD_LOCK_WP, &scratch);
87762306a36Sopenharmony_ci		if (ret)
87862306a36Sopenharmony_ci			return ret;
87962306a36Sopenharmony_ci
88062306a36Sopenharmony_ci		scratch &= 0x7f;
88162306a36Sopenharmony_ci		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
88262306a36Sopenharmony_ci
88362306a36Sopenharmony_ci		/* DevId=8520 subId= 0x11 or 0x12  Type Chip support */
88462306a36Sopenharmony_ci		if (chip->pdev->device == PCI_DEVICE_ID_O2_FUJIN2) {
88562306a36Sopenharmony_ci			ret = pci_read_config_dword(chip->pdev,
88662306a36Sopenharmony_ci						    O2_SD_FUNC_REG0,
88762306a36Sopenharmony_ci						    &scratch_32);
88862306a36Sopenharmony_ci			if (ret)
88962306a36Sopenharmony_ci				return ret;
89062306a36Sopenharmony_ci			scratch_32 = ((scratch_32 & 0xFF000000) >> 24);
89162306a36Sopenharmony_ci
89262306a36Sopenharmony_ci			/* Check Whether subId is 0x11 or 0x12 */
89362306a36Sopenharmony_ci			if ((scratch_32 == 0x11) || (scratch_32 == 0x12)) {
89462306a36Sopenharmony_ci				scratch_32 = 0x25100000;
89562306a36Sopenharmony_ci
89662306a36Sopenharmony_ci				o2_pci_set_baseclk(chip, scratch_32);
89762306a36Sopenharmony_ci				ret = pci_read_config_dword(chip->pdev,
89862306a36Sopenharmony_ci							    O2_SD_FUNC_REG4,
89962306a36Sopenharmony_ci							    &scratch_32);
90062306a36Sopenharmony_ci				if (ret)
90162306a36Sopenharmony_ci					return ret;
90262306a36Sopenharmony_ci
90362306a36Sopenharmony_ci				/* Enable Base Clk setting change */
90462306a36Sopenharmony_ci				scratch_32 |= O2_SD_FREG4_ENABLE_CLK_SET;
90562306a36Sopenharmony_ci				pci_write_config_dword(chip->pdev,
90662306a36Sopenharmony_ci						       O2_SD_FUNC_REG4,
90762306a36Sopenharmony_ci						       scratch_32);
90862306a36Sopenharmony_ci
90962306a36Sopenharmony_ci				/* Set Tuning Window to 4 */
91062306a36Sopenharmony_ci				pci_write_config_byte(chip->pdev,
91162306a36Sopenharmony_ci						      O2_SD_TUNING_CTRL, 0x44);
91262306a36Sopenharmony_ci
91362306a36Sopenharmony_ci				break;
91462306a36Sopenharmony_ci			}
91562306a36Sopenharmony_ci		}
91662306a36Sopenharmony_ci
91762306a36Sopenharmony_ci		/* Enable 8520 led function */
91862306a36Sopenharmony_ci		o2_pci_led_enable(chip);
91962306a36Sopenharmony_ci
92062306a36Sopenharmony_ci		/* Set timeout CLK */
92162306a36Sopenharmony_ci		ret = pci_read_config_dword(chip->pdev,
92262306a36Sopenharmony_ci					    O2_SD_CLK_SETTING, &scratch_32);
92362306a36Sopenharmony_ci		if (ret)
92462306a36Sopenharmony_ci			return ret;
92562306a36Sopenharmony_ci
92662306a36Sopenharmony_ci		scratch_32 &= ~(0xFF00);
92762306a36Sopenharmony_ci		scratch_32 |= 0x07E0C800;
92862306a36Sopenharmony_ci		pci_write_config_dword(chip->pdev,
92962306a36Sopenharmony_ci				       O2_SD_CLK_SETTING, scratch_32);
93062306a36Sopenharmony_ci
93162306a36Sopenharmony_ci		ret = pci_read_config_dword(chip->pdev,
93262306a36Sopenharmony_ci					    O2_SD_CLKREQ, &scratch_32);
93362306a36Sopenharmony_ci		if (ret)
93462306a36Sopenharmony_ci			return ret;
93562306a36Sopenharmony_ci		scratch_32 |= 0x3;
93662306a36Sopenharmony_ci		pci_write_config_dword(chip->pdev, O2_SD_CLKREQ, scratch_32);
93762306a36Sopenharmony_ci
93862306a36Sopenharmony_ci		ret = pci_read_config_dword(chip->pdev,
93962306a36Sopenharmony_ci					    O2_SD_PLL_SETTING, &scratch_32);
94062306a36Sopenharmony_ci		if (ret)
94162306a36Sopenharmony_ci			return ret;
94262306a36Sopenharmony_ci
94362306a36Sopenharmony_ci		scratch_32 &= ~(0x1F3F070E);
94462306a36Sopenharmony_ci		scratch_32 |= 0x18270106;
94562306a36Sopenharmony_ci		pci_write_config_dword(chip->pdev,
94662306a36Sopenharmony_ci				       O2_SD_PLL_SETTING, scratch_32);
94762306a36Sopenharmony_ci
94862306a36Sopenharmony_ci		/* Disable UHS1 funciton */
94962306a36Sopenharmony_ci		ret = pci_read_config_dword(chip->pdev,
95062306a36Sopenharmony_ci					    O2_SD_CAP_REG2, &scratch_32);
95162306a36Sopenharmony_ci		if (ret)
95262306a36Sopenharmony_ci			return ret;
95362306a36Sopenharmony_ci		scratch_32 &= ~(0xE0);
95462306a36Sopenharmony_ci		pci_write_config_dword(chip->pdev,
95562306a36Sopenharmony_ci				       O2_SD_CAP_REG2, scratch_32);
95662306a36Sopenharmony_ci
95762306a36Sopenharmony_ci		if (chip->pdev->device == PCI_DEVICE_ID_O2_FUJIN2)
95862306a36Sopenharmony_ci			sdhci_pci_o2_fujin2_pci_init(chip);
95962306a36Sopenharmony_ci
96062306a36Sopenharmony_ci		/* Lock WP */
96162306a36Sopenharmony_ci		ret = pci_read_config_byte(chip->pdev,
96262306a36Sopenharmony_ci					   O2_SD_LOCK_WP, &scratch);
96362306a36Sopenharmony_ci		if (ret)
96462306a36Sopenharmony_ci			return ret;
96562306a36Sopenharmony_ci		scratch |= 0x80;
96662306a36Sopenharmony_ci		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
96762306a36Sopenharmony_ci		break;
96862306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_SEABIRD0:
96962306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_SEABIRD1:
97062306a36Sopenharmony_ci		/* UnLock WP */
97162306a36Sopenharmony_ci		ret = pci_read_config_byte(chip->pdev,
97262306a36Sopenharmony_ci				O2_SD_LOCK_WP, &scratch);
97362306a36Sopenharmony_ci		if (ret)
97462306a36Sopenharmony_ci			return ret;
97562306a36Sopenharmony_ci
97662306a36Sopenharmony_ci		scratch &= 0x7f;
97762306a36Sopenharmony_ci		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
97862306a36Sopenharmony_ci
97962306a36Sopenharmony_ci		ret = pci_read_config_dword(chip->pdev,
98062306a36Sopenharmony_ci					    O2_SD_PLL_SETTING, &scratch_32);
98162306a36Sopenharmony_ci		if (ret)
98262306a36Sopenharmony_ci			return ret;
98362306a36Sopenharmony_ci
98462306a36Sopenharmony_ci		if ((scratch_32 & 0xff000000) == 0x01000000) {
98562306a36Sopenharmony_ci			scratch_32 &= 0x0000FFFF;
98662306a36Sopenharmony_ci			scratch_32 |= 0x1F340000;
98762306a36Sopenharmony_ci
98862306a36Sopenharmony_ci			pci_write_config_dword(chip->pdev,
98962306a36Sopenharmony_ci					       O2_SD_PLL_SETTING, scratch_32);
99062306a36Sopenharmony_ci		} else {
99162306a36Sopenharmony_ci			scratch_32 &= 0x0000FFFF;
99262306a36Sopenharmony_ci			scratch_32 |= 0x25100000;
99362306a36Sopenharmony_ci
99462306a36Sopenharmony_ci			pci_write_config_dword(chip->pdev,
99562306a36Sopenharmony_ci					       O2_SD_PLL_SETTING, scratch_32);
99662306a36Sopenharmony_ci
99762306a36Sopenharmony_ci			ret = pci_read_config_dword(chip->pdev,
99862306a36Sopenharmony_ci						    O2_SD_FUNC_REG4,
99962306a36Sopenharmony_ci						    &scratch_32);
100062306a36Sopenharmony_ci			if (ret)
100162306a36Sopenharmony_ci				return ret;
100262306a36Sopenharmony_ci			scratch_32 |= (1 << 22);
100362306a36Sopenharmony_ci			pci_write_config_dword(chip->pdev,
100462306a36Sopenharmony_ci					       O2_SD_FUNC_REG4, scratch_32);
100562306a36Sopenharmony_ci		}
100662306a36Sopenharmony_ci
100762306a36Sopenharmony_ci		/* Set Tuning Windows to 5 */
100862306a36Sopenharmony_ci		pci_write_config_byte(chip->pdev,
100962306a36Sopenharmony_ci				O2_SD_TUNING_CTRL, 0x55);
101062306a36Sopenharmony_ci		//Adjust 1st and 2nd CD debounce time
101162306a36Sopenharmony_ci		pci_read_config_dword(chip->pdev, O2_SD_MISC_CTRL2, &scratch_32);
101262306a36Sopenharmony_ci		scratch_32 &= 0xFFE7FFFF;
101362306a36Sopenharmony_ci		scratch_32 |= 0x00180000;
101462306a36Sopenharmony_ci		pci_write_config_dword(chip->pdev, O2_SD_MISC_CTRL2, scratch_32);
101562306a36Sopenharmony_ci		pci_write_config_dword(chip->pdev, O2_SD_DETECT_SETTING, 1);
101662306a36Sopenharmony_ci		/* Lock WP */
101762306a36Sopenharmony_ci		ret = pci_read_config_byte(chip->pdev,
101862306a36Sopenharmony_ci					   O2_SD_LOCK_WP, &scratch);
101962306a36Sopenharmony_ci		if (ret)
102062306a36Sopenharmony_ci			return ret;
102162306a36Sopenharmony_ci		scratch |= 0x80;
102262306a36Sopenharmony_ci		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
102362306a36Sopenharmony_ci		break;
102462306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_GG8_9860:
102562306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_GG8_9861:
102662306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_GG8_9862:
102762306a36Sopenharmony_ci	case PCI_DEVICE_ID_O2_GG8_9863:
102862306a36Sopenharmony_ci		/* UnLock WP */
102962306a36Sopenharmony_ci		ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
103062306a36Sopenharmony_ci		if (ret)
103162306a36Sopenharmony_ci			return ret;
103262306a36Sopenharmony_ci		scratch &= 0x7f;
103362306a36Sopenharmony_ci		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
103462306a36Sopenharmony_ci
103562306a36Sopenharmony_ci		/* Select mode switch source as software control */
103662306a36Sopenharmony_ci		pci_read_config_word(chip->pdev, O2_SD_PARA_SET_REG1, &scratch16);
103762306a36Sopenharmony_ci		scratch16 &= 0xF8FF;
103862306a36Sopenharmony_ci		scratch16 |= BIT(9);
103962306a36Sopenharmony_ci		pci_write_config_word(chip->pdev, O2_SD_PARA_SET_REG1, scratch16);
104062306a36Sopenharmony_ci
104162306a36Sopenharmony_ci		/* set VDD1 supply source */
104262306a36Sopenharmony_ci		pci_read_config_word(chip->pdev, O2_SD_VDDX_CTRL_REG, &scratch16);
104362306a36Sopenharmony_ci		scratch16 &= 0xFFE3;
104462306a36Sopenharmony_ci		scratch16 |= BIT(3);
104562306a36Sopenharmony_ci		pci_write_config_word(chip->pdev, O2_SD_VDDX_CTRL_REG, scratch16);
104662306a36Sopenharmony_ci
104762306a36Sopenharmony_ci		/* Set host drive strength*/
104862306a36Sopenharmony_ci		scratch16 = 0x0025;
104962306a36Sopenharmony_ci		pci_write_config_word(chip->pdev, O2_SD_PLL_SETTING, scratch16);
105062306a36Sopenharmony_ci
105162306a36Sopenharmony_ci		/* Set output delay*/
105262306a36Sopenharmony_ci		pci_read_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, &scratch_32);
105362306a36Sopenharmony_ci		scratch_32 &= 0xFF0FFF00;
105462306a36Sopenharmony_ci		scratch_32 |= 0x00B0003B;
105562306a36Sopenharmony_ci		pci_write_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, scratch_32);
105662306a36Sopenharmony_ci
105762306a36Sopenharmony_ci		/* Lock WP */
105862306a36Sopenharmony_ci		ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
105962306a36Sopenharmony_ci		if (ret)
106062306a36Sopenharmony_ci			return ret;
106162306a36Sopenharmony_ci		scratch |= 0x80;
106262306a36Sopenharmony_ci		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
106362306a36Sopenharmony_ci		break;
106462306a36Sopenharmony_ci	}
106562306a36Sopenharmony_ci
106662306a36Sopenharmony_ci	return 0;
106762306a36Sopenharmony_ci}
106862306a36Sopenharmony_ci
106962306a36Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
107062306a36Sopenharmony_cistatic int sdhci_pci_o2_resume(struct sdhci_pci_chip *chip)
107162306a36Sopenharmony_ci{
107262306a36Sopenharmony_ci	sdhci_pci_o2_probe(chip);
107362306a36Sopenharmony_ci	return sdhci_pci_resume_host(chip);
107462306a36Sopenharmony_ci}
107562306a36Sopenharmony_ci#endif
107662306a36Sopenharmony_ci
107762306a36Sopenharmony_cistatic const struct sdhci_ops sdhci_pci_o2_ops = {
107862306a36Sopenharmony_ci	.set_clock = sdhci_pci_o2_set_clock,
107962306a36Sopenharmony_ci	.enable_dma = sdhci_pci_enable_dma,
108062306a36Sopenharmony_ci	.set_bus_width = sdhci_set_bus_width,
108162306a36Sopenharmony_ci	.reset = sdhci_reset,
108262306a36Sopenharmony_ci	.set_uhs_signaling = sdhci_set_uhs_signaling,
108362306a36Sopenharmony_ci	.set_power = sdhci_pci_o2_set_power,
108462306a36Sopenharmony_ci};
108562306a36Sopenharmony_ci
108662306a36Sopenharmony_ciconst struct sdhci_pci_fixes sdhci_o2 = {
108762306a36Sopenharmony_ci	.probe = sdhci_pci_o2_probe,
108862306a36Sopenharmony_ci	.quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
108962306a36Sopenharmony_ci	.quirks2 = SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD,
109062306a36Sopenharmony_ci	.probe_slot = sdhci_pci_o2_probe_slot,
109162306a36Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
109262306a36Sopenharmony_ci	.resume = sdhci_pci_o2_resume,
109362306a36Sopenharmony_ci#endif
109462306a36Sopenharmony_ci	.ops = &sdhci_pci_o2_ops,
109562306a36Sopenharmony_ci	.priv_size = sizeof(struct o2_host),
109662306a36Sopenharmony_ci};
1097