#Compiling Linux Kernel 3.10 for Acqua A5 SoM

<img src="./tuxcase_small.jpg">

This article illustrates how to generate a bootable <b>Linux Kernel</b> image for 
the [Acqua A5 SoM](/acqua) based on Atmel SAMA5D53x MPU.

###Hardware requirements:

* An [Acqua A5 SoM](/acqua) installed on a Berta evaluation board:
  * [Berta A5 basic](/BERTA-A5-BASIC)
  * [Berta A5 extended](/BERTA-A5-EXT)
  * [Berta A5 lcd](/BERTA-A5-LCD)
* a PC with Linux <b>Ubuntu 13.10</b> or higher
* a [formatted microSD card](/microsd_format)
* a [Debug Port Interface](/DPI)

###Software requirements

* @article='arm9_toolchain'

##Step-by-step Kernel cross-compilation procedure:

Download from the Atmel GitHub repository the patched Linux Kernel sources for SAMA5D3x:

* [linux-3.10-at91.zip](https://github.com/linux4sam/linux-at91/archive/linux-3.10-at91.zip)

Extract the Kernel sources from the compressed file by typing:

<pre class="minicom">
~$ <b>unzip linux-at91-linux-3.10-at91.zip</b>
~$ <b>mv linux-at91-linux-3.10-at91 linux-3.10-acqua</b>
...
</pre>

Move inside the new folder:

<pre class="minicom">
~$ <b>cd linux-3.10-acqua</b>
~/linux-3.10-acqua$
</pre>

At this point I suggest to create a your own personal branch
on Linux Kernel sources to keep a trace of any changes from the
Kernel mainline using [git](/git). 


###Apply the patches for Acqua A5 module

Download the patch file:

* [acme.patch](./acme.patch)

and apply it:

<pre class="minicom">
~/linux-3.10-acqua$ <b>patch -p1 < acme.patch</b>
</pre>

Download the device tree file for the LCD you are using:

* [Display 4.3 inch - acme-acqua_lcd_43.dts](./acme-acqua_lcd_43.dts)
* [Display 5.0 inch - acme-acqua_lcd_50.dts](./acme-acqua_lcd_50.dts)
* [Display 7.0 inch - acme-acqua_lcd_70.dts](./acme-acqua_lcd_70.dts)
* [No display - acme-acqua_no_lcd.dts](./acme-acqua_no_lcd.dts)

and save it in __arch/arm/boot/dts__.

Download the default Kernel config and save it in __arch/arm/configs__.

* [acme-acqua_defconfig](./acme-acqua_defconfig)

###Set the basic Kernel configuration

<pre class="minicom">
~/linux-3.10-acqua1$ <b>make ARCH=arm acme-acqua_defconfig</b>
...
#
# configuration written to .config
#
</pre>


###Customize the default Linux Kernel configuration:

If you need to customize the Kernel configuration or just taking a look
to the drivers and features activated type:

<pre class="minicom">
~/linux-3.10-acqua$ <b>make ARCH=arm menuconfig</b>
</pre>

and navigate inside the Kernel configuration using the arrow keys and 
following the help provided by the menuconfig interface.

###Compile the Device Tree Blob file

Compile the device tree file source to generate the file __.dtb__.

For the LCD 4.3 inch for example use this line:

<pre class="minicom">
~/linux-3.10-acqua$ <b>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- acme-acqua_lcd_43.dtb</b>
...
DTC     arch/arm/boot/dts/acme-acqua_lcd_43.dtb
</pre>

###Compile the Kernel image

To compile the Linux Kernel sources and generate a binary 
image file to place in your bootable microSD card type:

<pre class="minicom">
~/linux-3.10-acqua$ <b>make -j8 ARCH=arm  CROSS_COMPILE=arm-linux-gnueabi- zImage</b>
...
Kernel: arch/arm/boot/zImage is ready
</pre>

On a mid-power PC the whole source compilation requires about 5 minutes. 

###Compile the Kernel modules

The image generated contains the Linux Kernel and all the built-in 
device drivers (option <b>[*]</b> in menuconfig) compiled with it.

Al the drivers compiled as external modules (option <b>[M]</b> in menuconfig)
need to be compiled and saved in the rootfs <b>/lib</b> directory on the second 
partition of the microSD. To compile them type:

<pre class="minicom">
~/linux-3.10-acqua$ <b>make modules -j8 ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-</b>
...
~/linux-3.10-acqua$ <b>make modules_install INSTALL_MOD_PATH=./modules ARCH=arm</b>
...
</pre>

#Copy the boot files into the first microSD partition

##Method 1 - Direct write on microSD

<blockquote markdown="1">
Insert a [formatted microSD](/microsd_format) in your Linux PC and copy 
on it the files directly:

Write the Linux Kernel image and Device tree blog files in the first microSD
partition:

<pre class="minicom">
~/linux-3.10-acqua$ <b>cp arch/arm/boot/dts/acme-acqua_lcd_43.dtb /media/$USER/KERNEL/at91-sama5d3_acqua.dtb</b>
~/linux-3.10-acqua$ <b>cp arch/arm/boot/zImage /media/$USER/KERNEL</b>
</pre>

<span class="smallnotes">
If you are using an Ubuntu release older than 13.10 remove **$USER** in the 
path
</span>

Write the Kernel modules in the rootfs (second microSD partition):

<pre class="minicom">
~/linux-3.10-acqua$ <b>sudo rsync -avc modules/lib/. /media/$USER/rootfs/lib/.</b>
</pre>
</blockquote>

##Method 2 - Remote writing via ssh 

<blockquote markdown="1">
Use this method if you have a working board accessible via LAN

Write the Linux Kernel image and Device tree blog files in the first microSD
partition:

<pre class="minicom">
~/linux-3.10-acqua$ <b>scp arch/arm/boot/dts/acme-acqua_lcd_43.dtb root@acqua-ip-address:/boot/at91-sama5d3_acqua.dtb</b>
~/linux-3.10-acqua$ <b>scp arch/arm/boot/zImage root@acqua-ip-address:/boot</b>
</pre>

Write the Kernel modules in the rootfs (second microSD partition):

<pre class="minicom">
~/linux-3.10-acqua$ <b>rsync -avc modules/lib/. root@acqua-ip-address:/lib/.</b>
</pre>
</blockquote>

<span class="smallnotes">
If this command doesn't work verify if rsync in installed on your board. Use <b>apt-get install rsync</b>.
</span>


##After the first boot

At the first access to the board command line update the module dependencies by typing this command:

<pre class="minicom">
~# <b>depmod -a</b>
</pre>

#Create the rootfs contents

* @article='debian_wheezy' 

# Related links

* [Acqua product description](/acqua)
* [Linux4Sam](https://github.com/linux4sam/linux-at91)
* [linux-at91 on GitHub](http://www.at91.com/linux4sam/bin/view/Linux4SAM/LinuxKernel)
* [Linux changes](http://kernelnewbies.org/LinuxChanges)
* [Linux Kernel mailing list archives](https://lkml.org/)
* [Kernel documentation](http://lxr.linux.no/linux+v3.13.6/Documentation)

