Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2008 |
| 3 | * Gururaja Hebbar gururajakr@sanyo.co.in |
| 4 | * |
| 5 | * reference linux-2.6.20.6/drivers/rtc/rtc-pl031.c |
| 6 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <command.h> |
| 12 | #include <rtc.h> |
| 13 | |
| 14 | #if defined(CONFIG_CMD_DATE) |
| 15 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 16 | #ifndef CONFIG_SYS_RTC_PL031_BASE |
| 17 | #error CONFIG_SYS_RTC_PL031_BASE is not defined! |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 18 | #endif |
| 19 | |
| 20 | /* |
| 21 | * Register definitions |
| 22 | */ |
| 23 | #define RTC_DR 0x00 /* Data read register */ |
| 24 | #define RTC_MR 0x04 /* Match register */ |
| 25 | #define RTC_LR 0x08 /* Data load register */ |
| 26 | #define RTC_CR 0x0c /* Control register */ |
| 27 | #define RTC_IMSC 0x10 /* Interrupt mask and set register */ |
| 28 | #define RTC_RIS 0x14 /* Raw interrupt status register */ |
| 29 | #define RTC_MIS 0x18 /* Masked interrupt status register */ |
| 30 | #define RTC_ICR 0x1c /* Interrupt clear register */ |
| 31 | |
| 32 | #define RTC_CR_START (1 << 0) |
| 33 | |
| 34 | #define RTC_WRITE_REG(addr, val) \ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 35 | (*(volatile unsigned int *)(CONFIG_SYS_RTC_PL031_BASE + (addr)) = (val)) |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 36 | #define RTC_READ_REG(addr) \ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 37 | (*(volatile unsigned int *)(CONFIG_SYS_RTC_PL031_BASE + (addr))) |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 38 | |
| 39 | static int pl031_initted = 0; |
| 40 | |
| 41 | /* Enable RTC Start in Control register*/ |
| 42 | void rtc_init(void) |
| 43 | { |
| 44 | RTC_WRITE_REG(RTC_CR, RTC_CR_START); |
| 45 | |
| 46 | pl031_initted = 1; |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * Reset the RTC. We set the date back to 1970-01-01. |
| 51 | */ |
| 52 | void rtc_reset(void) |
| 53 | { |
| 54 | RTC_WRITE_REG(RTC_LR, 0x00); |
| 55 | if(!pl031_initted) |
| 56 | rtc_init(); |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Set the RTC |
| 61 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 62 | int rtc_set(struct rtc_time *tmp) |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 63 | { |
| 64 | unsigned long tim; |
| 65 | |
| 66 | if(!pl031_initted) |
| 67 | rtc_init(); |
| 68 | |
| 69 | if (tmp == NULL) { |
| 70 | puts("Error setting the date/time\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 71 | return -1; |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* Calculate number of seconds this incoming time represents */ |
Simon Glass | 4283e84 | 2015-04-20 12:37:19 -0600 | [diff] [blame] | 75 | tim = rtc_mktime(tmp); |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 76 | |
| 77 | RTC_WRITE_REG(RTC_LR, tim); |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 78 | |
| 79 | return -1; |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Get the current time from the RTC |
| 84 | */ |
| 85 | int rtc_get(struct rtc_time *tmp) |
| 86 | { |
| 87 | ulong tim; |
| 88 | |
| 89 | if(!pl031_initted) |
| 90 | rtc_init(); |
| 91 | |
| 92 | if (tmp == NULL) { |
| 93 | puts("Error getting the date/time\n"); |
| 94 | return -1; |
| 95 | } |
| 96 | |
| 97 | tim = RTC_READ_REG(RTC_DR); |
| 98 | |
Simon Glass | 1299466 | 2015-04-20 12:37:18 -0600 | [diff] [blame] | 99 | rtc_to_tm(tim, tmp); |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 100 | |
| 101 | debug ( "Get 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 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | #endif |