/*
 * Copyright (c) 2018 Linaro Limited
 * Copyright (c) 2018 Hisilicon Limited
 *. All rights reserved.
 * SPDX-License-Identifier: BSD-2-Clause-Patent
 *
*/

#include <IndustryStandard/Acpi.h>
#include <IndustryStandard/DebugPort2Table.h>
#include <Library/AcpiLib.h>
#include <Library/PcdLib.h>
#include "Hi1620Platform.h"

#define NUMBER_DEBUG_DEVICE_INFO    1
#define NUMBER_OF_GENERIC_ADDRESS   1
#define NAMESPACE_STRING_SIZE       12
#define UART_LENGTH                 0x1000

#pragma pack(1)

typedef struct {
  EFI_ACPI_DBG2_DEBUG_DEVICE_INFORMATION_STRUCT DdiHeader;
  EFI_ACPI_6_1_GENERIC_ADDRESS_STRUCTURE Address[NUMBER_OF_GENERIC_ADDRESS];
  UINT32 AddressSize[NUMBER_OF_GENERIC_ADDRESS];
  CHAR8  NamespaceString[NAMESPACE_STRING_SIZE];
} EFI_ACPI_DBG2_DDI_STRUCT;

typedef struct {
  EFI_ACPI_DEBUG_PORT_2_DESCRIPTION_TABLE Desc;
  EFI_ACPI_DBG2_DDI_STRUCT Ddi[NUMBER_DEBUG_DEVICE_INFO];
} EFI_ACPI_DEBUG_PORT_2_TABLE;

#pragma pack()

EFI_ACPI_DEBUG_PORT_2_TABLE Dbg2 = {
  {
    ARM_ACPI_HEADER(
      EFI_ACPI_6_1_DEBUG_PORT_2_TABLE_SIGNATURE,
      EFI_ACPI_DEBUG_PORT_2_TABLE,
      EFI_ACPI_DEBUG_PORT_2_TABLE_REVISION
      ),
    OFFSET_OF(EFI_ACPI_DEBUG_PORT_2_TABLE, Ddi),
    NUMBER_DEBUG_DEVICE_INFO
  },
  {
    {
      {
        EFI_ACPI_DBG2_DEBUG_DEVICE_INFORMATION_STRUCT_REVISION,
        sizeof(EFI_ACPI_DBG2_DDI_STRUCT),
        NUMBER_OF_GENERIC_ADDRESS,
        NAMESPACE_STRING_SIZE,
        OFFSET_OF(EFI_ACPI_DBG2_DDI_STRUCT, NamespaceString),
        0,  //OemDataLength
        0,  //OemDataOffset
        EFI_ACPI_DBG2_PORT_TYPE_SERIAL,
         EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_ARM_PL011_UART,
        {EFI_ACPI_RESERVED_BYTE, EFI_ACPI_RESERVED_BYTE},
        OFFSET_OF(EFI_ACPI_DBG2_DDI_STRUCT, Address),
        OFFSET_OF(EFI_ACPI_DBG2_DDI_STRUCT, AddressSize),
      },
      {
        {
          EFI_ACPI_6_1_SYSTEM_MEMORY,
          32,
          0,
          EFI_ACPI_6_1_BYTE,
          FixedPcdGet64 (PcdSerialDbgRegisterBase)
        }
      },
      {
        UART_LENGTH
      },
      "\\_SB.COM0"
    }
  }
};

//
// Reference the table being generated to prevent the optimizer from removing the
// data structure from the executable
//
VOID* CONST ReferenceAcpiTable = &Dbg2;
