blob: 8955805e3bc12766595234cf6f7a09cb6f312d0c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Gururaja Hebbar K R76873a62008-08-25 11:30:29 +02002/*
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 R76873a62008-08-25 11:30:29 +02007 */
8
9#include <common.h>
10#include <command.h>
11#include <rtc.h>
12
13#if defined(CONFIG_CMD_DATE)
14
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020015#ifndef CONFIG_SYS_RTC_PL031_BASE
16#error CONFIG_SYS_RTC_PL031_BASE is not defined!
Gururaja Hebbar K R76873a62008-08-25 11:30:29 +020017#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-VILLARD03836942008-10-16 15:01:15 +020034 (*(volatile unsigned int *)(CONFIG_SYS_RTC_PL031_BASE + (addr)) = (val))
Gururaja Hebbar K R76873a62008-08-25 11:30:29 +020035#define RTC_READ_REG(addr) \
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020036 (*(volatile unsigned int *)(CONFIG_SYS_RTC_PL031_BASE + (addr)))
Gururaja Hebbar K R76873a62008-08-25 11:30:29 +020037
38static int pl031_initted = 0;
39
40/* Enable RTC Start in Control register*/
41void 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 */
51void 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-VILLARD97a2e102008-09-01 23:06:23 +020061int rtc_set(struct rtc_time *tmp)
Gururaja Hebbar K R76873a62008-08-25 11:30:29 +020062{
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-VILLARD97a2e102008-09-01 23:06:23 +020070 return -1;
Gururaja Hebbar K R76873a62008-08-25 11:30:29 +020071 }
72
73 /* Calculate number of seconds this incoming time represents */
Simon Glass4283e842015-04-20 12:37:19 -060074 tim = rtc_mktime(tmp);
Gururaja Hebbar K R76873a62008-08-25 11:30:29 +020075
76 RTC_WRITE_REG(RTC_LR, tim);
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020077
78 return -1;
Gururaja Hebbar K R76873a62008-08-25 11:30:29 +020079}
80
81/*
82 * Get the current time from the RTC
83 */
84int 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 Glass12994662015-04-20 12:37:18 -060098 rtc_to_tm(tim, tmp);
Gururaja Hebbar K R76873a62008-08-25 11:30:29 +020099
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