ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / ftape / lowlevel / ftape-calibr.c
1 /*
2  *      Copyright (C) 1993-1996 Bas Laarhoven.
3
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2, or (at your option)
7  any later version.
8
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  GNU General Public License for more details.
13
14  You should have received a copy of the GNU General Public License
15  along with this program; see the file COPYING.  If not, write to
16  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
17
18  *
19  * $Source: /homes/cvs/ftape-stacked/ftape/lowlevel/ftape-calibr.c,v $
20  * $Revision: 1.2 $
21  * $Date: 1997/10/05 19:18:08 $
22  *
23  *      GP calibration routine for processor speed dependent
24  *      functions.
25  */
26
27 #include <linux/config.h>
28 #include <linux/errno.h>
29 #include <linux/jiffies.h>
30 #include <asm/system.h>
31 #include <asm/io.h>
32 #if defined(__alpha__)
33 # include <asm/hwrpb.h>
34 #elif defined(__i386__) || defined(__x86_64__)
35 # include <linux/timex.h>
36 #endif
37 #include <linux/ftape.h>
38 #include "../lowlevel/ftape-tracing.h"
39 #include "../lowlevel/ftape-calibr.h"
40 #include "../lowlevel/fdc-io.h"
41
42 #undef DEBUG
43
44 #if !defined(__alpha__) && !defined(__i386__) && !defined(__x86_64__)
45 # error Ftape is not implemented for this architecture!
46 #endif
47
48 #if defined(__alpha__)
49 static unsigned long ps_per_cycle = 0;
50 #endif
51
52 static spinlock_t calibr_lock;
53
54 /*
55  * Note: On Intel PCs, the clock ticks at 100 Hz (HZ==100) which is
56  * too slow for certain timeouts (and that clock doesn't even tick
57  * when interrupts are disabled).  For that reason, the 8254 timer is
58  * used directly to implement fine-grained timeouts.  However, on
59  * Alpha PCs, the 8254 is *not* used to implement the clock tick
60  * (which is 1024 Hz, normally) and the 8254 timer runs at some
61  * "random" frequency (it seems to run at 18Hz, but it's not safe to
62  * rely on this value).  Instead, we use the Alpha's "rpcc"
63  * instruction to read cycle counts.  As this is a 32 bit counter,
64  * it will overflow only once per 30 seconds (on a 200MHz machine),
65  * which is plenty.
66  */
67
68 unsigned int ftape_timestamp(void)
69 {
70 #if defined(__alpha__)
71         unsigned long r;
72
73         asm volatile ("rpcc %0" : "=r" (r));
74         return r;
75 #elif defined(__i386__) || defined(__x86_64__)
76         unsigned long flags;
77         __u16 lo;
78         __u16 hi;
79
80         spin_lock_irqsave(&calibr_lock, flags);
81         outb_p(0x00, 0x43);     /* latch the count ASAP */
82         lo = inb_p(0x40);       /* read the latched count */
83         lo |= inb(0x40) << 8;
84         hi = jiffies;
85         spin_unlock_irqrestore(&calibr_lock, flags);
86         return ((hi + 1) * (unsigned int) LATCH) - lo;  /* downcounter ! */
87 #endif
88 }
89
90 static unsigned int short_ftape_timestamp(void)
91 {
92 #if defined(__alpha__)
93         return ftape_timestamp();
94 #elif defined(__i386__) || defined(__x86_64__)
95         unsigned int count;
96         unsigned long flags;
97  
98         spin_lock_irqsave(&calibr_lock, flags);
99         outb_p(0x00, 0x43);     /* latch the count ASAP */
100         count = inb_p(0x40);    /* read the latched count */
101         count |= inb(0x40) << 8;
102         spin_unlock_irqrestore(&calibr_lock, flags);
103         return (LATCH - count); /* normal: downcounter */
104 #endif
105 }
106
107 static unsigned int diff(unsigned int t0, unsigned int t1)
108 {
109 #if defined(__alpha__)
110         return (t1 <= t0) ? t1 + (1UL << 32) - t0 : t1 - t0;
111 #elif defined(__i386__) || defined(__x86_64__)
112         /*
113          * This is tricky: to work for both short and full ftape_timestamps
114          * we'll have to discriminate between these.
115          * If it _looks_ like short stamps with wrapping around we'll
116          * asume it are. This will generate a small error if it really
117          * was a (very large) delta from full ftape_timestamps.
118          */
119         return (t1 <= t0 && t0 <= LATCH) ? t1 + LATCH - t0 : t1 - t0;
120 #endif
121 }
122
123 static unsigned int usecs(unsigned int count)
124 {
125 #if defined(__alpha__)
126         return (ps_per_cycle * count) / 1000000UL;
127 #elif defined(__i386__) || defined(__x86_64__)
128         return (10000 * count) / ((CLOCK_TICK_RATE + 50) / 100);
129 #endif
130 }
131
132 unsigned int ftape_timediff(unsigned int t0, unsigned int t1)
133 {
134         /*
135          *  Calculate difference in usec for ftape_timestamp results t0 & t1.
136          *  Note that on the i386 platform with short time-stamps, the
137          *  maximum allowed timespan is 1/HZ or we'll lose ticks!
138          */
139         return usecs(diff(t0, t1));
140 }
141
142 /*      To get an indication of the I/O performance,
143  *      measure the duration of the inb() function.
144  */
145 static void time_inb(void)
146 {
147         int i;
148         int t0, t1;
149         unsigned long flags;
150         int status;
151         TRACE_FUN(ft_t_any);
152
153         spin_lock_irqsave(&calibr_lock, flags);
154         t0 = short_ftape_timestamp();
155         for (i = 0; i < 1000; ++i) {
156                 status = inb(fdc.msr);
157         }
158         t1 = short_ftape_timestamp();
159         spin_unlock_irqrestore(&calibr_lock, flags);
160         TRACE(ft_t_info, "inb() duration: %d nsec", ftape_timediff(t0, t1));
161         TRACE_EXIT;
162 }
163
164 static void init_clock(void)
165 {
166 #if defined(__i386__) || defined(__x86_64__)
167         unsigned int t;
168         int i;
169         TRACE_FUN(ft_t_any);
170
171         /*  Haven't studied on why, but there sometimes is a problem
172          *  with the tick timer readout. The two bytes get swapped.
173          *  This hack solves that problem by doing one extra input.
174          */
175         for (i = 0; i < 1000; ++i) {
176                 t = short_ftape_timestamp();
177                 if (t > LATCH) {
178                         inb_p(0x40);    /* get in sync again */
179                         TRACE(ft_t_warn, "clock counter fixed");
180                         break;
181                 }
182         }
183 #elif defined(__alpha__)
184 #if CONFIG_FT_ALPHA_CLOCK == 0
185 #error You must define and set CONFIG_FT_ALPHA_CLOCK in 'make config' !
186 #endif
187         extern struct hwrpb_struct *hwrpb;
188         TRACE_FUN(ft_t_any);
189
190         if (hwrpb->cycle_freq != 0) {
191                 ps_per_cycle = (1000*1000*1000*1000UL) / hwrpb->cycle_freq;
192         } else {
193                 /*
194                  * HELP:  Linux 2.0.x doesn't set cycle_freq on my noname !
195                  */
196                 ps_per_cycle = (1000*1000*1000*1000UL) / CONFIG_FT_ALPHA_CLOCK;
197         }
198 #endif
199         TRACE_EXIT;
200 }
201
202 /*
203  *      Input:  function taking int count as parameter.
204  *              pointers to calculated calibration variables.
205  */
206 void ftape_calibrate(char *name,
207                     void (*fun) (unsigned int), 
208                     unsigned int *calibr_count, 
209                     unsigned int *calibr_time)
210 {
211         static int first_time = 1;
212         int i;
213         unsigned int tc = 0;
214         unsigned int count;
215         unsigned int time;
216 #if defined(__i386__) || defined(__x86_64__)
217         unsigned int old_tc = 0;
218         unsigned int old_count = 1;
219         unsigned int old_time = 1;
220 #endif
221         TRACE_FUN(ft_t_flow);
222
223         if (first_time) {             /* get idea of I/O performance */
224                 init_clock();
225                 time_inb();
226                 first_time = 0;
227         }
228         /*    value of timeout must be set so that on very slow systems
229          *    it will give a time less than one jiffy, and on
230          *    very fast systems it'll give reasonable precision.
231          */
232
233         count = 40;
234         for (i = 0; i < 15; ++i) {
235                 unsigned int t0;
236                 unsigned int t1;
237                 unsigned int once;
238                 unsigned int multiple;
239                 unsigned long flags;
240
241                 *calibr_count =
242                 *calibr_time = count;   /* set TC to 1 */
243                 spin_lock_irqsave(&calibr_lock, flags);
244                 fun(0);         /* dummy, get code into cache */
245                 t0 = short_ftape_timestamp();
246                 fun(0);         /* overhead + one test */
247                 t1 = short_ftape_timestamp();
248                 once = diff(t0, t1);
249                 t0 = short_ftape_timestamp();
250                 fun(count);             /* overhead + count tests */
251                 t1 = short_ftape_timestamp();
252                 multiple = diff(t0, t1);
253                 spin_unlock_irqrestore(&calibr_lock, flags);
254                 time = ftape_timediff(0, multiple - once);
255                 tc = (1000 * time) / (count - 1);
256                 TRACE(ft_t_any, "once:%3d us,%6d times:%6d us, TC:%5d ns",
257                         usecs(once), count - 1, usecs(multiple), tc);
258 #if defined(__alpha__)
259                 /*
260                  * Increase the calibration count exponentially until the
261                  * calibration time exceeds 100 ms.
262                  */
263                 if (time >= 100*1000) {
264                         break;
265                 }
266 #elif defined(__i386__) || defined(__x86_64__)
267                 /*
268                  * increase the count until the resulting time nears 2/HZ,
269                  * then the tc will drop sharply because we lose LATCH counts.
270                  */
271                 if (tc <= old_tc / 2) {
272                         time = old_time;
273                         count = old_count;
274                         break;
275                 }
276                 old_tc = tc;
277                 old_count = count;
278                 old_time = time;
279 #endif
280                 count *= 2;
281         }
282         *calibr_count = count - 1;
283         *calibr_time  = time;
284         TRACE(ft_t_info, "TC for `%s()' = %d nsec (at %d counts)",
285              name, (1000 * *calibr_time) / *calibr_count, *calibr_count);
286         TRACE_EXIT;
287 }