sdhci-pci.c 37 KB

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