18ffdff6aSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
28ffdff6aSGreg Kroah-Hartman /*
38ffdff6aSGreg Kroah-Hartman * comedi/drivers/ni_labpc_pci.c
48ffdff6aSGreg Kroah-Hartman * Driver for National Instruments Lab-PC PCI-1200
58ffdff6aSGreg Kroah-Hartman * Copyright (C) 2001, 2002, 2003 Frank Mori Hess <fmhess@users.sourceforge.net>
68ffdff6aSGreg Kroah-Hartman */
78ffdff6aSGreg Kroah-Hartman
88ffdff6aSGreg Kroah-Hartman /*
98ffdff6aSGreg Kroah-Hartman * Driver: ni_labpc_pci
108ffdff6aSGreg Kroah-Hartman * Description: National Instruments Lab-PC PCI-1200
118ffdff6aSGreg Kroah-Hartman * Devices: [National Instruments] PCI-1200 (ni_pci-1200)
128ffdff6aSGreg Kroah-Hartman * Author: Frank Mori Hess <fmhess@users.sourceforge.net>
138ffdff6aSGreg Kroah-Hartman * Status: works
148ffdff6aSGreg Kroah-Hartman *
158ffdff6aSGreg Kroah-Hartman * This is the PCI-specific support split off from the ni_labpc driver.
168ffdff6aSGreg Kroah-Hartman *
178ffdff6aSGreg Kroah-Hartman * Configuration Options: not applicable, uses PCI auto config
188ffdff6aSGreg Kroah-Hartman *
198ffdff6aSGreg Kroah-Hartman * NI manuals:
208ffdff6aSGreg Kroah-Hartman * 340914a (pci-1200)
218ffdff6aSGreg Kroah-Hartman */
228ffdff6aSGreg Kroah-Hartman
238ffdff6aSGreg Kroah-Hartman #include <linux/module.h>
248ffdff6aSGreg Kroah-Hartman #include <linux/interrupt.h>
25*df0e68c1SIan Abbott #include <linux/comedi/comedi_pci.h>
268ffdff6aSGreg Kroah-Hartman
278ffdff6aSGreg Kroah-Hartman #include "ni_labpc.h"
288ffdff6aSGreg Kroah-Hartman
298ffdff6aSGreg Kroah-Hartman enum labpc_pci_boardid {
308ffdff6aSGreg Kroah-Hartman BOARD_NI_PCI1200,
318ffdff6aSGreg Kroah-Hartman };
328ffdff6aSGreg Kroah-Hartman
338ffdff6aSGreg Kroah-Hartman static const struct labpc_boardinfo labpc_pci_boards[] = {
348ffdff6aSGreg Kroah-Hartman [BOARD_NI_PCI1200] = {
358ffdff6aSGreg Kroah-Hartman .name = "ni_pci-1200",
368ffdff6aSGreg Kroah-Hartman .ai_speed = 10000,
378ffdff6aSGreg Kroah-Hartman .ai_scan_up = 1,
388ffdff6aSGreg Kroah-Hartman .has_ao = 1,
398ffdff6aSGreg Kroah-Hartman .is_labpc1200 = 1,
408ffdff6aSGreg Kroah-Hartman },
418ffdff6aSGreg Kroah-Hartman };
428ffdff6aSGreg Kroah-Hartman
438ffdff6aSGreg Kroah-Hartman /* ripped from mite.h and mite_setup2() to avoid mite dependency */
448ffdff6aSGreg Kroah-Hartman #define MITE_IODWBSR 0xc0 /* IO Device Window Base Size Register */
458ffdff6aSGreg Kroah-Hartman #define WENAB BIT(7) /* window enable */
468ffdff6aSGreg Kroah-Hartman
labpc_pci_mite_init(struct pci_dev * pcidev)478ffdff6aSGreg Kroah-Hartman static int labpc_pci_mite_init(struct pci_dev *pcidev)
488ffdff6aSGreg Kroah-Hartman {
498ffdff6aSGreg Kroah-Hartman void __iomem *mite_base;
508ffdff6aSGreg Kroah-Hartman u32 main_phys_addr;
518ffdff6aSGreg Kroah-Hartman
528ffdff6aSGreg Kroah-Hartman /* ioremap the MITE registers (BAR 0) temporarily */
538ffdff6aSGreg Kroah-Hartman mite_base = pci_ioremap_bar(pcidev, 0);
548ffdff6aSGreg Kroah-Hartman if (!mite_base)
558ffdff6aSGreg Kroah-Hartman return -ENOMEM;
568ffdff6aSGreg Kroah-Hartman
578ffdff6aSGreg Kroah-Hartman /* set data window to main registers (BAR 1) */
588ffdff6aSGreg Kroah-Hartman main_phys_addr = pci_resource_start(pcidev, 1);
598ffdff6aSGreg Kroah-Hartman writel(main_phys_addr | WENAB, mite_base + MITE_IODWBSR);
608ffdff6aSGreg Kroah-Hartman
618ffdff6aSGreg Kroah-Hartman /* finished with MITE registers */
628ffdff6aSGreg Kroah-Hartman iounmap(mite_base);
638ffdff6aSGreg Kroah-Hartman return 0;
648ffdff6aSGreg Kroah-Hartman }
658ffdff6aSGreg Kroah-Hartman
labpc_pci_auto_attach(struct comedi_device * dev,unsigned long context)668ffdff6aSGreg Kroah-Hartman static int labpc_pci_auto_attach(struct comedi_device *dev,
678ffdff6aSGreg Kroah-Hartman unsigned long context)
688ffdff6aSGreg Kroah-Hartman {
698ffdff6aSGreg Kroah-Hartman struct pci_dev *pcidev = comedi_to_pci_dev(dev);
708ffdff6aSGreg Kroah-Hartman const struct labpc_boardinfo *board = NULL;
718ffdff6aSGreg Kroah-Hartman int ret;
728ffdff6aSGreg Kroah-Hartman
738ffdff6aSGreg Kroah-Hartman if (context < ARRAY_SIZE(labpc_pci_boards))
748ffdff6aSGreg Kroah-Hartman board = &labpc_pci_boards[context];
758ffdff6aSGreg Kroah-Hartman if (!board)
768ffdff6aSGreg Kroah-Hartman return -ENODEV;
778ffdff6aSGreg Kroah-Hartman dev->board_ptr = board;
788ffdff6aSGreg Kroah-Hartman dev->board_name = board->name;
798ffdff6aSGreg Kroah-Hartman
808ffdff6aSGreg Kroah-Hartman ret = comedi_pci_enable(dev);
818ffdff6aSGreg Kroah-Hartman if (ret)
828ffdff6aSGreg Kroah-Hartman return ret;
838ffdff6aSGreg Kroah-Hartman
848ffdff6aSGreg Kroah-Hartman ret = labpc_pci_mite_init(pcidev);
858ffdff6aSGreg Kroah-Hartman if (ret)
868ffdff6aSGreg Kroah-Hartman return ret;
878ffdff6aSGreg Kroah-Hartman
888ffdff6aSGreg Kroah-Hartman dev->mmio = pci_ioremap_bar(pcidev, 1);
898ffdff6aSGreg Kroah-Hartman if (!dev->mmio)
908ffdff6aSGreg Kroah-Hartman return -ENOMEM;
918ffdff6aSGreg Kroah-Hartman
928ffdff6aSGreg Kroah-Hartman return labpc_common_attach(dev, pcidev->irq, IRQF_SHARED);
938ffdff6aSGreg Kroah-Hartman }
948ffdff6aSGreg Kroah-Hartman
labpc_pci_detach(struct comedi_device * dev)958ffdff6aSGreg Kroah-Hartman static void labpc_pci_detach(struct comedi_device *dev)
968ffdff6aSGreg Kroah-Hartman {
978ffdff6aSGreg Kroah-Hartman labpc_common_detach(dev);
988ffdff6aSGreg Kroah-Hartman comedi_pci_detach(dev);
998ffdff6aSGreg Kroah-Hartman }
1008ffdff6aSGreg Kroah-Hartman
1018ffdff6aSGreg Kroah-Hartman static struct comedi_driver labpc_pci_comedi_driver = {
1028ffdff6aSGreg Kroah-Hartman .driver_name = "labpc_pci",
1038ffdff6aSGreg Kroah-Hartman .module = THIS_MODULE,
1048ffdff6aSGreg Kroah-Hartman .auto_attach = labpc_pci_auto_attach,
1058ffdff6aSGreg Kroah-Hartman .detach = labpc_pci_detach,
1068ffdff6aSGreg Kroah-Hartman };
1078ffdff6aSGreg Kroah-Hartman
1088ffdff6aSGreg Kroah-Hartman static const struct pci_device_id labpc_pci_table[] = {
1098ffdff6aSGreg Kroah-Hartman { PCI_VDEVICE(NI, 0x161), BOARD_NI_PCI1200 },
1108ffdff6aSGreg Kroah-Hartman { 0 }
1118ffdff6aSGreg Kroah-Hartman };
1128ffdff6aSGreg Kroah-Hartman MODULE_DEVICE_TABLE(pci, labpc_pci_table);
1138ffdff6aSGreg Kroah-Hartman
labpc_pci_probe(struct pci_dev * dev,const struct pci_device_id * id)1148ffdff6aSGreg Kroah-Hartman static int labpc_pci_probe(struct pci_dev *dev,
1158ffdff6aSGreg Kroah-Hartman const struct pci_device_id *id)
1168ffdff6aSGreg Kroah-Hartman {
1178ffdff6aSGreg Kroah-Hartman return comedi_pci_auto_config(dev, &labpc_pci_comedi_driver,
1188ffdff6aSGreg Kroah-Hartman id->driver_data);
1198ffdff6aSGreg Kroah-Hartman }
1208ffdff6aSGreg Kroah-Hartman
1218ffdff6aSGreg Kroah-Hartman static struct pci_driver labpc_pci_driver = {
1228ffdff6aSGreg Kroah-Hartman .name = "labpc_pci",
1238ffdff6aSGreg Kroah-Hartman .id_table = labpc_pci_table,
1248ffdff6aSGreg Kroah-Hartman .probe = labpc_pci_probe,
1258ffdff6aSGreg Kroah-Hartman .remove = comedi_pci_auto_unconfig,
1268ffdff6aSGreg Kroah-Hartman };
1278ffdff6aSGreg Kroah-Hartman module_comedi_pci_driver(labpc_pci_comedi_driver, labpc_pci_driver);
1288ffdff6aSGreg Kroah-Hartman
1298ffdff6aSGreg Kroah-Hartman MODULE_DESCRIPTION("Comedi: National Instruments Lab-PC PCI-1200 driver");
1308ffdff6aSGreg Kroah-Hartman MODULE_AUTHOR("Comedi https://www.comedi.org");
1318ffdff6aSGreg Kroah-Hartman MODULE_LICENSE("GPL");
132