ATSAMA5D27-SOM1-EK1 - Booting from QSPI Flash Memory

Last modified by Microchip on 2024/06/20 12:43

Introduction

In this training, you will configure and program the ATSAMA5D27-SOM1-EK1 evaluation kit to boot the second- and third-stage bootloaders at91bootstrap and u-boot-at91 from NOR Flash Quad Serial Peripheral Interface (QSPI) memory and the Flattened Image Tree (FIT) (containing the Linux® kernel and Device Tree) and Root File System (ROOTFS) from an SD memory card. You will use the SAM-BA® In-System Programmer to write at91bootstrap and u-boot-at91 to QSPI Flash memory.

You will also use the SAM-BA In-System Programmer to read the Boot Configuration Word from the ATSAMA5D27-SOM1-EK1 evaluation kit, then configure the Boot Configuration Word to Backup Register 0 (BUREG0), and finally, set the Boot Configuration Word in BUREG0 to boot from QSPI Flash memory.

Prerequisites

A working cross-toolchain for Arm®-based MPU target is required

Basic knowledge of the SAM-BA In-System Programmer is required to use the software utility to write to QSPI Flash memory:

Knowledge of the Boot Process is required as this topic discusses how to load the bootloaders to QSPI Flash memory and Linux kernel/Device Tree/Root File System to SD memory card:

See also:

Hardware

You will be using the ATSAMA5D27-SOM1-EK1 evaluation kit.

Photo of the ATSAMA5D27-SOM1-EK1 Evaluation Kit

The ATSAMA5D27-SOM1-EK1 evaluation kit has four bootable memories:

  • J12 – MMCplus/SD memory card
  • J14 – microSD memory card
  • U4 – QSPI Flash memory (that is integrated in the ATSAMA5D27-SOM1 System on Module (SoM))
  • U9 – Printed Circuit Board (PCB) footprint for QSPI Flash memory (Unpopulated)

Bootable Memories

You will configure and build bootable files at91bootstrap and u-boot-at91 to be flashed into U4, the QSPI Flash memory (a 64 Mbit Quad Serial I/O™ Flash memory (SST26VF064B)), using the SAM-BA In-System Programmer.

ATSAMA5D27 System on Module (SOM) details

SAM-BA In-System Programmer

You will use the SAM-BA In-System Programmer to write at91bootstrap and u-boot-at91 to QSPI Flash memory.

For more information about the SAM-BA ISP, see the "SAM-BA® In-System Programmer (ISP)" page.

The SAM-BA ISP Host to Monitor serial communications is shown in the accompanying figure.

SAM-BA ISP Host to Monitor serial communications

Read and Configure the Boot Configuration Word

In this section, you will use the SAM-BA In-System Programmer to read the Boot Configuration Word from the ATSAMA5D27-SOM1-EK1 evaluation kit, then configure the Boot Configuration Word to Backup Register 0 (BUREG0), and finally, set the Boot Configuration Word in BUREG0 to boot from QSPI Flash Memory.

For more information on the SAMA5D2 Series Boot Process, see the "SAMA5D2 Series Boot Process" page.

The first step is to read the Boot Configuration Word to learn where it is stored (fuse or backup register) and which one is active.

For more information on reading and writing the Boot Configuration Word, see the "SAM-BA Applets - Read and Write Boot Configuration (bootconfig)" section.

Read the Boot Configuration Word

The first step is to read the Boot Configuration Word to learn where it is stored (fuse or backup register) and which one is active. To read the Boot Configuration Word located in the Secure Fuse Controller (SFC), execute the following command on the SAM-BA Host command line:

$ sam-ba -p serial -b sama5d27-som1-ek -a bootconfig -c readcfg:fuse

The result:

$ sam-ba -p serial -b sama5d27-som1-ek -a bootconfig -c readcfg:fuse
Opening serial port 'ttyACM1'
Connection opened.
Executing command 'readcfg:fuse'
FUSE=0x00020000 / QSPI0_IOSET1,QSPI1_IOSET1,SPI0_IOSET1,SPI1_IOSET1,NFC_IOSET1,SDMMC0,
SDMMC1,UART1_IOSET1,JTAG_IOSET3
Connection closed.

SAMA5D2 Series Boot Process

Observe that the Boot Configuration Word bit 22 (DISABLE_BSCR), disable read of Boot Sequence Controller Configuration Register (BSC_CR), is cleared. Therefore, the MPU will read the Boot Sequence Controller Configuration Register (BSC_CR).

You can read the Boot Sequence Controller Configuration Register (BSC_CR) with the following command:

$ sam-ba -p serial -b sama5d27-som1-ek -a bootconfig -c readcfg:bscr

The result:

$ sam-ba -p serial -b sama5d27-som1-ek -a bootconfig -c readcfg:bscr
Opening serial port 'ttyACM1'
Connection opened.
Executing command 'readcfg:bscr'
BSCR=0x00000000 / BUREG0
Connection closed.

Observe that the Boot Sequence Controller Configuration Register (BSC_CR) bit 2, validate the data in BUGREG_INDEX field, is cleared. Therefore, no BUREG contains valid boot configuration data.

In summary, the MPU reads the active Boot Configuration Word stored in the SFC. Bit 18, Enable Boot on External Memories, is set/enabled. All remaining bits are in their cleared/default state.

Back to Top


The Boot Configuration Word stored in the SFC can be overridden by the contents of one of four Backup Registers (BUREG). In this step, you will configure the MPU to read the Boot Configuration Word from Backup Register 0 (BUREG0), and in Step 3, you will set the Boot Configuration Word to boot from QSPI Flash memory.

For more information on the Backup Registers, see the SAMA5D2 Series datasheet (Section 16 – Boot Strategies).

During prototyping and development, it is highly recommended to use the Backup Registers (BUREG) to load the Boot Configuration Word. This will allow the developer to test the options of Boot Configuration Word in the Boot Process. Also, without permanently setting any bits in the SFC.

Once the device is put into production, it is highly recommended that the Boot Configuration Word be programmed into the SFC Data Register 16, bit 22 (SFC_DR16:22) must be set by the user.

To enable reading of the Boot Configuration Word from Backup Register 0 (BUREG0), perform the following steps:

Observe that the SFC Data Register 16, bit 22 (SFC_DR16:22), disable read of Boot Sequence Controller Configuration Register (BSC_CR), must be cleared to allow the reading of the Boot Sequence Controller Configuration Register (BSC_CR). As we observed in Step 1 above, this is true: SFC_SR16:22 = 0b0. The result will be when the MPU executes the first-stage bootloader (ROM Boot Code) it will read the Boot Sequence Controller Configuration Register.

Set the Boot Sequence Controller Configuration Register (BSC_CR) bit 2, Backup Register Valid (BUREG_VALID) bit. The result will be that the first-stage bootloader will read the BUREG pointed to by the Backup Register Index bits (shown in the next step).

The BSC_CR:BUREG_VALID bit can be set with the following command:

$ sam-ba -p serial -b sama5d27-som1-ek -a bootconfig -c writecfg:bscr:0x4

The result:

$ sam-ba -p serial -b sama5d27-som1-ek -a bootconfig -c readcfg:bscr
Opening serial port 'ttyACM1'
Connection opened.
Executing command 'readcfg:bscr'
BSCR=0x00000000 / BUREG0
Connection closed.

Backup Register Index bits indicate which BUREG will be used.

The Backup Register Index bits = 0b00. The result will be that Backup Register 0 (BUREG0) will be the register read during the first-stage boot process.

Back to Top


Set the Boot Configuration Word in BUREG0 to boot from QSPI Flash memory.

The following command will write a Boot Configuration Word to Backup Register 0 (BUREG0) to:

  • Boot from external memory, and
  • Configure peripheral QSPI1 to IOSET2. This will allow the use of the QSPI Flash memory located on the ATSAMA5D27-SOM1 module (Jumper J13 must be open).

The command also disables reading from QSPI0, SDMMC0, and SDMMC1.

For more information on the QSPI, see Section 50 of the SAMA5D2 Series datasheet (DS60001476).

$ sam-ba -p serial -b sama5d27-som1-ek -a bootconfig -c
writecfg:bureg0:QSPI0_DISABLED,QSPI1_IOSET2,SDMMC0_DISABLED,SDMMC1_DISABLED,EXT_MEM_BOOT

The result:

sam-ba -p serial -b sama5d27-som1-ek -a bootconfig -c writecfg:bureg0: QSPI0_DISABLED,QSPI1_IOSET2,SDMMC0_DISABLED,SDMMC1_DISABLED,EXT_MEM_BOOT
Opening serial port 'ttyACM1'
Connection opened.
Executing command 'writecfg:bureg0:QSPI0_DISABLED,QSPI1_IOSET2,SDMMC0_DISABLED,
SDMMC1_DISABLED,EXT_MEM_BOOT'
Setting BUREG0 to 0x00040c07 (QSPI0_DISABLED,QSPI1_IOSET2,SPI0_IOSET1,SPI1_IOSET1,NFC_IOSET1,
SDMMC0_DISABLED,SDMMC1_DISABLED,UART1_IOSET1,JTAG_IOSET1,EXT_MEM_BOOT)
Connection closed.

Upon the next reset, the ATSAMA5D27-SOM1-EK1 evaluation kit will boot from QSPI Flash memory located on the SAMA5D27-SOM1 module. However, the QSPI Flash memory is likely to be empty or hold old code. In the following sections, we show you how to first erase the QSPI Flash memory and then program the second and third-stage bootloaders, at91bootstrap and u-boot-at91.

Back to Top


Erase QSPI Flash Memory

In this section, you will erase the contents of the QSPI Flash memory located on the ATSAMA5D27-SOM1-EK1 evaluation kit. This is a necessary step prior to writing data to the QSPI Flash memory.

Erase the QSPI Flash memory on the SAMA5D27-SOM1

Erase the contents of the QSPI Flash memory on the SAMA5D27-SOM1 with the following command:

$ sam-ba -p serial -b sama5d27-som1-ek -a qspiflash -c erase
Opening serial port 'ttyACM1'
Connection opened.
Applet serialflash found Detected memory size is 4194304 bytes.
Executing command 'erase'
Erased 65536 bytes at address 0x00000000 (1.56%)
Erased 65536 bytes at address 0x00010000 (3.13%)
..
..
Erased 65536 bytes at address 0x003e0000 (98.44%)
Erased 65536 bytes at address 0x003f0000 (100.00%)
Connection closed.

Back to Top


Build and Write at91bootstrap to QSPI Flash Memory

In this section, you will configure and build the at91bootstrap from source code to boot from QSPI Flash memory.

Change the directory to your working directory containing the at91bootstrap.

$ cd at91bootstrap

Back to Top


Using a default configuration file, configure at91bootstrap with u-boot for QSPI Flash memory:

$ make mrproper
$ make sama5d27_som1_ekqspi_uboot_defconfig
#
# configuration written to .config
#
#
# make dependencies written to .auto.deps
# See top of this file before playing with this auto-preprequisites!

You can view the available default configuration files in the directory: ~/project_1/at91bootstrap/board

If you would like to verify the QSPI Flash memory settings or make customization changes:

$ make menuconfig

QSPI Flash memory settings

Observe that Board Type (sama5d27_som1_ek) has been selected.

Highlight Memory selection ---> and press ENTER.

Highlight QSPI Configuration ---> and press ENTER.

Highlight QSPI IOSET Select ---> and press ENTER.

Observe that Board Type (sama5d27_som1_ek) has been selected.

Observe that QSPI IOSET (QSPI1 IOSET 2) ---> has been selected.

You may exit menu config by repeatedly typing "ESC-ESC" until it closes. Do not save any changes.

Back to Top


Build at91bootstrap:

A working cross-toolchain for Arm-based MPU target is required to build at91bootstrap.

Ensure you set the CROSS_COMPILE=<cross_compiler> and ARCH=arm environment variables.

For example, if your cross-compiler executable is arm-linux-gcc then CROSS_COMPILE=<path_to>/arm-linux-

$ make
Once the build is complete, the resulting binary files are located in the at91bootstrap/binaries/ directory:

$ cd binaries
$ ls -l
at91bootstrap.bin -> sama5d27_som1_ek-dataflashboot—3.8.12.bin
boot.bin -> sama5d27_som1_ek-dataflashboot—3.8.12.bin
sama5d27_som1_ek-dataflashboot—3.8.12.map
sama5d27_som1_ek-dataflashboot—3.8.12.bin
sama5d27_som1_ek-dataflashboot—3.8.12.elf

Back to Top


From the SAM-BA Host command line, write at91bootstrap.bin to QSPI Flash memory starting at location 0x0 using the writeboot command:

$ sam-ba -p serial -b sama5d27-som1-ek -a qspiflash -c writeboot:at91bootstrap.bin
Opening serial port 'ttyACM1'
Connection opened.
Detected memory size is 4194304 bytes.
Executing command 'writeboot:at91bootstrap.bin'
Patched file length (10704) at offset 20
Added 48 bytes of padding to align to page size
Wrote 10752 bytes at address 0x00000000 (100.00%)
Connection closed.

The -a qspiflash -c writeboot command should only be used when programming a bootstrap file into an external QSPI NOR Flash memory boot partition.

The -a qspiflash -c writeboot command modifies the relevant unused Arm exception vector to store the size of the bootstrap binary as required by the ROM Boot code during the boot process.

For more information on the SAM-BA Applet qspiflash command, see the "Program External QSPI NOR Flash Memory qspiflash" section of the "SAM-BA® In-System Programmer - Applets" page.

The result is at91bootstrap.bin is stored in the QSPI Flash memory starting at address 0x0.

The result is at91bootstrap.bin is stored in the QSPI Flash memory starting at address 0x0

Back to Top


Build and Write u-boot-at91 to QSPI Flash Memory

In this section, you will configure and build the u-boot-at91 from source code to boot from QSPI Flash memory.

u-boot-at91 is maintained by Microchip Technology and hosted on GitHub.

For more information about the third-stage bootloader see the Das U-Boot "The U-Boot Documentation" page.

Change directory to your working directory containing the u-boot-at91:

$ cd u-boot-at91

Back to Top


Using a default configuration file, configure u-boot-at91 for QSPI Flash memory:

$ make sama5d27_som1_ek_qspiflash_defconfig
HOSTCC scripts/basic/fixdep
HOSTCC scripts/kconfig/conf.o
HOSTCC scripts/kconfig/zconf.tab.o
HOSTLD scripts/kconfig/conf
#
# configuration written .config
#

If you would like to verify settings for booting from QSPI Flash memory or make customization changes:

$ make menuconfig

verify settings for booting from QSPI Flash memory menu

Highlight Boot media ---> and press ENTER.

Highlight Boot media ---> and press ENTER.

Observe that [*] Support for booting from QSPI flash ---> has been selected.

You may exit menuconfig by typing "ESC-ESC" repeatedly until it closes. Do not save changes.

Back to Top


Build u-boot-at91:

$ make

Once the build is complete, the resulting binary file u-boot.bin is located in the u-boot-at91 directory.

A working cross-toolchain for Arm-based MPU target is required to build the at91bootstrap.

Ensure you set the CROSS_COMPILE=<cross_compiler> and ARCH=arm environment variables.

For example, if your cross-compiler executable is arm-linux-gcc then CROSS_COMPILE=<path_to>/arm-linux-

Back to Top


The SAM-BA Host command line, write u-boot.bin to QSPI Flash memory starting at location 0x4000 using the applet -a qspiflash -c write command:

$ sam-ba -p serial -b sama5d27-som1-ek -a qspiflash -c write:u-boot.bin:0x40000
Opening serial port 'ttyACM0'
Connection opened.
Detected memory size is 8388608 bytes.
Executing command 'write:u-boot.bin:0x40000'
Added 192 bytes of padding to align to page size
Wrote 95232 bytes at address 0x00008000 (33.97%)
Wrote 95232 bytes at address 0x0001f400 (67.95%)
Wrote 89856 bytes at address 0x00036800 (100.00%)

For more information on the SAM-BA Applet qspiflash command, see the "SAM-BA Applets - Program External QSPI NOR Flash Memory (qspiflash)" section.

The result is u-boot.bin is stored on the QSPI Flash memory starting at address 0x40000.

The result is u-boot.bin is stored on the QSPI Flash memory starting at address 0x40000

Back to Top


Create and Write u-boot.env to QSPI Flash Memory

Configuration of U-Boot is performed using environment variables. They are stored in binary format, generally, on the same boot memory (mass storage) that U-Boot is stored.

In this section, you will create a U-Boot environment binary file by first creating a text file. The text file is then converted to binary format using a utility program mkenvimage. Finally, you will write the file to QSPI Flash memory using the SAM-BA In-System Programmer.

u-boot-at91 is maintained by Microchip Technology and hosted on GitHub.

For more information about the third-stage bootloader see the "Das U-Boot – The Universal Boot Loader" page.

Using your favorite text editor, create a text file with the following information. Name the text file u-boot-env.txt.

More information on U-Boot Environment Variables can be found in the U-Boot Documentation "Environment Variablespage.

bootargs=console=ttyS0,115200 root=/dev/mmcblk0p2 rw rootwait rootfstype=ext4
bootcmd=ext4load mmc 0:2 0x24000000 boot/sama5d27_som1_ek.itb; bootm 0x24000000#kernel_dtb
bootdelay=1
ethact=gmac0
stderr=serial
stdin=serial
stdout=serial

The bootargs line tells U-Boot:

  • The console= device and baud
  • The root= location of the root filesystem and permissions
  • The rootfstype= that the root filesystem type

The bootcmd= line tells U-Boot to load the sama5d27_som1_ek.itb file (FIT) from the EXT4 formatted partition 2 on the SD memory card (which you will prepare in future sections) to external DDR2 SDRAM starting at address 0x24000000.

the BOOT partition (partition 1) is empty

Notice that the BOOT partition (partition 1) is empty in this example. It will not be used as U-Boot will be loaded from QSPI Flash memory. It is left here as you may create the SD memory card image using balenaEtcher (explained below) which will create two partitions as shown in the figure above.

atsama5d27 programmers model

Back to Top


Convert the text file u-boot-env.txt into a binary file u-boot-env.bin using the utility program mkenvimage.

$ mkenvimage -s 0x20000 -o u-boot-env.bin u-boot-env.tx

Note:

The mkenvimage utility program is part of the U-Boot software package. See directory ../u-boot-at91/tools/

Back to Top


From the SAM-BA Host command line, write u-boot-env.bin to QSPI Flash momory at location 0x140000 using the applet -a qspiflash -c write command:

$ sam-ba -p serial -b sama5d27-som1-ek -a qspiflash -c write:u-boot-env.bin:0x140000
Opening serial port 'ttyACM0'
Connection opened.
Detected memory size is 4194304 bytes.
Executing command 'write:u-boot-env.bin:0x140000'
Wrote 8192 bytes at address 0x00140000 (100.00%)
Connection closed.

The result is u-boot is stored in the QSPI Flash memory starting at address 0x140000.

The result is u-boot is stored in the QSPI Flash memory starting at address 0x140000

Back to Top


Build the Linux Kernel Image and DTB

In this section, you will configure the Linux kernel and Device Tree for the ATSAMA5D27-SOM1-EK1 evaluation kit using default configuration files and build the files. In the next section you use these files to create a FIT image to write to the SD memory card.

linux-at91 is maintained by Microchip Technology and hosted on GitHub.

Change directory to your working directory containing the linux-at91:

$ cd linux-at91

Back to Top


Using default configuration files, configure the Linux kernel:

$ make ARCH=arm sama5_defconfig
HOSTCC scripts/basic/fixdep
HOSTCC scripts/kconfig/conf.o
SHIPPED scripts/kconfig/zconf.tab.c
SHIPPED scripts/kconfig/zconf.lex.c
SHIPPED scripts/kconfig/zconf.hash.c
HOSTCC scripts/kconfig/zconf.tab.o
HOSTLD scripts/kconfig/conf
#
# configuration written to .config
#

If you would like to verify settings or make customization changes:

$ make menuconfig

If you would like to verify settings or make customization changes:

Back to Top


Build linux-at91:

$ make ARCH=arm
Once the build is complete, the resulting binary files are created:

/arch/arm/boot/zImage
/arch/arm/boot/dts/at91-sama5d27_som1_ek.dtb

A working cross-toolchain for ARM-based MPU target is required to build the at91bootstrap.

Ensure you set the CROSS_COMPILE=<cross_compiler> and ARCH=arm environment variables.

For example, if your cross-compiler executable is arm-linux-gcc then CROSS_COMPILE=<path_to>/arm-linux-

Back to Top


Create FIT Image

In this section, you will create a Flattened Image Tree (FIT) image. The FIT image allows for more flexibility in handling images of various types and enhances the integrity protection of images with stronger checksums. An Image Source (*.its) file and a set of data files are used as an input. An Image Tree Blob (*.itb) file is generated as the output.

Git clone the Device Tree Overlay files from the Linux4SAM repository:

$ git clone git://github.com/linux4sam/dt-overlay-at91.git

Back to Top


Change directory to your working directory containing the dt-overlay-at91:

$ cd dt-overlay-at91

Back to Top


Open Makefile with your favorite editor and verify the KERNEL_DIR is pointing to your linux-at91 directory:

CC?=$(CROSS_COMPILE)gcc
DTC_OPTIONS?=-@
DTC_OPTIONS += -Wno-unit_address_vs_reg -Wno-graph_child_address -Wno_pwms_property
KERNEL_DIR?=../linux-at91 <--- Ensure pointing to your linux-at91 directory
KERNEL_BUILD_DIR?=$(KERNEL_DIR)
DTC?=$(KERNEL_BUILD_DIR)/scripts/dtc/dtc

Or you can set the KERNEL_DIR environment variable:

$ export KERNEL_DIR=path
$ echo $KERNEL_DIR

Back to Top


Build the *.itb image file:

$ make sama5d27_som1_ek.itb

Once the build is complete, the resulting image file sama5d27_som1_ek.itb is located in the dt-overlay-at91 directory. Please remember this because later you will be copying the sama5d27_som1_ek.itb image to the SD memory card.

In the next section, you will prepare an SD memory card.

Back to Top


Prepare a ROOTFS for SD Memory Card

In this section, you will download a demonstration image from the Linux4SAM website for the purpose of using the ROOTFS on the image. You will Flash the image to an SD memory card and delete the files in the BOOT partition. Finally, you will copy the FIT file that you created in the previous section to the ROOTFS/boot directory.

Download ATSAMA5D27-SOM1-EK1 demonstration image from the Linux4SAM website.

The latest demonstration images for the ATSAMA5D27-SOM1-EK1 are available on the Linux4SAM.org website.

There you will find three sets of images created from one of three Build Systems:

  • The Yocto Project
  • Buildroot
  • OpenWRT

Any one of the images can be used for this step. If you are undecided, choose the buildroot_sama5d27_som1_ek-headless file.

Back to Top


Flash the demo image to an SD memory card.

Back to Top


Insert the SD memory card into a Linux desktop computer.

Back to Top


Delete the files in the BOOT (FAT32) partition.

Delete the files in the BOOT (FAT32) partition.

Note:

Notice that the BOOT partition (partition 1) is empty in this example. It will not be used as U-Boot will be loaded from QSPI Flash memory.

Back to Top


Write ITB to SD Memory Card

Copy the sama5d27_som1_ek.itb file to the ROOTFS/boot directory.

$ cd dt-overlay-at91
$ cp sama5d27_som1_ek.itb /media/<username>/ROOTFS/boot

atsama5d27-som1 programmers model

Back to Top


Boot from QSPI Flash Memory

Connect a Host PC to J10 using a Micro-B USB cable.

Back to Top


Start a terminal emulation program and configure it to the USB COM port connected to J10.

These two steps set up Console Serial Communications with the target device.

Back to Top


Insert the SD memory card into J12.

Back to Top


Press the Reset button PB1.

The boot sequence will display in the terminal window. At the end the command line will prompt the user to log in.

Proper operation of the image will be indicated by LED D5 flashing blue which indicates operation of the Linux operating system image.

Proper operation of the on-board J-Link Debugger will be indicated by LED D4 flashing green which indicates operation of the J-Link Debugger.

Congratulations! You have created an embedded Linux system that boots from QSPI Flash memory.

Back to Top


Summary

In this topic, you configured and built the second-stage bootloader at91bootstap and the third-stage bootloader u-boot-at91 from source code. You configured the boot process on the ATSAMA5D27-SOM1-EK1 evaluation kit to boot from QSPI Flash memory. You wrote the second- and third-stage bootloaders into QSPI Flash Memory. You also prepared an SD memory card with FIT (containing the Linux kernel and Device Tree) and ROOTFS. The result is an embedded Linux system that boots from QSPI Flash memory.
 

Back to Top