mxcmmc.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. /*
  2. * linux/drivers/mmc/host/mxcmmc.c - Freescale i.MX MMCI driver
  3. *
  4. * This is a driver for the SDHC controller found in Freescale MX2/MX3
  5. * SoCs. It is basically the same hardware as found on MX1 (imxmmc.c).
  6. * Unlike the hardware found on MX1, this hardware just works and does
  7. * not need all the quirks found in imxmmc.c, hence the separate driver.
  8. *
  9. * Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
  10. * Copyright (C) 2006 Pavel Pisa, PiKRON <ppisa@pikron.com>
  11. *
  12. * derived from pxamci.c by Russell King
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. *
  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/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/mmc/host.h>
  28. #include <linux/mmc/card.h>
  29. #include <linux/delay.h>
  30. #include <linux/clk.h>
  31. #include <linux/io.h>
  32. #include <linux/gpio.h>
  33. #include <linux/regulator/consumer.h>
  34. #include <linux/dmaengine.h>
  35. #include <linux/types.h>
  36. #include <asm/dma.h>
  37. #include <asm/irq.h>
  38. #include <asm/sizes.h>
  39. #include <mach/mmc.h>
  40. #include <mach/dma.h>
  41. #include <mach/hardware.h>
  42. #define DRIVER_NAME "mxc-mmc"
  43. #define MMC_REG_STR_STP_CLK 0x00
  44. #define MMC_REG_STATUS 0x04
  45. #define MMC_REG_CLK_RATE 0x08
  46. #define MMC_REG_CMD_DAT_CONT 0x0C
  47. #define MMC_REG_RES_TO 0x10
  48. #define MMC_REG_READ_TO 0x14
  49. #define MMC_REG_BLK_LEN 0x18
  50. #define MMC_REG_NOB 0x1C
  51. #define MMC_REG_REV_NO 0x20
  52. #define MMC_REG_INT_CNTR 0x24
  53. #define MMC_REG_CMD 0x28
  54. #define MMC_REG_ARG 0x2C
  55. #define MMC_REG_RES_FIFO 0x34
  56. #define MMC_REG_BUFFER_ACCESS 0x38
  57. #define STR_STP_CLK_RESET (1 << 3)
  58. #define STR_STP_CLK_START_CLK (1 << 1)
  59. #define STR_STP_CLK_STOP_CLK (1 << 0)
  60. #define STATUS_CARD_INSERTION (1 << 31)
  61. #define STATUS_CARD_REMOVAL (1 << 30)
  62. #define STATUS_YBUF_EMPTY (1 << 29)
  63. #define STATUS_XBUF_EMPTY (1 << 28)
  64. #define STATUS_YBUF_FULL (1 << 27)
  65. #define STATUS_XBUF_FULL (1 << 26)
  66. #define STATUS_BUF_UND_RUN (1 << 25)
  67. #define STATUS_BUF_OVFL (1 << 24)
  68. #define STATUS_SDIO_INT_ACTIVE (1 << 14)
  69. #define STATUS_END_CMD_RESP (1 << 13)
  70. #define STATUS_WRITE_OP_DONE (1 << 12)
  71. #define STATUS_DATA_TRANS_DONE (1 << 11)
  72. #define STATUS_READ_OP_DONE (1 << 11)
  73. #define STATUS_WR_CRC_ERROR_CODE_MASK (3 << 10)
  74. #define STATUS_CARD_BUS_CLK_RUN (1 << 8)
  75. #define STATUS_BUF_READ_RDY (1 << 7)
  76. #define STATUS_BUF_WRITE_RDY (1 << 6)
  77. #define STATUS_RESP_CRC_ERR (1 << 5)
  78. #define STATUS_CRC_READ_ERR (1 << 3)
  79. #define STATUS_CRC_WRITE_ERR (1 << 2)
  80. #define STATUS_TIME_OUT_RESP (1 << 1)
  81. #define STATUS_TIME_OUT_READ (1 << 0)
  82. #define STATUS_ERR_MASK 0x2f
  83. #define CMD_DAT_CONT_CMD_RESP_LONG_OFF (1 << 12)
  84. #define CMD_DAT_CONT_STOP_READWAIT (1 << 11)
  85. #define CMD_DAT_CONT_START_READWAIT (1 << 10)
  86. #define CMD_DAT_CONT_BUS_WIDTH_4 (2 << 8)
  87. #define CMD_DAT_CONT_INIT (1 << 7)
  88. #define CMD_DAT_CONT_WRITE (1 << 4)
  89. #define CMD_DAT_CONT_DATA_ENABLE (1 << 3)
  90. #define CMD_DAT_CONT_RESPONSE_48BIT_CRC (1 << 0)
  91. #define CMD_DAT_CONT_RESPONSE_136BIT (2 << 0)
  92. #define CMD_DAT_CONT_RESPONSE_48BIT (3 << 0)
  93. #define INT_SDIO_INT_WKP_EN (1 << 18)
  94. #define INT_CARD_INSERTION_WKP_EN (1 << 17)
  95. #define INT_CARD_REMOVAL_WKP_EN (1 << 16)
  96. #define INT_CARD_INSERTION_EN (1 << 15)
  97. #define INT_CARD_REMOVAL_EN (1 << 14)
  98. #define INT_SDIO_IRQ_EN (1 << 13)
  99. #define INT_DAT0_EN (1 << 12)
  100. #define INT_BUF_READ_EN (1 << 4)
  101. #define INT_BUF_WRITE_EN (1 << 3)
  102. #define INT_END_CMD_RES_EN (1 << 2)
  103. #define INT_WRITE_OP_DONE_EN (1 << 1)
  104. #define INT_READ_OP_EN (1 << 0)
  105. struct mxcmci_host {
  106. struct mmc_host *mmc;
  107. struct resource *res;
  108. void __iomem *base;
  109. int irq;
  110. int detect_irq;
  111. struct dma_chan *dma;
  112. struct dma_async_tx_descriptor *desc;
  113. int do_dma;
  114. int default_irq_mask;
  115. int use_sdio;
  116. unsigned int power_mode;
  117. struct imxmmc_platform_data *pdata;
  118. struct mmc_request *req;
  119. struct mmc_command *cmd;
  120. struct mmc_data *data;
  121. unsigned int datasize;
  122. unsigned int dma_dir;
  123. u16 rev_no;
  124. unsigned int cmdat;
  125. struct clk *clk;
  126. int clock;
  127. struct work_struct datawork;
  128. spinlock_t lock;
  129. struct regulator *vcc;
  130. int burstlen;
  131. int dmareq;
  132. struct dma_slave_config dma_slave_config;
  133. struct imx_dma_data dma_data;
  134. };
  135. static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios);
  136. static inline void mxcmci_init_ocr(struct mxcmci_host *host)
  137. {
  138. host->vcc = regulator_get(mmc_dev(host->mmc), "vmmc");
  139. if (IS_ERR(host->vcc)) {
  140. host->vcc = NULL;
  141. } else {
  142. host->mmc->ocr_avail = mmc_regulator_get_ocrmask(host->vcc);
  143. if (host->pdata && host->pdata->ocr_avail)
  144. dev_warn(mmc_dev(host->mmc),
  145. "pdata->ocr_avail will not be used\n");
  146. }
  147. if (host->vcc == NULL) {
  148. /* fall-back to platform data */
  149. if (host->pdata && host->pdata->ocr_avail)
  150. host->mmc->ocr_avail = host->pdata->ocr_avail;
  151. else
  152. host->mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  153. }
  154. }
  155. static inline void mxcmci_set_power(struct mxcmci_host *host,
  156. unsigned char power_mode,
  157. unsigned int vdd)
  158. {
  159. if (host->vcc) {
  160. if (power_mode == MMC_POWER_UP)
  161. mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
  162. else if (power_mode == MMC_POWER_OFF)
  163. mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
  164. }
  165. if (host->pdata && host->pdata->setpower)
  166. host->pdata->setpower(mmc_dev(host->mmc), vdd);
  167. }
  168. static inline int mxcmci_use_dma(struct mxcmci_host *host)
  169. {
  170. return host->do_dma;
  171. }
  172. static void mxcmci_softreset(struct mxcmci_host *host)
  173. {
  174. int i;
  175. dev_dbg(mmc_dev(host->mmc), "mxcmci_softreset\n");
  176. /* reset sequence */
  177. writew(STR_STP_CLK_RESET, host->base + MMC_REG_STR_STP_CLK);
  178. writew(STR_STP_CLK_RESET | STR_STP_CLK_START_CLK,
  179. host->base + MMC_REG_STR_STP_CLK);
  180. for (i = 0; i < 8; i++)
  181. writew(STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK);
  182. writew(0xff, host->base + MMC_REG_RES_TO);
  183. }
  184. static int mxcmci_setup_dma(struct mmc_host *mmc);
  185. static int mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data)
  186. {
  187. unsigned int nob = data->blocks;
  188. unsigned int blksz = data->blksz;
  189. unsigned int datasize = nob * blksz;
  190. struct scatterlist *sg;
  191. enum dma_transfer_direction slave_dirn;
  192. int i, nents;
  193. if (data->flags & MMC_DATA_STREAM)
  194. nob = 0xffff;
  195. host->data = data;
  196. data->bytes_xfered = 0;
  197. writew(nob, host->base + MMC_REG_NOB);
  198. writew(blksz, host->base + MMC_REG_BLK_LEN);
  199. host->datasize = datasize;
  200. if (!mxcmci_use_dma(host))
  201. return 0;
  202. for_each_sg(data->sg, sg, data->sg_len, i) {
  203. if (sg->offset & 3 || sg->length & 3) {
  204. host->do_dma = 0;
  205. return 0;
  206. }
  207. }
  208. if (data->flags & MMC_DATA_READ) {
  209. host->dma_dir = DMA_FROM_DEVICE;
  210. slave_dirn = DMA_DEV_TO_MEM;
  211. } else {
  212. host->dma_dir = DMA_TO_DEVICE;
  213. slave_dirn = DMA_MEM_TO_DEV;
  214. }
  215. nents = dma_map_sg(host->dma->device->dev, data->sg,
  216. data->sg_len, host->dma_dir);
  217. if (nents != data->sg_len)
  218. return -EINVAL;
  219. host->desc = dmaengine_prep_slave_sg(host->dma,
  220. data->sg, data->sg_len, slave_dirn,
  221. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  222. if (!host->desc) {
  223. dma_unmap_sg(host->dma->device->dev, data->sg, data->sg_len,
  224. host->dma_dir);
  225. host->do_dma = 0;
  226. return 0; /* Fall back to PIO */
  227. }
  228. wmb();
  229. dmaengine_submit(host->desc);
  230. dma_async_issue_pending(host->dma);
  231. return 0;
  232. }
  233. static int mxcmci_start_cmd(struct mxcmci_host *host, struct mmc_command *cmd,
  234. unsigned int cmdat)
  235. {
  236. u32 int_cntr = host->default_irq_mask;
  237. unsigned long flags;
  238. WARN_ON(host->cmd != NULL);
  239. host->cmd = cmd;
  240. switch (mmc_resp_type(cmd)) {
  241. case MMC_RSP_R1: /* short CRC, OPCODE */
  242. case MMC_RSP_R1B:/* short CRC, OPCODE, BUSY */
  243. cmdat |= CMD_DAT_CONT_RESPONSE_48BIT_CRC;
  244. break;
  245. case MMC_RSP_R2: /* long 136 bit + CRC */
  246. cmdat |= CMD_DAT_CONT_RESPONSE_136BIT;
  247. break;
  248. case MMC_RSP_R3: /* short */
  249. cmdat |= CMD_DAT_CONT_RESPONSE_48BIT;
  250. break;
  251. case MMC_RSP_NONE:
  252. break;
  253. default:
  254. dev_err(mmc_dev(host->mmc), "unhandled response type 0x%x\n",
  255. mmc_resp_type(cmd));
  256. cmd->error = -EINVAL;
  257. return -EINVAL;
  258. }
  259. int_cntr = INT_END_CMD_RES_EN;
  260. if (mxcmci_use_dma(host))
  261. int_cntr |= INT_READ_OP_EN | INT_WRITE_OP_DONE_EN;
  262. spin_lock_irqsave(&host->lock, flags);
  263. if (host->use_sdio)
  264. int_cntr |= INT_SDIO_IRQ_EN;
  265. writel(int_cntr, host->base + MMC_REG_INT_CNTR);
  266. spin_unlock_irqrestore(&host->lock, flags);
  267. writew(cmd->opcode, host->base + MMC_REG_CMD);
  268. writel(cmd->arg, host->base + MMC_REG_ARG);
  269. writew(cmdat, host->base + MMC_REG_CMD_DAT_CONT);
  270. return 0;
  271. }
  272. static void mxcmci_finish_request(struct mxcmci_host *host,
  273. struct mmc_request *req)
  274. {
  275. u32 int_cntr = host->default_irq_mask;
  276. unsigned long flags;
  277. spin_lock_irqsave(&host->lock, flags);
  278. if (host->use_sdio)
  279. int_cntr |= INT_SDIO_IRQ_EN;
  280. writel(int_cntr, host->base + MMC_REG_INT_CNTR);
  281. spin_unlock_irqrestore(&host->lock, flags);
  282. host->req = NULL;
  283. host->cmd = NULL;
  284. host->data = NULL;
  285. mmc_request_done(host->mmc, req);
  286. }
  287. static int mxcmci_finish_data(struct mxcmci_host *host, unsigned int stat)
  288. {
  289. struct mmc_data *data = host->data;
  290. int data_error;
  291. if (mxcmci_use_dma(host)) {
  292. dmaengine_terminate_all(host->dma);
  293. dma_unmap_sg(host->dma->device->dev, data->sg, data->sg_len,
  294. host->dma_dir);
  295. }
  296. if (stat & STATUS_ERR_MASK) {
  297. dev_dbg(mmc_dev(host->mmc), "request failed. status: 0x%08x\n",
  298. stat);
  299. if (stat & STATUS_CRC_READ_ERR) {
  300. dev_err(mmc_dev(host->mmc), "%s: -EILSEQ\n", __func__);
  301. data->error = -EILSEQ;
  302. } else if (stat & STATUS_CRC_WRITE_ERR) {
  303. u32 err_code = (stat >> 9) & 0x3;
  304. if (err_code == 2) { /* No CRC response */
  305. dev_err(mmc_dev(host->mmc),
  306. "%s: No CRC -ETIMEDOUT\n", __func__);
  307. data->error = -ETIMEDOUT;
  308. } else {
  309. dev_err(mmc_dev(host->mmc),
  310. "%s: -EILSEQ\n", __func__);
  311. data->error = -EILSEQ;
  312. }
  313. } else if (stat & STATUS_TIME_OUT_READ) {
  314. dev_err(mmc_dev(host->mmc),
  315. "%s: read -ETIMEDOUT\n", __func__);
  316. data->error = -ETIMEDOUT;
  317. } else {
  318. dev_err(mmc_dev(host->mmc), "%s: -EIO\n", __func__);
  319. data->error = -EIO;
  320. }
  321. } else {
  322. data->bytes_xfered = host->datasize;
  323. }
  324. data_error = data->error;
  325. host->data = NULL;
  326. return data_error;
  327. }
  328. static void mxcmci_read_response(struct mxcmci_host *host, unsigned int stat)
  329. {
  330. struct mmc_command *cmd = host->cmd;
  331. int i;
  332. u32 a, b, c;
  333. if (!cmd)
  334. return;
  335. if (stat & STATUS_TIME_OUT_RESP) {
  336. dev_dbg(mmc_dev(host->mmc), "CMD TIMEOUT\n");
  337. cmd->error = -ETIMEDOUT;
  338. } else if (stat & STATUS_RESP_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
  339. dev_dbg(mmc_dev(host->mmc), "cmd crc error\n");
  340. cmd->error = -EILSEQ;
  341. }
  342. if (cmd->flags & MMC_RSP_PRESENT) {
  343. if (cmd->flags & MMC_RSP_136) {
  344. for (i = 0; i < 4; i++) {
  345. a = readw(host->base + MMC_REG_RES_FIFO);
  346. b = readw(host->base + MMC_REG_RES_FIFO);
  347. cmd->resp[i] = a << 16 | b;
  348. }
  349. } else {
  350. a = readw(host->base + MMC_REG_RES_FIFO);
  351. b = readw(host->base + MMC_REG_RES_FIFO);
  352. c = readw(host->base + MMC_REG_RES_FIFO);
  353. cmd->resp[0] = a << 24 | b << 8 | c >> 8;
  354. }
  355. }
  356. }
  357. static int mxcmci_poll_status(struct mxcmci_host *host, u32 mask)
  358. {
  359. u32 stat;
  360. unsigned long timeout = jiffies + HZ;
  361. do {
  362. stat = readl(host->base + MMC_REG_STATUS);
  363. if (stat & STATUS_ERR_MASK)
  364. return stat;
  365. if (time_after(jiffies, timeout)) {
  366. mxcmci_softreset(host);
  367. mxcmci_set_clk_rate(host, host->clock);
  368. return STATUS_TIME_OUT_READ;
  369. }
  370. if (stat & mask)
  371. return 0;
  372. cpu_relax();
  373. } while (1);
  374. }
  375. static int mxcmci_pull(struct mxcmci_host *host, void *_buf, int bytes)
  376. {
  377. unsigned int stat;
  378. u32 *buf = _buf;
  379. while (bytes > 3) {
  380. stat = mxcmci_poll_status(host,
  381. STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
  382. if (stat)
  383. return stat;
  384. *buf++ = readl(host->base + MMC_REG_BUFFER_ACCESS);
  385. bytes -= 4;
  386. }
  387. if (bytes) {
  388. u8 *b = (u8 *)buf;
  389. u32 tmp;
  390. stat = mxcmci_poll_status(host,
  391. STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
  392. if (stat)
  393. return stat;
  394. tmp = readl(host->base + MMC_REG_BUFFER_ACCESS);
  395. memcpy(b, &tmp, bytes);
  396. }
  397. return 0;
  398. }
  399. static int mxcmci_push(struct mxcmci_host *host, void *_buf, int bytes)
  400. {
  401. unsigned int stat;
  402. u32 *buf = _buf;
  403. while (bytes > 3) {
  404. stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
  405. if (stat)
  406. return stat;
  407. writel(*buf++, host->base + MMC_REG_BUFFER_ACCESS);
  408. bytes -= 4;
  409. }
  410. if (bytes) {
  411. u8 *b = (u8 *)buf;
  412. u32 tmp;
  413. stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
  414. if (stat)
  415. return stat;
  416. memcpy(&tmp, b, bytes);
  417. writel(tmp, host->base + MMC_REG_BUFFER_ACCESS);
  418. }
  419. stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
  420. if (stat)
  421. return stat;
  422. return 0;
  423. }
  424. static int mxcmci_transfer_data(struct mxcmci_host *host)
  425. {
  426. struct mmc_data *data = host->req->data;
  427. struct scatterlist *sg;
  428. int stat, i;
  429. host->data = data;
  430. host->datasize = 0;
  431. if (data->flags & MMC_DATA_READ) {
  432. for_each_sg(data->sg, sg, data->sg_len, i) {
  433. stat = mxcmci_pull(host, sg_virt(sg), sg->length);
  434. if (stat)
  435. return stat;
  436. host->datasize += sg->length;
  437. }
  438. } else {
  439. for_each_sg(data->sg, sg, data->sg_len, i) {
  440. stat = mxcmci_push(host, sg_virt(sg), sg->length);
  441. if (stat)
  442. return stat;
  443. host->datasize += sg->length;
  444. }
  445. stat = mxcmci_poll_status(host, STATUS_WRITE_OP_DONE);
  446. if (stat)
  447. return stat;
  448. }
  449. return 0;
  450. }
  451. static void mxcmci_datawork(struct work_struct *work)
  452. {
  453. struct mxcmci_host *host = container_of(work, struct mxcmci_host,
  454. datawork);
  455. int datastat = mxcmci_transfer_data(host);
  456. writel(STATUS_READ_OP_DONE | STATUS_WRITE_OP_DONE,
  457. host->base + MMC_REG_STATUS);
  458. mxcmci_finish_data(host, datastat);
  459. if (host->req->stop) {
  460. if (mxcmci_start_cmd(host, host->req->stop, 0)) {
  461. mxcmci_finish_request(host, host->req);
  462. return;
  463. }
  464. } else {
  465. mxcmci_finish_request(host, host->req);
  466. }
  467. }
  468. static void mxcmci_data_done(struct mxcmci_host *host, unsigned int stat)
  469. {
  470. struct mmc_data *data = host->data;
  471. int data_error;
  472. if (!data)
  473. return;
  474. data_error = mxcmci_finish_data(host, stat);
  475. mxcmci_read_response(host, stat);
  476. host->cmd = NULL;
  477. if (host->req->stop) {
  478. if (mxcmci_start_cmd(host, host->req->stop, 0)) {
  479. mxcmci_finish_request(host, host->req);
  480. return;
  481. }
  482. } else {
  483. mxcmci_finish_request(host, host->req);
  484. }
  485. }
  486. static void mxcmci_cmd_done(struct mxcmci_host *host, unsigned int stat)
  487. {
  488. mxcmci_read_response(host, stat);
  489. host->cmd = NULL;
  490. if (!host->data && host->req) {
  491. mxcmci_finish_request(host, host->req);
  492. return;
  493. }
  494. /* For the DMA case the DMA engine handles the data transfer
  495. * automatically. For non DMA we have to do it ourselves.
  496. * Don't do it in interrupt context though.
  497. */
  498. if (!mxcmci_use_dma(host) && host->data)
  499. schedule_work(&host->datawork);
  500. }
  501. static irqreturn_t mxcmci_irq(int irq, void *devid)
  502. {
  503. struct mxcmci_host *host = devid;
  504. unsigned long flags;
  505. bool sdio_irq;
  506. u32 stat;
  507. stat = readl(host->base + MMC_REG_STATUS);
  508. writel(stat & ~(STATUS_SDIO_INT_ACTIVE | STATUS_DATA_TRANS_DONE |
  509. STATUS_WRITE_OP_DONE), host->base + MMC_REG_STATUS);
  510. dev_dbg(mmc_dev(host->mmc), "%s: 0x%08x\n", __func__, stat);
  511. spin_lock_irqsave(&host->lock, flags);
  512. sdio_irq = (stat & STATUS_SDIO_INT_ACTIVE) && host->use_sdio;
  513. spin_unlock_irqrestore(&host->lock, flags);
  514. if (mxcmci_use_dma(host) &&
  515. (stat & (STATUS_READ_OP_DONE | STATUS_WRITE_OP_DONE)))
  516. writel(STATUS_READ_OP_DONE | STATUS_WRITE_OP_DONE,
  517. host->base + MMC_REG_STATUS);
  518. if (sdio_irq) {
  519. writel(STATUS_SDIO_INT_ACTIVE, host->base + MMC_REG_STATUS);
  520. mmc_signal_sdio_irq(host->mmc);
  521. }
  522. if (stat & STATUS_END_CMD_RESP)
  523. mxcmci_cmd_done(host, stat);
  524. if (mxcmci_use_dma(host) &&
  525. (stat & (STATUS_DATA_TRANS_DONE | STATUS_WRITE_OP_DONE)))
  526. mxcmci_data_done(host, stat);
  527. if (host->default_irq_mask &&
  528. (stat & (STATUS_CARD_INSERTION | STATUS_CARD_REMOVAL)))
  529. mmc_detect_change(host->mmc, msecs_to_jiffies(200));
  530. return IRQ_HANDLED;
  531. }
  532. static void mxcmci_request(struct mmc_host *mmc, struct mmc_request *req)
  533. {
  534. struct mxcmci_host *host = mmc_priv(mmc);
  535. unsigned int cmdat = host->cmdat;
  536. int error;
  537. WARN_ON(host->req != NULL);
  538. host->req = req;
  539. host->cmdat &= ~CMD_DAT_CONT_INIT;
  540. if (host->dma)
  541. host->do_dma = 1;
  542. if (req->data) {
  543. error = mxcmci_setup_data(host, req->data);
  544. if (error) {
  545. req->cmd->error = error;
  546. goto out;
  547. }
  548. cmdat |= CMD_DAT_CONT_DATA_ENABLE;
  549. if (req->data->flags & MMC_DATA_WRITE)
  550. cmdat |= CMD_DAT_CONT_WRITE;
  551. }
  552. error = mxcmci_start_cmd(host, req->cmd, cmdat);
  553. out:
  554. if (error)
  555. mxcmci_finish_request(host, req);
  556. }
  557. static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios)
  558. {
  559. unsigned int divider;
  560. int prescaler = 0;
  561. unsigned int clk_in = clk_get_rate(host->clk);
  562. while (prescaler <= 0x800) {
  563. for (divider = 1; divider <= 0xF; divider++) {
  564. int x;
  565. x = (clk_in / (divider + 1));
  566. if (prescaler)
  567. x /= (prescaler * 2);
  568. if (x <= clk_ios)
  569. break;
  570. }
  571. if (divider < 0x10)
  572. break;
  573. if (prescaler == 0)
  574. prescaler = 1;
  575. else
  576. prescaler <<= 1;
  577. }
  578. writew((prescaler << 4) | divider, host->base + MMC_REG_CLK_RATE);
  579. dev_dbg(mmc_dev(host->mmc), "scaler: %d divider: %d in: %d out: %d\n",
  580. prescaler, divider, clk_in, clk_ios);
  581. }
  582. static int mxcmci_setup_dma(struct mmc_host *mmc)
  583. {
  584. struct mxcmci_host *host = mmc_priv(mmc);
  585. struct dma_slave_config *config = &host->dma_slave_config;
  586. config->dst_addr = host->res->start + MMC_REG_BUFFER_ACCESS;
  587. config->src_addr = host->res->start + MMC_REG_BUFFER_ACCESS;
  588. config->dst_addr_width = 4;
  589. config->src_addr_width = 4;
  590. config->dst_maxburst = host->burstlen;
  591. config->src_maxburst = host->burstlen;
  592. config->device_fc = false;
  593. return dmaengine_slave_config(host->dma, config);
  594. }
  595. static void mxcmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  596. {
  597. struct mxcmci_host *host = mmc_priv(mmc);
  598. int burstlen, ret;
  599. /*
  600. * use burstlen of 64 (16 words) in 4 bit mode (--> reg value 0)
  601. * use burstlen of 16 (4 words) in 1 bit mode (--> reg value 16)
  602. */
  603. if (ios->bus_width == MMC_BUS_WIDTH_4)
  604. burstlen = 16;
  605. else
  606. burstlen = 4;
  607. if (mxcmci_use_dma(host) && burstlen != host->burstlen) {
  608. host->burstlen = burstlen;
  609. ret = mxcmci_setup_dma(mmc);
  610. if (ret) {
  611. dev_err(mmc_dev(host->mmc),
  612. "failed to config DMA channel. Falling back to PIO\n");
  613. dma_release_channel(host->dma);
  614. host->do_dma = 0;
  615. host->dma = NULL;
  616. }
  617. }
  618. if (ios->bus_width == MMC_BUS_WIDTH_4)
  619. host->cmdat |= CMD_DAT_CONT_BUS_WIDTH_4;
  620. else
  621. host->cmdat &= ~CMD_DAT_CONT_BUS_WIDTH_4;
  622. if (host->power_mode != ios->power_mode) {
  623. mxcmci_set_power(host, ios->power_mode, ios->vdd);
  624. host->power_mode = ios->power_mode;
  625. if (ios->power_mode == MMC_POWER_ON)
  626. host->cmdat |= CMD_DAT_CONT_INIT;
  627. }
  628. if (ios->clock) {
  629. mxcmci_set_clk_rate(host, ios->clock);
  630. writew(STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK);
  631. } else {
  632. writew(STR_STP_CLK_STOP_CLK, host->base + MMC_REG_STR_STP_CLK);
  633. }
  634. host->clock = ios->clock;
  635. }
  636. static irqreturn_t mxcmci_detect_irq(int irq, void *data)
  637. {
  638. struct mmc_host *mmc = data;
  639. dev_dbg(mmc_dev(mmc), "%s\n", __func__);
  640. mmc_detect_change(mmc, msecs_to_jiffies(250));
  641. return IRQ_HANDLED;
  642. }
  643. static int mxcmci_get_ro(struct mmc_host *mmc)
  644. {
  645. struct mxcmci_host *host = mmc_priv(mmc);
  646. if (host->pdata && host->pdata->get_ro)
  647. return !!host->pdata->get_ro(mmc_dev(mmc));
  648. /*
  649. * Board doesn't support read only detection; let the mmc core
  650. * decide what to do.
  651. */
  652. return -ENOSYS;
  653. }
  654. static void mxcmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
  655. {
  656. struct mxcmci_host *host = mmc_priv(mmc);
  657. unsigned long flags;
  658. u32 int_cntr;
  659. spin_lock_irqsave(&host->lock, flags);
  660. host->use_sdio = enable;
  661. int_cntr = readl(host->base + MMC_REG_INT_CNTR);
  662. if (enable)
  663. int_cntr |= INT_SDIO_IRQ_EN;
  664. else
  665. int_cntr &= ~INT_SDIO_IRQ_EN;
  666. writel(int_cntr, host->base + MMC_REG_INT_CNTR);
  667. spin_unlock_irqrestore(&host->lock, flags);
  668. }
  669. static void mxcmci_init_card(struct mmc_host *host, struct mmc_card *card)
  670. {
  671. /*
  672. * MX3 SoCs have a silicon bug which corrupts CRC calculation of
  673. * multi-block transfers when connected SDIO peripheral doesn't
  674. * drive the BUSY line as required by the specs.
  675. * One way to prevent this is to only allow 1-bit transfers.
  676. */
  677. if (cpu_is_mx3() && card->type == MMC_TYPE_SDIO)
  678. host->caps &= ~MMC_CAP_4_BIT_DATA;
  679. else
  680. host->caps |= MMC_CAP_4_BIT_DATA;
  681. }
  682. static bool filter(struct dma_chan *chan, void *param)
  683. {
  684. struct mxcmci_host *host = param;
  685. if (!imx_dma_is_general_purpose(chan))
  686. return false;
  687. chan->private = &host->dma_data;
  688. return true;
  689. }
  690. static const struct mmc_host_ops mxcmci_ops = {
  691. .request = mxcmci_request,
  692. .set_ios = mxcmci_set_ios,
  693. .get_ro = mxcmci_get_ro,
  694. .enable_sdio_irq = mxcmci_enable_sdio_irq,
  695. .init_card = mxcmci_init_card,
  696. };
  697. static int mxcmci_probe(struct platform_device *pdev)
  698. {
  699. struct mmc_host *mmc;
  700. struct mxcmci_host *host = NULL;
  701. struct resource *iores, *r;
  702. int ret = 0, irq;
  703. dma_cap_mask_t mask;
  704. pr_info("i.MX SDHC driver\n");
  705. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  706. irq = platform_get_irq(pdev, 0);
  707. if (!iores || irq < 0)
  708. return -EINVAL;
  709. r = request_mem_region(iores->start, resource_size(iores), pdev->name);
  710. if (!r)
  711. return -EBUSY;
  712. mmc = mmc_alloc_host(sizeof(struct mxcmci_host), &pdev->dev);
  713. if (!mmc) {
  714. ret = -ENOMEM;
  715. goto out_release_mem;
  716. }
  717. mmc->ops = &mxcmci_ops;
  718. mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
  719. /* MMC core transfer sizes tunable parameters */
  720. mmc->max_segs = 64;
  721. mmc->max_blk_size = 2048;
  722. mmc->max_blk_count = 65535;
  723. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  724. mmc->max_seg_size = mmc->max_req_size;
  725. host = mmc_priv(mmc);
  726. host->base = ioremap(r->start, resource_size(r));
  727. if (!host->base) {
  728. ret = -ENOMEM;
  729. goto out_free;
  730. }
  731. host->mmc = mmc;
  732. host->pdata = pdev->dev.platform_data;
  733. spin_lock_init(&host->lock);
  734. mxcmci_init_ocr(host);
  735. if (host->pdata && host->pdata->dat3_card_detect)
  736. host->default_irq_mask =
  737. INT_CARD_INSERTION_EN | INT_CARD_REMOVAL_EN;
  738. else
  739. host->default_irq_mask = 0;
  740. host->res = r;
  741. host->irq = irq;
  742. host->clk = clk_get(&pdev->dev, NULL);
  743. if (IS_ERR(host->clk)) {
  744. ret = PTR_ERR(host->clk);
  745. goto out_iounmap;
  746. }
  747. clk_enable(host->clk);
  748. mxcmci_softreset(host);
  749. host->rev_no = readw(host->base + MMC_REG_REV_NO);
  750. if (host->rev_no != 0x400) {
  751. ret = -ENODEV;
  752. dev_err(mmc_dev(host->mmc), "wrong rev.no. 0x%08x. aborting.\n",
  753. host->rev_no);
  754. goto out_clk_put;
  755. }
  756. mmc->f_min = clk_get_rate(host->clk) >> 16;
  757. mmc->f_max = clk_get_rate(host->clk) >> 1;
  758. /* recommended in data sheet */
  759. writew(0x2db4, host->base + MMC_REG_READ_TO);
  760. writel(host->default_irq_mask, host->base + MMC_REG_INT_CNTR);
  761. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  762. if (r) {
  763. host->dmareq = r->start;
  764. host->dma_data.peripheral_type = IMX_DMATYPE_SDHC;
  765. host->dma_data.priority = DMA_PRIO_LOW;
  766. host->dma_data.dma_request = host->dmareq;
  767. dma_cap_zero(mask);
  768. dma_cap_set(DMA_SLAVE, mask);
  769. host->dma = dma_request_channel(mask, filter, host);
  770. if (host->dma)
  771. mmc->max_seg_size = dma_get_max_seg_size(
  772. host->dma->device->dev);
  773. }
  774. if (!host->dma)
  775. dev_info(mmc_dev(host->mmc), "dma not available. Using PIO\n");
  776. INIT_WORK(&host->datawork, mxcmci_datawork);
  777. ret = request_irq(host->irq, mxcmci_irq, 0, DRIVER_NAME, host);
  778. if (ret)
  779. goto out_free_dma;
  780. platform_set_drvdata(pdev, mmc);
  781. if (host->pdata && host->pdata->init) {
  782. ret = host->pdata->init(&pdev->dev, mxcmci_detect_irq,
  783. host->mmc);
  784. if (ret)
  785. goto out_free_irq;
  786. }
  787. mmc_add_host(mmc);
  788. return 0;
  789. out_free_irq:
  790. free_irq(host->irq, host);
  791. out_free_dma:
  792. if (host->dma)
  793. dma_release_channel(host->dma);
  794. out_clk_put:
  795. clk_disable(host->clk);
  796. clk_put(host->clk);
  797. out_iounmap:
  798. iounmap(host->base);
  799. out_free:
  800. mmc_free_host(mmc);
  801. out_release_mem:
  802. release_mem_region(iores->start, resource_size(iores));
  803. return ret;
  804. }
  805. static int mxcmci_remove(struct platform_device *pdev)
  806. {
  807. struct mmc_host *mmc = platform_get_drvdata(pdev);
  808. struct mxcmci_host *host = mmc_priv(mmc);
  809. platform_set_drvdata(pdev, NULL);
  810. mmc_remove_host(mmc);
  811. if (host->vcc)
  812. regulator_put(host->vcc);
  813. if (host->pdata && host->pdata->exit)
  814. host->pdata->exit(&pdev->dev, mmc);
  815. free_irq(host->irq, host);
  816. iounmap(host->base);
  817. if (host->dma)
  818. dma_release_channel(host->dma);
  819. clk_disable(host->clk);
  820. clk_put(host->clk);
  821. release_mem_region(host->res->start, resource_size(host->res));
  822. mmc_free_host(mmc);
  823. return 0;
  824. }
  825. #ifdef CONFIG_PM
  826. static int mxcmci_suspend(struct device *dev)
  827. {
  828. struct mmc_host *mmc = dev_get_drvdata(dev);
  829. struct mxcmci_host *host = mmc_priv(mmc);
  830. int ret = 0;
  831. if (mmc)
  832. ret = mmc_suspend_host(mmc);
  833. clk_disable(host->clk);
  834. return ret;
  835. }
  836. static int mxcmci_resume(struct device *dev)
  837. {
  838. struct mmc_host *mmc = dev_get_drvdata(dev);
  839. struct mxcmci_host *host = mmc_priv(mmc);
  840. int ret = 0;
  841. clk_enable(host->clk);
  842. if (mmc)
  843. ret = mmc_resume_host(mmc);
  844. return ret;
  845. }
  846. static const struct dev_pm_ops mxcmci_pm_ops = {
  847. .suspend = mxcmci_suspend,
  848. .resume = mxcmci_resume,
  849. };
  850. #endif
  851. static struct platform_driver mxcmci_driver = {
  852. .probe = mxcmci_probe,
  853. .remove = mxcmci_remove,
  854. .driver = {
  855. .name = DRIVER_NAME,
  856. .owner = THIS_MODULE,
  857. #ifdef CONFIG_PM
  858. .pm = &mxcmci_pm_ops,
  859. #endif
  860. }
  861. };
  862. module_platform_driver(mxcmci_driver);
  863. MODULE_DESCRIPTION("i.MX Multimedia Card Interface Driver");
  864. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  865. MODULE_LICENSE("GPL");
  866. MODULE_ALIAS("platform:imx-mmc");