mmci.c 41 KB

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