sdhci-pci-o2micro.c 10 KB

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