Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial port of little-kernel to vc4 #259

Draft
wants to merge 101 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
ed6bcd6
[platform][vc4] initial port of little-kernel to vc4
cleverca22 Jan 14, 2020
6177b60
[platform][vc4] implement setting the baud rate, currently hard-coded…
cleverca22 Jan 14, 2020
b87e962
[platform][vc4] fix time resolution, start genet driver, fix rpi2-tes…
cleverca22 Jan 14, 2020
6e53a65
various changes, moving repo to another pc
cleverca22 Jan 20, 2020
63983b1
fix CI and enable hw led
cleverca22 Jan 24, 2020
7688dad
most of the code needed to make an rpi3 bootcode.bin
cleverca22 Jan 29, 2020
34a6c86
various changes done on an rpi4
cleverca22 Jan 29, 2020
8057162
add the basis for nix CI
cleverca22 Jan 29, 2020
e6aa53e
add the rpi ddr2 controller
cleverca22 Jan 29, 2020
964020e
move the vector table
cleverca22 Jan 30, 2020
265ddb4
bootcode.bin bootloader start
cleverca22 Feb 12, 2020
a8a1796
start the rpi3 start.elf code
cleverca22 Feb 12, 2020
bba0459
fix CI errors on hydra
cleverca22 Feb 12, 2020
bbd44bf
many improvements
cleverca22 Feb 16, 2020
2a56c6f
clean up unused things in linker script
cleverca22 Feb 22, 2020
b5f15ad
Re-implement OTP access
ptesarik Feb 22, 2020
a92f697
otp_write command
ptesarik Feb 22, 2020
37a056c
Rename OTP_STAT_PROG_OK to OTP_STAT_PROG_ENABLE
ptesarik Feb 22, 2020
e07d1f8
OTP write support for BCM283x
ptesarik Feb 22, 2020
b310295
commit the current arm and pll stuff
cleverca22 Feb 25, 2020
6ddcdc6
Merge pull request #1 from ptesarik/vc4
cleverca22 Feb 25, 2020
7b8171a
Add missing PLL hw register definitions
ptesarik Feb 27, 2020
84aba01
Define a PLL enum and put all PLLs into a single table
ptesarik Feb 27, 2020
5fa7811
Constify the PLL definition table
ptesarik Feb 27, 2020
30e6929
Make PLL definitions globally available
ptesarik Feb 27, 2020
b73f135
Make a single A2_PLL_FRAC_MASK macro
ptesarik Feb 27, 2020
fb53a2e
Enhance PLL definitions and reuse them in pll_read
ptesarik Feb 27, 2020
8bc41de
Take the ANA1 pre-divider into account for get_pll_freq()
ptesarik Feb 27, 2020
71a9556
Add a PLL channel enum and store existing data in a table
ptesarik Feb 27, 2020
dd73f4e
Remove unneeded pllX() functions
ptesarik Feb 26, 2020
0420cc6
Implement missing clock sources
ptesarik Feb 27, 2020
4580228
Add all remaining PLL definitions
ptesarik Feb 26, 2020
f926545
Loop over PLLs and channels in pll_dump
ptesarik Feb 26, 2020
e00822e
get_pll_freq: Fix use of pre-dividers
ptesarik Feb 29, 2020
b410017
handle starting a PLL
ptesarik Feb 29, 2020
659558d
Fix get_pll_freq on RPi4
ptesarik Mar 5, 2020
6aeb35d
Rename ANARST and DIGRST bit masks
ptesarik Mar 6, 2020
ebd0961
Consolidate common PLL control register bits
ptesarik Mar 6, 2020
ac4ac8b
Move xtal_freq to pll.h
ptesarik Mar 6, 2020
cfdd1eb
Rename ana1_pdiv_bit to ana1_prescale_bit
ptesarik Mar 6, 2020
53dc6ba
Put more information into the PLL definitions
ptesarik Mar 6, 2020
8429abc
set_pll_freq command
ptesarik Mar 6, 2020
ba3f97a
PLL control fixes, cleanups and clarifications
ptesarik Mar 7, 2020
55fb47a
I2C raw transfer function
ptesarik Mar 8, 2020
da1995d
i2c_set_rate
ptesarik Mar 9, 2020
c27e129
implement sdhost
cleverca22 Jun 9, 2020
73356d1
expose dram init as a command
cleverca22 Jun 9, 2020
67fa6b0
jitter test
cleverca22 Jun 9, 2020
453f4ef
implement clock measuring
cleverca22 Aug 23, 2020
1da15f1
begin the vc4-stage1 binary
cleverca22 Sep 8, 2020
573b1a8
fix release file
cleverca22 Sep 8, 2020
81eddd7
add missing default.nix line
cleverca22 Sep 8, 2020
b0b46be
implement 2 stage bootloader
cleverca22 Sep 10, 2020
bc3a357
clean up alignments in linker script
cleverca22 Sep 10, 2020
ce5757d
redo alignment again
cleverca22 Sep 10, 2020
7d8e9af
fix use after free
cleverca22 Sep 10, 2020
0a982f0
build stage2 on CI
cleverca22 Sep 10, 2020
e9a8f0e
missed a fix in ddr2 driver
cleverca22 Sep 10, 2020
6396c6e
add a hydra-build-products
cleverca22 Sep 10, 2020
697c381
implement xmodem loading of stage2
cleverca22 Sep 13, 2020
18a4f56
improve stage1 more
cleverca22 Sep 14, 2020
1b69585
it versk!! https://www.youtube.com/watch?v=Yu61pURyucM
cleverca22 Sep 15, 2020
6c501e2
add another graphics example
cleverca22 Sep 16, 2020
34483f7
fix the rpi3-bootcode project
cleverca22 Oct 3, 2020
fa37f8b
[arch][arm] fix booting when in HYP mode
cleverca22 Oct 9, 2020
e2367de
overhaul the HVS code, and test it under arm
cleverca22 Oct 9, 2020
d587d3b
build rpi2-test on hydra
cleverca22 Oct 9, 2020
531aba7
implement image loading
cleverca22 Oct 9, 2020
e9db3ab
partially fix pi1, start pi4, overhaul many things
cleverca22 Oct 11, 2020
79eb929
it can now render 13 at once, and the glitch near the top is fixed
cleverca22 Oct 13, 2020
6aed09c
fix rpi3-test on CI
cleverca22 Oct 16, 2020
8f27630
missed a platform change
cleverca22 Oct 16, 2020
fda45ef
and this too
cleverca22 Oct 16, 2020
5ae9f70
fill in the arm64 compiler prefix
cleverca22 Oct 16, 2020
199cb4a
try to fix pi-logo.h rule, improve src filter name
cleverca22 Oct 16, 2020
7b88aa7
also include imagemagick in the env
cleverca22 Oct 16, 2020
42ef69f
work around a bug in nixpkgs
cleverca22 Oct 16, 2020
cba5030
also include python in the build
cleverca22 Oct 16, 2020
4081cf6
arm1176 fixes
cleverca22 Oct 17, 2020
a91059b
include fake barrier opcodes
cleverca22 Oct 17, 2020
abdf484
partially implement dwc2 device mode code
cleverca22 Nov 20, 2020
2e4a35f
the latest code, which works
cleverca22 Nov 27, 2020
4a93a73
saving before mass-delete
cleverca22 Dec 7, 2020
43f9cf2
cleanup of many things, including dwc2 gadget driver
cleverca22 Dec 7, 2020
800027b
move dwc code to its own module, it currently crashes on boot if the …
cleverca22 Jan 8, 2021
1528f64
dwc2 gadget mode can now fully enumerate on a pi0!
cleverca22 Jan 16, 2021
ab4d081
clean up dwc2 code
cleverca22 Jan 16, 2021
8166bb5
fix a missing include
cleverca22 Jan 16, 2021
5f01019
implement the VEC in ntsc mode
cleverca22 Mar 19, 2021
163a6ea
missed this file
cleverca22 Mar 20, 2021
51fe20e
and this file!
cleverca22 Mar 20, 2021
d1ce174
fix power domain issues when booting from SD
cleverca22 Mar 21, 2021
86ecba0
saving previously unsaved changes to dance, and fixing it
cleverca22 Mar 21, 2021
1787a2e
fix vc4-stage1
cleverca22 Mar 21, 2021
8043bb7
and vc4-stage2
cleverca22 Mar 21, 2021
715ef3e
vec dance works
cleverca22 Mar 22, 2021
af84c4d
arm looks to be starting?
cleverca22 Mar 26, 2021
8687ab0
dtop generating pi-logo.h
cleverca22 Mar 28, 2021
15741ea
dont include arm in the rpi3-start project
cleverca22 Mar 28, 2021
38dbc0f
fix dpi.c
cleverca22 Mar 29, 2021
d8738f0
remove unused code
cleverca22 Mar 31, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/rpi-vpu-bootload/bootloader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <app.h>
#include <stdio.h>
#include <platform/bcm28xx/sdram.h>
#include <platform/bcm28xx/otp.h>

static void bootloader_init(const struct app_descriptor *app) {
printf("bootloader init\n");
}

static void bootloader_entry(const struct app_descriptor *app, void *args) {
printf("bootloader entry\n\n");
otp_pretty_print();
//sdram_init();
}

APP_START(bootloader)
.init = bootloader_init,
.entry = bootloader_entry,
APP_END
12 changes: 12 additions & 0 deletions app/rpi-vpu-bootload/rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
LOCAL_DIR := $(GET_LOCAL_DIR)

MODULE := $(LOCAL_DIR)

MODULE_DEPS += \
platform/bcm28xx/rpi-ddr2 \
platform/bcm28xx/otp \


MODULE_SRCS += $(LOCAL_DIR)/bootloader.c

include make/module.mk
15 changes: 15 additions & 0 deletions app/vc4-stage1/rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
LOCAL_DIR := $(GET_LOCAL_DIR)

MODULE := $(LOCAL_DIR)

MODULE_DEPS += \
lib/partition \
lib/fs \
lib/fs/ext2 \
lib/elf \

MODULE_SRCS += \
$(LOCAL_DIR)/stage1.c \

include make/module.mk

212 changes: 212 additions & 0 deletions app/vc4-stage1/stage1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
#include <app.h>
#include <kernel/novm.h>
#include <kernel/thread.h>
#include <kernel/wait.h>
#include <lib/elf.h>
#include <lib/fs.h>
#include <lib/io.h>
#include <lib/partition.h>
#include <lk/debug.h>
#include <lk/init.h>
#include <lk/reg.h>
#include <platform.h>
#include <platform/bcm28xx/pll_read.h>
#include <platform/bcm28xx/power.h>
#include <platform/bcm28xx/sdhost_impl.h>
#include <platform/bcm28xx/sdram.h>
#include <string.h>

#define UNCACHED_RAM 0xc0000000
#define MB (1024*1024)

static void stage2_dram_init(uint level) {
sdram_init();
uint32_t start = UNCACHED_RAM | (1 * MB);
uint32_t length = 10 * MB;
novm_add_arena("dram", start, length);
}

LK_INIT_HOOK(stage1, &stage2_dram_init, LK_INIT_LEVEL_PLATFORM_EARLY + 1);

static ssize_t fs_read_wrapper(struct elf_handle *handle, void *buf, uint64_t offset, size_t len) {
return fs_read_file(handle->read_hook_arg, buf, offset, len);
}

static wait_queue_t waiter;

static int waker_entry(wait_queue_t *waiter) {
char c;
int ret = platform_dgetc(&c, true);
if (ret) {
puts("failed to getc\n");
return 0;
}
if (c == 'X') {
printf("got char 0x%x\n", c);
THREAD_LOCK(state);
wait_queue_wake_all(waiter, false, c);
THREAD_UNLOCK(state);
}
return 0;
}

static void *load_and_run_elf(elf_handle_t *stage2_elf) {
int ret = elf_load(stage2_elf);
if (ret) {
printf("failed to load elf: %d\n", ret);
return;
}
elf_close_handle(stage2_elf);
void *entry = stage2_elf->entry;
free(stage2_elf);
return entry;
}

static void load_stage2(void) {
int ret;

bdev_t *sd = rpi_sdhost_init();
partition_publish("sdhost", 0);
//fs_mount("/boot", "fat32", "sdhostp0");
ret = fs_mount("/root", "ext2", "sdhostp1");
if (ret) {
printf("mount failure: %d\n", ret);
return;
}
filehandle *stage2;
ret = fs_open_file("/root/lk.elf", &stage2);
if (ret) {
printf("open failed: %d\n", ret);
return;
}
elf_handle_t *stage2_elf = malloc(sizeof(elf_handle_t));
ret = elf_open_handle(stage2_elf, fs_read_wrapper, stage2, false);
if (ret) {
printf("failed to elf open: %d\n", ret);
return;
}
void *entry = load_and_run_elf(stage2_elf);
fs_close_file(stage2);
arch_chain_load(entry, 0, 0, 0, 0);
}

struct xmodem_packet {
uint8_t magic;
uint8_t block_num;
uint8_t block_num_invert;
uint8_t payload[128];
uint8_t checksum;
} __attribute__((packed));

//static_assert(sizeof(struct xmodem_packet) == 132, "xmodem packet malformed");

static ssize_t read_repeat(io_handle_t *in, void *buf, ssize_t len) {
ssize_t total_read = 0;
ssize_t ret;
while ((ret = io_read(in, buf, len)) > 0) {
//printf("0X%02x %d\n\n", ((uint8_t*)buf)[0], ret);
len -= ret;
total_read += ret;
buf += ret;
if (len <= 0) return total_read;
}
return -1;
}

static void xmodem_receive(void) {
size_t capacity = 2 * MB;
void *buffer = malloc(capacity);
size_t used = 0;
struct xmodem_packet *packet = malloc(sizeof(struct xmodem_packet));
ssize_t ret;
int blockNr = 1;
bool success = false;

io_write(&console_io, "\x15", 1);
while ((ret = io_read(&console_io, packet, 1)) == 1) {
if (packet->magic == 4) {
puts("R: EOF!");
success = true;
break;
}
ret = read_repeat(&console_io, &packet->block_num, sizeof(struct xmodem_packet) - 1);
if (ret != (sizeof(struct xmodem_packet)-1)) {
puts("read error");
break;
}
uint8_t checksum = 0;
for (int i=0; i<128; i++) {
checksum += packet->payload[i];
}
bool fail = true;
if (packet->checksum == checksum) {
if (packet->block_num_invert == (255 - packet->block_num)) {
if (packet->block_num == (blockNr & 0xff)) {
memcpy(buffer + (128 * (blockNr-1)), packet->payload, 128);
blockNr++;
io_write(&console_io, "\6", 1);
fail = false;
} else if (packet->block_num == ((blockNr - 1) & 0xff)) { // ack was lost, just re-ack
io_write(&console_io, "\6", 1);
} else {
io_write(&console_io, "\x15", 1);
}
} else { // block_invert wrong
io_write(&console_io, "\x15", 1);
}
} else { // wrong checksum
io_write(&console_io, "\x15", 1);
}
if (fail) printf("got packet: %d %d %d %d/%d\n", packet->magic, packet->block_num, packet->block_num_invert, packet->checksum, checksum);
}
printf("final ret was %ld\n", ret);
free(packet);
if (success) {
elf_handle_t *stage2_elf = malloc(sizeof(elf_handle_t));
ret = elf_open_handle_memory(stage2_elf, buffer, blockNr*128);
if (ret) {
printf("failed to elf open: %d\n", ret);
return;
}
void *entry = load_and_run_elf(stage2_elf);
free(buffer);
arch_chain_load(entry, 0, 0, 0, 0);
return;
}
free(buffer);
}


static void stage2_init(const struct app_descriptor *app) {
puts("stage2_init");
}

static void stage2_entry(const struct app_descriptor *app, void *args) {
puts("stage2 entry\n");

puts("press X to stop autoboot and go into xmodem mode...");
wait_queue_init(&waiter);

thread_t *waker = thread_create("waker", waker_entry, &waiter, DEFAULT_PRIORITY, ARCH_DEFAULT_STACK_SIZE);
thread_resume(waker);

THREAD_LOCK(state);
int ret = wait_queue_block(&waiter, 100000);
THREAD_UNLOCK(state);

printf("wait result: %d\n", ret);

if (ret == 'X') {
puts("going into xmodem mode");
uint32_t rsts = *REG32(PM_RSTS);
printf("%x\n", rsts);
xmodem_receive();
} else {
load_stage2();
}
}

APP_START(stage2)
.init = stage2_init,
.entry = stage2_entry,
APP_END
19 changes: 19 additions & 0 deletions app/vc4-stage2/rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
LOCAL_DIR := $(GET_LOCAL_DIR)

MODULE := $(LOCAL_DIR)

MODULE_DEPS += \
lib/partition \
lib/fs \
lib/fs/fat32 \
lib/fs/ext2 \
lib/debugcommands \
app/stringtests \
platform/bcm28xx/dpi \
app/tests \

MODULE_SRCS += \
$(LOCAL_DIR)/stage2.c \

include make/module.mk

26 changes: 26 additions & 0 deletions app/vc4-stage2/stage2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <app.h>
#include <lib/fs.h>
#include <lib/partition.h>
#include <lk/debug.h>
#include <platform/bcm28xx/dpi.h>
#include <platform/bcm28xx/sdhost_impl.h>

static void stage2_init(const struct app_descriptor *app) {
printf("stage2 init\n");
bdev_t *sd = rpi_sdhost_init();
partition_publish("sdhost", 0);
//fs_mount("/boot", "fat32", "sdhostp0");
fs_mount("/root", "ext2", "sdhostp0");
}

static void stage2_entry(const struct app_descriptor *app, void *args) {
printf("stage2 entry\n");
cmd_dpi_start(0, NULL);
cmd_dpi_move(0, NULL);
//cmd_dpi_count(0, NULL);
}

APP_START(stage2)
.init = stage2_init,
.entry = stage2_entry,
APP_END
2 changes: 1 addition & 1 deletion arch/arm/arm/arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ static void arm_basic_setup(void) {
#endif

/* set the vector base to our exception vectors so we don't need to double map at 0 */
#if ARM_ISA_ARMV7
#if ARM_ISA_ARMV7 || ARM_ISA_ARMV6K
arm_write_vbar(KERNEL_BASE + KERNEL_LOAD_OFFSET);
#endif
}
Expand Down
8 changes: 8 additions & 0 deletions arch/arm/arm/ops.S
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
#include <lk/asm.h>
#include <arch/arm/cores.h>

.macro DMB
mcr p15, 0, r0, c7, c10, 5
.endm

.macro DSB
mcr p15, 0, r0, c7, c10, 4
.endm

.text

/* void _arch_enable_ints(void); */
Expand Down
Loading