sdhci-pci.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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/sfi.h>
  26. #include <linux/pm_runtime.h>
  27. #include "sdhci.h"
  28. /*
  29. * PCI registers
  30. */
  31. #define PCI_SDHCI_IFPIO 0x00
  32. #define PCI_SDHCI_IFDMA 0x01
  33. #define PCI_SDHCI_IFVENDOR 0x02
  34. #define PCI_SLOT_INFO 0x40 /* 8 bits */
  35. #define PCI_SLOT_INFO_SLOTS(x) ((x >> 4) & 7)
  36. #define PCI_SLOT_INFO_FIRST_BAR_MASK 0x07
  37. #define MAX_SLOTS 8
  38. struct sdhci_pci_chip;
  39. struct sdhci_pci_slot;
  40. struct sdhci_pci_fixes {
  41. unsigned int quirks;
  42. bool allow_runtime_pm;
  43. int (*probe) (struct sdhci_pci_chip *);
  44. int (*probe_slot) (struct sdhci_pci_slot *);
  45. void (*remove_slot) (struct sdhci_pci_slot *, int);
  46. int (*suspend) (struct sdhci_pci_chip *,
  47. pm_message_t);
  48. int (*resume) (struct sdhci_pci_chip *);
  49. };
  50. struct sdhci_pci_slot {
  51. struct sdhci_pci_chip *chip;
  52. struct sdhci_host *host;
  53. int pci_bar;
  54. int rst_n_gpio;
  55. int cd_gpio;
  56. int cd_irq;
  57. };
  58. struct sdhci_pci_chip {
  59. struct pci_dev *pdev;
  60. unsigned int quirks;
  61. bool allow_runtime_pm;
  62. const struct sdhci_pci_fixes *fixes;
  63. int num_slots; /* Slots on controller */
  64. struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
  65. };
  66. /*****************************************************************************\
  67. * *
  68. * Hardware specific quirk handling *
  69. * *
  70. \*****************************************************************************/
  71. static int ricoh_probe(struct sdhci_pci_chip *chip)
  72. {
  73. if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
  74. chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
  75. chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
  76. return 0;
  77. }
  78. static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
  79. {
  80. slot->host->caps =
  81. ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
  82. & SDHCI_TIMEOUT_CLK_MASK) |
  83. ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
  84. & SDHCI_CLOCK_BASE_MASK) |
  85. SDHCI_TIMEOUT_CLK_UNIT |
  86. SDHCI_CAN_VDD_330 |
  87. SDHCI_CAN_DO_SDMA;
  88. return 0;
  89. }
  90. static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
  91. {
  92. /* Apply a delay to allow controller to settle */
  93. /* Otherwise it becomes confused if card state changed
  94. during suspend */
  95. msleep(500);
  96. return 0;
  97. }
  98. static const struct sdhci_pci_fixes sdhci_ricoh = {
  99. .probe = ricoh_probe,
  100. .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
  101. SDHCI_QUIRK_FORCE_DMA |
  102. SDHCI_QUIRK_CLOCK_BEFORE_RESET,
  103. };
  104. static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
  105. .probe_slot = ricoh_mmc_probe_slot,
  106. .resume = ricoh_mmc_resume,
  107. .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
  108. SDHCI_QUIRK_CLOCK_BEFORE_RESET |
  109. SDHCI_QUIRK_NO_CARD_NO_RESET |
  110. SDHCI_QUIRK_MISSING_CAPS
  111. };
  112. static const struct sdhci_pci_fixes sdhci_ene_712 = {
  113. .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
  114. SDHCI_QUIRK_BROKEN_DMA,
  115. };
  116. static const struct sdhci_pci_fixes sdhci_ene_714 = {
  117. .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
  118. SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
  119. SDHCI_QUIRK_BROKEN_DMA,
  120. };
  121. static const struct sdhci_pci_fixes sdhci_cafe = {
  122. .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
  123. SDHCI_QUIRK_NO_BUSY_IRQ |
  124. SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
  125. };
  126. static int mrst_hc_probe_slot(struct sdhci_pci_slot *slot)
  127. {
  128. slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  129. return 0;
  130. }
  131. /*
  132. * ADMA operation is disabled for Moorestown platform due to
  133. * hardware bugs.
  134. */
  135. static int mrst_hc_probe(struct sdhci_pci_chip *chip)
  136. {
  137. /*
  138. * slots number is fixed here for MRST as SDIO3/5 are never used and
  139. * have hardware bugs.
  140. */
  141. chip->num_slots = 1;
  142. return 0;
  143. }
  144. /* Medfield eMMC hardware reset GPIOs */
  145. static int mfd_emmc0_rst_gpio = -EINVAL;
  146. static int mfd_emmc1_rst_gpio = -EINVAL;
  147. static int mfd_emmc_gpio_parse(struct sfi_table_header *table)
  148. {
  149. struct sfi_table_simple *sb = (struct sfi_table_simple *)table;
  150. struct sfi_gpio_table_entry *entry;
  151. int i, num;
  152. num = SFI_GET_NUM_ENTRIES(sb, struct sfi_gpio_table_entry);
  153. entry = (struct sfi_gpio_table_entry *)sb->pentry;
  154. for (i = 0; i < num; i++, entry++) {
  155. if (!strncmp(entry->pin_name, "emmc0_rst", SFI_NAME_LEN))
  156. mfd_emmc0_rst_gpio = entry->pin_no;
  157. else if (!strncmp(entry->pin_name, "emmc1_rst", SFI_NAME_LEN))
  158. mfd_emmc1_rst_gpio = entry->pin_no;
  159. }
  160. return 0;
  161. }
  162. #ifdef CONFIG_PM_RUNTIME
  163. static irqreturn_t mfd_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. #define MFLD_SD_CD_PIN 69
  171. static int mfd_sd_probe_slot(struct sdhci_pci_slot *slot)
  172. {
  173. int err, irq, gpio = MFLD_SD_CD_PIN;
  174. slot->cd_gpio = -EINVAL;
  175. slot->cd_irq = -EINVAL;
  176. err = gpio_request(gpio, "sd_cd");
  177. if (err < 0)
  178. goto out;
  179. err = gpio_direction_input(gpio);
  180. if (err < 0)
  181. goto out_free;
  182. irq = gpio_to_irq(gpio);
  183. if (irq < 0)
  184. goto out_free;
  185. err = request_irq(irq, mfd_sd_cd, IRQF_TRIGGER_RISING |
  186. IRQF_TRIGGER_FALLING, "sd_cd", slot);
  187. if (err)
  188. goto out_free;
  189. slot->cd_gpio = gpio;
  190. slot->cd_irq = irq;
  191. slot->host->quirks2 |= SDHCI_QUIRK2_OWN_CARD_DETECTION;
  192. return 0;
  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. return 0;
  198. }
  199. static void mfd_sd_remove_slot(struct sdhci_pci_slot *slot, int dead)
  200. {
  201. if (slot->cd_irq >= 0)
  202. free_irq(slot->cd_irq, slot);
  203. gpio_free(slot->cd_gpio);
  204. }
  205. #else
  206. #define mfd_sd_probe_slot NULL
  207. #define mfd_sd_remove_slot NULL
  208. #endif
  209. static int mfd_emmc_probe_slot(struct sdhci_pci_slot *slot)
  210. {
  211. const char *name = NULL;
  212. int gpio = -EINVAL;
  213. sfi_table_parse(SFI_SIG_GPIO, NULL, NULL, mfd_emmc_gpio_parse);
  214. switch (slot->chip->pdev->device) {
  215. case PCI_DEVICE_ID_INTEL_MFD_EMMC0:
  216. gpio = mfd_emmc0_rst_gpio;
  217. name = "eMMC0_reset";
  218. break;
  219. case PCI_DEVICE_ID_INTEL_MFD_EMMC1:
  220. gpio = mfd_emmc1_rst_gpio;
  221. name = "eMMC1_reset";
  222. break;
  223. }
  224. if (!gpio_request(gpio, name)) {
  225. gpio_direction_output(gpio, 1);
  226. slot->rst_n_gpio = gpio;
  227. slot->host->mmc->caps |= MMC_CAP_HW_RESET;
  228. }
  229. slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE;
  230. slot->host->mmc->caps2 = MMC_CAP2_BOOTPART_NOACC;
  231. return 0;
  232. }
  233. static void mfd_emmc_remove_slot(struct sdhci_pci_slot *slot, int dead)
  234. {
  235. gpio_free(slot->rst_n_gpio);
  236. }
  237. static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
  238. .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
  239. .probe_slot = mrst_hc_probe_slot,
  240. };
  241. static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1_hc2 = {
  242. .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
  243. .probe = mrst_hc_probe,
  244. };
  245. static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
  246. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  247. .allow_runtime_pm = true,
  248. .probe_slot = mfd_sd_probe_slot,
  249. .remove_slot = mfd_sd_remove_slot,
  250. };
  251. static const struct sdhci_pci_fixes sdhci_intel_mfd_sdio = {
  252. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  253. .allow_runtime_pm = true,
  254. };
  255. static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc = {
  256. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  257. .allow_runtime_pm = true,
  258. .probe_slot = mfd_emmc_probe_slot,
  259. .remove_slot = mfd_emmc_remove_slot,
  260. };
  261. /* O2Micro extra registers */
  262. #define O2_SD_LOCK_WP 0xD3
  263. #define O2_SD_MULTI_VCC3V 0xEE
  264. #define O2_SD_CLKREQ 0xEC
  265. #define O2_SD_CAPS 0xE0
  266. #define O2_SD_ADMA1 0xE2
  267. #define O2_SD_ADMA2 0xE7
  268. #define O2_SD_INF_MOD 0xF1
  269. static int o2_probe(struct sdhci_pci_chip *chip)
  270. {
  271. int ret;
  272. u8 scratch;
  273. switch (chip->pdev->device) {
  274. case PCI_DEVICE_ID_O2_8220:
  275. case PCI_DEVICE_ID_O2_8221:
  276. case PCI_DEVICE_ID_O2_8320:
  277. case PCI_DEVICE_ID_O2_8321:
  278. /* This extra setup is required due to broken ADMA. */
  279. ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
  280. if (ret)
  281. return ret;
  282. scratch &= 0x7f;
  283. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  284. /* Set Multi 3 to VCC3V# */
  285. pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
  286. /* Disable CLK_REQ# support after media DET */
  287. ret = pci_read_config_byte(chip->pdev, O2_SD_CLKREQ, &scratch);
  288. if (ret)
  289. return ret;
  290. scratch |= 0x20;
  291. pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
  292. /* Choose capabilities, enable SDMA. We have to write 0x01
  293. * to the capabilities register first to unlock it.
  294. */
  295. ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
  296. if (ret)
  297. return ret;
  298. scratch |= 0x01;
  299. pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
  300. pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
  301. /* Disable ADMA1/2 */
  302. pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
  303. pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
  304. /* Disable the infinite transfer mode */
  305. ret = pci_read_config_byte(chip->pdev, O2_SD_INF_MOD, &scratch);
  306. if (ret)
  307. return ret;
  308. scratch |= 0x08;
  309. pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
  310. /* Lock WP */
  311. ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
  312. if (ret)
  313. return ret;
  314. scratch |= 0x80;
  315. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  316. }
  317. return 0;
  318. }
  319. static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
  320. {
  321. u8 scratch;
  322. int ret;
  323. ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
  324. if (ret)
  325. return ret;
  326. /*
  327. * Turn PMOS on [bit 0], set over current detection to 2.4 V
  328. * [bit 1:2] and enable over current debouncing [bit 6].
  329. */
  330. if (on)
  331. scratch |= 0x47;
  332. else
  333. scratch &= ~0x47;
  334. ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
  335. if (ret)
  336. return ret;
  337. return 0;
  338. }
  339. static int jmicron_probe(struct sdhci_pci_chip *chip)
  340. {
  341. int ret;
  342. u16 mmcdev = 0;
  343. if (chip->pdev->revision == 0) {
  344. chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
  345. SDHCI_QUIRK_32BIT_DMA_SIZE |
  346. SDHCI_QUIRK_32BIT_ADMA_SIZE |
  347. SDHCI_QUIRK_RESET_AFTER_REQUEST |
  348. SDHCI_QUIRK_BROKEN_SMALL_PIO;
  349. }
  350. /*
  351. * JMicron chips can have two interfaces to the same hardware
  352. * in order to work around limitations in Microsoft's driver.
  353. * We need to make sure we only bind to one of them.
  354. *
  355. * This code assumes two things:
  356. *
  357. * 1. The PCI code adds subfunctions in order.
  358. *
  359. * 2. The MMC interface has a lower subfunction number
  360. * than the SD interface.
  361. */
  362. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
  363. mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
  364. else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
  365. mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
  366. if (mmcdev) {
  367. struct pci_dev *sd_dev;
  368. sd_dev = NULL;
  369. while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
  370. mmcdev, sd_dev)) != NULL) {
  371. if ((PCI_SLOT(chip->pdev->devfn) ==
  372. PCI_SLOT(sd_dev->devfn)) &&
  373. (chip->pdev->bus == sd_dev->bus))
  374. break;
  375. }
  376. if (sd_dev) {
  377. pci_dev_put(sd_dev);
  378. dev_info(&chip->pdev->dev, "Refusing to bind to "
  379. "secondary interface.\n");
  380. return -ENODEV;
  381. }
  382. }
  383. /*
  384. * JMicron chips need a bit of a nudge to enable the power
  385. * output pins.
  386. */
  387. ret = jmicron_pmos(chip, 1);
  388. if (ret) {
  389. dev_err(&chip->pdev->dev, "Failure enabling card power\n");
  390. return ret;
  391. }
  392. /* quirk for unsable RO-detection on JM388 chips */
  393. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
  394. chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
  395. chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
  396. return 0;
  397. }
  398. static void jmicron_enable_mmc(struct sdhci_host *host, int on)
  399. {
  400. u8 scratch;
  401. scratch = readb(host->ioaddr + 0xC0);
  402. if (on)
  403. scratch |= 0x01;
  404. else
  405. scratch &= ~0x01;
  406. writeb(scratch, host->ioaddr + 0xC0);
  407. }
  408. static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
  409. {
  410. if (slot->chip->pdev->revision == 0) {
  411. u16 version;
  412. version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
  413. version = (version & SDHCI_VENDOR_VER_MASK) >>
  414. SDHCI_VENDOR_VER_SHIFT;
  415. /*
  416. * Older versions of the chip have lots of nasty glitches
  417. * in the ADMA engine. It's best just to avoid it
  418. * completely.
  419. */
  420. if (version < 0xAC)
  421. slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
  422. }
  423. /* JM388 MMC doesn't support 1.8V while SD supports it */
  424. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
  425. slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
  426. MMC_VDD_29_30 | MMC_VDD_30_31 |
  427. MMC_VDD_165_195; /* allow 1.8V */
  428. slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
  429. MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
  430. }
  431. /*
  432. * The secondary interface requires a bit set to get the
  433. * interrupts.
  434. */
  435. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  436. slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
  437. jmicron_enable_mmc(slot->host, 1);
  438. slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
  439. return 0;
  440. }
  441. static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
  442. {
  443. if (dead)
  444. return;
  445. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  446. slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
  447. jmicron_enable_mmc(slot->host, 0);
  448. }
  449. static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state)
  450. {
  451. int i;
  452. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  453. chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
  454. for (i = 0; i < chip->num_slots; i++)
  455. jmicron_enable_mmc(chip->slots[i]->host, 0);
  456. }
  457. return 0;
  458. }
  459. static int jmicron_resume(struct sdhci_pci_chip *chip)
  460. {
  461. int ret, i;
  462. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  463. chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
  464. for (i = 0; i < chip->num_slots; i++)
  465. jmicron_enable_mmc(chip->slots[i]->host, 1);
  466. }
  467. ret = jmicron_pmos(chip, 1);
  468. if (ret) {
  469. dev_err(&chip->pdev->dev, "Failure enabling card power\n");
  470. return ret;
  471. }
  472. return 0;
  473. }
  474. static const struct sdhci_pci_fixes sdhci_o2 = {
  475. .probe = o2_probe,
  476. };
  477. static const struct sdhci_pci_fixes sdhci_jmicron = {
  478. .probe = jmicron_probe,
  479. .probe_slot = jmicron_probe_slot,
  480. .remove_slot = jmicron_remove_slot,
  481. .suspend = jmicron_suspend,
  482. .resume = jmicron_resume,
  483. };
  484. /* SysKonnect CardBus2SDIO extra registers */
  485. #define SYSKT_CTRL 0x200
  486. #define SYSKT_RDFIFO_STAT 0x204
  487. #define SYSKT_WRFIFO_STAT 0x208
  488. #define SYSKT_POWER_DATA 0x20c
  489. #define SYSKT_POWER_330 0xef
  490. #define SYSKT_POWER_300 0xf8
  491. #define SYSKT_POWER_184 0xcc
  492. #define SYSKT_POWER_CMD 0x20d
  493. #define SYSKT_POWER_START (1 << 7)
  494. #define SYSKT_POWER_STATUS 0x20e
  495. #define SYSKT_POWER_STATUS_OK (1 << 0)
  496. #define SYSKT_BOARD_REV 0x210
  497. #define SYSKT_CHIP_REV 0x211
  498. #define SYSKT_CONF_DATA 0x212
  499. #define SYSKT_CONF_DATA_1V8 (1 << 2)
  500. #define SYSKT_CONF_DATA_2V5 (1 << 1)
  501. #define SYSKT_CONF_DATA_3V3 (1 << 0)
  502. static int syskt_probe(struct sdhci_pci_chip *chip)
  503. {
  504. if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
  505. chip->pdev->class &= ~0x0000FF;
  506. chip->pdev->class |= PCI_SDHCI_IFDMA;
  507. }
  508. return 0;
  509. }
  510. static int syskt_probe_slot(struct sdhci_pci_slot *slot)
  511. {
  512. int tm, ps;
  513. u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
  514. u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
  515. dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
  516. "board rev %d.%d, chip rev %d.%d\n",
  517. board_rev >> 4, board_rev & 0xf,
  518. chip_rev >> 4, chip_rev & 0xf);
  519. if (chip_rev >= 0x20)
  520. slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
  521. writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
  522. writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
  523. udelay(50);
  524. tm = 10; /* Wait max 1 ms */
  525. do {
  526. ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
  527. if (ps & SYSKT_POWER_STATUS_OK)
  528. break;
  529. udelay(100);
  530. } while (--tm);
  531. if (!tm) {
  532. dev_err(&slot->chip->pdev->dev,
  533. "power regulator never stabilized");
  534. writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
  535. return -ENODEV;
  536. }
  537. return 0;
  538. }
  539. static const struct sdhci_pci_fixes sdhci_syskt = {
  540. .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
  541. .probe = syskt_probe,
  542. .probe_slot = syskt_probe_slot,
  543. };
  544. static int via_probe(struct sdhci_pci_chip *chip)
  545. {
  546. if (chip->pdev->revision == 0x10)
  547. chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
  548. return 0;
  549. }
  550. static const struct sdhci_pci_fixes sdhci_via = {
  551. .probe = via_probe,
  552. };
  553. static const struct pci_device_id pci_ids[] __devinitdata = {
  554. {
  555. .vendor = PCI_VENDOR_ID_RICOH,
  556. .device = PCI_DEVICE_ID_RICOH_R5C822,
  557. .subvendor = PCI_ANY_ID,
  558. .subdevice = PCI_ANY_ID,
  559. .driver_data = (kernel_ulong_t)&sdhci_ricoh,
  560. },
  561. {
  562. .vendor = PCI_VENDOR_ID_RICOH,
  563. .device = 0x843,
  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_RICOH,
  570. .device = 0xe822,
  571. .subvendor = PCI_ANY_ID,
  572. .subdevice = PCI_ANY_ID,
  573. .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
  574. },
  575. {
  576. .vendor = PCI_VENDOR_ID_RICOH,
  577. .device = 0xe823,
  578. .subvendor = PCI_ANY_ID,
  579. .subdevice = PCI_ANY_ID,
  580. .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
  581. },
  582. {
  583. .vendor = PCI_VENDOR_ID_ENE,
  584. .device = PCI_DEVICE_ID_ENE_CB712_SD,
  585. .subvendor = PCI_ANY_ID,
  586. .subdevice = PCI_ANY_ID,
  587. .driver_data = (kernel_ulong_t)&sdhci_ene_712,
  588. },
  589. {
  590. .vendor = PCI_VENDOR_ID_ENE,
  591. .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
  592. .subvendor = PCI_ANY_ID,
  593. .subdevice = PCI_ANY_ID,
  594. .driver_data = (kernel_ulong_t)&sdhci_ene_712,
  595. },
  596. {
  597. .vendor = PCI_VENDOR_ID_ENE,
  598. .device = PCI_DEVICE_ID_ENE_CB714_SD,
  599. .subvendor = PCI_ANY_ID,
  600. .subdevice = PCI_ANY_ID,
  601. .driver_data = (kernel_ulong_t)&sdhci_ene_714,
  602. },
  603. {
  604. .vendor = PCI_VENDOR_ID_ENE,
  605. .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
  606. .subvendor = PCI_ANY_ID,
  607. .subdevice = PCI_ANY_ID,
  608. .driver_data = (kernel_ulong_t)&sdhci_ene_714,
  609. },
  610. {
  611. .vendor = PCI_VENDOR_ID_MARVELL,
  612. .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
  613. .subvendor = PCI_ANY_ID,
  614. .subdevice = PCI_ANY_ID,
  615. .driver_data = (kernel_ulong_t)&sdhci_cafe,
  616. },
  617. {
  618. .vendor = PCI_VENDOR_ID_JMICRON,
  619. .device = PCI_DEVICE_ID_JMICRON_JMB38X_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_JMB38X_MMC,
  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_JMICRON,
  633. .device = PCI_DEVICE_ID_JMICRON_JMB388_SD,
  634. .subvendor = PCI_ANY_ID,
  635. .subdevice = PCI_ANY_ID,
  636. .driver_data = (kernel_ulong_t)&sdhci_jmicron,
  637. },
  638. {
  639. .vendor = PCI_VENDOR_ID_JMICRON,
  640. .device = PCI_DEVICE_ID_JMICRON_JMB388_ESD,
  641. .subvendor = PCI_ANY_ID,
  642. .subdevice = PCI_ANY_ID,
  643. .driver_data = (kernel_ulong_t)&sdhci_jmicron,
  644. },
  645. {
  646. .vendor = PCI_VENDOR_ID_SYSKONNECT,
  647. .device = 0x8000,
  648. .subvendor = PCI_ANY_ID,
  649. .subdevice = PCI_ANY_ID,
  650. .driver_data = (kernel_ulong_t)&sdhci_syskt,
  651. },
  652. {
  653. .vendor = PCI_VENDOR_ID_VIA,
  654. .device = 0x95d0,
  655. .subvendor = PCI_ANY_ID,
  656. .subdevice = PCI_ANY_ID,
  657. .driver_data = (kernel_ulong_t)&sdhci_via,
  658. },
  659. {
  660. .vendor = PCI_VENDOR_ID_INTEL,
  661. .device = PCI_DEVICE_ID_INTEL_MRST_SD0,
  662. .subvendor = PCI_ANY_ID,
  663. .subdevice = PCI_ANY_ID,
  664. .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc0,
  665. },
  666. {
  667. .vendor = PCI_VENDOR_ID_INTEL,
  668. .device = PCI_DEVICE_ID_INTEL_MRST_SD1,
  669. .subvendor = PCI_ANY_ID,
  670. .subdevice = PCI_ANY_ID,
  671. .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
  672. },
  673. {
  674. .vendor = PCI_VENDOR_ID_INTEL,
  675. .device = PCI_DEVICE_ID_INTEL_MRST_SD2,
  676. .subvendor = PCI_ANY_ID,
  677. .subdevice = PCI_ANY_ID,
  678. .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
  679. },
  680. {
  681. .vendor = PCI_VENDOR_ID_INTEL,
  682. .device = PCI_DEVICE_ID_INTEL_MFD_SD,
  683. .subvendor = PCI_ANY_ID,
  684. .subdevice = PCI_ANY_ID,
  685. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
  686. },
  687. {
  688. .vendor = PCI_VENDOR_ID_INTEL,
  689. .device = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
  690. .subvendor = PCI_ANY_ID,
  691. .subdevice = PCI_ANY_ID,
  692. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
  693. },
  694. {
  695. .vendor = PCI_VENDOR_ID_INTEL,
  696. .device = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
  697. .subvendor = PCI_ANY_ID,
  698. .subdevice = PCI_ANY_ID,
  699. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
  700. },
  701. {
  702. .vendor = PCI_VENDOR_ID_INTEL,
  703. .device = PCI_DEVICE_ID_INTEL_MFD_EMMC0,
  704. .subvendor = PCI_ANY_ID,
  705. .subdevice = PCI_ANY_ID,
  706. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
  707. },
  708. {
  709. .vendor = PCI_VENDOR_ID_INTEL,
  710. .device = PCI_DEVICE_ID_INTEL_MFD_EMMC1,
  711. .subvendor = PCI_ANY_ID,
  712. .subdevice = PCI_ANY_ID,
  713. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
  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 pci_dev *pdev, pm_message_t state)
  825. {
  826. struct sdhci_pci_chip *chip;
  827. struct sdhci_pci_slot *slot;
  828. mmc_pm_flag_t slot_pm_flags;
  829. mmc_pm_flag_t pm_flags = 0;
  830. int i, ret;
  831. chip = pci_get_drvdata(pdev);
  832. if (!chip)
  833. return 0;
  834. for (i = 0; i < chip->num_slots; i++) {
  835. slot = chip->slots[i];
  836. if (!slot)
  837. continue;
  838. ret = sdhci_suspend_host(slot->host, state);
  839. if (ret) {
  840. for (i--; i >= 0; i--)
  841. sdhci_resume_host(chip->slots[i]->host);
  842. return ret;
  843. }
  844. slot_pm_flags = slot->host->mmc->pm_flags;
  845. if (slot_pm_flags & MMC_PM_WAKE_SDIO_IRQ)
  846. sdhci_enable_irq_wakeups(slot->host);
  847. pm_flags |= slot_pm_flags;
  848. }
  849. if (chip->fixes && chip->fixes->suspend) {
  850. ret = chip->fixes->suspend(chip, state);
  851. if (ret) {
  852. for (i = chip->num_slots - 1; i >= 0; i--)
  853. sdhci_resume_host(chip->slots[i]->host);
  854. return ret;
  855. }
  856. }
  857. pci_save_state(pdev);
  858. if (pm_flags & MMC_PM_KEEP_POWER) {
  859. if (pm_flags & MMC_PM_WAKE_SDIO_IRQ) {
  860. pci_pme_active(pdev, true);
  861. pci_enable_wake(pdev, PCI_D3hot, 1);
  862. }
  863. pci_set_power_state(pdev, PCI_D3hot);
  864. } else {
  865. pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
  866. pci_disable_device(pdev);
  867. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  868. }
  869. return 0;
  870. }
  871. static int sdhci_pci_resume(struct pci_dev *pdev)
  872. {
  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. pm_message_t state = { .event = PM_EVENT_SUSPEND };
  910. int i, ret;
  911. chip = pci_get_drvdata(pdev);
  912. if (!chip)
  913. return 0;
  914. for (i = 0; i < chip->num_slots; i++) {
  915. slot = chip->slots[i];
  916. if (!slot)
  917. continue;
  918. ret = sdhci_runtime_suspend_host(slot->host);
  919. if (ret) {
  920. for (i--; i >= 0; i--)
  921. sdhci_runtime_resume_host(chip->slots[i]->host);
  922. return ret;
  923. }
  924. }
  925. if (chip->fixes && chip->fixes->suspend) {
  926. ret = chip->fixes->suspend(chip, state);
  927. if (ret) {
  928. for (i = chip->num_slots - 1; i >= 0; i--)
  929. sdhci_runtime_resume_host(chip->slots[i]->host);
  930. return ret;
  931. }
  932. }
  933. return 0;
  934. }
  935. static int sdhci_pci_runtime_resume(struct device *dev)
  936. {
  937. struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
  938. struct sdhci_pci_chip *chip;
  939. struct sdhci_pci_slot *slot;
  940. int i, ret;
  941. chip = pci_get_drvdata(pdev);
  942. if (!chip)
  943. return 0;
  944. if (chip->fixes && chip->fixes->resume) {
  945. ret = chip->fixes->resume(chip);
  946. if (ret)
  947. return ret;
  948. }
  949. for (i = 0; i < chip->num_slots; i++) {
  950. slot = chip->slots[i];
  951. if (!slot)
  952. continue;
  953. ret = sdhci_runtime_resume_host(slot->host);
  954. if (ret)
  955. return ret;
  956. }
  957. return 0;
  958. }
  959. static int sdhci_pci_runtime_idle(struct device *dev)
  960. {
  961. return 0;
  962. }
  963. #else
  964. #define sdhci_pci_runtime_suspend NULL
  965. #define sdhci_pci_runtime_resume NULL
  966. #define sdhci_pci_runtime_idle NULL
  967. #endif
  968. static const struct dev_pm_ops sdhci_pci_pm_ops = {
  969. .runtime_suspend = sdhci_pci_runtime_suspend,
  970. .runtime_resume = sdhci_pci_runtime_resume,
  971. .runtime_idle = sdhci_pci_runtime_idle,
  972. };
  973. /*****************************************************************************\
  974. * *
  975. * Device probing/removal *
  976. * *
  977. \*****************************************************************************/
  978. static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
  979. struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
  980. {
  981. struct sdhci_pci_slot *slot;
  982. struct sdhci_host *host;
  983. int ret;
  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. host->hw_name = "PCI";
  1011. host->ops = &sdhci_pci_ops;
  1012. host->quirks = chip->quirks;
  1013. host->irq = pdev->irq;
  1014. ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
  1015. if (ret) {
  1016. dev_err(&pdev->dev, "cannot request region\n");
  1017. goto free;
  1018. }
  1019. host->ioaddr = pci_ioremap_bar(pdev, bar);
  1020. if (!host->ioaddr) {
  1021. dev_err(&pdev->dev, "failed to remap registers\n");
  1022. ret = -ENOMEM;
  1023. goto release;
  1024. }
  1025. if (chip->fixes && chip->fixes->probe_slot) {
  1026. ret = chip->fixes->probe_slot(slot);
  1027. if (ret)
  1028. goto unmap;
  1029. }
  1030. host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
  1031. ret = sdhci_add_host(host);
  1032. if (ret)
  1033. goto remove;
  1034. return slot;
  1035. remove:
  1036. if (chip->fixes && chip->fixes->remove_slot)
  1037. chip->fixes->remove_slot(slot, 0);
  1038. unmap:
  1039. iounmap(host->ioaddr);
  1040. release:
  1041. pci_release_region(pdev, bar);
  1042. free:
  1043. sdhci_free_host(host);
  1044. return ERR_PTR(ret);
  1045. }
  1046. static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
  1047. {
  1048. int dead;
  1049. u32 scratch;
  1050. dead = 0;
  1051. scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
  1052. if (scratch == (u32)-1)
  1053. dead = 1;
  1054. sdhci_remove_host(slot->host, dead);
  1055. if (slot->chip->fixes && slot->chip->fixes->remove_slot)
  1056. slot->chip->fixes->remove_slot(slot, dead);
  1057. pci_release_region(slot->chip->pdev, slot->pci_bar);
  1058. sdhci_free_host(slot->host);
  1059. }
  1060. static void __devinit sdhci_pci_runtime_pm_allow(struct device *dev)
  1061. {
  1062. pm_runtime_put_noidle(dev);
  1063. pm_runtime_allow(dev);
  1064. pm_runtime_set_autosuspend_delay(dev, 50);
  1065. pm_runtime_use_autosuspend(dev);
  1066. pm_suspend_ignore_children(dev, 1);
  1067. }
  1068. static void __devexit sdhci_pci_runtime_pm_forbid(struct device *dev)
  1069. {
  1070. pm_runtime_forbid(dev);
  1071. pm_runtime_get_noresume(dev);
  1072. }
  1073. static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
  1074. const struct pci_device_id *ent)
  1075. {
  1076. struct sdhci_pci_chip *chip;
  1077. struct sdhci_pci_slot *slot;
  1078. u8 slots, first_bar;
  1079. int ret, i;
  1080. BUG_ON(pdev == NULL);
  1081. BUG_ON(ent == NULL);
  1082. dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
  1083. (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
  1084. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
  1085. if (ret)
  1086. return ret;
  1087. slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
  1088. dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
  1089. if (slots == 0)
  1090. return -ENODEV;
  1091. BUG_ON(slots > MAX_SLOTS);
  1092. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
  1093. if (ret)
  1094. return ret;
  1095. first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
  1096. if (first_bar > 5) {
  1097. dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
  1098. return -ENODEV;
  1099. }
  1100. ret = pci_enable_device(pdev);
  1101. if (ret)
  1102. return ret;
  1103. chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
  1104. if (!chip) {
  1105. ret = -ENOMEM;
  1106. goto err;
  1107. }
  1108. chip->pdev = pdev;
  1109. chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
  1110. if (chip->fixes) {
  1111. chip->quirks = chip->fixes->quirks;
  1112. chip->allow_runtime_pm = chip->fixes->allow_runtime_pm;
  1113. }
  1114. chip->num_slots = slots;
  1115. pci_set_drvdata(pdev, chip);
  1116. if (chip->fixes && chip->fixes->probe) {
  1117. ret = chip->fixes->probe(chip);
  1118. if (ret)
  1119. goto free;
  1120. }
  1121. slots = chip->num_slots; /* Quirk may have changed this */
  1122. for (i = 0; i < slots; i++) {
  1123. slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
  1124. if (IS_ERR(slot)) {
  1125. for (i--; i >= 0; i--)
  1126. sdhci_pci_remove_slot(chip->slots[i]);
  1127. ret = PTR_ERR(slot);
  1128. goto free;
  1129. }
  1130. chip->slots[i] = slot;
  1131. }
  1132. if (chip->allow_runtime_pm)
  1133. sdhci_pci_runtime_pm_allow(&pdev->dev);
  1134. return 0;
  1135. free:
  1136. pci_set_drvdata(pdev, NULL);
  1137. kfree(chip);
  1138. err:
  1139. pci_disable_device(pdev);
  1140. return ret;
  1141. }
  1142. static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
  1143. {
  1144. int i;
  1145. struct sdhci_pci_chip *chip;
  1146. chip = pci_get_drvdata(pdev);
  1147. if (chip) {
  1148. if (chip->allow_runtime_pm)
  1149. sdhci_pci_runtime_pm_forbid(&pdev->dev);
  1150. for (i = 0; i < chip->num_slots; i++)
  1151. sdhci_pci_remove_slot(chip->slots[i]);
  1152. pci_set_drvdata(pdev, NULL);
  1153. kfree(chip);
  1154. }
  1155. pci_disable_device(pdev);
  1156. }
  1157. static struct pci_driver sdhci_driver = {
  1158. .name = "sdhci-pci",
  1159. .id_table = pci_ids,
  1160. .probe = sdhci_pci_probe,
  1161. .remove = __devexit_p(sdhci_pci_remove),
  1162. .suspend = sdhci_pci_suspend,
  1163. .resume = sdhci_pci_resume,
  1164. .driver = {
  1165. .pm = &sdhci_pci_pm_ops
  1166. },
  1167. };
  1168. /*****************************************************************************\
  1169. * *
  1170. * Driver init/exit *
  1171. * *
  1172. \*****************************************************************************/
  1173. static int __init sdhci_drv_init(void)
  1174. {
  1175. return pci_register_driver(&sdhci_driver);
  1176. }
  1177. static void __exit sdhci_drv_exit(void)
  1178. {
  1179. pci_unregister_driver(&sdhci_driver);
  1180. }
  1181. module_init(sdhci_drv_init);
  1182. module_exit(sdhci_drv_exit);
  1183. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  1184. MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
  1185. MODULE_LICENSE("GPL");