blob: f0d58d82d64417fbd52b9c1b8bb1b8ed554d48f7 [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001#ifndef PC80_MC146818RTC_H
2#define PC80_MC146818RTC_H
3
4#ifndef RTC_BASE_PORT
5#define RTC_BASE_PORT 0x70
6#endif
7
8#define RTC_PORT(x) (RTC_BASE_PORT + (x))
9
Eric Biederman8ca8d762003-04-22 19:02:15 +000010/* control registers - Moto names
11 */
12#define RTC_REG_A 10
13#define RTC_REG_B 11
14#define RTC_REG_C 12
15#define RTC_REG_D 13
16
17
18/**********************************************************************
19 * register details
20 **********************************************************************/
21#define RTC_FREQ_SELECT RTC_REG_A
22
23/* update-in-progress - set to "1" 244 microsecs before RTC goes off the bus,
24 * reset after update (may take 1.984ms @ 32768Hz RefClock) is complete,
25 * totalling to a max high interval of 2.228 ms.
26 */
27# define RTC_UIP 0x80
28# define RTC_DIV_CTL 0x70
29 /* divider control: refclock values 4.194 / 1.049 MHz / 32.768 kHz */
30# define RTC_REF_CLCK_4MHZ 0x00
31# define RTC_REF_CLCK_1MHZ 0x10
32# define RTC_REF_CLCK_32KHZ 0x20
33 /* 2 values for divider stage reset, others for "testing purposes only" */
34# define RTC_DIV_RESET1 0x60
35# define RTC_DIV_RESET2 0x70
36 /* Periodic intr. / Square wave rate select. 0=none, 1=32.8kHz,... 15=2Hz */
37# define RTC_RATE_SELECT 0x0F
38# define RTC_RATE_NONE 0x00
39# define RTC_RATE_32786HZ 0x01
40# define RTC_RATE_16384HZ 0x02
41# define RTC_RATE_8192HZ 0x03
42# define RTC_RATE_4096HZ 0x04
43# define RTC_RATE_2048HZ 0x05
44# define RTC_RATE_1024HZ 0x06
45# define RTC_RATE_512HZ 0x07
46# define RTC_RATE_256HZ 0x08
47# define RTC_RATE_128HZ 0x09
48# define RTC_RATE_64HZ 0x0a
49# define RTC_RATE_32HZ 0x0b
50# define RTC_RATE_16HZ 0x0c
51# define RTC_RATE_8HZ 0x0d
52# define RTC_RATE_4HZ 0x0e
53# define RTC_RATE_2HZ 0x0f
54
55/**********************************************************************/
56#define RTC_CONTROL RTC_REG_B
57# define RTC_SET 0x80 /* disable updates for clock setting */
58# define RTC_PIE 0x40 /* periodic interrupt enable */
59# define RTC_AIE 0x20 /* alarm interrupt enable */
60# define RTC_UIE 0x10 /* update-finished interrupt enable */
61# define RTC_SQWE 0x08 /* enable square-wave output */
62# define RTC_DM_BINARY 0x04 /* all time/date values are BCD if clear */
63# define RTC_24H 0x02 /* 24 hour mode - else hours bit 7 means pm */
64# define RTC_DST_EN 0x01 /* auto switch DST - works f. USA only */
65
Duncan Laurie5c103aa2012-08-16 16:06:03 -070066#define RTC_TO_BCD(value) (((value / 10) << 4) | (value % 10))
67
Eric Biederman8ca8d762003-04-22 19:02:15 +000068/**********************************************************************/
69#define RTC_INTR_FLAGS RTC_REG_C
70/* caution - cleared by read */
71# define RTC_IRQF 0x80 /* any of the following 3 is active */
72# define RTC_PF 0x40
73# define RTC_AF 0x20
74# define RTC_UF 0x10
75
76/**********************************************************************/
77#define RTC_VALID RTC_REG_D
78# define RTC_VRT 0x80 /* valid RAM and time */
79/**********************************************************************/
80
Duncan Lauriec8c836f2012-06-23 13:22:25 -070081/* Date and Time in RTC CMOS */
82#define RTC_CLK_SECOND 0
83#define RTC_CLK_SECOND_ALARM 1
84#define RTC_CLK_MINUTE 2
85#define RTC_CLK_MINUTE_ALARM 3
86#define RTC_CLK_HOUR 4
87#define RTC_CLK_HOUR_ALARM 5
88#define RTC_CLK_DAYOFWEEK 6
89#define RTC_CLK_DAYOFMONTH 7
90#define RTC_CLK_MONTH 8
91#define RTC_CLK_YEAR 9
zbaoa1e6a9c2012-08-02 19:02:26 +080092#define RTC_CLK_ALTCENTURY 0x32
93
94#define RTC_HAS_ALTCENTURY 1
95#define RTC_HAS_NO_ALTCENTURY 0
Duncan Lauriec8c836f2012-06-23 13:22:25 -070096
Eric Biederman8ca8d762003-04-22 19:02:15 +000097/* On PCs, the checksum is built only over bytes 16..45 */
98#define PC_CKS_RANGE_START 16
99#define PC_CKS_RANGE_END 45
100#define PC_CKS_LOC 46
101
Edwin Beasanteb50c7d2010-07-06 21:05:04 +0000102#ifndef UTIL_BUILD_OPTION_TABLE
103#include <arch/io.h>
104static inline unsigned char cmos_read(unsigned char addr)
105{
106 int offs = 0;
107 if (addr >= 128) {
108 offs = 2;
109 addr -= 128;
110 }
111 outb(addr, RTC_BASE_PORT + offs + 0);
112 return inb(RTC_BASE_PORT + offs + 1);
113}
114
115static inline void cmos_write(unsigned char val, unsigned char addr)
116{
117 int offs = 0;
118 if (addr >= 128) {
119 offs = 2;
120 addr -= 128;
121 }
122 outb(addr, RTC_BASE_PORT + offs + 0);
123 outb(val, RTC_BASE_PORT + offs + 1);
124}
Duncan Laurie654f2932011-09-26 13:24:40 -0700125
126static inline u32 cmos_read32(u8 offset)
127{
128 u32 value = 0;
129 u8 i;
130 for (i = 0; i < sizeof(value); ++i)
131 value |= cmos_read(offset + i) << (i << 3);
132 return value;
133}
134
135static inline void cmos_write32(u8 offset, u32 value)
136{
137 u8 i;
138 for (i = 0; i < sizeof(value); ++i)
139 cmos_write((value >> (i << 3)) & 0xff, offset + i);
140}
Edwin Beasanteb50c7d2010-07-06 21:05:04 +0000141#endif
142
143#if !defined(__ROMCC__)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000144void rtc_init(int invalid);
zbaoa1e6a9c2012-08-02 19:02:26 +0800145void rtc_check_update_cmos_date(u8 has_century);
Edwin Beasanteb50c7d2010-07-06 21:05:04 +0000146#if CONFIG_USE_OPTION_TABLE
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200147int set_option(const char *name, void *val);
Myles Watson3fe6b702009-10-09 20:13:43 +0000148int get_option(void *dest, const char *name);
Patrick Georgib2517532011-05-10 21:53:13 +0000149unsigned read_option_lowlevel(unsigned start, unsigned size, unsigned def);
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000150#else
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200151static inline int set_option(const char *name __attribute__((unused)), void *val __attribute__((unused))) { return -2; };
Stefan Reinauerb5828d72010-03-29 17:14:28 +0000152static inline int get_option(void *dest __attribute__((unused)),
153 const char *name __attribute__((unused))) { return -2; }
Patrick Georgia27561c2011-11-22 10:27:24 +0100154#define read_option_lowlevel(start, size, def) def
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000155#endif
Edwin Beasanteb50c7d2010-07-06 21:05:04 +0000156#else
Stefan Reinauerae5e11d2012-04-27 02:31:28 +0200157#include <drivers/pc80/mc146818rtc_early.c>
Eric Biederman8ca8d762003-04-22 19:02:15 +0000158#endif
Patrick Georgib2517532011-05-10 21:53:13 +0000159#define read_option(name, default) read_option_lowlevel(CMOS_VSTART_ ##name, CMOS_VLEN_ ##name, (default))
Eric Biederman8ca8d762003-04-22 19:02:15 +0000160
Duncan Laurieb6e97b12012-09-09 19:09:56 -0700161#if CONFIG_CMOS_POST
162#if CONFIG_USE_OPTION_TABLE
163# include "option_table.h"
164# define CMOS_POST_OFFSET (CMOS_VSTART_cmos_post_offset >> 3)
165#else
166# if defined(CONFIG_CMOS_POST_OFFSET)
167# define CMOS_POST_OFFSET CONFIG_CMOS_POST_OFFSET
168# else
169# error "Must define CONFIG_CMOS_POST_OFFSET"
170# endif
171#endif
172
173#define CMOS_POST_BANK_OFFSET (CMOS_POST_OFFSET)
174#define CMOS_POST_BANK_0_MAGIC 0x80
175#define CMOS_POST_BANK_0_OFFSET (CMOS_POST_OFFSET + 1)
176#define CMOS_POST_BANK_1_MAGIC 0x81
177#define CMOS_POST_BANK_1_OFFSET (CMOS_POST_OFFSET + 2)
Duncan Laurie1fc34612012-09-09 19:14:45 -0700178
179#if !defined(__ROMCC__)
180void cmos_post_log(void);
181#endif
Duncan Laurieb6e97b12012-09-09 19:09:56 -0700182#endif /* CONFIG_CMOS_POST */
183
Eric Biederman8ca8d762003-04-22 19:02:15 +0000184#endif /* PC80_MC146818RTC_H */