blob: ebbfab3c6e5ac542019d175c520beddb9197b432 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/s390/kernel/time.c
3 * Time of day based timer functions.
4 *
5 * S390 version
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02006 * Copyright IBM Corp. 1999, 2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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)
10 *
11 * Derived from "arch/i386/kernel/time.c"
12 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
13 */
14
Martin Schwidefskyfeab6502008-12-25 13:39:38 +010015#define KMSG_COMPONENT "time"
16#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17
Heiko Carstens052ff462011-01-05 12:47:28 +010018#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/errno.h>
20#include <linux/module.h>
21#include <linux/sched.h>
22#include <linux/kernel.h>
23#include <linux/param.h>
24#include <linux/string.h>
25#include <linux/mm.h>
26#include <linux/interrupt.h>
Heiko Carstens750887d2008-12-25 13:38:37 +010027#include <linux/cpu.h>
28#include <linux/stop_machine.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/time.h>
Ralf Baechle3367b992007-05-08 00:27:52 -070030#include <linux/sysdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/delay.h>
32#include <linux/init.h>
33#include <linux/smp.h>
34#include <linux/types.h>
35#include <linux/profile.h>
36#include <linux/timex.h>
37#include <linux/notifier.h>
Martin Schwidefskydc64bef2006-10-06 16:38:48 +020038#include <linux/clocksource.h>
Heiko Carstens5a62b192008-04-17 07:46:25 +020039#include <linux/clockchips.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/gfp.h>
Martin Schwidefsky860dba42011-01-05 12:47:25 +010041#include <linux/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/uaccess.h>
43#include <asm/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/div64.h>
Martin Schwidefskyb0206322008-12-25 13:38:36 +010045#include <asm/vdso.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/irq.h>
Heiko Carstens5a489b92006-10-06 16:38:35 +020047#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/timer.h>
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010049#include <asm/etr.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020050#include <asm/cio.h>
Martin Schwidefsky638ad342011-10-30 15:17:13 +010051#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/* change this if you have some constant time drift */
54#define USECS_PER_JIFFY ((unsigned long) 1000000/HZ)
55#define CLK_TICKS_PER_JIFFY ((unsigned long) USECS_PER_JIFFY << 12)
56
Martin Schwidefskyb6112cc2009-04-14 15:36:28 +020057u64 sched_clock_base_cc = -1; /* Force to data section. */
Heiko Carstens05e7ff72009-09-11 10:28:31 +020058EXPORT_SYMBOL_GPL(sched_clock_base_cc);
Martin Schwidefskyb6112cc2009-04-14 15:36:28 +020059
Heiko Carstens5a62b192008-04-17 07:46:25 +020060static DEFINE_PER_CPU(struct clock_event_device, comparators);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
63 * Scheduler clock - returns current time in nanosec units.
64 */
Martin Schwidefsky860dba42011-01-05 12:47:25 +010065unsigned long long notrace __kprobes sched_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Heiko Carstens05e7ff72009-09-11 10:28:31 +020067 return (get_clock_monotonic() * 125) >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Jan Glauber32f65f22006-02-01 03:06:33 -080070/*
71 * Monotonic_clock - returns # of nanoseconds passed since time_init()
72 */
73unsigned long long monotonic_clock(void)
74{
75 return sched_clock();
76}
77EXPORT_SYMBOL(monotonic_clock);
78
John Stultzb1e2ba82010-03-08 12:25:19 +010079void tod_to_timeval(__u64 todval, struct timespec *xt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 unsigned long long sec;
82
83 sec = todval >> 12;
84 do_div(sec, 1000000);
John Stultzb1e2ba82010-03-08 12:25:19 +010085 xt->tv_sec = sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 todval -= (sec * 1000000) << 12;
John Stultzb1e2ba82010-03-08 12:25:19 +010087 xt->tv_nsec = ((todval * 1000) >> 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
Christof Schmittb592e892009-08-18 15:43:31 +020089EXPORT_SYMBOL(tod_to_timeval);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Heiko Carstens5a62b192008-04-17 07:46:25 +020091void clock_comparator_work(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Heiko Carstens5a62b192008-04-17 07:46:25 +020093 struct clock_event_device *cd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Heiko Carstens5a62b192008-04-17 07:46:25 +020095 S390_lowcore.clock_comparator = -1ULL;
96 set_clock_comparator(S390_lowcore.clock_comparator);
97 cd = &__get_cpu_var(comparators);
98 cd->event_handler(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/*
Heiko Carstens5a62b192008-04-17 07:46:25 +0200102 * Fixup the clock comparator.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
Heiko Carstens5a62b192008-04-17 07:46:25 +0200104static void fixup_clock_comparator(unsigned long long delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Heiko Carstens5a62b192008-04-17 07:46:25 +0200106 /* If nobody is waiting there's nothing to fix. */
107 if (S390_lowcore.clock_comparator == -1ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200109 S390_lowcore.clock_comparator += delta;
110 set_clock_comparator(S390_lowcore.clock_comparator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Martin Schwidefsky4f37a682011-08-23 15:29:44 +0200113static int s390_next_ktime(ktime_t expires,
Heiko Carstens5a62b192008-04-17 07:46:25 +0200114 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Heiko Carstense35f95b2011-09-22 09:19:17 +0200116 u64 nsecs;
Martin Schwidefsky4f37a682011-08-23 15:29:44 +0200117
118 nsecs = ktime_to_ns(ktime_sub(expires, ktime_get_monotonic_offset()));
119 do_div(nsecs, 125);
120 S390_lowcore.clock_comparator = TOD_UNIX_EPOCH + (nsecs << 9);
Heiko Carstens5a62b192008-04-17 07:46:25 +0200121 set_clock_comparator(S390_lowcore.clock_comparator);
122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Heiko Carstens5a62b192008-04-17 07:46:25 +0200125static void s390_set_mode(enum clock_event_mode mode,
126 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100128}
129
130/*
131 * Set up lowcore and control register of the current cpu to
132 * enable TOD clock and clock comparator interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
134void init_cpu_timer(void)
135{
Heiko Carstens5a62b192008-04-17 07:46:25 +0200136 struct clock_event_device *cd;
137 int cpu;
138
139 S390_lowcore.clock_comparator = -1ULL;
140 set_clock_comparator(S390_lowcore.clock_comparator);
141
142 cpu = smp_processor_id();
143 cd = &per_cpu(comparators, cpu);
144 cd->name = "comparator";
Martin Schwidefsky4f37a682011-08-23 15:29:44 +0200145 cd->features = CLOCK_EVT_FEAT_ONESHOT |
146 CLOCK_EVT_FEAT_KTIME;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200147 cd->mult = 16777;
148 cd->shift = 12;
149 cd->min_delta_ns = 1;
150 cd->max_delta_ns = LONG_MAX;
151 cd->rating = 400;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030152 cd->cpumask = cpumask_of(cpu);
Martin Schwidefsky4f37a682011-08-23 15:29:44 +0200153 cd->set_next_ktime = s390_next_ktime;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200154 cd->set_mode = s390_set_mode;
155
156 clockevents_register_device(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100158 /* Enable clock comparator timer interrupt. */
159 __ctl_set_bit(0,11);
160
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200161 /* Always allow the timing alert external interrupt. */
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100162 __ctl_set_bit(0, 4);
163}
164
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200165static void clock_comparator_interrupt(unsigned int ext_int_code,
166 unsigned int param32,
167 unsigned long param64)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100168{
Heiko Carstens052ff462011-01-05 12:47:28 +0100169 kstat_cpu(smp_processor_id()).irqs[EXTINT_CLK]++;
Heiko Carstensd3d238c2008-10-03 21:54:59 +0200170 if (S390_lowcore.clock_comparator == -1ULL)
171 set_clock_comparator(S390_lowcore.clock_comparator);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100172}
173
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200174static void etr_timing_alert(struct etr_irq_parm *);
175static void stp_timing_alert(struct stp_irq_parm *);
176
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200177static void timing_alert_interrupt(unsigned int ext_int_code,
178 unsigned int param32, unsigned long param64)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200179{
Heiko Carstens052ff462011-01-05 12:47:28 +0100180 kstat_cpu(smp_processor_id()).irqs[EXTINT_TLA]++;
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200181 if (param32 & 0x00c40000)
182 etr_timing_alert((struct etr_irq_parm *) &param32);
183 if (param32 & 0x00038000)
184 stp_timing_alert((struct stp_irq_parm *) &param32);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200185}
186
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100187static void etr_reset(void);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200188static void stp_reset(void);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100189
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200190void read_persistent_clock(struct timespec *ts)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100191{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200192 tod_to_timeval(get_clock() - TOD_UNIX_EPOCH, ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200195void read_boot_clock(struct timespec *ts)
196{
197 tod_to_timeval(sched_clock_base_cc - TOD_UNIX_EPOCH, ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Magnus Damm8e196082009-04-21 12:24:00 -0700200static cycle_t read_tod_clock(struct clocksource *cs)
Martin Schwidefskydc64bef2006-10-06 16:38:48 +0200201{
202 return get_clock();
203}
204
205static struct clocksource clocksource_tod = {
206 .name = "tod",
Christian Borntraegerd2cb0e62007-11-05 11:10:14 +0100207 .rating = 400,
Martin Schwidefskydc64bef2006-10-06 16:38:48 +0200208 .read = read_tod_clock,
209 .mask = -1ULL,
210 .mult = 1000,
211 .shift = 12,
Thomas Gleixnercc02d802007-02-16 01:27:39 -0800212 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Martin Schwidefskydc64bef2006-10-06 16:38:48 +0200213};
214
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200215struct clocksource * __init clocksource_default_clock(void)
216{
217 return &clocksource_tod;
218}
Martin Schwidefskydc64bef2006-10-06 16:38:48 +0200219
John Stultz76158562010-07-13 17:56:23 -0700220void update_vsyscall(struct timespec *wall_time, struct timespec *wtm,
221 struct clocksource *clock, u32 mult)
Martin Schwidefskyb0206322008-12-25 13:38:36 +0100222{
223 if (clock != &clocksource_tod)
224 return;
225
226 /* Make userspace gettimeofday spin until we're done. */
227 ++vdso_data->tb_update_count;
228 smp_wmb();
229 vdso_data->xtime_tod_stamp = clock->cycle_last;
John Stultzb1e2ba82010-03-08 12:25:19 +0100230 vdso_data->xtime_clock_sec = wall_time->tv_sec;
231 vdso_data->xtime_clock_nsec = wall_time->tv_nsec;
John Stultz76158562010-07-13 17:56:23 -0700232 vdso_data->wtom_clock_sec = wtm->tv_sec;
233 vdso_data->wtom_clock_nsec = wtm->tv_nsec;
Hendrik Brueckner157a1a22010-04-22 17:17:06 +0200234 vdso_data->ntp_mult = mult;
Martin Schwidefskyb0206322008-12-25 13:38:36 +0100235 smp_wmb();
236 ++vdso_data->tb_update_count;
237}
238
239extern struct timezone sys_tz;
240
241void update_vsyscall_tz(void)
242{
243 /* Make userspace gettimeofday spin until we're done. */
244 ++vdso_data->tb_update_count;
245 smp_wmb();
246 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
247 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
248 smp_wmb();
249 ++vdso_data->tb_update_count;
250}
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252/*
253 * Initialize the TOD clock and the CPU timer of
254 * the boot cpu.
255 */
256void __init time_init(void)
257{
Martin Schwidefskyb6112cc2009-04-14 15:36:28 +0200258 /* Reset time synchronization interfaces. */
259 etr_reset();
260 stp_reset();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 /* request the clock comparator external interrupt */
Heiko Carstensd7d11042009-06-22 12:08:03 +0200263 if (register_external_interrupt(0x1004, clock_comparator_interrupt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 panic("Couldn't request external interrupt 0x1004");
265
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200266 /* request the timing alert external interrupt */
Heiko Carstensd7d11042009-06-22 12:08:03 +0200267 if (register_external_interrupt(0x1406, timing_alert_interrupt))
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100268 panic("Couldn't request external interrupt 0x1406");
269
Martin Schwidefskyab96e792009-04-14 15:36:29 +0200270 if (clocksource_register(&clocksource_tod) != 0)
271 panic("Could not register TOD clock source");
272
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100273 /* Enable TOD clock interrupts on the boot cpu. */
274 init_cpu_timer();
Martin Schwidefskyab96e792009-04-14 15:36:29 +0200275
Martin Schwidefskyc185b782008-12-25 13:39:25 +0100276 /* Enable cpu timer interrupts on the boot cpu. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 vtime_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100280/*
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200281 * The time is "clock". old is what we think the time is.
282 * Adjust the value by a multiple of jiffies and add the delta to ntp.
283 * "delay" is an approximation how long the synchronization took. If
284 * the time correction is positive, then "delay" is subtracted from
285 * the time difference and only the remaining part is passed to ntp.
286 */
287static unsigned long long adjust_time(unsigned long long old,
288 unsigned long long clock,
289 unsigned long long delay)
290{
291 unsigned long long delta, ticks;
292 struct timex adjust;
293
294 if (clock > old) {
295 /* It is later than we thought. */
296 delta = ticks = clock - old;
297 delta = ticks = (delta < delay) ? 0 : delta - delay;
298 delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
299 adjust.offset = ticks * (1000000 / HZ);
300 } else {
301 /* It is earlier than we thought. */
302 delta = ticks = old - clock;
303 delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
304 delta = -delta;
305 adjust.offset = -ticks * (1000000 / HZ);
306 }
Christian Borntraeger8107d822008-11-27 11:05:56 +0100307 sched_clock_base_cc += delta;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200308 if (adjust.offset != 0) {
Martin Schwidefskyfeab6502008-12-25 13:39:38 +0100309 pr_notice("The ETR interface has adjusted the clock "
310 "by %li microseconds\n", adjust.offset);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200311 adjust.modes = ADJ_OFFSET_SINGLESHOT;
312 do_adjtimex(&adjust);
313 }
314 return delta;
315}
316
317static DEFINE_PER_CPU(atomic_t, clock_sync_word);
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100318static DEFINE_MUTEX(clock_sync_mutex);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200319static unsigned long clock_sync_flags;
320
321#define CLOCK_SYNC_HAS_ETR 0
322#define CLOCK_SYNC_HAS_STP 1
323#define CLOCK_SYNC_ETR 2
324#define CLOCK_SYNC_STP 3
325
326/*
327 * The synchronous get_clock function. It will write the current clock
328 * value to the clock pointer and return 0 if the clock is in sync with
329 * the external time source. If the clock mode is local it will return
330 * -ENOSYS and -EAGAIN if the clock is not in sync with the external
331 * reference.
332 */
333int get_sync_clock(unsigned long long *clock)
334{
335 atomic_t *sw_ptr;
336 unsigned int sw0, sw1;
337
338 sw_ptr = &get_cpu_var(clock_sync_word);
339 sw0 = atomic_read(sw_ptr);
340 *clock = get_clock();
341 sw1 = atomic_read(sw_ptr);
Heiko Carstensbd119ee2009-12-07 12:52:14 +0100342 put_cpu_var(clock_sync_word);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200343 if (sw0 == sw1 && (sw0 & 0x80000000U))
344 /* Success: time is in sync. */
345 return 0;
346 if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags) &&
347 !test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
348 return -ENOSYS;
349 if (!test_bit(CLOCK_SYNC_ETR, &clock_sync_flags) &&
350 !test_bit(CLOCK_SYNC_STP, &clock_sync_flags))
351 return -EACCES;
352 return -EAGAIN;
353}
354EXPORT_SYMBOL(get_sync_clock);
355
356/*
357 * Make get_sync_clock return -EAGAIN.
358 */
359static void disable_sync_clock(void *dummy)
360{
361 atomic_t *sw_ptr = &__get_cpu_var(clock_sync_word);
362 /*
363 * Clear the in-sync bit 2^31. All get_sync_clock calls will
364 * fail until the sync bit is turned back on. In addition
365 * increase the "sequence" counter to avoid the race of an
366 * etr event and the complete recovery against get_sync_clock.
367 */
368 atomic_clear_mask(0x80000000, sw_ptr);
369 atomic_inc(sw_ptr);
370}
371
372/*
373 * Make get_sync_clock return 0 again.
374 * Needs to be called from a context disabled for preemption.
375 */
376static void enable_sync_clock(void)
377{
378 atomic_t *sw_ptr = &__get_cpu_var(clock_sync_word);
379 atomic_set_mask(0x80000000, sw_ptr);
380}
381
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100382/*
383 * Function to check if the clock is in sync.
384 */
385static inline int check_sync_clock(void)
386{
387 atomic_t *sw_ptr;
388 int rc;
389
390 sw_ptr = &get_cpu_var(clock_sync_word);
391 rc = (atomic_read(sw_ptr) & 0x80000000U) != 0;
Heiko Carstensbd119ee2009-12-07 12:52:14 +0100392 put_cpu_var(clock_sync_word);
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100393 return rc;
394}
395
Heiko Carstens750887d2008-12-25 13:38:37 +0100396/* Single threaded workqueue used for etr and stp sync events */
397static struct workqueue_struct *time_sync_wq;
398
399static void __init time_init_wq(void)
400{
Heiko Carstens179cb812009-01-23 16:40:26 +0100401 if (time_sync_wq)
402 return;
403 time_sync_wq = create_singlethread_workqueue("timesync");
Heiko Carstens750887d2008-12-25 13:38:37 +0100404}
405
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200406/*
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100407 * External Time Reference (ETR) code.
408 */
409static int etr_port0_online;
410static int etr_port1_online;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200411static int etr_steai_available;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100412
413static int __init early_parse_etr(char *p)
414{
415 if (strncmp(p, "off", 3) == 0)
416 etr_port0_online = etr_port1_online = 0;
417 else if (strncmp(p, "port0", 5) == 0)
418 etr_port0_online = 1;
419 else if (strncmp(p, "port1", 5) == 0)
420 etr_port1_online = 1;
421 else if (strncmp(p, "on", 2) == 0)
422 etr_port0_online = etr_port1_online = 1;
423 return 0;
424}
425early_param("etr", early_parse_etr);
426
427enum etr_event {
428 ETR_EVENT_PORT0_CHANGE,
429 ETR_EVENT_PORT1_CHANGE,
430 ETR_EVENT_PORT_ALERT,
431 ETR_EVENT_SYNC_CHECK,
432 ETR_EVENT_SWITCH_LOCAL,
433 ETR_EVENT_UPDATE,
434};
435
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100436/*
437 * Valid bit combinations of the eacr register are (x = don't care):
438 * e0 e1 dp p0 p1 ea es sl
439 * 0 0 x 0 0 0 0 0 initial, disabled state
440 * 0 0 x 0 1 1 0 0 port 1 online
441 * 0 0 x 1 0 1 0 0 port 0 online
442 * 0 0 x 1 1 1 0 0 both ports online
443 * 0 1 x 0 1 1 0 0 port 1 online and usable, ETR or PPS mode
444 * 0 1 x 0 1 1 0 1 port 1 online, usable and ETR mode
445 * 0 1 x 0 1 1 1 0 port 1 online, usable, PPS mode, in-sync
446 * 0 1 x 0 1 1 1 1 port 1 online, usable, ETR mode, in-sync
447 * 0 1 x 1 1 1 0 0 both ports online, port 1 usable
448 * 0 1 x 1 1 1 1 0 both ports online, port 1 usable, PPS mode, in-sync
449 * 0 1 x 1 1 1 1 1 both ports online, port 1 usable, ETR mode, in-sync
450 * 1 0 x 1 0 1 0 0 port 0 online and usable, ETR or PPS mode
451 * 1 0 x 1 0 1 0 1 port 0 online, usable and ETR mode
452 * 1 0 x 1 0 1 1 0 port 0 online, usable, PPS mode, in-sync
453 * 1 0 x 1 0 1 1 1 port 0 online, usable, ETR mode, in-sync
454 * 1 0 x 1 1 1 0 0 both ports online, port 0 usable
455 * 1 0 x 1 1 1 1 0 both ports online, port 0 usable, PPS mode, in-sync
456 * 1 0 x 1 1 1 1 1 both ports online, port 0 usable, ETR mode, in-sync
457 * 1 1 x 1 1 1 1 0 both ports online & usable, ETR, in-sync
458 * 1 1 x 1 1 1 1 1 both ports online & usable, ETR, in-sync
459 */
460static struct etr_eacr etr_eacr;
461static u64 etr_tolec; /* time of last eacr update */
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100462static struct etr_aib etr_port0;
463static int etr_port0_uptodate;
464static struct etr_aib etr_port1;
465static int etr_port1_uptodate;
466static unsigned long etr_events;
467static struct timer_list etr_timer;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100468
469static void etr_timeout(unsigned long dummy);
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200470static void etr_work_fn(struct work_struct *work);
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +0100471static DEFINE_MUTEX(etr_work_mutex);
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200472static DECLARE_WORK(etr_work, etr_work_fn);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100473
474/*
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100475 * Reset ETR attachment.
476 */
477static void etr_reset(void)
478{
479 etr_eacr = (struct etr_eacr) {
480 .e0 = 0, .e1 = 0, ._pad0 = 4, .dp = 0,
481 .p0 = 0, .p1 = 0, ._pad1 = 0, .ea = 0,
482 .es = 0, .sl = 0 };
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200483 if (etr_setr(&etr_eacr) == 0) {
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100484 etr_tolec = get_clock();
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200485 set_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags);
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100486 if (etr_port0_online && etr_port1_online)
487 set_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200488 } else if (etr_port0_online || etr_port1_online) {
Martin Schwidefskyfeab6502008-12-25 13:39:38 +0100489 pr_warning("The real or virtual hardware system does "
490 "not provide an ETR interface\n");
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200491 etr_port0_online = etr_port1_online = 0;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100492 }
493}
494
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200495static int __init etr_init(void)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100496{
497 struct etr_aib aib;
498
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200499 if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags))
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200500 return 0;
Heiko Carstens750887d2008-12-25 13:38:37 +0100501 time_init_wq();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100502 /* Check if this machine has the steai instruction. */
503 if (etr_steai(&aib, ETR_STEAI_STEPPING_PORT) == 0)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200504 etr_steai_available = 1;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100505 setup_timer(&etr_timer, etr_timeout, 0UL);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100506 if (etr_port0_online) {
507 set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
Heiko Carstens750887d2008-12-25 13:38:37 +0100508 queue_work(time_sync_wq, &etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100509 }
510 if (etr_port1_online) {
511 set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
Heiko Carstens750887d2008-12-25 13:38:37 +0100512 queue_work(time_sync_wq, &etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100513 }
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200514 return 0;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100515}
516
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200517arch_initcall(etr_init);
518
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100519/*
520 * Two sorts of ETR machine checks. The architecture reads:
521 * "When a machine-check niterruption occurs and if a switch-to-local or
522 * ETR-sync-check interrupt request is pending but disabled, this pending
523 * disabled interruption request is indicated and is cleared".
524 * Which means that we can get etr_switch_to_local events from the machine
525 * check handler although the interruption condition is disabled. Lovely..
526 */
527
528/*
529 * Switch to local machine check. This is called when the last usable
530 * ETR port goes inactive. After switch to local the clock is not in sync.
531 */
532void etr_switch_to_local(void)
533{
534 if (!etr_eacr.sl)
535 return;
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100536 disable_sync_clock(NULL);
Martin Schwidefsky33fea792010-07-27 19:29:38 +0200537 if (!test_and_set_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events)) {
538 etr_eacr.es = etr_eacr.sl = 0;
539 etr_setr(&etr_eacr);
540 queue_work(time_sync_wq, &etr_work);
541 }
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100542}
543
544/*
545 * ETR sync check machine check. This is called when the ETR OTE and the
546 * local clock OTE are farther apart than the ETR sync check tolerance.
547 * After a ETR sync check the clock is not in sync. The machine check
548 * is broadcasted to all cpus at the same time.
549 */
550void etr_sync_check(void)
551{
552 if (!etr_eacr.es)
553 return;
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100554 disable_sync_clock(NULL);
Martin Schwidefsky33fea792010-07-27 19:29:38 +0200555 if (!test_and_set_bit(ETR_EVENT_SYNC_CHECK, &etr_events)) {
556 etr_eacr.es = 0;
557 etr_setr(&etr_eacr);
558 queue_work(time_sync_wq, &etr_work);
559 }
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100560}
561
562/*
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200563 * ETR timing alert. There are two causes:
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100564 * 1) port state change, check the usability of the port
565 * 2) port alert, one of the ETR-data-validity bits (v1-v2 bits of the
566 * sldr-status word) or ETR-data word 1 (edf1) or ETR-data word 3 (edf3)
567 * or ETR-data word 4 (edf4) has changed.
568 */
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200569static void etr_timing_alert(struct etr_irq_parm *intparm)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100570{
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100571 if (intparm->pc0)
572 /* ETR port 0 state change. */
573 set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
574 if (intparm->pc1)
575 /* ETR port 1 state change. */
576 set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
577 if (intparm->eai)
578 /*
579 * ETR port alert on either port 0, 1 or both.
580 * Both ports are not up-to-date now.
581 */
582 set_bit(ETR_EVENT_PORT_ALERT, &etr_events);
Heiko Carstens750887d2008-12-25 13:38:37 +0100583 queue_work(time_sync_wq, &etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100584}
585
586static void etr_timeout(unsigned long dummy)
587{
588 set_bit(ETR_EVENT_UPDATE, &etr_events);
Heiko Carstens750887d2008-12-25 13:38:37 +0100589 queue_work(time_sync_wq, &etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100590}
591
592/*
593 * Check if the etr mode is pss.
594 */
595static inline int etr_mode_is_pps(struct etr_eacr eacr)
596{
597 return eacr.es && !eacr.sl;
598}
599
600/*
601 * Check if the etr mode is etr.
602 */
603static inline int etr_mode_is_etr(struct etr_eacr eacr)
604{
605 return eacr.es && eacr.sl;
606}
607
608/*
609 * Check if the port can be used for TOD synchronization.
610 * For PPS mode the port has to receive OTEs. For ETR mode
611 * the port has to receive OTEs, the ETR stepping bit has to
612 * be zero and the validity bits for data frame 1, 2, and 3
613 * have to be 1.
614 */
615static int etr_port_valid(struct etr_aib *aib, int port)
616{
617 unsigned int psc;
618
619 /* Check that this port is receiving OTEs. */
620 if (aib->tsp == 0)
621 return 0;
622
623 psc = port ? aib->esw.psc1 : aib->esw.psc0;
624 if (psc == etr_lpsc_pps_mode)
625 return 1;
626 if (psc == etr_lpsc_operational_step)
627 return !aib->esw.y && aib->slsw.v1 &&
628 aib->slsw.v2 && aib->slsw.v3;
629 return 0;
630}
631
632/*
633 * Check if two ports are on the same network.
634 */
635static int etr_compare_network(struct etr_aib *aib1, struct etr_aib *aib2)
636{
637 // FIXME: any other fields we have to compare?
638 return aib1->edf1.net_id == aib2->edf1.net_id;
639}
640
641/*
642 * Wrapper for etr_stei that converts physical port states
643 * to logical port states to be consistent with the output
644 * of stetr (see etr_psc vs. etr_lpsc).
645 */
646static void etr_steai_cv(struct etr_aib *aib, unsigned int func)
647{
648 BUG_ON(etr_steai(aib, func) != 0);
649 /* Convert port state to logical port state. */
650 if (aib->esw.psc0 == 1)
651 aib->esw.psc0 = 2;
652 else if (aib->esw.psc0 == 0 && aib->esw.p == 0)
653 aib->esw.psc0 = 1;
654 if (aib->esw.psc1 == 1)
655 aib->esw.psc1 = 2;
656 else if (aib->esw.psc1 == 0 && aib->esw.p == 1)
657 aib->esw.psc1 = 1;
658}
659
660/*
661 * Check if the aib a2 is still connected to the same attachment as
662 * aib a1, the etv values differ by one and a2 is valid.
663 */
664static int etr_aib_follows(struct etr_aib *a1, struct etr_aib *a2, int p)
665{
666 int state_a1, state_a2;
667
668 /* Paranoia check: e0/e1 should better be the same. */
669 if (a1->esw.eacr.e0 != a2->esw.eacr.e0 ||
670 a1->esw.eacr.e1 != a2->esw.eacr.e1)
671 return 0;
672
673 /* Still connected to the same etr ? */
674 state_a1 = p ? a1->esw.psc1 : a1->esw.psc0;
675 state_a2 = p ? a2->esw.psc1 : a2->esw.psc0;
676 if (state_a1 == etr_lpsc_operational_step) {
677 if (state_a2 != etr_lpsc_operational_step ||
678 a1->edf1.net_id != a2->edf1.net_id ||
679 a1->edf1.etr_id != a2->edf1.etr_id ||
680 a1->edf1.etr_pn != a2->edf1.etr_pn)
681 return 0;
682 } else if (state_a2 != etr_lpsc_pps_mode)
683 return 0;
684
685 /* The ETV value of a2 needs to be ETV of a1 + 1. */
686 if (a1->edf2.etv + 1 != a2->edf2.etv)
687 return 0;
688
689 if (!etr_port_valid(a2, p))
690 return 0;
691
692 return 1;
693}
694
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200695struct clock_sync_data {
Heiko Carstens750887d2008-12-25 13:38:37 +0100696 atomic_t cpus;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200697 int in_sync;
698 unsigned long long fixup_cc;
Heiko Carstens750887d2008-12-25 13:38:37 +0100699 int etr_port;
700 struct etr_aib *etr_aib;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200701};
Heiko Carstens5a62b192008-04-17 07:46:25 +0200702
Heiko Carstens750887d2008-12-25 13:38:37 +0100703static void clock_sync_cpu(struct clock_sync_data *sync)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100704{
Heiko Carstens750887d2008-12-25 13:38:37 +0100705 atomic_dec(&sync->cpus);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200706 enable_sync_clock();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100707 /*
708 * This looks like a busy wait loop but it isn't. etr_sync_cpus
709 * is called on all other cpus while the TOD clocks is stopped.
710 * __udelay will stop the cpu on an enabled wait psw until the
711 * TOD is running again.
712 */
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200713 while (sync->in_sync == 0) {
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100714 __udelay(1);
Heiko Carstens6c732de2007-02-21 10:55:15 +0100715 /*
716 * A different cpu changes *in_sync. Therefore use
717 * barrier() to force memory access.
718 */
719 barrier();
720 }
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200721 if (sync->in_sync != 1)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100722 /* Didn't work. Clear per-cpu in sync bit again. */
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200723 disable_sync_clock(NULL);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100724 /*
725 * This round of TOD syncing is done. Set the clock comparator
726 * to the next tick and let the processor continue.
727 */
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200728 fixup_clock_comparator(sync->fixup_cc);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100729}
730
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100731/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300732 * Sync the TOD clock using the port referred to by aibp. This port
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100733 * has to be enabled and the other port has to be disabled. The
734 * last eacr update has to be more than 1.6 seconds in the past.
735 */
Heiko Carstens750887d2008-12-25 13:38:37 +0100736static int etr_sync_clock(void *data)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100737{
Heiko Carstens750887d2008-12-25 13:38:37 +0100738 static int first;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200739 unsigned long long clock, old_clock, delay, delta;
Heiko Carstens750887d2008-12-25 13:38:37 +0100740 struct clock_sync_data *etr_sync;
741 struct etr_aib *sync_port, *aib;
742 int port;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100743 int rc;
744
Heiko Carstens750887d2008-12-25 13:38:37 +0100745 etr_sync = data;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100746
Heiko Carstens750887d2008-12-25 13:38:37 +0100747 if (xchg(&first, 1) == 1) {
748 /* Slave */
749 clock_sync_cpu(etr_sync);
750 return 0;
751 }
752
753 /* Wait until all other cpus entered the sync function. */
754 while (atomic_read(&etr_sync->cpus) != 0)
755 cpu_relax();
756
757 port = etr_sync->etr_port;
758 aib = etr_sync->etr_aib;
759 sync_port = (port == 0) ? &etr_port0 : &etr_port1;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200760 enable_sync_clock();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100761
762 /* Set clock to next OTE. */
763 __ctl_set_bit(14, 21);
764 __ctl_set_bit(0, 29);
765 clock = ((unsigned long long) (aib->edf2.etv + 1)) << 32;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200766 old_clock = get_clock();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100767 if (set_clock(clock) == 0) {
768 __udelay(1); /* Wait for the clock to start. */
769 __ctl_clear_bit(0, 29);
770 __ctl_clear_bit(14, 21);
771 etr_stetr(aib);
772 /* Adjust Linux timing variables. */
773 delay = (unsigned long long)
774 (aib->edf2.etv - sync_port->edf2.etv) << 32;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200775 delta = adjust_time(old_clock, clock, delay);
Heiko Carstens750887d2008-12-25 13:38:37 +0100776 etr_sync->fixup_cc = delta;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200777 fixup_clock_comparator(delta);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100778 /* Verify that the clock is properly set. */
779 if (!etr_aib_follows(sync_port, aib, port)) {
780 /* Didn't work. */
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200781 disable_sync_clock(NULL);
Heiko Carstens750887d2008-12-25 13:38:37 +0100782 etr_sync->in_sync = -EAGAIN;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100783 rc = -EAGAIN;
784 } else {
Heiko Carstens750887d2008-12-25 13:38:37 +0100785 etr_sync->in_sync = 1;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100786 rc = 0;
787 }
788 } else {
789 /* Could not set the clock ?!? */
790 __ctl_clear_bit(0, 29);
791 __ctl_clear_bit(14, 21);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200792 disable_sync_clock(NULL);
Heiko Carstens750887d2008-12-25 13:38:37 +0100793 etr_sync->in_sync = -EAGAIN;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100794 rc = -EAGAIN;
795 }
Heiko Carstens750887d2008-12-25 13:38:37 +0100796 xchg(&first, 0);
797 return rc;
798}
799
800static int etr_sync_clock_stop(struct etr_aib *aib, int port)
801{
802 struct clock_sync_data etr_sync;
803 struct etr_aib *sync_port;
804 int follows;
805 int rc;
806
807 /* Check if the current aib is adjacent to the sync port aib. */
808 sync_port = (port == 0) ? &etr_port0 : &etr_port1;
809 follows = etr_aib_follows(sync_port, aib, port);
810 memcpy(sync_port, aib, sizeof(*aib));
811 if (!follows)
812 return -EAGAIN;
813 memset(&etr_sync, 0, sizeof(etr_sync));
814 etr_sync.etr_aib = aib;
815 etr_sync.etr_port = port;
816 get_online_cpus();
817 atomic_set(&etr_sync.cpus, num_online_cpus() - 1);
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200818 rc = stop_machine(etr_sync_clock, &etr_sync, cpu_online_mask);
Heiko Carstens750887d2008-12-25 13:38:37 +0100819 put_online_cpus();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100820 return rc;
821}
822
823/*
824 * Handle the immediate effects of the different events.
825 * The port change event is used for online/offline changes.
826 */
827static struct etr_eacr etr_handle_events(struct etr_eacr eacr)
828{
829 if (test_and_clear_bit(ETR_EVENT_SYNC_CHECK, &etr_events))
830 eacr.es = 0;
831 if (test_and_clear_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events))
832 eacr.es = eacr.sl = 0;
833 if (test_and_clear_bit(ETR_EVENT_PORT_ALERT, &etr_events))
834 etr_port0_uptodate = etr_port1_uptodate = 0;
835
836 if (test_and_clear_bit(ETR_EVENT_PORT0_CHANGE, &etr_events)) {
837 if (eacr.e0)
838 /*
839 * Port change of an enabled port. We have to
840 * assume that this can have caused an stepping
841 * port switch.
842 */
843 etr_tolec = get_clock();
844 eacr.p0 = etr_port0_online;
845 if (!eacr.p0)
846 eacr.e0 = 0;
847 etr_port0_uptodate = 0;
848 }
849 if (test_and_clear_bit(ETR_EVENT_PORT1_CHANGE, &etr_events)) {
850 if (eacr.e1)
851 /*
852 * Port change of an enabled port. We have to
853 * assume that this can have caused an stepping
854 * port switch.
855 */
856 etr_tolec = get_clock();
857 eacr.p1 = etr_port1_online;
858 if (!eacr.p1)
859 eacr.e1 = 0;
860 etr_port1_uptodate = 0;
861 }
862 clear_bit(ETR_EVENT_UPDATE, &etr_events);
863 return eacr;
864}
865
866/*
867 * Set up a timer that expires after the etr_tolec + 1.6 seconds if
868 * one of the ports needs an update.
869 */
870static void etr_set_tolec_timeout(unsigned long long now)
871{
872 unsigned long micros;
873
874 if ((!etr_eacr.p0 || etr_port0_uptodate) &&
875 (!etr_eacr.p1 || etr_port1_uptodate))
876 return;
877 micros = (now > etr_tolec) ? ((now - etr_tolec) >> 12) : 0;
878 micros = (micros > 1600000) ? 0 : 1600000 - micros;
879 mod_timer(&etr_timer, jiffies + (micros * HZ) / 1000000 + 1);
880}
881
882/*
883 * Set up a time that expires after 1/2 second.
884 */
885static void etr_set_sync_timeout(void)
886{
887 mod_timer(&etr_timer, jiffies + HZ/2);
888}
889
890/*
891 * Update the aib information for one or both ports.
892 */
893static struct etr_eacr etr_handle_update(struct etr_aib *aib,
894 struct etr_eacr eacr)
895{
896 /* With both ports disabled the aib information is useless. */
897 if (!eacr.e0 && !eacr.e1)
898 return eacr;
899
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200900 /* Update port0 or port1 with aib stored in etr_work_fn. */
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100901 if (aib->esw.q == 0) {
902 /* Information for port 0 stored. */
903 if (eacr.p0 && !etr_port0_uptodate) {
904 etr_port0 = *aib;
905 if (etr_port0_online)
906 etr_port0_uptodate = 1;
907 }
908 } else {
909 /* Information for port 1 stored. */
910 if (eacr.p1 && !etr_port1_uptodate) {
911 etr_port1 = *aib;
912 if (etr_port0_online)
913 etr_port1_uptodate = 1;
914 }
915 }
916
917 /*
918 * Do not try to get the alternate port aib if the clock
919 * is not in sync yet.
920 */
Martin Schwidefsky33fea792010-07-27 19:29:38 +0200921 if (!eacr.es || !check_sync_clock())
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100922 return eacr;
923
924 /*
925 * If steai is available we can get the information about
926 * the other port immediately. If only stetr is available the
927 * data-port bit toggle has to be used.
928 */
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200929 if (etr_steai_available) {
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100930 if (eacr.p0 && !etr_port0_uptodate) {
931 etr_steai_cv(&etr_port0, ETR_STEAI_PORT_0);
932 etr_port0_uptodate = 1;
933 }
934 if (eacr.p1 && !etr_port1_uptodate) {
935 etr_steai_cv(&etr_port1, ETR_STEAI_PORT_1);
936 etr_port1_uptodate = 1;
937 }
938 } else {
939 /*
940 * One port was updated above, if the other
941 * port is not uptodate toggle dp bit.
942 */
943 if ((eacr.p0 && !etr_port0_uptodate) ||
944 (eacr.p1 && !etr_port1_uptodate))
945 eacr.dp ^= 1;
946 else
947 eacr.dp = 0;
948 }
949 return eacr;
950}
951
952/*
953 * Write new etr control register if it differs from the current one.
954 * Return 1 if etr_tolec has been updated as well.
955 */
956static void etr_update_eacr(struct etr_eacr eacr)
957{
958 int dp_changed;
959
960 if (memcmp(&etr_eacr, &eacr, sizeof(eacr)) == 0)
961 /* No change, return. */
962 return;
963 /*
964 * The disable of an active port of the change of the data port
965 * bit can/will cause a change in the data port.
966 */
967 dp_changed = etr_eacr.e0 > eacr.e0 || etr_eacr.e1 > eacr.e1 ||
968 (etr_eacr.dp ^ eacr.dp) != 0;
969 etr_eacr = eacr;
970 etr_setr(&etr_eacr);
971 if (dp_changed)
972 etr_tolec = get_clock();
973}
974
975/*
Heiko Carstens750887d2008-12-25 13:38:37 +0100976 * ETR work. In this function you'll find the main logic. In
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100977 * particular this is the only function that calls etr_update_eacr(),
978 * it "controls" the etr control register.
979 */
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200980static void etr_work_fn(struct work_struct *work)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100981{
982 unsigned long long now;
983 struct etr_eacr eacr;
984 struct etr_aib aib;
985 int sync_port;
986
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +0100987 /* prevent multiple execution. */
988 mutex_lock(&etr_work_mutex);
989
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100990 /* Create working copy of etr_eacr. */
991 eacr = etr_eacr;
992
993 /* Check for the different events and their immediate effects. */
994 eacr = etr_handle_events(eacr);
995
996 /* Check if ETR is supposed to be active. */
997 eacr.ea = eacr.p0 || eacr.p1;
998 if (!eacr.ea) {
999 /* Both ports offline. Reset everything. */
1000 eacr.dp = eacr.es = eacr.sl = 0;
Ingo Molnar1a781a72008-07-15 21:55:59 +02001001 on_each_cpu(disable_sync_clock, NULL, 1);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001002 del_timer_sync(&etr_timer);
1003 etr_update_eacr(eacr);
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +01001004 goto out_unlock;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001005 }
1006
1007 /* Store aib to get the current ETR status word. */
1008 BUG_ON(etr_stetr(&aib) != 0);
1009 etr_port0.esw = etr_port1.esw = aib.esw; /* Copy status word. */
1010 now = get_clock();
1011
1012 /*
1013 * Update the port information if the last stepping port change
1014 * or data port change is older than 1.6 seconds.
1015 */
1016 if (now >= etr_tolec + (1600000 << 12))
1017 eacr = etr_handle_update(&aib, eacr);
1018
1019 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001020 * Select ports to enable. The preferred synchronization mode is PPS.
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001021 * If a port can be enabled depends on a number of things:
1022 * 1) The port needs to be online and uptodate. A port is not
1023 * disabled just because it is not uptodate, but it is only
1024 * enabled if it is uptodate.
1025 * 2) The port needs to have the same mode (pps / etr).
1026 * 3) The port needs to be usable -> etr_port_valid() == 1
1027 * 4) To enable the second port the clock needs to be in sync.
1028 * 5) If both ports are useable and are ETR ports, the network id
1029 * has to be the same.
1030 * The eacr.sl bit is used to indicate etr mode vs. pps mode.
1031 */
1032 if (eacr.p0 && aib.esw.psc0 == etr_lpsc_pps_mode) {
1033 eacr.sl = 0;
1034 eacr.e0 = 1;
1035 if (!etr_mode_is_pps(etr_eacr))
1036 eacr.es = 0;
1037 if (!eacr.es || !eacr.p1 || aib.esw.psc1 != etr_lpsc_pps_mode)
1038 eacr.e1 = 0;
1039 // FIXME: uptodate checks ?
1040 else if (etr_port0_uptodate && etr_port1_uptodate)
1041 eacr.e1 = 1;
1042 sync_port = (etr_port0_uptodate &&
1043 etr_port_valid(&etr_port0, 0)) ? 0 : -1;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001044 } else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_pps_mode) {
1045 eacr.sl = 0;
1046 eacr.e0 = 0;
1047 eacr.e1 = 1;
1048 if (!etr_mode_is_pps(etr_eacr))
1049 eacr.es = 0;
1050 sync_port = (etr_port1_uptodate &&
1051 etr_port_valid(&etr_port1, 1)) ? 1 : -1;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001052 } else if (eacr.p0 && aib.esw.psc0 == etr_lpsc_operational_step) {
1053 eacr.sl = 1;
1054 eacr.e0 = 1;
1055 if (!etr_mode_is_etr(etr_eacr))
1056 eacr.es = 0;
1057 if (!eacr.es || !eacr.p1 ||
1058 aib.esw.psc1 != etr_lpsc_operational_alt)
1059 eacr.e1 = 0;
1060 else if (etr_port0_uptodate && etr_port1_uptodate &&
1061 etr_compare_network(&etr_port0, &etr_port1))
1062 eacr.e1 = 1;
1063 sync_port = (etr_port0_uptodate &&
1064 etr_port_valid(&etr_port0, 0)) ? 0 : -1;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001065 } else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_operational_step) {
1066 eacr.sl = 1;
1067 eacr.e0 = 0;
1068 eacr.e1 = 1;
1069 if (!etr_mode_is_etr(etr_eacr))
1070 eacr.es = 0;
1071 sync_port = (etr_port1_uptodate &&
1072 etr_port_valid(&etr_port1, 1)) ? 1 : -1;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001073 } else {
1074 /* Both ports not usable. */
1075 eacr.es = eacr.sl = 0;
1076 sync_port = -1;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001077 }
1078
1079 /*
1080 * If the clock is in sync just update the eacr and return.
1081 * If there is no valid sync port wait for a port update.
1082 */
Martin Schwidefsky33fea792010-07-27 19:29:38 +02001083 if ((eacr.es && check_sync_clock()) || sync_port < 0) {
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001084 etr_update_eacr(eacr);
1085 etr_set_tolec_timeout(now);
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +01001086 goto out_unlock;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001087 }
1088
1089 /*
1090 * Prepare control register for clock syncing
1091 * (reset data port bit, set sync check control.
1092 */
1093 eacr.dp = 0;
1094 eacr.es = 1;
1095
1096 /*
1097 * Update eacr and try to synchronize the clock. If the update
1098 * of eacr caused a stepping port switch (or if we have to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001099 * assume that a stepping port switch has occurred) or the
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001100 * clock syncing failed, reset the sync check control bit
1101 * and set up a timer to try again after 0.5 seconds
1102 */
1103 etr_update_eacr(eacr);
1104 if (now < etr_tolec + (1600000 << 12) ||
Heiko Carstens750887d2008-12-25 13:38:37 +01001105 etr_sync_clock_stop(&aib, sync_port) != 0) {
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001106 /* Sync failed. Try again in 1/2 second. */
1107 eacr.es = 0;
1108 etr_update_eacr(eacr);
1109 etr_set_sync_timeout();
1110 } else
1111 etr_set_tolec_timeout(now);
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +01001112out_unlock:
1113 mutex_unlock(&etr_work_mutex);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001114}
1115
1116/*
1117 * Sysfs interface functions
1118 */
1119static struct sysdev_class etr_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001120 .name = "etr",
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001121};
1122
1123static struct sys_device etr_port0_dev = {
1124 .id = 0,
1125 .cls = &etr_sysclass,
1126};
1127
1128static struct sys_device etr_port1_dev = {
1129 .id = 1,
1130 .cls = &etr_sysclass,
1131};
1132
1133/*
1134 * ETR class attributes
1135 */
Andi Kleenc9be0a32010-01-05 12:47:58 +01001136static ssize_t etr_stepping_port_show(struct sysdev_class *class,
1137 struct sysdev_class_attribute *attr,
1138 char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001139{
1140 return sprintf(buf, "%i\n", etr_port0.esw.p);
1141}
1142
1143static SYSDEV_CLASS_ATTR(stepping_port, 0400, etr_stepping_port_show, NULL);
1144
Andi Kleenc9be0a32010-01-05 12:47:58 +01001145static ssize_t etr_stepping_mode_show(struct sysdev_class *class,
1146 struct sysdev_class_attribute *attr,
1147 char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001148{
1149 char *mode_str;
1150
1151 if (etr_mode_is_pps(etr_eacr))
1152 mode_str = "pps";
1153 else if (etr_mode_is_etr(etr_eacr))
1154 mode_str = "etr";
1155 else
1156 mode_str = "local";
1157 return sprintf(buf, "%s\n", mode_str);
1158}
1159
1160static SYSDEV_CLASS_ATTR(stepping_mode, 0400, etr_stepping_mode_show, NULL);
1161
1162/*
1163 * ETR port attributes
1164 */
1165static inline struct etr_aib *etr_aib_from_dev(struct sys_device *dev)
1166{
1167 if (dev == &etr_port0_dev)
1168 return etr_port0_online ? &etr_port0 : NULL;
1169 else
1170 return etr_port1_online ? &etr_port1 : NULL;
1171}
1172
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001173static ssize_t etr_online_show(struct sys_device *dev,
1174 struct sysdev_attribute *attr,
1175 char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001176{
1177 unsigned int online;
1178
1179 online = (dev == &etr_port0_dev) ? etr_port0_online : etr_port1_online;
1180 return sprintf(buf, "%i\n", online);
1181}
1182
1183static ssize_t etr_online_store(struct sys_device *dev,
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001184 struct sysdev_attribute *attr,
1185 const char *buf, size_t count)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001186{
1187 unsigned int value;
1188
1189 value = simple_strtoul(buf, NULL, 0);
1190 if (value != 0 && value != 1)
1191 return -EINVAL;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001192 if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags))
1193 return -EOPNOTSUPP;
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001194 mutex_lock(&clock_sync_mutex);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001195 if (dev == &etr_port0_dev) {
1196 if (etr_port0_online == value)
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001197 goto out; /* Nothing to do. */
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001198 etr_port0_online = value;
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001199 if (etr_port0_online && etr_port1_online)
1200 set_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
1201 else
1202 clear_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001203 set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
Heiko Carstens750887d2008-12-25 13:38:37 +01001204 queue_work(time_sync_wq, &etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001205 } else {
1206 if (etr_port1_online == value)
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001207 goto out; /* Nothing to do. */
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001208 etr_port1_online = value;
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001209 if (etr_port0_online && etr_port1_online)
1210 set_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
1211 else
1212 clear_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001213 set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
Heiko Carstens750887d2008-12-25 13:38:37 +01001214 queue_work(time_sync_wq, &etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001215 }
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001216out:
1217 mutex_unlock(&clock_sync_mutex);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001218 return count;
1219}
1220
1221static SYSDEV_ATTR(online, 0600, etr_online_show, etr_online_store);
1222
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001223static ssize_t etr_stepping_control_show(struct sys_device *dev,
1224 struct sysdev_attribute *attr,
1225 char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001226{
1227 return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
1228 etr_eacr.e0 : etr_eacr.e1);
1229}
1230
1231static SYSDEV_ATTR(stepping_control, 0400, etr_stepping_control_show, NULL);
1232
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001233static ssize_t etr_mode_code_show(struct sys_device *dev,
1234 struct sysdev_attribute *attr, char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001235{
1236 if (!etr_port0_online && !etr_port1_online)
1237 /* Status word is not uptodate if both ports are offline. */
1238 return -ENODATA;
1239 return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
1240 etr_port0.esw.psc0 : etr_port0.esw.psc1);
1241}
1242
1243static SYSDEV_ATTR(state_code, 0400, etr_mode_code_show, NULL);
1244
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001245static ssize_t etr_untuned_show(struct sys_device *dev,
1246 struct sysdev_attribute *attr, char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001247{
1248 struct etr_aib *aib = etr_aib_from_dev(dev);
1249
1250 if (!aib || !aib->slsw.v1)
1251 return -ENODATA;
1252 return sprintf(buf, "%i\n", aib->edf1.u);
1253}
1254
1255static SYSDEV_ATTR(untuned, 0400, etr_untuned_show, NULL);
1256
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001257static ssize_t etr_network_id_show(struct sys_device *dev,
1258 struct sysdev_attribute *attr, char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001259{
1260 struct etr_aib *aib = etr_aib_from_dev(dev);
1261
1262 if (!aib || !aib->slsw.v1)
1263 return -ENODATA;
1264 return sprintf(buf, "%i\n", aib->edf1.net_id);
1265}
1266
1267static SYSDEV_ATTR(network, 0400, etr_network_id_show, NULL);
1268
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001269static ssize_t etr_id_show(struct sys_device *dev,
1270 struct sysdev_attribute *attr, char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001271{
1272 struct etr_aib *aib = etr_aib_from_dev(dev);
1273
1274 if (!aib || !aib->slsw.v1)
1275 return -ENODATA;
1276 return sprintf(buf, "%i\n", aib->edf1.etr_id);
1277}
1278
1279static SYSDEV_ATTR(id, 0400, etr_id_show, NULL);
1280
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001281static ssize_t etr_port_number_show(struct sys_device *dev,
1282 struct sysdev_attribute *attr, char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001283{
1284 struct etr_aib *aib = etr_aib_from_dev(dev);
1285
1286 if (!aib || !aib->slsw.v1)
1287 return -ENODATA;
1288 return sprintf(buf, "%i\n", aib->edf1.etr_pn);
1289}
1290
1291static SYSDEV_ATTR(port, 0400, etr_port_number_show, NULL);
1292
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001293static ssize_t etr_coupled_show(struct sys_device *dev,
1294 struct sysdev_attribute *attr, char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001295{
1296 struct etr_aib *aib = etr_aib_from_dev(dev);
1297
1298 if (!aib || !aib->slsw.v3)
1299 return -ENODATA;
1300 return sprintf(buf, "%i\n", aib->edf3.c);
1301}
1302
1303static SYSDEV_ATTR(coupled, 0400, etr_coupled_show, NULL);
1304
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001305static ssize_t etr_local_time_show(struct sys_device *dev,
1306 struct sysdev_attribute *attr, char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001307{
1308 struct etr_aib *aib = etr_aib_from_dev(dev);
1309
1310 if (!aib || !aib->slsw.v3)
1311 return -ENODATA;
1312 return sprintf(buf, "%i\n", aib->edf3.blto);
1313}
1314
1315static SYSDEV_ATTR(local_time, 0400, etr_local_time_show, NULL);
1316
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001317static ssize_t etr_utc_offset_show(struct sys_device *dev,
1318 struct sysdev_attribute *attr, char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001319{
1320 struct etr_aib *aib = etr_aib_from_dev(dev);
1321
1322 if (!aib || !aib->slsw.v3)
1323 return -ENODATA;
1324 return sprintf(buf, "%i\n", aib->edf3.buo);
1325}
1326
1327static SYSDEV_ATTR(utc_offset, 0400, etr_utc_offset_show, NULL);
1328
1329static struct sysdev_attribute *etr_port_attributes[] = {
1330 &attr_online,
1331 &attr_stepping_control,
1332 &attr_state_code,
1333 &attr_untuned,
1334 &attr_network,
1335 &attr_id,
1336 &attr_port,
1337 &attr_coupled,
1338 &attr_local_time,
1339 &attr_utc_offset,
1340 NULL
1341};
1342
1343static int __init etr_register_port(struct sys_device *dev)
1344{
1345 struct sysdev_attribute **attr;
1346 int rc;
1347
1348 rc = sysdev_register(dev);
1349 if (rc)
1350 goto out;
1351 for (attr = etr_port_attributes; *attr; attr++) {
1352 rc = sysdev_create_file(dev, *attr);
1353 if (rc)
1354 goto out_unreg;
1355 }
1356 return 0;
1357out_unreg:
1358 for (; attr >= etr_port_attributes; attr--)
1359 sysdev_remove_file(dev, *attr);
1360 sysdev_unregister(dev);
1361out:
1362 return rc;
1363}
1364
1365static void __init etr_unregister_port(struct sys_device *dev)
1366{
1367 struct sysdev_attribute **attr;
1368
1369 for (attr = etr_port_attributes; *attr; attr++)
1370 sysdev_remove_file(dev, *attr);
1371 sysdev_unregister(dev);
1372}
1373
1374static int __init etr_init_sysfs(void)
1375{
1376 int rc;
1377
1378 rc = sysdev_class_register(&etr_sysclass);
1379 if (rc)
1380 goto out;
1381 rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_port);
1382 if (rc)
1383 goto out_unreg_class;
1384 rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_mode);
1385 if (rc)
1386 goto out_remove_stepping_port;
1387 rc = etr_register_port(&etr_port0_dev);
1388 if (rc)
1389 goto out_remove_stepping_mode;
1390 rc = etr_register_port(&etr_port1_dev);
1391 if (rc)
1392 goto out_remove_port0;
1393 return 0;
1394
1395out_remove_port0:
1396 etr_unregister_port(&etr_port0_dev);
1397out_remove_stepping_mode:
1398 sysdev_class_remove_file(&etr_sysclass, &attr_stepping_mode);
1399out_remove_stepping_port:
1400 sysdev_class_remove_file(&etr_sysclass, &attr_stepping_port);
1401out_unreg_class:
1402 sysdev_class_unregister(&etr_sysclass);
1403out:
1404 return rc;
1405}
1406
1407device_initcall(etr_init_sysfs);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001408
1409/*
1410 * Server Time Protocol (STP) code.
1411 */
1412static int stp_online;
1413static struct stp_sstpi stp_info;
1414static void *stp_page;
1415
1416static void stp_work_fn(struct work_struct *work);
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +01001417static DEFINE_MUTEX(stp_work_mutex);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001418static DECLARE_WORK(stp_work, stp_work_fn);
Martin Schwidefsky04362302009-04-14 15:36:19 +02001419static struct timer_list stp_timer;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001420
1421static int __init early_parse_stp(char *p)
1422{
1423 if (strncmp(p, "off", 3) == 0)
1424 stp_online = 0;
1425 else if (strncmp(p, "on", 2) == 0)
1426 stp_online = 1;
1427 return 0;
1428}
1429early_param("stp", early_parse_stp);
1430
1431/*
1432 * Reset STP attachment.
1433 */
Heiko Carstens8f847002008-08-01 16:39:19 +02001434static void __init stp_reset(void)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001435{
1436 int rc;
1437
Heiko Carstensd7d11042009-06-22 12:08:03 +02001438 stp_page = (void *) get_zeroed_page(GFP_ATOMIC);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001439 rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000);
Martin Schwidefsky4a672cf2008-10-10 21:33:29 +02001440 if (rc == 0)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001441 set_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags);
1442 else if (stp_online) {
Martin Schwidefskyfeab6502008-12-25 13:39:38 +01001443 pr_warning("The real or virtual hardware system does "
1444 "not provide an STP interface\n");
Heiko Carstensd7d11042009-06-22 12:08:03 +02001445 free_page((unsigned long) stp_page);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001446 stp_page = NULL;
1447 stp_online = 0;
1448 }
1449}
1450
Martin Schwidefsky04362302009-04-14 15:36:19 +02001451static void stp_timeout(unsigned long dummy)
1452{
1453 queue_work(time_sync_wq, &stp_work);
1454}
1455
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001456static int __init stp_init(void)
1457{
Heiko Carstens750887d2008-12-25 13:38:37 +01001458 if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
1459 return 0;
Martin Schwidefsky04362302009-04-14 15:36:19 +02001460 setup_timer(&stp_timer, stp_timeout, 0UL);
Heiko Carstens750887d2008-12-25 13:38:37 +01001461 time_init_wq();
1462 if (!stp_online)
1463 return 0;
1464 queue_work(time_sync_wq, &stp_work);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001465 return 0;
1466}
1467
1468arch_initcall(stp_init);
1469
1470/*
1471 * STP timing alert. There are three causes:
1472 * 1) timing status change
1473 * 2) link availability change
1474 * 3) time control parameter change
1475 * In all three cases we are only interested in the clock source state.
1476 * If a STP clock source is now available use it.
1477 */
1478static void stp_timing_alert(struct stp_irq_parm *intparm)
1479{
1480 if (intparm->tsc || intparm->lac || intparm->tcpc)
Heiko Carstens750887d2008-12-25 13:38:37 +01001481 queue_work(time_sync_wq, &stp_work);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001482}
1483
1484/*
1485 * STP sync check machine check. This is called when the timing state
1486 * changes from the synchronized state to the unsynchronized state.
1487 * After a STP sync check the clock is not in sync. The machine check
1488 * is broadcasted to all cpus at the same time.
1489 */
1490void stp_sync_check(void)
1491{
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001492 disable_sync_clock(NULL);
Heiko Carstens750887d2008-12-25 13:38:37 +01001493 queue_work(time_sync_wq, &stp_work);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001494}
1495
1496/*
1497 * STP island condition machine check. This is called when an attached
1498 * server attempts to communicate over an STP link and the servers
1499 * have matching CTN ids and have a valid stratum-1 configuration
1500 * but the configurations do not match.
1501 */
1502void stp_island_check(void)
1503{
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001504 disable_sync_clock(NULL);
Heiko Carstens750887d2008-12-25 13:38:37 +01001505 queue_work(time_sync_wq, &stp_work);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001506}
1507
Heiko Carstens750887d2008-12-25 13:38:37 +01001508
1509static int stp_sync_clock(void *data)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001510{
Heiko Carstens750887d2008-12-25 13:38:37 +01001511 static int first;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001512 unsigned long long old_clock, delta;
Heiko Carstens750887d2008-12-25 13:38:37 +01001513 struct clock_sync_data *stp_sync;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001514 int rc;
1515
Heiko Carstens750887d2008-12-25 13:38:37 +01001516 stp_sync = data;
1517
1518 if (xchg(&first, 1) == 1) {
1519 /* Slave */
1520 clock_sync_cpu(stp_sync);
1521 return 0;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001522 }
1523
Heiko Carstens750887d2008-12-25 13:38:37 +01001524 /* Wait until all other cpus entered the sync function. */
1525 while (atomic_read(&stp_sync->cpus) != 0)
1526 cpu_relax();
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001527
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001528 enable_sync_clock();
1529
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001530 rc = 0;
1531 if (stp_info.todoff[0] || stp_info.todoff[1] ||
1532 stp_info.todoff[2] || stp_info.todoff[3] ||
1533 stp_info.tmd != 2) {
1534 old_clock = get_clock();
1535 rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0);
1536 if (rc == 0) {
1537 delta = adjust_time(old_clock, get_clock(), 0);
1538 fixup_clock_comparator(delta);
1539 rc = chsc_sstpi(stp_page, &stp_info,
1540 sizeof(struct stp_sstpi));
1541 if (rc == 0 && stp_info.tmd != 2)
1542 rc = -EAGAIN;
1543 }
1544 }
1545 if (rc) {
1546 disable_sync_clock(NULL);
Heiko Carstens750887d2008-12-25 13:38:37 +01001547 stp_sync->in_sync = -EAGAIN;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001548 } else
Heiko Carstens750887d2008-12-25 13:38:37 +01001549 stp_sync->in_sync = 1;
1550 xchg(&first, 0);
1551 return 0;
1552}
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001553
Heiko Carstens750887d2008-12-25 13:38:37 +01001554/*
1555 * STP work. Check for the STP state and take over the clock
1556 * synchronization if the STP clock source is usable.
1557 */
1558static void stp_work_fn(struct work_struct *work)
1559{
1560 struct clock_sync_data stp_sync;
1561 int rc;
1562
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +01001563 /* prevent multiple execution. */
1564 mutex_lock(&stp_work_mutex);
1565
Heiko Carstens750887d2008-12-25 13:38:37 +01001566 if (!stp_online) {
1567 chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000);
Martin Schwidefsky04362302009-04-14 15:36:19 +02001568 del_timer_sync(&stp_timer);
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +01001569 goto out_unlock;
Heiko Carstens750887d2008-12-25 13:38:37 +01001570 }
1571
1572 rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0xb0e0);
1573 if (rc)
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +01001574 goto out_unlock;
Heiko Carstens750887d2008-12-25 13:38:37 +01001575
1576 rc = chsc_sstpi(stp_page, &stp_info, sizeof(struct stp_sstpi));
1577 if (rc || stp_info.c == 0)
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +01001578 goto out_unlock;
Heiko Carstens750887d2008-12-25 13:38:37 +01001579
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001580 /* Skip synchronization if the clock is already in sync. */
1581 if (check_sync_clock())
1582 goto out_unlock;
1583
Heiko Carstens750887d2008-12-25 13:38:37 +01001584 memset(&stp_sync, 0, sizeof(stp_sync));
1585 get_online_cpus();
1586 atomic_set(&stp_sync.cpus, num_online_cpus() - 1);
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +02001587 stop_machine(stp_sync_clock, &stp_sync, cpu_online_mask);
Heiko Carstens750887d2008-12-25 13:38:37 +01001588 put_online_cpus();
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +01001589
Martin Schwidefsky04362302009-04-14 15:36:19 +02001590 if (!check_sync_clock())
1591 /*
1592 * There is a usable clock but the synchonization failed.
1593 * Retry after a second.
1594 */
1595 mod_timer(&stp_timer, jiffies + HZ);
1596
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +01001597out_unlock:
1598 mutex_unlock(&stp_work_mutex);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001599}
1600
1601/*
1602 * STP class sysfs interface functions
1603 */
1604static struct sysdev_class stp_sysclass = {
1605 .name = "stp",
1606};
1607
Andi Kleenc9be0a32010-01-05 12:47:58 +01001608static ssize_t stp_ctn_id_show(struct sysdev_class *class,
1609 struct sysdev_class_attribute *attr,
1610 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001611{
1612 if (!stp_online)
1613 return -ENODATA;
1614 return sprintf(buf, "%016llx\n",
1615 *(unsigned long long *) stp_info.ctnid);
1616}
1617
1618static SYSDEV_CLASS_ATTR(ctn_id, 0400, stp_ctn_id_show, NULL);
1619
Andi Kleenc9be0a32010-01-05 12:47:58 +01001620static ssize_t stp_ctn_type_show(struct sysdev_class *class,
1621 struct sysdev_class_attribute *attr,
1622 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001623{
1624 if (!stp_online)
1625 return -ENODATA;
1626 return sprintf(buf, "%i\n", stp_info.ctn);
1627}
1628
1629static SYSDEV_CLASS_ATTR(ctn_type, 0400, stp_ctn_type_show, NULL);
1630
Andi Kleenc9be0a32010-01-05 12:47:58 +01001631static ssize_t stp_dst_offset_show(struct sysdev_class *class,
1632 struct sysdev_class_attribute *attr,
1633 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001634{
1635 if (!stp_online || !(stp_info.vbits & 0x2000))
1636 return -ENODATA;
1637 return sprintf(buf, "%i\n", (int)(s16) stp_info.dsto);
1638}
1639
1640static SYSDEV_CLASS_ATTR(dst_offset, 0400, stp_dst_offset_show, NULL);
1641
Andi Kleenc9be0a32010-01-05 12:47:58 +01001642static ssize_t stp_leap_seconds_show(struct sysdev_class *class,
1643 struct sysdev_class_attribute *attr,
1644 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001645{
1646 if (!stp_online || !(stp_info.vbits & 0x8000))
1647 return -ENODATA;
1648 return sprintf(buf, "%i\n", (int)(s16) stp_info.leaps);
1649}
1650
1651static SYSDEV_CLASS_ATTR(leap_seconds, 0400, stp_leap_seconds_show, NULL);
1652
Andi Kleenc9be0a32010-01-05 12:47:58 +01001653static ssize_t stp_stratum_show(struct sysdev_class *class,
1654 struct sysdev_class_attribute *attr,
1655 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001656{
1657 if (!stp_online)
1658 return -ENODATA;
1659 return sprintf(buf, "%i\n", (int)(s16) stp_info.stratum);
1660}
1661
1662static SYSDEV_CLASS_ATTR(stratum, 0400, stp_stratum_show, NULL);
1663
Andi Kleenc9be0a32010-01-05 12:47:58 +01001664static ssize_t stp_time_offset_show(struct sysdev_class *class,
1665 struct sysdev_class_attribute *attr,
1666 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001667{
1668 if (!stp_online || !(stp_info.vbits & 0x0800))
1669 return -ENODATA;
1670 return sprintf(buf, "%i\n", (int) stp_info.tto);
1671}
1672
1673static SYSDEV_CLASS_ATTR(time_offset, 0400, stp_time_offset_show, NULL);
1674
Andi Kleenc9be0a32010-01-05 12:47:58 +01001675static ssize_t stp_time_zone_offset_show(struct sysdev_class *class,
1676 struct sysdev_class_attribute *attr,
1677 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001678{
1679 if (!stp_online || !(stp_info.vbits & 0x4000))
1680 return -ENODATA;
1681 return sprintf(buf, "%i\n", (int)(s16) stp_info.tzo);
1682}
1683
1684static SYSDEV_CLASS_ATTR(time_zone_offset, 0400,
1685 stp_time_zone_offset_show, NULL);
1686
Andi Kleenc9be0a32010-01-05 12:47:58 +01001687static ssize_t stp_timing_mode_show(struct sysdev_class *class,
1688 struct sysdev_class_attribute *attr,
1689 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001690{
1691 if (!stp_online)
1692 return -ENODATA;
1693 return sprintf(buf, "%i\n", stp_info.tmd);
1694}
1695
1696static SYSDEV_CLASS_ATTR(timing_mode, 0400, stp_timing_mode_show, NULL);
1697
Andi Kleenc9be0a32010-01-05 12:47:58 +01001698static ssize_t stp_timing_state_show(struct sysdev_class *class,
1699 struct sysdev_class_attribute *attr,
1700 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001701{
1702 if (!stp_online)
1703 return -ENODATA;
1704 return sprintf(buf, "%i\n", stp_info.tst);
1705}
1706
1707static SYSDEV_CLASS_ATTR(timing_state, 0400, stp_timing_state_show, NULL);
1708
Andi Kleenc9be0a32010-01-05 12:47:58 +01001709static ssize_t stp_online_show(struct sysdev_class *class,
1710 struct sysdev_class_attribute *attr,
1711 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001712{
1713 return sprintf(buf, "%i\n", stp_online);
1714}
1715
1716static ssize_t stp_online_store(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +01001717 struct sysdev_class_attribute *attr,
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001718 const char *buf, size_t count)
1719{
1720 unsigned int value;
1721
1722 value = simple_strtoul(buf, NULL, 0);
1723 if (value != 0 && value != 1)
1724 return -EINVAL;
1725 if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
1726 return -EOPNOTSUPP;
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001727 mutex_lock(&clock_sync_mutex);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001728 stp_online = value;
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001729 if (stp_online)
1730 set_bit(CLOCK_SYNC_STP, &clock_sync_flags);
1731 else
1732 clear_bit(CLOCK_SYNC_STP, &clock_sync_flags);
Heiko Carstens750887d2008-12-25 13:38:37 +01001733 queue_work(time_sync_wq, &stp_work);
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001734 mutex_unlock(&clock_sync_mutex);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001735 return count;
1736}
1737
1738/*
1739 * Can't use SYSDEV_CLASS_ATTR because the attribute should be named
1740 * stp/online but attr_online already exists in this file ..
1741 */
1742static struct sysdev_class_attribute attr_stp_online = {
1743 .attr = { .name = "online", .mode = 0600 },
1744 .show = stp_online_show,
1745 .store = stp_online_store,
1746};
1747
1748static struct sysdev_class_attribute *stp_attributes[] = {
1749 &attr_ctn_id,
1750 &attr_ctn_type,
1751 &attr_dst_offset,
1752 &attr_leap_seconds,
1753 &attr_stp_online,
1754 &attr_stratum,
1755 &attr_time_offset,
1756 &attr_time_zone_offset,
1757 &attr_timing_mode,
1758 &attr_timing_state,
1759 NULL
1760};
1761
1762static int __init stp_init_sysfs(void)
1763{
1764 struct sysdev_class_attribute **attr;
1765 int rc;
1766
1767 rc = sysdev_class_register(&stp_sysclass);
1768 if (rc)
1769 goto out;
1770 for (attr = stp_attributes; *attr; attr++) {
1771 rc = sysdev_class_create_file(&stp_sysclass, *attr);
1772 if (rc)
1773 goto out_unreg;
1774 }
1775 return 0;
1776out_unreg:
1777 for (; attr >= stp_attributes; attr--)
1778 sysdev_class_remove_file(&stp_sysclass, *attr);
1779 sysdev_class_unregister(&stp_sysclass);
1780out:
1781 return rc;
1782}
1783
1784device_initcall(stp_init_sysfs);