2 Implementation for SMBus DXE driver entry point and SMBus Host
5 Copyright (c) 2013-2015 Intel Corporation.
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
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.
16 #include "CommonHeader.h"
18 #include "DxeQNCSmbus.h"
21 // Interface defintion of SMBUS Host Controller Protocol.
23 EFI_SMBUS_HC_PROTOCOL mSmbusHc
= {
31 // Handle to install SMBus Host Controller protocol.
33 EFI_HANDLE mSmbusHcHandle
= NULL
;
34 UINT8 mDeviceMapEntries
= 0;
35 EFI_SMBUS_DEVICE_MAP mDeviceMap
[MAX_SMBUS_DEVICES
];
36 UINT8 mPlatformNumRsvd
= 0;
37 UINT8
*mPlatformAddrRsvd
= NULL
;
40 // These addresses are reserved by the SMBus 2.0 specification
42 UINT8 mReservedAddress
[SMBUS_NUM_RESERVED
] = {
43 0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E, 0x10, 0x18, 0x50, 0x6E, 0xC2,
44 0xF0, 0xF2, 0xF4, 0xF6, 0xF8, 0xFA, 0xFC, 0xFE
49 Gets Io port base address of Smbus Host Controller.
51 This internal function depends on a feature flag named PcdIchSmbusFixedIoPortBaseAddress
52 to retrieve Smbus Io port base. If that feature flag is true, it will get Smbus Io port base
53 address from a preset Pcd entry named PcdIchSmbusIoPortBaseAddress; otherwise, it will always
54 read Pci configuration space to get that value in each Smbus bus transaction.
56 @return The Io port base address of Smbus host controller.
60 GetSmbusIoPortBaseAddress (
64 UINTN IoPortBaseAddress
;
66 if (FeaturePcdGet (PcdSmbaIoBaseAddressFixed
)) {
67 IoPortBaseAddress
= (UINTN
) PcdGet16 (PcdSmbaIoBaseAddress
);
69 IoPortBaseAddress
= (UINTN
) LpcPciCfg32 (R_QNC_LPC_SMBUS_BASE
) & B_QNC_LPC_SMBUS_BASE_MASK
;
73 // Make sure that the IO port base address has been properly set.
75 ASSERT (IoPortBaseAddress
!= 0);
77 return IoPortBaseAddress
;
85 UINTN IoPortBaseAddress
;
87 IoPortBaseAddress
= GetSmbusIoPortBaseAddress ();
90 // Step1: Enable QNC SMBUS I/O space.
92 LpcPciCfg32Or(R_QNC_LPC_SMBUS_BASE
, B_QNC_LPC_SMBUS_BASE_EN
);
95 // Step2: Clear Status Register before anyone uses the interfaces.
97 IoWrite8 (IoPortBaseAddress
+ R_QNC_SMBUS_HSTS
, B_QNC_SMBUS_HSTS_ALL
);
100 // Step3: Program the correct smbus clock
102 IoWrite8 (IoPortBaseAddress
+ R_QNC_SMBUS_HCLK
, V_QNC_SMBUS_HCLK_100KHZ
);
110 IN EFI_SMBUS_DEVICE_ADDRESS SlaveAddress
116 // See if we have already assigned this address to a device
118 for (Index
= 0; Index
< mDeviceMapEntries
; Index
++) {
119 if (SlaveAddress
.SmbusDeviceAddress
==
120 mDeviceMap
[Index
].SmbusDeviceAddress
.SmbusDeviceAddress
) {
126 // See if this address is claimed by a platform non-ARP-capable device
128 for (Index
= 0; Index
< mPlatformNumRsvd
; Index
++) {
129 if ((SlaveAddress
.SmbusDeviceAddress
<< 1) == mPlatformAddrRsvd
[Index
]) {
135 // See if this is a reserved address
137 for (Index
= 0; Index
< SMBUS_NUM_RESERVED
; Index
++) {
138 if (SlaveAddress
.SmbusDeviceAddress
== (UINTN
) mReservedAddress
[Index
]) {
148 GetNextAvailableAddress (
149 IN EFI_SMBUS_DEVICE_ADDRESS
*SlaveAddress
152 for (SlaveAddress
->SmbusDeviceAddress
= 0x03;
153 SlaveAddress
->SmbusDeviceAddress
< 0x7F;
154 SlaveAddress
->SmbusDeviceAddress
++
156 if (IsAddressAvailable (*SlaveAddress
)) {
161 return EFI_OUT_OF_RESOURCES
;
168 EFI_SMBUS_DEVICE_ADDRESS SlaveAddress
;
173 SlaveAddress
.SmbusDeviceAddress
= SMBUS_ADDRESS_ARP
;
175 Buffer
= SMBUS_DATA_PREPARE_TO_ARP
;
189 SmbusGetUdidGeneral (
190 IN OUT EFI_SMBUS_DEVICE_MAP
*DeviceMap
193 EFI_SMBUS_DEVICE_ADDRESS SlaveAddress
;
196 UINT8 Buffer
[SMBUS_GET_UDID_LENGTH
];
198 SlaveAddress
.SmbusDeviceAddress
= SMBUS_ADDRESS_ARP
;
199 Length
= SMBUS_GET_UDID_LENGTH
;
203 SMBUS_DATA_GET_UDID_GENERAL
,
210 if (!EFI_ERROR(Status
)) {
211 if (Length
== SMBUS_GET_UDID_LENGTH
) {
212 DeviceMap
->SmbusDeviceUdid
.DeviceCapabilities
= Buffer
[0];
213 DeviceMap
->SmbusDeviceUdid
.VendorRevision
= Buffer
[1];
214 DeviceMap
->SmbusDeviceUdid
.VendorId
= (UINT16
)((Buffer
[2] << 8) + Buffer
[3]);
215 DeviceMap
->SmbusDeviceUdid
.DeviceId
= (UINT16
)((Buffer
[4] << 8) + Buffer
[5]);
216 DeviceMap
->SmbusDeviceUdid
.Interface
= (UINT16
)((Buffer
[6] << 8) + Buffer
[7]);
217 DeviceMap
->SmbusDeviceUdid
.SubsystemVendorId
= (UINT16
)((Buffer
[8] << 8) + Buffer
[9]);
218 DeviceMap
->SmbusDeviceUdid
.SubsystemDeviceId
= (UINT16
)((Buffer
[10] << 8) + Buffer
[11]);
219 DeviceMap
->SmbusDeviceUdid
.VendorSpecificId
= (UINT32
)((Buffer
[12] << 24) + (Buffer
[13] << 16) + (Buffer
[14] << 8) + Buffer
[15]);
220 DeviceMap
->SmbusDeviceAddress
.SmbusDeviceAddress
= (UINT8
)(Buffer
[16] >> 1);
222 Status
= EFI_DEVICE_ERROR
;
231 IN OUT EFI_SMBUS_DEVICE_MAP
*DeviceMap
234 EFI_SMBUS_DEVICE_ADDRESS SlaveAddress
;
237 UINT8 Buffer
[SMBUS_GET_UDID_LENGTH
];
239 Buffer
[0] = DeviceMap
->SmbusDeviceUdid
.DeviceCapabilities
;
240 Buffer
[1] = DeviceMap
->SmbusDeviceUdid
.VendorRevision
;
241 Buffer
[2] = (UINT8
)(DeviceMap
->SmbusDeviceUdid
.VendorId
>> 8);
242 Buffer
[3] = (UINT8
)(DeviceMap
->SmbusDeviceUdid
.VendorId
);
243 Buffer
[4] = (UINT8
)(DeviceMap
->SmbusDeviceUdid
.DeviceId
>> 8);
244 Buffer
[5] = (UINT8
)(DeviceMap
->SmbusDeviceUdid
.DeviceId
);
245 Buffer
[6] = (UINT8
)(DeviceMap
->SmbusDeviceUdid
.Interface
>> 8);
246 Buffer
[7] = (UINT8
)(DeviceMap
->SmbusDeviceUdid
.Interface
);
247 Buffer
[8] = (UINT8
)(DeviceMap
->SmbusDeviceUdid
.SubsystemVendorId
>> 8);
248 Buffer
[9] = (UINT8
)(DeviceMap
->SmbusDeviceUdid
.SubsystemVendorId
);
249 Buffer
[10] = (UINT8
)(DeviceMap
->SmbusDeviceUdid
.SubsystemDeviceId
>> 8);
250 Buffer
[11] = (UINT8
)(DeviceMap
->SmbusDeviceUdid
.SubsystemDeviceId
);
251 Buffer
[12] = (UINT8
)(DeviceMap
->SmbusDeviceUdid
.VendorSpecificId
>> 24);
252 Buffer
[13] = (UINT8
)(DeviceMap
->SmbusDeviceUdid
.VendorSpecificId
>> 16);
253 Buffer
[14] = (UINT8
)(DeviceMap
->SmbusDeviceUdid
.VendorSpecificId
>> 8);
254 Buffer
[15] = (UINT8
)(DeviceMap
->SmbusDeviceUdid
.VendorSpecificId
);
255 Buffer
[16] = (UINT8
)(DeviceMap
->SmbusDeviceAddress
.SmbusDeviceAddress
<< 1);
257 SlaveAddress
.SmbusDeviceAddress
= SMBUS_ADDRESS_ARP
;
258 Length
= SMBUS_GET_UDID_LENGTH
;
262 SMBUS_DATA_ASSIGN_ADDRESS
,
277 EFI_SMBUS_DEVICE_MAP
*CurrentDeviceMap
;
279 Status
= SmbusPrepareToArp ();
280 if (EFI_ERROR(Status
)) {
281 if (Status
== EFI_DEVICE_ERROR
) {
292 // Main loop to ARP all ARP-capable devices
295 CurrentDeviceMap
= &mDeviceMap
[mDeviceMapEntries
];
296 Status
= SmbusGetUdidGeneral (CurrentDeviceMap
);
297 if (EFI_ERROR(Status
)) {
301 if (CurrentDeviceMap
->SmbusDeviceAddress
.SmbusDeviceAddress
== (0xFF >> 1)) {
303 // If address is unassigned, assign it
305 Status
= GetNextAvailableAddress (
306 &CurrentDeviceMap
->SmbusDeviceAddress
308 if (EFI_ERROR(Status
)) {
309 return EFI_OUT_OF_RESOURCES
;
311 } else if (((CurrentDeviceMap
->SmbusDeviceUdid
.DeviceCapabilities
) & 0xC0) != 0) {
313 // if address is not fixed, check if the current address is available
315 if (!IsAddressAvailable (
316 CurrentDeviceMap
->SmbusDeviceAddress
319 // if currently assigned address is already used, get a new one
321 Status
= GetNextAvailableAddress (
322 &CurrentDeviceMap
->SmbusDeviceAddress
324 if (EFI_ERROR(Status
)) {
325 return EFI_OUT_OF_RESOURCES
;
330 Status
= SmbusAssignAddress (CurrentDeviceMap
);
331 if (EFI_ERROR(Status
)) {
333 // If there was a device error, just continue on and try again.
334 // Other errors should be reported.
336 if (Status
!= EFI_DEVICE_ERROR
) {
341 // If there was no error, the address was assigned and we must update our
347 } while (mDeviceMapEntries
< MAX_SMBUS_DEVICES
);
355 IN EFI_SMBUS_UDID
*SmbusUdid
,
356 IN OUT EFI_SMBUS_DEVICE_ADDRESS
*SlaveAddress
360 EFI_SMBUS_DEVICE_MAP
*CurrentDeviceMap
;
362 if (mDeviceMapEntries
>= MAX_SMBUS_DEVICES
) {
363 return EFI_OUT_OF_RESOURCES
;
366 CurrentDeviceMap
= &mDeviceMap
[mDeviceMapEntries
];
369 // Find an available address to assign
371 Status
= GetNextAvailableAddress (
372 &CurrentDeviceMap
->SmbusDeviceAddress
374 if (EFI_ERROR(Status
)) {
375 return EFI_OUT_OF_RESOURCES
;
378 CurrentDeviceMap
->SmbusDeviceUdid
.DeviceCapabilities
= SmbusUdid
->DeviceCapabilities
;
379 CurrentDeviceMap
->SmbusDeviceUdid
.DeviceId
= SmbusUdid
->DeviceId
;
380 CurrentDeviceMap
->SmbusDeviceUdid
.Interface
= SmbusUdid
->Interface
;
381 CurrentDeviceMap
->SmbusDeviceUdid
.SubsystemDeviceId
= SmbusUdid
->SubsystemDeviceId
;
382 CurrentDeviceMap
->SmbusDeviceUdid
.SubsystemVendorId
= SmbusUdid
->SubsystemVendorId
;
383 CurrentDeviceMap
->SmbusDeviceUdid
.VendorId
= SmbusUdid
->VendorId
;
384 CurrentDeviceMap
->SmbusDeviceUdid
.VendorRevision
= SmbusUdid
->VendorRevision
;
385 CurrentDeviceMap
->SmbusDeviceUdid
.VendorSpecificId
= SmbusUdid
->VendorSpecificId
;
387 Status
= SmbusAssignAddress (CurrentDeviceMap
);
388 if (EFI_ERROR(Status
)) {
393 SlaveAddress
->SmbusDeviceAddress
= CurrentDeviceMap
->SmbusDeviceAddress
.SmbusDeviceAddress
;
401 Executes an SMBus operation to an SMBus controller. Returns when either the command has been
402 executed or an error is encountered in doing the operation.
404 The Execute() function provides a standard way to execute an operation as defined in the System
405 Management Bus (SMBus) Specification. The resulting transaction will be either that the SMBus
406 slave devices accept this transaction or that this function returns with error.
408 @param This A pointer to the EFI_SMBUS_HC_PROTOCOL instance.
409 @param SlaveAddress The SMBus slave address of the device with which to communicate.
410 @param Command This command is transmitted by the SMBus host controller to the
411 SMBus slave device and the interpretation is SMBus slave device
412 specific. It can mean the offset to a list of functions inside an
413 SMBus slave device. Not all operations or slave devices support
414 this command's registers.
415 @param Operation Signifies which particular SMBus hardware protocol instance that
416 it will use to execute the SMBus transactions. This SMBus
417 hardware protocol is defined by the SMBus Specification and is
419 @param PecCheck Defines if Packet Error Code (PEC) checking is required for this
421 @param Length Signifies the number of bytes that this operation will do. The
422 maximum number of bytes can be revision specific and operation
423 specific. This field will contain the actual number of bytes that
424 are executed for this operation. Not all operations require this
426 @param Buffer Contains the value of data to execute to the SMBus slave device.
427 Not all operations require this argument. The length of this
428 buffer is identified by Length.
430 @retval EFI_SUCCESS The last data that was returned from the access matched the poll
432 @retval EFI_CRC_ERROR Checksum is not correct (PEC is incorrect).
433 @retval EFI_TIMEOUT Timeout expired before the operation was completed. Timeout is
434 determined by the SMBus host controller device.
435 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
436 @retval EFI_DEVICE_ERROR The request was not completed because a failure that was
437 reflected in the Host Status Register bit. Device errors are a
438 result of a transaction collision, illegal command field,
439 unclaimed cycle (host initiated), or bus errors (collisions).
440 @retval EFI_INVALID_PARAMETER Operation is not defined in EFI_SMBUS_OPERATION.
441 @retval EFI_INVALID_PARAMETER Length/Buffer is NULL for operations except for EfiSmbusQuickRead
442 and EfiSmbusQuickWrite. Length is outside the range of valid
444 @retval EFI_UNSUPPORTED The SMBus operation or PEC is not supported.
445 @retval EFI_BUFFER_TOO_SMALL Buffer is not sufficient for this operation.
451 IN CONST EFI_SMBUS_HC_PROTOCOL
*This
,
452 IN CONST EFI_SMBUS_DEVICE_ADDRESS SlaveAddress
,
453 IN CONST EFI_SMBUS_DEVICE_COMMAND Command
,
454 IN CONST EFI_SMBUS_OPERATION Operation
,
455 IN CONST BOOLEAN PecCheck
,
456 IN OUT UINTN
*Length
,
460 InitializeInternal ();
472 Sets the SMBus slave device addresses for the device with a given unique ID or enumerates the
475 The ArpDevice() function provides a standard way for a device driver to enumerate the entire
476 SMBus or specific devices on the bus.
478 @param This A pointer to the EFI_SMBUS_HC_PROTOCOL instance.
479 @param ArpAll A Boolean expression that indicates if the host drivers need to
480 enumerate all the devices or enumerate only the device that is
481 identified by SmbusUdid. If ArpAll is TRUE, SmbusUdid and
482 SlaveAddress are optional. If ArpAll is FALSE, ArpDevice will
483 enumerate SmbusUdid and the address will be at SlaveAddress.
484 @param SmbusUdid The Unique Device Identifier (UDID) that is associated with this
486 @param SlaveAddress The SMBus slave address that is associated with an SMBus UDID.
488 @retval EFI_SUCCESS The last data that was returned from the access matched the poll
490 @retval EFI_CRC_ERROR Checksum is not correct (PEC is incorrect).
491 @retval EFI_TIMEOUT Timeout expired before the operation was completed. Timeout is
492 determined by the SMBus host controller device.
493 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
494 @retval EFI_DEVICE_ERROR The request was not completed because a failure that was
495 reflected in the Host Status Register bit. Device errors are a
496 result of a transaction collision, illegal command field,
497 unclaimed cycle (host initiated), or bus errors (collisions).
498 @retval EFI_UNSUPPORTED The corresponding SMBus operation is not supported.
504 IN CONST EFI_SMBUS_HC_PROTOCOL
*This
,
506 IN EFI_SMBUS_UDID
*SmbusUdid
, OPTIONAL
507 IN OUT EFI_SMBUS_DEVICE_ADDRESS
*SlaveAddress OPTIONAL
510 InitializeInternal ();
513 return SmbusFullArp ();
515 if ((SmbusUdid
== NULL
) || (SlaveAddress
== NULL
)) {
516 return EFI_INVALID_PARAMETER
;
518 return SmbusDirectedArp ((EFI_SMBUS_UDID
*)SmbusUdid
, SlaveAddress
);
523 Returns a pointer to the Address Resolution Protocol (ARP) map that contains the ID/address pair
524 of the slave devices that were enumerated by the SMBus host controller driver.
526 The GetArpMap() function returns the mapping of all the SMBus devices that were enumerated by the
529 @param This A pointer to the EFI_SMBUS_HC_PROTOCOL instance.
530 @param Length Size of the buffer that contains the SMBus device map.
531 @param SmbusDeviceMap The pointer to the device map as enumerated by the SMBus
534 @retval EFI_SUCCESS The SMBus returned the current device map.
535 @retval EFI_UNSUPPORTED The corresponding operation is not supported.
541 IN CONST EFI_SMBUS_HC_PROTOCOL
*This
,
542 IN OUT UINTN
*Length
,
543 IN OUT EFI_SMBUS_DEVICE_MAP
**SmbusDeviceMap
546 *Length
= mDeviceMapEntries
;
547 *SmbusDeviceMap
= mDeviceMap
;
553 Allows a device driver to register for a callback when the bus driver detects a state that it
554 needs to propagate to other drivers that are registered for a callback.
556 The Notify() function registers all the callback functions to allow the bus driver to call these
557 functions when the SlaveAddress/Data pair happens.
558 If NotifyFunction is NULL, then ASSERT ().
560 @param This A pointer to the EFI_SMBUS_HC_PROTOCOL instance.
561 @param SlaveAddress The SMBUS hardware address to which the SMBUS device is
562 preassigned or allocated.
563 @param Data Data of the SMBus host notify command that the caller wants to be
565 @param NotifyFunction The function to call when the bus driver detects the SlaveAddress
568 @retval EFI_SUCCESS NotifyFunction was registered.
569 @retval EFI_UNSUPPORTED The corresponding operation is not supported.
575 IN CONST EFI_SMBUS_HC_PROTOCOL
*This
,
576 IN CONST EFI_SMBUS_DEVICE_ADDRESS SlaveAddress
,
578 IN CONST EFI_SMBUS_NOTIFY_FUNCTION NotifyFunction
581 return EFI_UNSUPPORTED
;
585 Entry point to the DXE Driver that produces the SMBus Host Controller Protocol.
587 @param ImageHandle ImageHandle of the loaded driver.
588 @param SystemTable Pointer to the EFI System Table.
590 @retval EFI_SUCCESS The entry point of SMBus DXE driver is executed successfully.
591 @retval !EFI_SUCESS Some error occurs in the entry point of SMBus DXE driver.
597 IN EFI_HANDLE ImageHandle
,
598 IN EFI_SYSTEM_TABLE
*SystemTable
603 mPlatformNumRsvd
= (UINT8
)PcdGet32 (PcdPlatformSmbusAddrNum
);
604 mPlatformAddrRsvd
= (UINT8
*)(UINTN
) PcdGet64 (PcdPlatformSmbusAddrTable
);
607 // Install SMBus Host Controller protocol interface.
609 Status
= gBS
->InstallMultipleProtocolInterfaces (
611 &gEfiSmbusHcProtocolGuid
,
615 ASSERT_EFI_ERROR (Status
);