sdhci-pci.c 37 KB

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