107 #include "../comedidev.h"
111 #define DRV_NAME "rtd520"
121 #define DMA_CHAIN_COUNT 2
126 #define TRANS_TARGET_PERIOD 10000000
130 #define RTD_MAX_CHANLIST 128
135 #define RTD_ADC_TIMEOUT 66000
136 #define RTD_DAC_TIMEOUT 66000
137 #define RTD_DMA_TIMEOUT 33000
140 #define WAIT_QUIETLY udelay(1)
141 #define RTD_ADC_TIMEOUT 2000
142 #define RTD_DAC_TIMEOUT 2000
143 #define RTD_DMA_TIMEOUT 1000
151 #define PCI_VENDOR_ID_RTD 0x1435
156 #define LCFG_PCIINDEX 0
158 #define LAS0_PCIINDEX 2
159 #define LAS1_PCIINDEX 3
160 #define LCFG_PCISIZE 0x100
161 #define LAS0_PCISIZE 0x200
162 #define LAS1_PCISIZE 0x10
164 #define RTD_CLOCK_RATE 8000000
165 #define RTD_CLOCK_BASE 125
168 #define RTD_MAX_SPEED 1625
170 #define RTD_MAX_SPEED_1 875
172 #define RTD_MIN_SPEED 2097151875
174 #define RTD_MIN_SPEED_1 5000000
180 #define DMA_MODE_BITS (\
181 PLX_LOCAL_BUS_16_WIDE_BITS \
182 | PLX_DMA_EN_READYIN_BIT \
183 | PLX_DMA_LOCAL_BURST_EN_BIT \
185 | PLX_DMA_INTR_PCI_BIT \
186 | PLX_LOCAL_ADDR_CONST_BIT \
187 | PLX_DEMAND_MODE_BIT)
189 #define DMA_TRANSFER_BITS (\
190 PLX_DESC_IN_PCI_BIT \
191 | PLX_INTR_TERM_COUNT \
192 | PLX_XFER_LOCAL_TO_PCI)
280 static const struct rtdBoard rtd520Boards[] = {
350 #define SEND_EOS 0x01
351 #define DMA0_ACTIVE 0x02
352 #define DMA1_ACTIVE 0x04
355 #define CHAN_ARRAY_TEST(array, index) \
356 (((array)[(index)/8] >> ((index) & 0x7)) & 0x1)
357 #define CHAN_ARRAY_SET(array, index) \
358 (((array)[(index)/8] |= 1 << ((index) & 0x7)))
359 #define CHAN_ARRAY_CLEAR(array, index) \
360 (((array)[(index)/8] &= ~(1 << ((index) & 0x7))))
368 static int rtd_ns_to_timer_base(
unsigned int *nanosec,
369 int round_mode,
int base)
373 switch (round_mode) {
376 divider = (*nanosec + base / 2) / base;
379 divider = (*nanosec) / base;
382 divider = (*nanosec + base - 1) / base;
391 *nanosec = base * divider;
400 static int rtd_ns_to_timer(
unsigned int *
ns,
int round_mode)
409 unsigned int comediChan,
int chanIndex)
414 unsigned short r = 0;
425 r |= (range & 0x7) << 4;
462 static void rtd_load_channelgain_list(
struct comedi_device *dev,
463 unsigned int n_chan,
unsigned int *
list)
472 for (ii = 0; ii < n_chan; ii++) {
473 writel(rtdConvertChanGain(dev, list[ii], ii),
478 writel(rtdConvertChanGain(dev, list[0], 0),
485 static int rtd520_probe_fifo_depth(
struct comedi_device *dev)
490 static const unsigned limit = 0x2000;
494 rtd_load_channelgain_list(dev, 1, &chanspec);
498 for (i = 0; i <
limit; ++
i) {
499 unsigned fifo_status;
515 if (fifo_size != 0x400 && fifo_size != 0x2000) {
517 (
KERN_INFO "\ncomedi: %s: unexpected fifo size of %i, expected 1024 or 8192.\n",
544 rtd_load_channelgain_list(dev, 1, &insn->
chanspec);
550 for (n = 0; n < insn->
n; n++) {
561 if (ii >= RTD_ADC_TIMEOUT) {
563 (
"rtd520: Error: ADC never finished! FifoStatus=0x%x\n",
595 for (ii = 0; ii <
count; ii++) {
605 DPRINTK(
"comedi: READ OOPS on %d of %d\n", ii + 1,
666 unsigned long dma_cs_addr;
671 dma_cs_addr = (
unsigned long)devpriv->
lcfg
678 status =
readb(dma_cs_addr);
680 DPRINTK(
"rtd520: AbortDma on non-active channel %d (0x%x)\n",
688 status =
readb(dma_cs_addr);
691 printk(
"rtd520: Timeout waiting for dma %i done clear\n",
703 status =
readb(dma_cs_addr);
706 status =
readb(dma_cs_addr);
709 if ((status & PLX_DMA_DONE_BIT) == 0) {
710 printk(
"rtd520: Timeout waiting for dma %i done set\n",
731 dp = devpriv->dma0Buff[devpriv->dma0Offset];
732 for (ii = 0; ii < devpriv->
fifoLen / 2;) {
736 sample = (*dp >> 3) + 2048;
742 if (++s->
async->cur_chan >= s->
async->cmd.chanlist_len)
743 s->
async->cur_chan = 0;
755 dp = devpriv->dma0Buff[devpriv->dma0Offset];
757 if (n < (ii *
sizeof(
s16))) {
758 DPRINTK(
"rtd520:ai_process_dma buffer overflow %d samples!\n",
759 ii - (n /
sizeof(
s16)));
772 devpriv->dma0Offset = 0;
802 DPRINTK(
"rtd520: FIFO full! fifo_status=0x%x\n", (fifoStatus ^ 0x6666) & 0x7777);
810 if (ai_process_dma(dev, s) < 0) {
812 (
"rtd520: comedi read buffer overflow (DMA) with %ld to go!\n",
816 writeb(devpriv->dma0Control,
825 writeb(devpriv->dma0Control,
828 DPRINTK(
"rtd520: Samples Done (DMA).\n");
851 if (ai_read_n(dev, s, devpriv->
fifoLen / 2) < 0) {
853 (
"rtd520: comedi read buffer overflow (1/2FIFO) with %ld to go!\n",
858 DPRINTK(
"rtd520: Samples Done (1/2). fifo_status was 0x%x\n", (fifoStatus ^ 0x6666) & 0x7777);
865 if (fifoStatus & FS_ADC_NOT_EMPTY) {
866 if (ai_read_n(dev, s, devpriv->
transCount) < 0) {
868 (
"rtd520: comedi read buffer overflow (N) with %ld to go!\n",
874 (
"rtd520: Samples Done (N). fifo_status was 0x%x\n",
875 (fifoStatus ^ 0x6666) & 0x7777);
882 (
"rtd520: Sample int. Wait for 1/2. fifo_status 0x%x\n",
883 (fifoStatus ^ 0x6666) & 0x7777);
886 DPRINTK(
"rtd520: unknown interrupt source!\n");
892 (
"rtd520: Interrupt overrun with %ld to go! over_status=0x%x\n",
924 DPRINTK(
"rtd520: Lost DMA data! %ld remain\n",
932 DPRINTK(
"rtd520: Finishing up. %ld remain, fifoStat=%x\n", devpriv->
aiCount, (fifoStatus ^ 0x6666) & 0x7777);
933 ai_read_dregs(dev, s);
948 (
"rtd520: Acquisition complete. %ld ints, intStat=%x, overStat=%x\n",
949 devpriv->
intCount, status, overrun);
962 return s->
async->buf_write_count - s->
async->buf_read_count;
996 err |= cfc_check_trigger_is_unique(cmd->
convert_src);
997 err |= cfc_check_trigger_is_unique(cmd->
stop_src);
1170 writeb(devpriv->dma0Control,
1181 DPRINTK(
"rtd520: ERROR! No interrupt available!\n");
1244 (
"rtd520: scanLen=%d transferCount=%d fifoLen=%d\n scanTime(ns)=%d flags=0x%x\n",
1273 DPRINTK(
"rtd520: Warning! ignoring stop_src mode %d\n",
1294 DPRINTK(
"rtd520: Warning! ignoring scan_begin_src mode %d\n",
1317 DPRINTK(
"rtd520: Warning! ignoring convert_src mode %d\n",
1338 devpriv->dma0Offset = 0;
1348 writeb(devpriv->dma0Control,
1351 writeb(devpriv->dma0Control,
1353 DPRINTK(
"rtd520: Using DMA0 transfers. plxInt %x RtdInt %x\n",
1358 DPRINTK(
"rtd520: Transferring every 1/2 FIFO\n");
1395 (
"rtd520: Acquisition canceled. %ld ints, intStat=%x, overStat=%x\n",
1396 devpriv->
intCount, status, overrun);
1418 for (i = 0; i < insn->
n; ++
i) {
1419 int val = data[
i] << 3;
1426 && (data[i] < 2048)) {
1428 val = (((
int)data[i]) - 2048) << 3;
1434 (
"comedi: rtd520 DAC chan=%d range=%d writing %d as 0x%x\n",
1435 chan, range, data[i], val);
1452 if (ii >= RTD_DAC_TIMEOUT) {
1454 (
"rtd520: Error: DAC never finished! FifoStatus=0x%x\n",
1474 for (i = 0; i < insn->
n; i++)
1475 data[i] = devpriv->
aoValue[chan];
1500 s->
state &= ~data[0];
1501 s->
state |= data[0] & data[1];
1545 DPRINTK(
"rtd520: port_0_direction=0x%x (1 means out)\n", s->
io_bits);
1573 if (pcidev->
bus->number != bus ||
1577 for (i = 0; i <
ARRAY_SIZE(rtd520Boards); i++) {
1578 thisboard = &rtd520Boards[
i];
1586 "no supported board found! (req. bus/slot: %d/%d)\n",
1605 #if defined(CONFIG_COMEDI_DEBUG) && defined(USE_DMA)
1607 if (0 == comedi_debug)
1615 if (alloc_private(dev,
sizeof(
struct rtdPrivate)) < 0)
1619 pcidev = rtd_find_pci(dev, it);
1622 comedi_set_hw_dev(dev, &pcidev->
dev);
1623 thisboard = comedi_board(dev);
1629 printk(
KERN_INFO "Failed to enable PCI device and request regions.\n");
1641 if (!devpriv->
las0 || !devpriv->
las1 || !devpriv->
lcfg)
1645 unsigned char pci_latency;
1653 pci_read_config_byte(pcidev,
1655 if (pci_latency < 32) {
1658 pci_write_config_byte(pcidev,
1661 DPRINTK(
"rtd520: PCI latency = %d\n", pci_latency);
1701 s->
cancel = rtd_ai_cancel;
1750 devpriv->
utcCtrl[0] = (0 << 6) | 0x30;
1751 devpriv->
utcCtrl[1] = (1 << 6) | 0x30;
1752 devpriv->
utcCtrl[2] = (2 << 6) | 0x30;
1753 devpriv->
utcCtrl[3] = (3 << 6) | 0x00;
1765 printk(
"Could not get interrupt! (%u)\n",
1772 ret = rtd520_probe_fifo_depth(dev);
1787 devpriv->dma0Offset = 0;
1790 devpriv->dma0Buff[
index] =
1795 dma0BuffPhysAddr[index]);
1796 if (devpriv->dma0Buff[index] ==
NULL) {
1798 goto rtd_attach_die_error;
1810 devpriv->dma0Chain =
1814 &devpriv->dma0ChainPhysAddr);
1816 devpriv->dma0Chain[
index].pci_start_addr =
1817 devpriv->dma0BuffPhysAddr[
index];
1818 devpriv->dma0Chain[
index].local_start_addr =
1820 devpriv->dma0Chain[
index].transfer_size =
1822 devpriv->dma0Chain[index].next =
1823 (devpriv->dma0ChainPhysAddr + ((index +
1826 *
sizeof(devpriv->dma0Chain[0]))
1838 if (devpriv->dma0Chain ==
NULL) {
1840 goto rtd_attach_die_error;
1854 printk(
"\ncomedi%d: rtd520 driver attached.\n", dev->
minor);
1862 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
1870 if (devpriv->
lcfg) {
1871 devpriv->dma0Control = 0;
1872 devpriv->dma1Control = 0;
1873 writeb(devpriv->dma0Control,
1875 writeb(devpriv->dma1Control,
1880 if (devpriv->
las0) {
1892 if (
NULL != devpriv->dma0Buff[index]) {
1896 devpriv->dma0Buff[index],
1898 dma0BuffPhysAddr[index]);
1902 if (
NULL != devpriv->dma0Chain) {
1905 DMA_CHAIN_COUNT, devpriv->dma0Chain,
1906 devpriv->dma0ChainPhysAddr);
1907 devpriv->dma0Chain =
NULL;
1931 .driver_name =
"rtd520",
1933 .attach = rtd_attach,
1934 .detach = rtd_detach,
1955 static struct pci_driver rtd520_pci_driver = {
1957 .id_table = rtd520_pci_table,
1958 .probe = rtd520_pci_probe,