mxcmmc.c 28 KB

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