mmci.c 14 KB

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