rtsx_usb.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. u8 *buf;
  168. int ret;
  169. if (!data)
  170. return -EINVAL;
  171. buf = kzalloc(sizeof(u8), GFP_KERNEL);
  172. if (!buf)
  173. return -ENOMEM;
  174. addr |= EP0_READ_REG_CMD << EP0_OP_SHIFT;
  175. value = swab16(addr);
  176. ret = usb_control_msg(ucr->pusb_dev,
  177. usb_rcvctrlpipe(ucr->pusb_dev, 0), RTSX_USB_REQ_REG_OP,
  178. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  179. value, 0, buf, 1, 100);
  180. *data = *buf;
  181. kfree(buf);
  182. return ret;
  183. }
  184. EXPORT_SYMBOL_GPL(rtsx_usb_ep0_read_register);
  185. void rtsx_usb_add_cmd(struct rtsx_ucr *ucr, u8 cmd_type, u16 reg_addr,
  186. u8 mask, u8 data)
  187. {
  188. int i;
  189. if (ucr->cmd_idx < (IOBUF_SIZE - CMD_OFFSET) / 4) {
  190. i = CMD_OFFSET + ucr->cmd_idx * 4;
  191. ucr->cmd_buf[i++] = ((cmd_type & 0x03) << 6) |
  192. (u8)((reg_addr >> 8) & 0x3F);
  193. ucr->cmd_buf[i++] = (u8)reg_addr;
  194. ucr->cmd_buf[i++] = mask;
  195. ucr->cmd_buf[i++] = data;
  196. ucr->cmd_idx++;
  197. }
  198. }
  199. EXPORT_SYMBOL_GPL(rtsx_usb_add_cmd);
  200. int rtsx_usb_send_cmd(struct rtsx_ucr *ucr, u8 flag, int timeout)
  201. {
  202. int ret;
  203. ucr->cmd_buf[CNT_H] = (u8)(ucr->cmd_idx >> 8);
  204. ucr->cmd_buf[CNT_L] = (u8)(ucr->cmd_idx);
  205. ucr->cmd_buf[STAGE_FLAG] = flag;
  206. ret = rtsx_usb_transfer_data(ucr,
  207. usb_sndbulkpipe(ucr->pusb_dev, EP_BULK_OUT),
  208. ucr->cmd_buf, ucr->cmd_idx * 4 + CMD_OFFSET,
  209. 0, NULL, timeout);
  210. if (ret) {
  211. rtsx_usb_clear_fsm_err(ucr);
  212. return ret;
  213. }
  214. return 0;
  215. }
  216. EXPORT_SYMBOL_GPL(rtsx_usb_send_cmd);
  217. int rtsx_usb_get_rsp(struct rtsx_ucr *ucr, int rsp_len, int timeout)
  218. {
  219. if (rsp_len <= 0)
  220. return -EINVAL;
  221. rsp_len = ALIGN(rsp_len, 4);
  222. return rtsx_usb_transfer_data(ucr,
  223. usb_rcvbulkpipe(ucr->pusb_dev, EP_BULK_IN),
  224. ucr->rsp_buf, rsp_len, 0, NULL, timeout);
  225. }
  226. EXPORT_SYMBOL_GPL(rtsx_usb_get_rsp);
  227. static int rtsx_usb_get_status_with_bulk(struct rtsx_ucr *ucr, u16 *status)
  228. {
  229. int ret;
  230. rtsx_usb_init_cmd(ucr);
  231. rtsx_usb_add_cmd(ucr, READ_REG_CMD, CARD_EXIST, 0x00, 0x00);
  232. rtsx_usb_add_cmd(ucr, READ_REG_CMD, OCPSTAT, 0x00, 0x00);
  233. ret = rtsx_usb_send_cmd(ucr, MODE_CR, 100);
  234. if (ret)
  235. return ret;
  236. ret = rtsx_usb_get_rsp(ucr, 2, 100);
  237. if (ret)
  238. return ret;
  239. *status = ((ucr->rsp_buf[0] >> 2) & 0x0f) |
  240. ((ucr->rsp_buf[1] & 0x03) << 4);
  241. return 0;
  242. }
  243. int rtsx_usb_get_card_status(struct rtsx_ucr *ucr, u16 *status)
  244. {
  245. int ret;
  246. u16 *buf;
  247. if (!status)
  248. return -EINVAL;
  249. if (polling_pipe == 0) {
  250. buf = kzalloc(sizeof(u16), GFP_KERNEL);
  251. if (!buf)
  252. return -ENOMEM;
  253. ret = usb_control_msg(ucr->pusb_dev,
  254. usb_rcvctrlpipe(ucr->pusb_dev, 0),
  255. RTSX_USB_REQ_POLL,
  256. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  257. 0, 0, buf, 2, 100);
  258. *status = *buf;
  259. kfree(buf);
  260. } else {
  261. ret = rtsx_usb_get_status_with_bulk(ucr, status);
  262. }
  263. /* usb_control_msg may return positive when success */
  264. if (ret < 0)
  265. return ret;
  266. return 0;
  267. }
  268. EXPORT_SYMBOL_GPL(rtsx_usb_get_card_status);
  269. static int rtsx_usb_write_phy_register(struct rtsx_ucr *ucr, u8 addr, u8 val)
  270. {
  271. dev_dbg(&ucr->pusb_intf->dev, "Write 0x%x to phy register 0x%x\n",
  272. val, addr);
  273. rtsx_usb_init_cmd(ucr);
  274. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VSTAIN, 0xFF, val);
  275. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VCONTROL, 0xFF, addr & 0x0F);
  276. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00);
  277. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00);
  278. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x01);
  279. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VCONTROL,
  280. 0xFF, (addr >> 4) & 0x0F);
  281. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00);
  282. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00);
  283. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x01);
  284. return rtsx_usb_send_cmd(ucr, MODE_C, 100);
  285. }
  286. int rtsx_usb_write_register(struct rtsx_ucr *ucr, u16 addr, u8 mask, u8 data)
  287. {
  288. rtsx_usb_init_cmd(ucr);
  289. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, addr, mask, data);
  290. return rtsx_usb_send_cmd(ucr, MODE_C, 100);
  291. }
  292. EXPORT_SYMBOL_GPL(rtsx_usb_write_register);
  293. int rtsx_usb_read_register(struct rtsx_ucr *ucr, u16 addr, u8 *data)
  294. {
  295. int ret;
  296. if (data != NULL)
  297. *data = 0;
  298. rtsx_usb_init_cmd(ucr);
  299. rtsx_usb_add_cmd(ucr, READ_REG_CMD, addr, 0, 0);
  300. ret = rtsx_usb_send_cmd(ucr, MODE_CR, 100);
  301. if (ret)
  302. return ret;
  303. ret = rtsx_usb_get_rsp(ucr, 1, 100);
  304. if (ret)
  305. return ret;
  306. if (data != NULL)
  307. *data = ucr->rsp_buf[0];
  308. return 0;
  309. }
  310. EXPORT_SYMBOL_GPL(rtsx_usb_read_register);
  311. static inline u8 double_ssc_depth(u8 depth)
  312. {
  313. return (depth > 1) ? (depth - 1) : depth;
  314. }
  315. static u8 revise_ssc_depth(u8 ssc_depth, u8 div)
  316. {
  317. if (div > CLK_DIV_1) {
  318. if (ssc_depth > div - 1)
  319. ssc_depth -= (div - 1);
  320. else
  321. ssc_depth = SSC_DEPTH_2M;
  322. }
  323. return ssc_depth;
  324. }
  325. int rtsx_usb_switch_clock(struct rtsx_ucr *ucr, unsigned int card_clock,
  326. u8 ssc_depth, bool initial_mode, bool double_clk, bool vpclk)
  327. {
  328. int ret;
  329. u8 n, clk_divider, mcu_cnt, div;
  330. if (!card_clock) {
  331. ucr->cur_clk = 0;
  332. return 0;
  333. }
  334. if (initial_mode) {
  335. /* We use 250k(around) here, in initial stage */
  336. clk_divider = SD_CLK_DIVIDE_128;
  337. card_clock = 30000000;
  338. } else {
  339. clk_divider = SD_CLK_DIVIDE_0;
  340. }
  341. ret = rtsx_usb_write_register(ucr, SD_CFG1,
  342. SD_CLK_DIVIDE_MASK, clk_divider);
  343. if (ret < 0)
  344. return ret;
  345. card_clock /= 1000000;
  346. dev_dbg(&ucr->pusb_intf->dev,
  347. "Switch card clock to %dMHz\n", card_clock);
  348. if (!initial_mode && double_clk)
  349. card_clock *= 2;
  350. dev_dbg(&ucr->pusb_intf->dev,
  351. "Internal SSC clock: %dMHz (cur_clk = %d)\n",
  352. card_clock, ucr->cur_clk);
  353. if (card_clock == ucr->cur_clk)
  354. return 0;
  355. /* Converting clock value into internal settings: n and div */
  356. n = card_clock - 2;
  357. if ((card_clock <= 2) || (n > MAX_DIV_N))
  358. return -EINVAL;
  359. mcu_cnt = 60/card_clock + 3;
  360. if (mcu_cnt > 15)
  361. mcu_cnt = 15;
  362. /* Make sure that the SSC clock div_n is not less than MIN_DIV_N */
  363. div = CLK_DIV_1;
  364. while (n < MIN_DIV_N && div < CLK_DIV_4) {
  365. n = (n + 2) * 2 - 2;
  366. div++;
  367. }
  368. dev_dbg(&ucr->pusb_intf->dev, "n = %d, div = %d\n", n, div);
  369. if (double_clk)
  370. ssc_depth = double_ssc_depth(ssc_depth);
  371. ssc_depth = revise_ssc_depth(ssc_depth, div);
  372. dev_dbg(&ucr->pusb_intf->dev, "ssc_depth = %d\n", ssc_depth);
  373. rtsx_usb_init_cmd(ucr);
  374. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CLK_DIV, CLK_CHANGE, CLK_CHANGE);
  375. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CLK_DIV,
  376. 0x3F, (div << 4) | mcu_cnt);
  377. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, 0);
  378. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_CTL2,
  379. SSC_DEPTH_MASK, ssc_depth);
  380. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_DIV_N_0, 0xFF, n);
  381. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, SSC_RSTB);
  382. if (vpclk) {
  383. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SD_VPCLK0_CTL,
  384. PHASE_NOT_RESET, 0);
  385. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SD_VPCLK0_CTL,
  386. PHASE_NOT_RESET, PHASE_NOT_RESET);
  387. }
  388. ret = rtsx_usb_send_cmd(ucr, MODE_C, 2000);
  389. if (ret < 0)
  390. return ret;
  391. ret = rtsx_usb_write_register(ucr, SSC_CTL1, 0xff,
  392. SSC_RSTB | SSC_8X_EN | SSC_SEL_4M);
  393. if (ret < 0)
  394. return ret;
  395. /* Wait SSC clock stable */
  396. usleep_range(100, 1000);
  397. ret = rtsx_usb_write_register(ucr, CLK_DIV, CLK_CHANGE, 0);
  398. if (ret < 0)
  399. return ret;
  400. ucr->cur_clk = card_clock;
  401. return 0;
  402. }
  403. EXPORT_SYMBOL_GPL(rtsx_usb_switch_clock);
  404. int rtsx_usb_card_exclusive_check(struct rtsx_ucr *ucr, int card)
  405. {
  406. int ret;
  407. u16 val;
  408. u16 cd_mask[] = {
  409. [RTSX_USB_SD_CARD] = (CD_MASK & ~SD_CD),
  410. [RTSX_USB_MS_CARD] = (CD_MASK & ~MS_CD)
  411. };
  412. ret = rtsx_usb_get_card_status(ucr, &val);
  413. /*
  414. * If get status fails, return 0 (ok) for the exclusive check
  415. * and let the flow fail at somewhere else.
  416. */
  417. if (ret)
  418. return 0;
  419. if (val & cd_mask[card])
  420. return -EIO;
  421. return 0;
  422. }
  423. EXPORT_SYMBOL_GPL(rtsx_usb_card_exclusive_check);
  424. static int rtsx_usb_reset_chip(struct rtsx_ucr *ucr)
  425. {
  426. int ret;
  427. u8 val;
  428. rtsx_usb_init_cmd(ucr);
  429. if (CHECK_PKG(ucr, LQFP48)) {
  430. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PWR_CTL,
  431. LDO3318_PWR_MASK, LDO_SUSPEND);
  432. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PWR_CTL,
  433. FORCE_LDO_POWERB, FORCE_LDO_POWERB);
  434. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL1,
  435. 0x30, 0x10);
  436. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL5,
  437. 0x03, 0x01);
  438. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL6,
  439. 0x0C, 0x04);
  440. }
  441. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SYS_DUMMY0, NYET_MSAK, NYET_EN);
  442. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CD_DEGLITCH_WIDTH, 0xFF, 0x08);
  443. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD,
  444. CD_DEGLITCH_EN, XD_CD_DEGLITCH_EN, 0x0);
  445. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SD30_DRIVE_SEL,
  446. SD30_DRIVE_MASK, DRIVER_TYPE_D);
  447. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD,
  448. CARD_DRIVE_SEL, SD20_DRIVE_MASK, 0x0);
  449. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, LDO_POWER_CFG, 0xE0, 0x0);
  450. if (ucr->is_rts5179)
  451. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD,
  452. CARD_PULL_CTL5, 0x03, 0x01);
  453. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_DMA1_CTL,
  454. EXTEND_DMA1_ASYNC_SIGNAL, EXTEND_DMA1_ASYNC_SIGNAL);
  455. rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_INT_PEND,
  456. XD_INT | MS_INT | SD_INT,
  457. XD_INT | MS_INT | SD_INT);
  458. ret = rtsx_usb_send_cmd(ucr, MODE_C, 100);
  459. if (ret)
  460. return ret;
  461. /* config non-crystal mode */
  462. rtsx_usb_read_register(ucr, CFG_MODE, &val);
  463. if ((val & XTAL_FREE) || ((val & CLK_MODE_MASK) == CLK_MODE_NON_XTAL)) {
  464. ret = rtsx_usb_write_phy_register(ucr, 0xC2, 0x7C);
  465. if (ret)
  466. return ret;
  467. }
  468. return 0;
  469. }
  470. static int rtsx_usb_init_chip(struct rtsx_ucr *ucr)
  471. {
  472. int ret;
  473. u8 val;
  474. rtsx_usb_clear_fsm_err(ucr);
  475. /* power on SSC */
  476. ret = rtsx_usb_write_register(ucr,
  477. FPDCTL, SSC_POWER_MASK, SSC_POWER_ON);
  478. if (ret)
  479. return ret;
  480. usleep_range(100, 1000);
  481. ret = rtsx_usb_write_register(ucr, CLK_DIV, CLK_CHANGE, 0x00);
  482. if (ret)
  483. return ret;
  484. /* determine IC version */
  485. ret = rtsx_usb_read_register(ucr, HW_VERSION, &val);
  486. if (ret)
  487. return ret;
  488. ucr->ic_version = val & HW_VER_MASK;
  489. /* determine package */
  490. ret = rtsx_usb_read_register(ucr, CARD_SHARE_MODE, &val);
  491. if (ret)
  492. return ret;
  493. if (val & CARD_SHARE_LQFP_SEL) {
  494. ucr->package = LQFP48;
  495. dev_dbg(&ucr->pusb_intf->dev, "Package: LQFP48\n");
  496. } else {
  497. ucr->package = QFN24;
  498. dev_dbg(&ucr->pusb_intf->dev, "Package: QFN24\n");
  499. }
  500. /* determine IC variations */
  501. rtsx_usb_read_register(ucr, CFG_MODE_1, &val);
  502. if (val & RTS5179) {
  503. ucr->is_rts5179 = true;
  504. dev_dbg(&ucr->pusb_intf->dev, "Device is rts5179\n");
  505. } else {
  506. ucr->is_rts5179 = false;
  507. }
  508. return rtsx_usb_reset_chip(ucr);
  509. }
  510. static int rtsx_usb_probe(struct usb_interface *intf,
  511. const struct usb_device_id *id)
  512. {
  513. struct usb_device *usb_dev = interface_to_usbdev(intf);
  514. struct rtsx_ucr *ucr;
  515. int ret;
  516. dev_dbg(&intf->dev,
  517. ": Realtek USB Card Reader found at bus %03d address %03d\n",
  518. usb_dev->bus->busnum, usb_dev->devnum);
  519. ucr = devm_kzalloc(&intf->dev, sizeof(*ucr), GFP_KERNEL);
  520. if (!ucr)
  521. return -ENOMEM;
  522. ucr->pusb_dev = usb_dev;
  523. ucr->iobuf = usb_alloc_coherent(ucr->pusb_dev, IOBUF_SIZE,
  524. GFP_KERNEL, &ucr->iobuf_dma);
  525. if (!ucr->iobuf)
  526. return -ENOMEM;
  527. usb_set_intfdata(intf, ucr);
  528. ucr->vendor_id = id->idVendor;
  529. ucr->product_id = id->idProduct;
  530. ucr->cmd_buf = ucr->rsp_buf = ucr->iobuf;
  531. mutex_init(&ucr->dev_mutex);
  532. ucr->pusb_intf = intf;
  533. /* initialize */
  534. ret = rtsx_usb_init_chip(ucr);
  535. if (ret)
  536. goto out_init_fail;
  537. /* initialize USB SG transfer timer */
  538. setup_timer(&ucr->sg_timer, rtsx_usb_sg_timed_out, (unsigned long) ucr);
  539. ret = mfd_add_hotplug_devices(&intf->dev, rtsx_usb_cells,
  540. ARRAY_SIZE(rtsx_usb_cells));
  541. if (ret)
  542. goto out_init_fail;
  543. #ifdef CONFIG_PM
  544. intf->needs_remote_wakeup = 1;
  545. usb_enable_autosuspend(usb_dev);
  546. #endif
  547. return 0;
  548. out_init_fail:
  549. usb_free_coherent(ucr->pusb_dev, IOBUF_SIZE, ucr->iobuf,
  550. ucr->iobuf_dma);
  551. return ret;
  552. }
  553. static void rtsx_usb_disconnect(struct usb_interface *intf)
  554. {
  555. struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf);
  556. dev_dbg(&intf->dev, "%s called\n", __func__);
  557. mfd_remove_devices(&intf->dev);
  558. usb_set_intfdata(ucr->pusb_intf, NULL);
  559. usb_free_coherent(ucr->pusb_dev, IOBUF_SIZE, ucr->iobuf,
  560. ucr->iobuf_dma);
  561. }
  562. #ifdef CONFIG_PM
  563. static int rtsx_usb_suspend(struct usb_interface *intf, pm_message_t message)
  564. {
  565. struct rtsx_ucr *ucr =
  566. (struct rtsx_ucr *)usb_get_intfdata(intf);
  567. u16 val = 0;
  568. dev_dbg(&intf->dev, "%s called with pm message 0x%04x\n",
  569. __func__, message.event);
  570. if (PMSG_IS_AUTO(message)) {
  571. if (mutex_trylock(&ucr->dev_mutex)) {
  572. rtsx_usb_get_card_status(ucr, &val);
  573. mutex_unlock(&ucr->dev_mutex);
  574. /* Defer the autosuspend if card exists */
  575. if (val & (SD_CD | MS_CD))
  576. return -EAGAIN;
  577. } else {
  578. /* There is an ongoing operation*/
  579. return -EAGAIN;
  580. }
  581. }
  582. return 0;
  583. }
  584. static int rtsx_usb_resume(struct usb_interface *intf)
  585. {
  586. return 0;
  587. }
  588. static int rtsx_usb_reset_resume(struct usb_interface *intf)
  589. {
  590. struct rtsx_ucr *ucr =
  591. (struct rtsx_ucr *)usb_get_intfdata(intf);
  592. rtsx_usb_reset_chip(ucr);
  593. return 0;
  594. }
  595. #else /* CONFIG_PM */
  596. #define rtsx_usb_suspend NULL
  597. #define rtsx_usb_resume NULL
  598. #define rtsx_usb_reset_resume NULL
  599. #endif /* CONFIG_PM */
  600. static int rtsx_usb_pre_reset(struct usb_interface *intf)
  601. {
  602. struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf);
  603. mutex_lock(&ucr->dev_mutex);
  604. return 0;
  605. }
  606. static int rtsx_usb_post_reset(struct usb_interface *intf)
  607. {
  608. struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf);
  609. mutex_unlock(&ucr->dev_mutex);
  610. return 0;
  611. }
  612. static struct usb_device_id rtsx_usb_usb_ids[] = {
  613. { USB_DEVICE(0x0BDA, 0x0129) },
  614. { USB_DEVICE(0x0BDA, 0x0139) },
  615. { USB_DEVICE(0x0BDA, 0x0140) },
  616. { }
  617. };
  618. MODULE_DEVICE_TABLE(usb, rtsx_usb_usb_ids);
  619. static struct usb_driver rtsx_usb_driver = {
  620. .name = "rtsx_usb",
  621. .probe = rtsx_usb_probe,
  622. .disconnect = rtsx_usb_disconnect,
  623. .suspend = rtsx_usb_suspend,
  624. .resume = rtsx_usb_resume,
  625. .reset_resume = rtsx_usb_reset_resume,
  626. .pre_reset = rtsx_usb_pre_reset,
  627. .post_reset = rtsx_usb_post_reset,
  628. .id_table = rtsx_usb_usb_ids,
  629. .supports_autosuspend = 1,
  630. .soft_unbind = 1,
  631. };
  632. module_usb_driver(rtsx_usb_driver);
  633. MODULE_LICENSE("GPL v2");
  634. MODULE_AUTHOR("Roger Tseng <rogerable@realtek.com>");
  635. MODULE_DESCRIPTION("Realtek USB Card Reader Driver");