mmci.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927
  1. /*
  2. * linux/drivers/mmc/host/mmci.c - ARM PrimeCell MMCI PL180/1 driver
  3. *
  4. * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
  5. * Copyright (C) 2010 ST-Ericsson SA
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/init.h>
  14. #include <linux/ioport.h>
  15. #include <linux/device.h>
  16. #include <linux/io.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/kernel.h>
  19. #include <linux/slab.h>
  20. #include <linux/delay.h>
  21. #include <linux/err.h>
  22. #include <linux/highmem.h>
  23. #include <linux/log2.h>
  24. #include <linux/mmc/pm.h>
  25. #include <linux/mmc/host.h>
  26. #include <linux/mmc/card.h>
  27. #include <linux/mmc/slot-gpio.h>
  28. #include <linux/amba/bus.h>
  29. #include <linux/clk.h>
  30. #include <linux/scatterlist.h>
  31. #include <linux/gpio.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/regulator/consumer.h>
  34. #include <linux/dmaengine.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/amba/mmci.h>
  37. #include <linux/pm_runtime.h>
  38. #include <linux/types.h>
  39. #include <linux/pinctrl/consumer.h>
  40. #include <asm/div64.h>
  41. #include <asm/io.h>
  42. #include <asm/sizes.h>
  43. #include "mmci.h"
  44. #include "mmci_qcom_dml.h"
  45. #define DRIVER_NAME "mmci-pl18x"
  46. static unsigned int fmax = 515633;
  47. /**
  48. * struct variant_data - MMCI variant-specific quirks
  49. * @clkreg: default value for MCICLOCK register
  50. * @clkreg_enable: enable value for MMCICLOCK register
  51. * @clkreg_8bit_bus_enable: enable value for 8 bit bus
  52. * @clkreg_neg_edge_enable: enable value for inverted data/cmd output
  53. * @datalength_bits: number of bits in the MMCIDATALENGTH register
  54. * @fifosize: number of bytes that can be written when MMCI_TXFIFOEMPTY
  55. * is asserted (likewise for RX)
  56. * @fifohalfsize: number of bytes that can be written when MCI_TXFIFOHALFEMPTY
  57. * is asserted (likewise for RX)
  58. * @data_cmd_enable: enable value for data commands.
  59. * @st_sdio: enable ST specific SDIO logic
  60. * @st_clkdiv: true if using a ST-specific clock divider algorithm
  61. * @datactrl_mask_ddrmode: ddr mode mask in datactrl register.
  62. * @blksz_datactrl16: true if Block size is at b16..b30 position in datactrl register
  63. * @blksz_datactrl4: true if Block size is at b4..b16 position in datactrl
  64. * register
  65. * @datactrl_mask_sdio: SDIO enable mask in datactrl register
  66. * @pwrreg_powerup: power up value for MMCIPOWER register
  67. * @f_max: maximum clk frequency supported by the controller.
  68. * @signal_direction: input/out direction of bus signals can be indicated
  69. * @pwrreg_clkgate: MMCIPOWER register must be used to gate the clock
  70. * @busy_detect: true if busy detection on dat0 is supported
  71. * @pwrreg_nopower: bits in MMCIPOWER don't controls ext. power supply
  72. * @explicit_mclk_control: enable explicit mclk control in driver.
  73. * @qcom_fifo: enables qcom specific fifo pio read logic.
  74. * @qcom_dml: enables qcom specific dma glue for dma transfers.
  75. * @reversed_irq_handling: handle data irq before cmd irq.
  76. */
  77. struct variant_data {
  78. unsigned int clkreg;
  79. unsigned int clkreg_enable;
  80. unsigned int clkreg_8bit_bus_enable;
  81. unsigned int clkreg_neg_edge_enable;
  82. unsigned int datalength_bits;
  83. unsigned int fifosize;
  84. unsigned int fifohalfsize;
  85. unsigned int data_cmd_enable;
  86. unsigned int datactrl_mask_ddrmode;
  87. unsigned int datactrl_mask_sdio;
  88. bool st_sdio;
  89. bool st_clkdiv;
  90. bool blksz_datactrl16;
  91. bool blksz_datactrl4;
  92. u32 pwrreg_powerup;
  93. u32 f_max;
  94. bool signal_direction;
  95. bool pwrreg_clkgate;
  96. bool busy_detect;
  97. bool pwrreg_nopower;
  98. bool explicit_mclk_control;
  99. bool qcom_fifo;
  100. bool qcom_dml;
  101. bool reversed_irq_handling;
  102. };
  103. static struct variant_data variant_arm = {
  104. .fifosize = 16 * 4,
  105. .fifohalfsize = 8 * 4,
  106. .datalength_bits = 16,
  107. .pwrreg_powerup = MCI_PWR_UP,
  108. .f_max = 100000000,
  109. .reversed_irq_handling = true,
  110. };
  111. static struct variant_data variant_arm_extended_fifo = {
  112. .fifosize = 128 * 4,
  113. .fifohalfsize = 64 * 4,
  114. .datalength_bits = 16,
  115. .pwrreg_powerup = MCI_PWR_UP,
  116. .f_max = 100000000,
  117. };
  118. static struct variant_data variant_arm_extended_fifo_hwfc = {
  119. .fifosize = 128 * 4,
  120. .fifohalfsize = 64 * 4,
  121. .clkreg_enable = MCI_ARM_HWFCEN,
  122. .datalength_bits = 16,
  123. .pwrreg_powerup = MCI_PWR_UP,
  124. .f_max = 100000000,
  125. };
  126. static struct variant_data variant_u300 = {
  127. .fifosize = 16 * 4,
  128. .fifohalfsize = 8 * 4,
  129. .clkreg_enable = MCI_ST_U300_HWFCEN,
  130. .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
  131. .datalength_bits = 16,
  132. .datactrl_mask_sdio = MCI_ST_DPSM_SDIOEN,
  133. .st_sdio = true,
  134. .pwrreg_powerup = MCI_PWR_ON,
  135. .f_max = 100000000,
  136. .signal_direction = true,
  137. .pwrreg_clkgate = true,
  138. .pwrreg_nopower = true,
  139. };
  140. static struct variant_data variant_nomadik = {
  141. .fifosize = 16 * 4,
  142. .fifohalfsize = 8 * 4,
  143. .clkreg = MCI_CLK_ENABLE,
  144. .datalength_bits = 24,
  145. .datactrl_mask_sdio = MCI_ST_DPSM_SDIOEN,
  146. .st_sdio = true,
  147. .st_clkdiv = true,
  148. .pwrreg_powerup = MCI_PWR_ON,
  149. .f_max = 100000000,
  150. .signal_direction = true,
  151. .pwrreg_clkgate = true,
  152. .pwrreg_nopower = true,
  153. };
  154. static struct variant_data variant_ux500 = {
  155. .fifosize = 30 * 4,
  156. .fifohalfsize = 8 * 4,
  157. .clkreg = MCI_CLK_ENABLE,
  158. .clkreg_enable = MCI_ST_UX500_HWFCEN,
  159. .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
  160. .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
  161. .datalength_bits = 24,
  162. .datactrl_mask_sdio = MCI_ST_DPSM_SDIOEN,
  163. .st_sdio = true,
  164. .st_clkdiv = true,
  165. .pwrreg_powerup = MCI_PWR_ON,
  166. .f_max = 100000000,
  167. .signal_direction = true,
  168. .pwrreg_clkgate = true,
  169. .busy_detect = true,
  170. .pwrreg_nopower = true,
  171. };
  172. static struct variant_data variant_ux500v2 = {
  173. .fifosize = 30 * 4,
  174. .fifohalfsize = 8 * 4,
  175. .clkreg = MCI_CLK_ENABLE,
  176. .clkreg_enable = MCI_ST_UX500_HWFCEN,
  177. .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
  178. .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
  179. .datactrl_mask_ddrmode = MCI_ST_DPSM_DDRMODE,
  180. .datalength_bits = 24,
  181. .datactrl_mask_sdio = MCI_ST_DPSM_SDIOEN,
  182. .st_sdio = true,
  183. .st_clkdiv = true,
  184. .blksz_datactrl16 = true,
  185. .pwrreg_powerup = MCI_PWR_ON,
  186. .f_max = 100000000,
  187. .signal_direction = true,
  188. .pwrreg_clkgate = true,
  189. .busy_detect = true,
  190. .pwrreg_nopower = true,
  191. };
  192. static struct variant_data variant_qcom = {
  193. .fifosize = 16 * 4,
  194. .fifohalfsize = 8 * 4,
  195. .clkreg = MCI_CLK_ENABLE,
  196. .clkreg_enable = MCI_QCOM_CLK_FLOWENA |
  197. MCI_QCOM_CLK_SELECT_IN_FBCLK,
  198. .clkreg_8bit_bus_enable = MCI_QCOM_CLK_WIDEBUS_8,
  199. .datactrl_mask_ddrmode = MCI_QCOM_CLK_SELECT_IN_DDR_MODE,
  200. .data_cmd_enable = MCI_QCOM_CSPM_DATCMD,
  201. .blksz_datactrl4 = true,
  202. .datalength_bits = 24,
  203. .pwrreg_powerup = MCI_PWR_UP,
  204. .f_max = 208000000,
  205. .explicit_mclk_control = true,
  206. .qcom_fifo = true,
  207. .qcom_dml = true,
  208. };
  209. static int mmci_card_busy(struct mmc_host *mmc)
  210. {
  211. struct mmci_host *host = mmc_priv(mmc);
  212. unsigned long flags;
  213. int busy = 0;
  214. pm_runtime_get_sync(mmc_dev(mmc));
  215. spin_lock_irqsave(&host->lock, flags);
  216. if (readl(host->base + MMCISTATUS) & MCI_ST_CARDBUSY)
  217. busy = 1;
  218. spin_unlock_irqrestore(&host->lock, flags);
  219. pm_runtime_mark_last_busy(mmc_dev(mmc));
  220. pm_runtime_put_autosuspend(mmc_dev(mmc));
  221. return busy;
  222. }
  223. /*
  224. * Validate mmc prerequisites
  225. */
  226. static int mmci_validate_data(struct mmci_host *host,
  227. struct mmc_data *data)
  228. {
  229. if (!data)
  230. return 0;
  231. if (!is_power_of_2(data->blksz)) {
  232. dev_err(mmc_dev(host->mmc),
  233. "unsupported block size (%d bytes)\n", data->blksz);
  234. return -EINVAL;
  235. }
  236. return 0;
  237. }
  238. static void mmci_reg_delay(struct mmci_host *host)
  239. {
  240. /*
  241. * According to the spec, at least three feedback clock cycles
  242. * of max 52 MHz must pass between two writes to the MMCICLOCK reg.
  243. * Three MCLK clock cycles must pass between two MMCIPOWER reg writes.
  244. * Worst delay time during card init is at 100 kHz => 30 us.
  245. * Worst delay time when up and running is at 25 MHz => 120 ns.
  246. */
  247. if (host->cclk < 25000000)
  248. udelay(30);
  249. else
  250. ndelay(120);
  251. }
  252. /*
  253. * This must be called with host->lock held
  254. */
  255. static void mmci_write_clkreg(struct mmci_host *host, u32 clk)
  256. {
  257. if (host->clk_reg != clk) {
  258. host->clk_reg = clk;
  259. writel(clk, host->base + MMCICLOCK);
  260. }
  261. }
  262. /*
  263. * This must be called with host->lock held
  264. */
  265. static void mmci_write_pwrreg(struct mmci_host *host, u32 pwr)
  266. {
  267. if (host->pwr_reg != pwr) {
  268. host->pwr_reg = pwr;
  269. writel(pwr, host->base + MMCIPOWER);
  270. }
  271. }
  272. /*
  273. * This must be called with host->lock held
  274. */
  275. static void mmci_write_datactrlreg(struct mmci_host *host, u32 datactrl)
  276. {
  277. /* Keep ST Micro busy mode if enabled */
  278. datactrl |= host->datactrl_reg & MCI_ST_DPSM_BUSYMODE;
  279. if (host->datactrl_reg != datactrl) {
  280. host->datactrl_reg = datactrl;
  281. writel(datactrl, host->base + MMCIDATACTRL);
  282. }
  283. }
  284. /*
  285. * This must be called with host->lock held
  286. */
  287. static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
  288. {
  289. struct variant_data *variant = host->variant;
  290. u32 clk = variant->clkreg;
  291. /* Make sure cclk reflects the current calculated clock */
  292. host->cclk = 0;
  293. if (desired) {
  294. if (variant->explicit_mclk_control) {
  295. host->cclk = host->mclk;
  296. } else if (desired >= host->mclk) {
  297. clk = MCI_CLK_BYPASS;
  298. if (variant->st_clkdiv)
  299. clk |= MCI_ST_UX500_NEG_EDGE;
  300. host->cclk = host->mclk;
  301. } else if (variant->st_clkdiv) {
  302. /*
  303. * DB8500 TRM says f = mclk / (clkdiv + 2)
  304. * => clkdiv = (mclk / f) - 2
  305. * Round the divider up so we don't exceed the max
  306. * frequency
  307. */
  308. clk = DIV_ROUND_UP(host->mclk, desired) - 2;
  309. if (clk >= 256)
  310. clk = 255;
  311. host->cclk = host->mclk / (clk + 2);
  312. } else {
  313. /*
  314. * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
  315. * => clkdiv = mclk / (2 * f) - 1
  316. */
  317. clk = host->mclk / (2 * desired) - 1;
  318. if (clk >= 256)
  319. clk = 255;
  320. host->cclk = host->mclk / (2 * (clk + 1));
  321. }
  322. clk |= variant->clkreg_enable;
  323. clk |= MCI_CLK_ENABLE;
  324. /* This hasn't proven to be worthwhile */
  325. /* clk |= MCI_CLK_PWRSAVE; */
  326. }
  327. /* Set actual clock for debug */
  328. host->mmc->actual_clock = host->cclk;
  329. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
  330. clk |= MCI_4BIT_BUS;
  331. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
  332. clk |= variant->clkreg_8bit_bus_enable;
  333. if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
  334. host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
  335. clk |= variant->clkreg_neg_edge_enable;
  336. mmci_write_clkreg(host, clk);
  337. }
  338. static void
  339. mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
  340. {
  341. writel(0, host->base + MMCICOMMAND);
  342. BUG_ON(host->data);
  343. host->mrq = NULL;
  344. host->cmd = NULL;
  345. mmc_request_done(host->mmc, mrq);
  346. pm_runtime_mark_last_busy(mmc_dev(host->mmc));
  347. pm_runtime_put_autosuspend(mmc_dev(host->mmc));
  348. }
  349. static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
  350. {
  351. void __iomem *base = host->base;
  352. if (host->singleirq) {
  353. unsigned int mask0 = readl(base + MMCIMASK0);
  354. mask0 &= ~MCI_IRQ1MASK;
  355. mask0 |= mask;
  356. writel(mask0, base + MMCIMASK0);
  357. }
  358. writel(mask, base + MMCIMASK1);
  359. }
  360. static void mmci_stop_data(struct mmci_host *host)
  361. {
  362. mmci_write_datactrlreg(host, 0);
  363. mmci_set_mask1(host, 0);
  364. host->data = NULL;
  365. }
  366. static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
  367. {
  368. unsigned int flags = SG_MITER_ATOMIC;
  369. if (data->flags & MMC_DATA_READ)
  370. flags |= SG_MITER_TO_SG;
  371. else
  372. flags |= SG_MITER_FROM_SG;
  373. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  374. }
  375. /*
  376. * All the DMA operation mode stuff goes inside this ifdef.
  377. * This assumes that you have a generic DMA device interface,
  378. * no custom DMA interfaces are supported.
  379. */
  380. #ifdef CONFIG_DMA_ENGINE
  381. static void mmci_dma_setup(struct mmci_host *host)
  382. {
  383. const char *rxname, *txname;
  384. struct variant_data *variant = host->variant;
  385. host->dma_rx_channel = dma_request_slave_channel(mmc_dev(host->mmc), "rx");
  386. host->dma_tx_channel = dma_request_slave_channel(mmc_dev(host->mmc), "tx");
  387. /* initialize pre request cookie */
  388. host->next_data.cookie = 1;
  389. /*
  390. * If only an RX channel is specified, the driver will
  391. * attempt to use it bidirectionally, however if it is
  392. * is specified but cannot be located, DMA will be disabled.
  393. */
  394. if (host->dma_rx_channel && !host->dma_tx_channel)
  395. host->dma_tx_channel = host->dma_rx_channel;
  396. if (host->dma_rx_channel)
  397. rxname = dma_chan_name(host->dma_rx_channel);
  398. else
  399. rxname = "none";
  400. if (host->dma_tx_channel)
  401. txname = dma_chan_name(host->dma_tx_channel);
  402. else
  403. txname = "none";
  404. dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
  405. rxname, txname);
  406. /*
  407. * Limit the maximum segment size in any SG entry according to
  408. * the parameters of the DMA engine device.
  409. */
  410. if (host->dma_tx_channel) {
  411. struct device *dev = host->dma_tx_channel->device->dev;
  412. unsigned int max_seg_size = dma_get_max_seg_size(dev);
  413. if (max_seg_size < host->mmc->max_seg_size)
  414. host->mmc->max_seg_size = max_seg_size;
  415. }
  416. if (host->dma_rx_channel) {
  417. struct device *dev = host->dma_rx_channel->device->dev;
  418. unsigned int max_seg_size = dma_get_max_seg_size(dev);
  419. if (max_seg_size < host->mmc->max_seg_size)
  420. host->mmc->max_seg_size = max_seg_size;
  421. }
  422. if (variant->qcom_dml && host->dma_rx_channel && host->dma_tx_channel)
  423. if (dml_hw_init(host, host->mmc->parent->of_node))
  424. variant->qcom_dml = false;
  425. }
  426. /*
  427. * This is used in or so inline it
  428. * so it can be discarded.
  429. */
  430. static inline void mmci_dma_release(struct mmci_host *host)
  431. {
  432. if (host->dma_rx_channel)
  433. dma_release_channel(host->dma_rx_channel);
  434. if (host->dma_tx_channel)
  435. dma_release_channel(host->dma_tx_channel);
  436. host->dma_rx_channel = host->dma_tx_channel = NULL;
  437. }
  438. static void mmci_dma_data_error(struct mmci_host *host)
  439. {
  440. dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
  441. dmaengine_terminate_all(host->dma_current);
  442. host->dma_current = NULL;
  443. host->dma_desc_current = NULL;
  444. host->data->host_cookie = 0;
  445. }
  446. static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
  447. {
  448. struct dma_chan *chan;
  449. enum dma_data_direction dir;
  450. if (data->flags & MMC_DATA_READ) {
  451. dir = DMA_FROM_DEVICE;
  452. chan = host->dma_rx_channel;
  453. } else {
  454. dir = DMA_TO_DEVICE;
  455. chan = host->dma_tx_channel;
  456. }
  457. dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, dir);
  458. }
  459. static void mmci_dma_finalize(struct mmci_host *host, struct mmc_data *data)
  460. {
  461. u32 status;
  462. int i;
  463. /* Wait up to 1ms for the DMA to complete */
  464. for (i = 0; ; i++) {
  465. status = readl(host->base + MMCISTATUS);
  466. if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
  467. break;
  468. udelay(10);
  469. }
  470. /*
  471. * Check to see whether we still have some data left in the FIFO -
  472. * this catches DMA controllers which are unable to monitor the
  473. * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
  474. * contiguous buffers. On TX, we'll get a FIFO underrun error.
  475. */
  476. if (status & MCI_RXDATAAVLBLMASK) {
  477. mmci_dma_data_error(host);
  478. if (!data->error)
  479. data->error = -EIO;
  480. }
  481. if (!data->host_cookie)
  482. mmci_dma_unmap(host, data);
  483. /*
  484. * Use of DMA with scatter-gather is impossible.
  485. * Give up with DMA and switch back to PIO mode.
  486. */
  487. if (status & MCI_RXDATAAVLBLMASK) {
  488. dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
  489. mmci_dma_release(host);
  490. }
  491. host->dma_current = NULL;
  492. host->dma_desc_current = NULL;
  493. }
  494. /* prepares DMA channel and DMA descriptor, returns non-zero on failure */
  495. static int __mmci_dma_prep_data(struct mmci_host *host, struct mmc_data *data,
  496. struct dma_chan **dma_chan,
  497. struct dma_async_tx_descriptor **dma_desc)
  498. {
  499. struct variant_data *variant = host->variant;
  500. struct dma_slave_config conf = {
  501. .src_addr = host->phybase + MMCIFIFO,
  502. .dst_addr = host->phybase + MMCIFIFO,
  503. .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  504. .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  505. .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
  506. .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
  507. .device_fc = false,
  508. };
  509. struct dma_chan *chan;
  510. struct dma_device *device;
  511. struct dma_async_tx_descriptor *desc;
  512. enum dma_data_direction buffer_dirn;
  513. int nr_sg;
  514. unsigned long flags = DMA_CTRL_ACK;
  515. if (data->flags & MMC_DATA_READ) {
  516. conf.direction = DMA_DEV_TO_MEM;
  517. buffer_dirn = DMA_FROM_DEVICE;
  518. chan = host->dma_rx_channel;
  519. } else {
  520. conf.direction = DMA_MEM_TO_DEV;
  521. buffer_dirn = DMA_TO_DEVICE;
  522. chan = host->dma_tx_channel;
  523. }
  524. /* If there's no DMA channel, fall back to PIO */
  525. if (!chan)
  526. return -EINVAL;
  527. /* If less than or equal to the fifo size, don't bother with DMA */
  528. if (data->blksz * data->blocks <= variant->fifosize)
  529. return -EINVAL;
  530. device = chan->device;
  531. nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len, buffer_dirn);
  532. if (nr_sg == 0)
  533. return -EINVAL;
  534. if (host->variant->qcom_dml)
  535. flags |= DMA_PREP_INTERRUPT;
  536. dmaengine_slave_config(chan, &conf);
  537. desc = dmaengine_prep_slave_sg(chan, data->sg, nr_sg,
  538. conf.direction, flags);
  539. if (!desc)
  540. goto unmap_exit;
  541. *dma_chan = chan;
  542. *dma_desc = desc;
  543. return 0;
  544. unmap_exit:
  545. dma_unmap_sg(device->dev, data->sg, data->sg_len, buffer_dirn);
  546. return -ENOMEM;
  547. }
  548. static inline int mmci_dma_prep_data(struct mmci_host *host,
  549. struct mmc_data *data)
  550. {
  551. /* Check if next job is already prepared. */
  552. if (host->dma_current && host->dma_desc_current)
  553. return 0;
  554. /* No job were prepared thus do it now. */
  555. return __mmci_dma_prep_data(host, data, &host->dma_current,
  556. &host->dma_desc_current);
  557. }
  558. static inline int mmci_dma_prep_next(struct mmci_host *host,
  559. struct mmc_data *data)
  560. {
  561. struct mmci_host_next *nd = &host->next_data;
  562. return __mmci_dma_prep_data(host, data, &nd->dma_chan, &nd->dma_desc);
  563. }
  564. static int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
  565. {
  566. int ret;
  567. struct mmc_data *data = host->data;
  568. ret = mmci_dma_prep_data(host, host->data);
  569. if (ret)
  570. return ret;
  571. /* Okay, go for it. */
  572. dev_vdbg(mmc_dev(host->mmc),
  573. "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
  574. data->sg_len, data->blksz, data->blocks, data->flags);
  575. dmaengine_submit(host->dma_desc_current);
  576. dma_async_issue_pending(host->dma_current);
  577. if (host->variant->qcom_dml)
  578. dml_start_xfer(host, data);
  579. datactrl |= MCI_DPSM_DMAENABLE;
  580. /* Trigger the DMA transfer */
  581. mmci_write_datactrlreg(host, datactrl);
  582. /*
  583. * Let the MMCI say when the data is ended and it's time
  584. * to fire next DMA request. When that happens, MMCI will
  585. * call mmci_data_end()
  586. */
  587. writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
  588. host->base + MMCIMASK0);
  589. return 0;
  590. }
  591. static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
  592. {
  593. struct mmci_host_next *next = &host->next_data;
  594. WARN_ON(data->host_cookie && data->host_cookie != next->cookie);
  595. WARN_ON(!data->host_cookie && (next->dma_desc || next->dma_chan));
  596. host->dma_desc_current = next->dma_desc;
  597. host->dma_current = next->dma_chan;
  598. next->dma_desc = NULL;
  599. next->dma_chan = NULL;
  600. }
  601. static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq,
  602. bool is_first_req)
  603. {
  604. struct mmci_host *host = mmc_priv(mmc);
  605. struct mmc_data *data = mrq->data;
  606. struct mmci_host_next *nd = &host->next_data;
  607. if (!data)
  608. return;
  609. BUG_ON(data->host_cookie);
  610. if (mmci_validate_data(host, data))
  611. return;
  612. if (!mmci_dma_prep_next(host, data))
  613. data->host_cookie = ++nd->cookie < 0 ? 1 : nd->cookie;
  614. }
  615. static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
  616. int err)
  617. {
  618. struct mmci_host *host = mmc_priv(mmc);
  619. struct mmc_data *data = mrq->data;
  620. if (!data || !data->host_cookie)
  621. return;
  622. mmci_dma_unmap(host, data);
  623. if (err) {
  624. struct mmci_host_next *next = &host->next_data;
  625. struct dma_chan *chan;
  626. if (data->flags & MMC_DATA_READ)
  627. chan = host->dma_rx_channel;
  628. else
  629. chan = host->dma_tx_channel;
  630. dmaengine_terminate_all(chan);
  631. if (host->dma_desc_current == next->dma_desc)
  632. host->dma_desc_current = NULL;
  633. if (host->dma_current == next->dma_chan)
  634. host->dma_current = NULL;
  635. next->dma_desc = NULL;
  636. next->dma_chan = NULL;
  637. data->host_cookie = 0;
  638. }
  639. }
  640. #else
  641. /* Blank functions if the DMA engine is not available */
  642. static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
  643. {
  644. }
  645. static inline void mmci_dma_setup(struct mmci_host *host)
  646. {
  647. }
  648. static inline void mmci_dma_release(struct mmci_host *host)
  649. {
  650. }
  651. static inline void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
  652. {
  653. }
  654. static inline void mmci_dma_finalize(struct mmci_host *host,
  655. struct mmc_data *data)
  656. {
  657. }
  658. static inline void mmci_dma_data_error(struct mmci_host *host)
  659. {
  660. }
  661. static inline int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
  662. {
  663. return -ENOSYS;
  664. }
  665. #define mmci_pre_request NULL
  666. #define mmci_post_request NULL
  667. #endif
  668. static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
  669. {
  670. struct variant_data *variant = host->variant;
  671. unsigned int datactrl, timeout, irqmask;
  672. unsigned long long clks;
  673. void __iomem *base;
  674. int blksz_bits;
  675. dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
  676. data->blksz, data->blocks, data->flags);
  677. host->data = data;
  678. host->size = data->blksz * data->blocks;
  679. data->bytes_xfered = 0;
  680. clks = (unsigned long long)data->timeout_ns * host->cclk;
  681. do_div(clks, NSEC_PER_SEC);
  682. timeout = data->timeout_clks + (unsigned int)clks;
  683. base = host->base;
  684. writel(timeout, base + MMCIDATATIMER);
  685. writel(host->size, base + MMCIDATALENGTH);
  686. blksz_bits = ffs(data->blksz) - 1;
  687. BUG_ON(1 << blksz_bits != data->blksz);
  688. if (variant->blksz_datactrl16)
  689. datactrl = MCI_DPSM_ENABLE | (data->blksz << 16);
  690. else if (variant->blksz_datactrl4)
  691. datactrl = MCI_DPSM_ENABLE | (data->blksz << 4);
  692. else
  693. datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
  694. if (data->flags & MMC_DATA_READ)
  695. datactrl |= MCI_DPSM_DIRECTION;
  696. if (host->mmc->card && mmc_card_sdio(host->mmc->card)) {
  697. u32 clk;
  698. datactrl |= variant->datactrl_mask_sdio;
  699. /*
  700. * The ST Micro variant for SDIO small write transfers
  701. * needs to have clock H/W flow control disabled,
  702. * otherwise the transfer will not start. The threshold
  703. * depends on the rate of MCLK.
  704. */
  705. if (variant->st_sdio && data->flags & MMC_DATA_WRITE &&
  706. (host->size < 8 ||
  707. (host->size <= 8 && host->mclk > 50000000)))
  708. clk = host->clk_reg & ~variant->clkreg_enable;
  709. else
  710. clk = host->clk_reg | variant->clkreg_enable;
  711. mmci_write_clkreg(host, clk);
  712. }
  713. if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
  714. host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
  715. datactrl |= variant->datactrl_mask_ddrmode;
  716. /*
  717. * Attempt to use DMA operation mode, if this
  718. * should fail, fall back to PIO mode
  719. */
  720. if (!mmci_dma_start_data(host, datactrl))
  721. return;
  722. /* IRQ mode, map the SG list for CPU reading/writing */
  723. mmci_init_sg(host, data);
  724. if (data->flags & MMC_DATA_READ) {
  725. irqmask = MCI_RXFIFOHALFFULLMASK;
  726. /*
  727. * If we have less than the fifo 'half-full' threshold to
  728. * transfer, trigger a PIO interrupt as soon as any data
  729. * is available.
  730. */
  731. if (host->size < variant->fifohalfsize)
  732. irqmask |= MCI_RXDATAAVLBLMASK;
  733. } else {
  734. /*
  735. * We don't actually need to include "FIFO empty" here
  736. * since its implicit in "FIFO half empty".
  737. */
  738. irqmask = MCI_TXFIFOHALFEMPTYMASK;
  739. }
  740. mmci_write_datactrlreg(host, datactrl);
  741. writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
  742. mmci_set_mask1(host, irqmask);
  743. }
  744. static void
  745. mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
  746. {
  747. void __iomem *base = host->base;
  748. dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
  749. cmd->opcode, cmd->arg, cmd->flags);
  750. if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
  751. writel(0, base + MMCICOMMAND);
  752. mmci_reg_delay(host);
  753. }
  754. c |= cmd->opcode | MCI_CPSM_ENABLE;
  755. if (cmd->flags & MMC_RSP_PRESENT) {
  756. if (cmd->flags & MMC_RSP_136)
  757. c |= MCI_CPSM_LONGRSP;
  758. c |= MCI_CPSM_RESPONSE;
  759. }
  760. if (/*interrupt*/0)
  761. c |= MCI_CPSM_INTERRUPT;
  762. if (mmc_cmd_type(cmd) == MMC_CMD_ADTC)
  763. c |= host->variant->data_cmd_enable;
  764. host->cmd = cmd;
  765. writel(cmd->arg, base + MMCIARGUMENT);
  766. writel(c, base + MMCICOMMAND);
  767. }
  768. static void
  769. mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
  770. unsigned int status)
  771. {
  772. /* Make sure we have data to handle */
  773. if (!data)
  774. return;
  775. /* First check for errors */
  776. if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_STARTBITERR|
  777. MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
  778. u32 remain, success;
  779. /* Terminate the DMA transfer */
  780. if (dma_inprogress(host)) {
  781. mmci_dma_data_error(host);
  782. mmci_dma_unmap(host, data);
  783. }
  784. /*
  785. * Calculate how far we are into the transfer. Note that
  786. * the data counter gives the number of bytes transferred
  787. * on the MMC bus, not on the host side. On reads, this
  788. * can be as much as a FIFO-worth of data ahead. This
  789. * matters for FIFO overruns only.
  790. */
  791. remain = readl(host->base + MMCIDATACNT);
  792. success = data->blksz * data->blocks - remain;
  793. dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
  794. status, success);
  795. if (status & MCI_DATACRCFAIL) {
  796. /* Last block was not successful */
  797. success -= 1;
  798. data->error = -EILSEQ;
  799. } else if (status & MCI_DATATIMEOUT) {
  800. data->error = -ETIMEDOUT;
  801. } else if (status & MCI_STARTBITERR) {
  802. data->error = -ECOMM;
  803. } else if (status & MCI_TXUNDERRUN) {
  804. data->error = -EIO;
  805. } else if (status & MCI_RXOVERRUN) {
  806. if (success > host->variant->fifosize)
  807. success -= host->variant->fifosize;
  808. else
  809. success = 0;
  810. data->error = -EIO;
  811. }
  812. data->bytes_xfered = round_down(success, data->blksz);
  813. }
  814. if (status & MCI_DATABLOCKEND)
  815. dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
  816. if (status & MCI_DATAEND || data->error) {
  817. if (dma_inprogress(host))
  818. mmci_dma_finalize(host, data);
  819. mmci_stop_data(host);
  820. if (!data->error)
  821. /* The error clause is handled above, success! */
  822. data->bytes_xfered = data->blksz * data->blocks;
  823. if (!data->stop || host->mrq->sbc) {
  824. mmci_request_end(host, data->mrq);
  825. } else {
  826. mmci_start_command(host, data->stop, 0);
  827. }
  828. }
  829. }
  830. static void
  831. mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
  832. unsigned int status)
  833. {
  834. void __iomem *base = host->base;
  835. bool sbc, busy_resp;
  836. if (!cmd)
  837. return;
  838. sbc = (cmd == host->mrq->sbc);
  839. busy_resp = host->variant->busy_detect && (cmd->flags & MMC_RSP_BUSY);
  840. if (!((status|host->busy_status) & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|
  841. MCI_CMDSENT|MCI_CMDRESPEND)))
  842. return;
  843. /* Check if we need to wait for busy completion. */
  844. if (host->busy_status && (status & MCI_ST_CARDBUSY))
  845. return;
  846. /* Enable busy completion if needed and supported. */
  847. if (!host->busy_status && busy_resp &&
  848. !(status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT)) &&
  849. (readl(base + MMCISTATUS) & MCI_ST_CARDBUSY)) {
  850. writel(readl(base + MMCIMASK0) | MCI_ST_BUSYEND,
  851. base + MMCIMASK0);
  852. host->busy_status = status & (MCI_CMDSENT|MCI_CMDRESPEND);
  853. return;
  854. }
  855. /* At busy completion, mask the IRQ and complete the request. */
  856. if (host->busy_status) {
  857. writel(readl(base + MMCIMASK0) & ~MCI_ST_BUSYEND,
  858. base + MMCIMASK0);
  859. host->busy_status = 0;
  860. }
  861. host->cmd = NULL;
  862. if (status & MCI_CMDTIMEOUT) {
  863. cmd->error = -ETIMEDOUT;
  864. } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
  865. cmd->error = -EILSEQ;
  866. } else {
  867. cmd->resp[0] = readl(base + MMCIRESPONSE0);
  868. cmd->resp[1] = readl(base + MMCIRESPONSE1);
  869. cmd->resp[2] = readl(base + MMCIRESPONSE2);
  870. cmd->resp[3] = readl(base + MMCIRESPONSE3);
  871. }
  872. if ((!sbc && !cmd->data) || cmd->error) {
  873. if (host->data) {
  874. /* Terminate the DMA transfer */
  875. if (dma_inprogress(host)) {
  876. mmci_dma_data_error(host);
  877. mmci_dma_unmap(host, host->data);
  878. }
  879. mmci_stop_data(host);
  880. }
  881. mmci_request_end(host, host->mrq);
  882. } else if (sbc) {
  883. mmci_start_command(host, host->mrq->cmd, 0);
  884. } else if (!(cmd->data->flags & MMC_DATA_READ)) {
  885. mmci_start_data(host, cmd->data);
  886. }
  887. }
  888. static int mmci_get_rx_fifocnt(struct mmci_host *host, u32 status, int remain)
  889. {
  890. return remain - (readl(host->base + MMCIFIFOCNT) << 2);
  891. }
  892. static int mmci_qcom_get_rx_fifocnt(struct mmci_host *host, u32 status, int r)
  893. {
  894. /*
  895. * on qcom SDCC4 only 8 words are used in each burst so only 8 addresses
  896. * from the fifo range should be used
  897. */
  898. if (status & MCI_RXFIFOHALFFULL)
  899. return host->variant->fifohalfsize;
  900. else if (status & MCI_RXDATAAVLBL)
  901. return 4;
  902. return 0;
  903. }
  904. static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
  905. {
  906. void __iomem *base = host->base;
  907. char *ptr = buffer;
  908. u32 status = readl(host->base + MMCISTATUS);
  909. int host_remain = host->size;
  910. do {
  911. int count = host->get_rx_fifocnt(host, status, host_remain);
  912. if (count > remain)
  913. count = remain;
  914. if (count <= 0)
  915. break;
  916. /*
  917. * SDIO especially may want to send something that is
  918. * not divisible by 4 (as opposed to card sectors
  919. * etc). Therefore make sure to always read the last bytes
  920. * while only doing full 32-bit reads towards the FIFO.
  921. */
  922. if (unlikely(count & 0x3)) {
  923. if (count < 4) {
  924. unsigned char buf[4];
  925. ioread32_rep(base + MMCIFIFO, buf, 1);
  926. memcpy(ptr, buf, count);
  927. } else {
  928. ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
  929. count &= ~0x3;
  930. }
  931. } else {
  932. ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
  933. }
  934. ptr += count;
  935. remain -= count;
  936. host_remain -= count;
  937. if (remain == 0)
  938. break;
  939. status = readl(base + MMCISTATUS);
  940. } while (status & MCI_RXDATAAVLBL);
  941. return ptr - buffer;
  942. }
  943. static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
  944. {
  945. struct variant_data *variant = host->variant;
  946. void __iomem *base = host->base;
  947. char *ptr = buffer;
  948. do {
  949. unsigned int count, maxcnt;
  950. maxcnt = status & MCI_TXFIFOEMPTY ?
  951. variant->fifosize : variant->fifohalfsize;
  952. count = min(remain, maxcnt);
  953. /*
  954. * SDIO especially may want to send something that is
  955. * not divisible by 4 (as opposed to card sectors
  956. * etc), and the FIFO only accept full 32-bit writes.
  957. * So compensate by adding +3 on the count, a single
  958. * byte become a 32bit write, 7 bytes will be two
  959. * 32bit writes etc.
  960. */
  961. iowrite32_rep(base + MMCIFIFO, ptr, (count + 3) >> 2);
  962. ptr += count;
  963. remain -= count;
  964. if (remain == 0)
  965. break;
  966. status = readl(base + MMCISTATUS);
  967. } while (status & MCI_TXFIFOHALFEMPTY);
  968. return ptr - buffer;
  969. }
  970. /*
  971. * PIO data transfer IRQ handler.
  972. */
  973. static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
  974. {
  975. struct mmci_host *host = dev_id;
  976. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  977. struct variant_data *variant = host->variant;
  978. void __iomem *base = host->base;
  979. unsigned long flags;
  980. u32 status;
  981. status = readl(base + MMCISTATUS);
  982. dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
  983. local_irq_save(flags);
  984. do {
  985. unsigned int remain, len;
  986. char *buffer;
  987. /*
  988. * For write, we only need to test the half-empty flag
  989. * here - if the FIFO is completely empty, then by
  990. * definition it is more than half empty.
  991. *
  992. * For read, check for data available.
  993. */
  994. if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
  995. break;
  996. if (!sg_miter_next(sg_miter))
  997. break;
  998. buffer = sg_miter->addr;
  999. remain = sg_miter->length;
  1000. len = 0;
  1001. if (status & MCI_RXACTIVE)
  1002. len = mmci_pio_read(host, buffer, remain);
  1003. if (status & MCI_TXACTIVE)
  1004. len = mmci_pio_write(host, buffer, remain, status);
  1005. sg_miter->consumed = len;
  1006. host->size -= len;
  1007. remain -= len;
  1008. if (remain)
  1009. break;
  1010. status = readl(base + MMCISTATUS);
  1011. } while (1);
  1012. sg_miter_stop(sg_miter);
  1013. local_irq_restore(flags);
  1014. /*
  1015. * If we have less than the fifo 'half-full' threshold to transfer,
  1016. * trigger a PIO interrupt as soon as any data is available.
  1017. */
  1018. if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
  1019. mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
  1020. /*
  1021. * If we run out of data, disable the data IRQs; this
  1022. * prevents a race where the FIFO becomes empty before
  1023. * the chip itself has disabled the data path, and
  1024. * stops us racing with our data end IRQ.
  1025. */
  1026. if (host->size == 0) {
  1027. mmci_set_mask1(host, 0);
  1028. writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
  1029. }
  1030. return IRQ_HANDLED;
  1031. }
  1032. /*
  1033. * Handle completion of command and data transfers.
  1034. */
  1035. static irqreturn_t mmci_irq(int irq, void *dev_id)
  1036. {
  1037. struct mmci_host *host = dev_id;
  1038. u32 status;
  1039. int ret = 0;
  1040. spin_lock(&host->lock);
  1041. do {
  1042. status = readl(host->base + MMCISTATUS);
  1043. if (host->singleirq) {
  1044. if (status & readl(host->base + MMCIMASK1))
  1045. mmci_pio_irq(irq, dev_id);
  1046. status &= ~MCI_IRQ1MASK;
  1047. }
  1048. /*
  1049. * We intentionally clear the MCI_ST_CARDBUSY IRQ here (if it's
  1050. * enabled) since the HW seems to be triggering the IRQ on both
  1051. * edges while monitoring DAT0 for busy completion.
  1052. */
  1053. status &= readl(host->base + MMCIMASK0);
  1054. writel(status, host->base + MMCICLEAR);
  1055. dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
  1056. if (host->variant->reversed_irq_handling) {
  1057. mmci_data_irq(host, host->data, status);
  1058. mmci_cmd_irq(host, host->cmd, status);
  1059. } else {
  1060. mmci_cmd_irq(host, host->cmd, status);
  1061. mmci_data_irq(host, host->data, status);
  1062. }
  1063. /* Don't poll for busy completion in irq context. */
  1064. if (host->busy_status)
  1065. status &= ~MCI_ST_CARDBUSY;
  1066. ret = 1;
  1067. } while (status);
  1068. spin_unlock(&host->lock);
  1069. return IRQ_RETVAL(ret);
  1070. }
  1071. static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  1072. {
  1073. struct mmci_host *host = mmc_priv(mmc);
  1074. unsigned long flags;
  1075. WARN_ON(host->mrq != NULL);
  1076. mrq->cmd->error = mmci_validate_data(host, mrq->data);
  1077. if (mrq->cmd->error) {
  1078. mmc_request_done(mmc, mrq);
  1079. return;
  1080. }
  1081. pm_runtime_get_sync(mmc_dev(mmc));
  1082. spin_lock_irqsave(&host->lock, flags);
  1083. host->mrq = mrq;
  1084. if (mrq->data)
  1085. mmci_get_next_data(host, mrq->data);
  1086. if (mrq->data && mrq->data->flags & MMC_DATA_READ)
  1087. mmci_start_data(host, mrq->data);
  1088. if (mrq->sbc)
  1089. mmci_start_command(host, mrq->sbc, 0);
  1090. else
  1091. mmci_start_command(host, mrq->cmd, 0);
  1092. spin_unlock_irqrestore(&host->lock, flags);
  1093. }
  1094. static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1095. {
  1096. struct mmci_host *host = mmc_priv(mmc);
  1097. struct variant_data *variant = host->variant;
  1098. u32 pwr = 0;
  1099. unsigned long flags;
  1100. int ret;
  1101. pm_runtime_get_sync(mmc_dev(mmc));
  1102. if (host->plat->ios_handler &&
  1103. host->plat->ios_handler(mmc_dev(mmc), ios))
  1104. dev_err(mmc_dev(mmc), "platform ios_handler failed\n");
  1105. switch (ios->power_mode) {
  1106. case MMC_POWER_OFF:
  1107. if (!IS_ERR(mmc->supply.vmmc))
  1108. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  1109. if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
  1110. regulator_disable(mmc->supply.vqmmc);
  1111. host->vqmmc_enabled = false;
  1112. }
  1113. break;
  1114. case MMC_POWER_UP:
  1115. if (!IS_ERR(mmc->supply.vmmc))
  1116. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
  1117. /*
  1118. * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
  1119. * and instead uses MCI_PWR_ON so apply whatever value is
  1120. * configured in the variant data.
  1121. */
  1122. pwr |= variant->pwrreg_powerup;
  1123. break;
  1124. case MMC_POWER_ON:
  1125. if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
  1126. ret = regulator_enable(mmc->supply.vqmmc);
  1127. if (ret < 0)
  1128. dev_err(mmc_dev(mmc),
  1129. "failed to enable vqmmc regulator\n");
  1130. else
  1131. host->vqmmc_enabled = true;
  1132. }
  1133. pwr |= MCI_PWR_ON;
  1134. break;
  1135. }
  1136. if (variant->signal_direction && ios->power_mode != MMC_POWER_OFF) {
  1137. /*
  1138. * The ST Micro variant has some additional bits
  1139. * indicating signal direction for the signals in
  1140. * the SD/MMC bus and feedback-clock usage.
  1141. */
  1142. pwr |= host->pwr_reg_add;
  1143. if (ios->bus_width == MMC_BUS_WIDTH_4)
  1144. pwr &= ~MCI_ST_DATA74DIREN;
  1145. else if (ios->bus_width == MMC_BUS_WIDTH_1)
  1146. pwr &= (~MCI_ST_DATA74DIREN &
  1147. ~MCI_ST_DATA31DIREN &
  1148. ~MCI_ST_DATA2DIREN);
  1149. }
  1150. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
  1151. if (host->hw_designer != AMBA_VENDOR_ST)
  1152. pwr |= MCI_ROD;
  1153. else {
  1154. /*
  1155. * The ST Micro variant use the ROD bit for something
  1156. * else and only has OD (Open Drain).
  1157. */
  1158. pwr |= MCI_OD;
  1159. }
  1160. }
  1161. /*
  1162. * If clock = 0 and the variant requires the MMCIPOWER to be used for
  1163. * gating the clock, the MCI_PWR_ON bit is cleared.
  1164. */
  1165. if (!ios->clock && variant->pwrreg_clkgate)
  1166. pwr &= ~MCI_PWR_ON;
  1167. if (host->variant->explicit_mclk_control &&
  1168. ios->clock != host->clock_cache) {
  1169. ret = clk_set_rate(host->clk, ios->clock);
  1170. if (ret < 0)
  1171. dev_err(mmc_dev(host->mmc),
  1172. "Error setting clock rate (%d)\n", ret);
  1173. else
  1174. host->mclk = clk_get_rate(host->clk);
  1175. }
  1176. host->clock_cache = ios->clock;
  1177. spin_lock_irqsave(&host->lock, flags);
  1178. mmci_set_clkreg(host, ios->clock);
  1179. mmci_write_pwrreg(host, pwr);
  1180. mmci_reg_delay(host);
  1181. spin_unlock_irqrestore(&host->lock, flags);
  1182. pm_runtime_mark_last_busy(mmc_dev(mmc));
  1183. pm_runtime_put_autosuspend(mmc_dev(mmc));
  1184. }
  1185. static int mmci_get_cd(struct mmc_host *mmc)
  1186. {
  1187. struct mmci_host *host = mmc_priv(mmc);
  1188. struct mmci_platform_data *plat = host->plat;
  1189. unsigned int status = mmc_gpio_get_cd(mmc);
  1190. if (status == -ENOSYS) {
  1191. if (!plat->status)
  1192. return 1; /* Assume always present */
  1193. status = plat->status(mmc_dev(host->mmc));
  1194. }
  1195. return status;
  1196. }
  1197. static int mmci_sig_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
  1198. {
  1199. int ret = 0;
  1200. if (!IS_ERR(mmc->supply.vqmmc)) {
  1201. pm_runtime_get_sync(mmc_dev(mmc));
  1202. switch (ios->signal_voltage) {
  1203. case MMC_SIGNAL_VOLTAGE_330:
  1204. ret = regulator_set_voltage(mmc->supply.vqmmc,
  1205. 2700000, 3600000);
  1206. break;
  1207. case MMC_SIGNAL_VOLTAGE_180:
  1208. ret = regulator_set_voltage(mmc->supply.vqmmc,
  1209. 1700000, 1950000);
  1210. break;
  1211. case MMC_SIGNAL_VOLTAGE_120:
  1212. ret = regulator_set_voltage(mmc->supply.vqmmc,
  1213. 1100000, 1300000);
  1214. break;
  1215. }
  1216. if (ret)
  1217. dev_warn(mmc_dev(mmc), "Voltage switch failed\n");
  1218. pm_runtime_mark_last_busy(mmc_dev(mmc));
  1219. pm_runtime_put_autosuspend(mmc_dev(mmc));
  1220. }
  1221. return ret;
  1222. }
  1223. static struct mmc_host_ops mmci_ops = {
  1224. .request = mmci_request,
  1225. .pre_req = mmci_pre_request,
  1226. .post_req = mmci_post_request,
  1227. .set_ios = mmci_set_ios,
  1228. .get_ro = mmc_gpio_get_ro,
  1229. .get_cd = mmci_get_cd,
  1230. .start_signal_voltage_switch = mmci_sig_volt_switch,
  1231. };
  1232. static int mmci_of_parse(struct device_node *np, struct mmc_host *mmc)
  1233. {
  1234. struct mmci_host *host = mmc_priv(mmc);
  1235. int ret = mmc_of_parse(mmc);
  1236. if (ret)
  1237. return ret;
  1238. if (of_get_property(np, "st,sig-dir-dat0", NULL))
  1239. host->pwr_reg_add |= MCI_ST_DATA0DIREN;
  1240. if (of_get_property(np, "st,sig-dir-dat2", NULL))
  1241. host->pwr_reg_add |= MCI_ST_DATA2DIREN;
  1242. if (of_get_property(np, "st,sig-dir-dat31", NULL))
  1243. host->pwr_reg_add |= MCI_ST_DATA31DIREN;
  1244. if (of_get_property(np, "st,sig-dir-dat74", NULL))
  1245. host->pwr_reg_add |= MCI_ST_DATA74DIREN;
  1246. if (of_get_property(np, "st,sig-dir-cmd", NULL))
  1247. host->pwr_reg_add |= MCI_ST_CMDDIREN;
  1248. if (of_get_property(np, "st,sig-pin-fbclk", NULL))
  1249. host->pwr_reg_add |= MCI_ST_FBCLKEN;
  1250. if (of_get_property(np, "mmc-cap-mmc-highspeed", NULL))
  1251. mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
  1252. if (of_get_property(np, "mmc-cap-sd-highspeed", NULL))
  1253. mmc->caps |= MMC_CAP_SD_HIGHSPEED;
  1254. return 0;
  1255. }
  1256. static int mmci_probe(struct amba_device *dev,
  1257. const struct amba_id *id)
  1258. {
  1259. struct mmci_platform_data *plat = dev->dev.platform_data;
  1260. struct device_node *np = dev->dev.of_node;
  1261. struct variant_data *variant = id->data;
  1262. struct mmci_host *host;
  1263. struct mmc_host *mmc;
  1264. int ret;
  1265. /* Must have platform data or Device Tree. */
  1266. if (!plat && !np) {
  1267. dev_err(&dev->dev, "No plat data or DT found\n");
  1268. return -EINVAL;
  1269. }
  1270. if (!plat) {
  1271. plat = devm_kzalloc(&dev->dev, sizeof(*plat), GFP_KERNEL);
  1272. if (!plat)
  1273. return -ENOMEM;
  1274. }
  1275. mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
  1276. if (!mmc)
  1277. return -ENOMEM;
  1278. ret = mmci_of_parse(np, mmc);
  1279. if (ret)
  1280. goto host_free;
  1281. host = mmc_priv(mmc);
  1282. host->mmc = mmc;
  1283. host->hw_designer = amba_manf(dev);
  1284. host->hw_revision = amba_rev(dev);
  1285. dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
  1286. dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
  1287. host->clk = devm_clk_get(&dev->dev, NULL);
  1288. if (IS_ERR(host->clk)) {
  1289. ret = PTR_ERR(host->clk);
  1290. goto host_free;
  1291. }
  1292. ret = clk_prepare_enable(host->clk);
  1293. if (ret)
  1294. goto host_free;
  1295. if (variant->qcom_fifo)
  1296. host->get_rx_fifocnt = mmci_qcom_get_rx_fifocnt;
  1297. else
  1298. host->get_rx_fifocnt = mmci_get_rx_fifocnt;
  1299. host->plat = plat;
  1300. host->variant = variant;
  1301. host->mclk = clk_get_rate(host->clk);
  1302. /*
  1303. * According to the spec, mclk is max 100 MHz,
  1304. * so we try to adjust the clock down to this,
  1305. * (if possible).
  1306. */
  1307. if (host->mclk > variant->f_max) {
  1308. ret = clk_set_rate(host->clk, variant->f_max);
  1309. if (ret < 0)
  1310. goto clk_disable;
  1311. host->mclk = clk_get_rate(host->clk);
  1312. dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
  1313. host->mclk);
  1314. }
  1315. host->phybase = dev->res.start;
  1316. host->base = devm_ioremap_resource(&dev->dev, &dev->res);
  1317. if (IS_ERR(host->base)) {
  1318. ret = PTR_ERR(host->base);
  1319. goto clk_disable;
  1320. }
  1321. /*
  1322. * The ARM and ST versions of the block have slightly different
  1323. * clock divider equations which means that the minimum divider
  1324. * differs too.
  1325. * on Qualcomm like controllers get the nearest minimum clock to 100Khz
  1326. */
  1327. if (variant->st_clkdiv)
  1328. mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
  1329. else if (variant->explicit_mclk_control)
  1330. mmc->f_min = clk_round_rate(host->clk, 100000);
  1331. else
  1332. mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
  1333. /*
  1334. * If no maximum operating frequency is supplied, fall back to use
  1335. * the module parameter, which has a (low) default value in case it
  1336. * is not specified. Either value must not exceed the clock rate into
  1337. * the block, of course.
  1338. */
  1339. if (mmc->f_max)
  1340. mmc->f_max = variant->explicit_mclk_control ?
  1341. min(variant->f_max, mmc->f_max) :
  1342. min(host->mclk, mmc->f_max);
  1343. else
  1344. mmc->f_max = variant->explicit_mclk_control ?
  1345. fmax : min(host->mclk, fmax);
  1346. dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
  1347. /* Get regulators and the supported OCR mask */
  1348. ret = mmc_regulator_get_supply(mmc);
  1349. if (ret == -EPROBE_DEFER)
  1350. goto clk_disable;
  1351. if (!mmc->ocr_avail)
  1352. mmc->ocr_avail = plat->ocr_mask;
  1353. else if (plat->ocr_mask)
  1354. dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
  1355. /* DT takes precedence over platform data. */
  1356. if (!np) {
  1357. if (!plat->cd_invert)
  1358. mmc->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
  1359. mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
  1360. }
  1361. /* We support these capabilities. */
  1362. mmc->caps |= MMC_CAP_CMD23;
  1363. if (variant->busy_detect) {
  1364. mmci_ops.card_busy = mmci_card_busy;
  1365. mmci_write_datactrlreg(host, MCI_ST_DPSM_BUSYMODE);
  1366. mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
  1367. mmc->max_busy_timeout = 0;
  1368. }
  1369. mmc->ops = &mmci_ops;
  1370. /* We support these PM capabilities. */
  1371. mmc->pm_caps |= MMC_PM_KEEP_POWER;
  1372. /*
  1373. * We can do SGIO
  1374. */
  1375. mmc->max_segs = NR_SG;
  1376. /*
  1377. * Since only a certain number of bits are valid in the data length
  1378. * register, we must ensure that we don't exceed 2^num-1 bytes in a
  1379. * single request.
  1380. */
  1381. mmc->max_req_size = (1 << variant->datalength_bits) - 1;
  1382. /*
  1383. * Set the maximum segment size. Since we aren't doing DMA
  1384. * (yet) we are only limited by the data length register.
  1385. */
  1386. mmc->max_seg_size = mmc->max_req_size;
  1387. /*
  1388. * Block size can be up to 2048 bytes, but must be a power of two.
  1389. */
  1390. mmc->max_blk_size = 1 << 11;
  1391. /*
  1392. * Limit the number of blocks transferred so that we don't overflow
  1393. * the maximum request size.
  1394. */
  1395. mmc->max_blk_count = mmc->max_req_size >> 11;
  1396. spin_lock_init(&host->lock);
  1397. writel(0, host->base + MMCIMASK0);
  1398. writel(0, host->base + MMCIMASK1);
  1399. writel(0xfff, host->base + MMCICLEAR);
  1400. /*
  1401. * If:
  1402. * - not using DT but using a descriptor table, or
  1403. * - using a table of descriptors ALONGSIDE DT, or
  1404. * look up these descriptors named "cd" and "wp" right here, fail
  1405. * silently of these do not exist and proceed to try platform data
  1406. */
  1407. if (!np) {
  1408. ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0, NULL);
  1409. if (ret < 0) {
  1410. if (ret == -EPROBE_DEFER)
  1411. goto clk_disable;
  1412. else if (gpio_is_valid(plat->gpio_cd)) {
  1413. ret = mmc_gpio_request_cd(mmc, plat->gpio_cd, 0);
  1414. if (ret)
  1415. goto clk_disable;
  1416. }
  1417. }
  1418. ret = mmc_gpiod_request_ro(mmc, "wp", 0, false, 0, NULL);
  1419. if (ret < 0) {
  1420. if (ret == -EPROBE_DEFER)
  1421. goto clk_disable;
  1422. else if (gpio_is_valid(plat->gpio_wp)) {
  1423. ret = mmc_gpio_request_ro(mmc, plat->gpio_wp);
  1424. if (ret)
  1425. goto clk_disable;
  1426. }
  1427. }
  1428. }
  1429. ret = devm_request_irq(&dev->dev, dev->irq[0], mmci_irq, IRQF_SHARED,
  1430. DRIVER_NAME " (cmd)", host);
  1431. if (ret)
  1432. goto clk_disable;
  1433. if (!dev->irq[1])
  1434. host->singleirq = true;
  1435. else {
  1436. ret = devm_request_irq(&dev->dev, dev->irq[1], mmci_pio_irq,
  1437. IRQF_SHARED, DRIVER_NAME " (pio)", host);
  1438. if (ret)
  1439. goto clk_disable;
  1440. }
  1441. writel(MCI_IRQENABLE, host->base + MMCIMASK0);
  1442. amba_set_drvdata(dev, mmc);
  1443. dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
  1444. mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
  1445. amba_rev(dev), (unsigned long long)dev->res.start,
  1446. dev->irq[0], dev->irq[1]);
  1447. mmci_dma_setup(host);
  1448. pm_runtime_set_autosuspend_delay(&dev->dev, 50);
  1449. pm_runtime_use_autosuspend(&dev->dev);
  1450. mmc_add_host(mmc);
  1451. pm_runtime_put(&dev->dev);
  1452. return 0;
  1453. clk_disable:
  1454. clk_disable_unprepare(host->clk);
  1455. host_free:
  1456. mmc_free_host(mmc);
  1457. return ret;
  1458. }
  1459. static int mmci_remove(struct amba_device *dev)
  1460. {
  1461. struct mmc_host *mmc = amba_get_drvdata(dev);
  1462. if (mmc) {
  1463. struct mmci_host *host = mmc_priv(mmc);
  1464. /*
  1465. * Undo pm_runtime_put() in probe. We use the _sync
  1466. * version here so that we can access the primecell.
  1467. */
  1468. pm_runtime_get_sync(&dev->dev);
  1469. mmc_remove_host(mmc);
  1470. writel(0, host->base + MMCIMASK0);
  1471. writel(0, host->base + MMCIMASK1);
  1472. writel(0, host->base + MMCICOMMAND);
  1473. writel(0, host->base + MMCIDATACTRL);
  1474. mmci_dma_release(host);
  1475. clk_disable_unprepare(host->clk);
  1476. mmc_free_host(mmc);
  1477. }
  1478. return 0;
  1479. }
  1480. #ifdef CONFIG_PM
  1481. static void mmci_save(struct mmci_host *host)
  1482. {
  1483. unsigned long flags;
  1484. spin_lock_irqsave(&host->lock, flags);
  1485. writel(0, host->base + MMCIMASK0);
  1486. if (host->variant->pwrreg_nopower) {
  1487. writel(0, host->base + MMCIDATACTRL);
  1488. writel(0, host->base + MMCIPOWER);
  1489. writel(0, host->base + MMCICLOCK);
  1490. }
  1491. mmci_reg_delay(host);
  1492. spin_unlock_irqrestore(&host->lock, flags);
  1493. }
  1494. static void mmci_restore(struct mmci_host *host)
  1495. {
  1496. unsigned long flags;
  1497. spin_lock_irqsave(&host->lock, flags);
  1498. if (host->variant->pwrreg_nopower) {
  1499. writel(host->clk_reg, host->base + MMCICLOCK);
  1500. writel(host->datactrl_reg, host->base + MMCIDATACTRL);
  1501. writel(host->pwr_reg, host->base + MMCIPOWER);
  1502. }
  1503. writel(MCI_IRQENABLE, host->base + MMCIMASK0);
  1504. mmci_reg_delay(host);
  1505. spin_unlock_irqrestore(&host->lock, flags);
  1506. }
  1507. static int mmci_runtime_suspend(struct device *dev)
  1508. {
  1509. struct amba_device *adev = to_amba_device(dev);
  1510. struct mmc_host *mmc = amba_get_drvdata(adev);
  1511. if (mmc) {
  1512. struct mmci_host *host = mmc_priv(mmc);
  1513. pinctrl_pm_select_sleep_state(dev);
  1514. mmci_save(host);
  1515. clk_disable_unprepare(host->clk);
  1516. }
  1517. return 0;
  1518. }
  1519. static int mmci_runtime_resume(struct device *dev)
  1520. {
  1521. struct amba_device *adev = to_amba_device(dev);
  1522. struct mmc_host *mmc = amba_get_drvdata(adev);
  1523. if (mmc) {
  1524. struct mmci_host *host = mmc_priv(mmc);
  1525. clk_prepare_enable(host->clk);
  1526. mmci_restore(host);
  1527. pinctrl_pm_select_default_state(dev);
  1528. }
  1529. return 0;
  1530. }
  1531. #endif
  1532. static const struct dev_pm_ops mmci_dev_pm_ops = {
  1533. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1534. pm_runtime_force_resume)
  1535. SET_RUNTIME_PM_OPS(mmci_runtime_suspend, mmci_runtime_resume, NULL)
  1536. };
  1537. static struct amba_id mmci_ids[] = {
  1538. {
  1539. .id = 0x00041180,
  1540. .mask = 0xff0fffff,
  1541. .data = &variant_arm,
  1542. },
  1543. {
  1544. .id = 0x01041180,
  1545. .mask = 0xff0fffff,
  1546. .data = &variant_arm_extended_fifo,
  1547. },
  1548. {
  1549. .id = 0x02041180,
  1550. .mask = 0xff0fffff,
  1551. .data = &variant_arm_extended_fifo_hwfc,
  1552. },
  1553. {
  1554. .id = 0x00041181,
  1555. .mask = 0x000fffff,
  1556. .data = &variant_arm,
  1557. },
  1558. /* ST Micro variants */
  1559. {
  1560. .id = 0x00180180,
  1561. .mask = 0x00ffffff,
  1562. .data = &variant_u300,
  1563. },
  1564. {
  1565. .id = 0x10180180,
  1566. .mask = 0xf0ffffff,
  1567. .data = &variant_nomadik,
  1568. },
  1569. {
  1570. .id = 0x00280180,
  1571. .mask = 0x00ffffff,
  1572. .data = &variant_u300,
  1573. },
  1574. {
  1575. .id = 0x00480180,
  1576. .mask = 0xf0ffffff,
  1577. .data = &variant_ux500,
  1578. },
  1579. {
  1580. .id = 0x10480180,
  1581. .mask = 0xf0ffffff,
  1582. .data = &variant_ux500v2,
  1583. },
  1584. /* Qualcomm variants */
  1585. {
  1586. .id = 0x00051180,
  1587. .mask = 0x000fffff,
  1588. .data = &variant_qcom,
  1589. },
  1590. { 0, 0 },
  1591. };
  1592. MODULE_DEVICE_TABLE(amba, mmci_ids);
  1593. static struct amba_driver mmci_driver = {
  1594. .drv = {
  1595. .name = DRIVER_NAME,
  1596. .pm = &mmci_dev_pm_ops,
  1597. },
  1598. .probe = mmci_probe,
  1599. .remove = mmci_remove,
  1600. .id_table = mmci_ids,
  1601. };
  1602. module_amba_driver(mmci_driver);
  1603. module_param(fmax, uint, 0444);
  1604. MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
  1605. MODULE_LICENSE("GPL");