sdhci-pci-core.c 46 KB

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