rts5229.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. */
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/mfd/rtsx_pci.h>
  24. #include "rtsx_pcr.h"
  25. static u8 rts5229_get_ic_version(struct rtsx_pcr *pcr)
  26. {
  27. u8 val;
  28. rtsx_pci_read_register(pcr, DUMMY_REG_RESET_0, &val);
  29. return val & 0x0F;
  30. }
  31. static void rts5229_fetch_vendor_settings(struct rtsx_pcr *pcr)
  32. {
  33. u32 reg;
  34. rtsx_pci_read_config_dword(pcr, PCR_SETTING_REG1, &reg);
  35. pcr_dbg(pcr, "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG1, reg);
  36. if (!rtsx_vendor_setting_valid(reg))
  37. return;
  38. pcr->aspm_en = rtsx_reg_to_aspm(reg);
  39. pcr->sd30_drive_sel_1v8 =
  40. map_sd_drive(rtsx_reg_to_sd30_drive_sel_1v8(reg));
  41. pcr->card_drive_sel &= 0x3F;
  42. pcr->card_drive_sel |= rtsx_reg_to_card_drive_sel(reg);
  43. rtsx_pci_read_config_dword(pcr, PCR_SETTING_REG2, &reg);
  44. pcr_dbg(pcr, "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG2, reg);
  45. pcr->sd30_drive_sel_3v3 =
  46. map_sd_drive(rtsx_reg_to_sd30_drive_sel_3v3(reg));
  47. }
  48. static void rts5229_force_power_down(struct rtsx_pcr *pcr, u8 pm_state)
  49. {
  50. rtsx_pci_write_register(pcr, FPDCTL, 0x03, 0x03);
  51. }
  52. static int rts5229_extra_init_hw(struct rtsx_pcr *pcr)
  53. {
  54. rtsx_pci_init_cmd(pcr);
  55. /* Configure GPIO as output */
  56. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, GPIO_CTL, 0x02, 0x02);
  57. /* Reset ASPM state to default value */
  58. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, ASPM_FORCE_CTL, 0x3F, 0);
  59. /* Force CLKREQ# PIN to drive 0 to request clock */
  60. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0x08, 0x08);
  61. /* Switch LDO3318 source from DV33 to card_3v3 */
  62. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x00);
  63. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x01);
  64. /* LED shine disabled, set initial shine cycle period */
  65. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OLT_LED_CTL, 0x0F, 0x02);
  66. /* Configure driving */
  67. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_DRIVE_SEL,
  68. 0xFF, pcr->sd30_drive_sel_3v3);
  69. return rtsx_pci_send_cmd(pcr, 100);
  70. }
  71. static int rts5229_optimize_phy(struct rtsx_pcr *pcr)
  72. {
  73. /* Optimize RX sensitivity */
  74. return rtsx_pci_write_phy_register(pcr, 0x00, 0xBA42);
  75. }
  76. static int rts5229_turn_on_led(struct rtsx_pcr *pcr)
  77. {
  78. return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x02);
  79. }
  80. static int rts5229_turn_off_led(struct rtsx_pcr *pcr)
  81. {
  82. return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x00);
  83. }
  84. static int rts5229_enable_auto_blink(struct rtsx_pcr *pcr)
  85. {
  86. return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x08);
  87. }
  88. static int rts5229_disable_auto_blink(struct rtsx_pcr *pcr)
  89. {
  90. return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x00);
  91. }
  92. static int rts5229_card_power_on(struct rtsx_pcr *pcr, int card)
  93. {
  94. int err;
  95. rtsx_pci_init_cmd(pcr);
  96. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  97. SD_POWER_MASK, SD_PARTIAL_POWER_ON);
  98. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  99. LDO3318_PWR_MASK, 0x02);
  100. err = rtsx_pci_send_cmd(pcr, 100);
  101. if (err < 0)
  102. return err;
  103. /* To avoid too large in-rush current */
  104. udelay(150);
  105. rtsx_pci_init_cmd(pcr);
  106. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  107. SD_POWER_MASK, SD_POWER_ON);
  108. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  109. LDO3318_PWR_MASK, 0x06);
  110. return rtsx_pci_send_cmd(pcr, 100);
  111. }
  112. static int rts5229_card_power_off(struct rtsx_pcr *pcr, int card)
  113. {
  114. rtsx_pci_init_cmd(pcr);
  115. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  116. SD_POWER_MASK | PMOS_STRG_MASK,
  117. SD_POWER_OFF | PMOS_STRG_400mA);
  118. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  119. LDO3318_PWR_MASK, 0x00);
  120. return rtsx_pci_send_cmd(pcr, 100);
  121. }
  122. static int rts5229_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
  123. {
  124. int err;
  125. if (voltage == OUTPUT_3V3) {
  126. err = rtsx_pci_write_register(pcr,
  127. SD30_DRIVE_SEL, 0x07, pcr->sd30_drive_sel_3v3);
  128. if (err < 0)
  129. return err;
  130. err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4FC0 | 0x24);
  131. if (err < 0)
  132. return err;
  133. } else if (voltage == OUTPUT_1V8) {
  134. err = rtsx_pci_write_register(pcr,
  135. SD30_DRIVE_SEL, 0x07, pcr->sd30_drive_sel_1v8);
  136. if (err < 0)
  137. return err;
  138. err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4C40 | 0x24);
  139. if (err < 0)
  140. return err;
  141. } else {
  142. return -EINVAL;
  143. }
  144. return 0;
  145. }
  146. static const struct pcr_ops rts5229_pcr_ops = {
  147. .fetch_vendor_settings = rts5229_fetch_vendor_settings,
  148. .extra_init_hw = rts5229_extra_init_hw,
  149. .optimize_phy = rts5229_optimize_phy,
  150. .turn_on_led = rts5229_turn_on_led,
  151. .turn_off_led = rts5229_turn_off_led,
  152. .enable_auto_blink = rts5229_enable_auto_blink,
  153. .disable_auto_blink = rts5229_disable_auto_blink,
  154. .card_power_on = rts5229_card_power_on,
  155. .card_power_off = rts5229_card_power_off,
  156. .switch_output_voltage = rts5229_switch_output_voltage,
  157. .cd_deglitch = NULL,
  158. .conv_clk_and_div_n = NULL,
  159. .force_power_down = rts5229_force_power_down,
  160. };
  161. /* SD Pull Control Enable:
  162. * SD_DAT[3:0] ==> pull up
  163. * SD_CD ==> pull up
  164. * SD_WP ==> pull up
  165. * SD_CMD ==> pull up
  166. * SD_CLK ==> pull down
  167. */
  168. static const u32 rts5229_sd_pull_ctl_enable_tbl1[] = {
  169. RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
  170. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9),
  171. 0,
  172. };
  173. /* For RTS5229 version C */
  174. static const u32 rts5229_sd_pull_ctl_enable_tbl2[] = {
  175. RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
  176. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD9),
  177. 0,
  178. };
  179. /* SD Pull Control Disable:
  180. * SD_DAT[3:0] ==> pull down
  181. * SD_CD ==> pull up
  182. * SD_WP ==> pull down
  183. * SD_CMD ==> pull down
  184. * SD_CLK ==> pull down
  185. */
  186. static const u32 rts5229_sd_pull_ctl_disable_tbl1[] = {
  187. RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
  188. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5),
  189. 0,
  190. };
  191. /* For RTS5229 version C */
  192. static const u32 rts5229_sd_pull_ctl_disable_tbl2[] = {
  193. RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
  194. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE5),
  195. 0,
  196. };
  197. /* MS Pull Control Enable:
  198. * MS CD ==> pull up
  199. * others ==> pull down
  200. */
  201. static const u32 rts5229_ms_pull_ctl_enable_tbl[] = {
  202. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
  203. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
  204. 0,
  205. };
  206. /* MS Pull Control Disable:
  207. * MS CD ==> pull up
  208. * others ==> pull down
  209. */
  210. static const u32 rts5229_ms_pull_ctl_disable_tbl[] = {
  211. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
  212. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
  213. 0,
  214. };
  215. void rts5229_init_params(struct rtsx_pcr *pcr)
  216. {
  217. pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104;
  218. pcr->num_slots = 2;
  219. pcr->ops = &rts5229_pcr_ops;
  220. pcr->flags = 0;
  221. pcr->card_drive_sel = RTSX_CARD_DRIVE_DEFAULT;
  222. pcr->sd30_drive_sel_1v8 = DRIVER_TYPE_B;
  223. pcr->sd30_drive_sel_3v3 = DRIVER_TYPE_D;
  224. pcr->aspm_en = ASPM_L1_EN;
  225. pcr->tx_initial_phase = SET_CLOCK_PHASE(27, 27, 15);
  226. pcr->rx_initial_phase = SET_CLOCK_PHASE(30, 6, 6);
  227. pcr->ic_version = rts5229_get_ic_version(pcr);
  228. if (pcr->ic_version == IC_VER_C) {
  229. pcr->sd_pull_ctl_enable_tbl = rts5229_sd_pull_ctl_enable_tbl2;
  230. pcr->sd_pull_ctl_disable_tbl = rts5229_sd_pull_ctl_disable_tbl2;
  231. } else {
  232. pcr->sd_pull_ctl_enable_tbl = rts5229_sd_pull_ctl_enable_tbl1;
  233. pcr->sd_pull_ctl_disable_tbl = rts5229_sd_pull_ctl_disable_tbl1;
  234. }
  235. pcr->ms_pull_ctl_enable_tbl = rts5229_ms_pull_ctl_enable_tbl;
  236. pcr->ms_pull_ctl_disable_tbl = rts5229_ms_pull_ctl_disable_tbl;
  237. }