pxamci.c 20 KB

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