54 unsigned generic_clock_source);
55 static unsigned ni_tio_generic_clock_src_select(
const struct ni_gpct *
counter);
155 static const unsigned ni_660x_max_rtsi_channel = 6;
156 static inline unsigned NI_660x_RTSI_Clock(
unsigned n)
158 BUG_ON(n > ni_660x_max_rtsi_channel);
162 static const unsigned ni_660x_max_source_pin = 7;
163 static inline unsigned NI_660x_Source_Pin_Clock(
unsigned n)
165 BUG_ON(n > ni_660x_max_source_pin);
181 static const unsigned ni_m_series_max_pfi_channel = 15;
182 static inline unsigned NI_M_Series_PFI_Clock(
unsigned n)
184 BUG_ON(n > ni_m_series_max_pfi_channel);
191 static const unsigned ni_m_series_max_rtsi_channel = 7;
192 static inline unsigned NI_M_Series_RTSI_Clock(
unsigned n)
194 BUG_ON(n > ni_m_series_max_rtsi_channel);
208 static const unsigned ni_660x_max_gate_pin = 7;
209 static inline unsigned NI_660x_Gate_Pin_Gate_Select(
unsigned n)
211 BUG_ON(n > ni_660x_max_gate_pin);
215 static inline unsigned NI_660x_RTSI_Gate_Select(
unsigned n)
217 BUG_ON(n > ni_660x_max_rtsi_channel);
231 static inline unsigned NI_M_Series_RTSI_Gate_Select(
unsigned n)
233 BUG_ON(n > ni_m_series_max_rtsi_channel);
239 static inline unsigned NI_M_Series_PFI_Gate_Select(
unsigned n)
241 BUG_ON(n > ni_m_series_max_pfi_channel);
247 static inline unsigned Gi_Source_Select_Bits(
unsigned source)
252 static inline unsigned Gi_Gate_Select_Bits(
unsigned gate_select)
265 static const unsigned ni_660x_max_up_down_pin = 7;
266 static inline unsigned NI_660x_Up_Down_Pin_Second_Gate_Select(
unsigned n)
268 BUG_ON(n > ni_660x_max_up_down_pin);
272 static inline unsigned NI_660x_RTSI_Second_Gate_Select(
unsigned n)
274 BUG_ON(n > ni_660x_max_rtsi_channel);
278 static const unsigned int counter_status_mask =
281 static int __init ni_tio_init_module(
void)
288 static void __exit ni_tio_cleanup_module(
void)
314 if (counter_dev ==
NULL)
321 BUG_ON(num_counters == 0);
329 counter_dev->
counters[
i].counter_dev = counter_dev;
346 static int ni_tio_second_gate_registers_present(
const struct ni_gpct_device
349 switch (counter_dev->
variant) {
364 static void ni_tio_reset_count_and_disarm(
struct ni_gpct *counter)
374 ni_tio_reset_count_and_disarm(counter);
382 ni_tio_set_bits(counter, NITIO_Gi_Command_Reg(counter->
counter_index),
384 ni_tio_set_bits(counter, NITIO_Gi_Mode_Reg(counter->
counter_index), ~0,
396 ni_tio_set_bits(counter,
399 if (ni_tio_counting_mode_registers_present(counter_dev)) {
400 ni_tio_set_bits(counter,
401 NITIO_Gi_Counting_Mode_Reg(counter->
405 if (ni_tio_second_gate_registers_present(counter_dev)) {
411 regs[NITIO_Gi_Second_Gate_Reg
413 NITIO_Gi_Second_Gate_Reg(counter->
416 ni_tio_set_bits(counter,
419 ni_tio_set_bits(counter,
425 static unsigned int ni_tio_counter_status(
struct ni_gpct *counter)
429 NITIO_Gxx_Status_Reg(counter->
439 static void ni_tio_set_sync_mode(
struct ni_gpct *counter,
int force_alt_sync)
442 const unsigned counting_mode_reg =
444 static const uint64_t min_normal_sync_period_ps = 25000;
445 const uint64_t clock_period_ps = ni_tio_clock_period_ps(counter,
446 ni_tio_generic_clock_src_select
449 if (ni_tio_counting_mode_registers_present(counter_dev) == 0)
465 if (force_alt_sync ||
466 (clock_period_ps && clock_period_ps < min_normal_sync_period_ps)) {
467 ni_tio_set_bits(counter, counting_mode_reg,
468 Gi_Alternate_Sync_Bit(counter_dev->
variant),
469 Gi_Alternate_Sync_Bit(counter_dev->
variant));
471 ni_tio_set_bits(counter, counting_mode_reg,
472 Gi_Alternate_Sync_Bit(counter_dev->
variant),
477 static int ni_tio_set_counter_mode(
struct ni_gpct *counter,
unsigned mode)
480 unsigned mode_reg_mask;
481 unsigned mode_reg_values;
482 unsigned input_select_bits = 0;
484 static const unsigned mode_reg_direct_mask =
491 mode_reg_values = mode & mode_reg_direct_mask;
506 ni_tio_set_bits(counter, NITIO_Gi_Mode_Reg(counter->
counter_index),
507 mode_reg_mask, mode_reg_values);
509 if (ni_tio_counting_mode_registers_present(counter_dev)) {
510 unsigned counting_mode_bits = 0;
511 counting_mode_bits |=
513 Gi_Counting_Mode_Mask;
514 counting_mode_bits |=
519 ni_tio_set_bits(counter,
520 NITIO_Gi_Counting_Mode_Reg(counter->
524 ni_tio_set_sync_mode(counter, 0);
527 ni_tio_set_bits(counter, NITIO_Gi_Command_Reg(counter->
counter_index),
536 ni_tio_set_bits(counter,
548 unsigned command_transient_bits = 0;
551 switch (start_trigger) {
561 if (ni_tio_counting_mode_registers_present(counter_dev)) {
562 unsigned counting_mode_bits = 0;
564 switch (start_trigger) {
571 unsigned hw_arm_select_bits =
574 Gi_HW_Arm_Select_Mask
577 counting_mode_bits |=
585 ni_tio_set_bits(counter,
586 NITIO_Gi_Counting_Mode_Reg
588 Gi_HW_Arm_Select_Mask
596 ni_tio_set_bits_transient(counter,
598 0, 0, command_transient_bits);
603 static unsigned ni_660x_source_select_bits(
unsigned int clock_source)
605 unsigned ni_660x_clock;
607 const unsigned clock_select_bits =
610 switch (clock_select_bits) {
633 for (i = 0; i <= ni_660x_max_rtsi_channel; ++
i) {
634 if (clock_select_bits == NI_GPCT_RTSI_CLOCK_SRC_BITS(i)) {
635 ni_660x_clock = NI_660x_RTSI_Clock(i);
639 if (i <= ni_660x_max_rtsi_channel)
641 for (i = 0; i <= ni_660x_max_source_pin; ++
i) {
642 if (clock_select_bits ==
643 NI_GPCT_SOURCE_PIN_CLOCK_SRC_BITS(i)) {
644 ni_660x_clock = NI_660x_Source_Pin_Clock(i);
648 if (i <= ni_660x_max_source_pin)
654 return Gi_Source_Select_Bits(ni_660x_clock);
657 static unsigned ni_m_series_source_select_bits(
unsigned int clock_source)
659 unsigned ni_m_series_clock;
661 const unsigned clock_select_bits =
663 switch (clock_select_bits) {
692 for (i = 0; i <= ni_m_series_max_rtsi_channel; ++
i) {
693 if (clock_select_bits == NI_GPCT_RTSI_CLOCK_SRC_BITS(i)) {
694 ni_m_series_clock = NI_M_Series_RTSI_Clock(i);
698 if (i <= ni_m_series_max_rtsi_channel)
700 for (i = 0; i <= ni_m_series_max_pfi_channel; ++
i) {
701 if (clock_select_bits == NI_GPCT_PFI_CLOCK_SRC_BITS(i)) {
702 ni_m_series_clock = NI_M_Series_PFI_Clock(i);
706 if (i <= ni_m_series_max_pfi_channel)
709 (
unsigned long)clock_source);
711 ni_m_series_clock = 0;
714 return Gi_Source_Select_Bits(ni_m_series_clock);
717 static void ni_tio_set_source_subselect(
struct ni_gpct *counter,
718 unsigned int clock_source)
721 const unsigned second_gate_reg =
726 switch (clock_source & NI_GPCT_CLOCK_SRC_SELECT_MASK) {
746 static int ni_tio_set_clock_src(
struct ni_gpct *counter,
747 unsigned int clock_source,
748 unsigned int period_ns)
751 unsigned input_select_bits = 0;
752 static const uint64_t pico_per_nano = 1000;
755 switch (counter_dev->
variant) {
757 input_select_bits |= ni_660x_source_select_bits(clock_source);
762 ni_m_series_source_select_bits(clock_source);
770 ni_tio_set_bits(counter,
774 ni_tio_set_source_subselect(counter, clock_source);
775 if (ni_tio_counting_mode_registers_present(counter_dev)) {
776 const unsigned prescaling_mode =
778 unsigned counting_mode_bits = 0;
780 switch (prescaling_mode) {
784 counting_mode_bits |=
785 Gi_Prescale_X2_Bit(counter_dev->
variant);
788 counting_mode_bits |=
789 Gi_Prescale_X8_Bit(counter_dev->
variant);
795 ni_tio_set_bits(counter,
796 NITIO_Gi_Counting_Mode_Reg(counter->
798 Gi_Prescale_X2_Bit(counter_dev->
variant) |
799 Gi_Prescale_X8_Bit(counter_dev->
variant),
803 ni_tio_set_sync_mode(counter, 0);
807 static unsigned ni_tio_clock_src_modifiers(
const struct ni_gpct *counter)
810 const unsigned counting_mode_bits = ni_tio_get_soft_copy(counter,
811 NITIO_Gi_Counting_Mode_Reg
816 if (ni_tio_get_soft_copy(counter,
817 NITIO_Gi_Input_Select_Reg
821 if (counting_mode_bits & Gi_Prescale_X2_Bit(counter_dev->
variant))
823 if (counting_mode_bits & Gi_Prescale_X8_Bit(counter_dev->
variant))
828 static unsigned ni_m_series_clock_src_select(
const struct ni_gpct *counter)
831 const unsigned second_gate_reg =
833 unsigned clock_source = 0;
835 const unsigned input_select = (ni_tio_get_soft_copy(counter,
836 NITIO_Gi_Input_Select_Reg
841 switch (input_select) {
849 if (counter_dev->
regs[second_gate_reg] &
860 if (counter_dev->
regs[second_gate_reg] &
861 Gi_Source_Subselect_Bit)
873 for (i = 0; i <= ni_m_series_max_rtsi_channel; ++
i) {
874 if (input_select == NI_M_Series_RTSI_Clock(i)) {
875 clock_source = NI_GPCT_RTSI_CLOCK_SRC_BITS(i);
879 if (i <= ni_m_series_max_rtsi_channel)
881 for (i = 0; i <= ni_m_series_max_pfi_channel; ++
i) {
882 if (input_select == NI_M_Series_PFI_Clock(i)) {
883 clock_source = NI_GPCT_PFI_CLOCK_SRC_BITS(i);
887 if (i <= ni_m_series_max_pfi_channel)
892 clock_source |= ni_tio_clock_src_modifiers(counter);
896 static unsigned ni_660x_clock_src_select(
const struct ni_gpct *counter)
898 unsigned clock_source = 0;
900 const unsigned input_select = (ni_tio_get_soft_copy(counter,
901 NITIO_Gi_Input_Select_Reg
906 switch (input_select) {
929 for (i = 0; i <= ni_660x_max_rtsi_channel; ++
i) {
930 if (input_select == NI_660x_RTSI_Clock(i)) {
931 clock_source = NI_GPCT_RTSI_CLOCK_SRC_BITS(i);
935 if (i <= ni_660x_max_rtsi_channel)
937 for (i = 0; i <= ni_660x_max_source_pin; ++
i) {
938 if (input_select == NI_660x_Source_Pin_Clock(i)) {
940 NI_GPCT_SOURCE_PIN_CLOCK_SRC_BITS(i);
944 if (i <= ni_660x_max_source_pin)
949 clock_source |= ni_tio_clock_src_modifiers(counter);
953 static unsigned ni_tio_generic_clock_src_select(
const struct ni_gpct *counter)
958 return ni_m_series_clock_src_select(counter);
961 return ni_660x_clock_src_select(counter);
971 unsigned generic_clock_source)
975 switch (generic_clock_source & NI_GPCT_CLOCK_SRC_SELECT_MASK) {
977 clock_period_ps = 50000;
980 clock_period_ps = 10000000;
983 clock_period_ps = 12500;
986 clock_period_ps = 100000;
994 switch (generic_clock_source & NI_GPCT_PRESCALE_MODE_CLOCK_SRC_MASK) {
998 clock_period_ps *= 2;
1001 clock_period_ps *= 8;
1007 return clock_period_ps;
1010 static void ni_tio_get_clock_src(
struct ni_gpct *counter,
1011 unsigned int *clock_source,
1012 unsigned int *period_ns)
1014 static const unsigned pico_per_nano = 1000;
1016 *clock_source = ni_tio_generic_clock_src_select(counter);
1017 temp64 = ni_tio_clock_period_ps(counter, *clock_source);
1018 do_div(temp64, pico_per_nano);
1019 *period_ns = temp64;
1022 static void ni_tio_set_first_gate_modifiers(
struct ni_gpct *counter,
1023 unsigned int gate_source)
1026 unsigned mode_values = 0;
1034 ni_tio_set_bits(counter, NITIO_Gi_Mode_Reg(counter->
counter_index),
1035 mode_mask, mode_values);
1038 static int ni_660x_set_first_gate(
struct ni_gpct *counter,
1039 unsigned int gate_source)
1041 const unsigned selected_gate =
CR_CHAN(gate_source);
1043 const unsigned selected_gate_mask = 0x1f;
1047 switch (selected_gate) {
1055 ni_660x_gate_select = selected_gate & selected_gate_mask;
1058 for (i = 0; i <= ni_660x_max_rtsi_channel; ++
i) {
1059 if (selected_gate == NI_GPCT_RTSI_GATE_SELECT(i)) {
1060 ni_660x_gate_select =
1061 selected_gate & selected_gate_mask;
1065 if (i <= ni_660x_max_rtsi_channel)
1067 for (i = 0; i <= ni_660x_max_gate_pin; ++
i) {
1068 if (selected_gate == NI_GPCT_GATE_PIN_GATE_SELECT(i)) {
1069 ni_660x_gate_select =
1070 selected_gate & selected_gate_mask;
1074 if (i <= ni_660x_max_gate_pin)
1079 ni_tio_set_bits(counter,
1082 Gi_Gate_Select_Bits(ni_660x_gate_select));
1086 static int ni_m_series_set_first_gate(
struct ni_gpct *counter,
1087 unsigned int gate_source)
1089 const unsigned selected_gate =
CR_CHAN(gate_source);
1091 const unsigned selected_gate_mask = 0x1f;
1095 switch (selected_gate) {
1104 ni_m_series_gate_select = selected_gate & selected_gate_mask;
1107 for (i = 0; i <= ni_m_series_max_rtsi_channel; ++
i) {
1108 if (selected_gate == NI_GPCT_RTSI_GATE_SELECT(i)) {
1109 ni_m_series_gate_select =
1110 selected_gate & selected_gate_mask;
1114 if (i <= ni_m_series_max_rtsi_channel)
1116 for (i = 0; i <= ni_m_series_max_pfi_channel; ++
i) {
1117 if (selected_gate == NI_GPCT_PFI_GATE_SELECT(i)) {
1118 ni_m_series_gate_select =
1119 selected_gate & selected_gate_mask;
1123 if (i <= ni_m_series_max_pfi_channel)
1128 ni_tio_set_bits(counter,
1131 Gi_Gate_Select_Bits(ni_m_series_gate_select));
1135 static int ni_660x_set_second_gate(
struct ni_gpct *counter,
1136 unsigned int gate_source)
1139 const unsigned second_gate_reg =
1141 const unsigned selected_second_gate =
CR_CHAN(gate_source);
1143 static const unsigned selected_second_gate_mask = 0x1f;
1147 switch (selected_second_gate) {
1153 ni_660x_second_gate_select =
1154 selected_second_gate & selected_second_gate_mask;
1157 ni_660x_second_gate_select =
1161 for (i = 0; i <= ni_660x_max_rtsi_channel; ++
i) {
1162 if (selected_second_gate == NI_GPCT_RTSI_GATE_SELECT(i)) {
1163 ni_660x_second_gate_select =
1164 selected_second_gate &
1165 selected_second_gate_mask;
1169 if (i <= ni_660x_max_rtsi_channel)
1171 for (i = 0; i <= ni_660x_max_up_down_pin; ++
i) {
1172 if (selected_second_gate ==
1173 NI_GPCT_UP_DOWN_PIN_GATE_SELECT(i)) {
1174 ni_660x_second_gate_select =
1175 selected_second_gate &
1176 selected_second_gate_mask;
1180 if (i <= ni_660x_max_up_down_pin)
1187 counter_dev->
regs[second_gate_reg] |=
1188 Gi_Second_Gate_Select_Bits(ni_660x_second_gate_select);
1194 static int ni_m_series_set_second_gate(
struct ni_gpct *counter,
1195 unsigned int gate_source)
1198 const unsigned second_gate_reg =
1200 const unsigned selected_second_gate =
CR_CHAN(gate_source);
1202 static const unsigned selected_second_gate_mask = 0x1f;
1203 unsigned ni_m_series_second_gate_select;
1207 switch (selected_second_gate) {
1209 ni_m_series_second_gate_select =
1210 selected_second_gate & selected_second_gate_mask;
1215 counter_dev->
regs[second_gate_reg] |=
1216 Gi_Second_Gate_Select_Bits(ni_m_series_second_gate_select);
1223 unsigned int gate_source)
1226 const unsigned second_gate_reg =
1229 switch (gate_index) {
1232 ni_tio_set_bits(counter,
1233 NITIO_Gi_Mode_Reg(counter->
1239 ni_tio_set_first_gate_modifiers(counter, gate_source);
1240 switch (counter_dev->
variant) {
1243 return ni_m_series_set_first_gate(counter, gate_source);
1246 return ni_660x_set_first_gate(counter, gate_source);
1254 if (ni_tio_second_gate_registers_present(counter_dev) == 0)
1257 counter_dev->
regs[second_gate_reg] &=
1260 counter_dev->
regs[second_gate_reg],
1264 if (gate_source & CR_INVERT) {
1265 counter_dev->
regs[second_gate_reg] |=
1268 counter_dev->
regs[second_gate_reg] &=
1271 switch (counter_dev->
variant) {
1273 return ni_m_series_set_second_gate(counter,
1277 return ni_660x_set_second_gate(counter, gate_source);
1292 static int ni_tio_set_other_src(
struct ni_gpct *counter,
unsigned index,
1293 unsigned int source)
1298 unsigned int abz_reg, shift,
mask;
1315 mask = 0x1f << shift;
1316 if (source > 0x1f) {
1320 counter_dev->
regs[abz_reg] &= ~mask;
1321 counter_dev->
regs[abz_reg] |= (source << shift) & mask;
1329 static unsigned ni_660x_first_gate_to_generic_gate_source(
unsigned
1330 ni_660x_gate_select)
1334 switch (ni_660x_gate_select) {
1351 for (i = 0; i <= ni_660x_max_rtsi_channel; ++
i) {
1352 if (ni_660x_gate_select == NI_660x_RTSI_Gate_Select(i)) {
1353 return NI_GPCT_RTSI_GATE_SELECT(i);
1357 if (i <= ni_660x_max_rtsi_channel)
1359 for (i = 0; i <= ni_660x_max_gate_pin; ++
i) {
1360 if (ni_660x_gate_select ==
1361 NI_660x_Gate_Pin_Gate_Select(i)) {
1362 return NI_GPCT_GATE_PIN_GATE_SELECT(i);
1366 if (i <= ni_660x_max_gate_pin)
1374 static unsigned ni_m_series_first_gate_to_generic_gate_source(
unsigned
1375 ni_m_series_gate_select)
1379 switch (ni_m_series_gate_select) {
1405 for (i = 0; i <= ni_m_series_max_rtsi_channel; ++
i) {
1406 if (ni_m_series_gate_select ==
1407 NI_M_Series_RTSI_Gate_Select(i)) {
1408 return NI_GPCT_RTSI_GATE_SELECT(i);
1412 if (i <= ni_m_series_max_rtsi_channel)
1414 for (i = 0; i <= ni_m_series_max_pfi_channel; ++
i) {
1415 if (ni_m_series_gate_select ==
1416 NI_M_Series_PFI_Gate_Select(i)) {
1417 return NI_GPCT_PFI_GATE_SELECT(i);
1421 if (i <= ni_m_series_max_pfi_channel)
1429 static unsigned ni_660x_second_gate_to_generic_gate_source(
unsigned
1430 ni_660x_gate_select)
1434 switch (ni_660x_gate_select) {
1454 for (i = 0; i <= ni_660x_max_rtsi_channel; ++
i) {
1455 if (ni_660x_gate_select ==
1456 NI_660x_RTSI_Second_Gate_Select(i)) {
1457 return NI_GPCT_RTSI_GATE_SELECT(i);
1461 if (i <= ni_660x_max_rtsi_channel)
1463 for (i = 0; i <= ni_660x_max_up_down_pin; ++
i) {
1464 if (ni_660x_gate_select ==
1465 NI_660x_Up_Down_Pin_Second_Gate_Select(i)) {
1466 return NI_GPCT_UP_DOWN_PIN_GATE_SELECT(i);
1470 if (i <= ni_660x_max_up_down_pin)
1478 static unsigned ni_m_series_second_gate_to_generic_gate_source(
unsigned
1479 ni_m_series_gate_select)
1483 switch (ni_m_series_gate_select) {
1491 static int ni_tio_get_gate_src(
struct ni_gpct *counter,
unsigned gate_index,
1492 unsigned int *gate_source)
1495 const unsigned mode_bits = ni_tio_get_soft_copy(counter,
1499 const unsigned second_gate_reg =
1501 unsigned gate_select_bits;
1503 switch (gate_index) {
1511 (ni_tio_get_soft_copy(counter,
1512 NITIO_Gi_Input_Select_Reg
1516 switch (counter_dev->
variant) {
1520 ni_m_series_first_gate_to_generic_gate_source
1525 ni_660x_first_gate_to_generic_gate_source
1539 || (counter_dev->
regs[second_gate_reg] &
1546 (counter_dev->
regs[second_gate_reg] &
1550 switch (counter_dev->
variant) {
1554 ni_m_series_second_gate_to_generic_gate_source
1559 ni_660x_second_gate_to_generic_gate_source
1566 if (counter_dev->
regs[second_gate_reg] &
1586 return ni_tio_set_counter_mode(counter, data[1]);
1596 data[1] = ni_tio_counter_status(counter);
1597 data[2] = counter_status_mask;
1601 return ni_tio_set_clock_src(counter, data[1], data[2]);
1604 ni_tio_get_clock_src(counter, &data[1], &data[2]);
1611 return ni_tio_get_gate_src(counter, data[1], &data[2]);
1614 return ni_tio_set_other_src(counter, data[1], data[2]);
1617 ni_tio_reset_count_and_disarm(counter);
1632 unsigned first_read;
1633 unsigned second_read;
1634 unsigned correct_read;
1640 ni_tio_set_bits(counter,
1643 ni_tio_set_bits(counter,
1657 if (first_read != second_read)
1660 NITIO_Gi_SW_Save_Reg(counter->
1663 correct_read = first_read;
1664 data[0] = correct_read;
1682 static unsigned ni_tio_next_load_register(
struct ni_gpct *counter)
1685 NITIO_Gxx_Status_Reg(counter->
1707 load_reg = ni_tio_next_load_register(counter);
1709 ni_tio_set_bits_transient(counter,
1710 NITIO_Gi_Command_Reg(counter->