mxcmmc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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 <linux/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. /* maintainer note: do we really want to have a global host pointer? */
  113. static struct mxcmci_host *host = &mxcmci_host;
  114. static inline int mxcmci_use_dma(struct mxcmci_host *host)
  115. {
  116. return host->do_dma;
  117. }
  118. static void mxcmci_softreset(struct mxcmci_host *host)
  119. {
  120. int i;
  121. /* reset sequence */
  122. writel(STR_STP_CLK_RESET, &host->base->str_stp_clk);
  123. writel(STR_STP_CLK_RESET | STR_STP_CLK_START_CLK,
  124. &host->base->str_stp_clk);
  125. for (i = 0; i < 8; i++)
  126. writel(STR_STP_CLK_START_CLK, &host->base->str_stp_clk);
  127. writel(0xff, &host->base->res_to);
  128. }
  129. static void mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data)
  130. {
  131. unsigned int nob = data->blocks;
  132. unsigned int blksz = data->blocksize;
  133. unsigned int datasize = nob * blksz;
  134. host->data = data;
  135. writel(nob, &host->base->nob);
  136. writel(blksz, &host->base->blk_len);
  137. host->datasize = datasize;
  138. }
  139. static int mxcmci_start_cmd(struct mxcmci_host *host, struct mmc_cmd *cmd,
  140. unsigned int cmdat)
  141. {
  142. if (host->cmd != NULL)
  143. printf("mxcmci: error!\n");
  144. host->cmd = cmd;
  145. switch (cmd->resp_type) {
  146. case MMC_RSP_R1: /* short CRC, OPCODE */
  147. case MMC_RSP_R1b:/* short CRC, OPCODE, BUSY */
  148. cmdat |= CMD_DAT_CONT_RESPONSE_48BIT_CRC;
  149. break;
  150. case MMC_RSP_R2: /* long 136 bit + CRC */
  151. cmdat |= CMD_DAT_CONT_RESPONSE_136BIT;
  152. break;
  153. case MMC_RSP_R3: /* short */
  154. cmdat |= CMD_DAT_CONT_RESPONSE_48BIT;
  155. break;
  156. case MMC_RSP_NONE:
  157. break;
  158. default:
  159. printf("mxcmci: unhandled response type 0x%x\n",
  160. cmd->resp_type);
  161. return -EINVAL;
  162. }
  163. writel(cmd->cmdidx, &host->base->cmd);
  164. writel(cmd->cmdarg, &host->base->arg);
  165. writel(cmdat, &host->base->cmd_dat_cont);
  166. return 0;
  167. }
  168. static void mxcmci_finish_request(struct mxcmci_host *host,
  169. struct mmc_cmd *cmd, struct mmc_data *data)
  170. {
  171. host->cmd = NULL;
  172. host->data = NULL;
  173. }
  174. static int mxcmci_finish_data(struct mxcmci_host *host, unsigned int stat)
  175. {
  176. int data_error = 0;
  177. if (stat & STATUS_ERR_MASK) {
  178. printf("request failed. status: 0x%08x\n",
  179. stat);
  180. if (stat & STATUS_CRC_READ_ERR) {
  181. data_error = -EILSEQ;
  182. } else if (stat & STATUS_CRC_WRITE_ERR) {
  183. u32 err_code = (stat >> 9) & 0x3;
  184. if (err_code == 2) /* No CRC response */
  185. data_error = -ETIMEDOUT;
  186. else
  187. data_error = -EILSEQ;
  188. } else if (stat & STATUS_TIME_OUT_READ) {
  189. data_error = -ETIMEDOUT;
  190. } else {
  191. data_error = -EIO;
  192. }
  193. }
  194. host->data = NULL;
  195. return data_error;
  196. }
  197. static int mxcmci_read_response(struct mxcmci_host *host, unsigned int stat)
  198. {
  199. struct mmc_cmd *cmd = host->cmd;
  200. int i;
  201. u32 a, b, c;
  202. u32 *resp = (u32 *)cmd->response;
  203. if (!cmd)
  204. return 0;
  205. if (stat & STATUS_TIME_OUT_RESP) {
  206. printf("CMD TIMEOUT\n");
  207. return -ETIMEDOUT;
  208. } else if (stat & STATUS_RESP_CRC_ERR && cmd->resp_type & MMC_RSP_CRC) {
  209. printf("cmd crc error\n");
  210. return -EILSEQ;
  211. }
  212. if (cmd->resp_type & MMC_RSP_PRESENT) {
  213. if (cmd->resp_type & MMC_RSP_136) {
  214. for (i = 0; i < 4; i++) {
  215. a = readl(&host->base->res_fifo) & 0xFFFF;
  216. b = readl(&host->base->res_fifo) & 0xFFFF;
  217. resp[i] = a << 16 | b;
  218. }
  219. } else {
  220. a = readl(&host->base->res_fifo) & 0xFFFF;
  221. b = readl(&host->base->res_fifo) & 0xFFFF;
  222. c = readl(&host->base->res_fifo) & 0xFFFF;
  223. resp[0] = a << 24 | b << 8 | c >> 8;
  224. }
  225. }
  226. return 0;
  227. }
  228. static int mxcmci_poll_status(struct mxcmci_host *host, u32 mask)
  229. {
  230. u32 stat;
  231. unsigned long timeout = get_ticks() + CONFIG_SYS_HZ;
  232. do {
  233. stat = readl(&host->base->status);
  234. if (stat & STATUS_ERR_MASK)
  235. return stat;
  236. if (timeout < get_ticks())
  237. return STATUS_TIME_OUT_READ;
  238. if (stat & mask)
  239. return 0;
  240. } while (1);
  241. }
  242. static int mxcmci_pull(struct mxcmci_host *host, void *_buf, int bytes)
  243. {
  244. unsigned int stat;
  245. u32 *buf = _buf;
  246. while (bytes > 3) {
  247. stat = mxcmci_poll_status(host,
  248. STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
  249. if (stat)
  250. return stat;
  251. *buf++ = readl(&host->base->buffer_access);
  252. bytes -= 4;
  253. }
  254. if (bytes) {
  255. u8 *b = (u8 *)buf;
  256. u32 tmp;
  257. stat = mxcmci_poll_status(host,
  258. STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
  259. if (stat)
  260. return stat;
  261. tmp = readl(&host->base->buffer_access);
  262. memcpy(b, &tmp, bytes);
  263. }
  264. return 0;
  265. }
  266. static int mxcmci_push(struct mxcmci_host *host, const void *_buf, int bytes)
  267. {
  268. unsigned int stat;
  269. const u32 *buf = _buf;
  270. while (bytes > 3) {
  271. stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
  272. if (stat)
  273. return stat;
  274. writel(*buf++, &host->base->buffer_access);
  275. bytes -= 4;
  276. }
  277. if (bytes) {
  278. const u8 *b = (u8 *)buf;
  279. u32 tmp;
  280. stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
  281. if (stat)
  282. return stat;
  283. memcpy(&tmp, b, bytes);
  284. writel(tmp, &host->base->buffer_access);
  285. }
  286. stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
  287. if (stat)
  288. return stat;
  289. return 0;
  290. }
  291. static int mxcmci_transfer_data(struct mxcmci_host *host)
  292. {
  293. struct mmc_data *data = host->data;
  294. int stat;
  295. unsigned long length;
  296. length = data->blocks * data->blocksize;
  297. host->datasize = 0;
  298. if (data->flags & MMC_DATA_READ) {
  299. stat = mxcmci_pull(host, data->dest, length);
  300. if (stat)
  301. return stat;
  302. host->datasize += length;
  303. } else {
  304. stat = mxcmci_push(host, (const void *)(data->src), length);
  305. if (stat)
  306. return stat;
  307. host->datasize += length;
  308. stat = mxcmci_poll_status(host, STATUS_WRITE_OP_DONE);
  309. if (stat)
  310. return stat;
  311. }
  312. return 0;
  313. }
  314. static int mxcmci_cmd_done(struct mxcmci_host *host, unsigned int stat)
  315. {
  316. int datastat;
  317. int ret;
  318. ret = mxcmci_read_response(host, stat);
  319. if (ret) {
  320. mxcmci_finish_request(host, host->cmd, host->data);
  321. return ret;
  322. }
  323. if (!host->data) {
  324. mxcmci_finish_request(host, host->cmd, host->data);
  325. return 0;
  326. }
  327. datastat = mxcmci_transfer_data(host);
  328. ret = mxcmci_finish_data(host, datastat);
  329. mxcmci_finish_request(host, host->cmd, host->data);
  330. return ret;
  331. }
  332. static int mxcmci_request(struct mmc *mmc, struct mmc_cmd *cmd,
  333. struct mmc_data *data)
  334. {
  335. struct mxcmci_host *host = mmc->priv;
  336. unsigned int cmdat = host->cmdat;
  337. u32 stat;
  338. int ret;
  339. host->cmdat &= ~CMD_DAT_CONT_INIT;
  340. if (data) {
  341. mxcmci_setup_data(host, data);
  342. cmdat |= CMD_DAT_CONT_DATA_ENABLE;
  343. if (data->flags & MMC_DATA_WRITE)
  344. cmdat |= CMD_DAT_CONT_WRITE;
  345. }
  346. if ((ret = mxcmci_start_cmd(host, cmd, cmdat))) {
  347. mxcmci_finish_request(host, cmd, data);
  348. return ret;
  349. }
  350. do {
  351. stat = readl(&host->base->status);
  352. writel(stat, &host->base->status);
  353. } while (!(stat & STATUS_END_CMD_RESP));
  354. return mxcmci_cmd_done(host, stat);
  355. }
  356. static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios)
  357. {
  358. unsigned int divider;
  359. int prescaler = 0;
  360. unsigned long clk_in = mxc_get_clock(MXC_ESDHC_CLK);
  361. while (prescaler <= 0x800) {
  362. for (divider = 1; divider <= 0xF; divider++) {
  363. int x;
  364. x = (clk_in / (divider + 1));
  365. if (prescaler)
  366. x /= (prescaler * 2);
  367. if (x <= clk_ios)
  368. break;
  369. }
  370. if (divider < 0x10)
  371. break;
  372. if (prescaler == 0)
  373. prescaler = 1;
  374. else
  375. prescaler <<= 1;
  376. }
  377. writel((prescaler << 4) | divider, &host->base->clk_rate);
  378. }
  379. static int mxcmci_set_ios(struct mmc *mmc)
  380. {
  381. struct mxcmci_host *host = mmc->priv;
  382. if (mmc->bus_width == 4)
  383. host->cmdat |= CMD_DAT_CONT_BUS_WIDTH_4;
  384. else
  385. host->cmdat &= ~CMD_DAT_CONT_BUS_WIDTH_4;
  386. if (mmc->clock) {
  387. mxcmci_set_clk_rate(host, mmc->clock);
  388. writel(STR_STP_CLK_START_CLK, &host->base->str_stp_clk);
  389. } else {
  390. writel(STR_STP_CLK_STOP_CLK, &host->base->str_stp_clk);
  391. }
  392. host->clock = mmc->clock;
  393. return 0;
  394. }
  395. static int mxcmci_init(struct mmc *mmc)
  396. {
  397. struct mxcmci_host *host = mmc->priv;
  398. mxcmci_softreset(host);
  399. host->rev_no = readl(&host->base->rev_no);
  400. if (host->rev_no != 0x400) {
  401. printf("wrong rev.no. 0x%08x. aborting.\n",
  402. host->rev_no);
  403. return -ENODEV;
  404. }
  405. /* recommended in data sheet */
  406. writel(0x2db4, &host->base->read_to);
  407. writel(0, &host->base->int_cntr);
  408. return 0;
  409. }
  410. static const struct mmc_ops mxcmci_ops = {
  411. .send_cmd = mxcmci_request,
  412. .set_ios = mxcmci_set_ios,
  413. .init = mxcmci_init,
  414. };
  415. static struct mmc_config mxcmci_cfg = {
  416. .name = "MXC MCI",
  417. .ops = &mxcmci_ops,
  418. .host_caps = MMC_MODE_4BIT,
  419. .voltages = MMC_VDD_32_33 | MMC_VDD_33_34,
  420. .b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT,
  421. };
  422. static int mxcmci_initialize(bd_t *bis)
  423. {
  424. host->base = (struct mxcmci_regs *)CONFIG_MXC_MCI_REGS_BASE;
  425. mxcmci_cfg.f_min = mxc_get_clock(MXC_ESDHC_CLK) >> 7;
  426. mxcmci_cfg.f_max = mxc_get_clock(MXC_ESDHC_CLK) >> 1;
  427. host->mmc = mmc_create(&mxcmci_cfg, host);
  428. if (host->mmc == NULL)
  429. return -1;
  430. return 0;
  431. }
  432. int mxc_mmc_init(bd_t *bis)
  433. {
  434. return mxcmci_initialize(bis);
  435. }