mmci.c 40 KB

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