sdhci-pci.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  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 int sdhci_pci_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. return 0;
  891. }
  892. static void sdhci_pci_gpio_hw_reset(struct sdhci_host *host)
  893. {
  894. struct sdhci_pci_slot *slot = sdhci_priv(host);
  895. int rst_n_gpio = slot->rst_n_gpio;
  896. if (!gpio_is_valid(rst_n_gpio))
  897. return;
  898. gpio_set_value_cansleep(rst_n_gpio, 0);
  899. /* For eMMC, minimum is 1us but give it 10us for good measure */
  900. udelay(10);
  901. gpio_set_value_cansleep(rst_n_gpio, 1);
  902. /* For eMMC, minimum is 200us but give it 300us for good measure */
  903. usleep_range(300, 1000);
  904. }
  905. static void sdhci_pci_hw_reset(struct sdhci_host *host)
  906. {
  907. struct sdhci_pci_slot *slot = sdhci_priv(host);
  908. if (slot->hw_reset)
  909. slot->hw_reset(host);
  910. }
  911. static const struct sdhci_ops sdhci_pci_ops = {
  912. .enable_dma = sdhci_pci_enable_dma,
  913. .platform_bus_width = sdhci_pci_bus_width,
  914. .hw_reset = sdhci_pci_hw_reset,
  915. };
  916. /*****************************************************************************\
  917. * *
  918. * Suspend/resume *
  919. * *
  920. \*****************************************************************************/
  921. #ifdef CONFIG_PM
  922. static int sdhci_pci_suspend(struct device *dev)
  923. {
  924. struct pci_dev *pdev = to_pci_dev(dev);
  925. struct sdhci_pci_chip *chip;
  926. struct sdhci_pci_slot *slot;
  927. mmc_pm_flag_t slot_pm_flags;
  928. mmc_pm_flag_t pm_flags = 0;
  929. int i, ret;
  930. chip = pci_get_drvdata(pdev);
  931. if (!chip)
  932. return 0;
  933. for (i = 0; i < chip->num_slots; i++) {
  934. slot = chip->slots[i];
  935. if (!slot)
  936. continue;
  937. ret = sdhci_suspend_host(slot->host);
  938. if (ret)
  939. goto err_pci_suspend;
  940. slot_pm_flags = slot->host->mmc->pm_flags;
  941. if (slot_pm_flags & MMC_PM_WAKE_SDIO_IRQ)
  942. sdhci_enable_irq_wakeups(slot->host);
  943. pm_flags |= slot_pm_flags;
  944. }
  945. if (chip->fixes && chip->fixes->suspend) {
  946. ret = chip->fixes->suspend(chip);
  947. if (ret)
  948. goto err_pci_suspend;
  949. }
  950. pci_save_state(pdev);
  951. if (pm_flags & MMC_PM_KEEP_POWER) {
  952. if (pm_flags & MMC_PM_WAKE_SDIO_IRQ) {
  953. pci_pme_active(pdev, true);
  954. pci_enable_wake(pdev, PCI_D3hot, 1);
  955. }
  956. pci_set_power_state(pdev, PCI_D3hot);
  957. } else {
  958. pci_enable_wake(pdev, PCI_D3hot, 0);
  959. pci_disable_device(pdev);
  960. pci_set_power_state(pdev, PCI_D3hot);
  961. }
  962. return 0;
  963. err_pci_suspend:
  964. while (--i >= 0)
  965. sdhci_resume_host(chip->slots[i]->host);
  966. return ret;
  967. }
  968. static int sdhci_pci_resume(struct device *dev)
  969. {
  970. struct pci_dev *pdev = to_pci_dev(dev);
  971. struct sdhci_pci_chip *chip;
  972. struct sdhci_pci_slot *slot;
  973. int i, ret;
  974. chip = pci_get_drvdata(pdev);
  975. if (!chip)
  976. return 0;
  977. pci_set_power_state(pdev, PCI_D0);
  978. pci_restore_state(pdev);
  979. ret = pci_enable_device(pdev);
  980. if (ret)
  981. return ret;
  982. if (chip->fixes && chip->fixes->resume) {
  983. ret = chip->fixes->resume(chip);
  984. if (ret)
  985. return ret;
  986. }
  987. for (i = 0; i < chip->num_slots; i++) {
  988. slot = chip->slots[i];
  989. if (!slot)
  990. continue;
  991. ret = sdhci_resume_host(slot->host);
  992. if (ret)
  993. return ret;
  994. }
  995. return 0;
  996. }
  997. #else /* CONFIG_PM */
  998. #define sdhci_pci_suspend NULL
  999. #define sdhci_pci_resume NULL
  1000. #endif /* CONFIG_PM */
  1001. #ifdef CONFIG_PM_RUNTIME
  1002. static int sdhci_pci_runtime_suspend(struct device *dev)
  1003. {
  1004. struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
  1005. struct sdhci_pci_chip *chip;
  1006. struct sdhci_pci_slot *slot;
  1007. int i, ret;
  1008. chip = pci_get_drvdata(pdev);
  1009. if (!chip)
  1010. return 0;
  1011. for (i = 0; i < chip->num_slots; i++) {
  1012. slot = chip->slots[i];
  1013. if (!slot)
  1014. continue;
  1015. ret = sdhci_runtime_suspend_host(slot->host);
  1016. if (ret)
  1017. goto err_pci_runtime_suspend;
  1018. }
  1019. if (chip->fixes && chip->fixes->suspend) {
  1020. ret = chip->fixes->suspend(chip);
  1021. if (ret)
  1022. goto err_pci_runtime_suspend;
  1023. }
  1024. return 0;
  1025. err_pci_runtime_suspend:
  1026. while (--i >= 0)
  1027. sdhci_runtime_resume_host(chip->slots[i]->host);
  1028. return ret;
  1029. }
  1030. static int sdhci_pci_runtime_resume(struct device *dev)
  1031. {
  1032. struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
  1033. struct sdhci_pci_chip *chip;
  1034. struct sdhci_pci_slot *slot;
  1035. int i, ret;
  1036. chip = pci_get_drvdata(pdev);
  1037. if (!chip)
  1038. return 0;
  1039. if (chip->fixes && chip->fixes->resume) {
  1040. ret = chip->fixes->resume(chip);
  1041. if (ret)
  1042. return ret;
  1043. }
  1044. for (i = 0; i < chip->num_slots; i++) {
  1045. slot = chip->slots[i];
  1046. if (!slot)
  1047. continue;
  1048. ret = sdhci_runtime_resume_host(slot->host);
  1049. if (ret)
  1050. return ret;
  1051. }
  1052. return 0;
  1053. }
  1054. static int sdhci_pci_runtime_idle(struct device *dev)
  1055. {
  1056. return 0;
  1057. }
  1058. #else
  1059. #define sdhci_pci_runtime_suspend NULL
  1060. #define sdhci_pci_runtime_resume NULL
  1061. #define sdhci_pci_runtime_idle NULL
  1062. #endif
  1063. static const struct dev_pm_ops sdhci_pci_pm_ops = {
  1064. .suspend = sdhci_pci_suspend,
  1065. .resume = sdhci_pci_resume,
  1066. .runtime_suspend = sdhci_pci_runtime_suspend,
  1067. .runtime_resume = sdhci_pci_runtime_resume,
  1068. .runtime_idle = sdhci_pci_runtime_idle,
  1069. };
  1070. /*****************************************************************************\
  1071. * *
  1072. * Device probing/removal *
  1073. * *
  1074. \*****************************************************************************/
  1075. static struct sdhci_pci_slot *sdhci_pci_probe_slot(
  1076. struct pci_dev *pdev, struct sdhci_pci_chip *chip, int first_bar,
  1077. int slotno)
  1078. {
  1079. struct sdhci_pci_slot *slot;
  1080. struct sdhci_host *host;
  1081. int ret, bar = first_bar + slotno;
  1082. if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
  1083. dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
  1084. return ERR_PTR(-ENODEV);
  1085. }
  1086. if (pci_resource_len(pdev, bar) < 0x100) {
  1087. dev_err(&pdev->dev, "Invalid iomem size. You may "
  1088. "experience problems.\n");
  1089. }
  1090. if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
  1091. dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
  1092. return ERR_PTR(-ENODEV);
  1093. }
  1094. if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
  1095. dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
  1096. return ERR_PTR(-ENODEV);
  1097. }
  1098. host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
  1099. if (IS_ERR(host)) {
  1100. dev_err(&pdev->dev, "cannot allocate host\n");
  1101. return ERR_CAST(host);
  1102. }
  1103. slot = sdhci_priv(host);
  1104. slot->chip = chip;
  1105. slot->host = host;
  1106. slot->pci_bar = bar;
  1107. slot->rst_n_gpio = -EINVAL;
  1108. slot->cd_gpio = -EINVAL;
  1109. /* Retrieve platform data if there is any */
  1110. if (*sdhci_pci_get_data)
  1111. slot->data = sdhci_pci_get_data(pdev, slotno);
  1112. if (slot->data) {
  1113. if (slot->data->setup) {
  1114. ret = slot->data->setup(slot->data);
  1115. if (ret) {
  1116. dev_err(&pdev->dev, "platform setup failed\n");
  1117. goto free;
  1118. }
  1119. }
  1120. slot->rst_n_gpio = slot->data->rst_n_gpio;
  1121. slot->cd_gpio = slot->data->cd_gpio;
  1122. }
  1123. host->hw_name = "PCI";
  1124. host->ops = &sdhci_pci_ops;
  1125. host->quirks = chip->quirks;
  1126. host->quirks2 = chip->quirks2;
  1127. host->irq = pdev->irq;
  1128. ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
  1129. if (ret) {
  1130. dev_err(&pdev->dev, "cannot request region\n");
  1131. goto cleanup;
  1132. }
  1133. host->ioaddr = pci_ioremap_bar(pdev, bar);
  1134. if (!host->ioaddr) {
  1135. dev_err(&pdev->dev, "failed to remap registers\n");
  1136. ret = -ENOMEM;
  1137. goto release;
  1138. }
  1139. if (chip->fixes && chip->fixes->probe_slot) {
  1140. ret = chip->fixes->probe_slot(slot);
  1141. if (ret)
  1142. goto unmap;
  1143. }
  1144. if (gpio_is_valid(slot->rst_n_gpio)) {
  1145. if (!gpio_request(slot->rst_n_gpio, "eMMC_reset")) {
  1146. gpio_direction_output(slot->rst_n_gpio, 1);
  1147. slot->host->mmc->caps |= MMC_CAP_HW_RESET;
  1148. slot->hw_reset = sdhci_pci_gpio_hw_reset;
  1149. } else {
  1150. dev_warn(&pdev->dev, "failed to request rst_n_gpio\n");
  1151. slot->rst_n_gpio = -EINVAL;
  1152. }
  1153. }
  1154. host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
  1155. host->mmc->slotno = slotno;
  1156. host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
  1157. ret = sdhci_add_host(host);
  1158. if (ret)
  1159. goto remove;
  1160. sdhci_pci_add_own_cd(slot);
  1161. /*
  1162. * Check if the chip needs a separate GPIO for card detect to wake up
  1163. * from runtime suspend. If it is not there, don't allow runtime PM.
  1164. * Note sdhci_pci_add_own_cd() sets slot->cd_gpio to -EINVAL on failure.
  1165. */
  1166. if (chip->fixes && chip->fixes->own_cd_for_runtime_pm &&
  1167. !gpio_is_valid(slot->cd_gpio))
  1168. chip->allow_runtime_pm = false;
  1169. return slot;
  1170. remove:
  1171. if (gpio_is_valid(slot->rst_n_gpio))
  1172. gpio_free(slot->rst_n_gpio);
  1173. if (chip->fixes && chip->fixes->remove_slot)
  1174. chip->fixes->remove_slot(slot, 0);
  1175. unmap:
  1176. iounmap(host->ioaddr);
  1177. release:
  1178. pci_release_region(pdev, bar);
  1179. cleanup:
  1180. if (slot->data && slot->data->cleanup)
  1181. slot->data->cleanup(slot->data);
  1182. free:
  1183. sdhci_free_host(host);
  1184. return ERR_PTR(ret);
  1185. }
  1186. static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
  1187. {
  1188. int dead;
  1189. u32 scratch;
  1190. sdhci_pci_remove_own_cd(slot);
  1191. dead = 0;
  1192. scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
  1193. if (scratch == (u32)-1)
  1194. dead = 1;
  1195. sdhci_remove_host(slot->host, dead);
  1196. if (gpio_is_valid(slot->rst_n_gpio))
  1197. gpio_free(slot->rst_n_gpio);
  1198. if (slot->chip->fixes && slot->chip->fixes->remove_slot)
  1199. slot->chip->fixes->remove_slot(slot, dead);
  1200. if (slot->data && slot->data->cleanup)
  1201. slot->data->cleanup(slot->data);
  1202. pci_release_region(slot->chip->pdev, slot->pci_bar);
  1203. sdhci_free_host(slot->host);
  1204. }
  1205. static void sdhci_pci_runtime_pm_allow(struct device *dev)
  1206. {
  1207. pm_runtime_put_noidle(dev);
  1208. pm_runtime_allow(dev);
  1209. pm_runtime_set_autosuspend_delay(dev, 50);
  1210. pm_runtime_use_autosuspend(dev);
  1211. pm_suspend_ignore_children(dev, 1);
  1212. }
  1213. static void sdhci_pci_runtime_pm_forbid(struct device *dev)
  1214. {
  1215. pm_runtime_forbid(dev);
  1216. pm_runtime_get_noresume(dev);
  1217. }
  1218. static int sdhci_pci_probe(struct pci_dev *pdev,
  1219. const struct pci_device_id *ent)
  1220. {
  1221. struct sdhci_pci_chip *chip;
  1222. struct sdhci_pci_slot *slot;
  1223. u8 slots, first_bar;
  1224. int ret, i;
  1225. BUG_ON(pdev == NULL);
  1226. BUG_ON(ent == NULL);
  1227. dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
  1228. (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
  1229. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
  1230. if (ret)
  1231. return ret;
  1232. slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
  1233. dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
  1234. if (slots == 0)
  1235. return -ENODEV;
  1236. BUG_ON(slots > MAX_SLOTS);
  1237. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
  1238. if (ret)
  1239. return ret;
  1240. first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
  1241. if (first_bar > 5) {
  1242. dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
  1243. return -ENODEV;
  1244. }
  1245. ret = pci_enable_device(pdev);
  1246. if (ret)
  1247. return ret;
  1248. chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
  1249. if (!chip) {
  1250. ret = -ENOMEM;
  1251. goto err;
  1252. }
  1253. chip->pdev = pdev;
  1254. chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
  1255. if (chip->fixes) {
  1256. chip->quirks = chip->fixes->quirks;
  1257. chip->quirks2 = chip->fixes->quirks2;
  1258. chip->allow_runtime_pm = chip->fixes->allow_runtime_pm;
  1259. }
  1260. chip->num_slots = slots;
  1261. pci_set_drvdata(pdev, chip);
  1262. if (chip->fixes && chip->fixes->probe) {
  1263. ret = chip->fixes->probe(chip);
  1264. if (ret)
  1265. goto free;
  1266. }
  1267. slots = chip->num_slots; /* Quirk may have changed this */
  1268. for (i = 0; i < slots; i++) {
  1269. slot = sdhci_pci_probe_slot(pdev, chip, first_bar, i);
  1270. if (IS_ERR(slot)) {
  1271. for (i--; i >= 0; i--)
  1272. sdhci_pci_remove_slot(chip->slots[i]);
  1273. ret = PTR_ERR(slot);
  1274. goto free;
  1275. }
  1276. chip->slots[i] = slot;
  1277. }
  1278. if (chip->allow_runtime_pm)
  1279. sdhci_pci_runtime_pm_allow(&pdev->dev);
  1280. return 0;
  1281. free:
  1282. pci_set_drvdata(pdev, NULL);
  1283. kfree(chip);
  1284. err:
  1285. pci_disable_device(pdev);
  1286. return ret;
  1287. }
  1288. static void sdhci_pci_remove(struct pci_dev *pdev)
  1289. {
  1290. int i;
  1291. struct sdhci_pci_chip *chip;
  1292. chip = pci_get_drvdata(pdev);
  1293. if (chip) {
  1294. if (chip->allow_runtime_pm)
  1295. sdhci_pci_runtime_pm_forbid(&pdev->dev);
  1296. for (i = 0; i < chip->num_slots; i++)
  1297. sdhci_pci_remove_slot(chip->slots[i]);
  1298. pci_set_drvdata(pdev, NULL);
  1299. kfree(chip);
  1300. }
  1301. pci_disable_device(pdev);
  1302. }
  1303. static struct pci_driver sdhci_driver = {
  1304. .name = "sdhci-pci",
  1305. .id_table = pci_ids,
  1306. .probe = sdhci_pci_probe,
  1307. .remove = sdhci_pci_remove,
  1308. .driver = {
  1309. .pm = &sdhci_pci_pm_ops
  1310. },
  1311. };
  1312. module_pci_driver(sdhci_driver);
  1313. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  1314. MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
  1315. MODULE_LICENSE("GPL");