mvebu_mmc.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Marvell MMC/SD/SDIO driver
  3. *
  4. * (C) Copyright 2012
  5. * Marvell Semiconductor <www.marvell.com>
  6. * Written-by: Maen Suleiman, Gerald Kerma
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <malloc.h>
  12. #include <part.h>
  13. #include <mmc.h>
  14. #include <asm/io.h>
  15. #include <asm/arch/cpu.h>
  16. #include <asm/arch/kirkwood.h>
  17. #include <mvebu_mmc.h>
  18. #define DRIVER_NAME "MVEBU_MMC"
  19. static void mvebu_mmc_write(u32 offs, u32 val)
  20. {
  21. writel(val, CONFIG_SYS_MMC_BASE + (offs));
  22. }
  23. static u32 mvebu_mmc_read(u32 offs)
  24. {
  25. return readl(CONFIG_SYS_MMC_BASE + (offs));
  26. }
  27. static int mvebu_mmc_setup_data(struct mmc_data *data)
  28. {
  29. u32 ctrl_reg;
  30. debug("%s, data %s : blocks=%d blksz=%d\n", DRIVER_NAME,
  31. (data->flags & MMC_DATA_READ) ? "read" : "write",
  32. data->blocks, data->blocksize);
  33. /* default to maximum timeout */
  34. ctrl_reg = mvebu_mmc_read(SDIO_HOST_CTRL);
  35. ctrl_reg |= SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX);
  36. mvebu_mmc_write(SDIO_HOST_CTRL, ctrl_reg);
  37. if (data->flags & MMC_DATA_READ) {
  38. mvebu_mmc_write(SDIO_SYS_ADDR_LOW, (u32)data->dest & 0xffff);
  39. mvebu_mmc_write(SDIO_SYS_ADDR_HI, (u32)data->dest >> 16);
  40. } else {
  41. mvebu_mmc_write(SDIO_SYS_ADDR_LOW, (u32)data->src & 0xffff);
  42. mvebu_mmc_write(SDIO_SYS_ADDR_HI, (u32)data->src >> 16);
  43. }
  44. mvebu_mmc_write(SDIO_BLK_COUNT, data->blocks);
  45. mvebu_mmc_write(SDIO_BLK_SIZE, data->blocksize);
  46. return 0;
  47. }
  48. static int mvebu_mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
  49. struct mmc_data *data)
  50. {
  51. int timeout = 10;
  52. ushort waittype = 0;
  53. ushort resptype = 0;
  54. ushort xfertype = 0;
  55. ushort resp_indx = 0;
  56. debug("cmdidx [0x%x] resp_type[0x%x] cmdarg[0x%x]\n",
  57. cmd->cmdidx, cmd->resp_type, cmd->cmdarg);
  58. udelay(10*1000);
  59. debug("%s: cmd %d (hw state 0x%04x)\n", DRIVER_NAME,
  60. cmd->cmdidx, mvebu_mmc_read(SDIO_HW_STATE));
  61. /* Checking if card is busy */
  62. while ((mvebu_mmc_read(SDIO_HW_STATE) & CARD_BUSY)) {
  63. if (timeout == 0) {
  64. printf("%s: card busy!\n", DRIVER_NAME);
  65. return -1;
  66. }
  67. timeout--;
  68. udelay(1000);
  69. }
  70. /* Set up for a data transfer if we have one */
  71. if (data) {
  72. int err = mvebu_mmc_setup_data(data);
  73. if (err)
  74. return err;
  75. }
  76. resptype = SDIO_CMD_INDEX(cmd->cmdidx);
  77. /* Analyzing resptype/xfertype/waittype for the command */
  78. if (cmd->resp_type & MMC_RSP_BUSY)
  79. resptype |= SDIO_CMD_RSP_48BUSY;
  80. else if (cmd->resp_type & MMC_RSP_136)
  81. resptype |= SDIO_CMD_RSP_136;
  82. else if (cmd->resp_type & MMC_RSP_PRESENT)
  83. resptype |= SDIO_CMD_RSP_48;
  84. else
  85. resptype |= SDIO_CMD_RSP_NONE;
  86. if (cmd->resp_type & MMC_RSP_CRC)
  87. resptype |= SDIO_CMD_CHECK_CMDCRC;
  88. if (cmd->resp_type & MMC_RSP_OPCODE)
  89. resptype |= SDIO_CMD_INDX_CHECK;
  90. if (cmd->resp_type & MMC_RSP_PRESENT) {
  91. resptype |= SDIO_UNEXPECTED_RESP;
  92. waittype |= SDIO_NOR_UNEXP_RSP;
  93. }
  94. if (data) {
  95. resptype |= SDIO_CMD_DATA_PRESENT | SDIO_CMD_CHECK_DATACRC16;
  96. xfertype |= SDIO_XFER_MODE_HW_WR_DATA_EN;
  97. if (data->flags & MMC_DATA_READ) {
  98. xfertype |= SDIO_XFER_MODE_TO_HOST;
  99. waittype = SDIO_NOR_DMA_INI;
  100. } else {
  101. waittype |= SDIO_NOR_XFER_DONE;
  102. }
  103. } else {
  104. waittype |= SDIO_NOR_CMD_DONE;
  105. }
  106. /* Setting cmd arguments */
  107. mvebu_mmc_write(SDIO_ARG_LOW, cmd->cmdarg & 0xffff);
  108. mvebu_mmc_write(SDIO_ARG_HI, cmd->cmdarg >> 16);
  109. /* Setting Xfer mode */
  110. mvebu_mmc_write(SDIO_XFER_MODE, xfertype);
  111. mvebu_mmc_write(SDIO_NOR_INTR_STATUS, ~SDIO_NOR_CARD_INT);
  112. mvebu_mmc_write(SDIO_ERR_INTR_STATUS, SDIO_POLL_MASK);
  113. /* Sending command */
  114. mvebu_mmc_write(SDIO_CMD, resptype);
  115. mvebu_mmc_write(SDIO_NOR_INTR_EN, SDIO_POLL_MASK);
  116. mvebu_mmc_write(SDIO_ERR_INTR_EN, SDIO_POLL_MASK);
  117. /* Waiting for completion */
  118. timeout = 1000000;
  119. while (!((mvebu_mmc_read(SDIO_NOR_INTR_STATUS)) & waittype)) {
  120. if (mvebu_mmc_read(SDIO_NOR_INTR_STATUS) & SDIO_NOR_ERROR) {
  121. debug("%s: error! cmdidx : %d, err reg: %04x\n",
  122. DRIVER_NAME, cmd->cmdidx,
  123. mvebu_mmc_read(SDIO_ERR_INTR_STATUS));
  124. if (mvebu_mmc_read(SDIO_ERR_INTR_STATUS) &
  125. (SDIO_ERR_CMD_TIMEOUT | SDIO_ERR_DATA_TIMEOUT))
  126. return TIMEOUT;
  127. return COMM_ERR;
  128. }
  129. timeout--;
  130. udelay(1);
  131. if (timeout <= 0) {
  132. printf("%s: command timed out\n", DRIVER_NAME);
  133. return TIMEOUT;
  134. }
  135. }
  136. /* Handling response */
  137. if (cmd->resp_type & MMC_RSP_136) {
  138. uint response[8];
  139. for (resp_indx = 0; resp_indx < 8; resp_indx++)
  140. response[resp_indx]
  141. = mvebu_mmc_read(SDIO_RSP(resp_indx));
  142. cmd->response[0] = ((response[0] & 0x03ff) << 22) |
  143. ((response[1] & 0xffff) << 6) |
  144. ((response[2] & 0xfc00) >> 10);
  145. cmd->response[1] = ((response[2] & 0x03ff) << 22) |
  146. ((response[3] & 0xffff) << 6) |
  147. ((response[4] & 0xfc00) >> 10);
  148. cmd->response[2] = ((response[4] & 0x03ff) << 22) |
  149. ((response[5] & 0xffff) << 6) |
  150. ((response[6] & 0xfc00) >> 10);
  151. cmd->response[3] = ((response[6] & 0x03ff) << 22) |
  152. ((response[7] & 0x3fff) << 8);
  153. } else if (cmd->resp_type & MMC_RSP_PRESENT) {
  154. uint response[3];
  155. for (resp_indx = 0; resp_indx < 3; resp_indx++)
  156. response[resp_indx]
  157. = mvebu_mmc_read(SDIO_RSP(resp_indx));
  158. cmd->response[0] = ((response[2] & 0x003f) << (8 - 8)) |
  159. ((response[1] & 0xffff) << (14 - 8)) |
  160. ((response[0] & 0x03ff) << (30 - 8));
  161. cmd->response[1] = ((response[0] & 0xfc00) >> 10);
  162. cmd->response[2] = 0;
  163. cmd->response[3] = 0;
  164. }
  165. debug("%s: resp[0x%x] ", DRIVER_NAME, cmd->resp_type);
  166. debug("[0x%x] ", cmd->response[0]);
  167. debug("[0x%x] ", cmd->response[1]);
  168. debug("[0x%x] ", cmd->response[2]);
  169. debug("[0x%x] ", cmd->response[3]);
  170. debug("\n");
  171. return 0;
  172. }
  173. static void mvebu_mmc_power_up(void)
  174. {
  175. debug("%s: power up\n", DRIVER_NAME);
  176. /* disable interrupts */
  177. mvebu_mmc_write(SDIO_NOR_INTR_EN, 0);
  178. mvebu_mmc_write(SDIO_ERR_INTR_EN, 0);
  179. /* SW reset */
  180. mvebu_mmc_write(SDIO_SW_RESET, SDIO_SW_RESET_NOW);
  181. mvebu_mmc_write(SDIO_XFER_MODE, 0);
  182. /* enable status */
  183. mvebu_mmc_write(SDIO_NOR_STATUS_EN, SDIO_POLL_MASK);
  184. mvebu_mmc_write(SDIO_ERR_STATUS_EN, SDIO_POLL_MASK);
  185. /* enable interrupts status */
  186. mvebu_mmc_write(SDIO_NOR_INTR_STATUS, SDIO_POLL_MASK);
  187. mvebu_mmc_write(SDIO_ERR_INTR_STATUS, SDIO_POLL_MASK);
  188. }
  189. static void mvebu_mmc_set_clk(unsigned int clock)
  190. {
  191. unsigned int m;
  192. if (clock == 0) {
  193. debug("%s: clock off\n", DRIVER_NAME);
  194. mvebu_mmc_write(SDIO_XFER_MODE, SDIO_XFER_MODE_STOP_CLK);
  195. mvebu_mmc_write(SDIO_CLK_DIV, MVEBU_MMC_BASE_DIV_MAX);
  196. } else {
  197. m = MVEBU_MMC_BASE_FAST_CLOCK/(2*clock) - 1;
  198. if (m > MVEBU_MMC_BASE_DIV_MAX)
  199. m = MVEBU_MMC_BASE_DIV_MAX;
  200. mvebu_mmc_write(SDIO_CLK_DIV, m & MVEBU_MMC_BASE_DIV_MAX);
  201. }
  202. udelay(10*1000);
  203. }
  204. static void mvebu_mmc_set_bus(unsigned int bus)
  205. {
  206. u32 ctrl_reg = 0;
  207. ctrl_reg = mvebu_mmc_read(SDIO_HOST_CTRL);
  208. ctrl_reg &= ~SDIO_HOST_CTRL_DATA_WIDTH_4_BITS;
  209. switch (bus) {
  210. case 4:
  211. ctrl_reg |= SDIO_HOST_CTRL_DATA_WIDTH_4_BITS;
  212. break;
  213. case 1:
  214. default:
  215. ctrl_reg |= SDIO_HOST_CTRL_DATA_WIDTH_1_BIT;
  216. }
  217. /* default transfer mode */
  218. ctrl_reg |= SDIO_HOST_CTRL_BIG_ENDIAN;
  219. ctrl_reg &= ~SDIO_HOST_CTRL_LSB_FIRST;
  220. /* default to maximum timeout */
  221. ctrl_reg |= SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX);
  222. ctrl_reg |= SDIO_HOST_CTRL_PUSH_PULL_EN;
  223. ctrl_reg |= SDIO_HOST_CTRL_CARD_TYPE_MEM_ONLY;
  224. debug("%s: ctrl 0x%04x: %s %s %s\n", DRIVER_NAME, ctrl_reg,
  225. (ctrl_reg & SDIO_HOST_CTRL_PUSH_PULL_EN) ?
  226. "push-pull" : "open-drain",
  227. (ctrl_reg & SDIO_HOST_CTRL_DATA_WIDTH_4_BITS) ?
  228. "4bit-width" : "1bit-width",
  229. (ctrl_reg & SDIO_HOST_CTRL_HI_SPEED_EN) ?
  230. "high-speed" : "");
  231. mvebu_mmc_write(SDIO_HOST_CTRL, ctrl_reg);
  232. udelay(10*1000);
  233. }
  234. static void mvebu_mmc_set_ios(struct mmc *mmc)
  235. {
  236. debug("%s: bus[%d] clock[%d]\n", DRIVER_NAME,
  237. mmc->bus_width, mmc->clock);
  238. mvebu_mmc_set_bus(mmc->bus_width);
  239. mvebu_mmc_set_clk(mmc->clock);
  240. }
  241. static int mvebu_mmc_initialize(struct mmc *mmc)
  242. {
  243. debug("%s: mvebu_mmc_initialize", DRIVER_NAME);
  244. /*
  245. * Setting host parameters
  246. * Initial Host Ctrl : Timeout : max , Normal Speed mode,
  247. * 4-bit data mode, Big Endian, SD memory Card, Push_pull CMD Line
  248. */
  249. mvebu_mmc_write(SDIO_HOST_CTRL,
  250. SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX) |
  251. SDIO_HOST_CTRL_DATA_WIDTH_4_BITS |
  252. SDIO_HOST_CTRL_BIG_ENDIAN |
  253. SDIO_HOST_CTRL_PUSH_PULL_EN |
  254. SDIO_HOST_CTRL_CARD_TYPE_MEM_ONLY);
  255. mvebu_mmc_write(SDIO_CLK_CTRL, 0);
  256. /* enable status */
  257. mvebu_mmc_write(SDIO_NOR_STATUS_EN, SDIO_POLL_MASK);
  258. mvebu_mmc_write(SDIO_ERR_STATUS_EN, SDIO_POLL_MASK);
  259. /* disable interrupts */
  260. mvebu_mmc_write(SDIO_NOR_INTR_EN, 0);
  261. mvebu_mmc_write(SDIO_ERR_INTR_EN, 0);
  262. /* SW reset */
  263. mvebu_mmc_write(SDIO_SW_RESET, SDIO_SW_RESET_NOW);
  264. udelay(10*1000);
  265. return 0;
  266. }
  267. static const struct mmc_ops mvebu_mmc_ops = {
  268. .send_cmd = mvebu_mmc_send_cmd,
  269. .set_ios = mvebu_mmc_set_ios,
  270. .init = mvebu_mmc_initialize,
  271. };
  272. static struct mmc_config mvebu_mmc_cfg = {
  273. .name = DRIVER_NAME,
  274. .ops = &mvebu_mmc_ops,
  275. .f_min = MVEBU_MMC_BASE_FAST_CLOCK / MVEBU_MMC_BASE_DIV_MAX,
  276. .f_max = MVEBU_MMC_CLOCKRATE_MAX,
  277. .voltages = MMC_VDD_32_33 | MMC_VDD_33_34,
  278. .host_caps = MMC_MODE_4BIT | MMC_MODE_HS,
  279. .part_type = PART_TYPE_DOS,
  280. .b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT,
  281. };
  282. int mvebu_mmc_init(bd_t *bis)
  283. {
  284. struct mmc *mmc;
  285. mvebu_mmc_power_up();
  286. mmc = mmc_create(&mvebu_mmc_cfg, bis);
  287. if (mmc == NULL)
  288. return -1;
  289. return 0;
  290. }