Mercurial > flash_v2
comparison packages/devs/wallclock/current/src/sh3.cxx @ 78:59d97b6ba612 ecos-sw-2000-03-28
Merge from eCos master repository on 2000-03-28-19:50:47-BST
| author | jlarmour |
|---|---|
| date | Tue, 28 Mar 2000 20:13:29 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 77:abb6bd809e81 | 78:59d97b6ba612 |
|---|---|
| 1 //========================================================================== | |
| 2 // | |
| 3 // devs/wallclock/sh3.cxx | |
| 4 // | |
| 5 // Wallclock emulation implementation | |
| 6 // | |
| 7 //========================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
| 9 // | |
| 10 // ------------------------------------------- | |
| 11 // The contents of this file are subject to the Red Hat eCos Public License | |
| 12 // Version 1.1 (the "License"); you may not use this file except in | |
| 13 // compliance with the License. You may obtain a copy of the License at | |
| 14 // http://www.redhat.com/ | |
| 15 // | |
| 16 // Software distributed under the License is distributed on an "AS IS" | |
| 17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the | |
| 18 // License for the specific language governing rights and limitations under | |
| 19 // the License. | |
| 20 // | |
| 21 // The Original Code is eCos - Embedded Configurable Operating System, | |
| 22 // released September 30, 1998. | |
| 23 // | |
| 24 // The Initial Developer of the Original Code is Red Hat. | |
| 25 // Portions created by Red Hat are | |
| 26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. | |
| 27 // All Rights Reserved. | |
| 28 // ------------------------------------------- | |
| 29 // | |
| 30 //####COPYRIGHTEND#### | |
| 31 //========================================================================== | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 34 // Author(s): jskov | |
| 35 // Contributors: jskov | |
| 36 // Date: 2000-03-17 | |
| 37 // Purpose: Wallclock driver for SH3 CPU RTC module | |
| 38 // | |
| 39 // Notes: Currently only reads from the RTC. There should be a config | |
| 40 // option for selecting whether to do init-read or read-write | |
| 41 // to the clock. This will be useful on targets which have | |
| 42 // battery backing on the RTC circuitry. | |
| 43 // | |
| 44 //####DESCRIPTIONEND#### | |
| 45 // | |
| 46 //========================================================================== | |
| 47 | |
| 48 #include <pkgconf/kernel.h> // Kernel config | |
| 49 #include <pkgconf/wallclock.h> // Wallclock device config | |
| 50 | |
| 51 #include <cyg/infra/cyg_type.h> // Common type definitions and support | |
| 52 #include <cyg/kernel/sched.inl> | |
| 53 | |
| 54 #include <cyg/devs/wallclock.hxx> | |
| 55 | |
| 56 #include <cyg/hal/sh_regs.h> // RTC register definitions | |
| 57 | |
| 58 #include <cyg/infra/diag.h> // Common type definitions and support | |
| 59 | |
| 60 | |
| 61 //----------------------------------------------------------------------------- | |
| 62 // Local static variables | |
| 63 | |
| 64 static Cyg_WallClock wallclock_instance CYG_INIT_PRIORITY( CLOCK ); | |
| 65 | |
| 66 static cyg_uint32 hw_epoch_base; | |
| 67 | |
| 68 static cyg_uint32 epoch_ticks; | |
| 69 static cyg_uint32 epoch_time_stamp; | |
| 70 | |
| 71 Cyg_WallClock *Cyg_WallClock::wallclock; | |
| 72 | |
| 73 | |
| 74 //----------------------------------------------------------------------------- | |
| 75 // BCD helper macros | |
| 76 #define TO_BCD(x) (((x/10)<<4) | (x%10)) | |
| 77 #define TO_DEC(x) (((x>>4)*10) + (x&0xf)) | |
| 78 | |
| 79 //----------------------------------------------------------------------------- | |
| 80 // Functions for setting and getting the hardware clock counters | |
| 81 | |
| 82 // Year must be last two digits of "western calendar year". Leap year when | |
| 83 // divisible by four. | |
| 84 static void | |
| 85 set_sh3_hwclock(cyg_uint32 year, cyg_uint32 month, cyg_uint32 mday, | |
| 86 cyg_uint32 hour, cyg_uint32 minute, cyg_uint32 second) | |
| 87 { | |
| 88 // Stop RTC | |
| 89 HAL_WRITE_UINT8(CYGARC_REG_RCR2, CYGARC_REG_RCR2_RESET); | |
| 90 | |
| 91 // Program it | |
| 92 HAL_WRITE_UINT8(CYGARC_REG_RYRCNT, TO_BCD(year)); | |
| 93 HAL_WRITE_UINT8(CYGARC_REG_RMONCNT, TO_BCD(month)); | |
| 94 HAL_WRITE_UINT8(CYGARC_REG_RDAYCNT, TO_BCD(mday)); | |
| 95 HAL_WRITE_UINT8(CYGARC_REG_RHRCNT, TO_BCD(hour)); | |
| 96 HAL_WRITE_UINT8(CYGARC_REG_RMINCNT, TO_BCD(minute)); | |
| 97 HAL_WRITE_UINT8(CYGARC_REG_RSECCNT, TO_BCD(second)); | |
| 98 | |
| 99 // Start RTC | |
| 100 HAL_WRITE_UINT8(CYGARC_REG_RCR1, CYGARC_REG_RCR1_CIE); | |
| 101 HAL_WRITE_UINT8(CYGARC_REG_RCR2, | |
| 102 CYGARC_REG_RCR2_RTCEN | CYGARC_REG_RCR2_START); | |
| 103 | |
| 104 } | |
| 105 | |
| 106 static void | |
| 107 get_sh3_hwclock(cyg_uint32* year, cyg_uint32* month, cyg_uint32* mday, | |
| 108 cyg_uint32* hour, cyg_uint32* minute, cyg_uint32* second) | |
| 109 { | |
| 110 cyg_uint8 tmp; | |
| 111 | |
| 112 do { | |
| 113 // Clear carry flag | |
| 114 HAL_WRITE_UINT8(CYGARC_REG_RCR1, 0); | |
| 115 | |
| 116 // Read time | |
| 117 HAL_READ_UINT8(CYGARC_REG_RYRCNT, tmp); | |
| 118 *year = TO_DEC(tmp); | |
| 119 HAL_READ_UINT8(CYGARC_REG_RMONCNT, tmp); | |
| 120 *month = TO_DEC(tmp); | |
| 121 HAL_READ_UINT8(CYGARC_REG_RDAYCNT, tmp); | |
| 122 *mday = TO_DEC(tmp); | |
| 123 HAL_READ_UINT8(CYGARC_REG_RHRCNT, tmp); | |
| 124 *hour = TO_DEC(tmp); | |
| 125 HAL_READ_UINT8(CYGARC_REG_RMINCNT, tmp); | |
| 126 *minute = TO_DEC(tmp); | |
| 127 HAL_READ_UINT8(CYGARC_REG_RSECCNT, tmp); | |
| 128 *second = TO_DEC(tmp); | |
| 129 | |
| 130 // Read carry flag | |
| 131 HAL_READ_UINT8(CYGARC_REG_RCR1, tmp); | |
| 132 } while (CYGARC_REG_RCR1_CF & tmp); // loop if carry set | |
| 133 } | |
| 134 | |
| 135 //----------------------------------------------------------------------------- | |
| 136 // Functions providing a simple read-elapsed-seconds interface to the | |
| 137 // hardware clock | |
| 138 | |
| 139 // This function is from the Linux kernel. | |
| 140 // | |
| 141 // Converts Gregorian date to seconds since 1970-01-01 00:00:00. | |
| 142 // Assumes input in normal date format, i.e. 1980-12-31 23:59:59 | |
| 143 // => year=1980, mon=12, day=31, hour=23, min=59, sec=59. | |
| 144 // | |
| 145 // This algorithm was first published by Gauss (I think). | |
| 146 // | |
| 147 // WARNING: this function will overflow on 2106-02-07 06:28:16 on | |
| 148 // machines were long is 32-bit! (However, as time_t is signed, we | |
| 149 // will already get problems at other places on 2038-01-19 03:14:08) | |
| 150 static cyg_uint32 | |
| 151 _simple_mktime(cyg_uint32 year, cyg_uint32 mon, | |
| 152 cyg_uint32 day, cyg_uint32 hour, | |
| 153 cyg_uint32 min, cyg_uint32 sec) | |
| 154 { | |
| 155 if (0 >= (int) (mon -= 2)) { /* 1..12 -> 11,12,1..10 */ | |
| 156 mon += 12; /* Puts Feb last since it has leap day */ | |
| 157 year -= 1; | |
| 158 } | |
| 159 return ((( | |
| 160 (cyg_uint32)(year/4 - year/100 + year/400 + 367*mon/12 + day) + | |
| 161 year*365 - 719499 | |
| 162 )*24 + hour /* now have hours */ | |
| 163 )*60 + min /* now have minutes */ | |
| 164 )*60 + sec; /* finally seconds */ | |
| 165 } | |
| 166 | |
| 167 // Returns number of elapsed ticks since the hw_epoch_base | |
| 168 static inline cyg_uint32 | |
| 169 get_hw_ticks(void) | |
| 170 { | |
| 171 cyg_uint32 year, month, mday, hour, minute, second; | |
| 172 | |
| 173 get_sh3_hwclock(&year, &month, &mday, &hour, &minute, &second); | |
| 174 | |
| 175 #if 0 | |
| 176 // This will cause the test to eventually fail due to these printouts | |
| 177 // causing timer interrupts to be lost... | |
| 178 diag_printf("year %02d\n", year); | |
| 179 diag_printf("month %02d\n", month); | |
| 180 diag_printf("mday %02d\n", mday); | |
| 181 diag_printf("hour %02d\n", hour); | |
| 182 diag_printf("minute %02d\n", minute); | |
| 183 diag_printf("second %02d\n", second); | |
| 184 #endif | |
| 185 | |
| 186 cyg_uint32 now = _simple_mktime(year, month, mday, hour, minute, second); | |
| 187 return now - hw_epoch_base; | |
| 188 } | |
| 189 | |
| 190 static inline void | |
| 191 init_hw_ticks(void) | |
| 192 { | |
| 193 // This is our base: 1970-01-01 00:00:00 | |
| 194 // Set the HW clock - if for nothing else, just to be sure it's in a | |
| 195 // legal range. Any arbitrary base could be used. | |
| 196 // After this the hardware clock is only read. | |
| 197 hw_epoch_base = _simple_mktime(1970,1,1,0,0,0); | |
| 198 set_sh3_hwclock(70,1,1,0,0,0); | |
| 199 } | |
| 200 | |
| 201 //----------------------------------------------------------------------------- | |
| 202 // Constructor | |
| 203 | |
| 204 Cyg_WallClock::Cyg_WallClock() | |
| 205 { | |
| 206 // install instance pointer | |
| 207 wallclock = &wallclock_instance; | |
| 208 | |
| 209 init_hw_ticks(); | |
| 210 } | |
| 211 | |
| 212 //----------------------------------------------------------------------------- | |
| 213 // Returns the current timestamp. This may involve reading the | |
| 214 // hardware, so it may take anything up to a second to complete. | |
| 215 | |
| 216 cyg_uint32 Cyg_WallClock::get_current_time() | |
| 217 { | |
| 218 Cyg_Scheduler::lock(); | |
| 219 | |
| 220 cyg_uint32 diff = get_hw_ticks() - epoch_ticks; | |
| 221 | |
| 222 Cyg_Scheduler::unlock(); | |
| 223 | |
| 224 return epoch_time_stamp + diff; | |
| 225 } | |
| 226 | |
| 227 //----------------------------------------------------------------------------- | |
| 228 // Sets the value of the timestamp relative to now. This may involve | |
| 229 // writing to the hardware, so it may take anything up to a second to | |
| 230 // complete. | |
| 231 void Cyg_WallClock::set_current_time( cyg_uint32 time_stamp ) | |
| 232 { | |
| 233 Cyg_Scheduler::lock(); | |
| 234 | |
| 235 epoch_time_stamp = time_stamp; | |
| 236 epoch_ticks = get_hw_ticks(); | |
| 237 | |
| 238 Cyg_Scheduler::unlock(); | |
| 239 } | |
| 240 | |
| 241 //----------------------------------------------------------------------------- | |
| 242 // End of devs/wallclock/sh3.cxx |
