mmci.c 15 KB

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