sdhci-pci.c 34 KB

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