xref: /plan9-contrib/sys/src/9/vt5/clock.c (revision 1c9d674cbfa2c1924558af05e99c43e5c5cd4845)
1 /* ppc440 clock */
2 #include	"u.h"
3 #include	"../port/lib.h"
4 #include	"mem.h"
5 #include	"dat.h"
6 #include	"fns.h"
7 
8 void
delay(int l)9 delay(int l)
10 {
11 	ulong i, j;
12 
13 	j = m->delayloop;
14 	while(l-- > 0)
15 		for(i=0; i < j; i++)
16 			;
17 }
18 
19 void
microdelay(int l)20 microdelay(int l)
21 {
22 	ulong i;
23 
24 	l *= m->delayloop;
25 	l /= 1000;
26 	if(l <= 0)
27 		l = 1;
28 	for(i = 0; i < l; i++)
29 		;
30 }
31 
32 enum {
33 	Timebase = 1,	/* system clock cycles per time base cycle */
34 
35 	Wp21=	0<<30,	/* watchdog period (2^x clocks) */
36 	Wp25=	1<<30,
37 	Wp29=	2<<30,
38 	Wp33=	3<<30,
39 	Wrnone=	0<<28,	/* no watchdog reset */
40 	Wrcore=	1<<28,	/* core reset */
41 	Wrchip=	2<<28,	/* chip reset */
42 	Wrsys=	3<<28,	/* system reset */
43 	Wie=		1<<27,	/* watchdog interrupt enable */
44 	Pie=		1<<26,	/* enable PIT interrupt */
45 	Fit13=	0<<24,	/* fit period (2^x clocks) */
46 	Fit17=	1<<24,
47 	Fit21=	2<<24,
48 	Fit25=	3<<24,
49 	Fie=		1<<23,	/* fit interrupt enable */
50 	Are=		1<<22,	/* auto reload enable */
51 
52 	/* dcr */
53 	Boot=	0x0F1,
54 	Epctl=	0x0F3,
55 	Pllmr0=	0x0F0,
56 	Pllmr1=	0x0F4,
57 	Ucr=		0x0F5,
58 };
59 
60 void	(*archclocktick)(void);	/* set by arch*.c if used */
61 
62 void
clockinit(void)63 clockinit(void)
64 {
65 	long x;
66 
67 	m->delayloop = m->cpuhz/1000;	/* initial estimate */
68 	do {
69 		x = gettbl();
70 		delay(10);
71 		x = gettbl() - x;
72 	} while(x < 0);
73 
74 	/*
75 	 *  fix count
76 	 */
77 	m->delayloop = ((vlong)m->delayloop * (10*(vlong)m->clockgen/1000)) /
78 		(x*Timebase);
79 	if((int)m->delayloop <= 0)
80 		m->delayloop = 20000;
81 
82 	x = (m->clockgen/Timebase)/HZ;
83 	putpit(x);
84 	puttsr(~0);
85 	/* not sure if we want Wrnone or Wrcore */
86 //	puttcr(Pie|Are|Wie|Wrcore|Wp29);
87 	puttcr(Pie|Are|Wrnone);
88 }
89 
90 void
clockintr(Ureg * ureg)91 clockintr(Ureg *ureg)
92 {
93 	/* PIT was set to reload automatically */
94 	puttsr(~0);
95 	m->fastclock++;
96 	timerintr(ureg, 0);
97 	if(archclocktick != nil)
98 		archclocktick();
99 }
100 
101 uvlong
fastticks(uvlong * hz)102 fastticks(uvlong *hz)
103 {
104 	if(hz)
105 		*hz = HZ;
106 	return m->fastclock;
107 }
108 
109 ulong
s(void)110 µs(void)
111 {
112 	return fastticks2us(m->fastclock);
113 }
114 
115 void
timerset(uvlong)116 timerset(uvlong)
117 {
118 }
119 
120 ulong
perfticks(void)121 perfticks(void)
122 {
123 	return (ulong)fastticks(nil);
124 }
125 
126 long
lcycles(void)127 lcycles(void)
128 {
129 	return perfticks();
130 }
131