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