mmc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright 2008,2010 Freescale Semiconductor, Inc
  3. * Andy Fleming
  4. *
  5. * Based (loosely) on the Linux code
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #ifndef _MMC_H_
  10. #define _MMC_H_
  11. #include <linux/list.h>
  12. #include <linux/compiler.h>
  13. #define SD_VERSION_SD 0x20000
  14. #define SD_VERSION_3 (SD_VERSION_SD | 0x300)
  15. #define SD_VERSION_2 (SD_VERSION_SD | 0x200)
  16. #define SD_VERSION_1_0 (SD_VERSION_SD | 0x100)
  17. #define SD_VERSION_1_10 (SD_VERSION_SD | 0x10a)
  18. #define MMC_VERSION_MMC 0x10000
  19. #define MMC_VERSION_UNKNOWN (MMC_VERSION_MMC)
  20. #define MMC_VERSION_1_2 (MMC_VERSION_MMC | 0x102)
  21. #define MMC_VERSION_1_4 (MMC_VERSION_MMC | 0x104)
  22. #define MMC_VERSION_2_2 (MMC_VERSION_MMC | 0x202)
  23. #define MMC_VERSION_3 (MMC_VERSION_MMC | 0x300)
  24. #define MMC_VERSION_4 (MMC_VERSION_MMC | 0x400)
  25. #define MMC_VERSION_4_1 (MMC_VERSION_MMC | 0x401)
  26. #define MMC_VERSION_4_2 (MMC_VERSION_MMC | 0x402)
  27. #define MMC_VERSION_4_3 (MMC_VERSION_MMC | 0x403)
  28. #define MMC_VERSION_4_41 (MMC_VERSION_MMC | 0x429)
  29. #define MMC_VERSION_4_5 (MMC_VERSION_MMC | 0x405)
  30. #define MMC_MODE_HS 0x001
  31. #define MMC_MODE_HS_52MHz 0x010
  32. #define MMC_MODE_4BIT 0x100
  33. #define MMC_MODE_8BIT 0x200
  34. #define MMC_MODE_SPI 0x400
  35. #define MMC_MODE_HC 0x800
  36. #define MMC_MODE_MASK_WIDTH_BITS (MMC_MODE_4BIT | MMC_MODE_8BIT)
  37. #define MMC_MODE_WIDTH_BITS_SHIFT 8
  38. #define SD_DATA_4BIT 0x00040000
  39. #define IS_SD(x) (x->version & SD_VERSION_SD)
  40. #define MMC_DATA_READ 1
  41. #define MMC_DATA_WRITE 2
  42. #define NO_CARD_ERR -16 /* No SD/MMC card inserted */
  43. #define UNUSABLE_ERR -17 /* Unusable Card */
  44. #define COMM_ERR -18 /* Communications Error */
  45. #define TIMEOUT -19
  46. #define IN_PROGRESS -20 /* operation is in progress */
  47. #define MMC_CMD_GO_IDLE_STATE 0
  48. #define MMC_CMD_SEND_OP_COND 1
  49. #define MMC_CMD_ALL_SEND_CID 2
  50. #define MMC_CMD_SET_RELATIVE_ADDR 3
  51. #define MMC_CMD_SET_DSR 4
  52. #define MMC_CMD_SWITCH 6
  53. #define MMC_CMD_SELECT_CARD 7
  54. #define MMC_CMD_SEND_EXT_CSD 8
  55. #define MMC_CMD_SEND_CSD 9
  56. #define MMC_CMD_SEND_CID 10
  57. #define MMC_CMD_STOP_TRANSMISSION 12
  58. #define MMC_CMD_SEND_STATUS 13
  59. #define MMC_CMD_SET_BLOCKLEN 16
  60. #define MMC_CMD_READ_SINGLE_BLOCK 17
  61. #define MMC_CMD_READ_MULTIPLE_BLOCK 18
  62. #define MMC_CMD_WRITE_SINGLE_BLOCK 24
  63. #define MMC_CMD_WRITE_MULTIPLE_BLOCK 25
  64. #define MMC_CMD_ERASE_GROUP_START 35
  65. #define MMC_CMD_ERASE_GROUP_END 36
  66. #define MMC_CMD_ERASE 38
  67. #define MMC_CMD_APP_CMD 55
  68. #define MMC_CMD_SPI_READ_OCR 58
  69. #define MMC_CMD_SPI_CRC_ON_OFF 59
  70. #define MMC_CMD_RES_MAN 62
  71. #define MMC_CMD62_ARG1 0xefac62ec
  72. #define MMC_CMD62_ARG2 0xcbaea7
  73. #define SD_CMD_SEND_RELATIVE_ADDR 3
  74. #define SD_CMD_SWITCH_FUNC 6
  75. #define SD_CMD_SEND_IF_COND 8
  76. #define SD_CMD_APP_SET_BUS_WIDTH 6
  77. #define SD_CMD_ERASE_WR_BLK_START 32
  78. #define SD_CMD_ERASE_WR_BLK_END 33
  79. #define SD_CMD_APP_SEND_OP_COND 41
  80. #define SD_CMD_APP_SEND_SCR 51
  81. /* SCR definitions in different words */
  82. #define SD_HIGHSPEED_BUSY 0x00020000
  83. #define SD_HIGHSPEED_SUPPORTED 0x00020000
  84. #define MMC_HS_TIMING 0x00000100
  85. #define MMC_HS_52MHZ 0x2
  86. #define OCR_BUSY 0x80000000
  87. #define OCR_HCS 0x40000000
  88. #define OCR_VOLTAGE_MASK 0x007FFF80
  89. #define OCR_ACCESS_MODE 0x60000000
  90. #define SECURE_ERASE 0x80000000
  91. #define MMC_STATUS_MASK (~0x0206BF7F)
  92. #define MMC_STATUS_RDY_FOR_DATA (1 << 8)
  93. #define MMC_STATUS_CURR_STATE (0xf << 9)
  94. #define MMC_STATUS_ERROR (1 << 19)
  95. #define MMC_STATE_PRG (7 << 9)
  96. #define MMC_VDD_165_195 0x00000080 /* VDD voltage 1.65 - 1.95 */
  97. #define MMC_VDD_20_21 0x00000100 /* VDD voltage 2.0 ~ 2.1 */
  98. #define MMC_VDD_21_22 0x00000200 /* VDD voltage 2.1 ~ 2.2 */
  99. #define MMC_VDD_22_23 0x00000400 /* VDD voltage 2.2 ~ 2.3 */
  100. #define MMC_VDD_23_24 0x00000800 /* VDD voltage 2.3 ~ 2.4 */
  101. #define MMC_VDD_24_25 0x00001000 /* VDD voltage 2.4 ~ 2.5 */
  102. #define MMC_VDD_25_26 0x00002000 /* VDD voltage 2.5 ~ 2.6 */
  103. #define MMC_VDD_26_27 0x00004000 /* VDD voltage 2.6 ~ 2.7 */
  104. #define MMC_VDD_27_28 0x00008000 /* VDD voltage 2.7 ~ 2.8 */
  105. #define MMC_VDD_28_29 0x00010000 /* VDD voltage 2.8 ~ 2.9 */
  106. #define MMC_VDD_29_30 0x00020000 /* VDD voltage 2.9 ~ 3.0 */
  107. #define MMC_VDD_30_31 0x00040000 /* VDD voltage 3.0 ~ 3.1 */
  108. #define MMC_VDD_31_32 0x00080000 /* VDD voltage 3.1 ~ 3.2 */
  109. #define MMC_VDD_32_33 0x00100000 /* VDD voltage 3.2 ~ 3.3 */
  110. #define MMC_VDD_33_34 0x00200000 /* VDD voltage 3.3 ~ 3.4 */
  111. #define MMC_VDD_34_35 0x00400000 /* VDD voltage 3.4 ~ 3.5 */
  112. #define MMC_VDD_35_36 0x00800000 /* VDD voltage 3.5 ~ 3.6 */
  113. #define MMC_SWITCH_MODE_CMD_SET 0x00 /* Change the command set */
  114. #define MMC_SWITCH_MODE_SET_BITS 0x01 /* Set bits in EXT_CSD byte
  115. addressed by index which are
  116. 1 in value field */
  117. #define MMC_SWITCH_MODE_CLEAR_BITS 0x02 /* Clear bits in EXT_CSD byte
  118. addressed by index, which are
  119. 1 in value field */
  120. #define MMC_SWITCH_MODE_WRITE_BYTE 0x03 /* Set target byte to value */
  121. #define SD_SWITCH_CHECK 0
  122. #define SD_SWITCH_SWITCH 1
  123. /*
  124. * EXT_CSD fields
  125. */
  126. #define EXT_CSD_GP_SIZE_MULT 143 /* R/W */
  127. #define EXT_CSD_PARTITIONING_SUPPORT 160 /* RO */
  128. #define EXT_CSD_RPMB_MULT 168 /* RO */
  129. #define EXT_CSD_ERASE_GROUP_DEF 175 /* R/W */
  130. #define EXT_CSD_BOOT_BUS_WIDTH 177
  131. #define EXT_CSD_PART_CONF 179 /* R/W */
  132. #define EXT_CSD_BUS_WIDTH 183 /* R/W */
  133. #define EXT_CSD_HS_TIMING 185 /* R/W */
  134. #define EXT_CSD_REV 192 /* RO */
  135. #define EXT_CSD_CARD_TYPE 196 /* RO */
  136. #define EXT_CSD_SEC_CNT 212 /* RO, 4 bytes */
  137. #define EXT_CSD_HC_WP_GRP_SIZE 221 /* RO */
  138. #define EXT_CSD_HC_ERASE_GRP_SIZE 224 /* RO */
  139. #define EXT_CSD_BOOT_MULT 226 /* RO */
  140. /*
  141. * EXT_CSD field definitions
  142. */
  143. #define EXT_CSD_CMD_SET_NORMAL (1 << 0)
  144. #define EXT_CSD_CMD_SET_SECURE (1 << 1)
  145. #define EXT_CSD_CMD_SET_CPSECURE (1 << 2)
  146. #define EXT_CSD_CARD_TYPE_26 (1 << 0) /* Card can run at 26MHz */
  147. #define EXT_CSD_CARD_TYPE_52 (1 << 1) /* Card can run at 52MHz */
  148. #define EXT_CSD_BUS_WIDTH_1 0 /* Card is in 1 bit mode */
  149. #define EXT_CSD_BUS_WIDTH_4 1 /* Card is in 4 bit mode */
  150. #define EXT_CSD_BUS_WIDTH_8 2 /* Card is in 8 bit mode */
  151. #define EXT_CSD_BOOT_ACK_ENABLE (1 << 6)
  152. #define EXT_CSD_BOOT_PARTITION_ENABLE (1 << 3)
  153. #define EXT_CSD_PARTITION_ACCESS_ENABLE (1 << 0)
  154. #define EXT_CSD_PARTITION_ACCESS_DISABLE (0 << 0)
  155. #define EXT_CSD_BOOT_ACK(x) (x << 6)
  156. #define EXT_CSD_BOOT_PART_NUM(x) (x << 3)
  157. #define EXT_CSD_PARTITION_ACCESS(x) (x << 0)
  158. #define R1_ILLEGAL_COMMAND (1 << 22)
  159. #define R1_APP_CMD (1 << 5)
  160. #define MMC_RSP_PRESENT (1 << 0)
  161. #define MMC_RSP_136 (1 << 1) /* 136 bit response */
  162. #define MMC_RSP_CRC (1 << 2) /* expect valid crc */
  163. #define MMC_RSP_BUSY (1 << 3) /* card may send busy */
  164. #define MMC_RSP_OPCODE (1 << 4) /* response contains opcode */
  165. #define MMC_RSP_NONE (0)
  166. #define MMC_RSP_R1 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
  167. #define MMC_RSP_R1b (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE| \
  168. MMC_RSP_BUSY)
  169. #define MMC_RSP_R2 (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC)
  170. #define MMC_RSP_R3 (MMC_RSP_PRESENT)
  171. #define MMC_RSP_R4 (MMC_RSP_PRESENT)
  172. #define MMC_RSP_R5 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
  173. #define MMC_RSP_R6 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
  174. #define MMC_RSP_R7 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
  175. #define MMCPART_NOAVAILABLE (0xff)
  176. #define PART_ACCESS_MASK (0x7)
  177. #define PART_SUPPORT (0x1)
  178. /* Maximum block size for MMC */
  179. #define MMC_MAX_BLOCK_LEN 512
  180. /* The number of MMC physical partitions. These consist of:
  181. * boot partitions (2), general purpose partitions (4) in MMC v4.4.
  182. */
  183. #define MMC_NUM_BOOT_PARTITION 2
  184. struct mmc_cid {
  185. unsigned long psn;
  186. unsigned short oid;
  187. unsigned char mid;
  188. unsigned char prv;
  189. unsigned char mdt;
  190. char pnm[7];
  191. };
  192. struct mmc_cmd {
  193. ushort cmdidx;
  194. uint resp_type;
  195. uint cmdarg;
  196. uint response[4];
  197. };
  198. struct mmc_data {
  199. union {
  200. char *dest;
  201. const char *src; /* src buffers don't get written to */
  202. };
  203. uint flags;
  204. uint blocks;
  205. uint blocksize;
  206. };
  207. struct mmc {
  208. struct list_head link;
  209. char name[32];
  210. void *priv;
  211. uint voltages;
  212. uint version;
  213. uint has_init;
  214. uint f_min;
  215. uint f_max;
  216. int high_capacity;
  217. uint bus_width;
  218. uint clock;
  219. uint card_caps;
  220. uint host_caps;
  221. uint ocr;
  222. uint scr[2];
  223. uint csd[4];
  224. uint cid[4];
  225. ushort rca;
  226. char part_config;
  227. char part_num;
  228. uint tran_speed;
  229. uint read_bl_len;
  230. uint write_bl_len;
  231. uint erase_grp_size;
  232. u64 capacity;
  233. u64 capacity_user;
  234. u64 capacity_boot;
  235. u64 capacity_rpmb;
  236. u64 capacity_gp[4];
  237. block_dev_desc_t block_dev;
  238. int (*send_cmd)(struct mmc *mmc,
  239. struct mmc_cmd *cmd, struct mmc_data *data);
  240. void (*set_ios)(struct mmc *mmc);
  241. int (*init)(struct mmc *mmc);
  242. int (*getcd)(struct mmc *mmc);
  243. int (*getwp)(struct mmc *mmc);
  244. uint b_max;
  245. char op_cond_pending; /* 1 if we are waiting on an op_cond command */
  246. char init_in_progress; /* 1 if we have done mmc_start_init() */
  247. char preinit; /* start init as early as possible */
  248. uint op_cond_response; /* the response byte from the last op_cond */
  249. };
  250. int mmc_register(struct mmc *mmc);
  251. int mmc_initialize(bd_t *bis);
  252. int mmc_init(struct mmc *mmc);
  253. int mmc_read(struct mmc *mmc, u64 src, uchar *dst, int size);
  254. void mmc_set_clock(struct mmc *mmc, uint clock);
  255. struct mmc *find_mmc_device(int dev_num);
  256. int mmc_set_dev(int dev_num);
  257. void print_mmc_devices(char separator);
  258. int get_mmc_num(void);
  259. int board_mmc_getcd(struct mmc *mmc);
  260. int mmc_switch_part(int dev_num, unsigned int part_num);
  261. int mmc_getcd(struct mmc *mmc);
  262. int mmc_getwp(struct mmc *mmc);
  263. void spl_mmc_load(void) __noreturn;
  264. /* Function to change the size of boot partition and rpmb partitions */
  265. int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
  266. unsigned long rpmbsize);
  267. /* Function to send commands to open/close the specified boot partition */
  268. int mmc_boot_part_access(struct mmc *mmc, u8 ack, u8 part_num, u8 access);
  269. /**
  270. * Start device initialization and return immediately; it does not block on
  271. * polling OCR (operation condition register) status. Then you should call
  272. * mmc_init, which would block on polling OCR status and complete the device
  273. * initializatin.
  274. *
  275. * @param mmc Pointer to a MMC device struct
  276. * @return 0 on success, IN_PROGRESS on waiting for OCR status, <0 on error.
  277. */
  278. int mmc_start_init(struct mmc *mmc);
  279. /**
  280. * Set preinit flag of mmc device.
  281. *
  282. * This will cause the device to be pre-inited during mmc_initialize(),
  283. * which may save boot time if the device is not accessed until later.
  284. * Some eMMC devices take 200-300ms to init, but unfortunately they
  285. * must be sent a series of commands to even get them to start preparing
  286. * for operation.
  287. *
  288. * @param mmc Pointer to a MMC device struct
  289. * @param preinit preinit flag value
  290. */
  291. void mmc_set_preinit(struct mmc *mmc, int preinit);
  292. #ifdef CONFIG_GENERIC_MMC
  293. #ifdef CONFIG_MMC_SPI
  294. #define mmc_host_is_spi(mmc) ((mmc)->host_caps & MMC_MODE_SPI)
  295. #else
  296. #define mmc_host_is_spi(mmc) 0
  297. #endif
  298. struct mmc *mmc_spi_init(uint bus, uint cs, uint speed, uint mode);
  299. #else
  300. int mmc_legacy_init(int verbose);
  301. #endif
  302. #endif /* _MMC_H_ */