Secure96Dxe.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /** @file
  2. 96boards Secure96 mezzanine board DXE driver.
  3. Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiDxe.h>
  7. #include <libfdt.h>
  8. #include <Library/BaseLib.h>
  9. #include <Library/DebugLib.h>
  10. #include <Library/DxeServicesLib.h>
  11. #include <Library/UefiBootServicesTableLib.h>
  12. #include <Library/UefiLib.h>
  13. #include <Protocol/LsConnector.h>
  14. #include <Protocol/Mezzanine.h>
  15. #include "Secure96.h"
  16. #define SECURE96_SSDT_OEM_TABLE_ID SIGNATURE_64('S','E','C','U','R','E','9','6')
  17. STATIC CONST UINT32 mI2cAtmelSha204aSlaveAddress[] = {
  18. ATSHA204A_SLAVE_ADDRESS,
  19. //
  20. // The Atmel AtSha204a has an annoying 'wake' mode where it will only wake
  21. // up if SDA is held low for a certain amount of time. Attempting to access
  22. // a device at address 0x0 at 100 kHz should be sufficient to create this
  23. // wake condition, so add address 0x0 to the slave addresses.
  24. //
  25. 0
  26. };
  27. STATIC CONST EFI_I2C_DEVICE mI2c0Devices[] = {
  28. {
  29. &gAtSha204aI2cDeviceGuid, // DeviceGuid
  30. 0, // DeviceIndex
  31. 0, // HardwareRevision
  32. 0, // I2C bus configuration
  33. ARRAY_SIZE (mI2cAtmelSha204aSlaveAddress), // SlaveAddressCount
  34. mI2cAtmelSha204aSlaveAddress // SlaveAddressArray
  35. }
  36. };
  37. STATIC CONST CHAR8 mLedNodes[][46] = {
  38. "/fragment@2/__overlay__/gpio-leds/secure96-u1",
  39. "/fragment@2/__overlay__/gpio-leds/secure96-u2",
  40. "/fragment@2/__overlay__/gpio-leds/secure96-u3",
  41. "/fragment@2/__overlay__/gpio-leds/secure96-u4",
  42. };
  43. STATIC
  44. VOID
  45. SetOverlayFragmentTarget (
  46. VOID *Overlay,
  47. CONST CHAR8 *NodeName,
  48. CONST CHAR8 *Target
  49. )
  50. {
  51. INT32 Node;
  52. INT32 Err;
  53. Node = fdt_path_offset (Overlay, NodeName);
  54. ASSERT (Node > 0);
  55. Err = fdt_setprop (Overlay, Node, "target-path", Target,
  56. AsciiStrLen (Target) + 1);
  57. if (Err) {
  58. DEBUG ((DEBUG_ERROR, "%a: fdt_setprop() failed - %a\n",
  59. __FUNCTION__, fdt_strerror (Err)));
  60. }
  61. }
  62. STATIC
  63. VOID
  64. FixupOverlay (
  65. VOID *Dtb,
  66. VOID *Overlay
  67. )
  68. {
  69. INT32 Node;
  70. UINT32 GpioPhandle;
  71. UINTN Idx;
  72. UINT32 *GpioProp;
  73. INT32 Err;
  74. //
  75. // Set the correct GPIO phandle in the LED nodes
  76. //
  77. Node = fdt_path_offset (Dtb, FixedPcdGetPtr (PcdGpioParent));
  78. ASSERT (Node > 0);
  79. GpioPhandle = fdt_get_phandle (Dtb, Node);
  80. if (!GpioPhandle) {
  81. //
  82. // Node has no phandle yet -> create one
  83. //
  84. GpioPhandle = 1 + fdt_get_max_phandle (Dtb);
  85. ASSERT (GpioPhandle >= 1);
  86. Err = fdt_setprop_u32 (Dtb, Node, "phandle", GpioPhandle);
  87. if (Err) {
  88. DEBUG ((DEBUG_ERROR,
  89. "%a: fdt_setprop_u32(.., .., \"phandle\", 0x%x) failed - %a\n",
  90. __FUNCTION__, GpioPhandle, fdt_strerror (Err)));
  91. }
  92. }
  93. for (Idx = 0; Idx < ARRAY_SIZE (mLedNodes); Idx++) {
  94. Node = fdt_path_offset (Overlay, mLedNodes[Idx]);
  95. ASSERT (Node > 0);
  96. GpioProp = fdt_getprop_w (Overlay, Node, "gpios", NULL);
  97. ASSERT (GpioProp != NULL);
  98. *GpioProp = cpu_to_fdt32 (GpioPhandle);
  99. }
  100. SetOverlayFragmentTarget (Overlay, "/fragment@0",
  101. FixedPcdGetPtr (PcdI2c0Parent));
  102. SetOverlayFragmentTarget (Overlay, "/fragment@1",
  103. FixedPcdGetPtr (PcdSpiParent));
  104. }
  105. /**
  106. Apply the mezzanine's DT overlay
  107. @param[in] This Pointer to the MEZZANINE_PROTOCOL instance.
  108. @param[in,out] Dtb Pointer to the device tree blob
  109. @return EFI_SUCCESS Operation succeeded.
  110. @return other An error has occurred.
  111. **/
  112. STATIC
  113. EFI_STATUS
  114. ApplyDeviceTreeOverlay (
  115. IN MEZZANINE_PROTOCOL *This,
  116. IN OUT VOID *Dtb
  117. )
  118. {
  119. VOID *Overlay;
  120. UINTN OverlaySize;
  121. EFI_STATUS Status;
  122. INT32 Err;
  123. UINTN Index;
  124. //
  125. // Load the raw overlay DTB image from the raw section of this FFS file.
  126. //
  127. for (Index = 0;; Index++) {
  128. Status = GetSectionFromFv (&gEfiCallerIdGuid,
  129. EFI_SECTION_RAW, Index, &Overlay, &OverlaySize);
  130. if (EFI_ERROR (Status)) {
  131. return EFI_NOT_FOUND;
  132. }
  133. if (!fdt_check_header (Overlay)) {
  134. break;
  135. }
  136. }
  137. //
  138. // Fix up unresolved references in the overlay.
  139. //
  140. FixupOverlay (Dtb, Overlay);
  141. //
  142. // Merge the overlay with the DTB
  143. //
  144. Err = fdt_overlay_apply (Dtb, Overlay);
  145. if (Err) {
  146. DEBUG ((DEBUG_ERROR, "%a: fdt_overlay_apply() failed - %a\n",
  147. __FUNCTION__, fdt_strerror (Err)));
  148. return EFI_NOT_FOUND;
  149. }
  150. return EFI_SUCCESS;
  151. }
  152. /**
  153. Install the mezzanine's SSDT table
  154. @param[in] This Pointer to the MEZZANINE_PROTOCOL instance.
  155. @param[in] Dtb Pointer to the device tree blob
  156. @return EFI_SUCCESS Operation succeeded.
  157. @return other An error has occurred.
  158. **/
  159. STATIC
  160. EFI_STATUS
  161. EFIAPI
  162. InstallSsdtTable (
  163. IN MEZZANINE_PROTOCOL *This,
  164. IN EFI_ACPI_TABLE_PROTOCOL *AcpiProtocol
  165. )
  166. {
  167. EFI_ACPI_DESCRIPTION_HEADER *Ssdt;
  168. UINTN SsdtSize;
  169. EFI_STATUS Status;
  170. UINTN Index;
  171. UINTN TableKey;
  172. //
  173. // Load SSDT table from the raw section of this FFS file.
  174. //
  175. for (Index = 0;; Index++) {
  176. Status = GetSectionFromFv (&gEfiCallerIdGuid, EFI_SECTION_RAW, Index,
  177. (VOID **)&Ssdt, &SsdtSize);
  178. if (EFI_ERROR (Status)) {
  179. return EFI_NOT_FOUND;
  180. }
  181. if (SsdtSize >= sizeof (EFI_ACPI_DESCRIPTION_HEADER) &&
  182. Ssdt->OemTableId == SECURE96_SSDT_OEM_TABLE_ID) {
  183. break;
  184. }
  185. }
  186. return AcpiProtocol->InstallAcpiTable (AcpiProtocol, Ssdt, SsdtSize,
  187. &TableKey);
  188. }
  189. STATIC MEZZANINE_PROTOCOL mMezzanine = {
  190. ApplyDeviceTreeOverlay,
  191. InstallSsdtTable,
  192. ARRAY_SIZE (mI2c0Devices),
  193. 0,
  194. mI2c0Devices,
  195. NULL,
  196. NULL,
  197. };
  198. EFI_STATUS
  199. EFIAPI
  200. Secure96DxeEntryPoint (
  201. IN EFI_HANDLE ImageHandle,
  202. IN EFI_SYSTEM_TABLE *SystemTable
  203. )
  204. {
  205. EFI_STATUS Status;
  206. LS_CONNECTOR_PROTOCOL *LsConnector;
  207. Status = gBS->LocateProtocol (&g96BoardsLsConnectorProtocolGuid, NULL,
  208. (VOID **)&LsConnector);
  209. ASSERT_EFI_ERROR (Status);
  210. if (LsConnector->MezzanineType != MezzanineSecure96) {
  211. return EFI_NOT_FOUND;
  212. }
  213. return gBS->InstallProtocolInterface (&ImageHandle,
  214. &g96BoardsMezzanineProtocolGuid,
  215. EFI_NATIVE_INTERFACE,
  216. &mMezzanine);
  217. }