pxamci.c 14 KB

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