pxamci.c 14 KB

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