OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [arch/] [mips64/] [kernel/] [time.c] - Blame information for rev 1275

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * Copyright 2001 MontaVista Software Inc.
3
 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
4
 * Copyright (c) 2003  Maciej W. Rozycki
5
 *
6
 * Common time service routines for MIPS machines. See
7
 * Documentation/mips/time.README.
8
 *
9
 * This program is free software; you can redistribute  it and/or modify it
10
 * under  the terms of  the GNU General  Public License as published by the
11
 * Free Software Foundation;  either version 2 of the  License, or (at your
12
 * option) any later version.
13
 */
14
#include <linux/config.h>
15
#include <linux/types.h>
16
#include <linux/kernel.h>
17
#include <linux/init.h>
18
#include <linux/sched.h>
19
#include <linux/param.h>
20
#include <linux/time.h>
21
#include <linux/timex.h>
22
#include <linux/smp.h>
23
#include <linux/kernel_stat.h>
24
#include <linux/spinlock.h>
25
#include <linux/interrupt.h>
26
#include <linux/module.h>
27
 
28
#include <asm/bootinfo.h>
29
#include <asm/cpu.h>
30
#include <asm/time.h>
31
#include <asm/hardirq.h>
32
#include <asm/div64.h>
33
 
34
/*
35
 * The integer part of the number of usecs per jiffy is taken from tick,
36
 * but the fractional part is not recorded, so we calculate it using the
37
 * initial value of HZ.  This aids systems where tick isn't really an
38
 * integer (e.g. for HZ = 128).
39
 */
40
#define USECS_PER_JIFFY         tick
41
#define USECS_PER_JIFFY_FRAC    ((unsigned long)(u32)((1000000ULL << 32) / HZ))
42
 
43
/*
44
 * forward reference
45
 */
46
extern rwlock_t xtime_lock;
47
extern volatile unsigned long wall_jiffies;
48
 
49
spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
50
 
51
/*
52
 * whether we emulate local_timer_interrupts for SMP machines.
53
 */
54
int emulate_local_timer_interrupt;
55
 
56
 
57
/*
58
 * By default we provide the null RTC ops
59
 */
60
static unsigned long null_rtc_get_time(void)
61
{
62
        return mktime(2000, 1, 1, 0, 0, 0);
63
}
64
 
65
static int null_rtc_set_time(unsigned long sec)
66
{
67
        return 0;
68
}
69
 
70
unsigned long (*rtc_get_time)(void) = null_rtc_get_time;
71
int (*rtc_set_time)(unsigned long) = null_rtc_set_time;
72
int (*rtc_set_mmss)(unsigned long);
73
 
74
 
75
/* usecs per counter cycle, shifted to left by 32 bits */
76
static unsigned int sll32_usecs_per_cycle;
77
 
78
/* how many counter cycles in a jiffy */
79
static unsigned long cycles_per_jiffy;
80
 
81
/* Cycle counter value at the previous timer interrupt.. */
82
static unsigned int timerhi, timerlo;
83
 
84
/* expirelo is the count value for next CPU timer interrupt */
85
static unsigned int expirelo;
86
 
87
 
88
/*
89
 * Null timer ack for systems not needing one (e.g. i8254).
90
 */
91
static void null_timer_ack(void) { /* nothing */ }
92
 
93
/*
94
 * Null high precision timer functions for systems lacking one.
95
 */
96
static unsigned int null_hpt_read(void)
97
{
98
        return 0;
99
}
100
 
101
static void null_hpt_init(unsigned int count) { /* nothing */ }
102
 
103
 
104
/*
105
 * Timer ack for an R4k-compatible timer of a known frequency.
106
 */
107
static void c0_timer_ack(void)
108
{
109
        unsigned int count;
110
 
111
        /* Ack this timer interrupt and set the next one.  */
112
        expirelo += cycles_per_jiffy;
113
        write_c0_compare(expirelo);
114
 
115
        /* Check to see if we have missed any timer interrupts.  */
116
        count = read_c0_count();
117
        if ((count - expirelo) < 0x7fffffff) {
118
                /* missed_timer_count++; */
119
                expirelo = count + cycles_per_jiffy;
120
                write_c0_compare(expirelo);
121
        }
122
}
123
 
124
/*
125
 * High precision timer functions for a R4k-compatible timer.
126
 */
127
static unsigned int c0_hpt_read(void)
128
{
129
        return read_c0_count();
130
}
131
 
132
/* For use solely as a high precision timer.  */
133
static void c0_hpt_init(unsigned int count)
134
{
135
        write_c0_count(read_c0_count() - count);
136
}
137
 
138
/* For use both as a high precision timer and an interrupt source.  */
139
static void c0_hpt_timer_init(unsigned int count)
140
{
141
        count = read_c0_count() - count;
142
        expirelo = (count / cycles_per_jiffy + 1) * cycles_per_jiffy;
143
        write_c0_count(expirelo - cycles_per_jiffy);
144
        write_c0_compare(expirelo);
145
        write_c0_count(count);
146
}
147
 
148
int (*mips_timer_state)(void);
149
void (*mips_timer_ack)(void);
150
unsigned int (*mips_hpt_read)(void);
151
void (*mips_hpt_init)(unsigned int);
152
 
153
 
154
/*
155
 * timeofday services, for syscalls.
156
 */
157
void do_gettimeofday(struct timeval *tv)
158
{
159
        unsigned long flags, lost;
160
 
161
        read_lock_irqsave(&xtime_lock, flags);
162
 
163
        *tv = xtime;
164
        tv->tv_usec += do_gettimeoffset();
165
        /*
166
         * xtime is atomically updated in timer_bh.  jiffies - wall_jiffies
167
         * is nonzero if the timer bottom half hasn't executed yet.
168
         */
169
        lost = jiffies - wall_jiffies;
170
        if (lost)
171
                tv->tv_usec += lost * USECS_PER_JIFFY;
172
 
173
        read_unlock_irqrestore(&xtime_lock, flags);
174
 
175
        while (tv->tv_usec >= 1000000) {
176
                tv->tv_usec -= 1000000;
177
                tv->tv_sec++;
178
        }
179
}
180
 
181
void do_settimeofday(struct timeval *tv)
182
{
183
        write_lock_irq(&xtime_lock);
184
 
185
        /*
186
         * This is revolting.  We need to set "xtime" correctly.  However,
187
         * the value in this location is the value at the most recent update
188
         * of wall time.  Discover what correction gettimeofday() would have
189
         * made, and then undo it!
190
         */
191
        tv->tv_usec -= do_gettimeoffset();
192
        tv->tv_usec -= (jiffies - wall_jiffies) * USECS_PER_JIFFY;
193
 
194
        while (tv->tv_usec < 0) {
195
                tv->tv_usec += 1000000;
196
                tv->tv_sec--;
197
        }
198
 
199
        xtime = *tv;
200
        time_adjust = 0;                 /* stop active adjtime() */
201
        time_status |= STA_UNSYNC;
202
        time_maxerror = NTP_PHASE_LIMIT;
203
        time_esterror = NTP_PHASE_LIMIT;
204
 
205
        write_unlock_irq(&xtime_lock);
206
}
207
 
208
 
209
/*
210
 * Gettimeoffset routines.  These routines returns the time duration
211
 * since last timer interrupt in usecs.
212
 *
213
 * If the exact CPU counter frequency is known, use fixed_rate_gettimeoffset.
214
 * Otherwise use calibrate_gettimeoffset()
215
 *
216
 * If the CPU does not have the counter register, you can either supply
217
 * your own gettimeoffset() routine, or use null_gettimeoffset(), which
218
 * gives the same resolution as HZ.
219
 */
220
 
221
static unsigned long null_gettimeoffset(void)
222
{
223
        return 0;
224
}
225
 
226
 
227
/* The function pointer to one of the gettimeoffset funcs.  */
228
unsigned long (*do_gettimeoffset)(void) = null_gettimeoffset;
229
 
230
 
231
static unsigned long fixed_rate_gettimeoffset(void)
232
{
233
        u32 count;
234
        unsigned long res;
235
 
236
        /* Get last timer tick in absolute kernel time */
237
        count = mips_hpt_read();
238
 
239
        /* .. relative to previous jiffy (32 bits is enough) */
240
        count -= timerlo;
241
 
242
        __asm__("multu  %1,%2"
243
                : "=h" (res)
244
                : "r" (count), "r" (sll32_usecs_per_cycle)
245
                : "lo", "accum");
246
 
247
        /*
248
         * Due to possible jiffies inconsistencies, we need to check
249
         * the result so that we'll get a timer that is monotonic.
250
         */
251
        if (res >= USECS_PER_JIFFY)
252
                res = USECS_PER_JIFFY - 1;
253
 
254
        return res;
255
}
256
 
257
 
258
/*
259
 * Cached "1/(clocks per usec) * 2^32" value.
260
 * It has to be recalculated once each jiffy.
261
 */
262
static unsigned long cached_quotient;
263
 
264
/* Last jiffy when calibrate_divXX_gettimeoffset() was called. */
265
static unsigned long last_jiffies;
266
 
267
/*
268
 * This is moved from dec/time.c:do_ioasic_gettimeoffset() by Maciej.
269
 */
270
static unsigned long calibrate_div32_gettimeoffset(void)
271
{
272
        u32 count;
273
        unsigned long res, tmp;
274
        unsigned long quotient;
275
 
276
        tmp = jiffies;
277
 
278
        quotient = cached_quotient;
279
 
280
        if (last_jiffies != tmp) {
281
                last_jiffies = tmp;
282
                if (last_jiffies != 0) {
283
                        unsigned long r0;
284
                        do_div64_32(r0, timerhi, timerlo, tmp);
285
                        do_div64_32(quotient, USECS_PER_JIFFY,
286
                                    USECS_PER_JIFFY_FRAC, r0);
287
                        cached_quotient = quotient;
288
                }
289
        }
290
 
291
        /* Get last timer tick in absolute kernel time */
292
        count = mips_hpt_read();
293
 
294
        /* .. relative to previous jiffy (32 bits is enough) */
295
        count -= timerlo;
296
 
297
        __asm__("multu  %1,%2"
298
                : "=h" (res)
299
                : "r" (count), "r" (quotient)
300
                : "lo", "accum");
301
 
302
        /*
303
         * Due to possible jiffies inconsistencies, we need to check
304
         * the result so that we'll get a timer that is monotonic.
305
         */
306
        if (res >= USECS_PER_JIFFY)
307
                res = USECS_PER_JIFFY - 1;
308
 
309
        return res;
310
}
311
 
312
static unsigned long calibrate_div64_gettimeoffset(void)
313
{
314
        u32 count;
315
        unsigned long res, tmp;
316
        unsigned long quotient;
317
 
318
        tmp = jiffies;
319
 
320
        quotient = cached_quotient;
321
 
322
        if (last_jiffies != tmp) {
323
                last_jiffies = tmp;
324
                if (last_jiffies) {
325
                        unsigned long r0;
326
                        __asm__(".set   push\n\t"
327
                                ".set   mips3\n\t"
328
                                "lwu    %0,%3\n\t"
329
                                "dsll32 %1,%2,0\n\t"
330
                                "or     %1,%1,%0\n\t"
331
                                "ddivu  $0,%1,%4\n\t"
332
                                "mflo   %1\n\t"
333
                                "dsll32 %0,%5,0\n\t"
334
                                "or     %0,%0,%6\n\t"
335
                                "ddivu  $0,%0,%1\n\t"
336
                                "mflo   %0\n\t"
337
                                ".set   pop"
338
                                : "=&r" (quotient), "=&r" (r0)
339
                                : "r" (timerhi), "m" (timerlo),
340
                                  "r" (tmp), "r" (USECS_PER_JIFFY),
341
                                  "r" (USECS_PER_JIFFY_FRAC)
342
                                : "hi", "lo", "accum");
343
                        cached_quotient = quotient;
344
                }
345
        }
346
 
347
        /* Get last timer tick in absolute kernel time */
348
        count = mips_hpt_read();
349
 
350
        /* .. relative to previous jiffy (32 bits is enough) */
351
        count -= timerlo;
352
 
353
        __asm__("multu  %1,%2"
354
                : "=h" (res)
355
                : "r" (count), "r" (quotient)
356
                : "lo", "accum");
357
 
358
        /*
359
         * Due to possible jiffies inconsistencies, we need to check
360
         * the result so that we'll get a timer that is monotonic.
361
         */
362
        if (res >= USECS_PER_JIFFY)
363
                res = USECS_PER_JIFFY - 1;
364
 
365
        return res;
366
}
367
 
368
 
369
/* last time when xtime and rtc are sync'ed up */
370
static long last_rtc_update;
371
 
372
/*
373
 * local_timer_interrupt() does profiling and process accounting
374
 * on a per-CPU basis.
375
 *
376
 * In UP mode, it is invoked from the (global) timer_interrupt.
377
 *
378
 * In SMP mode, it might invoked by per-CPU timer interrupt, or
379
 * a broadcasted inter-processor interrupt which itself is triggered
380
 * by the global timer interrupt.
381
 */
382
void local_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
383
{
384
        if (!user_mode(regs)) {
385
                if (prof_buffer && current->pid) {
386
                        extern int _stext;
387
                        unsigned long pc = regs->cp0_epc;
388
 
389
                        pc -= (unsigned long) &_stext;
390
                        pc >>= prof_shift;
391
                        /*
392
                         * Dont ignore out-of-bounds pc values silently,
393
                         * put them into the last histogram slot, so if
394
                         * present, they will show up as a sharp peak.
395
                         */
396
                        if (pc > prof_len - 1)
397
                                pc = prof_len - 1;
398
                        atomic_inc((atomic_t *)&prof_buffer[pc]);
399
                }
400
        }
401
 
402
#ifdef CONFIG_SMP
403
        /* in UP mode, update_process_times() is invoked by do_timer() */
404
        update_process_times(user_mode(regs));
405
#endif
406
}
407
 
408
/*
409
 * High-level timer interrupt service routines.  This function
410
 * is set as irqaction->handler and is invoked through do_IRQ.
411
 */
412
void timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
413
{
414
        unsigned long j;
415
        unsigned int count;
416
 
417
        count = mips_hpt_read();
418
        mips_timer_ack();
419
 
420
        /* Update timerhi/timerlo for intra-jiffy calibration. */
421
        timerhi += count < timerlo;                     /* Wrap around */
422
        timerlo = count;
423
 
424
        /*
425
         * call the generic timer interrupt handling
426
         */
427
        do_timer(regs);
428
 
429
        /*
430
         * If we have an externally synchronized Linux clock, then update
431
         * CMOS clock accordingly every ~11 minutes. rtc_set_time() has to be
432
         * called as close as possible to 500 ms before the new second starts.
433
         */
434
        read_lock(&xtime_lock);
435
        if ((time_status & STA_UNSYNC) == 0 &&
436
            xtime.tv_sec > last_rtc_update + 660 &&
437
            xtime.tv_usec >= 500000 - ((unsigned) tick) / 2 &&
438
            xtime.tv_usec <= 500000 + ((unsigned) tick) / 2) {
439
                if (rtc_set_mmss(xtime.tv_sec) == 0) {
440
                        last_rtc_update = xtime.tv_sec;
441
                } else {
442
                        /* do it again in 60 s */
443
                        last_rtc_update = xtime.tv_sec - 600;
444
                }
445
        }
446
        read_unlock(&xtime_lock);
447
 
448
        /*
449
         * If jiffies has overflown in this timer_interrupt, we must
450
         * update the timer[hi]/[lo] to make fast gettimeoffset funcs
451
         * quotient calc still valid. -arca
452
         *
453
         * The first timer interrupt comes late as interrupts are
454
         * enabled long after timers are initialized.  Therefore the
455
         * high precision timer is fast, leading to wrong gettimeoffset()
456
         * calculations.  We deal with it by setting it based on the
457
         * number of its ticks between the second and the third interrupt.
458
         * That is still somewhat imprecise, but it's a good estimate.
459
         * --macro
460
         */
461
        j = jiffies;
462
        if (j < 4) {
463
                static unsigned int prev_count;
464
                static int hpt_initialized;
465
 
466
                switch (j) {
467
                case 0:
468
                        timerhi = timerlo = 0;
469
                        mips_hpt_init(count);
470
                        break;
471
                case 2:
472
                        prev_count = count;
473
                        break;
474
                case 3:
475
                        if (!hpt_initialized) {
476
                                unsigned int c3 = 3 * (count - prev_count);
477
 
478
                                timerhi = 0;
479
                                timerlo = c3;
480
                                mips_hpt_init(count - c3);
481
                                hpt_initialized = 1;
482
                        }
483
                        break;
484
                default:
485
                        break;
486
                }
487
        }
488
 
489
#if !defined(CONFIG_SMP)
490
        /*
491
         * In UP mode, we call local_timer_interrupt() to do profiling
492
         * and process accouting.
493
         *
494
         * In SMP mode, local_timer_interrupt() is invoked by appropriate
495
         * low-level local timer interrupt handler.
496
         */
497
        local_timer_interrupt(irq, dev_id, regs);
498
 
499
#else   /* CONFIG_SMP */
500
 
501
        if (emulate_local_timer_interrupt) {
502
                /*
503
                 * this is the place where we send out inter-process
504
                 * interrupts and let each CPU do its own profiling
505
                 * and process accouting.
506
                 *
507
                 * Obviously we need to call local_timer_interrupt() for
508
                 * the current CPU too.
509
                 */
510
                panic("Not implemented yet!!!");
511
        }
512
#endif  /* CONFIG_SMP */
513
}
514
 
515
asmlinkage void ll_timer_interrupt(int irq, struct pt_regs *regs)
516
{
517
        int cpu = smp_processor_id();
518
 
519
        irq_enter(cpu, irq);
520
        kstat.irqs[cpu][irq]++;
521
 
522
        /* we keep interrupt disabled all the time */
523
        timer_interrupt(irq, NULL, regs);
524
 
525
        irq_exit(cpu, irq);
526
 
527
        if (softirq_pending(cpu))
528
                do_softirq();
529
}
530
 
531
asmlinkage void ll_local_timer_interrupt(int irq, struct pt_regs *regs)
532
{
533
        int cpu = smp_processor_id();
534
 
535
        irq_enter(cpu, irq);
536
 
537
        if (cpu != 0)
538
                kstat.irqs[cpu][irq]++;
539
 
540
        /* we keep interrupt disabled all the time */
541
        local_timer_interrupt(irq, NULL, regs);
542
 
543
        irq_exit(cpu, irq);
544
 
545
        if (softirq_pending(cpu))
546
                do_softirq();
547
}
548
 
549
/*
550
 * time_init() - it does the following things.
551
 *
552
 * 1) board_time_init() -
553
 *      a) (optional) set up RTC routines,
554
 *      b) (optional) calibrate and set the mips_hpt_frequency
555
 *          (only needed if you intended to use fixed_rate_gettimeoffset
556
 *           or use cpu counter as timer interrupt source)
557
 * 2) setup xtime based on rtc_get_time().
558
 * 3) choose a appropriate gettimeoffset routine.
559
 * 4) calculate a couple of cached variables for later usage
560
 * 5) board_timer_setup() -
561
 *      a) (optional) over-write any choices made above by time_init().
562
 *      b) machine specific code should setup the timer irqaction.
563
 *      c) enable the timer interrupt
564
 */
565
 
566
void (*board_time_init)(void);
567
void (*board_timer_setup)(struct irqaction *irq);
568
 
569
unsigned int mips_hpt_frequency;
570
 
571
static struct irqaction timer_irqaction = {
572
        .handler = timer_interrupt,
573
        .flags = SA_INTERRUPT,
574
        .name = "timer",
575
};
576
 
577
static unsigned int __init calibrate_hpt(void)
578
{
579
        u64 frequency;
580
        u32 hpt_start, hpt_end, hpt_count, hz;
581
 
582
        const int loops = HZ / 10;
583
        int log_2_loops = 0;
584
        int i;
585
 
586
        /*
587
         * We want to calibrate for 0.1s, but to avoid a 64-bit
588
         * division we round the number of loops up to the nearest
589
         * power of 2.
590
         */
591
        while (loops > 1 << log_2_loops)
592
                log_2_loops++;
593
        i = 1 << log_2_loops;
594
 
595
        /*
596
         * Wait for a rising edge of the timer interrupt.
597
         */
598
        while (mips_timer_state());
599
        while (!mips_timer_state());
600
 
601
        /*
602
         * Now see how many high precision timer ticks happen
603
         * during the calculated number of periods between timer
604
         * interrupts.
605
         */
606
        hpt_start = mips_hpt_read();
607
        do {
608
                while (mips_timer_state());
609
                while (!mips_timer_state());
610
        } while (--i);
611
        hpt_end = mips_hpt_read();
612
 
613
        hpt_count = hpt_end - hpt_start;
614
        hz = HZ;
615
        frequency = (u64)hpt_count * (u64)hz;
616
 
617
        return frequency >> log_2_loops;
618
}
619
 
620
void __init time_init(void)
621
{
622
        if (board_time_init)
623
                board_time_init();
624
 
625
        if (!rtc_set_mmss)
626
                rtc_set_mmss = rtc_set_time;
627
 
628
        xtime.tv_sec = rtc_get_time();
629
        xtime.tv_usec = 0;
630
 
631
        /* Choose appropriate high precision timer routines.  */
632
        if (!cpu_has_counter && !mips_hpt_read) {
633
                /* No high precision timer -- sorry.  */
634
                mips_hpt_read = null_hpt_read;
635
                mips_hpt_init = null_hpt_init;
636
        } else if (!mips_hpt_frequency && !mips_timer_state) {
637
                /* A high precision timer of unknown frequency.  */
638
                if (!mips_hpt_read) {
639
                        /* No external high precision timer -- use R4k.  */
640
                        mips_hpt_read = c0_hpt_read;
641
                        mips_hpt_init = c0_hpt_init;
642
                }
643
 
644
                if ((current_cpu_data.isa_level == MIPS_CPU_ISA_M32) ||
645
                         (current_cpu_data.isa_level == MIPS_CPU_ISA_I) ||
646
                         (current_cpu_data.isa_level == MIPS_CPU_ISA_II))
647
                        /*
648
                         * We need to calibrate the counter but we don't have
649
                         * 64-bit division.
650
                         */
651
                        do_gettimeoffset = calibrate_div32_gettimeoffset;
652
                else
653
                        /*
654
                         * We need to calibrate the counter but we *do* have
655
                         * 64-bit division.
656
                         */
657
                        do_gettimeoffset = calibrate_div64_gettimeoffset;
658
        } else {
659
                /* We know counter frequency.  Or we can get it.  */
660
                if (!mips_hpt_read) {
661
                        /* No external high precision timer -- use R4k.  */
662
                        mips_hpt_read = c0_hpt_read;
663
 
664
                        if (mips_timer_state)
665
                                mips_hpt_init = c0_hpt_init;
666
                        else {
667
                                /* No external timer interrupt -- use R4k.  */
668
                                mips_hpt_init = c0_hpt_timer_init;
669
                                mips_timer_ack = c0_timer_ack;
670
                        }
671
                }
672
                if (!mips_hpt_frequency)
673
                        mips_hpt_frequency = calibrate_hpt();
674
 
675
                do_gettimeoffset = fixed_rate_gettimeoffset;
676
 
677
                /* Calculate cache parameters.  */
678
                cycles_per_jiffy = (mips_hpt_frequency + HZ / 2) / HZ;
679
 
680
                /* sll32_usecs_per_cycle = 10^6 * 2^32 / mips_counter_freq  */
681
                do_div64_32(sll32_usecs_per_cycle,
682
                            1000000, mips_hpt_frequency / 2,
683
                            mips_hpt_frequency);
684
 
685
                /* Report the high precision timer rate for a reference.  */
686
                printk("Using %u.%03u MHz high precision timer.\n",
687
                       ((mips_hpt_frequency + 500) / 1000) / 1000,
688
                       ((mips_hpt_frequency + 500) / 1000) % 1000);
689
        }
690
 
691
        if (!mips_timer_ack)
692
                /* No timer interrupt ack (e.g. i8254).  */
693
                mips_timer_ack = null_timer_ack;
694
 
695
        /* This sets up the high precision timer for the first interrupt.  */
696
        mips_hpt_init(mips_hpt_read());
697
 
698
        /*
699
         * Call board specific timer interrupt setup.
700
         *
701
         * this pointer must be setup in machine setup routine.
702
         *
703
         * Even if a machine chooses to use a low-level timer interrupt,
704
         * it still needs to setup the timer_irqaction.
705
         * In that case, it might be better to set timer_irqaction.handler
706
         * to be NULL function so that we are sure the high-level code
707
         * is not invoked accidentally.
708
         */
709
        board_timer_setup(&timer_irqaction);
710
}
711
 
712
#define FEBRUARY                2
713
#define STARTOFTIME             1970
714
#define SECDAY                  86400L
715
#define SECYR                   (SECDAY * 365)
716
#define leapyear(y)             ((!((y) % 4) && ((y) % 100)) || !((y) % 400))
717
#define days_in_year(y)         (leapyear(y) ? 366 : 365)
718
#define days_in_month(m)        (month_days[(m) - 1])
719
 
720
static int month_days[12] = {
721
        31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
722
};
723
 
724
void to_tm(unsigned long tim, struct rtc_time *tm)
725
{
726
        long hms, day, gday;
727
        int i;
728
 
729
        gday = day = tim / SECDAY;
730
        hms = tim % SECDAY;
731
 
732
        /* Hours, minutes, seconds are easy */
733
        tm->tm_hour = hms / 3600;
734
        tm->tm_min = (hms % 3600) / 60;
735
        tm->tm_sec = (hms % 3600) % 60;
736
 
737
        /* Number of years in days */
738
        for (i = STARTOFTIME; day >= days_in_year(i); i++)
739
                day -= days_in_year(i);
740
        tm->tm_year = i;
741
 
742
        /* Number of months in days left */
743
        if (leapyear(tm->tm_year))
744
                days_in_month(FEBRUARY) = 29;
745
        for (i = 1; day >= days_in_month(i); i++)
746
                day -= days_in_month(i);
747
        days_in_month(FEBRUARY) = 28;
748
        tm->tm_mon = i - 1;             /* tm_mon starts from 0 to 11 */
749
 
750
        /* Days are what is left over (+1) from all that. */
751
        tm->tm_mday = day + 1;
752
 
753
        /*
754
         * Determine the day of week
755
         */
756
        tm->tm_wday = (gday + 4) % 7;   /* 1970/1/1 was Thursday */
757
}
758
 
759
EXPORT_SYMBOL(rtc_lock);
760
EXPORT_SYMBOL(to_tm);
761
EXPORT_SYMBOL(rtc_set_time);
762
EXPORT_SYMBOL(rtc_get_time);

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.