Do a major clean-up of the BUSDMA architecture. A large number of
[dragonfly.git] / sys / dev / serial / rp / rp_isa.c
1 /* 
2  * Copyright (c) Comtrol Corporation <support@comtrol.com>
3  * All rights reserved.
4  *
5  * ISA-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_isa.c,v 1.3.2.1 2002/06/18 03:11:46 obrien Exp $
36  * $DragonFly: src/sys/dev/serial/rp/rp_isa.c,v 1.7 2006/10/25 20:56:02 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 <sys/bus.h>
47 #include <sys/rman.h>
48
49 #define ROCKET_C
50 #include "rpreg.h"
51 #include "rpvar.h"
52
53 #include <bus/isa/isavar.h>
54
55 /* ISA-specific part of CONTROLLER_t */
56 struct ISACONTROLLER_T {
57         int             MBaseIO;        /* rid of the Mudbac controller for this controller */
58         int             MReg0IO;        /* offset0 of the Mudbac controller for this controller */
59         int             MReg1IO;        /* offset1 of the Mudbac controller for this controller */
60         int             MReg2IO;        /* offset2 of the Mudbac controller for this controller */
61         int             MReg3IO;        /* offset3 of the Mudbac controller for this controller */
62         Byte_t          MReg2;
63         Byte_t          MReg3;
64 };
65 typedef struct ISACONTROLLER_T ISACONTROLLER_t;
66
67 #define ISACTL(ctlp) ((ISACONTROLLER_t *)((ctlp)->bus_ctlp))
68
69 /***************************************************************************
70 Function: sControllerEOI
71 Purpose:  Strobe the MUDBAC's End Of Interrupt bit.
72 Call:     sControllerEOI(MudbacCtlP,CtlP)
73           CONTROLLER_T *MudbacCtlP; Ptr to Mudbac controller structure
74           CONTROLLER_T *CtlP; Ptr to controller structure
75 */
76 #define sControllerEOI(MudbacCtlP,CtlP) \
77         rp_writeio1(MudbacCtlP,ISACTL(CtlP)->MBaseIO,ISACTL(CtlP)->MReg2IO,ISACTL(CtlP)->MReg2 | INT_STROB)
78
79 /***************************************************************************
80 Function: sDisAiop
81 Purpose:  Disable I/O access to an AIOP
82 Call:     sDisAiop(MudbacCtlP,CtlP)
83           CONTROLLER_T *MudbacCtlP; Ptr to Mudbac controller structure
84           CONTROLLER_T *CtlP; Ptr to controller structure
85           int AiopNum; Number of AIOP on controller
86 */
87 #define sDisAiop(MudbacCtlP,CtlP,AIOPNUM) \
88 { \
89    ISACTL(CtlP)->MReg3 &= rp_sBitMapClrTbl[AIOPNUM]; \
90    rp_writeio1(MudbacCtlP,ISACTL(CtlP)->MBaseIO,ISACTL(CtlP)->MReg3IO,ISACTL(CtlP)->MReg3); \
91 }
92
93 /***************************************************************************
94 Function: sEnAiop
95 Purpose:  Enable I/O access to an AIOP
96 Call:     sEnAiop(MudbacCtlP,CtlP)
97           CONTROLLER_T *MudbacCtlP; Ptr to Mudbac controller structure
98           CONTROLLER_T *CtlP; Ptr to controller structure
99           int AiopNum; Number of AIOP on controller
100 */
101 #define sEnAiop(MudbacCtlP,CtlP,AIOPNUM) \
102 { \
103    ISACTL(CtlP)->MReg3 |= rp_sBitMapSetTbl[AIOPNUM]; \
104    rp_writeio1(MudbacCtlP,ISACTL(CtlP)->MBaseIO,ISACTL(CtlP)->MReg3IO,ISACTL(CtlP)->MReg3); \
105 }
106
107 /***************************************************************************
108 Function: sGetControllerIntStatus
109 Purpose:  Get the controller interrupt status
110 Call:     sGetControllerIntStatus(MudbacCtlP,CtlP)
111           CONTROLLER_T *MudbacCtlP; Ptr to Mudbac controller structure
112           CONTROLLER_T *CtlP; Ptr to controller structure
113 Return:   Byte_t: The controller interrupt status in the lower 4
114                          bits.  Bits 0 through 3 represent AIOP's 0
115                          through 3 respectively.  If a bit is set that
116                          AIOP is interrupting.  Bits 4 through 7 will
117                          always be cleared.
118 */
119 #define sGetControllerIntStatus(MudbacCtlP,CtlP) \
120         (rp_readio1(MudbacCtlP,ISACTL(CtlP)->MBaseIO,ISACTL(CtlP)->MReg1IO) & 0x0f)
121
122 static devclass_t rp_devclass;
123 static CONTROLLER_t *rp_controller;
124 static int rp_nisadevs;
125
126 static int rp_probe(device_t dev);
127 static int rp_attach(device_t dev);
128 static void rp_isareleaseresource(CONTROLLER_t *ctlp);
129 static int sInitController(CONTROLLER_T *CtlP,
130                            CONTROLLER_T *MudbacCtlP,
131                            int AiopNum,
132                            int IRQNum,
133                            Byte_t Frequency,
134                            int PeriodicOnly);
135 static rp_aiop2rid_t rp_isa_aiop2rid;
136 static rp_aiop2off_t rp_isa_aiop2off;
137 static rp_ctlmask_t rp_isa_ctlmask;
138
139 static int
140 rp_probe(device_t dev)
141 {
142         int unit;
143         CONTROLLER_t *controller;
144         int num_aiops;
145         CONTROLLER_t *ctlp;
146         int retval;
147
148         /*
149          * We have no PnP RocketPort cards.
150          * (At least according to LINT)
151          */
152         if (isa_get_logicalid(dev) != 0)
153                 return (ENXIO);
154
155         /* We need IO port resource to configure an ISA device. */
156         if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 0)
157                 return (ENXIO);
158
159         unit = device_get_unit(dev);
160         if (unit >= 4) {
161                 device_printf(dev, "rpprobe: unit number %d invalid.\n", unit);
162                 return (ENXIO);
163         }
164         device_printf(dev, "probing for RocketPort(ISA) unit %d.\n", unit);
165
166         ctlp = device_get_softc(dev);
167         bzero(ctlp, sizeof(*ctlp));
168         ctlp->dev = dev;
169         ctlp->aiop2rid = rp_isa_aiop2rid;
170         ctlp->aiop2off = rp_isa_aiop2off;
171         ctlp->ctlmask = rp_isa_ctlmask;
172
173         /* The IO ports of AIOPs for an ISA controller are discrete. */
174         ctlp->io_num = 1;
175         ctlp->io_rid = kmalloc(sizeof(*(ctlp->io_rid)) * MAX_AIOPS_PER_BOARD,
176                                 M_DEVBUF, M_WAITOK | M_ZERO);
177         ctlp->io = kmalloc(sizeof(*(ctlp->io)) * MAX_AIOPS_PER_BOARD, 
178                                 M_DEVBUF, M_WAITOK | M_ZERO);
179
180         ctlp->bus_ctlp = kmalloc(sizeof(ISACONTROLLER_t) * 1,
181                                 M_DEVBUF, M_WAITOK | M_ZERO);
182
183         ctlp->io_rid[0] = 0;
184         if (rp_controller != NULL) {
185                 controller = rp_controller;
186                 ctlp->io[0] = bus_alloc_resource(dev, SYS_RES_IOPORT, &ctlp->io_rid[0], 0, ~0, 0x40, RF_ACTIVE);
187         } else {
188                 controller = rp_controller = ctlp;
189                 ctlp->io[0] = bus_alloc_resource(dev, SYS_RES_IOPORT, &ctlp->io_rid[0], 0, ~0, 0x44, RF_ACTIVE);
190         }
191         if (ctlp->io[0] == NULL) {
192                 device_printf(dev, "rp_attach: Resource not available.\n");
193                 retval = ENXIO;
194                 goto nogo;
195         }
196
197         num_aiops = sInitController(ctlp,
198                                 controller,
199                                 MAX_AIOPS_PER_BOARD, 0,
200                                 FREQ_DIS, 0);
201         if (num_aiops <= 0) {
202                 device_printf(dev, "board%d init failed.\n", unit);
203                 retval = ENXIO;
204                 goto nogo;
205         }
206
207         if (rp_controller == NULL)
208                 rp_controller = controller;
209         rp_nisadevs++;
210
211         device_set_desc(dev, "RocketPort ISA");
212
213         return (0);
214
215 nogo:
216         rp_isareleaseresource(ctlp);
217
218         return (retval);
219 }
220
221 static int
222 rp_attach(device_t dev)
223 {
224         int     unit;
225         int     num_ports, num_aiops;
226         int     aiop;
227         CONTROLLER_t    *ctlp;
228         int     retval;
229
230         unit = device_get_unit(dev);
231
232         ctlp = device_get_softc(dev);
233
234 #if notdef
235         num_aiops = sInitController(ctlp,
236                                 rp_controller,
237                                 MAX_AIOPS_PER_BOARD, 0,
238                                 FREQ_DIS, 0);
239 #else
240         num_aiops = ctlp->NumAiop;
241 #endif /* notdef */
242
243         num_ports = 0;
244         for(aiop=0; aiop < num_aiops; aiop++) {
245                 sResetAiopByNum(ctlp, aiop);
246                 sEnAiop(rp_controller, ctlp, aiop);
247                 num_ports += sGetAiopNumChan(ctlp, aiop);
248         }
249
250         retval = rp_attachcommon(ctlp, num_aiops, num_ports);
251         if (retval != 0)
252                 goto nogo;
253
254         return (0);
255
256 nogo:
257         rp_isareleaseresource(ctlp);
258
259         return (retval);
260 }
261
262 static void
263 rp_isareleaseresource(CONTROLLER_t *ctlp)
264 {
265         int i;
266
267         rp_releaseresource(ctlp);
268
269         if (ctlp == rp_controller)
270                 rp_controller = NULL;
271         if (ctlp->io != NULL) {
272                 for (i = 0 ; i < MAX_AIOPS_PER_BOARD ; i++)
273                         if (ctlp->io[i] != NULL)
274                                 bus_release_resource(ctlp->dev, SYS_RES_IOPORT, ctlp->io_rid[i], ctlp->io[i]);
275                 kfree(ctlp->io, M_DEVBUF);
276         }
277         if (ctlp->io_rid != NULL)
278                 kfree(ctlp->io_rid, M_DEVBUF);
279         if (rp_controller != NULL && rp_controller->io[ISACTL(ctlp)->MBaseIO] != NULL) {
280                 bus_release_resource(rp_controller->dev, SYS_RES_IOPORT, rp_controller->io_rid[ISACTL(ctlp)->MBaseIO], rp_controller->io[ISACTL(ctlp)->MBaseIO]);
281                 rp_controller->io[ISACTL(ctlp)->MBaseIO] = NULL;
282                 rp_controller->io_rid[ISACTL(ctlp)->MBaseIO] = 0;
283         }
284         if (ctlp->bus_ctlp != NULL)
285                 kfree(ctlp->bus_ctlp, M_DEVBUF);
286 }
287
288 /***************************************************************************
289 Function: sInitController
290 Purpose:  Initialization of controller global registers and controller
291           structure.
292 Call:     sInitController(CtlP,MudbacCtlP,AiopNum,
293                           IRQNum,Frequency,PeriodicOnly)
294           CONTROLLER_T *CtlP; Ptr to controller structure
295           CONTROLLER_T *MudbacCtlP; Ptr to Mudbac controller structure
296           int AiopNum; Number of Aiops
297           int IRQNum; Interrupt Request number.  Can be any of the following:
298                          0: Disable global interrupts
299                          3: IRQ 3
300                          4: IRQ 4
301                          5: IRQ 5
302                          9: IRQ 9
303                          10: IRQ 10
304                          11: IRQ 11
305                          12: IRQ 12
306                          15: IRQ 15
307           Byte_t Frequency: A flag identifying the frequency
308                    of the periodic interrupt, can be any one of the following:
309                       FREQ_DIS - periodic interrupt disabled
310                       FREQ_137HZ - 137 Hertz
311                       FREQ_69HZ - 69 Hertz
312                       FREQ_34HZ - 34 Hertz
313                       FREQ_17HZ - 17 Hertz
314                       FREQ_9HZ - 9 Hertz
315                       FREQ_4HZ - 4 Hertz
316                    If IRQNum is set to 0 the Frequency parameter is
317                    overidden, it is forced to a value of FREQ_DIS.
318           int PeriodicOnly: TRUE if all interrupts except the periodic
319                                interrupt are to be blocked.
320                             FALSE is both the periodic interrupt and
321                                other channel interrupts are allowed.
322                             If IRQNum is set to 0 the PeriodicOnly parameter is
323                                overidden, it is forced to a value of FALSE.
324 Return:   int: Number of AIOPs on the controller, or CTLID_NULL if controller
325                initialization failed.
326
327 Comments:
328           If periodic interrupts are to be disabled but AIOP interrupts
329           are allowed, set Frequency to FREQ_DIS and PeriodicOnly to FALSE.
330
331           If interrupts are to be completely disabled set IRQNum to 0.
332
333           Setting Frequency to FREQ_DIS and PeriodicOnly to TRUE is an
334           invalid combination.
335
336           This function performs initialization of global interrupt modes,
337           but it does not actually enable global interrupts.  To enable
338           and disable global interrupts use functions sEnGlobalInt() and
339           sDisGlobalInt().  Enabling of global interrupts is normally not
340           done until all other initializations are complete.
341
342           Even if interrupts are globally enabled, they must also be
343           individually enabled for each channel that is to generate
344           interrupts.
345
346 Warnings: No range checking on any of the parameters is done.
347
348           No context switches are allowed while executing this function.
349
350           After this function all AIOPs on the controller are disabled,
351           they can be enabled with sEnAiop().
352 */
353 static int
354 sInitController(        CONTROLLER_T *CtlP,
355                         CONTROLLER_T *MudbacCtlP,
356                         int AiopNum,
357                         int IRQNum,
358                         Byte_t Frequency,
359                         int PeriodicOnly)
360 {
361         int             i;
362         int             ctl_base, aiop_base, aiop_size;
363
364         CtlP->CtlID = CTLID_0001;               /* controller release 1 */
365
366         ISACTL(CtlP)->MBaseIO = rp_nisadevs;
367         if (MudbacCtlP->io[ISACTL(CtlP)->MBaseIO] != NULL) {
368                 ISACTL(CtlP)->MReg0IO = 0x40 + 0;
369                 ISACTL(CtlP)->MReg1IO = 0x40 + 1;
370                 ISACTL(CtlP)->MReg2IO = 0x40 + 2;
371                 ISACTL(CtlP)->MReg3IO = 0x40 + 3;
372         } else {
373                 MudbacCtlP->io_rid[ISACTL(CtlP)->MBaseIO] = ISACTL(CtlP)->MBaseIO;
374                 ctl_base = rman_get_start(MudbacCtlP->io[0]) + 0x40 + 0x400 * rp_nisadevs;
375                 MudbacCtlP->io[ISACTL(CtlP)->MBaseIO] = bus_alloc_resource(MudbacCtlP->dev, SYS_RES_IOPORT, &CtlP->io_rid[ISACTL(CtlP)->MBaseIO], ctl_base, ctl_base + 3, 4, RF_ACTIVE);
376                 ISACTL(CtlP)->MReg0IO = 0;
377                 ISACTL(CtlP)->MReg1IO = 1;
378                 ISACTL(CtlP)->MReg2IO = 2;
379                 ISACTL(CtlP)->MReg3IO = 3;
380         }
381 #if 1
382         ISACTL(CtlP)->MReg2 = 0;                        /* interrupt disable */
383         ISACTL(CtlP)->MReg3 = 0;                        /* no periodic interrupts */
384 #else
385         if(sIRQMap[IRQNum] == 0)                /* interrupts globally disabled */
386         {
387                 ISACTL(CtlP)->MReg2 = 0;                /* interrupt disable */
388                 ISACTL(CtlP)->MReg3 = 0;                /* no periodic interrupts */
389         }
390         else
391         {
392                 ISACTL(CtlP)->MReg2 = sIRQMap[IRQNum];  /* set IRQ number */
393                 ISACTL(CtlP)->MReg3 = Frequency;        /* set frequency */
394                 if(PeriodicOnly)                /* periodic interrupt only */
395                 {
396                         ISACTL(CtlP)->MReg3 |= PERIODIC_ONLY;
397                 }
398         }
399 #endif
400         rp_writeio1(MudbacCtlP,ISACTL(CtlP)->MBaseIO,ISACTL(CtlP)->MReg2IO,ISACTL(CtlP)->MReg2);
401         rp_writeio1(MudbacCtlP,ISACTL(CtlP)->MBaseIO,ISACTL(CtlP)->MReg3IO,ISACTL(CtlP)->MReg3);
402         sControllerEOI(MudbacCtlP,CtlP);                        /* clear EOI if warm init */
403
404         /* Init AIOPs */
405         CtlP->NumAiop = 0;
406         for(i=0; i < AiopNum; i++)
407         {
408                 if (CtlP->io[i] == NULL) {
409                         CtlP->io_rid[i] = i;
410                         aiop_base = rman_get_start(CtlP->io[0]) + 0x400 * i;
411                         if (rp_nisadevs == 0)
412                                 aiop_size = 0x44;
413                         else
414                                 aiop_size = 0x40;
415                         CtlP->io[i] = bus_alloc_resource(CtlP->dev, SYS_RES_IOPORT, &CtlP->io_rid[i], aiop_base, aiop_base + aiop_size - 1, aiop_size, RF_ACTIVE);
416                 } else
417                         aiop_base = rman_get_start(CtlP->io[i]);
418                 rp_writeio1(MudbacCtlP,ISACTL(CtlP)->MBaseIO,
419                             ISACTL(CtlP)->MReg2IO,
420                             ISACTL(CtlP)->MReg2 | (i & 0x03));  /* AIOP index */
421                 rp_writeio1(MudbacCtlP,ISACTL(CtlP)->MBaseIO,
422                             ISACTL(CtlP)->MReg0IO,
423                             (Byte_t)(aiop_base >> 6));          /* set up AIOP I/O in MUDBAC */
424                 sEnAiop(MudbacCtlP,CtlP,i);                     /* enable the AIOP */
425
426                 CtlP->AiopID[i] = sReadAiopID(CtlP, i);         /* read AIOP ID */
427                 if(CtlP->AiopID[i] == AIOPID_NULL)              /* if AIOP does not exist */
428                 {
429                         sDisAiop(MudbacCtlP,CtlP,i);            /* disable AIOP */
430                         bus_release_resource(CtlP->dev, SYS_RES_IOPORT, CtlP->io_rid[i], CtlP->io[i]);
431                         CtlP->io[i] = NULL;
432                         break;                                  /* done looking for AIOPs */
433                 }
434
435                 CtlP->AiopNumChan[i] = sReadAiopNumChan(CtlP, i);       /* num channels in AIOP */
436                 rp_writeaiop2(CtlP,i,_INDX_ADDR,_CLK_PRE);      /* clock prescaler */
437                 rp_writeaiop1(CtlP,i,_INDX_DATA,CLOCK_PRESC);
438                 CtlP->NumAiop++;                                /* bump count of AIOPs */
439                 sDisAiop(MudbacCtlP,CtlP,i);                    /* disable AIOP */
440         }
441
442         if(CtlP->NumAiop == 0)
443                 return(-1);
444         else
445                 return(CtlP->NumAiop);
446 }
447
448 /*
449  * ARGSUSED
450  * Maps (aiop, offset) to rid.
451  */
452 static int
453 rp_isa_aiop2rid(int aiop, int offset)
454 {
455         /* rid equals to aiop for an ISA controller. */
456         return aiop;
457 }
458
459 /*
460  * ARGSUSED
461  * Maps (aiop, offset) to the offset of resource.
462  */
463 static int
464 rp_isa_aiop2off(int aiop, int offset)
465 {
466         /* Each aiop has its own resource. */
467         return offset;
468 }
469
470 /* Read the int status for an ISA controller. */
471 unsigned char
472 rp_isa_ctlmask(CONTROLLER_t *ctlp)
473 {
474         return sGetControllerIntStatus(rp_controller,ctlp);
475 }
476
477 static device_method_t rp_methods[] = {
478         /* Device interface */
479         DEVMETHOD(device_probe,         rp_probe),
480         DEVMETHOD(device_attach,        rp_attach),
481
482         { 0, 0 }
483 };
484
485 static driver_t rp_driver = {
486         "rp",
487         rp_methods,
488         sizeof(CONTROLLER_t),
489 };
490
491 /*
492  * rp can be attached to an isa bus.
493  */
494 DRIVER_MODULE(rp, isa, rp_driver, rp_devclass, 0, 0);