rts5227.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* Driver for Realtek PCI-Express card reader
  2. *
  3. * Copyright(c) 2009-2013 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. * Wei WANG <wei_wang@realsil.com.cn>
  20. * Roger Tseng <rogerable@realtek.com>
  21. */
  22. #include <linux/module.h>
  23. #include <linux/delay.h>
  24. #include <linux/mfd/rtsx_pci.h>
  25. #include "rtsx_pcr.h"
  26. static void rts5227_fill_driving(struct rtsx_pcr *pcr, u8 voltage)
  27. {
  28. u8 driving_3v3[4][3] = {
  29. {0x13, 0x13, 0x13},
  30. {0x96, 0x96, 0x96},
  31. {0x7F, 0x7F, 0x7F},
  32. {0x96, 0x96, 0x96},
  33. };
  34. u8 driving_1v8[4][3] = {
  35. {0x99, 0x99, 0x99},
  36. {0xAA, 0xAA, 0xAA},
  37. {0xFE, 0xFE, 0xFE},
  38. {0xB3, 0xB3, 0xB3},
  39. };
  40. u8 (*driving)[3], drive_sel;
  41. if (voltage == OUTPUT_3V3) {
  42. driving = driving_3v3;
  43. drive_sel = pcr->sd30_drive_sel_3v3;
  44. } else {
  45. driving = driving_1v8;
  46. drive_sel = pcr->sd30_drive_sel_1v8;
  47. }
  48. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_CLK_DRIVE_SEL,
  49. 0xFF, driving[drive_sel][0]);
  50. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_CMD_DRIVE_SEL,
  51. 0xFF, driving[drive_sel][1]);
  52. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_DAT_DRIVE_SEL,
  53. 0xFF, driving[drive_sel][2]);
  54. }
  55. static void rts5227_fetch_vendor_settings(struct rtsx_pcr *pcr)
  56. {
  57. u32 reg;
  58. rtsx_pci_read_config_dword(pcr, PCR_SETTING_REG1, &reg);
  59. pcr_dbg(pcr, "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG1, reg);
  60. if (!rtsx_vendor_setting_valid(reg))
  61. return;
  62. pcr->aspm_en = rtsx_reg_to_aspm(reg);
  63. pcr->sd30_drive_sel_1v8 = rtsx_reg_to_sd30_drive_sel_1v8(reg);
  64. pcr->card_drive_sel &= 0x3F;
  65. pcr->card_drive_sel |= rtsx_reg_to_card_drive_sel(reg);
  66. rtsx_pci_read_config_dword(pcr, PCR_SETTING_REG2, &reg);
  67. pcr_dbg(pcr, "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG2, reg);
  68. pcr->sd30_drive_sel_3v3 = rtsx_reg_to_sd30_drive_sel_3v3(reg);
  69. if (rtsx_reg_check_reverse_socket(reg))
  70. pcr->flags |= PCR_REVERSE_SOCKET;
  71. }
  72. static void rts5227_force_power_down(struct rtsx_pcr *pcr, u8 pm_state)
  73. {
  74. /* Set relink_time to 0 */
  75. rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 1, 0xFF, 0);
  76. rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 2, 0xFF, 0);
  77. rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 3, 0x01, 0);
  78. if (pm_state == HOST_ENTER_S3)
  79. rtsx_pci_write_register(pcr, PM_CTRL3, 0x10, 0x10);
  80. rtsx_pci_write_register(pcr, FPDCTL, 0x03, 0x03);
  81. }
  82. static int rts5227_extra_init_hw(struct rtsx_pcr *pcr)
  83. {
  84. u16 cap;
  85. rtsx_pci_init_cmd(pcr);
  86. /* Configure GPIO as output */
  87. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, GPIO_CTL, 0x02, 0x02);
  88. /* Reset ASPM state to default value */
  89. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, ASPM_FORCE_CTL, 0x3F, 0);
  90. /* Switch LDO3318 source from DV33 to card_3v3 */
  91. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x00);
  92. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x01);
  93. /* LED shine disabled, set initial shine cycle period */
  94. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OLT_LED_CTL, 0x0F, 0x02);
  95. /* Configure LTR */
  96. pcie_capability_read_word(pcr->pci, PCI_EXP_DEVCTL2, &cap);
  97. if (cap & PCI_EXP_DEVCTL2_LTR_EN)
  98. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LTR_CTL, 0xFF, 0xA3);
  99. /* Configure OBFF */
  100. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OBFF_CFG, 0x03, 0x03);
  101. /* Configure driving */
  102. rts5227_fill_driving(pcr, OUTPUT_3V3);
  103. /* Configure force_clock_req */
  104. if (pcr->flags & PCR_REVERSE_SOCKET)
  105. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0xB8, 0xB8);
  106. else
  107. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0xB8, 0x88);
  108. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PM_CTRL3, 0x10, 0x00);
  109. return rtsx_pci_send_cmd(pcr, 100);
  110. }
  111. static int rts5227_optimize_phy(struct rtsx_pcr *pcr)
  112. {
  113. int err;
  114. err = rtsx_pci_write_register(pcr, PM_CTRL3, D3_DELINK_MODE_EN, 0x00);
  115. if (err < 0)
  116. return err;
  117. /* Optimize RX sensitivity */
  118. return rtsx_pci_write_phy_register(pcr, 0x00, 0xBA42);
  119. }
  120. static int rts5227_turn_on_led(struct rtsx_pcr *pcr)
  121. {
  122. return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x02);
  123. }
  124. static int rts5227_turn_off_led(struct rtsx_pcr *pcr)
  125. {
  126. return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x00);
  127. }
  128. static int rts5227_enable_auto_blink(struct rtsx_pcr *pcr)
  129. {
  130. return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x08);
  131. }
  132. static int rts5227_disable_auto_blink(struct rtsx_pcr *pcr)
  133. {
  134. return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x00);
  135. }
  136. static int rts5227_card_power_on(struct rtsx_pcr *pcr, int card)
  137. {
  138. int err;
  139. rtsx_pci_init_cmd(pcr);
  140. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  141. SD_POWER_MASK, SD_PARTIAL_POWER_ON);
  142. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  143. LDO3318_PWR_MASK, 0x02);
  144. err = rtsx_pci_send_cmd(pcr, 100);
  145. if (err < 0)
  146. return err;
  147. /* To avoid too large in-rush current */
  148. udelay(150);
  149. rtsx_pci_init_cmd(pcr);
  150. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  151. SD_POWER_MASK, SD_POWER_ON);
  152. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  153. LDO3318_PWR_MASK, 0x06);
  154. err = rtsx_pci_send_cmd(pcr, 100);
  155. if (err < 0)
  156. return err;
  157. return 0;
  158. }
  159. static int rts5227_card_power_off(struct rtsx_pcr *pcr, int card)
  160. {
  161. rtsx_pci_init_cmd(pcr);
  162. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  163. SD_POWER_MASK | PMOS_STRG_MASK,
  164. SD_POWER_OFF | PMOS_STRG_400mA);
  165. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  166. LDO3318_PWR_MASK, 0X00);
  167. return rtsx_pci_send_cmd(pcr, 100);
  168. }
  169. static int rts5227_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
  170. {
  171. int err;
  172. if (voltage == OUTPUT_3V3) {
  173. err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4FC0 | 0x24);
  174. if (err < 0)
  175. return err;
  176. } else if (voltage == OUTPUT_1V8) {
  177. err = rtsx_pci_write_phy_register(pcr, 0x11, 0x3C02);
  178. if (err < 0)
  179. return err;
  180. err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4C80 | 0x24);
  181. if (err < 0)
  182. return err;
  183. } else {
  184. return -EINVAL;
  185. }
  186. /* set pad drive */
  187. rtsx_pci_init_cmd(pcr);
  188. rts5227_fill_driving(pcr, voltage);
  189. return rtsx_pci_send_cmd(pcr, 100);
  190. }
  191. static const struct pcr_ops rts5227_pcr_ops = {
  192. .fetch_vendor_settings = rts5227_fetch_vendor_settings,
  193. .extra_init_hw = rts5227_extra_init_hw,
  194. .optimize_phy = rts5227_optimize_phy,
  195. .turn_on_led = rts5227_turn_on_led,
  196. .turn_off_led = rts5227_turn_off_led,
  197. .enable_auto_blink = rts5227_enable_auto_blink,
  198. .disable_auto_blink = rts5227_disable_auto_blink,
  199. .card_power_on = rts5227_card_power_on,
  200. .card_power_off = rts5227_card_power_off,
  201. .switch_output_voltage = rts5227_switch_output_voltage,
  202. .cd_deglitch = NULL,
  203. .conv_clk_and_div_n = NULL,
  204. .force_power_down = rts5227_force_power_down,
  205. };
  206. /* SD Pull Control Enable:
  207. * SD_DAT[3:0] ==> pull up
  208. * SD_CD ==> pull up
  209. * SD_WP ==> pull up
  210. * SD_CMD ==> pull up
  211. * SD_CLK ==> pull down
  212. */
  213. static const u32 rts5227_sd_pull_ctl_enable_tbl[] = {
  214. RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
  215. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9),
  216. 0,
  217. };
  218. /* SD Pull Control Disable:
  219. * SD_DAT[3:0] ==> pull down
  220. * SD_CD ==> pull up
  221. * SD_WP ==> pull down
  222. * SD_CMD ==> pull down
  223. * SD_CLK ==> pull down
  224. */
  225. static const u32 rts5227_sd_pull_ctl_disable_tbl[] = {
  226. RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
  227. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5),
  228. 0,
  229. };
  230. /* MS Pull Control Enable:
  231. * MS CD ==> pull up
  232. * others ==> pull down
  233. */
  234. static const u32 rts5227_ms_pull_ctl_enable_tbl[] = {
  235. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
  236. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
  237. 0,
  238. };
  239. /* MS Pull Control Disable:
  240. * MS CD ==> pull up
  241. * others ==> pull down
  242. */
  243. static const u32 rts5227_ms_pull_ctl_disable_tbl[] = {
  244. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
  245. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
  246. 0,
  247. };
  248. void rts5227_init_params(struct rtsx_pcr *pcr)
  249. {
  250. pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104;
  251. pcr->num_slots = 2;
  252. pcr->ops = &rts5227_pcr_ops;
  253. pcr->flags = 0;
  254. pcr->card_drive_sel = RTSX_CARD_DRIVE_DEFAULT;
  255. pcr->sd30_drive_sel_1v8 = CFG_DRIVER_TYPE_B;
  256. pcr->sd30_drive_sel_3v3 = CFG_DRIVER_TYPE_B;
  257. pcr->aspm_en = ASPM_L1_EN;
  258. pcr->tx_initial_phase = SET_CLOCK_PHASE(27, 27, 15);
  259. pcr->rx_initial_phase = SET_CLOCK_PHASE(30, 7, 7);
  260. pcr->sd_pull_ctl_enable_tbl = rts5227_sd_pull_ctl_enable_tbl;
  261. pcr->sd_pull_ctl_disable_tbl = rts5227_sd_pull_ctl_disable_tbl;
  262. pcr->ms_pull_ctl_enable_tbl = rts5227_ms_pull_ctl_enable_tbl;
  263. pcr->ms_pull_ctl_disable_tbl = rts5227_ms_pull_ctl_disable_tbl;
  264. }