wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Date & Time support for the DS12887 RTC |
| 9 | */ |
| 10 | |
| 11 | #undef RTC_DEBUG |
| 12 | |
| 13 | #include <common.h> |
| 14 | #include <command.h> |
| 15 | #include <config.h> |
| 16 | #include <rtc.h> |
| 17 | |
Michal Simek | c3e6c55 | 2008-07-14 19:45:37 +0200 | [diff] [blame] | 18 | #if defined(CONFIG_CMD_DATE) |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 19 | |
| 20 | #define RTC_SECONDS 0x00 |
| 21 | #define RTC_SECONDS_ALARM 0x01 |
| 22 | #define RTC_MINUTES 0x02 |
| 23 | #define RTC_MINUTES_ALARM 0x03 |
| 24 | #define RTC_HOURS 0x04 |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 25 | #define RTC_HOURS_ALARM 0x05 |
| 26 | #define RTC_DAY_OF_WEEK 0x06 |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 27 | #define RTC_DATE_OF_MONTH 0x07 |
| 28 | #define RTC_MONTH 0x08 |
| 29 | #define RTC_YEAR 0x09 |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 30 | #define RTC_CONTROL_A 0x0A |
| 31 | #define RTC_CONTROL_B 0x0B |
| 32 | #define RTC_CONTROL_C 0x0C |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 33 | #define RTC_CONTROL_D 0x0D |
| 34 | |
wdenk | 70764a3 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 35 | #define RTC_CA_UIP 0x80 |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 36 | #define RTC_CB_DM 0x04 |
| 37 | #define RTC_CB_24_12 0x02 |
| 38 | #define RTC_CB_SET 0x80 |
| 39 | |
| 40 | #if defined(CONFIG_ATC) |
| 41 | |
| 42 | static uchar rtc_read (uchar reg) |
| 43 | { |
| 44 | uchar val; |
wdenk | 70764a3 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 45 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 46 | *(volatile unsigned char*)(RTC_PORT_ADDR) = reg; |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 47 | __asm__ __volatile__ ("sync"); |
wdenk | 70764a3 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 48 | |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 49 | val = *(volatile unsigned char*)(RTC_PORT_DATA); |
| 50 | return (val); |
| 51 | } |
| 52 | |
| 53 | static void rtc_write (uchar reg, uchar val) |
| 54 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 55 | *(volatile unsigned char*)(RTC_PORT_ADDR) = reg; |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 56 | __asm__ __volatile__ ("sync"); |
| 57 | |
| 58 | *(volatile unsigned char*)(RTC_PORT_DATA) = val; |
| 59 | __asm__ __volatile__ ("sync"); |
| 60 | } |
| 61 | |
| 62 | #else |
| 63 | # error Board specific rtc access functions should be supplied |
| 64 | #endif |
| 65 | |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 66 | int rtc_get (struct rtc_time *tmp) |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 67 | { |
| 68 | uchar sec, min, hour, mday, wday, mon, year; |
| 69 | |
| 70 | /* check if rtc is available for access */ |
| 71 | while( rtc_read(RTC_CONTROL_A) & RTC_CA_UIP) |
| 72 | ; |
wdenk | 70764a3 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 73 | |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 74 | sec = rtc_read(RTC_SECONDS); |
| 75 | min = rtc_read(RTC_MINUTES); |
| 76 | hour = rtc_read(RTC_HOURS); |
| 77 | mday = rtc_read(RTC_DATE_OF_MONTH); |
| 78 | wday = rtc_read(RTC_DAY_OF_WEEK); |
| 79 | mon = rtc_read(RTC_MONTH); |
| 80 | year = rtc_read(RTC_YEAR); |
| 81 | |
| 82 | #ifdef RTC_DEBUG |
| 83 | printf( "Get RTC year: %d; mon: %d; mday: %d; wday: %d; " |
| 84 | "hr: %d; min: %d; sec: %d\n", |
| 85 | year, mon, mday, wday, hour, min, sec ); |
| 86 | |
| 87 | printf ( "Alarms: hour: %02x min: %02x sec: %02x\n", |
| 88 | rtc_read (RTC_HOURS_ALARM), |
| 89 | rtc_read (RTC_MINUTES_ALARM), |
| 90 | rtc_read (RTC_SECONDS_ALARM) ); |
| 91 | #endif |
| 92 | |
| 93 | if( !(rtc_read(RTC_CONTROL_B) & RTC_CB_DM)) |
| 94 | { /* Information is in BCD format */ |
| 95 | printf(" Get: Convert BSD to BIN\n"); |
| 96 | tmp->tm_sec = bcd2bin (sec & 0x7F); |
| 97 | tmp->tm_min = bcd2bin (min & 0x7F); |
| 98 | tmp->tm_hour = bcd2bin (hour & 0x3F); |
| 99 | tmp->tm_mday = bcd2bin (mday & 0x3F); |
| 100 | tmp->tm_mon = bcd2bin (mon & 0x1F); |
| 101 | tmp->tm_year = bcd2bin (year); |
| 102 | tmp->tm_wday = bcd2bin (wday & 0x07); |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | tmp->tm_sec = sec & 0x7F; |
| 107 | tmp->tm_min = min & 0x7F; |
| 108 | tmp->tm_hour = hour & 0x3F; |
| 109 | tmp->tm_mday = mday & 0x3F; |
| 110 | tmp->tm_mon = mon & 0x1F; |
| 111 | tmp->tm_year = year; |
| 112 | tmp->tm_wday = wday & 0x07; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | if(tmp->tm_year<70) |
| 117 | tmp->tm_year+=2000; |
| 118 | else |
| 119 | tmp->tm_year+=1900; |
| 120 | |
| 121 | tmp->tm_yday = 0; |
| 122 | tmp->tm_isdst= 0; |
| 123 | #ifdef RTC_DEBUG |
| 124 | printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 125 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 126 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 127 | #endif |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 128 | |
| 129 | return 0; |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 132 | int rtc_set (struct rtc_time *tmp) |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 133 | { |
| 134 | uchar save_ctrl_b; |
| 135 | uchar sec, min, hour, mday, wday, mon, year; |
| 136 | |
| 137 | #ifdef RTC_DEBUG |
| 138 | printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 139 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 140 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 141 | #endif |
| 142 | |
| 143 | if( !(rtc_read(RTC_CONTROL_B) & RTC_CB_DM)) |
| 144 | { /* Information is in BCD format */ |
| 145 | year = bin2bcd(tmp->tm_year % 100); |
| 146 | mon = bin2bcd(tmp->tm_mon); |
| 147 | wday = bin2bcd(tmp->tm_wday); |
| 148 | mday = bin2bcd(tmp->tm_mday); |
| 149 | hour = bin2bcd(tmp->tm_hour); |
| 150 | min = bin2bcd(tmp->tm_min); |
| 151 | sec = bin2bcd(tmp->tm_sec); |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | year = tmp->tm_year % 100; |
| 156 | mon = tmp->tm_mon; |
| 157 | wday = tmp->tm_wday; |
| 158 | mday = tmp->tm_mday; |
| 159 | hour = tmp->tm_hour; |
| 160 | min = tmp->tm_min; |
| 161 | sec = tmp->tm_sec; |
| 162 | } |
wdenk | 70764a3 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 163 | |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 164 | /* disables the RTC to update the regs */ |
| 165 | save_ctrl_b = rtc_read(RTC_CONTROL_B); |
| 166 | save_ctrl_b |= RTC_CB_SET; |
wdenk | 70764a3 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 167 | rtc_write(RTC_CONTROL_B, save_ctrl_b); |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 168 | |
| 169 | rtc_write (RTC_YEAR, year); |
| 170 | rtc_write (RTC_MONTH, mon); |
| 171 | rtc_write (RTC_DAY_OF_WEEK, wday); |
| 172 | rtc_write (RTC_DATE_OF_MONTH, mday); |
| 173 | rtc_write (RTC_HOURS, hour); |
| 174 | rtc_write (RTC_MINUTES, min); |
| 175 | rtc_write (RTC_SECONDS, sec); |
| 176 | |
| 177 | /* enables the RTC to update the regs */ |
| 178 | save_ctrl_b &= ~RTC_CB_SET; |
wdenk | 70764a3 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 179 | rtc_write(RTC_CONTROL_B, save_ctrl_b); |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 180 | |
| 181 | return 0; |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void rtc_reset (void) |
| 185 | { |
| 186 | struct rtc_time tmp; |
| 187 | uchar ctrl_rg; |
wdenk | 70764a3 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 188 | |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 189 | ctrl_rg = RTC_CB_SET; |
wdenk | 70764a3 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 190 | rtc_write(RTC_CONTROL_B,ctrl_rg); |
| 191 | |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 192 | tmp.tm_year = 1970 % 100; |
| 193 | tmp.tm_mon = 1; |
| 194 | tmp.tm_mday= 1; |
| 195 | tmp.tm_hour = 0; |
| 196 | tmp.tm_min = 0; |
| 197 | tmp.tm_sec = 0; |
wdenk | 70764a3 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 198 | |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 199 | #ifdef RTC_DEBUG |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 200 | printf ( "RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n", |
| 201 | tmp.tm_year, tmp.tm_mon, tmp.tm_mday, |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 202 | tmp.tm_hour, tmp.tm_min, tmp.tm_sec); |
| 203 | #endif |
| 204 | |
| 205 | ctrl_rg = RTC_CB_SET | RTC_CB_24_12 | RTC_CB_DM; |
wdenk | 70764a3 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 206 | rtc_write(RTC_CONTROL_B,ctrl_rg); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 207 | rtc_set(&tmp); |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 208 | |
| 209 | rtc_write(RTC_HOURS_ALARM, 0), |
wdenk | 70764a3 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 210 | rtc_write(RTC_MINUTES_ALARM, 0), |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 211 | rtc_write(RTC_SECONDS_ALARM, 0); |
wdenk | 70764a3 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 212 | |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 213 | ctrl_rg = RTC_CB_24_12 | RTC_CB_DM; |
wdenk | 70764a3 | 2003-06-26 22:04:09 +0000 | [diff] [blame] | 214 | rtc_write(RTC_CONTROL_B,ctrl_rg); |
wdenk | d155afb | 2003-06-18 20:22:24 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Jon Loeliger | f7e9125 | 2007-07-09 18:10:50 -0500 | [diff] [blame] | 217 | #endif |