mmci.c 46 KB

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