pxamci.c 14 KB

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