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

# 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
require conf/machine/include/xilinx-board.inc

MACHINE_FEATURES = "rtc ext2 ext3 vfat usbhost"

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"
KERNEL_DEVICETREE = "xilinx/zynqmp-zcu102-rev1.0.dtb"

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"

# PMU instance args
PMU_ROM ?= "${DEPLOY_DIR_IMAGE}/pmu-rom.elf"
PMU_FIRMWARE_DEPLOY_DIR ?=  "${@ '${TOPDIR}/pmutmp/deploy/images/zynqmp-pmu' if d.getVar('BMULTICONFIG') == 'pmu' else '${TOPDIR}/tmp/deploy/images/${MACHINE}'}"
PMU_FIRMWARE_IMAGE_NAME ?= "${@ 'pmu-firmware-zynqmp-pmu' if d.getVar('BBMULTICONFIG') == 'pmu' else 'pmu-${MACHINE}'}"

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"
