sdhci-pci-o2micro.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * Copyright (C) 2013 BayHub Technology Ltd.
  3. *
  4. * Authors: Peter Guo <peter.guo@bayhubtech.com>
  5. * Adam Lee <adam.lee@canonical.com>
  6. * Ernest Zhang <ernest.zhang@bayhubtech.com>
  7. *
  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.
  11. *
  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.
  16. *
  17. */
  18. #include <linux/pci.h>
  19. #include <linux/mmc/host.h>
  20. #include <linux/mmc/mmc.h>
  21. #include <linux/delay.h>
  22. #include "sdhci.h"
  23. #include "sdhci-pci.h"
  24. /*
  25. * O2Micro device registers
  26. */
  27. #define O2_SD_MISC_REG5 0x64
  28. #define O2_SD_LD0_CTRL 0x68
  29. #define O2_SD_DEV_CTRL 0x88
  30. #define O2_SD_LOCK_WP 0xD3
  31. #define O2_SD_TEST_REG 0xD4
  32. #define O2_SD_FUNC_REG0 0xDC
  33. #define O2_SD_MULTI_VCC3V 0xEE
  34. #define O2_SD_CLKREQ 0xEC
  35. #define O2_SD_CAPS 0xE0
  36. #define O2_SD_ADMA1 0xE2
  37. #define O2_SD_ADMA2 0xE7
  38. #define O2_SD_INF_MOD 0xF1
  39. #define O2_SD_MISC_CTRL4 0xFC
  40. #define O2_SD_TUNING_CTRL 0x300
  41. #define O2_SD_PLL_SETTING 0x304
  42. #define O2_SD_MISC_SETTING 0x308
  43. #define O2_SD_CLK_SETTING 0x328
  44. #define O2_SD_CAP_REG2 0x330
  45. #define O2_SD_CAP_REG0 0x334
  46. #define O2_SD_UHS1_CAP_SETTING 0x33C
  47. #define O2_SD_DELAY_CTRL 0x350
  48. #define O2_SD_UHS2_L1_CTRL 0x35C
  49. #define O2_SD_FUNC_REG3 0x3E0
  50. #define O2_SD_FUNC_REG4 0x3E4
  51. #define O2_SD_LED_ENABLE BIT(6)
  52. #define O2_SD_FREG0_LEDOFF BIT(13)
  53. #define O2_SD_FREG4_ENABLE_CLK_SET BIT(22)
  54. #define O2_SD_VENDOR_SETTING 0x110
  55. #define O2_SD_VENDOR_SETTING2 0x1C8
  56. #define O2_SD_HW_TUNING_DISABLE BIT(4)
  57. static void sdhci_o2_set_tuning_mode(struct sdhci_host *host)
  58. {
  59. u16 reg;
  60. /* enable hardware tuning */
  61. reg = sdhci_readw(host, O2_SD_VENDOR_SETTING);
  62. reg &= ~O2_SD_HW_TUNING_DISABLE;
  63. sdhci_writew(host, reg, O2_SD_VENDOR_SETTING);
  64. }
  65. static void __sdhci_o2_execute_tuning(struct sdhci_host *host, u32 opcode)
  66. {
  67. int i;
  68. sdhci_send_tuning(host, MMC_SEND_TUNING_BLOCK_HS200);
  69. for (i = 0; i < 150; i++) {
  70. u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  71. if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
  72. if (ctrl & SDHCI_CTRL_TUNED_CLK) {
  73. host->tuning_done = true;
  74. return;
  75. }
  76. pr_warn("%s: HW tuning failed !\n",
  77. mmc_hostname(host->mmc));
  78. break;
  79. }
  80. mdelay(1);
  81. }
  82. pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
  83. mmc_hostname(host->mmc));
  84. sdhci_reset_tuning(host);
  85. }
  86. static int sdhci_o2_execute_tuning(struct mmc_host *mmc, u32 opcode)
  87. {
  88. struct sdhci_host *host = mmc_priv(mmc);
  89. int current_bus_width = 0;
  90. /*
  91. * This handler only implements the eMMC tuning that is specific to
  92. * this controller. Fall back to the standard method for other TIMING.
  93. */
  94. if (host->timing != MMC_TIMING_MMC_HS200)
  95. return sdhci_execute_tuning(mmc, opcode);
  96. if (WARN_ON(opcode != MMC_SEND_TUNING_BLOCK_HS200))
  97. return -EINVAL;
  98. /*
  99. * o2 sdhci host didn't support 8bit emmc tuning
  100. */
  101. if (mmc->ios.bus_width == MMC_BUS_WIDTH_8) {
  102. current_bus_width = mmc->ios.bus_width;
  103. sdhci_set_bus_width(host, MMC_BUS_WIDTH_4);
  104. }
  105. sdhci_o2_set_tuning_mode(host);
  106. sdhci_start_tuning(host);
  107. __sdhci_o2_execute_tuning(host, opcode);
  108. sdhci_end_tuning(host);
  109. if (current_bus_width == MMC_BUS_WIDTH_8)
  110. sdhci_set_bus_width(host, current_bus_width);
  111. host->flags &= ~SDHCI_HS400_TUNING;
  112. return 0;
  113. }
  114. static void o2_pci_set_baseclk(struct sdhci_pci_chip *chip, u32 value)
  115. {
  116. u32 scratch_32;
  117. pci_read_config_dword(chip->pdev,
  118. O2_SD_PLL_SETTING, &scratch_32);
  119. scratch_32 &= 0x0000FFFF;
  120. scratch_32 |= value;
  121. pci_write_config_dword(chip->pdev,
  122. O2_SD_PLL_SETTING, scratch_32);
  123. }
  124. static void o2_pci_led_enable(struct sdhci_pci_chip *chip)
  125. {
  126. int ret;
  127. u32 scratch_32;
  128. /* Set led of SD host function enable */
  129. ret = pci_read_config_dword(chip->pdev,
  130. O2_SD_FUNC_REG0, &scratch_32);
  131. if (ret)
  132. return;
  133. scratch_32 &= ~O2_SD_FREG0_LEDOFF;
  134. pci_write_config_dword(chip->pdev,
  135. O2_SD_FUNC_REG0, scratch_32);
  136. ret = pci_read_config_dword(chip->pdev,
  137. O2_SD_TEST_REG, &scratch_32);
  138. if (ret)
  139. return;
  140. scratch_32 |= O2_SD_LED_ENABLE;
  141. pci_write_config_dword(chip->pdev,
  142. O2_SD_TEST_REG, scratch_32);
  143. }
  144. static void sdhci_pci_o2_fujin2_pci_init(struct sdhci_pci_chip *chip)
  145. {
  146. u32 scratch_32;
  147. int ret;
  148. /* Improve write performance for SD3.0 */
  149. ret = pci_read_config_dword(chip->pdev, O2_SD_DEV_CTRL, &scratch_32);
  150. if (ret)
  151. return;
  152. scratch_32 &= ~((1 << 12) | (1 << 13) | (1 << 14));
  153. pci_write_config_dword(chip->pdev, O2_SD_DEV_CTRL, scratch_32);
  154. /* Enable Link abnormal reset generating Reset */
  155. ret = pci_read_config_dword(chip->pdev, O2_SD_MISC_REG5, &scratch_32);
  156. if (ret)
  157. return;
  158. scratch_32 &= ~((1 << 19) | (1 << 11));
  159. scratch_32 |= (1 << 10);
  160. pci_write_config_dword(chip->pdev, O2_SD_MISC_REG5, scratch_32);
  161. /* set card power over current protection */
  162. ret = pci_read_config_dword(chip->pdev, O2_SD_TEST_REG, &scratch_32);
  163. if (ret)
  164. return;
  165. scratch_32 |= (1 << 4);
  166. pci_write_config_dword(chip->pdev, O2_SD_TEST_REG, scratch_32);
  167. /* adjust the output delay for SD mode */
  168. pci_write_config_dword(chip->pdev, O2_SD_DELAY_CTRL, 0x00002492);
  169. /* Set the output voltage setting of Aux 1.2v LDO */
  170. ret = pci_read_config_dword(chip->pdev, O2_SD_LD0_CTRL, &scratch_32);
  171. if (ret)
  172. return;
  173. scratch_32 &= ~(3 << 12);
  174. pci_write_config_dword(chip->pdev, O2_SD_LD0_CTRL, scratch_32);
  175. /* Set Max power supply capability of SD host */
  176. ret = pci_read_config_dword(chip->pdev, O2_SD_CAP_REG0, &scratch_32);
  177. if (ret)
  178. return;
  179. scratch_32 &= ~(0x01FE);
  180. scratch_32 |= 0x00CC;
  181. pci_write_config_dword(chip->pdev, O2_SD_CAP_REG0, scratch_32);
  182. /* Set DLL Tuning Window */
  183. ret = pci_read_config_dword(chip->pdev,
  184. O2_SD_TUNING_CTRL, &scratch_32);
  185. if (ret)
  186. return;
  187. scratch_32 &= ~(0x000000FF);
  188. scratch_32 |= 0x00000066;
  189. pci_write_config_dword(chip->pdev, O2_SD_TUNING_CTRL, scratch_32);
  190. /* Set UHS2 T_EIDLE */
  191. ret = pci_read_config_dword(chip->pdev,
  192. O2_SD_UHS2_L1_CTRL, &scratch_32);
  193. if (ret)
  194. return;
  195. scratch_32 &= ~(0x000000FC);
  196. scratch_32 |= 0x00000084;
  197. pci_write_config_dword(chip->pdev, O2_SD_UHS2_L1_CTRL, scratch_32);
  198. /* Set UHS2 Termination */
  199. ret = pci_read_config_dword(chip->pdev, O2_SD_FUNC_REG3, &scratch_32);
  200. if (ret)
  201. return;
  202. scratch_32 &= ~((1 << 21) | (1 << 30));
  203. pci_write_config_dword(chip->pdev, O2_SD_FUNC_REG3, scratch_32);
  204. /* Set L1 Entrance Timer */
  205. ret = pci_read_config_dword(chip->pdev, O2_SD_CAPS, &scratch_32);
  206. if (ret)
  207. return;
  208. scratch_32 &= ~(0xf0000000);
  209. scratch_32 |= 0x30000000;
  210. pci_write_config_dword(chip->pdev, O2_SD_CAPS, scratch_32);
  211. ret = pci_read_config_dword(chip->pdev,
  212. O2_SD_MISC_CTRL4, &scratch_32);
  213. if (ret)
  214. return;
  215. scratch_32 &= ~(0x000f0000);
  216. scratch_32 |= 0x00080000;
  217. pci_write_config_dword(chip->pdev, O2_SD_MISC_CTRL4, scratch_32);
  218. }
  219. static void sdhci_pci_o2_enable_msi(struct sdhci_pci_chip *chip,
  220. struct sdhci_host *host)
  221. {
  222. int ret;
  223. ret = pci_find_capability(chip->pdev, PCI_CAP_ID_MSI);
  224. if (!ret) {
  225. pr_info("%s: unsupport msi, use INTx irq\n",
  226. mmc_hostname(host->mmc));
  227. return;
  228. }
  229. ret = pci_alloc_irq_vectors(chip->pdev, 1, 1,
  230. PCI_IRQ_MSI | PCI_IRQ_MSIX);
  231. if (ret < 0) {
  232. pr_err("%s: enable PCI MSI failed, err=%d\n",
  233. mmc_hostname(host->mmc), ret);
  234. return;
  235. }
  236. host->irq = pci_irq_vector(chip->pdev, 0);
  237. }
  238. int sdhci_pci_o2_probe_slot(struct sdhci_pci_slot *slot)
  239. {
  240. struct sdhci_pci_chip *chip;
  241. struct sdhci_host *host;
  242. u32 reg;
  243. int ret;
  244. chip = slot->chip;
  245. host = slot->host;
  246. switch (chip->pdev->device) {
  247. case PCI_DEVICE_ID_O2_SDS0:
  248. case PCI_DEVICE_ID_O2_SEABIRD0:
  249. case PCI_DEVICE_ID_O2_SEABIRD1:
  250. case PCI_DEVICE_ID_O2_SDS1:
  251. case PCI_DEVICE_ID_O2_FUJIN2:
  252. reg = sdhci_readl(host, O2_SD_VENDOR_SETTING);
  253. if (reg & 0x1)
  254. host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
  255. sdhci_pci_o2_enable_msi(chip, host);
  256. if (chip->pdev->device == PCI_DEVICE_ID_O2_SEABIRD0) {
  257. ret = pci_read_config_dword(chip->pdev,
  258. O2_SD_MISC_SETTING, &reg);
  259. if (ret)
  260. return -EIO;
  261. if (reg & (1 << 4)) {
  262. pr_info("%s: emmc 1.8v flag is set, force 1.8v signaling voltage\n",
  263. mmc_hostname(host->mmc));
  264. host->flags &= ~SDHCI_SIGNALING_330;
  265. host->flags |= SDHCI_SIGNALING_180;
  266. host->mmc->caps2 |= MMC_CAP2_NO_SD;
  267. host->mmc->caps2 |= MMC_CAP2_NO_SDIO;
  268. }
  269. }
  270. host->mmc_host_ops.execute_tuning = sdhci_o2_execute_tuning;
  271. if (chip->pdev->device != PCI_DEVICE_ID_O2_FUJIN2)
  272. break;
  273. /* set dll watch dog timer */
  274. reg = sdhci_readl(host, O2_SD_VENDOR_SETTING2);
  275. reg |= (1 << 12);
  276. sdhci_writel(host, reg, O2_SD_VENDOR_SETTING2);
  277. break;
  278. default:
  279. break;
  280. }
  281. return 0;
  282. }
  283. int sdhci_pci_o2_probe(struct sdhci_pci_chip *chip)
  284. {
  285. int ret;
  286. u8 scratch;
  287. u32 scratch_32;
  288. switch (chip->pdev->device) {
  289. case PCI_DEVICE_ID_O2_8220:
  290. case PCI_DEVICE_ID_O2_8221:
  291. case PCI_DEVICE_ID_O2_8320:
  292. case PCI_DEVICE_ID_O2_8321:
  293. /* This extra setup is required due to broken ADMA. */
  294. ret = pci_read_config_byte(chip->pdev,
  295. O2_SD_LOCK_WP, &scratch);
  296. if (ret)
  297. return ret;
  298. scratch &= 0x7f;
  299. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  300. /* Set Multi 3 to VCC3V# */
  301. pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
  302. /* Disable CLK_REQ# support after media DET */
  303. ret = pci_read_config_byte(chip->pdev,
  304. O2_SD_CLKREQ, &scratch);
  305. if (ret)
  306. return ret;
  307. scratch |= 0x20;
  308. pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
  309. /* Choose capabilities, enable SDMA. We have to write 0x01
  310. * to the capabilities register first to unlock it.
  311. */
  312. ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
  313. if (ret)
  314. return ret;
  315. scratch |= 0x01;
  316. pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
  317. pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
  318. /* Disable ADMA1/2 */
  319. pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
  320. pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
  321. /* Disable the infinite transfer mode */
  322. ret = pci_read_config_byte(chip->pdev,
  323. O2_SD_INF_MOD, &scratch);
  324. if (ret)
  325. return ret;
  326. scratch |= 0x08;
  327. pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
  328. /* Lock WP */
  329. ret = pci_read_config_byte(chip->pdev,
  330. O2_SD_LOCK_WP, &scratch);
  331. if (ret)
  332. return ret;
  333. scratch |= 0x80;
  334. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  335. break;
  336. case PCI_DEVICE_ID_O2_SDS0:
  337. case PCI_DEVICE_ID_O2_SDS1:
  338. case PCI_DEVICE_ID_O2_FUJIN2:
  339. /* UnLock WP */
  340. ret = pci_read_config_byte(chip->pdev,
  341. O2_SD_LOCK_WP, &scratch);
  342. if (ret)
  343. return ret;
  344. scratch &= 0x7f;
  345. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  346. /* DevId=8520 subId= 0x11 or 0x12 Type Chip support */
  347. if (chip->pdev->device == PCI_DEVICE_ID_O2_FUJIN2) {
  348. ret = pci_read_config_dword(chip->pdev,
  349. O2_SD_FUNC_REG0,
  350. &scratch_32);
  351. scratch_32 = ((scratch_32 & 0xFF000000) >> 24);
  352. /* Check Whether subId is 0x11 or 0x12 */
  353. if ((scratch_32 == 0x11) || (scratch_32 == 0x12)) {
  354. scratch_32 = 0x25100000;
  355. o2_pci_set_baseclk(chip, scratch_32);
  356. ret = pci_read_config_dword(chip->pdev,
  357. O2_SD_FUNC_REG4,
  358. &scratch_32);
  359. /* Enable Base Clk setting change */
  360. scratch_32 |= O2_SD_FREG4_ENABLE_CLK_SET;
  361. pci_write_config_dword(chip->pdev,
  362. O2_SD_FUNC_REG4,
  363. scratch_32);
  364. /* Set Tuning Window to 4 */
  365. pci_write_config_byte(chip->pdev,
  366. O2_SD_TUNING_CTRL, 0x44);
  367. break;
  368. }
  369. }
  370. /* Enable 8520 led function */
  371. o2_pci_led_enable(chip);
  372. /* Set timeout CLK */
  373. ret = pci_read_config_dword(chip->pdev,
  374. O2_SD_CLK_SETTING, &scratch_32);
  375. if (ret)
  376. return ret;
  377. scratch_32 &= ~(0xFF00);
  378. scratch_32 |= 0x07E0C800;
  379. pci_write_config_dword(chip->pdev,
  380. O2_SD_CLK_SETTING, scratch_32);
  381. ret = pci_read_config_dword(chip->pdev,
  382. O2_SD_CLKREQ, &scratch_32);
  383. if (ret)
  384. return ret;
  385. scratch_32 |= 0x3;
  386. pci_write_config_dword(chip->pdev, O2_SD_CLKREQ, scratch_32);
  387. ret = pci_read_config_dword(chip->pdev,
  388. O2_SD_PLL_SETTING, &scratch_32);
  389. if (ret)
  390. return ret;
  391. scratch_32 &= ~(0x1F3F070E);
  392. scratch_32 |= 0x18270106;
  393. pci_write_config_dword(chip->pdev,
  394. O2_SD_PLL_SETTING, scratch_32);
  395. /* Disable UHS1 funciton */
  396. ret = pci_read_config_dword(chip->pdev,
  397. O2_SD_CAP_REG2, &scratch_32);
  398. if (ret)
  399. return ret;
  400. scratch_32 &= ~(0xE0);
  401. pci_write_config_dword(chip->pdev,
  402. O2_SD_CAP_REG2, scratch_32);
  403. if (chip->pdev->device == PCI_DEVICE_ID_O2_FUJIN2)
  404. sdhci_pci_o2_fujin2_pci_init(chip);
  405. /* Lock WP */
  406. ret = pci_read_config_byte(chip->pdev,
  407. O2_SD_LOCK_WP, &scratch);
  408. if (ret)
  409. return ret;
  410. scratch |= 0x80;
  411. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  412. break;
  413. case PCI_DEVICE_ID_O2_SEABIRD0:
  414. if (chip->pdev->revision == 0x01)
  415. chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
  416. /* fall through */
  417. case PCI_DEVICE_ID_O2_SEABIRD1:
  418. /* UnLock WP */
  419. ret = pci_read_config_byte(chip->pdev,
  420. O2_SD_LOCK_WP, &scratch);
  421. if (ret)
  422. return ret;
  423. scratch &= 0x7f;
  424. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  425. ret = pci_read_config_dword(chip->pdev,
  426. O2_SD_PLL_SETTING, &scratch_32);
  427. if ((scratch_32 & 0xff000000) == 0x01000000) {
  428. scratch_32 &= 0x0000FFFF;
  429. scratch_32 |= 0x1F340000;
  430. pci_write_config_dword(chip->pdev,
  431. O2_SD_PLL_SETTING, scratch_32);
  432. } else {
  433. scratch_32 &= 0x0000FFFF;
  434. scratch_32 |= 0x25100000;
  435. pci_write_config_dword(chip->pdev,
  436. O2_SD_PLL_SETTING, scratch_32);
  437. ret = pci_read_config_dword(chip->pdev,
  438. O2_SD_FUNC_REG4,
  439. &scratch_32);
  440. scratch_32 |= (1 << 22);
  441. pci_write_config_dword(chip->pdev,
  442. O2_SD_FUNC_REG4, scratch_32);
  443. }
  444. /* Set Tuning Windows to 5 */
  445. pci_write_config_byte(chip->pdev,
  446. O2_SD_TUNING_CTRL, 0x55);
  447. /* Lock WP */
  448. ret = pci_read_config_byte(chip->pdev,
  449. O2_SD_LOCK_WP, &scratch);
  450. if (ret)
  451. return ret;
  452. scratch |= 0x80;
  453. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  454. break;
  455. }
  456. return 0;
  457. }
  458. #ifdef CONFIG_PM_SLEEP
  459. int sdhci_pci_o2_resume(struct sdhci_pci_chip *chip)
  460. {
  461. sdhci_pci_o2_probe(chip);
  462. return sdhci_pci_resume_host(chip);
  463. }
  464. #endif