sdhci-pci-core.c 49 KB

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