77 #include <linux/slab.h>
79 #include "../comedidev.h"
90 #define DRV_NAME "ni_labpc"
95 #define LABPC_TIMER_BASE 500
100 #define COMMAND1_REG 0x0
101 #define ADC_GAIN_MASK (0x7 << 4)
102 #define ADC_CHAN_BITS(x) ((x) & 0x7)
104 #define ADC_SCAN_EN_BIT 0x80
105 #define COMMAND2_REG 0x1
107 #define PRETRIG_BIT 0x1
109 #define HWTRIG_BIT 0x2
111 #define SWTRIG_BIT 0x4
113 #define CASCADE_BIT 0x8
114 #define DAC_PACED_BIT(channel) (0x40 << ((channel) & 0x1))
115 #define COMMAND3_REG 0x2
117 #define DMA_EN_BIT 0x1
119 #define DIO_INTR_EN_BIT 0x2
121 #define DMATC_INTR_EN_BIT 0x4
123 #define TIMER_INTR_EN_BIT 0x8
125 #define ERR_INTR_EN_BIT 0x10
127 #define ADC_FNE_INTR_EN_BIT 0x20
128 #define ADC_CONVERT_REG 0x3
129 #define DAC_LSB_REG(channel) (0x4 + 2 * ((channel) & 0x1))
130 #define DAC_MSB_REG(channel) (0x5 + 2 * ((channel) & 0x1))
131 #define ADC_CLEAR_REG 0x8
132 #define DMATC_CLEAR_REG 0xa
133 #define TIMER_CLEAR_REG 0xc
135 #define COMMAND6_REG 0xe
137 #define ADC_COMMON_BIT 0x1
139 #define ADC_UNIP_BIT 0x2
141 #define DAC_UNIP_BIT(channel) (0x4 << ((channel) & 0x1))
143 #define ADC_FHF_INTR_EN_BIT 0x20
145 #define A1_INTR_EN_BIT 0x40
147 #define ADC_SCAN_UP_BIT 0x80
148 #define COMMAND4_REG 0xf
150 #define INTERVAL_SCAN_EN_BIT 0x1
152 #define EXT_SCAN_EN_BIT 0x2
154 #define EXT_CONVERT_OUT_BIT 0x4
156 #define ADC_DIFF_BIT 0x8
157 #define EXT_CONVERT_DISABLE_BIT 0x10
159 #define COMMAND5_REG 0x1c
161 #define EEPROM_WRITE_UNPROTECT_BIT 0x4
163 #define DITHER_EN_BIT 0x8
165 #define CALDAC_LOAD_BIT 0x10
167 #define SCLOCK_BIT 0x20
169 #define SDATA_BIT 0x40
171 #define EEPROM_EN_BIT 0x80
172 #define INTERVAL_COUNT_REG 0x1e
173 #define INTERVAL_LOAD_REG 0x1f
174 #define INTERVAL_LOAD_BITS 0x1
177 #define STATUS1_REG 0x0
179 #define DATA_AVAIL_BIT 0x1
181 #define OVERRUN_BIT 0x2
183 #define OVERFLOW_BIT 0x4
185 #define TIMER_BIT 0x8
187 #define DMATC_BIT 0x10
189 #define EXT_TRIG_BIT 0x40
191 #define STATUS2_REG 0x1d
193 #define EEPROM_OUT_BIT 0x1
195 #define A1_TC_BIT 0x2
198 #define ADC_FIFO_REG 0xa
200 #define DIO_BASE_REG 0x10
201 #define COUNTER_A_BASE_REG 0x14
202 #define COUNTER_A_CONTROL_REG (COUNTER_A_BASE_REG + 0x3)
204 #define INIT_A0_BITS 0x14
206 #define INIT_A1_BITS 0x70
207 #define COUNTER_B_BASE_REG 0x18
219 #ifdef CONFIG_ISA_DMA_API
248 #ifdef CONFIG_ISA_DMA_API
249 static unsigned int labpc_suggest_transfer_size(
const struct comedi_cmd *
cmd);
251 static int labpc_dio_mem_callback(
int dir,
int port,
int data,
254 unsigned int num_bits);
266 #define NUM_LABPC_PLUS_AI_RANGES 16
329 #define NUM_LABPC_1200_AI_RANGES 14
390 #define AO_RANGE_IS_UNIPOLAR 0x1
401 static inline unsigned int labpc_inb(
unsigned long address)
406 static inline void labpc_outb(
unsigned int byte,
unsigned long address)
411 static inline unsigned int labpc_readb(
unsigned long address)
416 static inline void labpc_writeb(
unsigned int byte,
unsigned long address)
423 .name =
"lab-pc-1200",
432 .memory_mapped_io = 0,
435 .name =
"lab-pc-1200ai",
444 .memory_mapped_io = 0,
452 .ai_range_table = &range_labpc_plus_ai,
453 .ai_range_code = labpc_plus_ai_gain_bits,
454 .ai_range_is_unipolar = labpc_plus_is_unipolar,
456 .memory_mapped_io = 0,
458 #ifdef CONFIG_COMEDI_PCI_DRIVERS
470 .memory_mapped_io = 1,
483 #define thisboard ((struct labpc_board_struct *)dev->board_ptr)
486 static const int dma_buffer_size = 0xff00;
490 #define devpriv ((struct labpc_private *)dev->private)
494 unsigned int counter_number,
498 return i8254_mm_load((
void __iomem *)base_address, 0,
499 counter_number, count, mode);
501 return i8254_load(base_address, 0, counter_number, count, mode);
505 unsigned int irq,
unsigned int dma_chan)
509 unsigned long isr_flags;
510 #ifdef CONFIG_ISA_DMA_API
511 unsigned long dma_flags;
532 devpriv->read_byte = labpc_readb;
533 devpriv->write_byte = labpc_writeb;
535 devpriv->read_byte = labpc_inb;
536 devpriv->write_byte = labpc_outb;
565 #ifdef CONFIG_ISA_DMA_API
570 }
else if (dma_chan) {
576 "failed to allocate dma buffer\n");
581 "failed to allocate dma channel %u\n",
630 for (i = 0; i < s->
n_chan; i++) {
633 msb = (
devpriv->ao_value[
i] >> 8) & 0xff;
661 for (i = 0; i < s->
n_chan; i++)
662 write_caldac(dev, i, s->
maxdata / 2);
677 devpriv->eeprom_data[i] = labpc_eeprom_read(dev, i);
691 for (n = 0; n <
ARRAY_SIZE(labpc_boards); n++) {
712 dev->
board_ptr = labpc_pci_find_boardinfo(pcidev);
721 iobase = (
unsigned long)
devpriv->mite->daq_io_addr;
728 unsigned long iobase = 0;
729 unsigned int irq = 0;
739 #ifdef CONFIG_ISA_DMA_API
745 "ni_labpc driver has not been built with ISA DMA support.\n");
750 #ifdef CONFIG_COMEDI_PCI_DRIVERS
752 "manual configuration of PCI board '%s' is not supported\n",
757 "ni_labpc driver has not been built with PCI support.\n");
763 "ni_labpc: bug! couldn't determine board type\n");
781 #ifdef CONFIG_ISA_DMA_API
791 #ifdef CONFIG_COMEDI_PCI_DRIVERS
800 static void labpc_clear_adc_fifo(
const struct comedi_device *dev)
814 spin_unlock_irqrestore(&dev->
spinlock, flags);
840 pr_err(
"ni_labpc: bug! cannot determine AI scan mode\n");
844 static int labpc_ai_chanlist_invalid(
const struct comedi_device *dev,
859 "ni_labpc: chanlist too long for single channel interval mode\n");
874 "channel scanning order specified in chanlist is not supported by hardware.\n");
881 "channel scanning order specified in chanlist is not supported by hardware.\n");
889 "channel scanning order specified in chanlist is not supported by hardware.\n");
895 "ni_labpc: bug! in chanlist check\n");
902 "entries in chanlist must all have the same range\n");
908 "entries in chanlist must all have the same reference\n");
916 static int labpc_use_continuous_mode(
const struct comedi_cmd *cmd,
928 static unsigned int labpc_ai_convert_period(
const struct comedi_cmd *cmd,
940 static void labpc_set_ai_convert_period(
struct comedi_cmd *cmd,
955 static unsigned int labpc_ai_scan_period(
const struct comedi_cmd *cmd,
967 static void labpc_set_ai_scan_period(
struct comedi_cmd *cmd,
984 unsigned int stop_mask;
998 err |= cfc_check_trigger_src(&cmd->
stop_src, stop_mask);
1005 err |= cfc_check_trigger_is_unique(cmd->
start_src);
1007 err |= cfc_check_trigger_is_unique(cmd->
convert_src);
1008 err |= cfc_check_trigger_is_unique(cmd->
stop_src);
1085 mode = labpc_ai_scan_mode(cmd);
1086 labpc_adc_timing(dev, cmd, mode);
1093 if (labpc_ai_chanlist_invalid(dev, cmd, mode))
1102 #ifdef CONFIG_ISA_DMA_API
1103 unsigned long irq_flags;
1110 unsigned long flags;
1113 comedi_error(dev,
"no irq assigned, cannot perform command");
1124 spin_unlock_irqrestore(&dev->
spinlock, flags);
1152 #ifdef CONFIG_ISA_DMA_API
1174 devpriv->current_transfer = xfer;
1175 mode = labpc_ai_scan_mode(cmd);
1185 if (
thisboard->ai_range_is_unipolar[range])
1237 if (labpc_use_continuous_mode(cmd, mode) == 0) {
1255 labpc_adc_timing(dev, cmd, mode);
1265 if (labpc_ai_convert_period(cmd, mode)) {
1278 if (labpc_ai_scan_period(cmd, mode)) {
1288 labpc_clear_adc_fifo(dev);
1290 #ifdef CONFIG_ISA_DMA_API
1297 clear_dma_ff(
devpriv->dma_chan);
1298 set_dma_addr(
devpriv->dma_chan,
1301 devpriv->dma_transfer_size = labpc_suggest_transfer_size(cmd);
1303 devpriv->count * sample_size < devpriv->dma_transfer_size) {
1342 spin_unlock_irqrestore(&dev->
spinlock, flags);
1355 spin_unlock_irqrestore(&dev->
spinlock, flags);
1359 spin_unlock_irqrestore(&dev->
spinlock, flags);
1403 #ifdef CONFIG_ISA_DMA_API
1412 handle_isa_dma(dev);
1416 labpc_drain_fifo(dev);
1435 labpc_drain_dregs(dev);
1436 labpc_cancel(dev, s);
1444 labpc_cancel(dev, s);
1474 data = (msb << 8) | lsb;
1488 #ifdef CONFIG_ISA_DMA_API
1494 unsigned long flags;
1495 unsigned int max_points, num_points, residue, leftover;
1498 status =
devpriv->status1_bits;
1504 clear_dma_ff(
devpriv->dma_chan);
1513 num_points = max_points - residue;
1521 }
else if (
devpriv->count > num_points) {
1522 leftover =
devpriv->count - num_points;
1523 if (leftover > max_points)
1524 leftover = max_points;
1528 for (i = 0; i < num_points; i++)
1529 cfc_write_to_buffer(s,
devpriv->dma_buffer[i]);
1544 labpc_drain_dma(dev);
1557 #ifdef CONFIG_ISA_DMA_API
1559 labpc_drain_dma(dev);
1562 labpc_drain_fifo(dev);
1572 unsigned long flags;
1578 spin_unlock_irqrestore(&dev->
spinlock, flags);
1603 if (
thisboard->ai_range_is_unipolar[range])
1629 labpc_clear_adc_fifo(dev);
1631 for (n = 0; n < insn->
n; n++) {
1635 for (i = 0; i < timeout; i++) {
1647 data[
n] = (msb << 8) | lsb;
1658 unsigned long flags;
1669 spin_unlock_irqrestore(&dev->
spinlock, flags);
1683 lsb = data[0] & 0xff;
1684 msb = (data[0] >> 8) & 0xff;
1712 static int labpc_calib_write_insn(
struct comedi_device *dev,
1718 write_caldac(dev, channel, data[0]);
1722 static int labpc_eeprom_read_insn(
struct comedi_device *dev,
1731 static int labpc_eeprom_write_insn(
struct comedi_device *dev,
1741 "eeprom writes are only allowed to channels 16 through 127 (the pointer and user areas)\n");
1745 ret = labpc_eeprom_write(dev, channel, data[0]);
1752 #ifdef CONFIG_ISA_DMA_API
1754 static unsigned int labpc_suggest_transfer_size(
const struct comedi_cmd *cmd)
1766 size = (freq / 3) * sample_size;
1769 if (size > dma_buffer_size)
1770 size = dma_buffer_size - dma_buffer_size %
sample_size;
1771 else if (size < sample_size)
1783 const int max_counter_value = 0x10000;
1785 const int min_counter_value = 2;
1786 unsigned int base_period;
1787 unsigned int scan_period;
1788 unsigned int convert_period;
1794 convert_period = labpc_ai_convert_period(cmd, mode);
1795 scan_period = labpc_ai_scan_period(cmd, mode);
1796 if (convert_period && scan_period) {
1801 devpriv->divisor_b0 = (scan_period - 1) /
1803 if (
devpriv->divisor_b0 < min_counter_value)
1804 devpriv->divisor_b0 = min_counter_value;
1805 if (
devpriv->divisor_b0 > max_counter_value)
1806 devpriv->divisor_b0 = max_counter_value;
1815 (convert_period + (base_period / 2)) / base_period;
1817 (scan_period + (base_period / 2)) / base_period;
1821 (convert_period + (base_period - 1)) / base_period;
1823 (scan_period + (base_period - 1)) / base_period;
1826 devpriv->divisor_a0 = convert_period / base_period;
1827 devpriv->divisor_b1 = scan_period / base_period;
1831 if (
devpriv->divisor_a0 < min_counter_value)
1832 devpriv->divisor_a0 = min_counter_value;
1833 if (
devpriv->divisor_a0 > max_counter_value)
1834 devpriv->divisor_a0 = max_counter_value;
1835 if (
devpriv->divisor_b1 < min_counter_value)
1836 devpriv->divisor_b1 = min_counter_value;
1837 if (
devpriv->divisor_b1 > max_counter_value)
1838 devpriv->divisor_b1 = max_counter_value;
1840 labpc_set_ai_convert_period(cmd, mode,
1841 base_period *
devpriv->divisor_a0);
1842 labpc_set_ai_scan_period(cmd, mode,
1843 base_period *
devpriv->divisor_b1);
1848 }
else if (scan_period) {
1858 labpc_set_ai_scan_period(cmd, mode, scan_period);
1859 }
else if (convert_period) {
1869 labpc_set_ai_convert_period(cmd, mode, convert_period);
1873 static int labpc_dio_mem_callback(
int dir,
int port,
int data,
1874 unsigned long iobase)
1886 unsigned int value_width)
1890 for (i = 1; i <= value_width; i++) {
1894 if (value & (1 << (value_width - i)))
1910 static unsigned int labpc_serial_in(
struct comedi_device *dev)
1912 unsigned int value = 0;
1914 const int value_width = 8;
1916 for (i = 1; i <= value_width; i++) {
1932 value |= 1 << (value_width -
i);
1938 static unsigned int labpc_eeprom_read(
struct comedi_device *dev,
1939 unsigned int address)
1943 const int read_instruction = 0x3;
1945 const int write_length = 8;
1956 labpc_serial_out(dev, read_instruction, write_length);
1958 labpc_serial_out(dev, address, write_length);
1960 value = labpc_serial_in(dev);
1971 unsigned int address,
unsigned int value)
1973 const int write_enable_instruction = 0x6;
1974 const int write_instruction = 0x2;
1975 const int write_length = 8;
1976 const int write_in_progress_bit = 0x1;
1977 const int timeout = 10000;
1981 for (i = 0; i < timeout; i++) {
1982 if ((labpc_eeprom_read_status(dev) & write_in_progress_bit) ==
2002 labpc_serial_out(dev, write_enable_instruction, write_length);
2011 labpc_serial_out(dev, write_instruction, write_length);
2013 labpc_serial_out(dev, address, write_length);
2015 labpc_serial_out(dev, value, write_length);
2028 static unsigned int labpc_eeprom_read_status(
struct comedi_device *dev)
2031 const int read_status_instruction = 0x5;
2032 const int write_length = 8;
2043 labpc_serial_out(dev, read_status_instruction, write_length);
2045 value = labpc_serial_in(dev);
2056 static void write_caldac(
struct comedi_device *dev,
unsigned int channel,
2059 if (value ==
devpriv->caldac[channel])
2070 labpc_serial_out(dev, channel, 4);
2072 labpc_serial_out(dev, value, 8);
2086 .attach = labpc_attach,
2087 .attach_pci = labpc_attach_pci,
2090 .board_name = &labpc_boards[0].
name,
2094 #ifdef CONFIG_COMEDI_PCI_DRIVERS
2112 static struct pci_driver labpc_pci_driver = {
2114 .id_table = labpc_pci_table,
2115 .probe = labpc_pci_probe,