gen_atmel_mci.c 9.1 KB

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