mmci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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/sizes.h>
  27. #include <asm/hardware/amba.h>
  28. #include <asm/hardware/clock.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. if (status & MCI_DATAEND) {
  137. mmci_stop_data(host);
  138. if (!data->stop) {
  139. mmci_request_end(host, data->mrq);
  140. } else {
  141. mmci_start_command(host, data->stop, 0);
  142. }
  143. }
  144. }
  145. static void
  146. mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
  147. unsigned int status)
  148. {
  149. void __iomem *base = host->base;
  150. host->cmd = NULL;
  151. cmd->resp[0] = readl(base + MMCIRESPONSE0);
  152. cmd->resp[1] = readl(base + MMCIRESPONSE1);
  153. cmd->resp[2] = readl(base + MMCIRESPONSE2);
  154. cmd->resp[3] = readl(base + MMCIRESPONSE3);
  155. if (status & MCI_CMDTIMEOUT) {
  156. cmd->error = MMC_ERR_TIMEOUT;
  157. } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
  158. cmd->error = MMC_ERR_BADCRC;
  159. }
  160. if (!cmd->data || cmd->error != MMC_ERR_NONE) {
  161. mmci_request_end(host, cmd->mrq);
  162. } else if (!(cmd->data->flags & MMC_DATA_READ)) {
  163. mmci_start_data(host, cmd->data);
  164. }
  165. }
  166. static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
  167. {
  168. void __iomem *base = host->base;
  169. char *ptr = buffer;
  170. u32 status;
  171. do {
  172. int count = host->size - (readl(base + MMCIFIFOCNT) << 2);
  173. if (count > remain)
  174. count = remain;
  175. if (count <= 0)
  176. break;
  177. readsl(base + MMCIFIFO, ptr, count >> 2);
  178. ptr += count;
  179. remain -= count;
  180. if (remain == 0)
  181. break;
  182. status = readl(base + MMCISTATUS);
  183. } while (status & MCI_RXDATAAVLBL);
  184. return ptr - buffer;
  185. }
  186. static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
  187. {
  188. void __iomem *base = host->base;
  189. char *ptr = buffer;
  190. do {
  191. unsigned int count, maxcnt;
  192. maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE : MCI_FIFOHALFSIZE;
  193. count = min(remain, maxcnt);
  194. writesl(base + MMCIFIFO, ptr, count >> 2);
  195. ptr += count;
  196. remain -= count;
  197. if (remain == 0)
  198. break;
  199. status = readl(base + MMCISTATUS);
  200. } while (status & MCI_TXFIFOHALFEMPTY);
  201. return ptr - buffer;
  202. }
  203. /*
  204. * PIO data transfer IRQ handler.
  205. */
  206. static irqreturn_t mmci_pio_irq(int irq, void *dev_id, struct pt_regs *regs)
  207. {
  208. struct mmci_host *host = dev_id;
  209. void __iomem *base = host->base;
  210. u32 status;
  211. status = readl(base + MMCISTATUS);
  212. DBG(host, "irq1 %08x\n", status);
  213. do {
  214. unsigned long flags;
  215. unsigned int remain, len;
  216. char *buffer;
  217. /*
  218. * For write, we only need to test the half-empty flag
  219. * here - if the FIFO is completely empty, then by
  220. * definition it is more than half empty.
  221. *
  222. * For read, check for data available.
  223. */
  224. if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
  225. break;
  226. /*
  227. * Map the current scatter buffer.
  228. */
  229. buffer = mmci_kmap_atomic(host, &flags) + host->sg_off;
  230. remain = host->sg_ptr->length - host->sg_off;
  231. len = 0;
  232. if (status & MCI_RXACTIVE)
  233. len = mmci_pio_read(host, buffer, remain);
  234. if (status & MCI_TXACTIVE)
  235. len = mmci_pio_write(host, buffer, remain, status);
  236. /*
  237. * Unmap the buffer.
  238. */
  239. mmci_kunmap_atomic(host, &flags);
  240. host->sg_off += len;
  241. host->size -= len;
  242. remain -= len;
  243. if (remain)
  244. break;
  245. if (!mmci_next_sg(host))
  246. break;
  247. status = readl(base + MMCISTATUS);
  248. } while (1);
  249. /*
  250. * If we're nearing the end of the read, switch to
  251. * "any data available" mode.
  252. */
  253. if (status & MCI_RXACTIVE && host->size < MCI_FIFOSIZE)
  254. writel(MCI_RXDATAAVLBLMASK, base + MMCIMASK1);
  255. /*
  256. * If we run out of data, disable the data IRQs; this
  257. * prevents a race where the FIFO becomes empty before
  258. * the chip itself has disabled the data path, and
  259. * stops us racing with our data end IRQ.
  260. */
  261. if (host->size == 0) {
  262. writel(0, base + MMCIMASK1);
  263. writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
  264. }
  265. return IRQ_HANDLED;
  266. }
  267. /*
  268. * Handle completion of command and data transfers.
  269. */
  270. static irqreturn_t mmci_irq(int irq, void *dev_id, struct pt_regs *regs)
  271. {
  272. struct mmci_host *host = dev_id;
  273. u32 status;
  274. int ret = 0;
  275. spin_lock(&host->lock);
  276. do {
  277. struct mmc_command *cmd;
  278. struct mmc_data *data;
  279. status = readl(host->base + MMCISTATUS);
  280. status &= readl(host->base + MMCIMASK0);
  281. writel(status, host->base + MMCICLEAR);
  282. DBG(host, "irq0 %08x\n", status);
  283. data = host->data;
  284. if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
  285. MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
  286. mmci_data_irq(host, data, status);
  287. cmd = host->cmd;
  288. if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
  289. mmci_cmd_irq(host, cmd, status);
  290. ret = 1;
  291. } while (status);
  292. spin_unlock(&host->lock);
  293. return IRQ_RETVAL(ret);
  294. }
  295. static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  296. {
  297. struct mmci_host *host = mmc_priv(mmc);
  298. WARN_ON(host->mrq != NULL);
  299. spin_lock_irq(&host->lock);
  300. host->mrq = mrq;
  301. if (mrq->data && mrq->data->flags & MMC_DATA_READ)
  302. mmci_start_data(host, mrq->data);
  303. mmci_start_command(host, mrq->cmd, 0);
  304. spin_unlock_irq(&host->lock);
  305. }
  306. static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  307. {
  308. struct mmci_host *host = mmc_priv(mmc);
  309. u32 clk = 0, pwr = 0;
  310. DBG(host, "clock %uHz busmode %u powermode %u Vdd %u\n",
  311. ios->clock, ios->bus_mode, ios->power_mode, ios->vdd);
  312. if (ios->clock) {
  313. if (ios->clock >= host->mclk) {
  314. clk = MCI_CLK_BYPASS;
  315. host->cclk = host->mclk;
  316. } else {
  317. clk = host->mclk / (2 * ios->clock) - 1;
  318. if (clk > 256)
  319. clk = 255;
  320. host->cclk = host->mclk / (2 * (clk + 1));
  321. }
  322. clk |= MCI_CLK_ENABLE;
  323. }
  324. if (host->plat->translate_vdd)
  325. pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
  326. switch (ios->power_mode) {
  327. case MMC_POWER_OFF:
  328. break;
  329. case MMC_POWER_UP:
  330. pwr |= MCI_PWR_UP;
  331. break;
  332. case MMC_POWER_ON:
  333. pwr |= MCI_PWR_ON;
  334. break;
  335. }
  336. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  337. pwr |= MCI_ROD;
  338. writel(clk, host->base + MMCICLOCK);
  339. if (host->pwr != pwr) {
  340. host->pwr = pwr;
  341. writel(pwr, host->base + MMCIPOWER);
  342. }
  343. }
  344. static struct mmc_host_ops mmci_ops = {
  345. .request = mmci_request,
  346. .set_ios = mmci_set_ios,
  347. };
  348. static void mmci_check_status(unsigned long data)
  349. {
  350. struct mmci_host *host = (struct mmci_host *)data;
  351. unsigned int status;
  352. status = host->plat->status(mmc_dev(host->mmc));
  353. if (status ^ host->oldstat)
  354. mmc_detect_change(host->mmc, 0);
  355. host->oldstat = status;
  356. mod_timer(&host->timer, jiffies + HZ);
  357. }
  358. static int mmci_probe(struct amba_device *dev, void *id)
  359. {
  360. struct mmc_platform_data *plat = dev->dev.platform_data;
  361. struct mmci_host *host;
  362. struct mmc_host *mmc;
  363. int ret;
  364. /* must have platform data */
  365. if (!plat) {
  366. ret = -EINVAL;
  367. goto out;
  368. }
  369. ret = amba_request_regions(dev, DRIVER_NAME);
  370. if (ret)
  371. goto out;
  372. mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
  373. if (!mmc) {
  374. ret = -ENOMEM;
  375. goto rel_regions;
  376. }
  377. host = mmc_priv(mmc);
  378. host->clk = clk_get(&dev->dev, "MCLK");
  379. if (IS_ERR(host->clk)) {
  380. ret = PTR_ERR(host->clk);
  381. host->clk = NULL;
  382. goto host_free;
  383. }
  384. ret = clk_use(host->clk);
  385. if (ret)
  386. goto clk_free;
  387. ret = clk_enable(host->clk);
  388. if (ret)
  389. goto clk_unuse;
  390. host->plat = plat;
  391. host->mclk = clk_get_rate(host->clk);
  392. host->mmc = mmc;
  393. host->base = ioremap(dev->res.start, SZ_4K);
  394. if (!host->base) {
  395. ret = -ENOMEM;
  396. goto clk_disable;
  397. }
  398. mmc->ops = &mmci_ops;
  399. mmc->f_min = (host->mclk + 511) / 512;
  400. mmc->f_max = min(host->mclk, fmax);
  401. mmc->ocr_avail = plat->ocr_mask;
  402. /*
  403. * We can do SGIO
  404. */
  405. mmc->max_hw_segs = 16;
  406. mmc->max_phys_segs = NR_SG;
  407. /*
  408. * Since we only have a 16-bit data length register, we must
  409. * ensure that we don't exceed 2^16-1 bytes in a single request.
  410. * Choose 64 (512-byte) sectors as the limit.
  411. */
  412. mmc->max_sectors = 64;
  413. /*
  414. * Set the maximum segment size. Since we aren't doing DMA
  415. * (yet) we are only limited by the data length register.
  416. */
  417. mmc->max_seg_size = mmc->max_sectors << 9;
  418. spin_lock_init(&host->lock);
  419. writel(0, host->base + MMCIMASK0);
  420. writel(0, host->base + MMCIMASK1);
  421. writel(0xfff, host->base + MMCICLEAR);
  422. ret = request_irq(dev->irq[0], mmci_irq, SA_SHIRQ, DRIVER_NAME " (cmd)", host);
  423. if (ret)
  424. goto unmap;
  425. ret = request_irq(dev->irq[1], mmci_pio_irq, SA_SHIRQ, DRIVER_NAME " (pio)", host);
  426. if (ret)
  427. goto irq0_free;
  428. writel(MCI_IRQENABLE, host->base + MMCIMASK0);
  429. amba_set_drvdata(dev, mmc);
  430. mmc_add_host(mmc);
  431. printk(KERN_INFO "%s: MMCI rev %x cfg %02x at 0x%08lx irq %d,%d\n",
  432. mmc_hostname(mmc), amba_rev(dev), amba_config(dev),
  433. dev->res.start, dev->irq[0], dev->irq[1]);
  434. init_timer(&host->timer);
  435. host->timer.data = (unsigned long)host;
  436. host->timer.function = mmci_check_status;
  437. host->timer.expires = jiffies + HZ;
  438. add_timer(&host->timer);
  439. return 0;
  440. irq0_free:
  441. free_irq(dev->irq[0], host);
  442. unmap:
  443. iounmap(host->base);
  444. clk_disable:
  445. clk_disable(host->clk);
  446. clk_unuse:
  447. clk_unuse(host->clk);
  448. clk_free:
  449. clk_put(host->clk);
  450. host_free:
  451. mmc_free_host(mmc);
  452. rel_regions:
  453. amba_release_regions(dev);
  454. out:
  455. return ret;
  456. }
  457. static int mmci_remove(struct amba_device *dev)
  458. {
  459. struct mmc_host *mmc = amba_get_drvdata(dev);
  460. amba_set_drvdata(dev, NULL);
  461. if (mmc) {
  462. struct mmci_host *host = mmc_priv(mmc);
  463. del_timer_sync(&host->timer);
  464. mmc_remove_host(mmc);
  465. writel(0, host->base + MMCIMASK0);
  466. writel(0, host->base + MMCIMASK1);
  467. writel(0, host->base + MMCICOMMAND);
  468. writel(0, host->base + MMCIDATACTRL);
  469. free_irq(dev->irq[0], host);
  470. free_irq(dev->irq[1], host);
  471. iounmap(host->base);
  472. clk_disable(host->clk);
  473. clk_unuse(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");