diff --git a/arch/z80/src/ez80/ez80_spi.c b/arch/z80/src/ez80/ez80_spi.c index 60d56f2e0c1..559da0468e1 100644 --- a/arch/z80/src/ez80/ez80_spi.c +++ b/arch/z80/src/ez80/ez80_spi.c @@ -57,19 +57,17 @@ #include "chip.h" -#if defined(CONFIG_ARCH_CHIP_EZ80F91) || defined(CONFIG_ARCH_CHIP_EZ80F92) -# include "ez80f91_spi.h" -#endif +#include "ez80_spi.h" /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ #if defined(CONFIG_ARCH_CHIP_EZ80F91) || defined(CONFIG_ARCH_CHIP_EZ80F92) -# define GPIOB_SPI_SS (1 << 2) /* Pin 2: /SS (not used by driver) */ -# define GPIOB_SPI_SCK (1 << 3) /* Pin 3: SCK */ -# define GPIOB_SPI_MISO (1 << 6) /* Pin 6: MISO */ -# define GPIOB_SPI_MOSI (1 << 7) /* Pin 7: MOSI */ +# define GPIOB_SPI_SS (1 << 2) /* PB2: /SS (not used by driver) */ +# define GPIOB_SPI_SCK (1 << 3) /* PB3: SCK */ +# define GPIOB_SPI_MISO (1 << 6) /* PB6: MISO */ +# define GPIOB_SPI_MOSI (1 << 7) /* PB7: MOSI */ # define GPIOB_SPI_PINSET (GPIOB_SPI_SS | GPIOB_SPI_SCK | GPIOB_SPI_MISO | \ GPIOB_SPI_MOSI) diff --git a/arch/z80/src/ez80/ez80f91_spi.h b/arch/z80/src/ez80/ez80_spi.h similarity index 94% rename from arch/z80/src/ez80/ez80f91_spi.h rename to arch/z80/src/ez80/ez80_spi.h index 97911d1822a..39809f27bb7 100644 --- a/arch/z80/src/ez80/ez80f91_spi.h +++ b/arch/z80/src/ez80/ez80_spi.h @@ -1,7 +1,7 @@ /************************************************************************************ - * arch/z80/src/ez80/ez80f91_spi.h + * arch/z80/src/ez80/ez80_spi.h * - * Copyright (C) 2009-2010, 2015 Gregory Nutt. All rights reserved. + * Copyright (C) 2009-2010, 2015, 2020 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -33,8 +33,8 @@ * ************************************************************************************/ -#ifndef __ARCH_Z80_SRC_EZ80_EZ80F91_SPI_H -#define __ARCH_Z80_SRC_EZ80_EZ80F91_SPI_H +#ifndef __ARCH_Z80_SRC_EZ80_EZ80_SPI_H +#define __ARCH_Z80_SRC_EZ80_EZ80_SPI_H /************************************************************************************ * Included Files @@ -51,7 +51,7 @@ /* SPIC Registers ******************************************************************/ -/* Provided in ez80f91.h */ +/* Provided in ez80f9x.h */ /* SPIC Register Bit Definitions ***************************************************/ @@ -63,6 +63,7 @@ /* SPI Control (CTL) Register Definitions */ #define SPI_CTL_IRQEN (1 << 7) /* Bit 7: 1=SPI system interrupt is enabled */ + /* Bit 6: Reserved */ #define SPI_CTL_SPIEN (1 << 5) /* Bit 5: 1=SPI is enabled */ #define SPI_CTL_MASTEREN (1 << 4) /* Bit 4: 1=SPI operates as a master */ #define SPI_CTL_CPOL (1 << 3) /* Bit 3: 1=Master SCK pin idles in a high (1) state */ @@ -73,7 +74,9 @@ #define SPI_SR_SPIF (1 << 7) /* Bit 7: 1=SPI data transfer is finished */ #define SPI_SR_WCOL (1 << 6) /* Bit 6: 1=SPI write collision is detected*/ + /* Bit 5: Reserved */ #define SPI_SR_MODF (1 << 4) /* Bit 4: 1=Mode fault (multimaster conflict) is detected */ + /* Bits 0-3: Reserved */ /* RBR/TSR Register Definitions */ @@ -155,4 +158,4 @@ int ez80_spicmddata(FAR struct spi_dev_s *dev, uint32_t devid, bool cmd); #endif /* __cplusplus */ #endif /* __ASSEMBLY__ */ -#endif /* __ARCH_Z80_SRC_EZ80_EZ80F91_SPI_H */ +#endif /* __ARCH_Z80_SRC_EZ80_EZ80_SPI_H */ diff --git a/arch/z80/src/ez80/ez80_startup.asm b/arch/z80/src/ez80/ez80_startup.asm index 93943f27462..b1b863b2ca4 100644 --- a/arch/z80/src/ez80/ez80_startup.asm +++ b/arch/z80/src/ez80/ez80_startup.asm @@ -102,6 +102,7 @@ _ez80_startup: ; Position the IDLE task stack point at an offset of 1Kb in on-chip SRAM ; On-chip SRAM resides at an offset of %00e000 from the RAM base address. + ; REVISIT: CONFIG_IDLETHREAD_STACKSIZE is not used! ld sp, __RAM_ADDR_U_INIT_PARAM << 16 + %00e400 diff --git a/boards/z80/ez80/makerlisp/src/ez80_spi.c b/boards/z80/ez80/makerlisp/src/ez80_spi.c index 9448f017338..c9b2599128d 100644 --- a/boards/z80/ez80/makerlisp/src/ez80_spi.c +++ b/boards/z80/ez80/makerlisp/src/ez80_spi.c @@ -1,4 +1,4 @@ -/***************************************************************************** +/**************************************************************************** * boards/z80/ez80/makerlisp/src/ez80_spi.c * * Copyright (C) 2019 Greg Nutt. All rights reserved. @@ -32,7 +32,7 @@ * POSSIBILITY OF SUCH DAMAGE. ****************************************************************************/ -/***************************************************************************** +/**************************************************************************** * Included Files ****************************************************************************/ @@ -44,10 +44,10 @@ #include "chip.h" #include "z80_internal.h" -#include "ez80f91_spi.h" +#include "ez80_spi.h" #include "makerlisp.h" -/***************************************************************************** +/**************************************************************************** * Private Functions ****************************************************************************/ @@ -55,7 +55,7 @@ * Then we don't need to Card Detect callback here. */ -/***************************************************************************** +/**************************************************************************** * Public Functions ****************************************************************************/ @@ -92,7 +92,7 @@ void ez80_spidev_initialize(void) #endif } -/***************************************************************************** +/**************************************************************************** * The external functions, ez80_spiselect, ez80_spistatus, and * ez80_spicmddata must be provided by board-specific logic. These are * implementations of the select, status, and cmddata methods of the SPI diff --git a/boards/z80/ez80/makerlisp/src/ez80_spimmcsd.c b/boards/z80/ez80/makerlisp/src/ez80_spimmcsd.c index b455b7fecde..09450f3807d 100644 --- a/boards/z80/ez80/makerlisp/src/ez80_spimmcsd.c +++ b/boards/z80/ez80/makerlisp/src/ez80_spimmcsd.c @@ -1,4 +1,4 @@ -/***************************************************************************** +/**************************************************************************** * boards/z80/ez80/makerlisp/src/ez80_spimmcsd.c * * Copyright (C) 2019 Greg Nutt. All rights reserved. @@ -32,7 +32,7 @@ * POSSIBILITY OF SUCH DAMAGE. ****************************************************************************/ -/***************************************************************************** +/**************************************************************************** * Included Files ****************************************************************************/ @@ -48,12 +48,12 @@ #include #include "chip.h" -#include "ez80f91_spi.h" +#include "ez80_spi.h" #include "makerlisp.h" #ifdef HAVE_MMCSD -/***************************************************************************** +/**************************************************************************** * Private Functions ****************************************************************************/ @@ -64,11 +64,11 @@ * 2. Media Change callbacks are not yet implemented in the SPI driver. */ -/***************************************************************************** +/**************************************************************************** * Public Functions ****************************************************************************/ -/***************************************************************************** +/**************************************************************************** * Name: ez80_mmcsd_initialize * * Description: diff --git a/boards/z80/ez80/z20x/Kconfig b/boards/z80/ez80/z20x/Kconfig index 95eb6965758..725c3e3c146 100644 --- a/boards/z80/ez80/z20x/Kconfig +++ b/boards/z80/ez80/z20x/Kconfig @@ -5,10 +5,53 @@ if ARCH_BOARD_Z20X +config Z20X_BOOTLOADER + bool + default n + select BOOT_RUNFROMFLASH + +choice + prompt "Build selection" + default Z20X_PROGRAM + +config Z20X_SDBOOT + bool "SD boot loader" + select LIB_HEX2BIN + select Z20X_BOOTLOADER + depends on EZ80_SPI && MMCSD_SPI + ---help--- + Select this option only with the boards/z20x/sdboot + configuration. This will enable the components needed only by the + boot loader. + +config Z20X_W25BOOT + bool "W25 boot loader" + select Z20X_W25_CHARDEV + select LIB_HEX2BIN + select Z20X_BOOTLOADER + depends on EZ80_SPI && MTD_W25 + ---help--- + Select this option only with the boards/z20x/w25boot + configuration. This will enable the components needed only by the + boot loader. + +config Z20X_PROGRAM + bool "Loadable program" + select CONFIG_BOOT_RUNFROMEXTSRAM + ---help--- + Normal program that resides on SD card or in W25 FLASH and is + loaded into RAM by a bootloader. + +config Z20X_STANDALONE + bool "Standalone FLASH progroam" + select BOOT_RUNFROMFLASH + ---help--- + Select this option only to build a small program that executes + from z20x FLASH memory. + config Z20X_COPYTORAM bool "Copy to RAM" - default n - depends on BOOT_RUNFROMEXTSRAM + select BOOT_RUNFROMEXTSRAM ---help--- This option is available only when running from external SRAM (CONFIG_BOOT_RUNFROMEXTSRAM). In that case there are two possible @@ -17,13 +60,18 @@ config Z20X_COPYTORAM boots out of FLASH, copies itself to SRAM, and runs from SRAM. This configuration setting selects that latter option. -config Z20X_SDBOOT - bool "Build SD boot loader" - default n +endchoice + +config Z20X_W25_PROGSIZE + int "W25 partition size" + default 262144 + range 131072 524288 + depends on Z20X_W25BOOT ---help--- - Select this option only with the boards/z20x/sdboot - configuration. This will enable the components needed only by the - boot loader. + This is the size of the partition at the beginning to the W25 + serial FLASH that will be used to hold the boot program. Since + this program must run from SRAM, there would be no purpose int + making this size any larger than the size of the internal SRAM. choice prompt "Winbond W25 Usage" @@ -41,4 +89,11 @@ config Z20X_W25_MTDDEV bool "MTD device" endchoice + +config Z20X_W25_MINOR + int "W25 device minor number" + default 0 + range 0 255 + depends on Z20X_W25_CHARDEV + endif # ARCH_BOARD_Z20X diff --git a/boards/z80/ez80/z20x/configs/w25boot/.gitignore b/boards/z80/ez80/z20x/configs/w25boot/.gitignore new file mode 100644 index 00000000000..23b96039ad9 --- /dev/null +++ b/boards/z80/ez80/z20x/configs/w25boot/.gitignore @@ -0,0 +1,6 @@ +sdboot.hex +sdboot.map +sdboot.lod +sdboot.wsp +*.asm +Debug diff --git a/boards/z80/ez80/z20x/configs/w25boot/README.txt b/boards/z80/ez80/z20x/configs/w25boot/README.txt new file mode 100644 index 00000000000..963af49cfee --- /dev/null +++ b/boards/z80/ez80/z20x/configs/w25boot/README.txt @@ -0,0 +1,13 @@ +README.txt +^^^^^^^^^^ + +w25boot.zdsproj is a simple ZDS-II project that will allow you + to use the ZDS-II debugger. +w25boot.zfpproj is a simple project that will allow you to use the Smart Flash + Programming. NOTE: As of this writing this project does not work, probably + due to RAM configuration in the project. Use ZDS-II instead as is described + in the upper README.txt file +w25boot_flash.ztgt is the target file that accompanies the project files. This + one is identical to boards/scripts/z20x_ram.ztgt. +w25boot_ram.ztgt is the target file that accompanies the project files. This + one is identical to boards/scripts/z20x_flash.ztgt. diff --git a/boards/z80/ez80/z20x/configs/w25boot/defconfig b/boards/z80/ez80/z20x/configs/w25boot/defconfig new file mode 100644 index 00000000000..e1a96be9ab9 --- /dev/null +++ b/boards/z80/ez80/z20x/configs/w25boot/defconfig @@ -0,0 +1,40 @@ +# +# This file is autogenerated: PLEASE DO NOT EDIT IT. +# +# You can use "make menuconfig" to make any modifications to the installed .config file. +# You can then do "make savedefconfig" to generate a new defconfig file that includes your +# modifications. +# +CONFIG_ARCH="z80" +CONFIG_ARCH_BOARD="z20x" +CONFIG_ARCH_BOARD_Z20X=y +CONFIG_ARCH_CHIP="ez80" +CONFIG_ARCH_CHIP_EZ80=y +CONFIG_ARCH_CHIP_EZ80F92=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARCH_Z80=y +CONFIG_BOARD_LOOPSPERMSEC=1250 +CONFIG_DISABLE_MQUEUE=y +CONFIG_EZ80_SPI=y +CONFIG_EZ80_UART1=y +CONFIG_HOST_WINDOWS=y +CONFIG_MAX_TASKS=8 +CONFIG_MAX_WDOGPARMS=2 +CONFIG_MTD=y +CONFIG_MTD_W25=y +CONFIG_NFILE_DESCRIPTORS=6 +CONFIG_NFILE_STREAMS=6 +CONFIG_PREALLOC_TIMERS=4 +CONFIG_PREALLOC_WDOGS=4 +CONFIG_PTHREAD_STACK_DEFAULT=1024 +CONFIG_RAM_SIZE=65536 +CONFIG_SDCLONE_DISABLE=y +CONFIG_START_MONTH=3 +CONFIG_START_YEAR=2020 +CONFIG_UART1_BITS=0 +CONFIG_UART1_RXBUFSIZE=64 +CONFIG_UART1_SERIAL_CONSOLE=y +CONFIG_UART1_TXBUFSIZE=64 +CONFIG_USER_ENTRYPOINT="w25_main" +CONFIG_WDOG_INTRESERVE=1 +CONFIG_Z20X_W25BOOT=y diff --git a/boards/z80/ez80/z20x/configs/w25boot/sdboot.zdsproj b/boards/z80/ez80/z20x/configs/w25boot/sdboot.zdsproj new file mode 100644 index 00000000000..2ee448c7351 --- /dev/null +++ b/boards/z80/ez80/z20x/configs/w25boot/sdboot.zdsproj @@ -0,0 +1,261 @@ + +eZ80F92 + + + +..\..\..\nuttx.hex + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/boards/z80/ez80/z20x/configs/w25boot/sdboot.zfpproj b/boards/z80/ez80/z20x/configs/w25boot/sdboot.zfpproj new file mode 100644 index 00000000000..602c81cc7e7 --- /dev/null +++ b/boards/z80/ez80/z20x/configs/w25boot/sdboot.zfpproj @@ -0,0 +1,315 @@ + +eZ80F92 + + + +..\..\..\nuttx.hex + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/boards/z80/ez80/z20x/configs/w25boot/sdboot_flash.ztgt b/boards/z80/ez80/z20x/configs/w25boot/sdboot_flash.ztgt new file mode 100644 index 00000000000..b691cd51021 --- /dev/null +++ b/boards/z80/ez80/z20x/configs/w25boot/sdboot_flash.ztgt @@ -0,0 +1,69 @@ + + + + + true + 1f4 + 8 + 20000000 + + PLL + 20000000 + + + 0 + 140000 + FFFF + true + + + + 200000 + false + 040000 + 0BFFFF + + 1 + false + true + + + + + 0 + 8 + 4 + B + + + 0 + 0 + 0 + 0 + + + 0 + 0 + 0 + 0 + + + 0 + 0 + 0 + 0 + + + + 0 + AF + true + true + false + + 4 + + EZ80F92 + 1.0.1 + 1.00 + diff --git a/boards/z80/ez80/z20x/configs/w25boot/sdboot_ram.ztgt b/boards/z80/ez80/z20x/configs/w25boot/sdboot_ram.ztgt new file mode 100644 index 00000000000..8f78f6b941d --- /dev/null +++ b/boards/z80/ez80/z20x/configs/w25boot/sdboot_ram.ztgt @@ -0,0 +1,69 @@ + + + + + true + 1f4 + 8 + 20000000 + + PLL + 20000000 + + + 0 + 100000 + ffff + true + + + + 200000 + false + 000000 + FFFFF + + 1 + false + true + + + + + 0 + 8 + 0 + 7 + + + 0 + 0 + 0 + 0 + + + 0 + 0 + 0 + 0 + + + 0 + 0 + 0 + 0 + + + + 0 + AF + false + true + false + + 4 + + EZ80F92 + 1.0.1 + 1.00 + diff --git a/boards/z80/ez80/z20x/scripts/Make.defs b/boards/z80/ez80/z20x/scripts/Make.defs index 71d9b080885..eb72efec940 100644 --- a/boards/z80/ez80/z20x/scripts/Make.defs +++ b/boards/z80/ez80/z20x/scripts/Make.defs @@ -65,13 +65,22 @@ CPPFLAGS = $(CPPDEFINES) $(CPPINCLUDES) ARFLAGS = -quiet -warn # Linker definitions +# +# Possible configurations +# +# 1. FLASH-resident bootloader (CONFIG_Z20X_BOOTLOADER) +# 2. RAM-resident applications loaded by bootloader (CONFIG_Z20X_PROGRAM) +# 3. FLASH-resident, standalone program (CONFIG_Z20X_STANDALONE) +# 4. FLASH-resident, standalone copy-to-RAM program (CONFIG_Z20X_COPYTORAM) -ifeq ($(CONFIG_BOOT_RUNFROMFLASH),y) +ifeq ($(CONFIG_Z20X_BOOTLOADER),y) + LDSCRIPT = z20x_loader.linkcmd +else ifeq ($(CONFIG_Z20X_PROGRAM),y) + LDSCRIPT = z20x_program.linkcmd +else ifeq ($(CONFIG_Z20X_STANDALONE),y) LDSCRIPT = z20x_flash.linkcmd else ifeq ($(CONFIG_Z20X_COPYTORAM),y) LDSCRIPT = z20x_copytoram.linkcmd -else # ifeq ($(CONFIG_BOOT_RUNFROMEXTSRAM),y) - LDSCRIPT = z20x_ram.linkcmd endif LINKCMDTEMPLATE = $(TOPDIR)$(DELIM)boards$(DELIM)$(CONFIG_ARCH)$(DELIM)$(CONFIG_ARCH_CHIP)$(DELIM)$(CONFIG_ARCH_BOARD)$(DELIM)scripts$(DELIM)$(LDSCRIPT) diff --git a/boards/z80/ez80/z20x/scripts/z20x_loader.linkcmd b/boards/z80/ez80/z20x/scripts/z20x_loader.linkcmd new file mode 100644 index 00000000000..38d869d58ec --- /dev/null +++ b/boards/z80/ez80/z20x/scripts/z20x_loader.linkcmd @@ -0,0 +1,79 @@ +/*****************************************************************************/ +/* boards/z80/ez80/z20x/scripts/z20x_loader.linkcmd */ +/* For bootloader configurations running entirely out of FLASH */ +/* */ +/* Licensed to the Apache Software Foundation (ASF) under one or more */ +/* contributor license agreements. See the NOTICE file distributed with */ +/* this work for additional information regarding copyright ownership. The */ +/* ASF licenses this file to you under the Apache License, Version 2.0 (the */ +/* "License"); you may not use this file except in compliance with the */ +/* License. You may obtain a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT */ +/* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the */ +/* License for the specific language governing permissions and limitations */ +/* under the License. */ +/* */ +/*****************************************************************************/ + +-FORMAT=OMF695,INTEL32 +-map -maxhexlen=64 -quiet -warnoverlap -xref -unresolved=fatal +-sort NAME=ascending -warn -debug -NOigcase + +RANGE ROM $000000 : $01ffff +RANGE RAM $040000 : $04ffff +RANGE EXTIO $000000 : $00ffff +RANGE INTIO $000000 : $0000ff + +CHANGE STRSECT is ROM + +ORDER .RESET,.IVECTS,.STARTUP,CODE,DATA +COPY DATA ROM + +DEFINE __low_romdata = copy base of DATA +DEFINE __low_data = base of DATA +DEFINE __len_data = length of DATA +DEFINE __low_bss = base of BSS +DEFINE __len_bss = length of BSS +DEFINE __stack = highaddr of RAM + 1 +DEFINE __heaptop = highaddr of RAM +DEFINE __heapbot = top of RAM + 1 +DEFINE __low_romcode = copy base of CODE +DEFINE __low_code = base of CODE +DEFINE __len_code = length of CODE +DEFINE __copy_code_to_ram = 0 +DEFINE __crtl = 1 + +DEFINE __vecstart = $040000 +DEFINE __loaderstart = $040400 +DEFINE __loaderend = $04ffff +DEFINE __progstart = $050000 +DEFINE __progend = $0bffff + +DEFINE __CS0_LBR_INIT_PARAM = $04 +DEFINE __CS0_UBR_INIT_PARAM = $0b +DEFINE __CS0_CTL_INIT_PARAM = $08 +DEFINE __CS0_BMC_INIT_PARAM = $00 +DEFINE __CS1_LBR_INIT_PARAM = $00 +DEFINE __CS1_UBR_INIT_PARAM = $00 +DEFINE __CS1_CTL_INIT_PARAM = $00 +DEFINE __CS1_BMC_INIT_PARAM = $00 +DEFINE __CS2_LBR_INIT_PARAM = $00 +DEFINE __CS2_UBR_INIT_PARAM = $00 +DEFINE __CS2_CTL_INIT_PARAM = $00 +DEFINE __CS2_BMC_INIT_PARAM = $00 +DEFINE __CS3_LBR_INIT_PARAM = $00 +DEFINE __CS3_UBR_INIT_PARAM = $00 +DEFINE __CS3_CTL_INIT_PARAM = $00 +DEFINE __CS3_BMC_INIT_PARAM = $00 +DEFINE __RAM_CTL_INIT_PARAM = $80 +DEFINE __RAM_ADDR_U_INIT_PARAM = $AF +DEFINE __FLASH_CTL_INIT_PARAM = $88 +DEFINE __FLASH_ADDR_U_INIT_PARAM = $00 + +define _SYS_CLK_FREQ = 20000000 + +/* arch/z80/src/Makefile.zdsii will append target, object and library paths below */ diff --git a/boards/z80/ez80/z20x/scripts/z20x_ram.linkcmd b/boards/z80/ez80/z20x/scripts/z20x_program.linkcmd similarity index 93% rename from boards/z80/ez80/z20x/scripts/z20x_ram.linkcmd rename to boards/z80/ez80/z20x/scripts/z20x_program.linkcmd index 966a4566d65..d0949d35348 100644 --- a/boards/z80/ez80/z20x/scripts/z20x_ram.linkcmd +++ b/boards/z80/ez80/z20x/scripts/z20x_program.linkcmd @@ -1,5 +1,5 @@ /*****************************************************************************/ -/* boards/z80/ez80/z20x/scripts/z20x_ram.linkcmd */ +/* boards/z80/ez80/z20x/scripts/z20x_program.linkcmd */ /* For configurations running entirely out of RAM with nothing in FLASH */ /* */ /* Licensed to the Apache Software Foundation (ASF) under one or more */ @@ -24,7 +24,7 @@ -sort NAME=ascending -warn -debug -NOigcase RANGE ROM $000000 : $01FFFF -RANGE RAM $040000 : $0BFFFF +RANGE RAM $050000 : $0BFFFF RANGE EXTIO $000000 : $00FFFF RANGE INTIO $000000 : $0000FF @@ -50,6 +50,12 @@ DEFINE __len_code = length of CODE DEFINE __copy_code_to_ram = 0 DEFINE __crtl = 1 +DEFINE __vecstart = $040000 +DEFINE __loaderstart = $040400 +DEFINE __loaderend = $04ffff +DEFINE __progstart = $050000 +DEFINE __progend = $0bffff + DEFINE __CS0_LBR_INIT_PARAM = $04 DEFINE __CS0_UBR_INIT_PARAM = $0b DEFINE __CS0_CTL_INIT_PARAM = $08 diff --git a/boards/z80/ez80/z20x/src/Makefile b/boards/z80/ez80/z20x/src/Makefile index 83f54ec657c..1a6ab01ecd7 100644 --- a/boards/z80/ez80/z20x/src/Makefile +++ b/boards/z80/ez80/z20x/src/Makefile @@ -36,6 +36,8 @@ endif ifeq ($(CONFIG_Z20X_SDBOOT),y) CSRCS += sd_main.c +else ifeq ($(CONFIG_Z20X_W25BOOT),y) +CSRCS += w25_main.c endif ifeq ($(CONFIG_MTD_W25),y) diff --git a/boards/z80/ez80/z20x/src/ez80_boot.c b/boards/z80/ez80/z20x/src/ez80_boot.c index 0d3b13c14d6..3f060e8e9e9 100644 --- a/boards/z80/ez80/z20x/src/ez80_boot.c +++ b/boards/z80/ez80/z20x/src/ez80_boot.c @@ -27,6 +27,7 @@ #include #include +#include #include #include "chip.h" @@ -49,6 +50,12 @@ void ez80_board_initialize(void) { +#ifdef CONFIG_Z20X_PROGRAM + /* Recover memory used by the bootloader */ + + kmm_addregion((FAR void *)PROGSTART, PROGSIZE); +#endif + #ifdef CONFIG_EZ80_SPI /* Initialize SPI chip selects */ diff --git a/boards/z80/ez80/z20x/src/ez80_bringup.c b/boards/z80/ez80/z20x/src/ez80_bringup.c index f9f1db6becc..efbb37da794 100644 --- a/boards/z80/ez80/z20x/src/ez80_bringup.c +++ b/boards/z80/ez80/z20x/src/ez80_bringup.c @@ -59,13 +59,14 @@ int ez80_bringup(void) if (ret < 0) { ferr("ERROR: Failed to mount procfs at /proc: %d\n", ret); + return ret; } #endif #ifdef HAVE_SPIFLASH /* Initialize and register the W25 FLASH file system. */ - ret = ez80_w25_initialize(0); + ret = ez80_w25_initialize(CONFIG_Z20X_W25_MINOR); if (ret < 0) { ferr("ERROR: Failed to initialize W25 minor %d: %d\n", 0, ret); @@ -80,9 +81,10 @@ int ez80_bringup(void) if (ret < 0) { mcerr("ERROR: Failed to initialize SD card: %d\n", ret); + return ret; } #endif UNUSED(ret); - return ret; + return OK; } diff --git a/boards/z80/ez80/z20x/src/ez80_spi.c b/boards/z80/ez80/z20x/src/ez80_spi.c index a77dd07a5e3..c3222b28e1a 100644 --- a/boards/z80/ez80/z20x/src/ez80_spi.c +++ b/boards/z80/ez80/z20x/src/ez80_spi.c @@ -1,4 +1,4 @@ -/***************************************************************************** +/**************************************************************************** * boards/z80/ez80/z20x/src/ez80_spi.c * * Licensed to the Apache Software Foundation (ASF) under one or more @@ -18,7 +18,7 @@ * ****************************************************************************/ -/***************************************************************************** +/**************************************************************************** * Included Files ****************************************************************************/ @@ -30,10 +30,10 @@ #include "chip.h" #include "z80_internal.h" -#include "ez80f91_spi.h" +#include "ez80_spi.h" #include "z20x.h" -/***************************************************************************** +/**************************************************************************** * Private Functions ****************************************************************************/ @@ -41,7 +41,7 @@ * Then we don't need to Card Detect callback here. */ -/***************************************************************************** +/**************************************************************************** * Public Functions ****************************************************************************/ @@ -99,7 +99,7 @@ void ez80_spidev_initialize(void) #endif } -/***************************************************************************** +/**************************************************************************** * The external functions, ez80_spiselect, ez80_spistatus, and * ez80_spicmddata must be provided by board-specific logic. These are * implementations of the select, status, and cmddata methods of the SPI diff --git a/boards/z80/ez80/z20x/src/ez80_spimmcsd.c b/boards/z80/ez80/z20x/src/ez80_spimmcsd.c index 86938c4da44..2cf01ba8eff 100644 --- a/boards/z80/ez80/z20x/src/ez80_spimmcsd.c +++ b/boards/z80/ez80/z20x/src/ez80_spimmcsd.c @@ -1,4 +1,4 @@ -/***************************************************************************** +/**************************************************************************** * boards/z80/ez80/z20x/src/ez80_spimmcsd.c * * Licensed to the Apache Software Foundation (ASF) under one or more @@ -18,7 +18,7 @@ * ****************************************************************************/ -/***************************************************************************** +/**************************************************************************** * Included Files ****************************************************************************/ @@ -34,12 +34,12 @@ #include #include "chip.h" -#include "ez80f91_spi.h" +#include "ez80_spi.h" #include "z20x.h" #ifdef HAVE_MMCSD -/***************************************************************************** +/**************************************************************************** * Private Functions ****************************************************************************/ @@ -50,11 +50,11 @@ * 2. Media Change callbacks are not yet implemented in the SPI driver. */ -/***************************************************************************** +/**************************************************************************** * Public Functions ****************************************************************************/ -/***************************************************************************** +/**************************************************************************** * Name: ez80_mmcsd_initialize * * Description: diff --git a/boards/z80/ez80/z20x/src/ez80_w25.c b/boards/z80/ez80/z20x/src/ez80_w25.c index e4683316afe..462d0642fba 100644 --- a/boards/z80/ez80/z20x/src/ez80_w25.c +++ b/boards/z80/ez80/z20x/src/ez80_w25.c @@ -35,7 +35,7 @@ #include #include -#include "ez80f91_spi.h" +#include "ez80_spi.h" #include "z20x.h" /**************************************************************************** @@ -54,10 +54,6 @@ int ez80_w25_initialize(int minor) { FAR struct spi_dev_s *spi; FAR struct mtd_dev_s *mtd; -#ifdef CONFIG_Z20X_W25_CHARDEV - char blockdev[18]; - char chardev[12]; -#endif int ret; /* Get the SPI port */ @@ -98,17 +94,12 @@ int ez80_w25_initialize(int minor) return ret; } - /* Use the minor number to create device paths */ + /* Create a character device on the block device */ - snprintf(blockdev, 18, "/dev/mtdblock%d", minor); - snprintf(chardev, 12, "/dev/mtd%d", minor); - - /* Now create a character device on the block device */ - - ret = bchdev_register(blockdev, chardev, false); + ret = bchdev_register(W25_BLOCKDEV, W25_CHARDEV, false); if (ret < 0) { - ferr("ERROR: bchdev_register %s failed: %d\n", chardev, ret); + ferr("ERROR: bchdev_register %s failed: %d\n", W25_CHARDEV, ret); return ret; } #endif diff --git a/boards/z80/ez80/z20x/src/sd_main.c b/boards/z80/ez80/z20x/src/sd_main.c index 93961cba53e..7355874595c 100644 --- a/boards/z80/ez80/z20x/src/sd_main.c +++ b/boards/z80/ez80/z20x/src/sd_main.c @@ -44,12 +44,7 @@ #define MMCSD_MOUNTPT "/mnt/sdcard" #define MMCSD_HEXFILE "/mnt/sdcard/nuttx.hex" -#define SRAM_START 0x040000 -#define SRAM_SIZE 0x100000 -#define SRAM_END (SRAM_START + SRAM_SIZE) - -#define SRAM_RESET SRAM_START -#define SRAM_ENTRY ((sram_entry_t)SRAM_START) +#define SRAM_ENTRY ((sram_entry_t)PROGSTART) /**************************************************************************** * Private Types @@ -115,7 +110,7 @@ int sd_main(int argc, char *argv) /* Load the HEX image into memory */ - ret = hex2mem(fd, (uint32_t)SRAM_START, (uint32_t)SRAM_END, 0); + ret = hex2mem(fd, (uint32_t)PROGSTART, (uint32_t)PROGEND, 0); if (ret < 0) { /* We failed to load the HEX image */ diff --git a/boards/z80/ez80/z20x/src/w25_main.c b/boards/z80/ez80/z20x/src/w25_main.c new file mode 100644 index 00000000000..934b229d760 --- /dev/null +++ b/boards/z80/ez80/z20x/src/w25_main.c @@ -0,0 +1,492 @@ +/**************************************************************************** + * boards/z80/ez80/z20x/src/w25_main.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "z20x.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Configuration ************************************************************/ + +#ifndef HAVE_SPIFLASH +# error The W25 Serial FLASH is not available +#endif + +#ifndef CONFIG_Z20X_W25_CHARDEV +# error CONFIG_Z20X_W25_CHARDEV must be selected +#endif + +#if !defined(CONFIG_Z20X_W25_PROGSIZE) || CONFIG_Z20X_W25_PROGSIZE < 128 +# error Large CONFIG_Z20X_W25_PROGSIZE must be selected +#endif + +#define IOBUFFER_SIZE 512 +#define PROG_MAGIC 0xfedbad + +#define SRAM_ENTRY ((sram_entry_t)PROGSTART) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef CODE void (*sram_entry_t)(void); + +/* This is the header places at the beginning of the binary data in FLASH */ + +struct prog_header_s +{ + uint24_t magic; /* Valid if PROG_MAGIC */ + uint24_t crc; /* Last 24-bits of CRC32 */ + uint24_t len; /* Binary length */ +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: w25_read_hex + * + * Description: + * Read and parse a HEX data stream into RAM. HEX data will be read from + * stdin and written to the program area reserved in RAM, that is, the + * address range from PROGSTART to PROGREND. + * + ****************************************************************************/ + +static int w25_read_hex(FAR uint24_t *len) +{ + struct lib_rawinstream_s rawinstream; + struct lib_memsostream_s memoutstream; + int fd; + int ret; + + /* Open the W25 device for writing */ + + fd = open(W25_CHARDEV, O_WRONLY); + if (fd < 0) + { + ret = -get_errno(); + fprintf(stderr, "ERROR: Failed to open %s: %d\n", W25_CHARDEV, ret); + return ret; + } + + /* Wrap stdin as an IN stream that can get the HEX data over the serial port */ + + lib_rawinstream(&rawinstream, 0); + + /* Wrap the memory area used to hold the program as a seek-able OUT stream in + * which we can buffer the binary data. + */ + + lib_memsostream(&memoutstream, (FAR char *)PROGSTART, PROGSIZE); + + /* We are ready to load the Intel HEX stream into external SRAM. + * + * Hmm.. With no hardware handshake, there is a possibility of data loss + * to overrunning incoming data buffer. So far I have not seen this at + * 115200 8N1, but still it is a possibility. + */ + + printf("Send Intel HEX file now\n"); + fflush(stdout); + + ret = hex2bin(&rawinstream.public, &memoutstream.public, + (uint32_t)PROGSTART, (uint32_t)(PROGSTART + PROGSIZE), + 0); + + close(fd); + if (ret < 0) + { + /* We failed the load */ + + fprintf(stderr, "ERROR: Intel HEX file load failed: %d\n", ret); + return ret; + } + + printf("Successfully loaded the Intel HEX file into memory...\n"); + *len = memoutstream.public.nput; + return OK; +} + +/**************************************************************************** + * Name: w25_write + * + * Description: + * Write to the open file descriptor, handling failures and repeated + * write operations as necessary. + * + ****************************************************************************/ + +static int w25_write(int fd, FAR const void *src, size_t len) +{ + ssize_t remaining = len; + ssize_t nwritten; + + do + { + nwritten = write(fd, src, remaining); + if (nwritten <= 0) + { + int errcode = get_errno(); + if (errno != EINTR) + { + fprintf(stderr, "ERROR: Write failed: %d\n", errcode); + return -errcode; + } + } + else + { + remaining -= nwritten; + src += nwritten; + } + } + while (remaining > 0); + + return OK; +} + +/**************************************************************************** + * Name: w25_write_binary + * + * Description: + * Write a program header followed by the binary data beginning at address + * PROGSTART into the first partition of the w25 FLASH memory. + * + ****************************************************************************/ + +static int w25_write_binary(FAR const struct prog_header_s *hdr) +{ + int fd; + int ret; + + /* Open the W25 device for writing */ + + fd = open(W25_CHARDEV, O_WRONLY); + if (fd < 0) + { + ret = -get_errno(); + fprintf(stderr, "ERROR: Failed to open %s: %d\n", W25_CHARDEV, ret); + return ret; + } + + /* Write the hdr to the W25 */ + + ret = w25_write(fd, hdr, sizeof(struct prog_header_s)); + if (fd < 0) + { + ret = -get_errno(); + fprintf(stderr, "ERROR: Failed write program header: %d\n", ret); + goto errout; + } + + printf("Writing %lu bytes to the W25 Serial FLASH\n", (unsigned long)hdr->len); + + ret = w25_write(fd, (FAR const void *)PROGSTART, hdr->len); + if (ret < 0) + { + ret = -get_errno(); + fprintf(stderr, "ERROR: Failed write program header: %d\n", ret); + } + +errout: + close(fd); + return ret; +} + +/**************************************************************************** + * Name: w25_read + * + * Description: + * Read from the open file descriptor, handling errors as retries as + * necessary. + * + ****************************************************************************/ + +static int w25_read(int fd, FAR void *dest, size_t len) +{ + ssize_t remaining = len; + ssize_t nread; + + do + { + nread = read(fd, dest, remaining); + if (nread <= 0) + { + int errcode = get_errno(); + if (errno != EINTR) + { + fprintf(stderr, "ERROR: Read failed: %d\n", errcode); + close(fd); + return -errcode; + } + } + + remaining -= nread; + dest += nread; + } + while (remaining > 0); + + return OK; +} + +/**************************************************************************** + * Name: w25_read_binary + * + * Description: + * Read the program in the first partition of the W25 part into memory + * at PROGSTART. + * + ****************************************************************************/ + +static int w25_read_binary(FAR struct prog_header_s *hdr) +{ + int fd; + int ret; + + /* Open the W25 device for reading */ + + fd = open(W25_CHARDEV, O_RDONLY); + if (fd < 0) + { + ret = -get_errno(); + fprintf(stderr, "ERROR: Failed to open %s: %d\n", W25_CHARDEV, ret); + return ret; + } + + /* Read the header at the beginning of the partition */ + + ret = w25_read(fd, hdr, sizeof(hdr)); + if (ret < 0) + { + ret = -get_errno(); + fprintf(stderr, "ERROR: Failed read program header: %d\n", ret); + goto errout; + } + + /* Check for a valid program header */ + + if (hdr->magic != PROG_MAGIC) + { + fprintf(stderr, "ERROR: No program in FLASH\n"); + ret = -ENOENT; + goto errout; + } + + if (hdr->len != PROGSIZE) + { + fprintf(stderr, "ERROR: Program too big\n"); + ret = -E2BIG; + goto errout; + } + + /* Read the program binary */ + + ret = w25_read(fd, (FAR void *)PROGSTART, hdr->len); + if (ret < 0) + { + ret = -get_errno(); + fprintf(stderr, "ERROR: Failed read program header: %d\n", ret); + } + +errout: + close(fd); + return ret; +} + +/**************************************************************************** + * Name: w25_crc24 + * + * Description: + * Calculate a CRC24 checksum on the data loaded into memory starting at + * PROGSTART. + * + ****************************************************************************/ + +uint24_t w25_crc24(uint32_t len) +{ + FAR const uint8_t *src = (FAR const uint8_t *)PROGSTART; + uint32_t crc = 0; + int i; + int j; + + for (i = 0; i < len; i++) + { + uint8_t val = *src++; + crc ^= (uint32_t)val << 16; + + for (j = 0; j < 8; j++) + { + crc <<= 1; + if ((crc & 0x1000000) != 0) + { + crc ^= 0x1864cfb; + } + } + } + + return (uint24_t)crc; +} + +/**************************************************************************** + * Name: w25_read_verify + * + * Description: + * Verify that the program in the first partition of the W25 part matches + * the program in memory at address PROGSTART. + * + ****************************************************************************/ + +static int w25_read_verify(void) +{ + struct prog_header_s hdr; + uint24_t crc; + int fd; + int ret; + + /* Read the program image into memory */ + + ret = w25_read_binary(&hdr); + if (ret < 0) + { + ret = -get_errno(); + fprintf(stderr, "ERROR: Failed to read binary: %d\n", ret); + return ret; + } + + printf("Verifying %lu bytes in the W25 Serial FLASH\n", + (unsigned long)hdr.len); + + /* Compare CRCs */ + + crc = w25_crc24(hdr.len); + if (crc == hdr.crc) + { + printf("Successfully verified %lu bytes in the W25 Serial FLASH\n", + (unsigned long)hdr.len); + } + else + { + fprintf(stderr, "ERROR: CRC check failed\n"); + return -EINVAL; + } + + return OK; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: w25_main + * + * Description: + * w25_main is a tiny program that runs in ISRAM. w25_main will + * configure DRAM and the W25 serial FLASH, present a prompt, and load + * an Intel HEX file into the W25 serial FLASH (after first buffering + * the binary data into memory). On re-boot, the program loaded into + * the W25 FLASH should then execute. + * + * On entry: + * - SDRAM has already been initialized (we are using it for .bss!) + * - SPI0 chip select has already been configured. + * + ****************************************************************************/ + +int w25_main(int argc, char *argv) +{ + struct prog_header_s hdr; + int ret; + +#ifndef CONFIG_BOARD_LATE_INITIALIZE + /* Initialize the board. We can do this with a direct call because we are + * a kernel thread. + */ + + ret = ez80_bringup(); + if (ret < 0) + { + fprintf(stderr, "ERROR: Failed initialize system: %d\n", ret); + return EXIT_FAILURE; + } +#endif + + for (; ; ) + { + /* Read the HEX data into RAM */ + + memset(&hdr, 0, sizeof(struct prog_header_s)); + hdr.magic = PROG_MAGIC; + + ret = w25_read_hex(&hdr.len); + if (ret < 0) + { + fprintf(stderr, "ERROR: Failed to load HEX: %d\n", ret); + return EXIT_FAILURE; + } + + /* Calculate a CRC24 checksum */ + + hdr.crc = w25_crc24(hdr.len); + + /* The HEX file load was successful, write the data to FLASH */ + + ret = w25_write_binary(&hdr); + if (ret < 0) + { + fprintf(stderr, "ERROR: Failed to write to W25: %d\n", ret); + return EXIT_FAILURE; + } + + /* Now verify that the image in memory and the image in FLASH are + * truly the same. + */ + + ret = w25_read_verify(); + if (ret < 0) + { + fprintf(stderr, "ERROR: Failed to verify program: %d\n", ret); + return EXIT_FAILURE; + } + } + + return EXIT_SUCCESS; +} diff --git a/boards/z80/ez80/z20x/src/z20x.h b/boards/z80/ez80/z20x/src/z20x.h index 1ca9052c337..e5297458f2f 100644 --- a/boards/z80/ez80/z20x/src/z20x.h +++ b/boards/z80/ez80/z20x/src/z20x.h @@ -86,6 +86,45 @@ * Chip select 3 is not used */ +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* RAM Memory map + * + * 040000 Beginning of RAM + * 040000 _vecstart Beginning of Vector information. Used to hand off + * to RAM-based handlers for interrupts caught by + * FLASH interrupt vectors. 1Kb is set aside for RAM- + * based interrupt handling information. + * 040400 _loaderstart Start of RAM used exclusively by the bootloader. + * This memory region an be recovered by the RAM-based + * program. + * 04ffff _loaderend + * 050000 _progstart Start of CODE for the RAM-based program. The + * program can freely use the memory region from + * 050000-0bffff and can recover the memory for + * 40400-04ffff for heap usage. + * 0bffff _progend End of RAM + */ + +extern unsigned long _vecstart; +#define VECSTART ((uintptr_t)&_vecstart) + +extern unsigned long _loaderstart; +#define LOADERSTART ((uintptr_t)&_loaderstart) + +extern unsigned long _loaderend; +#define LOADEREND ((uintptr_t)&_loaderend) + +extern unsigned long _progstart; +#define PROGSTART ((uintptr_t)&_progstart) + +extern unsigned long _progend; +#define PROGEND ((uintptr_t)&_progend) + +#define PROGSIZE (PROGEND - PROGSTART + 1) + /* LED and port emulation memory register addresses */ /* GPIO data bit definitions */ @@ -99,8 +138,20 @@ #define EZ80_GPIOD6 (1 << 6) #define EZ80_GPIOD7 (1 << 7) +/* Winbond W25 SPI FLASH */ + +#ifndef CONFIG_Z20X_W25_MINOR +# define CONFIG_Z20X_W25_MINOR 0 +#endif + +#define __STR(s) #s +#define __XSTR(s) __STR(s) + +#define W25_CHARDEV "/dev/mtd" __XSTR(CONFIG_Z20X_W25_MINOR) +#define W25_BLOCKDEV "/dev/mtdblock" __XSTR(CONFIG_Z20X_W25_MINOR) + /**************************************************************************** - * Public Functions + * Public Function Prototypes ****************************************************************************/ #undef EXTERN @@ -128,7 +179,7 @@ extern "C" int ez80_bringup(void); -/***************************************************************************** +/**************************************************************************** * Name: ez80_mmcsd_initialize * * Description: