sdhci-pci-o2micro.c 10 KB

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