rts5227.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. dev_dbg(&(pcr->pci->dev), "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. dev_dbg(&(pcr->pci->dev), "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,
  106. AUTOLOAD_CFG_BASE + 3, 0xB8, 0xB8);
  107. else
  108. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
  109. AUTOLOAD_CFG_BASE + 3, 0xB8, 0x88);
  110. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PM_CTRL3, 0x10, 0x00);
  111. return rtsx_pci_send_cmd(pcr, 100);
  112. }
  113. static int rts5227_optimize_phy(struct rtsx_pcr *pcr)
  114. {
  115. int err;
  116. err = rtsx_gops_pm_reset(pcr);
  117. if (err < 0)
  118. return err;
  119. /* Optimize RX sensitivity */
  120. return rtsx_pci_write_phy_register(pcr, 0x00, 0xBA42);
  121. }
  122. static int rts5227_turn_on_led(struct rtsx_pcr *pcr)
  123. {
  124. return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x02);
  125. }
  126. static int rts5227_turn_off_led(struct rtsx_pcr *pcr)
  127. {
  128. return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x00);
  129. }
  130. static int rts5227_enable_auto_blink(struct rtsx_pcr *pcr)
  131. {
  132. return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x08);
  133. }
  134. static int rts5227_disable_auto_blink(struct rtsx_pcr *pcr)
  135. {
  136. return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x00);
  137. }
  138. static int rts5227_card_power_on(struct rtsx_pcr *pcr, int card)
  139. {
  140. int err;
  141. rtsx_pci_init_cmd(pcr);
  142. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  143. SD_POWER_MASK, SD_PARTIAL_POWER_ON);
  144. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  145. LDO3318_PWR_MASK, 0x02);
  146. err = rtsx_pci_send_cmd(pcr, 100);
  147. if (err < 0)
  148. return err;
  149. /* To avoid too large in-rush current */
  150. udelay(150);
  151. rtsx_pci_init_cmd(pcr);
  152. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  153. SD_POWER_MASK, SD_POWER_ON);
  154. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  155. LDO3318_PWR_MASK, 0x06);
  156. err = rtsx_pci_send_cmd(pcr, 100);
  157. if (err < 0)
  158. return err;
  159. return 0;
  160. }
  161. static int rts5227_card_power_off(struct rtsx_pcr *pcr, int card)
  162. {
  163. rtsx_pci_init_cmd(pcr);
  164. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  165. SD_POWER_MASK | PMOS_STRG_MASK,
  166. SD_POWER_OFF | PMOS_STRG_400mA);
  167. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  168. LDO3318_PWR_MASK, 0X00);
  169. return rtsx_pci_send_cmd(pcr, 100);
  170. }
  171. static int rts5227_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
  172. {
  173. int err;
  174. if (voltage == OUTPUT_3V3) {
  175. err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4FC0 | 0x24);
  176. if (err < 0)
  177. return err;
  178. } else if (voltage == OUTPUT_1V8) {
  179. err = rtsx_pci_write_phy_register(pcr, 0x11, 0x3C02);
  180. if (err < 0)
  181. return err;
  182. err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4C80 | 0x24);
  183. if (err < 0)
  184. return err;
  185. } else {
  186. return -EINVAL;
  187. }
  188. /* set pad drive */
  189. rtsx_pci_init_cmd(pcr);
  190. rts5227_fill_driving(pcr, voltage);
  191. return rtsx_pci_send_cmd(pcr, 100);
  192. }
  193. static const struct pcr_ops rts5227_pcr_ops = {
  194. .fetch_vendor_settings = rts5227_fetch_vendor_settings,
  195. .extra_init_hw = rts5227_extra_init_hw,
  196. .optimize_phy = rts5227_optimize_phy,
  197. .turn_on_led = rts5227_turn_on_led,
  198. .turn_off_led = rts5227_turn_off_led,
  199. .enable_auto_blink = rts5227_enable_auto_blink,
  200. .disable_auto_blink = rts5227_disable_auto_blink,
  201. .card_power_on = rts5227_card_power_on,
  202. .card_power_off = rts5227_card_power_off,
  203. .switch_output_voltage = rts5227_switch_output_voltage,
  204. .cd_deglitch = NULL,
  205. .conv_clk_and_div_n = NULL,
  206. .force_power_down = rts5227_force_power_down,
  207. };
  208. /* SD Pull Control Enable:
  209. * SD_DAT[3:0] ==> pull up
  210. * SD_CD ==> pull up
  211. * SD_WP ==> pull up
  212. * SD_CMD ==> pull up
  213. * SD_CLK ==> pull down
  214. */
  215. static const u32 rts5227_sd_pull_ctl_enable_tbl[] = {
  216. RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
  217. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9),
  218. 0,
  219. };
  220. /* SD Pull Control Disable:
  221. * SD_DAT[3:0] ==> pull down
  222. * SD_CD ==> pull up
  223. * SD_WP ==> pull down
  224. * SD_CMD ==> pull down
  225. * SD_CLK ==> pull down
  226. */
  227. static const u32 rts5227_sd_pull_ctl_disable_tbl[] = {
  228. RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
  229. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5),
  230. 0,
  231. };
  232. /* MS Pull Control Enable:
  233. * MS CD ==> pull up
  234. * others ==> pull down
  235. */
  236. static const u32 rts5227_ms_pull_ctl_enable_tbl[] = {
  237. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
  238. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
  239. 0,
  240. };
  241. /* MS Pull Control Disable:
  242. * MS CD ==> pull up
  243. * others ==> pull down
  244. */
  245. static const u32 rts5227_ms_pull_ctl_disable_tbl[] = {
  246. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
  247. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
  248. 0,
  249. };
  250. void rts5227_init_params(struct rtsx_pcr *pcr)
  251. {
  252. pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104;
  253. pcr->num_slots = 2;
  254. pcr->ops = &rts5227_pcr_ops;
  255. pcr->flags = 0;
  256. pcr->card_drive_sel = RTSX_CARD_DRIVE_DEFAULT;
  257. pcr->sd30_drive_sel_1v8 = CFG_DRIVER_TYPE_B;
  258. pcr->sd30_drive_sel_3v3 = CFG_DRIVER_TYPE_B;
  259. pcr->aspm_en = ASPM_L1_EN;
  260. pcr->tx_initial_phase = SET_CLOCK_PHASE(27, 27, 15);
  261. pcr->rx_initial_phase = SET_CLOCK_PHASE(30, 7, 7);
  262. pcr->sd_pull_ctl_enable_tbl = rts5227_sd_pull_ctl_enable_tbl;
  263. pcr->sd_pull_ctl_disable_tbl = rts5227_sd_pull_ctl_disable_tbl;
  264. pcr->ms_pull_ctl_enable_tbl = rts5227_ms_pull_ctl_enable_tbl;
  265. pcr->ms_pull_ctl_disable_tbl = rts5227_ms_pull_ctl_disable_tbl;
  266. }