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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/**
*
*  Copyright (c) 2018, Marvell International Ltd. All rights reserved.
*
*  SPDX-License-Identifier: BSD-2-Clause-Patent
*
**/
 
#include "MvGpioDxe.h"
 
STATIC MV_GPIO *mGpioInstance;
 
STATIC MV_GPIO_DEVICE_PATH mDevicePathTemplate = {
  {
    {
      HARDWARE_DEVICE_PATH,
      HW_VENDOR_DP,
      {
        (UINT8) (sizeof (VENDOR_DEVICE_PATH) +
                 sizeof (MV_GPIO_DRIVER_TYPE)),
        (UINT8) ((sizeof (VENDOR_DEVICE_PATH) +
                 sizeof (MV_GPIO_DRIVER_TYPE)) >> 8),
      },
    },
    EFI_CALLER_ID_GUID
  },
  MV_GPIO_DRIVER_TYPE_SOC_CONTROLLER,
  {
    END_DEVICE_PATH_TYPE,
    END_ENTIRE_DEVICE_PATH_SUBTYPE,
    {
      sizeof(EFI_DEVICE_PATH_PROTOCOL),
      0
    }
  }
};
 
#if !defined(MDEPKG_NDEBUG)
/**
 
Routine Description:
 
  Verifies if controller index / GPIO pin values
  are within proper boundaries.
 
Arguments:
 
  ControllerIndex - index of controller
  GpioPin - which pin to read
 
Returns:
 
  EFI_SUCCESS           - GPIO pin / controller index are proper
  EFI_INVALID_PARAMETER - GPIO pin / controller index is out of range
**/
STATIC
EFI_STATUS
MvGpioValidate (
  IN UINTN ControllerIndex,
  IN UINTN GpioPin
  )
{
  if (ControllerIndex >= mGpioInstance->GpioDeviceCount) {
    DEBUG ((DEBUG_ERROR,
      "%a: Invalid GPIO ControllerIndex: %d\n",
      __FUNCTION__,
      ControllerIndex));
    return EFI_INVALID_PARAMETER;
  }
 
  if (GpioPin >= mGpioInstance->SoCGpio[ControllerIndex].InternalGpioCount) {
    DEBUG ((DEBUG_ERROR,
      "%a: GPIO pin #%d not available in Controller#%d\n",
      __FUNCTION__,
      GpioPin,
      ControllerIndex));
    return EFI_INVALID_PARAMETER;
  }
 
  return EFI_SUCCESS;
}
#endif
 
/**
 
Routine Description:
 
  Gets the mode (function) of a GPIO pin
 
Arguments:
 
  This  - pointer to protocol
  Gpio  - which pin
  Mode  - pointer to output mode value
 
Returns:
 
  EFI_SUCCESS           - mode value retrieved
  EFI_INVALID_PARAMETER - Mode is a null pointer or Gpio pin is out of range
 
**/
STATIC
EFI_STATUS
MvGpioGetMode (
  IN  EMBEDDED_GPIO       *This,
  IN  EMBEDDED_GPIO_PIN    Gpio,
  OUT EMBEDDED_GPIO_MODE  *Mode
  )
{
  UINTN ControllerIndex;
  UINTN BaseAddress;
  UINTN GpioPin;
 
  GpioPin = GPIO_PIN (Gpio);
  ControllerIndex = GPIO_PORT (Gpio);
 
  ASSERT_EFI_ERROR (MvGpioValidate (ControllerIndex, GpioPin));
 
  BaseAddress = mGpioInstance->SoCGpio[ControllerIndex].RegisterBase;
 
  if (MmioRead32 (BaseAddress + MV_GPIO_OUT_EN_REG) & (1 << GpioPin)) {
    *Mode = GPIO_MODE_INPUT;
  } else {
    if (MmioRead32 (BaseAddress + MV_GPIO_DATA_IN_REG) & (1 << GpioPin)) {
      *Mode = GPIO_MODE_OUTPUT_1;
    } else {
      *Mode = GPIO_MODE_OUTPUT_0;
    }
  }
 
  return EFI_SUCCESS;
}
 
/**
 
Routine Description:
 
  Gets the state of a GPIO pin
 
Arguments:
 
  This  - pointer to protocol
  Gpio  - which pin to read
  Value - state of the pin
 
Returns:
 
  EFI_SUCCESS           - GPIO state returned in Value
  EFI_INVALID_PARAMETER - Value is NULL pointer or Gpio pin is out of range
**/
STATIC
EFI_STATUS
MvGpioGet (
  IN  EMBEDDED_GPIO      *This,
  IN  EMBEDDED_GPIO_PIN   Gpio,
  OUT UINTN              *Value
  )
{
  UINTN ControllerIndex;
  UINTN BaseAddress;
  UINTN GpioPin;
 
  GpioPin = GPIO_PIN (Gpio);
  ControllerIndex = GPIO_PORT (Gpio);
 
  ASSERT_EFI_ERROR (MvGpioValidate (ControllerIndex, GpioPin));
 
  BaseAddress = mGpioInstance->SoCGpio[ControllerIndex].RegisterBase;
 
  if (MmioRead32 (BaseAddress + MV_GPIO_DATA_IN_REG) & (1 << GpioPin)) {
    *Value = 1;
  } else {
    *Value = 0;
  }
 
  return EFI_SUCCESS;
}
 
/**
 
Routine Description:
 
  Sets the state of a GPIO pin
 
Arguments:
 
  This  - pointer to protocol
  Gpio  - which pin to modify
  Mode  - mode to set
 
Returns:
 
  EFI_SUCCESS           - GPIO set as requested
  EFI_UNSUPPORTED       - Mode is not supported
  EFI_INVALID_PARAMETER - Gpio pin is out of range
**/
STATIC
EFI_STATUS
MvGpioSet (
  IN EMBEDDED_GPIO       *This,
  IN EMBEDDED_GPIO_PIN    Gpio,
  IN EMBEDDED_GPIO_MODE   Mode
  )
{
  UINTN ControllerIndex;
  UINTN BaseAddress;
  UINTN GpioPin;
 
  GpioPin = GPIO_PIN (Gpio);
  ControllerIndex = GPIO_PORT (Gpio);
 
  ASSERT_EFI_ERROR (MvGpioValidate (ControllerIndex, GpioPin));
 
  BaseAddress = mGpioInstance->SoCGpio[ControllerIndex].RegisterBase;
 
  switch (Mode) {
  case GPIO_MODE_OUTPUT_0:
    MmioAnd32 (BaseAddress + MV_GPIO_DATA_OUT_REG, ~(1 << GpioPin));
    MmioAnd32 (BaseAddress + MV_GPIO_OUT_EN_REG, ~(1 << GpioPin));
    break;
 
  case GPIO_MODE_OUTPUT_1:
    MmioOr32 (BaseAddress + MV_GPIO_DATA_OUT_REG, (1 << GpioPin));
    MmioAnd32 (BaseAddress + MV_GPIO_OUT_EN_REG, ~(1 << GpioPin));
    break;
 
  case GPIO_MODE_INPUT:
    MmioOr32 (BaseAddress + MV_GPIO_OUT_EN_REG, (1 << GpioPin));
    break;
 
  default:
    return EFI_UNSUPPORTED;
  }
 
  return EFI_SUCCESS;
}
 
/**
 
Routine Description:
 
  Sets the pull-up / pull-down resistor of a GPIO pin
 
Arguments:
 
  This  - pointer to protocol
  Gpio  - which pin
  Direction - pull-up, pull-down, or none
 
Returns:
 
  EFI_UNSUPPORTED - Can not perform the requested operation
 
**/
EFI_STATUS
EFIAPI
MvGpioSetPull (
  IN  EMBEDDED_GPIO       *This,
  IN  EMBEDDED_GPIO_PIN   Gpio,
  IN  EMBEDDED_GPIO_PULL  Direction
  )
{
  return EFI_UNSUPPORTED;
}
 
STATIC
VOID
MvGpioInitProtocol (
  IN EMBEDDED_GPIO *GpioProtocol
  )
{
  GpioProtocol->Get     = MvGpioGet;
  GpioProtocol->Set     = MvGpioSet;
  GpioProtocol->GetMode = MvGpioGetMode;
  GpioProtocol->SetPull = MvGpioSetPull;
}
 
EFI_STATUS
EFIAPI
MvGpioEntryPoint (
  IN EFI_HANDLE       ImageHandle,
  IN EFI_SYSTEM_TABLE *SystemTable
  )
{
  MARVELL_BOARD_DESC_PROTOCOL *MvBoardProtocol;
  MV_BOARD_GPIO_DESCRIPTION *GpioDescription;
  MV_GPIO_DEVICE_PATH *GpioDevicePath;
  EFI_STATUS Status;
 
  GpioDevicePath = AllocateCopyPool (sizeof (MV_GPIO_DEVICE_PATH),
                     &mDevicePathTemplate);
  if (GpioDevicePath == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }
 
  mGpioInstance = AllocateZeroPool (sizeof (MV_GPIO));
  if (mGpioInstance == NULL) {
    Status = EFI_OUT_OF_RESOURCES;
    goto ErrGpioInstanceAlloc;
  }
 
  /* Obtain list of available controllers */
  Status = gBS->LocateProtocol (&gMarvellBoardDescProtocolGuid,
                  NULL,
                  (VOID **)&MvBoardProtocol);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR,
      "%a: Cannot locate BoardDesc protocol\n",
      __FUNCTION__));
    goto ErrLocateBoardDesc;
  }
 
  Status = MvBoardProtocol->GpioDescriptionGet (MvBoardProtocol,
             &GpioDescription);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR,
      "%a: Cannot get GPIO board desc from BoardDesc protocol\n",
      __FUNCTION__));
    goto ErrLocateBoardDesc;
  }
 
  mGpioInstance->Signature = MV_GPIO_SIGNATURE;
  mGpioInstance->GpioDeviceCount = GpioDescription->GpioDeviceCount;
  mGpioInstance->SoCGpio = GpioDescription->SoCGpio;
 
  MvGpioInitProtocol (&mGpioInstance->GpioProtocol);
 
  Status = gBS->InstallMultipleProtocolInterfaces (&(mGpioInstance->Handle),
                  &gEmbeddedGpioProtocolGuid,
                  &(mGpioInstance->GpioProtocol),
                  &gEfiDevicePathProtocolGuid,
                  (EFI_DEVICE_PATH_PROTOCOL *)GpioDevicePath,
                  NULL);
  if (EFI_ERROR (Status)) {
    goto ErrLocateBoardDesc;
  }
 
  return EFI_SUCCESS;
 
ErrLocateBoardDesc:
  gBS->FreePool (mGpioInstance);
 
ErrGpioInstanceAlloc:
  gBS->FreePool (GpioDevicePath);
 
  return Status;
}