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. #ifdef CONFIG_MMC_DEBUG
  33. #define DBG(host,fmt,args...) \
  34. pr_debug("%s: %s: " fmt, mmc_hostname(host->mmc), __func__ , args)
  35. #else
  36. #define DBG(host,fmt,args...) do { } while (0)
  37. #endif
  38. static unsigned int fmax = 515633;
  39. static void
  40. mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
  41. {
  42. writel(0, host->base + MMCICOMMAND);
  43. host->mrq = NULL;
  44. host->cmd = NULL;
  45. if (mrq->data)
  46. mrq->data->bytes_xfered = host->data_xfered;
  47. /*
  48. * Need to drop the host lock here; mmc_request_done may call
  49. * back into the driver...
  50. */
  51. spin_unlock(&host->lock);
  52. mmc_request_done(host->mmc, mrq);
  53. spin_lock(&host->lock);
  54. }
  55. static void mmci_stop_data(struct mmci_host *host)
  56. {
  57. writel(0, host->base + MMCIDATACTRL);
  58. writel(0, host->base + MMCIMASK1);
  59. host->data = NULL;
  60. }
  61. static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
  62. {
  63. unsigned int datactrl, timeout, irqmask;
  64. unsigned long long clks;
  65. void __iomem *base;
  66. DBG(host, "blksz %04x blks %04x flags %08x\n",
  67. 1 << data->blksz_bits, data->blocks, data->flags);
  68. host->data = data;
  69. host->size = data->blocks << data->blksz_bits;
  70. host->data_xfered = 0;
  71. mmci_init_sg(host, data);
  72. clks = (unsigned long long)data->timeout_ns * host->cclk;
  73. do_div(clks, 1000000000UL);
  74. timeout = data->timeout_clks + (unsigned int)clks;
  75. base = host->base;
  76. writel(timeout, base + MMCIDATATIMER);
  77. writel(host->size, base + MMCIDATALENGTH);
  78. datactrl = MCI_DPSM_ENABLE | data->blksz_bits << 4;
  79. if (data->flags & MMC_DATA_READ) {
  80. datactrl |= MCI_DPSM_DIRECTION;
  81. irqmask = MCI_RXFIFOHALFFULLMASK;
  82. } else {
  83. /*
  84. * We don't actually need to include "FIFO empty" here
  85. * since its implicit in "FIFO half empty".
  86. */
  87. irqmask = MCI_TXFIFOHALFEMPTYMASK;
  88. }
  89. writel(datactrl, base + MMCIDATACTRL);
  90. writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
  91. writel(irqmask, base + MMCIMASK1);
  92. }
  93. static void
  94. mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
  95. {
  96. void __iomem *base = host->base;
  97. DBG(host, "op %02x arg %08x flags %08x\n",
  98. cmd->opcode, cmd->arg, cmd->flags);
  99. if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
  100. writel(0, base + MMCICOMMAND);
  101. udelay(1);
  102. }
  103. c |= cmd->opcode | MCI_CPSM_ENABLE;
  104. if (cmd->flags & MMC_RSP_PRESENT) {
  105. if (cmd->flags & MMC_RSP_136)
  106. c |= MCI_CPSM_LONGRSP;
  107. c |= MCI_CPSM_RESPONSE;
  108. }
  109. if (/*interrupt*/0)
  110. c |= MCI_CPSM_INTERRUPT;
  111. host->cmd = cmd;
  112. writel(cmd->arg, base + MMCIARGUMENT);
  113. writel(c, base + MMCICOMMAND);
  114. }
  115. static void
  116. mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
  117. unsigned int status)
  118. {
  119. if (status & MCI_DATABLOCKEND) {
  120. host->data_xfered += 1 << data->blksz_bits;
  121. }
  122. if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
  123. if (status & MCI_DATACRCFAIL)
  124. data->error = MMC_ERR_BADCRC;
  125. else if (status & MCI_DATATIMEOUT)
  126. data->error = MMC_ERR_TIMEOUT;
  127. else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN))
  128. data->error = MMC_ERR_FIFO;
  129. status |= MCI_DATAEND;
  130. /*
  131. * We hit an error condition. Ensure that any data
  132. * partially written to a page is properly coherent.
  133. */
  134. if (host->sg_len && data->flags & MMC_DATA_READ)
  135. flush_dcache_page(host->sg_ptr->page);
  136. }
  137. if (status & MCI_DATAEND) {
  138. mmci_stop_data(host);
  139. if (!data->stop) {
  140. mmci_request_end(host, data->mrq);
  141. } else {
  142. mmci_start_command(host, data->stop, 0);
  143. }
  144. }
  145. }
  146. static void
  147. mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
  148. unsigned int status)
  149. {
  150. void __iomem *base = host->base;
  151. host->cmd = NULL;
  152. cmd->resp[0] = readl(base + MMCIRESPONSE0);
  153. cmd->resp[1] = readl(base + MMCIRESPONSE1);
  154. cmd->resp[2] = readl(base + MMCIRESPONSE2);
  155. cmd->resp[3] = readl(base + MMCIRESPONSE3);
  156. if (status & MCI_CMDTIMEOUT) {
  157. cmd->error = MMC_ERR_TIMEOUT;
  158. } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
  159. cmd->error = MMC_ERR_BADCRC;
  160. }
  161. if (!cmd->data || cmd->error != MMC_ERR_NONE) {
  162. mmci_request_end(host, cmd->mrq);
  163. } else if (!(cmd->data->flags & MMC_DATA_READ)) {
  164. mmci_start_data(host, cmd->data);
  165. }
  166. }
  167. static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
  168. {
  169. void __iomem *base = host->base;
  170. char *ptr = buffer;
  171. u32 status;
  172. do {
  173. int count = host->size - (readl(base + MMCIFIFOCNT) << 2);
  174. if (count > remain)
  175. count = remain;
  176. if (count <= 0)
  177. break;
  178. readsl(base + MMCIFIFO, ptr, count >> 2);
  179. ptr += count;
  180. remain -= count;
  181. if (remain == 0)
  182. break;
  183. status = readl(base + MMCISTATUS);
  184. } while (status & MCI_RXDATAAVLBL);
  185. return ptr - buffer;
  186. }
  187. static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
  188. {
  189. void __iomem *base = host->base;
  190. char *ptr = buffer;
  191. do {
  192. unsigned int count, maxcnt;
  193. maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE : MCI_FIFOHALFSIZE;
  194. count = min(remain, maxcnt);
  195. writesl(base + MMCIFIFO, ptr, count >> 2);
  196. ptr += count;
  197. remain -= count;
  198. if (remain == 0)
  199. break;
  200. status = readl(base + MMCISTATUS);
  201. } while (status & MCI_TXFIFOHALFEMPTY);
  202. return ptr - buffer;
  203. }
  204. /*
  205. * PIO data transfer IRQ handler.
  206. */
  207. static irqreturn_t mmci_pio_irq(int irq, void *dev_id, struct pt_regs *regs)
  208. {
  209. struct mmci_host *host = dev_id;
  210. void __iomem *base = host->base;
  211. u32 status;
  212. status = readl(base + MMCISTATUS);
  213. DBG(host, "irq1 %08x\n", status);
  214. do {
  215. unsigned long flags;
  216. unsigned int remain, len;
  217. char *buffer;
  218. /*
  219. * For write, we only need to test the half-empty flag
  220. * here - if the FIFO is completely empty, then by
  221. * definition it is more than half empty.
  222. *
  223. * For read, check for data available.
  224. */
  225. if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
  226. break;
  227. /*
  228. * Map the current scatter buffer.
  229. */
  230. buffer = mmci_kmap_atomic(host, &flags) + host->sg_off;
  231. remain = host->sg_ptr->length - host->sg_off;
  232. len = 0;
  233. if (status & MCI_RXACTIVE)
  234. len = mmci_pio_read(host, buffer, remain);
  235. if (status & MCI_TXACTIVE)
  236. len = mmci_pio_write(host, buffer, remain, status);
  237. /*
  238. * Unmap the buffer.
  239. */
  240. mmci_kunmap_atomic(host, buffer, &flags);
  241. host->sg_off += len;
  242. host->size -= len;
  243. remain -= len;
  244. if (remain)
  245. break;
  246. /*
  247. * If we were reading, and we have completed this
  248. * page, ensure that the data cache is coherent.
  249. */
  250. if (status & MCI_RXACTIVE)
  251. flush_dcache_page(host->sg_ptr->page);
  252. if (!mmci_next_sg(host))
  253. break;
  254. status = readl(base + MMCISTATUS);
  255. } while (1);
  256. /*
  257. * If we're nearing the end of the read, switch to
  258. * "any data available" mode.
  259. */
  260. if (status & MCI_RXACTIVE && host->size < MCI_FIFOSIZE)
  261. writel(MCI_RXDATAAVLBLMASK, base + MMCIMASK1);
  262. /*
  263. * If we run out of data, disable the data IRQs; this
  264. * prevents a race where the FIFO becomes empty before
  265. * the chip itself has disabled the data path, and
  266. * stops us racing with our data end IRQ.
  267. */
  268. if (host->size == 0) {
  269. writel(0, base + MMCIMASK1);
  270. writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
  271. }
  272. return IRQ_HANDLED;
  273. }
  274. /*
  275. * Handle completion of command and data transfers.
  276. */
  277. static irqreturn_t mmci_irq(int irq, void *dev_id, struct pt_regs *regs)
  278. {
  279. struct mmci_host *host = dev_id;
  280. u32 status;
  281. int ret = 0;
  282. spin_lock(&host->lock);
  283. do {
  284. struct mmc_command *cmd;
  285. struct mmc_data *data;
  286. status = readl(host->base + MMCISTATUS);
  287. status &= readl(host->base + MMCIMASK0);
  288. writel(status, host->base + MMCICLEAR);
  289. DBG(host, "irq0 %08x\n", status);
  290. data = host->data;
  291. if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
  292. MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
  293. mmci_data_irq(host, data, status);
  294. cmd = host->cmd;
  295. if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
  296. mmci_cmd_irq(host, cmd, status);
  297. ret = 1;
  298. } while (status);
  299. spin_unlock(&host->lock);
  300. return IRQ_RETVAL(ret);
  301. }
  302. static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  303. {
  304. struct mmci_host *host = mmc_priv(mmc);
  305. WARN_ON(host->mrq != NULL);
  306. spin_lock_irq(&host->lock);
  307. host->mrq = mrq;
  308. if (mrq->data && mrq->data->flags & MMC_DATA_READ)
  309. mmci_start_data(host, mrq->data);
  310. mmci_start_command(host, mrq->cmd, 0);
  311. spin_unlock_irq(&host->lock);
  312. }
  313. static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  314. {
  315. struct mmci_host *host = mmc_priv(mmc);
  316. u32 clk = 0, pwr = 0;
  317. DBG(host, "clock %uHz busmode %u powermode %u Vdd %u\n",
  318. ios->clock, ios->bus_mode, ios->power_mode, ios->vdd);
  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");