sdhci-pci.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  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 int rtsx_probe_slot(struct sdhci_pci_slot *slot)
  510. {
  511. slot->host->mmc->caps2 |= MMC_CAP2_HS200;
  512. return 0;
  513. }
  514. static const struct sdhci_pci_fixes sdhci_rtsx = {
  515. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  516. SDHCI_QUIRK2_BROKEN_DDR50,
  517. .probe_slot = rtsx_probe_slot,
  518. };
  519. static const struct pci_device_id pci_ids[] = {
  520. {
  521. .vendor = PCI_VENDOR_ID_RICOH,
  522. .device = PCI_DEVICE_ID_RICOH_R5C822,
  523. .subvendor = PCI_ANY_ID,
  524. .subdevice = PCI_ANY_ID,
  525. .driver_data = (kernel_ulong_t)&sdhci_ricoh,
  526. },
  527. {
  528. .vendor = PCI_VENDOR_ID_RICOH,
  529. .device = 0x843,
  530. .subvendor = PCI_ANY_ID,
  531. .subdevice = PCI_ANY_ID,
  532. .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
  533. },
  534. {
  535. .vendor = PCI_VENDOR_ID_RICOH,
  536. .device = 0xe822,
  537. .subvendor = PCI_ANY_ID,
  538. .subdevice = PCI_ANY_ID,
  539. .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
  540. },
  541. {
  542. .vendor = PCI_VENDOR_ID_RICOH,
  543. .device = 0xe823,
  544. .subvendor = PCI_ANY_ID,
  545. .subdevice = PCI_ANY_ID,
  546. .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
  547. },
  548. {
  549. .vendor = PCI_VENDOR_ID_ENE,
  550. .device = PCI_DEVICE_ID_ENE_CB712_SD,
  551. .subvendor = PCI_ANY_ID,
  552. .subdevice = PCI_ANY_ID,
  553. .driver_data = (kernel_ulong_t)&sdhci_ene_712,
  554. },
  555. {
  556. .vendor = PCI_VENDOR_ID_ENE,
  557. .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
  558. .subvendor = PCI_ANY_ID,
  559. .subdevice = PCI_ANY_ID,
  560. .driver_data = (kernel_ulong_t)&sdhci_ene_712,
  561. },
  562. {
  563. .vendor = PCI_VENDOR_ID_ENE,
  564. .device = PCI_DEVICE_ID_ENE_CB714_SD,
  565. .subvendor = PCI_ANY_ID,
  566. .subdevice = PCI_ANY_ID,
  567. .driver_data = (kernel_ulong_t)&sdhci_ene_714,
  568. },
  569. {
  570. .vendor = PCI_VENDOR_ID_ENE,
  571. .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
  572. .subvendor = PCI_ANY_ID,
  573. .subdevice = PCI_ANY_ID,
  574. .driver_data = (kernel_ulong_t)&sdhci_ene_714,
  575. },
  576. {
  577. .vendor = PCI_VENDOR_ID_MARVELL,
  578. .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
  579. .subvendor = PCI_ANY_ID,
  580. .subdevice = PCI_ANY_ID,
  581. .driver_data = (kernel_ulong_t)&sdhci_cafe,
  582. },
  583. {
  584. .vendor = PCI_VENDOR_ID_JMICRON,
  585. .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
  586. .subvendor = PCI_ANY_ID,
  587. .subdevice = PCI_ANY_ID,
  588. .driver_data = (kernel_ulong_t)&sdhci_jmicron,
  589. },
  590. {
  591. .vendor = PCI_VENDOR_ID_JMICRON,
  592. .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
  593. .subvendor = PCI_ANY_ID,
  594. .subdevice = PCI_ANY_ID,
  595. .driver_data = (kernel_ulong_t)&sdhci_jmicron,
  596. },
  597. {
  598. .vendor = PCI_VENDOR_ID_JMICRON,
  599. .device = PCI_DEVICE_ID_JMICRON_JMB388_SD,
  600. .subvendor = PCI_ANY_ID,
  601. .subdevice = PCI_ANY_ID,
  602. .driver_data = (kernel_ulong_t)&sdhci_jmicron,
  603. },
  604. {
  605. .vendor = PCI_VENDOR_ID_JMICRON,
  606. .device = PCI_DEVICE_ID_JMICRON_JMB388_ESD,
  607. .subvendor = PCI_ANY_ID,
  608. .subdevice = PCI_ANY_ID,
  609. .driver_data = (kernel_ulong_t)&sdhci_jmicron,
  610. },
  611. {
  612. .vendor = PCI_VENDOR_ID_SYSKONNECT,
  613. .device = 0x8000,
  614. .subvendor = PCI_ANY_ID,
  615. .subdevice = PCI_ANY_ID,
  616. .driver_data = (kernel_ulong_t)&sdhci_syskt,
  617. },
  618. {
  619. .vendor = PCI_VENDOR_ID_VIA,
  620. .device = 0x95d0,
  621. .subvendor = PCI_ANY_ID,
  622. .subdevice = PCI_ANY_ID,
  623. .driver_data = (kernel_ulong_t)&sdhci_via,
  624. },
  625. {
  626. .vendor = PCI_VENDOR_ID_REALTEK,
  627. .device = 0x5250,
  628. .subvendor = PCI_ANY_ID,
  629. .subdevice = PCI_ANY_ID,
  630. .driver_data = (kernel_ulong_t)&sdhci_rtsx,
  631. },
  632. {
  633. .vendor = PCI_VENDOR_ID_INTEL,
  634. .device = PCI_DEVICE_ID_INTEL_MRST_SD0,
  635. .subvendor = PCI_ANY_ID,
  636. .subdevice = PCI_ANY_ID,
  637. .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc0,
  638. },
  639. {
  640. .vendor = PCI_VENDOR_ID_INTEL,
  641. .device = PCI_DEVICE_ID_INTEL_MRST_SD1,
  642. .subvendor = PCI_ANY_ID,
  643. .subdevice = PCI_ANY_ID,
  644. .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
  645. },
  646. {
  647. .vendor = PCI_VENDOR_ID_INTEL,
  648. .device = PCI_DEVICE_ID_INTEL_MRST_SD2,
  649. .subvendor = PCI_ANY_ID,
  650. .subdevice = PCI_ANY_ID,
  651. .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
  652. },
  653. {
  654. .vendor = PCI_VENDOR_ID_INTEL,
  655. .device = PCI_DEVICE_ID_INTEL_MFD_SD,
  656. .subvendor = PCI_ANY_ID,
  657. .subdevice = PCI_ANY_ID,
  658. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
  659. },
  660. {
  661. .vendor = PCI_VENDOR_ID_INTEL,
  662. .device = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
  663. .subvendor = PCI_ANY_ID,
  664. .subdevice = PCI_ANY_ID,
  665. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
  666. },
  667. {
  668. .vendor = PCI_VENDOR_ID_INTEL,
  669. .device = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
  670. .subvendor = PCI_ANY_ID,
  671. .subdevice = PCI_ANY_ID,
  672. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
  673. },
  674. {
  675. .vendor = PCI_VENDOR_ID_INTEL,
  676. .device = PCI_DEVICE_ID_INTEL_MFD_EMMC0,
  677. .subvendor = PCI_ANY_ID,
  678. .subdevice = PCI_ANY_ID,
  679. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
  680. },
  681. {
  682. .vendor = PCI_VENDOR_ID_INTEL,
  683. .device = PCI_DEVICE_ID_INTEL_MFD_EMMC1,
  684. .subvendor = PCI_ANY_ID,
  685. .subdevice = PCI_ANY_ID,
  686. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
  687. },
  688. {
  689. .vendor = PCI_VENDOR_ID_INTEL,
  690. .device = PCI_DEVICE_ID_INTEL_PCH_SDIO0,
  691. .subvendor = PCI_ANY_ID,
  692. .subdevice = PCI_ANY_ID,
  693. .driver_data = (kernel_ulong_t)&sdhci_intel_pch_sdio,
  694. },
  695. {
  696. .vendor = PCI_VENDOR_ID_INTEL,
  697. .device = PCI_DEVICE_ID_INTEL_PCH_SDIO1,
  698. .subvendor = PCI_ANY_ID,
  699. .subdevice = PCI_ANY_ID,
  700. .driver_data = (kernel_ulong_t)&sdhci_intel_pch_sdio,
  701. },
  702. {
  703. .vendor = PCI_VENDOR_ID_INTEL,
  704. .device = PCI_DEVICE_ID_INTEL_BYT_EMMC,
  705. .subvendor = PCI_ANY_ID,
  706. .subdevice = PCI_ANY_ID,
  707. .driver_data = (kernel_ulong_t)&sdhci_intel_byt_emmc,
  708. },
  709. {
  710. .vendor = PCI_VENDOR_ID_INTEL,
  711. .device = PCI_DEVICE_ID_INTEL_BYT_SDIO,
  712. .subvendor = PCI_ANY_ID,
  713. .subdevice = PCI_ANY_ID,
  714. .driver_data = (kernel_ulong_t)&sdhci_intel_byt_sdio,
  715. },
  716. {
  717. .vendor = PCI_VENDOR_ID_INTEL,
  718. .device = PCI_DEVICE_ID_INTEL_BYT_SD,
  719. .subvendor = PCI_ANY_ID,
  720. .subdevice = PCI_ANY_ID,
  721. .driver_data = (kernel_ulong_t)&sdhci_intel_byt_sd,
  722. },
  723. {
  724. .vendor = PCI_VENDOR_ID_INTEL,
  725. .device = PCI_DEVICE_ID_INTEL_BYT_EMMC2,
  726. .subvendor = PCI_ANY_ID,
  727. .subdevice = PCI_ANY_ID,
  728. .driver_data = (kernel_ulong_t)&sdhci_intel_byt_emmc,
  729. },
  730. {
  731. .vendor = PCI_VENDOR_ID_INTEL,
  732. .device = PCI_DEVICE_ID_INTEL_CLV_SDIO0,
  733. .subvendor = PCI_ANY_ID,
  734. .subdevice = PCI_ANY_ID,
  735. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
  736. },
  737. {
  738. .vendor = PCI_VENDOR_ID_INTEL,
  739. .device = PCI_DEVICE_ID_INTEL_CLV_SDIO1,
  740. .subvendor = PCI_ANY_ID,
  741. .subdevice = PCI_ANY_ID,
  742. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
  743. },
  744. {
  745. .vendor = PCI_VENDOR_ID_INTEL,
  746. .device = PCI_DEVICE_ID_INTEL_CLV_SDIO2,
  747. .subvendor = PCI_ANY_ID,
  748. .subdevice = PCI_ANY_ID,
  749. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
  750. },
  751. {
  752. .vendor = PCI_VENDOR_ID_INTEL,
  753. .device = PCI_DEVICE_ID_INTEL_CLV_EMMC0,
  754. .subvendor = PCI_ANY_ID,
  755. .subdevice = PCI_ANY_ID,
  756. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
  757. },
  758. {
  759. .vendor = PCI_VENDOR_ID_INTEL,
  760. .device = PCI_DEVICE_ID_INTEL_CLV_EMMC1,
  761. .subvendor = PCI_ANY_ID,
  762. .subdevice = PCI_ANY_ID,
  763. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
  764. },
  765. {
  766. .vendor = PCI_VENDOR_ID_INTEL,
  767. .device = PCI_DEVICE_ID_INTEL_MRFL_MMC,
  768. .subvendor = PCI_ANY_ID,
  769. .subdevice = PCI_ANY_ID,
  770. .driver_data = (kernel_ulong_t)&sdhci_intel_mrfl_mmc,
  771. },
  772. {
  773. .vendor = PCI_VENDOR_ID_O2,
  774. .device = PCI_DEVICE_ID_O2_8120,
  775. .subvendor = PCI_ANY_ID,
  776. .subdevice = PCI_ANY_ID,
  777. .driver_data = (kernel_ulong_t)&sdhci_o2,
  778. },
  779. {
  780. .vendor = PCI_VENDOR_ID_O2,
  781. .device = PCI_DEVICE_ID_O2_8220,
  782. .subvendor = PCI_ANY_ID,
  783. .subdevice = PCI_ANY_ID,
  784. .driver_data = (kernel_ulong_t)&sdhci_o2,
  785. },
  786. {
  787. .vendor = PCI_VENDOR_ID_O2,
  788. .device = PCI_DEVICE_ID_O2_8221,
  789. .subvendor = PCI_ANY_ID,
  790. .subdevice = PCI_ANY_ID,
  791. .driver_data = (kernel_ulong_t)&sdhci_o2,
  792. },
  793. {
  794. .vendor = PCI_VENDOR_ID_O2,
  795. .device = PCI_DEVICE_ID_O2_8320,
  796. .subvendor = PCI_ANY_ID,
  797. .subdevice = PCI_ANY_ID,
  798. .driver_data = (kernel_ulong_t)&sdhci_o2,
  799. },
  800. {
  801. .vendor = PCI_VENDOR_ID_O2,
  802. .device = PCI_DEVICE_ID_O2_8321,
  803. .subvendor = PCI_ANY_ID,
  804. .subdevice = PCI_ANY_ID,
  805. .driver_data = (kernel_ulong_t)&sdhci_o2,
  806. },
  807. {
  808. .vendor = PCI_VENDOR_ID_O2,
  809. .device = PCI_DEVICE_ID_O2_FUJIN2,
  810. .subvendor = PCI_ANY_ID,
  811. .subdevice = PCI_ANY_ID,
  812. .driver_data = (kernel_ulong_t)&sdhci_o2,
  813. },
  814. {
  815. .vendor = PCI_VENDOR_ID_O2,
  816. .device = PCI_DEVICE_ID_O2_SDS0,
  817. .subvendor = PCI_ANY_ID,
  818. .subdevice = PCI_ANY_ID,
  819. .driver_data = (kernel_ulong_t)&sdhci_o2,
  820. },
  821. {
  822. .vendor = PCI_VENDOR_ID_O2,
  823. .device = PCI_DEVICE_ID_O2_SDS1,
  824. .subvendor = PCI_ANY_ID,
  825. .subdevice = PCI_ANY_ID,
  826. .driver_data = (kernel_ulong_t)&sdhci_o2,
  827. },
  828. {
  829. .vendor = PCI_VENDOR_ID_O2,
  830. .device = PCI_DEVICE_ID_O2_SEABIRD0,
  831. .subvendor = PCI_ANY_ID,
  832. .subdevice = PCI_ANY_ID,
  833. .driver_data = (kernel_ulong_t)&sdhci_o2,
  834. },
  835. {
  836. .vendor = PCI_VENDOR_ID_O2,
  837. .device = PCI_DEVICE_ID_O2_SEABIRD1,
  838. .subvendor = PCI_ANY_ID,
  839. .subdevice = PCI_ANY_ID,
  840. .driver_data = (kernel_ulong_t)&sdhci_o2,
  841. },
  842. { /* Generic SD host controller */
  843. PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
  844. },
  845. { /* end: all zeroes */ },
  846. };
  847. MODULE_DEVICE_TABLE(pci, pci_ids);
  848. /*****************************************************************************\
  849. * *
  850. * SDHCI core callbacks *
  851. * *
  852. \*****************************************************************************/
  853. static int sdhci_pci_enable_dma(struct sdhci_host *host)
  854. {
  855. struct sdhci_pci_slot *slot;
  856. struct pci_dev *pdev;
  857. int ret;
  858. slot = sdhci_priv(host);
  859. pdev = slot->chip->pdev;
  860. if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
  861. ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
  862. (host->flags & SDHCI_USE_SDMA)) {
  863. dev_warn(&pdev->dev, "Will use DMA mode even though HW "
  864. "doesn't fully claim to support it.\n");
  865. }
  866. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  867. if (ret)
  868. return ret;
  869. pci_set_master(pdev);
  870. return 0;
  871. }
  872. static void sdhci_pci_set_bus_width(struct sdhci_host *host, int width)
  873. {
  874. u8 ctrl;
  875. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  876. switch (width) {
  877. case MMC_BUS_WIDTH_8:
  878. ctrl |= SDHCI_CTRL_8BITBUS;
  879. ctrl &= ~SDHCI_CTRL_4BITBUS;
  880. break;
  881. case MMC_BUS_WIDTH_4:
  882. ctrl |= SDHCI_CTRL_4BITBUS;
  883. ctrl &= ~SDHCI_CTRL_8BITBUS;
  884. break;
  885. default:
  886. ctrl &= ~(SDHCI_CTRL_8BITBUS | SDHCI_CTRL_4BITBUS);
  887. break;
  888. }
  889. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  890. }
  891. static void sdhci_pci_gpio_hw_reset(struct sdhci_host *host)
  892. {
  893. struct sdhci_pci_slot *slot = sdhci_priv(host);
  894. int rst_n_gpio = slot->rst_n_gpio;
  895. if (!gpio_is_valid(rst_n_gpio))
  896. return;
  897. gpio_set_value_cansleep(rst_n_gpio, 0);
  898. /* For eMMC, minimum is 1us but give it 10us for good measure */
  899. udelay(10);
  900. gpio_set_value_cansleep(rst_n_gpio, 1);
  901. /* For eMMC, minimum is 200us but give it 300us for good measure */
  902. usleep_range(300, 1000);
  903. }
  904. static void sdhci_pci_hw_reset(struct sdhci_host *host)
  905. {
  906. struct sdhci_pci_slot *slot = sdhci_priv(host);
  907. if (slot->hw_reset)
  908. slot->hw_reset(host);
  909. }
  910. static const struct sdhci_ops sdhci_pci_ops = {
  911. .set_clock = sdhci_set_clock,
  912. .enable_dma = sdhci_pci_enable_dma,
  913. .set_bus_width = sdhci_pci_set_bus_width,
  914. .reset = sdhci_reset,
  915. .set_uhs_signaling = sdhci_set_uhs_signaling,
  916. .hw_reset = sdhci_pci_hw_reset,
  917. };
  918. /*****************************************************************************\
  919. * *
  920. * Suspend/resume *
  921. * *
  922. \*****************************************************************************/
  923. #ifdef CONFIG_PM
  924. static int sdhci_pci_suspend(struct device *dev)
  925. {
  926. struct pci_dev *pdev = to_pci_dev(dev);
  927. struct sdhci_pci_chip *chip;
  928. struct sdhci_pci_slot *slot;
  929. mmc_pm_flag_t slot_pm_flags;
  930. mmc_pm_flag_t pm_flags = 0;
  931. int i, ret;
  932. chip = pci_get_drvdata(pdev);
  933. if (!chip)
  934. return 0;
  935. for (i = 0; i < chip->num_slots; i++) {
  936. slot = chip->slots[i];
  937. if (!slot)
  938. continue;
  939. ret = sdhci_suspend_host(slot->host);
  940. if (ret)
  941. goto err_pci_suspend;
  942. slot_pm_flags = slot->host->mmc->pm_flags;
  943. if (slot_pm_flags & MMC_PM_WAKE_SDIO_IRQ)
  944. sdhci_enable_irq_wakeups(slot->host);
  945. pm_flags |= slot_pm_flags;
  946. }
  947. if (chip->fixes && chip->fixes->suspend) {
  948. ret = chip->fixes->suspend(chip);
  949. if (ret)
  950. goto err_pci_suspend;
  951. }
  952. pci_save_state(pdev);
  953. if (pm_flags & MMC_PM_KEEP_POWER) {
  954. if (pm_flags & MMC_PM_WAKE_SDIO_IRQ) {
  955. pci_pme_active(pdev, true);
  956. pci_enable_wake(pdev, PCI_D3hot, 1);
  957. }
  958. pci_set_power_state(pdev, PCI_D3hot);
  959. } else {
  960. pci_enable_wake(pdev, PCI_D3hot, 0);
  961. pci_disable_device(pdev);
  962. pci_set_power_state(pdev, PCI_D3hot);
  963. }
  964. return 0;
  965. err_pci_suspend:
  966. while (--i >= 0)
  967. sdhci_resume_host(chip->slots[i]->host);
  968. return ret;
  969. }
  970. static int sdhci_pci_resume(struct device *dev)
  971. {
  972. struct pci_dev *pdev = to_pci_dev(dev);
  973. struct sdhci_pci_chip *chip;
  974. struct sdhci_pci_slot *slot;
  975. int i, ret;
  976. chip = pci_get_drvdata(pdev);
  977. if (!chip)
  978. return 0;
  979. pci_set_power_state(pdev, PCI_D0);
  980. pci_restore_state(pdev);
  981. ret = pci_enable_device(pdev);
  982. if (ret)
  983. return ret;
  984. if (chip->fixes && chip->fixes->resume) {
  985. ret = chip->fixes->resume(chip);
  986. if (ret)
  987. return ret;
  988. }
  989. for (i = 0; i < chip->num_slots; i++) {
  990. slot = chip->slots[i];
  991. if (!slot)
  992. continue;
  993. ret = sdhci_resume_host(slot->host);
  994. if (ret)
  995. return ret;
  996. }
  997. return 0;
  998. }
  999. #else /* CONFIG_PM */
  1000. #define sdhci_pci_suspend NULL
  1001. #define sdhci_pci_resume NULL
  1002. #endif /* CONFIG_PM */
  1003. #ifdef CONFIG_PM_RUNTIME
  1004. static int sdhci_pci_runtime_suspend(struct device *dev)
  1005. {
  1006. struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
  1007. struct sdhci_pci_chip *chip;
  1008. struct sdhci_pci_slot *slot;
  1009. int i, ret;
  1010. chip = pci_get_drvdata(pdev);
  1011. if (!chip)
  1012. return 0;
  1013. for (i = 0; i < chip->num_slots; i++) {
  1014. slot = chip->slots[i];
  1015. if (!slot)
  1016. continue;
  1017. ret = sdhci_runtime_suspend_host(slot->host);
  1018. if (ret)
  1019. goto err_pci_runtime_suspend;
  1020. }
  1021. if (chip->fixes && chip->fixes->suspend) {
  1022. ret = chip->fixes->suspend(chip);
  1023. if (ret)
  1024. goto err_pci_runtime_suspend;
  1025. }
  1026. return 0;
  1027. err_pci_runtime_suspend:
  1028. while (--i >= 0)
  1029. sdhci_runtime_resume_host(chip->slots[i]->host);
  1030. return ret;
  1031. }
  1032. static int sdhci_pci_runtime_resume(struct device *dev)
  1033. {
  1034. struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
  1035. struct sdhci_pci_chip *chip;
  1036. struct sdhci_pci_slot *slot;
  1037. int i, ret;
  1038. chip = pci_get_drvdata(pdev);
  1039. if (!chip)
  1040. return 0;
  1041. if (chip->fixes && chip->fixes->resume) {
  1042. ret = chip->fixes->resume(chip);
  1043. if (ret)
  1044. return ret;
  1045. }
  1046. for (i = 0; i < chip->num_slots; i++) {
  1047. slot = chip->slots[i];
  1048. if (!slot)
  1049. continue;
  1050. ret = sdhci_runtime_resume_host(slot->host);
  1051. if (ret)
  1052. return ret;
  1053. }
  1054. return 0;
  1055. }
  1056. static int sdhci_pci_runtime_idle(struct device *dev)
  1057. {
  1058. return 0;
  1059. }
  1060. #else
  1061. #define sdhci_pci_runtime_suspend NULL
  1062. #define sdhci_pci_runtime_resume NULL
  1063. #define sdhci_pci_runtime_idle NULL
  1064. #endif
  1065. static const struct dev_pm_ops sdhci_pci_pm_ops = {
  1066. .suspend = sdhci_pci_suspend,
  1067. .resume = sdhci_pci_resume,
  1068. .runtime_suspend = sdhci_pci_runtime_suspend,
  1069. .runtime_resume = sdhci_pci_runtime_resume,
  1070. .runtime_idle = sdhci_pci_runtime_idle,
  1071. };
  1072. /*****************************************************************************\
  1073. * *
  1074. * Device probing/removal *
  1075. * *
  1076. \*****************************************************************************/
  1077. static struct sdhci_pci_slot *sdhci_pci_probe_slot(
  1078. struct pci_dev *pdev, struct sdhci_pci_chip *chip, int first_bar,
  1079. int slotno)
  1080. {
  1081. struct sdhci_pci_slot *slot;
  1082. struct sdhci_host *host;
  1083. int ret, bar = first_bar + slotno;
  1084. if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
  1085. dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
  1086. return ERR_PTR(-ENODEV);
  1087. }
  1088. if (pci_resource_len(pdev, bar) < 0x100) {
  1089. dev_err(&pdev->dev, "Invalid iomem size. You may "
  1090. "experience problems.\n");
  1091. }
  1092. if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
  1093. dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
  1094. return ERR_PTR(-ENODEV);
  1095. }
  1096. if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
  1097. dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
  1098. return ERR_PTR(-ENODEV);
  1099. }
  1100. host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
  1101. if (IS_ERR(host)) {
  1102. dev_err(&pdev->dev, "cannot allocate host\n");
  1103. return ERR_CAST(host);
  1104. }
  1105. slot = sdhci_priv(host);
  1106. slot->chip = chip;
  1107. slot->host = host;
  1108. slot->pci_bar = bar;
  1109. slot->rst_n_gpio = -EINVAL;
  1110. slot->cd_gpio = -EINVAL;
  1111. /* Retrieve platform data if there is any */
  1112. if (*sdhci_pci_get_data)
  1113. slot->data = sdhci_pci_get_data(pdev, slotno);
  1114. if (slot->data) {
  1115. if (slot->data->setup) {
  1116. ret = slot->data->setup(slot->data);
  1117. if (ret) {
  1118. dev_err(&pdev->dev, "platform setup failed\n");
  1119. goto free;
  1120. }
  1121. }
  1122. slot->rst_n_gpio = slot->data->rst_n_gpio;
  1123. slot->cd_gpio = slot->data->cd_gpio;
  1124. }
  1125. host->hw_name = "PCI";
  1126. host->ops = &sdhci_pci_ops;
  1127. host->quirks = chip->quirks;
  1128. host->quirks2 = chip->quirks2;
  1129. host->irq = pdev->irq;
  1130. ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
  1131. if (ret) {
  1132. dev_err(&pdev->dev, "cannot request region\n");
  1133. goto cleanup;
  1134. }
  1135. host->ioaddr = pci_ioremap_bar(pdev, bar);
  1136. if (!host->ioaddr) {
  1137. dev_err(&pdev->dev, "failed to remap registers\n");
  1138. ret = -ENOMEM;
  1139. goto release;
  1140. }
  1141. if (chip->fixes && chip->fixes->probe_slot) {
  1142. ret = chip->fixes->probe_slot(slot);
  1143. if (ret)
  1144. goto unmap;
  1145. }
  1146. if (gpio_is_valid(slot->rst_n_gpio)) {
  1147. if (!gpio_request(slot->rst_n_gpio, "eMMC_reset")) {
  1148. gpio_direction_output(slot->rst_n_gpio, 1);
  1149. slot->host->mmc->caps |= MMC_CAP_HW_RESET;
  1150. slot->hw_reset = sdhci_pci_gpio_hw_reset;
  1151. } else {
  1152. dev_warn(&pdev->dev, "failed to request rst_n_gpio\n");
  1153. slot->rst_n_gpio = -EINVAL;
  1154. }
  1155. }
  1156. host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
  1157. host->mmc->slotno = slotno;
  1158. host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
  1159. ret = sdhci_add_host(host);
  1160. if (ret)
  1161. goto remove;
  1162. sdhci_pci_add_own_cd(slot);
  1163. /*
  1164. * Check if the chip needs a separate GPIO for card detect to wake up
  1165. * from runtime suspend. If it is not there, don't allow runtime PM.
  1166. * Note sdhci_pci_add_own_cd() sets slot->cd_gpio to -EINVAL on failure.
  1167. */
  1168. if (chip->fixes && chip->fixes->own_cd_for_runtime_pm &&
  1169. !gpio_is_valid(slot->cd_gpio))
  1170. chip->allow_runtime_pm = false;
  1171. return slot;
  1172. remove:
  1173. if (gpio_is_valid(slot->rst_n_gpio))
  1174. gpio_free(slot->rst_n_gpio);
  1175. if (chip->fixes && chip->fixes->remove_slot)
  1176. chip->fixes->remove_slot(slot, 0);
  1177. unmap:
  1178. iounmap(host->ioaddr);
  1179. release:
  1180. pci_release_region(pdev, bar);
  1181. cleanup:
  1182. if (slot->data && slot->data->cleanup)
  1183. slot->data->cleanup(slot->data);
  1184. free:
  1185. sdhci_free_host(host);
  1186. return ERR_PTR(ret);
  1187. }
  1188. static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
  1189. {
  1190. int dead;
  1191. u32 scratch;
  1192. sdhci_pci_remove_own_cd(slot);
  1193. dead = 0;
  1194. scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
  1195. if (scratch == (u32)-1)
  1196. dead = 1;
  1197. sdhci_remove_host(slot->host, dead);
  1198. if (gpio_is_valid(slot->rst_n_gpio))
  1199. gpio_free(slot->rst_n_gpio);
  1200. if (slot->chip->fixes && slot->chip->fixes->remove_slot)
  1201. slot->chip->fixes->remove_slot(slot, dead);
  1202. if (slot->data && slot->data->cleanup)
  1203. slot->data->cleanup(slot->data);
  1204. pci_release_region(slot->chip->pdev, slot->pci_bar);
  1205. sdhci_free_host(slot->host);
  1206. }
  1207. static void sdhci_pci_runtime_pm_allow(struct device *dev)
  1208. {
  1209. pm_runtime_put_noidle(dev);
  1210. pm_runtime_allow(dev);
  1211. pm_runtime_set_autosuspend_delay(dev, 50);
  1212. pm_runtime_use_autosuspend(dev);
  1213. pm_suspend_ignore_children(dev, 1);
  1214. }
  1215. static void sdhci_pci_runtime_pm_forbid(struct device *dev)
  1216. {
  1217. pm_runtime_forbid(dev);
  1218. pm_runtime_get_noresume(dev);
  1219. }
  1220. static int sdhci_pci_probe(struct pci_dev *pdev,
  1221. const struct pci_device_id *ent)
  1222. {
  1223. struct sdhci_pci_chip *chip;
  1224. struct sdhci_pci_slot *slot;
  1225. u8 slots, first_bar;
  1226. int ret, i;
  1227. BUG_ON(pdev == NULL);
  1228. BUG_ON(ent == NULL);
  1229. dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
  1230. (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
  1231. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
  1232. if (ret)
  1233. return ret;
  1234. slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
  1235. dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
  1236. if (slots == 0)
  1237. return -ENODEV;
  1238. BUG_ON(slots > MAX_SLOTS);
  1239. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
  1240. if (ret)
  1241. return ret;
  1242. first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
  1243. if (first_bar > 5) {
  1244. dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
  1245. return -ENODEV;
  1246. }
  1247. ret = pci_enable_device(pdev);
  1248. if (ret)
  1249. return ret;
  1250. chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
  1251. if (!chip) {
  1252. ret = -ENOMEM;
  1253. goto err;
  1254. }
  1255. chip->pdev = pdev;
  1256. chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
  1257. if (chip->fixes) {
  1258. chip->quirks = chip->fixes->quirks;
  1259. chip->quirks2 = chip->fixes->quirks2;
  1260. chip->allow_runtime_pm = chip->fixes->allow_runtime_pm;
  1261. }
  1262. chip->num_slots = slots;
  1263. pci_set_drvdata(pdev, chip);
  1264. if (chip->fixes && chip->fixes->probe) {
  1265. ret = chip->fixes->probe(chip);
  1266. if (ret)
  1267. goto free;
  1268. }
  1269. slots = chip->num_slots; /* Quirk may have changed this */
  1270. for (i = 0; i < slots; i++) {
  1271. slot = sdhci_pci_probe_slot(pdev, chip, first_bar, i);
  1272. if (IS_ERR(slot)) {
  1273. for (i--; i >= 0; i--)
  1274. sdhci_pci_remove_slot(chip->slots[i]);
  1275. ret = PTR_ERR(slot);
  1276. goto free;
  1277. }
  1278. chip->slots[i] = slot;
  1279. }
  1280. if (chip->allow_runtime_pm)
  1281. sdhci_pci_runtime_pm_allow(&pdev->dev);
  1282. return 0;
  1283. free:
  1284. pci_set_drvdata(pdev, NULL);
  1285. kfree(chip);
  1286. err:
  1287. pci_disable_device(pdev);
  1288. return ret;
  1289. }
  1290. static void sdhci_pci_remove(struct pci_dev *pdev)
  1291. {
  1292. int i;
  1293. struct sdhci_pci_chip *chip;
  1294. chip = pci_get_drvdata(pdev);
  1295. if (chip) {
  1296. if (chip->allow_runtime_pm)
  1297. sdhci_pci_runtime_pm_forbid(&pdev->dev);
  1298. for (i = 0; i < chip->num_slots; i++)
  1299. sdhci_pci_remove_slot(chip->slots[i]);
  1300. pci_set_drvdata(pdev, NULL);
  1301. kfree(chip);
  1302. }
  1303. pci_disable_device(pdev);
  1304. }
  1305. static struct pci_driver sdhci_driver = {
  1306. .name = "sdhci-pci",
  1307. .id_table = pci_ids,
  1308. .probe = sdhci_pci_probe,
  1309. .remove = sdhci_pci_remove,
  1310. .driver = {
  1311. .pm = &sdhci_pci_pm_ops
  1312. },
  1313. };
  1314. module_pci_driver(sdhci_driver);
  1315. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  1316. MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
  1317. MODULE_LICENSE("GPL");