1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
|
/** @file
UEFI Driver Entry and Binding support.
Copyright (c) 1999 - 2016, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "MmcHostDriver.h"
#include <PiDxe.h>
//
// MMCSDIOController Driver Global Variables
//
EFI_DRIVER_BINDING_PROTOCOL gMmcHostDriverBinding = {
MmcHostDriverBindingSupported,
MmcHostDriverBindingStart,
MmcHostDriverBindingStop,
0x10,
NULL,
NULL
};
EFI_MMC_HOST_IO_PROTOCOL mMmcHostIo = {
EFI_MMC_HOST_IO_PROTOCOL_REVISION_01,
{
0, ///< HighSpeedSupport
0, ///< V18Support
0, ///< V30Support
0, ///< V33Support
0, ///< HS400Support
0, ///< BusWidth4
0, ///< BusWidth8
0, ///< Reserved1
0,
0,
0,
0,
0, ///< ADMA2Support
0, ///< DmaMode
0, ///< ReTune Timer
0, ///< ReTune Mode
0, ///< Reserved2
(512 * 1024) ///<BoundarySize 512 KB
},
SendCommand,
SetClockFrequency,
SetBusWidth,
SetHostVoltage,
SetHostDdrMode,
SetHostSdrMode,
ResetMmcHost,
EnableAutoStopCmd,
DetectCardAndInitHost,
SetBlockLength,
SetupDevice,
SetHostSpeedMode
};
/**
Entry point for MMC Host EFI drivers.
@param[in] ImageHandle EFI_HANDLE
@param[in] SystemTable EFI_SYSTEM_TABLE
@retval EFI_SUCCESS The function completed successfully
@retval EFI_DEVICE_ERROR The function failed to complete
**/
EFI_STATUS
EFIAPI
MmcHostDriverEntryPoint (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
return EfiLibInstallAllDriverProtocols (
ImageHandle,
SystemTable,
&gMmcHostDriverBinding,
ImageHandle,
&gMmcHostComponentName,
NULL,
NULL
);
}
/**
Test to see if this MMC Host driver supports ControllerHandle.
Any ControllerHandle that has installed will be supported.
@param[in] This Protocol instance pointer
@param[in] Controller Handle of device to test
@param[in] RemainingDevicePath Not used
@retval EFI_SUCCESS This driver supports this device
@retval EFI_UNSUPPORTED This driver does not support this device
**/
EFI_STATUS
EFIAPI
MmcHostDriverBindingSupported (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE Controller,
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
)
{
EFI_STATUS Status;
EFI_PCI_IO_PROTOCOL *PciIo;
EFI_MMC_HOST_IO_PROTOCOL *MmcHostIo;
PCI_CLASSC PciClass;
UINT32 VidDid;
UINT32 Bar0 = 0;
UINT32 Bar1 = 0;
UINTN Seg, Bus, Dev, Func;
//
// Verify the MMC IO Protocol, which installed by the
// IdeController module.
//
Status = gBS->OpenProtocol (
Controller,
&gEfiMmcHostIoProtocolGuid,
(VOID **) &MmcHostIo,
This->DriverBindingHandle,
Controller,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (!EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "MmcHost controller already started, Controller: 0x%016Lx\r\n", (UINT64) (UINTN) Controller));
Status = EFI_ALREADY_STARTED;
return Status;
}
//
// Test whether there is PCI IO Protocol attached on the controller handle.
//
Status = gBS->OpenProtocol (
Controller,
&gEfiPciIoProtocolGuid,
(VOID**) &PciIo,
This->DriverBindingHandle,
Controller,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (EFI_ERROR (Status)) {
return Status;
}
Status = PciIo->Pci.Read (
PciIo,
EfiPciIoWidthUint32,
PCI_VENDOR_ID_OFFSET,
1,
&VidDid
);
if (EFI_ERROR (Status)) {
goto Exit;
}
Status = PciIo->Pci.Read (
PciIo,
EfiPciIoWidthUint8,
PCI_CLASSCODE_OFFSET,
sizeof (PCI_CLASSC) / sizeof (UINT8),
&PciClass
);
if (EFI_ERROR (Status)) {
Status = EFI_UNSUPPORTED;
goto Exit;
}
Status = PciIo->Pci.Read (
PciIo,
EfiPciIoWidthUint32,
PCI_BASE_ADDRESSREG_OFFSET,
1,
&Bar0
);
if (EFI_ERROR (Status)) {
Status = EFI_UNSUPPORTED;
goto Exit;
}
Status = PciIo->Pci.Read (
PciIo,
EfiPciIoWidthUint32,
PCI_BASE_ADDRESSREG_OFFSET + 4,
1,
&Bar1
);
if (EFI_ERROR (Status)) {
Status = EFI_UNSUPPORTED;
goto Exit;
}
Status = PciIo->GetLocation (
PciIo,
&Seg,
&Bus,
&Dev,
&Func
);
if (EFI_ERROR (Status)) {
Status = EFI_UNSUPPORTED;
goto Exit;
}
if ((Seg != 0) || (Bus != 0) || (Dev != 28) || (Func != 0)) {
//
// This is not the eMMC controller, bail.
//
Status = EFI_UNSUPPORTED;
goto Exit;
}
if ((PciClass.BaseCode != PCI_CLASS_SYSTEM_PERIPHERAL) ||
(PciClass.SubClassCode != PCI_SUBCLASS_SD_HOST_CONTROLLER) ||
((PciClass.PI != PCI_IF_STANDARD_HOST_NO_DMA) && (PciClass.PI != PCI_IF_STANDARD_HOST_SUPPORT_DMA))
) {
Status = EFI_UNSUPPORTED;
goto Exit;
}
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingSupported0: Seg %d, bus:%d, Dev:%d, Func:%d\n", Seg, Bus, Dev, Func));
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingSupported1: VidDid %08x\n", VidDid));
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingSupported1: Base Code %x\n", PciClass.BaseCode));
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingSupported1: SubClassCode %x\n", PciClass.SubClassCode));
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingSupported1: PI %x\n", PciClass.PI));
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingSupported0: MEMIO Base0 %x\n", Bar0));
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingSupported0: MEMIO Base1 %x\n", Bar1));
Exit:
gBS->CloseProtocol (
Controller,
&gEfiPciIoProtocolGuid,
This->DriverBindingHandle,
Controller
);
return Status;
}
/**
Starting the MMC Host Driver
@param[in] This Protocol instance pointer
@param[in] Controller Handle of device to start
@param[in] RemainingDevicePath Not used
@retval EFI_SUCCESS This driver start this device
@retval EFI_UNSUPPORTED This driver does not support this device
@retval EFI_DEVICE_ERROR This driver cannot be started due to device Error
@retval EFI_OUT_OF_RESOURCES This driver cannot allocate resources
**/
EFI_STATUS
EFIAPI
MmcHostDriverBindingStart (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE Controller,
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
)
{
EFI_PCI_IO_PROTOCOL *PciIo;
EFI_STATUS Status;
MMCHOST_DATA *MmcHostData;
UINT32 Data;
UINT16 Data16;
UINT32 VidDid;
UINT32 Bar0 = 0;
UINT32 Bar1 = 0;
UINTN Seg, Bus, Dev, Func;
MmcHostData = NULL;
Data = 0;
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: Start\n"));
//
// Open PCI I/O Protocol and save pointer to open protocol
// in private data area.
//
Status = gBS->OpenProtocol (
Controller,
&gEfiPciIoProtocolGuid,
(VOID **) &PciIo,
This->DriverBindingHandle,
Controller,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (EFI_ERROR (Status)) {
goto Exit;
}
//
// Enable the MMC Host Controller MMIO space
//
Status = PciIo->Attributes (
PciIo,
EfiPciIoAttributeOperationEnable,
EFI_PCI_DEVICE_ENABLE,
NULL
);
if (EFI_ERROR (Status)) {
Status = EFI_OUT_OF_RESOURCES;
goto Exit;
}
MmcHostData = (MMCHOST_DATA *) AllocateZeroPool (sizeof (MMCHOST_DATA));
if (MmcHostData == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto Exit;
}
MmcHostData->Signature = MMCHOST_DATA_SIGNATURE;
MmcHostData->PciIo = PciIo;
CopyMem (&MmcHostData->MmcHostIo, &mMmcHostIo, sizeof (EFI_MMC_HOST_IO_PROTOCOL));
Status = PciIo->Pci.Read (
PciIo,
EfiPciIoWidthUint32,
PCI_VENDOR_ID_OFFSET,
1,
&VidDid
);
if (EFI_ERROR (Status)) {
goto Exit;
}
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: VidDid = 0x%08x\n", VidDid));
Status = PciIo->GetLocation (
PciIo,
&Seg,
&Bus,
&Dev,
&Func
);
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: Seg %d, bus:%d,Dev:%d,Func:%d\n", Seg, Bus, Dev,Func));
MmcHostData->PciVid = (UINT16) (VidDid & 0xffff);
MmcHostData->PciDid = (UINT16) (VidDid >> 16);
Status = PciIo->Pci.Read (
PciIo,
EfiPciIoWidthUint32,
PCI_BASE_ADDRESSREG_OFFSET,
1,
&Bar0
);
Status = PciIo->Pci.Read (
PciIo,
EfiPciIoWidthUint32,
PCI_BASE_ADDRESSREG_OFFSET + 4,
1,
&Bar1
);
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: MEMIO Base0 %x\n", Bar0));
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: MEMIO Base1 %x\n", Bar1));
MmcHostData->MmcHostIo.ResetMmcHost (&MmcHostData->MmcHostIo, Reset_All);
MmcHostData->EnableVerboseDebug = FALSE;
PciIo->Mem.Read (
PciIo,
EfiPciIoWidthUint16,
0,
(UINT64) MMIO_CTRLRVER,
1,
&Data16
);
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: MMIO_CTRLRVER = 0x%08x\n", Data16));
MmcHostData->ControllerVersion = Data16 & 0xFF;
switch (MmcHostData->ControllerVersion) {
case 0: DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: MMC Host Controller Version 1.0\n")); break;
case 1: DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: MMC Host Controller Version 2.0\n")); break;
case 2: DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: MMC Host Controller Version 3.0\n")); break;
default:
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: Unknown MMC Host Controller Version, Stopping Driver!!\n"));
goto Exit;
}
PciIo->Mem.Read (
PciIo,
EfiPciIoWidthUint32,
0,
(UINT32) MMIO_CAP,
1,
&Data
);
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: Low MMIO_CAP = 0x%08x\n", Data));
if ((Data & BIT18) != 0) {
MmcHostData->MmcHostIo.HostCapability.BusWidth8 = TRUE;
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: BusWidth8\n"));
}
MmcHostData->MmcHostIo.HostCapability.BusWidth4 = TRUE;
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: BusWidth4\n"));
if ((Data & BIT19) != 0) {
MmcHostData->MmcHostIo.HostCapability.ADMA2Support = TRUE;
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: ADMA2Support\n"));
}
if ((Data & BIT21) != 0) {
MmcHostData->MmcHostIo.HostCapability.HighSpeedSupport = TRUE;
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: HighSpeedSupport\n"));
}
if ((Data & BIT22) != 0) {
MmcHostData->MmcHostIo.HostCapability.SDMASupport = TRUE;
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: SDMASupport\n"));
}
if ((Data & BIT24) != 0) {
MmcHostData->MmcHostIo.HostCapability.V33Support = TRUE;
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: V33Support\n"));
}
if ((Data & BIT25) != 0) {
MmcHostData->MmcHostIo.HostCapability.V30Support = TRUE;
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: V30Support\n"));
}
if ((Data & BIT26) != 0) {
MmcHostData->MmcHostIo.HostCapability.V18Support = TRUE;
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: V18Support\n"));
}
if (((Data) & (BIT30 | BIT31)) == 0) {
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: Removable Card Slot\n"));
} else if ((Data & BIT30) && (Data & (~BIT31))) {
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: Embedded Slot for One Device\n"));
}
MmcHostData->BaseClockInMHz = (Data >> 8) & 0xFF;
PciIo->Mem.Read (
PciIo,
EfiPciIoWidthUint32,
0,
(UINT32) (MMIO_CAP + 4),
1,
&Data
);
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: High MMIO_CAP = 0x%08x\n", Data));
if ((Data & 0x1<<(32-32)) != 0) {
MmcHostData->MmcHostIo.HostCapability.SDR50Support= TRUE;
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: SDR50Support\n"));
}
if ((Data & 0x1<<(33-32)) != 0) {
MmcHostData->MmcHostIo.HostCapability.SDR104Support= TRUE;
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: SDR104Support\n"));
}
if ((Data & 0x1<<(34-32)) != 0) {
MmcHostData->MmcHostIo.HostCapability.DDR50Support= TRUE;
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: DDR50Support\n"));
}
if ((Data & 0x1<<(63-32)) != 0) {
MmcHostData->MmcHostIo.HostCapability.HS400Support = TRUE;
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: HS400Support\n"));
}
if (MmcHostData->ControllerVersion >= 2) {
MmcHostData->MmcHostIo.HostCapability.ReTuneMode = (Data >> (46-32)) & 0x3;
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: ReTuneMode = 0x%08x\n", MmcHostData->MmcHostIo.HostCapability.ReTuneMode));
MmcHostData->MmcHostIo.HostCapability.ReTuneTimer = (Data>>(40-32)) & 0xF;
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: ReTuneTimer = 0x%08x\n", MmcHostData->MmcHostIo.HostCapability.ReTuneTimer));
}
MmcHostData->BlockLength = BLOCK_SIZE;
MmcHostData->IsAutoStopCmd = TRUE;
Status = gBS->InstallProtocolInterface (
&Controller,
&gEfiMmcHostIoProtocolGuid,
EFI_NATIVE_INTERFACE,
&MmcHostData->MmcHostIo
);
if (EFI_ERROR (Status)) {
goto Exit;
}
//
// Component name protocol
//
Status = AddUnicodeString (
"eng",
gMmcHostComponentName.SupportedLanguages,
&MmcHostData->ControllerNameTable,
L"MMC Host Controller"
);
if (EFI_ERROR (Status)) {
Status = EFI_OUT_OF_RESOURCES;
gBS->UninstallProtocolInterface (
Controller,
&gEfiMmcHostIoProtocolGuid,
&MmcHostData->MmcHostIo
);
}
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStart: Host Started\n"));
Exit:
if (EFI_ERROR (Status)) {
if (MmcHostData != NULL) {
FreePool (MmcHostData);
}
gBS->CloseProtocol (
Controller,
&gEfiPciIoProtocolGuid,
This->DriverBindingHandle,
Controller
);
}
return Status;
}
/**
Stop MMC Host driver on ControllerHandle. Support stoping any child handles
created by this driver.
@param[in] This Protocol instance pointer
@param[in] Controller Handle of device to stop driver on
@param[in] NumberOfChildren Number of Children in the ChildHandleBuffer
@param[in] ChildHandleBuffer List of handles for the children we need to stop
@retval EFI_SUCCESS This driver stop this device
@retval EFI_DEVICE_ERROR This driver cannot be stop due to device Error
**/
EFI_STATUS
EFIAPI
MmcHostDriverBindingStop (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE Controller,
IN UINTN NumberOfChildren,
IN EFI_HANDLE *ChildHandleBuffer
)
{
EFI_STATUS Status;
EFI_MMC_HOST_IO_PROTOCOL *MmcHostIo;
MMCHOST_DATA *MmcHostData;
Status = gBS->OpenProtocol (
Controller,
&gEfiMmcHostIoProtocolGuid,
(VOID **) &MmcHostIo,
This->DriverBindingHandle,
Controller,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (EFI_ERROR (Status)) {
goto _exit_MmcHostDriverBindingStop;
}
gBS->CloseProtocol (
Controller,
&gEfiPciIoProtocolGuid,
This->DriverBindingHandle,
Controller
);
MmcHostData = MMCHOST_DATA_FROM_THIS (MmcHostIo);
//
// Uninstall Block I/O protocol from the device handle
//
Status = gBS->UninstallProtocolInterface (
Controller,
&gEfiMmcHostIoProtocolGuid,
&MmcHostData->MmcHostIo
);
if (EFI_ERROR (Status)) {
goto _exit_MmcHostDriverBindingStop;
}
FreeUnicodeStringTable (MmcHostData->ControllerNameTable);
FreePool (MmcHostData);
_exit_MmcHostDriverBindingStop:
DEBUG ((EFI_D_INFO, "MmcHostDriverBindingStop exited with Status %r\n", Status));
return Status;
}
|