mmci.c 15 KB

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