gen_atmel_mci.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright (C) 2010
  3. * Rob Emanuele <rob@emanuele.us>
  4. * Reinhard Meyer, EMK Elektronik <reinhard.meyer@emk-elektronik.de>
  5. *
  6. * Original Driver:
  7. * Copyright (C) 2004-2006 Atmel Corporation
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <mmc.h>
  13. #include <part.h>
  14. #include <malloc.h>
  15. #include <asm/io.h>
  16. #include <asm/errno.h>
  17. #include <asm/byteorder.h>
  18. #include <asm/arch/clk.h>
  19. #include <asm/arch/hardware.h>
  20. #include "atmel_mci.h"
  21. #ifndef CONFIG_SYS_MMC_CLK_OD
  22. # define CONFIG_SYS_MMC_CLK_OD 150000
  23. #endif
  24. #define MMC_DEFAULT_BLKLEN 512
  25. #if defined(CONFIG_ATMEL_MCI_PORTB)
  26. # define MCI_BUS 1
  27. #else
  28. # define MCI_BUS 0
  29. #endif
  30. static int initialized = 0;
  31. /* Read Atmel MCI IP version */
  32. static unsigned int atmel_mci_get_version(struct atmel_mci *mci)
  33. {
  34. return readl(&mci->version) & 0x00000fff;
  35. }
  36. /*
  37. * Print command and status:
  38. *
  39. * - always when DEBUG is defined
  40. * - on command errors
  41. */
  42. static void dump_cmd(u32 cmdr, u32 arg, u32 status, const char* msg)
  43. {
  44. printf("gen_atmel_mci: CMDR %08x (%2u) ARGR %08x (SR: %08x) %s\n",
  45. cmdr, cmdr&0x3F, arg, status, msg);
  46. }
  47. /* Setup for MCI Clock and Block Size */
  48. static void mci_set_mode(struct mmc *mmc, u32 hz, u32 blklen)
  49. {
  50. atmel_mci_t *mci = (atmel_mci_t *)mmc->priv;
  51. u32 bus_hz = get_mci_clk_rate();
  52. u32 clkdiv = 255;
  53. debug("mci: bus_hz is %u, setting clock %u Hz, block size %u\n",
  54. bus_hz, hz, blklen);
  55. if (hz > 0) {
  56. /* find lowest clkdiv yielding a rate <= than requested */
  57. for (clkdiv=0; clkdiv<255; clkdiv++) {
  58. if ((bus_hz / (clkdiv+1) / 2) <= hz)
  59. break;
  60. }
  61. }
  62. printf("mci: setting clock %u Hz, block size %u\n",
  63. (bus_hz / (clkdiv+1)) / 2, blklen);
  64. blklen &= 0xfffc;
  65. /* On some platforms RDPROOF and WRPROOF are ignored */
  66. writel((MMCI_BF(CLKDIV, clkdiv)
  67. | MMCI_BF(BLKLEN, blklen)
  68. | MMCI_BIT(RDPROOF)
  69. | MMCI_BIT(WRPROOF)), &mci->mr);
  70. /*
  71. * On some new platforms BLKLEN in mci->mr is ignored.
  72. * Should use the BLKLEN in the block register.
  73. */
  74. writel(MMCI_BF(BLKLEN, blklen), &mci->blkr);
  75. initialized = 1;
  76. }
  77. /* Return the CMDR with flags for a given command and data packet */
  78. static u32 mci_encode_cmd(
  79. struct mmc_cmd *cmd, struct mmc_data *data, u32* error_flags)
  80. {
  81. u32 cmdr = 0;
  82. /* Default Flags for Errors */
  83. *error_flags |= (MMCI_BIT(DTOE) | MMCI_BIT(RDIRE) | MMCI_BIT(RENDE) |
  84. MMCI_BIT(RINDE) | MMCI_BIT(RTOE));
  85. /* Default Flags for the Command */
  86. cmdr |= MMCI_BIT(MAXLAT);
  87. if (data) {
  88. cmdr |= MMCI_BF(TRCMD, 1);
  89. if (data->blocks > 1)
  90. cmdr |= MMCI_BF(TRTYP, 1);
  91. if (data->flags & MMC_DATA_READ)
  92. cmdr |= MMCI_BIT(TRDIR);
  93. }
  94. if (cmd->resp_type & MMC_RSP_CRC)
  95. *error_flags |= MMCI_BIT(RCRCE);
  96. if (cmd->resp_type & MMC_RSP_136)
  97. cmdr |= MMCI_BF(RSPTYP, 2);
  98. else if (cmd->resp_type & MMC_RSP_BUSY)
  99. cmdr |= MMCI_BF(RSPTYP, 3);
  100. else if (cmd->resp_type & MMC_RSP_PRESENT)
  101. cmdr |= MMCI_BF(RSPTYP, 1);
  102. return cmdr | MMCI_BF(CMDNB, cmd->cmdidx);
  103. }
  104. /* Entered into function pointer in mci_send_cmd */
  105. static u32 mci_data_read(atmel_mci_t *mci, u32* data, u32 error_flags)
  106. {
  107. u32 status;
  108. do {
  109. status = readl(&mci->sr);
  110. if (status & (error_flags | MMCI_BIT(OVRE)))
  111. goto io_fail;
  112. } while (!(status & MMCI_BIT(RXRDY)));
  113. if (status & MMCI_BIT(RXRDY)) {
  114. *data = readl(&mci->rdr);
  115. status = 0;
  116. }
  117. io_fail:
  118. return status;
  119. }
  120. /* Entered into function pointer in mci_send_cmd */
  121. static u32 mci_data_write(atmel_mci_t *mci, u32* data, u32 error_flags)
  122. {
  123. u32 status;
  124. do {
  125. status = readl(&mci->sr);
  126. if (status & (error_flags | MMCI_BIT(UNRE)))
  127. goto io_fail;
  128. } while (!(status & MMCI_BIT(TXRDY)));
  129. if (status & MMCI_BIT(TXRDY)) {
  130. writel(*data, &mci->tdr);
  131. status = 0;
  132. }
  133. io_fail:
  134. return status;
  135. }
  136. /*
  137. * Entered into mmc structure during driver init
  138. *
  139. * Sends a command out on the bus and deals with the block data.
  140. * Takes the mmc pointer, a command pointer, and an optional data pointer.
  141. */
  142. static int
  143. mci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
  144. {
  145. atmel_mci_t *mci = (atmel_mci_t *)mmc->priv;
  146. u32 cmdr;
  147. u32 error_flags = 0;
  148. u32 status;
  149. if (!initialized) {
  150. puts ("MCI not initialized!\n");
  151. return COMM_ERR;
  152. }
  153. /* Figure out the transfer arguments */
  154. cmdr = mci_encode_cmd(cmd, data, &error_flags);
  155. /* For multi blocks read/write, set the block register */
  156. if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK)
  157. || (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK))
  158. writel(data->blocks | MMCI_BF(BLKLEN, mmc->read_bl_len),
  159. &mci->blkr);
  160. /* Send the command */
  161. writel(cmd->cmdarg, &mci->argr);
  162. writel(cmdr, &mci->cmdr);
  163. #ifdef DEBUG
  164. dump_cmd(cmdr, cmd->cmdarg, 0, "DEBUG");
  165. #endif
  166. /* Wait for the command to complete */
  167. while (!((status = readl(&mci->sr)) & MMCI_BIT(CMDRDY)));
  168. if ((status & error_flags) & MMCI_BIT(RTOE)) {
  169. dump_cmd(cmdr, cmd->cmdarg, status, "Command Time Out");
  170. return TIMEOUT;
  171. } else if (status & error_flags) {
  172. dump_cmd(cmdr, cmd->cmdarg, status, "Command Failed");
  173. return COMM_ERR;
  174. }
  175. /* Copy the response to the response buffer */
  176. if (cmd->resp_type & MMC_RSP_136) {
  177. cmd->response[0] = readl(&mci->rspr);
  178. cmd->response[1] = readl(&mci->rspr1);
  179. cmd->response[2] = readl(&mci->rspr2);
  180. cmd->response[3] = readl(&mci->rspr3);
  181. } else
  182. cmd->response[0] = readl(&mci->rspr);
  183. /* transfer all of the blocks */
  184. if (data) {
  185. u32 word_count, block_count;
  186. u32* ioptr;
  187. u32 sys_blocksize, dummy, i;
  188. u32 (*mci_data_op)
  189. (atmel_mci_t *mci, u32* data, u32 error_flags);
  190. if (data->flags & MMC_DATA_READ) {
  191. mci_data_op = mci_data_read;
  192. sys_blocksize = mmc->read_bl_len;
  193. ioptr = (u32*)data->dest;
  194. } else {
  195. mci_data_op = mci_data_write;
  196. sys_blocksize = mmc->write_bl_len;
  197. ioptr = (u32*)data->src;
  198. }
  199. status = 0;
  200. for (block_count = 0;
  201. block_count < data->blocks && !status;
  202. block_count++) {
  203. word_count = 0;
  204. do {
  205. status = mci_data_op(mci, ioptr, error_flags);
  206. word_count++;
  207. ioptr++;
  208. } while (!status && word_count < (data->blocksize/4));
  209. #ifdef DEBUG
  210. if (data->flags & MMC_DATA_READ)
  211. {
  212. printf("Read Data:\n");
  213. print_buffer(0, data->dest, 1,
  214. word_count*4, 0);
  215. }
  216. #endif
  217. #ifdef DEBUG
  218. if (!status && word_count < (sys_blocksize / 4))
  219. printf("filling rest of block...\n");
  220. #endif
  221. /* fill the rest of a full block */
  222. while (!status && word_count < (sys_blocksize / 4)) {
  223. status = mci_data_op(mci, &dummy,
  224. error_flags);
  225. word_count++;
  226. }
  227. if (status) {
  228. dump_cmd(cmdr, cmd->cmdarg, status,
  229. "Data Transfer Failed");
  230. return COMM_ERR;
  231. }
  232. }
  233. /* Wait for Transfer End */
  234. i = 0;
  235. do {
  236. status = readl(&mci->sr);
  237. if (status & error_flags) {
  238. dump_cmd(cmdr, cmd->cmdarg, status,
  239. "DTIP Wait Failed");
  240. return COMM_ERR;
  241. }
  242. i++;
  243. } while ((status & MMCI_BIT(DTIP)) && i < 10000);
  244. if (status & MMCI_BIT(DTIP)) {
  245. dump_cmd(cmdr, cmd->cmdarg, status,
  246. "XFER DTIP never unset, ignoring");
  247. }
  248. }
  249. return 0;
  250. }
  251. /* Entered into mmc structure during driver init */
  252. static void mci_set_ios(struct mmc *mmc)
  253. {
  254. atmel_mci_t *mci = (atmel_mci_t *)mmc->priv;
  255. int bus_width = mmc->bus_width;
  256. unsigned int version = atmel_mci_get_version(mci);
  257. int busw;
  258. /* Set the clock speed */
  259. mci_set_mode(mmc, mmc->clock, MMC_DEFAULT_BLKLEN);
  260. /*
  261. * set the bus width and select slot for this interface
  262. * there is no capability for multiple slots on the same interface yet
  263. */
  264. if ((version & 0xf00) >= 0x300) {
  265. switch (bus_width) {
  266. case 8:
  267. busw = 3;
  268. break;
  269. case 4:
  270. busw = 2;
  271. break;
  272. default:
  273. busw = 0;
  274. break;
  275. }
  276. writel(busw << 6 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
  277. } else {
  278. busw = (bus_width == 4) ? 1 : 0;
  279. writel(busw << 7 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
  280. }
  281. }
  282. /* Entered into mmc structure during driver init */
  283. static int mci_init(struct mmc *mmc)
  284. {
  285. atmel_mci_t *mci = (atmel_mci_t *)mmc->priv;
  286. /* Initialize controller */
  287. writel(MMCI_BIT(SWRST), &mci->cr); /* soft reset */
  288. writel(MMCI_BIT(PWSDIS), &mci->cr); /* disable power save */
  289. writel(MMCI_BIT(MCIEN), &mci->cr); /* enable mci */
  290. writel(MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr); /* select port */
  291. /* This delay can be optimized, but stick with max value */
  292. writel(0x7f, &mci->dtor);
  293. /* Disable Interrupts */
  294. writel(~0UL, &mci->idr);
  295. /* Set default clocks and blocklen */
  296. mci_set_mode(mmc, CONFIG_SYS_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
  297. return 0;
  298. }
  299. static const struct mmc_ops atmel_mci_ops = {
  300. .send_cmd = mci_send_cmd,
  301. .set_ios = mci_set_ios,
  302. .init = mci_init,
  303. };
  304. /*
  305. * This is the only exported function
  306. *
  307. * Call it with the MCI register base address
  308. */
  309. int atmel_mci_init(void *regs)
  310. {
  311. struct mmc *mmc = malloc(sizeof(struct mmc));
  312. struct atmel_mci *mci;
  313. unsigned int version;
  314. if (!mmc)
  315. return -1;
  316. strcpy(mmc->name, "mci");
  317. mmc->priv = regs;
  318. mmc->ops = &atmel_mci_ops;
  319. /* need to be able to pass these in on a board by board basis */
  320. mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  321. mci = (struct atmel_mci *)mmc->priv;
  322. version = atmel_mci_get_version(mci);
  323. if ((version & 0xf00) >= 0x300)
  324. mmc->host_caps = MMC_MODE_8BIT;
  325. mmc->host_caps |= MMC_MODE_4BIT;
  326. /*
  327. * min and max frequencies determined by
  328. * max and min of clock divider
  329. */
  330. mmc->f_min = get_mci_clk_rate() / (2*256);
  331. mmc->f_max = get_mci_clk_rate() / (2*1);
  332. mmc->b_max = 0;
  333. mmc_register(mmc);
  334. return 0;
  335. }