sdhci-pci-core.c 51 KB

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