mxcmmc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * This is a driver for the SDHC controller found in Freescale MX2/MX3
  3. * SoCs. It is basically the same hardware as found on MX1 (imxmmc.c).
  4. * Unlike the hardware found on MX1, this hardware just works and does
  5. * not need all the quirks found in imxmmc.c, hence the seperate driver.
  6. *
  7. * Copyright (C) 2009 Ilya Yanok, <yanok@emcraft.com>
  8. * Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
  9. * Copyright (C) 2006 Pavel Pisa, PiKRON <ppisa@pikron.com>
  10. *
  11. * derived from pxamci.c by Russell King
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. *
  17. */
  18. #include <config.h>
  19. #include <common.h>
  20. #include <command.h>
  21. #include <mmc.h>
  22. #include <part.h>
  23. #include <malloc.h>
  24. #include <mmc.h>
  25. #include <asm/errno.h>
  26. #include <asm/io.h>
  27. #include <asm/arch/clock.h>
  28. #define DRIVER_NAME "mxc-mmc"
  29. struct mxcmci_regs {
  30. u32 str_stp_clk;
  31. u32 status;
  32. u32 clk_rate;
  33. u32 cmd_dat_cont;
  34. u32 res_to;
  35. u32 read_to;
  36. u32 blk_len;
  37. u32 nob;
  38. u32 rev_no;
  39. u32 int_cntr;
  40. u32 cmd;
  41. u32 arg;
  42. u32 pad;
  43. u32 res_fifo;
  44. u32 buffer_access;
  45. };
  46. #define STR_STP_CLK_RESET (1 << 3)
  47. #define STR_STP_CLK_START_CLK (1 << 1)
  48. #define STR_STP_CLK_STOP_CLK (1 << 0)
  49. #define STATUS_CARD_INSERTION (1 << 31)
  50. #define STATUS_CARD_REMOVAL (1 << 30)
  51. #define STATUS_YBUF_EMPTY (1 << 29)
  52. #define STATUS_XBUF_EMPTY (1 << 28)
  53. #define STATUS_YBUF_FULL (1 << 27)
  54. #define STATUS_XBUF_FULL (1 << 26)
  55. #define STATUS_BUF_UND_RUN (1 << 25)
  56. #define STATUS_BUF_OVFL (1 << 24)
  57. #define STATUS_SDIO_INT_ACTIVE (1 << 14)
  58. #define STATUS_END_CMD_RESP (1 << 13)
  59. #define STATUS_WRITE_OP_DONE (1 << 12)
  60. #define STATUS_DATA_TRANS_DONE (1 << 11)
  61. #define STATUS_READ_OP_DONE (1 << 11)
  62. #define STATUS_WR_CRC_ERROR_CODE_MASK (3 << 10)
  63. #define STATUS_CARD_BUS_CLK_RUN (1 << 8)
  64. #define STATUS_BUF_READ_RDY (1 << 7)
  65. #define STATUS_BUF_WRITE_RDY (1 << 6)
  66. #define STATUS_RESP_CRC_ERR (1 << 5)
  67. #define STATUS_CRC_READ_ERR (1 << 3)
  68. #define STATUS_CRC_WRITE_ERR (1 << 2)
  69. #define STATUS_TIME_OUT_RESP (1 << 1)
  70. #define STATUS_TIME_OUT_READ (1 << 0)
  71. #define STATUS_ERR_MASK 0x2f
  72. #define CMD_DAT_CONT_CMD_RESP_LONG_OFF (1 << 12)
  73. #define CMD_DAT_CONT_STOP_READWAIT (1 << 11)
  74. #define CMD_DAT_CONT_START_READWAIT (1 << 10)
  75. #define CMD_DAT_CONT_BUS_WIDTH_4 (2 << 8)
  76. #define CMD_DAT_CONT_INIT (1 << 7)
  77. #define CMD_DAT_CONT_WRITE (1 << 4)
  78. #define CMD_DAT_CONT_DATA_ENABLE (1 << 3)
  79. #define CMD_DAT_CONT_RESPONSE_48BIT_CRC (1 << 0)
  80. #define CMD_DAT_CONT_RESPONSE_136BIT (2 << 0)
  81. #define CMD_DAT_CONT_RESPONSE_48BIT (3 << 0)
  82. #define INT_SDIO_INT_WKP_EN (1 << 18)
  83. #define INT_CARD_INSERTION_WKP_EN (1 << 17)
  84. #define INT_CARD_REMOVAL_WKP_EN (1 << 16)
  85. #define INT_CARD_INSERTION_EN (1 << 15)
  86. #define INT_CARD_REMOVAL_EN (1 << 14)
  87. #define INT_SDIO_IRQ_EN (1 << 13)
  88. #define INT_DAT0_EN (1 << 12)
  89. #define INT_BUF_READ_EN (1 << 4)
  90. #define INT_BUF_WRITE_EN (1 << 3)
  91. #define INT_END_CMD_RES_EN (1 << 2)
  92. #define INT_WRITE_OP_DONE_EN (1 << 1)
  93. #define INT_READ_OP_EN (1 << 0)
  94. struct mxcmci_host {
  95. struct mmc *mmc;
  96. struct mxcmci_regs *base;
  97. int irq;
  98. int detect_irq;
  99. int dma;
  100. int do_dma;
  101. unsigned int power_mode;
  102. struct mmc_cmd *cmd;
  103. struct mmc_data *data;
  104. unsigned int dma_nents;
  105. unsigned int datasize;
  106. unsigned int dma_dir;
  107. u16 rev_no;
  108. unsigned int cmdat;
  109. int clock;
  110. };
  111. static struct mxcmci_host mxcmci_host;
  112. static struct mxcmci_host *host = &mxcmci_host;
  113. static inline int mxcmci_use_dma(struct mxcmci_host *host)
  114. {
  115. return host->do_dma;
  116. }
  117. static void mxcmci_softreset(struct mxcmci_host *host)
  118. {
  119. int i;
  120. /* reset sequence */
  121. writel(STR_STP_CLK_RESET, &host->base->str_stp_clk);
  122. writel(STR_STP_CLK_RESET | STR_STP_CLK_START_CLK,
  123. &host->base->str_stp_clk);
  124. for (i = 0; i < 8; i++)
  125. writel(STR_STP_CLK_START_CLK, &host->base->str_stp_clk);
  126. writel(0xff, &host->base->res_to);
  127. }
  128. static void mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data)
  129. {
  130. unsigned int nob = data->blocks;
  131. unsigned int blksz = data->blocksize;
  132. unsigned int datasize = nob * blksz;
  133. host->data = data;
  134. writel(nob, &host->base->nob);
  135. writel(blksz, &host->base->blk_len);
  136. host->datasize = datasize;
  137. }
  138. static int mxcmci_start_cmd(struct mxcmci_host *host, struct mmc_cmd *cmd,
  139. unsigned int cmdat)
  140. {
  141. if (host->cmd != NULL)
  142. printf("mxcmci: error!\n");
  143. host->cmd = cmd;
  144. switch (cmd->resp_type) {
  145. case MMC_RSP_R1: /* short CRC, OPCODE */
  146. case MMC_RSP_R1b:/* short CRC, OPCODE, BUSY */
  147. cmdat |= CMD_DAT_CONT_RESPONSE_48BIT_CRC;
  148. break;
  149. case MMC_RSP_R2: /* long 136 bit + CRC */
  150. cmdat |= CMD_DAT_CONT_RESPONSE_136BIT;
  151. break;
  152. case MMC_RSP_R3: /* short */
  153. cmdat |= CMD_DAT_CONT_RESPONSE_48BIT;
  154. break;
  155. case MMC_RSP_NONE:
  156. break;
  157. default:
  158. printf("mxcmci: unhandled response type 0x%x\n",
  159. cmd->resp_type);
  160. return -EINVAL;
  161. }
  162. writel(cmd->cmdidx, &host->base->cmd);
  163. writel(cmd->cmdarg, &host->base->arg);
  164. writel(cmdat, &host->base->cmd_dat_cont);
  165. return 0;
  166. }
  167. static void mxcmci_finish_request(struct mxcmci_host *host,
  168. struct mmc_cmd *cmd, struct mmc_data *data)
  169. {
  170. host->cmd = NULL;
  171. host->data = NULL;
  172. }
  173. static int mxcmci_finish_data(struct mxcmci_host *host, unsigned int stat)
  174. {
  175. int data_error = 0;
  176. if (stat & STATUS_ERR_MASK) {
  177. printf("request failed. status: 0x%08x\n",
  178. stat);
  179. if (stat & STATUS_CRC_READ_ERR) {
  180. data_error = -EILSEQ;
  181. } else if (stat & STATUS_CRC_WRITE_ERR) {
  182. u32 err_code = (stat >> 9) & 0x3;
  183. if (err_code == 2) /* No CRC response */
  184. data_error = TIMEOUT;
  185. else
  186. data_error = -EILSEQ;
  187. } else if (stat & STATUS_TIME_OUT_READ) {
  188. data_error = TIMEOUT;
  189. } else {
  190. data_error = -EIO;
  191. }
  192. }
  193. host->data = NULL;
  194. return data_error;
  195. }
  196. static int mxcmci_read_response(struct mxcmci_host *host, unsigned int stat)
  197. {
  198. struct mmc_cmd *cmd = host->cmd;
  199. int i;
  200. u32 a, b, c;
  201. u32 *resp = (u32 *)cmd->response;
  202. if (!cmd)
  203. return 0;
  204. if (stat & STATUS_TIME_OUT_RESP) {
  205. printf("CMD TIMEOUT\n");
  206. return TIMEOUT;
  207. } else if (stat & STATUS_RESP_CRC_ERR && cmd->resp_type & MMC_RSP_CRC) {
  208. printf("cmd crc error\n");
  209. return -EILSEQ;
  210. }
  211. if (cmd->resp_type & MMC_RSP_PRESENT) {
  212. if (cmd->resp_type & MMC_RSP_136) {
  213. for (i = 0; i < 4; i++) {
  214. a = readl(&host->base->res_fifo) & 0xFFFF;
  215. b = readl(&host->base->res_fifo) & 0xFFFF;
  216. resp[i] = a << 16 | b;
  217. }
  218. } else {
  219. a = readl(&host->base->res_fifo) & 0xFFFF;
  220. b = readl(&host->base->res_fifo) & 0xFFFF;
  221. c = readl(&host->base->res_fifo) & 0xFFFF;
  222. resp[0] = a << 24 | b << 8 | c >> 8;
  223. }
  224. }
  225. return 0;
  226. }
  227. static int mxcmci_poll_status(struct mxcmci_host *host, u32 mask)
  228. {
  229. u32 stat;
  230. unsigned long timeout = get_ticks() + CONFIG_SYS_HZ;
  231. do {
  232. stat = readl(&host->base->status);
  233. if (stat & STATUS_ERR_MASK)
  234. return stat;
  235. if (timeout < get_ticks())
  236. return STATUS_TIME_OUT_READ;
  237. if (stat & mask)
  238. return 0;
  239. } while (1);
  240. }
  241. static int mxcmci_pull(struct mxcmci_host *host, void *_buf, int bytes)
  242. {
  243. unsigned int stat;
  244. u32 *buf = _buf;
  245. while (bytes > 3) {
  246. stat = mxcmci_poll_status(host,
  247. STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
  248. if (stat)
  249. return stat;
  250. *buf++ = readl(&host->base->buffer_access);
  251. bytes -= 4;
  252. }
  253. if (bytes) {
  254. u8 *b = (u8 *)buf;
  255. u32 tmp;
  256. stat = mxcmci_poll_status(host,
  257. STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
  258. if (stat)
  259. return stat;
  260. tmp = readl(&host->base->buffer_access);
  261. memcpy(b, &tmp, bytes);
  262. }
  263. return 0;
  264. }
  265. static int mxcmci_push(struct mxcmci_host *host, const void *_buf, int bytes)
  266. {
  267. unsigned int stat;
  268. const u32 *buf = _buf;
  269. while (bytes > 3) {
  270. stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
  271. if (stat)
  272. return stat;
  273. writel(*buf++, &host->base->buffer_access);
  274. bytes -= 4;
  275. }
  276. if (bytes) {
  277. const u8 *b = (u8 *)buf;
  278. u32 tmp;
  279. stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
  280. if (stat)
  281. return stat;
  282. memcpy(&tmp, b, bytes);
  283. writel(tmp, &host->base->buffer_access);
  284. }
  285. stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
  286. if (stat)
  287. return stat;
  288. return 0;
  289. }
  290. static int mxcmci_transfer_data(struct mxcmci_host *host)
  291. {
  292. struct mmc_data *data = host->data;
  293. int stat;
  294. unsigned long length;
  295. length = data->blocks * data->blocksize;
  296. host->datasize = 0;
  297. if (data->flags & MMC_DATA_READ) {
  298. stat = mxcmci_pull(host, data->dest, length);
  299. if (stat)
  300. return stat;
  301. host->datasize += length;
  302. } else {
  303. stat = mxcmci_push(host, (const void *)(data->src), length);
  304. if (stat)
  305. return stat;
  306. host->datasize += length;
  307. stat = mxcmci_poll_status(host, STATUS_WRITE_OP_DONE);
  308. if (stat)
  309. return stat;
  310. }
  311. return 0;
  312. }
  313. static int mxcmci_cmd_done(struct mxcmci_host *host, unsigned int stat)
  314. {
  315. int datastat;
  316. int ret;
  317. ret = mxcmci_read_response(host, stat);
  318. if (ret) {
  319. mxcmci_finish_request(host, host->cmd, host->data);
  320. return ret;
  321. }
  322. if (!host->data) {
  323. mxcmci_finish_request(host, host->cmd, host->data);
  324. return 0;
  325. }
  326. datastat = mxcmci_transfer_data(host);
  327. ret = mxcmci_finish_data(host, datastat);
  328. mxcmci_finish_request(host, host->cmd, host->data);
  329. return ret;
  330. }
  331. static int mxcmci_request(struct mmc *mmc, struct mmc_cmd *cmd,
  332. struct mmc_data *data)
  333. {
  334. struct mxcmci_host *host = mmc->priv;
  335. unsigned int cmdat = host->cmdat;
  336. u32 stat;
  337. int ret;
  338. host->cmdat &= ~CMD_DAT_CONT_INIT;
  339. if (data) {
  340. mxcmci_setup_data(host, data);
  341. cmdat |= CMD_DAT_CONT_DATA_ENABLE;
  342. if (data->flags & MMC_DATA_WRITE)
  343. cmdat |= CMD_DAT_CONT_WRITE;
  344. }
  345. if ((ret = mxcmci_start_cmd(host, cmd, cmdat))) {
  346. mxcmci_finish_request(host, cmd, data);
  347. return ret;
  348. }
  349. do {
  350. stat = readl(&host->base->status);
  351. writel(stat, &host->base->status);
  352. } while (!(stat & STATUS_END_CMD_RESP));
  353. return mxcmci_cmd_done(host, stat);
  354. }
  355. static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios)
  356. {
  357. unsigned int divider;
  358. int prescaler = 0;
  359. unsigned long clk_in = mxc_get_clock(MXC_ESDHC_CLK);
  360. while (prescaler <= 0x800) {
  361. for (divider = 1; divider <= 0xF; divider++) {
  362. int x;
  363. x = (clk_in / (divider + 1));
  364. if (prescaler)
  365. x /= (prescaler * 2);
  366. if (x <= clk_ios)
  367. break;
  368. }
  369. if (divider < 0x10)
  370. break;
  371. if (prescaler == 0)
  372. prescaler = 1;
  373. else
  374. prescaler <<= 1;
  375. }
  376. writel((prescaler << 4) | divider, &host->base->clk_rate);
  377. }
  378. static void mxcmci_set_ios(struct mmc *mmc)
  379. {
  380. struct mxcmci_host *host = mmc->priv;
  381. if (mmc->bus_width == 4)
  382. host->cmdat |= CMD_DAT_CONT_BUS_WIDTH_4;
  383. else
  384. host->cmdat &= ~CMD_DAT_CONT_BUS_WIDTH_4;
  385. if (mmc->clock) {
  386. mxcmci_set_clk_rate(host, mmc->clock);
  387. writel(STR_STP_CLK_START_CLK, &host->base->str_stp_clk);
  388. } else {
  389. writel(STR_STP_CLK_STOP_CLK, &host->base->str_stp_clk);
  390. }
  391. host->clock = mmc->clock;
  392. }
  393. static int mxcmci_init(struct mmc *mmc)
  394. {
  395. struct mxcmci_host *host = mmc->priv;
  396. mxcmci_softreset(host);
  397. host->rev_no = readl(&host->base->rev_no);
  398. if (host->rev_no != 0x400) {
  399. printf("wrong rev.no. 0x%08x. aborting.\n",
  400. host->rev_no);
  401. return -ENODEV;
  402. }
  403. /* recommended in data sheet */
  404. writel(0x2db4, &host->base->read_to);
  405. writel(0, &host->base->int_cntr);
  406. return 0;
  407. }
  408. static int mxcmci_initialize(bd_t *bis)
  409. {
  410. struct mmc *mmc = NULL;
  411. mmc = malloc(sizeof(struct mmc));
  412. if (!mmc)
  413. return -ENOMEM;
  414. sprintf(mmc->name, "MXC MCI");
  415. mmc->send_cmd = mxcmci_request;
  416. mmc->set_ios = mxcmci_set_ios;
  417. mmc->init = mxcmci_init;
  418. mmc->getcd = NULL;
  419. mmc->getwp = NULL;
  420. mmc->host_caps = MMC_MODE_4BIT;
  421. host->base = (struct mxcmci_regs *)CONFIG_MXC_MCI_REGS_BASE;
  422. mmc->priv = host;
  423. host->mmc = mmc;
  424. mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  425. mmc->f_min = mxc_get_clock(MXC_ESDHC_CLK) >> 7;
  426. mmc->f_max = mxc_get_clock(MXC_ESDHC_CLK) >> 1;
  427. mmc->b_max = 0;
  428. mmc_register(mmc);
  429. return 0;
  430. }
  431. int mxc_mmc_init(bd_t *bis)
  432. {
  433. return mxcmci_initialize(bis);
  434. }