sdhci-pci.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  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_HISPD |
  95. SDHCI_CAN_DO_SDMA;
  96. return 0;
  97. }
  98. static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
  99. {
  100. /* Apply a delay to allow controller to settle */
  101. /* Otherwise it becomes confused if card state changed
  102. during suspend */
  103. msleep(500);
  104. return 0;
  105. }
  106. static const struct sdhci_pci_fixes sdhci_ricoh = {
  107. .probe = ricoh_probe,
  108. .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
  109. SDHCI_QUIRK_FORCE_DMA |
  110. SDHCI_QUIRK_CLOCK_BEFORE_RESET,
  111. };
  112. static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
  113. .probe_slot = ricoh_mmc_probe_slot,
  114. .resume = ricoh_mmc_resume,
  115. .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
  116. SDHCI_QUIRK_CLOCK_BEFORE_RESET |
  117. SDHCI_QUIRK_NO_CARD_NO_RESET |
  118. SDHCI_QUIRK_MISSING_CAPS
  119. };
  120. static const struct sdhci_pci_fixes sdhci_ene_712 = {
  121. .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
  122. SDHCI_QUIRK_BROKEN_DMA,
  123. };
  124. static const struct sdhci_pci_fixes sdhci_ene_714 = {
  125. .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
  126. SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
  127. SDHCI_QUIRK_BROKEN_DMA,
  128. };
  129. static const struct sdhci_pci_fixes sdhci_cafe = {
  130. .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
  131. SDHCI_QUIRK_NO_BUSY_IRQ |
  132. SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  133. SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
  134. };
  135. static int mrst_hc_probe_slot(struct sdhci_pci_slot *slot)
  136. {
  137. slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  138. return 0;
  139. }
  140. /*
  141. * ADMA operation is disabled for Moorestown platform due to
  142. * hardware bugs.
  143. */
  144. static int mrst_hc_probe(struct sdhci_pci_chip *chip)
  145. {
  146. /*
  147. * slots number is fixed here for MRST as SDIO3/5 are never used and
  148. * have hardware bugs.
  149. */
  150. chip->num_slots = 1;
  151. return 0;
  152. }
  153. static int pch_hc_probe_slot(struct sdhci_pci_slot *slot)
  154. {
  155. slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  156. return 0;
  157. }
  158. #ifdef CONFIG_PM_RUNTIME
  159. static irqreturn_t sdhci_pci_sd_cd(int irq, void *dev_id)
  160. {
  161. struct sdhci_pci_slot *slot = dev_id;
  162. struct sdhci_host *host = slot->host;
  163. mmc_detect_change(host->mmc, msecs_to_jiffies(200));
  164. return IRQ_HANDLED;
  165. }
  166. static void sdhci_pci_add_own_cd(struct sdhci_pci_slot *slot)
  167. {
  168. int err, irq, gpio = slot->cd_gpio;
  169. slot->cd_gpio = -EINVAL;
  170. slot->cd_irq = -EINVAL;
  171. if (!gpio_is_valid(gpio))
  172. return;
  173. err = gpio_request(gpio, "sd_cd");
  174. if (err < 0)
  175. goto out;
  176. err = gpio_direction_input(gpio);
  177. if (err < 0)
  178. goto out_free;
  179. irq = gpio_to_irq(gpio);
  180. if (irq < 0)
  181. goto out_free;
  182. err = request_irq(irq, sdhci_pci_sd_cd, IRQF_TRIGGER_RISING |
  183. IRQF_TRIGGER_FALLING, "sd_cd", slot);
  184. if (err)
  185. goto out_free;
  186. slot->cd_gpio = gpio;
  187. slot->cd_irq = irq;
  188. return;
  189. out_free:
  190. gpio_free(gpio);
  191. out:
  192. dev_warn(&slot->chip->pdev->dev, "failed to setup card detect wake up\n");
  193. }
  194. static void sdhci_pci_remove_own_cd(struct sdhci_pci_slot *slot)
  195. {
  196. if (slot->cd_irq >= 0)
  197. free_irq(slot->cd_irq, slot);
  198. if (gpio_is_valid(slot->cd_gpio))
  199. gpio_free(slot->cd_gpio);
  200. }
  201. #else
  202. static inline void sdhci_pci_add_own_cd(struct sdhci_pci_slot *slot)
  203. {
  204. }
  205. static inline void sdhci_pci_remove_own_cd(struct sdhci_pci_slot *slot)
  206. {
  207. }
  208. #endif
  209. static int mfd_emmc_probe_slot(struct sdhci_pci_slot *slot)
  210. {
  211. slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE;
  212. slot->host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC |
  213. MMC_CAP2_HC_ERASE_SZ;
  214. return 0;
  215. }
  216. static int mfd_sdio_probe_slot(struct sdhci_pci_slot *slot)
  217. {
  218. slot->host->mmc->caps |= MMC_CAP_POWER_OFF_CARD | MMC_CAP_NONREMOVABLE;
  219. return 0;
  220. }
  221. static const struct sdhci_pci_fixes sdhci_intel_mrst_hc0 = {
  222. .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
  223. .probe_slot = mrst_hc_probe_slot,
  224. };
  225. static const struct sdhci_pci_fixes sdhci_intel_mrst_hc1_hc2 = {
  226. .quirks = SDHCI_QUIRK_BROKEN_ADMA | SDHCI_QUIRK_NO_HISPD_BIT,
  227. .probe = mrst_hc_probe,
  228. };
  229. static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
  230. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  231. .allow_runtime_pm = true,
  232. };
  233. static const struct sdhci_pci_fixes sdhci_intel_mfd_sdio = {
  234. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  235. .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
  236. .allow_runtime_pm = true,
  237. .probe_slot = mfd_sdio_probe_slot,
  238. };
  239. static const struct sdhci_pci_fixes sdhci_intel_mfd_emmc = {
  240. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  241. .allow_runtime_pm = true,
  242. .probe_slot = mfd_emmc_probe_slot,
  243. };
  244. static const struct sdhci_pci_fixes sdhci_intel_pch_sdio = {
  245. .quirks = SDHCI_QUIRK_BROKEN_ADMA,
  246. .probe_slot = pch_hc_probe_slot,
  247. };
  248. /* O2Micro extra registers */
  249. #define O2_SD_LOCK_WP 0xD3
  250. #define O2_SD_MULTI_VCC3V 0xEE
  251. #define O2_SD_CLKREQ 0xEC
  252. #define O2_SD_CAPS 0xE0
  253. #define O2_SD_ADMA1 0xE2
  254. #define O2_SD_ADMA2 0xE7
  255. #define O2_SD_INF_MOD 0xF1
  256. static int o2_probe(struct sdhci_pci_chip *chip)
  257. {
  258. int ret;
  259. u8 scratch;
  260. switch (chip->pdev->device) {
  261. case PCI_DEVICE_ID_O2_8220:
  262. case PCI_DEVICE_ID_O2_8221:
  263. case PCI_DEVICE_ID_O2_8320:
  264. case PCI_DEVICE_ID_O2_8321:
  265. /* This extra setup is required due to broken ADMA. */
  266. ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
  267. if (ret)
  268. return ret;
  269. scratch &= 0x7f;
  270. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  271. /* Set Multi 3 to VCC3V# */
  272. pci_write_config_byte(chip->pdev, O2_SD_MULTI_VCC3V, 0x08);
  273. /* Disable CLK_REQ# support after media DET */
  274. ret = pci_read_config_byte(chip->pdev, O2_SD_CLKREQ, &scratch);
  275. if (ret)
  276. return ret;
  277. scratch |= 0x20;
  278. pci_write_config_byte(chip->pdev, O2_SD_CLKREQ, scratch);
  279. /* Choose capabilities, enable SDMA. We have to write 0x01
  280. * to the capabilities register first to unlock it.
  281. */
  282. ret = pci_read_config_byte(chip->pdev, O2_SD_CAPS, &scratch);
  283. if (ret)
  284. return ret;
  285. scratch |= 0x01;
  286. pci_write_config_byte(chip->pdev, O2_SD_CAPS, scratch);
  287. pci_write_config_byte(chip->pdev, O2_SD_CAPS, 0x73);
  288. /* Disable ADMA1/2 */
  289. pci_write_config_byte(chip->pdev, O2_SD_ADMA1, 0x39);
  290. pci_write_config_byte(chip->pdev, O2_SD_ADMA2, 0x08);
  291. /* Disable the infinite transfer mode */
  292. ret = pci_read_config_byte(chip->pdev, O2_SD_INF_MOD, &scratch);
  293. if (ret)
  294. return ret;
  295. scratch |= 0x08;
  296. pci_write_config_byte(chip->pdev, O2_SD_INF_MOD, scratch);
  297. /* Lock WP */
  298. ret = pci_read_config_byte(chip->pdev, O2_SD_LOCK_WP, &scratch);
  299. if (ret)
  300. return ret;
  301. scratch |= 0x80;
  302. pci_write_config_byte(chip->pdev, O2_SD_LOCK_WP, scratch);
  303. }
  304. return 0;
  305. }
  306. static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
  307. {
  308. u8 scratch;
  309. int ret;
  310. ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
  311. if (ret)
  312. return ret;
  313. /*
  314. * Turn PMOS on [bit 0], set over current detection to 2.4 V
  315. * [bit 1:2] and enable over current debouncing [bit 6].
  316. */
  317. if (on)
  318. scratch |= 0x47;
  319. else
  320. scratch &= ~0x47;
  321. ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
  322. if (ret)
  323. return ret;
  324. return 0;
  325. }
  326. static int jmicron_probe(struct sdhci_pci_chip *chip)
  327. {
  328. int ret;
  329. u16 mmcdev = 0;
  330. if (chip->pdev->revision == 0) {
  331. chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
  332. SDHCI_QUIRK_32BIT_DMA_SIZE |
  333. SDHCI_QUIRK_32BIT_ADMA_SIZE |
  334. SDHCI_QUIRK_RESET_AFTER_REQUEST |
  335. SDHCI_QUIRK_BROKEN_SMALL_PIO;
  336. }
  337. /*
  338. * JMicron chips can have two interfaces to the same hardware
  339. * in order to work around limitations in Microsoft's driver.
  340. * We need to make sure we only bind to one of them.
  341. *
  342. * This code assumes two things:
  343. *
  344. * 1. The PCI code adds subfunctions in order.
  345. *
  346. * 2. The MMC interface has a lower subfunction number
  347. * than the SD interface.
  348. */
  349. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD)
  350. mmcdev = PCI_DEVICE_ID_JMICRON_JMB38X_MMC;
  351. else if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD)
  352. mmcdev = PCI_DEVICE_ID_JMICRON_JMB388_ESD;
  353. if (mmcdev) {
  354. struct pci_dev *sd_dev;
  355. sd_dev = NULL;
  356. while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
  357. mmcdev, sd_dev)) != NULL) {
  358. if ((PCI_SLOT(chip->pdev->devfn) ==
  359. PCI_SLOT(sd_dev->devfn)) &&
  360. (chip->pdev->bus == sd_dev->bus))
  361. break;
  362. }
  363. if (sd_dev) {
  364. pci_dev_put(sd_dev);
  365. dev_info(&chip->pdev->dev, "Refusing to bind to "
  366. "secondary interface.\n");
  367. return -ENODEV;
  368. }
  369. }
  370. /*
  371. * JMicron chips need a bit of a nudge to enable the power
  372. * output pins.
  373. */
  374. ret = jmicron_pmos(chip, 1);
  375. if (ret) {
  376. dev_err(&chip->pdev->dev, "Failure enabling card power\n");
  377. return ret;
  378. }
  379. /* quirk for unsable RO-detection on JM388 chips */
  380. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
  381. chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
  382. chip->quirks |= SDHCI_QUIRK_UNSTABLE_RO_DETECT;
  383. return 0;
  384. }
  385. static void jmicron_enable_mmc(struct sdhci_host *host, int on)
  386. {
  387. u8 scratch;
  388. scratch = readb(host->ioaddr + 0xC0);
  389. if (on)
  390. scratch |= 0x01;
  391. else
  392. scratch &= ~0x01;
  393. writeb(scratch, host->ioaddr + 0xC0);
  394. }
  395. static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
  396. {
  397. if (slot->chip->pdev->revision == 0) {
  398. u16 version;
  399. version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
  400. version = (version & SDHCI_VENDOR_VER_MASK) >>
  401. SDHCI_VENDOR_VER_SHIFT;
  402. /*
  403. * Older versions of the chip have lots of nasty glitches
  404. * in the ADMA engine. It's best just to avoid it
  405. * completely.
  406. */
  407. if (version < 0xAC)
  408. slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
  409. }
  410. /* JM388 MMC doesn't support 1.8V while SD supports it */
  411. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
  412. slot->host->ocr_avail_sd = MMC_VDD_32_33 | MMC_VDD_33_34 |
  413. MMC_VDD_29_30 | MMC_VDD_30_31 |
  414. MMC_VDD_165_195; /* allow 1.8V */
  415. slot->host->ocr_avail_mmc = MMC_VDD_32_33 | MMC_VDD_33_34 |
  416. MMC_VDD_29_30 | MMC_VDD_30_31; /* no 1.8V for MMC */
  417. }
  418. /*
  419. * The secondary interface requires a bit set to get the
  420. * interrupts.
  421. */
  422. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  423. slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
  424. jmicron_enable_mmc(slot->host, 1);
  425. slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
  426. return 0;
  427. }
  428. static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
  429. {
  430. if (dead)
  431. return;
  432. if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  433. slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
  434. jmicron_enable_mmc(slot->host, 0);
  435. }
  436. static int jmicron_suspend(struct sdhci_pci_chip *chip)
  437. {
  438. int i;
  439. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  440. chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
  441. for (i = 0; i < chip->num_slots; i++)
  442. jmicron_enable_mmc(chip->slots[i]->host, 0);
  443. }
  444. return 0;
  445. }
  446. static int jmicron_resume(struct sdhci_pci_chip *chip)
  447. {
  448. int ret, i;
  449. if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC ||
  450. chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD) {
  451. for (i = 0; i < chip->num_slots; i++)
  452. jmicron_enable_mmc(chip->slots[i]->host, 1);
  453. }
  454. ret = jmicron_pmos(chip, 1);
  455. if (ret) {
  456. dev_err(&chip->pdev->dev, "Failure enabling card power\n");
  457. return ret;
  458. }
  459. return 0;
  460. }
  461. static const struct sdhci_pci_fixes sdhci_o2 = {
  462. .probe = o2_probe,
  463. };
  464. static const struct sdhci_pci_fixes sdhci_jmicron = {
  465. .probe = jmicron_probe,
  466. .probe_slot = jmicron_probe_slot,
  467. .remove_slot = jmicron_remove_slot,
  468. .suspend = jmicron_suspend,
  469. .resume = jmicron_resume,
  470. };
  471. /* SysKonnect CardBus2SDIO extra registers */
  472. #define SYSKT_CTRL 0x200
  473. #define SYSKT_RDFIFO_STAT 0x204
  474. #define SYSKT_WRFIFO_STAT 0x208
  475. #define SYSKT_POWER_DATA 0x20c
  476. #define SYSKT_POWER_330 0xef
  477. #define SYSKT_POWER_300 0xf8
  478. #define SYSKT_POWER_184 0xcc
  479. #define SYSKT_POWER_CMD 0x20d
  480. #define SYSKT_POWER_START (1 << 7)
  481. #define SYSKT_POWER_STATUS 0x20e
  482. #define SYSKT_POWER_STATUS_OK (1 << 0)
  483. #define SYSKT_BOARD_REV 0x210
  484. #define SYSKT_CHIP_REV 0x211
  485. #define SYSKT_CONF_DATA 0x212
  486. #define SYSKT_CONF_DATA_1V8 (1 << 2)
  487. #define SYSKT_CONF_DATA_2V5 (1 << 1)
  488. #define SYSKT_CONF_DATA_3V3 (1 << 0)
  489. static int syskt_probe(struct sdhci_pci_chip *chip)
  490. {
  491. if ((chip->pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
  492. chip->pdev->class &= ~0x0000FF;
  493. chip->pdev->class |= PCI_SDHCI_IFDMA;
  494. }
  495. return 0;
  496. }
  497. static int syskt_probe_slot(struct sdhci_pci_slot *slot)
  498. {
  499. int tm, ps;
  500. u8 board_rev = readb(slot->host->ioaddr + SYSKT_BOARD_REV);
  501. u8 chip_rev = readb(slot->host->ioaddr + SYSKT_CHIP_REV);
  502. dev_info(&slot->chip->pdev->dev, "SysKonnect CardBus2SDIO, "
  503. "board rev %d.%d, chip rev %d.%d\n",
  504. board_rev >> 4, board_rev & 0xf,
  505. chip_rev >> 4, chip_rev & 0xf);
  506. if (chip_rev >= 0x20)
  507. slot->host->quirks |= SDHCI_QUIRK_FORCE_DMA;
  508. writeb(SYSKT_POWER_330, slot->host->ioaddr + SYSKT_POWER_DATA);
  509. writeb(SYSKT_POWER_START, slot->host->ioaddr + SYSKT_POWER_CMD);
  510. udelay(50);
  511. tm = 10; /* Wait max 1 ms */
  512. do {
  513. ps = readw(slot->host->ioaddr + SYSKT_POWER_STATUS);
  514. if (ps & SYSKT_POWER_STATUS_OK)
  515. break;
  516. udelay(100);
  517. } while (--tm);
  518. if (!tm) {
  519. dev_err(&slot->chip->pdev->dev,
  520. "power regulator never stabilized");
  521. writeb(0, slot->host->ioaddr + SYSKT_POWER_CMD);
  522. return -ENODEV;
  523. }
  524. return 0;
  525. }
  526. static const struct sdhci_pci_fixes sdhci_syskt = {
  527. .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER,
  528. .probe = syskt_probe,
  529. .probe_slot = syskt_probe_slot,
  530. };
  531. static int via_probe(struct sdhci_pci_chip *chip)
  532. {
  533. if (chip->pdev->revision == 0x10)
  534. chip->quirks |= SDHCI_QUIRK_DELAY_AFTER_POWER;
  535. return 0;
  536. }
  537. static const struct sdhci_pci_fixes sdhci_via = {
  538. .probe = via_probe,
  539. };
  540. static const struct pci_device_id pci_ids[] = {
  541. {
  542. .vendor = PCI_VENDOR_ID_RICOH,
  543. .device = PCI_DEVICE_ID_RICOH_R5C822,
  544. .subvendor = PCI_ANY_ID,
  545. .subdevice = PCI_ANY_ID,
  546. .driver_data = (kernel_ulong_t)&sdhci_ricoh,
  547. },
  548. {
  549. .vendor = PCI_VENDOR_ID_RICOH,
  550. .device = 0x843,
  551. .subvendor = PCI_ANY_ID,
  552. .subdevice = PCI_ANY_ID,
  553. .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
  554. },
  555. {
  556. .vendor = PCI_VENDOR_ID_RICOH,
  557. .device = 0xe822,
  558. .subvendor = PCI_ANY_ID,
  559. .subdevice = PCI_ANY_ID,
  560. .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
  561. },
  562. {
  563. .vendor = PCI_VENDOR_ID_RICOH,
  564. .device = 0xe823,
  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_ENE,
  571. .device = PCI_DEVICE_ID_ENE_CB712_SD,
  572. .subvendor = PCI_ANY_ID,
  573. .subdevice = PCI_ANY_ID,
  574. .driver_data = (kernel_ulong_t)&sdhci_ene_712,
  575. },
  576. {
  577. .vendor = PCI_VENDOR_ID_ENE,
  578. .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
  579. .subvendor = PCI_ANY_ID,
  580. .subdevice = PCI_ANY_ID,
  581. .driver_data = (kernel_ulong_t)&sdhci_ene_712,
  582. },
  583. {
  584. .vendor = PCI_VENDOR_ID_ENE,
  585. .device = PCI_DEVICE_ID_ENE_CB714_SD,
  586. .subvendor = PCI_ANY_ID,
  587. .subdevice = PCI_ANY_ID,
  588. .driver_data = (kernel_ulong_t)&sdhci_ene_714,
  589. },
  590. {
  591. .vendor = PCI_VENDOR_ID_ENE,
  592. .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
  593. .subvendor = PCI_ANY_ID,
  594. .subdevice = PCI_ANY_ID,
  595. .driver_data = (kernel_ulong_t)&sdhci_ene_714,
  596. },
  597. {
  598. .vendor = PCI_VENDOR_ID_MARVELL,
  599. .device = PCI_DEVICE_ID_MARVELL_88ALP01_SD,
  600. .subvendor = PCI_ANY_ID,
  601. .subdevice = PCI_ANY_ID,
  602. .driver_data = (kernel_ulong_t)&sdhci_cafe,
  603. },
  604. {
  605. .vendor = PCI_VENDOR_ID_JMICRON,
  606. .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
  607. .subvendor = PCI_ANY_ID,
  608. .subdevice = PCI_ANY_ID,
  609. .driver_data = (kernel_ulong_t)&sdhci_jmicron,
  610. },
  611. {
  612. .vendor = PCI_VENDOR_ID_JMICRON,
  613. .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
  614. .subvendor = PCI_ANY_ID,
  615. .subdevice = PCI_ANY_ID,
  616. .driver_data = (kernel_ulong_t)&sdhci_jmicron,
  617. },
  618. {
  619. .vendor = PCI_VENDOR_ID_JMICRON,
  620. .device = PCI_DEVICE_ID_JMICRON_JMB388_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_JMB388_ESD,
  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_SYSKONNECT,
  634. .device = 0x8000,
  635. .subvendor = PCI_ANY_ID,
  636. .subdevice = PCI_ANY_ID,
  637. .driver_data = (kernel_ulong_t)&sdhci_syskt,
  638. },
  639. {
  640. .vendor = PCI_VENDOR_ID_VIA,
  641. .device = 0x95d0,
  642. .subvendor = PCI_ANY_ID,
  643. .subdevice = PCI_ANY_ID,
  644. .driver_data = (kernel_ulong_t)&sdhci_via,
  645. },
  646. {
  647. .vendor = PCI_VENDOR_ID_INTEL,
  648. .device = PCI_DEVICE_ID_INTEL_MRST_SD0,
  649. .subvendor = PCI_ANY_ID,
  650. .subdevice = PCI_ANY_ID,
  651. .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc0,
  652. },
  653. {
  654. .vendor = PCI_VENDOR_ID_INTEL,
  655. .device = PCI_DEVICE_ID_INTEL_MRST_SD1,
  656. .subvendor = PCI_ANY_ID,
  657. .subdevice = PCI_ANY_ID,
  658. .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
  659. },
  660. {
  661. .vendor = PCI_VENDOR_ID_INTEL,
  662. .device = PCI_DEVICE_ID_INTEL_MRST_SD2,
  663. .subvendor = PCI_ANY_ID,
  664. .subdevice = PCI_ANY_ID,
  665. .driver_data = (kernel_ulong_t)&sdhci_intel_mrst_hc1_hc2,
  666. },
  667. {
  668. .vendor = PCI_VENDOR_ID_INTEL,
  669. .device = PCI_DEVICE_ID_INTEL_MFD_SD,
  670. .subvendor = PCI_ANY_ID,
  671. .subdevice = PCI_ANY_ID,
  672. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sd,
  673. },
  674. {
  675. .vendor = PCI_VENDOR_ID_INTEL,
  676. .device = PCI_DEVICE_ID_INTEL_MFD_SDIO1,
  677. .subvendor = PCI_ANY_ID,
  678. .subdevice = PCI_ANY_ID,
  679. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
  680. },
  681. {
  682. .vendor = PCI_VENDOR_ID_INTEL,
  683. .device = PCI_DEVICE_ID_INTEL_MFD_SDIO2,
  684. .subvendor = PCI_ANY_ID,
  685. .subdevice = PCI_ANY_ID,
  686. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_sdio,
  687. },
  688. {
  689. .vendor = PCI_VENDOR_ID_INTEL,
  690. .device = PCI_DEVICE_ID_INTEL_MFD_EMMC0,
  691. .subvendor = PCI_ANY_ID,
  692. .subdevice = PCI_ANY_ID,
  693. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
  694. },
  695. {
  696. .vendor = PCI_VENDOR_ID_INTEL,
  697. .device = PCI_DEVICE_ID_INTEL_MFD_EMMC1,
  698. .subvendor = PCI_ANY_ID,
  699. .subdevice = PCI_ANY_ID,
  700. .driver_data = (kernel_ulong_t)&sdhci_intel_mfd_emmc,
  701. },
  702. {
  703. .vendor = PCI_VENDOR_ID_INTEL,
  704. .device = PCI_DEVICE_ID_INTEL_PCH_SDIO0,
  705. .subvendor = PCI_ANY_ID,
  706. .subdevice = PCI_ANY_ID,
  707. .driver_data = (kernel_ulong_t)&sdhci_intel_pch_sdio,
  708. },
  709. {
  710. .vendor = PCI_VENDOR_ID_INTEL,
  711. .device = PCI_DEVICE_ID_INTEL_PCH_SDIO1,
  712. .subvendor = PCI_ANY_ID,
  713. .subdevice = PCI_ANY_ID,
  714. .driver_data = (kernel_ulong_t)&sdhci_intel_pch_sdio,
  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_bus_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_bus_width = sdhci_pci_bus_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 *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. slot->cd_gpio = -EINVAL;
  1012. /* Retrieve platform data if there is any */
  1013. if (*sdhci_pci_get_data)
  1014. slot->data = sdhci_pci_get_data(pdev, slotno);
  1015. if (slot->data) {
  1016. if (slot->data->setup) {
  1017. ret = slot->data->setup(slot->data);
  1018. if (ret) {
  1019. dev_err(&pdev->dev, "platform setup failed\n");
  1020. goto free;
  1021. }
  1022. }
  1023. slot->rst_n_gpio = slot->data->rst_n_gpio;
  1024. slot->cd_gpio = slot->data->cd_gpio;
  1025. }
  1026. host->hw_name = "PCI";
  1027. host->ops = &sdhci_pci_ops;
  1028. host->quirks = chip->quirks;
  1029. host->quirks2 = chip->quirks2;
  1030. host->irq = pdev->irq;
  1031. ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
  1032. if (ret) {
  1033. dev_err(&pdev->dev, "cannot request region\n");
  1034. goto cleanup;
  1035. }
  1036. host->ioaddr = pci_ioremap_bar(pdev, bar);
  1037. if (!host->ioaddr) {
  1038. dev_err(&pdev->dev, "failed to remap registers\n");
  1039. ret = -ENOMEM;
  1040. goto release;
  1041. }
  1042. if (chip->fixes && chip->fixes->probe_slot) {
  1043. ret = chip->fixes->probe_slot(slot);
  1044. if (ret)
  1045. goto unmap;
  1046. }
  1047. if (gpio_is_valid(slot->rst_n_gpio)) {
  1048. if (!gpio_request(slot->rst_n_gpio, "eMMC_reset")) {
  1049. gpio_direction_output(slot->rst_n_gpio, 1);
  1050. slot->host->mmc->caps |= MMC_CAP_HW_RESET;
  1051. } else {
  1052. dev_warn(&pdev->dev, "failed to request rst_n_gpio\n");
  1053. slot->rst_n_gpio = -EINVAL;
  1054. }
  1055. }
  1056. host->mmc->pm_caps = MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
  1057. ret = sdhci_add_host(host);
  1058. if (ret)
  1059. goto remove;
  1060. sdhci_pci_add_own_cd(slot);
  1061. return slot;
  1062. remove:
  1063. if (gpio_is_valid(slot->rst_n_gpio))
  1064. gpio_free(slot->rst_n_gpio);
  1065. if (chip->fixes && chip->fixes->remove_slot)
  1066. chip->fixes->remove_slot(slot, 0);
  1067. unmap:
  1068. iounmap(host->ioaddr);
  1069. release:
  1070. pci_release_region(pdev, bar);
  1071. cleanup:
  1072. if (slot->data && slot->data->cleanup)
  1073. slot->data->cleanup(slot->data);
  1074. free:
  1075. sdhci_free_host(host);
  1076. return ERR_PTR(ret);
  1077. }
  1078. static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
  1079. {
  1080. int dead;
  1081. u32 scratch;
  1082. sdhci_pci_remove_own_cd(slot);
  1083. dead = 0;
  1084. scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
  1085. if (scratch == (u32)-1)
  1086. dead = 1;
  1087. sdhci_remove_host(slot->host, dead);
  1088. if (gpio_is_valid(slot->rst_n_gpio))
  1089. gpio_free(slot->rst_n_gpio);
  1090. if (slot->chip->fixes && slot->chip->fixes->remove_slot)
  1091. slot->chip->fixes->remove_slot(slot, dead);
  1092. if (slot->data && slot->data->cleanup)
  1093. slot->data->cleanup(slot->data);
  1094. pci_release_region(slot->chip->pdev, slot->pci_bar);
  1095. sdhci_free_host(slot->host);
  1096. }
  1097. static void sdhci_pci_runtime_pm_allow(struct device *dev)
  1098. {
  1099. pm_runtime_put_noidle(dev);
  1100. pm_runtime_allow(dev);
  1101. pm_runtime_set_autosuspend_delay(dev, 50);
  1102. pm_runtime_use_autosuspend(dev);
  1103. pm_suspend_ignore_children(dev, 1);
  1104. }
  1105. static void sdhci_pci_runtime_pm_forbid(struct device *dev)
  1106. {
  1107. pm_runtime_forbid(dev);
  1108. pm_runtime_get_noresume(dev);
  1109. }
  1110. static int sdhci_pci_probe(struct pci_dev *pdev,
  1111. const struct pci_device_id *ent)
  1112. {
  1113. struct sdhci_pci_chip *chip;
  1114. struct sdhci_pci_slot *slot;
  1115. u8 slots, first_bar;
  1116. int ret, i;
  1117. BUG_ON(pdev == NULL);
  1118. BUG_ON(ent == NULL);
  1119. dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
  1120. (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
  1121. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
  1122. if (ret)
  1123. return ret;
  1124. slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
  1125. dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
  1126. if (slots == 0)
  1127. return -ENODEV;
  1128. BUG_ON(slots > MAX_SLOTS);
  1129. ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
  1130. if (ret)
  1131. return ret;
  1132. first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
  1133. if (first_bar > 5) {
  1134. dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
  1135. return -ENODEV;
  1136. }
  1137. ret = pci_enable_device(pdev);
  1138. if (ret)
  1139. return ret;
  1140. chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
  1141. if (!chip) {
  1142. ret = -ENOMEM;
  1143. goto err;
  1144. }
  1145. chip->pdev = pdev;
  1146. chip->fixes = (const struct sdhci_pci_fixes *)ent->driver_data;
  1147. if (chip->fixes) {
  1148. chip->quirks = chip->fixes->quirks;
  1149. chip->quirks2 = chip->fixes->quirks2;
  1150. chip->allow_runtime_pm = chip->fixes->allow_runtime_pm;
  1151. }
  1152. chip->num_slots = slots;
  1153. pci_set_drvdata(pdev, chip);
  1154. if (chip->fixes && chip->fixes->probe) {
  1155. ret = chip->fixes->probe(chip);
  1156. if (ret)
  1157. goto free;
  1158. }
  1159. slots = chip->num_slots; /* Quirk may have changed this */
  1160. for (i = 0; i < slots; i++) {
  1161. slot = sdhci_pci_probe_slot(pdev, chip, first_bar, i);
  1162. if (IS_ERR(slot)) {
  1163. for (i--; i >= 0; i--)
  1164. sdhci_pci_remove_slot(chip->slots[i]);
  1165. ret = PTR_ERR(slot);
  1166. goto free;
  1167. }
  1168. chip->slots[i] = slot;
  1169. }
  1170. if (chip->allow_runtime_pm)
  1171. sdhci_pci_runtime_pm_allow(&pdev->dev);
  1172. return 0;
  1173. free:
  1174. pci_set_drvdata(pdev, NULL);
  1175. kfree(chip);
  1176. err:
  1177. pci_disable_device(pdev);
  1178. return ret;
  1179. }
  1180. static void 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_set_drvdata(pdev, NULL);
  1191. kfree(chip);
  1192. }
  1193. pci_disable_device(pdev);
  1194. }
  1195. static struct pci_driver sdhci_driver = {
  1196. .name = "sdhci-pci",
  1197. .id_table = pci_ids,
  1198. .probe = sdhci_pci_probe,
  1199. .remove = sdhci_pci_remove,
  1200. .driver = {
  1201. .pm = &sdhci_pci_pm_ops
  1202. },
  1203. };
  1204. module_pci_driver(sdhci_driver);
  1205. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  1206. MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
  1207. MODULE_LICENSE("GPL");