wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation; either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 17 | * MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Date & Time support for the DS12887 RTC |
| 22 | */ |
| 23 | |
| 24 | #undef RTC_DEBUG |
| 25 | |
| 26 | #include <common.h> |
| 27 | #include <command.h> |
| 28 | #include <config.h> |
| 29 | #include <rtc.h> |
| 30 | |
Michal Simek | 871c18d | 2008-07-14 19:45:37 +0200 | [diff] [blame] | 31 | #if defined(CONFIG_CMD_DATE) |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 32 | |
| 33 | #define RTC_SECONDS 0x00 |
| 34 | #define RTC_SECONDS_ALARM 0x01 |
| 35 | #define RTC_MINUTES 0x02 |
| 36 | #define RTC_MINUTES_ALARM 0x03 |
| 37 | #define RTC_HOURS 0x04 |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 38 | #define RTC_HOURS_ALARM 0x05 |
| 39 | #define RTC_DAY_OF_WEEK 0x06 |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 40 | #define RTC_DATE_OF_MONTH 0x07 |
| 41 | #define RTC_MONTH 0x08 |
| 42 | #define RTC_YEAR 0x09 |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 43 | #define RTC_CONTROL_A 0x0A |
| 44 | #define RTC_CONTROL_B 0x0B |
| 45 | #define RTC_CONTROL_C 0x0C |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 46 | #define RTC_CONTROL_D 0x0D |
| 47 | |
wdenk | 993cad9 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 48 | #define RTC_CA_UIP 0x80 |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 49 | #define RTC_CB_DM 0x04 |
| 50 | #define RTC_CB_24_12 0x02 |
| 51 | #define RTC_CB_SET 0x80 |
| 52 | |
| 53 | #if defined(CONFIG_ATC) |
| 54 | |
| 55 | static uchar rtc_read (uchar reg) |
| 56 | { |
| 57 | uchar val; |
wdenk | 993cad9 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 58 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 59 | *(volatile unsigned char*)(RTC_PORT_ADDR) = reg; |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 60 | __asm__ __volatile__ ("sync"); |
wdenk | 993cad9 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 61 | |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 62 | val = *(volatile unsigned char*)(RTC_PORT_DATA); |
| 63 | return (val); |
| 64 | } |
| 65 | |
| 66 | static void rtc_write (uchar reg, uchar val) |
| 67 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 68 | *(volatile unsigned char*)(RTC_PORT_ADDR) = reg; |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 69 | __asm__ __volatile__ ("sync"); |
| 70 | |
| 71 | *(volatile unsigned char*)(RTC_PORT_DATA) = val; |
| 72 | __asm__ __volatile__ ("sync"); |
| 73 | } |
| 74 | |
| 75 | #else |
| 76 | # error Board specific rtc access functions should be supplied |
| 77 | #endif |
| 78 | |
| 79 | static unsigned bcd2bin (uchar n) |
| 80 | { |
| 81 | return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F)); |
| 82 | } |
| 83 | |
| 84 | static unsigned char bin2bcd (unsigned int n) |
| 85 | { |
| 86 | return (((n / 10) << 4) | (n % 10)); |
| 87 | } |
| 88 | |
| 89 | /* ------------------------------------------------------------------------- */ |
| 90 | |
Yuri Tikhonov | b73a19e | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 91 | int rtc_get (struct rtc_time *tmp) |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 92 | { |
| 93 | uchar sec, min, hour, mday, wday, mon, year; |
| 94 | |
| 95 | /* check if rtc is available for access */ |
| 96 | while( rtc_read(RTC_CONTROL_A) & RTC_CA_UIP) |
| 97 | ; |
wdenk | 993cad9 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 98 | |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 99 | sec = rtc_read(RTC_SECONDS); |
| 100 | min = rtc_read(RTC_MINUTES); |
| 101 | hour = rtc_read(RTC_HOURS); |
| 102 | mday = rtc_read(RTC_DATE_OF_MONTH); |
| 103 | wday = rtc_read(RTC_DAY_OF_WEEK); |
| 104 | mon = rtc_read(RTC_MONTH); |
| 105 | year = rtc_read(RTC_YEAR); |
| 106 | |
| 107 | #ifdef RTC_DEBUG |
| 108 | printf( "Get RTC year: %d; mon: %d; mday: %d; wday: %d; " |
| 109 | "hr: %d; min: %d; sec: %d\n", |
| 110 | year, mon, mday, wday, hour, min, sec ); |
| 111 | |
| 112 | printf ( "Alarms: hour: %02x min: %02x sec: %02x\n", |
| 113 | rtc_read (RTC_HOURS_ALARM), |
| 114 | rtc_read (RTC_MINUTES_ALARM), |
| 115 | rtc_read (RTC_SECONDS_ALARM) ); |
| 116 | #endif |
| 117 | |
| 118 | if( !(rtc_read(RTC_CONTROL_B) & RTC_CB_DM)) |
| 119 | { /* Information is in BCD format */ |
| 120 | printf(" Get: Convert BSD to BIN\n"); |
| 121 | tmp->tm_sec = bcd2bin (sec & 0x7F); |
| 122 | tmp->tm_min = bcd2bin (min & 0x7F); |
| 123 | tmp->tm_hour = bcd2bin (hour & 0x3F); |
| 124 | tmp->tm_mday = bcd2bin (mday & 0x3F); |
| 125 | tmp->tm_mon = bcd2bin (mon & 0x1F); |
| 126 | tmp->tm_year = bcd2bin (year); |
| 127 | tmp->tm_wday = bcd2bin (wday & 0x07); |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | tmp->tm_sec = sec & 0x7F; |
| 132 | tmp->tm_min = min & 0x7F; |
| 133 | tmp->tm_hour = hour & 0x3F; |
| 134 | tmp->tm_mday = mday & 0x3F; |
| 135 | tmp->tm_mon = mon & 0x1F; |
| 136 | tmp->tm_year = year; |
| 137 | tmp->tm_wday = wday & 0x07; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | if(tmp->tm_year<70) |
| 142 | tmp->tm_year+=2000; |
| 143 | else |
| 144 | tmp->tm_year+=1900; |
| 145 | |
| 146 | tmp->tm_yday = 0; |
| 147 | tmp->tm_isdst= 0; |
| 148 | #ifdef RTC_DEBUG |
| 149 | printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 150 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 151 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 152 | #endif |
Yuri Tikhonov | b73a19e | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 153 | |
| 154 | return 0; |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 157 | int rtc_set (struct rtc_time *tmp) |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 158 | { |
| 159 | uchar save_ctrl_b; |
| 160 | uchar sec, min, hour, mday, wday, mon, year; |
| 161 | |
| 162 | #ifdef RTC_DEBUG |
| 163 | printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 164 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 165 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 166 | #endif |
| 167 | |
| 168 | if( !(rtc_read(RTC_CONTROL_B) & RTC_CB_DM)) |
| 169 | { /* Information is in BCD format */ |
| 170 | year = bin2bcd(tmp->tm_year % 100); |
| 171 | mon = bin2bcd(tmp->tm_mon); |
| 172 | wday = bin2bcd(tmp->tm_wday); |
| 173 | mday = bin2bcd(tmp->tm_mday); |
| 174 | hour = bin2bcd(tmp->tm_hour); |
| 175 | min = bin2bcd(tmp->tm_min); |
| 176 | sec = bin2bcd(tmp->tm_sec); |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | year = tmp->tm_year % 100; |
| 181 | mon = tmp->tm_mon; |
| 182 | wday = tmp->tm_wday; |
| 183 | mday = tmp->tm_mday; |
| 184 | hour = tmp->tm_hour; |
| 185 | min = tmp->tm_min; |
| 186 | sec = tmp->tm_sec; |
| 187 | } |
wdenk | 993cad9 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 188 | |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 189 | /* disables the RTC to update the regs */ |
| 190 | save_ctrl_b = rtc_read(RTC_CONTROL_B); |
| 191 | save_ctrl_b |= RTC_CB_SET; |
wdenk | 993cad9 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 192 | rtc_write(RTC_CONTROL_B, save_ctrl_b); |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 193 | |
| 194 | rtc_write (RTC_YEAR, year); |
| 195 | rtc_write (RTC_MONTH, mon); |
| 196 | rtc_write (RTC_DAY_OF_WEEK, wday); |
| 197 | rtc_write (RTC_DATE_OF_MONTH, mday); |
| 198 | rtc_write (RTC_HOURS, hour); |
| 199 | rtc_write (RTC_MINUTES, min); |
| 200 | rtc_write (RTC_SECONDS, sec); |
| 201 | |
| 202 | /* enables the RTC to update the regs */ |
| 203 | save_ctrl_b &= ~RTC_CB_SET; |
wdenk | 993cad9 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 204 | rtc_write(RTC_CONTROL_B, save_ctrl_b); |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 205 | |
| 206 | return 0; |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void rtc_reset (void) |
| 210 | { |
| 211 | struct rtc_time tmp; |
| 212 | uchar ctrl_rg; |
wdenk | 993cad9 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 213 | |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 214 | ctrl_rg = RTC_CB_SET; |
wdenk | 993cad9 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 215 | rtc_write(RTC_CONTROL_B,ctrl_rg); |
| 216 | |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 217 | tmp.tm_year = 1970 % 100; |
| 218 | tmp.tm_mon = 1; |
| 219 | tmp.tm_mday= 1; |
| 220 | tmp.tm_hour = 0; |
| 221 | tmp.tm_min = 0; |
| 222 | tmp.tm_sec = 0; |
wdenk | 993cad9 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 223 | |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 224 | #ifdef RTC_DEBUG |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 225 | printf ( "RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n", |
| 226 | tmp.tm_year, tmp.tm_mon, tmp.tm_mday, |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 227 | tmp.tm_hour, tmp.tm_min, tmp.tm_sec); |
| 228 | #endif |
| 229 | |
| 230 | ctrl_rg = RTC_CB_SET | RTC_CB_24_12 | RTC_CB_DM; |
wdenk | 993cad9 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 231 | rtc_write(RTC_CONTROL_B,ctrl_rg); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 232 | rtc_set(&tmp); |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 233 | |
| 234 | rtc_write(RTC_HOURS_ALARM, 0), |
wdenk | 993cad9 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 235 | rtc_write(RTC_MINUTES_ALARM, 0), |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 236 | rtc_write(RTC_SECONDS_ALARM, 0); |
wdenk | 993cad9 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 237 | |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 238 | ctrl_rg = RTC_CB_24_12 | RTC_CB_DM; |
wdenk | 993cad9 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 239 | rtc_write(RTC_CONTROL_B,ctrl_rg); |
wdenk | 15ef8a5 | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Jon Loeliger | a593814 | 2007-07-09 18:10:50 -0500 | [diff] [blame] | 242 | #endif |