Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | 62f3ec7 | 2011-09-14 19:48:23 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2011 DENX Software Engineering GmbH |
| 4 | * Heiko Schocher <hs@denx.de> |
Heiko Schocher | 62f3ec7 | 2011-09-14 19:48:23 +0000 | [diff] [blame] | 5 | */ |
| 6 | #include <common.h> |
| 7 | #include <command.h> |
| 8 | #include <rtc.h> |
| 9 | #include <asm/io.h> |
Tom Rini | 56424eb | 2013-08-28 09:00:28 -0400 | [diff] [blame] | 10 | #include <asm/davinci_rtc.h> |
Heiko Schocher | 62f3ec7 | 2011-09-14 19:48:23 +0000 | [diff] [blame] | 11 | |
| 12 | #if defined(CONFIG_CMD_DATE) |
Heiko Schocher | 62f3ec7 | 2011-09-14 19:48:23 +0000 | [diff] [blame] | 13 | int rtc_get(struct rtc_time *tmp) |
| 14 | { |
Tom Rini | 9d02d3a | 2013-08-28 09:00:27 -0400 | [diff] [blame] | 15 | struct davinci_rtc *rtc = (struct davinci_rtc *)DAVINCI_RTC_BASE; |
Heiko Schocher | 62f3ec7 | 2011-09-14 19:48:23 +0000 | [diff] [blame] | 16 | unsigned long sec, min, hour, mday, wday, mon_cent, year; |
| 17 | unsigned long status; |
| 18 | |
| 19 | status = readl(&rtc->status); |
| 20 | if ((status & RTC_STATE_RUN) != RTC_STATE_RUN) { |
| 21 | printf("RTC doesn't run\n"); |
| 22 | return -1; |
| 23 | } |
| 24 | if ((status & RTC_STATE_BUSY) == RTC_STATE_BUSY) |
| 25 | udelay(20); |
| 26 | |
| 27 | sec = readl(&rtc->second); |
| 28 | min = readl(&rtc->minutes); |
| 29 | hour = readl(&rtc->hours); |
| 30 | mday = readl(&rtc->day); |
| 31 | wday = readl(&rtc->dotw); |
| 32 | mon_cent = readl(&rtc->month); |
| 33 | year = readl(&rtc->year); |
| 34 | |
| 35 | debug("Get RTC year: %02lx mon/cent: %02lx mday: %02lx wday: %02lx " |
| 36 | "hr: %02lx min: %02lx sec: %02lx\n", |
| 37 | year, mon_cent, mday, wday, |
| 38 | hour, min, sec); |
| 39 | |
| 40 | tmp->tm_sec = bcd2bin(sec & 0x7F); |
| 41 | tmp->tm_min = bcd2bin(min & 0x7F); |
| 42 | tmp->tm_hour = bcd2bin(hour & 0x3F); |
| 43 | tmp->tm_mday = bcd2bin(mday & 0x3F); |
| 44 | tmp->tm_mon = bcd2bin(mon_cent & 0x1F); |
| 45 | tmp->tm_year = bcd2bin(year) + 2000; |
| 46 | tmp->tm_wday = bcd2bin(wday & 0x07); |
| 47 | tmp->tm_yday = 0; |
| 48 | tmp->tm_isdst = 0; |
| 49 | |
| 50 | debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 51 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 52 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | int rtc_set(struct rtc_time *tmp) |
| 58 | { |
Tom Rini | 9d02d3a | 2013-08-28 09:00:27 -0400 | [diff] [blame] | 59 | struct davinci_rtc *rtc = (struct davinci_rtc *)DAVINCI_RTC_BASE; |
Heiko Schocher | 62f3ec7 | 2011-09-14 19:48:23 +0000 | [diff] [blame] | 60 | |
| 61 | debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 62 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 63 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 64 | writel(bin2bcd(tmp->tm_year % 100), &rtc->year); |
| 65 | writel(bin2bcd(tmp->tm_mon), &rtc->month); |
| 66 | |
| 67 | writel(bin2bcd(tmp->tm_wday), &rtc->dotw); |
| 68 | writel(bin2bcd(tmp->tm_mday), &rtc->day); |
| 69 | writel(bin2bcd(tmp->tm_hour), &rtc->hours); |
| 70 | writel(bin2bcd(tmp->tm_min), &rtc->minutes); |
| 71 | writel(bin2bcd(tmp->tm_sec), &rtc->second); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | void rtc_reset(void) |
| 76 | { |
Tom Rini | 9d02d3a | 2013-08-28 09:00:27 -0400 | [diff] [blame] | 77 | struct davinci_rtc *rtc = (struct davinci_rtc *)DAVINCI_RTC_BASE; |
Heiko Schocher | 62f3ec7 | 2011-09-14 19:48:23 +0000 | [diff] [blame] | 78 | |
| 79 | /* run RTC counter */ |
| 80 | writel(0x01, &rtc->ctrl); |
| 81 | } |
| 82 | #endif |