gen_atmel_mci.c 10 KB

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