5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 2000 David A. Schleef <ds@schleef.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "../comedi.h"
29 #define i8253_cascade_ns_to_timer i8253_cascade_ns_to_timer_2div
31 static inline void i8253_cascade_ns_to_timer_2div_old(int i8253_osc_base,
32 unsigned int *d1, unsigned int *d2, unsigned int *nanosec,
37 int div1_glb, div2_glb, ns_glb;
38 int div1_lub, div2_lub, ns_lub;
41 divider = (*nanosec + i8253_osc_base / 2) / i8253_osc_base;
43 /* find 2 integers 1<={x,y}<=65536 such that x*y is
46 div1_lub = div2_lub = 0;
47 div1_glb = div2_glb = 0;
53 for (div1 = divider / 65536 + 1; div1 < div2; div1++) {
54 div2 = divider / div1;
56 ns = i8253_osc_base * div1 * div2;
57 if (ns <= *nanosec && ns > ns_glb) {
65 ns = i8253_osc_base * div1 * div2;
66 if (ns > *nanosec && ns < ns_lub) {
74 *nanosec = div1_lub * div2_lub * i8253_osc_base;
75 *d1 = div1_lub & 0xffff;
76 *d2 = div2_lub & 0xffff;
80 static inline void i8253_cascade_ns_to_timer_power(int i8253_osc_base,
81 unsigned int *d1, unsigned int *d2, unsigned int *nanosec,
87 for (div1 = 2; div1 <= (1 << 16); div1 <<= 1) {
88 base = i8253_osc_base * div1;
89 round_mode &= TRIG_ROUND_MASK;
91 case TRIG_ROUND_NEAREST:
93 div2 = (*nanosec + base / 2) / base;
96 div2 = (*nanosec) / base;
99 div2 = (*nanosec + base - 1) / base;
105 *nanosec = div2 * base;
112 /* shouldn't get here */
115 *nanosec = div1 * div2 * i8253_osc_base;
120 static inline void i8253_cascade_ns_to_timer_2div(int i8253_osc_base,
121 unsigned int *d1, unsigned int *d2, unsigned int *nanosec,
124 unsigned int divider;
125 unsigned int div1, div2;
126 unsigned int div1_glb, div2_glb, ns_glb;
127 unsigned int div1_lub, div2_lub, ns_lub;
130 unsigned int ns_low, ns_high;
131 static const unsigned int max_count = 0x10000;
132 /* exit early if everything is already correct (this can save time
133 * since this function may be called repeatedly during command tests
135 div1 = *d1 ? *d1 : max_count;
136 div2 = *d2 ? *d2 : max_count;
137 divider = div1 * div2;
138 if (div1 * div2 * i8253_osc_base == *nanosec &&
139 div1 > 1 && div1 <= max_count &&
140 div2 > 1 && div2 <= max_count &&
141 /* check for overflow */
142 divider > div1 && divider > div2 &&
143 divider * i8253_osc_base > divider &&
144 divider * i8253_osc_base > i8253_osc_base) {
148 divider = *nanosec / i8253_osc_base;
150 div1_lub = div2_lub = 0;
151 div1_glb = div2_glb = 0;
157 start = divider / div2;
160 for (div1 = start; div1 <= divider / div1 + 1 && div1 <= max_count;
162 for (div2 = divider / div1;
163 div1 * div2 <= divider + div1 + 1 && div2 <= max_count;
165 ns = i8253_osc_base * div1 * div2;
166 if (ns <= *nanosec && ns > ns_glb) {
171 if (ns >= *nanosec && ns < ns_lub) {
179 round_mode &= TRIG_ROUND_MASK;
180 switch (round_mode) {
181 case TRIG_ROUND_NEAREST:
183 ns_high = div1_lub * div2_lub * i8253_osc_base;
184 ns_low = div1_glb * div2_glb * i8253_osc_base;
185 if (ns_high - *nanosec < *nanosec - ns_low) {
197 case TRIG_ROUND_DOWN:
203 *nanosec = div1 * div2 * i8253_osc_base;
204 *d1 = div1 & 0xffff; /* masking is done since counter maps zero to 0x10000 */
210 /* i8254_load programs 8254 counter chip. It should also work for the 8253.
211 * base_address is the lowest io address for the chip (the address of counter 0).
212 * counter_number is the counter you want to load (0,1 or 2)
213 * count is the number to load into the counter.
215 * You probably want to use mode 2.
217 * Use i8254_mm_load() if you board uses memory-mapped io, it is
218 * the same as i8254_load() except it uses writeb() instead of outb().
220 * Neither i8254_load() or i8254_read() do their loading/reading
221 * atomically. The 16 bit read/writes are performed with two successive
222 * 8 bit read/writes. So if two parts of your driver do a load/read on
223 * the same counter, it may be necessary to protect these functions
229 #define i8254_control_reg 3
231 static inline int i8254_load(unsigned long base_address, unsigned int regshift,
232 unsigned int counter_number, unsigned int count, unsigned int mode)
236 if (counter_number > 2)
242 if ((mode == 2 || mode == 3) && count == 1)
245 byte = counter_number << 6;
246 byte |= 0x30; /* load low then high byte */
247 byte |= (mode << 1); /* set counter mode */
248 outb(byte, base_address + (i8254_control_reg << regshift));
249 byte = count & 0xff; /* lsb of counter value */
250 outb(byte, base_address + (counter_number << regshift));
251 byte = (count >> 8) & 0xff; /* msb of counter value */
252 outb(byte, base_address + (counter_number << regshift));
257 static inline int i8254_mm_load(void *base_address, unsigned int regshift,
258 unsigned int counter_number, unsigned int count, unsigned int mode)
262 if (counter_number > 2)
268 if ((mode == 2 || mode == 3) && count == 1)
271 byte = counter_number << 6;
272 byte |= 0x30; /* load low then high byte */
273 byte |= (mode << 1); /* set counter mode */
274 writeb(byte, base_address + (i8254_control_reg << regshift));
275 byte = count & 0xff; /* lsb of counter value */
276 writeb(byte, base_address + (counter_number << regshift));
277 byte = (count >> 8) & 0xff; /* msb of counter value */
278 writeb(byte, base_address + (counter_number << regshift));
283 /* Returns 16 bit counter value, should work for 8253 also.*/
284 static inline int i8254_read(unsigned long base_address, unsigned int regshift,
285 unsigned int counter_number)
290 if (counter_number > 2)
294 byte = counter_number << 6;
295 outb(byte, base_address + (i8254_control_reg << regshift));
298 ret = inb(base_address + (counter_number << regshift));
300 ret += inb(base_address + (counter_number << regshift)) << 8;
305 static inline int i8254_mm_read(void *base_address, unsigned int regshift,
306 unsigned int counter_number)
311 if (counter_number > 2)
315 byte = counter_number << 6;
316 writeb(byte, base_address + (i8254_control_reg << regshift));
319 ret = readb(base_address + (counter_number << regshift));
321 ret += readb(base_address + (counter_number << regshift)) << 8;
326 /* Loads 16 bit initial counter value, should work for 8253 also. */
327 static inline void i8254_write(unsigned long base_address,
328 unsigned int regshift, unsigned int counter_number, unsigned int count)
332 if (counter_number > 2)
335 byte = count & 0xff; /* lsb of counter value */
336 outb(byte, base_address + (counter_number << regshift));
337 byte = (count >> 8) & 0xff; /* msb of counter value */
338 outb(byte, base_address + (counter_number << regshift));
341 static inline void i8254_mm_write(void *base_address,
342 unsigned int regshift, unsigned int counter_number, unsigned int count)
346 if (counter_number > 2)
349 byte = count & 0xff; /* lsb of counter value */
350 writeb(byte, base_address + (counter_number << regshift));
351 byte = (count >> 8) & 0xff; /* msb of counter value */
352 writeb(byte, base_address + (counter_number << regshift));
355 /* Set counter mode, should work for 8253 also.
356 * Note: the 'mode' value is different to that for i8254_load() and comes
357 * from the INSN_CONFIG_8254_SET_MODE command:
358 * I8254_MODE0, I8254_MODE1, ..., I8254_MODE5
360 * I8254_BCD, I8254_BINARY
362 static inline int i8254_set_mode(unsigned long base_address,
363 unsigned int regshift, unsigned int counter_number, unsigned int mode)
367 if (counter_number > 2)
369 if (mode > (I8254_MODE5 | I8254_BINARY))
372 byte = counter_number << 6;
373 byte |= 0x30; /* load low then high byte */
374 byte |= mode; /* set counter mode and BCD|binary */
375 outb(byte, base_address + (i8254_control_reg << regshift));
380 static inline int i8254_mm_set_mode(void *base_address,
381 unsigned int regshift, unsigned int counter_number, unsigned int mode)
385 if (counter_number > 2)
387 if (mode > (I8254_MODE5 | I8254_BINARY))
390 byte = counter_number << 6;
391 byte |= 0x30; /* load low then high byte */
392 byte |= mode; /* set counter mode and BCD|binary */
393 writeb(byte, base_address + (i8254_control_reg << regshift));
398 static inline int i8254_status(unsigned long base_address,
399 unsigned int regshift, unsigned int counter_number)
401 outb(0xE0 | (2 << counter_number),
402 base_address + (i8254_control_reg << regshift));
403 return inb(base_address + (counter_number << regshift));
406 static inline int i8254_mm_status(void *base_address,
407 unsigned int regshift, unsigned int counter_number)
409 writeb(0xE0 | (2 << counter_number),
410 base_address + (i8254_control_reg << regshift));
411 return readb(base_address + (counter_number << regshift));