sdhci-pci-core.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  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/string.h>
  15. #include <linux/delay.h>
  16. #include <linux/highmem.h>
  17. #include <linux/module.h>
  18. #include <linux/pci.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/slab.h>
  21. #include <linux/device.h>
  22. #include <linux/mmc/host.h>
  23. #include <linux/mmc/mmc.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/io.h>
  26. #include <linux/gpio.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/mmc/slot-gpio.h>
  29. #include <linux/mmc/sdhci-pci-data.h>
  30. #include <linux/acpi.h>
  31. #include "cqhci.h"
  32. #include "sdhci.h"
  33. #include "sdhci-pci.h"
  34. static int sdhci_pci_enable_dma(struct sdhci_host *host);
  35. static void sdhci_pci_hw_reset(struct sdhci_host *host);
  36. #ifdef CONFIG_PM_SLEEP
  37. static int __sdhci_pci_suspend_host(struct sdhci_pci_chip *chip)
  38. {
  39. int i, ret;
  40. for (i = 0; i < chip->num_slots; i++) {
  41. struct sdhci_pci_slot *slot = chip->slots[i];
  42. struct sdhci_host *host;
  43. if (!slot)
  44. continue;
  45. host = slot->host;
  46. if (chip->pm_retune && host->tuning_mode != SDHCI_TUNING_MODE_3)
  47. mmc_retune_needed(host->mmc);
  48. ret = sdhci_suspend_host(host);
  49. if (ret)
  50. goto err_pci_suspend;
  51. if (host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ)
  52. sdhci_enable_irq_wakeups(host);
  53. }
  54. return 0;
  55. err_pci_suspend:
  56. while (--i >= 0)
  57. sdhci_resume_host(chip->slots[i]->host);
  58. return ret;
  59. }
  60. static int sdhci_pci_init_wakeup(struct sdhci_pci_chip *chip)
  61. {
  62. mmc_pm_flag_t pm_flags = 0;
  63. int i;
  64. for (i = 0; i < chip->num_slots; i++) {
  65. struct sdhci_pci_slot *slot = chip->slots[i];
  66. if (slot)
  67. pm_flags |= slot->host->mmc->pm_flags;
  68. }
  69. return device_init_wakeup(&chip->pdev->dev,
  70. (pm_flags & MMC_PM_KEEP_POWER) &&
  71. (pm_flags & MMC_PM_WAKE_SDIO_IRQ));
  72. }
  73. static int sdhci_pci_suspend_host(struct sdhci_pci_chip *chip)
  74. {
  75. int ret;
  76. ret = __sdhci_pci_suspend_host(chip);
  77. if (ret)
  78. return ret;
  79. sdhci_pci_init_wakeup(chip);
  80. return 0;
  81. }
  82. int sdhci_pci_resume_host(struct sdhci_pci_chip *chip)
  83. {
  84. struct sdhci_pci_slot *slot;
  85. int i, ret;
  86. for (i = 0; i < chip->num_slots; i++) {
  87. slot = chip->slots[i];
  88. if (!slot)
  89. continue;
  90. ret = sdhci_resume_host(slot->host);
  91. if (ret)
  92. return ret;
  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_DRV_STRENGTH = 9,
  365. INTEL_DSM_D3_RETUNE = 10,
  366. };
  367. struct intel_host {
  368. u32 dsm_fns;
  369. int drv_strength;
  370. bool d3_retune;
  371. };
  372. static const guid_t intel_dsm_guid =
  373. GUID_INIT(0xF6C13EA5, 0x65CD, 0x461F,
  374. 0xAB, 0x7A, 0x29, 0xF7, 0xE8, 0xD5, 0xBD, 0x61);
  375. static int __intel_dsm(struct intel_host *intel_host, struct device *dev,
  376. unsigned int fn, u32 *result)
  377. {
  378. union acpi_object *obj;
  379. int err = 0;
  380. size_t len;
  381. obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &intel_dsm_guid, 0, fn, NULL);
  382. if (!obj)
  383. return -EOPNOTSUPP;
  384. if (obj->type != ACPI_TYPE_BUFFER || obj->buffer.length < 1) {
  385. err = -EINVAL;
  386. goto out;
  387. }
  388. len = min_t(size_t, obj->buffer.length, 4);
  389. *result = 0;
  390. memcpy(result, obj->buffer.pointer, len);
  391. out:
  392. ACPI_FREE(obj);
  393. return err;
  394. }
  395. static int intel_dsm(struct intel_host *intel_host, struct device *dev,
  396. unsigned int fn, u32 *result)
  397. {
  398. if (fn > 31 || !(intel_host->dsm_fns & (1 << fn)))
  399. return -EOPNOTSUPP;
  400. return __intel_dsm(intel_host, dev, fn, result);
  401. }
  402. static void intel_dsm_init(struct intel_host *intel_host, struct device *dev,
  403. struct mmc_host *mmc)
  404. {
  405. int err;
  406. u32 val;
  407. intel_host->d3_retune = true;
  408. err = __intel_dsm(intel_host, dev, INTEL_DSM_FNS, &intel_host->dsm_fns);
  409. if (err) {
  410. pr_debug("%s: DSM not supported, error %d\n",
  411. mmc_hostname(mmc), err);
  412. return;
  413. }
  414. pr_debug("%s: DSM function mask %#x\n",
  415. mmc_hostname(mmc), intel_host->dsm_fns);
  416. err = intel_dsm(intel_host, dev, INTEL_DSM_DRV_STRENGTH, &val);
  417. intel_host->drv_strength = err ? 0 : val;
  418. err = intel_dsm(intel_host, dev, INTEL_DSM_D3_RETUNE, &val);
  419. intel_host->d3_retune = err ? true : !!val;
  420. }
  421. static void sdhci_pci_int_hw_reset(struct sdhci_host *host)
  422. {
  423. u8 reg;
  424. reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
  425. reg |= 0x10;
  426. sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
  427. /* For eMMC, minimum is 1us but give it 9us for good measure */
  428. udelay(9);
  429. reg &= ~0x10;
  430. sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
  431. /* For eMMC, minimum is 200us but give it 300us for good measure */
  432. usleep_range(300, 1000);
  433. }
  434. static int intel_select_drive_strength(struct mmc_card *card,
  435. unsigned int max_dtr, int host_drv,
  436. int card_drv, int *drv_type)
  437. {
  438. struct sdhci_host *host = mmc_priv(card->host);
  439. struct sdhci_pci_slot *slot = sdhci_priv(host);
  440. struct intel_host *intel_host = sdhci_pci_priv(slot);
  441. return intel_host->drv_strength;
  442. }
  443. static int bxt_get_cd(struct mmc_host *mmc)
  444. {
  445. int gpio_cd = mmc_gpio_get_cd(mmc);
  446. struct sdhci_host *host = mmc_priv(mmc);
  447. unsigned long flags;
  448. int ret = 0;
  449. if (!gpio_cd)
  450. return 0;
  451. spin_lock_irqsave(&host->lock, flags);
  452. if (host->flags & SDHCI_DEVICE_DEAD)
  453. goto out;
  454. ret = !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
  455. out:
  456. spin_unlock_irqrestore(&host->lock, flags);
  457. return ret;
  458. }
  459. #define SDHCI_INTEL_PWR_TIMEOUT_CNT 20
  460. #define SDHCI_INTEL_PWR_TIMEOUT_UDELAY 100
  461. static void sdhci_intel_set_power(struct sdhci_host *host, unsigned char mode,
  462. unsigned short vdd)
  463. {
  464. int cntr;
  465. u8 reg;
  466. sdhci_set_power(host, mode, vdd);
  467. if (mode == MMC_POWER_OFF)
  468. return;
  469. /*
  470. * Bus power might not enable after D3 -> D0 transition due to the
  471. * present state not yet having propagated. Retry for up to 2ms.
  472. */
  473. for (cntr = 0; cntr < SDHCI_INTEL_PWR_TIMEOUT_CNT; cntr++) {
  474. reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
  475. if (reg & SDHCI_POWER_ON)
  476. break;
  477. udelay(SDHCI_INTEL_PWR_TIMEOUT_UDELAY);
  478. reg |= SDHCI_POWER_ON;
  479. sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
  480. }
  481. }
  482. #define INTEL_HS400_ES_REG 0x78
  483. #define INTEL_HS400_ES_BIT BIT(0)
  484. static void intel_hs400_enhanced_strobe(struct mmc_host *mmc,
  485. struct mmc_ios *ios)
  486. {
  487. struct sdhci_host *host = mmc_priv(mmc);
  488. u32 val;
  489. val = sdhci_readl(host, INTEL_HS400_ES_REG);
  490. if (ios->enhanced_strobe)
  491. val |= INTEL_HS400_ES_BIT;
  492. else
  493. val &= ~INTEL_HS400_ES_BIT;
  494. sdhci_writel(host, val, INTEL_HS400_ES_REG);
  495. }
  496. static void sdhci_intel_voltage_switch(struct sdhci_host *host)
  497. {
  498. struct sdhci_pci_slot *slot = sdhci_priv(host);
  499. struct intel_host *intel_host = sdhci_pci_priv(slot);
  500. struct device *dev = &slot->chip->pdev->dev;
  501. u32 result = 0;
  502. int err;
  503. err = intel_dsm(intel_host, dev, INTEL_DSM_V18_SWITCH, &result);
  504. pr_debug("%s: %s DSM error %d result %u\n",
  505. mmc_hostname(host->mmc), __func__, err, result);
  506. }
  507. static const struct sdhci_ops sdhci_intel_byt_ops = {
  508. .set_clock = sdhci_set_clock,
  509. .set_power = sdhci_intel_set_power,
  510. .enable_dma = sdhci_pci_enable_dma,
  511. .set_bus_width = sdhci_set_bus_width,
  512. .reset = sdhci_reset,
  513. .set_uhs_signaling = sdhci_set_uhs_signaling,
  514. .hw_reset = sdhci_pci_hw_reset,
  515. .voltage_switch = sdhci_intel_voltage_switch,
  516. };
  517. static const struct sdhci_ops sdhci_intel_glk_ops = {
  518. .set_clock = sdhci_set_clock,
  519. .set_power = sdhci_intel_set_power,
  520. .enable_dma = sdhci_pci_enable_dma,
  521. .set_bus_width = sdhci_set_bus_width,
  522. .reset = sdhci_reset,
  523. .set_uhs_signaling = sdhci_set_uhs_signaling,
  524. .hw_reset = sdhci_pci_hw_reset,
  525. .voltage_switch = sdhci_intel_voltage_switch,
  526. .irq = sdhci_cqhci_irq,
  527. };
  528. static void byt_read_dsm(struct sdhci_pci_slot *slot)
  529. {
  530. struct intel_host *intel_host = sdhci_pci_priv(slot);
  531. struct device *dev = &slot->chip->pdev->dev;
  532. struct mmc_host *mmc = slot->host->mmc;
  533. intel_dsm_init(intel_host, dev, mmc);
  534. slot->chip->rpm_retune = intel_host->d3_retune;
  535. }
  536. static int byt_emmc_probe_slot(struct sdhci_pci_slot *slot)
  537. {
  538. byt_read_dsm(slot);
  539. slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
  540. MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR |
  541. MMC_CAP_CMD_DURING_TFR |
  542. MMC_CAP_WAIT_WHILE_BUSY;
  543. slot->hw_reset = sdhci_pci_int_hw_reset;
  544. if (slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_BSW_EMMC)
  545. slot->host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */
  546. slot->host->mmc_host_ops.select_drive_strength =
  547. intel_select_drive_strength;
  548. return 0;
  549. }
  550. static int glk_emmc_probe_slot(struct sdhci_pci_slot *slot)
  551. {
  552. int ret = byt_emmc_probe_slot(slot);
  553. slot->host->mmc->caps2 |= MMC_CAP2_CQE;
  554. if (slot->chip->pdev->device != PCI_DEVICE_ID_INTEL_GLK_EMMC) {
  555. slot->host->mmc->caps2 |= MMC_CAP2_HS400_ES,
  556. slot->host->mmc_host_ops.hs400_enhanced_strobe =
  557. intel_hs400_enhanced_strobe;
  558. slot->host->mmc->caps2 |= MMC_CAP2_CQE_DCMD;
  559. }
  560. return ret;
  561. }
  562. static void glk_cqe_enable(struct mmc_host *mmc)
  563. {
  564. struct sdhci_host *host = mmc_priv(mmc);
  565. u32 reg;
  566. /*
  567. * CQE gets stuck if it sees Buffer Read Enable bit set, which can be
  568. * the case after tuning, so ensure the buffer is drained.
  569. */
  570. reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
  571. while (reg & SDHCI_DATA_AVAILABLE) {
  572. sdhci_readl(host, SDHCI_BUFFER);
  573. reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
  574. }
  575. sdhci_cqe_enable(mmc);
  576. }
  577. static const struct cqhci_host_ops glk_cqhci_ops = {
  578. .enable = glk_cqe_enable,
  579. .disable = sdhci_cqe_disable,
  580. .dumpregs = sdhci_pci_dumpregs,
  581. };
  582. static int glk_emmc_add_host(struct sdhci_pci_slot *slot)
  583. {
  584. struct device *dev = &slot->chip->pdev->dev;
  585. struct sdhci_host *host = slot->host;
  586. struct cqhci_host *cq_host;
  587. bool dma64;
  588. int ret;
  589. ret = sdhci_setup_host(host);
  590. if (ret)
  591. return ret;
  592. cq_host = devm_kzalloc(dev, sizeof(*cq_host), GFP_KERNEL);
  593. if (!cq_host) {
  594. ret = -ENOMEM;
  595. goto cleanup;
  596. }
  597. cq_host->mmio = host->ioaddr + 0x200;
  598. cq_host->quirks |= CQHCI_QUIRK_SHORT_TXFR_DESC_SZ;
  599. cq_host->ops = &glk_cqhci_ops;
  600. dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
  601. if (dma64)
  602. cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
  603. ret = cqhci_init(cq_host, host->mmc, dma64);
  604. if (ret)
  605. goto cleanup;
  606. ret = __sdhci_add_host(host);
  607. if (ret)
  608. goto cleanup;
  609. return 0;
  610. cleanup:
  611. sdhci_cleanup_host(host);
  612. return ret;
  613. }
  614. #ifdef CONFIG_ACPI
  615. static int ni_set_max_freq(struct sdhci_pci_slot *slot)
  616. {
  617. acpi_status status;
  618. unsigned long long max_freq;
  619. status = acpi_evaluate_integer(ACPI_HANDLE(&slot->chip->pdev->dev),
  620. "MXFQ", NULL, &max_freq);
  621. if (ACPI_FAILURE(status)) {
  622. dev_err(&slot->chip->pdev->dev,
  623. "MXFQ not found in acpi table\n");
  624. return -EINVAL;
  625. }
  626. slot->host->mmc->f_max = max_freq * 1000000;
  627. return 0;
  628. }
  629. #else
  630. static inline int ni_set_max_freq(struct sdhci_pci_slot *slot)
  631. {
  632. return 0;
  633. }
  634. #endif
  635. static int ni_byt_sdio_probe_slot(struct sdhci_pci_slot *slot)
  636. {
  637. int err;
  638. byt_read_dsm(slot);
  639. err = ni_set_max_freq(slot);
  640. if (err)
  641. return err;
  642. slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE |
  643. MMC_CAP_WAIT_WHILE_BUSY;
  644. return 0;
  645. }
  646. static int byt_sdio_probe_slot(struct sdhci_pci_slot *slot)
  647. {
  648. byt_read_dsm(slot);
  649. slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE |
  650. MMC_CAP_WAIT_WHILE_BUSY;
  651. return 0;
  652. }
  653. static int byt_sd_probe_slot(struct sdhci_pci_slot *slot)
  654. {
  655. byt_read_dsm(slot);
  656. slot->host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY |
  657. MMC_CAP_AGGRESSIVE_PM | MMC_CAP_CD_WAKE;
  658. slot->cd_idx = 0;
  659. slot->cd_override_level = true;
  660. if (slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_BXT_SD ||
  661. slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_BXTM_SD ||
  662. slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_APL_SD ||
  663. slot->chip->pdev->device == PCI_DEVICE_ID_INTEL_GLK_SD)
  664. slot->host->mmc_host_ops.get_cd = bxt_get_cd;
  665. return 0;
  666. }
  667. static const struct sdhci_pci_fixes sdhci_intel_byt_emmc = {
  668. .allow_runtime_pm = true,
  669. .probe_slot = byt_emmc_probe_slot,
  670. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  671. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  672. SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 |
  673. SDHCI_QUIRK2_STOP_WITH_TC,
  674. .ops = &sdhci_intel_byt_ops,
  675. .priv_size = sizeof(struct intel_host),
  676. };
  677. static const struct sdhci_pci_fixes sdhci_intel_glk_emmc = {
  678. .allow_runtime_pm = true,
  679. .probe_slot = glk_emmc_probe_slot,
  680. .add_host = glk_emmc_add_host,
  681. #ifdef CONFIG_PM_SLEEP
  682. .suspend = sdhci_cqhci_suspend,
  683. .resume = sdhci_cqhci_resume,
  684. #endif
  685. #ifdef CONFIG_PM
  686. .runtime_suspend = sdhci_cqhci_runtime_suspend,
  687. .runtime_resume = sdhci_cqhci_runtime_resume,
  688. #endif
  689. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  690. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  691. SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 |
  692. SDHCI_QUIRK2_STOP_WITH_TC,
  693. .ops = &sdhci_intel_glk_ops,
  694. .priv_size = sizeof(struct intel_host),
  695. };
  696. static const struct sdhci_pci_fixes sdhci_ni_byt_sdio = {
  697. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  698. .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON |
  699. SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
  700. .allow_runtime_pm = true,
  701. .probe_slot = ni_byt_sdio_probe_slot,
  702. .ops = &sdhci_intel_byt_ops,
  703. .priv_size = sizeof(struct intel_host),
  704. };
  705. static const struct sdhci_pci_fixes sdhci_intel_byt_sdio = {
  706. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  707. .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON |
  708. SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
  709. .allow_runtime_pm = true,
  710. .probe_slot = byt_sdio_probe_slot,
  711. .ops = &sdhci_intel_byt_ops,
  712. .priv_size = sizeof(struct intel_host),
  713. };
  714. static const struct sdhci_pci_fixes sdhci_intel_byt_sd = {
  715. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  716. .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
  717. SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  718. SDHCI_QUIRK2_STOP_WITH_TC,
  719. .allow_runtime_pm = true,
  720. .own_cd_for_runtime_pm = true,
  721. .probe_slot = byt_sd_probe_slot,
  722. .ops = &sdhci_intel_byt_ops,
  723. .priv_size = sizeof(struct intel_host),
  724. };
  725. /* Define Host controllers for Intel Merrifield platform */
  726. #define INTEL_MRFLD_EMMC_0 0
  727. #define INTEL_MRFLD_EMMC_1 1
  728. #define INTEL_MRFLD_SD 2
  729. #define INTEL_MRFLD_SDIO 3
  730. #ifdef CONFIG_ACPI
  731. static void intel_mrfld_mmc_fix_up_power_slot(struct sdhci_pci_slot *slot)
  732. {
  733. struct acpi_device *device, *child;
  734. device = ACPI_COMPANION(&slot->chip->pdev->dev);
  735. if (!device)
  736. return;
  737. acpi_device_fix_up_power(device);
  738. list_for_each_entry(child, &device->children, node)
  739. if (child->status.present && child->status.enabled)
  740. acpi_device_fix_up_power(child);
  741. }
  742. #else
  743. static inline void intel_mrfld_mmc_fix_up_power_slot(struct sdhci_pci_slot *slot) {}
  744. #endif
  745. static int intel_mrfld_mmc_probe_slot(struct sdhci_pci_slot *slot)
  746. {
  747. unsigned int func = PCI_FUNC(slot->chip->pdev->devfn);
  748. switch (func) {
  749. case INTEL_MRFLD_EMMC_0:
  750. case INTEL_MRFLD_EMMC_1:
  751. slot->host->mmc->caps |= MMC_CAP_NONREMOVABLE |
  752. MMC_CAP_8_BIT_DATA |
  753. MMC_CAP_1_8V_DDR;
  754. break;
  755. case INTEL_MRFLD_SD:
  756. slot->host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
  757. break;
  758. case INTEL_MRFLD_SDIO:
  759. slot->host->mmc->caps |= MMC_CAP_NONREMOVABLE |
  760. MMC_CAP_POWER_OFF_CARD;
  761. break;
  762. default:
  763. return -ENODEV;
  764. }
  765. intel_mrfld_mmc_fix_up_power_slot(slot);
  766. return 0;
  767. }
  768. static const struct sdhci_pci_fixes sdhci_intel_mrfld_mmc = {
  769. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  770. .quirks2 = SDHCI_QUIRK2_BROKEN_HS200 |
  771. SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
  772. .allow_runtime_pm = true,
  773. .probe_slot = intel_mrfld_mmc_probe_slot,
  774. };
  775. static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
  776. {
  777. u8 scratch;
  778. int ret;
  779. ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
  780. if (ret)
  781. return ret;
  782. /*
  783. * Turn PMOS on [bit 0], set over current detection to 2.4 V
  784. * [bit 1:2] and enable over current debouncing [bit 6].
  785. */
  786. if (on)
  787. scratch |= 0x47;
  788. else
  789. scratch &= ~0x47;
  790. return pci_write_config_byte(chip->pdev, 0xAE, scratch);
  791. }
  792. static int jmicron_probe(struct sdhci_pci_chip *chip)
  793. {
  794. int ret;
  795. u16 mmcdev = 0;
  796. if (chip->pdev->revision == 0) {
  797. chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
  798. SDHCI_QUIRK_32BIT_DMA_SIZE |
  799. SDHCI_QUIRK_32BIT_ADMA_SIZE |
  800. SDHCI_QUIRK_RESET_AFTER_REQUEST |
  801. SDHCI_QUIRK_BROKEN_SMALL_PIO;
  802. }
  803. /*
  804. * JMicron chips can have two interfaces to the same hardware
  805. * in order to work around limitations in Microsoft's driver.
  806. * We need to make sure we only bind to one of them.
  807. *
  808. * This code assumes two things:
  809. *
  810. * 1. The PCI code adds subfunctions in order.
  811. *
  812. * 2. The MMC interface has a lower subfunction number
  813. * than the SD interface.
  814. */
  815. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
  816. mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
  817. else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
  818. mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
  819. if (mmcdev) {
  820. struct pci_dev *sd_dev;
  821. sd_dev = NULL;
  822. while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
  823. mmcdev, sd_dev)) != NULL) {
  824. if ((PCI_SLOT(chip->pdev->devfn) ==
  825. PCI_SLOT(sd_dev->devfn)) &&
  826. (chip->pdev->bus == sd_dev->bus))
  827. break;
  828. }
  829. if (sd_dev) {
  830. pci_dev_put(sd_dev);
  831. dev_info(&chip->pdev->dev, "Refusing to bind to "
  832. "secondary interface.\n");
  833. return -ENODEV;
  834. }
  835. }
  836. /*
  837. * JMicron chips need a bit of a nudge to enable the power
  838. * output pins.
  839. */
  840. ret = jmicron_pmos(chip, 1);
  841. if (ret) {
  842. dev_err(&chip->pdev->dev, "Failure enabling card power\n");
  843. return ret;
  844. }
  845. /* quirk for unsable RO-detection on JM388 chips */
  846. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
  847. chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
  848. chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
  849. return 0;
  850. }
  851. static void jmicron_enable_mmc(struct sdhci_host *host, int on)
  852. {
  853. u8 scratch;
  854. scratch = readb(host->ioaddr + 0xC0);
  855. if (on)
  856. scratch |= 0x01;
  857. else
  858. scratch &= ~0x01;
  859. writeb(scratch, host->ioaddr + 0xC0);
  860. }
  861. static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
  862. {
  863. if (slot->chip->pdev->revision == 0) {
  864. u16 version;
  865. version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
  866. version = (version & SDHCI_VENDOR_VER_MASK) >>
  867. SDHCI_VENDOR_VER_SHIFT;
  868. /*
  869. * Older versions of the chip have lots of nasty glitches
  870. * in the ADMA engine. It's best just to avoid it
  871. * completely.
  872. */
  873. if (version < 0xAC)
  874. slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
  875. }
  876. /* JM388 MMC doesn't support 1.8V while SD supports it */
  877. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
  878. slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
  879. MMC_VDD_29_30 | MMC_VDD_30_31 |
  880. MMC_VDD_165_195; /* allow 1.8V */
  881. slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
  882. MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
  883. }
  884. /*
  885. * The secondary interface requires a bit set to get the
  886. * interrupts.
  887. */
  888. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  889. slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
  890. jmicron_enable_mmc(slot->host, 1);
  891. slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
  892. return 0;
  893. }
  894. static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
  895. {
  896. if (dead)
  897. return;
  898. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  899. slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
  900. jmicron_enable_mmc(slot->host, 0);
  901. }
  902. #ifdef CONFIG_PM_SLEEP
  903. static int jmicron_suspend(struct sdhci_pci_chip *chip)
  904. {
  905. int i, ret;
  906. ret = __sdhci_pci_suspend_host(chip);
  907. if (ret)
  908. return ret;
  909. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  910. chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
  911. for (i = 0; i < chip->num_slots; i++)
  912. jmicron_enable_mmc(chip->slots[i]->host, 0);
  913. }
  914. sdhci_pci_init_wakeup(chip);
  915. return 0;
  916. }
  917. static int jmicron_resume(struct sdhci_pci_chip *chip)
  918. {
  919. int ret, i;
  920. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  921. chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
  922. for (i = 0; i < chip->num_slots; i++)
  923. jmicron_enable_mmc(chip->slots[i]->host, 1);
  924. }
  925. ret = jmicron_pmos(chip, 1);
  926. if (ret) {
  927. dev_err(&chip->pdev->dev, "Failure enabling card power\n");
  928. return ret;
  929. }
  930. return sdhci_pci_resume_host(chip);
  931. }
  932. #endif
  933. static const struct sdhci_pci_fixes sdhci_o2 = {
  934. .probe = sdhci_pci_o2_probe,
  935. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  936. .quirks2 = SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD,
  937. .probe_slot = sdhci_pci_o2_probe_slot,
  938. #ifdef CONFIG_PM_SLEEP
  939. .resume = sdhci_pci_o2_resume,
  940. #endif
  941. };
  942. static const struct sdhci_pci_fixes sdhci_jmicron = {
  943. .probe = jmicron_probe,
  944. .probe_slot = jmicron_probe_slot,
  945. .remove_slot = jmicron_remove_slot,
  946. #ifdef CONFIG_PM_SLEEP
  947. .suspend = jmicron_suspend,
  948. .resume = jmicron_resume,
  949. #endif
  950. };
  951. /* SysKonnect CardBus2SDIO extra registers */
  952. #define SYSKT_CTRL 0x200
  953. #define SYSKT_RDFIFO_STAT 0x204
  954. #define SYSKT_WRFIFO_STAT 0x208
  955. #define SYSKT_POWER_DATA 0x20c
  956. #define SYSKT_POWER_330 0xef
  957. #define SYSKT_POWER_300 0xf8
  958. #define SYSKT_POWER_184 0xcc
  959. #define SYSKT_POWER_CMD 0x20d
  960. #define SYSKT_POWER_START (1 << 7)
  961. #define SYSKT_POWER_STATUS 0x20e
  962. #define SYSKT_POWER_STATUS_OK (1 << 0)
  963. #define SYSKT_BOARD_REV 0x210
  964. #define SYSKT_CHIP_REV 0x211
  965. #define SYSKT_CONF_DATA 0x212
  966. #define SYSKT_CONF_DATA_1V8 (1 << 2)
  967. #define SYSKT_CONF_DATA_2V5 (1 << 1)
  968. #define SYSKT_CONF_DATA_3V3 (1 << 0)
  969. static int syskt_probe(struct sdhci_pci_chip *chip)
  970. {
  971. if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
  972. chip->pdev->class &= ~0x0000FF;
  973. chip->pdev->class |= PCI_SDHCI_IFDMA;
  974. }
  975. return 0;
  976. }
  977. static int syskt_probe_slot(struct sdhci_pci_slot *slot)
  978. {
  979. int tm, ps;
  980. u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
  981. u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
  982. dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
  983. "board rev %d.%d, chip rev %d.%d\n",
  984. board_rev >> 4, board_rev & 0xf,
  985. chip_rev >> 4, chip_rev & 0xf);
  986. if (chip_rev >= 0x20)
  987. slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
  988. writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
  989. writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
  990. udelay(50);
  991. tm = 10; /* Wait max 1 ms */
  992. do {
  993. ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
  994. if (ps & SYSKT_POWER_STATUS_OK)
  995. break;
  996. udelay(100);
  997. } while (--tm);
  998. if (!tm) {
  999. dev_err(&slot->chip->pdev->dev,
  1000. "power regulator never stabilized");
  1001. writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
  1002. return -ENODEV;
  1003. }
  1004. return 0;
  1005. }
  1006. static const struct sdhci_pci_fixes sdhci_syskt = {
  1007. .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
  1008. .probe = syskt_probe,
  1009. .probe_slot = syskt_probe_slot,
  1010. };
  1011. static int via_probe(struct sdhci_pci_chip *chip)
  1012. {
  1013. if (chip->pdev->revision == 0x10)
  1014. chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
  1015. return 0;
  1016. }
  1017. static const struct sdhci_pci_fixes sdhci_via = {
  1018. .probe = via_probe,
  1019. };
  1020. static int rtsx_probe_slot(struct sdhci_pci_slot *slot)
  1021. {
  1022. slot->host->mmc->caps2 |= MMC_CAP2_HS200;
  1023. return 0;
  1024. }
  1025. static const struct sdhci_pci_fixes sdhci_rtsx = {
  1026. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  1027. SDHCI_QUIRK2_BROKEN_64_BIT_DMA |
  1028. SDHCI_QUIRK2_BROKEN_DDR50,
  1029. .probe_slot = rtsx_probe_slot,
  1030. };
  1031. /*AMD chipset generation*/
  1032. enum amd_chipset_gen {
  1033. AMD_CHIPSET_BEFORE_ML,
  1034. AMD_CHIPSET_CZ,
  1035. AMD_CHIPSET_NL,
  1036. AMD_CHIPSET_UNKNOWN,
  1037. };
  1038. /* AMD registers */
  1039. #define AMD_SD_AUTO_PATTERN 0xB8
  1040. #define AMD_MSLEEP_DURATION 4
  1041. #define AMD_SD_MISC_CONTROL 0xD0
  1042. #define AMD_MAX_TUNE_VALUE 0x0B
  1043. #define AMD_AUTO_TUNE_SEL 0x10800
  1044. #define AMD_FIFO_PTR 0x30
  1045. #define AMD_BIT_MASK 0x1F
  1046. static void amd_tuning_reset(struct sdhci_host *host)
  1047. {
  1048. unsigned int val;
  1049. val = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1050. val |= SDHCI_CTRL_PRESET_VAL_ENABLE | SDHCI_CTRL_EXEC_TUNING;
  1051. sdhci_writew(host, val, SDHCI_HOST_CONTROL2);
  1052. val = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1053. val &= ~SDHCI_CTRL_EXEC_TUNING;
  1054. sdhci_writew(host, val, SDHCI_HOST_CONTROL2);
  1055. }
  1056. static void amd_config_tuning_phase(struct pci_dev *pdev, u8 phase)
  1057. {
  1058. unsigned int val;
  1059. pci_read_config_dword(pdev, AMD_SD_AUTO_PATTERN, &val);
  1060. val &= ~AMD_BIT_MASK;
  1061. val |= (AMD_AUTO_TUNE_SEL | (phase << 1));
  1062. pci_write_config_dword(pdev, AMD_SD_AUTO_PATTERN, val);
  1063. }
  1064. static void amd_enable_manual_tuning(struct pci_dev *pdev)
  1065. {
  1066. unsigned int val;
  1067. pci_read_config_dword(pdev, AMD_SD_MISC_CONTROL, &val);
  1068. val |= AMD_FIFO_PTR;
  1069. pci_write_config_dword(pdev, AMD_SD_MISC_CONTROL, val);
  1070. }
  1071. static int amd_execute_tuning(struct sdhci_host *host, u32 opcode)
  1072. {
  1073. struct sdhci_pci_slot *slot = sdhci_priv(host);
  1074. struct pci_dev *pdev = slot->chip->pdev;
  1075. u8 valid_win = 0;
  1076. u8 valid_win_max = 0;
  1077. u8 valid_win_end = 0;
  1078. u8 ctrl, tune_around;
  1079. amd_tuning_reset(host);
  1080. for (tune_around = 0; tune_around < 12; tune_around++) {
  1081. amd_config_tuning_phase(pdev, tune_around);
  1082. if (mmc_send_tuning(host->mmc, opcode, NULL)) {
  1083. valid_win = 0;
  1084. msleep(AMD_MSLEEP_DURATION);
  1085. ctrl = SDHCI_RESET_CMD | SDHCI_RESET_DATA;
  1086. sdhci_writeb(host, ctrl, SDHCI_SOFTWARE_RESET);
  1087. } else if (++valid_win > valid_win_max) {
  1088. valid_win_max = valid_win;
  1089. valid_win_end = tune_around;
  1090. }
  1091. }
  1092. if (!valid_win_max) {
  1093. dev_err(&pdev->dev, "no tuning point found\n");
  1094. return -EIO;
  1095. }
  1096. amd_config_tuning_phase(pdev, valid_win_end - valid_win_max / 2);
  1097. amd_enable_manual_tuning(pdev);
  1098. host->mmc->retune_period = 0;
  1099. return 0;
  1100. }
  1101. static int amd_probe(struct sdhci_pci_chip *chip)
  1102. {
  1103. struct pci_dev *smbus_dev;
  1104. enum amd_chipset_gen gen;
  1105. smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
  1106. PCI_DEVICE_ID_AMD_HUDSON2_SMBUS, NULL);
  1107. if (smbus_dev) {
  1108. gen = AMD_CHIPSET_BEFORE_ML;
  1109. } else {
  1110. smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
  1111. PCI_DEVICE_ID_AMD_KERNCZ_SMBUS, NULL);
  1112. if (smbus_dev) {
  1113. if (smbus_dev->revision < 0x51)
  1114. gen = AMD_CHIPSET_CZ;
  1115. else
  1116. gen = AMD_CHIPSET_NL;
  1117. } else {
  1118. gen = AMD_CHIPSET_UNKNOWN;
  1119. }
  1120. }
  1121. if (gen == AMD_CHIPSET_BEFORE_ML || gen == AMD_CHIPSET_CZ)
  1122. chip->quirks2 |= SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD;
  1123. return 0;
  1124. }
  1125. static const struct sdhci_ops amd_sdhci_pci_ops = {
  1126. .set_clock = sdhci_set_clock,
  1127. .enable_dma = sdhci_pci_enable_dma,
  1128. .set_bus_width = sdhci_set_bus_width,
  1129. .reset = sdhci_reset,
  1130. .set_uhs_signaling = sdhci_set_uhs_signaling,
  1131. .platform_execute_tuning = amd_execute_tuning,
  1132. };
  1133. static const struct sdhci_pci_fixes sdhci_amd = {
  1134. .probe = amd_probe,
  1135. .ops = &amd_sdhci_pci_ops,
  1136. };
  1137. static const struct pci_device_id pci_ids[] = {
  1138. SDHCI_PCI_DEVICE(RICOH, R5C822, ricoh),
  1139. SDHCI_PCI_DEVICE(RICOH, R5C843, ricoh_mmc),
  1140. SDHCI_PCI_DEVICE(RICOH, R5CE822, ricoh_mmc),
  1141. SDHCI_PCI_DEVICE(RICOH, R5CE823, ricoh_mmc),
  1142. SDHCI_PCI_DEVICE(ENE, CB712_SD, ene_712),
  1143. SDHCI_PCI_DEVICE(ENE, CB712_SD_2, ene_712),
  1144. SDHCI_PCI_DEVICE(ENE, CB714_SD, ene_714),
  1145. SDHCI_PCI_DEVICE(ENE, CB714_SD_2, ene_714),
  1146. SDHCI_PCI_DEVICE(MARVELL, 88ALP01_SD, cafe),
  1147. SDHCI_PCI_DEVICE(JMICRON, JMB38X_SD, jmicron),
  1148. SDHCI_PCI_DEVICE(JMICRON, JMB38X_MMC, jmicron),
  1149. SDHCI_PCI_DEVICE(JMICRON, JMB388_SD, jmicron),
  1150. SDHCI_PCI_DEVICE(JMICRON, JMB388_ESD, jmicron),
  1151. SDHCI_PCI_DEVICE(SYSKONNECT, 8000, syskt),
  1152. SDHCI_PCI_DEVICE(VIA, 95D0, via),
  1153. SDHCI_PCI_DEVICE(REALTEK, 5250, rtsx),
  1154. SDHCI_PCI_DEVICE(INTEL, QRK_SD, intel_qrk),
  1155. SDHCI_PCI_DEVICE(INTEL, MRST_SD0, intel_mrst_hc0),
  1156. SDHCI_PCI_DEVICE(INTEL, MRST_SD1, intel_mrst_hc1_hc2),
  1157. SDHCI_PCI_DEVICE(INTEL, MRST_SD2, intel_mrst_hc1_hc2),
  1158. SDHCI_PCI_DEVICE(INTEL, MFD_SD, intel_mfd_sd),
  1159. SDHCI_PCI_DEVICE(INTEL, MFD_SDIO1, intel_mfd_sdio),
  1160. SDHCI_PCI_DEVICE(INTEL, MFD_SDIO2, intel_mfd_sdio),
  1161. SDHCI_PCI_DEVICE(INTEL, MFD_EMMC0, intel_mfd_emmc),
  1162. SDHCI_PCI_DEVICE(INTEL, MFD_EMMC1, intel_mfd_emmc),
  1163. SDHCI_PCI_DEVICE(INTEL, PCH_SDIO0, intel_pch_sdio),
  1164. SDHCI_PCI_DEVICE(INTEL, PCH_SDIO1, intel_pch_sdio),
  1165. SDHCI_PCI_DEVICE(INTEL, BYT_EMMC, intel_byt_emmc),
  1166. SDHCI_PCI_SUBDEVICE(INTEL, BYT_SDIO, NI, 7884, ni_byt_sdio),
  1167. SDHCI_PCI_DEVICE(INTEL, BYT_SDIO, intel_byt_sdio),
  1168. SDHCI_PCI_DEVICE(INTEL, BYT_SD, intel_byt_sd),
  1169. SDHCI_PCI_DEVICE(INTEL, BYT_EMMC2, intel_byt_emmc),
  1170. SDHCI_PCI_DEVICE(INTEL, BSW_EMMC, intel_byt_emmc),
  1171. SDHCI_PCI_DEVICE(INTEL, BSW_SDIO, intel_byt_sdio),
  1172. SDHCI_PCI_DEVICE(INTEL, BSW_SD, intel_byt_sd),
  1173. SDHCI_PCI_DEVICE(INTEL, CLV_SDIO0, intel_mfd_sd),
  1174. SDHCI_PCI_DEVICE(INTEL, CLV_SDIO1, intel_mfd_sdio),
  1175. SDHCI_PCI_DEVICE(INTEL, CLV_SDIO2, intel_mfd_sdio),
  1176. SDHCI_PCI_DEVICE(INTEL, CLV_EMMC0, intel_mfd_emmc),
  1177. SDHCI_PCI_DEVICE(INTEL, CLV_EMMC1, intel_mfd_emmc),
  1178. SDHCI_PCI_DEVICE(INTEL, MRFLD_MMC, intel_mrfld_mmc),
  1179. SDHCI_PCI_DEVICE(INTEL, SPT_EMMC, intel_byt_emmc),
  1180. SDHCI_PCI_DEVICE(INTEL, SPT_SDIO, intel_byt_sdio),
  1181. SDHCI_PCI_DEVICE(INTEL, SPT_SD, intel_byt_sd),
  1182. SDHCI_PCI_DEVICE(INTEL, DNV_EMMC, intel_byt_emmc),
  1183. SDHCI_PCI_DEVICE(INTEL, CDF_EMMC, intel_glk_emmc),
  1184. SDHCI_PCI_DEVICE(INTEL, BXT_EMMC, intel_byt_emmc),
  1185. SDHCI_PCI_DEVICE(INTEL, BXT_SDIO, intel_byt_sdio),
  1186. SDHCI_PCI_DEVICE(INTEL, BXT_SD, intel_byt_sd),
  1187. SDHCI_PCI_DEVICE(INTEL, BXTM_EMMC, intel_byt_emmc),
  1188. SDHCI_PCI_DEVICE(INTEL, BXTM_SDIO, intel_byt_sdio),
  1189. SDHCI_PCI_DEVICE(INTEL, BXTM_SD, intel_byt_sd),
  1190. SDHCI_PCI_DEVICE(INTEL, APL_EMMC, intel_byt_emmc),
  1191. SDHCI_PCI_DEVICE(INTEL, APL_SDIO, intel_byt_sdio),
  1192. SDHCI_PCI_DEVICE(INTEL, APL_SD, intel_byt_sd),
  1193. SDHCI_PCI_DEVICE(INTEL, GLK_EMMC, intel_glk_emmc),
  1194. SDHCI_PCI_DEVICE(INTEL, GLK_SDIO, intel_byt_sdio),
  1195. SDHCI_PCI_DEVICE(INTEL, GLK_SD, intel_byt_sd),
  1196. SDHCI_PCI_DEVICE(INTEL, CNP_EMMC, intel_glk_emmc),
  1197. SDHCI_PCI_DEVICE(INTEL, CNP_SD, intel_byt_sd),
  1198. SDHCI_PCI_DEVICE(INTEL, CNPH_SD, intel_byt_sd),
  1199. SDHCI_PCI_DEVICE(O2, 8120, o2),
  1200. SDHCI_PCI_DEVICE(O2, 8220, o2),
  1201. SDHCI_PCI_DEVICE(O2, 8221, o2),
  1202. SDHCI_PCI_DEVICE(O2, 8320, o2),
  1203. SDHCI_PCI_DEVICE(O2, 8321, o2),
  1204. SDHCI_PCI_DEVICE(O2, FUJIN2, o2),
  1205. SDHCI_PCI_DEVICE(O2, SDS0, o2),
  1206. SDHCI_PCI_DEVICE(O2, SDS1, o2),
  1207. SDHCI_PCI_DEVICE(O2, SEABIRD0, o2),
  1208. SDHCI_PCI_DEVICE(O2, SEABIRD1, o2),
  1209. SDHCI_PCI_DEVICE_CLASS(AMD, SYSTEM_SDHCI, PCI_CLASS_MASK, amd),
  1210. /* Generic SD host controller */
  1211. {PCI_DEVICE_CLASS(SYSTEM_SDHCI, PCI_CLASS_MASK)},
  1212. { /* end: all zeroes */ },
  1213. };
  1214. MODULE_DEVICE_TABLE(pci, pci_ids);
  1215. /*****************************************************************************\
  1216. * *
  1217. * SDHCI core callbacks *
  1218. * *
  1219. \*****************************************************************************/
  1220. static int sdhci_pci_enable_dma(struct sdhci_host *host)
  1221. {
  1222. struct sdhci_pci_slot *slot;
  1223. struct pci_dev *pdev;
  1224. slot = sdhci_priv(host);
  1225. pdev = slot->chip->pdev;
  1226. if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
  1227. ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
  1228. (host->flags & SDHCI_USE_SDMA)) {
  1229. dev_warn(&pdev->dev, "Will use DMA mode even though HW "
  1230. "doesn't fully claim to support it.\n");
  1231. }
  1232. pci_set_master(pdev);
  1233. return 0;
  1234. }
  1235. static void sdhci_pci_gpio_hw_reset(struct sdhci_host *host)
  1236. {
  1237. struct sdhci_pci_slot *slot = sdhci_priv(host);
  1238. int rst_n_gpio = slot->rst_n_gpio;
  1239. if (!gpio_is_valid(rst_n_gpio))
  1240. return;
  1241. gpio_set_value_cansleep(rst_n_gpio, 0);
  1242. /* For eMMC, minimum is 1us but give it 10us for good measure */
  1243. udelay(10);
  1244. gpio_set_value_cansleep(rst_n_gpio, 1);
  1245. /* For eMMC, minimum is 200us but give it 300us for good measure */
  1246. usleep_range(300, 1000);
  1247. }
  1248. static void sdhci_pci_hw_reset(struct sdhci_host *host)
  1249. {
  1250. struct sdhci_pci_slot *slot = sdhci_priv(host);
  1251. if (slot->hw_reset)
  1252. slot->hw_reset(host);
  1253. }
  1254. static const struct sdhci_ops sdhci_pci_ops = {
  1255. .set_clock = sdhci_set_clock,
  1256. .enable_dma = sdhci_pci_enable_dma,
  1257. .set_bus_width = sdhci_set_bus_width,
  1258. .reset = sdhci_reset,
  1259. .set_uhs_signaling = sdhci_set_uhs_signaling,
  1260. .hw_reset = sdhci_pci_hw_reset,
  1261. };
  1262. /*****************************************************************************\
  1263. * *
  1264. * Suspend/resume *
  1265. * *
  1266. \*****************************************************************************/
  1267. #ifdef CONFIG_PM_SLEEP
  1268. static int sdhci_pci_suspend(struct device *dev)
  1269. {
  1270. struct pci_dev *pdev = to_pci_dev(dev);
  1271. struct sdhci_pci_chip *chip = pci_get_drvdata(pdev);
  1272. if (!chip)
  1273. return 0;
  1274. if (chip->fixes && chip->fixes->suspend)
  1275. return chip->fixes->suspend(chip);
  1276. return sdhci_pci_suspend_host(chip);
  1277. }
  1278. static int sdhci_pci_resume(struct device *dev)
  1279. {
  1280. struct pci_dev *pdev = to_pci_dev(dev);
  1281. struct sdhci_pci_chip *chip = pci_get_drvdata(pdev);
  1282. if (!chip)
  1283. return 0;
  1284. if (chip->fixes && chip->fixes->resume)
  1285. return chip->fixes->resume(chip);
  1286. return sdhci_pci_resume_host(chip);
  1287. }
  1288. #endif
  1289. #ifdef CONFIG_PM
  1290. static int sdhci_pci_runtime_suspend(struct device *dev)
  1291. {
  1292. struct pci_dev *pdev = to_pci_dev(dev);
  1293. struct sdhci_pci_chip *chip = pci_get_drvdata(pdev);
  1294. if (!chip)
  1295. return 0;
  1296. if (chip->fixes && chip->fixes->runtime_suspend)
  1297. return chip->fixes->runtime_suspend(chip);
  1298. return sdhci_pci_runtime_suspend_host(chip);
  1299. }
  1300. static int sdhci_pci_runtime_resume(struct device *dev)
  1301. {
  1302. struct pci_dev *pdev = to_pci_dev(dev);
  1303. struct sdhci_pci_chip *chip = pci_get_drvdata(pdev);
  1304. if (!chip)
  1305. return 0;
  1306. if (chip->fixes && chip->fixes->runtime_resume)
  1307. return chip->fixes->runtime_resume(chip);
  1308. return sdhci_pci_runtime_resume_host(chip);
  1309. }
  1310. #endif
  1311. static const struct dev_pm_ops sdhci_pci_pm_ops = {
  1312. SET_SYSTEM_SLEEP_PM_OPS(sdhci_pci_suspend, sdhci_pci_resume)
  1313. SET_RUNTIME_PM_OPS(sdhci_pci_runtime_suspend,
  1314. sdhci_pci_runtime_resume, NULL)
  1315. };
  1316. /*****************************************************************************\
  1317. * *
  1318. * Device probing/removal *
  1319. * *
  1320. \*****************************************************************************/
  1321. static struct sdhci_pci_slot *sdhci_pci_probe_slot(
  1322. struct pci_dev *pdev, struct sdhci_pci_chip *chip, int first_bar,
  1323. int slotno)
  1324. {
  1325. struct sdhci_pci_slot *slot;
  1326. struct sdhci_host *host;
  1327. int ret, bar = first_bar + slotno;
  1328. size_t priv_size = chip->fixes ? chip->fixes->priv_size : 0;
  1329. if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
  1330. dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
  1331. return ERR_PTR(-ENODEV);
  1332. }
  1333. if (pci_resource_len(pdev, bar) < 0x100) {
  1334. dev_err(&pdev->dev, "Invalid iomem size. You may "
  1335. "experience problems.\n");
  1336. }
  1337. if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
  1338. dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
  1339. return ERR_PTR(-ENODEV);
  1340. }
  1341. if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
  1342. dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
  1343. return ERR_PTR(-ENODEV);
  1344. }
  1345. host = sdhci_alloc_host(&pdev->dev, sizeof(*slot) + priv_size);
  1346. if (IS_ERR(host)) {
  1347. dev_err(&pdev->dev, "cannot allocate host\n");
  1348. return ERR_CAST(host);
  1349. }
  1350. slot = sdhci_priv(host);
  1351. slot->chip = chip;
  1352. slot->host = host;
  1353. slot->rst_n_gpio = -EINVAL;
  1354. slot->cd_gpio = -EINVAL;
  1355. slot->cd_idx = -1;
  1356. /* Retrieve platform data if there is any */
  1357. if (*sdhci_pci_get_data)
  1358. slot->data = sdhci_pci_get_data(pdev, slotno);
  1359. if (slot->data) {
  1360. if (slot->data->setup) {
  1361. ret = slot->data->setup(slot->data);
  1362. if (ret) {
  1363. dev_err(&pdev->dev, "platform setup failed\n");
  1364. goto free;
  1365. }
  1366. }
  1367. slot->rst_n_gpio = slot->data->rst_n_gpio;
  1368. slot->cd_gpio = slot->data->cd_gpio;
  1369. }
  1370. host->hw_name = "PCI";
  1371. host->ops = chip->fixes && chip->fixes->ops ?
  1372. chip->fixes->ops :
  1373. &sdhci_pci_ops;
  1374. host->quirks = chip->quirks;
  1375. host->quirks2 = chip->quirks2;
  1376. host->irq = pdev->irq;
  1377. ret = pcim_iomap_regions(pdev, BIT(bar), mmc_hostname(host->mmc));
  1378. if (ret) {
  1379. dev_err(&pdev->dev, "cannot request region\n");
  1380. goto cleanup;
  1381. }
  1382. host->ioaddr = pcim_iomap_table(pdev)[bar];
  1383. if (chip->fixes && chip->fixes->probe_slot) {
  1384. ret = chip->fixes->probe_slot(slot);
  1385. if (ret)
  1386. goto cleanup;
  1387. }
  1388. if (gpio_is_valid(slot->rst_n_gpio)) {
  1389. if (!devm_gpio_request(&pdev->dev, slot->rst_n_gpio, "eMMC_reset")) {
  1390. gpio_direction_output(slot->rst_n_gpio, 1);
  1391. slot->host->mmc->caps |= MMC_CAP_HW_RESET;
  1392. slot->hw_reset = sdhci_pci_gpio_hw_reset;
  1393. } else {
  1394. dev_warn(&pdev->dev, "failed to request rst_n_gpio\n");
  1395. slot->rst_n_gpio = -EINVAL;
  1396. }
  1397. }
  1398. host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
  1399. host->mmc->slotno = slotno;
  1400. host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
  1401. if (slot->cd_idx >= 0) {
  1402. ret = mmc_gpiod_request_cd(host->mmc, NULL, slot->cd_idx,
  1403. slot->cd_override_level, 0, NULL);
  1404. if (ret == -EPROBE_DEFER)
  1405. goto remove;
  1406. if (ret) {
  1407. dev_warn(&pdev->dev, "failed to setup card detect gpio\n");
  1408. slot->cd_idx = -1;
  1409. }
  1410. }
  1411. if (chip->fixes && chip->fixes->add_host)
  1412. ret = chip->fixes->add_host(slot);
  1413. else
  1414. ret = sdhci_add_host(host);
  1415. if (ret)
  1416. goto remove;
  1417. sdhci_pci_add_own_cd(slot);
  1418. /*
  1419. * Check if the chip needs a separate GPIO for card detect to wake up
  1420. * from runtime suspend. If it is not there, don't allow runtime PM.
  1421. * Note sdhci_pci_add_own_cd() sets slot->cd_gpio to -EINVAL on failure.
  1422. */
  1423. if (chip->fixes && chip->fixes->own_cd_for_runtime_pm &&
  1424. !gpio_is_valid(slot->cd_gpio) && slot->cd_idx < 0)
  1425. chip->allow_runtime_pm = false;
  1426. return slot;
  1427. remove:
  1428. if (chip->fixes && chip->fixes->remove_slot)
  1429. chip->fixes->remove_slot(slot, 0);
  1430. cleanup:
  1431. if (slot->data && slot->data->cleanup)
  1432. slot->data->cleanup(slot->data);
  1433. free:
  1434. sdhci_free_host(host);
  1435. return ERR_PTR(ret);
  1436. }
  1437. static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
  1438. {
  1439. int dead;
  1440. u32 scratch;
  1441. sdhci_pci_remove_own_cd(slot);
  1442. dead = 0;
  1443. scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
  1444. if (scratch == (u32)-1)
  1445. dead = 1;
  1446. sdhci_remove_host(slot->host, dead);
  1447. if (slot->chip->fixes && slot->chip->fixes->remove_slot)
  1448. slot->chip->fixes->remove_slot(slot, dead);
  1449. if (slot->data && slot->data->cleanup)
  1450. slot->data->cleanup(slot->data);
  1451. sdhci_free_host(slot->host);
  1452. }
  1453. static void sdhci_pci_runtime_pm_allow(struct device *dev)
  1454. {
  1455. pm_suspend_ignore_children(dev, 1);
  1456. pm_runtime_set_autosuspend_delay(dev, 50);
  1457. pm_runtime_use_autosuspend(dev);
  1458. pm_runtime_allow(dev);
  1459. /* Stay active until mmc core scans for a card */
  1460. pm_runtime_put_noidle(dev);
  1461. }
  1462. static void sdhci_pci_runtime_pm_forbid(struct device *dev)
  1463. {
  1464. pm_runtime_forbid(dev);
  1465. pm_runtime_get_noresume(dev);
  1466. }
  1467. static int sdhci_pci_probe(struct pci_dev *pdev,
  1468. const struct pci_device_id *ent)
  1469. {
  1470. struct sdhci_pci_chip *chip;
  1471. struct sdhci_pci_slot *slot;
  1472. u8 slots, first_bar;
  1473. int ret, i;
  1474. BUG_ON(pdev == NULL);
  1475. BUG_ON(ent == NULL);
  1476. dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
  1477. (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
  1478. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
  1479. if (ret)
  1480. return ret;
  1481. slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
  1482. dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
  1483. if (slots == 0)
  1484. return -ENODEV;
  1485. BUG_ON(slots > MAX_SLOTS);
  1486. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
  1487. if (ret)
  1488. return ret;
  1489. first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
  1490. if (first_bar > 5) {
  1491. dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
  1492. return -ENODEV;
  1493. }
  1494. ret = pcim_enable_device(pdev);
  1495. if (ret)
  1496. return ret;
  1497. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  1498. if (!chip)
  1499. return -ENOMEM;
  1500. chip->pdev = pdev;
  1501. chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
  1502. if (chip->fixes) {
  1503. chip->quirks = chip->fixes->quirks;
  1504. chip->quirks2 = chip->fixes->quirks2;
  1505. chip->allow_runtime_pm = chip->fixes->allow_runtime_pm;
  1506. }
  1507. chip->num_slots = slots;
  1508. chip->pm_retune = true;
  1509. chip->rpm_retune = true;
  1510. pci_set_drvdata(pdev, chip);
  1511. if (chip->fixes && chip->fixes->probe) {
  1512. ret = chip->fixes->probe(chip);
  1513. if (ret)
  1514. return ret;
  1515. }
  1516. slots = chip->num_slots; /* Quirk may have changed this */
  1517. for (i = 0; i < slots; i++) {
  1518. slot = sdhci_pci_probe_slot(pdev, chip, first_bar, i);
  1519. if (IS_ERR(slot)) {
  1520. for (i--; i >= 0; i--)
  1521. sdhci_pci_remove_slot(chip->slots[i]);
  1522. return PTR_ERR(slot);
  1523. }
  1524. chip->slots[i] = slot;
  1525. }
  1526. if (chip->allow_runtime_pm)
  1527. sdhci_pci_runtime_pm_allow(&pdev->dev);
  1528. return 0;
  1529. }
  1530. static void sdhci_pci_remove(struct pci_dev *pdev)
  1531. {
  1532. int i;
  1533. struct sdhci_pci_chip *chip = pci_get_drvdata(pdev);
  1534. if (chip->allow_runtime_pm)
  1535. sdhci_pci_runtime_pm_forbid(&pdev->dev);
  1536. for (i = 0; i < chip->num_slots; i++)
  1537. sdhci_pci_remove_slot(chip->slots[i]);
  1538. }
  1539. static struct pci_driver sdhci_driver = {
  1540. .name = "sdhci-pci",
  1541. .id_table = pci_ids,
  1542. .probe = sdhci_pci_probe,
  1543. .remove = sdhci_pci_remove,
  1544. .driver = {
  1545. .pm = &sdhci_pci_pm_ops
  1546. },
  1547. };
  1548. module_pci_driver(sdhci_driver);
  1549. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  1550. MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
  1551. MODULE_LICENSE("GPL");