1 /* Get frequency of the system processor. sparc64 version.
2 Copyright (C) 2001-2014 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, see
17 <http://www.gnu.org/licenses/>. */
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
;
97 while ((len
= __getdents (obp_fd
, (char *) dirp
, sizeof (buf
))) > 0)
99 struct dirent
*this_dirp
= dirp
;
103 char node
[strlen ("/proc/openprom/")
104 + _D_ALLOC_NAMLEN (this_dirp
)
105 + strlen ("/clock-frequency")];
110 strlen("/clock-frequency") > strlen("/device_type")
112 __stpcpy (prop
= __stpcpy (__stpcpy (node
, "/proc/openprom/"),
115 fd
= __open (node
, O_RDONLY
);
118 char type_string
[128];
121 ret
= __read (fd
, type_string
, sizeof (type_string
));
122 if (ret
> 0 && strncmp (type_string
, "'cpu'", 5) == 0)
126 __stpcpy (prop
, "/clock-frequency");
127 clkfreq_fd
= __open (node
, O_RDONLY
);
128 if (clkfreq_fd
!= -1)
130 if (__read (clkfreq_fd
, type_string
,
131 sizeof (type_string
)) > 0)
132 result
= (hp_timing_t
)
133 strtoumax (type_string
, NULL
, 16);
134 __close (clkfreq_fd
);
143 len
-= this_dirp
->d_reclen
;
144 this_dirp
= (struct dirent
*)
145 ((char *) this_dirp
+ this_dirp
->d_reclen
);
156 static void set_obp_int (struct openpromio
*op
, int val
)
158 char *cp
= op
->oprom_array
;
159 int *ip
= (int *) cp
;
164 static int get_obp_int (struct openpromio
*op
)
166 char *cp
= op
->oprom_array
;
167 int *ip
= (int *) cp
;
173 __get_clockfreq_via_dev_openprom (void)
180 obp_dev_fd
= __open ("/dev/openprom", O_RDONLY
);
181 if (obp_dev_fd
!= -1)
184 struct openpromio
*obp_cmd
= (struct openpromio
*)obp_buf
;
187 obp_cmd
->oprom_size
=
188 sizeof (obp_buf
) - sizeof (unsigned int);
189 set_obp_int (obp_cmd
, 0);
190 ret
= __ioctl (obp_dev_fd
, OPROMCHILD
, (char *) obp_cmd
);
193 int cur_node
= get_obp_int (obp_cmd
);
195 while (cur_node
!= 0 && cur_node
!= -1)
197 obp_cmd
->oprom_size
= sizeof (obp_buf
) - sizeof (unsigned int);
198 strcpy (obp_cmd
->oprom_array
, "device_type");
199 ret
= __ioctl (obp_dev_fd
, OPROMGETPROP
, (char *) obp_cmd
);
201 && strncmp (obp_cmd
->oprom_array
, "cpu", 3) == 0)
203 obp_cmd
->oprom_size
= (sizeof (obp_buf
)
204 - sizeof (unsigned int));
205 strcpy (obp_cmd
->oprom_array
, "clock-frequency");
206 ret
= __ioctl (obp_dev_fd
, OPROMGETPROP
, (char *) obp_cmd
);
208 result
= (hp_timing_t
) get_obp_int (obp_cmd
);
210 obp_cmd
->oprom_size
= sizeof (obp_buf
) - sizeof (unsigned int);
211 set_obp_int (obp_cmd
, cur_node
);
212 ret
= __ioctl (obp_dev_fd
, OPROMNEXT
, (char *) obp_cmd
);
215 cur_node
= get_obp_int (obp_cmd
);
224 __get_clockfreq (void)
226 static hp_timing_t result
;
228 /* If this function was called before, we know the result. */
232 /* We first read the information from the /proc/cpuinfo file.
233 It contains at least one line like
234 Cpu0ClkTick : 000000002cb41780
235 We search for this line and convert the number in an integer. */
236 result
= __get_clockfreq_via_cpuinfo ();
240 /* If that did not work, try to find an OpenPROM node
241 with device_type equal to 'cpu' using /dev/openprom
242 and fetch the clock-frequency property from there. */
243 result
= __get_clockfreq_via_dev_openprom ();
247 /* Finally, try the same lookup as above but using /proc/openprom. */
248 result
= __get_clockfreq_via_proc_openprom ();