mmc.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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/sizes.h>
  13. #include <linux/compiler.h>
  14. #include <part.h>
  15. /* SD/MMC version bits; 8 flags, 8 major, 8 minor, 8 change */
  16. #define SD_VERSION_SD (1U << 31)
  17. #define MMC_VERSION_MMC (1U << 30)
  18. #define MAKE_SDMMC_VERSION(a, b, c) \
  19. ((((u32)(a)) << 16) | ((u32)(b) << 8) | (u32)(c))
  20. #define MAKE_SD_VERSION(a, b, c) \
  21. (SD_VERSION_SD | MAKE_SDMMC_VERSION(a, b, c))
  22. #define MAKE_MMC_VERSION(a, b, c) \
  23. (MMC_VERSION_MMC | MAKE_SDMMC_VERSION(a, b, c))
  24. #define EXTRACT_SDMMC_MAJOR_VERSION(x) \
  25. (((u32)(x) >> 16) & 0xff)
  26. #define EXTRACT_SDMMC_MINOR_VERSION(x) \
  27. (((u32)(x) >> 8) & 0xff)
  28. #define EXTRACT_SDMMC_CHANGE_VERSION(x) \
  29. ((u32)(x) & 0xff)
  30. #define SD_VERSION_3 MAKE_SD_VERSION(3, 0, 0)
  31. #define SD_VERSION_2 MAKE_SD_VERSION(2, 0, 0)
  32. #define SD_VERSION_1_0 MAKE_SD_VERSION(1, 0, 0)
  33. #define SD_VERSION_1_10 MAKE_SD_VERSION(1, 10, 0)
  34. #define MMC_VERSION_UNKNOWN MAKE_MMC_VERSION(0, 0, 0)
  35. #define MMC_VERSION_1_2 MAKE_MMC_VERSION(1, 2, 0)
  36. #define MMC_VERSION_1_4 MAKE_MMC_VERSION(1, 4, 0)
  37. #define MMC_VERSION_2_2 MAKE_MMC_VERSION(2, 2, 0)
  38. #define MMC_VERSION_3 MAKE_MMC_VERSION(3, 0, 0)
  39. #define MMC_VERSION_4 MAKE_MMC_VERSION(4, 0, 0)
  40. #define MMC_VERSION_4_1 MAKE_MMC_VERSION(4, 1, 0)
  41. #define MMC_VERSION_4_2 MAKE_MMC_VERSION(4, 2, 0)
  42. #define MMC_VERSION_4_3 MAKE_MMC_VERSION(4, 3, 0)
  43. #define MMC_VERSION_4_41 MAKE_MMC_VERSION(4, 4, 1)
  44. #define MMC_VERSION_4_5 MAKE_MMC_VERSION(4, 5, 0)
  45. #define MMC_VERSION_5_0 MAKE_MMC_VERSION(5, 0, 0)
  46. #define MMC_VERSION_5_1 MAKE_MMC_VERSION(5, 1, 0)
  47. #define MMC_CAP(mode) (1 << mode)
  48. #define MMC_MODE_HS (MMC_CAP(MMC_HS) | MMC_CAP(SD_HS))
  49. #define MMC_MODE_HS_52MHz MMC_CAP(MMC_HS_52)
  50. #define MMC_MODE_DDR_52MHz MMC_CAP(MMC_DDR_52)
  51. #define MMC_MODE_8BIT BIT(30)
  52. #define MMC_MODE_4BIT BIT(29)
  53. #define MMC_MODE_1BIT BIT(28)
  54. #define MMC_MODE_SPI BIT(27)
  55. #define SD_DATA_4BIT 0x00040000
  56. #define IS_SD(x) ((x)->version & SD_VERSION_SD)
  57. #define IS_MMC(x) ((x)->version & MMC_VERSION_MMC)
  58. #define MMC_DATA_READ 1
  59. #define MMC_DATA_WRITE 2
  60. #define MMC_CMD_GO_IDLE_STATE 0
  61. #define MMC_CMD_SEND_OP_COND 1
  62. #define MMC_CMD_ALL_SEND_CID 2
  63. #define MMC_CMD_SET_RELATIVE_ADDR 3
  64. #define MMC_CMD_SET_DSR 4
  65. #define MMC_CMD_SWITCH 6
  66. #define MMC_CMD_SELECT_CARD 7
  67. #define MMC_CMD_SEND_EXT_CSD 8
  68. #define MMC_CMD_SEND_CSD 9
  69. #define MMC_CMD_SEND_CID 10
  70. #define MMC_CMD_STOP_TRANSMISSION 12
  71. #define MMC_CMD_SEND_STATUS 13
  72. #define MMC_CMD_SET_BLOCKLEN 16
  73. #define MMC_CMD_READ_SINGLE_BLOCK 17
  74. #define MMC_CMD_READ_MULTIPLE_BLOCK 18
  75. #define MMC_CMD_SET_BLOCK_COUNT 23
  76. #define MMC_CMD_WRITE_SINGLE_BLOCK 24
  77. #define MMC_CMD_WRITE_MULTIPLE_BLOCK 25
  78. #define MMC_CMD_ERASE_GROUP_START 35
  79. #define MMC_CMD_ERASE_GROUP_END 36
  80. #define MMC_CMD_ERASE 38
  81. #define MMC_CMD_APP_CMD 55
  82. #define MMC_CMD_SPI_READ_OCR 58
  83. #define MMC_CMD_SPI_CRC_ON_OFF 59
  84. #define MMC_CMD_RES_MAN 62
  85. #define MMC_CMD62_ARG1 0xefac62ec
  86. #define MMC_CMD62_ARG2 0xcbaea7
  87. #define SD_CMD_SEND_RELATIVE_ADDR 3
  88. #define SD_CMD_SWITCH_FUNC 6
  89. #define SD_CMD_SEND_IF_COND 8
  90. #define SD_CMD_SWITCH_UHS18V 11
  91. #define SD_CMD_APP_SET_BUS_WIDTH 6
  92. #define SD_CMD_APP_SD_STATUS 13
  93. #define SD_CMD_ERASE_WR_BLK_START 32
  94. #define SD_CMD_ERASE_WR_BLK_END 33
  95. #define SD_CMD_APP_SEND_OP_COND 41
  96. #define SD_CMD_APP_SEND_SCR 51
  97. /* SCR definitions in different words */
  98. #define SD_HIGHSPEED_BUSY 0x00020000
  99. #define SD_HIGHSPEED_SUPPORTED 0x00020000
  100. #define OCR_BUSY 0x80000000
  101. #define OCR_HCS 0x40000000
  102. #define OCR_VOLTAGE_MASK 0x007FFF80
  103. #define OCR_ACCESS_MODE 0x60000000
  104. #define MMC_ERASE_ARG 0x00000000
  105. #define MMC_SECURE_ERASE_ARG 0x80000000
  106. #define MMC_TRIM_ARG 0x00000001
  107. #define MMC_DISCARD_ARG 0x00000003
  108. #define MMC_SECURE_TRIM1_ARG 0x80000001
  109. #define MMC_SECURE_TRIM2_ARG 0x80008000
  110. #define MMC_STATUS_MASK (~0x0206BF7F)
  111. #define MMC_STATUS_SWITCH_ERROR (1 << 7)
  112. #define MMC_STATUS_RDY_FOR_DATA (1 << 8)
  113. #define MMC_STATUS_CURR_STATE (0xf << 9)
  114. #define MMC_STATUS_ERROR (1 << 19)
  115. #define MMC_STATE_PRG (7 << 9)
  116. #define MMC_VDD_165_195 0x00000080 /* VDD voltage 1.65 - 1.95 */
  117. #define MMC_VDD_20_21 0x00000100 /* VDD voltage 2.0 ~ 2.1 */
  118. #define MMC_VDD_21_22 0x00000200 /* VDD voltage 2.1 ~ 2.2 */
  119. #define MMC_VDD_22_23 0x00000400 /* VDD voltage 2.2 ~ 2.3 */
  120. #define MMC_VDD_23_24 0x00000800 /* VDD voltage 2.3 ~ 2.4 */
  121. #define MMC_VDD_24_25 0x00001000 /* VDD voltage 2.4 ~ 2.5 */
  122. #define MMC_VDD_25_26 0x00002000 /* VDD voltage 2.5 ~ 2.6 */
  123. #define MMC_VDD_26_27 0x00004000 /* VDD voltage 2.6 ~ 2.7 */
  124. #define MMC_VDD_27_28 0x00008000 /* VDD voltage 2.7 ~ 2.8 */
  125. #define MMC_VDD_28_29 0x00010000 /* VDD voltage 2.8 ~ 2.9 */
  126. #define MMC_VDD_29_30 0x00020000 /* VDD voltage 2.9 ~ 3.0 */
  127. #define MMC_VDD_30_31 0x00040000 /* VDD voltage 3.0 ~ 3.1 */
  128. #define MMC_VDD_31_32 0x00080000 /* VDD voltage 3.1 ~ 3.2 */
  129. #define MMC_VDD_32_33 0x00100000 /* VDD voltage 3.2 ~ 3.3 */
  130. #define MMC_VDD_33_34 0x00200000 /* VDD voltage 3.3 ~ 3.4 */
  131. #define MMC_VDD_34_35 0x00400000 /* VDD voltage 3.4 ~ 3.5 */
  132. #define MMC_VDD_35_36 0x00800000 /* VDD voltage 3.5 ~ 3.6 */
  133. #define MMC_SWITCH_MODE_CMD_SET 0x00 /* Change the command set */
  134. #define MMC_SWITCH_MODE_SET_BITS 0x01 /* Set bits in EXT_CSD byte
  135. addressed by index which are
  136. 1 in value field */
  137. #define MMC_SWITCH_MODE_CLEAR_BITS 0x02 /* Clear bits in EXT_CSD byte
  138. addressed by index, which are
  139. 1 in value field */
  140. #define MMC_SWITCH_MODE_WRITE_BYTE 0x03 /* Set target byte to value */
  141. #define SD_SWITCH_CHECK 0
  142. #define SD_SWITCH_SWITCH 1
  143. /*
  144. * EXT_CSD fields
  145. */
  146. #define EXT_CSD_ENH_START_ADDR 136 /* R/W */
  147. #define EXT_CSD_ENH_SIZE_MULT 140 /* R/W */
  148. #define EXT_CSD_GP_SIZE_MULT 143 /* R/W */
  149. #define EXT_CSD_PARTITION_SETTING 155 /* R/W */
  150. #define EXT_CSD_PARTITIONS_ATTRIBUTE 156 /* R/W */
  151. #define EXT_CSD_MAX_ENH_SIZE_MULT 157 /* R */
  152. #define EXT_CSD_PARTITIONING_SUPPORT 160 /* RO */
  153. #define EXT_CSD_RST_N_FUNCTION 162 /* R/W */
  154. #define EXT_CSD_BKOPS_EN 163 /* R/W & R/W/E */
  155. #define EXT_CSD_WR_REL_PARAM 166 /* R */
  156. #define EXT_CSD_WR_REL_SET 167 /* R/W */
  157. #define EXT_CSD_RPMB_MULT 168 /* RO */
  158. #define EXT_CSD_ERASE_GROUP_DEF 175 /* R/W */
  159. #define EXT_CSD_BOOT_BUS_WIDTH 177
  160. #define EXT_CSD_PART_CONF 179 /* R/W */
  161. #define EXT_CSD_BUS_WIDTH 183 /* R/W */
  162. #define EXT_CSD_HS_TIMING 185 /* R/W */
  163. #define EXT_CSD_REV 192 /* RO */
  164. #define EXT_CSD_CARD_TYPE 196 /* RO */
  165. #define EXT_CSD_SEC_CNT 212 /* RO, 4 bytes */
  166. #define EXT_CSD_HC_WP_GRP_SIZE 221 /* RO */
  167. #define EXT_CSD_HC_ERASE_GRP_SIZE 224 /* RO */
  168. #define EXT_CSD_BOOT_MULT 226 /* RO */
  169. #define EXT_CSD_BKOPS_SUPPORT 502 /* RO */
  170. /*
  171. * EXT_CSD field definitions
  172. */
  173. #define EXT_CSD_CMD_SET_NORMAL (1 << 0)
  174. #define EXT_CSD_CMD_SET_SECURE (1 << 1)
  175. #define EXT_CSD_CMD_SET_CPSECURE (1 << 2)
  176. #define EXT_CSD_CARD_TYPE_26 (1 << 0) /* Card can run at 26MHz */
  177. #define EXT_CSD_CARD_TYPE_52 (1 << 1) /* Card can run at 52MHz */
  178. #define EXT_CSD_CARD_TYPE_DDR_1_8V (1 << 2)
  179. #define EXT_CSD_CARD_TYPE_DDR_1_2V (1 << 3)
  180. #define EXT_CSD_CARD_TYPE_DDR_52 (EXT_CSD_CARD_TYPE_DDR_1_8V \
  181. | EXT_CSD_CARD_TYPE_DDR_1_2V)
  182. #define EXT_CSD_BUS_WIDTH_1 0 /* Card is in 1 bit mode */
  183. #define EXT_CSD_BUS_WIDTH_4 1 /* Card is in 4 bit mode */
  184. #define EXT_CSD_BUS_WIDTH_8 2 /* Card is in 8 bit mode */
  185. #define EXT_CSD_DDR_BUS_WIDTH_4 5 /* Card is in 4 bit DDR mode */
  186. #define EXT_CSD_DDR_BUS_WIDTH_8 6 /* Card is in 8 bit DDR mode */
  187. #define EXT_CSD_DDR_FLAG BIT(2) /* Flag for DDR mode */
  188. #define EXT_CSD_TIMING_LEGACY 0 /* no high speed */
  189. #define EXT_CSD_TIMING_HS 1 /* HS */
  190. #define EXT_CSD_BOOT_ACK_ENABLE (1 << 6)
  191. #define EXT_CSD_BOOT_PARTITION_ENABLE (1 << 3)
  192. #define EXT_CSD_PARTITION_ACCESS_ENABLE (1 << 0)
  193. #define EXT_CSD_PARTITION_ACCESS_DISABLE (0 << 0)
  194. #define EXT_CSD_BOOT_ACK(x) (x << 6)
  195. #define EXT_CSD_BOOT_PART_NUM(x) (x << 3)
  196. #define EXT_CSD_PARTITION_ACCESS(x) (x << 0)
  197. #define EXT_CSD_EXTRACT_BOOT_ACK(x) (((x) >> 6) & 0x1)
  198. #define EXT_CSD_EXTRACT_BOOT_PART(x) (((x) >> 3) & 0x7)
  199. #define EXT_CSD_EXTRACT_PARTITION_ACCESS(x) ((x) & 0x7)
  200. #define EXT_CSD_BOOT_BUS_WIDTH_MODE(x) (x << 3)
  201. #define EXT_CSD_BOOT_BUS_WIDTH_RESET(x) (x << 2)
  202. #define EXT_CSD_BOOT_BUS_WIDTH_WIDTH(x) (x)
  203. #define EXT_CSD_PARTITION_SETTING_COMPLETED (1 << 0)
  204. #define EXT_CSD_ENH_USR (1 << 0) /* user data area is enhanced */
  205. #define EXT_CSD_ENH_GP(x) (1 << ((x)+1)) /* GP part (x+1) is enhanced */
  206. #define EXT_CSD_HS_CTRL_REL (1 << 0) /* host controlled WR_REL_SET */
  207. #define EXT_CSD_WR_DATA_REL_USR (1 << 0) /* user data area WR_REL */
  208. #define EXT_CSD_WR_DATA_REL_GP(x) (1 << ((x)+1)) /* GP part (x+1) WR_REL */
  209. #define R1_ILLEGAL_COMMAND (1 << 22)
  210. #define R1_APP_CMD (1 << 5)
  211. #define MMC_RSP_PRESENT (1 << 0)
  212. #define MMC_RSP_136 (1 << 1) /* 136 bit response */
  213. #define MMC_RSP_CRC (1 << 2) /* expect valid crc */
  214. #define MMC_RSP_BUSY (1 << 3) /* card may send busy */
  215. #define MMC_RSP_OPCODE (1 << 4) /* response contains opcode */
  216. #define MMC_RSP_NONE (0)
  217. #define MMC_RSP_R1 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
  218. #define MMC_RSP_R1b (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE| \
  219. MMC_RSP_BUSY)
  220. #define MMC_RSP_R2 (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC)
  221. #define MMC_RSP_R3 (MMC_RSP_PRESENT)
  222. #define MMC_RSP_R4 (MMC_RSP_PRESENT)
  223. #define MMC_RSP_R5 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
  224. #define MMC_RSP_R6 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
  225. #define MMC_RSP_R7 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
  226. #define MMCPART_NOAVAILABLE (0xff)
  227. #define PART_ACCESS_MASK (0x7)
  228. #define PART_SUPPORT (0x1)
  229. #define ENHNCD_SUPPORT (0x2)
  230. #define PART_ENH_ATTRIB (0x1f)
  231. enum mmc_voltage {
  232. MMC_SIGNAL_VOLTAGE_000 = 0,
  233. MMC_SIGNAL_VOLTAGE_120,
  234. MMC_SIGNAL_VOLTAGE_180,
  235. MMC_SIGNAL_VOLTAGE_330
  236. };
  237. /* Maximum block size for MMC */
  238. #define MMC_MAX_BLOCK_LEN 512
  239. /* The number of MMC physical partitions. These consist of:
  240. * boot partitions (2), general purpose partitions (4) in MMC v4.4.
  241. */
  242. #define MMC_NUM_BOOT_PARTITION 2
  243. #define MMC_PART_RPMB 3 /* RPMB partition number */
  244. /* Driver model support */
  245. /**
  246. * struct mmc_uclass_priv - Holds information about a device used by the uclass
  247. */
  248. struct mmc_uclass_priv {
  249. struct mmc *mmc;
  250. };
  251. /**
  252. * mmc_get_mmc_dev() - get the MMC struct pointer for a device
  253. *
  254. * Provided that the device is already probed and ready for use, this value
  255. * will be available.
  256. *
  257. * @dev: Device
  258. * @return associated mmc struct pointer if available, else NULL
  259. */
  260. struct mmc *mmc_get_mmc_dev(struct udevice *dev);
  261. /* End of driver model support */
  262. struct mmc_cid {
  263. unsigned long psn;
  264. unsigned short oid;
  265. unsigned char mid;
  266. unsigned char prv;
  267. unsigned char mdt;
  268. char pnm[7];
  269. };
  270. struct mmc_cmd {
  271. ushort cmdidx;
  272. uint resp_type;
  273. uint cmdarg;
  274. uint response[4];
  275. };
  276. struct mmc_data {
  277. union {
  278. char *dest;
  279. const char *src; /* src buffers don't get written to */
  280. };
  281. uint flags;
  282. uint blocks;
  283. uint blocksize;
  284. };
  285. /* forward decl. */
  286. struct mmc;
  287. #if CONFIG_IS_ENABLED(DM_MMC)
  288. struct dm_mmc_ops {
  289. /**
  290. * send_cmd() - Send a command to the MMC device
  291. *
  292. * @dev: Device to receive the command
  293. * @cmd: Command to send
  294. * @data: Additional data to send/receive
  295. * @return 0 if OK, -ve on error
  296. */
  297. int (*send_cmd)(struct udevice *dev, struct mmc_cmd *cmd,
  298. struct mmc_data *data);
  299. /**
  300. * set_ios() - Set the I/O speed/width for an MMC device
  301. *
  302. * @dev: Device to update
  303. * @return 0 if OK, -ve on error
  304. */
  305. int (*set_ios)(struct udevice *dev);
  306. /**
  307. * send_init_stream() - send the initialization stream: 74 clock cycles
  308. * This is used after power up before sending the first command
  309. *
  310. * @dev: Device to update
  311. */
  312. void (*send_init_stream)(struct udevice *dev);
  313. /**
  314. * get_cd() - See whether a card is present
  315. *
  316. * @dev: Device to check
  317. * @return 0 if not present, 1 if present, -ve on error
  318. */
  319. int (*get_cd)(struct udevice *dev);
  320. /**
  321. * get_wp() - See whether a card has write-protect enabled
  322. *
  323. * @dev: Device to check
  324. * @return 0 if write-enabled, 1 if write-protected, -ve on error
  325. */
  326. int (*get_wp)(struct udevice *dev);
  327. };
  328. #define mmc_get_ops(dev) ((struct dm_mmc_ops *)(dev)->driver->ops)
  329. int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
  330. struct mmc_data *data);
  331. int dm_mmc_set_ios(struct udevice *dev);
  332. void dm_mmc_send_init_stream(struct udevice *dev);
  333. int dm_mmc_get_cd(struct udevice *dev);
  334. int dm_mmc_get_wp(struct udevice *dev);
  335. /* Transition functions for compatibility */
  336. int mmc_set_ios(struct mmc *mmc);
  337. void mmc_send_init_stream(struct mmc *mmc);
  338. int mmc_getcd(struct mmc *mmc);
  339. int mmc_getwp(struct mmc *mmc);
  340. #else
  341. struct mmc_ops {
  342. int (*send_cmd)(struct mmc *mmc,
  343. struct mmc_cmd *cmd, struct mmc_data *data);
  344. int (*set_ios)(struct mmc *mmc);
  345. int (*init)(struct mmc *mmc);
  346. int (*getcd)(struct mmc *mmc);
  347. int (*getwp)(struct mmc *mmc);
  348. };
  349. #endif
  350. struct mmc_config {
  351. const char *name;
  352. #if !CONFIG_IS_ENABLED(DM_MMC)
  353. const struct mmc_ops *ops;
  354. #endif
  355. uint host_caps;
  356. uint voltages;
  357. uint f_min;
  358. uint f_max;
  359. uint b_max;
  360. unsigned char part_type;
  361. };
  362. struct sd_ssr {
  363. unsigned int au; /* In sectors */
  364. unsigned int erase_timeout; /* In milliseconds */
  365. unsigned int erase_offset; /* In milliseconds */
  366. };
  367. enum bus_mode {
  368. MMC_LEGACY,
  369. SD_LEGACY,
  370. MMC_HS,
  371. SD_HS,
  372. UHS_SDR12,
  373. UHS_SDR25,
  374. UHS_SDR50,
  375. UHS_SDR104,
  376. UHS_DDR50,
  377. MMC_HS_52,
  378. MMC_DDR_52,
  379. MMC_HS_200,
  380. MMC_MODES_END
  381. };
  382. const char *mmc_mode_name(enum bus_mode mode);
  383. void mmc_dump_capabilities(const char *text, uint caps);
  384. static inline bool mmc_is_mode_ddr(enum bus_mode mode)
  385. {
  386. if ((mode == MMC_DDR_52) || (mode == UHS_DDR50))
  387. return true;
  388. else
  389. return false;
  390. }
  391. /*
  392. * With CONFIG_DM_MMC enabled, struct mmc can be accessed from the MMC device
  393. * with mmc_get_mmc_dev().
  394. *
  395. * TODO struct mmc should be in mmc_private but it's hard to fix right now
  396. */
  397. struct mmc {
  398. #if !CONFIG_IS_ENABLED(BLK)
  399. struct list_head link;
  400. #endif
  401. const struct mmc_config *cfg; /* provided configuration */
  402. uint version;
  403. void *priv;
  404. uint has_init;
  405. int high_capacity;
  406. uint bus_width;
  407. uint clock;
  408. enum mmc_voltage signal_voltage;
  409. uint card_caps;
  410. uint ocr;
  411. uint dsr;
  412. uint dsr_imp;
  413. uint scr[2];
  414. uint csd[4];
  415. uint cid[4];
  416. ushort rca;
  417. u8 part_support;
  418. u8 part_attr;
  419. u8 wr_rel_set;
  420. u8 part_config;
  421. uint tran_speed;
  422. uint legacy_speed; /* speed for the legacy mode provided by the card */
  423. uint read_bl_len;
  424. uint write_bl_len;
  425. uint erase_grp_size; /* in 512-byte sectors */
  426. uint hc_wp_grp_size; /* in 512-byte sectors */
  427. struct sd_ssr ssr; /* SD status register */
  428. u64 capacity;
  429. u64 capacity_user;
  430. u64 capacity_boot;
  431. u64 capacity_rpmb;
  432. u64 capacity_gp[4];
  433. u64 enh_user_start;
  434. u64 enh_user_size;
  435. #if !CONFIG_IS_ENABLED(BLK)
  436. struct blk_desc block_dev;
  437. #endif
  438. char op_cond_pending; /* 1 if we are waiting on an op_cond command */
  439. char init_in_progress; /* 1 if we have done mmc_start_init() */
  440. char preinit; /* start init as early as possible */
  441. int ddr_mode;
  442. #if CONFIG_IS_ENABLED(DM_MMC)
  443. struct udevice *dev; /* Device for this MMC controller */
  444. #if CONFIG_IS_ENABLED(DM_REGULATOR)
  445. struct udevice *vmmc_supply; /* Main voltage regulator (Vcc)*/
  446. struct udevice *vqmmc_supply; /* IO voltage regulator (Vccq)*/
  447. #endif
  448. #endif
  449. u8 *ext_csd;
  450. enum bus_mode selected_mode;
  451. };
  452. struct mmc_hwpart_conf {
  453. struct {
  454. uint enh_start; /* in 512-byte sectors */
  455. uint enh_size; /* in 512-byte sectors, if 0 no enh area */
  456. unsigned wr_rel_change : 1;
  457. unsigned wr_rel_set : 1;
  458. } user;
  459. struct {
  460. uint size; /* in 512-byte sectors */
  461. unsigned enhanced : 1;
  462. unsigned wr_rel_change : 1;
  463. unsigned wr_rel_set : 1;
  464. } gp_part[4];
  465. };
  466. enum mmc_hwpart_conf_mode {
  467. MMC_HWPART_CONF_CHECK,
  468. MMC_HWPART_CONF_SET,
  469. MMC_HWPART_CONF_COMPLETE,
  470. };
  471. struct mmc *mmc_create(const struct mmc_config *cfg, void *priv);
  472. /**
  473. * mmc_bind() - Set up a new MMC device ready for probing
  474. *
  475. * A child block device is bound with the IF_TYPE_MMC interface type. This
  476. * allows the device to be used with CONFIG_BLK
  477. *
  478. * @dev: MMC device to set up
  479. * @mmc: MMC struct
  480. * @cfg: MMC configuration
  481. * @return 0 if OK, -ve on error
  482. */
  483. int mmc_bind(struct udevice *dev, struct mmc *mmc,
  484. const struct mmc_config *cfg);
  485. void mmc_destroy(struct mmc *mmc);
  486. /**
  487. * mmc_unbind() - Unbind a MMC device's child block device
  488. *
  489. * @dev: MMC device
  490. * @return 0 if OK, -ve on error
  491. */
  492. int mmc_unbind(struct udevice *dev);
  493. int mmc_initialize(bd_t *bis);
  494. int mmc_init(struct mmc *mmc);
  495. int mmc_read(struct mmc *mmc, u64 src, uchar *dst, int size);
  496. int mmc_set_clock(struct mmc *mmc, uint clock);
  497. struct mmc *find_mmc_device(int dev_num);
  498. int mmc_set_dev(int dev_num);
  499. void print_mmc_devices(char separator);
  500. /**
  501. * get_mmc_num() - get the total MMC device number
  502. *
  503. * @return 0 if there is no MMC device, else the number of devices
  504. */
  505. int get_mmc_num(void);
  506. int mmc_switch_part(struct mmc *mmc, unsigned int part_num);
  507. int mmc_hwpart_config(struct mmc *mmc, const struct mmc_hwpart_conf *conf,
  508. enum mmc_hwpart_conf_mode mode);
  509. #if !CONFIG_IS_ENABLED(DM_MMC)
  510. int mmc_getcd(struct mmc *mmc);
  511. int board_mmc_getcd(struct mmc *mmc);
  512. int mmc_getwp(struct mmc *mmc);
  513. int board_mmc_getwp(struct mmc *mmc);
  514. #endif
  515. int mmc_set_dsr(struct mmc *mmc, u16 val);
  516. /* Function to change the size of boot partition and rpmb partitions */
  517. int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
  518. unsigned long rpmbsize);
  519. /* Function to modify the PARTITION_CONFIG field of EXT_CSD */
  520. int mmc_set_part_conf(struct mmc *mmc, u8 ack, u8 part_num, u8 access);
  521. /* Function to modify the BOOT_BUS_WIDTH field of EXT_CSD */
  522. int mmc_set_boot_bus_width(struct mmc *mmc, u8 width, u8 reset, u8 mode);
  523. /* Function to modify the RST_n_FUNCTION field of EXT_CSD */
  524. int mmc_set_rst_n_function(struct mmc *mmc, u8 enable);
  525. /* Functions to read / write the RPMB partition */
  526. int mmc_rpmb_set_key(struct mmc *mmc, void *key);
  527. int mmc_rpmb_get_counter(struct mmc *mmc, unsigned long *counter);
  528. int mmc_rpmb_read(struct mmc *mmc, void *addr, unsigned short blk,
  529. unsigned short cnt, unsigned char *key);
  530. int mmc_rpmb_write(struct mmc *mmc, void *addr, unsigned short blk,
  531. unsigned short cnt, unsigned char *key);
  532. #ifdef CONFIG_CMD_BKOPS_ENABLE
  533. int mmc_set_bkops_enable(struct mmc *mmc);
  534. #endif
  535. /**
  536. * Start device initialization and return immediately; it does not block on
  537. * polling OCR (operation condition register) status. Then you should call
  538. * mmc_init, which would block on polling OCR status and complete the device
  539. * initializatin.
  540. *
  541. * @param mmc Pointer to a MMC device struct
  542. * @return 0 on success, IN_PROGRESS on waiting for OCR status, <0 on error.
  543. */
  544. int mmc_start_init(struct mmc *mmc);
  545. /**
  546. * Set preinit flag of mmc device.
  547. *
  548. * This will cause the device to be pre-inited during mmc_initialize(),
  549. * which may save boot time if the device is not accessed until later.
  550. * Some eMMC devices take 200-300ms to init, but unfortunately they
  551. * must be sent a series of commands to even get them to start preparing
  552. * for operation.
  553. *
  554. * @param mmc Pointer to a MMC device struct
  555. * @param preinit preinit flag value
  556. */
  557. void mmc_set_preinit(struct mmc *mmc, int preinit);
  558. #ifdef CONFIG_MMC_SPI
  559. #define mmc_host_is_spi(mmc) ((mmc)->cfg->host_caps & MMC_MODE_SPI)
  560. #else
  561. #define mmc_host_is_spi(mmc) 0
  562. #endif
  563. struct mmc *mmc_spi_init(uint bus, uint cs, uint speed, uint mode);
  564. void board_mmc_power_init(void);
  565. int board_mmc_init(bd_t *bis);
  566. int cpu_mmc_init(bd_t *bis);
  567. int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr);
  568. int mmc_get_env_dev(void);
  569. /* Set block count limit because of 16 bit register limit on some hardware*/
  570. #ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT
  571. #define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535
  572. #endif
  573. /**
  574. * mmc_get_blk_desc() - Get the block descriptor for an MMC device
  575. *
  576. * @mmc: MMC device
  577. * @return block device if found, else NULL
  578. */
  579. struct blk_desc *mmc_get_blk_desc(struct mmc *mmc);
  580. #endif /* _MMC_H_ */