mxsmmc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * Freescale i.MX28 SSP MMC driver
  3. *
  4. * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
  5. * on behalf of DENX Software Engineering GmbH
  6. *
  7. * Based on code from LTIB:
  8. * (C) Copyright 2008-2010 Freescale Semiconductor, Inc.
  9. * Terry Lv
  10. *
  11. * Copyright 2007, Freescale Semiconductor, Inc
  12. * Andy Fleming
  13. *
  14. * Based vaguely on the pxa mmc code:
  15. * (C) Copyright 2003
  16. * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
  17. *
  18. * See file CREDITS for list of people who contributed to this
  19. * project.
  20. *
  21. * This program is free software; you can redistribute it and/or
  22. * modify it under the terms of the GNU General Public License as
  23. * published by the Free Software Foundation; either version 2 of
  24. * the License, or (at your option) any later version.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with this program; if not, write to the Free Software
  33. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  34. * MA 02111-1307 USA
  35. */
  36. #include <common.h>
  37. #include <malloc.h>
  38. #include <mmc.h>
  39. #include <asm/errno.h>
  40. #include <asm/io.h>
  41. #include <asm/arch/clock.h>
  42. #include <asm/arch/imx-regs.h>
  43. #include <asm/arch/sys_proto.h>
  44. #include <asm/arch/dma.h>
  45. #include <bouncebuf.h>
  46. struct mxsmmc_priv {
  47. int id;
  48. struct mxs_ssp_regs *regs;
  49. uint32_t buswidth;
  50. int (*mmc_is_wp)(int);
  51. int (*mmc_cd)(int);
  52. struct mxs_dma_desc *desc;
  53. };
  54. #if defined(CONFIG_MX23)
  55. static const unsigned int mxsmmc_id_offset = 1;
  56. #elif defined(CONFIG_MX28)
  57. static const unsigned int mxsmmc_id_offset = 0;
  58. #endif
  59. #define MXSMMC_MAX_TIMEOUT 10000
  60. #define MXSMMC_SMALL_TRANSFER 512
  61. static int mxsmmc_cd(struct mxsmmc_priv *priv)
  62. {
  63. struct mxs_ssp_regs *ssp_regs = priv->regs;
  64. if (priv->mmc_cd)
  65. return priv->mmc_cd(priv->id);
  66. return !(readl(&ssp_regs->hw_ssp_status) & SSP_STATUS_CARD_DETECT);
  67. }
  68. static int mxsmmc_send_cmd_pio(struct mxsmmc_priv *priv, struct mmc_data *data)
  69. {
  70. struct mxs_ssp_regs *ssp_regs = priv->regs;
  71. uint32_t *data_ptr;
  72. int timeout = MXSMMC_MAX_TIMEOUT;
  73. uint32_t reg;
  74. uint32_t data_count = data->blocksize * data->blocks;
  75. if (data->flags & MMC_DATA_READ) {
  76. data_ptr = (uint32_t *)data->dest;
  77. while (data_count && --timeout) {
  78. reg = readl(&ssp_regs->hw_ssp_status);
  79. if (!(reg & SSP_STATUS_FIFO_EMPTY)) {
  80. *data_ptr++ = readl(&ssp_regs->hw_ssp_data);
  81. data_count -= 4;
  82. timeout = MXSMMC_MAX_TIMEOUT;
  83. } else
  84. udelay(1000);
  85. }
  86. } else {
  87. data_ptr = (uint32_t *)data->src;
  88. timeout *= 100;
  89. while (data_count && --timeout) {
  90. reg = readl(&ssp_regs->hw_ssp_status);
  91. if (!(reg & SSP_STATUS_FIFO_FULL)) {
  92. writel(*data_ptr++, &ssp_regs->hw_ssp_data);
  93. data_count -= 4;
  94. timeout = MXSMMC_MAX_TIMEOUT;
  95. } else
  96. udelay(1000);
  97. }
  98. }
  99. return timeout ? 0 : COMM_ERR;
  100. }
  101. static int mxsmmc_send_cmd_dma(struct mxsmmc_priv *priv, struct mmc_data *data)
  102. {
  103. uint32_t data_count = data->blocksize * data->blocks;
  104. int dmach;
  105. struct mxs_dma_desc *desc = priv->desc;
  106. void *addr;
  107. unsigned int flags;
  108. struct bounce_buffer bbstate;
  109. memset(desc, 0, sizeof(struct mxs_dma_desc));
  110. desc->address = (dma_addr_t)desc;
  111. if (data->flags & MMC_DATA_READ) {
  112. priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_WRITE;
  113. addr = data->dest;
  114. flags = GEN_BB_WRITE;
  115. } else {
  116. priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_READ;
  117. addr = (void *)data->src;
  118. flags = GEN_BB_READ;
  119. }
  120. bounce_buffer_start(&bbstate, addr, data_count, flags);
  121. priv->desc->cmd.address = (dma_addr_t)bbstate.bounce_buffer;
  122. priv->desc->cmd.data |= MXS_DMA_DESC_IRQ | MXS_DMA_DESC_DEC_SEM |
  123. (data_count << MXS_DMA_DESC_BYTES_OFFSET);
  124. dmach = MXS_DMA_CHANNEL_AHB_APBH_SSP0 + priv->id + mxsmmc_id_offset;
  125. mxs_dma_desc_append(dmach, priv->desc);
  126. if (mxs_dma_go(dmach)) {
  127. bounce_buffer_stop(&bbstate);
  128. return COMM_ERR;
  129. }
  130. bounce_buffer_stop(&bbstate);
  131. return 0;
  132. }
  133. /*
  134. * Sends a command out on the bus. Takes the mmc pointer,
  135. * a command pointer, and an optional data pointer.
  136. */
  137. static int
  138. mxsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
  139. {
  140. struct mxsmmc_priv *priv = (struct mxsmmc_priv *)mmc->priv;
  141. struct mxs_ssp_regs *ssp_regs = priv->regs;
  142. uint32_t reg;
  143. int timeout;
  144. uint32_t ctrl0;
  145. int ret;
  146. debug("MMC%d: CMD%d\n", mmc->block_dev.dev, cmd->cmdidx);
  147. /* Check bus busy */
  148. timeout = MXSMMC_MAX_TIMEOUT;
  149. while (--timeout) {
  150. udelay(1000);
  151. reg = readl(&ssp_regs->hw_ssp_status);
  152. if (!(reg &
  153. (SSP_STATUS_BUSY | SSP_STATUS_DATA_BUSY |
  154. SSP_STATUS_CMD_BUSY))) {
  155. break;
  156. }
  157. }
  158. if (!timeout) {
  159. printf("MMC%d: Bus busy timeout!\n", mmc->block_dev.dev);
  160. return TIMEOUT;
  161. }
  162. /* See if card is present */
  163. if (!mxsmmc_cd(priv)) {
  164. printf("MMC%d: No card detected!\n", mmc->block_dev.dev);
  165. return NO_CARD_ERR;
  166. }
  167. /* Start building CTRL0 contents */
  168. ctrl0 = priv->buswidth;
  169. /* Set up command */
  170. if (!(cmd->resp_type & MMC_RSP_CRC))
  171. ctrl0 |= SSP_CTRL0_IGNORE_CRC;
  172. if (cmd->resp_type & MMC_RSP_PRESENT) /* Need to get response */
  173. ctrl0 |= SSP_CTRL0_GET_RESP;
  174. if (cmd->resp_type & MMC_RSP_136) /* It's a 136 bits response */
  175. ctrl0 |= SSP_CTRL0_LONG_RESP;
  176. if (data && (data->blocksize * data->blocks < MXSMMC_SMALL_TRANSFER))
  177. writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_clr);
  178. else
  179. writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_set);
  180. /* Command index */
  181. reg = readl(&ssp_regs->hw_ssp_cmd0);
  182. reg &= ~(SSP_CMD0_CMD_MASK | SSP_CMD0_APPEND_8CYC);
  183. reg |= cmd->cmdidx << SSP_CMD0_CMD_OFFSET;
  184. if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
  185. reg |= SSP_CMD0_APPEND_8CYC;
  186. writel(reg, &ssp_regs->hw_ssp_cmd0);
  187. /* Command argument */
  188. writel(cmd->cmdarg, &ssp_regs->hw_ssp_cmd1);
  189. /* Set up data */
  190. if (data) {
  191. /* READ or WRITE */
  192. if (data->flags & MMC_DATA_READ) {
  193. ctrl0 |= SSP_CTRL0_READ;
  194. } else if (priv->mmc_is_wp &&
  195. priv->mmc_is_wp(mmc->block_dev.dev)) {
  196. printf("MMC%d: Can not write a locked card!\n",
  197. mmc->block_dev.dev);
  198. return UNUSABLE_ERR;
  199. }
  200. ctrl0 |= SSP_CTRL0_DATA_XFER;
  201. reg = data->blocksize * data->blocks;
  202. #if defined(CONFIG_MX23)
  203. ctrl0 |= reg & SSP_CTRL0_XFER_COUNT_MASK;
  204. clrsetbits_le32(&ssp_regs->hw_ssp_cmd0,
  205. SSP_CMD0_BLOCK_SIZE_MASK | SSP_CMD0_BLOCK_COUNT_MASK,
  206. ((data->blocks - 1) << SSP_CMD0_BLOCK_COUNT_OFFSET) |
  207. ((ffs(data->blocksize) - 1) <<
  208. SSP_CMD0_BLOCK_SIZE_OFFSET));
  209. #elif defined(CONFIG_MX28)
  210. writel(reg, &ssp_regs->hw_ssp_xfer_size);
  211. reg = ((data->blocks - 1) <<
  212. SSP_BLOCK_SIZE_BLOCK_COUNT_OFFSET) |
  213. ((ffs(data->blocksize) - 1) <<
  214. SSP_BLOCK_SIZE_BLOCK_SIZE_OFFSET);
  215. writel(reg, &ssp_regs->hw_ssp_block_size);
  216. #endif
  217. }
  218. /* Kick off the command */
  219. ctrl0 |= SSP_CTRL0_WAIT_FOR_IRQ | SSP_CTRL0_ENABLE | SSP_CTRL0_RUN;
  220. writel(ctrl0, &ssp_regs->hw_ssp_ctrl0);
  221. /* Wait for the command to complete */
  222. timeout = MXSMMC_MAX_TIMEOUT;
  223. while (--timeout) {
  224. udelay(1000);
  225. reg = readl(&ssp_regs->hw_ssp_status);
  226. if (!(reg & SSP_STATUS_CMD_BUSY))
  227. break;
  228. }
  229. if (!timeout) {
  230. printf("MMC%d: Command %d busy\n",
  231. mmc->block_dev.dev, cmd->cmdidx);
  232. return TIMEOUT;
  233. }
  234. /* Check command timeout */
  235. if (reg & SSP_STATUS_RESP_TIMEOUT) {
  236. printf("MMC%d: Command %d timeout (status 0x%08x)\n",
  237. mmc->block_dev.dev, cmd->cmdidx, reg);
  238. return TIMEOUT;
  239. }
  240. /* Check command errors */
  241. if (reg & (SSP_STATUS_RESP_CRC_ERR | SSP_STATUS_RESP_ERR)) {
  242. printf("MMC%d: Command %d error (status 0x%08x)!\n",
  243. mmc->block_dev.dev, cmd->cmdidx, reg);
  244. return COMM_ERR;
  245. }
  246. /* Copy response to response buffer */
  247. if (cmd->resp_type & MMC_RSP_136) {
  248. cmd->response[3] = readl(&ssp_regs->hw_ssp_sdresp0);
  249. cmd->response[2] = readl(&ssp_regs->hw_ssp_sdresp1);
  250. cmd->response[1] = readl(&ssp_regs->hw_ssp_sdresp2);
  251. cmd->response[0] = readl(&ssp_regs->hw_ssp_sdresp3);
  252. } else
  253. cmd->response[0] = readl(&ssp_regs->hw_ssp_sdresp0);
  254. /* Return if no data to process */
  255. if (!data)
  256. return 0;
  257. if (data->blocksize * data->blocks < MXSMMC_SMALL_TRANSFER) {
  258. ret = mxsmmc_send_cmd_pio(priv, data);
  259. if (ret) {
  260. printf("MMC%d: Data timeout with command %d "
  261. "(status 0x%08x)!\n",
  262. mmc->block_dev.dev, cmd->cmdidx, reg);
  263. return ret;
  264. }
  265. } else {
  266. ret = mxsmmc_send_cmd_dma(priv, data);
  267. if (ret) {
  268. printf("MMC%d: DMA transfer failed\n",
  269. mmc->block_dev.dev);
  270. return ret;
  271. }
  272. }
  273. /* Check data errors */
  274. reg = readl(&ssp_regs->hw_ssp_status);
  275. if (reg &
  276. (SSP_STATUS_TIMEOUT | SSP_STATUS_DATA_CRC_ERR |
  277. SSP_STATUS_FIFO_OVRFLW | SSP_STATUS_FIFO_UNDRFLW)) {
  278. printf("MMC%d: Data error with command %d (status 0x%08x)!\n",
  279. mmc->block_dev.dev, cmd->cmdidx, reg);
  280. return COMM_ERR;
  281. }
  282. return 0;
  283. }
  284. static void mxsmmc_set_ios(struct mmc *mmc)
  285. {
  286. struct mxsmmc_priv *priv = (struct mxsmmc_priv *)mmc->priv;
  287. struct mxs_ssp_regs *ssp_regs = priv->regs;
  288. /* Set the clock speed */
  289. if (mmc->clock)
  290. mxs_set_ssp_busclock(priv->id, mmc->clock / 1000);
  291. switch (mmc->bus_width) {
  292. case 1:
  293. priv->buswidth = SSP_CTRL0_BUS_WIDTH_ONE_BIT;
  294. break;
  295. case 4:
  296. priv->buswidth = SSP_CTRL0_BUS_WIDTH_FOUR_BIT;
  297. break;
  298. case 8:
  299. priv->buswidth = SSP_CTRL0_BUS_WIDTH_EIGHT_BIT;
  300. break;
  301. }
  302. /* Set the bus width */
  303. clrsetbits_le32(&ssp_regs->hw_ssp_ctrl0,
  304. SSP_CTRL0_BUS_WIDTH_MASK, priv->buswidth);
  305. debug("MMC%d: Set %d bits bus width\n",
  306. mmc->block_dev.dev, mmc->bus_width);
  307. }
  308. static int mxsmmc_init(struct mmc *mmc)
  309. {
  310. struct mxsmmc_priv *priv = (struct mxsmmc_priv *)mmc->priv;
  311. struct mxs_ssp_regs *ssp_regs = priv->regs;
  312. /* Reset SSP */
  313. mxs_reset_block(&ssp_regs->hw_ssp_ctrl0_reg);
  314. /* Reconfigure the SSP block for MMC operation */
  315. writel(SSP_CTRL1_SSP_MODE_SD_MMC |
  316. SSP_CTRL1_WORD_LENGTH_EIGHT_BITS |
  317. SSP_CTRL1_DMA_ENABLE |
  318. SSP_CTRL1_POLARITY |
  319. SSP_CTRL1_RECV_TIMEOUT_IRQ_EN |
  320. SSP_CTRL1_DATA_CRC_IRQ_EN |
  321. SSP_CTRL1_DATA_TIMEOUT_IRQ_EN |
  322. SSP_CTRL1_RESP_TIMEOUT_IRQ_EN |
  323. SSP_CTRL1_RESP_ERR_IRQ_EN,
  324. &ssp_regs->hw_ssp_ctrl1_set);
  325. /* Set initial bit clock 400 KHz */
  326. mxs_set_ssp_busclock(priv->id, 400);
  327. /* Send initial 74 clock cycles (185 us @ 400 KHz)*/
  328. writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_set);
  329. udelay(200);
  330. writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_clr);
  331. return 0;
  332. }
  333. int mxsmmc_initialize(bd_t *bis, int id, int (*wp)(int), int (*cd)(int))
  334. {
  335. struct mmc *mmc = NULL;
  336. struct mxsmmc_priv *priv = NULL;
  337. int ret;
  338. #if defined(CONFIG_MX23)
  339. const unsigned int mxsmmc_max_id = 2;
  340. const unsigned int mxsmmc_clk_id = 0;
  341. #elif defined(CONFIG_MX28)
  342. const unsigned int mxsmmc_max_id = 4;
  343. const unsigned int mxsmmc_clk_id = id;
  344. #endif
  345. if (id >= mxsmmc_max_id)
  346. return -ENODEV;
  347. mmc = malloc(sizeof(struct mmc));
  348. if (!mmc)
  349. return -ENOMEM;
  350. priv = malloc(sizeof(struct mxsmmc_priv));
  351. if (!priv) {
  352. free(mmc);
  353. return -ENOMEM;
  354. }
  355. priv->desc = mxs_dma_desc_alloc();
  356. if (!priv->desc) {
  357. free(priv);
  358. free(mmc);
  359. return -ENOMEM;
  360. }
  361. ret = mxs_dma_init_channel(id + mxsmmc_id_offset);
  362. if (ret)
  363. return ret;
  364. priv->mmc_is_wp = wp;
  365. priv->mmc_cd = cd;
  366. priv->id = id;
  367. priv->regs = mxs_ssp_regs_by_bus(id);
  368. sprintf(mmc->name, "MXS MMC");
  369. mmc->send_cmd = mxsmmc_send_cmd;
  370. mmc->set_ios = mxsmmc_set_ios;
  371. mmc->init = mxsmmc_init;
  372. mmc->getcd = NULL;
  373. mmc->priv = priv;
  374. mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  375. mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT |
  376. MMC_MODE_HS_52MHz | MMC_MODE_HS;
  377. /*
  378. * SSPCLK = 480 * 18 / 29 / 1 = 297.731 MHz
  379. * SSP bit rate = SSPCLK / (CLOCK_DIVIDE * (1 + CLOCK_RATE)),
  380. * CLOCK_DIVIDE has to be an even value from 2 to 254, and
  381. * CLOCK_RATE could be any integer from 0 to 255.
  382. */
  383. mmc->f_min = 400000;
  384. mmc->f_max = mxc_get_clock(MXC_SSP0_CLK + mxsmmc_clk_id) * 1000 / 2;
  385. mmc->b_max = 0x20;
  386. mmc_register(mmc);
  387. return 0;
  388. }