sdhci-pci.c 34 KB

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