rtsx_pcr.c 33 KB

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