mmci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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/amba/bus.h>
  21. #include <linux/clk.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/div64.h>
  24. #include <asm/io.h>
  25. #include <asm/scatterlist.h>
  26. #include <asm/sizes.h>
  27. #include <asm/mach/mmc.h>
  28. #include "mmci.h"
  29. #define DRIVER_NAME "mmci-pl18x"
  30. #define DBG(host,fmt,args...) \
  31. pr_debug("%s: %s: " fmt, mmc_hostname(host->mmc), __func__ , args)
  32. static unsigned int fmax = 515633;
  33. static void
  34. mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
  35. {
  36. writel(0, host->base + MMCICOMMAND);
  37. BUG_ON(host->data);
  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. int blksz_bits;
  62. DBG(host, "blksz %04x blks %04x flags %08x\n",
  63. data->blksz, data->blocks, data->flags);
  64. host->data = data;
  65. host->size = data->blksz;
  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. blksz_bits = ffs(data->blksz) - 1;
  75. BUG_ON(1 << blksz_bits != data->blksz);
  76. datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
  77. if (data->flags & MMC_DATA_READ) {
  78. datactrl |= MCI_DPSM_DIRECTION;
  79. irqmask = MCI_RXFIFOHALFFULLMASK;
  80. /*
  81. * If we have less than a FIFOSIZE of bytes to transfer,
  82. * trigger a PIO interrupt as soon as any data is available.
  83. */
  84. if (host->size < MCI_FIFOSIZE)
  85. irqmask |= MCI_RXDATAAVLBLMASK;
  86. } else {
  87. /*
  88. * We don't actually need to include "FIFO empty" here
  89. * since its implicit in "FIFO half empty".
  90. */
  91. irqmask = MCI_TXFIFOHALFEMPTYMASK;
  92. }
  93. writel(datactrl, base + MMCIDATACTRL);
  94. writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
  95. writel(irqmask, base + MMCIMASK1);
  96. }
  97. static void
  98. mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
  99. {
  100. void __iomem *base = host->base;
  101. DBG(host, "op %02x arg %08x flags %08x\n",
  102. cmd->opcode, cmd->arg, cmd->flags);
  103. if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
  104. writel(0, base + MMCICOMMAND);
  105. udelay(1);
  106. }
  107. c |= cmd->opcode | MCI_CPSM_ENABLE;
  108. if (cmd->flags & MMC_RSP_PRESENT) {
  109. if (cmd->flags & MMC_RSP_136)
  110. c |= MCI_CPSM_LONGRSP;
  111. c |= MCI_CPSM_RESPONSE;
  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 += data->blksz;
  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. * We hit an error condition. Ensure that any data
  136. * partially written to a page is properly coherent.
  137. */
  138. if (host->sg_len && data->flags & MMC_DATA_READ)
  139. flush_dcache_page(host->sg_ptr->page);
  140. }
  141. if (status & MCI_DATAEND) {
  142. mmci_stop_data(host);
  143. if (!data->stop) {
  144. mmci_request_end(host, data->mrq);
  145. } else {
  146. mmci_start_command(host, data->stop, 0);
  147. }
  148. }
  149. }
  150. static void
  151. mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
  152. unsigned int status)
  153. {
  154. void __iomem *base = host->base;
  155. host->cmd = NULL;
  156. cmd->resp[0] = readl(base + MMCIRESPONSE0);
  157. cmd->resp[1] = readl(base + MMCIRESPONSE1);
  158. cmd->resp[2] = readl(base + MMCIRESPONSE2);
  159. cmd->resp[3] = readl(base + MMCIRESPONSE3);
  160. if (status & MCI_CMDTIMEOUT) {
  161. cmd->error = MMC_ERR_TIMEOUT;
  162. } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
  163. cmd->error = MMC_ERR_BADCRC;
  164. }
  165. if (!cmd->data || cmd->error != MMC_ERR_NONE) {
  166. if (host->data)
  167. mmci_stop_data(host);
  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)
  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)
  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. if (ios->clock) {
  324. if (ios->clock >= host->mclk) {
  325. clk = MCI_CLK_BYPASS;
  326. host->cclk = host->mclk;
  327. } else {
  328. clk = host->mclk / (2 * ios->clock) - 1;
  329. if (clk > 256)
  330. clk = 255;
  331. host->cclk = host->mclk / (2 * (clk + 1));
  332. }
  333. clk |= MCI_CLK_ENABLE;
  334. }
  335. if (host->plat->translate_vdd)
  336. pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
  337. switch (ios->power_mode) {
  338. case MMC_POWER_OFF:
  339. break;
  340. case MMC_POWER_UP:
  341. pwr |= MCI_PWR_UP;
  342. break;
  343. case MMC_POWER_ON:
  344. pwr |= MCI_PWR_ON;
  345. break;
  346. }
  347. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  348. pwr |= MCI_ROD;
  349. writel(clk, host->base + MMCICLOCK);
  350. if (host->pwr != pwr) {
  351. host->pwr = pwr;
  352. writel(pwr, host->base + MMCIPOWER);
  353. }
  354. }
  355. static const struct mmc_host_ops mmci_ops = {
  356. .request = mmci_request,
  357. .set_ios = mmci_set_ios,
  358. };
  359. static void mmci_check_status(unsigned long data)
  360. {
  361. struct mmci_host *host = (struct mmci_host *)data;
  362. unsigned int status;
  363. status = host->plat->status(mmc_dev(host->mmc));
  364. if (status ^ host->oldstat)
  365. mmc_detect_change(host->mmc, 0);
  366. host->oldstat = status;
  367. mod_timer(&host->timer, jiffies + HZ);
  368. }
  369. static int mmci_probe(struct amba_device *dev, void *id)
  370. {
  371. struct mmc_platform_data *plat = dev->dev.platform_data;
  372. struct mmci_host *host;
  373. struct mmc_host *mmc;
  374. int ret;
  375. /* must have platform data */
  376. if (!plat) {
  377. ret = -EINVAL;
  378. goto out;
  379. }
  380. ret = amba_request_regions(dev, DRIVER_NAME);
  381. if (ret)
  382. goto out;
  383. mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
  384. if (!mmc) {
  385. ret = -ENOMEM;
  386. goto rel_regions;
  387. }
  388. host = mmc_priv(mmc);
  389. host->clk = clk_get(&dev->dev, "MCLK");
  390. if (IS_ERR(host->clk)) {
  391. ret = PTR_ERR(host->clk);
  392. host->clk = NULL;
  393. goto host_free;
  394. }
  395. ret = clk_enable(host->clk);
  396. if (ret)
  397. goto clk_free;
  398. host->plat = plat;
  399. host->mclk = clk_get_rate(host->clk);
  400. host->mmc = mmc;
  401. host->base = ioremap(dev->res.start, SZ_4K);
  402. if (!host->base) {
  403. ret = -ENOMEM;
  404. goto clk_disable;
  405. }
  406. mmc->ops = &mmci_ops;
  407. mmc->f_min = (host->mclk + 511) / 512;
  408. mmc->f_max = min(host->mclk, fmax);
  409. mmc->ocr_avail = plat->ocr_mask;
  410. mmc->caps = MMC_CAP_MULTIWRITE;
  411. /*
  412. * We can do SGIO
  413. */
  414. mmc->max_hw_segs = 16;
  415. mmc->max_phys_segs = NR_SG;
  416. /*
  417. * Since we only have a 16-bit data length register, we must
  418. * ensure that we don't exceed 2^16-1 bytes in a single request.
  419. */
  420. mmc->max_req_size = 65535;
  421. /*
  422. * Set the maximum segment size. Since we aren't doing DMA
  423. * (yet) we are only limited by the data length register.
  424. */
  425. mmc->max_seg_size = mmc->max_req_size;
  426. /*
  427. * Block size can be up to 2048 bytes, but must be a power of two.
  428. */
  429. mmc->max_blk_size = 2048;
  430. /*
  431. * No limit on the number of blocks transferred.
  432. */
  433. mmc->max_blk_count = mmc->max_req_size;
  434. spin_lock_init(&host->lock);
  435. writel(0, host->base + MMCIMASK0);
  436. writel(0, host->base + MMCIMASK1);
  437. writel(0xfff, host->base + MMCICLEAR);
  438. ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
  439. if (ret)
  440. goto unmap;
  441. ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED, DRIVER_NAME " (pio)", host);
  442. if (ret)
  443. goto irq0_free;
  444. writel(MCI_IRQENABLE, host->base + MMCIMASK0);
  445. amba_set_drvdata(dev, mmc);
  446. mmc_add_host(mmc);
  447. printk(KERN_INFO "%s: MMCI rev %x cfg %02x at 0x%016llx irq %d,%d\n",
  448. mmc_hostname(mmc), amba_rev(dev), amba_config(dev),
  449. (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
  450. init_timer(&host->timer);
  451. host->timer.data = (unsigned long)host;
  452. host->timer.function = mmci_check_status;
  453. host->timer.expires = jiffies + HZ;
  454. add_timer(&host->timer);
  455. return 0;
  456. irq0_free:
  457. free_irq(dev->irq[0], host);
  458. unmap:
  459. iounmap(host->base);
  460. clk_disable:
  461. clk_disable(host->clk);
  462. clk_free:
  463. clk_put(host->clk);
  464. host_free:
  465. mmc_free_host(mmc);
  466. rel_regions:
  467. amba_release_regions(dev);
  468. out:
  469. return ret;
  470. }
  471. static int mmci_remove(struct amba_device *dev)
  472. {
  473. struct mmc_host *mmc = amba_get_drvdata(dev);
  474. amba_set_drvdata(dev, NULL);
  475. if (mmc) {
  476. struct mmci_host *host = mmc_priv(mmc);
  477. del_timer_sync(&host->timer);
  478. mmc_remove_host(mmc);
  479. writel(0, host->base + MMCIMASK0);
  480. writel(0, host->base + MMCIMASK1);
  481. writel(0, host->base + MMCICOMMAND);
  482. writel(0, host->base + MMCIDATACTRL);
  483. free_irq(dev->irq[0], host);
  484. free_irq(dev->irq[1], host);
  485. iounmap(host->base);
  486. clk_disable(host->clk);
  487. clk_put(host->clk);
  488. mmc_free_host(mmc);
  489. amba_release_regions(dev);
  490. }
  491. return 0;
  492. }
  493. #ifdef CONFIG_PM
  494. static int mmci_suspend(struct amba_device *dev, pm_message_t state)
  495. {
  496. struct mmc_host *mmc = amba_get_drvdata(dev);
  497. int ret = 0;
  498. if (mmc) {
  499. struct mmci_host *host = mmc_priv(mmc);
  500. ret = mmc_suspend_host(mmc, state);
  501. if (ret == 0)
  502. writel(0, host->base + MMCIMASK0);
  503. }
  504. return ret;
  505. }
  506. static int mmci_resume(struct amba_device *dev)
  507. {
  508. struct mmc_host *mmc = amba_get_drvdata(dev);
  509. int ret = 0;
  510. if (mmc) {
  511. struct mmci_host *host = mmc_priv(mmc);
  512. writel(MCI_IRQENABLE, host->base + MMCIMASK0);
  513. ret = mmc_resume_host(mmc);
  514. }
  515. return ret;
  516. }
  517. #else
  518. #define mmci_suspend NULL
  519. #define mmci_resume NULL
  520. #endif
  521. static struct amba_id mmci_ids[] = {
  522. {
  523. .id = 0x00041180,
  524. .mask = 0x000fffff,
  525. },
  526. {
  527. .id = 0x00041181,
  528. .mask = 0x000fffff,
  529. },
  530. { 0, 0 },
  531. };
  532. static struct amba_driver mmci_driver = {
  533. .drv = {
  534. .name = DRIVER_NAME,
  535. },
  536. .probe = mmci_probe,
  537. .remove = mmci_remove,
  538. .suspend = mmci_suspend,
  539. .resume = mmci_resume,
  540. .id_table = mmci_ids,
  541. };
  542. static int __init mmci_init(void)
  543. {
  544. return amba_driver_register(&mmci_driver);
  545. }
  546. static void __exit mmci_exit(void)
  547. {
  548. amba_driver_unregister(&mmci_driver);
  549. }
  550. module_init(mmci_init);
  551. module_exit(mmci_exit);
  552. module_param(fmax, uint, 0444);
  553. MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
  554. MODULE_LICENSE("GPL");