adding a simple cpu id program that detects between pi1/zero, pi2 and pi3.

This commit is contained in:
root
2017-02-08 20:38:05 -05:00
parent 9a33349952
commit dfda4913db
5 changed files with 257 additions and 0 deletions

31
boards/cpuid/Makefile Normal file
View File

@@ -0,0 +1,31 @@
ARMGNU ?= arm-none-eabi
COPS = -Wall -O2 -nostdlib -nostartfiles -ffreestanding
all : kernel.img
clean :
rm -f *.o
rm -f *.bin
rm -f *.hex
rm -f *.elf
rm -f *.list
rm -f *.img
rm -f *.bc
rm -f *.clang.s
vectors.o : vectors.s
$(ARMGNU)-as vectors.s -o vectors.o
cpuid.o : cpuid.c
$(ARMGNU)-gcc $(COPS) -c cpuid.c -o cpuid.o
kernel.img : loader vectors.o cpuid.o
$(ARMGNU)-ld vectors.o cpuid.o -T loader -o cpuid.elf
$(ARMGNU)-objdump -D cpuid.elf > cpuid.list
$(ARMGNU)-objcopy cpuid.elf -O ihex cpuid.hex
$(ARMGNU)-objcopy cpuid.elf -O binary kernel.img

18
boards/cpuid/README Normal file
View File

@@ -0,0 +1,18 @@
See the top level README for information on where to find documentation
for the raspberry pi and the ARM processor inside. Also find information
on how to load and run these programs.
It was asked how to detect which board you are on, specifically how to
know which peripheral base, they used a different cpu core on each
the pi1/zero armv6 based, pi2 armv7 based and pi3 armv8 based. You
can just look at one of the cpuid registers.
This is a simple program, that can be run on any of the boards (so
far) and it will detect what base address it needs to use and then
print out some info.
Now that the bootloader (bootcode.bin + start.elf) support various
file names (kernel.img, kernel7.img, etc) and will grab the lesser
name if it cant find the one most relevant to that board, we can make
this just kernel.img and it works on all of them.

145
boards/cpuid/cpuid.c Normal file
View File

@@ -0,0 +1,145 @@
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
// 2 outer corner
// 4
// 6
// 8 TX out
// 10 RX in
extern void PUT32 ( unsigned int, unsigned int );
extern unsigned int GET32 ( unsigned int );
extern unsigned int GETPC ( void );
extern unsigned int GETCPUID ( void );
extern void dummy ( unsigned int );
static unsigned int PBASE;
#define GPFSEL1 (PBASE+0x00200004)
#define GPSET0 (PBASE+0x0020001C)
#define GPCLR0 (PBASE+0x00200028)
#define AUX_ENABLES (PBASE+0x00215004)
#define AUX_MU_IO_REG (PBASE+0x00215040)
#define AUX_MU_IER_REG (PBASE+0x00215044)
#define AUX_MU_IIR_REG (PBASE+0x00215048)
#define AUX_MU_LCR_REG (PBASE+0x0021504C)
#define AUX_MU_MCR_REG (PBASE+0x00215050)
#define AUX_MU_LSR_REG (PBASE+0x00215054)
#define AUX_MU_MSR_REG (PBASE+0x00215058)
#define AUX_MU_SCRATCH (PBASE+0x0021505C)
#define AUX_MU_CNTL_REG (PBASE+0x00215060)
#define AUX_MU_STAT_REG (PBASE+0x00215064)
#define AUX_MU_BAUD_REG (PBASE+0x00215068)
//GPIO14 TXD0 and TXD1
//GPIO15 RXD0 and RXD1
//------------------------------------------------------------------------
static void uart_send ( unsigned int c )
{
while(1)
{
if(GET32(AUX_MU_LSR_REG)&0x20) break;
}
PUT32(AUX_MU_IO_REG,c);
}
//------------------------------------------------------------------------
static void hexstrings ( unsigned int d )
{
//unsigned int ra;
unsigned int rb;
unsigned int rc;
rb=32;
while(1)
{
rb-=4;
rc=(d>>rb)&0xF;
if(rc>9) rc+=0x37; else rc+=0x30;
uart_send(rc);
if(rb==0) break;
}
uart_send(0x20);
}
//------------------------------------------------------------------------
static void hexstring ( unsigned int d )
{
hexstrings(d);
uart_send(0x0D);
uart_send(0x0A);
}
//------------------------------------------------------------------------
static void uart_init ( void )
{
unsigned int ra;
PUT32(AUX_ENABLES,1);
PUT32(AUX_MU_IER_REG,0);
PUT32(AUX_MU_CNTL_REG,0);
PUT32(AUX_MU_LCR_REG,3);
PUT32(AUX_MU_MCR_REG,0);
PUT32(AUX_MU_IER_REG,0);
PUT32(AUX_MU_IIR_REG,0xC6);
PUT32(AUX_MU_BAUD_REG,270);
ra=GET32(GPFSEL1);
ra&=~(7<<12); //gpio14
ra|=2<<12; //alt5
ra&=~(7<<15); //gpio15
ra|=2<<15; //alt5
PUT32(GPFSEL1,ra);
PUT32(AUX_MU_CNTL_REG,3);
}
//------------------------------------------------------------------------
void send_string ( const unsigned char *s )
{
while(*s)
{
uart_send(*s);
s++;
}
}
//------------------------------------------------------------------------
static const unsigned char pi1[]="Raspberry Pi 1/zero\r\n";
static const unsigned char pi2[]="Raspberry Pi 2\r\n";
static const unsigned char pi3[]="Raspberry Pi 3\r\n";
//------------------------------------------------------------------------
int notmain ( void )
{
unsigned int id;
id=GETCPUID();
if((id&0xFFFFFFFF)==0x410FB767) PBASE=0x20000000;
else PBASE=0x3F000000;
PBASE=0x20000000;
uart_init();
hexstring(0x12345678);
hexstring(GETPC());
hexstring(GETCPUID());
switch(id&0xFFFFFFFF)
{
case 0x410FB767: { send_string(pi1); break; }
case 0x410FC075: { send_string(pi2); break; }
case 0x410FD034: { send_string(pi3); break; }
}
hexstring(0x11223344);
return(0);
}
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
//PI3 0x410FD034
//PI2 0x410FC075
//PI1 0x410FB767
//-------------------------------------------------------------------------
//
// Copyright (c) 2014 David Welch dwelch@dwelch.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//-------------------------------------------------------------------------

14
boards/cpuid/loader Normal file
View File

@@ -0,0 +1,14 @@
MEMORY
{
ram : ORIGIN = 0x8000, LENGTH = 0x1000
}
SECTIONS
{
.text : { *(.text*) } > ram
.rodata : { *(.rodata*) } > ram
.data : { *(.data*) } > ram
.bss : { *(.bss*) } > ram
}

49
boards/cpuid/vectors.s Normal file
View File

@@ -0,0 +1,49 @@
;@-------------------------------------------------------------------------
;@-------------------------------------------------------------------------
.globl _start
_start:
mov sp,#0x8000
bl notmain
hang: b hang
.globl PUT32
PUT32:
str r1,[r0]
bx lr
.globl GET32
GET32:
ldr r0,[r0]
bx lr
.globl GETPC
GETPC:
mov r0,lr
bx lr
.globl dummy
dummy:
bx lr
.globl GETCPUID
GETCPUID:
mrc p15,0,r0,c0,c0,0
bx lr
;@-------------------------------------------------------------------------
;@-------------------------------------------------------------------------
;@-------------------------------------------------------------------------
;@
;@ Copyright (c) 2012 David Welch dwelch@dwelch.com
;@
;@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
;@
;@ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
;@
;@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
;@
;@-------------------------------------------------------------------------