sdhci-pci.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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/mmc/host.h>
  20. #include <asm/scatterlist.h>
  21. #include <asm/io.h>
  22. #include "sdhci.h"
  23. /*
  24. * PCI registers
  25. */
  26. #define PCI_SDHCI_IFPIO 0x00
  27. #define PCI_SDHCI_IFDMA 0x01
  28. #define PCI_SDHCI_IFVENDOR 0x02
  29. #define PCI_SLOT_INFO 0x40 /* 8 bits */
  30. #define PCI_SLOT_INFO_SLOTS(x) ((x >> 4) & 7)
  31. #define PCI_SLOT_INFO_FIRST_BAR_MASK 0x07
  32. #define MAX_SLOTS 8
  33. struct sdhci_pci_chip;
  34. struct sdhci_pci_slot;
  35. struct sdhci_pci_fixes {
  36. unsigned int quirks;
  37. int (*probe)(struct sdhci_pci_chip*);
  38. int (*probe_slot)(struct sdhci_pci_slot*);
  39. void (*remove_slot)(struct sdhci_pci_slot*, int);
  40. int (*suspend)(struct sdhci_pci_chip*,
  41. pm_message_t);
  42. int (*resume)(struct sdhci_pci_chip*);
  43. };
  44. struct sdhci_pci_slot {
  45. struct sdhci_pci_chip *chip;
  46. struct sdhci_host *host;
  47. int pci_bar;
  48. };
  49. struct sdhci_pci_chip {
  50. struct pci_dev *pdev;
  51. unsigned int quirks;
  52. const struct sdhci_pci_fixes *fixes;
  53. int num_slots; /* Slots on controller */
  54. struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
  55. };
  56. /*****************************************************************************\
  57. * *
  58. * Hardware specific quirk handling *
  59. * *
  60. \*****************************************************************************/
  61. static int ricoh_probe(struct sdhci_pci_chip *chip)
  62. {
  63. if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
  64. chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
  65. chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
  66. return 0;
  67. }
  68. static const struct sdhci_pci_fixes sdhci_ricoh = {
  69. .probe = ricoh_probe,
  70. .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
  71. SDHCI_QUIRK_FORCE_DMA |
  72. SDHCI_QUIRK_CLOCK_BEFORE_RESET,
  73. };
  74. static const struct sdhci_pci_fixes sdhci_ene_712 = {
  75. .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
  76. SDHCI_QUIRK_BROKEN_DMA,
  77. };
  78. static const struct sdhci_pci_fixes sdhci_ene_714 = {
  79. .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
  80. SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
  81. SDHCI_QUIRK_BROKEN_DMA,
  82. };
  83. static const struct sdhci_pci_fixes sdhci_cafe = {
  84. .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
  85. SDHCI_QUIRK_NO_BUSY_IRQ |
  86. SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
  87. };
  88. static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
  89. {
  90. u8 scratch;
  91. int ret;
  92. ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
  93. if (ret)
  94. return ret;
  95. /*
  96. * Turn PMOS on [bit 0], set over current detection to 2.4 V
  97. * [bit 1:2] and enable over current debouncing [bit 6].
  98. */
  99. if (on)
  100. scratch |= 0x47;
  101. else
  102. scratch &= ~0x47;
  103. ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
  104. if (ret)
  105. return ret;
  106. return 0;
  107. }
  108. static int jmicron_probe(struct sdhci_pci_chip *chip)
  109. {
  110. int ret;
  111. if (chip->pdev->revision == 0) {
  112. chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
  113. SDHCI_QUIRK_32BIT_DMA_SIZE |
  114. SDHCI_QUIRK_32BIT_ADMA_SIZE |
  115. SDHCI_QUIRK_RESET_AFTER_REQUEST |
  116. SDHCI_QUIRK_BROKEN_SMALL_PIO;
  117. }
  118. /*
  119. * JMicron chips can have two interfaces to the same hardware
  120. * in order to work around limitations in Microsoft's driver.
  121. * We need to make sure we only bind to one of them.
  122. *
  123. * This code assumes two things:
  124. *
  125. * 1. The PCI code adds subfunctions in order.
  126. *
  127. * 2. The MMC interface has a lower subfunction number
  128. * than the SD interface.
  129. */
  130. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD) {
  131. struct pci_dev *sd_dev;
  132. sd_dev = NULL;
  133. while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
  134. PCI_DEVICE_ID_JMICRON_JMB38X_MMC, sd_dev)) != NULL) {
  135. if ((PCI_SLOT(chip->pdev->devfn) ==
  136. PCI_SLOT(sd_dev->devfn)) &&
  137. (chip->pdev->bus == sd_dev->bus))
  138. break;
  139. }
  140. if (sd_dev) {
  141. pci_dev_put(sd_dev);
  142. dev_info(&chip->pdev->dev, "Refusing to bind to "
  143. "secondary interface.\n");
  144. return -ENODEV;
  145. }
  146. }
  147. /*
  148. * JMicron chips need a bit of a nudge to enable the power
  149. * output pins.
  150. */
  151. ret = jmicron_pmos(chip, 1);
  152. if (ret) {
  153. dev_err(&chip->pdev->dev, "Failure enabling card power\n");
  154. return ret;
  155. }
  156. return 0;
  157. }
  158. static void jmicron_enable_mmc(struct sdhci_host *host, int on)
  159. {
  160. u8 scratch;
  161. scratch = readb(host->ioaddr + 0xC0);
  162. if (on)
  163. scratch |= 0x01;
  164. else
  165. scratch &= ~0x01;
  166. writeb(scratch, host->ioaddr + 0xC0);
  167. }
  168. static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
  169. {
  170. if (slot->chip->pdev->revision == 0) {
  171. u16 version;
  172. version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
  173. version = (version & SDHCI_VENDOR_VER_MASK) >>
  174. SDHCI_VENDOR_VER_SHIFT;
  175. /*
  176. * Older versions of the chip have lots of nasty glitches
  177. * in the ADMA engine. It's best just to avoid it
  178. * completely.
  179. */
  180. if (version < 0xAC)
  181. slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
  182. }
  183. /*
  184. * The secondary interface requires a bit set to get the
  185. * interrupts.
  186. */
  187. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
  188. jmicron_enable_mmc(slot->host, 1);
  189. return 0;
  190. }
  191. static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
  192. {
  193. if (dead)
  194. return;
  195. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
  196. jmicron_enable_mmc(slot->host, 0);
  197. }
  198. static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state)
  199. {
  200. int i;
  201. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
  202. for (i = 0;i < chip->num_slots;i++)
  203. jmicron_enable_mmc(chip->slots[i]->host, 0);
  204. }
  205. return 0;
  206. }
  207. static int jmicron_resume(struct sdhci_pci_chip *chip)
  208. {
  209. int ret, i;
  210. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
  211. for (i = 0;i < chip->num_slots;i++)
  212. jmicron_enable_mmc(chip->slots[i]->host, 1);
  213. }
  214. ret = jmicron_pmos(chip, 1);
  215. if (ret) {
  216. dev_err(&chip->pdev->dev, "Failure enabling card power\n");
  217. return ret;
  218. }
  219. return 0;
  220. }
  221. static const struct sdhci_pci_fixes sdhci_jmicron = {
  222. .probe = jmicron_probe,
  223. .probe_slot = jmicron_probe_slot,
  224. .remove_slot = jmicron_remove_slot,
  225. .suspend = jmicron_suspend,
  226. .resume = jmicron_resume,
  227. };
  228. /* SysKonnect CardBus2SDIO extra registers */
  229. #define SYSKT_CTRL 0x200
  230. #define SYSKT_RDFIFO_STAT 0x204
  231. #define SYSKT_WRFIFO_STAT 0x208
  232. #define SYSKT_POWER_DATA 0x20c
  233. #define SYSKT_POWER_330 0xef
  234. #define SYSKT_POWER_300 0xf8
  235. #define SYSKT_POWER_184 0xcc
  236. #define SYSKT_POWER_CMD 0x20d
  237. #define SYSKT_POWER_START (1 << 7)
  238. #define SYSKT_POWER_STATUS 0x20e
  239. #define SYSKT_POWER_STATUS_OK (1 << 0)
  240. #define SYSKT_BOARD_REV 0x210
  241. #define SYSKT_CHIP_REV 0x211
  242. #define SYSKT_CONF_DATA 0x212
  243. #define SYSKT_CONF_DATA_1V8 (1 << 2)
  244. #define SYSKT_CONF_DATA_2V5 (1 << 1)
  245. #define SYSKT_CONF_DATA_3V3 (1 << 0)
  246. static int syskt_probe(struct sdhci_pci_chip *chip)
  247. {
  248. if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
  249. chip->pdev->class &= ~0x0000FF;
  250. chip->pdev->class |= PCI_SDHCI_IFDMA;
  251. }
  252. return 0;
  253. }
  254. static int syskt_probe_slot(struct sdhci_pci_slot *slot)
  255. {
  256. int tm, ps;
  257. u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
  258. u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
  259. dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
  260. "board rev %d.%d, chip rev %d.%d\n",
  261. board_rev >> 4, board_rev & 0xf,
  262. chip_rev >> 4, chip_rev & 0xf);
  263. if (chip_rev >= 0x20)
  264. slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
  265. writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
  266. writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
  267. udelay(50);
  268. tm = 10; /* Wait max 1 ms */
  269. do {
  270. ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
  271. if (ps & SYSKT_POWER_STATUS_OK)
  272. break;
  273. udelay(100);
  274. } while (--tm);
  275. if (!tm) {
  276. dev_err(&slot->chip->pdev->dev,
  277. "power regulator never stabilized");
  278. writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
  279. return -ENODEV;
  280. }
  281. return 0;
  282. }
  283. static const struct sdhci_pci_fixes sdhci_syskt = {
  284. .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
  285. .probe = syskt_probe,
  286. .probe_slot = syskt_probe_slot,
  287. };
  288. static int via_probe(struct sdhci_pci_chip *chip)
  289. {
  290. if (chip->pdev->revision == 0x10)
  291. chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
  292. return 0;
  293. }
  294. static const struct sdhci_pci_fixes sdhci_via = {
  295. .probe = via_probe,
  296. };
  297. static const struct pci_device_id pci_ids[] __devinitdata = {
  298. {
  299. .vendor = PCI_VENDOR_ID_RICOH,
  300. .device = PCI_DEVICE_ID_RICOH_R5C822,
  301. .subvendor = PCI_ANY_ID,
  302. .subdevice = PCI_ANY_ID,
  303. .driver_data = (kernel_ulong_t)&sdhci_ricoh,
  304. },
  305. {
  306. .vendor = PCI_VENDOR_ID_ENE,
  307. .device = PCI_DEVICE_ID_ENE_CB712_SD,
  308. .subvendor = PCI_ANY_ID,
  309. .subdevice = PCI_ANY_ID,
  310. .driver_data = (kernel_ulong_t)&sdhci_ene_712,
  311. },
  312. {
  313. .vendor = PCI_VENDOR_ID_ENE,
  314. .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
  315. .subvendor = PCI_ANY_ID,
  316. .subdevice = PCI_ANY_ID,
  317. .driver_data = (kernel_ulong_t)&sdhci_ene_712,
  318. },
  319. {
  320. .vendor = PCI_VENDOR_ID_ENE,
  321. .device = PCI_DEVICE_ID_ENE_CB714_SD,
  322. .subvendor = PCI_ANY_ID,
  323. .subdevice = PCI_ANY_ID,
  324. .driver_data = (kernel_ulong_t)&sdhci_ene_714,
  325. },
  326. {
  327. .vendor = PCI_VENDOR_ID_ENE,
  328. .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
  329. .subvendor = PCI_ANY_ID,
  330. .subdevice = PCI_ANY_ID,
  331. .driver_data = (kernel_ulong_t)&sdhci_ene_714,
  332. },
  333. {
  334. .vendor = PCI_VENDOR_ID_MARVELL,
  335. .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
  336. .subvendor = PCI_ANY_ID,
  337. .subdevice = PCI_ANY_ID,
  338. .driver_data = (kernel_ulong_t)&sdhci_cafe,
  339. },
  340. {
  341. .vendor = PCI_VENDOR_ID_JMICRON,
  342. .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
  343. .subvendor = PCI_ANY_ID,
  344. .subdevice = PCI_ANY_ID,
  345. .driver_data = (kernel_ulong_t)&sdhci_jmicron,
  346. },
  347. {
  348. .vendor = PCI_VENDOR_ID_JMICRON,
  349. .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
  350. .subvendor = PCI_ANY_ID,
  351. .subdevice = PCI_ANY_ID,
  352. .driver_data = (kernel_ulong_t)&sdhci_jmicron,
  353. },
  354. {
  355. .vendor = PCI_VENDOR_ID_SYSKONNECT,
  356. .device = 0x8000,
  357. .subvendor = PCI_ANY_ID,
  358. .subdevice = PCI_ANY_ID,
  359. .driver_data = (kernel_ulong_t)&sdhci_syskt,
  360. },
  361. {
  362. .vendor = PCI_VENDOR_ID_VIA,
  363. .device = 0x95d0,
  364. .subvendor = PCI_ANY_ID,
  365. .subdevice = PCI_ANY_ID,
  366. .driver_data = (kernel_ulong_t)&sdhci_via,
  367. },
  368. { /* Generic SD host controller */
  369. PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
  370. },
  371. { /* end: all zeroes */ },
  372. };
  373. MODULE_DEVICE_TABLE(pci, pci_ids);
  374. /*****************************************************************************\
  375. * *
  376. * SDHCI core callbacks *
  377. * *
  378. \*****************************************************************************/
  379. static int sdhci_pci_enable_dma(struct sdhci_host *host)
  380. {
  381. struct sdhci_pci_slot *slot;
  382. struct pci_dev *pdev;
  383. int ret;
  384. slot = sdhci_priv(host);
  385. pdev = slot->chip->pdev;
  386. if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
  387. ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
  388. (host->flags & SDHCI_USE_SDMA)) {
  389. dev_warn(&pdev->dev, "Will use DMA mode even though HW "
  390. "doesn't fully claim to support it.\n");
  391. }
  392. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  393. if (ret)
  394. return ret;
  395. pci_set_master(pdev);
  396. return 0;
  397. }
  398. static struct sdhci_ops sdhci_pci_ops = {
  399. .enable_dma = sdhci_pci_enable_dma,
  400. };
  401. /*****************************************************************************\
  402. * *
  403. * Suspend/resume *
  404. * *
  405. \*****************************************************************************/
  406. #ifdef CONFIG_PM
  407. static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state)
  408. {
  409. struct sdhci_pci_chip *chip;
  410. struct sdhci_pci_slot *slot;
  411. mmc_pm_flag_t pm_flags = 0;
  412. int i, ret;
  413. chip = pci_get_drvdata(pdev);
  414. if (!chip)
  415. return 0;
  416. for (i = 0;i < chip->num_slots;i++) {
  417. slot = chip->slots[i];
  418. if (!slot)
  419. continue;
  420. ret = sdhci_suspend_host(slot->host, state);
  421. if (ret) {
  422. for (i--;i >= 0;i--)
  423. sdhci_resume_host(chip->slots[i]->host);
  424. return ret;
  425. }
  426. pm_flags |= slot->host->mmc->pm_flags;
  427. }
  428. if (chip->fixes && chip->fixes->suspend) {
  429. ret = chip->fixes->suspend(chip, state);
  430. if (ret) {
  431. for (i = chip->num_slots - 1;i >= 0;i--)
  432. sdhci_resume_host(chip->slots[i]->host);
  433. return ret;
  434. }
  435. }
  436. pci_save_state(pdev);
  437. if (pm_flags & MMC_PM_KEEP_POWER) {
  438. if (pm_flags & MMC_PM_WAKE_SDIO_IRQ)
  439. pci_enable_wake(pdev, PCI_D3hot, 1);
  440. pci_set_power_state(pdev, PCI_D3hot);
  441. } else {
  442. pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
  443. pci_disable_device(pdev);
  444. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  445. }
  446. return 0;
  447. }
  448. static int sdhci_pci_resume (struct pci_dev *pdev)
  449. {
  450. struct sdhci_pci_chip *chip;
  451. struct sdhci_pci_slot *slot;
  452. int i, ret;
  453. chip = pci_get_drvdata(pdev);
  454. if (!chip)
  455. return 0;
  456. pci_set_power_state(pdev, PCI_D0);
  457. pci_restore_state(pdev);
  458. ret = pci_enable_device(pdev);
  459. if (ret)
  460. return ret;
  461. if (chip->fixes && chip->fixes->resume) {
  462. ret = chip->fixes->resume(chip);
  463. if (ret)
  464. return ret;
  465. }
  466. for (i = 0;i < chip->num_slots;i++) {
  467. slot = chip->slots[i];
  468. if (!slot)
  469. continue;
  470. ret = sdhci_resume_host(slot->host);
  471. if (ret)
  472. return ret;
  473. }
  474. return 0;
  475. }
  476. #else /* CONFIG_PM */
  477. #define sdhci_pci_suspend NULL
  478. #define sdhci_pci_resume NULL
  479. #endif /* CONFIG_PM */
  480. /*****************************************************************************\
  481. * *
  482. * Device probing/removal *
  483. * *
  484. \*****************************************************************************/
  485. static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
  486. struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
  487. {
  488. struct sdhci_pci_slot *slot;
  489. struct sdhci_host *host;
  490. resource_size_t addr;
  491. int ret;
  492. if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
  493. dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
  494. return ERR_PTR(-ENODEV);
  495. }
  496. if (pci_resource_len(pdev, bar) != 0x100) {
  497. dev_err(&pdev->dev, "Invalid iomem size. You may "
  498. "experience problems.\n");
  499. }
  500. if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
  501. dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
  502. return ERR_PTR(-ENODEV);
  503. }
  504. if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
  505. dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
  506. return ERR_PTR(-ENODEV);
  507. }
  508. host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
  509. if (IS_ERR(host)) {
  510. dev_err(&pdev->dev, "cannot allocate host\n");
  511. return ERR_CAST(host);
  512. }
  513. slot = sdhci_priv(host);
  514. slot->chip = chip;
  515. slot->host = host;
  516. slot->pci_bar = bar;
  517. host->hw_name = "PCI";
  518. host->ops = &sdhci_pci_ops;
  519. host->quirks = chip->quirks;
  520. host->irq = pdev->irq;
  521. ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
  522. if (ret) {
  523. dev_err(&pdev->dev, "cannot request region\n");
  524. goto free;
  525. }
  526. addr = pci_resource_start(pdev, bar);
  527. host->ioaddr = pci_ioremap_bar(pdev, bar);
  528. if (!host->ioaddr) {
  529. dev_err(&pdev->dev, "failed to remap registers\n");
  530. goto release;
  531. }
  532. if (chip->fixes && chip->fixes->probe_slot) {
  533. ret = chip->fixes->probe_slot(slot);
  534. if (ret)
  535. goto unmap;
  536. }
  537. host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
  538. ret = sdhci_add_host(host);
  539. if (ret)
  540. goto remove;
  541. return slot;
  542. remove:
  543. if (chip->fixes && chip->fixes->remove_slot)
  544. chip->fixes->remove_slot(slot, 0);
  545. unmap:
  546. iounmap(host->ioaddr);
  547. release:
  548. pci_release_region(pdev, bar);
  549. free:
  550. sdhci_free_host(host);
  551. return ERR_PTR(ret);
  552. }
  553. static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
  554. {
  555. int dead;
  556. u32 scratch;
  557. dead = 0;
  558. scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
  559. if (scratch == (u32)-1)
  560. dead = 1;
  561. sdhci_remove_host(slot->host, dead);
  562. if (slot->chip->fixes && slot->chip->fixes->remove_slot)
  563. slot->chip->fixes->remove_slot(slot, dead);
  564. pci_release_region(slot->chip->pdev, slot->pci_bar);
  565. sdhci_free_host(slot->host);
  566. }
  567. static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
  568. const struct pci_device_id *ent)
  569. {
  570. struct sdhci_pci_chip *chip;
  571. struct sdhci_pci_slot *slot;
  572. u8 slots, rev, first_bar;
  573. int ret, i;
  574. BUG_ON(pdev == NULL);
  575. BUG_ON(ent == NULL);
  576. pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
  577. dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
  578. (int)pdev->vendor, (int)pdev->device, (int)rev);
  579. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
  580. if (ret)
  581. return ret;
  582. slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
  583. dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
  584. if (slots == 0)
  585. return -ENODEV;
  586. BUG_ON(slots > MAX_SLOTS);
  587. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
  588. if (ret)
  589. return ret;
  590. first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
  591. if (first_bar > 5) {
  592. dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
  593. return -ENODEV;
  594. }
  595. ret = pci_enable_device(pdev);
  596. if (ret)
  597. return ret;
  598. chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
  599. if (!chip) {
  600. ret = -ENOMEM;
  601. goto err;
  602. }
  603. chip->pdev = pdev;
  604. chip->fixes = (const struct sdhci_pci_fixes*)ent->driver_data;
  605. if (chip->fixes)
  606. chip->quirks = chip->fixes->quirks;
  607. chip->num_slots = slots;
  608. pci_set_drvdata(pdev, chip);
  609. if (chip->fixes && chip->fixes->probe) {
  610. ret = chip->fixes->probe(chip);
  611. if (ret)
  612. goto free;
  613. }
  614. for (i = 0;i < slots;i++) {
  615. slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
  616. if (IS_ERR(slot)) {
  617. for (i--;i >= 0;i--)
  618. sdhci_pci_remove_slot(chip->slots[i]);
  619. ret = PTR_ERR(slot);
  620. goto free;
  621. }
  622. chip->slots[i] = slot;
  623. }
  624. return 0;
  625. free:
  626. pci_set_drvdata(pdev, NULL);
  627. kfree(chip);
  628. err:
  629. pci_disable_device(pdev);
  630. return ret;
  631. }
  632. static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
  633. {
  634. int i;
  635. struct sdhci_pci_chip *chip;
  636. chip = pci_get_drvdata(pdev);
  637. if (chip) {
  638. for (i = 0;i < chip->num_slots; i++)
  639. sdhci_pci_remove_slot(chip->slots[i]);
  640. pci_set_drvdata(pdev, NULL);
  641. kfree(chip);
  642. }
  643. pci_disable_device(pdev);
  644. }
  645. static struct pci_driver sdhci_driver = {
  646. .name = "sdhci-pci",
  647. .id_table = pci_ids,
  648. .probe = sdhci_pci_probe,
  649. .remove = __devexit_p(sdhci_pci_remove),
  650. .suspend = sdhci_pci_suspend,
  651. .resume = sdhci_pci_resume,
  652. };
  653. /*****************************************************************************\
  654. * *
  655. * Driver init/exit *
  656. * *
  657. \*****************************************************************************/
  658. static int __init sdhci_drv_init(void)
  659. {
  660. return pci_register_driver(&sdhci_driver);
  661. }
  662. static void __exit sdhci_drv_exit(void)
  663. {
  664. pci_unregister_driver(&sdhci_driver);
  665. }
  666. module_init(sdhci_drv_init);
  667. module_exit(sdhci_drv_exit);
  668. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  669. MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
  670. MODULE_LICENSE("GPL");