sdhci-pci.c 35 KB

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