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/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/irq.h>
  32. #include <asm/scatterlist.h>
  33. #include <asm/sizes.h>
  34. #include <asm/arch/pxa-regs.h>
  35. #include <asm/arch/mmc.h>
  36. #include "pxamci.h"
  37. #ifdef CONFIG_MMC_DEBUG
  38. #define DBG(x...) printk(KERN_DEBUG x)
  39. #else
  40. #define DBG(x...) do { } while (0)
  41. #endif
  42. #define DRIVER_NAME "pxa2xx-mci"
  43. #define NR_SG 1
  44. struct pxamci_host {
  45. struct mmc_host *mmc;
  46. spinlock_t lock;
  47. struct resource *res;
  48. void __iomem *base;
  49. int irq;
  50. int dma;
  51. unsigned int clkrt;
  52. unsigned int cmdat;
  53. unsigned int imask;
  54. unsigned int power_mode;
  55. struct pxamci_platform_data *pdata;
  56. struct mmc_request *mrq;
  57. struct mmc_command *cmd;
  58. struct mmc_data *data;
  59. dma_addr_t sg_dma;
  60. struct pxa_dma_desc *sg_cpu;
  61. unsigned int dma_len;
  62. unsigned int dma_dir;
  63. };
  64. static inline unsigned int ns_to_clocks(unsigned int ns)
  65. {
  66. return (ns * (CLOCKRATE / 1000000) + 999) / 1000;
  67. }
  68. static void pxamci_stop_clock(struct pxamci_host *host)
  69. {
  70. if (readl(host->base + MMC_STAT) & STAT_CLK_EN) {
  71. unsigned long timeout = 10000;
  72. unsigned int v;
  73. writel(STOP_CLOCK, host->base + MMC_STRPCL);
  74. do {
  75. v = readl(host->base + MMC_STAT);
  76. if (!(v & STAT_CLK_EN))
  77. break;
  78. udelay(1);
  79. } while (timeout--);
  80. if (v & STAT_CLK_EN)
  81. dev_err(mmc_dev(host->mmc), "unable to stop clock\n");
  82. }
  83. }
  84. static void pxamci_enable_irq(struct pxamci_host *host, unsigned int mask)
  85. {
  86. unsigned long flags;
  87. spin_lock_irqsave(&host->lock, flags);
  88. host->imask &= ~mask;
  89. writel(host->imask, host->base + MMC_I_MASK);
  90. spin_unlock_irqrestore(&host->lock, flags);
  91. }
  92. static void pxamci_disable_irq(struct pxamci_host *host, unsigned int mask)
  93. {
  94. unsigned long flags;
  95. spin_lock_irqsave(&host->lock, flags);
  96. host->imask |= mask;
  97. writel(host->imask, host->base + MMC_I_MASK);
  98. spin_unlock_irqrestore(&host->lock, flags);
  99. }
  100. static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data)
  101. {
  102. unsigned int nob = data->blocks;
  103. unsigned int timeout;
  104. u32 dcmd;
  105. int i;
  106. host->data = data;
  107. if (data->flags & MMC_DATA_STREAM)
  108. nob = 0xffff;
  109. writel(nob, host->base + MMC_NOB);
  110. writel(1 << data->blksz_bits, host->base + MMC_BLKLEN);
  111. timeout = ns_to_clocks(data->timeout_ns) + data->timeout_clks;
  112. writel((timeout + 255) / 256, host->base + MMC_RDTO);
  113. if (data->flags & MMC_DATA_READ) {
  114. host->dma_dir = DMA_FROM_DEVICE;
  115. dcmd = DCMD_INCTRGADDR | DCMD_FLOWTRG;
  116. DRCMRTXMMC = 0;
  117. DRCMRRXMMC = host->dma | DRCMR_MAPVLD;
  118. } else {
  119. host->dma_dir = DMA_TO_DEVICE;
  120. dcmd = DCMD_INCSRCADDR | DCMD_FLOWSRC;
  121. DRCMRRXMMC = 0;
  122. DRCMRTXMMC = host->dma | DRCMR_MAPVLD;
  123. }
  124. dcmd |= DCMD_BURST32 | DCMD_WIDTH1;
  125. host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  126. host->dma_dir);
  127. for (i = 0; i < host->dma_len; i++) {
  128. if (data->flags & MMC_DATA_READ) {
  129. host->sg_cpu[i].dsadr = host->res->start + MMC_RXFIFO;
  130. host->sg_cpu[i].dtadr = sg_dma_address(&data->sg[i]);
  131. } else {
  132. host->sg_cpu[i].dsadr = sg_dma_address(&data->sg[i]);
  133. host->sg_cpu[i].dtadr = host->res->start + MMC_TXFIFO;
  134. }
  135. host->sg_cpu[i].dcmd = dcmd | sg_dma_len(&data->sg[i]);
  136. host->sg_cpu[i].ddadr = host->sg_dma + (i + 1) *
  137. sizeof(struct pxa_dma_desc);
  138. }
  139. host->sg_cpu[host->dma_len - 1].ddadr = DDADR_STOP;
  140. wmb();
  141. DDADR(host->dma) = host->sg_dma;
  142. DCSR(host->dma) = DCSR_RUN;
  143. }
  144. static void pxamci_start_cmd(struct pxamci_host *host, struct mmc_command *cmd, unsigned int cmdat)
  145. {
  146. WARN_ON(host->cmd != NULL);
  147. host->cmd = cmd;
  148. if (cmd->flags & MMC_RSP_BUSY)
  149. cmdat |= CMDAT_BUSY;
  150. switch (cmd->flags & (MMC_RSP_MASK | MMC_RSP_CRC)) {
  151. case MMC_RSP_SHORT | MMC_RSP_CRC:
  152. cmdat |= CMDAT_RESP_SHORT;
  153. break;
  154. case MMC_RSP_SHORT:
  155. cmdat |= CMDAT_RESP_R3;
  156. break;
  157. case MMC_RSP_LONG | MMC_RSP_CRC:
  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 device *dev)
  351. {
  352. struct platform_device *pdev = to_platform_device(dev);
  353. struct mmc_host *mmc;
  354. struct pxamci_host *host = NULL;
  355. struct resource *r;
  356. int ret, irq;
  357. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  358. irq = platform_get_irq(pdev, 0);
  359. if (!r || irq == NO_IRQ)
  360. return -ENXIO;
  361. r = request_mem_region(r->start, SZ_4K, DRIVER_NAME);
  362. if (!r)
  363. return -EBUSY;
  364. mmc = mmc_alloc_host(sizeof(struct pxamci_host), dev);
  365. if (!mmc) {
  366. ret = -ENOMEM;
  367. goto out;
  368. }
  369. mmc->ops = &pxamci_ops;
  370. mmc->f_min = CLOCKRATE_MIN;
  371. mmc->f_max = CLOCKRATE_MAX;
  372. /*
  373. * We can do SG-DMA, but we don't because we never know how much
  374. * data we successfully wrote to the card.
  375. */
  376. mmc->max_phys_segs = NR_SG;
  377. /*
  378. * Our hardware DMA can handle a maximum of one page per SG entry.
  379. */
  380. mmc->max_seg_size = PAGE_SIZE;
  381. host = mmc_priv(mmc);
  382. host->mmc = mmc;
  383. host->dma = -1;
  384. host->pdata = pdev->dev.platform_data;
  385. mmc->ocr_avail = host->pdata ?
  386. host->pdata->ocr_mask :
  387. MMC_VDD_32_33|MMC_VDD_33_34;
  388. host->sg_cpu = dma_alloc_coherent(dev, PAGE_SIZE, &host->sg_dma, GFP_KERNEL);
  389. if (!host->sg_cpu) {
  390. ret = -ENOMEM;
  391. goto out;
  392. }
  393. spin_lock_init(&host->lock);
  394. host->res = r;
  395. host->irq = irq;
  396. host->imask = MMC_I_MASK_ALL;
  397. host->base = ioremap(r->start, SZ_4K);
  398. if (!host->base) {
  399. ret = -ENOMEM;
  400. goto out;
  401. }
  402. /*
  403. * Ensure that the host controller is shut down, and setup
  404. * with our defaults.
  405. */
  406. pxamci_stop_clock(host);
  407. writel(0, host->base + MMC_SPI);
  408. writel(64, host->base + MMC_RESTO);
  409. writel(host->imask, host->base + MMC_I_MASK);
  410. host->dma = pxa_request_dma(DRIVER_NAME, DMA_PRIO_LOW,
  411. pxamci_dma_irq, host);
  412. if (host->dma < 0) {
  413. ret = -EBUSY;
  414. goto out;
  415. }
  416. ret = request_irq(host->irq, pxamci_irq, 0, DRIVER_NAME, host);
  417. if (ret)
  418. goto out;
  419. dev_set_drvdata(dev, mmc);
  420. if (host->pdata && host->pdata->init)
  421. host->pdata->init(dev, pxamci_detect_irq, mmc);
  422. mmc_add_host(mmc);
  423. return 0;
  424. out:
  425. if (host) {
  426. if (host->dma >= 0)
  427. pxa_free_dma(host->dma);
  428. if (host->base)
  429. iounmap(host->base);
  430. if (host->sg_cpu)
  431. dma_free_coherent(dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
  432. }
  433. if (mmc)
  434. mmc_free_host(mmc);
  435. release_resource(r);
  436. return ret;
  437. }
  438. static int pxamci_remove(struct device *dev)
  439. {
  440. struct mmc_host *mmc = dev_get_drvdata(dev);
  441. dev_set_drvdata(dev, NULL);
  442. if (mmc) {
  443. struct pxamci_host *host = mmc_priv(mmc);
  444. if (host->pdata && host->pdata->exit)
  445. host->pdata->exit(dev, mmc);
  446. mmc_remove_host(mmc);
  447. pxamci_stop_clock(host);
  448. writel(TXFIFO_WR_REQ|RXFIFO_RD_REQ|CLK_IS_OFF|STOP_CMD|
  449. END_CMD_RES|PRG_DONE|DATA_TRAN_DONE,
  450. host->base + MMC_I_MASK);
  451. DRCMRRXMMC = 0;
  452. DRCMRTXMMC = 0;
  453. free_irq(host->irq, host);
  454. pxa_free_dma(host->dma);
  455. iounmap(host->base);
  456. dma_free_coherent(dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
  457. release_resource(host->res);
  458. mmc_free_host(mmc);
  459. }
  460. return 0;
  461. }
  462. #ifdef CONFIG_PM
  463. static int pxamci_suspend(struct device *dev, pm_message_t state, u32 level)
  464. {
  465. struct mmc_host *mmc = dev_get_drvdata(dev);
  466. int ret = 0;
  467. if (mmc && level == SUSPEND_DISABLE)
  468. ret = mmc_suspend_host(mmc, state);
  469. return ret;
  470. }
  471. static int pxamci_resume(struct device *dev, u32 level)
  472. {
  473. struct mmc_host *mmc = dev_get_drvdata(dev);
  474. int ret = 0;
  475. if (mmc && level == RESUME_ENABLE)
  476. ret = mmc_resume_host(mmc);
  477. return ret;
  478. }
  479. #else
  480. #define pxamci_suspend NULL
  481. #define pxamci_resume NULL
  482. #endif
  483. static struct device_driver pxamci_driver = {
  484. .name = DRIVER_NAME,
  485. .bus = &platform_bus_type,
  486. .probe = pxamci_probe,
  487. .remove = pxamci_remove,
  488. .suspend = pxamci_suspend,
  489. .resume = pxamci_resume,
  490. };
  491. static int __init pxamci_init(void)
  492. {
  493. return driver_register(&pxamci_driver);
  494. }
  495. static void __exit pxamci_exit(void)
  496. {
  497. 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");