rtl8411.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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/bitops.h>
  24. #include <linux/delay.h>
  25. #include <linux/mfd/rtsx_pci.h>
  26. #include "rtsx_pcr.h"
  27. static u8 rtl8411_get_ic_version(struct rtsx_pcr *pcr)
  28. {
  29. u8 val;
  30. rtsx_pci_read_register(pcr, SYS_VER, &val);
  31. return val & 0x0F;
  32. }
  33. static int rtl8411_extra_init_hw(struct rtsx_pcr *pcr)
  34. {
  35. return rtsx_pci_write_register(pcr, CD_PAD_CTL,
  36. CD_DISABLE_MASK | CD_AUTO_DISABLE, CD_ENABLE);
  37. }
  38. static int rtl8411_turn_on_led(struct rtsx_pcr *pcr)
  39. {
  40. return rtsx_pci_write_register(pcr, CARD_GPIO, 0x01, 0x00);
  41. }
  42. static int rtl8411_turn_off_led(struct rtsx_pcr *pcr)
  43. {
  44. return rtsx_pci_write_register(pcr, CARD_GPIO, 0x01, 0x01);
  45. }
  46. static int rtl8411_enable_auto_blink(struct rtsx_pcr *pcr)
  47. {
  48. return rtsx_pci_write_register(pcr, CARD_AUTO_BLINK, 0xFF, 0x0D);
  49. }
  50. static int rtl8411_disable_auto_blink(struct rtsx_pcr *pcr)
  51. {
  52. return rtsx_pci_write_register(pcr, CARD_AUTO_BLINK, 0x08, 0x00);
  53. }
  54. static int rtl8411_card_power_on(struct rtsx_pcr *pcr, int card)
  55. {
  56. int err;
  57. rtsx_pci_init_cmd(pcr);
  58. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  59. BPP_POWER_MASK, BPP_POWER_5_PERCENT_ON);
  60. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_CTL,
  61. BPP_LDO_POWB, BPP_LDO_SUSPEND);
  62. err = rtsx_pci_send_cmd(pcr, 100);
  63. if (err < 0)
  64. return err;
  65. /* To avoid too large in-rush current */
  66. udelay(150);
  67. err = rtsx_pci_write_register(pcr, CARD_PWR_CTL,
  68. BPP_POWER_MASK, BPP_POWER_10_PERCENT_ON);
  69. if (err < 0)
  70. return err;
  71. udelay(150);
  72. err = rtsx_pci_write_register(pcr, CARD_PWR_CTL,
  73. BPP_POWER_MASK, BPP_POWER_15_PERCENT_ON);
  74. if (err < 0)
  75. return err;
  76. udelay(150);
  77. err = rtsx_pci_write_register(pcr, CARD_PWR_CTL,
  78. BPP_POWER_MASK, BPP_POWER_ON);
  79. if (err < 0)
  80. return err;
  81. return rtsx_pci_write_register(pcr, LDO_CTL, BPP_LDO_POWB, BPP_LDO_ON);
  82. }
  83. static int rtl8411_card_power_off(struct rtsx_pcr *pcr, int card)
  84. {
  85. int err;
  86. err = rtsx_pci_write_register(pcr, CARD_PWR_CTL,
  87. BPP_POWER_MASK, BPP_POWER_OFF);
  88. if (err < 0)
  89. return err;
  90. return rtsx_pci_write_register(pcr, LDO_CTL,
  91. BPP_LDO_POWB, BPP_LDO_SUSPEND);
  92. }
  93. static unsigned int rtl8411_cd_deglitch(struct rtsx_pcr *pcr)
  94. {
  95. unsigned int card_exist;
  96. card_exist = rtsx_pci_readl(pcr, RTSX_BIPR);
  97. card_exist &= CARD_EXIST;
  98. if (!card_exist) {
  99. /* Enable card CD */
  100. rtsx_pci_write_register(pcr, CD_PAD_CTL,
  101. CD_DISABLE_MASK, CD_ENABLE);
  102. /* Enable card interrupt */
  103. rtsx_pci_write_register(pcr, EFUSE_CONTENT, 0xe0, 0x00);
  104. return 0;
  105. }
  106. if (hweight32(card_exist) > 1) {
  107. rtsx_pci_write_register(pcr, CARD_PWR_CTL,
  108. BPP_POWER_MASK, BPP_POWER_5_PERCENT_ON);
  109. msleep(100);
  110. card_exist = rtsx_pci_readl(pcr, RTSX_BIPR);
  111. if (card_exist & MS_EXIST)
  112. card_exist = MS_EXIST;
  113. else if (card_exist & SD_EXIST)
  114. card_exist = SD_EXIST;
  115. else
  116. card_exist = 0;
  117. rtsx_pci_write_register(pcr, CARD_PWR_CTL,
  118. BPP_POWER_MASK, BPP_POWER_OFF);
  119. dev_dbg(&(pcr->pci->dev),
  120. "After CD deglitch, card_exist = 0x%x\n",
  121. card_exist);
  122. }
  123. if (card_exist & MS_EXIST) {
  124. /* Disable SD interrupt */
  125. rtsx_pci_write_register(pcr, EFUSE_CONTENT, 0xe0, 0x40);
  126. rtsx_pci_write_register(pcr, CD_PAD_CTL,
  127. CD_DISABLE_MASK, MS_CD_EN_ONLY);
  128. } else if (card_exist & SD_EXIST) {
  129. /* Disable MS interrupt */
  130. rtsx_pci_write_register(pcr, EFUSE_CONTENT, 0xe0, 0x80);
  131. rtsx_pci_write_register(pcr, CD_PAD_CTL,
  132. CD_DISABLE_MASK, SD_CD_EN_ONLY);
  133. }
  134. return card_exist;
  135. }
  136. static const struct pcr_ops rtl8411_pcr_ops = {
  137. .extra_init_hw = rtl8411_extra_init_hw,
  138. .optimize_phy = NULL,
  139. .turn_on_led = rtl8411_turn_on_led,
  140. .turn_off_led = rtl8411_turn_off_led,
  141. .enable_auto_blink = rtl8411_enable_auto_blink,
  142. .disable_auto_blink = rtl8411_disable_auto_blink,
  143. .card_power_on = rtl8411_card_power_on,
  144. .card_power_off = rtl8411_card_power_off,
  145. .cd_deglitch = rtl8411_cd_deglitch,
  146. };
  147. /* SD Pull Control Enable:
  148. * SD_DAT[3:0] ==> pull up
  149. * SD_CD ==> pull up
  150. * SD_WP ==> pull up
  151. * SD_CMD ==> pull up
  152. * SD_CLK ==> pull down
  153. */
  154. static const u32 rtl8411_sd_pull_ctl_enable_tbl[] = {
  155. RTSX_REG_PAIR(CARD_PULL_CTL1, 0xAA),
  156. RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
  157. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xA9),
  158. RTSX_REG_PAIR(CARD_PULL_CTL4, 0x09),
  159. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x09),
  160. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x04),
  161. 0,
  162. };
  163. /* SD Pull Control Disable:
  164. * SD_DAT[3:0] ==> pull down
  165. * SD_CD ==> pull up
  166. * SD_WP ==> pull down
  167. * SD_CMD ==> pull down
  168. * SD_CLK ==> pull down
  169. */
  170. static const u32 rtl8411_sd_pull_ctl_disable_tbl[] = {
  171. RTSX_REG_PAIR(CARD_PULL_CTL1, 0x65),
  172. RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
  173. RTSX_REG_PAIR(CARD_PULL_CTL3, 0x95),
  174. RTSX_REG_PAIR(CARD_PULL_CTL4, 0x09),
  175. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x05),
  176. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x04),
  177. 0,
  178. };
  179. /* MS Pull Control Enable:
  180. * MS CD ==> pull up
  181. * others ==> pull down
  182. */
  183. static const u32 rtl8411_ms_pull_ctl_enable_tbl[] = {
  184. RTSX_REG_PAIR(CARD_PULL_CTL1, 0x65),
  185. RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
  186. RTSX_REG_PAIR(CARD_PULL_CTL3, 0x95),
  187. RTSX_REG_PAIR(CARD_PULL_CTL4, 0x05),
  188. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x05),
  189. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x04),
  190. 0,
  191. };
  192. /* MS Pull Control Disable:
  193. * MS CD ==> pull up
  194. * others ==> pull down
  195. */
  196. static const u32 rtl8411_ms_pull_ctl_disable_tbl[] = {
  197. RTSX_REG_PAIR(CARD_PULL_CTL1, 0x65),
  198. RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
  199. RTSX_REG_PAIR(CARD_PULL_CTL3, 0x95),
  200. RTSX_REG_PAIR(CARD_PULL_CTL4, 0x09),
  201. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x05),
  202. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x04),
  203. 0,
  204. };
  205. void rtl8411_init_params(struct rtsx_pcr *pcr)
  206. {
  207. pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104;
  208. pcr->num_slots = 2;
  209. pcr->ops = &rtl8411_pcr_ops;
  210. pcr->ic_version = rtl8411_get_ic_version(pcr);
  211. pcr->sd_pull_ctl_enable_tbl = rtl8411_sd_pull_ctl_enable_tbl;
  212. pcr->sd_pull_ctl_disable_tbl = rtl8411_sd_pull_ctl_disable_tbl;
  213. pcr->ms_pull_ctl_enable_tbl = rtl8411_ms_pull_ctl_enable_tbl;
  214. pcr->ms_pull_ctl_disable_tbl = rtl8411_ms_pull_ctl_disable_tbl;
  215. }