sdhci-pci-o2micro.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. /* Set RTD3 function disabled */
  111. scratch_32 |= ((1 << 29) | (1 << 28));
  112. pci_write_config_dword(chip->pdev, O2_SD_FUNC_REG3, scratch_32);
  113. /* Set L1 Entrance Timer */
  114. ret = pci_read_config_dword(chip->pdev, O2_SD_CAPS, &scratch_32);
  115. if (ret)
  116. return;
  117. scratch_32 &= ~(0xf0000000);
  118. scratch_32 |= 0x30000000;
  119. pci_write_config_dword(chip->pdev, O2_SD_CAPS, scratch_32);
  120. ret = pci_read_config_dword(chip->pdev,
  121. O2_SD_MISC_CTRL4, &scratch_32);
  122. if (ret)
  123. return;
  124. scratch_32 &= ~(0x000f0000);
  125. scratch_32 |= 0x00080000;
  126. pci_write_config_dword(chip->pdev, O2_SD_MISC_CTRL4, scratch_32);
  127. }
  128. EXPORT_SYMBOL_GPL(sdhci_pci_o2_fujin2_pci_init);
  129. int sdhci_pci_o2_probe_slot(struct sdhci_pci_slot *slot)
  130. {
  131. struct sdhci_pci_chip *chip;
  132. struct sdhci_host *host;
  133. u32 reg;
  134. chip = slot->chip;
  135. host = slot->host;
  136. switch (chip->pdev->device) {
  137. case PCI_DEVICE_ID_O2_SDS0:
  138. case PCI_DEVICE_ID_O2_SEABIRD0:
  139. case PCI_DEVICE_ID_O2_SEABIRD1:
  140. case PCI_DEVICE_ID_O2_SDS1:
  141. case PCI_DEVICE_ID_O2_FUJIN2:
  142. reg = sdhci_readl(host, O2_SD_VENDOR_SETTING);
  143. if (reg & 0x1)
  144. host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
  145. if (chip->pdev->device != PCI_DEVICE_ID_O2_FUJIN2)
  146. break;
  147. /* set dll watch dog timer */
  148. reg = sdhci_readl(host, O2_SD_VENDOR_SETTING2);
  149. reg |= (1 << 12);
  150. sdhci_writel(host, reg, O2_SD_VENDOR_SETTING2);
  151. break;
  152. default:
  153. break;
  154. }
  155. return 0;
  156. }
  157. EXPORT_SYMBOL_GPL(sdhci_pci_o2_probe_slot);
  158. int sdhci_pci_o2_probe(struct sdhci_pci_chip *chip)
  159. {
  160. int ret;
  161. u8 scratch;
  162. u32 scratch_32;
  163. switch (chip->pdev->device) {
  164. case PCI_DEVICE_ID_O2_8220:
  165. case PCI_DEVICE_ID_O2_8221:
  166. case PCI_DEVICE_ID_O2_8320:
  167. case PCI_DEVICE_ID_O2_8321:
  168. /* This extra setup is required due to broken ADMA. */
  169. ret = pci_read_config_byte(chip->pdev,
  170. O2_SD_LOCK_WP, &scratch);
  171. if (ret)
  172. return ret;
  173. scratch &= 0x7f;
  174. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  175. /* Set Multi 3 to VCC3V# */
  176. pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
  177. /* Disable CLK_REQ# support after media DET */
  178. ret = pci_read_config_byte(chip->pdev,
  179. O2_SD_CLKREQ, &scratch);
  180. if (ret)
  181. return ret;
  182. scratch |= 0x20;
  183. pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
  184. /* Choose capabilities, enable SDMA. We have to write 0x01
  185. * to the capabilities register first to unlock it.
  186. */
  187. ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
  188. if (ret)
  189. return ret;
  190. scratch |= 0x01;
  191. pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
  192. pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
  193. /* Disable ADMA1/2 */
  194. pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
  195. pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
  196. /* Disable the infinite transfer mode */
  197. ret = pci_read_config_byte(chip->pdev,
  198. O2_SD_INF_MOD, &scratch);
  199. if (ret)
  200. return ret;
  201. scratch |= 0x08;
  202. pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
  203. /* Lock WP */
  204. ret = pci_read_config_byte(chip->pdev,
  205. O2_SD_LOCK_WP, &scratch);
  206. if (ret)
  207. return ret;
  208. scratch |= 0x80;
  209. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  210. break;
  211. case PCI_DEVICE_ID_O2_SDS0:
  212. case PCI_DEVICE_ID_O2_SDS1:
  213. case PCI_DEVICE_ID_O2_FUJIN2:
  214. /* UnLock WP */
  215. ret = pci_read_config_byte(chip->pdev,
  216. O2_SD_LOCK_WP, &scratch);
  217. if (ret)
  218. return ret;
  219. scratch &= 0x7f;
  220. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  221. /* DevId=8520 subId= 0x11 or 0x12 Type Chip support */
  222. if (chip->pdev->device == PCI_DEVICE_ID_O2_FUJIN2) {
  223. ret = pci_read_config_dword(chip->pdev,
  224. O2_SD_FUNC_REG0,
  225. &scratch_32);
  226. scratch_32 = ((scratch_32 & 0xFF000000) >> 24);
  227. /* Check Whether subId is 0x11 or 0x12 */
  228. if ((scratch_32 == 0x11) || (scratch_32 == 0x12)) {
  229. scratch_32 = 0x2c280000;
  230. /* Set Base Clock to 208MZ */
  231. o2_pci_set_baseclk(chip, scratch_32);
  232. ret = pci_read_config_dword(chip->pdev,
  233. O2_SD_FUNC_REG4,
  234. &scratch_32);
  235. /* Enable Base Clk setting change */
  236. scratch_32 |= O2_SD_FREG4_ENABLE_CLK_SET;
  237. pci_write_config_dword(chip->pdev,
  238. O2_SD_FUNC_REG4,
  239. scratch_32);
  240. /* Set Tuning Window to 4 */
  241. pci_write_config_byte(chip->pdev,
  242. O2_SD_TUNING_CTRL, 0x44);
  243. break;
  244. }
  245. }
  246. /* Enable 8520 led function */
  247. o2_pci_led_enable(chip);
  248. /* Set timeout CLK */
  249. ret = pci_read_config_dword(chip->pdev,
  250. O2_SD_CLK_SETTING, &scratch_32);
  251. if (ret)
  252. return ret;
  253. scratch_32 &= ~(0xFF00);
  254. scratch_32 |= 0x07E0C800;
  255. pci_write_config_dword(chip->pdev,
  256. O2_SD_CLK_SETTING, scratch_32);
  257. ret = pci_read_config_dword(chip->pdev,
  258. O2_SD_CLKREQ, &scratch_32);
  259. if (ret)
  260. return ret;
  261. scratch_32 |= 0x3;
  262. pci_write_config_dword(chip->pdev, O2_SD_CLKREQ, scratch_32);
  263. ret = pci_read_config_dword(chip->pdev,
  264. O2_SD_PLL_SETTING, &scratch_32);
  265. if (ret)
  266. return ret;
  267. scratch_32 &= ~(0x1F3F070E);
  268. scratch_32 |= 0x18270106;
  269. pci_write_config_dword(chip->pdev,
  270. O2_SD_PLL_SETTING, scratch_32);
  271. /* Disable UHS1 funciton */
  272. ret = pci_read_config_dword(chip->pdev,
  273. O2_SD_CAP_REG2, &scratch_32);
  274. if (ret)
  275. return ret;
  276. scratch_32 &= ~(0xE0);
  277. pci_write_config_dword(chip->pdev,
  278. O2_SD_CAP_REG2, scratch_32);
  279. if (chip->pdev->device == PCI_DEVICE_ID_O2_FUJIN2)
  280. sdhci_pci_o2_fujin2_pci_init(chip);
  281. /* Lock WP */
  282. ret = pci_read_config_byte(chip->pdev,
  283. O2_SD_LOCK_WP, &scratch);
  284. if (ret)
  285. return ret;
  286. scratch |= 0x80;
  287. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  288. break;
  289. case PCI_DEVICE_ID_O2_SEABIRD0:
  290. case PCI_DEVICE_ID_O2_SEABIRD1:
  291. /* UnLock WP */
  292. ret = pci_read_config_byte(chip->pdev,
  293. O2_SD_LOCK_WP, &scratch);
  294. if (ret)
  295. return ret;
  296. scratch &= 0x7f;
  297. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  298. ret = pci_read_config_dword(chip->pdev,
  299. O2_SD_PLL_SETTING, &scratch_32);
  300. if ((scratch_32 & 0xff000000) == 0x01000000) {
  301. scratch_32 &= 0x0000FFFF;
  302. scratch_32 |= 0x1F340000;
  303. pci_write_config_dword(chip->pdev,
  304. O2_SD_PLL_SETTING, scratch_32);
  305. } else {
  306. scratch_32 &= 0x0000FFFF;
  307. scratch_32 |= 0x2c280000;
  308. pci_write_config_dword(chip->pdev,
  309. O2_SD_PLL_SETTING, scratch_32);
  310. ret = pci_read_config_dword(chip->pdev,
  311. O2_SD_FUNC_REG4,
  312. &scratch_32);
  313. scratch_32 |= (1 << 22);
  314. pci_write_config_dword(chip->pdev,
  315. O2_SD_FUNC_REG4, scratch_32);
  316. }
  317. /* Set Tuning Windows to 5 */
  318. pci_write_config_byte(chip->pdev,
  319. O2_SD_TUNING_CTRL, 0x55);
  320. /* Lock WP */
  321. ret = pci_read_config_byte(chip->pdev,
  322. O2_SD_LOCK_WP, &scratch);
  323. if (ret)
  324. return ret;
  325. scratch |= 0x80;
  326. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  327. break;
  328. }
  329. return 0;
  330. }
  331. EXPORT_SYMBOL_GPL(sdhci_pci_o2_probe);
  332. int sdhci_pci_o2_resume(struct sdhci_pci_chip *chip)
  333. {
  334. sdhci_pci_o2_probe(chip);
  335. return 0;
  336. }
  337. EXPORT_SYMBOL_GPL(sdhci_pci_o2_resume);