File: rtl_time.c

package info (click to toggle)
rtlinux 3.1pre3-3
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 4,896 kB
  • ctags: 4,228
  • sloc: ansic: 26,204; sh: 2,069; makefile: 1,414; perl: 855; tcl: 489; asm: 380; cpp: 42
file content (217 lines) | stat: -rw-r--r-- 4,781 bytes parent folder | download | duplicates (2)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * Alpha-specific clock support
 *
 * Copyright (C) 2000 FSM Labs (http://www.fsmlabs.com/)
 *  Written by Cort Dougan <cort@fsmlabs.com>
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/timex.h>
#include <linux/mc146818rtc.h>

#include <asm/smp.h>
#include <asm/system.h>
#include <asm/io.h>
#include <asm/hwrpb.h>

#include <rtl_core.h>
#include <rtl_time.h>
#include <rtl_sync.h>
#include <rtl.h>

MODULE_AUTHOR("Cort Dougan <cort@fsmlabs.com>");
MODULE_DESCRIPTION("RTLinux Alpha Timer Module");

static unsigned long hrtime_last_tick = 0;
static __u32 cycles_last_tick = 0;
struct rtl_clock _i8254_clock;
static unsigned int clock_counter; /* current latch value */

#define wait_value(x) do {; } while ((inb(0x61) & 0x20) != (x))
#define wait_cycle() do { wait_value(0); wait_value(0x20); } while (0)

#define WRITE_COUNTER_ZERO16(x)  { \
	outb_p(x&0xff,0x40); outb_p((x>>8)&0xff,0x40);\
      	clock_counter =x; \
} 

#define WRITE_COUNTER_ZERO8(x)  { \
	outb_p(x&0xff,0x40); \
	clock_counter =x; \
}

#define WRITE_COUNTER_ZERO_ONESHOT(x) WRITE_COUNTER_ZERO16(x)

#define CLATCH (1024 * 32)
#define NLOOPS 50

#define READ_CNT0(var) \
{ var = inb(0x40); var |= (inb(0x40) << 8); }

#define READ_CNT2(var) \
{ var = inb(0x42); var |= (inb(0x42) << 8); }

#define LATCH2 0x8000

#define LATCH_CNT0() \
outb(0xd2, 0x43);

#define LATCH_CNT0_AND_2() \
outb(0xda, 0x43);

#define LATCH_CNT2() \
outb(0xd8, 0x43);

static inline long RTIME_to_8254_ticks(long t)
{
	return t / (NSECS_PER_SEC / CLOCK_TICK_RATE);
}

static int _8254_setperiodic (clockid_t c, hrtime_t interval)
{
	long t;

	t = RTIME_to_8254_ticks (interval) + 1;
	if(t < 2)
		t = 2;
	else if ( t > 0xffff )
		t = 0xffff;
	outb_p(0x34,0x43);              /* binary, mode 2, LSB/MSB, ch 0 */
	WRITE_COUNTER_ZERO16 (t);
	
	_i8254_clock.value = gethrtime();
	_i8254_clock.resolution = interval;
	_i8254_clock.arch.istimerset = 1;
	return 0;
}
	
static int _8254_setoneshot (clockid_t c, hrtime_t interval)
{
	long t;

	t = RTIME_to_8254_ticks (interval);

	if (t < 1) 
		t = 1;
	else if (t > 0xffff)
		t = 0xffff;
	
	WRITE_COUNTER_ZERO_ONESHOT(t);
	_i8254_clock.arch.istimerset = 1;
	return 0;
}

int _8254_settimermode (struct rtl_clock *c, int mode)
{
	if (mode == RTL_CLOCK_MODE_PERIODIC) {
		outb_p(0x30, 0x43);/* 8254, channel 0, mode 0, lsb+msb */
		outb_p(0x34, 0x43); /* binary, mode 2, LSB/MSB, ch 0 */
		_i8254_clock.mode = mode;
		_i8254_clock.settimer = _8254_setperiodic;
		_i8254_clock.arch.count_irqs = 0;
	} else if (mode == RTL_CLOCK_MODE_ONESHOT) {
		outb_p(0x30, 0x43);    /* 8254, channel 0, mode 0, lsb+msb */
		_i8254_clock.mode = mode;
		_i8254_clock.settimer = _8254_setoneshot;
		_i8254_clock.resolution = HRTICKS_PER_SEC / CLOCK_TICK_RATE;
	} else {
		return -EINVAL;
	}
	return 0;
}

static unsigned int _8254_irq(unsigned int irq, struct pt_regs *regs)
{
	/* keep track of the cycle timer */
	__u32 x;
	rdtscl(x);
	hrtime_last_tick += ((hrtime_t)(x - cycles_last_tick) * (hrtime_t)NSECS_PER_SEC) / (ulong)hwrpb->cycle_freq;
	cycles_last_tick = x;

	if (_i8254_clock.mode == RTL_CLOCK_MODE_PERIODIC) {
		if (test_and_set_bit(0, &_i8254_clock.arch.count_irqs)) {
			_i8254_clock.value += _i8254_clock.resolution;
		}
	} else {
		_i8254_clock.arch.istimerset = 0;
	}
	rtl_hard_enable_irq(0);
	_i8254_clock.handler(regs);
	return 0;
}

static int _8254_init (clockid_t clock)
{
	int flags;
	rtl_no_interrupts (flags);
	rdtscl(cycles_last_tick);
	
	if ( rtl_request_global_irq(0, _8254_irq) )
	{
		printk("_8254_init(): failed to get irq for timer\n");
		return -1;
	}
	_8254_settimermode (clock, RTL_CLOCK_MODE_ONESHOT);
	rtl_restore_interrupts (flags);
	return 0;
}

static void _8254_uninit (clockid_t clock)
{
	if (clock -> mode == RTL_CLOCK_MODE_UNINITIALIZED) {
		return;
	}
	clock->handler = RTL_CLOCK_DEFAULTS.handler;
	rtl_free_global_irq(0);
	clock -> mode = RTL_CLOCK_MODE_UNINITIALIZED;
}

hrtime_t _gethrtime(struct rtl_clock *c)
{
	return gethrtime();
}

hrtime_t gethrtime(void)
{
	u32 x = 0;
	rdtscl(x);
	x -= cycles_last_tick;
	return (((ulong)x * (ulong)NSECS_PER_SEC) / (ulong)hwrpb->cycle_freq)
		+ hrtime_last_tick;
}

hrtime_t gethrtimeres(void)
{
	return NSECS_PER_SEC / hwrpb->cycle_freq;
}

int rtl_create_clock_8254(void)
{
	_i8254_clock = RTL_CLOCK_DEFAULTS;
	_i8254_clock.gethrtime = _gethrtime;
	_i8254_clock.init = _8254_init;
	_i8254_clock.uninit = _8254_uninit;
	_i8254_clock.settimermode = _8254_settimermode;
	return 0;
}

clockid_t rtl_getbestclock (unsigned int cpu)
{
	return &_i8254_clock;
}

int init_module (void)
{
	rtl_create_clock_8254();
	rtl_init_standard_clocks();
	return 0;
}

void cleanup_module(void)
{
	rtl_cleanup_standard_clocks();
	_8254_uninit(&_i8254_clock);
}