1c7260d15SReinhard Meyer /*
2c7260d15SReinhard Meyer * (C) Copyright 2010
3c7260d15SReinhard Meyer * Reinhard Meyer, reinhard.meyer@emk-elektronik.de
4c7260d15SReinhard Meyer *
51a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+
6c7260d15SReinhard Meyer */
7c7260d15SReinhard Meyer
8c7260d15SReinhard Meyer /*
9c7260d15SReinhard Meyer * Date & Time support for the internal Real-time Timer
10c7260d15SReinhard Meyer * of AT91SAM9260 and compatibles.
11c7260d15SReinhard Meyer * Compatible with the LinuX rtc driver workaround:
12c7260d15SReinhard Meyer * The RTT cannot be written to, but only reset.
13c7260d15SReinhard Meyer * The actual time is the sum of RTT and one of
14c7260d15SReinhard Meyer * the four GPBR registers.
15c7260d15SReinhard Meyer *
16c7260d15SReinhard Meyer * The at91sam9260 has 4 GPBR (0-3).
17c7260d15SReinhard Meyer * For their typical use see at91_gpbr.h !
18c7260d15SReinhard Meyer *
19c7260d15SReinhard Meyer * make sure u-boot and kernel use the same GPBR !
20c7260d15SReinhard Meyer */
21c7260d15SReinhard Meyer
22c7260d15SReinhard Meyer #include <common.h>
23c7260d15SReinhard Meyer #include <command.h>
24c7260d15SReinhard Meyer #include <rtc.h>
2586592f60SReinhard Meyer #include <asm/io.h>
26*1221ce45SMasahiro Yamada #include <linux/errno.h>
27c7260d15SReinhard Meyer #include <asm/arch/hardware.h>
28c7260d15SReinhard Meyer #include <asm/arch/at91_rtt.h>
29c7260d15SReinhard Meyer #include <asm/arch/at91_gpbr.h>
30c7260d15SReinhard Meyer
31c7260d15SReinhard Meyer #if defined(CONFIG_CMD_DATE)
32c7260d15SReinhard Meyer
rtc_get(struct rtc_time * tmp)33c7260d15SReinhard Meyer int rtc_get (struct rtc_time *tmp)
34c7260d15SReinhard Meyer {
35372f2783SReinhard Meyer at91_rtt_t *rtt = (at91_rtt_t *) ATMEL_BASE_RTT;
36372f2783SReinhard Meyer at91_gpbr_t *gpbr = (at91_gpbr_t *) ATMEL_BASE_GPBR;
37c7260d15SReinhard Meyer ulong tim;
38c7260d15SReinhard Meyer ulong tim2;
39c7260d15SReinhard Meyer ulong off;
40c7260d15SReinhard Meyer
41c7260d15SReinhard Meyer do {
42c7260d15SReinhard Meyer tim = readl(&rtt->vr);
43c7260d15SReinhard Meyer tim2 = readl(&rtt->vr);
44c7260d15SReinhard Meyer } while (tim!=tim2);
45c7260d15SReinhard Meyer off = readl(&gpbr->reg[AT91_GPBR_INDEX_TIMEOFF]);
46c7260d15SReinhard Meyer /* off==0 means time is invalid, but we ignore that */
479f9276c3SSimon Glass rtc_to_tm(tim+off, tmp);
48c7260d15SReinhard Meyer return 0;
49c7260d15SReinhard Meyer }
50c7260d15SReinhard Meyer
rtc_set(struct rtc_time * tmp)51c7260d15SReinhard Meyer int rtc_set (struct rtc_time *tmp)
52c7260d15SReinhard Meyer {
53372f2783SReinhard Meyer at91_rtt_t *rtt = (at91_rtt_t *) ATMEL_BASE_RTT;
54372f2783SReinhard Meyer at91_gpbr_t *gpbr = (at91_gpbr_t *) ATMEL_BASE_GPBR;
55c7260d15SReinhard Meyer ulong tim;
56c7260d15SReinhard Meyer
5771420983SSimon Glass tim = rtc_mktime(tmp);
58c7260d15SReinhard Meyer
59c7260d15SReinhard Meyer /* clear alarm, set prescaler to 32768, clear counter */
60c7260d15SReinhard Meyer writel(32768+AT91_RTT_RTTRST, &rtt->mr);
61c7260d15SReinhard Meyer writel(~0, &rtt->ar);
62c7260d15SReinhard Meyer writel(tim, &gpbr->reg[AT91_GPBR_INDEX_TIMEOFF]);
63c7260d15SReinhard Meyer /* wait for counter clear to happen, takes less than a 1/32768th second */
64c7260d15SReinhard Meyer while (readl(&rtt->vr) != 0)
65c7260d15SReinhard Meyer ;
66c7260d15SReinhard Meyer return 0;
67c7260d15SReinhard Meyer }
68c7260d15SReinhard Meyer
rtc_reset(void)69c7260d15SReinhard Meyer void rtc_reset (void)
70c7260d15SReinhard Meyer {
71372f2783SReinhard Meyer at91_rtt_t *rtt = (at91_rtt_t *) ATMEL_BASE_RTT;
72372f2783SReinhard Meyer at91_gpbr_t *gpbr = (at91_gpbr_t *) ATMEL_BASE_GPBR;
73c7260d15SReinhard Meyer
74c7260d15SReinhard Meyer /* clear alarm, set prescaler to 32768, clear counter */
75c7260d15SReinhard Meyer writel(32768+AT91_RTT_RTTRST, &rtt->mr);
76c7260d15SReinhard Meyer writel(~0, &rtt->ar);
77c7260d15SReinhard Meyer writel(0, &gpbr->reg[AT91_GPBR_INDEX_TIMEOFF]);
78c7260d15SReinhard Meyer /* wait for counter clear to happen, takes less than a 1/32768th second */
79c7260d15SReinhard Meyer while (readl(&rtt->vr) != 0)
80c7260d15SReinhard Meyer ;
81c7260d15SReinhard Meyer }
82c7260d15SReinhard Meyer
83c7260d15SReinhard Meyer #endif
84