gen_atmel_mci.c 9.8 KB

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