Mike Frysinger | 6742eaf | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2004-2008 Analog Devices Inc. |
| 3 | * |
| 4 | * (C) Copyright 2001 |
| 5 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 6 | * |
| 7 | * Licensed under the GPL-2 or later. |
| 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <command.h> |
| 12 | #include <rtc.h> |
| 13 | |
Michal Simek | c3e6c55 | 2008-07-14 19:45:37 +0200 | [diff] [blame] | 14 | #if defined(CONFIG_CMD_DATE) |
Mike Frysinger | 6742eaf | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 15 | |
| 16 | #include <asm/blackfin.h> |
| 17 | #include <asm/mach-common/bits/rtc.h> |
| 18 | |
| 19 | #define pr_stamp() debug("%s:%s:%i: here i am\n", __FILE__, __func__, __LINE__) |
| 20 | |
| 21 | #define MIN_TO_SECS(x) (60 * (x)) |
| 22 | #define HRS_TO_SECS(x) (60 * MIN_TO_SECS(x)) |
| 23 | #define DAYS_TO_SECS(x) (24 * HRS_TO_SECS(x)) |
| 24 | |
| 25 | #define NUM_SECS_IN_MIN MIN_TO_SECS(1) |
| 26 | #define NUM_SECS_IN_HR HRS_TO_SECS(1) |
| 27 | #define NUM_SECS_IN_DAY DAYS_TO_SECS(1) |
| 28 | |
Mike Frysinger | ca2dbd4 | 2008-10-06 03:39:07 -0400 | [diff] [blame] | 29 | /* Enable the RTC prescaler enable register */ |
Simon Glass | 538059d | 2014-11-14 18:18:26 -0700 | [diff] [blame] | 30 | void rtc_init(void) |
Mike Frysinger | ca2dbd4 | 2008-10-06 03:39:07 -0400 | [diff] [blame] | 31 | { |
| 32 | if (!(bfin_read_RTC_PREN() & 0x1)) |
| 33 | bfin_write_RTC_PREN(0x1); |
| 34 | } |
| 35 | |
Mike Frysinger | 6742eaf | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 36 | /* Our on-chip RTC has no notion of "reset" */ |
| 37 | void rtc_reset(void) |
| 38 | { |
Mike Frysinger | ca2dbd4 | 2008-10-06 03:39:07 -0400 | [diff] [blame] | 39 | rtc_init(); |
Mike Frysinger | 6742eaf | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /* Wait for pending writes to complete */ |
| 43 | static void wait_for_complete(void) |
| 44 | { |
| 45 | pr_stamp(); |
| 46 | while (!(bfin_read_RTC_ISTAT() & WRITE_COMPLETE)) |
| 47 | if (!(bfin_read_RTC_ISTAT() & WRITE_PENDING)) |
| 48 | break; |
| 49 | bfin_write_RTC_ISTAT(WRITE_COMPLETE); |
| 50 | } |
| 51 | |
Mike Frysinger | 6742eaf | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 52 | /* Set the time. Get the time_in_secs which is the number of seconds since Jan 1970 and set the RTC registers |
| 53 | * based on this value. |
| 54 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 55 | int rtc_set(struct rtc_time *tmp) |
Mike Frysinger | 6742eaf | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 56 | { |
| 57 | unsigned long remain, days, hrs, mins, secs; |
| 58 | |
| 59 | pr_stamp(); |
| 60 | |
| 61 | if (tmp == NULL) { |
| 62 | puts("Error setting the date/time\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 63 | return -1; |
Mike Frysinger | 6742eaf | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 64 | } |
| 65 | |
Mike Frysinger | ca2dbd4 | 2008-10-06 03:39:07 -0400 | [diff] [blame] | 66 | rtc_init(); |
Mike Frysinger | 6742eaf | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 67 | wait_for_complete(); |
| 68 | |
| 69 | /* Calculate number of seconds this incoming time represents */ |
| 70 | remain = mktime(tmp->tm_year, tmp->tm_mon, tmp->tm_mday, |
Wolfgang Denk | ec7fbf5 | 2013-10-04 17:43:24 +0200 | [diff] [blame] | 71 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
Mike Frysinger | 6742eaf | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 72 | |
| 73 | /* Figure out how many days since epoch */ |
| 74 | days = remain / NUM_SECS_IN_DAY; |
| 75 | |
| 76 | /* From the remaining secs, compute the hrs(0-23), mins(0-59) and secs(0-59) */ |
| 77 | remain = remain % NUM_SECS_IN_DAY; |
| 78 | hrs = remain / NUM_SECS_IN_HR; |
| 79 | remain = remain % NUM_SECS_IN_HR; |
| 80 | mins = remain / NUM_SECS_IN_MIN; |
| 81 | secs = remain % NUM_SECS_IN_MIN; |
| 82 | |
| 83 | /* Encode these time values into our RTC_STAT register */ |
| 84 | bfin_write_RTC_STAT(SET_ALARM(days, hrs, mins, secs)); |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 85 | |
| 86 | return 0; |
Mike Frysinger | 6742eaf | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* Read the time from the RTC_STAT. time_in_seconds is seconds since Jan 1970 */ |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 90 | int rtc_get(struct rtc_time *tmp) |
Mike Frysinger | 6742eaf | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 91 | { |
| 92 | uint32_t cur_rtc_stat; |
| 93 | int time_in_sec; |
| 94 | int tm_sec, tm_min, tm_hr, tm_day; |
| 95 | |
| 96 | pr_stamp(); |
| 97 | |
| 98 | if (tmp == NULL) { |
| 99 | puts("Error getting the date/time\n"); |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 100 | return -1; |
Mike Frysinger | 6742eaf | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Mike Frysinger | ca2dbd4 | 2008-10-06 03:39:07 -0400 | [diff] [blame] | 103 | rtc_init(); |
Mike Frysinger | 6742eaf | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 104 | wait_for_complete(); |
| 105 | |
| 106 | /* Read the RTC_STAT register */ |
| 107 | cur_rtc_stat = bfin_read_RTC_STAT(); |
| 108 | |
| 109 | /* Convert our encoded format into actual time values */ |
| 110 | tm_sec = (cur_rtc_stat & RTC_SEC) >> RTC_SEC_P; |
| 111 | tm_min = (cur_rtc_stat & RTC_MIN) >> RTC_MIN_P; |
| 112 | tm_hr = (cur_rtc_stat & RTC_HR ) >> RTC_HR_P; |
| 113 | tm_day = (cur_rtc_stat & RTC_DAY) >> RTC_DAY_P; |
| 114 | |
| 115 | /* Calculate the total number of seconds since epoch */ |
| 116 | time_in_sec = (tm_sec) + MIN_TO_SECS(tm_min) + HRS_TO_SECS(tm_hr) + DAYS_TO_SECS(tm_day); |
Simon Glass | 1299466 | 2015-04-20 12:37:18 -0600 | [diff] [blame^] | 117 | rtc_to_tm(time_in_sec, tmp); |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 118 | |
| 119 | return 0; |
Mike Frysinger | 6742eaf | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | #endif |