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
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
/** @file
  Phytium Spi Master Drivers.
 
  Copyright (C) 2020, Phytium Technology Co Ltd. All rights reserved.<BR>
 
  SPDX-License-Identifier: BSD-2-Clause-Patent
 
**/
#include <Library/DebugLib.h>
#include <Library/IoLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Uefi/UefiBaseType.h>
#include "SpiDxe.h"
 
PHYT_SPI_MASTER *pSpiMasterInstance;
static UINTN     mSpiControlBase;
 
/**
  This function inited a spi driver.
 
  @param  None.
 
  @retval None.
 
**/
EFI_STATUS
EFIAPI
SpiMasterInit (
  VOID
  )
{
  return EFI_SUCCESS;
}
 
 
/**
  This function seted config to spi registers.
 
  @param[in]  CmdId    The id of command.
 
  @param[in]  Config   The value to be seted.
 
  @param[in]  RegAddr  The address of spi registers.
 
  @retval EFI_SUCCESS  SpiMasterSetConfig() is executed successfully.
 
**/
EFI_STATUS
EFIAPI
SpiMasterSetConfig (
  IN UINT8  CmdId,
  IN UINT32 Config,
  IN UINTN  RegAddr
  )
{
  UINTN SpiAddr;
  UINT32 Value;
 
  SpiAddr = 0;
  Value   = 0;
 
  if (CmdId != 0) {
    Value = (CmdId << 24) | (Config & 0xffffff);
  } else {
    Value = Config;
  }
 
  SpiAddr = mSpiControlBase + RegAddr;
  MmioWrite32 (SpiAddr, Value);
 
  return EFI_SUCCESS;
}
 
 
/**
  This function geted config from spi registers.
 
  @param[in]  CmdId    The id of command.
 
  @param[out] Config   The pointer of the config.
 
  @param[in]  RegAddr  The address of spi registers.
 
  @retval EFI_SUCCESS  SpiMasterGetConfig() is executed successfully.
 
**/
EFI_STATUS
EFIAPI
SpiMasterGetConfig (
  IN  UINT8  CmdId,
  OUT UINT32 *Config,
  IN  UINTN  RegAddr
  )
{
  UINTN SpiAddr;
  UINT32 Value;
 
  SpiAddr = 0;
  Value   = 0;
 
  SpiAddr = mSpiControlBase + RegAddr;
  Value = MmioRead32 (SpiAddr);
 
  if (CmdId != 0) {
    *Config = Value & 0xffffff;
  } else {
    *Config = Value;
  }
 
  return EFI_SUCCESS;
}
 
 
/**
  This function seted spi mode.
 
  @param[in] Config    The value to seted.
 
  @retval EFI_SUCCESS  SpiMasterSetMode() is executed successfully.
 
**/
EFI_STATUS
EFIAPI
SpiMasterSetMode (
  IN UINT32 Config
  )
{
 
  SpiMasterSetConfig (0, Config, REG_MODE_REG);
 
  return EFI_SUCCESS;
}
 
 
/**
  This function inited the spi driver protocol.
 
  @param[in] SpiMasterProtocol    A pointer to the master protocol struct.
 
  @retval EFI_SUCCESS             SpiMasterInitProtocol() is executed successfully.
 
**/
STATIC
EFI_STATUS
EFIAPI
SpiMasterInitProtocol (
  IN EFI_SPI_DRV_PROTOCOL *SpiMasterProtocol
  )
{
 
  SpiMasterProtocol->SpiInit        = SpiMasterInit;
  SpiMasterProtocol->SpiSetConfig   = SpiMasterSetConfig;
  SpiMasterProtocol->SpiGetConfig   = SpiMasterGetConfig;
  SpiMasterProtocol->SpiSetMode     = SpiMasterSetMode;
 
  return EFI_SUCCESS;
}
 
 
/**
  This function is the entrypoint of the spi driver.
 
  @param[in] ImageHandle    The firmware allocated handle for the EFI image.
 
  @param[in] SystemTable    A pointer to the EFI System Table.
 
  @retval EFI_SUCCESS       The entry point is executed successfully.
 
  @retval other             Some error occurs when executing this entry point.
 
**/
EFI_STATUS
EFIAPI
SpiMasterDrvEntryPoint (
  IN EFI_HANDLE         ImageHandle,
  IN EFI_SYSTEM_TABLE   *SystemTable
  )
{
  EFI_STATUS  Status;
 
  pSpiMasterInstance = AllocateRuntimeZeroPool (sizeof (PHYT_SPI_MASTER));
  if (pSpiMasterInstance == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }
 
  mSpiControlBase = FixedPcdGet64 (PcdSpiControllerBase);
 
  SpiMasterInitProtocol (&pSpiMasterInstance->SpiMasterProtocol);
 
  pSpiMasterInstance->Signature = SPI_MASTER_SIGNATURE;
 
  Status = gBS->InstallMultipleProtocolInterfaces (
                &(pSpiMasterInstance->Handle),
                &gSpiMasterProtocolGuid,
                &(pSpiMasterInstance->SpiMasterProtocol),
                NULL
                );
  ASSERT_EFI_ERROR (Status);
 
  return EFI_SUCCESS;
}