blob: ce59d2369e034a0bfd32554e5b01d8f2395d46e7 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/******************************************************************************
2 *
3 * (C)Copyright 2005 - 2011 Marvell. All Rights Reserved.
4 *
5 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF MARVELL.
6 * The copyright notice above does not evidence any actual or intended
7 * publication of such source code.
8 * This Module contains Proprietary Information of Marvell and should be
9 * treated as Confidential.
10 * The information in this file is provided for the exclusive use of the
11 * licensees of Marvell.
12 * Such users have the right to use, modify, and incorporate this code into
13 * products for purposes authorized by the license agreement provided they
14 * include this notice and the associated copyright notice with any such
15 * product.
16 * The information in this file is provided "AS IS" without warranty.
17 *
18******************************************************************************/
19
20#ifndef __SDMMC_API_H__
21#define __SDMMC_API_H__
22
23#include "Typedef.h"
24#include "Flash.h"
25#include "xllp_defs.h"
26
27// Globals
28#define TRUE 1
29#define FALSE 0
30#define OSCR_OFFSET 0x10
31#define OCR_ARG 0x80FF8000
32#define SD_OCR_ARG 0x40FF8000
33#define OCR_ACCESS_MODE_MASK 0x60000000
34#define RESPONSE_LENGTH 8
35#define R2_BYTE_LENGTH 16
36#define MMC_BUSY_BIT 0x80000000
37#define SD_NO_CMD1 0x300
38#define LOW_ARG 0x0000ffff
39#define NO_FLAGS 0
40#define BLK_CNT 1
41#define BLK_LEN 512
42#define SDVHS_2_7_TO_3_6 0x1
43#define SDVHS_LOW_VOLT 0x2
44#define SDVHSARGSHIFT 8
45#define SDVHSCHECKPATTERN 0x33
46#define HARD512BLOCKLENGTH 512
47#define PARTITIONMASK 0xF
48#define MMC_SD_BOOT_PARTITION 1
49#define MMC_SD_BOOT_PARTITION2 2
50#define MMC_SD_USER_PARTITION 0
51#define SCRSD1BITMODE 0x1
52#define SCRSD4BITMODE 0x5
53#define SD_CMD6_4BITMODE 2
54#define R1_LOCKEDCARDMASK 0xFDFFFFFF
55#define R1_NOMASK 0xFFFFFFFF
56#define SD_DMA_128_BRST_SIZE_MASK 0x00000002
57#define SD_DMA_BRST_SIZE_CLEAR_MASK 0xFFFFFFFC
58#define SGPT_ILLEGAL_LENGTH 0x5a5a5a5a
59
60// MMC Controller selections
61typedef enum
62{
63 MMCNOTENABLED = 0,
64 MMCSDHC0_1 = 1,
65 MMCSDHC0_2 = 2,
66 MMCSDHC1_1 = 3,
67 MMCSDHC1_2 = 4
68}CONTROLLER_TYPE;
69
70enum
71{
72 XLLP_MMC = 0,
73 XLLP_SD =1,
74 XLLP_eSD = 2,
75 XLLP_SDIO =3,
76};
77
78typedef enum
79{
80 XLLP_MMC_CMD0 = 0x0, // go idle state
81 XLLP_MMC_CMD1 = 0x1, // send op command
82 XLLP_MMC_CMD2 = 0x2, // all send cid
83 XLLP_MMC_CMD3 = 0x3, // set relative addr
84 XLLP_SD_CMD3 = 0x3, // set relative addr
85 XLLP_SDIO_CMD3 = 0x3, // set relative addr
86 XLLP_MMC_CMD4 = 0x4, // set dsr
87 XLLP_SDIO_CMD5 = 0x5, // SDIO
88 XLLP_SDIO_CMD7 = 0x7, // select/deselect card for SDIO
89 XLLP_MMC_CMD7 = 0x7, // select/deselect card
90 XLLP_SD_CMD6 = 0x6, // SD Switch Function Command
91 XLLP_MMC_CMD6 = 0x6, // MMC Switch Function Command
92 XLLP_SD_ACMD6 = 0x6, // SD ACMD Command for SET_BUS_WIDTH
93 XLLP_SD_CMD8 = 0x8, // SD Card Interface Condition
94 XLLP_MMC_CMD8 = 0x8, // MMC request to read EXT CSD
95 XLLP_MMC_CMD9 = 0x9, // send csd
96 XLLP_MMC_CMD10 = 0xa, // send cid
97 XLLP_MMC_CMD11 = 0xb, // read data until stop
98 XLLP_MMC_CMD12 = 0xc, // stop transmission
99 XLLP_MMC_CMD13 = 0xd, // send status
100 XLLP_MMC_CMD14 = 0xe, // receive bus width test pattern
101 XLLP_MMC_CMD15 = 0xf, // go inactive state
102 XLLP_MMC_CMD16 = 0x10, // set block length
103 XLLP_MMC_CMD17 = 0x11, // read single block
104 XLLP_MMC_CMD18 = 0x12, // read multiple block
105 XLLP_MMC_CMD19 = 0x13, // Send Buswidth test pattern
106 XLLP_MMC_CMD20 = 0x14, // write data until stop
107 XLLP_MMC_CMD23 = 0x17, // Set Block Count
108 XLLP_MMC_CMD24 = 0x18, // write block
109 XLLP_MMC_CMD25 = 0x19, // write multiple block
110 XLLP_MMC_CMD26 = 0x1a, // program CID
111 XLLP_MMC_CMD27 = 0x1b, // program CSD
112 XLLP_MMC_CMD28 = 0x1c, // set write prot
113 XLLP_MMC_CMD29 = 0x1d, // clr write prot
114 XLLP_MMC_CMD30 = 0x1e, // send write prot
115 XLLP_SD_CMD32 = 0x20, // tag sector start
116 XLLP_SD_CMD33 = 0x21, // tag sector end
117 XLLP_MMC_CMD34 = 0x22, // untag sector
118 XLLP_MMC_CMD35 = 0x23, // tag erase group start
119 XLLP_MMC_CMD36 = 0x24, // tag erase group end
120 XLLP_MMC_CMD37 = 0x25, // untag erase group
121 XLLP_eSD_CMD37 = 0x25, // SD PartitionManagement group Command
122 XLLP_eSD_CMD57 = 0x39,
123 XLLP_MMC_CMD38 = 0x26, // erase
124 XLLP_MMC_CMD39 = 0x27, //fast IO
125 XLLP_MMC_CMD40 = 0x28, // go irq state
126 XLLP_MMC_CMD42 = 0x2a, // lock-unlock
127 XLLP_eSD_CMD43 = 0x2b, // Select Partition
128 XLLP_SDIO_CMD52 = 0x34, // SDIO read/write single register
129 XLLP_SDIO_CMD53 = 0x35, // SDIO read/write blocks
130 XLLP_SD_CMD55 = 0x37, // app cmd
131 XLLP_MMC_CMD56 = 0x38, // gen cmd
132 XLLP_SPI_CMD58 = 0x3a, // read ocr
133 XLLP_SPI_CMD59 = 0x3b, // crc on-off
134 XLLP_SD_ACMD41 = 0x29,
135 XLLP_SD_ACMD51 = 0x33 // Read SD Configuration Register (SCR)
136} XLLP_MMC_CMD;
137
138typedef struct
139{
140 UINT_T CID_VALUE[4];
141 UINT_T SerialNum;
142} CID_LAYOUT;
143
144
145// note:
146// in the mmc spec, the first bits of the csd are numbered 127:126, etc.
147// the csd_structure field has bits labelled "127:126" in the mmc spec.
148// => the csd_structure field appears first on the logic analyzer.
149//
150// within a byte, the high order bits of a byte correspond to the
151// higher numbers in the mmc spec. for example, the two high order bits
152// of a byte are the ones that contain the two bit "csd_structure" field.
153//
154//
155// CSD structure, as layed out in our SD/MMC controller's response buffer:
156//
157// the 128 bit response is stored backwards:
158//
159// when looking at a logic analyzer trace, the first eight bits (bits 127-120) of the csd response
160// are stored in the sixteenth byte of the reponse buffer. (ie. respbuf[15] has csd_structure field).
161//
162// then the next eight bits of the response are stored in the fifteenth byte of the response buffer.
163// (ie. respbuf[14] has taac field).
164// the next eight bits of the response are stored in the fourteenth byte of the response buffer.
165// (ie. respbuf[13] has nsac field).
166//
167// the process continues until the last eight bits of the response
168// are stored in the first byte of the response buffer.
169// (ie. respbuf[0] has file_format_grp, copy, perm... ecc fields)
170//
171
172typedef struct __attribute__ ((__packed__)) SDMMC_CSD_S
173{
174// unsigned rsv4:1; // 0: 0 // stripped by our controller
175// unsigned crc:7; // 7: 1 // stripped by our conroller. if not, it would have been the first byte in the response buffer.
176 unsigned ecc:2; // 9: 8 // low order bit of first byte in response buffer, response buffer[0]
177 unsigned file_format:2; // 11: 10
178 unsigned tmp_write_protect:1; // 12: 12
179 unsigned perm_write_protect:1; // 13: 13
180 unsigned copy:1; // 14: 14
181 unsigned file_format_grp:1; // 15: 15 // high order bit of first byte in response buffer, response buffer[0]
182 unsigned content_prot_app:1; // 16: 16
183 unsigned rsv3:4; // 20: 17
184 unsigned write_bl_partial:1; // 21: 21
185 unsigned write_bl_len:4; // 25: 22
186 unsigned r2w_factor:3; // 28: 26
187 unsigned default_ecc:2; // 30: 29
188 unsigned wp_grp_enable:1; // 31: 31
189 unsigned wp_grp_size:5; // 36: 32
190 unsigned erase_grp_mult:5; // 41: 37
191 unsigned erase_grp_size:5; // 46: 42
192 unsigned c_size_mult:3; // 49: 47
193 unsigned vdd_w_curr_max:3; // 52: 50
194 unsigned vdd_w_curr_min:3; // 55: 53
195 unsigned vdd_r_curr_max:3; // 58: 56
196 unsigned vdd_r_curr_min:3; // 61: 59
197 unsigned c_size:12; // 73: 62
198 unsigned rsv2:2; // 75: 74
199 unsigned dsr_imp:1; // 76: 76
200 unsigned read_blk_misalign:1; // 77: 77
201 unsigned write_blk_misalign:1; // 78: 78
202 unsigned read_bl_partial:1; // 79: 79
203 unsigned read_bl_len:4; // 83: 80
204 unsigned ccc:12; // 95: 84
205 unsigned tran_speed:8; // 103: 96
206 unsigned nsac:8; // 111:104 // response buffer[13]
207 unsigned taac:8; // 119:112 // response buffer[14]
208 unsigned rsv1:2; // 121:120 // low order bit of 15th byte in the response buffer.
209 unsigned spec_vers:4; // 125:122
210 unsigned csd_structure:2; // 127:126 // this first bit in the response package is stored as the high order bit of 15th byte in the response buffer.
211} SDMMC_CSD_T;
212
213
214
215typedef struct
216{
217 UINT_T CSD_VALUE[4];
218} CSD_LAYOUT;
219
220typedef struct
221{
222 UINT_T SCR_VALUE[2];
223} SCR_LAYOUT;
224
225// SD CMD 6 Related
226typedef struct
227{
228 unsigned int FunctionGroup :24;
229 unsigned int Reserved :7;
230 unsigned int Mode :1;
231} SD_CMD6_LAYOUT;
232
233#define SDMMC_MAX_BUSWIDTH 8
234// EXT_CSD
235#define BOOT_BUS_WIDTH_MMC_EXT_CSD_OFFSET 177
236#define BUS_WIDTH_8_WITH_SDR 0x2 // 8 bit data bus
237
238#define PARTITION_CONFIG_MMC_EXT_CSD_OFFSET 179
239#define BOOT_FROM_PARTITION_1_WITH_BOOTACK 0x49 // boot from partition 1 with boot ack enable
240
241#define BUS_WIDTH_MMC_EXT_CSD_OFFSET 183
242#define HS_TIMING_MMC_EXT_CSD_OFFSET 185
243#define EXT_CSD_ACCESS_CMD_SET 0
244#define EXT_CSD_ACCESS_SET_BITS 1
245#define EXT_CSD_ACCESS_CLEAR_BITS 2
246#define EXT_CSD_ACCESS_WRITE_BYTE 3
247#define PARTITION_ACCESS_BITS 0x7
248
249
250#define PARTITION_MANAGEMENT_CMD_SET 0xFFFF2F
251#define DEFAULT_CMD_SET 0xFFFFFF
252#define CHECK_FUNCTION_MODE 0
253#define SWITCH_FUNCTION_MODE 1
254#define PARTITION_MANAGEMENT_FUNCTION 2
255#define MMC_ALTERNATE_BOOT_ARGUMENT 0xFFFFFFFA
256#define MMC_CMD0_PRE_IDLE_ARGUMENT 0xF0F0F0F0
257
258
259typedef union
260{
261 UINT_T SD_CMD6_Bits;
262 SD_CMD6_LAYOUT SD_CMD6_Layout;
263} SD_CMD6_OVERLAY;
264
265
266// MMC CMD 6 Related
267typedef struct
268{
269 unsigned int CmdSet :3;
270 unsigned int Reserved0 :5;
271 unsigned int Value :8;
272 unsigned int Index :8;
273 unsigned int Access :2;
274 unsigned int Reserved1 :6;
275} MMC_CMD6_LAYOUT;
276
277typedef union
278{
279 UINT_T MMC_CMD6_Bits;
280 MMC_CMD6_LAYOUT MMC_CMD6_Layout;
281} MMC_CMD6_OVERLAY;
282
283
284
285typedef enum
286{
287 UNINITIALIZED, // Controller and Card are uninitialized
288 INITIALIZE, // Controller and Card are being ninitialized
289 READ, // Multiple Block Read State
290 ERASE, // Erase State
291 WRITE, // Multiple Block Write State
292 READY, // The Card is ready for Data Transfer
293 DATATRAN, // The controller has finished data transfer but card may be busy
294 FAULT // Fault
295} SDMMC_IO_TRANSFERS;
296
297// Vital information used in Data Transfers by the ISR and driver routines.
298typedef struct
299{
300 UINT_T CardAddress; // Starting Card Address
301 UINT_T TransWordSize; // Total Number of Bytes involved in this transaction
302 UINT_T NumBlocks; // Total Number of Blocks involved in this transaction
303 UINT_T LocalAddr; // Destination Address Pointer for reads, and source addr for writes
304 UINT_T StartDiscardWords; // Words - from the first block that caller doesn't want
305 UINT_T EndDiscardWords; // Words - from the last block that caller doesn't want
306 UINT_T WordIndex; // Words - Word index to the progress in this transfer request
307 UINT8_T Cmd; // The command that started the transaction
308 UINT8_T ACmdFlag; // Indicates if the executing command is standard vs. application specific (ACMD)
309 UINT8_T AutoCMD12Mode; // When "true", the controller is operating in auto command 12 mode.
310 UINT8_T RespType; // Info about the response type and potential for busy state.
311} SDMMC_TRANSFER;
312
313typedef struct
314{
315 UINT_T RCA; // RCA of the card
316 UINT_T OCR; // OCR Register Contents
317 CID_LAYOUT CID; // CID Register
318 CSD_LAYOUT CSD; // CSD Register Contents
319 SCR_LAYOUT SCR; // SCR Register Contents
320} SDMMC_CARD_REGISTERS;
321#define SD_SPEC_MASK 0x0F000000
322#define SD_SPEC_OFFSET 24
323
324// Legacy controller related driver functions
325typedef struct
326{
327 UINT_T (*Init_F) (); // Internal Initialization function
328 UINT_T (*SwitchPartition_F) (); // Internal Switch Partition function
329 UINT_T (*Read_F) (); // Internal Read function
330 UINT_T (*Write_F) (); // Internal Write Function
331 UINT_T (*Erase_F) (); // Internal Erase function
332 UINT_T (*Shut_F) (); // Internal Shutdown function
333} SDMMC_FUNCTIONS;
334
335typedef struct
336{
337 UINT_T pBuffer[4]; // Buffer to read values in to
338 UINT_T CommandComplete;
339 UINT_T TransferComplete;
340// TBD UINT_T CommandError;
341 UINT_T SendStopCommand; // If some read/write transactions require stop command
342 UINT_T R1_RESP; // Capture the R1 Response of the Card for most commands
343} SDMMC_RESPONSE;
344
345typedef enum
346{
347 BYTE_ACCESS = 0,
348 SECTOR_ACCESS = 1
349}ACCESS_MODE;
350
351typedef enum
352{
353 NODMA = 0,
354 SDMA = 1,
355 ADMA2 = 2,
356}DMA_TYPE;
357
358// This Properties structure is shared between MM4 and legacy type drivers. The pContext field
359// is specific to the variant.
360typedef struct
361{
362 CONTROLLER_TYPE ControllerType; // See CONTROLLER_TYPE platformconfig.h
363 void *pContext; // Pointer to MMC control registers
364 UINT8_T SD; // Indicates if the card is SD, eSD or MMC
365 UINT8_T Slot; // Indicates which slot used
366 SDMMC_CARD_REGISTERS CardReg; // Card Registers
367 SDMMC_RESPONSE CardReponse; // Card Response Related
368 UINT_T SD_VHS; // SD Voltage Acceptance Return Value (SD only)
369 volatile SDMMC_IO_TRANSFERS State; // Indicate State of the card
370 ACCESS_MODE AccessMode; // High Density Card
371 SDMMC_TRANSFER Trans; // Transfer State of the Card
372 UINT_T ReadBlockSize; // Bytes - Block Size Used for Reads
373 UINT_T WriteBlockSize; // Bytes - Block Size Used for Writes
374 UINT_T EraseSize; // Bytes - Minimum Size of an erasable unit
375 UINT_T CardCapacity; // Bytes - Maximum Capacity of the card
376 P_FlashProperties_T pFlashP; // A pointer to generic FlashProperties structure
377 SDMMC_FUNCTIONS Funcs; // Pointer to some internal driver functions
378 DMA_TYPE SDMMC_DMA_Mode; // Enable ADMA/SDMA Mode or Not.
379 UINT_T StrictErrorCheck; // Relax error checking during card init. Not all cards are strictly compliant.
380 UINT_T BootPartitionSize; // Bytes - maximum size of boot partition
381 UINT_T DeviceType; // Device Type
382 UINT_T DeviceStatus; // Error Interrupt Status Register
383}
384SDMMC_Properties_T, *P_SDMMC_Properties_T;
385
386
387/*********************************************************************
388* SDMMC R1 Response Bits
389**********************************************************************/
390#define R1_SWITCH_ERROR XLLP_BIT_7
391#define R1_ERASE_RESET_ERROR XLLP_BIT_13
392#define R1_CIDCSD_OVERWRITE_ERROR XLLP_BIT_16
393//#define R1_OVERRUN_ERROR XLLP_BIT_17
394//#define R1_UNDERUN_ERROR XLLP_BIT_18
395#define R1_GENERAL_ERROR XLLP_BIT_19
396#define R1_CC_ERROR XLLP_BIT_20
397#define R1_ECC_ERROR XLLP_BIT_21
398#define R1_ILL_CMD_ERROR XLLP_BIT_22
399#define R1_COM_CRC_ERROR XLLP_BIT_23
400#define R1_LOCK_ULOCK_ERRROR XLLP_BIT_24
401#define R1_LOCK_ERROR XLLP_BIT_25
402#define R1_WP_ERROR XLLP_BIT_26
403#define R1_ERASE_PARAM_ERROR XLLP_BIT_27
404#define R1_ERASE_SEQ_ERROR XLLP_BIT_28
405#define R1_BLK_LEN_ERROR XLLP_BIT_29
406#define R1_ADDR_MISALIGN_ERROR XLLP_BIT_30
407#define R1_ADDR_RANGE_ERROR XLLP_BIT_31
408
409typedef struct
410{
411 unsigned int Reserved0 : 1;
412 unsigned int BootAck : 1;
413 unsigned int BootPartitionEn : 3;
414 unsigned int PartitionAccess : 3;
415} PARTITION_CONFIG_EXT_CSD;
416
417/*********************************************************************
418* SDMMC Interface API's
419**********************************************************************/
420UINT_T InitializeSDMMCDevice(UINT8_T FlashNum, FlashBootType_T FlashBootType, UINT8_T* P_DefaultPartitionNum);
421UINT_T SDMMC_SHUTDOWN(void);
422UINT_T SDMMC_READ (UINT_T FlashOffset, UINT_T LocalBuffer, UINT_T Size);
423UINT_T SDMMC_WRITE (UINT_T FlashOffset, UINT_T LocalBuffer, UINT_T Size);
424UINT_T SDMMC_ERASE (UINT_T FlashOffset, UINT_T Size);
425UINT_T SDMMC_SETPARTITION (UINT_T PartitionNum);
426UINT_T SDMMC_GetFlashLastRoom(UINT_T Size);
427
428P_SDMMC_Properties_T GetSDMMCProperties(void);
429void SetSDMMCProperties(P_SDMMC_Properties_T pNewSDMMC_Prop);
430UINT_T MM4SwitchPartitionForAlternateBootMode(void); // expose for eMMC alternate boot configuration
431UINT_T MM4_MMCReadEXTCSD (UINT_T *pBuffer); // expose for eMMC alternate boot configuration
432
433void SDMMC_ISR(void);
434UINT_T SDMMCGetCardErrorState(P_SDMMC_Properties_T pSDMMCP);
435
436#endif // __SDMMC_API_H__