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