rtsx_pcr.c 30 KB

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