1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 BayHub Technology Ltd.
5 * Authors: Peter Guo <peter.guo@bayhubtech.com>
6 * Adam Lee <adam.lee@canonical.com>
7 * Ernest Zhang <ernest.zhang@bayhubtech.com>
10 #include <linux/pci.h>
11 #include <linux/mmc/host.h>
12 #include <linux/mmc/mmc.h>
13 #include <linux/delay.h>
16 #include "sdhci-pci.h"
19 * O2Micro device registers
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)
50 #define O2_SD_VENDOR_SETTING 0x110
51 #define O2_SD_VENDOR_SETTING2 0x1C8
52 #define O2_SD_HW_TUNING_DISABLE BIT(4)
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)
59 #define O2_SD_DETECT_SETTING 0x324
61 static void sdhci_o2_set_tuning_mode(struct sdhci_host *host)
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);
71 static void __sdhci_o2_execute_tuning(struct sdhci_host *host, u32 opcode)
75 sdhci_send_tuning(host, MMC_SEND_TUNING_BLOCK_HS200);
77 for (i = 0; i < 150; i++) {
78 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
80 if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
81 if (ctrl & SDHCI_CTRL_TUNED_CLK) {
82 host->tuning_done = true;
85 pr_warn("%s: HW tuning failed !\n",
86 mmc_hostname(host->mmc));
93 pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
94 mmc_hostname(host->mmc));
95 sdhci_reset_tuning(host);
98 static int sdhci_o2_execute_tuning(struct mmc_host *mmc, u32 opcode)
100 struct sdhci_host *host = mmc_priv(mmc);
101 int current_bus_width = 0;
104 * This handler only implements the eMMC tuning that is specific to
105 * this controller. Fall back to the standard method for other TIMING.
107 if (host->timing != MMC_TIMING_MMC_HS200)
108 return sdhci_execute_tuning(mmc, opcode);
110 if (WARN_ON(opcode != MMC_SEND_TUNING_BLOCK_HS200))
114 * o2 sdhci host didn't support 8bit emmc tuning
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);
122 sdhci_o2_set_tuning_mode(host);
124 sdhci_start_tuning(host);
126 __sdhci_o2_execute_tuning(host, opcode);
128 sdhci_end_tuning(host);
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);
135 host->flags &= ~SDHCI_HS400_TUNING;
139 static void o2_pci_set_baseclk(struct sdhci_pci_chip *chip, u32 value)
142 pci_read_config_dword(chip->pdev,
143 O2_SD_PLL_SETTING, &scratch_32);
145 scratch_32 &= 0x0000FFFF;
148 pci_write_config_dword(chip->pdev,
149 O2_SD_PLL_SETTING, scratch_32);
152 static void o2_pci_led_enable(struct sdhci_pci_chip *chip)
157 /* Set led of SD host function enable */
158 ret = pci_read_config_dword(chip->pdev,
159 O2_SD_FUNC_REG0, &scratch_32);
163 scratch_32 &= ~O2_SD_FREG0_LEDOFF;
164 pci_write_config_dword(chip->pdev,
165 O2_SD_FUNC_REG0, scratch_32);
167 ret = pci_read_config_dword(chip->pdev,
168 O2_SD_TEST_REG, &scratch_32);
172 scratch_32 |= O2_SD_LED_ENABLE;
173 pci_write_config_dword(chip->pdev,
174 O2_SD_TEST_REG, scratch_32);
178 static void sdhci_pci_o2_fujin2_pci_init(struct sdhci_pci_chip *chip)
182 /* Improve write performance for SD3.0 */
183 ret = pci_read_config_dword(chip->pdev, O2_SD_DEV_CTRL, &scratch_32);
186 scratch_32 &= ~((1 << 12) | (1 << 13) | (1 << 14));
187 pci_write_config_dword(chip->pdev, O2_SD_DEV_CTRL, scratch_32);
189 /* Enable Link abnormal reset generating Reset */
190 ret = pci_read_config_dword(chip->pdev, O2_SD_MISC_REG5, &scratch_32);
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);
197 /* set card power over current protection */
198 ret = pci_read_config_dword(chip->pdev, O2_SD_TEST_REG, &scratch_32);
201 scratch_32 |= (1 << 4);
202 pci_write_config_dword(chip->pdev, O2_SD_TEST_REG, scratch_32);
204 /* adjust the output delay for SD mode */
205 pci_write_config_dword(chip->pdev, O2_SD_DELAY_CTRL, 0x00002492);
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);
211 scratch_32 &= ~(3 << 12);
212 pci_write_config_dword(chip->pdev, O2_SD_LD0_CTRL, scratch_32);
214 /* Set Max power supply capability of SD host */
215 ret = pci_read_config_dword(chip->pdev, O2_SD_CAP_REG0, &scratch_32);
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);
226 scratch_32 &= ~(0x000000FF);
227 scratch_32 |= 0x00000066;
228 pci_write_config_dword(chip->pdev, O2_SD_TUNING_CTRL, scratch_32);
230 /* Set UHS2 T_EIDLE */
231 ret = pci_read_config_dword(chip->pdev,
232 O2_SD_UHS2_L1_CTRL, &scratch_32);
235 scratch_32 &= ~(0x000000FC);
236 scratch_32 |= 0x00000084;
237 pci_write_config_dword(chip->pdev, O2_SD_UHS2_L1_CTRL, scratch_32);
239 /* Set UHS2 Termination */
240 ret = pci_read_config_dword(chip->pdev, O2_SD_FUNC_REG3, &scratch_32);
243 scratch_32 &= ~((1 << 21) | (1 << 30));
245 pci_write_config_dword(chip->pdev, O2_SD_FUNC_REG3, scratch_32);
247 /* Set L1 Entrance Timer */
248 ret = pci_read_config_dword(chip->pdev, O2_SD_CAPS, &scratch_32);
251 scratch_32 &= ~(0xf0000000);
252 scratch_32 |= 0x30000000;
253 pci_write_config_dword(chip->pdev, O2_SD_CAPS, scratch_32);
255 ret = pci_read_config_dword(chip->pdev,
256 O2_SD_MISC_CTRL4, &scratch_32);
259 scratch_32 &= ~(0x000f0000);
260 scratch_32 |= 0x00080000;
261 pci_write_config_dword(chip->pdev, O2_SD_MISC_CTRL4, scratch_32);
264 static void sdhci_pci_o2_enable_msi(struct sdhci_pci_chip *chip,
265 struct sdhci_host *host)
269 ret = pci_find_capability(chip->pdev, PCI_CAP_ID_MSI);
271 pr_info("%s: unsupport msi, use INTx irq\n",
272 mmc_hostname(host->mmc));
276 ret = pci_alloc_irq_vectors(chip->pdev, 1, 1,
277 PCI_IRQ_MSI | PCI_IRQ_MSIX);
279 pr_err("%s: enable PCI MSI failed, err=%d\n",
280 mmc_hostname(host->mmc), ret);
284 host->irq = pci_irq_vector(chip->pdev, 0);
287 static void sdhci_o2_wait_card_detect_stable(struct sdhci_host *host)
293 timeout = ktime_add_ms(ktime_get(), 50);
295 bool timedout = ktime_after(ktime_get(), timeout);
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)
303 pr_err("%s: Card Detect debounce never finished.\n",
304 mmc_hostname(host->mmc));
305 sdhci_dumpregs(host);
312 static void sdhci_o2_enable_internal_clock(struct sdhci_host *host)
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);
323 scratch32 &= ~(O2_PLL_SOFT_RESET);
324 sdhci_writel(host, scratch32, O2_PLL_WDT_CONTROL1);
326 /* PLL force active */
327 scratch32 |= O2_PLL_FORCE_ACTIVE;
328 sdhci_writel(host, scratch32, O2_PLL_WDT_CONTROL1);
331 timeout = ktime_add_ms(ktime_get(), 20);
333 bool timedout = ktime_after(ktime_get(), timeout);
335 scratch = sdhci_readw(host, O2_PLL_WDT_CONTROL1);
336 if (scratch & O2_PLL_LOCK_STATUS)
339 pr_err("%s: Internal clock never stabilised.\n",
340 mmc_hostname(host->mmc));
341 sdhci_dumpregs(host);
347 /* Wait for card detect finish */
349 sdhci_o2_wait_card_detect_stable(host);
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);
358 static int sdhci_o2_get_cd(struct mmc_host *mmc)
360 struct sdhci_host *host = mmc_priv(mmc);
362 sdhci_o2_enable_internal_clock(host);
364 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
367 static void sdhci_o2_enable_clk(struct sdhci_host *host, u16 clk)
369 /* Enable internal clock */
370 clk |= SDHCI_CLOCK_INT_EN;
371 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
373 if (sdhci_o2_get_cd(host->mmc)) {
374 clk |= SDHCI_CLOCK_CARD_EN;
375 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
379 void sdhci_pci_o2_set_clock(struct sdhci_host *host, unsigned int clock)
383 host->mmc->actual_clock = 0;
385 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
390 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
391 sdhci_o2_enable_clk(host, clk);
394 int sdhci_pci_o2_probe_slot(struct sdhci_pci_slot *slot)
396 struct sdhci_pci_chip *chip;
397 struct sdhci_host *host;
404 caps = sdhci_readl(host, SDHCI_CAPABILITIES);
407 * mmc_select_bus_width() will test the bus to determine the actual bus
410 if (caps & SDHCI_CAN_DO_8BIT)
411 host->mmc->caps |= MMC_CAP_8_BIT_DATA;
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);
421 host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
423 sdhci_pci_o2_enable_msi(chip, host);
425 if (chip->pdev->device == PCI_DEVICE_ID_O2_SEABIRD0) {
426 ret = pci_read_config_dword(chip->pdev,
427 O2_SD_MISC_SETTING, ®);
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);
442 slot->host->mmc_host_ops.get_cd = sdhci_o2_get_cd;
445 host->mmc_host_ops.execute_tuning = sdhci_o2_execute_tuning;
447 if (chip->pdev->device != PCI_DEVICE_ID_O2_FUJIN2)
449 /* set dll watch dog timer */
450 reg = sdhci_readl(host, O2_SD_VENDOR_SETTING2);
452 sdhci_writel(host, reg, O2_SD_VENDOR_SETTING2);
462 int sdhci_pci_o2_probe(struct sdhci_pci_chip *chip)
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);
479 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
481 /* Set Multi 3 to VCC3V# */
482 pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
484 /* Disable CLK_REQ# support after media DET */
485 ret = pci_read_config_byte(chip->pdev,
486 O2_SD_CLKREQ, &scratch);
490 pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
492 /* Choose capabilities, enable SDMA. We have to write 0x01
493 * to the capabilities register first to unlock it.
495 ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
499 pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
500 pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
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);
506 /* Disable the infinite transfer mode */
507 ret = pci_read_config_byte(chip->pdev,
508 O2_SD_INF_MOD, &scratch);
512 pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
515 ret = pci_read_config_byte(chip->pdev,
516 O2_SD_LOCK_WP, &scratch);
520 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
522 case PCI_DEVICE_ID_O2_SDS0:
523 case PCI_DEVICE_ID_O2_SDS1:
524 case PCI_DEVICE_ID_O2_FUJIN2:
526 ret = pci_read_config_byte(chip->pdev,
527 O2_SD_LOCK_WP, &scratch);
532 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
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,
539 scratch_32 = ((scratch_32 & 0xFF000000) >> 24);
541 /* Check Whether subId is 0x11 or 0x12 */
542 if ((scratch_32 == 0x11) || (scratch_32 == 0x12)) {
543 scratch_32 = 0x25100000;
545 o2_pci_set_baseclk(chip, scratch_32);
546 ret = pci_read_config_dword(chip->pdev,
550 /* Enable Base Clk setting change */
551 scratch_32 |= O2_SD_FREG4_ENABLE_CLK_SET;
552 pci_write_config_dword(chip->pdev,
556 /* Set Tuning Window to 4 */
557 pci_write_config_byte(chip->pdev,
558 O2_SD_TUNING_CTRL, 0x44);
564 /* Enable 8520 led function */
565 o2_pci_led_enable(chip);
567 /* Set timeout CLK */
568 ret = pci_read_config_dword(chip->pdev,
569 O2_SD_CLK_SETTING, &scratch_32);
573 scratch_32 &= ~(0xFF00);
574 scratch_32 |= 0x07E0C800;
575 pci_write_config_dword(chip->pdev,
576 O2_SD_CLK_SETTING, scratch_32);
578 ret = pci_read_config_dword(chip->pdev,
579 O2_SD_CLKREQ, &scratch_32);
583 pci_write_config_dword(chip->pdev, O2_SD_CLKREQ, scratch_32);
585 ret = pci_read_config_dword(chip->pdev,
586 O2_SD_PLL_SETTING, &scratch_32);
590 scratch_32 &= ~(0x1F3F070E);
591 scratch_32 |= 0x18270106;
592 pci_write_config_dword(chip->pdev,
593 O2_SD_PLL_SETTING, scratch_32);
595 /* Disable UHS1 funciton */
596 ret = pci_read_config_dword(chip->pdev,
597 O2_SD_CAP_REG2, &scratch_32);
600 scratch_32 &= ~(0xE0);
601 pci_write_config_dword(chip->pdev,
602 O2_SD_CAP_REG2, scratch_32);
604 if (chip->pdev->device == PCI_DEVICE_ID_O2_FUJIN2)
605 sdhci_pci_o2_fujin2_pci_init(chip);
608 ret = pci_read_config_byte(chip->pdev,
609 O2_SD_LOCK_WP, &scratch);
613 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
615 case PCI_DEVICE_ID_O2_SEABIRD0:
616 case PCI_DEVICE_ID_O2_SEABIRD1:
618 ret = pci_read_config_byte(chip->pdev,
619 O2_SD_LOCK_WP, &scratch);
624 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
626 ret = pci_read_config_dword(chip->pdev,
627 O2_SD_PLL_SETTING, &scratch_32);
629 if ((scratch_32 & 0xff000000) == 0x01000000) {
630 scratch_32 &= 0x0000FFFF;
631 scratch_32 |= 0x1F340000;
633 pci_write_config_dword(chip->pdev,
634 O2_SD_PLL_SETTING, scratch_32);
636 scratch_32 &= 0x0000FFFF;
637 scratch_32 |= 0x25100000;
639 pci_write_config_dword(chip->pdev,
640 O2_SD_PLL_SETTING, scratch_32);
642 ret = pci_read_config_dword(chip->pdev,
645 scratch_32 |= (1 << 22);
646 pci_write_config_dword(chip->pdev,
647 O2_SD_FUNC_REG4, scratch_32);
650 /* Set Tuning Windows to 5 */
651 pci_write_config_byte(chip->pdev,
652 O2_SD_TUNING_CTRL, 0x55);
654 ret = pci_read_config_byte(chip->pdev,
655 O2_SD_LOCK_WP, &scratch);
659 pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
666 #ifdef CONFIG_PM_SLEEP
667 int sdhci_pci_o2_resume(struct sdhci_pci_chip *chip)
669 sdhci_pci_o2_probe(chip);
670 return sdhci_pci_resume_host(chip);
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,
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,
689 .ops = &sdhci_pci_o2_ops,