1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 BayHub Technology Ltd.
4  *
5  * Authors: Peter Guo <peter.guo@bayhubtech.com>
6  *          Adam Lee <adam.lee@canonical.com>
7  *          Ernest Zhang <ernest.zhang@bayhubtech.com>
8  */
9 
10 #include <linux/pci.h>
11 #include <linux/mmc/host.h>
12 #include <linux/mmc/mmc.h>
13 #include <linux/delay.h>
14 
15 #include "sdhci.h"
16 #include "sdhci-pci.h"
17 
18 /*
19  * O2Micro device registers
20  */
21 
22 #define O2_SD_MISC_REG5		0x64
23 #define O2_SD_LD0_CTRL		0x68
24 #define O2_SD_DEV_CTRL		0x88
25 #define O2_SD_LOCK_WP		0xD3
26 #define O2_SD_TEST_REG		0xD4
27 #define O2_SD_FUNC_REG0		0xDC
28 #define O2_SD_MULTI_VCC3V	0xEE
29 #define O2_SD_CLKREQ		0xEC
30 #define O2_SD_CAPS		0xE0
31 #define O2_SD_ADMA1		0xE2
32 #define O2_SD_ADMA2		0xE7
33 #define O2_SD_INF_MOD		0xF1
34 #define O2_SD_MISC_CTRL4	0xFC
35 #define O2_SD_TUNING_CTRL	0x300
36 #define O2_SD_PLL_SETTING	0x304
37 #define O2_SD_MISC_SETTING	0x308
38 #define O2_SD_CLK_SETTING	0x328
39 #define O2_SD_CAP_REG2		0x330
40 #define O2_SD_CAP_REG0		0x334
41 #define O2_SD_UHS1_CAP_SETTING	0x33C
42 #define O2_SD_DELAY_CTRL	0x350
43 #define O2_SD_UHS2_L1_CTRL	0x35C
44 #define O2_SD_FUNC_REG3		0x3E0
45 #define O2_SD_FUNC_REG4		0x3E4
46 #define O2_SD_LED_ENABLE	BIT(6)
47 #define O2_SD_FREG0_LEDOFF	BIT(13)
48 #define O2_SD_FREG4_ENABLE_CLK_SET	BIT(22)
49 
50 #define O2_SD_VENDOR_SETTING	0x110
51 #define O2_SD_VENDOR_SETTING2	0x1C8
52 #define O2_SD_HW_TUNING_DISABLE	BIT(4)
53 
54 #define O2_PLL_WDT_CONTROL1	0x1CC
55 #define  O2_PLL_FORCE_ACTIVE	BIT(18)
56 #define  O2_PLL_LOCK_STATUS	BIT(14)
57 #define  O2_PLL_SOFT_RESET	BIT(12)
58 
59 #define O2_SD_DETECT_SETTING 0x324
60 
61 static void sdhci_o2_set_tuning_mode(struct sdhci_host *host)
62 {
63 	u16 reg;
64 
65 	/* enable hardware tuning */
66 	reg = sdhci_readw(host, O2_SD_VENDOR_SETTING);
67 	reg &= ~O2_SD_HW_TUNING_DISABLE;
68 	sdhci_writew(host, reg, O2_SD_VENDOR_SETTING);
69 }
70 
71 static void __sdhci_o2_execute_tuning(struct sdhci_host *host, u32 opcode)
72 {
73 	int i;
74 
75 	sdhci_send_tuning(host, MMC_SEND_TUNING_BLOCK_HS200);
76 
77 	for (i = 0; i < 150; i++) {
78 		u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
79 
80 		if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
81 			if (ctrl & SDHCI_CTRL_TUNED_CLK) {
82 				host->tuning_done = true;
83 				return;
84 			}
85 			pr_warn("%s: HW tuning failed !\n",
86 				mmc_hostname(host->mmc));
87 			break;
88 		}
89 
90 		mdelay(1);
91 	}
92 
93 	pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
94 		mmc_hostname(host->mmc));
95 	sdhci_reset_tuning(host);
96 }
97 
98 static int sdhci_o2_execute_tuning(struct mmc_host *mmc, u32 opcode)
99 {
100 	struct sdhci_host *host = mmc_priv(mmc);
101 	int current_bus_width = 0;
102 
103 	/*
104 	 * This handler only implements the eMMC tuning that is specific to
105 	 * this controller.  Fall back to the standard method for other TIMING.
106 	 */
107 	if (host->timing != MMC_TIMING_MMC_HS200)
108 		return sdhci_execute_tuning(mmc, opcode);
109 
110 	if (WARN_ON(opcode != MMC_SEND_TUNING_BLOCK_HS200))
111 		return -EINVAL;
112 
113 	/*
114 	 * o2 sdhci host didn't support 8bit emmc tuning
115 	 */
116 	if (mmc->ios.bus_width == MMC_BUS_WIDTH_8) {
117 		current_bus_width = mmc->ios.bus_width;
118 		mmc->ios.bus_width = MMC_BUS_WIDTH_4;
119 		sdhci_set_bus_width(host, MMC_BUS_WIDTH_4);
120 	}
121 
122 	sdhci_o2_set_tuning_mode(host);
123 
124 	sdhci_start_tuning(host);
125 
126 	__sdhci_o2_execute_tuning(host, opcode);
127 
128 	sdhci_end_tuning(host);
129 
130 	if (current_bus_width == MMC_BUS_WIDTH_8) {
131 		mmc->ios.bus_width = MMC_BUS_WIDTH_8;
132 		sdhci_set_bus_width(host, current_bus_width);
133 	}
134 
135 	host->flags &= ~SDHCI_HS400_TUNING;
136 	return 0;
137 }
138 
139 static void o2_pci_set_baseclk(struct sdhci_pci_chip *chip, u32 value)
140 {
141 	u32 scratch_32;
142 	pci_read_config_dword(chip->pdev,
143 			      O2_SD_PLL_SETTING, &scratch_32);
144 
145 	scratch_32 &= 0x0000FFFF;
146 	scratch_32 |= value;
147 
148 	pci_write_config_dword(chip->pdev,
149 			       O2_SD_PLL_SETTING, scratch_32);
150 }
151 
152 static void o2_pci_led_enable(struct sdhci_pci_chip *chip)
153 {
154 	int ret;
155 	u32 scratch_32;
156 
157 	/* Set led of SD host function enable */
158 	ret = pci_read_config_dword(chip->pdev,
159 				    O2_SD_FUNC_REG0, &scratch_32);
160 	if (ret)
161 		return;
162 
163 	scratch_32 &= ~O2_SD_FREG0_LEDOFF;
164 	pci_write_config_dword(chip->pdev,
165 			       O2_SD_FUNC_REG0, scratch_32);
166 
167 	ret = pci_read_config_dword(chip->pdev,
168 				    O2_SD_TEST_REG, &scratch_32);
169 	if (ret)
170 		return;
171 
172 	scratch_32 |= O2_SD_LED_ENABLE;
173 	pci_write_config_dword(chip->pdev,
174 			       O2_SD_TEST_REG, scratch_32);
175 
176 }
177 
178 static void sdhci_pci_o2_fujin2_pci_init(struct sdhci_pci_chip *chip)
179 {
180 	u32 scratch_32;
181 	int ret;
182 	/* Improve write performance for SD3.0 */
183 	ret = pci_read_config_dword(chip->pdev, O2_SD_DEV_CTRL, &scratch_32);
184 	if (ret)
185 		return;
186 	scratch_32 &= ~((1 << 12) | (1 << 13) | (1 << 14));
187 	pci_write_config_dword(chip->pdev, O2_SD_DEV_CTRL, scratch_32);
188 
189 	/* Enable Link abnormal reset generating Reset */
190 	ret = pci_read_config_dword(chip->pdev, O2_SD_MISC_REG5, &scratch_32);
191 	if (ret)
192 		return;
193 	scratch_32 &= ~((1 << 19) | (1 << 11));
194 	scratch_32 |= (1 << 10);
195 	pci_write_config_dword(chip->pdev, O2_SD_MISC_REG5, scratch_32);
196 
197 	/* set card power over current protection */
198 	ret = pci_read_config_dword(chip->pdev, O2_SD_TEST_REG, &scratch_32);
199 	if (ret)
200 		return;
201 	scratch_32 |= (1 << 4);
202 	pci_write_config_dword(chip->pdev, O2_SD_TEST_REG, scratch_32);
203 
204 	/* adjust the output delay for SD mode */
205 	pci_write_config_dword(chip->pdev, O2_SD_DELAY_CTRL, 0x00002492);
206 
207 	/* Set the output voltage setting of Aux 1.2v LDO */
208 	ret = pci_read_config_dword(chip->pdev, O2_SD_LD0_CTRL, &scratch_32);
209 	if (ret)
210 		return;
211 	scratch_32 &= ~(3 << 12);
212 	pci_write_config_dword(chip->pdev, O2_SD_LD0_CTRL, scratch_32);
213 
214 	/* Set Max power supply capability of SD host */
215 	ret = pci_read_config_dword(chip->pdev, O2_SD_CAP_REG0, &scratch_32);
216 	if (ret)
217 		return;
218 	scratch_32 &= ~(0x01FE);
219 	scratch_32 |= 0x00CC;
220 	pci_write_config_dword(chip->pdev, O2_SD_CAP_REG0, scratch_32);
221 	/* Set DLL Tuning Window */
222 	ret = pci_read_config_dword(chip->pdev,
223 				    O2_SD_TUNING_CTRL, &scratch_32);
224 	if (ret)
225 		return;
226 	scratch_32 &= ~(0x000000FF);
227 	scratch_32 |= 0x00000066;
228 	pci_write_config_dword(chip->pdev, O2_SD_TUNING_CTRL, scratch_32);
229 
230 	/* Set UHS2 T_EIDLE */
231 	ret = pci_read_config_dword(chip->pdev,
232 				    O2_SD_UHS2_L1_CTRL, &scratch_32);
233 	if (ret)
234 		return;
235 	scratch_32 &= ~(0x000000FC);
236 	scratch_32 |= 0x00000084;
237 	pci_write_config_dword(chip->pdev, O2_SD_UHS2_L1_CTRL, scratch_32);
238 
239 	/* Set UHS2 Termination */
240 	ret = pci_read_config_dword(chip->pdev, O2_SD_FUNC_REG3, &scratch_32);
241 	if (ret)
242 		return;
243 	scratch_32 &= ~((1 << 21) | (1 << 30));
244 
245 	pci_write_config_dword(chip->pdev, O2_SD_FUNC_REG3, scratch_32);
246 
247 	/* Set L1 Entrance Timer */
248 	ret = pci_read_config_dword(chip->pdev, O2_SD_CAPS, &scratch_32);
249 	if (ret)
250 		return;
251 	scratch_32 &= ~(0xf0000000);
252 	scratch_32 |= 0x30000000;
253 	pci_write_config_dword(chip->pdev, O2_SD_CAPS, scratch_32);
254 
255 	ret = pci_read_config_dword(chip->pdev,
256 				    O2_SD_MISC_CTRL4, &scratch_32);
257 	if (ret)
258 		return;
259 	scratch_32 &= ~(0x000f0000);
260 	scratch_32 |= 0x00080000;
261 	pci_write_config_dword(chip->pdev, O2_SD_MISC_CTRL4, scratch_32);
262 }
263 
264 static void sdhci_pci_o2_enable_msi(struct sdhci_pci_chip *chip,
265 				    struct sdhci_host *host)
266 {
267 	int ret;
268 
269 	ret = pci_find_capability(chip->pdev, PCI_CAP_ID_MSI);
270 	if (!ret) {
271 		pr_info("%s: unsupport msi, use INTx irq\n",
272 			mmc_hostname(host->mmc));
273 		return;
274 	}
275 
276 	ret = pci_alloc_irq_vectors(chip->pdev, 1, 1,
277 				    PCI_IRQ_MSI | PCI_IRQ_MSIX);
278 	if (ret < 0) {
279 		pr_err("%s: enable PCI MSI failed, err=%d\n",
280 		       mmc_hostname(host->mmc), ret);
281 		return;
282 	}
283 
284 	host->irq = pci_irq_vector(chip->pdev, 0);
285 }
286 
287 static void sdhci_o2_wait_card_detect_stable(struct sdhci_host *host)
288 {
289 	ktime_t timeout;
290 	u32 scratch32;
291 
292 	/* Wait max 50 ms */
293 	timeout = ktime_add_ms(ktime_get(), 50);
294 	while (1) {
295 		bool timedout = ktime_after(ktime_get(), timeout);
296 
297 		scratch32 = sdhci_readl(host, SDHCI_PRESENT_STATE);
298 		if ((scratch32 & SDHCI_CARD_PRESENT) >> SDHCI_CARD_PRES_SHIFT
299 		    == (scratch32 & SDHCI_CD_LVL) >> SDHCI_CD_LVL_SHIFT)
300 			break;
301 
302 		if (timedout) {
303 			pr_err("%s: Card Detect debounce never finished.\n",
304 			       mmc_hostname(host->mmc));
305 			sdhci_dumpregs(host);
306 			return;
307 		}
308 		udelay(10);
309 	}
310 }
311 
312 static void sdhci_o2_enable_internal_clock(struct sdhci_host *host)
313 {
314 	ktime_t timeout;
315 	u16 scratch;
316 	u32 scratch32;
317 
318 	/* PLL software reset */
319 	scratch32 = sdhci_readl(host, O2_PLL_WDT_CONTROL1);
320 	scratch32 |= O2_PLL_SOFT_RESET;
321 	sdhci_writel(host, scratch32, O2_PLL_WDT_CONTROL1);
322 	udelay(1);
323 	scratch32 &= ~(O2_PLL_SOFT_RESET);
324 	sdhci_writel(host, scratch32, O2_PLL_WDT_CONTROL1);
325 
326 	/* PLL force active */
327 	scratch32 |= O2_PLL_FORCE_ACTIVE;
328 	sdhci_writel(host, scratch32, O2_PLL_WDT_CONTROL1);
329 
330 	/* Wait max 20 ms */
331 	timeout = ktime_add_ms(ktime_get(), 20);
332 	while (1) {
333 		bool timedout = ktime_after(ktime_get(), timeout);
334 
335 		scratch = sdhci_readw(host, O2_PLL_WDT_CONTROL1);
336 		if (scratch & O2_PLL_LOCK_STATUS)
337 			break;
338 		if (timedout) {
339 			pr_err("%s: Internal clock never stabilised.\n",
340 			       mmc_hostname(host->mmc));
341 			sdhci_dumpregs(host);
342 			goto out;
343 		}
344 		udelay(10);
345 	}
346 
347 	/* Wait for card detect finish */
348 	udelay(1);
349 	sdhci_o2_wait_card_detect_stable(host);
350 
351 out:
352 	/* Cancel PLL force active */
353 	scratch32 = sdhci_readl(host, O2_PLL_WDT_CONTROL1);
354 	scratch32 &= ~O2_PLL_FORCE_ACTIVE;
355 	sdhci_writel(host, scratch32, O2_PLL_WDT_CONTROL1);
356 }
357 
358 static int sdhci_o2_get_cd(struct mmc_host *mmc)
359 {
360 	struct sdhci_host *host = mmc_priv(mmc);
361 
362 	sdhci_o2_enable_internal_clock(host);
363 
364 	return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
365 }
366 
367 static void sdhci_o2_enable_clk(struct sdhci_host *host, u16 clk)
368 {
369 	/* Enable internal clock */
370 	clk |= SDHCI_CLOCK_INT_EN;
371 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
372 
373 	if (sdhci_o2_get_cd(host->mmc)) {
374 		clk |= SDHCI_CLOCK_CARD_EN;
375 		sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
376 	}
377 }
378 
379 void sdhci_pci_o2_set_clock(struct sdhci_host *host, unsigned int clock)
380 {
381 	u16 clk;
382 
383 	host->mmc->actual_clock = 0;
384 
385 	sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
386 
387 	if (clock == 0)
388 		return;
389 
390 	clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
391 	sdhci_o2_enable_clk(host, clk);
392 }
393 
394 int sdhci_pci_o2_probe_slot(struct sdhci_pci_slot *slot)
395 {
396 	struct sdhci_pci_chip *chip;
397 	struct sdhci_host *host;
398 	u32 reg, caps;
399 	int ret;
400 
401 	chip = slot->chip;
402 	host = slot->host;
403 
404 	caps = sdhci_readl(host, SDHCI_CAPABILITIES);
405 
406 	/*
407 	 * mmc_select_bus_width() will test the bus to determine the actual bus
408 	 * width.
409 	 */
410 	if (caps & SDHCI_CAN_DO_8BIT)
411 		host->mmc->caps |= MMC_CAP_8_BIT_DATA;
412 
413 	switch (chip->pdev->device) {
414 	case PCI_DEVICE_ID_O2_SDS0:
415 	case PCI_DEVICE_ID_O2_SEABIRD0:
416 	case PCI_DEVICE_ID_O2_SEABIRD1:
417 	case PCI_DEVICE_ID_O2_SDS1:
418 	case PCI_DEVICE_ID_O2_FUJIN2:
419 		reg = sdhci_readl(host, O2_SD_VENDOR_SETTING);
420 		if (reg & 0x1)
421 			host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
422 
423 		sdhci_pci_o2_enable_msi(chip, host);
424 
425 		if (chip->pdev->device == PCI_DEVICE_ID_O2_SEABIRD0) {
426 			ret = pci_read_config_dword(chip->pdev,
427 						    O2_SD_MISC_SETTING, &reg);
428 			if (ret)
429 				return -EIO;
430 			if (reg & (1 << 4)) {
431 				pr_info("%s: emmc 1.8v flag is set, force 1.8v signaling voltage\n",
432 					mmc_hostname(host->mmc));
433 				host->flags &= ~SDHCI_SIGNALING_330;
434 				host->flags |= SDHCI_SIGNALING_180;
435 				host->quirks2 |= SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD;
436 				host->mmc->caps2 |= MMC_CAP2_NO_SD;
437 				host->mmc->caps2 |= MMC_CAP2_NO_SDIO;
438 				pci_write_config_dword(chip->pdev,
439 						       O2_SD_DETECT_SETTING, 3);
440 			}
441 
442 			slot->host->mmc_host_ops.get_cd = sdhci_o2_get_cd;
443 		}
444 
445 		host->mmc_host_ops.execute_tuning = sdhci_o2_execute_tuning;
446 
447 		if (chip->pdev->device != PCI_DEVICE_ID_O2_FUJIN2)
448 			break;
449 		/* set dll watch dog timer */
450 		reg = sdhci_readl(host, O2_SD_VENDOR_SETTING2);
451 		reg |= (1 << 12);
452 		sdhci_writel(host, reg, O2_SD_VENDOR_SETTING2);
453 
454 		break;
455 	default:
456 		break;
457 	}
458 
459 	return 0;
460 }
461 
462 int sdhci_pci_o2_probe(struct sdhci_pci_chip *chip)
463 {
464 	int ret;
465 	u8 scratch;
466 	u32 scratch_32;
467 
468 	switch (chip->pdev->device) {
469 	case PCI_DEVICE_ID_O2_8220:
470 	case PCI_DEVICE_ID_O2_8221:
471 	case PCI_DEVICE_ID_O2_8320:
472 	case PCI_DEVICE_ID_O2_8321:
473 		/* This extra setup is required due to broken ADMA. */
474 		ret = pci_read_config_byte(chip->pdev,
475 				O2_SD_LOCK_WP, &scratch);
476 		if (ret)
477 			return ret;
478 		scratch &= 0x7f;
479 		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
480 
481 		/* Set Multi 3 to VCC3V# */
482 		pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
483 
484 		/* Disable CLK_REQ# support after media DET */
485 		ret = pci_read_config_byte(chip->pdev,
486 				O2_SD_CLKREQ, &scratch);
487 		if (ret)
488 			return ret;
489 		scratch |= 0x20;
490 		pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
491 
492 		/* Choose capabilities, enable SDMA.  We have to write 0x01
493 		 * to the capabilities register first to unlock it.
494 		 */
495 		ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
496 		if (ret)
497 			return ret;
498 		scratch |= 0x01;
499 		pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
500 		pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
501 
502 		/* Disable ADMA1/2 */
503 		pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
504 		pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
505 
506 		/* Disable the infinite transfer mode */
507 		ret = pci_read_config_byte(chip->pdev,
508 				O2_SD_INF_MOD, &scratch);
509 		if (ret)
510 			return ret;
511 		scratch |= 0x08;
512 		pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
513 
514 		/* Lock WP */
515 		ret = pci_read_config_byte(chip->pdev,
516 				O2_SD_LOCK_WP, &scratch);
517 		if (ret)
518 			return ret;
519 		scratch |= 0x80;
520 		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
521 		break;
522 	case PCI_DEVICE_ID_O2_SDS0:
523 	case PCI_DEVICE_ID_O2_SDS1:
524 	case PCI_DEVICE_ID_O2_FUJIN2:
525 		/* UnLock WP */
526 		ret = pci_read_config_byte(chip->pdev,
527 				O2_SD_LOCK_WP, &scratch);
528 		if (ret)
529 			return ret;
530 
531 		scratch &= 0x7f;
532 		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
533 
534 		/* DevId=8520 subId= 0x11 or 0x12  Type Chip support */
535 		if (chip->pdev->device == PCI_DEVICE_ID_O2_FUJIN2) {
536 			ret = pci_read_config_dword(chip->pdev,
537 						    O2_SD_FUNC_REG0,
538 						    &scratch_32);
539 			scratch_32 = ((scratch_32 & 0xFF000000) >> 24);
540 
541 			/* Check Whether subId is 0x11 or 0x12 */
542 			if ((scratch_32 == 0x11) || (scratch_32 == 0x12)) {
543 				scratch_32 = 0x25100000;
544 
545 				o2_pci_set_baseclk(chip, scratch_32);
546 				ret = pci_read_config_dword(chip->pdev,
547 							    O2_SD_FUNC_REG4,
548 							    &scratch_32);
549 
550 				/* Enable Base Clk setting change */
551 				scratch_32 |= O2_SD_FREG4_ENABLE_CLK_SET;
552 				pci_write_config_dword(chip->pdev,
553 						       O2_SD_FUNC_REG4,
554 						       scratch_32);
555 
556 				/* Set Tuning Window to 4 */
557 				pci_write_config_byte(chip->pdev,
558 						      O2_SD_TUNING_CTRL, 0x44);
559 
560 				break;
561 			}
562 		}
563 
564 		/* Enable 8520 led function */
565 		o2_pci_led_enable(chip);
566 
567 		/* Set timeout CLK */
568 		ret = pci_read_config_dword(chip->pdev,
569 					    O2_SD_CLK_SETTING, &scratch_32);
570 		if (ret)
571 			return ret;
572 
573 		scratch_32 &= ~(0xFF00);
574 		scratch_32 |= 0x07E0C800;
575 		pci_write_config_dword(chip->pdev,
576 				       O2_SD_CLK_SETTING, scratch_32);
577 
578 		ret = pci_read_config_dword(chip->pdev,
579 					    O2_SD_CLKREQ, &scratch_32);
580 		if (ret)
581 			return ret;
582 		scratch_32 |= 0x3;
583 		pci_write_config_dword(chip->pdev, O2_SD_CLKREQ, scratch_32);
584 
585 		ret = pci_read_config_dword(chip->pdev,
586 					    O2_SD_PLL_SETTING, &scratch_32);
587 		if (ret)
588 			return ret;
589 
590 		scratch_32 &= ~(0x1F3F070E);
591 		scratch_32 |= 0x18270106;
592 		pci_write_config_dword(chip->pdev,
593 				       O2_SD_PLL_SETTING, scratch_32);
594 
595 		/* Disable UHS1 funciton */
596 		ret = pci_read_config_dword(chip->pdev,
597 					    O2_SD_CAP_REG2, &scratch_32);
598 		if (ret)
599 			return ret;
600 		scratch_32 &= ~(0xE0);
601 		pci_write_config_dword(chip->pdev,
602 				       O2_SD_CAP_REG2, scratch_32);
603 
604 		if (chip->pdev->device == PCI_DEVICE_ID_O2_FUJIN2)
605 			sdhci_pci_o2_fujin2_pci_init(chip);
606 
607 		/* Lock WP */
608 		ret = pci_read_config_byte(chip->pdev,
609 					   O2_SD_LOCK_WP, &scratch);
610 		if (ret)
611 			return ret;
612 		scratch |= 0x80;
613 		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
614 		break;
615 	case PCI_DEVICE_ID_O2_SEABIRD0:
616 	case PCI_DEVICE_ID_O2_SEABIRD1:
617 		/* UnLock WP */
618 		ret = pci_read_config_byte(chip->pdev,
619 				O2_SD_LOCK_WP, &scratch);
620 		if (ret)
621 			return ret;
622 
623 		scratch &= 0x7f;
624 		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
625 
626 		ret = pci_read_config_dword(chip->pdev,
627 					    O2_SD_PLL_SETTING, &scratch_32);
628 
629 		if ((scratch_32 & 0xff000000) == 0x01000000) {
630 			scratch_32 &= 0x0000FFFF;
631 			scratch_32 |= 0x1F340000;
632 
633 			pci_write_config_dword(chip->pdev,
634 					       O2_SD_PLL_SETTING, scratch_32);
635 		} else {
636 			scratch_32 &= 0x0000FFFF;
637 			scratch_32 |= 0x25100000;
638 
639 			pci_write_config_dword(chip->pdev,
640 					       O2_SD_PLL_SETTING, scratch_32);
641 
642 			ret = pci_read_config_dword(chip->pdev,
643 						    O2_SD_FUNC_REG4,
644 						    &scratch_32);
645 			scratch_32 |= (1 << 22);
646 			pci_write_config_dword(chip->pdev,
647 					       O2_SD_FUNC_REG4, scratch_32);
648 		}
649 
650 		/* Set Tuning Windows to 5 */
651 		pci_write_config_byte(chip->pdev,
652 				O2_SD_TUNING_CTRL, 0x55);
653 		/* Lock WP */
654 		ret = pci_read_config_byte(chip->pdev,
655 					   O2_SD_LOCK_WP, &scratch);
656 		if (ret)
657 			return ret;
658 		scratch |= 0x80;
659 		pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
660 		break;
661 	}
662 
663 	return 0;
664 }
665 
666 #ifdef CONFIG_PM_SLEEP
667 int sdhci_pci_o2_resume(struct sdhci_pci_chip *chip)
668 {
669 	sdhci_pci_o2_probe(chip);
670 	return sdhci_pci_resume_host(chip);
671 }
672 #endif
673 
674 static const struct sdhci_ops sdhci_pci_o2_ops = {
675 	.set_clock = sdhci_pci_o2_set_clock,
676 	.enable_dma = sdhci_pci_enable_dma,
677 	.set_bus_width = sdhci_set_bus_width,
678 	.reset = sdhci_reset,
679 	.set_uhs_signaling = sdhci_set_uhs_signaling,
680 };
681 
682 const struct sdhci_pci_fixes sdhci_o2 = {
683 	.probe = sdhci_pci_o2_probe,
684 	.quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
685 	.probe_slot = sdhci_pci_o2_probe_slot,
686 #ifdef CONFIG_PM_SLEEP
687 	.resume = sdhci_pci_o2_resume,
688 #endif
689 	.ops = &sdhci_pci_o2_ops,
690 };
691