sdhci-pci.c 35 KB

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