# Run a full working Buildroot Linux using just a 128MB QuadSPI 

<abstract>
This article illustrates how to save AT91Bootstrap, the device tree, the Linux Kernel and the Buildroot rootfs filesystem
fully on QuadSPI with no use of MicroSD at all.
</abstract>

<img src="quadspi.jpg">

This example has been tested on a 128MB QuadSPI using a RoadRunner SOM model Q128

## at91bootstrap

Run the at91bootstrap __make menuconfig__ and set this items:
	
	Memory selection  ---> 
		Flash Memory Technology (Dataflash)  --->
		SPI Interface Select (Quad SPI Interface)  ---> 
		QSPI Configuration  ---> 
			QSPI Bus Select (QSPI Bus 0)  --->
			QSPI IOSET Select  --->
				QSPI0 IOSET (QSPI0 IOSET 3)  --->
			[*] Quad SPI NOR flash memory is >16MiB (>128Mib)
			[ ] eXecute In Place

	Kernel Image Storage Setup  --->
		(0x0000C000) Flash Offset for Linux Kernel Image
			Flattened Device Tree  --->
				[*] Flattened Device Tree Support
				(0x00003000) The Offset of Flash Device Tree Blob

Compile ad copy the binary file on SAM-BA the directory:

	make CROSS_COMPILE=arm-linux-gnueabihf-
	cp binaries/sama5d2_roadrunner-dataflashboot-linux-image-dt-3.9.1.bin ../sam-ba_3.3.1/at91bootstrap.bin

To know how to install and use SAM-BA read:

* @article='SAM-BA'

To know how to compile at91bootstrap read:

* @article='at91bootstrap'

## Linux Kernel

Run the Linux Kernel __make menuconfig__:

	make ARCH=arm menuconfig

and set this items:

	Device Drivers  ---> 
		<*> Memory Technology Device (MTD) support  --->
			<*>   SPI-NOR device support  ---> 
				<*>   Atmel Quad SPI Controller

	File systems  --->
		<*> F2FS filesystem support

Launch the Kernel compilation and copy the zImage on the SAM-BA directory:

	make -j8 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage
	cp arch/arm/boot/zImage ../sam-ba_3.3.1/

To know how to compile the Linux Kernel read:

* @article='roadrunner_compile_kernel_4_19'

## Device tree

Save this definition inside the device tree file:


Definition to insert inside the __apb__ section:

	/* https://github.com/linux4sam/linux-at91/blob/linux-4.9-at91/arch/arm/boot/dts/at91-sama5d2_xplained_common.dtsi */

	qspi0: spi@f0020000 {
		pinctrl-names = "default";
		pinctrl-0 = <&pinctrl_qspi0_default>;

		/* conflict with sdmmc1 */
		status = "okay"; 

		flash@0 {
			#address-cells = <1>;
			#size-cells = <1>;
			compatible = "jedec,spi-nor";
			reg = <0>;
			spi-max-frequency = <83000000>;
			m25p,fast-read;

			at91bootstrap@00000000 {
				label = "at91bootstrap";
				reg = <0x00000000 0x00010000>;
			};

			dtb@00003000 {
				label = "device tree";
				reg = <0x00003000 0x00009000>;
			};

			kernel@00080000 {
				label = "kernel";
				reg = <0x0000C000 0x00500000>;
			};

			rootfs@00400000 {
				label = "rootfs";
				reg = <0x0050C000 0x00000000>;
			};
		};
	};

Definition to insert inside the __pinctrl@fc038000__ section:

	pinctrl_qspi0_default: qspi0_default {
		sck_cs {
			pinmux = <PIN_PA22__QSPI0_SCK>,
				 <PIN_PA23__QSPI0_CS>;
			bias-disable;
		};

		data {
			pinmux = <PIN_PA24__QSPI0_IO0>,
				 <PIN_PA25__QSPI0_IO1>,
				 <PIN_PA26__QSPI0_IO2>,
				 <PIN_PA27__QSPI0_IO3>;
			bias-pull-up;
		};
	};

Compile the device tree and copy it on SAM-BA directory:

	make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- acme-roadrunner-qspi.dtb
	cp arch/arm/boot/dts/acme-roadrunner-qspi.dtb ../sam-ba_3.3.1/acme-roadrunner-qspi.dtb

## Buildroot linux

Create a rootfs image and move it on the SAM-BA directory:

	make menuconfig
	make
	cp output/images/rootfs.f2fs ../sam-ba_3.3.1/

## SAM-BA commands

Move inside the SAM-BA directory and save the binaries on the QuadSPI:

    sudo ./sam-ba -p usb -b sama5d2-xplained -a qspiflash -c erase
    sudo ./sam-ba -p usb -b sama5d2-xplained -a qspiflash -c writeboot:at91bootstrap.bin
	sudo ./sam-ba -p usb -b sama5d2-xplained -a qspiflash -c write:acme-roadrunner-qspi.dtb:0x3000
	sudo ./sam-ba -p usb -b sama5d2-xplained -a qspiflash -c write:zImage:0xC000
	sudo ./sam-ba -p usb -b sama5d2-xplained -a qspiflash -c write:rootfs.f2fs:0x50C000


If you need to erase a portion of QuadSPI the update some binaries use these commands:

Erase at91bootstrap area:

	sudo ./sam-ba -p usb -b sama5d2-xplained -a qspiflash -c erase:0x0:0x3000

Erase dtb area:

	sudo ./sam-ba -p usb -b sama5d2-xplained -a qspiflash -c erase:0x3000:0x9000

Erase zImage area:

	sudo ./sam-ba -p usb -b sama5d2-xplained -a qspiflash -c erase:0xC000:0x500000

## Links

* <https://en.wikipedia.org/wiki/F2FS>
* <https://github.com/linux4sam/linux-at91/blob/linux-4.9-at91/arch/arm/boot/dts/at91-sama5d2_xplained_common.dtsi>

@include='adv_roadrunner'
