pxamci.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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/dmaengine.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/dma/pxa-dma.h>
  28. #include <linux/clk.h>
  29. #include <linux/err.h>
  30. #include <linux/mmc/host.h>
  31. #include <linux/mmc/slot-gpio.h>
  32. #include <linux/io.h>
  33. #include <linux/regulator/consumer.h>
  34. #include <linux/gpio.h>
  35. #include <linux/gfp.h>
  36. #include <linux/of.h>
  37. #include <linux/of_gpio.h>
  38. #include <linux/of_device.h>
  39. #include <asm/sizes.h>
  40. #include <mach/hardware.h>
  41. #include <linux/platform_data/mmc-pxamci.h>
  42. #include "pxamci.h"
  43. #define DRIVER_NAME "pxa2xx-mci"
  44. #define NR_SG 1
  45. #define CLKRT_OFF (~0)
  46. #define mmc_has_26MHz() (cpu_is_pxa300() || cpu_is_pxa310() \
  47. || cpu_is_pxa935())
  48. struct pxamci_host {
  49. struct mmc_host *mmc;
  50. spinlock_t lock;
  51. struct resource *res;
  52. void __iomem *base;
  53. struct clk *clk;
  54. unsigned long clkrate;
  55. int irq;
  56. unsigned int clkrt;
  57. unsigned int cmdat;
  58. unsigned int imask;
  59. unsigned int power_mode;
  60. struct pxamci_platform_data *pdata;
  61. struct mmc_request *mrq;
  62. struct mmc_command *cmd;
  63. struct mmc_data *data;
  64. struct dma_chan *dma_chan_rx;
  65. struct dma_chan *dma_chan_tx;
  66. dma_cookie_t dma_cookie;
  67. dma_addr_t sg_dma;
  68. unsigned int dma_len;
  69. unsigned int dma_dir;
  70. unsigned int dma_drcmrrx;
  71. unsigned int dma_drcmrtx;
  72. struct regulator *vcc;
  73. };
  74. static inline void pxamci_init_ocr(struct pxamci_host *host)
  75. {
  76. #ifdef CONFIG_REGULATOR
  77. host->vcc = regulator_get_optional(mmc_dev(host->mmc), "vmmc");
  78. if (IS_ERR(host->vcc))
  79. host->vcc = NULL;
  80. else {
  81. host->mmc->ocr_avail = mmc_regulator_get_ocrmask(host->vcc);
  82. if (host->pdata && host->pdata->ocr_mask)
  83. dev_warn(mmc_dev(host->mmc),
  84. "ocr_mask/setpower will not be used\n");
  85. }
  86. #endif
  87. if (host->vcc == NULL) {
  88. /* fall-back to platform data */
  89. host->mmc->ocr_avail = host->pdata ?
  90. host->pdata->ocr_mask :
  91. MMC_VDD_32_33 | MMC_VDD_33_34;
  92. }
  93. }
  94. static inline int pxamci_set_power(struct pxamci_host *host,
  95. unsigned char power_mode,
  96. unsigned int vdd)
  97. {
  98. int on;
  99. if (host->vcc) {
  100. int ret;
  101. if (power_mode == MMC_POWER_UP) {
  102. ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
  103. if (ret)
  104. return ret;
  105. } else if (power_mode == MMC_POWER_OFF) {
  106. ret = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
  107. if (ret)
  108. return ret;
  109. }
  110. }
  111. if (!host->vcc && host->pdata &&
  112. gpio_is_valid(host->pdata->gpio_power)) {
  113. on = ((1 << vdd) & host->pdata->ocr_mask);
  114. gpio_set_value(host->pdata->gpio_power,
  115. !!on ^ host->pdata->gpio_power_invert);
  116. }
  117. if (!host->vcc && host->pdata && host->pdata->setpower)
  118. return host->pdata->setpower(mmc_dev(host->mmc), vdd);
  119. return 0;
  120. }
  121. static void pxamci_stop_clock(struct pxamci_host *host)
  122. {
  123. if (readl(host->base + MMC_STAT) & STAT_CLK_EN) {
  124. unsigned long timeout = 10000;
  125. unsigned int v;
  126. writel(STOP_CLOCK, host->base + MMC_STRPCL);
  127. do {
  128. v = readl(host->base + MMC_STAT);
  129. if (!(v & STAT_CLK_EN))
  130. break;
  131. udelay(1);
  132. } while (timeout--);
  133. if (v & STAT_CLK_EN)
  134. dev_err(mmc_dev(host->mmc), "unable to stop clock\n");
  135. }
  136. }
  137. static void pxamci_enable_irq(struct pxamci_host *host, unsigned int mask)
  138. {
  139. unsigned long flags;
  140. spin_lock_irqsave(&host->lock, flags);
  141. host->imask &= ~mask;
  142. writel(host->imask, host->base + MMC_I_MASK);
  143. spin_unlock_irqrestore(&host->lock, flags);
  144. }
  145. static void pxamci_disable_irq(struct pxamci_host *host, unsigned int mask)
  146. {
  147. unsigned long flags;
  148. spin_lock_irqsave(&host->lock, flags);
  149. host->imask |= mask;
  150. writel(host->imask, host->base + MMC_I_MASK);
  151. spin_unlock_irqrestore(&host->lock, flags);
  152. }
  153. static void pxamci_dma_irq(void *param);
  154. static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data)
  155. {
  156. struct dma_async_tx_descriptor *tx;
  157. enum dma_data_direction direction;
  158. struct dma_slave_config config;
  159. struct dma_chan *chan;
  160. unsigned int nob = data->blocks;
  161. unsigned long long clks;
  162. unsigned int timeout;
  163. int ret;
  164. host->data = data;
  165. if (data->flags & MMC_DATA_STREAM)
  166. nob = 0xffff;
  167. writel(nob, host->base + MMC_NOB);
  168. writel(data->blksz, host->base + MMC_BLKLEN);
  169. clks = (unsigned long long)data->timeout_ns * host->clkrate;
  170. do_div(clks, 1000000000UL);
  171. timeout = (unsigned int)clks + (data->timeout_clks << host->clkrt);
  172. writel((timeout + 255) / 256, host->base + MMC_RDTO);
  173. memset(&config, 0, sizeof(config));
  174. config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  175. config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  176. config.src_addr = host->res->start + MMC_RXFIFO;
  177. config.dst_addr = host->res->start + MMC_TXFIFO;
  178. config.src_maxburst = 32;
  179. config.dst_maxburst = 32;
  180. if (data->flags & MMC_DATA_READ) {
  181. host->dma_dir = DMA_FROM_DEVICE;
  182. direction = DMA_DEV_TO_MEM;
  183. chan = host->dma_chan_rx;
  184. } else {
  185. host->dma_dir = DMA_TO_DEVICE;
  186. direction = DMA_MEM_TO_DEV;
  187. chan = host->dma_chan_tx;
  188. }
  189. config.direction = direction;
  190. ret = dmaengine_slave_config(chan, &config);
  191. if (ret < 0) {
  192. dev_err(mmc_dev(host->mmc), "dma slave config failed\n");
  193. return;
  194. }
  195. host->dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
  196. host->dma_dir);
  197. tx = dmaengine_prep_slave_sg(chan, data->sg, host->dma_len, direction,
  198. DMA_PREP_INTERRUPT);
  199. if (!tx) {
  200. dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
  201. return;
  202. }
  203. if (!(data->flags & MMC_DATA_READ)) {
  204. tx->callback = pxamci_dma_irq;
  205. tx->callback_param = host;
  206. }
  207. host->dma_cookie = dmaengine_submit(tx);
  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. dma_async_issue_pending(chan);
  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. dma_async_issue_pending(host->dma_chan_tx);
  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. struct dma_chan *chan;
  304. if (!data)
  305. return 0;
  306. if (data->flags & MMC_DATA_READ)
  307. chan = host->dma_chan_rx;
  308. else
  309. chan = host->dma_chan_tx;
  310. dma_unmap_sg(chan->device->dev,
  311. data->sg, data->sg_len, host->dma_dir);
  312. if (stat & STAT_READ_TIME_OUT)
  313. data->error = -ETIMEDOUT;
  314. else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR))
  315. data->error = -EILSEQ;
  316. /*
  317. * There appears to be a hardware design bug here. There seems to
  318. * be no way to find out how much data was transferred to the card.
  319. * This means that if there was an error on any block, we mark all
  320. * data blocks as being in error.
  321. */
  322. if (!data->error)
  323. data->bytes_xfered = data->blocks * data->blksz;
  324. else
  325. data->bytes_xfered = 0;
  326. pxamci_disable_irq(host, DATA_TRAN_DONE);
  327. host->data = NULL;
  328. if (host->mrq->stop) {
  329. pxamci_stop_clock(host);
  330. pxamci_start_cmd(host, host->mrq->stop, host->cmdat);
  331. } else {
  332. pxamci_finish_request(host, host->mrq);
  333. }
  334. return 1;
  335. }
  336. static irqreturn_t pxamci_irq(int irq, void *devid)
  337. {
  338. struct pxamci_host *host = devid;
  339. unsigned int ireg;
  340. int handled = 0;
  341. ireg = readl(host->base + MMC_I_REG) & ~readl(host->base + MMC_I_MASK);
  342. if (ireg) {
  343. unsigned stat = readl(host->base + MMC_STAT);
  344. pr_debug("PXAMCI: irq %08x stat %08x\n", ireg, stat);
  345. if (ireg & END_CMD_RES)
  346. handled |= pxamci_cmd_done(host, stat);
  347. if (ireg & DATA_TRAN_DONE)
  348. handled |= pxamci_data_done(host, stat);
  349. if (ireg & SDIO_INT) {
  350. mmc_signal_sdio_irq(host->mmc);
  351. handled = 1;
  352. }
  353. }
  354. return IRQ_RETVAL(handled);
  355. }
  356. static void pxamci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  357. {
  358. struct pxamci_host *host = mmc_priv(mmc);
  359. unsigned int cmdat;
  360. WARN_ON(host->mrq != NULL);
  361. host->mrq = mrq;
  362. pxamci_stop_clock(host);
  363. cmdat = host->cmdat;
  364. host->cmdat &= ~CMDAT_INIT;
  365. if (mrq->data) {
  366. pxamci_setup_data(host, mrq->data);
  367. cmdat &= ~CMDAT_BUSY;
  368. cmdat |= CMDAT_DATAEN | CMDAT_DMAEN;
  369. if (mrq->data->flags & MMC_DATA_WRITE)
  370. cmdat |= CMDAT_WRITE;
  371. if (mrq->data->flags & MMC_DATA_STREAM)
  372. cmdat |= CMDAT_STREAM;
  373. }
  374. pxamci_start_cmd(host, mrq->cmd, cmdat);
  375. }
  376. static int pxamci_get_ro(struct mmc_host *mmc)
  377. {
  378. struct pxamci_host *host = mmc_priv(mmc);
  379. if (host->pdata && gpio_is_valid(host->pdata->gpio_card_ro))
  380. return mmc_gpio_get_ro(mmc);
  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_prepare_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_unprepare(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_cd = mmc_gpio_get_cd,
  458. .get_ro = pxamci_get_ro,
  459. .set_ios = pxamci_set_ios,
  460. .enable_sdio_irq = pxamci_enable_sdio_irq,
  461. };
  462. static void pxamci_dma_irq(void *param)
  463. {
  464. struct pxamci_host *host = param;
  465. struct dma_tx_state state;
  466. enum dma_status status;
  467. struct dma_chan *chan;
  468. unsigned long flags;
  469. spin_lock_irqsave(&host->lock, flags);
  470. if (!host->data)
  471. goto out_unlock;
  472. if (host->data->flags & MMC_DATA_READ)
  473. chan = host->dma_chan_rx;
  474. else
  475. chan = host->dma_chan_tx;
  476. status = dmaengine_tx_status(chan, host->dma_cookie, &state);
  477. if (likely(status == DMA_COMPLETE)) {
  478. writel(BUF_PART_FULL, host->base + MMC_PRTBUF);
  479. } else {
  480. pr_err("%s: DMA error on %s channel\n", mmc_hostname(host->mmc),
  481. host->data->flags & MMC_DATA_READ ? "rx" : "tx");
  482. host->data->error = -EIO;
  483. pxamci_data_done(host, 0);
  484. }
  485. out_unlock:
  486. spin_unlock_irqrestore(&host->lock, flags);
  487. }
  488. static irqreturn_t pxamci_detect_irq(int irq, void *devid)
  489. {
  490. struct pxamci_host *host = mmc_priv(devid);
  491. mmc_detect_change(devid, msecs_to_jiffies(host->pdata->detect_delay_ms));
  492. return IRQ_HANDLED;
  493. }
  494. #ifdef CONFIG_OF
  495. static const struct of_device_id pxa_mmc_dt_ids[] = {
  496. { .compatible = "marvell,pxa-mmc" },
  497. { }
  498. };
  499. MODULE_DEVICE_TABLE(of, pxa_mmc_dt_ids);
  500. static int pxamci_of_init(struct platform_device *pdev)
  501. {
  502. struct device_node *np = pdev->dev.of_node;
  503. struct pxamci_platform_data *pdata;
  504. u32 tmp;
  505. if (!np)
  506. return 0;
  507. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  508. if (!pdata)
  509. return -ENOMEM;
  510. pdata->gpio_card_detect =
  511. of_get_named_gpio(np, "cd-gpios", 0);
  512. pdata->gpio_card_ro =
  513. of_get_named_gpio(np, "wp-gpios", 0);
  514. /* pxa-mmc specific */
  515. pdata->gpio_power =
  516. of_get_named_gpio(np, "pxa-mmc,gpio-power", 0);
  517. if (of_property_read_u32(np, "pxa-mmc,detect-delay-ms", &tmp) == 0)
  518. pdata->detect_delay_ms = tmp;
  519. pdev->dev.platform_data = pdata;
  520. return 0;
  521. }
  522. #else
  523. static int pxamci_of_init(struct platform_device *pdev)
  524. {
  525. return 0;
  526. }
  527. #endif
  528. static int pxamci_probe(struct platform_device *pdev)
  529. {
  530. struct mmc_host *mmc;
  531. struct pxamci_host *host = NULL;
  532. struct resource *r, *dmarx, *dmatx;
  533. struct pxad_param param_rx, param_tx;
  534. int ret, irq, gpio_cd = -1, gpio_ro = -1, gpio_power = -1;
  535. dma_cap_mask_t mask;
  536. ret = pxamci_of_init(pdev);
  537. if (ret)
  538. return ret;
  539. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  540. irq = platform_get_irq(pdev, 0);
  541. if (!r || irq < 0)
  542. return -ENXIO;
  543. r = request_mem_region(r->start, SZ_4K, DRIVER_NAME);
  544. if (!r)
  545. return -EBUSY;
  546. mmc = mmc_alloc_host(sizeof(struct pxamci_host), &pdev->dev);
  547. if (!mmc) {
  548. ret = -ENOMEM;
  549. goto out;
  550. }
  551. mmc->ops = &pxamci_ops;
  552. /*
  553. * We can do SG-DMA, but we don't because we never know how much
  554. * data we successfully wrote to the card.
  555. */
  556. mmc->max_segs = NR_SG;
  557. /*
  558. * Our hardware DMA can handle a maximum of one page per SG entry.
  559. */
  560. mmc->max_seg_size = PAGE_SIZE;
  561. /*
  562. * Block length register is only 10 bits before PXA27x.
  563. */
  564. mmc->max_blk_size = cpu_is_pxa25x() ? 1023 : 2048;
  565. /*
  566. * Block count register is 16 bits.
  567. */
  568. mmc->max_blk_count = 65535;
  569. host = mmc_priv(mmc);
  570. host->mmc = mmc;
  571. host->pdata = pdev->dev.platform_data;
  572. host->clkrt = CLKRT_OFF;
  573. host->clk = clk_get(&pdev->dev, NULL);
  574. if (IS_ERR(host->clk)) {
  575. ret = PTR_ERR(host->clk);
  576. host->clk = NULL;
  577. goto out;
  578. }
  579. host->clkrate = clk_get_rate(host->clk);
  580. /*
  581. * Calculate minimum clock rate, rounding up.
  582. */
  583. mmc->f_min = (host->clkrate + 63) / 64;
  584. mmc->f_max = (mmc_has_26MHz()) ? 26000000 : host->clkrate;
  585. pxamci_init_ocr(host);
  586. mmc->caps = 0;
  587. host->cmdat = 0;
  588. if (!cpu_is_pxa25x()) {
  589. mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
  590. host->cmdat |= CMDAT_SDIO_INT_EN;
  591. if (mmc_has_26MHz())
  592. mmc->caps |= MMC_CAP_MMC_HIGHSPEED |
  593. MMC_CAP_SD_HIGHSPEED;
  594. }
  595. spin_lock_init(&host->lock);
  596. host->res = r;
  597. host->irq = irq;
  598. host->imask = MMC_I_MASK_ALL;
  599. host->base = ioremap(r->start, SZ_4K);
  600. if (!host->base) {
  601. ret = -ENOMEM;
  602. goto out;
  603. }
  604. /*
  605. * Ensure that the host controller is shut down, and setup
  606. * with our defaults.
  607. */
  608. pxamci_stop_clock(host);
  609. writel(0, host->base + MMC_SPI);
  610. writel(64, host->base + MMC_RESTO);
  611. writel(host->imask, host->base + MMC_I_MASK);
  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. if (!pdev->dev.of_node) {
  617. dmarx = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  618. dmatx = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  619. if (!dmarx || !dmatx) {
  620. ret = -ENXIO;
  621. goto out;
  622. }
  623. param_rx.prio = PXAD_PRIO_LOWEST;
  624. param_rx.drcmr = dmarx->start;
  625. param_tx.prio = PXAD_PRIO_LOWEST;
  626. param_tx.drcmr = dmatx->start;
  627. }
  628. dma_cap_zero(mask);
  629. dma_cap_set(DMA_SLAVE, mask);
  630. host->dma_chan_rx =
  631. dma_request_slave_channel_compat(mask, pxad_filter_fn,
  632. &param_rx, &pdev->dev, "rx");
  633. if (host->dma_chan_rx == NULL) {
  634. dev_err(&pdev->dev, "unable to request rx dma channel\n");
  635. ret = -ENODEV;
  636. goto out;
  637. }
  638. host->dma_chan_tx =
  639. dma_request_slave_channel_compat(mask, pxad_filter_fn,
  640. &param_tx, &pdev->dev, "tx");
  641. if (host->dma_chan_tx == NULL) {
  642. dev_err(&pdev->dev, "unable to request tx dma channel\n");
  643. ret = -ENODEV;
  644. goto out;
  645. }
  646. if (host->pdata) {
  647. gpio_cd = host->pdata->gpio_card_detect;
  648. gpio_ro = host->pdata->gpio_card_ro;
  649. gpio_power = host->pdata->gpio_power;
  650. }
  651. if (gpio_is_valid(gpio_power)) {
  652. ret = devm_gpio_request(&pdev->dev, gpio_power,
  653. "mmc card power");
  654. if (ret) {
  655. dev_err(&pdev->dev, "Failed requesting gpio_power %d\n",
  656. gpio_power);
  657. goto out;
  658. }
  659. gpio_direction_output(gpio_power,
  660. host->pdata->gpio_power_invert);
  661. }
  662. if (gpio_is_valid(gpio_ro))
  663. ret = mmc_gpio_request_ro(mmc, gpio_ro);
  664. if (ret) {
  665. dev_err(&pdev->dev, "Failed requesting gpio_ro %d\n", gpio_ro);
  666. goto out;
  667. } else {
  668. mmc->caps |= host->pdata->gpio_card_ro_invert ?
  669. MMC_CAP2_RO_ACTIVE_HIGH : 0;
  670. }
  671. if (gpio_is_valid(gpio_cd))
  672. ret = mmc_gpio_request_cd(mmc, gpio_cd, 0);
  673. if (ret) {
  674. dev_err(&pdev->dev, "Failed requesting gpio_cd %d\n", gpio_cd);
  675. goto out;
  676. }
  677. if (host->pdata && host->pdata->init)
  678. host->pdata->init(&pdev->dev, pxamci_detect_irq, mmc);
  679. if (gpio_is_valid(gpio_power) && host->pdata->setpower)
  680. dev_warn(&pdev->dev, "gpio_power and setpower() both defined\n");
  681. if (gpio_is_valid(gpio_ro) && host->pdata->get_ro)
  682. dev_warn(&pdev->dev, "gpio_ro and get_ro() both defined\n");
  683. mmc_add_host(mmc);
  684. return 0;
  685. out:
  686. if (host) {
  687. if (host->dma_chan_rx)
  688. dma_release_channel(host->dma_chan_rx);
  689. if (host->dma_chan_tx)
  690. dma_release_channel(host->dma_chan_tx);
  691. if (host->base)
  692. iounmap(host->base);
  693. if (host->clk)
  694. clk_put(host->clk);
  695. }
  696. if (mmc)
  697. mmc_free_host(mmc);
  698. release_resource(r);
  699. return ret;
  700. }
  701. static int pxamci_remove(struct platform_device *pdev)
  702. {
  703. struct mmc_host *mmc = platform_get_drvdata(pdev);
  704. int gpio_cd = -1, gpio_ro = -1, gpio_power = -1;
  705. if (mmc) {
  706. struct pxamci_host *host = mmc_priv(mmc);
  707. mmc_remove_host(mmc);
  708. if (host->pdata) {
  709. gpio_cd = host->pdata->gpio_card_detect;
  710. gpio_ro = host->pdata->gpio_card_ro;
  711. gpio_power = host->pdata->gpio_power;
  712. }
  713. if (host->vcc)
  714. regulator_put(host->vcc);
  715. if (host->pdata && host->pdata->exit)
  716. host->pdata->exit(&pdev->dev, mmc);
  717. pxamci_stop_clock(host);
  718. writel(TXFIFO_WR_REQ|RXFIFO_RD_REQ|CLK_IS_OFF|STOP_CMD|
  719. END_CMD_RES|PRG_DONE|DATA_TRAN_DONE,
  720. host->base + MMC_I_MASK);
  721. free_irq(host->irq, host);
  722. dmaengine_terminate_all(host->dma_chan_rx);
  723. dmaengine_terminate_all(host->dma_chan_tx);
  724. dma_release_channel(host->dma_chan_rx);
  725. dma_release_channel(host->dma_chan_tx);
  726. iounmap(host->base);
  727. clk_put(host->clk);
  728. release_resource(host->res);
  729. mmc_free_host(mmc);
  730. }
  731. return 0;
  732. }
  733. static struct platform_driver pxamci_driver = {
  734. .probe = pxamci_probe,
  735. .remove = pxamci_remove,
  736. .driver = {
  737. .name = DRIVER_NAME,
  738. .of_match_table = of_match_ptr(pxa_mmc_dt_ids),
  739. },
  740. };
  741. module_platform_driver(pxamci_driver);
  742. MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver");
  743. MODULE_LICENSE("GPL");
  744. MODULE_ALIAS("platform:pxa2xx-mci");