sdhci-pci.c 39 KB

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