pxamci.c 22 KB

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