blob: 7aec676fefd593fcc7305613a6d1e3e4964cdbb0 [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
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)
10 *
11 * Derived from "arch/i386/kernel/time.c"
12 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
13 */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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>
21#include <linux/mm.h>
22#include <linux/interrupt.h>
23#include <linux/time.h>
Ralf Baechle3367b992007-05-08 00:27:52 -070024#include <linux/sysdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/delay.h>
26#include <linux/init.h>
27#include <linux/smp.h>
28#include <linux/types.h>
29#include <linux/profile.h>
30#include <linux/timex.h>
31#include <linux/notifier.h>
Martin Schwidefskydc64bef2006-10-06 16:38:48 +020032#include <linux/clocksource.h>
Heiko Carstens5a62b192008-04-17 07:46:25 +020033#include <linux/clockchips.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/uaccess.h>
35#include <asm/delay.h>
36#include <asm/s390_ext.h>
37#include <asm/div64.h>
38#include <asm/irq.h>
Heiko Carstens5a489b92006-10-06 16:38:35 +020039#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/timer.h>
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010041#include <asm/etr.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020042#include <asm/cio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/* change this if you have some constant time drift */
45#define USECS_PER_JIFFY ((unsigned long) 1000000/HZ)
46#define CLK_TICKS_PER_JIFFY ((unsigned long) USECS_PER_JIFFY << 12)
47
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010048/* The value of the TOD clock for 1.1.1970. */
49#define TOD_UNIX_EPOCH 0x7d91048bca000000ULL
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * Create a small time difference between the timer interrupts
53 * on the different cpus to avoid lock contention.
54 */
55#define CPU_DEVIATION (smp_processor_id() << 12)
56
57#define TICK_SIZE tick
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static ext_int_info_t ext_int_info_cc;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010060static ext_int_info_t ext_int_etr_cc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static u64 jiffies_timer_cc;
Heiko Carstens5a62b192008-04-17 07:46:25 +020062
63static DEFINE_PER_CPU(struct clock_event_device, comparators);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/*
66 * Scheduler clock - returns current time in nanosec units.
67 */
68unsigned long long sched_clock(void)
69{
Jan Glauberc0015f92008-04-17 07:46:16 +020070 return ((get_clock_xt() - jiffies_timer_cc) * 125) >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
Jan Glauber32f65f22006-02-01 03:06:33 -080073/*
74 * Monotonic_clock - returns # of nanoseconds passed since time_init()
75 */
76unsigned long long monotonic_clock(void)
77{
78 return sched_clock();
79}
80EXPORT_SYMBOL(monotonic_clock);
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082void tod_to_timeval(__u64 todval, struct timespec *xtime)
83{
84 unsigned long long sec;
85
86 sec = todval >> 12;
87 do_div(sec, 1000000);
88 xtime->tv_sec = sec;
89 todval -= (sec * 1000000) << 12;
90 xtime->tv_nsec = ((todval * 1000) >> 12);
91}
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#ifdef CONFIG_PROFILING
Heiko Carstens5a489b92006-10-06 16:38:35 +020094#define s390_do_profile() profile_tick(CPU_PROFILING)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#else
Heiko Carstens5a489b92006-10-06 16:38:35 +020096#define s390_do_profile() do { ; } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#endif /* CONFIG_PROFILING */
98
Heiko Carstens5a62b192008-04-17 07:46:25 +020099void clock_comparator_work(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Heiko Carstens5a62b192008-04-17 07:46:25 +0200101 struct clock_event_device *cd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Heiko Carstens5a62b192008-04-17 07:46:25 +0200103 S390_lowcore.clock_comparator = -1ULL;
104 set_clock_comparator(S390_lowcore.clock_comparator);
105 cd = &__get_cpu_var(comparators);
106 cd->event_handler(cd);
Heiko Carstens5a489b92006-10-06 16:38:35 +0200107 s390_do_profile();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/*
Heiko Carstens5a62b192008-04-17 07:46:25 +0200111 * Fixup the clock comparator.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 */
Heiko Carstens5a62b192008-04-17 07:46:25 +0200113static void fixup_clock_comparator(unsigned long long delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Heiko Carstens5a62b192008-04-17 07:46:25 +0200115 /* If nobody is waiting there's nothing to fix. */
116 if (S390_lowcore.clock_comparator == -1ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200118 S390_lowcore.clock_comparator += delta;
119 set_clock_comparator(S390_lowcore.clock_comparator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Heiko Carstens5a62b192008-04-17 07:46:25 +0200122static int s390_next_event(unsigned long delta,
123 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Heiko Carstens5a62b192008-04-17 07:46:25 +0200125 S390_lowcore.clock_comparator = get_clock() + delta;
126 set_clock_comparator(S390_lowcore.clock_comparator);
127 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Heiko Carstens5a62b192008-04-17 07:46:25 +0200130static void s390_set_mode(enum clock_event_mode mode,
131 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100133}
134
135/*
136 * Set up lowcore and control register of the current cpu to
137 * enable TOD clock and clock comparator interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 */
139void init_cpu_timer(void)
140{
Heiko Carstens5a62b192008-04-17 07:46:25 +0200141 struct clock_event_device *cd;
142 int cpu;
143
144 S390_lowcore.clock_comparator = -1ULL;
145 set_clock_comparator(S390_lowcore.clock_comparator);
146
147 cpu = smp_processor_id();
148 cd = &per_cpu(comparators, cpu);
149 cd->name = "comparator";
150 cd->features = CLOCK_EVT_FEAT_ONESHOT;
151 cd->mult = 16777;
152 cd->shift = 12;
153 cd->min_delta_ns = 1;
154 cd->max_delta_ns = LONG_MAX;
155 cd->rating = 400;
156 cd->cpumask = cpumask_of_cpu(cpu);
157 cd->set_next_event = s390_next_event;
158 cd->set_mode = s390_set_mode;
159
160 clockevents_register_device(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100162 /* Enable clock comparator timer interrupt. */
163 __ctl_set_bit(0,11);
164
165 /* Always allow ETR external interrupts, even without an ETR. */
166 __ctl_set_bit(0, 4);
167}
168
169static void clock_comparator_interrupt(__u16 code)
170{
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100171}
172
173static void etr_reset(void);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100174static void etr_ext_handler(__u16);
175
176/*
177 * Get the TOD clock running.
178 */
179static u64 __init reset_tod_clock(void)
180{
181 u64 time;
182
183 etr_reset();
184 if (store_clock(&time) == 0)
185 return time;
186 /* TOD clock not running. Set the clock to Unix Epoch. */
187 if (set_clock(TOD_UNIX_EPOCH) != 0 || store_clock(&time) != 0)
188 panic("TOD clock not operational.");
189
190 return TOD_UNIX_EPOCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Martin Schwidefskydc64bef2006-10-06 16:38:48 +0200193static cycle_t read_tod_clock(void)
194{
195 return get_clock();
196}
197
198static struct clocksource clocksource_tod = {
199 .name = "tod",
Christian Borntraegerd2cb0e62007-11-05 11:10:14 +0100200 .rating = 400,
Martin Schwidefskydc64bef2006-10-06 16:38:48 +0200201 .read = read_tod_clock,
202 .mask = -1ULL,
203 .mult = 1000,
204 .shift = 12,
Thomas Gleixnercc02d802007-02-16 01:27:39 -0800205 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Martin Schwidefskydc64bef2006-10-06 16:38:48 +0200206};
207
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
210 * Initialize the TOD clock and the CPU timer of
211 * the boot cpu.
212 */
213void __init time_init(void)
214{
Heiko Carstens5a62b192008-04-17 07:46:25 +0200215 u64 init_timer_cc;
216
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100217 init_timer_cc = reset_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 jiffies_timer_cc = init_timer_cc - jiffies_64 * CLK_TICKS_PER_JIFFY;
219
220 /* set xtime */
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100221 tod_to_timeval(init_timer_cc - TOD_UNIX_EPOCH, &xtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 set_normalized_timespec(&wall_to_monotonic,
223 -xtime.tv_sec, -xtime.tv_nsec);
224
225 /* request the clock comparator external interrupt */
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100226 if (register_early_external_interrupt(0x1004,
227 clock_comparator_interrupt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 &ext_int_info_cc) != 0)
229 panic("Couldn't request external interrupt 0x1004");
230
Martin Schwidefskydc64bef2006-10-06 16:38:48 +0200231 if (clocksource_register(&clocksource_tod) != 0)
232 panic("Could not register TOD clock source");
233
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100234 /* request the etr external interrupt */
235 if (register_early_external_interrupt(0x1406, etr_ext_handler,
236 &ext_int_etr_cc) != 0)
237 panic("Couldn't request external interrupt 0x1406");
238
239 /* Enable TOD clock interrupts on the boot cpu. */
240 init_cpu_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242#ifdef CONFIG_VIRT_TIMER
243 vtime_init();
244#endif
245}
246
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100247/*
248 * External Time Reference (ETR) code.
249 */
250static int etr_port0_online;
251static int etr_port1_online;
252
253static int __init early_parse_etr(char *p)
254{
255 if (strncmp(p, "off", 3) == 0)
256 etr_port0_online = etr_port1_online = 0;
257 else if (strncmp(p, "port0", 5) == 0)
258 etr_port0_online = 1;
259 else if (strncmp(p, "port1", 5) == 0)
260 etr_port1_online = 1;
261 else if (strncmp(p, "on", 2) == 0)
262 etr_port0_online = etr_port1_online = 1;
263 return 0;
264}
265early_param("etr", early_parse_etr);
266
267enum etr_event {
268 ETR_EVENT_PORT0_CHANGE,
269 ETR_EVENT_PORT1_CHANGE,
270 ETR_EVENT_PORT_ALERT,
271 ETR_EVENT_SYNC_CHECK,
272 ETR_EVENT_SWITCH_LOCAL,
273 ETR_EVENT_UPDATE,
274};
275
276enum etr_flags {
277 ETR_FLAG_ENOSYS,
278 ETR_FLAG_EACCES,
279 ETR_FLAG_STEAI,
280};
281
282/*
283 * Valid bit combinations of the eacr register are (x = don't care):
284 * e0 e1 dp p0 p1 ea es sl
285 * 0 0 x 0 0 0 0 0 initial, disabled state
286 * 0 0 x 0 1 1 0 0 port 1 online
287 * 0 0 x 1 0 1 0 0 port 0 online
288 * 0 0 x 1 1 1 0 0 both ports online
289 * 0 1 x 0 1 1 0 0 port 1 online and usable, ETR or PPS mode
290 * 0 1 x 0 1 1 0 1 port 1 online, usable and ETR mode
291 * 0 1 x 0 1 1 1 0 port 1 online, usable, PPS mode, in-sync
292 * 0 1 x 0 1 1 1 1 port 1 online, usable, ETR mode, in-sync
293 * 0 1 x 1 1 1 0 0 both ports online, port 1 usable
294 * 0 1 x 1 1 1 1 0 both ports online, port 1 usable, PPS mode, in-sync
295 * 0 1 x 1 1 1 1 1 both ports online, port 1 usable, ETR mode, in-sync
296 * 1 0 x 1 0 1 0 0 port 0 online and usable, ETR or PPS mode
297 * 1 0 x 1 0 1 0 1 port 0 online, usable and ETR mode
298 * 1 0 x 1 0 1 1 0 port 0 online, usable, PPS mode, in-sync
299 * 1 0 x 1 0 1 1 1 port 0 online, usable, ETR mode, in-sync
300 * 1 0 x 1 1 1 0 0 both ports online, port 0 usable
301 * 1 0 x 1 1 1 1 0 both ports online, port 0 usable, PPS mode, in-sync
302 * 1 0 x 1 1 1 1 1 both ports online, port 0 usable, ETR mode, in-sync
303 * 1 1 x 1 1 1 1 0 both ports online & usable, ETR, in-sync
304 * 1 1 x 1 1 1 1 1 both ports online & usable, ETR, in-sync
305 */
306static struct etr_eacr etr_eacr;
307static u64 etr_tolec; /* time of last eacr update */
308static unsigned long etr_flags;
309static struct etr_aib etr_port0;
310static int etr_port0_uptodate;
311static struct etr_aib etr_port1;
312static int etr_port1_uptodate;
313static unsigned long etr_events;
314static struct timer_list etr_timer;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100315static DEFINE_PER_CPU(atomic_t, etr_sync_word);
316
317static void etr_timeout(unsigned long dummy);
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200318static void etr_work_fn(struct work_struct *work);
319static DECLARE_WORK(etr_work, etr_work_fn);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100320
321/*
322 * The etr get_clock function. It will write the current clock value
323 * to the clock pointer and return 0 if the clock is in sync with the
324 * external time source. If the clock mode is local it will return
325 * -ENOSYS and -EAGAIN if the clock is not in sync with the external
326 * reference. This function is what ETR is all about..
327 */
328int get_sync_clock(unsigned long long *clock)
329{
330 atomic_t *sw_ptr;
331 unsigned int sw0, sw1;
332
333 sw_ptr = &get_cpu_var(etr_sync_word);
334 sw0 = atomic_read(sw_ptr);
335 *clock = get_clock();
336 sw1 = atomic_read(sw_ptr);
337 put_cpu_var(etr_sync_sync);
338 if (sw0 == sw1 && (sw0 & 0x80000000U))
339 /* Success: time is in sync. */
340 return 0;
341 if (test_bit(ETR_FLAG_ENOSYS, &etr_flags))
342 return -ENOSYS;
343 if (test_bit(ETR_FLAG_EACCES, &etr_flags))
344 return -EACCES;
345 return -EAGAIN;
346}
347EXPORT_SYMBOL(get_sync_clock);
348
349/*
350 * Make get_sync_clock return -EAGAIN.
351 */
352static void etr_disable_sync_clock(void *dummy)
353{
354 atomic_t *sw_ptr = &__get_cpu_var(etr_sync_word);
355 /*
356 * Clear the in-sync bit 2^31. All get_sync_clock calls will
357 * fail until the sync bit is turned back on. In addition
358 * increase the "sequence" counter to avoid the race of an
359 * etr event and the complete recovery against get_sync_clock.
360 */
361 atomic_clear_mask(0x80000000, sw_ptr);
362 atomic_inc(sw_ptr);
363}
364
365/*
366 * Make get_sync_clock return 0 again.
367 * Needs to be called from a context disabled for preemption.
368 */
369static void etr_enable_sync_clock(void)
370{
371 atomic_t *sw_ptr = &__get_cpu_var(etr_sync_word);
372 atomic_set_mask(0x80000000, sw_ptr);
373}
374
375/*
376 * Reset ETR attachment.
377 */
378static void etr_reset(void)
379{
380 etr_eacr = (struct etr_eacr) {
381 .e0 = 0, .e1 = 0, ._pad0 = 4, .dp = 0,
382 .p0 = 0, .p1 = 0, ._pad1 = 0, .ea = 0,
383 .es = 0, .sl = 0 };
384 if (etr_setr(&etr_eacr) == 0)
385 etr_tolec = get_clock();
386 else {
387 set_bit(ETR_FLAG_ENOSYS, &etr_flags);
388 if (etr_port0_online || etr_port1_online) {
389 printk(KERN_WARNING "Running on non ETR capable "
390 "machine, only local mode available.\n");
391 etr_port0_online = etr_port1_online = 0;
392 }
393 }
394}
395
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200396static int __init etr_init(void)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100397{
398 struct etr_aib aib;
399
400 if (test_bit(ETR_FLAG_ENOSYS, &etr_flags))
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200401 return 0;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100402 /* Check if this machine has the steai instruction. */
403 if (etr_steai(&aib, ETR_STEAI_STEPPING_PORT) == 0)
404 set_bit(ETR_FLAG_STEAI, &etr_flags);
405 setup_timer(&etr_timer, etr_timeout, 0UL);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100406 if (!etr_port0_online && !etr_port1_online)
407 set_bit(ETR_FLAG_EACCES, &etr_flags);
408 if (etr_port0_online) {
409 set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200410 schedule_work(&etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100411 }
412 if (etr_port1_online) {
413 set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200414 schedule_work(&etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100415 }
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200416 return 0;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100417}
418
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200419arch_initcall(etr_init);
420
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100421/*
422 * Two sorts of ETR machine checks. The architecture reads:
423 * "When a machine-check niterruption occurs and if a switch-to-local or
424 * ETR-sync-check interrupt request is pending but disabled, this pending
425 * disabled interruption request is indicated and is cleared".
426 * Which means that we can get etr_switch_to_local events from the machine
427 * check handler although the interruption condition is disabled. Lovely..
428 */
429
430/*
431 * Switch to local machine check. This is called when the last usable
432 * ETR port goes inactive. After switch to local the clock is not in sync.
433 */
434void etr_switch_to_local(void)
435{
436 if (!etr_eacr.sl)
437 return;
438 etr_disable_sync_clock(NULL);
439 set_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events);
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200440 schedule_work(&etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100441}
442
443/*
444 * ETR sync check machine check. This is called when the ETR OTE and the
445 * local clock OTE are farther apart than the ETR sync check tolerance.
446 * After a ETR sync check the clock is not in sync. The machine check
447 * is broadcasted to all cpus at the same time.
448 */
449void etr_sync_check(void)
450{
451 if (!etr_eacr.es)
452 return;
453 etr_disable_sync_clock(NULL);
454 set_bit(ETR_EVENT_SYNC_CHECK, &etr_events);
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200455 schedule_work(&etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100456}
457
458/*
459 * ETR external interrupt. There are two causes:
460 * 1) port state change, check the usability of the port
461 * 2) port alert, one of the ETR-data-validity bits (v1-v2 bits of the
462 * sldr-status word) or ETR-data word 1 (edf1) or ETR-data word 3 (edf3)
463 * or ETR-data word 4 (edf4) has changed.
464 */
465static void etr_ext_handler(__u16 code)
466{
467 struct etr_interruption_parameter *intparm =
468 (struct etr_interruption_parameter *) &S390_lowcore.ext_params;
469
470 if (intparm->pc0)
471 /* ETR port 0 state change. */
472 set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
473 if (intparm->pc1)
474 /* ETR port 1 state change. */
475 set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
476 if (intparm->eai)
477 /*
478 * ETR port alert on either port 0, 1 or both.
479 * Both ports are not up-to-date now.
480 */
481 set_bit(ETR_EVENT_PORT_ALERT, &etr_events);
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200482 schedule_work(&etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100483}
484
485static void etr_timeout(unsigned long dummy)
486{
487 set_bit(ETR_EVENT_UPDATE, &etr_events);
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200488 schedule_work(&etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100489}
490
491/*
492 * Check if the etr mode is pss.
493 */
494static inline int etr_mode_is_pps(struct etr_eacr eacr)
495{
496 return eacr.es && !eacr.sl;
497}
498
499/*
500 * Check if the etr mode is etr.
501 */
502static inline int etr_mode_is_etr(struct etr_eacr eacr)
503{
504 return eacr.es && eacr.sl;
505}
506
507/*
508 * Check if the port can be used for TOD synchronization.
509 * For PPS mode the port has to receive OTEs. For ETR mode
510 * the port has to receive OTEs, the ETR stepping bit has to
511 * be zero and the validity bits for data frame 1, 2, and 3
512 * have to be 1.
513 */
514static int etr_port_valid(struct etr_aib *aib, int port)
515{
516 unsigned int psc;
517
518 /* Check that this port is receiving OTEs. */
519 if (aib->tsp == 0)
520 return 0;
521
522 psc = port ? aib->esw.psc1 : aib->esw.psc0;
523 if (psc == etr_lpsc_pps_mode)
524 return 1;
525 if (psc == etr_lpsc_operational_step)
526 return !aib->esw.y && aib->slsw.v1 &&
527 aib->slsw.v2 && aib->slsw.v3;
528 return 0;
529}
530
531/*
532 * Check if two ports are on the same network.
533 */
534static int etr_compare_network(struct etr_aib *aib1, struct etr_aib *aib2)
535{
536 // FIXME: any other fields we have to compare?
537 return aib1->edf1.net_id == aib2->edf1.net_id;
538}
539
540/*
541 * Wrapper for etr_stei that converts physical port states
542 * to logical port states to be consistent with the output
543 * of stetr (see etr_psc vs. etr_lpsc).
544 */
545static void etr_steai_cv(struct etr_aib *aib, unsigned int func)
546{
547 BUG_ON(etr_steai(aib, func) != 0);
548 /* Convert port state to logical port state. */
549 if (aib->esw.psc0 == 1)
550 aib->esw.psc0 = 2;
551 else if (aib->esw.psc0 == 0 && aib->esw.p == 0)
552 aib->esw.psc0 = 1;
553 if (aib->esw.psc1 == 1)
554 aib->esw.psc1 = 2;
555 else if (aib->esw.psc1 == 0 && aib->esw.p == 1)
556 aib->esw.psc1 = 1;
557}
558
559/*
560 * Check if the aib a2 is still connected to the same attachment as
561 * aib a1, the etv values differ by one and a2 is valid.
562 */
563static int etr_aib_follows(struct etr_aib *a1, struct etr_aib *a2, int p)
564{
565 int state_a1, state_a2;
566
567 /* Paranoia check: e0/e1 should better be the same. */
568 if (a1->esw.eacr.e0 != a2->esw.eacr.e0 ||
569 a1->esw.eacr.e1 != a2->esw.eacr.e1)
570 return 0;
571
572 /* Still connected to the same etr ? */
573 state_a1 = p ? a1->esw.psc1 : a1->esw.psc0;
574 state_a2 = p ? a2->esw.psc1 : a2->esw.psc0;
575 if (state_a1 == etr_lpsc_operational_step) {
576 if (state_a2 != etr_lpsc_operational_step ||
577 a1->edf1.net_id != a2->edf1.net_id ||
578 a1->edf1.etr_id != a2->edf1.etr_id ||
579 a1->edf1.etr_pn != a2->edf1.etr_pn)
580 return 0;
581 } else if (state_a2 != etr_lpsc_pps_mode)
582 return 0;
583
584 /* The ETV value of a2 needs to be ETV of a1 + 1. */
585 if (a1->edf2.etv + 1 != a2->edf2.etv)
586 return 0;
587
588 if (!etr_port_valid(a2, p))
589 return 0;
590
591 return 1;
592}
593
594/*
Heiko Carstens5a62b192008-04-17 07:46:25 +0200595 * The time is "clock". old is what we think the time is.
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100596 * Adjust the value by a multiple of jiffies and add the delta to ntp.
597 * "delay" is an approximation how long the synchronization took. If
598 * the time correction is positive, then "delay" is subtracted from
599 * the time difference and only the remaining part is passed to ntp.
600 */
Heiko Carstens5a62b192008-04-17 07:46:25 +0200601static unsigned long long etr_adjust_time(unsigned long long old,
602 unsigned long long clock,
603 unsigned long long delay)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100604{
605 unsigned long long delta, ticks;
606 struct timex adjust;
607
Heiko Carstens5a62b192008-04-17 07:46:25 +0200608 if (clock > old) {
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100609 /* It is later than we thought. */
Heiko Carstens5a62b192008-04-17 07:46:25 +0200610 delta = ticks = clock - old;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100611 delta = ticks = (delta < delay) ? 0 : delta - delay;
612 delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100613 adjust.offset = ticks * (1000000 / HZ);
614 } else {
615 /* It is earlier than we thought. */
Heiko Carstens5a62b192008-04-17 07:46:25 +0200616 delta = ticks = old - clock;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100617 delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
Heiko Carstens5a62b192008-04-17 07:46:25 +0200618 delta = -delta;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100619 adjust.offset = -ticks * (1000000 / HZ);
620 }
Heiko Carstens5a62b192008-04-17 07:46:25 +0200621 jiffies_timer_cc += delta;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100622 if (adjust.offset != 0) {
623 printk(KERN_NOTICE "etr: time adjusted by %li micro-seconds\n",
624 adjust.offset);
625 adjust.modes = ADJ_OFFSET_SINGLESHOT;
626 do_adjtimex(&adjust);
627 }
Heiko Carstens5a62b192008-04-17 07:46:25 +0200628 return delta;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100629}
630
Heiko Carstens5a62b192008-04-17 07:46:25 +0200631static struct {
632 int in_sync;
633 unsigned long long fixup_cc;
634} etr_sync;
635
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100636static void etr_sync_cpu_start(void *dummy)
637{
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100638 etr_enable_sync_clock();
639 /*
640 * This looks like a busy wait loop but it isn't. etr_sync_cpus
641 * is called on all other cpus while the TOD clocks is stopped.
642 * __udelay will stop the cpu on an enabled wait psw until the
643 * TOD is running again.
644 */
Heiko Carstens5a62b192008-04-17 07:46:25 +0200645 while (etr_sync.in_sync == 0) {
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100646 __udelay(1);
Heiko Carstens6c732de2007-02-21 10:55:15 +0100647 /*
648 * A different cpu changes *in_sync. Therefore use
649 * barrier() to force memory access.
650 */
651 barrier();
652 }
Heiko Carstens5a62b192008-04-17 07:46:25 +0200653 if (etr_sync.in_sync != 1)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100654 /* Didn't work. Clear per-cpu in sync bit again. */
655 etr_disable_sync_clock(NULL);
656 /*
657 * This round of TOD syncing is done. Set the clock comparator
658 * to the next tick and let the processor continue.
659 */
Heiko Carstens5a62b192008-04-17 07:46:25 +0200660 fixup_clock_comparator(etr_sync.fixup_cc);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100661}
662
663static void etr_sync_cpu_end(void *dummy)
664{
665}
666
667/*
668 * Sync the TOD clock using the port refered to by aibp. This port
669 * has to be enabled and the other port has to be disabled. The
670 * last eacr update has to be more than 1.6 seconds in the past.
671 */
672static int etr_sync_clock(struct etr_aib *aib, int port)
673{
674 struct etr_aib *sync_port;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200675 unsigned long long clock, old_clock, delay, delta;
676 int follows;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100677 int rc;
678
679 /* Check if the current aib is adjacent to the sync port aib. */
680 sync_port = (port == 0) ? &etr_port0 : &etr_port1;
681 follows = etr_aib_follows(sync_port, aib, port);
682 memcpy(sync_port, aib, sizeof(*aib));
683 if (!follows)
684 return -EAGAIN;
685
686 /*
687 * Catch all other cpus and make them wait until we have
688 * successfully synced the clock. smp_call_function will
689 * return after all other cpus are in etr_sync_cpu_start.
690 */
Heiko Carstens5a62b192008-04-17 07:46:25 +0200691 memset(&etr_sync, 0, sizeof(etr_sync));
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100692 preempt_disable();
Heiko Carstens5a62b192008-04-17 07:46:25 +0200693 smp_call_function(etr_sync_cpu_start, NULL, 0, 0);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100694 local_irq_disable();
695 etr_enable_sync_clock();
696
697 /* Set clock to next OTE. */
698 __ctl_set_bit(14, 21);
699 __ctl_set_bit(0, 29);
700 clock = ((unsigned long long) (aib->edf2.etv + 1)) << 32;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200701 old_clock = get_clock();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100702 if (set_clock(clock) == 0) {
703 __udelay(1); /* Wait for the clock to start. */
704 __ctl_clear_bit(0, 29);
705 __ctl_clear_bit(14, 21);
706 etr_stetr(aib);
707 /* Adjust Linux timing variables. */
708 delay = (unsigned long long)
709 (aib->edf2.etv - sync_port->edf2.etv) << 32;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200710 delta = etr_adjust_time(old_clock, clock, delay);
711 etr_sync.fixup_cc = delta;
712 fixup_clock_comparator(delta);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100713 /* Verify that the clock is properly set. */
714 if (!etr_aib_follows(sync_port, aib, port)) {
715 /* Didn't work. */
716 etr_disable_sync_clock(NULL);
Heiko Carstens5a62b192008-04-17 07:46:25 +0200717 etr_sync.in_sync = -EAGAIN;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100718 rc = -EAGAIN;
719 } else {
Heiko Carstens5a62b192008-04-17 07:46:25 +0200720 etr_sync.in_sync = 1;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100721 rc = 0;
722 }
723 } else {
724 /* Could not set the clock ?!? */
725 __ctl_clear_bit(0, 29);
726 __ctl_clear_bit(14, 21);
727 etr_disable_sync_clock(NULL);
Heiko Carstens5a62b192008-04-17 07:46:25 +0200728 etr_sync.in_sync = -EAGAIN;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100729 rc = -EAGAIN;
730 }
731 local_irq_enable();
732 smp_call_function(etr_sync_cpu_end,NULL,0,0);
733 preempt_enable();
734 return rc;
735}
736
737/*
738 * Handle the immediate effects of the different events.
739 * The port change event is used for online/offline changes.
740 */
741static struct etr_eacr etr_handle_events(struct etr_eacr eacr)
742{
743 if (test_and_clear_bit(ETR_EVENT_SYNC_CHECK, &etr_events))
744 eacr.es = 0;
745 if (test_and_clear_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events))
746 eacr.es = eacr.sl = 0;
747 if (test_and_clear_bit(ETR_EVENT_PORT_ALERT, &etr_events))
748 etr_port0_uptodate = etr_port1_uptodate = 0;
749
750 if (test_and_clear_bit(ETR_EVENT_PORT0_CHANGE, &etr_events)) {
751 if (eacr.e0)
752 /*
753 * Port change of an enabled port. We have to
754 * assume that this can have caused an stepping
755 * port switch.
756 */
757 etr_tolec = get_clock();
758 eacr.p0 = etr_port0_online;
759 if (!eacr.p0)
760 eacr.e0 = 0;
761 etr_port0_uptodate = 0;
762 }
763 if (test_and_clear_bit(ETR_EVENT_PORT1_CHANGE, &etr_events)) {
764 if (eacr.e1)
765 /*
766 * Port change of an enabled port. We have to
767 * assume that this can have caused an stepping
768 * port switch.
769 */
770 etr_tolec = get_clock();
771 eacr.p1 = etr_port1_online;
772 if (!eacr.p1)
773 eacr.e1 = 0;
774 etr_port1_uptodate = 0;
775 }
776 clear_bit(ETR_EVENT_UPDATE, &etr_events);
777 return eacr;
778}
779
780/*
781 * Set up a timer that expires after the etr_tolec + 1.6 seconds if
782 * one of the ports needs an update.
783 */
784static void etr_set_tolec_timeout(unsigned long long now)
785{
786 unsigned long micros;
787
788 if ((!etr_eacr.p0 || etr_port0_uptodate) &&
789 (!etr_eacr.p1 || etr_port1_uptodate))
790 return;
791 micros = (now > etr_tolec) ? ((now - etr_tolec) >> 12) : 0;
792 micros = (micros > 1600000) ? 0 : 1600000 - micros;
793 mod_timer(&etr_timer, jiffies + (micros * HZ) / 1000000 + 1);
794}
795
796/*
797 * Set up a time that expires after 1/2 second.
798 */
799static void etr_set_sync_timeout(void)
800{
801 mod_timer(&etr_timer, jiffies + HZ/2);
802}
803
804/*
805 * Update the aib information for one or both ports.
806 */
807static struct etr_eacr etr_handle_update(struct etr_aib *aib,
808 struct etr_eacr eacr)
809{
810 /* With both ports disabled the aib information is useless. */
811 if (!eacr.e0 && !eacr.e1)
812 return eacr;
813
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200814 /* Update port0 or port1 with aib stored in etr_work_fn. */
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100815 if (aib->esw.q == 0) {
816 /* Information for port 0 stored. */
817 if (eacr.p0 && !etr_port0_uptodate) {
818 etr_port0 = *aib;
819 if (etr_port0_online)
820 etr_port0_uptodate = 1;
821 }
822 } else {
823 /* Information for port 1 stored. */
824 if (eacr.p1 && !etr_port1_uptodate) {
825 etr_port1 = *aib;
826 if (etr_port0_online)
827 etr_port1_uptodate = 1;
828 }
829 }
830
831 /*
832 * Do not try to get the alternate port aib if the clock
833 * is not in sync yet.
834 */
835 if (!eacr.es)
836 return eacr;
837
838 /*
839 * If steai is available we can get the information about
840 * the other port immediately. If only stetr is available the
841 * data-port bit toggle has to be used.
842 */
843 if (test_bit(ETR_FLAG_STEAI, &etr_flags)) {
844 if (eacr.p0 && !etr_port0_uptodate) {
845 etr_steai_cv(&etr_port0, ETR_STEAI_PORT_0);
846 etr_port0_uptodate = 1;
847 }
848 if (eacr.p1 && !etr_port1_uptodate) {
849 etr_steai_cv(&etr_port1, ETR_STEAI_PORT_1);
850 etr_port1_uptodate = 1;
851 }
852 } else {
853 /*
854 * One port was updated above, if the other
855 * port is not uptodate toggle dp bit.
856 */
857 if ((eacr.p0 && !etr_port0_uptodate) ||
858 (eacr.p1 && !etr_port1_uptodate))
859 eacr.dp ^= 1;
860 else
861 eacr.dp = 0;
862 }
863 return eacr;
864}
865
866/*
867 * Write new etr control register if it differs from the current one.
868 * Return 1 if etr_tolec has been updated as well.
869 */
870static void etr_update_eacr(struct etr_eacr eacr)
871{
872 int dp_changed;
873
874 if (memcmp(&etr_eacr, &eacr, sizeof(eacr)) == 0)
875 /* No change, return. */
876 return;
877 /*
878 * The disable of an active port of the change of the data port
879 * bit can/will cause a change in the data port.
880 */
881 dp_changed = etr_eacr.e0 > eacr.e0 || etr_eacr.e1 > eacr.e1 ||
882 (etr_eacr.dp ^ eacr.dp) != 0;
883 etr_eacr = eacr;
884 etr_setr(&etr_eacr);
885 if (dp_changed)
886 etr_tolec = get_clock();
887}
888
889/*
890 * ETR tasklet. In this function you'll find the main logic. In
891 * particular this is the only function that calls etr_update_eacr(),
892 * it "controls" the etr control register.
893 */
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200894static void etr_work_fn(struct work_struct *work)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100895{
896 unsigned long long now;
897 struct etr_eacr eacr;
898 struct etr_aib aib;
899 int sync_port;
900
901 /* Create working copy of etr_eacr. */
902 eacr = etr_eacr;
903
904 /* Check for the different events and their immediate effects. */
905 eacr = etr_handle_events(eacr);
906
907 /* Check if ETR is supposed to be active. */
908 eacr.ea = eacr.p0 || eacr.p1;
909 if (!eacr.ea) {
910 /* Both ports offline. Reset everything. */
911 eacr.dp = eacr.es = eacr.sl = 0;
912 on_each_cpu(etr_disable_sync_clock, NULL, 0, 1);
913 del_timer_sync(&etr_timer);
914 etr_update_eacr(eacr);
915 set_bit(ETR_FLAG_EACCES, &etr_flags);
916 return;
917 }
918
919 /* Store aib to get the current ETR status word. */
920 BUG_ON(etr_stetr(&aib) != 0);
921 etr_port0.esw = etr_port1.esw = aib.esw; /* Copy status word. */
922 now = get_clock();
923
924 /*
925 * Update the port information if the last stepping port change
926 * or data port change is older than 1.6 seconds.
927 */
928 if (now >= etr_tolec + (1600000 << 12))
929 eacr = etr_handle_update(&aib, eacr);
930
931 /*
932 * Select ports to enable. The prefered synchronization mode is PPS.
933 * If a port can be enabled depends on a number of things:
934 * 1) The port needs to be online and uptodate. A port is not
935 * disabled just because it is not uptodate, but it is only
936 * enabled if it is uptodate.
937 * 2) The port needs to have the same mode (pps / etr).
938 * 3) The port needs to be usable -> etr_port_valid() == 1
939 * 4) To enable the second port the clock needs to be in sync.
940 * 5) If both ports are useable and are ETR ports, the network id
941 * has to be the same.
942 * The eacr.sl bit is used to indicate etr mode vs. pps mode.
943 */
944 if (eacr.p0 && aib.esw.psc0 == etr_lpsc_pps_mode) {
945 eacr.sl = 0;
946 eacr.e0 = 1;
947 if (!etr_mode_is_pps(etr_eacr))
948 eacr.es = 0;
949 if (!eacr.es || !eacr.p1 || aib.esw.psc1 != etr_lpsc_pps_mode)
950 eacr.e1 = 0;
951 // FIXME: uptodate checks ?
952 else if (etr_port0_uptodate && etr_port1_uptodate)
953 eacr.e1 = 1;
954 sync_port = (etr_port0_uptodate &&
955 etr_port_valid(&etr_port0, 0)) ? 0 : -1;
956 clear_bit(ETR_FLAG_EACCES, &etr_flags);
957 } else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_pps_mode) {
958 eacr.sl = 0;
959 eacr.e0 = 0;
960 eacr.e1 = 1;
961 if (!etr_mode_is_pps(etr_eacr))
962 eacr.es = 0;
963 sync_port = (etr_port1_uptodate &&
964 etr_port_valid(&etr_port1, 1)) ? 1 : -1;
965 clear_bit(ETR_FLAG_EACCES, &etr_flags);
966 } else if (eacr.p0 && aib.esw.psc0 == etr_lpsc_operational_step) {
967 eacr.sl = 1;
968 eacr.e0 = 1;
969 if (!etr_mode_is_etr(etr_eacr))
970 eacr.es = 0;
971 if (!eacr.es || !eacr.p1 ||
972 aib.esw.psc1 != etr_lpsc_operational_alt)
973 eacr.e1 = 0;
974 else if (etr_port0_uptodate && etr_port1_uptodate &&
975 etr_compare_network(&etr_port0, &etr_port1))
976 eacr.e1 = 1;
977 sync_port = (etr_port0_uptodate &&
978 etr_port_valid(&etr_port0, 0)) ? 0 : -1;
979 clear_bit(ETR_FLAG_EACCES, &etr_flags);
980 } else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_operational_step) {
981 eacr.sl = 1;
982 eacr.e0 = 0;
983 eacr.e1 = 1;
984 if (!etr_mode_is_etr(etr_eacr))
985 eacr.es = 0;
986 sync_port = (etr_port1_uptodate &&
987 etr_port_valid(&etr_port1, 1)) ? 1 : -1;
988 clear_bit(ETR_FLAG_EACCES, &etr_flags);
989 } else {
990 /* Both ports not usable. */
991 eacr.es = eacr.sl = 0;
992 sync_port = -1;
993 set_bit(ETR_FLAG_EACCES, &etr_flags);
994 }
995
996 /*
997 * If the clock is in sync just update the eacr and return.
998 * If there is no valid sync port wait for a port update.
999 */
1000 if (eacr.es || sync_port < 0) {
1001 etr_update_eacr(eacr);
1002 etr_set_tolec_timeout(now);
1003 return;
1004 }
1005
1006 /*
1007 * Prepare control register for clock syncing
1008 * (reset data port bit, set sync check control.
1009 */
1010 eacr.dp = 0;
1011 eacr.es = 1;
1012
1013 /*
1014 * Update eacr and try to synchronize the clock. If the update
1015 * of eacr caused a stepping port switch (or if we have to
1016 * assume that a stepping port switch has occured) or the
1017 * clock syncing failed, reset the sync check control bit
1018 * and set up a timer to try again after 0.5 seconds
1019 */
1020 etr_update_eacr(eacr);
1021 if (now < etr_tolec + (1600000 << 12) ||
1022 etr_sync_clock(&aib, sync_port) != 0) {
1023 /* Sync failed. Try again in 1/2 second. */
1024 eacr.es = 0;
1025 etr_update_eacr(eacr);
1026 etr_set_sync_timeout();
1027 } else
1028 etr_set_tolec_timeout(now);
1029}
1030
1031/*
1032 * Sysfs interface functions
1033 */
1034static struct sysdev_class etr_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001035 .name = "etr",
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001036};
1037
1038static struct sys_device etr_port0_dev = {
1039 .id = 0,
1040 .cls = &etr_sysclass,
1041};
1042
1043static struct sys_device etr_port1_dev = {
1044 .id = 1,
1045 .cls = &etr_sysclass,
1046};
1047
1048/*
1049 * ETR class attributes
1050 */
1051static ssize_t etr_stepping_port_show(struct sysdev_class *class, char *buf)
1052{
1053 return sprintf(buf, "%i\n", etr_port0.esw.p);
1054}
1055
1056static SYSDEV_CLASS_ATTR(stepping_port, 0400, etr_stepping_port_show, NULL);
1057
1058static ssize_t etr_stepping_mode_show(struct sysdev_class *class, char *buf)
1059{
1060 char *mode_str;
1061
1062 if (etr_mode_is_pps(etr_eacr))
1063 mode_str = "pps";
1064 else if (etr_mode_is_etr(etr_eacr))
1065 mode_str = "etr";
1066 else
1067 mode_str = "local";
1068 return sprintf(buf, "%s\n", mode_str);
1069}
1070
1071static SYSDEV_CLASS_ATTR(stepping_mode, 0400, etr_stepping_mode_show, NULL);
1072
1073/*
1074 * ETR port attributes
1075 */
1076static inline struct etr_aib *etr_aib_from_dev(struct sys_device *dev)
1077{
1078 if (dev == &etr_port0_dev)
1079 return etr_port0_online ? &etr_port0 : NULL;
1080 else
1081 return etr_port1_online ? &etr_port1 : NULL;
1082}
1083
1084static ssize_t etr_online_show(struct sys_device *dev, char *buf)
1085{
1086 unsigned int online;
1087
1088 online = (dev == &etr_port0_dev) ? etr_port0_online : etr_port1_online;
1089 return sprintf(buf, "%i\n", online);
1090}
1091
1092static ssize_t etr_online_store(struct sys_device *dev,
1093 const char *buf, size_t count)
1094{
1095 unsigned int value;
1096
1097 value = simple_strtoul(buf, NULL, 0);
1098 if (value != 0 && value != 1)
1099 return -EINVAL;
1100 if (test_bit(ETR_FLAG_ENOSYS, &etr_flags))
1101 return -ENOSYS;
1102 if (dev == &etr_port0_dev) {
1103 if (etr_port0_online == value)
1104 return count; /* Nothing to do. */
1105 etr_port0_online = value;
1106 set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
Martin Schwidefskyecdcc022007-04-27 16:01:58 +02001107 schedule_work(&etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001108 } else {
1109 if (etr_port1_online == value)
1110 return count; /* Nothing to do. */
1111 etr_port1_online = value;
1112 set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
Martin Schwidefskyecdcc022007-04-27 16:01:58 +02001113 schedule_work(&etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001114 }
1115 return count;
1116}
1117
1118static SYSDEV_ATTR(online, 0600, etr_online_show, etr_online_store);
1119
1120static ssize_t etr_stepping_control_show(struct sys_device *dev, char *buf)
1121{
1122 return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
1123 etr_eacr.e0 : etr_eacr.e1);
1124}
1125
1126static SYSDEV_ATTR(stepping_control, 0400, etr_stepping_control_show, NULL);
1127
1128static ssize_t etr_mode_code_show(struct sys_device *dev, char *buf)
1129{
1130 if (!etr_port0_online && !etr_port1_online)
1131 /* Status word is not uptodate if both ports are offline. */
1132 return -ENODATA;
1133 return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
1134 etr_port0.esw.psc0 : etr_port0.esw.psc1);
1135}
1136
1137static SYSDEV_ATTR(state_code, 0400, etr_mode_code_show, NULL);
1138
1139static ssize_t etr_untuned_show(struct sys_device *dev, char *buf)
1140{
1141 struct etr_aib *aib = etr_aib_from_dev(dev);
1142
1143 if (!aib || !aib->slsw.v1)
1144 return -ENODATA;
1145 return sprintf(buf, "%i\n", aib->edf1.u);
1146}
1147
1148static SYSDEV_ATTR(untuned, 0400, etr_untuned_show, NULL);
1149
1150static ssize_t etr_network_id_show(struct sys_device *dev, char *buf)
1151{
1152 struct etr_aib *aib = etr_aib_from_dev(dev);
1153
1154 if (!aib || !aib->slsw.v1)
1155 return -ENODATA;
1156 return sprintf(buf, "%i\n", aib->edf1.net_id);
1157}
1158
1159static SYSDEV_ATTR(network, 0400, etr_network_id_show, NULL);
1160
1161static ssize_t etr_id_show(struct sys_device *dev, char *buf)
1162{
1163 struct etr_aib *aib = etr_aib_from_dev(dev);
1164
1165 if (!aib || !aib->slsw.v1)
1166 return -ENODATA;
1167 return sprintf(buf, "%i\n", aib->edf1.etr_id);
1168}
1169
1170static SYSDEV_ATTR(id, 0400, etr_id_show, NULL);
1171
1172static ssize_t etr_port_number_show(struct sys_device *dev, char *buf)
1173{
1174 struct etr_aib *aib = etr_aib_from_dev(dev);
1175
1176 if (!aib || !aib->slsw.v1)
1177 return -ENODATA;
1178 return sprintf(buf, "%i\n", aib->edf1.etr_pn);
1179}
1180
1181static SYSDEV_ATTR(port, 0400, etr_port_number_show, NULL);
1182
1183static ssize_t etr_coupled_show(struct sys_device *dev, char *buf)
1184{
1185 struct etr_aib *aib = etr_aib_from_dev(dev);
1186
1187 if (!aib || !aib->slsw.v3)
1188 return -ENODATA;
1189 return sprintf(buf, "%i\n", aib->edf3.c);
1190}
1191
1192static SYSDEV_ATTR(coupled, 0400, etr_coupled_show, NULL);
1193
1194static ssize_t etr_local_time_show(struct sys_device *dev, char *buf)
1195{
1196 struct etr_aib *aib = etr_aib_from_dev(dev);
1197
1198 if (!aib || !aib->slsw.v3)
1199 return -ENODATA;
1200 return sprintf(buf, "%i\n", aib->edf3.blto);
1201}
1202
1203static SYSDEV_ATTR(local_time, 0400, etr_local_time_show, NULL);
1204
1205static ssize_t etr_utc_offset_show(struct sys_device *dev, char *buf)
1206{
1207 struct etr_aib *aib = etr_aib_from_dev(dev);
1208
1209 if (!aib || !aib->slsw.v3)
1210 return -ENODATA;
1211 return sprintf(buf, "%i\n", aib->edf3.buo);
1212}
1213
1214static SYSDEV_ATTR(utc_offset, 0400, etr_utc_offset_show, NULL);
1215
1216static struct sysdev_attribute *etr_port_attributes[] = {
1217 &attr_online,
1218 &attr_stepping_control,
1219 &attr_state_code,
1220 &attr_untuned,
1221 &attr_network,
1222 &attr_id,
1223 &attr_port,
1224 &attr_coupled,
1225 &attr_local_time,
1226 &attr_utc_offset,
1227 NULL
1228};
1229
1230static int __init etr_register_port(struct sys_device *dev)
1231{
1232 struct sysdev_attribute **attr;
1233 int rc;
1234
1235 rc = sysdev_register(dev);
1236 if (rc)
1237 goto out;
1238 for (attr = etr_port_attributes; *attr; attr++) {
1239 rc = sysdev_create_file(dev, *attr);
1240 if (rc)
1241 goto out_unreg;
1242 }
1243 return 0;
1244out_unreg:
1245 for (; attr >= etr_port_attributes; attr--)
1246 sysdev_remove_file(dev, *attr);
1247 sysdev_unregister(dev);
1248out:
1249 return rc;
1250}
1251
1252static void __init etr_unregister_port(struct sys_device *dev)
1253{
1254 struct sysdev_attribute **attr;
1255
1256 for (attr = etr_port_attributes; *attr; attr++)
1257 sysdev_remove_file(dev, *attr);
1258 sysdev_unregister(dev);
1259}
1260
1261static int __init etr_init_sysfs(void)
1262{
1263 int rc;
1264
1265 rc = sysdev_class_register(&etr_sysclass);
1266 if (rc)
1267 goto out;
1268 rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_port);
1269 if (rc)
1270 goto out_unreg_class;
1271 rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_mode);
1272 if (rc)
1273 goto out_remove_stepping_port;
1274 rc = etr_register_port(&etr_port0_dev);
1275 if (rc)
1276 goto out_remove_stepping_mode;
1277 rc = etr_register_port(&etr_port1_dev);
1278 if (rc)
1279 goto out_remove_port0;
1280 return 0;
1281
1282out_remove_port0:
1283 etr_unregister_port(&etr_port0_dev);
1284out_remove_stepping_mode:
1285 sysdev_class_remove_file(&etr_sysclass, &attr_stepping_mode);
1286out_remove_stepping_port:
1287 sysdev_class_remove_file(&etr_sysclass, &attr_stepping_port);
1288out_unreg_class:
1289 sysdev_class_unregister(&etr_sysclass);
1290out:
1291 return rc;
1292}
1293
1294device_initcall(etr_init_sysfs);