86 #include "../comedidev.h"
99 #define DEBUG_PRINT(format, args...) printk(format , ## args)
101 #define DEBUG_PRINT(format, args...)
104 #define TIMER_BASE 25
105 #define PRESCALED_TIMER_BASE 10000
106 #define DMA_BUFFER_SIZE 0x1000
108 #define PCI_VENDOR_ID_COMPUTERBOARDS 0x1307
111 static const int max_counter_value = 0xffffff;
151 static inline unsigned int dac_convert_reg(
unsigned int channel)
153 return 0x70 + (2 * (channel & 0x1));
156 static inline unsigned int dac_lsb_4020_reg(
unsigned int channel)
158 return 0x70 + (4 * (channel & 0x1));
161 static inline unsigned int dac_msb_4020_reg(
unsigned int channel)
163 return 0x72 + (4 * (channel & 0x1));
226 #define DAC_FIFO_SIZE 0x2000
235 return threshold & 0xfff;
272 return (channel & 0x3) << 8;
277 return (channel & 0x3) << 10;
282 return (mode & 0xf) << 12;
307 return (source & 0xf) << 3;
310 static inline uint16_t adc_convert_chan_4020_bits(
unsigned int channel)
312 return (channel & 0x3) << 8;
322 static inline uint16_t adc_chan_bits(
unsigned int channel)
324 return channel & 0x3f;
370 return (hw_status_bits >> 10) & 0x3;
373 static inline unsigned int dma_chain_flag_bits(
uint16_t prepost_bits)
375 return (prepost_bits >> 6) & 0x3;
378 static inline unsigned int adc_upper_read_ptr_code(
uint16_t prepost_bits)
380 return (prepost_bits >> 12) & 0x3;
383 static inline unsigned int adc_upper_write_ptr_code(
uint16_t prepost_bits)
385 return (prepost_bits >> 14) & 0x3;
404 static inline uint8_t attenuate_bit(
unsigned int channel)
407 return 1 << (channel & 0x3);
499 static const int ao_range_code_64xx[] = {
513 static const int ao_range_code_60xx[] = {
525 static const int ao_range_code_6030[] = {
538 static const int ao_range_code_4020[] = {
575 .max_segment_length = 0x8000,
576 .sample_packing_ratio = 2,
577 .fifo_size_reg_mask = 0x7f,
582 .max_segment_length = 0x800,
583 .sample_packing_ratio = 1,
584 .fifo_size_reg_mask = 0x3f,
589 .max_segment_length = 0x800,
590 .sample_packing_ratio = 1,
591 .fifo_size_reg_mask = 0x7f,
596 #define MAX_AI_DMA_RING_COUNT (0x80000 / DMA_BUFFER_SIZE)
597 #define MIN_AI_DMA_RING_COUNT (0x10000 / DMA_BUFFER_SIZE)
598 #define AO_DMA_RING_COUNT (0x10000 / DMA_BUFFER_SIZE)
607 static const int bytes_in_sample = 2;
611 .name =
"pci-das6402/16",
618 .ao_scan_speed = 10000,
620 .ai_range_table = &ai_ranges_64xx,
621 .ao_range_table = &ao_ranges_64xx,
622 .ao_range_code = ao_range_code_64xx,
623 .ai_fifo = &ai_fifo_64xx,
627 .name =
"pci-das6402/12",
634 .ao_scan_speed = 10000,
636 .ai_range_table = &ai_ranges_64xx,
637 .ao_range_table = &ao_ranges_64xx,
638 .ao_range_code = ao_range_code_64xx,
639 .ai_fifo = &ai_fifo_64xx,
643 .name =
"pci-das64/m1/16",
650 .ao_scan_speed = 10000,
652 .ai_range_table = &ai_ranges_64xx,
653 .ao_range_table = &ao_ranges_64xx,
654 .ao_range_code = ao_range_code_64xx,
655 .ai_fifo = &ai_fifo_64xx,
659 .name =
"pci-das64/m2/16",
666 .ao_scan_speed = 10000,
668 .ai_range_table = &ai_ranges_64xx,
669 .ao_range_table = &ao_ranges_64xx,
670 .ao_range_code = ao_range_code_64xx,
671 .ai_fifo = &ai_fifo_64xx,
675 .name =
"pci-das64/m3/16",
682 .ao_scan_speed = 10000,
684 .ai_range_table = &ai_ranges_64xx,
685 .ao_range_table = &ao_ranges_64xx,
686 .ao_range_code = ao_range_code_64xx,
687 .ai_fifo = &ai_fifo_64xx,
691 .name =
"pci-das6013",
699 .ai_range_table = &ai_ranges_60xx,
700 .ao_range_table = &ao_ranges_60xx,
701 .ao_range_code = ao_range_code_60xx,
702 .ai_fifo = &ai_fifo_60xx,
706 .name =
"pci-das6014",
713 .ao_scan_speed = 100000,
715 .ai_range_table = &ai_ranges_60xx,
716 .ao_range_table = &ao_ranges_60xx,
717 .ao_range_code = ao_range_code_60xx,
718 .ai_fifo = &ai_fifo_60xx,
722 .name =
"pci-das6023",
728 .ao_scan_speed = 100000,
730 .ai_range_table = &ai_ranges_60xx,
731 .ao_range_table = &ao_ranges_60xx,
732 .ao_range_code = ao_range_code_60xx,
733 .ai_fifo = &ai_fifo_60xx,
737 .name =
"pci-das6025",
744 .ao_scan_speed = 100000,
746 .ai_range_table = &ai_ranges_60xx,
747 .ao_range_table = &ao_ranges_60xx,
748 .ao_range_code = ao_range_code_60xx,
749 .ai_fifo = &ai_fifo_60xx,
753 .name =
"pci-das6030",
760 .ao_scan_speed = 10000,
762 .ai_range_table = &ai_ranges_6030,
763 .ao_range_table = &ao_ranges_6030,
764 .ao_range_code = ao_range_code_6030,
765 .ai_fifo = &ai_fifo_60xx,
769 .name =
"pci-das6031",
776 .ao_scan_speed = 10000,
778 .ai_range_table = &ai_ranges_6030,
779 .ao_range_table = &ao_ranges_6030,
780 .ao_range_code = ao_range_code_6030,
781 .ai_fifo = &ai_fifo_60xx,
785 .name =
"pci-das6032",
792 .ai_range_table = &ai_ranges_6030,
793 .ai_fifo = &ai_fifo_60xx,
797 .name =
"pci-das6033",
804 .ai_range_table = &ai_ranges_6030,
805 .ai_fifo = &ai_fifo_60xx,
809 .name =
"pci-das6034",
817 .ai_range_table = &ai_ranges_60xx,
818 .ai_fifo = &ai_fifo_60xx,
822 .name =
"pci-das6035",
829 .ao_scan_speed = 100000,
831 .ai_range_table = &ai_ranges_60xx,
832 .ao_range_table = &ao_ranges_60xx,
833 .ao_range_code = ao_range_code_60xx,
834 .ai_fifo = &ai_fifo_60xx,
838 .name =
"pci-das6036",
845 .ao_scan_speed = 100000,
847 .ai_range_table = &ai_ranges_60xx,
848 .ao_range_table = &ao_ranges_60xx,
849 .ao_range_code = ao_range_code_60xx,
850 .ai_fifo = &ai_fifo_60xx,
854 .name =
"pci-das6040",
861 .ao_scan_speed = 1000,
863 .ai_range_table = &ai_ranges_6052,
864 .ao_range_table = &ao_ranges_6030,
865 .ao_range_code = ao_range_code_6030,
866 .ai_fifo = &ai_fifo_60xx,
870 .name =
"pci-das6052",
877 .ao_scan_speed = 3333,
879 .ai_range_table = &ai_ranges_6052,
880 .ao_range_table = &ao_ranges_6030,
881 .ao_range_code = ao_range_code_6030,
882 .ai_fifo = &ai_fifo_60xx,
886 .name =
"pci-das6070",
893 .ao_scan_speed = 1000,
895 .ai_range_table = &ai_ranges_6052,
896 .ao_range_table = &ao_ranges_6030,
897 .ao_range_code = ao_range_code_6030,
898 .ai_fifo = &ai_fifo_60xx,
902 .name =
"pci-das6071",
909 .ao_scan_speed = 1000,
911 .ai_range_table = &ai_ranges_6052,
912 .ao_range_table = &ao_ranges_6030,
913 .ao_range_code = ao_range_code_6030,
914 .ai_fifo = &ai_fifo_60xx,
918 .name =
"pci-das4020/12",
927 .ai_range_table = &ai_ranges_4020,
928 .ao_range_table = &ao_ranges_4020,
929 .ao_range_code = ao_range_code_4020,
930 .ai_fifo = &ai_fifo_4020,
935 .name =
"pci-das6402/16/jr",
941 .ao_scan_speed = 10000,
943 .ai_range_table = &ai_ranges_64xx,
944 .ai_fifo = ai_fifo_64xx,
948 .name =
"pci-das64/m1/16/jr",
954 .ao_scan_speed = 10000,
956 .ai_range_table = &ai_ranges_64xx,
957 .ai_fifo = ai_fifo_64xx,
961 .name =
"pci-das64/m2/16/jr",
967 .ao_scan_speed = 10000,
969 .ai_range_table = &ai_ranges_64xx,
970 .ai_fifo = ai_fifo_64xx,
974 .name =
"pci-das64/m3/16/jr",
980 .ao_scan_speed = 10000,
982 .ai_range_table = &ai_ranges_64xx,
983 .ai_fifo = ai_fifo_64xx,
987 .name =
"pci-das64/m1/14",
993 .ao_scan_speed = 10000,
995 .ai_range_table = &ai_ranges_64xx,
996 .ai_fifo = ai_fifo_64xx,
1000 .name =
"pci-das64/m2/14",
1006 .ao_scan_speed = 10000,
1008 .ai_range_table = &ai_ranges_64xx,
1009 .ai_fifo = ai_fifo_64xx,
1013 .name =
"pci-das64/m3/14",
1019 .ao_scan_speed = 10000,
1021 .ai_range_table = &ai_ranges_64xx,
1022 .ai_fifo = ai_fifo_64xx,
1033 static inline unsigned short se_diff_bit_6xxx(
struct comedi_device *dev,
1034 int use_differential)
1122 static int dio_callback(
int dir,
int port,
int data,
unsigned long arg);
1123 static int dio_callback_4020(
int dir,
int port,
int data,
unsigned long arg);
1135 unsigned int *
data);
1142 static void ad8402_write(
struct comedi_device *dev,
unsigned int channel,
1143 unsigned int value);
1151 static unsigned int get_divisor(
unsigned int ns,
unsigned int flags);
1154 static void caldac_write(
struct comedi_device *dev,
unsigned int channel,
1155 unsigned int value);
1160 unsigned int caldac_channel,
unsigned int value);
1161 static void abort_dma(
struct comedi_device *dev,
unsigned int channel);
1162 static void disable_plx_interrupts(
struct comedi_device *dev);
1164 unsigned int num_samples);
1165 static unsigned int ai_fifo_size(
struct comedi_device *dev);
1166 static int set_ai_fifo_segment_length(
struct comedi_device *dev,
1169 static void disable_ai_interrupts(
struct comedi_device *dev);
1172 static unsigned int get_ao_divisor(
unsigned int ns,
unsigned int flags);
1176 static unsigned int ai_range_bits_6xxx(
const struct comedi_device *dev,
1177 unsigned int range_index)
1180 &
board(dev)->ai_range_table->range[range_index];
1181 unsigned int bits = 0;
1183 switch (range->
max) {
1215 if (range->
min == 0)
1220 static unsigned int hw_revision(
const struct comedi_device *dev,
1224 return (hw_status_bits >> 13) & 0x7;
1226 return (hw_status_bits >> 12) & 0xf;
1230 volatile uint16_t *bits,
unsigned int channel,
1240 *bits &= ~(0x3 << (2 *
channel));
1241 *bits |= code << (2 *
channel);
1253 void __iomem *plx_iobase =
priv(dev)->plx9080_iobase;
1255 priv(dev)->plx_control_bits =
1276 DEBUG_PRINT(
" plx dma channel 0 pci address 0x%x\n",
1278 DEBUG_PRINT(
" plx dma channel 0 local address 0x%x\n",
1280 DEBUG_PRINT(
" plx dma channel 0 transfer size 0x%x\n",
1282 DEBUG_PRINT(
" plx dma channel 0 descriptor 0x%x\n",
1284 DEBUG_PRINT(
" plx dma channel 0 command status 0x%x\n",
1286 DEBUG_PRINT(
" plx dma channel 0 threshold 0x%x\n",
1297 disable_plx_interrupts(dev);
1327 if (ao_cmd_is_supported(
board(dev)))
1331 priv(dev)->plx_intcsr_bits |=
1343 void __iomem *dio_8255_iobase;
1373 priv(dev)->i2c_cal_range_bits = adc_src_4020_bits(4);
1375 for (i = 0; i < s->
n_chan; i++)
1376 priv(dev)->i2c_cal_range_bits |= attenuate_bit(i);
1377 data =
priv(dev)->i2c_cal_range_bits;
1383 if (
board(dev)->ao_nchan) {
1392 if (ao_cmd_is_supported(
board(dev))) {
1429 if (
board(dev)->has_8255) {
1434 (
unsigned long)dio_8255_iobase);
1439 (
unsigned long)dio_8255_iobase);
1468 for (i = 0; i < s->
n_chan; i++)
1469 caldac_write(dev, i, s->
maxdata / 2);
1480 for (i = 0; i < s->
n_chan; i++)
1481 ad8402_write(dev, i, s->
maxdata / 2);
1503 static void disable_plx_interrupts(
struct comedi_device *dev)
1505 priv(dev)->plx_intcsr_bits = 0;
1513 unsigned long flags;
1536 spin_unlock_irqrestore(&dev->
spinlock, flags);
1540 set_ai_fifo_segment_length(dev,
1541 board(dev)->ai_fifo->max_segment_length);
1544 priv(dev)->intr_enable_bits =
1549 disable_ai_pacing(dev);
1552 static int alloc_and_init_dma_members(
struct comedi_device *dev)
1558 for (i = 0; i < ai_dma_ring_count(
board(dev)); i++) {
1559 priv(dev)->ai_buffer[
i] =
1561 &
priv(dev)->ai_buffer_bus_addr[i]);
1562 if (
priv(dev)->ai_buffer[i] ==
NULL)
1567 if (ao_cmd_is_supported(
board(dev))) {
1568 priv(dev)->ao_buffer[
i] =
1572 ao_buffer_bus_addr[i]);
1573 if (
priv(dev)->ao_buffer[i] ==
NULL)
1579 priv(dev)->ai_dma_desc =
1582 ai_dma_ring_count(
board(dev)),
1583 &
priv(dev)->ai_dma_desc_bus_addr);
1584 if (
priv(dev)->ai_dma_desc ==
NULL)
1587 DEBUG_PRINT(
"ai dma descriptors start at bus addr 0x%x\n",
1588 priv(dev)->ai_dma_desc_bus_addr);
1589 if (ao_cmd_is_supported(
board(dev))) {
1590 priv(dev)->ao_dma_desc =
1594 &
priv(dev)->ao_dma_desc_bus_addr);
1595 if (
priv(dev)->ao_dma_desc ==
NULL)
1598 DEBUG_PRINT(
"ao dma descriptors start at bus addr 0x%x\n",
1599 priv(dev)->ao_dma_desc_bus_addr);
1602 for (i = 0; i < ai_dma_ring_count(
board(dev)); i++) {
1603 priv(dev)->ai_dma_desc[
i].pci_start_addr =
1606 priv(dev)->ai_dma_desc[
i].local_start_addr =
1610 priv(dev)->ai_dma_desc[
i].local_start_addr =
1614 priv(dev)->ai_dma_desc[
i].next =
1620 sizeof(
priv(dev)->ai_dma_desc[0])) |
1624 if (ao_cmd_is_supported(
board(dev))) {
1626 priv(dev)->ao_dma_desc[
i].pci_start_addr =
1628 priv(dev)->ao_dma_desc[
i].local_start_addr =
1631 priv(dev)->ao_dma_desc[
i].transfer_size =
1633 priv(dev)->ao_dma_desc[
i].next =
1635 ((i + 1) % (AO_DMA_RING_COUNT)) *
1636 sizeof(
priv(dev)->ao_dma_desc[0])) |
1644 static inline void warn_external_queue(
struct comedi_device *dev)
1647 "AO command and AI external channel queue cannot be used simultaneously.");
1649 "Use internal AI channel queue (channels must be consecutive and use same range/aref)");
1662 if (bus != pcidev->
bus->number ||
1669 for (i = 0; i <
ARRAY_SIZE(pcidas64_boards); i++) {
1677 "No supported board found! (req. bus %d, slot %d)\n",
1689 uint32_t local_range, local_decode;
1698 pcidev = cb_pcidas64_find_pci_dev(dev, it);
1701 comedi_set_hw_dev(dev, &pcidev->
dev);
1705 "failed to enable PCI device and request regions\n");
1715 priv(dev)->plx9080_phys_iobase =
1718 priv(dev)->dio_counter_phys_iobase =
1725 priv(dev)->main_iobase =
1728 priv(dev)->dio_counter_iobase =
1732 if (!
priv(dev)->plx9080_iobase || !
priv(dev)->main_iobase
1733 || !
priv(dev)->dio_counter_iobase) {
1738 DEBUG_PRINT(
" plx9080 remapped to 0x%p\n",
priv(dev)->plx9080_iobase);
1741 priv(dev)->dio_counter_iobase);
1749 priv(dev)->local0_iobase =
1750 ((
uint32_t)
priv(dev)->main_phys_iobase & ~local_range) |
1757 priv(dev)->local1_iobase =
1758 ((
uint32_t)
priv(dev)->dio_counter_phys_iobase & ~local_range) |
1764 retval = alloc_and_init_dma_members(dev);
1768 priv(dev)->hw_revision =
1771 priv(dev)->hw_revision);
1773 init_stc_registers(dev);
1776 "cb_pcidas64", dev)) {
1784 retval = setup_subdevices(dev);
1794 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
1801 if (
priv(dev)->plx9080_iobase) {
1802 disable_plx_interrupts(dev);
1805 if (
priv(dev)->main_iobase)
1807 if (
priv(dev)->dio_counter_iobase)
1810 for (i = 0; i < ai_dma_ring_count(
board(dev)); i++) {
1811 if (
priv(dev)->ai_buffer[
i])
1817 (dev)->ai_buffer_bus_addr
1821 if (
priv(dev)->ao_buffer[
i])
1827 (dev)->ao_buffer_bus_addr
1831 if (
priv(dev)->ai_dma_desc)
1835 ai_dma_ring_count(
board
1837 priv(dev)->ai_dma_desc,
1839 ai_dma_desc_bus_addr);
1840 if (
priv(dev)->ao_dma_desc)
1844 priv(dev)->ao_dma_desc,
1846 ao_dma_desc_bus_addr);
1862 unsigned int bits = 0,
n,
i;
1864 unsigned long flags;
1865 static const int timeout = 100;
1874 disable_ai_pacing(dev);
1883 spin_unlock_irqrestore(&dev->
spinlock, flags);
1893 unsigned int cal_en_bit;
1902 adc_src_bits(
priv(dev)->calibration_source),
1913 bits |= se_diff_bit_6xxx(dev, aref ==
AREF_DIFF);
1916 bits |= adc_chan_bits(channel);
1918 writew(adc_chan_bits(channel),
1923 uint8_t old_cal_range_bits =
priv(dev)->i2c_cal_range_bits;
1928 priv(dev)->i2c_cal_range_bits |=
1929 adc_src_4020_bits(
priv(dev)->calibration_source);
1931 priv(dev)->i2c_cal_range_bits |= adc_src_4020_bits(4);
1935 priv(dev)->i2c_cal_range_bits |= attenuate_bit(channel);
1937 priv(dev)->i2c_cal_range_bits &=
1938 ~attenuate_bit(channel);
1940 if (old_cal_range_bits !=
priv(dev)->i2c_cal_range_bits) {
1954 for (
n = 0;
n < insn->
n;
n++) {
1964 for (i = 0; i < timeout; i++) {
1966 DEBUG_PRINT(
" pipe bits 0x%x\n", pipe_full_bits(bits));
1972 if (pipe_full_bits(bits))
1977 DEBUG_PRINT(
" looped %i times waiting for data\n", i);
1979 comedi_error(dev,
" analog input read insn timed out");
1980 printk(
" status 0x%x\n", bits);
1995 static int ai_config_calibration_source(
struct comedi_device *dev,
1998 unsigned int source = data[1];
1999 int num_calibration_sources;
2002 num_calibration_sources = 16;
2004 num_calibration_sources = 8;
2005 if (source >= num_calibration_sources) {
2011 DEBUG_PRINT(
"setting calibration source to %i\n", source);
2017 static int ai_config_block_size(
struct comedi_device *dev,
unsigned int *data)
2021 unsigned int block_size, requested_block_size;
2024 requested_block_size = data[1];
2026 if (requested_block_size) {
2028 requested_block_size * fifo->
num_segments / bytes_in_sample;
2030 retval = set_ai_fifo_size(dev, fifo_size);
2036 block_size = ai_fifo_size(dev) / fifo->
num_segments * bytes_in_sample;
2043 static int ai_config_master_clock_4020(
struct comedi_device *dev,
2046 unsigned int divisor = data[4];
2057 priv(dev)->ext_clock.chanspec = data[2];
2066 return retval ? retval : 5;
2070 static int ai_config_master_clock(
struct comedi_device *dev,
unsigned int *data)
2075 return ai_config_master_clock_4020(dev, data);
2092 return ai_config_calibration_source(dev, data);
2095 return ai_config_block_size(dev, data);
2098 return ai_config_master_clock(dev, data);
2111 unsigned int tmp_arg, tmp_arg2;
2114 unsigned int triggers;
2132 err |= cfc_check_trigger_src(&cmd->
convert_src, triggers);
2135 err |= cfc_check_trigger_src(&cmd->
stop_src,
2143 err |= cfc_check_trigger_is_unique(cmd->
start_src);
2145 err |= cfc_check_trigger_is_unique(cmd->
convert_src);
2146 err |= cfc_check_trigger_is_unique(cmd->
stop_src);
2221 check_adc_timing(dev, cmd);
2237 "all elements in chanlist must use the same analog reference");
2247 first_channel + i) {
2249 "chanlist must use consecutive channels");
2256 "chanlist cannot be 3 channels long, use 1, 2, or 4 channels");
2268 static int use_hw_sample_counter(
struct comedi_cmd *cmd)
2279 static void setup_sample_counters(
struct comedi_device *dev,
2287 if (use_hw_sample_counter(cmd)) {
2297 static inline unsigned int dma_transfer_size(
struct comedi_device *dev)
2299 unsigned int num_samples;
2302 priv(dev)->ai_fifo_segment_length *
2303 board(dev)->ai_fifo->sample_packing_ratio;
2312 unsigned long flags;
2314 disable_ai_interrupts(dev);
2320 spin_unlock_irqrestore(&dev->
spinlock, flags);
2327 static void disable_ai_interrupts(
struct comedi_device *dev)
2329 unsigned long flags;
2332 priv(dev)->intr_enable_bits &=
2338 spin_unlock_irqrestore(&dev->
spinlock, flags);
2347 unsigned long flags;
2358 priv(dev)->intr_enable_bits |=
bits;
2362 spin_unlock_irqrestore(&dev->
spinlock, flags);
2403 divisor =
priv(dev)->ext_clock.divisor;
2415 static void select_master_clock_4020(
struct comedi_device *dev,
2421 int chanspec =
priv(dev)->ext_clock.chanspec;
2439 select_master_clock_4020(dev, cmd);
2446 static inline void dma_start_sync(
struct comedi_device *dev,
2447 unsigned int channel)
2449 unsigned long flags;
2461 spin_unlock_irqrestore(&dev->
spinlock, flags);
2466 uint32_t convert_counter = 0, scan_counter = 0;
2468 check_adc_timing(dev, cmd);
2470 select_master_clock(dev, cmd);
2473 convert_counter = ai_convert_counter_4020(dev, cmd);
2475 convert_counter = ai_convert_counter_6xxx(dev, cmd);
2476 scan_counter = ai_scan_counter_6xxx(dev, cmd);
2480 writew(convert_counter & 0xffff,
2482 DEBUG_PRINT(
"convert counter 0x%x\n", convert_counter);
2484 writew((convert_counter >> 16) & 0xff,
2487 writew(scan_counter & 0xffff,
2490 writew((scan_counter >> 16) & 0xff,
2495 static int use_internal_queue_6xxx(
const struct comedi_cmd *cmd)
2514 unsigned short bits;
2518 if (use_internal_queue_6xxx(cmd)) {
2526 bits |= ai_range_bits_6xxx(dev,
2529 bits |= se_diff_bit_6xxx(dev,
2544 warn_external_queue(dev);
2562 bits |= ai_range_bits_6xxx(dev,
2567 bits |= se_diff_bit_6xxx(dev,
2578 priv(dev)->main_iobase +
2581 (
"wrote 0x%x to external channel queue\n",
2591 unsigned short old_cal_range_bits =
2592 priv(dev)->i2c_cal_range_bits;
2596 priv(dev)->i2c_cal_range_bits |= adc_src_4020_bits(4);
2603 priv(dev)->i2c_cal_range_bits |=
2604 attenuate_bit(channel);
2606 priv(dev)->i2c_cal_range_bits &=
2607 ~attenuate_bit(channel);
2610 if (old_cal_range_bits !=
priv(dev)->i2c_cal_range_bits) {
2619 static inline void load_first_dma_descriptor(
struct comedi_device *dev,
2621 unsigned int descriptor_bits)
2653 unsigned long flags;
2656 disable_ai_pacing(dev);
2659 retval = setup_channel_queue(dev, cmd);
2666 set_ai_pacing(dev, cmd);
2668 setup_sample_counters(dev, cmd);
2670 enable_ai_interrupts(dev, cmd);
2679 priv(dev)->adc_control1_bits |= adc_mode_bits(13);
2681 priv(dev)->adc_control1_bits |= adc_mode_bits(8);
2689 priv(dev)->adc_control1_bits |=
2692 priv(dev)->adc_control1_bits |=
2700 spin_unlock_irqrestore(&dev->
spinlock, flags);
2707 priv(dev)->ai_dma_index = 0;
2710 for (i = 0; i < ai_dma_ring_count(
board(dev)); i++)
2711 priv(dev)->ai_dma_desc[
i].transfer_size =
2716 load_first_dma_descriptor(dev, 1,
2717 priv(dev)->ai_dma_desc_bus_addr |
2722 dma_start_sync(dev, 1);
2748 if (use_hw_sample_counter(cmd))
2753 priv(dev)->ai_cmd_running = 1;
2755 spin_unlock_irqrestore(&dev->
spinlock, flags);
2774 int read_segment, read_index, write_segment, write_index;
2791 read_segment = adc_upper_read_ptr_code(prepost_bits);
2792 write_segment = adc_upper_write_ptr_code(prepost_bits);
2794 DEBUG_PRINT(
" rd seg %i, wrt seg %i, rd idx %i, wrt idx %i\n",
2795 read_segment, write_segment, read_index,
2798 if (read_segment != write_segment)
2800 priv(dev)->ai_fifo_segment_length - read_index;
2802 num_samples = write_index - read_index;
2805 if (
priv(dev)->ai_count == 0)
2807 if (num_samples >
priv(dev)->ai_count)
2808 num_samples =
priv(dev)->ai_count;
2810 priv(dev)->ai_count -= num_samples;
2813 if (num_samples < 0) {
2815 "cb_pcidas64: bug! num_samples < 0\n");
2819 DEBUG_PRINT(
" read %i samples from fifo\n", num_samples);
2821 for (i = 0; i < num_samples; i++) {
2822 cfc_write_to_buffer(s,
2827 }
while (read_segment != write_segment);
2841 unsigned int max_transfer = 100000;
2849 if (max_transfer >
priv(dev)->ai_count)
2850 max_transfer =
priv(dev)->ai_count;
2853 for (i = 0; read_code != write_code && i < max_transfer;) {
2855 cfc_write_to_buffer(s, fifo_data & 0xffff);
2857 if (i < max_transfer) {
2858 cfc_write_to_buffer(s, (fifo_data >> 16) & 0xffff);
2864 priv(dev)->ai_count -=
i;
2871 pio_drain_ai_fifo_32(dev);
2873 pio_drain_ai_fifo_16(dev);
2876 static void drain_dma_buffers(
struct comedi_device *dev,
unsigned int channel)
2881 int num_samples = 0;
2892 for (j = 0, next_transfer_addr =
readl(pci_addr_reg);
2893 (next_transfer_addr <
2894 priv(dev)->ai_buffer_bus_addr[
priv(dev)->ai_dma_index]
2895 || next_transfer_addr >=
2896 priv(dev)->ai_buffer_bus_addr[
priv(dev)->ai_dma_index] +
2899 num_samples = dma_transfer_size(dev);
2901 if (num_samples >
priv(dev)->ai_count)
2902 num_samples =
priv(dev)->ai_count;
2903 priv(dev)->ai_count -= num_samples;
2909 priv(dev)->ai_dma_index =
2910 (
priv(dev)->ai_dma_index +
2911 1) % ai_dma_ring_count(
board(dev));
2914 (
unsigned long)
priv(dev)->
2915 ai_buffer_bus_addr[
priv(dev)->ai_dma_index]);
2916 DEBUG_PRINT(
"pci addr reg 0x%x\n", next_transfer_addr);
2924 unsigned int plx_status)
2930 unsigned long flags;
2945 if (dma1_status & PLX_DMA_EN_BIT)
2946 drain_dma_buffers(dev, 1);
2950 spin_unlock_irqrestore(&dev->
spinlock, flags);
2956 if ((status & ADC_DONE_BIT) ||
2962 if (
priv(dev)->ai_cmd_running) {
2963 spin_unlock_irqrestore(&dev->
spinlock, flags);
2964 pio_drain_ai_fifo(dev);
2966 spin_unlock_irqrestore(&dev->
spinlock, flags);
2977 static inline unsigned int prev_ao_dma_index(
struct comedi_device *dev)
2979 unsigned int buffer_index;
2981 if (
priv(dev)->ao_dma_index == 0)
2982 buffer_index = AO_DMA_RING_COUNT - 1;
2984 buffer_index =
priv(dev)->ao_dma_index - 1;
2985 return buffer_index;
2988 static int last_ao_dma_load_completed(
struct comedi_device *dev)
2990 unsigned int buffer_index;
2991 unsigned int transfer_address;
2994 buffer_index = prev_ao_dma_index(dev);
3001 if (transfer_address !=
priv(dev)->ao_buffer_bus_addr[buffer_index])
3013 if (
priv(dev)->ao_count)
3015 if (last_ao_dma_load_completed(dev) == 0)
3021 static inline int ao_dma_needs_restart(
struct comedi_device *dev,
3022 unsigned short dma_status)
3024 if ((dma_status & PLX_DMA_DONE_BIT) == 0 ||
3025 (dma_status & PLX_DMA_EN_BIT) == 0)
3027 if (last_ao_dma_load_completed(dev))
3035 unsigned int dma_desc_bits;
3040 DEBUG_PRINT(
"restarting ao dma, descriptor reg 0x%x\n", dma_desc_bits);
3041 load_first_dma_descriptor(dev, 0, dma_desc_bits);
3043 dma_start_sync(dev, 0);
3047 unsigned short status,
unsigned int plx_status)
3053 unsigned long flags;
3065 if ((dma0_status & PLX_DMA_EN_BIT)
3066 && !(dma0_status & PLX_DMA_DONE_BIT))
3072 spin_unlock_irqrestore(&dev->
spinlock, flags);
3074 if (dma0_status & PLX_DMA_EN_BIT) {
3075 load_ao_dma(dev, cmd);
3077 if (ao_dma_needs_restart(dev, dma0_status))
3078 restart_ao_dma(dev);
3082 spin_unlock_irqrestore(&dev->
spinlock, flags);
3086 if (ao_stopped_by_error(dev, cmd))
3108 DEBUG_PRINT(
"cb_pcidas64: hw status 0x%x ", status);
3115 DEBUG_PRINT(
"cb_pcidas64: premature interrupt, ignoring",
3119 handle_ai_interrupt(dev, status, plx_status);
3120 handle_ao_interrupt(dev, status, plx_status);
3126 DEBUG_PRINT(
" cleared local doorbell bits 0x%x\n", plx_bits);
3134 static void abort_dma(
struct comedi_device *dev,
unsigned int channel)
3136 unsigned long flags;
3141 plx9080_abort_dma(
priv(dev)->plx9080_iobase, channel);
3143 spin_unlock_irqrestore(&dev->
spinlock, flags);
3148 unsigned long flags;
3151 if (
priv(dev)->ai_cmd_running == 0) {
3152 spin_unlock_irqrestore(&dev->
spinlock, flags);
3155 priv(dev)->ai_cmd_running = 0;
3156 spin_unlock_irqrestore(&dev->
spinlock, flags);
3158 disable_ai_pacing(dev);
3176 set_dac_range_bits(dev, &
priv(dev)->dac_control1_bits, chan, range);
3183 priv(dev)->main_iobase + dac_lsb_4020_reg(chan));
3184 writew((data[0] >> 8) & 0xf,
3185 priv(dev)->main_iobase + dac_msb_4020_reg(chan));
3187 writew(data[0],
priv(dev)->main_iobase + dac_convert_reg(chan));
3191 priv(dev)->ao_value[chan] = data[0];
3236 set_dac_range_bits(dev, &
priv(dev)->dac_control1_bits, channel,
3252 if (last_channel < first_channel)
3253 comedi_error(dev,
"bug! last ao channel < first ao channel");
3255 bits = (first_channel & 0x7) | (last_channel & 0x7) << 3;
3260 static void set_dac_interval_regs(
struct comedi_device *dev,
3269 if (divisor > max_counter_value) {
3271 divisor = max_counter_value;
3275 writew((divisor >> 16) & 0xff,
3279 static unsigned int load_ao_dma_buffer(
struct comedi_device *dev,
3282 unsigned int num_bytes, buffer_index, prev_buffer_index;
3283 unsigned int next_bits;
3285 buffer_index =
priv(dev)->ao_dma_index;
3286 prev_buffer_index = prev_ao_dma_index(dev);
3288 DEBUG_PRINT(
"attempting to load ao buffer %i (0x%x)\n", buffer_index,
3289 priv(dev)->ao_buffer_bus_addr[buffer_index]);
3295 num_bytes =
priv(dev)->ao_count;
3296 num_bytes -= num_bytes % bytes_in_sample;
3305 ao_buffer[buffer_index],
3307 priv(dev)->ao_dma_desc[buffer_index].transfer_size =
3317 priv(dev)->ao_dma_desc[prev_buffer_index].next =
cpu_to_le32(next_bits);
3319 priv(dev)->ao_dma_index = (buffer_index + 1) % AO_DMA_RING_COUNT;
3328 unsigned int next_transfer_addr;
3331 unsigned int buffer_index;
3334 buffer_index =
priv(dev)->ao_dma_index;
3336 next_transfer_addr =
readl(pci_addr_reg);
3337 if (next_transfer_addr >=
3338 priv(dev)->ao_buffer_bus_addr[buffer_index]
3339 && next_transfer_addr <
3340 priv(dev)->ao_buffer_bus_addr[buffer_index] +
3343 num_bytes = load_ao_dma_buffer(dev, cmd);
3359 num_bytes / bytes_in_sample >
priv(dev)->ao_count)
3360 num_bytes =
priv(dev)->ao_count * bytes_in_sample;
3362 priv(dev)->ao_bounce_buffer,
3364 for (i = 0; i < num_bytes / bytes_in_sample; i++) {
3368 priv(dev)->ao_count -= num_bytes / bytes_in_sample;
3371 num_bytes = load_ao_dma_buffer(dev, cmd);
3375 load_ao_dma(dev, cmd);
3377 dma_start_sync(dev, 0);
3382 static inline int external_ai_queue_in_use(
struct comedi_device *dev)
3388 else if (use_internal_queue_6xxx(&dev->
read_subdev->async->cmd))
3397 if (external_ai_queue_in_use(dev)) {
3398 warn_external_queue(dev);
3404 priv(dev)->ao_dma_index = 0;
3407 set_dac_select_reg(dev, cmd);
3408 set_dac_interval_regs(dev, cmd);
3409 load_first_dma_descriptor(dev, 0,
priv(dev)->ao_dma_desc_bus_addr |
3412 set_dac_control1_reg(dev, cmd);
3413 s->
async->inttrig = ao_inttrig;
3419 unsigned int trig_num)
3427 retval = prep_ao_dma(dev, cmd);
3431 set_dac_control0_reg(dev, cmd);
3445 unsigned int tmp_arg;
3462 err |= cfc_check_trigger_is_unique(cmd->
start_src);
3484 cmd->
flags) > max_counter_value) {
3521 "chanlist must use consecutive channels");
3541 static int dio_callback(
int dir,
int port,
int data,
unsigned long arg)
3545 writeb(data, iobase + port);
3546 DEBUG_PRINT(
"wrote 0x%x to port %i\n", data, port);
3549 return readb(iobase + port);
3553 static int dio_callback_4020(
int dir,
int port,
int data,
unsigned long arg)
3557 writew(data, iobase + 2 * port);
3560 return readw(iobase + 2 * port);
3582 s->
state &= ~data[0];
3584 s->
state |= data[0] & data[1];
3625 s->
state &= ~data[0];
3626 s->
state |= (data[0] & data[1]);
3636 static void caldac_write(
struct comedi_device *dev,
unsigned int channel,
3644 caldac_8800_write(dev, channel, value);
3647 caldac_i2c_write(dev, channel, value);
3662 if (
priv(dev)->caldac_state[channel] == data[0])
3665 caldac_write(dev, channel, data[0]);
3681 static void ad8402_write(
struct comedi_device *dev,
unsigned int channel,
3684 static const int bitstream_length = 10;
3685 unsigned int bit, register_bits;
3686 unsigned int bitstream = ((channel & 0x3) << 8) | (value & 0xff);
3687 static const int ad8402_udelay = 1;
3695 for (bit = 1 << (bitstream_length - 1);
bit; bit >>= 1) {
3696 if (bitstream & bit)
3720 if (
priv(dev)->ad8402_state[channel] == data[0])
3725 ad8402_write(dev, channel, data[0]);
3743 static const int bitstream_length = 11;
3744 static const int read_command = 0x6;
3745 unsigned int bitstream = (read_command << 8) | address;
3747 void __iomem *
const plx_control_addr =
3751 static const int eeprom_udelay = 1;
3757 writel(
priv(dev)->plx_control_bits, plx_control_addr);
3761 writel(
priv(dev)->plx_control_bits, plx_control_addr);
3764 for (bit = 1 << (bitstream_length - 1);
bit; bit >>= 1) {
3767 if (bitstream & bit)
3771 writel(
priv(dev)->plx_control_bits, plx_control_addr);
3775 writel(
priv(dev)->plx_control_bits, plx_control_addr);
3778 writel(
priv(dev)->plx_control_bits, plx_control_addr);
3782 for (bit = 1 << (value_length - 1);
bit; bit >>= 1) {
3786 writel(
priv(dev)->plx_control_bits, plx_control_addr);
3789 writel(
priv(dev)->plx_control_bits, plx_control_addr);
3798 writel(
priv(dev)->plx_control_bits, plx_control_addr);
3818 unsigned int convert_divisor = 0, scan_divisor;
3819 static const int min_convert_divisor = 3;
3820 static const int max_convert_divisor =
3821 max_counter_value + min_convert_divisor;
3822 static const int min_scan_divisor_4020 = 2;
3823 unsigned long long max_scan_divisor, min_scan_divisor;
3831 if (convert_divisor > max_convert_divisor)
3832 convert_divisor = max_convert_divisor;
3833 if (convert_divisor < min_convert_divisor)
3834 convert_divisor = min_convert_divisor;
3844 min_scan_divisor = convert_divisor * cmd->
chanlist_len;
3849 min_scan_divisor = min_scan_divisor_4020;
3850 max_scan_divisor = max_counter_value + min_scan_divisor;
3852 if (scan_divisor > max_scan_divisor)
3853 scan_divisor = max_scan_divisor;
3854 if (scan_divisor < min_scan_divisor)
3855 scan_divisor = min_scan_divisor;
3865 static unsigned int get_divisor(
unsigned int ns,
unsigned int flags)
3884 static unsigned int get_ao_divisor(
unsigned int ns,
unsigned int flags)
3886 return get_divisor(ns, flags) - 2;
3890 static int set_ai_fifo_size(
struct comedi_device *dev,
unsigned int num_samples)
3892 unsigned int num_fifo_entries;
3898 retval = set_ai_fifo_segment_length(dev,
3906 DEBUG_PRINT(
"set hardware fifo size to %i\n", num_samples);
3914 return priv(dev)->ai_fifo_segment_length *
3915 board(dev)->ai_fifo->num_segments *
3916 board(dev)->ai_fifo->sample_packing_ratio;
3919 static int set_ai_fifo_segment_length(
struct comedi_device *dev,
3922 static const int increment_size = 0x100;
3924 unsigned int num_increments;
3927 if (num_entries < increment_size)
3928 num_entries = increment_size;
3933 num_increments = (num_entries + increment_size / 2) / increment_size;
3941 priv(dev)->ai_fifo_segment_length = num_increments * increment_size;
3943 DEBUG_PRINT(
"set hardware fifo segment length to %i\n",
3944 priv(dev)->ai_fifo_segment_length);
3946 return priv(dev)->ai_fifo_segment_length;
3970 static int caldac_8800_write(
struct comedi_device *dev,
unsigned int address,
3973 static const int num_caldac_channels = 8;
3974 static const int bitstream_length = 11;
3975 unsigned int bitstream = ((address & 0x7) << 8) |
value;
3976 unsigned int bit, register_bits;
3977 static const int caldac_8800_udelay = 1;
3979 if (address >= num_caldac_channels) {
3983 for (bit = 1 << (bitstream_length - 1);
bit; bit >>= 1) {
3985 if (bitstream & bit)
3987 udelay(caldac_8800_udelay);
3990 udelay(caldac_8800_udelay);
3993 udelay(caldac_8800_udelay);
3995 udelay(caldac_8800_udelay);
3997 udelay(caldac_8800_udelay);
4003 unsigned int caldac_channel,
unsigned int value)
4015 NOT_CLEAR_REGISTERS = 0x20,
4018 switch (caldac_channel) {
4021 serial_bytes[0] = OFFSET_0_2;
4025 serial_bytes[0] = OFFSET_1_3;
4029 serial_bytes[0] = OFFSET_0_2;
4033 serial_bytes[0] = OFFSET_1_3;
4037 serial_bytes[0] = GAIN_0_2;
4041 serial_bytes[0] = GAIN_1_3;
4045 serial_bytes[0] = GAIN_0_2;
4049 serial_bytes[0] = GAIN_1_3;
4056 serial_bytes[1] = NOT_CLEAR_REGISTERS | ((value >> 8) & 0xf);
4057 serial_bytes[2] = value & 0xff;
4058 i2c_write(dev, i2c_addr, serial_bytes, 3);
4063 static const int i2c_high_udelay = 1000;
4064 static const int i2c_low_udelay = 10;
4069 static const int data_bit =
CTL_EE_W;
4070 void __iomem *plx_control_addr =
priv(dev)->plx9080_iobase +
4075 priv(dev)->plx_control_bits &= ~data_bit;
4076 writel(
priv(dev)->plx_control_bits, plx_control_addr);
4080 priv(dev)->plx_control_bits |= data_bit;
4081 writel(
priv(dev)->plx_control_bits, plx_control_addr);
4087 static void i2c_set_scl(
struct comedi_device *dev,
int state)
4090 void __iomem *plx_control_addr =
priv(dev)->plx9080_iobase +
4095 priv(dev)->plx_control_bits &= ~clock_bit;
4096 writel(
priv(dev)->plx_control_bits, plx_control_addr);
4100 priv(dev)->plx_control_bits |= clock_bit;
4101 writel(
priv(dev)->plx_control_bits, plx_control_addr);
4109 unsigned int num_bits = 8;
4113 for (bit = 1 << (num_bits - 1);
bit; bit >>= 1) {
4114 i2c_set_scl(dev, 0);
4116 i2c_set_sda(dev, 1);
4118 i2c_set_sda(dev, 0);
4119 i2c_set_scl(dev, 1);
4126 i2c_set_scl(dev, 0);
4127 i2c_set_sda(dev, 1);
4128 i2c_set_scl(dev, 1);
4136 i2c_set_scl(dev, 1);
4137 i2c_set_sda(dev, 1);
4138 i2c_set_sda(dev, 0);
4144 i2c_set_scl(dev, 0);
4145 i2c_set_sda(dev, 0);
4146 i2c_set_scl(dev, 1);
4147 i2c_set_sda(dev, 1);
4166 bitstream = (address << 1) & ~read_bit;
4167 i2c_write_byte(dev, bitstream);
4170 if (i2c_read_ack(dev) != 0) {
4176 for (i = 0; i <
length; i++) {
4177 i2c_write_byte(dev, data[i]);
4178 if (i2c_read_ack(dev) != 0) {
4188 .driver_name =
"cb_pcidas64",
4229 static struct pci_driver cb_pcidas64_pci_driver = {
4230 .name =
"cb_pcidas64",
4231 .id_table = cb_pcidas64_pci_table,
4232 .probe = cb_pcidas64_pci_probe,