pxamci.c 20 KB

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