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, 0x522A), 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. { PCI_DEVICE(0x10EC, 0x5286), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  56. { PCI_DEVICE(0x10EC, 0x524A), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  57. { PCI_DEVICE(0x10EC, 0x525A), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  58. { 0, }
  59. };
  60. MODULE_DEVICE_TABLE(pci, rtsx_pci_ids);
  61. static inline void rtsx_pci_enable_aspm(struct rtsx_pcr *pcr)
  62. {
  63. rtsx_pci_update_cfg_byte(pcr, pcr->pcie_cap + PCI_EXP_LNKCTL,
  64. 0xFC, pcr->aspm_en);
  65. }
  66. static inline void rtsx_pci_disable_aspm(struct rtsx_pcr *pcr)
  67. {
  68. rtsx_pci_update_cfg_byte(pcr, pcr->pcie_cap + PCI_EXP_LNKCTL,
  69. 0xFC, 0);
  70. }
  71. void rtsx_pci_start_run(struct rtsx_pcr *pcr)
  72. {
  73. /* If pci device removed, don't queue idle work any more */
  74. if (pcr->remove_pci)
  75. return;
  76. if (pcr->state != PDEV_STAT_RUN) {
  77. pcr->state = PDEV_STAT_RUN;
  78. if (pcr->ops->enable_auto_blink)
  79. pcr->ops->enable_auto_blink(pcr);
  80. if (pcr->aspm_en)
  81. rtsx_pci_disable_aspm(pcr);
  82. }
  83. mod_delayed_work(system_wq, &pcr->idle_work, msecs_to_jiffies(200));
  84. }
  85. EXPORT_SYMBOL_GPL(rtsx_pci_start_run);
  86. int rtsx_pci_write_register(struct rtsx_pcr *pcr, u16 addr, u8 mask, u8 data)
  87. {
  88. int i;
  89. u32 val = HAIMR_WRITE_START;
  90. val |= (u32)(addr & 0x3FFF) << 16;
  91. val |= (u32)mask << 8;
  92. val |= (u32)data;
  93. rtsx_pci_writel(pcr, RTSX_HAIMR, val);
  94. for (i = 0; i < MAX_RW_REG_CNT; i++) {
  95. val = rtsx_pci_readl(pcr, RTSX_HAIMR);
  96. if ((val & HAIMR_TRANS_END) == 0) {
  97. if (data != (u8)val)
  98. return -EIO;
  99. return 0;
  100. }
  101. }
  102. return -ETIMEDOUT;
  103. }
  104. EXPORT_SYMBOL_GPL(rtsx_pci_write_register);
  105. int rtsx_pci_read_register(struct rtsx_pcr *pcr, u16 addr, u8 *data)
  106. {
  107. u32 val = HAIMR_READ_START;
  108. int i;
  109. val |= (u32)(addr & 0x3FFF) << 16;
  110. rtsx_pci_writel(pcr, RTSX_HAIMR, val);
  111. for (i = 0; i < MAX_RW_REG_CNT; i++) {
  112. val = rtsx_pci_readl(pcr, RTSX_HAIMR);
  113. if ((val & HAIMR_TRANS_END) == 0)
  114. break;
  115. }
  116. if (i >= MAX_RW_REG_CNT)
  117. return -ETIMEDOUT;
  118. if (data)
  119. *data = (u8)(val & 0xFF);
  120. return 0;
  121. }
  122. EXPORT_SYMBOL_GPL(rtsx_pci_read_register);
  123. int __rtsx_pci_write_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 val)
  124. {
  125. int err, i, finished = 0;
  126. u8 tmp;
  127. rtsx_pci_init_cmd(pcr);
  128. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYDATA0, 0xFF, (u8)val);
  129. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYDATA1, 0xFF, (u8)(val >> 8));
  130. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYADDR, 0xFF, addr);
  131. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYRWCTL, 0xFF, 0x81);
  132. err = rtsx_pci_send_cmd(pcr, 100);
  133. if (err < 0)
  134. return err;
  135. for (i = 0; i < 100000; i++) {
  136. err = rtsx_pci_read_register(pcr, PHYRWCTL, &tmp);
  137. if (err < 0)
  138. return err;
  139. if (!(tmp & 0x80)) {
  140. finished = 1;
  141. break;
  142. }
  143. }
  144. if (!finished)
  145. return -ETIMEDOUT;
  146. return 0;
  147. }
  148. int rtsx_pci_write_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 val)
  149. {
  150. if (pcr->ops->write_phy)
  151. return pcr->ops->write_phy(pcr, addr, val);
  152. return __rtsx_pci_write_phy_register(pcr, addr, val);
  153. }
  154. EXPORT_SYMBOL_GPL(rtsx_pci_write_phy_register);
  155. int __rtsx_pci_read_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 *val)
  156. {
  157. int err, i, finished = 0;
  158. u16 data;
  159. u8 *ptr, tmp;
  160. rtsx_pci_init_cmd(pcr);
  161. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYADDR, 0xFF, addr);
  162. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYRWCTL, 0xFF, 0x80);
  163. err = rtsx_pci_send_cmd(pcr, 100);
  164. if (err < 0)
  165. return err;
  166. for (i = 0; i < 100000; i++) {
  167. err = rtsx_pci_read_register(pcr, PHYRWCTL, &tmp);
  168. if (err < 0)
  169. return err;
  170. if (!(tmp & 0x80)) {
  171. finished = 1;
  172. break;
  173. }
  174. }
  175. if (!finished)
  176. return -ETIMEDOUT;
  177. rtsx_pci_init_cmd(pcr);
  178. rtsx_pci_add_cmd(pcr, READ_REG_CMD, PHYDATA0, 0, 0);
  179. rtsx_pci_add_cmd(pcr, READ_REG_CMD, PHYDATA1, 0, 0);
  180. err = rtsx_pci_send_cmd(pcr, 100);
  181. if (err < 0)
  182. return err;
  183. ptr = rtsx_pci_get_cmd_data(pcr);
  184. data = ((u16)ptr[1] << 8) | ptr[0];
  185. if (val)
  186. *val = data;
  187. return 0;
  188. }
  189. int rtsx_pci_read_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 *val)
  190. {
  191. if (pcr->ops->read_phy)
  192. return pcr->ops->read_phy(pcr, addr, val);
  193. return __rtsx_pci_read_phy_register(pcr, addr, val);
  194. }
  195. EXPORT_SYMBOL_GPL(rtsx_pci_read_phy_register);
  196. void rtsx_pci_stop_cmd(struct rtsx_pcr *pcr)
  197. {
  198. rtsx_pci_writel(pcr, RTSX_HCBCTLR, STOP_CMD);
  199. rtsx_pci_writel(pcr, RTSX_HDBCTLR, STOP_DMA);
  200. rtsx_pci_write_register(pcr, DMACTL, 0x80, 0x80);
  201. rtsx_pci_write_register(pcr, RBCTL, 0x80, 0x80);
  202. }
  203. EXPORT_SYMBOL_GPL(rtsx_pci_stop_cmd);
  204. void rtsx_pci_add_cmd(struct rtsx_pcr *pcr,
  205. u8 cmd_type, u16 reg_addr, u8 mask, u8 data)
  206. {
  207. unsigned long flags;
  208. u32 val = 0;
  209. u32 *ptr = (u32 *)(pcr->host_cmds_ptr);
  210. val |= (u32)(cmd_type & 0x03) << 30;
  211. val |= (u32)(reg_addr & 0x3FFF) << 16;
  212. val |= (u32)mask << 8;
  213. val |= (u32)data;
  214. spin_lock_irqsave(&pcr->lock, flags);
  215. ptr += pcr->ci;
  216. if (pcr->ci < (HOST_CMDS_BUF_LEN / 4)) {
  217. put_unaligned_le32(val, ptr);
  218. ptr++;
  219. pcr->ci++;
  220. }
  221. spin_unlock_irqrestore(&pcr->lock, flags);
  222. }
  223. EXPORT_SYMBOL_GPL(rtsx_pci_add_cmd);
  224. void rtsx_pci_send_cmd_no_wait(struct rtsx_pcr *pcr)
  225. {
  226. u32 val = 1 << 31;
  227. rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr);
  228. val |= (u32)(pcr->ci * 4) & 0x00FFFFFF;
  229. /* Hardware Auto Response */
  230. val |= 0x40000000;
  231. rtsx_pci_writel(pcr, RTSX_HCBCTLR, val);
  232. }
  233. EXPORT_SYMBOL_GPL(rtsx_pci_send_cmd_no_wait);
  234. int rtsx_pci_send_cmd(struct rtsx_pcr *pcr, int timeout)
  235. {
  236. struct completion trans_done;
  237. u32 val = 1 << 31;
  238. long timeleft;
  239. unsigned long flags;
  240. int err = 0;
  241. spin_lock_irqsave(&pcr->lock, flags);
  242. /* set up data structures for the wakeup system */
  243. pcr->done = &trans_done;
  244. pcr->trans_result = TRANS_NOT_READY;
  245. init_completion(&trans_done);
  246. rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr);
  247. val |= (u32)(pcr->ci * 4) & 0x00FFFFFF;
  248. /* Hardware Auto Response */
  249. val |= 0x40000000;
  250. rtsx_pci_writel(pcr, RTSX_HCBCTLR, val);
  251. spin_unlock_irqrestore(&pcr->lock, flags);
  252. /* Wait for TRANS_OK_INT */
  253. timeleft = wait_for_completion_interruptible_timeout(
  254. &trans_done, msecs_to_jiffies(timeout));
  255. if (timeleft <= 0) {
  256. pcr_dbg(pcr, "Timeout (%s %d)\n", __func__, __LINE__);
  257. err = -ETIMEDOUT;
  258. goto finish_send_cmd;
  259. }
  260. spin_lock_irqsave(&pcr->lock, flags);
  261. if (pcr->trans_result == TRANS_RESULT_FAIL)
  262. err = -EINVAL;
  263. else if (pcr->trans_result == TRANS_RESULT_OK)
  264. err = 0;
  265. else if (pcr->trans_result == TRANS_NO_DEVICE)
  266. err = -ENODEV;
  267. spin_unlock_irqrestore(&pcr->lock, flags);
  268. finish_send_cmd:
  269. spin_lock_irqsave(&pcr->lock, flags);
  270. pcr->done = NULL;
  271. spin_unlock_irqrestore(&pcr->lock, flags);
  272. if ((err < 0) && (err != -ENODEV))
  273. rtsx_pci_stop_cmd(pcr);
  274. if (pcr->finish_me)
  275. complete(pcr->finish_me);
  276. return err;
  277. }
  278. EXPORT_SYMBOL_GPL(rtsx_pci_send_cmd);
  279. static void rtsx_pci_add_sg_tbl(struct rtsx_pcr *pcr,
  280. dma_addr_t addr, unsigned int len, int end)
  281. {
  282. u64 *ptr = (u64 *)(pcr->host_sg_tbl_ptr) + pcr->sgi;
  283. u64 val;
  284. u8 option = SG_VALID | SG_TRANS_DATA;
  285. pcr_dbg(pcr, "DMA addr: 0x%x, Len: 0x%x\n", (unsigned int)addr, len);
  286. if (end)
  287. option |= SG_END;
  288. val = ((u64)addr << 32) | ((u64)len << 12) | option;
  289. put_unaligned_le64(val, ptr);
  290. pcr->sgi++;
  291. }
  292. int rtsx_pci_transfer_data(struct rtsx_pcr *pcr, struct scatterlist *sglist,
  293. int num_sg, bool read, int timeout)
  294. {
  295. int err = 0, count;
  296. pcr_dbg(pcr, "--> %s: num_sg = %d\n", __func__, num_sg);
  297. count = rtsx_pci_dma_map_sg(pcr, sglist, num_sg, read);
  298. if (count < 1)
  299. return -EINVAL;
  300. pcr_dbg(pcr, "DMA mapping count: %d\n", count);
  301. err = rtsx_pci_dma_transfer(pcr, sglist, count, read, timeout);
  302. rtsx_pci_dma_unmap_sg(pcr, sglist, num_sg, read);
  303. return err;
  304. }
  305. EXPORT_SYMBOL_GPL(rtsx_pci_transfer_data);
  306. int rtsx_pci_dma_map_sg(struct rtsx_pcr *pcr, struct scatterlist *sglist,
  307. int num_sg, bool read)
  308. {
  309. enum dma_data_direction dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  310. if (pcr->remove_pci)
  311. return -EINVAL;
  312. if ((sglist == NULL) || (num_sg <= 0))
  313. return -EINVAL;
  314. return dma_map_sg(&(pcr->pci->dev), sglist, num_sg, dir);
  315. }
  316. EXPORT_SYMBOL_GPL(rtsx_pci_dma_map_sg);
  317. void rtsx_pci_dma_unmap_sg(struct rtsx_pcr *pcr, struct scatterlist *sglist,
  318. int num_sg, bool read)
  319. {
  320. enum dma_data_direction dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  321. dma_unmap_sg(&(pcr->pci->dev), sglist, num_sg, dir);
  322. }
  323. EXPORT_SYMBOL_GPL(rtsx_pci_dma_unmap_sg);
  324. int rtsx_pci_dma_transfer(struct rtsx_pcr *pcr, struct scatterlist *sglist,
  325. int count, bool read, int timeout)
  326. {
  327. struct completion trans_done;
  328. struct scatterlist *sg;
  329. dma_addr_t addr;
  330. long timeleft;
  331. unsigned long flags;
  332. unsigned int len;
  333. int i, err = 0;
  334. u32 val;
  335. u8 dir = read ? DEVICE_TO_HOST : HOST_TO_DEVICE;
  336. if (pcr->remove_pci)
  337. return -ENODEV;
  338. if ((sglist == NULL) || (count < 1))
  339. return -EINVAL;
  340. val = ((u32)(dir & 0x01) << 29) | TRIG_DMA | ADMA_MODE;
  341. pcr->sgi = 0;
  342. for_each_sg(sglist, sg, count, i) {
  343. addr = sg_dma_address(sg);
  344. len = sg_dma_len(sg);
  345. rtsx_pci_add_sg_tbl(pcr, addr, len, i == count - 1);
  346. }
  347. spin_lock_irqsave(&pcr->lock, flags);
  348. pcr->done = &trans_done;
  349. pcr->trans_result = TRANS_NOT_READY;
  350. init_completion(&trans_done);
  351. rtsx_pci_writel(pcr, RTSX_HDBAR, pcr->host_sg_tbl_addr);
  352. rtsx_pci_writel(pcr, RTSX_HDBCTLR, val);
  353. spin_unlock_irqrestore(&pcr->lock, flags);
  354. timeleft = wait_for_completion_interruptible_timeout(
  355. &trans_done, msecs_to_jiffies(timeout));
  356. if (timeleft <= 0) {
  357. pcr_dbg(pcr, "Timeout (%s %d)\n", __func__, __LINE__);
  358. err = -ETIMEDOUT;
  359. goto out;
  360. }
  361. spin_lock_irqsave(&pcr->lock, flags);
  362. if (pcr->trans_result == TRANS_RESULT_FAIL)
  363. err = -EINVAL;
  364. else if (pcr->trans_result == TRANS_NO_DEVICE)
  365. err = -ENODEV;
  366. spin_unlock_irqrestore(&pcr->lock, flags);
  367. out:
  368. spin_lock_irqsave(&pcr->lock, flags);
  369. pcr->done = NULL;
  370. spin_unlock_irqrestore(&pcr->lock, flags);
  371. if ((err < 0) && (err != -ENODEV))
  372. rtsx_pci_stop_cmd(pcr);
  373. if (pcr->finish_me)
  374. complete(pcr->finish_me);
  375. return err;
  376. }
  377. EXPORT_SYMBOL_GPL(rtsx_pci_dma_transfer);
  378. int rtsx_pci_read_ppbuf(struct rtsx_pcr *pcr, u8 *buf, int buf_len)
  379. {
  380. int err;
  381. int i, j;
  382. u16 reg;
  383. u8 *ptr;
  384. if (buf_len > 512)
  385. buf_len = 512;
  386. ptr = buf;
  387. reg = PPBUF_BASE2;
  388. for (i = 0; i < buf_len / 256; i++) {
  389. rtsx_pci_init_cmd(pcr);
  390. for (j = 0; j < 256; j++)
  391. rtsx_pci_add_cmd(pcr, READ_REG_CMD, reg++, 0, 0);
  392. err = rtsx_pci_send_cmd(pcr, 250);
  393. if (err < 0)
  394. return err;
  395. memcpy(ptr, rtsx_pci_get_cmd_data(pcr), 256);
  396. ptr += 256;
  397. }
  398. if (buf_len % 256) {
  399. rtsx_pci_init_cmd(pcr);
  400. for (j = 0; j < buf_len % 256; j++)
  401. rtsx_pci_add_cmd(pcr, READ_REG_CMD, reg++, 0, 0);
  402. err = rtsx_pci_send_cmd(pcr, 250);
  403. if (err < 0)
  404. return err;
  405. }
  406. memcpy(ptr, rtsx_pci_get_cmd_data(pcr), buf_len % 256);
  407. return 0;
  408. }
  409. EXPORT_SYMBOL_GPL(rtsx_pci_read_ppbuf);
  410. int rtsx_pci_write_ppbuf(struct rtsx_pcr *pcr, u8 *buf, int buf_len)
  411. {
  412. int err;
  413. int i, j;
  414. u16 reg;
  415. u8 *ptr;
  416. if (buf_len > 512)
  417. buf_len = 512;
  418. ptr = buf;
  419. reg = PPBUF_BASE2;
  420. for (i = 0; i < buf_len / 256; i++) {
  421. rtsx_pci_init_cmd(pcr);
  422. for (j = 0; j < 256; j++) {
  423. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
  424. reg++, 0xFF, *ptr);
  425. ptr++;
  426. }
  427. err = rtsx_pci_send_cmd(pcr, 250);
  428. if (err < 0)
  429. return err;
  430. }
  431. if (buf_len % 256) {
  432. rtsx_pci_init_cmd(pcr);
  433. for (j = 0; j < buf_len % 256; j++) {
  434. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
  435. reg++, 0xFF, *ptr);
  436. ptr++;
  437. }
  438. err = rtsx_pci_send_cmd(pcr, 250);
  439. if (err < 0)
  440. return err;
  441. }
  442. return 0;
  443. }
  444. EXPORT_SYMBOL_GPL(rtsx_pci_write_ppbuf);
  445. static int rtsx_pci_set_pull_ctl(struct rtsx_pcr *pcr, const u32 *tbl)
  446. {
  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. return rtsx_pci_send_cmd(pcr, 100);
  454. }
  455. int rtsx_pci_card_pull_ctl_enable(struct rtsx_pcr *pcr, int card)
  456. {
  457. const u32 *tbl;
  458. if (card == RTSX_SD_CARD)
  459. tbl = pcr->sd_pull_ctl_enable_tbl;
  460. else if (card == RTSX_MS_CARD)
  461. tbl = pcr->ms_pull_ctl_enable_tbl;
  462. else
  463. return -EINVAL;
  464. return rtsx_pci_set_pull_ctl(pcr, tbl);
  465. }
  466. EXPORT_SYMBOL_GPL(rtsx_pci_card_pull_ctl_enable);
  467. int rtsx_pci_card_pull_ctl_disable(struct rtsx_pcr *pcr, int card)
  468. {
  469. const u32 *tbl;
  470. if (card == RTSX_SD_CARD)
  471. tbl = pcr->sd_pull_ctl_disable_tbl;
  472. else if (card == RTSX_MS_CARD)
  473. tbl = pcr->ms_pull_ctl_disable_tbl;
  474. else
  475. return -EINVAL;
  476. return rtsx_pci_set_pull_ctl(pcr, tbl);
  477. }
  478. EXPORT_SYMBOL_GPL(rtsx_pci_card_pull_ctl_disable);
  479. static void rtsx_pci_enable_bus_int(struct rtsx_pcr *pcr)
  480. {
  481. pcr->bier = TRANS_OK_INT_EN | TRANS_FAIL_INT_EN | SD_INT_EN;
  482. if (pcr->num_slots > 1)
  483. pcr->bier |= MS_INT_EN;
  484. /* Enable Bus Interrupt */
  485. rtsx_pci_writel(pcr, RTSX_BIER, pcr->bier);
  486. pcr_dbg(pcr, "RTSX_BIER: 0x%08x\n", pcr->bier);
  487. }
  488. static inline u8 double_ssc_depth(u8 depth)
  489. {
  490. return ((depth > 1) ? (depth - 1) : depth);
  491. }
  492. static u8 revise_ssc_depth(u8 ssc_depth, u8 div)
  493. {
  494. if (div > CLK_DIV_1) {
  495. if (ssc_depth > (div - 1))
  496. ssc_depth -= (div - 1);
  497. else
  498. ssc_depth = SSC_DEPTH_4M;
  499. }
  500. return ssc_depth;
  501. }
  502. int rtsx_pci_switch_clock(struct rtsx_pcr *pcr, unsigned int card_clock,
  503. u8 ssc_depth, bool initial_mode, bool double_clk, bool vpclk)
  504. {
  505. int err, clk;
  506. u8 n, clk_divider, mcu_cnt, div;
  507. u8 depth[] = {
  508. [RTSX_SSC_DEPTH_4M] = SSC_DEPTH_4M,
  509. [RTSX_SSC_DEPTH_2M] = SSC_DEPTH_2M,
  510. [RTSX_SSC_DEPTH_1M] = SSC_DEPTH_1M,
  511. [RTSX_SSC_DEPTH_500K] = SSC_DEPTH_500K,
  512. [RTSX_SSC_DEPTH_250K] = SSC_DEPTH_250K,
  513. };
  514. if (initial_mode) {
  515. /* We use 250k(around) here, in initial stage */
  516. clk_divider = SD_CLK_DIVIDE_128;
  517. card_clock = 30000000;
  518. } else {
  519. clk_divider = SD_CLK_DIVIDE_0;
  520. }
  521. err = rtsx_pci_write_register(pcr, SD_CFG1,
  522. SD_CLK_DIVIDE_MASK, clk_divider);
  523. if (err < 0)
  524. return err;
  525. card_clock /= 1000000;
  526. pcr_dbg(pcr, "Switch card clock to %dMHz\n", card_clock);
  527. clk = card_clock;
  528. if (!initial_mode && double_clk)
  529. clk = card_clock * 2;
  530. pcr_dbg(pcr, "Internal SSC clock: %dMHz (cur_clock = %d)\n",
  531. clk, pcr->cur_clock);
  532. if (clk == pcr->cur_clock)
  533. return 0;
  534. if (pcr->ops->conv_clk_and_div_n)
  535. n = (u8)pcr->ops->conv_clk_and_div_n(clk, CLK_TO_DIV_N);
  536. else
  537. n = (u8)(clk - 2);
  538. if ((clk <= 2) || (n > MAX_DIV_N_PCR))
  539. return -EINVAL;
  540. mcu_cnt = (u8)(125/clk + 3);
  541. if (mcu_cnt > 15)
  542. mcu_cnt = 15;
  543. /* Make sure that the SSC clock div_n is not less than MIN_DIV_N_PCR */
  544. div = CLK_DIV_1;
  545. while ((n < MIN_DIV_N_PCR) && (div < CLK_DIV_8)) {
  546. if (pcr->ops->conv_clk_and_div_n) {
  547. int dbl_clk = pcr->ops->conv_clk_and_div_n(n,
  548. DIV_N_TO_CLK) * 2;
  549. n = (u8)pcr->ops->conv_clk_and_div_n(dbl_clk,
  550. CLK_TO_DIV_N);
  551. } else {
  552. n = (n + 2) * 2 - 2;
  553. }
  554. div++;
  555. }
  556. pcr_dbg(pcr, "n = %d, div = %d\n", n, div);
  557. ssc_depth = depth[ssc_depth];
  558. if (double_clk)
  559. ssc_depth = double_ssc_depth(ssc_depth);
  560. ssc_depth = revise_ssc_depth(ssc_depth, div);
  561. pcr_dbg(pcr, "ssc_depth = %d\n", ssc_depth);
  562. rtsx_pci_init_cmd(pcr);
  563. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL,
  564. CLK_LOW_FREQ, CLK_LOW_FREQ);
  565. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_DIV,
  566. 0xFF, (div << 4) | mcu_cnt);
  567. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, 0);
  568. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL2,
  569. SSC_DEPTH_MASK, ssc_depth);
  570. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_DIV_N_0, 0xFF, n);
  571. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, SSC_RSTB);
  572. if (vpclk) {
  573. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPCLK0_CTL,
  574. PHASE_NOT_RESET, 0);
  575. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPCLK0_CTL,
  576. PHASE_NOT_RESET, PHASE_NOT_RESET);
  577. }
  578. err = rtsx_pci_send_cmd(pcr, 2000);
  579. if (err < 0)
  580. return err;
  581. /* Wait SSC clock stable */
  582. udelay(10);
  583. err = rtsx_pci_write_register(pcr, CLK_CTL, CLK_LOW_FREQ, 0);
  584. if (err < 0)
  585. return err;
  586. pcr->cur_clock = clk;
  587. return 0;
  588. }
  589. EXPORT_SYMBOL_GPL(rtsx_pci_switch_clock);
  590. int rtsx_pci_card_power_on(struct rtsx_pcr *pcr, int card)
  591. {
  592. if (pcr->ops->card_power_on)
  593. return pcr->ops->card_power_on(pcr, card);
  594. return 0;
  595. }
  596. EXPORT_SYMBOL_GPL(rtsx_pci_card_power_on);
  597. int rtsx_pci_card_power_off(struct rtsx_pcr *pcr, int card)
  598. {
  599. if (pcr->ops->card_power_off)
  600. return pcr->ops->card_power_off(pcr, card);
  601. return 0;
  602. }
  603. EXPORT_SYMBOL_GPL(rtsx_pci_card_power_off);
  604. int rtsx_pci_card_exclusive_check(struct rtsx_pcr *pcr, int card)
  605. {
  606. unsigned int cd_mask[] = {
  607. [RTSX_SD_CARD] = SD_EXIST,
  608. [RTSX_MS_CARD] = MS_EXIST
  609. };
  610. if (!(pcr->flags & PCR_MS_PMOS)) {
  611. /* When using single PMOS, accessing card is not permitted
  612. * if the existing card is not the designated one.
  613. */
  614. if (pcr->card_exist & (~cd_mask[card]))
  615. return -EIO;
  616. }
  617. return 0;
  618. }
  619. EXPORT_SYMBOL_GPL(rtsx_pci_card_exclusive_check);
  620. int rtsx_pci_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
  621. {
  622. if (pcr->ops->switch_output_voltage)
  623. return pcr->ops->switch_output_voltage(pcr, voltage);
  624. return 0;
  625. }
  626. EXPORT_SYMBOL_GPL(rtsx_pci_switch_output_voltage);
  627. unsigned int rtsx_pci_card_exist(struct rtsx_pcr *pcr)
  628. {
  629. unsigned int val;
  630. val = rtsx_pci_readl(pcr, RTSX_BIPR);
  631. if (pcr->ops->cd_deglitch)
  632. val = pcr->ops->cd_deglitch(pcr);
  633. return val;
  634. }
  635. EXPORT_SYMBOL_GPL(rtsx_pci_card_exist);
  636. void rtsx_pci_complete_unfinished_transfer(struct rtsx_pcr *pcr)
  637. {
  638. struct completion finish;
  639. pcr->finish_me = &finish;
  640. init_completion(&finish);
  641. if (pcr->done)
  642. complete(pcr->done);
  643. if (!pcr->remove_pci)
  644. rtsx_pci_stop_cmd(pcr);
  645. wait_for_completion_interruptible_timeout(&finish,
  646. msecs_to_jiffies(2));
  647. pcr->finish_me = NULL;
  648. }
  649. EXPORT_SYMBOL_GPL(rtsx_pci_complete_unfinished_transfer);
  650. static void rtsx_pci_card_detect(struct work_struct *work)
  651. {
  652. struct delayed_work *dwork;
  653. struct rtsx_pcr *pcr;
  654. unsigned long flags;
  655. unsigned int card_detect = 0, card_inserted, card_removed;
  656. u32 irq_status;
  657. dwork = to_delayed_work(work);
  658. pcr = container_of(dwork, struct rtsx_pcr, carddet_work);
  659. pcr_dbg(pcr, "--> %s\n", __func__);
  660. mutex_lock(&pcr->pcr_mutex);
  661. spin_lock_irqsave(&pcr->lock, flags);
  662. irq_status = rtsx_pci_readl(pcr, RTSX_BIPR);
  663. pcr_dbg(pcr, "irq_status: 0x%08x\n", irq_status);
  664. irq_status &= CARD_EXIST;
  665. card_inserted = pcr->card_inserted & irq_status;
  666. card_removed = pcr->card_removed;
  667. pcr->card_inserted = 0;
  668. pcr->card_removed = 0;
  669. spin_unlock_irqrestore(&pcr->lock, flags);
  670. if (card_inserted || card_removed) {
  671. pcr_dbg(pcr, "card_inserted: 0x%x, card_removed: 0x%x\n",
  672. card_inserted, card_removed);
  673. if (pcr->ops->cd_deglitch)
  674. card_inserted = pcr->ops->cd_deglitch(pcr);
  675. card_detect = card_inserted | card_removed;
  676. pcr->card_exist |= card_inserted;
  677. pcr->card_exist &= ~card_removed;
  678. }
  679. mutex_unlock(&pcr->pcr_mutex);
  680. if ((card_detect & SD_EXIST) && pcr->slots[RTSX_SD_CARD].card_event)
  681. pcr->slots[RTSX_SD_CARD].card_event(
  682. pcr->slots[RTSX_SD_CARD].p_dev);
  683. if ((card_detect & MS_EXIST) && pcr->slots[RTSX_MS_CARD].card_event)
  684. pcr->slots[RTSX_MS_CARD].card_event(
  685. pcr->slots[RTSX_MS_CARD].p_dev);
  686. }
  687. static irqreturn_t rtsx_pci_isr(int irq, void *dev_id)
  688. {
  689. struct rtsx_pcr *pcr = dev_id;
  690. u32 int_reg;
  691. if (!pcr)
  692. return IRQ_NONE;
  693. spin_lock(&pcr->lock);
  694. int_reg = rtsx_pci_readl(pcr, RTSX_BIPR);
  695. /* Clear interrupt flag */
  696. rtsx_pci_writel(pcr, RTSX_BIPR, int_reg);
  697. if ((int_reg & pcr->bier) == 0) {
  698. spin_unlock(&pcr->lock);
  699. return IRQ_NONE;
  700. }
  701. if (int_reg == 0xFFFFFFFF) {
  702. spin_unlock(&pcr->lock);
  703. return IRQ_HANDLED;
  704. }
  705. int_reg &= (pcr->bier | 0x7FFFFF);
  706. if (int_reg & SD_INT) {
  707. if (int_reg & SD_EXIST) {
  708. pcr->card_inserted |= SD_EXIST;
  709. } else {
  710. pcr->card_removed |= SD_EXIST;
  711. pcr->card_inserted &= ~SD_EXIST;
  712. }
  713. }
  714. if (int_reg & MS_INT) {
  715. if (int_reg & MS_EXIST) {
  716. pcr->card_inserted |= MS_EXIST;
  717. } else {
  718. pcr->card_removed |= MS_EXIST;
  719. pcr->card_inserted &= ~MS_EXIST;
  720. }
  721. }
  722. if (int_reg & (NEED_COMPLETE_INT | DELINK_INT)) {
  723. if (int_reg & (TRANS_FAIL_INT | DELINK_INT)) {
  724. pcr->trans_result = TRANS_RESULT_FAIL;
  725. if (pcr->done)
  726. complete(pcr->done);
  727. } else if (int_reg & TRANS_OK_INT) {
  728. pcr->trans_result = TRANS_RESULT_OK;
  729. if (pcr->done)
  730. complete(pcr->done);
  731. }
  732. }
  733. if (pcr->card_inserted || pcr->card_removed)
  734. schedule_delayed_work(&pcr->carddet_work,
  735. msecs_to_jiffies(200));
  736. spin_unlock(&pcr->lock);
  737. return IRQ_HANDLED;
  738. }
  739. static int rtsx_pci_acquire_irq(struct rtsx_pcr *pcr)
  740. {
  741. dev_info(&(pcr->pci->dev), "%s: pcr->msi_en = %d, pci->irq = %d\n",
  742. __func__, pcr->msi_en, pcr->pci->irq);
  743. if (request_irq(pcr->pci->irq, rtsx_pci_isr,
  744. pcr->msi_en ? 0 : IRQF_SHARED,
  745. DRV_NAME_RTSX_PCI, pcr)) {
  746. dev_err(&(pcr->pci->dev),
  747. "rtsx_sdmmc: unable to grab IRQ %d, disabling device\n",
  748. pcr->pci->irq);
  749. return -1;
  750. }
  751. pcr->irq = pcr->pci->irq;
  752. pci_intx(pcr->pci, !pcr->msi_en);
  753. return 0;
  754. }
  755. static void rtsx_pci_idle_work(struct work_struct *work)
  756. {
  757. struct delayed_work *dwork = to_delayed_work(work);
  758. struct rtsx_pcr *pcr = container_of(dwork, struct rtsx_pcr, idle_work);
  759. pcr_dbg(pcr, "--> %s\n", __func__);
  760. mutex_lock(&pcr->pcr_mutex);
  761. pcr->state = PDEV_STAT_IDLE;
  762. if (pcr->ops->disable_auto_blink)
  763. pcr->ops->disable_auto_blink(pcr);
  764. if (pcr->ops->turn_off_led)
  765. pcr->ops->turn_off_led(pcr);
  766. if (pcr->aspm_en)
  767. rtsx_pci_enable_aspm(pcr);
  768. mutex_unlock(&pcr->pcr_mutex);
  769. }
  770. #ifdef CONFIG_PM
  771. static void rtsx_pci_power_off(struct rtsx_pcr *pcr, u8 pm_state)
  772. {
  773. if (pcr->ops->turn_off_led)
  774. pcr->ops->turn_off_led(pcr);
  775. rtsx_pci_writel(pcr, RTSX_BIER, 0);
  776. pcr->bier = 0;
  777. rtsx_pci_write_register(pcr, PETXCFG, 0x08, 0x08);
  778. rtsx_pci_write_register(pcr, HOST_SLEEP_STATE, 0x03, pm_state);
  779. if (pcr->ops->force_power_down)
  780. pcr->ops->force_power_down(pcr, pm_state);
  781. }
  782. #endif
  783. static int rtsx_pci_init_hw(struct rtsx_pcr *pcr)
  784. {
  785. int err;
  786. pcr->pcie_cap = pci_find_capability(pcr->pci, PCI_CAP_ID_EXP);
  787. rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr);
  788. rtsx_pci_enable_bus_int(pcr);
  789. /* Power on SSC */
  790. err = rtsx_pci_write_register(pcr, FPDCTL, SSC_POWER_DOWN, 0);
  791. if (err < 0)
  792. return err;
  793. /* Wait SSC power stable */
  794. udelay(200);
  795. rtsx_pci_disable_aspm(pcr);
  796. if (pcr->ops->optimize_phy) {
  797. err = pcr->ops->optimize_phy(pcr);
  798. if (err < 0)
  799. return err;
  800. }
  801. rtsx_pci_init_cmd(pcr);
  802. /* Set mcu_cnt to 7 to ensure data can be sampled properly */
  803. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_DIV, 0x07, 0x07);
  804. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, HOST_SLEEP_STATE, 0x03, 0x00);
  805. /* Disable card clock */
  806. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_EN, 0x1E, 0);
  807. /* Reset delink mode */
  808. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CHANGE_LINK_STATE, 0x0A, 0);
  809. /* Card driving select */
  810. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DRIVE_SEL,
  811. 0xFF, pcr->card_drive_sel);
  812. /* Enable SSC Clock */
  813. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1,
  814. 0xFF, SSC_8X_EN | SSC_SEL_4M);
  815. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL2, 0xFF, 0x12);
  816. /* Disable cd_pwr_save */
  817. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CHANGE_LINK_STATE, 0x16, 0x10);
  818. /* Clear Link Ready Interrupt */
  819. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, IRQSTAT0,
  820. LINK_RDY_INT, LINK_RDY_INT);
  821. /* Enlarge the estimation window of PERST# glitch
  822. * to reduce the chance of invalid card interrupt
  823. */
  824. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PERST_GLITCH_WIDTH, 0xFF, 0x80);
  825. /* Update RC oscillator to 400k
  826. * bit[0] F_HIGH: for RC oscillator, Rst_value is 1'b1
  827. * 1: 2M 0: 400k
  828. */
  829. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, RCCTL, 0x01, 0x00);
  830. /* Set interrupt write clear
  831. * bit 1: U_elbi_if_rd_clr_en
  832. * 1: Enable ELBI interrupt[31:22] & [7:0] flag read clear
  833. * 0: ELBI interrupt flag[31:22] & [7:0] only can be write clear
  834. */
  835. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, NFTS_TX_CTRL, 0x02, 0);
  836. err = rtsx_pci_send_cmd(pcr, 100);
  837. if (err < 0)
  838. return err;
  839. /* Enable clk_request_n to enable clock power management */
  840. rtsx_pci_write_config_byte(pcr, pcr->pcie_cap + PCI_EXP_LNKCTL + 1, 1);
  841. /* Enter L1 when host tx idle */
  842. rtsx_pci_write_config_byte(pcr, 0x70F, 0x5B);
  843. if (pcr->ops->extra_init_hw) {
  844. err = pcr->ops->extra_init_hw(pcr);
  845. if (err < 0)
  846. return err;
  847. }
  848. /* No CD interrupt if probing driver with card inserted.
  849. * So we need to initialize pcr->card_exist here.
  850. */
  851. if (pcr->ops->cd_deglitch)
  852. pcr->card_exist = pcr->ops->cd_deglitch(pcr);
  853. else
  854. pcr->card_exist = rtsx_pci_readl(pcr, RTSX_BIPR) & CARD_EXIST;
  855. return 0;
  856. }
  857. static int rtsx_pci_init_chip(struct rtsx_pcr *pcr)
  858. {
  859. int err;
  860. spin_lock_init(&pcr->lock);
  861. mutex_init(&pcr->pcr_mutex);
  862. switch (PCI_PID(pcr)) {
  863. default:
  864. case 0x5209:
  865. rts5209_init_params(pcr);
  866. break;
  867. case 0x5229:
  868. rts5229_init_params(pcr);
  869. break;
  870. case 0x5289:
  871. rtl8411_init_params(pcr);
  872. break;
  873. case 0x5227:
  874. rts5227_init_params(pcr);
  875. break;
  876. case 0x522A:
  877. rts522a_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");