blob: 93fa586d52e2d480d6157cc0fcffdf962cad6534 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Manuel Lauss78814462009-10-07 20:15:15 +02002 * Copyright (C) 2008-2009 Manuel Lauss <manuel.lauss@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Manuel Lauss0c694de2008-12-21 09:26:23 +01004 * Previous incarnations were:
Sergei Shtylyov01675092008-03-24 23:15:50 +03005 * Copyright (C) 2001, 2006, 2008 MontaVista Software, <source@mvista.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copied and modified Carsten Langgaard's time.c
7 *
8 * Carsten Langgaard, carstenl@mips.com
9 * Copyright (C) 1999,2000 MIPS Technologies, Inc. All rights reserved.
10 *
11 * ########################################################################
12 *
13 * This program is free software; you can distribute it and/or modify it
14 * under the terms of the GNU General Public License (Version 2) as
15 * published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
25 *
26 * ########################################################################
27 *
Manuel Lauss0c694de2008-12-21 09:26:23 +010028 * Clocksource/event using the 32.768kHz-clocked Counter1 ('RTC' in the
29 * databooks). Firmware/Board init code must enable the counters in the
30 * counter control register, otherwise the CP0 counter clocksource/event
31 * will be installed instead (and use of 'wait' instruction is prohibited).
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 */
33
Manuel Lauss0c694de2008-12-21 09:26:23 +010034#include <linux/clockchips.h>
35#include <linux/clocksource.h>
36#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Ralf Baechlebdc92d742013-05-21 16:59:19 +020039#include <asm/idle.h>
Manuel Lauss2882b0c2009-08-22 18:09:27 +020040#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/mach-au1x00/au1000.h>
43
Manuel Lauss0c694de2008-12-21 09:26:23 +010044/* 32kHz clock enabled and detected */
45#define CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S)
46
Manuel Laussad058e92009-04-22 08:01:48 +020047static cycle_t au1x_counter1_read(struct clocksource *cs)
Manuel Lauss0c694de2008-12-21 09:26:23 +010048{
49 return au_readl(SYS_RTCREAD);
50}
51
52static struct clocksource au1x_counter1_clocksource = {
53 .name = "alchemy-counter1",
54 .read = au1x_counter1_read,
55 .mask = CLOCKSOURCE_MASK(32),
56 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Manuel Lauss8e0d7372012-12-17 06:14:08 +000057 .rating = 1500,
Manuel Lauss0c694de2008-12-21 09:26:23 +010058};
59
60static int au1x_rtcmatch2_set_next_event(unsigned long delta,
61 struct clock_event_device *cd)
62{
63 delta += au_readl(SYS_RTCREAD);
64 /* wait for register access */
65 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M21)
66 ;
67 au_writel(delta, SYS_RTCMATCH2);
68 au_sync();
69
70 return 0;
71}
72
73static void au1x_rtcmatch2_set_mode(enum clock_event_mode mode,
74 struct clock_event_device *cd)
75{
76}
77
78static irqreturn_t au1x_rtcmatch2_irq(int irq, void *dev_id)
79{
80 struct clock_event_device *cd = dev_id;
81 cd->event_handler(cd);
82 return IRQ_HANDLED;
83}
84
85static struct clock_event_device au1x_rtcmatch2_clockdev = {
86 .name = "rtcmatch2",
87 .features = CLOCK_EVT_FEAT_ONESHOT,
Manuel Lauss8e0d7372012-12-17 06:14:08 +000088 .rating = 1500,
Ralf Baechle70342282013-01-22 12:59:30 +010089 .set_next_event = au1x_rtcmatch2_set_next_event,
Manuel Lauss0c694de2008-12-21 09:26:23 +010090 .set_mode = au1x_rtcmatch2_set_mode,
Rusty Russell51c870a2009-09-24 09:34:35 -060091 .cpumask = cpu_all_mask,
Manuel Lauss0c694de2008-12-21 09:26:23 +010092};
93
94static struct irqaction au1x_rtcmatch2_irqaction = {
95 .handler = au1x_rtcmatch2_irq,
Yong Zhang8b5690f2011-11-22 14:38:03 +000096 .flags = IRQF_TIMER,
Manuel Lauss0c694de2008-12-21 09:26:23 +010097 .name = "timer",
98 .dev_id = &au1x_rtcmatch2_clockdev,
99};
100
Manuel Lauss78814462009-10-07 20:15:15 +0200101static int __init alchemy_time_init(unsigned int m2int)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Manuel Lauss0c694de2008-12-21 09:26:23 +0100103 struct clock_event_device *cd = &au1x_rtcmatch2_clockdev;
104 unsigned long t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Manuel Lauss78814462009-10-07 20:15:15 +0200106 au1x_rtcmatch2_clockdev.irq = m2int;
107
Manuel Lauss0c694de2008-12-21 09:26:23 +0100108 /* Check if firmware (YAMON, ...) has enabled 32kHz and clock
109 * has been detected. If so install the rtcmatch2 clocksource,
110 * otherwise don't bother. Note that both bits being set is by
111 * no means a definite guarantee that the counters actually work
112 * (the 32S bit seems to be stuck set to 1 once a single clock-
113 * edge is detected, hence the timeouts).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
Manuel Lauss0c694de2008-12-21 09:26:23 +0100115 if (CNTR_OK != (au_readl(SYS_COUNTER_CNTRL) & CNTR_OK))
116 goto cntr_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Manuel Lauss0c694de2008-12-21 09:26:23 +0100118 /*
119 * setup counter 1 (RTC) to tick at full speed
120 */
121 t = 0xffffff;
Roel Kluin4b0d3f52009-01-31 12:23:34 +0100122 while ((au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S) && --t)
Manuel Lauss0c694de2008-12-21 09:26:23 +0100123 asm volatile ("nop");
124 if (!t)
125 goto cntr_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Manuel Lauss0c694de2008-12-21 09:26:23 +0100127 au_writel(0, SYS_RTCTRIM); /* 32.768 kHz */
128 au_sync();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Manuel Lauss0c694de2008-12-21 09:26:23 +0100130 t = 0xffffff;
Roel Kluin4b0d3f52009-01-31 12:23:34 +0100131 while ((au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S) && --t)
Manuel Lauss0c694de2008-12-21 09:26:23 +0100132 asm volatile ("nop");
133 if (!t)
134 goto cntr_err;
135 au_writel(0, SYS_RTCWRITE);
136 au_sync();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Manuel Lauss0c694de2008-12-21 09:26:23 +0100138 t = 0xffffff;
Roel Kluin4b0d3f52009-01-31 12:23:34 +0100139 while ((au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S) && --t)
Manuel Lauss0c694de2008-12-21 09:26:23 +0100140 asm volatile ("nop");
141 if (!t)
142 goto cntr_err;
143
144 /* register counter1 clocksource and event device */
John Stultz75c4fd82010-04-26 20:23:11 -0700145 clocksource_register_hz(&au1x_counter1_clocksource, 32768);
Manuel Lauss0c694de2008-12-21 09:26:23 +0100146
147 cd->shift = 32;
148 cd->mult = div_sc(32768, NSEC_PER_SEC, cd->shift);
149 cd->max_delta_ns = clockevent_delta2ns(0xffffffff, cd);
Manuel Lauss8e365792011-12-20 17:37:29 +0100150 cd->min_delta_ns = clockevent_delta2ns(9, cd); /* ~0.28ms */
Manuel Lauss0c694de2008-12-21 09:26:23 +0100151 clockevents_register_device(cd);
Manuel Lauss78814462009-10-07 20:15:15 +0200152 setup_irq(m2int, &au1x_rtcmatch2_irqaction);
Manuel Lauss0c694de2008-12-21 09:26:23 +0100153
154 printk(KERN_INFO "Alchemy clocksource installed\n");
155
Manuel Lauss78814462009-10-07 20:15:15 +0200156 return 0;
Manuel Lauss0c694de2008-12-21 09:26:23 +0100157
158cntr_err:
Manuel Lauss78814462009-10-07 20:15:15 +0200159 return -1;
160}
161
Manuel Lauss78814462009-10-07 20:15:15 +0200162static int alchemy_m2inttab[] __initdata = {
163 AU1000_RTC_MATCH2_INT,
164 AU1500_RTC_MATCH2_INT,
165 AU1100_RTC_MATCH2_INT,
166 AU1550_RTC_MATCH2_INT,
167 AU1200_RTC_MATCH2_INT,
Manuel Lauss809f36c2011-11-01 20:03:30 +0100168 AU1300_RTC_MATCH2_INT,
Manuel Lauss78814462009-10-07 20:15:15 +0200169};
170
171void __init plat_time_init(void)
172{
173 int t;
174
175 t = alchemy_get_cputype();
Manuel Lauss8e0d7372012-12-17 06:14:08 +0000176 if (t == ALCHEMY_CPU_UNKNOWN ||
177 alchemy_time_init(alchemy_m2inttab[t]))
178 cpu_wait = NULL; /* wait doesn't work with r4k timer */
Manuel Lauss78814462009-10-07 20:15:15 +0200179}