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.
22 #include "../comedi.h"
25 * Common oscillator base values in nanoseconds
27 #define I8254_OSC_BASE_10MHZ 100
28 #define I8254_OSC_BASE_5MHZ 200
29 #define I8254_OSC_BASE_4MHZ 250
30 #define I8254_OSC_BASE_2MHZ 500
31 #define I8254_OSC_BASE_1MHZ 1000
33 #define i8253_cascade_ns_to_timer i8253_cascade_ns_to_timer_2div
35 static inline void i8253_cascade_ns_to_timer_2div_old(int i8253_osc_base,
38 unsigned int *nanosec,
43 int div1_glb, div2_glb, ns_glb;
44 int div1_lub, div2_lub, ns_lub;
47 divider = (*nanosec + i8253_osc_base / 2) / i8253_osc_base;
49 /* find 2 integers 1<={x,y}<=65536 such that x*y is
52 div1_lub = div2_lub = 0;
53 div1_glb = div2_glb = 0;
59 for (div1 = divider / 65536 + 1; div1 < div2; div1++) {
60 div2 = divider / div1;
62 ns = i8253_osc_base * div1 * div2;
63 if (ns <= *nanosec && ns > ns_glb) {
71 ns = i8253_osc_base * div1 * div2;
72 if (ns > *nanosec && ns < ns_lub) {
80 *nanosec = div1_lub * div2_lub * i8253_osc_base;
81 *d1 = div1_lub & 0xffff;
82 *d2 = div2_lub & 0xffff;
86 static inline void i8253_cascade_ns_to_timer_power(int i8253_osc_base,
89 unsigned int *nanosec,
95 for (div1 = 2; div1 <= (1 << 16); div1 <<= 1) {
96 base = i8253_osc_base * div1;
97 round_mode &= TRIG_ROUND_MASK;
99 case TRIG_ROUND_NEAREST:
101 div2 = (*nanosec + base / 2) / base;
103 case TRIG_ROUND_DOWN:
104 div2 = (*nanosec) / base;
107 div2 = (*nanosec + base - 1) / base;
113 *nanosec = div2 * base;
120 /* shouldn't get here */
123 *nanosec = div1 * div2 * i8253_osc_base;
128 static inline void i8253_cascade_ns_to_timer_2div(int i8253_osc_base,
131 unsigned int *nanosec,
134 unsigned int divider;
135 unsigned int div1, div2;
136 unsigned int div1_glb, div2_glb, ns_glb;
137 unsigned int div1_lub, div2_lub, ns_lub;
140 unsigned int ns_low, ns_high;
141 static const unsigned int max_count = 0x10000;
142 /* exit early if everything is already correct (this can save time
143 * since this function may be called repeatedly during command tests
145 div1 = *d1 ? *d1 : max_count;
146 div2 = *d2 ? *d2 : max_count;
147 divider = div1 * div2;
148 if (div1 * div2 * i8253_osc_base == *nanosec &&
149 div1 > 1 && div1 <= max_count && div2 > 1 && div2 <= max_count &&
150 /* check for overflow */
151 divider > div1 && divider > div2 &&
152 divider * i8253_osc_base > divider &&
153 divider * i8253_osc_base > i8253_osc_base) {
157 divider = *nanosec / i8253_osc_base;
159 div1_lub = div2_lub = 0;
160 div1_glb = div2_glb = 0;
166 start = divider / div2;
169 for (div1 = start; div1 <= divider / div1 + 1 && div1 <= max_count;
171 for (div2 = divider / div1;
172 div1 * div2 <= divider + div1 + 1 && div2 <= max_count;
174 ns = i8253_osc_base * div1 * div2;
175 if (ns <= *nanosec && ns > ns_glb) {
180 if (ns >= *nanosec && ns < ns_lub) {
188 round_mode &= TRIG_ROUND_MASK;
189 switch (round_mode) {
190 case TRIG_ROUND_NEAREST:
192 ns_high = div1_lub * div2_lub * i8253_osc_base;
193 ns_low = div1_glb * div2_glb * i8253_osc_base;
194 if (ns_high - *nanosec < *nanosec - ns_low) {
206 case TRIG_ROUND_DOWN:
212 *nanosec = div1 * div2 * i8253_osc_base;
213 /* masking is done since counter maps zero to 0x10000 */
220 /* i8254_load programs 8254 counter chip. It should also work for the 8253.
221 * base_address is the lowest io address
222 * for the chip (the address of counter 0).
223 * counter_number is the counter you want to load (0,1 or 2)
224 * count is the number to load into the counter.
226 * You probably want to use mode 2.
228 * Use i8254_mm_load() if you board uses memory-mapped io, it is
229 * the same as i8254_load() except it uses writeb() instead of outb().
231 * Neither i8254_load() or i8254_read() do their loading/reading
232 * atomically. The 16 bit read/writes are performed with two successive
233 * 8 bit read/writes. So if two parts of your driver do a load/read on
234 * the same counter, it may be necessary to protect these functions
240 #define i8254_control_reg 3
242 static inline int i8254_load(unsigned long base_address, unsigned int regshift,
243 unsigned int counter_number, unsigned int count,
248 if (counter_number > 2)
254 if ((mode == 2 || mode == 3) && count == 1)
257 byte = counter_number << 6;
258 byte |= 0x30; /* load low then high byte */
259 byte |= (mode << 1); /* set counter mode */
260 outb(byte, base_address + (i8254_control_reg << regshift));
261 byte = count & 0xff; /* lsb of counter value */
262 outb(byte, base_address + (counter_number << regshift));
263 byte = (count >> 8) & 0xff; /* msb of counter value */
264 outb(byte, base_address + (counter_number << regshift));
269 static inline int i8254_mm_load(void __iomem *base_address,
270 unsigned int regshift,
271 unsigned int counter_number,
277 if (counter_number > 2)
283 if ((mode == 2 || mode == 3) && count == 1)
286 byte = counter_number << 6;
287 byte |= 0x30; /* load low then high byte */
288 byte |= (mode << 1); /* set counter mode */
289 writeb(byte, base_address + (i8254_control_reg << regshift));
290 byte = count & 0xff; /* lsb of counter value */
291 writeb(byte, base_address + (counter_number << regshift));
292 byte = (count >> 8) & 0xff; /* msb of counter value */
293 writeb(byte, base_address + (counter_number << regshift));
298 /* Returns 16 bit counter value, should work for 8253 also.*/
299 static inline int i8254_read(unsigned long base_address, unsigned int regshift,
300 unsigned int counter_number)
305 if (counter_number > 2)
309 byte = counter_number << 6;
310 outb(byte, base_address + (i8254_control_reg << regshift));
313 ret = inb(base_address + (counter_number << regshift));
315 ret += inb(base_address + (counter_number << regshift)) << 8;
320 static inline int i8254_mm_read(void __iomem *base_address,
321 unsigned int regshift,
322 unsigned int counter_number)
327 if (counter_number > 2)
331 byte = counter_number << 6;
332 writeb(byte, base_address + (i8254_control_reg << regshift));
335 ret = readb(base_address + (counter_number << regshift));
337 ret += readb(base_address + (counter_number << regshift)) << 8;
342 /* Loads 16 bit initial counter value, should work for 8253 also. */
343 static inline void i8254_write(unsigned long base_address,
344 unsigned int regshift,
345 unsigned int counter_number, unsigned int count)
349 if (counter_number > 2)
352 byte = count & 0xff; /* lsb of counter value */
353 outb(byte, base_address + (counter_number << regshift));
354 byte = (count >> 8) & 0xff; /* msb of counter value */
355 outb(byte, base_address + (counter_number << regshift));
358 static inline void i8254_mm_write(void __iomem *base_address,
359 unsigned int regshift,
360 unsigned int counter_number,
365 if (counter_number > 2)
368 byte = count & 0xff; /* lsb of counter value */
369 writeb(byte, base_address + (counter_number << regshift));
370 byte = (count >> 8) & 0xff; /* msb of counter value */
371 writeb(byte, base_address + (counter_number << regshift));
374 /* Set counter mode, should work for 8253 also.
375 * Note: the 'mode' value is different to that for i8254_load() and comes
376 * from the INSN_CONFIG_8254_SET_MODE command:
377 * I8254_MODE0, I8254_MODE1, ..., I8254_MODE5
379 * I8254_BCD, I8254_BINARY
381 static inline int i8254_set_mode(unsigned long base_address,
382 unsigned int regshift,
383 unsigned int counter_number, unsigned int mode)
387 if (counter_number > 2)
389 if (mode > (I8254_MODE5 | I8254_BINARY))
392 byte = counter_number << 6;
393 byte |= 0x30; /* load low then high byte */
394 byte |= mode; /* set counter mode and BCD|binary */
395 outb(byte, base_address + (i8254_control_reg << regshift));
400 static inline int i8254_mm_set_mode(void __iomem *base_address,
401 unsigned int regshift,
402 unsigned int counter_number,
407 if (counter_number > 2)
409 if (mode > (I8254_MODE5 | I8254_BINARY))
412 byte = counter_number << 6;
413 byte |= 0x30; /* load low then high byte */
414 byte |= mode; /* set counter mode and BCD|binary */
415 writeb(byte, base_address + (i8254_control_reg << regshift));
420 static inline int i8254_status(unsigned long base_address,
421 unsigned int regshift,
422 unsigned int counter_number)
424 outb(0xE0 | (2 << counter_number),
425 base_address + (i8254_control_reg << regshift));
426 return inb(base_address + (counter_number << regshift));
429 static inline int i8254_mm_status(void __iomem *base_address,
430 unsigned int regshift,
431 unsigned int counter_number)
433 writeb(0xE0 | (2 << counter_number),
434 base_address + (i8254_control_reg << regshift));
435 return readb(base_address + (counter_number << regshift));