sdhci-pci.c 36 KB

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