#@TYPE: Machine
#@NAME: Generic zynqmp
#@DESCRIPTION: zynqmp devices

# Deprecated board config
USE_BOARD = "${@"conf/machine/include/xilinx-board-pre.inc" if d.getVar("BOARD") or d.getVar("BOARD_VARIANT") else ""}"
require ${USE_BOARD}
unset USE_BOARD

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

# CG is the lowest common demoninator, so use this by default
SOC_VARIANT ?= "cg"

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

# Machine features must result in a superset
# Basic features:
MACHINE_FEATURES += "rtc ext2 ext3 vfat usbhost"
# Ultra96 features:
MACHINE_FEATURES += " usbgadget wifi bluetooth"

# Qemu Xilinx Native when targeting ZynqMP generic requries the pmu rom
DEPENDS:append:pn-qemu-xilinx-native = " pmu-rom-native"

EXTRA_IMAGEDEPENDS += "libyaml-native python3-cython-native python3-pyyaml-native"

UBOOT_MACHINE ?= "xilinx_zynqmp_virt_defconfig"
SPL_BINARY ?= "spl/boot.bin"

# Default SD image build onfiguration, use qemu-sd to pad
IMAGE_CLASSES += "image-types-xilinx-qemu"
IMAGE_FSTYPES += "wic.qemu-sd"
WKS_FILES ?= "sdimage-bootpart.wks"

SERIAL_CONSOLES ?= "115200;ttyPS0"

MACHINE_ESSENTIAL_EXTRA_RDEPENDS += "device-tree"

# We need a generic one that works with QEMU...
HDF_MACHINE = "zcu102-zynqmp"

EXTRA_IMAGEDEPENDS += " \
		u-boot-zynq-uenv \
		arm-trusted-firmware \
		qemu-devicetrees \
		virtual/boot-bin \
		virtual/bootloader \
		u-boot-zynq-scr \
		"

IMAGE_BOOT_FILES += " \
		uEnv.txt \
		atf-uboot.ub \
		${@bb.utils.contains('PREFERRED_PROVIDER_virtual/dtb', 'device-tree', 'system.dtb', '', d)} \
		boot.scr \
		"

# This machine has a QEMU model, runqemu setup:
QB_MEM = "-m 4096"
QB_OPT_APPEND ?= "-nographic -serial mon:stdio -serial null"
QB_NETWORK_DEVICE = "-net nic -net nic -net nic -net nic,netdev=net0,macaddr=@MAC@"

# Replicate BootROM like behaviour, having loaded SPL and PMU(ROM+FW)
QB_OPT_APPEND:append:qemuboot-xilinx = " \
		-hw-dtb ${DEPLOY_DIR_IMAGE}/qemu-hw-devicetrees/multiarch/zcu102-arm.dtb \
		${@qemu_zynqmp_unhalt(d, True)} \
		-device loader,file=${DEPLOY_DIR_IMAGE}/arm-trusted-firmware.elf,cpu-num=0 \
		-device loader,file=${DEPLOY_DIR_IMAGE}/u-boot.elf \
		-device loader,file=${DEPLOY_DIR_IMAGE}/system.dtb,addr=0x100000 \
		"

# Attach the rootfs disk image to the second SD interface of QEMU (which is SD0)
QB_DEFAULT_FSTYPE:qemuboot-xilinx = "wic.qemu-sd"
QB_OPT_APPEND:append:qemuboot-xilinx = " -boot mode=5"
QB_ROOTFS_OPT:qemuboot-xilinx = " -drive if=sd,index=1,file=@ROOTFS@,format=raw"

QB_PMU_OPT = " \
		-M microblaze-fdt \
		-display none \
		-hw-dtb ${DEPLOY_DIR_IMAGE}/qemu-hw-devicetrees/multiarch/zynqmp-pmu.dtb \
		-kernel ${PMU_ROM} \
		-device loader,file=${PMU_FIRMWARE_DEPLOY_DIR}/${PMU_FIRMWARE_IMAGE_NAME}.elf \
		-device loader,addr=0xfd1a0074,data=0x1011003,data-len=4 \
		-device loader,addr=0xfd1a007C,data=0x1010f03,data-len=4 \
		"
QB_OPT_APPEND:append:qemuboot-xilinx = " -pmu-args '${QB_PMU_OPT}'"

do_write_qemuboot_conf[depends] += "u-boot-zynq-uenv:do_deploy"

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

# Deprecated board config
USE_BOARD = "${@"conf/machine/include/xilinx-board-post.inc" if d.getVar("BOARD") or d.getVar("BOARD_VARIANT") else ""}"
require ${USE_BOARD}
unset USE_BOARD
