pxamci.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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 = devm_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. writel(nob, host->base + MMC_NOB);
  166. writel(data->blksz, host->base + MMC_BLKLEN);
  167. clks = (unsigned long long)data->timeout_ns * host->clkrate;
  168. do_div(clks, 1000000000UL);
  169. timeout = (unsigned int)clks + (data->timeout_clks << host->clkrt);
  170. writel((timeout + 255) / 256, host->base + MMC_RDTO);
  171. memset(&config, 0, sizeof(config));
  172. config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  173. config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  174. config.src_addr = host->res->start + MMC_RXFIFO;
  175. config.dst_addr = host->res->start + MMC_TXFIFO;
  176. config.src_maxburst = 32;
  177. config.dst_maxburst = 32;
  178. if (data->flags & MMC_DATA_READ) {
  179. host->dma_dir = DMA_FROM_DEVICE;
  180. direction = DMA_DEV_TO_MEM;
  181. chan = host->dma_chan_rx;
  182. } else {
  183. host->dma_dir = DMA_TO_DEVICE;
  184. direction = DMA_MEM_TO_DEV;
  185. chan = host->dma_chan_tx;
  186. }
  187. config.direction = direction;
  188. ret = dmaengine_slave_config(chan, &config);
  189. if (ret < 0) {
  190. dev_err(mmc_dev(host->mmc), "dma slave config failed\n");
  191. return;
  192. }
  193. host->dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
  194. host->dma_dir);
  195. tx = dmaengine_prep_slave_sg(chan, data->sg, host->dma_len, direction,
  196. DMA_PREP_INTERRUPT);
  197. if (!tx) {
  198. dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
  199. return;
  200. }
  201. if (!(data->flags & MMC_DATA_READ)) {
  202. tx->callback = pxamci_dma_irq;
  203. tx->callback_param = host;
  204. }
  205. host->dma_cookie = dmaengine_submit(tx);
  206. /*
  207. * workaround for erratum #91:
  208. * only start DMA now if we are doing a read,
  209. * otherwise we wait until CMD/RESP has finished
  210. * before starting DMA.
  211. */
  212. if (!cpu_is_pxa27x() || data->flags & MMC_DATA_READ)
  213. dma_async_issue_pending(chan);
  214. }
  215. static void pxamci_start_cmd(struct pxamci_host *host, struct mmc_command *cmd, unsigned int cmdat)
  216. {
  217. WARN_ON(host->cmd != NULL);
  218. host->cmd = cmd;
  219. if (cmd->flags & MMC_RSP_BUSY)
  220. cmdat |= CMDAT_BUSY;
  221. #define RSP_TYPE(x) ((x) & ~(MMC_RSP_BUSY|MMC_RSP_OPCODE))
  222. switch (RSP_TYPE(mmc_resp_type(cmd))) {
  223. case RSP_TYPE(MMC_RSP_R1): /* r1, r1b, r6, r7 */
  224. cmdat |= CMDAT_RESP_SHORT;
  225. break;
  226. case RSP_TYPE(MMC_RSP_R3):
  227. cmdat |= CMDAT_RESP_R3;
  228. break;
  229. case RSP_TYPE(MMC_RSP_R2):
  230. cmdat |= CMDAT_RESP_R2;
  231. break;
  232. default:
  233. break;
  234. }
  235. writel(cmd->opcode, host->base + MMC_CMD);
  236. writel(cmd->arg >> 16, host->base + MMC_ARGH);
  237. writel(cmd->arg & 0xffff, host->base + MMC_ARGL);
  238. writel(cmdat, host->base + MMC_CMDAT);
  239. writel(host->clkrt, host->base + MMC_CLKRT);
  240. writel(START_CLOCK, host->base + MMC_STRPCL);
  241. pxamci_enable_irq(host, END_CMD_RES);
  242. }
  243. static void pxamci_finish_request(struct pxamci_host *host, struct mmc_request *mrq)
  244. {
  245. host->mrq = NULL;
  246. host->cmd = NULL;
  247. host->data = NULL;
  248. mmc_request_done(host->mmc, mrq);
  249. }
  250. static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat)
  251. {
  252. struct mmc_command *cmd = host->cmd;
  253. int i;
  254. u32 v;
  255. if (!cmd)
  256. return 0;
  257. host->cmd = NULL;
  258. /*
  259. * Did I mention this is Sick. We always need to
  260. * discard the upper 8 bits of the first 16-bit word.
  261. */
  262. v = readl(host->base + MMC_RES) & 0xffff;
  263. for (i = 0; i < 4; i++) {
  264. u32 w1 = readl(host->base + MMC_RES) & 0xffff;
  265. u32 w2 = readl(host->base + MMC_RES) & 0xffff;
  266. cmd->resp[i] = v << 24 | w1 << 8 | w2 >> 8;
  267. v = w2;
  268. }
  269. if (stat & STAT_TIME_OUT_RESPONSE) {
  270. cmd->error = -ETIMEDOUT;
  271. } else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
  272. /*
  273. * workaround for erratum #42:
  274. * Intel PXA27x Family Processor Specification Update Rev 001
  275. * A bogus CRC error can appear if the msb of a 136 bit
  276. * response is a one.
  277. */
  278. if (cpu_is_pxa27x() &&
  279. (cmd->flags & MMC_RSP_136 && cmd->resp[0] & 0x80000000))
  280. pr_debug("ignoring CRC from command %d - *risky*\n", cmd->opcode);
  281. else
  282. cmd->error = -EILSEQ;
  283. }
  284. pxamci_disable_irq(host, END_CMD_RES);
  285. if (host->data && !cmd->error) {
  286. pxamci_enable_irq(host, DATA_TRAN_DONE);
  287. /*
  288. * workaround for erratum #91, if doing write
  289. * enable DMA late
  290. */
  291. if (cpu_is_pxa27x() && host->data->flags & MMC_DATA_WRITE)
  292. dma_async_issue_pending(host->dma_chan_tx);
  293. } else {
  294. pxamci_finish_request(host, host->mrq);
  295. }
  296. return 1;
  297. }
  298. static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
  299. {
  300. struct mmc_data *data = host->data;
  301. struct dma_chan *chan;
  302. if (!data)
  303. return 0;
  304. if (data->flags & MMC_DATA_READ)
  305. chan = host->dma_chan_rx;
  306. else
  307. chan = host->dma_chan_tx;
  308. dma_unmap_sg(chan->device->dev,
  309. data->sg, data->sg_len, host->dma_dir);
  310. if (stat & STAT_READ_TIME_OUT)
  311. data->error = -ETIMEDOUT;
  312. else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR))
  313. data->error = -EILSEQ;
  314. /*
  315. * There appears to be a hardware design bug here. There seems to
  316. * be no way to find out how much data was transferred to the card.
  317. * This means that if there was an error on any block, we mark all
  318. * data blocks as being in error.
  319. */
  320. if (!data->error)
  321. data->bytes_xfered = data->blocks * data->blksz;
  322. else
  323. data->bytes_xfered = 0;
  324. pxamci_disable_irq(host, DATA_TRAN_DONE);
  325. host->data = NULL;
  326. if (host->mrq->stop) {
  327. pxamci_stop_clock(host);
  328. pxamci_start_cmd(host, host->mrq->stop, host->cmdat);
  329. } else {
  330. pxamci_finish_request(host, host->mrq);
  331. }
  332. return 1;
  333. }
  334. static irqreturn_t pxamci_irq(int irq, void *devid)
  335. {
  336. struct pxamci_host *host = devid;
  337. unsigned int ireg;
  338. int handled = 0;
  339. ireg = readl(host->base + MMC_I_REG) & ~readl(host->base + MMC_I_MASK);
  340. if (ireg) {
  341. unsigned stat = readl(host->base + MMC_STAT);
  342. pr_debug("PXAMCI: irq %08x stat %08x\n", ireg, stat);
  343. if (ireg & END_CMD_RES)
  344. handled |= pxamci_cmd_done(host, stat);
  345. if (ireg & DATA_TRAN_DONE)
  346. handled |= pxamci_data_done(host, stat);
  347. if (ireg & SDIO_INT) {
  348. mmc_signal_sdio_irq(host->mmc);
  349. handled = 1;
  350. }
  351. }
  352. return IRQ_RETVAL(handled);
  353. }
  354. static void pxamci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  355. {
  356. struct pxamci_host *host = mmc_priv(mmc);
  357. unsigned int cmdat;
  358. WARN_ON(host->mrq != NULL);
  359. host->mrq = mrq;
  360. pxamci_stop_clock(host);
  361. cmdat = host->cmdat;
  362. host->cmdat &= ~CMDAT_INIT;
  363. if (mrq->data) {
  364. pxamci_setup_data(host, mrq->data);
  365. cmdat &= ~CMDAT_BUSY;
  366. cmdat |= CMDAT_DATAEN | CMDAT_DMAEN;
  367. if (mrq->data->flags & MMC_DATA_WRITE)
  368. cmdat |= CMDAT_WRITE;
  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. return mmc_gpio_get_ro(mmc);
  377. if (host->pdata && host->pdata->get_ro)
  378. return !!host->pdata->get_ro(mmc_dev(mmc));
  379. /*
  380. * Board doesn't support read only detection; let the mmc core
  381. * decide what to do.
  382. */
  383. return -ENOSYS;
  384. }
  385. static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  386. {
  387. struct pxamci_host *host = mmc_priv(mmc);
  388. if (ios->clock) {
  389. unsigned long rate = host->clkrate;
  390. unsigned int clk = rate / ios->clock;
  391. if (host->clkrt == CLKRT_OFF)
  392. clk_prepare_enable(host->clk);
  393. if (ios->clock == 26000000) {
  394. /* to support 26MHz */
  395. host->clkrt = 7;
  396. } else {
  397. /* to handle (19.5MHz, 26MHz) */
  398. if (!clk)
  399. clk = 1;
  400. /*
  401. * clk might result in a lower divisor than we
  402. * desire. check for that condition and adjust
  403. * as appropriate.
  404. */
  405. if (rate / clk > ios->clock)
  406. clk <<= 1;
  407. host->clkrt = fls(clk) - 1;
  408. }
  409. /*
  410. * we write clkrt on the next command
  411. */
  412. } else {
  413. pxamci_stop_clock(host);
  414. if (host->clkrt != CLKRT_OFF) {
  415. host->clkrt = CLKRT_OFF;
  416. clk_disable_unprepare(host->clk);
  417. }
  418. }
  419. if (host->power_mode != ios->power_mode) {
  420. int ret;
  421. host->power_mode = ios->power_mode;
  422. ret = pxamci_set_power(host, ios->power_mode, ios->vdd);
  423. if (ret) {
  424. dev_err(mmc_dev(mmc), "unable to set power\n");
  425. /*
  426. * The .set_ios() function in the mmc_host_ops
  427. * struct return void, and failing to set the
  428. * power should be rare so we print an error and
  429. * return here.
  430. */
  431. return;
  432. }
  433. if (ios->power_mode == MMC_POWER_ON)
  434. host->cmdat |= CMDAT_INIT;
  435. }
  436. if (ios->bus_width == MMC_BUS_WIDTH_4)
  437. host->cmdat |= CMDAT_SD_4DAT;
  438. else
  439. host->cmdat &= ~CMDAT_SD_4DAT;
  440. dev_dbg(mmc_dev(mmc), "PXAMCI: clkrt = %x cmdat = %x\n",
  441. host->clkrt, host->cmdat);
  442. }
  443. static void pxamci_enable_sdio_irq(struct mmc_host *host, int enable)
  444. {
  445. struct pxamci_host *pxa_host = mmc_priv(host);
  446. if (enable)
  447. pxamci_enable_irq(pxa_host, SDIO_INT);
  448. else
  449. pxamci_disable_irq(pxa_host, SDIO_INT);
  450. }
  451. static const struct mmc_host_ops pxamci_ops = {
  452. .request = pxamci_request,
  453. .get_cd = mmc_gpio_get_cd,
  454. .get_ro = pxamci_get_ro,
  455. .set_ios = pxamci_set_ios,
  456. .enable_sdio_irq = pxamci_enable_sdio_irq,
  457. };
  458. static void pxamci_dma_irq(void *param)
  459. {
  460. struct pxamci_host *host = param;
  461. struct dma_tx_state state;
  462. enum dma_status status;
  463. struct dma_chan *chan;
  464. unsigned long flags;
  465. spin_lock_irqsave(&host->lock, flags);
  466. if (!host->data)
  467. goto out_unlock;
  468. if (host->data->flags & MMC_DATA_READ)
  469. chan = host->dma_chan_rx;
  470. else
  471. chan = host->dma_chan_tx;
  472. status = dmaengine_tx_status(chan, host->dma_cookie, &state);
  473. if (likely(status == DMA_COMPLETE)) {
  474. writel(BUF_PART_FULL, host->base + MMC_PRTBUF);
  475. } else {
  476. pr_err("%s: DMA error on %s channel\n", mmc_hostname(host->mmc),
  477. host->data->flags & MMC_DATA_READ ? "rx" : "tx");
  478. host->data->error = -EIO;
  479. pxamci_data_done(host, 0);
  480. }
  481. out_unlock:
  482. spin_unlock_irqrestore(&host->lock, flags);
  483. }
  484. static irqreturn_t pxamci_detect_irq(int irq, void *devid)
  485. {
  486. struct pxamci_host *host = mmc_priv(devid);
  487. mmc_detect_change(devid, msecs_to_jiffies(host->pdata->detect_delay_ms));
  488. return IRQ_HANDLED;
  489. }
  490. #ifdef CONFIG_OF
  491. static const struct of_device_id pxa_mmc_dt_ids[] = {
  492. { .compatible = "marvell,pxa-mmc" },
  493. { }
  494. };
  495. MODULE_DEVICE_TABLE(of, pxa_mmc_dt_ids);
  496. static int pxamci_of_init(struct platform_device *pdev)
  497. {
  498. struct device_node *np = pdev->dev.of_node;
  499. struct pxamci_platform_data *pdata;
  500. u32 tmp;
  501. if (!np)
  502. return 0;
  503. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  504. if (!pdata)
  505. return -ENOMEM;
  506. pdata->gpio_card_detect =
  507. of_get_named_gpio(np, "cd-gpios", 0);
  508. pdata->gpio_card_ro =
  509. of_get_named_gpio(np, "wp-gpios", 0);
  510. /* pxa-mmc specific */
  511. pdata->gpio_power =
  512. of_get_named_gpio(np, "pxa-mmc,gpio-power", 0);
  513. if (of_property_read_u32(np, "pxa-mmc,detect-delay-ms", &tmp) == 0)
  514. pdata->detect_delay_ms = tmp;
  515. pdev->dev.platform_data = pdata;
  516. return 0;
  517. }
  518. #else
  519. static int pxamci_of_init(struct platform_device *pdev)
  520. {
  521. return 0;
  522. }
  523. #endif
  524. static int pxamci_probe(struct platform_device *pdev)
  525. {
  526. struct mmc_host *mmc;
  527. struct pxamci_host *host = NULL;
  528. struct resource *r, *dmarx, *dmatx;
  529. struct pxad_param param_rx, param_tx;
  530. int ret, irq, gpio_cd = -1, gpio_ro = -1, gpio_power = -1;
  531. dma_cap_mask_t mask;
  532. ret = pxamci_of_init(pdev);
  533. if (ret)
  534. return ret;
  535. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  536. irq = platform_get_irq(pdev, 0);
  537. if (irq < 0)
  538. return irq;
  539. mmc = mmc_alloc_host(sizeof(struct pxamci_host), &pdev->dev);
  540. if (!mmc) {
  541. ret = -ENOMEM;
  542. goto out;
  543. }
  544. mmc->ops = &pxamci_ops;
  545. /*
  546. * We can do SG-DMA, but we don't because we never know how much
  547. * data we successfully wrote to the card.
  548. */
  549. mmc->max_segs = NR_SG;
  550. /*
  551. * Our hardware DMA can handle a maximum of one page per SG entry.
  552. */
  553. mmc->max_seg_size = PAGE_SIZE;
  554. /*
  555. * Block length register is only 10 bits before PXA27x.
  556. */
  557. mmc->max_blk_size = cpu_is_pxa25x() ? 1023 : 2048;
  558. /*
  559. * Block count register is 16 bits.
  560. */
  561. mmc->max_blk_count = 65535;
  562. host = mmc_priv(mmc);
  563. host->mmc = mmc;
  564. host->pdata = pdev->dev.platform_data;
  565. host->clkrt = CLKRT_OFF;
  566. host->clk = devm_clk_get(&pdev->dev, NULL);
  567. if (IS_ERR(host->clk)) {
  568. ret = PTR_ERR(host->clk);
  569. host->clk = NULL;
  570. goto out;
  571. }
  572. host->clkrate = clk_get_rate(host->clk);
  573. /*
  574. * Calculate minimum clock rate, rounding up.
  575. */
  576. mmc->f_min = (host->clkrate + 63) / 64;
  577. mmc->f_max = (mmc_has_26MHz()) ? 26000000 : host->clkrate;
  578. pxamci_init_ocr(host);
  579. mmc->caps = 0;
  580. host->cmdat = 0;
  581. if (!cpu_is_pxa25x()) {
  582. mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
  583. host->cmdat |= CMDAT_SDIO_INT_EN;
  584. if (mmc_has_26MHz())
  585. mmc->caps |= MMC_CAP_MMC_HIGHSPEED |
  586. MMC_CAP_SD_HIGHSPEED;
  587. }
  588. spin_lock_init(&host->lock);
  589. host->res = r;
  590. host->irq = irq;
  591. host->imask = MMC_I_MASK_ALL;
  592. host->base = devm_ioremap_resource(&pdev->dev, r);
  593. if (IS_ERR(host->base)) {
  594. ret = PTR_ERR(host->base);
  595. goto out;
  596. }
  597. /*
  598. * Ensure that the host controller is shut down, and setup
  599. * with our defaults.
  600. */
  601. pxamci_stop_clock(host);
  602. writel(0, host->base + MMC_SPI);
  603. writel(64, host->base + MMC_RESTO);
  604. writel(host->imask, host->base + MMC_I_MASK);
  605. ret = devm_request_irq(&pdev->dev, host->irq, pxamci_irq, 0,
  606. DRIVER_NAME, host);
  607. if (ret)
  608. goto out;
  609. platform_set_drvdata(pdev, mmc);
  610. if (!pdev->dev.of_node) {
  611. dmarx = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  612. dmatx = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  613. if (!dmarx || !dmatx) {
  614. ret = -ENXIO;
  615. goto out;
  616. }
  617. param_rx.prio = PXAD_PRIO_LOWEST;
  618. param_rx.drcmr = dmarx->start;
  619. param_tx.prio = PXAD_PRIO_LOWEST;
  620. param_tx.drcmr = dmatx->start;
  621. }
  622. dma_cap_zero(mask);
  623. dma_cap_set(DMA_SLAVE, mask);
  624. host->dma_chan_rx =
  625. dma_request_slave_channel_compat(mask, pxad_filter_fn,
  626. &param_rx, &pdev->dev, "rx");
  627. if (host->dma_chan_rx == NULL) {
  628. dev_err(&pdev->dev, "unable to request rx dma channel\n");
  629. ret = -ENODEV;
  630. goto out;
  631. }
  632. host->dma_chan_tx =
  633. dma_request_slave_channel_compat(mask, pxad_filter_fn,
  634. &param_tx, &pdev->dev, "tx");
  635. if (host->dma_chan_tx == NULL) {
  636. dev_err(&pdev->dev, "unable to request tx dma channel\n");
  637. ret = -ENODEV;
  638. goto out;
  639. }
  640. if (host->pdata) {
  641. gpio_cd = host->pdata->gpio_card_detect;
  642. gpio_ro = host->pdata->gpio_card_ro;
  643. gpio_power = host->pdata->gpio_power;
  644. }
  645. if (gpio_is_valid(gpio_power)) {
  646. ret = devm_gpio_request(&pdev->dev, gpio_power,
  647. "mmc card power");
  648. if (ret) {
  649. dev_err(&pdev->dev, "Failed requesting gpio_power %d\n",
  650. gpio_power);
  651. goto out;
  652. }
  653. gpio_direction_output(gpio_power,
  654. host->pdata->gpio_power_invert);
  655. }
  656. if (gpio_is_valid(gpio_ro)) {
  657. ret = mmc_gpio_request_ro(mmc, gpio_ro);
  658. if (ret) {
  659. dev_err(&pdev->dev, "Failed requesting gpio_ro %d\n",
  660. gpio_ro);
  661. goto out;
  662. } else {
  663. mmc->caps2 |= host->pdata->gpio_card_ro_invert ?
  664. 0 : MMC_CAP2_RO_ACTIVE_HIGH;
  665. }
  666. }
  667. if (gpio_is_valid(gpio_cd))
  668. ret = mmc_gpio_request_cd(mmc, gpio_cd, 0);
  669. if (ret) {
  670. dev_err(&pdev->dev, "Failed requesting gpio_cd %d\n", gpio_cd);
  671. goto out;
  672. }
  673. if (host->pdata && host->pdata->init)
  674. host->pdata->init(&pdev->dev, pxamci_detect_irq, mmc);
  675. if (gpio_is_valid(gpio_power) && host->pdata->setpower)
  676. dev_warn(&pdev->dev, "gpio_power and setpower() both defined\n");
  677. if (gpio_is_valid(gpio_ro) && host->pdata->get_ro)
  678. dev_warn(&pdev->dev, "gpio_ro and get_ro() both defined\n");
  679. mmc_add_host(mmc);
  680. return 0;
  681. out:
  682. if (host) {
  683. if (host->dma_chan_rx)
  684. dma_release_channel(host->dma_chan_rx);
  685. if (host->dma_chan_tx)
  686. dma_release_channel(host->dma_chan_tx);
  687. }
  688. if (mmc)
  689. mmc_free_host(mmc);
  690. return ret;
  691. }
  692. static int pxamci_remove(struct platform_device *pdev)
  693. {
  694. struct mmc_host *mmc = platform_get_drvdata(pdev);
  695. int gpio_cd = -1, gpio_ro = -1, gpio_power = -1;
  696. if (mmc) {
  697. struct pxamci_host *host = mmc_priv(mmc);
  698. mmc_remove_host(mmc);
  699. if (host->pdata) {
  700. gpio_cd = host->pdata->gpio_card_detect;
  701. gpio_ro = host->pdata->gpio_card_ro;
  702. gpio_power = host->pdata->gpio_power;
  703. }
  704. if (host->pdata && host->pdata->exit)
  705. host->pdata->exit(&pdev->dev, mmc);
  706. pxamci_stop_clock(host);
  707. writel(TXFIFO_WR_REQ|RXFIFO_RD_REQ|CLK_IS_OFF|STOP_CMD|
  708. END_CMD_RES|PRG_DONE|DATA_TRAN_DONE,
  709. host->base + MMC_I_MASK);
  710. dmaengine_terminate_all(host->dma_chan_rx);
  711. dmaengine_terminate_all(host->dma_chan_tx);
  712. dma_release_channel(host->dma_chan_rx);
  713. dma_release_channel(host->dma_chan_tx);
  714. mmc_free_host(mmc);
  715. }
  716. return 0;
  717. }
  718. static struct platform_driver pxamci_driver = {
  719. .probe = pxamci_probe,
  720. .remove = pxamci_remove,
  721. .driver = {
  722. .name = DRIVER_NAME,
  723. .of_match_table = of_match_ptr(pxa_mmc_dt_ids),
  724. },
  725. };
  726. module_platform_driver(pxamci_driver);
  727. MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver");
  728. MODULE_LICENSE("GPL");
  729. MODULE_ALIAS("platform:pxa2xx-mci");