1 /* Get frequency of the system processor. sparc64 version.
2 Copyright (C) 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #include <sys/ioctl.h>
27 #include <libc-internal.h>
28 #include <asm/openpromio.h>
31 __get_clockfreq_via_cpuinfo (void)
38 fd
= open ("/proc/cpuinfo", O_RDONLY
);
44 n
= read (fd
, buf
, sizeof buf
);
47 char *mhz
= memmem (buf
, n
, "Cpu0ClkTck", 7);
53 /* Search for the beginning of the string. */
55 && (*mhz
< '0' || *mhz
> '9')
56 && (*mhz
< 'a' || *mhz
> 'f')
60 while (mhz
< endp
&& *mhz
!= '\n')
62 if ((*mhz
>= '0' && *mhz
<= '9') ||
63 (*mhz
>= 'a' && *mhz
<= 'f'))
66 if (*mhz
>= '0' && *mhz
<= '9')
69 result
+= (*mhz
- 'a') + 10;
83 __get_clockfreq_via_proc_openprom (void)
90 obp_fd
= open ("/proc/openprom", O_RDONLY
);
93 unsigned long int buf
[4096 / sizeof (unsigned long int)];
94 struct dirent
*dirp
= (struct dirent
*) buf
;
95 off_t dbase
= (off_t
) 0;
98 while ((len
= getdirentries (obp_fd
, (char *) dirp
,
99 sizeof (buf
), &dbase
)) > 0)
101 struct dirent
*this_dirp
= dirp
;
105 char node
[strlen ("/proc/openprom/")
106 + _D_ALLOC_NAMLEN (this_dirp
)
107 + strlen ("/clock-frequency")];
112 strlen("/clock-frequency") > strlen("/device_type")
114 __stpcpy (prop
= __stpcpy (__stpcpy (node
, "/proc/openprom/"),
117 fd
= open (node
, O_RDONLY
);
120 char type_string
[128];
123 ret
= read (fd
, type_string
, sizeof (type_string
));
124 if (ret
> 0 && strncmp (type_string
, "'cpu'", 5) == 0)
128 __stpcpy (prop
, "/clock-frequency");
129 clkfreq_fd
= open (node
, O_RDONLY
);
132 if (read (clkfreq_fd
, type_string
,
133 sizeof (type_string
)) > 0)
134 result
= (hp_timing_t
)
135 strtoull (type_string
, NULL
, 16);
145 len
-= this_dirp
->d_reclen
;
146 this_dirp
= (struct dirent
*)
147 ((char *) this_dirp
+ this_dirp
->d_reclen
);
159 __get_clockfreq_via_dev_openprom (void)
166 obp_dev_fd
= open ("/dev/openprom", O_RDONLY
);
167 if (obp_dev_fd
!= -1)
170 struct openpromio
*obp_cmd
= (struct openpromio
*)obp_buf
;
173 obp_cmd
->oprom_size
=
174 sizeof (obp_buf
) - sizeof (unsigned int);
175 *(int *) obp_cmd
->oprom_array
= 0;
176 ret
= ioctl (obp_dev_fd
, OPROMCHILD
, (char *) obp_cmd
);
179 int cur_node
= *(int *) obp_cmd
->oprom_array
;
181 while (cur_node
!= 0 && cur_node
!= -1)
183 obp_cmd
->oprom_size
= sizeof (obp_buf
) - sizeof (unsigned int);
184 strcpy (obp_cmd
->oprom_array
, "device_type");
185 ret
= ioctl (obp_dev_fd
, OPROMGETPROP
, (char *) obp_cmd
);
187 && strncmp (obp_cmd
->oprom_array
, "cpu", 3) == 0)
189 obp_cmd
->oprom_size
= (sizeof (obp_buf
)
190 - sizeof (unsigned int));
191 strcpy (obp_cmd
->oprom_array
, "clock-frequency");
192 ret
= ioctl (obp_dev_fd
, OPROMGETPROP
, (char *) obp_cmd
);
195 (hp_timing_t
) *(unsigned int *) obp_cmd
->oprom_array
;
197 obp_cmd
->oprom_size
= sizeof (obp_buf
) - sizeof (unsigned int);
198 *(int *) obp_cmd
->oprom_array
= cur_node
;
199 ret
= ioctl (obp_dev_fd
, OPROMNEXT
, (char *) obp_cmd
);
202 cur_node
= *(int *)obp_cmd
->oprom_array
;
211 __get_clockfreq (void)
213 static hp_timing_t result
;
215 /* If this function was called before, we know the result. */
219 /* We first read the information from the /proc/cpuinfo file.
220 It contains at least one line like
221 Cpu0ClkTick : 000000002cb41780
222 We search for this line and convert the number in an integer. */
223 result
= __get_clockfreq_via_cpuinfo ();
227 /* If that did not work, try to find an OpenPROM node
228 with device_type equal to 'cpu' using /dev/openprom
229 and fetch the clock-frequency property from there. */
230 result
= __get_clockfreq_via_dev_openprom ();
234 /* Finally, try the same lookup as above but using /proc/openprom. */
235 result
= __get_clockfreq_via_proc_openprom ();