mmci.c 15 KB

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