sdhci-pci-o2micro.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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. #include <linux/pci.h>
  10. #include <linux/mmc/host.h>
  11. #include <linux/mmc/mmc.h>
  12. #include <linux/delay.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/bitfield.h>
  15. #include "sdhci.h"
  16. #include "sdhci-pci.h"
  17. /*
  18. * O2Micro device registers
  19. */
  20. #define O2_SD_PCIE_SWITCH 0x54
  21. #define O2_SD_MISC_REG5 0x64
  22. #define O2_SD_LD0_CTRL 0x68
  23. #define O2_SD_DEV_CTRL 0x88
  24. #define O2_SD_LOCK_WP 0xD3
  25. #define O2_SD_TEST_REG 0xD4
  26. #define O2_SD_FUNC_REG0 0xDC
  27. #define O2_SD_MULTI_VCC3V 0xEE
  28. #define O2_SD_CLKREQ 0xEC
  29. #define O2_SD_CAPS 0xE0
  30. #define O2_SD_ADMA1 0xE2
  31. #define O2_SD_ADMA2 0xE7
  32. #define O2_SD_MISC_CTRL2 0xF0
  33. #define O2_SD_INF_MOD 0xF1
  34. #define O2_SD_MISC_CTRL4 0xFC
  35. #define O2_SD_MISC_CTRL 0x1C0
  36. #define O2_SD_EXP_INT_REG 0x1E0
  37. #define O2_SD_PWR_FORCE_L0 0x0002
  38. #define O2_SD_TUNING_CTRL 0x300
  39. #define O2_SD_PLL_SETTING 0x304
  40. #define O2_SD_MISC_SETTING 0x308
  41. #define O2_SD_CLK_SETTING 0x328
  42. #define O2_SD_CAP_REG2 0x330
  43. #define O2_SD_CAP_REG0 0x334
  44. #define O2_SD_UHS1_CAP_SETTING 0x33C
  45. #define O2_SD_DELAY_CTRL 0x350
  46. #define O2_SD_OUTPUT_CLK_SOURCE_SWITCH 0x354
  47. #define O2_SD_UHS2_L1_CTRL 0x35C
  48. #define O2_SD_FUNC_REG3 0x3E0
  49. #define O2_SD_FUNC_REG4 0x3E4
  50. #define O2_SD_PARA_SET_REG1 0x444
  51. #define O2_SD_VDDX_CTRL_REG 0x508
  52. #define O2_SD_GPIO_CTRL_REG1 0x510
  53. #define O2_SD_LED_ENABLE BIT(6)
  54. #define O2_SD_FREG0_LEDOFF BIT(13)
  55. #define O2_SD_SEL_DLL BIT(16)
  56. #define O2_SD_FREG4_ENABLE_CLK_SET BIT(22)
  57. #define O2_SD_PHASE_MASK GENMASK(23, 20)
  58. #define O2_SD_FIX_PHASE FIELD_PREP(O2_SD_PHASE_MASK, 0x9)
  59. #define O2_SD_VENDOR_SETTING 0x110
  60. #define O2_SD_VENDOR_SETTING2 0x1C8
  61. #define O2_SD_HW_TUNING_DISABLE BIT(4)
  62. #define O2_PLL_DLL_WDT_CONTROL1 0x1CC
  63. #define O2_PLL_FORCE_ACTIVE BIT(18)
  64. #define O2_PLL_LOCK_STATUS BIT(14)
  65. #define O2_PLL_SOFT_RESET BIT(12)
  66. #define O2_DLL_LOCK_STATUS BIT(11)
  67. #define O2_SD_DETECT_SETTING 0x324
  68. static const u32 dmdn_table[] = {0x2B1C0000,
  69. 0x2C1A0000, 0x371B0000, 0x35100000};
  70. #define DMDN_SZ ARRAY_SIZE(dmdn_table)
  71. struct o2_host {
  72. u8 dll_adjust_count;
  73. };
  74. static void sdhci_o2_wait_card_detect_stable(struct sdhci_host *host)
  75. {
  76. ktime_t timeout;
  77. u32 scratch32;
  78. /* Wait max 50 ms */
  79. timeout = ktime_add_ms(ktime_get(), 50);
  80. while (1) {
  81. bool timedout = ktime_after(ktime_get(), timeout);
  82. scratch32 = sdhci_readl(host, SDHCI_PRESENT_STATE);
  83. if ((scratch32 & SDHCI_CARD_PRESENT) >> SDHCI_CARD_PRES_SHIFT
  84. == (scratch32 & SDHCI_CD_LVL) >> SDHCI_CD_LVL_SHIFT)
  85. break;
  86. if (timedout) {
  87. pr_err("%s: Card Detect debounce never finished.\n",
  88. mmc_hostname(host->mmc));
  89. sdhci_dumpregs(host);
  90. return;
  91. }
  92. udelay(10);
  93. }
  94. }
  95. static void sdhci_o2_enable_internal_clock(struct sdhci_host *host)
  96. {
  97. ktime_t timeout;
  98. u16 scratch;
  99. u32 scratch32;
  100. /* PLL software reset */
  101. scratch32 = sdhci_readl(host, O2_PLL_DLL_WDT_CONTROL1);
  102. scratch32 |= O2_PLL_SOFT_RESET;
  103. sdhci_writel(host, scratch32, O2_PLL_DLL_WDT_CONTROL1);
  104. udelay(1);
  105. scratch32 &= ~(O2_PLL_SOFT_RESET);
  106. sdhci_writel(host, scratch32, O2_PLL_DLL_WDT_CONTROL1);
  107. /* PLL force active */
  108. scratch32 |= O2_PLL_FORCE_ACTIVE;
  109. sdhci_writel(host, scratch32, O2_PLL_DLL_WDT_CONTROL1);
  110. /* Wait max 20 ms */
  111. timeout = ktime_add_ms(ktime_get(), 20);
  112. while (1) {
  113. bool timedout = ktime_after(ktime_get(), timeout);
  114. scratch = sdhci_readw(host, O2_PLL_DLL_WDT_CONTROL1);
  115. if (scratch & O2_PLL_LOCK_STATUS)
  116. break;
  117. if (timedout) {
  118. pr_err("%s: Internal clock never stabilised.\n",
  119. mmc_hostname(host->mmc));
  120. sdhci_dumpregs(host);
  121. goto out;
  122. }
  123. udelay(10);
  124. }
  125. /* Wait for card detect finish */
  126. udelay(1);
  127. sdhci_o2_wait_card_detect_stable(host);
  128. out:
  129. /* Cancel PLL force active */
  130. scratch32 = sdhci_readl(host, O2_PLL_DLL_WDT_CONTROL1);
  131. scratch32 &= ~O2_PLL_FORCE_ACTIVE;
  132. sdhci_writel(host, scratch32, O2_PLL_DLL_WDT_CONTROL1);
  133. }
  134. static int sdhci_o2_get_cd(struct mmc_host *mmc)
  135. {
  136. struct sdhci_host *host = mmc_priv(mmc);
  137. if (!(sdhci_readw(host, O2_PLL_DLL_WDT_CONTROL1) & O2_PLL_LOCK_STATUS))
  138. sdhci_o2_enable_internal_clock(host);
  139. else
  140. sdhci_o2_wait_card_detect_stable(host);
  141. return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
  142. }
  143. static void o2_pci_set_baseclk(struct sdhci_pci_chip *chip, u32 value)
  144. {
  145. u32 scratch_32;
  146. pci_read_config_dword(chip->pdev,
  147. O2_SD_PLL_SETTING, &scratch_32);
  148. scratch_32 &= 0x0000FFFF;
  149. scratch_32 |= value;
  150. pci_write_config_dword(chip->pdev,
  151. O2_SD_PLL_SETTING, scratch_32);
  152. }
  153. static u32 sdhci_o2_pll_dll_wdt_control(struct sdhci_host *host)
  154. {
  155. return sdhci_readl(host, O2_PLL_DLL_WDT_CONTROL1);
  156. }
  157. /*
  158. * This function is used to detect dll lock status.
  159. * Since the dll lock status bit will toggle randomly
  160. * with very short interval which needs to be polled
  161. * as fast as possible. Set sleep_us as 1 microsecond.
  162. */
  163. static int sdhci_o2_wait_dll_detect_lock(struct sdhci_host *host)
  164. {
  165. u32 scratch32 = 0;
  166. return readx_poll_timeout(sdhci_o2_pll_dll_wdt_control, host,
  167. scratch32, !(scratch32 & O2_DLL_LOCK_STATUS), 1, 1000000);
  168. }
  169. static void sdhci_o2_set_tuning_mode(struct sdhci_host *host)
  170. {
  171. u16 reg;
  172. /* enable hardware tuning */
  173. reg = sdhci_readw(host, O2_SD_VENDOR_SETTING);
  174. reg &= ~O2_SD_HW_TUNING_DISABLE;
  175. sdhci_writew(host, reg, O2_SD_VENDOR_SETTING);
  176. }
  177. static void __sdhci_o2_execute_tuning(struct sdhci_host *host, u32 opcode)
  178. {
  179. int i;
  180. sdhci_send_tuning(host, opcode);
  181. for (i = 0; i < 150; i++) {
  182. u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  183. if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
  184. if (ctrl & SDHCI_CTRL_TUNED_CLK) {
  185. host->tuning_done = true;
  186. return;
  187. }
  188. pr_warn("%s: HW tuning failed !\n",
  189. mmc_hostname(host->mmc));
  190. break;
  191. }
  192. mdelay(1);
  193. }
  194. pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
  195. mmc_hostname(host->mmc));
  196. sdhci_reset_tuning(host);
  197. }
  198. /*
  199. * This function is used to fix o2 dll shift issue.
  200. * It isn't necessary to detect card present before recovery.
  201. * Firstly, it is used by bht emmc card, which is embedded.
  202. * Second, before call recovery card present will be detected
  203. * outside of the execute tuning function.
  204. */
  205. static int sdhci_o2_dll_recovery(struct sdhci_host *host)
  206. {
  207. int ret = 0;
  208. u8 scratch_8 = 0;
  209. u32 scratch_32 = 0;
  210. struct sdhci_pci_slot *slot = sdhci_priv(host);
  211. struct sdhci_pci_chip *chip = slot->chip;
  212. struct o2_host *o2_host = sdhci_pci_priv(slot);
  213. /* UnLock WP */
  214. pci_read_config_byte(chip->pdev,
  215. O2_SD_LOCK_WP, &scratch_8);
  216. scratch_8 &= 0x7f;
  217. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
  218. while (o2_host->dll_adjust_count < DMDN_SZ && !ret) {
  219. /* Disable clock */
  220. sdhci_writeb(host, 0, SDHCI_CLOCK_CONTROL);
  221. /* PLL software reset */
  222. scratch_32 = sdhci_readl(host, O2_PLL_DLL_WDT_CONTROL1);
  223. scratch_32 |= O2_PLL_SOFT_RESET;
  224. sdhci_writel(host, scratch_32, O2_PLL_DLL_WDT_CONTROL1);
  225. pci_read_config_dword(chip->pdev,
  226. O2_SD_FUNC_REG4,
  227. &scratch_32);
  228. /* Enable Base Clk setting change */
  229. scratch_32 |= O2_SD_FREG4_ENABLE_CLK_SET;
  230. pci_write_config_dword(chip->pdev, O2_SD_FUNC_REG4, scratch_32);
  231. o2_pci_set_baseclk(chip, dmdn_table[o2_host->dll_adjust_count]);
  232. /* Enable internal clock */
  233. scratch_8 = SDHCI_CLOCK_INT_EN;
  234. sdhci_writeb(host, scratch_8, SDHCI_CLOCK_CONTROL);
  235. if (sdhci_o2_get_cd(host->mmc)) {
  236. /*
  237. * need wait at least 5ms for dll status stable,
  238. * after enable internal clock
  239. */
  240. usleep_range(5000, 6000);
  241. if (sdhci_o2_wait_dll_detect_lock(host)) {
  242. scratch_8 |= SDHCI_CLOCK_CARD_EN;
  243. sdhci_writeb(host, scratch_8,
  244. SDHCI_CLOCK_CONTROL);
  245. ret = 1;
  246. } else {
  247. pr_warn("%s: DLL unlocked when dll_adjust_count is %d.\n",
  248. mmc_hostname(host->mmc),
  249. o2_host->dll_adjust_count);
  250. }
  251. } else {
  252. pr_err("%s: card present detect failed.\n",
  253. mmc_hostname(host->mmc));
  254. break;
  255. }
  256. o2_host->dll_adjust_count++;
  257. }
  258. if (!ret && o2_host->dll_adjust_count == DMDN_SZ)
  259. pr_err("%s: DLL adjust over max times\n",
  260. mmc_hostname(host->mmc));
  261. /* Lock WP */
  262. pci_read_config_byte(chip->pdev,
  263. O2_SD_LOCK_WP, &scratch_8);
  264. scratch_8 |= 0x80;
  265. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
  266. return ret;
  267. }
  268. static int sdhci_o2_execute_tuning(struct mmc_host *mmc, u32 opcode)
  269. {
  270. struct sdhci_host *host = mmc_priv(mmc);
  271. struct sdhci_pci_slot *slot = sdhci_priv(host);
  272. struct sdhci_pci_chip *chip = slot->chip;
  273. int current_bus_width = 0;
  274. u32 scratch32 = 0;
  275. u16 scratch = 0;
  276. u8 scratch_8 = 0;
  277. u32 reg_val;
  278. /*
  279. * This handler implements the hardware tuning that is specific to
  280. * this controller. Fall back to the standard method for other TIMING.
  281. */
  282. if ((host->timing != MMC_TIMING_MMC_HS200) &&
  283. (host->timing != MMC_TIMING_UHS_SDR104) &&
  284. (host->timing != MMC_TIMING_UHS_SDR50))
  285. return sdhci_execute_tuning(mmc, opcode);
  286. if (WARN_ON(!mmc_op_tuning(opcode)))
  287. return -EINVAL;
  288. /* Force power mode enter L0 */
  289. scratch = sdhci_readw(host, O2_SD_MISC_CTRL);
  290. scratch |= O2_SD_PWR_FORCE_L0;
  291. sdhci_writew(host, scratch, O2_SD_MISC_CTRL);
  292. /* Update output phase */
  293. switch (chip->pdev->device) {
  294. case PCI_DEVICE_ID_O2_SDS0:
  295. case PCI_DEVICE_ID_O2_SEABIRD0:
  296. case PCI_DEVICE_ID_O2_SEABIRD1:
  297. case PCI_DEVICE_ID_O2_SDS1:
  298. case PCI_DEVICE_ID_O2_FUJIN2:
  299. /* Stop clk */
  300. reg_val = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  301. reg_val &= ~SDHCI_CLOCK_CARD_EN;
  302. sdhci_writew(host, reg_val, SDHCI_CLOCK_CONTROL);
  303. if (host->timing == MMC_TIMING_MMC_HS200 ||
  304. host->timing == MMC_TIMING_UHS_SDR104) {
  305. /* UnLock WP */
  306. pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch_8);
  307. scratch_8 &= 0x7f;
  308. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
  309. /* Set pcr 0x354[16] to choose dll clock, and set the default phase */
  310. pci_read_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, &reg_val);
  311. reg_val &= ~(O2_SD_SEL_DLL | O2_SD_PHASE_MASK);
  312. reg_val |= (O2_SD_SEL_DLL | O2_SD_FIX_PHASE);
  313. pci_write_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, reg_val);
  314. /* Lock WP */
  315. pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch_8);
  316. scratch_8 |= 0x80;
  317. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
  318. }
  319. /* Start clk */
  320. reg_val = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  321. reg_val |= SDHCI_CLOCK_CARD_EN;
  322. sdhci_writew(host, reg_val, SDHCI_CLOCK_CONTROL);
  323. break;
  324. default:
  325. break;
  326. }
  327. /* wait DLL lock, timeout value 5ms */
  328. if (readx_poll_timeout(sdhci_o2_pll_dll_wdt_control, host,
  329. scratch32, (scratch32 & O2_DLL_LOCK_STATUS), 1, 5000))
  330. pr_warn("%s: DLL can't lock in 5ms after force L0 during tuning.\n",
  331. mmc_hostname(host->mmc));
  332. /*
  333. * Judge the tuning reason, whether caused by dll shift
  334. * If cause by dll shift, should call sdhci_o2_dll_recovery
  335. */
  336. if (!sdhci_o2_wait_dll_detect_lock(host))
  337. if (!sdhci_o2_dll_recovery(host)) {
  338. pr_err("%s: o2 dll recovery failed\n",
  339. mmc_hostname(host->mmc));
  340. return -EINVAL;
  341. }
  342. /*
  343. * o2 sdhci host didn't support 8bit emmc tuning
  344. */
  345. if (mmc->ios.bus_width == MMC_BUS_WIDTH_8) {
  346. current_bus_width = mmc->ios.bus_width;
  347. mmc->ios.bus_width = MMC_BUS_WIDTH_4;
  348. sdhci_set_bus_width(host, MMC_BUS_WIDTH_4);
  349. }
  350. sdhci_o2_set_tuning_mode(host);
  351. sdhci_start_tuning(host);
  352. __sdhci_o2_execute_tuning(host, opcode);
  353. sdhci_end_tuning(host);
  354. if (current_bus_width == MMC_BUS_WIDTH_8) {
  355. mmc->ios.bus_width = MMC_BUS_WIDTH_8;
  356. sdhci_set_bus_width(host, current_bus_width);
  357. }
  358. /* Cancel force power mode enter L0 */
  359. scratch = sdhci_readw(host, O2_SD_MISC_CTRL);
  360. scratch &= ~(O2_SD_PWR_FORCE_L0);
  361. sdhci_writew(host, scratch, O2_SD_MISC_CTRL);
  362. sdhci_reset(host, SDHCI_RESET_CMD);
  363. sdhci_reset(host, SDHCI_RESET_DATA);
  364. host->flags &= ~SDHCI_HS400_TUNING;
  365. return 0;
  366. }
  367. static void o2_pci_led_enable(struct sdhci_pci_chip *chip)
  368. {
  369. int ret;
  370. u32 scratch_32;
  371. /* Set led of SD host function enable */
  372. ret = pci_read_config_dword(chip->pdev,
  373. O2_SD_FUNC_REG0, &scratch_32);
  374. if (ret)
  375. return;
  376. scratch_32 &= ~O2_SD_FREG0_LEDOFF;
  377. pci_write_config_dword(chip->pdev,
  378. O2_SD_FUNC_REG0, scratch_32);
  379. ret = pci_read_config_dword(chip->pdev,
  380. O2_SD_TEST_REG, &scratch_32);
  381. if (ret)
  382. return;
  383. scratch_32 |= O2_SD_LED_ENABLE;
  384. pci_write_config_dword(chip->pdev,
  385. O2_SD_TEST_REG, scratch_32);
  386. }
  387. static void sdhci_pci_o2_fujin2_pci_init(struct sdhci_pci_chip *chip)
  388. {
  389. u32 scratch_32;
  390. int ret;
  391. /* Improve write performance for SD3.0 */
  392. ret = pci_read_config_dword(chip->pdev, O2_SD_DEV_CTRL, &scratch_32);
  393. if (ret)
  394. return;
  395. scratch_32 &= ~((1 << 12) | (1 << 13) | (1 << 14));
  396. pci_write_config_dword(chip->pdev, O2_SD_DEV_CTRL, scratch_32);
  397. /* Enable Link abnormal reset generating Reset */
  398. ret = pci_read_config_dword(chip->pdev, O2_SD_MISC_REG5, &scratch_32);
  399. if (ret)
  400. return;
  401. scratch_32 &= ~((1 << 19) | (1 << 11));
  402. scratch_32 |= (1 << 10);
  403. pci_write_config_dword(chip->pdev, O2_SD_MISC_REG5, scratch_32);
  404. /* set card power over current protection */
  405. ret = pci_read_config_dword(chip->pdev, O2_SD_TEST_REG, &scratch_32);
  406. if (ret)
  407. return;
  408. scratch_32 |= (1 << 4);
  409. pci_write_config_dword(chip->pdev, O2_SD_TEST_REG, scratch_32);
  410. /* adjust the output delay for SD mode */
  411. pci_write_config_dword(chip->pdev, O2_SD_DELAY_CTRL, 0x00002492);
  412. /* Set the output voltage setting of Aux 1.2v LDO */
  413. ret = pci_read_config_dword(chip->pdev, O2_SD_LD0_CTRL, &scratch_32);
  414. if (ret)
  415. return;
  416. scratch_32 &= ~(3 << 12);
  417. pci_write_config_dword(chip->pdev, O2_SD_LD0_CTRL, scratch_32);
  418. /* Set Max power supply capability of SD host */
  419. ret = pci_read_config_dword(chip->pdev, O2_SD_CAP_REG0, &scratch_32);
  420. if (ret)
  421. return;
  422. scratch_32 &= ~(0x01FE);
  423. scratch_32 |= 0x00CC;
  424. pci_write_config_dword(chip->pdev, O2_SD_CAP_REG0, scratch_32);
  425. /* Set DLL Tuning Window */
  426. ret = pci_read_config_dword(chip->pdev,
  427. O2_SD_TUNING_CTRL, &scratch_32);
  428. if (ret)
  429. return;
  430. scratch_32 &= ~(0x000000FF);
  431. scratch_32 |= 0x00000066;
  432. pci_write_config_dword(chip->pdev, O2_SD_TUNING_CTRL, scratch_32);
  433. /* Set UHS2 T_EIDLE */
  434. ret = pci_read_config_dword(chip->pdev,
  435. O2_SD_UHS2_L1_CTRL, &scratch_32);
  436. if (ret)
  437. return;
  438. scratch_32 &= ~(0x000000FC);
  439. scratch_32 |= 0x00000084;
  440. pci_write_config_dword(chip->pdev, O2_SD_UHS2_L1_CTRL, scratch_32);
  441. /* Set UHS2 Termination */
  442. ret = pci_read_config_dword(chip->pdev, O2_SD_FUNC_REG3, &scratch_32);
  443. if (ret)
  444. return;
  445. scratch_32 &= ~((1 << 21) | (1 << 30));
  446. pci_write_config_dword(chip->pdev, O2_SD_FUNC_REG3, scratch_32);
  447. /* Set L1 Entrance Timer */
  448. ret = pci_read_config_dword(chip->pdev, O2_SD_CAPS, &scratch_32);
  449. if (ret)
  450. return;
  451. scratch_32 &= ~(0xf0000000);
  452. scratch_32 |= 0x30000000;
  453. pci_write_config_dword(chip->pdev, O2_SD_CAPS, scratch_32);
  454. ret = pci_read_config_dword(chip->pdev,
  455. O2_SD_MISC_CTRL4, &scratch_32);
  456. if (ret)
  457. return;
  458. scratch_32 &= ~(0x000f0000);
  459. scratch_32 |= 0x00080000;
  460. pci_write_config_dword(chip->pdev, O2_SD_MISC_CTRL4, scratch_32);
  461. }
  462. static void sdhci_pci_o2_enable_msi(struct sdhci_pci_chip *chip,
  463. struct sdhci_host *host)
  464. {
  465. int ret;
  466. ret = pci_find_capability(chip->pdev, PCI_CAP_ID_MSI);
  467. if (!ret) {
  468. pr_info("%s: unsupported MSI, use INTx irq\n",
  469. mmc_hostname(host->mmc));
  470. return;
  471. }
  472. ret = pci_alloc_irq_vectors(chip->pdev, 1, 1,
  473. PCI_IRQ_MSI | PCI_IRQ_MSIX);
  474. if (ret < 0) {
  475. pr_err("%s: enable PCI MSI failed, err=%d\n",
  476. mmc_hostname(host->mmc), ret);
  477. return;
  478. }
  479. host->irq = pci_irq_vector(chip->pdev, 0);
  480. }
  481. static void sdhci_o2_enable_clk(struct sdhci_host *host, u16 clk)
  482. {
  483. /* Enable internal clock */
  484. clk |= SDHCI_CLOCK_INT_EN;
  485. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  486. sdhci_o2_enable_internal_clock(host);
  487. if (sdhci_o2_get_cd(host->mmc)) {
  488. clk |= SDHCI_CLOCK_CARD_EN;
  489. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  490. }
  491. }
  492. static void sdhci_pci_o2_set_clock(struct sdhci_host *host, unsigned int clock)
  493. {
  494. u16 clk;
  495. u8 scratch;
  496. u32 scratch_32;
  497. u32 dmdn_208m, dmdn_200m;
  498. struct sdhci_pci_slot *slot = sdhci_priv(host);
  499. struct sdhci_pci_chip *chip = slot->chip;
  500. host->mmc->actual_clock = 0;
  501. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  502. if (clock == 0)
  503. return;
  504. /* UnLock WP */
  505. pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
  506. scratch &= 0x7f;
  507. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  508. if (chip->pdev->device == PCI_DEVICE_ID_O2_GG8_9860 ||
  509. chip->pdev->device == PCI_DEVICE_ID_O2_GG8_9861 ||
  510. chip->pdev->device == PCI_DEVICE_ID_O2_GG8_9862 ||
  511. chip->pdev->device == PCI_DEVICE_ID_O2_GG8_9863) {
  512. dmdn_208m = 0x2c500000;
  513. dmdn_200m = 0x25200000;
  514. } else {
  515. dmdn_208m = 0x2c280000;
  516. dmdn_200m = 0x25100000;
  517. }
  518. if ((host->timing == MMC_TIMING_UHS_SDR104) && (clock == 200000000)) {
  519. pci_read_config_dword(chip->pdev, O2_SD_PLL_SETTING, &scratch_32);
  520. if ((scratch_32 & 0xFFFF0000) != dmdn_208m)
  521. o2_pci_set_baseclk(chip, dmdn_208m);
  522. } else {
  523. pci_read_config_dword(chip->pdev, O2_SD_PLL_SETTING, &scratch_32);
  524. if ((scratch_32 & 0xFFFF0000) != dmdn_200m)
  525. o2_pci_set_baseclk(chip, dmdn_200m);
  526. }
  527. pci_read_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, &scratch_32);
  528. scratch_32 &= ~(O2_SD_SEL_DLL | O2_SD_PHASE_MASK);
  529. pci_write_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, scratch_32);
  530. /* Lock WP */
  531. pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
  532. scratch |= 0x80;
  533. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  534. clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
  535. sdhci_o2_enable_clk(host, clk);
  536. }
  537. static int sdhci_pci_o2_init_sd_express(struct mmc_host *mmc, struct mmc_ios *ios)
  538. {
  539. struct sdhci_host *host = mmc_priv(mmc);
  540. struct sdhci_pci_slot *slot = sdhci_priv(host);
  541. struct sdhci_pci_chip *chip = slot->chip;
  542. u8 scratch8;
  543. u16 scratch16;
  544. int ret;
  545. /* Disable clock */
  546. sdhci_writeb(host, 0, SDHCI_CLOCK_CONTROL);
  547. /* Set VDD2 voltage*/
  548. scratch8 = sdhci_readb(host, SDHCI_POWER_CONTROL);
  549. scratch8 &= 0x0F;
  550. if (host->mmc->ios.timing == MMC_TIMING_SD_EXP_1_2V &&
  551. host->mmc->caps2 & MMC_CAP2_SD_EXP_1_2V) {
  552. scratch8 |= SDHCI_VDD2_POWER_ON | SDHCI_VDD2_POWER_120;
  553. } else {
  554. scratch8 |= SDHCI_VDD2_POWER_ON | SDHCI_VDD2_POWER_180;
  555. }
  556. sdhci_writeb(host, scratch8, SDHCI_POWER_CONTROL);
  557. /* UnLock WP */
  558. pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch8);
  559. scratch8 &= 0x7f;
  560. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch8);
  561. /* Wait for express card clkreqn assert */
  562. ret = read_poll_timeout(sdhci_readb, scratch8, !(scratch8 & BIT(0)),
  563. 1, 30000, false, host, O2_SD_EXP_INT_REG);
  564. if (!ret) {
  565. /* Switch to PCIe mode */
  566. scratch16 = sdhci_readw(host, O2_SD_PCIE_SWITCH);
  567. scratch16 |= BIT(8);
  568. sdhci_writew(host, scratch16, O2_SD_PCIE_SWITCH);
  569. } else {
  570. /* Power off VDD2 voltage*/
  571. scratch8 = sdhci_readb(host, SDHCI_POWER_CONTROL);
  572. scratch8 &= 0x0F;
  573. sdhci_writeb(host, scratch8, SDHCI_POWER_CONTROL);
  574. /* Keep mode as UHSI */
  575. pci_read_config_word(chip->pdev, O2_SD_PARA_SET_REG1, &scratch16);
  576. scratch16 &= ~BIT(11);
  577. pci_write_config_word(chip->pdev, O2_SD_PARA_SET_REG1, scratch16);
  578. host->mmc->ios.timing = MMC_TIMING_LEGACY;
  579. pr_info("%s: Express card initialization failed, falling back to Legacy\n",
  580. mmc_hostname(host->mmc));
  581. }
  582. /* Lock WP */
  583. pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch8);
  584. scratch8 |= 0x80;
  585. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch8);
  586. return 0;
  587. }
  588. static void sdhci_pci_o2_set_power(struct sdhci_host *host, unsigned char mode, unsigned short vdd)
  589. {
  590. struct sdhci_pci_chip *chip;
  591. struct sdhci_pci_slot *slot = sdhci_priv(host);
  592. u32 scratch_32 = 0;
  593. u8 scratch_8 = 0;
  594. chip = slot->chip;
  595. if (mode == MMC_POWER_OFF) {
  596. /* UnLock WP */
  597. pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch_8);
  598. scratch_8 &= 0x7f;
  599. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
  600. /* Set PCR 0x354[16] to switch Clock Source back to OPE Clock */
  601. pci_read_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, &scratch_32);
  602. scratch_32 &= ~(O2_SD_SEL_DLL);
  603. pci_write_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, scratch_32);
  604. /* Lock WP */
  605. pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch_8);
  606. scratch_8 |= 0x80;
  607. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch_8);
  608. }
  609. sdhci_set_power(host, mode, vdd);
  610. }
  611. static int sdhci_pci_o2_probe_slot(struct sdhci_pci_slot *slot)
  612. {
  613. struct sdhci_pci_chip *chip;
  614. struct sdhci_host *host;
  615. struct o2_host *o2_host = sdhci_pci_priv(slot);
  616. u32 reg, caps;
  617. int ret;
  618. chip = slot->chip;
  619. host = slot->host;
  620. o2_host->dll_adjust_count = 0;
  621. caps = sdhci_readl(host, SDHCI_CAPABILITIES);
  622. /*
  623. * mmc_select_bus_width() will test the bus to determine the actual bus
  624. * width.
  625. */
  626. if (caps & SDHCI_CAN_DO_8BIT)
  627. host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  628. host->quirks2 |= SDHCI_QUIRK2_BROKEN_DDR50;
  629. sdhci_pci_o2_enable_msi(chip, host);
  630. host->mmc_host_ops.execute_tuning = sdhci_o2_execute_tuning;
  631. switch (chip->pdev->device) {
  632. case PCI_DEVICE_ID_O2_SDS0:
  633. case PCI_DEVICE_ID_O2_SEABIRD0:
  634. case PCI_DEVICE_ID_O2_SEABIRD1:
  635. case PCI_DEVICE_ID_O2_SDS1:
  636. case PCI_DEVICE_ID_O2_FUJIN2:
  637. reg = sdhci_readl(host, O2_SD_VENDOR_SETTING);
  638. if (reg & 0x1)
  639. host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
  640. if (chip->pdev->device == PCI_DEVICE_ID_O2_SEABIRD0) {
  641. ret = pci_read_config_dword(chip->pdev,
  642. O2_SD_MISC_SETTING, &reg);
  643. if (ret)
  644. return -EIO;
  645. if (reg & (1 << 4)) {
  646. pr_info("%s: emmc 1.8v flag is set, force 1.8v signaling voltage\n",
  647. mmc_hostname(host->mmc));
  648. host->flags &= ~SDHCI_SIGNALING_330;
  649. host->flags |= SDHCI_SIGNALING_180;
  650. host->mmc->caps2 |= MMC_CAP2_NO_SD;
  651. host->mmc->caps2 |= MMC_CAP2_NO_SDIO;
  652. pci_write_config_dword(chip->pdev,
  653. O2_SD_DETECT_SETTING, 3);
  654. }
  655. slot->host->mmc_host_ops.get_cd = sdhci_o2_get_cd;
  656. }
  657. if (chip->pdev->device == PCI_DEVICE_ID_O2_SEABIRD1) {
  658. slot->host->mmc_host_ops.get_cd = sdhci_o2_get_cd;
  659. host->mmc->caps2 |= MMC_CAP2_NO_SDIO;
  660. host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
  661. }
  662. if (chip->pdev->device != PCI_DEVICE_ID_O2_FUJIN2)
  663. break;
  664. /* set dll watch dog timer */
  665. reg = sdhci_readl(host, O2_SD_VENDOR_SETTING2);
  666. reg |= (1 << 12);
  667. sdhci_writel(host, reg, O2_SD_VENDOR_SETTING2);
  668. break;
  669. case PCI_DEVICE_ID_O2_GG8_9860:
  670. case PCI_DEVICE_ID_O2_GG8_9861:
  671. case PCI_DEVICE_ID_O2_GG8_9862:
  672. case PCI_DEVICE_ID_O2_GG8_9863:
  673. host->mmc->caps2 |= MMC_CAP2_NO_SDIO | MMC_CAP2_SD_EXP | MMC_CAP2_SD_EXP_1_2V;
  674. host->mmc->caps |= MMC_CAP_HW_RESET;
  675. host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
  676. slot->host->mmc_host_ops.get_cd = sdhci_o2_get_cd;
  677. host->mmc_host_ops.init_sd_express = sdhci_pci_o2_init_sd_express;
  678. break;
  679. default:
  680. break;
  681. }
  682. return 0;
  683. }
  684. static int sdhci_pci_o2_probe(struct sdhci_pci_chip *chip)
  685. {
  686. int ret;
  687. u8 scratch;
  688. u16 scratch16;
  689. u32 scratch_32;
  690. switch (chip->pdev->device) {
  691. case PCI_DEVICE_ID_O2_8220:
  692. case PCI_DEVICE_ID_O2_8221:
  693. case PCI_DEVICE_ID_O2_8320:
  694. case PCI_DEVICE_ID_O2_8321:
  695. /* This extra setup is required due to broken ADMA. */
  696. ret = pci_read_config_byte(chip->pdev,
  697. O2_SD_LOCK_WP, &scratch);
  698. if (ret)
  699. goto read_fail;
  700. scratch &= 0x7f;
  701. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  702. /* Set Multi 3 to VCC3V# */
  703. pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
  704. /* Disable CLK_REQ# support after media DET */
  705. ret = pci_read_config_byte(chip->pdev,
  706. O2_SD_CLKREQ, &scratch);
  707. if (ret)
  708. goto read_fail;
  709. scratch |= 0x20;
  710. pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
  711. /* Choose capabilities, enable SDMA. We have to write 0x01
  712. * to the capabilities register first to unlock it.
  713. */
  714. ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
  715. if (ret)
  716. goto read_fail;
  717. scratch |= 0x01;
  718. pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
  719. pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
  720. /* Disable ADMA1/2 */
  721. pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
  722. pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
  723. /* Disable the infinite transfer mode */
  724. ret = pci_read_config_byte(chip->pdev,
  725. O2_SD_INF_MOD, &scratch);
  726. if (ret)
  727. goto read_fail;
  728. scratch |= 0x08;
  729. pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
  730. /* Lock WP */
  731. ret = pci_read_config_byte(chip->pdev,
  732. O2_SD_LOCK_WP, &scratch);
  733. if (ret)
  734. goto read_fail;
  735. scratch |= 0x80;
  736. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  737. break;
  738. case PCI_DEVICE_ID_O2_SDS0:
  739. case PCI_DEVICE_ID_O2_SDS1:
  740. case PCI_DEVICE_ID_O2_FUJIN2:
  741. /* UnLock WP */
  742. ret = pci_read_config_byte(chip->pdev,
  743. O2_SD_LOCK_WP, &scratch);
  744. if (ret)
  745. goto read_fail;
  746. scratch &= 0x7f;
  747. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  748. /* DevId=8520 subId= 0x11 or 0x12 Type Chip support */
  749. if (chip->pdev->device == PCI_DEVICE_ID_O2_FUJIN2) {
  750. ret = pci_read_config_dword(chip->pdev,
  751. O2_SD_FUNC_REG0,
  752. &scratch_32);
  753. if (ret)
  754. goto read_fail;
  755. scratch_32 = ((scratch_32 & 0xFF000000) >> 24);
  756. /* Check Whether subId is 0x11 or 0x12 */
  757. if ((scratch_32 == 0x11) || (scratch_32 == 0x12)) {
  758. scratch_32 = 0x25100000;
  759. o2_pci_set_baseclk(chip, scratch_32);
  760. ret = pci_read_config_dword(chip->pdev,
  761. O2_SD_FUNC_REG4,
  762. &scratch_32);
  763. if (ret)
  764. goto read_fail;
  765. /* Enable Base Clk setting change */
  766. scratch_32 |= O2_SD_FREG4_ENABLE_CLK_SET;
  767. pci_write_config_dword(chip->pdev,
  768. O2_SD_FUNC_REG4,
  769. scratch_32);
  770. /* Set Tuning Window to 4 */
  771. pci_write_config_byte(chip->pdev,
  772. O2_SD_TUNING_CTRL, 0x44);
  773. break;
  774. }
  775. }
  776. /* Enable 8520 led function */
  777. o2_pci_led_enable(chip);
  778. /* Set timeout CLK */
  779. ret = pci_read_config_dword(chip->pdev,
  780. O2_SD_CLK_SETTING, &scratch_32);
  781. if (ret)
  782. goto read_fail;
  783. scratch_32 &= ~(0xFF00);
  784. scratch_32 |= 0x07E0C800;
  785. pci_write_config_dword(chip->pdev,
  786. O2_SD_CLK_SETTING, scratch_32);
  787. ret = pci_read_config_dword(chip->pdev,
  788. O2_SD_CLKREQ, &scratch_32);
  789. if (ret)
  790. goto read_fail;
  791. scratch_32 |= 0x3;
  792. pci_write_config_dword(chip->pdev, O2_SD_CLKREQ, scratch_32);
  793. ret = pci_read_config_dword(chip->pdev,
  794. O2_SD_PLL_SETTING, &scratch_32);
  795. if (ret)
  796. goto read_fail;
  797. scratch_32 &= ~(0x1F3F070E);
  798. scratch_32 |= 0x18270106;
  799. pci_write_config_dword(chip->pdev,
  800. O2_SD_PLL_SETTING, scratch_32);
  801. /* Disable UHS1 funciton */
  802. ret = pci_read_config_dword(chip->pdev,
  803. O2_SD_CAP_REG2, &scratch_32);
  804. if (ret)
  805. goto read_fail;
  806. scratch_32 &= ~(0xE0);
  807. pci_write_config_dword(chip->pdev,
  808. O2_SD_CAP_REG2, scratch_32);
  809. if (chip->pdev->device == PCI_DEVICE_ID_O2_FUJIN2)
  810. sdhci_pci_o2_fujin2_pci_init(chip);
  811. /* Lock WP */
  812. ret = pci_read_config_byte(chip->pdev,
  813. O2_SD_LOCK_WP, &scratch);
  814. if (ret)
  815. goto read_fail;
  816. scratch |= 0x80;
  817. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  818. break;
  819. case PCI_DEVICE_ID_O2_SEABIRD0:
  820. case PCI_DEVICE_ID_O2_SEABIRD1:
  821. /* UnLock WP */
  822. ret = pci_read_config_byte(chip->pdev,
  823. O2_SD_LOCK_WP, &scratch);
  824. if (ret)
  825. goto read_fail;
  826. scratch &= 0x7f;
  827. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  828. ret = pci_read_config_dword(chip->pdev,
  829. O2_SD_PLL_SETTING, &scratch_32);
  830. if (ret)
  831. goto read_fail;
  832. if ((scratch_32 & 0xff000000) == 0x01000000) {
  833. scratch_32 &= 0x0000FFFF;
  834. scratch_32 |= 0x1F340000;
  835. pci_write_config_dword(chip->pdev,
  836. O2_SD_PLL_SETTING, scratch_32);
  837. } else {
  838. scratch_32 &= 0x0000FFFF;
  839. scratch_32 |= 0x25100000;
  840. pci_write_config_dword(chip->pdev,
  841. O2_SD_PLL_SETTING, scratch_32);
  842. ret = pci_read_config_dword(chip->pdev,
  843. O2_SD_FUNC_REG4,
  844. &scratch_32);
  845. if (ret)
  846. goto read_fail;
  847. scratch_32 |= (1 << 22);
  848. pci_write_config_dword(chip->pdev,
  849. O2_SD_FUNC_REG4, scratch_32);
  850. }
  851. /* Set Tuning Windows to 5 */
  852. pci_write_config_byte(chip->pdev,
  853. O2_SD_TUNING_CTRL, 0x55);
  854. //Adjust 1st and 2nd CD debounce time
  855. pci_read_config_dword(chip->pdev, O2_SD_MISC_CTRL2, &scratch_32);
  856. scratch_32 &= 0xFFE7FFFF;
  857. scratch_32 |= 0x00180000;
  858. pci_write_config_dword(chip->pdev, O2_SD_MISC_CTRL2, scratch_32);
  859. pci_write_config_dword(chip->pdev, O2_SD_DETECT_SETTING, 1);
  860. /* Lock WP */
  861. ret = pci_read_config_byte(chip->pdev,
  862. O2_SD_LOCK_WP, &scratch);
  863. if (ret)
  864. goto read_fail;
  865. scratch |= 0x80;
  866. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  867. break;
  868. case PCI_DEVICE_ID_O2_GG8_9860:
  869. case PCI_DEVICE_ID_O2_GG8_9861:
  870. case PCI_DEVICE_ID_O2_GG8_9862:
  871. case PCI_DEVICE_ID_O2_GG8_9863:
  872. /* UnLock WP */
  873. ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
  874. if (ret)
  875. goto read_fail;
  876. scratch &= 0x7f;
  877. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  878. /* Select mode switch source as software control */
  879. pci_read_config_word(chip->pdev, O2_SD_PARA_SET_REG1, &scratch16);
  880. scratch16 &= 0xF8FF;
  881. scratch16 |= BIT(9);
  882. pci_write_config_word(chip->pdev, O2_SD_PARA_SET_REG1, scratch16);
  883. /* set VDD1 supply source */
  884. pci_read_config_word(chip->pdev, O2_SD_VDDX_CTRL_REG, &scratch16);
  885. scratch16 &= 0xFFE3;
  886. scratch16 |= BIT(3);
  887. pci_write_config_word(chip->pdev, O2_SD_VDDX_CTRL_REG, scratch16);
  888. /* Set host drive strength*/
  889. scratch16 = 0x0025;
  890. pci_write_config_word(chip->pdev, O2_SD_PLL_SETTING, scratch16);
  891. /* Set output delay*/
  892. pci_read_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, &scratch_32);
  893. scratch_32 &= 0xFF0FFF00;
  894. scratch_32 |= 0x00B0003B;
  895. pci_write_config_dword(chip->pdev, O2_SD_OUTPUT_CLK_SOURCE_SWITCH, scratch_32);
  896. /* Lock WP */
  897. ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
  898. if (ret)
  899. goto read_fail;
  900. scratch |= 0x80;
  901. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  902. break;
  903. }
  904. return 0;
  905. read_fail:
  906. return pcibios_err_to_errno(ret);
  907. }
  908. #ifdef CONFIG_PM_SLEEP
  909. static int sdhci_pci_o2_resume(struct sdhci_pci_chip *chip)
  910. {
  911. sdhci_pci_o2_probe(chip);
  912. return sdhci_pci_resume_host(chip);
  913. }
  914. #endif
  915. static const struct sdhci_ops sdhci_pci_o2_ops = {
  916. .set_clock = sdhci_pci_o2_set_clock,
  917. .enable_dma = sdhci_pci_enable_dma,
  918. .set_bus_width = sdhci_set_bus_width,
  919. .reset = sdhci_reset,
  920. .set_uhs_signaling = sdhci_set_uhs_signaling,
  921. .set_power = sdhci_pci_o2_set_power,
  922. };
  923. const struct sdhci_pci_fixes sdhci_o2 = {
  924. .probe = sdhci_pci_o2_probe,
  925. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  926. .quirks2 = SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD,
  927. .probe_slot = sdhci_pci_o2_probe_slot,
  928. #ifdef CONFIG_PM_SLEEP
  929. .resume = sdhci_pci_o2_resume,
  930. #endif
  931. .ops = &sdhci_pci_o2_ops,
  932. .priv_size = sizeof(struct o2_host),
  933. };