mmci.c 38 KB

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