1 /*
2  * comedi_8254.h
3  * Generic 8254 timer/counter support
4  * Copyright (C) 2014 H Hartley Sweeten <hsweeten@visionengravers.com>
5  *
6  * COMEDI - Linux Control and Measurement Device Interface
7  * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  */
19 
20 #ifndef _COMEDI_8254_H
21 #define _COMEDI_8254_H
22 
23 /*
24  * Common oscillator base values in nanoseconds
25  */
26 #define I8254_OSC_BASE_10MHZ	100
27 #define I8254_OSC_BASE_5MHZ	200
28 #define I8254_OSC_BASE_4MHZ	250
29 #define I8254_OSC_BASE_2MHZ	500
30 #define I8254_OSC_BASE_1MHZ	1000
31 #define I8254_OSC_BASE_100KHZ	10000
32 #define I8254_OSC_BASE_10KHZ	100000
33 #define I8254_OSC_BASE_1KHZ	1000000
34 
35 /*
36  * I/O access size used to read/write registers
37  */
38 #define I8254_IO8		1
39 #define I8254_IO16		2
40 #define I8254_IO32		4
41 
42 /*
43  * Register map for generic 8254 timer (I8254_IO8 with 0 regshift)
44  */
45 #define I8254_COUNTER0_REG		0x00
46 #define I8254_COUNTER1_REG		0x01
47 #define I8254_COUNTER2_REG		0x02
48 #define I8254_CTRL_REG			0x03
49 #define I8254_CTRL_SEL_CTR(x)		((x) << 6)
50 #define I8254_CTRL_READBACK_COUNT	((3 << 6) | (1 << 4))
51 #define I8254_CTRL_READBACK_STATUS	((3 << 6) | (1 << 5))
52 #define I8254_CTRL_READBACK_SEL_CTR(x)	(2 << (x))
53 #define I8254_CTRL_LATCH		(0 << 4)
54 #define I8254_CTRL_LSB_ONLY		(1 << 4)
55 #define I8254_CTRL_MSB_ONLY		(2 << 4)
56 #define I8254_CTRL_LSB_MSB		(3 << 4)
57 
58 /* counter maps zero to 0x10000 */
59 #define I8254_MAX_COUNT			0x10000
60 
61 /**
62  * struct comedi_8254 - private data used by this module
63  * @iobase:		PIO base address of the registers (in/out)
64  * @mmio:		MMIO base address of the registers (read/write)
65  * @iosize:		I/O size used to access the registers (b/w/l)
66  * @regshift:		register gap shift
67  * @osc_base:		cascaded oscillator speed in ns
68  * @divisor:		divisor for single counter
69  * @divisor1:		divisor loaded into first cascaded counter
70  * @divisor2:		divisor loaded into second cascaded counter
71  * #next_div:		next divisor for single counter
72  * @next_div1:		next divisor to use for first cascaded counter
73  * @next_div2:		next divisor to use for second cascaded counter
74  * @clock_src;		current clock source for each counter (driver specific)
75  * @gate_src;		current gate source  for each counter (driver specific)
76  * @busy:		flags used to indicate that a counter is "busy"
77  * @insn_config:	driver specific (*insn_config) callback
78  */
79 struct comedi_8254 {
80 	unsigned long iobase;
81 	void __iomem *mmio;
82 	unsigned int iosize;
83 	unsigned int regshift;
84 	unsigned int osc_base;
85 	unsigned int divisor;
86 	unsigned int divisor1;
87 	unsigned int divisor2;
88 	unsigned int next_div;
89 	unsigned int next_div1;
90 	unsigned int next_div2;
91 	unsigned int clock_src[3];
92 	unsigned int gate_src[3];
93 	bool busy[3];
94 
95 	int (*insn_config)(struct comedi_device *, struct comedi_subdevice *s,
96 			   struct comedi_insn *, unsigned int *data);
97 };
98 
99 unsigned int comedi_8254_status(struct comedi_8254 *, unsigned int counter);
100 unsigned int comedi_8254_read(struct comedi_8254 *, unsigned int counter);
101 void comedi_8254_write(struct comedi_8254 *,
102 		       unsigned int counter, unsigned int val);
103 
104 int comedi_8254_set_mode(struct comedi_8254 *,
105 			 unsigned int counter, unsigned int mode);
106 int comedi_8254_load(struct comedi_8254 *,
107 		     unsigned int counter, unsigned int val, unsigned int mode);
108 
109 void comedi_8254_pacer_enable(struct comedi_8254 *,
110 			      unsigned int counter1, unsigned int counter2,
111 			      bool enable);
112 void comedi_8254_update_divisors(struct comedi_8254 *);
113 void comedi_8254_cascade_ns_to_timer(struct comedi_8254 *,
114 				     unsigned int *nanosec, unsigned int flags);
115 void comedi_8254_ns_to_timer(struct comedi_8254 *,
116 			     unsigned int *nanosec, unsigned int flags);
117 
118 void comedi_8254_set_busy(struct comedi_8254 *,
119 			  unsigned int counter, bool busy);
120 
121 void comedi_8254_subdevice_init(struct comedi_subdevice *,
122 				struct comedi_8254 *);
123 
124 struct comedi_8254 *comedi_8254_init(unsigned long iobase,
125 				     unsigned int osc_base,
126 				     unsigned int iosize,
127 				     unsigned int regshift);
128 struct comedi_8254 *comedi_8254_mm_init(void __iomem *mmio,
129 					unsigned int osc_base,
130 					unsigned int iosize,
131 					unsigned int regshift);
132 
133 #endif	/* _COMEDI_8254_H */
134