mmci.c 48 KB

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