sdhci-pci-core.c 45 KB

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