mxcmmc.c 29 KB

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