]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/rp/rp_pci.c
Add KBD_BOOTPROTO quirk for Logitech G510s USB keyboard
[FreeBSD/FreeBSD.git] / sys / dev / rp / rp_pci.c
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) Comtrol Corporation <support@comtrol.com>
5  * All rights reserved.
6  *
7  * PCI-specific part separated from:
8  * sys/i386/isa/rp.c,v 1.33 1999/09/28 11:45:27 phk Exp
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted prodived that the follwoing conditions
12  * are met.
13  * 1. Redistributions of source code must retain the above copyright 
14  *    notive, this list of conditions and the following disclainer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials prodided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *       This product includes software developed by Comtrol Corporation.
21  * 4. The name of Comtrol Corporation may not be used to endorse or 
22  *    promote products derived from this software without specific 
23  *    prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY COMTROL CORPORATION ``AS IS'' AND ANY
26  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL COMTROL CORPORATION BE LIABLE FOR
29  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/fcntl.h>
44 #include <sys/malloc.h>
45 #include <sys/tty.h>
46 #include <sys/conf.h>
47 #include <sys/kernel.h>
48 #include <sys/module.h>
49 #include <machine/resource.h>
50 #include <machine/bus.h>
51 #include <sys/bus.h>
52 #include <sys/rman.h>
53
54 #define ROCKET_C
55 #include <dev/rp/rpreg.h>
56 #include <dev/rp/rpvar.h>
57
58 #include <dev/pci/pcireg.h>
59 #include <dev/pci/pcivar.h>
60
61 /* PCI IDs  */
62 #define RP_VENDOR_ID            0x11FE
63 #define RP_DEVICE_ID_32I        0x0001
64 #define RP_DEVICE_ID_8I         0x0002
65 #define RP_DEVICE_ID_16I        0x0003
66 #define RP_DEVICE_ID_4Q         0x0004
67 #define RP_DEVICE_ID_8O         0x0005
68 #define RP_DEVICE_ID_8J         0x0006
69 #define RP_DEVICE_ID_4J         0x0007
70 #define RP_DEVICE_ID_6M         0x000C
71 #define RP_DEVICE_ID_4M         0x000D
72 #define RP_DEVICE_ID_UPCI_32    0x0801
73 #define RP_DEVICE_ID_UPCI_16    0x0803
74 #define RP_DEVICE_ID_UPCI_8O    0x0805
75
76 /**************************************************************************
77   MUDBAC remapped for PCI
78 **************************************************************************/
79
80 #define _CFG_INT_PCI    0x40
81 #define _PCI_INT_FUNC   0x3A
82
83 #define PCI_STROB       0x2000
84 #define INTR_EN_PCI     0x0010
85
86 /***************************************************************************
87 Function: sPCIControllerEOI
88 Purpose:  Strobe the MUDBAC's End Of Interrupt bit.
89 Call:     sPCIControllerEOI(CtlP)
90           CONTROLLER_T *CtlP; Ptr to controller structure
91 */
92 #define sPCIControllerEOI(CtlP) rp_writeio2(CtlP, 0, _PCI_INT_FUNC, PCI_STROB)
93
94 /***************************************************************************
95 Function: sPCIGetControllerIntStatus
96 Purpose:  Get the controller interrupt status
97 Call:     sPCIGetControllerIntStatus(CtlP)
98           CONTROLLER_T *CtlP; Ptr to controller structure
99 Return:   Byte_t: The controller interrupt status in the lower 4
100                          bits.  Bits 0 through 3 represent AIOP's 0
101                          through 3 respectively.  If a bit is set that
102                          AIOP is interrupting.  Bits 4 through 7 will
103                          always be cleared.
104 */
105 #define sPCIGetControllerIntStatus(CTLP) ((rp_readio2(CTLP, 0, _PCI_INT_FUNC) >> 8) & 0x1f)
106
107 static devclass_t rp_devclass;
108
109 static int rp_pciprobe(device_t dev);
110 static int rp_pciattach(device_t dev);
111 #ifdef notdef
112 static int rp_pcidetach(device_t dev);
113 static int rp_pcishutdown(device_t dev);
114 #endif /* notdef */
115 static void rp_pcireleaseresource(CONTROLLER_t *ctlp);
116 static int sPCIInitController( CONTROLLER_t *CtlP,
117                                int AiopNum,
118                                int IRQNum,
119                                Byte_t Frequency,
120                                int PeriodicOnly,
121                                int VendorDevice);
122 static rp_aiop2rid_t rp_pci_aiop2rid;
123 static rp_aiop2off_t rp_pci_aiop2off;
124 static rp_ctlmask_t rp_pci_ctlmask;
125
126 /*
127  * The following functions are the pci-specific part
128  * of rp driver.
129  */
130
131 static int
132 rp_pciprobe(device_t dev)
133 {
134         char *s;
135
136         s = NULL;
137         if (pci_get_vendor(dev) == RP_VENDOR_ID)
138                 s = "RocketPort PCI";
139
140         if (s != NULL) {
141                 device_set_desc(dev, s);
142                 return (BUS_PROBE_DEFAULT);
143         }
144
145         return (ENXIO);
146 }
147
148 static int
149 rp_pciattach(device_t dev)
150 {
151         int     num_ports, num_aiops;
152         int     aiop;
153         CONTROLLER_t    *ctlp;
154         int     unit;
155         int     retval;
156
157         ctlp = device_get_softc(dev);
158         bzero(ctlp, sizeof(*ctlp));
159         ctlp->dev = dev;
160         unit = device_get_unit(dev);
161         ctlp->aiop2rid = rp_pci_aiop2rid;
162         ctlp->aiop2off = rp_pci_aiop2off;
163         ctlp->ctlmask = rp_pci_ctlmask;
164
165         /* The IO ports of AIOPs for a PCI controller are continuous. */
166         ctlp->io_num = 1;
167         ctlp->io_rid = malloc(sizeof(*(ctlp->io_rid)) * ctlp->io_num, M_DEVBUF, M_NOWAIT | M_ZERO);
168         ctlp->io = malloc(sizeof(*(ctlp->io)) * ctlp->io_num, M_DEVBUF, M_NOWAIT | M_ZERO);
169         if (ctlp->io_rid == NULL || ctlp->io == NULL) {
170                 device_printf(dev, "rp_pciattach: Out of memory.\n");
171                 retval = ENOMEM;
172                 goto nogo;
173         }
174
175         ctlp->bus_ctlp = NULL;
176
177         switch (pci_get_device(dev)) {
178         case RP_DEVICE_ID_UPCI_16:
179         case RP_DEVICE_ID_UPCI_32:
180         case RP_DEVICE_ID_UPCI_8O:
181                 ctlp->io_rid[0] = PCIR_BAR(2);
182                 break;
183         default:
184                 ctlp->io_rid[0] = PCIR_BAR(0);
185                 break;
186         }
187         ctlp->io[0] = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
188                 &ctlp->io_rid[0], RF_ACTIVE);
189         if(ctlp->io[0] == NULL) {
190                 device_printf(dev, "ioaddr mapping failed for RocketPort(PCI).\n");
191                 retval = ENXIO;
192                 goto nogo;
193         }
194
195         num_aiops = sPCIInitController(ctlp,
196                                        MAX_AIOPS_PER_BOARD, 0,
197                                        FREQ_DIS, 0, pci_get_device(dev));
198
199         num_ports = 0;
200         for(aiop=0; aiop < num_aiops; aiop++) {
201                 sResetAiopByNum(ctlp, aiop);
202                 num_ports += sGetAiopNumChan(ctlp, aiop);
203         }
204
205         retval = rp_attachcommon(ctlp, num_aiops, num_ports);
206         if (retval != 0)
207                 goto nogo;
208
209         return (0);
210
211 nogo:
212         rp_pcireleaseresource(ctlp);
213
214         return (retval);
215 }
216
217 static int
218 rp_pcidetach(device_t dev)
219 {
220         CONTROLLER_t    *ctlp;
221
222         ctlp = device_get_softc(dev);
223         rp_pcireleaseresource(ctlp);
224
225         return (0);
226 }
227
228 static int
229 rp_pcishutdown(device_t dev)
230 {
231         CONTROLLER_t    *ctlp;
232
233         ctlp = device_get_softc(dev);
234         rp_pcireleaseresource(ctlp);
235
236         return (0);
237 }
238
239 static void
240 rp_pcireleaseresource(CONTROLLER_t *ctlp)
241 {
242         rp_releaseresource(ctlp);
243         if (ctlp->io != NULL) {
244                 if (ctlp->io[0] != NULL)
245                         bus_release_resource(ctlp->dev, SYS_RES_IOPORT, ctlp->io_rid[0], ctlp->io[0]);
246                 free(ctlp->io, M_DEVBUF);
247                 ctlp->io = NULL;
248         }
249         if (ctlp->io_rid != NULL) {
250                 free(ctlp->io_rid, M_DEVBUF);
251                 ctlp->io = NULL;
252         }
253 }
254
255 static int
256 sPCIInitController( CONTROLLER_t *CtlP,
257                     int AiopNum,
258                     int IRQNum,
259                     Byte_t Frequency,
260                     int PeriodicOnly,
261                     int VendorDevice)
262 {
263         int             i;
264
265         CtlP->CtlID = CTLID_0001;       /* controller release 1 */
266
267         sPCIControllerEOI(CtlP);
268
269         /* Init AIOPs */
270         CtlP->NumAiop = 0;
271         for(i=0; i < AiopNum; i++)
272         {
273                 /*device_printf(CtlP->dev, "aiop %d.\n", i);*/
274                 CtlP->AiopID[i] = sReadAiopID(CtlP, i); /* read AIOP ID */
275                 /*device_printf(CtlP->dev, "ID = %d.\n", CtlP->AiopID[i]);*/
276                 if(CtlP->AiopID[i] == AIOPID_NULL)      /* if AIOP does not exist */
277                 {
278                         break;                          /* done looking for AIOPs */
279                 }
280
281                 switch( VendorDevice ) {
282                 case RP_DEVICE_ID_4Q:
283                 case RP_DEVICE_ID_4J:
284                 case RP_DEVICE_ID_4M:
285                         CtlP->AiopNumChan[i] = 4;
286                         break;
287                 case RP_DEVICE_ID_6M:
288                         CtlP->AiopNumChan[i] = 6;
289                         break;
290                 case RP_DEVICE_ID_8O:
291                 case RP_DEVICE_ID_8J:
292                 case RP_DEVICE_ID_8I:
293                 case RP_DEVICE_ID_16I:
294                 case RP_DEVICE_ID_32I:
295                         CtlP->AiopNumChan[i] = 8;
296                         break;
297                 default:
298 #ifdef notdef
299                         CtlP->AiopNumChan[i] = 8;
300 #else
301                         CtlP->AiopNumChan[i] = sReadAiopNumChan(CtlP, i);
302 #endif /* notdef */
303                         break;
304                 }
305                 /*device_printf(CtlP->dev, "%d channels.\n", CtlP->AiopNumChan[i]);*/
306                 rp_writeaiop2(CtlP, i, _INDX_ADDR,_CLK_PRE);    /* clock prescaler */
307                 /*device_printf(CtlP->dev, "configuring clock prescaler.\n");*/
308                 rp_writeaiop1(CtlP, i, _INDX_DATA,CLOCK_PRESC);
309                 /*device_printf(CtlP->dev, "configured clock prescaler.\n");*/
310                 CtlP->NumAiop++;                                /* bump count of AIOPs */
311         }
312
313         if(CtlP->NumAiop == 0)
314                 return(-1);
315         else
316                 return(CtlP->NumAiop);
317 }
318
319 /*
320  * ARGSUSED
321  * Maps (aiop, offset) to rid.
322  */
323 static int
324 rp_pci_aiop2rid(int aiop, int offset)
325 {
326         /* Always return zero for a PCI controller. */
327         return 0;
328 }
329
330 /*
331  * ARGSUSED
332  * Maps (aiop, offset) to the offset of resource.
333  */
334 static int
335 rp_pci_aiop2off(int aiop, int offset)
336 {
337         /* Each AIOP reserves 0x40 bytes. */
338         return aiop * 0x40 + offset;
339 }
340
341 /* Read the int status for a PCI controller. */
342 static unsigned char
343 rp_pci_ctlmask(CONTROLLER_t *ctlp)
344 {
345         return sPCIGetControllerIntStatus(ctlp);
346 }
347
348 static device_method_t rp_pcimethods[] = {
349         /* Device interface */
350         DEVMETHOD(device_probe,         rp_pciprobe),
351         DEVMETHOD(device_attach,        rp_pciattach),
352         DEVMETHOD(device_detach,        rp_pcidetach),
353         DEVMETHOD(device_shutdown,      rp_pcishutdown),
354
355         { 0, 0 }
356 };
357
358 static driver_t rp_pcidriver = {
359         "rp",
360         rp_pcimethods,
361         sizeof(CONTROLLER_t),
362 };
363
364 /*
365  * rp can be attached to a pci bus.
366  */
367 DRIVER_MODULE(rp, pci, rp_pcidriver, rp_devclass, 0, 0);