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