1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/errno.h>
34#include <linux/module.h>
35#include <linux/sched.h>
36#include <linux/kernel.h>
37#include <linux/param.h>
38#include <linux/string.h>
39#include <linux/mm.h>
40#include <linux/interrupt.h>
41#include <linux/time.h>
42#include <linux/delay.h>
43#include <linux/init.h>
44#include <linux/smp.h>
45
46#include <asm/io.h>
47#include <asm/smp.h>
48#include <asm/irq.h>
49#include <asm/msr.h>
50#include <asm/delay.h>
51#include <asm/mpspec.h>
52#include <asm/uaccess.h>
53#include <asm/processor.h>
54
55#include <linux/mc146818rtc.h>
56#include <linux/timex.h>
57#include <linux/config.h>
58
59#include <asm/fixmap.h>
60#include <asm/cobalt.h>
61
62
63
64
65#include <linux/irq.h>
66
67
68unsigned long cpu_khz;
69
70
71static int delay_at_last_interrupt;
72
73static unsigned long last_tsc_low;
74
75
76
77
78
79
80unsigned long fast_gettimeoffset_quotient;
81
82extern rwlock_t xtime_lock;
83extern unsigned long wall_jiffies;
84
85spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
86
87static inline unsigned long do_fast_gettimeoffset(void)
88{
89 register unsigned long eax, edx;
90
91
92
93 rdtsc(eax,edx);
94
95
96 eax -= last_tsc_low;
97
98
99
100
101
102
103
104
105
106
107 __asm__("mull %2"
108 :"=a" (eax), "=d" (edx)
109 :"rm" (fast_gettimeoffset_quotient),
110 "0" (eax));
111
112
113 return delay_at_last_interrupt + edx;
114}
115
116#define TICK_SIZE tick
117
118spinlock_t i8253_lock = SPIN_LOCK_UNLOCKED;
119
120EXPORT_SYMBOL(i8253_lock);
121
122extern spinlock_t i8259A_lock;
123
124#ifndef CONFIG_X86_TSC
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158static unsigned long do_slow_gettimeoffset(void)
159{
160 int count;
161
162 static int count_p = LATCH;
163 static unsigned long jiffies_p = 0;
164
165
166
167
168 unsigned long jiffies_t;
169
170
171 spin_lock(&i8253_lock);
172
173 outb_p(0x00, 0x43);
174
175 count = inb_p(0x40);
176
177
178
179
180
181 jiffies_t = jiffies;
182
183 count |= inb_p(0x40) << 8;
184
185
186 if (count > LATCH) {
187 outb_p(0x34, 0x43);
188 outb_p(LATCH & 0xff, 0x40);
189 outb(LATCH >> 8, 0x40);
190 count = LATCH - 1;
191 }
192
193 spin_unlock(&i8253_lock);
194
195
196
197
198
199
200
201
202
203
204
205
206#define BUGGY_NEPTUN_TIMER
207
208 if( jiffies_t == jiffies_p ) {
209 if( count > count_p ) {
210
211
212 int i;
213
214 spin_lock(&i8259A_lock);
215
216
217
218
219 i = inb(0x20);
220 spin_unlock(&i8259A_lock);
221
222
223 if (i & 0x01) {
224
225
226
227
228
229 count -= LATCH;
230 } else {
231#ifdef BUGGY_NEPTUN_TIMER
232
233
234
235
236
237
238
239
240 count -= 256;
241#else
242 printk("do_slow_gettimeoffset(): hardware timer problem?\n");
243#endif
244 }
245 }
246 } else
247 jiffies_p = jiffies_t;
248
249 count_p = count;
250
251 count = ((LATCH-1) - count) * TICK_SIZE;
252 count = (count + LATCH/2) / LATCH;
253
254 return count;
255}
256
257static unsigned long (*do_gettimeoffset)(void) = do_slow_gettimeoffset;
258
259
260
261#ifdef CONFIG_X86_SUMMIT
262
263#define CYCLONE_CBAR_ADDR 0xFEB00CD0
264#define CYCLONE_PMCC_OFFSET 0x51A0
265#define CYCLONE_MPMC_OFFSET 0x51D0
266#define CYCLONE_MPCS_OFFSET 0x51A8
267#define CYCLONE_TIMER_FREQ 100000000
268
269int use_cyclone = 0;
270int __init cyclone_setup(char *str)
271{
272 use_cyclone = 1;
273 return 1;
274}
275
276static u32* volatile cyclone_timer;
277static u32 last_cyclone_timer;
278
279static inline void mark_timeoffset_cyclone(void)
280{
281 int count;
282 unsigned long delta = last_cyclone_timer;
283 spin_lock(&i8253_lock);
284
285 if(cyclone_timer)
286 last_cyclone_timer = cyclone_timer[0];
287
288
289 outb_p(0x00, 0x43);
290
291 count = inb_p(0x40);
292 count |= inb(0x40) << 8;
293 spin_unlock(&i8253_lock);
294
295
296 delta = last_cyclone_timer - delta;
297 if(delta > loops_per_jiffy+2000){
298 delta = (delta/loops_per_jiffy)-1;
299 jiffies += delta;
300 }
301
302 count = ((LATCH-1) - count) * TICK_SIZE;
303 delay_at_last_interrupt = (count + LATCH/2) / LATCH;
304}
305
306static unsigned long do_gettimeoffset_cyclone(void)
307{
308 u32 offset;
309
310 if(!cyclone_timer)
311 return delay_at_last_interrupt;
312
313
314 offset = cyclone_timer[0];
315
316
317 offset = offset - last_cyclone_timer;
318
319
320
321 offset = offset/(CYCLONE_TIMER_FREQ/1000000);
322
323
324 return delay_at_last_interrupt + offset;
325}
326
327static void __init init_cyclone_clock(void)
328{
329 u32* reg;
330 u32 base;
331 u32 pageaddr;
332 u32 offset;
333 int i;
334
335 printk(KERN_INFO "Summit chipset: Starting Cyclone Counter.\n");
336
337
338 pageaddr = (CYCLONE_CBAR_ADDR)&PAGE_MASK;
339 offset = (CYCLONE_CBAR_ADDR)&(~PAGE_MASK);
340 set_fixmap_nocache(FIX_CYCLONE_TIMER, pageaddr);
341 reg = (u32*)(fix_to_virt(FIX_CYCLONE_TIMER) + offset);
342 if(!reg){
343 printk(KERN_ERR "Summit chipset: Could not find valid CBAR register.\n");
344 use_cyclone = 0;
345 return;
346 }
347 base = *reg;
348 if(!base){
349 printk(KERN_ERR "Summit chipset: Could not find valid CBAR value.\n");
350 use_cyclone = 0;
351 return;
352 }
353
354
355 pageaddr = (base + CYCLONE_PMCC_OFFSET)&PAGE_MASK;
356 offset = (base + CYCLONE_PMCC_OFFSET)&(~PAGE_MASK);
357 set_fixmap_nocache(FIX_CYCLONE_TIMER, pageaddr);
358 reg = (u32*)(fix_to_virt(FIX_CYCLONE_TIMER) + offset);
359 if(!reg){
360 printk(KERN_ERR "Summit chipset: Could not find valid PMCC register.\n");
361 use_cyclone = 0;
362 return;
363 }
364 reg[0] = 0x00000001;
365
366
367 pageaddr = (base + CYCLONE_MPCS_OFFSET)&PAGE_MASK;
368 offset = (base + CYCLONE_MPCS_OFFSET)&(~PAGE_MASK);
369 set_fixmap_nocache(FIX_CYCLONE_TIMER, pageaddr);
370 reg = (u32*)(fix_to_virt(FIX_CYCLONE_TIMER) + offset);
371 if(!reg){
372 printk(KERN_ERR "Summit chipset: Could not find valid MPCS register.\n");
373 use_cyclone = 0;
374 return;
375 }
376 reg[0] = 0x00000001;
377
378
379 pageaddr = (base + CYCLONE_MPMC_OFFSET)&PAGE_MASK;
380 offset = (base + CYCLONE_MPMC_OFFSET)&(~PAGE_MASK);
381 set_fixmap_nocache(FIX_CYCLONE_TIMER, pageaddr);
382 cyclone_timer = (u32*)(fix_to_virt(FIX_CYCLONE_TIMER) + offset);
383 if(!cyclone_timer){
384 printk(KERN_ERR "Summit chipset: Could not find valid MPMC register.\n");
385 use_cyclone = 0;
386 return;
387 }
388
389
390 for(i=0; i<3; i++){
391 u32 old = cyclone_timer[0];
392 int stall = 100;
393 while(stall--) barrier();
394 if(cyclone_timer[0] == old){
395 printk(KERN_ERR "Summit chipset: Counter not counting! DISABLED\n");
396 cyclone_timer = 0;
397 use_cyclone = 0;
398 return;
399 }
400 }
401
402 do_gettimeoffset = do_gettimeoffset_cyclone;
403}
404void __cyclone_delay(unsigned long loops)
405{
406 unsigned long bclock, now;
407 if(!cyclone_timer)
408 return;
409 bclock = cyclone_timer[0];
410 do {
411 rep_nop();
412 now = cyclone_timer[0];
413 } while ((now-bclock) < loops);
414}
415#endif
416
417#else
418
419#define do_gettimeoffset() do_fast_gettimeoffset()
420
421#endif
422
423
424#ifndef CONFIG_X86_SUMMIT
425int __init cyclone_setup(char *str)
426{
427 printk(KERN_ERR "cyclone: Kernel not compiled with CONFIG_X86_SUMMIT, cannot use the cyclone-timer.\n");
428 return 1;
429}
430
431const int use_cyclone = 0;
432static void mark_timeoffset_cyclone(void) {}
433static unsigned long do_gettimeoffset_cyclone(void) {return 0;}
434static void init_cyclone_clock(void) {}
435void __cyclone_delay(unsigned long loops) {}
436#endif
437
438
439
440
441
442void do_gettimeofday(struct timeval *tv)
443{
444 unsigned long flags;
445 unsigned long usec, sec;
446
447 read_lock_irqsave(&xtime_lock, flags);
448 usec = do_gettimeoffset();
449 {
450 unsigned long lost = jiffies - wall_jiffies;
451 if (lost)
452 usec += lost * (1000000 / HZ);
453 }
454 sec = xtime.tv_sec;
455 usec += xtime.tv_usec;
456 read_unlock_irqrestore(&xtime_lock, flags);
457
458 while (usec >= 1000000) {
459 usec -= 1000000;
460 sec++;
461 }
462
463 tv->tv_sec = sec;
464 tv->tv_usec = usec;
465}
466
467void do_settimeofday(struct timeval *tv)
468{
469 write_lock_irq(&xtime_lock);
470
471
472
473
474
475
476 tv->tv_usec -= do_gettimeoffset();
477 tv->tv_usec -= (jiffies - wall_jiffies) * (1000000 / HZ);
478
479 while (tv->tv_usec < 0) {
480 tv->tv_usec += 1000000;
481 tv->tv_sec--;
482 }
483
484 xtime = *tv;
485 time_adjust = 0;
486 time_status |= STA_UNSYNC;
487 time_maxerror = NTP_PHASE_LIMIT;
488 time_esterror = NTP_PHASE_LIMIT;
489 write_unlock_irq(&xtime_lock);
490}
491
492
493
494
495
496
497
498
499
500
501
502static int set_rtc_mmss(unsigned long nowtime)
503{
504 int retval = 0;
505 int real_seconds, real_minutes, cmos_minutes;
506 unsigned char save_control, save_freq_select;
507
508
509 spin_lock(&rtc_lock);
510 save_control = CMOS_READ(RTC_CONTROL);
511 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
512
513 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
514 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
515
516 cmos_minutes = CMOS_READ(RTC_MINUTES);
517 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
518 BCD_TO_BIN(cmos_minutes);
519
520
521
522
523
524
525
526 real_seconds = nowtime % 60;
527 real_minutes = nowtime / 60;
528 if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
529 real_minutes += 30;
530 real_minutes %= 60;
531
532 if (abs(real_minutes - cmos_minutes) < 30) {
533 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
534 BIN_TO_BCD(real_seconds);
535 BIN_TO_BCD(real_minutes);
536 }
537 CMOS_WRITE(real_seconds,RTC_SECONDS);
538 CMOS_WRITE(real_minutes,RTC_MINUTES);
539 } else {
540 printk(KERN_WARNING
541 "set_rtc_mmss: can't update from %d to %d\n",
542 cmos_minutes, real_minutes);
543 retval = -1;
544 }
545
546
547
548
549
550
551
552
553 CMOS_WRITE(save_control, RTC_CONTROL);
554 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
555 spin_unlock(&rtc_lock);
556
557 return retval;
558}
559
560
561static long last_rtc_update;
562
563int timer_ack;
564
565
566
567
568
569static inline void do_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
570{
571#ifdef CONFIG_X86_IO_APIC
572 if (timer_ack) {
573
574
575
576
577
578
579 spin_lock(&i8259A_lock);
580 outb(0x0c, 0x20);
581
582 inb(0x20);
583 spin_unlock(&i8259A_lock);
584 }
585#endif
586
587#ifdef CONFIG_VISWS
588
589 co_cpu_write(CO_CPU_STAT,co_cpu_read(CO_CPU_STAT) & ~CO_STAT_TIMEINTR);
590#endif
591 do_timer(regs);
592
593
594
595
596
597#ifndef CONFIG_X86_LOCAL_APIC
598 if (!user_mode(regs))
599 x86_do_profile(regs->eip);
600#else
601 if (!using_apic_timer)
602 smp_local_timer_interrupt(regs);
603#endif
604
605
606
607
608
609
610 if ((time_status & STA_UNSYNC) == 0 &&
611 xtime.tv_sec > last_rtc_update + 660 &&
612 xtime.tv_usec >= 500000 - ((unsigned) tick) / 2 &&
613 xtime.tv_usec <= 500000 + ((unsigned) tick) / 2) {
614 if (set_rtc_mmss(xtime.tv_sec) == 0)
615 last_rtc_update = xtime.tv_sec;
616 else
617 last_rtc_update = xtime.tv_sec - 600;
618 }
619
620#ifdef CONFIG_MCA
621 if( MCA_bus ) {
622
623
624
625
626
627
628
629
630
631 irq = inb_p( 0x61 );
632 outb_p( irq|0x80, 0x61 );
633 }
634#endif
635}
636
637static int use_tsc;
638
639
640
641
642
643
644static void timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
645{
646 int count;
647
648
649
650
651
652
653
654
655 write_lock(&xtime_lock);
656
657 if(use_cyclone)
658 mark_timeoffset_cyclone();
659 else if (use_tsc) {
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674 rdtscl(last_tsc_low);
675
676 spin_lock(&i8253_lock);
677 outb_p(0x00, 0x43);
678
679 count = inb_p(0x40);
680 count |= inb(0x40) << 8;
681
682
683
684
685
686
687
688 if (count > LATCH) {
689 printk(KERN_WARNING
690 "i8253 count too high! resetting..\n");
691 outb_p(0x34, 0x43);
692 outb_p(LATCH & 0xff, 0x40);
693 outb(LATCH >> 8, 0x40);
694 count = LATCH - 1;
695 }
696
697 spin_unlock(&i8253_lock);
698
699
700
701 if (count == LATCH) {
702 count--;
703 }
704
705 count = ((LATCH-1) - count) * TICK_SIZE;
706 delay_at_last_interrupt = (count + LATCH/2) / LATCH;
707 }
708
709 do_timer_interrupt(irq, NULL, regs);
710
711 write_unlock(&xtime_lock);
712
713}
714
715
716unsigned long get_cmos_time(void)
717{
718 unsigned int year, mon, day, hour, min, sec;
719 int i;
720
721 spin_lock(&rtc_lock);
722
723
724
725
726
727
728 for (i = 0 ; i < 1000000 ; i++)
729 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
730 break;
731 for (i = 0 ; i < 1000000 ; i++)
732 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
733 break;
734 do {
735 sec = CMOS_READ(RTC_SECONDS);
736 min = CMOS_READ(RTC_MINUTES);
737 hour = CMOS_READ(RTC_HOURS);
738 day = CMOS_READ(RTC_DAY_OF_MONTH);
739 mon = CMOS_READ(RTC_MONTH);
740 year = CMOS_READ(RTC_YEAR);
741 } while (sec != CMOS_READ(RTC_SECONDS));
742 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
743 {
744 BCD_TO_BIN(sec);
745 BCD_TO_BIN(min);
746 BCD_TO_BIN(hour);
747 BCD_TO_BIN(day);
748 BCD_TO_BIN(mon);
749 BCD_TO_BIN(year);
750 }
751 spin_unlock(&rtc_lock);
752 if ((year += 1900) < 1970)
753 year += 100;
754 return mktime(year, mon, day, hour, min, sec);
755}
756
757static struct irqaction irq0 = { timer_interrupt, SA_INTERRUPT, 0, "timer", NULL, NULL};
758
759
760
761
762
763
764
765
766
767
768#define CALIBRATE_LATCH (5 * LATCH)
769#define CALIBRATE_TIME (5 * 1000020/HZ)
770
771static unsigned long __init calibrate_tsc(void)
772{
773
774 outb((inb(0x61) & ~0x02) | 0x01, 0x61);
775
776
777
778
779
780
781
782
783 outb(0xb0, 0x43);
784 outb(CALIBRATE_LATCH & 0xff, 0x42);
785 outb(CALIBRATE_LATCH >> 8, 0x42);
786
787 {
788 unsigned long startlow, starthigh;
789 unsigned long endlow, endhigh;
790 unsigned long count;
791
792 rdtsc(startlow,starthigh);
793 count = 0;
794 do {
795 count++;
796 } while ((inb(0x61) & 0x20) == 0);
797 rdtsc(endlow,endhigh);
798
799 last_tsc_low = endlow;
800
801
802 if (count <= 1)
803 goto bad_ctc;
804
805
806 __asm__("subl %2,%0\n\t"
807 "sbbl %3,%1"
808 :"=a" (endlow), "=d" (endhigh)
809 :"g" (startlow), "g" (starthigh),
810 "0" (endlow), "1" (endhigh));
811
812
813 if (endhigh)
814 goto bad_ctc;
815
816
817 if (endlow <= CALIBRATE_TIME)
818 goto bad_ctc;
819
820 __asm__("divl %2"
821 :"=a" (endlow), "=d" (endhigh)
822 :"r" (endlow), "0" (0), "1" (CALIBRATE_TIME));
823
824 return endlow;
825 }
826
827
828
829
830
831
832bad_ctc:
833 return 0;
834}
835
836void __init time_init(void)
837{
838 extern int x86_udelay_tsc;
839
840 xtime.tv_sec = get_cmos_time();
841 xtime.tv_usec = 0;
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868 dodgy_tsc();
869
870 if(use_cyclone)
871 init_cyclone_clock();
872
873 if (cpu_has_tsc) {
874 unsigned long tsc_quotient = calibrate_tsc();
875 if (tsc_quotient) {
876 fast_gettimeoffset_quotient = tsc_quotient;
877
878
879
880
881
882 if(!use_cyclone){
883
884
885
886
887 use_tsc = 1;
888 x86_udelay_tsc = 1;
889#ifndef do_gettimeoffset
890 do_gettimeoffset = do_fast_gettimeoffset;
891#endif
892 }
893
894
895
896
897 { unsigned long eax=0, edx=1000;
898 __asm__("divl %2"
899 :"=a" (cpu_khz), "=d" (edx)
900 :"r" (tsc_quotient),
901 "0" (eax), "1" (edx));
902 printk("Detected %lu.%03lu MHz processor.\n", cpu_khz / 1000, cpu_khz % 1000);
903 }
904 }
905 }
906
907
908#ifdef CONFIG_VISWS
909 printk("Starting Cobalt Timer system clock\n");
910
911
912 co_cpu_write(CO_CPU_TIMEVAL, CO_TIME_HZ/HZ);
913
914
915 co_cpu_write(CO_CPU_CTRL, co_cpu_read(CO_CPU_CTRL) | CO_CTRL_TIMERUN);
916
917
918 co_cpu_write(CO_CPU_CTRL, co_cpu_read(CO_CPU_CTRL) & ~CO_CTRL_TIMEMASK);
919
920
921 setup_irq(CO_IRQ_TIMER, &irq0);
922#else
923 setup_irq(0, &irq0);
924#endif
925}
926