rtsx_pcr.c 31 KB

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