summaryrefslogtreecommitdiff
path: root/firmware
diff options
context:
space:
mode:
authorAmaury Pouly <pamaury@rockbox.org>2011-12-31 13:35:45 +0000
committerAmaury Pouly <pamaury@rockbox.org>2011-12-31 13:35:45 +0000
commit094e62a528e95677a16a9465a1decfdc045e7197 (patch)
tree03018b3aebf4c0938f2b3e0936233d036ea54829 /firmware
parent162fdeaa979074a0ad9711b94571894d737d3efe (diff)
imx233/fuze+: implement rtc (time only, alarm still to implement)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@31473 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware')
-rw-r--r--firmware/SOURCES1
-rw-r--r--firmware/drivers/rtc/rtc_imx233.c142
-rw-r--r--firmware/target/arm/imx233/debug-imx233.c36
-rw-r--r--firmware/target/arm/imx233/rtc-imx233.h74
4 files changed, 247 insertions, 6 deletions
diff --git a/firmware/SOURCES b/firmware/SOURCES
index 46ef51beea..e792bee89f 100644
--- a/firmware/SOURCES
+++ b/firmware/SOURCES
@@ -540,6 +540,7 @@ target/arm/imx233/power-imx233.c
target/arm/imx233/powermgmt-imx233.c
target/arm/imx233/adc-imx233.c
target/arm/imx233/lradc-imx233.c
+target/arm/imx233/rtc-imx233.c
#ifndef BOOTLOADER
target/arm/imx233/debug-imx233.c
#endif
diff --git a/firmware/drivers/rtc/rtc_imx233.c b/firmware/drivers/rtc/rtc_imx233.c
index 4368610875..9e62476be6 100644
--- a/firmware/drivers/rtc/rtc_imx233.c
+++ b/firmware/drivers/rtc/rtc_imx233.c
@@ -22,21 +22,155 @@
#include "system.h"
#include "rtc.h"
#include "timefuncs.h"
+#include "rtc-imx233.h"
+
+#if defined(SANSA_FUZEPLUS)
+#define SECS_ADJUST 315532800 /* seconds between 1970-1-1 and 1980-1-1 */
+#else
+#define SECS_ADJUST 0
+#endif
+
+#define MINUTE_SECONDS 60
+#define HOUR_SECONDS 3600
+#define DAY_SECONDS 86400
+#define WEEK_SECONDS 604800
+#define YEAR_SECONDS 31536000
+#define LEAP_YEAR_SECONDS 31622400
+
+/* Days in each month */
+static unsigned int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
+
+static inline bool is_leapyear(int year)
+{
+ if( ((year%4)==0) && (((year%100)!=0) || ((year%400)==0)) )
+ return true;
+ else
+ return false;
+}
void rtc_init(void)
{
+ imx233_rtc_init();
}
int rtc_read_datetime(struct tm *tm)
{
- (void) tm;
- return -1;
+ uint32_t seconds = imx233_rtc_read_seconds() - SECS_ADJUST;
+ #ifdef SANSA_FUZEPLUS
+ /* The OF uses PERSISTENT2 register to keep the adjustment and only changes
+ * SECONDS if necessary. */
+ seconds += imx233_rtc_read_persistent(2);
+ #else
+ /* The Freescale recommended way of keeping time is the number of seconds
+ * since 00:00 1/1/1980 */
+ #endif
+
+ /* Convert seconds since 00:00 1/1/xxxx (xxxx=year) */
+
+ /* weekday */
+ tm->tm_wday = ((seconds % WEEK_SECONDS) / DAY_SECONDS + 2) % 7;
+
+ /* Year */
+ int year = 1980;
+ while(seconds >= LEAP_YEAR_SECONDS)
+ {
+ if(is_leapyear(year))
+ seconds -= LEAP_YEAR_SECONDS;
+ else
+ seconds -= YEAR_SECONDS;
+
+ year++;
+ }
+
+ if(is_leapyear(year))
+ days_in_month[1] = 29;
+ else
+ {
+ days_in_month[1] = 28;
+ if(seconds>YEAR_SECONDS)
+ {
+ year++;
+ seconds -= YEAR_SECONDS;
+ }
+ }
+ tm->tm_year = year % 100 + 100;
+
+ /* Month */
+ for(int i = 0; i < 12; i++)
+ {
+ if(seconds < days_in_month[i] * DAY_SECONDS)
+ {
+ tm->tm_mon = i;
+ break;
+ }
+
+ seconds -= days_in_month[i] * DAY_SECONDS;
+ }
+
+ /* Month Day */
+ int mday = seconds / DAY_SECONDS;
+ seconds -= mday * DAY_SECONDS;
+ tm->tm_mday = mday + 1; /* 1 ... 31 */
+
+ /* Hour */
+ int hour = seconds / HOUR_SECONDS;
+ seconds -= hour*HOUR_SECONDS;
+ tm->tm_hour = hour;
+
+ /* Minute */
+ int min = seconds / MINUTE_SECONDS;
+ seconds -= min*MINUTE_SECONDS;
+ tm->tm_min = min;
+
+ /* Second */
+ tm->tm_sec = seconds;
+
+ return 0;
}
int rtc_write_datetime(const struct tm *tm)
{
- (void) tm;
- return -1;
+ int i, year;
+ unsigned int year_days = 0;
+ unsigned int month_days = 0;
+ unsigned int seconds = 0;
+
+ year = 2000 + tm->tm_year - 100;
+
+ if(is_leapyear(year))
+ days_in_month[1] = 29;
+ else
+ days_in_month[1] = 28;
+
+ /* Number of days in months gone by this year*/
+ for(i = 0; i < tm->tm_mon; i++)
+ month_days += days_in_month[i];
+
+ /* Number of days in years gone by since 1-Jan-1980 */
+ year_days = 365*(tm->tm_year-100+20) + (tm->tm_year-100-1)/4 + 6;
+
+ /* Convert to seconds since 1-Jan-1980 */
+ seconds = tm->tm_sec
+ + tm->tm_min*MINUTE_SECONDS
+ + tm->tm_hour*HOUR_SECONDS
+ + (tm->tm_mday-1)*DAY_SECONDS
+ + month_days*DAY_SECONDS
+ + year_days*DAY_SECONDS;
+ seconds += SECS_ADJUST;
+
+ #ifdef SANSA_FUZEPLUS
+ /* The OF uses PERSISTENT2 register to keep the adjustment and only changes
+ * SECONDS if necessary.
+ * NOTE: the OF uses this mechanism to prevent roll back in time. Although
+ * Rockbox will handle a negative PERSISTENT2 value, the OF will detect
+ * it and won't return in time before SECONDS */
+ imx233_rtc_write_persistent(2, seconds - imx233_rtc_read_seconds());
+ #else
+ /* The Freescale recommended way of keeping time is the number of seconds
+ * since 00:00 1/1/1980 */
+ imx233_rtc_write_seconds(seconds);
+ #endif
+ return 0;
}
void rtc_set_alarm(int h, int m)
diff --git a/firmware/target/arm/imx233/debug-imx233.c b/firmware/target/arm/imx233/debug-imx233.c
index 896c5bf6c3..228d2abcdb 100644
--- a/firmware/target/arm/imx233/debug-imx233.c
+++ b/firmware/target/arm/imx233/debug-imx233.c
@@ -30,6 +30,7 @@
#include "power-imx233.h"
#include "clkctrl-imx233.h"
#include "powermgmt-imx233.h"
+#include "rtc-imx233.h"
#include "string.h"
static struct
@@ -313,10 +314,43 @@ bool dbg_hw_info_powermgmt(void)
}
}
+bool dbg_hw_info_rtc(void)
+{
+ lcd_setfont(FONT_SYSFIXED);
+
+ while(1)
+ {
+ int button = get_action(CONTEXT_STD, HZ / 10);
+ switch(button)
+ {
+ case ACTION_STD_NEXT:
+ case ACTION_STD_PREV:
+ case ACTION_STD_OK:
+ case ACTION_STD_MENU:
+ lcd_setfont(FONT_UI);
+ return true;
+ case ACTION_STD_CANCEL:
+ lcd_setfont(FONT_UI);
+ return false;
+ }
+
+ lcd_clear_display();
+ struct imx233_rtc_info_t info = imx233_rtc_get_info();
+
+ lcd_putsf(0, 0, "seconds: %lu", info.seconds);
+ for(int i = 0; i < 6; i++)
+ lcd_putsf(0, i + 1, "persistent%d: 0x%lx", i, info.persistent[i]);
+
+ lcd_update();
+ yield();
+ }
+}
+
bool dbg_hw_info(void)
{
return dbg_hw_info_clkctrl() && dbg_hw_info_dma() && dbg_hw_info_adc() &&
- dbg_hw_info_power() && dbg_hw_info_powermgmt() && dbg_hw_target_info();
+ dbg_hw_info_power() && dbg_hw_info_powermgmt() && dbg_hw_info_rtc() &&
+ dbg_hw_target_info();
}
bool dbg_ports(void)
diff --git a/firmware/target/arm/imx233/rtc-imx233.h b/firmware/target/arm/imx233/rtc-imx233.h
index c26832ab57..054ace40b8 100644
--- a/firmware/target/arm/imx233/rtc-imx233.h
+++ b/firmware/target/arm/imx233/rtc-imx233.h
@@ -25,13 +25,85 @@
#include "system.h"
#include "cpu.h"
-#define HW_RTC_BASE 0x8005C000
+#define HW_RTC_BASE 0x8005c000
#define HW_RTC_CTRL (*(volatile uint32_t *)(HW_RTC_BASE + 0x0))
+#define HW_RTC_CTRL__ALARM_IRQ_EN (1 << 0)
+#define HW_RTC_CTRL__ONEMSEC_IRQ_EN (1 << 1)
+#define HW_RTC_CTRL__ALARM_IRQ (1 << 2)
+#define HW_RTC_CTRL__ONEMSEC_IRQ (1 << 3)
+#define HW_RTC_CTRL__WATCHDOGEN (1 << 4)
+#define HW_RTC_CTRL__FORCE_UPDATE (1 << 5)
+#define HW_RTC_CTRL__SUPPRESS_COPY2ANALOG (1 << 6)
+
+#define HW_RTC_STAT (*(volatile uint32_t *)(HW_RTC_BASE + 0x10))
+#define HW_RTC_STAT__NEW_REGS_BP 8
+#define HW_RTC_STAT__NEW_REGS_BM 0xff00
+#define HW_RTC_STAT__STALE_REGS_BP 16
+#define HW_RTC_STAT__STALE_REGS_BM 0xff0000
+#define HW_RTC_STAT__XTAL32768_PRESENT (1 << 27)
+#define HW_RTC_STAT__XTAL32000_PRESENT (1 << 28)
+#define HW_RTC_STAT__WATCHDOG_PRESENT (1 << 29)
+#define HW_RTC_STAT__ALARM_PRESENT (1 << 30)
+#define HW_RTC_STAT__RTC_PRESENT (1 << 31)
+
+#define HW_RTC_MILLISECONDS (*(volatile uint32_t *)(HW_RTC_BASE + 0x20))
+
+#define HW_RTC_SECONDS (*(volatile uint32_t *)(HW_RTC_BASE + 0x30))
+
+#define HW_RTC_ALARM (*(volatile uint32_t *)(HW_RTC_BASE + 0x40))
+
+#define HW_RTC_WATCHDOG (*(volatile uint32_t *)(HW_RTC_BASE + 0x50))
+
+#define HW_RTC_PERSISTENTx(x) (*(volatile uint32_t *)(HW_RTC_BASE + 0x60 + (x) * 0x10))
#define HW_RTC_PERSISTENT0 (*(volatile uint32_t *)(HW_RTC_BASE + 0x60))
+#define HW_RTC_PERSISTENT0__CLOCKSOURCE (1 << 0)
+#define HW_RTC_PERSISTENT0__ALARM_WAKE_EN (1 << 1)
+#define HW_RTC_PERSISTENT0__ALARM_EN (1 << 2)
+#define HW_RTC_PERSISTENT0__XTAL24MHZ_PWRUP (1 << 4)
+#define HW_RTC_PERSISTENT0__XTAL32KHZ_PWRUP (1 << 5)
+#define HW_RTC_PERSISTENT0__XTAL32_FREQ (1 << 6)
+#define HW_RTC_PERSISTENT0__ALARM_WAKE (1 << 7)
+#define HW_RTC_PERSISTENT0__AUTO_RESTART (1 << 17)
#define HW_RTC_PERSISTENT0__SPARE_BP 18
#define HW_RTC_PERSISTENT0__SPARE_BM (0x3fff << 18)
#define HW_RTC_PERSISTENT0__SPARE__RELEASE_GND (1 << 19)
+#define HW_RTC_PERSISTENT1 (*(volatile uint32_t *)(HW_RTC_BASE + 0x70))
+
+#define HW_RTC_PERSISTENT2 (*(volatile uint32_t *)(HW_RTC_BASE + 0x80))
+
+#define HW_RTC_PERSISTENT3 (*(volatile uint32_t *)(HW_RTC_BASE + 0x90))
+
+#define HW_RTC_PERSISTENT4 (*(volatile uint32_t *)(HW_RTC_BASE + 0xa0))
+
+#define HW_RTC_PERSISTENT5 (*(volatile uint32_t *)(HW_RTC_BASE + 0xb0))
+
+struct imx233_rtc_info_t
+{
+ uint32_t seconds;
+ uint32_t persistent[6];
+};
+
+static inline void imx233_rtc_init(void)
+{
+ __REG_CLR(HW_RTC_CTRL) = __BLOCK_CLKGATE;
+}
+
+static inline uint32_t imx233_rtc_read_seconds(void)
+{
+ return HW_RTC_SECONDS;
+}
+
+static inline uint32_t imx233_rtc_read_persistent(int idx)
+{
+ return HW_RTC_PERSISTENTx(idx);
+}
+
+void imx233_rtc_write_seconds(uint32_t seconds);
+void imx233_rtc_write_persistent(int idx, uint32_t val);
+
+struct imx233_rtc_info_t imx233_rtc_get_info(void);
+
#endif /* RTC_IMX233_H */