mmci.c 50 KB

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