mmci.c 15 KB

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