mmci.c 15 KB

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