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