1 /** @file
2 
3 The definition for SD host controller driver model and HC 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_CONTROLLER_H_
18 #define _SD_CONTROLLER_H_
19 
20 
21 #include <Uefi.h>
22 
23 
24 #include <Protocol/PciIo.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 
37 #include "ComponentName.h"
38 #include "SDHostIo.h"
39 
40 
41 extern EFI_DRIVER_BINDING_PROTOCOL   gSDControllerDriverBinding;
42 extern EFI_COMPONENT_NAME_PROTOCOL   gSDControllerName;
43 extern EFI_COMPONENT_NAME2_PROTOCOL  gSDControllerName2;
44 
45 
46 #define SDHOST_DATA_SIGNATURE  SIGNATURE_32 ('s', 'd', 'h', 's')
47 
48 #define BLOCK_SIZE   0x200
49 #define TIME_OUT_1S  1000
50 
51 #pragma pack(1)
52 //
53 // PCI Class Code structure
54 //
55 typedef struct {
56   UINT8 PI;
57   UINT8 SubClassCode;
58   UINT8 BaseCode;
59 } PCI_CLASSC;
60 
61 #pragma pack()
62 
63 
64 typedef struct {
65   UINTN                      Signature;
66   EFI_SD_HOST_IO_PROTOCOL    SDHostIo;
67   EFI_PCI_IO_PROTOCOL        *PciIo;
68   BOOLEAN                    IsAutoStopCmd;
69   UINT32                     BaseClockInMHz;
70   UINT32                     CurrentClockInKHz;
71   UINT32                     BlockLength;
72   EFI_UNICODE_STRING_TABLE   *ControllerNameTable;
73 }SDHOST_DATA;
74 
75 #define SDHOST_DATA_FROM_THIS(a) \
76     CR(a, SDHOST_DATA, SDHostIo, SDHOST_DATA_SIGNATURE)
77 
78 /**
79   Test to see if this driver supports ControllerHandle. Any
80   ControllerHandle that has SDHostIoProtocol installed will be supported.
81 
82   @param  This                 Protocol instance pointer.
83   @param  Controller           Handle of device to test.
84   @param  RemainingDevicePath  Not used.
85 
86   @return EFI_SUCCESS          This driver supports this device.
87   @return EFI_UNSUPPORTED      This driver does not support this device.
88 
89 **/
90 EFI_STATUS
91 EFIAPI
92 SDControllerSupported (
93   IN EFI_DRIVER_BINDING_PROTOCOL     *This,
94   IN EFI_HANDLE                      Controller,
95   IN EFI_DEVICE_PATH_PROTOCOL        *RemainingDevicePath
96   );
97 
98 /**
99   Starting the SD Host Controller Driver.
100 
101   @param  This                 Protocol instance pointer.
102   @param  Controller           Handle of device to test.
103   @param  RemainingDevicePath  Not used.
104 
105   @retval EFI_SUCCESS          This driver supports this device.
106   @retval EFI_UNSUPPORTED      This driver does not support this device.
107   @retval EFI_DEVICE_ERROR     This driver cannot be started due to device Error.
108                                EFI_OUT_OF_RESOURCES- Failed due to resource shortage.
109 
110 **/
111 EFI_STATUS
112 EFIAPI
113 SDControllerStart (
114   IN EFI_DRIVER_BINDING_PROTOCOL     *This,
115   IN EFI_HANDLE                      Controller,
116   IN EFI_DEVICE_PATH_PROTOCOL        *RemainingDevicePath
117   );
118 
119 /**
120   Stop this driver on ControllerHandle. Support stoping any child handles
121   created by this driver.
122 
123   @param  This                 Protocol instance pointer.
124   @param  Controller           Handle of device to stop driver on.
125   @param  NumberOfChildren     Number of Children in the ChildHandleBuffer.
126   @param  ChildHandleBuffer    List of handles for the children we need to stop.
127 
128   @return EFI_SUCCESS
129   @return others
130 
131 **/
132 EFI_STATUS
133 EFIAPI
134 SDControllerStop (
135   IN EFI_DRIVER_BINDING_PROTOCOL     *This,
136   IN EFI_HANDLE                      Controller,
137   IN UINTN                           NumberOfChildren,
138   IN EFI_HANDLE                      *ChildHandleBuffer
139   );
140 
141 /**
142   The main function used to send the command to the card inserted into the SD host slot.
143   It will assemble the arguments to set the command register and wait for the command
144   and transfer completed until timeout. Then it will read the response register to fill
145   the ResponseData.
146 
147   @param  This                  A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
148   @param  CommandIndex          The command index to set the command index field of command register.
149   @param  Argument              Command argument to set the argument field of command register.
150   @param  DataType              TRANSFER_TYPE, indicates no data, data in or data out.
151   @param  Buffer                Contains the data read from / write to the device.
152   @param  BufferSize            The size of the buffer.
153   @param  ResponseType          RESPONSE_TYPE.
154   @param  TimeOut               Time out value in 1 ms unit.
155   @param  ResponseData          Depending on the ResponseType, such as CSD or card status.
156 
157   @retval EFI_SUCCESS
158   @retval EFI_INVALID_PARAMETER
159   @retval EFI_OUT_OF_RESOURCES
160   @retval EFI_TIMEOUT
161   @retval EFI_DEVICE_ERROR
162 
163 **/
164 EFI_STATUS
165 EFIAPI
166 SendCommand (
167   IN   EFI_SD_HOST_IO_PROTOCOL    *This,
168   IN   UINT16                     CommandIndex,
169   IN   UINT32                     Argument,
170   IN   TRANSFER_TYPE              DataType,
171   IN   UINT8                      *Buffer, OPTIONAL
172   IN   UINT32                     BufferSize,
173   IN   RESPONSE_TYPE              ResponseType,
174   IN   UINT32                     TimeOut,
175   OUT  UINT32                     *ResponseData OPTIONAL
176   );
177 
178 /**
179   Set max clock frequency of the host, the actual frequency may not be the same as MaxFrequency.
180   It depends on the max frequency the host can support, divider, and host speed mode.
181 
182   @param  This                  A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
183   @param  MaxFrequency          Max frequency in HZ.
184 
185   @retval EFI_SUCCESS
186   @retval EFI_TIMEOUT
187 
188 **/
189 EFI_STATUS
190 EFIAPI
191 SetClockFrequency (
192   IN  EFI_SD_HOST_IO_PROTOCOL    *This,
193   IN  UINT32                     MaxFrequencyInKHz
194   );
195 
196 /**
197   Set bus width of the host controller
198 
199   @param  This                  A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
200   @param  BusWidth              Bus width in 1, 4, 8 bits.
201 
202   @retval EFI_SUCCESS
203   @retval EFI_INVALID_PARAMETER
204 
205 **/
206 EFI_STATUS
207 EFIAPI
208 SetBusWidth (
209   IN  EFI_SD_HOST_IO_PROTOCOL    *This,
210   IN  UINT32                     BusWidth
211   );
212 
213 
214 /**
215   Set voltage which could supported by the host controller.
216   Support 0(Power off the host), 1.8V, 3.0V, 3.3V
217 
218   @param  This                  A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
219   @param  Voltage               Units in 0.1 V.
220 
221   @retval EFI_SUCCESS
222   @retval EFI_INVALID_PARAMETER
223 
224 **/
225 EFI_STATUS
226 EFIAPI
227 SetHostVoltage (
228   IN  EFI_SD_HOST_IO_PROTOCOL    *This,
229   IN  UINT32                     Voltage
230   );
231 
232 
233 /**
234   Reset the host controller.
235 
236   @param  This                  A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
237   @param  ResetAll              TRUE to reset all.
238 
239   @retval EFI_SUCCESS
240   @retval EFI_TIMEOUT
241 
242 **/
243 EFI_STATUS
244 EFIAPI
245 ResetSDHost (
246   IN  EFI_SD_HOST_IO_PROTOCOL    *This,
247   IN  RESET_TYPE                 ResetType
248   );
249 
250 
251 /**
252   Enable auto stop on the host controller.
253 
254   @param  This                  A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
255   @param  Enable                TRUE to enable, FALSE to disable.
256 
257   @retval EFI_SUCCESS
258   @retval EFI_TIMEOUT
259 
260 **/
261 EFI_STATUS
262 EFIAPI
263 EnableAutoStopCmd (
264   IN  EFI_SD_HOST_IO_PROTOCOL    *This,
265   IN  BOOLEAN                    Enable
266   );
267 
268 /**
269   Find whether these is a card inserted into the slot. If so init the host.
270   If not, return EFI_NOT_FOUND.
271 
272   @param  This                  A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
273 
274   @retval EFI_SUCCESS
275   @retval EFI_NOT_FOUND
276 
277 **/
278 EFI_STATUS
279 EFIAPI
280 DetectCardAndInitHost (
281   IN  EFI_SD_HOST_IO_PROTOCOL    *This
282   );
283 
284 /**
285   Set the Block length on the host controller.
286 
287   @param  This                  A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
288   @param  BlockLength           card supportes block length.
289 
290   @retval EFI_SUCCESS
291   @retval EFI_TIMEOUT
292 
293 **/
294 EFI_STATUS
295 EFIAPI
296 SetBlockLength (
297   IN  EFI_SD_HOST_IO_PROTOCOL    *This,
298   IN  UINT32                     BlockLength
299   );
300 
301 /**
302   Enable/Disable High Speed transfer mode
303 
304   @param  This                  A pointer to the EFI_SD_HOST_IO_PROTOCOL instance.
305   @param  Enable                TRUE to Enable, FALSE to Disable
306 
307   @return EFI_SUCCESS
308 **/
309 EFI_STATUS
310 EFIAPI
311 SetHighSpeedMode (
312   IN  EFI_SD_HOST_IO_PROTOCOL    *This,
313   IN  BOOLEAN                    Enable
314   );
315 
316 EFI_STATUS
317 EFIAPI
318 SetDDRMode (
319   IN  EFI_SD_HOST_IO_PROTOCOL    *This,
320   IN  BOOLEAN                    Enable
321   );
322 #endif
323