rts5229.c 7.7 KB

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