pxamci.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/drivers/mmc/host/pxa.c - PXA MMCI driver
  4. *
  5. * Copyright (C) 2003 Russell King, All Rights Reserved.
  6. *
  7. * This hardware is really sick:
  8. * - No way to clear interrupts.
  9. * - Have to turn off the clock whenever we touch the device.
  10. * - Doesn't tell you how many data blocks were transferred.
  11. * Yuck!
  12. *
  13. * 1 and 3 byte data transfers not supported
  14. * max block length up to 1023
  15. */
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/ioport.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/delay.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/dmaengine.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/clk.h>
  25. #include <linux/err.h>
  26. #include <linux/mmc/host.h>
  27. #include <linux/mmc/slot-gpio.h>
  28. #include <linux/io.h>
  29. #include <linux/regulator/consumer.h>
  30. #include <linux/gpio/consumer.h>
  31. #include <linux/gfp.h>
  32. #include <linux/of.h>
  33. #include <linux/soc/pxa/cpu.h>
  34. #include <linux/sizes.h>
  35. #include <linux/platform_data/mmc-pxamci.h>
  36. #include "pxamci.h"
  37. #define DRIVER_NAME "pxa2xx-mci"
  38. #define NR_SG 1
  39. #define CLKRT_OFF (~0)
  40. #define mmc_has_26MHz() (cpu_is_pxa300() || cpu_is_pxa310() \
  41. || cpu_is_pxa935())
  42. struct pxamci_host {
  43. struct mmc_host *mmc;
  44. spinlock_t lock;
  45. struct resource *res;
  46. void __iomem *base;
  47. struct clk *clk;
  48. unsigned long clkrate;
  49. unsigned int clkrt;
  50. unsigned int cmdat;
  51. unsigned int imask;
  52. unsigned int power_mode;
  53. unsigned long detect_delay_ms;
  54. bool use_ro_gpio;
  55. struct gpio_desc *power;
  56. struct pxamci_platform_data *pdata;
  57. struct mmc_request *mrq;
  58. struct mmc_command *cmd;
  59. struct mmc_data *data;
  60. struct dma_chan *dma_chan_rx;
  61. struct dma_chan *dma_chan_tx;
  62. dma_cookie_t dma_cookie;
  63. unsigned int dma_len;
  64. unsigned int dma_dir;
  65. };
  66. static int pxamci_init_ocr(struct pxamci_host *host)
  67. {
  68. struct mmc_host *mmc = host->mmc;
  69. int ret;
  70. ret = mmc_regulator_get_supply(mmc);
  71. if (ret < 0)
  72. return ret;
  73. if (IS_ERR(mmc->supply.vmmc)) {
  74. /* fall-back to platform data */
  75. mmc->ocr_avail = host->pdata ?
  76. host->pdata->ocr_mask :
  77. MMC_VDD_32_33 | MMC_VDD_33_34;
  78. }
  79. return 0;
  80. }
  81. static inline int pxamci_set_power(struct pxamci_host *host,
  82. unsigned char power_mode,
  83. unsigned int vdd)
  84. {
  85. struct mmc_host *mmc = host->mmc;
  86. struct regulator *supply = mmc->supply.vmmc;
  87. if (!IS_ERR(supply))
  88. return mmc_regulator_set_ocr(mmc, supply, vdd);
  89. if (host->power) {
  90. bool on = !!((1 << vdd) & host->pdata->ocr_mask);
  91. gpiod_set_value(host->power, on);
  92. }
  93. if (host->pdata && host->pdata->setpower)
  94. return host->pdata->setpower(mmc_dev(host->mmc), vdd);
  95. return 0;
  96. }
  97. static void pxamci_stop_clock(struct pxamci_host *host)
  98. {
  99. if (readl(host->base + MMC_STAT) & STAT_CLK_EN) {
  100. unsigned long timeout = 10000;
  101. unsigned int v;
  102. writel(STOP_CLOCK, host->base + MMC_STRPCL);
  103. do {
  104. v = readl(host->base + MMC_STAT);
  105. if (!(v & STAT_CLK_EN))
  106. break;
  107. udelay(1);
  108. } while (timeout--);
  109. if (v & STAT_CLK_EN)
  110. dev_err(mmc_dev(host->mmc), "unable to stop clock\n");
  111. }
  112. }
  113. static void pxamci_enable_irq(struct pxamci_host *host, unsigned int mask)
  114. {
  115. unsigned long flags;
  116. spin_lock_irqsave(&host->lock, flags);
  117. host->imask &= ~mask;
  118. writel(host->imask, host->base + MMC_I_MASK);
  119. spin_unlock_irqrestore(&host->lock, flags);
  120. }
  121. static void pxamci_disable_irq(struct pxamci_host *host, unsigned int mask)
  122. {
  123. unsigned long flags;
  124. spin_lock_irqsave(&host->lock, flags);
  125. host->imask |= mask;
  126. writel(host->imask, host->base + MMC_I_MASK);
  127. spin_unlock_irqrestore(&host->lock, flags);
  128. }
  129. static void pxamci_dma_irq(void *param);
  130. static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data)
  131. {
  132. struct dma_async_tx_descriptor *tx;
  133. enum dma_transfer_direction direction;
  134. struct dma_slave_config config;
  135. struct dma_chan *chan;
  136. unsigned int nob = data->blocks;
  137. unsigned long long clks;
  138. unsigned int timeout;
  139. int ret;
  140. host->data = data;
  141. writel(nob, host->base + MMC_NOB);
  142. writel(data->blksz, host->base + MMC_BLKLEN);
  143. clks = (unsigned long long)data->timeout_ns * host->clkrate;
  144. do_div(clks, 1000000000UL);
  145. timeout = (unsigned int)clks + (data->timeout_clks << host->clkrt);
  146. writel((timeout + 255) / 256, host->base + MMC_RDTO);
  147. memset(&config, 0, sizeof(config));
  148. config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  149. config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  150. config.src_addr = host->res->start + MMC_RXFIFO;
  151. config.dst_addr = host->res->start + MMC_TXFIFO;
  152. config.src_maxburst = 32;
  153. config.dst_maxburst = 32;
  154. if (data->flags & MMC_DATA_READ) {
  155. host->dma_dir = DMA_FROM_DEVICE;
  156. direction = DMA_DEV_TO_MEM;
  157. chan = host->dma_chan_rx;
  158. } else {
  159. host->dma_dir = DMA_TO_DEVICE;
  160. direction = DMA_MEM_TO_DEV;
  161. chan = host->dma_chan_tx;
  162. }
  163. config.direction = direction;
  164. ret = dmaengine_slave_config(chan, &config);
  165. if (ret < 0) {
  166. dev_err(mmc_dev(host->mmc), "dma slave config failed\n");
  167. return;
  168. }
  169. host->dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
  170. host->dma_dir);
  171. tx = dmaengine_prep_slave_sg(chan, data->sg, host->dma_len, direction,
  172. DMA_PREP_INTERRUPT);
  173. if (!tx) {
  174. dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
  175. return;
  176. }
  177. if (!(data->flags & MMC_DATA_READ)) {
  178. tx->callback = pxamci_dma_irq;
  179. tx->callback_param = host;
  180. }
  181. host->dma_cookie = dmaengine_submit(tx);
  182. /*
  183. * workaround for erratum #91:
  184. * only start DMA now if we are doing a read,
  185. * otherwise we wait until CMD/RESP has finished
  186. * before starting DMA.
  187. */
  188. if (!cpu_is_pxa27x() || data->flags & MMC_DATA_READ)
  189. dma_async_issue_pending(chan);
  190. }
  191. static void pxamci_start_cmd(struct pxamci_host *host, struct mmc_command *cmd, unsigned int cmdat)
  192. {
  193. WARN_ON(host->cmd != NULL);
  194. host->cmd = cmd;
  195. if (cmd->flags & MMC_RSP_BUSY)
  196. cmdat |= CMDAT_BUSY;
  197. #define RSP_TYPE(x) ((x) & ~(MMC_RSP_BUSY|MMC_RSP_OPCODE))
  198. switch (RSP_TYPE(mmc_resp_type(cmd))) {
  199. case RSP_TYPE(MMC_RSP_R1): /* r1, r1b, r6, r7 */
  200. cmdat |= CMDAT_RESP_SHORT;
  201. break;
  202. case RSP_TYPE(MMC_RSP_R3):
  203. cmdat |= CMDAT_RESP_R3;
  204. break;
  205. case RSP_TYPE(MMC_RSP_R2):
  206. cmdat |= CMDAT_RESP_R2;
  207. break;
  208. default:
  209. break;
  210. }
  211. writel(cmd->opcode, host->base + MMC_CMD);
  212. writel(cmd->arg >> 16, host->base + MMC_ARGH);
  213. writel(cmd->arg & 0xffff, host->base + MMC_ARGL);
  214. writel(cmdat, host->base + MMC_CMDAT);
  215. writel(host->clkrt, host->base + MMC_CLKRT);
  216. writel(START_CLOCK, host->base + MMC_STRPCL);
  217. pxamci_enable_irq(host, END_CMD_RES);
  218. }
  219. static void pxamci_finish_request(struct pxamci_host *host, struct mmc_request *mrq)
  220. {
  221. host->mrq = NULL;
  222. host->cmd = NULL;
  223. host->data = NULL;
  224. mmc_request_done(host->mmc, mrq);
  225. }
  226. static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat)
  227. {
  228. struct mmc_command *cmd = host->cmd;
  229. int i;
  230. u32 v;
  231. if (!cmd)
  232. return 0;
  233. host->cmd = NULL;
  234. /*
  235. * Did I mention this is Sick. We always need to
  236. * discard the upper 8 bits of the first 16-bit word.
  237. */
  238. v = readl(host->base + MMC_RES) & 0xffff;
  239. for (i = 0; i < 4; i++) {
  240. u32 w1 = readl(host->base + MMC_RES) & 0xffff;
  241. u32 w2 = readl(host->base + MMC_RES) & 0xffff;
  242. cmd->resp[i] = v << 24 | w1 << 8 | w2 >> 8;
  243. v = w2;
  244. }
  245. if (stat & STAT_TIME_OUT_RESPONSE) {
  246. cmd->error = -ETIMEDOUT;
  247. } else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
  248. /*
  249. * workaround for erratum #42:
  250. * Intel PXA27x Family Processor Specification Update Rev 001
  251. * A bogus CRC error can appear if the msb of a 136 bit
  252. * response is a one.
  253. */
  254. if (cpu_is_pxa27x() &&
  255. (cmd->flags & MMC_RSP_136 && cmd->resp[0] & 0x80000000))
  256. pr_debug("ignoring CRC from command %d - *risky*\n", cmd->opcode);
  257. else
  258. cmd->error = -EILSEQ;
  259. }
  260. pxamci_disable_irq(host, END_CMD_RES);
  261. if (host->data && !cmd->error) {
  262. pxamci_enable_irq(host, DATA_TRAN_DONE);
  263. /*
  264. * workaround for erratum #91, if doing write
  265. * enable DMA late
  266. */
  267. if (cpu_is_pxa27x() && host->data->flags & MMC_DATA_WRITE)
  268. dma_async_issue_pending(host->dma_chan_tx);
  269. } else {
  270. pxamci_finish_request(host, host->mrq);
  271. }
  272. return 1;
  273. }
  274. static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
  275. {
  276. struct mmc_data *data = host->data;
  277. struct dma_chan *chan;
  278. if (!data)
  279. return 0;
  280. if (data->flags & MMC_DATA_READ)
  281. chan = host->dma_chan_rx;
  282. else
  283. chan = host->dma_chan_tx;
  284. dma_unmap_sg(chan->device->dev,
  285. data->sg, data->sg_len, host->dma_dir);
  286. if (stat & STAT_READ_TIME_OUT)
  287. data->error = -ETIMEDOUT;
  288. else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR))
  289. data->error = -EILSEQ;
  290. /*
  291. * There appears to be a hardware design bug here. There seems to
  292. * be no way to find out how much data was transferred to the card.
  293. * This means that if there was an error on any block, we mark all
  294. * data blocks as being in error.
  295. */
  296. if (!data->error)
  297. data->bytes_xfered = data->blocks * data->blksz;
  298. else
  299. data->bytes_xfered = 0;
  300. pxamci_disable_irq(host, DATA_TRAN_DONE);
  301. host->data = NULL;
  302. if (host->mrq->stop) {
  303. pxamci_stop_clock(host);
  304. pxamci_start_cmd(host, host->mrq->stop, host->cmdat);
  305. } else {
  306. pxamci_finish_request(host, host->mrq);
  307. }
  308. return 1;
  309. }
  310. static irqreturn_t pxamci_irq(int irq, void *devid)
  311. {
  312. struct pxamci_host *host = devid;
  313. unsigned int ireg;
  314. int handled = 0;
  315. ireg = readl(host->base + MMC_I_REG) & ~readl(host->base + MMC_I_MASK);
  316. if (ireg) {
  317. unsigned stat = readl(host->base + MMC_STAT);
  318. pr_debug("PXAMCI: irq %08x stat %08x\n", ireg, stat);
  319. if (ireg & END_CMD_RES)
  320. handled |= pxamci_cmd_done(host, stat);
  321. if (ireg & DATA_TRAN_DONE)
  322. handled |= pxamci_data_done(host, stat);
  323. if (ireg & SDIO_INT) {
  324. mmc_signal_sdio_irq(host->mmc);
  325. handled = 1;
  326. }
  327. }
  328. return IRQ_RETVAL(handled);
  329. }
  330. static void pxamci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  331. {
  332. struct pxamci_host *host = mmc_priv(mmc);
  333. unsigned int cmdat;
  334. WARN_ON(host->mrq != NULL);
  335. host->mrq = mrq;
  336. pxamci_stop_clock(host);
  337. cmdat = host->cmdat;
  338. host->cmdat &= ~CMDAT_INIT;
  339. if (mrq->data) {
  340. pxamci_setup_data(host, mrq->data);
  341. cmdat &= ~CMDAT_BUSY;
  342. cmdat |= CMDAT_DATAEN | CMDAT_DMAEN;
  343. if (mrq->data->flags & MMC_DATA_WRITE)
  344. cmdat |= CMDAT_WRITE;
  345. }
  346. pxamci_start_cmd(host, mrq->cmd, cmdat);
  347. }
  348. static int pxamci_get_ro(struct mmc_host *mmc)
  349. {
  350. struct pxamci_host *host = mmc_priv(mmc);
  351. if (host->use_ro_gpio)
  352. return mmc_gpio_get_ro(mmc);
  353. if (host->pdata && host->pdata->get_ro)
  354. return !!host->pdata->get_ro(mmc_dev(mmc));
  355. /*
  356. * Board doesn't support read only detection; let the mmc core
  357. * decide what to do.
  358. */
  359. return -ENOSYS;
  360. }
  361. static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  362. {
  363. struct pxamci_host *host = mmc_priv(mmc);
  364. if (ios->clock) {
  365. unsigned long rate = host->clkrate;
  366. unsigned int clk = rate / ios->clock;
  367. if (host->clkrt == CLKRT_OFF)
  368. clk_prepare_enable(host->clk);
  369. if (ios->clock == 26000000) {
  370. /* to support 26MHz */
  371. host->clkrt = 7;
  372. } else {
  373. /* to handle (19.5MHz, 26MHz) */
  374. if (!clk)
  375. clk = 1;
  376. /*
  377. * clk might result in a lower divisor than we
  378. * desire. check for that condition and adjust
  379. * as appropriate.
  380. */
  381. if (rate / clk > ios->clock)
  382. clk <<= 1;
  383. host->clkrt = fls(clk) - 1;
  384. }
  385. /*
  386. * we write clkrt on the next command
  387. */
  388. } else {
  389. pxamci_stop_clock(host);
  390. if (host->clkrt != CLKRT_OFF) {
  391. host->clkrt = CLKRT_OFF;
  392. clk_disable_unprepare(host->clk);
  393. }
  394. }
  395. if (host->power_mode != ios->power_mode) {
  396. int ret;
  397. host->power_mode = ios->power_mode;
  398. ret = pxamci_set_power(host, ios->power_mode, ios->vdd);
  399. if (ret) {
  400. dev_err(mmc_dev(mmc), "unable to set power\n");
  401. /*
  402. * The .set_ios() function in the mmc_host_ops
  403. * struct return void, and failing to set the
  404. * power should be rare so we print an error and
  405. * return here.
  406. */
  407. return;
  408. }
  409. if (ios->power_mode == MMC_POWER_ON)
  410. host->cmdat |= CMDAT_INIT;
  411. }
  412. if (ios->bus_width == MMC_BUS_WIDTH_4)
  413. host->cmdat |= CMDAT_SD_4DAT;
  414. else
  415. host->cmdat &= ~CMDAT_SD_4DAT;
  416. dev_dbg(mmc_dev(mmc), "PXAMCI: clkrt = %x cmdat = %x\n",
  417. host->clkrt, host->cmdat);
  418. }
  419. static void pxamci_enable_sdio_irq(struct mmc_host *host, int enable)
  420. {
  421. struct pxamci_host *pxa_host = mmc_priv(host);
  422. if (enable)
  423. pxamci_enable_irq(pxa_host, SDIO_INT);
  424. else
  425. pxamci_disable_irq(pxa_host, SDIO_INT);
  426. }
  427. static const struct mmc_host_ops pxamci_ops = {
  428. .request = pxamci_request,
  429. .get_cd = mmc_gpio_get_cd,
  430. .get_ro = pxamci_get_ro,
  431. .set_ios = pxamci_set_ios,
  432. .enable_sdio_irq = pxamci_enable_sdio_irq,
  433. };
  434. static void pxamci_dma_irq(void *param)
  435. {
  436. struct pxamci_host *host = param;
  437. struct dma_tx_state state;
  438. enum dma_status status;
  439. struct dma_chan *chan;
  440. unsigned long flags;
  441. spin_lock_irqsave(&host->lock, flags);
  442. if (!host->data)
  443. goto out_unlock;
  444. if (host->data->flags & MMC_DATA_READ)
  445. chan = host->dma_chan_rx;
  446. else
  447. chan = host->dma_chan_tx;
  448. status = dmaengine_tx_status(chan, host->dma_cookie, &state);
  449. if (likely(status == DMA_COMPLETE)) {
  450. writel(BUF_PART_FULL, host->base + MMC_PRTBUF);
  451. } else {
  452. pr_err("%s: DMA error on %s channel\n", mmc_hostname(host->mmc),
  453. host->data->flags & MMC_DATA_READ ? "rx" : "tx");
  454. host->data->error = -EIO;
  455. pxamci_data_done(host, 0);
  456. }
  457. out_unlock:
  458. spin_unlock_irqrestore(&host->lock, flags);
  459. }
  460. static irqreturn_t pxamci_detect_irq(int irq, void *devid)
  461. {
  462. struct pxamci_host *host = mmc_priv(devid);
  463. mmc_detect_change(devid, msecs_to_jiffies(host->detect_delay_ms));
  464. return IRQ_HANDLED;
  465. }
  466. #ifdef CONFIG_OF
  467. static const struct of_device_id pxa_mmc_dt_ids[] = {
  468. { .compatible = "marvell,pxa-mmc" },
  469. { }
  470. };
  471. MODULE_DEVICE_TABLE(of, pxa_mmc_dt_ids);
  472. static int pxamci_of_init(struct platform_device *pdev,
  473. struct mmc_host *mmc)
  474. {
  475. struct device_node *np = pdev->dev.of_node;
  476. struct pxamci_host *host = mmc_priv(mmc);
  477. u32 tmp;
  478. int ret;
  479. if (!np)
  480. return 0;
  481. /* pxa-mmc specific */
  482. if (of_property_read_u32(np, "pxa-mmc,detect-delay-ms", &tmp) == 0)
  483. host->detect_delay_ms = tmp;
  484. ret = mmc_of_parse(mmc);
  485. if (ret < 0)
  486. return ret;
  487. return 0;
  488. }
  489. #else
  490. static int pxamci_of_init(struct platform_device *pdev,
  491. struct mmc_host *mmc)
  492. {
  493. return 0;
  494. }
  495. #endif
  496. static int pxamci_probe(struct platform_device *pdev)
  497. {
  498. struct mmc_host *mmc;
  499. struct pxamci_host *host = NULL;
  500. struct device *dev = &pdev->dev;
  501. struct resource *r;
  502. int ret, irq;
  503. irq = platform_get_irq(pdev, 0);
  504. if (irq < 0)
  505. return irq;
  506. mmc = devm_mmc_alloc_host(dev, sizeof(*host));
  507. if (!mmc)
  508. return -ENOMEM;
  509. mmc->ops = &pxamci_ops;
  510. /*
  511. * We can do SG-DMA, but we don't because we never know how much
  512. * data we successfully wrote to the card.
  513. */
  514. mmc->max_segs = NR_SG;
  515. /*
  516. * Our hardware DMA can handle a maximum of one page per SG entry.
  517. */
  518. mmc->max_seg_size = PAGE_SIZE;
  519. /*
  520. * Block length register is only 10 bits before PXA27x.
  521. */
  522. mmc->max_blk_size = cpu_is_pxa25x() ? 1023 : 2048;
  523. /*
  524. * Block count register is 16 bits.
  525. */
  526. mmc->max_blk_count = 65535;
  527. ret = pxamci_of_init(pdev, mmc);
  528. if (ret)
  529. return ret;
  530. host = mmc_priv(mmc);
  531. host->mmc = mmc;
  532. host->pdata = pdev->dev.platform_data;
  533. host->clkrt = CLKRT_OFF;
  534. host->clk = devm_clk_get(dev, NULL);
  535. if (IS_ERR(host->clk))
  536. return dev_err_probe(dev, PTR_ERR(host->clk),
  537. "Failed to acquire clock\n");
  538. host->clkrate = clk_get_rate(host->clk);
  539. /*
  540. * Calculate minimum clock rate, rounding up.
  541. */
  542. mmc->f_min = (host->clkrate + 63) / 64;
  543. mmc->f_max = (mmc_has_26MHz()) ? 26000000 : host->clkrate;
  544. ret = pxamci_init_ocr(host);
  545. if (ret < 0)
  546. return ret;
  547. mmc->caps = 0;
  548. host->cmdat = 0;
  549. if (!cpu_is_pxa25x()) {
  550. mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
  551. host->cmdat |= CMDAT_SDIO_INT_EN;
  552. if (mmc_has_26MHz())
  553. mmc->caps |= MMC_CAP_MMC_HIGHSPEED |
  554. MMC_CAP_SD_HIGHSPEED;
  555. }
  556. spin_lock_init(&host->lock);
  557. host->imask = MMC_I_MASK_ALL;
  558. host->base = devm_platform_get_and_ioremap_resource(pdev, 0, &r);
  559. if (IS_ERR(host->base))
  560. return PTR_ERR(host->base);
  561. host->res = r;
  562. /*
  563. * Ensure that the host controller is shut down, and setup
  564. * with our defaults.
  565. */
  566. pxamci_stop_clock(host);
  567. writel(0, host->base + MMC_SPI);
  568. writel(64, host->base + MMC_RESTO);
  569. writel(host->imask, host->base + MMC_I_MASK);
  570. ret = devm_request_irq(dev, irq, pxamci_irq, 0,
  571. DRIVER_NAME, host);
  572. if (ret)
  573. return ret;
  574. platform_set_drvdata(pdev, mmc);
  575. host->dma_chan_rx = devm_dma_request_chan(dev, "rx");
  576. if (IS_ERR(host->dma_chan_rx))
  577. return dev_err_probe(dev, PTR_ERR(host->dma_chan_rx),
  578. "unable to request rx dma channel\n");
  579. host->dma_chan_tx = devm_dma_request_chan(dev, "tx");
  580. if (IS_ERR(host->dma_chan_tx))
  581. return dev_err_probe(dev, PTR_ERR(host->dma_chan_tx),
  582. "unable to request tx dma channel\n");
  583. if (host->pdata) {
  584. host->detect_delay_ms = host->pdata->detect_delay_ms;
  585. host->power = devm_gpiod_get_optional(dev, "power", GPIOD_OUT_LOW);
  586. if (IS_ERR(host->power))
  587. return dev_err_probe(dev, PTR_ERR(host->power),
  588. "Failed requesting gpio_power\n");
  589. /* FIXME: should we pass detection delay to debounce? */
  590. ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0);
  591. if (ret && ret != -ENOENT)
  592. return dev_err_probe(dev, ret, "Failed requesting gpio_cd\n");
  593. if (!host->pdata->gpio_card_ro_invert)
  594. mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
  595. ret = mmc_gpiod_request_ro(mmc, "wp", 0, 0);
  596. if (ret && ret != -ENOENT)
  597. return dev_err_probe(dev, ret, "Failed requesting gpio_ro\n");
  598. if (!ret)
  599. host->use_ro_gpio = true;
  600. if (host->pdata->init)
  601. host->pdata->init(dev, pxamci_detect_irq, mmc);
  602. if (host->power && host->pdata->setpower)
  603. dev_warn(dev, "gpio_power and setpower() both defined\n");
  604. if (host->use_ro_gpio && host->pdata->get_ro)
  605. dev_warn(dev, "gpio_ro and get_ro() both defined\n");
  606. }
  607. ret = mmc_add_host(mmc);
  608. if (ret) {
  609. if (host->pdata && host->pdata->exit)
  610. host->pdata->exit(dev, mmc);
  611. }
  612. return ret;
  613. }
  614. static void pxamci_remove(struct platform_device *pdev)
  615. {
  616. struct mmc_host *mmc = platform_get_drvdata(pdev);
  617. if (mmc) {
  618. struct pxamci_host *host = mmc_priv(mmc);
  619. mmc_remove_host(mmc);
  620. if (host->pdata && host->pdata->exit)
  621. host->pdata->exit(&pdev->dev, mmc);
  622. pxamci_stop_clock(host);
  623. writel(TXFIFO_WR_REQ|RXFIFO_RD_REQ|CLK_IS_OFF|STOP_CMD|
  624. END_CMD_RES|PRG_DONE|DATA_TRAN_DONE,
  625. host->base + MMC_I_MASK);
  626. dmaengine_terminate_all(host->dma_chan_rx);
  627. dmaengine_terminate_all(host->dma_chan_tx);
  628. }
  629. }
  630. static struct platform_driver pxamci_driver = {
  631. .probe = pxamci_probe,
  632. .remove = pxamci_remove,
  633. .driver = {
  634. .name = DRIVER_NAME,
  635. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  636. .of_match_table = of_match_ptr(pxa_mmc_dt_ids),
  637. },
  638. };
  639. module_platform_driver(pxamci_driver);
  640. MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver");
  641. MODULE_LICENSE("GPL");
  642. MODULE_ALIAS("platform:pxa2xx-mci");