1 /* Get frequency of the system processor. sparc64 version.
2 Copyright (C) 2001, 2012 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/>. */
25 #include <sys/ioctl.h>
26 #include <libc-internal.h>
27 #include <asm/openpromio.h>
30 __get_clockfreq_via_cpuinfo (void)
37 fd
= __open ("/proc/cpuinfo", O_RDONLY
);
43 n
= __read (fd
, buf
, sizeof buf
);
46 char *mhz
= memmem (buf
, n
, "Cpu0ClkTck", 7);
52 /* Search for the beginning of the string. */
54 && (*mhz
< '0' || *mhz
> '9')
55 && (*mhz
< 'a' || *mhz
> 'f')
59 while (mhz
< endp
&& *mhz
!= '\n')
61 if ((*mhz
>= '0' && *mhz
<= '9') ||
62 (*mhz
>= 'a' && *mhz
<= 'f'))
65 if (*mhz
>= '0' && *mhz
<= '9')
68 result
+= (*mhz
- 'a') + 10;
82 __get_clockfreq_via_proc_openprom (void)
89 obp_fd
= __open ("/proc/openprom", O_RDONLY
);
92 unsigned long int buf
[4096 / sizeof (unsigned long int)];
93 struct dirent
*dirp
= (struct dirent
*) buf
;
96 while ((len
= __getdents (obp_fd
, (char *) dirp
, sizeof (buf
))) > 0)
98 struct dirent
*this_dirp
= dirp
;
102 char node
[strlen ("/proc/openprom/")
103 + _D_ALLOC_NAMLEN (this_dirp
)
104 + strlen ("/clock-frequency")];
109 strlen("/clock-frequency") > strlen("/device_type")
111 __stpcpy (prop
= __stpcpy (__stpcpy (node
, "/proc/openprom/"),
114 fd
= __open (node
, O_RDONLY
);
117 char type_string
[128];
120 ret
= __read (fd
, type_string
, sizeof (type_string
));
121 if (ret
> 0 && strncmp (type_string
, "'cpu'", 5) == 0)
125 __stpcpy (prop
, "/clock-frequency");
126 clkfreq_fd
= open (node
, O_RDONLY
);
127 if (clkfreq_fd
!= -1)
129 if (read (clkfreq_fd
, type_string
,
130 sizeof (type_string
)) > 0)
131 result
= (hp_timing_t
)
132 strtoull (type_string
, NULL
, 16);
142 len
-= this_dirp
->d_reclen
;
143 this_dirp
= (struct dirent
*)
144 ((char *) this_dirp
+ this_dirp
->d_reclen
);
155 static void set_obp_int (struct openpromio
*op
, int val
)
157 char *cp
= op
->oprom_array
;
158 int *ip
= (int *) cp
;
163 static int get_obp_int (struct openpromio
*op
)
165 char *cp
= op
->oprom_array
;
166 int *ip
= (int *) cp
;
172 __get_clockfreq_via_dev_openprom (void)
179 obp_dev_fd
= __open ("/dev/openprom", O_RDONLY
);
180 if (obp_dev_fd
!= -1)
183 struct openpromio
*obp_cmd
= (struct openpromio
*)obp_buf
;
186 obp_cmd
->oprom_size
=
187 sizeof (obp_buf
) - sizeof (unsigned int);
188 set_obp_int (obp_cmd
, 0);
189 ret
= __ioctl (obp_dev_fd
, OPROMCHILD
, (char *) obp_cmd
);
192 int cur_node
= get_obp_int (obp_cmd
);
194 while (cur_node
!= 0 && cur_node
!= -1)
196 obp_cmd
->oprom_size
= sizeof (obp_buf
) - sizeof (unsigned int);
197 strcpy (obp_cmd
->oprom_array
, "device_type");
198 ret
= __ioctl (obp_dev_fd
, OPROMGETPROP
, (char *) obp_cmd
);
200 && strncmp (obp_cmd
->oprom_array
, "cpu", 3) == 0)
202 obp_cmd
->oprom_size
= (sizeof (obp_buf
)
203 - sizeof (unsigned int));
204 strcpy (obp_cmd
->oprom_array
, "clock-frequency");
205 ret
= __ioctl (obp_dev_fd
, OPROMGETPROP
, (char *) obp_cmd
);
207 result
= (hp_timing_t
) get_obp_int (obp_cmd
);
209 obp_cmd
->oprom_size
= sizeof (obp_buf
) - sizeof (unsigned int);
210 set_obp_int (obp_cmd
, cur_node
);
211 ret
= __ioctl (obp_dev_fd
, OPROMNEXT
, (char *) obp_cmd
);
214 cur_node
= get_obp_int (obp_cmd
);
223 __get_clockfreq (void)
225 static hp_timing_t result
;
227 /* If this function was called before, we know the result. */
231 /* We first read the information from the /proc/cpuinfo file.
232 It contains at least one line like
233 Cpu0ClkTick : 000000002cb41780
234 We search for this line and convert the number in an integer. */
235 result
= __get_clockfreq_via_cpuinfo ();
239 /* If that did not work, try to find an OpenPROM node
240 with device_type equal to 'cpu' using /dev/openprom
241 and fetch the clock-frequency property from there. */
242 result
= __get_clockfreq_via_dev_openprom ();
246 /* Finally, try the same lookup as above but using /proc/openprom. */
247 result
= __get_clockfreq_via_proc_openprom ();