1*8ffdff6aSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2*8ffdff6aSGreg Kroah-Hartman /* comedi/drivers/amplc_dio200_pci.c
3*8ffdff6aSGreg Kroah-Hartman  *
4*8ffdff6aSGreg Kroah-Hartman  * Driver for Amplicon PCI215, PCI272, PCIe215, PCIe236, PCIe296.
5*8ffdff6aSGreg Kroah-Hartman  *
6*8ffdff6aSGreg Kroah-Hartman  * Copyright (C) 2005-2013 MEV Ltd. <https://www.mev.co.uk/>
7*8ffdff6aSGreg Kroah-Hartman  *
8*8ffdff6aSGreg Kroah-Hartman  * COMEDI - Linux Control and Measurement Device Interface
9*8ffdff6aSGreg Kroah-Hartman  * Copyright (C) 1998,2000 David A. Schleef <ds@schleef.org>
10*8ffdff6aSGreg Kroah-Hartman  */
11*8ffdff6aSGreg Kroah-Hartman 
12*8ffdff6aSGreg Kroah-Hartman /*
13*8ffdff6aSGreg Kroah-Hartman  * Driver: amplc_dio200_pci
14*8ffdff6aSGreg Kroah-Hartman  * Description: Amplicon 200 Series PCI Digital I/O
15*8ffdff6aSGreg Kroah-Hartman  * Author: Ian Abbott <abbotti@mev.co.uk>
16*8ffdff6aSGreg Kroah-Hartman  * Devices: [Amplicon] PCI215 (amplc_dio200_pci), PCIe215, PCIe236,
17*8ffdff6aSGreg Kroah-Hartman  *   PCI272, PCIe296
18*8ffdff6aSGreg Kroah-Hartman  * Updated: Mon, 18 Mar 2013 15:03:50 +0000
19*8ffdff6aSGreg Kroah-Hartman  * Status: works
20*8ffdff6aSGreg Kroah-Hartman  *
21*8ffdff6aSGreg Kroah-Hartman  * Configuration options:
22*8ffdff6aSGreg Kroah-Hartman  *   none
23*8ffdff6aSGreg Kroah-Hartman  *
24*8ffdff6aSGreg Kroah-Hartman  * Manual configuration of PCI(e) cards is not supported; they are configured
25*8ffdff6aSGreg Kroah-Hartman  * automatically.
26*8ffdff6aSGreg Kroah-Hartman  *
27*8ffdff6aSGreg Kroah-Hartman  * SUBDEVICES
28*8ffdff6aSGreg Kroah-Hartman  *
29*8ffdff6aSGreg Kroah-Hartman  *                     PCI215         PCIe215        PCIe236
30*8ffdff6aSGreg Kroah-Hartman  *                  -------------  -------------  -------------
31*8ffdff6aSGreg Kroah-Hartman  *   Subdevices           5              8              8
32*8ffdff6aSGreg Kroah-Hartman  *    0                 PPI-X          PPI-X          PPI-X
33*8ffdff6aSGreg Kroah-Hartman  *    1                 PPI-Y          UNUSED         UNUSED
34*8ffdff6aSGreg Kroah-Hartman  *    2                 CTR-Z1         PPI-Y          UNUSED
35*8ffdff6aSGreg Kroah-Hartman  *    3                 CTR-Z2         UNUSED         UNUSED
36*8ffdff6aSGreg Kroah-Hartman  *    4               INTERRUPT        CTR-Z1         CTR-Z1
37*8ffdff6aSGreg Kroah-Hartman  *    5                                CTR-Z2         CTR-Z2
38*8ffdff6aSGreg Kroah-Hartman  *    6                                TIMER          TIMER
39*8ffdff6aSGreg Kroah-Hartman  *    7                              INTERRUPT      INTERRUPT
40*8ffdff6aSGreg Kroah-Hartman  *
41*8ffdff6aSGreg Kroah-Hartman  *
42*8ffdff6aSGreg Kroah-Hartman  *                     PCI272         PCIe296
43*8ffdff6aSGreg Kroah-Hartman  *                  -------------  -------------
44*8ffdff6aSGreg Kroah-Hartman  *   Subdevices           4              8
45*8ffdff6aSGreg Kroah-Hartman  *    0                 PPI-X          PPI-X1
46*8ffdff6aSGreg Kroah-Hartman  *    1                 PPI-Y          PPI-X2
47*8ffdff6aSGreg Kroah-Hartman  *    2                 PPI-Z          PPI-Y1
48*8ffdff6aSGreg Kroah-Hartman  *    3               INTERRUPT        PPI-Y2
49*8ffdff6aSGreg Kroah-Hartman  *    4                                CTR-Z1
50*8ffdff6aSGreg Kroah-Hartman  *    5                                CTR-Z2
51*8ffdff6aSGreg Kroah-Hartman  *    6                                TIMER
52*8ffdff6aSGreg Kroah-Hartman  *    7                              INTERRUPT
53*8ffdff6aSGreg Kroah-Hartman  *
54*8ffdff6aSGreg Kroah-Hartman  * Each PPI is a 8255 chip providing 24 DIO channels.  The DIO channels
55*8ffdff6aSGreg Kroah-Hartman  * are configurable as inputs or outputs in four groups:
56*8ffdff6aSGreg Kroah-Hartman  *
57*8ffdff6aSGreg Kroah-Hartman  *   Port A  - channels  0 to  7
58*8ffdff6aSGreg Kroah-Hartman  *   Port B  - channels  8 to 15
59*8ffdff6aSGreg Kroah-Hartman  *   Port CL - channels 16 to 19
60*8ffdff6aSGreg Kroah-Hartman  *   Port CH - channels 20 to 23
61*8ffdff6aSGreg Kroah-Hartman  *
62*8ffdff6aSGreg Kroah-Hartman  * Only mode 0 of the 8255 chips is supported.
63*8ffdff6aSGreg Kroah-Hartman  *
64*8ffdff6aSGreg Kroah-Hartman  * Each CTR is a 8254 chip providing 3 16-bit counter channels.  Each
65*8ffdff6aSGreg Kroah-Hartman  * channel is configured individually with INSN_CONFIG instructions.  The
66*8ffdff6aSGreg Kroah-Hartman  * specific type of configuration instruction is specified in data[0].
67*8ffdff6aSGreg Kroah-Hartman  * Some configuration instructions expect an additional parameter in
68*8ffdff6aSGreg Kroah-Hartman  * data[1]; others return a value in data[1].  The following configuration
69*8ffdff6aSGreg Kroah-Hartman  * instructions are supported:
70*8ffdff6aSGreg Kroah-Hartman  *
71*8ffdff6aSGreg Kroah-Hartman  *   INSN_CONFIG_SET_COUNTER_MODE.  Sets the counter channel's mode and
72*8ffdff6aSGreg Kroah-Hartman  *     BCD/binary setting specified in data[1].
73*8ffdff6aSGreg Kroah-Hartman  *
74*8ffdff6aSGreg Kroah-Hartman  *   INSN_CONFIG_8254_READ_STATUS.  Reads the status register value for the
75*8ffdff6aSGreg Kroah-Hartman  *     counter channel into data[1].
76*8ffdff6aSGreg Kroah-Hartman  *
77*8ffdff6aSGreg Kroah-Hartman  *   INSN_CONFIG_SET_CLOCK_SRC.  Sets the counter channel's clock source as
78*8ffdff6aSGreg Kroah-Hartman  *     specified in data[1] (this is a hardware-specific value).  Not
79*8ffdff6aSGreg Kroah-Hartman  *     supported on PC214E.  For the other boards, valid clock sources are
80*8ffdff6aSGreg Kroah-Hartman  *     0 to 7 as follows:
81*8ffdff6aSGreg Kroah-Hartman  *
82*8ffdff6aSGreg Kroah-Hartman  *       0.  CLK n, the counter channel's dedicated CLK input from the SK1
83*8ffdff6aSGreg Kroah-Hartman  *         connector.  (N.B. for other values, the counter channel's CLKn
84*8ffdff6aSGreg Kroah-Hartman  *         pin on the SK1 connector is an output!)
85*8ffdff6aSGreg Kroah-Hartman  *       1.  Internal 10 MHz clock.
86*8ffdff6aSGreg Kroah-Hartman  *       2.  Internal 1 MHz clock.
87*8ffdff6aSGreg Kroah-Hartman  *       3.  Internal 100 kHz clock.
88*8ffdff6aSGreg Kroah-Hartman  *       4.  Internal 10 kHz clock.
89*8ffdff6aSGreg Kroah-Hartman  *       5.  Internal 1 kHz clock.
90*8ffdff6aSGreg Kroah-Hartman  *       6.  OUT n-1, the output of counter channel n-1 (see note 1 below).
91*8ffdff6aSGreg Kroah-Hartman  *       7.  Ext Clock, the counter chip's dedicated Ext Clock input from
92*8ffdff6aSGreg Kroah-Hartman  *         the SK1 connector.  This pin is shared by all three counter
93*8ffdff6aSGreg Kroah-Hartman  *         channels on the chip.
94*8ffdff6aSGreg Kroah-Hartman  *
95*8ffdff6aSGreg Kroah-Hartman  *     For the PCIe boards, clock sources in the range 0 to 31 are allowed
96*8ffdff6aSGreg Kroah-Hartman  *     and the following additional clock sources are defined:
97*8ffdff6aSGreg Kroah-Hartman  *
98*8ffdff6aSGreg Kroah-Hartman  *       8.  HIGH logic level.
99*8ffdff6aSGreg Kroah-Hartman  *       9.  LOW logic level.
100*8ffdff6aSGreg Kroah-Hartman  *      10.  "Pattern present" signal.
101*8ffdff6aSGreg Kroah-Hartman  *      11.  Internal 20 MHz clock.
102*8ffdff6aSGreg Kroah-Hartman  *
103*8ffdff6aSGreg Kroah-Hartman  *   INSN_CONFIG_GET_CLOCK_SRC.  Returns the counter channel's current
104*8ffdff6aSGreg Kroah-Hartman  *     clock source in data[1].  For internal clock sources, data[2] is set
105*8ffdff6aSGreg Kroah-Hartman  *     to the period in ns.
106*8ffdff6aSGreg Kroah-Hartman  *
107*8ffdff6aSGreg Kroah-Hartman  *   INSN_CONFIG_SET_GATE_SRC.  Sets the counter channel's gate source as
108*8ffdff6aSGreg Kroah-Hartman  *     specified in data[2] (this is a hardware-specific value).  Not
109*8ffdff6aSGreg Kroah-Hartman  *     supported on PC214E.  For the other boards, valid gate sources are 0
110*8ffdff6aSGreg Kroah-Hartman  *     to 7 as follows:
111*8ffdff6aSGreg Kroah-Hartman  *
112*8ffdff6aSGreg Kroah-Hartman  *       0.  VCC (internal +5V d.c.), i.e. gate permanently enabled.
113*8ffdff6aSGreg Kroah-Hartman  *       1.  GND (internal 0V d.c.), i.e. gate permanently disabled.
114*8ffdff6aSGreg Kroah-Hartman  *       2.  GAT n, the counter channel's dedicated GAT input from the SK1
115*8ffdff6aSGreg Kroah-Hartman  *         connector.  (N.B. for other values, the counter channel's GATn
116*8ffdff6aSGreg Kroah-Hartman  *         pin on the SK1 connector is an output!)
117*8ffdff6aSGreg Kroah-Hartman  *       3.  /OUT n-2, the inverted output of counter channel n-2 (see note
118*8ffdff6aSGreg Kroah-Hartman  *         2 below).
119*8ffdff6aSGreg Kroah-Hartman  *       4.  Reserved.
120*8ffdff6aSGreg Kroah-Hartman  *       5.  Reserved.
121*8ffdff6aSGreg Kroah-Hartman  *       6.  Reserved.
122*8ffdff6aSGreg Kroah-Hartman  *       7.  Reserved.
123*8ffdff6aSGreg Kroah-Hartman  *
124*8ffdff6aSGreg Kroah-Hartman  *     For the PCIe boards, gate sources in the range 0 to 31 are allowed;
125*8ffdff6aSGreg Kroah-Hartman  *     the following additional clock sources and clock sources 6 and 7 are
126*8ffdff6aSGreg Kroah-Hartman  *     (re)defined:
127*8ffdff6aSGreg Kroah-Hartman  *
128*8ffdff6aSGreg Kroah-Hartman  *       6.  /GAT n, negated version of the counter channel's dedicated
129*8ffdff6aSGreg Kroah-Hartman  *         GAT input (negated version of gate source 2).
130*8ffdff6aSGreg Kroah-Hartman  *       7.  OUT n-2, the non-inverted output of counter channel n-2
131*8ffdff6aSGreg Kroah-Hartman  *         (negated version of gate source 3).
132*8ffdff6aSGreg Kroah-Hartman  *       8.  "Pattern present" signal, HIGH while pattern present.
133*8ffdff6aSGreg Kroah-Hartman  *       9.  "Pattern occurred" latched signal, latches HIGH when pattern
134*8ffdff6aSGreg Kroah-Hartman  *         occurs.
135*8ffdff6aSGreg Kroah-Hartman  *      10.  "Pattern gone away" latched signal, latches LOW when pattern
136*8ffdff6aSGreg Kroah-Hartman  *         goes away after it occurred.
137*8ffdff6aSGreg Kroah-Hartman  *      11.  Negated "pattern present" signal, LOW while pattern present
138*8ffdff6aSGreg Kroah-Hartman  *         (negated version of gate source 8).
139*8ffdff6aSGreg Kroah-Hartman  *      12.  Negated "pattern occurred" latched signal, latches LOW when
140*8ffdff6aSGreg Kroah-Hartman  *         pattern occurs (negated version of gate source 9).
141*8ffdff6aSGreg Kroah-Hartman  *      13.  Negated "pattern gone away" latched signal, latches LOW when
142*8ffdff6aSGreg Kroah-Hartman  *         pattern goes away after it occurred (negated version of gate
143*8ffdff6aSGreg Kroah-Hartman  *         source 10).
144*8ffdff6aSGreg Kroah-Hartman  *
145*8ffdff6aSGreg Kroah-Hartman  *   INSN_CONFIG_GET_GATE_SRC.  Returns the counter channel's current gate
146*8ffdff6aSGreg Kroah-Hartman  *     source in data[2].
147*8ffdff6aSGreg Kroah-Hartman  *
148*8ffdff6aSGreg Kroah-Hartman  * Clock and gate interconnection notes:
149*8ffdff6aSGreg Kroah-Hartman  *
150*8ffdff6aSGreg Kroah-Hartman  *   1.  Clock source OUT n-1 is the output of the preceding channel on the
151*8ffdff6aSGreg Kroah-Hartman  *   same counter subdevice if n > 0, or the output of channel 2 on the
152*8ffdff6aSGreg Kroah-Hartman  *   preceding counter subdevice (see note 3) if n = 0.
153*8ffdff6aSGreg Kroah-Hartman  *
154*8ffdff6aSGreg Kroah-Hartman  *   2.  Gate source /OUT n-2 is the inverted output of channel 0 on the
155*8ffdff6aSGreg Kroah-Hartman  *   same counter subdevice if n = 2, or the inverted output of channel n+1
156*8ffdff6aSGreg Kroah-Hartman  *   on the preceding counter subdevice (see note 3) if n < 2.
157*8ffdff6aSGreg Kroah-Hartman  *
158*8ffdff6aSGreg Kroah-Hartman  *   3.  The counter subdevices are connected in a ring, so the highest
159*8ffdff6aSGreg Kroah-Hartman  *   counter subdevice precedes the lowest.
160*8ffdff6aSGreg Kroah-Hartman  *
161*8ffdff6aSGreg Kroah-Hartman  * The 'TIMER' subdevice is a free-running 32-bit timer subdevice.
162*8ffdff6aSGreg Kroah-Hartman  *
163*8ffdff6aSGreg Kroah-Hartman  * The 'INTERRUPT' subdevice pretends to be a digital input subdevice.  The
164*8ffdff6aSGreg Kroah-Hartman  * digital inputs come from the interrupt status register.  The number of
165*8ffdff6aSGreg Kroah-Hartman  * channels matches the number of interrupt sources.  The PC214E does not
166*8ffdff6aSGreg Kroah-Hartman  * have an interrupt status register; see notes on 'INTERRUPT SOURCES'
167*8ffdff6aSGreg Kroah-Hartman  * below.
168*8ffdff6aSGreg Kroah-Hartman  *
169*8ffdff6aSGreg Kroah-Hartman  * INTERRUPT SOURCES
170*8ffdff6aSGreg Kroah-Hartman  *
171*8ffdff6aSGreg Kroah-Hartman  *                     PCI215         PCIe215        PCIe236
172*8ffdff6aSGreg Kroah-Hartman  *                  -------------  -------------  -------------
173*8ffdff6aSGreg Kroah-Hartman  *   Sources              6              6              6
174*8ffdff6aSGreg Kroah-Hartman  *    0               PPI-X-C0       PPI-X-C0       PPI-X-C0
175*8ffdff6aSGreg Kroah-Hartman  *    1               PPI-X-C3       PPI-X-C3       PPI-X-C3
176*8ffdff6aSGreg Kroah-Hartman  *    2               PPI-Y-C0       PPI-Y-C0        unused
177*8ffdff6aSGreg Kroah-Hartman  *    3               PPI-Y-C3       PPI-Y-C3        unused
178*8ffdff6aSGreg Kroah-Hartman  *    4              CTR-Z1-OUT1    CTR-Z1-OUT1    CTR-Z1-OUT1
179*8ffdff6aSGreg Kroah-Hartman  *    5              CTR-Z2-OUT1    CTR-Z2-OUT1    CTR-Z2-OUT1
180*8ffdff6aSGreg Kroah-Hartman  *
181*8ffdff6aSGreg Kroah-Hartman  *                     PCI272         PCIe296
182*8ffdff6aSGreg Kroah-Hartman  *                  -------------  -------------
183*8ffdff6aSGreg Kroah-Hartman  *   Sources              6              6
184*8ffdff6aSGreg Kroah-Hartman  *    0               PPI-X-C0       PPI-X1-C0
185*8ffdff6aSGreg Kroah-Hartman  *    1               PPI-X-C3       PPI-X1-C3
186*8ffdff6aSGreg Kroah-Hartman  *    2               PPI-Y-C0       PPI-Y1-C0
187*8ffdff6aSGreg Kroah-Hartman  *    3               PPI-Y-C3       PPI-Y1-C3
188*8ffdff6aSGreg Kroah-Hartman  *    4               PPI-Z-C0      CTR-Z1-OUT1
189*8ffdff6aSGreg Kroah-Hartman  *    5               PPI-Z-C3      CTR-Z2-OUT1
190*8ffdff6aSGreg Kroah-Hartman  *
191*8ffdff6aSGreg Kroah-Hartman  * When an interrupt source is enabled in the interrupt source enable
192*8ffdff6aSGreg Kroah-Hartman  * register, a rising edge on the source signal latches the corresponding
193*8ffdff6aSGreg Kroah-Hartman  * bit to 1 in the interrupt status register.
194*8ffdff6aSGreg Kroah-Hartman  *
195*8ffdff6aSGreg Kroah-Hartman  * When the interrupt status register value as a whole (actually, just the
196*8ffdff6aSGreg Kroah-Hartman  * 6 least significant bits) goes from zero to non-zero, the board will
197*8ffdff6aSGreg Kroah-Hartman  * generate an interrupt.  The interrupt will remain asserted until the
198*8ffdff6aSGreg Kroah-Hartman  * interrupt status register is cleared to zero.  To clear a bit to zero in
199*8ffdff6aSGreg Kroah-Hartman  * the interrupt status register, the corresponding interrupt source must
200*8ffdff6aSGreg Kroah-Hartman  * be disabled in the interrupt source enable register (there is no
201*8ffdff6aSGreg Kroah-Hartman  * separate interrupt clear register).
202*8ffdff6aSGreg Kroah-Hartman  *
203*8ffdff6aSGreg Kroah-Hartman  * COMMANDS
204*8ffdff6aSGreg Kroah-Hartman  *
205*8ffdff6aSGreg Kroah-Hartman  * The driver supports a read streaming acquisition command on the
206*8ffdff6aSGreg Kroah-Hartman  * 'INTERRUPT' subdevice.  The channel list selects the interrupt sources
207*8ffdff6aSGreg Kroah-Hartman  * to be enabled.  All channels will be sampled together (convert_src ==
208*8ffdff6aSGreg Kroah-Hartman  * TRIG_NOW).  The scan begins a short time after the hardware interrupt
209*8ffdff6aSGreg Kroah-Hartman  * occurs, subject to interrupt latencies (scan_begin_src == TRIG_EXT,
210*8ffdff6aSGreg Kroah-Hartman  * scan_begin_arg == 0).  The value read from the interrupt status register
211*8ffdff6aSGreg Kroah-Hartman  * is packed into a short value, one bit per requested channel, in the
212*8ffdff6aSGreg Kroah-Hartman  * order they appear in the channel list.
213*8ffdff6aSGreg Kroah-Hartman  */
214*8ffdff6aSGreg Kroah-Hartman 
215*8ffdff6aSGreg Kroah-Hartman #include <linux/module.h>
216*8ffdff6aSGreg Kroah-Hartman #include <linux/interrupt.h>
217*8ffdff6aSGreg Kroah-Hartman 
218*8ffdff6aSGreg Kroah-Hartman #include "../comedi_pci.h"
219*8ffdff6aSGreg Kroah-Hartman 
220*8ffdff6aSGreg Kroah-Hartman #include "amplc_dio200.h"
221*8ffdff6aSGreg Kroah-Hartman 
222*8ffdff6aSGreg Kroah-Hartman /*
223*8ffdff6aSGreg Kroah-Hartman  * Board descriptions.
224*8ffdff6aSGreg Kroah-Hartman  */
225*8ffdff6aSGreg Kroah-Hartman 
226*8ffdff6aSGreg Kroah-Hartman enum dio200_pci_model {
227*8ffdff6aSGreg Kroah-Hartman 	pci215_model,
228*8ffdff6aSGreg Kroah-Hartman 	pci272_model,
229*8ffdff6aSGreg Kroah-Hartman 	pcie215_model,
230*8ffdff6aSGreg Kroah-Hartman 	pcie236_model,
231*8ffdff6aSGreg Kroah-Hartman 	pcie296_model
232*8ffdff6aSGreg Kroah-Hartman };
233*8ffdff6aSGreg Kroah-Hartman 
234*8ffdff6aSGreg Kroah-Hartman static const struct dio200_board dio200_pci_boards[] = {
235*8ffdff6aSGreg Kroah-Hartman 	[pci215_model] = {
236*8ffdff6aSGreg Kroah-Hartman 		.name		= "pci215",
237*8ffdff6aSGreg Kroah-Hartman 		.mainbar	= 2,
238*8ffdff6aSGreg Kroah-Hartman 		.n_subdevs	= 5,
239*8ffdff6aSGreg Kroah-Hartman 		.sdtype		= {
240*8ffdff6aSGreg Kroah-Hartman 			sd_8255, sd_8255, sd_8254, sd_8254, sd_intr
241*8ffdff6aSGreg Kroah-Hartman 		},
242*8ffdff6aSGreg Kroah-Hartman 		.sdinfo		= { 0x00, 0x08, 0x10, 0x14, 0x3f },
243*8ffdff6aSGreg Kroah-Hartman 		.has_int_sce	= true,
244*8ffdff6aSGreg Kroah-Hartman 		.has_clk_gat_sce = true,
245*8ffdff6aSGreg Kroah-Hartman 	},
246*8ffdff6aSGreg Kroah-Hartman 	[pci272_model] = {
247*8ffdff6aSGreg Kroah-Hartman 		.name		= "pci272",
248*8ffdff6aSGreg Kroah-Hartman 		.mainbar	= 2,
249*8ffdff6aSGreg Kroah-Hartman 		.n_subdevs	= 4,
250*8ffdff6aSGreg Kroah-Hartman 		.sdtype		= {
251*8ffdff6aSGreg Kroah-Hartman 			sd_8255, sd_8255, sd_8255, sd_intr
252*8ffdff6aSGreg Kroah-Hartman 		},
253*8ffdff6aSGreg Kroah-Hartman 		.sdinfo		= { 0x00, 0x08, 0x10, 0x3f },
254*8ffdff6aSGreg Kroah-Hartman 		.has_int_sce	= true,
255*8ffdff6aSGreg Kroah-Hartman 	},
256*8ffdff6aSGreg Kroah-Hartman 	[pcie215_model] = {
257*8ffdff6aSGreg Kroah-Hartman 		.name		= "pcie215",
258*8ffdff6aSGreg Kroah-Hartman 		.mainbar	= 1,
259*8ffdff6aSGreg Kroah-Hartman 		.n_subdevs	= 8,
260*8ffdff6aSGreg Kroah-Hartman 		.sdtype		= {
261*8ffdff6aSGreg Kroah-Hartman 			sd_8255, sd_none, sd_8255, sd_none,
262*8ffdff6aSGreg Kroah-Hartman 			sd_8254, sd_8254, sd_timer, sd_intr
263*8ffdff6aSGreg Kroah-Hartman 		},
264*8ffdff6aSGreg Kroah-Hartman 		.sdinfo		= {
265*8ffdff6aSGreg Kroah-Hartman 			0x00, 0x00, 0x08, 0x00, 0x10, 0x14, 0x00, 0x3f
266*8ffdff6aSGreg Kroah-Hartman 		},
267*8ffdff6aSGreg Kroah-Hartman 		.has_int_sce	= true,
268*8ffdff6aSGreg Kroah-Hartman 		.has_clk_gat_sce = true,
269*8ffdff6aSGreg Kroah-Hartman 		.is_pcie	= true,
270*8ffdff6aSGreg Kroah-Hartman 	},
271*8ffdff6aSGreg Kroah-Hartman 	[pcie236_model] = {
272*8ffdff6aSGreg Kroah-Hartman 		.name		= "pcie236",
273*8ffdff6aSGreg Kroah-Hartman 		.mainbar	= 1,
274*8ffdff6aSGreg Kroah-Hartman 		.n_subdevs	= 8,
275*8ffdff6aSGreg Kroah-Hartman 		.sdtype		= {
276*8ffdff6aSGreg Kroah-Hartman 			sd_8255, sd_none, sd_none, sd_none,
277*8ffdff6aSGreg Kroah-Hartman 			sd_8254, sd_8254, sd_timer, sd_intr
278*8ffdff6aSGreg Kroah-Hartman 		},
279*8ffdff6aSGreg Kroah-Hartman 		.sdinfo		= {
280*8ffdff6aSGreg Kroah-Hartman 			0x00, 0x00, 0x00, 0x00, 0x10, 0x14, 0x00, 0x3f
281*8ffdff6aSGreg Kroah-Hartman 		},
282*8ffdff6aSGreg Kroah-Hartman 		.has_int_sce	= true,
283*8ffdff6aSGreg Kroah-Hartman 		.has_clk_gat_sce = true,
284*8ffdff6aSGreg Kroah-Hartman 		.is_pcie	= true,
285*8ffdff6aSGreg Kroah-Hartman 	},
286*8ffdff6aSGreg Kroah-Hartman 	[pcie296_model] = {
287*8ffdff6aSGreg Kroah-Hartman 		.name		= "pcie296",
288*8ffdff6aSGreg Kroah-Hartman 		.mainbar	= 1,
289*8ffdff6aSGreg Kroah-Hartman 		.n_subdevs	= 8,
290*8ffdff6aSGreg Kroah-Hartman 		.sdtype		= {
291*8ffdff6aSGreg Kroah-Hartman 			sd_8255, sd_8255, sd_8255, sd_8255,
292*8ffdff6aSGreg Kroah-Hartman 			sd_8254, sd_8254, sd_timer, sd_intr
293*8ffdff6aSGreg Kroah-Hartman 		},
294*8ffdff6aSGreg Kroah-Hartman 		.sdinfo		= {
295*8ffdff6aSGreg Kroah-Hartman 			0x00, 0x04, 0x08, 0x0c, 0x10, 0x14, 0x00, 0x3f
296*8ffdff6aSGreg Kroah-Hartman 		},
297*8ffdff6aSGreg Kroah-Hartman 		.has_int_sce	= true,
298*8ffdff6aSGreg Kroah-Hartman 		.has_clk_gat_sce = true,
299*8ffdff6aSGreg Kroah-Hartman 		.is_pcie	= true,
300*8ffdff6aSGreg Kroah-Hartman 	},
301*8ffdff6aSGreg Kroah-Hartman };
302*8ffdff6aSGreg Kroah-Hartman 
303*8ffdff6aSGreg Kroah-Hartman /*
304*8ffdff6aSGreg Kroah-Hartman  * This function does some special set-up for the PCIe boards
305*8ffdff6aSGreg Kroah-Hartman  * PCIe215, PCIe236, PCIe296.
306*8ffdff6aSGreg Kroah-Hartman  */
307*8ffdff6aSGreg Kroah-Hartman static int dio200_pcie_board_setup(struct comedi_device *dev)
308*8ffdff6aSGreg Kroah-Hartman {
309*8ffdff6aSGreg Kroah-Hartman 	struct pci_dev *pcidev = comedi_to_pci_dev(dev);
310*8ffdff6aSGreg Kroah-Hartman 	void __iomem *brbase;
311*8ffdff6aSGreg Kroah-Hartman 
312*8ffdff6aSGreg Kroah-Hartman 	/*
313*8ffdff6aSGreg Kroah-Hartman 	 * The board uses Altera Cyclone IV with PCI-Express hard IP.
314*8ffdff6aSGreg Kroah-Hartman 	 * The FPGA configuration has the PCI-Express Avalon-MM Bridge
315*8ffdff6aSGreg Kroah-Hartman 	 * Control registers in PCI BAR 0, offset 0, and the length of
316*8ffdff6aSGreg Kroah-Hartman 	 * these registers is 0x4000.
317*8ffdff6aSGreg Kroah-Hartman 	 *
318*8ffdff6aSGreg Kroah-Hartman 	 * We need to write 0x80 to the "Avalon-MM to PCI-Express Interrupt
319*8ffdff6aSGreg Kroah-Hartman 	 * Enable" register at offset 0x50 to allow generation of PCIe
320*8ffdff6aSGreg Kroah-Hartman 	 * interrupts when RXmlrq_i is asserted in the SOPC Builder system.
321*8ffdff6aSGreg Kroah-Hartman 	 */
322*8ffdff6aSGreg Kroah-Hartman 	if (pci_resource_len(pcidev, 0) < 0x4000) {
323*8ffdff6aSGreg Kroah-Hartman 		dev_err(dev->class_dev, "error! bad PCI region!\n");
324*8ffdff6aSGreg Kroah-Hartman 		return -EINVAL;
325*8ffdff6aSGreg Kroah-Hartman 	}
326*8ffdff6aSGreg Kroah-Hartman 	brbase = pci_ioremap_bar(pcidev, 0);
327*8ffdff6aSGreg Kroah-Hartman 	if (!brbase) {
328*8ffdff6aSGreg Kroah-Hartman 		dev_err(dev->class_dev, "error! failed to map registers!\n");
329*8ffdff6aSGreg Kroah-Hartman 		return -ENOMEM;
330*8ffdff6aSGreg Kroah-Hartman 	}
331*8ffdff6aSGreg Kroah-Hartman 	writel(0x80, brbase + 0x50);
332*8ffdff6aSGreg Kroah-Hartman 	iounmap(brbase);
333*8ffdff6aSGreg Kroah-Hartman 	/* Enable "enhanced" features of board. */
334*8ffdff6aSGreg Kroah-Hartman 	amplc_dio200_set_enhance(dev, 1);
335*8ffdff6aSGreg Kroah-Hartman 	return 0;
336*8ffdff6aSGreg Kroah-Hartman }
337*8ffdff6aSGreg Kroah-Hartman 
338*8ffdff6aSGreg Kroah-Hartman static int dio200_pci_auto_attach(struct comedi_device *dev,
339*8ffdff6aSGreg Kroah-Hartman 				  unsigned long context_model)
340*8ffdff6aSGreg Kroah-Hartman {
341*8ffdff6aSGreg Kroah-Hartman 	struct pci_dev *pci_dev = comedi_to_pci_dev(dev);
342*8ffdff6aSGreg Kroah-Hartman 	const struct dio200_board *board = NULL;
343*8ffdff6aSGreg Kroah-Hartman 	unsigned int bar;
344*8ffdff6aSGreg Kroah-Hartman 	int ret;
345*8ffdff6aSGreg Kroah-Hartman 
346*8ffdff6aSGreg Kroah-Hartman 	if (context_model < ARRAY_SIZE(dio200_pci_boards))
347*8ffdff6aSGreg Kroah-Hartman 		board = &dio200_pci_boards[context_model];
348*8ffdff6aSGreg Kroah-Hartman 	if (!board)
349*8ffdff6aSGreg Kroah-Hartman 		return -EINVAL;
350*8ffdff6aSGreg Kroah-Hartman 	dev->board_ptr = board;
351*8ffdff6aSGreg Kroah-Hartman 	dev->board_name = board->name;
352*8ffdff6aSGreg Kroah-Hartman 
353*8ffdff6aSGreg Kroah-Hartman 	dev_info(dev->class_dev, "%s: attach pci %s (%s)\n",
354*8ffdff6aSGreg Kroah-Hartman 		 dev->driver->driver_name, pci_name(pci_dev), dev->board_name);
355*8ffdff6aSGreg Kroah-Hartman 
356*8ffdff6aSGreg Kroah-Hartman 	ret = comedi_pci_enable(dev);
357*8ffdff6aSGreg Kroah-Hartman 	if (ret)
358*8ffdff6aSGreg Kroah-Hartman 		return ret;
359*8ffdff6aSGreg Kroah-Hartman 
360*8ffdff6aSGreg Kroah-Hartman 	bar = board->mainbar;
361*8ffdff6aSGreg Kroah-Hartman 	if (pci_resource_flags(pci_dev, bar) & IORESOURCE_MEM) {
362*8ffdff6aSGreg Kroah-Hartman 		dev->mmio = pci_ioremap_bar(pci_dev, bar);
363*8ffdff6aSGreg Kroah-Hartman 		if (!dev->mmio) {
364*8ffdff6aSGreg Kroah-Hartman 			dev_err(dev->class_dev,
365*8ffdff6aSGreg Kroah-Hartman 				"error! cannot remap registers\n");
366*8ffdff6aSGreg Kroah-Hartman 			return -ENOMEM;
367*8ffdff6aSGreg Kroah-Hartman 		}
368*8ffdff6aSGreg Kroah-Hartman 	} else {
369*8ffdff6aSGreg Kroah-Hartman 		dev->iobase = pci_resource_start(pci_dev, bar);
370*8ffdff6aSGreg Kroah-Hartman 	}
371*8ffdff6aSGreg Kroah-Hartman 
372*8ffdff6aSGreg Kroah-Hartman 	if (board->is_pcie) {
373*8ffdff6aSGreg Kroah-Hartman 		ret = dio200_pcie_board_setup(dev);
374*8ffdff6aSGreg Kroah-Hartman 		if (ret < 0)
375*8ffdff6aSGreg Kroah-Hartman 			return ret;
376*8ffdff6aSGreg Kroah-Hartman 	}
377*8ffdff6aSGreg Kroah-Hartman 
378*8ffdff6aSGreg Kroah-Hartman 	return amplc_dio200_common_attach(dev, pci_dev->irq, IRQF_SHARED);
379*8ffdff6aSGreg Kroah-Hartman }
380*8ffdff6aSGreg Kroah-Hartman 
381*8ffdff6aSGreg Kroah-Hartman static struct comedi_driver dio200_pci_comedi_driver = {
382*8ffdff6aSGreg Kroah-Hartman 	.driver_name	= "amplc_dio200_pci",
383*8ffdff6aSGreg Kroah-Hartman 	.module		= THIS_MODULE,
384*8ffdff6aSGreg Kroah-Hartman 	.auto_attach	= dio200_pci_auto_attach,
385*8ffdff6aSGreg Kroah-Hartman 	.detach		= comedi_pci_detach,
386*8ffdff6aSGreg Kroah-Hartman };
387*8ffdff6aSGreg Kroah-Hartman 
388*8ffdff6aSGreg Kroah-Hartman static const struct pci_device_id dio200_pci_table[] = {
389*8ffdff6aSGreg Kroah-Hartman 	{ PCI_VDEVICE(AMPLICON, 0x000b), pci215_model },
390*8ffdff6aSGreg Kroah-Hartman 	{ PCI_VDEVICE(AMPLICON, 0x000a), pci272_model },
391*8ffdff6aSGreg Kroah-Hartman 	{ PCI_VDEVICE(AMPLICON, 0x0011), pcie236_model },
392*8ffdff6aSGreg Kroah-Hartman 	{ PCI_VDEVICE(AMPLICON, 0x0012), pcie215_model },
393*8ffdff6aSGreg Kroah-Hartman 	{ PCI_VDEVICE(AMPLICON, 0x0014), pcie296_model },
394*8ffdff6aSGreg Kroah-Hartman 	{0}
395*8ffdff6aSGreg Kroah-Hartman };
396*8ffdff6aSGreg Kroah-Hartman 
397*8ffdff6aSGreg Kroah-Hartman MODULE_DEVICE_TABLE(pci, dio200_pci_table);
398*8ffdff6aSGreg Kroah-Hartman 
399*8ffdff6aSGreg Kroah-Hartman static int dio200_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
400*8ffdff6aSGreg Kroah-Hartman {
401*8ffdff6aSGreg Kroah-Hartman 	return comedi_pci_auto_config(dev, &dio200_pci_comedi_driver,
402*8ffdff6aSGreg Kroah-Hartman 				      id->driver_data);
403*8ffdff6aSGreg Kroah-Hartman }
404*8ffdff6aSGreg Kroah-Hartman 
405*8ffdff6aSGreg Kroah-Hartman static struct pci_driver dio200_pci_pci_driver = {
406*8ffdff6aSGreg Kroah-Hartman 	.name		= "amplc_dio200_pci",
407*8ffdff6aSGreg Kroah-Hartman 	.id_table	= dio200_pci_table,
408*8ffdff6aSGreg Kroah-Hartman 	.probe		= dio200_pci_probe,
409*8ffdff6aSGreg Kroah-Hartman 	.remove		= comedi_pci_auto_unconfig,
410*8ffdff6aSGreg Kroah-Hartman };
411*8ffdff6aSGreg Kroah-Hartman module_comedi_pci_driver(dio200_pci_comedi_driver, dio200_pci_pci_driver);
412*8ffdff6aSGreg Kroah-Hartman 
413*8ffdff6aSGreg Kroah-Hartman MODULE_AUTHOR("Comedi https://www.comedi.org");
414*8ffdff6aSGreg Kroah-Hartman MODULE_DESCRIPTION("Comedi driver for Amplicon 200 Series PCI(e) DIO boards");
415*8ffdff6aSGreg Kroah-Hartman MODULE_LICENSE("GPL");
416