gen_atmel_mci.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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/memory-map.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. initialized = 1;
  82. }
  83. /* Return the CMDR with flags for a given command and data packet */
  84. static u32 mci_encode_cmd(
  85. struct mmc_cmd *cmd, struct mmc_data *data, u32* error_flags)
  86. {
  87. u32 cmdr = 0;
  88. /* Default Flags for Errors */
  89. *error_flags |= (MMCI_BIT(DTOE) | MMCI_BIT(RDIRE) | MMCI_BIT(RENDE) |
  90. MMCI_BIT(RINDE) | MMCI_BIT(RTOE));
  91. /* Default Flags for the Command */
  92. cmdr |= MMCI_BIT(MAXLAT);
  93. if (data) {
  94. cmdr |= MMCI_BF(TRCMD, 1);
  95. if (data->blocks > 1)
  96. cmdr |= MMCI_BF(TRTYP, 1);
  97. if (data->flags & MMC_DATA_READ)
  98. cmdr |= MMCI_BIT(TRDIR);
  99. }
  100. if (cmd->resp_type & MMC_RSP_CRC)
  101. *error_flags |= MMCI_BIT(RCRCE);
  102. if (cmd->resp_type & MMC_RSP_136)
  103. cmdr |= MMCI_BF(RSPTYP, 2);
  104. else if (cmd->resp_type & MMC_RSP_BUSY)
  105. cmdr |= MMCI_BF(RSPTYP, 3);
  106. else if (cmd->resp_type & MMC_RSP_PRESENT)
  107. cmdr |= MMCI_BF(RSPTYP, 1);
  108. return cmdr | MMCI_BF(CMDNB, cmd->cmdidx);
  109. }
  110. /* Entered into function pointer in mci_send_cmd */
  111. static u32 mci_data_read(atmel_mci_t *mci, u32* data, u32 error_flags)
  112. {
  113. u32 status;
  114. do {
  115. status = readl(&mci->sr);
  116. if (status & (error_flags | MMCI_BIT(OVRE)))
  117. goto io_fail;
  118. } while (!(status & MMCI_BIT(RXRDY)));
  119. if (status & MMCI_BIT(RXRDY)) {
  120. *data = readl(&mci->rdr);
  121. status = 0;
  122. }
  123. io_fail:
  124. return status;
  125. }
  126. /* Entered into function pointer in mci_send_cmd */
  127. static u32 mci_data_write(atmel_mci_t *mci, u32* data, u32 error_flags)
  128. {
  129. u32 status;
  130. do {
  131. status = readl(&mci->sr);
  132. if (status & (error_flags | MMCI_BIT(UNRE)))
  133. goto io_fail;
  134. } while (!(status & MMCI_BIT(TXRDY)));
  135. if (status & MMCI_BIT(TXRDY)) {
  136. writel(*data, &mci->tdr);
  137. status = 0;
  138. }
  139. io_fail:
  140. return status;
  141. }
  142. /*
  143. * Entered into mmc structure during driver init
  144. *
  145. * Sends a command out on the bus and deals with the block data.
  146. * Takes the mmc pointer, a command pointer, and an optional data pointer.
  147. */
  148. static int
  149. mci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
  150. {
  151. atmel_mci_t *mci = (atmel_mci_t *)mmc->priv;
  152. u32 cmdr;
  153. u32 error_flags = 0;
  154. u32 status;
  155. if (!initialized) {
  156. puts ("MCI not initialized!\n");
  157. return COMM_ERR;
  158. }
  159. /* Figure out the transfer arguments */
  160. cmdr = mci_encode_cmd(cmd, data, &error_flags);
  161. /* Send the command */
  162. writel(cmd->cmdarg, &mci->argr);
  163. writel(cmdr, &mci->cmdr);
  164. #ifdef DEBUG
  165. dump_cmd(cmdr, cmd->cmdarg, 0, "DEBUG");
  166. #endif
  167. /* Wait for the command to complete */
  168. while (!((status = readl(&mci->sr)) & MMCI_BIT(CMDRDY)));
  169. if (status & error_flags) {
  170. dump_cmd(cmdr, cmd->cmdarg, status, "Command Failed");
  171. return COMM_ERR;
  172. }
  173. /* Copy the response to the response buffer */
  174. if (cmd->resp_type & MMC_RSP_136) {
  175. cmd->response[0] = readl(&mci->rspr);
  176. cmd->response[1] = readl(&mci->rspr1);
  177. cmd->response[2] = readl(&mci->rspr2);
  178. cmd->response[3] = readl(&mci->rspr3);
  179. } else
  180. cmd->response[0] = readl(&mci->rspr);
  181. /* transfer all of the blocks */
  182. if (data) {
  183. u32 word_count, block_count;
  184. u32* ioptr;
  185. u32 sys_blocksize, dummy, i;
  186. u32 (*mci_data_op)
  187. (atmel_mci_t *mci, u32* data, u32 error_flags);
  188. if (data->flags & MMC_DATA_READ) {
  189. mci_data_op = mci_data_read;
  190. sys_blocksize = mmc->read_bl_len;
  191. ioptr = (u32*)data->dest;
  192. } else {
  193. mci_data_op = mci_data_write;
  194. sys_blocksize = mmc->write_bl_len;
  195. ioptr = (u32*)data->src;
  196. }
  197. status = 0;
  198. for (block_count = 0;
  199. block_count < data->blocks && !status;
  200. block_count++) {
  201. word_count = 0;
  202. do {
  203. status = mci_data_op(mci, ioptr, error_flags);
  204. word_count++;
  205. ioptr++;
  206. } while (!status && word_count < (data->blocksize/4));
  207. #ifdef DEBUG
  208. if (data->flags & MMC_DATA_READ)
  209. {
  210. printf("Read Data:\n");
  211. print_buffer(0, data->dest, 1,
  212. word_count*4, 0);
  213. }
  214. #endif
  215. #ifdef DEBUG
  216. if (!status && word_count < (sys_blocksize / 4))
  217. printf("filling rest of block...\n");
  218. #endif
  219. /* fill the rest of a full block */
  220. while (!status && word_count < (sys_blocksize / 4)) {
  221. status = mci_data_op(mci, &dummy,
  222. error_flags);
  223. word_count++;
  224. }
  225. if (status) {
  226. dump_cmd(cmdr, cmd->cmdarg, status,
  227. "Data Transfer Failed");
  228. return COMM_ERR;
  229. }
  230. }
  231. /* Wait for Transfer End */
  232. i = 0;
  233. do {
  234. status = readl(&mci->sr);
  235. if (status & error_flags) {
  236. dump_cmd(cmdr, cmd->cmdarg, status,
  237. "DTIP Wait Failed");
  238. return COMM_ERR;
  239. }
  240. i++;
  241. } while ((status & MMCI_BIT(DTIP)) && i < 10000);
  242. if (status & MMCI_BIT(DTIP)) {
  243. dump_cmd(cmdr, cmd->cmdarg, status,
  244. "XFER DTIP never unset, ignoring");
  245. }
  246. }
  247. return 0;
  248. }
  249. /* Entered into mmc structure during driver init */
  250. static void mci_set_ios(struct mmc *mmc)
  251. {
  252. atmel_mci_t *mci = (atmel_mci_t *)mmc->priv;
  253. int busw = (mmc->bus_width == 4) ? 1 : 0;
  254. /* Set the clock speed */
  255. mci_set_mode(mmc, mmc->clock, MMC_DEFAULT_BLKLEN);
  256. /*
  257. * set the bus width and select slot for this interface
  258. * there is no capability for multiple slots on the same interface yet
  259. * Bitfield SCDBUS needs to be expanded to 2 bits for 8-bit buses
  260. */
  261. writel(MMCI_BF(SCDBUS, busw) | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
  262. }
  263. /* Entered into mmc structure during driver init */
  264. static int mci_init(struct mmc *mmc)
  265. {
  266. atmel_mci_t *mci = (atmel_mci_t *)mmc->priv;
  267. /* Initialize controller */
  268. writel(MMCI_BIT(SWRST), &mci->cr); /* soft reset */
  269. writel(MMCI_BIT(PWSDIS), &mci->cr); /* disable power save */
  270. writel(MMCI_BIT(MCIEN), &mci->cr); /* enable mci */
  271. writel(MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr); /* select port */
  272. /* Initial Time-outs */
  273. writel(0x5f, &mci->dtor);
  274. /* Disable Interrupts */
  275. writel(~0UL, &mci->idr);
  276. /* Set default clocks and blocklen */
  277. mci_set_mode(mmc, CONFIG_SYS_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
  278. return 0;
  279. }
  280. /*
  281. * This is the only exported function
  282. *
  283. * Call it with the MCI register base address
  284. */
  285. int atmel_mci_init(void *regs)
  286. {
  287. struct mmc *mmc = malloc(sizeof(struct mmc));
  288. if (!mmc)
  289. return -1;
  290. strcpy(mmc->name, "mci");
  291. mmc->priv = regs;
  292. mmc->send_cmd = mci_send_cmd;
  293. mmc->set_ios = mci_set_ios;
  294. mmc->init = mci_init;
  295. /* need to be able to pass these in on a board by board basis */
  296. mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  297. mmc->host_caps = MMC_MODE_4BIT;
  298. /*
  299. * min and max frequencies determined by
  300. * max and min of clock divider
  301. */
  302. mmc->f_min = get_mci_clk_rate() / (2*256);
  303. mmc->f_max = get_mci_clk_rate() / (2*1);
  304. mmc_register(mmc);
  305. return 0;
  306. }