pxamci.c 18 KB

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