wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Denis Peter MPL AG Switzerland. d.peter@mpl.ch |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * Date & Time support for the MC146818 (PIXX4) RTC |
| 26 | */ |
| 27 | |
| 28 | /*#define DEBUG*/ |
| 29 | |
| 30 | #include <common.h> |
| 31 | #include <command.h> |
| 32 | #include <rtc.h> |
| 33 | |
Michal Simek | c3e6c55 | 2008-07-14 19:45:37 +0200 | [diff] [blame] | 34 | #if defined(CONFIG_CMD_DATE) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 35 | |
| 36 | static uchar rtc_read (uchar reg); |
| 37 | static void rtc_write (uchar reg, uchar val); |
| 38 | static uchar bin2bcd (unsigned int n); |
| 39 | static unsigned bcd2bin(uchar c); |
| 40 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame^] | 41 | #define RTC_PORT_MC146818 CONFIG_SYS_ISA_IO_BASE_ADDRESS + 0x70 |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 42 | #define RTC_SECONDS 0x00 |
| 43 | #define RTC_SECONDS_ALARM 0x01 |
| 44 | #define RTC_MINUTES 0x02 |
| 45 | #define RTC_MINUTES_ALARM 0x03 |
| 46 | #define RTC_HOURS 0x04 |
| 47 | #define RTC_HOURS_ALARM 0x05 |
| 48 | #define RTC_DAY_OF_WEEK 0x06 |
| 49 | #define RTC_DATE_OF_MONTH 0x07 |
| 50 | #define RTC_MONTH 0x08 |
| 51 | #define RTC_YEAR 0x09 |
| 52 | #define RTC_CONFIG_A 0x0A |
| 53 | #define RTC_CONFIG_B 0x0B |
| 54 | #define RTC_CONFIG_C 0x0C |
| 55 | #define RTC_CONFIG_D 0x0D |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 56 | |
| 57 | |
| 58 | /* ------------------------------------------------------------------------- */ |
| 59 | |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 60 | int rtc_get (struct rtc_time *tmp) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 61 | { |
| 62 | uchar sec, min, hour, mday, wday, mon, year; |
| 63 | /* here check if rtc can be accessed */ |
| 64 | while((rtc_read(RTC_CONFIG_A)&0x80)==0x80); |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 65 | sec = rtc_read (RTC_SECONDS); |
| 66 | min = rtc_read (RTC_MINUTES); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 67 | hour = rtc_read (RTC_HOURS); |
| 68 | mday = rtc_read (RTC_DATE_OF_MONTH); |
| 69 | wday = rtc_read (RTC_DAY_OF_WEEK); |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 70 | mon = rtc_read (RTC_MONTH); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 71 | year = rtc_read (RTC_YEAR); |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 72 | #ifdef CONFIG_AMIGAONEG3SE |
| 73 | wday -= 1; /* VIA 686 stores Sunday = 1, Monday = 2, ... */ |
| 74 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 75 | #ifdef RTC_DEBUG |
| 76 | printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x " |
| 77 | "hr: %02x min: %02x sec: %02x\n", |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 78 | year, mon, mday, wday, |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 79 | hour, min, sec ); |
| 80 | printf ( "Alarms: month: %02x hour: %02x min: %02x sec: %02x\n", |
| 81 | rtc_read (RTC_CONFIG_D) & 0x3F, |
| 82 | rtc_read (RTC_HOURS_ALARM), |
| 83 | rtc_read (RTC_MINUTES_ALARM), |
| 84 | rtc_read (RTC_SECONDS_ALARM) ); |
| 85 | #endif |
| 86 | tmp->tm_sec = bcd2bin (sec & 0x7F); |
| 87 | tmp->tm_min = bcd2bin (min & 0x7F); |
| 88 | tmp->tm_hour = bcd2bin (hour & 0x3F); |
| 89 | tmp->tm_mday = bcd2bin (mday & 0x3F); |
| 90 | tmp->tm_mon = bcd2bin (mon & 0x1F); |
| 91 | tmp->tm_year = bcd2bin (year); |
| 92 | tmp->tm_wday = bcd2bin (wday & 0x07); |
| 93 | if(tmp->tm_year<70) |
| 94 | tmp->tm_year+=2000; |
| 95 | else |
| 96 | tmp->tm_year+=1900; |
| 97 | tmp->tm_yday = 0; |
| 98 | tmp->tm_isdst= 0; |
| 99 | #ifdef RTC_DEBUG |
| 100 | printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 101 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 102 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 103 | #endif |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 104 | |
| 105 | return 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 108 | int rtc_set (struct rtc_time *tmp) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 109 | { |
| 110 | #ifdef RTC_DEBUG |
| 111 | printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 112 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 113 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 114 | #endif |
| 115 | rtc_write(RTC_CONFIG_B,0x82); /* disables the RTC to update the regs */ |
| 116 | |
| 117 | rtc_write (RTC_YEAR, bin2bcd(tmp->tm_year % 100)); |
| 118 | rtc_write (RTC_MONTH, bin2bcd(tmp->tm_mon)); |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 119 | #ifdef CONFIG_AMIGAONEG3SE |
| 120 | rtc_write (RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday)+1); |
| 121 | #else |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 122 | rtc_write (RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday)); |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 123 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 124 | rtc_write (RTC_DATE_OF_MONTH, bin2bcd(tmp->tm_mday)); |
| 125 | rtc_write (RTC_HOURS, bin2bcd(tmp->tm_hour)); |
| 126 | rtc_write (RTC_MINUTES, bin2bcd(tmp->tm_min )); |
| 127 | rtc_write (RTC_SECONDS, bin2bcd(tmp->tm_sec )); |
| 128 | rtc_write(RTC_CONFIG_B,0x02); /* enables the RTC to update the regs */ |
| 129 | |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 130 | return 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void rtc_reset (void) |
| 134 | { |
| 135 | rtc_write(RTC_CONFIG_B,0x82); /* disables the RTC to update the regs */ |
| 136 | rtc_write(RTC_CONFIG_A,0x20); /* Normal OP */ |
| 137 | rtc_write(RTC_CONFIG_B,0x00); |
| 138 | rtc_write(RTC_CONFIG_B,0x00); |
| 139 | rtc_write(RTC_CONFIG_B,0x02); /* enables the RTC to update the regs */ |
| 140 | } |
| 141 | |
| 142 | /* ------------------------------------------------------------------------- */ |
| 143 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame^] | 144 | #ifdef CONFIG_SYS_RTC_REG_BASE_ADDR |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 145 | /* |
| 146 | * use direct memory access |
| 147 | */ |
| 148 | static uchar rtc_read (uchar reg) |
| 149 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame^] | 150 | return(in8(CONFIG_SYS_RTC_REG_BASE_ADDR+reg)); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static void rtc_write (uchar reg, uchar val) |
| 154 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame^] | 155 | out8(CONFIG_SYS_RTC_REG_BASE_ADDR+reg, val); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 156 | } |
| 157 | #else |
| 158 | static uchar rtc_read (uchar reg) |
| 159 | { |
| 160 | out8(RTC_PORT_MC146818,reg); |
| 161 | return(in8(RTC_PORT_MC146818+1)); |
| 162 | } |
| 163 | |
| 164 | static void rtc_write (uchar reg, uchar val) |
| 165 | { |
| 166 | out8(RTC_PORT_MC146818,reg); |
| 167 | out8(RTC_PORT_MC146818+1,val); |
| 168 | } |
| 169 | #endif |
| 170 | |
| 171 | static unsigned bcd2bin (uchar n) |
| 172 | { |
| 173 | return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F)); |
| 174 | } |
| 175 | |
| 176 | static unsigned char bin2bcd (unsigned int n) |
| 177 | { |
| 178 | return (((n / 10) << 4) | (n % 10)); |
| 179 | } |
| 180 | |
Jon Loeliger | 07efe2a | 2007-07-10 10:27:39 -0500 | [diff] [blame] | 181 | #endif |