1 /** @file
2 
3 The definition for SD media device driver model and blkio protocol routines.
4 
5 Copyright (c) 2013-2015 Intel Corporation.
6 
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution.  The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11 
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 
15 **/
16 
17 #ifndef _SD_MEDIA_DEVICE_H_
18 #define _SD_MEDIA_DEVICE_H_
19 
20 
21 #include <Uefi.h>
22 
23 #include <Protocol/PciIo.h>
24 #include <Protocol/BlockIo.h>
25 
26 #include <Library/DebugLib.h>
27 #include <Library/BaseMemoryLib.h>
28 #include <Library/UefiDriverEntryPoint.h>
29 #include <Library/UefiBootServicesTableLib.h>
30 #include <Library/UefiLib.h>
31 #include <Library/BaseLib.h>
32 #include <Library/MemoryAllocationLib.h>
33 #include <Library/PcdLib.h>
34 #include <IndustryStandard/Pci22.h>
35 
36 #include "ComponentName.h"
37 #include "SDHostIo.h"
38 
39 
40 extern EFI_DRIVER_BINDING_PROTOCOL   gSDMediaDeviceDriverBinding;
41 extern EFI_COMPONENT_NAME_PROTOCOL   gSDMediaDeviceName;
42 extern EFI_COMPONENT_NAME2_PROTOCOL  gSDMediaDeviceName2;
43 
44 //
45 // Define the region of memory used for DMA memory
46 //
47 #define DMA_MEMORY_TOP          0x0000000001FFFFFFULL
48 
49 #define CARD_DATA_SIGNATURE  SIGNATURE_32 ('c', 'a', 'r', 'd')
50 
51 //
52 // Command timeout will be max 100 ms
53 //
54 #define  TIMEOUT_COMMAND     100
55 #define  TIMEOUT_DATA        5000
56 
57 typedef enum{
58   UnknownCard = 0,
59   MMCCard,                // MMC card
60   MMCCardHighCap,          // MMC Card High Capacity
61   CEATACard,              // CE-ATA device
62   SDMemoryCard,           // SD 1.1 card
63   SDMemoryCard2,          // SD 2.0 or above standard card
64   SDMemoryCard2High       // SD 2.0 or above high capacity card
65 }CARD_TYPE;
66 
67 
68 typedef struct {
69   //
70   //BlockIO
71   //
72   UINTN                     Signature;
73   EFI_BLOCK_IO_PROTOCOL     BlockIo;
74 
75   EFI_BLOCK_IO_MEDIA        BlockIoMedia;
76 
77   EFI_SD_HOST_IO_PROTOCOL   *SDHostIo;
78   EFI_UNICODE_STRING_TABLE  *ControllerNameTable;
79   CARD_TYPE                 CardType;
80 
81   UINT8                     CurrentBusWidth;
82   BOOLEAN                   DualVoltage;
83   BOOLEAN                   NeedFlush;
84   UINT8                     Reserved[3];
85 
86   UINT16                    Address;
87   UINT32                    BlockLen;
88   UINT32                    MaxFrequency;
89   UINT64                    BlockNumber;
90   //
91   //Common used
92   //
93   CARD_STATUS               CardStatus;
94   OCR                       OCRRegister;
95   CID                       CIDRegister;
96   CSD                       CSDRegister;
97   EXT_CSD                   ExtCSDRegister;
98   UINT8                     *RawBufferPointer;
99   UINT8                     *AlignedBuffer;
100   //
101   //CE-ATA specific
102   //
103   TASK_FILE                 TaskFile;
104   IDENTIFY_DEVICE_DATA      IndentifyDeviceData;
105   //
106   //SD specific
107   //
108   SCR                       SCRRegister;
109   SD_STATUS_REG             SDSattus;
110   SWITCH_STATUS             SwitchStatus;
111 }CARD_DATA;
112 
113 #define CARD_DATA_FROM_THIS(a) \
114     CR(a, CARD_DATA, BlockIo, CARD_DATA_SIGNATURE)
115 
116 /**
117   Test to see if this driver supports ControllerHandle. Any
118   ControllerHandle that has BlockIoProtocol installed will be supported.
119 
120   @param  This                 Protocol instance pointer.
121   @param  Controller           Handle of device to test.
122   @param  RemainingDevicePath  Not used.
123 
124   @return EFI_SUCCESS          This driver supports this device.
125   @return EFI_UNSUPPORTED      This driver does not support this device.
126 
127 **/
128 EFI_STATUS
129 EFIAPI
130 SDMediaDeviceSupported (
131   IN EFI_DRIVER_BINDING_PROTOCOL     *This,
132   IN EFI_HANDLE                      Controller,
133   IN EFI_DEVICE_PATH_PROTOCOL        *RemainingDevicePath
134   );
135 
136 /**
137   Starting the SD Media Device Driver.
138 
139   @param  This                 Protocol instance pointer.
140   @param  Controller           Handle of device to test.
141   @param  RemainingDevicePath  Not used.
142 
143   @retval EFI_SUCCESS          This driver supports this device.
144   @retval EFI_UNSUPPORTED      This driver does not support this device.
145   @retval EFI_DEVICE_ERROR     This driver cannot be started due to device Error.
146                                EFI_OUT_OF_RESOURCES- Failed due to resource shortage.
147 
148 **/
149 EFI_STATUS
150 EFIAPI
151 SDMediaDeviceStart (
152   IN EFI_DRIVER_BINDING_PROTOCOL     *This,
153   IN EFI_HANDLE                      Controller,
154   IN EFI_DEVICE_PATH_PROTOCOL        *RemainingDevicePath
155   );
156 
157 /**
158   Stop this driver on ControllerHandle. Support stoping any child handles
159   created by this driver.
160 
161   @param  This                 Protocol instance pointer.
162   @param  Controller           Handle of device to stop driver on.
163   @param  NumberOfChildren     Number of Children in the ChildHandleBuffer.
164   @param  ChildHandleBuffer    List of handles for the children we need to stop.
165 
166   @return EFI_SUCCESS
167   @return others
168 
169 **/
170 EFI_STATUS
171 EFIAPI
172 SDMediaDeviceStop (
173   IN EFI_DRIVER_BINDING_PROTOCOL     *This,
174   IN EFI_HANDLE                      Controller,
175   IN UINTN                           NumberOfChildren,
176   IN EFI_HANDLE                      *ChildHandleBuffer
177   );
178 
179 /**
180   MMC/SD card init function
181 
182   @param  CardData             Pointer to CARD_DATA.
183 
184   @return EFI_SUCCESS
185   @return others
186 
187 **/
188 EFI_STATUS
189 MMCSDCardInit (
190   IN  CARD_DATA    *CardData
191   );
192 
193 /**
194   Send command by using Host IO protocol
195 
196   @param  This                  A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
197   @param  CommandIndex          The command index to set the command index field of command register.
198   @param  Argument              Command argument to set the argument field of command register.
199   @param  DataType              TRANSFER_TYPE, indicates no data, data in or data out.
200   @param  Buffer                Contains the data read from / write to the device.
201   @param  BufferSize            The size of the buffer.
202   @param  ResponseType          RESPONSE_TYPE.
203   @param  TimeOut               Time out value in 1 ms unit.
204   @param  ResponseData          Depending on the ResponseType, such as CSD or card status.
205 
206   @retval EFI_SUCCESS
207   @retval EFI_INVALID_PARAMETER
208   @retval EFI_UNSUPPORTED
209   @retval EFI_DEVICE_ERROR
210 
211 **/
212 EFI_STATUS
213 SendCommand (
214   IN   CARD_DATA                  *CardData,
215   IN   UINT16                     CommandIndex,
216   IN   UINT32                     Argument,
217   IN   TRANSFER_TYPE              DataType,
218   IN   UINT8                      *Buffer, OPTIONAL
219   IN   UINT32                     BufferSize,
220   IN   RESPONSE_TYPE              ResponseType,
221   IN   UINT32                     TimeOut,
222   OUT  UINT32                     *ResponseData
223   );
224 
225 /**
226   Send the card APP_CMD command with the following command indicated by CommandIndex
227 
228   @param  CardData              Pointer to CARD_DATA.
229   @param  CommandIndex          The command index to set the command index field of command register.
230   @param  Argument              Command argument to set the argument field of command register.
231   @param  DataType              TRANSFER_TYPE, indicates no data, data in or data out.
232   @param  Buffer                Contains the data read from / write to the device.
233   @param  BufferSize            The size of the buffer.
234   @param  ResponseType          RESPONSE_TYPE.
235   @param  TimeOut               Time out value in 1 ms unit.
236   @param  ResponseData          Depending on the ResponseType, such as CSD or card status.
237 
238   @retval EFI_SUCCESS
239   @retval EFI_INVALID_PARAMETER
240   @retval EFI_UNSUPPORTED
241   @retval EFI_DEVICE_ERROR
242 
243 **/
244 EFI_STATUS
245 SendAppCommand (
246   IN   CARD_DATA                  *CardData,
247   IN   UINT16                     CommandIndex,
248   IN   UINT32                     Argument,
249   IN   TRANSFER_TYPE              DataType,
250   IN   UINT8                      *Buffer, OPTIONAL
251   IN   UINT32                     BufferSize,
252   IN   RESPONSE_TYPE              ResponseType,
253   IN   UINT32                     TimeOut,
254   OUT  UINT32                     *ResponseData
255   );
256 
257 /**
258   Send the card FAST_IO command
259 
260   @param  CardData               Pointer to CARD_DATA.
261   @param  RegisterAddress        Register Address.
262   @param  RegisterData           Pointer to register Data.
263   @param  Write                  TRUE for write, FALSE for read.
264 
265   @retval EFI_SUCCESS
266   @retval EFI_UNSUPPORTED
267   @retval EFI_INVALID_PARAMETER
268   @retval EFI_DEVICE_ERROR
269 
270 **/
271 EFI_STATUS
272 FastIO (
273   IN      CARD_DATA   *CardData,
274   IN      UINT8       RegisterAddress,
275   IN  OUT UINT8       *RegisterData,
276   IN      BOOLEAN     Write
277   );
278 
279 /**
280   Judge whether it is CE-ATA device or not.
281 
282   @param  CardData             Pointer to CARD_DATA.
283 
284   @retval TRUE
285   @retval FALSE
286 
287 **/
288 BOOLEAN
289 IsCEATADevice (
290   IN  CARD_DATA    *CardData
291   );
292 
293 /**
294   Send software reset
295 
296   @param  CardData             Pointer to CARD_DATA.
297 
298   @retval EFI_SUCCESS                Success
299   @retval EFI_DEVICE_ERROR           Hardware Error
300   @retval EFI_INVALID_PARAMETER      Parameter is error
301   @retval EFI_NO_MEDIA               No media
302   @retval EFI_MEDIA_CHANGED          Media Change
303   @retval EFI_BAD_BUFFER_SIZE        Buffer size is bad
304 
305 **/
306 EFI_STATUS
307 SoftwareReset (
308   IN  CARD_DATA    *CardData
309   );
310 
311 /**
312   SendATACommand specificed in Taskfile
313 
314   @param  CardData             Pointer to CARD_DATA.
315   @param  TaskFile             Pointer to TASK_FILE.
316   @param  Write                TRUE means write, FALSE means read.
317   @param  Buffer               If NULL, means no data transfer, neither read nor write.
318   @param  SectorCount          Buffer size in 512 bytes unit.
319 
320   @retval EFI_SUCCESS                Success
321   @retval EFI_DEVICE_ERROR           Hardware Error
322   @retval EFI_INVALID_PARAMETER      Parameter is error
323   @retval EFI_NO_MEDIA               No media
324   @retval EFI_MEDIA_CHANGED          Media Change
325   @retval EFI_BAD_BUFFER_SIZE        Buffer size is bad
326 
327 **/
328 EFI_STATUS
329 SendATACommand (
330   IN  CARD_DATA   *CardData,
331   IN  TASK_FILE   *TaskFile,
332   IN  BOOLEAN     Write,
333   IN  UINT8       *Buffer,
334   IN  UINT16      SectorCount
335   );
336 
337 /**
338   IDENTIFY_DEVICE command
339 
340   @param  CardData             Pointer to CARD_DATA.
341 
342   @retval EFI_SUCCESS                Success
343   @retval EFI_DEVICE_ERROR           Hardware Error
344   @retval EFI_INVALID_PARAMETER      Parameter is error
345   @retval EFI_NO_MEDIA               No media
346   @retval EFI_MEDIA_CHANGED          Media Change
347   @retval EFI_BAD_BUFFER_SIZE        Buffer size is bad
348 
349 **/
350 EFI_STATUS
351 IndentifyDevice (
352   IN  CARD_DATA    *CardData
353   );
354 
355 /**
356   FLUSH_CACHE_EXT command
357 
358   @param  CardData             Pointer to CARD_DATA.
359 
360   @retval EFI_SUCCESS                Success
361   @retval EFI_DEVICE_ERROR           Hardware Error
362   @retval EFI_INVALID_PARAMETER      Parameter is error
363   @retval EFI_NO_MEDIA               No media
364   @retval EFI_MEDIA_CHANGED          Media Change
365   @retval EFI_BAD_BUFFER_SIZE        Buffer size is bad
366 
367 **/
368 EFI_STATUS
369 FlushCache (
370   IN  CARD_DATA    *CardData
371   );
372 
373 /**
374   STANDBY_IMMEDIATE command
375 
376   @param  CardData             Pointer to CARD_DATA.
377 
378   @retval EFI_SUCCESS                Success
379   @retval EFI_DEVICE_ERROR           Hardware Error
380   @retval EFI_INVALID_PARAMETER      Parameter is error
381   @retval EFI_NO_MEDIA               No media
382   @retval EFI_MEDIA_CHANGED          Media Change
383   @retval EFI_BAD_BUFFER_SIZE        Buffer size is bad
384 
385 **/
386 EFI_STATUS
387 StandByImmediate (
388   IN  CARD_DATA    *CardData
389   );
390 
391 /**
392   READ_DMA_EXT command
393 
394   @param  CardData             Pointer to CARD_DATA.
395   @param  LBA                  The starting logical block address to read from on the device.
396   @param  Buffer               A pointer to the destination buffer for the data. The caller
397                                is responsible for either having implicit or explicit ownership
398                                of the buffer.
399   @param  SectorCount          Size in 512 bytes unit.
400 
401   @retval EFI_SUCCESS                Success
402   @retval EFI_DEVICE_ERROR           Hardware Error
403   @retval EFI_INVALID_PARAMETER      Parameter is error
404   @retval EFI_NO_MEDIA               No media
405   @retval EFI_MEDIA_CHANGED          Media Change
406   @retval EFI_BAD_BUFFER_SIZE        Buffer size is bad
407 
408 **/
409 EFI_STATUS
410 ReadDMAExt (
411   IN  CARD_DATA   *CardData,
412   IN  EFI_LBA     LBA,
413   IN  UINT8       *Buffer,
414   IN  UINT16      SectorCount
415   );
416 
417 /**
418   WRITE_DMA_EXT command
419 
420   @param  CardData             Pointer to CARD_DATA.
421   @param  LBA                  The starting logical block address to read from on the device.
422   @param  Buffer               A pointer to the destination buffer for the data. The caller
423                                is responsible for either having implicit or explicit ownership
424                                of the buffer.
425   @param  SectorCount          Size in 512 bytes unit.
426 
427   @retval EFI_SUCCESS                Success
428   @retval EFI_DEVICE_ERROR           Hardware Error
429   @retval EFI_INVALID_PARAMETER      Parameter is error
430   @retval EFI_NO_MEDIA               No media
431   @retval EFI_MEDIA_CHANGED          Media Change
432   @retval EFI_BAD_BUFFER_SIZE        Buffer size is bad
433 
434 **/
435 EFI_STATUS
436 WriteDMAExt (
437   IN  CARD_DATA   *CardData,
438   IN  EFI_LBA     LBA,
439   IN  UINT8       *Buffer,
440   IN  UINT16      SectorCount
441   );
442 
443 /**
444   CEATA card BlockIo init function.
445 
446   @param  CardData               Pointer to CARD_DATA.
447 
448   @retval EFI_SUCCESS
449   @retval Others
450 **/
451 EFI_STATUS
452 CEATABlockIoInit (
453   IN  CARD_DATA    *CardData
454   );
455 
456 /**
457   MMC/SD card BlockIo init function.
458 
459   @param  CardData               Pointer to CARD_DATA.
460 
461   @retval EFI_SUCCESS
462   @retval Others
463 **/
464 EFI_STATUS
465 MMCSDBlockIoInit (
466   IN  CARD_DATA    *CardData
467   );
468 #endif
469