44 #include "../comedidev.h"
54 #define NUM_PFI_CHANNELS 40
57 #define MAX_DMA_CHANNEL 4
162 static inline unsigned IOConfigReg(
unsigned pfi_channel)
192 static inline unsigned NI_660X_GPCT_SUBDEV(
unsigned index)
312 static inline unsigned ioconfig_bitshift(
unsigned pfi_channel)
320 static inline unsigned pfi_output_select_mask(
unsigned pfi_channel)
322 return 0x3 << ioconfig_bitshift(pfi_channel);
325 static inline unsigned pfi_output_select_bits(
unsigned pfi_channel,
326 unsigned output_select)
328 return (output_select & 0x3) << ioconfig_bitshift(pfi_channel);
331 static inline unsigned pfi_input_select_mask(
unsigned pfi_channel)
333 return 0x7 << (4 + ioconfig_bitshift(pfi_channel));
336 static inline unsigned pfi_input_select_bits(
unsigned pfi_channel,
337 unsigned input_select)
339 return (input_select & 0x7) << (4 + ioconfig_bitshift(pfi_channel));
343 static inline unsigned dma_select_mask(
unsigned dma_channel)
346 return 0x1f << (8 * dma_channel);
352 static inline unsigned dma_selection_counter(
unsigned counter_index)
355 return counter_index;
358 static inline unsigned dma_select_bits(
unsigned dma_channel,
unsigned selection)
361 return (selection << (8 * dma_channel)) & dma_select_mask(dma_channel);
364 static inline unsigned dma_reset_bit(
unsigned dma_channel)
367 return 0x80 << (8 * dma_channel);
387 static const unsigned GPCT_OFFSET[2] = { 0x0, 0x800 };
419 #define NI_660X_MAX_NUM_CHIPS 2
420 #define NI_660X_MAX_NUM_COUNTERS (NI_660X_MAX_NUM_CHIPS * counters_per_chip)
462 unsigned pfi_channel,
463 unsigned output_select);
466 .driver_name =
"ni_660x",
468 .attach_pci = ni_660x_attach_pci,
469 .detach = ni_660x_detach,
483 static struct pci_driver ni_660x_pci_driver = {
485 .id_table = ni_660x_pci_table,
486 .probe = ni_660x_pci_probe,
733 return ni_660x_register;
737 unsigned chip_index,
unsigned bits,
741 private(
dev)->mite->daq_io_addr + GPCT_OFFSET[chip_index] +
744 switch (registerData[reg].
size) {
746 writew(bits, write_address);
749 writel(bits, write_address);
757 static inline unsigned ni_660x_read_register(
struct comedi_device *dev,
762 private(
dev)->mite->daq_io_addr + GPCT_OFFSET[chip_index] +
765 switch (registerData[reg].
size) {
767 return readw(read_address);
770 return readl(read_address);
779 static void ni_gpct_write_register(
struct ni_gpct *
counter,
unsigned bits,
784 ni_660x_write_register(dev, counter->
chip_index, bits,
793 return ni_660x_read_register(dev, counter->
chip_index,
805 static inline void ni_660x_set_dma_channel(
struct comedi_device *dev,
811 private(
dev)->dma_configuration_soft_copies[counter->
chip_index] &=
812 ~dma_select_mask(mite_channel);
813 private(
dev)->dma_configuration_soft_copies[counter->
chip_index] |=
814 dma_select_bits(mite_channel,
816 ni_660x_write_register(dev, counter->
chip_index,
818 dma_configuration_soft_copies
822 spin_unlock_irqrestore(&
private(dev)->soft_reg_copy_lock, flags);
825 static inline void ni_660x_unset_dma_channel(
struct comedi_device *dev,
826 unsigned mite_channel,
831 private(
dev)->dma_configuration_soft_copies[counter->
chip_index] &=
832 ~dma_select_mask(mite_channel);
833 private(
dev)->dma_configuration_soft_copies[counter->
chip_index] |=
835 ni_660x_write_register(dev, counter->
chip_index,
837 dma_configuration_soft_copies
840 spin_unlock_irqrestore(&
private(dev)->soft_reg_copy_lock, flags);
843 static int ni_660x_request_mite_channel(
struct comedi_device *dev,
848 struct mite_channel *mite_chan;
853 mite_request_channel(
private(dev)->
mite, mite_ring(
private(dev),
855 if (mite_chan ==
NULL) {
856 spin_unlock_irqrestore(&
private(dev)->mite_channel_lock, flags);
858 "failed to reserve mite dma channel for counter.");
863 ni_660x_set_dma_channel(dev, mite_chan->
channel, counter);
864 spin_unlock_irqrestore(&
private(dev)->mite_channel_lock, flags);
868 static void ni_660x_release_mite_channel(
struct comedi_device *dev,
875 struct mite_channel *mite_chan = counter->
mite_chan;
877 ni_660x_unset_dma_channel(dev, mite_chan->
channel, counter);
881 spin_unlock_irqrestore(&
private(dev)->mite_channel_lock, flags);
888 struct ni_gpct *counter = subdev_to_counter(s);
891 retval = ni_660x_request_mite_channel(dev, counter,
COMEDI_INPUT);
894 "no dma channel available for use by counter");
906 struct ni_gpct *counter = subdev_to_counter(s);
913 struct ni_gpct *counter = subdev_to_counter(s);
917 ni_660x_release_mite_channel(dev, counter);
934 static void ni_660x_handle_gpct_interrupt(
struct comedi_device *dev,
938 if (s->
async->events) {
941 ni_660x_cancel(dev, s);
959 for (i = 0; i < ni_660x_num_counters(dev); ++
i) {
961 ni_660x_handle_gpct_interrupt(dev, s);
963 spin_unlock_irqrestore(&
private(dev)->interrupt_lock, flags);
974 spin_unlock_irqrestore(&
private(dev)->interrupt_lock, flags);
980 unsigned long new_size)
992 static int ni_660x_allocate_private(
struct comedi_device *dev)
1010 static int ni_660x_alloc_mite_rings(
struct comedi_device *dev)
1015 for (i = 0; i <
board(dev)->n_chips; ++
i) {
1017 private(
dev)->mite_rings[i][j] =
1019 if (
private(dev)->mite_rings[
i][
j] ==
NULL)
1026 static void ni_660x_free_mite_rings(
struct comedi_device *dev)
1031 for (i = 0; i <
board(dev)->n_chips; ++
i) {
1043 for (n = 0; n <
ARRAY_SIZE(ni_660x_boards); n++) {
1045 if (board->
dev_id == dev_id)
1057 unsigned global_interrupt_config_bits;
1059 ret = ni_660x_allocate_private(dev);
1062 dev->
board_ptr = ni_660x_find_boardinfo(pcidev);
1066 if (!
private(dev)->mite)
1076 comedi_set_hw_dev(dev, &
private(dev)->mite->pcidev->dev);
1077 ret = ni_660x_alloc_mite_rings(dev);
1104 &ni_gpct_write_register,
1105 &ni_gpct_read_register,
1107 ni_660x_num_counters
1109 if (
private(dev)->counter_dev ==
NULL)
1112 s = &dev->
subdevices[NI_660X_GPCT_SUBDEV(i)];
1113 if (i < ni_660x_num_counters(dev)) {
1123 s->
do_cmd = &ni_660x_cmd;
1126 s->
cancel = &ni_660x_cancel;
1127 s->
poll = &ni_660x_input_poll;
1130 s->
private = &
private(
dev)->counter_dev->counters[i];
1132 private(dev)->counter_dev->counters[
i].chip_index =
1134 private(
dev)->counter_dev->counters[i].counter_index =
1135 i % counters_per_chip;
1140 for (i = 0; i <
board(dev)->n_chips; ++
i)
1141 init_tio_chip(dev, i);
1143 for (i = 0; i < ni_660x_num_counters(dev); ++
i)
1150 ni_660x_set_pfi_routing(dev, i,
1156 for (i = 0; i <
board(dev)->n_chips; ++
i)
1157 set_tio_counterswap(dev, i);
1159 ret =
request_irq(mite_irq(
private(dev)->mite), ni_660x_interrupt,
1165 dev->
irq = mite_irq(
private(dev)->mite);
1167 if (
board(dev)->n_chips > 1)
1169 ni_660x_write_register(dev, 0, global_interrupt_config_bits,
1180 if (
private(dev)->counter_dev)
1182 if (
private(dev)->mite) {
1183 ni_660x_free_mite_rings(dev);
1185 mite_free(
private(dev)->mite);
1197 static void init_tio_chip(
struct comedi_device *dev,
int chipset)
1202 private(
dev)->dma_configuration_soft_copies[chipset] = 0;
1204 private(
dev)->dma_configuration_soft_copies[chipset] |=
1207 ni_660x_write_register(dev, chipset,
1209 dma_configuration_soft_copies[chipset],
1212 ni_660x_write_register(dev, chipset, 0, IOConfigReg(i));
1237 s->
state &= ~(data[0] << base_bitfield_channel);
1238 s->
state |= (data[0] & data[1]) << base_bitfield_channel;
1245 (ni_660x_read_register(dev, 0,
1250 static void ni_660x_select_pfi_output(
struct comedi_device *dev,
1251 unsigned pfi_channel,
1252 unsigned output_select)
1254 static const unsigned counter_4_7_first_pfi = 8;
1255 static const unsigned counter_4_7_last_pfi = 23;
1256 unsigned active_chipset = 0;
1257 unsigned idle_chipset = 0;
1258 unsigned active_bits;
1261 if (
board(dev)->n_chips > 1) {
1263 pfi_channel >= counter_4_7_first_pfi &&
1264 pfi_channel <= counter_4_7_last_pfi) {
1273 if (idle_chipset != active_chipset) {
1275 ni_660x_read_register(dev, idle_chipset,
1276 IOConfigReg(pfi_channel));
1277 idle_bits &= ~pfi_output_select_mask(pfi_channel);
1279 pfi_output_select_bits(pfi_channel,
1281 ni_660x_write_register(dev, idle_chipset, idle_bits,
1282 IOConfigReg(pfi_channel));
1286 ni_660x_read_register(dev, active_chipset,
1287 IOConfigReg(pfi_channel));
1288 active_bits &= ~pfi_output_select_mask(pfi_channel);
1289 active_bits |= pfi_output_select_bits(pfi_channel, output_select);
1290 ni_660x_write_register(dev, active_chipset, active_bits,
1291 IOConfigReg(pfi_channel));
1308 BUG_ON(chan >= NUM_PFI_CHANNELS);
1310 private(
dev)->pfi_output_selects[chan] = source;
1311 if (
private(dev)->pfi_direction_bits & (((
uint64_t) 1) << chan))
1312 ni_660x_select_pfi_output(dev, chan,
1314 pfi_output_selects[chan]);
1318 static unsigned ni_660x_get_pfi_routing(
struct comedi_device *dev,
1321 BUG_ON(chan >= NUM_PFI_CHANNELS);
1322 return private(
dev)->pfi_output_selects[chan];
1326 unsigned pfi_channel,
1329 unsigned bits = ni_660x_read_register(dev, 0, IOConfigReg(pfi_channel));
1330 bits &= ~pfi_input_select_mask(pfi_channel);
1331 bits |= pfi_input_select_bits(pfi_channel, filter);
1332 ni_660x_write_register(dev, 0, bits, IOConfigReg(pfi_channel));
1335 static int ni_660x_dio_insn_config(
struct comedi_device *dev,
1348 private(
dev)->pfi_direction_bits |= ((
uint64_t) 1) << chan;
1349 ni_660x_select_pfi_output(dev, chan,
1351 pfi_output_selects[chan]);
1354 private(
dev)->pfi_direction_bits &= ~(((
uint64_t) 1) << chan);
1359 (
private(
dev)->pfi_direction_bits &
1363 return ni_660x_set_pfi_routing(dev, chan, data[1]);
1366 data[1] = ni_660x_get_pfi_routing(dev, chan);
1369 ni660x_config_filter(dev, chan, data[1]);