sdhci-pci-core.c 48 KB

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