1 /** @file
2 *
3 *  Copyright (c) 2016, Hisilicon Limited. All rights reserved.
4 *  Copyright (c) 2016, Linaro Limited. All rights reserved.
5 *
6 *  This program and the accompanying materials
7 *  are licensed and made available under the terms and conditions of the BSD License
8 *  which accompanies this distribution.  The full text of the license may be found at
9 *  http://opensource.org/licenses/bsd-license.php
10 *
11 *  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 *  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 *
14 **/
15 
16 #include <PiDxe.h>
17 #include <Library/DebugLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
19 #include <Guid/EventGroup.h>
20 #include <Library/UefiRuntimeLib.h>
21 #include <Library/DxeServicesTableLib.h>
22 #include <Library/PcdLib.h>
23 #include <Library/IoLib.h>
24 #include <Library/CpldIoLib.h>
25 
26 UINTN           mCpldRegAddr;
27 EFI_EVENT       mCpldVirtualAddressChangeEvent;
28 
29 
30 VOID
31 EFIAPI
CpldVirtualAddressChange(IN EFI_EVENT Event,IN VOID * Context)32 CpldVirtualAddressChange (
33   IN EFI_EVENT        Event,
34   IN VOID             *Context
35   )
36 {
37   EfiConvertPointer (0, (VOID **) &mCpldRegAddr);
38 
39   return;
40 }
41 
42 RETURN_STATUS
43 EFIAPI
CpldRuntimeLibConstructor(VOID)44 CpldRuntimeLibConstructor (
45     VOID
46 )
47 {
48     EFI_STATUS              Status;
49     EFI_GCD_MEMORY_SPACE_DESCRIPTOR desp = {0};
50 
51     mCpldRegAddr = PcdGet64(PcdCpldBaseAddress);
52     Status = gDS->GetMemorySpaceDescriptor(mCpldRegAddr,&desp);
53     if(EFI_ERROR(Status)){
54         DEBUG ((EFI_D_ERROR, "[%a]:[%dL] GetMemorySpaceDescriptor failed: %r\n", __FUNCTION__, __LINE__, Status));
55         return Status;
56     }
57     desp.Attributes |= EFI_MEMORY_RUNTIME;
58     Status = gDS->SetMemorySpaceAttributes(mCpldRegAddr,0x10000, desp.Attributes);
59     if(EFI_ERROR(Status)){
60         DEBUG ((EFI_D_ERROR, "[%a]:[%dL] SetMemorySpaceAttributes failed: %r\n", __FUNCTION__, __LINE__, Status));
61         return Status;
62     }
63     //
64     // Register notify function for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
65     //
66     Status = gBS->CreateEventEx (
67                     EVT_NOTIFY_SIGNAL,
68                     TPL_NOTIFY,
69                     CpldVirtualAddressChange,
70                     NULL,
71                     &gEfiEventVirtualAddressChangeGuid,
72                     &mCpldVirtualAddressChangeEvent
73                     );
74     ASSERT_EFI_ERROR (Status);
75     return Status;
76 }
77 
78 EFI_STATUS
79 EFIAPI
CpldRuntimeLibDestructor(VOID)80 CpldRuntimeLibDestructor (
81     VOID
82   )
83 {
84   EFI_STATUS  Status = EFI_SUCCESS;
85 
86   if(!mCpldVirtualAddressChangeEvent ){
87      return Status;
88   }
89 
90   Status = gBS->CloseEvent(mCpldVirtualAddressChangeEvent);
91   return Status;
92 }
93 
WriteCpldReg(UINTN ulRegAddr,UINT8 ulValue)94 VOID WriteCpldReg(UINTN ulRegAddr, UINT8 ulValue)
95 {
96     MmioWrite8 (ulRegAddr + mCpldRegAddr, ulValue);
97 }
98 
ReadCpldReg(UINTN ulRegAddr)99 UINT8 ReadCpldReg(UINTN ulRegAddr)
100 {
101     return MmioRead8 (ulRegAddr + mCpldRegAddr);
102 }
103 
104 
105