rtl8411.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 int rtl8411_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
  94. {
  95. u8 mask, val;
  96. mask = (BPP_REG_TUNED18 << BPP_TUNED18_SHIFT_8411) | BPP_PAD_MASK;
  97. if (voltage == OUTPUT_3V3)
  98. val = (BPP_ASIC_3V3 << BPP_TUNED18_SHIFT_8411) | BPP_PAD_3V3;
  99. else if (voltage == OUTPUT_1V8)
  100. val = (BPP_ASIC_1V8 << BPP_TUNED18_SHIFT_8411) | BPP_PAD_1V8;
  101. else
  102. return -EINVAL;
  103. return rtsx_pci_write_register(pcr, LDO_CTL, mask, val);
  104. }
  105. static unsigned int rtl8411_cd_deglitch(struct rtsx_pcr *pcr)
  106. {
  107. unsigned int card_exist;
  108. card_exist = rtsx_pci_readl(pcr, RTSX_BIPR);
  109. card_exist &= CARD_EXIST;
  110. if (!card_exist) {
  111. /* Enable card CD */
  112. rtsx_pci_write_register(pcr, CD_PAD_CTL,
  113. CD_DISABLE_MASK, CD_ENABLE);
  114. /* Enable card interrupt */
  115. rtsx_pci_write_register(pcr, EFUSE_CONTENT, 0xe0, 0x00);
  116. return 0;
  117. }
  118. if (hweight32(card_exist) > 1) {
  119. rtsx_pci_write_register(pcr, CARD_PWR_CTL,
  120. BPP_POWER_MASK, BPP_POWER_5_PERCENT_ON);
  121. msleep(100);
  122. card_exist = rtsx_pci_readl(pcr, RTSX_BIPR);
  123. if (card_exist & MS_EXIST)
  124. card_exist = MS_EXIST;
  125. else if (card_exist & SD_EXIST)
  126. card_exist = SD_EXIST;
  127. else
  128. card_exist = 0;
  129. rtsx_pci_write_register(pcr, CARD_PWR_CTL,
  130. BPP_POWER_MASK, BPP_POWER_OFF);
  131. dev_dbg(&(pcr->pci->dev),
  132. "After CD deglitch, card_exist = 0x%x\n",
  133. card_exist);
  134. }
  135. if (card_exist & MS_EXIST) {
  136. /* Disable SD interrupt */
  137. rtsx_pci_write_register(pcr, EFUSE_CONTENT, 0xe0, 0x40);
  138. rtsx_pci_write_register(pcr, CD_PAD_CTL,
  139. CD_DISABLE_MASK, MS_CD_EN_ONLY);
  140. } else if (card_exist & SD_EXIST) {
  141. /* Disable MS interrupt */
  142. rtsx_pci_write_register(pcr, EFUSE_CONTENT, 0xe0, 0x80);
  143. rtsx_pci_write_register(pcr, CD_PAD_CTL,
  144. CD_DISABLE_MASK, SD_CD_EN_ONLY);
  145. }
  146. return card_exist;
  147. }
  148. static int rtl8411_conv_clk_and_div_n(int input, int dir)
  149. {
  150. int output;
  151. if (dir == CLK_TO_DIV_N)
  152. output = input * 4 / 5 - 2;
  153. else
  154. output = (input + 2) * 5 / 4;
  155. return output;
  156. }
  157. static const struct pcr_ops rtl8411_pcr_ops = {
  158. .extra_init_hw = rtl8411_extra_init_hw,
  159. .optimize_phy = NULL,
  160. .turn_on_led = rtl8411_turn_on_led,
  161. .turn_off_led = rtl8411_turn_off_led,
  162. .enable_auto_blink = rtl8411_enable_auto_blink,
  163. .disable_auto_blink = rtl8411_disable_auto_blink,
  164. .card_power_on = rtl8411_card_power_on,
  165. .card_power_off = rtl8411_card_power_off,
  166. .switch_output_voltage = rtl8411_switch_output_voltage,
  167. .cd_deglitch = rtl8411_cd_deglitch,
  168. .conv_clk_and_div_n = rtl8411_conv_clk_and_div_n,
  169. };
  170. /* SD Pull Control Enable:
  171. * SD_DAT[3:0] ==> pull up
  172. * SD_CD ==> pull up
  173. * SD_WP ==> pull up
  174. * SD_CMD ==> pull up
  175. * SD_CLK ==> pull down
  176. */
  177. static const u32 rtl8411_sd_pull_ctl_enable_tbl[] = {
  178. RTSX_REG_PAIR(CARD_PULL_CTL1, 0xAA),
  179. RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
  180. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xA9),
  181. RTSX_REG_PAIR(CARD_PULL_CTL4, 0x09),
  182. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x09),
  183. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x04),
  184. 0,
  185. };
  186. /* SD Pull Control Disable:
  187. * SD_DAT[3:0] ==> pull down
  188. * SD_CD ==> pull up
  189. * SD_WP ==> pull down
  190. * SD_CMD ==> pull down
  191. * SD_CLK ==> pull down
  192. */
  193. static const u32 rtl8411_sd_pull_ctl_disable_tbl[] = {
  194. RTSX_REG_PAIR(CARD_PULL_CTL1, 0x65),
  195. RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
  196. RTSX_REG_PAIR(CARD_PULL_CTL3, 0x95),
  197. RTSX_REG_PAIR(CARD_PULL_CTL4, 0x09),
  198. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x05),
  199. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x04),
  200. 0,
  201. };
  202. /* MS Pull Control Enable:
  203. * MS CD ==> pull up
  204. * others ==> pull down
  205. */
  206. static const u32 rtl8411_ms_pull_ctl_enable_tbl[] = {
  207. RTSX_REG_PAIR(CARD_PULL_CTL1, 0x65),
  208. RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
  209. RTSX_REG_PAIR(CARD_PULL_CTL3, 0x95),
  210. RTSX_REG_PAIR(CARD_PULL_CTL4, 0x05),
  211. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x05),
  212. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x04),
  213. 0,
  214. };
  215. /* MS Pull Control Disable:
  216. * MS CD ==> pull up
  217. * others ==> pull down
  218. */
  219. static const u32 rtl8411_ms_pull_ctl_disable_tbl[] = {
  220. RTSX_REG_PAIR(CARD_PULL_CTL1, 0x65),
  221. RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
  222. RTSX_REG_PAIR(CARD_PULL_CTL3, 0x95),
  223. RTSX_REG_PAIR(CARD_PULL_CTL4, 0x09),
  224. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x05),
  225. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x04),
  226. 0,
  227. };
  228. void rtl8411_init_params(struct rtsx_pcr *pcr)
  229. {
  230. pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104;
  231. pcr->num_slots = 2;
  232. pcr->ops = &rtl8411_pcr_ops;
  233. pcr->ic_version = rtl8411_get_ic_version(pcr);
  234. pcr->sd_pull_ctl_enable_tbl = rtl8411_sd_pull_ctl_enable_tbl;
  235. pcr->sd_pull_ctl_disable_tbl = rtl8411_sd_pull_ctl_disable_tbl;
  236. pcr->ms_pull_ctl_enable_tbl = rtl8411_ms_pull_ctl_enable_tbl;
  237. pcr->ms_pull_ctl_disable_tbl = rtl8411_ms_pull_ctl_disable_tbl;
  238. }