kernel: Add 'static' to some function definitions.
[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 #include "rpreg.h"
49 #include "rpvar.h"
50
51 #include "pcidevs.h"
52 #include <bus/pci/pcireg.h>
53 #include <bus/pci/pcivar.h>
54
55 /**************************************************************************
56   MUDBAC remapped for PCI
57 **************************************************************************/
58
59 #define _CFG_INT_PCI    0x40
60 #define _PCI_INT_FUNC   0x3A
61
62 #define PCI_STROB       0x2000
63 #define INTR_EN_PCI     0x0010
64
65 /***************************************************************************
66 Function: sPCIControllerEOI
67 Purpose:  Strobe the MUDBAC's End Of Interrupt bit.
68 Call:     sPCIControllerEOI(CtlP)
69           CONTROLLER_T *CtlP; Ptr to controller structure
70 */
71 #define sPCIControllerEOI(CtlP) rp_writeio2(CtlP, 0, _PCI_INT_FUNC, PCI_STROB)
72
73 /***************************************************************************
74 Function: sPCIGetControllerIntStatus
75 Purpose:  Get the controller interrupt status
76 Call:     sPCIGetControllerIntStatus(CtlP)
77           CONTROLLER_T *CtlP; Ptr to controller structure
78 Return:   Byte_t: The controller interrupt status in the lower 4
79                          bits.  Bits 0 through 3 represent AIOP's 0
80                          through 3 respectively.  If a bit is set that
81                          AIOP is interrupting.  Bits 4 through 7 will
82                          always be cleared.
83 */
84 #define sPCIGetControllerIntStatus(CTLP) ((rp_readio2(CTLP, 0, _PCI_INT_FUNC) >> 8) & 0x1f)
85
86 static devclass_t rp_devclass;
87
88 static int rp_pciprobe(device_t dev);
89 static int rp_pciattach(device_t dev);
90 static void rp_pcireleaseresource(CONTROLLER_t *ctlp);
91 static int sPCIInitController( CONTROLLER_t *CtlP,
92                                int AiopNum,
93                                int IRQNum,
94                                Byte_t Frequency,
95                                int PeriodicOnly,
96                                int VendorDevice);
97 static rp_aiop2rid_t rp_pci_aiop2rid;
98 static rp_aiop2off_t rp_pci_aiop2off;
99 static rp_ctlmask_t rp_pci_ctlmask;
100
101 /*
102  * The following functions are the pci-specific part
103  * of rp driver.
104  */
105
106 static int
107 rp_pciprobe(device_t dev)
108 {
109         char *s;
110
111         s = NULL;
112         if ((pci_get_devid(dev) & 0xffff) == PCI_VENDOR_COMTROL)
113                 s = "RocketPort PCI";
114
115         if (s != NULL) {
116                 device_set_desc(dev, s);
117                 return (0);
118         }
119
120         return (ENXIO);
121 }
122
123 static int
124 rp_pciattach(device_t dev)
125 {
126         int     num_ports, num_aiops;
127         int     aiop;
128         CONTROLLER_t    *ctlp;
129         int     retval;
130         u_int32_t       stcmd;
131
132         ctlp = device_get_softc(dev);
133         bzero(ctlp, sizeof(*ctlp));
134         ctlp->dev = dev;
135         ctlp->aiop2rid = rp_pci_aiop2rid;
136         ctlp->aiop2off = rp_pci_aiop2off;
137         ctlp->ctlmask = rp_pci_ctlmask;
138
139         /* Wake up the device. */
140         stcmd = pci_read_config(dev, PCIR_COMMAND, 4);
141         if ((stcmd & PCIM_CMD_PORTEN) == 0) {
142                 stcmd |= (PCIM_CMD_PORTEN);
143                 pci_write_config(dev, PCIR_COMMAND, 4, stcmd);
144         }
145
146         /* The IO ports of AIOPs for a PCI controller are continuous. */
147         ctlp->io_num = 1;
148         ctlp->io_rid = kmalloc(sizeof(*(ctlp->io_rid)) * ctlp->io_num, 
149                                 M_DEVBUF, M_WAITOK | M_ZERO);
150         ctlp->io = kmalloc(sizeof(*(ctlp->io)) * ctlp->io_num, 
151                                 M_DEVBUF, M_WAITOK | M_ZERO);
152
153         ctlp->bus_ctlp = NULL;
154
155         ctlp->io_rid[0] = 0x10;
156         ctlp->io[0] = bus_alloc_resource(dev, SYS_RES_IOPORT, &ctlp->io_rid[0], 0, ~0, 1, RF_ACTIVE);
157         if(ctlp->io[0] == NULL) {
158                 device_printf(dev, "ioaddr mapping failed for RocketPort(PCI).\n");
159                 retval = ENXIO;
160                 goto nogo;
161         }
162
163         num_aiops = sPCIInitController(ctlp,
164                                        MAX_AIOPS_PER_BOARD, 0,
165                                        FREQ_DIS, 0, (pci_get_devid(dev) >> 16) & 0xffff);
166
167         num_ports = 0;
168         for(aiop=0; aiop < num_aiops; aiop++) {
169                 sResetAiopByNum(ctlp, aiop);
170                 num_ports += sGetAiopNumChan(ctlp, aiop);
171         }
172
173         retval = rp_attachcommon(ctlp, num_aiops, num_ports);
174         if (retval != 0)
175                 goto nogo;
176
177         return (0);
178
179 nogo:
180         rp_pcireleaseresource(ctlp);
181
182         return (retval);
183 }
184
185 static void
186 rp_pcireleaseresource(CONTROLLER_t *ctlp)
187 {
188         rp_releaseresource(ctlp);
189
190         if (ctlp->io != NULL) {
191                 if (ctlp->io[0] != NULL)
192                         bus_release_resource(ctlp->dev, SYS_RES_IOPORT, ctlp->io_rid[0], ctlp->io[0]);
193                 kfree(ctlp->io, M_DEVBUF);
194         }
195         if (ctlp->io_rid != NULL)
196                 kfree(ctlp->io_rid, M_DEVBUF);
197 }
198
199 static int
200 sPCIInitController( CONTROLLER_t *CtlP,
201                     int AiopNum,
202                     int IRQNum,
203                     Byte_t Frequency,
204                     int PeriodicOnly,
205                     int VendorDevice)
206 {
207         int             i;
208
209         CtlP->CtlID = CTLID_0001;       /* controller release 1 */
210
211         sPCIControllerEOI(CtlP);
212
213         /* Init AIOPs */
214         CtlP->NumAiop = 0;
215         for(i=0; i < AiopNum; i++)
216         {
217                 /*device_printf(CtlP->dev, "aiop %d.\n", i);*/
218                 CtlP->AiopID[i] = sReadAiopID(CtlP, i); /* read AIOP ID */
219                 /*device_printf(CtlP->dev, "ID = %d.\n", CtlP->AiopID[i]);*/
220                 if(CtlP->AiopID[i] == AIOPID_NULL)      /* if AIOP does not exist */
221                 {
222                         break;                          /* done looking for AIOPs */
223                 }
224
225                 switch( VendorDevice ) {
226                 case PCI_PRODUCT_COMTROL_ROCKETPORT4QUAD:
227                 case PCI_PRODUCT_COMTROL_ROCKETPORT4RJ:
228                 case PCI_PRODUCT_COMTROL_ROCKETMODEM4:
229                         CtlP->AiopNumChan[i] = 4;
230                         break;
231                 case PCI_PRODUCT_COMTROL_ROCKETMODEM6:
232                         CtlP->AiopNumChan[i] = 6;
233                         break;
234                 case PCI_PRODUCT_COMTROL_ROCKETPORT8OCTA:
235                 case PCI_PRODUCT_COMTROL_ROCKETPORT8RJ:
236                 case PCI_PRODUCT_COMTROL_ROCKETPORT8EXT:
237                 case PCI_PRODUCT_COMTROL_ROCKETPORT16EXT:
238                 case PCI_PRODUCT_COMTROL_ROCKETPORT32EXT:
239                         CtlP->AiopNumChan[i] = 8;
240                         break;
241                 default:
242                         CtlP->AiopNumChan[i] = sReadAiopNumChan(CtlP, i);
243                         break;
244                 }
245                 /*device_printf(CtlP->dev, "%d channels.\n", CtlP->AiopNumChan[i]);*/
246                 rp_writeaiop2(CtlP, i, _INDX_ADDR,_CLK_PRE);    /* clock prescaler */
247                 /*device_printf(CtlP->dev, "configuring clock prescaler.\n");*/
248                 rp_writeaiop1(CtlP, i, _INDX_DATA,CLOCK_PRESC);
249                 /*device_printf(CtlP->dev, "configured clock prescaler.\n");*/
250                 CtlP->NumAiop++;                                /* bump count of AIOPs */
251         }
252
253         if(CtlP->NumAiop == 0)
254                 return(-1);
255         else
256                 return(CtlP->NumAiop);
257 }
258
259 /*
260  * ARGSUSED
261  * Maps (aiop, offset) to rid.
262  */
263 static int
264 rp_pci_aiop2rid(int aiop, int offset)
265 {
266         /* Always return zero for a PCI controller. */
267         return 0;
268 }
269
270 /*
271  * ARGSUSED
272  * Maps (aiop, offset) to the offset of resource.
273  */
274 static int
275 rp_pci_aiop2off(int aiop, int offset)
276 {
277         /* Each AIOP reserves 0x40 bytes. */
278         return aiop * 0x40 + offset;
279 }
280
281 /* Read the int status for a PCI controller. */
282 static unsigned char
283 rp_pci_ctlmask(CONTROLLER_t *ctlp)
284 {
285         return sPCIGetControllerIntStatus(ctlp);
286 }
287
288 static device_method_t rp_pcimethods[] = {
289         /* Device interface */
290         DEVMETHOD(device_probe,         rp_pciprobe),
291         DEVMETHOD(device_attach,        rp_pciattach),
292
293         DEVMETHOD_END
294 };
295
296 static driver_t rp_pcidriver = {
297         "rp",
298         rp_pcimethods,
299         sizeof(CONTROLLER_t),
300 };
301
302 /*
303  * rp can be attached to a pci bus.
304  */
305 DRIVER_MODULE(rp, pci, rp_pcidriver, rp_devclass, NULL, NULL);