mmci.c 40 KB

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