sdhci-pci-core.c 52 KB

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