rts5227.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. * Roger Tseng <rogerable@realtek.com>
  23. * No. 2, Innovation Road II, Hsinchu Science Park, Hsinchu 300, Taiwan
  24. */
  25. #include <linux/module.h>
  26. #include <linux/delay.h>
  27. #include <linux/mfd/rtsx_pci.h>
  28. #include "rtsx_pcr.h"
  29. static int rts5227_extra_init_hw(struct rtsx_pcr *pcr)
  30. {
  31. u16 cap;
  32. rtsx_pci_init_cmd(pcr);
  33. /* Configure GPIO as output */
  34. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, GPIO_CTL, 0x02, 0x02);
  35. /* Switch LDO3318 source from DV33 to card_3v3 */
  36. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x00);
  37. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x01);
  38. /* LED shine disabled, set initial shine cycle period */
  39. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OLT_LED_CTL, 0x0F, 0x02);
  40. /* Configure LTR */
  41. pcie_capability_read_word(pcr->pci, PCI_EXP_DEVCTL2, &cap);
  42. if (cap & PCI_EXP_LTR_EN)
  43. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LTR_CTL, 0xFF, 0xA3);
  44. /* Configure OBFF */
  45. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OBFF_CFG, 0x03, 0x03);
  46. /* Configure force_clock_req
  47. * Maybe We should define 0xFF03 as some name
  48. */
  49. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, 0xFF03, 0x08, 0x08);
  50. /* Correct driving */
  51. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
  52. SD30_CLK_DRIVE_SEL, 0xFF, 0x96);
  53. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
  54. SD30_CMD_DRIVE_SEL, 0xFF, 0x96);
  55. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
  56. SD30_DAT_DRIVE_SEL, 0xFF, 0x96);
  57. return rtsx_pci_send_cmd(pcr, 100);
  58. }
  59. static int rts5227_optimize_phy(struct rtsx_pcr *pcr)
  60. {
  61. /* Optimize RX sensitivity */
  62. return rtsx_pci_write_phy_register(pcr, 0x00, 0xBA42);
  63. }
  64. static int rts5227_turn_on_led(struct rtsx_pcr *pcr)
  65. {
  66. return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x02);
  67. }
  68. static int rts5227_turn_off_led(struct rtsx_pcr *pcr)
  69. {
  70. return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x00);
  71. }
  72. static int rts5227_enable_auto_blink(struct rtsx_pcr *pcr)
  73. {
  74. return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x08);
  75. }
  76. static int rts5227_disable_auto_blink(struct rtsx_pcr *pcr)
  77. {
  78. return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x00);
  79. }
  80. static int rts5227_card_power_on(struct rtsx_pcr *pcr, int card)
  81. {
  82. int err;
  83. rtsx_pci_init_cmd(pcr);
  84. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  85. SD_POWER_MASK, SD_PARTIAL_POWER_ON);
  86. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  87. LDO3318_PWR_MASK, 0x02);
  88. err = rtsx_pci_send_cmd(pcr, 100);
  89. if (err < 0)
  90. return err;
  91. /* To avoid too large in-rush current */
  92. udelay(150);
  93. rtsx_pci_init_cmd(pcr);
  94. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  95. SD_POWER_MASK, SD_POWER_ON);
  96. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  97. LDO3318_PWR_MASK, 0x06);
  98. err = rtsx_pci_send_cmd(pcr, 100);
  99. if (err < 0)
  100. return err;
  101. return 0;
  102. }
  103. static int rts5227_card_power_off(struct rtsx_pcr *pcr, int card)
  104. {
  105. rtsx_pci_init_cmd(pcr);
  106. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  107. SD_POWER_MASK | PMOS_STRG_MASK,
  108. SD_POWER_OFF | PMOS_STRG_400mA);
  109. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  110. LDO3318_PWR_MASK, 0X00);
  111. return rtsx_pci_send_cmd(pcr, 100);
  112. }
  113. static int rts5227_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
  114. {
  115. int err;
  116. u8 drive_sel;
  117. if (voltage == OUTPUT_3V3) {
  118. err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4FC0 | 0x24);
  119. if (err < 0)
  120. return err;
  121. drive_sel = 0x96;
  122. } else if (voltage == OUTPUT_1V8) {
  123. err = rtsx_pci_write_phy_register(pcr, 0x11, 0x3C02);
  124. if (err < 0)
  125. return err;
  126. err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4C80 | 0x24);
  127. if (err < 0)
  128. return err;
  129. drive_sel = 0xB3;
  130. } else {
  131. return -EINVAL;
  132. }
  133. /* set pad drive */
  134. rtsx_pci_init_cmd(pcr);
  135. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_CLK_DRIVE_SEL,
  136. 0xFF, drive_sel);
  137. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_CMD_DRIVE_SEL,
  138. 0xFF, drive_sel);
  139. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_DAT_DRIVE_SEL,
  140. 0xFF, drive_sel);
  141. return rtsx_pci_send_cmd(pcr, 100);
  142. }
  143. static const struct pcr_ops rts5227_pcr_ops = {
  144. .extra_init_hw = rts5227_extra_init_hw,
  145. .optimize_phy = rts5227_optimize_phy,
  146. .turn_on_led = rts5227_turn_on_led,
  147. .turn_off_led = rts5227_turn_off_led,
  148. .enable_auto_blink = rts5227_enable_auto_blink,
  149. .disable_auto_blink = rts5227_disable_auto_blink,
  150. .card_power_on = rts5227_card_power_on,
  151. .card_power_off = rts5227_card_power_off,
  152. .switch_output_voltage = rts5227_switch_output_voltage,
  153. .cd_deglitch = NULL,
  154. .conv_clk_and_div_n = NULL,
  155. };
  156. /* SD Pull Control Enable:
  157. * SD_DAT[3:0] ==> pull up
  158. * SD_CD ==> pull up
  159. * SD_WP ==> pull up
  160. * SD_CMD ==> pull up
  161. * SD_CLK ==> pull down
  162. */
  163. static const u32 rts5227_sd_pull_ctl_enable_tbl[] = {
  164. RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
  165. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9),
  166. 0,
  167. };
  168. /* SD Pull Control Disable:
  169. * SD_DAT[3:0] ==> pull down
  170. * SD_CD ==> pull up
  171. * SD_WP ==> pull down
  172. * SD_CMD ==> pull down
  173. * SD_CLK ==> pull down
  174. */
  175. static const u32 rts5227_sd_pull_ctl_disable_tbl[] = {
  176. RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
  177. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5),
  178. 0,
  179. };
  180. /* MS Pull Control Enable:
  181. * MS CD ==> pull up
  182. * others ==> pull down
  183. */
  184. static const u32 rts5227_ms_pull_ctl_enable_tbl[] = {
  185. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
  186. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
  187. 0,
  188. };
  189. /* MS Pull Control Disable:
  190. * MS CD ==> pull up
  191. * others ==> pull down
  192. */
  193. static const u32 rts5227_ms_pull_ctl_disable_tbl[] = {
  194. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
  195. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
  196. 0,
  197. };
  198. void rts5227_init_params(struct rtsx_pcr *pcr)
  199. {
  200. pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104;
  201. pcr->num_slots = 2;
  202. pcr->ops = &rts5227_pcr_ops;
  203. pcr->sd_pull_ctl_enable_tbl = rts5227_sd_pull_ctl_enable_tbl;
  204. pcr->sd_pull_ctl_disable_tbl = rts5227_sd_pull_ctl_disable_tbl;
  205. pcr->ms_pull_ctl_enable_tbl = rts5227_ms_pull_ctl_enable_tbl;
  206. pcr->ms_pull_ctl_disable_tbl = rts5227_ms_pull_ctl_disable_tbl;
  207. }