rts5209.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 const struct pcr_ops rts5209_pcr_ops = {
  121. .extra_init_hw = rts5209_extra_init_hw,
  122. .optimize_phy = rts5209_optimize_phy,
  123. .turn_on_led = rts5209_turn_on_led,
  124. .turn_off_led = rts5209_turn_off_led,
  125. .enable_auto_blink = rts5209_enable_auto_blink,
  126. .disable_auto_blink = rts5209_disable_auto_blink,
  127. .card_power_on = rts5209_card_power_on,
  128. .card_power_off = rts5209_card_power_off,
  129. .cd_deglitch = NULL,
  130. };
  131. /* SD Pull Control Enable:
  132. * SD_DAT[3:0] ==> pull up
  133. * SD_CD ==> pull up
  134. * SD_WP ==> pull up
  135. * SD_CMD ==> pull up
  136. * SD_CLK ==> pull down
  137. */
  138. static const u32 rts5209_sd_pull_ctl_enable_tbl[] = {
  139. RTSX_REG_PAIR(CARD_PULL_CTL1, 0xAA),
  140. RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
  141. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9),
  142. 0,
  143. };
  144. /* SD Pull Control Disable:
  145. * SD_DAT[3:0] ==> pull down
  146. * SD_CD ==> pull up
  147. * SD_WP ==> pull down
  148. * SD_CMD ==> pull down
  149. * SD_CLK ==> pull down
  150. */
  151. static const u32 rts5209_sd_pull_ctl_disable_tbl[] = {
  152. RTSX_REG_PAIR(CARD_PULL_CTL1, 0x55),
  153. RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
  154. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5),
  155. 0,
  156. };
  157. /* MS Pull Control Enable:
  158. * MS CD ==> pull up
  159. * others ==> pull down
  160. */
  161. static const u32 rts5209_ms_pull_ctl_enable_tbl[] = {
  162. RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
  163. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
  164. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
  165. 0,
  166. };
  167. /* MS Pull Control Disable:
  168. * MS CD ==> pull up
  169. * others ==> pull down
  170. */
  171. static const u32 rts5209_ms_pull_ctl_disable_tbl[] = {
  172. RTSX_REG_PAIR(CARD_PULL_CTL4, 0x55),
  173. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
  174. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
  175. 0,
  176. };
  177. void rts5209_init_params(struct rtsx_pcr *pcr)
  178. {
  179. pcr->extra_caps = EXTRA_CAPS_SD_SDR50 |
  180. EXTRA_CAPS_SD_SDR104 | EXTRA_CAPS_MMC_8BIT;
  181. pcr->num_slots = 2;
  182. pcr->ops = &rts5209_pcr_ops;
  183. rts5209_init_vendor_cfg(pcr);
  184. pcr->ic_version = rts5209_get_ic_version(pcr);
  185. pcr->sd_pull_ctl_enable_tbl = rts5209_sd_pull_ctl_enable_tbl;
  186. pcr->sd_pull_ctl_disable_tbl = rts5209_sd_pull_ctl_disable_tbl;
  187. pcr->ms_pull_ctl_enable_tbl = rts5209_ms_pull_ctl_enable_tbl;
  188. pcr->ms_pull_ctl_disable_tbl = rts5209_ms_pull_ctl_disable_tbl;
  189. }