sdhci-pci-core.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* linux/drivers/mmc/host/sdhci-pci.c - SDHCI on PCI bus interface
  3. *
  4. * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
  5. *
  6. * Thanks to the following companies for their support:
  7. *
  8. * - JMicron (hardware and technical support)
  9. */
  10. #include <linux/bitfield.h>
  11. #include <linux/string.h>
  12. #include <linux/delay.h>
  13. #include <linux/highmem.h>
  14. #include <linux/module.h>
  15. #include <linux/pci.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/slab.h>
  18. #include <linux/device.h>
  19. #include <linux/scatterlist.h>
  20. #include <linux/io.h>
  21. #include <linux/iopoll.h>
  22. #include <linux/gpio.h>
  23. #include <linux/gpio/machine.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/pm_qos.h>
  26. #include <linux/debugfs.h>
  27. #include <linux/acpi.h>
  28. #include <linux/dmi.h>
  29. #include <linux/mmc/host.h>
  30. #include <linux/mmc/mmc.h>
  31. #include <linux/mmc/slot-gpio.h>
  32. #ifdef CONFIG_X86
  33. #include <asm/iosf_mbi.h>
  34. #endif
  35. #include "cqhci.h"
  36. #include "sdhci.h"
  37. #include "sdhci-cqhci.h"
  38. #include "sdhci-pci.h"
  39. #include "sdhci-uhs2.h"
  40. static void sdhci_pci_hw_reset(struct sdhci_host *host);
  41. #ifdef CONFIG_PM_SLEEP
  42. static int sdhci_pci_init_wakeup(struct sdhci_pci_chip *chip)
  43. {
  44. mmc_pm_flag_t pm_flags = 0;
  45. bool cap_cd_wake = false;
  46. int i;
  47. for (i = 0; i < chip->num_slots; i++) {
  48. struct sdhci_pci_slot *slot = chip->slots[i];
  49. if (slot) {
  50. pm_flags |= slot->host->mmc->pm_flags;
  51. if (slot->host->mmc->caps & MMC_CAP_CD_WAKE)
  52. cap_cd_wake = true;
  53. }
  54. }
  55. if ((pm_flags & MMC_PM_KEEP_POWER) && (pm_flags & MMC_PM_WAKE_SDIO_IRQ))
  56. return device_wakeup_enable(&chip->pdev->dev);
  57. else if (!cap_cd_wake)
  58. device_wakeup_disable(&chip->pdev->dev);
  59. return 0;
  60. }
  61. static int sdhci_pci_suspend_host(struct sdhci_pci_chip *chip)
  62. {
  63. int i, ret;
  64. sdhci_pci_init_wakeup(chip);
  65. for (i = 0; i < chip->num_slots; i++) {
  66. struct sdhci_pci_slot *slot = chip->slots[i];
  67. struct sdhci_host *host;
  68. if (!slot)
  69. continue;
  70. host = slot->host;
  71. if (chip->pm_retune && host->tuning_mode != SDHCI_TUNING_MODE_3)
  72. mmc_retune_needed(host->mmc);
  73. ret = sdhci_suspend_host(host);
  74. if (ret)
  75. goto err_pci_suspend;
  76. if (device_may_wakeup(&chip->pdev->dev))
  77. mmc_gpio_set_cd_wake(host->mmc, true);
  78. }
  79. return 0;
  80. err_pci_suspend:
  81. while (--i >= 0)
  82. sdhci_resume_host(chip->slots[i]->host);
  83. return ret;
  84. }
  85. int sdhci_pci_resume_host(struct sdhci_pci_chip *chip)
  86. {
  87. struct sdhci_pci_slot *slot;
  88. int i, ret;
  89. for (i = 0; i < chip->num_slots; i++) {
  90. slot = chip->slots[i];
  91. if (!slot)
  92. continue;
  93. ret = sdhci_resume_host(slot->host);
  94. if (ret)
  95. return ret;
  96. mmc_gpio_set_cd_wake(slot->host->mmc, false);
  97. }
  98. return 0;
  99. }
  100. static int sdhci_cqhci_suspend(struct sdhci_pci_chip *chip)
  101. {
  102. int ret;
  103. ret = cqhci_suspend(chip->slots[0]->host->mmc);
  104. if (ret)
  105. return ret;
  106. return sdhci_pci_suspend_host(chip);
  107. }
  108. static int sdhci_cqhci_resume(struct sdhci_pci_chip *chip)
  109. {
  110. int ret;
  111. ret = sdhci_pci_resume_host(chip);
  112. if (ret)
  113. return ret;
  114. return cqhci_resume(chip->slots[0]->host->mmc);
  115. }
  116. #endif
  117. #ifdef CONFIG_PM
  118. static int sdhci_pci_runtime_suspend_host(struct sdhci_pci_chip *chip)
  119. {
  120. struct sdhci_pci_slot *slot;
  121. struct sdhci_host *host;
  122. for (int i = 0; i < chip->num_slots; i++) {
  123. slot = chip->slots[i];
  124. if (!slot)
  125. continue;
  126. host = slot->host;
  127. sdhci_runtime_suspend_host(host);
  128. if (chip->rpm_retune &&
  129. host->tuning_mode != SDHCI_TUNING_MODE_3)
  130. mmc_retune_needed(host->mmc);
  131. }
  132. return 0;
  133. }
  134. static int sdhci_pci_runtime_resume_host(struct sdhci_pci_chip *chip)
  135. {
  136. struct sdhci_pci_slot *slot;
  137. for (int i = 0; i < chip->num_slots; i++) {
  138. slot = chip->slots[i];
  139. if (!slot)
  140. continue;
  141. sdhci_runtime_resume_host(slot->host, 0);
  142. }
  143. return 0;
  144. }
  145. static int sdhci_cqhci_runtime_suspend(struct sdhci_pci_chip *chip)
  146. {
  147. int ret;
  148. ret = cqhci_suspend(chip->slots[0]->host->mmc);
  149. if (ret)
  150. return ret;
  151. return sdhci_pci_runtime_suspend_host(chip);
  152. }
  153. static int sdhci_cqhci_runtime_resume(struct sdhci_pci_chip *chip)
  154. {
  155. int ret;
  156. ret = sdhci_pci_runtime_resume_host(chip);
  157. if (ret)
  158. return ret;
  159. return cqhci_resume(chip->slots[0]->host->mmc);
  160. }
  161. #endif
  162. static u32 sdhci_cqhci_irq(struct sdhci_host *host, u32 intmask)
  163. {
  164. int cmd_error = 0;
  165. int data_error = 0;
  166. if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
  167. return intmask;
  168. cqhci_irq(host->mmc, intmask, cmd_error, data_error);
  169. return 0;
  170. }
  171. static void sdhci_pci_dumpregs(struct mmc_host *mmc)
  172. {
  173. sdhci_dumpregs(mmc_priv(mmc));
  174. }
  175. /*****************************************************************************\
  176. * *
  177. * Hardware specific quirk handling *
  178. * *
  179. \*****************************************************************************/
  180. static int ricoh_probe(struct sdhci_pci_chip *chip)
  181. {
  182. if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
  183. chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
  184. chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
  185. return 0;
  186. }
  187. static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
  188. {
  189. u32 caps =
  190. FIELD_PREP(SDHCI_TIMEOUT_CLK_MASK, 0x21) |
  191. FIELD_PREP(SDHCI_CLOCK_BASE_MASK, 0x21) |
  192. SDHCI_TIMEOUT_CLK_UNIT |
  193. SDHCI_CAN_VDD_330 |
  194. SDHCI_CAN_DO_HISPD |
  195. SDHCI_CAN_DO_SDMA;
  196. u32 caps1 = 0;
  197. __sdhci_read_caps(slot->host, NULL, &caps, &caps1);
  198. return 0;
  199. }
  200. #ifdef CONFIG_PM_SLEEP
  201. static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
  202. {
  203. /* Apply a delay to allow controller to settle */
  204. /* Otherwise it becomes confused if card state changed
  205. during suspend */
  206. msleep(500);
  207. return sdhci_pci_resume_host(chip);
  208. }
  209. #endif
  210. static const struct sdhci_pci_fixes sdhci_ricoh = {
  211. .probe = ricoh_probe,
  212. .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
  213. SDHCI_QUIRK_FORCE_DMA |
  214. SDHCI_QUIRK_CLOCK_BEFORE_RESET,
  215. };
  216. static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
  217. .probe_slot = ricoh_mmc_probe_slot,
  218. #ifdef CONFIG_PM_SLEEP
  219. .resume = ricoh_mmc_resume,
  220. #endif
  221. .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
  222. SDHCI_QUIRK_CLOCK_BEFORE_RESET |
  223. SDHCI_QUIRK_NO_CARD_NO_RESET,
  224. };
  225. static void ene_714_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  226. {
  227. struct sdhci_host *host = mmc_priv(mmc);
  228. sdhci_set_ios(mmc, ios);
  229. /*
  230. * Some (ENE) controllers misbehave on some ios operations,
  231. * signalling timeout and CRC errors even on CMD0. Resetting
  232. * it on each ios seems to solve the problem.
  233. */
  234. if (!(host->flags & SDHCI_DEVICE_DEAD))
  235. sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  236. }
  237. static int ene_714_probe_slot(struct sdhci_pci_slot *slot)
  238. {
  239. slot->host->mmc_host_ops.set_ios = ene_714_set_ios;
  240. return 0;
  241. }
  242. static const struct sdhci_pci_fixes sdhci_ene_712 = {
  243. .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
  244. SDHCI_QUIRK_BROKEN_DMA,
  245. };
  246. static const struct sdhci_pci_fixes sdhci_ene_714 = {
  247. .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
  248. SDHCI_QUIRK_BROKEN_DMA,
  249. .probe_slot = ene_714_probe_slot,
  250. };
  251. static const struct sdhci_pci_fixes sdhci_cafe = {
  252. .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
  253. SDHCI_QUIRK_NO_BUSY_IRQ |
  254. SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  255. SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
  256. };
  257. static const struct sdhci_pci_fixes sdhci_intel_qrk = {
  258. .quirks = SDHCI_QUIRK_NO_HISPD_BIT,
  259. };
  260. static int mrst_hc_probe_slot(struct sdhci_pci_slot *slot)
  261. {
  262. slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  263. return 0;
  264. }
  265. /*
  266. * ADMA operation is disabled for Moorestown platform due to
  267. * hardware bugs.
  268. */
  269. static int mrst_hc_probe(struct sdhci_pci_chip *chip)
  270. {
  271. /*
  272. * slots number is fixed here for MRST as SDIO3/5 are never used and
  273. * have hardware bugs.
  274. */
  275. chip->num_slots = 1;
  276. return 0;
  277. }
  278. static int pch_hc_probe_slot(struct sdhci_pci_slot *slot)
  279. {
  280. slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  281. return 0;
  282. }
  283. static int mfd_emmc_probe_slot(struct sdhci_pci_slot *slot)
  284. {
  285. slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE;
  286. slot->host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC;
  287. return 0;
  288. }
  289. static int mfd_sdio_probe_slot(struct sdhci_pci_slot *slot)
  290. {
  291. slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE;
  292. return 0;
  293. }
  294. static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
  295. .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
  296. .probe_slot = mrst_hc_probe_slot,
  297. };
  298. static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1_hc2 = {
  299. .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
  300. .probe = mrst_hc_probe,
  301. };
  302. static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
  303. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  304. .allow_runtime_pm = true,
  305. .own_cd_for_runtime_pm = true,
  306. };
  307. static const struct sdhci_pci_fixes sdhci_intel_mfd_sdio = {
  308. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  309. .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
  310. .allow_runtime_pm = true,
  311. .probe_slot = mfd_sdio_probe_slot,
  312. };
  313. static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc = {
  314. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  315. .allow_runtime_pm = true,
  316. .probe_slot = mfd_emmc_probe_slot,
  317. };
  318. static const struct sdhci_pci_fixes sdhci_intel_pch_sdio = {
  319. .quirks = SDHCI_QUIRK_BROKEN_ADMA,
  320. .probe_slot = pch_hc_probe_slot,
  321. };
  322. #ifdef CONFIG_X86
  323. #define BYT_IOSF_SCCEP 0x63
  324. #define BYT_IOSF_OCP_NETCTRL0 0x1078
  325. #define BYT_IOSF_OCP_TIMEOUT_BASE GENMASK(10, 8)
  326. static void byt_ocp_setting(struct pci_dev *pdev)
  327. {
  328. u32 val = 0;
  329. if (pdev->device != PCI_DEVICE_ID_INTEL_BYT_EMMC &&
  330. pdev->device != PCI_DEVICE_ID_INTEL_BYT_SDIO &&
  331. pdev->device != PCI_DEVICE_ID_INTEL_BYT_SD &&
  332. pdev->device != PCI_DEVICE_ID_INTEL_BYT_EMMC2)
  333. return;
  334. if (iosf_mbi_read(BYT_IOSF_SCCEP, MBI_CR_READ, BYT_IOSF_OCP_NETCTRL0,
  335. &val)) {
  336. dev_err(&pdev->dev, "%s read error\n", __func__);
  337. return;
  338. }
  339. if (!(val & BYT_IOSF_OCP_TIMEOUT_BASE))
  340. return;
  341. val &= ~BYT_IOSF_OCP_TIMEOUT_BASE;
  342. if (iosf_mbi_write(BYT_IOSF_SCCEP, MBI_CR_WRITE, BYT_IOSF_OCP_NETCTRL0,
  343. val)) {
  344. dev_err(&pdev->dev, "%s write error\n", __func__);
  345. return;
  346. }
  347. dev_dbg(&pdev->dev, "%s completed\n", __func__);
  348. }
  349. #else
  350. static inline void byt_ocp_setting(struct pci_dev *pdev)
  351. {
  352. }
  353. #endif
  354. enum {
  355. INTEL_DSM_FNS = 0,
  356. INTEL_DSM_V18_SWITCH = 3,
  357. INTEL_DSM_V33_SWITCH = 4,
  358. INTEL_DSM_DRV_STRENGTH = 9,
  359. INTEL_DSM_D3_RETUNE = 10,
  360. };
  361. struct intel_host {
  362. u32 dsm_fns;
  363. int drv_strength;
  364. bool d3_retune;
  365. bool rpm_retune_ok;
  366. bool needs_pwr_off;
  367. u32 glk_rx_ctrl1;
  368. u32 glk_tun_val;
  369. u32 active_ltr;
  370. u32 idle_ltr;
  371. };
  372. static const guid_t intel_dsm_guid =
  373. GUID_INIT(0xF6C13EA5, 0x65CD, 0x461F,
  374. 0xAB, 0x7A, 0x29, 0xF7, 0xE8, 0xD5, 0xBD, 0x61);
  375. static int __intel_dsm(struct intel_host *intel_host, struct device *dev,
  376. unsigned int fn, u32 *result)
  377. {
  378. union acpi_object *obj;
  379. int err = 0;
  380. size_t len;
  381. obj = acpi_evaluate_dsm_typed(ACPI_HANDLE(dev), &intel_dsm_guid, 0, fn, NULL,
  382. ACPI_TYPE_BUFFER);
  383. if (!obj)
  384. return -EOPNOTSUPP;
  385. if (obj->buffer.length < 1) {
  386. err = -EINVAL;
  387. goto out;
  388. }
  389. len = min_t(size_t, obj->buffer.length, 4);
  390. *result = 0;
  391. memcpy(result, obj->buffer.pointer, len);
  392. out:
  393. ACPI_FREE(obj);
  394. return err;
  395. }
  396. static int intel_dsm(struct intel_host *intel_host, struct device *dev,
  397. unsigned int fn, u32 *result)
  398. {
  399. if (fn > 31 || !(intel_host->dsm_fns & (1 << fn)))
  400. return -EOPNOTSUPP;
  401. return __intel_dsm(intel_host, dev, fn, result);
  402. }
  403. static void intel_dsm_init(struct intel_host *intel_host, struct device *dev,
  404. struct mmc_host *mmc)
  405. {
  406. int err;
  407. u32 val;
  408. intel_host->d3_retune = true;
  409. err = __intel_dsm(intel_host, dev, INTEL_DSM_FNS, &intel_host->dsm_fns);
  410. if (err) {
  411. pr_debug("%s: DSM not supported, error %d\n",
  412. mmc_hostname(mmc), err);
  413. return;
  414. }
  415. pr_debug("%s: DSM function mask %#x\n",
  416. mmc_hostname(mmc), intel_host->dsm_fns);
  417. err = intel_dsm(intel_host, dev, INTEL_DSM_DRV_STRENGTH, &val);
  418. intel_host->drv_strength = err ? 0 : val;
  419. err = intel_dsm(intel_host, dev, INTEL_DSM_D3_RETUNE, &val);
  420. intel_host->d3_retune = err ? true : !!val;
  421. }
  422. static void sdhci_pci_int_hw_reset(struct sdhci_host *host)
  423. {
  424. u8 reg;
  425. reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
  426. reg |= 0x10;
  427. sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
  428. /* For eMMC, minimum is 1us but give it 9us for good measure */
  429. udelay(9);
  430. reg &= ~0x10;
  431. sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
  432. /* For eMMC, minimum is 200us but give it 300us for good measure */
  433. usleep_range(300, 1000);
  434. }
  435. static int intel_select_drive_strength(struct mmc_card *card,
  436. unsigned int max_dtr, int host_drv,
  437. int card_drv, int *drv_type)
  438. {
  439. struct sdhci_host *host = mmc_priv(card->host);
  440. struct sdhci_pci_slot *slot = sdhci_priv(host);
  441. struct intel_host *intel_host = sdhci_pci_priv(slot);
  442. if (!(mmc_driver_type_mask(intel_host->drv_strength) & card_drv))
  443. return 0;
  444. return intel_host->drv_strength;
  445. }
  446. static int bxt_get_cd(struct mmc_host *mmc)
  447. {
  448. int gpio_cd = mmc_gpio_get_cd(mmc);
  449. if (!gpio_cd)
  450. return 0;
  451. return sdhci_get_cd_nogpio(mmc);
  452. }
  453. static int mrfld_get_cd(struct mmc_host *mmc)
  454. {
  455. return sdhci_get_cd_nogpio(mmc);
  456. }
  457. #define SDHCI_INTEL_PWR_TIMEOUT_CNT 20
  458. #define SDHCI_INTEL_PWR_TIMEOUT_UDELAY 100
  459. static void sdhci_intel_set_power(struct sdhci_host *host, unsigned char mode,
  460. unsigned short vdd)
  461. {
  462. struct sdhci_pci_slot *slot = sdhci_priv(host);
  463. struct intel_host *intel_host = sdhci_pci_priv(slot);
  464. int cntr;
  465. u8 reg;
  466. /*
  467. * Bus power may control card power, but a full reset still may not
  468. * reset the power, whereas a direct write to SDHCI_POWER_CONTROL can.
  469. * That might be needed to initialize correctly, if the card was left
  470. * powered on previously.
  471. */
  472. if (intel_host->needs_pwr_off) {
  473. intel_host->needs_pwr_off = false;
  474. if (mode != MMC_POWER_OFF) {
  475. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  476. usleep_range(10000, 12500);
  477. }
  478. }
  479. sdhci_set_power(host, mode, vdd);
  480. if (mode == MMC_POWER_OFF) {
  481. if (slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_APL_SD ||
  482. slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_BYT_SD)
  483. usleep_range(15000, 17500);
  484. return;
  485. }
  486. /*
  487. * Bus power might not enable after D3 -> D0 transition due to the
  488. * present state not yet having propagated. Retry for up to 2ms.
  489. */
  490. for (cntr = 0; cntr < SDHCI_INTEL_PWR_TIMEOUT_CNT; cntr++) {
  491. reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
  492. if (reg & SDHCI_POWER_ON)
  493. break;
  494. udelay(SDHCI_INTEL_PWR_TIMEOUT_UDELAY);
  495. reg |= SDHCI_POWER_ON;
  496. sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
  497. }
  498. }
  499. static void sdhci_intel_set_uhs_signaling(struct sdhci_host *host,
  500. unsigned int timing)
  501. {
  502. /* Set UHS timing to SDR25 for High Speed mode */
  503. if (timing == MMC_TIMING_MMC_HS || timing == MMC_TIMING_SD_HS)
  504. timing = MMC_TIMING_UHS_SDR25;
  505. sdhci_set_uhs_signaling(host, timing);
  506. }
  507. #define INTEL_HS400_ES_REG 0x78
  508. #define INTEL_HS400_ES_BIT BIT(0)
  509. static void intel_hs400_enhanced_strobe(struct mmc_host *mmc,
  510. struct mmc_ios *ios)
  511. {
  512. struct sdhci_host *host = mmc_priv(mmc);
  513. u32 val;
  514. val = sdhci_readl(host, INTEL_HS400_ES_REG);
  515. if (ios->enhanced_strobe)
  516. val |= INTEL_HS400_ES_BIT;
  517. else
  518. val &= ~INTEL_HS400_ES_BIT;
  519. sdhci_writel(host, val, INTEL_HS400_ES_REG);
  520. }
  521. static int intel_start_signal_voltage_switch(struct mmc_host *mmc,
  522. struct mmc_ios *ios)
  523. {
  524. struct device *dev = mmc_dev(mmc);
  525. struct sdhci_host *host = mmc_priv(mmc);
  526. struct sdhci_pci_slot *slot = sdhci_priv(host);
  527. struct intel_host *intel_host = sdhci_pci_priv(slot);
  528. unsigned int fn;
  529. u32 result = 0;
  530. int err;
  531. err = sdhci_start_signal_voltage_switch(mmc, ios);
  532. if (err)
  533. return err;
  534. switch (ios->signal_voltage) {
  535. case MMC_SIGNAL_VOLTAGE_330:
  536. fn = INTEL_DSM_V33_SWITCH;
  537. break;
  538. case MMC_SIGNAL_VOLTAGE_180:
  539. fn = INTEL_DSM_V18_SWITCH;
  540. break;
  541. default:
  542. return 0;
  543. }
  544. err = intel_dsm(intel_host, dev, fn, &result);
  545. pr_debug("%s: %s DSM fn %u error %d result %u\n",
  546. mmc_hostname(mmc), __func__, fn, err, result);
  547. return 0;
  548. }
  549. static void sdhci_intel_set_clock(struct sdhci_host *host, unsigned int clock)
  550. {
  551. u16 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  552. /* Stop card clock separately to avoid glitches on clock line */
  553. if (clk & SDHCI_CLOCK_CARD_EN)
  554. sdhci_writew(host, clk & ~SDHCI_CLOCK_CARD_EN, SDHCI_CLOCK_CONTROL);
  555. sdhci_set_clock(host, clock);
  556. }
  557. static const struct sdhci_ops sdhci_intel_byt_ops = {
  558. .set_clock = sdhci_intel_set_clock,
  559. .set_power = sdhci_intel_set_power,
  560. .enable_dma = sdhci_pci_enable_dma,
  561. .set_bus_width = sdhci_set_bus_width,
  562. .reset = sdhci_reset,
  563. .set_uhs_signaling = sdhci_intel_set_uhs_signaling,
  564. .hw_reset = sdhci_pci_hw_reset,
  565. };
  566. static const struct sdhci_ops sdhci_intel_glk_ops = {
  567. .set_clock = sdhci_intel_set_clock,
  568. .set_power = sdhci_intel_set_power,
  569. .enable_dma = sdhci_pci_enable_dma,
  570. .set_bus_width = sdhci_set_bus_width,
  571. .reset = sdhci_and_cqhci_reset,
  572. .set_uhs_signaling = sdhci_intel_set_uhs_signaling,
  573. .hw_reset = sdhci_pci_hw_reset,
  574. .irq = sdhci_cqhci_irq,
  575. };
  576. static void byt_read_dsm(struct sdhci_pci_slot *slot)
  577. {
  578. struct intel_host *intel_host = sdhci_pci_priv(slot);
  579. struct device *dev = &slot->chip->pdev->dev;
  580. struct mmc_host *mmc = slot->host->mmc;
  581. intel_dsm_init(intel_host, dev, mmc);
  582. slot->chip->rpm_retune = intel_host->d3_retune;
  583. }
  584. static int intel_execute_tuning(struct mmc_host *mmc, u32 opcode)
  585. {
  586. int err = sdhci_execute_tuning(mmc, opcode);
  587. struct sdhci_host *host = mmc_priv(mmc);
  588. if (err)
  589. return err;
  590. /*
  591. * Tuning can leave the IP in an active state (Buffer Read Enable bit
  592. * set) which prevents the entry to low power states (i.e. S0i3). Data
  593. * reset will clear it.
  594. */
  595. sdhci_reset(host, SDHCI_RESET_DATA);
  596. return 0;
  597. }
  598. #define INTEL_ACTIVELTR 0x804
  599. #define INTEL_IDLELTR 0x808
  600. #define INTEL_LTR_REQ BIT(15)
  601. #define INTEL_LTR_SCALE_MASK GENMASK(11, 10)
  602. #define INTEL_LTR_SCALE_1US (2 << 10)
  603. #define INTEL_LTR_SCALE_32US (3 << 10)
  604. #define INTEL_LTR_VALUE_MASK GENMASK(9, 0)
  605. static void intel_cache_ltr(struct sdhci_pci_slot *slot)
  606. {
  607. struct intel_host *intel_host = sdhci_pci_priv(slot);
  608. struct sdhci_host *host = slot->host;
  609. intel_host->active_ltr = readl(host->ioaddr + INTEL_ACTIVELTR);
  610. intel_host->idle_ltr = readl(host->ioaddr + INTEL_IDLELTR);
  611. }
  612. static void intel_ltr_set(struct device *dev, s32 val)
  613. {
  614. struct sdhci_pci_chip *chip = dev_get_drvdata(dev);
  615. struct sdhci_pci_slot *slot = chip->slots[0];
  616. struct intel_host *intel_host = sdhci_pci_priv(slot);
  617. struct sdhci_host *host = slot->host;
  618. u32 ltr;
  619. pm_runtime_get_sync(dev);
  620. /*
  621. * Program latency tolerance (LTR) accordingly what has been asked
  622. * by the PM QoS layer or disable it in case we were passed
  623. * negative value or PM_QOS_LATENCY_ANY.
  624. */
  625. ltr = readl(host->ioaddr + INTEL_ACTIVELTR);
  626. if (val == PM_QOS_LATENCY_ANY || val < 0) {
  627. ltr &= ~INTEL_LTR_REQ;
  628. } else {
  629. ltr |= INTEL_LTR_REQ;
  630. ltr &= ~INTEL_LTR_SCALE_MASK;
  631. ltr &= ~INTEL_LTR_VALUE_MASK;
  632. if (val > INTEL_LTR_VALUE_MASK) {
  633. val >>= 5;
  634. if (val > INTEL_LTR_VALUE_MASK)
  635. val = INTEL_LTR_VALUE_MASK;
  636. ltr |= INTEL_LTR_SCALE_32US | val;
  637. } else {
  638. ltr |= INTEL_LTR_SCALE_1US | val;
  639. }
  640. }
  641. if (ltr == intel_host->active_ltr)
  642. goto out;
  643. writel(ltr, host->ioaddr + INTEL_ACTIVELTR);
  644. writel(ltr, host->ioaddr + INTEL_IDLELTR);
  645. /* Cache the values into lpss structure */
  646. intel_cache_ltr(slot);
  647. out:
  648. pm_runtime_put_autosuspend(dev);
  649. }
  650. static bool intel_use_ltr(struct sdhci_pci_chip *chip)
  651. {
  652. switch (chip->pdev->device) {
  653. case PCI_DEVICE_ID_INTEL_BYT_EMMC:
  654. case PCI_DEVICE_ID_INTEL_BYT_EMMC2:
  655. case PCI_DEVICE_ID_INTEL_BYT_SDIO:
  656. case PCI_DEVICE_ID_INTEL_BYT_SD:
  657. case PCI_DEVICE_ID_INTEL_BSW_EMMC:
  658. case PCI_DEVICE_ID_INTEL_BSW_SDIO:
  659. case PCI_DEVICE_ID_INTEL_BSW_SD:
  660. return false;
  661. default:
  662. return true;
  663. }
  664. }
  665. static void intel_ltr_expose(struct sdhci_pci_chip *chip)
  666. {
  667. struct device *dev = &chip->pdev->dev;
  668. if (!intel_use_ltr(chip))
  669. return;
  670. dev->power.set_latency_tolerance = intel_ltr_set;
  671. dev_pm_qos_expose_latency_tolerance(dev);
  672. }
  673. static void intel_ltr_hide(struct sdhci_pci_chip *chip)
  674. {
  675. struct device *dev = &chip->pdev->dev;
  676. if (!intel_use_ltr(chip))
  677. return;
  678. dev_pm_qos_hide_latency_tolerance(dev);
  679. dev->power.set_latency_tolerance = NULL;
  680. }
  681. static void byt_probe_slot(struct sdhci_pci_slot *slot)
  682. {
  683. struct mmc_host_ops *ops = &slot->host->mmc_host_ops;
  684. struct device *dev = &slot->chip->pdev->dev;
  685. struct mmc_host *mmc = slot->host->mmc;
  686. byt_read_dsm(slot);
  687. byt_ocp_setting(slot->chip->pdev);
  688. ops->execute_tuning = intel_execute_tuning;
  689. ops->start_signal_voltage_switch = intel_start_signal_voltage_switch;
  690. device_property_read_u32(dev, "max-frequency", &mmc->f_max);
  691. if (!mmc->slotno) {
  692. slot->chip->slots[mmc->slotno] = slot;
  693. intel_ltr_expose(slot->chip);
  694. }
  695. }
  696. static void byt_add_debugfs(struct sdhci_pci_slot *slot)
  697. {
  698. struct intel_host *intel_host = sdhci_pci_priv(slot);
  699. struct mmc_host *mmc = slot->host->mmc;
  700. struct dentry *dir = mmc->debugfs_root;
  701. if (!intel_use_ltr(slot->chip))
  702. return;
  703. debugfs_create_x32("active_ltr", 0444, dir, &intel_host->active_ltr);
  704. debugfs_create_x32("idle_ltr", 0444, dir, &intel_host->idle_ltr);
  705. intel_cache_ltr(slot);
  706. }
  707. static int byt_add_host(struct sdhci_pci_slot *slot)
  708. {
  709. int ret = sdhci_add_host(slot->host);
  710. if (!ret)
  711. byt_add_debugfs(slot);
  712. return ret;
  713. }
  714. static void byt_remove_slot(struct sdhci_pci_slot *slot, int dead)
  715. {
  716. struct mmc_host *mmc = slot->host->mmc;
  717. if (!mmc->slotno)
  718. intel_ltr_hide(slot->chip);
  719. }
  720. static int byt_emmc_probe_slot(struct sdhci_pci_slot *slot)
  721. {
  722. byt_probe_slot(slot);
  723. slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
  724. MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR |
  725. MMC_CAP_CMD_DURING_TFR |
  726. MMC_CAP_WAIT_WHILE_BUSY;
  727. slot->hw_reset = sdhci_pci_int_hw_reset;
  728. if (slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_BSW_EMMC)
  729. slot->host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */
  730. slot->host->mmc_host_ops.select_drive_strength =
  731. intel_select_drive_strength;
  732. return 0;
  733. }
  734. static bool glk_broken_cqhci(struct sdhci_pci_slot *slot)
  735. {
  736. return slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_GLK_EMMC &&
  737. (dmi_match(DMI_BIOS_VENDOR, "LENOVO") ||
  738. dmi_match(DMI_SYS_VENDOR, "IRBIS") ||
  739. dmi_match(DMI_SYS_VENDOR, "Positivo Tecnologia SA"));
  740. }
  741. static bool jsl_broken_hs400es(struct sdhci_pci_slot *slot)
  742. {
  743. return slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_JSL_EMMC &&
  744. dmi_match(DMI_BIOS_VENDOR, "ASUSTeK COMPUTER INC.");
  745. }
  746. static int glk_emmc_probe_slot(struct sdhci_pci_slot *slot)
  747. {
  748. int ret = byt_emmc_probe_slot(slot);
  749. if (!glk_broken_cqhci(slot))
  750. slot->host->mmc->caps2 |= MMC_CAP2_CQE;
  751. if (slot->chip->pdev->device != PCI_DEVICE_ID_INTEL_GLK_EMMC) {
  752. if (!jsl_broken_hs400es(slot)) {
  753. slot->host->mmc->caps2 |= MMC_CAP2_HS400_ES;
  754. slot->host->mmc_host_ops.hs400_enhanced_strobe =
  755. intel_hs400_enhanced_strobe;
  756. }
  757. slot->host->mmc->caps2 |= MMC_CAP2_CQE_DCMD;
  758. }
  759. return ret;
  760. }
  761. static const struct cqhci_host_ops glk_cqhci_ops = {
  762. .enable = sdhci_cqe_enable,
  763. .disable = sdhci_cqe_disable,
  764. .dumpregs = sdhci_pci_dumpregs,
  765. };
  766. static int glk_emmc_add_host(struct sdhci_pci_slot *slot)
  767. {
  768. struct device *dev = &slot->chip->pdev->dev;
  769. struct sdhci_host *host = slot->host;
  770. struct cqhci_host *cq_host;
  771. bool dma64;
  772. int ret;
  773. ret = sdhci_setup_host(host);
  774. if (ret)
  775. return ret;
  776. cq_host = devm_kzalloc(dev, sizeof(*cq_host), GFP_KERNEL);
  777. if (!cq_host) {
  778. ret = -ENOMEM;
  779. goto cleanup;
  780. }
  781. cq_host->mmio = host->ioaddr + 0x200;
  782. cq_host->quirks |= CQHCI_QUIRK_SHORT_TXFR_DESC_SZ;
  783. cq_host->ops = &glk_cqhci_ops;
  784. dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
  785. if (dma64)
  786. cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
  787. ret = cqhci_init(cq_host, host->mmc, dma64);
  788. if (ret)
  789. goto cleanup;
  790. ret = __sdhci_add_host(host);
  791. if (ret)
  792. goto cleanup;
  793. byt_add_debugfs(slot);
  794. return 0;
  795. cleanup:
  796. sdhci_cleanup_host(host);
  797. return ret;
  798. }
  799. #ifdef CONFIG_PM
  800. #define GLK_RX_CTRL1 0x834
  801. #define GLK_TUN_VAL 0x840
  802. #define GLK_PATH_PLL GENMASK(13, 8)
  803. #define GLK_DLY GENMASK(6, 0)
  804. /* Workaround firmware failing to restore the tuning value */
  805. static void glk_rpm_retune_wa(struct sdhci_pci_chip *chip, bool susp)
  806. {
  807. struct sdhci_pci_slot *slot = chip->slots[0];
  808. struct intel_host *intel_host = sdhci_pci_priv(slot);
  809. struct sdhci_host *host = slot->host;
  810. u32 glk_rx_ctrl1;
  811. u32 glk_tun_val;
  812. u32 dly;
  813. if (intel_host->rpm_retune_ok || !mmc_can_retune(host->mmc))
  814. return;
  815. glk_rx_ctrl1 = sdhci_readl(host, GLK_RX_CTRL1);
  816. glk_tun_val = sdhci_readl(host, GLK_TUN_VAL);
  817. if (susp) {
  818. intel_host->glk_rx_ctrl1 = glk_rx_ctrl1;
  819. intel_host->glk_tun_val = glk_tun_val;
  820. return;
  821. }
  822. if (!intel_host->glk_tun_val)
  823. return;
  824. if (glk_rx_ctrl1 != intel_host->glk_rx_ctrl1) {
  825. intel_host->rpm_retune_ok = true;
  826. return;
  827. }
  828. dly = FIELD_PREP(GLK_DLY, FIELD_GET(GLK_PATH_PLL, glk_rx_ctrl1) +
  829. (intel_host->glk_tun_val << 1));
  830. if (dly == FIELD_GET(GLK_DLY, glk_rx_ctrl1))
  831. return;
  832. glk_rx_ctrl1 = (glk_rx_ctrl1 & ~GLK_DLY) | dly;
  833. sdhci_writel(host, glk_rx_ctrl1, GLK_RX_CTRL1);
  834. intel_host->rpm_retune_ok = true;
  835. chip->rpm_retune = true;
  836. mmc_retune_needed(host->mmc);
  837. pr_info("%s: Requiring re-tune after rpm resume", mmc_hostname(host->mmc));
  838. }
  839. static void glk_rpm_retune_chk(struct sdhci_pci_chip *chip, bool susp)
  840. {
  841. if (chip->pdev->device == PCI_DEVICE_ID_INTEL_GLK_EMMC &&
  842. !chip->rpm_retune)
  843. glk_rpm_retune_wa(chip, susp);
  844. }
  845. static int glk_runtime_suspend(struct sdhci_pci_chip *chip)
  846. {
  847. glk_rpm_retune_chk(chip, true);
  848. return sdhci_cqhci_runtime_suspend(chip);
  849. }
  850. static int glk_runtime_resume(struct sdhci_pci_chip *chip)
  851. {
  852. glk_rpm_retune_chk(chip, false);
  853. return sdhci_cqhci_runtime_resume(chip);
  854. }
  855. #endif
  856. #ifdef CONFIG_ACPI
  857. static int ni_set_max_freq(struct sdhci_pci_slot *slot)
  858. {
  859. acpi_status status;
  860. unsigned long long max_freq;
  861. status = acpi_evaluate_integer(ACPI_HANDLE(&slot->chip->pdev->dev),
  862. "MXFQ", NULL, &max_freq);
  863. if (ACPI_FAILURE(status)) {
  864. dev_err(&slot->chip->pdev->dev,
  865. "MXFQ not found in acpi table\n");
  866. return -EINVAL;
  867. }
  868. slot->host->mmc->f_max = max_freq * 1000000;
  869. return 0;
  870. }
  871. #else
  872. static inline int ni_set_max_freq(struct sdhci_pci_slot *slot)
  873. {
  874. return 0;
  875. }
  876. #endif
  877. static int ni_byt_sdio_probe_slot(struct sdhci_pci_slot *slot)
  878. {
  879. int err;
  880. byt_probe_slot(slot);
  881. err = ni_set_max_freq(slot);
  882. if (err)
  883. return err;
  884. slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE |
  885. MMC_CAP_WAIT_WHILE_BUSY;
  886. return 0;
  887. }
  888. static int byt_sdio_probe_slot(struct sdhci_pci_slot *slot)
  889. {
  890. byt_probe_slot(slot);
  891. slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE |
  892. MMC_CAP_WAIT_WHILE_BUSY;
  893. return 0;
  894. }
  895. static void byt_needs_pwr_off(struct sdhci_pci_slot *slot)
  896. {
  897. struct intel_host *intel_host = sdhci_pci_priv(slot);
  898. u8 reg = sdhci_readb(slot->host, SDHCI_POWER_CONTROL);
  899. intel_host->needs_pwr_off = reg & SDHCI_POWER_ON;
  900. }
  901. static int byt_sd_probe_slot(struct sdhci_pci_slot *slot)
  902. {
  903. byt_probe_slot(slot);
  904. slot->host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY |
  905. MMC_CAP_AGGRESSIVE_PM | MMC_CAP_CD_WAKE;
  906. slot->cd_idx = 0;
  907. slot->cd_override_level = true;
  908. if (slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_BXT_SD ||
  909. slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_BXTM_SD ||
  910. slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_APL_SD ||
  911. slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_GLK_SD)
  912. slot->host->mmc_host_ops.get_cd = bxt_get_cd;
  913. if (slot->chip->pdev->subsystem_vendor == PCI_VENDOR_ID_NI &&
  914. slot->chip->pdev->subsystem_device == PCI_SUBDEVICE_ID_NI_78E3)
  915. slot->host->mmc->caps2 |= MMC_CAP2_AVOID_3_3V;
  916. byt_needs_pwr_off(slot);
  917. return 0;
  918. }
  919. #ifdef CONFIG_PM_SLEEP
  920. static int byt_resume(struct sdhci_pci_chip *chip)
  921. {
  922. byt_ocp_setting(chip->pdev);
  923. return sdhci_pci_resume_host(chip);
  924. }
  925. #endif
  926. #ifdef CONFIG_PM
  927. static int byt_runtime_resume(struct sdhci_pci_chip *chip)
  928. {
  929. byt_ocp_setting(chip->pdev);
  930. return sdhci_pci_runtime_resume_host(chip);
  931. }
  932. #endif
  933. static const struct sdhci_pci_fixes sdhci_intel_byt_emmc = {
  934. #ifdef CONFIG_PM_SLEEP
  935. .resume = byt_resume,
  936. #endif
  937. #ifdef CONFIG_PM
  938. .runtime_resume = byt_runtime_resume,
  939. #endif
  940. .allow_runtime_pm = true,
  941. .probe_slot = byt_emmc_probe_slot,
  942. .add_host = byt_add_host,
  943. .remove_slot = byt_remove_slot,
  944. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
  945. SDHCI_QUIRK_NO_LED,
  946. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  947. SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 |
  948. SDHCI_QUIRK2_STOP_WITH_TC,
  949. .ops = &sdhci_intel_byt_ops,
  950. .priv_size = sizeof(struct intel_host),
  951. };
  952. static const struct sdhci_pci_fixes sdhci_intel_glk_emmc = {
  953. .allow_runtime_pm = true,
  954. .probe_slot = glk_emmc_probe_slot,
  955. .add_host = glk_emmc_add_host,
  956. .remove_slot = byt_remove_slot,
  957. #ifdef CONFIG_PM_SLEEP
  958. .suspend = sdhci_cqhci_suspend,
  959. .resume = sdhci_cqhci_resume,
  960. #endif
  961. #ifdef CONFIG_PM
  962. .runtime_suspend = glk_runtime_suspend,
  963. .runtime_resume = glk_runtime_resume,
  964. #endif
  965. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
  966. SDHCI_QUIRK_NO_LED,
  967. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  968. SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 |
  969. SDHCI_QUIRK2_STOP_WITH_TC,
  970. .ops = &sdhci_intel_glk_ops,
  971. .priv_size = sizeof(struct intel_host),
  972. };
  973. static const struct sdhci_pci_fixes sdhci_ni_byt_sdio = {
  974. #ifdef CONFIG_PM_SLEEP
  975. .resume = byt_resume,
  976. #endif
  977. #ifdef CONFIG_PM
  978. .runtime_resume = byt_runtime_resume,
  979. #endif
  980. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
  981. SDHCI_QUIRK_NO_LED,
  982. .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON |
  983. SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
  984. .allow_runtime_pm = true,
  985. .probe_slot = ni_byt_sdio_probe_slot,
  986. .add_host = byt_add_host,
  987. .remove_slot = byt_remove_slot,
  988. .ops = &sdhci_intel_byt_ops,
  989. .priv_size = sizeof(struct intel_host),
  990. };
  991. static const struct sdhci_pci_fixes sdhci_intel_byt_sdio = {
  992. #ifdef CONFIG_PM_SLEEP
  993. .resume = byt_resume,
  994. #endif
  995. #ifdef CONFIG_PM
  996. .runtime_resume = byt_runtime_resume,
  997. #endif
  998. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
  999. SDHCI_QUIRK_NO_LED,
  1000. .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON |
  1001. SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
  1002. .allow_runtime_pm = true,
  1003. .probe_slot = byt_sdio_probe_slot,
  1004. .add_host = byt_add_host,
  1005. .remove_slot = byt_remove_slot,
  1006. .ops = &sdhci_intel_byt_ops,
  1007. .priv_size = sizeof(struct intel_host),
  1008. };
  1009. /* DMI quirks for devices with missing or broken CD GPIO info */
  1010. static const struct gpiod_lookup_table vexia_edu_atla10_cd_gpios = {
  1011. .dev_id = "0000:00:12.0",
  1012. .table = {
  1013. GPIO_LOOKUP("INT33FC:00", 38, "cd", GPIO_ACTIVE_HIGH),
  1014. { }
  1015. },
  1016. };
  1017. static const struct dmi_system_id sdhci_intel_byt_cd_gpio_override[] = {
  1018. {
  1019. /* Vexia Edu Atla 10 tablet 9V version */
  1020. .matches = {
  1021. DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
  1022. DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
  1023. /* Above strings are too generic, also match on BIOS date */
  1024. DMI_MATCH(DMI_BIOS_DATE, "08/25/2014"),
  1025. },
  1026. .driver_data = (void *)&vexia_edu_atla10_cd_gpios,
  1027. },
  1028. { }
  1029. };
  1030. static const struct sdhci_pci_fixes sdhci_intel_byt_sd = {
  1031. #ifdef CONFIG_PM_SLEEP
  1032. .resume = byt_resume,
  1033. #endif
  1034. #ifdef CONFIG_PM
  1035. .runtime_resume = byt_runtime_resume,
  1036. #endif
  1037. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
  1038. SDHCI_QUIRK_NO_LED,
  1039. .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
  1040. SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  1041. SDHCI_QUIRK2_STOP_WITH_TC,
  1042. .allow_runtime_pm = true,
  1043. .own_cd_for_runtime_pm = true,
  1044. .probe_slot = byt_sd_probe_slot,
  1045. .add_host = byt_add_host,
  1046. .remove_slot = byt_remove_slot,
  1047. .ops = &sdhci_intel_byt_ops,
  1048. .cd_gpio_override = sdhci_intel_byt_cd_gpio_override,
  1049. .priv_size = sizeof(struct intel_host),
  1050. };
  1051. /* Define Host controllers for Intel Merrifield platform */
  1052. #define INTEL_MRFLD_EMMC_0 0
  1053. #define INTEL_MRFLD_EMMC_1 1
  1054. #define INTEL_MRFLD_SD 2
  1055. #define INTEL_MRFLD_SDIO 3
  1056. #ifdef CONFIG_ACPI
  1057. static void intel_mrfld_mmc_fix_up_power_slot(struct sdhci_pci_slot *slot)
  1058. {
  1059. struct acpi_device *device;
  1060. device = ACPI_COMPANION(&slot->chip->pdev->dev);
  1061. if (device)
  1062. acpi_device_fix_up_power_extended(device);
  1063. }
  1064. #else
  1065. static inline void intel_mrfld_mmc_fix_up_power_slot(struct sdhci_pci_slot *slot) {}
  1066. #endif
  1067. static int intel_mrfld_mmc_probe_slot(struct sdhci_pci_slot *slot)
  1068. {
  1069. unsigned int func = PCI_FUNC(slot->chip->pdev->devfn);
  1070. switch (func) {
  1071. case INTEL_MRFLD_EMMC_0:
  1072. case INTEL_MRFLD_EMMC_1:
  1073. slot->host->mmc->caps |= MMC_CAP_NONREMOVABLE |
  1074. MMC_CAP_8_BIT_DATA |
  1075. MMC_CAP_1_8V_DDR;
  1076. break;
  1077. case INTEL_MRFLD_SD:
  1078. slot->cd_idx = 0;
  1079. slot->cd_override_level = true;
  1080. /*
  1081. * There are two PCB designs of SD card slot with the opposite
  1082. * card detection sense. Quirk this out by ignoring GPIO state
  1083. * completely in the custom ->get_cd() callback.
  1084. */
  1085. slot->host->mmc_host_ops.get_cd = mrfld_get_cd;
  1086. slot->host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
  1087. break;
  1088. case INTEL_MRFLD_SDIO:
  1089. /* Advertise 2.0v for compatibility with the SDIO card's OCR */
  1090. slot->host->ocr_mask = MMC_VDD_20_21 | MMC_VDD_165_195;
  1091. slot->host->mmc->caps |= MMC_CAP_NONREMOVABLE |
  1092. MMC_CAP_POWER_OFF_CARD;
  1093. break;
  1094. default:
  1095. return -ENODEV;
  1096. }
  1097. intel_mrfld_mmc_fix_up_power_slot(slot);
  1098. return 0;
  1099. }
  1100. static const struct sdhci_pci_fixes sdhci_intel_mrfld_mmc = {
  1101. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  1102. .quirks2 = SDHCI_QUIRK2_BROKEN_HS200 |
  1103. SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
  1104. .allow_runtime_pm = true,
  1105. .probe_slot = intel_mrfld_mmc_probe_slot,
  1106. };
  1107. #define JMB388_SAMPLE_COUNT 5
  1108. static int jmicron_jmb388_get_ro(struct mmc_host *mmc)
  1109. {
  1110. int i, ro_count;
  1111. ro_count = 0;
  1112. for (i = 0; i < JMB388_SAMPLE_COUNT; i++) {
  1113. if (sdhci_get_ro(mmc) > 0) {
  1114. if (++ro_count > JMB388_SAMPLE_COUNT / 2)
  1115. return 1;
  1116. }
  1117. msleep(30);
  1118. }
  1119. return 0;
  1120. }
  1121. static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
  1122. {
  1123. u8 scratch;
  1124. int ret;
  1125. ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
  1126. if (ret)
  1127. goto fail;
  1128. /*
  1129. * Turn PMOS on [bit 0], set over current detection to 2.4 V
  1130. * [bit 1:2] and enable over current debouncing [bit 6].
  1131. */
  1132. if (on)
  1133. scratch |= 0x47;
  1134. else
  1135. scratch &= ~0x47;
  1136. ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
  1137. fail:
  1138. return pcibios_err_to_errno(ret);
  1139. }
  1140. static int jmicron_probe(struct sdhci_pci_chip *chip)
  1141. {
  1142. int ret;
  1143. u16 mmcdev = 0;
  1144. if (chip->pdev->revision == 0) {
  1145. chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
  1146. SDHCI_QUIRK_32BIT_DMA_SIZE |
  1147. SDHCI_QUIRK_32BIT_ADMA_SIZE |
  1148. SDHCI_QUIRK_RESET_AFTER_REQUEST |
  1149. SDHCI_QUIRK_BROKEN_SMALL_PIO;
  1150. }
  1151. /*
  1152. * JMicron chips can have two interfaces to the same hardware
  1153. * in order to work around limitations in Microsoft's driver.
  1154. * We need to make sure we only bind to one of them.
  1155. *
  1156. * This code assumes two things:
  1157. *
  1158. * 1. The PCI code adds subfunctions in order.
  1159. *
  1160. * 2. The MMC interface has a lower subfunction number
  1161. * than the SD interface.
  1162. */
  1163. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
  1164. mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
  1165. else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
  1166. mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
  1167. if (mmcdev) {
  1168. struct pci_dev *sd_dev;
  1169. sd_dev = NULL;
  1170. while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
  1171. mmcdev, sd_dev)) != NULL) {
  1172. if ((PCI_SLOT(chip->pdev->devfn) ==
  1173. PCI_SLOT(sd_dev->devfn)) &&
  1174. (chip->pdev->bus == sd_dev->bus))
  1175. break;
  1176. }
  1177. if (sd_dev) {
  1178. pci_dev_put(sd_dev);
  1179. dev_info(&chip->pdev->dev, "Refusing to bind to "
  1180. "secondary interface.\n");
  1181. return -ENODEV;
  1182. }
  1183. }
  1184. /*
  1185. * JMicron chips need a bit of a nudge to enable the power
  1186. * output pins.
  1187. */
  1188. ret = jmicron_pmos(chip, 1);
  1189. if (ret) {
  1190. dev_err(&chip->pdev->dev, "Failure enabling card power\n");
  1191. return ret;
  1192. }
  1193. return 0;
  1194. }
  1195. static void jmicron_enable_mmc(struct sdhci_host *host, int on)
  1196. {
  1197. u8 scratch;
  1198. scratch = readb(host->ioaddr + 0xC0);
  1199. if (on)
  1200. scratch |= 0x01;
  1201. else
  1202. scratch &= ~0x01;
  1203. writeb(scratch, host->ioaddr + 0xC0);
  1204. }
  1205. static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
  1206. {
  1207. if (slot->chip->pdev->revision == 0) {
  1208. u16 version;
  1209. version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
  1210. version = (version & SDHCI_VENDOR_VER_MASK) >>
  1211. SDHCI_VENDOR_VER_SHIFT;
  1212. /*
  1213. * Older versions of the chip have lots of nasty glitches
  1214. * in the ADMA engine. It's best just to avoid it
  1215. * completely.
  1216. */
  1217. if (version < 0xAC)
  1218. slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
  1219. }
  1220. /* JM388 MMC doesn't support 1.8V while SD supports it */
  1221. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
  1222. slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
  1223. MMC_VDD_29_30 | MMC_VDD_30_31 |
  1224. MMC_VDD_165_195; /* allow 1.8V */
  1225. slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
  1226. MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
  1227. }
  1228. /*
  1229. * The secondary interface requires a bit set to get the
  1230. * interrupts.
  1231. */
  1232. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  1233. slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
  1234. jmicron_enable_mmc(slot->host, 1);
  1235. slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
  1236. /* Handle unstable RO-detection on JM388 chips */
  1237. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
  1238. slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
  1239. slot->host->mmc_host_ops.get_ro = jmicron_jmb388_get_ro;
  1240. return 0;
  1241. }
  1242. static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
  1243. {
  1244. if (dead)
  1245. return;
  1246. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  1247. slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
  1248. jmicron_enable_mmc(slot->host, 0);
  1249. }
  1250. #ifdef CONFIG_PM_SLEEP
  1251. static int jmicron_suspend(struct sdhci_pci_chip *chip)
  1252. {
  1253. int i, ret;
  1254. ret = sdhci_pci_suspend_host(chip);
  1255. if (ret)
  1256. return ret;
  1257. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  1258. chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
  1259. for (i = 0; i < chip->num_slots; i++)
  1260. jmicron_enable_mmc(chip->slots[i]->host, 0);
  1261. }
  1262. return 0;
  1263. }
  1264. static int jmicron_resume(struct sdhci_pci_chip *chip)
  1265. {
  1266. int ret, i;
  1267. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  1268. chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
  1269. for (i = 0; i < chip->num_slots; i++)
  1270. jmicron_enable_mmc(chip->slots[i]->host, 1);
  1271. }
  1272. ret = jmicron_pmos(chip, 1);
  1273. if (ret) {
  1274. dev_err(&chip->pdev->dev, "Failure enabling card power\n");
  1275. return ret;
  1276. }
  1277. return sdhci_pci_resume_host(chip);
  1278. }
  1279. #endif
  1280. static const struct sdhci_pci_fixes sdhci_jmicron = {
  1281. .probe = jmicron_probe,
  1282. .probe_slot = jmicron_probe_slot,
  1283. .remove_slot = jmicron_remove_slot,
  1284. #ifdef CONFIG_PM_SLEEP
  1285. .suspend = jmicron_suspend,
  1286. .resume = jmicron_resume,
  1287. #endif
  1288. };
  1289. /* SysKonnect CardBus2SDIO extra registers */
  1290. #define SYSKT_CTRL 0x200
  1291. #define SYSKT_RDFIFO_STAT 0x204
  1292. #define SYSKT_WRFIFO_STAT 0x208
  1293. #define SYSKT_POWER_DATA 0x20c
  1294. #define SYSKT_POWER_330 0xef
  1295. #define SYSKT_POWER_300 0xf8
  1296. #define SYSKT_POWER_184 0xcc
  1297. #define SYSKT_POWER_CMD 0x20d
  1298. #define SYSKT_POWER_START (1 << 7)
  1299. #define SYSKT_POWER_STATUS 0x20e
  1300. #define SYSKT_POWER_STATUS_OK (1 << 0)
  1301. #define SYSKT_BOARD_REV 0x210
  1302. #define SYSKT_CHIP_REV 0x211
  1303. #define SYSKT_CONF_DATA 0x212
  1304. #define SYSKT_CONF_DATA_1V8 (1 << 2)
  1305. #define SYSKT_CONF_DATA_2V5 (1 << 1)
  1306. #define SYSKT_CONF_DATA_3V3 (1 << 0)
  1307. static int syskt_probe(struct sdhci_pci_chip *chip)
  1308. {
  1309. if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
  1310. chip->pdev->class &= ~0x0000FF;
  1311. chip->pdev->class |= PCI_SDHCI_IFDMA;
  1312. }
  1313. return 0;
  1314. }
  1315. static int syskt_probe_slot(struct sdhci_pci_slot *slot)
  1316. {
  1317. int tm, ps;
  1318. u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
  1319. u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
  1320. dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
  1321. "board rev %d.%d, chip rev %d.%d\n",
  1322. board_rev >> 4, board_rev & 0xf,
  1323. chip_rev >> 4, chip_rev & 0xf);
  1324. if (chip_rev >= 0x20)
  1325. slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
  1326. writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
  1327. writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
  1328. udelay(50);
  1329. tm = 10; /* Wait max 1 ms */
  1330. do {
  1331. ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
  1332. if (ps & SYSKT_POWER_STATUS_OK)
  1333. break;
  1334. udelay(100);
  1335. } while (--tm);
  1336. if (!tm) {
  1337. dev_err(&slot->chip->pdev->dev,
  1338. "power regulator never stabilized");
  1339. writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
  1340. return -ENODEV;
  1341. }
  1342. return 0;
  1343. }
  1344. static const struct sdhci_pci_fixes sdhci_syskt = {
  1345. .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
  1346. .probe = syskt_probe,
  1347. .probe_slot = syskt_probe_slot,
  1348. };
  1349. static int via_probe(struct sdhci_pci_chip *chip)
  1350. {
  1351. if (chip->pdev->revision == 0x10)
  1352. chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
  1353. return 0;
  1354. }
  1355. static const struct sdhci_pci_fixes sdhci_via = {
  1356. .probe = via_probe,
  1357. };
  1358. static int rtsx_probe_slot(struct sdhci_pci_slot *slot)
  1359. {
  1360. slot->host->mmc->caps2 |= MMC_CAP2_HS200;
  1361. return 0;
  1362. }
  1363. static const struct sdhci_pci_fixes sdhci_rtsx = {
  1364. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  1365. SDHCI_QUIRK2_BROKEN_64_BIT_DMA |
  1366. SDHCI_QUIRK2_BROKEN_DDR50,
  1367. .probe_slot = rtsx_probe_slot,
  1368. };
  1369. /*AMD chipset generation*/
  1370. enum amd_chipset_gen {
  1371. AMD_CHIPSET_BEFORE_ML,
  1372. AMD_CHIPSET_CZ,
  1373. AMD_CHIPSET_NL,
  1374. AMD_CHIPSET_UNKNOWN,
  1375. };
  1376. /* AMD registers */
  1377. #define AMD_SD_AUTO_PATTERN 0xB8
  1378. #define AMD_MSLEEP_DURATION 4
  1379. #define AMD_SD_MISC_CONTROL 0xD0
  1380. #define AMD_MAX_TUNE_VALUE 0x0B
  1381. #define AMD_AUTO_TUNE_SEL 0x10800
  1382. #define AMD_FIFO_PTR 0x30
  1383. #define AMD_BIT_MASK 0x1F
  1384. static void amd_tuning_reset(struct sdhci_host *host)
  1385. {
  1386. unsigned int val;
  1387. val = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1388. val |= SDHCI_CTRL_PRESET_VAL_ENABLE | SDHCI_CTRL_EXEC_TUNING;
  1389. sdhci_writew(host, val, SDHCI_HOST_CONTROL2);
  1390. val = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1391. val &= ~SDHCI_CTRL_EXEC_TUNING;
  1392. sdhci_writew(host, val, SDHCI_HOST_CONTROL2);
  1393. }
  1394. static void amd_config_tuning_phase(struct pci_dev *pdev, u8 phase)
  1395. {
  1396. unsigned int val;
  1397. pci_read_config_dword(pdev, AMD_SD_AUTO_PATTERN, &val);
  1398. val &= ~AMD_BIT_MASK;
  1399. val |= (AMD_AUTO_TUNE_SEL | (phase << 1));
  1400. pci_write_config_dword(pdev, AMD_SD_AUTO_PATTERN, val);
  1401. }
  1402. static void amd_enable_manual_tuning(struct pci_dev *pdev)
  1403. {
  1404. unsigned int val;
  1405. pci_read_config_dword(pdev, AMD_SD_MISC_CONTROL, &val);
  1406. val |= AMD_FIFO_PTR;
  1407. pci_write_config_dword(pdev, AMD_SD_MISC_CONTROL, val);
  1408. }
  1409. static int amd_execute_tuning_hs200(struct sdhci_host *host, u32 opcode)
  1410. {
  1411. struct sdhci_pci_slot *slot = sdhci_priv(host);
  1412. struct pci_dev *pdev = slot->chip->pdev;
  1413. u8 valid_win = 0;
  1414. u8 valid_win_max = 0;
  1415. u8 valid_win_end = 0;
  1416. u8 ctrl, tune_around;
  1417. amd_tuning_reset(host);
  1418. for (tune_around = 0; tune_around < 12; tune_around++) {
  1419. amd_config_tuning_phase(pdev, tune_around);
  1420. if (mmc_send_tuning(host->mmc, opcode, NULL)) {
  1421. valid_win = 0;
  1422. msleep(AMD_MSLEEP_DURATION);
  1423. ctrl = SDHCI_RESET_CMD | SDHCI_RESET_DATA;
  1424. sdhci_writeb(host, ctrl, SDHCI_SOFTWARE_RESET);
  1425. } else if (++valid_win > valid_win_max) {
  1426. valid_win_max = valid_win;
  1427. valid_win_end = tune_around;
  1428. }
  1429. }
  1430. if (!valid_win_max) {
  1431. dev_err(&pdev->dev, "no tuning point found\n");
  1432. return -EIO;
  1433. }
  1434. amd_config_tuning_phase(pdev, valid_win_end - valid_win_max / 2);
  1435. amd_enable_manual_tuning(pdev);
  1436. host->mmc->retune_period = 0;
  1437. return 0;
  1438. }
  1439. static int amd_execute_tuning(struct mmc_host *mmc, u32 opcode)
  1440. {
  1441. struct sdhci_host *host = mmc_priv(mmc);
  1442. /* AMD requires custom HS200 tuning */
  1443. if (host->timing == MMC_TIMING_MMC_HS200)
  1444. return amd_execute_tuning_hs200(host, opcode);
  1445. /* Otherwise perform standard SDHCI tuning */
  1446. return sdhci_execute_tuning(mmc, opcode);
  1447. }
  1448. static int amd_probe_slot(struct sdhci_pci_slot *slot)
  1449. {
  1450. struct mmc_host_ops *ops = &slot->host->mmc_host_ops;
  1451. ops->execute_tuning = amd_execute_tuning;
  1452. return 0;
  1453. }
  1454. static int amd_probe(struct sdhci_pci_chip *chip)
  1455. {
  1456. struct pci_dev *smbus_dev;
  1457. enum amd_chipset_gen gen;
  1458. smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
  1459. PCI_DEVICE_ID_AMD_HUDSON2_SMBUS, NULL);
  1460. if (smbus_dev) {
  1461. gen = AMD_CHIPSET_BEFORE_ML;
  1462. } else {
  1463. smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
  1464. PCI_DEVICE_ID_AMD_KERNCZ_SMBUS, NULL);
  1465. if (smbus_dev) {
  1466. if (smbus_dev->revision < 0x51)
  1467. gen = AMD_CHIPSET_CZ;
  1468. else
  1469. gen = AMD_CHIPSET_NL;
  1470. } else {
  1471. gen = AMD_CHIPSET_UNKNOWN;
  1472. }
  1473. }
  1474. pci_dev_put(smbus_dev);
  1475. if (gen == AMD_CHIPSET_BEFORE_ML || gen == AMD_CHIPSET_CZ)
  1476. chip->quirks2 |= SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD;
  1477. return 0;
  1478. }
  1479. static u32 sdhci_read_present_state(struct sdhci_host *host)
  1480. {
  1481. return sdhci_readl(host, SDHCI_PRESENT_STATE);
  1482. }
  1483. static void amd_sdhci_reset(struct sdhci_host *host, u8 mask)
  1484. {
  1485. struct sdhci_pci_slot *slot = sdhci_priv(host);
  1486. struct pci_dev *pdev = slot->chip->pdev;
  1487. u32 present_state;
  1488. /*
  1489. * SDHC 0x7906 requires a hard reset to clear all internal state.
  1490. * Otherwise it can get into a bad state where the DATA lines are always
  1491. * read as zeros.
  1492. */
  1493. if (pdev->device == 0x7906 && (mask & SDHCI_RESET_ALL)) {
  1494. pci_clear_master(pdev);
  1495. pci_save_state(pdev);
  1496. pci_set_power_state(pdev, PCI_D3cold);
  1497. pr_debug("%s: power_state=%u\n", mmc_hostname(host->mmc),
  1498. pdev->current_state);
  1499. pci_set_power_state(pdev, PCI_D0);
  1500. pci_restore_state(pdev);
  1501. /*
  1502. * SDHCI_RESET_ALL says the card detect logic should not be
  1503. * reset, but since we need to reset the entire controller
  1504. * we should wait until the card detect logic has stabilized.
  1505. *
  1506. * This normally takes about 40ms.
  1507. */
  1508. readx_poll_timeout(
  1509. sdhci_read_present_state,
  1510. host,
  1511. present_state,
  1512. present_state & SDHCI_CD_STABLE,
  1513. 10000,
  1514. 100000
  1515. );
  1516. }
  1517. return sdhci_reset(host, mask);
  1518. }
  1519. static const struct sdhci_ops amd_sdhci_pci_ops = {
  1520. .set_clock = sdhci_set_clock,
  1521. .enable_dma = sdhci_pci_enable_dma,
  1522. .set_bus_width = sdhci_set_bus_width,
  1523. .reset = amd_sdhci_reset,
  1524. .set_uhs_signaling = sdhci_set_uhs_signaling,
  1525. };
  1526. static const struct sdhci_pci_fixes sdhci_amd = {
  1527. .probe = amd_probe,
  1528. .ops = &amd_sdhci_pci_ops,
  1529. .probe_slot = amd_probe_slot,
  1530. };
  1531. static const struct pci_device_id pci_ids[] = {
  1532. SDHCI_PCI_DEVICE(RICOH, R5C822, ricoh),
  1533. SDHCI_PCI_DEVICE(RICOH, R5C843, ricoh_mmc),
  1534. SDHCI_PCI_DEVICE(RICOH, R5CE822, ricoh_mmc),
  1535. SDHCI_PCI_DEVICE(RICOH, R5CE823, ricoh_mmc),
  1536. SDHCI_PCI_DEVICE(ENE, CB712_SD, ene_712),
  1537. SDHCI_PCI_DEVICE(ENE, CB712_SD_2, ene_712),
  1538. SDHCI_PCI_DEVICE(ENE, CB714_SD, ene_714),
  1539. SDHCI_PCI_DEVICE(ENE, CB714_SD_2, ene_714),
  1540. SDHCI_PCI_DEVICE(MARVELL, 88ALP01_SD, cafe),
  1541. SDHCI_PCI_DEVICE(JMICRON, JMB38X_SD, jmicron),
  1542. SDHCI_PCI_DEVICE(JMICRON, JMB38X_MMC, jmicron),
  1543. SDHCI_PCI_DEVICE(JMICRON, JMB388_SD, jmicron),
  1544. SDHCI_PCI_DEVICE(JMICRON, JMB388_ESD, jmicron),
  1545. SDHCI_PCI_DEVICE(SYSKONNECT, 8000, syskt),
  1546. SDHCI_PCI_DEVICE(VIA, 95D0, via),
  1547. SDHCI_PCI_DEVICE(REALTEK, 5250, rtsx),
  1548. SDHCI_PCI_DEVICE(INTEL, QRK_SD, intel_qrk),
  1549. SDHCI_PCI_DEVICE(INTEL, MRST_SD0, intel_mrst_hc0),
  1550. SDHCI_PCI_DEVICE(INTEL, MRST_SD1, intel_mrst_hc1_hc2),
  1551. SDHCI_PCI_DEVICE(INTEL, MRST_SD2, intel_mrst_hc1_hc2),
  1552. SDHCI_PCI_DEVICE(INTEL, MFD_SD, intel_mfd_sd),
  1553. SDHCI_PCI_DEVICE(INTEL, MFD_SDIO1, intel_mfd_sdio),
  1554. SDHCI_PCI_DEVICE(INTEL, MFD_SDIO2, intel_mfd_sdio),
  1555. SDHCI_PCI_DEVICE(INTEL, MFD_EMMC0, intel_mfd_emmc),
  1556. SDHCI_PCI_DEVICE(INTEL, MFD_EMMC1, intel_mfd_emmc),
  1557. SDHCI_PCI_DEVICE(INTEL, PCH_SDIO0, intel_pch_sdio),
  1558. SDHCI_PCI_DEVICE(INTEL, PCH_SDIO1, intel_pch_sdio),
  1559. SDHCI_PCI_DEVICE(INTEL, BYT_EMMC, intel_byt_emmc),
  1560. SDHCI_PCI_SUBDEVICE(INTEL, BYT_SDIO, NI, 7884, ni_byt_sdio),
  1561. SDHCI_PCI_DEVICE(INTEL, BYT_SDIO, intel_byt_sdio),
  1562. SDHCI_PCI_DEVICE(INTEL, BYT_SD, intel_byt_sd),
  1563. SDHCI_PCI_DEVICE(INTEL, BYT_EMMC2, intel_byt_emmc),
  1564. SDHCI_PCI_DEVICE(INTEL, BSW_EMMC, intel_byt_emmc),
  1565. SDHCI_PCI_DEVICE(INTEL, BSW_SDIO, intel_byt_sdio),
  1566. SDHCI_PCI_DEVICE(INTEL, BSW_SD, intel_byt_sd),
  1567. SDHCI_PCI_DEVICE(INTEL, CLV_SDIO0, intel_mfd_sd),
  1568. SDHCI_PCI_DEVICE(INTEL, CLV_SDIO1, intel_mfd_sdio),
  1569. SDHCI_PCI_DEVICE(INTEL, CLV_SDIO2, intel_mfd_sdio),
  1570. SDHCI_PCI_DEVICE(INTEL, CLV_EMMC0, intel_mfd_emmc),
  1571. SDHCI_PCI_DEVICE(INTEL, CLV_EMMC1, intel_mfd_emmc),
  1572. SDHCI_PCI_DEVICE(INTEL, MRFLD_MMC, intel_mrfld_mmc),
  1573. SDHCI_PCI_DEVICE(INTEL, SPT_EMMC, intel_byt_emmc),
  1574. SDHCI_PCI_DEVICE(INTEL, SPT_SDIO, intel_byt_sdio),
  1575. SDHCI_PCI_DEVICE(INTEL, SPT_SD, intel_byt_sd),
  1576. SDHCI_PCI_DEVICE(INTEL, DNV_EMMC, intel_byt_emmc),
  1577. SDHCI_PCI_DEVICE(INTEL, CDF_EMMC, intel_glk_emmc),
  1578. SDHCI_PCI_DEVICE(INTEL, BXT_EMMC, intel_byt_emmc),
  1579. SDHCI_PCI_DEVICE(INTEL, BXT_SDIO, intel_byt_sdio),
  1580. SDHCI_PCI_DEVICE(INTEL, BXT_SD, intel_byt_sd),
  1581. SDHCI_PCI_DEVICE(INTEL, BXTM_EMMC, intel_byt_emmc),
  1582. SDHCI_PCI_DEVICE(INTEL, BXTM_SDIO, intel_byt_sdio),
  1583. SDHCI_PCI_DEVICE(INTEL, BXTM_SD, intel_byt_sd),
  1584. SDHCI_PCI_DEVICE(INTEL, APL_EMMC, intel_byt_emmc),
  1585. SDHCI_PCI_DEVICE(INTEL, APL_SDIO, intel_byt_sdio),
  1586. SDHCI_PCI_DEVICE(INTEL, APL_SD, intel_byt_sd),
  1587. SDHCI_PCI_DEVICE(INTEL, GLK_EMMC, intel_glk_emmc),
  1588. SDHCI_PCI_DEVICE(INTEL, GLK_SDIO, intel_byt_sdio),
  1589. SDHCI_PCI_DEVICE(INTEL, GLK_SD, intel_byt_sd),
  1590. SDHCI_PCI_DEVICE(INTEL, CNP_EMMC, intel_glk_emmc),
  1591. SDHCI_PCI_DEVICE(INTEL, CNP_SD, intel_byt_sd),
  1592. SDHCI_PCI_DEVICE(INTEL, CNPH_SD, intel_byt_sd),
  1593. SDHCI_PCI_DEVICE(INTEL, ICP_EMMC, intel_glk_emmc),
  1594. SDHCI_PCI_DEVICE(INTEL, ICP_SD, intel_byt_sd),
  1595. SDHCI_PCI_DEVICE(INTEL, EHL_EMMC, intel_glk_emmc),
  1596. SDHCI_PCI_DEVICE(INTEL, EHL_SD, intel_byt_sd),
  1597. SDHCI_PCI_DEVICE(INTEL, CML_EMMC, intel_glk_emmc),
  1598. SDHCI_PCI_DEVICE(INTEL, CML_SD, intel_byt_sd),
  1599. SDHCI_PCI_DEVICE(INTEL, CMLH_SD, intel_byt_sd),
  1600. SDHCI_PCI_DEVICE(INTEL, JSL_EMMC, intel_glk_emmc),
  1601. SDHCI_PCI_DEVICE(INTEL, JSL_SD, intel_byt_sd),
  1602. SDHCI_PCI_DEVICE(INTEL, LKF_EMMC, intel_glk_emmc),
  1603. SDHCI_PCI_DEVICE(INTEL, LKF_SD, intel_byt_sd),
  1604. SDHCI_PCI_DEVICE(INTEL, ADL_EMMC, intel_glk_emmc),
  1605. SDHCI_PCI_DEVICE(O2, 8120, o2),
  1606. SDHCI_PCI_DEVICE(O2, 8220, o2),
  1607. SDHCI_PCI_DEVICE(O2, 8221, o2),
  1608. SDHCI_PCI_DEVICE(O2, 8320, o2),
  1609. SDHCI_PCI_DEVICE(O2, 8321, o2),
  1610. SDHCI_PCI_DEVICE(O2, FUJIN2, o2),
  1611. SDHCI_PCI_DEVICE(O2, SDS0, o2),
  1612. SDHCI_PCI_DEVICE(O2, SDS1, o2),
  1613. SDHCI_PCI_DEVICE(O2, SEABIRD0, o2),
  1614. SDHCI_PCI_DEVICE(O2, SEABIRD1, o2),
  1615. SDHCI_PCI_DEVICE(O2, GG8_9860, o2),
  1616. SDHCI_PCI_DEVICE(O2, GG8_9861, o2),
  1617. SDHCI_PCI_DEVICE(O2, GG8_9862, o2),
  1618. SDHCI_PCI_DEVICE(O2, GG8_9863, o2),
  1619. SDHCI_PCI_DEVICE(ARASAN, PHY_EMMC, arasan),
  1620. SDHCI_PCI_DEVICE(SYNOPSYS, DWC_MSHC, snps),
  1621. SDHCI_PCI_DEVICE(GLI, 9750, gl9750),
  1622. SDHCI_PCI_DEVICE(GLI, 9755, gl9755),
  1623. SDHCI_PCI_DEVICE(GLI, 9763E, gl9763e),
  1624. SDHCI_PCI_DEVICE(GLI, 9767, gl9767),
  1625. SDHCI_PCI_DEVICE_CLASS(AMD, SYSTEM_SDHCI, PCI_CLASS_MASK, amd),
  1626. /* Generic SD host controller */
  1627. {PCI_DEVICE_CLASS(SYSTEM_SDHCI, PCI_CLASS_MASK)},
  1628. { /* end: all zeroes */ },
  1629. };
  1630. MODULE_DEVICE_TABLE(pci, pci_ids);
  1631. /*****************************************************************************\
  1632. * *
  1633. * SDHCI core callbacks *
  1634. * *
  1635. \*****************************************************************************/
  1636. int sdhci_pci_enable_dma(struct sdhci_host *host)
  1637. {
  1638. struct sdhci_pci_slot *slot;
  1639. struct pci_dev *pdev;
  1640. slot = sdhci_priv(host);
  1641. pdev = slot->chip->pdev;
  1642. if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
  1643. ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
  1644. (host->flags & SDHCI_USE_SDMA)) {
  1645. dev_warn(&pdev->dev, "Will use DMA mode even though HW "
  1646. "doesn't fully claim to support it.\n");
  1647. }
  1648. pci_set_master(pdev);
  1649. return 0;
  1650. }
  1651. static void sdhci_pci_hw_reset(struct sdhci_host *host)
  1652. {
  1653. struct sdhci_pci_slot *slot = sdhci_priv(host);
  1654. if (slot->hw_reset)
  1655. slot->hw_reset(host);
  1656. }
  1657. static const struct sdhci_ops sdhci_pci_ops = {
  1658. .set_clock = sdhci_set_clock,
  1659. .enable_dma = sdhci_pci_enable_dma,
  1660. .set_bus_width = sdhci_set_bus_width,
  1661. .reset = sdhci_reset,
  1662. .set_uhs_signaling = sdhci_set_uhs_signaling,
  1663. .hw_reset = sdhci_pci_hw_reset,
  1664. };
  1665. /*****************************************************************************\
  1666. * *
  1667. * Suspend/resume *
  1668. * *
  1669. \*****************************************************************************/
  1670. #ifdef CONFIG_PM_SLEEP
  1671. static int sdhci_pci_suspend(struct device *dev)
  1672. {
  1673. struct sdhci_pci_chip *chip = dev_get_drvdata(dev);
  1674. if (!chip)
  1675. return 0;
  1676. if (chip->fixes && chip->fixes->suspend)
  1677. return chip->fixes->suspend(chip);
  1678. return sdhci_pci_suspend_host(chip);
  1679. }
  1680. static int sdhci_pci_resume(struct device *dev)
  1681. {
  1682. struct sdhci_pci_chip *chip = dev_get_drvdata(dev);
  1683. if (!chip)
  1684. return 0;
  1685. if (chip->fixes && chip->fixes->resume)
  1686. return chip->fixes->resume(chip);
  1687. return sdhci_pci_resume_host(chip);
  1688. }
  1689. #endif
  1690. #ifdef CONFIG_PM
  1691. static int sdhci_pci_runtime_suspend(struct device *dev)
  1692. {
  1693. struct sdhci_pci_chip *chip = dev_get_drvdata(dev);
  1694. if (!chip)
  1695. return 0;
  1696. if (chip->fixes && chip->fixes->runtime_suspend)
  1697. return chip->fixes->runtime_suspend(chip);
  1698. return sdhci_pci_runtime_suspend_host(chip);
  1699. }
  1700. static int sdhci_pci_runtime_resume(struct device *dev)
  1701. {
  1702. struct sdhci_pci_chip *chip = dev_get_drvdata(dev);
  1703. if (!chip)
  1704. return 0;
  1705. if (chip->fixes && chip->fixes->runtime_resume)
  1706. return chip->fixes->runtime_resume(chip);
  1707. return sdhci_pci_runtime_resume_host(chip);
  1708. }
  1709. #endif
  1710. static const struct dev_pm_ops sdhci_pci_pm_ops = {
  1711. SET_SYSTEM_SLEEP_PM_OPS(sdhci_pci_suspend, sdhci_pci_resume)
  1712. SET_RUNTIME_PM_OPS(sdhci_pci_runtime_suspend,
  1713. sdhci_pci_runtime_resume, NULL)
  1714. };
  1715. /*****************************************************************************\
  1716. * *
  1717. * Device probing/removal *
  1718. * *
  1719. \*****************************************************************************/
  1720. static struct gpiod_lookup_table *sdhci_pci_add_gpio_lookup_table(
  1721. struct sdhci_pci_chip *chip)
  1722. {
  1723. struct gpiod_lookup_table *cd_gpio_lookup_table;
  1724. const struct dmi_system_id *dmi_id = NULL;
  1725. size_t count;
  1726. if (chip->fixes && chip->fixes->cd_gpio_override)
  1727. dmi_id = dmi_first_match(chip->fixes->cd_gpio_override);
  1728. if (!dmi_id)
  1729. return NULL;
  1730. cd_gpio_lookup_table = dmi_id->driver_data;
  1731. for (count = 0; cd_gpio_lookup_table->table[count].key; count++)
  1732. ;
  1733. cd_gpio_lookup_table = kmemdup(dmi_id->driver_data,
  1734. /* count + 1 terminating entry */
  1735. struct_size(cd_gpio_lookup_table, table, count + 1),
  1736. GFP_KERNEL);
  1737. if (!cd_gpio_lookup_table)
  1738. return ERR_PTR(-ENOMEM);
  1739. gpiod_add_lookup_table(cd_gpio_lookup_table);
  1740. return cd_gpio_lookup_table;
  1741. }
  1742. static void sdhci_pci_remove_gpio_lookup_table(struct gpiod_lookup_table *lookup_table)
  1743. {
  1744. if (lookup_table) {
  1745. gpiod_remove_lookup_table(lookup_table);
  1746. kfree(lookup_table);
  1747. }
  1748. }
  1749. static struct sdhci_pci_slot *sdhci_pci_probe_slot(
  1750. struct pci_dev *pdev, struct sdhci_pci_chip *chip, int first_bar,
  1751. int slotno)
  1752. {
  1753. struct sdhci_pci_slot *slot;
  1754. struct sdhci_host *host;
  1755. int ret, bar = first_bar + slotno;
  1756. size_t priv_size = chip->fixes ? chip->fixes->priv_size : 0;
  1757. if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
  1758. dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
  1759. return ERR_PTR(-ENODEV);
  1760. }
  1761. if (pci_resource_len(pdev, bar) < 0x100) {
  1762. dev_err(&pdev->dev, "Invalid iomem size. You may "
  1763. "experience problems.\n");
  1764. }
  1765. if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
  1766. dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
  1767. return ERR_PTR(-ENODEV);
  1768. }
  1769. if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
  1770. dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
  1771. return ERR_PTR(-ENODEV);
  1772. }
  1773. host = sdhci_alloc_host(&pdev->dev, sizeof(*slot) + priv_size);
  1774. if (IS_ERR(host)) {
  1775. dev_err(&pdev->dev, "cannot allocate host\n");
  1776. return ERR_CAST(host);
  1777. }
  1778. slot = sdhci_priv(host);
  1779. slot->chip = chip;
  1780. slot->host = host;
  1781. slot->cd_idx = -1;
  1782. host->hw_name = "PCI";
  1783. host->ops = chip->fixes && chip->fixes->ops ?
  1784. chip->fixes->ops :
  1785. &sdhci_pci_ops;
  1786. host->quirks = chip->quirks;
  1787. host->quirks2 = chip->quirks2;
  1788. host->irq = pdev->irq;
  1789. ret = pcim_iomap_regions(pdev, BIT(bar), mmc_hostname(host->mmc));
  1790. if (ret) {
  1791. dev_err(&pdev->dev, "cannot request region\n");
  1792. return ERR_PTR(ret);
  1793. }
  1794. host->ioaddr = pcim_iomap_table(pdev)[bar];
  1795. if (chip->fixes && chip->fixes->probe_slot) {
  1796. ret = chip->fixes->probe_slot(slot);
  1797. if (ret)
  1798. return ERR_PTR(ret);
  1799. }
  1800. host->mmc->pm_caps = MMC_PM_KEEP_POWER;
  1801. host->mmc->slotno = slotno;
  1802. host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
  1803. if (device_can_wakeup(&pdev->dev))
  1804. host->mmc->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
  1805. if (host->mmc->caps & MMC_CAP_CD_WAKE)
  1806. device_init_wakeup(&pdev->dev, true);
  1807. if (slot->cd_idx >= 0) {
  1808. struct gpiod_lookup_table *cd_gpio_lookup_table;
  1809. cd_gpio_lookup_table = sdhci_pci_add_gpio_lookup_table(chip);
  1810. if (IS_ERR(cd_gpio_lookup_table)) {
  1811. ret = PTR_ERR(cd_gpio_lookup_table);
  1812. goto remove;
  1813. }
  1814. ret = mmc_gpiod_request_cd(host->mmc, "cd", slot->cd_idx,
  1815. slot->cd_override_level, 0);
  1816. sdhci_pci_remove_gpio_lookup_table(cd_gpio_lookup_table);
  1817. if (ret && ret != -EPROBE_DEFER)
  1818. ret = mmc_gpiod_request_cd(host->mmc, NULL,
  1819. slot->cd_idx,
  1820. slot->cd_override_level,
  1821. 0);
  1822. if (ret == -EPROBE_DEFER)
  1823. goto remove;
  1824. if (ret) {
  1825. dev_warn(&pdev->dev, "failed to setup card detect gpio\n");
  1826. slot->cd_idx = -1;
  1827. }
  1828. }
  1829. if (chip->fixes && chip->fixes->add_host)
  1830. ret = chip->fixes->add_host(slot);
  1831. else
  1832. ret = sdhci_add_host(host);
  1833. if (ret)
  1834. goto remove;
  1835. /*
  1836. * Check if the chip needs a separate GPIO for card detect to wake up
  1837. * from runtime suspend. If it is not there, don't allow runtime PM.
  1838. */
  1839. if (chip->fixes && chip->fixes->own_cd_for_runtime_pm && slot->cd_idx < 0)
  1840. chip->allow_runtime_pm = false;
  1841. return slot;
  1842. remove:
  1843. if (chip->fixes && chip->fixes->remove_slot)
  1844. chip->fixes->remove_slot(slot, 0);
  1845. return ERR_PTR(ret);
  1846. }
  1847. static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
  1848. {
  1849. int dead;
  1850. u32 scratch;
  1851. dead = 0;
  1852. scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
  1853. if (scratch == (u32)-1)
  1854. dead = 1;
  1855. if (slot->chip->fixes && slot->chip->fixes->remove_host)
  1856. slot->chip->fixes->remove_host(slot, dead);
  1857. else
  1858. sdhci_remove_host(slot->host, dead);
  1859. if (slot->chip->fixes && slot->chip->fixes->remove_slot)
  1860. slot->chip->fixes->remove_slot(slot, dead);
  1861. }
  1862. int sdhci_pci_uhs2_add_host(struct sdhci_pci_slot *slot)
  1863. {
  1864. return sdhci_uhs2_add_host(slot->host);
  1865. }
  1866. void sdhci_pci_uhs2_remove_host(struct sdhci_pci_slot *slot, int dead)
  1867. {
  1868. sdhci_uhs2_remove_host(slot->host, dead);
  1869. }
  1870. static void sdhci_pci_runtime_pm_allow(struct device *dev)
  1871. {
  1872. pm_suspend_ignore_children(dev, 1);
  1873. pm_runtime_set_autosuspend_delay(dev, 50);
  1874. pm_runtime_use_autosuspend(dev);
  1875. pm_runtime_allow(dev);
  1876. /* Stay active until mmc core scans for a card */
  1877. pm_runtime_put_noidle(dev);
  1878. }
  1879. static void sdhci_pci_runtime_pm_forbid(struct device *dev)
  1880. {
  1881. pm_runtime_forbid(dev);
  1882. pm_runtime_get_noresume(dev);
  1883. }
  1884. static int sdhci_pci_probe(struct pci_dev *pdev,
  1885. const struct pci_device_id *ent)
  1886. {
  1887. struct sdhci_pci_chip *chip;
  1888. struct sdhci_pci_slot *slot;
  1889. u8 slots, first_bar;
  1890. int ret, i;
  1891. BUG_ON(pdev == NULL);
  1892. BUG_ON(ent == NULL);
  1893. dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
  1894. (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
  1895. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
  1896. if (ret)
  1897. return pcibios_err_to_errno(ret);
  1898. slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
  1899. dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
  1900. BUG_ON(slots > MAX_SLOTS);
  1901. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
  1902. if (ret)
  1903. return pcibios_err_to_errno(ret);
  1904. first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
  1905. if (first_bar > 5) {
  1906. dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
  1907. return -ENODEV;
  1908. }
  1909. ret = pcim_enable_device(pdev);
  1910. if (ret)
  1911. return ret;
  1912. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  1913. if (!chip)
  1914. return -ENOMEM;
  1915. chip->pdev = pdev;
  1916. chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
  1917. if (chip->fixes) {
  1918. chip->quirks = chip->fixes->quirks;
  1919. chip->quirks2 = chip->fixes->quirks2;
  1920. chip->allow_runtime_pm = chip->fixes->allow_runtime_pm;
  1921. }
  1922. chip->num_slots = slots;
  1923. chip->pm_retune = true;
  1924. chip->rpm_retune = true;
  1925. pci_set_drvdata(pdev, chip);
  1926. if (chip->fixes && chip->fixes->probe) {
  1927. ret = chip->fixes->probe(chip);
  1928. if (ret)
  1929. return ret;
  1930. }
  1931. slots = chip->num_slots; /* Quirk may have changed this */
  1932. for (i = 0; i < slots; i++) {
  1933. slot = sdhci_pci_probe_slot(pdev, chip, first_bar, i);
  1934. if (IS_ERR(slot)) {
  1935. for (i--; i >= 0; i--)
  1936. sdhci_pci_remove_slot(chip->slots[i]);
  1937. return PTR_ERR(slot);
  1938. }
  1939. chip->slots[i] = slot;
  1940. }
  1941. if (chip->allow_runtime_pm)
  1942. sdhci_pci_runtime_pm_allow(&pdev->dev);
  1943. return 0;
  1944. }
  1945. static void sdhci_pci_remove(struct pci_dev *pdev)
  1946. {
  1947. int i;
  1948. struct sdhci_pci_chip *chip = pci_get_drvdata(pdev);
  1949. if (chip->allow_runtime_pm)
  1950. sdhci_pci_runtime_pm_forbid(&pdev->dev);
  1951. for (i = 0; i < chip->num_slots; i++)
  1952. sdhci_pci_remove_slot(chip->slots[i]);
  1953. }
  1954. static struct pci_driver sdhci_driver = {
  1955. .name = "sdhci-pci",
  1956. .id_table = pci_ids,
  1957. .probe = sdhci_pci_probe,
  1958. .remove = sdhci_pci_remove,
  1959. .driver = {
  1960. .pm = &sdhci_pci_pm_ops,
  1961. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1962. },
  1963. };
  1964. module_pci_driver(sdhci_driver);
  1965. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  1966. MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
  1967. MODULE_LICENSE("GPL");