uniphier_emmc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stdint.h>
  8. #include <platform_def.h>
  9. #include <arch_helpers.h>
  10. #include <drivers/io/io_block.h>
  11. #include <lib/mmio.h>
  12. #include <lib/utils_def.h>
  13. #include "uniphier.h"
  14. #define MMC_CMD_SWITCH 6
  15. #define MMC_CMD_SELECT_CARD 7
  16. #define MMC_CMD_SEND_CSD 9
  17. #define MMC_CMD_READ_MULTIPLE_BLOCK 18
  18. #define EXT_CSD_PART_CONF 179 /* R/W */
  19. #define MMC_RSP_PRESENT BIT(0)
  20. #define MMC_RSP_136 BIT(1) /* 136 bit response */
  21. #define MMC_RSP_CRC BIT(2) /* expect valid crc */
  22. #define MMC_RSP_BUSY BIT(3) /* card may send busy */
  23. #define MMC_RSP_OPCODE BIT(4) /* response contains opcode */
  24. #define MMC_RSP_NONE (0)
  25. #define MMC_RSP_R1 (MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE)
  26. #define MMC_RSP_R1b (MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE | \
  27. MMC_RSP_BUSY)
  28. #define MMC_RSP_R2 (MMC_RSP_PRESENT | MMC_RSP_136 | MMC_RSP_CRC)
  29. #define MMC_RSP_R3 (MMC_RSP_PRESENT)
  30. #define MMC_RSP_R4 (MMC_RSP_PRESENT)
  31. #define MMC_RSP_R5 (MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE)
  32. #define MMC_RSP_R6 (MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE)
  33. #define MMC_RSP_R7 (MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE)
  34. #define SDHCI_DMA_ADDRESS 0x00
  35. #define SDHCI_BLOCK_SIZE 0x04
  36. #define SDHCI_MAKE_BLKSZ(dma, blksz) ((((dma) & 0x7) << 12) | ((blksz) & 0xFFF))
  37. #define SDHCI_BLOCK_COUNT 0x06
  38. #define SDHCI_ARGUMENT 0x08
  39. #define SDHCI_TRANSFER_MODE 0x0C
  40. #define SDHCI_TRNS_DMA BIT(0)
  41. #define SDHCI_TRNS_BLK_CNT_EN BIT(1)
  42. #define SDHCI_TRNS_ACMD12 BIT(2)
  43. #define SDHCI_TRNS_READ BIT(4)
  44. #define SDHCI_TRNS_MULTI BIT(5)
  45. #define SDHCI_COMMAND 0x0E
  46. #define SDHCI_CMD_RESP_MASK 0x03
  47. #define SDHCI_CMD_CRC 0x08
  48. #define SDHCI_CMD_INDEX 0x10
  49. #define SDHCI_CMD_DATA 0x20
  50. #define SDHCI_CMD_ABORTCMD 0xC0
  51. #define SDHCI_CMD_RESP_NONE 0x00
  52. #define SDHCI_CMD_RESP_LONG 0x01
  53. #define SDHCI_CMD_RESP_SHORT 0x02
  54. #define SDHCI_CMD_RESP_SHORT_BUSY 0x03
  55. #define SDHCI_MAKE_CMD(c, f) ((((c) & 0xff) << 8) | ((f) & 0xff))
  56. #define SDHCI_RESPONSE 0x10
  57. #define SDHCI_HOST_CONTROL 0x28
  58. #define SDHCI_CTRL_DMA_MASK 0x18
  59. #define SDHCI_CTRL_SDMA 0x00
  60. #define SDHCI_BLOCK_GAP_CONTROL 0x2A
  61. #define SDHCI_SOFTWARE_RESET 0x2F
  62. #define SDHCI_RESET_CMD 0x02
  63. #define SDHCI_RESET_DATA 0x04
  64. #define SDHCI_INT_STATUS 0x30
  65. #define SDHCI_INT_RESPONSE BIT(0)
  66. #define SDHCI_INT_DATA_END BIT(1)
  67. #define SDHCI_INT_DMA_END BIT(3)
  68. #define SDHCI_INT_ERROR BIT(15)
  69. #define SDHCI_SIGNAL_ENABLE 0x38
  70. /* RCA assigned by Boot ROM */
  71. #define UNIPHIER_EMMC_RCA 0x1000
  72. struct uniphier_mmc_cmd {
  73. unsigned int cmdidx;
  74. unsigned int resp_type;
  75. unsigned int cmdarg;
  76. unsigned int is_data;
  77. };
  78. struct uniphier_emmc_host {
  79. uintptr_t base;
  80. bool is_block_addressing;
  81. };
  82. static struct uniphier_emmc_host uniphier_emmc_host;
  83. static int uniphier_emmc_send_cmd(uintptr_t host_base,
  84. struct uniphier_mmc_cmd *cmd)
  85. {
  86. uint32_t mode = 0;
  87. uint32_t end_bit;
  88. uint32_t stat, flags, dma_addr;
  89. mmio_write_32(host_base + SDHCI_INT_STATUS, -1);
  90. mmio_write_32(host_base + SDHCI_SIGNAL_ENABLE, 0);
  91. mmio_write_32(host_base + SDHCI_ARGUMENT, cmd->cmdarg);
  92. if (cmd->is_data)
  93. mode = SDHCI_TRNS_DMA | SDHCI_TRNS_BLK_CNT_EN |
  94. SDHCI_TRNS_ACMD12 | SDHCI_TRNS_READ |
  95. SDHCI_TRNS_MULTI;
  96. mmio_write_16(host_base + SDHCI_TRANSFER_MODE, mode);
  97. if (!(cmd->resp_type & MMC_RSP_PRESENT))
  98. flags = SDHCI_CMD_RESP_NONE;
  99. else if (cmd->resp_type & MMC_RSP_136)
  100. flags = SDHCI_CMD_RESP_LONG;
  101. else if (cmd->resp_type & MMC_RSP_BUSY)
  102. flags = SDHCI_CMD_RESP_SHORT_BUSY;
  103. else
  104. flags = SDHCI_CMD_RESP_SHORT;
  105. if (cmd->resp_type & MMC_RSP_CRC)
  106. flags |= SDHCI_CMD_CRC;
  107. if (cmd->resp_type & MMC_RSP_OPCODE)
  108. flags |= SDHCI_CMD_INDEX;
  109. if (cmd->is_data)
  110. flags |= SDHCI_CMD_DATA;
  111. if (cmd->resp_type & MMC_RSP_BUSY || cmd->is_data)
  112. end_bit = SDHCI_INT_DATA_END;
  113. else
  114. end_bit = SDHCI_INT_RESPONSE;
  115. mmio_write_16(host_base + SDHCI_COMMAND,
  116. SDHCI_MAKE_CMD(cmd->cmdidx, flags));
  117. do {
  118. stat = mmio_read_32(host_base + SDHCI_INT_STATUS);
  119. if (stat & SDHCI_INT_ERROR)
  120. return -EIO;
  121. if (stat & SDHCI_INT_DMA_END) {
  122. mmio_write_32(host_base + SDHCI_INT_STATUS, stat);
  123. dma_addr = mmio_read_32(host_base + SDHCI_DMA_ADDRESS);
  124. mmio_write_32(host_base + SDHCI_DMA_ADDRESS, dma_addr);
  125. }
  126. } while (!(stat & end_bit));
  127. return 0;
  128. }
  129. static int uniphier_emmc_switch_part(uintptr_t host_base, int part_num)
  130. {
  131. struct uniphier_mmc_cmd cmd = {0};
  132. cmd.cmdidx = MMC_CMD_SWITCH;
  133. cmd.resp_type = MMC_RSP_R1b;
  134. cmd.cmdarg = (EXT_CSD_PART_CONF << 16) | (part_num << 8) | (3 << 24);
  135. return uniphier_emmc_send_cmd(host_base, &cmd);
  136. }
  137. static int uniphier_emmc_check_device_size(uintptr_t host_base,
  138. bool *is_block_addressing)
  139. {
  140. struct uniphier_mmc_cmd cmd = {0};
  141. uint32_t csd40, csd72; /* CSD[71:40], CSD[103:72] */
  142. int ret;
  143. cmd.cmdidx = MMC_CMD_SEND_CSD;
  144. cmd.resp_type = MMC_RSP_R2;
  145. cmd.cmdarg = UNIPHIER_EMMC_RCA << 16;
  146. ret = uniphier_emmc_send_cmd(host_base, &cmd);
  147. if (ret)
  148. return ret;
  149. csd40 = mmio_read_32(host_base + SDHCI_RESPONSE + 4);
  150. csd72 = mmio_read_32(host_base + SDHCI_RESPONSE + 8);
  151. /* C_SIZE == 0xfff && C_SIZE_MULT == 0x7 ? */
  152. *is_block_addressing = !(~csd40 & 0xffc00380) && !(~csd72 & 0x3);
  153. return 0;
  154. }
  155. static int uniphier_emmc_load_image(uintptr_t host_base,
  156. uint32_t dev_addr,
  157. unsigned long load_addr,
  158. uint32_t block_cnt)
  159. {
  160. struct uniphier_mmc_cmd cmd = {0};
  161. uint8_t tmp;
  162. assert((load_addr >> 32) == 0);
  163. mmio_write_32(host_base + SDHCI_DMA_ADDRESS, load_addr);
  164. mmio_write_16(host_base + SDHCI_BLOCK_SIZE, SDHCI_MAKE_BLKSZ(7, 512));
  165. mmio_write_16(host_base + SDHCI_BLOCK_COUNT, block_cnt);
  166. tmp = mmio_read_8(host_base + SDHCI_HOST_CONTROL);
  167. tmp &= ~SDHCI_CTRL_DMA_MASK;
  168. tmp |= SDHCI_CTRL_SDMA;
  169. mmio_write_8(host_base + SDHCI_HOST_CONTROL, tmp);
  170. tmp = mmio_read_8(host_base + SDHCI_BLOCK_GAP_CONTROL);
  171. tmp &= ~1; /* clear Stop At Block Gap Request */
  172. mmio_write_8(host_base + SDHCI_BLOCK_GAP_CONTROL, tmp);
  173. cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
  174. cmd.resp_type = MMC_RSP_R1;
  175. cmd.cmdarg = dev_addr;
  176. cmd.is_data = 1;
  177. return uniphier_emmc_send_cmd(host_base, &cmd);
  178. }
  179. static size_t uniphier_emmc_read(int lba, uintptr_t buf, size_t size)
  180. {
  181. int ret;
  182. inv_dcache_range(buf, size);
  183. if (!uniphier_emmc_host.is_block_addressing)
  184. lba *= 512;
  185. ret = uniphier_emmc_load_image(uniphier_emmc_host.base,
  186. lba, buf, size / 512);
  187. inv_dcache_range(buf, size);
  188. return ret ? 0 : size;
  189. }
  190. static struct io_block_dev_spec uniphier_emmc_dev_spec = {
  191. .ops = {
  192. .read = uniphier_emmc_read,
  193. },
  194. .block_size = 512,
  195. };
  196. static int uniphier_emmc_hw_init(struct uniphier_emmc_host *host)
  197. {
  198. struct uniphier_mmc_cmd cmd = {0};
  199. uintptr_t host_base = uniphier_emmc_host.base;
  200. int ret;
  201. /*
  202. * deselect card before SEND_CSD command.
  203. * Do not check the return code. It fails, but it is OK.
  204. */
  205. cmd.cmdidx = MMC_CMD_SELECT_CARD;
  206. cmd.resp_type = MMC_RSP_R1;
  207. uniphier_emmc_send_cmd(host_base, &cmd); /* CMD7 (arg=0) */
  208. /* reset CMD Line */
  209. mmio_write_8(host_base + SDHCI_SOFTWARE_RESET,
  210. SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  211. while (mmio_read_8(host_base + SDHCI_SOFTWARE_RESET))
  212. ;
  213. ret = uniphier_emmc_check_device_size(host_base,
  214. &uniphier_emmc_host.is_block_addressing);
  215. if (ret)
  216. return ret;
  217. cmd.cmdarg = UNIPHIER_EMMC_RCA << 16;
  218. /* select card again */
  219. ret = uniphier_emmc_send_cmd(host_base, &cmd);
  220. if (ret)
  221. return ret;
  222. /* switch to Boot Partition 1 */
  223. ret = uniphier_emmc_switch_part(host_base, 1);
  224. if (ret)
  225. return ret;
  226. return 0;
  227. }
  228. static const uintptr_t uniphier_emmc_base[] = {
  229. [UNIPHIER_SOC_LD11] = 0x5a000200,
  230. [UNIPHIER_SOC_LD20] = 0x5a000200,
  231. [UNIPHIER_SOC_PXS3] = 0x5a000200,
  232. };
  233. int uniphier_emmc_init(unsigned int soc,
  234. struct io_block_dev_spec **block_dev_spec)
  235. {
  236. int ret;
  237. assert(soc < ARRAY_SIZE(uniphier_emmc_base));
  238. uniphier_emmc_host.base = uniphier_emmc_base[soc];
  239. if (uniphier_emmc_host.base == 0UL)
  240. return -ENOTSUP;
  241. ret = uniphier_emmc_hw_init(&uniphier_emmc_host);
  242. if (ret)
  243. return ret;
  244. *block_dev_spec = &uniphier_emmc_dev_spec;
  245. return 0;
  246. }