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_STATUS_REG(x) (NITIO_G01_STATUS + ((x) / 2))
34 #define NITIO_RESET_REG(x) (NITIO_G01_RESET + ((x) / 2))
36 static inline enum ni_gpct_register NITIO_Gxx_Joint_Status1_Reg(unsigned idx)
41 return NITIO_G01_STATUS1;
44 return NITIO_G23_STATUS1;
49 static inline enum ni_gpct_register NITIO_Gxx_Joint_Status2_Reg(unsigned idx)
54 return NITIO_G01_STATUS2;
57 return NITIO_G23_STATUS2;
62 static inline enum ni_gpct_register NITIO_Gi_DMA_Config_Reg(unsigned idx)
66 return NITIO_G0_DMA_CFG;
68 return NITIO_G1_DMA_CFG;
70 return NITIO_G2_DMA_CFG;
72 return NITIO_G3_DMA_CFG;
77 static inline enum ni_gpct_register NITIO_Gi_DMA_Status_Reg(unsigned idx)
81 return NITIO_G0_DMA_STATUS;
83 return NITIO_G1_DMA_STATUS;
85 return NITIO_G2_DMA_STATUS;
87 return NITIO_G3_DMA_STATUS;
92 static inline enum ni_gpct_register NITIO_Gi_ABZ_Reg(unsigned idx)
103 static inline enum ni_gpct_register NITIO_Gi_Interrupt_Acknowledge_Reg(unsigned idx)
107 return NITIO_G0_INT_ACK;
109 return NITIO_G1_INT_ACK;
111 return NITIO_G2_INT_ACK;
113 return NITIO_G3_INT_ACK;
118 static inline enum ni_gpct_register NITIO_Gi_Status_Reg(unsigned idx)
122 return NITIO_G0_STATUS;
124 return NITIO_G1_STATUS;
126 return NITIO_G2_STATUS;
128 return NITIO_G3_STATUS;
133 static inline enum ni_gpct_register NITIO_Gi_Interrupt_Enable_Reg(unsigned idx)
137 return NITIO_G0_INT_ENA;
139 return NITIO_G1_INT_ENA;
141 return NITIO_G2_INT_ENA;
143 return NITIO_G3_INT_ENA;
148 enum Gi_Auto_Increment_Reg_Bits {
149 Gi_Auto_Increment_Mask = 0xff
152 #define Gi_Up_Down_Shift 5
153 enum Gi_Command_Reg_Bits {
155 Gi_Save_Trace_Bit = 0x2,
157 Gi_Disarm_Bit = 0x10,
158 Gi_Up_Down_Mask = 0x3 << Gi_Up_Down_Shift,
159 Gi_Always_Down_Bits = 0x0 << Gi_Up_Down_Shift,
160 Gi_Always_Up_Bits = 0x1 << Gi_Up_Down_Shift,
161 Gi_Up_Down_Hardware_IO_Bits = 0x2 << Gi_Up_Down_Shift,
162 Gi_Up_Down_Hardware_Gate_Bits = 0x3 << Gi_Up_Down_Shift,
163 Gi_Write_Switch_Bit = 0x80,
164 Gi_Synchronize_Gate_Bit = 0x100,
165 Gi_Little_Big_Endian_Bit = 0x200,
166 Gi_Bank_Switch_Start_Bit = 0x400,
167 Gi_Bank_Switch_Mode_Bit = 0x800,
168 Gi_Bank_Switch_Enable_Bit = 0x1000,
169 Gi_Arm_Copy_Bit = 0x2000,
170 Gi_Save_Trace_Copy_Bit = 0x4000,
171 Gi_Disarm_Copy_Bit = 0x8000
174 #define Gi_Index_Phase_Bitshift 5
175 #define Gi_HW_Arm_Select_Shift 8
176 enum Gi_Counting_Mode_Reg_Bits {
177 Gi_Counting_Mode_Mask = 0x7,
178 Gi_Counting_Mode_Normal_Bits = 0x0,
179 Gi_Counting_Mode_QuadratureX1_Bits = 0x1,
180 Gi_Counting_Mode_QuadratureX2_Bits = 0x2,
181 Gi_Counting_Mode_QuadratureX4_Bits = 0x3,
182 Gi_Counting_Mode_Two_Pulse_Bits = 0x4,
183 Gi_Counting_Mode_Sync_Source_Bits = 0x6,
184 Gi_Index_Mode_Bit = 0x10,
185 Gi_Index_Phase_Mask = 0x3 << Gi_Index_Phase_Bitshift,
186 Gi_Index_Phase_LowA_LowB = 0x0 << Gi_Index_Phase_Bitshift,
187 Gi_Index_Phase_LowA_HighB = 0x1 << Gi_Index_Phase_Bitshift,
188 Gi_Index_Phase_HighA_LowB = 0x2 << Gi_Index_Phase_Bitshift,
189 Gi_Index_Phase_HighA_HighB = 0x3 << Gi_Index_Phase_Bitshift,
190 /* from m-series example code, not documented in 660x register level
192 Gi_HW_Arm_Enable_Bit = 0x80,
193 /* from m-series example code, not documented in 660x register level
195 Gi_660x_HW_Arm_Select_Mask = 0x7 << Gi_HW_Arm_Select_Shift,
196 Gi_660x_Prescale_X8_Bit = 0x1000,
197 Gi_M_Series_Prescale_X8_Bit = 0x2000,
198 Gi_M_Series_HW_Arm_Select_Mask = 0x1f << Gi_HW_Arm_Select_Shift,
199 /* must be set for clocks over 40MHz, which includes synchronous
200 * counting and quadrature modes */
201 Gi_660x_Alternate_Sync_Bit = 0x2000,
202 Gi_M_Series_Alternate_Sync_Bit = 0x4000,
203 /* from m-series example code, not documented in 660x register level
205 Gi_660x_Prescale_X2_Bit = 0x4000,
206 Gi_M_Series_Prescale_X2_Bit = 0x8000,
209 #define Gi_Source_Select_Shift 2
210 #define Gi_Gate_Select_Shift 7
211 enum Gi_Input_Select_Bits {
212 Gi_Read_Acknowledges_Irq = 0x1, /* not present on 660x */
213 Gi_Write_Acknowledges_Irq = 0x2, /* not present on 660x */
214 Gi_Source_Select_Mask = 0x7c,
215 Gi_Gate_Select_Mask = 0x1f << Gi_Gate_Select_Shift,
216 Gi_Gate_Select_Load_Source_Bit = 0x1000,
217 Gi_Or_Gate_Bit = 0x2000,
218 Gi_Output_Polarity_Bit = 0x4000, /* set to invert */
219 Gi_Source_Polarity_Bit = 0x8000 /* set to invert */
223 Gi_Gating_Mode_Mask = 0x3,
224 Gi_Gating_Disabled_Bits = 0x0,
225 Gi_Level_Gating_Bits = 0x1,
226 Gi_Rising_Edge_Gating_Bits = 0x2,
227 Gi_Falling_Edge_Gating_Bits = 0x3,
228 Gi_Gate_On_Both_Edges_Bit = 0x4, /* used in conjunction with
229 * rising edge gating mode */
230 Gi_Trigger_Mode_for_Edge_Gate_Mask = 0x18,
231 Gi_Edge_Gate_Starts_Stops_Bits = 0x0,
232 Gi_Edge_Gate_Stops_Starts_Bits = 0x8,
233 Gi_Edge_Gate_Starts_Bits = 0x10,
234 Gi_Edge_Gate_No_Starts_or_Stops_Bits = 0x18,
235 Gi_Stop_Mode_Mask = 0x60,
236 Gi_Stop_on_Gate_Bits = 0x00,
237 Gi_Stop_on_Gate_or_TC_Bits = 0x20,
238 Gi_Stop_on_Gate_or_Second_TC_Bits = 0x40,
239 Gi_Load_Source_Select_Bit = 0x80,
240 Gi_Output_Mode_Mask = 0x300,
241 Gi_Output_TC_Pulse_Bits = 0x100,
242 Gi_Output_TC_Toggle_Bits = 0x200,
243 Gi_Output_TC_or_Gate_Toggle_Bits = 0x300,
244 Gi_Counting_Once_Mask = 0xc00,
245 Gi_No_Hardware_Disarm_Bits = 0x000,
246 Gi_Disarm_at_TC_Bits = 0x400,
247 Gi_Disarm_at_Gate_Bits = 0x800,
248 Gi_Disarm_at_TC_or_Gate_Bits = 0xc00,
249 Gi_Loading_On_TC_Bit = 0x1000,
250 Gi_Gate_Polarity_Bit = 0x2000,
251 Gi_Loading_On_Gate_Bit = 0x4000,
252 Gi_Reload_Source_Switching_Bit = 0x8000
255 #define Gi_Second_Gate_Select_Shift 7
256 /*FIXME: m-series has a second gate subselect bit */
257 /*FIXME: m-series second gate sources are undocumented (by NI)*/
258 enum Gi_Second_Gate_Bits {
259 Gi_Second_Gate_Mode_Bit = 0x1,
260 Gi_Second_Gate_Select_Mask = 0x1f << Gi_Second_Gate_Select_Shift,
261 Gi_Second_Gate_Polarity_Bit = 0x2000,
262 Gi_Second_Gate_Subselect_Bit = 0x4000, /* m-series only */
263 Gi_Source_Subselect_Bit = 0x8000 /* m-series only */
265 static inline unsigned Gi_Second_Gate_Select_Bits(unsigned second_gate_select)
267 return (second_gate_select << Gi_Second_Gate_Select_Shift) &
268 Gi_Second_Gate_Select_Mask;
271 enum Gxx_Status_Bits {
274 G0_Counting_Bit = 0x4,
275 G1_Counting_Bit = 0x8,
276 G0_Next_Load_Source_Bit = 0x10,
277 G1_Next_Load_Source_Bit = 0x20,
278 G0_Stale_Data_Bit = 0x40,
279 G1_Stale_Data_Bit = 0x80,
280 G0_Armed_Bit = 0x100,
281 G1_Armed_Bit = 0x200,
282 G0_No_Load_Between_Gates_Bit = 0x400,
283 G1_No_Load_Between_Gates_Bit = 0x800,
284 G0_TC_Error_Bit = 0x1000,
285 G1_TC_Error_Bit = 0x2000,
286 G0_Gate_Error_Bit = 0x4000,
287 G1_Gate_Error_Bit = 0x8000
289 static inline enum Gxx_Status_Bits Gi_Counting_Bit(unsigned counter_index)
291 if (counter_index % 2)
292 return G1_Counting_Bit;
293 return G0_Counting_Bit;
296 static inline enum Gxx_Status_Bits Gi_Armed_Bit(unsigned counter_index)
298 if (counter_index % 2)
303 static inline enum Gxx_Status_Bits Gi_Next_Load_Source_Bit(unsigned
306 if (counter_index % 2)
307 return G1_Next_Load_Source_Bit;
308 return G0_Next_Load_Source_Bit;
311 static inline enum Gxx_Status_Bits Gi_Stale_Data_Bit(unsigned counter_index)
313 if (counter_index % 2)
314 return G1_Stale_Data_Bit;
315 return G0_Stale_Data_Bit;
318 static inline enum Gxx_Status_Bits Gi_TC_Error_Bit(unsigned counter_index)
320 if (counter_index % 2)
321 return G1_TC_Error_Bit;
322 return G0_TC_Error_Bit;
325 static inline enum Gxx_Status_Bits Gi_Gate_Error_Bit(unsigned counter_index)
327 if (counter_index % 2)
328 return G1_Gate_Error_Bit;
329 return G0_Gate_Error_Bit;
332 /* joint reset register bits */
333 static inline unsigned Gi_Reset_Bit(unsigned counter_index)
335 return 0x1 << (2 + (counter_index % 2));
338 enum Gxx_Joint_Status2_Bits {
341 G0_HW_Save_Bit = 0x1000,
342 G1_HW_Save_Bit = 0x2000,
343 G0_Permanent_Stale_Bit = 0x4000,
344 G1_Permanent_Stale_Bit = 0x8000
346 static inline enum Gxx_Joint_Status2_Bits Gi_Permanent_Stale_Bit(unsigned
349 if (counter_index % 2)
350 return G1_Permanent_Stale_Bit;
351 return G0_Permanent_Stale_Bit;
354 enum Gi_DMA_Config_Reg_Bits {
355 Gi_DMA_Enable_Bit = 0x1,
356 Gi_DMA_Write_Bit = 0x2,
360 enum Gi_DMA_Status_Reg_Bits {
361 Gi_DMA_Readbank_Bit = 0x2000,
362 Gi_DRQ_Error_Bit = 0x4000,
363 Gi_DRQ_Status_Bit = 0x8000
366 enum G02_Interrupt_Acknowledge_Bits {
367 G0_Gate_Error_Confirm_Bit = 0x20,
368 G0_TC_Error_Confirm_Bit = 0x40
370 enum G13_Interrupt_Acknowledge_Bits {
371 G1_Gate_Error_Confirm_Bit = 0x2,
372 G1_TC_Error_Confirm_Bit = 0x4
374 static inline unsigned Gi_Gate_Error_Confirm_Bit(unsigned counter_index)
376 if (counter_index % 2)
377 return G1_Gate_Error_Confirm_Bit;
378 return G0_Gate_Error_Confirm_Bit;
381 static inline unsigned Gi_TC_Error_Confirm_Bit(unsigned counter_index)
383 if (counter_index % 2)
384 return G1_TC_Error_Confirm_Bit;
385 return G0_TC_Error_Confirm_Bit;
388 /* bits that are the same in G0/G2 and G1/G3 interrupt acknowledge registers */
389 enum Gxx_Interrupt_Acknowledge_Bits {
390 Gi_TC_Interrupt_Ack_Bit = 0x4000,
391 Gi_Gate_Interrupt_Ack_Bit = 0x8000
394 enum Gi_Status_Bits {
395 Gi_Gate_Interrupt_Bit = 0x4,
397 Gi_Interrupt_Bit = 0x8000
400 enum G02_Interrupt_Enable_Bits {
401 G0_TC_Interrupt_Enable_Bit = 0x40,
402 G0_Gate_Interrupt_Enable_Bit = 0x100
404 enum G13_Interrupt_Enable_Bits {
405 G1_TC_Interrupt_Enable_Bit = 0x200,
406 G1_Gate_Interrupt_Enable_Bit = 0x400
408 static inline unsigned Gi_Gate_Interrupt_Enable_Bit(unsigned counter_index)
412 if (counter_index % 2)
413 bit = G1_Gate_Interrupt_Enable_Bit;
415 bit = G0_Gate_Interrupt_Enable_Bit;
419 static inline void write_register(struct ni_gpct *counter, unsigned bits,
420 enum ni_gpct_register reg)
422 BUG_ON(reg >= NITIO_NUM_REGS);
423 counter->counter_dev->write_register(counter, bits, reg);
426 static inline unsigned read_register(struct ni_gpct *counter,
427 enum ni_gpct_register reg)
429 BUG_ON(reg >= NITIO_NUM_REGS);
430 return counter->counter_dev->read_register(counter, reg);
433 static inline int ni_tio_counting_mode_registers_present(const struct
437 switch (counter_dev->variant) {
438 case ni_gpct_variant_e_series:
441 case ni_gpct_variant_m_series:
442 case ni_gpct_variant_660x:
452 static inline void ni_tio_set_bits_transient(struct ni_gpct *counter,
453 enum ni_gpct_register
454 register_index, unsigned bit_mask,
456 unsigned transient_bit_values)
458 struct ni_gpct_device *counter_dev = counter->counter_dev;
461 BUG_ON(register_index >= NITIO_NUM_REGS);
462 spin_lock_irqsave(&counter_dev->regs_lock, flags);
463 counter_dev->regs[register_index] &= ~bit_mask;
464 counter_dev->regs[register_index] |= (bit_values & bit_mask);
465 write_register(counter,
466 counter_dev->regs[register_index] | transient_bit_values,
469 spin_unlock_irqrestore(&counter_dev->regs_lock, flags);
472 /* ni_tio_set_bits( ) is for safely writing to registers whose bits may be
473 * twiddled in interrupt context, or whose software copy may be read in
476 static inline void ni_tio_set_bits(struct ni_gpct *counter,
477 enum ni_gpct_register register_index,
478 unsigned bit_mask, unsigned bit_values)
480 ni_tio_set_bits_transient(counter, register_index, bit_mask, bit_values,
484 /* ni_tio_get_soft_copy( ) is for safely reading the software copy of a register
485 whose bits might be modified in interrupt context, or whose software copy
486 might need to be read in interrupt context.
488 static inline unsigned ni_tio_get_soft_copy(const struct ni_gpct *counter,
489 enum ni_gpct_register
492 struct ni_gpct_device *counter_dev = counter->counter_dev;
496 BUG_ON(register_index >= NITIO_NUM_REGS);
497 spin_lock_irqsave(&counter_dev->regs_lock, flags);
498 value = counter_dev->regs[register_index];
499 spin_unlock_irqrestore(&counter_dev->regs_lock, flags);
503 int ni_tio_arm(struct ni_gpct *counter, int arm, unsigned start_trigger);
504 int ni_tio_set_gate_src(struct ni_gpct *counter, unsigned gate_index,
505 unsigned int gate_source);
507 #endif /* _COMEDI_NI_TIO_INTERNAL_H */