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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #ifndef _COMEDI_NI_TIO_INTERNAL_H
25 #define _COMEDI_NI_TIO_INTERNAL_H
29 static inline enum ni_gpct_register NITIO_Gi_Autoincrement_Reg(unsigned
32 switch (counter_index) {
34 return NITIO_G0_Autoincrement_Reg;
37 return NITIO_G1_Autoincrement_Reg;
40 return NITIO_G2_Autoincrement_Reg;
43 return NITIO_G3_Autoincrement_Reg;
52 static inline enum ni_gpct_register NITIO_Gi_Command_Reg(unsigned counter_index)
54 switch (counter_index) {
56 return NITIO_G0_Command_Reg;
59 return NITIO_G1_Command_Reg;
62 return NITIO_G2_Command_Reg;
65 return NITIO_G3_Command_Reg;
74 static inline enum ni_gpct_register NITIO_Gi_Counting_Mode_Reg(unsigned
77 switch (counter_index) {
79 return NITIO_G0_Counting_Mode_Reg;
82 return NITIO_G1_Counting_Mode_Reg;
85 return NITIO_G2_Counting_Mode_Reg;
88 return NITIO_G3_Counting_Mode_Reg;
97 static inline enum ni_gpct_register NITIO_Gi_Input_Select_Reg(unsigned
100 switch (counter_index) {
102 return NITIO_G0_Input_Select_Reg;
105 return NITIO_G1_Input_Select_Reg;
108 return NITIO_G2_Input_Select_Reg;
111 return NITIO_G3_Input_Select_Reg;
120 static inline enum ni_gpct_register NITIO_Gxx_Joint_Reset_Reg(unsigned
123 switch (counter_index) {
126 return NITIO_G01_Joint_Reset_Reg;
130 return NITIO_G23_Joint_Reset_Reg;
139 static inline enum ni_gpct_register NITIO_Gxx_Joint_Status1_Reg(unsigned
142 switch (counter_index) {
145 return NITIO_G01_Joint_Status1_Reg;
149 return NITIO_G23_Joint_Status1_Reg;
158 static inline enum ni_gpct_register NITIO_Gxx_Joint_Status2_Reg(unsigned
161 switch (counter_index) {
164 return NITIO_G01_Joint_Status2_Reg;
168 return NITIO_G23_Joint_Status2_Reg;
177 static inline enum ni_gpct_register NITIO_Gxx_Status_Reg(unsigned counter_index)
179 switch (counter_index) {
182 return NITIO_G01_Status_Reg;
186 return NITIO_G23_Status_Reg;
195 static inline enum ni_gpct_register NITIO_Gi_LoadA_Reg(unsigned counter_index)
197 switch (counter_index) {
199 return NITIO_G0_LoadA_Reg;
202 return NITIO_G1_LoadA_Reg;
205 return NITIO_G2_LoadA_Reg;
208 return NITIO_G3_LoadA_Reg;
217 static inline enum ni_gpct_register NITIO_Gi_LoadB_Reg(unsigned counter_index)
219 switch (counter_index) {
221 return NITIO_G0_LoadB_Reg;
224 return NITIO_G1_LoadB_Reg;
227 return NITIO_G2_LoadB_Reg;
230 return NITIO_G3_LoadB_Reg;
239 static inline enum ni_gpct_register NITIO_Gi_Mode_Reg(unsigned counter_index)
241 switch (counter_index) {
243 return NITIO_G0_Mode_Reg;
246 return NITIO_G1_Mode_Reg;
249 return NITIO_G2_Mode_Reg;
252 return NITIO_G3_Mode_Reg;
261 static inline enum ni_gpct_register NITIO_Gi_SW_Save_Reg(int counter_index)
263 switch (counter_index) {
265 return NITIO_G0_SW_Save_Reg;
268 return NITIO_G1_SW_Save_Reg;
271 return NITIO_G2_SW_Save_Reg;
274 return NITIO_G3_SW_Save_Reg;
283 static inline enum ni_gpct_register NITIO_Gi_Second_Gate_Reg(int counter_index)
285 switch (counter_index) {
287 return NITIO_G0_Second_Gate_Reg;
290 return NITIO_G1_Second_Gate_Reg;
293 return NITIO_G2_Second_Gate_Reg;
296 return NITIO_G3_Second_Gate_Reg;
305 static inline enum ni_gpct_register NITIO_Gi_DMA_Config_Reg(int counter_index)
307 switch (counter_index) {
309 return NITIO_G0_DMA_Config_Reg;
312 return NITIO_G1_DMA_Config_Reg;
315 return NITIO_G2_DMA_Config_Reg;
318 return NITIO_G3_DMA_Config_Reg;
327 static inline enum ni_gpct_register NITIO_Gi_DMA_Status_Reg(int counter_index)
329 switch (counter_index) {
331 return NITIO_G0_DMA_Status_Reg;
334 return NITIO_G1_DMA_Status_Reg;
337 return NITIO_G2_DMA_Status_Reg;
340 return NITIO_G3_DMA_Status_Reg;
349 static inline enum ni_gpct_register NITIO_Gi_ABZ_Reg(int counter_index)
351 switch (counter_index) {
353 return NITIO_G0_ABZ_Reg;
356 return NITIO_G1_ABZ_Reg;
365 static inline enum ni_gpct_register NITIO_Gi_Interrupt_Acknowledge_Reg(int
368 switch (counter_index) {
370 return NITIO_G0_Interrupt_Acknowledge_Reg;
373 return NITIO_G1_Interrupt_Acknowledge_Reg;
376 return NITIO_G2_Interrupt_Acknowledge_Reg;
379 return NITIO_G3_Interrupt_Acknowledge_Reg;
388 static inline enum ni_gpct_register NITIO_Gi_Status_Reg(int counter_index)
390 switch (counter_index) {
392 return NITIO_G0_Status_Reg;
395 return NITIO_G1_Status_Reg;
398 return NITIO_G2_Status_Reg;
401 return NITIO_G3_Status_Reg;
410 static inline enum ni_gpct_register NITIO_Gi_Interrupt_Enable_Reg(int
413 switch (counter_index) {
415 return NITIO_G0_Interrupt_Enable_Reg;
418 return NITIO_G1_Interrupt_Enable_Reg;
421 return NITIO_G2_Interrupt_Enable_Reg;
424 return NITIO_G3_Interrupt_Enable_Reg;
433 enum Gi_Auto_Increment_Reg_Bits {
434 Gi_Auto_Increment_Mask = 0xff
437 #define Gi_Up_Down_Shift 5
438 enum Gi_Command_Reg_Bits {
440 Gi_Save_Trace_Bit = 0x2,
442 Gi_Disarm_Bit = 0x10,
443 Gi_Up_Down_Mask = 0x3 << Gi_Up_Down_Shift,
444 Gi_Always_Down_Bits = 0x0 << Gi_Up_Down_Shift,
445 Gi_Always_Up_Bits = 0x1 << Gi_Up_Down_Shift,
446 Gi_Up_Down_Hardware_IO_Bits = 0x2 << Gi_Up_Down_Shift,
447 Gi_Up_Down_Hardware_Gate_Bits = 0x3 << Gi_Up_Down_Shift,
448 Gi_Write_Switch_Bit = 0x80,
449 Gi_Synchronize_Gate_Bit = 0x100,
450 Gi_Little_Big_Endian_Bit = 0x200,
451 Gi_Bank_Switch_Start_Bit = 0x400,
452 Gi_Bank_Switch_Mode_Bit = 0x800,
453 Gi_Bank_Switch_Enable_Bit = 0x1000,
454 Gi_Arm_Copy_Bit = 0x2000,
455 Gi_Save_Trace_Copy_Bit = 0x4000,
456 Gi_Disarm_Copy_Bit = 0x8000
459 #define Gi_Index_Phase_Bitshift 5
460 #define Gi_HW_Arm_Select_Shift 8
461 enum Gi_Counting_Mode_Reg_Bits {
462 Gi_Counting_Mode_Mask = 0x7,
463 Gi_Counting_Mode_Normal_Bits = 0x0,
464 Gi_Counting_Mode_QuadratureX1_Bits = 0x1,
465 Gi_Counting_Mode_QuadratureX2_Bits = 0x2,
466 Gi_Counting_Mode_QuadratureX4_Bits = 0x3,
467 Gi_Counting_Mode_Two_Pulse_Bits = 0x4,
468 Gi_Counting_Mode_Sync_Source_Bits = 0x6,
469 Gi_Index_Mode_Bit = 0x10,
470 Gi_Index_Phase_Mask = 0x3 << Gi_Index_Phase_Bitshift,
471 Gi_Index_Phase_LowA_LowB = 0x0 << Gi_Index_Phase_Bitshift,
472 Gi_Index_Phase_LowA_HighB = 0x1 << Gi_Index_Phase_Bitshift,
473 Gi_Index_Phase_HighA_LowB = 0x2 << Gi_Index_Phase_Bitshift,
474 Gi_Index_Phase_HighA_HighB = 0x3 << Gi_Index_Phase_Bitshift,
475 Gi_HW_Arm_Enable_Bit = 0x80, /* from m-series example code, not documented in 660x register level manual */
476 Gi_660x_HW_Arm_Select_Mask = 0x7 << Gi_HW_Arm_Select_Shift, /* from m-series example code, not documented in 660x register level manual */
477 Gi_660x_Prescale_X8_Bit = 0x1000,
478 Gi_M_Series_Prescale_X8_Bit = 0x2000,
479 Gi_M_Series_HW_Arm_Select_Mask = 0x1f << Gi_HW_Arm_Select_Shift,
480 /* must be set for clocks over 40MHz, which includes synchronous counting and quadrature modes */
481 Gi_660x_Alternate_Sync_Bit = 0x2000,
482 Gi_M_Series_Alternate_Sync_Bit = 0x4000,
483 Gi_660x_Prescale_X2_Bit = 0x4000, /* from m-series example code, not documented in 660x register level manual */
484 Gi_M_Series_Prescale_X2_Bit = 0x8000,
487 #define Gi_Source_Select_Shift 2
488 #define Gi_Gate_Select_Shift 7
489 enum Gi_Input_Select_Bits {
490 Gi_Read_Acknowledges_Irq = 0x1, // not present on 660x
491 Gi_Write_Acknowledges_Irq = 0x2, // not present on 660x
492 Gi_Source_Select_Mask = 0x7c,
493 Gi_Gate_Select_Mask = 0x1f << Gi_Gate_Select_Shift,
494 Gi_Gate_Select_Load_Source_Bit = 0x1000,
495 Gi_Or_Gate_Bit = 0x2000,
496 Gi_Output_Polarity_Bit = 0x4000, /* set to invert */
497 Gi_Source_Polarity_Bit = 0x8000 /* set to invert */
501 Gi_Gating_Mode_Mask = 0x3,
502 Gi_Gating_Disabled_Bits = 0x0,
503 Gi_Level_Gating_Bits = 0x1,
504 Gi_Rising_Edge_Gating_Bits = 0x2,
505 Gi_Falling_Edge_Gating_Bits = 0x3,
506 Gi_Gate_On_Both_Edges_Bit = 0x4, /* used in conjunction with rising edge gating mode */
507 Gi_Trigger_Mode_for_Edge_Gate_Mask = 0x18,
508 Gi_Edge_Gate_Starts_Stops_Bits = 0x0,
509 Gi_Edge_Gate_Stops_Starts_Bits = 0x8,
510 Gi_Edge_Gate_Starts_Bits = 0x10,
511 Gi_Edge_Gate_No_Starts_or_Stops_Bits = 0x18,
512 Gi_Stop_Mode_Mask = 0x60,
513 Gi_Stop_on_Gate_Bits = 0x00,
514 Gi_Stop_on_Gate_or_TC_Bits = 0x20,
515 Gi_Stop_on_Gate_or_Second_TC_Bits = 0x40,
516 Gi_Load_Source_Select_Bit = 0x80,
517 Gi_Output_Mode_Mask = 0x300,
518 Gi_Output_TC_Pulse_Bits = 0x100,
519 Gi_Output_TC_Toggle_Bits = 0x200,
520 Gi_Output_TC_or_Gate_Toggle_Bits = 0x300,
521 Gi_Counting_Once_Mask = 0xc00,
522 Gi_No_Hardware_Disarm_Bits = 0x000,
523 Gi_Disarm_at_TC_Bits = 0x400,
524 Gi_Disarm_at_Gate_Bits = 0x800,
525 Gi_Disarm_at_TC_or_Gate_Bits = 0xc00,
526 Gi_Loading_On_TC_Bit = 0x1000,
527 Gi_Gate_Polarity_Bit = 0x2000,
528 Gi_Loading_On_Gate_Bit = 0x4000,
529 Gi_Reload_Source_Switching_Bit = 0x8000
532 #define Gi_Second_Gate_Select_Shift 7
533 /*FIXME: m-series has a second gate subselect bit */
534 /*FIXME: m-series second gate sources are undocumented (by NI)*/
535 enum Gi_Second_Gate_Bits {
536 Gi_Second_Gate_Mode_Bit = 0x1,
537 Gi_Second_Gate_Select_Mask = 0x1f << Gi_Second_Gate_Select_Shift,
538 Gi_Second_Gate_Polarity_Bit = 0x2000,
539 Gi_Second_Gate_Subselect_Bit = 0x4000, /* m-series only */
540 Gi_Source_Subselect_Bit = 0x8000 /* m-series only */
542 static inline unsigned Gi_Second_Gate_Select_Bits(unsigned second_gate_select)
544 return (second_gate_select << Gi_Second_Gate_Select_Shift) &
545 Gi_Second_Gate_Select_Mask;
548 enum Gxx_Status_Bits {
551 G0_Counting_Bit = 0x4,
552 G1_Counting_Bit = 0x8,
553 G0_Next_Load_Source_Bit = 0x10,
554 G1_Next_Load_Source_Bit = 0x20,
555 G0_Stale_Data_Bit = 0x40,
556 G1_Stale_Data_Bit = 0x80,
557 G0_Armed_Bit = 0x100,
558 G1_Armed_Bit = 0x200,
559 G0_No_Load_Between_Gates_Bit = 0x400,
560 G1_No_Load_Between_Gates_Bit = 0x800,
561 G0_TC_Error_Bit = 0x1000,
562 G1_TC_Error_Bit = 0x2000,
563 G0_Gate_Error_Bit = 0x4000,
564 G1_Gate_Error_Bit = 0x8000
566 static inline enum Gxx_Status_Bits Gi_Counting_Bit(unsigned counter_index)
568 if (counter_index % 2)
569 return G1_Counting_Bit;
570 return G0_Counting_Bit;
572 static inline enum Gxx_Status_Bits Gi_Armed_Bit(unsigned counter_index)
574 if (counter_index % 2)
578 static inline enum Gxx_Status_Bits Gi_Next_Load_Source_Bit(unsigned
581 if (counter_index % 2)
582 return G1_Next_Load_Source_Bit;
583 return G0_Next_Load_Source_Bit;
585 static inline enum Gxx_Status_Bits Gi_Stale_Data_Bit(unsigned counter_index)
587 if (counter_index % 2)
588 return G1_Stale_Data_Bit;
589 return G0_Stale_Data_Bit;
591 static inline enum Gxx_Status_Bits Gi_TC_Error_Bit(unsigned counter_index)
593 if (counter_index % 2)
594 return G1_TC_Error_Bit;
595 return G0_TC_Error_Bit;
597 static inline enum Gxx_Status_Bits Gi_Gate_Error_Bit(unsigned counter_index)
599 if (counter_index % 2)
600 return G1_Gate_Error_Bit;
601 return G0_Gate_Error_Bit;
604 /* joint reset register bits */
605 static inline unsigned Gi_Reset_Bit(unsigned counter_index)
607 return 0x1 << (2 + (counter_index % 2));
610 enum Gxx_Joint_Status2_Bits {
613 G0_HW_Save_Bit = 0x1000,
614 G1_HW_Save_Bit = 0x2000,
615 G0_Permanent_Stale_Bit = 0x4000,
616 G1_Permanent_Stale_Bit = 0x8000
618 static inline enum Gxx_Joint_Status2_Bits Gi_Permanent_Stale_Bit(unsigned
621 if (counter_index % 2)
622 return G1_Permanent_Stale_Bit;
623 return G0_Permanent_Stale_Bit;
626 enum Gi_DMA_Config_Reg_Bits {
627 Gi_DMA_Enable_Bit = 0x1,
628 Gi_DMA_Write_Bit = 0x2,
632 enum Gi_DMA_Status_Reg_Bits {
633 Gi_DMA_Readbank_Bit = 0x2000,
634 Gi_DRQ_Error_Bit = 0x4000,
635 Gi_DRQ_Status_Bit = 0x8000
638 enum G02_Interrupt_Acknowledge_Bits {
639 G0_Gate_Error_Confirm_Bit = 0x20,
640 G0_TC_Error_Confirm_Bit = 0x40
642 enum G13_Interrupt_Acknowledge_Bits {
643 G1_Gate_Error_Confirm_Bit = 0x2,
644 G1_TC_Error_Confirm_Bit = 0x4
646 static inline unsigned Gi_Gate_Error_Confirm_Bit(unsigned counter_index)
648 if (counter_index % 2)
649 return G1_Gate_Error_Confirm_Bit;
650 return G0_Gate_Error_Confirm_Bit;
652 static inline unsigned Gi_TC_Error_Confirm_Bit(unsigned counter_index)
654 if (counter_index % 2)
655 return G1_TC_Error_Confirm_Bit;
656 return G0_TC_Error_Confirm_Bit;
659 // bits that are the same in G0/G2 and G1/G3 interrupt acknowledge registers
660 enum Gxx_Interrupt_Acknowledge_Bits {
661 Gi_TC_Interrupt_Ack_Bit = 0x4000,
662 Gi_Gate_Interrupt_Ack_Bit = 0x8000
665 enum Gi_Status_Bits {
666 Gi_Gate_Interrupt_Bit = 0x4,
668 Gi_Interrupt_Bit = 0x8000
671 enum G02_Interrupt_Enable_Bits {
672 G0_TC_Interrupt_Enable_Bit = 0x40,
673 G0_Gate_Interrupt_Enable_Bit = 0x100
675 enum G13_Interrupt_Enable_Bits {
676 G1_TC_Interrupt_Enable_Bit = 0x200,
677 G1_Gate_Interrupt_Enable_Bit = 0x400
679 static inline unsigned Gi_Gate_Interrupt_Enable_Bit(unsigned counter_index)
683 if (counter_index % 2) {
684 bit = G1_Gate_Interrupt_Enable_Bit;
686 bit = G0_Gate_Interrupt_Enable_Bit;
691 static inline void write_register(struct ni_gpct *counter, unsigned bits,
692 enum ni_gpct_register reg)
694 BUG_ON(reg >= NITIO_Num_Registers);
695 counter->counter_dev->write_register(counter, bits, reg);
698 static inline unsigned read_register(struct ni_gpct *counter,
699 enum ni_gpct_register reg)
701 BUG_ON(reg >= NITIO_Num_Registers);
702 return counter->counter_dev->read_register(counter, reg);
705 static inline int ni_tio_counting_mode_registers_present(
706 const struct ni_gpct_device *counter_dev)
708 switch (counter_dev->variant) {
709 case ni_gpct_variant_e_series:
712 case ni_gpct_variant_m_series:
713 case ni_gpct_variant_660x:
723 static inline void ni_tio_set_bits_transient(struct ni_gpct *counter,
724 enum ni_gpct_register register_index, unsigned bit_mask,
725 unsigned bit_values, unsigned transient_bit_values)
727 struct ni_gpct_device *counter_dev = counter->counter_dev;
730 BUG_ON(register_index >= NITIO_Num_Registers);
731 comedi_spin_lock_irqsave(&counter_dev->regs_lock, flags);
732 counter_dev->regs[register_index] &= ~bit_mask;
733 counter_dev->regs[register_index] |= (bit_values & bit_mask);
734 write_register(counter,
735 counter_dev->regs[register_index] | transient_bit_values,
738 comedi_spin_unlock_irqrestore(&counter_dev->regs_lock, flags);
741 /* ni_tio_set_bits( ) is for safely writing to registers whose bits may be
742 twiddled in interrupt context, or whose software copy may be read in interrupt context.
744 static inline void ni_tio_set_bits(struct ni_gpct *counter,
745 enum ni_gpct_register register_index, unsigned bit_mask,
748 ni_tio_set_bits_transient(counter, register_index, bit_mask, bit_values,
752 /* ni_tio_get_soft_copy( ) is for safely reading the software copy of a register
753 whose bits might be modified in interrupt context, or whose software copy
754 might need to be read in interrupt context.
756 static inline unsigned ni_tio_get_soft_copy(const struct ni_gpct *counter,
757 enum ni_gpct_register register_index)
759 struct ni_gpct_device *counter_dev = counter->counter_dev;
763 BUG_ON(register_index >= NITIO_Num_Registers);
764 comedi_spin_lock_irqsave(&counter_dev->regs_lock, flags);
765 value = counter_dev->regs[register_index];
766 comedi_spin_unlock_irqrestore(&counter_dev->regs_lock, flags);
770 int ni_tio_arm(struct ni_gpct *counter, int arm, unsigned start_trigger);
771 int ni_tio_set_gate_src(struct ni_gpct *counter, unsigned gate_index,
772 lsampl_t gate_source);
774 #endif /* _COMEDI_NI_TIO_INTERNAL_H */