rtsx_pcr.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Driver for Realtek PCI-Express card reader
  3. *
  4. * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
  5. *
  6. * Author:
  7. * Wei WANG <[email protected]>
  8. */
  9. #include <linux/pci.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/highmem.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/delay.h>
  16. #include <linux/idr.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/mfd/core.h>
  19. #include <linux/rtsx_pci.h>
  20. #include <linux/mmc/card.h>
  21. #include <asm/unaligned.h>
  22. #include <linux/pm.h>
  23. #include <linux/pm_runtime.h>
  24. #include "rtsx_pcr.h"
  25. #include "rts5261.h"
  26. #include "rts5228.h"
  27. static bool msi_en = true;
  28. module_param(msi_en, bool, S_IRUGO | S_IWUSR);
  29. MODULE_PARM_DESC(msi_en, "Enable MSI");
  30. static DEFINE_IDR(rtsx_pci_idr);
  31. static DEFINE_SPINLOCK(rtsx_pci_lock);
  32. static struct mfd_cell rtsx_pcr_cells[] = {
  33. [RTSX_SD_CARD] = {
  34. .name = DRV_NAME_RTSX_PCI_SDMMC,
  35. },
  36. };
  37. static const struct pci_device_id rtsx_pci_ids[] = {
  38. { PCI_DEVICE(0x10EC, 0x5209), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  39. { PCI_DEVICE(0x10EC, 0x5229), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  40. { PCI_DEVICE(0x10EC, 0x5289), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  41. { PCI_DEVICE(0x10EC, 0x5227), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  42. { PCI_DEVICE(0x10EC, 0x522A), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  43. { PCI_DEVICE(0x10EC, 0x5249), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  44. { PCI_DEVICE(0x10EC, 0x5287), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  45. { PCI_DEVICE(0x10EC, 0x5286), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  46. { PCI_DEVICE(0x10EC, 0x524A), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  47. { PCI_DEVICE(0x10EC, 0x525A), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  48. { PCI_DEVICE(0x10EC, 0x5260), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  49. { PCI_DEVICE(0x10EC, 0x5261), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  50. { PCI_DEVICE(0x10EC, 0x5228), PCI_CLASS_OTHERS << 16, 0xFF0000 },
  51. { 0, }
  52. };
  53. MODULE_DEVICE_TABLE(pci, rtsx_pci_ids);
  54. static int rtsx_comm_set_ltr_latency(struct rtsx_pcr *pcr, u32 latency)
  55. {
  56. rtsx_pci_write_register(pcr, MSGTXDATA0,
  57. MASK_8_BIT_DEF, (u8) (latency & 0xFF));
  58. rtsx_pci_write_register(pcr, MSGTXDATA1,
  59. MASK_8_BIT_DEF, (u8)((latency >> 8) & 0xFF));
  60. rtsx_pci_write_register(pcr, MSGTXDATA2,
  61. MASK_8_BIT_DEF, (u8)((latency >> 16) & 0xFF));
  62. rtsx_pci_write_register(pcr, MSGTXDATA3,
  63. MASK_8_BIT_DEF, (u8)((latency >> 24) & 0xFF));
  64. rtsx_pci_write_register(pcr, LTR_CTL, LTR_TX_EN_MASK |
  65. LTR_LATENCY_MODE_MASK, LTR_TX_EN_1 | LTR_LATENCY_MODE_SW);
  66. return 0;
  67. }
  68. int rtsx_set_ltr_latency(struct rtsx_pcr *pcr, u32 latency)
  69. {
  70. return rtsx_comm_set_ltr_latency(pcr, latency);
  71. }
  72. static void rtsx_comm_set_aspm(struct rtsx_pcr *pcr, bool enable)
  73. {
  74. if (pcr->aspm_enabled == enable)
  75. return;
  76. if (pcr->aspm_mode == ASPM_MODE_CFG) {
  77. pcie_capability_clear_and_set_word(pcr->pci, PCI_EXP_LNKCTL,
  78. PCI_EXP_LNKCTL_ASPMC,
  79. enable ? pcr->aspm_en : 0);
  80. } else if (pcr->aspm_mode == ASPM_MODE_REG) {
  81. if (pcr->aspm_en & 0x02)
  82. rtsx_pci_write_register(pcr, ASPM_FORCE_CTL, FORCE_ASPM_CTL0 |
  83. FORCE_ASPM_CTL1, enable ? 0 : FORCE_ASPM_CTL0 | FORCE_ASPM_CTL1);
  84. else
  85. rtsx_pci_write_register(pcr, ASPM_FORCE_CTL, FORCE_ASPM_CTL0 |
  86. FORCE_ASPM_CTL1, FORCE_ASPM_CTL0 | FORCE_ASPM_CTL1);
  87. }
  88. if (!enable && (pcr->aspm_en & 0x02))
  89. mdelay(10);
  90. pcr->aspm_enabled = enable;
  91. }
  92. static void rtsx_disable_aspm(struct rtsx_pcr *pcr)
  93. {
  94. if (pcr->ops->set_aspm)
  95. pcr->ops->set_aspm(pcr, false);
  96. else
  97. rtsx_comm_set_aspm(pcr, false);
  98. }
  99. int rtsx_set_l1off_sub(struct rtsx_pcr *pcr, u8 val)
  100. {
  101. rtsx_pci_write_register(pcr, L1SUB_CONFIG3, 0xFF, val);
  102. return 0;
  103. }
  104. static void rtsx_set_l1off_sub_cfg_d0(struct rtsx_pcr *pcr, int active)
  105. {
  106. if (pcr->ops->set_l1off_cfg_sub_d0)
  107. pcr->ops->set_l1off_cfg_sub_d0(pcr, active);
  108. }
  109. static void rtsx_comm_pm_full_on(struct rtsx_pcr *pcr)
  110. {
  111. struct rtsx_cr_option *option = &pcr->option;
  112. rtsx_disable_aspm(pcr);
  113. /* Fixes DMA transfer timeout issue after disabling ASPM on RTS5260 */
  114. msleep(1);
  115. if (option->ltr_enabled)
  116. rtsx_set_ltr_latency(pcr, option->ltr_active_latency);
  117. if (rtsx_check_dev_flag(pcr, LTR_L1SS_PWR_GATE_EN))
  118. rtsx_set_l1off_sub_cfg_d0(pcr, 1);
  119. }
  120. static void rtsx_pm_full_on(struct rtsx_pcr *pcr)
  121. {
  122. rtsx_comm_pm_full_on(pcr);
  123. }
  124. void rtsx_pci_start_run(struct rtsx_pcr *pcr)
  125. {
  126. /* If pci device removed, don't queue idle work any more */
  127. if (pcr->remove_pci)
  128. return;
  129. if (pcr->state != PDEV_STAT_RUN) {
  130. pcr->state = PDEV_STAT_RUN;
  131. if (pcr->ops->enable_auto_blink)
  132. pcr->ops->enable_auto_blink(pcr);
  133. rtsx_pm_full_on(pcr);
  134. }
  135. }
  136. EXPORT_SYMBOL_GPL(rtsx_pci_start_run);
  137. int rtsx_pci_write_register(struct rtsx_pcr *pcr, u16 addr, u8 mask, u8 data)
  138. {
  139. int i;
  140. u32 val = HAIMR_WRITE_START;
  141. val |= (u32)(addr & 0x3FFF) << 16;
  142. val |= (u32)mask << 8;
  143. val |= (u32)data;
  144. rtsx_pci_writel(pcr, RTSX_HAIMR, val);
  145. for (i = 0; i < MAX_RW_REG_CNT; i++) {
  146. val = rtsx_pci_readl(pcr, RTSX_HAIMR);
  147. if ((val & HAIMR_TRANS_END) == 0) {
  148. if (data != (u8)val)
  149. return -EIO;
  150. return 0;
  151. }
  152. }
  153. return -ETIMEDOUT;
  154. }
  155. EXPORT_SYMBOL_GPL(rtsx_pci_write_register);
  156. int rtsx_pci_read_register(struct rtsx_pcr *pcr, u16 addr, u8 *data)
  157. {
  158. u32 val = HAIMR_READ_START;
  159. int i;
  160. val |= (u32)(addr & 0x3FFF) << 16;
  161. rtsx_pci_writel(pcr, RTSX_HAIMR, val);
  162. for (i = 0; i < MAX_RW_REG_CNT; i++) {
  163. val = rtsx_pci_readl(pcr, RTSX_HAIMR);
  164. if ((val & HAIMR_TRANS_END) == 0)
  165. break;
  166. }
  167. if (i >= MAX_RW_REG_CNT)
  168. return -ETIMEDOUT;
  169. if (data)
  170. *data = (u8)(val & 0xFF);
  171. return 0;
  172. }
  173. EXPORT_SYMBOL_GPL(rtsx_pci_read_register);
  174. int __rtsx_pci_write_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 val)
  175. {
  176. int err, i, finished = 0;
  177. u8 tmp;
  178. rtsx_pci_write_register(pcr, PHYDATA0, 0xFF, (u8)val);
  179. rtsx_pci_write_register(pcr, PHYDATA1, 0xFF, (u8)(val >> 8));
  180. rtsx_pci_write_register(pcr, PHYADDR, 0xFF, addr);
  181. rtsx_pci_write_register(pcr, PHYRWCTL, 0xFF, 0x81);
  182. for (i = 0; i < 100000; i++) {
  183. err = rtsx_pci_read_register(pcr, PHYRWCTL, &tmp);
  184. if (err < 0)
  185. return err;
  186. if (!(tmp & 0x80)) {
  187. finished = 1;
  188. break;
  189. }
  190. }
  191. if (!finished)
  192. return -ETIMEDOUT;
  193. return 0;
  194. }
  195. int rtsx_pci_write_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 val)
  196. {
  197. if (pcr->ops->write_phy)
  198. return pcr->ops->write_phy(pcr, addr, val);
  199. return __rtsx_pci_write_phy_register(pcr, addr, val);
  200. }
  201. EXPORT_SYMBOL_GPL(rtsx_pci_write_phy_register);
  202. int __rtsx_pci_read_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 *val)
  203. {
  204. int err, i, finished = 0;
  205. u16 data;
  206. u8 tmp, val1, val2;
  207. rtsx_pci_write_register(pcr, PHYADDR, 0xFF, addr);
  208. rtsx_pci_write_register(pcr, PHYRWCTL, 0xFF, 0x80);
  209. for (i = 0; i < 100000; i++) {
  210. err = rtsx_pci_read_register(pcr, PHYRWCTL, &tmp);
  211. if (err < 0)
  212. return err;
  213. if (!(tmp & 0x80)) {
  214. finished = 1;
  215. break;
  216. }
  217. }
  218. if (!finished)
  219. return -ETIMEDOUT;
  220. rtsx_pci_read_register(pcr, PHYDATA0, &val1);
  221. rtsx_pci_read_register(pcr, PHYDATA1, &val2);
  222. data = val1 | (val2 << 8);
  223. if (val)
  224. *val = data;
  225. return 0;
  226. }
  227. int rtsx_pci_read_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 *val)
  228. {
  229. if (pcr->ops->read_phy)
  230. return pcr->ops->read_phy(pcr, addr, val);
  231. return __rtsx_pci_read_phy_register(pcr, addr, val);
  232. }
  233. EXPORT_SYMBOL_GPL(rtsx_pci_read_phy_register);
  234. void rtsx_pci_stop_cmd(struct rtsx_pcr *pcr)
  235. {
  236. if (pcr->ops->stop_cmd)
  237. return pcr->ops->stop_cmd(pcr);
  238. rtsx_pci_writel(pcr, RTSX_HCBCTLR, STOP_CMD);
  239. rtsx_pci_writel(pcr, RTSX_HDBCTLR, STOP_DMA);
  240. rtsx_pci_write_register(pcr, DMACTL, 0x80, 0x80);
  241. rtsx_pci_write_register(pcr, RBCTL, 0x80, 0x80);
  242. }
  243. EXPORT_SYMBOL_GPL(rtsx_pci_stop_cmd);
  244. void rtsx_pci_add_cmd(struct rtsx_pcr *pcr,
  245. u8 cmd_type, u16 reg_addr, u8 mask, u8 data)
  246. {
  247. unsigned long flags;
  248. u32 val = 0;
  249. u32 *ptr = (u32 *)(pcr->host_cmds_ptr);
  250. val |= (u32)(cmd_type & 0x03) << 30;
  251. val |= (u32)(reg_addr & 0x3FFF) << 16;
  252. val |= (u32)mask << 8;
  253. val |= (u32)data;
  254. spin_lock_irqsave(&pcr->lock, flags);
  255. ptr += pcr->ci;
  256. if (pcr->ci < (HOST_CMDS_BUF_LEN / 4)) {
  257. put_unaligned_le32(val, ptr);
  258. ptr++;
  259. pcr->ci++;
  260. }
  261. spin_unlock_irqrestore(&pcr->lock, flags);
  262. }
  263. EXPORT_SYMBOL_GPL(rtsx_pci_add_cmd);
  264. void rtsx_pci_send_cmd_no_wait(struct rtsx_pcr *pcr)
  265. {
  266. u32 val = 1 << 31;
  267. rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr);
  268. val |= (u32)(pcr->ci * 4) & 0x00FFFFFF;
  269. /* Hardware Auto Response */
  270. val |= 0x40000000;
  271. rtsx_pci_writel(pcr, RTSX_HCBCTLR, val);
  272. }
  273. EXPORT_SYMBOL_GPL(rtsx_pci_send_cmd_no_wait);
  274. int rtsx_pci_send_cmd(struct rtsx_pcr *pcr, int timeout)
  275. {
  276. struct completion trans_done;
  277. u32 val = 1 << 31;
  278. long timeleft;
  279. unsigned long flags;
  280. int err = 0;
  281. spin_lock_irqsave(&pcr->lock, flags);
  282. /* set up data structures for the wakeup system */
  283. pcr->done = &trans_done;
  284. pcr->trans_result = TRANS_NOT_READY;
  285. init_completion(&trans_done);
  286. rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr);
  287. val |= (u32)(pcr->ci * 4) & 0x00FFFFFF;
  288. /* Hardware Auto Response */
  289. val |= 0x40000000;
  290. rtsx_pci_writel(pcr, RTSX_HCBCTLR, val);
  291. spin_unlock_irqrestore(&pcr->lock, flags);
  292. /* Wait for TRANS_OK_INT */
  293. timeleft = wait_for_completion_interruptible_timeout(
  294. &trans_done, msecs_to_jiffies(timeout));
  295. if (timeleft <= 0) {
  296. pcr_dbg(pcr, "Timeout (%s %d)\n", __func__, __LINE__);
  297. err = -ETIMEDOUT;
  298. goto finish_send_cmd;
  299. }
  300. spin_lock_irqsave(&pcr->lock, flags);
  301. if (pcr->trans_result == TRANS_RESULT_FAIL)
  302. err = -EINVAL;
  303. else if (pcr->trans_result == TRANS_RESULT_OK)
  304. err = 0;
  305. else if (pcr->trans_result == TRANS_NO_DEVICE)
  306. err = -ENODEV;
  307. spin_unlock_irqrestore(&pcr->lock, flags);
  308. finish_send_cmd:
  309. spin_lock_irqsave(&pcr->lock, flags);
  310. pcr->done = NULL;
  311. spin_unlock_irqrestore(&pcr->lock, flags);
  312. if ((err < 0) && (err != -ENODEV))
  313. rtsx_pci_stop_cmd(pcr);
  314. if (pcr->finish_me)
  315. complete(pcr->finish_me);
  316. return err;
  317. }
  318. EXPORT_SYMBOL_GPL(rtsx_pci_send_cmd);
  319. static void rtsx_pci_add_sg_tbl(struct rtsx_pcr *pcr,
  320. dma_addr_t addr, unsigned int len, int end)
  321. {
  322. u64 *ptr = (u64 *)(pcr->host_sg_tbl_ptr) + pcr->sgi;
  323. u64 val;
  324. u8 option = RTSX_SG_VALID | RTSX_SG_TRANS_DATA;
  325. pcr_dbg(pcr, "DMA addr: 0x%x, Len: 0x%x\n", (unsigned int)addr, len);
  326. if (end)
  327. option |= RTSX_SG_END;
  328. if ((PCI_PID(pcr) == PID_5261) || (PCI_PID(pcr) == PID_5228)) {
  329. if (len > 0xFFFF)
  330. val = ((u64)addr << 32) | (((u64)len & 0xFFFF) << 16)
  331. | (((u64)len >> 16) << 6) | option;
  332. else
  333. val = ((u64)addr << 32) | ((u64)len << 16) | option;
  334. } else {
  335. val = ((u64)addr << 32) | ((u64)len << 12) | option;
  336. }
  337. put_unaligned_le64(val, ptr);
  338. pcr->sgi++;
  339. }
  340. int rtsx_pci_transfer_data(struct rtsx_pcr *pcr, struct scatterlist *sglist,
  341. int num_sg, bool read, int timeout)
  342. {
  343. int err = 0, count;
  344. pcr_dbg(pcr, "--> %s: num_sg = %d\n", __func__, num_sg);
  345. count = rtsx_pci_dma_map_sg(pcr, sglist, num_sg, read);
  346. if (count < 1)
  347. return -EINVAL;
  348. pcr_dbg(pcr, "DMA mapping count: %d\n", count);
  349. err = rtsx_pci_dma_transfer(pcr, sglist, count, read, timeout);
  350. rtsx_pci_dma_unmap_sg(pcr, sglist, num_sg, read);
  351. return err;
  352. }
  353. EXPORT_SYMBOL_GPL(rtsx_pci_transfer_data);
  354. int rtsx_pci_dma_map_sg(struct rtsx_pcr *pcr, struct scatterlist *sglist,
  355. int num_sg, bool read)
  356. {
  357. enum dma_data_direction dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  358. if (pcr->remove_pci)
  359. return -EINVAL;
  360. if ((sglist == NULL) || (num_sg <= 0))
  361. return -EINVAL;
  362. return dma_map_sg(&(pcr->pci->dev), sglist, num_sg, dir);
  363. }
  364. EXPORT_SYMBOL_GPL(rtsx_pci_dma_map_sg);
  365. void rtsx_pci_dma_unmap_sg(struct rtsx_pcr *pcr, struct scatterlist *sglist,
  366. int num_sg, bool read)
  367. {
  368. enum dma_data_direction dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  369. dma_unmap_sg(&(pcr->pci->dev), sglist, num_sg, dir);
  370. }
  371. EXPORT_SYMBOL_GPL(rtsx_pci_dma_unmap_sg);
  372. int rtsx_pci_dma_transfer(struct rtsx_pcr *pcr, struct scatterlist *sglist,
  373. int count, bool read, int timeout)
  374. {
  375. struct completion trans_done;
  376. struct scatterlist *sg;
  377. dma_addr_t addr;
  378. long timeleft;
  379. unsigned long flags;
  380. unsigned int len;
  381. int i, err = 0;
  382. u32 val;
  383. u8 dir = read ? DEVICE_TO_HOST : HOST_TO_DEVICE;
  384. if (pcr->remove_pci)
  385. return -ENODEV;
  386. if ((sglist == NULL) || (count < 1))
  387. return -EINVAL;
  388. val = ((u32)(dir & 0x01) << 29) | TRIG_DMA | ADMA_MODE;
  389. pcr->sgi = 0;
  390. for_each_sg(sglist, sg, count, i) {
  391. addr = sg_dma_address(sg);
  392. len = sg_dma_len(sg);
  393. rtsx_pci_add_sg_tbl(pcr, addr, len, i == count - 1);
  394. }
  395. spin_lock_irqsave(&pcr->lock, flags);
  396. pcr->done = &trans_done;
  397. pcr->trans_result = TRANS_NOT_READY;
  398. init_completion(&trans_done);
  399. rtsx_pci_writel(pcr, RTSX_HDBAR, pcr->host_sg_tbl_addr);
  400. rtsx_pci_writel(pcr, RTSX_HDBCTLR, val);
  401. spin_unlock_irqrestore(&pcr->lock, flags);
  402. timeleft = wait_for_completion_interruptible_timeout(
  403. &trans_done, msecs_to_jiffies(timeout));
  404. if (timeleft <= 0) {
  405. pcr_dbg(pcr, "Timeout (%s %d)\n", __func__, __LINE__);
  406. err = -ETIMEDOUT;
  407. goto out;
  408. }
  409. spin_lock_irqsave(&pcr->lock, flags);
  410. if (pcr->trans_result == TRANS_RESULT_FAIL) {
  411. err = -EILSEQ;
  412. if (pcr->dma_error_count < RTS_MAX_TIMES_FREQ_REDUCTION)
  413. pcr->dma_error_count++;
  414. }
  415. else if (pcr->trans_result == TRANS_NO_DEVICE)
  416. err = -ENODEV;
  417. spin_unlock_irqrestore(&pcr->lock, flags);
  418. out:
  419. spin_lock_irqsave(&pcr->lock, flags);
  420. pcr->done = NULL;
  421. spin_unlock_irqrestore(&pcr->lock, flags);
  422. if ((err < 0) && (err != -ENODEV))
  423. rtsx_pci_stop_cmd(pcr);
  424. if (pcr->finish_me)
  425. complete(pcr->finish_me);
  426. return err;
  427. }
  428. EXPORT_SYMBOL_GPL(rtsx_pci_dma_transfer);
  429. int rtsx_pci_read_ppbuf(struct rtsx_pcr *pcr, u8 *buf, int buf_len)
  430. {
  431. int err;
  432. int i, j;
  433. u16 reg;
  434. u8 *ptr;
  435. if (buf_len > 512)
  436. buf_len = 512;
  437. ptr = buf;
  438. reg = PPBUF_BASE2;
  439. for (i = 0; i < buf_len / 256; i++) {
  440. rtsx_pci_init_cmd(pcr);
  441. for (j = 0; j < 256; j++)
  442. rtsx_pci_add_cmd(pcr, READ_REG_CMD, reg++, 0, 0);
  443. err = rtsx_pci_send_cmd(pcr, 250);
  444. if (err < 0)
  445. return err;
  446. memcpy(ptr, rtsx_pci_get_cmd_data(pcr), 256);
  447. ptr += 256;
  448. }
  449. if (buf_len % 256) {
  450. rtsx_pci_init_cmd(pcr);
  451. for (j = 0; j < buf_len % 256; j++)
  452. rtsx_pci_add_cmd(pcr, READ_REG_CMD, reg++, 0, 0);
  453. err = rtsx_pci_send_cmd(pcr, 250);
  454. if (err < 0)
  455. return err;
  456. }
  457. memcpy(ptr, rtsx_pci_get_cmd_data(pcr), buf_len % 256);
  458. return 0;
  459. }
  460. EXPORT_SYMBOL_GPL(rtsx_pci_read_ppbuf);
  461. int rtsx_pci_write_ppbuf(struct rtsx_pcr *pcr, u8 *buf, int buf_len)
  462. {
  463. int err;
  464. int i, j;
  465. u16 reg;
  466. u8 *ptr;
  467. if (buf_len > 512)
  468. buf_len = 512;
  469. ptr = buf;
  470. reg = PPBUF_BASE2;
  471. for (i = 0; i < buf_len / 256; i++) {
  472. rtsx_pci_init_cmd(pcr);
  473. for (j = 0; j < 256; j++) {
  474. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
  475. reg++, 0xFF, *ptr);
  476. ptr++;
  477. }
  478. err = rtsx_pci_send_cmd(pcr, 250);
  479. if (err < 0)
  480. return err;
  481. }
  482. if (buf_len % 256) {
  483. rtsx_pci_init_cmd(pcr);
  484. for (j = 0; j < buf_len % 256; j++) {
  485. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
  486. reg++, 0xFF, *ptr);
  487. ptr++;
  488. }
  489. err = rtsx_pci_send_cmd(pcr, 250);
  490. if (err < 0)
  491. return err;
  492. }
  493. return 0;
  494. }
  495. EXPORT_SYMBOL_GPL(rtsx_pci_write_ppbuf);
  496. static int rtsx_pci_set_pull_ctl(struct rtsx_pcr *pcr, const u32 *tbl)
  497. {
  498. rtsx_pci_init_cmd(pcr);
  499. while (*tbl & 0xFFFF0000) {
  500. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
  501. (u16)(*tbl >> 16), 0xFF, (u8)(*tbl));
  502. tbl++;
  503. }
  504. return rtsx_pci_send_cmd(pcr, 100);
  505. }
  506. int rtsx_pci_card_pull_ctl_enable(struct rtsx_pcr *pcr, int card)
  507. {
  508. const u32 *tbl;
  509. if (card == RTSX_SD_CARD)
  510. tbl = pcr->sd_pull_ctl_enable_tbl;
  511. else if (card == RTSX_MS_CARD)
  512. tbl = pcr->ms_pull_ctl_enable_tbl;
  513. else
  514. return -EINVAL;
  515. return rtsx_pci_set_pull_ctl(pcr, tbl);
  516. }
  517. EXPORT_SYMBOL_GPL(rtsx_pci_card_pull_ctl_enable);
  518. int rtsx_pci_card_pull_ctl_disable(struct rtsx_pcr *pcr, int card)
  519. {
  520. const u32 *tbl;
  521. if (card == RTSX_SD_CARD)
  522. tbl = pcr->sd_pull_ctl_disable_tbl;
  523. else if (card == RTSX_MS_CARD)
  524. tbl = pcr->ms_pull_ctl_disable_tbl;
  525. else
  526. return -EINVAL;
  527. return rtsx_pci_set_pull_ctl(pcr, tbl);
  528. }
  529. EXPORT_SYMBOL_GPL(rtsx_pci_card_pull_ctl_disable);
  530. static void rtsx_pci_enable_bus_int(struct rtsx_pcr *pcr)
  531. {
  532. struct rtsx_hw_param *hw_param = &pcr->hw_param;
  533. pcr->bier = TRANS_OK_INT_EN | TRANS_FAIL_INT_EN | SD_INT_EN
  534. | hw_param->interrupt_en;
  535. if (pcr->num_slots > 1)
  536. pcr->bier |= MS_INT_EN;
  537. /* Enable Bus Interrupt */
  538. rtsx_pci_writel(pcr, RTSX_BIER, pcr->bier);
  539. pcr_dbg(pcr, "RTSX_BIER: 0x%08x\n", pcr->bier);
  540. }
  541. static inline u8 double_ssc_depth(u8 depth)
  542. {
  543. return ((depth > 1) ? (depth - 1) : depth);
  544. }
  545. static u8 revise_ssc_depth(u8 ssc_depth, u8 div)
  546. {
  547. if (div > CLK_DIV_1) {
  548. if (ssc_depth > (div - 1))
  549. ssc_depth -= (div - 1);
  550. else
  551. ssc_depth = SSC_DEPTH_4M;
  552. }
  553. return ssc_depth;
  554. }
  555. int rtsx_pci_switch_clock(struct rtsx_pcr *pcr, unsigned int card_clock,
  556. u8 ssc_depth, bool initial_mode, bool double_clk, bool vpclk)
  557. {
  558. int err, clk;
  559. u8 n, clk_divider, mcu_cnt, div;
  560. static const u8 depth[] = {
  561. [RTSX_SSC_DEPTH_4M] = SSC_DEPTH_4M,
  562. [RTSX_SSC_DEPTH_2M] = SSC_DEPTH_2M,
  563. [RTSX_SSC_DEPTH_1M] = SSC_DEPTH_1M,
  564. [RTSX_SSC_DEPTH_500K] = SSC_DEPTH_500K,
  565. [RTSX_SSC_DEPTH_250K] = SSC_DEPTH_250K,
  566. };
  567. if (PCI_PID(pcr) == PID_5261)
  568. return rts5261_pci_switch_clock(pcr, card_clock,
  569. ssc_depth, initial_mode, double_clk, vpclk);
  570. if (PCI_PID(pcr) == PID_5228)
  571. return rts5228_pci_switch_clock(pcr, card_clock,
  572. ssc_depth, initial_mode, double_clk, vpclk);
  573. if (initial_mode) {
  574. /* We use 250k(around) here, in initial stage */
  575. clk_divider = SD_CLK_DIVIDE_128;
  576. card_clock = 30000000;
  577. } else {
  578. clk_divider = SD_CLK_DIVIDE_0;
  579. }
  580. err = rtsx_pci_write_register(pcr, SD_CFG1,
  581. SD_CLK_DIVIDE_MASK, clk_divider);
  582. if (err < 0)
  583. return err;
  584. /* Reduce card clock by 20MHz each time a DMA transfer error occurs */
  585. if (card_clock == UHS_SDR104_MAX_DTR &&
  586. pcr->dma_error_count &&
  587. PCI_PID(pcr) == RTS5227_DEVICE_ID)
  588. card_clock = UHS_SDR104_MAX_DTR -
  589. (pcr->dma_error_count * 20000000);
  590. card_clock /= 1000000;
  591. pcr_dbg(pcr, "Switch card clock to %dMHz\n", card_clock);
  592. clk = card_clock;
  593. if (!initial_mode && double_clk)
  594. clk = card_clock * 2;
  595. pcr_dbg(pcr, "Internal SSC clock: %dMHz (cur_clock = %d)\n",
  596. clk, pcr->cur_clock);
  597. if (clk == pcr->cur_clock)
  598. return 0;
  599. if (pcr->ops->conv_clk_and_div_n)
  600. n = (u8)pcr->ops->conv_clk_and_div_n(clk, CLK_TO_DIV_N);
  601. else
  602. n = (u8)(clk - 2);
  603. if ((clk <= 2) || (n > MAX_DIV_N_PCR))
  604. return -EINVAL;
  605. mcu_cnt = (u8)(125/clk + 3);
  606. if (mcu_cnt > 15)
  607. mcu_cnt = 15;
  608. /* Make sure that the SSC clock div_n is not less than MIN_DIV_N_PCR */
  609. div = CLK_DIV_1;
  610. while ((n < MIN_DIV_N_PCR) && (div < CLK_DIV_8)) {
  611. if (pcr->ops->conv_clk_and_div_n) {
  612. int dbl_clk = pcr->ops->conv_clk_and_div_n(n,
  613. DIV_N_TO_CLK) * 2;
  614. n = (u8)pcr->ops->conv_clk_and_div_n(dbl_clk,
  615. CLK_TO_DIV_N);
  616. } else {
  617. n = (n + 2) * 2 - 2;
  618. }
  619. div++;
  620. }
  621. pcr_dbg(pcr, "n = %d, div = %d\n", n, div);
  622. ssc_depth = depth[ssc_depth];
  623. if (double_clk)
  624. ssc_depth = double_ssc_depth(ssc_depth);
  625. ssc_depth = revise_ssc_depth(ssc_depth, div);
  626. pcr_dbg(pcr, "ssc_depth = %d\n", ssc_depth);
  627. rtsx_pci_init_cmd(pcr);
  628. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL,
  629. CLK_LOW_FREQ, CLK_LOW_FREQ);
  630. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_DIV,
  631. 0xFF, (div << 4) | mcu_cnt);
  632. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, 0);
  633. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL2,
  634. SSC_DEPTH_MASK, ssc_depth);
  635. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_DIV_N_0, 0xFF, n);
  636. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, SSC_RSTB);
  637. if (vpclk) {
  638. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPCLK0_CTL,
  639. PHASE_NOT_RESET, 0);
  640. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPCLK0_CTL,
  641. PHASE_NOT_RESET, PHASE_NOT_RESET);
  642. }
  643. err = rtsx_pci_send_cmd(pcr, 2000);
  644. if (err < 0)
  645. return err;
  646. /* Wait SSC clock stable */
  647. udelay(SSC_CLOCK_STABLE_WAIT);
  648. err = rtsx_pci_write_register(pcr, CLK_CTL, CLK_LOW_FREQ, 0);
  649. if (err < 0)
  650. return err;
  651. pcr->cur_clock = clk;
  652. return 0;
  653. }
  654. EXPORT_SYMBOL_GPL(rtsx_pci_switch_clock);
  655. int rtsx_pci_card_power_on(struct rtsx_pcr *pcr, int card)
  656. {
  657. if (pcr->ops->card_power_on)
  658. return pcr->ops->card_power_on(pcr, card);
  659. return 0;
  660. }
  661. EXPORT_SYMBOL_GPL(rtsx_pci_card_power_on);
  662. int rtsx_pci_card_power_off(struct rtsx_pcr *pcr, int card)
  663. {
  664. if (pcr->ops->card_power_off)
  665. return pcr->ops->card_power_off(pcr, card);
  666. return 0;
  667. }
  668. EXPORT_SYMBOL_GPL(rtsx_pci_card_power_off);
  669. int rtsx_pci_card_exclusive_check(struct rtsx_pcr *pcr, int card)
  670. {
  671. static const unsigned int cd_mask[] = {
  672. [RTSX_SD_CARD] = SD_EXIST,
  673. [RTSX_MS_CARD] = MS_EXIST
  674. };
  675. if (!(pcr->flags & PCR_MS_PMOS)) {
  676. /* When using single PMOS, accessing card is not permitted
  677. * if the existing card is not the designated one.
  678. */
  679. if (pcr->card_exist & (~cd_mask[card]))
  680. return -EIO;
  681. }
  682. return 0;
  683. }
  684. EXPORT_SYMBOL_GPL(rtsx_pci_card_exclusive_check);
  685. int rtsx_pci_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
  686. {
  687. if (pcr->ops->switch_output_voltage)
  688. return pcr->ops->switch_output_voltage(pcr, voltage);
  689. return 0;
  690. }
  691. EXPORT_SYMBOL_GPL(rtsx_pci_switch_output_voltage);
  692. unsigned int rtsx_pci_card_exist(struct rtsx_pcr *pcr)
  693. {
  694. unsigned int val;
  695. val = rtsx_pci_readl(pcr, RTSX_BIPR);
  696. if (pcr->ops->cd_deglitch)
  697. val = pcr->ops->cd_deglitch(pcr);
  698. return val;
  699. }
  700. EXPORT_SYMBOL_GPL(rtsx_pci_card_exist);
  701. void rtsx_pci_complete_unfinished_transfer(struct rtsx_pcr *pcr)
  702. {
  703. struct completion finish;
  704. pcr->finish_me = &finish;
  705. init_completion(&finish);
  706. if (pcr->done)
  707. complete(pcr->done);
  708. if (!pcr->remove_pci)
  709. rtsx_pci_stop_cmd(pcr);
  710. wait_for_completion_interruptible_timeout(&finish,
  711. msecs_to_jiffies(2));
  712. pcr->finish_me = NULL;
  713. }
  714. EXPORT_SYMBOL_GPL(rtsx_pci_complete_unfinished_transfer);
  715. static void rtsx_pci_card_detect(struct work_struct *work)
  716. {
  717. struct delayed_work *dwork;
  718. struct rtsx_pcr *pcr;
  719. unsigned long flags;
  720. unsigned int card_detect = 0, card_inserted, card_removed;
  721. u32 irq_status;
  722. dwork = to_delayed_work(work);
  723. pcr = container_of(dwork, struct rtsx_pcr, carddet_work);
  724. pcr_dbg(pcr, "--> %s\n", __func__);
  725. mutex_lock(&pcr->pcr_mutex);
  726. spin_lock_irqsave(&pcr->lock, flags);
  727. irq_status = rtsx_pci_readl(pcr, RTSX_BIPR);
  728. pcr_dbg(pcr, "irq_status: 0x%08x\n", irq_status);
  729. irq_status &= CARD_EXIST;
  730. card_inserted = pcr->card_inserted & irq_status;
  731. card_removed = pcr->card_removed;
  732. pcr->card_inserted = 0;
  733. pcr->card_removed = 0;
  734. spin_unlock_irqrestore(&pcr->lock, flags);
  735. if (card_inserted || card_removed) {
  736. pcr_dbg(pcr, "card_inserted: 0x%x, card_removed: 0x%x\n",
  737. card_inserted, card_removed);
  738. if (pcr->ops->cd_deglitch)
  739. card_inserted = pcr->ops->cd_deglitch(pcr);
  740. card_detect = card_inserted | card_removed;
  741. pcr->card_exist |= card_inserted;
  742. pcr->card_exist &= ~card_removed;
  743. }
  744. mutex_unlock(&pcr->pcr_mutex);
  745. if ((card_detect & SD_EXIST) && pcr->slots[RTSX_SD_CARD].card_event)
  746. pcr->slots[RTSX_SD_CARD].card_event(
  747. pcr->slots[RTSX_SD_CARD].p_dev);
  748. if ((card_detect & MS_EXIST) && pcr->slots[RTSX_MS_CARD].card_event)
  749. pcr->slots[RTSX_MS_CARD].card_event(
  750. pcr->slots[RTSX_MS_CARD].p_dev);
  751. }
  752. static void rtsx_pci_process_ocp(struct rtsx_pcr *pcr)
  753. {
  754. if (pcr->ops->process_ocp) {
  755. pcr->ops->process_ocp(pcr);
  756. } else {
  757. if (!pcr->option.ocp_en)
  758. return;
  759. rtsx_pci_get_ocpstat(pcr, &pcr->ocp_stat);
  760. if (pcr->ocp_stat & (SD_OC_NOW | SD_OC_EVER)) {
  761. rtsx_pci_card_power_off(pcr, RTSX_SD_CARD);
  762. rtsx_pci_write_register(pcr, CARD_OE, SD_OUTPUT_EN, 0);
  763. rtsx_pci_clear_ocpstat(pcr);
  764. pcr->ocp_stat = 0;
  765. }
  766. }
  767. }
  768. static int rtsx_pci_process_ocp_interrupt(struct rtsx_pcr *pcr)
  769. {
  770. if (pcr->option.ocp_en)
  771. rtsx_pci_process_ocp(pcr);
  772. return 0;
  773. }
  774. static irqreturn_t rtsx_pci_isr(int irq, void *dev_id)
  775. {
  776. struct rtsx_pcr *pcr = dev_id;
  777. u32 int_reg;
  778. if (!pcr)
  779. return IRQ_NONE;
  780. spin_lock(&pcr->lock);
  781. int_reg = rtsx_pci_readl(pcr, RTSX_BIPR);
  782. /* Clear interrupt flag */
  783. rtsx_pci_writel(pcr, RTSX_BIPR, int_reg);
  784. if ((int_reg & pcr->bier) == 0) {
  785. spin_unlock(&pcr->lock);
  786. return IRQ_NONE;
  787. }
  788. if (int_reg == 0xFFFFFFFF) {
  789. spin_unlock(&pcr->lock);
  790. return IRQ_HANDLED;
  791. }
  792. int_reg &= (pcr->bier | 0x7FFFFF);
  793. if (int_reg & SD_OC_INT)
  794. rtsx_pci_process_ocp_interrupt(pcr);
  795. if (int_reg & SD_INT) {
  796. if (int_reg & SD_EXIST) {
  797. pcr->card_inserted |= SD_EXIST;
  798. } else {
  799. pcr->card_removed |= SD_EXIST;
  800. pcr->card_inserted &= ~SD_EXIST;
  801. if (PCI_PID(pcr) == PID_5261) {
  802. rtsx_pci_write_register(pcr, RTS5261_FW_STATUS,
  803. RTS5261_EXPRESS_LINK_FAIL_MASK, 0);
  804. pcr->extra_caps |= EXTRA_CAPS_SD_EXPRESS;
  805. }
  806. }
  807. pcr->dma_error_count = 0;
  808. }
  809. if (int_reg & MS_INT) {
  810. if (int_reg & MS_EXIST) {
  811. pcr->card_inserted |= MS_EXIST;
  812. } else {
  813. pcr->card_removed |= MS_EXIST;
  814. pcr->card_inserted &= ~MS_EXIST;
  815. }
  816. }
  817. if (int_reg & (NEED_COMPLETE_INT | DELINK_INT)) {
  818. if (int_reg & (TRANS_FAIL_INT | DELINK_INT)) {
  819. pcr->trans_result = TRANS_RESULT_FAIL;
  820. if (pcr->done)
  821. complete(pcr->done);
  822. } else if (int_reg & TRANS_OK_INT) {
  823. pcr->trans_result = TRANS_RESULT_OK;
  824. if (pcr->done)
  825. complete(pcr->done);
  826. }
  827. }
  828. if ((pcr->card_inserted || pcr->card_removed) && !(int_reg & SD_OC_INT))
  829. schedule_delayed_work(&pcr->carddet_work,
  830. msecs_to_jiffies(200));
  831. spin_unlock(&pcr->lock);
  832. return IRQ_HANDLED;
  833. }
  834. static int rtsx_pci_acquire_irq(struct rtsx_pcr *pcr)
  835. {
  836. pcr_dbg(pcr, "%s: pcr->msi_en = %d, pci->irq = %d\n",
  837. __func__, pcr->msi_en, pcr->pci->irq);
  838. if (request_irq(pcr->pci->irq, rtsx_pci_isr,
  839. pcr->msi_en ? 0 : IRQF_SHARED,
  840. DRV_NAME_RTSX_PCI, pcr)) {
  841. dev_err(&(pcr->pci->dev),
  842. "rtsx_sdmmc: unable to grab IRQ %d, disabling device\n",
  843. pcr->pci->irq);
  844. return -1;
  845. }
  846. pcr->irq = pcr->pci->irq;
  847. pci_intx(pcr->pci, !pcr->msi_en);
  848. return 0;
  849. }
  850. static void rtsx_base_force_power_down(struct rtsx_pcr *pcr)
  851. {
  852. /* Set relink_time to 0 */
  853. rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 1, MASK_8_BIT_DEF, 0);
  854. rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 2, MASK_8_BIT_DEF, 0);
  855. rtsx_pci_write_register(pcr, AUTOLOAD_CFG_BASE + 3,
  856. RELINK_TIME_MASK, 0);
  857. rtsx_pci_write_register(pcr, pcr->reg_pm_ctrl3,
  858. D3_DELINK_MODE_EN, D3_DELINK_MODE_EN);
  859. rtsx_pci_write_register(pcr, FPDCTL, ALL_POWER_DOWN, ALL_POWER_DOWN);
  860. }
  861. static void __maybe_unused rtsx_pci_power_off(struct rtsx_pcr *pcr, u8 pm_state, bool runtime)
  862. {
  863. if (pcr->ops->turn_off_led)
  864. pcr->ops->turn_off_led(pcr);
  865. rtsx_pci_writel(pcr, RTSX_BIER, 0);
  866. pcr->bier = 0;
  867. rtsx_pci_write_register(pcr, PETXCFG, 0x08, 0x08);
  868. rtsx_pci_write_register(pcr, HOST_SLEEP_STATE, 0x03, pm_state);
  869. if (pcr->ops->force_power_down)
  870. pcr->ops->force_power_down(pcr, pm_state, runtime);
  871. else
  872. rtsx_base_force_power_down(pcr);
  873. }
  874. void rtsx_pci_enable_ocp(struct rtsx_pcr *pcr)
  875. {
  876. u8 val = SD_OCP_INT_EN | SD_DETECT_EN;
  877. if (pcr->ops->enable_ocp) {
  878. pcr->ops->enable_ocp(pcr);
  879. } else {
  880. rtsx_pci_write_register(pcr, FPDCTL, OC_POWER_DOWN, 0);
  881. rtsx_pci_write_register(pcr, REG_OCPCTL, 0xFF, val);
  882. }
  883. }
  884. void rtsx_pci_disable_ocp(struct rtsx_pcr *pcr)
  885. {
  886. u8 mask = SD_OCP_INT_EN | SD_DETECT_EN;
  887. if (pcr->ops->disable_ocp) {
  888. pcr->ops->disable_ocp(pcr);
  889. } else {
  890. rtsx_pci_write_register(pcr, REG_OCPCTL, mask, 0);
  891. rtsx_pci_write_register(pcr, FPDCTL, OC_POWER_DOWN,
  892. OC_POWER_DOWN);
  893. }
  894. }
  895. void rtsx_pci_init_ocp(struct rtsx_pcr *pcr)
  896. {
  897. if (pcr->ops->init_ocp) {
  898. pcr->ops->init_ocp(pcr);
  899. } else {
  900. struct rtsx_cr_option *option = &(pcr->option);
  901. if (option->ocp_en) {
  902. u8 val = option->sd_800mA_ocp_thd;
  903. rtsx_pci_write_register(pcr, FPDCTL, OC_POWER_DOWN, 0);
  904. rtsx_pci_write_register(pcr, REG_OCPPARA1,
  905. SD_OCP_TIME_MASK, SD_OCP_TIME_800);
  906. rtsx_pci_write_register(pcr, REG_OCPPARA2,
  907. SD_OCP_THD_MASK, val);
  908. rtsx_pci_write_register(pcr, REG_OCPGLITCH,
  909. SD_OCP_GLITCH_MASK, pcr->hw_param.ocp_glitch);
  910. rtsx_pci_enable_ocp(pcr);
  911. }
  912. }
  913. }
  914. int rtsx_pci_get_ocpstat(struct rtsx_pcr *pcr, u8 *val)
  915. {
  916. if (pcr->ops->get_ocpstat)
  917. return pcr->ops->get_ocpstat(pcr, val);
  918. else
  919. return rtsx_pci_read_register(pcr, REG_OCPSTAT, val);
  920. }
  921. void rtsx_pci_clear_ocpstat(struct rtsx_pcr *pcr)
  922. {
  923. if (pcr->ops->clear_ocpstat) {
  924. pcr->ops->clear_ocpstat(pcr);
  925. } else {
  926. u8 mask = SD_OCP_INT_CLR | SD_OC_CLR;
  927. u8 val = SD_OCP_INT_CLR | SD_OC_CLR;
  928. rtsx_pci_write_register(pcr, REG_OCPCTL, mask, val);
  929. udelay(100);
  930. rtsx_pci_write_register(pcr, REG_OCPCTL, mask, 0);
  931. }
  932. }
  933. void rtsx_pci_enable_oobs_polling(struct rtsx_pcr *pcr)
  934. {
  935. u16 val;
  936. if ((PCI_PID(pcr) != PID_525A) && (PCI_PID(pcr) != PID_5260)) {
  937. rtsx_pci_read_phy_register(pcr, 0x01, &val);
  938. val |= 1<<9;
  939. rtsx_pci_write_phy_register(pcr, 0x01, val);
  940. }
  941. rtsx_pci_write_register(pcr, REG_CFG_OOBS_OFF_TIMER, 0xFF, 0x32);
  942. rtsx_pci_write_register(pcr, REG_CFG_OOBS_ON_TIMER, 0xFF, 0x05);
  943. rtsx_pci_write_register(pcr, REG_CFG_VCM_ON_TIMER, 0xFF, 0x83);
  944. rtsx_pci_write_register(pcr, REG_CFG_OOBS_POLLING, 0xFF, 0xDE);
  945. }
  946. void rtsx_pci_disable_oobs_polling(struct rtsx_pcr *pcr)
  947. {
  948. u16 val;
  949. if ((PCI_PID(pcr) != PID_525A) && (PCI_PID(pcr) != PID_5260)) {
  950. rtsx_pci_read_phy_register(pcr, 0x01, &val);
  951. val &= ~(1<<9);
  952. rtsx_pci_write_phy_register(pcr, 0x01, val);
  953. }
  954. rtsx_pci_write_register(pcr, REG_CFG_VCM_ON_TIMER, 0xFF, 0x03);
  955. rtsx_pci_write_register(pcr, REG_CFG_OOBS_POLLING, 0xFF, 0x00);
  956. }
  957. int rtsx_sd_power_off_card3v3(struct rtsx_pcr *pcr)
  958. {
  959. rtsx_pci_write_register(pcr, CARD_CLK_EN, SD_CLK_EN |
  960. MS_CLK_EN | SD40_CLK_EN, 0);
  961. rtsx_pci_write_register(pcr, CARD_OE, SD_OUTPUT_EN, 0);
  962. rtsx_pci_card_power_off(pcr, RTSX_SD_CARD);
  963. msleep(50);
  964. rtsx_pci_card_pull_ctl_disable(pcr, RTSX_SD_CARD);
  965. return 0;
  966. }
  967. int rtsx_ms_power_off_card3v3(struct rtsx_pcr *pcr)
  968. {
  969. rtsx_pci_write_register(pcr, CARD_CLK_EN, SD_CLK_EN |
  970. MS_CLK_EN | SD40_CLK_EN, 0);
  971. rtsx_pci_card_pull_ctl_disable(pcr, RTSX_MS_CARD);
  972. rtsx_pci_write_register(pcr, CARD_OE, MS_OUTPUT_EN, 0);
  973. rtsx_pci_card_power_off(pcr, RTSX_MS_CARD);
  974. return 0;
  975. }
  976. static int rtsx_pci_init_hw(struct rtsx_pcr *pcr)
  977. {
  978. struct pci_dev *pdev = pcr->pci;
  979. int err;
  980. if (PCI_PID(pcr) == PID_5228)
  981. rtsx_pci_write_register(pcr, RTS5228_LDO1_CFG1, RTS5228_LDO1_SR_TIME_MASK,
  982. RTS5228_LDO1_SR_0_5);
  983. rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr);
  984. rtsx_pci_enable_bus_int(pcr);
  985. /* Power on SSC */
  986. if (PCI_PID(pcr) == PID_5261) {
  987. /* Gating real mcu clock */
  988. err = rtsx_pci_write_register(pcr, RTS5261_FW_CFG1,
  989. RTS5261_MCU_CLOCK_GATING, 0);
  990. err = rtsx_pci_write_register(pcr, RTS5261_REG_FPDCTL,
  991. SSC_POWER_DOWN, 0);
  992. } else {
  993. err = rtsx_pci_write_register(pcr, FPDCTL, SSC_POWER_DOWN, 0);
  994. }
  995. if (err < 0)
  996. return err;
  997. /* Wait SSC power stable */
  998. udelay(200);
  999. rtsx_disable_aspm(pcr);
  1000. if (pcr->ops->optimize_phy) {
  1001. err = pcr->ops->optimize_phy(pcr);
  1002. if (err < 0)
  1003. return err;
  1004. }
  1005. rtsx_pci_init_cmd(pcr);
  1006. /* Set mcu_cnt to 7 to ensure data can be sampled properly */
  1007. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_DIV, 0x07, 0x07);
  1008. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, HOST_SLEEP_STATE, 0x03, 0x00);
  1009. /* Disable card clock */
  1010. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_EN, 0x1E, 0);
  1011. /* Reset delink mode */
  1012. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CHANGE_LINK_STATE, 0x0A, 0);
  1013. /* Card driving select */
  1014. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DRIVE_SEL,
  1015. 0xFF, pcr->card_drive_sel);
  1016. /* Enable SSC Clock */
  1017. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1,
  1018. 0xFF, SSC_8X_EN | SSC_SEL_4M);
  1019. if (PCI_PID(pcr) == PID_5261)
  1020. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL2, 0xFF,
  1021. RTS5261_SSC_DEPTH_2M);
  1022. else if (PCI_PID(pcr) == PID_5228)
  1023. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL2, 0xFF,
  1024. RTS5228_SSC_DEPTH_2M);
  1025. else
  1026. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL2, 0xFF, 0x12);
  1027. /* Disable cd_pwr_save */
  1028. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CHANGE_LINK_STATE, 0x16, 0x10);
  1029. /* Clear Link Ready Interrupt */
  1030. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, IRQSTAT0,
  1031. LINK_RDY_INT, LINK_RDY_INT);
  1032. /* Enlarge the estimation window of PERST# glitch
  1033. * to reduce the chance of invalid card interrupt
  1034. */
  1035. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PERST_GLITCH_WIDTH, 0xFF, 0x80);
  1036. /* Update RC oscillator to 400k
  1037. * bit[0] F_HIGH: for RC oscillator, Rst_value is 1'b1
  1038. * 1: 2M 0: 400k
  1039. */
  1040. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, RCCTL, 0x01, 0x00);
  1041. /* Set interrupt write clear
  1042. * bit 1: U_elbi_if_rd_clr_en
  1043. * 1: Enable ELBI interrupt[31:22] & [7:0] flag read clear
  1044. * 0: ELBI interrupt flag[31:22] & [7:0] only can be write clear
  1045. */
  1046. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, NFTS_TX_CTRL, 0x02, 0);
  1047. err = rtsx_pci_send_cmd(pcr, 100);
  1048. if (err < 0)
  1049. return err;
  1050. switch (PCI_PID(pcr)) {
  1051. case PID_5250:
  1052. case PID_524A:
  1053. case PID_525A:
  1054. case PID_5260:
  1055. case PID_5261:
  1056. case PID_5228:
  1057. rtsx_pci_write_register(pcr, PM_CLK_FORCE_CTL, 1, 1);
  1058. break;
  1059. default:
  1060. break;
  1061. }
  1062. /*init ocp*/
  1063. rtsx_pci_init_ocp(pcr);
  1064. /* Enable clk_request_n to enable clock power management */
  1065. pcie_capability_clear_and_set_word(pcr->pci, PCI_EXP_LNKCTL,
  1066. 0, PCI_EXP_LNKCTL_CLKREQ_EN);
  1067. /* Enter L1 when host tx idle */
  1068. pci_write_config_byte(pdev, 0x70F, 0x5B);
  1069. if (pcr->ops->extra_init_hw) {
  1070. err = pcr->ops->extra_init_hw(pcr);
  1071. if (err < 0)
  1072. return err;
  1073. }
  1074. if (pcr->aspm_mode == ASPM_MODE_REG)
  1075. rtsx_pci_write_register(pcr, ASPM_FORCE_CTL, 0x30, 0x30);
  1076. /* No CD interrupt if probing driver with card inserted.
  1077. * So we need to initialize pcr->card_exist here.
  1078. */
  1079. if (pcr->ops->cd_deglitch)
  1080. pcr->card_exist = pcr->ops->cd_deglitch(pcr);
  1081. else
  1082. pcr->card_exist = rtsx_pci_readl(pcr, RTSX_BIPR) & CARD_EXIST;
  1083. return 0;
  1084. }
  1085. static int rtsx_pci_init_chip(struct rtsx_pcr *pcr)
  1086. {
  1087. struct rtsx_cr_option *option = &(pcr->option);
  1088. int err, l1ss;
  1089. u32 lval;
  1090. u16 cfg_val;
  1091. u8 val;
  1092. spin_lock_init(&pcr->lock);
  1093. mutex_init(&pcr->pcr_mutex);
  1094. switch (PCI_PID(pcr)) {
  1095. default:
  1096. case 0x5209:
  1097. rts5209_init_params(pcr);
  1098. break;
  1099. case 0x5229:
  1100. rts5229_init_params(pcr);
  1101. break;
  1102. case 0x5289:
  1103. rtl8411_init_params(pcr);
  1104. break;
  1105. case 0x5227:
  1106. rts5227_init_params(pcr);
  1107. break;
  1108. case 0x522A:
  1109. rts522a_init_params(pcr);
  1110. break;
  1111. case 0x5249:
  1112. rts5249_init_params(pcr);
  1113. break;
  1114. case 0x524A:
  1115. rts524a_init_params(pcr);
  1116. break;
  1117. case 0x525A:
  1118. rts525a_init_params(pcr);
  1119. break;
  1120. case 0x5287:
  1121. rtl8411b_init_params(pcr);
  1122. break;
  1123. case 0x5286:
  1124. rtl8402_init_params(pcr);
  1125. break;
  1126. case 0x5260:
  1127. rts5260_init_params(pcr);
  1128. break;
  1129. case 0x5261:
  1130. rts5261_init_params(pcr);
  1131. break;
  1132. case 0x5228:
  1133. rts5228_init_params(pcr);
  1134. break;
  1135. }
  1136. pcr_dbg(pcr, "PID: 0x%04x, IC version: 0x%02x\n",
  1137. PCI_PID(pcr), pcr->ic_version);
  1138. pcr->slots = kcalloc(pcr->num_slots, sizeof(struct rtsx_slot),
  1139. GFP_KERNEL);
  1140. if (!pcr->slots)
  1141. return -ENOMEM;
  1142. if (pcr->aspm_mode == ASPM_MODE_CFG) {
  1143. pcie_capability_read_word(pcr->pci, PCI_EXP_LNKCTL, &cfg_val);
  1144. if (cfg_val & PCI_EXP_LNKCTL_ASPM_L1)
  1145. pcr->aspm_enabled = true;
  1146. else
  1147. pcr->aspm_enabled = false;
  1148. } else if (pcr->aspm_mode == ASPM_MODE_REG) {
  1149. rtsx_pci_read_register(pcr, ASPM_FORCE_CTL, &val);
  1150. if (val & FORCE_ASPM_CTL0 && val & FORCE_ASPM_CTL1)
  1151. pcr->aspm_enabled = false;
  1152. else
  1153. pcr->aspm_enabled = true;
  1154. }
  1155. l1ss = pci_find_ext_capability(pcr->pci, PCI_EXT_CAP_ID_L1SS);
  1156. if (l1ss) {
  1157. pci_read_config_dword(pcr->pci, l1ss + PCI_L1SS_CTL1, &lval);
  1158. if (lval & PCI_L1SS_CTL1_ASPM_L1_1)
  1159. rtsx_set_dev_flag(pcr, ASPM_L1_1_EN);
  1160. else
  1161. rtsx_clear_dev_flag(pcr, ASPM_L1_1_EN);
  1162. if (lval & PCI_L1SS_CTL1_ASPM_L1_2)
  1163. rtsx_set_dev_flag(pcr, ASPM_L1_2_EN);
  1164. else
  1165. rtsx_clear_dev_flag(pcr, ASPM_L1_2_EN);
  1166. if (lval & PCI_L1SS_CTL1_PCIPM_L1_1)
  1167. rtsx_set_dev_flag(pcr, PM_L1_1_EN);
  1168. else
  1169. rtsx_clear_dev_flag(pcr, PM_L1_1_EN);
  1170. if (lval & PCI_L1SS_CTL1_PCIPM_L1_2)
  1171. rtsx_set_dev_flag(pcr, PM_L1_2_EN);
  1172. else
  1173. rtsx_clear_dev_flag(pcr, PM_L1_2_EN);
  1174. pcie_capability_read_word(pcr->pci, PCI_EXP_DEVCTL2, &cfg_val);
  1175. if (cfg_val & PCI_EXP_DEVCTL2_LTR_EN) {
  1176. option->ltr_enabled = true;
  1177. option->ltr_active = true;
  1178. } else {
  1179. option->ltr_enabled = false;
  1180. }
  1181. if (rtsx_check_dev_flag(pcr, ASPM_L1_1_EN | ASPM_L1_2_EN
  1182. | PM_L1_1_EN | PM_L1_2_EN))
  1183. option->force_clkreq_0 = false;
  1184. else
  1185. option->force_clkreq_0 = true;
  1186. } else {
  1187. option->ltr_enabled = false;
  1188. option->force_clkreq_0 = true;
  1189. }
  1190. if (pcr->ops->fetch_vendor_settings)
  1191. pcr->ops->fetch_vendor_settings(pcr);
  1192. pcr_dbg(pcr, "pcr->aspm_en = 0x%x\n", pcr->aspm_en);
  1193. pcr_dbg(pcr, "pcr->sd30_drive_sel_1v8 = 0x%x\n",
  1194. pcr->sd30_drive_sel_1v8);
  1195. pcr_dbg(pcr, "pcr->sd30_drive_sel_3v3 = 0x%x\n",
  1196. pcr->sd30_drive_sel_3v3);
  1197. pcr_dbg(pcr, "pcr->card_drive_sel = 0x%x\n",
  1198. pcr->card_drive_sel);
  1199. pcr_dbg(pcr, "pcr->flags = 0x%x\n", pcr->flags);
  1200. pcr->state = PDEV_STAT_IDLE;
  1201. err = rtsx_pci_init_hw(pcr);
  1202. if (err < 0) {
  1203. kfree(pcr->slots);
  1204. return err;
  1205. }
  1206. return 0;
  1207. }
  1208. static int rtsx_pci_probe(struct pci_dev *pcidev,
  1209. const struct pci_device_id *id)
  1210. {
  1211. struct rtsx_pcr *pcr;
  1212. struct pcr_handle *handle;
  1213. u32 base, len;
  1214. int ret, i, bar = 0;
  1215. dev_dbg(&(pcidev->dev),
  1216. ": Realtek PCI-E Card Reader found at %s [%04x:%04x] (rev %x)\n",
  1217. pci_name(pcidev), (int)pcidev->vendor, (int)pcidev->device,
  1218. (int)pcidev->revision);
  1219. ret = dma_set_mask(&pcidev->dev, DMA_BIT_MASK(32));
  1220. if (ret < 0)
  1221. return ret;
  1222. ret = pci_enable_device(pcidev);
  1223. if (ret)
  1224. return ret;
  1225. ret = pci_request_regions(pcidev, DRV_NAME_RTSX_PCI);
  1226. if (ret)
  1227. goto disable;
  1228. pcr = kzalloc(sizeof(*pcr), GFP_KERNEL);
  1229. if (!pcr) {
  1230. ret = -ENOMEM;
  1231. goto release_pci;
  1232. }
  1233. handle = kzalloc(sizeof(*handle), GFP_KERNEL);
  1234. if (!handle) {
  1235. ret = -ENOMEM;
  1236. goto free_pcr;
  1237. }
  1238. handle->pcr = pcr;
  1239. idr_preload(GFP_KERNEL);
  1240. spin_lock(&rtsx_pci_lock);
  1241. ret = idr_alloc(&rtsx_pci_idr, pcr, 0, 0, GFP_NOWAIT);
  1242. if (ret >= 0)
  1243. pcr->id = ret;
  1244. spin_unlock(&rtsx_pci_lock);
  1245. idr_preload_end();
  1246. if (ret < 0)
  1247. goto free_handle;
  1248. pcr->pci = pcidev;
  1249. dev_set_drvdata(&pcidev->dev, handle);
  1250. if (CHK_PCI_PID(pcr, 0x525A))
  1251. bar = 1;
  1252. len = pci_resource_len(pcidev, bar);
  1253. base = pci_resource_start(pcidev, bar);
  1254. pcr->remap_addr = ioremap(base, len);
  1255. if (!pcr->remap_addr) {
  1256. ret = -ENOMEM;
  1257. goto free_idr;
  1258. }
  1259. pcr->rtsx_resv_buf = dma_alloc_coherent(&(pcidev->dev),
  1260. RTSX_RESV_BUF_LEN, &(pcr->rtsx_resv_buf_addr),
  1261. GFP_KERNEL);
  1262. if (pcr->rtsx_resv_buf == NULL) {
  1263. ret = -ENXIO;
  1264. goto unmap;
  1265. }
  1266. pcr->host_cmds_ptr = pcr->rtsx_resv_buf;
  1267. pcr->host_cmds_addr = pcr->rtsx_resv_buf_addr;
  1268. pcr->host_sg_tbl_ptr = pcr->rtsx_resv_buf + HOST_CMDS_BUF_LEN;
  1269. pcr->host_sg_tbl_addr = pcr->rtsx_resv_buf_addr + HOST_CMDS_BUF_LEN;
  1270. pcr->card_inserted = 0;
  1271. pcr->card_removed = 0;
  1272. INIT_DELAYED_WORK(&pcr->carddet_work, rtsx_pci_card_detect);
  1273. pcr->msi_en = msi_en;
  1274. if (pcr->msi_en) {
  1275. ret = pci_enable_msi(pcidev);
  1276. if (ret)
  1277. pcr->msi_en = false;
  1278. }
  1279. ret = rtsx_pci_acquire_irq(pcr);
  1280. if (ret < 0)
  1281. goto disable_msi;
  1282. pci_set_master(pcidev);
  1283. synchronize_irq(pcr->irq);
  1284. ret = rtsx_pci_init_chip(pcr);
  1285. if (ret < 0)
  1286. goto disable_irq;
  1287. for (i = 0; i < ARRAY_SIZE(rtsx_pcr_cells); i++) {
  1288. rtsx_pcr_cells[i].platform_data = handle;
  1289. rtsx_pcr_cells[i].pdata_size = sizeof(*handle);
  1290. }
  1291. ret = mfd_add_devices(&pcidev->dev, pcr->id, rtsx_pcr_cells,
  1292. ARRAY_SIZE(rtsx_pcr_cells), NULL, 0, NULL);
  1293. if (ret < 0)
  1294. goto free_slots;
  1295. pm_runtime_allow(&pcidev->dev);
  1296. pm_runtime_put(&pcidev->dev);
  1297. return 0;
  1298. free_slots:
  1299. kfree(pcr->slots);
  1300. disable_irq:
  1301. free_irq(pcr->irq, (void *)pcr);
  1302. disable_msi:
  1303. if (pcr->msi_en)
  1304. pci_disable_msi(pcr->pci);
  1305. dma_free_coherent(&(pcr->pci->dev), RTSX_RESV_BUF_LEN,
  1306. pcr->rtsx_resv_buf, pcr->rtsx_resv_buf_addr);
  1307. unmap:
  1308. iounmap(pcr->remap_addr);
  1309. free_idr:
  1310. spin_lock(&rtsx_pci_lock);
  1311. idr_remove(&rtsx_pci_idr, pcr->id);
  1312. spin_unlock(&rtsx_pci_lock);
  1313. free_handle:
  1314. kfree(handle);
  1315. free_pcr:
  1316. kfree(pcr);
  1317. release_pci:
  1318. pci_release_regions(pcidev);
  1319. disable:
  1320. pci_disable_device(pcidev);
  1321. return ret;
  1322. }
  1323. static void rtsx_pci_remove(struct pci_dev *pcidev)
  1324. {
  1325. struct pcr_handle *handle = pci_get_drvdata(pcidev);
  1326. struct rtsx_pcr *pcr = handle->pcr;
  1327. pcr->remove_pci = true;
  1328. pm_runtime_get_sync(&pcidev->dev);
  1329. pm_runtime_forbid(&pcidev->dev);
  1330. /* Disable interrupts at the pcr level */
  1331. spin_lock_irq(&pcr->lock);
  1332. rtsx_pci_writel(pcr, RTSX_BIER, 0);
  1333. pcr->bier = 0;
  1334. spin_unlock_irq(&pcr->lock);
  1335. cancel_delayed_work_sync(&pcr->carddet_work);
  1336. mfd_remove_devices(&pcidev->dev);
  1337. dma_free_coherent(&(pcr->pci->dev), RTSX_RESV_BUF_LEN,
  1338. pcr->rtsx_resv_buf, pcr->rtsx_resv_buf_addr);
  1339. free_irq(pcr->irq, (void *)pcr);
  1340. if (pcr->msi_en)
  1341. pci_disable_msi(pcr->pci);
  1342. iounmap(pcr->remap_addr);
  1343. pci_release_regions(pcidev);
  1344. pci_disable_device(pcidev);
  1345. spin_lock(&rtsx_pci_lock);
  1346. idr_remove(&rtsx_pci_idr, pcr->id);
  1347. spin_unlock(&rtsx_pci_lock);
  1348. kfree(pcr->slots);
  1349. kfree(pcr);
  1350. kfree(handle);
  1351. dev_dbg(&(pcidev->dev),
  1352. ": Realtek PCI-E Card Reader at %s [%04x:%04x] has been removed\n",
  1353. pci_name(pcidev), (int)pcidev->vendor, (int)pcidev->device);
  1354. }
  1355. static int __maybe_unused rtsx_pci_suspend(struct device *dev_d)
  1356. {
  1357. struct pci_dev *pcidev = to_pci_dev(dev_d);
  1358. struct pcr_handle *handle = pci_get_drvdata(pcidev);
  1359. struct rtsx_pcr *pcr = handle->pcr;
  1360. dev_dbg(&(pcidev->dev), "--> %s\n", __func__);
  1361. cancel_delayed_work_sync(&pcr->carddet_work);
  1362. mutex_lock(&pcr->pcr_mutex);
  1363. rtsx_pci_power_off(pcr, HOST_ENTER_S3, false);
  1364. mutex_unlock(&pcr->pcr_mutex);
  1365. return 0;
  1366. }
  1367. static int __maybe_unused rtsx_pci_resume(struct device *dev_d)
  1368. {
  1369. struct pci_dev *pcidev = to_pci_dev(dev_d);
  1370. struct pcr_handle *handle = pci_get_drvdata(pcidev);
  1371. struct rtsx_pcr *pcr = handle->pcr;
  1372. int ret = 0;
  1373. dev_dbg(&(pcidev->dev), "--> %s\n", __func__);
  1374. mutex_lock(&pcr->pcr_mutex);
  1375. ret = rtsx_pci_write_register(pcr, HOST_SLEEP_STATE, 0x03, 0x00);
  1376. if (ret)
  1377. goto out;
  1378. ret = rtsx_pci_init_hw(pcr);
  1379. if (ret)
  1380. goto out;
  1381. out:
  1382. mutex_unlock(&pcr->pcr_mutex);
  1383. return ret;
  1384. }
  1385. #ifdef CONFIG_PM
  1386. static void rtsx_enable_aspm(struct rtsx_pcr *pcr)
  1387. {
  1388. if (pcr->ops->set_aspm)
  1389. pcr->ops->set_aspm(pcr, true);
  1390. else
  1391. rtsx_comm_set_aspm(pcr, true);
  1392. }
  1393. static void rtsx_comm_pm_power_saving(struct rtsx_pcr *pcr)
  1394. {
  1395. struct rtsx_cr_option *option = &pcr->option;
  1396. if (option->ltr_enabled) {
  1397. u32 latency = option->ltr_l1off_latency;
  1398. if (rtsx_check_dev_flag(pcr, L1_SNOOZE_TEST_EN))
  1399. mdelay(option->l1_snooze_delay);
  1400. rtsx_set_ltr_latency(pcr, latency);
  1401. }
  1402. if (rtsx_check_dev_flag(pcr, LTR_L1SS_PWR_GATE_EN))
  1403. rtsx_set_l1off_sub_cfg_d0(pcr, 0);
  1404. rtsx_enable_aspm(pcr);
  1405. }
  1406. static void rtsx_pm_power_saving(struct rtsx_pcr *pcr)
  1407. {
  1408. rtsx_comm_pm_power_saving(pcr);
  1409. }
  1410. static void rtsx_pci_shutdown(struct pci_dev *pcidev)
  1411. {
  1412. struct pcr_handle *handle = pci_get_drvdata(pcidev);
  1413. struct rtsx_pcr *pcr = handle->pcr;
  1414. dev_dbg(&(pcidev->dev), "--> %s\n", __func__);
  1415. rtsx_pci_power_off(pcr, HOST_ENTER_S1, false);
  1416. pci_disable_device(pcidev);
  1417. free_irq(pcr->irq, (void *)pcr);
  1418. if (pcr->msi_en)
  1419. pci_disable_msi(pcr->pci);
  1420. }
  1421. static int rtsx_pci_runtime_idle(struct device *device)
  1422. {
  1423. struct pci_dev *pcidev = to_pci_dev(device);
  1424. struct pcr_handle *handle = pci_get_drvdata(pcidev);
  1425. struct rtsx_pcr *pcr = handle->pcr;
  1426. dev_dbg(device, "--> %s\n", __func__);
  1427. mutex_lock(&pcr->pcr_mutex);
  1428. pcr->state = PDEV_STAT_IDLE;
  1429. if (pcr->ops->disable_auto_blink)
  1430. pcr->ops->disable_auto_blink(pcr);
  1431. if (pcr->ops->turn_off_led)
  1432. pcr->ops->turn_off_led(pcr);
  1433. rtsx_pm_power_saving(pcr);
  1434. mutex_unlock(&pcr->pcr_mutex);
  1435. if (pcr->rtd3_en)
  1436. pm_schedule_suspend(device, 10000);
  1437. return -EBUSY;
  1438. }
  1439. static int rtsx_pci_runtime_suspend(struct device *device)
  1440. {
  1441. struct pci_dev *pcidev = to_pci_dev(device);
  1442. struct pcr_handle *handle = pci_get_drvdata(pcidev);
  1443. struct rtsx_pcr *pcr = handle->pcr;
  1444. dev_dbg(device, "--> %s\n", __func__);
  1445. cancel_delayed_work_sync(&pcr->carddet_work);
  1446. mutex_lock(&pcr->pcr_mutex);
  1447. rtsx_pci_power_off(pcr, HOST_ENTER_S3, true);
  1448. mutex_unlock(&pcr->pcr_mutex);
  1449. return 0;
  1450. }
  1451. static int rtsx_pci_runtime_resume(struct device *device)
  1452. {
  1453. struct pci_dev *pcidev = to_pci_dev(device);
  1454. struct pcr_handle *handle = pci_get_drvdata(pcidev);
  1455. struct rtsx_pcr *pcr = handle->pcr;
  1456. dev_dbg(device, "--> %s\n", __func__);
  1457. mutex_lock(&pcr->pcr_mutex);
  1458. rtsx_pci_write_register(pcr, HOST_SLEEP_STATE, 0x03, 0x00);
  1459. rtsx_pci_init_hw(pcr);
  1460. if (pcr->slots[RTSX_SD_CARD].p_dev != NULL) {
  1461. pcr->slots[RTSX_SD_CARD].card_event(
  1462. pcr->slots[RTSX_SD_CARD].p_dev);
  1463. }
  1464. mutex_unlock(&pcr->pcr_mutex);
  1465. return 0;
  1466. }
  1467. #else /* CONFIG_PM */
  1468. #define rtsx_pci_shutdown NULL
  1469. #define rtsx_pci_runtime_suspend NULL
  1470. #define rtsx_pic_runtime_resume NULL
  1471. #endif /* CONFIG_PM */
  1472. static const struct dev_pm_ops rtsx_pci_pm_ops = {
  1473. SET_SYSTEM_SLEEP_PM_OPS(rtsx_pci_suspend, rtsx_pci_resume)
  1474. SET_RUNTIME_PM_OPS(rtsx_pci_runtime_suspend, rtsx_pci_runtime_resume, rtsx_pci_runtime_idle)
  1475. };
  1476. static struct pci_driver rtsx_pci_driver = {
  1477. .name = DRV_NAME_RTSX_PCI,
  1478. .id_table = rtsx_pci_ids,
  1479. .probe = rtsx_pci_probe,
  1480. .remove = rtsx_pci_remove,
  1481. .driver.pm = &rtsx_pci_pm_ops,
  1482. .shutdown = rtsx_pci_shutdown,
  1483. };
  1484. module_pci_driver(rtsx_pci_driver);
  1485. MODULE_LICENSE("GPL");
  1486. MODULE_AUTHOR("Wei WANG <[email protected]>");
  1487. MODULE_DESCRIPTION("Realtek PCI-E Card Reader Driver");