Gabe Black | abb001a | 2014-04-30 21:37:14 -0700 | [diff] [blame] | 1 | /* |
| 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. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <bcd.h> |
| 21 | #include <console/console.h> |
| 22 | #include <device/i2c.h> |
| 23 | #include <rtc.h> |
| 24 | #include <stdint.h> |
| 25 | |
| 26 | enum AS3722_RTC_REG |
| 27 | { |
| 28 | AS3722_RTC_CONTROL = 0x60, |
| 29 | AS3722_RTC_SECOND = 0x61, |
| 30 | AS3722_RTC_MINUTE = 0x62, |
| 31 | AS3722_RTC_HOUR = 0x63, |
| 32 | AS3722_RTC_DAY = 0x64, |
| 33 | AS3722_RTC_MONTH = 0x65, |
| 34 | AS3722_RTC_YEAR = 0x66 |
| 35 | }; |
| 36 | |
| 37 | enum { |
| 38 | AS3722_RTC_CONTROL_ON = 0x1 << 2 |
| 39 | }; |
| 40 | |
| 41 | static uint8_t as3722_read(enum AS3722_RTC_REG reg) |
| 42 | { |
| 43 | uint8_t val; |
| 44 | i2c_readb(CONFIG_DRIVERS_AS3722_RTC_BUS, |
| 45 | CONFIG_DRIVERS_AS3722_RTC_ADDR, reg, &val); |
| 46 | return val; |
| 47 | } |
| 48 | |
| 49 | static void as3722_write(enum AS3722_RTC_REG reg, uint8_t val) |
| 50 | { |
| 51 | i2c_writeb(CONFIG_DRIVERS_AS3722_RTC_BUS, |
| 52 | CONFIG_DRIVERS_AS3722_RTC_ADDR, reg, val); |
| 53 | } |
| 54 | |
| 55 | static void as3722rtc_init(void) |
| 56 | { |
| 57 | static int initialized; |
| 58 | if (initialized) |
| 59 | return; |
| 60 | |
| 61 | uint8_t control = as3722_read(AS3722_RTC_CONTROL); |
| 62 | as3722_write(AS3722_RTC_CONTROL, control | AS3722_RTC_CONTROL_ON); |
| 63 | |
| 64 | initialized = 1; |
| 65 | } |
| 66 | |
| 67 | int rtc_set(const struct rtc_time *time) |
| 68 | { |
| 69 | as3722rtc_init(); |
| 70 | |
| 71 | as3722_write(AS3722_RTC_SECOND, bin2bcd(time->sec)); |
| 72 | as3722_write(AS3722_RTC_MINUTE, bin2bcd(time->min)); |
| 73 | as3722_write(AS3722_RTC_HOUR, bin2bcd(time->hour)); |
| 74 | as3722_write(AS3722_RTC_DAY, bin2bcd(time->mday)); |
| 75 | as3722_write(AS3722_RTC_MONTH, bin2bcd(time->mon + 1)); |
| 76 | as3722_write(AS3722_RTC_YEAR, bin2bcd(time->year)); |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | int rtc_get(struct rtc_time *time) |
| 81 | { |
| 82 | as3722rtc_init(); |
| 83 | |
| 84 | time->sec = bcd2bin(as3722_read(AS3722_RTC_SECOND) & 0x7f); |
| 85 | time->min = bcd2bin(as3722_read(AS3722_RTC_MINUTE) & 0x7f); |
| 86 | time->hour = bcd2bin(as3722_read(AS3722_RTC_HOUR) & 0x3f); |
| 87 | time->mday = bcd2bin(as3722_read(AS3722_RTC_DAY) & 0x3f); |
| 88 | time->mon = bcd2bin(as3722_read(AS3722_RTC_MONTH) & 0x1f) - 1; |
| 89 | time->year = bcd2bin(as3722_read(AS3722_RTC_YEAR) & 0x7f); |
| 90 | return 0; |
| 91 | } |