pxamci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. #define RSP_TYPE(x) ((x) & ~(MMC_RSP_BUSY|MMC_RSP_OPCODE))
  150. switch (RSP_TYPE(mmc_resp_type(cmd))) {
  151. case RSP_TYPE(MMC_RSP_R1): /* r1, r1b, r6 */
  152. cmdat |= CMDAT_RESP_SHORT;
  153. break;
  154. case RSP_TYPE(MMC_RSP_R3):
  155. cmdat |= CMDAT_RESP_R3;
  156. break;
  157. case RSP_TYPE(MMC_RSP_R2):
  158. cmdat |= CMDAT_RESP_R2;
  159. break;
  160. default:
  161. break;
  162. }
  163. writel(cmd->opcode, host->base + MMC_CMD);
  164. writel(cmd->arg >> 16, host->base + MMC_ARGH);
  165. writel(cmd->arg & 0xffff, host->base + MMC_ARGL);
  166. writel(cmdat, host->base + MMC_CMDAT);
  167. writel(host->clkrt, host->base + MMC_CLKRT);
  168. writel(START_CLOCK, host->base + MMC_STRPCL);
  169. pxamci_enable_irq(host, END_CMD_RES);
  170. }
  171. static void pxamci_finish_request(struct pxamci_host *host, struct mmc_request *mrq)
  172. {
  173. DBG("PXAMCI: request done\n");
  174. host->mrq = NULL;
  175. host->cmd = NULL;
  176. host->data = NULL;
  177. mmc_request_done(host->mmc, mrq);
  178. }
  179. static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat)
  180. {
  181. struct mmc_command *cmd = host->cmd;
  182. int i;
  183. u32 v;
  184. if (!cmd)
  185. return 0;
  186. host->cmd = NULL;
  187. /*
  188. * Did I mention this is Sick. We always need to
  189. * discard the upper 8 bits of the first 16-bit word.
  190. */
  191. v = readl(host->base + MMC_RES) & 0xffff;
  192. for (i = 0; i < 4; i++) {
  193. u32 w1 = readl(host->base + MMC_RES) & 0xffff;
  194. u32 w2 = readl(host->base + MMC_RES) & 0xffff;
  195. cmd->resp[i] = v << 24 | w1 << 8 | w2 >> 8;
  196. v = w2;
  197. }
  198. if (stat & STAT_TIME_OUT_RESPONSE) {
  199. cmd->error = MMC_ERR_TIMEOUT;
  200. } else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
  201. #ifdef CONFIG_PXA27x
  202. /*
  203. * workaround for erratum #42:
  204. * Intel PXA27x Family Processor Specification Update Rev 001
  205. */
  206. if (cmd->opcode == MMC_ALL_SEND_CID ||
  207. cmd->opcode == MMC_SEND_CSD ||
  208. cmd->opcode == MMC_SEND_CID) {
  209. /* a bogus CRC error can appear if the msb of
  210. the 15 byte response is a one */
  211. if ((cmd->resp[0] & 0x80000000) == 0)
  212. cmd->error = MMC_ERR_BADCRC;
  213. } else {
  214. DBG("ignoring CRC from command %d - *risky*\n",cmd->opcode);
  215. }
  216. #else
  217. cmd->error = MMC_ERR_BADCRC;
  218. #endif
  219. }
  220. pxamci_disable_irq(host, END_CMD_RES);
  221. if (host->data && cmd->error == MMC_ERR_NONE) {
  222. pxamci_enable_irq(host, DATA_TRAN_DONE);
  223. } else {
  224. pxamci_finish_request(host, host->mrq);
  225. }
  226. return 1;
  227. }
  228. static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
  229. {
  230. struct mmc_data *data = host->data;
  231. if (!data)
  232. return 0;
  233. DCSR(host->dma) = 0;
  234. dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
  235. host->dma_dir);
  236. if (stat & STAT_READ_TIME_OUT)
  237. data->error = MMC_ERR_TIMEOUT;
  238. else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR))
  239. data->error = MMC_ERR_BADCRC;
  240. /*
  241. * There appears to be a hardware design bug here. There seems to
  242. * be no way to find out how much data was transferred to the card.
  243. * This means that if there was an error on any block, we mark all
  244. * data blocks as being in error.
  245. */
  246. if (data->error == MMC_ERR_NONE)
  247. data->bytes_xfered = data->blocks << data->blksz_bits;
  248. else
  249. data->bytes_xfered = 0;
  250. pxamci_disable_irq(host, DATA_TRAN_DONE);
  251. host->data = NULL;
  252. if (host->mrq->stop && data->error == MMC_ERR_NONE) {
  253. pxamci_stop_clock(host);
  254. pxamci_start_cmd(host, host->mrq->stop, 0);
  255. } else {
  256. pxamci_finish_request(host, host->mrq);
  257. }
  258. return 1;
  259. }
  260. static irqreturn_t pxamci_irq(int irq, void *devid, struct pt_regs *regs)
  261. {
  262. struct pxamci_host *host = devid;
  263. unsigned int ireg;
  264. int handled = 0;
  265. ireg = readl(host->base + MMC_I_REG);
  266. DBG("PXAMCI: irq %08x\n", ireg);
  267. if (ireg) {
  268. unsigned stat = readl(host->base + MMC_STAT);
  269. DBG("PXAMCI: stat %08x\n", stat);
  270. if (ireg & END_CMD_RES)
  271. handled |= pxamci_cmd_done(host, stat);
  272. if (ireg & DATA_TRAN_DONE)
  273. handled |= pxamci_data_done(host, stat);
  274. }
  275. return IRQ_RETVAL(handled);
  276. }
  277. static void pxamci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  278. {
  279. struct pxamci_host *host = mmc_priv(mmc);
  280. unsigned int cmdat;
  281. WARN_ON(host->mrq != NULL);
  282. host->mrq = mrq;
  283. pxamci_stop_clock(host);
  284. cmdat = host->cmdat;
  285. host->cmdat &= ~CMDAT_INIT;
  286. if (mrq->data) {
  287. pxamci_setup_data(host, mrq->data);
  288. cmdat &= ~CMDAT_BUSY;
  289. cmdat |= CMDAT_DATAEN | CMDAT_DMAEN;
  290. if (mrq->data->flags & MMC_DATA_WRITE)
  291. cmdat |= CMDAT_WRITE;
  292. if (mrq->data->flags & MMC_DATA_STREAM)
  293. cmdat |= CMDAT_STREAM;
  294. }
  295. pxamci_start_cmd(host, mrq->cmd, cmdat);
  296. }
  297. static int pxamci_get_ro(struct mmc_host *mmc)
  298. {
  299. struct pxamci_host *host = mmc_priv(mmc);
  300. if (host->pdata && host->pdata->get_ro)
  301. return host->pdata->get_ro(mmc->dev);
  302. /* Host doesn't support read only detection so assume writeable */
  303. return 0;
  304. }
  305. static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  306. {
  307. struct pxamci_host *host = mmc_priv(mmc);
  308. DBG("pxamci_set_ios: clock %u power %u vdd %u.%02u\n",
  309. ios->clock, ios->power_mode, ios->vdd / 100,
  310. ios->vdd % 100);
  311. if (ios->clock) {
  312. unsigned int clk = CLOCKRATE / ios->clock;
  313. if (CLOCKRATE / clk > ios->clock)
  314. clk <<= 1;
  315. host->clkrt = fls(clk) - 1;
  316. pxa_set_cken(CKEN12_MMC, 1);
  317. /*
  318. * we write clkrt on the next command
  319. */
  320. } else {
  321. pxamci_stop_clock(host);
  322. pxa_set_cken(CKEN12_MMC, 0);
  323. }
  324. if (host->power_mode != ios->power_mode) {
  325. host->power_mode = ios->power_mode;
  326. if (host->pdata && host->pdata->setpower)
  327. host->pdata->setpower(mmc->dev, ios->vdd);
  328. if (ios->power_mode == MMC_POWER_ON)
  329. host->cmdat |= CMDAT_INIT;
  330. }
  331. DBG("pxamci_set_ios: clkrt = %x cmdat = %x\n",
  332. host->clkrt, host->cmdat);
  333. }
  334. static struct mmc_host_ops pxamci_ops = {
  335. .request = pxamci_request,
  336. .get_ro = pxamci_get_ro,
  337. .set_ios = pxamci_set_ios,
  338. };
  339. static void pxamci_dma_irq(int dma, void *devid, struct pt_regs *regs)
  340. {
  341. printk(KERN_ERR "DMA%d: IRQ???\n", dma);
  342. DCSR(dma) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
  343. }
  344. static irqreturn_t pxamci_detect_irq(int irq, void *devid, struct pt_regs *regs)
  345. {
  346. struct pxamci_host *host = mmc_priv(devid);
  347. mmc_detect_change(devid, host->pdata->detect_delay);
  348. return IRQ_HANDLED;
  349. }
  350. static int pxamci_probe(struct platform_device *pdev)
  351. {
  352. struct mmc_host *mmc;
  353. struct pxamci_host *host = NULL;
  354. struct resource *r;
  355. int ret, irq;
  356. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  357. irq = platform_get_irq(pdev, 0);
  358. if (!r || irq == NO_IRQ)
  359. return -ENXIO;
  360. r = request_mem_region(r->start, SZ_4K, DRIVER_NAME);
  361. if (!r)
  362. return -EBUSY;
  363. mmc = mmc_alloc_host(sizeof(struct pxamci_host), &pdev->dev);
  364. if (!mmc) {
  365. ret = -ENOMEM;
  366. goto out;
  367. }
  368. mmc->ops = &pxamci_ops;
  369. mmc->f_min = CLOCKRATE_MIN;
  370. mmc->f_max = CLOCKRATE_MAX;
  371. /*
  372. * We can do SG-DMA, but we don't because we never know how much
  373. * data we successfully wrote to the card.
  374. */
  375. mmc->max_phys_segs = NR_SG;
  376. /*
  377. * Our hardware DMA can handle a maximum of one page per SG entry.
  378. */
  379. mmc->max_seg_size = PAGE_SIZE;
  380. host = mmc_priv(mmc);
  381. host->mmc = mmc;
  382. host->dma = -1;
  383. host->pdata = pdev->dev.platform_data;
  384. mmc->ocr_avail = host->pdata ?
  385. host->pdata->ocr_mask :
  386. MMC_VDD_32_33|MMC_VDD_33_34;
  387. host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &host->sg_dma, GFP_KERNEL);
  388. if (!host->sg_cpu) {
  389. ret = -ENOMEM;
  390. goto out;
  391. }
  392. spin_lock_init(&host->lock);
  393. host->res = r;
  394. host->irq = irq;
  395. host->imask = MMC_I_MASK_ALL;
  396. host->base = ioremap(r->start, SZ_4K);
  397. if (!host->base) {
  398. ret = -ENOMEM;
  399. goto out;
  400. }
  401. /*
  402. * Ensure that the host controller is shut down, and setup
  403. * with our defaults.
  404. */
  405. pxamci_stop_clock(host);
  406. writel(0, host->base + MMC_SPI);
  407. writel(64, host->base + MMC_RESTO);
  408. writel(host->imask, host->base + MMC_I_MASK);
  409. host->dma = pxa_request_dma(DRIVER_NAME, DMA_PRIO_LOW,
  410. pxamci_dma_irq, host);
  411. if (host->dma < 0) {
  412. ret = -EBUSY;
  413. goto out;
  414. }
  415. ret = request_irq(host->irq, pxamci_irq, 0, DRIVER_NAME, host);
  416. if (ret)
  417. goto out;
  418. platform_set_drvdata(pdev, mmc);
  419. if (host->pdata && host->pdata->init)
  420. host->pdata->init(&pdev->dev, pxamci_detect_irq, mmc);
  421. mmc_add_host(mmc);
  422. return 0;
  423. out:
  424. if (host) {
  425. if (host->dma >= 0)
  426. pxa_free_dma(host->dma);
  427. if (host->base)
  428. iounmap(host->base);
  429. if (host->sg_cpu)
  430. dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
  431. }
  432. if (mmc)
  433. mmc_free_host(mmc);
  434. release_resource(r);
  435. return ret;
  436. }
  437. static int pxamci_remove(struct platform_device *pdev)
  438. {
  439. struct mmc_host *mmc = platform_get_drvdata(pdev);
  440. platform_set_drvdata(pdev, NULL);
  441. if (mmc) {
  442. struct pxamci_host *host = mmc_priv(mmc);
  443. if (host->pdata && host->pdata->exit)
  444. host->pdata->exit(&pdev->dev, mmc);
  445. mmc_remove_host(mmc);
  446. pxamci_stop_clock(host);
  447. writel(TXFIFO_WR_REQ|RXFIFO_RD_REQ|CLK_IS_OFF|STOP_CMD|
  448. END_CMD_RES|PRG_DONE|DATA_TRAN_DONE,
  449. host->base + MMC_I_MASK);
  450. DRCMRRXMMC = 0;
  451. DRCMRTXMMC = 0;
  452. free_irq(host->irq, host);
  453. pxa_free_dma(host->dma);
  454. iounmap(host->base);
  455. dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
  456. release_resource(host->res);
  457. mmc_free_host(mmc);
  458. }
  459. return 0;
  460. }
  461. #ifdef CONFIG_PM
  462. static int pxamci_suspend(struct platform_device *dev, pm_message_t state)
  463. {
  464. struct mmc_host *mmc = platform_get_drvdata(dev);
  465. int ret = 0;
  466. if (mmc)
  467. ret = mmc_suspend_host(mmc, state);
  468. return ret;
  469. }
  470. static int pxamci_resume(struct platform_device *dev)
  471. {
  472. struct mmc_host *mmc = platform_get_drvdata(dev);
  473. int ret = 0;
  474. if (mmc)
  475. ret = mmc_resume_host(mmc);
  476. return ret;
  477. }
  478. #else
  479. #define pxamci_suspend NULL
  480. #define pxamci_resume NULL
  481. #endif
  482. static struct platform_driver pxamci_driver = {
  483. .probe = pxamci_probe,
  484. .remove = pxamci_remove,
  485. .suspend = pxamci_suspend,
  486. .resume = pxamci_resume,
  487. .driver = {
  488. .name = DRIVER_NAME,
  489. },
  490. };
  491. static int __init pxamci_init(void)
  492. {
  493. return platform_driver_register(&pxamci_driver);
  494. }
  495. static void __exit pxamci_exit(void)
  496. {
  497. platform_driver_unregister(&pxamci_driver);
  498. }
  499. module_init(pxamci_init);
  500. module_exit(pxamci_exit);
  501. MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver");
  502. MODULE_LICENSE("GPL");