rtsx_pcr.c 43 KB

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