gen_atmel_mci.c 11 KB

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