hc
2024-03-26 e0728245c89800c2038c23308f2d88969d5b41c8
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
/** @file
*
*  Copyright (c) 2015, Linaro Limited. All rights reserved.
*  Copyright (c) 2017, Rockchip Inc. All rights reserved.
*
*  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 <PiDxe.h>
#include <Library/DebugLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/ArmLib.h>
#include <Library/BaseLib.h>
#include <Library/TimerLib.h>
#include <Library/IoLib.h>
#include <Library/UefiRuntimeLib.h>
#include <Library/DxeServicesTableLib.h>
#include <Guid/EventGroup.h>
 
#include <Library/I2CLib.h>
#include <Rk3399/Rk3399.h>
 
STATIC EFI_EVENT              mI2cLibVirtualAddrChangeEvent;
 
struct RkI2CInfo RkI2CDev[I2C_BUS_MAX] = {
   { .Regs = (UINT32)RK3399_I2C0_BASE, 0},
   { .Regs = (UINT32)RK3399_I2C1_BASE, 0},
   { .Regs = (UINT32)RK3399_I2C2_BASE, 0},
   { .Regs = (UINT32)RK3399_I2C3_BASE, 0},
   { .Regs = (UINT32)RK3399_I2C4_BASE, 0},
   { .Regs = (UINT32)RK3399_I2C5_BASE, 0},
   { .Regs = (UINT32)RK3399_I2C6_BASE, 0},
   { .Regs = (UINT32)RK3399_I2C7_BASE, 0}
};
 
STATIC UINTN gI2cRuntimeBase[I2C_BUS_MAX] = {0};
 
void *RkI2CGetBase(enum RkI2CBusID BusId)
{
   if (BusId >= I2C_BUS_MAX) {
       DEBUG ((EFI_D_ERROR, "I2C bus error, PLS set i2c bus first!\n"));
       return (void *)NULL;
   }
 
   if (RkI2CDev[BusId].Regs == 0) {
       DEBUG ((EFI_D_ERROR, "I2C base register error, PLS check i2c config!\n"));
       return (void *)NULL;
   }
 
   if (gI2cRuntimeBase[BusId] != 0) {
       RkI2CDev[BusId].Regs = gI2cRuntimeBase[BusId];
   }
 
   return (void *)&RkI2CDev[BusId];
}
 
VOID
EFIAPI
RkI2cLibVirtualNotifyEvent (
  IN EFI_EVENT        Event,
  IN VOID             *Context
  )
{
  UINT32 BusId;
 
  for (BusId = 0; BusId < I2C_BUS_MAX; BusId++) {
    if (gI2cRuntimeBase[BusId] != 0) {
      EfiConvertPointer (0x0, (VOID **)&gI2cRuntimeBase[BusId]);
      DEBUG ((EFI_D_INFO, "I2cLibVirtualNotifyEvent: gI2cRuntimeBase[%d]=0x%x\n", BusId, gI2cRuntimeBase[BusId]));
    }
  }
 
  return;
}
 
EFI_STATUS
RkI2cLibRuntimeSetup(enum RkI2CBusID BusId)
{
  EFI_STATUS Status;
  struct RkI2CInfo *I2CInfo = (struct RkI2CInfo *)RkI2CGetBase(BusId);
  gI2cRuntimeBase[BusId] = I2CInfo->Regs;
 
  // Declare the controller as EFI_MEMORY_RUNTIME
  Status = gDS->AddMemorySpace (
      EfiGcdMemoryTypeMemoryMappedIo,
      gI2cRuntimeBase[BusId], SIZE_64KB,
      EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
      );
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_WARN, "[%a:%d] AddMemorySpace failed: %r\n", __FUNCTION__, __LINE__, Status));
  }
 
  Status = gDS->SetMemorySpaceAttributes (gI2cRuntimeBase[BusId], SIZE_64KB, EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_ERROR, "[%a:%d] SetMemorySpaceAttributes failed: %r\n", __FUNCTION__, __LINE__, Status));
    return Status;
  }
 
  //
  // Register for the virtual address change event
  //
  // Only create event once
  if (mI2cLibVirtualAddrChangeEvent == NULL) {
    Status = gBS->CreateEventEx (
        EVT_NOTIFY_SIGNAL,
        TPL_NOTIFY,
        RkI2cLibVirtualNotifyEvent,
        NULL,
        &gEfiEventVirtualAddressChangeGuid,
        &mI2cLibVirtualAddrChangeEvent
    );
    if (EFI_ERROR (Status)) {
      DEBUG ((EFI_D_ERROR, "[%a:%d] Create event failed: %r\n", __FUNCTION__, __LINE__, Status));
      return Status;
    }
  }
 
  return EFI_SUCCESS;
}