sdhci-pci-core.c 52 KB

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