gen_atmel_mci.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 = mmc->priv;
  51. u32 bus_hz = get_mci_clk_rate();
  52. u32 clkdiv = 255;
  53. unsigned int version = atmel_mci_get_version(mci);
  54. u32 clkodd = 0;
  55. u32 mr;
  56. debug("mci: bus_hz is %u, setting clock %u Hz, block size %u\n",
  57. bus_hz, hz, blklen);
  58. if (hz > 0) {
  59. if (version >= 0x500) {
  60. clkdiv = DIV_ROUND_UP(bus_hz, hz) - 2;
  61. if (clkdiv > 511)
  62. clkdiv = 511;
  63. clkodd = clkdiv & 1;
  64. clkdiv >>= 1;
  65. printf("mci: setting clock %u Hz, block size %u\n",
  66. bus_hz / (clkdiv * 2 + clkodd + 2), blklen);
  67. } else {
  68. /* find clkdiv yielding a rate <= than requested */
  69. for (clkdiv = 0; clkdiv < 255; clkdiv++) {
  70. if ((bus_hz / (clkdiv + 1) / 2) <= hz)
  71. break;
  72. }
  73. printf("mci: setting clock %u Hz, block size %u\n",
  74. (bus_hz / (clkdiv + 1)) / 2, blklen);
  75. }
  76. }
  77. blklen &= 0xfffc;
  78. mr = MMCI_BF(CLKDIV, clkdiv);
  79. /* MCI IP version >= 0x200 has R/WPROOF */
  80. if (version >= 0x200)
  81. mr |= MMCI_BIT(RDPROOF) | MMCI_BIT(WRPROOF);
  82. /*
  83. * MCI IP version >= 0x500 use bit 16 as clkodd.
  84. * MCI IP version < 0x500 use upper 16 bits for blklen.
  85. */
  86. if (version >= 0x500)
  87. mr |= MMCI_BF(CLKODD, clkodd);
  88. else
  89. mr |= MMCI_BF(BLKLEN, blklen);
  90. writel(mr, &mci->mr);
  91. /* MCI IP version >= 0x200 has blkr */
  92. if (version >= 0x200)
  93. writel(MMCI_BF(BLKLEN, blklen), &mci->blkr);
  94. if (mmc->card_caps & mmc->cfg->host_caps & MMC_MODE_HS)
  95. writel(MMCI_BIT(HSMODE), &mci->cfg);
  96. initialized = 1;
  97. }
  98. /* Return the CMDR with flags for a given command and data packet */
  99. static u32 mci_encode_cmd(
  100. struct mmc_cmd *cmd, struct mmc_data *data, u32* error_flags)
  101. {
  102. u32 cmdr = 0;
  103. /* Default Flags for Errors */
  104. *error_flags |= (MMCI_BIT(DTOE) | MMCI_BIT(RDIRE) | MMCI_BIT(RENDE) |
  105. MMCI_BIT(RINDE) | MMCI_BIT(RTOE));
  106. /* Default Flags for the Command */
  107. cmdr |= MMCI_BIT(MAXLAT);
  108. if (data) {
  109. cmdr |= MMCI_BF(TRCMD, 1);
  110. if (data->blocks > 1)
  111. cmdr |= MMCI_BF(TRTYP, 1);
  112. if (data->flags & MMC_DATA_READ)
  113. cmdr |= MMCI_BIT(TRDIR);
  114. }
  115. if (cmd->resp_type & MMC_RSP_CRC)
  116. *error_flags |= MMCI_BIT(RCRCE);
  117. if (cmd->resp_type & MMC_RSP_136)
  118. cmdr |= MMCI_BF(RSPTYP, 2);
  119. else if (cmd->resp_type & MMC_RSP_BUSY)
  120. cmdr |= MMCI_BF(RSPTYP, 3);
  121. else if (cmd->resp_type & MMC_RSP_PRESENT)
  122. cmdr |= MMCI_BF(RSPTYP, 1);
  123. return cmdr | MMCI_BF(CMDNB, cmd->cmdidx);
  124. }
  125. /* Entered into function pointer in mci_send_cmd */
  126. static u32 mci_data_read(atmel_mci_t *mci, u32* data, u32 error_flags)
  127. {
  128. u32 status;
  129. do {
  130. status = readl(&mci->sr);
  131. if (status & (error_flags | MMCI_BIT(OVRE)))
  132. goto io_fail;
  133. } while (!(status & MMCI_BIT(RXRDY)));
  134. if (status & MMCI_BIT(RXRDY)) {
  135. *data = readl(&mci->rdr);
  136. status = 0;
  137. }
  138. io_fail:
  139. return status;
  140. }
  141. /* Entered into function pointer in mci_send_cmd */
  142. static u32 mci_data_write(atmel_mci_t *mci, u32* data, u32 error_flags)
  143. {
  144. u32 status;
  145. do {
  146. status = readl(&mci->sr);
  147. if (status & (error_flags | MMCI_BIT(UNRE)))
  148. goto io_fail;
  149. } while (!(status & MMCI_BIT(TXRDY)));
  150. if (status & MMCI_BIT(TXRDY)) {
  151. writel(*data, &mci->tdr);
  152. status = 0;
  153. }
  154. io_fail:
  155. return status;
  156. }
  157. /*
  158. * Entered into mmc structure during driver init
  159. *
  160. * Sends a command out on the bus and deals with the block data.
  161. * Takes the mmc pointer, a command pointer, and an optional data pointer.
  162. */
  163. static int
  164. mci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
  165. {
  166. atmel_mci_t *mci = mmc->priv;
  167. u32 cmdr;
  168. u32 error_flags = 0;
  169. u32 status;
  170. if (!initialized) {
  171. puts ("MCI not initialized!\n");
  172. return COMM_ERR;
  173. }
  174. /* Figure out the transfer arguments */
  175. cmdr = mci_encode_cmd(cmd, data, &error_flags);
  176. /* For multi blocks read/write, set the block register */
  177. if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK)
  178. || (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK))
  179. writel(data->blocks | MMCI_BF(BLKLEN, mmc->read_bl_len),
  180. &mci->blkr);
  181. /* Send the command */
  182. writel(cmd->cmdarg, &mci->argr);
  183. writel(cmdr, &mci->cmdr);
  184. #ifdef DEBUG
  185. dump_cmd(cmdr, cmd->cmdarg, 0, "DEBUG");
  186. #endif
  187. /* Wait for the command to complete */
  188. while (!((status = readl(&mci->sr)) & MMCI_BIT(CMDRDY)));
  189. if ((status & error_flags) & MMCI_BIT(RTOE)) {
  190. dump_cmd(cmdr, cmd->cmdarg, status, "Command Time Out");
  191. return TIMEOUT;
  192. } else if (status & error_flags) {
  193. dump_cmd(cmdr, cmd->cmdarg, status, "Command Failed");
  194. return COMM_ERR;
  195. }
  196. /* Copy the response to the response buffer */
  197. if (cmd->resp_type & MMC_RSP_136) {
  198. cmd->response[0] = readl(&mci->rspr);
  199. cmd->response[1] = readl(&mci->rspr1);
  200. cmd->response[2] = readl(&mci->rspr2);
  201. cmd->response[3] = readl(&mci->rspr3);
  202. } else
  203. cmd->response[0] = readl(&mci->rspr);
  204. /* transfer all of the blocks */
  205. if (data) {
  206. u32 word_count, block_count;
  207. u32* ioptr;
  208. u32 sys_blocksize, dummy, i;
  209. u32 (*mci_data_op)
  210. (atmel_mci_t *mci, u32* data, u32 error_flags);
  211. if (data->flags & MMC_DATA_READ) {
  212. mci_data_op = mci_data_read;
  213. sys_blocksize = mmc->read_bl_len;
  214. ioptr = (u32*)data->dest;
  215. } else {
  216. mci_data_op = mci_data_write;
  217. sys_blocksize = mmc->write_bl_len;
  218. ioptr = (u32*)data->src;
  219. }
  220. status = 0;
  221. for (block_count = 0;
  222. block_count < data->blocks && !status;
  223. block_count++) {
  224. word_count = 0;
  225. do {
  226. status = mci_data_op(mci, ioptr, error_flags);
  227. word_count++;
  228. ioptr++;
  229. } while (!status && word_count < (data->blocksize/4));
  230. #ifdef DEBUG
  231. if (data->flags & MMC_DATA_READ)
  232. {
  233. u32 cnt = word_count * 4;
  234. printf("Read Data:\n");
  235. print_buffer(0, data->dest + cnt * block_count,
  236. 1, cnt, 0);
  237. }
  238. #endif
  239. #ifdef DEBUG
  240. if (!status && word_count < (sys_blocksize / 4))
  241. printf("filling rest of block...\n");
  242. #endif
  243. /* fill the rest of a full block */
  244. while (!status && word_count < (sys_blocksize / 4)) {
  245. status = mci_data_op(mci, &dummy,
  246. error_flags);
  247. word_count++;
  248. }
  249. if (status) {
  250. dump_cmd(cmdr, cmd->cmdarg, status,
  251. "Data Transfer Failed");
  252. return COMM_ERR;
  253. }
  254. }
  255. /* Wait for Transfer End */
  256. i = 0;
  257. do {
  258. status = readl(&mci->sr);
  259. if (status & error_flags) {
  260. dump_cmd(cmdr, cmd->cmdarg, status,
  261. "DTIP Wait Failed");
  262. return COMM_ERR;
  263. }
  264. i++;
  265. } while ((status & MMCI_BIT(DTIP)) && i < 10000);
  266. if (status & MMCI_BIT(DTIP)) {
  267. dump_cmd(cmdr, cmd->cmdarg, status,
  268. "XFER DTIP never unset, ignoring");
  269. }
  270. }
  271. return 0;
  272. }
  273. /* Entered into mmc structure during driver init */
  274. static void mci_set_ios(struct mmc *mmc)
  275. {
  276. atmel_mci_t *mci = mmc->priv;
  277. int bus_width = mmc->bus_width;
  278. unsigned int version = atmel_mci_get_version(mci);
  279. int busw;
  280. /* Set the clock speed */
  281. mci_set_mode(mmc, mmc->clock, MMC_DEFAULT_BLKLEN);
  282. /*
  283. * set the bus width and select slot for this interface
  284. * there is no capability for multiple slots on the same interface yet
  285. */
  286. if ((version & 0xf00) >= 0x300) {
  287. switch (bus_width) {
  288. case 8:
  289. busw = 3;
  290. break;
  291. case 4:
  292. busw = 2;
  293. break;
  294. default:
  295. busw = 0;
  296. break;
  297. }
  298. writel(busw << 6 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
  299. } else {
  300. busw = (bus_width == 4) ? 1 : 0;
  301. writel(busw << 7 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
  302. }
  303. }
  304. /* Entered into mmc structure during driver init */
  305. static int mci_init(struct mmc *mmc)
  306. {
  307. atmel_mci_t *mci = mmc->priv;
  308. /* Initialize controller */
  309. writel(MMCI_BIT(SWRST), &mci->cr); /* soft reset */
  310. writel(MMCI_BIT(PWSDIS), &mci->cr); /* disable power save */
  311. writel(MMCI_BIT(MCIEN), &mci->cr); /* enable mci */
  312. writel(MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr); /* select port */
  313. /* This delay can be optimized, but stick with max value */
  314. writel(0x7f, &mci->dtor);
  315. /* Disable Interrupts */
  316. writel(~0UL, &mci->idr);
  317. /* Set default clocks and blocklen */
  318. mci_set_mode(mmc, CONFIG_SYS_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
  319. return 0;
  320. }
  321. static const struct mmc_ops atmel_mci_ops = {
  322. .send_cmd = mci_send_cmd,
  323. .set_ios = mci_set_ios,
  324. .init = mci_init,
  325. };
  326. /*
  327. * This is the only exported function
  328. *
  329. * Call it with the MCI register base address
  330. */
  331. int atmel_mci_init(void *regs)
  332. {
  333. struct mmc *mmc;
  334. struct mmc_config *cfg;
  335. struct atmel_mci *mci;
  336. unsigned int version;
  337. cfg = malloc(sizeof(*cfg));
  338. if (cfg == NULL)
  339. return -1;
  340. memset(cfg, 0, sizeof(*cfg));
  341. mci = (struct atmel_mci *)regs;
  342. cfg->name = "mci";
  343. cfg->ops = &atmel_mci_ops;
  344. /* need to be able to pass these in on a board by board basis */
  345. cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  346. version = atmel_mci_get_version(mci);
  347. if ((version & 0xf00) >= 0x300) {
  348. cfg->host_caps = MMC_MODE_8BIT;
  349. cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
  350. }
  351. cfg->host_caps |= MMC_MODE_4BIT;
  352. /*
  353. * min and max frequencies determined by
  354. * max and min of clock divider
  355. */
  356. cfg->f_min = get_mci_clk_rate() / (2*256);
  357. cfg->f_max = get_mci_clk_rate() / (2*1);
  358. cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  359. mmc = mmc_create(cfg, regs);
  360. if (mmc == NULL) {
  361. free(cfg);
  362. return -1;
  363. }
  364. /* NOTE: possibly leaking the cfg structure */
  365. return 0;
  366. }