]> git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/Universal/MmcDxe/Mmc.h
EmbeddedPkg: Remove dependency on TimerLib
[mirror_edk2.git] / EmbeddedPkg / Universal / MmcDxe / Mmc.h
1 /** @file
2 Main Header file for the MMC DXE driver
3
4 Copyright (c) 2011-2015, ARM Limited. All rights reserved.
5
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #ifndef __MMC_H
17 #define __MMC_H
18
19 #include <Uefi.h>
20
21 #include <Protocol/DiskIo.h>
22 #include <Protocol/BlockIo.h>
23 #include <Protocol/DevicePath.h>
24 #include <Protocol/MmcHost.h>
25
26 #include <Library/UefiLib.h>
27 #include <Library/DebugLib.h>
28 #include <Library/UefiBootServicesTableLib.h>
29
30 #define MMC_TRACE(txt) DEBUG((EFI_D_BLKIO, "MMC: " txt "\n"))
31
32 #define MMC_IOBLOCKS_READ 0
33 #define MMC_IOBLOCKS_WRITE 1
34
35 #define MMC_OCR_POWERUP 0x80000000
36
37 #define MMC_CSD_GET_CCC(Response) (Response[2] >> 20)
38 #define MMC_CSD_GET_TRANSPEED(Response) (Response[3] & 0xFF)
39 #define MMC_CSD_GET_READBLLEN(Response) ((Response[2] >> 16) & 0xF)
40 #define MMC_CSD_GET_WRITEBLLEN(Response) ((Response[0] >> 22) & 0xF)
41 #define MMC_CSD_GET_FILEFORMAT(Response) ((Response[0] >> 10) & 0x3)
42 #define MMC_CSD_GET_FILEFORMATGRP(Response) ((Response[0] >> 15) & 0x1)
43 #define MMC_CSD_GET_DEVICESIZE(csd) (((Response[1] >> 30) & 0x3) | ((Response[2] & 0x3FF) << 2))
44 #define HC_MMC_CSD_GET_DEVICESIZE(Response) ((Response[1] >> 16) | ((Response[2] & 0x40) << 16));
45 #define MMC_CSD_GET_DEVICESIZEMULT(csd) ((Response[1] >> 15) & 0x7)
46
47 #define MMC_R0_READY_FOR_DATA (1 << 8)
48
49 #define MMC_R0_CURRENTSTATE(Response) ((Response[0] >> 9) & 0xF)
50
51 #define MMC_R0_STATE_IDLE 0
52 #define MMC_R0_STATE_READY 1
53 #define MMC_R0_STATE_IDENT 2
54 #define MMC_R0_STATE_STDBY 3
55 #define MMC_R0_STATE_TRAN 4
56 #define MMC_R0_STATE_DATA 5
57
58 typedef enum {
59 UNKNOWN_CARD,
60 MMC_CARD, //MMC card
61 MMC_CARD_HIGH, //MMC Card with High capacity
62 EMMC_CARD, //eMMC 4.41 card
63 SD_CARD, //SD 1.1 card
64 SD_CARD_2, //SD 2.0 or above standard card
65 SD_CARD_2_HIGH //SD 2.0 or above high capacity card
66 } CARD_TYPE;
67
68 typedef struct {
69 UINT32 Reserved0: 7; // 0
70 UINT32 V170_V195: 1; // 1.70V - 1.95V
71 UINT32 V200_V260: 7; // 2.00V - 2.60V
72 UINT32 V270_V360: 9; // 2.70V - 3.60V
73 UINT32 RESERVED_1: 5; // Reserved
74 UINT32 AccessMode: 2; // 00b (byte mode), 10b (sector mode)
75 UINT32 PowerUp: 1; // This bit is set to LOW if the card has not finished the power up routine
76 } OCR;
77
78 typedef struct {
79 UINT32 NOT_USED; // 1 [0:0]
80 UINT32 CRC; // CRC7 checksum [7:1]
81 UINT32 MDT; // Manufacturing date [19:8]
82 UINT32 RESERVED_1; // Reserved [23:20]
83 UINT32 PSN; // Product serial number [55:24]
84 UINT8 PRV; // Product revision [63:56]
85 UINT8 PNM[5]; // Product name [64:103]
86 UINT16 OID; // OEM/Application ID [119:104]
87 UINT8 MID; // Manufacturer ID [127:120]
88 } CID;
89
90 typedef struct {
91 UINT8 NOT_USED: 1; // Not used, always 1 [0:0]
92 UINT8 CRC: 7; // CRC [7:1]
93
94 UINT8 RESERVED_1: 2; // Reserved [9:8]
95 UINT8 FILE_FORMAT: 2; // File format [11:10]
96 UINT8 TMP_WRITE_PROTECT: 1; // Temporary write protection [12:12]
97 UINT8 PERM_WRITE_PROTECT: 1; // Permanent write protection [13:13]
98 UINT8 COPY: 1; // Copy flag (OTP) [14:14]
99 UINT8 FILE_FORMAT_GRP: 1; // File format group [15:15]
100
101 UINT16 RESERVED_2: 5; // Reserved [20:16]
102 UINT16 WRITE_BL_PARTIAL: 1; // Partial blocks for write allowed [21:21]
103 UINT16 WRITE_BL_LEN: 4; // Max. write data block length [25:22]
104 UINT16 R2W_FACTOR: 3; // Write speed factor [28:26]
105 UINT16 RESERVED_3: 2; // Reserved [30:29]
106 UINT16 WP_GRP_ENABLE: 1; // Write protect group enable [31:31]
107
108 UINT32 WP_GRP_SIZE: 7; // Write protect group size [38:32]
109 UINT32 SECTOR_SIZE: 7; // Erase sector size [45:39]
110 UINT32 ERASE_BLK_EN: 1; // Erase single block enable [46:46]
111 UINT32 C_SIZE_MULT: 3; // Device size multiplier [49:47]
112 UINT32 VDD_W_CURR_MAX: 3; // Max. write current @ VDD max [52:50]
113 UINT32 VDD_W_CURR_MIN: 3; // Max. write current @ VDD min [55:53]
114 UINT32 VDD_R_CURR_MAX: 3; // Max. read current @ VDD max [58:56]
115 UINT32 VDD_R_CURR_MIN: 3; // Max. read current @ VDD min [61:59]
116 UINT32 C_SIZELow2: 2; // Device size [63:62]
117
118 UINT32 C_SIZEHigh10: 10;// Device size [73:64]
119 UINT32 RESERVED_4: 2; // Reserved [75:74]
120 UINT32 DSR_IMP: 1; // DSR implemented [76:76]
121 UINT32 READ_BLK_MISALIGN: 1; // Read block misalignment [77:77]
122 UINT32 WRITE_BLK_MISALIGN: 1; // Write block misalignment [78:78]
123 UINT32 READ_BL_PARTIAL: 1; // Partial blocks for read allowed [79:79]
124 UINT32 READ_BL_LEN: 4; // Max. read data block length [83:80]
125 UINT32 CCC: 12;// Card command classes [95:84]
126
127 UINT8 TRAN_SPEED ; // Max. bus clock frequency [103:96]
128 UINT8 NSAC ; // Data read access-time 2 in CLK cycles (NSAC*100) [111:104]
129 UINT8 TAAC ; // Data read access-time 1 [119:112]
130
131 UINT8 RESERVED_5: 6; // Reserved [125:120]
132 UINT8 CSD_STRUCTURE: 2; // CSD structure [127:126]
133 } CSD;
134
135 typedef struct {
136 UINT16 RCA;
137 CARD_TYPE CardType;
138 OCR OCRData;
139 CID CIDData;
140 CSD CSDData;
141 } CARD_INFO;
142
143 typedef struct _MMC_HOST_INSTANCE {
144 UINTN Signature;
145 LIST_ENTRY Link;
146 EFI_HANDLE MmcHandle;
147 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
148
149 MMC_STATE State;
150 EFI_BLOCK_IO_PROTOCOL BlockIo;
151 CARD_INFO CardInfo;
152 EFI_MMC_HOST_PROTOCOL *MmcHost;
153
154 BOOLEAN Initialized;
155 } MMC_HOST_INSTANCE;
156
157 #define MMC_HOST_INSTANCE_SIGNATURE SIGNATURE_32('m', 'm', 'c', 'h')
158 #define MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS(a) CR (a, MMC_HOST_INSTANCE, BlockIo, MMC_HOST_INSTANCE_SIGNATURE)
159 #define MMC_HOST_INSTANCE_FROM_LINK(a) CR (a, MMC_HOST_INSTANCE, Link, MMC_HOST_INSTANCE_SIGNATURE)
160
161
162 EFI_STATUS
163 EFIAPI
164 MmcGetDriverName (
165 IN EFI_COMPONENT_NAME_PROTOCOL *This,
166 IN CHAR8 *Language,
167 OUT CHAR16 **DriverName
168 );
169
170 EFI_STATUS
171 EFIAPI
172 MmcGetControllerName (
173 IN EFI_COMPONENT_NAME_PROTOCOL *This,
174 IN EFI_HANDLE ControllerHandle,
175 IN EFI_HANDLE ChildHandle OPTIONAL,
176 IN CHAR8 *Language,
177 OUT CHAR16 **ControllerName
178 );
179
180 extern EFI_COMPONENT_NAME_PROTOCOL gMmcComponentName;
181 extern EFI_COMPONENT_NAME2_PROTOCOL gMmcComponentName2;
182
183 extern EFI_DRIVER_DIAGNOSTICS2_PROTOCOL gMmcDriverDiagnostics2;
184
185 extern LIST_ENTRY mMmcHostPool;
186
187 /**
188 Reset the block device.
189
190 This function implements EFI_BLOCK_IO_PROTOCOL.Reset().
191 It resets the block device hardware.
192 ExtendedVerification is ignored in this implementation.
193
194 @param This Indicates a pointer to the calling context.
195 @param ExtendedVerification Indicates that the driver may perform a more exhaustive
196 verification operation of the device during reset.
197
198 @retval EFI_SUCCESS The block device was reset.
199 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be reset.
200
201 **/
202 EFI_STATUS
203 EFIAPI
204 MmcReset (
205 IN EFI_BLOCK_IO_PROTOCOL *This,
206 IN BOOLEAN ExtendedVerification
207 );
208
209 /**
210 Reads the requested number of blocks from the device.
211
212 This function implements EFI_BLOCK_IO_PROTOCOL.ReadBlocks().
213 It reads the requested number of blocks from the device.
214 All the blocks are read, or an error is returned.
215
216 @param This Indicates a pointer to the calling context.
217 @param MediaId The media ID that the read request is for.
218 @param Lba The starting logical block address to read from on the device.
219 @param BufferSize The size of the Buffer in bytes.
220 This must be a multiple of the intrinsic block size of the device.
221 @param Buffer A pointer to the destination buffer for the data. The caller is
222 responsible for either having implicit or explicit ownership of the buffer.
223
224 @retval EFI_SUCCESS The data was read correctly from the device.
225 @retval EFI_DEVICE_ERROR The device reported an error while attempting to perform the read operation.
226 @retval EFI_NO_MEDIA There is no media in the device.
227 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
228 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic block size of the device.
229 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
230 or the buffer is not on proper alignment.
231
232 **/
233 EFI_STATUS
234 EFIAPI
235 MmcReadBlocks (
236 IN EFI_BLOCK_IO_PROTOCOL *This,
237 IN UINT32 MediaId,
238 IN EFI_LBA Lba,
239 IN UINTN BufferSize,
240 OUT VOID *Buffer
241 );
242
243 /**
244 Writes a specified number of blocks to the device.
245
246 This function implements EFI_BLOCK_IO_PROTOCOL.WriteBlocks().
247 It writes a specified number of blocks to the device.
248 All blocks are written, or an error is returned.
249
250 @param This Indicates a pointer to the calling context.
251 @param MediaId The media ID that the write request is for.
252 @param Lba The starting logical block address to be written.
253 @param BufferSize The size of the Buffer in bytes.
254 This must be a multiple of the intrinsic block size of the device.
255 @param Buffer Pointer to the source buffer for the data.
256
257 @retval EFI_SUCCESS The data were written correctly to the device.
258 @retval EFI_WRITE_PROTECTED The device cannot be written to.
259 @retval EFI_NO_MEDIA There is no media in the device.
260 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
261 @retval EFI_DEVICE_ERROR The device reported an error while attempting to perform the write operation.
262 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic
263 block size of the device.
264 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
265 or the buffer is not on proper alignment.
266
267 **/
268 EFI_STATUS
269 EFIAPI
270 MmcWriteBlocks (
271 IN EFI_BLOCK_IO_PROTOCOL *This,
272 IN UINT32 MediaId,
273 IN EFI_LBA Lba,
274 IN UINTN BufferSize,
275 IN VOID *Buffer
276 );
277
278 /**
279 Flushes all modified data to a physical block device.
280
281 @param This Indicates a pointer to the calling context.
282
283 @retval EFI_SUCCESS All outstanding data were written correctly to the device.
284 @retval EFI_DEVICE_ERROR The device reported an error while attempting to write data.
285 @retval EFI_NO_MEDIA There is no media in the device.
286
287 **/
288 EFI_STATUS
289 EFIAPI
290 MmcFlushBlocks (
291 IN EFI_BLOCK_IO_PROTOCOL *This
292 );
293
294 EFI_STATUS
295 MmcNotifyState (
296 IN MMC_HOST_INSTANCE *MmcHostInstance,
297 IN MMC_STATE State
298 );
299
300 EFI_STATUS
301 InitializeMmcDevice (
302 IN MMC_HOST_INSTANCE *MmcHost
303 );
304
305 VOID
306 EFIAPI
307 CheckCardsCallback (
308 IN EFI_EVENT Event,
309 IN VOID *Context
310 );
311
312 VOID
313 PrintCSD (
314 IN UINT32* Csd
315 );
316
317 VOID
318 PrintRCA (
319 IN UINT32 Rca
320 );
321
322 VOID
323 PrintOCR (
324 IN UINT32 Ocr
325 );
326
327 VOID
328 PrintResponseR1 (
329 IN UINT32 Response
330 );
331
332 VOID
333 PrintCID (
334 IN UINT32* Cid
335 );
336
337 #endif