2 comedi/drivers/ni_tio.c
3 Support for NI general purpose counters
5 Copyright (C) 2006 Frank Mori Hess <fmhess@users.sourceforge.net>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 Description: National Instruments general purpose counters
26 Author: J.P. Mellor <jpmellor@rose-hulman.edu>,
27 Herman.Bruyninckx@mech.kuleuven.ac.be,
28 Wim.Meeussen@mech.kuleuven.ac.be,
29 Klaas.Gadeyne@mech.kuleuven.ac.be,
30 Frank Mori Hess <fmhess@users.sourceforge.net>
31 Updated: Thu Nov 16 09:50:32 EST 2006
34 This module is not used directly by end-users. Rather, it
35 is used by other drivers (for example ni_660x and ni_pcimio)
36 to provide support for NI's general purpose counters. It was
37 originally based on the counter code from ni_660x.c and
41 DAQ 660x Register-Level Programmer Manual (NI 370505A-01)
42 DAQ 6601/6602 User Manual (NI 322137B-01)
43 340934b.pdf DAQ-STC reference manual
50 static uint64_t ni_tio_clock_period_ps(const struct ni_gpct *counter, unsigned generic_clock_source);
51 static unsigned ni_tio_generic_clock_src_select(const struct ni_gpct *counter);
53 MODULE_AUTHOR("Comedi <comedi@comedi.org>");
54 MODULE_DESCRIPTION("Comedi support for NI general-purpose counters");
55 MODULE_LICENSE("GPL");
57 static inline enum ni_gpct_register NITIO_Gi_Autoincrement_Reg(unsigned counter_index)
62 return NITIO_G0_Autoincrement_Reg;
65 return NITIO_G1_Autoincrement_Reg;
68 return NITIO_G2_Autoincrement_Reg;
71 return NITIO_G3_Autoincrement_Reg;
80 static inline enum ni_gpct_register NITIO_Gi_Command_Reg(unsigned counter_index)
85 return NITIO_G0_Command_Reg;
88 return NITIO_G1_Command_Reg;
91 return NITIO_G2_Command_Reg;
94 return NITIO_G3_Command_Reg;
103 static inline enum ni_gpct_register NITIO_Gi_Counting_Mode_Reg(unsigned counter_index)
105 switch(counter_index)
108 return NITIO_G0_Counting_Mode_Reg;
111 return NITIO_G1_Counting_Mode_Reg;
114 return NITIO_G2_Counting_Mode_Reg;
117 return NITIO_G3_Counting_Mode_Reg;
126 static inline enum ni_gpct_register NITIO_Gi_Input_Select_Reg(unsigned counter_index)
128 switch(counter_index)
131 return NITIO_G0_Input_Select_Reg;
134 return NITIO_G1_Input_Select_Reg;
137 return NITIO_G2_Input_Select_Reg;
140 return NITIO_G3_Input_Select_Reg;
149 static inline enum ni_gpct_register NITIO_Gxx_Joint_Reset_Reg(unsigned counter_index)
151 switch(counter_index)
155 return NITIO_G01_Joint_Reset_Reg;
159 return NITIO_G23_Joint_Reset_Reg;
168 static inline enum ni_gpct_register NITIO_Gxx_Status_Reg(unsigned counter_index)
170 switch(counter_index)
174 return NITIO_G01_Status_Reg;
178 return NITIO_G23_Status_Reg;
187 static inline enum ni_gpct_register NITIO_Gi_LoadA_Reg(unsigned counter_index)
189 switch(counter_index)
192 return NITIO_G0_LoadA_Reg;
195 return NITIO_G1_LoadA_Reg;
198 return NITIO_G2_LoadA_Reg;
201 return NITIO_G3_LoadA_Reg;
210 static inline enum ni_gpct_register NITIO_Gi_LoadB_Reg(unsigned counter_index)
212 switch(counter_index)
215 return NITIO_G0_LoadB_Reg;
218 return NITIO_G1_LoadB_Reg;
221 return NITIO_G2_LoadB_Reg;
224 return NITIO_G3_LoadB_Reg;
233 static inline enum ni_gpct_register NITIO_Gi_Mode_Reg(unsigned counter_index)
235 switch(counter_index)
238 return NITIO_G0_Mode_Reg;
241 return NITIO_G1_Mode_Reg;
244 return NITIO_G2_Mode_Reg;
247 return NITIO_G3_Mode_Reg;
256 static inline enum ni_gpct_register NITIO_Gi_SW_Save_Reg(int counter_index)
258 switch(counter_index)
261 return NITIO_G0_SW_Save_Reg;
264 return NITIO_G1_SW_Save_Reg;
267 return NITIO_G2_SW_Save_Reg;
270 return NITIO_G3_SW_Save_Reg;
279 static inline enum ni_gpct_register NITIO_Gi_Second_Gate_Reg(int counter_index)
281 switch(counter_index)
284 return NITIO_G0_Second_Gate_Reg;
287 return NITIO_G1_Second_Gate_Reg;
290 return NITIO_G2_Second_Gate_Reg;
293 return NITIO_G3_Second_Gate_Reg;
302 static inline enum ni_gpct_register NITIO_Gi_DMA_Config_Reg(int counter_index)
304 switch(counter_index)
307 return NITIO_G0_DMA_Config_Reg;
310 return NITIO_G1_DMA_Config_Reg;
313 return NITIO_G2_DMA_Config_Reg;
316 return NITIO_G3_DMA_Config_Reg;
325 static inline enum ni_gpct_register NITIO_Gi_DMA_Status_Reg(int counter_index)
327 switch(counter_index)
330 return NITIO_G0_DMA_Status_Reg;
333 return NITIO_G1_DMA_Status_Reg;
336 return NITIO_G2_DMA_Status_Reg;
339 return NITIO_G3_DMA_Status_Reg;
348 static inline enum ni_gpct_register NITIO_Gi_ABZ_Reg(int counter_index)
350 switch(counter_index)
353 return NITIO_G0_ABZ_Reg;
356 return NITIO_G1_ABZ_Reg;
365 enum Gi_Auto_Increment_Reg_Bits
367 Gi_Auto_Increment_Mask = 0xff
370 #define Gi_Up_Down_Shift 5
371 enum Gi_Command_Reg_Bits
374 Gi_Save_Trace_Bit = 0x2,
376 Gi_Disarm_Bit = 0x10,
377 Gi_Up_Down_Mask = 0x3 << Gi_Up_Down_Shift,
378 Gi_Always_Down_Bits = 0x0 << Gi_Up_Down_Shift,
379 Gi_Always_Up_Bits = 0x1 << Gi_Up_Down_Shift,
380 Gi_Up_Down_Hardware_IO_Bits = 0x2 << Gi_Up_Down_Shift,
381 Gi_Up_Down_Hardware_Gate_Bits = 0x3 << Gi_Up_Down_Shift,
382 Gi_Write_Switch_Bit = 0x80,
383 Gi_Synchronize_Gate_Bit = 0x100,
384 Gi_Little_Big_Endian_Bit = 0x200,
385 Gi_Bank_Switch_Start_Bit = 0x400,
386 Gi_Bank_Switch_Mode_Bit = 0x800,
387 Gi_Bank_Switch_Enable_Bit = 0x1000,
388 Gi_Arm_Copy_Bit = 0x2000,
389 Gi_Save_Trace_Copy_Bit = 0x4000,
390 Gi_Disarm_Copy_Bit = 0x8000
393 #define Gi_Index_Phase_Bitshift 5
394 #define Gi_HW_Arm_Select_Shift 8
395 enum Gi_Counting_Mode_Reg_Bits
397 Gi_Counting_Mode_Mask = 0x7,
398 Gi_Counting_Mode_Normal_Bits = 0x0,
399 Gi_Counting_Mode_QuadratureX1_Bits = 0x1,
400 Gi_Counting_Mode_QuadratureX2_Bits = 0x2,
401 Gi_Counting_Mode_QuadratureX4_Bits = 0x3,
402 Gi_Counting_Mode_Two_Pulse_Bits = 0x4,
403 Gi_Counting_Mode_Sync_Source_Bits = 0x6,
404 Gi_Index_Mode_Bit = 0x10,
405 Gi_Index_Phase_Mask = 0x3 << Gi_Index_Phase_Bitshift,
406 Gi_Index_Phase_LowA_LowB = 0x0 << Gi_Index_Phase_Bitshift,
407 Gi_Index_Phase_LowA_HighB = 0x1 << Gi_Index_Phase_Bitshift,
408 Gi_Index_Phase_HighA_LowB = 0x2 << Gi_Index_Phase_Bitshift,
409 Gi_Index_Phase_HighA_HighB = 0x3 << Gi_Index_Phase_Bitshift,
410 Gi_HW_Arm_Enable_Bit = 0x80, /* from m-series example code, not documented in 660x register level manual */
411 Gi_660x_HW_Arm_Select_Mask = 0x7 << Gi_HW_Arm_Select_Shift, /* from m-series example code, not documented in 660x register level manual */
412 Gi_660x_Prescale_X8_Bit = 0x1000,
413 Gi_M_Series_Prescale_X8_Bit = 0x2000,
414 Gi_M_Series_HW_Arm_Select_Mask = 0x1f << Gi_HW_Arm_Select_Shift,
415 /* must be set for clocks over 40MHz, which includes synchronous counting and quadrature modes */
416 Gi_660x_Alternate_Sync_Bit = 0x2000,
417 Gi_M_Series_Alternate_Sync_Bit = 0x4000,
418 Gi_660x_Prescale_X2_Bit = 0x4000, /* from m-series example code, not documented in 660x register level manual */
419 Gi_M_Series_Prescale_X2_Bit = 0x8000,
421 static inline unsigned Gi_Alternate_Sync_Bit(enum ni_gpct_variant variant)
425 case ni_gpct_variant_e_series:
428 case ni_gpct_variant_m_series:
429 return Gi_M_Series_Alternate_Sync_Bit;
431 case ni_gpct_variant_660x:
432 return Gi_660x_Alternate_Sync_Bit;
440 static inline unsigned Gi_Prescale_X2_Bit(enum ni_gpct_variant variant)
444 case ni_gpct_variant_e_series:
447 case ni_gpct_variant_m_series:
448 return Gi_M_Series_Prescale_X2_Bit;
450 case ni_gpct_variant_660x:
451 return Gi_660x_Prescale_X2_Bit;
459 static inline unsigned Gi_Prescale_X8_Bit(enum ni_gpct_variant variant)
463 case ni_gpct_variant_e_series:
466 case ni_gpct_variant_m_series:
467 return Gi_M_Series_Prescale_X8_Bit;
469 case ni_gpct_variant_660x:
470 return Gi_660x_Prescale_X8_Bit;
478 static inline unsigned Gi_HW_Arm_Select_Mask(enum ni_gpct_variant variant)
482 case ni_gpct_variant_e_series:
485 case ni_gpct_variant_m_series:
486 return Gi_M_Series_HW_Arm_Select_Mask;
488 case ni_gpct_variant_660x:
489 return Gi_660x_HW_Arm_Select_Mask;
498 /* clock sources for ni_660x boards, get bits with Gi_Source_Select_Bits() */
499 enum ni_660x_clock_source
501 NI_660x_Timebase_1_Clock = 0x0, /* 20MHz */
502 NI_660x_Source_Pin_i_Clock = 0x1,
503 NI_660x_Next_Gate_Clock = 0xa,
504 NI_660x_Timebase_2_Clock = 0x12, /* 100KHz */
505 NI_660x_Next_TC_Clock = 0x13,
506 NI_660x_Timebase_3_Clock = 0x1e, /* 80MHz */
507 NI_660x_Logic_Low_Clock = 0x1f,
509 static const unsigned ni_660x_max_rtsi_channel = 6;
510 static inline unsigned NI_660x_RTSI_Clock(unsigned n)
512 BUG_ON(n > ni_660x_max_rtsi_channel);
515 static const unsigned ni_660x_max_source_pin = 7;
516 static inline unsigned NI_660x_Source_Pin_Clock(unsigned n)
518 BUG_ON(n > ni_660x_max_source_pin);
522 /* clock sources for ni e and m series boards, get bits with Gi_Source_Select_Bits() */
523 enum ni_m_series_clock_source
525 NI_M_Series_Timebase_1_Clock = 0x0, /* 20MHz */
526 NI_M_Series_Timebase_2_Clock = 0x12, /* 100KHz */
527 NI_M_Series_Next_TC_Clock = 0x13,
528 NI_M_Series_Next_Gate_Clock = 0x14, /* when Gi_Src_SubSelect = 0 */
529 NI_M_Series_PXI_Star_Trigger_Clock = 0x14, /* when Gi_Src_SubSelect = 1 */
530 NI_M_Series_PXI10_Clock = 0x1d,
531 NI_M_Series_Timebase_3_Clock = 0x1e, /* 80MHz, when Gi_Src_SubSelect = 0 */
532 NI_M_Series_Analog_Trigger_Out_Clock = 0x1e, /* when Gi_Src_SubSelect = 1 */
533 NI_M_Series_Logic_Low_Clock = 0x1f,
535 static const unsigned ni_m_series_max_pfi_channel = 15;
536 static inline unsigned NI_M_Series_PFI_Clock(unsigned n)
538 BUG_ON(n > ni_m_series_max_pfi_channel);
539 if(n < 10) return 1 + n;
542 static const unsigned ni_m_series_max_rtsi_channel = 7;
543 static inline unsigned NI_M_Series_RTSI_Clock(unsigned n)
545 BUG_ON(n > ni_m_series_max_rtsi_channel);
546 if(n == 7) return 0x1b;
550 enum ni_660x_gate_select
552 NI_660x_Source_Pin_i_Gate_Select = 0x0,
553 NI_660x_Gate_Pin_i_Gate_Select = 0x1,
554 NI_660x_Next_SRC_Gate_Select = 0xa,
555 NI_660x_Next_Out_Gate_Select = 0x14,
556 NI_660x_Logic_Low_Gate_Select = 0x1f,
558 static const unsigned ni_660x_max_gate_pin = 7;
559 static inline unsigned NI_660x_Gate_Pin_Gate_Select(unsigned n)
561 BUG_ON(n > ni_660x_max_gate_pin);
564 static inline unsigned NI_660x_RTSI_Gate_Select(unsigned n)
566 BUG_ON(n > ni_660x_max_rtsi_channel);
570 enum ni_m_series_gate_select
572 NI_M_Series_Timestamp_Mux_Gate_Select = 0x0,
573 NI_M_Series_AI_START2_Gate_Select = 0x12,
574 NI_M_Series_PXI_Star_Trigger_Gate_Select = 0x13,
575 NI_M_Series_Next_Out_Gate_Select = 0x14,
576 NI_M_Series_AI_START1_Gate_Select = 0x1c,
577 NI_M_Series_Next_SRC_Gate_Select = 0x1d,
578 NI_M_Series_Analog_Trigger_Out_Gate_Select = 0x1e,
579 NI_M_Series_Logic_Low_Gate_Select = 0x1f,
581 static inline unsigned NI_M_Series_RTSI_Gate_Select(unsigned n)
583 BUG_ON(n > ni_m_series_max_rtsi_channel);
588 static inline unsigned NI_M_Series_PFI_Gate_Select(unsigned n)
590 BUG_ON(n > ni_m_series_max_pfi_channel);
596 #define Gi_Source_Select_Shift 2
597 #define Gi_Gate_Select_Shift 7
598 enum Gi_Input_Select_Bits
600 Gi_Read_Acknowledges_Irq = 0x1, // e-series only
601 Gi_Write_Acknowledges_Irq = 0x2, // e-series only
602 Gi_Source_Select_Mask = 0x7c,
603 Gi_Gate_Select_Mask = 0x1f << Gi_Gate_Select_Shift,
604 Gi_Gate_Select_Load_Source_Bit = 0x1000,
605 Gi_Or_Gate_Bit = 0x2000,
606 Gi_Output_Polarity_Bit = 0x4000, /* set to invert */
607 Gi_Source_Polarity_Bit = 0x8000 /* set to invert */
609 static inline unsigned Gi_Source_Select_Bits(unsigned source)
611 return (source << Gi_Source_Select_Shift) & Gi_Source_Select_Mask;
613 static inline unsigned Gi_Gate_Select_Bits(unsigned gate_select)
615 return (gate_select << Gi_Gate_Select_Shift) & Gi_Gate_Select_Mask;
620 Gi_Gating_Mode_Mask = 0x3,
621 Gi_Gating_Disabled_Bits = 0x0,
622 Gi_Level_Gating_Bits = 0x1,
623 Gi_Rising_Edge_Gating_Bits = 0x2,
624 Gi_Falling_Edge_Gating_Bits = 0x3,
625 Gi_Gate_On_Both_Edges_Bit = 0x4, /* used in conjunction with rising edge gating mode */
626 Gi_Trigger_Mode_for_Edge_Gate_Mask = 0x18,
627 Gi_Edge_Gate_Starts_Stops_Bits = 0x0,
628 Gi_Edge_Gate_Stops_Starts_Bits = 0x8,
629 Gi_Edge_Gate_Starts_Bits = 0x10,
630 Gi_Edge_Gate_No_Starts_or_Stops_Bits = 0x18,
631 Gi_Stop_Mode_Mask = 0x60,
632 Gi_Stop_on_Gate_Bits = 0x00,
633 Gi_Stop_on_Gate_or_TC_Bits = 0x20,
634 Gi_Stop_on_Gate_or_Second_TC_Bits = 0x40,
635 Gi_Load_Source_Select_Bit = 0x80,
636 Gi_Output_Mode_Mask = 0x300,
637 Gi_Output_TC_Pulse_Bits = 0x100,
638 Gi_Output_TC_Toggle_Bits = 0x200,
639 Gi_Output_TC_or_Gate_Toggle_Bits = 0x300,
640 Gi_Counting_Once_Mask = 0xc00,
641 Gi_No_Hardware_Disarm_Bits = 0x000,
642 Gi_Disarm_at_TC_Bits = 0x400,
643 Gi_Disarm_at_Gate_Bits = 0x800,
644 Gi_Disarm_at_TC_or_Gate_Bits = 0xc00,
645 Gi_Loading_On_TC_Bit = 0x1000,
646 Gi_Gate_Polarity_Bit = 0x2000,
647 Gi_Loading_On_Gate_Bit = 0x4000,
648 Gi_Reload_Source_Switching_Bit = 0x8000
651 enum ni_660x_second_gate_select
653 NI_660x_Source_Pin_i_Second_Gate_Select = 0x0,
654 NI_660x_Up_Down_Pin_i_Second_Gate_Select = 0x1,
655 NI_660x_Next_SRC_Second_Gate_Select = 0xa,
656 NI_660x_Next_Out_Second_Gate_Select = 0x14,
657 NI_660x_Selected_Gate_Second_Gate_Select = 0x1e,
658 NI_660x_Logic_Low_Second_Gate_Select = 0x1f,
660 static const unsigned ni_660x_max_up_down_pin = 7;
661 static inline unsigned NI_660x_Up_Down_Pin_Second_Gate_Select(unsigned n)
663 BUG_ON(n > ni_660x_max_up_down_pin);
666 static inline unsigned NI_660x_RTSI_Second_Gate_Select(unsigned n)
668 BUG_ON(n > ni_660x_max_rtsi_channel);
672 #define Gi_Second_Gate_Select_Shift 7
673 /*FIXME: m-series has a second gate subselect bit */
674 /*FIXME: m-series second gate sources are undocumented (by NI)*/
675 enum Gi_Second_Gate_Bits
677 Gi_Second_Gate_Mode_Bit = 0x1,
678 Gi_Second_Gate_Select_Mask = 0x1f << Gi_Second_Gate_Select_Shift,
679 Gi_Second_Gate_Polarity_Bit = 0x2000,
680 Gi_Second_Gate_Subselect_Bit = 0x4000, /* m-series only */
681 Gi_Source_Subselect_Bit = 0x8000 /* m-series only */
683 static inline unsigned Gi_Second_Gate_Select_Bits(unsigned second_gate_select)
685 return (second_gate_select << Gi_Second_Gate_Select_Shift) & Gi_Second_Gate_Select_Mask;
692 G0_Counting_Bit = 0x4,
693 G1_Counting_Bit = 0x8,
694 G0_Next_Load_Source_Bit = 0x10,
695 G1_Next_Load_Source_Bit = 0x20,
696 G0_Stale_Data_Bit = 0x40,
697 G1_Stale_Data_Bit = 0x80,
698 G0_Armed_Bit = 0x100,
699 G1_Armed_Bit = 0x200,
700 G0_No_Load_Between_Gates_Bit = 0x400,
701 G1_No_Load_Between_Gates_Bit = 0x800,
702 G0_TC_Error_Bit = 0x1000,
703 G1_TC_Error_Bit = 0x2000
705 static inline unsigned Gi_Counting_Bit(unsigned counter_index)
707 if(counter_index % 2) return G1_Counting_Bit;
708 return G0_Counting_Bit;
710 static inline unsigned Gi_Armed_Bit(unsigned counter_index)
712 if(counter_index % 2) return G1_Armed_Bit;
715 static inline unsigned Gi_Next_Load_Source_Bit(unsigned counter_index)
717 if(counter_index % 2) return G1_Next_Load_Source_Bit;
718 return G0_Next_Load_Source_Bit;
721 /* joint reset register bits */
722 static inline unsigned Gi_Reset_Bit(unsigned counter_index)
724 return 0x1 << (2 + (counter_index % 2));
727 enum Gi_DMA_Config_Reg_Bits
729 Gi_DMA_Enable_Bit = 0x1,
730 Gi_DMA_Write_Bit = 0x2,
734 static const lsampl_t counter_status_mask = COMEDI_COUNTER_ARMED | COMEDI_COUNTER_COUNTING;
736 static int __init ni_tio_init_module(void)
740 module_init(ni_tio_init_module);
742 static void __exit ni_tio_cleanup_module(void)
745 module_exit(ni_tio_cleanup_module);
747 struct ni_gpct_device* ni_gpct_device_construct(comedi_device *dev,
748 void (*write_register)(struct ni_gpct *counter, unsigned bits, enum ni_gpct_register reg),
749 unsigned (*read_register)(struct ni_gpct *counter, enum ni_gpct_register reg),
750 enum ni_gpct_variant variant, unsigned num_counters)
754 struct ni_gpct_device *counter_dev = kmalloc(sizeof(struct ni_gpct_device), GFP_KERNEL);
755 if(counter_dev == NULL) return NULL;
756 counter_dev->dev = dev;
757 counter_dev->write_register = write_register;
758 counter_dev->read_register = read_register;
759 counter_dev->variant = variant;
760 BUG_ON(num_counters == 0);
761 counter_dev->counters = kzalloc(sizeof(struct ni_gpct) * num_counters, GFP_KERNEL);
762 if(counter_dev->counters == NULL)
767 for(i = 0; i < num_counters; ++i)
769 counter_dev->counters[i].counter_dev = counter_dev;
771 counter_dev->num_counters = num_counters;
775 void ni_gpct_device_destroy(struct ni_gpct_device *counter_dev)
777 if(counter_dev->counters == NULL) return;
778 kfree(counter_dev->counters);
782 static int ni_tio_counting_mode_registers_present(const struct ni_gpct_device *counter_dev)
784 switch(counter_dev->variant)
786 case ni_gpct_variant_e_series:
789 case ni_gpct_variant_m_series:
790 case ni_gpct_variant_660x:
800 static int ni_tio_second_gate_registers_present(const struct ni_gpct_device *counter_dev)
802 switch(counter_dev->variant)
804 case ni_gpct_variant_e_series:
807 case ni_gpct_variant_m_series:
808 case ni_gpct_variant_660x:
818 static void ni_tio_reset_count_and_disarm(struct ni_gpct *counter)
820 counter->counter_dev->write_register(counter, Gi_Reset_Bit(counter->counter_index),
821 NITIO_Gxx_Joint_Reset_Reg(counter->counter_index));
824 void ni_tio_init_counter(struct ni_gpct *counter)
826 struct ni_gpct_device *counter_dev = counter->counter_dev;
828 ni_tio_reset_count_and_disarm(counter);
829 /* initialize counter registers */
830 counter_dev->regs[NITIO_Gi_Autoincrement_Reg(counter->counter_index)] = 0x0;
831 counter_dev->write_register(counter, counter_dev->regs[NITIO_Gi_Autoincrement_Reg(counter->counter_index)],
832 NITIO_Gi_Autoincrement_Reg(counter->counter_index));
833 counter_dev->regs[NITIO_Gi_Command_Reg(counter->counter_index)] = Gi_Synchronize_Gate_Bit;
834 counter_dev->write_register(counter, counter_dev->regs[NITIO_Gi_Command_Reg(counter->counter_index)],
835 NITIO_Gi_Command_Reg(counter->counter_index));
836 counter_dev->regs[NITIO_Gi_Mode_Reg(counter->counter_index)] = 0x0;
837 counter_dev->write_register(counter, counter_dev->regs[NITIO_Gi_Mode_Reg(counter->counter_index)],
838 NITIO_Gi_Mode_Reg(counter->counter_index));
839 counter_dev->regs[NITIO_Gi_LoadA_Reg(counter->counter_index)] = 0x0;
840 counter_dev->write_register(counter, counter_dev->regs[NITIO_Gi_LoadA_Reg(counter->counter_index)],
841 NITIO_Gi_LoadA_Reg(counter->counter_index));
842 counter_dev->regs[NITIO_Gi_LoadB_Reg(counter->counter_index)] = 0x0;
843 counter_dev->write_register(counter, counter_dev->regs[NITIO_Gi_LoadB_Reg(counter->counter_index)],
844 NITIO_Gi_LoadB_Reg(counter->counter_index));
845 counter_dev->regs[NITIO_Gi_Input_Select_Reg(counter->counter_index)] = 0x0;
846 counter_dev->write_register(counter, counter_dev->regs[NITIO_Gi_Input_Select_Reg(counter->counter_index)],
847 NITIO_Gi_Input_Select_Reg(counter->counter_index));
848 if(ni_tio_counting_mode_registers_present(counter_dev))
850 counter_dev->regs[NITIO_Gi_Counting_Mode_Reg(counter->counter_index)] = 0x0;
851 counter_dev->write_register(counter, counter_dev->regs[NITIO_Gi_Counting_Mode_Reg(counter->counter_index)],
852 NITIO_Gi_Counting_Mode_Reg(counter->counter_index));
854 if(ni_tio_second_gate_registers_present(counter_dev))
856 counter_dev->regs[NITIO_Gi_Second_Gate_Reg(counter->counter_index)] = 0x0;
857 counter_dev->write_register(counter, counter_dev->regs[NITIO_Gi_Second_Gate_Reg(counter->counter_index)],
858 NITIO_Gi_Second_Gate_Reg(counter->counter_index));
862 static lsampl_t ni_tio_counter_status(struct ni_gpct *counter)
865 const unsigned bits = counter->counter_dev->read_register(counter, NITIO_Gxx_Status_Reg(counter->counter_index));
866 if(bits & Gi_Armed_Bit(counter->counter_index))
868 status |= COMEDI_COUNTER_ARMED;
869 if(bits & Gi_Counting_Bit(counter->counter_index))
870 status |= COMEDI_COUNTER_COUNTING;
875 static void ni_tio_set_sync_mode(struct ni_gpct *counter,
878 struct ni_gpct_device *counter_dev = counter->counter_dev;
879 const unsigned counting_mode_reg = NITIO_Gi_Counting_Mode_Reg(counter->counter_index);
880 static const uint64_t min_normal_sync_period_ps = 25000;
881 const uint64_t clock_period_ps = ni_tio_clock_period_ps(counter,
882 ni_tio_generic_clock_src_select(counter));
884 if(ni_tio_counting_mode_registers_present(counter_dev) == 0) return;
886 switch(counter_dev->regs[counting_mode_reg] & Gi_Counting_Mode_Mask)
888 case Gi_Counting_Mode_QuadratureX1_Bits:
889 case Gi_Counting_Mode_QuadratureX2_Bits:
890 case Gi_Counting_Mode_QuadratureX4_Bits:
891 case Gi_Counting_Mode_Sync_Source_Bits:
897 /* It's not clear what we should do if clock_period is unknown, so we are not
898 using the alt sync bit in that case, but allow the caller to decide by using the
899 force_alt_sync parameter. */
901 (clock_period_ps && clock_period_ps < min_normal_sync_period_ps))
903 counter_dev->regs[counting_mode_reg] |= Gi_Alternate_Sync_Bit(counter_dev->variant);
906 counter_dev->regs[counting_mode_reg] &= ~Gi_Alternate_Sync_Bit(counter_dev->variant);
908 counter_dev->write_register(counter, counter_dev->regs[counting_mode_reg], counting_mode_reg);
911 static int ni_tio_set_counter_mode(struct ni_gpct *counter, unsigned mode)
913 struct ni_gpct_device *counter_dev = counter->counter_dev;
915 const unsigned counting_mode_reg = NITIO_Gi_Counting_Mode_Reg(counter->counter_index);
916 const unsigned mode_reg = NITIO_Gi_Mode_Reg(counter->counter_index);
917 const unsigned command_reg = NITIO_Gi_Command_Reg(counter->counter_index);
918 const unsigned input_select_reg = NITIO_Gi_Input_Select_Reg(counter->counter_index);
919 /* these bits map directly on to the mode register */
920 static const unsigned mode_reg_direct_mask = NI_GPCT_GATE_ON_BOTH_EDGES_BIT |
921 NI_GPCT_EDGE_GATE_MODE_MASK | NI_GPCT_STOP_MODE_MASK |
922 NI_GPCT_OUTPUT_MODE_MASK | NI_GPCT_HARDWARE_DISARM_MASK |
923 NI_GPCT_LOADING_ON_TC_BIT | NI_GPCT_LOADING_ON_GATE_BIT |
924 NI_GPCT_LOAD_B_SELECT_BIT;
926 switch(mode & NI_GPCT_RELOAD_SOURCE_MASK)
928 case NI_GPCT_RELOAD_SOURCE_FIXED_BITS:
929 counter_dev->regs[mode_reg] &= ~Gi_Reload_Source_Switching_Bit;
930 counter_dev->regs[input_select_reg] &= ~Gi_Gate_Select_Load_Source_Bit;
932 case NI_GPCT_RELOAD_SOURCE_SWITCHING_BITS:
933 counter_dev->regs[mode_reg] |= Gi_Reload_Source_Switching_Bit;
934 counter_dev->regs[input_select_reg] &= ~Gi_Gate_Select_Load_Source_Bit;
936 case NI_GPCT_RELOAD_SOURCE_GATE_SELECT_BITS:
937 counter_dev->regs[input_select_reg] |= Gi_Gate_Select_Load_Source_Bit;
938 counter_dev->regs[mode_reg] &= ~(Gi_Reload_Source_Switching_Bit | Gi_Gating_Mode_Mask);
939 counter_dev->regs[mode_reg] |= Gi_Level_Gating_Bits;
945 counter_dev->regs[mode_reg] &= ~mode_reg_direct_mask;
946 counter_dev->regs[mode_reg] |= mode & mode_reg_direct_mask;
947 counter_dev->write_register(counter, counter_dev->regs[mode_reg], mode_reg);
949 if(ni_tio_counting_mode_registers_present(counter_dev))
951 counter_dev->regs[counting_mode_reg] &= ~Gi_Counting_Mode_Mask;
952 counter_dev->regs[counting_mode_reg] |= (mode >> NI_GPCT_COUNTING_MODE_SHIFT) & Gi_Counting_Mode_Mask;
953 counter_dev->regs[counting_mode_reg] &= ~Gi_Index_Phase_Mask;
954 counter_dev->regs[counting_mode_reg] |= ((mode >> NI_GPCT_INDEX_PHASE_BITSHIFT) << Gi_Index_Phase_Bitshift) & Gi_Index_Phase_Mask;
955 if(mode & NI_GPCT_INDEX_ENABLE_BIT)
957 counter_dev->regs[counting_mode_reg] |= Gi_Index_Mode_Bit;
960 counter_dev->regs[counting_mode_reg] &= ~Gi_Index_Mode_Bit;
962 counter_dev->write_register(counter, counter_dev->regs[counting_mode_reg], counting_mode_reg);
963 ni_tio_set_sync_mode(counter, 0);
966 counter_dev->regs[command_reg] &= ~Gi_Up_Down_Mask;
967 counter_dev->regs[command_reg] |= ((mode >> NI_GPCT_COUNTING_DIRECTION_SHIFT) << Gi_Up_Down_Shift) & Gi_Up_Down_Mask;
968 counter_dev->write_register(counter, counter_dev->regs[command_reg], command_reg);
970 if(mode & NI_GPCT_OR_GATE_BIT)
972 counter_dev->regs[input_select_reg] |= Gi_Or_Gate_Bit;
975 counter_dev->regs[input_select_reg] &= ~Gi_Or_Gate_Bit;
977 if(mode & NI_GPCT_INVERT_OUTPUT_BIT)
979 counter_dev->regs[input_select_reg] |= Gi_Output_Polarity_Bit;
982 counter_dev->regs[input_select_reg] &= ~Gi_Output_Polarity_Bit;
984 counter_dev->write_register(counter, counter_dev->regs[input_select_reg], input_select_reg);
989 static int ni_tio_arm(struct ni_gpct *counter,
990 int arm, unsigned start_trigger)
992 struct ni_gpct_device *counter_dev = counter->counter_dev;
994 unsigned command_bits = counter_dev->regs[NITIO_Gi_Command_Reg(counter->counter_index)];
997 switch(start_trigger)
999 case NI_GPCT_ARM_IMMEDIATE:
1000 command_bits |= Gi_Arm_Bit;
1002 case NI_GPCT_ARM_PAIRED_IMMEDIATE:
1003 command_bits |= Gi_Arm_Bit | Gi_Arm_Copy_Bit;
1008 if(ni_tio_counting_mode_registers_present(counter_dev))
1010 const unsigned counting_mode_reg = NITIO_Gi_Counting_Mode_Reg(counter->counter_index);
1011 switch(start_trigger)
1013 case NI_GPCT_ARM_IMMEDIATE:
1014 case NI_GPCT_ARM_PAIRED_IMMEDIATE:
1015 counter_dev->regs[counting_mode_reg] &= ~Gi_HW_Arm_Enable_Bit;
1018 if(start_trigger & NI_GPCT_ARM_UNKNOWN)
1020 /* pass-through the least significant bits so we can figure out what select later */
1021 unsigned hw_arm_select_bits;
1023 counter_dev->regs[counting_mode_reg] &= ~Gi_HW_Arm_Select_Mask(counter_dev->variant);
1024 hw_arm_select_bits = (start_trigger << Gi_HW_Arm_Select_Shift) & Gi_HW_Arm_Select_Mask(counter_dev->variant);
1025 counter_dev->regs[counting_mode_reg] |= Gi_HW_Arm_Enable_Bit | hw_arm_select_bits;
1026 counter_dev->write_register(counter, counter_dev->regs[counting_mode_reg], counting_mode_reg);
1036 command_bits |= Gi_Disarm_Bit;
1038 counter_dev->write_register(counter, command_bits, NITIO_Gi_Command_Reg(counter->counter_index));
1042 static unsigned ni_660x_source_select_bits(lsampl_t clock_source)
1044 unsigned ni_660x_clock;
1046 const unsigned clock_select_bits = clock_source & NI_GPCT_CLOCK_SRC_SELECT_MASK;
1048 switch(clock_select_bits)
1050 case NI_GPCT_TIMEBASE_1_CLOCK_SRC_BITS:
1051 ni_660x_clock = NI_660x_Timebase_1_Clock;
1053 case NI_GPCT_TIMEBASE_2_CLOCK_SRC_BITS:
1054 ni_660x_clock = NI_660x_Timebase_2_Clock;
1056 case NI_GPCT_TIMEBASE_3_CLOCK_SRC_BITS:
1057 ni_660x_clock = NI_660x_Timebase_3_Clock;
1059 case NI_GPCT_LOGIC_LOW_CLOCK_SRC_BITS:
1060 ni_660x_clock = NI_660x_Logic_Low_Clock;
1062 case NI_GPCT_SOURCE_PIN_i_CLOCK_SRC_BITS:
1063 ni_660x_clock = NI_660x_Source_Pin_i_Clock;
1065 case NI_GPCT_NEXT_GATE_CLOCK_SRC_BITS:
1066 ni_660x_clock = NI_660x_Next_Gate_Clock;
1068 case NI_GPCT_NEXT_TC_CLOCK_SRC_BITS:
1069 ni_660x_clock = NI_660x_Next_TC_Clock;
1072 for(i = 0; i <= ni_660x_max_rtsi_channel; ++i)
1074 if(clock_select_bits == NI_GPCT_RTSI_CLOCK_SRC_BITS(i))
1076 ni_660x_clock = NI_660x_RTSI_Clock(i);
1080 if(i <= ni_660x_max_rtsi_channel) break;
1081 for(i = 0; i <= ni_660x_max_source_pin; ++i)
1083 if(clock_select_bits == NI_GPCT_SOURCE_PIN_CLOCK_SRC_BITS(i))
1085 ni_660x_clock = NI_660x_Source_Pin_Clock(i);
1089 if(i <= ni_660x_max_source_pin) break;
1094 return Gi_Source_Select_Bits(ni_660x_clock);
1097 static unsigned ni_m_series_source_select_bits(lsampl_t clock_source)
1099 unsigned ni_m_series_clock;
1101 const unsigned clock_select_bits = clock_source & NI_GPCT_CLOCK_SRC_SELECT_MASK;
1102 switch(clock_select_bits)
1104 case NI_GPCT_TIMEBASE_1_CLOCK_SRC_BITS:
1105 ni_m_series_clock = NI_M_Series_Timebase_1_Clock;
1107 case NI_GPCT_TIMEBASE_2_CLOCK_SRC_BITS:
1108 ni_m_series_clock = NI_M_Series_Timebase_2_Clock;
1110 case NI_GPCT_TIMEBASE_3_CLOCK_SRC_BITS:
1111 ni_m_series_clock = NI_M_Series_Timebase_3_Clock;
1113 case NI_GPCT_LOGIC_LOW_CLOCK_SRC_BITS:
1114 ni_m_series_clock = NI_M_Series_Logic_Low_Clock;
1116 case NI_GPCT_NEXT_GATE_CLOCK_SRC_BITS:
1117 ni_m_series_clock = NI_M_Series_Next_Gate_Clock;
1119 case NI_GPCT_NEXT_TC_CLOCK_SRC_BITS:
1120 ni_m_series_clock = NI_M_Series_Next_TC_Clock;
1122 case NI_GPCT_PXI10_CLOCK_SRC_BITS:
1123 ni_m_series_clock = NI_M_Series_PXI10_Clock;
1125 case NI_GPCT_PXI_STAR_TRIGGER_CLOCK_SRC_BITS:
1126 ni_m_series_clock = NI_M_Series_PXI_Star_Trigger_Clock;
1128 case NI_GPCT_ANALOG_TRIGGER_OUT_CLOCK_SRC_BITS:
1129 ni_m_series_clock = NI_M_Series_Analog_Trigger_Out_Clock;
1132 for(i = 0; i <= ni_m_series_max_rtsi_channel; ++i)
1134 if(clock_select_bits == NI_GPCT_RTSI_CLOCK_SRC_BITS(i))
1136 ni_m_series_clock = NI_M_Series_RTSI_Clock(i);
1140 if(i <= ni_m_series_max_rtsi_channel) break;
1141 for(i = 0; i <= ni_m_series_max_pfi_channel; ++i)
1143 if(clock_select_bits == NI_GPCT_PFI_CLOCK_SRC_BITS(i))
1145 ni_m_series_clock = NI_M_Series_PFI_Clock(i);
1149 if(i <= ni_m_series_max_pfi_channel) break;
1150 rt_printk("invalid clock source 0x%lx\n", (unsigned long)clock_source);
1152 ni_m_series_clock = 0;
1155 return Gi_Source_Select_Bits(ni_m_series_clock);
1158 static void ni_tio_set_source_subselect(struct ni_gpct *counter, lsampl_t clock_source)
1160 struct ni_gpct_device *counter_dev = counter->counter_dev;
1161 const unsigned second_gate_reg = NITIO_Gi_Second_Gate_Reg(counter->counter_index);
1163 if(counter_dev->variant != ni_gpct_variant_m_series) return;
1164 switch(clock_source & NI_GPCT_CLOCK_SRC_SELECT_MASK)
1166 /* Gi_Source_Subselect is zero */
1167 case NI_GPCT_NEXT_GATE_CLOCK_SRC_BITS:
1168 case NI_GPCT_TIMEBASE_3_CLOCK_SRC_BITS:
1169 counter_dev->regs[second_gate_reg] &= ~Gi_Source_Subselect_Bit;
1171 /* Gi_Source_Subselect is one */
1172 case NI_GPCT_ANALOG_TRIGGER_OUT_CLOCK_SRC_BITS:
1173 case NI_GPCT_PXI_STAR_TRIGGER_CLOCK_SRC_BITS:
1174 counter_dev->regs[second_gate_reg] |= Gi_Source_Subselect_Bit;
1176 /* Gi_Source_Subselect doesn't matter */
1181 counter_dev->write_register(counter, counter_dev->regs[second_gate_reg], second_gate_reg);
1184 static int ni_tio_set_clock_src(struct ni_gpct *counter,
1185 lsampl_t clock_source, lsampl_t period_ns)
1187 struct ni_gpct_device *counter_dev = counter->counter_dev;
1188 const unsigned input_select_reg = NITIO_Gi_Input_Select_Reg(counter->counter_index);
1189 static const uint64_t pico_per_nano = 1000;
1191 /*FIXME: validate clock source */
1192 counter_dev->regs[input_select_reg] &= ~Gi_Source_Select_Mask;
1193 switch(counter_dev->variant)
1195 case ni_gpct_variant_660x:
1196 counter_dev->regs[input_select_reg] |= ni_660x_source_select_bits(clock_source);
1198 case ni_gpct_variant_e_series:
1199 case ni_gpct_variant_m_series:
1200 counter_dev->regs[input_select_reg] |= ni_m_series_source_select_bits(clock_source);
1206 if(clock_source & NI_GPCT_INVERT_CLOCK_SRC_BIT)
1207 counter_dev->regs[input_select_reg] |= Gi_Source_Polarity_Bit;
1209 counter_dev->regs[input_select_reg] &= ~Gi_Source_Polarity_Bit;
1210 counter_dev->write_register(counter, counter_dev->regs[input_select_reg], input_select_reg);
1211 ni_tio_set_source_subselect(counter, clock_source);
1212 if(ni_tio_counting_mode_registers_present(counter_dev))
1214 const unsigned counting_mode_reg = NITIO_Gi_Counting_Mode_Reg(counter->counter_index);
1215 const unsigned prescaling_mode = clock_source & NI_GPCT_PRESCALE_MODE_CLOCK_SRC_MASK;
1217 switch(prescaling_mode)
1219 case NI_GPCT_NO_PRESCALE_CLOCK_SRC_BITS:
1220 counter_dev->regs[counting_mode_reg] &= ~(Gi_Prescale_X2_Bit(counter_dev->variant) | Gi_Prescale_X8_Bit(counter_dev->variant));
1222 case NI_GPCT_PRESCALE_X2_CLOCK_SRC_BITS:
1223 counter_dev->regs[counting_mode_reg] |= Gi_Prescale_X2_Bit(counter_dev->variant);
1224 counter_dev->regs[counting_mode_reg] &= ~Gi_Prescale_X8_Bit(counter_dev->variant);
1226 case NI_GPCT_PRESCALE_X8_CLOCK_SRC_BITS:
1227 counter_dev->regs[counting_mode_reg] |= Gi_Prescale_X8_Bit(counter_dev->variant);
1228 counter_dev->regs[counting_mode_reg] &= ~Gi_Prescale_X2_Bit(counter_dev->variant);
1234 counter_dev->write_register(counter, counter_dev->regs[counting_mode_reg], counting_mode_reg);
1236 counter->clock_period_ps = pico_per_nano * period_ns;
1237 ni_tio_set_sync_mode(counter, 0);
1241 static unsigned ni_tio_clock_src_modifiers(const struct ni_gpct *counter)
1243 struct ni_gpct_device *counter_dev = counter->counter_dev;
1244 const unsigned input_select_reg = NITIO_Gi_Input_Select_Reg(counter->counter_index);
1245 const unsigned counting_mode_reg = NITIO_Gi_Counting_Mode_Reg(counter->counter_index);
1248 if(counter_dev->regs[input_select_reg] & Gi_Source_Polarity_Bit)
1249 bits |= NI_GPCT_INVERT_CLOCK_SRC_BIT;
1250 if(counter_dev->regs[counting_mode_reg] & Gi_Prescale_X2_Bit(counter_dev->variant))
1251 bits |= NI_GPCT_PRESCALE_X2_CLOCK_SRC_BITS;
1252 if(counter_dev->regs[counting_mode_reg] & Gi_Prescale_X8_Bit(counter_dev->variant))
1253 bits |= NI_GPCT_PRESCALE_X8_CLOCK_SRC_BITS;
1257 static unsigned ni_m_series_clock_src_select(const struct ni_gpct *counter)
1259 struct ni_gpct_device *counter_dev = counter->counter_dev;
1260 const unsigned input_select_reg = NITIO_Gi_Input_Select_Reg(counter->counter_index);
1261 const unsigned second_gate_reg = NITIO_Gi_Second_Gate_Reg(counter->counter_index);
1262 unsigned clock_source = 0;
1264 const unsigned input_select = (counter_dev->regs[input_select_reg] & Gi_Source_Select_Mask) >> Gi_Source_Select_Shift;
1266 switch(input_select)
1268 case NI_M_Series_Timebase_1_Clock:
1269 clock_source = NI_GPCT_TIMEBASE_1_CLOCK_SRC_BITS;
1271 case NI_M_Series_Timebase_2_Clock:
1272 clock_source = NI_GPCT_TIMEBASE_2_CLOCK_SRC_BITS;
1274 case NI_M_Series_Timebase_3_Clock:
1275 if(counter_dev->regs[second_gate_reg] & Gi_Source_Subselect_Bit)
1276 clock_source = NI_GPCT_ANALOG_TRIGGER_OUT_CLOCK_SRC_BITS;
1278 clock_source = NI_GPCT_TIMEBASE_3_CLOCK_SRC_BITS;
1280 case NI_M_Series_Logic_Low_Clock:
1281 clock_source = NI_GPCT_LOGIC_LOW_CLOCK_SRC_BITS;
1283 case NI_M_Series_Next_Gate_Clock:
1284 if(counter_dev->regs[second_gate_reg] & Gi_Source_Subselect_Bit)
1285 clock_source = NI_GPCT_PXI_STAR_TRIGGER_CLOCK_SRC_BITS;
1287 clock_source = NI_GPCT_NEXT_GATE_CLOCK_SRC_BITS;
1289 case NI_M_Series_PXI10_Clock:
1290 clock_source = NI_GPCT_PXI10_CLOCK_SRC_BITS;
1292 case NI_M_Series_Next_TC_Clock:
1293 clock_source = NI_GPCT_NEXT_TC_CLOCK_SRC_BITS;
1296 for(i = 0; i <= ni_m_series_max_rtsi_channel; ++i)
1298 if(input_select == NI_M_Series_RTSI_Clock(i))
1300 clock_source = NI_GPCT_RTSI_CLOCK_SRC_BITS(i);
1304 if(i <= ni_m_series_max_rtsi_channel) break;
1305 for(i = 0; i <= ni_m_series_max_pfi_channel; ++i)
1307 if(input_select == NI_M_Series_PFI_Clock(i))
1309 clock_source = NI_GPCT_PFI_CLOCK_SRC_BITS(i);
1313 if(i <= ni_m_series_max_pfi_channel) break;
1317 clock_source |= ni_tio_clock_src_modifiers(counter);
1318 return clock_source;
1321 static unsigned ni_660x_clock_src_select(const struct ni_gpct *counter)
1323 const unsigned input_select_reg = NITIO_Gi_Input_Select_Reg(counter->counter_index);
1324 unsigned clock_source = 0;
1326 const unsigned input_select = (counter->counter_dev->regs[input_select_reg] & Gi_Source_Select_Mask) >> Gi_Source_Select_Shift;
1328 switch(input_select)
1330 case NI_660x_Timebase_1_Clock:
1331 clock_source = NI_GPCT_TIMEBASE_1_CLOCK_SRC_BITS;
1333 case NI_660x_Timebase_2_Clock:
1334 clock_source = NI_GPCT_TIMEBASE_2_CLOCK_SRC_BITS;
1336 case NI_660x_Timebase_3_Clock:
1337 clock_source = NI_GPCT_TIMEBASE_3_CLOCK_SRC_BITS;
1339 case NI_660x_Logic_Low_Clock:
1340 clock_source = NI_GPCT_LOGIC_LOW_CLOCK_SRC_BITS;
1342 case NI_660x_Source_Pin_i_Clock:
1343 clock_source = NI_GPCT_SOURCE_PIN_i_CLOCK_SRC_BITS;
1345 case NI_660x_Next_Gate_Clock:
1346 clock_source = NI_GPCT_NEXT_GATE_CLOCK_SRC_BITS;
1348 case NI_660x_Next_TC_Clock:
1349 clock_source = NI_GPCT_NEXT_TC_CLOCK_SRC_BITS;
1352 for(i = 0; i <= ni_660x_max_rtsi_channel; ++i)
1354 if(input_select == NI_660x_RTSI_Clock(i))
1356 clock_source = NI_GPCT_RTSI_CLOCK_SRC_BITS(i);
1360 if(i <= ni_660x_max_rtsi_channel) break;
1361 for(i = 0; i <= ni_660x_max_source_pin; ++i)
1363 if(input_select == NI_660x_Source_Pin_Clock(i))
1365 clock_source = NI_GPCT_SOURCE_PIN_CLOCK_SRC_BITS(i);
1369 if(i <= ni_660x_max_source_pin) break;
1373 clock_source |= ni_tio_clock_src_modifiers(counter);
1374 return clock_source;
1377 static unsigned ni_tio_generic_clock_src_select(const struct ni_gpct *counter)
1379 switch(counter->counter_dev->variant)
1381 case ni_gpct_variant_e_series:
1382 case ni_gpct_variant_m_series:
1383 return ni_m_series_clock_src_select(counter);
1385 case ni_gpct_variant_660x:
1386 return ni_660x_clock_src_select(counter);
1395 static uint64_t ni_tio_clock_period_ps(const struct ni_gpct *counter, unsigned generic_clock_source)
1397 uint64_t clock_period_ps;
1399 switch(generic_clock_source & NI_GPCT_CLOCK_SRC_SELECT_MASK)
1401 case NI_GPCT_TIMEBASE_1_CLOCK_SRC_BITS:
1402 clock_period_ps = 50000;
1404 case NI_GPCT_TIMEBASE_2_CLOCK_SRC_BITS:
1405 clock_period_ps = 10000000;
1407 case NI_GPCT_TIMEBASE_3_CLOCK_SRC_BITS:
1408 clock_period_ps = 12500;
1410 case NI_GPCT_PXI10_CLOCK_SRC_BITS:
1411 clock_period_ps = 100000;
1414 /* clock period is specified by user with prescaling already taken into account. */
1415 return counter->clock_period_ps;
1419 switch(generic_clock_source & NI_GPCT_PRESCALE_MODE_CLOCK_SRC_MASK)
1421 case NI_GPCT_NO_PRESCALE_CLOCK_SRC_BITS:
1423 case NI_GPCT_PRESCALE_X2_CLOCK_SRC_BITS:
1424 clock_period_ps *= 2;
1426 case NI_GPCT_PRESCALE_X8_CLOCK_SRC_BITS:
1427 clock_period_ps *= 8;
1433 return clock_period_ps;
1436 static void ni_tio_get_clock_src(struct ni_gpct *counter,
1437 lsampl_t *clock_source, lsampl_t *period_ns)
1439 static const unsigned pico_per_nano = 1000;
1441 *clock_source = ni_tio_generic_clock_src_select(counter);
1442 temp64 = ni_tio_clock_period_ps(counter, *clock_source);
1443 do_div(temp64, pico_per_nano);
1444 *period_ns = temp64;
1447 static void ni_tio_set_first_gate_modifiers(struct ni_gpct *counter, lsampl_t gate_source)
1449 struct ni_gpct_device *counter_dev = counter->counter_dev;
1450 const unsigned mode_reg = NITIO_Gi_Mode_Reg(counter->counter_index);
1452 if(gate_source & CR_INVERT)
1454 counter_dev->regs[mode_reg] |= Gi_Gate_Polarity_Bit;
1457 counter_dev->regs[mode_reg] &= ~Gi_Gate_Polarity_Bit;
1459 counter_dev->regs[mode_reg] &= ~Gi_Gating_Mode_Mask;
1460 if(gate_source & CR_EDGE)
1462 counter_dev->regs[mode_reg] |= Gi_Rising_Edge_Gating_Bits;
1465 counter_dev->regs[mode_reg] |= Gi_Level_Gating_Bits;
1467 counter_dev->write_register(counter, counter_dev->regs[mode_reg], mode_reg);
1470 static int ni_660x_set_first_gate(struct ni_gpct *counter, lsampl_t gate_source)
1472 struct ni_gpct_device *counter_dev = counter->counter_dev;
1473 const unsigned input_select_reg = NITIO_Gi_Input_Select_Reg(counter->counter_index);
1474 const unsigned selected_gate = CR_CHAN(gate_source);
1475 /* bits of selected_gate that may be meaningful to input select register */
1476 const unsigned selected_gate_mask = 0x1f;
1477 unsigned ni_660x_gate_select;
1480 switch(selected_gate)
1482 case NI_GPCT_NEXT_SOURCE_GATE_SELECT:
1483 ni_660x_gate_select = NI_660x_Next_SRC_Gate_Select;
1485 case NI_GPCT_NEXT_OUT_GATE_SELECT:
1486 case NI_GPCT_LOGIC_LOW_GATE_SELECT:
1487 case NI_GPCT_SOURCE_PIN_i_GATE_SELECT:
1488 case NI_GPCT_GATE_PIN_i_GATE_SELECT:
1489 ni_660x_gate_select = selected_gate & selected_gate_mask;
1492 for(i = 0; i <= ni_660x_max_rtsi_channel; ++i)
1494 if(selected_gate == NI_GPCT_RTSI_GATE_SELECT(i))
1496 ni_660x_gate_select = selected_gate & selected_gate_mask;
1500 if(i <= ni_660x_max_rtsi_channel) break;
1501 for(i = 0; i <= ni_660x_max_gate_pin; ++i)
1503 if(selected_gate == NI_GPCT_GATE_PIN_GATE_SELECT(i))
1505 ni_660x_gate_select = selected_gate & selected_gate_mask;
1509 if(i <= ni_660x_max_gate_pin) break;
1513 counter_dev->regs[input_select_reg] &= ~Gi_Gate_Select_Mask;
1514 counter_dev->regs[input_select_reg] |= Gi_Gate_Select_Bits(ni_660x_gate_select);
1515 counter_dev->write_register(counter, counter_dev->regs[input_select_reg], input_select_reg);
1519 static int ni_m_series_set_first_gate(struct ni_gpct *counter, lsampl_t gate_source)
1521 struct ni_gpct_device *counter_dev = counter->counter_dev;
1522 const unsigned input_select_reg = NITIO_Gi_Input_Select_Reg(counter->counter_index);
1523 const unsigned selected_gate = CR_CHAN(gate_source);
1524 /* bits of selected_gate that may be meaningful to input select register */
1525 const unsigned selected_gate_mask = 0x1f;
1526 unsigned ni_m_series_gate_select;
1529 switch(selected_gate)
1531 case NI_GPCT_TIMESTAMP_MUX_GATE_SELECT:
1532 case NI_GPCT_AI_START2_GATE_SELECT:
1533 case NI_GPCT_PXI_STAR_TRIGGER_GATE_SELECT:
1534 case NI_GPCT_NEXT_OUT_GATE_SELECT:
1535 case NI_GPCT_AI_START1_GATE_SELECT:
1536 case NI_GPCT_NEXT_SOURCE_GATE_SELECT:
1537 case NI_GPCT_ANALOG_TRIGGER_OUT_GATE_SELECT:
1538 case NI_GPCT_LOGIC_LOW_GATE_SELECT:
1539 ni_m_series_gate_select = selected_gate & selected_gate_mask;
1542 for(i = 0; i <= ni_m_series_max_rtsi_channel; ++i)
1544 if(selected_gate == NI_GPCT_RTSI_GATE_SELECT(i))
1546 ni_m_series_gate_select = selected_gate & selected_gate_mask;
1550 if(i <= ni_m_series_max_rtsi_channel) break;
1551 for(i = 0; i <= ni_m_series_max_pfi_channel; ++i)
1553 if(selected_gate == NI_GPCT_PFI_GATE_SELECT(i))
1555 ni_m_series_gate_select = selected_gate & selected_gate_mask;
1559 if(i <= ni_m_series_max_pfi_channel) break;
1563 counter_dev->regs[input_select_reg] &= ~Gi_Gate_Select_Mask;
1564 counter_dev->regs[input_select_reg] |= Gi_Gate_Select_Bits(ni_m_series_gate_select);
1565 counter_dev->write_register(counter, counter_dev->regs[input_select_reg], input_select_reg);
1569 static int ni_660x_set_second_gate(struct ni_gpct *counter, lsampl_t gate_source)
1571 struct ni_gpct_device *counter_dev = counter->counter_dev;
1572 const unsigned second_gate_reg = NITIO_Gi_Second_Gate_Reg(counter->counter_index);
1573 const unsigned selected_second_gate = CR_CHAN(gate_source);
1574 /* bits of second_gate that may be meaningful to second gate register */
1575 static const unsigned selected_second_gate_mask = 0x1f;
1576 unsigned ni_660x_second_gate_select;
1579 switch(selected_second_gate)
1581 case NI_GPCT_SOURCE_PIN_i_GATE_SELECT:
1582 case NI_GPCT_UP_DOWN_PIN_i_GATE_SELECT:
1583 case NI_GPCT_SELECTED_GATE_GATE_SELECT:
1584 case NI_GPCT_NEXT_OUT_GATE_SELECT:
1585 case NI_GPCT_LOGIC_LOW_GATE_SELECT:
1586 ni_660x_second_gate_select = selected_second_gate & selected_second_gate_mask;
1588 case NI_GPCT_NEXT_SOURCE_GATE_SELECT:
1589 ni_660x_second_gate_select = NI_660x_Next_SRC_Second_Gate_Select;
1592 for(i = 0; i <= ni_660x_max_rtsi_channel; ++i)
1594 if(selected_second_gate == NI_GPCT_RTSI_GATE_SELECT(i))
1596 ni_660x_second_gate_select = selected_second_gate & selected_second_gate_mask;
1600 if(i <= ni_660x_max_rtsi_channel) break;
1601 for(i = 0; i <= ni_660x_max_up_down_pin; ++i)
1603 if(selected_second_gate == NI_GPCT_UP_DOWN_PIN_GATE_SELECT(i))
1605 ni_660x_second_gate_select = selected_second_gate & selected_second_gate_mask;
1609 if(i <= ni_660x_max_up_down_pin) break;
1613 counter_dev->regs[second_gate_reg] |= Gi_Second_Gate_Mode_Bit;
1614 counter_dev->regs[second_gate_reg] &= ~Gi_Second_Gate_Select_Mask;
1615 counter_dev->regs[second_gate_reg] |= Gi_Second_Gate_Select_Bits(ni_660x_second_gate_select);
1616 counter_dev->write_register(counter, counter_dev->regs[second_gate_reg], second_gate_reg);
1620 static int ni_m_series_set_second_gate(struct ni_gpct *counter, lsampl_t gate_source)
1622 struct ni_gpct_device *counter_dev = counter->counter_dev;
1623 const unsigned second_gate_reg = NITIO_Gi_Second_Gate_Reg(counter->counter_index);
1624 const unsigned selected_second_gate = CR_CHAN(gate_source);
1625 /* bits of second_gate that may be meaningful to second gate register */
1626 static const unsigned selected_second_gate_mask = 0x1f;
1627 unsigned ni_m_series_second_gate_select;
1629 /* FIXME: We don't know what the m-series second gate codes are, so we'll just pass
1630 the bits through for now. */
1631 switch(selected_second_gate)
1634 ni_m_series_second_gate_select = selected_second_gate & selected_second_gate_mask;
1637 counter_dev->regs[second_gate_reg] |= Gi_Second_Gate_Mode_Bit;
1638 counter_dev->regs[second_gate_reg] &= ~Gi_Second_Gate_Select_Mask;
1639 counter_dev->regs[second_gate_reg] |= Gi_Second_Gate_Select_Bits(ni_m_series_second_gate_select);
1640 counter_dev->write_register(counter, counter_dev->regs[second_gate_reg], second_gate_reg);
1644 static int ni_tio_set_gate_src(struct ni_gpct *counter, unsigned gate_index, lsampl_t gate_source)
1646 struct ni_gpct_device *counter_dev = counter->counter_dev;
1647 const unsigned mode_reg = NITIO_Gi_Mode_Reg(counter->counter_index);
1648 const unsigned second_gate_reg = NITIO_Gi_Second_Gate_Reg(counter->counter_index);
1653 if(CR_CHAN(gate_source) == NI_GPCT_DISABLED_GATE_SELECT)
1655 counter_dev->regs[mode_reg] &= ~Gi_Gating_Mode_Mask;
1656 counter_dev->regs[mode_reg] |= Gi_Gating_Disabled_Bits;
1657 counter_dev->write_register(counter, counter_dev->regs[mode_reg], mode_reg);
1660 ni_tio_set_first_gate_modifiers(counter, gate_source);
1661 switch(counter_dev->variant)
1663 case ni_gpct_variant_e_series:
1664 case ni_gpct_variant_m_series:
1665 return ni_m_series_set_first_gate(counter, gate_source);
1667 case ni_gpct_variant_660x:
1668 return ni_660x_set_first_gate(counter, gate_source);
1676 if(ni_tio_second_gate_registers_present(counter_dev) == 0) return -EINVAL;
1677 if(CR_CHAN(gate_source) == NI_GPCT_DISABLED_GATE_SELECT)
1679 counter_dev->regs[second_gate_reg] &= ~Gi_Second_Gate_Mode_Bit;
1680 counter_dev->write_register(counter, counter_dev->regs[second_gate_reg], second_gate_reg);
1683 if(gate_source & CR_INVERT)
1685 counter_dev->regs[second_gate_reg] |= Gi_Second_Gate_Polarity_Bit;
1688 counter_dev->regs[second_gate_reg] &= ~Gi_Second_Gate_Polarity_Bit;
1690 switch(counter_dev->variant)
1692 case ni_gpct_variant_m_series:
1693 return ni_m_series_set_second_gate(counter, gate_source);
1695 case ni_gpct_variant_660x:
1696 return ni_660x_set_second_gate(counter, gate_source);
1710 static int ni_tio_set_other_src(struct ni_gpct *counter, unsigned index, lsampl_t source)
1712 struct ni_gpct_device *counter_dev = counter->counter_dev;
1714 if(counter_dev->variant == ni_gpct_variant_m_series)
1716 unsigned int abz_reg, shift, mask;
1718 abz_reg = NITIO_Gi_ABZ_Reg(counter->counter_index);
1720 case NI_GPCT_SOURCE_ENCODER_A:
1723 case NI_GPCT_SOURCE_ENCODER_B:
1726 case NI_GPCT_SOURCE_ENCODER_Z:
1733 mask = 0x1f << shift;
1739 counter_dev->regs[abz_reg] &= ~mask;
1740 counter_dev->regs[abz_reg] |= (source << shift) & mask;
1741 counter_dev->write_register(counter, counter_dev->regs[abz_reg], abz_reg);
1742 // rt_printk("%s %x %d %d\n", __FUNCTION__, counter_dev->regs[abz_reg], index, source);
1748 static unsigned ni_660x_first_gate_to_generic_gate_source(unsigned ni_660x_gate_select)
1752 switch(ni_660x_gate_select)
1754 case NI_660x_Source_Pin_i_Gate_Select:
1755 return NI_GPCT_SOURCE_PIN_i_GATE_SELECT;
1757 case NI_660x_Gate_Pin_i_Gate_Select:
1758 return NI_GPCT_GATE_PIN_i_GATE_SELECT;
1760 case NI_660x_Next_SRC_Gate_Select:
1761 return NI_GPCT_NEXT_SOURCE_GATE_SELECT;
1763 case NI_660x_Next_Out_Gate_Select:
1764 return NI_GPCT_NEXT_OUT_GATE_SELECT;
1766 case NI_660x_Logic_Low_Gate_Select:
1767 return NI_GPCT_LOGIC_LOW_GATE_SELECT;
1770 for(i = 0; i <= ni_660x_max_rtsi_channel; ++i)
1772 if(ni_660x_gate_select == NI_660x_RTSI_Gate_Select(i))
1774 return NI_GPCT_RTSI_GATE_SELECT(i);
1778 if(i <= ni_660x_max_rtsi_channel) break;
1779 for(i = 0; i <= ni_660x_max_gate_pin; ++i)
1781 if(ni_660x_gate_select == NI_660x_Gate_Pin_Gate_Select(i))
1783 return NI_GPCT_GATE_PIN_GATE_SELECT(i);
1787 if(i <= ni_660x_max_gate_pin) break;
1794 static unsigned ni_m_series_first_gate_to_generic_gate_source(unsigned ni_m_series_gate_select)
1798 switch(ni_m_series_gate_select)
1800 case NI_M_Series_Timestamp_Mux_Gate_Select:
1801 return NI_GPCT_TIMESTAMP_MUX_GATE_SELECT;
1803 case NI_M_Series_AI_START2_Gate_Select:
1804 return NI_GPCT_AI_START2_GATE_SELECT;
1806 case NI_M_Series_PXI_Star_Trigger_Gate_Select:
1807 return NI_GPCT_PXI_STAR_TRIGGER_GATE_SELECT;
1809 case NI_M_Series_Next_Out_Gate_Select:
1810 return NI_GPCT_NEXT_OUT_GATE_SELECT;
1812 case NI_M_Series_AI_START1_Gate_Select:
1813 return NI_GPCT_AI_START1_GATE_SELECT;
1815 case NI_M_Series_Next_SRC_Gate_Select:
1816 return NI_GPCT_NEXT_SOURCE_GATE_SELECT;
1818 case NI_M_Series_Analog_Trigger_Out_Gate_Select:
1819 return NI_GPCT_ANALOG_TRIGGER_OUT_GATE_SELECT;
1821 case NI_M_Series_Logic_Low_Gate_Select:
1822 return NI_GPCT_LOGIC_LOW_GATE_SELECT;
1825 for(i = 0; i <= ni_m_series_max_rtsi_channel; ++i)
1827 if(ni_m_series_gate_select == NI_M_Series_RTSI_Gate_Select(i))
1829 return NI_GPCT_RTSI_GATE_SELECT(i);
1833 if(i <= ni_m_series_max_rtsi_channel) break;
1834 for(i = 0; i <= ni_m_series_max_pfi_channel; ++i)
1836 if(ni_m_series_gate_select == NI_M_Series_PFI_Gate_Select(i))
1838 return NI_GPCT_PFI_GATE_SELECT(i);
1842 if(i <= ni_m_series_max_pfi_channel) break;
1849 static unsigned ni_660x_second_gate_to_generic_gate_source(unsigned ni_660x_gate_select)
1853 switch(ni_660x_gate_select)
1855 case NI_660x_Source_Pin_i_Second_Gate_Select:
1856 return NI_GPCT_SOURCE_PIN_i_GATE_SELECT;
1858 case NI_660x_Up_Down_Pin_i_Second_Gate_Select:
1859 return NI_GPCT_UP_DOWN_PIN_i_GATE_SELECT;
1861 case NI_660x_Next_SRC_Second_Gate_Select:
1862 return NI_GPCT_NEXT_SOURCE_GATE_SELECT;
1864 case NI_660x_Next_Out_Second_Gate_Select:
1865 return NI_GPCT_NEXT_OUT_GATE_SELECT;
1867 case NI_660x_Selected_Gate_Second_Gate_Select:
1868 return NI_GPCT_SELECTED_GATE_GATE_SELECT;
1870 case NI_660x_Logic_Low_Second_Gate_Select:
1871 return NI_GPCT_LOGIC_LOW_GATE_SELECT;
1874 for(i = 0; i <= ni_660x_max_rtsi_channel; ++i)
1876 if(ni_660x_gate_select == NI_660x_RTSI_Second_Gate_Select(i))
1878 return NI_GPCT_RTSI_GATE_SELECT(i);
1882 if(i <= ni_660x_max_rtsi_channel) break;
1883 for(i = 0; i <= ni_660x_max_up_down_pin; ++i)
1885 if(ni_660x_gate_select == NI_660x_Up_Down_Pin_Second_Gate_Select(i))
1887 return NI_GPCT_UP_DOWN_PIN_GATE_SELECT(i);
1891 if(i <= ni_660x_max_up_down_pin) break;
1898 static unsigned ni_m_series_second_gate_to_generic_gate_source(unsigned ni_m_series_gate_select)
1900 /*FIXME: the second gate sources for the m series are undocumented, so we just return
1901 * the raw bits for now. */
1902 switch(ni_m_series_gate_select)
1905 return ni_m_series_gate_select;
1911 static int ni_tio_get_gate_src(struct ni_gpct *counter, unsigned gate_index, lsampl_t *gate_source)
1913 struct ni_gpct_device *counter_dev = counter->counter_dev;
1914 const unsigned input_select_reg = NITIO_Gi_Input_Select_Reg(counter->counter_index);
1915 const unsigned mode_reg = NITIO_Gi_Mode_Reg(counter->counter_index);
1916 const unsigned second_gate_reg = NITIO_Gi_Second_Gate_Reg(counter->counter_index);
1917 unsigned gate_select_bits;
1922 if((counter_dev->regs[mode_reg] & Gi_Gating_Mode_Mask) == Gi_Gating_Disabled_Bits)
1924 *gate_source = NI_GPCT_DISABLED_GATE_SELECT;
1928 gate_select_bits = (counter_dev->regs[input_select_reg] & Gi_Gate_Select_Mask) >> Gi_Gate_Select_Shift;
1930 switch(counter_dev->variant)
1932 case ni_gpct_variant_e_series:
1933 case ni_gpct_variant_m_series:
1934 *gate_source = ni_m_series_first_gate_to_generic_gate_source(gate_select_bits);
1936 case ni_gpct_variant_660x:
1937 *gate_source = ni_660x_first_gate_to_generic_gate_source(gate_select_bits);
1943 if(counter_dev->regs[mode_reg] & Gi_Gate_Polarity_Bit)
1945 *gate_source |= CR_INVERT;
1947 if((counter_dev->regs[mode_reg] & Gi_Gating_Mode_Mask) != Gi_Level_Gating_Bits)
1949 *gate_source |= CR_EDGE;
1953 if((counter_dev->regs[mode_reg] & Gi_Gating_Mode_Mask) == Gi_Gating_Disabled_Bits ||
1954 (counter_dev->regs[second_gate_reg] & Gi_Second_Gate_Mode_Bit) == 0)
1956 *gate_source = NI_GPCT_DISABLED_GATE_SELECT;
1960 gate_select_bits = (counter_dev->regs[second_gate_reg] & Gi_Second_Gate_Select_Mask) >> Gi_Second_Gate_Select_Shift;
1962 switch(counter_dev->variant)
1964 case ni_gpct_variant_e_series:
1965 case ni_gpct_variant_m_series:
1966 *gate_source = ni_m_series_second_gate_to_generic_gate_source(gate_select_bits);
1968 case ni_gpct_variant_660x:
1969 *gate_source = ni_660x_second_gate_to_generic_gate_source(gate_select_bits);
1975 if(counter_dev->regs[second_gate_reg] & Gi_Second_Gate_Polarity_Bit)
1977 *gate_source |= CR_INVERT;
1979 /* second gate can't have edge/level mode set independently */
1980 if((counter_dev->regs[mode_reg] & Gi_Gating_Mode_Mask) != Gi_Level_Gating_Bits)
1982 *gate_source |= CR_EDGE;
1992 int ni_tio_insn_config(struct ni_gpct *counter,
1993 comedi_insn *insn, lsampl_t *data)
1997 case INSN_CONFIG_SET_COUNTER_MODE:
1998 return ni_tio_set_counter_mode(counter, data[1]);
2000 case INSN_CONFIG_ARM:
2001 return ni_tio_arm(counter, 1, data[1]);
2003 case INSN_CONFIG_DISARM:
2004 ni_tio_arm(counter, 0, 0);
2007 case INSN_CONFIG_GET_COUNTER_STATUS:
2008 data[1] = ni_tio_counter_status(counter);
2009 data[2] = counter_status_mask;
2012 case INSN_CONFIG_SET_CLOCK_SRC:
2013 return ni_tio_set_clock_src(counter, data[1], data[2]);
2015 case INSN_CONFIG_GET_CLOCK_SRC:
2016 ni_tio_get_clock_src(counter, &data[1], &data[2]);
2019 case INSN_CONFIG_SET_GATE_SRC:
2020 return ni_tio_set_gate_src(counter, data[1], data[2]);
2022 case INSN_CONFIG_GET_GATE_SRC:
2023 return ni_tio_get_gate_src(counter, data[1], &data[2]);
2025 case INSN_CONFIG_SET_OTHER_SRC:
2026 return ni_tio_set_other_src(counter, data[1], data[2]);
2028 case INSN_CONFIG_RESET:
2029 ni_tio_reset_count_and_disarm(counter);
2038 int ni_tio_rinsn(struct ni_gpct *counter,
2039 comedi_insn *insn, lsampl_t *data)
2041 struct ni_gpct_device *counter_dev = counter->counter_dev;
2042 const unsigned command_reg = NITIO_Gi_Command_Reg(counter->counter_index);
2043 const unsigned channel = CR_CHAN(insn->chanspec);
2044 unsigned first_read;
2045 unsigned second_read;
2046 unsigned correct_read;
2048 if(insn->n < 1) return 0;
2052 counter_dev->regs[command_reg] &= ~Gi_Save_Trace_Bit;
2053 counter_dev->write_register(counter, counter_dev->regs[command_reg], command_reg);
2054 counter_dev->write_register(counter, counter_dev->regs[command_reg] | Gi_Save_Trace_Bit,
2056 /* The count doesn't get latched until the next clock edge, so it is possible the count
2057 may change (once) while we are reading. Since the read of the SW_Save_Reg isn't
2058 atomic (apparently even when it's a 32 bit register according to 660x docs),
2059 we need to read twice and make sure the reading hasn't changed. If it has,
2060 a third read will be correct since the count value will definitely have latched by then. */
2061 first_read = counter_dev->read_register(counter, NITIO_Gi_SW_Save_Reg(counter->counter_index));
2062 second_read = counter_dev->read_register(counter, NITIO_Gi_SW_Save_Reg(counter->counter_index));
2063 if(first_read != second_read)
2064 correct_read = counter_dev->read_register(counter, NITIO_Gi_SW_Save_Reg(counter->counter_index));
2066 correct_read = first_read;
2067 data[0] = correct_read;
2071 data[0] = counter_dev->regs[NITIO_Gi_LoadA_Reg(counter->counter_index)];
2074 data[0] = counter_dev->regs[NITIO_Gi_LoadB_Reg(counter->counter_index)];
2080 static unsigned ni_tio_next_load_register(struct ni_gpct *counter)
2082 const unsigned bits = counter->counter_dev->read_register(counter, NITIO_Gxx_Status_Reg(counter->counter_index));
2084 if(bits & Gi_Next_Load_Source_Bit(counter->counter_index))
2086 return NITIO_Gi_LoadB_Reg(counter->counter_index);
2089 return NITIO_Gi_LoadA_Reg(counter->counter_index);
2093 int ni_tio_winsn(struct ni_gpct *counter,
2097 struct ni_gpct_device *counter_dev = counter->counter_dev;
2098 const unsigned channel = CR_CHAN(insn->chanspec);
2099 const unsigned command_reg = NITIO_Gi_Command_Reg(counter->counter_index);
2102 if(insn->n < 1) return 0;
2106 /* Unsafe if counter is armed. Should probably check status and return -EBUSY if armed. */
2107 /* Don't disturb load source select, just use whichever load register is already selected. */
2108 load_reg = ni_tio_next_load_register(counter);
2109 counter_dev->write_register(counter, data[0], load_reg);
2110 counter_dev->write_register(counter, counter_dev->regs[command_reg] | Gi_Load_Bit, command_reg);
2111 /* restore state of load reg to whatever the user set last set it to */
2112 counter_dev->write_register(counter, counter_dev->regs[load_reg], load_reg);
2115 counter_dev->regs[NITIO_Gi_LoadA_Reg(counter->counter_index)] = data[0];
2116 counter_dev->write_register(counter, data[0], NITIO_Gi_LoadA_Reg(counter->counter_index));
2119 counter_dev->regs[NITIO_Gi_LoadB_Reg(counter->counter_index)] = data[0];
2120 counter_dev->write_register(counter, data[0], NITIO_Gi_LoadB_Reg(counter->counter_index));
2129 static void ni_tio_configure_dma(struct ni_gpct *counter, short enable, short read_not_write)
2131 struct ni_gpct_device *counter_dev = counter->counter_dev;
2133 switch(counter_dev->variant)
2135 case ni_gpct_variant_e_series:
2137 const unsigned input_select_reg = NITIO_Gi_Input_Select_Reg(counter->counter_index);
2142 counter_dev->regs[input_select_reg] |= Gi_Read_Acknowledges_Irq;
2143 counter_dev->regs[input_select_reg] &= ~Gi_Write_Acknowledges_Irq;
2146 counter_dev->regs[input_select_reg] &= ~Gi_Read_Acknowledges_Irq;
2147 counter_dev->regs[input_select_reg] |= Gi_Write_Acknowledges_Irq;
2151 counter_dev->regs[input_select_reg] &= ~(Gi_Read_Acknowledges_Irq | Gi_Write_Acknowledges_Irq);
2153 counter_dev->write_register(counter, counter_dev->regs[input_select_reg], input_select_reg);
2156 case ni_gpct_variant_m_series:
2157 case ni_gpct_variant_660x:
2159 const unsigned gi_dma_config_reg = NITIO_Gi_DMA_Config_Reg(counter->counter_index);
2163 counter_dev->regs[gi_dma_config_reg] |= Gi_DMA_Enable_Bit;
2164 counter_dev->regs[gi_dma_config_reg] |= Gi_DMA_Int_Bit;
2167 counter_dev->regs[gi_dma_config_reg] &= ~Gi_DMA_Enable_Bit;
2168 counter_dev->regs[gi_dma_config_reg] &= ~Gi_DMA_Int_Bit;
2172 counter_dev->regs[gi_dma_config_reg] &= ~Gi_DMA_Write_Bit;
2175 counter_dev->regs[gi_dma_config_reg] |= Gi_DMA_Write_Bit;
2177 counter_dev->write_register(counter, counter_dev->regs[gi_dma_config_reg], gi_dma_config_reg);
2183 static int ni_tio_input_cmd(struct ni_gpct *counter, comedi_async *async)
2185 struct ni_gpct_device *counter_dev = counter->counter_dev;
2186 comedi_cmd *cmd = &async->cmd;
2187 const unsigned command_reg = NITIO_Gi_Command_Reg(counter->counter_index);
2189 /* write alloc the entire buffer */
2190 comedi_buf_write_alloc(async, async->prealloc_bufsz);
2191 counter->mite_chan->dir = COMEDI_INPUT;
2192 switch(counter_dev->variant)
2194 case ni_gpct_variant_m_series:
2195 case ni_gpct_variant_660x:
2196 mite_prep_dma(counter->mite_chan, 32, 32);
2198 case ni_gpct_variant_e_series:
2199 mite_prep_dma(counter->mite_chan, 16, 32);
2205 counter_dev->regs[command_reg] &= ~Gi_Save_Trace_Bit;
2206 counter_dev->write_register(counter, counter_dev->regs[command_reg], command_reg);
2207 ni_tio_configure_dma(counter, 1, 1);
2208 mite_dma_arm(counter->mite_chan);
2209 return ni_tio_arm(counter, 1, NI_GPCT_ARM_IMMEDIATE);
2212 static int ni_tio_output_cmd(struct ni_gpct *counter, comedi_async *async)
2214 rt_printk("ni_tio: output commands not yet implemented.\n");
2217 counter->mite_chan->dir = COMEDI_OUTPUT;
2218 mite_prep_dma(counter->mite_chan, 32, 32);
2219 ni_tio_configure_dma(counter, 1, 0);
2220 mite_dma_arm(counter->mite_chan);
2221 return ni_tio_arm(counter, 1, NI_GPCT_ARM_IMMEDIATE);
2224 int ni_tio_cmd(struct ni_gpct *counter, comedi_async *async)
2226 comedi_cmd *cmd = &async->cmd;
2229 if(counter->mite_chan == NULL)
2231 rt_printk("ni_tio: commands only supported with DMA. Interrupt-driven commands not yet implemented.\n");
2235 if(cmd->flags & CMDF_WRITE)
2237 retval = ni_tio_output_cmd(counter, async);
2240 retval = ni_tio_input_cmd(counter, async);
2246 int ni_tio_cmdtest(struct ni_gpct *counter)
2251 int ni_tio_cancel(struct ni_gpct *counter)
2253 ni_tio_arm(counter, 0, 0);
2254 if(counter->mite_chan)
2256 mite_dma_disarm(counter->mite_chan);
2258 ni_tio_configure_dma(counter, 0, 0);
2262 EXPORT_SYMBOL_GPL(ni_tio_rinsn);
2263 EXPORT_SYMBOL_GPL(ni_tio_winsn);
2264 EXPORT_SYMBOL_GPL(ni_tio_cmd);
2265 EXPORT_SYMBOL_GPL(ni_tio_cmdtest);
2266 EXPORT_SYMBOL_GPL(ni_tio_cancel);
2267 EXPORT_SYMBOL_GPL(ni_tio_insn_config);
2268 EXPORT_SYMBOL_GPL(ni_tio_init_counter);
2269 EXPORT_SYMBOL_GPL(ni_gpct_device_construct);
2270 EXPORT_SYMBOL_GPL(ni_gpct_device_destroy);