pxamci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * linux/drivers/mmc/pxa.c - PXA MMCI driver
  3. *
  4. * Copyright (C) 2003 Russell King, 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. * This hardware is really sick:
  11. * - No way to clear interrupts.
  12. * - Have to turn off the clock whenever we touch the device.
  13. * - Doesn't tell you how many data blocks were transferred.
  14. * Yuck!
  15. *
  16. * 1 and 3 byte data transfers not supported
  17. * max block length up to 1023
  18. */
  19. #include <linux/config.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/ioport.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/delay.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/mmc/host.h>
  28. #include <linux/mmc/protocol.h>
  29. #include <asm/dma.h>
  30. #include <asm/io.h>
  31. #include <asm/scatterlist.h>
  32. #include <asm/sizes.h>
  33. #include <asm/arch/pxa-regs.h>
  34. #include <asm/arch/mmc.h>
  35. #include "pxamci.h"
  36. #ifdef CONFIG_MMC_DEBUG
  37. #define DBG(x...) printk(KERN_DEBUG x)
  38. #else
  39. #define DBG(x...) do { } while (0)
  40. #endif
  41. #define DRIVER_NAME "pxa2xx-mci"
  42. #define NR_SG 1
  43. struct pxamci_host {
  44. struct mmc_host *mmc;
  45. spinlock_t lock;
  46. struct resource *res;
  47. void __iomem *base;
  48. int irq;
  49. int dma;
  50. unsigned int clkrt;
  51. unsigned int cmdat;
  52. unsigned int imask;
  53. unsigned int power_mode;
  54. struct pxamci_platform_data *pdata;
  55. struct mmc_request *mrq;
  56. struct mmc_command *cmd;
  57. struct mmc_data *data;
  58. dma_addr_t sg_dma;
  59. struct pxa_dma_desc *sg_cpu;
  60. unsigned int dma_len;
  61. unsigned int dma_dir;
  62. };
  63. static inline unsigned int ns_to_clocks(unsigned int ns)
  64. {
  65. return (ns * (CLOCKRATE / 1000000) + 999) / 1000;
  66. }
  67. static void pxamci_stop_clock(struct pxamci_host *host)
  68. {
  69. if (readl(host->base + MMC_STAT) & STAT_CLK_EN) {
  70. unsigned long timeout = 10000;
  71. unsigned int v;
  72. writel(STOP_CLOCK, host->base + MMC_STRPCL);
  73. do {
  74. v = readl(host->base + MMC_STAT);
  75. if (!(v & STAT_CLK_EN))
  76. break;
  77. udelay(1);
  78. } while (timeout--);
  79. if (v & STAT_CLK_EN)
  80. dev_err(mmc_dev(host->mmc), "unable to stop clock\n");
  81. }
  82. }
  83. static void pxamci_enable_irq(struct pxamci_host *host, unsigned int mask)
  84. {
  85. unsigned long flags;
  86. spin_lock_irqsave(&host->lock, flags);
  87. host->imask &= ~mask;
  88. writel(host->imask, host->base + MMC_I_MASK);
  89. spin_unlock_irqrestore(&host->lock, flags);
  90. }
  91. static void pxamci_disable_irq(struct pxamci_host *host, unsigned int mask)
  92. {
  93. unsigned long flags;
  94. spin_lock_irqsave(&host->lock, flags);
  95. host->imask |= mask;
  96. writel(host->imask, host->base + MMC_I_MASK);
  97. spin_unlock_irqrestore(&host->lock, flags);
  98. }
  99. static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data)
  100. {
  101. unsigned int nob = data->blocks;
  102. unsigned int timeout;
  103. u32 dcmd;
  104. int i;
  105. host->data = data;
  106. if (data->flags & MMC_DATA_STREAM)
  107. nob = 0xffff;
  108. writel(nob, host->base + MMC_NOB);
  109. writel(1 << data->blksz_bits, host->base + MMC_BLKLEN);
  110. timeout = ns_to_clocks(data->timeout_ns) + data->timeout_clks;
  111. writel((timeout + 255) / 256, host->base + MMC_RDTO);
  112. if (data->flags & MMC_DATA_READ) {
  113. host->dma_dir = DMA_FROM_DEVICE;
  114. dcmd = DCMD_INCTRGADDR | DCMD_FLOWTRG;
  115. DRCMRTXMMC = 0;
  116. DRCMRRXMMC = host->dma | DRCMR_MAPVLD;
  117. } else {
  118. host->dma_dir = DMA_TO_DEVICE;
  119. dcmd = DCMD_INCSRCADDR | DCMD_FLOWSRC;
  120. DRCMRRXMMC = 0;
  121. DRCMRTXMMC = host->dma | DRCMR_MAPVLD;
  122. }
  123. dcmd |= DCMD_BURST32 | DCMD_WIDTH1;
  124. host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  125. host->dma_dir);
  126. for (i = 0; i < host->dma_len; i++) {
  127. if (data->flags & MMC_DATA_READ) {
  128. host->sg_cpu[i].dsadr = host->res->start + MMC_RXFIFO;
  129. host->sg_cpu[i].dtadr = sg_dma_address(&data->sg[i]);
  130. } else {
  131. host->sg_cpu[i].dsadr = sg_dma_address(&data->sg[i]);
  132. host->sg_cpu[i].dtadr = host->res->start + MMC_TXFIFO;
  133. }
  134. host->sg_cpu[i].dcmd = dcmd | sg_dma_len(&data->sg[i]);
  135. host->sg_cpu[i].ddadr = host->sg_dma + (i + 1) *
  136. sizeof(struct pxa_dma_desc);
  137. }
  138. host->sg_cpu[host->dma_len - 1].ddadr = DDADR_STOP;
  139. wmb();
  140. DDADR(host->dma) = host->sg_dma;
  141. DCSR(host->dma) = DCSR_RUN;
  142. }
  143. static void pxamci_start_cmd(struct pxamci_host *host, struct mmc_command *cmd, unsigned int cmdat)
  144. {
  145. WARN_ON(host->cmd != NULL);
  146. host->cmd = cmd;
  147. if (cmd->flags & MMC_RSP_BUSY)
  148. cmdat |= CMDAT_BUSY;
  149. switch (cmd->flags & (MMC_RSP_MASK | MMC_RSP_CRC)) {
  150. case MMC_RSP_SHORT | MMC_RSP_CRC:
  151. cmdat |= CMDAT_RESP_SHORT;
  152. break;
  153. case MMC_RSP_SHORT:
  154. cmdat |= CMDAT_RESP_R3;
  155. break;
  156. case MMC_RSP_LONG | MMC_RSP_CRC:
  157. cmdat |= CMDAT_RESP_R2;
  158. break;
  159. default:
  160. break;
  161. }
  162. writel(cmd->opcode, host->base + MMC_CMD);
  163. writel(cmd->arg >> 16, host->base + MMC_ARGH);
  164. writel(cmd->arg & 0xffff, host->base + MMC_ARGL);
  165. writel(cmdat, host->base + MMC_CMDAT);
  166. writel(host->clkrt, host->base + MMC_CLKRT);
  167. writel(START_CLOCK, host->base + MMC_STRPCL);
  168. pxamci_enable_irq(host, END_CMD_RES);
  169. }
  170. static void pxamci_finish_request(struct pxamci_host *host, struct mmc_request *mrq)
  171. {
  172. DBG("PXAMCI: request done\n");
  173. host->mrq = NULL;
  174. host->cmd = NULL;
  175. host->data = NULL;
  176. mmc_request_done(host->mmc, mrq);
  177. }
  178. static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat)
  179. {
  180. struct mmc_command *cmd = host->cmd;
  181. int i;
  182. u32 v;
  183. if (!cmd)
  184. return 0;
  185. host->cmd = NULL;
  186. /*
  187. * Did I mention this is Sick. We always need to
  188. * discard the upper 8 bits of the first 16-bit word.
  189. */
  190. v = readl(host->base + MMC_RES) & 0xffff;
  191. for (i = 0; i < 4; i++) {
  192. u32 w1 = readl(host->base + MMC_RES) & 0xffff;
  193. u32 w2 = readl(host->base + MMC_RES) & 0xffff;
  194. cmd->resp[i] = v << 24 | w1 << 8 | w2 >> 8;
  195. v = w2;
  196. }
  197. if (stat & STAT_TIME_OUT_RESPONSE) {
  198. cmd->error = MMC_ERR_TIMEOUT;
  199. } else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
  200. #ifdef CONFIG_PXA27x
  201. /*
  202. * workaround for erratum #42:
  203. * Intel PXA27x Family Processor Specification Update Rev 001
  204. */
  205. if (cmd->opcode == MMC_ALL_SEND_CID ||
  206. cmd->opcode == MMC_SEND_CSD ||
  207. cmd->opcode == MMC_SEND_CID) {
  208. /* a bogus CRC error can appear if the msb of
  209. the 15 byte response is a one */
  210. if ((cmd->resp[0] & 0x80000000) == 0)
  211. cmd->error = MMC_ERR_BADCRC;
  212. } else {
  213. DBG("ignoring CRC from command %d - *risky*\n",cmd->opcode);
  214. }
  215. #else
  216. cmd->error = MMC_ERR_BADCRC;
  217. #endif
  218. }
  219. pxamci_disable_irq(host, END_CMD_RES);
  220. if (host->data && cmd->error == MMC_ERR_NONE) {
  221. pxamci_enable_irq(host, DATA_TRAN_DONE);
  222. } else {
  223. pxamci_finish_request(host, host->mrq);
  224. }
  225. return 1;
  226. }
  227. static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
  228. {
  229. struct mmc_data *data = host->data;
  230. if (!data)
  231. return 0;
  232. DCSR(host->dma) = 0;
  233. dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
  234. host->dma_dir);
  235. if (stat & STAT_READ_TIME_OUT)
  236. data->error = MMC_ERR_TIMEOUT;
  237. else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR))
  238. data->error = MMC_ERR_BADCRC;
  239. /*
  240. * There appears to be a hardware design bug here. There seems to
  241. * be no way to find out how much data was transferred to the card.
  242. * This means that if there was an error on any block, we mark all
  243. * data blocks as being in error.
  244. */
  245. if (data->error == MMC_ERR_NONE)
  246. data->bytes_xfered = data->blocks << data->blksz_bits;
  247. else
  248. data->bytes_xfered = 0;
  249. pxamci_disable_irq(host, DATA_TRAN_DONE);
  250. host->data = NULL;
  251. if (host->mrq->stop && data->error == MMC_ERR_NONE) {
  252. pxamci_stop_clock(host);
  253. pxamci_start_cmd(host, host->mrq->stop, 0);
  254. } else {
  255. pxamci_finish_request(host, host->mrq);
  256. }
  257. return 1;
  258. }
  259. static irqreturn_t pxamci_irq(int irq, void *devid, struct pt_regs *regs)
  260. {
  261. struct pxamci_host *host = devid;
  262. unsigned int ireg;
  263. int handled = 0;
  264. ireg = readl(host->base + MMC_I_REG);
  265. DBG("PXAMCI: irq %08x\n", ireg);
  266. if (ireg) {
  267. unsigned stat = readl(host->base + MMC_STAT);
  268. DBG("PXAMCI: stat %08x\n", stat);
  269. if (ireg & END_CMD_RES)
  270. handled |= pxamci_cmd_done(host, stat);
  271. if (ireg & DATA_TRAN_DONE)
  272. handled |= pxamci_data_done(host, stat);
  273. }
  274. return IRQ_RETVAL(handled);
  275. }
  276. static void pxamci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  277. {
  278. struct pxamci_host *host = mmc_priv(mmc);
  279. unsigned int cmdat;
  280. WARN_ON(host->mrq != NULL);
  281. host->mrq = mrq;
  282. pxamci_stop_clock(host);
  283. cmdat = host->cmdat;
  284. host->cmdat &= ~CMDAT_INIT;
  285. if (mrq->data) {
  286. pxamci_setup_data(host, mrq->data);
  287. cmdat &= ~CMDAT_BUSY;
  288. cmdat |= CMDAT_DATAEN | CMDAT_DMAEN;
  289. if (mrq->data->flags & MMC_DATA_WRITE)
  290. cmdat |= CMDAT_WRITE;
  291. if (mrq->data->flags & MMC_DATA_STREAM)
  292. cmdat |= CMDAT_STREAM;
  293. }
  294. pxamci_start_cmd(host, mrq->cmd, cmdat);
  295. }
  296. static int pxamci_get_ro(struct mmc_host *mmc)
  297. {
  298. struct pxamci_host *host = mmc_priv(mmc);
  299. if (host->pdata && host->pdata->get_ro)
  300. return host->pdata->get_ro(mmc->dev);
  301. /* Host doesn't support read only detection so assume writeable */
  302. return 0;
  303. }
  304. static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  305. {
  306. struct pxamci_host *host = mmc_priv(mmc);
  307. DBG("pxamci_set_ios: clock %u power %u vdd %u.%02u\n",
  308. ios->clock, ios->power_mode, ios->vdd / 100,
  309. ios->vdd % 100);
  310. if (ios->clock) {
  311. unsigned int clk = CLOCKRATE / ios->clock;
  312. if (CLOCKRATE / clk > ios->clock)
  313. clk <<= 1;
  314. host->clkrt = fls(clk) - 1;
  315. pxa_set_cken(CKEN12_MMC, 1);
  316. /*
  317. * we write clkrt on the next command
  318. */
  319. } else {
  320. pxamci_stop_clock(host);
  321. pxa_set_cken(CKEN12_MMC, 0);
  322. }
  323. if (host->power_mode != ios->power_mode) {
  324. host->power_mode = ios->power_mode;
  325. if (host->pdata && host->pdata->setpower)
  326. host->pdata->setpower(mmc->dev, ios->vdd);
  327. if (ios->power_mode == MMC_POWER_ON)
  328. host->cmdat |= CMDAT_INIT;
  329. }
  330. DBG("pxamci_set_ios: clkrt = %x cmdat = %x\n",
  331. host->clkrt, host->cmdat);
  332. }
  333. static struct mmc_host_ops pxamci_ops = {
  334. .request = pxamci_request,
  335. .get_ro = pxamci_get_ro,
  336. .set_ios = pxamci_set_ios,
  337. };
  338. static void pxamci_dma_irq(int dma, void *devid, struct pt_regs *regs)
  339. {
  340. printk(KERN_ERR "DMA%d: IRQ???\n", dma);
  341. DCSR(dma) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
  342. }
  343. static irqreturn_t pxamci_detect_irq(int irq, void *devid, struct pt_regs *regs)
  344. {
  345. struct pxamci_host *host = mmc_priv(devid);
  346. mmc_detect_change(devid, host->pdata->detect_delay);
  347. return IRQ_HANDLED;
  348. }
  349. static int pxamci_probe(struct platform_device *pdev)
  350. {
  351. struct mmc_host *mmc;
  352. struct pxamci_host *host = NULL;
  353. struct resource *r;
  354. int ret, irq;
  355. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  356. irq = platform_get_irq(pdev, 0);
  357. if (!r || irq == NO_IRQ)
  358. return -ENXIO;
  359. r = request_mem_region(r->start, SZ_4K, DRIVER_NAME);
  360. if (!r)
  361. return -EBUSY;
  362. mmc = mmc_alloc_host(sizeof(struct pxamci_host), &pdev->dev);
  363. if (!mmc) {
  364. ret = -ENOMEM;
  365. goto out;
  366. }
  367. mmc->ops = &pxamci_ops;
  368. mmc->f_min = CLOCKRATE_MIN;
  369. mmc->f_max = CLOCKRATE_MAX;
  370. /*
  371. * We can do SG-DMA, but we don't because we never know how much
  372. * data we successfully wrote to the card.
  373. */
  374. mmc->max_phys_segs = NR_SG;
  375. /*
  376. * Our hardware DMA can handle a maximum of one page per SG entry.
  377. */
  378. mmc->max_seg_size = PAGE_SIZE;
  379. host = mmc_priv(mmc);
  380. host->mmc = mmc;
  381. host->dma = -1;
  382. host->pdata = pdev->dev.platform_data;
  383. mmc->ocr_avail = host->pdata ?
  384. host->pdata->ocr_mask :
  385. MMC_VDD_32_33|MMC_VDD_33_34;
  386. host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &host->sg_dma, GFP_KERNEL);
  387. if (!host->sg_cpu) {
  388. ret = -ENOMEM;
  389. goto out;
  390. }
  391. spin_lock_init(&host->lock);
  392. host->res = r;
  393. host->irq = irq;
  394. host->imask = MMC_I_MASK_ALL;
  395. host->base = ioremap(r->start, SZ_4K);
  396. if (!host->base) {
  397. ret = -ENOMEM;
  398. goto out;
  399. }
  400. /*
  401. * Ensure that the host controller is shut down, and setup
  402. * with our defaults.
  403. */
  404. pxamci_stop_clock(host);
  405. writel(0, host->base + MMC_SPI);
  406. writel(64, host->base + MMC_RESTO);
  407. writel(host->imask, host->base + MMC_I_MASK);
  408. host->dma = pxa_request_dma(DRIVER_NAME, DMA_PRIO_LOW,
  409. pxamci_dma_irq, host);
  410. if (host->dma < 0) {
  411. ret = -EBUSY;
  412. goto out;
  413. }
  414. ret = request_irq(host->irq, pxamci_irq, 0, DRIVER_NAME, host);
  415. if (ret)
  416. goto out;
  417. platform_set_drvdata(pdev, mmc);
  418. if (host->pdata && host->pdata->init)
  419. host->pdata->init(&pdev->dev, pxamci_detect_irq, mmc);
  420. mmc_add_host(mmc);
  421. return 0;
  422. out:
  423. if (host) {
  424. if (host->dma >= 0)
  425. pxa_free_dma(host->dma);
  426. if (host->base)
  427. iounmap(host->base);
  428. if (host->sg_cpu)
  429. dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
  430. }
  431. if (mmc)
  432. mmc_free_host(mmc);
  433. release_resource(r);
  434. return ret;
  435. }
  436. static int pxamci_remove(struct platform_device *pdev)
  437. {
  438. struct mmc_host *mmc = platform_get_drvdata(pdev);
  439. platform_set_drvdata(pdev, NULL);
  440. if (mmc) {
  441. struct pxamci_host *host = mmc_priv(mmc);
  442. if (host->pdata && host->pdata->exit)
  443. host->pdata->exit(&pdev->dev, mmc);
  444. mmc_remove_host(mmc);
  445. pxamci_stop_clock(host);
  446. writel(TXFIFO_WR_REQ|RXFIFO_RD_REQ|CLK_IS_OFF|STOP_CMD|
  447. END_CMD_RES|PRG_DONE|DATA_TRAN_DONE,
  448. host->base + MMC_I_MASK);
  449. DRCMRRXMMC = 0;
  450. DRCMRTXMMC = 0;
  451. free_irq(host->irq, host);
  452. pxa_free_dma(host->dma);
  453. iounmap(host->base);
  454. dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
  455. release_resource(host->res);
  456. mmc_free_host(mmc);
  457. }
  458. return 0;
  459. }
  460. #ifdef CONFIG_PM
  461. static int pxamci_suspend(struct platform_device *dev, pm_message_t state)
  462. {
  463. struct mmc_host *mmc = platform_get_drvdata(dev);
  464. int ret = 0;
  465. if (mmc)
  466. ret = mmc_suspend_host(mmc, state);
  467. return ret;
  468. }
  469. static int pxamci_resume(struct platform_device *dev)
  470. {
  471. struct mmc_host *mmc = platform_get_drvdata(dev);
  472. int ret = 0;
  473. if (mmc)
  474. ret = mmc_resume_host(mmc);
  475. return ret;
  476. }
  477. #else
  478. #define pxamci_suspend NULL
  479. #define pxamci_resume NULL
  480. #endif
  481. static struct platform_driver pxamci_driver = {
  482. .probe = pxamci_probe,
  483. .remove = pxamci_remove,
  484. .suspend = pxamci_suspend,
  485. .resume = pxamci_resume,
  486. .driver = {
  487. .name = DRIVER_NAME,
  488. },
  489. };
  490. static int __init pxamci_init(void)
  491. {
  492. return platform_driver_register(&pxamci_driver);
  493. }
  494. static void __exit pxamci_exit(void)
  495. {
  496. platform_driver_unregister(&pxamci_driver);
  497. }
  498. module_init(pxamci_init);
  499. module_exit(pxamci_exit);
  500. MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver");
  501. MODULE_LICENSE("GPL");