rts5209.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 rts5209_get_ic_version(struct rtsx_pcr *pcr)
  26. {
  27. u8 val;
  28. val = rtsx_pci_readb(pcr, 0x1C);
  29. return val & 0x0F;
  30. }
  31. static void rts5209_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 (rts5209_vendor_setting1_valid(reg)) {
  37. if (rts5209_reg_check_ms_pmos(reg))
  38. pcr->flags |= PCR_MS_PMOS;
  39. pcr->aspm_en = rts5209_reg_to_aspm(reg);
  40. }
  41. rtsx_pci_read_config_dword(pcr, PCR_SETTING_REG2, &reg);
  42. pcr_dbg(pcr, "Cfg 0x%x: 0x%x\n", PCR_SETTING_REG2, reg);
  43. if (rts5209_vendor_setting2_valid(reg)) {
  44. pcr->sd30_drive_sel_1v8 =
  45. rts5209_reg_to_sd30_drive_sel_1v8(reg);
  46. pcr->sd30_drive_sel_3v3 =
  47. rts5209_reg_to_sd30_drive_sel_3v3(reg);
  48. pcr->card_drive_sel = rts5209_reg_to_card_drive_sel(reg);
  49. }
  50. }
  51. static void rts5209_force_power_down(struct rtsx_pcr *pcr, u8 pm_state)
  52. {
  53. rtsx_pci_write_register(pcr, FPDCTL, 0x07, 0x07);
  54. }
  55. static int rts5209_extra_init_hw(struct rtsx_pcr *pcr)
  56. {
  57. rtsx_pci_init_cmd(pcr);
  58. /* Turn off LED */
  59. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_GPIO, 0xFF, 0x03);
  60. /* Reset ASPM state to default value */
  61. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, ASPM_FORCE_CTL, 0x3F, 0);
  62. /* Force CLKREQ# PIN to drive 0 to request clock */
  63. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0x08, 0x08);
  64. /* Configure GPIO as output */
  65. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_GPIO_DIR, 0xFF, 0x03);
  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 rts5209_optimize_phy(struct rtsx_pcr *pcr)
  72. {
  73. return rtsx_pci_write_phy_register(pcr, 0x00, 0xB966);
  74. }
  75. static int rts5209_turn_on_led(struct rtsx_pcr *pcr)
  76. {
  77. return rtsx_pci_write_register(pcr, CARD_GPIO, 0x01, 0x00);
  78. }
  79. static int rts5209_turn_off_led(struct rtsx_pcr *pcr)
  80. {
  81. return rtsx_pci_write_register(pcr, CARD_GPIO, 0x01, 0x01);
  82. }
  83. static int rts5209_enable_auto_blink(struct rtsx_pcr *pcr)
  84. {
  85. return rtsx_pci_write_register(pcr, CARD_AUTO_BLINK, 0xFF, 0x0D);
  86. }
  87. static int rts5209_disable_auto_blink(struct rtsx_pcr *pcr)
  88. {
  89. return rtsx_pci_write_register(pcr, CARD_AUTO_BLINK, 0x08, 0x00);
  90. }
  91. static int rts5209_card_power_on(struct rtsx_pcr *pcr, int card)
  92. {
  93. int err;
  94. u8 pwr_mask, partial_pwr_on, pwr_on;
  95. pwr_mask = SD_POWER_MASK;
  96. partial_pwr_on = SD_PARTIAL_POWER_ON;
  97. pwr_on = SD_POWER_ON;
  98. if ((pcr->flags & PCR_MS_PMOS) && (card == RTSX_MS_CARD)) {
  99. pwr_mask = MS_POWER_MASK;
  100. partial_pwr_on = MS_PARTIAL_POWER_ON;
  101. pwr_on = MS_POWER_ON;
  102. }
  103. rtsx_pci_init_cmd(pcr);
  104. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  105. pwr_mask, partial_pwr_on);
  106. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  107. LDO3318_PWR_MASK, 0x04);
  108. err = rtsx_pci_send_cmd(pcr, 100);
  109. if (err < 0)
  110. return err;
  111. /* To avoid too large in-rush current */
  112. udelay(150);
  113. rtsx_pci_init_cmd(pcr);
  114. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL, pwr_mask, pwr_on);
  115. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  116. LDO3318_PWR_MASK, 0x00);
  117. return rtsx_pci_send_cmd(pcr, 100);
  118. }
  119. static int rts5209_card_power_off(struct rtsx_pcr *pcr, int card)
  120. {
  121. u8 pwr_mask, pwr_off;
  122. pwr_mask = SD_POWER_MASK;
  123. pwr_off = SD_POWER_OFF;
  124. if ((pcr->flags & PCR_MS_PMOS) && (card == RTSX_MS_CARD)) {
  125. pwr_mask = MS_POWER_MASK;
  126. pwr_off = MS_POWER_OFF;
  127. }
  128. rtsx_pci_init_cmd(pcr);
  129. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  130. pwr_mask | PMOS_STRG_MASK, pwr_off | PMOS_STRG_400mA);
  131. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  132. LDO3318_PWR_MASK, 0x06);
  133. return rtsx_pci_send_cmd(pcr, 100);
  134. }
  135. static int rts5209_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
  136. {
  137. int err;
  138. if (voltage == OUTPUT_3V3) {
  139. err = rtsx_pci_write_register(pcr,
  140. SD30_DRIVE_SEL, 0x07, pcr->sd30_drive_sel_3v3);
  141. if (err < 0)
  142. return err;
  143. err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4FC0 | 0x24);
  144. if (err < 0)
  145. return err;
  146. } else if (voltage == OUTPUT_1V8) {
  147. err = rtsx_pci_write_register(pcr,
  148. SD30_DRIVE_SEL, 0x07, pcr->sd30_drive_sel_1v8);
  149. if (err < 0)
  150. return err;
  151. err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4C40 | 0x24);
  152. if (err < 0)
  153. return err;
  154. } else {
  155. return -EINVAL;
  156. }
  157. return 0;
  158. }
  159. static const struct pcr_ops rts5209_pcr_ops = {
  160. .fetch_vendor_settings = rts5209_fetch_vendor_settings,
  161. .extra_init_hw = rts5209_extra_init_hw,
  162. .optimize_phy = rts5209_optimize_phy,
  163. .turn_on_led = rts5209_turn_on_led,
  164. .turn_off_led = rts5209_turn_off_led,
  165. .enable_auto_blink = rts5209_enable_auto_blink,
  166. .disable_auto_blink = rts5209_disable_auto_blink,
  167. .card_power_on = rts5209_card_power_on,
  168. .card_power_off = rts5209_card_power_off,
  169. .switch_output_voltage = rts5209_switch_output_voltage,
  170. .cd_deglitch = NULL,
  171. .conv_clk_and_div_n = NULL,
  172. .force_power_down = rts5209_force_power_down,
  173. };
  174. /* SD Pull Control Enable:
  175. * SD_DAT[3:0] ==> pull up
  176. * SD_CD ==> pull up
  177. * SD_WP ==> pull up
  178. * SD_CMD ==> pull up
  179. * SD_CLK ==> pull down
  180. */
  181. static const u32 rts5209_sd_pull_ctl_enable_tbl[] = {
  182. RTSX_REG_PAIR(CARD_PULL_CTL1, 0xAA),
  183. RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
  184. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9),
  185. 0,
  186. };
  187. /* SD Pull Control Disable:
  188. * SD_DAT[3:0] ==> pull down
  189. * SD_CD ==> pull up
  190. * SD_WP ==> pull down
  191. * SD_CMD ==> pull down
  192. * SD_CLK ==> pull down
  193. */
  194. static const u32 rts5209_sd_pull_ctl_disable_tbl[] = {
  195. RTSX_REG_PAIR(CARD_PULL_CTL1, 0x55),
  196. RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
  197. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5),
  198. 0,
  199. };
  200. /* MS Pull Control Enable:
  201. * MS CD ==> pull up
  202. * others ==> pull down
  203. */
  204. static const u32 rts5209_ms_pull_ctl_enable_tbl[] = {
  205. RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
  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 rts5209_ms_pull_ctl_disable_tbl[] = {
  215. RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
  216. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
  217. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
  218. 0,
  219. };
  220. void rts5209_init_params(struct rtsx_pcr *pcr)
  221. {
  222. pcr->extra_caps = EXTRA_CAPS_SD_SDR50 |
  223. EXTRA_CAPS_SD_SDR104 | EXTRA_CAPS_MMC_8BIT;
  224. pcr->num_slots = 2;
  225. pcr->ops = &rts5209_pcr_ops;
  226. pcr->flags = 0;
  227. pcr->card_drive_sel = RTS5209_CARD_DRIVE_DEFAULT;
  228. pcr->sd30_drive_sel_1v8 = DRIVER_TYPE_B;
  229. pcr->sd30_drive_sel_3v3 = DRIVER_TYPE_D;
  230. pcr->aspm_en = ASPM_L1_EN;
  231. pcr->tx_initial_phase = SET_CLOCK_PHASE(27, 27, 16);
  232. pcr->rx_initial_phase = SET_CLOCK_PHASE(24, 6, 5);
  233. pcr->ic_version = rts5209_get_ic_version(pcr);
  234. pcr->sd_pull_ctl_enable_tbl = rts5209_sd_pull_ctl_enable_tbl;
  235. pcr->sd_pull_ctl_disable_tbl = rts5209_sd_pull_ctl_disable_tbl;
  236. pcr->ms_pull_ctl_enable_tbl = rts5209_ms_pull_ctl_enable_tbl;
  237. pcr->ms_pull_ctl_disable_tbl = rts5209_ms_pull_ctl_disable_tbl;
  238. }