mmci.c 36 KB

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