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> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Heiko Schocher | 62f3ec7 | 2011-09-14 19:48:23 +0000 | [diff] [blame] | 9 | #include <rtc.h> |
| 10 | #include <asm/io.h> |
Tom Rini | 56424eb | 2013-08-28 09:00:28 -0400 | [diff] [blame] | 11 | #include <asm/davinci_rtc.h> |
Dario Binacchi | d898c8b | 2021-06-02 22:37:59 +0200 | [diff] [blame] | 12 | #include <asm/arch/hardware.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 13 | #include <linux/delay.h> |
Heiko Schocher | 62f3ec7 | 2011-09-14 19:48:23 +0000 | [diff] [blame] | 14 | |
Dario Binacchi | d898c8b | 2021-06-02 22:37:59 +0200 | [diff] [blame] | 15 | #if !defined(RTC_BASE) && defined(DAVINCI_RTC_BASE) |
| 16 | #define RTC_BASE DAVINCI_RTC_BASE |
| 17 | #endif |
| 18 | |
Dario Binacchi | 64a7a68 | 2021-06-02 22:38:01 +0200 | [diff] [blame^] | 19 | static int davinci_rtc_wait_not_busy(struct davinci_rtc *rtc) |
Heiko Schocher | 62f3ec7 | 2011-09-14 19:48:23 +0000 | [diff] [blame] | 20 | { |
Dario Binacchi | 64a7a68 | 2021-06-02 22:38:01 +0200 | [diff] [blame^] | 21 | int count; |
| 22 | u8 status; |
Heiko Schocher | 62f3ec7 | 2011-09-14 19:48:23 +0000 | [diff] [blame] | 23 | |
Dario Binacchi | 0eb3a62 | 2021-06-02 22:38:00 +0200 | [diff] [blame] | 24 | status = readb(&rtc->status); |
Heiko Schocher | 62f3ec7 | 2011-09-14 19:48:23 +0000 | [diff] [blame] | 25 | if ((status & RTC_STATE_RUN) != RTC_STATE_RUN) { |
| 26 | printf("RTC doesn't run\n"); |
| 27 | return -1; |
| 28 | } |
Dario Binacchi | 64a7a68 | 2021-06-02 22:38:01 +0200 | [diff] [blame^] | 29 | |
| 30 | /* BUSY may stay active for 1/32768 second (~30 usec) */ |
| 31 | for (count = 0; count < 50; count++) { |
| 32 | if (!(status & RTC_STATE_BUSY)) |
| 33 | break; |
| 34 | |
| 35 | udelay(1); |
| 36 | status = readb(&rtc->status); |
| 37 | } |
| 38 | |
| 39 | /* now we have ~15 usec to read/write various registers */ |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | int rtc_get(struct rtc_time *tmp) |
| 44 | { |
| 45 | struct davinci_rtc *rtc = (struct davinci_rtc *)RTC_BASE; |
| 46 | unsigned long sec, min, hour, mday, wday, mon_cent, year; |
| 47 | int ret; |
| 48 | |
| 49 | ret = davinci_rtc_wait_not_busy(rtc); |
| 50 | if (ret) |
| 51 | return ret; |
Heiko Schocher | 62f3ec7 | 2011-09-14 19:48:23 +0000 | [diff] [blame] | 52 | |
Dario Binacchi | 0eb3a62 | 2021-06-02 22:38:00 +0200 | [diff] [blame] | 53 | sec = readb(&rtc->second); |
| 54 | min = readb(&rtc->minutes); |
| 55 | hour = readb(&rtc->hours); |
| 56 | mday = readb(&rtc->day); |
| 57 | wday = readb(&rtc->dotw); |
| 58 | mon_cent = readb(&rtc->month); |
| 59 | year = readb(&rtc->year); |
Heiko Schocher | 62f3ec7 | 2011-09-14 19:48:23 +0000 | [diff] [blame] | 60 | |
| 61 | debug("Get RTC year: %02lx mon/cent: %02lx mday: %02lx wday: %02lx " |
| 62 | "hr: %02lx min: %02lx sec: %02lx\n", |
| 63 | year, mon_cent, mday, wday, |
| 64 | hour, min, sec); |
| 65 | |
| 66 | tmp->tm_sec = bcd2bin(sec & 0x7F); |
| 67 | tmp->tm_min = bcd2bin(min & 0x7F); |
| 68 | tmp->tm_hour = bcd2bin(hour & 0x3F); |
| 69 | tmp->tm_mday = bcd2bin(mday & 0x3F); |
| 70 | tmp->tm_mon = bcd2bin(mon_cent & 0x1F); |
| 71 | tmp->tm_year = bcd2bin(year) + 2000; |
| 72 | tmp->tm_wday = bcd2bin(wday & 0x07); |
| 73 | tmp->tm_yday = 0; |
| 74 | tmp->tm_isdst = 0; |
| 75 | |
| 76 | debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 77 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 78 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | int rtc_set(struct rtc_time *tmp) |
| 84 | { |
Dario Binacchi | d898c8b | 2021-06-02 22:37:59 +0200 | [diff] [blame] | 85 | struct davinci_rtc *rtc = (struct davinci_rtc *)RTC_BASE; |
Dario Binacchi | 64a7a68 | 2021-06-02 22:38:01 +0200 | [diff] [blame^] | 86 | int ret; |
Heiko Schocher | 62f3ec7 | 2011-09-14 19:48:23 +0000 | [diff] [blame] | 87 | |
Dario Binacchi | 64a7a68 | 2021-06-02 22:38:01 +0200 | [diff] [blame^] | 88 | ret = davinci_rtc_wait_not_busy(rtc); |
| 89 | if (ret) |
| 90 | return ret; |
| 91 | |
Dario Binacchi | 0eb3a62 | 2021-06-02 22:38:00 +0200 | [diff] [blame] | 92 | writeb(bin2bcd(tmp->tm_year % 100), &rtc->year); |
| 93 | writeb(bin2bcd(tmp->tm_mon), &rtc->month); |
Heiko Schocher | 62f3ec7 | 2011-09-14 19:48:23 +0000 | [diff] [blame] | 94 | |
Dario Binacchi | 0eb3a62 | 2021-06-02 22:38:00 +0200 | [diff] [blame] | 95 | writeb(bin2bcd(tmp->tm_wday), &rtc->dotw); |
| 96 | writeb(bin2bcd(tmp->tm_mday), &rtc->day); |
| 97 | writeb(bin2bcd(tmp->tm_hour), &rtc->hours); |
| 98 | writeb(bin2bcd(tmp->tm_min), &rtc->minutes); |
| 99 | writeb(bin2bcd(tmp->tm_sec), &rtc->second); |
Dario Binacchi | 64a7a68 | 2021-06-02 22:38:01 +0200 | [diff] [blame^] | 100 | |
| 101 | debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 102 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 103 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 104 | |
Heiko Schocher | 62f3ec7 | 2011-09-14 19:48:23 +0000 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | void rtc_reset(void) |
| 109 | { |
Dario Binacchi | d898c8b | 2021-06-02 22:37:59 +0200 | [diff] [blame] | 110 | struct davinci_rtc *rtc = (struct davinci_rtc *)RTC_BASE; |
Heiko Schocher | 62f3ec7 | 2011-09-14 19:48:23 +0000 | [diff] [blame] | 111 | |
| 112 | /* run RTC counter */ |
Dario Binacchi | 0eb3a62 | 2021-06-02 22:38:00 +0200 | [diff] [blame] | 113 | writeb(0x01, &rtc->ctrl); |
Heiko Schocher | 62f3ec7 | 2011-09-14 19:48:23 +0000 | [diff] [blame] | 114 | } |