aboutsummaryrefslogtreecommitdiff
path: root/drivers/rtc/mvrtc.c
blob: ed057f7bc4e41fc770e5f7ea3a98443accfeafbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2011
 * Jason Cooper <u-boot@lakedaemon.net>
 */

/*
 * Date & Time support for Marvell Integrated RTC
 */

#include <common.h>
#include <command.h>
#include <dm.h>
#include <rtc.h>
#include <asm/io.h>
#include <linux/delay.h>
#include "mvrtc.h"

/* This RTC does not support century, so we assume 20 */
#define CENTURY 20

static int __mv_rtc_get(struct mvrtc_registers *regs, struct rtc_time *t)
{
	u32 time;
	u32 date;

	/* read the time register */
	time = readl(&regs->time);

	/* read the date register */
	date = readl(&regs->date);

	/* test for 12 hour clock (can't tell if it's am/pm) */
	if (time & MVRTC_HRFMT_MSK) {
		printf("Error: RTC in 12 hour mode, can't determine AM/PM.\n");
		return -1;
	}

	/* time */
	t->tm_sec  = bcd2bin((time >> MVRTC_SEC_SFT)  & MVRTC_SEC_MSK);
	t->tm_min  = bcd2bin((time >> MVRTC_MIN_SFT)  & MVRTC_MIN_MSK);
	t->tm_hour = bcd2bin((time >> MVRTC_HOUR_SFT) & MVRTC_HOUR_MSK);
	t->tm_wday = bcd2bin((time >> MVRTC_DAY_SFT)  & MVRTC_DAY_MSK);
	t->tm_wday--;

	/* date */
	t->tm_mday = bcd2bin((date >> MVRTC_DATE_SFT) & MVRTC_DATE_MSK);
	t->tm_mon  = bcd2bin((date >> MVRTC_MON_SFT)  & MVRTC_MON_MSK);
	t->tm_year = bcd2bin((date >> MVRTC_YEAR_SFT) & MVRTC_YEAR_MSK);
	t->tm_year += CENTURY * 100;

	/* not supported in this RTC */
	t->tm_yday  = 0;
	t->tm_isdst = 0;

	return 0;
}

#ifndef CONFIG_DM_RTC
int rtc_get(struct rtc_time *t)
{
	struct mvrtc_registers *regs;

	regs = (struct mvrtc_registers *)KW_RTC_BASE;
	return __mv_rtc_get(regs, t);
}
#endif /* !CONFIG_DM_RTC */

static int __mv_rtc_set(struct mvrtc_registers *regs, const struct rtc_time *t)
{
	u32 time = 0; /* sets hour format bit to zero, 24hr format. */
	u32 date = 0;

	/* check that this code isn't 80+ years old ;-) */
	if ((t->tm_year / 100) != CENTURY)
		printf("Warning: Only century %d supported.\n", CENTURY);

	/* time */
	time |= (bin2bcd(t->tm_sec)      & MVRTC_SEC_MSK)  << MVRTC_SEC_SFT;
	time |= (bin2bcd(t->tm_min)      & MVRTC_MIN_MSK)  << MVRTC_MIN_SFT;
	time |= (bin2bcd(t->tm_hour)     & MVRTC_HOUR_MSK) << MVRTC_HOUR_SFT;
	time |= (bin2bcd(t->tm_wday + 1) & MVRTC_DAY_MSK)  << MVRTC_DAY_SFT;

	/* date */
	date |= (bin2bcd(t->tm_mday)       & MVRTC_DATE_MSK) << MVRTC_DATE_SFT;
	date |= (bin2bcd(t->tm_mon)        & MVRTC_MON_MSK)  << MVRTC_MON_SFT;
	date |= (bin2bcd(t->tm_year % 100) & MVRTC_YEAR_MSK) << MVRTC_YEAR_SFT;

	/* write the time register */
	writel(time, &regs->time);

	/* write the date register */
	writel(date, &regs->date);

	return 0;
}

#ifndef CONFIG_DM_RTC
int rtc_set(struct rtc_time *t)
{
	struct mvrtc_registers *regs;

	regs = (struct mvrtc_registers *)KW_RTC_BASE;
	return __mv_rtc_set(regs, t);
}
#endif /* !CONFIG_DM_RTC */

static void __mv_rtc_reset(struct mvrtc_registers *regs)
{
	u32 time;
	u32 sec;

	/* no init routine for this RTC needed, just check that it's working */
	time = readl(&regs->time);
	sec  = bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK);
	udelay(1000000);
	time = readl(&regs->time);

	if (sec == bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK))
		printf("Error: RTC did not increment.\n");
}

#ifndef CONFIG_DM_RTC
void rtc_reset(void)
{
	struct mvrtc_registers *regs;

	regs = (struct mvrtc_registers *)KW_RTC_BASE;
	__mv_rtc_reset(regs);
}
#endif /* !CONFIG_DM_RTC */

#ifdef CONFIG_DM_RTC
static int mv_rtc_get(struct udevice *dev, struct rtc_time *tm)
{
	struct mvrtc_pdata *pdata = dev_get_platdata(dev);
	struct mvrtc_registers *regs = (struct mvrtc_registers *)pdata->iobase;

	return __mv_rtc_get(regs, tm);
}

static int mv_rtc_set(struct udevice *dev, const struct rtc_time *tm)
{
	struct mvrtc_pdata *pdata = dev_get_platdata(dev);
	struct mvrtc_registers *regs = (struct mvrtc_registers *)pdata->iobase;

	return __mv_rtc_set(regs, tm);
}

static int mv_rtc_reset(struct udevice *dev)
{
	struct mvrtc_pdata *pdata = dev_get_platdata(dev);
	struct mvrtc_registers *regs = (struct mvrtc_registers *)pdata->iobase;

	__mv_rtc_reset(regs);
	return 0;
}

static const struct rtc_ops mv_rtc_ops = {
	.get = mv_rtc_get,
	.set = mv_rtc_set,
	.reset = mv_rtc_reset,
};

static const struct udevice_id mv_rtc_ids[] = {
	{ .compatible = "marvell,kirkwood-rtc" },
	{ .compatible = "marvell,orion-rtc" },
	{ }
};

static int mv_rtc_ofdata_to_platdata(struct udevice *dev)
{
	struct mvrtc_pdata *pdata = dev_get_platdata(dev);

	pdata->iobase = dev_read_addr(dev);
	return 0;
}

U_BOOT_DRIVER(rtc_mv) = {
	.name	= "rtc-mv",
	.id	= UCLASS_RTC,
	.ofdata_to_platdata = mv_rtc_ofdata_to_platdata,
	.of_match = mv_rtc_ids,
	.ops	= &mv_rtc_ops,
};
#endif /* CONFIG_DM_RTC */