blob: ff4acb57e4e77a1c03e64c9608e75217c0c15b1d [file] [log] [blame]
Reinhard Meyerc7260d12010-07-27 16:22:09 +02001/*
2 * (C) Copyright 2010
3 * Reinhard Meyer, reinhard.meyer@emk-elektronik.de
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24/*
25 * Date & Time support for the internal Real-time Timer
26 * of AT91SAM9260 and compatibles.
27 * Compatible with the LinuX rtc driver workaround:
28 * The RTT cannot be written to, but only reset.
29 * The actual time is the sum of RTT and one of
30 * the four GPBR registers.
31 *
32 * The at91sam9260 has 4 GPBR (0-3).
33 * For their typical use see at91_gpbr.h !
34 *
35 * make sure u-boot and kernel use the same GPBR !
36 */
37
38#include <common.h>
39#include <command.h>
40#include <rtc.h>
Reinhard Meyer86592f62010-11-07 13:26:14 +010041#include <asm/io.h>
Reinhard Meyerc7260d12010-07-27 16:22:09 +020042#include <asm/errno.h>
43#include <asm/arch/hardware.h>
Reinhard Meyerc7260d12010-07-27 16:22:09 +020044#include <asm/arch/at91_rtt.h>
45#include <asm/arch/at91_gpbr.h>
46
47#if defined(CONFIG_CMD_DATE)
48
49int rtc_get (struct rtc_time *tmp)
50{
Reinhard Meyer372f2782010-11-03 15:47:20 +010051 at91_rtt_t *rtt = (at91_rtt_t *) ATMEL_BASE_RTT;
52 at91_gpbr_t *gpbr = (at91_gpbr_t *) ATMEL_BASE_GPBR;
Reinhard Meyerc7260d12010-07-27 16:22:09 +020053 ulong tim;
54 ulong tim2;
55 ulong off;
56
57 do {
58 tim = readl(&rtt->vr);
59 tim2 = readl(&rtt->vr);
60 } while (tim!=tim2);
61 off = readl(&gpbr->reg[AT91_GPBR_INDEX_TIMEOFF]);
62 /* off==0 means time is invalid, but we ignore that */
63 to_tm (tim+off, tmp);
64 return 0;
65}
66
67int rtc_set (struct rtc_time *tmp)
68{
Reinhard Meyer372f2782010-11-03 15:47:20 +010069 at91_rtt_t *rtt = (at91_rtt_t *) ATMEL_BASE_RTT;
70 at91_gpbr_t *gpbr = (at91_gpbr_t *) ATMEL_BASE_GPBR;
Reinhard Meyerc7260d12010-07-27 16:22:09 +020071 ulong tim;
72
73 tim = mktime (tmp->tm_year, tmp->tm_mon, tmp->tm_mday,
74 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
75
76 /* clear alarm, set prescaler to 32768, clear counter */
77 writel(32768+AT91_RTT_RTTRST, &rtt->mr);
78 writel(~0, &rtt->ar);
79 writel(tim, &gpbr->reg[AT91_GPBR_INDEX_TIMEOFF]);
80 /* wait for counter clear to happen, takes less than a 1/32768th second */
81 while (readl(&rtt->vr) != 0)
82 ;
83 return 0;
84}
85
86void rtc_reset (void)
87{
Reinhard Meyer372f2782010-11-03 15:47:20 +010088 at91_rtt_t *rtt = (at91_rtt_t *) ATMEL_BASE_RTT;
89 at91_gpbr_t *gpbr = (at91_gpbr_t *) ATMEL_BASE_GPBR;
Reinhard Meyerc7260d12010-07-27 16:22:09 +020090
91 /* clear alarm, set prescaler to 32768, clear counter */
92 writel(32768+AT91_RTT_RTTRST, &rtt->mr);
93 writel(~0, &rtt->ar);
94 writel(0, &gpbr->reg[AT91_GPBR_INDEX_TIMEOFF]);
95 /* wait for counter clear to happen, takes less than a 1/32768th second */
96 while (readl(&rtt->vr) != 0)
97 ;
98}
99
100#endif