blob: 9cd00b525c11dcfeca92c513325befb4715fa677 [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001#ifndef PC80_MC146818RTC_H
2#define PC80_MC146818RTC_H
3
Julius Wernercd49cce2019-03-05 16:53:33 -08004#if CONFIG(ARCH_X86)
Edward O'Callaghanc5fff752014-06-24 16:29:12 +10005
Edward O'Callaghan502c3db2014-06-21 23:02:46 +10006#include <arch/io.h>
Alexandru Gagniucd7134e02013-11-23 18:54:44 -06007#include <types.h>
8
Eric Biederman8ca8d762003-04-22 19:02:15 +00009#ifndef RTC_BASE_PORT
10#define RTC_BASE_PORT 0x70
11#endif
12
13#define RTC_PORT(x) (RTC_BASE_PORT + (x))
14
Eric Biederman8ca8d762003-04-22 19:02:15 +000015/* control registers - Moto names
16 */
17#define RTC_REG_A 10
18#define RTC_REG_B 11
19#define RTC_REG_C 12
20#define RTC_REG_D 13
21
22
23/**********************************************************************
24 * register details
25 **********************************************************************/
26#define RTC_FREQ_SELECT RTC_REG_A
27
28/* update-in-progress - set to "1" 244 microsecs before RTC goes off the bus,
29 * reset after update (may take 1.984ms @ 32768Hz RefClock) is complete,
Martin Roth0cb07e32013-07-09 21:46:01 -060030 * totaling to a max high interval of 2.228 ms.
Eric Biederman8ca8d762003-04-22 19:02:15 +000031 */
32# define RTC_UIP 0x80
33# define RTC_DIV_CTL 0x70
34 /* divider control: refclock values 4.194 / 1.049 MHz / 32.768 kHz */
35# define RTC_REF_CLCK_4MHZ 0x00
36# define RTC_REF_CLCK_1MHZ 0x10
37# define RTC_REF_CLCK_32KHZ 0x20
38 /* 2 values for divider stage reset, others for "testing purposes only" */
39# define RTC_DIV_RESET1 0x60
40# define RTC_DIV_RESET2 0x70
Lee Leahy6a566d72017-03-07 17:45:12 -080041 /* Periodic intr. / Square wave rate select. 0 = none,
42 * 1 = 32.8kHz,... 15 = 2Hz
43 */
Lee Leahy84d20d02017-03-07 15:00:18 -080044# define RTC_RATE_SELECT 0x0F
Eric Biederman8ca8d762003-04-22 19:02:15 +000045# define RTC_RATE_NONE 0x00
46# define RTC_RATE_32786HZ 0x01
47# define RTC_RATE_16384HZ 0x02
48# define RTC_RATE_8192HZ 0x03
49# define RTC_RATE_4096HZ 0x04
50# define RTC_RATE_2048HZ 0x05
51# define RTC_RATE_1024HZ 0x06
52# define RTC_RATE_512HZ 0x07
53# define RTC_RATE_256HZ 0x08
54# define RTC_RATE_128HZ 0x09
55# define RTC_RATE_64HZ 0x0a
56# define RTC_RATE_32HZ 0x0b
57# define RTC_RATE_16HZ 0x0c
58# define RTC_RATE_8HZ 0x0d
59# define RTC_RATE_4HZ 0x0e
60# define RTC_RATE_2HZ 0x0f
61
62/**********************************************************************/
63#define RTC_CONTROL RTC_REG_B
64# define RTC_SET 0x80 /* disable updates for clock setting */
65# define RTC_PIE 0x40 /* periodic interrupt enable */
66# define RTC_AIE 0x20 /* alarm interrupt enable */
67# define RTC_UIE 0x10 /* update-finished interrupt enable */
68# define RTC_SQWE 0x08 /* enable square-wave output */
69# define RTC_DM_BINARY 0x04 /* all time/date values are BCD if clear */
70# define RTC_24H 0x02 /* 24 hour mode - else hours bit 7 means pm */
71# define RTC_DST_EN 0x01 /* auto switch DST - works f. USA only */
72
73/**********************************************************************/
74#define RTC_INTR_FLAGS RTC_REG_C
75/* caution - cleared by read */
76# define RTC_IRQF 0x80 /* any of the following 3 is active */
77# define RTC_PF 0x40
78# define RTC_AF 0x20
79# define RTC_UF 0x10
80
81/**********************************************************************/
82#define RTC_VALID RTC_REG_D
83# define RTC_VRT 0x80 /* valid RAM and time */
84/**********************************************************************/
85
Duncan Lauriec8c836f2012-06-23 13:22:25 -070086/* Date and Time in RTC CMOS */
87#define RTC_CLK_SECOND 0
88#define RTC_CLK_SECOND_ALARM 1
89#define RTC_CLK_MINUTE 2
90#define RTC_CLK_MINUTE_ALARM 3
91#define RTC_CLK_HOUR 4
92#define RTC_CLK_HOUR_ALARM 5
93#define RTC_CLK_DAYOFWEEK 6
94#define RTC_CLK_DAYOFMONTH 7
95#define RTC_CLK_MONTH 8
96#define RTC_CLK_YEAR 9
zbaoa1e6a9c2012-08-02 19:02:26 +080097#define RTC_CLK_ALTCENTURY 0x32
98
Eric Biederman8ca8d762003-04-22 19:02:15 +000099/* On PCs, the checksum is built only over bytes 16..45 */
100#define PC_CKS_RANGE_START 16
101#define PC_CKS_RANGE_END 45
102#define PC_CKS_LOC 46
103
Kyösti Mälkkia5811662020-01-04 22:07:37 +0200104/* Tracking of fallback/normal boot. */
105#define RTC_BOOT_BYTE 48
106#define RTC_BOOT_NORMAL 0x1
107
Edwin Beasanteb50c7d2010-07-06 21:05:04 +0000108static inline unsigned char cmos_read(unsigned char addr)
109{
110 int offs = 0;
111 if (addr >= 128) {
112 offs = 2;
113 addr -= 128;
114 }
115 outb(addr, RTC_BASE_PORT + offs + 0);
116 return inb(RTC_BASE_PORT + offs + 1);
117}
118
Patrick Georgif9439012012-11-15 14:54:05 +0100119static inline void cmos_write_inner(unsigned char val, unsigned char addr)
Edwin Beasanteb50c7d2010-07-06 21:05:04 +0000120{
121 int offs = 0;
122 if (addr >= 128) {
123 offs = 2;
124 addr -= 128;
125 }
126 outb(addr, RTC_BASE_PORT + offs + 0);
127 outb(val, RTC_BASE_PORT + offs + 1);
128}
Duncan Laurie654f2932011-09-26 13:24:40 -0700129
Kyösti Mälkki1a9b7b52020-01-04 17:38:17 +0200130static inline u8 cmos_disable_rtc(void)
Patrick Georgif9439012012-11-15 14:54:05 +0100131{
132 u8 control_state = cmos_read(RTC_CONTROL);
Kyösti Mälkki1a9b7b52020-01-04 17:38:17 +0200133 if (!(control_state & RTC_SET))
Patrick Georgif9439012012-11-15 14:54:05 +0100134 cmos_write_inner(control_state | RTC_SET, RTC_CONTROL);
Kyösti Mälkki1a9b7b52020-01-04 17:38:17 +0200135 return control_state;
136}
137
138static inline void cmos_restore_rtc(u8 control_state)
139{
140 if (!(control_state & RTC_SET))
Patrick Georgif9439012012-11-15 14:54:05 +0100141 cmos_write_inner(control_state, RTC_CONTROL);
Patrick Georgif9439012012-11-15 14:54:05 +0100142}
143
Kyösti Mälkki1a9b7b52020-01-04 17:38:17 +0200144static inline void cmos_write(unsigned char val, unsigned char addr)
Patrick Georgif9439012012-11-15 14:54:05 +0100145{
Kyösti Mälkki1a9b7b52020-01-04 17:38:17 +0200146 u8 control_state;
Patrick Georgif9439012012-11-15 14:54:05 +0100147
Kyösti Mälkki1a9b7b52020-01-04 17:38:17 +0200148 /*
149 * There are various places where RTC bits might be hiding,
150 * eg. the Century / AltCentury byte. So to be safe, disable
151 * RTC before changing any value.
152 */
153 if (addr != RTC_CONTROL)
154 control_state = cmos_disable_rtc();
155 cmos_write_inner(val, addr);
156 if (addr != RTC_CONTROL)
157 cmos_restore_rtc(control_state);
Patrick Georgif9439012012-11-15 14:54:05 +0100158}
159
Duncan Laurie654f2932011-09-26 13:24:40 -0700160static inline u32 cmos_read32(u8 offset)
161{
162 u32 value = 0;
163 u8 i;
164 for (i = 0; i < sizeof(value); ++i)
165 value |= cmos_read(offset + i) << (i << 3);
166 return value;
167}
168
Kyösti Mälkki28791072020-01-04 12:58:53 +0200169static inline void cmos_write32(u32 value, u8 offset)
Duncan Laurie654f2932011-09-26 13:24:40 -0700170{
171 u8 i;
172 for (i = 0; i < sizeof(value); ++i)
173 cmos_write((value >> (i << 3)) & 0xff, offset + i);
174}
Edwin Beasanteb50c7d2010-07-06 21:05:04 +0000175
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600176void cmos_init(bool invalid);
Gabe Black03abaee212014-04-30 21:31:44 -0700177void cmos_check_update_date(void);
Kyösti Mälkki836b8d22019-12-16 06:54:11 +0200178int cmos_error(void);
Kyösti Mälkki2a0e3b22020-01-04 18:04:39 +0200179int cmos_lb_cks_valid(void);
180
181int cmos_checksum_valid(int range_start, int range_end, int cks_loc);
182void cmos_set_checksum(int range_start, int range_end, int cks_loc);
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600183
Julius Wernercd49cce2019-03-05 16:53:33 -0800184#if CONFIG(CMOS_POST)
185#if CONFIG(USE_OPTION_TABLE)
Duncan Laurieb6e97b12012-09-09 19:09:56 -0700186# include "option_table.h"
187# define CMOS_POST_OFFSET (CMOS_VSTART_cmos_post_offset >> 3)
188#else
Elyes HAOUASb58e99d2019-01-23 12:04:43 +0100189# if (CONFIG_CMOS_POST_OFFSET != 0)
Duncan Laurieb6e97b12012-09-09 19:09:56 -0700190# define CMOS_POST_OFFSET CONFIG_CMOS_POST_OFFSET
191# else
Martin Roth46cf9f72015-07-11 13:56:58 -0600192# error "Must configure CONFIG_CMOS_POST_OFFSET"
Duncan Laurieb6e97b12012-09-09 19:09:56 -0700193# endif
194#endif
195
Duncan Lauried5686fe2013-06-10 10:21:41 -0700196/*
197 * 0 = Bank Select Magic
198 * 1 = Bank 0 POST
199 * 2 = Bank 1 POST
200 * 3-6 = BANK 0 Extra log
201 * 7-10 = BANK 1 Extra log
202 */
Duncan Laurieb6e97b12012-09-09 19:09:56 -0700203#define CMOS_POST_BANK_OFFSET (CMOS_POST_OFFSET)
204#define CMOS_POST_BANK_0_MAGIC 0x80
205#define CMOS_POST_BANK_0_OFFSET (CMOS_POST_OFFSET + 1)
Duncan Lauried5686fe2013-06-10 10:21:41 -0700206#define CMOS_POST_BANK_0_EXTRA (CMOS_POST_OFFSET + 3)
Duncan Laurieb6e97b12012-09-09 19:09:56 -0700207#define CMOS_POST_BANK_1_MAGIC 0x81
208#define CMOS_POST_BANK_1_OFFSET (CMOS_POST_OFFSET + 2)
Duncan Lauried5686fe2013-06-10 10:21:41 -0700209#define CMOS_POST_BANK_1_EXTRA (CMOS_POST_OFFSET + 7)
Duncan Laurie1fc34612012-09-09 19:14:45 -0700210
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700211#define CMOS_POST_EXTRA_DEV_PATH 0x01
212
Duncan Laurie1fc34612012-09-09 19:14:45 -0700213void cmos_post_log(void);
Edward O'Callaghan5c971422014-04-15 14:32:53 +1000214#else
215static inline void cmos_post_log(void) {}
Duncan Laurieb6e97b12012-09-09 19:09:56 -0700216#endif /* CONFIG_CMOS_POST */
217
Edward O'Callaghanc5fff752014-06-24 16:29:12 +1000218#endif /* CONFIG_ARCH_X86 */
219
Kyösti Mälkki731e58e2020-01-04 13:00:02 +0200220void sanitize_cmos(void);
Kyösti Mälkkibb5b9fe2020-01-04 13:00:02 +0200221void cmos_post_init(void);
222
Eric Biederman8ca8d762003-04-22 19:02:15 +0000223#endif /* PC80_MC146818RTC_H */