time.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  1. /*
  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. #include <linux/errno.h>
  15. #include <linux/module.h>
  16. #include <linux/sched.h>
  17. #include <linux/kernel.h>
  18. #include <linux/param.h>
  19. #include <linux/string.h>
  20. #include <linux/mm.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/time.h>
  23. #include <linux/sysdev.h>
  24. #include <linux/delay.h>
  25. #include <linux/init.h>
  26. #include <linux/smp.h>
  27. #include <linux/types.h>
  28. #include <linux/profile.h>
  29. #include <linux/timex.h>
  30. #include <linux/notifier.h>
  31. #include <linux/clocksource.h>
  32. #include <linux/clockchips.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/delay.h>
  35. #include <asm/s390_ext.h>
  36. #include <asm/div64.h>
  37. #include <asm/irq.h>
  38. #include <asm/irq_regs.h>
  39. #include <asm/timer.h>
  40. #include <asm/etr.h>
  41. /* change this if you have some constant time drift */
  42. #define USECS_PER_JIFFY ((unsigned long) 1000000/HZ)
  43. #define CLK_TICKS_PER_JIFFY ((unsigned long) USECS_PER_JIFFY << 12)
  44. /* The value of the TOD clock for 1.1.1970. */
  45. #define TOD_UNIX_EPOCH 0x7d91048bca000000ULL
  46. /*
  47. * Create a small time difference between the timer interrupts
  48. * on the different cpus to avoid lock contention.
  49. */
  50. #define CPU_DEVIATION (smp_processor_id() << 12)
  51. #define TICK_SIZE tick
  52. static ext_int_info_t ext_int_info_cc;
  53. static ext_int_info_t ext_int_etr_cc;
  54. static u64 jiffies_timer_cc;
  55. static DEFINE_PER_CPU(struct clock_event_device, comparators);
  56. /*
  57. * Scheduler clock - returns current time in nanosec units.
  58. */
  59. unsigned long long sched_clock(void)
  60. {
  61. return ((get_clock_xt() - jiffies_timer_cc) * 125) >> 9;
  62. }
  63. /*
  64. * Monotonic_clock - returns # of nanoseconds passed since time_init()
  65. */
  66. unsigned long long monotonic_clock(void)
  67. {
  68. return sched_clock();
  69. }
  70. EXPORT_SYMBOL(monotonic_clock);
  71. void tod_to_timeval(__u64 todval, struct timespec *xtime)
  72. {
  73. unsigned long long sec;
  74. sec = todval >> 12;
  75. do_div(sec, 1000000);
  76. xtime->tv_sec = sec;
  77. todval -= (sec * 1000000) << 12;
  78. xtime->tv_nsec = ((todval * 1000) >> 12);
  79. }
  80. #ifdef CONFIG_PROFILING
  81. #define s390_do_profile() profile_tick(CPU_PROFILING)
  82. #else
  83. #define s390_do_profile() do { ; } while(0)
  84. #endif /* CONFIG_PROFILING */
  85. void clock_comparator_work(void)
  86. {
  87. struct clock_event_device *cd;
  88. S390_lowcore.clock_comparator = -1ULL;
  89. set_clock_comparator(S390_lowcore.clock_comparator);
  90. cd = &__get_cpu_var(comparators);
  91. cd->event_handler(cd);
  92. s390_do_profile();
  93. }
  94. /*
  95. * Fixup the clock comparator.
  96. */
  97. static void fixup_clock_comparator(unsigned long long delta)
  98. {
  99. /* If nobody is waiting there's nothing to fix. */
  100. if (S390_lowcore.clock_comparator == -1ULL)
  101. return;
  102. S390_lowcore.clock_comparator += delta;
  103. set_clock_comparator(S390_lowcore.clock_comparator);
  104. }
  105. static int s390_next_event(unsigned long delta,
  106. struct clock_event_device *evt)
  107. {
  108. S390_lowcore.clock_comparator = get_clock() + delta;
  109. set_clock_comparator(S390_lowcore.clock_comparator);
  110. return 0;
  111. }
  112. static void s390_set_mode(enum clock_event_mode mode,
  113. struct clock_event_device *evt)
  114. {
  115. }
  116. /*
  117. * Set up lowcore and control register of the current cpu to
  118. * enable TOD clock and clock comparator interrupts.
  119. */
  120. void init_cpu_timer(void)
  121. {
  122. struct clock_event_device *cd;
  123. int cpu;
  124. S390_lowcore.clock_comparator = -1ULL;
  125. set_clock_comparator(S390_lowcore.clock_comparator);
  126. cpu = smp_processor_id();
  127. cd = &per_cpu(comparators, cpu);
  128. cd->name = "comparator";
  129. cd->features = CLOCK_EVT_FEAT_ONESHOT;
  130. cd->mult = 16777;
  131. cd->shift = 12;
  132. cd->min_delta_ns = 1;
  133. cd->max_delta_ns = LONG_MAX;
  134. cd->rating = 400;
  135. cd->cpumask = cpumask_of_cpu(cpu);
  136. cd->set_next_event = s390_next_event;
  137. cd->set_mode = s390_set_mode;
  138. clockevents_register_device(cd);
  139. /* Enable clock comparator timer interrupt. */
  140. __ctl_set_bit(0,11);
  141. /* Always allow ETR external interrupts, even without an ETR. */
  142. __ctl_set_bit(0, 4);
  143. }
  144. static void clock_comparator_interrupt(__u16 code)
  145. {
  146. }
  147. static void etr_reset(void);
  148. static void etr_ext_handler(__u16);
  149. /*
  150. * Get the TOD clock running.
  151. */
  152. static u64 __init reset_tod_clock(void)
  153. {
  154. u64 time;
  155. etr_reset();
  156. if (store_clock(&time) == 0)
  157. return time;
  158. /* TOD clock not running. Set the clock to Unix Epoch. */
  159. if (set_clock(TOD_UNIX_EPOCH) != 0 || store_clock(&time) != 0)
  160. panic("TOD clock not operational.");
  161. return TOD_UNIX_EPOCH;
  162. }
  163. static cycle_t read_tod_clock(void)
  164. {
  165. return get_clock();
  166. }
  167. static struct clocksource clocksource_tod = {
  168. .name = "tod",
  169. .rating = 400,
  170. .read = read_tod_clock,
  171. .mask = -1ULL,
  172. .mult = 1000,
  173. .shift = 12,
  174. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  175. };
  176. /*
  177. * Initialize the TOD clock and the CPU timer of
  178. * the boot cpu.
  179. */
  180. void __init time_init(void)
  181. {
  182. u64 init_timer_cc;
  183. init_timer_cc = reset_tod_clock();
  184. jiffies_timer_cc = init_timer_cc - jiffies_64 * CLK_TICKS_PER_JIFFY;
  185. /* set xtime */
  186. tod_to_timeval(init_timer_cc - TOD_UNIX_EPOCH, &xtime);
  187. set_normalized_timespec(&wall_to_monotonic,
  188. -xtime.tv_sec, -xtime.tv_nsec);
  189. /* request the clock comparator external interrupt */
  190. if (register_early_external_interrupt(0x1004,
  191. clock_comparator_interrupt,
  192. &ext_int_info_cc) != 0)
  193. panic("Couldn't request external interrupt 0x1004");
  194. if (clocksource_register(&clocksource_tod) != 0)
  195. panic("Could not register TOD clock source");
  196. /* request the etr external interrupt */
  197. if (register_early_external_interrupt(0x1406, etr_ext_handler,
  198. &ext_int_etr_cc) != 0)
  199. panic("Couldn't request external interrupt 0x1406");
  200. /* Enable TOD clock interrupts on the boot cpu. */
  201. init_cpu_timer();
  202. #ifdef CONFIG_VIRT_TIMER
  203. vtime_init();
  204. #endif
  205. }
  206. /*
  207. * External Time Reference (ETR) code.
  208. */
  209. static int etr_port0_online;
  210. static int etr_port1_online;
  211. static int __init early_parse_etr(char *p)
  212. {
  213. if (strncmp(p, "off", 3) == 0)
  214. etr_port0_online = etr_port1_online = 0;
  215. else if (strncmp(p, "port0", 5) == 0)
  216. etr_port0_online = 1;
  217. else if (strncmp(p, "port1", 5) == 0)
  218. etr_port1_online = 1;
  219. else if (strncmp(p, "on", 2) == 0)
  220. etr_port0_online = etr_port1_online = 1;
  221. return 0;
  222. }
  223. early_param("etr", early_parse_etr);
  224. enum etr_event {
  225. ETR_EVENT_PORT0_CHANGE,
  226. ETR_EVENT_PORT1_CHANGE,
  227. ETR_EVENT_PORT_ALERT,
  228. ETR_EVENT_SYNC_CHECK,
  229. ETR_EVENT_SWITCH_LOCAL,
  230. ETR_EVENT_UPDATE,
  231. };
  232. enum etr_flags {
  233. ETR_FLAG_ENOSYS,
  234. ETR_FLAG_EACCES,
  235. ETR_FLAG_STEAI,
  236. };
  237. /*
  238. * Valid bit combinations of the eacr register are (x = don't care):
  239. * e0 e1 dp p0 p1 ea es sl
  240. * 0 0 x 0 0 0 0 0 initial, disabled state
  241. * 0 0 x 0 1 1 0 0 port 1 online
  242. * 0 0 x 1 0 1 0 0 port 0 online
  243. * 0 0 x 1 1 1 0 0 both ports online
  244. * 0 1 x 0 1 1 0 0 port 1 online and usable, ETR or PPS mode
  245. * 0 1 x 0 1 1 0 1 port 1 online, usable and ETR mode
  246. * 0 1 x 0 1 1 1 0 port 1 online, usable, PPS mode, in-sync
  247. * 0 1 x 0 1 1 1 1 port 1 online, usable, ETR mode, in-sync
  248. * 0 1 x 1 1 1 0 0 both ports online, port 1 usable
  249. * 0 1 x 1 1 1 1 0 both ports online, port 1 usable, PPS mode, in-sync
  250. * 0 1 x 1 1 1 1 1 both ports online, port 1 usable, ETR mode, in-sync
  251. * 1 0 x 1 0 1 0 0 port 0 online and usable, ETR or PPS mode
  252. * 1 0 x 1 0 1 0 1 port 0 online, usable and ETR mode
  253. * 1 0 x 1 0 1 1 0 port 0 online, usable, PPS mode, in-sync
  254. * 1 0 x 1 0 1 1 1 port 0 online, usable, ETR mode, in-sync
  255. * 1 0 x 1 1 1 0 0 both ports online, port 0 usable
  256. * 1 0 x 1 1 1 1 0 both ports online, port 0 usable, PPS mode, in-sync
  257. * 1 0 x 1 1 1 1 1 both ports online, port 0 usable, ETR mode, in-sync
  258. * 1 1 x 1 1 1 1 0 both ports online & usable, ETR, in-sync
  259. * 1 1 x 1 1 1 1 1 both ports online & usable, ETR, in-sync
  260. */
  261. static struct etr_eacr etr_eacr;
  262. static u64 etr_tolec; /* time of last eacr update */
  263. static unsigned long etr_flags;
  264. static struct etr_aib etr_port0;
  265. static int etr_port0_uptodate;
  266. static struct etr_aib etr_port1;
  267. static int etr_port1_uptodate;
  268. static unsigned long etr_events;
  269. static struct timer_list etr_timer;
  270. static DEFINE_PER_CPU(atomic_t, etr_sync_word);
  271. static void etr_timeout(unsigned long dummy);
  272. static void etr_work_fn(struct work_struct *work);
  273. static DECLARE_WORK(etr_work, etr_work_fn);
  274. /*
  275. * The etr get_clock function. It will write the current clock value
  276. * to the clock pointer and return 0 if the clock is in sync with the
  277. * external time source. If the clock mode is local it will return
  278. * -ENOSYS and -EAGAIN if the clock is not in sync with the external
  279. * reference. This function is what ETR is all about..
  280. */
  281. int get_sync_clock(unsigned long long *clock)
  282. {
  283. atomic_t *sw_ptr;
  284. unsigned int sw0, sw1;
  285. sw_ptr = &get_cpu_var(etr_sync_word);
  286. sw0 = atomic_read(sw_ptr);
  287. *clock = get_clock();
  288. sw1 = atomic_read(sw_ptr);
  289. put_cpu_var(etr_sync_sync);
  290. if (sw0 == sw1 && (sw0 & 0x80000000U))
  291. /* Success: time is in sync. */
  292. return 0;
  293. if (test_bit(ETR_FLAG_ENOSYS, &etr_flags))
  294. return -ENOSYS;
  295. if (test_bit(ETR_FLAG_EACCES, &etr_flags))
  296. return -EACCES;
  297. return -EAGAIN;
  298. }
  299. EXPORT_SYMBOL(get_sync_clock);
  300. /*
  301. * Make get_sync_clock return -EAGAIN.
  302. */
  303. static void etr_disable_sync_clock(void *dummy)
  304. {
  305. atomic_t *sw_ptr = &__get_cpu_var(etr_sync_word);
  306. /*
  307. * Clear the in-sync bit 2^31. All get_sync_clock calls will
  308. * fail until the sync bit is turned back on. In addition
  309. * increase the "sequence" counter to avoid the race of an
  310. * etr event and the complete recovery against get_sync_clock.
  311. */
  312. atomic_clear_mask(0x80000000, sw_ptr);
  313. atomic_inc(sw_ptr);
  314. }
  315. /*
  316. * Make get_sync_clock return 0 again.
  317. * Needs to be called from a context disabled for preemption.
  318. */
  319. static void etr_enable_sync_clock(void)
  320. {
  321. atomic_t *sw_ptr = &__get_cpu_var(etr_sync_word);
  322. atomic_set_mask(0x80000000, sw_ptr);
  323. }
  324. /*
  325. * Reset ETR attachment.
  326. */
  327. static void etr_reset(void)
  328. {
  329. etr_eacr = (struct etr_eacr) {
  330. .e0 = 0, .e1 = 0, ._pad0 = 4, .dp = 0,
  331. .p0 = 0, .p1 = 0, ._pad1 = 0, .ea = 0,
  332. .es = 0, .sl = 0 };
  333. if (etr_setr(&etr_eacr) == 0)
  334. etr_tolec = get_clock();
  335. else {
  336. set_bit(ETR_FLAG_ENOSYS, &etr_flags);
  337. if (etr_port0_online || etr_port1_online) {
  338. printk(KERN_WARNING "Running on non ETR capable "
  339. "machine, only local mode available.\n");
  340. etr_port0_online = etr_port1_online = 0;
  341. }
  342. }
  343. }
  344. static int __init etr_init(void)
  345. {
  346. struct etr_aib aib;
  347. if (test_bit(ETR_FLAG_ENOSYS, &etr_flags))
  348. return 0;
  349. /* Check if this machine has the steai instruction. */
  350. if (etr_steai(&aib, ETR_STEAI_STEPPING_PORT) == 0)
  351. set_bit(ETR_FLAG_STEAI, &etr_flags);
  352. setup_timer(&etr_timer, etr_timeout, 0UL);
  353. if (!etr_port0_online && !etr_port1_online)
  354. set_bit(ETR_FLAG_EACCES, &etr_flags);
  355. if (etr_port0_online) {
  356. set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
  357. schedule_work(&etr_work);
  358. }
  359. if (etr_port1_online) {
  360. set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
  361. schedule_work(&etr_work);
  362. }
  363. return 0;
  364. }
  365. arch_initcall(etr_init);
  366. /*
  367. * Two sorts of ETR machine checks. The architecture reads:
  368. * "When a machine-check niterruption occurs and if a switch-to-local or
  369. * ETR-sync-check interrupt request is pending but disabled, this pending
  370. * disabled interruption request is indicated and is cleared".
  371. * Which means that we can get etr_switch_to_local events from the machine
  372. * check handler although the interruption condition is disabled. Lovely..
  373. */
  374. /*
  375. * Switch to local machine check. This is called when the last usable
  376. * ETR port goes inactive. After switch to local the clock is not in sync.
  377. */
  378. void etr_switch_to_local(void)
  379. {
  380. if (!etr_eacr.sl)
  381. return;
  382. etr_disable_sync_clock(NULL);
  383. set_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events);
  384. schedule_work(&etr_work);
  385. }
  386. /*
  387. * ETR sync check machine check. This is called when the ETR OTE and the
  388. * local clock OTE are farther apart than the ETR sync check tolerance.
  389. * After a ETR sync check the clock is not in sync. The machine check
  390. * is broadcasted to all cpus at the same time.
  391. */
  392. void etr_sync_check(void)
  393. {
  394. if (!etr_eacr.es)
  395. return;
  396. etr_disable_sync_clock(NULL);
  397. set_bit(ETR_EVENT_SYNC_CHECK, &etr_events);
  398. schedule_work(&etr_work);
  399. }
  400. /*
  401. * ETR external interrupt. There are two causes:
  402. * 1) port state change, check the usability of the port
  403. * 2) port alert, one of the ETR-data-validity bits (v1-v2 bits of the
  404. * sldr-status word) or ETR-data word 1 (edf1) or ETR-data word 3 (edf3)
  405. * or ETR-data word 4 (edf4) has changed.
  406. */
  407. static void etr_ext_handler(__u16 code)
  408. {
  409. struct etr_interruption_parameter *intparm =
  410. (struct etr_interruption_parameter *) &S390_lowcore.ext_params;
  411. if (intparm->pc0)
  412. /* ETR port 0 state change. */
  413. set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
  414. if (intparm->pc1)
  415. /* ETR port 1 state change. */
  416. set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
  417. if (intparm->eai)
  418. /*
  419. * ETR port alert on either port 0, 1 or both.
  420. * Both ports are not up-to-date now.
  421. */
  422. set_bit(ETR_EVENT_PORT_ALERT, &etr_events);
  423. schedule_work(&etr_work);
  424. }
  425. static void etr_timeout(unsigned long dummy)
  426. {
  427. set_bit(ETR_EVENT_UPDATE, &etr_events);
  428. schedule_work(&etr_work);
  429. }
  430. /*
  431. * Check if the etr mode is pss.
  432. */
  433. static inline int etr_mode_is_pps(struct etr_eacr eacr)
  434. {
  435. return eacr.es && !eacr.sl;
  436. }
  437. /*
  438. * Check if the etr mode is etr.
  439. */
  440. static inline int etr_mode_is_etr(struct etr_eacr eacr)
  441. {
  442. return eacr.es && eacr.sl;
  443. }
  444. /*
  445. * Check if the port can be used for TOD synchronization.
  446. * For PPS mode the port has to receive OTEs. For ETR mode
  447. * the port has to receive OTEs, the ETR stepping bit has to
  448. * be zero and the validity bits for data frame 1, 2, and 3
  449. * have to be 1.
  450. */
  451. static int etr_port_valid(struct etr_aib *aib, int port)
  452. {
  453. unsigned int psc;
  454. /* Check that this port is receiving OTEs. */
  455. if (aib->tsp == 0)
  456. return 0;
  457. psc = port ? aib->esw.psc1 : aib->esw.psc0;
  458. if (psc == etr_lpsc_pps_mode)
  459. return 1;
  460. if (psc == etr_lpsc_operational_step)
  461. return !aib->esw.y && aib->slsw.v1 &&
  462. aib->slsw.v2 && aib->slsw.v3;
  463. return 0;
  464. }
  465. /*
  466. * Check if two ports are on the same network.
  467. */
  468. static int etr_compare_network(struct etr_aib *aib1, struct etr_aib *aib2)
  469. {
  470. // FIXME: any other fields we have to compare?
  471. return aib1->edf1.net_id == aib2->edf1.net_id;
  472. }
  473. /*
  474. * Wrapper for etr_stei that converts physical port states
  475. * to logical port states to be consistent with the output
  476. * of stetr (see etr_psc vs. etr_lpsc).
  477. */
  478. static void etr_steai_cv(struct etr_aib *aib, unsigned int func)
  479. {
  480. BUG_ON(etr_steai(aib, func) != 0);
  481. /* Convert port state to logical port state. */
  482. if (aib->esw.psc0 == 1)
  483. aib->esw.psc0 = 2;
  484. else if (aib->esw.psc0 == 0 && aib->esw.p == 0)
  485. aib->esw.psc0 = 1;
  486. if (aib->esw.psc1 == 1)
  487. aib->esw.psc1 = 2;
  488. else if (aib->esw.psc1 == 0 && aib->esw.p == 1)
  489. aib->esw.psc1 = 1;
  490. }
  491. /*
  492. * Check if the aib a2 is still connected to the same attachment as
  493. * aib a1, the etv values differ by one and a2 is valid.
  494. */
  495. static int etr_aib_follows(struct etr_aib *a1, struct etr_aib *a2, int p)
  496. {
  497. int state_a1, state_a2;
  498. /* Paranoia check: e0/e1 should better be the same. */
  499. if (a1->esw.eacr.e0 != a2->esw.eacr.e0 ||
  500. a1->esw.eacr.e1 != a2->esw.eacr.e1)
  501. return 0;
  502. /* Still connected to the same etr ? */
  503. state_a1 = p ? a1->esw.psc1 : a1->esw.psc0;
  504. state_a2 = p ? a2->esw.psc1 : a2->esw.psc0;
  505. if (state_a1 == etr_lpsc_operational_step) {
  506. if (state_a2 != etr_lpsc_operational_step ||
  507. a1->edf1.net_id != a2->edf1.net_id ||
  508. a1->edf1.etr_id != a2->edf1.etr_id ||
  509. a1->edf1.etr_pn != a2->edf1.etr_pn)
  510. return 0;
  511. } else if (state_a2 != etr_lpsc_pps_mode)
  512. return 0;
  513. /* The ETV value of a2 needs to be ETV of a1 + 1. */
  514. if (a1->edf2.etv + 1 != a2->edf2.etv)
  515. return 0;
  516. if (!etr_port_valid(a2, p))
  517. return 0;
  518. return 1;
  519. }
  520. /*
  521. * The time is "clock". old is what we think the time is.
  522. * Adjust the value by a multiple of jiffies and add the delta to ntp.
  523. * "delay" is an approximation how long the synchronization took. If
  524. * the time correction is positive, then "delay" is subtracted from
  525. * the time difference and only the remaining part is passed to ntp.
  526. */
  527. static unsigned long long etr_adjust_time(unsigned long long old,
  528. unsigned long long clock,
  529. unsigned long long delay)
  530. {
  531. unsigned long long delta, ticks;
  532. struct timex adjust;
  533. if (clock > old) {
  534. /* It is later than we thought. */
  535. delta = ticks = clock - old;
  536. delta = ticks = (delta < delay) ? 0 : delta - delay;
  537. delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
  538. adjust.offset = ticks * (1000000 / HZ);
  539. } else {
  540. /* It is earlier than we thought. */
  541. delta = ticks = old - clock;
  542. delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
  543. delta = -delta;
  544. adjust.offset = -ticks * (1000000 / HZ);
  545. }
  546. jiffies_timer_cc += delta;
  547. if (adjust.offset != 0) {
  548. printk(KERN_NOTICE "etr: time adjusted by %li micro-seconds\n",
  549. adjust.offset);
  550. adjust.modes = ADJ_OFFSET_SINGLESHOT;
  551. do_adjtimex(&adjust);
  552. }
  553. return delta;
  554. }
  555. static struct {
  556. int in_sync;
  557. unsigned long long fixup_cc;
  558. } etr_sync;
  559. static void etr_sync_cpu_start(void *dummy)
  560. {
  561. etr_enable_sync_clock();
  562. /*
  563. * This looks like a busy wait loop but it isn't. etr_sync_cpus
  564. * is called on all other cpus while the TOD clocks is stopped.
  565. * __udelay will stop the cpu on an enabled wait psw until the
  566. * TOD is running again.
  567. */
  568. while (etr_sync.in_sync == 0) {
  569. __udelay(1);
  570. /*
  571. * A different cpu changes *in_sync. Therefore use
  572. * barrier() to force memory access.
  573. */
  574. barrier();
  575. }
  576. if (etr_sync.in_sync != 1)
  577. /* Didn't work. Clear per-cpu in sync bit again. */
  578. etr_disable_sync_clock(NULL);
  579. /*
  580. * This round of TOD syncing is done. Set the clock comparator
  581. * to the next tick and let the processor continue.
  582. */
  583. fixup_clock_comparator(etr_sync.fixup_cc);
  584. }
  585. static void etr_sync_cpu_end(void *dummy)
  586. {
  587. }
  588. /*
  589. * Sync the TOD clock using the port refered to by aibp. This port
  590. * has to be enabled and the other port has to be disabled. The
  591. * last eacr update has to be more than 1.6 seconds in the past.
  592. */
  593. static int etr_sync_clock(struct etr_aib *aib, int port)
  594. {
  595. struct etr_aib *sync_port;
  596. unsigned long long clock, old_clock, delay, delta;
  597. int follows;
  598. int rc;
  599. /* Check if the current aib is adjacent to the sync port aib. */
  600. sync_port = (port == 0) ? &etr_port0 : &etr_port1;
  601. follows = etr_aib_follows(sync_port, aib, port);
  602. memcpy(sync_port, aib, sizeof(*aib));
  603. if (!follows)
  604. return -EAGAIN;
  605. /*
  606. * Catch all other cpus and make them wait until we have
  607. * successfully synced the clock. smp_call_function will
  608. * return after all other cpus are in etr_sync_cpu_start.
  609. */
  610. memset(&etr_sync, 0, sizeof(etr_sync));
  611. preempt_disable();
  612. smp_call_function(etr_sync_cpu_start, NULL, 0, 0);
  613. local_irq_disable();
  614. etr_enable_sync_clock();
  615. /* Set clock to next OTE. */
  616. __ctl_set_bit(14, 21);
  617. __ctl_set_bit(0, 29);
  618. clock = ((unsigned long long) (aib->edf2.etv + 1)) << 32;
  619. old_clock = get_clock();
  620. if (set_clock(clock) == 0) {
  621. __udelay(1); /* Wait for the clock to start. */
  622. __ctl_clear_bit(0, 29);
  623. __ctl_clear_bit(14, 21);
  624. etr_stetr(aib);
  625. /* Adjust Linux timing variables. */
  626. delay = (unsigned long long)
  627. (aib->edf2.etv - sync_port->edf2.etv) << 32;
  628. delta = etr_adjust_time(old_clock, clock, delay);
  629. etr_sync.fixup_cc = delta;
  630. fixup_clock_comparator(delta);
  631. /* Verify that the clock is properly set. */
  632. if (!etr_aib_follows(sync_port, aib, port)) {
  633. /* Didn't work. */
  634. etr_disable_sync_clock(NULL);
  635. etr_sync.in_sync = -EAGAIN;
  636. rc = -EAGAIN;
  637. } else {
  638. etr_sync.in_sync = 1;
  639. rc = 0;
  640. }
  641. } else {
  642. /* Could not set the clock ?!? */
  643. __ctl_clear_bit(0, 29);
  644. __ctl_clear_bit(14, 21);
  645. etr_disable_sync_clock(NULL);
  646. etr_sync.in_sync = -EAGAIN;
  647. rc = -EAGAIN;
  648. }
  649. local_irq_enable();
  650. smp_call_function(etr_sync_cpu_end,NULL,0,0);
  651. preempt_enable();
  652. return rc;
  653. }
  654. /*
  655. * Handle the immediate effects of the different events.
  656. * The port change event is used for online/offline changes.
  657. */
  658. static struct etr_eacr etr_handle_events(struct etr_eacr eacr)
  659. {
  660. if (test_and_clear_bit(ETR_EVENT_SYNC_CHECK, &etr_events))
  661. eacr.es = 0;
  662. if (test_and_clear_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events))
  663. eacr.es = eacr.sl = 0;
  664. if (test_and_clear_bit(ETR_EVENT_PORT_ALERT, &etr_events))
  665. etr_port0_uptodate = etr_port1_uptodate = 0;
  666. if (test_and_clear_bit(ETR_EVENT_PORT0_CHANGE, &etr_events)) {
  667. if (eacr.e0)
  668. /*
  669. * Port change of an enabled port. We have to
  670. * assume that this can have caused an stepping
  671. * port switch.
  672. */
  673. etr_tolec = get_clock();
  674. eacr.p0 = etr_port0_online;
  675. if (!eacr.p0)
  676. eacr.e0 = 0;
  677. etr_port0_uptodate = 0;
  678. }
  679. if (test_and_clear_bit(ETR_EVENT_PORT1_CHANGE, &etr_events)) {
  680. if (eacr.e1)
  681. /*
  682. * Port change of an enabled port. We have to
  683. * assume that this can have caused an stepping
  684. * port switch.
  685. */
  686. etr_tolec = get_clock();
  687. eacr.p1 = etr_port1_online;
  688. if (!eacr.p1)
  689. eacr.e1 = 0;
  690. etr_port1_uptodate = 0;
  691. }
  692. clear_bit(ETR_EVENT_UPDATE, &etr_events);
  693. return eacr;
  694. }
  695. /*
  696. * Set up a timer that expires after the etr_tolec + 1.6 seconds if
  697. * one of the ports needs an update.
  698. */
  699. static void etr_set_tolec_timeout(unsigned long long now)
  700. {
  701. unsigned long micros;
  702. if ((!etr_eacr.p0 || etr_port0_uptodate) &&
  703. (!etr_eacr.p1 || etr_port1_uptodate))
  704. return;
  705. micros = (now > etr_tolec) ? ((now - etr_tolec) >> 12) : 0;
  706. micros = (micros > 1600000) ? 0 : 1600000 - micros;
  707. mod_timer(&etr_timer, jiffies + (micros * HZ) / 1000000 + 1);
  708. }
  709. /*
  710. * Set up a time that expires after 1/2 second.
  711. */
  712. static void etr_set_sync_timeout(void)
  713. {
  714. mod_timer(&etr_timer, jiffies + HZ/2);
  715. }
  716. /*
  717. * Update the aib information for one or both ports.
  718. */
  719. static struct etr_eacr etr_handle_update(struct etr_aib *aib,
  720. struct etr_eacr eacr)
  721. {
  722. /* With both ports disabled the aib information is useless. */
  723. if (!eacr.e0 && !eacr.e1)
  724. return eacr;
  725. /* Update port0 or port1 with aib stored in etr_work_fn. */
  726. if (aib->esw.q == 0) {
  727. /* Information for port 0 stored. */
  728. if (eacr.p0 && !etr_port0_uptodate) {
  729. etr_port0 = *aib;
  730. if (etr_port0_online)
  731. etr_port0_uptodate = 1;
  732. }
  733. } else {
  734. /* Information for port 1 stored. */
  735. if (eacr.p1 && !etr_port1_uptodate) {
  736. etr_port1 = *aib;
  737. if (etr_port0_online)
  738. etr_port1_uptodate = 1;
  739. }
  740. }
  741. /*
  742. * Do not try to get the alternate port aib if the clock
  743. * is not in sync yet.
  744. */
  745. if (!eacr.es)
  746. return eacr;
  747. /*
  748. * If steai is available we can get the information about
  749. * the other port immediately. If only stetr is available the
  750. * data-port bit toggle has to be used.
  751. */
  752. if (test_bit(ETR_FLAG_STEAI, &etr_flags)) {
  753. if (eacr.p0 && !etr_port0_uptodate) {
  754. etr_steai_cv(&etr_port0, ETR_STEAI_PORT_0);
  755. etr_port0_uptodate = 1;
  756. }
  757. if (eacr.p1 && !etr_port1_uptodate) {
  758. etr_steai_cv(&etr_port1, ETR_STEAI_PORT_1);
  759. etr_port1_uptodate = 1;
  760. }
  761. } else {
  762. /*
  763. * One port was updated above, if the other
  764. * port is not uptodate toggle dp bit.
  765. */
  766. if ((eacr.p0 && !etr_port0_uptodate) ||
  767. (eacr.p1 && !etr_port1_uptodate))
  768. eacr.dp ^= 1;
  769. else
  770. eacr.dp = 0;
  771. }
  772. return eacr;
  773. }
  774. /*
  775. * Write new etr control register if it differs from the current one.
  776. * Return 1 if etr_tolec has been updated as well.
  777. */
  778. static void etr_update_eacr(struct etr_eacr eacr)
  779. {
  780. int dp_changed;
  781. if (memcmp(&etr_eacr, &eacr, sizeof(eacr)) == 0)
  782. /* No change, return. */
  783. return;
  784. /*
  785. * The disable of an active port of the change of the data port
  786. * bit can/will cause a change in the data port.
  787. */
  788. dp_changed = etr_eacr.e0 > eacr.e0 || etr_eacr.e1 > eacr.e1 ||
  789. (etr_eacr.dp ^ eacr.dp) != 0;
  790. etr_eacr = eacr;
  791. etr_setr(&etr_eacr);
  792. if (dp_changed)
  793. etr_tolec = get_clock();
  794. }
  795. /*
  796. * ETR tasklet. In this function you'll find the main logic. In
  797. * particular this is the only function that calls etr_update_eacr(),
  798. * it "controls" the etr control register.
  799. */
  800. static void etr_work_fn(struct work_struct *work)
  801. {
  802. unsigned long long now;
  803. struct etr_eacr eacr;
  804. struct etr_aib aib;
  805. int sync_port;
  806. /* Create working copy of etr_eacr. */
  807. eacr = etr_eacr;
  808. /* Check for the different events and their immediate effects. */
  809. eacr = etr_handle_events(eacr);
  810. /* Check if ETR is supposed to be active. */
  811. eacr.ea = eacr.p0 || eacr.p1;
  812. if (!eacr.ea) {
  813. /* Both ports offline. Reset everything. */
  814. eacr.dp = eacr.es = eacr.sl = 0;
  815. on_each_cpu(etr_disable_sync_clock, NULL, 0, 1);
  816. del_timer_sync(&etr_timer);
  817. etr_update_eacr(eacr);
  818. set_bit(ETR_FLAG_EACCES, &etr_flags);
  819. return;
  820. }
  821. /* Store aib to get the current ETR status word. */
  822. BUG_ON(etr_stetr(&aib) != 0);
  823. etr_port0.esw = etr_port1.esw = aib.esw; /* Copy status word. */
  824. now = get_clock();
  825. /*
  826. * Update the port information if the last stepping port change
  827. * or data port change is older than 1.6 seconds.
  828. */
  829. if (now >= etr_tolec + (1600000 << 12))
  830. eacr = etr_handle_update(&aib, eacr);
  831. /*
  832. * Select ports to enable. The prefered synchronization mode is PPS.
  833. * If a port can be enabled depends on a number of things:
  834. * 1) The port needs to be online and uptodate. A port is not
  835. * disabled just because it is not uptodate, but it is only
  836. * enabled if it is uptodate.
  837. * 2) The port needs to have the same mode (pps / etr).
  838. * 3) The port needs to be usable -> etr_port_valid() == 1
  839. * 4) To enable the second port the clock needs to be in sync.
  840. * 5) If both ports are useable and are ETR ports, the network id
  841. * has to be the same.
  842. * The eacr.sl bit is used to indicate etr mode vs. pps mode.
  843. */
  844. if (eacr.p0 && aib.esw.psc0 == etr_lpsc_pps_mode) {
  845. eacr.sl = 0;
  846. eacr.e0 = 1;
  847. if (!etr_mode_is_pps(etr_eacr))
  848. eacr.es = 0;
  849. if (!eacr.es || !eacr.p1 || aib.esw.psc1 != etr_lpsc_pps_mode)
  850. eacr.e1 = 0;
  851. // FIXME: uptodate checks ?
  852. else if (etr_port0_uptodate && etr_port1_uptodate)
  853. eacr.e1 = 1;
  854. sync_port = (etr_port0_uptodate &&
  855. etr_port_valid(&etr_port0, 0)) ? 0 : -1;
  856. clear_bit(ETR_FLAG_EACCES, &etr_flags);
  857. } else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_pps_mode) {
  858. eacr.sl = 0;
  859. eacr.e0 = 0;
  860. eacr.e1 = 1;
  861. if (!etr_mode_is_pps(etr_eacr))
  862. eacr.es = 0;
  863. sync_port = (etr_port1_uptodate &&
  864. etr_port_valid(&etr_port1, 1)) ? 1 : -1;
  865. clear_bit(ETR_FLAG_EACCES, &etr_flags);
  866. } else if (eacr.p0 && aib.esw.psc0 == etr_lpsc_operational_step) {
  867. eacr.sl = 1;
  868. eacr.e0 = 1;
  869. if (!etr_mode_is_etr(etr_eacr))
  870. eacr.es = 0;
  871. if (!eacr.es || !eacr.p1 ||
  872. aib.esw.psc1 != etr_lpsc_operational_alt)
  873. eacr.e1 = 0;
  874. else if (etr_port0_uptodate && etr_port1_uptodate &&
  875. etr_compare_network(&etr_port0, &etr_port1))
  876. eacr.e1 = 1;
  877. sync_port = (etr_port0_uptodate &&
  878. etr_port_valid(&etr_port0, 0)) ? 0 : -1;
  879. clear_bit(ETR_FLAG_EACCES, &etr_flags);
  880. } else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_operational_step) {
  881. eacr.sl = 1;
  882. eacr.e0 = 0;
  883. eacr.e1 = 1;
  884. if (!etr_mode_is_etr(etr_eacr))
  885. eacr.es = 0;
  886. sync_port = (etr_port1_uptodate &&
  887. etr_port_valid(&etr_port1, 1)) ? 1 : -1;
  888. clear_bit(ETR_FLAG_EACCES, &etr_flags);
  889. } else {
  890. /* Both ports not usable. */
  891. eacr.es = eacr.sl = 0;
  892. sync_port = -1;
  893. set_bit(ETR_FLAG_EACCES, &etr_flags);
  894. }
  895. /*
  896. * If the clock is in sync just update the eacr and return.
  897. * If there is no valid sync port wait for a port update.
  898. */
  899. if (eacr.es || sync_port < 0) {
  900. etr_update_eacr(eacr);
  901. etr_set_tolec_timeout(now);
  902. return;
  903. }
  904. /*
  905. * Prepare control register for clock syncing
  906. * (reset data port bit, set sync check control.
  907. */
  908. eacr.dp = 0;
  909. eacr.es = 1;
  910. /*
  911. * Update eacr and try to synchronize the clock. If the update
  912. * of eacr caused a stepping port switch (or if we have to
  913. * assume that a stepping port switch has occured) or the
  914. * clock syncing failed, reset the sync check control bit
  915. * and set up a timer to try again after 0.5 seconds
  916. */
  917. etr_update_eacr(eacr);
  918. if (now < etr_tolec + (1600000 << 12) ||
  919. etr_sync_clock(&aib, sync_port) != 0) {
  920. /* Sync failed. Try again in 1/2 second. */
  921. eacr.es = 0;
  922. etr_update_eacr(eacr);
  923. etr_set_sync_timeout();
  924. } else
  925. etr_set_tolec_timeout(now);
  926. }
  927. /*
  928. * Sysfs interface functions
  929. */
  930. static struct sysdev_class etr_sysclass = {
  931. .name = "etr",
  932. };
  933. static struct sys_device etr_port0_dev = {
  934. .id = 0,
  935. .cls = &etr_sysclass,
  936. };
  937. static struct sys_device etr_port1_dev = {
  938. .id = 1,
  939. .cls = &etr_sysclass,
  940. };
  941. /*
  942. * ETR class attributes
  943. */
  944. static ssize_t etr_stepping_port_show(struct sysdev_class *class, char *buf)
  945. {
  946. return sprintf(buf, "%i\n", etr_port0.esw.p);
  947. }
  948. static SYSDEV_CLASS_ATTR(stepping_port, 0400, etr_stepping_port_show, NULL);
  949. static ssize_t etr_stepping_mode_show(struct sysdev_class *class, char *buf)
  950. {
  951. char *mode_str;
  952. if (etr_mode_is_pps(etr_eacr))
  953. mode_str = "pps";
  954. else if (etr_mode_is_etr(etr_eacr))
  955. mode_str = "etr";
  956. else
  957. mode_str = "local";
  958. return sprintf(buf, "%s\n", mode_str);
  959. }
  960. static SYSDEV_CLASS_ATTR(stepping_mode, 0400, etr_stepping_mode_show, NULL);
  961. /*
  962. * ETR port attributes
  963. */
  964. static inline struct etr_aib *etr_aib_from_dev(struct sys_device *dev)
  965. {
  966. if (dev == &etr_port0_dev)
  967. return etr_port0_online ? &etr_port0 : NULL;
  968. else
  969. return etr_port1_online ? &etr_port1 : NULL;
  970. }
  971. static ssize_t etr_online_show(struct sys_device *dev, char *buf)
  972. {
  973. unsigned int online;
  974. online = (dev == &etr_port0_dev) ? etr_port0_online : etr_port1_online;
  975. return sprintf(buf, "%i\n", online);
  976. }
  977. static ssize_t etr_online_store(struct sys_device *dev,
  978. const char *buf, size_t count)
  979. {
  980. unsigned int value;
  981. value = simple_strtoul(buf, NULL, 0);
  982. if (value != 0 && value != 1)
  983. return -EINVAL;
  984. if (test_bit(ETR_FLAG_ENOSYS, &etr_flags))
  985. return -ENOSYS;
  986. if (dev == &etr_port0_dev) {
  987. if (etr_port0_online == value)
  988. return count; /* Nothing to do. */
  989. etr_port0_online = value;
  990. set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
  991. schedule_work(&etr_work);
  992. } else {
  993. if (etr_port1_online == value)
  994. return count; /* Nothing to do. */
  995. etr_port1_online = value;
  996. set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
  997. schedule_work(&etr_work);
  998. }
  999. return count;
  1000. }
  1001. static SYSDEV_ATTR(online, 0600, etr_online_show, etr_online_store);
  1002. static ssize_t etr_stepping_control_show(struct sys_device *dev, char *buf)
  1003. {
  1004. return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
  1005. etr_eacr.e0 : etr_eacr.e1);
  1006. }
  1007. static SYSDEV_ATTR(stepping_control, 0400, etr_stepping_control_show, NULL);
  1008. static ssize_t etr_mode_code_show(struct sys_device *dev, char *buf)
  1009. {
  1010. if (!etr_port0_online && !etr_port1_online)
  1011. /* Status word is not uptodate if both ports are offline. */
  1012. return -ENODATA;
  1013. return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
  1014. etr_port0.esw.psc0 : etr_port0.esw.psc1);
  1015. }
  1016. static SYSDEV_ATTR(state_code, 0400, etr_mode_code_show, NULL);
  1017. static ssize_t etr_untuned_show(struct sys_device *dev, char *buf)
  1018. {
  1019. struct etr_aib *aib = etr_aib_from_dev(dev);
  1020. if (!aib || !aib->slsw.v1)
  1021. return -ENODATA;
  1022. return sprintf(buf, "%i\n", aib->edf1.u);
  1023. }
  1024. static SYSDEV_ATTR(untuned, 0400, etr_untuned_show, NULL);
  1025. static ssize_t etr_network_id_show(struct sys_device *dev, char *buf)
  1026. {
  1027. struct etr_aib *aib = etr_aib_from_dev(dev);
  1028. if (!aib || !aib->slsw.v1)
  1029. return -ENODATA;
  1030. return sprintf(buf, "%i\n", aib->edf1.net_id);
  1031. }
  1032. static SYSDEV_ATTR(network, 0400, etr_network_id_show, NULL);
  1033. static ssize_t etr_id_show(struct sys_device *dev, char *buf)
  1034. {
  1035. struct etr_aib *aib = etr_aib_from_dev(dev);
  1036. if (!aib || !aib->slsw.v1)
  1037. return -ENODATA;
  1038. return sprintf(buf, "%i\n", aib->edf1.etr_id);
  1039. }
  1040. static SYSDEV_ATTR(id, 0400, etr_id_show, NULL);
  1041. static ssize_t etr_port_number_show(struct sys_device *dev, char *buf)
  1042. {
  1043. struct etr_aib *aib = etr_aib_from_dev(dev);
  1044. if (!aib || !aib->slsw.v1)
  1045. return -ENODATA;
  1046. return sprintf(buf, "%i\n", aib->edf1.etr_pn);
  1047. }
  1048. static SYSDEV_ATTR(port, 0400, etr_port_number_show, NULL);
  1049. static ssize_t etr_coupled_show(struct sys_device *dev, char *buf)
  1050. {
  1051. struct etr_aib *aib = etr_aib_from_dev(dev);
  1052. if (!aib || !aib->slsw.v3)
  1053. return -ENODATA;
  1054. return sprintf(buf, "%i\n", aib->edf3.c);
  1055. }
  1056. static SYSDEV_ATTR(coupled, 0400, etr_coupled_show, NULL);
  1057. static ssize_t etr_local_time_show(struct sys_device *dev, char *buf)
  1058. {
  1059. struct etr_aib *aib = etr_aib_from_dev(dev);
  1060. if (!aib || !aib->slsw.v3)
  1061. return -ENODATA;
  1062. return sprintf(buf, "%i\n", aib->edf3.blto);
  1063. }
  1064. static SYSDEV_ATTR(local_time, 0400, etr_local_time_show, NULL);
  1065. static ssize_t etr_utc_offset_show(struct sys_device *dev, char *buf)
  1066. {
  1067. struct etr_aib *aib = etr_aib_from_dev(dev);
  1068. if (!aib || !aib->slsw.v3)
  1069. return -ENODATA;
  1070. return sprintf(buf, "%i\n", aib->edf3.buo);
  1071. }
  1072. static SYSDEV_ATTR(utc_offset, 0400, etr_utc_offset_show, NULL);
  1073. static struct sysdev_attribute *etr_port_attributes[] = {
  1074. &attr_online,
  1075. &attr_stepping_control,
  1076. &attr_state_code,
  1077. &attr_untuned,
  1078. &attr_network,
  1079. &attr_id,
  1080. &attr_port,
  1081. &attr_coupled,
  1082. &attr_local_time,
  1083. &attr_utc_offset,
  1084. NULL
  1085. };
  1086. static int __init etr_register_port(struct sys_device *dev)
  1087. {
  1088. struct sysdev_attribute **attr;
  1089. int rc;
  1090. rc = sysdev_register(dev);
  1091. if (rc)
  1092. goto out;
  1093. for (attr = etr_port_attributes; *attr; attr++) {
  1094. rc = sysdev_create_file(dev, *attr);
  1095. if (rc)
  1096. goto out_unreg;
  1097. }
  1098. return 0;
  1099. out_unreg:
  1100. for (; attr >= etr_port_attributes; attr--)
  1101. sysdev_remove_file(dev, *attr);
  1102. sysdev_unregister(dev);
  1103. out:
  1104. return rc;
  1105. }
  1106. static void __init etr_unregister_port(struct sys_device *dev)
  1107. {
  1108. struct sysdev_attribute **attr;
  1109. for (attr = etr_port_attributes; *attr; attr++)
  1110. sysdev_remove_file(dev, *attr);
  1111. sysdev_unregister(dev);
  1112. }
  1113. static int __init etr_init_sysfs(void)
  1114. {
  1115. int rc;
  1116. rc = sysdev_class_register(&etr_sysclass);
  1117. if (rc)
  1118. goto out;
  1119. rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_port);
  1120. if (rc)
  1121. goto out_unreg_class;
  1122. rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_mode);
  1123. if (rc)
  1124. goto out_remove_stepping_port;
  1125. rc = etr_register_port(&etr_port0_dev);
  1126. if (rc)
  1127. goto out_remove_stepping_mode;
  1128. rc = etr_register_port(&etr_port1_dev);
  1129. if (rc)
  1130. goto out_remove_port0;
  1131. return 0;
  1132. out_remove_port0:
  1133. etr_unregister_port(&etr_port0_dev);
  1134. out_remove_stepping_mode:
  1135. sysdev_class_remove_file(&etr_sysclass, &attr_stepping_mode);
  1136. out_remove_stepping_port:
  1137. sysdev_class_remove_file(&etr_sysclass, &attr_stepping_port);
  1138. out_unreg_class:
  1139. sysdev_class_unregister(&etr_sysclass);
  1140. out:
  1141. return rc;
  1142. }
  1143. device_initcall(etr_init_sysfs);