mmci.c 41 KB

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