sdhci-pci.c 29 KB

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