From 275ed9cd9ce4033f29cad0a05c15e51be2264238 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Mon, 21 May 2012 22:56:21 +0200 Subject: [PATCH] bios: timer support --- software/bios/timer.c | 63 +++++++++++++++++++++++++++++++++++++ software/bios/timer.h | 28 +++++++++++++++++ software/include/hw/timer.h | 44 ++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 software/bios/timer.c create mode 100644 software/bios/timer.h create mode 100644 software/include/hw/timer.h diff --git a/software/bios/timer.c b/software/bios/timer.c new file mode 100644 index 00000000..14e6a8c5 --- /dev/null +++ b/software/bios/timer.c @@ -0,0 +1,63 @@ +/* + * Milkymist SoC (Software) + * Copyright (C) 2012 Sebastien Bourdeauducq + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include "timer.h" + +unsigned int get_system_frequency(void) +{ + return 83333333; /* TODO */ +} + +void timer_enable(int en) +{ + CSR_TIMER0_EN = en; +} + +unsigned int timer_get(void) +{ + return (CSR_TIMER0_COUNT3 << 24) + |(CSR_TIMER0_COUNT2 << 16) + |(CSR_TIMER0_COUNT1 << 8) + |CSR_TIMER0_COUNT0; +} + +void timer_set_counter(unsigned int value) +{ + CSR_TIMER0_COUNT3 = (value & 0xff000000) >> 24; + CSR_TIMER0_COUNT2 = (value & 0x00ff0000) >> 16; + CSR_TIMER0_COUNT1 = (value & 0x0000ff00) >> 8; + CSR_TIMER0_COUNT0 = value & 0x000000ff; +} + +void timer_set_reload(unsigned int value) +{ + CSR_TIMER0_RELOAD3 = (value & 0xff000000) >> 24; + CSR_TIMER0_RELOAD2 = (value & 0x00ff0000) >> 16; + CSR_TIMER0_RELOAD1 = (value & 0x0000ff00) >> 8; + CSR_TIMER0_RELOAD0 = value & 0x000000ff; +} + +void busy_wait(unsigned int ds) +{ + timer_enable(0); + timer_set_reload(0); + timer_set_counter(get_system_frequency()/10*ds); + timer_enable(1); + while(timer_get()); +} diff --git a/software/bios/timer.h b/software/bios/timer.h new file mode 100644 index 00000000..a7810ed2 --- /dev/null +++ b/software/bios/timer.h @@ -0,0 +1,28 @@ +/* + * Milkymist SoC (Software) + * Copyright (C) 2012 Sebastien Bourdeauducq + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __TIMER_H +#define __TIMER_H + +unsigned int get_system_frequency(void); +void timer_enable(int en); +unsigned int timer_get(void); +void timer_set_counter(unsigned int value); +void timer_set_reload(unsigned int value); +void busy_wait(unsigned int ms); + +#endif /* __TIMER_H */ diff --git a/software/include/hw/timer.h b/software/include/hw/timer.h new file mode 100644 index 00000000..3f987e62 --- /dev/null +++ b/software/include/hw/timer.h @@ -0,0 +1,44 @@ +/* + * Milkymist SoC (Software) + * Copyright (C) 2007, 2008, 2009, 2010, 2012 Sebastien Bourdeauducq + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __HW_TIMER_H +#define __HW_TIMER_H + +#include +#include + +#define TIMER0_CSR(x) MMPTR(TIMER0_BASE+(x)) + +#define CSR_TIMER0_EN TIMER0_CSR(0x00) + +#define CSR_TIMER0_COUNT3 TIMER0_CSR(0x04) +#define CSR_TIMER0_COUNT2 TIMER0_CSR(0x08) +#define CSR_TIMER0_COUNT1 TIMER0_CSR(0x0C) +#define CSR_TIMER0_COUNT0 TIMER0_CSR(0x10) + +#define CSR_TIMER0_RELOAD3 TIMER0_CSR(0x14) +#define CSR_TIMER0_RELOAD2 TIMER0_CSR(0x18) +#define CSR_TIMER0_RELOAD1 TIMER0_CSR(0x1C) +#define CSR_TIMER0_RELOAD0 TIMER0_CSR(0x20) + +#define CSR_TIMER0_EV_STAT TIMER0_CSR(0x24) +#define CSR_TIMER0_EV_PENDING TIMER0_CSR(0x28) +#define CSR_TIMER0_EV_ENABLE TIMER0_CSR(0x2C) + +#define TIMER0_EV 0x1 + +#endif /* __HW_TIMER_H */ -- 2.30.2