rtl8411.c 7.7 KB

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