mxcmmc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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 seperate 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. unsigned int power_mode;
  113. struct imxmmc_platform_data *pdata;
  114. struct mmc_request *req;
  115. struct mmc_command *cmd;
  116. struct mmc_data *data;
  117. unsigned int dma_nents;
  118. unsigned int datasize;
  119. unsigned int dma_dir;
  120. u16 rev_no;
  121. unsigned int cmdat;
  122. struct clk *clk;
  123. int clock;
  124. struct work_struct datawork;
  125. };
  126. static inline int mxcmci_use_dma(struct mxcmci_host *host)
  127. {
  128. return host->do_dma;
  129. }
  130. static void mxcmci_softreset(struct mxcmci_host *host)
  131. {
  132. int i;
  133. /* reset sequence */
  134. writew(STR_STP_CLK_RESET, host->base + MMC_REG_STR_STP_CLK);
  135. writew(STR_STP_CLK_RESET | STR_STP_CLK_START_CLK,
  136. host->base + MMC_REG_STR_STP_CLK);
  137. for (i = 0; i < 8; i++)
  138. writew(STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK);
  139. writew(0xff, host->base + MMC_REG_RES_TO);
  140. }
  141. static void mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data)
  142. {
  143. unsigned int nob = data->blocks;
  144. unsigned int blksz = data->blksz;
  145. unsigned int datasize = nob * blksz;
  146. #ifdef HAS_DMA
  147. struct scatterlist *sg;
  148. int i;
  149. #endif
  150. if (data->flags & MMC_DATA_STREAM)
  151. nob = 0xffff;
  152. host->data = data;
  153. data->bytes_xfered = 0;
  154. writew(nob, host->base + MMC_REG_NOB);
  155. writew(blksz, host->base + MMC_REG_BLK_LEN);
  156. host->datasize = datasize;
  157. #ifdef HAS_DMA
  158. for_each_sg(data->sg, sg, data->sg_len, i) {
  159. if (sg->offset & 3 || sg->length & 3) {
  160. host->do_dma = 0;
  161. return;
  162. }
  163. }
  164. if (data->flags & MMC_DATA_READ) {
  165. host->dma_dir = DMA_FROM_DEVICE;
  166. host->dma_nents = dma_map_sg(mmc_dev(host->mmc), data->sg,
  167. data->sg_len, host->dma_dir);
  168. imx_dma_setup_sg(host->dma, data->sg, host->dma_nents, datasize,
  169. host->res->start + MMC_REG_BUFFER_ACCESS,
  170. DMA_MODE_READ);
  171. } else {
  172. host->dma_dir = DMA_TO_DEVICE;
  173. host->dma_nents = dma_map_sg(mmc_dev(host->mmc), data->sg,
  174. data->sg_len, host->dma_dir);
  175. imx_dma_setup_sg(host->dma, data->sg, host->dma_nents, datasize,
  176. host->res->start + MMC_REG_BUFFER_ACCESS,
  177. DMA_MODE_WRITE);
  178. }
  179. wmb();
  180. imx_dma_enable(host->dma);
  181. #endif /* HAS_DMA */
  182. }
  183. static int mxcmci_start_cmd(struct mxcmci_host *host, struct mmc_command *cmd,
  184. unsigned int cmdat)
  185. {
  186. WARN_ON(host->cmd != NULL);
  187. host->cmd = cmd;
  188. switch (mmc_resp_type(cmd)) {
  189. case MMC_RSP_R1: /* short CRC, OPCODE */
  190. case MMC_RSP_R1B:/* short CRC, OPCODE, BUSY */
  191. cmdat |= CMD_DAT_CONT_RESPONSE_48BIT_CRC;
  192. break;
  193. case MMC_RSP_R2: /* long 136 bit + CRC */
  194. cmdat |= CMD_DAT_CONT_RESPONSE_136BIT;
  195. break;
  196. case MMC_RSP_R3: /* short */
  197. cmdat |= CMD_DAT_CONT_RESPONSE_48BIT;
  198. break;
  199. case MMC_RSP_NONE:
  200. break;
  201. default:
  202. dev_err(mmc_dev(host->mmc), "unhandled response type 0x%x\n",
  203. mmc_resp_type(cmd));
  204. cmd->error = -EINVAL;
  205. return -EINVAL;
  206. }
  207. if (mxcmci_use_dma(host))
  208. writel(INT_READ_OP_EN | INT_WRITE_OP_DONE_EN |
  209. INT_END_CMD_RES_EN,
  210. host->base + MMC_REG_INT_CNTR);
  211. else
  212. writel(INT_END_CMD_RES_EN, host->base + MMC_REG_INT_CNTR);
  213. writew(cmd->opcode, host->base + MMC_REG_CMD);
  214. writel(cmd->arg, host->base + MMC_REG_ARG);
  215. writew(cmdat, host->base + MMC_REG_CMD_DAT_CONT);
  216. return 0;
  217. }
  218. static void mxcmci_finish_request(struct mxcmci_host *host,
  219. struct mmc_request *req)
  220. {
  221. writel(0, host->base + MMC_REG_INT_CNTR);
  222. host->req = NULL;
  223. host->cmd = NULL;
  224. host->data = NULL;
  225. mmc_request_done(host->mmc, req);
  226. }
  227. static int mxcmci_finish_data(struct mxcmci_host *host, unsigned int stat)
  228. {
  229. struct mmc_data *data = host->data;
  230. int data_error;
  231. #ifdef HAS_DMA
  232. if (mxcmci_use_dma(host)) {
  233. imx_dma_disable(host->dma);
  234. dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_nents,
  235. host->dma_dir);
  236. }
  237. #endif
  238. if (stat & STATUS_ERR_MASK) {
  239. dev_dbg(mmc_dev(host->mmc), "request failed. status: 0x%08x\n",
  240. stat);
  241. if (stat & STATUS_CRC_READ_ERR) {
  242. data->error = -EILSEQ;
  243. } else if (stat & STATUS_CRC_WRITE_ERR) {
  244. u32 err_code = (stat >> 9) & 0x3;
  245. if (err_code == 2) /* No CRC response */
  246. data->error = -ETIMEDOUT;
  247. else
  248. data->error = -EILSEQ;
  249. } else if (stat & STATUS_TIME_OUT_READ) {
  250. data->error = -ETIMEDOUT;
  251. } else {
  252. data->error = -EIO;
  253. }
  254. } else {
  255. data->bytes_xfered = host->datasize;
  256. }
  257. data_error = data->error;
  258. host->data = NULL;
  259. return data_error;
  260. }
  261. static void mxcmci_read_response(struct mxcmci_host *host, unsigned int stat)
  262. {
  263. struct mmc_command *cmd = host->cmd;
  264. int i;
  265. u32 a, b, c;
  266. if (!cmd)
  267. return;
  268. if (stat & STATUS_TIME_OUT_RESP) {
  269. dev_dbg(mmc_dev(host->mmc), "CMD TIMEOUT\n");
  270. cmd->error = -ETIMEDOUT;
  271. } else if (stat & STATUS_RESP_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
  272. dev_dbg(mmc_dev(host->mmc), "cmd crc error\n");
  273. cmd->error = -EILSEQ;
  274. }
  275. if (cmd->flags & MMC_RSP_PRESENT) {
  276. if (cmd->flags & MMC_RSP_136) {
  277. for (i = 0; i < 4; i++) {
  278. a = readw(host->base + MMC_REG_RES_FIFO);
  279. b = readw(host->base + MMC_REG_RES_FIFO);
  280. cmd->resp[i] = a << 16 | b;
  281. }
  282. } else {
  283. a = readw(host->base + MMC_REG_RES_FIFO);
  284. b = readw(host->base + MMC_REG_RES_FIFO);
  285. c = readw(host->base + MMC_REG_RES_FIFO);
  286. cmd->resp[0] = a << 24 | b << 8 | c >> 8;
  287. }
  288. }
  289. }
  290. static int mxcmci_poll_status(struct mxcmci_host *host, u32 mask)
  291. {
  292. u32 stat;
  293. unsigned long timeout = jiffies + HZ;
  294. do {
  295. stat = readl(host->base + MMC_REG_STATUS);
  296. if (stat & STATUS_ERR_MASK)
  297. return stat;
  298. if (time_after(jiffies, timeout))
  299. return STATUS_TIME_OUT_READ;
  300. if (stat & mask)
  301. return 0;
  302. cpu_relax();
  303. } while (1);
  304. }
  305. static int mxcmci_pull(struct mxcmci_host *host, void *_buf, int bytes)
  306. {
  307. unsigned int stat;
  308. u32 *buf = _buf;
  309. while (bytes > 3) {
  310. stat = mxcmci_poll_status(host,
  311. STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
  312. if (stat)
  313. return stat;
  314. *buf++ = readl(host->base + MMC_REG_BUFFER_ACCESS);
  315. bytes -= 4;
  316. }
  317. if (bytes) {
  318. u8 *b = (u8 *)buf;
  319. u32 tmp;
  320. stat = mxcmci_poll_status(host,
  321. STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
  322. if (stat)
  323. return stat;
  324. tmp = readl(host->base + MMC_REG_BUFFER_ACCESS);
  325. memcpy(b, &tmp, bytes);
  326. }
  327. return 0;
  328. }
  329. static int mxcmci_push(struct mxcmci_host *host, void *_buf, int bytes)
  330. {
  331. unsigned int stat;
  332. u32 *buf = _buf;
  333. while (bytes > 3) {
  334. stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
  335. if (stat)
  336. return stat;
  337. writel(*buf++, host->base + MMC_REG_BUFFER_ACCESS);
  338. bytes -= 4;
  339. }
  340. if (bytes) {
  341. u8 *b = (u8 *)buf;
  342. u32 tmp;
  343. stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
  344. if (stat)
  345. return stat;
  346. memcpy(&tmp, b, bytes);
  347. writel(tmp, host->base + MMC_REG_BUFFER_ACCESS);
  348. }
  349. stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
  350. if (stat)
  351. return stat;
  352. return 0;
  353. }
  354. static int mxcmci_transfer_data(struct mxcmci_host *host)
  355. {
  356. struct mmc_data *data = host->req->data;
  357. struct scatterlist *sg;
  358. int stat, i;
  359. host->datasize = 0;
  360. host->data = data;
  361. host->datasize = 0;
  362. if (data->flags & MMC_DATA_READ) {
  363. for_each_sg(data->sg, sg, data->sg_len, i) {
  364. stat = mxcmci_pull(host, sg_virt(sg), sg->length);
  365. if (stat)
  366. return stat;
  367. host->datasize += sg->length;
  368. }
  369. } else {
  370. for_each_sg(data->sg, sg, data->sg_len, i) {
  371. stat = mxcmci_push(host, sg_virt(sg), sg->length);
  372. if (stat)
  373. return stat;
  374. host->datasize += sg->length;
  375. }
  376. stat = mxcmci_poll_status(host, STATUS_WRITE_OP_DONE);
  377. if (stat)
  378. return stat;
  379. }
  380. return 0;
  381. }
  382. static void mxcmci_datawork(struct work_struct *work)
  383. {
  384. struct mxcmci_host *host = container_of(work, struct mxcmci_host,
  385. datawork);
  386. int datastat = mxcmci_transfer_data(host);
  387. mxcmci_finish_data(host, datastat);
  388. if (host->req->stop) {
  389. if (mxcmci_start_cmd(host, host->req->stop, 0)) {
  390. mxcmci_finish_request(host, host->req);
  391. return;
  392. }
  393. } else {
  394. mxcmci_finish_request(host, host->req);
  395. }
  396. }
  397. #ifdef HAS_DMA
  398. static void mxcmci_data_done(struct mxcmci_host *host, unsigned int stat)
  399. {
  400. struct mmc_data *data = host->data;
  401. int data_error;
  402. if (!data)
  403. return;
  404. data_error = mxcmci_finish_data(host, stat);
  405. mxcmci_read_response(host, stat);
  406. host->cmd = NULL;
  407. if (host->req->stop) {
  408. if (mxcmci_start_cmd(host, host->req->stop, 0)) {
  409. mxcmci_finish_request(host, host->req);
  410. return;
  411. }
  412. } else {
  413. mxcmci_finish_request(host, host->req);
  414. }
  415. }
  416. #endif /* HAS_DMA */
  417. static void mxcmci_cmd_done(struct mxcmci_host *host, unsigned int stat)
  418. {
  419. mxcmci_read_response(host, stat);
  420. host->cmd = NULL;
  421. if (!host->data && host->req) {
  422. mxcmci_finish_request(host, host->req);
  423. return;
  424. }
  425. /* For the DMA case the DMA engine handles the data transfer
  426. * automatically. For non DMA we have to to it ourselves.
  427. * Don't do it in interrupt context though.
  428. */
  429. if (!mxcmci_use_dma(host) && host->data)
  430. schedule_work(&host->datawork);
  431. }
  432. static irqreturn_t mxcmci_irq(int irq, void *devid)
  433. {
  434. struct mxcmci_host *host = devid;
  435. u32 stat;
  436. stat = readl(host->base + MMC_REG_STATUS);
  437. writel(stat, host->base + MMC_REG_STATUS);
  438. dev_dbg(mmc_dev(host->mmc), "%s: 0x%08x\n", __func__, stat);
  439. if (stat & STATUS_END_CMD_RESP)
  440. mxcmci_cmd_done(host, stat);
  441. #ifdef HAS_DMA
  442. if (mxcmci_use_dma(host) &&
  443. (stat & (STATUS_DATA_TRANS_DONE | STATUS_WRITE_OP_DONE)))
  444. mxcmci_data_done(host, stat);
  445. #endif
  446. return IRQ_HANDLED;
  447. }
  448. static void mxcmci_request(struct mmc_host *mmc, struct mmc_request *req)
  449. {
  450. struct mxcmci_host *host = mmc_priv(mmc);
  451. unsigned int cmdat = host->cmdat;
  452. WARN_ON(host->req != NULL);
  453. host->req = req;
  454. host->cmdat &= ~CMD_DAT_CONT_INIT;
  455. #ifdef HAS_DMA
  456. host->do_dma = 1;
  457. #endif
  458. if (req->data) {
  459. mxcmci_setup_data(host, req->data);
  460. cmdat |= CMD_DAT_CONT_DATA_ENABLE;
  461. if (req->data->flags & MMC_DATA_WRITE)
  462. cmdat |= CMD_DAT_CONT_WRITE;
  463. }
  464. if (mxcmci_start_cmd(host, req->cmd, cmdat))
  465. mxcmci_finish_request(host, req);
  466. }
  467. static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios)
  468. {
  469. unsigned int divider;
  470. int prescaler = 0;
  471. unsigned int clk_in = clk_get_rate(host->clk);
  472. while (prescaler <= 0x800) {
  473. for (divider = 1; divider <= 0xF; divider++) {
  474. int x;
  475. x = (clk_in / (divider + 1));
  476. if (prescaler)
  477. x /= (prescaler * 2);
  478. if (x <= clk_ios)
  479. break;
  480. }
  481. if (divider < 0x10)
  482. break;
  483. if (prescaler == 0)
  484. prescaler = 1;
  485. else
  486. prescaler <<= 1;
  487. }
  488. writew((prescaler << 4) | divider, host->base + MMC_REG_CLK_RATE);
  489. dev_dbg(mmc_dev(host->mmc), "scaler: %d divider: %d in: %d out: %d\n",
  490. prescaler, divider, clk_in, clk_ios);
  491. }
  492. static void mxcmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  493. {
  494. struct mxcmci_host *host = mmc_priv(mmc);
  495. #ifdef HAS_DMA
  496. unsigned int blen;
  497. /*
  498. * use burstlen of 64 in 4 bit mode (--> reg value 0)
  499. * use burstlen of 16 in 1 bit mode (--> reg value 16)
  500. */
  501. if (ios->bus_width == MMC_BUS_WIDTH_4)
  502. blen = 0;
  503. else
  504. blen = 16;
  505. imx_dma_config_burstlen(host->dma, blen);
  506. #endif
  507. if (ios->bus_width == MMC_BUS_WIDTH_4)
  508. host->cmdat |= CMD_DAT_CONT_BUS_WIDTH_4;
  509. else
  510. host->cmdat &= ~CMD_DAT_CONT_BUS_WIDTH_4;
  511. if (host->power_mode != ios->power_mode) {
  512. if (host->pdata && host->pdata->setpower)
  513. host->pdata->setpower(mmc_dev(mmc), ios->vdd);
  514. host->power_mode = ios->power_mode;
  515. if (ios->power_mode == MMC_POWER_ON)
  516. host->cmdat |= CMD_DAT_CONT_INIT;
  517. }
  518. if (ios->clock) {
  519. mxcmci_set_clk_rate(host, ios->clock);
  520. writew(STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK);
  521. } else {
  522. writew(STR_STP_CLK_STOP_CLK, host->base + MMC_REG_STR_STP_CLK);
  523. }
  524. host->clock = ios->clock;
  525. }
  526. static irqreturn_t mxcmci_detect_irq(int irq, void *data)
  527. {
  528. struct mmc_host *mmc = data;
  529. dev_dbg(mmc_dev(mmc), "%s\n", __func__);
  530. mmc_detect_change(mmc, msecs_to_jiffies(250));
  531. return IRQ_HANDLED;
  532. }
  533. static int mxcmci_get_ro(struct mmc_host *mmc)
  534. {
  535. struct mxcmci_host *host = mmc_priv(mmc);
  536. if (host->pdata && host->pdata->get_ro)
  537. return !!host->pdata->get_ro(mmc_dev(mmc));
  538. /*
  539. * Board doesn't support read only detection; let the mmc core
  540. * decide what to do.
  541. */
  542. return -ENOSYS;
  543. }
  544. static const struct mmc_host_ops mxcmci_ops = {
  545. .request = mxcmci_request,
  546. .set_ios = mxcmci_set_ios,
  547. .get_ro = mxcmci_get_ro,
  548. };
  549. static int mxcmci_probe(struct platform_device *pdev)
  550. {
  551. struct mmc_host *mmc;
  552. struct mxcmci_host *host = NULL;
  553. struct resource *r;
  554. int ret = 0, irq;
  555. printk(KERN_INFO "i.MX SDHC driver\n");
  556. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  557. irq = platform_get_irq(pdev, 0);
  558. if (!r || irq < 0)
  559. return -EINVAL;
  560. r = request_mem_region(r->start, resource_size(r), pdev->name);
  561. if (!r)
  562. return -EBUSY;
  563. mmc = mmc_alloc_host(sizeof(struct mxcmci_host), &pdev->dev);
  564. if (!mmc) {
  565. ret = -ENOMEM;
  566. goto out_release_mem;
  567. }
  568. mmc->ops = &mxcmci_ops;
  569. mmc->caps = MMC_CAP_4_BIT_DATA;
  570. /* MMC core transfer sizes tunable parameters */
  571. mmc->max_hw_segs = 64;
  572. mmc->max_phys_segs = 64;
  573. mmc->max_blk_size = 2048;
  574. mmc->max_blk_count = 65535;
  575. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  576. mmc->max_seg_size = mmc->max_seg_size;
  577. host = mmc_priv(mmc);
  578. host->base = ioremap(r->start, resource_size(r));
  579. if (!host->base) {
  580. ret = -ENOMEM;
  581. goto out_free;
  582. }
  583. host->mmc = mmc;
  584. host->pdata = pdev->dev.platform_data;
  585. if (host->pdata && host->pdata->ocr_avail)
  586. mmc->ocr_avail = host->pdata->ocr_avail;
  587. else
  588. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  589. host->res = r;
  590. host->irq = irq;
  591. host->clk = clk_get(&pdev->dev, NULL);
  592. if (IS_ERR(host->clk)) {
  593. ret = PTR_ERR(host->clk);
  594. goto out_iounmap;
  595. }
  596. clk_enable(host->clk);
  597. mxcmci_softreset(host);
  598. host->rev_no = readw(host->base + MMC_REG_REV_NO);
  599. if (host->rev_no != 0x400) {
  600. ret = -ENODEV;
  601. dev_err(mmc_dev(host->mmc), "wrong rev.no. 0x%08x. aborting.\n",
  602. host->rev_no);
  603. goto out_clk_put;
  604. }
  605. mmc->f_min = clk_get_rate(host->clk) >> 7;
  606. mmc->f_max = clk_get_rate(host->clk) >> 1;
  607. /* recommended in data sheet */
  608. writew(0x2db4, host->base + MMC_REG_READ_TO);
  609. writel(0, host->base + MMC_REG_INT_CNTR);
  610. #ifdef HAS_DMA
  611. host->dma = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_LOW);
  612. if (host->dma < 0) {
  613. dev_err(mmc_dev(host->mmc), "imx_dma_request_by_prio failed\n");
  614. ret = -EBUSY;
  615. goto out_clk_put;
  616. }
  617. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  618. if (!r) {
  619. ret = -EINVAL;
  620. goto out_free_dma;
  621. }
  622. ret = imx_dma_config_channel(host->dma,
  623. IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_FIFO,
  624. IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR,
  625. r->start, 0);
  626. if (ret) {
  627. dev_err(mmc_dev(host->mmc), "failed to config DMA channel\n");
  628. goto out_free_dma;
  629. }
  630. #endif
  631. INIT_WORK(&host->datawork, mxcmci_datawork);
  632. ret = request_irq(host->irq, mxcmci_irq, 0, DRIVER_NAME, host);
  633. if (ret)
  634. goto out_free_dma;
  635. platform_set_drvdata(pdev, mmc);
  636. if (host->pdata && host->pdata->init) {
  637. ret = host->pdata->init(&pdev->dev, mxcmci_detect_irq,
  638. host->mmc);
  639. if (ret)
  640. goto out_free_irq;
  641. }
  642. mmc_add_host(mmc);
  643. return 0;
  644. out_free_irq:
  645. free_irq(host->irq, host);
  646. out_free_dma:
  647. #ifdef HAS_DMA
  648. imx_dma_free(host->dma);
  649. #endif
  650. out_clk_put:
  651. clk_disable(host->clk);
  652. clk_put(host->clk);
  653. out_iounmap:
  654. iounmap(host->base);
  655. out_free:
  656. mmc_free_host(mmc);
  657. out_release_mem:
  658. release_mem_region(host->res->start, resource_size(host->res));
  659. return ret;
  660. }
  661. static int mxcmci_remove(struct platform_device *pdev)
  662. {
  663. struct mmc_host *mmc = platform_get_drvdata(pdev);
  664. struct mxcmci_host *host = mmc_priv(mmc);
  665. platform_set_drvdata(pdev, NULL);
  666. mmc_remove_host(mmc);
  667. if (host->pdata && host->pdata->exit)
  668. host->pdata->exit(&pdev->dev, mmc);
  669. free_irq(host->irq, host);
  670. iounmap(host->base);
  671. #ifdef HAS_DMA
  672. imx_dma_free(host->dma);
  673. #endif
  674. clk_disable(host->clk);
  675. clk_put(host->clk);
  676. release_mem_region(host->res->start, resource_size(host->res));
  677. release_resource(host->res);
  678. mmc_free_host(mmc);
  679. return 0;
  680. }
  681. #ifdef CONFIG_PM
  682. static int mxcmci_suspend(struct platform_device *dev, pm_message_t state)
  683. {
  684. struct mmc_host *mmc = platform_get_drvdata(dev);
  685. int ret = 0;
  686. if (mmc)
  687. ret = mmc_suspend_host(mmc, state);
  688. return ret;
  689. }
  690. static int mxcmci_resume(struct platform_device *dev)
  691. {
  692. struct mmc_host *mmc = platform_get_drvdata(dev);
  693. struct mxcmci_host *host;
  694. int ret = 0;
  695. if (mmc) {
  696. host = mmc_priv(mmc);
  697. ret = mmc_resume_host(mmc);
  698. }
  699. return ret;
  700. }
  701. #else
  702. #define mxcmci_suspend NULL
  703. #define mxcmci_resume NULL
  704. #endif /* CONFIG_PM */
  705. static struct platform_driver mxcmci_driver = {
  706. .probe = mxcmci_probe,
  707. .remove = mxcmci_remove,
  708. .suspend = mxcmci_suspend,
  709. .resume = mxcmci_resume,
  710. .driver = {
  711. .name = DRIVER_NAME,
  712. .owner = THIS_MODULE,
  713. }
  714. };
  715. static int __init mxcmci_init(void)
  716. {
  717. return platform_driver_register(&mxcmci_driver);
  718. }
  719. static void __exit mxcmci_exit(void)
  720. {
  721. platform_driver_unregister(&mxcmci_driver);
  722. }
  723. module_init(mxcmci_init);
  724. module_exit(mxcmci_exit);
  725. MODULE_DESCRIPTION("i.MX Multimedia Card Interface Driver");
  726. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  727. MODULE_LICENSE("GPL");
  728. MODULE_ALIAS("platform:imx-mmc");