pxamci.c 20 KB

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