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
/** @file
*
*  Copyright (c) 2015, Hisilicon Limited. All rights reserved.
*  Copyright (c) 2015, Linaro Limited. All rights reserved.
*
*  SPDX-License-Identifier: BSD-2-Clause-Patent
*
**/
 
 
#include <PiDxe.h>
#include <Library/DebugLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/BaseLib.h>
#include <Library/UefiRuntimeLib.h>
#include <Library/DxeServicesTableLib.h>
#include <Guid/EventGroup.h>
 
#include <Library/PlatformSysCtrlLib.h>
#include "I2CLibInternal.h"
 
STATIC EFI_EVENT              mI2cLibVirtualAddrChangeEvent;
 
STATIC UINTN gI2cBase[MAX_SOCKET][I2C_PORT_MAX];
 
UINTN GetI2cBase (UINT32 Socket, UINT8 Port)
{
  if (gI2cBase[Socket][Port] == 0) {
    gI2cBase[Socket][Port] = PlatformGetI2cBase(Socket, Port);
  }
 
  return gI2cBase[Socket][Port];
}
 
VOID
EFIAPI
I2cLibVirtualNotifyEvent (
  IN EFI_EVENT        Event,
  IN VOID             *Context
  )
{
  UINT32 Socket;
  UINT8  Port;
 
  // We assume that all I2C ports used in one runtime driver need to be
  // converted into virtual address.
  for (Socket = 0; Socket < MAX_SOCKET; Socket++) {
    for (Port = 0; Port < I2C_PORT_MAX; Port++) {
      if (gI2cBase[Socket][Port] != 0) {
        EfiConvertPointer (0x0, (VOID **)&gI2cBase[Socket][Port]);
      }
    }
  }
 
  return;
}
 
EFI_STATUS
I2cLibRuntimeSetup (UINT32 Socket, UINT8 Port)
{
  EFI_STATUS Status;
 
  UINTN Base = GetI2cBase (Socket, Port);
 
  // Declare the controller as EFI_MEMORY_RUNTIME
  Status = gDS->AddMemorySpace (
      EfiGcdMemoryTypeMemoryMappedIo,
      Base, 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 (Base, 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,
        I2cLibVirtualNotifyEvent,
        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;
}