xref: /openbmc/u-boot/drivers/rtc/ftrtc010.c (revision 730d2544)
1 /*
2  * Faraday FTRTC010 Real Time Clock
3  *
4  * (C) Copyright 2009 Faraday Technology
5  * Po-Yu Chuang <ratbert@faraday-tech.com>
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 #include <config.h>
11 #include <common.h>
12 #include <rtc.h>
13 #include <asm/io.h>
14 
15 struct ftrtc010 {
16 	unsigned int sec;		/* 0x00 */
17 	unsigned int min;		/* 0x04 */
18 	unsigned int hour;		/* 0x08 */
19 	unsigned int day;		/* 0x0c */
20 	unsigned int alarm_sec;		/* 0x10 */
21 	unsigned int alarm_min;		/* 0x14 */
22 	unsigned int alarm_hour;	/* 0x18 */
23 	unsigned int record;		/* 0x1c */
24 	unsigned int cr;		/* 0x20 */
25 	unsigned int wsec;		/* 0x24 */
26 	unsigned int wmin;		/* 0x28 */
27 	unsigned int whour;		/* 0x2c */
28 	unsigned int wday;		/* 0x30 */
29 	unsigned int intr;		/* 0x34 */
30 	unsigned int div;		/* 0x38 */
31 	unsigned int rev;		/* 0x3c */
32 };
33 
34 /*
35  * RTC Control Register
36  */
37 #define FTRTC010_CR_ENABLE		(1 << 0)
38 #define FTRTC010_CR_INTERRUPT_SEC	(1 << 1)	/* per second irq */
39 #define FTRTC010_CR_INTERRUPT_MIN	(1 << 2)	/* per minute irq */
40 #define FTRTC010_CR_INTERRUPT_HR	(1 << 3)	/* per hour   irq */
41 #define FTRTC010_CR_INTERRUPT_DAY	(1 << 4)	/* per day    irq */
42 
43 static struct ftrtc010 *rtc = (struct ftrtc010 *)CONFIG_FTRTC010_BASE;
44 
45 static void ftrtc010_enable(void)
46 {
47 	writel(FTRTC010_CR_ENABLE, &rtc->cr);
48 }
49 
50 /*
51  * return current time in seconds
52  */
53 static unsigned long ftrtc010_time(void)
54 {
55 	unsigned long day;
56 	unsigned long hour;
57 	unsigned long minute;
58 	unsigned long second;
59 	unsigned long second2;
60 
61 	do {
62 		second	= readl(&rtc->sec);
63 		day	= readl(&rtc->day);
64 		hour	= readl(&rtc->hour);
65 		minute	= readl(&rtc->min);
66 		second2	= readl(&rtc->sec);
67 	} while (second != second2);
68 
69 	return day * 24 * 60 * 60 + hour * 60 * 60 + minute * 60 + second;
70 }
71 
72 /*
73  * Get the current time from the RTC
74  */
75 
76 int rtc_get(struct rtc_time *tmp)
77 {
78 	unsigned long now;
79 
80 	debug("%s(): record register: %x\n",
81 	      __func__, readl(&rtc->record));
82 
83 #ifdef CONFIG_FTRTC010_PCLK
84 	now = (ftrtc010_time() + readl(&rtc->record)) / RTC_DIV_COUNT;
85 #else /* CONFIG_FTRTC010_EXTCLK */
86 	now = ftrtc010_time() + readl(&rtc->record);
87 #endif
88 
89 	rtc_to_tm(now, tmp);
90 
91 	return 0;
92 }
93 
94 /*
95  * Set the RTC
96  */
97 int rtc_set(struct rtc_time *tmp)
98 {
99 	unsigned long new;
100 	unsigned long now;
101 
102 	debug("%s(): DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
103 	      __func__,
104 	      tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
105 	      tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
106 
107 	new = rtc_mktime(tmp);
108 
109 	now = ftrtc010_time();
110 
111 	debug("%s(): write %lx to record register\n", __func__, new - now);
112 
113 	writel(new - now, &rtc->record);
114 
115 	return 0;
116 }
117 
118 void rtc_reset(void)
119 {
120 	debug("%s()\n", __func__);
121 	ftrtc010_enable();
122 }
123