Upload Tizen:Base source
[external/eglibc.git] / sysdeps / unix / sysv / linux / sparc / sparc64 / get_clockfreq.c
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.
4
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.
9
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.
14
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
18    02111-1307 USA.  */
19
20 #include <ctype.h>
21 #include <fcntl.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <dirent.h>
25 #include <stdlib.h>
26 #include <sys/ioctl.h>
27 #include <libc-internal.h>
28 #include <asm/openpromio.h>
29
30 static hp_timing_t
31 __get_clockfreq_via_cpuinfo (void)
32 {
33   hp_timing_t result;
34   int fd;
35
36   result = 0;
37
38   fd = open ("/proc/cpuinfo", O_RDONLY);
39   if (fd != -1)
40     {
41       char buf[8192];
42       ssize_t n;
43
44       n = read (fd, buf, sizeof buf);
45       if (n > 0)
46         {
47           char *mhz = memmem (buf, n, "Cpu0ClkTck", 7);
48
49           if (mhz != NULL)
50             {
51               char *endp = buf + n;
52
53               /* Search for the beginning of the string.  */
54               while (mhz < endp
55                      && (*mhz < '0' || *mhz > '9')
56                      && (*mhz < 'a' || *mhz > 'f')
57                      && *mhz != '\n')
58                 ++mhz;
59
60               while (mhz < endp && *mhz != '\n')
61                 {
62                   if ((*mhz >= '0' && *mhz <= '9') ||
63                       (*mhz >= 'a' && *mhz <= 'f'))
64                     {
65                       result <<= 4;
66                       if (*mhz >= '0' && *mhz <= '9')
67                         result += *mhz - '0';
68                       else
69                         result += (*mhz - 'a') + 10;
70                     }
71                   ++mhz;
72                 }
73             }
74         }
75
76       close (fd);
77     }
78
79   return result;
80 }
81
82 static hp_timing_t
83 __get_clockfreq_via_proc_openprom (void)
84 {
85   hp_timing_t result;
86   int obp_fd;
87
88   result = 0;
89
90   obp_fd = open ("/proc/openprom", O_RDONLY);
91   if (obp_fd != -1)
92     {
93       unsigned long int buf[4096 / sizeof (unsigned long int)];
94       struct dirent *dirp = (struct dirent *) buf;
95       off_t dbase = (off_t) 0;
96       ssize_t len;
97
98       while ((len = getdirentries (obp_fd, (char *) dirp,
99                                    sizeof (buf), &dbase)) > 0)
100         {
101           struct dirent *this_dirp = dirp;
102
103           while (len > 0)
104             {
105               char node[strlen ("/proc/openprom/")
106                         + _D_ALLOC_NAMLEN (this_dirp)
107                         + strlen ("/clock-frequency")];
108               char *prop;
109               int fd;
110
111               /* Note that
112                    strlen("/clock-frequency") > strlen("/device_type")
113               */
114               __stpcpy (prop = __stpcpy (__stpcpy (node, "/proc/openprom/"),
115                                          this_dirp->d_name),
116                         "/device_type");
117               fd = open (node, O_RDONLY);
118               if (fd != -1)
119                 {
120                   char type_string[128];
121                   int ret;
122
123                   ret = read (fd, type_string, sizeof (type_string));
124                   if (ret > 0 && strncmp (type_string, "'cpu'", 5) == 0)
125                     {
126                       int clkfreq_fd;
127
128                       __stpcpy (prop, "/clock-frequency");
129                       clkfreq_fd = open (node, O_RDONLY);
130                       if (fd != -1)
131                         {
132                           if (read (clkfreq_fd, type_string,
133                                     sizeof (type_string)) > 0)
134                             result = (hp_timing_t)
135                               strtoull (type_string, NULL, 16);
136                           close (clkfreq_fd);
137                         }
138                     }
139                   close (fd);
140                 }
141
142               if (result != 0)
143                 break;
144
145               len -= this_dirp->d_reclen;
146               this_dirp = (struct dirent *)
147                 ((char *) this_dirp + this_dirp->d_reclen);
148             }
149           if (result != 0)
150             break;
151         }
152       close (obp_fd);
153     }
154
155   return result;
156 }
157
158 static hp_timing_t
159 __get_clockfreq_via_dev_openprom (void)
160 {
161   hp_timing_t result;
162   int obp_dev_fd;
163
164   result = 0;
165
166   obp_dev_fd = open ("/dev/openprom", O_RDONLY);
167   if (obp_dev_fd != -1)
168     {
169       char obp_buf[8192];
170       struct openpromio *obp_cmd = (struct openpromio *)obp_buf;
171       int ret;
172
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);
177       if (ret == 0)
178         {
179           int cur_node = *(int *) obp_cmd->oprom_array;
180
181           while (cur_node != 0 && cur_node != -1)
182             {
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);
186               if (ret == 0
187                   && strncmp (obp_cmd->oprom_array, "cpu", 3) == 0)
188                 {
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);
193                   if (ret == 0)
194                     result =
195                       (hp_timing_t) *(unsigned int *) obp_cmd->oprom_array;
196                 }
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);
200               if (ret < 0)
201                 break;
202               cur_node = *(int *)obp_cmd->oprom_array;
203             }
204         }
205     }
206
207   return result;
208 }
209
210 hp_timing_t
211 __get_clockfreq (void)
212 {
213   static hp_timing_t result;
214
215   /* If this function was called before, we know the result.  */
216   if (result != 0)
217     return result;
218
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 ();
224   if (result != 0)
225     return result;
226
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 ();
231   if (result != 0)
232     return result;
233
234   /* Finally, try the same lookup as above but using /proc/openprom.  */
235   result = __get_clockfreq_via_proc_openprom ();
236
237   return result;
238 }