mmci.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  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/interrupt.h>
  17. #include <linux/kernel.h>
  18. #include <linux/delay.h>
  19. #include <linux/err.h>
  20. #include <linux/highmem.h>
  21. #include <linux/log2.h>
  22. #include <linux/mmc/host.h>
  23. #include <linux/mmc/card.h>
  24. #include <linux/amba/bus.h>
  25. #include <linux/clk.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/gpio.h>
  28. #include <linux/regulator/consumer.h>
  29. #include <linux/dmaengine.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/amba/mmci.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/types.h>
  34. #include <asm/div64.h>
  35. #include <asm/io.h>
  36. #include <asm/sizes.h>
  37. #include "mmci.h"
  38. #define DRIVER_NAME "mmci-pl18x"
  39. static unsigned int fmax = 515633;
  40. /**
  41. * struct variant_data - MMCI variant-specific quirks
  42. * @clkreg: default value for MCICLOCK register
  43. * @clkreg_enable: enable value for MMCICLOCK register
  44. * @datalength_bits: number of bits in the MMCIDATALENGTH register
  45. * @fifosize: number of bytes that can be written when MMCI_TXFIFOEMPTY
  46. * is asserted (likewise for RX)
  47. * @fifohalfsize: number of bytes that can be written when MCI_TXFIFOHALFEMPTY
  48. * is asserted (likewise for RX)
  49. * @sdio: variant supports SDIO
  50. * @st_clkdiv: true if using a ST-specific clock divider algorithm
  51. * @blksz_datactrl16: true if Block size is at b16..b30 position in datactrl register
  52. */
  53. struct variant_data {
  54. unsigned int clkreg;
  55. unsigned int clkreg_enable;
  56. unsigned int datalength_bits;
  57. unsigned int fifosize;
  58. unsigned int fifohalfsize;
  59. bool sdio;
  60. bool st_clkdiv;
  61. bool blksz_datactrl16;
  62. };
  63. static struct variant_data variant_arm = {
  64. .fifosize = 16 * 4,
  65. .fifohalfsize = 8 * 4,
  66. .datalength_bits = 16,
  67. };
  68. static struct variant_data variant_arm_extended_fifo = {
  69. .fifosize = 128 * 4,
  70. .fifohalfsize = 64 * 4,
  71. .datalength_bits = 16,
  72. };
  73. static struct variant_data variant_u300 = {
  74. .fifosize = 16 * 4,
  75. .fifohalfsize = 8 * 4,
  76. .clkreg_enable = MCI_ST_U300_HWFCEN,
  77. .datalength_bits = 16,
  78. .sdio = true,
  79. };
  80. static struct variant_data variant_ux500 = {
  81. .fifosize = 30 * 4,
  82. .fifohalfsize = 8 * 4,
  83. .clkreg = MCI_CLK_ENABLE,
  84. .clkreg_enable = MCI_ST_UX500_HWFCEN,
  85. .datalength_bits = 24,
  86. .sdio = true,
  87. .st_clkdiv = true,
  88. };
  89. static struct variant_data variant_ux500v2 = {
  90. .fifosize = 30 * 4,
  91. .fifohalfsize = 8 * 4,
  92. .clkreg = MCI_CLK_ENABLE,
  93. .clkreg_enable = MCI_ST_UX500_HWFCEN,
  94. .datalength_bits = 24,
  95. .sdio = true,
  96. .st_clkdiv = true,
  97. .blksz_datactrl16 = true,
  98. };
  99. /*
  100. * This must be called with host->lock held
  101. */
  102. static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
  103. {
  104. struct variant_data *variant = host->variant;
  105. u32 clk = variant->clkreg;
  106. if (desired) {
  107. if (desired >= host->mclk) {
  108. clk = MCI_CLK_BYPASS;
  109. if (variant->st_clkdiv)
  110. clk |= MCI_ST_UX500_NEG_EDGE;
  111. host->cclk = host->mclk;
  112. } else if (variant->st_clkdiv) {
  113. /*
  114. * DB8500 TRM says f = mclk / (clkdiv + 2)
  115. * => clkdiv = (mclk / f) - 2
  116. * Round the divider up so we don't exceed the max
  117. * frequency
  118. */
  119. clk = DIV_ROUND_UP(host->mclk, desired) - 2;
  120. if (clk >= 256)
  121. clk = 255;
  122. host->cclk = host->mclk / (clk + 2);
  123. } else {
  124. /*
  125. * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
  126. * => clkdiv = mclk / (2 * f) - 1
  127. */
  128. clk = host->mclk / (2 * desired) - 1;
  129. if (clk >= 256)
  130. clk = 255;
  131. host->cclk = host->mclk / (2 * (clk + 1));
  132. }
  133. clk |= variant->clkreg_enable;
  134. clk |= MCI_CLK_ENABLE;
  135. /* This hasn't proven to be worthwhile */
  136. /* clk |= MCI_CLK_PWRSAVE; */
  137. }
  138. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
  139. clk |= MCI_4BIT_BUS;
  140. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
  141. clk |= MCI_ST_8BIT_BUS;
  142. writel(clk, host->base + MMCICLOCK);
  143. }
  144. static void
  145. mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
  146. {
  147. writel(0, host->base + MMCICOMMAND);
  148. BUG_ON(host->data);
  149. host->mrq = NULL;
  150. host->cmd = NULL;
  151. /*
  152. * Need to drop the host lock here; mmc_request_done may call
  153. * back into the driver...
  154. */
  155. spin_unlock(&host->lock);
  156. pm_runtime_put(mmc_dev(host->mmc));
  157. mmc_request_done(host->mmc, mrq);
  158. spin_lock(&host->lock);
  159. }
  160. static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
  161. {
  162. void __iomem *base = host->base;
  163. if (host->singleirq) {
  164. unsigned int mask0 = readl(base + MMCIMASK0);
  165. mask0 &= ~MCI_IRQ1MASK;
  166. mask0 |= mask;
  167. writel(mask0, base + MMCIMASK0);
  168. }
  169. writel(mask, base + MMCIMASK1);
  170. }
  171. static void mmci_stop_data(struct mmci_host *host)
  172. {
  173. writel(0, host->base + MMCIDATACTRL);
  174. mmci_set_mask1(host, 0);
  175. host->data = NULL;
  176. }
  177. static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
  178. {
  179. unsigned int flags = SG_MITER_ATOMIC;
  180. if (data->flags & MMC_DATA_READ)
  181. flags |= SG_MITER_TO_SG;
  182. else
  183. flags |= SG_MITER_FROM_SG;
  184. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  185. }
  186. /*
  187. * All the DMA operation mode stuff goes inside this ifdef.
  188. * This assumes that you have a generic DMA device interface,
  189. * no custom DMA interfaces are supported.
  190. */
  191. #ifdef CONFIG_DMA_ENGINE
  192. static void __devinit mmci_dma_setup(struct mmci_host *host)
  193. {
  194. struct mmci_platform_data *plat = host->plat;
  195. const char *rxname, *txname;
  196. dma_cap_mask_t mask;
  197. if (!plat || !plat->dma_filter) {
  198. dev_info(mmc_dev(host->mmc), "no DMA platform data\n");
  199. return;
  200. }
  201. /* initialize pre request cookie */
  202. host->next_data.cookie = 1;
  203. /* Try to acquire a generic DMA engine slave channel */
  204. dma_cap_zero(mask);
  205. dma_cap_set(DMA_SLAVE, mask);
  206. /*
  207. * If only an RX channel is specified, the driver will
  208. * attempt to use it bidirectionally, however if it is
  209. * is specified but cannot be located, DMA will be disabled.
  210. */
  211. if (plat->dma_rx_param) {
  212. host->dma_rx_channel = dma_request_channel(mask,
  213. plat->dma_filter,
  214. plat->dma_rx_param);
  215. /* E.g if no DMA hardware is present */
  216. if (!host->dma_rx_channel)
  217. dev_err(mmc_dev(host->mmc), "no RX DMA channel\n");
  218. }
  219. if (plat->dma_tx_param) {
  220. host->dma_tx_channel = dma_request_channel(mask,
  221. plat->dma_filter,
  222. plat->dma_tx_param);
  223. if (!host->dma_tx_channel)
  224. dev_warn(mmc_dev(host->mmc), "no TX DMA channel\n");
  225. } else {
  226. host->dma_tx_channel = host->dma_rx_channel;
  227. }
  228. if (host->dma_rx_channel)
  229. rxname = dma_chan_name(host->dma_rx_channel);
  230. else
  231. rxname = "none";
  232. if (host->dma_tx_channel)
  233. txname = dma_chan_name(host->dma_tx_channel);
  234. else
  235. txname = "none";
  236. dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
  237. rxname, txname);
  238. /*
  239. * Limit the maximum segment size in any SG entry according to
  240. * the parameters of the DMA engine device.
  241. */
  242. if (host->dma_tx_channel) {
  243. struct device *dev = host->dma_tx_channel->device->dev;
  244. unsigned int max_seg_size = dma_get_max_seg_size(dev);
  245. if (max_seg_size < host->mmc->max_seg_size)
  246. host->mmc->max_seg_size = max_seg_size;
  247. }
  248. if (host->dma_rx_channel) {
  249. struct device *dev = host->dma_rx_channel->device->dev;
  250. unsigned int max_seg_size = dma_get_max_seg_size(dev);
  251. if (max_seg_size < host->mmc->max_seg_size)
  252. host->mmc->max_seg_size = max_seg_size;
  253. }
  254. }
  255. /*
  256. * This is used in __devinit or __devexit so inline it
  257. * so it can be discarded.
  258. */
  259. static inline void mmci_dma_release(struct mmci_host *host)
  260. {
  261. struct mmci_platform_data *plat = host->plat;
  262. if (host->dma_rx_channel)
  263. dma_release_channel(host->dma_rx_channel);
  264. if (host->dma_tx_channel && plat->dma_tx_param)
  265. dma_release_channel(host->dma_tx_channel);
  266. host->dma_rx_channel = host->dma_tx_channel = NULL;
  267. }
  268. static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
  269. {
  270. struct dma_chan *chan = host->dma_current;
  271. enum dma_data_direction dir;
  272. u32 status;
  273. int i;
  274. /* Wait up to 1ms for the DMA to complete */
  275. for (i = 0; ; i++) {
  276. status = readl(host->base + MMCISTATUS);
  277. if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
  278. break;
  279. udelay(10);
  280. }
  281. /*
  282. * Check to see whether we still have some data left in the FIFO -
  283. * this catches DMA controllers which are unable to monitor the
  284. * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
  285. * contiguous buffers. On TX, we'll get a FIFO underrun error.
  286. */
  287. if (status & MCI_RXDATAAVLBLMASK) {
  288. dmaengine_terminate_all(chan);
  289. if (!data->error)
  290. data->error = -EIO;
  291. }
  292. if (data->flags & MMC_DATA_WRITE) {
  293. dir = DMA_TO_DEVICE;
  294. } else {
  295. dir = DMA_FROM_DEVICE;
  296. }
  297. if (!data->host_cookie)
  298. dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, dir);
  299. /*
  300. * Use of DMA with scatter-gather is impossible.
  301. * Give up with DMA and switch back to PIO mode.
  302. */
  303. if (status & MCI_RXDATAAVLBLMASK) {
  304. dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
  305. mmci_dma_release(host);
  306. }
  307. }
  308. static void mmci_dma_data_error(struct mmci_host *host)
  309. {
  310. dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
  311. dmaengine_terminate_all(host->dma_current);
  312. }
  313. static int mmci_dma_prep_data(struct mmci_host *host, struct mmc_data *data,
  314. struct mmci_host_next *next)
  315. {
  316. struct variant_data *variant = host->variant;
  317. struct dma_slave_config conf = {
  318. .src_addr = host->phybase + MMCIFIFO,
  319. .dst_addr = host->phybase + MMCIFIFO,
  320. .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  321. .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  322. .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
  323. .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
  324. .device_fc = false,
  325. };
  326. struct dma_chan *chan;
  327. struct dma_device *device;
  328. struct dma_async_tx_descriptor *desc;
  329. enum dma_data_direction buffer_dirn;
  330. int nr_sg;
  331. /* Check if next job is already prepared */
  332. if (data->host_cookie && !next &&
  333. host->dma_current && host->dma_desc_current)
  334. return 0;
  335. if (!next) {
  336. host->dma_current = NULL;
  337. host->dma_desc_current = NULL;
  338. }
  339. if (data->flags & MMC_DATA_READ) {
  340. conf.direction = DMA_DEV_TO_MEM;
  341. buffer_dirn = DMA_FROM_DEVICE;
  342. chan = host->dma_rx_channel;
  343. } else {
  344. conf.direction = DMA_MEM_TO_DEV;
  345. buffer_dirn = DMA_TO_DEVICE;
  346. chan = host->dma_tx_channel;
  347. }
  348. /* If there's no DMA channel, fall back to PIO */
  349. if (!chan)
  350. return -EINVAL;
  351. /* If less than or equal to the fifo size, don't bother with DMA */
  352. if (data->blksz * data->blocks <= variant->fifosize)
  353. return -EINVAL;
  354. device = chan->device;
  355. nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len, buffer_dirn);
  356. if (nr_sg == 0)
  357. return -EINVAL;
  358. dmaengine_slave_config(chan, &conf);
  359. desc = dmaengine_prep_slave_sg(chan, data->sg, nr_sg,
  360. conf.direction, DMA_CTRL_ACK);
  361. if (!desc)
  362. goto unmap_exit;
  363. if (next) {
  364. next->dma_chan = chan;
  365. next->dma_desc = desc;
  366. } else {
  367. host->dma_current = chan;
  368. host->dma_desc_current = desc;
  369. }
  370. return 0;
  371. unmap_exit:
  372. if (!next)
  373. dmaengine_terminate_all(chan);
  374. dma_unmap_sg(device->dev, data->sg, data->sg_len, buffer_dirn);
  375. return -ENOMEM;
  376. }
  377. static int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
  378. {
  379. int ret;
  380. struct mmc_data *data = host->data;
  381. ret = mmci_dma_prep_data(host, host->data, NULL);
  382. if (ret)
  383. return ret;
  384. /* Okay, go for it. */
  385. dev_vdbg(mmc_dev(host->mmc),
  386. "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
  387. data->sg_len, data->blksz, data->blocks, data->flags);
  388. dmaengine_submit(host->dma_desc_current);
  389. dma_async_issue_pending(host->dma_current);
  390. datactrl |= MCI_DPSM_DMAENABLE;
  391. /* Trigger the DMA transfer */
  392. writel(datactrl, host->base + MMCIDATACTRL);
  393. /*
  394. * Let the MMCI say when the data is ended and it's time
  395. * to fire next DMA request. When that happens, MMCI will
  396. * call mmci_data_end()
  397. */
  398. writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
  399. host->base + MMCIMASK0);
  400. return 0;
  401. }
  402. static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
  403. {
  404. struct mmci_host_next *next = &host->next_data;
  405. if (data->host_cookie && data->host_cookie != next->cookie) {
  406. pr_warning("[%s] invalid cookie: data->host_cookie %d"
  407. " host->next_data.cookie %d\n",
  408. __func__, data->host_cookie, host->next_data.cookie);
  409. data->host_cookie = 0;
  410. }
  411. if (!data->host_cookie)
  412. return;
  413. host->dma_desc_current = next->dma_desc;
  414. host->dma_current = next->dma_chan;
  415. next->dma_desc = NULL;
  416. next->dma_chan = NULL;
  417. }
  418. static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq,
  419. bool is_first_req)
  420. {
  421. struct mmci_host *host = mmc_priv(mmc);
  422. struct mmc_data *data = mrq->data;
  423. struct mmci_host_next *nd = &host->next_data;
  424. if (!data)
  425. return;
  426. if (data->host_cookie) {
  427. data->host_cookie = 0;
  428. return;
  429. }
  430. /* if config for dma */
  431. if (((data->flags & MMC_DATA_WRITE) && host->dma_tx_channel) ||
  432. ((data->flags & MMC_DATA_READ) && host->dma_rx_channel)) {
  433. if (mmci_dma_prep_data(host, data, nd))
  434. data->host_cookie = 0;
  435. else
  436. data->host_cookie = ++nd->cookie < 0 ? 1 : nd->cookie;
  437. }
  438. }
  439. static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
  440. int err)
  441. {
  442. struct mmci_host *host = mmc_priv(mmc);
  443. struct mmc_data *data = mrq->data;
  444. struct dma_chan *chan;
  445. enum dma_data_direction dir;
  446. if (!data)
  447. return;
  448. if (data->flags & MMC_DATA_READ) {
  449. dir = DMA_FROM_DEVICE;
  450. chan = host->dma_rx_channel;
  451. } else {
  452. dir = DMA_TO_DEVICE;
  453. chan = host->dma_tx_channel;
  454. }
  455. /* if config for dma */
  456. if (chan) {
  457. if (err)
  458. dmaengine_terminate_all(chan);
  459. if (data->host_cookie)
  460. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  461. data->sg_len, dir);
  462. mrq->data->host_cookie = 0;
  463. }
  464. }
  465. #else
  466. /* Blank functions if the DMA engine is not available */
  467. static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
  468. {
  469. }
  470. static inline void mmci_dma_setup(struct mmci_host *host)
  471. {
  472. }
  473. static inline void mmci_dma_release(struct mmci_host *host)
  474. {
  475. }
  476. static inline void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
  477. {
  478. }
  479. static inline void mmci_dma_data_error(struct mmci_host *host)
  480. {
  481. }
  482. static inline int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
  483. {
  484. return -ENOSYS;
  485. }
  486. #define mmci_pre_request NULL
  487. #define mmci_post_request NULL
  488. #endif
  489. static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
  490. {
  491. struct variant_data *variant = host->variant;
  492. unsigned int datactrl, timeout, irqmask;
  493. unsigned long long clks;
  494. void __iomem *base;
  495. int blksz_bits;
  496. dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
  497. data->blksz, data->blocks, data->flags);
  498. host->data = data;
  499. host->size = data->blksz * data->blocks;
  500. data->bytes_xfered = 0;
  501. clks = (unsigned long long)data->timeout_ns * host->cclk;
  502. do_div(clks, 1000000000UL);
  503. timeout = data->timeout_clks + (unsigned int)clks;
  504. base = host->base;
  505. writel(timeout, base + MMCIDATATIMER);
  506. writel(host->size, base + MMCIDATALENGTH);
  507. blksz_bits = ffs(data->blksz) - 1;
  508. BUG_ON(1 << blksz_bits != data->blksz);
  509. if (variant->blksz_datactrl16)
  510. datactrl = MCI_DPSM_ENABLE | (data->blksz << 16);
  511. else
  512. datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
  513. if (data->flags & MMC_DATA_READ)
  514. datactrl |= MCI_DPSM_DIRECTION;
  515. /*
  516. * Attempt to use DMA operation mode, if this
  517. * should fail, fall back to PIO mode
  518. */
  519. if (!mmci_dma_start_data(host, datactrl))
  520. return;
  521. /* IRQ mode, map the SG list for CPU reading/writing */
  522. mmci_init_sg(host, data);
  523. if (data->flags & MMC_DATA_READ) {
  524. irqmask = MCI_RXFIFOHALFFULLMASK;
  525. /*
  526. * If we have less than the fifo 'half-full' threshold to
  527. * transfer, trigger a PIO interrupt as soon as any data
  528. * is available.
  529. */
  530. if (host->size < variant->fifohalfsize)
  531. irqmask |= MCI_RXDATAAVLBLMASK;
  532. } else {
  533. /*
  534. * We don't actually need to include "FIFO empty" here
  535. * since its implicit in "FIFO half empty".
  536. */
  537. irqmask = MCI_TXFIFOHALFEMPTYMASK;
  538. }
  539. /* The ST Micro variants has a special bit to enable SDIO */
  540. if (variant->sdio && host->mmc->card)
  541. if (mmc_card_sdio(host->mmc->card))
  542. datactrl |= MCI_ST_DPSM_SDIOEN;
  543. writel(datactrl, base + MMCIDATACTRL);
  544. writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
  545. mmci_set_mask1(host, irqmask);
  546. }
  547. static void
  548. mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
  549. {
  550. void __iomem *base = host->base;
  551. dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
  552. cmd->opcode, cmd->arg, cmd->flags);
  553. if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
  554. writel(0, base + MMCICOMMAND);
  555. udelay(1);
  556. }
  557. c |= cmd->opcode | MCI_CPSM_ENABLE;
  558. if (cmd->flags & MMC_RSP_PRESENT) {
  559. if (cmd->flags & MMC_RSP_136)
  560. c |= MCI_CPSM_LONGRSP;
  561. c |= MCI_CPSM_RESPONSE;
  562. }
  563. if (/*interrupt*/0)
  564. c |= MCI_CPSM_INTERRUPT;
  565. host->cmd = cmd;
  566. writel(cmd->arg, base + MMCIARGUMENT);
  567. writel(c, base + MMCICOMMAND);
  568. }
  569. static void
  570. mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
  571. unsigned int status)
  572. {
  573. /* First check for errors */
  574. if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_STARTBITERR|
  575. MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
  576. u32 remain, success;
  577. /* Terminate the DMA transfer */
  578. if (dma_inprogress(host))
  579. mmci_dma_data_error(host);
  580. /*
  581. * Calculate how far we are into the transfer. Note that
  582. * the data counter gives the number of bytes transferred
  583. * on the MMC bus, not on the host side. On reads, this
  584. * can be as much as a FIFO-worth of data ahead. This
  585. * matters for FIFO overruns only.
  586. */
  587. remain = readl(host->base + MMCIDATACNT);
  588. success = data->blksz * data->blocks - remain;
  589. dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
  590. status, success);
  591. if (status & MCI_DATACRCFAIL) {
  592. /* Last block was not successful */
  593. success -= 1;
  594. data->error = -EILSEQ;
  595. } else if (status & MCI_DATATIMEOUT) {
  596. data->error = -ETIMEDOUT;
  597. } else if (status & MCI_STARTBITERR) {
  598. data->error = -ECOMM;
  599. } else if (status & MCI_TXUNDERRUN) {
  600. data->error = -EIO;
  601. } else if (status & MCI_RXOVERRUN) {
  602. if (success > host->variant->fifosize)
  603. success -= host->variant->fifosize;
  604. else
  605. success = 0;
  606. data->error = -EIO;
  607. }
  608. data->bytes_xfered = round_down(success, data->blksz);
  609. }
  610. if (status & MCI_DATABLOCKEND)
  611. dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
  612. if (status & MCI_DATAEND || data->error) {
  613. if (dma_inprogress(host))
  614. mmci_dma_unmap(host, data);
  615. mmci_stop_data(host);
  616. if (!data->error)
  617. /* The error clause is handled above, success! */
  618. data->bytes_xfered = data->blksz * data->blocks;
  619. if (!data->stop) {
  620. mmci_request_end(host, data->mrq);
  621. } else {
  622. mmci_start_command(host, data->stop, 0);
  623. }
  624. }
  625. }
  626. static void
  627. mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
  628. unsigned int status)
  629. {
  630. void __iomem *base = host->base;
  631. host->cmd = NULL;
  632. if (status & MCI_CMDTIMEOUT) {
  633. cmd->error = -ETIMEDOUT;
  634. } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
  635. cmd->error = -EILSEQ;
  636. } else {
  637. cmd->resp[0] = readl(base + MMCIRESPONSE0);
  638. cmd->resp[1] = readl(base + MMCIRESPONSE1);
  639. cmd->resp[2] = readl(base + MMCIRESPONSE2);
  640. cmd->resp[3] = readl(base + MMCIRESPONSE3);
  641. }
  642. if (!cmd->data || cmd->error) {
  643. if (host->data) {
  644. /* Terminate the DMA transfer */
  645. if (dma_inprogress(host))
  646. mmci_dma_data_error(host);
  647. mmci_stop_data(host);
  648. }
  649. mmci_request_end(host, cmd->mrq);
  650. } else if (!(cmd->data->flags & MMC_DATA_READ)) {
  651. mmci_start_data(host, cmd->data);
  652. }
  653. }
  654. static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
  655. {
  656. void __iomem *base = host->base;
  657. char *ptr = buffer;
  658. u32 status;
  659. int host_remain = host->size;
  660. do {
  661. int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
  662. if (count > remain)
  663. count = remain;
  664. if (count <= 0)
  665. break;
  666. readsl(base + MMCIFIFO, ptr, count >> 2);
  667. ptr += count;
  668. remain -= count;
  669. host_remain -= count;
  670. if (remain == 0)
  671. break;
  672. status = readl(base + MMCISTATUS);
  673. } while (status & MCI_RXDATAAVLBL);
  674. return ptr - buffer;
  675. }
  676. static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
  677. {
  678. struct variant_data *variant = host->variant;
  679. void __iomem *base = host->base;
  680. char *ptr = buffer;
  681. do {
  682. unsigned int count, maxcnt;
  683. maxcnt = status & MCI_TXFIFOEMPTY ?
  684. variant->fifosize : variant->fifohalfsize;
  685. count = min(remain, maxcnt);
  686. /*
  687. * The ST Micro variant for SDIO transfer sizes
  688. * less then 8 bytes should have clock H/W flow
  689. * control disabled.
  690. */
  691. if (variant->sdio &&
  692. mmc_card_sdio(host->mmc->card)) {
  693. if (count < 8)
  694. writel(readl(host->base + MMCICLOCK) &
  695. ~variant->clkreg_enable,
  696. host->base + MMCICLOCK);
  697. else
  698. writel(readl(host->base + MMCICLOCK) |
  699. variant->clkreg_enable,
  700. host->base + MMCICLOCK);
  701. }
  702. /*
  703. * SDIO especially may want to send something that is
  704. * not divisible by 4 (as opposed to card sectors
  705. * etc), and the FIFO only accept full 32-bit writes.
  706. * So compensate by adding +3 on the count, a single
  707. * byte become a 32bit write, 7 bytes will be two
  708. * 32bit writes etc.
  709. */
  710. writesl(base + MMCIFIFO, ptr, (count + 3) >> 2);
  711. ptr += count;
  712. remain -= count;
  713. if (remain == 0)
  714. break;
  715. status = readl(base + MMCISTATUS);
  716. } while (status & MCI_TXFIFOHALFEMPTY);
  717. return ptr - buffer;
  718. }
  719. /*
  720. * PIO data transfer IRQ handler.
  721. */
  722. static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
  723. {
  724. struct mmci_host *host = dev_id;
  725. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  726. struct variant_data *variant = host->variant;
  727. void __iomem *base = host->base;
  728. unsigned long flags;
  729. u32 status;
  730. status = readl(base + MMCISTATUS);
  731. dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
  732. local_irq_save(flags);
  733. do {
  734. unsigned int remain, len;
  735. char *buffer;
  736. /*
  737. * For write, we only need to test the half-empty flag
  738. * here - if the FIFO is completely empty, then by
  739. * definition it is more than half empty.
  740. *
  741. * For read, check for data available.
  742. */
  743. if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
  744. break;
  745. if (!sg_miter_next(sg_miter))
  746. break;
  747. buffer = sg_miter->addr;
  748. remain = sg_miter->length;
  749. len = 0;
  750. if (status & MCI_RXACTIVE)
  751. len = mmci_pio_read(host, buffer, remain);
  752. if (status & MCI_TXACTIVE)
  753. len = mmci_pio_write(host, buffer, remain, status);
  754. sg_miter->consumed = len;
  755. host->size -= len;
  756. remain -= len;
  757. if (remain)
  758. break;
  759. status = readl(base + MMCISTATUS);
  760. } while (1);
  761. sg_miter_stop(sg_miter);
  762. local_irq_restore(flags);
  763. /*
  764. * If we have less than the fifo 'half-full' threshold to transfer,
  765. * trigger a PIO interrupt as soon as any data is available.
  766. */
  767. if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
  768. mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
  769. /*
  770. * If we run out of data, disable the data IRQs; this
  771. * prevents a race where the FIFO becomes empty before
  772. * the chip itself has disabled the data path, and
  773. * stops us racing with our data end IRQ.
  774. */
  775. if (host->size == 0) {
  776. mmci_set_mask1(host, 0);
  777. writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
  778. }
  779. return IRQ_HANDLED;
  780. }
  781. /*
  782. * Handle completion of command and data transfers.
  783. */
  784. static irqreturn_t mmci_irq(int irq, void *dev_id)
  785. {
  786. struct mmci_host *host = dev_id;
  787. u32 status;
  788. int ret = 0;
  789. spin_lock(&host->lock);
  790. do {
  791. struct mmc_command *cmd;
  792. struct mmc_data *data;
  793. status = readl(host->base + MMCISTATUS);
  794. if (host->singleirq) {
  795. if (status & readl(host->base + MMCIMASK1))
  796. mmci_pio_irq(irq, dev_id);
  797. status &= ~MCI_IRQ1MASK;
  798. }
  799. status &= readl(host->base + MMCIMASK0);
  800. writel(status, host->base + MMCICLEAR);
  801. dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
  802. data = host->data;
  803. if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_STARTBITERR|
  804. MCI_TXUNDERRUN|MCI_RXOVERRUN|MCI_DATAEND|
  805. MCI_DATABLOCKEND) && data)
  806. mmci_data_irq(host, data, status);
  807. cmd = host->cmd;
  808. if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
  809. mmci_cmd_irq(host, cmd, status);
  810. ret = 1;
  811. } while (status);
  812. spin_unlock(&host->lock);
  813. return IRQ_RETVAL(ret);
  814. }
  815. static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  816. {
  817. struct mmci_host *host = mmc_priv(mmc);
  818. unsigned long flags;
  819. WARN_ON(host->mrq != NULL);
  820. if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
  821. dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n",
  822. mrq->data->blksz);
  823. mrq->cmd->error = -EINVAL;
  824. mmc_request_done(mmc, mrq);
  825. return;
  826. }
  827. pm_runtime_get_sync(mmc_dev(mmc));
  828. spin_lock_irqsave(&host->lock, flags);
  829. host->mrq = mrq;
  830. if (mrq->data)
  831. mmci_get_next_data(host, mrq->data);
  832. if (mrq->data && mrq->data->flags & MMC_DATA_READ)
  833. mmci_start_data(host, mrq->data);
  834. mmci_start_command(host, mrq->cmd, 0);
  835. spin_unlock_irqrestore(&host->lock, flags);
  836. }
  837. static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  838. {
  839. struct mmci_host *host = mmc_priv(mmc);
  840. u32 pwr = 0;
  841. unsigned long flags;
  842. int ret;
  843. switch (ios->power_mode) {
  844. case MMC_POWER_OFF:
  845. if (host->vcc)
  846. ret = mmc_regulator_set_ocr(mmc, host->vcc, 0);
  847. break;
  848. case MMC_POWER_UP:
  849. if (host->vcc) {
  850. ret = mmc_regulator_set_ocr(mmc, host->vcc, ios->vdd);
  851. if (ret) {
  852. dev_err(mmc_dev(mmc), "unable to set OCR\n");
  853. /*
  854. * The .set_ios() function in the mmc_host_ops
  855. * struct return void, and failing to set the
  856. * power should be rare so we print an error
  857. * and return here.
  858. */
  859. return;
  860. }
  861. }
  862. if (host->plat->vdd_handler)
  863. pwr |= host->plat->vdd_handler(mmc_dev(mmc), ios->vdd,
  864. ios->power_mode);
  865. /* The ST version does not have this, fall through to POWER_ON */
  866. if (host->hw_designer != AMBA_VENDOR_ST) {
  867. pwr |= MCI_PWR_UP;
  868. break;
  869. }
  870. case MMC_POWER_ON:
  871. pwr |= MCI_PWR_ON;
  872. break;
  873. }
  874. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
  875. if (host->hw_designer != AMBA_VENDOR_ST)
  876. pwr |= MCI_ROD;
  877. else {
  878. /*
  879. * The ST Micro variant use the ROD bit for something
  880. * else and only has OD (Open Drain).
  881. */
  882. pwr |= MCI_OD;
  883. }
  884. }
  885. spin_lock_irqsave(&host->lock, flags);
  886. mmci_set_clkreg(host, ios->clock);
  887. if (host->pwr != pwr) {
  888. host->pwr = pwr;
  889. writel(pwr, host->base + MMCIPOWER);
  890. }
  891. spin_unlock_irqrestore(&host->lock, flags);
  892. }
  893. static int mmci_get_ro(struct mmc_host *mmc)
  894. {
  895. struct mmci_host *host = mmc_priv(mmc);
  896. if (host->gpio_wp == -ENOSYS)
  897. return -ENOSYS;
  898. return gpio_get_value_cansleep(host->gpio_wp);
  899. }
  900. static int mmci_get_cd(struct mmc_host *mmc)
  901. {
  902. struct mmci_host *host = mmc_priv(mmc);
  903. struct mmci_platform_data *plat = host->plat;
  904. unsigned int status;
  905. if (host->gpio_cd == -ENOSYS) {
  906. if (!plat->status)
  907. return 1; /* Assume always present */
  908. status = plat->status(mmc_dev(host->mmc));
  909. } else
  910. status = !!gpio_get_value_cansleep(host->gpio_cd)
  911. ^ plat->cd_invert;
  912. /*
  913. * Use positive logic throughout - status is zero for no card,
  914. * non-zero for card inserted.
  915. */
  916. return status;
  917. }
  918. static irqreturn_t mmci_cd_irq(int irq, void *dev_id)
  919. {
  920. struct mmci_host *host = dev_id;
  921. mmc_detect_change(host->mmc, msecs_to_jiffies(500));
  922. return IRQ_HANDLED;
  923. }
  924. static const struct mmc_host_ops mmci_ops = {
  925. .request = mmci_request,
  926. .pre_req = mmci_pre_request,
  927. .post_req = mmci_post_request,
  928. .set_ios = mmci_set_ios,
  929. .get_ro = mmci_get_ro,
  930. .get_cd = mmci_get_cd,
  931. };
  932. static int __devinit mmci_probe(struct amba_device *dev,
  933. const struct amba_id *id)
  934. {
  935. struct mmci_platform_data *plat = dev->dev.platform_data;
  936. struct variant_data *variant = id->data;
  937. struct mmci_host *host;
  938. struct mmc_host *mmc;
  939. int ret;
  940. /* must have platform data */
  941. if (!plat) {
  942. ret = -EINVAL;
  943. goto out;
  944. }
  945. ret = amba_request_regions(dev, DRIVER_NAME);
  946. if (ret)
  947. goto out;
  948. mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
  949. if (!mmc) {
  950. ret = -ENOMEM;
  951. goto rel_regions;
  952. }
  953. host = mmc_priv(mmc);
  954. host->mmc = mmc;
  955. host->gpio_wp = -ENOSYS;
  956. host->gpio_cd = -ENOSYS;
  957. host->gpio_cd_irq = -1;
  958. host->hw_designer = amba_manf(dev);
  959. host->hw_revision = amba_rev(dev);
  960. dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
  961. dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
  962. host->clk = clk_get(&dev->dev, NULL);
  963. if (IS_ERR(host->clk)) {
  964. ret = PTR_ERR(host->clk);
  965. host->clk = NULL;
  966. goto host_free;
  967. }
  968. ret = clk_prepare(host->clk);
  969. if (ret)
  970. goto clk_free;
  971. ret = clk_enable(host->clk);
  972. if (ret)
  973. goto clk_unprep;
  974. host->plat = plat;
  975. host->variant = variant;
  976. host->mclk = clk_get_rate(host->clk);
  977. /*
  978. * According to the spec, mclk is max 100 MHz,
  979. * so we try to adjust the clock down to this,
  980. * (if possible).
  981. */
  982. if (host->mclk > 100000000) {
  983. ret = clk_set_rate(host->clk, 100000000);
  984. if (ret < 0)
  985. goto clk_disable;
  986. host->mclk = clk_get_rate(host->clk);
  987. dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
  988. host->mclk);
  989. }
  990. host->phybase = dev->res.start;
  991. host->base = ioremap(dev->res.start, resource_size(&dev->res));
  992. if (!host->base) {
  993. ret = -ENOMEM;
  994. goto clk_disable;
  995. }
  996. mmc->ops = &mmci_ops;
  997. /*
  998. * The ARM and ST versions of the block have slightly different
  999. * clock divider equations which means that the minimum divider
  1000. * differs too.
  1001. */
  1002. if (variant->st_clkdiv)
  1003. mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
  1004. else
  1005. mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
  1006. /*
  1007. * If the platform data supplies a maximum operating
  1008. * frequency, this takes precedence. Else, we fall back
  1009. * to using the module parameter, which has a (low)
  1010. * default value in case it is not specified. Either
  1011. * value must not exceed the clock rate into the block,
  1012. * of course.
  1013. */
  1014. if (plat->f_max)
  1015. mmc->f_max = min(host->mclk, plat->f_max);
  1016. else
  1017. mmc->f_max = min(host->mclk, fmax);
  1018. dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
  1019. #ifdef CONFIG_REGULATOR
  1020. /* If we're using the regulator framework, try to fetch a regulator */
  1021. host->vcc = regulator_get(&dev->dev, "vmmc");
  1022. if (IS_ERR(host->vcc))
  1023. host->vcc = NULL;
  1024. else {
  1025. int mask = mmc_regulator_get_ocrmask(host->vcc);
  1026. if (mask < 0)
  1027. dev_err(&dev->dev, "error getting OCR mask (%d)\n",
  1028. mask);
  1029. else {
  1030. host->mmc->ocr_avail = (u32) mask;
  1031. if (plat->ocr_mask)
  1032. dev_warn(&dev->dev,
  1033. "Provided ocr_mask/setpower will not be used "
  1034. "(using regulator instead)\n");
  1035. }
  1036. }
  1037. #endif
  1038. /* Fall back to platform data if no regulator is found */
  1039. if (host->vcc == NULL)
  1040. mmc->ocr_avail = plat->ocr_mask;
  1041. mmc->caps = plat->capabilities;
  1042. mmc->caps2 = plat->capabilities2;
  1043. /*
  1044. * We can do SGIO
  1045. */
  1046. mmc->max_segs = NR_SG;
  1047. /*
  1048. * Since only a certain number of bits are valid in the data length
  1049. * register, we must ensure that we don't exceed 2^num-1 bytes in a
  1050. * single request.
  1051. */
  1052. mmc->max_req_size = (1 << variant->datalength_bits) - 1;
  1053. /*
  1054. * Set the maximum segment size. Since we aren't doing DMA
  1055. * (yet) we are only limited by the data length register.
  1056. */
  1057. mmc->max_seg_size = mmc->max_req_size;
  1058. /*
  1059. * Block size can be up to 2048 bytes, but must be a power of two.
  1060. */
  1061. mmc->max_blk_size = 2048;
  1062. /*
  1063. * No limit on the number of blocks transferred.
  1064. */
  1065. mmc->max_blk_count = mmc->max_req_size;
  1066. spin_lock_init(&host->lock);
  1067. writel(0, host->base + MMCIMASK0);
  1068. writel(0, host->base + MMCIMASK1);
  1069. writel(0xfff, host->base + MMCICLEAR);
  1070. if (gpio_is_valid(plat->gpio_cd)) {
  1071. ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
  1072. if (ret == 0)
  1073. ret = gpio_direction_input(plat->gpio_cd);
  1074. if (ret == 0)
  1075. host->gpio_cd = plat->gpio_cd;
  1076. else if (ret != -ENOSYS)
  1077. goto err_gpio_cd;
  1078. /*
  1079. * A gpio pin that will detect cards when inserted and removed
  1080. * will most likely want to trigger on the edges if it is
  1081. * 0 when ejected and 1 when inserted (or mutatis mutandis
  1082. * for the inverted case) so we request triggers on both
  1083. * edges.
  1084. */
  1085. ret = request_any_context_irq(gpio_to_irq(plat->gpio_cd),
  1086. mmci_cd_irq,
  1087. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  1088. DRIVER_NAME " (cd)", host);
  1089. if (ret >= 0)
  1090. host->gpio_cd_irq = gpio_to_irq(plat->gpio_cd);
  1091. }
  1092. if (gpio_is_valid(plat->gpio_wp)) {
  1093. ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
  1094. if (ret == 0)
  1095. ret = gpio_direction_input(plat->gpio_wp);
  1096. if (ret == 0)
  1097. host->gpio_wp = plat->gpio_wp;
  1098. else if (ret != -ENOSYS)
  1099. goto err_gpio_wp;
  1100. }
  1101. if ((host->plat->status || host->gpio_cd != -ENOSYS)
  1102. && host->gpio_cd_irq < 0)
  1103. mmc->caps |= MMC_CAP_NEEDS_POLL;
  1104. ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
  1105. if (ret)
  1106. goto unmap;
  1107. if (dev->irq[1] == NO_IRQ)
  1108. host->singleirq = true;
  1109. else {
  1110. ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED,
  1111. DRIVER_NAME " (pio)", host);
  1112. if (ret)
  1113. goto irq0_free;
  1114. }
  1115. writel(MCI_IRQENABLE, host->base + MMCIMASK0);
  1116. amba_set_drvdata(dev, mmc);
  1117. dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
  1118. mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
  1119. amba_rev(dev), (unsigned long long)dev->res.start,
  1120. dev->irq[0], dev->irq[1]);
  1121. mmci_dma_setup(host);
  1122. pm_runtime_put(&dev->dev);
  1123. mmc_add_host(mmc);
  1124. return 0;
  1125. irq0_free:
  1126. free_irq(dev->irq[0], host);
  1127. unmap:
  1128. if (host->gpio_wp != -ENOSYS)
  1129. gpio_free(host->gpio_wp);
  1130. err_gpio_wp:
  1131. if (host->gpio_cd_irq >= 0)
  1132. free_irq(host->gpio_cd_irq, host);
  1133. if (host->gpio_cd != -ENOSYS)
  1134. gpio_free(host->gpio_cd);
  1135. err_gpio_cd:
  1136. iounmap(host->base);
  1137. clk_disable:
  1138. clk_disable(host->clk);
  1139. clk_unprep:
  1140. clk_unprepare(host->clk);
  1141. clk_free:
  1142. clk_put(host->clk);
  1143. host_free:
  1144. mmc_free_host(mmc);
  1145. rel_regions:
  1146. amba_release_regions(dev);
  1147. out:
  1148. return ret;
  1149. }
  1150. static int __devexit mmci_remove(struct amba_device *dev)
  1151. {
  1152. struct mmc_host *mmc = amba_get_drvdata(dev);
  1153. amba_set_drvdata(dev, NULL);
  1154. if (mmc) {
  1155. struct mmci_host *host = mmc_priv(mmc);
  1156. /*
  1157. * Undo pm_runtime_put() in probe. We use the _sync
  1158. * version here so that we can access the primecell.
  1159. */
  1160. pm_runtime_get_sync(&dev->dev);
  1161. mmc_remove_host(mmc);
  1162. writel(0, host->base + MMCIMASK0);
  1163. writel(0, host->base + MMCIMASK1);
  1164. writel(0, host->base + MMCICOMMAND);
  1165. writel(0, host->base + MMCIDATACTRL);
  1166. mmci_dma_release(host);
  1167. free_irq(dev->irq[0], host);
  1168. if (!host->singleirq)
  1169. free_irq(dev->irq[1], host);
  1170. if (host->gpio_wp != -ENOSYS)
  1171. gpio_free(host->gpio_wp);
  1172. if (host->gpio_cd_irq >= 0)
  1173. free_irq(host->gpio_cd_irq, host);
  1174. if (host->gpio_cd != -ENOSYS)
  1175. gpio_free(host->gpio_cd);
  1176. iounmap(host->base);
  1177. clk_disable(host->clk);
  1178. clk_unprepare(host->clk);
  1179. clk_put(host->clk);
  1180. if (host->vcc)
  1181. mmc_regulator_set_ocr(mmc, host->vcc, 0);
  1182. regulator_put(host->vcc);
  1183. mmc_free_host(mmc);
  1184. amba_release_regions(dev);
  1185. }
  1186. return 0;
  1187. }
  1188. #ifdef CONFIG_PM
  1189. static int mmci_suspend(struct amba_device *dev, pm_message_t state)
  1190. {
  1191. struct mmc_host *mmc = amba_get_drvdata(dev);
  1192. int ret = 0;
  1193. if (mmc) {
  1194. struct mmci_host *host = mmc_priv(mmc);
  1195. ret = mmc_suspend_host(mmc);
  1196. if (ret == 0)
  1197. writel(0, host->base + MMCIMASK0);
  1198. }
  1199. return ret;
  1200. }
  1201. static int mmci_resume(struct amba_device *dev)
  1202. {
  1203. struct mmc_host *mmc = amba_get_drvdata(dev);
  1204. int ret = 0;
  1205. if (mmc) {
  1206. struct mmci_host *host = mmc_priv(mmc);
  1207. writel(MCI_IRQENABLE, host->base + MMCIMASK0);
  1208. ret = mmc_resume_host(mmc);
  1209. }
  1210. return ret;
  1211. }
  1212. #else
  1213. #define mmci_suspend NULL
  1214. #define mmci_resume NULL
  1215. #endif
  1216. static struct amba_id mmci_ids[] = {
  1217. {
  1218. .id = 0x00041180,
  1219. .mask = 0xff0fffff,
  1220. .data = &variant_arm,
  1221. },
  1222. {
  1223. .id = 0x01041180,
  1224. .mask = 0xff0fffff,
  1225. .data = &variant_arm_extended_fifo,
  1226. },
  1227. {
  1228. .id = 0x00041181,
  1229. .mask = 0x000fffff,
  1230. .data = &variant_arm,
  1231. },
  1232. /* ST Micro variants */
  1233. {
  1234. .id = 0x00180180,
  1235. .mask = 0x00ffffff,
  1236. .data = &variant_u300,
  1237. },
  1238. {
  1239. .id = 0x00280180,
  1240. .mask = 0x00ffffff,
  1241. .data = &variant_u300,
  1242. },
  1243. {
  1244. .id = 0x00480180,
  1245. .mask = 0xf0ffffff,
  1246. .data = &variant_ux500,
  1247. },
  1248. {
  1249. .id = 0x10480180,
  1250. .mask = 0xf0ffffff,
  1251. .data = &variant_ux500v2,
  1252. },
  1253. { 0, 0 },
  1254. };
  1255. MODULE_DEVICE_TABLE(amba, mmci_ids);
  1256. static struct amba_driver mmci_driver = {
  1257. .drv = {
  1258. .name = DRIVER_NAME,
  1259. },
  1260. .probe = mmci_probe,
  1261. .remove = __devexit_p(mmci_remove),
  1262. .suspend = mmci_suspend,
  1263. .resume = mmci_resume,
  1264. .id_table = mmci_ids,
  1265. };
  1266. static int __init mmci_init(void)
  1267. {
  1268. return amba_driver_register(&mmci_driver);
  1269. }
  1270. static void __exit mmci_exit(void)
  1271. {
  1272. amba_driver_unregister(&mmci_driver);
  1273. }
  1274. module_init(mmci_init);
  1275. module_exit(mmci_exit);
  1276. module_param(fmax, uint, 0444);
  1277. MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
  1278. MODULE_LICENSE("GPL");