:name: i.MX 8M Plus Linux
:description: This script runs Linux on NXP i.MX 8M Plus

$name?="frdm_imx8mp"
using sysbus
mach create $name

set plat """
using "platforms/cpus/nxp-mimx8ml.repl"

memory_ddr_cortexA_cluster:
    size: 0x100000000

gpt1:
    frequency: 24000000

gpt2:
    frequency: 24000000

gpt3:
    frequency: 24000000

gpt4:
    frequency: 24000000

gpt5:
    frequency: 24000000

gpt6:
    frequency: 24000000

// eCSPI2 external chip-select (GPIO5)
ecspi2:
    externalChipSelect: true
"""

machine LoadPlatformDescriptionFromString $plat

using sysbus.ca53Cluster

$uboot_elf?=@https://dl.antmicro.com/projects/renode/u-boot-s_10073016-46fad25d02e0cb851495d007cc4802cac4e08834
$uboot_dtb?=@https://dl.antmicro.com/projects/renode/imx8mp-frdm.dtb-s_83584-a112d75a6de330d9bdd682c90e656491c2bd3c4e
$bl31_elf?=@https://dl.antmicro.com/projects/renode/bl31.elf-s_67984-0399ce1fd2cc1be77a20338f8a47e02467287ef0
$sd_img?=@https://dl.antmicro.com/projects/renode/sd.img-s_268435456-b5a85f15cceb52baf5dc215966e809181fe14f31

# Spoof i.MX8MP Boot ROM at 0x980
# https://github.com/nxp-imx/uboot-imx/blob/99518e6/arch/arm/mach-imx/romapi.c#L44
# Point g_rom_api->query_boot_infor (at offset +0x10) with our stub at 0x1000.
sysbus WriteQuadWord 0x990 0x1000 cpu0

# Fake query_boot_infor(info_type=w0, info=x1, xor=w2):
# *info = 0x20000 (boot_type = BT_DEV_TYPE_MMC (0x2), boot_instance = 0x0)
# w2 holds the unused `xor` arg on entry; we clobber it as a scratch reg.
cpu0 AssembleBlock 0x1000 """
  movz w2, #2, lsl #16;   // w2 = 0x20000
  str  w2, [x1];          // *info = 0x20000
  movz w0, #0xf0;         // return ROM_API_OKAY (0xF0)
  ret
"""

showAnalyzer uart2

machine SdCardFromFile $sd_img usdhc3 size=0x20000000 persistent=False name="emmc-internal-eMMC5_1" emmc=true

macro reset
"""
    sysbus LoadELF $uboot_elf
    sysbus LoadBinary $uboot_dtb `sysbus GetSymbolAddress "_end"`
    sysbus LoadELF $bl31_elf

    ca53Cluster ForEach EnableUbootMode
    ca53Cluster ForEach IsHalted true
    # cpu0 is unhalted to start U-Boot; remaining cores are released by PSCI
    cpu0 IsHalted false

    # Disable the power-on Watchdog Timer. On hardware this is handled by the bootrom
    wdog1 WriteWord 0x8 0x0
    wdog2 WriteWord 0x8 0x0
    wdog3 WriteWord 0x8 0x0
"""

runMacro $reset

