2 * Copyright (C) 2013 BayHub Technology Ltd.
4 * Authors: Peter Guo <peter.guo@bayhubtech.com>
5 * Adam Lee <adam.lee@canonical.com>
6 * Ernest Zhang <ernest.zhang@bayhubtech.com>
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 #include <linux/pci.h>
20 #include <linux/mmc/host.h>
21 #include <linux/mmc/mmc.h>
22 #include <linux/delay.h>
25 #include "sdhci-pci.h"
28 * O2Micro device registers
31 #define O2_SD_MISC_REG5 0x64
32 #define O2_SD_LD0_CTRL 0x68
33 #define O2_SD_DEV_CTRL 0x88
34 #define O2_SD_LOCK_WP 0xD3
35 #define O2_SD_TEST_REG 0xD4
36 #define O2_SD_FUNC_REG0 0xDC
37 #define O2_SD_MULTI_VCC3V 0xEE
38 #define O2_SD_CLKREQ 0xEC
39 #define O2_SD_CAPS 0xE0
40 #define O2_SD_ADMA1 0xE2
41 #define O2_SD_ADMA2 0xE7
42 #define O2_SD_INF_MOD 0xF1
43 #define O2_SD_MISC_CTRL4 0xFC
44 #define O2_SD_TUNING_CTRL 0x300
45 #define O2_SD_PLL_SETTING 0x304
46 #define O2_SD_MISC_SETTING 0x308
47 #define O2_SD_CLK_SETTING 0x328
48 #define O2_SD_CAP_REG2 0x330
49 #define O2_SD_CAP_REG0 0x334
50 #define O2_SD_UHS1_CAP_SETTING 0x33C
51 #define O2_SD_DELAY_CTRL 0x350
52 #define O2_SD_UHS2_L1_CTRL 0x35C
53 #define O2_SD_FUNC_REG3 0x3E0
54 #define O2_SD_FUNC_REG4 0x3E4
55 #define O2_SD_LED_ENABLE BIT(6)
56 #define O2_SD_FREG0_LEDOFF BIT(13)
57 #define O2_SD_FREG4_ENABLE_CLK_SET BIT(22)
59 #define O2_SD_VENDOR_SETTING 0x110
60 #define O2_SD_VENDOR_SETTING2 0x1C8
61 #define O2_SD_HW_TUNING_DISABLE BIT(4)
63 static void sdhci_o2_set_tuning_mode(struct sdhci_host
*host
)
67 /* enable hardware tuning */
68 reg
= sdhci_readw(host
, O2_SD_VENDOR_SETTING
);
69 reg
&= ~O2_SD_HW_TUNING_DISABLE
;
70 sdhci_writew(host
, reg
, O2_SD_VENDOR_SETTING
);
73 static void __sdhci_o2_execute_tuning(struct sdhci_host
*host
, u32 opcode
)
77 sdhci_send_tuning(host
, MMC_SEND_TUNING_BLOCK_HS200
);
79 for (i
= 0; i
< 150; i
++) {
80 u16 ctrl
= sdhci_readw(host
, SDHCI_HOST_CONTROL2
);
82 if (!(ctrl
& SDHCI_CTRL_EXEC_TUNING
)) {
83 if (ctrl
& SDHCI_CTRL_TUNED_CLK
) {
84 host
->tuning_done
= true;
87 pr_warn("%s: HW tuning failed !\n",
88 mmc_hostname(host
->mmc
));
95 pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
96 mmc_hostname(host
->mmc
));
97 sdhci_reset_tuning(host
);
100 static int sdhci_o2_execute_tuning(struct mmc_host
*mmc
, u32 opcode
)
102 struct sdhci_host
*host
= mmc_priv(mmc
);
103 int current_bus_width
= 0;
106 * This handler only implements the eMMC tuning that is specific to
107 * this controller. Fall back to the standard method for other TIMING.
109 if (host
->timing
!= MMC_TIMING_MMC_HS200
)
110 return sdhci_execute_tuning(mmc
, opcode
);
112 if (WARN_ON(opcode
!= MMC_SEND_TUNING_BLOCK_HS200
))
116 * o2 sdhci host didn't support 8bit emmc tuning
118 if (mmc
->ios
.bus_width
== MMC_BUS_WIDTH_8
) {
119 current_bus_width
= mmc
->ios
.bus_width
;
120 mmc
->ios
.bus_width
= MMC_BUS_WIDTH_4
;
121 sdhci_set_bus_width(host
, MMC_BUS_WIDTH_4
);
124 sdhci_o2_set_tuning_mode(host
);
126 sdhci_start_tuning(host
);
128 __sdhci_o2_execute_tuning(host
, opcode
);
130 sdhci_end_tuning(host
);
132 if (current_bus_width
== MMC_BUS_WIDTH_8
) {
133 mmc
->ios
.bus_width
= MMC_BUS_WIDTH_8
;
134 sdhci_set_bus_width(host
, current_bus_width
);
137 host
->flags
&= ~SDHCI_HS400_TUNING
;
141 static void o2_pci_set_baseclk(struct sdhci_pci_chip
*chip
, u32 value
)
144 pci_read_config_dword(chip
->pdev
,
145 O2_SD_PLL_SETTING
, &scratch_32
);
147 scratch_32
&= 0x0000FFFF;
150 pci_write_config_dword(chip
->pdev
,
151 O2_SD_PLL_SETTING
, scratch_32
);
154 static void o2_pci_led_enable(struct sdhci_pci_chip
*chip
)
159 /* Set led of SD host function enable */
160 ret
= pci_read_config_dword(chip
->pdev
,
161 O2_SD_FUNC_REG0
, &scratch_32
);
165 scratch_32
&= ~O2_SD_FREG0_LEDOFF
;
166 pci_write_config_dword(chip
->pdev
,
167 O2_SD_FUNC_REG0
, scratch_32
);
169 ret
= pci_read_config_dword(chip
->pdev
,
170 O2_SD_TEST_REG
, &scratch_32
);
174 scratch_32
|= O2_SD_LED_ENABLE
;
175 pci_write_config_dword(chip
->pdev
,
176 O2_SD_TEST_REG
, scratch_32
);
180 static void sdhci_pci_o2_fujin2_pci_init(struct sdhci_pci_chip
*chip
)
184 /* Improve write performance for SD3.0 */
185 ret
= pci_read_config_dword(chip
->pdev
, O2_SD_DEV_CTRL
, &scratch_32
);
188 scratch_32
&= ~((1 << 12) | (1 << 13) | (1 << 14));
189 pci_write_config_dword(chip
->pdev
, O2_SD_DEV_CTRL
, scratch_32
);
191 /* Enable Link abnormal reset generating Reset */
192 ret
= pci_read_config_dword(chip
->pdev
, O2_SD_MISC_REG5
, &scratch_32
);
195 scratch_32
&= ~((1 << 19) | (1 << 11));
196 scratch_32
|= (1 << 10);
197 pci_write_config_dword(chip
->pdev
, O2_SD_MISC_REG5
, scratch_32
);
199 /* set card power over current protection */
200 ret
= pci_read_config_dword(chip
->pdev
, O2_SD_TEST_REG
, &scratch_32
);
203 scratch_32
|= (1 << 4);
204 pci_write_config_dword(chip
->pdev
, O2_SD_TEST_REG
, scratch_32
);
206 /* adjust the output delay for SD mode */
207 pci_write_config_dword(chip
->pdev
, O2_SD_DELAY_CTRL
, 0x00002492);
209 /* Set the output voltage setting of Aux 1.2v LDO */
210 ret
= pci_read_config_dword(chip
->pdev
, O2_SD_LD0_CTRL
, &scratch_32
);
213 scratch_32
&= ~(3 << 12);
214 pci_write_config_dword(chip
->pdev
, O2_SD_LD0_CTRL
, scratch_32
);
216 /* Set Max power supply capability of SD host */
217 ret
= pci_read_config_dword(chip
->pdev
, O2_SD_CAP_REG0
, &scratch_32
);
220 scratch_32
&= ~(0x01FE);
221 scratch_32
|= 0x00CC;
222 pci_write_config_dword(chip
->pdev
, O2_SD_CAP_REG0
, scratch_32
);
223 /* Set DLL Tuning Window */
224 ret
= pci_read_config_dword(chip
->pdev
,
225 O2_SD_TUNING_CTRL
, &scratch_32
);
228 scratch_32
&= ~(0x000000FF);
229 scratch_32
|= 0x00000066;
230 pci_write_config_dword(chip
->pdev
, O2_SD_TUNING_CTRL
, scratch_32
);
232 /* Set UHS2 T_EIDLE */
233 ret
= pci_read_config_dword(chip
->pdev
,
234 O2_SD_UHS2_L1_CTRL
, &scratch_32
);
237 scratch_32
&= ~(0x000000FC);
238 scratch_32
|= 0x00000084;
239 pci_write_config_dword(chip
->pdev
, O2_SD_UHS2_L1_CTRL
, scratch_32
);
241 /* Set UHS2 Termination */
242 ret
= pci_read_config_dword(chip
->pdev
, O2_SD_FUNC_REG3
, &scratch_32
);
245 scratch_32
&= ~((1 << 21) | (1 << 30));
247 pci_write_config_dword(chip
->pdev
, O2_SD_FUNC_REG3
, scratch_32
);
249 /* Set L1 Entrance Timer */
250 ret
= pci_read_config_dword(chip
->pdev
, O2_SD_CAPS
, &scratch_32
);
253 scratch_32
&= ~(0xf0000000);
254 scratch_32
|= 0x30000000;
255 pci_write_config_dword(chip
->pdev
, O2_SD_CAPS
, scratch_32
);
257 ret
= pci_read_config_dword(chip
->pdev
,
258 O2_SD_MISC_CTRL4
, &scratch_32
);
261 scratch_32
&= ~(0x000f0000);
262 scratch_32
|= 0x00080000;
263 pci_write_config_dword(chip
->pdev
, O2_SD_MISC_CTRL4
, scratch_32
);
266 static void sdhci_pci_o2_enable_msi(struct sdhci_pci_chip
*chip
,
267 struct sdhci_host
*host
)
271 ret
= pci_find_capability(chip
->pdev
, PCI_CAP_ID_MSI
);
273 pr_info("%s: unsupport msi, use INTx irq\n",
274 mmc_hostname(host
->mmc
));
278 ret
= pci_alloc_irq_vectors(chip
->pdev
, 1, 1,
279 PCI_IRQ_MSI
| PCI_IRQ_MSIX
);
281 pr_err("%s: enable PCI MSI failed, err=%d\n",
282 mmc_hostname(host
->mmc
), ret
);
286 host
->irq
= pci_irq_vector(chip
->pdev
, 0);
289 int sdhci_pci_o2_probe_slot(struct sdhci_pci_slot
*slot
)
291 struct sdhci_pci_chip
*chip
;
292 struct sdhci_host
*host
;
299 caps
= sdhci_readl(host
, SDHCI_CAPABILITIES
);
302 * mmc_select_bus_width() will test the bus to determine the actual bus
305 if (caps
& SDHCI_CAN_DO_8BIT
)
306 host
->mmc
->caps
|= MMC_CAP_8_BIT_DATA
;
308 switch (chip
->pdev
->device
) {
309 case PCI_DEVICE_ID_O2_SDS0
:
310 case PCI_DEVICE_ID_O2_SEABIRD0
:
311 case PCI_DEVICE_ID_O2_SEABIRD1
:
312 case PCI_DEVICE_ID_O2_SDS1
:
313 case PCI_DEVICE_ID_O2_FUJIN2
:
314 reg
= sdhci_readl(host
, O2_SD_VENDOR_SETTING
);
316 host
->quirks
|= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12
;
318 sdhci_pci_o2_enable_msi(chip
, host
);
320 if (chip
->pdev
->device
== PCI_DEVICE_ID_O2_SEABIRD0
) {
321 ret
= pci_read_config_dword(chip
->pdev
,
322 O2_SD_MISC_SETTING
, ®
);
325 if (reg
& (1 << 4)) {
326 pr_info("%s: emmc 1.8v flag is set, force 1.8v signaling voltage\n",
327 mmc_hostname(host
->mmc
));
328 host
->flags
&= ~SDHCI_SIGNALING_330
;
329 host
->flags
|= SDHCI_SIGNALING_180
;
330 host
->mmc
->caps2
|= MMC_CAP2_NO_SD
;
331 host
->mmc
->caps2
|= MMC_CAP2_NO_SDIO
;
335 host
->mmc_host_ops
.execute_tuning
= sdhci_o2_execute_tuning
;
337 if (chip
->pdev
->device
!= PCI_DEVICE_ID_O2_FUJIN2
)
339 /* set dll watch dog timer */
340 reg
= sdhci_readl(host
, O2_SD_VENDOR_SETTING2
);
342 sdhci_writel(host
, reg
, O2_SD_VENDOR_SETTING2
);
352 int sdhci_pci_o2_probe(struct sdhci_pci_chip
*chip
)
358 switch (chip
->pdev
->device
) {
359 case PCI_DEVICE_ID_O2_8220
:
360 case PCI_DEVICE_ID_O2_8221
:
361 case PCI_DEVICE_ID_O2_8320
:
362 case PCI_DEVICE_ID_O2_8321
:
363 /* This extra setup is required due to broken ADMA. */
364 ret
= pci_read_config_byte(chip
->pdev
,
365 O2_SD_LOCK_WP
, &scratch
);
369 pci_write_config_byte(chip
->pdev
, O2_SD_LOCK_WP
, scratch
);
371 /* Set Multi 3 to VCC3V# */
372 pci_write_config_byte(chip
->pdev
, O2_SD_MULTI_VCC3V
, 0x08);
374 /* Disable CLK_REQ# support after media DET */
375 ret
= pci_read_config_byte(chip
->pdev
,
376 O2_SD_CLKREQ
, &scratch
);
380 pci_write_config_byte(chip
->pdev
, O2_SD_CLKREQ
, scratch
);
382 /* Choose capabilities, enable SDMA. We have to write 0x01
383 * to the capabilities register first to unlock it.
385 ret
= pci_read_config_byte(chip
->pdev
, O2_SD_CAPS
, &scratch
);
389 pci_write_config_byte(chip
->pdev
, O2_SD_CAPS
, scratch
);
390 pci_write_config_byte(chip
->pdev
, O2_SD_CAPS
, 0x73);
392 /* Disable ADMA1/2 */
393 pci_write_config_byte(chip
->pdev
, O2_SD_ADMA1
, 0x39);
394 pci_write_config_byte(chip
->pdev
, O2_SD_ADMA2
, 0x08);
396 /* Disable the infinite transfer mode */
397 ret
= pci_read_config_byte(chip
->pdev
,
398 O2_SD_INF_MOD
, &scratch
);
402 pci_write_config_byte(chip
->pdev
, O2_SD_INF_MOD
, scratch
);
405 ret
= pci_read_config_byte(chip
->pdev
,
406 O2_SD_LOCK_WP
, &scratch
);
410 pci_write_config_byte(chip
->pdev
, O2_SD_LOCK_WP
, scratch
);
412 case PCI_DEVICE_ID_O2_SDS0
:
413 case PCI_DEVICE_ID_O2_SDS1
:
414 case PCI_DEVICE_ID_O2_FUJIN2
:
416 ret
= pci_read_config_byte(chip
->pdev
,
417 O2_SD_LOCK_WP
, &scratch
);
422 pci_write_config_byte(chip
->pdev
, O2_SD_LOCK_WP
, scratch
);
424 /* DevId=8520 subId= 0x11 or 0x12 Type Chip support */
425 if (chip
->pdev
->device
== PCI_DEVICE_ID_O2_FUJIN2
) {
426 ret
= pci_read_config_dword(chip
->pdev
,
429 scratch_32
= ((scratch_32
& 0xFF000000) >> 24);
431 /* Check Whether subId is 0x11 or 0x12 */
432 if ((scratch_32
== 0x11) || (scratch_32
== 0x12)) {
433 scratch_32
= 0x25100000;
435 o2_pci_set_baseclk(chip
, scratch_32
);
436 ret
= pci_read_config_dword(chip
->pdev
,
440 /* Enable Base Clk setting change */
441 scratch_32
|= O2_SD_FREG4_ENABLE_CLK_SET
;
442 pci_write_config_dword(chip
->pdev
,
446 /* Set Tuning Window to 4 */
447 pci_write_config_byte(chip
->pdev
,
448 O2_SD_TUNING_CTRL
, 0x44);
454 /* Enable 8520 led function */
455 o2_pci_led_enable(chip
);
457 /* Set timeout CLK */
458 ret
= pci_read_config_dword(chip
->pdev
,
459 O2_SD_CLK_SETTING
, &scratch_32
);
463 scratch_32
&= ~(0xFF00);
464 scratch_32
|= 0x07E0C800;
465 pci_write_config_dword(chip
->pdev
,
466 O2_SD_CLK_SETTING
, scratch_32
);
468 ret
= pci_read_config_dword(chip
->pdev
,
469 O2_SD_CLKREQ
, &scratch_32
);
473 pci_write_config_dword(chip
->pdev
, O2_SD_CLKREQ
, scratch_32
);
475 ret
= pci_read_config_dword(chip
->pdev
,
476 O2_SD_PLL_SETTING
, &scratch_32
);
480 scratch_32
&= ~(0x1F3F070E);
481 scratch_32
|= 0x18270106;
482 pci_write_config_dword(chip
->pdev
,
483 O2_SD_PLL_SETTING
, scratch_32
);
485 /* Disable UHS1 funciton */
486 ret
= pci_read_config_dword(chip
->pdev
,
487 O2_SD_CAP_REG2
, &scratch_32
);
490 scratch_32
&= ~(0xE0);
491 pci_write_config_dword(chip
->pdev
,
492 O2_SD_CAP_REG2
, scratch_32
);
494 if (chip
->pdev
->device
== PCI_DEVICE_ID_O2_FUJIN2
)
495 sdhci_pci_o2_fujin2_pci_init(chip
);
498 ret
= pci_read_config_byte(chip
->pdev
,
499 O2_SD_LOCK_WP
, &scratch
);
503 pci_write_config_byte(chip
->pdev
, O2_SD_LOCK_WP
, scratch
);
505 case PCI_DEVICE_ID_O2_SEABIRD0
:
506 if (chip
->pdev
->revision
== 0x01)
507 chip
->quirks
|= SDHCI_QUIRK_DELAY_AFTER_POWER
;
509 case PCI_DEVICE_ID_O2_SEABIRD1
:
511 ret
= pci_read_config_byte(chip
->pdev
,
512 O2_SD_LOCK_WP
, &scratch
);
517 pci_write_config_byte(chip
->pdev
, O2_SD_LOCK_WP
, scratch
);
519 ret
= pci_read_config_dword(chip
->pdev
,
520 O2_SD_PLL_SETTING
, &scratch_32
);
522 if ((scratch_32
& 0xff000000) == 0x01000000) {
523 scratch_32
&= 0x0000FFFF;
524 scratch_32
|= 0x1F340000;
526 pci_write_config_dword(chip
->pdev
,
527 O2_SD_PLL_SETTING
, scratch_32
);
529 scratch_32
&= 0x0000FFFF;
530 scratch_32
|= 0x25100000;
532 pci_write_config_dword(chip
->pdev
,
533 O2_SD_PLL_SETTING
, scratch_32
);
535 ret
= pci_read_config_dword(chip
->pdev
,
538 scratch_32
|= (1 << 22);
539 pci_write_config_dword(chip
->pdev
,
540 O2_SD_FUNC_REG4
, scratch_32
);
543 /* Set Tuning Windows to 5 */
544 pci_write_config_byte(chip
->pdev
,
545 O2_SD_TUNING_CTRL
, 0x55);
547 ret
= pci_read_config_byte(chip
->pdev
,
548 O2_SD_LOCK_WP
, &scratch
);
552 pci_write_config_byte(chip
->pdev
, O2_SD_LOCK_WP
, scratch
);
559 #ifdef CONFIG_PM_SLEEP
560 int sdhci_pci_o2_resume(struct sdhci_pci_chip
*chip
)
562 sdhci_pci_o2_probe(chip
);
563 return sdhci_pci_resume_host(chip
);