rtsx_pcr.c 32 KB

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