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