blob: 4afcdcc22c5ae1999766b94a5143b6f5f6a58715 [file] [log] [blame]
Gabe Blackabb001a2014-04-30 21:37:14 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2014 The Chromium OS Authors. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Gabe Blackabb001a2014-04-30 21:37:14 -070014 */
15
16#include <bcd.h>
17#include <console/console.h>
18#include <device/i2c.h>
19#include <rtc.h>
20#include <stdint.h>
21
22enum AS3722_RTC_REG
23{
24 AS3722_RTC_CONTROL = 0x60,
25 AS3722_RTC_SECOND = 0x61,
26 AS3722_RTC_MINUTE = 0x62,
27 AS3722_RTC_HOUR = 0x63,
28 AS3722_RTC_DAY = 0x64,
29 AS3722_RTC_MONTH = 0x65,
30 AS3722_RTC_YEAR = 0x66
31};
32
33enum {
34 AS3722_RTC_CONTROL_ON = 0x1 << 2
35};
36
37static uint8_t as3722_read(enum AS3722_RTC_REG reg)
38{
39 uint8_t val;
40 i2c_readb(CONFIG_DRIVERS_AS3722_RTC_BUS,
41 CONFIG_DRIVERS_AS3722_RTC_ADDR, reg, &val);
42 return val;
43}
44
45static void as3722_write(enum AS3722_RTC_REG reg, uint8_t val)
46{
47 i2c_writeb(CONFIG_DRIVERS_AS3722_RTC_BUS,
48 CONFIG_DRIVERS_AS3722_RTC_ADDR, reg, val);
49}
50
51static void as3722rtc_init(void)
52{
53 static int initialized;
54 if (initialized)
55 return;
56
57 uint8_t control = as3722_read(AS3722_RTC_CONTROL);
58 as3722_write(AS3722_RTC_CONTROL, control | AS3722_RTC_CONTROL_ON);
59
60 initialized = 1;
61}
62
63int rtc_set(const struct rtc_time *time)
64{
65 as3722rtc_init();
66
67 as3722_write(AS3722_RTC_SECOND, bin2bcd(time->sec));
68 as3722_write(AS3722_RTC_MINUTE, bin2bcd(time->min));
69 as3722_write(AS3722_RTC_HOUR, bin2bcd(time->hour));
70 as3722_write(AS3722_RTC_DAY, bin2bcd(time->mday));
Julius Werner90d0acb2014-12-30 18:38:06 -080071 as3722_write(AS3722_RTC_MONTH, bin2bcd(time->mon));
Gabe Blackabb001a2014-04-30 21:37:14 -070072 as3722_write(AS3722_RTC_YEAR, bin2bcd(time->year));
73 return 0;
74}
75
76int rtc_get(struct rtc_time *time)
77{
78 as3722rtc_init();
79
80 time->sec = bcd2bin(as3722_read(AS3722_RTC_SECOND) & 0x7f);
81 time->min = bcd2bin(as3722_read(AS3722_RTC_MINUTE) & 0x7f);
82 time->hour = bcd2bin(as3722_read(AS3722_RTC_HOUR) & 0x3f);
83 time->mday = bcd2bin(as3722_read(AS3722_RTC_DAY) & 0x3f);
Julius Werner90d0acb2014-12-30 18:38:06 -080084 time->mon = bcd2bin(as3722_read(AS3722_RTC_MONTH) & 0x1f);
Gabe Blackabb001a2014-04-30 21:37:14 -070085 time->year = bcd2bin(as3722_read(AS3722_RTC_YEAR) & 0x7f);
86 return 0;
87}