mxcmmc.c 23 KB

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