sdhci-pci.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  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/delay.h>
  15. #include <linux/highmem.h>
  16. #include <linux/module.h>
  17. #include <linux/pci.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/slab.h>
  20. #include <linux/device.h>
  21. #include <linux/mmc/host.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/io.h>
  24. #include <linux/gpio.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/mmc/sdhci-pci-data.h>
  27. #include "sdhci.h"
  28. #include "sdhci-pci.h"
  29. #include "sdhci-pci-o2micro.h"
  30. /*****************************************************************************\
  31. * *
  32. * Hardware specific quirk handling *
  33. * *
  34. \*****************************************************************************/
  35. static int ricoh_probe(struct sdhci_pci_chip *chip)
  36. {
  37. if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
  38. chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
  39. chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
  40. return 0;
  41. }
  42. static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
  43. {
  44. slot->host->caps =
  45. ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
  46. & SDHCI_TIMEOUT_CLK_MASK) |
  47. ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
  48. & SDHCI_CLOCK_BASE_MASK) |
  49. SDHCI_TIMEOUT_CLK_UNIT |
  50. SDHCI_CAN_VDD_330 |
  51. SDHCI_CAN_DO_HISPD |
  52. SDHCI_CAN_DO_SDMA;
  53. return 0;
  54. }
  55. static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
  56. {
  57. /* Apply a delay to allow controller to settle */
  58. /* Otherwise it becomes confused if card state changed
  59. during suspend */
  60. msleep(500);
  61. return 0;
  62. }
  63. static const struct sdhci_pci_fixes sdhci_ricoh = {
  64. .probe = ricoh_probe,
  65. .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
  66. SDHCI_QUIRK_FORCE_DMA |
  67. SDHCI_QUIRK_CLOCK_BEFORE_RESET,
  68. };
  69. static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
  70. .probe_slot = ricoh_mmc_probe_slot,
  71. .resume = ricoh_mmc_resume,
  72. .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
  73. SDHCI_QUIRK_CLOCK_BEFORE_RESET |
  74. SDHCI_QUIRK_NO_CARD_NO_RESET |
  75. SDHCI_QUIRK_MISSING_CAPS
  76. };
  77. static const struct sdhci_pci_fixes sdhci_ene_712 = {
  78. .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
  79. SDHCI_QUIRK_BROKEN_DMA,
  80. };
  81. static const struct sdhci_pci_fixes sdhci_ene_714 = {
  82. .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
  83. SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
  84. SDHCI_QUIRK_BROKEN_DMA,
  85. };
  86. static const struct sdhci_pci_fixes sdhci_cafe = {
  87. .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
  88. SDHCI_QUIRK_NO_BUSY_IRQ |
  89. SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  90. SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
  91. };
  92. static int mrst_hc_probe_slot(struct sdhci_pci_slot *slot)
  93. {
  94. slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  95. return 0;
  96. }
  97. /*
  98. * ADMA operation is disabled for Moorestown platform due to
  99. * hardware bugs.
  100. */
  101. static int mrst_hc_probe(struct sdhci_pci_chip *chip)
  102. {
  103. /*
  104. * slots number is fixed here for MRST as SDIO3/5 are never used and
  105. * have hardware bugs.
  106. */
  107. chip->num_slots = 1;
  108. return 0;
  109. }
  110. static int pch_hc_probe_slot(struct sdhci_pci_slot *slot)
  111. {
  112. slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  113. return 0;
  114. }
  115. #ifdef CONFIG_PM_RUNTIME
  116. static irqreturn_t sdhci_pci_sd_cd(int irq, void *dev_id)
  117. {
  118. struct sdhci_pci_slot *slot = dev_id;
  119. struct sdhci_host *host = slot->host;
  120. mmc_detect_change(host->mmc, msecs_to_jiffies(200));
  121. return IRQ_HANDLED;
  122. }
  123. static void sdhci_pci_add_own_cd(struct sdhci_pci_slot *slot)
  124. {
  125. int err, irq, gpio = slot->cd_gpio;
  126. slot->cd_gpio = -EINVAL;
  127. slot->cd_irq = -EINVAL;
  128. if (!gpio_is_valid(gpio))
  129. return;
  130. err = gpio_request(gpio, "sd_cd");
  131. if (err < 0)
  132. goto out;
  133. err = gpio_direction_input(gpio);
  134. if (err < 0)
  135. goto out_free;
  136. irq = gpio_to_irq(gpio);
  137. if (irq < 0)
  138. goto out_free;
  139. err = request_irq(irq, sdhci_pci_sd_cd, IRQF_TRIGGER_RISING |
  140. IRQF_TRIGGER_FALLING, "sd_cd", slot);
  141. if (err)
  142. goto out_free;
  143. slot->cd_gpio = gpio;
  144. slot->cd_irq = irq;
  145. return;
  146. out_free:
  147. gpio_free(gpio);
  148. out:
  149. dev_warn(&slot->chip->pdev->dev, "failed to setup card detect wake up\n");
  150. }
  151. static void sdhci_pci_remove_own_cd(struct sdhci_pci_slot *slot)
  152. {
  153. if (slot->cd_irq >= 0)
  154. free_irq(slot->cd_irq, slot);
  155. if (gpio_is_valid(slot->cd_gpio))
  156. gpio_free(slot->cd_gpio);
  157. }
  158. #else
  159. static inline void sdhci_pci_add_own_cd(struct sdhci_pci_slot *slot)
  160. {
  161. }
  162. static inline void sdhci_pci_remove_own_cd(struct sdhci_pci_slot *slot)
  163. {
  164. }
  165. #endif
  166. static int mfd_emmc_probe_slot(struct sdhci_pci_slot *slot)
  167. {
  168. slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE;
  169. slot->host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC |
  170. MMC_CAP2_HC_ERASE_SZ;
  171. return 0;
  172. }
  173. static int mfd_sdio_probe_slot(struct sdhci_pci_slot *slot)
  174. {
  175. slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE;
  176. return 0;
  177. }
  178. static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
  179. .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
  180. .probe_slot = mrst_hc_probe_slot,
  181. };
  182. static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1_hc2 = {
  183. .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
  184. .probe = mrst_hc_probe,
  185. };
  186. static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
  187. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  188. .allow_runtime_pm = true,
  189. .own_cd_for_runtime_pm = true,
  190. };
  191. static const struct sdhci_pci_fixes sdhci_intel_mfd_sdio = {
  192. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  193. .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
  194. .allow_runtime_pm = true,
  195. .probe_slot = mfd_sdio_probe_slot,
  196. };
  197. static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc = {
  198. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  199. .allow_runtime_pm = true,
  200. .probe_slot = mfd_emmc_probe_slot,
  201. };
  202. static const struct sdhci_pci_fixes sdhci_intel_pch_sdio = {
  203. .quirks = SDHCI_QUIRK_BROKEN_ADMA,
  204. .probe_slot = pch_hc_probe_slot,
  205. };
  206. static void sdhci_pci_int_hw_reset(struct sdhci_host *host)
  207. {
  208. u8 reg;
  209. reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
  210. reg |= 0x10;
  211. sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
  212. /* For eMMC, minimum is 1us but give it 9us for good measure */
  213. udelay(9);
  214. reg &= ~0x10;
  215. sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
  216. /* For eMMC, minimum is 200us but give it 300us for good measure */
  217. usleep_range(300, 1000);
  218. }
  219. static int byt_emmc_probe_slot(struct sdhci_pci_slot *slot)
  220. {
  221. slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
  222. MMC_CAP_HW_RESET;
  223. slot->host->mmc->caps2 |= MMC_CAP2_HC_ERASE_SZ;
  224. slot->hw_reset = sdhci_pci_int_hw_reset;
  225. return 0;
  226. }
  227. static int byt_sdio_probe_slot(struct sdhci_pci_slot *slot)
  228. {
  229. slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE;
  230. return 0;
  231. }
  232. static const struct sdhci_pci_fixes sdhci_intel_byt_emmc = {
  233. .allow_runtime_pm = true,
  234. .probe_slot = byt_emmc_probe_slot,
  235. };
  236. static const struct sdhci_pci_fixes sdhci_intel_byt_sdio = {
  237. .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
  238. .allow_runtime_pm = true,
  239. .probe_slot = byt_sdio_probe_slot,
  240. };
  241. static const struct sdhci_pci_fixes sdhci_intel_byt_sd = {
  242. .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON,
  243. .allow_runtime_pm = true,
  244. .own_cd_for_runtime_pm = true,
  245. };
  246. /* Define Host controllers for Intel Merrifield platform */
  247. #define INTEL_MRFL_EMMC_0 0
  248. #define INTEL_MRFL_EMMC_1 1
  249. static int intel_mrfl_mmc_probe_slot(struct sdhci_pci_slot *slot)
  250. {
  251. if ((PCI_FUNC(slot->chip->pdev->devfn) != INTEL_MRFL_EMMC_0) &&
  252. (PCI_FUNC(slot->chip->pdev->devfn) != INTEL_MRFL_EMMC_1))
  253. /* SD support is not ready yet */
  254. return -ENODEV;
  255. slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
  256. MMC_CAP_1_8V_DDR;
  257. return 0;
  258. }
  259. static const struct sdhci_pci_fixes sdhci_intel_mrfl_mmc = {
  260. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  261. .quirks2 = SDHCI_QUIRK2_BROKEN_HS200,
  262. .probe_slot = intel_mrfl_mmc_probe_slot,
  263. };
  264. /* O2Micro extra registers */
  265. #define O2_SD_LOCK_WP 0xD3
  266. #define O2_SD_MULTI_VCC3V 0xEE
  267. #define O2_SD_CLKREQ 0xEC
  268. #define O2_SD_CAPS 0xE0
  269. #define O2_SD_ADMA1 0xE2
  270. #define O2_SD_ADMA2 0xE7
  271. #define O2_SD_INF_MOD 0xF1
  272. static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
  273. {
  274. u8 scratch;
  275. int ret;
  276. ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
  277. if (ret)
  278. return ret;
  279. /*
  280. * Turn PMOS on [bit 0], set over current detection to 2.4 V
  281. * [bit 1:2] and enable over current debouncing [bit 6].
  282. */
  283. if (on)
  284. scratch |= 0x47;
  285. else
  286. scratch &= ~0x47;
  287. ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
  288. if (ret)
  289. return ret;
  290. return 0;
  291. }
  292. static int jmicron_probe(struct sdhci_pci_chip *chip)
  293. {
  294. int ret;
  295. u16 mmcdev = 0;
  296. if (chip->pdev->revision == 0) {
  297. chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
  298. SDHCI_QUIRK_32BIT_DMA_SIZE |
  299. SDHCI_QUIRK_32BIT_ADMA_SIZE |
  300. SDHCI_QUIRK_RESET_AFTER_REQUEST |
  301. SDHCI_QUIRK_BROKEN_SMALL_PIO;
  302. }
  303. /*
  304. * JMicron chips can have two interfaces to the same hardware
  305. * in order to work around limitations in Microsoft's driver.
  306. * We need to make sure we only bind to one of them.
  307. *
  308. * This code assumes two things:
  309. *
  310. * 1. The PCI code adds subfunctions in order.
  311. *
  312. * 2. The MMC interface has a lower subfunction number
  313. * than the SD interface.
  314. */
  315. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
  316. mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
  317. else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
  318. mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
  319. if (mmcdev) {
  320. struct pci_dev *sd_dev;
  321. sd_dev = NULL;
  322. while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
  323. mmcdev, sd_dev)) != NULL) {
  324. if ((PCI_SLOT(chip->pdev->devfn) ==
  325. PCI_SLOT(sd_dev->devfn)) &&
  326. (chip->pdev->bus == sd_dev->bus))
  327. break;
  328. }
  329. if (sd_dev) {
  330. pci_dev_put(sd_dev);
  331. dev_info(&chip->pdev->dev, "Refusing to bind to "
  332. "secondary interface.\n");
  333. return -ENODEV;
  334. }
  335. }
  336. /*
  337. * JMicron chips need a bit of a nudge to enable the power
  338. * output pins.
  339. */
  340. ret = jmicron_pmos(chip, 1);
  341. if (ret) {
  342. dev_err(&chip->pdev->dev, "Failure enabling card power\n");
  343. return ret;
  344. }
  345. /* quirk for unsable RO-detection on JM388 chips */
  346. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
  347. chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
  348. chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
  349. return 0;
  350. }
  351. static void jmicron_enable_mmc(struct sdhci_host *host, int on)
  352. {
  353. u8 scratch;
  354. scratch = readb(host->ioaddr + 0xC0);
  355. if (on)
  356. scratch |= 0x01;
  357. else
  358. scratch &= ~0x01;
  359. writeb(scratch, host->ioaddr + 0xC0);
  360. }
  361. static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
  362. {
  363. if (slot->chip->pdev->revision == 0) {
  364. u16 version;
  365. version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
  366. version = (version & SDHCI_VENDOR_VER_MASK) >>
  367. SDHCI_VENDOR_VER_SHIFT;
  368. /*
  369. * Older versions of the chip have lots of nasty glitches
  370. * in the ADMA engine. It's best just to avoid it
  371. * completely.
  372. */
  373. if (version < 0xAC)
  374. slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
  375. }
  376. /* JM388 MMC doesn't support 1.8V while SD supports it */
  377. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
  378. slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
  379. MMC_VDD_29_30 | MMC_VDD_30_31 |
  380. MMC_VDD_165_195; /* allow 1.8V */
  381. slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
  382. MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
  383. }
  384. /*
  385. * The secondary interface requires a bit set to get the
  386. * interrupts.
  387. */
  388. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  389. slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
  390. jmicron_enable_mmc(slot->host, 1);
  391. slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
  392. return 0;
  393. }
  394. static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
  395. {
  396. if (dead)
  397. return;
  398. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  399. slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
  400. jmicron_enable_mmc(slot->host, 0);
  401. }
  402. static int jmicron_suspend(struct sdhci_pci_chip *chip)
  403. {
  404. int i;
  405. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  406. chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
  407. for (i = 0; i < chip->num_slots; i++)
  408. jmicron_enable_mmc(chip->slots[i]->host, 0);
  409. }
  410. return 0;
  411. }
  412. static int jmicron_resume(struct sdhci_pci_chip *chip)
  413. {
  414. int ret, i;
  415. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  416. chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
  417. for (i = 0; i < chip->num_slots; i++)
  418. jmicron_enable_mmc(chip->slots[i]->host, 1);
  419. }
  420. ret = jmicron_pmos(chip, 1);
  421. if (ret) {
  422. dev_err(&chip->pdev->dev, "Failure enabling card power\n");
  423. return ret;
  424. }
  425. return 0;
  426. }
  427. static const struct sdhci_pci_fixes sdhci_o2 = {
  428. .probe = sdhci_pci_o2_probe,
  429. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  430. .probe_slot = sdhci_pci_o2_probe_slot,
  431. .resume = sdhci_pci_o2_resume,
  432. };
  433. static const struct sdhci_pci_fixes sdhci_jmicron = {
  434. .probe = jmicron_probe,
  435. .probe_slot = jmicron_probe_slot,
  436. .remove_slot = jmicron_remove_slot,
  437. .suspend = jmicron_suspend,
  438. .resume = jmicron_resume,
  439. };
  440. /* SysKonnect CardBus2SDIO extra registers */
  441. #define SYSKT_CTRL 0x200
  442. #define SYSKT_RDFIFO_STAT 0x204
  443. #define SYSKT_WRFIFO_STAT 0x208
  444. #define SYSKT_POWER_DATA 0x20c
  445. #define SYSKT_POWER_330 0xef
  446. #define SYSKT_POWER_300 0xf8
  447. #define SYSKT_POWER_184 0xcc
  448. #define SYSKT_POWER_CMD 0x20d
  449. #define SYSKT_POWER_START (1 << 7)
  450. #define SYSKT_POWER_STATUS 0x20e
  451. #define SYSKT_POWER_STATUS_OK (1 << 0)
  452. #define SYSKT_BOARD_REV 0x210
  453. #define SYSKT_CHIP_REV 0x211
  454. #define SYSKT_CONF_DATA 0x212
  455. #define SYSKT_CONF_DATA_1V8 (1 << 2)
  456. #define SYSKT_CONF_DATA_2V5 (1 << 1)
  457. #define SYSKT_CONF_DATA_3V3 (1 << 0)
  458. static int syskt_probe(struct sdhci_pci_chip *chip)
  459. {
  460. if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
  461. chip->pdev->class &= ~0x0000FF;
  462. chip->pdev->class |= PCI_SDHCI_IFDMA;
  463. }
  464. return 0;
  465. }
  466. static int syskt_probe_slot(struct sdhci_pci_slot *slot)
  467. {
  468. int tm, ps;
  469. u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
  470. u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
  471. dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
  472. "board rev %d.%d, chip rev %d.%d\n",
  473. board_rev >> 4, board_rev & 0xf,
  474. chip_rev >> 4, chip_rev & 0xf);
  475. if (chip_rev >= 0x20)
  476. slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
  477. writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
  478. writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
  479. udelay(50);
  480. tm = 10; /* Wait max 1 ms */
  481. do {
  482. ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
  483. if (ps & SYSKT_POWER_STATUS_OK)
  484. break;
  485. udelay(100);
  486. } while (--tm);
  487. if (!tm) {
  488. dev_err(&slot->chip->pdev->dev,
  489. "power regulator never stabilized");
  490. writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
  491. return -ENODEV;
  492. }
  493. return 0;
  494. }
  495. static const struct sdhci_pci_fixes sdhci_syskt = {
  496. .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
  497. .probe = syskt_probe,
  498. .probe_slot = syskt_probe_slot,
  499. };
  500. static int via_probe(struct sdhci_pci_chip *chip)
  501. {
  502. if (chip->pdev->revision == 0x10)
  503. chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
  504. return 0;
  505. }
  506. static const struct sdhci_pci_fixes sdhci_via = {
  507. .probe = via_probe,
  508. };
  509. static const struct pci_device_id pci_ids[] = {
  510. {
  511. .vendor = PCI_VENDOR_ID_RICOH,
  512. .device = PCI_DEVICE_ID_RICOH_R5C822,
  513. .subvendor = PCI_ANY_ID,
  514. .subdevice = PCI_ANY_ID,
  515. .driver_data = (kernel_ulong_t)&sdhci_ricoh,
  516. },
  517. {
  518. .vendor = PCI_VENDOR_ID_RICOH,
  519. .device = 0x843,
  520. .subvendor = PCI_ANY_ID,
  521. .subdevice = PCI_ANY_ID,
  522. .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
  523. },
  524. {
  525. .vendor = PCI_VENDOR_ID_RICOH,
  526. .device = 0xe822,
  527. .subvendor = PCI_ANY_ID,
  528. .subdevice = PCI_ANY_ID,
  529. .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
  530. },
  531. {
  532. .vendor = PCI_VENDOR_ID_RICOH,
  533. .device = 0xe823,
  534. .subvendor = PCI_ANY_ID,
  535. .subdevice = PCI_ANY_ID,
  536. .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
  537. },
  538. {
  539. .vendor = PCI_VENDOR_ID_ENE,
  540. .device = PCI_DEVICE_ID_ENE_CB712_SD,
  541. .subvendor = PCI_ANY_ID,
  542. .subdevice = PCI_ANY_ID,
  543. .driver_data = (kernel_ulong_t)&sdhci_ene_712,
  544. },
  545. {
  546. .vendor = PCI_VENDOR_ID_ENE,
  547. .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
  548. .subvendor = PCI_ANY_ID,
  549. .subdevice = PCI_ANY_ID,
  550. .driver_data = (kernel_ulong_t)&sdhci_ene_712,
  551. },
  552. {
  553. .vendor = PCI_VENDOR_ID_ENE,
  554. .device = PCI_DEVICE_ID_ENE_CB714_SD,
  555. .subvendor = PCI_ANY_ID,
  556. .subdevice = PCI_ANY_ID,
  557. .driver_data = (kernel_ulong_t)&sdhci_ene_714,
  558. },
  559. {
  560. .vendor = PCI_VENDOR_ID_ENE,
  561. .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
  562. .subvendor = PCI_ANY_ID,
  563. .subdevice = PCI_ANY_ID,
  564. .driver_data = (kernel_ulong_t)&sdhci_ene_714,
  565. },
  566. {
  567. .vendor = PCI_VENDOR_ID_MARVELL,
  568. .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
  569. .subvendor = PCI_ANY_ID,
  570. .subdevice = PCI_ANY_ID,
  571. .driver_data = (kernel_ulong_t)&sdhci_cafe,
  572. },
  573. {
  574. .vendor = PCI_VENDOR_ID_JMICRON,
  575. .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
  576. .subvendor = PCI_ANY_ID,
  577. .subdevice = PCI_ANY_ID,
  578. .driver_data = (kernel_ulong_t)&sdhci_jmicron,
  579. },
  580. {
  581. .vendor = PCI_VENDOR_ID_JMICRON,
  582. .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
  583. .subvendor = PCI_ANY_ID,
  584. .subdevice = PCI_ANY_ID,
  585. .driver_data = (kernel_ulong_t)&sdhci_jmicron,
  586. },
  587. {
  588. .vendor = PCI_VENDOR_ID_JMICRON,
  589. .device = PCI_DEVICE_ID_JMICRON_JMB388_SD,
  590. .subvendor = PCI_ANY_ID,
  591. .subdevice = PCI_ANY_ID,
  592. .driver_data = (kernel_ulong_t)&sdhci_jmicron,
  593. },
  594. {
  595. .vendor = PCI_VENDOR_ID_JMICRON,
  596. .device = PCI_DEVICE_ID_JMICRON_JMB388_ESD,
  597. .subvendor = PCI_ANY_ID,
  598. .subdevice = PCI_ANY_ID,
  599. .driver_data = (kernel_ulong_t)&sdhci_jmicron,
  600. },
  601. {
  602. .vendor = PCI_VENDOR_ID_SYSKONNECT,
  603. .device = 0x8000,
  604. .subvendor = PCI_ANY_ID,
  605. .subdevice = PCI_ANY_ID,
  606. .driver_data = (kernel_ulong_t)&sdhci_syskt,
  607. },
  608. {
  609. .vendor = PCI_VENDOR_ID_VIA,
  610. .device = 0x95d0,
  611. .subvendor = PCI_ANY_ID,
  612. .subdevice = PCI_ANY_ID,
  613. .driver_data = (kernel_ulong_t)&sdhci_via,
  614. },
  615. {
  616. .vendor = PCI_VENDOR_ID_INTEL,
  617. .device = PCI_DEVICE_ID_INTEL_MRST_SD0,
  618. .subvendor = PCI_ANY_ID,
  619. .subdevice = PCI_ANY_ID,
  620. .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc0,
  621. },
  622. {
  623. .vendor = PCI_VENDOR_ID_INTEL,
  624. .device = PCI_DEVICE_ID_INTEL_MRST_SD1,
  625. .subvendor = PCI_ANY_ID,
  626. .subdevice = PCI_ANY_ID,
  627. .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
  628. },
  629. {
  630. .vendor = PCI_VENDOR_ID_INTEL,
  631. .device = PCI_DEVICE_ID_INTEL_MRST_SD2,
  632. .subvendor = PCI_ANY_ID,
  633. .subdevice = PCI_ANY_ID,
  634. .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
  635. },
  636. {
  637. .vendor = PCI_VENDOR_ID_INTEL,
  638. .device = PCI_DEVICE_ID_INTEL_MFD_SD,
  639. .subvendor = PCI_ANY_ID,
  640. .subdevice = PCI_ANY_ID,
  641. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
  642. },
  643. {
  644. .vendor = PCI_VENDOR_ID_INTEL,
  645. .device = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
  646. .subvendor = PCI_ANY_ID,
  647. .subdevice = PCI_ANY_ID,
  648. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
  649. },
  650. {
  651. .vendor = PCI_VENDOR_ID_INTEL,
  652. .device = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
  653. .subvendor = PCI_ANY_ID,
  654. .subdevice = PCI_ANY_ID,
  655. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
  656. },
  657. {
  658. .vendor = PCI_VENDOR_ID_INTEL,
  659. .device = PCI_DEVICE_ID_INTEL_MFD_EMMC0,
  660. .subvendor = PCI_ANY_ID,
  661. .subdevice = PCI_ANY_ID,
  662. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
  663. },
  664. {
  665. .vendor = PCI_VENDOR_ID_INTEL,
  666. .device = PCI_DEVICE_ID_INTEL_MFD_EMMC1,
  667. .subvendor = PCI_ANY_ID,
  668. .subdevice = PCI_ANY_ID,
  669. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
  670. },
  671. {
  672. .vendor = PCI_VENDOR_ID_INTEL,
  673. .device = PCI_DEVICE_ID_INTEL_PCH_SDIO0,
  674. .subvendor = PCI_ANY_ID,
  675. .subdevice = PCI_ANY_ID,
  676. .driver_data = (kernel_ulong_t)&sdhci_intel_pch_sdio,
  677. },
  678. {
  679. .vendor = PCI_VENDOR_ID_INTEL,
  680. .device = PCI_DEVICE_ID_INTEL_PCH_SDIO1,
  681. .subvendor = PCI_ANY_ID,
  682. .subdevice = PCI_ANY_ID,
  683. .driver_data = (kernel_ulong_t)&sdhci_intel_pch_sdio,
  684. },
  685. {
  686. .vendor = PCI_VENDOR_ID_INTEL,
  687. .device = PCI_DEVICE_ID_INTEL_BYT_EMMC,
  688. .subvendor = PCI_ANY_ID,
  689. .subdevice = PCI_ANY_ID,
  690. .driver_data = (kernel_ulong_t)&sdhci_intel_byt_emmc,
  691. },
  692. {
  693. .vendor = PCI_VENDOR_ID_INTEL,
  694. .device = PCI_DEVICE_ID_INTEL_BYT_SDIO,
  695. .subvendor = PCI_ANY_ID,
  696. .subdevice = PCI_ANY_ID,
  697. .driver_data = (kernel_ulong_t)&sdhci_intel_byt_sdio,
  698. },
  699. {
  700. .vendor = PCI_VENDOR_ID_INTEL,
  701. .device = PCI_DEVICE_ID_INTEL_BYT_SD,
  702. .subvendor = PCI_ANY_ID,
  703. .subdevice = PCI_ANY_ID,
  704. .driver_data = (kernel_ulong_t)&sdhci_intel_byt_sd,
  705. },
  706. {
  707. .vendor = PCI_VENDOR_ID_INTEL,
  708. .device = PCI_DEVICE_ID_INTEL_BYT_EMMC2,
  709. .subvendor = PCI_ANY_ID,
  710. .subdevice = PCI_ANY_ID,
  711. .driver_data = (kernel_ulong_t)&sdhci_intel_byt_emmc,
  712. },
  713. {
  714. .vendor = PCI_VENDOR_ID_INTEL,
  715. .device = PCI_DEVICE_ID_INTEL_CLV_SDIO0,
  716. .subvendor = PCI_ANY_ID,
  717. .subdevice = PCI_ANY_ID,
  718. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
  719. },
  720. {
  721. .vendor = PCI_VENDOR_ID_INTEL,
  722. .device = PCI_DEVICE_ID_INTEL_CLV_SDIO1,
  723. .subvendor = PCI_ANY_ID,
  724. .subdevice = PCI_ANY_ID,
  725. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
  726. },
  727. {
  728. .vendor = PCI_VENDOR_ID_INTEL,
  729. .device = PCI_DEVICE_ID_INTEL_CLV_SDIO2,
  730. .subvendor = PCI_ANY_ID,
  731. .subdevice = PCI_ANY_ID,
  732. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
  733. },
  734. {
  735. .vendor = PCI_VENDOR_ID_INTEL,
  736. .device = PCI_DEVICE_ID_INTEL_CLV_EMMC0,
  737. .subvendor = PCI_ANY_ID,
  738. .subdevice = PCI_ANY_ID,
  739. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
  740. },
  741. {
  742. .vendor = PCI_VENDOR_ID_INTEL,
  743. .device = PCI_DEVICE_ID_INTEL_CLV_EMMC1,
  744. .subvendor = PCI_ANY_ID,
  745. .subdevice = PCI_ANY_ID,
  746. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
  747. },
  748. {
  749. .vendor = PCI_VENDOR_ID_INTEL,
  750. .device = PCI_DEVICE_ID_INTEL_MRFL_MMC,
  751. .subvendor = PCI_ANY_ID,
  752. .subdevice = PCI_ANY_ID,
  753. .driver_data = (kernel_ulong_t)&sdhci_intel_mrfl_mmc,
  754. },
  755. {
  756. .vendor = PCI_VENDOR_ID_O2,
  757. .device = PCI_DEVICE_ID_O2_8120,
  758. .subvendor = PCI_ANY_ID,
  759. .subdevice = PCI_ANY_ID,
  760. .driver_data = (kernel_ulong_t)&sdhci_o2,
  761. },
  762. {
  763. .vendor = PCI_VENDOR_ID_O2,
  764. .device = PCI_DEVICE_ID_O2_8220,
  765. .subvendor = PCI_ANY_ID,
  766. .subdevice = PCI_ANY_ID,
  767. .driver_data = (kernel_ulong_t)&sdhci_o2,
  768. },
  769. {
  770. .vendor = PCI_VENDOR_ID_O2,
  771. .device = PCI_DEVICE_ID_O2_8221,
  772. .subvendor = PCI_ANY_ID,
  773. .subdevice = PCI_ANY_ID,
  774. .driver_data = (kernel_ulong_t)&sdhci_o2,
  775. },
  776. {
  777. .vendor = PCI_VENDOR_ID_O2,
  778. .device = PCI_DEVICE_ID_O2_8320,
  779. .subvendor = PCI_ANY_ID,
  780. .subdevice = PCI_ANY_ID,
  781. .driver_data = (kernel_ulong_t)&sdhci_o2,
  782. },
  783. {
  784. .vendor = PCI_VENDOR_ID_O2,
  785. .device = PCI_DEVICE_ID_O2_8321,
  786. .subvendor = PCI_ANY_ID,
  787. .subdevice = PCI_ANY_ID,
  788. .driver_data = (kernel_ulong_t)&sdhci_o2,
  789. },
  790. {
  791. .vendor = PCI_VENDOR_ID_O2,
  792. .device = PCI_DEVICE_ID_O2_FUJIN2,
  793. .subvendor = PCI_ANY_ID,
  794. .subdevice = PCI_ANY_ID,
  795. .driver_data = (kernel_ulong_t)&sdhci_o2,
  796. },
  797. {
  798. .vendor = PCI_VENDOR_ID_O2,
  799. .device = PCI_DEVICE_ID_O2_SDS0,
  800. .subvendor = PCI_ANY_ID,
  801. .subdevice = PCI_ANY_ID,
  802. .driver_data = (kernel_ulong_t)&sdhci_o2,
  803. },
  804. {
  805. .vendor = PCI_VENDOR_ID_O2,
  806. .device = PCI_DEVICE_ID_O2_SDS1,
  807. .subvendor = PCI_ANY_ID,
  808. .subdevice = PCI_ANY_ID,
  809. .driver_data = (kernel_ulong_t)&sdhci_o2,
  810. },
  811. {
  812. .vendor = PCI_VENDOR_ID_O2,
  813. .device = PCI_DEVICE_ID_O2_SEABIRD0,
  814. .subvendor = PCI_ANY_ID,
  815. .subdevice = PCI_ANY_ID,
  816. .driver_data = (kernel_ulong_t)&sdhci_o2,
  817. },
  818. {
  819. .vendor = PCI_VENDOR_ID_O2,
  820. .device = PCI_DEVICE_ID_O2_SEABIRD1,
  821. .subvendor = PCI_ANY_ID,
  822. .subdevice = PCI_ANY_ID,
  823. .driver_data = (kernel_ulong_t)&sdhci_o2,
  824. },
  825. { /* Generic SD host controller */
  826. PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
  827. },
  828. { /* end: all zeroes */ },
  829. };
  830. MODULE_DEVICE_TABLE(pci, pci_ids);
  831. /*****************************************************************************\
  832. * *
  833. * SDHCI core callbacks *
  834. * *
  835. \*****************************************************************************/
  836. static int sdhci_pci_enable_dma(struct sdhci_host *host)
  837. {
  838. struct sdhci_pci_slot *slot;
  839. struct pci_dev *pdev;
  840. int ret;
  841. slot = sdhci_priv(host);
  842. pdev = slot->chip->pdev;
  843. if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
  844. ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
  845. (host->flags & SDHCI_USE_SDMA)) {
  846. dev_warn(&pdev->dev, "Will use DMA mode even though HW "
  847. "doesn't fully claim to support it.\n");
  848. }
  849. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  850. if (ret)
  851. return ret;
  852. pci_set_master(pdev);
  853. return 0;
  854. }
  855. static int sdhci_pci_bus_width(struct sdhci_host *host, int width)
  856. {
  857. u8 ctrl;
  858. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  859. switch (width) {
  860. case MMC_BUS_WIDTH_8:
  861. ctrl |= SDHCI_CTRL_8BITBUS;
  862. ctrl &= ~SDHCI_CTRL_4BITBUS;
  863. break;
  864. case MMC_BUS_WIDTH_4:
  865. ctrl |= SDHCI_CTRL_4BITBUS;
  866. ctrl &= ~SDHCI_CTRL_8BITBUS;
  867. break;
  868. default:
  869. ctrl &= ~(SDHCI_CTRL_8BITBUS | SDHCI_CTRL_4BITBUS);
  870. break;
  871. }
  872. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  873. return 0;
  874. }
  875. static void sdhci_pci_gpio_hw_reset(struct sdhci_host *host)
  876. {
  877. struct sdhci_pci_slot *slot = sdhci_priv(host);
  878. int rst_n_gpio = slot->rst_n_gpio;
  879. if (!gpio_is_valid(rst_n_gpio))
  880. return;
  881. gpio_set_value_cansleep(rst_n_gpio, 0);
  882. /* For eMMC, minimum is 1us but give it 10us for good measure */
  883. udelay(10);
  884. gpio_set_value_cansleep(rst_n_gpio, 1);
  885. /* For eMMC, minimum is 200us but give it 300us for good measure */
  886. usleep_range(300, 1000);
  887. }
  888. static void sdhci_pci_hw_reset(struct sdhci_host *host)
  889. {
  890. struct sdhci_pci_slot *slot = sdhci_priv(host);
  891. if (slot->hw_reset)
  892. slot->hw_reset(host);
  893. }
  894. static const struct sdhci_ops sdhci_pci_ops = {
  895. .enable_dma = sdhci_pci_enable_dma,
  896. .platform_bus_width = sdhci_pci_bus_width,
  897. .hw_reset = sdhci_pci_hw_reset,
  898. };
  899. /*****************************************************************************\
  900. * *
  901. * Suspend/resume *
  902. * *
  903. \*****************************************************************************/
  904. #ifdef CONFIG_PM
  905. static int sdhci_pci_suspend(struct device *dev)
  906. {
  907. struct pci_dev *pdev = to_pci_dev(dev);
  908. struct sdhci_pci_chip *chip;
  909. struct sdhci_pci_slot *slot;
  910. mmc_pm_flag_t slot_pm_flags;
  911. mmc_pm_flag_t pm_flags = 0;
  912. int i, ret;
  913. chip = pci_get_drvdata(pdev);
  914. if (!chip)
  915. return 0;
  916. for (i = 0; i < chip->num_slots; i++) {
  917. slot = chip->slots[i];
  918. if (!slot)
  919. continue;
  920. ret = sdhci_suspend_host(slot->host);
  921. if (ret)
  922. goto err_pci_suspend;
  923. slot_pm_flags = slot->host->mmc->pm_flags;
  924. if (slot_pm_flags & MMC_PM_WAKE_SDIO_IRQ)
  925. sdhci_enable_irq_wakeups(slot->host);
  926. pm_flags |= slot_pm_flags;
  927. }
  928. if (chip->fixes && chip->fixes->suspend) {
  929. ret = chip->fixes->suspend(chip);
  930. if (ret)
  931. goto err_pci_suspend;
  932. }
  933. pci_save_state(pdev);
  934. if (pm_flags & MMC_PM_KEEP_POWER) {
  935. if (pm_flags & MMC_PM_WAKE_SDIO_IRQ) {
  936. pci_pme_active(pdev, true);
  937. pci_enable_wake(pdev, PCI_D3hot, 1);
  938. }
  939. pci_set_power_state(pdev, PCI_D3hot);
  940. } else {
  941. pci_enable_wake(pdev, PCI_D3hot, 0);
  942. pci_disable_device(pdev);
  943. pci_set_power_state(pdev, PCI_D3hot);
  944. }
  945. return 0;
  946. err_pci_suspend:
  947. while (--i >= 0)
  948. sdhci_resume_host(chip->slots[i]->host);
  949. return ret;
  950. }
  951. static int sdhci_pci_resume(struct device *dev)
  952. {
  953. struct pci_dev *pdev = to_pci_dev(dev);
  954. struct sdhci_pci_chip *chip;
  955. struct sdhci_pci_slot *slot;
  956. int i, ret;
  957. chip = pci_get_drvdata(pdev);
  958. if (!chip)
  959. return 0;
  960. pci_set_power_state(pdev, PCI_D0);
  961. pci_restore_state(pdev);
  962. ret = pci_enable_device(pdev);
  963. if (ret)
  964. return ret;
  965. if (chip->fixes && chip->fixes->resume) {
  966. ret = chip->fixes->resume(chip);
  967. if (ret)
  968. return ret;
  969. }
  970. for (i = 0; i < chip->num_slots; i++) {
  971. slot = chip->slots[i];
  972. if (!slot)
  973. continue;
  974. ret = sdhci_resume_host(slot->host);
  975. if (ret)
  976. return ret;
  977. }
  978. return 0;
  979. }
  980. #else /* CONFIG_PM */
  981. #define sdhci_pci_suspend NULL
  982. #define sdhci_pci_resume NULL
  983. #endif /* CONFIG_PM */
  984. #ifdef CONFIG_PM_RUNTIME
  985. static int sdhci_pci_runtime_suspend(struct device *dev)
  986. {
  987. struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
  988. struct sdhci_pci_chip *chip;
  989. struct sdhci_pci_slot *slot;
  990. int i, ret;
  991. chip = pci_get_drvdata(pdev);
  992. if (!chip)
  993. return 0;
  994. for (i = 0; i < chip->num_slots; i++) {
  995. slot = chip->slots[i];
  996. if (!slot)
  997. continue;
  998. ret = sdhci_runtime_suspend_host(slot->host);
  999. if (ret)
  1000. goto err_pci_runtime_suspend;
  1001. }
  1002. if (chip->fixes && chip->fixes->suspend) {
  1003. ret = chip->fixes->suspend(chip);
  1004. if (ret)
  1005. goto err_pci_runtime_suspend;
  1006. }
  1007. return 0;
  1008. err_pci_runtime_suspend:
  1009. while (--i >= 0)
  1010. sdhci_runtime_resume_host(chip->slots[i]->host);
  1011. return ret;
  1012. }
  1013. static int sdhci_pci_runtime_resume(struct device *dev)
  1014. {
  1015. struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
  1016. struct sdhci_pci_chip *chip;
  1017. struct sdhci_pci_slot *slot;
  1018. int i, ret;
  1019. chip = pci_get_drvdata(pdev);
  1020. if (!chip)
  1021. return 0;
  1022. if (chip->fixes && chip->fixes->resume) {
  1023. ret = chip->fixes->resume(chip);
  1024. if (ret)
  1025. return ret;
  1026. }
  1027. for (i = 0; i < chip->num_slots; i++) {
  1028. slot = chip->slots[i];
  1029. if (!slot)
  1030. continue;
  1031. ret = sdhci_runtime_resume_host(slot->host);
  1032. if (ret)
  1033. return ret;
  1034. }
  1035. return 0;
  1036. }
  1037. static int sdhci_pci_runtime_idle(struct device *dev)
  1038. {
  1039. return 0;
  1040. }
  1041. #else
  1042. #define sdhci_pci_runtime_suspend NULL
  1043. #define sdhci_pci_runtime_resume NULL
  1044. #define sdhci_pci_runtime_idle NULL
  1045. #endif
  1046. static const struct dev_pm_ops sdhci_pci_pm_ops = {
  1047. .suspend = sdhci_pci_suspend,
  1048. .resume = sdhci_pci_resume,
  1049. .runtime_suspend = sdhci_pci_runtime_suspend,
  1050. .runtime_resume = sdhci_pci_runtime_resume,
  1051. .runtime_idle = sdhci_pci_runtime_idle,
  1052. };
  1053. /*****************************************************************************\
  1054. * *
  1055. * Device probing/removal *
  1056. * *
  1057. \*****************************************************************************/
  1058. static struct sdhci_pci_slot *sdhci_pci_probe_slot(
  1059. struct pci_dev *pdev, struct sdhci_pci_chip *chip, int first_bar,
  1060. int slotno)
  1061. {
  1062. struct sdhci_pci_slot *slot;
  1063. struct sdhci_host *host;
  1064. int ret, bar = first_bar + slotno;
  1065. if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
  1066. dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
  1067. return ERR_PTR(-ENODEV);
  1068. }
  1069. if (pci_resource_len(pdev, bar) < 0x100) {
  1070. dev_err(&pdev->dev, "Invalid iomem size. You may "
  1071. "experience problems.\n");
  1072. }
  1073. if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
  1074. dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
  1075. return ERR_PTR(-ENODEV);
  1076. }
  1077. if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
  1078. dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
  1079. return ERR_PTR(-ENODEV);
  1080. }
  1081. host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
  1082. if (IS_ERR(host)) {
  1083. dev_err(&pdev->dev, "cannot allocate host\n");
  1084. return ERR_CAST(host);
  1085. }
  1086. slot = sdhci_priv(host);
  1087. slot->chip = chip;
  1088. slot->host = host;
  1089. slot->pci_bar = bar;
  1090. slot->rst_n_gpio = -EINVAL;
  1091. slot->cd_gpio = -EINVAL;
  1092. /* Retrieve platform data if there is any */
  1093. if (*sdhci_pci_get_data)
  1094. slot->data = sdhci_pci_get_data(pdev, slotno);
  1095. if (slot->data) {
  1096. if (slot->data->setup) {
  1097. ret = slot->data->setup(slot->data);
  1098. if (ret) {
  1099. dev_err(&pdev->dev, "platform setup failed\n");
  1100. goto free;
  1101. }
  1102. }
  1103. slot->rst_n_gpio = slot->data->rst_n_gpio;
  1104. slot->cd_gpio = slot->data->cd_gpio;
  1105. }
  1106. host->hw_name = "PCI";
  1107. host->ops = &sdhci_pci_ops;
  1108. host->quirks = chip->quirks;
  1109. host->quirks2 = chip->quirks2;
  1110. host->irq = pdev->irq;
  1111. ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
  1112. if (ret) {
  1113. dev_err(&pdev->dev, "cannot request region\n");
  1114. goto cleanup;
  1115. }
  1116. host->ioaddr = pci_ioremap_bar(pdev, bar);
  1117. if (!host->ioaddr) {
  1118. dev_err(&pdev->dev, "failed to remap registers\n");
  1119. ret = -ENOMEM;
  1120. goto release;
  1121. }
  1122. if (chip->fixes && chip->fixes->probe_slot) {
  1123. ret = chip->fixes->probe_slot(slot);
  1124. if (ret)
  1125. goto unmap;
  1126. }
  1127. if (gpio_is_valid(slot->rst_n_gpio)) {
  1128. if (!gpio_request(slot->rst_n_gpio, "eMMC_reset")) {
  1129. gpio_direction_output(slot->rst_n_gpio, 1);
  1130. slot->host->mmc->caps |= MMC_CAP_HW_RESET;
  1131. slot->hw_reset = sdhci_pci_gpio_hw_reset;
  1132. } else {
  1133. dev_warn(&pdev->dev, "failed to request rst_n_gpio\n");
  1134. slot->rst_n_gpio = -EINVAL;
  1135. }
  1136. }
  1137. host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
  1138. host->mmc->slotno = slotno;
  1139. host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
  1140. ret = sdhci_add_host(host);
  1141. if (ret)
  1142. goto remove;
  1143. sdhci_pci_add_own_cd(slot);
  1144. /*
  1145. * Check if the chip needs a separate GPIO for card detect to wake up
  1146. * from runtime suspend. If it is not there, don't allow runtime PM.
  1147. * Note sdhci_pci_add_own_cd() sets slot->cd_gpio to -EINVAL on failure.
  1148. */
  1149. if (chip->fixes && chip->fixes->own_cd_for_runtime_pm &&
  1150. !gpio_is_valid(slot->cd_gpio))
  1151. chip->allow_runtime_pm = false;
  1152. return slot;
  1153. remove:
  1154. if (gpio_is_valid(slot->rst_n_gpio))
  1155. gpio_free(slot->rst_n_gpio);
  1156. if (chip->fixes && chip->fixes->remove_slot)
  1157. chip->fixes->remove_slot(slot, 0);
  1158. unmap:
  1159. iounmap(host->ioaddr);
  1160. release:
  1161. pci_release_region(pdev, bar);
  1162. cleanup:
  1163. if (slot->data && slot->data->cleanup)
  1164. slot->data->cleanup(slot->data);
  1165. free:
  1166. sdhci_free_host(host);
  1167. return ERR_PTR(ret);
  1168. }
  1169. static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
  1170. {
  1171. int dead;
  1172. u32 scratch;
  1173. sdhci_pci_remove_own_cd(slot);
  1174. dead = 0;
  1175. scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
  1176. if (scratch == (u32)-1)
  1177. dead = 1;
  1178. sdhci_remove_host(slot->host, dead);
  1179. if (gpio_is_valid(slot->rst_n_gpio))
  1180. gpio_free(slot->rst_n_gpio);
  1181. if (slot->chip->fixes && slot->chip->fixes->remove_slot)
  1182. slot->chip->fixes->remove_slot(slot, dead);
  1183. if (slot->data && slot->data->cleanup)
  1184. slot->data->cleanup(slot->data);
  1185. pci_release_region(slot->chip->pdev, slot->pci_bar);
  1186. sdhci_free_host(slot->host);
  1187. }
  1188. static void sdhci_pci_runtime_pm_allow(struct device *dev)
  1189. {
  1190. pm_runtime_put_noidle(dev);
  1191. pm_runtime_allow(dev);
  1192. pm_runtime_set_autosuspend_delay(dev, 50);
  1193. pm_runtime_use_autosuspend(dev);
  1194. pm_suspend_ignore_children(dev, 1);
  1195. }
  1196. static void sdhci_pci_runtime_pm_forbid(struct device *dev)
  1197. {
  1198. pm_runtime_forbid(dev);
  1199. pm_runtime_get_noresume(dev);
  1200. }
  1201. static int sdhci_pci_probe(struct pci_dev *pdev,
  1202. const struct pci_device_id *ent)
  1203. {
  1204. struct sdhci_pci_chip *chip;
  1205. struct sdhci_pci_slot *slot;
  1206. u8 slots, first_bar;
  1207. int ret, i;
  1208. BUG_ON(pdev == NULL);
  1209. BUG_ON(ent == NULL);
  1210. dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
  1211. (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
  1212. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
  1213. if (ret)
  1214. return ret;
  1215. slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
  1216. dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
  1217. if (slots == 0)
  1218. return -ENODEV;
  1219. BUG_ON(slots > MAX_SLOTS);
  1220. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
  1221. if (ret)
  1222. return ret;
  1223. first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
  1224. if (first_bar > 5) {
  1225. dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
  1226. return -ENODEV;
  1227. }
  1228. ret = pci_enable_device(pdev);
  1229. if (ret)
  1230. return ret;
  1231. chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
  1232. if (!chip) {
  1233. ret = -ENOMEM;
  1234. goto err;
  1235. }
  1236. chip->pdev = pdev;
  1237. chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
  1238. if (chip->fixes) {
  1239. chip->quirks = chip->fixes->quirks;
  1240. chip->quirks2 = chip->fixes->quirks2;
  1241. chip->allow_runtime_pm = chip->fixes->allow_runtime_pm;
  1242. }
  1243. chip->num_slots = slots;
  1244. pci_set_drvdata(pdev, chip);
  1245. if (chip->fixes && chip->fixes->probe) {
  1246. ret = chip->fixes->probe(chip);
  1247. if (ret)
  1248. goto free;
  1249. }
  1250. slots = chip->num_slots; /* Quirk may have changed this */
  1251. for (i = 0; i < slots; i++) {
  1252. slot = sdhci_pci_probe_slot(pdev, chip, first_bar, i);
  1253. if (IS_ERR(slot)) {
  1254. for (i--; i >= 0; i--)
  1255. sdhci_pci_remove_slot(chip->slots[i]);
  1256. ret = PTR_ERR(slot);
  1257. goto free;
  1258. }
  1259. chip->slots[i] = slot;
  1260. }
  1261. if (chip->allow_runtime_pm)
  1262. sdhci_pci_runtime_pm_allow(&pdev->dev);
  1263. return 0;
  1264. free:
  1265. pci_set_drvdata(pdev, NULL);
  1266. kfree(chip);
  1267. err:
  1268. pci_disable_device(pdev);
  1269. return ret;
  1270. }
  1271. static void sdhci_pci_remove(struct pci_dev *pdev)
  1272. {
  1273. int i;
  1274. struct sdhci_pci_chip *chip;
  1275. chip = pci_get_drvdata(pdev);
  1276. if (chip) {
  1277. if (chip->allow_runtime_pm)
  1278. sdhci_pci_runtime_pm_forbid(&pdev->dev);
  1279. for (i = 0; i < chip->num_slots; i++)
  1280. sdhci_pci_remove_slot(chip->slots[i]);
  1281. pci_set_drvdata(pdev, NULL);
  1282. kfree(chip);
  1283. }
  1284. pci_disable_device(pdev);
  1285. }
  1286. static struct pci_driver sdhci_driver = {
  1287. .name = "sdhci-pci",
  1288. .id_table = pci_ids,
  1289. .probe = sdhci_pci_probe,
  1290. .remove = sdhci_pci_remove,
  1291. .driver = {
  1292. .pm = &sdhci_pci_pm_ops
  1293. },
  1294. };
  1295. module_pci_driver(sdhci_driver);
  1296. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  1297. MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
  1298. MODULE_LICENSE("GPL");