pxamci.c 20 KB

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