sdhci-pci-core.c 60 KB

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