rtsx_pcr.c 39 KB

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