Elyes HAOUAS | 081c4d5 | 2020-05-07 07:53:59 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | |
Simon Glass | 2cf99e1 | 2016-06-10 20:58:24 -0600 | [diff] [blame] | 3 | /* |
Simon Glass | 2cf99e1 | 2016-06-10 20:58:24 -0600 | [diff] [blame] | 4 | * From U-Boot 2016.05 |
| 5 | */ |
| 6 | |
| 7 | #include <console/console.h> |
| 8 | #include <rtc.h> |
| 9 | |
| 10 | #define FEBRUARY 2 |
| 11 | #define STARTOFTIME 1970 |
| 12 | #define SECDAY 86400L |
| 13 | #define SECYR (SECDAY * 365) |
Jakub Czapiga | 48b0079 | 2021-04-15 12:30:27 +0200 | [diff] [blame] | 14 | #define LEAP_YEAR(year) (((year) % 4 == 0 && (year) % 100 != 0) || (year) % 400 == 0) |
Simon Glass | 2cf99e1 | 2016-06-10 20:58:24 -0600 | [diff] [blame] | 15 | #define DAYS_IN_YEAR(a) (LEAP_YEAR(a) ? 366 : 365) |
| 16 | #define DAYS_IN_MONTH(a) (month_days[(a) - 1]) |
| 17 | |
Elyes HAOUAS | c92f5f2 | 2018-07-08 12:36:18 +0200 | [diff] [blame] | 18 | static const char *const weekdays[] = { |
Tim Wawrzynczak | b47c633 | 2019-02-21 15:02:26 -0700 | [diff] [blame] | 19 | "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur" |
Simon Glass | 2cf99e1 | 2016-06-10 20:58:24 -0600 | [diff] [blame] | 20 | }; |
| 21 | |
Tim Wawrzynczak | b47c633 | 2019-02-21 15:02:26 -0700 | [diff] [blame] | 22 | /* Zeller's rule */ |
Simon Glass | 2cf99e1 | 2016-06-10 20:58:24 -0600 | [diff] [blame] | 23 | static int rtc_calc_weekday(struct rtc_time *tm) |
| 24 | { |
Tim Wawrzynczak | b47c633 | 2019-02-21 15:02:26 -0700 | [diff] [blame] | 25 | /* In Zeller's rule, January and February are treated as if they |
| 26 | are months 13 and 14 of the previous year (March is still month 3) */ |
| 27 | const int zyear = ((tm->mon < 3) ? tm->year - 1 : tm->year); |
| 28 | const int q = tm->mday; |
| 29 | const int m = (tm->mon < 3) ? tm->mon + 12 : tm->mon; |
| 30 | const int K = zyear % 100; |
| 31 | const int J = zyear / 100; |
Simon Glass | 2cf99e1 | 2016-06-10 20:58:24 -0600 | [diff] [blame] | 32 | |
| 33 | /* |
Tim Wawrzynczak | b47c633 | 2019-02-21 15:02:26 -0700 | [diff] [blame] | 34 | * Because of the way the modulo operator works with negative numbers, |
| 35 | * the traditional formulation of Zeller's rule must be modified |
| 36 | * slightly to make the numerator positive (i.e., add 5J instead of |
| 37 | * subtracting 2J). Also subtract 1 so that Sunday is day 0. |
Simon Glass | 2cf99e1 | 2016-06-10 20:58:24 -0600 | [diff] [blame] | 38 | */ |
Tim Wawrzynczak | b47c633 | 2019-02-21 15:02:26 -0700 | [diff] [blame] | 39 | const int h = (q + (13 * (m + 1)) / 5 |
| 40 | + K + (K / 4) + (J / 4) + (5 * J) - 1) % 7; |
Simon Glass | 2cf99e1 | 2016-06-10 20:58:24 -0600 | [diff] [blame] | 41 | |
Tim Wawrzynczak | b47c633 | 2019-02-21 15:02:26 -0700 | [diff] [blame] | 42 | tm->wday = h; |
Simon Glass | 2cf99e1 | 2016-06-10 20:58:24 -0600 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | int rtc_to_tm(int tim, struct rtc_time *tm) |
| 47 | { |
| 48 | int month_days[12] = { |
Tim Wawrzynczak | b47c633 | 2019-02-21 15:02:26 -0700 | [diff] [blame] | 49 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
Simon Glass | 2cf99e1 | 2016-06-10 20:58:24 -0600 | [diff] [blame] | 50 | }; |
| 51 | register int i; |
| 52 | register long hms, day; |
| 53 | |
| 54 | day = tim / SECDAY; |
| 55 | hms = tim % SECDAY; |
| 56 | |
| 57 | /* Hours, minutes, seconds are easy */ |
| 58 | tm->hour = hms / 3600; |
| 59 | tm->min = (hms % 3600) / 60; |
| 60 | tm->sec = (hms % 3600) % 60; |
| 61 | |
| 62 | /* Number of years in days */ |
| 63 | for (i = STARTOFTIME; day >= DAYS_IN_YEAR(i); i++) |
| 64 | day -= DAYS_IN_YEAR(i); |
| 65 | tm->year = i; |
| 66 | |
| 67 | /* Number of months in days left */ |
| 68 | if (LEAP_YEAR(tm->year)) |
| 69 | DAYS_IN_MONTH(FEBRUARY) = 29; |
| 70 | for (i = 1; day >= DAYS_IN_MONTH(i); i++) |
| 71 | day -= DAYS_IN_MONTH(i); |
| 72 | DAYS_IN_MONTH(FEBRUARY) = 28; |
| 73 | tm->mon = i; |
| 74 | |
| 75 | /* Days are what is left over (+1) from all that */ |
| 76 | tm->mday = day + 1; |
| 77 | |
| 78 | /* Determine the day of week */ |
| 79 | return rtc_calc_weekday(tm); |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Converts Gregorian date to seconds since 1970-01-01 00:00:00. |
Tim Wawrzynczak | b47c633 | 2019-02-21 15:02:26 -0700 | [diff] [blame] | 84 | * Assumes input in normal date format, i.e. 1980-12-31 23:59:59 |
Simon Glass | 2cf99e1 | 2016-06-10 20:58:24 -0600 | [diff] [blame] | 85 | * => year=1980, mon=12, day=31, hour=23, min=59, sec=59. |
| 86 | * |
| 87 | * [For the Julian calendar (which was used in Russia before 1917, |
| 88 | * Britain & colonies before 1752, anywhere else before 1582, |
| 89 | * and is still in use by some communities) leave out the |
| 90 | * -year / 100 + year / 400 terms, and add 10.] |
| 91 | * |
| 92 | * This algorithm was first published by Gauss (I think). |
| 93 | * |
| 94 | * WARNING: this function will overflow on 2106-02-07 06:28:16 on |
| 95 | * machines where long is 32-bit! (However, as time_t is signed, we |
| 96 | * will already get problems at other places on 2038-01-19 03:14:08) |
| 97 | */ |
| 98 | unsigned long rtc_mktime(const struct rtc_time *tm) |
| 99 | { |
| 100 | int mon = tm->mon; |
| 101 | int year = tm->year; |
Tim Wawrzynczak | b47c633 | 2019-02-21 15:02:26 -0700 | [diff] [blame] | 102 | int days, hours; |
Simon Glass | 2cf99e1 | 2016-06-10 20:58:24 -0600 | [diff] [blame] | 103 | |
| 104 | mon -= 2; |
| 105 | if (0 >= (int)mon) { /* 1..12 -> 11, 12, 1..10 */ |
| 106 | mon += 12; /* Puts Feb last since it has leap day */ |
| 107 | year -= 1; |
| 108 | } |
| 109 | |
| 110 | days = (unsigned long)(year / 4 - year / 100 + year / 400 + |
Tim Wawrzynczak | b47c633 | 2019-02-21 15:02:26 -0700 | [diff] [blame] | 111 | 367 * mon / 12 + tm->mday) + |
| 112 | year * 365 - 719499; |
Simon Glass | 2cf99e1 | 2016-06-10 20:58:24 -0600 | [diff] [blame] | 113 | hours = days * 24 + tm->hour; |
| 114 | return (hours * 60 + tm->min) * 60 + tm->sec; |
| 115 | } |
| 116 | |
| 117 | void rtc_display(const struct rtc_time *tm) |
| 118 | { |
Tim Wawrzynczak | b47c633 | 2019-02-21 15:02:26 -0700 | [diff] [blame] | 119 | printk(BIOS_INFO, "Date: %5d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n", |
Simon Glass | 2cf99e1 | 2016-06-10 20:58:24 -0600 | [diff] [blame] | 120 | tm->year, tm->mon, tm->mday, |
| 121 | (tm->wday < 0 || tm->wday > 6) ? "unknown " : weekdays[tm->wday], |
| 122 | tm->hour, tm->min, tm->sec); |
| 123 | } |
Werner Zeh | 1724b74 | 2021-05-25 14:19:50 +0200 | [diff] [blame] | 124 | |
| 125 | static int rtc_month_days(unsigned int month, unsigned int year) |
| 126 | { |
| 127 | int month_days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
| 128 | |
Michał Żygowski | adf042f | 2024-02-29 11:50:58 +0100 | [diff] [blame] | 129 | return month_days[month] + (LEAP_YEAR(year) && month == 1); |
Werner Zeh | 1724b74 | 2021-05-25 14:19:50 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | int rtc_invalid(const struct rtc_time *tm) |
| 133 | { |
| 134 | if (tm->sec > 59 || tm->min > 59 || tm->hour > 23 || tm->mon == 0 || tm->mon > 12 || |
| 135 | tm->year < 1970 || tm->mday > rtc_month_days(tm->mon - 1, tm->year)) |
| 136 | return 1; |
| 137 | else |
| 138 | return 0; |
| 139 | } |