rts5260.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /* Driver for Realtek PCI-Express card reader
  2. *
  3. * Copyright(c) 2016-2017 Realtek Semiconductor Corp. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2, or (at your option) any
  8. * later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Author:
  19. * Steven FENG <steven_feng@realsil.com.cn>
  20. * Rui FENG <rui_feng@realsil.com.cn>
  21. * Wei WANG <wei_wang@realsil.com.cn>
  22. */
  23. #include <linux/module.h>
  24. #include <linux/delay.h>
  25. #include <linux/rtsx_pci.h>
  26. #include "rts5260.h"
  27. #include "rtsx_pcr.h"
  28. static u8 rts5260_get_ic_version(struct rtsx_pcr *pcr)
  29. {
  30. u8 val;
  31. rtsx_pci_read_register(pcr, DUMMY_REG_RESET_0, &val);
  32. return val & IC_VERSION_MASK;
  33. }
  34. static void rts5260_fill_driving(struct rtsx_pcr *pcr, u8 voltage)
  35. {
  36. u8 driving_3v3[6][3] = {
  37. {0x94, 0x94, 0x94},
  38. {0x11, 0x11, 0x18},
  39. {0x55, 0x55, 0x5C},
  40. {0x94, 0x94, 0x94},
  41. {0x94, 0x94, 0x94},
  42. {0xFF, 0xFF, 0xFF},
  43. };
  44. u8 driving_1v8[6][3] = {
  45. {0x9A, 0x89, 0x89},
  46. {0xC4, 0xC4, 0xC4},
  47. {0x3C, 0x3C, 0x3C},
  48. {0x9B, 0x99, 0x99},
  49. {0x9A, 0x89, 0x89},
  50. {0xFE, 0xFE, 0xFE},
  51. };
  52. u8 (*driving)[3], drive_sel;
  53. if (voltage == OUTPUT_3V3) {
  54. driving = driving_3v3;
  55. drive_sel = pcr->sd30_drive_sel_3v3;
  56. } else {
  57. driving = driving_1v8;
  58. drive_sel = pcr->sd30_drive_sel_1v8;
  59. }
  60. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_CLK_DRIVE_SEL,
  61. 0xFF, driving[drive_sel][0]);
  62. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_CMD_DRIVE_SEL,
  63. 0xFF, driving[drive_sel][1]);
  64. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_DAT_DRIVE_SEL,
  65. 0xFF, driving[drive_sel][2]);
  66. }
  67. static void rtsx_base_fetch_vendor_settings(struct rtsx_pcr *pcr)
  68. {
  69. u32 reg;
  70. rtsx_pci_read_config_dword(pcr, PCR_SETTING_REG1, &reg);
  71. pcr_dbg(pcr, "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG1, reg);
  72. if (!rtsx_vendor_setting_valid(reg)) {
  73. pcr_dbg(pcr, "skip fetch vendor setting\n");
  74. return;
  75. }
  76. pcr->aspm_en = rtsx_reg_to_aspm(reg);
  77. pcr->sd30_drive_sel_1v8 = rtsx_reg_to_sd30_drive_sel_1v8(reg);
  78. pcr->card_drive_sel &= 0x3F;
  79. pcr->card_drive_sel |= rtsx_reg_to_card_drive_sel(reg);
  80. rtsx_pci_read_config_dword(pcr, PCR_SETTING_REG2, &reg);
  81. pcr_dbg(pcr, "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG2, reg);
  82. pcr->sd30_drive_sel_3v3 = rtsx_reg_to_sd30_drive_sel_3v3(reg);
  83. if (rtsx_reg_check_reverse_socket(reg))
  84. pcr->flags |= PCR_REVERSE_SOCKET;
  85. }
  86. static void rtsx_base_force_power_down(struct rtsx_pcr *pcr, u8 pm_state)
  87. {
  88. /* Set relink_time to 0 */
  89. rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 1, MASK_8_BIT_DEF, 0);
  90. rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 2, MASK_8_BIT_DEF, 0);
  91. rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 3,
  92. RELINK_TIME_MASK, 0);
  93. if (pm_state == HOST_ENTER_S3)
  94. rtsx_pci_write_register(pcr, pcr->reg_pm_ctrl3,
  95. D3_DELINK_MODE_EN, D3_DELINK_MODE_EN);
  96. rtsx_pci_write_register(pcr, FPDCTL, ALL_POWER_DOWN, ALL_POWER_DOWN);
  97. }
  98. static int rtsx_base_enable_auto_blink(struct rtsx_pcr *pcr)
  99. {
  100. return rtsx_pci_write_register(pcr, OLT_LED_CTL,
  101. LED_SHINE_MASK, LED_SHINE_EN);
  102. }
  103. static int rtsx_base_disable_auto_blink(struct rtsx_pcr *pcr)
  104. {
  105. return rtsx_pci_write_register(pcr, OLT_LED_CTL,
  106. LED_SHINE_MASK, LED_SHINE_DISABLE);
  107. }
  108. static int rts5260_turn_on_led(struct rtsx_pcr *pcr)
  109. {
  110. return rtsx_pci_write_register(pcr, RTS5260_REG_GPIO_CTL0,
  111. RTS5260_REG_GPIO_MASK, RTS5260_REG_GPIO_ON);
  112. }
  113. static int rts5260_turn_off_led(struct rtsx_pcr *pcr)
  114. {
  115. return rtsx_pci_write_register(pcr, RTS5260_REG_GPIO_CTL0,
  116. RTS5260_REG_GPIO_MASK, RTS5260_REG_GPIO_OFF);
  117. }
  118. /* SD Pull Control Enable:
  119. * SD_DAT[3:0] ==> pull up
  120. * SD_CD ==> pull up
  121. * SD_WP ==> pull up
  122. * SD_CMD ==> pull up
  123. * SD_CLK ==> pull down
  124. */
  125. static const u32 rts5260_sd_pull_ctl_enable_tbl[] = {
  126. RTSX_REG_PAIR(CARD_PULL_CTL1, 0x66),
  127. RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
  128. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9),
  129. RTSX_REG_PAIR(CARD_PULL_CTL4, 0xAA),
  130. 0,
  131. };
  132. /* SD Pull Control Disable:
  133. * SD_DAT[3:0] ==> pull down
  134. * SD_CD ==> pull up
  135. * SD_WP ==> pull down
  136. * SD_CMD ==> pull down
  137. * SD_CLK ==> pull down
  138. */
  139. static const u32 rts5260_sd_pull_ctl_disable_tbl[] = {
  140. RTSX_REG_PAIR(CARD_PULL_CTL1, 0x66),
  141. RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
  142. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5),
  143. RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
  144. 0,
  145. };
  146. /* MS Pull Control Enable:
  147. * MS CD ==> pull up
  148. * others ==> pull down
  149. */
  150. static const u32 rts5260_ms_pull_ctl_enable_tbl[] = {
  151. RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
  152. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
  153. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
  154. 0,
  155. };
  156. /* MS Pull Control Disable:
  157. * MS CD ==> pull up
  158. * others ==> pull down
  159. */
  160. static const u32 rts5260_ms_pull_ctl_disable_tbl[] = {
  161. RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
  162. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
  163. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
  164. 0,
  165. };
  166. static int sd_set_sample_push_timing_sd30(struct rtsx_pcr *pcr)
  167. {
  168. rtsx_pci_write_register(pcr, SD_CFG1, SD_MODE_SELECT_MASK
  169. | SD_ASYNC_FIFO_NOT_RST, SD_30_MODE | SD_ASYNC_FIFO_NOT_RST);
  170. rtsx_pci_write_register(pcr, CLK_CTL, CLK_LOW_FREQ, CLK_LOW_FREQ);
  171. rtsx_pci_write_register(pcr, CARD_CLK_SOURCE, 0xFF,
  172. CRC_VAR_CLK0 | SD30_FIX_CLK | SAMPLE_VAR_CLK1);
  173. rtsx_pci_write_register(pcr, CLK_CTL, CLK_LOW_FREQ, 0);
  174. return 0;
  175. }
  176. static int rts5260_card_power_on(struct rtsx_pcr *pcr, int card)
  177. {
  178. int err = 0;
  179. struct rtsx_cr_option *option = &pcr->option;
  180. if (option->ocp_en)
  181. rtsx_pci_enable_ocp(pcr);
  182. rtsx_pci_init_cmd(pcr);
  183. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_CONFIG2,
  184. DV331812_VDD1, DV331812_VDD1);
  185. err = rtsx_pci_send_cmd(pcr, CMD_TIMEOUT_DEF);
  186. if (err < 0)
  187. return err;
  188. rtsx_pci_init_cmd(pcr);
  189. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_VCC_CFG0,
  190. RTS5260_DVCC_TUNE_MASK, RTS5260_DVCC_33);
  191. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_VCC_CFG1,
  192. LDO_POW_SDVDD1_MASK, LDO_POW_SDVDD1_ON);
  193. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_CONFIG2,
  194. DV331812_POWERON, DV331812_POWERON);
  195. err = rtsx_pci_send_cmd(pcr, CMD_TIMEOUT_DEF);
  196. msleep(20);
  197. if (pcr->extra_caps & EXTRA_CAPS_SD_SDR50 ||
  198. pcr->extra_caps & EXTRA_CAPS_SD_SDR104)
  199. sd_set_sample_push_timing_sd30(pcr);
  200. /* Initialize SD_CFG1 register */
  201. rtsx_pci_write_register(pcr, SD_CFG1, 0xFF,
  202. SD_CLK_DIVIDE_128 | SD_20_MODE);
  203. rtsx_pci_write_register(pcr, SD_SAMPLE_POINT_CTL,
  204. 0xFF, SD20_RX_POS_EDGE);
  205. rtsx_pci_write_register(pcr, SD_PUSH_POINT_CTL, 0xFF, 0);
  206. rtsx_pci_write_register(pcr, CARD_STOP, SD_STOP | SD_CLR_ERR,
  207. SD_STOP | SD_CLR_ERR);
  208. /* Reset SD_CFG3 register */
  209. rtsx_pci_write_register(pcr, SD_CFG3, SD30_CLK_END_EN, 0);
  210. rtsx_pci_write_register(pcr, REG_SD_STOP_SDCLK_CFG,
  211. SD30_CLK_STOP_CFG_EN | SD30_CLK_STOP_CFG1 |
  212. SD30_CLK_STOP_CFG0, 0);
  213. rtsx_pci_write_register(pcr, REG_PRE_RW_MODE, EN_INFINITE_MODE, 0);
  214. return err;
  215. }
  216. static int rts5260_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
  217. {
  218. switch (voltage) {
  219. case OUTPUT_3V3:
  220. rtsx_pci_write_register(pcr, LDO_CONFIG2,
  221. DV331812_VDD1, DV331812_VDD1);
  222. rtsx_pci_write_register(pcr, LDO_DV18_CFG,
  223. DV331812_MASK, DV331812_33);
  224. rtsx_pci_write_register(pcr, SD_PAD_CTL, SD_IO_USING_1V8, 0);
  225. break;
  226. case OUTPUT_1V8:
  227. rtsx_pci_write_register(pcr, LDO_CONFIG2,
  228. DV331812_VDD1, DV331812_VDD1);
  229. rtsx_pci_write_register(pcr, LDO_DV18_CFG,
  230. DV331812_MASK, DV331812_17);
  231. rtsx_pci_write_register(pcr, SD_PAD_CTL, SD_IO_USING_1V8,
  232. SD_IO_USING_1V8);
  233. break;
  234. default:
  235. return -EINVAL;
  236. }
  237. /* set pad drive */
  238. rtsx_pci_init_cmd(pcr);
  239. rts5260_fill_driving(pcr, voltage);
  240. return rtsx_pci_send_cmd(pcr, CMD_TIMEOUT_DEF);
  241. }
  242. static void rts5260_stop_cmd(struct rtsx_pcr *pcr)
  243. {
  244. rtsx_pci_writel(pcr, RTSX_HCBCTLR, STOP_CMD);
  245. rtsx_pci_writel(pcr, RTSX_HDBCTLR, STOP_DMA);
  246. rtsx_pci_write_register(pcr, RTS5260_DMA_RST_CTL_0,
  247. RTS5260_DMA_RST | RTS5260_ADMA3_RST,
  248. RTS5260_DMA_RST | RTS5260_ADMA3_RST);
  249. rtsx_pci_write_register(pcr, RBCTL, RB_FLUSH, RB_FLUSH);
  250. }
  251. static void rts5260_card_before_power_off(struct rtsx_pcr *pcr)
  252. {
  253. struct rtsx_cr_option *option = &pcr->option;
  254. rts5260_stop_cmd(pcr);
  255. rts5260_switch_output_voltage(pcr, OUTPUT_3V3);
  256. if (option->ocp_en)
  257. rtsx_pci_disable_ocp(pcr);
  258. }
  259. static int rts5260_card_power_off(struct rtsx_pcr *pcr, int card)
  260. {
  261. int err = 0;
  262. rts5260_card_before_power_off(pcr);
  263. rtsx_pci_init_cmd(pcr);
  264. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_VCC_CFG1,
  265. LDO_POW_SDVDD1_MASK, LDO_POW_SDVDD1_OFF);
  266. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_CONFIG2,
  267. DV331812_POWERON, DV331812_POWEROFF);
  268. err = rtsx_pci_send_cmd(pcr, CMD_TIMEOUT_DEF);
  269. return err;
  270. }
  271. static void rts5260_init_ocp(struct rtsx_pcr *pcr)
  272. {
  273. struct rtsx_cr_option *option = &pcr->option;
  274. if (option->ocp_en) {
  275. u8 mask, val;
  276. rtsx_pci_write_register(pcr, RTS5260_DVCC_CTRL,
  277. RTS5260_DVCC_OCP_EN |
  278. RTS5260_DVCC_OCP_CL_EN,
  279. RTS5260_DVCC_OCP_EN |
  280. RTS5260_DVCC_OCP_CL_EN);
  281. rtsx_pci_write_register(pcr, RTS5260_DVIO_CTRL,
  282. RTS5260_DVIO_OCP_EN |
  283. RTS5260_DVIO_OCP_CL_EN,
  284. RTS5260_DVIO_OCP_EN |
  285. RTS5260_DVIO_OCP_CL_EN);
  286. rtsx_pci_write_register(pcr, RTS5260_DVCC_CTRL,
  287. RTS5260_DVCC_OCP_THD_MASK,
  288. option->sd_400mA_ocp_thd);
  289. rtsx_pci_write_register(pcr, RTS5260_DVIO_CTRL,
  290. RTS5260_DVIO_OCP_THD_MASK,
  291. RTS5260_DVIO_OCP_THD_350);
  292. rtsx_pci_write_register(pcr, RTS5260_DV331812_CFG,
  293. RTS5260_DV331812_OCP_THD_MASK,
  294. RTS5260_DV331812_OCP_THD_210);
  295. mask = SD_OCP_GLITCH_MASK | SDVIO_OCP_GLITCH_MASK;
  296. val = pcr->hw_param.ocp_glitch;
  297. rtsx_pci_write_register(pcr, REG_OCPGLITCH, mask, val);
  298. rtsx_pci_enable_ocp(pcr);
  299. } else {
  300. rtsx_pci_write_register(pcr, RTS5260_DVCC_CTRL,
  301. RTS5260_DVCC_OCP_EN |
  302. RTS5260_DVCC_OCP_CL_EN, 0);
  303. rtsx_pci_write_register(pcr, RTS5260_DVIO_CTRL,
  304. RTS5260_DVIO_OCP_EN |
  305. RTS5260_DVIO_OCP_CL_EN, 0);
  306. }
  307. }
  308. static void rts5260_enable_ocp(struct rtsx_pcr *pcr)
  309. {
  310. u8 val = 0;
  311. rtsx_pci_write_register(pcr, FPDCTL, OC_POWER_DOWN, 0);
  312. val = SD_OCP_INT_EN | SD_DETECT_EN;
  313. val |= SDVIO_OCP_INT_EN | SDVIO_DETECT_EN;
  314. rtsx_pci_write_register(pcr, REG_OCPCTL, 0xFF, val);
  315. rtsx_pci_write_register(pcr, REG_DV3318_OCPCTL,
  316. DV3318_DETECT_EN | DV3318_OCP_INT_EN,
  317. DV3318_DETECT_EN | DV3318_OCP_INT_EN);
  318. }
  319. static void rts5260_disable_ocp(struct rtsx_pcr *pcr)
  320. {
  321. u8 mask = 0;
  322. mask = SD_OCP_INT_EN | SD_DETECT_EN;
  323. mask |= SDVIO_OCP_INT_EN | SDVIO_DETECT_EN;
  324. rtsx_pci_write_register(pcr, REG_OCPCTL, mask, 0);
  325. rtsx_pci_write_register(pcr, REG_DV3318_OCPCTL,
  326. DV3318_DETECT_EN | DV3318_OCP_INT_EN, 0);
  327. rtsx_pci_write_register(pcr, FPDCTL, OC_POWER_DOWN,
  328. OC_POWER_DOWN);
  329. }
  330. static int rts5260_get_ocpstat(struct rtsx_pcr *pcr, u8 *val)
  331. {
  332. return rtsx_pci_read_register(pcr, REG_OCPSTAT, val);
  333. }
  334. static int rts5260_get_ocpstat2(struct rtsx_pcr *pcr, u8 *val)
  335. {
  336. return rtsx_pci_read_register(pcr, REG_DV3318_OCPSTAT, val);
  337. }
  338. static void rts5260_clear_ocpstat(struct rtsx_pcr *pcr)
  339. {
  340. u8 mask = 0;
  341. u8 val = 0;
  342. mask = SD_OCP_INT_CLR | SD_OC_CLR;
  343. mask |= SDVIO_OCP_INT_CLR | SDVIO_OC_CLR;
  344. val = SD_OCP_INT_CLR | SD_OC_CLR;
  345. val |= SDVIO_OCP_INT_CLR | SDVIO_OC_CLR;
  346. rtsx_pci_write_register(pcr, REG_OCPCTL, mask, val);
  347. rtsx_pci_write_register(pcr, REG_DV3318_OCPCTL,
  348. DV3318_OCP_INT_CLR | DV3318_OCP_CLR,
  349. DV3318_OCP_INT_CLR | DV3318_OCP_CLR);
  350. udelay(10);
  351. rtsx_pci_write_register(pcr, REG_OCPCTL, mask, 0);
  352. rtsx_pci_write_register(pcr, REG_DV3318_OCPCTL,
  353. DV3318_OCP_INT_CLR | DV3318_OCP_CLR, 0);
  354. }
  355. static void rts5260_process_ocp(struct rtsx_pcr *pcr)
  356. {
  357. if (!pcr->option.ocp_en)
  358. return;
  359. rtsx_pci_get_ocpstat(pcr, &pcr->ocp_stat);
  360. rts5260_get_ocpstat2(pcr, &pcr->ocp_stat2);
  361. if (pcr->card_exist & SD_EXIST)
  362. rtsx_sd_power_off_card3v3(pcr);
  363. else if (pcr->card_exist & MS_EXIST)
  364. rtsx_ms_power_off_card3v3(pcr);
  365. if (!(pcr->card_exist & MS_EXIST) && !(pcr->card_exist & SD_EXIST)) {
  366. if ((pcr->ocp_stat & (SD_OC_NOW | SD_OC_EVER |
  367. SDVIO_OC_NOW | SDVIO_OC_EVER)) ||
  368. (pcr->ocp_stat2 & (DV3318_OCP_NOW | DV3318_OCP_EVER)))
  369. rtsx_pci_clear_ocpstat(pcr);
  370. pcr->ocp_stat = 0;
  371. pcr->ocp_stat2 = 0;
  372. }
  373. if ((pcr->ocp_stat & (SD_OC_NOW | SD_OC_EVER |
  374. SDVIO_OC_NOW | SDVIO_OC_EVER)) ||
  375. (pcr->ocp_stat2 & (DV3318_OCP_NOW | DV3318_OCP_EVER))) {
  376. if (pcr->card_exist & SD_EXIST)
  377. rtsx_pci_write_register(pcr, CARD_OE, SD_OUTPUT_EN, 0);
  378. else if (pcr->card_exist & MS_EXIST)
  379. rtsx_pci_write_register(pcr, CARD_OE, MS_OUTPUT_EN, 0);
  380. }
  381. }
  382. static int rts5260_init_hw(struct rtsx_pcr *pcr)
  383. {
  384. int err;
  385. rtsx_pci_init_ocp(pcr);
  386. rtsx_pci_init_cmd(pcr);
  387. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, L1SUB_CONFIG1,
  388. AUX_CLK_ACTIVE_SEL_MASK, MAC_CKSW_DONE);
  389. /* Rest L1SUB Config */
  390. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, L1SUB_CONFIG3, 0xFF, 0x00);
  391. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PM_CLK_FORCE_CTL,
  392. CLK_PM_EN, CLK_PM_EN);
  393. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWD_SUSPEND_EN, 0xFF, 0xFF);
  394. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  395. PWR_GATE_EN, PWR_GATE_EN);
  396. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, REG_VREF,
  397. PWD_SUSPND_EN, PWD_SUSPND_EN);
  398. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, RBCTL,
  399. U_AUTO_DMA_EN_MASK, U_AUTO_DMA_DISABLE);
  400. if (pcr->flags & PCR_REVERSE_SOCKET)
  401. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0xB0, 0xB0);
  402. else
  403. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0xB0, 0x80);
  404. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OBFF_CFG,
  405. OBFF_EN_MASK, OBFF_DISABLE);
  406. err = rtsx_pci_send_cmd(pcr, CMD_TIMEOUT_DEF);
  407. if (err < 0)
  408. return err;
  409. return 0;
  410. }
  411. static void rts5260_pwr_saving_setting(struct rtsx_pcr *pcr)
  412. {
  413. int lss_l1_1, lss_l1_2;
  414. lss_l1_1 = rtsx_check_dev_flag(pcr, ASPM_L1_1_EN)
  415. | rtsx_check_dev_flag(pcr, PM_L1_1_EN);
  416. lss_l1_2 = rtsx_check_dev_flag(pcr, ASPM_L1_2_EN)
  417. | rtsx_check_dev_flag(pcr, PM_L1_2_EN);
  418. if (lss_l1_2) {
  419. pcr_dbg(pcr, "Set parameters for L1.2.");
  420. rtsx_pci_write_register(pcr, PWR_GLOBAL_CTRL,
  421. 0xFF, PCIE_L1_2_EN);
  422. rtsx_pci_write_register(pcr, PWR_FE_CTL,
  423. 0xFF, PCIE_L1_2_PD_FE_EN);
  424. } else if (lss_l1_1) {
  425. pcr_dbg(pcr, "Set parameters for L1.1.");
  426. rtsx_pci_write_register(pcr, PWR_GLOBAL_CTRL,
  427. 0xFF, PCIE_L1_1_EN);
  428. rtsx_pci_write_register(pcr, PWR_FE_CTL,
  429. 0xFF, PCIE_L1_1_PD_FE_EN);
  430. } else {
  431. pcr_dbg(pcr, "Set parameters for L1.");
  432. rtsx_pci_write_register(pcr, PWR_GLOBAL_CTRL,
  433. 0xFF, PCIE_L1_0_EN);
  434. rtsx_pci_write_register(pcr, PWR_FE_CTL,
  435. 0xFF, PCIE_L1_0_PD_FE_EN);
  436. }
  437. rtsx_pci_write_register(pcr, CFG_L1_0_PCIE_DPHY_RET_VALUE,
  438. 0xFF, CFG_L1_0_RET_VALUE_DEFAULT);
  439. rtsx_pci_write_register(pcr, CFG_L1_0_PCIE_MAC_RET_VALUE,
  440. 0xFF, CFG_L1_0_RET_VALUE_DEFAULT);
  441. rtsx_pci_write_register(pcr, CFG_L1_0_CRC_SD30_RET_VALUE,
  442. 0xFF, CFG_L1_0_RET_VALUE_DEFAULT);
  443. rtsx_pci_write_register(pcr, CFG_L1_0_CRC_SD40_RET_VALUE,
  444. 0xFF, CFG_L1_0_RET_VALUE_DEFAULT);
  445. rtsx_pci_write_register(pcr, CFG_L1_0_SYS_RET_VALUE,
  446. 0xFF, CFG_L1_0_RET_VALUE_DEFAULT);
  447. /*Option cut APHY*/
  448. rtsx_pci_write_register(pcr, CFG_PCIE_APHY_OFF_0,
  449. 0xFF, CFG_PCIE_APHY_OFF_0_DEFAULT);
  450. rtsx_pci_write_register(pcr, CFG_PCIE_APHY_OFF_1,
  451. 0xFF, CFG_PCIE_APHY_OFF_1_DEFAULT);
  452. rtsx_pci_write_register(pcr, CFG_PCIE_APHY_OFF_2,
  453. 0xFF, CFG_PCIE_APHY_OFF_2_DEFAULT);
  454. rtsx_pci_write_register(pcr, CFG_PCIE_APHY_OFF_3,
  455. 0xFF, CFG_PCIE_APHY_OFF_3_DEFAULT);
  456. /*CDR DEC*/
  457. rtsx_pci_write_register(pcr, PWC_CDR, 0xFF, PWC_CDR_DEFAULT);
  458. /*PWMPFM*/
  459. rtsx_pci_write_register(pcr, CFG_LP_FPWM_VALUE,
  460. 0xFF, CFG_LP_FPWM_VALUE_DEFAULT);
  461. /*No Power Saving WA*/
  462. rtsx_pci_write_register(pcr, CFG_L1_0_CRC_MISC_RET_VALUE,
  463. 0xFF, CFG_L1_0_CRC_MISC_RET_VALUE_DEFAULT);
  464. }
  465. static void rts5260_init_from_cfg(struct rtsx_pcr *pcr)
  466. {
  467. struct rtsx_cr_option *option = &pcr->option;
  468. u32 lval;
  469. rtsx_pci_read_config_dword(pcr, PCR_ASPM_SETTING_5260, &lval);
  470. if (lval & ASPM_L1_1_EN_MASK)
  471. rtsx_set_dev_flag(pcr, ASPM_L1_1_EN);
  472. if (lval & ASPM_L1_2_EN_MASK)
  473. rtsx_set_dev_flag(pcr, ASPM_L1_2_EN);
  474. if (lval & PM_L1_1_EN_MASK)
  475. rtsx_set_dev_flag(pcr, PM_L1_1_EN);
  476. if (lval & PM_L1_2_EN_MASK)
  477. rtsx_set_dev_flag(pcr, PM_L1_2_EN);
  478. rts5260_pwr_saving_setting(pcr);
  479. if (option->ltr_en) {
  480. u16 val;
  481. pcie_capability_read_word(pcr->pci, PCI_EXP_DEVCTL2, &val);
  482. if (val & PCI_EXP_DEVCTL2_LTR_EN) {
  483. option->ltr_enabled = true;
  484. option->ltr_active = true;
  485. rtsx_set_ltr_latency(pcr, option->ltr_active_latency);
  486. } else {
  487. option->ltr_enabled = false;
  488. }
  489. }
  490. if (rtsx_check_dev_flag(pcr, ASPM_L1_1_EN | ASPM_L1_2_EN
  491. | PM_L1_1_EN | PM_L1_2_EN))
  492. option->force_clkreq_0 = false;
  493. else
  494. option->force_clkreq_0 = true;
  495. }
  496. static int rts5260_extra_init_hw(struct rtsx_pcr *pcr)
  497. {
  498. struct rtsx_cr_option *option = &pcr->option;
  499. /* Set mcu_cnt to 7 to ensure data can be sampled properly */
  500. rtsx_pci_write_register(pcr, 0xFC03, 0x7F, 0x07);
  501. rtsx_pci_write_register(pcr, SSC_DIV_N_0, 0xFF, 0x5D);
  502. rts5260_init_from_cfg(pcr);
  503. /* force no MDIO*/
  504. rtsx_pci_write_register(pcr, RTS5260_AUTOLOAD_CFG4,
  505. 0xFF, RTS5260_MIMO_DISABLE);
  506. /*Modify SDVCC Tune Default Parameters!*/
  507. rtsx_pci_write_register(pcr, LDO_VCC_CFG0,
  508. RTS5260_DVCC_TUNE_MASK, RTS5260_DVCC_33);
  509. rtsx_pci_write_register(pcr, PCLK_CTL, PCLK_MODE_SEL, PCLK_MODE_SEL);
  510. rts5260_init_hw(pcr);
  511. /*
  512. * If u_force_clkreq_0 is enabled, CLKREQ# PIN will be forced
  513. * to drive low, and we forcibly request clock.
  514. */
  515. if (option->force_clkreq_0)
  516. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG,
  517. FORCE_CLKREQ_DELINK_MASK, FORCE_CLKREQ_LOW);
  518. else
  519. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG,
  520. FORCE_CLKREQ_DELINK_MASK, FORCE_CLKREQ_HIGH);
  521. return 0;
  522. }
  523. static void rts5260_set_aspm(struct rtsx_pcr *pcr, bool enable)
  524. {
  525. struct rtsx_cr_option *option = &pcr->option;
  526. u8 val = 0;
  527. if (pcr->aspm_enabled == enable)
  528. return;
  529. if (option->dev_aspm_mode == DEV_ASPM_DYNAMIC) {
  530. if (enable)
  531. val = pcr->aspm_en;
  532. rtsx_pci_update_cfg_byte(pcr, pcr->pcie_cap + PCI_EXP_LNKCTL,
  533. ASPM_MASK_NEG, val);
  534. } else if (option->dev_aspm_mode == DEV_ASPM_BACKDOOR) {
  535. u8 mask = FORCE_ASPM_VAL_MASK | FORCE_ASPM_CTL0;
  536. if (!enable)
  537. val = FORCE_ASPM_CTL0;
  538. rtsx_pci_write_register(pcr, ASPM_FORCE_CTL, mask, val);
  539. }
  540. pcr->aspm_enabled = enable;
  541. }
  542. static void rts5260_set_l1off_cfg_sub_d0(struct rtsx_pcr *pcr, int active)
  543. {
  544. struct rtsx_cr_option *option = &pcr->option;
  545. u32 interrupt = rtsx_pci_readl(pcr, RTSX_BIPR);
  546. int card_exist = (interrupt & SD_EXIST) | (interrupt & MS_EXIST);
  547. int aspm_L1_1, aspm_L1_2;
  548. u8 val = 0;
  549. aspm_L1_1 = rtsx_check_dev_flag(pcr, ASPM_L1_1_EN);
  550. aspm_L1_2 = rtsx_check_dev_flag(pcr, ASPM_L1_2_EN);
  551. if (active) {
  552. /* run, latency: 60us */
  553. if (aspm_L1_1)
  554. val = option->ltr_l1off_snooze_sspwrgate;
  555. } else {
  556. /* l1off, latency: 300us */
  557. if (aspm_L1_2)
  558. val = option->ltr_l1off_sspwrgate;
  559. }
  560. if (aspm_L1_1 || aspm_L1_2) {
  561. if (rtsx_check_dev_flag(pcr,
  562. LTR_L1SS_PWR_GATE_CHECK_CARD_EN)) {
  563. if (card_exist)
  564. val &= ~L1OFF_MBIAS2_EN_5250;
  565. else
  566. val |= L1OFF_MBIAS2_EN_5250;
  567. }
  568. }
  569. rtsx_set_l1off_sub(pcr, val);
  570. }
  571. static const struct pcr_ops rts5260_pcr_ops = {
  572. .fetch_vendor_settings = rtsx_base_fetch_vendor_settings,
  573. .turn_on_led = rts5260_turn_on_led,
  574. .turn_off_led = rts5260_turn_off_led,
  575. .extra_init_hw = rts5260_extra_init_hw,
  576. .enable_auto_blink = rtsx_base_enable_auto_blink,
  577. .disable_auto_blink = rtsx_base_disable_auto_blink,
  578. .card_power_on = rts5260_card_power_on,
  579. .card_power_off = rts5260_card_power_off,
  580. .switch_output_voltage = rts5260_switch_output_voltage,
  581. .force_power_down = rtsx_base_force_power_down,
  582. .stop_cmd = rts5260_stop_cmd,
  583. .set_aspm = rts5260_set_aspm,
  584. .set_l1off_cfg_sub_d0 = rts5260_set_l1off_cfg_sub_d0,
  585. .enable_ocp = rts5260_enable_ocp,
  586. .disable_ocp = rts5260_disable_ocp,
  587. .init_ocp = rts5260_init_ocp,
  588. .process_ocp = rts5260_process_ocp,
  589. .get_ocpstat = rts5260_get_ocpstat,
  590. .clear_ocpstat = rts5260_clear_ocpstat,
  591. };
  592. void rts5260_init_params(struct rtsx_pcr *pcr)
  593. {
  594. struct rtsx_cr_option *option = &pcr->option;
  595. struct rtsx_hw_param *hw_param = &pcr->hw_param;
  596. pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104;
  597. pcr->num_slots = 2;
  598. pcr->flags = 0;
  599. pcr->card_drive_sel = RTSX_CARD_DRIVE_DEFAULT;
  600. pcr->sd30_drive_sel_1v8 = CFG_DRIVER_TYPE_B;
  601. pcr->sd30_drive_sel_3v3 = CFG_DRIVER_TYPE_B;
  602. pcr->aspm_en = ASPM_L1_EN;
  603. pcr->tx_initial_phase = SET_CLOCK_PHASE(27, 29, 11);
  604. pcr->rx_initial_phase = SET_CLOCK_PHASE(24, 6, 5);
  605. pcr->ic_version = rts5260_get_ic_version(pcr);
  606. pcr->sd_pull_ctl_enable_tbl = rts5260_sd_pull_ctl_enable_tbl;
  607. pcr->sd_pull_ctl_disable_tbl = rts5260_sd_pull_ctl_disable_tbl;
  608. pcr->ms_pull_ctl_enable_tbl = rts5260_ms_pull_ctl_enable_tbl;
  609. pcr->ms_pull_ctl_disable_tbl = rts5260_ms_pull_ctl_disable_tbl;
  610. pcr->reg_pm_ctrl3 = RTS524A_PM_CTRL3;
  611. pcr->ops = &rts5260_pcr_ops;
  612. option->dev_flags = (LTR_L1SS_PWR_GATE_CHECK_CARD_EN
  613. | LTR_L1SS_PWR_GATE_EN);
  614. option->ltr_en = true;
  615. /* init latency of active, idle, L1OFF to 60us, 300us, 3ms */
  616. option->ltr_active_latency = LTR_ACTIVE_LATENCY_DEF;
  617. option->ltr_idle_latency = LTR_IDLE_LATENCY_DEF;
  618. option->ltr_l1off_latency = LTR_L1OFF_LATENCY_DEF;
  619. option->dev_aspm_mode = DEV_ASPM_DYNAMIC;
  620. option->l1_snooze_delay = L1_SNOOZE_DELAY_DEF;
  621. option->ltr_l1off_sspwrgate = LTR_L1OFF_SSPWRGATE_5250_DEF;
  622. option->ltr_l1off_snooze_sspwrgate =
  623. LTR_L1OFF_SNOOZE_SSPWRGATE_5250_DEF;
  624. option->ocp_en = 1;
  625. if (option->ocp_en)
  626. hw_param->interrupt_en |= SD_OC_INT_EN;
  627. hw_param->ocp_glitch = SD_OCP_GLITCH_10M | SDVIO_OCP_GLITCH_800U;
  628. option->sd_400mA_ocp_thd = RTS5260_DVCC_OCP_THD_550;
  629. option->sd_800mA_ocp_thd = RTS5260_DVCC_OCP_THD_970;
  630. }