| 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
| 2 | /* |
| 3 | * comedi_8254.h |
| 4 | * Generic 8254 timer/counter support |
| 5 | * Copyright (C) 2014 H Hartley Sweeten <hsweeten@visionengravers.com> |
| 6 | * |
| 7 | * COMEDI - Linux Control and Measurement Device Interface |
| 8 | * Copyright (C) 2000 David A. Schleef <ds@schleef.org> |
| 9 | */ |
| 10 | |
| 11 | #ifndef _COMEDI_8254_H |
| 12 | #define _COMEDI_8254_H |
| 13 | |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/err.h> |
| 17 | |
| 18 | struct comedi_device; |
| 19 | struct comedi_insn; |
| 20 | struct comedi_subdevice; |
| 21 | |
| 22 | /* |
| 23 | * Common oscillator base values in nanoseconds |
| 24 | */ |
| 25 | #define I8254_OSC_BASE_10MHZ 100 |
| 26 | #define I8254_OSC_BASE_5MHZ 200 |
| 27 | #define I8254_OSC_BASE_4MHZ 250 |
| 28 | #define I8254_OSC_BASE_2MHZ 500 |
| 29 | #define I8254_OSC_BASE_1MHZ 1000 |
| 30 | #define I8254_OSC_BASE_100KHZ 10000 |
| 31 | #define I8254_OSC_BASE_10KHZ 100000 |
| 32 | #define I8254_OSC_BASE_1KHZ 1000000 |
| 33 | |
| 34 | /* |
| 35 | * I/O access size used to read/write registers |
| 36 | */ |
| 37 | #define I8254_IO8 1 |
| 38 | #define I8254_IO16 2 |
| 39 | #define I8254_IO32 4 |
| 40 | |
| 41 | /* |
| 42 | * Register map for generic 8254 timer (I8254_IO8 with 0 regshift) |
| 43 | */ |
| 44 | #define I8254_COUNTER0_REG 0x00 |
| 45 | #define I8254_COUNTER1_REG 0x01 |
| 46 | #define I8254_COUNTER2_REG 0x02 |
| 47 | #define I8254_CTRL_REG 0x03 |
| 48 | #define I8254_CTRL_SEL_CTR(x) ((x) << 6) |
| 49 | #define I8254_CTRL_READBACK(x) (I8254_CTRL_SEL_CTR(3) | BIT(x)) |
| 50 | #define I8254_CTRL_READBACK_COUNT I8254_CTRL_READBACK(4) |
| 51 | #define I8254_CTRL_READBACK_STATUS I8254_CTRL_READBACK(5) |
| 52 | #define I8254_CTRL_READBACK_SEL_CTR(x) (2 << (x)) |
| 53 | #define I8254_CTRL_RW(x) (((x) & 0x3) << 4) |
| 54 | #define I8254_CTRL_LATCH I8254_CTRL_RW(0) |
| 55 | #define I8254_CTRL_LSB_ONLY I8254_CTRL_RW(1) |
| 56 | #define I8254_CTRL_MSB_ONLY I8254_CTRL_RW(2) |
| 57 | #define I8254_CTRL_LSB_MSB I8254_CTRL_RW(3) |
| 58 | |
| 59 | /* counter maps zero to 0x10000 */ |
| 60 | #define I8254_MAX_COUNT 0x10000 |
| 61 | |
| 62 | struct comedi_8254; |
| 63 | |
| 64 | /** |
| 65 | * typedef comedi_8254_iocb_fn - call-back function type for 8254 register access |
| 66 | * @i8254: pointer to struct comedi_8254 |
| 67 | * @dir: direction (0 = read, 1 = write) |
| 68 | * @reg: register number |
| 69 | * @val: value to write |
| 70 | * |
| 71 | * Return: Register value when reading, 0 when writing. |
| 72 | */ |
| 73 | typedef unsigned int comedi_8254_iocb_fn(struct comedi_8254 *i8254, int dir, |
| 74 | unsigned int reg, unsigned int val); |
| 75 | |
| 76 | /** |
| 77 | * struct comedi_8254 - private data used by this module |
| 78 | * @iocb: I/O call-back function for register access |
| 79 | * @context: context for register access (e.g. a base address) |
| 80 | * @iosize: I/O size used to access the registers (b/w/l) |
| 81 | * @regshift: register gap shift |
| 82 | * @osc_base: cascaded oscillator speed in ns |
| 83 | * @divisor: divisor for single counter |
| 84 | * @divisor1: divisor loaded into first cascaded counter |
| 85 | * @divisor2: divisor loaded into second cascaded counter |
| 86 | * #next_div: next divisor for single counter |
| 87 | * @next_div1: next divisor to use for first cascaded counter |
| 88 | * @next_div2: next divisor to use for second cascaded counter |
| 89 | * @clock_src; current clock source for each counter (driver specific) |
| 90 | * @gate_src; current gate source for each counter (driver specific) |
| 91 | * @busy: flags used to indicate that a counter is "busy" |
| 92 | * @insn_config: driver specific (*insn_config) callback |
| 93 | */ |
| 94 | struct comedi_8254 { |
| 95 | comedi_8254_iocb_fn *iocb; |
| 96 | unsigned long context; |
| 97 | unsigned int iosize; |
| 98 | unsigned int regshift; |
| 99 | unsigned int osc_base; |
| 100 | unsigned int divisor; |
| 101 | unsigned int divisor1; |
| 102 | unsigned int divisor2; |
| 103 | unsigned int next_div; |
| 104 | unsigned int next_div1; |
| 105 | unsigned int next_div2; |
| 106 | unsigned int clock_src[3]; |
| 107 | unsigned int gate_src[3]; |
| 108 | bool busy[3]; |
| 109 | |
| 110 | int (*insn_config)(struct comedi_device *dev, |
| 111 | struct comedi_subdevice *s, |
| 112 | struct comedi_insn *insn, unsigned int *data); |
| 113 | }; |
| 114 | |
| 115 | unsigned int comedi_8254_status(struct comedi_8254 *i8254, |
| 116 | unsigned int counter); |
| 117 | unsigned int comedi_8254_read(struct comedi_8254 *i8254, unsigned int counter); |
| 118 | void comedi_8254_write(struct comedi_8254 *i8254, |
| 119 | unsigned int counter, unsigned int val); |
| 120 | |
| 121 | int comedi_8254_set_mode(struct comedi_8254 *i8254, |
| 122 | unsigned int counter, unsigned int mode); |
| 123 | int comedi_8254_load(struct comedi_8254 *i8254, |
| 124 | unsigned int counter, unsigned int val, unsigned int mode); |
| 125 | |
| 126 | void comedi_8254_pacer_enable(struct comedi_8254 *i8254, |
| 127 | unsigned int counter1, unsigned int counter2, |
| 128 | bool enable); |
| 129 | void comedi_8254_update_divisors(struct comedi_8254 *i8254); |
| 130 | void comedi_8254_cascade_ns_to_timer(struct comedi_8254 *i8254, |
| 131 | unsigned int *nanosec, unsigned int flags); |
| 132 | void comedi_8254_ns_to_timer(struct comedi_8254 *i8254, |
| 133 | unsigned int *nanosec, unsigned int flags); |
| 134 | |
| 135 | void comedi_8254_set_busy(struct comedi_8254 *i8254, |
| 136 | unsigned int counter, bool busy); |
| 137 | |
| 138 | void comedi_8254_subdevice_init(struct comedi_subdevice *s, |
| 139 | struct comedi_8254 *i8254); |
| 140 | |
| 141 | #ifdef CONFIG_HAS_IOPORT |
| 142 | struct comedi_8254 *comedi_8254_io_alloc(unsigned long iobase, |
| 143 | unsigned int osc_base, |
| 144 | unsigned int iosize, |
| 145 | unsigned int regshift); |
| 146 | #else |
| 147 | static inline struct comedi_8254 *comedi_8254_io_alloc(unsigned long iobase, |
| 148 | unsigned int osc_base, |
| 149 | unsigned int iosize, |
| 150 | unsigned int regshift) |
| 151 | { |
| 152 | return ERR_PTR(-ENXIO); |
| 153 | } |
| 154 | #endif |
| 155 | |
| 156 | struct comedi_8254 *comedi_8254_mm_alloc(void __iomem *mmio, |
| 157 | unsigned int osc_base, |
| 158 | unsigned int iosize, |
| 159 | unsigned int regshift); |
| 160 | |
| 161 | #endif /* _COMEDI_8254_H */ |
| 162 | |