sdhci-pci.c 35 KB

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