mmci.c 37 KB

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