summaryrefslogtreecommitdiffstats
path: root/MdeModulePkg/Bus/Spi/SpiBus/SpiBusDxe.c
blob: cd0a2c99a27b12738e4e0433535de44661f4aa2a (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
/** @file

  SPI bus DXE driver

  Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent

**/
#include <Base.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Protocol/SpiConfiguration.h>
#include <Protocol/SpiHc.h>
#include <Protocol/SpiIo.h>
#include "SpiBus.h"

/**
  Entry point of the Spi Bus layer

  @param[in] ImageHandle  Image handle of this driver.
  @param[in] SystemTable  Pointer to standard EFI system table.

  @retval EFI_SUCCESS           Succeed.
  @retval EFI_DEVICE_ERROR      SpiPeripheral is NULL.
  @retval EFI_NOT_FOUND         Fail to locate SpiHcProtocol or SpiIoConfigurationProtocol
  @retval EFI_OUT_OF_RESOURCES  Failed to allocate SpiIoChip
**/
EFI_STATUS
EFIAPI
SpiBusEntry (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_STATUS                      Status;
  SPI_IO_CHIP                     *SpiChip;
  EFI_SPI_HC_PROTOCOL             *SpiHc;
  EFI_SPI_CONFIGURATION_PROTOCOL  *SpiConfiguration;
  EFI_SPI_PERIPHERAL              *SpiPeripheral;
  EFI_SPI_BUS                     *Bus;
  UINTN                           BusIndex;
  UINTN                           HcIndex;
  EFI_HANDLE                      *SpiHcHandles;
  UINTN                           HandleCount;
  EFI_DEVICE_PATH_PROTOCOL        *SpiHcDevicePath;

  DEBUG ((DEBUG_VERBOSE, "%a - ENTRY\n", __func__));

  // Get all SPI HC protocols, could be multiple SPI HC's on a single platform
  Status = gBS->LocateHandleBuffer (
                  ByProtocol,
                  &gEfiSpiHcProtocolGuid,
                  NULL,
                  &HandleCount,
                  &SpiHcHandles
                  );

  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_VERBOSE, "No SpiHcProtocol is found\n"));
    Status = EFI_NOT_FOUND;
    goto Exit;
  }

  // Locate the SPI Configuration Protocol
  Status = gBS->LocateProtocol (
                  &gEfiSpiConfigurationProtocolGuid,
                  NULL,
                  (VOID **)&SpiConfiguration
                  );

  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_VERBOSE, "No SpiConfigurationProtocol is found\n"));
    Status = EFI_NOT_FOUND;
    goto Exit;
  }

  // Parse through Hc protocols, find correct device path
  for (HcIndex = 0; HcIndex < HandleCount; HcIndex++) {
    Status = gBS->HandleProtocol (
                    SpiHcHandles[HcIndex],
                    &gEfiDevicePathProtocolGuid,
                    (VOID **)&SpiHcDevicePath
                    );

    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_VERBOSE, "Error locating EFI device path for this SPI controller, status=%r \n", Status));
      continue; // Continue searching
    }

    // Parse through SpiConfiguration's SpiBuses, find matching devicepath for SpiHc
    for (BusIndex = 0; BusIndex < SpiConfiguration->BusCount; BusIndex++) {
      Bus = (EFI_SPI_BUS *)SpiConfiguration->Buslist[BusIndex];
      if (!DevicePathsAreEqual (SpiHcDevicePath, Bus->ControllerPath)) {
        DEBUG ((DEBUG_VERBOSE, "SpiHc and SpiConfig device paths dont match, continue parsing\n"));
        continue;
      }

      DEBUG ((
        DEBUG_VERBOSE,
        "%a: Found matching device paths, Enumerating SPI BUS: %s with DevicePath: %s\n",
        __func__,
        Bus->FriendlyName,
        ConvertDevicePathToText (SpiHcDevicePath, FALSE, FALSE)
        ));

      // Get SpiHc from the SpiHcHandles
      Status = gBS->HandleProtocol (
                      SpiHcHandles[HcIndex],
                      &gEfiDevicePathProtocolGuid,
                      (VOID **)&SpiHc
                      );

      if (EFI_ERROR (Status)) {
        DEBUG ((DEBUG_VERBOSE, "%a - Error getting SpiHc from Handle\n", __func__));
        goto Exit;
      }

      SpiPeripheral = (EFI_SPI_PERIPHERAL *)Bus->Peripherallist;
      if (SpiPeripheral != NULL) {
        do {
          DEBUG ((
            DEBUG_VERBOSE,
            "%a: Installing SPI IO protocol for %s, by %s, PN=%s\n",
            __func__,
            SpiPeripheral->FriendlyName,
            SpiPeripheral->SpiPart->Vendor,
            SpiPeripheral->SpiPart->PartNumber
            ));
          // Allocate the SPI IO Device
          SpiChip = AllocateZeroPool (sizeof (SPI_IO_CHIP));
          ASSERT (SpiChip != NULL);
          if (SpiChip != NULL) {
            // Fill in the SpiChip
            SpiChip->Signature                      = SPI_IO_SIGNATURE;
            SpiChip->SpiConfig                      = SpiConfiguration;
            SpiChip->SpiHc                          = SpiHc;
            SpiChip->SpiBus                         = Bus;
            SpiChip->Protocol.SpiPeripheral         = SpiPeripheral;
            SpiChip->Protocol.OriginalSpiPeripheral = SpiPeripheral;
            SpiChip->Protocol.FrameSizeSupportMask  = SpiHc->FrameSizeSupportMask;
            SpiChip->Protocol.MaximumTransferBytes  = SpiHc->MaximumTransferBytes;
            if ((SpiHc->Attributes & HC_TRANSFER_SIZE_INCLUDES_ADDRESS) != 0) {
              SpiChip->Protocol.Attributes |= SPI_IO_TRANSFER_SIZE_INCLUDES_ADDRESS;
            }

            if ((SpiHc->Attributes & HC_TRANSFER_SIZE_INCLUDES_OPCODE) != 0) {
              SpiChip->Protocol.Attributes |= SPI_IO_TRANSFER_SIZE_INCLUDES_OPCODE;
            }

            if ((SpiHc->Attributes & HC_SUPPORTS_8_BIT_DATA_BUS_WIDTH) != 0) {
              SpiChip->Protocol.Attributes |= SPI_IO_SUPPORTS_8_BIT_DATA_BUS_WIDTH;
            }

            if ((SpiHc->Attributes & HC_SUPPORTS_4_BIT_DATA_BUS_WIDTH) != 0) {
              SpiChip->Protocol.Attributes |= SPI_IO_SUPPORTS_4_BIT_DATA_BUS_WIDTH;
            }

            if ((SpiHc->Attributes & HC_SUPPORTS_2_BIT_DATA_BUS_WIDTH) != 0) {
              SpiChip->Protocol.Attributes |= SPI_IO_SUPPORTS_2_BIT_DATA_BUS_WIDTH;
            }

            SpiChip->Protocol.Transaction         = Transaction;
            SpiChip->Protocol.UpdateSpiPeripheral = UpdateSpiPeripheral;
            // Install the SPI IO Protocol
            Status = gBS->InstallProtocolInterface (
                            &SpiChip->Handle,
                            (GUID *)SpiPeripheral->SpiPeripheralDriverGuid,
                            EFI_NATIVE_INTERFACE,
                            &SpiChip->Protocol
                            );
            if (EFI_ERROR (Status)) {
              DEBUG ((DEBUG_VERBOSE, "%a - Error installing SpiIoProtocol\n", __func__));
              continue;
            }
          } else {
            Status = EFI_OUT_OF_RESOURCES;
            DEBUG ((
              DEBUG_ERROR,
              "%a: Out of Memory resources\n",
              __func__
              ));
            break;
          }

          SpiPeripheral = (EFI_SPI_PERIPHERAL *)SpiPeripheral->NextSpiPeripheral;
        } while (SpiPeripheral != NULL);
      } else {
        Status = EFI_DEVICE_ERROR;
      }
    }
  }

Exit:
  DEBUG ((DEBUG_VERBOSE, "%a - EXIT (Status = %r)\n", __func__, Status));
  return Status;
}