summaryrefslogtreecommitdiff
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
parent162fdeaa979074a0ad9711b94571894d737d3efe (diff)
downloadrockbox-094e62a528e95677a16a9465a1decfdc045e7197.tar.gz
rockbox-094e62a528e95677a16a9465a1decfdc045e7197.zip
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
-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
540target/arm/imx233/powermgmt-imx233.c 540target/arm/imx233/powermgmt-imx233.c
541target/arm/imx233/adc-imx233.c 541target/arm/imx233/adc-imx233.c
542target/arm/imx233/lradc-imx233.c 542target/arm/imx233/lradc-imx233.c
543target/arm/imx233/rtc-imx233.c
543#ifndef BOOTLOADER 544#ifndef BOOTLOADER
544target/arm/imx233/debug-imx233.c 545target/arm/imx233/debug-imx233.c
545#endif 546#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 @@
22#include "system.h" 22#include "system.h"
23#include "rtc.h" 23#include "rtc.h"
24#include "timefuncs.h" 24#include "timefuncs.h"
25#include "rtc-imx233.h"
26
27#if defined(SANSA_FUZEPLUS)
28#define SECS_ADJUST 315532800 /* seconds between 1970-1-1 and 1980-1-1 */
29#else
30#define SECS_ADJUST 0
31#endif
32
33#define MINUTE_SECONDS 60
34#define HOUR_SECONDS 3600
35#define DAY_SECONDS 86400
36#define WEEK_SECONDS 604800
37#define YEAR_SECONDS 31536000
38#define LEAP_YEAR_SECONDS 31622400
39
40/* Days in each month */
41static unsigned int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
42
43static inline bool is_leapyear(int year)
44{
45 if( ((year%4)==0) && (((year%100)!=0) || ((year%400)==0)) )
46 return true;
47 else
48 return false;
49}
25 50
26void rtc_init(void) 51void rtc_init(void)
27{ 52{
53 imx233_rtc_init();
28} 54}
29 55
30int rtc_read_datetime(struct tm *tm) 56int rtc_read_datetime(struct tm *tm)
31{ 57{
32 (void) tm; 58 uint32_t seconds = imx233_rtc_read_seconds() - SECS_ADJUST;
33 return -1; 59 #ifdef SANSA_FUZEPLUS
60 /* The OF uses PERSISTENT2 register to keep the adjustment and only changes
61 * SECONDS if necessary. */
62 seconds += imx233_rtc_read_persistent(2);
63 #else
64 /* The Freescale recommended way of keeping time is the number of seconds
65 * since 00:00 1/1/1980 */
66 #endif
67
68 /* Convert seconds since 00:00 1/1/xxxx (xxxx=year) */
69
70 /* weekday */
71 tm->tm_wday = ((seconds % WEEK_SECONDS) / DAY_SECONDS + 2) % 7;
72
73 /* Year */
74 int year = 1980;
75 while(seconds >= LEAP_YEAR_SECONDS)
76 {
77 if(is_leapyear(year))
78 seconds -= LEAP_YEAR_SECONDS;
79 else
80 seconds -= YEAR_SECONDS;
81
82 year++;
83 }
84
85 if(is_leapyear(year))
86 days_in_month[1] = 29;
87 else
88 {
89 days_in_month[1] = 28;
90 if(seconds>YEAR_SECONDS)
91 {
92 year++;
93 seconds -= YEAR_SECONDS;
94 }
95 }
96 tm->tm_year = year % 100 + 100;
97
98 /* Month */
99 for(int i = 0; i < 12; i++)
100 {
101 if(seconds < days_in_month[i] * DAY_SECONDS)
102 {
103 tm->tm_mon = i;
104 break;
105 }
106
107 seconds -= days_in_month[i] * DAY_SECONDS;
108 }
109
110 /* Month Day */
111 int mday = seconds / DAY_SECONDS;
112 seconds -= mday * DAY_SECONDS;
113 tm->tm_mday = mday + 1; /* 1 ... 31 */
114
115 /* Hour */
116 int hour = seconds / HOUR_SECONDS;
117 seconds -= hour*HOUR_SECONDS;
118 tm->tm_hour = hour;
119
120 /* Minute */
121 int min = seconds / MINUTE_SECONDS;
122 seconds -= min*MINUTE_SECONDS;
123 tm->tm_min = min;
124
125 /* Second */
126 tm->tm_sec = seconds;
127
128 return 0;
34} 129}
35 130
36int rtc_write_datetime(const struct tm *tm) 131int rtc_write_datetime(const struct tm *tm)
37{ 132{
38 (void) tm; 133 int i, year;
39 return -1; 134 unsigned int year_days = 0;
135 unsigned int month_days = 0;
136 unsigned int seconds = 0;
137
138 year = 2000 + tm->tm_year - 100;
139
140 if(is_leapyear(year))
141 days_in_month[1] = 29;
142 else
143 days_in_month[1] = 28;
144
145 /* Number of days in months gone by this year*/
146 for(i = 0; i < tm->tm_mon; i++)
147 month_days += days_in_month[i];
148
149 /* Number of days in years gone by since 1-Jan-1980 */
150 year_days = 365*(tm->tm_year-100+20) + (tm->tm_year-100-1)/4 + 6;
151
152 /* Convert to seconds since 1-Jan-1980 */
153 seconds = tm->tm_sec
154 + tm->tm_min*MINUTE_SECONDS
155 + tm->tm_hour*HOUR_SECONDS
156 + (tm->tm_mday-1)*DAY_SECONDS
157 + month_days*DAY_SECONDS
158 + year_days*DAY_SECONDS;
159 seconds += SECS_ADJUST;
160
161 #ifdef SANSA_FUZEPLUS
162 /* The OF uses PERSISTENT2 register to keep the adjustment and only changes
163 * SECONDS if necessary.
164 * NOTE: the OF uses this mechanism to prevent roll back in time. Although
165 * Rockbox will handle a negative PERSISTENT2 value, the OF will detect
166 * it and won't return in time before SECONDS */
167 imx233_rtc_write_persistent(2, seconds - imx233_rtc_read_seconds());
168 #else
169 /* The Freescale recommended way of keeping time is the number of seconds
170 * since 00:00 1/1/1980 */
171 imx233_rtc_write_seconds(seconds);
172 #endif
173 return 0;
40} 174}
41 175
42void rtc_set_alarm(int h, int m) 176void 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 @@
30#include "power-imx233.h" 30#include "power-imx233.h"
31#include "clkctrl-imx233.h" 31#include "clkctrl-imx233.h"
32#include "powermgmt-imx233.h" 32#include "powermgmt-imx233.h"
33#include "rtc-imx233.h"
33#include "string.h" 34#include "string.h"
34 35
35static struct 36static struct
@@ -313,10 +314,43 @@ bool dbg_hw_info_powermgmt(void)
313 } 314 }
314} 315}
315 316
317bool dbg_hw_info_rtc(void)
318{
319 lcd_setfont(FONT_SYSFIXED);
320
321 while(1)
322 {
323 int button = get_action(CONTEXT_STD, HZ / 10);
324 switch(button)
325 {
326 case ACTION_STD_NEXT:
327 case ACTION_STD_PREV:
328 case ACTION_STD_OK:
329 case ACTION_STD_MENU:
330 lcd_setfont(FONT_UI);
331 return true;
332 case ACTION_STD_CANCEL:
333 lcd_setfont(FONT_UI);
334 return false;
335 }
336
337 lcd_clear_display();
338 struct imx233_rtc_info_t info = imx233_rtc_get_info();
339
340 lcd_putsf(0, 0, "seconds: %lu", info.seconds);
341 for(int i = 0; i < 6; i++)
342 lcd_putsf(0, i + 1, "persistent%d: 0x%lx", i, info.persistent[i]);
343
344 lcd_update();
345 yield();
346 }
347}
348
316bool dbg_hw_info(void) 349bool dbg_hw_info(void)
317{ 350{
318 return dbg_hw_info_clkctrl() && dbg_hw_info_dma() && dbg_hw_info_adc() && 351 return dbg_hw_info_clkctrl() && dbg_hw_info_dma() && dbg_hw_info_adc() &&
319 dbg_hw_info_power() && dbg_hw_info_powermgmt() && dbg_hw_target_info(); 352 dbg_hw_info_power() && dbg_hw_info_powermgmt() && dbg_hw_info_rtc() &&
353 dbg_hw_target_info();
320} 354}
321 355
322bool dbg_ports(void) 356bool 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 @@
25#include "system.h" 25#include "system.h"
26#include "cpu.h" 26#include "cpu.h"
27 27
28#define HW_RTC_BASE 0x8005C000 28#define HW_RTC_BASE 0x8005c000
29 29
30#define HW_RTC_CTRL (*(volatile uint32_t *)(HW_RTC_BASE + 0x0)) 30#define HW_RTC_CTRL (*(volatile uint32_t *)(HW_RTC_BASE + 0x0))
31#define HW_RTC_CTRL__ALARM_IRQ_EN (1 << 0)
32#define HW_RTC_CTRL__ONEMSEC_IRQ_EN (1 << 1)
33#define HW_RTC_CTRL__ALARM_IRQ (1 << 2)
34#define HW_RTC_CTRL__ONEMSEC_IRQ (1 << 3)
35#define HW_RTC_CTRL__WATCHDOGEN (1 << 4)
36#define HW_RTC_CTRL__FORCE_UPDATE (1 << 5)
37#define HW_RTC_CTRL__SUPPRESS_COPY2ANALOG (1 << 6)
38
39#define HW_RTC_STAT (*(volatile uint32_t *)(HW_RTC_BASE + 0x10))
40#define HW_RTC_STAT__NEW_REGS_BP 8
41#define HW_RTC_STAT__NEW_REGS_BM 0xff00
42#define HW_RTC_STAT__STALE_REGS_BP 16
43#define HW_RTC_STAT__STALE_REGS_BM 0xff0000
44#define HW_RTC_STAT__XTAL32768_PRESENT (1 << 27)
45#define HW_RTC_STAT__XTAL32000_PRESENT (1 << 28)
46#define HW_RTC_STAT__WATCHDOG_PRESENT (1 << 29)
47#define HW_RTC_STAT__ALARM_PRESENT (1 << 30)
48#define HW_RTC_STAT__RTC_PRESENT (1 << 31)
49
50#define HW_RTC_MILLISECONDS (*(volatile uint32_t *)(HW_RTC_BASE + 0x20))
51
52#define HW_RTC_SECONDS (*(volatile uint32_t *)(HW_RTC_BASE + 0x30))
53
54#define HW_RTC_ALARM (*(volatile uint32_t *)(HW_RTC_BASE + 0x40))
55
56#define HW_RTC_WATCHDOG (*(volatile uint32_t *)(HW_RTC_BASE + 0x50))
57
58#define HW_RTC_PERSISTENTx(x) (*(volatile uint32_t *)(HW_RTC_BASE + 0x60 + (x) * 0x10))
31 59
32#define HW_RTC_PERSISTENT0 (*(volatile uint32_t *)(HW_RTC_BASE + 0x60)) 60#define HW_RTC_PERSISTENT0 (*(volatile uint32_t *)(HW_RTC_BASE + 0x60))
61#define HW_RTC_PERSISTENT0__CLOCKSOURCE (1 << 0)
62#define HW_RTC_PERSISTENT0__ALARM_WAKE_EN (1 << 1)
63#define HW_RTC_PERSISTENT0__ALARM_EN (1 << 2)
64#define HW_RTC_PERSISTENT0__XTAL24MHZ_PWRUP (1 << 4)
65#define HW_RTC_PERSISTENT0__XTAL32KHZ_PWRUP (1 << 5)
66#define HW_RTC_PERSISTENT0__XTAL32_FREQ (1 << 6)
67#define HW_RTC_PERSISTENT0__ALARM_WAKE (1 << 7)
68#define HW_RTC_PERSISTENT0__AUTO_RESTART (1 << 17)
33#define HW_RTC_PERSISTENT0__SPARE_BP 18 69#define HW_RTC_PERSISTENT0__SPARE_BP 18
34#define HW_RTC_PERSISTENT0__SPARE_BM (0x3fff << 18) 70#define HW_RTC_PERSISTENT0__SPARE_BM (0x3fff << 18)
35#define HW_RTC_PERSISTENT0__SPARE__RELEASE_GND (1 << 19) 71#define HW_RTC_PERSISTENT0__SPARE__RELEASE_GND (1 << 19)
36 72
73#define HW_RTC_PERSISTENT1 (*(volatile uint32_t *)(HW_RTC_BASE + 0x70))
74
75#define HW_RTC_PERSISTENT2 (*(volatile uint32_t *)(HW_RTC_BASE + 0x80))
76
77#define HW_RTC_PERSISTENT3 (*(volatile uint32_t *)(HW_RTC_BASE + 0x90))
78
79#define HW_RTC_PERSISTENT4 (*(volatile uint32_t *)(HW_RTC_BASE + 0xa0))
80
81#define HW_RTC_PERSISTENT5 (*(volatile uint32_t *)(HW_RTC_BASE + 0xb0))
82
83struct imx233_rtc_info_t
84{
85 uint32_t seconds;
86 uint32_t persistent[6];
87};
88
89static inline void imx233_rtc_init(void)
90{
91 __REG_CLR(HW_RTC_CTRL) = __BLOCK_CLKGATE;
92}
93
94static inline uint32_t imx233_rtc_read_seconds(void)
95{
96 return HW_RTC_SECONDS;
97}
98
99static inline uint32_t imx233_rtc_read_persistent(int idx)
100{
101 return HW_RTC_PERSISTENTx(idx);
102}
103
104void imx233_rtc_write_seconds(uint32_t seconds);
105void imx233_rtc_write_persistent(int idx, uint32_t val);
106
107struct imx233_rtc_info_t imx233_rtc_get_info(void);
108
37#endif /* RTC_IMX233_H */ 109#endif /* RTC_IMX233_H */