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"
24 #define i8253_cascade_ns_to_timer i8253_cascade_ns_to_timer_2div
26 static inline void i8253_cascade_ns_to_timer_2div_old(int i8253_osc_base,
29 unsigned int *nanosec,
34 int div1_glb, div2_glb, ns_glb;
35 int div1_lub, div2_lub, ns_lub;
38 divider = (*nanosec + i8253_osc_base / 2) / i8253_osc_base;
40 /* find 2 integers 1<={x,y}<=65536 such that x*y is
43 div1_lub = div2_lub = 0;
44 div1_glb = div2_glb = 0;
50 for (div1 = divider / 65536 + 1; div1 < div2; div1++) {
51 div2 = divider / div1;
53 ns = i8253_osc_base * div1 * div2;
54 if (ns <= *nanosec && ns > ns_glb) {
62 ns = i8253_osc_base * div1 * div2;
63 if (ns > *nanosec && ns < ns_lub) {
71 *nanosec = div1_lub * div2_lub * i8253_osc_base;
72 *d1 = div1_lub & 0xffff;
73 *d2 = div2_lub & 0xffff;
77 static inline void i8253_cascade_ns_to_timer_power(int i8253_osc_base,
80 unsigned int *nanosec,
86 for (div1 = 2; div1 <= (1 << 16); div1 <<= 1) {
87 base = i8253_osc_base * div1;
88 round_mode &= TRIG_ROUND_MASK;
90 case TRIG_ROUND_NEAREST:
92 div2 = (*nanosec + base / 2) / base;
95 div2 = (*nanosec) / base;
98 div2 = (*nanosec + base - 1) / base;
104 *nanosec = div2 * base;
111 /* shouldn't get here */
114 *nanosec = div1 * div2 * i8253_osc_base;
119 static inline void i8253_cascade_ns_to_timer_2div(int i8253_osc_base,
122 unsigned int *nanosec,
125 unsigned int divider;
126 unsigned int div1, div2;
127 unsigned int div1_glb, div2_glb, ns_glb;
128 unsigned int div1_lub, div2_lub, ns_lub;
131 unsigned int ns_low, ns_high;
132 static const unsigned int max_count = 0x10000;
133 /* exit early if everything is already correct (this can save time
134 * since this function may be called repeatedly during command tests
136 div1 = *d1 ? *d1 : max_count;
137 div2 = *d2 ? *d2 : max_count;
138 divider = div1 * div2;
139 if (div1 * div2 * i8253_osc_base == *nanosec &&
140 div1 > 1 && div1 <= max_count && 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 /* masking is done since counter maps zero to 0x10000 */
211 /* i8254_load programs 8254 counter chip. It should also work for the 8253.
212 * base_address is the lowest io address
213 * for the chip (the address of counter 0).
214 * counter_number is the counter you want to load (0,1 or 2)
215 * count is the number to load into the counter.
217 * You probably want to use mode 2.
219 * Use i8254_mm_load() if you board uses memory-mapped io, it is
220 * the same as i8254_load() except it uses writeb() instead of outb().
222 * Neither i8254_load() or i8254_read() do their loading/reading
223 * atomically. The 16 bit read/writes are performed with two successive
224 * 8 bit read/writes. So if two parts of your driver do a load/read on
225 * the same counter, it may be necessary to protect these functions
231 #define i8254_control_reg 3
233 static inline int i8254_load(unsigned long base_address, unsigned int regshift,
234 unsigned int counter_number, unsigned int count,
239 if (counter_number > 2)
245 if ((mode == 2 || mode == 3) && count == 1)
248 byte = counter_number << 6;
249 byte |= 0x30; /* load low then high byte */
250 byte |= (mode << 1); /* set counter mode */
251 outb(byte, base_address + (i8254_control_reg << regshift));
252 byte = count & 0xff; /* lsb of counter value */
253 outb(byte, base_address + (counter_number << regshift));
254 byte = (count >> 8) & 0xff; /* msb of counter value */
255 outb(byte, base_address + (counter_number << regshift));
260 static inline int i8254_mm_load(void __iomem *base_address,
261 unsigned int regshift,
262 unsigned int counter_number,
268 if (counter_number > 2)
274 if ((mode == 2 || mode == 3) && count == 1)
277 byte = counter_number << 6;
278 byte |= 0x30; /* load low then high byte */
279 byte |= (mode << 1); /* set counter mode */
280 writeb(byte, base_address + (i8254_control_reg << regshift));
281 byte = count & 0xff; /* lsb of counter value */
282 writeb(byte, base_address + (counter_number << regshift));
283 byte = (count >> 8) & 0xff; /* msb of counter value */
284 writeb(byte, base_address + (counter_number << regshift));
289 /* Returns 16 bit counter value, should work for 8253 also.*/
290 static inline int i8254_read(unsigned long base_address, unsigned int regshift,
291 unsigned int counter_number)
296 if (counter_number > 2)
300 byte = counter_number << 6;
301 outb(byte, base_address + (i8254_control_reg << regshift));
304 ret = inb(base_address + (counter_number << regshift));
306 ret += inb(base_address + (counter_number << regshift)) << 8;
311 static inline int i8254_mm_read(void __iomem *base_address,
312 unsigned int regshift,
313 unsigned int counter_number)
318 if (counter_number > 2)
322 byte = counter_number << 6;
323 writeb(byte, base_address + (i8254_control_reg << regshift));
326 ret = readb(base_address + (counter_number << regshift));
328 ret += readb(base_address + (counter_number << regshift)) << 8;
333 /* Loads 16 bit initial counter value, should work for 8253 also. */
334 static inline void i8254_write(unsigned long base_address,
335 unsigned int regshift,
336 unsigned int counter_number, unsigned int count)
340 if (counter_number > 2)
343 byte = count & 0xff; /* lsb of counter value */
344 outb(byte, base_address + (counter_number << regshift));
345 byte = (count >> 8) & 0xff; /* msb of counter value */
346 outb(byte, base_address + (counter_number << regshift));
349 static inline void i8254_mm_write(void __iomem *base_address,
350 unsigned int regshift,
351 unsigned int counter_number,
356 if (counter_number > 2)
359 byte = count & 0xff; /* lsb of counter value */
360 writeb(byte, base_address + (counter_number << regshift));
361 byte = (count >> 8) & 0xff; /* msb of counter value */
362 writeb(byte, base_address + (counter_number << regshift));
365 /* Set counter mode, should work for 8253 also.
366 * Note: the 'mode' value is different to that for i8254_load() and comes
367 * from the INSN_CONFIG_8254_SET_MODE command:
368 * I8254_MODE0, I8254_MODE1, ..., I8254_MODE5
370 * I8254_BCD, I8254_BINARY
372 static inline int i8254_set_mode(unsigned long base_address,
373 unsigned int regshift,
374 unsigned int counter_number, unsigned int mode)
378 if (counter_number > 2)
380 if (mode > (I8254_MODE5 | I8254_BINARY))
383 byte = counter_number << 6;
384 byte |= 0x30; /* load low then high byte */
385 byte |= mode; /* set counter mode and BCD|binary */
386 outb(byte, base_address + (i8254_control_reg << regshift));
391 static inline int i8254_mm_set_mode(void __iomem *base_address,
392 unsigned int regshift,
393 unsigned int counter_number,
398 if (counter_number > 2)
400 if (mode > (I8254_MODE5 | I8254_BINARY))
403 byte = counter_number << 6;
404 byte |= 0x30; /* load low then high byte */
405 byte |= mode; /* set counter mode and BCD|binary */
406 writeb(byte, base_address + (i8254_control_reg << regshift));
411 static inline int i8254_status(unsigned long base_address,
412 unsigned int regshift,
413 unsigned int counter_number)
415 outb(0xE0 | (2 << counter_number),
416 base_address + (i8254_control_reg << regshift));
417 return inb(base_address + (counter_number << regshift));
420 static inline int i8254_mm_status(void __iomem *base_address,
421 unsigned int regshift,
422 unsigned int counter_number)
424 writeb(0xE0 | (2 << counter_number),
425 base_address + (i8254_control_reg << regshift));
426 return readb(base_address + (counter_number << regshift));