mmci.c 15 KB

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