rtsx_pcr.c 31 KB

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