sdhci-pci-o2micro.c 25 KB

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