2 drivers/ni_tio_internal.h
3 Header file for NI general purpose counter support code (ni_tio.c and
6 COMEDI - Linux Control and Measurement Device Interface
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.
19 #ifndef _COMEDI_NI_TIO_INTERNAL_H
20 #define _COMEDI_NI_TIO_INTERNAL_H
24 #define NITIO_AUTO_INC_REG(x) (NITIO_G0_AUTO_INC + (x))
25 #define NITIO_CMD_REG(x) (NITIO_G0_CMD + (x))
26 #define NITIO_SW_SAVE_REG(x) (NITIO_G0_SW_SAVE + (x))
27 #define NITIO_MODE_REG(x) (NITIO_G0_MODE + (x))
28 #define NITIO_LOADA_REG(x) (NITIO_G0_LOADA + (x))
29 #define NITIO_LOADB_REG(x) (NITIO_G0_LOADB + (x))
30 #define NITIO_INPUT_SEL_REG(x) (NITIO_G0_INPUT_SEL + (x))
31 #define NITIO_CNT_MODE_REG(x) (NITIO_G0_CNT_MODE + (x))
32 #define NITIO_GATE2_REG(x) (NITIO_G0_GATE2 + (x))
33 #define NITIO_SHARED_STATUS_REG(x) (NITIO_G01_STATUS + ((x) / 2))
34 #define NITIO_RESET_REG(x) (NITIO_G01_RESET + ((x) / 2))
35 #define NITIO_STATUS1_REG(x) (NITIO_G01_STATUS1 + ((x) / 2))
36 #define NITIO_STATUS2_REG(x) (NITIO_G01_STATUS2 + ((x) / 2))
37 #define NITIO_DMA_CFG_REG(x) (NITIO_G0_DMA_CFG + (x))
38 #define NITIO_DMA_STATUS_REG(x) (NITIO_G0_DMA_STATUS + (x))
39 #define NITIO_ABZ_REG(x) (NITIO_G0_ABZ + (x))
40 #define NITIO_INT_ACK_REG(x) (NITIO_G0_INT_ACK + (x))
41 #define NITIO_STATUS_REG(x) (NITIO_G0_STATUS + (x))
43 static inline enum ni_gpct_register NITIO_Gi_Interrupt_Enable_Reg(unsigned idx)
47 return NITIO_G0_INT_ENA;
49 return NITIO_G1_INT_ENA;
51 return NITIO_G2_INT_ENA;
53 return NITIO_G3_INT_ENA;
58 enum Gi_Auto_Increment_Reg_Bits {
59 Gi_Auto_Increment_Mask = 0xff
62 #define Gi_Up_Down_Shift 5
63 enum Gi_Command_Reg_Bits {
65 Gi_Save_Trace_Bit = 0x2,
68 Gi_Up_Down_Mask = 0x3 << Gi_Up_Down_Shift,
69 Gi_Always_Down_Bits = 0x0 << Gi_Up_Down_Shift,
70 Gi_Always_Up_Bits = 0x1 << Gi_Up_Down_Shift,
71 Gi_Up_Down_Hardware_IO_Bits = 0x2 << Gi_Up_Down_Shift,
72 Gi_Up_Down_Hardware_Gate_Bits = 0x3 << Gi_Up_Down_Shift,
73 Gi_Write_Switch_Bit = 0x80,
74 Gi_Synchronize_Gate_Bit = 0x100,
75 Gi_Little_Big_Endian_Bit = 0x200,
76 Gi_Bank_Switch_Start_Bit = 0x400,
77 Gi_Bank_Switch_Mode_Bit = 0x800,
78 Gi_Bank_Switch_Enable_Bit = 0x1000,
79 Gi_Arm_Copy_Bit = 0x2000,
80 Gi_Save_Trace_Copy_Bit = 0x4000,
81 Gi_Disarm_Copy_Bit = 0x8000
84 #define Gi_Index_Phase_Bitshift 5
85 #define Gi_HW_Arm_Select_Shift 8
86 enum Gi_Counting_Mode_Reg_Bits {
87 Gi_Counting_Mode_Mask = 0x7,
88 Gi_Counting_Mode_Normal_Bits = 0x0,
89 Gi_Counting_Mode_QuadratureX1_Bits = 0x1,
90 Gi_Counting_Mode_QuadratureX2_Bits = 0x2,
91 Gi_Counting_Mode_QuadratureX4_Bits = 0x3,
92 Gi_Counting_Mode_Two_Pulse_Bits = 0x4,
93 Gi_Counting_Mode_Sync_Source_Bits = 0x6,
94 Gi_Index_Mode_Bit = 0x10,
95 Gi_Index_Phase_Mask = 0x3 << Gi_Index_Phase_Bitshift,
96 Gi_Index_Phase_LowA_LowB = 0x0 << Gi_Index_Phase_Bitshift,
97 Gi_Index_Phase_LowA_HighB = 0x1 << Gi_Index_Phase_Bitshift,
98 Gi_Index_Phase_HighA_LowB = 0x2 << Gi_Index_Phase_Bitshift,
99 Gi_Index_Phase_HighA_HighB = 0x3 << Gi_Index_Phase_Bitshift,
100 /* from m-series example code, not documented in 660x register level
102 Gi_HW_Arm_Enable_Bit = 0x80,
103 /* from m-series example code, not documented in 660x register level
105 Gi_660x_HW_Arm_Select_Mask = 0x7 << Gi_HW_Arm_Select_Shift,
106 Gi_660x_Prescale_X8_Bit = 0x1000,
107 Gi_M_Series_Prescale_X8_Bit = 0x2000,
108 Gi_M_Series_HW_Arm_Select_Mask = 0x1f << Gi_HW_Arm_Select_Shift,
109 /* must be set for clocks over 40MHz, which includes synchronous
110 * counting and quadrature modes */
111 Gi_660x_Alternate_Sync_Bit = 0x2000,
112 Gi_M_Series_Alternate_Sync_Bit = 0x4000,
113 /* from m-series example code, not documented in 660x register level
115 Gi_660x_Prescale_X2_Bit = 0x4000,
116 Gi_M_Series_Prescale_X2_Bit = 0x8000,
119 #define Gi_Source_Select_Shift 2
120 #define Gi_Gate_Select_Shift 7
121 enum Gi_Input_Select_Bits {
122 Gi_Read_Acknowledges_Irq = 0x1, /* not present on 660x */
123 Gi_Write_Acknowledges_Irq = 0x2, /* not present on 660x */
124 Gi_Source_Select_Mask = 0x7c,
125 Gi_Gate_Select_Mask = 0x1f << Gi_Gate_Select_Shift,
126 Gi_Gate_Select_Load_Source_Bit = 0x1000,
127 Gi_Or_Gate_Bit = 0x2000,
128 Gi_Output_Polarity_Bit = 0x4000, /* set to invert */
129 Gi_Source_Polarity_Bit = 0x8000 /* set to invert */
133 Gi_Gating_Mode_Mask = 0x3,
134 Gi_Gating_Disabled_Bits = 0x0,
135 Gi_Level_Gating_Bits = 0x1,
136 Gi_Rising_Edge_Gating_Bits = 0x2,
137 Gi_Falling_Edge_Gating_Bits = 0x3,
138 Gi_Gate_On_Both_Edges_Bit = 0x4, /* used in conjunction with
139 * rising edge gating mode */
140 Gi_Trigger_Mode_for_Edge_Gate_Mask = 0x18,
141 Gi_Edge_Gate_Starts_Stops_Bits = 0x0,
142 Gi_Edge_Gate_Stops_Starts_Bits = 0x8,
143 Gi_Edge_Gate_Starts_Bits = 0x10,
144 Gi_Edge_Gate_No_Starts_or_Stops_Bits = 0x18,
145 Gi_Stop_Mode_Mask = 0x60,
146 Gi_Stop_on_Gate_Bits = 0x00,
147 Gi_Stop_on_Gate_or_TC_Bits = 0x20,
148 Gi_Stop_on_Gate_or_Second_TC_Bits = 0x40,
149 Gi_Load_Source_Select_Bit = 0x80,
150 Gi_Output_Mode_Mask = 0x300,
151 Gi_Output_TC_Pulse_Bits = 0x100,
152 Gi_Output_TC_Toggle_Bits = 0x200,
153 Gi_Output_TC_or_Gate_Toggle_Bits = 0x300,
154 Gi_Counting_Once_Mask = 0xc00,
155 Gi_No_Hardware_Disarm_Bits = 0x000,
156 Gi_Disarm_at_TC_Bits = 0x400,
157 Gi_Disarm_at_Gate_Bits = 0x800,
158 Gi_Disarm_at_TC_or_Gate_Bits = 0xc00,
159 Gi_Loading_On_TC_Bit = 0x1000,
160 Gi_Gate_Polarity_Bit = 0x2000,
161 Gi_Loading_On_Gate_Bit = 0x4000,
162 Gi_Reload_Source_Switching_Bit = 0x8000
165 #define Gi_Second_Gate_Select_Shift 7
166 /*FIXME: m-series has a second gate subselect bit */
167 /*FIXME: m-series second gate sources are undocumented (by NI)*/
168 enum Gi_Second_Gate_Bits {
169 Gi_Second_Gate_Mode_Bit = 0x1,
170 Gi_Second_Gate_Select_Mask = 0x1f << Gi_Second_Gate_Select_Shift,
171 Gi_Second_Gate_Polarity_Bit = 0x2000,
172 Gi_Second_Gate_Subselect_Bit = 0x4000, /* m-series only */
173 Gi_Source_Subselect_Bit = 0x8000 /* m-series only */
175 static inline unsigned Gi_Second_Gate_Select_Bits(unsigned second_gate_select)
177 return (second_gate_select << Gi_Second_Gate_Select_Shift) &
178 Gi_Second_Gate_Select_Mask;
181 enum Gxx_Status_Bits {
184 G0_Counting_Bit = 0x4,
185 G1_Counting_Bit = 0x8,
186 G0_Next_Load_Source_Bit = 0x10,
187 G1_Next_Load_Source_Bit = 0x20,
188 G0_Stale_Data_Bit = 0x40,
189 G1_Stale_Data_Bit = 0x80,
190 G0_Armed_Bit = 0x100,
191 G1_Armed_Bit = 0x200,
192 G0_No_Load_Between_Gates_Bit = 0x400,
193 G1_No_Load_Between_Gates_Bit = 0x800,
194 G0_TC_Error_Bit = 0x1000,
195 G1_TC_Error_Bit = 0x2000,
196 G0_Gate_Error_Bit = 0x4000,
197 G1_Gate_Error_Bit = 0x8000
199 static inline enum Gxx_Status_Bits Gi_Counting_Bit(unsigned counter_index)
201 if (counter_index % 2)
202 return G1_Counting_Bit;
203 return G0_Counting_Bit;
206 static inline enum Gxx_Status_Bits Gi_Armed_Bit(unsigned counter_index)
208 if (counter_index % 2)
213 static inline enum Gxx_Status_Bits Gi_Next_Load_Source_Bit(unsigned
216 if (counter_index % 2)
217 return G1_Next_Load_Source_Bit;
218 return G0_Next_Load_Source_Bit;
221 static inline enum Gxx_Status_Bits Gi_Stale_Data_Bit(unsigned counter_index)
223 if (counter_index % 2)
224 return G1_Stale_Data_Bit;
225 return G0_Stale_Data_Bit;
228 static inline enum Gxx_Status_Bits Gi_TC_Error_Bit(unsigned counter_index)
230 if (counter_index % 2)
231 return G1_TC_Error_Bit;
232 return G0_TC_Error_Bit;
235 static inline enum Gxx_Status_Bits Gi_Gate_Error_Bit(unsigned counter_index)
237 if (counter_index % 2)
238 return G1_Gate_Error_Bit;
239 return G0_Gate_Error_Bit;
242 /* joint reset register bits */
243 static inline unsigned Gi_Reset_Bit(unsigned counter_index)
245 return 0x1 << (2 + (counter_index % 2));
248 enum Gxx_Joint_Status2_Bits {
251 G0_HW_Save_Bit = 0x1000,
252 G1_HW_Save_Bit = 0x2000,
253 G0_Permanent_Stale_Bit = 0x4000,
254 G1_Permanent_Stale_Bit = 0x8000
256 static inline enum Gxx_Joint_Status2_Bits Gi_Permanent_Stale_Bit(unsigned
259 if (counter_index % 2)
260 return G1_Permanent_Stale_Bit;
261 return G0_Permanent_Stale_Bit;
264 enum Gi_DMA_Config_Reg_Bits {
265 Gi_DMA_Enable_Bit = 0x1,
266 Gi_DMA_Write_Bit = 0x2,
270 enum Gi_DMA_Status_Reg_Bits {
271 Gi_DMA_Readbank_Bit = 0x2000,
272 Gi_DRQ_Error_Bit = 0x4000,
273 Gi_DRQ_Status_Bit = 0x8000
276 enum G02_Interrupt_Acknowledge_Bits {
277 G0_Gate_Error_Confirm_Bit = 0x20,
278 G0_TC_Error_Confirm_Bit = 0x40
280 enum G13_Interrupt_Acknowledge_Bits {
281 G1_Gate_Error_Confirm_Bit = 0x2,
282 G1_TC_Error_Confirm_Bit = 0x4
284 static inline unsigned Gi_Gate_Error_Confirm_Bit(unsigned counter_index)
286 if (counter_index % 2)
287 return G1_Gate_Error_Confirm_Bit;
288 return G0_Gate_Error_Confirm_Bit;
291 static inline unsigned Gi_TC_Error_Confirm_Bit(unsigned counter_index)
293 if (counter_index % 2)
294 return G1_TC_Error_Confirm_Bit;
295 return G0_TC_Error_Confirm_Bit;
298 /* bits that are the same in G0/G2 and G1/G3 interrupt acknowledge registers */
299 enum Gxx_Interrupt_Acknowledge_Bits {
300 Gi_TC_Interrupt_Ack_Bit = 0x4000,
301 Gi_Gate_Interrupt_Ack_Bit = 0x8000
304 enum Gi_Status_Bits {
305 Gi_Gate_Interrupt_Bit = 0x4,
307 Gi_Interrupt_Bit = 0x8000
310 enum G02_Interrupt_Enable_Bits {
311 G0_TC_Interrupt_Enable_Bit = 0x40,
312 G0_Gate_Interrupt_Enable_Bit = 0x100
314 enum G13_Interrupt_Enable_Bits {
315 G1_TC_Interrupt_Enable_Bit = 0x200,
316 G1_Gate_Interrupt_Enable_Bit = 0x400
318 static inline unsigned Gi_Gate_Interrupt_Enable_Bit(unsigned counter_index)
322 if (counter_index % 2)
323 bit = G1_Gate_Interrupt_Enable_Bit;
325 bit = G0_Gate_Interrupt_Enable_Bit;
329 static inline void write_register(struct ni_gpct *counter, unsigned bits,
330 enum ni_gpct_register reg)
332 BUG_ON(reg >= NITIO_NUM_REGS);
333 counter->counter_dev->write_register(counter, bits, reg);
336 static inline unsigned read_register(struct ni_gpct *counter,
337 enum ni_gpct_register reg)
339 BUG_ON(reg >= NITIO_NUM_REGS);
340 return counter->counter_dev->read_register(counter, reg);
343 static inline int ni_tio_counting_mode_registers_present(const struct
347 switch (counter_dev->variant) {
348 case ni_gpct_variant_e_series:
351 case ni_gpct_variant_m_series:
352 case ni_gpct_variant_660x:
362 static inline void ni_tio_set_bits_transient(struct ni_gpct *counter,
363 enum ni_gpct_register
364 register_index, unsigned bit_mask,
366 unsigned transient_bit_values)
368 struct ni_gpct_device *counter_dev = counter->counter_dev;
371 BUG_ON(register_index >= NITIO_NUM_REGS);
372 spin_lock_irqsave(&counter_dev->regs_lock, flags);
373 counter_dev->regs[register_index] &= ~bit_mask;
374 counter_dev->regs[register_index] |= (bit_values & bit_mask);
375 write_register(counter,
376 counter_dev->regs[register_index] | transient_bit_values,
379 spin_unlock_irqrestore(&counter_dev->regs_lock, flags);
382 /* ni_tio_set_bits( ) is for safely writing to registers whose bits may be
383 * twiddled in interrupt context, or whose software copy may be read in
386 static inline void ni_tio_set_bits(struct ni_gpct *counter,
387 enum ni_gpct_register register_index,
388 unsigned bit_mask, unsigned bit_values)
390 ni_tio_set_bits_transient(counter, register_index, bit_mask, bit_values,
394 /* ni_tio_get_soft_copy( ) is for safely reading the software copy of a register
395 whose bits might be modified in interrupt context, or whose software copy
396 might need to be read in interrupt context.
398 static inline unsigned ni_tio_get_soft_copy(const struct ni_gpct *counter,
399 enum ni_gpct_register
402 struct ni_gpct_device *counter_dev = counter->counter_dev;
406 BUG_ON(register_index >= NITIO_NUM_REGS);
407 spin_lock_irqsave(&counter_dev->regs_lock, flags);
408 value = counter_dev->regs[register_index];
409 spin_unlock_irqrestore(&counter_dev->regs_lock, flags);
413 int ni_tio_arm(struct ni_gpct *counter, int arm, unsigned start_trigger);
414 int ni_tio_set_gate_src(struct ni_gpct *counter, unsigned gate_index,
415 unsigned int gate_source);
417 #endif /* _COMEDI_NI_TIO_INTERNAL_H */