pxamci.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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/clk.h>
  27. #include <linux/err.h>
  28. #include <linux/mmc/host.h>
  29. #include <linux/io.h>
  30. #include <asm/sizes.h>
  31. #include <mach/hardware.h>
  32. #include <mach/dma.h>
  33. #include <mach/mmc.h>
  34. #include "pxamci.h"
  35. #define DRIVER_NAME "pxa2xx-mci"
  36. #define NR_SG 1
  37. #define CLKRT_OFF (~0)
  38. struct pxamci_host {
  39. struct mmc_host *mmc;
  40. spinlock_t lock;
  41. struct resource *res;
  42. void __iomem *base;
  43. struct clk *clk;
  44. unsigned long clkrate;
  45. int irq;
  46. int dma;
  47. unsigned int clkrt;
  48. unsigned int cmdat;
  49. unsigned int imask;
  50. unsigned int power_mode;
  51. struct pxamci_platform_data *pdata;
  52. struct mmc_request *mrq;
  53. struct mmc_command *cmd;
  54. struct mmc_data *data;
  55. dma_addr_t sg_dma;
  56. struct pxa_dma_desc *sg_cpu;
  57. unsigned int dma_len;
  58. unsigned int dma_dir;
  59. unsigned int dma_drcmrrx;
  60. unsigned int dma_drcmrtx;
  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 long long clks;
  98. unsigned int timeout;
  99. bool dalgn = 0;
  100. u32 dcmd;
  101. int i;
  102. host->data = data;
  103. if (data->flags & MMC_DATA_STREAM)
  104. nob = 0xffff;
  105. writel(nob, host->base + MMC_NOB);
  106. writel(data->blksz, host->base + MMC_BLKLEN);
  107. clks = (unsigned long long)data->timeout_ns * host->clkrate;
  108. do_div(clks, 1000000000UL);
  109. timeout = (unsigned int)clks + (data->timeout_clks << host->clkrt);
  110. writel((timeout + 255) / 256, host->base + MMC_RDTO);
  111. if (data->flags & MMC_DATA_READ) {
  112. host->dma_dir = DMA_FROM_DEVICE;
  113. dcmd = DCMD_INCTRGADDR | DCMD_FLOWTRG;
  114. DRCMR(host->dma_drcmrtx) = 0;
  115. DRCMR(host->dma_drcmrrx) = host->dma | DRCMR_MAPVLD;
  116. } else {
  117. host->dma_dir = DMA_TO_DEVICE;
  118. dcmd = DCMD_INCSRCADDR | DCMD_FLOWSRC;
  119. DRCMR(host->dma_drcmrrx) = 0;
  120. DRCMR(host->dma_drcmrtx) = host->dma | DRCMR_MAPVLD;
  121. }
  122. dcmd |= DCMD_BURST32 | DCMD_WIDTH1;
  123. host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  124. host->dma_dir);
  125. for (i = 0; i < host->dma_len; i++) {
  126. unsigned int length = sg_dma_len(&data->sg[i]);
  127. host->sg_cpu[i].dcmd = dcmd | length;
  128. if (length & 31 && !(data->flags & MMC_DATA_READ))
  129. host->sg_cpu[i].dcmd |= DCMD_ENDIRQEN;
  130. /* Not aligned to 8-byte boundary? */
  131. if (sg_dma_address(&data->sg[i]) & 0x7)
  132. dalgn = 1;
  133. if (data->flags & MMC_DATA_READ) {
  134. host->sg_cpu[i].dsadr = host->res->start + MMC_RXFIFO;
  135. host->sg_cpu[i].dtadr = sg_dma_address(&data->sg[i]);
  136. } else {
  137. host->sg_cpu[i].dsadr = sg_dma_address(&data->sg[i]);
  138. host->sg_cpu[i].dtadr = host->res->start + MMC_TXFIFO;
  139. }
  140. host->sg_cpu[i].ddadr = host->sg_dma + (i + 1) *
  141. sizeof(struct pxa_dma_desc);
  142. }
  143. host->sg_cpu[host->dma_len - 1].ddadr = DDADR_STOP;
  144. wmb();
  145. /*
  146. * The PXA27x DMA controller encounters overhead when working with
  147. * unaligned (to 8-byte boundaries) data, so switch on byte alignment
  148. * mode only if we have unaligned data.
  149. */
  150. if (dalgn)
  151. DALGN |= (1 << host->dma);
  152. else
  153. DALGN &= ~(1 << host->dma);
  154. DDADR(host->dma) = host->sg_dma;
  155. /*
  156. * workaround for erratum #91:
  157. * only start DMA now if we are doing a read,
  158. * otherwise we wait until CMD/RESP has finished
  159. * before starting DMA.
  160. */
  161. if (!cpu_is_pxa27x() || data->flags & MMC_DATA_READ)
  162. DCSR(host->dma) = DCSR_RUN;
  163. }
  164. static void pxamci_start_cmd(struct pxamci_host *host, struct mmc_command *cmd, unsigned int cmdat)
  165. {
  166. WARN_ON(host->cmd != NULL);
  167. host->cmd = cmd;
  168. if (cmd->flags & MMC_RSP_BUSY)
  169. cmdat |= CMDAT_BUSY;
  170. #define RSP_TYPE(x) ((x) & ~(MMC_RSP_BUSY|MMC_RSP_OPCODE))
  171. switch (RSP_TYPE(mmc_resp_type(cmd))) {
  172. case RSP_TYPE(MMC_RSP_R1): /* r1, r1b, r6, r7 */
  173. cmdat |= CMDAT_RESP_SHORT;
  174. break;
  175. case RSP_TYPE(MMC_RSP_R3):
  176. cmdat |= CMDAT_RESP_R3;
  177. break;
  178. case RSP_TYPE(MMC_RSP_R2):
  179. cmdat |= CMDAT_RESP_R2;
  180. break;
  181. default:
  182. break;
  183. }
  184. writel(cmd->opcode, host->base + MMC_CMD);
  185. writel(cmd->arg >> 16, host->base + MMC_ARGH);
  186. writel(cmd->arg & 0xffff, host->base + MMC_ARGL);
  187. writel(cmdat, host->base + MMC_CMDAT);
  188. writel(host->clkrt, host->base + MMC_CLKRT);
  189. writel(START_CLOCK, host->base + MMC_STRPCL);
  190. pxamci_enable_irq(host, END_CMD_RES);
  191. }
  192. static void pxamci_finish_request(struct pxamci_host *host, struct mmc_request *mrq)
  193. {
  194. host->mrq = NULL;
  195. host->cmd = NULL;
  196. host->data = NULL;
  197. mmc_request_done(host->mmc, mrq);
  198. }
  199. static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat)
  200. {
  201. struct mmc_command *cmd = host->cmd;
  202. int i;
  203. u32 v;
  204. if (!cmd)
  205. return 0;
  206. host->cmd = NULL;
  207. /*
  208. * Did I mention this is Sick. We always need to
  209. * discard the upper 8 bits of the first 16-bit word.
  210. */
  211. v = readl(host->base + MMC_RES) & 0xffff;
  212. for (i = 0; i < 4; i++) {
  213. u32 w1 = readl(host->base + MMC_RES) & 0xffff;
  214. u32 w2 = readl(host->base + MMC_RES) & 0xffff;
  215. cmd->resp[i] = v << 24 | w1 << 8 | w2 >> 8;
  216. v = w2;
  217. }
  218. if (stat & STAT_TIME_OUT_RESPONSE) {
  219. cmd->error = -ETIMEDOUT;
  220. } else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
  221. /*
  222. * workaround for erratum #42:
  223. * Intel PXA27x Family Processor Specification Update Rev 001
  224. * A bogus CRC error can appear if the msb of a 136 bit
  225. * response is a one.
  226. */
  227. if (cpu_is_pxa27x() &&
  228. (cmd->flags & MMC_RSP_136 && cmd->resp[0] & 0x80000000))
  229. pr_debug("ignoring CRC from command %d - *risky*\n", cmd->opcode);
  230. else
  231. cmd->error = -EILSEQ;
  232. }
  233. pxamci_disable_irq(host, END_CMD_RES);
  234. if (host->data && !cmd->error) {
  235. pxamci_enable_irq(host, DATA_TRAN_DONE);
  236. /*
  237. * workaround for erratum #91, if doing write
  238. * enable DMA late
  239. */
  240. if (cpu_is_pxa27x() && host->data->flags & MMC_DATA_WRITE)
  241. DCSR(host->dma) = DCSR_RUN;
  242. } else {
  243. pxamci_finish_request(host, host->mrq);
  244. }
  245. return 1;
  246. }
  247. static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
  248. {
  249. struct mmc_data *data = host->data;
  250. if (!data)
  251. return 0;
  252. DCSR(host->dma) = 0;
  253. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  254. host->dma_dir);
  255. if (stat & STAT_READ_TIME_OUT)
  256. data->error = -ETIMEDOUT;
  257. else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR))
  258. data->error = -EILSEQ;
  259. /*
  260. * There appears to be a hardware design bug here. There seems to
  261. * be no way to find out how much data was transferred to the card.
  262. * This means that if there was an error on any block, we mark all
  263. * data blocks as being in error.
  264. */
  265. if (!data->error)
  266. data->bytes_xfered = data->blocks * data->blksz;
  267. else
  268. data->bytes_xfered = 0;
  269. pxamci_disable_irq(host, DATA_TRAN_DONE);
  270. host->data = NULL;
  271. if (host->mrq->stop) {
  272. pxamci_stop_clock(host);
  273. pxamci_start_cmd(host, host->mrq->stop, host->cmdat);
  274. } else {
  275. pxamci_finish_request(host, host->mrq);
  276. }
  277. return 1;
  278. }
  279. static irqreturn_t pxamci_irq(int irq, void *devid)
  280. {
  281. struct pxamci_host *host = devid;
  282. unsigned int ireg;
  283. int handled = 0;
  284. ireg = readl(host->base + MMC_I_REG) & ~readl(host->base + MMC_I_MASK);
  285. if (ireg) {
  286. unsigned stat = readl(host->base + MMC_STAT);
  287. pr_debug("PXAMCI: irq %08x stat %08x\n", ireg, stat);
  288. if (ireg & END_CMD_RES)
  289. handled |= pxamci_cmd_done(host, stat);
  290. if (ireg & DATA_TRAN_DONE)
  291. handled |= pxamci_data_done(host, stat);
  292. if (ireg & SDIO_INT) {
  293. mmc_signal_sdio_irq(host->mmc);
  294. handled = 1;
  295. }
  296. }
  297. return IRQ_RETVAL(handled);
  298. }
  299. static void pxamci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  300. {
  301. struct pxamci_host *host = mmc_priv(mmc);
  302. unsigned int cmdat;
  303. WARN_ON(host->mrq != NULL);
  304. host->mrq = mrq;
  305. pxamci_stop_clock(host);
  306. cmdat = host->cmdat;
  307. host->cmdat &= ~CMDAT_INIT;
  308. if (mrq->data) {
  309. pxamci_setup_data(host, mrq->data);
  310. cmdat &= ~CMDAT_BUSY;
  311. cmdat |= CMDAT_DATAEN | CMDAT_DMAEN;
  312. if (mrq->data->flags & MMC_DATA_WRITE)
  313. cmdat |= CMDAT_WRITE;
  314. if (mrq->data->flags & MMC_DATA_STREAM)
  315. cmdat |= CMDAT_STREAM;
  316. }
  317. pxamci_start_cmd(host, mrq->cmd, cmdat);
  318. }
  319. static int pxamci_get_ro(struct mmc_host *mmc)
  320. {
  321. struct pxamci_host *host = mmc_priv(mmc);
  322. if (host->pdata && host->pdata->get_ro)
  323. return !!host->pdata->get_ro(mmc_dev(mmc));
  324. /*
  325. * Board doesn't support read only detection; let the mmc core
  326. * decide what to do.
  327. */
  328. return -ENOSYS;
  329. }
  330. static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  331. {
  332. struct pxamci_host *host = mmc_priv(mmc);
  333. if (ios->clock) {
  334. unsigned long rate = host->clkrate;
  335. unsigned int clk = rate / ios->clock;
  336. if (host->clkrt == CLKRT_OFF)
  337. clk_enable(host->clk);
  338. if (ios->clock == 26000000) {
  339. /* to support 26MHz on pxa300/pxa310 */
  340. host->clkrt = 7;
  341. } else {
  342. /* to handle (19.5MHz, 26MHz) */
  343. if (!clk)
  344. clk = 1;
  345. /*
  346. * clk might result in a lower divisor than we
  347. * desire. check for that condition and adjust
  348. * as appropriate.
  349. */
  350. if (rate / clk > ios->clock)
  351. clk <<= 1;
  352. host->clkrt = fls(clk) - 1;
  353. }
  354. /*
  355. * we write clkrt on the next command
  356. */
  357. } else {
  358. pxamci_stop_clock(host);
  359. if (host->clkrt != CLKRT_OFF) {
  360. host->clkrt = CLKRT_OFF;
  361. clk_disable(host->clk);
  362. }
  363. }
  364. if (host->power_mode != ios->power_mode) {
  365. host->power_mode = ios->power_mode;
  366. if (host->pdata && host->pdata->setpower)
  367. host->pdata->setpower(mmc_dev(mmc), ios->vdd);
  368. if (ios->power_mode == MMC_POWER_ON)
  369. host->cmdat |= CMDAT_INIT;
  370. }
  371. if (ios->bus_width == MMC_BUS_WIDTH_4)
  372. host->cmdat |= CMDAT_SD_4DAT;
  373. else
  374. host->cmdat &= ~CMDAT_SD_4DAT;
  375. pr_debug("PXAMCI: clkrt = %x cmdat = %x\n",
  376. host->clkrt, host->cmdat);
  377. }
  378. static void pxamci_enable_sdio_irq(struct mmc_host *host, int enable)
  379. {
  380. struct pxamci_host *pxa_host = mmc_priv(host);
  381. if (enable)
  382. pxamci_enable_irq(pxa_host, SDIO_INT);
  383. else
  384. pxamci_disable_irq(pxa_host, SDIO_INT);
  385. }
  386. static const struct mmc_host_ops pxamci_ops = {
  387. .request = pxamci_request,
  388. .get_ro = pxamci_get_ro,
  389. .set_ios = pxamci_set_ios,
  390. .enable_sdio_irq = pxamci_enable_sdio_irq,
  391. };
  392. static void pxamci_dma_irq(int dma, void *devid)
  393. {
  394. struct pxamci_host *host = devid;
  395. int dcsr = DCSR(dma);
  396. DCSR(dma) = dcsr & ~DCSR_STOPIRQEN;
  397. if (dcsr & DCSR_ENDINTR) {
  398. writel(BUF_PART_FULL, host->base + MMC_PRTBUF);
  399. } else {
  400. printk(KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n",
  401. mmc_hostname(host->mmc), dma, dcsr);
  402. host->data->error = -EIO;
  403. pxamci_data_done(host, 0);
  404. }
  405. }
  406. static irqreturn_t pxamci_detect_irq(int irq, void *devid)
  407. {
  408. struct pxamci_host *host = mmc_priv(devid);
  409. mmc_detect_change(devid, host->pdata->detect_delay);
  410. return IRQ_HANDLED;
  411. }
  412. static int pxamci_probe(struct platform_device *pdev)
  413. {
  414. struct mmc_host *mmc;
  415. struct pxamci_host *host = NULL;
  416. struct resource *r, *dmarx, *dmatx;
  417. int ret, irq;
  418. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  419. irq = platform_get_irq(pdev, 0);
  420. if (!r || irq < 0)
  421. return -ENXIO;
  422. r = request_mem_region(r->start, SZ_4K, DRIVER_NAME);
  423. if (!r)
  424. return -EBUSY;
  425. mmc = mmc_alloc_host(sizeof(struct pxamci_host), &pdev->dev);
  426. if (!mmc) {
  427. ret = -ENOMEM;
  428. goto out;
  429. }
  430. mmc->ops = &pxamci_ops;
  431. /*
  432. * We can do SG-DMA, but we don't because we never know how much
  433. * data we successfully wrote to the card.
  434. */
  435. mmc->max_phys_segs = NR_SG;
  436. /*
  437. * Our hardware DMA can handle a maximum of one page per SG entry.
  438. */
  439. mmc->max_seg_size = PAGE_SIZE;
  440. /*
  441. * Block length register is only 10 bits before PXA27x.
  442. */
  443. mmc->max_blk_size = cpu_is_pxa25x() ? 1023 : 2048;
  444. /*
  445. * Block count register is 16 bits.
  446. */
  447. mmc->max_blk_count = 65535;
  448. host = mmc_priv(mmc);
  449. host->mmc = mmc;
  450. host->dma = -1;
  451. host->pdata = pdev->dev.platform_data;
  452. host->clkrt = CLKRT_OFF;
  453. host->clk = clk_get(&pdev->dev, NULL);
  454. if (IS_ERR(host->clk)) {
  455. ret = PTR_ERR(host->clk);
  456. host->clk = NULL;
  457. goto out;
  458. }
  459. host->clkrate = clk_get_rate(host->clk);
  460. /*
  461. * Calculate minimum clock rate, rounding up.
  462. */
  463. mmc->f_min = (host->clkrate + 63) / 64;
  464. mmc->f_max = (cpu_is_pxa300() || cpu_is_pxa310()) ? 26000000
  465. : host->clkrate;
  466. mmc->ocr_avail = host->pdata ?
  467. host->pdata->ocr_mask :
  468. MMC_VDD_32_33|MMC_VDD_33_34;
  469. mmc->caps = 0;
  470. host->cmdat = 0;
  471. if (!cpu_is_pxa25x()) {
  472. mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
  473. host->cmdat |= CMDAT_SDIO_INT_EN;
  474. if (cpu_is_pxa300() || cpu_is_pxa310())
  475. mmc->caps |= MMC_CAP_MMC_HIGHSPEED |
  476. MMC_CAP_SD_HIGHSPEED;
  477. }
  478. host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &host->sg_dma, GFP_KERNEL);
  479. if (!host->sg_cpu) {
  480. ret = -ENOMEM;
  481. goto out;
  482. }
  483. spin_lock_init(&host->lock);
  484. host->res = r;
  485. host->irq = irq;
  486. host->imask = MMC_I_MASK_ALL;
  487. host->base = ioremap(r->start, SZ_4K);
  488. if (!host->base) {
  489. ret = -ENOMEM;
  490. goto out;
  491. }
  492. /*
  493. * Ensure that the host controller is shut down, and setup
  494. * with our defaults.
  495. */
  496. pxamci_stop_clock(host);
  497. writel(0, host->base + MMC_SPI);
  498. writel(64, host->base + MMC_RESTO);
  499. writel(host->imask, host->base + MMC_I_MASK);
  500. host->dma = pxa_request_dma(DRIVER_NAME, DMA_PRIO_LOW,
  501. pxamci_dma_irq, host);
  502. if (host->dma < 0) {
  503. ret = -EBUSY;
  504. goto out;
  505. }
  506. ret = request_irq(host->irq, pxamci_irq, 0, DRIVER_NAME, host);
  507. if (ret)
  508. goto out;
  509. platform_set_drvdata(pdev, mmc);
  510. dmarx = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  511. if (!dmarx) {
  512. ret = -ENXIO;
  513. goto out;
  514. }
  515. host->dma_drcmrrx = dmarx->start;
  516. dmatx = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  517. if (!dmatx) {
  518. ret = -ENXIO;
  519. goto out;
  520. }
  521. host->dma_drcmrtx = dmatx->start;
  522. if (host->pdata && host->pdata->init)
  523. host->pdata->init(&pdev->dev, pxamci_detect_irq, mmc);
  524. mmc_add_host(mmc);
  525. return 0;
  526. out:
  527. if (host) {
  528. if (host->dma >= 0)
  529. pxa_free_dma(host->dma);
  530. if (host->base)
  531. iounmap(host->base);
  532. if (host->sg_cpu)
  533. dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
  534. if (host->clk)
  535. clk_put(host->clk);
  536. }
  537. if (mmc)
  538. mmc_free_host(mmc);
  539. release_resource(r);
  540. return ret;
  541. }
  542. static int pxamci_remove(struct platform_device *pdev)
  543. {
  544. struct mmc_host *mmc = platform_get_drvdata(pdev);
  545. platform_set_drvdata(pdev, NULL);
  546. if (mmc) {
  547. struct pxamci_host *host = mmc_priv(mmc);
  548. if (host->pdata && host->pdata->exit)
  549. host->pdata->exit(&pdev->dev, mmc);
  550. mmc_remove_host(mmc);
  551. pxamci_stop_clock(host);
  552. writel(TXFIFO_WR_REQ|RXFIFO_RD_REQ|CLK_IS_OFF|STOP_CMD|
  553. END_CMD_RES|PRG_DONE|DATA_TRAN_DONE,
  554. host->base + MMC_I_MASK);
  555. DRCMR(host->dma_drcmrrx) = 0;
  556. DRCMR(host->dma_drcmrtx) = 0;
  557. free_irq(host->irq, host);
  558. pxa_free_dma(host->dma);
  559. iounmap(host->base);
  560. dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
  561. clk_put(host->clk);
  562. release_resource(host->res);
  563. mmc_free_host(mmc);
  564. }
  565. return 0;
  566. }
  567. #ifdef CONFIG_PM
  568. static int pxamci_suspend(struct platform_device *dev, pm_message_t state)
  569. {
  570. struct mmc_host *mmc = platform_get_drvdata(dev);
  571. int ret = 0;
  572. if (mmc)
  573. ret = mmc_suspend_host(mmc, state);
  574. return ret;
  575. }
  576. static int pxamci_resume(struct platform_device *dev)
  577. {
  578. struct mmc_host *mmc = platform_get_drvdata(dev);
  579. int ret = 0;
  580. if (mmc)
  581. ret = mmc_resume_host(mmc);
  582. return ret;
  583. }
  584. #else
  585. #define pxamci_suspend NULL
  586. #define pxamci_resume NULL
  587. #endif
  588. static struct platform_driver pxamci_driver = {
  589. .probe = pxamci_probe,
  590. .remove = pxamci_remove,
  591. .suspend = pxamci_suspend,
  592. .resume = pxamci_resume,
  593. .driver = {
  594. .name = DRIVER_NAME,
  595. .owner = THIS_MODULE,
  596. },
  597. };
  598. static int __init pxamci_init(void)
  599. {
  600. return platform_driver_register(&pxamci_driver);
  601. }
  602. static void __exit pxamci_exit(void)
  603. {
  604. platform_driver_unregister(&pxamci_driver);
  605. }
  606. module_init(pxamci_init);
  607. module_exit(pxamci_exit);
  608. MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver");
  609. MODULE_LICENSE("GPL");
  610. MODULE_ALIAS("platform:pxa2xx-mci");