gen_atmel_mci.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. 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. if (data)
  162. writel(MMCI_BF(BCNT, data->blocks) |
  163. MMCI_BF(BLKLEN, mmc->read_bl_len), &mci->blkr);
  164. /* Send the command */
  165. writel(cmd->cmdarg, &mci->argr);
  166. writel(cmdr, &mci->cmdr);
  167. #ifdef DEBUG
  168. dump_cmd(cmdr, cmd->cmdarg, 0, "DEBUG");
  169. #endif
  170. /* Wait for the command to complete */
  171. while (!((status = readl(&mci->sr)) & MMCI_BIT(CMDRDY)));
  172. if (status & error_flags) {
  173. dump_cmd(cmdr, cmd->cmdarg, status, "Command Failed");
  174. return COMM_ERR;
  175. }
  176. /* Copy the response to the response buffer */
  177. if (cmd->resp_type & MMC_RSP_136) {
  178. cmd->response[0] = readl(&mci->rspr);
  179. cmd->response[1] = readl(&mci->rspr1);
  180. cmd->response[2] = readl(&mci->rspr2);
  181. cmd->response[3] = readl(&mci->rspr3);
  182. } else
  183. cmd->response[0] = readl(&mci->rspr);
  184. /* transfer all of the blocks */
  185. if (data) {
  186. u32 word_count, block_count;
  187. u32* ioptr;
  188. u32 sys_blocksize, dummy, i;
  189. u32 (*mci_data_op)
  190. (atmel_mci_t *mci, u32* data, u32 error_flags);
  191. if (data->flags & MMC_DATA_READ) {
  192. mci_data_op = mci_data_read;
  193. sys_blocksize = mmc->read_bl_len;
  194. ioptr = (u32*)data->dest;
  195. } else {
  196. mci_data_op = mci_data_write;
  197. sys_blocksize = mmc->write_bl_len;
  198. ioptr = (u32*)data->src;
  199. }
  200. status = 0;
  201. for (block_count = 0;
  202. block_count < data->blocks && !status;
  203. block_count++) {
  204. word_count = 0;
  205. do {
  206. status = mci_data_op(mci, ioptr, error_flags);
  207. word_count++;
  208. ioptr++;
  209. } while (!status && word_count < (data->blocksize/4));
  210. #ifdef DEBUG
  211. if (data->flags & MMC_DATA_READ)
  212. {
  213. printf("Read Data:\n");
  214. print_buffer(0, data->dest, 1,
  215. word_count*4, 0);
  216. }
  217. #endif
  218. #ifdef DEBUG
  219. if (!status && word_count < (sys_blocksize / 4))
  220. printf("filling rest of block...\n");
  221. #endif
  222. /* fill the rest of a full block */
  223. while (!status && word_count < (sys_blocksize / 4)) {
  224. status = mci_data_op(mci, &dummy,
  225. error_flags);
  226. word_count++;
  227. }
  228. if (status) {
  229. dump_cmd(cmdr, cmd->cmdarg, status,
  230. "Data Transfer Failed");
  231. return COMM_ERR;
  232. }
  233. }
  234. /* Wait for Transfer End */
  235. i = 0;
  236. do {
  237. status = readl(&mci->sr);
  238. if (status & error_flags) {
  239. dump_cmd(cmdr, cmd->cmdarg, status,
  240. "DTIP Wait Failed");
  241. return COMM_ERR;
  242. }
  243. i++;
  244. } while ((status & MMCI_BIT(DTIP)) && i < 10000);
  245. if (status & MMCI_BIT(DTIP)) {
  246. dump_cmd(cmdr, cmd->cmdarg, status,
  247. "XFER DTIP never unset, ignoring");
  248. }
  249. }
  250. return 0;
  251. }
  252. /* Entered into mmc structure during driver init */
  253. static void mci_set_ios(struct mmc *mmc)
  254. {
  255. atmel_mci_t *mci = (atmel_mci_t *)mmc->priv;
  256. int busw = (mmc->bus_width == 4) ? 1 : 0;
  257. /* Set the clock speed */
  258. mci_set_mode(mmc, mmc->clock, MMC_DEFAULT_BLKLEN);
  259. /*
  260. * set the bus width and select slot for this interface
  261. * there is no capability for multiple slots on the same interface yet
  262. * Bitfield SCDBUS needs to be expanded to 2 bits for 8-bit buses
  263. */
  264. writel(MMCI_BF(SCDBUS, busw) | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
  265. }
  266. /* Entered into mmc structure during driver init */
  267. static int mci_init(struct mmc *mmc)
  268. {
  269. atmel_mci_t *mci = (atmel_mci_t *)mmc->priv;
  270. /* Initialize controller */
  271. writel(MMCI_BIT(SWRST), &mci->cr); /* soft reset */
  272. writel(MMCI_BIT(PWSDIS), &mci->cr); /* disable power save */
  273. writel(MMCI_BIT(MCIEN), &mci->cr); /* enable mci */
  274. writel(MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr); /* select port */
  275. /* Initial Time-outs */
  276. writel(0x5f, &mci->dtor);
  277. /* Disable Interrupts */
  278. writel(~0UL, &mci->idr);
  279. /* Set default clocks and blocklen */
  280. mci_set_mode(mmc, CONFIG_SYS_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
  281. return 0;
  282. }
  283. /*
  284. * This is the only exported function
  285. *
  286. * Call it with the MCI register base address
  287. */
  288. int atmel_mci_init(void *regs)
  289. {
  290. struct mmc *mmc = malloc(sizeof(struct mmc));
  291. if (!mmc)
  292. return -1;
  293. strcpy(mmc->name, "mci");
  294. mmc->priv = regs;
  295. mmc->send_cmd = mci_send_cmd;
  296. mmc->set_ios = mci_set_ios;
  297. mmc->init = mci_init;
  298. /* need to be able to pass these in on a board by board basis */
  299. mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  300. mmc->host_caps = MMC_MODE_4BIT;
  301. /*
  302. * min and max frequencies determined by
  303. * max and min of clock divider
  304. */
  305. mmc->f_min = get_mci_clk_rate() / (2*256);
  306. mmc->f_max = get_mci_clk_rate() / (2*1);
  307. mmc->b_max = 0;
  308. mmc_register(mmc);
  309. return 0;
  310. }