blob: 57f0aec590b8a9f4af0620e32dc53af0c239a452 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Manuel Lauss0c694de2008-12-21 09:26:23 +01002 * Copyright (C) 2008 Manuel Lauss <mano@roarinelk.homelinux.net>
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/mach-au1x00/au1000.h>
41
Manuel Lauss0c694de2008-12-21 09:26:23 +010042/* 32kHz clock enabled and detected */
43#define CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S)
44
Pete Popovfe359bf2005-04-08 08:34:43 +000045extern int allow_au1k_wait; /* default off for CP0 Counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static DEFINE_SPINLOCK(time_lock);
48
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +040049/*
50 * I haven't found anyone that doesn't use a 12 MHz source clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * but just in case.....
52 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define AU1000_SRC_CLK 12000000
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
56 * We read the real processor speed from the PLL. This is important
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +040057 * because it is more accurate than computing it from the 32 KHz
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * counter, if it exists. If we don't have an accurate processor
59 * speed, all of the peripherals that derive their clocks based on
60 * this advertised speed will introduce error and sometimes not work
61 * properly. This function is futher convoluted to still allow configurations
62 * to do that in case they have really, really old silicon with a
Manuel Lauss0c694de2008-12-21 09:26:23 +010063 * write-only PLL register. -- Dan
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 */
Sergei Shtylyoveba82912008-03-27 22:05:57 +030065unsigned long calc_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 unsigned long cpu_speed;
68 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 spin_lock_irqsave(&time_lock, flags);
71
Sergei Shtylyov758e2852008-03-27 16:09:31 +030072 /*
73 * On early Au1000, sys_cpupll was write-only. Since these
74 * silicon versions of Au1000 are not sold by AMD, we don't bend
75 * over backwards trying to determine the frequency.
76 */
Manuel Lauss074cf652008-12-21 09:26:21 +010077 if (au1xxx_cpu_has_pll_wo())
Sergei Shtylyov758e2852008-03-27 16:09:31 +030078#ifdef CONFIG_SOC_AU1000_FREQUENCY
79 cpu_speed = CONFIG_SOC_AU1000_FREQUENCY;
80#else
81 cpu_speed = 396000000;
82#endif
83 else
84 cpu_speed = (au_readl(SYS_CPUPLL) & 0x0000003f) * AU1000_SRC_CLK;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +040085 /* On Alchemy CPU:counter ratio is 1:1 */
Sergei Shtylyov53c1b192006-09-03 22:17:10 +040086 mips_hpt_frequency = cpu_speed;
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +040087 /* Equation: Baudrate = CPU / (SD * 2 * CLKDIV * 16) */
88 set_au1x00_uart_baud_base(cpu_speed / (2 * ((int)(au_readl(SYS_POWERCTRL)
89 & 0x03) + 2) * 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 spin_unlock_irqrestore(&time_lock, flags);
Sergei Shtylyoveba82912008-03-27 22:05:57 +030091 return cpu_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Manuel Lauss0c694de2008-12-21 09:26:23 +010094static cycle_t au1x_counter1_read(void)
95{
96 return au_readl(SYS_RTCREAD);
97}
98
99static struct clocksource au1x_counter1_clocksource = {
100 .name = "alchemy-counter1",
101 .read = au1x_counter1_read,
102 .mask = CLOCKSOURCE_MASK(32),
103 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
104 .rating = 100,
105};
106
107static int au1x_rtcmatch2_set_next_event(unsigned long delta,
108 struct clock_event_device *cd)
109{
110 delta += au_readl(SYS_RTCREAD);
111 /* wait for register access */
112 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M21)
113 ;
114 au_writel(delta, SYS_RTCMATCH2);
115 au_sync();
116
117 return 0;
118}
119
120static void au1x_rtcmatch2_set_mode(enum clock_event_mode mode,
121 struct clock_event_device *cd)
122{
123}
124
125static irqreturn_t au1x_rtcmatch2_irq(int irq, void *dev_id)
126{
127 struct clock_event_device *cd = dev_id;
128 cd->event_handler(cd);
129 return IRQ_HANDLED;
130}
131
132static struct clock_event_device au1x_rtcmatch2_clockdev = {
133 .name = "rtcmatch2",
134 .features = CLOCK_EVT_FEAT_ONESHOT,
135 .rating = 100,
136 .irq = AU1000_RTC_MATCH2_INT,
137 .set_next_event = au1x_rtcmatch2_set_next_event,
138 .set_mode = au1x_rtcmatch2_set_mode,
139 .cpumask = CPU_MASK_ALL,
140};
141
142static struct irqaction au1x_rtcmatch2_irqaction = {
143 .handler = au1x_rtcmatch2_irq,
144 .flags = IRQF_DISABLED | IRQF_TIMER,
145 .name = "timer",
146 .dev_id = &au1x_rtcmatch2_clockdev,
147};
148
Ralf Baechlebc2f2a22007-10-26 12:58:02 +0100149void __init plat_time_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Manuel Lauss0c694de2008-12-21 09:26:23 +0100151 struct clock_event_device *cd = &au1x_rtcmatch2_clockdev;
152 unsigned long t;
Sergei Shtylyoveba82912008-03-27 22:05:57 +0300153 unsigned int est_freq = calc_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 est_freq += 5000; /* round */
156 est_freq -= est_freq%10000;
Manuel Lauss074cf652008-12-21 09:26:21 +0100157 printk(KERN_INFO "(PRId %08x) @ %u.%02u MHz\n", read_c0_prid(),
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +0400158 est_freq / 1000000, ((est_freq % 1000000) * 100) / 1000000);
159 set_au1x00_speed(est_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Manuel Lauss0c694de2008-12-21 09:26:23 +0100161 /* Check if firmware (YAMON, ...) has enabled 32kHz and clock
162 * has been detected. If so install the rtcmatch2 clocksource,
163 * otherwise don't bother. Note that both bits being set is by
164 * no means a definite guarantee that the counters actually work
165 * (the 32S bit seems to be stuck set to 1 once a single clock-
166 * edge is detected, hence the timeouts).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
Manuel Lauss0c694de2008-12-21 09:26:23 +0100168 if (CNTR_OK != (au_readl(SYS_COUNTER_CNTRL) & CNTR_OK))
169 goto cntr_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Manuel Lauss0c694de2008-12-21 09:26:23 +0100171 /*
172 * setup counter 1 (RTC) to tick at full speed
173 */
174 t = 0xffffff;
175 while ((au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S) && t--)
176 asm volatile ("nop");
177 if (!t)
178 goto cntr_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Manuel Lauss0c694de2008-12-21 09:26:23 +0100180 au_writel(0, SYS_RTCTRIM); /* 32.768 kHz */
181 au_sync();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Manuel Lauss0c694de2008-12-21 09:26:23 +0100183 t = 0xffffff;
184 while ((au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S) && t--)
185 asm volatile ("nop");
186 if (!t)
187 goto cntr_err;
188 au_writel(0, SYS_RTCWRITE);
189 au_sync();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Manuel Lauss0c694de2008-12-21 09:26:23 +0100191 t = 0xffffff;
192 while ((au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S) && t--)
193 asm volatile ("nop");
194 if (!t)
195 goto cntr_err;
196
197 /* register counter1 clocksource and event device */
198 clocksource_set_clock(&au1x_counter1_clocksource, 32768);
199 clocksource_register(&au1x_counter1_clocksource);
200
201 cd->shift = 32;
202 cd->mult = div_sc(32768, NSEC_PER_SEC, cd->shift);
203 cd->max_delta_ns = clockevent_delta2ns(0xffffffff, cd);
204 cd->min_delta_ns = clockevent_delta2ns(8, cd); /* ~0.25ms */
205 clockevents_register_device(cd);
206 setup_irq(AU1000_RTC_MATCH2_INT, &au1x_rtcmatch2_irqaction);
207
208 printk(KERN_INFO "Alchemy clocksource installed\n");
209
210 /* can now use 'wait' */
211 allow_au1k_wait = 1;
212 return;
213
214cntr_err:
215 /* counters unusable, use C0 counter */
216 r4k_clockevent_init();
217 init_r4k_clocksource();
218 allow_au1k_wait = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}