summaryrefslogtreecommitdiff
path: root/Platform/96Boards/Secure96Dxe/Secure96Dxe.c
blob: 6c48d7c0b0242c7be2a32432db4455c97057efff (plain)
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
/** @file
  96boards Secure96 mezzanine board DXE driver.

  Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>

  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 <libfdt.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/DxeServicesLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>
#include <Protocol/LsConnector.h>
#include <Protocol/Mezzanine.h>

#include "Secure96.h"

STATIC CONST UINT32 mI2cAtmelSha204aSlaveAddress[] = {
  ATSHA204A_SLAVE_ADDRESS,

  //
  // The Atmel AtSha204a has an annoying 'wake' mode where it will only wake
  // up if SDA is held low for a certain amount of time. Attempting to access
  // a device at address 0x0 at 100 kHz should be sufficient to create this
  // wake condition, so add address 0x0 to the slave addresses.
  //
  0
};

STATIC CONST EFI_I2C_DEVICE mI2c0Devices[] = {
  {
    &gAtSha204aI2cDeviceGuid,                     // DeviceGuid
    0,                                            // DeviceIndex
    0,                                            // HardwareRevision
    0,                                            // I2C bus configuration
    ARRAY_SIZE (mI2cAtmelSha204aSlaveAddress),    // SlaveAddressCount
    mI2cAtmelSha204aSlaveAddress                  // SlaveAddressArray
  }
};

STATIC CONST CHAR8 mLedNodes[][46] = {
  "/fragment@2/__overlay__/gpio-leds/secure96-u1",
  "/fragment@2/__overlay__/gpio-leds/secure96-u2",
  "/fragment@2/__overlay__/gpio-leds/secure96-u3",
  "/fragment@2/__overlay__/gpio-leds/secure96-u4",
};

STATIC
VOID
SetOverlayFragmentTarget (
  VOID            *Overlay,
  CONST CHAR8     *NodeName,
  CONST CHAR8     *Target
  )
{
  INT32       Node;
  INT32       Err;

  Node = fdt_path_offset (Overlay, NodeName);
  ASSERT (Node > 0);

  Err = fdt_setprop (Overlay, Node, "target-path", Target,
          AsciiStrLen (Target) + 1);
  if (Err) {
    DEBUG ((DEBUG_ERROR, "%a: fdt_setprop() failed - %a\n",
      __FUNCTION__, fdt_strerror (Err)));
  }
}

STATIC
VOID
FixupOverlay (
  VOID        *Dtb,
  VOID        *Overlay
  )
{
  INT32       Node;
  UINT32      GpioPhandle;
  UINTN       Idx;
  UINT32      *GpioProp;
  INT32       Err;

  //
  // Set the correct GPIO phandle in the LED nodes
  //
  Node = fdt_path_offset (Dtb, FixedPcdGetPtr (PcdGpioParent));
  ASSERT (Node > 0);

  GpioPhandle = fdt_get_phandle (Dtb, Node);
  if (!GpioPhandle) {
    //
    // Node has no phandle yet -> create one
    //
    GpioPhandle = 1 + fdt_get_max_phandle (Dtb);
    ASSERT (GpioPhandle >= 1);

    Err = fdt_setprop_u32 (Dtb, Node, "phandle", GpioPhandle);
    if (Err) {
      DEBUG ((DEBUG_ERROR,
        "%a: fdt_setprop_u32(.., .., \"phandle\", 0x%x) failed - %a\n",
        __FUNCTION__, GpioPhandle, fdt_strerror (Err)));
    }
  }

  for (Idx = 0; Idx < ARRAY_SIZE (mLedNodes); Idx++) {
    Node = fdt_path_offset (Overlay, mLedNodes[Idx]);
    ASSERT (Node > 0);

    GpioProp = fdt_getprop_w (Overlay, Node, "gpios", NULL);
    ASSERT (GpioProp != NULL);

    *GpioProp = cpu_to_fdt32 (GpioPhandle);
  }

  SetOverlayFragmentTarget (Overlay, "/fragment@0",
    FixedPcdGetPtr (PcdI2c0Parent));

  SetOverlayFragmentTarget (Overlay, "/fragment@1",
    FixedPcdGetPtr (PcdSpiParent));
}

/**
  Apply the mezzanine's DT overlay

  @param[in]      This      Pointer to the MEZZANINE_PROTOCOL instance.
  @param[in,out]  Dtb       Pointer to the device tree blob

  @return   EFI_SUCCESS     Operation succeeded.
  @return   other           An error has occurred.
**/
STATIC
EFI_STATUS
ApplyDeviceTreeOverlay (
  IN      MEZZANINE_PROTOCOL    *This,
  IN  OUT VOID                  *Dtb
  )
{
  VOID            *Overlay;
  UINTN           OverlaySize;
  EFI_STATUS      Status;
  INT32           Err;

  //
  // Load the raw overlay DTB image from the raw section of this FFS file.
  //
  Status = GetSectionFromFv (&gEfiCallerIdGuid,
             EFI_SECTION_RAW, 0, &Overlay, &OverlaySize);
  ASSERT_EFI_ERROR (Status);
  if (EFI_ERROR (Status)) {
    return EFI_NOT_FOUND;
  }

  //
  // Fix up unresolved references in the overlay.
  //
  FixupOverlay (Dtb, Overlay);

  //
  // Merge the overlay with the DTB
  //
  Err = fdt_overlay_apply (Dtb, Overlay);
  if (Err) {
    DEBUG ((DEBUG_ERROR, "%a: fdt_overlay_apply() failed - %a\n",
      __FUNCTION__, fdt_strerror (Err)));
    return EFI_NOT_FOUND;
  }

  return EFI_SUCCESS;
}

STATIC MEZZANINE_PROTOCOL mMezzanine = {
  ApplyDeviceTreeOverlay,
  ARRAY_SIZE (mI2c0Devices),
  0,
  mI2c0Devices,
  NULL,
  NULL,
};

EFI_STATUS
EFIAPI
Secure96DxeEntryPoint (
  IN EFI_HANDLE         ImageHandle,
  IN EFI_SYSTEM_TABLE   *SystemTable
  )
{
  EFI_STATUS              Status;
  LS_CONNECTOR_PROTOCOL   *LsConnector;

  Status = gBS->LocateProtocol (&g96BoardsLsConnectorProtocolGuid, NULL,
                  (VOID **)&LsConnector);
  ASSERT_EFI_ERROR (Status);

  if (LsConnector->MezzanineType != MezzanineSecure96) {
    return EFI_NOT_FOUND;
  }

  return gBS->InstallProtocolInterface (&ImageHandle,
                &g96BoardsMezzanineProtocolGuid,
                EFI_NATIVE_INTERFACE,
                &mMezzanine);
}