rts5209.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 rts5209_get_ic_version(struct rtsx_pcr *pcr)
  27. {
  28. u8 val;
  29. val = rtsx_pci_readb(pcr, 0x1C);
  30. return val & 0x0F;
  31. }
  32. static void rts5209_init_vendor_cfg(struct rtsx_pcr *pcr)
  33. {
  34. u32 val;
  35. rtsx_pci_read_config_dword(pcr, 0x724, &val);
  36. dev_dbg(&(pcr->pci->dev), "Cfg 0x724: 0x%x\n", val);
  37. if (!(val & 0x80)) {
  38. if (val & 0x08)
  39. pcr->ms_pmos = false;
  40. else
  41. pcr->ms_pmos = true;
  42. }
  43. }
  44. static int rts5209_extra_init_hw(struct rtsx_pcr *pcr)
  45. {
  46. rtsx_pci_init_cmd(pcr);
  47. /* Turn off LED */
  48. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_GPIO, 0xFF, 0x03);
  49. /* Configure GPIO as output */
  50. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_GPIO_DIR, 0xFF, 0x03);
  51. return rtsx_pci_send_cmd(pcr, 100);
  52. }
  53. static int rts5209_optimize_phy(struct rtsx_pcr *pcr)
  54. {
  55. return rtsx_pci_write_phy_register(pcr, 0x00, 0xB966);
  56. }
  57. static int rts5209_turn_on_led(struct rtsx_pcr *pcr)
  58. {
  59. return rtsx_pci_write_register(pcr, CARD_GPIO, 0x01, 0x00);
  60. }
  61. static int rts5209_turn_off_led(struct rtsx_pcr *pcr)
  62. {
  63. return rtsx_pci_write_register(pcr, CARD_GPIO, 0x01, 0x01);
  64. }
  65. static int rts5209_enable_auto_blink(struct rtsx_pcr *pcr)
  66. {
  67. return rtsx_pci_write_register(pcr, CARD_AUTO_BLINK, 0xFF, 0x0D);
  68. }
  69. static int rts5209_disable_auto_blink(struct rtsx_pcr *pcr)
  70. {
  71. return rtsx_pci_write_register(pcr, CARD_AUTO_BLINK, 0x08, 0x00);
  72. }
  73. static int rts5209_card_power_on(struct rtsx_pcr *pcr, int card)
  74. {
  75. int err;
  76. u8 pwr_mask, partial_pwr_on, pwr_on;
  77. pwr_mask = SD_POWER_MASK;
  78. partial_pwr_on = SD_PARTIAL_POWER_ON;
  79. pwr_on = SD_POWER_ON;
  80. if (pcr->ms_pmos && (card == RTSX_MS_CARD)) {
  81. pwr_mask = MS_POWER_MASK;
  82. partial_pwr_on = MS_PARTIAL_POWER_ON;
  83. pwr_on = MS_POWER_ON;
  84. }
  85. rtsx_pci_init_cmd(pcr);
  86. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  87. pwr_mask, partial_pwr_on);
  88. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  89. LDO3318_PWR_MASK, 0x04);
  90. err = rtsx_pci_send_cmd(pcr, 100);
  91. if (err < 0)
  92. return err;
  93. /* To avoid too large in-rush current */
  94. udelay(150);
  95. rtsx_pci_init_cmd(pcr);
  96. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL, pwr_mask, pwr_on);
  97. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  98. LDO3318_PWR_MASK, 0x00);
  99. err = rtsx_pci_send_cmd(pcr, 100);
  100. if (err < 0)
  101. return err;
  102. return 0;
  103. }
  104. static int rts5209_card_power_off(struct rtsx_pcr *pcr, int card)
  105. {
  106. u8 pwr_mask, pwr_off;
  107. pwr_mask = SD_POWER_MASK;
  108. pwr_off = SD_POWER_OFF;
  109. if (pcr->ms_pmos && (card == RTSX_MS_CARD)) {
  110. pwr_mask = MS_POWER_MASK;
  111. pwr_off = MS_POWER_OFF;
  112. }
  113. rtsx_pci_init_cmd(pcr);
  114. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  115. pwr_mask | PMOS_STRG_MASK, pwr_off | PMOS_STRG_400mA);
  116. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  117. LDO3318_PWR_MASK, 0X06);
  118. return rtsx_pci_send_cmd(pcr, 100);
  119. }
  120. static int rts5209_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
  121. {
  122. int err;
  123. if (voltage == OUTPUT_3V3) {
  124. err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4FC0 | 0x24);
  125. if (err < 0)
  126. return err;
  127. } else if (voltage == OUTPUT_1V8) {
  128. err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4C40 | 0x24);
  129. if (err < 0)
  130. return err;
  131. } else {
  132. return -EINVAL;
  133. }
  134. return 0;
  135. }
  136. static const struct pcr_ops rts5209_pcr_ops = {
  137. .extra_init_hw = rts5209_extra_init_hw,
  138. .optimize_phy = rts5209_optimize_phy,
  139. .turn_on_led = rts5209_turn_on_led,
  140. .turn_off_led = rts5209_turn_off_led,
  141. .enable_auto_blink = rts5209_enable_auto_blink,
  142. .disable_auto_blink = rts5209_disable_auto_blink,
  143. .card_power_on = rts5209_card_power_on,
  144. .card_power_off = rts5209_card_power_off,
  145. .switch_output_voltage = rts5209_switch_output_voltage,
  146. .cd_deglitch = NULL,
  147. .conv_clk_and_div_n = NULL,
  148. };
  149. /* SD Pull Control Enable:
  150. * SD_DAT[3:0] ==> pull up
  151. * SD_CD ==> pull up
  152. * SD_WP ==> pull up
  153. * SD_CMD ==> pull up
  154. * SD_CLK ==> pull down
  155. */
  156. static const u32 rts5209_sd_pull_ctl_enable_tbl[] = {
  157. RTSX_REG_PAIR(CARD_PULL_CTL1, 0xAA),
  158. RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
  159. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9),
  160. 0,
  161. };
  162. /* SD Pull Control Disable:
  163. * SD_DAT[3:0] ==> pull down
  164. * SD_CD ==> pull up
  165. * SD_WP ==> pull down
  166. * SD_CMD ==> pull down
  167. * SD_CLK ==> pull down
  168. */
  169. static const u32 rts5209_sd_pull_ctl_disable_tbl[] = {
  170. RTSX_REG_PAIR(CARD_PULL_CTL1, 0x55),
  171. RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
  172. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5),
  173. 0,
  174. };
  175. /* MS Pull Control Enable:
  176. * MS CD ==> pull up
  177. * others ==> pull down
  178. */
  179. static const u32 rts5209_ms_pull_ctl_enable_tbl[] = {
  180. RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
  181. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
  182. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
  183. 0,
  184. };
  185. /* MS Pull Control Disable:
  186. * MS CD ==> pull up
  187. * others ==> pull down
  188. */
  189. static const u32 rts5209_ms_pull_ctl_disable_tbl[] = {
  190. RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
  191. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
  192. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
  193. 0,
  194. };
  195. void rts5209_init_params(struct rtsx_pcr *pcr)
  196. {
  197. pcr->extra_caps = EXTRA_CAPS_SD_SDR50 |
  198. EXTRA_CAPS_SD_SDR104 | EXTRA_CAPS_MMC_8BIT;
  199. pcr->num_slots = 2;
  200. pcr->ops = &rts5209_pcr_ops;
  201. rts5209_init_vendor_cfg(pcr);
  202. pcr->ic_version = rts5209_get_ic_version(pcr);
  203. pcr->sd_pull_ctl_enable_tbl = rts5209_sd_pull_ctl_enable_tbl;
  204. pcr->sd_pull_ctl_disable_tbl = rts5209_sd_pull_ctl_disable_tbl;
  205. pcr->ms_pull_ctl_enable_tbl = rts5209_ms_pull_ctl_enable_tbl;
  206. pcr->ms_pull_ctl_disable_tbl = rts5209_ms_pull_ctl_disable_tbl;
  207. }