2 Header file for SdDxe Driver.
4 This file defines common data structures, macro definitions and some module
5 internal function header files.
7 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
16 #include <IndustryStandard/Sd.h>
18 #include <Protocol/SdMmcPassThru.h>
19 #include <Protocol/BlockIo.h>
20 #include <Protocol/BlockIo2.h>
21 #include <Protocol/EraseBlock.h>
22 #include <Protocol/DiskInfo.h>
24 #include <Protocol/DevicePath.h>
26 #include <Library/DebugLib.h>
27 #include <Library/UefiDriverEntryPoint.h>
28 #include <Library/BaseLib.h>
29 #include <Library/UefiLib.h>
30 #include <Library/BaseMemoryLib.h>
31 #include <Library/MemoryAllocationLib.h>
32 #include <Library/UefiBootServicesTableLib.h>
33 #include <Library/DevicePathLib.h>
34 #include <Library/UefiRuntimeServicesTableLib.h>
36 #include "SdBlockIo.h"
37 #include "SdDiskInfo.h"
42 extern EFI_DRIVER_BINDING_PROTOCOL gSdDxeDriverBinding
;
43 extern EFI_COMPONENT_NAME_PROTOCOL gSdDxeComponentName
;
44 extern EFI_COMPONENT_NAME2_PROTOCOL gSdDxeComponentName2
;
46 #define SD_DEVICE_SIGNATURE SIGNATURE_32 ('S', 'D', 't', 'f')
48 #define SD_DEVICE_DATA_FROM_BLKIO(a) \
49 CR(a, SD_DEVICE, BlockIo, SD_DEVICE_SIGNATURE)
51 #define SD_DEVICE_DATA_FROM_BLKIO2(a) \
52 CR(a, SD_DEVICE, BlockIo2, SD_DEVICE_SIGNATURE)
54 #define SD_DEVICE_DATA_FROM_ERASEBLK(a) \
55 CR(a, SD_DEVICE, EraseBlock, SD_DEVICE_SIGNATURE)
57 #define SD_DEVICE_DATA_FROM_DISKINFO(a) \
58 CR(a, SD_DEVICE, DiskInfo, SD_DEVICE_SIGNATURE)
61 // Take 2.5 seconds as generic time out value, 1 microsecond as unit.
63 #define SD_GENERIC_TIMEOUT 2500 * 1000
65 #define SD_REQUEST_SIGNATURE SIGNATURE_32 ('S', 'D', 'R', 'E')
67 #define SD_MODEL_NAME_MAX_LEN 32
69 typedef struct _SD_DEVICE SD_DEVICE
;
70 typedef struct _SD_DRIVER_PRIVATE_DATA SD_DRIVER_PRIVATE_DATA
;
73 // Asynchronous I/O request.
79 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk
;
80 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk
;
81 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet
;
85 EFI_BLOCK_IO2_TOKEN
*Token
;
90 #define SD_REQUEST_FROM_LINK(a) \
91 CR(a, SD_REQUEST, Link, SD_REQUEST_SIGNATURE)
96 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
98 BOOLEAN SectorAddressing
;
99 EFI_BLOCK_IO_PROTOCOL BlockIo
;
100 EFI_BLOCK_IO2_PROTOCOL BlockIo2
;
101 EFI_BLOCK_IO_MEDIA BlockMedia
;
102 EFI_ERASE_BLOCK_PROTOCOL EraseBlock
;
103 EFI_DISK_INFO_PROTOCOL DiskInfo
;
109 EFI_UNICODE_STRING_TABLE
*ControllerNameTable
;
111 // The model name consists of three fields in CID register
112 // 1) OEM/Application ID (2 bytes)
113 // 2) Product Name (5 bytes)
114 // 3) Product Serial Number (4 bytes)
115 // The delimiters of these fields are whitespace.
117 CHAR16 ModelName
[SD_MODEL_NAME_MAX_LEN
];
118 SD_DRIVER_PRIVATE_DATA
*Private
;
122 // SD DXE driver private data structure
124 struct _SD_DRIVER_PRIVATE_DATA
{
125 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
126 EFI_HANDLE Controller
;
127 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
128 EFI_HANDLE DriverBindingHandle
;
132 Tests to see if this driver supports a given controller. If a child device is provided,
133 it further tests to see if this driver supports creating a handle for the specified child device.
135 This function checks to see if the driver specified by This supports the device specified by
136 ControllerHandle. Drivers will typically use the device path attached to
137 ControllerHandle and/or the services from the bus I/O abstraction attached to
138 ControllerHandle to determine if the driver supports ControllerHandle. This function
139 may be called many times during platform initialization. In order to reduce boot times, the tests
140 performed by this function must be very small, and take as little time as possible to execute. This
141 function must not change the state of any hardware devices, and this function must be aware that the
142 device specified by ControllerHandle may already be managed by the same driver or a
143 different driver. This function must match its calls to AllocatePages() with FreePages(),
144 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
145 Since ControllerHandle may have been previously started by the same driver, if a protocol is
146 already in the opened state, then it must not be closed with CloseProtocol(). This is required
147 to guarantee the state of ControllerHandle is not modified by this function.
149 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
150 @param[in] ControllerHandle The handle of the controller to test. This handle
151 must support a protocol interface that supplies
152 an I/O abstraction to the driver.
153 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
154 parameter is ignored by device drivers, and is optional for bus
155 drivers. For bus drivers, if this parameter is not NULL, then
156 the bus driver must determine if the bus controller specified
157 by ControllerHandle and the child controller specified
158 by RemainingDevicePath are both supported by this
161 @retval EFI_SUCCESS The device specified by ControllerHandle and
162 RemainingDevicePath is supported by the driver specified by This.
163 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
164 RemainingDevicePath is already being managed by the driver
166 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
167 RemainingDevicePath is already being managed by a different
168 driver or an application that requires exclusive access.
169 Currently not implemented.
170 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
171 RemainingDevicePath is not supported by the driver specified by This.
175 SdDxeDriverBindingSupported (
176 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
177 IN EFI_HANDLE Controller
,
178 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
182 Starts a device controller or a bus controller.
184 The Start() function is designed to be invoked from the EFI boot service ConnectController().
185 As a result, much of the error checking on the parameters to Start() has been moved into this
186 common boot service. It is legal to call Start() from other locations,
187 but the following calling restrictions must be followed or the system behavior will not be deterministic.
188 1. ControllerHandle must be a valid EFI_HANDLE.
189 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
190 EFI_DEVICE_PATH_PROTOCOL.
191 3. Prior to calling Start(), the Supported() function for the driver specified by This must
192 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
194 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
195 @param[in] ControllerHandle The handle of the controller to start. This handle
196 must support a protocol interface that supplies
197 an I/O abstraction to the driver.
198 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
199 parameter is ignored by device drivers, and is optional for bus
200 drivers. For a bus driver, if this parameter is NULL, then handles
201 for all the children of Controller are created by this driver.
202 If this parameter is not NULL and the first Device Path Node is
203 not the End of Device Path Node, then only the handle for the
204 child device specified by the first Device Path Node of
205 RemainingDevicePath is created by this driver.
206 If the first Device Path Node of RemainingDevicePath is
207 the End of Device Path Node, no child handle is created by this
210 @retval EFI_SUCCESS The device was started.
211 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
212 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
213 @retval Others The driver failed to start the device.
218 SdDxeDriverBindingStart (
219 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
220 IN EFI_HANDLE Controller
,
221 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
225 Stops a device controller or a bus controller.
227 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
228 As a result, much of the error checking on the parameters to Stop() has been moved
229 into this common boot service. It is legal to call Stop() from other locations,
230 but the following calling restrictions must be followed or the system behavior will not be deterministic.
231 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
232 same driver's Start() function.
233 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
234 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
235 Start() function, and the Start() function must have called OpenProtocol() on
236 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
238 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
239 @param[in] ControllerHandle A handle to the device being stopped. The handle must
240 support a bus specific I/O protocol for the driver
241 to use to stop the device.
242 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
243 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
244 if NumberOfChildren is 0.
246 @retval EFI_SUCCESS The device was stopped.
247 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
252 SdDxeDriverBindingStop (
253 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
254 IN EFI_HANDLE Controller
,
255 IN UINTN NumberOfChildren
,
256 IN EFI_HANDLE
*ChildHandleBuffer
260 Retrieves a Unicode string that is the user readable name of the driver.
262 This function retrieves the user readable name of a driver in the form of a
263 Unicode string. If the driver specified by This has a user readable name in
264 the language specified by Language, then a pointer to the driver name is
265 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
266 by This does not support the language specified by Language,
267 then EFI_UNSUPPORTED is returned.
269 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
270 EFI_COMPONENT_NAME_PROTOCOL instance.
272 @param Language[in] A pointer to a Null-terminated ASCII string
273 array indicating the language. This is the
274 language of the driver name that the caller is
275 requesting, and it must match one of the
276 languages specified in SupportedLanguages. The
277 number of languages supported by a driver is up
278 to the driver writer. Language is specified
279 in RFC 4646 or ISO 639-2 language code format.
281 @param DriverName[out] A pointer to the Unicode string to return.
282 This Unicode string is the name of the
283 driver specified by This in the language
284 specified by Language.
286 @retval EFI_SUCCESS The Unicode string for the Driver specified by
287 This and the language specified by Language was
288 returned in DriverName.
290 @retval EFI_INVALID_PARAMETER Language is NULL.
292 @retval EFI_INVALID_PARAMETER DriverName is NULL.
294 @retval EFI_UNSUPPORTED The driver specified by This does not support
295 the language specified by Language.
300 SdDxeComponentNameGetDriverName (
301 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
303 OUT CHAR16
**DriverName
307 Retrieves a Unicode string that is the user readable name of the controller
308 that is being managed by a driver.
310 This function retrieves the user readable name of the controller specified by
311 ControllerHandle and ChildHandle in the form of a Unicode string. If the
312 driver specified by This has a user readable name in the language specified by
313 Language, then a pointer to the controller name is returned in ControllerName,
314 and EFI_SUCCESS is returned. If the driver specified by This is not currently
315 managing the controller specified by ControllerHandle and ChildHandle,
316 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
317 support the language specified by Language, then EFI_UNSUPPORTED is returned.
319 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
320 EFI_COMPONENT_NAME_PROTOCOL instance.
322 @param ControllerHandle[in] The handle of a controller that the driver
323 specified by This is managing. This handle
324 specifies the controller whose name is to be
327 @param ChildHandle[in] The handle of the child controller to retrieve
328 the name of. This is an optional parameter that
329 may be NULL. It will be NULL for device
330 drivers. It will also be NULL for a bus drivers
331 that wish to retrieve the name of the bus
332 controller. It will not be NULL for a bus
333 driver that wishes to retrieve the name of a
336 @param Language[in] A pointer to a Null-terminated ASCII string
337 array indicating the language. This is the
338 language of the driver name that the caller is
339 requesting, and it must match one of the
340 languages specified in SupportedLanguages. The
341 number of languages supported by a driver is up
342 to the driver writer. Language is specified in
343 RFC 4646 or ISO 639-2 language code format.
345 @param ControllerName[out] A pointer to the Unicode string to return.
346 This Unicode string is the name of the
347 controller specified by ControllerHandle and
348 ChildHandle in the language specified by
349 Language from the point of view of the driver
352 @retval EFI_SUCCESS The Unicode string for the user readable name in
353 the language specified by Language for the
354 driver specified by This was returned in
357 @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.
359 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
362 @retval EFI_INVALID_PARAMETER Language is NULL.
364 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
366 @retval EFI_UNSUPPORTED The driver specified by This is not currently
367 managing the controller specified by
368 ControllerHandle and ChildHandle.
370 @retval EFI_UNSUPPORTED The driver specified by This does not support
371 the language specified by Language.
376 SdDxeComponentNameGetControllerName (
377 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
378 IN EFI_HANDLE ControllerHandle
,
379 IN EFI_HANDLE ChildHandle OPTIONAL
,
381 OUT CHAR16
**ControllerName
385 Send command SET_RELATIVE_ADDRESS to the device to set the device address.
387 @param[in] Device A pointer to the SD_DEVICE instance.
388 @param[out] Rca The relative device address to assign.
390 @retval EFI_SUCCESS The request is executed successfully.
391 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
392 @retval Others The request could not be executed successfully.
397 IN SD_DEVICE
*Device
,
402 Send command SELECT to the device to select/deselect the device.
404 @param[in] Device A pointer to the SD_DEVICE instance.
405 @param[in] Rca The relative device address to use.
407 @retval EFI_SUCCESS The request is executed successfully.
408 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
409 @retval Others The request could not be executed successfully.
414 IN SD_DEVICE
*Device
,
419 Send command SEND_STATUS to the device to get device status.
421 @param[in] Device A pointer to the SD_DEVICE instance.
422 @param[in] Rca The relative device address to use.
423 @param[out] DevStatus The buffer to store the device status.
424 @retval EFI_SUCCESS The request is executed successfully.
425 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
426 @retval Others The request could not be executed successfully.
431 IN SD_DEVICE
*Device
,
433 OUT UINT32
*DevStatus
437 Send command SEND_CSD to the device to get the CSD register data.
439 @param[in] Device A pointer to the SD_DEVICE instance.
440 @param[in] Rca The relative device address to use.
441 @param[out] Csd The buffer to store the SD_CSD register data.
443 @retval EFI_SUCCESS The request is executed successfully.
444 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
445 @retval Others The request could not be executed successfully.
450 IN SD_DEVICE
*Device
,
456 Send command SEND_CID to the device to get the CID register data.
458 @param[in] Device A pointer to the SD_DEVICE instance.
459 @param[in] Rca The relative device address to use.
460 @param[out] Cid The buffer to store the SD_CID register data.
462 @retval EFI_SUCCESS The request is executed successfully.
463 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
464 @retval Others The request could not be executed successfully.
469 IN SD_DEVICE
*Device
,