mmci.c 44 KB

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