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 static inline void i8253_cascade_ns_to_timer(int i8253_osc_base,
36 unsigned int *nanosec,
40 unsigned int div1, div2;
41 unsigned int div1_glb, div2_glb, ns_glb;
42 unsigned int div1_lub, div2_lub, ns_lub;
45 unsigned int ns_low, ns_high;
46 static const unsigned int max_count = 0x10000;
47 /* exit early if everything is already correct (this can save time
48 * since this function may be called repeatedly during command tests
50 div1 = *d1 ? *d1 : max_count;
51 div2 = *d2 ? *d2 : max_count;
52 divider = div1 * div2;
53 if (div1 * div2 * i8253_osc_base == *nanosec &&
54 div1 > 1 && div1 <= max_count && div2 > 1 && div2 <= max_count &&
55 /* check for overflow */
56 divider > div1 && divider > div2 &&
57 divider * i8253_osc_base > divider &&
58 divider * i8253_osc_base > i8253_osc_base) {
62 divider = *nanosec / i8253_osc_base;
64 div1_lub = div2_lub = 0;
65 div1_glb = div2_glb = 0;
71 start = divider / div2;
74 for (div1 = start; div1 <= divider / div1 + 1 && div1 <= max_count;
76 for (div2 = divider / div1;
77 div1 * div2 <= divider + div1 + 1 && div2 <= max_count;
79 ns = i8253_osc_base * div1 * div2;
80 if (ns <= *nanosec && ns > ns_glb) {
85 if (ns >= *nanosec && ns < ns_lub) {
93 switch (flags & TRIG_ROUND_MASK) {
94 case TRIG_ROUND_NEAREST:
96 ns_high = div1_lub * div2_lub * i8253_osc_base;
97 ns_low = div1_glb * div2_glb * i8253_osc_base;
98 if (ns_high - *nanosec < *nanosec - ns_low) {
110 case TRIG_ROUND_DOWN:
116 *nanosec = div1 * div2 * i8253_osc_base;
117 /* masking is done since counter maps zero to 0x10000 */
123 /* i8254_load programs 8254 counter chip. It should also work for the 8253.
124 * base_address is the lowest io address
125 * for the chip (the address of counter 0).
126 * counter_number is the counter you want to load (0,1 or 2)
127 * count is the number to load into the counter.
129 * You probably want to use mode 2.
131 * Use i8254_mm_load() if you board uses memory-mapped io, it is
132 * the same as i8254_load() except it uses writeb() instead of outb().
134 * Neither i8254_load() or i8254_read() do their loading/reading
135 * atomically. The 16 bit read/writes are performed with two successive
136 * 8 bit read/writes. So if two parts of your driver do a load/read on
137 * the same counter, it may be necessary to protect these functions
143 #define i8254_control_reg 3
145 static inline int i8254_load(unsigned long base_address, unsigned int regshift,
146 unsigned int counter_number, unsigned int count,
151 if (counter_number > 2)
157 if ((mode == 2 || mode == 3) && count == 1)
160 byte = counter_number << 6;
161 byte |= 0x30; /* load low then high byte */
162 byte |= (mode << 1); /* set counter mode */
163 outb(byte, base_address + (i8254_control_reg << regshift));
164 byte = count & 0xff; /* lsb of counter value */
165 outb(byte, base_address + (counter_number << regshift));
166 byte = (count >> 8) & 0xff; /* msb of counter value */
167 outb(byte, base_address + (counter_number << regshift));
172 static inline int i8254_mm_load(void __iomem *base_address,
173 unsigned int regshift,
174 unsigned int counter_number,
180 if (counter_number > 2)
186 if ((mode == 2 || mode == 3) && count == 1)
189 byte = counter_number << 6;
190 byte |= 0x30; /* load low then high byte */
191 byte |= (mode << 1); /* set counter mode */
192 writeb(byte, base_address + (i8254_control_reg << regshift));
193 byte = count & 0xff; /* lsb of counter value */
194 writeb(byte, base_address + (counter_number << regshift));
195 byte = (count >> 8) & 0xff; /* msb of counter value */
196 writeb(byte, base_address + (counter_number << regshift));
201 /* Returns 16 bit counter value, should work for 8253 also.*/
202 static inline int i8254_read(unsigned long base_address, unsigned int regshift,
203 unsigned int counter_number)
208 if (counter_number > 2)
212 byte = counter_number << 6;
213 outb(byte, base_address + (i8254_control_reg << regshift));
216 ret = inb(base_address + (counter_number << regshift));
218 ret += inb(base_address + (counter_number << regshift)) << 8;
223 static inline int i8254_mm_read(void __iomem *base_address,
224 unsigned int regshift,
225 unsigned int counter_number)
230 if (counter_number > 2)
234 byte = counter_number << 6;
235 writeb(byte, base_address + (i8254_control_reg << regshift));
238 ret = readb(base_address + (counter_number << regshift));
240 ret += readb(base_address + (counter_number << regshift)) << 8;
245 /* Loads 16 bit initial counter value, should work for 8253 also. */
246 static inline void i8254_write(unsigned long base_address,
247 unsigned int regshift,
248 unsigned int counter_number, unsigned int count)
252 if (counter_number > 2)
255 byte = count & 0xff; /* lsb of counter value */
256 outb(byte, base_address + (counter_number << regshift));
257 byte = (count >> 8) & 0xff; /* msb of counter value */
258 outb(byte, base_address + (counter_number << regshift));
261 static inline void i8254_mm_write(void __iomem *base_address,
262 unsigned int regshift,
263 unsigned int counter_number,
268 if (counter_number > 2)
271 byte = count & 0xff; /* lsb of counter value */
272 writeb(byte, base_address + (counter_number << regshift));
273 byte = (count >> 8) & 0xff; /* msb of counter value */
274 writeb(byte, base_address + (counter_number << regshift));
277 /* Set counter mode, should work for 8253 also.
278 * Note: the 'mode' value is different to that for i8254_load() and comes
279 * from the INSN_CONFIG_8254_SET_MODE command:
280 * I8254_MODE0, I8254_MODE1, ..., I8254_MODE5
282 * I8254_BCD, I8254_BINARY
284 static inline int i8254_set_mode(unsigned long base_address,
285 unsigned int regshift,
286 unsigned int counter_number, unsigned int mode)
290 if (counter_number > 2)
292 if (mode > (I8254_MODE5 | I8254_BCD))
295 byte = counter_number << 6;
296 byte |= 0x30; /* load low then high byte */
297 byte |= mode; /* set counter mode and BCD|binary */
298 outb(byte, base_address + (i8254_control_reg << regshift));
303 static inline int i8254_mm_set_mode(void __iomem *base_address,
304 unsigned int regshift,
305 unsigned int counter_number,
310 if (counter_number > 2)
312 if (mode > (I8254_MODE5 | I8254_BCD))
315 byte = counter_number << 6;
316 byte |= 0x30; /* load low then high byte */
317 byte |= mode; /* set counter mode and BCD|binary */
318 writeb(byte, base_address + (i8254_control_reg << regshift));
323 static inline int i8254_status(unsigned long base_address,
324 unsigned int regshift,
325 unsigned int counter_number)
327 outb(0xE0 | (2 << counter_number),
328 base_address + (i8254_control_reg << regshift));
329 return inb(base_address + (counter_number << regshift));
332 static inline int i8254_mm_status(void __iomem *base_address,
333 unsigned int regshift,
334 unsigned int counter_number)
336 writeb(0xE0 | (2 << counter_number),
337 base_address + (i8254_control_reg << regshift));
338 return readb(base_address + (counter_number << regshift));