mmci.c 35 KB

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