File: timer.c

package info (click to toggle)
silo 0.9.8-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 988 kB
  • ctags: 2,245
  • sloc: ansic: 10,619; asm: 2,783; makefile: 405; perl: 77; sh: 3
file content (301 lines) | stat: -rw-r--r-- 9,140 bytes parent folder | download
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* Timer/counter utilities
   
   Copyright (C) 1996 David S. Miller
                 1996,1997,1998 Jakub Jelinek
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include "silo.h"
#include <asm/mostek.h>

struct sun4m_timer_regs {
	volatile unsigned int limit;
	volatile unsigned int count;
	volatile unsigned int noclear;
	volatile unsigned int foobar;
	volatile unsigned int cfg;
};

struct sun4c_timer_info {
	volatile unsigned int count10;
	volatile unsigned int limit10;
	volatile unsigned int count14;
	volatile unsigned int limit14;
};

static volatile struct sun4m_timer_regs *sun4m_timer;
static volatile struct sun4c_timer_info *sun4c_timer;
static unsigned char *addr_to_free = 0;
static int len_to_free;
static unsigned long long sun4u_tickcmpr;
static int sun4u_notimer = 0;
static struct mostek48t02 *mregs;
static long clock_frequency;

#define TICKER_VIRTUAL 		0xfc000000
#define SUN4C_TIMER_PHYSADDR	0xf3000000
#define PCIC_PHYSADDR		0x300c0000	/* c0000 Registers */
#define PCIC_SYS_LIMIT		0xb8
#define PCIC_SYS_COUNT		0xbc
#ifndef ASI_M_BYPASS
#define ASI_M_BYPASS 0x20
#endif

static unsigned int swab4(unsigned int n)
{
    return ((n>>24)&0xFF) | ((n>>8)&0xFF00) | ((n&0xFF)<<24) | ((n&0xFF00)<<8);
}

static inline int sun4d_init_timer ()
{
    int node = prom_getchild(prom_searchsiblings(prom_getchild(prom_searchsiblings(prom_getchild(prom_root_node), "cpu-unit")), "bootbus"));
    unsigned int addr[2];

    if (!node)
        return -1;    
    node = prom_searchsiblings(node,"eeprom");
    if (!node)
    	return -1;
    if(prom_getproperty(node, "address", (char *)addr, 2*sizeof(int)) == -1)
    	return -1;
    mregs = (struct mostek48t02 *)(addr[0] + 0x1800);
    return 0;
}

static inline int sun4m_init_timer ()
{
    int reg_count;
    struct linux_prom_registers cnt_regs[PROMREG_MAX];
    int obio_node, cnt_node;
    
    cnt_node = 0;
    if ((obio_node = prom_searchsiblings (prom_getchild (prom_root_node), "obio")) == 0 ||
	(obio_node = prom_getchild (obio_node)) == 0 ||
	(cnt_node = prom_searchsiblings (obio_node, "counter")) == 0) {
	return -1;
    }
    reg_count = prom_getproperty (cnt_node, "reg",
				  (void *) cnt_regs, sizeof (cnt_regs));
    reg_count = (reg_count / sizeof (struct linux_prom_registers));

    prom_apply_obio_ranges (cnt_regs, reg_count);
    
    sun4m_timer = (struct sun4m_timer_regs *)(*romvec->pv_v2devops.v2_dumb_mmap)((char *)TICKER_VIRTUAL, cnt_regs[reg_count-1].which_io, (unsigned)cnt_regs[reg_count-1].phys_addr, cnt_regs[reg_count-1].reg_size);
    if (!sun4m_timer) return -1;
    sun4m_timer->limit = 0x7FFFFFFF;
    addr_to_free = (unsigned char *)sun4m_timer;
    len_to_free = cnt_regs[reg_count-1].reg_size;
    return 0;
}

static inline int sun4c_init_timer ()
{
#if 0
    if (romvec->pv_romvers >= 2) {
        sun4c_timer = (struct sun4c_timer_info *)(*romvec->pv_v2devops.v2_dumb_mmap)((char *)TICKER_VIRTUAL, 0, SUN4C_TIMER_PHYSADDR, sizeof (struct sun4c_timer_info));
        if (!sun4c_timer) return -1;
        addr_to_free = (unsigned char *)sun4c_timer;
        len_to_free = sizeof (struct sun4c_timer_info);
    } else 
#endif
    {
        sun4c_timer = (struct sun4c_timer_info *)TICKER_VIRTUAL;
    	if (sun4c_mapio (SUN4C_TIMER_PHYSADDR, TICKER_VIRTUAL, 0) < 0)
    	    return -1;
        addr_to_free = (unsigned char *)0xffffffff;
    }
    sun4c_timer->limit10 = 0x7FFFFFFF;
    return 0;
}

static int sun4p_init_timer ()
{
    __asm__ __volatile__("sta %2, [%0] %1\n\t": :
        "r" (PCIC_PHYSADDR+PCIC_SYS_LIMIT),
        "i" (ASI_M_BYPASS), "r" (0xFFFFFF7F) : "memory");
    /* Could allocate something here... */
    addr_to_free = 0;
    return 0;
}

static inline int sun4u_init_timer ()
{
    char node_str[128];
    int node, foundcpu, notimer;
    
    addr_to_free = 0; 
    foundcpu = 0;
    notimer = 1;
    node = prom_getchild(prom_root_node);
    while ((node = prom_getsibling(node)) != 0) {
    	if (!foundcpu) {
    	    prom_getstring(node, "device_type", node_str, sizeof(node_str));
            if (!strcmp(node_str, "cpu")) {
                foundcpu = 1;
                clock_frequency = prom_getintdefault(node, "clock-frequency", 0) / 100;
            }
        }
        if (notimer) {
            prom_getstring(node, "name", node_str, sizeof(node_str));
            if (!strcmp(node_str, "counter-timer"))
                notimer = 0;
        }
    }
    if (!foundcpu || !clock_frequency)
        clock_frequency = prom_getint(prom_root_node, "clock-frequency") / 100;
    if (notimer) {
        sun4u_notimer = 1;
        __asm__ __volatile__ ("
        	rd	%%tick_cmpr, %%g1
        	stx	%%g1, [%0]
        	mov	1, %%g1
        	sllx    %%g1, 63, %%g1
        	wr      %%g1, 0, %%tick_cmpr"
        	: : "r" (&sun4u_tickcmpr) : "g1");
    }
    return 0;
}

static inline unsigned long mktime(unsigned int year, unsigned int mon,
        unsigned int day, unsigned int hour,
        unsigned int min, unsigned int sec)
{
        if (0 >= (int) (mon -= 2)) {    /* 1..12 -> 11,12,1..10 */
                mon += 12;      /* Puts Feb last since it has leap day */
                year -= 1;
        }
        return (((
            (unsigned long)(year/4 - year/100 + year/400 + 367*mon/12 + day) +
              year*365 - 719499
            )*24 + hour /* now have hours */
           )*60 + min /* now have minutes */
          )*60 + sec; /* finally seconds */
}

int init_timer ()
{
    int retval;
    
    switch (architecture) {
        case sun4c: retval = sun4c_init_timer (); break;
        case sun4m: retval = sun4m_init_timer (); break;
        case sun4d: retval = sun4d_init_timer (); break;
        case sun4p: retval = sun4p_init_timer (); break;
        case sun4u: retval = sun4u_init_timer (); break;
        default: retval = -1; break;
    }  
    if (retval < 0) {
        addr_to_free = 0;
        return -1;
    }
    return 0;
}

void close_timer ()
{
    if (sun4u_notimer) {
        __asm__ __volatile__("
        	ldx	[%0], %%g1
        	wrpr	%%g0, 0, %%tick
        	wr	%%g1, 0, %%tick_cmpr"
        	: : "r" (&sun4u_tickcmpr) : "g1");
    }
    if (addr_to_free) {
        if (addr_to_free == (unsigned char *)0xffffffff)
            sun4c_unmapio (TICKER_VIRTUAL);
        else
	    (*romvec->pv_v2devops.v2_dumb_munmap)(addr_to_free, len_to_free);
	addr_to_free = 0;
    }
}

static long long ticks = 0;

void reset_ticks (void)
{
    ticks = 0;
    if (architecture == sun4u) {
        __asm__ __volatile__ ("
        	rd	%%tick, %%g1
        	stx	%%g1, [%0]
        	" : : "r" (&ticks) : "g1");
    }
}

static inline unsigned int sun4d_read(void)
{
	unsigned int year, mon, day, hour, min, sec;

	for (;;) {
	        sec = MSTK_REG_SEC(mregs);
        	min = MSTK_REG_MIN(mregs);
	        hour = MSTK_REG_HOUR(mregs);
        	day = MSTK_REG_DOM(mregs);
	        mon = MSTK_REG_MONTH(mregs);
        	year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
        	if (MSTK_REG_SEC(mregs) == sec)
        		break;
        }
        return mktime(year, mon, day, hour, min, sec);
}

static unsigned int sun4p_lda(unsigned int addr)
{
    register unsigned int w;
    __asm__ __volatile__("\n\tlda [%1] %2, %0\n\t" : "=r" (w) :
                                "r" (addr), "i" (ASI_M_BYPASS));
    return swab4(w);
}

/* 10ms ticks */
int get_ticks (void)
{
    unsigned int i;
    static unsigned int lasti = 0;
    
    switch (architecture) {
        case sun4c: i = sun4c_timer->count10; break;
        case sun4m: i = sun4m_timer->count; break;
        case sun4p: i = sun4p_lda(PCIC_PHYSADDR+PCIC_SYS_COUNT);
                    i >>= 2;  /* PROLL does this and its timing is sure right */
                    break;
        case sun4d: /* I cannot get the normal sun4d timer working
		       during bootstrapping, so unfortunately I can give
		       just a 1000ms precision. */
		    if (!lasti) {
			lasti = (sun4d_read() * 100) + 100;
			return 0;
		    }
		    i = sun4d_read() * 100;
		    if (i <= lasti) return 0;
		    return i - lasti;
        case sun4u: 
        	__asm__ __volatile__ ("
        		ldx	[%2], %%g2
        		rd	%%tick, %%g1
        		sub	%%g1, %%g2, %%g1
        		udivx	%%g1, %1, %0
        	" : "=r" (i) : "r" (clock_frequency), "r" (&ticks) : "g1", "g2");
        	return i;
        default: return 0;
    }
    i >>= 9;
    if (i >= lasti)
        ticks += i - lasti;
    else
        ticks += (1 << 22) + i - lasti - 1;
    lasti = i;
    return (int)(ticks / 20000);
}