rtsx_pcr.c 30 KB

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