rtsx_pcr.c 31 KB

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