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