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