1 //*****************************************************************************
2 //
3 //! @file am_hal_card.h
4 //!
5 //! @brief Functions for interfacing with the card host.
6 //!
7 //! @addtogroup card_4p Card Functionality for SD/MMC/eMMC/SDIO
8 //! @ingroup apollo4p_hal
9 //! @{
10 //
11 //*****************************************************************************
12
13 //*****************************************************************************
14 //
15 // Copyright (c) 2023, Ambiq Micro, Inc.
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are met:
20 //
21 // 1. Redistributions of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimer.
23 //
24 // 2. Redistributions in binary form must reproduce the above copyright
25 // notice, this list of conditions and the following disclaimer in the
26 // documentation and/or other materials provided with the distribution.
27 //
28 // 3. Neither the name of the copyright holder nor the names of its
29 // contributors may be used to endorse or promote products derived from this
30 // software without specific prior written permission.
31 //
32 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
36 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 // POSSIBILITY OF SUCH DAMAGE.
43 //
44 // This is part of revision release_sdk_4_4_0-3c5977e664 of the AmbiqSuite Development Package.
45 //
46 //*****************************************************************************
47 #ifndef AM_HAL_CARD_H
48 #define AM_HAL_CARD_H
49
50 #ifdef __cplusplus
51 extern "C"
52 {
53 #endif
54
55 //
56 // SD/MMC(eMMC)/SDIO protocol HAL driver APIs
57 //
58
59 #define MMC_CMD_GO_IDLE_STATE 0
60 #define MMC_CMD_SEND_OP_COND 1
61 #define MMC_CMD_ALL_SEND_CID 2
62 #define MMC_CMD_SET_RELATIVE_ADDR 3
63 #define MMC_CMD_SET_DSR 4
64 #define MMC_CMD_SLEEP_AWAKE 5
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_READ_TUNING_BLOCK 14
73 #define MMC_CMD_SET_BLOCKLEN 16
74 #define MMC_CMD_READ_SINGLE_BLOCK 17
75 #define MMC_CMD_READ_MULTIPLE_BLOCK 18
76 #define MMC_CMD_SEND_TUNING_BLOCK 19
77 #define MMC_CMD_SEND_TUNING_BLOCK_HS200 21
78 #define MMC_CMD_SET_BLOCK_COUNT 23
79 #define MMC_CMD_WRITE_SINGLE_BLOCK 24
80 #define MMC_CMD_WRITE_MULTIPLE_BLOCK 25
81 #define MMC_CMD_ERASE_GROUP_START 35
82 #define MMC_CMD_ERASE_GROUP_END 36
83 #define MMC_CMD_ERASE 38
84 #define MMC_CMD_APP_CMD 55
85 #define MMC_CMD_CMD56 56
86 #define MMC_CMD_SPI_READ_OCR 58
87 #define MMC_CMD_SPI_CRC_ON_OFF 59
88 #define MMC_CMD_RES_MAN 62
89
90 #define MMC_RSP_PRESENT (1 << 0)
91 #define MMC_RSP_136 (1 << 1) /* 136 bit response */
92 #define MMC_RSP_CRC (1 << 2) /* expect valid crc */
93 #define MMC_RSP_BUSY (1 << 3) /* card may send busy */
94 #define MMC_RSP_OPCODE (1 << 4) /* response contains opcode */
95
96 #define MMC_RSP_NONE (0)
97 #define MMC_RSP_R1 (MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE)
98 #define MMC_RSP_R1b (MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE | MMC_RSP_BUSY)
99 #define MMC_RSP_R2 (MMC_RSP_PRESENT | MMC_RSP_136 | MMC_RSP_CRC)
100 #define MMC_RSP_R3 (MMC_RSP_PRESENT)
101 #define MMC_RSP_R4 (MMC_RSP_PRESENT)
102 #define MMC_RSP_R5 (MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE)
103 #define MMC_RSP_R6 (MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE)
104 #define MMC_RSP_R7 (MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE)
105
106 //
107 // defines for EXT_CSD register in MMC HIGH SPEED cards
108 //
109 // Valid POWER_OFF_NOTIFICATION values
110 #define MMC_EXT_CSD_NO_POWER_NOTIFICATION (0x00)
111 #define MMC_EXT_CSD_POWERED_ON (0x01)
112 #define MMC_EXT_CSD_POWER_OFF_SHORT (0x02)
113 #define MMC_EXT_CSD_POWER_OFF_LONG (0x03)
114 #define MMC_EXT_CSD_SLEEP_NOTIFICATION (0x04)
115
116 //
117 // Supported Bus Width Types
118 //
119 #define SD_BUS_WIDTH1 0
120 #define SD_BUS_WIDTH4 1
121 #define SD_BUS_WIDTH8 2
122 #define SD_BUS_WIDTH4_DDR 5
123 #define SD_BUS_WIDTH8_DDR 6
124
125 #define MMC_EXT_MODE_COMMAND_SET (0 << 24)
126 #define MMC_EXT_MODE_SET_BIT (1 << 24)
127 #define MMC_EXT_MODE_CLEAR_BITS (2 << 24)
128 #define MMC_EXT_MODE_WRITE_BYTE (3 << 24)
129
130 // Added for MMC4.3
131 #define eMMC_BGA_TYPE 0x01
132
133 #define MMC_EXT_REGS_CMD_SET (191 << 16)
134 #define MMC_EXT_REGS_POWER_CLASS (187 << 16)
135 #define MMC_EXT_REGS_HIGH_SPEED (185 << 16)
136 #define MMC_EXT_REGS_BUS_WIDTH (183 << 16)
137 #define MMC_EXT_REGS_BOOT_CONFIG (179 << 16)
138 #define MMC_EXT_REGS_BOOT_GONFIGPROT (178 << 16)
139 #define MMC_EXT_REGS_BOOT_BUSWIDTH (177 << 16)
140 #define MMC_EXT_REGS_ERASEGRP_DEFN (175 << 16)
141 #define MMC_EXT_REGS_BOOT_WP (173 << 16)
142 #define MMC_EXT_REGS_USR_WP (171 << 16)
143 #define MMC_EXT_REGS_FW_CONFIG (169 << 16)
144 #define MMC_EXT_REGS_SANITIZE_START 165
145 #define MMC_EXT_REGS_BKOPS_START 164
146 #define MMC_EXT_REGS_BKOPS_ENABLE 163
147 #define MMC_EXT_REGS_RST_FUNC (162 << 16)
148 #define MMC_EXT_REGS_HPI_MGMT 161
149 #define MMC_EXT_REGS_PART_ATTRB (156 << 16)
150 #define MMC_EXT_REGS_PART_COMPLETE (155 << 16)
151 #define MMC_EXT_REGS_GP_SIZE_MULT_4_2 (154 << 16)
152 #define MMC_EXT_REGS_GP_SIZE_MULT_4_1 (153 << 16)
153 #define MMC_EXT_REGS_GP_SIZE_MULT_4_0 (152 << 16)
154 #define MMC_EXT_REGS_GP_SIZE_MULT_3_2 (151 << 16)
155 #define MMC_EXT_REGS_GP_SIZE_MULT_3_1 (150 << 16)
156 #define MMC_EXT_REGS_GP_SIZE_MULT_3_0 (149 << 16)
157 #define MMC_EXT_REGS_GP_SIZE_MULT_2_2 (148 << 16)
158 #define MMC_EXT_REGS_GP_SIZE_MULT_2_1 (147 << 16)
159 #define MMC_EXT_REGS_GP_SIZE_MULT_2_0 (146 << 16)
160 #define MMC_EXT_REGS_GP_SIZE_MULT_1_2 (145 << 16)
161 #define MMC_EXT_REGS_GP_SIZE_MULT_1_1 (144 << 16)
162 #define MMC_EXT_REGS_GP_SIZE_MULT_1_0 (143 << 16)
163 #define MMC_EXT_REGS_ENH_SIZE_MULT2 (142 << 16)
164 #define MMC_EXT_REGS_ENH_SIZE_MULT1 (141 << 16)
165 #define MMC_EXT_REGS_ENH_SIZE_MULT0 (140 << 16)
166 #define MMC_EXT_REGS_ENH_START_ADDR (136 << 16)
167 #define MMC_EXT_REGS_EXCEPTION_EVTS_CTRL (56 << 16)
168 #define MMC_EXT_REGS_CONTEXT_CONF 37
169 #define MMC_EXT_REGS_PWR_OFF_NOTIFY 34
170 #define MMC_EXT_REGS_CACHE_CTRL 33
171 #define MMC_EXT_REGS_FLUSH_CACHE 32
172 #define MMC_EXT_REGS_HS_TIMING 185
173 #define MMC_EXT_REGS_PARTITON_CONFIG 179
174 #define MMC_EXT_REGS_RPMB_SIZE_MULT 168
175
176 #define MMC_EXP_PACKED_EVENT_EN 0x8
177 #define MMC_EXP_SYSPOOL_EVENT_EN 0x4
178 #define MMC_EXP_DYN_CAP_EVENT_EN 0x2
179 #define MMC_EXT_SEC_SANITIZE (0x1 << 6)
180
181 #define MMC_EXT_SET_BUS_WIDTH1 (SD_BUS_WIDTH1 << 8)
182 #define MMC_EXT_SET_BUS_WIDTH4 (SD_BUS_WIDTH4 << 8)
183 #define MMC_EXT_SET_BUS_WIDTH8 (SD_BUS_WIDTH8 << 8)
184 #define MMC_EXT_SET_BUS_WIDTH4_DDR (SD_BUS_WIDTH4_DDR << 8)
185 #define MMC_EXT_SET_BUS_WIDTH8_DDR (SD_BUS_WIDTH8_DDR << 8)
186
187 #define MMC_EXT_SET_HIGH_SPEED (1 << 8)
188 #define MMC_EXT_SET_CMD_ATA (1 << 4)
189
190 // Added for MMC4.3
191
192 // CARD TYPE FLAGS
193 #define HS_26MHZ 0x1
194 #define HS_52MHZ 0x2
195 #define HS_DDR_52MHZ_18_3V 0x4
196 #define HS_DDR_52MHZ_12V 0x4
197 #define HS200_200MHZ_18V 0x10
198 #define HS200_200MHZ_12V 0x20
199 #define HS200_TIMING 0x02
200
201 // BOOT ACK FLAGS
202 #define eMMC_BOOT_ACK_EN 0x01
203 #define eMMC_NO_BOOT_ACK 0x00
204
205 // BOOT PARTITION ENABLE FLAGS
206 #define eMMC_NO_BOOT_EN 0x00
207 #define eMMC_BOOT_PART1_EN 0x01
208 #define eMMC_BOOT_PART2_EN 0x02
209 #define eMMC_BOOT_USER_EN 0x07
210
211 // BOOT PARTITION ACCESS FLAGS
212 #define eMMC_NO_BOOT_ACCESS 0x00
213 #define eMMC_BOOT1_ACCESS 0x01
214 #define eMMC_BOOT2_ACCESS 0x02
215 #define eMMC_RPMB_ACCESS 0x03
216 #define eMMC_GPP1_ACCESS 0x04
217 #define eMMC_GPP2_ACCESS 0x05
218 #define eMMC_GPP3_ACCESS 0x06
219 #define eMMC_GPP4_ACCESS 0x07
220
221 // BOOT BUS WIDTH FLAGS
222 #define eMMC_RESET_BUSWDITH 0x00
223 #define eMMC_RETAIN_BUSWDITH 0x01
224
225 // BOOT BUS WIDTH MODES
226 #define SDR_BCKWARD_COMP_BOOT_MOD 0x00
227 #define SDR_HS_TIMING_BOOT_MODE 0x01
228 #define DDR_BOOT_MODE 0x02
229
230 // BOOT BUS WIDTHS
231 #define eMMC_BOOT_BUSWIDTH1 0x00
232 #define eMMC_BOOT_BUSWIDTH4 0x01
233 #define eMMC_BOOT_BUSWIDTH8 0x02
234
235 // Boot Config Protection Flags
236 #define PERM_BOOT_CONFIG_PROT_EN 0x01
237 #define PERM_BOOT_CONFIG_PROT_DIS 0x00
238 #define PWR_BOOT_CONFIG_PROT_EN 0x01
239 #define PWR_BOOT_CONFIG_PROT_DIS 0x00
240
241 // Boot Write Protection Flags
242 #define BOOT_PWR_WP_DIS 0x01
243 #define BOOT_PWR_WP_USE 0x00
244 #define BOOT_PERM_WP_DIS 0x01
245 #define BOOT_PERM_WP_USE 0x00
246 #define BOOT_PERM_WP_EN 0x01
247 #define BOOT_PERM_NO_WP 0x00
248 #define BOOT_PWR_WP_EN 0x01
249 #define BOOT_PWR_NO_WP 0x00
250
251 // User Write Protection Flags
252 #define PERM_PSWD_DIS 0x1
253 #define PERM_PSWD_EN 0x0
254 #define CD_PERM_WP_DIS 0x1
255 #define CD_PERM_WP_EN 0x0
256 #define US_PERM_WP_GRP_DIS 0x1
257 #define US_PERM_WP_GRP_EN 0x0
258 #define US_PWR_WP_GRP_DIS 0x1
259 #define US_PWR_WP_GRP_EN 0x0
260 #define US_PERM_WP_CMD28GRP_EN 0x1
261 #define US_PERM_WP_CMD28GRP_DIS 0x0
262 #define US_PWR_WP_CMD28GRP_EN 0x1
263 #define US_PWR_WP_CMD28GRP_DIS 0x0
264
265 // FW update Flags
266 #define FW_UPDATE_EN 0x0
267 #define FW_UPDATE_DIS 0x1
268
269 // Reset Function Enable
270 #define RST_N_TEMP_DIS 0x0
271 #define RST_N_PERM_EN 0x1
272 #define RST_N_PERM_DIS 0x2
273
274 // Partition Support Flags
275 #define ENH_ATTRIBUTE_EN 0x1
276 #define ENH_ATTRIBUTE_DIS 0x0
277 #define PARTITIONING_EN 0x1
278 #define PARTITIONING_DIS 0x0
279
280 // Partition Atrribute Features
281 #define SET_ENH_USR 0x1
282 #define SET_ENH_GPP1 0x2
283 #define SET_ENH_GPP2 0x4
284 #define SET_ENH_GPP3 0x8
285 #define SET_ENH_GPP4 0x10
286 #define SET_ALL_PART_ATTRB 0x1F
287 #define SET_ALL_GPP_PART_ATTRB 0x1E
288
289 // Partition Completion Flags
290 #define PARTITION_COMPLETE 0x1
291
292 // Ext CSD Read Flags
293 #define HS_BOOT_MODE_SUPPORT 0x1
294 #define DDR_BOOT_MODE_SUPPORT (1 << 1)
295 #define ALT_BOOT_MODE_SUPPORT (1 << 2)
296 #define SEC_ER_EN 0x1
297 #define SEC_GB_CL_EN 0x10
298
299 // eMMC44 RPMB Flags
300 #define AUTH_KEY_PGM_REQ 0x0001
301 #define WR_CNTR_VAL_REQ 0x0002
302 #define AUTH_DAT_WR_REQ 0x0003
303 #define AUTH_DAT_RD_REQ 0x0004
304 #define RESULT_RD_REQ 0x0005
305
306 #define AUTH_KEY_PGM_RESP 0x0100
307 #define WR_CNTR_VAL_RESP 0x0200
308 #define AUTH_DAT_WR_RESP 0x0300
309 #define AUTH_DAT_RD_RESP 0x0400
310
311 #define OPR_OK 0x00
312 #define WRCNT_EXP_OPR_OK (0x80)
313 #define GEN_FAILURE 0x01
314 #define WRCNT_EXP_GEN_FAILURE (0x81)
315 #define AUTH_FAILURE 0x02
316 #define WRCNT_EXP_AUTH_FAILURE (0x82)
317 #define COUNTER_FAILURE 0x03
318 #define WRCNT_EXP_COUNTER_FAILURE (0x83)
319 #define ADDR_FAILURE 0x04
320 #define WRCNT_EXP_ADDR_FAILURE (0x84)
321 #define WRITE_FAILURE 0x05
322 #define WRCNT_EXP_WRITE_FAILURE (0x85)
323 #define READ_FAILURE 0x06
324 #define WRCNT_EXP_READ_FAILURE (0x86)
325 #define AUTH_KEY_NOT_PGMMED 0x07
326
327 #define RPMB_MAC_KEY_LENGTH 32
328 #define RPMB_NONCE_LENGTH 16
329 #define RPMB_DATA_LENGTH 256
330 #define RPMB_STUFF_LENGTH 196
331 #define RPMB_COUNTER_LENGTH 4
332 #define RPMB_ADDR_LENGTH 2
333 #define RPMB_BLKCNT_LENGTH 2
334 #define RPMB_RESULT_LENGTH 2
335 #define RPMB_RSPREQ_LENGTH 2
336 #define TOTAL_MAC_LEN_PER_FRAME 284
337 #define MACLEN_EXCL_DATA_PER_FRAME 28
338 #define RPMB_BLKCNT_MAX(x) ((x)*128*1024/256)
339
340 // EMMC ERROR CODES
341 #define EMMC_INVALID_BOOT_ACK -26
342 #define EMMC_INVALID_BOOT_PART -27
343 #define EMMC_INVALID_BOOT_ACCESS -28
344 #define EMMC_INVALID_BOOT_BUSWIDTH -29
345 #define EMMC_INVALID_BOOT_BUSWIDTHFLAG -30
346 #define EMMC_INVALID_BOOT_BUSMODE -31
347 #define EMMC_INVALID_BOOT_CONFIGPROT -32
348 #define EMMC_INVALID_BOOT_WPENABLE -33
349 #define EMMC_INVALID_USR_WPENABLE -34
350 #define EMMC_INVALID_ERASEGRPDEFN -35
351 #define EMMC_INVALID_GPP_NO -36
352
353 // Added for eMMC4.51
354 #define EMMC_CMD38_ERASE 0x00000000
355 #define EMMC_CMD38_SECURE_ERASE 0x80000000
356 #define EMMC_CMD38_SECURE_TRIM1 0x80000001
357 #define EMMC_CMD38_SECURE_TRIM2 0x80008000
358 #define EMMC_CMD38_TRIM 0x00000001
359 #define EMMC_CMD38_DISCARD 0x00000003
360
361 #define DEFAULT_CMD6_TIMEOUT_MS 500
362
363 #define MMC_STATUS_MASK (~0x0206BF7F)
364 #define MMC_STATUS_SWITCH_ERROR (1 << 7)
365 #define MMC_STATUS_RDY_FOR_DATA (1 << 8)
366 #define MMC_STATUS_CURR_STATE (0xf << 9)
367 #define MMC_STATUS_ERROR (1 << 19)
368
369 //
370 // SD/MMC/eMMC/SDIO protocl driver APIs
371 //
372
am_hal_unstuff_bits(uint32_t * resp,uint32_t start,uint32_t size)373 static inline uint32_t am_hal_unstuff_bits(uint32_t *resp, uint32_t start, uint32_t size)
374 {
375 const int __size = size;
376 const uint32_t __mask = (__size < 32 ? 1 << __size : 0) - 1;
377 const int __off = 3 - ((start) / 32);
378 const int __shft = (start)&31;
379 uint32_t __res;
380
381 __res = resp[__off] >> __shft;
382 if (__size + __shft > 32)
383 {
384 __res |= resp[__off - 1] << ((32 - __shft) % 32);
385 }
386 return __res &__mask;
387 }
388
am_hal_unstuff_bytes(uint32_t * ext_csd,uint32_t start,uint32_t size)389 static inline uint32_t am_hal_unstuff_bytes(uint32_t *ext_csd, uint32_t start, uint32_t size)
390 {
391 int i;
392 const uint32_t __size = size;
393 uint32_t __res = 0x0;
394 const uint8_t *__p = (const uint8_t *)ext_csd;
395 for (i = 0; i < __size; i++)
396 {
397 __res |= __p[start + i] << 8 * i;
398 }
399 return __res;
400 }
401
402
403 typedef enum
404 {
405 AM_HAL_ERASE = 0x00000000, // Erase
406 AM_HAL_DISCARD = 0x00000003, // Discard
407 AM_HAL_SECURE_ERASE = (int32_t)0x80000000, // Secure Erase
408 AM_HAL_TRIM = 0x00000001, // Trim
409 AM_HAL_SECURE_TRIM1 = (int32_t)0x80000001, // Secure Trim Step 1
410 AM_HAL_SECURE_TRIM2 = (int32_t)0x80008000, // Secure Trim Step 2
411 } am_hal_card_erase_type_t;
412
413 //
414 // SDHC card types
415 //
416 typedef enum
417 {
418 AM_HAL_CARD_TYPE_UNKNOWN,
419 AM_HAL_CARD_TYPE_MMC,
420 AM_HAL_CARD_TYPE_EMMC,
421 AM_HAL_CARD_TYPE_SDSC,
422 AM_HAL_CARD_TYPE_SDHC,
423 AM_HAL_CARD_TYPE_SDIO,
424 AM_HAL_CARD_TYPE_COMBO,
425 } am_hal_card_type_e;
426
427 typedef enum
428 {
429 AM_HAL_CARD_STATE_NOT_PRESENT,
430 AM_HAL_CARD_STATE_PRESENT,
431 AM_HAL_CARD_STATE_IDLE,
432 AM_HAL_CARD_STATE_READY,
433 AM_HAL_CARD_STATE_IDENT,
434 AM_HAL_CARD_STATE_STDY,
435 AM_HAL_CARD_STATE_TRANS,
436 AM_HAL_CARD_STATE_DATA,
437 AM_HAL_CARD_STATE_RCV,
438 AM_HAL_CARD_STATE_PRG,
439 AM_HAL_CARD_STATE_DIS,
440 AM_HAL_CARD_STATE_BTST,
441 AM_HAL_CARD_STATE_SLP,
442 AM_HAL_CARD_STATE_ERROR,
443 AM_HAL_CARD_STATE_PWROFF,
444 AM_HAL_CARD_STATE_PWRON,
445 } am_hal_card_state_e;
446
447 typedef struct
448 {
449 am_hal_card_type_e eType; // Card type
450 uint16_t ui16CmdClass; // Card command classes
451 uint8_t ui8RCA; // Relative card address
452 uint32_t ui32MaxBlks; // Card capacity in blocks
453 uint32_t ui32BlkSize; // Block size in bytes
454 uint32_t ui32LogiMaxBlks; // Card logical capacity in blocks
455 uint32_t ui32LogiBlkSize; // Logical block size in bytes
456 } am_hal_card_info_t;
457
458 typedef enum
459 {
460 AM_HAL_CARD_PWR_ON,
461 AM_HAL_CARD_PWR_OFF,
462 } am_hal_card_pwr_e;
463
464 typedef uint32_t (*am_hal_card_pwr_ctrl_func)(am_hal_card_pwr_e eCardPwr);
465
466 typedef enum
467 {
468 AM_HAL_CARD_PWR_CTRL_NONE,
469 AM_HAL_CARD_PWR_CTRL_SDHC_OFF,
470 AM_HAL_CARD_PWR_CTRL_SDHC_OFF_AND_CARD_SLEEP,
471 AM_HAL_CARD_PWR_CTRL_SDHC_AND_CARD_OFF,
472 } am_hal_card_pwr_ctrl_policy_e;
473
474 //
475 // SDHC card
476 //
477 typedef struct
478 {
479 am_hal_card_type_e eType;
480 am_hal_card_state_e eState;
481 uint32_t bCidValid:1;
482 uint32_t bCsdValid:1;
483 uint32_t bExtCsdValid:1;
484 uint32_t ui32OCR;
485 uint8_t ui8RCA;
486 uint32_t ui32CID[4];
487 uint32_t ui32CSD[4];
488 uint32_t ui32ExtCSD[128];
489 uint8_t ui8SpecVer;
490 uint8_t ui8ExtCSDRev;
491 uint8_t ui8PowerOffNotification;
492 bool bHighCapcity;
493 uint32_t ui32CacheSize;
494 uint32_t ui32SleepNotificationTimeout;
495 uint32_t ui32PowerOffNotificationLongTimeout;
496 uint32_t ui32GenericCmd6Timeout;
497 uint32_t ui32MaxBlks;
498 uint32_t ui32Capacity;
499 uint8_t ui8SecureErase;
500 uint8_t ui8DeviceType;
501 uint32_t ui32BlkNum;
502 bool bUseBlkEmulation;
503 uint32_t ui32NativeBlkSize;
504 uint32_t ui32BlkSize;
505 uint32_t ui32RpmbSizeMult;
506 uint16_t ui16CmdClass;
507 am_hal_card_pwr_ctrl_func pCardPwrCtrlFunc;
508 am_hal_card_pwr_ctrl_policy_e eCardPwrCtrlPolicy;
509 am_hal_card_cfg_t cfg;
510 am_hal_card_host_t *pHost;
511 } am_hal_card_t;
512
513 //*****************************************************************************
514 //
515 //! @brief Power off the SDHC or eMMC CARD
516 //!
517 //! @param pCard - pointer to the card instance.
518 //!
519 //! This function is used to power off the SDHC host controller or eMMC card according
520 //! to the power policy setting in pCard->eCardPwrCtrlPolicy
521 //!
522 //! @return status - generic or interface specific status.
523 //
524 //*****************************************************************************
525 extern uint32_t am_hal_card_pwrctrl_sleep(am_hal_card_t *pCard);
526
527 //*****************************************************************************
528 //
529 //! @brief Power on the SDHC or eMMC CARD
530 //!
531 //! @param pCard - pointer to the card instance.
532 //!
533 //! This function is used to power on the SDHC host controller or eMMC card according
534 //! to the power policy setting in pCard->eCardPwrCtrlPolicy
535 //!
536 //! @return status - generic or interface specific status.
537 //
538 //*****************************************************************************
539 extern uint32_t am_hal_card_pwrctrl_wakeup(am_hal_card_t *pCard);
540
541 //*****************************************************************************
542 //
543 //! @brief Get the card instance function
544 //!
545 //! @param pHost - pointer to the card host instance.
546 //!
547 //! @param pCard - pointer to the card instance.
548 //!
549 //! This function finds a card and trying to initialize it. if
550 //! card's initialization succeeds, the card will be 'Trans' state and can do
551 //! block read, write and erase operations.
552 //! @return status - generic or interface specific status..
553 //
554 //*****************************************************************************
555 extern uint32_t am_hal_card_host_find_card(am_hal_card_host_t *pHost, am_hal_card_t *pCard);
556
557 //*****************************************************************************
558 //
559 //! @brief Set the card operation configurations
560 //!
561 //! @param pCard - pointer to the card instance.
562 //!
563 //! @param eType - card type like eMMC, MMC, SDIO, .etc
564 //!
565 //! @param eBusWidth - card bus width like 1, 4, 8 bit bus width
566 //!
567 //! @param ui32Clock - card bus clock speed
568 //!
569 //! @param eIoVoltage - card bus operation voltage
570 //!
571 //! @param eUHSMode - card UHS mode
572 //!
573 //! This function sets the card's type, bus width, clock speed, bus operation voltage and UHS mode
574 //! but these settings will take effect immediately.
575 //!
576 //! @return status - generic or interface specific status..
577 //
578 //*****************************************************************************
579 extern uint32_t am_hal_card_cfg_set(am_hal_card_t *pCard, am_hal_card_type_e eType,
580 am_hal_host_bus_width_e eBusWidth, uint32_t ui32Clock, am_hal_host_bus_voltage_e eIoVoltage,
581 am_hal_host_uhs_mode_e eUHSMode);
582
583 //*****************************************************************************
584 //
585 //! @brief Initialize the card instance function
586 //!
587 //! @param pCard - pointer to the card instance.
588 //! @param pCardPwrCtrlFunc - pointer to the User defined Card Pwr Ctrl Func
589 //! @param eCardPwrCtrlPolicy - Power Control Policy can be:
590 //! AM_HAL_CARD_PWR_CTRL_NONE
591 //! AM_HAL_CARD_PWR_CTRL_SDHC_OFF
592 //! AM_HAL_CARD_PWR_CTRL_SDHC_OFF_AND_CARD_SLEEP
593 //! AM_HAL_CARD_PWR_CTRL_SDHC_AND_CARD_OFF
594 //!
595 //! This function sends a sequence of commands to initalize the card and let the card
596 //! enter into the 'Trans' state.
597 //!
598 //! @return status - generic or interface specific status..
599 //
600 //*****************************************************************************
601 extern uint32_t am_hal_card_init(am_hal_card_t *pCard, am_hal_card_pwr_ctrl_func pCardPwrCtrlFunc,
602 am_hal_card_pwr_ctrl_policy_e eCardPwrCtrlPolicy);
603
604 //*****************************************************************************
605 //
606 //! @brief De-Initialize the card instance function
607 //!
608 //! @param pCard - pointer to the card instance.
609 //!
610 //! This function resets the card and cleans the card related setting and information.
611 //!
612 //! @return status - generic or interface specific status..
613 //
614 //*****************************************************************************
615 extern uint32_t am_hal_card_deinit(am_hal_card_t *pCard);
616
617 //*****************************************************************************
618 //
619 //! @brief synchronous block-oriented read function
620 //!
621 //! @param pCard - pointer to the card instance.
622 //!
623 //! @param ui32Blk - start block number
624 //!
625 //! @param ui32BlkCnt - read block count
626 //!
627 //! @param pui8Buf - read buffer
628 //!
629 //! This function reads the 'ui32BlkCnt' blocks starting from 'ui32Blk' block and
630 //! saves the data in the 'pui8Buf' read buffer. The caller will be blocked until all
631 //! data has been received or failed.
632 //!
633 //! @return status - generic or interface specific status..
634 //
635 //*****************************************************************************
636 extern uint32_t am_hal_card_block_read_sync(am_hal_card_t *pCard, uint32_t ui32Blk, uint32_t ui32BlkCnt, uint8_t *pui8Buf);
637
638 //*****************************************************************************
639 //
640 //! @brief synchronous block-oriented write function
641 //!
642 //! @param pCard - pointer to the card instance.
643 //!
644 //! @param ui32Blk - start block number
645 //!
646 //! @param ui32BlkCnt - write block count
647 //!
648 //! @param pui8Buf - write buffer
649 //!
650 //! This function writes 'ui32BlkCnt' blocks in the 'pui8Buf' write buffer to the card blocks
651 //! starting from 'ui32Blk' block. The caller will be blocked until all data has been sent
652 //! out or failed.
653 //!
654 //! @return status - generic or interface specific status..
655 //
656 //*****************************************************************************
657 extern uint32_t am_hal_card_block_write_sync(am_hal_card_t *pCard, uint32_t ui32Blk, uint32_t ui32BlkCnt, uint8_t *pui8Buf);
658
659 //*****************************************************************************
660 //
661 //! @brief asynchronous block-oriented read function
662 //!
663 //! @param pCard - pointer to the card instance.
664 //!
665 //! @param ui32Blk - start block number
666 //!
667 //! @param ui32BlkCnt - read block count
668 //!
669 //! @param pui8Buf - read buffer
670 //!
671 //! This function reads the 'ui32BlkCnt' blocks starting from 'ui32Blk' block and
672 //! saves the data in the 'pui8Buf' read buffer. The caller will not be blocked,
673 //! Data is ready in the buffer will be notified by the register callback function.
674 //!
675 //! @return status - generic or interface specific status..
676 //
677 //*****************************************************************************
678 extern uint32_t am_hal_card_block_read_async(am_hal_card_t *pCard, uint32_t ui32Blk, uint32_t ui32BlkCnt, uint8_t *pui8Buf);
679
680 //*****************************************************************************
681 //
682 //! @brief asynchronous block-oriented write function
683 //!
684 //! @param pCard - pointer to the card instance.
685 //!
686 //! @param ui32Blk - start block number
687 //!
688 //! @param ui32BlkCnt - write block count
689 //!
690 //! @param pui8Buf - write buffer
691 //!
692 //! This function writes 'ui32BlkCnt' blocks in the 'pui8Buf' write buffer to the card blocks
693 //! starting from 'ui32Blk' block. The caller will not be blocked, Completion of data transfer
694 //! will be notified by the register callback function.
695 //!
696 //! @return status - generic or interface specific status..
697 //
698 //*****************************************************************************
699 extern uint32_t am_hal_card_block_write_async(am_hal_card_t *pCard, uint32_t ui32Blk, uint32_t ui32BlkCnt, uint8_t *pui8Buf);
700
701 //*****************************************************************************
702 //
703 //! @brief block-oriented erase function
704 //!
705 //! @param pCard - pointer to the card instance.
706 //!
707 //! @param ui32Blk - start block number
708 //!
709 //! @param ui32BlkCnt - write block count
710 //!
711 //! @param erasetype - erase type
712 //!
713 //! @param ui32TimeoutMS - erase timeout value in millisecond
714 //!
715 //! This function erases 'ui32BlkCnt' blocks staring from 'ui32Blk' block. The caller will be blocked
716 //! until the erase is done or failed.
717 //!
718 //! @return status - generic or interface specific status..
719 //
720 //*****************************************************************************
721 extern uint32_t am_hal_card_block_erase(am_hal_card_t *pCard, uint32_t ui32Blk, uint32_t ui32BlkCnt, am_hal_card_erase_type_t erasetype, uint32_t ui32TimeoutMS);
722
723 //*****************************************************************************
724 //
725 //! @brief Get the card status function
726 //!
727 //! @param pCard - pointer to the card instance.
728 //!
729 //! @param pui32Status - the card status
730 //!
731 //! This function gets the card status by sending CMD13.
732 //!
733 //! @return status - generic or interface specific status..
734 //
735 //*****************************************************************************
736 extern uint32_t am_hal_card_status(am_hal_card_t *pCard, uint32_t *pui32Status);
737
738 //*****************************************************************************
739 //
740 //! @brief Get the card state function
741 //!
742 //! @param pCard - pointer to the card instance.
743 //!
744 //! This function gets the card state by sending CMD13.
745 //!
746 //! @return status - emmc card state.
747 //
748 //*****************************************************************************
749 extern am_hal_card_state_e am_hal_card_state(am_hal_card_t *pCard);
750
751 //*****************************************************************************
752 //
753 //! @brief register the card event call back function
754 //!
755 //! @param pCard - pointer to the card instance.
756 //!
757 //! @param pfunCallback - function pointer to the call back function.
758 //!
759 //! This function registers a card event callback function for async block-oriented
760 //! read and write functions.
761 //!
762 //! @return status - generic or interface specific status..
763 //
764 //*****************************************************************************
765 extern uint32_t am_hal_card_register_evt_callback(am_hal_card_t *pCard, am_hal_host_event_cb_t pfunCallback);
766
767 //*****************************************************************************
768 //
769 //! @brief card cid information parse function
770 //!
771 //! @param pCard - pointer to the card instance.
772 //! @param ui16Offset - bit start position in the CID data structure.
773 //! @param ui8Size - bit length in the CID data structrure.
774 //!
775 //! This function parses the specified CID field in the CID data structure and returns
776 //! as 32bit length integer.
777 //!
778 //! @return status - generic or interface specific status..
779 //
780 //*****************************************************************************
781 extern uint32_t am_hal_card_get_cid_field(am_hal_card_t *pCard, uint16_t ui16Offset, uint8_t ui8Size);
782
783 //*****************************************************************************
784 //
785 //! @brief card csd information parse function
786 //!
787 //! @param pCard - pointer to the card instance.
788 //! @param ui16Offset - bit start position in the CSD data structure.
789 //! @param ui8Size - bit length in the CSD data structrure.
790 //!
791 //! This function parses the specified CSD field in the CSD data structure and returns
792 //! as 32bit length integer.
793 //!
794 //! @return status - generic or interface specific status..
795 //
796 //*****************************************************************************
797 extern uint32_t am_hal_card_get_csd_field(am_hal_card_t *pCard, uint16_t ui16Offset, uint8_t ui8Size);
798
799 //*****************************************************************************
800 //
801 //! @brief card ext csd information parse function
802 //!
803 //! @param pCard - pointer to the card instance.
804 //! @param ui16Offset - byte start position in the EXT CSD data structure.
805 //! @param ui8Size - byte length in the EXT CSD data structrure.
806 //!
807 //! This function parses the specified EXT CSD field in the EXT CSD data structure and returns
808 //! as 32bit length integer.
809 //!
810 //! @return status - generic or interface specific status..
811 //
812 //*****************************************************************************
813 extern uint32_t am_hal_card_get_ext_csd_field(am_hal_card_t *pCard, uint16_t ui16Offset, uint8_t ui8Size);
814
815 //*****************************************************************************
816 //
817 //! @brief Get the card information function
818 //!
819 //! @param pCard - pointer to the card instance.
820 //! @param pCardInfo - pointer to am_hal_card_info_t structure that contains card info.
821 //!
822 //! This function gets the general eMMC card info into am_hal_card_info_t structure.
823 //!
824 //! @return status - generic or interface specific status..
825 //
826 //*****************************************************************************
827 extern uint32_t am_hal_card_get_info(am_hal_card_t *pCard, am_hal_card_info_t *pCardInfo);
828
829 //*****************************************************************************
830 //
831 //! @brief Set the card mode function
832 //!
833 //! @param pCard - pointer to the card instance.
834 //! @param ui32Mode - the card mode
835 //! @param ui32Timeout - mode switch timeout
836 //!
837 //! This function sets the card mode by sending CMD6.
838 //!
839 //! @return status - generic or interface specific status..
840 //
841 //*****************************************************************************
842 extern uint32_t am_hal_card_mode_switch(am_hal_card_t *pCard, uint32_t ui32Mode, uint32_t ui32Timeout);
843
844 //*****************************************************************************
845 //
846 //! @brief Get the TX/RX delay setting by calibration
847 //!
848 //! @param eUHSMode - card UHS mode
849 //! @param ui32Clock - card bus clock speed
850 //! @param eBusWidth - SDIO bus width.
851 //! @param ui8CalibBuf - data buffer used to do the calibration
852 //! @param ui32StartBlk - eMMC start block used to do the calibration
853 //! @param ui32BlkCnt - eMMC block number used to do the calibration
854 //! @param ui8TxRxDelays- an averaged the TX/RX delay values
855 //!
856 //! This function get the tx rx delay setting by finding all workable TX/RX delay
857 //! settings, then an average TX/RX values are returned.
858 //!
859 //! @return status - generic or interface specific status..
860 //
861 //*****************************************************************************
862 extern uint32_t am_hal_card_emmc_calibrate(am_hal_host_uhs_mode_e eUHSMode,
863 uint32_t ui32Clock,
864 am_hal_host_bus_width_e eBusWidth,
865 uint8_t *ui8CalibBuf,
866 uint32_t ui32StartBlk,
867 uint32_t ui32BlkCnt,
868 uint8_t ui8TxRxDelays[2]);
869
870 //*****************************************************************************
871 //
872 //! @brief Read blocks of data from the card (GEN_CMD) asynchronously
873 //!
874 //! @param pCard - pointer to the card instance.
875 //! @param ui32Arg - command arguments
876 //! @param pui8Buf - mode switch timeout
877 //!
878 //! @return status - generic or interface specific status..
879 //
880 //*****************************************************************************
881 extern uint32_t am_hal_card_cmd56_read_async(am_hal_card_t *pCard, uint32_t ui32Arg, uint8_t *pui8Buf);
882
883 //*****************************************************************************
884 //
885 //! @brief Read blocks of data from the card (GEN_CMD) synchronously
886 //!
887 //! @param pCard - pointer to the card instance.
888 //! @param ui32Arg - command arguments
889 //! @param pui8Buf - mode switch timeout
890 //!
891 //! @return status - generic or interface specific status..
892 //
893 //*****************************************************************************
894 extern uint32_t am_hal_card_cmd56_read_sync(am_hal_card_t *pCard, uint32_t ui32Arg, uint8_t *pui8Buf);
895
896 //*****************************************************************************
897 //
898 //! @brief emmc rpmb read write function
899 //!
900 //! @param pCard - pointer to the card instance.
901 //!
902 //! @param pui8Buf - write/read buffer
903 //!
904 //! @param bRead - eMMC rpmb write/read direction
905 //!
906 //! @param bRelWrite - eMMC rpmb is reliable write type.
907 //!
908 //! This function only support for eMMC rpmb operation.
909 //!
910 //! @return status - generic or interface specific status..
911 //
912 //*****************************************************************************
913 extern uint32_t
914 am_hal_card_block_rpmb_rw(am_hal_card_t *pCard, uint8_t *pui8Buf, bool bRead, bool bRelWrite);
915
916 #ifdef __cplusplus
917 }
918 #endif
919
920 #endif // AM_HAL_CARD_H
921
922 //*****************************************************************************
923 //
924 // End Doxygen group.
925 //! @}
926 //
927 //*****************************************************************************
928
929