rtsx_usb.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /* Driver for Realtek USB card reader
  2. *
  3. * Copyright(c) 2009-2013 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 version 2
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Author:
  18. * Roger Tseng <rogerable@realtek.com>
  19. */
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/mutex.h>
  23. #include <linux/usb.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/mfd/core.h>
  26. #include <linux/mfd/rtsx_usb.h>
  27. static int polling_pipe = 1;
  28. module_param(polling_pipe, int, S_IRUGO | S_IWUSR);
  29. MODULE_PARM_DESC(polling_pipe, "polling pipe (0: ctl, 1: bulk)");
  30. static const struct mfd_cell rtsx_usb_cells[] = {
  31. [RTSX_USB_SD_CARD] = {
  32. .name = "rtsx_usb_sdmmc",
  33. .pdata_size = 0,
  34. },
  35. [RTSX_USB_MS_CARD] = {
  36. .name = "rtsx_usb_ms",
  37. .pdata_size = 0,
  38. },
  39. };
  40. static void rtsx_usb_sg_timed_out(unsigned long data)
  41. {
  42. struct rtsx_ucr *ucr = (struct rtsx_ucr *)data;
  43. dev_dbg(&ucr->pusb_intf->dev, "%s: sg transfer timed out", __func__);
  44. usb_sg_cancel(&ucr->current_sg);
  45. /* we know the cancellation is caused by time-out */
  46. ucr->current_sg.status = -ETIMEDOUT;
  47. }
  48. static int rtsx_usb_bulk_transfer_sglist(struct rtsx_ucr *ucr,
  49. unsigned int pipe, struct scatterlist *sg, int num_sg,
  50. unsigned int length, unsigned int *act_len, int timeout)
  51. {
  52. int ret;
  53. dev_dbg(&ucr->pusb_intf->dev, "%s: xfer %u bytes, %d entries\n",
  54. __func__, length, num_sg);
  55. ret = usb_sg_init(&ucr->current_sg, ucr->pusb_dev, pipe, 0,
  56. sg, num_sg, length, GFP_NOIO);
  57. if (ret)
  58. return ret;
  59. ucr->sg_timer.expires = jiffies + msecs_to_jiffies(timeout);
  60. add_timer(&ucr->sg_timer);
  61. usb_sg_wait(&ucr->current_sg);
  62. del_timer_sync(&ucr->sg_timer);
  63. if (act_len)
  64. *act_len = ucr->current_sg.bytes;
  65. return ucr->current_sg.status;
  66. }
  67. int rtsx_usb_transfer_data(struct rtsx_ucr *ucr, unsigned int pipe,
  68. void *buf, unsigned int len, int num_sg,
  69. unsigned int *act_len, int timeout)
  70. {
  71. if (timeout < 600)
  72. timeout = 600;
  73. if (num_sg)
  74. return rtsx_usb_bulk_transfer_sglist(ucr, pipe,
  75. (struct scatterlist *)buf, num_sg, len, act_len,
  76. timeout);
  77. else
  78. return usb_bulk_msg(ucr->pusb_dev, pipe, buf, len, act_len,
  79. timeout);
  80. }
  81. EXPORT_SYMBOL_GPL(rtsx_usb_transfer_data);
  82. static inline void rtsx_usb_seq_cmd_hdr(struct rtsx_ucr *ucr,
  83. u16 addr, u16 len, u8 seq_type)
  84. {
  85. rtsx_usb_cmd_hdr_tag(ucr);
  86. ucr->cmd_buf[PACKET_TYPE] = seq_type;
  87. ucr->cmd_buf[5] = (u8)(len >> 8);
  88. ucr->cmd_buf[6] = (u8)len;
  89. ucr->cmd_buf[8] = (u8)(addr >> 8);
  90. ucr->cmd_buf[9] = (u8)addr;
  91. if (seq_type == SEQ_WRITE)
  92. ucr->cmd_buf[STAGE_FLAG] = 0;
  93. else
  94. ucr->cmd_buf[STAGE_FLAG] = STAGE_R;
  95. }
  96. static int rtsx_usb_seq_write_register(struct rtsx_ucr *ucr,
  97. u16 addr, u16 len, u8 *data)
  98. {
  99. u16 cmd_len = ALIGN(SEQ_WRITE_DATA_OFFSET + len, 4);
  100. if (!data)
  101. return -EINVAL;
  102. if (cmd_len > IOBUF_SIZE)
  103. return -EINVAL;
  104. rtsx_usb_seq_cmd_hdr(ucr, addr, len, SEQ_WRITE);
  105. memcpy(ucr->cmd_buf + SEQ_WRITE_DATA_OFFSET, data, len);
  106. return rtsx_usb_transfer_data(ucr,
  107. usb_sndbulkpipe(ucr->pusb_dev, EP_BULK_OUT),
  108. ucr->cmd_buf, cmd_len, 0, NULL, 100);
  109. }
  110. static int rtsx_usb_seq_read_register(struct rtsx_ucr *ucr,
  111. u16 addr, u16 len, u8 *data)
  112. {
  113. int i, ret;
  114. u16 rsp_len = round_down(len, 4);
  115. u16 res_len = len - rsp_len;
  116. if (!data)
  117. return -EINVAL;
  118. /* 4-byte aligned part */
  119. if (rsp_len) {
  120. rtsx_usb_seq_cmd_hdr(ucr, addr, len, SEQ_READ);
  121. ret = rtsx_usb_transfer_data(ucr,
  122. usb_sndbulkpipe(ucr->pusb_dev, EP_BULK_OUT),
  123. ucr->cmd_buf, 12, 0, NULL, 100);
  124. if (ret)
  125. return ret;
  126. ret = rtsx_usb_transfer_data(ucr,
  127. usb_rcvbulkpipe(ucr->pusb_dev, EP_BULK_IN),
  128. data, rsp_len, 0, NULL, 100);
  129. if (ret)
  130. return ret;
  131. }
  132. /* unaligned part */
  133. for (i = 0; i < res_len; i++) {
  134. ret = rtsx_usb_read_register(ucr, addr + rsp_len + i,
  135. data + rsp_len + i);
  136. if (ret)
  137. return ret;
  138. }
  139. return 0;
  140. }
  141. int rtsx_usb_read_ppbuf(struct rtsx_ucr *ucr, u8 *buf, int buf_len)
  142. {
  143. return rtsx_usb_seq_read_register(ucr, PPBUF_BASE2, (u16)buf_len, buf);
  144. }
  145. EXPORT_SYMBOL_GPL(rtsx_usb_read_ppbuf);
  146. int rtsx_usb_write_ppbuf(struct rtsx_ucr *ucr, u8 *buf, int buf_len)
  147. {
  148. return rtsx_usb_seq_write_register(ucr, PPBUF_BASE2, (u16)buf_len, buf);
  149. }
  150. EXPORT_SYMBOL_GPL(rtsx_usb_write_ppbuf);
  151. int rtsx_usb_ep0_write_register(struct rtsx_ucr *ucr, u16 addr,
  152. u8 mask, u8 data)
  153. {
  154. u16 value, index;
  155. addr |= EP0_WRITE_REG_CMD << EP0_OP_SHIFT;
  156. value = swab16(addr);
  157. index = mask | data << 8;
  158. return usb_control_msg(ucr->pusb_dev,
  159. usb_sndctrlpipe(ucr->pusb_dev, 0), RTSX_USB_REQ_REG_OP,
  160. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  161. value, index, NULL, 0, 100);
  162. }
  163. EXPORT_SYMBOL_GPL(rtsx_usb_ep0_write_register);
  164. int rtsx_usb_ep0_read_register(struct rtsx_ucr *ucr, u16 addr, u8 *data)
  165. {
  166. u16 value;
  167. if (!data)
  168. return -EINVAL;
  169. *data = 0;
  170. addr |= EP0_READ_REG_CMD << EP0_OP_SHIFT;
  171. value = swab16(addr);
  172. return usb_control_msg(ucr->pusb_dev,
  173. usb_rcvctrlpipe(ucr->pusb_dev, 0), RTSX_USB_REQ_REG_OP,
  174. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  175. value, 0, data, 1, 100);
  176. }
  177. EXPORT_SYMBOL_GPL(rtsx_usb_ep0_read_register);
  178. void rtsx_usb_add_cmd(struct rtsx_ucr *ucr, u8 cmd_type, u16 reg_addr,
  179. u8 mask, u8 data)
  180. {
  181. int i;
  182. if (ucr->cmd_idx < (IOBUF_SIZE - CMD_OFFSET) / 4) {
  183. i = CMD_OFFSET + ucr->cmd_idx * 4;
  184. ucr->cmd_buf[i++] = ((cmd_type & 0x03) << 6) |
  185. (u8)((reg_addr >> 8) & 0x3F);
  186. ucr->cmd_buf[i++] = (u8)reg_addr;
  187. ucr->cmd_buf[i++] = mask;
  188. ucr->cmd_buf[i++] = data;
  189. ucr->cmd_idx++;
  190. }
  191. }
  192. EXPORT_SYMBOL_GPL(rtsx_usb_add_cmd);
  193. int rtsx_usb_send_cmd(struct rtsx_ucr *ucr, u8 flag, int timeout)
  194. {
  195. int ret;
  196. ucr->cmd_buf[CNT_H] = (u8)(ucr->cmd_idx >> 8);
  197. ucr->cmd_buf[CNT_L] = (u8)(ucr->cmd_idx);
  198. ucr->cmd_buf[STAGE_FLAG] = flag;
  199. ret = rtsx_usb_transfer_data(ucr,
  200. usb_sndbulkpipe(ucr->pusb_dev, EP_BULK_OUT),
  201. ucr->cmd_buf, ucr->cmd_idx * 4 + CMD_OFFSET,
  202. 0, NULL, timeout);
  203. if (ret) {
  204. rtsx_usb_clear_fsm_err(ucr);
  205. return ret;
  206. }
  207. return 0;
  208. }
  209. EXPORT_SYMBOL_GPL(rtsx_usb_send_cmd);
  210. int rtsx_usb_get_rsp(struct rtsx_ucr *ucr, int rsp_len, int timeout)
  211. {
  212. if (rsp_len <= 0)
  213. return -EINVAL;
  214. rsp_len = ALIGN(rsp_len, 4);
  215. return rtsx_usb_transfer_data(ucr,
  216. usb_rcvbulkpipe(ucr->pusb_dev, EP_BULK_IN),
  217. ucr->rsp_buf, rsp_len, 0, NULL, timeout);
  218. }
  219. EXPORT_SYMBOL_GPL(rtsx_usb_get_rsp);
  220. static int rtsx_usb_get_status_with_bulk(struct rtsx_ucr *ucr, u16 *status)
  221. {
  222. int ret;
  223. rtsx_usb_init_cmd(ucr);
  224. rtsx_usb_add_cmd(ucr, READ_REG_CMD, CARD_EXIST, 0x00, 0x00);
  225. rtsx_usb_add_cmd(ucr, READ_REG_CMD, OCPSTAT, 0x00, 0x00);
  226. ret = rtsx_usb_send_cmd(ucr, MODE_CR, 100);
  227. if (ret)
  228. return ret;
  229. ret = rtsx_usb_get_rsp(ucr, 2, 100);
  230. if (ret)
  231. return ret;
  232. *status = ((ucr->rsp_buf[0] >> 2) & 0x0f) |
  233. ((ucr->rsp_buf[1] & 0x03) << 4);
  234. return 0;
  235. }
  236. int rtsx_usb_get_card_status(struct rtsx_ucr *ucr, u16 *status)
  237. {
  238. int ret;
  239. if (!status)
  240. return -EINVAL;
  241. if (polling_pipe == 0)
  242. ret = usb_control_msg(ucr->pusb_dev,
  243. usb_rcvctrlpipe(ucr->pusb_dev, 0),
  244. RTSX_USB_REQ_POLL,
  245. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  246. 0, 0, status, 2, 100);
  247. else
  248. ret = rtsx_usb_get_status_with_bulk(ucr, status);
  249. /* usb_control_msg may return positive when success */
  250. if (ret < 0)
  251. return ret;
  252. return 0;
  253. }
  254. EXPORT_SYMBOL_GPL(rtsx_usb_get_card_status);
  255. static int rtsx_usb_write_phy_register(struct rtsx_ucr *ucr, u8 addr, u8 val)
  256. {
  257. dev_dbg(&ucr->pusb_intf->dev, "Write 0x%x to phy register 0x%x\n",
  258. val, addr);
  259. rtsx_usb_init_cmd(ucr);
  260. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VSTAIN, 0xFF, val);
  261. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VCONTROL, 0xFF, addr & 0x0F);
  262. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00);
  263. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00);
  264. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x01);
  265. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VCONTROL,
  266. 0xFF, (addr >> 4) & 0x0F);
  267. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00);
  268. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00);
  269. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x01);
  270. return rtsx_usb_send_cmd(ucr, MODE_C, 100);
  271. }
  272. int rtsx_usb_write_register(struct rtsx_ucr *ucr, u16 addr, u8 mask, u8 data)
  273. {
  274. rtsx_usb_init_cmd(ucr);
  275. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, addr, mask, data);
  276. return rtsx_usb_send_cmd(ucr, MODE_C, 100);
  277. }
  278. EXPORT_SYMBOL_GPL(rtsx_usb_write_register);
  279. int rtsx_usb_read_register(struct rtsx_ucr *ucr, u16 addr, u8 *data)
  280. {
  281. int ret;
  282. if (data != NULL)
  283. *data = 0;
  284. rtsx_usb_init_cmd(ucr);
  285. rtsx_usb_add_cmd(ucr, READ_REG_CMD, addr, 0, 0);
  286. ret = rtsx_usb_send_cmd(ucr, MODE_CR, 100);
  287. if (ret)
  288. return ret;
  289. ret = rtsx_usb_get_rsp(ucr, 1, 100);
  290. if (ret)
  291. return ret;
  292. if (data != NULL)
  293. *data = ucr->rsp_buf[0];
  294. return 0;
  295. }
  296. EXPORT_SYMBOL_GPL(rtsx_usb_read_register);
  297. static inline u8 double_ssc_depth(u8 depth)
  298. {
  299. return (depth > 1) ? (depth - 1) : depth;
  300. }
  301. static u8 revise_ssc_depth(u8 ssc_depth, u8 div)
  302. {
  303. if (div > CLK_DIV_1) {
  304. if (ssc_depth > div - 1)
  305. ssc_depth -= (div - 1);
  306. else
  307. ssc_depth = SSC_DEPTH_2M;
  308. }
  309. return ssc_depth;
  310. }
  311. int rtsx_usb_switch_clock(struct rtsx_ucr *ucr, unsigned int card_clock,
  312. u8 ssc_depth, bool initial_mode, bool double_clk, bool vpclk)
  313. {
  314. int ret;
  315. u8 n, clk_divider, mcu_cnt, div;
  316. if (!card_clock) {
  317. ucr->cur_clk = 0;
  318. return 0;
  319. }
  320. if (initial_mode) {
  321. /* We use 250k(around) here, in initial stage */
  322. clk_divider = SD_CLK_DIVIDE_128;
  323. card_clock = 30000000;
  324. } else {
  325. clk_divider = SD_CLK_DIVIDE_0;
  326. }
  327. ret = rtsx_usb_write_register(ucr, SD_CFG1,
  328. SD_CLK_DIVIDE_MASK, clk_divider);
  329. if (ret < 0)
  330. return ret;
  331. card_clock /= 1000000;
  332. dev_dbg(&ucr->pusb_intf->dev,
  333. "Switch card clock to %dMHz\n", card_clock);
  334. if (!initial_mode && double_clk)
  335. card_clock *= 2;
  336. dev_dbg(&ucr->pusb_intf->dev,
  337. "Internal SSC clock: %dMHz (cur_clk = %d)\n",
  338. card_clock, ucr->cur_clk);
  339. if (card_clock == ucr->cur_clk)
  340. return 0;
  341. /* Converting clock value into internal settings: n and div */
  342. n = card_clock - 2;
  343. if ((card_clock <= 2) || (n > MAX_DIV_N))
  344. return -EINVAL;
  345. mcu_cnt = 60/card_clock + 3;
  346. if (mcu_cnt > 15)
  347. mcu_cnt = 15;
  348. /* Make sure that the SSC clock div_n is not less than MIN_DIV_N */
  349. div = CLK_DIV_1;
  350. while (n < MIN_DIV_N && div < CLK_DIV_4) {
  351. n = (n + 2) * 2 - 2;
  352. div++;
  353. }
  354. dev_dbg(&ucr->pusb_intf->dev, "n = %d, div = %d\n", n, div);
  355. if (double_clk)
  356. ssc_depth = double_ssc_depth(ssc_depth);
  357. ssc_depth = revise_ssc_depth(ssc_depth, div);
  358. dev_dbg(&ucr->pusb_intf->dev, "ssc_depth = %d\n", ssc_depth);
  359. rtsx_usb_init_cmd(ucr);
  360. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CLK_DIV, CLK_CHANGE, CLK_CHANGE);
  361. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CLK_DIV,
  362. 0x3F, (div << 4) | mcu_cnt);
  363. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, 0);
  364. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_CTL2,
  365. SSC_DEPTH_MASK, ssc_depth);
  366. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_DIV_N_0, 0xFF, n);
  367. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, SSC_RSTB);
  368. if (vpclk) {
  369. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SD_VPCLK0_CTL,
  370. PHASE_NOT_RESET, 0);
  371. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SD_VPCLK0_CTL,
  372. PHASE_NOT_RESET, PHASE_NOT_RESET);
  373. }
  374. ret = rtsx_usb_send_cmd(ucr, MODE_C, 2000);
  375. if (ret < 0)
  376. return ret;
  377. ret = rtsx_usb_write_register(ucr, SSC_CTL1, 0xff,
  378. SSC_RSTB | SSC_8X_EN | SSC_SEL_4M);
  379. if (ret < 0)
  380. return ret;
  381. /* Wait SSC clock stable */
  382. usleep_range(100, 1000);
  383. ret = rtsx_usb_write_register(ucr, CLK_DIV, CLK_CHANGE, 0);
  384. if (ret < 0)
  385. return ret;
  386. ucr->cur_clk = card_clock;
  387. return 0;
  388. }
  389. EXPORT_SYMBOL_GPL(rtsx_usb_switch_clock);
  390. int rtsx_usb_card_exclusive_check(struct rtsx_ucr *ucr, int card)
  391. {
  392. int ret;
  393. u16 val;
  394. u16 cd_mask[] = {
  395. [RTSX_USB_SD_CARD] = (CD_MASK & ~SD_CD),
  396. [RTSX_USB_MS_CARD] = (CD_MASK & ~MS_CD)
  397. };
  398. ret = rtsx_usb_get_card_status(ucr, &val);
  399. /*
  400. * If get status fails, return 0 (ok) for the exclusive check
  401. * and let the flow fail at somewhere else.
  402. */
  403. if (ret)
  404. return 0;
  405. if (val & cd_mask[card])
  406. return -EIO;
  407. return 0;
  408. }
  409. EXPORT_SYMBOL_GPL(rtsx_usb_card_exclusive_check);
  410. static int rtsx_usb_reset_chip(struct rtsx_ucr *ucr)
  411. {
  412. int ret;
  413. u8 val;
  414. rtsx_usb_init_cmd(ucr);
  415. if (CHECK_PKG(ucr, LQFP48)) {
  416. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PWR_CTL,
  417. LDO3318_PWR_MASK, LDO_SUSPEND);
  418. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PWR_CTL,
  419. FORCE_LDO_POWERB, FORCE_LDO_POWERB);
  420. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL1,
  421. 0x30, 0x10);
  422. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL5,
  423. 0x03, 0x01);
  424. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL6,
  425. 0x0C, 0x04);
  426. }
  427. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SYS_DUMMY0, NYET_MSAK, NYET_EN);
  428. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CD_DEGLITCH_WIDTH, 0xFF, 0x08);
  429. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD,
  430. CD_DEGLITCH_EN, XD_CD_DEGLITCH_EN, 0x0);
  431. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SD30_DRIVE_SEL,
  432. SD30_DRIVE_MASK, DRIVER_TYPE_D);
  433. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD,
  434. CARD_DRIVE_SEL, SD20_DRIVE_MASK, 0x0);
  435. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, LDO_POWER_CFG, 0xE0, 0x0);
  436. if (ucr->is_rts5179)
  437. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD,
  438. CARD_PULL_CTL5, 0x03, 0x01);
  439. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_DMA1_CTL,
  440. EXTEND_DMA1_ASYNC_SIGNAL, EXTEND_DMA1_ASYNC_SIGNAL);
  441. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_INT_PEND,
  442. XD_INT | MS_INT | SD_INT,
  443. XD_INT | MS_INT | SD_INT);
  444. ret = rtsx_usb_send_cmd(ucr, MODE_C, 100);
  445. if (ret)
  446. return ret;
  447. /* config non-crystal mode */
  448. rtsx_usb_read_register(ucr, CFG_MODE, &val);
  449. if ((val & XTAL_FREE) || ((val & CLK_MODE_MASK) == CLK_MODE_NON_XTAL)) {
  450. ret = rtsx_usb_write_phy_register(ucr, 0xC2, 0x7C);
  451. if (ret)
  452. return ret;
  453. }
  454. return 0;
  455. }
  456. static int rtsx_usb_init_chip(struct rtsx_ucr *ucr)
  457. {
  458. int ret;
  459. u8 val;
  460. rtsx_usb_clear_fsm_err(ucr);
  461. /* power on SSC */
  462. ret = rtsx_usb_write_register(ucr,
  463. FPDCTL, SSC_POWER_MASK, SSC_POWER_ON);
  464. if (ret)
  465. return ret;
  466. usleep_range(100, 1000);
  467. ret = rtsx_usb_write_register(ucr, CLK_DIV, CLK_CHANGE, 0x00);
  468. if (ret)
  469. return ret;
  470. /* determine IC version */
  471. ret = rtsx_usb_read_register(ucr, HW_VERSION, &val);
  472. if (ret)
  473. return ret;
  474. ucr->ic_version = val & HW_VER_MASK;
  475. /* determine package */
  476. ret = rtsx_usb_read_register(ucr, CARD_SHARE_MODE, &val);
  477. if (ret)
  478. return ret;
  479. if (val & CARD_SHARE_LQFP_SEL) {
  480. ucr->package = LQFP48;
  481. dev_dbg(&ucr->pusb_intf->dev, "Package: LQFP48\n");
  482. } else {
  483. ucr->package = QFN24;
  484. dev_dbg(&ucr->pusb_intf->dev, "Package: QFN24\n");
  485. }
  486. /* determine IC variations */
  487. rtsx_usb_read_register(ucr, CFG_MODE_1, &val);
  488. if (val & RTS5179) {
  489. ucr->is_rts5179 = true;
  490. dev_dbg(&ucr->pusb_intf->dev, "Device is rts5179\n");
  491. } else {
  492. ucr->is_rts5179 = false;
  493. }
  494. return rtsx_usb_reset_chip(ucr);
  495. }
  496. static int rtsx_usb_probe(struct usb_interface *intf,
  497. const struct usb_device_id *id)
  498. {
  499. struct usb_device *usb_dev = interface_to_usbdev(intf);
  500. struct rtsx_ucr *ucr;
  501. int ret;
  502. dev_dbg(&intf->dev,
  503. ": Realtek USB Card Reader found at bus %03d address %03d\n",
  504. usb_dev->bus->busnum, usb_dev->devnum);
  505. ucr = devm_kzalloc(&intf->dev, sizeof(*ucr), GFP_KERNEL);
  506. if (!ucr)
  507. return -ENOMEM;
  508. ucr->pusb_dev = usb_dev;
  509. ucr->iobuf = usb_alloc_coherent(ucr->pusb_dev, IOBUF_SIZE,
  510. GFP_KERNEL, &ucr->iobuf_dma);
  511. if (!ucr->iobuf)
  512. return -ENOMEM;
  513. usb_set_intfdata(intf, ucr);
  514. ucr->vendor_id = id->idVendor;
  515. ucr->product_id = id->idProduct;
  516. ucr->cmd_buf = ucr->rsp_buf = ucr->iobuf;
  517. mutex_init(&ucr->dev_mutex);
  518. ucr->pusb_intf = intf;
  519. /* initialize */
  520. ret = rtsx_usb_init_chip(ucr);
  521. if (ret)
  522. goto out_init_fail;
  523. /* initialize USB SG transfer timer */
  524. setup_timer(&ucr->sg_timer, rtsx_usb_sg_timed_out, (unsigned long) ucr);
  525. ret = mfd_add_devices(&intf->dev, usb_dev->devnum, rtsx_usb_cells,
  526. ARRAY_SIZE(rtsx_usb_cells), NULL, 0, NULL);
  527. if (ret)
  528. goto out_init_fail;
  529. #ifdef CONFIG_PM
  530. intf->needs_remote_wakeup = 1;
  531. usb_enable_autosuspend(usb_dev);
  532. #endif
  533. return 0;
  534. out_init_fail:
  535. usb_free_coherent(ucr->pusb_dev, IOBUF_SIZE, ucr->iobuf,
  536. ucr->iobuf_dma);
  537. return ret;
  538. }
  539. static void rtsx_usb_disconnect(struct usb_interface *intf)
  540. {
  541. struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf);
  542. dev_dbg(&intf->dev, "%s called\n", __func__);
  543. mfd_remove_devices(&intf->dev);
  544. usb_set_intfdata(ucr->pusb_intf, NULL);
  545. usb_free_coherent(ucr->pusb_dev, IOBUF_SIZE, ucr->iobuf,
  546. ucr->iobuf_dma);
  547. }
  548. #ifdef CONFIG_PM
  549. static int rtsx_usb_suspend(struct usb_interface *intf, pm_message_t message)
  550. {
  551. struct rtsx_ucr *ucr =
  552. (struct rtsx_ucr *)usb_get_intfdata(intf);
  553. dev_dbg(&intf->dev, "%s called with pm message 0x%04x\n",
  554. __func__, message.event);
  555. /*
  556. * Call to make sure LED is off during suspend to save more power.
  557. * It is NOT a permanent state and could be turned on anytime later.
  558. * Thus no need to call turn_on when resunming.
  559. */
  560. mutex_lock(&ucr->dev_mutex);
  561. rtsx_usb_turn_off_led(ucr);
  562. mutex_unlock(&ucr->dev_mutex);
  563. return 0;
  564. }
  565. static int rtsx_usb_resume(struct usb_interface *intf)
  566. {
  567. return 0;
  568. }
  569. static int rtsx_usb_reset_resume(struct usb_interface *intf)
  570. {
  571. struct rtsx_ucr *ucr =
  572. (struct rtsx_ucr *)usb_get_intfdata(intf);
  573. rtsx_usb_reset_chip(ucr);
  574. return 0;
  575. }
  576. #else /* CONFIG_PM */
  577. #define rtsx_usb_suspend NULL
  578. #define rtsx_usb_resume NULL
  579. #define rtsx_usb_reset_resume NULL
  580. #endif /* CONFIG_PM */
  581. static int rtsx_usb_pre_reset(struct usb_interface *intf)
  582. {
  583. struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf);
  584. mutex_lock(&ucr->dev_mutex);
  585. return 0;
  586. }
  587. static int rtsx_usb_post_reset(struct usb_interface *intf)
  588. {
  589. struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf);
  590. mutex_unlock(&ucr->dev_mutex);
  591. return 0;
  592. }
  593. static struct usb_device_id rtsx_usb_usb_ids[] = {
  594. { USB_DEVICE(0x0BDA, 0x0129) },
  595. { USB_DEVICE(0x0BDA, 0x0139) },
  596. { USB_DEVICE(0x0BDA, 0x0140) },
  597. { }
  598. };
  599. MODULE_DEVICE_TABLE(usb, rtsx_usb_usb_ids);
  600. static struct usb_driver rtsx_usb_driver = {
  601. .name = "rtsx_usb",
  602. .probe = rtsx_usb_probe,
  603. .disconnect = rtsx_usb_disconnect,
  604. .suspend = rtsx_usb_suspend,
  605. .resume = rtsx_usb_resume,
  606. .reset_resume = rtsx_usb_reset_resume,
  607. .pre_reset = rtsx_usb_pre_reset,
  608. .post_reset = rtsx_usb_post_reset,
  609. .id_table = rtsx_usb_usb_ids,
  610. .supports_autosuspend = 1,
  611. .soft_unbind = 1,
  612. };
  613. module_usb_driver(rtsx_usb_driver);
  614. MODULE_LICENSE("GPL v2");
  615. MODULE_AUTHOR("Roger Tseng <rogerable@realtek.com>");
  616. MODULE_DESCRIPTION("Realtek USB Card Reader Driver");