rtsx_pcr.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  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/pci.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/highmem.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/delay.h>
  29. #include <linux/idr.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/mfd/core.h>
  32. #include <linux/mfd/rtsx_pci.h>
  33. #include <asm/unaligned.h>
  34. #include "rtsx_pcr.h"
  35. static bool msi_en = true;
  36. module_param(msi_en, bool, S_IRUGO | S_IWUSR);
  37. MODULE_PARM_DESC(msi_en, "Enable MSI");
  38. static DEFINE_IDR(rtsx_pci_idr);
  39. static DEFINE_SPINLOCK(rtsx_pci_lock);
  40. static struct mfd_cell rtsx_pcr_cells[] = {
  41. [RTSX_SD_CARD] = {
  42. .name = DRV_NAME_RTSX_PCI_SDMMC,
  43. },
  44. [RTSX_MS_CARD] = {
  45. .name = DRV_NAME_RTSX_PCI_MS,
  46. },
  47. };
  48. static DEFINE_PCI_DEVICE_TABLE(rtsx_pci_ids) = {
  49. { PCI_DEVICE(0x10EC, 0x5209), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  50. { PCI_DEVICE(0x10EC, 0x5229), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  51. { PCI_DEVICE(0x10EC, 0x5289), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  52. { PCI_DEVICE(0x10EC, 0x5227), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  53. { 0, }
  54. };
  55. MODULE_DEVICE_TABLE(pci, rtsx_pci_ids);
  56. void rtsx_pci_start_run(struct rtsx_pcr *pcr)
  57. {
  58. /* If pci device removed, don't queue idle work any more */
  59. if (pcr->remove_pci)
  60. return;
  61. if (pcr->state != PDEV_STAT_RUN) {
  62. pcr->state = PDEV_STAT_RUN;
  63. if (pcr->ops->enable_auto_blink)
  64. pcr->ops->enable_auto_blink(pcr);
  65. }
  66. mod_delayed_work(system_wq, &pcr->idle_work, msecs_to_jiffies(200));
  67. }
  68. EXPORT_SYMBOL_GPL(rtsx_pci_start_run);
  69. int rtsx_pci_write_register(struct rtsx_pcr *pcr, u16 addr, u8 mask, u8 data)
  70. {
  71. int i;
  72. u32 val = HAIMR_WRITE_START;
  73. val |= (u32)(addr & 0x3FFF) << 16;
  74. val |= (u32)mask << 8;
  75. val |= (u32)data;
  76. rtsx_pci_writel(pcr, RTSX_HAIMR, val);
  77. for (i = 0; i < MAX_RW_REG_CNT; i++) {
  78. val = rtsx_pci_readl(pcr, RTSX_HAIMR);
  79. if ((val & HAIMR_TRANS_END) == 0) {
  80. if (data != (u8)val)
  81. return -EIO;
  82. return 0;
  83. }
  84. }
  85. return -ETIMEDOUT;
  86. }
  87. EXPORT_SYMBOL_GPL(rtsx_pci_write_register);
  88. int rtsx_pci_read_register(struct rtsx_pcr *pcr, u16 addr, u8 *data)
  89. {
  90. u32 val = HAIMR_READ_START;
  91. int i;
  92. val |= (u32)(addr & 0x3FFF) << 16;
  93. rtsx_pci_writel(pcr, RTSX_HAIMR, val);
  94. for (i = 0; i < MAX_RW_REG_CNT; i++) {
  95. val = rtsx_pci_readl(pcr, RTSX_HAIMR);
  96. if ((val & HAIMR_TRANS_END) == 0)
  97. break;
  98. }
  99. if (i >= MAX_RW_REG_CNT)
  100. return -ETIMEDOUT;
  101. if (data)
  102. *data = (u8)(val & 0xFF);
  103. return 0;
  104. }
  105. EXPORT_SYMBOL_GPL(rtsx_pci_read_register);
  106. int rtsx_pci_write_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 val)
  107. {
  108. int err, i, finished = 0;
  109. u8 tmp;
  110. rtsx_pci_init_cmd(pcr);
  111. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYDATA0, 0xFF, (u8)val);
  112. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYDATA1, 0xFF, (u8)(val >> 8));
  113. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYADDR, 0xFF, addr);
  114. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYRWCTL, 0xFF, 0x81);
  115. err = rtsx_pci_send_cmd(pcr, 100);
  116. if (err < 0)
  117. return err;
  118. for (i = 0; i < 100000; i++) {
  119. err = rtsx_pci_read_register(pcr, PHYRWCTL, &tmp);
  120. if (err < 0)
  121. return err;
  122. if (!(tmp & 0x80)) {
  123. finished = 1;
  124. break;
  125. }
  126. }
  127. if (!finished)
  128. return -ETIMEDOUT;
  129. return 0;
  130. }
  131. EXPORT_SYMBOL_GPL(rtsx_pci_write_phy_register);
  132. int rtsx_pci_read_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 *val)
  133. {
  134. int err, i, finished = 0;
  135. u16 data;
  136. u8 *ptr, tmp;
  137. rtsx_pci_init_cmd(pcr);
  138. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYADDR, 0xFF, addr);
  139. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYRWCTL, 0xFF, 0x80);
  140. err = rtsx_pci_send_cmd(pcr, 100);
  141. if (err < 0)
  142. return err;
  143. for (i = 0; i < 100000; i++) {
  144. err = rtsx_pci_read_register(pcr, PHYRWCTL, &tmp);
  145. if (err < 0)
  146. return err;
  147. if (!(tmp & 0x80)) {
  148. finished = 1;
  149. break;
  150. }
  151. }
  152. if (!finished)
  153. return -ETIMEDOUT;
  154. rtsx_pci_init_cmd(pcr);
  155. rtsx_pci_add_cmd(pcr, READ_REG_CMD, PHYDATA0, 0, 0);
  156. rtsx_pci_add_cmd(pcr, READ_REG_CMD, PHYDATA1, 0, 0);
  157. err = rtsx_pci_send_cmd(pcr, 100);
  158. if (err < 0)
  159. return err;
  160. ptr = rtsx_pci_get_cmd_data(pcr);
  161. data = ((u16)ptr[1] << 8) | ptr[0];
  162. if (val)
  163. *val = data;
  164. return 0;
  165. }
  166. EXPORT_SYMBOL_GPL(rtsx_pci_read_phy_register);
  167. void rtsx_pci_stop_cmd(struct rtsx_pcr *pcr)
  168. {
  169. rtsx_pci_writel(pcr, RTSX_HCBCTLR, STOP_CMD);
  170. rtsx_pci_writel(pcr, RTSX_HDBCTLR, STOP_DMA);
  171. rtsx_pci_write_register(pcr, DMACTL, 0x80, 0x80);
  172. rtsx_pci_write_register(pcr, RBCTL, 0x80, 0x80);
  173. }
  174. EXPORT_SYMBOL_GPL(rtsx_pci_stop_cmd);
  175. void rtsx_pci_add_cmd(struct rtsx_pcr *pcr,
  176. u8 cmd_type, u16 reg_addr, u8 mask, u8 data)
  177. {
  178. unsigned long flags;
  179. u32 val = 0;
  180. u32 *ptr = (u32 *)(pcr->host_cmds_ptr);
  181. val |= (u32)(cmd_type & 0x03) << 30;
  182. val |= (u32)(reg_addr & 0x3FFF) << 16;
  183. val |= (u32)mask << 8;
  184. val |= (u32)data;
  185. spin_lock_irqsave(&pcr->lock, flags);
  186. ptr += pcr->ci;
  187. if (pcr->ci < (HOST_CMDS_BUF_LEN / 4)) {
  188. put_unaligned_le32(val, ptr);
  189. ptr++;
  190. pcr->ci++;
  191. }
  192. spin_unlock_irqrestore(&pcr->lock, flags);
  193. }
  194. EXPORT_SYMBOL_GPL(rtsx_pci_add_cmd);
  195. void rtsx_pci_send_cmd_no_wait(struct rtsx_pcr *pcr)
  196. {
  197. u32 val = 1 << 31;
  198. rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr);
  199. val |= (u32)(pcr->ci * 4) & 0x00FFFFFF;
  200. /* Hardware Auto Response */
  201. val |= 0x40000000;
  202. rtsx_pci_writel(pcr, RTSX_HCBCTLR, val);
  203. }
  204. EXPORT_SYMBOL_GPL(rtsx_pci_send_cmd_no_wait);
  205. int rtsx_pci_send_cmd(struct rtsx_pcr *pcr, int timeout)
  206. {
  207. struct completion trans_done;
  208. u32 val = 1 << 31;
  209. long timeleft;
  210. unsigned long flags;
  211. int err = 0;
  212. spin_lock_irqsave(&pcr->lock, flags);
  213. /* set up data structures for the wakeup system */
  214. pcr->done = &trans_done;
  215. pcr->trans_result = TRANS_NOT_READY;
  216. init_completion(&trans_done);
  217. rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr);
  218. val |= (u32)(pcr->ci * 4) & 0x00FFFFFF;
  219. /* Hardware Auto Response */
  220. val |= 0x40000000;
  221. rtsx_pci_writel(pcr, RTSX_HCBCTLR, val);
  222. spin_unlock_irqrestore(&pcr->lock, flags);
  223. /* Wait for TRANS_OK_INT */
  224. timeleft = wait_for_completion_interruptible_timeout(
  225. &trans_done, msecs_to_jiffies(timeout));
  226. if (timeleft <= 0) {
  227. dev_dbg(&(pcr->pci->dev), "Timeout (%s %d)\n",
  228. __func__, __LINE__);
  229. err = -ETIMEDOUT;
  230. goto finish_send_cmd;
  231. }
  232. spin_lock_irqsave(&pcr->lock, flags);
  233. if (pcr->trans_result == TRANS_RESULT_FAIL)
  234. err = -EINVAL;
  235. else if (pcr->trans_result == TRANS_RESULT_OK)
  236. err = 0;
  237. else if (pcr->trans_result == TRANS_NO_DEVICE)
  238. err = -ENODEV;
  239. spin_unlock_irqrestore(&pcr->lock, flags);
  240. finish_send_cmd:
  241. spin_lock_irqsave(&pcr->lock, flags);
  242. pcr->done = NULL;
  243. spin_unlock_irqrestore(&pcr->lock, flags);
  244. if ((err < 0) && (err != -ENODEV))
  245. rtsx_pci_stop_cmd(pcr);
  246. if (pcr->finish_me)
  247. complete(pcr->finish_me);
  248. return err;
  249. }
  250. EXPORT_SYMBOL_GPL(rtsx_pci_send_cmd);
  251. static void rtsx_pci_add_sg_tbl(struct rtsx_pcr *pcr,
  252. dma_addr_t addr, unsigned int len, int end)
  253. {
  254. u64 *ptr = (u64 *)(pcr->host_sg_tbl_ptr) + pcr->sgi;
  255. u64 val;
  256. u8 option = SG_VALID | SG_TRANS_DATA;
  257. dev_dbg(&(pcr->pci->dev), "DMA addr: 0x%x, Len: 0x%x\n",
  258. (unsigned int)addr, len);
  259. if (end)
  260. option |= SG_END;
  261. val = ((u64)addr << 32) | ((u64)len << 12) | option;
  262. put_unaligned_le64(val, ptr);
  263. pcr->sgi++;
  264. }
  265. int rtsx_pci_transfer_data(struct rtsx_pcr *pcr, struct scatterlist *sglist,
  266. int num_sg, bool read, int timeout)
  267. {
  268. struct completion trans_done;
  269. u8 dir;
  270. int err = 0, i, count;
  271. long timeleft;
  272. unsigned long flags;
  273. struct scatterlist *sg;
  274. enum dma_data_direction dma_dir;
  275. u32 val;
  276. dma_addr_t addr;
  277. unsigned int len;
  278. dev_dbg(&(pcr->pci->dev), "--> %s: num_sg = %d\n", __func__, num_sg);
  279. /* don't transfer data during abort processing */
  280. if (pcr->remove_pci)
  281. return -EINVAL;
  282. if ((sglist == NULL) || (num_sg <= 0))
  283. return -EINVAL;
  284. if (read) {
  285. dir = DEVICE_TO_HOST;
  286. dma_dir = DMA_FROM_DEVICE;
  287. } else {
  288. dir = HOST_TO_DEVICE;
  289. dma_dir = DMA_TO_DEVICE;
  290. }
  291. count = dma_map_sg(&(pcr->pci->dev), sglist, num_sg, dma_dir);
  292. if (count < 1) {
  293. dev_err(&(pcr->pci->dev), "scatterlist map failed\n");
  294. return -EINVAL;
  295. }
  296. dev_dbg(&(pcr->pci->dev), "DMA mapping count: %d\n", count);
  297. val = ((u32)(dir & 0x01) << 29) | TRIG_DMA | ADMA_MODE;
  298. pcr->sgi = 0;
  299. for_each_sg(sglist, sg, count, i) {
  300. addr = sg_dma_address(sg);
  301. len = sg_dma_len(sg);
  302. rtsx_pci_add_sg_tbl(pcr, addr, len, i == count - 1);
  303. }
  304. spin_lock_irqsave(&pcr->lock, flags);
  305. pcr->done = &trans_done;
  306. pcr->trans_result = TRANS_NOT_READY;
  307. init_completion(&trans_done);
  308. rtsx_pci_writel(pcr, RTSX_HDBAR, pcr->host_sg_tbl_addr);
  309. rtsx_pci_writel(pcr, RTSX_HDBCTLR, val);
  310. spin_unlock_irqrestore(&pcr->lock, flags);
  311. timeleft = wait_for_completion_interruptible_timeout(
  312. &trans_done, msecs_to_jiffies(timeout));
  313. if (timeleft <= 0) {
  314. dev_dbg(&(pcr->pci->dev), "Timeout (%s %d)\n",
  315. __func__, __LINE__);
  316. err = -ETIMEDOUT;
  317. goto out;
  318. }
  319. spin_lock_irqsave(&pcr->lock, flags);
  320. if (pcr->trans_result == TRANS_RESULT_FAIL)
  321. err = -EINVAL;
  322. else if (pcr->trans_result == TRANS_NO_DEVICE)
  323. err = -ENODEV;
  324. spin_unlock_irqrestore(&pcr->lock, flags);
  325. out:
  326. spin_lock_irqsave(&pcr->lock, flags);
  327. pcr->done = NULL;
  328. spin_unlock_irqrestore(&pcr->lock, flags);
  329. dma_unmap_sg(&(pcr->pci->dev), sglist, num_sg, dma_dir);
  330. if ((err < 0) && (err != -ENODEV))
  331. rtsx_pci_stop_cmd(pcr);
  332. if (pcr->finish_me)
  333. complete(pcr->finish_me);
  334. return err;
  335. }
  336. EXPORT_SYMBOL_GPL(rtsx_pci_transfer_data);
  337. int rtsx_pci_read_ppbuf(struct rtsx_pcr *pcr, u8 *buf, int buf_len)
  338. {
  339. int err;
  340. int i, j;
  341. u16 reg;
  342. u8 *ptr;
  343. if (buf_len > 512)
  344. buf_len = 512;
  345. ptr = buf;
  346. reg = PPBUF_BASE2;
  347. for (i = 0; i < buf_len / 256; i++) {
  348. rtsx_pci_init_cmd(pcr);
  349. for (j = 0; j < 256; j++)
  350. rtsx_pci_add_cmd(pcr, READ_REG_CMD, reg++, 0, 0);
  351. err = rtsx_pci_send_cmd(pcr, 250);
  352. if (err < 0)
  353. return err;
  354. memcpy(ptr, rtsx_pci_get_cmd_data(pcr), 256);
  355. ptr += 256;
  356. }
  357. if (buf_len % 256) {
  358. rtsx_pci_init_cmd(pcr);
  359. for (j = 0; j < buf_len % 256; j++)
  360. rtsx_pci_add_cmd(pcr, READ_REG_CMD, reg++, 0, 0);
  361. err = rtsx_pci_send_cmd(pcr, 250);
  362. if (err < 0)
  363. return err;
  364. }
  365. memcpy(ptr, rtsx_pci_get_cmd_data(pcr), buf_len % 256);
  366. return 0;
  367. }
  368. EXPORT_SYMBOL_GPL(rtsx_pci_read_ppbuf);
  369. int rtsx_pci_write_ppbuf(struct rtsx_pcr *pcr, u8 *buf, int buf_len)
  370. {
  371. int err;
  372. int i, j;
  373. u16 reg;
  374. u8 *ptr;
  375. if (buf_len > 512)
  376. buf_len = 512;
  377. ptr = buf;
  378. reg = PPBUF_BASE2;
  379. for (i = 0; i < buf_len / 256; i++) {
  380. rtsx_pci_init_cmd(pcr);
  381. for (j = 0; j < 256; j++) {
  382. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
  383. reg++, 0xFF, *ptr);
  384. ptr++;
  385. }
  386. err = rtsx_pci_send_cmd(pcr, 250);
  387. if (err < 0)
  388. return err;
  389. }
  390. if (buf_len % 256) {
  391. rtsx_pci_init_cmd(pcr);
  392. for (j = 0; j < buf_len % 256; j++) {
  393. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
  394. reg++, 0xFF, *ptr);
  395. ptr++;
  396. }
  397. err = rtsx_pci_send_cmd(pcr, 250);
  398. if (err < 0)
  399. return err;
  400. }
  401. return 0;
  402. }
  403. EXPORT_SYMBOL_GPL(rtsx_pci_write_ppbuf);
  404. static int rtsx_pci_set_pull_ctl(struct rtsx_pcr *pcr, const u32 *tbl)
  405. {
  406. int err;
  407. rtsx_pci_init_cmd(pcr);
  408. while (*tbl & 0xFFFF0000) {
  409. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
  410. (u16)(*tbl >> 16), 0xFF, (u8)(*tbl));
  411. tbl++;
  412. }
  413. err = rtsx_pci_send_cmd(pcr, 100);
  414. if (err < 0)
  415. return err;
  416. return 0;
  417. }
  418. int rtsx_pci_card_pull_ctl_enable(struct rtsx_pcr *pcr, int card)
  419. {
  420. const u32 *tbl;
  421. if (card == RTSX_SD_CARD)
  422. tbl = pcr->sd_pull_ctl_enable_tbl;
  423. else if (card == RTSX_MS_CARD)
  424. tbl = pcr->ms_pull_ctl_enable_tbl;
  425. else
  426. return -EINVAL;
  427. return rtsx_pci_set_pull_ctl(pcr, tbl);
  428. }
  429. EXPORT_SYMBOL_GPL(rtsx_pci_card_pull_ctl_enable);
  430. int rtsx_pci_card_pull_ctl_disable(struct rtsx_pcr *pcr, int card)
  431. {
  432. const u32 *tbl;
  433. if (card == RTSX_SD_CARD)
  434. tbl = pcr->sd_pull_ctl_disable_tbl;
  435. else if (card == RTSX_MS_CARD)
  436. tbl = pcr->ms_pull_ctl_disable_tbl;
  437. else
  438. return -EINVAL;
  439. return rtsx_pci_set_pull_ctl(pcr, tbl);
  440. }
  441. EXPORT_SYMBOL_GPL(rtsx_pci_card_pull_ctl_disable);
  442. static void rtsx_pci_enable_bus_int(struct rtsx_pcr *pcr)
  443. {
  444. pcr->bier = TRANS_OK_INT_EN | TRANS_FAIL_INT_EN | SD_INT_EN;
  445. if (pcr->num_slots > 1)
  446. pcr->bier |= MS_INT_EN;
  447. /* Enable Bus Interrupt */
  448. rtsx_pci_writel(pcr, RTSX_BIER, pcr->bier);
  449. dev_dbg(&(pcr->pci->dev), "RTSX_BIER: 0x%08x\n", pcr->bier);
  450. }
  451. static inline u8 double_ssc_depth(u8 depth)
  452. {
  453. return ((depth > 1) ? (depth - 1) : depth);
  454. }
  455. static u8 revise_ssc_depth(u8 ssc_depth, u8 div)
  456. {
  457. if (div > CLK_DIV_1) {
  458. if (ssc_depth > (div - 1))
  459. ssc_depth -= (div - 1);
  460. else
  461. ssc_depth = SSC_DEPTH_4M;
  462. }
  463. return ssc_depth;
  464. }
  465. int rtsx_pci_switch_clock(struct rtsx_pcr *pcr, unsigned int card_clock,
  466. u8 ssc_depth, bool initial_mode, bool double_clk, bool vpclk)
  467. {
  468. int err, clk;
  469. u8 n, clk_divider, mcu_cnt, div;
  470. u8 depth[] = {
  471. [RTSX_SSC_DEPTH_4M] = SSC_DEPTH_4M,
  472. [RTSX_SSC_DEPTH_2M] = SSC_DEPTH_2M,
  473. [RTSX_SSC_DEPTH_1M] = SSC_DEPTH_1M,
  474. [RTSX_SSC_DEPTH_500K] = SSC_DEPTH_500K,
  475. [RTSX_SSC_DEPTH_250K] = SSC_DEPTH_250K,
  476. };
  477. if (initial_mode) {
  478. /* We use 250k(around) here, in initial stage */
  479. clk_divider = SD_CLK_DIVIDE_128;
  480. card_clock = 30000000;
  481. } else {
  482. clk_divider = SD_CLK_DIVIDE_0;
  483. }
  484. err = rtsx_pci_write_register(pcr, SD_CFG1,
  485. SD_CLK_DIVIDE_MASK, clk_divider);
  486. if (err < 0)
  487. return err;
  488. card_clock /= 1000000;
  489. dev_dbg(&(pcr->pci->dev), "Switch card clock to %dMHz\n", card_clock);
  490. clk = card_clock;
  491. if (!initial_mode && double_clk)
  492. clk = card_clock * 2;
  493. dev_dbg(&(pcr->pci->dev),
  494. "Internal SSC clock: %dMHz (cur_clock = %d)\n",
  495. clk, pcr->cur_clock);
  496. if (clk == pcr->cur_clock)
  497. return 0;
  498. if (pcr->ops->conv_clk_and_div_n)
  499. n = (u8)pcr->ops->conv_clk_and_div_n(clk, CLK_TO_DIV_N);
  500. else
  501. n = (u8)(clk - 2);
  502. if ((clk <= 2) || (n > MAX_DIV_N_PCR))
  503. return -EINVAL;
  504. mcu_cnt = (u8)(125/clk + 3);
  505. if (mcu_cnt > 15)
  506. mcu_cnt = 15;
  507. /* Make sure that the SSC clock div_n is not less than MIN_DIV_N_PCR */
  508. div = CLK_DIV_1;
  509. while ((n < MIN_DIV_N_PCR) && (div < CLK_DIV_8)) {
  510. if (pcr->ops->conv_clk_and_div_n) {
  511. int dbl_clk = pcr->ops->conv_clk_and_div_n(n,
  512. DIV_N_TO_CLK) * 2;
  513. n = (u8)pcr->ops->conv_clk_and_div_n(dbl_clk,
  514. CLK_TO_DIV_N);
  515. } else {
  516. n = (n + 2) * 2 - 2;
  517. }
  518. div++;
  519. }
  520. dev_dbg(&(pcr->pci->dev), "n = %d, div = %d\n", n, div);
  521. ssc_depth = depth[ssc_depth];
  522. if (double_clk)
  523. ssc_depth = double_ssc_depth(ssc_depth);
  524. ssc_depth = revise_ssc_depth(ssc_depth, div);
  525. dev_dbg(&(pcr->pci->dev), "ssc_depth = %d\n", ssc_depth);
  526. rtsx_pci_init_cmd(pcr);
  527. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL,
  528. CLK_LOW_FREQ, CLK_LOW_FREQ);
  529. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_DIV,
  530. 0xFF, (div << 4) | mcu_cnt);
  531. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, 0);
  532. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL2,
  533. SSC_DEPTH_MASK, ssc_depth);
  534. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_DIV_N_0, 0xFF, n);
  535. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, SSC_RSTB);
  536. if (vpclk) {
  537. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPCLK0_CTL,
  538. PHASE_NOT_RESET, 0);
  539. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPCLK0_CTL,
  540. PHASE_NOT_RESET, PHASE_NOT_RESET);
  541. }
  542. err = rtsx_pci_send_cmd(pcr, 2000);
  543. if (err < 0)
  544. return err;
  545. /* Wait SSC clock stable */
  546. udelay(10);
  547. err = rtsx_pci_write_register(pcr, CLK_CTL, CLK_LOW_FREQ, 0);
  548. if (err < 0)
  549. return err;
  550. pcr->cur_clock = clk;
  551. return 0;
  552. }
  553. EXPORT_SYMBOL_GPL(rtsx_pci_switch_clock);
  554. int rtsx_pci_card_power_on(struct rtsx_pcr *pcr, int card)
  555. {
  556. if (pcr->ops->card_power_on)
  557. return pcr->ops->card_power_on(pcr, card);
  558. return 0;
  559. }
  560. EXPORT_SYMBOL_GPL(rtsx_pci_card_power_on);
  561. int rtsx_pci_card_power_off(struct rtsx_pcr *pcr, int card)
  562. {
  563. if (pcr->ops->card_power_off)
  564. return pcr->ops->card_power_off(pcr, card);
  565. return 0;
  566. }
  567. EXPORT_SYMBOL_GPL(rtsx_pci_card_power_off);
  568. int rtsx_pci_card_exclusive_check(struct rtsx_pcr *pcr, int card)
  569. {
  570. unsigned int cd_mask[] = {
  571. [RTSX_SD_CARD] = SD_EXIST,
  572. [RTSX_MS_CARD] = MS_EXIST
  573. };
  574. if (!pcr->ms_pmos) {
  575. /* When using single PMOS, accessing card is not permitted
  576. * if the existing card is not the designated one.
  577. */
  578. if (pcr->card_exist & (~cd_mask[card]))
  579. return -EIO;
  580. }
  581. return 0;
  582. }
  583. EXPORT_SYMBOL_GPL(rtsx_pci_card_exclusive_check);
  584. int rtsx_pci_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
  585. {
  586. if (pcr->ops->switch_output_voltage)
  587. return pcr->ops->switch_output_voltage(pcr, voltage);
  588. return 0;
  589. }
  590. EXPORT_SYMBOL_GPL(rtsx_pci_switch_output_voltage);
  591. unsigned int rtsx_pci_card_exist(struct rtsx_pcr *pcr)
  592. {
  593. unsigned int val;
  594. val = rtsx_pci_readl(pcr, RTSX_BIPR);
  595. if (pcr->ops->cd_deglitch)
  596. val = pcr->ops->cd_deglitch(pcr);
  597. return val;
  598. }
  599. EXPORT_SYMBOL_GPL(rtsx_pci_card_exist);
  600. void rtsx_pci_complete_unfinished_transfer(struct rtsx_pcr *pcr)
  601. {
  602. struct completion finish;
  603. pcr->finish_me = &finish;
  604. init_completion(&finish);
  605. if (pcr->done)
  606. complete(pcr->done);
  607. if (!pcr->remove_pci)
  608. rtsx_pci_stop_cmd(pcr);
  609. wait_for_completion_interruptible_timeout(&finish,
  610. msecs_to_jiffies(2));
  611. pcr->finish_me = NULL;
  612. }
  613. EXPORT_SYMBOL_GPL(rtsx_pci_complete_unfinished_transfer);
  614. static void rtsx_pci_card_detect(struct work_struct *work)
  615. {
  616. struct delayed_work *dwork;
  617. struct rtsx_pcr *pcr;
  618. unsigned long flags;
  619. unsigned int card_detect = 0, card_inserted, card_removed;
  620. u32 irq_status;
  621. dwork = to_delayed_work(work);
  622. pcr = container_of(dwork, struct rtsx_pcr, carddet_work);
  623. dev_dbg(&(pcr->pci->dev), "--> %s\n", __func__);
  624. mutex_lock(&pcr->pcr_mutex);
  625. spin_lock_irqsave(&pcr->lock, flags);
  626. irq_status = rtsx_pci_readl(pcr, RTSX_BIPR);
  627. dev_dbg(&(pcr->pci->dev), "irq_status: 0x%08x\n", irq_status);
  628. irq_status &= CARD_EXIST;
  629. card_inserted = pcr->card_inserted & irq_status;
  630. card_removed = pcr->card_removed;
  631. pcr->card_inserted = 0;
  632. pcr->card_removed = 0;
  633. spin_unlock_irqrestore(&pcr->lock, flags);
  634. if (card_inserted || card_removed) {
  635. dev_dbg(&(pcr->pci->dev),
  636. "card_inserted: 0x%x, card_removed: 0x%x\n",
  637. card_inserted, card_removed);
  638. if (pcr->ops->cd_deglitch)
  639. card_inserted = pcr->ops->cd_deglitch(pcr);
  640. card_detect = card_inserted | card_removed;
  641. pcr->card_exist |= card_inserted;
  642. pcr->card_exist &= ~card_removed;
  643. }
  644. mutex_unlock(&pcr->pcr_mutex);
  645. if ((card_detect & SD_EXIST) && pcr->slots[RTSX_SD_CARD].card_event)
  646. pcr->slots[RTSX_SD_CARD].card_event(
  647. pcr->slots[RTSX_SD_CARD].p_dev);
  648. if ((card_detect & MS_EXIST) && pcr->slots[RTSX_MS_CARD].card_event)
  649. pcr->slots[RTSX_MS_CARD].card_event(
  650. pcr->slots[RTSX_MS_CARD].p_dev);
  651. }
  652. static irqreturn_t rtsx_pci_isr(int irq, void *dev_id)
  653. {
  654. struct rtsx_pcr *pcr = dev_id;
  655. u32 int_reg;
  656. if (!pcr)
  657. return IRQ_NONE;
  658. spin_lock(&pcr->lock);
  659. int_reg = rtsx_pci_readl(pcr, RTSX_BIPR);
  660. /* Clear interrupt flag */
  661. rtsx_pci_writel(pcr, RTSX_BIPR, int_reg);
  662. if ((int_reg & pcr->bier) == 0) {
  663. spin_unlock(&pcr->lock);
  664. return IRQ_NONE;
  665. }
  666. if (int_reg == 0xFFFFFFFF) {
  667. spin_unlock(&pcr->lock);
  668. return IRQ_HANDLED;
  669. }
  670. int_reg &= (pcr->bier | 0x7FFFFF);
  671. if (int_reg & SD_INT) {
  672. if (int_reg & SD_EXIST) {
  673. pcr->card_inserted |= SD_EXIST;
  674. } else {
  675. pcr->card_removed |= SD_EXIST;
  676. pcr->card_inserted &= ~SD_EXIST;
  677. }
  678. }
  679. if (int_reg & MS_INT) {
  680. if (int_reg & MS_EXIST) {
  681. pcr->card_inserted |= MS_EXIST;
  682. } else {
  683. pcr->card_removed |= MS_EXIST;
  684. pcr->card_inserted &= ~MS_EXIST;
  685. }
  686. }
  687. if (int_reg & (NEED_COMPLETE_INT | DELINK_INT)) {
  688. if (int_reg & (TRANS_FAIL_INT | DELINK_INT)) {
  689. pcr->trans_result = TRANS_RESULT_FAIL;
  690. if (pcr->done)
  691. complete(pcr->done);
  692. } else if (int_reg & TRANS_OK_INT) {
  693. pcr->trans_result = TRANS_RESULT_OK;
  694. if (pcr->done)
  695. complete(pcr->done);
  696. }
  697. }
  698. if (pcr->card_inserted || pcr->card_removed)
  699. schedule_delayed_work(&pcr->carddet_work,
  700. msecs_to_jiffies(200));
  701. spin_unlock(&pcr->lock);
  702. return IRQ_HANDLED;
  703. }
  704. static int rtsx_pci_acquire_irq(struct rtsx_pcr *pcr)
  705. {
  706. dev_info(&(pcr->pci->dev), "%s: pcr->msi_en = %d, pci->irq = %d\n",
  707. __func__, pcr->msi_en, pcr->pci->irq);
  708. if (request_irq(pcr->pci->irq, rtsx_pci_isr,
  709. pcr->msi_en ? 0 : IRQF_SHARED,
  710. DRV_NAME_RTSX_PCI, pcr)) {
  711. dev_err(&(pcr->pci->dev),
  712. "rtsx_sdmmc: unable to grab IRQ %d, disabling device\n",
  713. pcr->pci->irq);
  714. return -1;
  715. }
  716. pcr->irq = pcr->pci->irq;
  717. pci_intx(pcr->pci, !pcr->msi_en);
  718. return 0;
  719. }
  720. static void rtsx_pci_idle_work(struct work_struct *work)
  721. {
  722. struct delayed_work *dwork = to_delayed_work(work);
  723. struct rtsx_pcr *pcr = container_of(dwork, struct rtsx_pcr, idle_work);
  724. dev_dbg(&(pcr->pci->dev), "--> %s\n", __func__);
  725. mutex_lock(&pcr->pcr_mutex);
  726. pcr->state = PDEV_STAT_IDLE;
  727. if (pcr->ops->disable_auto_blink)
  728. pcr->ops->disable_auto_blink(pcr);
  729. if (pcr->ops->turn_off_led)
  730. pcr->ops->turn_off_led(pcr);
  731. mutex_unlock(&pcr->pcr_mutex);
  732. }
  733. static int rtsx_pci_init_hw(struct rtsx_pcr *pcr)
  734. {
  735. int err;
  736. rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr);
  737. rtsx_pci_enable_bus_int(pcr);
  738. /* Power on SSC */
  739. err = rtsx_pci_write_register(pcr, FPDCTL, SSC_POWER_DOWN, 0);
  740. if (err < 0)
  741. return err;
  742. /* Wait SSC power stable */
  743. udelay(200);
  744. if (pcr->ops->optimize_phy) {
  745. err = pcr->ops->optimize_phy(pcr);
  746. if (err < 0)
  747. return err;
  748. }
  749. rtsx_pci_init_cmd(pcr);
  750. /* Set mcu_cnt to 7 to ensure data can be sampled properly */
  751. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_DIV, 0x07, 0x07);
  752. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, HOST_SLEEP_STATE, 0x03, 0x00);
  753. /* Disable card clock */
  754. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_EN, 0x1E, 0);
  755. /* Reset ASPM state to default value */
  756. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, ASPM_FORCE_CTL, 0x3F, 0);
  757. /* Reset delink mode */
  758. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CHANGE_LINK_STATE, 0x0A, 0);
  759. /* Card driving select */
  760. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_DRIVE_SEL,
  761. 0x07, DRIVER_TYPE_D);
  762. /* Enable SSC Clock */
  763. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1,
  764. 0xFF, SSC_8X_EN | SSC_SEL_4M);
  765. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL2, 0xFF, 0x12);
  766. /* Disable cd_pwr_save */
  767. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CHANGE_LINK_STATE, 0x16, 0x10);
  768. /* Clear Link Ready Interrupt */
  769. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, IRQSTAT0,
  770. LINK_RDY_INT, LINK_RDY_INT);
  771. /* Enlarge the estimation window of PERST# glitch
  772. * to reduce the chance of invalid card interrupt
  773. */
  774. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PERST_GLITCH_WIDTH, 0xFF, 0x80);
  775. /* Update RC oscillator to 400k
  776. * bit[0] F_HIGH: for RC oscillator, Rst_value is 1'b1
  777. * 1: 2M 0: 400k
  778. */
  779. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, RCCTL, 0x01, 0x00);
  780. /* Set interrupt write clear
  781. * bit 1: U_elbi_if_rd_clr_en
  782. * 1: Enable ELBI interrupt[31:22] & [7:0] flag read clear
  783. * 0: ELBI interrupt flag[31:22] & [7:0] only can be write clear
  784. */
  785. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, NFTS_TX_CTRL, 0x02, 0);
  786. /* Force CLKREQ# PIN to drive 0 to request clock */
  787. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0x08, 0x08);
  788. err = rtsx_pci_send_cmd(pcr, 100);
  789. if (err < 0)
  790. return err;
  791. /* Enable clk_request_n to enable clock power management */
  792. rtsx_pci_write_config_byte(pcr, 0x81, 1);
  793. /* Enter L1 when host tx idle */
  794. rtsx_pci_write_config_byte(pcr, 0x70F, 0x5B);
  795. if (pcr->ops->extra_init_hw) {
  796. err = pcr->ops->extra_init_hw(pcr);
  797. if (err < 0)
  798. return err;
  799. }
  800. /* No CD interrupt if probing driver with card inserted.
  801. * So we need to initialize pcr->card_exist here.
  802. */
  803. if (pcr->ops->cd_deglitch)
  804. pcr->card_exist = pcr->ops->cd_deglitch(pcr);
  805. else
  806. pcr->card_exist = rtsx_pci_readl(pcr, RTSX_BIPR) & CARD_EXIST;
  807. return 0;
  808. }
  809. static int rtsx_pci_init_chip(struct rtsx_pcr *pcr)
  810. {
  811. int err;
  812. spin_lock_init(&pcr->lock);
  813. mutex_init(&pcr->pcr_mutex);
  814. switch (PCI_PID(pcr)) {
  815. default:
  816. case 0x5209:
  817. rts5209_init_params(pcr);
  818. break;
  819. case 0x5229:
  820. rts5229_init_params(pcr);
  821. break;
  822. case 0x5289:
  823. rtl8411_init_params(pcr);
  824. break;
  825. case 0x5227:
  826. rts5227_init_params(pcr);
  827. break;
  828. }
  829. dev_dbg(&(pcr->pci->dev), "PID: 0x%04x, IC version: 0x%02x\n",
  830. PCI_PID(pcr), pcr->ic_version);
  831. pcr->slots = kcalloc(pcr->num_slots, sizeof(struct rtsx_slot),
  832. GFP_KERNEL);
  833. if (!pcr->slots)
  834. return -ENOMEM;
  835. pcr->state = PDEV_STAT_IDLE;
  836. err = rtsx_pci_init_hw(pcr);
  837. if (err < 0) {
  838. kfree(pcr->slots);
  839. return err;
  840. }
  841. return 0;
  842. }
  843. static int rtsx_pci_probe(struct pci_dev *pcidev,
  844. const struct pci_device_id *id)
  845. {
  846. struct rtsx_pcr *pcr;
  847. struct pcr_handle *handle;
  848. u32 base, len;
  849. int ret, i;
  850. dev_dbg(&(pcidev->dev),
  851. ": Realtek PCI-E Card Reader found at %s [%04x:%04x] (rev %x)\n",
  852. pci_name(pcidev), (int)pcidev->vendor, (int)pcidev->device,
  853. (int)pcidev->revision);
  854. ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(32));
  855. if (ret < 0)
  856. return ret;
  857. ret = pci_enable_device(pcidev);
  858. if (ret)
  859. return ret;
  860. ret = pci_request_regions(pcidev, DRV_NAME_RTSX_PCI);
  861. if (ret)
  862. goto disable;
  863. pcr = kzalloc(sizeof(*pcr), GFP_KERNEL);
  864. if (!pcr) {
  865. ret = -ENOMEM;
  866. goto release_pci;
  867. }
  868. handle = kzalloc(sizeof(*handle), GFP_KERNEL);
  869. if (!handle) {
  870. ret = -ENOMEM;
  871. goto free_pcr;
  872. }
  873. handle->pcr = pcr;
  874. if (!idr_pre_get(&rtsx_pci_idr, GFP_KERNEL)) {
  875. ret = -ENOMEM;
  876. goto free_handle;
  877. }
  878. spin_lock(&rtsx_pci_lock);
  879. ret = idr_get_new(&rtsx_pci_idr, pcr, &pcr->id);
  880. spin_unlock(&rtsx_pci_lock);
  881. if (ret)
  882. goto free_handle;
  883. pcr->pci = pcidev;
  884. dev_set_drvdata(&pcidev->dev, handle);
  885. len = pci_resource_len(pcidev, 0);
  886. base = pci_resource_start(pcidev, 0);
  887. pcr->remap_addr = ioremap_nocache(base, len);
  888. if (!pcr->remap_addr) {
  889. ret = -ENOMEM;
  890. goto free_host;
  891. }
  892. pcr->rtsx_resv_buf = dma_alloc_coherent(&(pcidev->dev),
  893. RTSX_RESV_BUF_LEN, &(pcr->rtsx_resv_buf_addr),
  894. GFP_KERNEL);
  895. if (pcr->rtsx_resv_buf == NULL) {
  896. ret = -ENXIO;
  897. goto unmap;
  898. }
  899. pcr->host_cmds_ptr = pcr->rtsx_resv_buf;
  900. pcr->host_cmds_addr = pcr->rtsx_resv_buf_addr;
  901. pcr->host_sg_tbl_ptr = pcr->rtsx_resv_buf + HOST_CMDS_BUF_LEN;
  902. pcr->host_sg_tbl_addr = pcr->rtsx_resv_buf_addr + HOST_CMDS_BUF_LEN;
  903. pcr->card_inserted = 0;
  904. pcr->card_removed = 0;
  905. INIT_DELAYED_WORK(&pcr->carddet_work, rtsx_pci_card_detect);
  906. INIT_DELAYED_WORK(&pcr->idle_work, rtsx_pci_idle_work);
  907. pcr->msi_en = msi_en;
  908. if (pcr->msi_en) {
  909. ret = pci_enable_msi(pcidev);
  910. if (ret < 0)
  911. pcr->msi_en = false;
  912. }
  913. ret = rtsx_pci_acquire_irq(pcr);
  914. if (ret < 0)
  915. goto free_dma;
  916. pci_set_master(pcidev);
  917. synchronize_irq(pcr->irq);
  918. ret = rtsx_pci_init_chip(pcr);
  919. if (ret < 0)
  920. goto disable_irq;
  921. for (i = 0; i < ARRAY_SIZE(rtsx_pcr_cells); i++) {
  922. rtsx_pcr_cells[i].platform_data = handle;
  923. rtsx_pcr_cells[i].pdata_size = sizeof(*handle);
  924. }
  925. ret = mfd_add_devices(&pcidev->dev, pcr->id, rtsx_pcr_cells,
  926. ARRAY_SIZE(rtsx_pcr_cells), NULL, 0, NULL);
  927. if (ret < 0)
  928. goto disable_irq;
  929. schedule_delayed_work(&pcr->idle_work, msecs_to_jiffies(200));
  930. return 0;
  931. disable_irq:
  932. free_irq(pcr->irq, (void *)pcr);
  933. free_dma:
  934. dma_free_coherent(&(pcr->pci->dev), RTSX_RESV_BUF_LEN,
  935. pcr->rtsx_resv_buf, pcr->rtsx_resv_buf_addr);
  936. unmap:
  937. iounmap(pcr->remap_addr);
  938. free_host:
  939. dev_set_drvdata(&pcidev->dev, NULL);
  940. free_handle:
  941. kfree(handle);
  942. free_pcr:
  943. kfree(pcr);
  944. release_pci:
  945. pci_release_regions(pcidev);
  946. disable:
  947. pci_disable_device(pcidev);
  948. return ret;
  949. }
  950. static void rtsx_pci_remove(struct pci_dev *pcidev)
  951. {
  952. struct pcr_handle *handle = pci_get_drvdata(pcidev);
  953. struct rtsx_pcr *pcr = handle->pcr;
  954. pcr->remove_pci = true;
  955. cancel_delayed_work(&pcr->carddet_work);
  956. cancel_delayed_work(&pcr->idle_work);
  957. mfd_remove_devices(&pcidev->dev);
  958. dma_free_coherent(&(pcr->pci->dev), RTSX_RESV_BUF_LEN,
  959. pcr->rtsx_resv_buf, pcr->rtsx_resv_buf_addr);
  960. free_irq(pcr->irq, (void *)pcr);
  961. if (pcr->msi_en)
  962. pci_disable_msi(pcr->pci);
  963. iounmap(pcr->remap_addr);
  964. dev_set_drvdata(&pcidev->dev, NULL);
  965. pci_release_regions(pcidev);
  966. pci_disable_device(pcidev);
  967. spin_lock(&rtsx_pci_lock);
  968. idr_remove(&rtsx_pci_idr, pcr->id);
  969. spin_unlock(&rtsx_pci_lock);
  970. kfree(pcr->slots);
  971. kfree(pcr);
  972. kfree(handle);
  973. dev_dbg(&(pcidev->dev),
  974. ": Realtek PCI-E Card Reader at %s [%04x:%04x] has been removed\n",
  975. pci_name(pcidev), (int)pcidev->vendor, (int)pcidev->device);
  976. }
  977. #ifdef CONFIG_PM
  978. static int rtsx_pci_suspend(struct pci_dev *pcidev, pm_message_t state)
  979. {
  980. struct pcr_handle *handle;
  981. struct rtsx_pcr *pcr;
  982. int ret = 0;
  983. dev_dbg(&(pcidev->dev), "--> %s\n", __func__);
  984. handle = pci_get_drvdata(pcidev);
  985. pcr = handle->pcr;
  986. cancel_delayed_work(&pcr->carddet_work);
  987. cancel_delayed_work(&pcr->idle_work);
  988. mutex_lock(&pcr->pcr_mutex);
  989. if (pcr->ops->turn_off_led)
  990. pcr->ops->turn_off_led(pcr);
  991. rtsx_pci_writel(pcr, RTSX_BIER, 0);
  992. pcr->bier = 0;
  993. rtsx_pci_write_register(pcr, PETXCFG, 0x08, 0x08);
  994. rtsx_pci_write_register(pcr, HOST_SLEEP_STATE, 0x03, 0x02);
  995. pci_save_state(pcidev);
  996. pci_enable_wake(pcidev, pci_choose_state(pcidev, state), 0);
  997. pci_disable_device(pcidev);
  998. pci_set_power_state(pcidev, pci_choose_state(pcidev, state));
  999. mutex_unlock(&pcr->pcr_mutex);
  1000. return ret;
  1001. }
  1002. static int rtsx_pci_resume(struct pci_dev *pcidev)
  1003. {
  1004. struct pcr_handle *handle;
  1005. struct rtsx_pcr *pcr;
  1006. int ret = 0;
  1007. dev_dbg(&(pcidev->dev), "--> %s\n", __func__);
  1008. handle = pci_get_drvdata(pcidev);
  1009. pcr = handle->pcr;
  1010. mutex_lock(&pcr->pcr_mutex);
  1011. pci_set_power_state(pcidev, PCI_D0);
  1012. pci_restore_state(pcidev);
  1013. ret = pci_enable_device(pcidev);
  1014. if (ret)
  1015. goto out;
  1016. pci_set_master(pcidev);
  1017. ret = rtsx_pci_write_register(pcr, HOST_SLEEP_STATE, 0x03, 0x00);
  1018. if (ret)
  1019. goto out;
  1020. ret = rtsx_pci_init_hw(pcr);
  1021. if (ret)
  1022. goto out;
  1023. schedule_delayed_work(&pcr->idle_work, msecs_to_jiffies(200));
  1024. out:
  1025. mutex_unlock(&pcr->pcr_mutex);
  1026. return ret;
  1027. }
  1028. #else /* CONFIG_PM */
  1029. #define rtsx_pci_suspend NULL
  1030. #define rtsx_pci_resume NULL
  1031. #endif /* CONFIG_PM */
  1032. static struct pci_driver rtsx_pci_driver = {
  1033. .name = DRV_NAME_RTSX_PCI,
  1034. .id_table = rtsx_pci_ids,
  1035. .probe = rtsx_pci_probe,
  1036. .remove = rtsx_pci_remove,
  1037. .suspend = rtsx_pci_suspend,
  1038. .resume = rtsx_pci_resume,
  1039. };
  1040. module_pci_driver(rtsx_pci_driver);
  1041. MODULE_LICENSE("GPL");
  1042. MODULE_AUTHOR("Wei WANG <wei_wang@realsil.com.cn>");
  1043. MODULE_DESCRIPTION("Realtek PCI-E Card Reader Driver");