blob: 3bd9e499755b6ff822f38a397f28342eed97d451 [file] [log] [blame]
David S. Millercf3d7c12008-03-26 01:11:55 -07001/* time.c: UltraSparc timer and TOD clock support.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David S. Millercf3d7c12008-03-26 01:11:55 -07003 * Copyright (C) 1997, 2008 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
5 *
6 * Based largely on code which is:
7 *
8 * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/errno.h>
Paul Gortmaker066bcac2011-07-22 13:18:16 -040012#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/sched.h>
14#include <linux/kernel.h>
15#include <linux/param.h>
16#include <linux/string.h>
17#include <linux/mm.h>
18#include <linux/interrupt.h>
19#include <linux/time.h>
20#include <linux/timex.h>
21#include <linux/init.h>
22#include <linux/ioport.h>
23#include <linux/mc146818rtc.h>
24#include <linux/delay.h>
25#include <linux/profile.h>
26#include <linux/bcd.h>
27#include <linux/jiffies.h>
28#include <linux/cpufreq.h>
29#include <linux/percpu.h>
David S. Miller8ba706a2006-03-01 17:32:46 -080030#include <linux/miscdevice.h>
David S. Miller1518e7e2008-08-28 21:06:27 -070031#include <linux/rtc/m48t59.h>
David S. Miller777a4472007-02-22 06:24:10 -080032#include <linux/kernel_stat.h>
David S. Miller112f4872007-03-05 15:28:37 -080033#include <linux/clockchips.h>
34#include <linux/clocksource.h>
Stephen Rothwell764f2572008-08-07 15:33:36 -070035#include <linux/of_device.h>
David S. Miller1518e7e2008-08-28 21:06:27 -070036#include <linux/platform_device.h>
David S. Miller9960e9e2010-04-07 04:41:33 -070037#include <linux/ftrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/oplib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/timer.h>
Ingo Molnar82268da2009-03-28 04:21:18 +010041#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/io.h>
David S. Millerff0d2fc2006-06-29 15:28:05 -070043#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/starfire.h>
45#include <asm/smp.h>
46#include <asm/sections.h>
47#include <asm/cpudata.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080048#include <linux/uaccess.h>
Al Viro63540ba2006-10-09 11:51:14 +010049#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
David S. Millercf3d7c12008-03-26 01:11:55 -070051#include "entry.h"
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053DEFINE_SPINLOCK(rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define TICK_PRIV_BIT (1UL << 63)
David S. Miller112f4872007-03-05 15:28:37 -080056#define TICKCMP_IRQ_BIT (1UL << 63)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#ifdef CONFIG_SMP
59unsigned long profile_pc(struct pt_regs *regs)
60{
61 unsigned long pc = instruction_pointer(regs);
62
63 if (in_lock_functions(pc))
64 return regs->u_regs[UREG_RETPC];
65 return pc;
66}
67EXPORT_SYMBOL(profile_pc);
68#endif
69
70static void tick_disable_protection(void)
71{
72 /* Set things up so user can access tick register for profiling
73 * purposes. Also workaround BB_ERRATA_1 by doing a dummy
74 * read back of %tick after writing it.
75 */
76 __asm__ __volatile__(
77 " ba,pt %%xcc, 1f\n"
78 " nop\n"
79 " .align 64\n"
80 "1: rd %%tick, %%g2\n"
81 " add %%g2, 6, %%g2\n"
82 " andn %%g2, %0, %%g2\n"
83 " wrpr %%g2, 0, %%tick\n"
84 " rdpr %%tick, %%g0"
85 : /* no outputs */
86 : "r" (TICK_PRIV_BIT)
87 : "g2");
88}
89
David S. Miller112f4872007-03-05 15:28:37 -080090static void tick_disable_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 __asm__ __volatile__(
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 " ba,pt %%xcc, 1f\n"
David S. Miller112f4872007-03-05 15:28:37 -080094 " nop\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 " .align 64\n"
David S. Miller112f4872007-03-05 15:28:37 -080096 "1: wr %0, 0x0, %%tick_cmpr\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 " rd %%tick_cmpr, %%g0"
98 : /* no outputs */
David S. Miller112f4872007-03-05 15:28:37 -080099 : "r" (TICKCMP_IRQ_BIT));
100}
101
102static void tick_init_tick(void)
103{
104 tick_disable_protection();
105 tick_disable_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Sam Ravnborg90181132009-01-06 13:19:28 -0800108static unsigned long long tick_get_tick(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 unsigned long ret;
111
112 __asm__ __volatile__("rd %%tick, %0\n\t"
113 "mov %0, %0"
114 : "=r" (ret));
115
116 return ret & ~TICK_PRIV_BIT;
117}
118
David S. Miller112f4872007-03-05 15:28:37 -0800119static int tick_add_compare(unsigned long adj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
David S. Miller112f4872007-03-05 15:28:37 -0800121 unsigned long orig_tick, new_tick, new_compare;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
David S. Miller112f4872007-03-05 15:28:37 -0800123 __asm__ __volatile__("rd %%tick, %0"
124 : "=r" (orig_tick));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
David S. Miller112f4872007-03-05 15:28:37 -0800126 orig_tick &= ~TICKCMP_IRQ_BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 /* Workaround for Spitfire Errata (#54 I think??), I discovered
129 * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch
130 * number 103640.
131 *
132 * On Blackbird writes to %tick_cmpr can fail, the
133 * workaround seems to be to execute the wr instruction
134 * at the start of an I-cache line, and perform a dummy
135 * read back from %tick_cmpr right after writing to it. -DaveM
136 */
David S. Miller112f4872007-03-05 15:28:37 -0800137 __asm__ __volatile__("ba,pt %%xcc, 1f\n\t"
138 " add %1, %2, %0\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 ".align 64\n"
140 "1:\n\t"
141 "wr %0, 0, %%tick_cmpr\n\t"
David S. Miller112f4872007-03-05 15:28:37 -0800142 "rd %%tick_cmpr, %%g0\n\t"
143 : "=r" (new_compare)
144 : "r" (orig_tick), "r" (adj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
David S. Miller112f4872007-03-05 15:28:37 -0800146 __asm__ __volatile__("rd %%tick, %0"
147 : "=r" (new_tick));
148 new_tick &= ~TICKCMP_IRQ_BIT;
149
150 return ((long)(new_tick - (orig_tick+adj))) > 0L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
David S. Miller112f4872007-03-05 15:28:37 -0800153static unsigned long tick_add_tick(unsigned long adj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
David S. Miller112f4872007-03-05 15:28:37 -0800155 unsigned long new_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 /* Also need to handle Blackbird bug here too. */
158 __asm__ __volatile__("rd %%tick, %0\n\t"
David S. Miller112f4872007-03-05 15:28:37 -0800159 "add %0, %1, %0\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 "wrpr %0, 0, %%tick\n\t"
David S. Miller112f4872007-03-05 15:28:37 -0800161 : "=&r" (new_tick)
162 : "r" (adj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 return new_tick;
165}
166
Pavel Tatashin89108c32017-06-12 16:41:45 -0400167static unsigned long tick_get_frequency(void)
168{
169 return local_cpu_data().clock_tick;
170}
171
Pavel Tatashin178bf2b2017-06-12 16:41:44 -0400172static struct sparc64_tick_ops tick_operations __cacheline_aligned = {
David S. Miller112f4872007-03-05 15:28:37 -0800173 .name = "tick",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 .init_tick = tick_init_tick,
David S. Miller112f4872007-03-05 15:28:37 -0800175 .disable_irq = tick_disable_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .get_tick = tick_get_tick,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 .add_tick = tick_add_tick,
178 .add_compare = tick_add_compare,
Pavel Tatashin89108c32017-06-12 16:41:45 -0400179 .get_frequency = tick_get_frequency,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 .softint_mask = 1UL << 0,
181};
182
David S. Millerfc321492005-11-07 14:10:10 -0800183struct sparc64_tick_ops *tick_ops __read_mostly = &tick_operations;
Sam Ravnborg917c3662009-01-08 16:58:20 -0800184EXPORT_SYMBOL(tick_ops);
David S. Millerfc321492005-11-07 14:10:10 -0800185
David S. Miller112f4872007-03-05 15:28:37 -0800186static void stick_disable_irq(void)
187{
188 __asm__ __volatile__(
189 "wr %0, 0x0, %%asr25"
190 : /* no outputs */
191 : "r" (TICKCMP_IRQ_BIT));
192}
193
194static void stick_init_tick(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
David S. Miller7aa62642006-02-11 23:14:59 -0800196 /* Writes to the %tick and %stick register are not
197 * allowed on sun4v. The Hypervisor controls that
198 * bit, per-strand.
199 */
200 if (tlb_type != hypervisor) {
201 tick_disable_protection();
David S. Miller112f4872007-03-05 15:28:37 -0800202 tick_disable_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
David S. Miller7aa62642006-02-11 23:14:59 -0800204 /* Let the user get at STICK too. */
205 __asm__ __volatile__(
206 " rd %%asr24, %%g2\n"
207 " andn %%g2, %0, %%g2\n"
208 " wr %%g2, 0, %%asr24"
209 : /* no outputs */
210 : "r" (TICK_PRIV_BIT)
211 : "g1", "g2");
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
David S. Miller112f4872007-03-05 15:28:37 -0800214 stick_disable_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Sam Ravnborg90181132009-01-06 13:19:28 -0800217static unsigned long long stick_get_tick(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 unsigned long ret;
220
221 __asm__ __volatile__("rd %%asr24, %0"
222 : "=r" (ret));
223
224 return ret & ~TICK_PRIV_BIT;
225}
226
David S. Miller112f4872007-03-05 15:28:37 -0800227static unsigned long stick_add_tick(unsigned long adj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
David S. Miller112f4872007-03-05 15:28:37 -0800229 unsigned long new_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 __asm__ __volatile__("rd %%asr24, %0\n\t"
David S. Miller112f4872007-03-05 15:28:37 -0800232 "add %0, %1, %0\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 "wr %0, 0, %%asr24\n\t"
David S. Miller112f4872007-03-05 15:28:37 -0800234 : "=&r" (new_tick)
235 : "r" (adj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 return new_tick;
238}
239
David S. Miller112f4872007-03-05 15:28:37 -0800240static int stick_add_compare(unsigned long adj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
David S. Miller112f4872007-03-05 15:28:37 -0800242 unsigned long orig_tick, new_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
David S. Miller112f4872007-03-05 15:28:37 -0800244 __asm__ __volatile__("rd %%asr24, %0"
245 : "=r" (orig_tick));
246 orig_tick &= ~TICKCMP_IRQ_BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
David S. Miller112f4872007-03-05 15:28:37 -0800248 __asm__ __volatile__("wr %0, 0, %%asr25"
249 : /* no outputs */
250 : "r" (orig_tick + adj));
251
252 __asm__ __volatile__("rd %%asr24, %0"
253 : "=r" (new_tick));
254 new_tick &= ~TICKCMP_IRQ_BIT;
255
256 return ((long)(new_tick - (orig_tick+adj))) > 0L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Pavel Tatashin89108c32017-06-12 16:41:45 -0400259static unsigned long stick_get_frequency(void)
260{
261 struct device_node *dp = of_find_node_by_path("/");
262
263 return of_getintprop_default(dp, "stick-frequency", 0);
264}
265
David S. Millerd369ddd2005-07-10 15:45:11 -0700266static struct sparc64_tick_ops stick_operations __read_mostly = {
David S. Miller112f4872007-03-05 15:28:37 -0800267 .name = "stick",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 .init_tick = stick_init_tick,
David S. Miller112f4872007-03-05 15:28:37 -0800269 .disable_irq = stick_disable_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 .get_tick = stick_get_tick,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 .add_tick = stick_add_tick,
272 .add_compare = stick_add_compare,
Pavel Tatashin89108c32017-06-12 16:41:45 -0400273 .get_frequency = stick_get_frequency,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 .softint_mask = 1UL << 16,
275};
276
277/* On Hummingbird the STICK/STICK_CMPR register is implemented
278 * in I/O space. There are two 64-bit registers each, the
279 * first holds the low 32-bits of the value and the second holds
280 * the high 32-bits.
281 *
282 * Since STICK is constantly updating, we have to access it carefully.
283 *
284 * The sequence we use to read is:
Richard Mortimer9eb33942006-01-17 15:21:01 -0800285 * 1) read high
286 * 2) read low
287 * 3) read high again, if it rolled re-read both low and high again.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 *
289 * Writing STICK safely is also tricky:
290 * 1) write low to zero
291 * 2) write high
292 * 3) write low
293 */
294#define HBIRD_STICKCMP_ADDR 0x1fe0000f060UL
295#define HBIRD_STICK_ADDR 0x1fe0000f070UL
296
297static unsigned long __hbird_read_stick(void)
298{
299 unsigned long ret, tmp1, tmp2, tmp3;
Richard Mortimer9eb33942006-01-17 15:21:01 -0800300 unsigned long addr = HBIRD_STICK_ADDR+8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Richard Mortimer9eb33942006-01-17 15:21:01 -0800302 __asm__ __volatile__("ldxa [%1] %5, %2\n"
303 "1:\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 "sub %1, 0x8, %1\n\t"
Richard Mortimer9eb33942006-01-17 15:21:01 -0800305 "ldxa [%1] %5, %3\n\t"
306 "add %1, 0x8, %1\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 "ldxa [%1] %5, %4\n\t"
308 "cmp %4, %2\n\t"
Richard Mortimer9eb33942006-01-17 15:21:01 -0800309 "bne,a,pn %%xcc, 1b\n\t"
310 " mov %4, %2\n\t"
311 "sllx %4, 32, %4\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 "or %3, %4, %0\n\t"
313 : "=&r" (ret), "=&r" (addr),
314 "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
315 : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr));
316
317 return ret;
318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320static void __hbird_write_stick(unsigned long val)
321{
322 unsigned long low = (val & 0xffffffffUL);
323 unsigned long high = (val >> 32UL);
324 unsigned long addr = HBIRD_STICK_ADDR;
325
326 __asm__ __volatile__("stxa %%g0, [%0] %4\n\t"
327 "add %0, 0x8, %0\n\t"
328 "stxa %3, [%0] %4\n\t"
329 "sub %0, 0x8, %0\n\t"
330 "stxa %2, [%0] %4"
331 : "=&r" (addr)
332 : "0" (addr), "r" (low), "r" (high),
333 "i" (ASI_PHYS_BYPASS_EC_E));
334}
335
336static void __hbird_write_compare(unsigned long val)
337{
338 unsigned long low = (val & 0xffffffffUL);
339 unsigned long high = (val >> 32UL);
340 unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL;
341
342 __asm__ __volatile__("stxa %3, [%0] %4\n\t"
343 "sub %0, 0x8, %0\n\t"
344 "stxa %2, [%0] %4"
345 : "=&r" (addr)
346 : "0" (addr), "r" (low), "r" (high),
347 "i" (ASI_PHYS_BYPASS_EC_E));
348}
349
David S. Miller112f4872007-03-05 15:28:37 -0800350static void hbtick_disable_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
David S. Miller112f4872007-03-05 15:28:37 -0800352 __hbird_write_compare(TICKCMP_IRQ_BIT);
353}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
David S. Miller112f4872007-03-05 15:28:37 -0800355static void hbtick_init_tick(void)
356{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 tick_disable_protection();
358
359 /* XXX This seems to be necessary to 'jumpstart' Hummingbird
360 * XXX into actually sending STICK interrupts. I think because
361 * XXX of how we store %tick_cmpr in head.S this somehow resets the
362 * XXX {TICK + STICK} interrupt mux. -DaveM
363 */
364 __hbird_write_stick(__hbird_read_stick());
365
David S. Miller112f4872007-03-05 15:28:37 -0800366 hbtick_disable_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Sam Ravnborg90181132009-01-06 13:19:28 -0800369static unsigned long long hbtick_get_tick(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 return __hbird_read_stick() & ~TICK_PRIV_BIT;
372}
373
David S. Miller112f4872007-03-05 15:28:37 -0800374static unsigned long hbtick_add_tick(unsigned long adj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 unsigned long val;
377
378 val = __hbird_read_stick() + adj;
379 __hbird_write_stick(val);
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return val;
382}
383
David S. Miller112f4872007-03-05 15:28:37 -0800384static int hbtick_add_compare(unsigned long adj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
David S. Miller112f4872007-03-05 15:28:37 -0800386 unsigned long val = __hbird_read_stick();
387 unsigned long val2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
David S. Miller112f4872007-03-05 15:28:37 -0800389 val &= ~TICKCMP_IRQ_BIT;
390 val += adj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 __hbird_write_compare(val);
392
David S. Miller112f4872007-03-05 15:28:37 -0800393 val2 = __hbird_read_stick() & ~TICKCMP_IRQ_BIT;
394
395 return ((long)(val2 - val)) > 0L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Pavel Tatashin89108c32017-06-12 16:41:45 -0400398static unsigned long hbtick_get_frequency(void)
399{
400 struct device_node *dp = of_find_node_by_path("/");
401
402 return of_getintprop_default(dp, "stick-frequency", 0);
403}
404
David S. Millerd369ddd2005-07-10 15:45:11 -0700405static struct sparc64_tick_ops hbtick_operations __read_mostly = {
David S. Miller112f4872007-03-05 15:28:37 -0800406 .name = "hbtick",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 .init_tick = hbtick_init_tick,
David S. Miller112f4872007-03-05 15:28:37 -0800408 .disable_irq = hbtick_disable_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 .get_tick = hbtick_get_tick,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 .add_tick = hbtick_add_tick,
411 .add_compare = hbtick_add_compare,
Pavel Tatashin89108c32017-06-12 16:41:45 -0400412 .get_frequency = hbtick_get_frequency,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 .softint_mask = 1UL << 0,
414};
415
David S. Millerda867832008-08-28 22:16:15 -0700416unsigned long cmos_regs;
417EXPORT_SYMBOL(cmos_regs);
David S. Miller690c8fd2006-06-22 19:12:03 -0700418
David S. Millerd8ada0a2008-09-11 23:39:11 -0700419static struct resource rtc_cmos_resource;
David S. Millerda867832008-08-28 22:16:15 -0700420
421static struct platform_device rtc_cmos_device = {
422 .name = "rtc_cmos",
423 .id = -1,
424 .resource = &rtc_cmos_resource,
425 .num_resources = 1,
426};
David S. Miller690c8fd2006-06-22 19:12:03 -0700427
Greg Kroah-Hartman7c9503b2012-12-21 14:03:26 -0800428static int rtc_probe(struct platform_device *op)
David S. Miller690c8fd2006-06-22 19:12:03 -0700429{
David S. Millerda867832008-08-28 22:16:15 -0700430 struct resource *r;
431
Sam Ravnborg90181132009-01-06 13:19:28 -0800432 printk(KERN_INFO "%s: RTC regs at 0x%llx\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700433 op->dev.of_node->full_name, op->resource[0].start);
David S. Millerda867832008-08-28 22:16:15 -0700434
435 /* The CMOS RTC driver only accepts IORESOURCE_IO, so cons
436 * up a fake resource so that the probe works for all cases.
437 * When the RTC is behind an ISA bus it will have IORESOURCE_IO
438 * already, whereas when it's behind EBUS is will be IORESOURCE_MEM.
439 */
440
441 r = &rtc_cmos_resource;
442 r->flags = IORESOURCE_IO;
443 r->name = op->resource[0].name;
444 r->start = op->resource[0].start;
445 r->end = op->resource[0].end;
446
447 cmos_regs = op->resource[0].start;
448 return platform_device_register(&rtc_cmos_device);
449}
450
David S. Miller3628aa02011-03-30 17:37:56 -0700451static const struct of_device_id rtc_match[] = {
David S. Millerda867832008-08-28 22:16:15 -0700452 {
453 .name = "rtc",
454 .compatible = "m5819",
455 },
456 {
457 .name = "rtc",
458 .compatible = "isa-m5819p",
459 },
460 {
461 .name = "rtc",
462 .compatible = "isa-m5823p",
463 },
464 {
465 .name = "rtc",
466 .compatible = "ds1287",
467 },
468 {},
469};
470
Grant Likely4ebb24f2011-02-22 20:01:33 -0700471static struct platform_driver rtc_driver = {
David S. Millerda867832008-08-28 22:16:15 -0700472 .probe = rtc_probe,
Grant Likely40182942010-04-13 16:13:02 -0700473 .driver = {
474 .name = "rtc",
Grant Likely40182942010-04-13 16:13:02 -0700475 .of_match_table = rtc_match,
David S. Millerda867832008-08-28 22:16:15 -0700476 },
477};
478
David S. Miller29b503f2008-08-28 21:54:34 -0700479static struct platform_device rtc_bq4802_device = {
480 .name = "rtc-bq4802",
481 .id = -1,
482 .num_resources = 1,
483};
484
Greg Kroah-Hartman7c9503b2012-12-21 14:03:26 -0800485static int bq4802_probe(struct platform_device *op)
David S. Millerda867832008-08-28 22:16:15 -0700486{
David S. Miller690c8fd2006-06-22 19:12:03 -0700487
Sam Ravnborg90181132009-01-06 13:19:28 -0800488 printk(KERN_INFO "%s: BQ4802 regs at 0x%llx\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700489 op->dev.of_node->full_name, op->resource[0].start);
David S. Miller690c8fd2006-06-22 19:12:03 -0700490
David S. Miller29b503f2008-08-28 21:54:34 -0700491 rtc_bq4802_device.resource = &op->resource[0];
492 return platform_device_register(&rtc_bq4802_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
David S. Miller3628aa02011-03-30 17:37:56 -0700495static const struct of_device_id bq4802_match[] = {
David S. Milleree5caf02006-06-29 14:36:52 -0700496 {
497 .name = "rtc",
David S. Millerda867832008-08-28 22:16:15 -0700498 .compatible = "bq4802",
David S. Milleree5caf02006-06-29 14:36:52 -0700499 },
David S. Miller770a4242008-10-30 22:58:06 -0700500 {},
David S. Milleree5caf02006-06-29 14:36:52 -0700501};
502
Grant Likely4ebb24f2011-02-22 20:01:33 -0700503static struct platform_driver bq4802_driver = {
David S. Millerda867832008-08-28 22:16:15 -0700504 .probe = bq4802_probe,
Grant Likely40182942010-04-13 16:13:02 -0700505 .driver = {
506 .name = "bq4802",
Grant Likely40182942010-04-13 16:13:02 -0700507 .of_match_table = bq4802_match,
David S. Miller1518e7e2008-08-28 21:06:27 -0700508 },
509};
510
511static unsigned char mostek_read_byte(struct device *dev, u32 ofs)
512{
513 struct platform_device *pdev = to_platform_device(dev);
Krzysztof Helt12a9ee32008-10-29 15:35:24 -0700514 void __iomem *regs = (void __iomem *) pdev->resource[0].start;
David S. Miller1518e7e2008-08-28 21:06:27 -0700515
Krzysztof Helt12a9ee32008-10-29 15:35:24 -0700516 return readb(regs + ofs);
David S. Miller1518e7e2008-08-28 21:06:27 -0700517}
518
519static void mostek_write_byte(struct device *dev, u32 ofs, u8 val)
520{
521 struct platform_device *pdev = to_platform_device(dev);
Krzysztof Helt12a9ee32008-10-29 15:35:24 -0700522 void __iomem *regs = (void __iomem *) pdev->resource[0].start;
David S. Miller1518e7e2008-08-28 21:06:27 -0700523
David S. Miller1518e7e2008-08-28 21:06:27 -0700524 writeb(val, regs + ofs);
525}
526
527static struct m48t59_plat_data m48t59_data = {
528 .read_byte = mostek_read_byte,
529 .write_byte = mostek_write_byte,
530};
531
532static struct platform_device m48t59_rtc = {
533 .name = "rtc-m48t59",
534 .id = 0,
535 .num_resources = 1,
536 .dev = {
537 .platform_data = &m48t59_data,
538 },
539};
540
Greg Kroah-Hartman7c9503b2012-12-21 14:03:26 -0800541static int mostek_probe(struct platform_device *op)
David S. Miller1518e7e2008-08-28 21:06:27 -0700542{
Grant Likely61c7a082010-04-13 16:12:29 -0700543 struct device_node *dp = op->dev.of_node;
David S. Miller1518e7e2008-08-28 21:06:27 -0700544
545 /* On an Enterprise system there can be multiple mostek clocks.
546 * We should only match the one that is on the central FHC bus.
547 */
548 if (!strcmp(dp->parent->name, "fhc") &&
549 strcmp(dp->parent->parent->name, "central") != 0)
550 return -ENODEV;
551
Sam Ravnborg90181132009-01-06 13:19:28 -0800552 printk(KERN_INFO "%s: Mostek regs at 0x%llx\n",
David S. Miller1518e7e2008-08-28 21:06:27 -0700553 dp->full_name, op->resource[0].start);
554
555 m48t59_rtc.resource = &op->resource[0];
556 return platform_device_register(&m48t59_rtc);
557}
558
David S. Miller3628aa02011-03-30 17:37:56 -0700559static const struct of_device_id mostek_match[] = {
David S. Miller1518e7e2008-08-28 21:06:27 -0700560 {
561 .name = "eeprom",
562 },
563 {},
564};
565
Grant Likely4ebb24f2011-02-22 20:01:33 -0700566static struct platform_driver mostek_driver = {
David S. Miller1518e7e2008-08-28 21:06:27 -0700567 .probe = mostek_probe,
Grant Likely40182942010-04-13 16:13:02 -0700568 .driver = {
569 .name = "mostek",
Grant Likely40182942010-04-13 16:13:02 -0700570 .of_match_table = mostek_match,
Stephen Rothwella2cd1552007-10-10 23:27:34 -0700571 },
David S. Milleree5caf02006-06-29 14:36:52 -0700572};
573
David S. Miller84d6bd52008-08-29 01:34:27 -0700574static struct platform_device rtc_sun4v_device = {
575 .name = "rtc-sun4v",
576 .id = -1,
577};
578
David S. Millerf2be6de2008-08-29 01:35:19 -0700579static struct platform_device rtc_starfire_device = {
580 .name = "rtc-starfire",
581 .id = -1,
582};
583
David S. Milleree5caf02006-06-29 14:36:52 -0700584static int __init clock_init(void)
585{
David S. Millerf2be6de2008-08-29 01:35:19 -0700586 if (this_is_starfire)
587 return platform_device_register(&rtc_starfire_device);
588
David S. Miller84d6bd52008-08-29 01:34:27 -0700589 if (tlb_type == hypervisor)
590 return platform_device_register(&rtc_sun4v_device);
David S. Milleree5caf02006-06-29 14:36:52 -0700591
Grant Likely4ebb24f2011-02-22 20:01:33 -0700592 (void) platform_driver_register(&rtc_driver);
593 (void) platform_driver_register(&mostek_driver);
594 (void) platform_driver_register(&bq4802_driver);
David S. Miller1518e7e2008-08-28 21:06:27 -0700595
596 return 0;
David S. Milleree5caf02006-06-29 14:36:52 -0700597}
598
599/* Must be after subsys_initcall() so that busses are probed. Must
600 * be before device_initcall() because things like the RTC driver
601 * need to see the clock registers.
602 */
603fs_initcall(clock_init);
604
Pavel Tatashin89108c32017-06-12 16:41:45 -0400605/* Return true if this is Hummingbird, aka Ultra-IIe */
606static bool is_hummingbird(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Pavel Tatashin89108c32017-06-12 16:41:45 -0400608 unsigned long ver, manuf, impl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Pavel Tatashin89108c32017-06-12 16:41:45 -0400610 __asm__ __volatile__ ("rdpr %%ver, %0"
611 : "=&r" (ver));
612 manuf = ((ver >> 48) & 0xffff);
613 impl = ((ver >> 32) & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Pavel Tatashin89108c32017-06-12 16:41:45 -0400615 return (manuf == 0x17 && impl == 0x13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618struct freq_table {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 unsigned long clock_tick_ref;
620 unsigned int ref_freq;
621};
David S. Miller3763be32006-02-17 12:33:13 -0800622static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624unsigned long sparc64_get_clock_tick(unsigned int cpu)
625{
626 struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
627
628 if (ft->clock_tick_ref)
629 return ft->clock_tick_ref;
630 return cpu_data(cpu).clock_tick;
631}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800632EXPORT_SYMBOL(sparc64_get_clock_tick);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634#ifdef CONFIG_CPU_FREQ
635
636static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
637 void *data)
638{
639 struct cpufreq_freqs *freq = data;
640 unsigned int cpu = freq->cpu;
641 struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
642
643 if (!ft->ref_freq) {
644 ft->ref_freq = freq->old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 ft->clock_tick_ref = cpu_data(cpu).clock_tick;
646 }
647 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
Viresh Kumar0b443ea2014-03-19 11:24:58 +0530648 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 cpu_data(cpu).clock_tick =
650 cpufreq_scale(ft->clock_tick_ref,
651 ft->ref_freq,
652 freq->new);
653 }
654
655 return 0;
656}
657
658static struct notifier_block sparc64_cpufreq_notifier_block = {
659 .notifier_call = sparc64_cpufreq_notifier
660};
661
David S. Miller7ae93f52008-07-23 16:21:07 -0700662static int __init register_sparc64_cpufreq_notifier(void)
663{
664
665 cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
666 CPUFREQ_TRANSITION_NOTIFIER);
667 return 0;
668}
669
670core_initcall(register_sparc64_cpufreq_notifier);
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672#endif /* CONFIG_CPU_FREQ */
673
David S. Miller112f4872007-03-05 15:28:37 -0800674static int sparc64_next_event(unsigned long delta,
675 struct clock_event_device *evt)
676{
Pavel Tatashinb8a83fc2017-06-12 16:41:42 -0400677 return tick_operations.add_compare(delta) ? -ETIME : 0;
David S. Miller112f4872007-03-05 15:28:37 -0800678}
679
Viresh Kumarff4aea42015-07-16 16:56:29 +0530680static int sparc64_timer_shutdown(struct clock_event_device *evt)
David S. Miller112f4872007-03-05 15:28:37 -0800681{
Pavel Tatashinb8a83fc2017-06-12 16:41:42 -0400682 tick_operations.disable_irq();
Viresh Kumarff4aea42015-07-16 16:56:29 +0530683 return 0;
David S. Miller112f4872007-03-05 15:28:37 -0800684}
685
686static struct clock_event_device sparc64_clockevent = {
Viresh Kumarff4aea42015-07-16 16:56:29 +0530687 .features = CLOCK_EVT_FEAT_ONESHOT,
688 .set_state_shutdown = sparc64_timer_shutdown,
689 .set_next_event = sparc64_next_event,
690 .rating = 100,
691 .shift = 30,
692 .irq = -1,
David S. Miller112f4872007-03-05 15:28:37 -0800693};
694static DEFINE_PER_CPU(struct clock_event_device, sparc64_events);
695
David S. Miller9960e9e2010-04-07 04:41:33 -0700696void __irq_entry timer_interrupt(int irq, struct pt_regs *regs)
David S. Miller112f4872007-03-05 15:28:37 -0800697{
698 struct pt_regs *old_regs = set_irq_regs(regs);
Pavel Tatashinb8a83fc2017-06-12 16:41:42 -0400699 unsigned long tick_mask = tick_operations.softint_mask;
David S. Miller112f4872007-03-05 15:28:37 -0800700 int cpu = smp_processor_id();
701 struct clock_event_device *evt = &per_cpu(sparc64_events, cpu);
702
703 clear_softint(tick_mask);
704
705 irq_enter();
706
David S. Millerdaecbf52010-04-06 17:38:52 -0700707 local_cpu_data().irq0_irqs++;
Thomas Gleixner87a69ad2014-02-23 21:40:15 +0000708 kstat_incr_irq_this_cpu(0);
David S. Miller112f4872007-03-05 15:28:37 -0800709
710 if (unlikely(!evt->event_handler)) {
711 printk(KERN_WARNING
712 "Spurious SPARC64 timer interrupt on cpu %d\n", cpu);
713 } else
714 evt->event_handler(evt);
715
716 irq_exit();
717
718 set_irq_regs(old_regs);
719}
720
Greg Kroah-Hartman7c9503b2012-12-21 14:03:26 -0800721void setup_sparc64_timer(void)
David S. Miller112f4872007-03-05 15:28:37 -0800722{
723 struct clock_event_device *sevt;
724 unsigned long pstate;
725
726 /* Guarantee that the following sequences execute
727 * uninterrupted.
728 */
729 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
730 "wrpr %0, %1, %%pstate"
731 : "=r" (pstate)
732 : "i" (PSTATE_IE));
733
Pavel Tatashinb8a83fc2017-06-12 16:41:42 -0400734 tick_operations.init_tick();
David S. Miller112f4872007-03-05 15:28:37 -0800735
736 /* Restore PSTATE_IE. */
737 __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
738 : /* no outputs */
739 : "r" (pstate));
740
Christoph Lameter494fc422014-08-17 12:30:54 -0500741 sevt = this_cpu_ptr(&sparc64_events);
David S. Miller112f4872007-03-05 15:28:37 -0800742
743 memcpy(sevt, &sparc64_clockevent, sizeof(*sevt));
Rusty Russell320ab2b2008-12-13 21:20:26 +1030744 sevt->cpumask = cpumask_of(smp_processor_id());
David S. Miller112f4872007-03-05 15:28:37 -0800745
746 clockevents_register_device(sevt);
747}
748
David S. Miller03983ab2007-05-17 22:55:26 -0700749#define SPARC64_NSEC_PER_CYC_SHIFT 10UL
David S. Miller112f4872007-03-05 15:28:37 -0800750
751static struct clocksource clocksource_tick = {
752 .rating = 100,
753 .mask = CLOCKSOURCE_MASK(64),
David S. Miller112f4872007-03-05 15:28:37 -0800754 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755};
756
David S. Miller8b99cfb2007-07-14 02:23:37 -0700757static unsigned long tb_ticks_per_usec __read_mostly;
758
759void __delay(unsigned long loops)
760{
761 unsigned long bclock, now;
762
Pavel Tatashinb8a83fc2017-06-12 16:41:42 -0400763 bclock = tick_operations.get_tick();
David S. Miller8b99cfb2007-07-14 02:23:37 -0700764 do {
Pavel Tatashinb8a83fc2017-06-12 16:41:42 -0400765 now = tick_operations.get_tick();
David S. Miller8b99cfb2007-07-14 02:23:37 -0700766 } while ((now-bclock) < loops);
767}
768EXPORT_SYMBOL(__delay);
769
770void udelay(unsigned long usecs)
771{
772 __delay(tb_ticks_per_usec * usecs);
773}
774EXPORT_SYMBOL(udelay);
775
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100776static u64 clocksource_tick_read(struct clocksource *cs)
Magnus Damm8e196082009-04-21 12:24:00 -0700777{
Pavel Tatashinb8a83fc2017-06-12 16:41:42 -0400778 return tick_operations.get_tick();
Magnus Damm8e196082009-04-21 12:24:00 -0700779}
780
Pavel Tatashin89108c32017-06-12 16:41:45 -0400781static void init_tick_ops(struct sparc64_tick_ops *ops)
782{
783 unsigned long freq, quotient, tick;
784
785 freq = ops->get_frequency();
786 quotient = clocksource_hz2mult(freq, SPARC64_NSEC_PER_CYC_SHIFT);
787 tick = ops->get_tick();
788
789 ops->offset = (tick * quotient) >> SPARC64_NSEC_PER_CYC_SHIFT;
790 ops->ticks_per_nsec_quotient = quotient;
791 ops->frequency = freq;
792 tick_operations = *ops;
793}
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795void __init time_init(void)
796{
Pavel Tatashin89108c32017-06-12 16:41:45 -0400797 unsigned long freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Pavel Tatashin89108c32017-06-12 16:41:45 -0400799 if (tlb_type == spitfire) {
800 if (is_hummingbird())
801 init_tick_ops(&hbtick_operations);
802 else
803 init_tick_ops(&tick_operations);
804 } else {
805 init_tick_ops(&stick_operations);
806 }
807
808 freq = tick_operations.frequency;
David S. Millerd8ada0a2008-09-11 23:39:11 -0700809 tb_ticks_per_usec = freq / USEC_PER_SEC;
David S. Miller8b99cfb2007-07-14 02:23:37 -0700810
Pavel Tatashin178bf2b2017-06-12 16:41:44 -0400811 tick_operations.ticks_per_nsec_quotient =
David S. Millerd8ada0a2008-09-11 23:39:11 -0700812 clocksource_hz2mult(freq, SPARC64_NSEC_PER_CYC_SHIFT);
David S. Miller112f4872007-03-05 15:28:37 -0800813
Pavel Tatashin178bf2b2017-06-12 16:41:44 -0400814 tick_operations.offset = (tick_operations.get_tick()
815 * tick_operations.ticks_per_nsec_quotient)
Pavel Tatashinb5dd4d82017-06-12 16:41:43 -0400816 >> SPARC64_NSEC_PER_CYC_SHIFT;
817
Pavel Tatashinb8a83fc2017-06-12 16:41:42 -0400818 clocksource_tick.name = tick_operations.name;
Magnus Damm8e196082009-04-21 12:24:00 -0700819 clocksource_tick.read = clocksource_tick_read;
David S. Miller112f4872007-03-05 15:28:37 -0800820
John Stultz81043e82011-02-23 13:08:21 -0800821 clocksource_register_hz(&clocksource_tick, freq);
David S. Miller112f4872007-03-05 15:28:37 -0800822 printk("clocksource: mult[%x] shift[%d]\n",
823 clocksource_tick.mult, clocksource_tick.shift);
824
Pavel Tatashinb8a83fc2017-06-12 16:41:42 -0400825 sparc64_clockevent.name = tick_operations.name;
David S. Miller6865b7f2009-12-11 01:27:29 -0800826 clockevents_calc_mult_shift(&sparc64_clockevent, freq, 4);
David S. Miller112f4872007-03-05 15:28:37 -0800827
828 sparc64_clockevent.max_delta_ns =
David S. Millercf3d7c12008-03-26 01:11:55 -0700829 clockevent_delta2ns(0x7fffffffffffffffUL, &sparc64_clockevent);
Nicolai Stange7fd53422017-03-30 21:56:47 +0200830 sparc64_clockevent.max_delta_ticks = 0x7fffffffffffffffUL;
David S. Miller112f4872007-03-05 15:28:37 -0800831 sparc64_clockevent.min_delta_ns =
832 clockevent_delta2ns(0xF, &sparc64_clockevent);
Nicolai Stange7fd53422017-03-30 21:56:47 +0200833 sparc64_clockevent.min_delta_ticks = 0xF;
David S. Miller112f4872007-03-05 15:28:37 -0800834
David S. Miller7466bd32009-12-11 02:05:05 -0800835 printk("clockevent: mult[%x] shift[%d]\n",
David S. Miller112f4872007-03-05 15:28:37 -0800836 sparc64_clockevent.mult, sparc64_clockevent.shift);
837
838 setup_sparc64_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841unsigned long long sched_clock(void)
842{
Pavel Tatashin178bf2b2017-06-12 16:41:44 -0400843 unsigned long quotient = tick_operations.ticks_per_nsec_quotient;
844 unsigned long offset = tick_operations.offset;
Pavel Tatashinb8a83fc2017-06-12 16:41:42 -0400845 unsigned long ticks = tick_operations.get_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Pavel Tatashin178bf2b2017-06-12 16:41:44 -0400847 return ((ticks * quotient) >> SPARC64_NSEC_PER_CYC_SHIFT) - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Greg Kroah-Hartman7c9503b2012-12-21 14:03:26 -0800850int read_current_timer(unsigned long *timer_val)
Andrew Morton941e4922008-02-06 01:36:42 -0800851{
Pavel Tatashinb8a83fc2017-06-12 16:41:42 -0400852 *timer_val = tick_operations.get_tick();
Andrew Morton941e4922008-02-06 01:36:42 -0800853 return 0;
854}