mmci.c 14 KB

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