pxamci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. unsigned int length = sg_dma_len(&data->sg[i]);
  120. host->sg_cpu[i].dcmd = dcmd | length;
  121. if (length & 31 && !(data->flags & MMC_DATA_READ))
  122. host->sg_cpu[i].dcmd |= DCMD_ENDIRQEN;
  123. if (data->flags & MMC_DATA_READ) {
  124. host->sg_cpu[i].dsadr = host->res->start + MMC_RXFIFO;
  125. host->sg_cpu[i].dtadr = sg_dma_address(&data->sg[i]);
  126. } else {
  127. host->sg_cpu[i].dsadr = sg_dma_address(&data->sg[i]);
  128. host->sg_cpu[i].dtadr = host->res->start + MMC_TXFIFO;
  129. }
  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, r7 */
  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. host->mrq = NULL;
  169. host->cmd = NULL;
  170. host->data = NULL;
  171. mmc_request_done(host->mmc, mrq);
  172. }
  173. static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat)
  174. {
  175. struct mmc_command *cmd = host->cmd;
  176. int i;
  177. u32 v;
  178. if (!cmd)
  179. return 0;
  180. host->cmd = NULL;
  181. /*
  182. * Did I mention this is Sick. We always need to
  183. * discard the upper 8 bits of the first 16-bit word.
  184. */
  185. v = readl(host->base + MMC_RES) & 0xffff;
  186. for (i = 0; i < 4; i++) {
  187. u32 w1 = readl(host->base + MMC_RES) & 0xffff;
  188. u32 w2 = readl(host->base + MMC_RES) & 0xffff;
  189. cmd->resp[i] = v << 24 | w1 << 8 | w2 >> 8;
  190. v = w2;
  191. }
  192. if (stat & STAT_TIME_OUT_RESPONSE) {
  193. cmd->error = -ETIMEDOUT;
  194. } else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
  195. #ifdef CONFIG_PXA27x
  196. /*
  197. * workaround for erratum #42:
  198. * Intel PXA27x Family Processor Specification Update Rev 001
  199. * A bogus CRC error can appear if the msb of a 136 bit
  200. * response is a one.
  201. */
  202. if (cmd->flags & MMC_RSP_136 && cmd->resp[0] & 0x80000000) {
  203. pr_debug("ignoring CRC from command %d - *risky*\n", cmd->opcode);
  204. } else
  205. #endif
  206. cmd->error = -EILSEQ;
  207. }
  208. pxamci_disable_irq(host, END_CMD_RES);
  209. if (host->data && !cmd->error) {
  210. pxamci_enable_irq(host, DATA_TRAN_DONE);
  211. } else {
  212. pxamci_finish_request(host, host->mrq);
  213. }
  214. return 1;
  215. }
  216. static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
  217. {
  218. struct mmc_data *data = host->data;
  219. if (!data)
  220. return 0;
  221. DCSR(host->dma) = 0;
  222. dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
  223. host->dma_dir);
  224. if (stat & STAT_READ_TIME_OUT)
  225. data->error = -ETIMEDOUT;
  226. else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR))
  227. data->error = -EILSEQ;
  228. /*
  229. * There appears to be a hardware design bug here. There seems to
  230. * be no way to find out how much data was transferred to the card.
  231. * This means that if there was an error on any block, we mark all
  232. * data blocks as being in error.
  233. */
  234. if (!data->error)
  235. data->bytes_xfered = data->blocks * data->blksz;
  236. else
  237. data->bytes_xfered = 0;
  238. pxamci_disable_irq(host, DATA_TRAN_DONE);
  239. host->data = NULL;
  240. if (host->mrq->stop) {
  241. pxamci_stop_clock(host);
  242. pxamci_start_cmd(host, host->mrq->stop, host->cmdat);
  243. } else {
  244. pxamci_finish_request(host, host->mrq);
  245. }
  246. return 1;
  247. }
  248. static irqreturn_t pxamci_irq(int irq, void *devid)
  249. {
  250. struct pxamci_host *host = devid;
  251. unsigned int ireg;
  252. int handled = 0;
  253. ireg = readl(host->base + MMC_I_REG) & ~readl(host->base + MMC_I_MASK);
  254. if (ireg) {
  255. unsigned stat = readl(host->base + MMC_STAT);
  256. pr_debug("PXAMCI: irq %08x stat %08x\n", ireg, stat);
  257. if (ireg & END_CMD_RES)
  258. handled |= pxamci_cmd_done(host, stat);
  259. if (ireg & DATA_TRAN_DONE)
  260. handled |= pxamci_data_done(host, stat);
  261. if (ireg & SDIO_INT) {
  262. mmc_signal_sdio_irq(host->mmc);
  263. handled = 1;
  264. }
  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(mmc));
  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(CKEN_MMC, 1);
  305. /*
  306. * we write clkrt on the next command
  307. */
  308. } else {
  309. pxamci_stop_clock(host);
  310. pxa_set_cken(CKEN_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(mmc), ios->vdd);
  316. if (ios->power_mode == MMC_POWER_ON)
  317. host->cmdat |= CMDAT_INIT;
  318. }
  319. if (ios->bus_width == MMC_BUS_WIDTH_4)
  320. host->cmdat |= CMDAT_SD_4DAT;
  321. else
  322. host->cmdat &= ~CMDAT_SD_4DAT;
  323. pr_debug("PXAMCI: clkrt = %x cmdat = %x\n",
  324. host->clkrt, host->cmdat);
  325. }
  326. static void pxamci_enable_sdio_irq(struct mmc_host *host, int enable)
  327. {
  328. struct pxamci_host *pxa_host = mmc_priv(host);
  329. if (enable)
  330. pxamci_enable_irq(pxa_host, SDIO_INT);
  331. else
  332. pxamci_disable_irq(pxa_host, SDIO_INT);
  333. }
  334. static const struct mmc_host_ops pxamci_ops = {
  335. .request = pxamci_request,
  336. .get_ro = pxamci_get_ro,
  337. .set_ios = pxamci_set_ios,
  338. .enable_sdio_irq = pxamci_enable_sdio_irq,
  339. };
  340. static void pxamci_dma_irq(int dma, void *devid)
  341. {
  342. struct pxamci_host *host = devid;
  343. int dcsr = DCSR(dma);
  344. DCSR(dma) = dcsr & ~DCSR_STOPIRQEN;
  345. if (dcsr & DCSR_ENDINTR) {
  346. writel(BUF_PART_FULL, host->base + MMC_PRTBUF);
  347. } else {
  348. printk(KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n",
  349. mmc_hostname(host->mmc), dma, dcsr);
  350. host->data->error = -EIO;
  351. pxamci_data_done(host, 0);
  352. }
  353. }
  354. static irqreturn_t pxamci_detect_irq(int irq, void *devid)
  355. {
  356. struct pxamci_host *host = mmc_priv(devid);
  357. mmc_detect_change(devid, host->pdata->detect_delay);
  358. return IRQ_HANDLED;
  359. }
  360. static int pxamci_probe(struct platform_device *pdev)
  361. {
  362. struct mmc_host *mmc;
  363. struct pxamci_host *host = NULL;
  364. struct resource *r;
  365. int ret, irq;
  366. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  367. irq = platform_get_irq(pdev, 0);
  368. if (!r || irq < 0)
  369. return -ENXIO;
  370. r = request_mem_region(r->start, SZ_4K, DRIVER_NAME);
  371. if (!r)
  372. return -EBUSY;
  373. mmc = mmc_alloc_host(sizeof(struct pxamci_host), &pdev->dev);
  374. if (!mmc) {
  375. ret = -ENOMEM;
  376. goto out;
  377. }
  378. mmc->ops = &pxamci_ops;
  379. mmc->f_min = CLOCKRATE_MIN;
  380. mmc->f_max = CLOCKRATE_MAX;
  381. /*
  382. * We can do SG-DMA, but we don't because we never know how much
  383. * data we successfully wrote to the card.
  384. */
  385. mmc->max_phys_segs = NR_SG;
  386. /*
  387. * Our hardware DMA can handle a maximum of one page per SG entry.
  388. */
  389. mmc->max_seg_size = PAGE_SIZE;
  390. /*
  391. * Block length register is only 10 bits before PXA27x.
  392. */
  393. mmc->max_blk_size = (cpu_is_pxa21x() || cpu_is_pxa25x()) ? 1023 : 2048;
  394. /*
  395. * Block count register is 16 bits.
  396. */
  397. mmc->max_blk_count = 65535;
  398. host = mmc_priv(mmc);
  399. host->mmc = mmc;
  400. host->dma = -1;
  401. host->pdata = pdev->dev.platform_data;
  402. mmc->ocr_avail = host->pdata ?
  403. host->pdata->ocr_mask :
  404. MMC_VDD_32_33|MMC_VDD_33_34;
  405. mmc->caps = 0;
  406. host->cmdat = 0;
  407. if (!cpu_is_pxa21x() && !cpu_is_pxa25x()) {
  408. mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
  409. host->cmdat |= CMDAT_SDIO_INT_EN;
  410. }
  411. host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &host->sg_dma, GFP_KERNEL);
  412. if (!host->sg_cpu) {
  413. ret = -ENOMEM;
  414. goto out;
  415. }
  416. spin_lock_init(&host->lock);
  417. host->res = r;
  418. host->irq = irq;
  419. host->imask = MMC_I_MASK_ALL;
  420. host->base = ioremap(r->start, SZ_4K);
  421. if (!host->base) {
  422. ret = -ENOMEM;
  423. goto out;
  424. }
  425. /*
  426. * Ensure that the host controller is shut down, and setup
  427. * with our defaults.
  428. */
  429. pxamci_stop_clock(host);
  430. writel(0, host->base + MMC_SPI);
  431. writel(64, host->base + MMC_RESTO);
  432. writel(host->imask, host->base + MMC_I_MASK);
  433. host->dma = pxa_request_dma(DRIVER_NAME, DMA_PRIO_LOW,
  434. pxamci_dma_irq, host);
  435. if (host->dma < 0) {
  436. ret = -EBUSY;
  437. goto out;
  438. }
  439. ret = request_irq(host->irq, pxamci_irq, 0, DRIVER_NAME, host);
  440. if (ret)
  441. goto out;
  442. platform_set_drvdata(pdev, mmc);
  443. if (host->pdata && host->pdata->init)
  444. host->pdata->init(&pdev->dev, pxamci_detect_irq, mmc);
  445. mmc_add_host(mmc);
  446. return 0;
  447. out:
  448. if (host) {
  449. if (host->dma >= 0)
  450. pxa_free_dma(host->dma);
  451. if (host->base)
  452. iounmap(host->base);
  453. if (host->sg_cpu)
  454. dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
  455. }
  456. if (mmc)
  457. mmc_free_host(mmc);
  458. release_resource(r);
  459. return ret;
  460. }
  461. static int pxamci_remove(struct platform_device *pdev)
  462. {
  463. struct mmc_host *mmc = platform_get_drvdata(pdev);
  464. platform_set_drvdata(pdev, NULL);
  465. if (mmc) {
  466. struct pxamci_host *host = mmc_priv(mmc);
  467. if (host->pdata && host->pdata->exit)
  468. host->pdata->exit(&pdev->dev, mmc);
  469. mmc_remove_host(mmc);
  470. pxamci_stop_clock(host);
  471. writel(TXFIFO_WR_REQ|RXFIFO_RD_REQ|CLK_IS_OFF|STOP_CMD|
  472. END_CMD_RES|PRG_DONE|DATA_TRAN_DONE,
  473. host->base + MMC_I_MASK);
  474. DRCMRRXMMC = 0;
  475. DRCMRTXMMC = 0;
  476. free_irq(host->irq, host);
  477. pxa_free_dma(host->dma);
  478. iounmap(host->base);
  479. dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
  480. release_resource(host->res);
  481. mmc_free_host(mmc);
  482. }
  483. return 0;
  484. }
  485. #ifdef CONFIG_PM
  486. static int pxamci_suspend(struct platform_device *dev, pm_message_t state)
  487. {
  488. struct mmc_host *mmc = platform_get_drvdata(dev);
  489. int ret = 0;
  490. if (mmc)
  491. ret = mmc_suspend_host(mmc, state);
  492. return ret;
  493. }
  494. static int pxamci_resume(struct platform_device *dev)
  495. {
  496. struct mmc_host *mmc = platform_get_drvdata(dev);
  497. int ret = 0;
  498. if (mmc)
  499. ret = mmc_resume_host(mmc);
  500. return ret;
  501. }
  502. #else
  503. #define pxamci_suspend NULL
  504. #define pxamci_resume NULL
  505. #endif
  506. static struct platform_driver pxamci_driver = {
  507. .probe = pxamci_probe,
  508. .remove = pxamci_remove,
  509. .suspend = pxamci_suspend,
  510. .resume = pxamci_resume,
  511. .driver = {
  512. .name = DRIVER_NAME,
  513. },
  514. };
  515. static int __init pxamci_init(void)
  516. {
  517. return platform_driver_register(&pxamci_driver);
  518. }
  519. static void __exit pxamci_exit(void)
  520. {
  521. platform_driver_unregister(&pxamci_driver);
  522. }
  523. module_init(pxamci_init);
  524. module_exit(pxamci_exit);
  525. MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver");
  526. MODULE_LICENSE("GPL");