mmci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*
  2. * linux/drivers/mmc/mmci.c - ARM PrimeCell MMCI PL180/1 driver
  3. *
  4. * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/init.h>
  13. #include <linux/ioport.h>
  14. #include <linux/device.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/delay.h>
  17. #include <linux/err.h>
  18. #include <linux/highmem.h>
  19. #include <linux/mmc/host.h>
  20. #include <linux/mmc/protocol.h>
  21. #include <linux/amba/bus.h>
  22. #include <linux/clk.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/div64.h>
  25. #include <asm/io.h>
  26. #include <asm/scatterlist.h>
  27. #include <asm/sizes.h>
  28. #include <asm/mach/mmc.h>
  29. #include "mmci.h"
  30. #define DRIVER_NAME "mmci-pl18x"
  31. #define DBG(host,fmt,args...) \
  32. pr_debug("%s: %s: " fmt, mmc_hostname(host->mmc), __func__ , args)
  33. static unsigned int fmax = 515633;
  34. static void
  35. mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
  36. {
  37. writel(0, host->base + MMCICOMMAND);
  38. host->mrq = NULL;
  39. host->cmd = NULL;
  40. if (mrq->data)
  41. mrq->data->bytes_xfered = host->data_xfered;
  42. /*
  43. * Need to drop the host lock here; mmc_request_done may call
  44. * back into the driver...
  45. */
  46. spin_unlock(&host->lock);
  47. mmc_request_done(host->mmc, mrq);
  48. spin_lock(&host->lock);
  49. }
  50. static void mmci_stop_data(struct mmci_host *host)
  51. {
  52. writel(0, host->base + MMCIDATACTRL);
  53. writel(0, host->base + MMCIMASK1);
  54. host->data = NULL;
  55. }
  56. static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
  57. {
  58. unsigned int datactrl, timeout, irqmask;
  59. unsigned long long clks;
  60. void __iomem *base;
  61. int blksz_bits;
  62. DBG(host, "blksz %04x blks %04x flags %08x\n",
  63. data->blksz, data->blocks, data->flags);
  64. host->data = data;
  65. host->size = data->blksz;
  66. host->data_xfered = 0;
  67. mmci_init_sg(host, data);
  68. clks = (unsigned long long)data->timeout_ns * host->cclk;
  69. do_div(clks, 1000000000UL);
  70. timeout = data->timeout_clks + (unsigned int)clks;
  71. base = host->base;
  72. writel(timeout, base + MMCIDATATIMER);
  73. writel(host->size, base + MMCIDATALENGTH);
  74. blksz_bits = ffs(data->blksz) - 1;
  75. BUG_ON(1 << blksz_bits != data->blksz);
  76. datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
  77. if (data->flags & MMC_DATA_READ) {
  78. datactrl |= MCI_DPSM_DIRECTION;
  79. irqmask = MCI_RXFIFOHALFFULLMASK;
  80. /*
  81. * If we have less than a FIFOSIZE of bytes to transfer,
  82. * trigger a PIO interrupt as soon as any data is available.
  83. */
  84. if (host->size < MCI_FIFOSIZE)
  85. irqmask |= MCI_RXDATAAVLBLMASK;
  86. } else {
  87. /*
  88. * We don't actually need to include "FIFO empty" here
  89. * since its implicit in "FIFO half empty".
  90. */
  91. irqmask = MCI_TXFIFOHALFEMPTYMASK;
  92. }
  93. writel(datactrl, base + MMCIDATACTRL);
  94. writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
  95. writel(irqmask, base + MMCIMASK1);
  96. }
  97. static void
  98. mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
  99. {
  100. void __iomem *base = host->base;
  101. DBG(host, "op %02x arg %08x flags %08x\n",
  102. cmd->opcode, cmd->arg, cmd->flags);
  103. if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
  104. writel(0, base + MMCICOMMAND);
  105. udelay(1);
  106. }
  107. c |= cmd->opcode | MCI_CPSM_ENABLE;
  108. if (cmd->flags & MMC_RSP_PRESENT) {
  109. if (cmd->flags & MMC_RSP_136)
  110. c |= MCI_CPSM_LONGRSP;
  111. c |= MCI_CPSM_RESPONSE;
  112. }
  113. if (/*interrupt*/0)
  114. c |= MCI_CPSM_INTERRUPT;
  115. host->cmd = cmd;
  116. writel(cmd->arg, base + MMCIARGUMENT);
  117. writel(c, base + MMCICOMMAND);
  118. }
  119. static void
  120. mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
  121. unsigned int status)
  122. {
  123. if (status & MCI_DATABLOCKEND) {
  124. host->data_xfered += data->blksz;
  125. }
  126. if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
  127. if (status & MCI_DATACRCFAIL)
  128. data->error = MMC_ERR_BADCRC;
  129. else if (status & MCI_DATATIMEOUT)
  130. data->error = MMC_ERR_TIMEOUT;
  131. else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN))
  132. data->error = MMC_ERR_FIFO;
  133. status |= MCI_DATAEND;
  134. /*
  135. * We hit an error condition. Ensure that any data
  136. * partially written to a page is properly coherent.
  137. */
  138. if (host->sg_len && data->flags & MMC_DATA_READ)
  139. flush_dcache_page(host->sg_ptr->page);
  140. }
  141. if (status & MCI_DATAEND) {
  142. mmci_stop_data(host);
  143. if (!data->stop) {
  144. mmci_request_end(host, data->mrq);
  145. } else {
  146. mmci_start_command(host, data->stop, 0);
  147. }
  148. }
  149. }
  150. static void
  151. mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
  152. unsigned int status)
  153. {
  154. void __iomem *base = host->base;
  155. host->cmd = NULL;
  156. cmd->resp[0] = readl(base + MMCIRESPONSE0);
  157. cmd->resp[1] = readl(base + MMCIRESPONSE1);
  158. cmd->resp[2] = readl(base + MMCIRESPONSE2);
  159. cmd->resp[3] = readl(base + MMCIRESPONSE3);
  160. if (status & MCI_CMDTIMEOUT) {
  161. cmd->error = MMC_ERR_TIMEOUT;
  162. } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
  163. cmd->error = MMC_ERR_BADCRC;
  164. }
  165. if (!cmd->data || cmd->error != MMC_ERR_NONE) {
  166. mmci_request_end(host, cmd->mrq);
  167. } else if (!(cmd->data->flags & MMC_DATA_READ)) {
  168. mmci_start_data(host, cmd->data);
  169. }
  170. }
  171. static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
  172. {
  173. void __iomem *base = host->base;
  174. char *ptr = buffer;
  175. u32 status;
  176. do {
  177. int count = host->size - (readl(base + MMCIFIFOCNT) << 2);
  178. if (count > remain)
  179. count = remain;
  180. if (count <= 0)
  181. break;
  182. readsl(base + MMCIFIFO, ptr, count >> 2);
  183. ptr += count;
  184. remain -= count;
  185. if (remain == 0)
  186. break;
  187. status = readl(base + MMCISTATUS);
  188. } while (status & MCI_RXDATAAVLBL);
  189. return ptr - buffer;
  190. }
  191. static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
  192. {
  193. void __iomem *base = host->base;
  194. char *ptr = buffer;
  195. do {
  196. unsigned int count, maxcnt;
  197. maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE : MCI_FIFOHALFSIZE;
  198. count = min(remain, maxcnt);
  199. writesl(base + MMCIFIFO, ptr, count >> 2);
  200. ptr += count;
  201. remain -= count;
  202. if (remain == 0)
  203. break;
  204. status = readl(base + MMCISTATUS);
  205. } while (status & MCI_TXFIFOHALFEMPTY);
  206. return ptr - buffer;
  207. }
  208. /*
  209. * PIO data transfer IRQ handler.
  210. */
  211. static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
  212. {
  213. struct mmci_host *host = dev_id;
  214. void __iomem *base = host->base;
  215. u32 status;
  216. status = readl(base + MMCISTATUS);
  217. DBG(host, "irq1 %08x\n", status);
  218. do {
  219. unsigned long flags;
  220. unsigned int remain, len;
  221. char *buffer;
  222. /*
  223. * For write, we only need to test the half-empty flag
  224. * here - if the FIFO is completely empty, then by
  225. * definition it is more than half empty.
  226. *
  227. * For read, check for data available.
  228. */
  229. if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
  230. break;
  231. /*
  232. * Map the current scatter buffer.
  233. */
  234. buffer = mmci_kmap_atomic(host, &flags) + host->sg_off;
  235. remain = host->sg_ptr->length - host->sg_off;
  236. len = 0;
  237. if (status & MCI_RXACTIVE)
  238. len = mmci_pio_read(host, buffer, remain);
  239. if (status & MCI_TXACTIVE)
  240. len = mmci_pio_write(host, buffer, remain, status);
  241. /*
  242. * Unmap the buffer.
  243. */
  244. mmci_kunmap_atomic(host, buffer, &flags);
  245. host->sg_off += len;
  246. host->size -= len;
  247. remain -= len;
  248. if (remain)
  249. break;
  250. /*
  251. * If we were reading, and we have completed this
  252. * page, ensure that the data cache is coherent.
  253. */
  254. if (status & MCI_RXACTIVE)
  255. flush_dcache_page(host->sg_ptr->page);
  256. if (!mmci_next_sg(host))
  257. break;
  258. status = readl(base + MMCISTATUS);
  259. } while (1);
  260. /*
  261. * If we're nearing the end of the read, switch to
  262. * "any data available" mode.
  263. */
  264. if (status & MCI_RXACTIVE && host->size < MCI_FIFOSIZE)
  265. writel(MCI_RXDATAAVLBLMASK, base + MMCIMASK1);
  266. /*
  267. * If we run out of data, disable the data IRQs; this
  268. * prevents a race where the FIFO becomes empty before
  269. * the chip itself has disabled the data path, and
  270. * stops us racing with our data end IRQ.
  271. */
  272. if (host->size == 0) {
  273. writel(0, base + MMCIMASK1);
  274. writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
  275. }
  276. return IRQ_HANDLED;
  277. }
  278. /*
  279. * Handle completion of command and data transfers.
  280. */
  281. static irqreturn_t mmci_irq(int irq, void *dev_id)
  282. {
  283. struct mmci_host *host = dev_id;
  284. u32 status;
  285. int ret = 0;
  286. spin_lock(&host->lock);
  287. do {
  288. struct mmc_command *cmd;
  289. struct mmc_data *data;
  290. status = readl(host->base + MMCISTATUS);
  291. status &= readl(host->base + MMCIMASK0);
  292. writel(status, host->base + MMCICLEAR);
  293. DBG(host, "irq0 %08x\n", status);
  294. data = host->data;
  295. if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
  296. MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
  297. mmci_data_irq(host, data, status);
  298. cmd = host->cmd;
  299. if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
  300. mmci_cmd_irq(host, cmd, status);
  301. ret = 1;
  302. } while (status);
  303. spin_unlock(&host->lock);
  304. return IRQ_RETVAL(ret);
  305. }
  306. static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  307. {
  308. struct mmci_host *host = mmc_priv(mmc);
  309. WARN_ON(host->mrq != NULL);
  310. spin_lock_irq(&host->lock);
  311. host->mrq = mrq;
  312. if (mrq->data && mrq->data->flags & MMC_DATA_READ)
  313. mmci_start_data(host, mrq->data);
  314. mmci_start_command(host, mrq->cmd, 0);
  315. spin_unlock_irq(&host->lock);
  316. }
  317. static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  318. {
  319. struct mmci_host *host = mmc_priv(mmc);
  320. u32 clk = 0, pwr = 0;
  321. if (ios->clock) {
  322. if (ios->clock >= host->mclk) {
  323. clk = MCI_CLK_BYPASS;
  324. host->cclk = host->mclk;
  325. } else {
  326. clk = host->mclk / (2 * ios->clock) - 1;
  327. if (clk > 256)
  328. clk = 255;
  329. host->cclk = host->mclk / (2 * (clk + 1));
  330. }
  331. clk |= MCI_CLK_ENABLE;
  332. }
  333. if (host->plat->translate_vdd)
  334. pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
  335. switch (ios->power_mode) {
  336. case MMC_POWER_OFF:
  337. break;
  338. case MMC_POWER_UP:
  339. pwr |= MCI_PWR_UP;
  340. break;
  341. case MMC_POWER_ON:
  342. pwr |= MCI_PWR_ON;
  343. break;
  344. }
  345. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  346. pwr |= MCI_ROD;
  347. writel(clk, host->base + MMCICLOCK);
  348. if (host->pwr != pwr) {
  349. host->pwr = pwr;
  350. writel(pwr, host->base + MMCIPOWER);
  351. }
  352. }
  353. static struct mmc_host_ops mmci_ops = {
  354. .request = mmci_request,
  355. .set_ios = mmci_set_ios,
  356. };
  357. static void mmci_check_status(unsigned long data)
  358. {
  359. struct mmci_host *host = (struct mmci_host *)data;
  360. unsigned int status;
  361. status = host->plat->status(mmc_dev(host->mmc));
  362. if (status ^ host->oldstat)
  363. mmc_detect_change(host->mmc, 0);
  364. host->oldstat = status;
  365. mod_timer(&host->timer, jiffies + HZ);
  366. }
  367. static int mmci_probe(struct amba_device *dev, void *id)
  368. {
  369. struct mmc_platform_data *plat = dev->dev.platform_data;
  370. struct mmci_host *host;
  371. struct mmc_host *mmc;
  372. int ret;
  373. /* must have platform data */
  374. if (!plat) {
  375. ret = -EINVAL;
  376. goto out;
  377. }
  378. ret = amba_request_regions(dev, DRIVER_NAME);
  379. if (ret)
  380. goto out;
  381. mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
  382. if (!mmc) {
  383. ret = -ENOMEM;
  384. goto rel_regions;
  385. }
  386. host = mmc_priv(mmc);
  387. host->clk = clk_get(&dev->dev, "MCLK");
  388. if (IS_ERR(host->clk)) {
  389. ret = PTR_ERR(host->clk);
  390. host->clk = NULL;
  391. goto host_free;
  392. }
  393. ret = clk_enable(host->clk);
  394. if (ret)
  395. goto clk_free;
  396. host->plat = plat;
  397. host->mclk = clk_get_rate(host->clk);
  398. host->mmc = mmc;
  399. host->base = ioremap(dev->res.start, SZ_4K);
  400. if (!host->base) {
  401. ret = -ENOMEM;
  402. goto clk_disable;
  403. }
  404. mmc->ops = &mmci_ops;
  405. mmc->f_min = (host->mclk + 511) / 512;
  406. mmc->f_max = min(host->mclk, fmax);
  407. mmc->ocr_avail = plat->ocr_mask;
  408. mmc->caps = MMC_CAP_MULTIWRITE;
  409. /*
  410. * We can do SGIO
  411. */
  412. mmc->max_hw_segs = 16;
  413. mmc->max_phys_segs = NR_SG;
  414. /*
  415. * Since we only have a 16-bit data length register, we must
  416. * ensure that we don't exceed 2^16-1 bytes in a single request.
  417. * Choose 64 (512-byte) sectors as the limit.
  418. */
  419. mmc->max_sectors = 64;
  420. /*
  421. * Set the maximum segment size. Since we aren't doing DMA
  422. * (yet) we are only limited by the data length register.
  423. */
  424. mmc->max_seg_size = mmc->max_sectors << 9;
  425. spin_lock_init(&host->lock);
  426. writel(0, host->base + MMCIMASK0);
  427. writel(0, host->base + MMCIMASK1);
  428. writel(0xfff, host->base + MMCICLEAR);
  429. ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
  430. if (ret)
  431. goto unmap;
  432. ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED, DRIVER_NAME " (pio)", host);
  433. if (ret)
  434. goto irq0_free;
  435. writel(MCI_IRQENABLE, host->base + MMCIMASK0);
  436. amba_set_drvdata(dev, mmc);
  437. mmc_add_host(mmc);
  438. printk(KERN_INFO "%s: MMCI rev %x cfg %02x at 0x%016llx irq %d,%d\n",
  439. mmc_hostname(mmc), amba_rev(dev), amba_config(dev),
  440. (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
  441. init_timer(&host->timer);
  442. host->timer.data = (unsigned long)host;
  443. host->timer.function = mmci_check_status;
  444. host->timer.expires = jiffies + HZ;
  445. add_timer(&host->timer);
  446. return 0;
  447. irq0_free:
  448. free_irq(dev->irq[0], host);
  449. unmap:
  450. iounmap(host->base);
  451. clk_disable:
  452. clk_disable(host->clk);
  453. clk_free:
  454. clk_put(host->clk);
  455. host_free:
  456. mmc_free_host(mmc);
  457. rel_regions:
  458. amba_release_regions(dev);
  459. out:
  460. return ret;
  461. }
  462. static int mmci_remove(struct amba_device *dev)
  463. {
  464. struct mmc_host *mmc = amba_get_drvdata(dev);
  465. amba_set_drvdata(dev, NULL);
  466. if (mmc) {
  467. struct mmci_host *host = mmc_priv(mmc);
  468. del_timer_sync(&host->timer);
  469. mmc_remove_host(mmc);
  470. writel(0, host->base + MMCIMASK0);
  471. writel(0, host->base + MMCIMASK1);
  472. writel(0, host->base + MMCICOMMAND);
  473. writel(0, host->base + MMCIDATACTRL);
  474. free_irq(dev->irq[0], host);
  475. free_irq(dev->irq[1], host);
  476. iounmap(host->base);
  477. clk_disable(host->clk);
  478. clk_put(host->clk);
  479. mmc_free_host(mmc);
  480. amba_release_regions(dev);
  481. }
  482. return 0;
  483. }
  484. #ifdef CONFIG_PM
  485. static int mmci_suspend(struct amba_device *dev, pm_message_t state)
  486. {
  487. struct mmc_host *mmc = amba_get_drvdata(dev);
  488. int ret = 0;
  489. if (mmc) {
  490. struct mmci_host *host = mmc_priv(mmc);
  491. ret = mmc_suspend_host(mmc, state);
  492. if (ret == 0)
  493. writel(0, host->base + MMCIMASK0);
  494. }
  495. return ret;
  496. }
  497. static int mmci_resume(struct amba_device *dev)
  498. {
  499. struct mmc_host *mmc = amba_get_drvdata(dev);
  500. int ret = 0;
  501. if (mmc) {
  502. struct mmci_host *host = mmc_priv(mmc);
  503. writel(MCI_IRQENABLE, host->base + MMCIMASK0);
  504. ret = mmc_resume_host(mmc);
  505. }
  506. return ret;
  507. }
  508. #else
  509. #define mmci_suspend NULL
  510. #define mmci_resume NULL
  511. #endif
  512. static struct amba_id mmci_ids[] = {
  513. {
  514. .id = 0x00041180,
  515. .mask = 0x000fffff,
  516. },
  517. {
  518. .id = 0x00041181,
  519. .mask = 0x000fffff,
  520. },
  521. { 0, 0 },
  522. };
  523. static struct amba_driver mmci_driver = {
  524. .drv = {
  525. .name = DRIVER_NAME,
  526. },
  527. .probe = mmci_probe,
  528. .remove = mmci_remove,
  529. .suspend = mmci_suspend,
  530. .resume = mmci_resume,
  531. .id_table = mmci_ids,
  532. };
  533. static int __init mmci_init(void)
  534. {
  535. return amba_driver_register(&mmci_driver);
  536. }
  537. static void __exit mmci_exit(void)
  538. {
  539. amba_driver_unregister(&mmci_driver);
  540. }
  541. module_init(mmci_init);
  542. module_exit(mmci_exit);
  543. module_param(fmax, uint, 0444);
  544. MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
  545. MODULE_LICENSE("GPL");