sdhci-pci-core.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079
  1. /* linux/drivers/mmc/host/sdhci-pci.c - SDHCI on PCI bus interface
  2. *
  3. * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * Thanks to the following companies for their support:
  11. *
  12. * - JMicron (hardware and technical support)
  13. */
  14. #include <linux/bitfield.h>
  15. #include <linux/string.h>
  16. #include <linux/delay.h>
  17. #include <linux/highmem.h>
  18. #include <linux/module.h>
  19. #include <linux/pci.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/slab.h>
  22. #include <linux/device.h>
  23. #include <linux/mmc/host.h>
  24. #include <linux/mmc/mmc.h>
  25. #include <linux/scatterlist.h>
  26. #include <linux/io.h>
  27. #include <linux/gpio.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/mmc/slot-gpio.h>
  30. #include <linux/mmc/sdhci-pci-data.h>
  31. #include <linux/acpi.h>
  32. #include <linux/dmi.h>
  33. #include "cqhci.h"
  34. #include "sdhci.h"
  35. #include "sdhci-pci.h"
  36. static void sdhci_pci_hw_reset(struct sdhci_host *host);
  37. #ifdef CONFIG_PM_SLEEP
  38. static int sdhci_pci_init_wakeup(struct sdhci_pci_chip *chip)
  39. {
  40. mmc_pm_flag_t pm_flags = 0;
  41. bool cap_cd_wake = false;
  42. int i;
  43. for (i = 0; i < chip->num_slots; i++) {
  44. struct sdhci_pci_slot *slot = chip->slots[i];
  45. if (slot) {
  46. pm_flags |= slot->host->mmc->pm_flags;
  47. if (slot->host->mmc->caps & MMC_CAP_CD_WAKE)
  48. cap_cd_wake = true;
  49. }
  50. }
  51. if ((pm_flags & MMC_PM_KEEP_POWER) && (pm_flags & MMC_PM_WAKE_SDIO_IRQ))
  52. return device_wakeup_enable(&chip->pdev->dev);
  53. else if (!cap_cd_wake)
  54. return device_wakeup_disable(&chip->pdev->dev);
  55. return 0;
  56. }
  57. static int sdhci_pci_suspend_host(struct sdhci_pci_chip *chip)
  58. {
  59. int i, ret;
  60. sdhci_pci_init_wakeup(chip);
  61. for (i = 0; i < chip->num_slots; i++) {
  62. struct sdhci_pci_slot *slot = chip->slots[i];
  63. struct sdhci_host *host;
  64. if (!slot)
  65. continue;
  66. host = slot->host;
  67. if (chip->pm_retune && host->tuning_mode != SDHCI_TUNING_MODE_3)
  68. mmc_retune_needed(host->mmc);
  69. ret = sdhci_suspend_host(host);
  70. if (ret)
  71. goto err_pci_suspend;
  72. if (device_may_wakeup(&chip->pdev->dev))
  73. mmc_gpio_set_cd_wake(host->mmc, true);
  74. }
  75. return 0;
  76. err_pci_suspend:
  77. while (--i >= 0)
  78. sdhci_resume_host(chip->slots[i]->host);
  79. return ret;
  80. }
  81. int sdhci_pci_resume_host(struct sdhci_pci_chip *chip)
  82. {
  83. struct sdhci_pci_slot *slot;
  84. int i, ret;
  85. for (i = 0; i < chip->num_slots; i++) {
  86. slot = chip->slots[i];
  87. if (!slot)
  88. continue;
  89. ret = sdhci_resume_host(slot->host);
  90. if (ret)
  91. return ret;
  92. mmc_gpio_set_cd_wake(slot->host->mmc, false);
  93. }
  94. return 0;
  95. }
  96. static int sdhci_cqhci_suspend(struct sdhci_pci_chip *chip)
  97. {
  98. int ret;
  99. ret = cqhci_suspend(chip->slots[0]->host->mmc);
  100. if (ret)
  101. return ret;
  102. return sdhci_pci_suspend_host(chip);
  103. }
  104. static int sdhci_cqhci_resume(struct sdhci_pci_chip *chip)
  105. {
  106. int ret;
  107. ret = sdhci_pci_resume_host(chip);
  108. if (ret)
  109. return ret;
  110. return cqhci_resume(chip->slots[0]->host->mmc);
  111. }
  112. #endif
  113. #ifdef CONFIG_PM
  114. static int sdhci_pci_runtime_suspend_host(struct sdhci_pci_chip *chip)
  115. {
  116. struct sdhci_pci_slot *slot;
  117. struct sdhci_host *host;
  118. int i, ret;
  119. for (i = 0; i < chip->num_slots; i++) {
  120. slot = chip->slots[i];
  121. if (!slot)
  122. continue;
  123. host = slot->host;
  124. ret = sdhci_runtime_suspend_host(host);
  125. if (ret)
  126. goto err_pci_runtime_suspend;
  127. if (chip->rpm_retune &&
  128. host->tuning_mode != SDHCI_TUNING_MODE_3)
  129. mmc_retune_needed(host->mmc);
  130. }
  131. return 0;
  132. err_pci_runtime_suspend:
  133. while (--i >= 0)
  134. sdhci_runtime_resume_host(chip->slots[i]->host);
  135. return ret;
  136. }
  137. static int sdhci_pci_runtime_resume_host(struct sdhci_pci_chip *chip)
  138. {
  139. struct sdhci_pci_slot *slot;
  140. int i, ret;
  141. for (i = 0; i < chip->num_slots; i++) {
  142. slot = chip->slots[i];
  143. if (!slot)
  144. continue;
  145. ret = sdhci_runtime_resume_host(slot->host);
  146. if (ret)
  147. return ret;
  148. }
  149. return 0;
  150. }
  151. static int sdhci_cqhci_runtime_suspend(struct sdhci_pci_chip *chip)
  152. {
  153. int ret;
  154. ret = cqhci_suspend(chip->slots[0]->host->mmc);
  155. if (ret)
  156. return ret;
  157. return sdhci_pci_runtime_suspend_host(chip);
  158. }
  159. static int sdhci_cqhci_runtime_resume(struct sdhci_pci_chip *chip)
  160. {
  161. int ret;
  162. ret = sdhci_pci_runtime_resume_host(chip);
  163. if (ret)
  164. return ret;
  165. return cqhci_resume(chip->slots[0]->host->mmc);
  166. }
  167. #endif
  168. static u32 sdhci_cqhci_irq(struct sdhci_host *host, u32 intmask)
  169. {
  170. int cmd_error = 0;
  171. int data_error = 0;
  172. if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
  173. return intmask;
  174. cqhci_irq(host->mmc, intmask, cmd_error, data_error);
  175. return 0;
  176. }
  177. static void sdhci_pci_dumpregs(struct mmc_host *mmc)
  178. {
  179. sdhci_dumpregs(mmc_priv(mmc));
  180. }
  181. /*****************************************************************************\
  182. * *
  183. * Hardware specific quirk handling *
  184. * *
  185. \*****************************************************************************/
  186. static int ricoh_probe(struct sdhci_pci_chip *chip)
  187. {
  188. if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
  189. chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
  190. chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
  191. return 0;
  192. }
  193. static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
  194. {
  195. slot->host->caps =
  196. ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
  197. & SDHCI_TIMEOUT_CLK_MASK) |
  198. ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
  199. & SDHCI_CLOCK_BASE_MASK) |
  200. SDHCI_TIMEOUT_CLK_UNIT |
  201. SDHCI_CAN_VDD_330 |
  202. SDHCI_CAN_DO_HISPD |
  203. SDHCI_CAN_DO_SDMA;
  204. return 0;
  205. }
  206. #ifdef CONFIG_PM_SLEEP
  207. static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
  208. {
  209. /* Apply a delay to allow controller to settle */
  210. /* Otherwise it becomes confused if card state changed
  211. during suspend */
  212. msleep(500);
  213. return sdhci_pci_resume_host(chip);
  214. }
  215. #endif
  216. static const struct sdhci_pci_fixes sdhci_ricoh = {
  217. .probe = ricoh_probe,
  218. .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
  219. SDHCI_QUIRK_FORCE_DMA |
  220. SDHCI_QUIRK_CLOCK_BEFORE_RESET,
  221. };
  222. static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
  223. .probe_slot = ricoh_mmc_probe_slot,
  224. #ifdef CONFIG_PM_SLEEP
  225. .resume = ricoh_mmc_resume,
  226. #endif
  227. .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
  228. SDHCI_QUIRK_CLOCK_BEFORE_RESET |
  229. SDHCI_QUIRK_NO_CARD_NO_RESET |
  230. SDHCI_QUIRK_MISSING_CAPS
  231. };
  232. static const struct sdhci_pci_fixes sdhci_ene_712 = {
  233. .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
  234. SDHCI_QUIRK_BROKEN_DMA,
  235. };
  236. static const struct sdhci_pci_fixes sdhci_ene_714 = {
  237. .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
  238. SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
  239. SDHCI_QUIRK_BROKEN_DMA,
  240. };
  241. static const struct sdhci_pci_fixes sdhci_cafe = {
  242. .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
  243. SDHCI_QUIRK_NO_BUSY_IRQ |
  244. SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  245. SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
  246. };
  247. static const struct sdhci_pci_fixes sdhci_intel_qrk = {
  248. .quirks = SDHCI_QUIRK_NO_HISPD_BIT,
  249. };
  250. static int mrst_hc_probe_slot(struct sdhci_pci_slot *slot)
  251. {
  252. slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  253. return 0;
  254. }
  255. /*
  256. * ADMA operation is disabled for Moorestown platform due to
  257. * hardware bugs.
  258. */
  259. static int mrst_hc_probe(struct sdhci_pci_chip *chip)
  260. {
  261. /*
  262. * slots number is fixed here for MRST as SDIO3/5 are never used and
  263. * have hardware bugs.
  264. */
  265. chip->num_slots = 1;
  266. return 0;
  267. }
  268. static int pch_hc_probe_slot(struct sdhci_pci_slot *slot)
  269. {
  270. slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  271. return 0;
  272. }
  273. #ifdef CONFIG_PM
  274. static irqreturn_t sdhci_pci_sd_cd(int irq, void *dev_id)
  275. {
  276. struct sdhci_pci_slot *slot = dev_id;
  277. struct sdhci_host *host = slot->host;
  278. mmc_detect_change(host->mmc, msecs_to_jiffies(200));
  279. return IRQ_HANDLED;
  280. }
  281. static void sdhci_pci_add_own_cd(struct sdhci_pci_slot *slot)
  282. {
  283. int err, irq, gpio = slot->cd_gpio;
  284. slot->cd_gpio = -EINVAL;
  285. slot->cd_irq = -EINVAL;
  286. if (!gpio_is_valid(gpio))
  287. return;
  288. err = devm_gpio_request(&slot->chip->pdev->dev, gpio, "sd_cd");
  289. if (err < 0)
  290. goto out;
  291. err = gpio_direction_input(gpio);
  292. if (err < 0)
  293. goto out_free;
  294. irq = gpio_to_irq(gpio);
  295. if (irq < 0)
  296. goto out_free;
  297. err = request_irq(irq, sdhci_pci_sd_cd, IRQF_TRIGGER_RISING |
  298. IRQF_TRIGGER_FALLING, "sd_cd", slot);
  299. if (err)
  300. goto out_free;
  301. slot->cd_gpio = gpio;
  302. slot->cd_irq = irq;
  303. return;
  304. out_free:
  305. devm_gpio_free(&slot->chip->pdev->dev, gpio);
  306. out:
  307. dev_warn(&slot->chip->pdev->dev, "failed to setup card detect wake up\n");
  308. }
  309. static void sdhci_pci_remove_own_cd(struct sdhci_pci_slot *slot)
  310. {
  311. if (slot->cd_irq >= 0)
  312. free_irq(slot->cd_irq, slot);
  313. }
  314. #else
  315. static inline void sdhci_pci_add_own_cd(struct sdhci_pci_slot *slot)
  316. {
  317. }
  318. static inline void sdhci_pci_remove_own_cd(struct sdhci_pci_slot *slot)
  319. {
  320. }
  321. #endif
  322. static int mfd_emmc_probe_slot(struct sdhci_pci_slot *slot)
  323. {
  324. slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE;
  325. slot->host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC;
  326. return 0;
  327. }
  328. static int mfd_sdio_probe_slot(struct sdhci_pci_slot *slot)
  329. {
  330. slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE;
  331. return 0;
  332. }
  333. static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
  334. .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
  335. .probe_slot = mrst_hc_probe_slot,
  336. };
  337. static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1_hc2 = {
  338. .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
  339. .probe = mrst_hc_probe,
  340. };
  341. static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
  342. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  343. .allow_runtime_pm = true,
  344. .own_cd_for_runtime_pm = true,
  345. };
  346. static const struct sdhci_pci_fixes sdhci_intel_mfd_sdio = {
  347. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  348. .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
  349. .allow_runtime_pm = true,
  350. .probe_slot = mfd_sdio_probe_slot,
  351. };
  352. static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc = {
  353. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  354. .allow_runtime_pm = true,
  355. .probe_slot = mfd_emmc_probe_slot,
  356. };
  357. static const struct sdhci_pci_fixes sdhci_intel_pch_sdio = {
  358. .quirks = SDHCI_QUIRK_BROKEN_ADMA,
  359. .probe_slot = pch_hc_probe_slot,
  360. };
  361. enum {
  362. INTEL_DSM_FNS = 0,
  363. INTEL_DSM_V18_SWITCH = 3,
  364. INTEL_DSM_V33_SWITCH = 4,
  365. INTEL_DSM_DRV_STRENGTH = 9,
  366. INTEL_DSM_D3_RETUNE = 10,
  367. };
  368. struct intel_host {
  369. u32 dsm_fns;
  370. int drv_strength;
  371. bool d3_retune;
  372. bool rpm_retune_ok;
  373. bool needs_pwr_off;
  374. u32 glk_rx_ctrl1;
  375. u32 glk_tun_val;
  376. };
  377. static const guid_t intel_dsm_guid =
  378. GUID_INIT(0xF6C13EA5, 0x65CD, 0x461F,
  379. 0xAB, 0x7A, 0x29, 0xF7, 0xE8, 0xD5, 0xBD, 0x61);
  380. static int __intel_dsm(struct intel_host *intel_host, struct device *dev,
  381. unsigned int fn, u32 *result)
  382. {
  383. union acpi_object *obj;
  384. int err = 0;
  385. size_t len;
  386. obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &intel_dsm_guid, 0, fn, NULL);
  387. if (!obj)
  388. return -EOPNOTSUPP;
  389. if (obj->type != ACPI_TYPE_BUFFER || obj->buffer.length < 1) {
  390. err = -EINVAL;
  391. goto out;
  392. }
  393. len = min_t(size_t, obj->buffer.length, 4);
  394. *result = 0;
  395. memcpy(result, obj->buffer.pointer, len);
  396. out:
  397. ACPI_FREE(obj);
  398. return err;
  399. }
  400. static int intel_dsm(struct intel_host *intel_host, struct device *dev,
  401. unsigned int fn, u32 *result)
  402. {
  403. if (fn > 31 || !(intel_host->dsm_fns & (1 << fn)))
  404. return -EOPNOTSUPP;
  405. return __intel_dsm(intel_host, dev, fn, result);
  406. }
  407. static void intel_dsm_init(struct intel_host *intel_host, struct device *dev,
  408. struct mmc_host *mmc)
  409. {
  410. int err;
  411. u32 val;
  412. intel_host->d3_retune = true;
  413. err = __intel_dsm(intel_host, dev, INTEL_DSM_FNS, &intel_host->dsm_fns);
  414. if (err) {
  415. pr_debug("%s: DSM not supported, error %d\n",
  416. mmc_hostname(mmc), err);
  417. return;
  418. }
  419. pr_debug("%s: DSM function mask %#x\n",
  420. mmc_hostname(mmc), intel_host->dsm_fns);
  421. err = intel_dsm(intel_host, dev, INTEL_DSM_DRV_STRENGTH, &val);
  422. intel_host->drv_strength = err ? 0 : val;
  423. err = intel_dsm(intel_host, dev, INTEL_DSM_D3_RETUNE, &val);
  424. intel_host->d3_retune = err ? true : !!val;
  425. }
  426. static void sdhci_pci_int_hw_reset(struct sdhci_host *host)
  427. {
  428. u8 reg;
  429. reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
  430. reg |= 0x10;
  431. sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
  432. /* For eMMC, minimum is 1us but give it 9us for good measure */
  433. udelay(9);
  434. reg &= ~0x10;
  435. sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
  436. /* For eMMC, minimum is 200us but give it 300us for good measure */
  437. usleep_range(300, 1000);
  438. }
  439. static int intel_select_drive_strength(struct mmc_card *card,
  440. unsigned int max_dtr, int host_drv,
  441. int card_drv, int *drv_type)
  442. {
  443. struct sdhci_host *host = mmc_priv(card->host);
  444. struct sdhci_pci_slot *slot = sdhci_priv(host);
  445. struct intel_host *intel_host = sdhci_pci_priv(slot);
  446. if (!(mmc_driver_type_mask(intel_host->drv_strength) & card_drv))
  447. return 0;
  448. return intel_host->drv_strength;
  449. }
  450. static int bxt_get_cd(struct mmc_host *mmc)
  451. {
  452. int gpio_cd = mmc_gpio_get_cd(mmc);
  453. struct sdhci_host *host = mmc_priv(mmc);
  454. unsigned long flags;
  455. int ret = 0;
  456. if (!gpio_cd)
  457. return 0;
  458. spin_lock_irqsave(&host->lock, flags);
  459. if (host->flags & SDHCI_DEVICE_DEAD)
  460. goto out;
  461. ret = !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
  462. out:
  463. spin_unlock_irqrestore(&host->lock, flags);
  464. return ret;
  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. #define INTEL_HS400_ES_REG 0x78
  505. #define INTEL_HS400_ES_BIT BIT(0)
  506. static void intel_hs400_enhanced_strobe(struct mmc_host *mmc,
  507. struct mmc_ios *ios)
  508. {
  509. struct sdhci_host *host = mmc_priv(mmc);
  510. u32 val;
  511. val = sdhci_readl(host, INTEL_HS400_ES_REG);
  512. if (ios->enhanced_strobe)
  513. val |= INTEL_HS400_ES_BIT;
  514. else
  515. val &= ~INTEL_HS400_ES_BIT;
  516. sdhci_writel(host, val, INTEL_HS400_ES_REG);
  517. }
  518. static int intel_start_signal_voltage_switch(struct mmc_host *mmc,
  519. struct mmc_ios *ios)
  520. {
  521. struct device *dev = mmc_dev(mmc);
  522. struct sdhci_host *host = mmc_priv(mmc);
  523. struct sdhci_pci_slot *slot = sdhci_priv(host);
  524. struct intel_host *intel_host = sdhci_pci_priv(slot);
  525. unsigned int fn;
  526. u32 result = 0;
  527. int err;
  528. err = sdhci_start_signal_voltage_switch(mmc, ios);
  529. if (err)
  530. return err;
  531. switch (ios->signal_voltage) {
  532. case MMC_SIGNAL_VOLTAGE_330:
  533. fn = INTEL_DSM_V33_SWITCH;
  534. break;
  535. case MMC_SIGNAL_VOLTAGE_180:
  536. fn = INTEL_DSM_V18_SWITCH;
  537. break;
  538. default:
  539. return 0;
  540. }
  541. err = intel_dsm(intel_host, dev, fn, &result);
  542. pr_debug("%s: %s DSM fn %u error %d result %u\n",
  543. mmc_hostname(mmc), __func__, fn, err, result);
  544. return 0;
  545. }
  546. static const struct sdhci_ops sdhci_intel_byt_ops = {
  547. .set_clock = sdhci_set_clock,
  548. .set_power = sdhci_intel_set_power,
  549. .enable_dma = sdhci_pci_enable_dma,
  550. .set_bus_width = sdhci_set_bus_width,
  551. .reset = sdhci_reset,
  552. .set_uhs_signaling = sdhci_set_uhs_signaling,
  553. .hw_reset = sdhci_pci_hw_reset,
  554. };
  555. static const struct sdhci_ops sdhci_intel_glk_ops = {
  556. .set_clock = sdhci_set_clock,
  557. .set_power = sdhci_intel_set_power,
  558. .enable_dma = sdhci_pci_enable_dma,
  559. .set_bus_width = sdhci_set_bus_width,
  560. .reset = sdhci_reset,
  561. .set_uhs_signaling = sdhci_set_uhs_signaling,
  562. .hw_reset = sdhci_pci_hw_reset,
  563. .irq = sdhci_cqhci_irq,
  564. };
  565. static void byt_read_dsm(struct sdhci_pci_slot *slot)
  566. {
  567. struct intel_host *intel_host = sdhci_pci_priv(slot);
  568. struct device *dev = &slot->chip->pdev->dev;
  569. struct mmc_host *mmc = slot->host->mmc;
  570. intel_dsm_init(intel_host, dev, mmc);
  571. slot->chip->rpm_retune = intel_host->d3_retune;
  572. }
  573. static int intel_execute_tuning(struct mmc_host *mmc, u32 opcode)
  574. {
  575. int err = sdhci_execute_tuning(mmc, opcode);
  576. struct sdhci_host *host = mmc_priv(mmc);
  577. if (err)
  578. return err;
  579. /*
  580. * Tuning can leave the IP in an active state (Buffer Read Enable bit
  581. * set) which prevents the entry to low power states (i.e. S0i3). Data
  582. * reset will clear it.
  583. */
  584. sdhci_reset(host, SDHCI_RESET_DATA);
  585. return 0;
  586. }
  587. static void byt_probe_slot(struct sdhci_pci_slot *slot)
  588. {
  589. struct mmc_host_ops *ops = &slot->host->mmc_host_ops;
  590. byt_read_dsm(slot);
  591. ops->execute_tuning = intel_execute_tuning;
  592. ops->start_signal_voltage_switch = intel_start_signal_voltage_switch;
  593. }
  594. static int byt_emmc_probe_slot(struct sdhci_pci_slot *slot)
  595. {
  596. byt_probe_slot(slot);
  597. slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
  598. MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR |
  599. MMC_CAP_CMD_DURING_TFR |
  600. MMC_CAP_WAIT_WHILE_BUSY;
  601. slot->hw_reset = sdhci_pci_int_hw_reset;
  602. if (slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_BSW_EMMC)
  603. slot->host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */
  604. slot->host->mmc_host_ops.select_drive_strength =
  605. intel_select_drive_strength;
  606. return 0;
  607. }
  608. static bool glk_broken_cqhci(struct sdhci_pci_slot *slot)
  609. {
  610. return slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_GLK_EMMC &&
  611. (dmi_match(DMI_BIOS_VENDOR, "LENOVO") ||
  612. dmi_match(DMI_SYS_VENDOR, "IRBIS"));
  613. }
  614. static int glk_emmc_probe_slot(struct sdhci_pci_slot *slot)
  615. {
  616. int ret = byt_emmc_probe_slot(slot);
  617. if (!glk_broken_cqhci(slot))
  618. slot->host->mmc->caps2 |= MMC_CAP2_CQE;
  619. if (slot->chip->pdev->device != PCI_DEVICE_ID_INTEL_GLK_EMMC) {
  620. slot->host->mmc->caps2 |= MMC_CAP2_HS400_ES,
  621. slot->host->mmc_host_ops.hs400_enhanced_strobe =
  622. intel_hs400_enhanced_strobe;
  623. slot->host->mmc->caps2 |= MMC_CAP2_CQE_DCMD;
  624. }
  625. return ret;
  626. }
  627. static const struct cqhci_host_ops glk_cqhci_ops = {
  628. .enable = sdhci_cqe_enable,
  629. .disable = sdhci_cqe_disable,
  630. .dumpregs = sdhci_pci_dumpregs,
  631. };
  632. static int glk_emmc_add_host(struct sdhci_pci_slot *slot)
  633. {
  634. struct device *dev = &slot->chip->pdev->dev;
  635. struct sdhci_host *host = slot->host;
  636. struct cqhci_host *cq_host;
  637. bool dma64;
  638. int ret;
  639. ret = sdhci_setup_host(host);
  640. if (ret)
  641. return ret;
  642. cq_host = devm_kzalloc(dev, sizeof(*cq_host), GFP_KERNEL);
  643. if (!cq_host) {
  644. ret = -ENOMEM;
  645. goto cleanup;
  646. }
  647. cq_host->mmio = host->ioaddr + 0x200;
  648. cq_host->quirks |= CQHCI_QUIRK_SHORT_TXFR_DESC_SZ;
  649. cq_host->ops = &glk_cqhci_ops;
  650. dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
  651. if (dma64)
  652. cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
  653. ret = cqhci_init(cq_host, host->mmc, dma64);
  654. if (ret)
  655. goto cleanup;
  656. ret = __sdhci_add_host(host);
  657. if (ret)
  658. goto cleanup;
  659. return 0;
  660. cleanup:
  661. sdhci_cleanup_host(host);
  662. return ret;
  663. }
  664. #ifdef CONFIG_PM
  665. #define GLK_RX_CTRL1 0x834
  666. #define GLK_TUN_VAL 0x840
  667. #define GLK_PATH_PLL GENMASK(13, 8)
  668. #define GLK_DLY GENMASK(6, 0)
  669. /* Workaround firmware failing to restore the tuning value */
  670. static void glk_rpm_retune_wa(struct sdhci_pci_chip *chip, bool susp)
  671. {
  672. struct sdhci_pci_slot *slot = chip->slots[0];
  673. struct intel_host *intel_host = sdhci_pci_priv(slot);
  674. struct sdhci_host *host = slot->host;
  675. u32 glk_rx_ctrl1;
  676. u32 glk_tun_val;
  677. u32 dly;
  678. if (intel_host->rpm_retune_ok || !mmc_can_retune(host->mmc))
  679. return;
  680. glk_rx_ctrl1 = sdhci_readl(host, GLK_RX_CTRL1);
  681. glk_tun_val = sdhci_readl(host, GLK_TUN_VAL);
  682. if (susp) {
  683. intel_host->glk_rx_ctrl1 = glk_rx_ctrl1;
  684. intel_host->glk_tun_val = glk_tun_val;
  685. return;
  686. }
  687. if (!intel_host->glk_tun_val)
  688. return;
  689. if (glk_rx_ctrl1 != intel_host->glk_rx_ctrl1) {
  690. intel_host->rpm_retune_ok = true;
  691. return;
  692. }
  693. dly = FIELD_PREP(GLK_DLY, FIELD_GET(GLK_PATH_PLL, glk_rx_ctrl1) +
  694. (intel_host->glk_tun_val << 1));
  695. if (dly == FIELD_GET(GLK_DLY, glk_rx_ctrl1))
  696. return;
  697. glk_rx_ctrl1 = (glk_rx_ctrl1 & ~GLK_DLY) | dly;
  698. sdhci_writel(host, glk_rx_ctrl1, GLK_RX_CTRL1);
  699. intel_host->rpm_retune_ok = true;
  700. chip->rpm_retune = true;
  701. mmc_retune_needed(host->mmc);
  702. pr_info("%s: Requiring re-tune after rpm resume", mmc_hostname(host->mmc));
  703. }
  704. static void glk_rpm_retune_chk(struct sdhci_pci_chip *chip, bool susp)
  705. {
  706. if (chip->pdev->device == PCI_DEVICE_ID_INTEL_GLK_EMMC &&
  707. !chip->rpm_retune)
  708. glk_rpm_retune_wa(chip, susp);
  709. }
  710. static int glk_runtime_suspend(struct sdhci_pci_chip *chip)
  711. {
  712. glk_rpm_retune_chk(chip, true);
  713. return sdhci_cqhci_runtime_suspend(chip);
  714. }
  715. static int glk_runtime_resume(struct sdhci_pci_chip *chip)
  716. {
  717. glk_rpm_retune_chk(chip, false);
  718. return sdhci_cqhci_runtime_resume(chip);
  719. }
  720. #endif
  721. #ifdef CONFIG_ACPI
  722. static int ni_set_max_freq(struct sdhci_pci_slot *slot)
  723. {
  724. acpi_status status;
  725. unsigned long long max_freq;
  726. status = acpi_evaluate_integer(ACPI_HANDLE(&slot->chip->pdev->dev),
  727. "MXFQ", NULL, &max_freq);
  728. if (ACPI_FAILURE(status)) {
  729. dev_err(&slot->chip->pdev->dev,
  730. "MXFQ not found in acpi table\n");
  731. return -EINVAL;
  732. }
  733. slot->host->mmc->f_max = max_freq * 1000000;
  734. return 0;
  735. }
  736. #else
  737. static inline int ni_set_max_freq(struct sdhci_pci_slot *slot)
  738. {
  739. return 0;
  740. }
  741. #endif
  742. static int ni_byt_sdio_probe_slot(struct sdhci_pci_slot *slot)
  743. {
  744. int err;
  745. byt_probe_slot(slot);
  746. err = ni_set_max_freq(slot);
  747. if (err)
  748. return err;
  749. slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE |
  750. MMC_CAP_WAIT_WHILE_BUSY;
  751. return 0;
  752. }
  753. static int byt_sdio_probe_slot(struct sdhci_pci_slot *slot)
  754. {
  755. byt_probe_slot(slot);
  756. slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE |
  757. MMC_CAP_WAIT_WHILE_BUSY;
  758. return 0;
  759. }
  760. static void byt_needs_pwr_off(struct sdhci_pci_slot *slot)
  761. {
  762. struct intel_host *intel_host = sdhci_pci_priv(slot);
  763. u8 reg = sdhci_readb(slot->host, SDHCI_POWER_CONTROL);
  764. intel_host->needs_pwr_off = reg & SDHCI_POWER_ON;
  765. }
  766. static int byt_sd_probe_slot(struct sdhci_pci_slot *slot)
  767. {
  768. byt_probe_slot(slot);
  769. slot->host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY |
  770. MMC_CAP_AGGRESSIVE_PM | MMC_CAP_CD_WAKE;
  771. slot->cd_idx = 0;
  772. slot->cd_override_level = true;
  773. if (slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_BXT_SD ||
  774. slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_BXTM_SD ||
  775. slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_APL_SD ||
  776. slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_GLK_SD)
  777. slot->host->mmc_host_ops.get_cd = bxt_get_cd;
  778. if (slot->chip->pdev->subsystem_vendor == PCI_VENDOR_ID_NI &&
  779. slot->chip->pdev->subsystem_device == PCI_SUBDEVICE_ID_NI_78E3)
  780. slot->host->mmc->caps2 |= MMC_CAP2_AVOID_3_3V;
  781. byt_needs_pwr_off(slot);
  782. return 0;
  783. }
  784. static const struct sdhci_pci_fixes sdhci_intel_byt_emmc = {
  785. .allow_runtime_pm = true,
  786. .probe_slot = byt_emmc_probe_slot,
  787. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  788. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  789. SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 |
  790. SDHCI_QUIRK2_STOP_WITH_TC,
  791. .ops = &sdhci_intel_byt_ops,
  792. .priv_size = sizeof(struct intel_host),
  793. };
  794. static const struct sdhci_pci_fixes sdhci_intel_glk_emmc = {
  795. .allow_runtime_pm = true,
  796. .probe_slot = glk_emmc_probe_slot,
  797. .add_host = glk_emmc_add_host,
  798. #ifdef CONFIG_PM_SLEEP
  799. .suspend = sdhci_cqhci_suspend,
  800. .resume = sdhci_cqhci_resume,
  801. #endif
  802. #ifdef CONFIG_PM
  803. .runtime_suspend = glk_runtime_suspend,
  804. .runtime_resume = glk_runtime_resume,
  805. #endif
  806. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  807. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  808. SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 |
  809. SDHCI_QUIRK2_STOP_WITH_TC,
  810. .ops = &sdhci_intel_glk_ops,
  811. .priv_size = sizeof(struct intel_host),
  812. };
  813. static const struct sdhci_pci_fixes sdhci_ni_byt_sdio = {
  814. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  815. .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON |
  816. SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
  817. .allow_runtime_pm = true,
  818. .probe_slot = ni_byt_sdio_probe_slot,
  819. .ops = &sdhci_intel_byt_ops,
  820. .priv_size = sizeof(struct intel_host),
  821. };
  822. static const struct sdhci_pci_fixes sdhci_intel_byt_sdio = {
  823. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  824. .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON |
  825. SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
  826. .allow_runtime_pm = true,
  827. .probe_slot = byt_sdio_probe_slot,
  828. .ops = &sdhci_intel_byt_ops,
  829. .priv_size = sizeof(struct intel_host),
  830. };
  831. static const struct sdhci_pci_fixes sdhci_intel_byt_sd = {
  832. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  833. .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
  834. SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  835. SDHCI_QUIRK2_STOP_WITH_TC,
  836. .allow_runtime_pm = true,
  837. .own_cd_for_runtime_pm = true,
  838. .probe_slot = byt_sd_probe_slot,
  839. .ops = &sdhci_intel_byt_ops,
  840. .priv_size = sizeof(struct intel_host),
  841. };
  842. /* Define Host controllers for Intel Merrifield platform */
  843. #define INTEL_MRFLD_EMMC_0 0
  844. #define INTEL_MRFLD_EMMC_1 1
  845. #define INTEL_MRFLD_SD 2
  846. #define INTEL_MRFLD_SDIO 3
  847. #ifdef CONFIG_ACPI
  848. static void intel_mrfld_mmc_fix_up_power_slot(struct sdhci_pci_slot *slot)
  849. {
  850. struct acpi_device *device, *child;
  851. device = ACPI_COMPANION(&slot->chip->pdev->dev);
  852. if (!device)
  853. return;
  854. acpi_device_fix_up_power(device);
  855. list_for_each_entry(child, &device->children, node)
  856. if (child->status.present && child->status.enabled)
  857. acpi_device_fix_up_power(child);
  858. }
  859. #else
  860. static inline void intel_mrfld_mmc_fix_up_power_slot(struct sdhci_pci_slot *slot) {}
  861. #endif
  862. static int intel_mrfld_mmc_probe_slot(struct sdhci_pci_slot *slot)
  863. {
  864. unsigned int func = PCI_FUNC(slot->chip->pdev->devfn);
  865. switch (func) {
  866. case INTEL_MRFLD_EMMC_0:
  867. case INTEL_MRFLD_EMMC_1:
  868. slot->host->mmc->caps |= MMC_CAP_NONREMOVABLE |
  869. MMC_CAP_8_BIT_DATA |
  870. MMC_CAP_1_8V_DDR;
  871. break;
  872. case INTEL_MRFLD_SD:
  873. slot->host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
  874. break;
  875. case INTEL_MRFLD_SDIO:
  876. /* Advertise 2.0v for compatibility with the SDIO card's OCR */
  877. slot->host->ocr_mask = MMC_VDD_20_21 | MMC_VDD_165_195;
  878. slot->host->mmc->caps |= MMC_CAP_NONREMOVABLE |
  879. MMC_CAP_POWER_OFF_CARD;
  880. break;
  881. default:
  882. return -ENODEV;
  883. }
  884. intel_mrfld_mmc_fix_up_power_slot(slot);
  885. return 0;
  886. }
  887. static const struct sdhci_pci_fixes sdhci_intel_mrfld_mmc = {
  888. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  889. .quirks2 = SDHCI_QUIRK2_BROKEN_HS200 |
  890. SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
  891. .allow_runtime_pm = true,
  892. .probe_slot = intel_mrfld_mmc_probe_slot,
  893. };
  894. static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
  895. {
  896. u8 scratch;
  897. int ret;
  898. ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
  899. if (ret)
  900. return ret;
  901. /*
  902. * Turn PMOS on [bit 0], set over current detection to 2.4 V
  903. * [bit 1:2] and enable over current debouncing [bit 6].
  904. */
  905. if (on)
  906. scratch |= 0x47;
  907. else
  908. scratch &= ~0x47;
  909. return pci_write_config_byte(chip->pdev, 0xAE, scratch);
  910. }
  911. static int jmicron_probe(struct sdhci_pci_chip *chip)
  912. {
  913. int ret;
  914. u16 mmcdev = 0;
  915. if (chip->pdev->revision == 0) {
  916. chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
  917. SDHCI_QUIRK_32BIT_DMA_SIZE |
  918. SDHCI_QUIRK_32BIT_ADMA_SIZE |
  919. SDHCI_QUIRK_RESET_AFTER_REQUEST |
  920. SDHCI_QUIRK_BROKEN_SMALL_PIO;
  921. }
  922. /*
  923. * JMicron chips can have two interfaces to the same hardware
  924. * in order to work around limitations in Microsoft's driver.
  925. * We need to make sure we only bind to one of them.
  926. *
  927. * This code assumes two things:
  928. *
  929. * 1. The PCI code adds subfunctions in order.
  930. *
  931. * 2. The MMC interface has a lower subfunction number
  932. * than the SD interface.
  933. */
  934. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
  935. mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
  936. else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
  937. mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
  938. if (mmcdev) {
  939. struct pci_dev *sd_dev;
  940. sd_dev = NULL;
  941. while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
  942. mmcdev, sd_dev)) != NULL) {
  943. if ((PCI_SLOT(chip->pdev->devfn) ==
  944. PCI_SLOT(sd_dev->devfn)) &&
  945. (chip->pdev->bus == sd_dev->bus))
  946. break;
  947. }
  948. if (sd_dev) {
  949. pci_dev_put(sd_dev);
  950. dev_info(&chip->pdev->dev, "Refusing to bind to "
  951. "secondary interface.\n");
  952. return -ENODEV;
  953. }
  954. }
  955. /*
  956. * JMicron chips need a bit of a nudge to enable the power
  957. * output pins.
  958. */
  959. ret = jmicron_pmos(chip, 1);
  960. if (ret) {
  961. dev_err(&chip->pdev->dev, "Failure enabling card power\n");
  962. return ret;
  963. }
  964. /* quirk for unsable RO-detection on JM388 chips */
  965. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
  966. chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
  967. chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
  968. return 0;
  969. }
  970. static void jmicron_enable_mmc(struct sdhci_host *host, int on)
  971. {
  972. u8 scratch;
  973. scratch = readb(host->ioaddr + 0xC0);
  974. if (on)
  975. scratch |= 0x01;
  976. else
  977. scratch &= ~0x01;
  978. writeb(scratch, host->ioaddr + 0xC0);
  979. }
  980. static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
  981. {
  982. if (slot->chip->pdev->revision == 0) {
  983. u16 version;
  984. version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
  985. version = (version & SDHCI_VENDOR_VER_MASK) >>
  986. SDHCI_VENDOR_VER_SHIFT;
  987. /*
  988. * Older versions of the chip have lots of nasty glitches
  989. * in the ADMA engine. It's best just to avoid it
  990. * completely.
  991. */
  992. if (version < 0xAC)
  993. slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
  994. }
  995. /* JM388 MMC doesn't support 1.8V while SD supports it */
  996. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
  997. slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
  998. MMC_VDD_29_30 | MMC_VDD_30_31 |
  999. MMC_VDD_165_195; /* allow 1.8V */
  1000. slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
  1001. MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
  1002. }
  1003. /*
  1004. * The secondary interface requires a bit set to get the
  1005. * interrupts.
  1006. */
  1007. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  1008. slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
  1009. jmicron_enable_mmc(slot->host, 1);
  1010. slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
  1011. return 0;
  1012. }
  1013. static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
  1014. {
  1015. if (dead)
  1016. return;
  1017. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  1018. slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
  1019. jmicron_enable_mmc(slot->host, 0);
  1020. }
  1021. #ifdef CONFIG_PM_SLEEP
  1022. static int jmicron_suspend(struct sdhci_pci_chip *chip)
  1023. {
  1024. int i, ret;
  1025. ret = sdhci_pci_suspend_host(chip);
  1026. if (ret)
  1027. return ret;
  1028. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  1029. chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
  1030. for (i = 0; i < chip->num_slots; i++)
  1031. jmicron_enable_mmc(chip->slots[i]->host, 0);
  1032. }
  1033. return 0;
  1034. }
  1035. static int jmicron_resume(struct sdhci_pci_chip *chip)
  1036. {
  1037. int ret, i;
  1038. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  1039. chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
  1040. for (i = 0; i < chip->num_slots; i++)
  1041. jmicron_enable_mmc(chip->slots[i]->host, 1);
  1042. }
  1043. ret = jmicron_pmos(chip, 1);
  1044. if (ret) {
  1045. dev_err(&chip->pdev->dev, "Failure enabling card power\n");
  1046. return ret;
  1047. }
  1048. return sdhci_pci_resume_host(chip);
  1049. }
  1050. #endif
  1051. static const struct sdhci_pci_fixes sdhci_o2 = {
  1052. .probe = sdhci_pci_o2_probe,
  1053. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  1054. .quirks2 = SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD,
  1055. .probe_slot = sdhci_pci_o2_probe_slot,
  1056. #ifdef CONFIG_PM_SLEEP
  1057. .resume = sdhci_pci_o2_resume,
  1058. #endif
  1059. };
  1060. static const struct sdhci_pci_fixes sdhci_jmicron = {
  1061. .probe = jmicron_probe,
  1062. .probe_slot = jmicron_probe_slot,
  1063. .remove_slot = jmicron_remove_slot,
  1064. #ifdef CONFIG_PM_SLEEP
  1065. .suspend = jmicron_suspend,
  1066. .resume = jmicron_resume,
  1067. #endif
  1068. };
  1069. /* SysKonnect CardBus2SDIO extra registers */
  1070. #define SYSKT_CTRL 0x200
  1071. #define SYSKT_RDFIFO_STAT 0x204
  1072. #define SYSKT_WRFIFO_STAT 0x208
  1073. #define SYSKT_POWER_DATA 0x20c
  1074. #define SYSKT_POWER_330 0xef
  1075. #define SYSKT_POWER_300 0xf8
  1076. #define SYSKT_POWER_184 0xcc
  1077. #define SYSKT_POWER_CMD 0x20d
  1078. #define SYSKT_POWER_START (1 << 7)
  1079. #define SYSKT_POWER_STATUS 0x20e
  1080. #define SYSKT_POWER_STATUS_OK (1 << 0)
  1081. #define SYSKT_BOARD_REV 0x210
  1082. #define SYSKT_CHIP_REV 0x211
  1083. #define SYSKT_CONF_DATA 0x212
  1084. #define SYSKT_CONF_DATA_1V8 (1 << 2)
  1085. #define SYSKT_CONF_DATA_2V5 (1 << 1)
  1086. #define SYSKT_CONF_DATA_3V3 (1 << 0)
  1087. static int syskt_probe(struct sdhci_pci_chip *chip)
  1088. {
  1089. if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
  1090. chip->pdev->class &= ~0x0000FF;
  1091. chip->pdev->class |= PCI_SDHCI_IFDMA;
  1092. }
  1093. return 0;
  1094. }
  1095. static int syskt_probe_slot(struct sdhci_pci_slot *slot)
  1096. {
  1097. int tm, ps;
  1098. u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
  1099. u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
  1100. dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
  1101. "board rev %d.%d, chip rev %d.%d\n",
  1102. board_rev >> 4, board_rev & 0xf,
  1103. chip_rev >> 4, chip_rev & 0xf);
  1104. if (chip_rev >= 0x20)
  1105. slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
  1106. writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
  1107. writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
  1108. udelay(50);
  1109. tm = 10; /* Wait max 1 ms */
  1110. do {
  1111. ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
  1112. if (ps & SYSKT_POWER_STATUS_OK)
  1113. break;
  1114. udelay(100);
  1115. } while (--tm);
  1116. if (!tm) {
  1117. dev_err(&slot->chip->pdev->dev,
  1118. "power regulator never stabilized");
  1119. writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
  1120. return -ENODEV;
  1121. }
  1122. return 0;
  1123. }
  1124. static const struct sdhci_pci_fixes sdhci_syskt = {
  1125. .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
  1126. .probe = syskt_probe,
  1127. .probe_slot = syskt_probe_slot,
  1128. };
  1129. static int via_probe(struct sdhci_pci_chip *chip)
  1130. {
  1131. if (chip->pdev->revision == 0x10)
  1132. chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
  1133. return 0;
  1134. }
  1135. static const struct sdhci_pci_fixes sdhci_via = {
  1136. .probe = via_probe,
  1137. };
  1138. static int rtsx_probe_slot(struct sdhci_pci_slot *slot)
  1139. {
  1140. slot->host->mmc->caps2 |= MMC_CAP2_HS200;
  1141. return 0;
  1142. }
  1143. static const struct sdhci_pci_fixes sdhci_rtsx = {
  1144. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  1145. SDHCI_QUIRK2_BROKEN_64_BIT_DMA |
  1146. SDHCI_QUIRK2_BROKEN_DDR50,
  1147. .probe_slot = rtsx_probe_slot,
  1148. };
  1149. /*AMD chipset generation*/
  1150. enum amd_chipset_gen {
  1151. AMD_CHIPSET_BEFORE_ML,
  1152. AMD_CHIPSET_CZ,
  1153. AMD_CHIPSET_NL,
  1154. AMD_CHIPSET_UNKNOWN,
  1155. };
  1156. /* AMD registers */
  1157. #define AMD_SD_AUTO_PATTERN 0xB8
  1158. #define AMD_MSLEEP_DURATION 4
  1159. #define AMD_SD_MISC_CONTROL 0xD0
  1160. #define AMD_MAX_TUNE_VALUE 0x0B
  1161. #define AMD_AUTO_TUNE_SEL 0x10800
  1162. #define AMD_FIFO_PTR 0x30
  1163. #define AMD_BIT_MASK 0x1F
  1164. static void amd_tuning_reset(struct sdhci_host *host)
  1165. {
  1166. unsigned int val;
  1167. val = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1168. val |= SDHCI_CTRL_PRESET_VAL_ENABLE | SDHCI_CTRL_EXEC_TUNING;
  1169. sdhci_writew(host, val, SDHCI_HOST_CONTROL2);
  1170. val = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1171. val &= ~SDHCI_CTRL_EXEC_TUNING;
  1172. sdhci_writew(host, val, SDHCI_HOST_CONTROL2);
  1173. }
  1174. static void amd_config_tuning_phase(struct pci_dev *pdev, u8 phase)
  1175. {
  1176. unsigned int val;
  1177. pci_read_config_dword(pdev, AMD_SD_AUTO_PATTERN, &val);
  1178. val &= ~AMD_BIT_MASK;
  1179. val |= (AMD_AUTO_TUNE_SEL | (phase << 1));
  1180. pci_write_config_dword(pdev, AMD_SD_AUTO_PATTERN, val);
  1181. }
  1182. static void amd_enable_manual_tuning(struct pci_dev *pdev)
  1183. {
  1184. unsigned int val;
  1185. pci_read_config_dword(pdev, AMD_SD_MISC_CONTROL, &val);
  1186. val |= AMD_FIFO_PTR;
  1187. pci_write_config_dword(pdev, AMD_SD_MISC_CONTROL, val);
  1188. }
  1189. static int amd_execute_tuning_hs200(struct sdhci_host *host, u32 opcode)
  1190. {
  1191. struct sdhci_pci_slot *slot = sdhci_priv(host);
  1192. struct pci_dev *pdev = slot->chip->pdev;
  1193. u8 valid_win = 0;
  1194. u8 valid_win_max = 0;
  1195. u8 valid_win_end = 0;
  1196. u8 ctrl, tune_around;
  1197. amd_tuning_reset(host);
  1198. for (tune_around = 0; tune_around < 12; tune_around++) {
  1199. amd_config_tuning_phase(pdev, tune_around);
  1200. if (mmc_send_tuning(host->mmc, opcode, NULL)) {
  1201. valid_win = 0;
  1202. msleep(AMD_MSLEEP_DURATION);
  1203. ctrl = SDHCI_RESET_CMD | SDHCI_RESET_DATA;
  1204. sdhci_writeb(host, ctrl, SDHCI_SOFTWARE_RESET);
  1205. } else if (++valid_win > valid_win_max) {
  1206. valid_win_max = valid_win;
  1207. valid_win_end = tune_around;
  1208. }
  1209. }
  1210. if (!valid_win_max) {
  1211. dev_err(&pdev->dev, "no tuning point found\n");
  1212. return -EIO;
  1213. }
  1214. amd_config_tuning_phase(pdev, valid_win_end - valid_win_max / 2);
  1215. amd_enable_manual_tuning(pdev);
  1216. host->mmc->retune_period = 0;
  1217. return 0;
  1218. }
  1219. static int amd_execute_tuning(struct mmc_host *mmc, u32 opcode)
  1220. {
  1221. struct sdhci_host *host = mmc_priv(mmc);
  1222. /* AMD requires custom HS200 tuning */
  1223. if (host->timing == MMC_TIMING_MMC_HS200)
  1224. return amd_execute_tuning_hs200(host, opcode);
  1225. /* Otherwise perform standard SDHCI tuning */
  1226. return sdhci_execute_tuning(mmc, opcode);
  1227. }
  1228. static int amd_probe_slot(struct sdhci_pci_slot *slot)
  1229. {
  1230. struct mmc_host_ops *ops = &slot->host->mmc_host_ops;
  1231. ops->execute_tuning = amd_execute_tuning;
  1232. return 0;
  1233. }
  1234. static int amd_probe(struct sdhci_pci_chip *chip)
  1235. {
  1236. struct pci_dev *smbus_dev;
  1237. enum amd_chipset_gen gen;
  1238. smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
  1239. PCI_DEVICE_ID_AMD_HUDSON2_SMBUS, NULL);
  1240. if (smbus_dev) {
  1241. gen = AMD_CHIPSET_BEFORE_ML;
  1242. } else {
  1243. smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
  1244. PCI_DEVICE_ID_AMD_KERNCZ_SMBUS, NULL);
  1245. if (smbus_dev) {
  1246. if (smbus_dev->revision < 0x51)
  1247. gen = AMD_CHIPSET_CZ;
  1248. else
  1249. gen = AMD_CHIPSET_NL;
  1250. } else {
  1251. gen = AMD_CHIPSET_UNKNOWN;
  1252. }
  1253. }
  1254. if (gen == AMD_CHIPSET_BEFORE_ML || gen == AMD_CHIPSET_CZ)
  1255. chip->quirks2 |= SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD;
  1256. return 0;
  1257. }
  1258. static const struct sdhci_ops amd_sdhci_pci_ops = {
  1259. .set_clock = sdhci_set_clock,
  1260. .enable_dma = sdhci_pci_enable_dma,
  1261. .set_bus_width = sdhci_set_bus_width,
  1262. .reset = sdhci_reset,
  1263. .set_uhs_signaling = sdhci_set_uhs_signaling,
  1264. };
  1265. static const struct sdhci_pci_fixes sdhci_amd = {
  1266. .probe = amd_probe,
  1267. .ops = &amd_sdhci_pci_ops,
  1268. .probe_slot = amd_probe_slot,
  1269. };
  1270. static const struct pci_device_id pci_ids[] = {
  1271. SDHCI_PCI_DEVICE(RICOH, R5C822, ricoh),
  1272. SDHCI_PCI_DEVICE(RICOH, R5C843, ricoh_mmc),
  1273. SDHCI_PCI_DEVICE(RICOH, R5CE822, ricoh_mmc),
  1274. SDHCI_PCI_DEVICE(RICOH, R5CE823, ricoh_mmc),
  1275. SDHCI_PCI_DEVICE(ENE, CB712_SD, ene_712),
  1276. SDHCI_PCI_DEVICE(ENE, CB712_SD_2, ene_712),
  1277. SDHCI_PCI_DEVICE(ENE, CB714_SD, ene_714),
  1278. SDHCI_PCI_DEVICE(ENE, CB714_SD_2, ene_714),
  1279. SDHCI_PCI_DEVICE(MARVELL, 88ALP01_SD, cafe),
  1280. SDHCI_PCI_DEVICE(JMICRON, JMB38X_SD, jmicron),
  1281. SDHCI_PCI_DEVICE(JMICRON, JMB38X_MMC, jmicron),
  1282. SDHCI_PCI_DEVICE(JMICRON, JMB388_SD, jmicron),
  1283. SDHCI_PCI_DEVICE(JMICRON, JMB388_ESD, jmicron),
  1284. SDHCI_PCI_DEVICE(SYSKONNECT, 8000, syskt),
  1285. SDHCI_PCI_DEVICE(VIA, 95D0, via),
  1286. SDHCI_PCI_DEVICE(REALTEK, 5250, rtsx),
  1287. SDHCI_PCI_DEVICE(INTEL, QRK_SD, intel_qrk),
  1288. SDHCI_PCI_DEVICE(INTEL, MRST_SD0, intel_mrst_hc0),
  1289. SDHCI_PCI_DEVICE(INTEL, MRST_SD1, intel_mrst_hc1_hc2),
  1290. SDHCI_PCI_DEVICE(INTEL, MRST_SD2, intel_mrst_hc1_hc2),
  1291. SDHCI_PCI_DEVICE(INTEL, MFD_SD, intel_mfd_sd),
  1292. SDHCI_PCI_DEVICE(INTEL, MFD_SDIO1, intel_mfd_sdio),
  1293. SDHCI_PCI_DEVICE(INTEL, MFD_SDIO2, intel_mfd_sdio),
  1294. SDHCI_PCI_DEVICE(INTEL, MFD_EMMC0, intel_mfd_emmc),
  1295. SDHCI_PCI_DEVICE(INTEL, MFD_EMMC1, intel_mfd_emmc),
  1296. SDHCI_PCI_DEVICE(INTEL, PCH_SDIO0, intel_pch_sdio),
  1297. SDHCI_PCI_DEVICE(INTEL, PCH_SDIO1, intel_pch_sdio),
  1298. SDHCI_PCI_DEVICE(INTEL, BYT_EMMC, intel_byt_emmc),
  1299. SDHCI_PCI_SUBDEVICE(INTEL, BYT_SDIO, NI, 7884, ni_byt_sdio),
  1300. SDHCI_PCI_DEVICE(INTEL, BYT_SDIO, intel_byt_sdio),
  1301. SDHCI_PCI_DEVICE(INTEL, BYT_SD, intel_byt_sd),
  1302. SDHCI_PCI_DEVICE(INTEL, BYT_EMMC2, intel_byt_emmc),
  1303. SDHCI_PCI_DEVICE(INTEL, BSW_EMMC, intel_byt_emmc),
  1304. SDHCI_PCI_DEVICE(INTEL, BSW_SDIO, intel_byt_sdio),
  1305. SDHCI_PCI_DEVICE(INTEL, BSW_SD, intel_byt_sd),
  1306. SDHCI_PCI_DEVICE(INTEL, CLV_SDIO0, intel_mfd_sd),
  1307. SDHCI_PCI_DEVICE(INTEL, CLV_SDIO1, intel_mfd_sdio),
  1308. SDHCI_PCI_DEVICE(INTEL, CLV_SDIO2, intel_mfd_sdio),
  1309. SDHCI_PCI_DEVICE(INTEL, CLV_EMMC0, intel_mfd_emmc),
  1310. SDHCI_PCI_DEVICE(INTEL, CLV_EMMC1, intel_mfd_emmc),
  1311. SDHCI_PCI_DEVICE(INTEL, MRFLD_MMC, intel_mrfld_mmc),
  1312. SDHCI_PCI_DEVICE(INTEL, SPT_EMMC, intel_byt_emmc),
  1313. SDHCI_PCI_DEVICE(INTEL, SPT_SDIO, intel_byt_sdio),
  1314. SDHCI_PCI_DEVICE(INTEL, SPT_SD, intel_byt_sd),
  1315. SDHCI_PCI_DEVICE(INTEL, DNV_EMMC, intel_byt_emmc),
  1316. SDHCI_PCI_DEVICE(INTEL, CDF_EMMC, intel_glk_emmc),
  1317. SDHCI_PCI_DEVICE(INTEL, BXT_EMMC, intel_byt_emmc),
  1318. SDHCI_PCI_DEVICE(INTEL, BXT_SDIO, intel_byt_sdio),
  1319. SDHCI_PCI_DEVICE(INTEL, BXT_SD, intel_byt_sd),
  1320. SDHCI_PCI_DEVICE(INTEL, BXTM_EMMC, intel_byt_emmc),
  1321. SDHCI_PCI_DEVICE(INTEL, BXTM_SDIO, intel_byt_sdio),
  1322. SDHCI_PCI_DEVICE(INTEL, BXTM_SD, intel_byt_sd),
  1323. SDHCI_PCI_DEVICE(INTEL, APL_EMMC, intel_byt_emmc),
  1324. SDHCI_PCI_DEVICE(INTEL, APL_SDIO, intel_byt_sdio),
  1325. SDHCI_PCI_DEVICE(INTEL, APL_SD, intel_byt_sd),
  1326. SDHCI_PCI_DEVICE(INTEL, GLK_EMMC, intel_glk_emmc),
  1327. SDHCI_PCI_DEVICE(INTEL, GLK_SDIO, intel_byt_sdio),
  1328. SDHCI_PCI_DEVICE(INTEL, GLK_SD, intel_byt_sd),
  1329. SDHCI_PCI_DEVICE(INTEL, CNP_EMMC, intel_glk_emmc),
  1330. SDHCI_PCI_DEVICE(INTEL, CNP_SD, intel_byt_sd),
  1331. SDHCI_PCI_DEVICE(INTEL, CNPH_SD, intel_byt_sd),
  1332. SDHCI_PCI_DEVICE(INTEL, ICP_EMMC, intel_glk_emmc),
  1333. SDHCI_PCI_DEVICE(INTEL, ICP_SD, intel_byt_sd),
  1334. SDHCI_PCI_DEVICE(INTEL, CML_EMMC, intel_glk_emmc),
  1335. SDHCI_PCI_DEVICE(INTEL, CML_SD, intel_byt_sd),
  1336. SDHCI_PCI_DEVICE(O2, 8120, o2),
  1337. SDHCI_PCI_DEVICE(O2, 8220, o2),
  1338. SDHCI_PCI_DEVICE(O2, 8221, o2),
  1339. SDHCI_PCI_DEVICE(O2, 8320, o2),
  1340. SDHCI_PCI_DEVICE(O2, 8321, o2),
  1341. SDHCI_PCI_DEVICE(O2, FUJIN2, o2),
  1342. SDHCI_PCI_DEVICE(O2, SDS0, o2),
  1343. SDHCI_PCI_DEVICE(O2, SDS1, o2),
  1344. SDHCI_PCI_DEVICE(O2, SEABIRD0, o2),
  1345. SDHCI_PCI_DEVICE(O2, SEABIRD1, o2),
  1346. SDHCI_PCI_DEVICE(ARASAN, PHY_EMMC, arasan),
  1347. SDHCI_PCI_DEVICE(SYNOPSYS, DWC_MSHC, snps),
  1348. SDHCI_PCI_DEVICE_CLASS(AMD, SYSTEM_SDHCI, PCI_CLASS_MASK, amd),
  1349. /* Generic SD host controller */
  1350. {PCI_DEVICE_CLASS(SYSTEM_SDHCI, PCI_CLASS_MASK)},
  1351. { /* end: all zeroes */ },
  1352. };
  1353. MODULE_DEVICE_TABLE(pci, pci_ids);
  1354. /*****************************************************************************\
  1355. * *
  1356. * SDHCI core callbacks *
  1357. * *
  1358. \*****************************************************************************/
  1359. int sdhci_pci_enable_dma(struct sdhci_host *host)
  1360. {
  1361. struct sdhci_pci_slot *slot;
  1362. struct pci_dev *pdev;
  1363. slot = sdhci_priv(host);
  1364. pdev = slot->chip->pdev;
  1365. if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
  1366. ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
  1367. (host->flags & SDHCI_USE_SDMA)) {
  1368. dev_warn(&pdev->dev, "Will use DMA mode even though HW "
  1369. "doesn't fully claim to support it.\n");
  1370. }
  1371. pci_set_master(pdev);
  1372. return 0;
  1373. }
  1374. static void sdhci_pci_gpio_hw_reset(struct sdhci_host *host)
  1375. {
  1376. struct sdhci_pci_slot *slot = sdhci_priv(host);
  1377. int rst_n_gpio = slot->rst_n_gpio;
  1378. if (!gpio_is_valid(rst_n_gpio))
  1379. return;
  1380. gpio_set_value_cansleep(rst_n_gpio, 0);
  1381. /* For eMMC, minimum is 1us but give it 10us for good measure */
  1382. udelay(10);
  1383. gpio_set_value_cansleep(rst_n_gpio, 1);
  1384. /* For eMMC, minimum is 200us but give it 300us for good measure */
  1385. usleep_range(300, 1000);
  1386. }
  1387. static void sdhci_pci_hw_reset(struct sdhci_host *host)
  1388. {
  1389. struct sdhci_pci_slot *slot = sdhci_priv(host);
  1390. if (slot->hw_reset)
  1391. slot->hw_reset(host);
  1392. }
  1393. static const struct sdhci_ops sdhci_pci_ops = {
  1394. .set_clock = sdhci_set_clock,
  1395. .enable_dma = sdhci_pci_enable_dma,
  1396. .set_bus_width = sdhci_set_bus_width,
  1397. .reset = sdhci_reset,
  1398. .set_uhs_signaling = sdhci_set_uhs_signaling,
  1399. .hw_reset = sdhci_pci_hw_reset,
  1400. };
  1401. /*****************************************************************************\
  1402. * *
  1403. * Suspend/resume *
  1404. * *
  1405. \*****************************************************************************/
  1406. #ifdef CONFIG_PM_SLEEP
  1407. static int sdhci_pci_suspend(struct device *dev)
  1408. {
  1409. struct pci_dev *pdev = to_pci_dev(dev);
  1410. struct sdhci_pci_chip *chip = pci_get_drvdata(pdev);
  1411. if (!chip)
  1412. return 0;
  1413. if (chip->fixes && chip->fixes->suspend)
  1414. return chip->fixes->suspend(chip);
  1415. return sdhci_pci_suspend_host(chip);
  1416. }
  1417. static int sdhci_pci_resume(struct device *dev)
  1418. {
  1419. struct pci_dev *pdev = to_pci_dev(dev);
  1420. struct sdhci_pci_chip *chip = pci_get_drvdata(pdev);
  1421. if (!chip)
  1422. return 0;
  1423. if (chip->fixes && chip->fixes->resume)
  1424. return chip->fixes->resume(chip);
  1425. return sdhci_pci_resume_host(chip);
  1426. }
  1427. #endif
  1428. #ifdef CONFIG_PM
  1429. static int sdhci_pci_runtime_suspend(struct device *dev)
  1430. {
  1431. struct pci_dev *pdev = to_pci_dev(dev);
  1432. struct sdhci_pci_chip *chip = pci_get_drvdata(pdev);
  1433. if (!chip)
  1434. return 0;
  1435. if (chip->fixes && chip->fixes->runtime_suspend)
  1436. return chip->fixes->runtime_suspend(chip);
  1437. return sdhci_pci_runtime_suspend_host(chip);
  1438. }
  1439. static int sdhci_pci_runtime_resume(struct device *dev)
  1440. {
  1441. struct pci_dev *pdev = to_pci_dev(dev);
  1442. struct sdhci_pci_chip *chip = pci_get_drvdata(pdev);
  1443. if (!chip)
  1444. return 0;
  1445. if (chip->fixes && chip->fixes->runtime_resume)
  1446. return chip->fixes->runtime_resume(chip);
  1447. return sdhci_pci_runtime_resume_host(chip);
  1448. }
  1449. #endif
  1450. static const struct dev_pm_ops sdhci_pci_pm_ops = {
  1451. SET_SYSTEM_SLEEP_PM_OPS(sdhci_pci_suspend, sdhci_pci_resume)
  1452. SET_RUNTIME_PM_OPS(sdhci_pci_runtime_suspend,
  1453. sdhci_pci_runtime_resume, NULL)
  1454. };
  1455. /*****************************************************************************\
  1456. * *
  1457. * Device probing/removal *
  1458. * *
  1459. \*****************************************************************************/
  1460. static struct sdhci_pci_slot *sdhci_pci_probe_slot(
  1461. struct pci_dev *pdev, struct sdhci_pci_chip *chip, int first_bar,
  1462. int slotno)
  1463. {
  1464. struct sdhci_pci_slot *slot;
  1465. struct sdhci_host *host;
  1466. int ret, bar = first_bar + slotno;
  1467. size_t priv_size = chip->fixes ? chip->fixes->priv_size : 0;
  1468. if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
  1469. dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
  1470. return ERR_PTR(-ENODEV);
  1471. }
  1472. if (pci_resource_len(pdev, bar) < 0x100) {
  1473. dev_err(&pdev->dev, "Invalid iomem size. You may "
  1474. "experience problems.\n");
  1475. }
  1476. if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
  1477. dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
  1478. return ERR_PTR(-ENODEV);
  1479. }
  1480. if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
  1481. dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
  1482. return ERR_PTR(-ENODEV);
  1483. }
  1484. host = sdhci_alloc_host(&pdev->dev, sizeof(*slot) + priv_size);
  1485. if (IS_ERR(host)) {
  1486. dev_err(&pdev->dev, "cannot allocate host\n");
  1487. return ERR_CAST(host);
  1488. }
  1489. slot = sdhci_priv(host);
  1490. slot->chip = chip;
  1491. slot->host = host;
  1492. slot->rst_n_gpio = -EINVAL;
  1493. slot->cd_gpio = -EINVAL;
  1494. slot->cd_idx = -1;
  1495. /* Retrieve platform data if there is any */
  1496. if (*sdhci_pci_get_data)
  1497. slot->data = sdhci_pci_get_data(pdev, slotno);
  1498. if (slot->data) {
  1499. if (slot->data->setup) {
  1500. ret = slot->data->setup(slot->data);
  1501. if (ret) {
  1502. dev_err(&pdev->dev, "platform setup failed\n");
  1503. goto free;
  1504. }
  1505. }
  1506. slot->rst_n_gpio = slot->data->rst_n_gpio;
  1507. slot->cd_gpio = slot->data->cd_gpio;
  1508. }
  1509. host->hw_name = "PCI";
  1510. host->ops = chip->fixes && chip->fixes->ops ?
  1511. chip->fixes->ops :
  1512. &sdhci_pci_ops;
  1513. host->quirks = chip->quirks;
  1514. host->quirks2 = chip->quirks2;
  1515. host->irq = pdev->irq;
  1516. ret = pcim_iomap_regions(pdev, BIT(bar), mmc_hostname(host->mmc));
  1517. if (ret) {
  1518. dev_err(&pdev->dev, "cannot request region\n");
  1519. goto cleanup;
  1520. }
  1521. host->ioaddr = pcim_iomap_table(pdev)[bar];
  1522. if (chip->fixes && chip->fixes->probe_slot) {
  1523. ret = chip->fixes->probe_slot(slot);
  1524. if (ret)
  1525. goto cleanup;
  1526. }
  1527. if (gpio_is_valid(slot->rst_n_gpio)) {
  1528. if (!devm_gpio_request(&pdev->dev, slot->rst_n_gpio, "eMMC_reset")) {
  1529. gpio_direction_output(slot->rst_n_gpio, 1);
  1530. slot->host->mmc->caps |= MMC_CAP_HW_RESET;
  1531. slot->hw_reset = sdhci_pci_gpio_hw_reset;
  1532. } else {
  1533. dev_warn(&pdev->dev, "failed to request rst_n_gpio\n");
  1534. slot->rst_n_gpio = -EINVAL;
  1535. }
  1536. }
  1537. host->mmc->pm_caps = MMC_PM_KEEP_POWER;
  1538. host->mmc->slotno = slotno;
  1539. host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
  1540. if (device_can_wakeup(&pdev->dev))
  1541. host->mmc->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
  1542. if (host->mmc->caps & MMC_CAP_CD_WAKE)
  1543. device_init_wakeup(&pdev->dev, true);
  1544. if (slot->cd_idx >= 0) {
  1545. ret = mmc_gpiod_request_cd(host->mmc, "cd", slot->cd_idx,
  1546. slot->cd_override_level, 0, NULL);
  1547. if (ret && ret != -EPROBE_DEFER)
  1548. ret = mmc_gpiod_request_cd(host->mmc, NULL,
  1549. slot->cd_idx,
  1550. slot->cd_override_level,
  1551. 0, NULL);
  1552. if (ret == -EPROBE_DEFER)
  1553. goto remove;
  1554. if (ret) {
  1555. dev_warn(&pdev->dev, "failed to setup card detect gpio\n");
  1556. slot->cd_idx = -1;
  1557. }
  1558. }
  1559. if (chip->fixes && chip->fixes->add_host)
  1560. ret = chip->fixes->add_host(slot);
  1561. else
  1562. ret = sdhci_add_host(host);
  1563. if (ret)
  1564. goto remove;
  1565. sdhci_pci_add_own_cd(slot);
  1566. /*
  1567. * Check if the chip needs a separate GPIO for card detect to wake up
  1568. * from runtime suspend. If it is not there, don't allow runtime PM.
  1569. * Note sdhci_pci_add_own_cd() sets slot->cd_gpio to -EINVAL on failure.
  1570. */
  1571. if (chip->fixes && chip->fixes->own_cd_for_runtime_pm &&
  1572. !gpio_is_valid(slot->cd_gpio) && slot->cd_idx < 0)
  1573. chip->allow_runtime_pm = false;
  1574. return slot;
  1575. remove:
  1576. if (chip->fixes && chip->fixes->remove_slot)
  1577. chip->fixes->remove_slot(slot, 0);
  1578. cleanup:
  1579. if (slot->data && slot->data->cleanup)
  1580. slot->data->cleanup(slot->data);
  1581. free:
  1582. sdhci_free_host(host);
  1583. return ERR_PTR(ret);
  1584. }
  1585. static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
  1586. {
  1587. int dead;
  1588. u32 scratch;
  1589. sdhci_pci_remove_own_cd(slot);
  1590. dead = 0;
  1591. scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
  1592. if (scratch == (u32)-1)
  1593. dead = 1;
  1594. sdhci_remove_host(slot->host, dead);
  1595. if (slot->chip->fixes && slot->chip->fixes->remove_slot)
  1596. slot->chip->fixes->remove_slot(slot, dead);
  1597. if (slot->data && slot->data->cleanup)
  1598. slot->data->cleanup(slot->data);
  1599. sdhci_free_host(slot->host);
  1600. }
  1601. static void sdhci_pci_runtime_pm_allow(struct device *dev)
  1602. {
  1603. pm_suspend_ignore_children(dev, 1);
  1604. pm_runtime_set_autosuspend_delay(dev, 50);
  1605. pm_runtime_use_autosuspend(dev);
  1606. pm_runtime_allow(dev);
  1607. /* Stay active until mmc core scans for a card */
  1608. pm_runtime_put_noidle(dev);
  1609. }
  1610. static void sdhci_pci_runtime_pm_forbid(struct device *dev)
  1611. {
  1612. pm_runtime_forbid(dev);
  1613. pm_runtime_get_noresume(dev);
  1614. }
  1615. static int sdhci_pci_probe(struct pci_dev *pdev,
  1616. const struct pci_device_id *ent)
  1617. {
  1618. struct sdhci_pci_chip *chip;
  1619. struct sdhci_pci_slot *slot;
  1620. u8 slots, first_bar;
  1621. int ret, i;
  1622. BUG_ON(pdev == NULL);
  1623. BUG_ON(ent == NULL);
  1624. dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
  1625. (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
  1626. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
  1627. if (ret)
  1628. return ret;
  1629. slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
  1630. dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
  1631. if (slots == 0)
  1632. return -ENODEV;
  1633. BUG_ON(slots > MAX_SLOTS);
  1634. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
  1635. if (ret)
  1636. return ret;
  1637. first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
  1638. if (first_bar > 5) {
  1639. dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
  1640. return -ENODEV;
  1641. }
  1642. ret = pcim_enable_device(pdev);
  1643. if (ret)
  1644. return ret;
  1645. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  1646. if (!chip)
  1647. return -ENOMEM;
  1648. chip->pdev = pdev;
  1649. chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
  1650. if (chip->fixes) {
  1651. chip->quirks = chip->fixes->quirks;
  1652. chip->quirks2 = chip->fixes->quirks2;
  1653. chip->allow_runtime_pm = chip->fixes->allow_runtime_pm;
  1654. }
  1655. chip->num_slots = slots;
  1656. chip->pm_retune = true;
  1657. chip->rpm_retune = true;
  1658. pci_set_drvdata(pdev, chip);
  1659. if (chip->fixes && chip->fixes->probe) {
  1660. ret = chip->fixes->probe(chip);
  1661. if (ret)
  1662. return ret;
  1663. }
  1664. slots = chip->num_slots; /* Quirk may have changed this */
  1665. for (i = 0; i < slots; i++) {
  1666. slot = sdhci_pci_probe_slot(pdev, chip, first_bar, i);
  1667. if (IS_ERR(slot)) {
  1668. for (i--; i >= 0; i--)
  1669. sdhci_pci_remove_slot(chip->slots[i]);
  1670. return PTR_ERR(slot);
  1671. }
  1672. chip->slots[i] = slot;
  1673. }
  1674. if (chip->allow_runtime_pm)
  1675. sdhci_pci_runtime_pm_allow(&pdev->dev);
  1676. return 0;
  1677. }
  1678. static void sdhci_pci_remove(struct pci_dev *pdev)
  1679. {
  1680. int i;
  1681. struct sdhci_pci_chip *chip = pci_get_drvdata(pdev);
  1682. if (chip->allow_runtime_pm)
  1683. sdhci_pci_runtime_pm_forbid(&pdev->dev);
  1684. for (i = 0; i < chip->num_slots; i++)
  1685. sdhci_pci_remove_slot(chip->slots[i]);
  1686. }
  1687. static struct pci_driver sdhci_driver = {
  1688. .name = "sdhci-pci",
  1689. .id_table = pci_ids,
  1690. .probe = sdhci_pci_probe,
  1691. .remove = sdhci_pci_remove,
  1692. .driver = {
  1693. .pm = &sdhci_pci_pm_ops
  1694. },
  1695. };
  1696. module_pci_driver(sdhci_driver);
  1697. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  1698. MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
  1699. MODULE_LICENSE("GPL");