2 * arch/s390/kernel/time.c
3 * Time of day based timer functions.
6 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Hartmut Penner (hp@de.ibm.com),
8 * Martin Schwidefsky (schwidefsky@de.ibm.com),
9 * Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
11 * Derived from "arch/i386/kernel/time.c"
12 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
15 #include <linux/errno.h>
16 #include <linux/module.h>
17 #include <linux/sched.h>
18 #include <linux/kernel.h>
19 #include <linux/param.h>
20 #include <linux/string.h>
22 #include <linux/interrupt.h>
23 #include <linux/time.h>
24 #include <linux/delay.h>
25 #include <linux/init.h>
26 #include <linux/smp.h>
27 #include <linux/types.h>
28 #include <linux/profile.h>
29 #include <linux/timex.h>
30 #include <linux/notifier.h>
31 #include <linux/clocksource.h>
33 #include <asm/uaccess.h>
34 #include <asm/delay.h>
35 #include <asm/s390_ext.h>
36 #include <asm/div64.h>
38 #include <asm/irq_regs.h>
39 #include <asm/timer.h>
42 /* change this if you have some constant time drift */
43 #define USECS_PER_JIFFY ((unsigned long) 1000000/HZ)
44 #define CLK_TICKS_PER_JIFFY ((unsigned long) USECS_PER_JIFFY << 12)
46 /* The value of the TOD clock for 1.1.1970. */
47 #define TOD_UNIX_EPOCH 0x7d91048bca000000ULL
50 * Create a small time difference between the timer interrupts
51 * on the different cpus to avoid lock contention.
53 #define CPU_DEVIATION (smp_processor_id() << 12)
55 #define TICK_SIZE tick
57 static ext_int_info_t ext_int_info_cc;
58 static ext_int_info_t ext_int_etr_cc;
59 static u64 init_timer_cc;
60 static u64 jiffies_timer_cc;
64 * Scheduler clock - returns current time in nanosec units.
66 unsigned long long sched_clock(void)
68 return ((get_clock() - jiffies_timer_cc) * 125) >> 9;
72 * Monotonic_clock - returns # of nanoseconds passed since time_init()
74 unsigned long long monotonic_clock(void)
78 EXPORT_SYMBOL(monotonic_clock);
80 void tod_to_timeval(__u64 todval, struct timespec *xtime)
82 unsigned long long sec;
87 todval -= (sec * 1000000) << 12;
88 xtime->tv_nsec = ((todval * 1000) >> 12);
91 #ifdef CONFIG_PROFILING
92 #define s390_do_profile() profile_tick(CPU_PROFILING)
94 #define s390_do_profile() do { ; } while(0)
95 #endif /* CONFIG_PROFILING */
98 * Advance the per cpu tick counter up to the time given with the
99 * "time" argument. The per cpu update consists of accounting
100 * the virtual cpu time, calling update_process_times and calling
101 * the profiling hook. If xtime is before time it is advanced as well.
103 void account_ticks(u64 time)
108 /* Calculate how many ticks have passed. */
109 if (time < S390_lowcore.jiffy_timer)
111 tmp = time - S390_lowcore.jiffy_timer;
112 if (tmp >= 2*CLK_TICKS_PER_JIFFY) { /* more than two ticks ? */
113 ticks = __div(tmp, CLK_TICKS_PER_JIFFY) + 1;
114 S390_lowcore.jiffy_timer +=
115 CLK_TICKS_PER_JIFFY * (__u64) ticks;
116 } else if (tmp >= CLK_TICKS_PER_JIFFY) {
118 S390_lowcore.jiffy_timer += 2*CLK_TICKS_PER_JIFFY;
121 S390_lowcore.jiffy_timer += CLK_TICKS_PER_JIFFY;
126 * Do not rely on the boot cpu to do the calls to do_timer.
127 * Spread it over all cpus instead.
129 write_seqlock(&xtime_lock);
130 if (S390_lowcore.jiffy_timer > xtime_cc) {
132 tmp = S390_lowcore.jiffy_timer - xtime_cc;
133 if (tmp >= 2*CLK_TICKS_PER_JIFFY) {
134 xticks = __div(tmp, CLK_TICKS_PER_JIFFY);
135 xtime_cc += (__u64) xticks * CLK_TICKS_PER_JIFFY;
138 xtime_cc += CLK_TICKS_PER_JIFFY;
142 write_sequnlock(&xtime_lock);
147 #ifdef CONFIG_VIRT_CPU_ACCOUNTING
148 account_tick_vtime(current);
151 update_process_times(user_mode(get_irq_regs()));
157 #ifdef CONFIG_NO_IDLE_HZ
159 #ifdef CONFIG_NO_IDLE_HZ_INIT
160 int sysctl_hz_timer = 0;
162 int sysctl_hz_timer = 1;
166 * Stop the HZ tick on the current CPU.
167 * Only cpu_idle may call this function.
169 static void stop_hz_timer(void)
172 unsigned long seq, next;
174 int cpu = smp_processor_id();
176 if (sysctl_hz_timer != 0)
179 cpu_set(cpu, nohz_cpu_mask);
182 * Leave the clock comparator set up for the next timer
183 * tick if either rcu or a softirq is pending.
185 if (rcu_needs_cpu(cpu) || local_softirq_pending()) {
186 cpu_clear(cpu, nohz_cpu_mask);
191 * This cpu is going really idle. Set up the clock comparator
192 * for the next event.
194 next = next_timer_interrupt();
196 seq = read_seqbegin_irqsave(&xtime_lock, flags);
197 timer = ((__u64) next) - ((__u64) jiffies) + jiffies_64;
198 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
200 /* Be careful about overflows. */
201 if (timer < (-1ULL / CLK_TICKS_PER_JIFFY)) {
202 timer = jiffies_timer_cc + timer * CLK_TICKS_PER_JIFFY;
203 if (timer >= jiffies_timer_cc)
206 set_clock_comparator(todval);
210 * Start the HZ tick on the current CPU.
211 * Only cpu_idle may call this function.
213 static void start_hz_timer(void)
215 BUG_ON(!in_interrupt());
217 if (!cpu_isset(smp_processor_id(), nohz_cpu_mask))
219 account_ticks(get_clock());
220 set_clock_comparator(S390_lowcore.jiffy_timer + CPU_DEVIATION);
221 cpu_clear(smp_processor_id(), nohz_cpu_mask);
224 static int nohz_idle_notify(struct notifier_block *self,
225 unsigned long action, void *hcpu)
238 static struct notifier_block nohz_idle_nb = {
239 .notifier_call = nohz_idle_notify,
242 static void __init nohz_init(void)
244 if (register_idle_notifier(&nohz_idle_nb))
245 panic("Couldn't register idle notifier");
251 * Set up per cpu jiffy timer and set the clock comparator.
253 static void setup_jiffy_timer(void)
255 /* Set up clock comparator to next jiffy. */
256 S390_lowcore.jiffy_timer =
257 jiffies_timer_cc + (jiffies_64 + 1) * CLK_TICKS_PER_JIFFY;
258 set_clock_comparator(S390_lowcore.jiffy_timer + CPU_DEVIATION);
262 * Set up lowcore and control register of the current cpu to
263 * enable TOD clock and clock comparator interrupts.
265 void init_cpu_timer(void)
269 /* Enable clock comparator timer interrupt. */
272 /* Always allow ETR external interrupts, even without an ETR. */
276 static void clock_comparator_interrupt(__u16 code)
278 /* set clock comparator for next tick */
279 set_clock_comparator(S390_lowcore.jiffy_timer + CPU_DEVIATION);
282 static void etr_reset(void);
283 static void etr_ext_handler(__u16);
286 * Get the TOD clock running.
288 static u64 __init reset_tod_clock(void)
293 if (store_clock(&time) == 0)
295 /* TOD clock not running. Set the clock to Unix Epoch. */
296 if (set_clock(TOD_UNIX_EPOCH) != 0 || store_clock(&time) != 0)
297 panic("TOD clock not operational.");
299 return TOD_UNIX_EPOCH;
302 static cycle_t read_tod_clock(void)
307 static struct clocksource clocksource_tod = {
310 .read = read_tod_clock,
314 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
319 * Initialize the TOD clock and the CPU timer of
322 void __init time_init(void)
324 init_timer_cc = reset_tod_clock();
325 xtime_cc = init_timer_cc + CLK_TICKS_PER_JIFFY;
326 jiffies_timer_cc = init_timer_cc - jiffies_64 * CLK_TICKS_PER_JIFFY;
329 tod_to_timeval(init_timer_cc - TOD_UNIX_EPOCH, &xtime);
330 set_normalized_timespec(&wall_to_monotonic,
331 -xtime.tv_sec, -xtime.tv_nsec);
333 /* request the clock comparator external interrupt */
334 if (register_early_external_interrupt(0x1004,
335 clock_comparator_interrupt,
336 &ext_int_info_cc) != 0)
337 panic("Couldn't request external interrupt 0x1004");
339 if (clocksource_register(&clocksource_tod) != 0)
340 panic("Could not register TOD clock source");
342 /* request the etr external interrupt */
343 if (register_early_external_interrupt(0x1406, etr_ext_handler,
344 &ext_int_etr_cc) != 0)
345 panic("Couldn't request external interrupt 0x1406");
347 /* Enable TOD clock interrupts on the boot cpu. */
350 #ifdef CONFIG_NO_IDLE_HZ
354 #ifdef CONFIG_VIRT_TIMER
360 * External Time Reference (ETR) code.
362 static int etr_port0_online;
363 static int etr_port1_online;
365 static int __init early_parse_etr(char *p)
367 if (strncmp(p, "off", 3) == 0)
368 etr_port0_online = etr_port1_online = 0;
369 else if (strncmp(p, "port0", 5) == 0)
370 etr_port0_online = 1;
371 else if (strncmp(p, "port1", 5) == 0)
372 etr_port1_online = 1;
373 else if (strncmp(p, "on", 2) == 0)
374 etr_port0_online = etr_port1_online = 1;
377 early_param("etr", early_parse_etr);
380 ETR_EVENT_PORT0_CHANGE,
381 ETR_EVENT_PORT1_CHANGE,
382 ETR_EVENT_PORT_ALERT,
383 ETR_EVENT_SYNC_CHECK,
384 ETR_EVENT_SWITCH_LOCAL,
395 * Valid bit combinations of the eacr register are (x = don't care):
396 * e0 e1 dp p0 p1 ea es sl
397 * 0 0 x 0 0 0 0 0 initial, disabled state
398 * 0 0 x 0 1 1 0 0 port 1 online
399 * 0 0 x 1 0 1 0 0 port 0 online
400 * 0 0 x 1 1 1 0 0 both ports online
401 * 0 1 x 0 1 1 0 0 port 1 online and usable, ETR or PPS mode
402 * 0 1 x 0 1 1 0 1 port 1 online, usable and ETR mode
403 * 0 1 x 0 1 1 1 0 port 1 online, usable, PPS mode, in-sync
404 * 0 1 x 0 1 1 1 1 port 1 online, usable, ETR mode, in-sync
405 * 0 1 x 1 1 1 0 0 both ports online, port 1 usable
406 * 0 1 x 1 1 1 1 0 both ports online, port 1 usable, PPS mode, in-sync
407 * 0 1 x 1 1 1 1 1 both ports online, port 1 usable, ETR mode, in-sync
408 * 1 0 x 1 0 1 0 0 port 0 online and usable, ETR or PPS mode
409 * 1 0 x 1 0 1 0 1 port 0 online, usable and ETR mode
410 * 1 0 x 1 0 1 1 0 port 0 online, usable, PPS mode, in-sync
411 * 1 0 x 1 0 1 1 1 port 0 online, usable, ETR mode, in-sync
412 * 1 0 x 1 1 1 0 0 both ports online, port 0 usable
413 * 1 0 x 1 1 1 1 0 both ports online, port 0 usable, PPS mode, in-sync
414 * 1 0 x 1 1 1 1 1 both ports online, port 0 usable, ETR mode, in-sync
415 * 1 1 x 1 1 1 1 0 both ports online & usable, ETR, in-sync
416 * 1 1 x 1 1 1 1 1 both ports online & usable, ETR, in-sync
418 static struct etr_eacr etr_eacr;
419 static u64 etr_tolec; /* time of last eacr update */
420 static unsigned long etr_flags;
421 static struct etr_aib etr_port0;
422 static int etr_port0_uptodate;
423 static struct etr_aib etr_port1;
424 static int etr_port1_uptodate;
425 static unsigned long etr_events;
426 static struct timer_list etr_timer;
427 static DEFINE_PER_CPU(atomic_t, etr_sync_word);
429 static void etr_timeout(unsigned long dummy);
430 static void etr_work_fn(struct work_struct *work);
431 static DECLARE_WORK(etr_work, etr_work_fn);
434 * The etr get_clock function. It will write the current clock value
435 * to the clock pointer and return 0 if the clock is in sync with the
436 * external time source. If the clock mode is local it will return
437 * -ENOSYS and -EAGAIN if the clock is not in sync with the external
438 * reference. This function is what ETR is all about..
440 int get_sync_clock(unsigned long long *clock)
443 unsigned int sw0, sw1;
445 sw_ptr = &get_cpu_var(etr_sync_word);
446 sw0 = atomic_read(sw_ptr);
447 *clock = get_clock();
448 sw1 = atomic_read(sw_ptr);
449 put_cpu_var(etr_sync_sync);
450 if (sw0 == sw1 && (sw0 & 0x80000000U))
451 /* Success: time is in sync. */
453 if (test_bit(ETR_FLAG_ENOSYS, &etr_flags))
455 if (test_bit(ETR_FLAG_EACCES, &etr_flags))
459 EXPORT_SYMBOL(get_sync_clock);
462 * Make get_sync_clock return -EAGAIN.
464 static void etr_disable_sync_clock(void *dummy)
466 atomic_t *sw_ptr = &__get_cpu_var(etr_sync_word);
468 * Clear the in-sync bit 2^31. All get_sync_clock calls will
469 * fail until the sync bit is turned back on. In addition
470 * increase the "sequence" counter to avoid the race of an
471 * etr event and the complete recovery against get_sync_clock.
473 atomic_clear_mask(0x80000000, sw_ptr);
478 * Make get_sync_clock return 0 again.
479 * Needs to be called from a context disabled for preemption.
481 static void etr_enable_sync_clock(void)
483 atomic_t *sw_ptr = &__get_cpu_var(etr_sync_word);
484 atomic_set_mask(0x80000000, sw_ptr);
488 * Reset ETR attachment.
490 static void etr_reset(void)
492 etr_eacr = (struct etr_eacr) {
493 .e0 = 0, .e1 = 0, ._pad0 = 4, .dp = 0,
494 .p0 = 0, .p1 = 0, ._pad1 = 0, .ea = 0,
496 if (etr_setr(&etr_eacr) == 0)
497 etr_tolec = get_clock();
499 set_bit(ETR_FLAG_ENOSYS, &etr_flags);
500 if (etr_port0_online || etr_port1_online) {
501 printk(KERN_WARNING "Running on non ETR capable "
502 "machine, only local mode available.\n");
503 etr_port0_online = etr_port1_online = 0;
508 static int __init etr_init(void)
512 if (test_bit(ETR_FLAG_ENOSYS, &etr_flags))
514 /* Check if this machine has the steai instruction. */
515 if (etr_steai(&aib, ETR_STEAI_STEPPING_PORT) == 0)
516 set_bit(ETR_FLAG_STEAI, &etr_flags);
517 setup_timer(&etr_timer, etr_timeout, 0UL);
518 if (!etr_port0_online && !etr_port1_online)
519 set_bit(ETR_FLAG_EACCES, &etr_flags);
520 if (etr_port0_online) {
521 set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
522 schedule_work(&etr_work);
524 if (etr_port1_online) {
525 set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
526 schedule_work(&etr_work);
531 arch_initcall(etr_init);
534 * Two sorts of ETR machine checks. The architecture reads:
535 * "When a machine-check niterruption occurs and if a switch-to-local or
536 * ETR-sync-check interrupt request is pending but disabled, this pending
537 * disabled interruption request is indicated and is cleared".
538 * Which means that we can get etr_switch_to_local events from the machine
539 * check handler although the interruption condition is disabled. Lovely..
543 * Switch to local machine check. This is called when the last usable
544 * ETR port goes inactive. After switch to local the clock is not in sync.
546 void etr_switch_to_local(void)
550 etr_disable_sync_clock(NULL);
551 set_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events);
552 schedule_work(&etr_work);
556 * ETR sync check machine check. This is called when the ETR OTE and the
557 * local clock OTE are farther apart than the ETR sync check tolerance.
558 * After a ETR sync check the clock is not in sync. The machine check
559 * is broadcasted to all cpus at the same time.
561 void etr_sync_check(void)
565 etr_disable_sync_clock(NULL);
566 set_bit(ETR_EVENT_SYNC_CHECK, &etr_events);
567 schedule_work(&etr_work);
571 * ETR external interrupt. There are two causes:
572 * 1) port state change, check the usability of the port
573 * 2) port alert, one of the ETR-data-validity bits (v1-v2 bits of the
574 * sldr-status word) or ETR-data word 1 (edf1) or ETR-data word 3 (edf3)
575 * or ETR-data word 4 (edf4) has changed.
577 static void etr_ext_handler(__u16 code)
579 struct etr_interruption_parameter *intparm =
580 (struct etr_interruption_parameter *) &S390_lowcore.ext_params;
583 /* ETR port 0 state change. */
584 set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
586 /* ETR port 1 state change. */
587 set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
590 * ETR port alert on either port 0, 1 or both.
591 * Both ports are not up-to-date now.
593 set_bit(ETR_EVENT_PORT_ALERT, &etr_events);
594 schedule_work(&etr_work);
597 static void etr_timeout(unsigned long dummy)
599 set_bit(ETR_EVENT_UPDATE, &etr_events);
600 schedule_work(&etr_work);
604 * Check if the etr mode is pss.
606 static inline int etr_mode_is_pps(struct etr_eacr eacr)
608 return eacr.es && !eacr.sl;
612 * Check if the etr mode is etr.
614 static inline int etr_mode_is_etr(struct etr_eacr eacr)
616 return eacr.es && eacr.sl;
620 * Check if the port can be used for TOD synchronization.
621 * For PPS mode the port has to receive OTEs. For ETR mode
622 * the port has to receive OTEs, the ETR stepping bit has to
623 * be zero and the validity bits for data frame 1, 2, and 3
626 static int etr_port_valid(struct etr_aib *aib, int port)
630 /* Check that this port is receiving OTEs. */
634 psc = port ? aib->esw.psc1 : aib->esw.psc0;
635 if (psc == etr_lpsc_pps_mode)
637 if (psc == etr_lpsc_operational_step)
638 return !aib->esw.y && aib->slsw.v1 &&
639 aib->slsw.v2 && aib->slsw.v3;
644 * Check if two ports are on the same network.
646 static int etr_compare_network(struct etr_aib *aib1, struct etr_aib *aib2)
648 // FIXME: any other fields we have to compare?
649 return aib1->edf1.net_id == aib2->edf1.net_id;
653 * Wrapper for etr_stei that converts physical port states
654 * to logical port states to be consistent with the output
655 * of stetr (see etr_psc vs. etr_lpsc).
657 static void etr_steai_cv(struct etr_aib *aib, unsigned int func)
659 BUG_ON(etr_steai(aib, func) != 0);
660 /* Convert port state to logical port state. */
661 if (aib->esw.psc0 == 1)
663 else if (aib->esw.psc0 == 0 && aib->esw.p == 0)
665 if (aib->esw.psc1 == 1)
667 else if (aib->esw.psc1 == 0 && aib->esw.p == 1)
672 * Check if the aib a2 is still connected to the same attachment as
673 * aib a1, the etv values differ by one and a2 is valid.
675 static int etr_aib_follows(struct etr_aib *a1, struct etr_aib *a2, int p)
677 int state_a1, state_a2;
679 /* Paranoia check: e0/e1 should better be the same. */
680 if (a1->esw.eacr.e0 != a2->esw.eacr.e0 ||
681 a1->esw.eacr.e1 != a2->esw.eacr.e1)
684 /* Still connected to the same etr ? */
685 state_a1 = p ? a1->esw.psc1 : a1->esw.psc0;
686 state_a2 = p ? a2->esw.psc1 : a2->esw.psc0;
687 if (state_a1 == etr_lpsc_operational_step) {
688 if (state_a2 != etr_lpsc_operational_step ||
689 a1->edf1.net_id != a2->edf1.net_id ||
690 a1->edf1.etr_id != a2->edf1.etr_id ||
691 a1->edf1.etr_pn != a2->edf1.etr_pn)
693 } else if (state_a2 != etr_lpsc_pps_mode)
696 /* The ETV value of a2 needs to be ETV of a1 + 1. */
697 if (a1->edf2.etv + 1 != a2->edf2.etv)
700 if (!etr_port_valid(a2, p))
707 * The time is "clock". xtime is what we think the time is.
708 * Adjust the value by a multiple of jiffies and add the delta to ntp.
709 * "delay" is an approximation how long the synchronization took. If
710 * the time correction is positive, then "delay" is subtracted from
711 * the time difference and only the remaining part is passed to ntp.
713 static void etr_adjust_time(unsigned long long clock, unsigned long long delay)
715 unsigned long long delta, ticks;
719 * We don't have to take the xtime lock because the cpu
720 * executing etr_adjust_time is running disabled in
721 * tasklet context and all other cpus are looping in
722 * etr_sync_cpu_start.
724 if (clock > xtime_cc) {
725 /* It is later than we thought. */
726 delta = ticks = clock - xtime_cc;
727 delta = ticks = (delta < delay) ? 0 : delta - delay;
728 delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
729 init_timer_cc = init_timer_cc + delta;
730 jiffies_timer_cc = jiffies_timer_cc + delta;
731 xtime_cc = xtime_cc + delta;
732 adjust.offset = ticks * (1000000 / HZ);
734 /* It is earlier than we thought. */
735 delta = ticks = xtime_cc - clock;
736 delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
737 init_timer_cc = init_timer_cc - delta;
738 jiffies_timer_cc = jiffies_timer_cc - delta;
739 xtime_cc = xtime_cc - delta;
740 adjust.offset = -ticks * (1000000 / HZ);
742 if (adjust.offset != 0) {
743 printk(KERN_NOTICE "etr: time adjusted by %li micro-seconds\n",
745 adjust.modes = ADJ_OFFSET_SINGLESHOT;
746 do_adjtimex(&adjust);
751 static void etr_sync_cpu_start(void *dummy)
753 int *in_sync = dummy;
755 etr_enable_sync_clock();
757 * This looks like a busy wait loop but it isn't. etr_sync_cpus
758 * is called on all other cpus while the TOD clocks is stopped.
759 * __udelay will stop the cpu on an enabled wait psw until the
760 * TOD is running again.
762 while (*in_sync == 0) {
765 * A different cpu changes *in_sync. Therefore use
766 * barrier() to force memory access.
771 /* Didn't work. Clear per-cpu in sync bit again. */
772 etr_disable_sync_clock(NULL);
774 * This round of TOD syncing is done. Set the clock comparator
775 * to the next tick and let the processor continue.
780 static void etr_sync_cpu_end(void *dummy)
783 #endif /* CONFIG_SMP */
786 * Sync the TOD clock using the port refered to by aibp. This port
787 * has to be enabled and the other port has to be disabled. The
788 * last eacr update has to be more than 1.6 seconds in the past.
790 static int etr_sync_clock(struct etr_aib *aib, int port)
792 struct etr_aib *sync_port;
793 unsigned long long clock, delay;
794 int in_sync, follows;
797 /* Check if the current aib is adjacent to the sync port aib. */
798 sync_port = (port == 0) ? &etr_port0 : &etr_port1;
799 follows = etr_aib_follows(sync_port, aib, port);
800 memcpy(sync_port, aib, sizeof(*aib));
805 * Catch all other cpus and make them wait until we have
806 * successfully synced the clock. smp_call_function will
807 * return after all other cpus are in etr_sync_cpu_start.
811 smp_call_function(etr_sync_cpu_start,&in_sync,0,0);
813 etr_enable_sync_clock();
815 /* Set clock to next OTE. */
816 __ctl_set_bit(14, 21);
817 __ctl_set_bit(0, 29);
818 clock = ((unsigned long long) (aib->edf2.etv + 1)) << 32;
819 if (set_clock(clock) == 0) {
820 __udelay(1); /* Wait for the clock to start. */
821 __ctl_clear_bit(0, 29);
822 __ctl_clear_bit(14, 21);
824 /* Adjust Linux timing variables. */
825 delay = (unsigned long long)
826 (aib->edf2.etv - sync_port->edf2.etv) << 32;
827 etr_adjust_time(clock, delay);
829 /* Verify that the clock is properly set. */
830 if (!etr_aib_follows(sync_port, aib, port)) {
832 etr_disable_sync_clock(NULL);
840 /* Could not set the clock ?!? */
841 __ctl_clear_bit(0, 29);
842 __ctl_clear_bit(14, 21);
843 etr_disable_sync_clock(NULL);
848 smp_call_function(etr_sync_cpu_end,NULL,0,0);
854 * Handle the immediate effects of the different events.
855 * The port change event is used for online/offline changes.
857 static struct etr_eacr etr_handle_events(struct etr_eacr eacr)
859 if (test_and_clear_bit(ETR_EVENT_SYNC_CHECK, &etr_events))
861 if (test_and_clear_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events))
862 eacr.es = eacr.sl = 0;
863 if (test_and_clear_bit(ETR_EVENT_PORT_ALERT, &etr_events))
864 etr_port0_uptodate = etr_port1_uptodate = 0;
866 if (test_and_clear_bit(ETR_EVENT_PORT0_CHANGE, &etr_events)) {
869 * Port change of an enabled port. We have to
870 * assume that this can have caused an stepping
873 etr_tolec = get_clock();
874 eacr.p0 = etr_port0_online;
877 etr_port0_uptodate = 0;
879 if (test_and_clear_bit(ETR_EVENT_PORT1_CHANGE, &etr_events)) {
882 * Port change of an enabled port. We have to
883 * assume that this can have caused an stepping
886 etr_tolec = get_clock();
887 eacr.p1 = etr_port1_online;
890 etr_port1_uptodate = 0;
892 clear_bit(ETR_EVENT_UPDATE, &etr_events);
897 * Set up a timer that expires after the etr_tolec + 1.6 seconds if
898 * one of the ports needs an update.
900 static void etr_set_tolec_timeout(unsigned long long now)
902 unsigned long micros;
904 if ((!etr_eacr.p0 || etr_port0_uptodate) &&
905 (!etr_eacr.p1 || etr_port1_uptodate))
907 micros = (now > etr_tolec) ? ((now - etr_tolec) >> 12) : 0;
908 micros = (micros > 1600000) ? 0 : 1600000 - micros;
909 mod_timer(&etr_timer, jiffies + (micros * HZ) / 1000000 + 1);
913 * Set up a time that expires after 1/2 second.
915 static void etr_set_sync_timeout(void)
917 mod_timer(&etr_timer, jiffies + HZ/2);
921 * Update the aib information for one or both ports.
923 static struct etr_eacr etr_handle_update(struct etr_aib *aib,
924 struct etr_eacr eacr)
926 /* With both ports disabled the aib information is useless. */
927 if (!eacr.e0 && !eacr.e1)
930 /* Update port0 or port1 with aib stored in etr_work_fn. */
931 if (aib->esw.q == 0) {
932 /* Information for port 0 stored. */
933 if (eacr.p0 && !etr_port0_uptodate) {
935 if (etr_port0_online)
936 etr_port0_uptodate = 1;
939 /* Information for port 1 stored. */
940 if (eacr.p1 && !etr_port1_uptodate) {
942 if (etr_port0_online)
943 etr_port1_uptodate = 1;
948 * Do not try to get the alternate port aib if the clock
949 * is not in sync yet.
955 * If steai is available we can get the information about
956 * the other port immediately. If only stetr is available the
957 * data-port bit toggle has to be used.
959 if (test_bit(ETR_FLAG_STEAI, &etr_flags)) {
960 if (eacr.p0 && !etr_port0_uptodate) {
961 etr_steai_cv(&etr_port0, ETR_STEAI_PORT_0);
962 etr_port0_uptodate = 1;
964 if (eacr.p1 && !etr_port1_uptodate) {
965 etr_steai_cv(&etr_port1, ETR_STEAI_PORT_1);
966 etr_port1_uptodate = 1;
970 * One port was updated above, if the other
971 * port is not uptodate toggle dp bit.
973 if ((eacr.p0 && !etr_port0_uptodate) ||
974 (eacr.p1 && !etr_port1_uptodate))
983 * Write new etr control register if it differs from the current one.
984 * Return 1 if etr_tolec has been updated as well.
986 static void etr_update_eacr(struct etr_eacr eacr)
990 if (memcmp(&etr_eacr, &eacr, sizeof(eacr)) == 0)
991 /* No change, return. */
994 * The disable of an active port of the change of the data port
995 * bit can/will cause a change in the data port.
997 dp_changed = etr_eacr.e0 > eacr.e0 || etr_eacr.e1 > eacr.e1 ||
998 (etr_eacr.dp ^ eacr.dp) != 0;
1000 etr_setr(&etr_eacr);
1002 etr_tolec = get_clock();
1006 * ETR tasklet. In this function you'll find the main logic. In
1007 * particular this is the only function that calls etr_update_eacr(),
1008 * it "controls" the etr control register.
1010 static void etr_work_fn(struct work_struct *work)
1012 unsigned long long now;
1013 struct etr_eacr eacr;
1017 /* Create working copy of etr_eacr. */
1020 /* Check for the different events and their immediate effects. */
1021 eacr = etr_handle_events(eacr);
1023 /* Check if ETR is supposed to be active. */
1024 eacr.ea = eacr.p0 || eacr.p1;
1026 /* Both ports offline. Reset everything. */
1027 eacr.dp = eacr.es = eacr.sl = 0;
1028 on_each_cpu(etr_disable_sync_clock, NULL, 0, 1);
1029 del_timer_sync(&etr_timer);
1030 etr_update_eacr(eacr);
1031 set_bit(ETR_FLAG_EACCES, &etr_flags);
1035 /* Store aib to get the current ETR status word. */
1036 BUG_ON(etr_stetr(&aib) != 0);
1037 etr_port0.esw = etr_port1.esw = aib.esw; /* Copy status word. */
1041 * Update the port information if the last stepping port change
1042 * or data port change is older than 1.6 seconds.
1044 if (now >= etr_tolec + (1600000 << 12))
1045 eacr = etr_handle_update(&aib, eacr);
1048 * Select ports to enable. The prefered synchronization mode is PPS.
1049 * If a port can be enabled depends on a number of things:
1050 * 1) The port needs to be online and uptodate. A port is not
1051 * disabled just because it is not uptodate, but it is only
1052 * enabled if it is uptodate.
1053 * 2) The port needs to have the same mode (pps / etr).
1054 * 3) The port needs to be usable -> etr_port_valid() == 1
1055 * 4) To enable the second port the clock needs to be in sync.
1056 * 5) If both ports are useable and are ETR ports, the network id
1057 * has to be the same.
1058 * The eacr.sl bit is used to indicate etr mode vs. pps mode.
1060 if (eacr.p0 && aib.esw.psc0 == etr_lpsc_pps_mode) {
1063 if (!etr_mode_is_pps(etr_eacr))
1065 if (!eacr.es || !eacr.p1 || aib.esw.psc1 != etr_lpsc_pps_mode)
1067 // FIXME: uptodate checks ?
1068 else if (etr_port0_uptodate && etr_port1_uptodate)
1070 sync_port = (etr_port0_uptodate &&
1071 etr_port_valid(&etr_port0, 0)) ? 0 : -1;
1072 clear_bit(ETR_FLAG_EACCES, &etr_flags);
1073 } else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_pps_mode) {
1077 if (!etr_mode_is_pps(etr_eacr))
1079 sync_port = (etr_port1_uptodate &&
1080 etr_port_valid(&etr_port1, 1)) ? 1 : -1;
1081 clear_bit(ETR_FLAG_EACCES, &etr_flags);
1082 } else if (eacr.p0 && aib.esw.psc0 == etr_lpsc_operational_step) {
1085 if (!etr_mode_is_etr(etr_eacr))
1087 if (!eacr.es || !eacr.p1 ||
1088 aib.esw.psc1 != etr_lpsc_operational_alt)
1090 else if (etr_port0_uptodate && etr_port1_uptodate &&
1091 etr_compare_network(&etr_port0, &etr_port1))
1093 sync_port = (etr_port0_uptodate &&
1094 etr_port_valid(&etr_port0, 0)) ? 0 : -1;
1095 clear_bit(ETR_FLAG_EACCES, &etr_flags);
1096 } else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_operational_step) {
1100 if (!etr_mode_is_etr(etr_eacr))
1102 sync_port = (etr_port1_uptodate &&
1103 etr_port_valid(&etr_port1, 1)) ? 1 : -1;
1104 clear_bit(ETR_FLAG_EACCES, &etr_flags);
1106 /* Both ports not usable. */
1107 eacr.es = eacr.sl = 0;
1109 set_bit(ETR_FLAG_EACCES, &etr_flags);
1113 * If the clock is in sync just update the eacr and return.
1114 * If there is no valid sync port wait for a port update.
1116 if (eacr.es || sync_port < 0) {
1117 etr_update_eacr(eacr);
1118 etr_set_tolec_timeout(now);
1123 * Prepare control register for clock syncing
1124 * (reset data port bit, set sync check control.
1130 * Update eacr and try to synchronize the clock. If the update
1131 * of eacr caused a stepping port switch (or if we have to
1132 * assume that a stepping port switch has occured) or the
1133 * clock syncing failed, reset the sync check control bit
1134 * and set up a timer to try again after 0.5 seconds
1136 etr_update_eacr(eacr);
1137 if (now < etr_tolec + (1600000 << 12) ||
1138 etr_sync_clock(&aib, sync_port) != 0) {
1139 /* Sync failed. Try again in 1/2 second. */
1141 etr_update_eacr(eacr);
1142 etr_set_sync_timeout();
1144 etr_set_tolec_timeout(now);
1148 * Sysfs interface functions
1150 static struct sysdev_class etr_sysclass = {
1151 set_kset_name("etr")
1154 static struct sys_device etr_port0_dev = {
1156 .cls = &etr_sysclass,
1159 static struct sys_device etr_port1_dev = {
1161 .cls = &etr_sysclass,
1165 * ETR class attributes
1167 static ssize_t etr_stepping_port_show(struct sysdev_class *class, char *buf)
1169 return sprintf(buf, "%i\n", etr_port0.esw.p);
1172 static SYSDEV_CLASS_ATTR(stepping_port, 0400, etr_stepping_port_show, NULL);
1174 static ssize_t etr_stepping_mode_show(struct sysdev_class *class, char *buf)
1178 if (etr_mode_is_pps(etr_eacr))
1180 else if (etr_mode_is_etr(etr_eacr))
1184 return sprintf(buf, "%s\n", mode_str);
1187 static SYSDEV_CLASS_ATTR(stepping_mode, 0400, etr_stepping_mode_show, NULL);
1190 * ETR port attributes
1192 static inline struct etr_aib *etr_aib_from_dev(struct sys_device *dev)
1194 if (dev == &etr_port0_dev)
1195 return etr_port0_online ? &etr_port0 : NULL;
1197 return etr_port1_online ? &etr_port1 : NULL;
1200 static ssize_t etr_online_show(struct sys_device *dev, char *buf)
1202 unsigned int online;
1204 online = (dev == &etr_port0_dev) ? etr_port0_online : etr_port1_online;
1205 return sprintf(buf, "%i\n", online);
1208 static ssize_t etr_online_store(struct sys_device *dev,
1209 const char *buf, size_t count)
1213 value = simple_strtoul(buf, NULL, 0);
1214 if (value != 0 && value != 1)
1216 if (test_bit(ETR_FLAG_ENOSYS, &etr_flags))
1218 if (dev == &etr_port0_dev) {
1219 if (etr_port0_online == value)
1220 return count; /* Nothing to do. */
1221 etr_port0_online = value;
1222 set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
1223 schedule_work(&etr_work);
1225 if (etr_port1_online == value)
1226 return count; /* Nothing to do. */
1227 etr_port1_online = value;
1228 set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
1229 schedule_work(&etr_work);
1234 static SYSDEV_ATTR(online, 0600, etr_online_show, etr_online_store);
1236 static ssize_t etr_stepping_control_show(struct sys_device *dev, char *buf)
1238 return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
1239 etr_eacr.e0 : etr_eacr.e1);
1242 static SYSDEV_ATTR(stepping_control, 0400, etr_stepping_control_show, NULL);
1244 static ssize_t etr_mode_code_show(struct sys_device *dev, char *buf)
1246 if (!etr_port0_online && !etr_port1_online)
1247 /* Status word is not uptodate if both ports are offline. */
1249 return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
1250 etr_port0.esw.psc0 : etr_port0.esw.psc1);
1253 static SYSDEV_ATTR(state_code, 0400, etr_mode_code_show, NULL);
1255 static ssize_t etr_untuned_show(struct sys_device *dev, char *buf)
1257 struct etr_aib *aib = etr_aib_from_dev(dev);
1259 if (!aib || !aib->slsw.v1)
1261 return sprintf(buf, "%i\n", aib->edf1.u);
1264 static SYSDEV_ATTR(untuned, 0400, etr_untuned_show, NULL);
1266 static ssize_t etr_network_id_show(struct sys_device *dev, char *buf)
1268 struct etr_aib *aib = etr_aib_from_dev(dev);
1270 if (!aib || !aib->slsw.v1)
1272 return sprintf(buf, "%i\n", aib->edf1.net_id);
1275 static SYSDEV_ATTR(network, 0400, etr_network_id_show, NULL);
1277 static ssize_t etr_id_show(struct sys_device *dev, char *buf)
1279 struct etr_aib *aib = etr_aib_from_dev(dev);
1281 if (!aib || !aib->slsw.v1)
1283 return sprintf(buf, "%i\n", aib->edf1.etr_id);
1286 static SYSDEV_ATTR(id, 0400, etr_id_show, NULL);
1288 static ssize_t etr_port_number_show(struct sys_device *dev, char *buf)
1290 struct etr_aib *aib = etr_aib_from_dev(dev);
1292 if (!aib || !aib->slsw.v1)
1294 return sprintf(buf, "%i\n", aib->edf1.etr_pn);
1297 static SYSDEV_ATTR(port, 0400, etr_port_number_show, NULL);
1299 static ssize_t etr_coupled_show(struct sys_device *dev, char *buf)
1301 struct etr_aib *aib = etr_aib_from_dev(dev);
1303 if (!aib || !aib->slsw.v3)
1305 return sprintf(buf, "%i\n", aib->edf3.c);
1308 static SYSDEV_ATTR(coupled, 0400, etr_coupled_show, NULL);
1310 static ssize_t etr_local_time_show(struct sys_device *dev, char *buf)
1312 struct etr_aib *aib = etr_aib_from_dev(dev);
1314 if (!aib || !aib->slsw.v3)
1316 return sprintf(buf, "%i\n", aib->edf3.blto);
1319 static SYSDEV_ATTR(local_time, 0400, etr_local_time_show, NULL);
1321 static ssize_t etr_utc_offset_show(struct sys_device *dev, char *buf)
1323 struct etr_aib *aib = etr_aib_from_dev(dev);
1325 if (!aib || !aib->slsw.v3)
1327 return sprintf(buf, "%i\n", aib->edf3.buo);
1330 static SYSDEV_ATTR(utc_offset, 0400, etr_utc_offset_show, NULL);
1332 static struct sysdev_attribute *etr_port_attributes[] = {
1334 &attr_stepping_control,
1346 static int __init etr_register_port(struct sys_device *dev)
1348 struct sysdev_attribute **attr;
1351 rc = sysdev_register(dev);
1354 for (attr = etr_port_attributes; *attr; attr++) {
1355 rc = sysdev_create_file(dev, *attr);
1361 for (; attr >= etr_port_attributes; attr--)
1362 sysdev_remove_file(dev, *attr);
1363 sysdev_unregister(dev);
1368 static void __init etr_unregister_port(struct sys_device *dev)
1370 struct sysdev_attribute **attr;
1372 for (attr = etr_port_attributes; *attr; attr++)
1373 sysdev_remove_file(dev, *attr);
1374 sysdev_unregister(dev);
1377 static int __init etr_init_sysfs(void)
1381 rc = sysdev_class_register(&etr_sysclass);
1384 rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_port);
1386 goto out_unreg_class;
1387 rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_mode);
1389 goto out_remove_stepping_port;
1390 rc = etr_register_port(&etr_port0_dev);
1392 goto out_remove_stepping_mode;
1393 rc = etr_register_port(&etr_port1_dev);
1395 goto out_remove_port0;
1399 etr_unregister_port(&etr_port0_dev);
1400 out_remove_stepping_mode:
1401 sysdev_class_remove_file(&etr_sysclass, &attr_stepping_mode);
1402 out_remove_stepping_port:
1403 sysdev_class_remove_file(&etr_sysclass, &attr_stepping_port);
1405 sysdev_class_unregister(&etr_sysclass);
1410 device_initcall(etr_init_sysfs);