blob: abdeead4e5b99ae2a0b158e39a2a0472b0b2b325 [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>
12#include <linux/module.h>
13#include <linux/sched.h>
Arnd Bergmann09de3612008-05-20 19:16:50 +020014#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kernel.h>
16#include <linux/param.h>
17#include <linux/string.h>
18#include <linux/mm.h>
19#include <linux/interrupt.h>
20#include <linux/time.h>
21#include <linux/timex.h>
22#include <linux/init.h>
23#include <linux/ioport.h>
24#include <linux/mc146818rtc.h>
25#include <linux/delay.h>
26#include <linux/profile.h>
27#include <linux/bcd.h>
28#include <linux/jiffies.h>
29#include <linux/cpufreq.h>
30#include <linux/percpu.h>
David S. Miller8ba706a2006-03-01 17:32:46 -080031#include <linux/miscdevice.h>
32#include <linux/rtc.h>
David S. Miller1518e7e2008-08-28 21:06:27 -070033#include <linux/rtc/m48t59.h>
David S. Miller777a4472007-02-22 06:24:10 -080034#include <linux/kernel_stat.h>
David S. Miller112f4872007-03-05 15:28:37 -080035#include <linux/clockchips.h>
36#include <linux/clocksource.h>
Stephen Rothwell764f2572008-08-07 15:33:36 -070037#include <linux/of_device.h>
David S. Miller1518e7e2008-08-28 21:06:27 -070038#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/oplib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/timer.h>
42#include <asm/irq.h>
43#include <asm/io.h>
David S. Millerff0d2fc2006-06-29 15:28:05 -070044#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/starfire.h>
46#include <asm/smp.h>
47#include <asm/sections.h>
48#include <asm/cpudata.h>
David S. Miller8ba706a2006-03-01 17:32:46 -080049#include <asm/uaccess.h>
Al Viro63540ba2006-10-09 11:51:14 +010050#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
David S. Millercf3d7c12008-03-26 01:11:55 -070052#include "entry.h"
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054DEFINE_SPINLOCK(rtc_lock);
David S. Millerd037e052007-05-11 21:18:50 -070055static void __iomem *bq4802_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static int set_rtc_mmss(unsigned long);
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define TICK_PRIV_BIT (1UL << 63)
David S. Miller112f4872007-03-05 15:28:37 -080060#define TICKCMP_IRQ_BIT (1UL << 63)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#ifdef CONFIG_SMP
63unsigned long profile_pc(struct pt_regs *regs)
64{
65 unsigned long pc = instruction_pointer(regs);
66
67 if (in_lock_functions(pc))
68 return regs->u_regs[UREG_RETPC];
69 return pc;
70}
71EXPORT_SYMBOL(profile_pc);
72#endif
73
74static void tick_disable_protection(void)
75{
76 /* Set things up so user can access tick register for profiling
77 * purposes. Also workaround BB_ERRATA_1 by doing a dummy
78 * read back of %tick after writing it.
79 */
80 __asm__ __volatile__(
81 " ba,pt %%xcc, 1f\n"
82 " nop\n"
83 " .align 64\n"
84 "1: rd %%tick, %%g2\n"
85 " add %%g2, 6, %%g2\n"
86 " andn %%g2, %0, %%g2\n"
87 " wrpr %%g2, 0, %%tick\n"
88 " rdpr %%tick, %%g0"
89 : /* no outputs */
90 : "r" (TICK_PRIV_BIT)
91 : "g2");
92}
93
David S. Miller112f4872007-03-05 15:28:37 -080094static void tick_disable_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 __asm__ __volatile__(
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 " ba,pt %%xcc, 1f\n"
David S. Miller112f4872007-03-05 15:28:37 -080098 " nop\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 " .align 64\n"
David S. Miller112f4872007-03-05 15:28:37 -0800100 "1: wr %0, 0x0, %%tick_cmpr\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 " rd %%tick_cmpr, %%g0"
102 : /* no outputs */
David S. Miller112f4872007-03-05 15:28:37 -0800103 : "r" (TICKCMP_IRQ_BIT));
104}
105
106static void tick_init_tick(void)
107{
108 tick_disable_protection();
109 tick_disable_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112static unsigned long tick_get_tick(void)
113{
114 unsigned long ret;
115
116 __asm__ __volatile__("rd %%tick, %0\n\t"
117 "mov %0, %0"
118 : "=r" (ret));
119
120 return ret & ~TICK_PRIV_BIT;
121}
122
David S. Miller112f4872007-03-05 15:28:37 -0800123static int tick_add_compare(unsigned long adj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
David S. Miller112f4872007-03-05 15:28:37 -0800125 unsigned long orig_tick, new_tick, new_compare;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
David S. Miller112f4872007-03-05 15:28:37 -0800127 __asm__ __volatile__("rd %%tick, %0"
128 : "=r" (orig_tick));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
David S. Miller112f4872007-03-05 15:28:37 -0800130 orig_tick &= ~TICKCMP_IRQ_BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 /* Workaround for Spitfire Errata (#54 I think??), I discovered
133 * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch
134 * number 103640.
135 *
136 * On Blackbird writes to %tick_cmpr can fail, the
137 * workaround seems to be to execute the wr instruction
138 * at the start of an I-cache line, and perform a dummy
139 * read back from %tick_cmpr right after writing to it. -DaveM
140 */
David S. Miller112f4872007-03-05 15:28:37 -0800141 __asm__ __volatile__("ba,pt %%xcc, 1f\n\t"
142 " add %1, %2, %0\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 ".align 64\n"
144 "1:\n\t"
145 "wr %0, 0, %%tick_cmpr\n\t"
David S. Miller112f4872007-03-05 15:28:37 -0800146 "rd %%tick_cmpr, %%g0\n\t"
147 : "=r" (new_compare)
148 : "r" (orig_tick), "r" (adj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
David S. Miller112f4872007-03-05 15:28:37 -0800150 __asm__ __volatile__("rd %%tick, %0"
151 : "=r" (new_tick));
152 new_tick &= ~TICKCMP_IRQ_BIT;
153
154 return ((long)(new_tick - (orig_tick+adj))) > 0L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
David S. Miller112f4872007-03-05 15:28:37 -0800157static unsigned long tick_add_tick(unsigned long adj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
David S. Miller112f4872007-03-05 15:28:37 -0800159 unsigned long new_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 /* Also need to handle Blackbird bug here too. */
162 __asm__ __volatile__("rd %%tick, %0\n\t"
David S. Miller112f4872007-03-05 15:28:37 -0800163 "add %0, %1, %0\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 "wrpr %0, 0, %%tick\n\t"
David S. Miller112f4872007-03-05 15:28:37 -0800165 : "=&r" (new_tick)
166 : "r" (adj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 return new_tick;
169}
170
David S. Millerd369ddd2005-07-10 15:45:11 -0700171static struct sparc64_tick_ops tick_operations __read_mostly = {
David S. Miller112f4872007-03-05 15:28:37 -0800172 .name = "tick",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 .init_tick = tick_init_tick,
David S. Miller112f4872007-03-05 15:28:37 -0800174 .disable_irq = tick_disable_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 .get_tick = tick_get_tick,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .add_tick = tick_add_tick,
177 .add_compare = tick_add_compare,
178 .softint_mask = 1UL << 0,
179};
180
David S. Millerfc321492005-11-07 14:10:10 -0800181struct sparc64_tick_ops *tick_ops __read_mostly = &tick_operations;
182
David S. Miller112f4872007-03-05 15:28:37 -0800183static void stick_disable_irq(void)
184{
185 __asm__ __volatile__(
186 "wr %0, 0x0, %%asr25"
187 : /* no outputs */
188 : "r" (TICKCMP_IRQ_BIT));
189}
190
191static void stick_init_tick(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
David S. Miller7aa62642006-02-11 23:14:59 -0800193 /* Writes to the %tick and %stick register are not
194 * allowed on sun4v. The Hypervisor controls that
195 * bit, per-strand.
196 */
197 if (tlb_type != hypervisor) {
198 tick_disable_protection();
David S. Miller112f4872007-03-05 15:28:37 -0800199 tick_disable_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
David S. Miller7aa62642006-02-11 23:14:59 -0800201 /* Let the user get at STICK too. */
202 __asm__ __volatile__(
203 " rd %%asr24, %%g2\n"
204 " andn %%g2, %0, %%g2\n"
205 " wr %%g2, 0, %%asr24"
206 : /* no outputs */
207 : "r" (TICK_PRIV_BIT)
208 : "g1", "g2");
209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
David S. Miller112f4872007-03-05 15:28:37 -0800211 stick_disable_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214static unsigned long stick_get_tick(void)
215{
216 unsigned long ret;
217
218 __asm__ __volatile__("rd %%asr24, %0"
219 : "=r" (ret));
220
221 return ret & ~TICK_PRIV_BIT;
222}
223
David S. Miller112f4872007-03-05 15:28:37 -0800224static unsigned long stick_add_tick(unsigned long adj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
David S. Miller112f4872007-03-05 15:28:37 -0800226 unsigned long new_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 __asm__ __volatile__("rd %%asr24, %0\n\t"
David S. Miller112f4872007-03-05 15:28:37 -0800229 "add %0, %1, %0\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 "wr %0, 0, %%asr24\n\t"
David S. Miller112f4872007-03-05 15:28:37 -0800231 : "=&r" (new_tick)
232 : "r" (adj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 return new_tick;
235}
236
David S. Miller112f4872007-03-05 15:28:37 -0800237static int stick_add_compare(unsigned long adj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
David S. Miller112f4872007-03-05 15:28:37 -0800239 unsigned long orig_tick, new_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
David S. Miller112f4872007-03-05 15:28:37 -0800241 __asm__ __volatile__("rd %%asr24, %0"
242 : "=r" (orig_tick));
243 orig_tick &= ~TICKCMP_IRQ_BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
David S. Miller112f4872007-03-05 15:28:37 -0800245 __asm__ __volatile__("wr %0, 0, %%asr25"
246 : /* no outputs */
247 : "r" (orig_tick + adj));
248
249 __asm__ __volatile__("rd %%asr24, %0"
250 : "=r" (new_tick));
251 new_tick &= ~TICKCMP_IRQ_BIT;
252
253 return ((long)(new_tick - (orig_tick+adj))) > 0L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
David S. Millerd369ddd2005-07-10 15:45:11 -0700256static struct sparc64_tick_ops stick_operations __read_mostly = {
David S. Miller112f4872007-03-05 15:28:37 -0800257 .name = "stick",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 .init_tick = stick_init_tick,
David S. Miller112f4872007-03-05 15:28:37 -0800259 .disable_irq = stick_disable_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .get_tick = stick_get_tick,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 .add_tick = stick_add_tick,
262 .add_compare = stick_add_compare,
263 .softint_mask = 1UL << 16,
264};
265
266/* On Hummingbird the STICK/STICK_CMPR register is implemented
267 * in I/O space. There are two 64-bit registers each, the
268 * first holds the low 32-bits of the value and the second holds
269 * the high 32-bits.
270 *
271 * Since STICK is constantly updating, we have to access it carefully.
272 *
273 * The sequence we use to read is:
Richard Mortimer9eb33942006-01-17 15:21:01 -0800274 * 1) read high
275 * 2) read low
276 * 3) read high again, if it rolled re-read both low and high again.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 *
278 * Writing STICK safely is also tricky:
279 * 1) write low to zero
280 * 2) write high
281 * 3) write low
282 */
283#define HBIRD_STICKCMP_ADDR 0x1fe0000f060UL
284#define HBIRD_STICK_ADDR 0x1fe0000f070UL
285
286static unsigned long __hbird_read_stick(void)
287{
288 unsigned long ret, tmp1, tmp2, tmp3;
Richard Mortimer9eb33942006-01-17 15:21:01 -0800289 unsigned long addr = HBIRD_STICK_ADDR+8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Richard Mortimer9eb33942006-01-17 15:21:01 -0800291 __asm__ __volatile__("ldxa [%1] %5, %2\n"
292 "1:\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 "sub %1, 0x8, %1\n\t"
Richard Mortimer9eb33942006-01-17 15:21:01 -0800294 "ldxa [%1] %5, %3\n\t"
295 "add %1, 0x8, %1\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 "ldxa [%1] %5, %4\n\t"
297 "cmp %4, %2\n\t"
Richard Mortimer9eb33942006-01-17 15:21:01 -0800298 "bne,a,pn %%xcc, 1b\n\t"
299 " mov %4, %2\n\t"
300 "sllx %4, 32, %4\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 "or %3, %4, %0\n\t"
302 : "=&r" (ret), "=&r" (addr),
303 "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
304 : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr));
305
306 return ret;
307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309static void __hbird_write_stick(unsigned long val)
310{
311 unsigned long low = (val & 0xffffffffUL);
312 unsigned long high = (val >> 32UL);
313 unsigned long addr = HBIRD_STICK_ADDR;
314
315 __asm__ __volatile__("stxa %%g0, [%0] %4\n\t"
316 "add %0, 0x8, %0\n\t"
317 "stxa %3, [%0] %4\n\t"
318 "sub %0, 0x8, %0\n\t"
319 "stxa %2, [%0] %4"
320 : "=&r" (addr)
321 : "0" (addr), "r" (low), "r" (high),
322 "i" (ASI_PHYS_BYPASS_EC_E));
323}
324
325static void __hbird_write_compare(unsigned long val)
326{
327 unsigned long low = (val & 0xffffffffUL);
328 unsigned long high = (val >> 32UL);
329 unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL;
330
331 __asm__ __volatile__("stxa %3, [%0] %4\n\t"
332 "sub %0, 0x8, %0\n\t"
333 "stxa %2, [%0] %4"
334 : "=&r" (addr)
335 : "0" (addr), "r" (low), "r" (high),
336 "i" (ASI_PHYS_BYPASS_EC_E));
337}
338
David S. Miller112f4872007-03-05 15:28:37 -0800339static void hbtick_disable_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
David S. Miller112f4872007-03-05 15:28:37 -0800341 __hbird_write_compare(TICKCMP_IRQ_BIT);
342}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
David S. Miller112f4872007-03-05 15:28:37 -0800344static void hbtick_init_tick(void)
345{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 tick_disable_protection();
347
348 /* XXX This seems to be necessary to 'jumpstart' Hummingbird
349 * XXX into actually sending STICK interrupts. I think because
350 * XXX of how we store %tick_cmpr in head.S this somehow resets the
351 * XXX {TICK + STICK} interrupt mux. -DaveM
352 */
353 __hbird_write_stick(__hbird_read_stick());
354
David S. Miller112f4872007-03-05 15:28:37 -0800355 hbtick_disable_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
358static unsigned long hbtick_get_tick(void)
359{
360 return __hbird_read_stick() & ~TICK_PRIV_BIT;
361}
362
David S. Miller112f4872007-03-05 15:28:37 -0800363static unsigned long hbtick_add_tick(unsigned long adj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 unsigned long val;
366
367 val = __hbird_read_stick() + adj;
368 __hbird_write_stick(val);
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return val;
371}
372
David S. Miller112f4872007-03-05 15:28:37 -0800373static int hbtick_add_compare(unsigned long adj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
David S. Miller112f4872007-03-05 15:28:37 -0800375 unsigned long val = __hbird_read_stick();
376 unsigned long val2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
David S. Miller112f4872007-03-05 15:28:37 -0800378 val &= ~TICKCMP_IRQ_BIT;
379 val += adj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 __hbird_write_compare(val);
381
David S. Miller112f4872007-03-05 15:28:37 -0800382 val2 = __hbird_read_stick() & ~TICKCMP_IRQ_BIT;
383
384 return ((long)(val2 - val)) > 0L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
David S. Millerd369ddd2005-07-10 15:45:11 -0700387static struct sparc64_tick_ops hbtick_operations __read_mostly = {
David S. Miller112f4872007-03-05 15:28:37 -0800388 .name = "hbtick",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 .init_tick = hbtick_init_tick,
David S. Miller112f4872007-03-05 15:28:37 -0800390 .disable_irq = hbtick_disable_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 .get_tick = hbtick_get_tick,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 .add_tick = hbtick_add_tick,
393 .add_compare = hbtick_add_compare,
394 .softint_mask = 1UL << 0,
395};
396
David S. Millerd369ddd2005-07-10 15:45:11 -0700397static unsigned long timer_ticks_per_nsec_quotient __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Thomas Gleixner82644452007-07-21 04:37:37 -0700399int update_persistent_clock(struct timespec now)
David S. Millera58c9f32007-02-22 04:16:21 -0800400{
David S. Millera0b31b52008-08-28 17:34:31 -0700401 struct rtc_device *rtc = rtc_class_open("rtc0");
402
403 if (rtc)
404 return rtc_set_mmss(rtc, now.tv_sec);
405
Thomas Gleixner82644452007-07-21 04:37:37 -0700406 return set_rtc_mmss(now.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/* Probe for the real time clock chip. */
410static void __init set_system_time(void)
411{
412 unsigned int year, mon, day, hour, min, sec;
David S. Millerd037e052007-05-11 21:18:50 -0700413 void __iomem *bregs = bq4802_regs;
David S. Millerda867832008-08-28 22:16:15 -0700414 unsigned char val = readb(bregs + 0x0e);
415 unsigned int century;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
David S. Millerda867832008-08-28 22:16:15 -0700417 if (!bregs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 prom_printf("Something wrong, clock regs not mapped yet.\n");
419 prom_halt();
420 }
421
David S. Millerda867832008-08-28 22:16:15 -0700422 /* BQ4802 RTC chip. */
David S. Millerd037e052007-05-11 21:18:50 -0700423
David S. Millerda867832008-08-28 22:16:15 -0700424 writeb(val | 0x08, bregs + 0x0e);
David S. Millerd037e052007-05-11 21:18:50 -0700425
David S. Millerda867832008-08-28 22:16:15 -0700426 sec = readb(bregs + 0x00);
427 min = readb(bregs + 0x02);
428 hour = readb(bregs + 0x04);
429 day = readb(bregs + 0x06);
430 mon = readb(bregs + 0x09);
431 year = readb(bregs + 0x0a);
432 century = readb(bregs + 0x0f);
David S. Millerd037e052007-05-11 21:18:50 -0700433
David S. Millerda867832008-08-28 22:16:15 -0700434 writeb(val, bregs + 0x0e);
David S. Millerd037e052007-05-11 21:18:50 -0700435
David S. Millerda867832008-08-28 22:16:15 -0700436 BCD_TO_BIN(sec);
437 BCD_TO_BIN(min);
438 BCD_TO_BIN(hour);
439 BCD_TO_BIN(day);
440 BCD_TO_BIN(mon);
441 BCD_TO_BIN(year);
442 BCD_TO_BIN(century);
David S. Millerd037e052007-05-11 21:18:50 -0700443
David S. Millerda867832008-08-28 22:16:15 -0700444 year += (century * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
447 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
448 set_normalized_timespec(&wall_to_monotonic,
449 -xtime.tv_sec, -xtime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
David S. Miller4bdff412006-02-11 01:01:55 -0800452/* davem suggests we keep this within the 4M locked kernel image */
453static u32 starfire_get_time(void)
454{
455 static char obp_gettod[32];
456 static u32 unix_tod;
457
458 sprintf(obp_gettod, "h# %08x unix-gettod",
459 (unsigned int) (long) &unix_tod);
460 prom_feval(obp_gettod);
461
462 return unix_tod;
463}
464
David S. Miller8ba706a2006-03-01 17:32:46 -0800465static int starfire_set_time(u32 val)
466{
467 /* Do nothing, time is set using the service processor
468 * console on this platform.
469 */
470 return 0;
471}
472
David S. Miller4bdff412006-02-11 01:01:55 -0800473static u32 hypervisor_get_time(void)
474{
David S. Miller7db35f32007-05-29 02:22:14 -0700475 unsigned long ret, time;
David S. Miller4bdff412006-02-11 01:01:55 -0800476 int retries = 10000;
477
478retry:
David S. Miller7db35f32007-05-29 02:22:14 -0700479 ret = sun4v_tod_get(&time);
480 if (ret == HV_EOK)
481 return time;
482 if (ret == HV_EWOULDBLOCK) {
David S. Miller4bdff412006-02-11 01:01:55 -0800483 if (--retries > 0) {
484 udelay(100);
485 goto retry;
486 }
487 printk(KERN_WARNING "SUN4V: tod_get() timed out.\n");
488 return 0;
489 }
490 printk(KERN_WARNING "SUN4V: tod_get() not supported.\n");
491 return 0;
492}
493
David S. Miller8ba706a2006-03-01 17:32:46 -0800494static int hypervisor_set_time(u32 secs)
495{
David S. Miller7db35f32007-05-29 02:22:14 -0700496 unsigned long ret;
David S. Miller8ba706a2006-03-01 17:32:46 -0800497 int retries = 10000;
498
499retry:
David S. Miller7db35f32007-05-29 02:22:14 -0700500 ret = sun4v_tod_set(secs);
501 if (ret == HV_EOK)
David S. Miller8ba706a2006-03-01 17:32:46 -0800502 return 0;
David S. Miller7db35f32007-05-29 02:22:14 -0700503 if (ret == HV_EWOULDBLOCK) {
David S. Miller8ba706a2006-03-01 17:32:46 -0800504 if (--retries > 0) {
505 udelay(100);
506 goto retry;
507 }
508 printk(KERN_WARNING "SUN4V: tod_set() timed out.\n");
509 return -EAGAIN;
510 }
511 printk(KERN_WARNING "SUN4V: tod_set() not supported.\n");
512 return -EOPNOTSUPP;
513}
514
David S. Millerda867832008-08-28 22:16:15 -0700515unsigned long cmos_regs;
516EXPORT_SYMBOL(cmos_regs);
David S. Miller690c8fd2006-06-22 19:12:03 -0700517
David S. Millerda867832008-08-28 22:16:15 -0700518struct resource rtc_cmos_resource;
519
520static struct platform_device rtc_cmos_device = {
521 .name = "rtc_cmos",
522 .id = -1,
523 .resource = &rtc_cmos_resource,
524 .num_resources = 1,
525};
David S. Miller690c8fd2006-06-22 19:12:03 -0700526
David S. Miller1518e7e2008-08-28 21:06:27 -0700527static int __devinit rtc_probe(struct of_device *op, const struct of_device_id *match)
David S. Miller690c8fd2006-06-22 19:12:03 -0700528{
David S. Millerda867832008-08-28 22:16:15 -0700529 struct resource *r;
530
531 printk(KERN_INFO "%s: RTC regs at 0x%lx\n",
532 op->node->full_name, op->resource[0].start);
533
534 /* The CMOS RTC driver only accepts IORESOURCE_IO, so cons
535 * up a fake resource so that the probe works for all cases.
536 * When the RTC is behind an ISA bus it will have IORESOURCE_IO
537 * already, whereas when it's behind EBUS is will be IORESOURCE_MEM.
538 */
539
540 r = &rtc_cmos_resource;
541 r->flags = IORESOURCE_IO;
542 r->name = op->resource[0].name;
543 r->start = op->resource[0].start;
544 r->end = op->resource[0].end;
545
546 cmos_regs = op->resource[0].start;
547 return platform_device_register(&rtc_cmos_device);
548}
549
550static struct of_device_id rtc_match[] = {
551 {
552 .name = "rtc",
553 .compatible = "m5819",
554 },
555 {
556 .name = "rtc",
557 .compatible = "isa-m5819p",
558 },
559 {
560 .name = "rtc",
561 .compatible = "isa-m5823p",
562 },
563 {
564 .name = "rtc",
565 .compatible = "ds1287",
566 },
567 {},
568};
569
570static struct of_platform_driver rtc_driver = {
571 .match_table = rtc_match,
572 .probe = rtc_probe,
573 .driver = {
574 .name = "rtc",
575 },
576};
577
578static int __devinit bq4802_probe(struct of_device *op, const struct of_device_id *match)
579{
David S. Milleree5caf02006-06-29 14:36:52 -0700580 struct device_node *dp = op->node;
David S. Millerda867832008-08-28 22:16:15 -0700581 unsigned long flags;
David S. Miller690c8fd2006-06-22 19:12:03 -0700582
David S. Millerda867832008-08-28 22:16:15 -0700583 bq4802_regs = of_ioremap(&op->resource[0], 0, resource_size(&op->resource[0]), "bq4802");
584 if (!bq4802_regs)
David S. Milleree5caf02006-06-29 14:36:52 -0700585 return -ENOMEM;
David S. Miller690c8fd2006-06-22 19:12:03 -0700586
David S. Millerda867832008-08-28 22:16:15 -0700587 printk(KERN_INFO "%s: Clock regs at %p\n", dp->full_name, bq4802_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
David S. Miller690c8fd2006-06-22 19:12:03 -0700589 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 set_system_time();
592
593 local_irq_restore(flags);
David S. Milleree5caf02006-06-29 14:36:52 -0700594
595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
David S. Millerda867832008-08-28 22:16:15 -0700598static struct of_device_id bq4802_match[] = {
David S. Milleree5caf02006-06-29 14:36:52 -0700599 {
600 .name = "rtc",
David S. Millerda867832008-08-28 22:16:15 -0700601 .compatible = "bq4802",
David S. Milleree5caf02006-06-29 14:36:52 -0700602 },
David S. Milleree5caf02006-06-29 14:36:52 -0700603};
604
David S. Millerda867832008-08-28 22:16:15 -0700605static struct of_platform_driver bq4802_driver = {
606 .match_table = bq4802_match,
607 .probe = bq4802_probe,
Stephen Rothwella2cd1552007-10-10 23:27:34 -0700608 .driver = {
David S. Millerda867832008-08-28 22:16:15 -0700609 .name = "bq4802",
David S. Miller1518e7e2008-08-28 21:06:27 -0700610 },
611};
612
613static unsigned char mostek_read_byte(struct device *dev, u32 ofs)
614{
615 struct platform_device *pdev = to_platform_device(dev);
616 void __iomem *regs;
617 unsigned char val;
618
619 regs = (void __iomem *) pdev->resource[0].start;
620 val = readb(regs + ofs);
621
622 /* the year 0 is 1968 */
623 if (ofs == M48T59_YEAR) {
624 val += 0x68;
625 if ((val & 0xf) > 9)
626 val += 6;
627 }
628 return val;
629}
630
631static void mostek_write_byte(struct device *dev, u32 ofs, u8 val)
632{
633 struct platform_device *pdev = to_platform_device(dev);
634 void __iomem *regs;
635
636 regs = (void __iomem *) pdev->resource[0].start;
637 if (ofs == M48T59_YEAR) {
638 if (val < 0x68)
639 val += 0x32;
640 else
641 val -= 0x68;
642 if ((val & 0xf) > 9)
643 val += 6;
644 if ((val & 0xf0) > 0x9A)
645 val += 0x60;
646 }
647 writeb(val, regs + ofs);
648}
649
650static struct m48t59_plat_data m48t59_data = {
651 .read_byte = mostek_read_byte,
652 .write_byte = mostek_write_byte,
653};
654
655static struct platform_device m48t59_rtc = {
656 .name = "rtc-m48t59",
657 .id = 0,
658 .num_resources = 1,
659 .dev = {
660 .platform_data = &m48t59_data,
661 },
662};
663
664static int __devinit mostek_probe(struct of_device *op, const struct of_device_id *match)
665{
666 struct device_node *dp = op->node;
667
668 /* On an Enterprise system there can be multiple mostek clocks.
669 * We should only match the one that is on the central FHC bus.
670 */
671 if (!strcmp(dp->parent->name, "fhc") &&
672 strcmp(dp->parent->parent->name, "central") != 0)
673 return -ENODEV;
674
675 printk(KERN_INFO "%s: Mostek regs at 0x%lx\n",
676 dp->full_name, op->resource[0].start);
677
678 m48t59_rtc.resource = &op->resource[0];
679 return platform_device_register(&m48t59_rtc);
680}
681
682static struct of_device_id mostek_match[] = {
683 {
684 .name = "eeprom",
685 },
686 {},
687};
688
689static struct of_platform_driver mostek_driver = {
690 .match_table = mostek_match,
691 .probe = mostek_probe,
692 .driver = {
693 .name = "mostek",
Stephen Rothwella2cd1552007-10-10 23:27:34 -0700694 },
David S. Milleree5caf02006-06-29 14:36:52 -0700695};
696
697static int __init clock_init(void)
698{
699 if (this_is_starfire) {
700 xtime.tv_sec = starfire_get_time();
701 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
702 set_normalized_timespec(&wall_to_monotonic,
703 -xtime.tv_sec, -xtime.tv_nsec);
704 return 0;
705 }
706 if (tlb_type == hypervisor) {
707 xtime.tv_sec = hypervisor_get_time();
708 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
709 set_normalized_timespec(&wall_to_monotonic,
710 -xtime.tv_sec, -xtime.tv_nsec);
711 return 0;
712 }
713
David S. Miller1518e7e2008-08-28 21:06:27 -0700714 (void) of_register_driver(&rtc_driver, &of_platform_bus_type);
715 (void) of_register_driver(&mostek_driver, &of_platform_bus_type);
David S. Millerda867832008-08-28 22:16:15 -0700716 (void) of_register_driver(&bq4802_driver, &of_platform_bus_type);
David S. Miller1518e7e2008-08-28 21:06:27 -0700717
718 return 0;
David S. Milleree5caf02006-06-29 14:36:52 -0700719}
720
721/* Must be after subsys_initcall() so that busses are probed. Must
722 * be before device_initcall() because things like the RTC driver
723 * need to see the clock registers.
724 */
725fs_initcall(clock_init);
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727/* This is gets the master TICK_INT timer going. */
728static unsigned long sparc64_init_timers(void)
729{
David S. Miller07f8e5f2006-06-21 23:34:02 -0700730 struct device_node *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 unsigned long clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
David S. Miller07f8e5f2006-06-21 23:34:02 -0700733 dp = of_find_node_by_path("/");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (tlb_type == spitfire) {
735 unsigned long ver, manuf, impl;
736
737 __asm__ __volatile__ ("rdpr %%ver, %0"
738 : "=&r" (ver));
739 manuf = ((ver >> 48) & 0xffff);
740 impl = ((ver >> 32) & 0xffff);
741 if (manuf == 0x17 && impl == 0x13) {
742 /* Hummingbird, aka Ultra-IIe */
743 tick_ops = &hbtick_operations;
David S. Miller5cbc3072007-05-25 15:49:59 -0700744 clock = of_getintprop_default(dp, "stick-frequency", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 } else {
746 tick_ops = &tick_operations;
David S. Miller5cbc3072007-05-25 15:49:59 -0700747 clock = local_cpu_data().clock_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749 } else {
750 tick_ops = &stick_operations;
David S. Miller5cbc3072007-05-25 15:49:59 -0700751 clock = of_getintprop_default(dp, "stick-frequency", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return clock;
755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757struct freq_table {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 unsigned long clock_tick_ref;
759 unsigned int ref_freq;
760};
David S. Miller3763be32006-02-17 12:33:13 -0800761static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763unsigned long sparc64_get_clock_tick(unsigned int cpu)
764{
765 struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
766
767 if (ft->clock_tick_ref)
768 return ft->clock_tick_ref;
769 return cpu_data(cpu).clock_tick;
770}
771
772#ifdef CONFIG_CPU_FREQ
773
774static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
775 void *data)
776{
777 struct cpufreq_freqs *freq = data;
778 unsigned int cpu = freq->cpu;
779 struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
780
781 if (!ft->ref_freq) {
782 ft->ref_freq = freq->old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 ft->clock_tick_ref = cpu_data(cpu).clock_tick;
784 }
785 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
786 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
787 (val == CPUFREQ_RESUMECHANGE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 cpu_data(cpu).clock_tick =
789 cpufreq_scale(ft->clock_tick_ref,
790 ft->ref_freq,
791 freq->new);
792 }
793
794 return 0;
795}
796
797static struct notifier_block sparc64_cpufreq_notifier_block = {
798 .notifier_call = sparc64_cpufreq_notifier
799};
800
David S. Miller7ae93f52008-07-23 16:21:07 -0700801static int __init register_sparc64_cpufreq_notifier(void)
802{
803
804 cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
805 CPUFREQ_TRANSITION_NOTIFIER);
806 return 0;
807}
808
809core_initcall(register_sparc64_cpufreq_notifier);
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811#endif /* CONFIG_CPU_FREQ */
812
David S. Miller112f4872007-03-05 15:28:37 -0800813static int sparc64_next_event(unsigned long delta,
814 struct clock_event_device *evt)
815{
David S. Millerd62c6f02007-03-27 01:20:14 -0700816 return tick_ops->add_compare(delta) ? -ETIME : 0;
David S. Miller112f4872007-03-05 15:28:37 -0800817}
818
819static void sparc64_timer_setup(enum clock_event_mode mode,
820 struct clock_event_device *evt)
821{
822 switch (mode) {
823 case CLOCK_EVT_MODE_ONESHOT:
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700824 case CLOCK_EVT_MODE_RESUME:
David S. Miller112f4872007-03-05 15:28:37 -0800825 break;
826
827 case CLOCK_EVT_MODE_SHUTDOWN:
828 tick_ops->disable_irq();
829 break;
830
831 case CLOCK_EVT_MODE_PERIODIC:
832 case CLOCK_EVT_MODE_UNUSED:
833 WARN_ON(1);
834 break;
835 };
836}
837
838static struct clock_event_device sparc64_clockevent = {
839 .features = CLOCK_EVT_FEAT_ONESHOT,
840 .set_mode = sparc64_timer_setup,
841 .set_next_event = sparc64_next_event,
842 .rating = 100,
843 .shift = 30,
844 .irq = -1,
845};
846static DEFINE_PER_CPU(struct clock_event_device, sparc64_events);
847
848void timer_interrupt(int irq, struct pt_regs *regs)
849{
850 struct pt_regs *old_regs = set_irq_regs(regs);
851 unsigned long tick_mask = tick_ops->softint_mask;
852 int cpu = smp_processor_id();
853 struct clock_event_device *evt = &per_cpu(sparc64_events, cpu);
854
855 clear_softint(tick_mask);
856
857 irq_enter();
858
859 kstat_this_cpu.irqs[0]++;
860
861 if (unlikely(!evt->event_handler)) {
862 printk(KERN_WARNING
863 "Spurious SPARC64 timer interrupt on cpu %d\n", cpu);
864 } else
865 evt->event_handler(evt);
866
867 irq_exit();
868
869 set_irq_regs(old_regs);
870}
871
872void __devinit setup_sparc64_timer(void)
873{
874 struct clock_event_device *sevt;
875 unsigned long pstate;
876
877 /* Guarantee that the following sequences execute
878 * uninterrupted.
879 */
880 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
881 "wrpr %0, %1, %%pstate"
882 : "=r" (pstate)
883 : "i" (PSTATE_IE));
884
885 tick_ops->init_tick();
886
887 /* Restore PSTATE_IE. */
888 __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
889 : /* no outputs */
890 : "r" (pstate));
891
892 sevt = &__get_cpu_var(sparc64_events);
893
894 memcpy(sevt, &sparc64_clockevent, sizeof(*sevt));
895 sevt->cpumask = cpumask_of_cpu(smp_processor_id());
896
897 clockevents_register_device(sevt);
898}
899
David S. Miller03983ab2007-05-17 22:55:26 -0700900#define SPARC64_NSEC_PER_CYC_SHIFT 10UL
David S. Miller112f4872007-03-05 15:28:37 -0800901
902static struct clocksource clocksource_tick = {
903 .rating = 100,
904 .mask = CLOCKSOURCE_MASK(64),
905 .shift = 16,
906 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907};
908
David S. Miller112f4872007-03-05 15:28:37 -0800909static void __init setup_clockevent_multiplier(unsigned long hz)
910{
911 unsigned long mult, shift = 32;
912
913 while (1) {
914 mult = div_sc(hz, NSEC_PER_SEC, shift);
915 if (mult && (mult >> 32UL) == 0UL)
916 break;
917
918 shift--;
919 }
920
921 sparc64_clockevent.shift = shift;
922 sparc64_clockevent.mult = mult;
923}
924
David S. Miller8b99cfb2007-07-14 02:23:37 -0700925static unsigned long tb_ticks_per_usec __read_mostly;
926
927void __delay(unsigned long loops)
928{
929 unsigned long bclock, now;
930
931 bclock = tick_ops->get_tick();
932 do {
933 now = tick_ops->get_tick();
934 } while ((now-bclock) < loops);
935}
936EXPORT_SYMBOL(__delay);
937
938void udelay(unsigned long usecs)
939{
940 __delay(tb_ticks_per_usec * usecs);
941}
942EXPORT_SYMBOL(udelay);
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944void __init time_init(void)
945{
946 unsigned long clock = sparc64_init_timers();
947
David S. Miller8b99cfb2007-07-14 02:23:37 -0700948 tb_ticks_per_usec = clock / USEC_PER_SEC;
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 timer_ticks_per_nsec_quotient =
David S. Miller112f4872007-03-05 15:28:37 -0800951 clocksource_hz2mult(clock, SPARC64_NSEC_PER_CYC_SHIFT);
952
953 clocksource_tick.name = tick_ops->name;
954 clocksource_tick.mult =
955 clocksource_hz2mult(clock,
956 clocksource_tick.shift);
957 clocksource_tick.read = tick_ops->get_tick;
958
959 printk("clocksource: mult[%x] shift[%d]\n",
960 clocksource_tick.mult, clocksource_tick.shift);
961
962 clocksource_register(&clocksource_tick);
963
964 sparc64_clockevent.name = tick_ops->name;
965
966 setup_clockevent_multiplier(clock);
967
968 sparc64_clockevent.max_delta_ns =
David S. Millercf3d7c12008-03-26 01:11:55 -0700969 clockevent_delta2ns(0x7fffffffffffffffUL, &sparc64_clockevent);
David S. Miller112f4872007-03-05 15:28:37 -0800970 sparc64_clockevent.min_delta_ns =
971 clockevent_delta2ns(0xF, &sparc64_clockevent);
972
973 printk("clockevent: mult[%lx] shift[%d]\n",
974 sparc64_clockevent.mult, sparc64_clockevent.shift);
975
976 setup_sparc64_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979unsigned long long sched_clock(void)
980{
981 unsigned long ticks = tick_ops->get_tick();
982
983 return (ticks * timer_ticks_per_nsec_quotient)
984 >> SPARC64_NSEC_PER_CYC_SHIFT;
985}
986
987static int set_rtc_mmss(unsigned long nowtime)
988{
989 int real_seconds, real_minutes, chip_minutes;
David S. Millerd037e052007-05-11 21:18:50 -0700990 void __iomem *bregs = bq4802_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 unsigned long flags;
David S. Millerda867832008-08-28 22:16:15 -0700992 unsigned char val;
993 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 /*
996 * Not having a register set can lead to trouble.
997 * Also starfire doesn't have a tod clock.
998 */
David S. Millerda867832008-08-28 22:16:15 -0700999 if (!bregs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return -1;
1001
David S. Millerda867832008-08-28 22:16:15 -07001002 spin_lock_irqsave(&rtc_lock, flags);
David S. Millerd037e052007-05-11 21:18:50 -07001003
David S. Millerda867832008-08-28 22:16:15 -07001004 val = readb(bregs + 0x0e);
David S. Millerd037e052007-05-11 21:18:50 -07001005
David S. Millerda867832008-08-28 22:16:15 -07001006 /* BQ4802 RTC chip. */
David S. Millerd037e052007-05-11 21:18:50 -07001007
David S. Millerda867832008-08-28 22:16:15 -07001008 writeb(val | 0x08, bregs + 0x0e);
David S. Millerd037e052007-05-11 21:18:50 -07001009
David S. Millerda867832008-08-28 22:16:15 -07001010 chip_minutes = readb(bregs + 0x02);
1011 BCD_TO_BIN(chip_minutes);
1012 real_seconds = nowtime % 60;
1013 real_minutes = nowtime / 60;
1014 if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
1015 real_minutes += 30;
1016 real_minutes %= 60;
David S. Millerd037e052007-05-11 21:18:50 -07001017
David S. Millerda867832008-08-28 22:16:15 -07001018 if (abs(real_minutes - chip_minutes) < 30) {
1019 BIN_TO_BCD(real_seconds);
1020 BIN_TO_BCD(real_minutes);
1021 writeb(real_seconds, bregs + 0x00);
1022 writeb(real_minutes, bregs + 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 } else {
David S. Millerda867832008-08-28 22:16:15 -07001024 printk(KERN_WARNING
1025 "set_rtc_mmss: can't update from %d to %d\n",
1026 chip_minutes, real_minutes);
1027 retval = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
David S. Millerda867832008-08-28 22:16:15 -07001029
1030 writeb(val, bregs + 0x0e);
1031
1032 spin_unlock_irqrestore(&rtc_lock, flags);
1033
1034 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
David S. Miller8ba706a2006-03-01 17:32:46 -08001036
1037#define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
1038static unsigned char mini_rtc_status; /* bitmapped status byte. */
1039
David S. Miller8ba706a2006-03-01 17:32:46 -08001040#define FEBRUARY 2
1041#define STARTOFTIME 1970
1042#define SECDAY 86400L
1043#define SECYR (SECDAY * 365)
1044#define leapyear(year) ((year) % 4 == 0 && \
1045 ((year) % 100 != 0 || (year) % 400 == 0))
1046#define days_in_year(a) (leapyear(a) ? 366 : 365)
1047#define days_in_month(a) (month_days[(a) - 1])
1048
1049static int month_days[12] = {
1050 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
1051};
1052
1053/*
1054 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
1055 */
1056static void GregorianDay(struct rtc_time * tm)
1057{
1058 int leapsToDate;
1059 int lastYear;
1060 int day;
1061 int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
1062
1063 lastYear = tm->tm_year - 1;
1064
1065 /*
1066 * Number of leap corrections to apply up to end of last year
1067 */
1068 leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
1069
1070 /*
1071 * This year is a leap year if it is divisible by 4 except when it is
1072 * divisible by 100 unless it is divisible by 400
1073 *
1074 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
1075 */
1076 day = tm->tm_mon > 2 && leapyear(tm->tm_year);
1077
1078 day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
1079 tm->tm_mday;
1080
1081 tm->tm_wday = day % 7;
1082}
1083
1084static void to_tm(int tim, struct rtc_time *tm)
1085{
1086 register int i;
1087 register long hms, day;
1088
1089 day = tim / SECDAY;
1090 hms = tim % SECDAY;
1091
1092 /* Hours, minutes, seconds are easy */
1093 tm->tm_hour = hms / 3600;
1094 tm->tm_min = (hms % 3600) / 60;
1095 tm->tm_sec = (hms % 3600) % 60;
1096
1097 /* Number of years in days */
1098 for (i = STARTOFTIME; day >= days_in_year(i); i++)
1099 day -= days_in_year(i);
1100 tm->tm_year = i;
1101
1102 /* Number of months in days left */
1103 if (leapyear(tm->tm_year))
1104 days_in_month(FEBRUARY) = 29;
1105 for (i = 1; day >= days_in_month(i); i++)
1106 day -= days_in_month(i);
1107 days_in_month(FEBRUARY) = 28;
1108 tm->tm_mon = i;
1109
1110 /* Days are what is left over (+1) from all that. */
1111 tm->tm_mday = day + 1;
1112
1113 /*
1114 * Determine the day of week
1115 */
1116 GregorianDay(tm);
1117}
1118
1119/* Both Starfire and SUN4V give us seconds since Jan 1st, 1970,
1120 * aka Unix time. So we have to convert to/from rtc_time.
1121 */
David S. Millerd037e052007-05-11 21:18:50 -07001122static void starfire_get_rtc_time(struct rtc_time *time)
David S. Miller8ba706a2006-03-01 17:32:46 -08001123{
David S. Millerd037e052007-05-11 21:18:50 -07001124 u32 seconds = starfire_get_time();
David S. Miller8ba706a2006-03-01 17:32:46 -08001125
1126 to_tm(seconds, time);
David S. Millerc4f8ef72006-03-02 20:28:34 -08001127 time->tm_year -= 1900;
1128 time->tm_mon -= 1;
David S. Miller8ba706a2006-03-01 17:32:46 -08001129}
1130
David S. Millerd037e052007-05-11 21:18:50 -07001131static int starfire_set_rtc_time(struct rtc_time *time)
David S. Miller8ba706a2006-03-01 17:32:46 -08001132{
1133 u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
1134 time->tm_mday, time->tm_hour,
1135 time->tm_min, time->tm_sec);
David S. Millerd037e052007-05-11 21:18:50 -07001136
1137 return starfire_set_time(seconds);
1138}
1139
1140static void hypervisor_get_rtc_time(struct rtc_time *time)
1141{
1142 u32 seconds = hypervisor_get_time();
1143
1144 to_tm(seconds, time);
1145 time->tm_year -= 1900;
1146 time->tm_mon -= 1;
1147}
1148
1149static int hypervisor_set_rtc_time(struct rtc_time *time)
1150{
1151 u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
1152 time->tm_mday, time->tm_hour,
1153 time->tm_min, time->tm_sec);
1154
1155 return hypervisor_set_time(seconds);
1156}
1157
1158static void bq4802_get_rtc_time(struct rtc_time *time)
1159{
1160 unsigned char val = readb(bq4802_regs + 0x0e);
1161 unsigned int century;
1162
1163 writeb(val | 0x08, bq4802_regs + 0x0e);
1164
1165 time->tm_sec = readb(bq4802_regs + 0x00);
1166 time->tm_min = readb(bq4802_regs + 0x02);
1167 time->tm_hour = readb(bq4802_regs + 0x04);
1168 time->tm_mday = readb(bq4802_regs + 0x06);
1169 time->tm_mon = readb(bq4802_regs + 0x09);
1170 time->tm_year = readb(bq4802_regs + 0x0a);
1171 time->tm_wday = readb(bq4802_regs + 0x08);
1172 century = readb(bq4802_regs + 0x0f);
1173
1174 writeb(val, bq4802_regs + 0x0e);
1175
1176 BCD_TO_BIN(time->tm_sec);
1177 BCD_TO_BIN(time->tm_min);
1178 BCD_TO_BIN(time->tm_hour);
1179 BCD_TO_BIN(time->tm_mday);
1180 BCD_TO_BIN(time->tm_mon);
1181 BCD_TO_BIN(time->tm_year);
1182 BCD_TO_BIN(time->tm_wday);
1183 BCD_TO_BIN(century);
1184
1185 time->tm_year += (century * 100);
1186 time->tm_year -= 1900;
1187
1188 time->tm_mon--;
1189}
1190
1191static int bq4802_set_rtc_time(struct rtc_time *time)
1192{
1193 unsigned char val = readb(bq4802_regs + 0x0e);
1194 unsigned char sec, min, hrs, day, mon, yrs, century;
1195 unsigned int year;
1196
1197 year = time->tm_year + 1900;
1198 century = year / 100;
1199 yrs = year % 100;
1200
1201 mon = time->tm_mon + 1; /* tm_mon starts at zero */
1202 day = time->tm_mday;
1203 hrs = time->tm_hour;
1204 min = time->tm_min;
1205 sec = time->tm_sec;
1206
1207 BIN_TO_BCD(sec);
1208 BIN_TO_BCD(min);
1209 BIN_TO_BCD(hrs);
1210 BIN_TO_BCD(day);
1211 BIN_TO_BCD(mon);
1212 BIN_TO_BCD(yrs);
1213 BIN_TO_BCD(century);
1214
1215 writeb(val | 0x08, bq4802_regs + 0x0e);
1216
1217 writeb(sec, bq4802_regs + 0x00);
1218 writeb(min, bq4802_regs + 0x02);
1219 writeb(hrs, bq4802_regs + 0x04);
1220 writeb(day, bq4802_regs + 0x06);
1221 writeb(mon, bq4802_regs + 0x09);
1222 writeb(yrs, bq4802_regs + 0x0a);
1223 writeb(century, bq4802_regs + 0x0f);
1224
1225 writeb(val, bq4802_regs + 0x0e);
1226
1227 return 0;
1228}
David S. Millercdee99d2007-07-19 13:59:58 -07001229
David S. Millerd037e052007-05-11 21:18:50 -07001230struct mini_rtc_ops {
1231 void (*get_rtc_time)(struct rtc_time *);
1232 int (*set_rtc_time)(struct rtc_time *);
1233};
1234
1235static struct mini_rtc_ops starfire_rtc_ops = {
1236 .get_rtc_time = starfire_get_rtc_time,
1237 .set_rtc_time = starfire_set_rtc_time,
1238};
1239
1240static struct mini_rtc_ops hypervisor_rtc_ops = {
1241 .get_rtc_time = hypervisor_get_rtc_time,
1242 .set_rtc_time = hypervisor_set_rtc_time,
1243};
1244
1245static struct mini_rtc_ops bq4802_rtc_ops = {
1246 .get_rtc_time = bq4802_get_rtc_time,
1247 .set_rtc_time = bq4802_set_rtc_time,
1248};
David S. Millercdee99d2007-07-19 13:59:58 -07001249
David S. Millerd037e052007-05-11 21:18:50 -07001250static struct mini_rtc_ops *mini_rtc_ops;
1251
1252static inline void mini_get_rtc_time(struct rtc_time *time)
1253{
1254 unsigned long flags;
1255
1256 spin_lock_irqsave(&rtc_lock, flags);
1257 mini_rtc_ops->get_rtc_time(time);
1258 spin_unlock_irqrestore(&rtc_lock, flags);
1259}
1260
1261static inline int mini_set_rtc_time(struct rtc_time *time)
1262{
David S. Miller8ba706a2006-03-01 17:32:46 -08001263 unsigned long flags;
1264 int err;
1265
1266 spin_lock_irqsave(&rtc_lock, flags);
David S. Millerd037e052007-05-11 21:18:50 -07001267 err = mini_rtc_ops->set_rtc_time(time);
David S. Miller8ba706a2006-03-01 17:32:46 -08001268 spin_unlock_irqrestore(&rtc_lock, flags);
1269
1270 return err;
1271}
1272
1273static int mini_rtc_ioctl(struct inode *inode, struct file *file,
1274 unsigned int cmd, unsigned long arg)
1275{
1276 struct rtc_time wtime;
1277 void __user *argp = (void __user *)arg;
1278
1279 switch (cmd) {
1280
1281 case RTC_PLL_GET:
1282 return -EINVAL;
1283
1284 case RTC_PLL_SET:
1285 return -EINVAL;
1286
1287 case RTC_UIE_OFF: /* disable ints from RTC updates. */
1288 return 0;
1289
1290 case RTC_UIE_ON: /* enable ints for RTC updates. */
1291 return -EINVAL;
1292
1293 case RTC_RD_TIME: /* Read the time/date from RTC */
1294 /* this doesn't get week-day, who cares */
1295 memset(&wtime, 0, sizeof(wtime));
1296 mini_get_rtc_time(&wtime);
1297
1298 return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0;
1299
1300 case RTC_SET_TIME: /* Set the RTC */
1301 {
Tony Breeds644923d2007-03-28 19:10:12 -07001302 int year, days;
David S. Miller8ba706a2006-03-01 17:32:46 -08001303
1304 if (!capable(CAP_SYS_TIME))
1305 return -EACCES;
1306
1307 if (copy_from_user(&wtime, argp, sizeof(wtime)))
1308 return -EFAULT;
1309
1310 year = wtime.tm_year + 1900;
Tony Breeds644923d2007-03-28 19:10:12 -07001311 days = month_days[wtime.tm_mon] +
1312 ((wtime.tm_mon == 1) && leapyear(year));
David S. Miller8ba706a2006-03-01 17:32:46 -08001313
Tony Breeds644923d2007-03-28 19:10:12 -07001314 if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) ||
1315 (wtime.tm_mday < 1))
David S. Miller8ba706a2006-03-01 17:32:46 -08001316 return -EINVAL;
1317
Tony Breeds644923d2007-03-28 19:10:12 -07001318 if (wtime.tm_mday < 0 || wtime.tm_mday > days)
David S. Miller8ba706a2006-03-01 17:32:46 -08001319 return -EINVAL;
1320
1321 if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
1322 wtime.tm_min < 0 || wtime.tm_min >= 60 ||
1323 wtime.tm_sec < 0 || wtime.tm_sec >= 60)
1324 return -EINVAL;
1325
1326 return mini_set_rtc_time(&wtime);
1327 }
1328 }
1329
1330 return -EINVAL;
1331}
1332
1333static int mini_rtc_open(struct inode *inode, struct file *file)
1334{
Arnd Bergmann09de3612008-05-20 19:16:50 +02001335 lock_kernel();
1336 if (mini_rtc_status & RTC_IS_OPEN) {
1337 unlock_kernel();
David S. Miller8ba706a2006-03-01 17:32:46 -08001338 return -EBUSY;
Arnd Bergmann09de3612008-05-20 19:16:50 +02001339 }
David S. Miller8ba706a2006-03-01 17:32:46 -08001340
1341 mini_rtc_status |= RTC_IS_OPEN;
Arnd Bergmann09de3612008-05-20 19:16:50 +02001342 unlock_kernel();
David S. Miller8ba706a2006-03-01 17:32:46 -08001343
1344 return 0;
1345}
1346
1347static int mini_rtc_release(struct inode *inode, struct file *file)
1348{
1349 mini_rtc_status &= ~RTC_IS_OPEN;
1350 return 0;
1351}
1352
1353
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001354static const struct file_operations mini_rtc_fops = {
David S. Miller8ba706a2006-03-01 17:32:46 -08001355 .owner = THIS_MODULE,
1356 .ioctl = mini_rtc_ioctl,
1357 .open = mini_rtc_open,
1358 .release = mini_rtc_release,
1359};
1360
1361static struct miscdevice rtc_mini_dev =
1362{
1363 .minor = RTC_MINOR,
1364 .name = "rtc",
1365 .fops = &mini_rtc_fops,
1366};
1367
1368static int __init rtc_mini_init(void)
1369{
1370 int retval;
1371
David S. Millerd037e052007-05-11 21:18:50 -07001372 if (tlb_type == hypervisor)
1373 mini_rtc_ops = &hypervisor_rtc_ops;
1374 else if (this_is_starfire)
1375 mini_rtc_ops = &starfire_rtc_ops;
1376 else if (bq4802_regs)
1377 mini_rtc_ops = &bq4802_rtc_ops;
1378 else
David S. Miller8ba706a2006-03-01 17:32:46 -08001379 return -ENODEV;
1380
1381 printk(KERN_INFO "Mini RTC Driver\n");
1382
1383 retval = misc_register(&rtc_mini_dev);
1384 if (retval < 0)
1385 return retval;
1386
1387 return 0;
1388}
1389
1390static void __exit rtc_mini_exit(void)
1391{
1392 misc_deregister(&rtc_mini_dev);
1393}
1394
Andrew Morton941e4922008-02-06 01:36:42 -08001395int __devinit read_current_timer(unsigned long *timer_val)
1396{
1397 *timer_val = tick_ops->get_tick();
1398 return 0;
1399}
David S. Miller8ba706a2006-03-01 17:32:46 -08001400
1401module_init(rtc_mini_init);
1402module_exit(rtc_mini_exit);