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