blob: 184321b88a682eebe3f2dc22623cb6847d8d2b6d [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. Miller777a4472007-02-22 06:24:10 -080033#include <linux/kernel_stat.h>
David S. Miller112f4872007-03-05 15:28:37 -080034#include <linux/clockchips.h>
35#include <linux/clocksource.h>
Stephen Rothwell764f2572008-08-07 15:33:36 -070036#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/oplib.h>
39#include <asm/mostek.h>
40#include <asm/timer.h>
41#include <asm/irq.h>
42#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>
David S. Miller8ba706a2006-03-01 17:32:46 -080048#include <asm/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(mostek_lock);
54DEFINE_SPINLOCK(rtc_lock);
Al Viroef0299b2005-04-24 12:28:36 -070055void __iomem *mstk48t02_regs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#ifdef CONFIG_PCI
57unsigned long ds1287_regs = 0UL;
David S. Millerd037e052007-05-11 21:18:50 -070058static void __iomem *bq4802_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
Al Viroef0299b2005-04-24 12:28:36 -070061static void __iomem *mstk48t08_regs;
62static void __iomem *mstk48t59_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64static int set_rtc_mmss(unsigned long);
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define TICK_PRIV_BIT (1UL << 63)
David S. Miller112f4872007-03-05 15:28:37 -080067#define TICKCMP_IRQ_BIT (1UL << 63)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#ifdef CONFIG_SMP
70unsigned long profile_pc(struct pt_regs *regs)
71{
72 unsigned long pc = instruction_pointer(regs);
73
74 if (in_lock_functions(pc))
75 return regs->u_regs[UREG_RETPC];
76 return pc;
77}
78EXPORT_SYMBOL(profile_pc);
79#endif
80
81static void tick_disable_protection(void)
82{
83 /* Set things up so user can access tick register for profiling
84 * purposes. Also workaround BB_ERRATA_1 by doing a dummy
85 * read back of %tick after writing it.
86 */
87 __asm__ __volatile__(
88 " ba,pt %%xcc, 1f\n"
89 " nop\n"
90 " .align 64\n"
91 "1: rd %%tick, %%g2\n"
92 " add %%g2, 6, %%g2\n"
93 " andn %%g2, %0, %%g2\n"
94 " wrpr %%g2, 0, %%tick\n"
95 " rdpr %%tick, %%g0"
96 : /* no outputs */
97 : "r" (TICK_PRIV_BIT)
98 : "g2");
99}
100
David S. Miller112f4872007-03-05 15:28:37 -0800101static void tick_disable_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 __asm__ __volatile__(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 " ba,pt %%xcc, 1f\n"
David S. Miller112f4872007-03-05 15:28:37 -0800105 " nop\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 " .align 64\n"
David S. Miller112f4872007-03-05 15:28:37 -0800107 "1: wr %0, 0x0, %%tick_cmpr\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 " rd %%tick_cmpr, %%g0"
109 : /* no outputs */
David S. Miller112f4872007-03-05 15:28:37 -0800110 : "r" (TICKCMP_IRQ_BIT));
111}
112
113static void tick_init_tick(void)
114{
115 tick_disable_protection();
116 tick_disable_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119static unsigned long tick_get_tick(void)
120{
121 unsigned long ret;
122
123 __asm__ __volatile__("rd %%tick, %0\n\t"
124 "mov %0, %0"
125 : "=r" (ret));
126
127 return ret & ~TICK_PRIV_BIT;
128}
129
David S. Miller112f4872007-03-05 15:28:37 -0800130static int tick_add_compare(unsigned long adj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
David S. Miller112f4872007-03-05 15:28:37 -0800132 unsigned long orig_tick, new_tick, new_compare;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
David S. Miller112f4872007-03-05 15:28:37 -0800134 __asm__ __volatile__("rd %%tick, %0"
135 : "=r" (orig_tick));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
David S. Miller112f4872007-03-05 15:28:37 -0800137 orig_tick &= ~TICKCMP_IRQ_BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 /* Workaround for Spitfire Errata (#54 I think??), I discovered
140 * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch
141 * number 103640.
142 *
143 * On Blackbird writes to %tick_cmpr can fail, the
144 * workaround seems to be to execute the wr instruction
145 * at the start of an I-cache line, and perform a dummy
146 * read back from %tick_cmpr right after writing to it. -DaveM
147 */
David S. Miller112f4872007-03-05 15:28:37 -0800148 __asm__ __volatile__("ba,pt %%xcc, 1f\n\t"
149 " add %1, %2, %0\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 ".align 64\n"
151 "1:\n\t"
152 "wr %0, 0, %%tick_cmpr\n\t"
David S. Miller112f4872007-03-05 15:28:37 -0800153 "rd %%tick_cmpr, %%g0\n\t"
154 : "=r" (new_compare)
155 : "r" (orig_tick), "r" (adj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
David S. Miller112f4872007-03-05 15:28:37 -0800157 __asm__ __volatile__("rd %%tick, %0"
158 : "=r" (new_tick));
159 new_tick &= ~TICKCMP_IRQ_BIT;
160
161 return ((long)(new_tick - (orig_tick+adj))) > 0L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
David S. Miller112f4872007-03-05 15:28:37 -0800164static unsigned long tick_add_tick(unsigned long adj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
David S. Miller112f4872007-03-05 15:28:37 -0800166 unsigned long new_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 /* Also need to handle Blackbird bug here too. */
169 __asm__ __volatile__("rd %%tick, %0\n\t"
David S. Miller112f4872007-03-05 15:28:37 -0800170 "add %0, %1, %0\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 "wrpr %0, 0, %%tick\n\t"
David S. Miller112f4872007-03-05 15:28:37 -0800172 : "=&r" (new_tick)
173 : "r" (adj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 return new_tick;
176}
177
David S. Millerd369ddd2005-07-10 15:45:11 -0700178static struct sparc64_tick_ops tick_operations __read_mostly = {
David S. Miller112f4872007-03-05 15:28:37 -0800179 .name = "tick",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 .init_tick = tick_init_tick,
David S. Miller112f4872007-03-05 15:28:37 -0800181 .disable_irq = tick_disable_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 .get_tick = tick_get_tick,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 .add_tick = tick_add_tick,
184 .add_compare = tick_add_compare,
185 .softint_mask = 1UL << 0,
186};
187
David S. Millerfc321492005-11-07 14:10:10 -0800188struct sparc64_tick_ops *tick_ops __read_mostly = &tick_operations;
189
David S. Miller112f4872007-03-05 15:28:37 -0800190static void stick_disable_irq(void)
191{
192 __asm__ __volatile__(
193 "wr %0, 0x0, %%asr25"
194 : /* no outputs */
195 : "r" (TICKCMP_IRQ_BIT));
196}
197
198static void stick_init_tick(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
David S. Miller7aa62642006-02-11 23:14:59 -0800200 /* Writes to the %tick and %stick register are not
201 * allowed on sun4v. The Hypervisor controls that
202 * bit, per-strand.
203 */
204 if (tlb_type != hypervisor) {
205 tick_disable_protection();
David S. Miller112f4872007-03-05 15:28:37 -0800206 tick_disable_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
David S. Miller7aa62642006-02-11 23:14:59 -0800208 /* Let the user get at STICK too. */
209 __asm__ __volatile__(
210 " rd %%asr24, %%g2\n"
211 " andn %%g2, %0, %%g2\n"
212 " wr %%g2, 0, %%asr24"
213 : /* no outputs */
214 : "r" (TICK_PRIV_BIT)
215 : "g1", "g2");
216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
David S. Miller112f4872007-03-05 15:28:37 -0800218 stick_disable_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221static unsigned long stick_get_tick(void)
222{
223 unsigned long ret;
224
225 __asm__ __volatile__("rd %%asr24, %0"
226 : "=r" (ret));
227
228 return ret & ~TICK_PRIV_BIT;
229}
230
David S. Miller112f4872007-03-05 15:28:37 -0800231static unsigned long stick_add_tick(unsigned long adj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
David S. Miller112f4872007-03-05 15:28:37 -0800233 unsigned long new_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 __asm__ __volatile__("rd %%asr24, %0\n\t"
David S. Miller112f4872007-03-05 15:28:37 -0800236 "add %0, %1, %0\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 "wr %0, 0, %%asr24\n\t"
David S. Miller112f4872007-03-05 15:28:37 -0800238 : "=&r" (new_tick)
239 : "r" (adj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 return new_tick;
242}
243
David S. Miller112f4872007-03-05 15:28:37 -0800244static int stick_add_compare(unsigned long adj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
David S. Miller112f4872007-03-05 15:28:37 -0800246 unsigned long orig_tick, new_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
David S. Miller112f4872007-03-05 15:28:37 -0800248 __asm__ __volatile__("rd %%asr24, %0"
249 : "=r" (orig_tick));
250 orig_tick &= ~TICKCMP_IRQ_BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
David S. Miller112f4872007-03-05 15:28:37 -0800252 __asm__ __volatile__("wr %0, 0, %%asr25"
253 : /* no outputs */
254 : "r" (orig_tick + adj));
255
256 __asm__ __volatile__("rd %%asr24, %0"
257 : "=r" (new_tick));
258 new_tick &= ~TICKCMP_IRQ_BIT;
259
260 return ((long)(new_tick - (orig_tick+adj))) > 0L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
David S. Millerd369ddd2005-07-10 15:45:11 -0700263static struct sparc64_tick_ops stick_operations __read_mostly = {
David S. Miller112f4872007-03-05 15:28:37 -0800264 .name = "stick",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 .init_tick = stick_init_tick,
David S. Miller112f4872007-03-05 15:28:37 -0800266 .disable_irq = stick_disable_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 .get_tick = stick_get_tick,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 .add_tick = stick_add_tick,
269 .add_compare = stick_add_compare,
270 .softint_mask = 1UL << 16,
271};
272
273/* On Hummingbird the STICK/STICK_CMPR register is implemented
274 * in I/O space. There are two 64-bit registers each, the
275 * first holds the low 32-bits of the value and the second holds
276 * the high 32-bits.
277 *
278 * Since STICK is constantly updating, we have to access it carefully.
279 *
280 * The sequence we use to read is:
Richard Mortimer9eb33942006-01-17 15:21:01 -0800281 * 1) read high
282 * 2) read low
283 * 3) read high again, if it rolled re-read both low and high again.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 *
285 * Writing STICK safely is also tricky:
286 * 1) write low to zero
287 * 2) write high
288 * 3) write low
289 */
290#define HBIRD_STICKCMP_ADDR 0x1fe0000f060UL
291#define HBIRD_STICK_ADDR 0x1fe0000f070UL
292
293static unsigned long __hbird_read_stick(void)
294{
295 unsigned long ret, tmp1, tmp2, tmp3;
Richard Mortimer9eb33942006-01-17 15:21:01 -0800296 unsigned long addr = HBIRD_STICK_ADDR+8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Richard Mortimer9eb33942006-01-17 15:21:01 -0800298 __asm__ __volatile__("ldxa [%1] %5, %2\n"
299 "1:\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 "sub %1, 0x8, %1\n\t"
Richard Mortimer9eb33942006-01-17 15:21:01 -0800301 "ldxa [%1] %5, %3\n\t"
302 "add %1, 0x8, %1\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 "ldxa [%1] %5, %4\n\t"
304 "cmp %4, %2\n\t"
Richard Mortimer9eb33942006-01-17 15:21:01 -0800305 "bne,a,pn %%xcc, 1b\n\t"
306 " mov %4, %2\n\t"
307 "sllx %4, 32, %4\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 "or %3, %4, %0\n\t"
309 : "=&r" (ret), "=&r" (addr),
310 "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
311 : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr));
312
313 return ret;
314}
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316static void __hbird_write_stick(unsigned long val)
317{
318 unsigned long low = (val & 0xffffffffUL);
319 unsigned long high = (val >> 32UL);
320 unsigned long addr = HBIRD_STICK_ADDR;
321
322 __asm__ __volatile__("stxa %%g0, [%0] %4\n\t"
323 "add %0, 0x8, %0\n\t"
324 "stxa %3, [%0] %4\n\t"
325 "sub %0, 0x8, %0\n\t"
326 "stxa %2, [%0] %4"
327 : "=&r" (addr)
328 : "0" (addr), "r" (low), "r" (high),
329 "i" (ASI_PHYS_BYPASS_EC_E));
330}
331
332static void __hbird_write_compare(unsigned long val)
333{
334 unsigned long low = (val & 0xffffffffUL);
335 unsigned long high = (val >> 32UL);
336 unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL;
337
338 __asm__ __volatile__("stxa %3, [%0] %4\n\t"
339 "sub %0, 0x8, %0\n\t"
340 "stxa %2, [%0] %4"
341 : "=&r" (addr)
342 : "0" (addr), "r" (low), "r" (high),
343 "i" (ASI_PHYS_BYPASS_EC_E));
344}
345
David S. Miller112f4872007-03-05 15:28:37 -0800346static void hbtick_disable_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
David S. Miller112f4872007-03-05 15:28:37 -0800348 __hbird_write_compare(TICKCMP_IRQ_BIT);
349}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
David S. Miller112f4872007-03-05 15:28:37 -0800351static void hbtick_init_tick(void)
352{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 tick_disable_protection();
354
355 /* XXX This seems to be necessary to 'jumpstart' Hummingbird
356 * XXX into actually sending STICK interrupts. I think because
357 * XXX of how we store %tick_cmpr in head.S this somehow resets the
358 * XXX {TICK + STICK} interrupt mux. -DaveM
359 */
360 __hbird_write_stick(__hbird_read_stick());
361
David S. Miller112f4872007-03-05 15:28:37 -0800362 hbtick_disable_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365static unsigned long hbtick_get_tick(void)
366{
367 return __hbird_read_stick() & ~TICK_PRIV_BIT;
368}
369
David S. Miller112f4872007-03-05 15:28:37 -0800370static unsigned long hbtick_add_tick(unsigned long adj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 unsigned long val;
373
374 val = __hbird_read_stick() + adj;
375 __hbird_write_stick(val);
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return val;
378}
379
David S. Miller112f4872007-03-05 15:28:37 -0800380static int hbtick_add_compare(unsigned long adj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
David S. Miller112f4872007-03-05 15:28:37 -0800382 unsigned long val = __hbird_read_stick();
383 unsigned long val2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
David S. Miller112f4872007-03-05 15:28:37 -0800385 val &= ~TICKCMP_IRQ_BIT;
386 val += adj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 __hbird_write_compare(val);
388
David S. Miller112f4872007-03-05 15:28:37 -0800389 val2 = __hbird_read_stick() & ~TICKCMP_IRQ_BIT;
390
391 return ((long)(val2 - val)) > 0L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
David S. Millerd369ddd2005-07-10 15:45:11 -0700394static struct sparc64_tick_ops hbtick_operations __read_mostly = {
David S. Miller112f4872007-03-05 15:28:37 -0800395 .name = "hbtick",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 .init_tick = hbtick_init_tick,
David S. Miller112f4872007-03-05 15:28:37 -0800397 .disable_irq = hbtick_disable_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 .get_tick = hbtick_get_tick,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 .add_tick = hbtick_add_tick,
400 .add_compare = hbtick_add_compare,
401 .softint_mask = 1UL << 0,
402};
403
David S. Millerd369ddd2005-07-10 15:45:11 -0700404static unsigned long timer_ticks_per_nsec_quotient __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Thomas Gleixner82644452007-07-21 04:37:37 -0700406int update_persistent_clock(struct timespec now)
David S. Millera58c9f32007-02-22 04:16:21 -0800407{
David S. Millera0b31b52008-08-28 17:34:31 -0700408 struct rtc_device *rtc = rtc_class_open("rtc0");
409
410 if (rtc)
411 return rtc_set_mmss(rtc, now.tv_sec);
412
Thomas Gleixner82644452007-07-21 04:37:37 -0700413 return set_rtc_mmss(now.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
417static void __init kick_start_clock(void)
418{
Al Viroef0299b2005-04-24 12:28:36 -0700419 void __iomem *regs = mstk48t02_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 u8 sec, tmp;
421 int i, count;
422
423 prom_printf("CLOCK: Clock was stopped. Kick start ");
424
425 spin_lock_irq(&mostek_lock);
426
427 /* Turn on the kick start bit to start the oscillator. */
428 tmp = mostek_read(regs + MOSTEK_CREG);
429 tmp |= MSTK_CREG_WRITE;
430 mostek_write(regs + MOSTEK_CREG, tmp);
431 tmp = mostek_read(regs + MOSTEK_SEC);
432 tmp &= ~MSTK_STOP;
433 mostek_write(regs + MOSTEK_SEC, tmp);
434 tmp = mostek_read(regs + MOSTEK_HOUR);
435 tmp |= MSTK_KICK_START;
436 mostek_write(regs + MOSTEK_HOUR, tmp);
437 tmp = mostek_read(regs + MOSTEK_CREG);
438 tmp &= ~MSTK_CREG_WRITE;
439 mostek_write(regs + MOSTEK_CREG, tmp);
440
441 spin_unlock_irq(&mostek_lock);
442
443 /* Delay to allow the clock oscillator to start. */
444 sec = MSTK_REG_SEC(regs);
445 for (i = 0; i < 3; i++) {
446 while (sec == MSTK_REG_SEC(regs))
447 for (count = 0; count < 100000; count++)
448 /* nothing */ ;
449 prom_printf(".");
450 sec = MSTK_REG_SEC(regs);
451 }
452 prom_printf("\n");
453
454 spin_lock_irq(&mostek_lock);
455
456 /* Turn off kick start and set a "valid" time and date. */
457 tmp = mostek_read(regs + MOSTEK_CREG);
458 tmp |= MSTK_CREG_WRITE;
459 mostek_write(regs + MOSTEK_CREG, tmp);
460 tmp = mostek_read(regs + MOSTEK_HOUR);
461 tmp &= ~MSTK_KICK_START;
462 mostek_write(regs + MOSTEK_HOUR, tmp);
463 MSTK_SET_REG_SEC(regs,0);
464 MSTK_SET_REG_MIN(regs,0);
465 MSTK_SET_REG_HOUR(regs,0);
466 MSTK_SET_REG_DOW(regs,5);
467 MSTK_SET_REG_DOM(regs,1);
468 MSTK_SET_REG_MONTH(regs,8);
469 MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
470 tmp = mostek_read(regs + MOSTEK_CREG);
471 tmp &= ~MSTK_CREG_WRITE;
472 mostek_write(regs + MOSTEK_CREG, tmp);
473
474 spin_unlock_irq(&mostek_lock);
475
476 /* Ensure the kick start bit is off. If it isn't, turn it off. */
477 while (mostek_read(regs + MOSTEK_HOUR) & MSTK_KICK_START) {
478 prom_printf("CLOCK: Kick start still on!\n");
479
480 spin_lock_irq(&mostek_lock);
481
482 tmp = mostek_read(regs + MOSTEK_CREG);
483 tmp |= MSTK_CREG_WRITE;
484 mostek_write(regs + MOSTEK_CREG, tmp);
485
486 tmp = mostek_read(regs + MOSTEK_HOUR);
487 tmp &= ~MSTK_KICK_START;
488 mostek_write(regs + MOSTEK_HOUR, tmp);
489
490 tmp = mostek_read(regs + MOSTEK_CREG);
491 tmp &= ~MSTK_CREG_WRITE;
492 mostek_write(regs + MOSTEK_CREG, tmp);
493
494 spin_unlock_irq(&mostek_lock);
495 }
496
497 prom_printf("CLOCK: Kick start procedure successful.\n");
498}
499
500/* Return nonzero if the clock chip battery is low. */
501static int __init has_low_battery(void)
502{
Al Viroef0299b2005-04-24 12:28:36 -0700503 void __iomem *regs = mstk48t02_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 u8 data1, data2;
505
506 spin_lock_irq(&mostek_lock);
507
508 data1 = mostek_read(regs + MOSTEK_EEPROM); /* Read some data. */
509 mostek_write(regs + MOSTEK_EEPROM, ~data1); /* Write back the complement. */
510 data2 = mostek_read(regs + MOSTEK_EEPROM); /* Read back the complement. */
511 mostek_write(regs + MOSTEK_EEPROM, data1); /* Restore original value. */
512
513 spin_unlock_irq(&mostek_lock);
514
515 return (data1 == data2); /* Was the write blocked? */
516}
517
David S. Millercf3d7c12008-03-26 01:11:55 -0700518static void __init mostek_set_system_time(void __iomem *mregs)
519{
520 unsigned int year, mon, day, hour, min, sec;
521 u8 tmp;
522
523 spin_lock_irq(&mostek_lock);
524
525 /* Traditional Mostek chip. */
526 tmp = mostek_read(mregs + MOSTEK_CREG);
527 tmp |= MSTK_CREG_READ;
528 mostek_write(mregs + MOSTEK_CREG, tmp);
529
530 sec = MSTK_REG_SEC(mregs);
531 min = MSTK_REG_MIN(mregs);
532 hour = MSTK_REG_HOUR(mregs);
533 day = MSTK_REG_DOM(mregs);
534 mon = MSTK_REG_MONTH(mregs);
535 year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
536
537 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
538 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
539 set_normalized_timespec(&wall_to_monotonic,
540 -xtime.tv_sec, -xtime.tv_nsec);
541
542 tmp = mostek_read(mregs + MOSTEK_CREG);
543 tmp &= ~MSTK_CREG_READ;
544 mostek_write(mregs + MOSTEK_CREG, tmp);
545
546 spin_unlock_irq(&mostek_lock);
547}
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549/* Probe for the real time clock chip. */
550static void __init set_system_time(void)
551{
552 unsigned int year, mon, day, hour, min, sec;
Al Viroef0299b2005-04-24 12:28:36 -0700553 void __iomem *mregs = mstk48t02_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554#ifdef CONFIG_PCI
555 unsigned long dregs = ds1287_regs;
David S. Millerd037e052007-05-11 21:18:50 -0700556 void __iomem *bregs = bq4802_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557#else
558 unsigned long dregs = 0UL;
David S. Millerd037e052007-05-11 21:18:50 -0700559 void __iomem *bregs = 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
David S. Millerd037e052007-05-11 21:18:50 -0700562 if (!mregs && !dregs && !bregs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 prom_printf("Something wrong, clock regs not mapped yet.\n");
564 prom_halt();
565 }
566
567 if (mregs) {
David S. Millercf3d7c12008-03-26 01:11:55 -0700568 mostek_set_system_time(mregs);
569 return;
570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
David S. Millercf3d7c12008-03-26 01:11:55 -0700572 if (bregs) {
David S. Millerd037e052007-05-11 21:18:50 -0700573 unsigned char val = readb(bregs + 0x0e);
574 unsigned int century;
575
576 /* BQ4802 RTC chip. */
577
578 writeb(val | 0x08, bregs + 0x0e);
579
580 sec = readb(bregs + 0x00);
581 min = readb(bregs + 0x02);
582 hour = readb(bregs + 0x04);
583 day = readb(bregs + 0x06);
584 mon = readb(bregs + 0x09);
585 year = readb(bregs + 0x0a);
586 century = readb(bregs + 0x0f);
587
588 writeb(val, bregs + 0x0e);
589
590 BCD_TO_BIN(sec);
591 BCD_TO_BIN(min);
592 BCD_TO_BIN(hour);
593 BCD_TO_BIN(day);
594 BCD_TO_BIN(mon);
595 BCD_TO_BIN(year);
596 BCD_TO_BIN(century);
597
598 year += (century * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 /* Dallas 12887 RTC chip. */
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 do {
603 sec = CMOS_READ(RTC_SECONDS);
604 min = CMOS_READ(RTC_MINUTES);
605 hour = CMOS_READ(RTC_HOURS);
606 day = CMOS_READ(RTC_DAY_OF_MONTH);
607 mon = CMOS_READ(RTC_MONTH);
608 year = CMOS_READ(RTC_YEAR);
609 } while (sec != CMOS_READ(RTC_SECONDS));
Matt Mackall3dedf532006-03-28 01:56:01 -0800610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
612 BCD_TO_BIN(sec);
613 BCD_TO_BIN(min);
614 BCD_TO_BIN(hour);
615 BCD_TO_BIN(day);
616 BCD_TO_BIN(mon);
617 BCD_TO_BIN(year);
618 }
619 if ((year += 1900) < 1970)
620 year += 100;
621 }
622
623 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
624 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
625 set_normalized_timespec(&wall_to_monotonic,
626 -xtime.tv_sec, -xtime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
David S. Miller4bdff412006-02-11 01:01:55 -0800629/* davem suggests we keep this within the 4M locked kernel image */
630static u32 starfire_get_time(void)
631{
632 static char obp_gettod[32];
633 static u32 unix_tod;
634
635 sprintf(obp_gettod, "h# %08x unix-gettod",
636 (unsigned int) (long) &unix_tod);
637 prom_feval(obp_gettod);
638
639 return unix_tod;
640}
641
David S. Miller8ba706a2006-03-01 17:32:46 -0800642static int starfire_set_time(u32 val)
643{
644 /* Do nothing, time is set using the service processor
645 * console on this platform.
646 */
647 return 0;
648}
649
David S. Miller4bdff412006-02-11 01:01:55 -0800650static u32 hypervisor_get_time(void)
651{
David S. Miller7db35f32007-05-29 02:22:14 -0700652 unsigned long ret, time;
David S. Miller4bdff412006-02-11 01:01:55 -0800653 int retries = 10000;
654
655retry:
David S. Miller7db35f32007-05-29 02:22:14 -0700656 ret = sun4v_tod_get(&time);
657 if (ret == HV_EOK)
658 return time;
659 if (ret == HV_EWOULDBLOCK) {
David S. Miller4bdff412006-02-11 01:01:55 -0800660 if (--retries > 0) {
661 udelay(100);
662 goto retry;
663 }
664 printk(KERN_WARNING "SUN4V: tod_get() timed out.\n");
665 return 0;
666 }
667 printk(KERN_WARNING "SUN4V: tod_get() not supported.\n");
668 return 0;
669}
670
David S. Miller8ba706a2006-03-01 17:32:46 -0800671static int hypervisor_set_time(u32 secs)
672{
David S. Miller7db35f32007-05-29 02:22:14 -0700673 unsigned long ret;
David S. Miller8ba706a2006-03-01 17:32:46 -0800674 int retries = 10000;
675
676retry:
David S. Miller7db35f32007-05-29 02:22:14 -0700677 ret = sun4v_tod_set(secs);
678 if (ret == HV_EOK)
David S. Miller8ba706a2006-03-01 17:32:46 -0800679 return 0;
David S. Miller7db35f32007-05-29 02:22:14 -0700680 if (ret == HV_EWOULDBLOCK) {
David S. Miller8ba706a2006-03-01 17:32:46 -0800681 if (--retries > 0) {
682 udelay(100);
683 goto retry;
684 }
685 printk(KERN_WARNING "SUN4V: tod_set() timed out.\n");
686 return -EAGAIN;
687 }
688 printk(KERN_WARNING "SUN4V: tod_set() not supported.\n");
689 return -EOPNOTSUPP;
690}
691
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700692static int __init clock_model_matches(const char *model)
David S. Miller690c8fd2006-06-22 19:12:03 -0700693{
694 if (strcmp(model, "mk48t02") &&
695 strcmp(model, "mk48t08") &&
696 strcmp(model, "mk48t59") &&
697 strcmp(model, "m5819") &&
698 strcmp(model, "m5819p") &&
699 strcmp(model, "m5823") &&
David S. Millerd037e052007-05-11 21:18:50 -0700700 strcmp(model, "ds1287") &&
701 strcmp(model, "bq4802"))
David S. Miller690c8fd2006-06-22 19:12:03 -0700702 return 0;
703
704 return 1;
705}
706
David S. Milleree5caf02006-06-29 14:36:52 -0700707static int __devinit clock_probe(struct of_device *op, const struct of_device_id *match)
David S. Miller690c8fd2006-06-22 19:12:03 -0700708{
David S. Milleree5caf02006-06-29 14:36:52 -0700709 struct device_node *dp = op->node;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700710 const char *model = of_get_property(dp, "model", NULL);
David S. Millerd037e052007-05-11 21:18:50 -0700711 const char *compat = of_get_property(dp, "compatible", NULL);
David S. Milleree5caf02006-06-29 14:36:52 -0700712 unsigned long size, flags;
713 void __iomem *regs;
David S. Miller690c8fd2006-06-22 19:12:03 -0700714
David S. Millerd037e052007-05-11 21:18:50 -0700715 if (!model)
716 model = compat;
717
David S. Milleree5caf02006-06-29 14:36:52 -0700718 if (!model || !clock_model_matches(model))
719 return -ENODEV;
David S. Miller690c8fd2006-06-22 19:12:03 -0700720
David S. Miller91521482006-06-29 14:39:40 -0700721 /* On an Enterprise system there can be multiple mostek clocks.
722 * We should only match the one that is on the central FHC bus.
723 */
David S. Millerc3a8b852006-06-29 14:43:37 -0700724 if (!strcmp(dp->parent->name, "fhc") &&
725 strcmp(dp->parent->parent->name, "central") != 0)
David S. Miller91521482006-06-29 14:39:40 -0700726 return -ENODEV;
727
David S. Milleree5caf02006-06-29 14:36:52 -0700728 size = (op->resource[0].end - op->resource[0].start) + 1;
729 regs = of_ioremap(&op->resource[0], 0, size, "clock");
730 if (!regs)
731 return -ENOMEM;
David S. Miller690c8fd2006-06-22 19:12:03 -0700732
Randy Dunlap72335892006-07-05 20:18:39 -0700733#ifdef CONFIG_PCI
David S. Miller690c8fd2006-06-22 19:12:03 -0700734 if (!strcmp(model, "ds1287") ||
735 !strcmp(model, "m5819") ||
736 !strcmp(model, "m5819p") ||
737 !strcmp(model, "m5823")) {
David S. Milleree5caf02006-06-29 14:36:52 -0700738 ds1287_regs = (unsigned long) regs;
David S. Millerd037e052007-05-11 21:18:50 -0700739 } else if (!strcmp(model, "bq4802")) {
740 bq4802_regs = regs;
Randy Dunlap72335892006-07-05 20:18:39 -0700741 } else
742#endif
743 if (model[5] == '0' && model[6] == '2') {
David S. Milleree5caf02006-06-29 14:36:52 -0700744 mstk48t02_regs = regs;
745 } else if(model[5] == '0' && model[6] == '8') {
746 mstk48t08_regs = regs;
747 mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02;
David S. Miller690c8fd2006-06-22 19:12:03 -0700748 } else {
David S. Milleree5caf02006-06-29 14:36:52 -0700749 mstk48t59_regs = regs;
David S. Miller690c8fd2006-06-22 19:12:03 -0700750 mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
751 }
David S. Miller690c8fd2006-06-22 19:12:03 -0700752
David S. Milleree5caf02006-06-29 14:36:52 -0700753 printk(KERN_INFO "%s: Clock regs at %p\n", dp->full_name, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
David S. Miller690c8fd2006-06-22 19:12:03 -0700755 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
David S. Millerb4bca262005-04-21 21:42:34 -0700757 if (mstk48t02_regs != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 /* Report a low battery voltage condition. */
759 if (has_low_battery())
760 prom_printf("NVRAM: Low battery voltage!\n");
761
762 /* Kick start the clock if it is completely stopped. */
763 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
764 kick_start_clock();
765 }
766
767 set_system_time();
768
769 local_irq_restore(flags);
David S. Milleree5caf02006-06-29 14:36:52 -0700770
771 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
David S. Milleree5caf02006-06-29 14:36:52 -0700774static struct of_device_id clock_match[] = {
775 {
776 .name = "eeprom",
777 },
778 {
779 .name = "rtc",
780 },
781 {},
782};
783
784static struct of_platform_driver clock_driver = {
David S. Milleree5caf02006-06-29 14:36:52 -0700785 .match_table = clock_match,
786 .probe = clock_probe,
Stephen Rothwella2cd1552007-10-10 23:27:34 -0700787 .driver = {
788 .name = "clock",
789 },
David S. Milleree5caf02006-06-29 14:36:52 -0700790};
791
792static int __init clock_init(void)
793{
794 if (this_is_starfire) {
795 xtime.tv_sec = starfire_get_time();
796 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
797 set_normalized_timespec(&wall_to_monotonic,
798 -xtime.tv_sec, -xtime.tv_nsec);
799 return 0;
800 }
801 if (tlb_type == hypervisor) {
802 xtime.tv_sec = hypervisor_get_time();
803 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
804 set_normalized_timespec(&wall_to_monotonic,
805 -xtime.tv_sec, -xtime.tv_nsec);
806 return 0;
807 }
808
Stephen Rothwell37b77542007-04-30 17:43:56 +1000809 return of_register_driver(&clock_driver, &of_platform_bus_type);
David S. Milleree5caf02006-06-29 14:36:52 -0700810}
811
812/* Must be after subsys_initcall() so that busses are probed. Must
813 * be before device_initcall() because things like the RTC driver
814 * need to see the clock registers.
815 */
816fs_initcall(clock_init);
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818/* This is gets the master TICK_INT timer going. */
819static unsigned long sparc64_init_timers(void)
820{
David S. Miller07f8e5f2006-06-21 23:34:02 -0700821 struct device_node *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 unsigned long clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
David S. Miller07f8e5f2006-06-21 23:34:02 -0700824 dp = of_find_node_by_path("/");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (tlb_type == spitfire) {
826 unsigned long ver, manuf, impl;
827
828 __asm__ __volatile__ ("rdpr %%ver, %0"
829 : "=&r" (ver));
830 manuf = ((ver >> 48) & 0xffff);
831 impl = ((ver >> 32) & 0xffff);
832 if (manuf == 0x17 && impl == 0x13) {
833 /* Hummingbird, aka Ultra-IIe */
834 tick_ops = &hbtick_operations;
David S. Miller5cbc3072007-05-25 15:49:59 -0700835 clock = of_getintprop_default(dp, "stick-frequency", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 } else {
837 tick_ops = &tick_operations;
David S. Miller5cbc3072007-05-25 15:49:59 -0700838 clock = local_cpu_data().clock_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840 } else {
841 tick_ops = &stick_operations;
David S. Miller5cbc3072007-05-25 15:49:59 -0700842 clock = of_getintprop_default(dp, "stick-frequency", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return clock;
846}
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848struct freq_table {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 unsigned long clock_tick_ref;
850 unsigned int ref_freq;
851};
David S. Miller3763be32006-02-17 12:33:13 -0800852static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854unsigned long sparc64_get_clock_tick(unsigned int cpu)
855{
856 struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
857
858 if (ft->clock_tick_ref)
859 return ft->clock_tick_ref;
860 return cpu_data(cpu).clock_tick;
861}
862
863#ifdef CONFIG_CPU_FREQ
864
865static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
866 void *data)
867{
868 struct cpufreq_freqs *freq = data;
869 unsigned int cpu = freq->cpu;
870 struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
871
872 if (!ft->ref_freq) {
873 ft->ref_freq = freq->old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 ft->clock_tick_ref = cpu_data(cpu).clock_tick;
875 }
876 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
877 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
878 (val == CPUFREQ_RESUMECHANGE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 cpu_data(cpu).clock_tick =
880 cpufreq_scale(ft->clock_tick_ref,
881 ft->ref_freq,
882 freq->new);
883 }
884
885 return 0;
886}
887
888static struct notifier_block sparc64_cpufreq_notifier_block = {
889 .notifier_call = sparc64_cpufreq_notifier
890};
891
David S. Miller7ae93f52008-07-23 16:21:07 -0700892static int __init register_sparc64_cpufreq_notifier(void)
893{
894
895 cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
896 CPUFREQ_TRANSITION_NOTIFIER);
897 return 0;
898}
899
900core_initcall(register_sparc64_cpufreq_notifier);
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902#endif /* CONFIG_CPU_FREQ */
903
David S. Miller112f4872007-03-05 15:28:37 -0800904static int sparc64_next_event(unsigned long delta,
905 struct clock_event_device *evt)
906{
David S. Millerd62c6f02007-03-27 01:20:14 -0700907 return tick_ops->add_compare(delta) ? -ETIME : 0;
David S. Miller112f4872007-03-05 15:28:37 -0800908}
909
910static void sparc64_timer_setup(enum clock_event_mode mode,
911 struct clock_event_device *evt)
912{
913 switch (mode) {
914 case CLOCK_EVT_MODE_ONESHOT:
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700915 case CLOCK_EVT_MODE_RESUME:
David S. Miller112f4872007-03-05 15:28:37 -0800916 break;
917
918 case CLOCK_EVT_MODE_SHUTDOWN:
919 tick_ops->disable_irq();
920 break;
921
922 case CLOCK_EVT_MODE_PERIODIC:
923 case CLOCK_EVT_MODE_UNUSED:
924 WARN_ON(1);
925 break;
926 };
927}
928
929static struct clock_event_device sparc64_clockevent = {
930 .features = CLOCK_EVT_FEAT_ONESHOT,
931 .set_mode = sparc64_timer_setup,
932 .set_next_event = sparc64_next_event,
933 .rating = 100,
934 .shift = 30,
935 .irq = -1,
936};
937static DEFINE_PER_CPU(struct clock_event_device, sparc64_events);
938
939void timer_interrupt(int irq, struct pt_regs *regs)
940{
941 struct pt_regs *old_regs = set_irq_regs(regs);
942 unsigned long tick_mask = tick_ops->softint_mask;
943 int cpu = smp_processor_id();
944 struct clock_event_device *evt = &per_cpu(sparc64_events, cpu);
945
946 clear_softint(tick_mask);
947
948 irq_enter();
949
950 kstat_this_cpu.irqs[0]++;
951
952 if (unlikely(!evt->event_handler)) {
953 printk(KERN_WARNING
954 "Spurious SPARC64 timer interrupt on cpu %d\n", cpu);
955 } else
956 evt->event_handler(evt);
957
958 irq_exit();
959
960 set_irq_regs(old_regs);
961}
962
963void __devinit setup_sparc64_timer(void)
964{
965 struct clock_event_device *sevt;
966 unsigned long pstate;
967
968 /* Guarantee that the following sequences execute
969 * uninterrupted.
970 */
971 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
972 "wrpr %0, %1, %%pstate"
973 : "=r" (pstate)
974 : "i" (PSTATE_IE));
975
976 tick_ops->init_tick();
977
978 /* Restore PSTATE_IE. */
979 __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
980 : /* no outputs */
981 : "r" (pstate));
982
983 sevt = &__get_cpu_var(sparc64_events);
984
985 memcpy(sevt, &sparc64_clockevent, sizeof(*sevt));
986 sevt->cpumask = cpumask_of_cpu(smp_processor_id());
987
988 clockevents_register_device(sevt);
989}
990
David S. Miller03983ab2007-05-17 22:55:26 -0700991#define SPARC64_NSEC_PER_CYC_SHIFT 10UL
David S. Miller112f4872007-03-05 15:28:37 -0800992
993static struct clocksource clocksource_tick = {
994 .rating = 100,
995 .mask = CLOCKSOURCE_MASK(64),
996 .shift = 16,
997 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998};
999
David S. Miller112f4872007-03-05 15:28:37 -08001000static void __init setup_clockevent_multiplier(unsigned long hz)
1001{
1002 unsigned long mult, shift = 32;
1003
1004 while (1) {
1005 mult = div_sc(hz, NSEC_PER_SEC, shift);
1006 if (mult && (mult >> 32UL) == 0UL)
1007 break;
1008
1009 shift--;
1010 }
1011
1012 sparc64_clockevent.shift = shift;
1013 sparc64_clockevent.mult = mult;
1014}
1015
David S. Miller8b99cfb2007-07-14 02:23:37 -07001016static unsigned long tb_ticks_per_usec __read_mostly;
1017
1018void __delay(unsigned long loops)
1019{
1020 unsigned long bclock, now;
1021
1022 bclock = tick_ops->get_tick();
1023 do {
1024 now = tick_ops->get_tick();
1025 } while ((now-bclock) < loops);
1026}
1027EXPORT_SYMBOL(__delay);
1028
1029void udelay(unsigned long usecs)
1030{
1031 __delay(tb_ticks_per_usec * usecs);
1032}
1033EXPORT_SYMBOL(udelay);
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035void __init time_init(void)
1036{
1037 unsigned long clock = sparc64_init_timers();
1038
David S. Miller8b99cfb2007-07-14 02:23:37 -07001039 tb_ticks_per_usec = clock / USEC_PER_SEC;
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 timer_ticks_per_nsec_quotient =
David S. Miller112f4872007-03-05 15:28:37 -08001042 clocksource_hz2mult(clock, SPARC64_NSEC_PER_CYC_SHIFT);
1043
1044 clocksource_tick.name = tick_ops->name;
1045 clocksource_tick.mult =
1046 clocksource_hz2mult(clock,
1047 clocksource_tick.shift);
1048 clocksource_tick.read = tick_ops->get_tick;
1049
1050 printk("clocksource: mult[%x] shift[%d]\n",
1051 clocksource_tick.mult, clocksource_tick.shift);
1052
1053 clocksource_register(&clocksource_tick);
1054
1055 sparc64_clockevent.name = tick_ops->name;
1056
1057 setup_clockevent_multiplier(clock);
1058
1059 sparc64_clockevent.max_delta_ns =
David S. Millercf3d7c12008-03-26 01:11:55 -07001060 clockevent_delta2ns(0x7fffffffffffffffUL, &sparc64_clockevent);
David S. Miller112f4872007-03-05 15:28:37 -08001061 sparc64_clockevent.min_delta_ns =
1062 clockevent_delta2ns(0xF, &sparc64_clockevent);
1063
1064 printk("clockevent: mult[%lx] shift[%d]\n",
1065 sparc64_clockevent.mult, sparc64_clockevent.shift);
1066
1067 setup_sparc64_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
1070unsigned long long sched_clock(void)
1071{
1072 unsigned long ticks = tick_ops->get_tick();
1073
1074 return (ticks * timer_ticks_per_nsec_quotient)
1075 >> SPARC64_NSEC_PER_CYC_SHIFT;
1076}
1077
1078static int set_rtc_mmss(unsigned long nowtime)
1079{
1080 int real_seconds, real_minutes, chip_minutes;
Al Viroef0299b2005-04-24 12:28:36 -07001081 void __iomem *mregs = mstk48t02_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082#ifdef CONFIG_PCI
1083 unsigned long dregs = ds1287_regs;
David S. Millerd037e052007-05-11 21:18:50 -07001084 void __iomem *bregs = bq4802_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085#else
1086 unsigned long dregs = 0UL;
David S. Millerd037e052007-05-11 21:18:50 -07001087 void __iomem *bregs = 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088#endif
1089 unsigned long flags;
1090 u8 tmp;
1091
1092 /*
1093 * Not having a register set can lead to trouble.
1094 * Also starfire doesn't have a tod clock.
1095 */
David S. Miller23e8bc22007-10-27 22:33:33 -07001096 if (!mregs && !dregs && !bregs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return -1;
1098
1099 if (mregs) {
1100 spin_lock_irqsave(&mostek_lock, flags);
1101
1102 /* Read the current RTC minutes. */
1103 tmp = mostek_read(mregs + MOSTEK_CREG);
1104 tmp |= MSTK_CREG_READ;
1105 mostek_write(mregs + MOSTEK_CREG, tmp);
1106
1107 chip_minutes = MSTK_REG_MIN(mregs);
1108
1109 tmp = mostek_read(mregs + MOSTEK_CREG);
1110 tmp &= ~MSTK_CREG_READ;
1111 mostek_write(mregs + MOSTEK_CREG, tmp);
1112
1113 /*
1114 * since we're only adjusting minutes and seconds,
1115 * don't interfere with hour overflow. This avoids
1116 * messing with unknown time zones but requires your
1117 * RTC not to be off by more than 15 minutes
1118 */
1119 real_seconds = nowtime % 60;
1120 real_minutes = nowtime / 60;
1121 if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
1122 real_minutes += 30; /* correct for half hour time zone */
1123 real_minutes %= 60;
1124
1125 if (abs(real_minutes - chip_minutes) < 30) {
1126 tmp = mostek_read(mregs + MOSTEK_CREG);
1127 tmp |= MSTK_CREG_WRITE;
1128 mostek_write(mregs + MOSTEK_CREG, tmp);
1129
1130 MSTK_SET_REG_SEC(mregs,real_seconds);
1131 MSTK_SET_REG_MIN(mregs,real_minutes);
1132
1133 tmp = mostek_read(mregs + MOSTEK_CREG);
1134 tmp &= ~MSTK_CREG_WRITE;
1135 mostek_write(mregs + MOSTEK_CREG, tmp);
1136
1137 spin_unlock_irqrestore(&mostek_lock, flags);
1138
1139 return 0;
1140 } else {
1141 spin_unlock_irqrestore(&mostek_lock, flags);
1142
1143 return -1;
1144 }
David S. Millerd037e052007-05-11 21:18:50 -07001145 } else if (bregs) {
1146 int retval = 0;
1147 unsigned char val = readb(bregs + 0x0e);
1148
1149 /* BQ4802 RTC chip. */
1150
1151 writeb(val | 0x08, bregs + 0x0e);
1152
1153 chip_minutes = readb(bregs + 0x02);
1154 BCD_TO_BIN(chip_minutes);
1155 real_seconds = nowtime % 60;
1156 real_minutes = nowtime / 60;
1157 if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
1158 real_minutes += 30;
1159 real_minutes %= 60;
1160
1161 if (abs(real_minutes - chip_minutes) < 30) {
1162 BIN_TO_BCD(real_seconds);
1163 BIN_TO_BCD(real_minutes);
1164 writeb(real_seconds, bregs + 0x00);
1165 writeb(real_minutes, bregs + 0x02);
1166 } else {
1167 printk(KERN_WARNING
1168 "set_rtc_mmss: can't update from %d to %d\n",
1169 chip_minutes, real_minutes);
1170 retval = -1;
1171 }
1172
1173 writeb(val, bregs + 0x0e);
1174
1175 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 } else {
1177 int retval = 0;
1178 unsigned char save_control, save_freq_select;
1179
1180 /* Stolen from arch/i386/kernel/time.c, see there for
1181 * credits and descriptive comments.
1182 */
1183 spin_lock_irqsave(&rtc_lock, flags);
1184 save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
1185 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
1186
1187 save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
1188 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
1189
1190 chip_minutes = CMOS_READ(RTC_MINUTES);
1191 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1192 BCD_TO_BIN(chip_minutes);
1193 real_seconds = nowtime % 60;
1194 real_minutes = nowtime / 60;
1195 if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
1196 real_minutes += 30;
1197 real_minutes %= 60;
1198
1199 if (abs(real_minutes - chip_minutes) < 30) {
1200 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
1201 BIN_TO_BCD(real_seconds);
1202 BIN_TO_BCD(real_minutes);
1203 }
1204 CMOS_WRITE(real_seconds,RTC_SECONDS);
1205 CMOS_WRITE(real_minutes,RTC_MINUTES);
1206 } else {
1207 printk(KERN_WARNING
1208 "set_rtc_mmss: can't update from %d to %d\n",
1209 chip_minutes, real_minutes);
1210 retval = -1;
1211 }
1212
1213 CMOS_WRITE(save_control, RTC_CONTROL);
1214 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1215 spin_unlock_irqrestore(&rtc_lock, flags);
1216
1217 return retval;
1218 }
1219}
David S. Miller8ba706a2006-03-01 17:32:46 -08001220
1221#define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
1222static unsigned char mini_rtc_status; /* bitmapped status byte. */
1223
David S. Miller8ba706a2006-03-01 17:32:46 -08001224#define FEBRUARY 2
1225#define STARTOFTIME 1970
1226#define SECDAY 86400L
1227#define SECYR (SECDAY * 365)
1228#define leapyear(year) ((year) % 4 == 0 && \
1229 ((year) % 100 != 0 || (year) % 400 == 0))
1230#define days_in_year(a) (leapyear(a) ? 366 : 365)
1231#define days_in_month(a) (month_days[(a) - 1])
1232
1233static int month_days[12] = {
1234 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
1235};
1236
1237/*
1238 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
1239 */
1240static void GregorianDay(struct rtc_time * tm)
1241{
1242 int leapsToDate;
1243 int lastYear;
1244 int day;
1245 int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
1246
1247 lastYear = tm->tm_year - 1;
1248
1249 /*
1250 * Number of leap corrections to apply up to end of last year
1251 */
1252 leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
1253
1254 /*
1255 * This year is a leap year if it is divisible by 4 except when it is
1256 * divisible by 100 unless it is divisible by 400
1257 *
1258 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
1259 */
1260 day = tm->tm_mon > 2 && leapyear(tm->tm_year);
1261
1262 day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
1263 tm->tm_mday;
1264
1265 tm->tm_wday = day % 7;
1266}
1267
1268static void to_tm(int tim, struct rtc_time *tm)
1269{
1270 register int i;
1271 register long hms, day;
1272
1273 day = tim / SECDAY;
1274 hms = tim % SECDAY;
1275
1276 /* Hours, minutes, seconds are easy */
1277 tm->tm_hour = hms / 3600;
1278 tm->tm_min = (hms % 3600) / 60;
1279 tm->tm_sec = (hms % 3600) % 60;
1280
1281 /* Number of years in days */
1282 for (i = STARTOFTIME; day >= days_in_year(i); i++)
1283 day -= days_in_year(i);
1284 tm->tm_year = i;
1285
1286 /* Number of months in days left */
1287 if (leapyear(tm->tm_year))
1288 days_in_month(FEBRUARY) = 29;
1289 for (i = 1; day >= days_in_month(i); i++)
1290 day -= days_in_month(i);
1291 days_in_month(FEBRUARY) = 28;
1292 tm->tm_mon = i;
1293
1294 /* Days are what is left over (+1) from all that. */
1295 tm->tm_mday = day + 1;
1296
1297 /*
1298 * Determine the day of week
1299 */
1300 GregorianDay(tm);
1301}
1302
1303/* Both Starfire and SUN4V give us seconds since Jan 1st, 1970,
1304 * aka Unix time. So we have to convert to/from rtc_time.
1305 */
David S. Millerd037e052007-05-11 21:18:50 -07001306static void starfire_get_rtc_time(struct rtc_time *time)
David S. Miller8ba706a2006-03-01 17:32:46 -08001307{
David S. Millerd037e052007-05-11 21:18:50 -07001308 u32 seconds = starfire_get_time();
David S. Miller8ba706a2006-03-01 17:32:46 -08001309
1310 to_tm(seconds, time);
David S. Millerc4f8ef72006-03-02 20:28:34 -08001311 time->tm_year -= 1900;
1312 time->tm_mon -= 1;
David S. Miller8ba706a2006-03-01 17:32:46 -08001313}
1314
David S. Millerd037e052007-05-11 21:18:50 -07001315static int starfire_set_rtc_time(struct rtc_time *time)
David S. Miller8ba706a2006-03-01 17:32:46 -08001316{
1317 u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
1318 time->tm_mday, time->tm_hour,
1319 time->tm_min, time->tm_sec);
David S. Millerd037e052007-05-11 21:18:50 -07001320
1321 return starfire_set_time(seconds);
1322}
1323
1324static void hypervisor_get_rtc_time(struct rtc_time *time)
1325{
1326 u32 seconds = hypervisor_get_time();
1327
1328 to_tm(seconds, time);
1329 time->tm_year -= 1900;
1330 time->tm_mon -= 1;
1331}
1332
1333static int hypervisor_set_rtc_time(struct rtc_time *time)
1334{
1335 u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
1336 time->tm_mday, time->tm_hour,
1337 time->tm_min, time->tm_sec);
1338
1339 return hypervisor_set_time(seconds);
1340}
1341
Horst H. von Brand71898592007-05-26 17:47:53 -07001342#ifdef CONFIG_PCI
David S. Millerd037e052007-05-11 21:18:50 -07001343static void bq4802_get_rtc_time(struct rtc_time *time)
1344{
1345 unsigned char val = readb(bq4802_regs + 0x0e);
1346 unsigned int century;
1347
1348 writeb(val | 0x08, bq4802_regs + 0x0e);
1349
1350 time->tm_sec = readb(bq4802_regs + 0x00);
1351 time->tm_min = readb(bq4802_regs + 0x02);
1352 time->tm_hour = readb(bq4802_regs + 0x04);
1353 time->tm_mday = readb(bq4802_regs + 0x06);
1354 time->tm_mon = readb(bq4802_regs + 0x09);
1355 time->tm_year = readb(bq4802_regs + 0x0a);
1356 time->tm_wday = readb(bq4802_regs + 0x08);
1357 century = readb(bq4802_regs + 0x0f);
1358
1359 writeb(val, bq4802_regs + 0x0e);
1360
1361 BCD_TO_BIN(time->tm_sec);
1362 BCD_TO_BIN(time->tm_min);
1363 BCD_TO_BIN(time->tm_hour);
1364 BCD_TO_BIN(time->tm_mday);
1365 BCD_TO_BIN(time->tm_mon);
1366 BCD_TO_BIN(time->tm_year);
1367 BCD_TO_BIN(time->tm_wday);
1368 BCD_TO_BIN(century);
1369
1370 time->tm_year += (century * 100);
1371 time->tm_year -= 1900;
1372
1373 time->tm_mon--;
1374}
1375
1376static int bq4802_set_rtc_time(struct rtc_time *time)
1377{
1378 unsigned char val = readb(bq4802_regs + 0x0e);
1379 unsigned char sec, min, hrs, day, mon, yrs, century;
1380 unsigned int year;
1381
1382 year = time->tm_year + 1900;
1383 century = year / 100;
1384 yrs = year % 100;
1385
1386 mon = time->tm_mon + 1; /* tm_mon starts at zero */
1387 day = time->tm_mday;
1388 hrs = time->tm_hour;
1389 min = time->tm_min;
1390 sec = time->tm_sec;
1391
1392 BIN_TO_BCD(sec);
1393 BIN_TO_BCD(min);
1394 BIN_TO_BCD(hrs);
1395 BIN_TO_BCD(day);
1396 BIN_TO_BCD(mon);
1397 BIN_TO_BCD(yrs);
1398 BIN_TO_BCD(century);
1399
1400 writeb(val | 0x08, bq4802_regs + 0x0e);
1401
1402 writeb(sec, bq4802_regs + 0x00);
1403 writeb(min, bq4802_regs + 0x02);
1404 writeb(hrs, bq4802_regs + 0x04);
1405 writeb(day, bq4802_regs + 0x06);
1406 writeb(mon, bq4802_regs + 0x09);
1407 writeb(yrs, bq4802_regs + 0x0a);
1408 writeb(century, bq4802_regs + 0x0f);
1409
1410 writeb(val, bq4802_regs + 0x0e);
1411
1412 return 0;
1413}
David S. Millercdee99d2007-07-19 13:59:58 -07001414
1415static void cmos_get_rtc_time(struct rtc_time *rtc_tm)
1416{
1417 unsigned char ctrl;
1418
1419 rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
1420 rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
1421 rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
1422 rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
1423 rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
1424 rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
1425 rtc_tm->tm_wday = CMOS_READ(RTC_DAY_OF_WEEK);
1426
1427 ctrl = CMOS_READ(RTC_CONTROL);
1428 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
1429 BCD_TO_BIN(rtc_tm->tm_sec);
1430 BCD_TO_BIN(rtc_tm->tm_min);
1431 BCD_TO_BIN(rtc_tm->tm_hour);
1432 BCD_TO_BIN(rtc_tm->tm_mday);
1433 BCD_TO_BIN(rtc_tm->tm_mon);
1434 BCD_TO_BIN(rtc_tm->tm_year);
1435 BCD_TO_BIN(rtc_tm->tm_wday);
1436 }
1437
1438 if (rtc_tm->tm_year <= 69)
1439 rtc_tm->tm_year += 100;
1440
1441 rtc_tm->tm_mon--;
1442}
1443
1444static int cmos_set_rtc_time(struct rtc_time *rtc_tm)
1445{
1446 unsigned char mon, day, hrs, min, sec;
1447 unsigned char save_control, save_freq_select;
1448 unsigned int yrs;
1449
1450 yrs = rtc_tm->tm_year;
1451 mon = rtc_tm->tm_mon + 1;
1452 day = rtc_tm->tm_mday;
1453 hrs = rtc_tm->tm_hour;
1454 min = rtc_tm->tm_min;
1455 sec = rtc_tm->tm_sec;
1456
1457 if (yrs >= 100)
1458 yrs -= 100;
1459
1460 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
1461 BIN_TO_BCD(sec);
1462 BIN_TO_BCD(min);
1463 BIN_TO_BCD(hrs);
1464 BIN_TO_BCD(day);
1465 BIN_TO_BCD(mon);
1466 BIN_TO_BCD(yrs);
1467 }
1468
1469 save_control = CMOS_READ(RTC_CONTROL);
1470 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
1471 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1472 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
1473
1474 CMOS_WRITE(yrs, RTC_YEAR);
1475 CMOS_WRITE(mon, RTC_MONTH);
1476 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
1477 CMOS_WRITE(hrs, RTC_HOURS);
1478 CMOS_WRITE(min, RTC_MINUTES);
1479 CMOS_WRITE(sec, RTC_SECONDS);
1480
1481 CMOS_WRITE(save_control, RTC_CONTROL);
1482 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1483
1484 return 0;
1485}
Horst H. von Brand71898592007-05-26 17:47:53 -07001486#endif /* CONFIG_PCI */
David S. Millerd037e052007-05-11 21:18:50 -07001487
David S. Millera0afaa62007-07-27 14:40:27 -07001488static void mostek_get_rtc_time(struct rtc_time *rtc_tm)
1489{
1490 void __iomem *regs = mstk48t02_regs;
1491 u8 tmp;
1492
1493 spin_lock_irq(&mostek_lock);
1494
1495 tmp = mostek_read(regs + MOSTEK_CREG);
1496 tmp |= MSTK_CREG_READ;
1497 mostek_write(regs + MOSTEK_CREG, tmp);
1498
1499 rtc_tm->tm_sec = MSTK_REG_SEC(regs);
1500 rtc_tm->tm_min = MSTK_REG_MIN(regs);
1501 rtc_tm->tm_hour = MSTK_REG_HOUR(regs);
1502 rtc_tm->tm_mday = MSTK_REG_DOM(regs);
1503 rtc_tm->tm_mon = MSTK_REG_MONTH(regs);
1504 rtc_tm->tm_year = MSTK_CVT_YEAR( MSTK_REG_YEAR(regs) );
1505 rtc_tm->tm_wday = MSTK_REG_DOW(regs);
1506
1507 tmp = mostek_read(regs + MOSTEK_CREG);
1508 tmp &= ~MSTK_CREG_READ;
1509 mostek_write(regs + MOSTEK_CREG, tmp);
1510
1511 spin_unlock_irq(&mostek_lock);
1512
1513 rtc_tm->tm_mon--;
1514 rtc_tm->tm_wday--;
1515 rtc_tm->tm_year -= 1900;
1516}
1517
1518static int mostek_set_rtc_time(struct rtc_time *rtc_tm)
1519{
1520 unsigned char mon, day, hrs, min, sec, wday;
1521 void __iomem *regs = mstk48t02_regs;
1522 unsigned int yrs;
1523 u8 tmp;
1524
1525 yrs = rtc_tm->tm_year + 1900;
1526 mon = rtc_tm->tm_mon + 1;
1527 day = rtc_tm->tm_mday;
1528 wday = rtc_tm->tm_wday + 1;
1529 hrs = rtc_tm->tm_hour;
1530 min = rtc_tm->tm_min;
1531 sec = rtc_tm->tm_sec;
1532
1533 spin_lock_irq(&mostek_lock);
1534
1535 tmp = mostek_read(regs + MOSTEK_CREG);
1536 tmp |= MSTK_CREG_WRITE;
1537 mostek_write(regs + MOSTEK_CREG, tmp);
1538
1539 MSTK_SET_REG_SEC(regs, sec);
1540 MSTK_SET_REG_MIN(regs, min);
1541 MSTK_SET_REG_HOUR(regs, hrs);
1542 MSTK_SET_REG_DOW(regs, wday);
1543 MSTK_SET_REG_DOM(regs, day);
1544 MSTK_SET_REG_MONTH(regs, mon);
1545 MSTK_SET_REG_YEAR(regs, yrs - MSTK_YEAR_ZERO);
1546
1547 tmp = mostek_read(regs + MOSTEK_CREG);
1548 tmp &= ~MSTK_CREG_WRITE;
1549 mostek_write(regs + MOSTEK_CREG, tmp);
1550
1551 spin_unlock_irq(&mostek_lock);
1552
1553 return 0;
1554}
1555
David S. Millerd037e052007-05-11 21:18:50 -07001556struct mini_rtc_ops {
1557 void (*get_rtc_time)(struct rtc_time *);
1558 int (*set_rtc_time)(struct rtc_time *);
1559};
1560
1561static struct mini_rtc_ops starfire_rtc_ops = {
1562 .get_rtc_time = starfire_get_rtc_time,
1563 .set_rtc_time = starfire_set_rtc_time,
1564};
1565
1566static struct mini_rtc_ops hypervisor_rtc_ops = {
1567 .get_rtc_time = hypervisor_get_rtc_time,
1568 .set_rtc_time = hypervisor_set_rtc_time,
1569};
1570
Horst H. von Brand71898592007-05-26 17:47:53 -07001571#ifdef CONFIG_PCI
David S. Millerd037e052007-05-11 21:18:50 -07001572static struct mini_rtc_ops bq4802_rtc_ops = {
1573 .get_rtc_time = bq4802_get_rtc_time,
1574 .set_rtc_time = bq4802_set_rtc_time,
1575};
David S. Millercdee99d2007-07-19 13:59:58 -07001576
1577static struct mini_rtc_ops cmos_rtc_ops = {
1578 .get_rtc_time = cmos_get_rtc_time,
1579 .set_rtc_time = cmos_set_rtc_time,
1580};
Horst H. von Brand71898592007-05-26 17:47:53 -07001581#endif /* CONFIG_PCI */
David S. Millerd037e052007-05-11 21:18:50 -07001582
David S. Millera0afaa62007-07-27 14:40:27 -07001583static struct mini_rtc_ops mostek_rtc_ops = {
1584 .get_rtc_time = mostek_get_rtc_time,
1585 .set_rtc_time = mostek_set_rtc_time,
1586};
1587
David S. Millerd037e052007-05-11 21:18:50 -07001588static struct mini_rtc_ops *mini_rtc_ops;
1589
1590static inline void mini_get_rtc_time(struct rtc_time *time)
1591{
1592 unsigned long flags;
1593
1594 spin_lock_irqsave(&rtc_lock, flags);
1595 mini_rtc_ops->get_rtc_time(time);
1596 spin_unlock_irqrestore(&rtc_lock, flags);
1597}
1598
1599static inline int mini_set_rtc_time(struct rtc_time *time)
1600{
David S. Miller8ba706a2006-03-01 17:32:46 -08001601 unsigned long flags;
1602 int err;
1603
1604 spin_lock_irqsave(&rtc_lock, flags);
David S. Millerd037e052007-05-11 21:18:50 -07001605 err = mini_rtc_ops->set_rtc_time(time);
David S. Miller8ba706a2006-03-01 17:32:46 -08001606 spin_unlock_irqrestore(&rtc_lock, flags);
1607
1608 return err;
1609}
1610
1611static int mini_rtc_ioctl(struct inode *inode, struct file *file,
1612 unsigned int cmd, unsigned long arg)
1613{
1614 struct rtc_time wtime;
1615 void __user *argp = (void __user *)arg;
1616
1617 switch (cmd) {
1618
1619 case RTC_PLL_GET:
1620 return -EINVAL;
1621
1622 case RTC_PLL_SET:
1623 return -EINVAL;
1624
1625 case RTC_UIE_OFF: /* disable ints from RTC updates. */
1626 return 0;
1627
1628 case RTC_UIE_ON: /* enable ints for RTC updates. */
1629 return -EINVAL;
1630
1631 case RTC_RD_TIME: /* Read the time/date from RTC */
1632 /* this doesn't get week-day, who cares */
1633 memset(&wtime, 0, sizeof(wtime));
1634 mini_get_rtc_time(&wtime);
1635
1636 return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0;
1637
1638 case RTC_SET_TIME: /* Set the RTC */
1639 {
Tony Breeds644923d2007-03-28 19:10:12 -07001640 int year, days;
David S. Miller8ba706a2006-03-01 17:32:46 -08001641
1642 if (!capable(CAP_SYS_TIME))
1643 return -EACCES;
1644
1645 if (copy_from_user(&wtime, argp, sizeof(wtime)))
1646 return -EFAULT;
1647
1648 year = wtime.tm_year + 1900;
Tony Breeds644923d2007-03-28 19:10:12 -07001649 days = month_days[wtime.tm_mon] +
1650 ((wtime.tm_mon == 1) && leapyear(year));
David S. Miller8ba706a2006-03-01 17:32:46 -08001651
Tony Breeds644923d2007-03-28 19:10:12 -07001652 if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) ||
1653 (wtime.tm_mday < 1))
David S. Miller8ba706a2006-03-01 17:32:46 -08001654 return -EINVAL;
1655
Tony Breeds644923d2007-03-28 19:10:12 -07001656 if (wtime.tm_mday < 0 || wtime.tm_mday > days)
David S. Miller8ba706a2006-03-01 17:32:46 -08001657 return -EINVAL;
1658
1659 if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
1660 wtime.tm_min < 0 || wtime.tm_min >= 60 ||
1661 wtime.tm_sec < 0 || wtime.tm_sec >= 60)
1662 return -EINVAL;
1663
1664 return mini_set_rtc_time(&wtime);
1665 }
1666 }
1667
1668 return -EINVAL;
1669}
1670
1671static int mini_rtc_open(struct inode *inode, struct file *file)
1672{
Arnd Bergmann09de3612008-05-20 19:16:50 +02001673 lock_kernel();
1674 if (mini_rtc_status & RTC_IS_OPEN) {
1675 unlock_kernel();
David S. Miller8ba706a2006-03-01 17:32:46 -08001676 return -EBUSY;
Arnd Bergmann09de3612008-05-20 19:16:50 +02001677 }
David S. Miller8ba706a2006-03-01 17:32:46 -08001678
1679 mini_rtc_status |= RTC_IS_OPEN;
Arnd Bergmann09de3612008-05-20 19:16:50 +02001680 unlock_kernel();
David S. Miller8ba706a2006-03-01 17:32:46 -08001681
1682 return 0;
1683}
1684
1685static int mini_rtc_release(struct inode *inode, struct file *file)
1686{
1687 mini_rtc_status &= ~RTC_IS_OPEN;
1688 return 0;
1689}
1690
1691
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001692static const struct file_operations mini_rtc_fops = {
David S. Miller8ba706a2006-03-01 17:32:46 -08001693 .owner = THIS_MODULE,
1694 .ioctl = mini_rtc_ioctl,
1695 .open = mini_rtc_open,
1696 .release = mini_rtc_release,
1697};
1698
1699static struct miscdevice rtc_mini_dev =
1700{
1701 .minor = RTC_MINOR,
1702 .name = "rtc",
1703 .fops = &mini_rtc_fops,
1704};
1705
1706static int __init rtc_mini_init(void)
1707{
1708 int retval;
1709
David S. Millerd037e052007-05-11 21:18:50 -07001710 if (tlb_type == hypervisor)
1711 mini_rtc_ops = &hypervisor_rtc_ops;
1712 else if (this_is_starfire)
1713 mini_rtc_ops = &starfire_rtc_ops;
Horst H. von Brand71898592007-05-26 17:47:53 -07001714#ifdef CONFIG_PCI
David S. Millerd037e052007-05-11 21:18:50 -07001715 else if (bq4802_regs)
1716 mini_rtc_ops = &bq4802_rtc_ops;
David S. Millercdee99d2007-07-19 13:59:58 -07001717 else if (ds1287_regs)
1718 mini_rtc_ops = &cmos_rtc_ops;
Horst H. von Brand71898592007-05-26 17:47:53 -07001719#endif /* CONFIG_PCI */
David S. Millera0afaa62007-07-27 14:40:27 -07001720 else if (mstk48t02_regs)
1721 mini_rtc_ops = &mostek_rtc_ops;
David S. Millerd037e052007-05-11 21:18:50 -07001722 else
David S. Miller8ba706a2006-03-01 17:32:46 -08001723 return -ENODEV;
1724
1725 printk(KERN_INFO "Mini RTC Driver\n");
1726
1727 retval = misc_register(&rtc_mini_dev);
1728 if (retval < 0)
1729 return retval;
1730
1731 return 0;
1732}
1733
1734static void __exit rtc_mini_exit(void)
1735{
1736 misc_deregister(&rtc_mini_dev);
1737}
1738
Andrew Morton941e4922008-02-06 01:36:42 -08001739int __devinit read_current_timer(unsigned long *timer_val)
1740{
1741 *timer_val = tick_ops->get_tick();
1742 return 0;
1743}
David S. Miller8ba706a2006-03-01 17:32:46 -08001744
1745module_init(rtc_mini_init);
1746module_exit(rtc_mini_exit);