sdhci-pci-o2micro.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/pci.h>
  18. #include "sdhci.h"
  19. #include "sdhci-pci.h"
  20. /*
  21. * O2Micro device registers
  22. */
  23. #define O2_SD_MISC_REG5 0x64
  24. #define O2_SD_LD0_CTRL 0x68
  25. #define O2_SD_DEV_CTRL 0x88
  26. #define O2_SD_LOCK_WP 0xD3
  27. #define O2_SD_TEST_REG 0xD4
  28. #define O2_SD_FUNC_REG0 0xDC
  29. #define O2_SD_MULTI_VCC3V 0xEE
  30. #define O2_SD_CLKREQ 0xEC
  31. #define O2_SD_CAPS 0xE0
  32. #define O2_SD_ADMA1 0xE2
  33. #define O2_SD_ADMA2 0xE7
  34. #define O2_SD_INF_MOD 0xF1
  35. #define O2_SD_MISC_CTRL4 0xFC
  36. #define O2_SD_TUNING_CTRL 0x300
  37. #define O2_SD_PLL_SETTING 0x304
  38. #define O2_SD_CLK_SETTING 0x328
  39. #define O2_SD_CAP_REG2 0x330
  40. #define O2_SD_CAP_REG0 0x334
  41. #define O2_SD_UHS1_CAP_SETTING 0x33C
  42. #define O2_SD_DELAY_CTRL 0x350
  43. #define O2_SD_UHS2_L1_CTRL 0x35C
  44. #define O2_SD_FUNC_REG3 0x3E0
  45. #define O2_SD_FUNC_REG4 0x3E4
  46. #define O2_SD_LED_ENABLE BIT(6)
  47. #define O2_SD_FREG0_LEDOFF BIT(13)
  48. #define O2_SD_FREG4_ENABLE_CLK_SET BIT(22)
  49. #define O2_SD_VENDOR_SETTING 0x110
  50. #define O2_SD_VENDOR_SETTING2 0x1C8
  51. static void o2_pci_set_baseclk(struct sdhci_pci_chip *chip, u32 value)
  52. {
  53. u32 scratch_32;
  54. pci_read_config_dword(chip->pdev,
  55. O2_SD_PLL_SETTING, &scratch_32);
  56. scratch_32 &= 0x0000FFFF;
  57. scratch_32 |= value;
  58. pci_write_config_dword(chip->pdev,
  59. O2_SD_PLL_SETTING, scratch_32);
  60. }
  61. static void o2_pci_led_enable(struct sdhci_pci_chip *chip)
  62. {
  63. int ret;
  64. u32 scratch_32;
  65. /* Set led of SD host function enable */
  66. ret = pci_read_config_dword(chip->pdev,
  67. O2_SD_FUNC_REG0, &scratch_32);
  68. if (ret)
  69. return;
  70. scratch_32 &= ~O2_SD_FREG0_LEDOFF;
  71. pci_write_config_dword(chip->pdev,
  72. O2_SD_FUNC_REG0, scratch_32);
  73. ret = pci_read_config_dword(chip->pdev,
  74. O2_SD_TEST_REG, &scratch_32);
  75. if (ret)
  76. return;
  77. scratch_32 |= O2_SD_LED_ENABLE;
  78. pci_write_config_dword(chip->pdev,
  79. O2_SD_TEST_REG, scratch_32);
  80. }
  81. static void sdhci_pci_o2_fujin2_pci_init(struct sdhci_pci_chip *chip)
  82. {
  83. u32 scratch_32;
  84. int ret;
  85. /* Improve write performance for SD3.0 */
  86. ret = pci_read_config_dword(chip->pdev, O2_SD_DEV_CTRL, &scratch_32);
  87. if (ret)
  88. return;
  89. scratch_32 &= ~((1 << 12) | (1 << 13) | (1 << 14));
  90. pci_write_config_dword(chip->pdev, O2_SD_DEV_CTRL, scratch_32);
  91. /* Enable Link abnormal reset generating Reset */
  92. ret = pci_read_config_dword(chip->pdev, O2_SD_MISC_REG5, &scratch_32);
  93. if (ret)
  94. return;
  95. scratch_32 &= ~((1 << 19) | (1 << 11));
  96. scratch_32 |= (1 << 10);
  97. pci_write_config_dword(chip->pdev, O2_SD_MISC_REG5, scratch_32);
  98. /* set card power over current protection */
  99. ret = pci_read_config_dword(chip->pdev, O2_SD_TEST_REG, &scratch_32);
  100. if (ret)
  101. return;
  102. scratch_32 |= (1 << 4);
  103. pci_write_config_dword(chip->pdev, O2_SD_TEST_REG, scratch_32);
  104. /* adjust the output delay for SD mode */
  105. pci_write_config_dword(chip->pdev, O2_SD_DELAY_CTRL, 0x00002492);
  106. /* Set the output voltage setting of Aux 1.2v LDO */
  107. ret = pci_read_config_dword(chip->pdev, O2_SD_LD0_CTRL, &scratch_32);
  108. if (ret)
  109. return;
  110. scratch_32 &= ~(3 << 12);
  111. pci_write_config_dword(chip->pdev, O2_SD_LD0_CTRL, scratch_32);
  112. /* Set Max power supply capability of SD host */
  113. ret = pci_read_config_dword(chip->pdev, O2_SD_CAP_REG0, &scratch_32);
  114. if (ret)
  115. return;
  116. scratch_32 &= ~(0x01FE);
  117. scratch_32 |= 0x00CC;
  118. pci_write_config_dword(chip->pdev, O2_SD_CAP_REG0, scratch_32);
  119. /* Set DLL Tuning Window */
  120. ret = pci_read_config_dword(chip->pdev,
  121. O2_SD_TUNING_CTRL, &scratch_32);
  122. if (ret)
  123. return;
  124. scratch_32 &= ~(0x000000FF);
  125. scratch_32 |= 0x00000066;
  126. pci_write_config_dword(chip->pdev, O2_SD_TUNING_CTRL, scratch_32);
  127. /* Set UHS2 T_EIDLE */
  128. ret = pci_read_config_dword(chip->pdev,
  129. O2_SD_UHS2_L1_CTRL, &scratch_32);
  130. if (ret)
  131. return;
  132. scratch_32 &= ~(0x000000FC);
  133. scratch_32 |= 0x00000084;
  134. pci_write_config_dword(chip->pdev, O2_SD_UHS2_L1_CTRL, scratch_32);
  135. /* Set UHS2 Termination */
  136. ret = pci_read_config_dword(chip->pdev, O2_SD_FUNC_REG3, &scratch_32);
  137. if (ret)
  138. return;
  139. scratch_32 &= ~((1 << 21) | (1 << 30));
  140. pci_write_config_dword(chip->pdev, O2_SD_FUNC_REG3, scratch_32);
  141. /* Set L1 Entrance Timer */
  142. ret = pci_read_config_dword(chip->pdev, O2_SD_CAPS, &scratch_32);
  143. if (ret)
  144. return;
  145. scratch_32 &= ~(0xf0000000);
  146. scratch_32 |= 0x30000000;
  147. pci_write_config_dword(chip->pdev, O2_SD_CAPS, scratch_32);
  148. ret = pci_read_config_dword(chip->pdev,
  149. O2_SD_MISC_CTRL4, &scratch_32);
  150. if (ret)
  151. return;
  152. scratch_32 &= ~(0x000f0000);
  153. scratch_32 |= 0x00080000;
  154. pci_write_config_dword(chip->pdev, O2_SD_MISC_CTRL4, scratch_32);
  155. }
  156. int sdhci_pci_o2_probe_slot(struct sdhci_pci_slot *slot)
  157. {
  158. struct sdhci_pci_chip *chip;
  159. struct sdhci_host *host;
  160. u32 reg;
  161. chip = slot->chip;
  162. host = slot->host;
  163. switch (chip->pdev->device) {
  164. case PCI_DEVICE_ID_O2_SDS0:
  165. case PCI_DEVICE_ID_O2_SEABIRD0:
  166. case PCI_DEVICE_ID_O2_SEABIRD1:
  167. case PCI_DEVICE_ID_O2_SDS1:
  168. case PCI_DEVICE_ID_O2_FUJIN2:
  169. reg = sdhci_readl(host, O2_SD_VENDOR_SETTING);
  170. if (reg & 0x1)
  171. host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
  172. if (chip->pdev->device != PCI_DEVICE_ID_O2_FUJIN2)
  173. break;
  174. /* set dll watch dog timer */
  175. reg = sdhci_readl(host, O2_SD_VENDOR_SETTING2);
  176. reg |= (1 << 12);
  177. sdhci_writel(host, reg, O2_SD_VENDOR_SETTING2);
  178. break;
  179. default:
  180. break;
  181. }
  182. return 0;
  183. }
  184. int sdhci_pci_o2_probe(struct sdhci_pci_chip *chip)
  185. {
  186. int ret;
  187. u8 scratch;
  188. u32 scratch_32;
  189. switch (chip->pdev->device) {
  190. case PCI_DEVICE_ID_O2_8220:
  191. case PCI_DEVICE_ID_O2_8221:
  192. case PCI_DEVICE_ID_O2_8320:
  193. case PCI_DEVICE_ID_O2_8321:
  194. /* This extra setup is required due to broken ADMA. */
  195. ret = pci_read_config_byte(chip->pdev,
  196. O2_SD_LOCK_WP, &scratch);
  197. if (ret)
  198. return ret;
  199. scratch &= 0x7f;
  200. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  201. /* Set Multi 3 to VCC3V# */
  202. pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
  203. /* Disable CLK_REQ# support after media DET */
  204. ret = pci_read_config_byte(chip->pdev,
  205. O2_SD_CLKREQ, &scratch);
  206. if (ret)
  207. return ret;
  208. scratch |= 0x20;
  209. pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
  210. /* Choose capabilities, enable SDMA. We have to write 0x01
  211. * to the capabilities register first to unlock it.
  212. */
  213. ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
  214. if (ret)
  215. return ret;
  216. scratch |= 0x01;
  217. pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
  218. pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
  219. /* Disable ADMA1/2 */
  220. pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
  221. pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
  222. /* Disable the infinite transfer mode */
  223. ret = pci_read_config_byte(chip->pdev,
  224. O2_SD_INF_MOD, &scratch);
  225. if (ret)
  226. return ret;
  227. scratch |= 0x08;
  228. pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
  229. /* Lock WP */
  230. ret = pci_read_config_byte(chip->pdev,
  231. O2_SD_LOCK_WP, &scratch);
  232. if (ret)
  233. return ret;
  234. scratch |= 0x80;
  235. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  236. break;
  237. case PCI_DEVICE_ID_O2_SDS0:
  238. case PCI_DEVICE_ID_O2_SDS1:
  239. case PCI_DEVICE_ID_O2_FUJIN2:
  240. /* UnLock WP */
  241. ret = pci_read_config_byte(chip->pdev,
  242. O2_SD_LOCK_WP, &scratch);
  243. if (ret)
  244. return ret;
  245. scratch &= 0x7f;
  246. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  247. /* DevId=8520 subId= 0x11 or 0x12 Type Chip support */
  248. if (chip->pdev->device == PCI_DEVICE_ID_O2_FUJIN2) {
  249. ret = pci_read_config_dword(chip->pdev,
  250. O2_SD_FUNC_REG0,
  251. &scratch_32);
  252. scratch_32 = ((scratch_32 & 0xFF000000) >> 24);
  253. /* Check Whether subId is 0x11 or 0x12 */
  254. if ((scratch_32 == 0x11) || (scratch_32 == 0x12)) {
  255. scratch_32 = 0x2c280000;
  256. /* Set Base Clock to 208MZ */
  257. o2_pci_set_baseclk(chip, scratch_32);
  258. ret = pci_read_config_dword(chip->pdev,
  259. O2_SD_FUNC_REG4,
  260. &scratch_32);
  261. /* Enable Base Clk setting change */
  262. scratch_32 |= O2_SD_FREG4_ENABLE_CLK_SET;
  263. pci_write_config_dword(chip->pdev,
  264. O2_SD_FUNC_REG4,
  265. scratch_32);
  266. /* Set Tuning Window to 4 */
  267. pci_write_config_byte(chip->pdev,
  268. O2_SD_TUNING_CTRL, 0x44);
  269. break;
  270. }
  271. }
  272. /* Enable 8520 led function */
  273. o2_pci_led_enable(chip);
  274. /* Set timeout CLK */
  275. ret = pci_read_config_dword(chip->pdev,
  276. O2_SD_CLK_SETTING, &scratch_32);
  277. if (ret)
  278. return ret;
  279. scratch_32 &= ~(0xFF00);
  280. scratch_32 |= 0x07E0C800;
  281. pci_write_config_dword(chip->pdev,
  282. O2_SD_CLK_SETTING, scratch_32);
  283. ret = pci_read_config_dword(chip->pdev,
  284. O2_SD_CLKREQ, &scratch_32);
  285. if (ret)
  286. return ret;
  287. scratch_32 |= 0x3;
  288. pci_write_config_dword(chip->pdev, O2_SD_CLKREQ, scratch_32);
  289. ret = pci_read_config_dword(chip->pdev,
  290. O2_SD_PLL_SETTING, &scratch_32);
  291. if (ret)
  292. return ret;
  293. scratch_32 &= ~(0x1F3F070E);
  294. scratch_32 |= 0x18270106;
  295. pci_write_config_dword(chip->pdev,
  296. O2_SD_PLL_SETTING, scratch_32);
  297. /* Disable UHS1 funciton */
  298. ret = pci_read_config_dword(chip->pdev,
  299. O2_SD_CAP_REG2, &scratch_32);
  300. if (ret)
  301. return ret;
  302. scratch_32 &= ~(0xE0);
  303. pci_write_config_dword(chip->pdev,
  304. O2_SD_CAP_REG2, scratch_32);
  305. if (chip->pdev->device == PCI_DEVICE_ID_O2_FUJIN2)
  306. sdhci_pci_o2_fujin2_pci_init(chip);
  307. /* Lock WP */
  308. ret = pci_read_config_byte(chip->pdev,
  309. O2_SD_LOCK_WP, &scratch);
  310. if (ret)
  311. return ret;
  312. scratch |= 0x80;
  313. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  314. break;
  315. case PCI_DEVICE_ID_O2_SEABIRD0:
  316. case PCI_DEVICE_ID_O2_SEABIRD1:
  317. /* UnLock WP */
  318. ret = pci_read_config_byte(chip->pdev,
  319. O2_SD_LOCK_WP, &scratch);
  320. if (ret)
  321. return ret;
  322. scratch &= 0x7f;
  323. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  324. ret = pci_read_config_dword(chip->pdev,
  325. O2_SD_PLL_SETTING, &scratch_32);
  326. if ((scratch_32 & 0xff000000) == 0x01000000) {
  327. scratch_32 &= 0x0000FFFF;
  328. scratch_32 |= 0x1F340000;
  329. pci_write_config_dword(chip->pdev,
  330. O2_SD_PLL_SETTING, scratch_32);
  331. } else {
  332. scratch_32 &= 0x0000FFFF;
  333. scratch_32 |= 0x2c280000;
  334. pci_write_config_dword(chip->pdev,
  335. O2_SD_PLL_SETTING, scratch_32);
  336. ret = pci_read_config_dword(chip->pdev,
  337. O2_SD_FUNC_REG4,
  338. &scratch_32);
  339. scratch_32 |= (1 << 22);
  340. pci_write_config_dword(chip->pdev,
  341. O2_SD_FUNC_REG4, scratch_32);
  342. }
  343. /* Set Tuning Windows to 5 */
  344. pci_write_config_byte(chip->pdev,
  345. O2_SD_TUNING_CTRL, 0x55);
  346. /* Lock WP */
  347. ret = pci_read_config_byte(chip->pdev,
  348. O2_SD_LOCK_WP, &scratch);
  349. if (ret)
  350. return ret;
  351. scratch |= 0x80;
  352. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  353. break;
  354. }
  355. return 0;
  356. }
  357. #ifdef CONFIG_PM_SLEEP
  358. int sdhci_pci_o2_resume(struct sdhci_pci_chip *chip)
  359. {
  360. sdhci_pci_o2_probe(chip);
  361. return sdhci_pci_resume_host(chip);
  362. }
  363. #endif