From ec25fec145ab1a0c33e8641af967ed3f8341acfa Mon Sep 17 00:00:00 2001 From: David Given Date: Sat, 25 May 2013 00:35:29 +0100 Subject: [PATCH] Build binaries that will run bare metal. Add enough syscall library to init the uart and write text. --HG-- branch : dtrg-videocore rename : plat/rpi/libsys/libsys.h => plat/rpi/libsys/libsysasm.h rename : plat/rpi/libsys/_sys_rawread.s => plat/rpi/libsys/phys_to_user.s rename : plat/rpi/libsys/_sys_rawread.s => plat/rpi/libsys/uart.s rename : plat/rpi/libsys/_sys_rawread.s => plat/rpi/libsys/user_to_phys.s --- plat/rpi/boot.s | 12 ++- plat/rpi/build.mk | 6 +- plat/rpi/include/pi.h | 21 +++++ plat/rpi/libsys/libsysasm.h | 20 +++++ plat/rpi/libsys/phys_to_user.s | 20 +++++ plat/rpi/libsys/uart.s | 135 +++++++++++++++++++++++++++++++++ plat/rpi/libsys/user_to_phys.s | 20 +++++ 7 files changed, 229 insertions(+), 5 deletions(-) create mode 100644 plat/rpi/include/pi.h create mode 100644 plat/rpi/libsys/libsysasm.h create mode 100644 plat/rpi/libsys/phys_to_user.s create mode 100644 plat/rpi/libsys/uart.s create mode 100644 plat/rpi/libsys/user_to_phys.s diff --git a/plat/rpi/boot.s b/plat/rpi/boot.s index 43811a6ad..523cf40dc 100644 --- a/plat/rpi/boot.s +++ b/plat/rpi/boot.s @@ -19,14 +19,17 @@ #define STACKSIZE 16*1024 ! MAIN ENTRY POINT -! -! When running as a kernel, our parameters are passed in in r0-r5, so -! the startup sequence mustn't disturb these. begtext: + ! This empty space is required by the boot loader. + + b start + .space 508 ! 512 minus space needed for branch instruction + ! Wipe the bss. This must happen absolutely first, because we need ! to store the old system registers into it. - + +start: lea r6, begbss lea r7, endbss mov r8, #0 @@ -79,6 +82,7 @@ _1: .define __exit __exit: + mov r0, sr ld fp, .returnfp ld sp, .returnsp ld lr, .returnlr diff --git a/plat/rpi/build.mk b/plat/rpi/build.mk index 9716eb058..dffe59d9b 100644 --- a/plat/rpi/build.mk +++ b/plat/rpi/build.mk @@ -13,10 +13,15 @@ D := plat/rpi/ platform-headers := \ unistd.h \ + pi.h \ ack/config.h platform-libsys := \ _hol0.s \ + phys_to_user.s \ + user_to_phys.s \ + uart.s \ + write.c \ ifeq (x,y) errno.s \ @@ -26,7 +31,6 @@ ifeq (x,y) creat.c \ close.c \ read.c \ - write.c \ brk.c \ getpid.c \ kill.c \ diff --git a/plat/rpi/include/pi.h b/plat/rpi/include/pi.h new file mode 100644 index 000000000..a69cdd8d9 --- /dev/null +++ b/plat/rpi/include/pi.h @@ -0,0 +1,21 @@ +/* + * Raspberry Pi support library for the ACK + * © 2013 David Given + * This file is redistributable under the terms of the 3-clause BSD license. + * See the file 'Copying' in the root of the distribution for the full text. + */ + +#ifndef PI_H +#define PI_H + +/* Initialise the mini UART (only do this if running on bare metal! */ +extern void init_uart(void); + +/* Converts a pointer from a physical address to a user address. */ +extern void* phys_to_user(void* ptr); + +/* Converts a pointer from a user address to a physical address. */ +extern void* user_to_phys(void* ptr); + +#endif + diff --git a/plat/rpi/libsys/libsysasm.h b/plat/rpi/libsys/libsysasm.h new file mode 100644 index 000000000..16dbbcfba --- /dev/null +++ b/plat/rpi/libsys/libsysasm.h @@ -0,0 +1,20 @@ +/* + * Raspberry Pi support library for the ACK + * © 2013 David Given + * This file is redistributable under the terms of the 3-clause BSD license. + * See the file 'Copying' in the root of the distribution for the full text. + */ + +#ifndef LIBSYSASM_H +#define LIBSYSASM_H + +! Declare segments (the order is important). + +.sect .text +.sect .rom +.sect .data +.sect .bss + +#define gp r15 + +#endif diff --git a/plat/rpi/libsys/phys_to_user.s b/plat/rpi/libsys/phys_to_user.s new file mode 100644 index 000000000..649b19b5a --- /dev/null +++ b/plat/rpi/libsys/phys_to_user.s @@ -0,0 +1,20 @@ +# +/* + * Raspberry Pi support library for the ACK + * © 2013 David Given + * This file is redistributable under the terms of the 3-clause BSD license. + * See the file 'Copying' in the root of the distribution for the full text. + */ + +#include "libsysasm.h" + +.sect .text + +! Transforms a physical address into a user address. + +.define _phys_to_user +_phys_to_user: + ld r0, 0 (sp) + sub r0, gp + b lr + diff --git a/plat/rpi/libsys/uart.s b/plat/rpi/libsys/uart.s new file mode 100644 index 000000000..b12f2efd2 --- /dev/null +++ b/plat/rpi/libsys/uart.s @@ -0,0 +1,135 @@ +# +/* + * Raspberry Pi support library for the ACK + * © 2013 David Given + * This file is redistributable under the terms of the 3-clause BSD license. + * See the file 'Copying' in the root of the distribution for the full text. + */ + +#include "libsysasm.h" + +.sect .text + +! Because of the low system clock rate, this baud rate might be inaccurate +! So be careful with your serial/terminal, some adjustment may be necessary. +TARGET_BAUD_RATE = 115200 + +! System clock is running directly off the 19.2MHz crystal at initial reset +SYSTEM_CLOCK = 19200000 + +GPFSEL1 = 0x7e200004 +GPSET0 = 0x7e20001C +GPCLR0 = 0x7e200028 +GPPUD = 0x7e200094 +GPPUDCLK0 = 0x7e200098 + +AUX_ENABLES = 0x7e215004 +AUX_MU_IO_REG = 0x7e215040 +AUX_MU_IER_REG = 0x7e215044 +AUX_MU_IIR_REG = 0x7e215048 +AUX_MU_LCR_REG = 0x7e21504C +AUX_MU_MCR_REG = 0x7e215050 +AUX_MU_LSR_REG = 0x7e215054 +AUX_MU_MSR_REG = 0x7e215058 +AUX_MU_SCRATCH = 0x7e21505C +AUX_MU_CNTL_REG = 0x7e215060 +AUX_MU_STAT_REG = 0x7e215064 +AUX_MU_BAUD_REG = 0x7e215068 + +! Sets up the mini UART for use as a console. + +.define _init_uart +_init_uart: + ! Configure TX and RX GPIO pins for Mini Uart function. + mov r1, #GPFSEL1 + ld r0, (r1) + and r0, #~[7<<12] + or r0, #2<<12 + and r0, #~[7<<15] + or r0, #2<<15 + st r0, (r1) + + mov r1, #GPPUD + mov r0, #0 + st r0, (r1) + +delay1: + add r0, #1 + cmp r0, #150 + b.ne delay1 + + mov r1, #GPPUDCLK0 + mov r0, #[1<<14]|[1<<15] + st r0, (r1) + + mov r0, #0 +delay2: + add r0, #1 + cmp r0, #150 + b.ne delay2 + + mov r1, #GPPUDCLK0 + mov r0, #0 + st r0, (r1) + + ! Set up serial port + mov r1, #AUX_ENABLES + mov r0, #1 + st r0, (r1) + + mov r1, #AUX_MU_IER_REG + mov r0, #0 + st r0, (r1) + + mov r1, #AUX_MU_CNTL_REG + mov r0, #0 + st r0, (r1) + + mov r1, #AUX_MU_LCR_REG + mov r0, #3 + st r0, (r1) + + mov r1, #AUX_MU_MCR_REG + mov r0, #0 + st r0, (r1) + + mov r1, #AUX_MU_IER_REG + mov r0, #0 + st r0, (r1) + + mov r1, #AUX_MU_IIR_REG + mov r0, #0xC6 + st r0, (r1) + + mov r1, #AUX_MU_BAUD_REG + mov r0, #[[SYSTEM_CLOCK/[TARGET_BAUD_RATE*8]]-1] + st r0, (r1) + + mov r1, #AUX_MU_LCR_REG + mov r0, #3 + st r0, (r1) + + mov r1, #AUX_MU_CNTL_REG + mov r0, #3 + st r0, (r1) + + b lr + +! Send a single byte. + +.define __sys_rawwrite +__sys_rawwrite: + ld r0, (sp) + mov r1, #AUX_MU_LSR_REG + ! loop until space available in Tx buffer +sendwait: + ld r2, (r1) + and r2, #0x20 + cmp r2, #0x20 + b.ne sendwait + + mov r1, #AUX_MU_IO_REG + stb r0, (r1) + + b lr + diff --git a/plat/rpi/libsys/user_to_phys.s b/plat/rpi/libsys/user_to_phys.s new file mode 100644 index 000000000..7b988fdd6 --- /dev/null +++ b/plat/rpi/libsys/user_to_phys.s @@ -0,0 +1,20 @@ +# +/* + * Raspberry Pi support library for the ACK + * © 2013 David Given + * This file is redistributable under the terms of the 3-clause BSD license. + * See the file 'Copying' in the root of the distribution for the full text. + */ + +#include "libsysasm.h" + +.sect .text + +! Transforms a user address into a physical address. + +.define _user_to_phys +_user_to_phys: + ld r0, 0 (sp) + add r0, gp + b lr +