blob: cc16fdcf98af1a84c81126525a974ce81bbffc74 [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{
Thomas Gleixner82644452007-07-21 04:37:37 -0700408 return set_rtc_mmss(now.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
412static void __init kick_start_clock(void)
413{
Al Viroef0299b2005-04-24 12:28:36 -0700414 void __iomem *regs = mstk48t02_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 u8 sec, tmp;
416 int i, count;
417
418 prom_printf("CLOCK: Clock was stopped. Kick start ");
419
420 spin_lock_irq(&mostek_lock);
421
422 /* Turn on the kick start bit to start the oscillator. */
423 tmp = mostek_read(regs + MOSTEK_CREG);
424 tmp |= MSTK_CREG_WRITE;
425 mostek_write(regs + MOSTEK_CREG, tmp);
426 tmp = mostek_read(regs + MOSTEK_SEC);
427 tmp &= ~MSTK_STOP;
428 mostek_write(regs + MOSTEK_SEC, tmp);
429 tmp = mostek_read(regs + MOSTEK_HOUR);
430 tmp |= MSTK_KICK_START;
431 mostek_write(regs + MOSTEK_HOUR, tmp);
432 tmp = mostek_read(regs + MOSTEK_CREG);
433 tmp &= ~MSTK_CREG_WRITE;
434 mostek_write(regs + MOSTEK_CREG, tmp);
435
436 spin_unlock_irq(&mostek_lock);
437
438 /* Delay to allow the clock oscillator to start. */
439 sec = MSTK_REG_SEC(regs);
440 for (i = 0; i < 3; i++) {
441 while (sec == MSTK_REG_SEC(regs))
442 for (count = 0; count < 100000; count++)
443 /* nothing */ ;
444 prom_printf(".");
445 sec = MSTK_REG_SEC(regs);
446 }
447 prom_printf("\n");
448
449 spin_lock_irq(&mostek_lock);
450
451 /* Turn off kick start and set a "valid" time and date. */
452 tmp = mostek_read(regs + MOSTEK_CREG);
453 tmp |= MSTK_CREG_WRITE;
454 mostek_write(regs + MOSTEK_CREG, tmp);
455 tmp = mostek_read(regs + MOSTEK_HOUR);
456 tmp &= ~MSTK_KICK_START;
457 mostek_write(regs + MOSTEK_HOUR, tmp);
458 MSTK_SET_REG_SEC(regs,0);
459 MSTK_SET_REG_MIN(regs,0);
460 MSTK_SET_REG_HOUR(regs,0);
461 MSTK_SET_REG_DOW(regs,5);
462 MSTK_SET_REG_DOM(regs,1);
463 MSTK_SET_REG_MONTH(regs,8);
464 MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
465 tmp = mostek_read(regs + MOSTEK_CREG);
466 tmp &= ~MSTK_CREG_WRITE;
467 mostek_write(regs + MOSTEK_CREG, tmp);
468
469 spin_unlock_irq(&mostek_lock);
470
471 /* Ensure the kick start bit is off. If it isn't, turn it off. */
472 while (mostek_read(regs + MOSTEK_HOUR) & MSTK_KICK_START) {
473 prom_printf("CLOCK: Kick start still on!\n");
474
475 spin_lock_irq(&mostek_lock);
476
477 tmp = mostek_read(regs + MOSTEK_CREG);
478 tmp |= MSTK_CREG_WRITE;
479 mostek_write(regs + MOSTEK_CREG, tmp);
480
481 tmp = mostek_read(regs + MOSTEK_HOUR);
482 tmp &= ~MSTK_KICK_START;
483 mostek_write(regs + MOSTEK_HOUR, tmp);
484
485 tmp = mostek_read(regs + MOSTEK_CREG);
486 tmp &= ~MSTK_CREG_WRITE;
487 mostek_write(regs + MOSTEK_CREG, tmp);
488
489 spin_unlock_irq(&mostek_lock);
490 }
491
492 prom_printf("CLOCK: Kick start procedure successful.\n");
493}
494
495/* Return nonzero if the clock chip battery is low. */
496static int __init has_low_battery(void)
497{
Al Viroef0299b2005-04-24 12:28:36 -0700498 void __iomem *regs = mstk48t02_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 u8 data1, data2;
500
501 spin_lock_irq(&mostek_lock);
502
503 data1 = mostek_read(regs + MOSTEK_EEPROM); /* Read some data. */
504 mostek_write(regs + MOSTEK_EEPROM, ~data1); /* Write back the complement. */
505 data2 = mostek_read(regs + MOSTEK_EEPROM); /* Read back the complement. */
506 mostek_write(regs + MOSTEK_EEPROM, data1); /* Restore original value. */
507
508 spin_unlock_irq(&mostek_lock);
509
510 return (data1 == data2); /* Was the write blocked? */
511}
512
David S. Millercf3d7c12008-03-26 01:11:55 -0700513static void __init mostek_set_system_time(void __iomem *mregs)
514{
515 unsigned int year, mon, day, hour, min, sec;
516 u8 tmp;
517
518 spin_lock_irq(&mostek_lock);
519
520 /* Traditional Mostek chip. */
521 tmp = mostek_read(mregs + MOSTEK_CREG);
522 tmp |= MSTK_CREG_READ;
523 mostek_write(mregs + MOSTEK_CREG, tmp);
524
525 sec = MSTK_REG_SEC(mregs);
526 min = MSTK_REG_MIN(mregs);
527 hour = MSTK_REG_HOUR(mregs);
528 day = MSTK_REG_DOM(mregs);
529 mon = MSTK_REG_MONTH(mregs);
530 year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
531
532 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
533 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
534 set_normalized_timespec(&wall_to_monotonic,
535 -xtime.tv_sec, -xtime.tv_nsec);
536
537 tmp = mostek_read(mregs + MOSTEK_CREG);
538 tmp &= ~MSTK_CREG_READ;
539 mostek_write(mregs + MOSTEK_CREG, tmp);
540
541 spin_unlock_irq(&mostek_lock);
542}
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544/* Probe for the real time clock chip. */
545static void __init set_system_time(void)
546{
547 unsigned int year, mon, day, hour, min, sec;
Al Viroef0299b2005-04-24 12:28:36 -0700548 void __iomem *mregs = mstk48t02_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549#ifdef CONFIG_PCI
550 unsigned long dregs = ds1287_regs;
David S. Millerd037e052007-05-11 21:18:50 -0700551 void __iomem *bregs = bq4802_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552#else
553 unsigned long dregs = 0UL;
David S. Millerd037e052007-05-11 21:18:50 -0700554 void __iomem *bregs = 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
David S. Millerd037e052007-05-11 21:18:50 -0700557 if (!mregs && !dregs && !bregs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 prom_printf("Something wrong, clock regs not mapped yet.\n");
559 prom_halt();
560 }
561
562 if (mregs) {
David S. Millercf3d7c12008-03-26 01:11:55 -0700563 mostek_set_system_time(mregs);
564 return;
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
David S. Millercf3d7c12008-03-26 01:11:55 -0700567 if (bregs) {
David S. Millerd037e052007-05-11 21:18:50 -0700568 unsigned char val = readb(bregs + 0x0e);
569 unsigned int century;
570
571 /* BQ4802 RTC chip. */
572
573 writeb(val | 0x08, bregs + 0x0e);
574
575 sec = readb(bregs + 0x00);
576 min = readb(bregs + 0x02);
577 hour = readb(bregs + 0x04);
578 day = readb(bregs + 0x06);
579 mon = readb(bregs + 0x09);
580 year = readb(bregs + 0x0a);
581 century = readb(bregs + 0x0f);
582
583 writeb(val, bregs + 0x0e);
584
585 BCD_TO_BIN(sec);
586 BCD_TO_BIN(min);
587 BCD_TO_BIN(hour);
588 BCD_TO_BIN(day);
589 BCD_TO_BIN(mon);
590 BCD_TO_BIN(year);
591 BCD_TO_BIN(century);
592
593 year += (century * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /* Dallas 12887 RTC chip. */
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 do {
598 sec = CMOS_READ(RTC_SECONDS);
599 min = CMOS_READ(RTC_MINUTES);
600 hour = CMOS_READ(RTC_HOURS);
601 day = CMOS_READ(RTC_DAY_OF_MONTH);
602 mon = CMOS_READ(RTC_MONTH);
603 year = CMOS_READ(RTC_YEAR);
604 } while (sec != CMOS_READ(RTC_SECONDS));
Matt Mackall3dedf532006-03-28 01:56:01 -0800605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
607 BCD_TO_BIN(sec);
608 BCD_TO_BIN(min);
609 BCD_TO_BIN(hour);
610 BCD_TO_BIN(day);
611 BCD_TO_BIN(mon);
612 BCD_TO_BIN(year);
613 }
614 if ((year += 1900) < 1970)
615 year += 100;
616 }
617
618 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
619 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
620 set_normalized_timespec(&wall_to_monotonic,
621 -xtime.tv_sec, -xtime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
David S. Miller4bdff412006-02-11 01:01:55 -0800624/* davem suggests we keep this within the 4M locked kernel image */
625static u32 starfire_get_time(void)
626{
627 static char obp_gettod[32];
628 static u32 unix_tod;
629
630 sprintf(obp_gettod, "h# %08x unix-gettod",
631 (unsigned int) (long) &unix_tod);
632 prom_feval(obp_gettod);
633
634 return unix_tod;
635}
636
David S. Miller8ba706a2006-03-01 17:32:46 -0800637static int starfire_set_time(u32 val)
638{
639 /* Do nothing, time is set using the service processor
640 * console on this platform.
641 */
642 return 0;
643}
644
David S. Miller4bdff412006-02-11 01:01:55 -0800645static u32 hypervisor_get_time(void)
646{
David S. Miller7db35f32007-05-29 02:22:14 -0700647 unsigned long ret, time;
David S. Miller4bdff412006-02-11 01:01:55 -0800648 int retries = 10000;
649
650retry:
David S. Miller7db35f32007-05-29 02:22:14 -0700651 ret = sun4v_tod_get(&time);
652 if (ret == HV_EOK)
653 return time;
654 if (ret == HV_EWOULDBLOCK) {
David S. Miller4bdff412006-02-11 01:01:55 -0800655 if (--retries > 0) {
656 udelay(100);
657 goto retry;
658 }
659 printk(KERN_WARNING "SUN4V: tod_get() timed out.\n");
660 return 0;
661 }
662 printk(KERN_WARNING "SUN4V: tod_get() not supported.\n");
663 return 0;
664}
665
David S. Miller8ba706a2006-03-01 17:32:46 -0800666static int hypervisor_set_time(u32 secs)
667{
David S. Miller7db35f32007-05-29 02:22:14 -0700668 unsigned long ret;
David S. Miller8ba706a2006-03-01 17:32:46 -0800669 int retries = 10000;
670
671retry:
David S. Miller7db35f32007-05-29 02:22:14 -0700672 ret = sun4v_tod_set(secs);
673 if (ret == HV_EOK)
David S. Miller8ba706a2006-03-01 17:32:46 -0800674 return 0;
David S. Miller7db35f32007-05-29 02:22:14 -0700675 if (ret == HV_EWOULDBLOCK) {
David S. Miller8ba706a2006-03-01 17:32:46 -0800676 if (--retries > 0) {
677 udelay(100);
678 goto retry;
679 }
680 printk(KERN_WARNING "SUN4V: tod_set() timed out.\n");
681 return -EAGAIN;
682 }
683 printk(KERN_WARNING "SUN4V: tod_set() not supported.\n");
684 return -EOPNOTSUPP;
685}
686
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700687static int __init clock_model_matches(const char *model)
David S. Miller690c8fd2006-06-22 19:12:03 -0700688{
689 if (strcmp(model, "mk48t02") &&
690 strcmp(model, "mk48t08") &&
691 strcmp(model, "mk48t59") &&
692 strcmp(model, "m5819") &&
693 strcmp(model, "m5819p") &&
694 strcmp(model, "m5823") &&
David S. Millerd037e052007-05-11 21:18:50 -0700695 strcmp(model, "ds1287") &&
696 strcmp(model, "bq4802"))
David S. Miller690c8fd2006-06-22 19:12:03 -0700697 return 0;
698
699 return 1;
700}
701
David S. Milleree5caf02006-06-29 14:36:52 -0700702static int __devinit clock_probe(struct of_device *op, const struct of_device_id *match)
David S. Miller690c8fd2006-06-22 19:12:03 -0700703{
David S. Milleree5caf02006-06-29 14:36:52 -0700704 struct device_node *dp = op->node;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700705 const char *model = of_get_property(dp, "model", NULL);
David S. Millerd037e052007-05-11 21:18:50 -0700706 const char *compat = of_get_property(dp, "compatible", NULL);
David S. Milleree5caf02006-06-29 14:36:52 -0700707 unsigned long size, flags;
708 void __iomem *regs;
David S. Miller690c8fd2006-06-22 19:12:03 -0700709
David S. Millerd037e052007-05-11 21:18:50 -0700710 if (!model)
711 model = compat;
712
David S. Milleree5caf02006-06-29 14:36:52 -0700713 if (!model || !clock_model_matches(model))
714 return -ENODEV;
David S. Miller690c8fd2006-06-22 19:12:03 -0700715
David S. Miller91521482006-06-29 14:39:40 -0700716 /* On an Enterprise system there can be multiple mostek clocks.
717 * We should only match the one that is on the central FHC bus.
718 */
David S. Millerc3a8b852006-06-29 14:43:37 -0700719 if (!strcmp(dp->parent->name, "fhc") &&
720 strcmp(dp->parent->parent->name, "central") != 0)
David S. Miller91521482006-06-29 14:39:40 -0700721 return -ENODEV;
722
David S. Milleree5caf02006-06-29 14:36:52 -0700723 size = (op->resource[0].end - op->resource[0].start) + 1;
724 regs = of_ioremap(&op->resource[0], 0, size, "clock");
725 if (!regs)
726 return -ENOMEM;
David S. Miller690c8fd2006-06-22 19:12:03 -0700727
Randy Dunlap72335892006-07-05 20:18:39 -0700728#ifdef CONFIG_PCI
David S. Miller690c8fd2006-06-22 19:12:03 -0700729 if (!strcmp(model, "ds1287") ||
730 !strcmp(model, "m5819") ||
731 !strcmp(model, "m5819p") ||
732 !strcmp(model, "m5823")) {
David S. Milleree5caf02006-06-29 14:36:52 -0700733 ds1287_regs = (unsigned long) regs;
David S. Millerd037e052007-05-11 21:18:50 -0700734 } else if (!strcmp(model, "bq4802")) {
735 bq4802_regs = regs;
Randy Dunlap72335892006-07-05 20:18:39 -0700736 } else
737#endif
738 if (model[5] == '0' && model[6] == '2') {
David S. Milleree5caf02006-06-29 14:36:52 -0700739 mstk48t02_regs = regs;
740 } else if(model[5] == '0' && model[6] == '8') {
741 mstk48t08_regs = regs;
742 mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02;
David S. Miller690c8fd2006-06-22 19:12:03 -0700743 } else {
David S. Milleree5caf02006-06-29 14:36:52 -0700744 mstk48t59_regs = regs;
David S. Miller690c8fd2006-06-22 19:12:03 -0700745 mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
746 }
David S. Miller690c8fd2006-06-22 19:12:03 -0700747
David S. Milleree5caf02006-06-29 14:36:52 -0700748 printk(KERN_INFO "%s: Clock regs at %p\n", dp->full_name, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
David S. Miller690c8fd2006-06-22 19:12:03 -0700750 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
David S. Millerb4bca262005-04-21 21:42:34 -0700752 if (mstk48t02_regs != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 /* Report a low battery voltage condition. */
754 if (has_low_battery())
755 prom_printf("NVRAM: Low battery voltage!\n");
756
757 /* Kick start the clock if it is completely stopped. */
758 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
759 kick_start_clock();
760 }
761
762 set_system_time();
763
764 local_irq_restore(flags);
David S. Milleree5caf02006-06-29 14:36:52 -0700765
766 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
David S. Milleree5caf02006-06-29 14:36:52 -0700769static struct of_device_id clock_match[] = {
770 {
771 .name = "eeprom",
772 },
773 {
774 .name = "rtc",
775 },
776 {},
777};
778
779static struct of_platform_driver clock_driver = {
David S. Milleree5caf02006-06-29 14:36:52 -0700780 .match_table = clock_match,
781 .probe = clock_probe,
Stephen Rothwella2cd1552007-10-10 23:27:34 -0700782 .driver = {
783 .name = "clock",
784 },
David S. Milleree5caf02006-06-29 14:36:52 -0700785};
786
787static int __init clock_init(void)
788{
789 if (this_is_starfire) {
790 xtime.tv_sec = starfire_get_time();
791 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
792 set_normalized_timespec(&wall_to_monotonic,
793 -xtime.tv_sec, -xtime.tv_nsec);
794 return 0;
795 }
796 if (tlb_type == hypervisor) {
797 xtime.tv_sec = hypervisor_get_time();
798 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
799 set_normalized_timespec(&wall_to_monotonic,
800 -xtime.tv_sec, -xtime.tv_nsec);
801 return 0;
802 }
803
Stephen Rothwell37b77542007-04-30 17:43:56 +1000804 return of_register_driver(&clock_driver, &of_platform_bus_type);
David S. Milleree5caf02006-06-29 14:36:52 -0700805}
806
807/* Must be after subsys_initcall() so that busses are probed. Must
808 * be before device_initcall() because things like the RTC driver
809 * need to see the clock registers.
810 */
811fs_initcall(clock_init);
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813/* This is gets the master TICK_INT timer going. */
814static unsigned long sparc64_init_timers(void)
815{
David S. Miller07f8e5f2006-06-21 23:34:02 -0700816 struct device_node *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 unsigned long clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
David S. Miller07f8e5f2006-06-21 23:34:02 -0700819 dp = of_find_node_by_path("/");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (tlb_type == spitfire) {
821 unsigned long ver, manuf, impl;
822
823 __asm__ __volatile__ ("rdpr %%ver, %0"
824 : "=&r" (ver));
825 manuf = ((ver >> 48) & 0xffff);
826 impl = ((ver >> 32) & 0xffff);
827 if (manuf == 0x17 && impl == 0x13) {
828 /* Hummingbird, aka Ultra-IIe */
829 tick_ops = &hbtick_operations;
David S. Miller5cbc3072007-05-25 15:49:59 -0700830 clock = of_getintprop_default(dp, "stick-frequency", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 } else {
832 tick_ops = &tick_operations;
David S. Miller5cbc3072007-05-25 15:49:59 -0700833 clock = local_cpu_data().clock_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835 } else {
836 tick_ops = &stick_operations;
David S. Miller5cbc3072007-05-25 15:49:59 -0700837 clock = of_getintprop_default(dp, "stick-frequency", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return clock;
841}
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843struct freq_table {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 unsigned long clock_tick_ref;
845 unsigned int ref_freq;
846};
David S. Miller3763be32006-02-17 12:33:13 -0800847static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849unsigned long sparc64_get_clock_tick(unsigned int cpu)
850{
851 struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
852
853 if (ft->clock_tick_ref)
854 return ft->clock_tick_ref;
855 return cpu_data(cpu).clock_tick;
856}
857
858#ifdef CONFIG_CPU_FREQ
859
860static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
861 void *data)
862{
863 struct cpufreq_freqs *freq = data;
864 unsigned int cpu = freq->cpu;
865 struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
866
867 if (!ft->ref_freq) {
868 ft->ref_freq = freq->old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 ft->clock_tick_ref = cpu_data(cpu).clock_tick;
870 }
871 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
872 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
873 (val == CPUFREQ_RESUMECHANGE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 cpu_data(cpu).clock_tick =
875 cpufreq_scale(ft->clock_tick_ref,
876 ft->ref_freq,
877 freq->new);
878 }
879
880 return 0;
881}
882
883static struct notifier_block sparc64_cpufreq_notifier_block = {
884 .notifier_call = sparc64_cpufreq_notifier
885};
886
David S. Miller7ae93f52008-07-23 16:21:07 -0700887static int __init register_sparc64_cpufreq_notifier(void)
888{
889
890 cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
891 CPUFREQ_TRANSITION_NOTIFIER);
892 return 0;
893}
894
895core_initcall(register_sparc64_cpufreq_notifier);
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897#endif /* CONFIG_CPU_FREQ */
898
David S. Miller112f4872007-03-05 15:28:37 -0800899static int sparc64_next_event(unsigned long delta,
900 struct clock_event_device *evt)
901{
David S. Millerd62c6f02007-03-27 01:20:14 -0700902 return tick_ops->add_compare(delta) ? -ETIME : 0;
David S. Miller112f4872007-03-05 15:28:37 -0800903}
904
905static void sparc64_timer_setup(enum clock_event_mode mode,
906 struct clock_event_device *evt)
907{
908 switch (mode) {
909 case CLOCK_EVT_MODE_ONESHOT:
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700910 case CLOCK_EVT_MODE_RESUME:
David S. Miller112f4872007-03-05 15:28:37 -0800911 break;
912
913 case CLOCK_EVT_MODE_SHUTDOWN:
914 tick_ops->disable_irq();
915 break;
916
917 case CLOCK_EVT_MODE_PERIODIC:
918 case CLOCK_EVT_MODE_UNUSED:
919 WARN_ON(1);
920 break;
921 };
922}
923
924static struct clock_event_device sparc64_clockevent = {
925 .features = CLOCK_EVT_FEAT_ONESHOT,
926 .set_mode = sparc64_timer_setup,
927 .set_next_event = sparc64_next_event,
928 .rating = 100,
929 .shift = 30,
930 .irq = -1,
931};
932static DEFINE_PER_CPU(struct clock_event_device, sparc64_events);
933
934void timer_interrupt(int irq, struct pt_regs *regs)
935{
936 struct pt_regs *old_regs = set_irq_regs(regs);
937 unsigned long tick_mask = tick_ops->softint_mask;
938 int cpu = smp_processor_id();
939 struct clock_event_device *evt = &per_cpu(sparc64_events, cpu);
940
941 clear_softint(tick_mask);
942
943 irq_enter();
944
945 kstat_this_cpu.irqs[0]++;
946
947 if (unlikely(!evt->event_handler)) {
948 printk(KERN_WARNING
949 "Spurious SPARC64 timer interrupt on cpu %d\n", cpu);
950 } else
951 evt->event_handler(evt);
952
953 irq_exit();
954
955 set_irq_regs(old_regs);
956}
957
958void __devinit setup_sparc64_timer(void)
959{
960 struct clock_event_device *sevt;
961 unsigned long pstate;
962
963 /* Guarantee that the following sequences execute
964 * uninterrupted.
965 */
966 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
967 "wrpr %0, %1, %%pstate"
968 : "=r" (pstate)
969 : "i" (PSTATE_IE));
970
971 tick_ops->init_tick();
972
973 /* Restore PSTATE_IE. */
974 __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
975 : /* no outputs */
976 : "r" (pstate));
977
978 sevt = &__get_cpu_var(sparc64_events);
979
980 memcpy(sevt, &sparc64_clockevent, sizeof(*sevt));
981 sevt->cpumask = cpumask_of_cpu(smp_processor_id());
982
983 clockevents_register_device(sevt);
984}
985
David S. Miller03983ab2007-05-17 22:55:26 -0700986#define SPARC64_NSEC_PER_CYC_SHIFT 10UL
David S. Miller112f4872007-03-05 15:28:37 -0800987
988static struct clocksource clocksource_tick = {
989 .rating = 100,
990 .mask = CLOCKSOURCE_MASK(64),
991 .shift = 16,
992 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993};
994
David S. Miller112f4872007-03-05 15:28:37 -0800995static void __init setup_clockevent_multiplier(unsigned long hz)
996{
997 unsigned long mult, shift = 32;
998
999 while (1) {
1000 mult = div_sc(hz, NSEC_PER_SEC, shift);
1001 if (mult && (mult >> 32UL) == 0UL)
1002 break;
1003
1004 shift--;
1005 }
1006
1007 sparc64_clockevent.shift = shift;
1008 sparc64_clockevent.mult = mult;
1009}
1010
David S. Miller8b99cfb2007-07-14 02:23:37 -07001011static unsigned long tb_ticks_per_usec __read_mostly;
1012
1013void __delay(unsigned long loops)
1014{
1015 unsigned long bclock, now;
1016
1017 bclock = tick_ops->get_tick();
1018 do {
1019 now = tick_ops->get_tick();
1020 } while ((now-bclock) < loops);
1021}
1022EXPORT_SYMBOL(__delay);
1023
1024void udelay(unsigned long usecs)
1025{
1026 __delay(tb_ticks_per_usec * usecs);
1027}
1028EXPORT_SYMBOL(udelay);
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030void __init time_init(void)
1031{
1032 unsigned long clock = sparc64_init_timers();
1033
David S. Miller8b99cfb2007-07-14 02:23:37 -07001034 tb_ticks_per_usec = clock / USEC_PER_SEC;
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 timer_ticks_per_nsec_quotient =
David S. Miller112f4872007-03-05 15:28:37 -08001037 clocksource_hz2mult(clock, SPARC64_NSEC_PER_CYC_SHIFT);
1038
1039 clocksource_tick.name = tick_ops->name;
1040 clocksource_tick.mult =
1041 clocksource_hz2mult(clock,
1042 clocksource_tick.shift);
1043 clocksource_tick.read = tick_ops->get_tick;
1044
1045 printk("clocksource: mult[%x] shift[%d]\n",
1046 clocksource_tick.mult, clocksource_tick.shift);
1047
1048 clocksource_register(&clocksource_tick);
1049
1050 sparc64_clockevent.name = tick_ops->name;
1051
1052 setup_clockevent_multiplier(clock);
1053
1054 sparc64_clockevent.max_delta_ns =
David S. Millercf3d7c12008-03-26 01:11:55 -07001055 clockevent_delta2ns(0x7fffffffffffffffUL, &sparc64_clockevent);
David S. Miller112f4872007-03-05 15:28:37 -08001056 sparc64_clockevent.min_delta_ns =
1057 clockevent_delta2ns(0xF, &sparc64_clockevent);
1058
1059 printk("clockevent: mult[%lx] shift[%d]\n",
1060 sparc64_clockevent.mult, sparc64_clockevent.shift);
1061
1062 setup_sparc64_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
1065unsigned long long sched_clock(void)
1066{
1067 unsigned long ticks = tick_ops->get_tick();
1068
1069 return (ticks * timer_ticks_per_nsec_quotient)
1070 >> SPARC64_NSEC_PER_CYC_SHIFT;
1071}
1072
1073static int set_rtc_mmss(unsigned long nowtime)
1074{
1075 int real_seconds, real_minutes, chip_minutes;
Al Viroef0299b2005-04-24 12:28:36 -07001076 void __iomem *mregs = mstk48t02_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077#ifdef CONFIG_PCI
1078 unsigned long dregs = ds1287_regs;
David S. Millerd037e052007-05-11 21:18:50 -07001079 void __iomem *bregs = bq4802_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080#else
1081 unsigned long dregs = 0UL;
David S. Millerd037e052007-05-11 21:18:50 -07001082 void __iomem *bregs = 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083#endif
1084 unsigned long flags;
1085 u8 tmp;
1086
1087 /*
1088 * Not having a register set can lead to trouble.
1089 * Also starfire doesn't have a tod clock.
1090 */
David S. Miller23e8bc22007-10-27 22:33:33 -07001091 if (!mregs && !dregs && !bregs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return -1;
1093
1094 if (mregs) {
1095 spin_lock_irqsave(&mostek_lock, flags);
1096
1097 /* Read the current RTC minutes. */
1098 tmp = mostek_read(mregs + MOSTEK_CREG);
1099 tmp |= MSTK_CREG_READ;
1100 mostek_write(mregs + MOSTEK_CREG, tmp);
1101
1102 chip_minutes = MSTK_REG_MIN(mregs);
1103
1104 tmp = mostek_read(mregs + MOSTEK_CREG);
1105 tmp &= ~MSTK_CREG_READ;
1106 mostek_write(mregs + MOSTEK_CREG, tmp);
1107
1108 /*
1109 * since we're only adjusting minutes and seconds,
1110 * don't interfere with hour overflow. This avoids
1111 * messing with unknown time zones but requires your
1112 * RTC not to be off by more than 15 minutes
1113 */
1114 real_seconds = nowtime % 60;
1115 real_minutes = nowtime / 60;
1116 if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
1117 real_minutes += 30; /* correct for half hour time zone */
1118 real_minutes %= 60;
1119
1120 if (abs(real_minutes - chip_minutes) < 30) {
1121 tmp = mostek_read(mregs + MOSTEK_CREG);
1122 tmp |= MSTK_CREG_WRITE;
1123 mostek_write(mregs + MOSTEK_CREG, tmp);
1124
1125 MSTK_SET_REG_SEC(mregs,real_seconds);
1126 MSTK_SET_REG_MIN(mregs,real_minutes);
1127
1128 tmp = mostek_read(mregs + MOSTEK_CREG);
1129 tmp &= ~MSTK_CREG_WRITE;
1130 mostek_write(mregs + MOSTEK_CREG, tmp);
1131
1132 spin_unlock_irqrestore(&mostek_lock, flags);
1133
1134 return 0;
1135 } else {
1136 spin_unlock_irqrestore(&mostek_lock, flags);
1137
1138 return -1;
1139 }
David S. Millerd037e052007-05-11 21:18:50 -07001140 } else if (bregs) {
1141 int retval = 0;
1142 unsigned char val = readb(bregs + 0x0e);
1143
1144 /* BQ4802 RTC chip. */
1145
1146 writeb(val | 0x08, bregs + 0x0e);
1147
1148 chip_minutes = readb(bregs + 0x02);
1149 BCD_TO_BIN(chip_minutes);
1150 real_seconds = nowtime % 60;
1151 real_minutes = nowtime / 60;
1152 if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
1153 real_minutes += 30;
1154 real_minutes %= 60;
1155
1156 if (abs(real_minutes - chip_minutes) < 30) {
1157 BIN_TO_BCD(real_seconds);
1158 BIN_TO_BCD(real_minutes);
1159 writeb(real_seconds, bregs + 0x00);
1160 writeb(real_minutes, bregs + 0x02);
1161 } else {
1162 printk(KERN_WARNING
1163 "set_rtc_mmss: can't update from %d to %d\n",
1164 chip_minutes, real_minutes);
1165 retval = -1;
1166 }
1167
1168 writeb(val, bregs + 0x0e);
1169
1170 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 } else {
1172 int retval = 0;
1173 unsigned char save_control, save_freq_select;
1174
1175 /* Stolen from arch/i386/kernel/time.c, see there for
1176 * credits and descriptive comments.
1177 */
1178 spin_lock_irqsave(&rtc_lock, flags);
1179 save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
1180 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
1181
1182 save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
1183 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
1184
1185 chip_minutes = CMOS_READ(RTC_MINUTES);
1186 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1187 BCD_TO_BIN(chip_minutes);
1188 real_seconds = nowtime % 60;
1189 real_minutes = nowtime / 60;
1190 if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
1191 real_minutes += 30;
1192 real_minutes %= 60;
1193
1194 if (abs(real_minutes - chip_minutes) < 30) {
1195 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
1196 BIN_TO_BCD(real_seconds);
1197 BIN_TO_BCD(real_minutes);
1198 }
1199 CMOS_WRITE(real_seconds,RTC_SECONDS);
1200 CMOS_WRITE(real_minutes,RTC_MINUTES);
1201 } else {
1202 printk(KERN_WARNING
1203 "set_rtc_mmss: can't update from %d to %d\n",
1204 chip_minutes, real_minutes);
1205 retval = -1;
1206 }
1207
1208 CMOS_WRITE(save_control, RTC_CONTROL);
1209 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1210 spin_unlock_irqrestore(&rtc_lock, flags);
1211
1212 return retval;
1213 }
1214}
David S. Miller8ba706a2006-03-01 17:32:46 -08001215
1216#define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
1217static unsigned char mini_rtc_status; /* bitmapped status byte. */
1218
David S. Miller8ba706a2006-03-01 17:32:46 -08001219#define FEBRUARY 2
1220#define STARTOFTIME 1970
1221#define SECDAY 86400L
1222#define SECYR (SECDAY * 365)
1223#define leapyear(year) ((year) % 4 == 0 && \
1224 ((year) % 100 != 0 || (year) % 400 == 0))
1225#define days_in_year(a) (leapyear(a) ? 366 : 365)
1226#define days_in_month(a) (month_days[(a) - 1])
1227
1228static int month_days[12] = {
1229 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
1230};
1231
1232/*
1233 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
1234 */
1235static void GregorianDay(struct rtc_time * tm)
1236{
1237 int leapsToDate;
1238 int lastYear;
1239 int day;
1240 int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
1241
1242 lastYear = tm->tm_year - 1;
1243
1244 /*
1245 * Number of leap corrections to apply up to end of last year
1246 */
1247 leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
1248
1249 /*
1250 * This year is a leap year if it is divisible by 4 except when it is
1251 * divisible by 100 unless it is divisible by 400
1252 *
1253 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
1254 */
1255 day = tm->tm_mon > 2 && leapyear(tm->tm_year);
1256
1257 day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
1258 tm->tm_mday;
1259
1260 tm->tm_wday = day % 7;
1261}
1262
1263static void to_tm(int tim, struct rtc_time *tm)
1264{
1265 register int i;
1266 register long hms, day;
1267
1268 day = tim / SECDAY;
1269 hms = tim % SECDAY;
1270
1271 /* Hours, minutes, seconds are easy */
1272 tm->tm_hour = hms / 3600;
1273 tm->tm_min = (hms % 3600) / 60;
1274 tm->tm_sec = (hms % 3600) % 60;
1275
1276 /* Number of years in days */
1277 for (i = STARTOFTIME; day >= days_in_year(i); i++)
1278 day -= days_in_year(i);
1279 tm->tm_year = i;
1280
1281 /* Number of months in days left */
1282 if (leapyear(tm->tm_year))
1283 days_in_month(FEBRUARY) = 29;
1284 for (i = 1; day >= days_in_month(i); i++)
1285 day -= days_in_month(i);
1286 days_in_month(FEBRUARY) = 28;
1287 tm->tm_mon = i;
1288
1289 /* Days are what is left over (+1) from all that. */
1290 tm->tm_mday = day + 1;
1291
1292 /*
1293 * Determine the day of week
1294 */
1295 GregorianDay(tm);
1296}
1297
1298/* Both Starfire and SUN4V give us seconds since Jan 1st, 1970,
1299 * aka Unix time. So we have to convert to/from rtc_time.
1300 */
David S. Millerd037e052007-05-11 21:18:50 -07001301static void starfire_get_rtc_time(struct rtc_time *time)
David S. Miller8ba706a2006-03-01 17:32:46 -08001302{
David S. Millerd037e052007-05-11 21:18:50 -07001303 u32 seconds = starfire_get_time();
David S. Miller8ba706a2006-03-01 17:32:46 -08001304
1305 to_tm(seconds, time);
David S. Millerc4f8ef72006-03-02 20:28:34 -08001306 time->tm_year -= 1900;
1307 time->tm_mon -= 1;
David S. Miller8ba706a2006-03-01 17:32:46 -08001308}
1309
David S. Millerd037e052007-05-11 21:18:50 -07001310static int starfire_set_rtc_time(struct rtc_time *time)
David S. Miller8ba706a2006-03-01 17:32:46 -08001311{
1312 u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
1313 time->tm_mday, time->tm_hour,
1314 time->tm_min, time->tm_sec);
David S. Millerd037e052007-05-11 21:18:50 -07001315
1316 return starfire_set_time(seconds);
1317}
1318
1319static void hypervisor_get_rtc_time(struct rtc_time *time)
1320{
1321 u32 seconds = hypervisor_get_time();
1322
1323 to_tm(seconds, time);
1324 time->tm_year -= 1900;
1325 time->tm_mon -= 1;
1326}
1327
1328static int hypervisor_set_rtc_time(struct rtc_time *time)
1329{
1330 u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
1331 time->tm_mday, time->tm_hour,
1332 time->tm_min, time->tm_sec);
1333
1334 return hypervisor_set_time(seconds);
1335}
1336
Horst H. von Brand71898592007-05-26 17:47:53 -07001337#ifdef CONFIG_PCI
David S. Millerd037e052007-05-11 21:18:50 -07001338static void bq4802_get_rtc_time(struct rtc_time *time)
1339{
1340 unsigned char val = readb(bq4802_regs + 0x0e);
1341 unsigned int century;
1342
1343 writeb(val | 0x08, bq4802_regs + 0x0e);
1344
1345 time->tm_sec = readb(bq4802_regs + 0x00);
1346 time->tm_min = readb(bq4802_regs + 0x02);
1347 time->tm_hour = readb(bq4802_regs + 0x04);
1348 time->tm_mday = readb(bq4802_regs + 0x06);
1349 time->tm_mon = readb(bq4802_regs + 0x09);
1350 time->tm_year = readb(bq4802_regs + 0x0a);
1351 time->tm_wday = readb(bq4802_regs + 0x08);
1352 century = readb(bq4802_regs + 0x0f);
1353
1354 writeb(val, bq4802_regs + 0x0e);
1355
1356 BCD_TO_BIN(time->tm_sec);
1357 BCD_TO_BIN(time->tm_min);
1358 BCD_TO_BIN(time->tm_hour);
1359 BCD_TO_BIN(time->tm_mday);
1360 BCD_TO_BIN(time->tm_mon);
1361 BCD_TO_BIN(time->tm_year);
1362 BCD_TO_BIN(time->tm_wday);
1363 BCD_TO_BIN(century);
1364
1365 time->tm_year += (century * 100);
1366 time->tm_year -= 1900;
1367
1368 time->tm_mon--;
1369}
1370
1371static int bq4802_set_rtc_time(struct rtc_time *time)
1372{
1373 unsigned char val = readb(bq4802_regs + 0x0e);
1374 unsigned char sec, min, hrs, day, mon, yrs, century;
1375 unsigned int year;
1376
1377 year = time->tm_year + 1900;
1378 century = year / 100;
1379 yrs = year % 100;
1380
1381 mon = time->tm_mon + 1; /* tm_mon starts at zero */
1382 day = time->tm_mday;
1383 hrs = time->tm_hour;
1384 min = time->tm_min;
1385 sec = time->tm_sec;
1386
1387 BIN_TO_BCD(sec);
1388 BIN_TO_BCD(min);
1389 BIN_TO_BCD(hrs);
1390 BIN_TO_BCD(day);
1391 BIN_TO_BCD(mon);
1392 BIN_TO_BCD(yrs);
1393 BIN_TO_BCD(century);
1394
1395 writeb(val | 0x08, bq4802_regs + 0x0e);
1396
1397 writeb(sec, bq4802_regs + 0x00);
1398 writeb(min, bq4802_regs + 0x02);
1399 writeb(hrs, bq4802_regs + 0x04);
1400 writeb(day, bq4802_regs + 0x06);
1401 writeb(mon, bq4802_regs + 0x09);
1402 writeb(yrs, bq4802_regs + 0x0a);
1403 writeb(century, bq4802_regs + 0x0f);
1404
1405 writeb(val, bq4802_regs + 0x0e);
1406
1407 return 0;
1408}
David S. Millercdee99d2007-07-19 13:59:58 -07001409
1410static void cmos_get_rtc_time(struct rtc_time *rtc_tm)
1411{
1412 unsigned char ctrl;
1413
1414 rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
1415 rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
1416 rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
1417 rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
1418 rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
1419 rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
1420 rtc_tm->tm_wday = CMOS_READ(RTC_DAY_OF_WEEK);
1421
1422 ctrl = CMOS_READ(RTC_CONTROL);
1423 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
1424 BCD_TO_BIN(rtc_tm->tm_sec);
1425 BCD_TO_BIN(rtc_tm->tm_min);
1426 BCD_TO_BIN(rtc_tm->tm_hour);
1427 BCD_TO_BIN(rtc_tm->tm_mday);
1428 BCD_TO_BIN(rtc_tm->tm_mon);
1429 BCD_TO_BIN(rtc_tm->tm_year);
1430 BCD_TO_BIN(rtc_tm->tm_wday);
1431 }
1432
1433 if (rtc_tm->tm_year <= 69)
1434 rtc_tm->tm_year += 100;
1435
1436 rtc_tm->tm_mon--;
1437}
1438
1439static int cmos_set_rtc_time(struct rtc_time *rtc_tm)
1440{
1441 unsigned char mon, day, hrs, min, sec;
1442 unsigned char save_control, save_freq_select;
1443 unsigned int yrs;
1444
1445 yrs = rtc_tm->tm_year;
1446 mon = rtc_tm->tm_mon + 1;
1447 day = rtc_tm->tm_mday;
1448 hrs = rtc_tm->tm_hour;
1449 min = rtc_tm->tm_min;
1450 sec = rtc_tm->tm_sec;
1451
1452 if (yrs >= 100)
1453 yrs -= 100;
1454
1455 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
1456 BIN_TO_BCD(sec);
1457 BIN_TO_BCD(min);
1458 BIN_TO_BCD(hrs);
1459 BIN_TO_BCD(day);
1460 BIN_TO_BCD(mon);
1461 BIN_TO_BCD(yrs);
1462 }
1463
1464 save_control = CMOS_READ(RTC_CONTROL);
1465 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
1466 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1467 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
1468
1469 CMOS_WRITE(yrs, RTC_YEAR);
1470 CMOS_WRITE(mon, RTC_MONTH);
1471 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
1472 CMOS_WRITE(hrs, RTC_HOURS);
1473 CMOS_WRITE(min, RTC_MINUTES);
1474 CMOS_WRITE(sec, RTC_SECONDS);
1475
1476 CMOS_WRITE(save_control, RTC_CONTROL);
1477 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1478
1479 return 0;
1480}
Horst H. von Brand71898592007-05-26 17:47:53 -07001481#endif /* CONFIG_PCI */
David S. Millerd037e052007-05-11 21:18:50 -07001482
David S. Millera0afaa62007-07-27 14:40:27 -07001483static void mostek_get_rtc_time(struct rtc_time *rtc_tm)
1484{
1485 void __iomem *regs = mstk48t02_regs;
1486 u8 tmp;
1487
1488 spin_lock_irq(&mostek_lock);
1489
1490 tmp = mostek_read(regs + MOSTEK_CREG);
1491 tmp |= MSTK_CREG_READ;
1492 mostek_write(regs + MOSTEK_CREG, tmp);
1493
1494 rtc_tm->tm_sec = MSTK_REG_SEC(regs);
1495 rtc_tm->tm_min = MSTK_REG_MIN(regs);
1496 rtc_tm->tm_hour = MSTK_REG_HOUR(regs);
1497 rtc_tm->tm_mday = MSTK_REG_DOM(regs);
1498 rtc_tm->tm_mon = MSTK_REG_MONTH(regs);
1499 rtc_tm->tm_year = MSTK_CVT_YEAR( MSTK_REG_YEAR(regs) );
1500 rtc_tm->tm_wday = MSTK_REG_DOW(regs);
1501
1502 tmp = mostek_read(regs + MOSTEK_CREG);
1503 tmp &= ~MSTK_CREG_READ;
1504 mostek_write(regs + MOSTEK_CREG, tmp);
1505
1506 spin_unlock_irq(&mostek_lock);
1507
1508 rtc_tm->tm_mon--;
1509 rtc_tm->tm_wday--;
1510 rtc_tm->tm_year -= 1900;
1511}
1512
1513static int mostek_set_rtc_time(struct rtc_time *rtc_tm)
1514{
1515 unsigned char mon, day, hrs, min, sec, wday;
1516 void __iomem *regs = mstk48t02_regs;
1517 unsigned int yrs;
1518 u8 tmp;
1519
1520 yrs = rtc_tm->tm_year + 1900;
1521 mon = rtc_tm->tm_mon + 1;
1522 day = rtc_tm->tm_mday;
1523 wday = rtc_tm->tm_wday + 1;
1524 hrs = rtc_tm->tm_hour;
1525 min = rtc_tm->tm_min;
1526 sec = rtc_tm->tm_sec;
1527
1528 spin_lock_irq(&mostek_lock);
1529
1530 tmp = mostek_read(regs + MOSTEK_CREG);
1531 tmp |= MSTK_CREG_WRITE;
1532 mostek_write(regs + MOSTEK_CREG, tmp);
1533
1534 MSTK_SET_REG_SEC(regs, sec);
1535 MSTK_SET_REG_MIN(regs, min);
1536 MSTK_SET_REG_HOUR(regs, hrs);
1537 MSTK_SET_REG_DOW(regs, wday);
1538 MSTK_SET_REG_DOM(regs, day);
1539 MSTK_SET_REG_MONTH(regs, mon);
1540 MSTK_SET_REG_YEAR(regs, yrs - MSTK_YEAR_ZERO);
1541
1542 tmp = mostek_read(regs + MOSTEK_CREG);
1543 tmp &= ~MSTK_CREG_WRITE;
1544 mostek_write(regs + MOSTEK_CREG, tmp);
1545
1546 spin_unlock_irq(&mostek_lock);
1547
1548 return 0;
1549}
1550
David S. Millerd037e052007-05-11 21:18:50 -07001551struct mini_rtc_ops {
1552 void (*get_rtc_time)(struct rtc_time *);
1553 int (*set_rtc_time)(struct rtc_time *);
1554};
1555
1556static struct mini_rtc_ops starfire_rtc_ops = {
1557 .get_rtc_time = starfire_get_rtc_time,
1558 .set_rtc_time = starfire_set_rtc_time,
1559};
1560
1561static struct mini_rtc_ops hypervisor_rtc_ops = {
1562 .get_rtc_time = hypervisor_get_rtc_time,
1563 .set_rtc_time = hypervisor_set_rtc_time,
1564};
1565
Horst H. von Brand71898592007-05-26 17:47:53 -07001566#ifdef CONFIG_PCI
David S. Millerd037e052007-05-11 21:18:50 -07001567static struct mini_rtc_ops bq4802_rtc_ops = {
1568 .get_rtc_time = bq4802_get_rtc_time,
1569 .set_rtc_time = bq4802_set_rtc_time,
1570};
David S. Millercdee99d2007-07-19 13:59:58 -07001571
1572static struct mini_rtc_ops cmos_rtc_ops = {
1573 .get_rtc_time = cmos_get_rtc_time,
1574 .set_rtc_time = cmos_set_rtc_time,
1575};
Horst H. von Brand71898592007-05-26 17:47:53 -07001576#endif /* CONFIG_PCI */
David S. Millerd037e052007-05-11 21:18:50 -07001577
David S. Millera0afaa62007-07-27 14:40:27 -07001578static struct mini_rtc_ops mostek_rtc_ops = {
1579 .get_rtc_time = mostek_get_rtc_time,
1580 .set_rtc_time = mostek_set_rtc_time,
1581};
1582
David S. Millerd037e052007-05-11 21:18:50 -07001583static struct mini_rtc_ops *mini_rtc_ops;
1584
1585static inline void mini_get_rtc_time(struct rtc_time *time)
1586{
1587 unsigned long flags;
1588
1589 spin_lock_irqsave(&rtc_lock, flags);
1590 mini_rtc_ops->get_rtc_time(time);
1591 spin_unlock_irqrestore(&rtc_lock, flags);
1592}
1593
1594static inline int mini_set_rtc_time(struct rtc_time *time)
1595{
David S. Miller8ba706a2006-03-01 17:32:46 -08001596 unsigned long flags;
1597 int err;
1598
1599 spin_lock_irqsave(&rtc_lock, flags);
David S. Millerd037e052007-05-11 21:18:50 -07001600 err = mini_rtc_ops->set_rtc_time(time);
David S. Miller8ba706a2006-03-01 17:32:46 -08001601 spin_unlock_irqrestore(&rtc_lock, flags);
1602
1603 return err;
1604}
1605
1606static int mini_rtc_ioctl(struct inode *inode, struct file *file,
1607 unsigned int cmd, unsigned long arg)
1608{
1609 struct rtc_time wtime;
1610 void __user *argp = (void __user *)arg;
1611
1612 switch (cmd) {
1613
1614 case RTC_PLL_GET:
1615 return -EINVAL;
1616
1617 case RTC_PLL_SET:
1618 return -EINVAL;
1619
1620 case RTC_UIE_OFF: /* disable ints from RTC updates. */
1621 return 0;
1622
1623 case RTC_UIE_ON: /* enable ints for RTC updates. */
1624 return -EINVAL;
1625
1626 case RTC_RD_TIME: /* Read the time/date from RTC */
1627 /* this doesn't get week-day, who cares */
1628 memset(&wtime, 0, sizeof(wtime));
1629 mini_get_rtc_time(&wtime);
1630
1631 return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0;
1632
1633 case RTC_SET_TIME: /* Set the RTC */
1634 {
Tony Breeds644923d2007-03-28 19:10:12 -07001635 int year, days;
David S. Miller8ba706a2006-03-01 17:32:46 -08001636
1637 if (!capable(CAP_SYS_TIME))
1638 return -EACCES;
1639
1640 if (copy_from_user(&wtime, argp, sizeof(wtime)))
1641 return -EFAULT;
1642
1643 year = wtime.tm_year + 1900;
Tony Breeds644923d2007-03-28 19:10:12 -07001644 days = month_days[wtime.tm_mon] +
1645 ((wtime.tm_mon == 1) && leapyear(year));
David S. Miller8ba706a2006-03-01 17:32:46 -08001646
Tony Breeds644923d2007-03-28 19:10:12 -07001647 if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) ||
1648 (wtime.tm_mday < 1))
David S. Miller8ba706a2006-03-01 17:32:46 -08001649 return -EINVAL;
1650
Tony Breeds644923d2007-03-28 19:10:12 -07001651 if (wtime.tm_mday < 0 || wtime.tm_mday > days)
David S. Miller8ba706a2006-03-01 17:32:46 -08001652 return -EINVAL;
1653
1654 if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
1655 wtime.tm_min < 0 || wtime.tm_min >= 60 ||
1656 wtime.tm_sec < 0 || wtime.tm_sec >= 60)
1657 return -EINVAL;
1658
1659 return mini_set_rtc_time(&wtime);
1660 }
1661 }
1662
1663 return -EINVAL;
1664}
1665
1666static int mini_rtc_open(struct inode *inode, struct file *file)
1667{
Arnd Bergmann09de3612008-05-20 19:16:50 +02001668 lock_kernel();
1669 if (mini_rtc_status & RTC_IS_OPEN) {
1670 unlock_kernel();
David S. Miller8ba706a2006-03-01 17:32:46 -08001671 return -EBUSY;
Arnd Bergmann09de3612008-05-20 19:16:50 +02001672 }
David S. Miller8ba706a2006-03-01 17:32:46 -08001673
1674 mini_rtc_status |= RTC_IS_OPEN;
Arnd Bergmann09de3612008-05-20 19:16:50 +02001675 unlock_kernel();
David S. Miller8ba706a2006-03-01 17:32:46 -08001676
1677 return 0;
1678}
1679
1680static int mini_rtc_release(struct inode *inode, struct file *file)
1681{
1682 mini_rtc_status &= ~RTC_IS_OPEN;
1683 return 0;
1684}
1685
1686
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001687static const struct file_operations mini_rtc_fops = {
David S. Miller8ba706a2006-03-01 17:32:46 -08001688 .owner = THIS_MODULE,
1689 .ioctl = mini_rtc_ioctl,
1690 .open = mini_rtc_open,
1691 .release = mini_rtc_release,
1692};
1693
1694static struct miscdevice rtc_mini_dev =
1695{
1696 .minor = RTC_MINOR,
1697 .name = "rtc",
1698 .fops = &mini_rtc_fops,
1699};
1700
1701static int __init rtc_mini_init(void)
1702{
1703 int retval;
1704
David S. Millerd037e052007-05-11 21:18:50 -07001705 if (tlb_type == hypervisor)
1706 mini_rtc_ops = &hypervisor_rtc_ops;
1707 else if (this_is_starfire)
1708 mini_rtc_ops = &starfire_rtc_ops;
Horst H. von Brand71898592007-05-26 17:47:53 -07001709#ifdef CONFIG_PCI
David S. Millerd037e052007-05-11 21:18:50 -07001710 else if (bq4802_regs)
1711 mini_rtc_ops = &bq4802_rtc_ops;
David S. Millercdee99d2007-07-19 13:59:58 -07001712 else if (ds1287_regs)
1713 mini_rtc_ops = &cmos_rtc_ops;
Horst H. von Brand71898592007-05-26 17:47:53 -07001714#endif /* CONFIG_PCI */
David S. Millera0afaa62007-07-27 14:40:27 -07001715 else if (mstk48t02_regs)
1716 mini_rtc_ops = &mostek_rtc_ops;
David S. Millerd037e052007-05-11 21:18:50 -07001717 else
David S. Miller8ba706a2006-03-01 17:32:46 -08001718 return -ENODEV;
1719
1720 printk(KERN_INFO "Mini RTC Driver\n");
1721
1722 retval = misc_register(&rtc_mini_dev);
1723 if (retval < 0)
1724 return retval;
1725
1726 return 0;
1727}
1728
1729static void __exit rtc_mini_exit(void)
1730{
1731 misc_deregister(&rtc_mini_dev);
1732}
1733
Andrew Morton941e4922008-02-06 01:36:42 -08001734int __devinit read_current_timer(unsigned long *timer_val)
1735{
1736 *timer_val = tick_ops->get_tick();
1737 return 0;
1738}
David S. Miller8ba706a2006-03-01 17:32:46 -08001739
1740module_init(rtc_mini_init);
1741module_exit(rtc_mini_exit);