blob: 7d0cfb3ba8c7320f6102ec533a5dde4e2aded88e [file] [log] [blame]
Po-Yu Chuange0142542009-11-11 17:26:00 +08001/*
2 * Faraday FTRTC010 Real Time Clock
3 *
4 * (C) Copyright 2009 Faraday Technology
5 * Po-Yu Chuang <ratbert@faraday-tech.com>
6 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Po-Yu Chuange0142542009-11-11 17:26:00 +08008 */
9
10#include <config.h>
11#include <common.h>
12#include <rtc.h>
13#include <asm/io.h>
14
15struct 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 */
Macpaul Linf90f1332010-10-19 20:14:36 +080025 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 */
Po-Yu Chuange0142542009-11-11 17:26:00 +080032};
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
43static struct ftrtc010 *rtc = (struct ftrtc010 *)CONFIG_FTRTC010_BASE;
44
45static void ftrtc010_enable(void)
46{
47 writel(FTRTC010_CR_ENABLE, &rtc->cr);
48}
49
50/*
51 * return current time in seconds
52 */
53static 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
76int 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
Macpaul Linf90f1332010-10-19 20:14:36 +080083#ifdef CONFIG_FTRTC010_PCLK
84 now = (ftrtc010_time() + readl(&rtc->record)) / RTC_DIV_COUNT;
85#else /* CONFIG_FTRTC010_EXTCLK */
Po-Yu Chuange0142542009-11-11 17:26:00 +080086 now = ftrtc010_time() + readl(&rtc->record);
Macpaul Linf90f1332010-10-19 20:14:36 +080087#endif
Po-Yu Chuange0142542009-11-11 17:26:00 +080088
Simon Glass12994662015-04-20 12:37:18 -060089 rtc_to_tm(now, tmp);
Po-Yu Chuange0142542009-11-11 17:26:00 +080090
91 return 0;
92}
93
94/*
95 * Set the RTC
96 */
97int 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
Simon Glass4283e842015-04-20 12:37:19 -0600107 new = rtc_mktime(tmp);
Po-Yu Chuange0142542009-11-11 17:26:00 +0800108
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
118void rtc_reset(void)
119{
120 debug("%s()\n", __func__);
121 ftrtc010_enable();
122}