Embedded Open-Source Development

Overview Going Embedded

• Embedded is not Desktop! – To achieve the goals of embedded systems, you can give up many parts of a multipurpose desktop or server-based system • Smaller is better ≠ – Do not include everything – Embedded systems have space constraints – Embedded systems have cost constraints – Every has unique hardware • Single fixed purpose – Configuration is done by the developer and/or manufacturer

Open Source Linux - 2 Dec. 7. 2012 Open-Source Software

is always available! • Designing products with Linux and other open-source software does not mean you have to make your software open source. The use of loadable modules and LGPL libraries protects your intellectual property

Open Source Linux - 3 Dec. 7. 2012 Goal

• Enhance your experience in the customization of a System on Chip (SoC) design with an embedded – Linux – by using the Xilinx tool capabilities

Open Source Linux - 4 Dec. 7. 2012 Linux Distributions

• Workstation Distributions – – Red Hat Linux – Suse – Others • Embedded Distributions – LynuxWorks – Wind River – MontaVista – Others

Open Source Linux - 5 Dec. 7. 2012 What is Linux?

• Here, “Linux” is the kernel

Graphical Application

Application X Server (xorg) Command line tools (cp, mv, tar, …) Kernel Interface (System Call) Distribution Kernel Subsystems Driver

Open Source Linux - 6 Dec. 7. 2012 Benefits of Linux

• Memory Manager • Filesystem • Networking Support (TCP/IP Stack, ARP, ...) • Protected Memory • Peripheral Support • Application Support • Scalability (from embedded to clusters)

Open Source Linux - 7 Dec. 7. 2012 Drawbacks of Linux

• Large Resource Requirements – External memory needed • Compulsory Filesystem – A complete filesystem can be an overkill for a small application

Open Source Linux - 8 Dec. 7. 2012 Linux System Components

ToolchainToolchain HardwareHardware -binutils -gcc DTS -libc cross-compile cp cross- Linux Kernel compile -ARCH-ARCH -CROSS_COMPILE-CROSS_COMPILE cp

shellshell RootfsRootfs (cp,(cp, ls, ls, …) …) cp /lib /bin /dev /etc

Open Source Linux - 9 Dec. 7. 2012 General HW Design

Processor Non-Volatile FPGA Memory (FLASH)

Memory (RAM) Network UART …

Open Source Linux - 10 Dec. 7. 2012 Evaluation Boards

Spartan 3E Starter Kit ML 405

ML 507

Open Source Linux - 11 Dec. 7. 2012 Evaluation Boards

Zedboard

SP 605

Open Source Linux - 12 Dec. 7. 2012 MicroBlaze 8.20a Processor

• At least MicroBlaze 7.20d due to bug fixes

MemoryMemory ManagementManagement UnitUnit (MMU)(MMU) D I I IPLB D-Cache I-Cache - IPLB - DPLB -

- DPLB Cache Cache Cache Cache

ILMBILMB DLMBDLMB ALU ProgramProgram CounterCounter SpecialSpecial Shift Purpose

Purpose BUS BUS BUS BUS Purpose BUS-IF BUS-IF Barrel Shift RegistersRegisters - - - - IF IF Multiplier IF IF Multiplier Divider Branch Target FPU Cache InstructionInstruction AXIAXI DecoderDecoder Instruction Buffer Instruction Buffer RegisterRegister FileFile FSLFSL 3232 xx 32b32b

Open Source Linux - 13 Dec. 7. 2012 Virtex-4 FX PowerPC405

• 32-bit RISC CPU, PowerPC Fetch Timers & and • 700+ DMIPS at 450 MHz Decode Debug Logic • 5-stage instruction pipeline I-Cache D-Cache 16KB 16KB • Instruction and Data Caches MMU • Embedded MMU • Unit Execution Unit – Multiply / divide unit 32x32b GPR ALU, MAC – 32 x 32-bit GPR • High-bandwidth direct interface to logic using OCM™ IBM • Timers: PIT, FIT, Watchdog PowerPC™ • Built-in Debug and Trace support 405 RISC CPU

Open Source Linux - 14 Dec. 7. 2012 Virtex-5 FXT FPGA PowerPC 440 Processor

Processor Block DMADMA PowerPC 440 processor core DMADMA – Industry-standard processor SPLB0 • IBM licensed PowerPCPowerPC – More DMIPS per MHz APU APU MCIMCI Control 440440 Hardened processor interconnect Control CrossbarCrossbar MPLBMPLB – Integrated “MPMC” data movement CPMCPM engine SPLB1SPLB1 – Simpler implementation DMADMA – Reclaim up to 5000 LUTs and 5000 DCRDCR DMADMA flip-flops Enhanced APU – Double-precision floating point unit – Custom hardware acceleration – Eliminate software bottlenecks

Open Source Linux - 15 Dec. 7. 2012 SW and HW Flows Combined

C/++C/C++ Assembly Cross- Source Code VHDL or SynthesisSynthesis Cross-Compiler System Netlist Assembler Source Code Constraints BuildBuild & & Map Map Assembler Merged & Relocatable Mapped Design Memory Place & Route LinkerLinker Libraries Map Place & Route Configuration File System Bitstream ELF

SystemSystem MemoryMemory Combined Image Download Bitstream

Open Source Linux - 16 Dec. 7. 2012 ZYNQ-7000 Processing System

Open Source Linux - 17 Dec. 7. 2012 Flat device tree

• ARM / MicroBlaze / PowerPC use a configuration method known as a device tree • Device tree is used by Open and Flattened Device Tree (FDT) • A device tree is data structure representing a hierarchical tree that describes the devices in the system • Device tree source DTS is compiled into a device tree blob DTB • The device tree is parsed by the kernel such that system configuration is done at run time rather than

Open Source Linux - 18 Dec. 7. 2012 Device tree source example

/ { compatible = "xlnx,microblaze"; model = "testing"; DDR3_SDRAM: memory@40000000 { device_type = "memory"; reg = < 0x40000000 0x8000000 >; } ; chosen { bootargs = "console=ttyS0 root=/dev/ram"; linux,stdout- = "/axi@1/serial@84000000"; } ; cpus { microblaze_0: cpu@0 { clock-frequency = <100000000>; compatible = "xlnx,microblaze-8.00.b"; ... } ; } ; AXI_Lite: axi@1 { compatible = "xlnx,axi-interconnect-1.01.a", "simple-bus"; Push_Buttons_4Bit: gpio@81400000 { compatible = "xlnx,axi-gpio-1.01.a", " xlnx,xps-gpio-1.00.a "; reg = < 0x81400000 0x10000 >; xlnx,all-inputs = <0x1>; ... } ; ... } ; } ;

Open Source Linux - 19 Dec. 7. 2012 Available Xilinx Drivers

• SPI (QSPI, SPI, XPS_SPI) • Ethernet (ll_temac, xps_emaclite, PS Ethernet, axi_ethernet) • Uart (UART 16550, UART Lite) • GPIO (PS GPIO, XPS GPIO) • I2C (xps_iic, PS I2C) • Video Framebuffer (xps-tft, xylon, plb-tft-cntlr-ref, plb-dvi-cntlr-ref) • System ACE (xps-sysace, opb-sysace) • USB Host (xps-usb-host, xusbps-ehci) • HWICAP (opb-hwicap, xps-hwicap) • ..

Open Source Linux - 20 Dec. 7. 2012 Environment Components

• Binutils • Cross-Compiler • C • Linux kernel ToolchainToolchain HardwareHardware • Root filesystem (rootfs)

KernelKernel

Rootfs ShellShell Rootfs

Environment

Open Source Linux - 21 Dec. 7. 2012 GNU Binutils

 ld – the GNU linker  as – the GNU assembler • ar – Create, modify, and extract from archives • nm – List symbols from object files • Objcopy – Copy and translate object files • Objdump – Display information from object files • ranlib – Generate index to archive contents • size – List section sizes and total size • strings – List printable strings from files • strip – Discard symbols • c++filt – Filter to demangle encoded C++ symbols • c++filt – MS-DOS name for c++filt • addr2line – Convert addresses to file and line • nlmconv – Convert object code into an NLM • readelf – Display the contents of ELF format files

Open Source Linux - 22 Dec. 7. 2012 Cross-Compiler

• Runs on the HOST system and generates binaries for a TARGET system – The HOST system is usually – The TARGET systems are ARM / PowerPC / MicroBlaze processors • Big projects are sometimes hard to cross-compile – Solve the problems or – Use a native compiler on the target system

Open Source Linux - 23 Dec. 7. 2012 C Libraries

• GNU C library ( glibc ) is extensive • For smaller embedded applications – eglibc (popular choice now) – uClibc – diet libc – (very small for initial ram file- Library ~ (def ) pre- system) compiled standard source code

Open Source Linux - 24 Dec. 7. 2012 Toolchain – Build Scripts

• Building a toolchain by hand can be a real pain – Due to version dependencies

• Use scripts instead – Crosstool –

• Use prebuilt Toolchain is preferred – ARM GNU Tools (wiki.xilinx.com) – MicroBlaze GNU Tools (wiki.xilinx.com) – PowerPC GNU Tools Embedded Linux Development Kit (ELDK - http://www.denx.de)

Open Source Linux - 25 Dec. 7. 2012 Acquiring a Linux Kernel

• Linux kernel sources – Xilinx Tree ( recommended ) • git.xilinx.com: git clone git.xilinx.com/linux-xlnx.git – Secret Lab Tree (new device tree developments) • git.secretlab.ca: git clone git://git.secretlab.ca/git/linux.git – Mainline Kernel • www.kernel.org – Many more • git.kernel.org

Open Source Linux - 26 Dec. 7. 2012 Configuring the Kernel

• Define environment for building [linux-xlnx]# export CROSS_COMPILE= • [linux-xlnx]# export ARCH= • Copy the hardware device tree into the Linux kernel sources arch/ /boot/dts/ • Use predefined configurations (arch/ /configs/) if available for example, make _defconfig replace with ml403, ml507, sp605, … • Or use minimal configuration make allnoconfig • Interactively configure the kernel by using a menu make

Open Source Linux - 27 Dec. 7. 2012 Building the Kernel

• Invoke the makefile from kernel directory to build the kernel – make (builds the kernel default target) – make modules (builds kernel modules) – make V=1 (builds with verbose messages) – make simpleImage. (builds with device tree included)

Open Source Linux - 28 Dec. 7. 2012 Creating a Target RootFS

1. From scratch 1. Create directory structure in the rootfs 2. Create Linux tools (shell, …) and user applications in the rootfs 3. Copy libraries to the rootfs 4. Create device files in the rootfs 5. Create configuration files in the rootfs 2. Use a build system e.g. buildroot 3. Use a prebuilt ramdisk to begin e.g. several ramdisks can be found on wiki.xilinx.com

Open Source Linux - 29 Dec. 7. 2012 Putting it all together

ToolchainToolchain HardwareHardware -binutils -gcc DTS -libc cross-compile cp cross- Linux Kernel compile -ARCH-ARCH -CROSS_COMPILE-CROSS_COMPILE cp

ShellShell RootfsRootfs (cp,(cp, ls, ls, …) …) cp /lib /bin /dev /etc

Open Source Linux - 30 Dec. 7. 2012 Access RootFS

• Several method depending on application and stage of development – ramdisk or ramfs: e.g. early development – nfsroot: e.g. application change intense development – flash memory – harddisk – ...

Open Source Linux - 31 Dec. 7. 2012 Boot-Load Scenarios

• Commonly used boot-load scenarios – from flash devices • A first-stage boot loader (included in the FPGA bitstream) loads the real software from flash – Booting with JTAG (for development) • The FPGA bitstream (with a boot loop) is programmed with iMPACT and the software is downloaded with XMD – Booting with ZYNQ is different from booting MicroBlaze and PowerPC systems • first-stage boot loader already in architecture implemented

Open Source Linux - 32 Dec. 7. 2012 Das U-Boot – the Universal

• Sources – git clone git://www.denx.de/git/u-boot.git (mainline) – git clone git://git.xilinx.com/u-boot-xlnx.git (recommended) • Build – export ARCH= – export CROSS_COMPILE=microblaze-unknown-linux- gnu- – make distclean – make _config – make

Open Source Linux - 33 Dec. 7. 2012 Linux OS Layers

Linux Filesystem Layers of the Linux Tools, Utilities, Libraries, and User Applications operating system  Linux Filesystem (rootfs) Linux Kernel  Linux Kernel Arch/Processor-Specific Boot and Interrupt-Handling Code  Hardware

Driver Driver Driver Driver Driver SW HW UART EMAC GPIO IIC TFT

Processor Core SDRAM Flash

Open Source Linux - 34 Dec. 7. 2012 Writing Device Drivers

• Standard device files – Character devices – Block devices • Network Devices • Userspace IO drivers (when kernel driver is overkill) – limitations • Integrating your IP into a already existing subsystem makes it easier to be long-term accepted and maintained – preferable way – Linux Kernel Documentation is the first way to go

Open Source Linux - 35 Dec. 7. 2012 Linux Kernel Structure

Application Normal Program Kernel System Call Interface

Virtual filesystem Sockets management Memory Process Manager Manager Protocol Stack Filesystem Driver TCP/IP

Char/Block Driver Ethernet Driver Kernel Hardware Hardware Network MAC

Open Source Linux - 36 Dec. 7. 2012 Resources

• http://elinux.org – Embedded Linux Community Wiki • http://lwn.net – Linux Weekly Newsletter • http://devicetree.org – Device Tree Examples • http://www.kernel.org/doc/ - Linux Kernel Documentation • http://www.xilinx.com – Embedded Linux on MicroBlaze Processor Workshop • http://www.makelinux.net/ldd3/ - Linux Device Drivers • http://lxr.linux.no – Linux Kernel Cross Reference • http://www.makelinux.com/books/ulk3/ - Understanding the Linux Kernel

Open Source Linux - 37 Dec. 7. 2012