pxamci.c 17 KB

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