#@TYPE: Machine
#@NAME: versal-generic
#@DESCRIPTION: Machine configuration for the versal-generic devices

#### Preamble
MACHINEOVERRIDES =. "${@['', 'versal-generic:']['versal-generic' != '${MACHINE}']}"
#### Regular settings follow

# Variables that changes based on hw design or board specific requirement must be
# defined before calling the required inclusion file else pre-expansion value
# defined in local.conf without machine override will not be reflected.

# Yocto Versal device-tree variables
YAML_CONSOLE_DEVICE_CONFIG:pn-device-tree ?= "CIPS_0_pspmc_0_psv_sbsauart_0"
DT_PADDING_SIZE:pn-device-tree ?= "0x1000"
DTC_FLAGS:pn-device-tree = "-@"
YAML_DT_BOARD_FLAGS ?= "{BOARD versal-vck190-reva-x-ebm-01-reva}"

# Yocto Versal u-boot-xlnx variables
UBOOT_MACHINE ?= "xilinx_versal_virt_defconfig"
BOOTMODE ?= "generic.root"

# Yocto Versal arm-trusted-firmware(TF-A) variables
TFA_BL33_LOAD ?= "0x8000000"

# Yocto Versal PLM variables
YAML_SERIAL_CONSOLE_STDIN:pn-plm-firmware ?= "CIPS_0_pspmc_0_psv_sbsauart_0"
YAML_SERIAL_CONSOLE_STDOUT:pn-plm-firmware ?= "CIPS_0_pspmc_0_psv_sbsauart_0"

# Yocto Versal KERNEL Variables
UBOOT_ENTRYPOINT  ?= "0x200000"
UBOOT_LOADADDRESS ?= "0x200000"

# Versal Serial Console
SERIAL_CONSOLES ?= "115200;ttyAMA0"
YAML_SERIAL_CONSOLE_BAUDRATE ?= "115200"

require conf/machine/include/soc-versal.inc
require conf/machine/include/machine-xilinx-default.inc
require conf/machine/include/machine-xilinx-qemu.inc

# versal-generic.conf uses vck190-versal xsa as reference input.
# User can override with custom xsa using HDF_BASE and HDF_PATH variables from
# local.conf.
HDF_MACHINE = "vck190-versal"

MACHINE_FEATURES += "rtc ext2 ext3 vfat usbhost"

MACHINE_ESSENTIAL_EXTRA_RDEPENDS += "${PREFERRED_PROVIDER_virtual/dtb}"

# Default SD image build onfiguration, use qemu-sd to pad
IMAGE_CLASSES += "image-types-xilinx-qemu"
# Add wic.qemu-sd only if initramfs_image not set due to circular dependecies
IMAGE_FSTYPES += "${@'wic.qemu-sd' if (d.getVar('INITRAMFS_IMAGE') or '') == '' else 'cpio.gz.u-boot.qemu-sd-fatimg'}"

EXTRA_IMAGEDEPENDS += " \
    libyaml-native \
    python3-cython-native \
    python3-pyyaml-native \
    arm-trusted-firmware \
    virtual/boot-bin \
    virtual/bootloader \
    virtual/psm-firmware \
    virtual/plm \
    u-boot-xlnx-scr \
    qemu-devicetrees \
    virtual/cdo \
    "

IMAGE_BOOT_FILES += " \
    boot.bin \
    ${@bb.utils.contains('PREFERRED_PROVIDER_virtual/dtb', 'device-tree', 'system.dtb', '', d)} \
    Image \
    boot.scr \
    "

# Versal QEMU Configurations
# This machine has a QEMU model, runqemu setup:
QB_MEM = "-m 8G"
QB_DEFAULT_KERNEL = "none"
# Iteration appears to be eth0 then eth1
QB_NETWORK_DEVICE = "-net nic,netdev=net0,macaddr=@MAC@ -net nic"
QB_KERNEL_CMDLINE_APPEND ?= ""

QEMU_HW_DTB_PATH = "${DEPLOY_DIR_IMAGE}/qemu-hw-devicetrees/multiarch"
QEMU_HW_DTB_PS = "${QEMU_HW_DTB_PATH}/board-versal-ps-vck190.dtb"
QEMU_HW_DTB_PMC = "${QEMU_HW_DTB_PATH}/board-versal-pmc-vc-p-a2197-00.dtb"

QEMU_HW_DTB_PS_vp1202-versal = "${QEMU_HW_DTB_PATH}/board-versal-vp1202-ps-virt.dtb"
QEMU_HW_DTB_PMC_vp1202-versal = "${QEMU_HW_DTB_PATH}/board-versal-vp1202-pmc-virt.dtb"

QB_OPT_APPEND = " \
    -hw-dtb ${QEMU_HW_DTB_PS} \
    -serial null -serial null  \
    ${@qemu_add_extra_args(d)} \
    "

# PLM instance args
QB_PLM_OPT = " \
    -M microblaze-fdt \
    -device loader,file=${DEPLOY_DIR_IMAGE}/BOOT-${MACHINE}_bh.bin,addr=0xF201E000,force-raw  \
    -device loader,addr=0xf0000000,data=0xba020004,data-len=4 \
    -device loader,addr=0xf0000004,data=0xb800fffc,data-len=4 \
    -device loader,file=${DEPLOY_DIR_IMAGE}/CDO/pmc_cdo.bin,addr=0xf2000000,force-raw \
    -device loader,file=${DEPLOY_DIR_IMAGE}/plm-${MACHINE}.elf,cpu-num=1 \
    -device loader,addr=0xF1110624,data=0x0,data-len=4 \
    -device loader,addr=0xF1110620,data=0x1,data-len=4 \
    -hw-dtb ${QEMU_HW_DTB_PMC} \
    -display none \
    "
QB_OPT_APPEND += " -plm-args '${QB_PLM_OPT}'"

#### No additional settings should be after the Postamble
#### Postamble
PACKAGE_EXTRA_ARCHS:append = "${@['', ' versal_generic']['versal-generic' != "${MACHINE}"]}"
