]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/pc98/pc98/ppc.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / sys / pc98 / pc98 / ppc.c
1 /*-
2  * Copyright (c) 1997-2000 Nicolas Souchu
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  *
28  */
29
30 #include "opt_ppc.h"
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/malloc.h>
37   
38 #include <vm/vm.h>
39 #include <vm/pmap.h>
40 #include <machine/clock.h>
41 #include <machine/bus.h>
42 #include <machine/resource.h>
43 #include <machine/vmparam.h>
44 #include <sys/rman.h>
45
46 #ifdef PC98
47 #include <pc98/pc98/pc98.h>
48 #else
49 #include <isa/isareg.h>
50 #endif
51 #include <isa/isavar.h>
52
53 #include <dev/ppbus/ppbconf.h>
54 #include <dev/ppbus/ppb_msq.h>
55
56 #ifdef PC98
57 #include <pc98/pc98/ppcreg.h>
58 #else
59 #include <isa/ppcreg.h>
60 #endif
61
62 #include "ppbus_if.h"
63
64 #define LOG_PPC(function, ppc, string) \
65                 if (bootverbose) printf("%s: %s\n", function, string)
66
67
68 #define DEVTOSOFTC(dev) ((struct ppc_data *)device_get_softc(dev))
69   
70 devclass_t ppc_devclass;
71
72 static int ppc_probe(device_t dev);
73 static int ppc_attach(device_t dev);
74 static int ppc_read_ivar(device_t bus, device_t dev, int index, uintptr_t *val);
75
76 static void ppc_reset_epp(device_t);
77 static void ppc_ecp_sync(device_t);
78 static void ppcintr(void *arg);
79
80 static int ppc_exec_microseq(device_t, struct ppb_microseq **);
81 static int ppc_setmode(device_t, int);
82
83 static int ppc_read(device_t, char *, int, int);
84 static int ppc_write(device_t, char *, int, int);
85
86 static u_char ppc_io(device_t, int, u_char *, int, u_char);
87
88 static int ppc_setup_intr(device_t, device_t, struct resource *, int,
89                 void (*)(void *), void *, void **);
90 static int ppc_teardown_intr(device_t, device_t, struct resource *, void *);
91
92 static device_method_t ppc_methods[] = {
93         /* device interface */
94         DEVMETHOD(device_probe,         ppc_probe),
95         DEVMETHOD(device_attach,        ppc_attach),
96
97         /* bus interface */
98         DEVMETHOD(bus_read_ivar,        ppc_read_ivar),
99         DEVMETHOD(bus_setup_intr,       ppc_setup_intr),
100         DEVMETHOD(bus_teardown_intr,    ppc_teardown_intr),
101         DEVMETHOD(bus_alloc_resource,   bus_generic_alloc_resource),
102
103         /* ppbus interface */
104         DEVMETHOD(ppbus_io,             ppc_io),
105         DEVMETHOD(ppbus_exec_microseq,  ppc_exec_microseq),
106         DEVMETHOD(ppbus_reset_epp,      ppc_reset_epp),
107         DEVMETHOD(ppbus_setmode,        ppc_setmode),
108         DEVMETHOD(ppbus_ecp_sync,       ppc_ecp_sync),
109         DEVMETHOD(ppbus_read,           ppc_read),
110         DEVMETHOD(ppbus_write,          ppc_write),
111
112         { 0, 0 }
113   };
114   
115 static driver_t ppc_driver = {
116         "ppc",
117         ppc_methods,
118         sizeof(struct ppc_data),
119 };
120   
121 static char *ppc_models[] = {
122         "SMC-like", "SMC FDC37C665GT", "SMC FDC37C666GT", "PC87332", "PC87306",
123         "82091AA", "Generic", "W83877F", "W83877AF", "Winbond", "PC87334", 0
124 };
125
126 /* list of available modes */
127 static char *ppc_avms[] = {
128         "COMPATIBLE", "NIBBLE-only", "PS2-only", "PS2/NIBBLE", "EPP-only",
129         "EPP/NIBBLE", "EPP/PS2", "EPP/PS2/NIBBLE", "ECP-only",
130         "ECP/NIBBLE", "ECP/PS2", "ECP/PS2/NIBBLE", "ECP/EPP",
131         "ECP/EPP/NIBBLE", "ECP/EPP/PS2", "ECP/EPP/PS2/NIBBLE", 0
132 };
133
134 /* list of current executing modes
135  * Note that few modes do not actually exist.
136  */
137 static char *ppc_modes[] = {
138         "COMPATIBLE", "NIBBLE", "PS/2", "PS/2", "EPP",
139         "EPP", "EPP", "EPP", "ECP",
140         "ECP", "ECP+PS2", "ECP+PS2", "ECP+EPP",
141         "ECP+EPP", "ECP+EPP", "ECP+EPP", 0
142 };
143
144 static char *ppc_epp_protocol[] = { " (EPP 1.9)", " (EPP 1.7)", 0 };
145
146 #ifdef __i386__
147 /*
148  * BIOS printer list - used by BIOS probe.
149  */
150 #define BIOS_PPC_PORTS  0x408
151 #define BIOS_PORTS      (short *)(KERNBASE+BIOS_PPC_PORTS)
152 #define BIOS_MAX_PPC    4
153 #endif
154
155 /*
156  * ppc_ecp_sync()               XXX
157  */
158 static void
159 ppc_ecp_sync(device_t dev) {
160
161         int i, r;
162         struct ppc_data *ppc = DEVTOSOFTC(dev);
163
164         if (!(ppc->ppc_avm & PPB_ECP))
165                 return;
166
167         r = r_ecr(ppc);
168         if ((r & 0xe0) != PPC_ECR_EPP)
169                 return;
170
171         for (i = 0; i < 100; i++) {
172                 r = r_ecr(ppc);
173                 if (r & 0x1)
174                         return;
175                 DELAY(100);
176         }
177
178         printf("ppc%d: ECP sync failed as data still " \
179                 "present in FIFO.\n", ppc->ppc_unit);
180
181         return;
182 }
183
184 /*
185  * ppc_detect_fifo()
186  *
187  * Detect parallel port FIFO
188  */
189 static int
190 ppc_detect_fifo(struct ppc_data *ppc)
191 {
192         char ecr_sav;
193         char ctr_sav, ctr, cc;
194         short i;
195         
196         /* save registers */
197         ecr_sav = r_ecr(ppc);
198         ctr_sav = r_ctr(ppc);
199
200         /* enter ECP configuration mode, no interrupt, no DMA */
201         w_ecr(ppc, 0xf4);
202
203         /* read PWord size - transfers in FIFO mode must be PWord aligned */
204         ppc->ppc_pword = (r_cnfgA(ppc) & PPC_PWORD_MASK);
205
206         /* XXX 16 and 32 bits implementations not supported */
207         if (ppc->ppc_pword != PPC_PWORD_8) {
208                 LOG_PPC(__FUNCTION__, ppc, "PWord not supported");
209                 goto error;
210         }
211
212         w_ecr(ppc, 0x34);               /* byte mode, no interrupt, no DMA */
213         ctr = r_ctr(ppc);
214         w_ctr(ppc, ctr | PCD);          /* set direction to 1 */
215
216         /* enter ECP test mode, no interrupt, no DMA */
217         w_ecr(ppc, 0xd4);
218
219         /* flush the FIFO */
220         for (i=0; i<1024; i++) {
221                 if (r_ecr(ppc) & PPC_FIFO_EMPTY)
222                         break;
223                 cc = r_fifo(ppc);
224         }
225
226         if (i >= 1024) {
227                 LOG_PPC(__FUNCTION__, ppc, "can't flush FIFO");
228                 goto error;
229         }
230
231         /* enable interrupts, no DMA */
232         w_ecr(ppc, 0xd0);
233
234         /* determine readIntrThreshold
235          * fill the FIFO until serviceIntr is set
236          */
237         for (i=0; i<1024; i++) {
238                 w_fifo(ppc, (char)i);
239                 if (!ppc->ppc_rthr && (r_ecr(ppc) & PPC_SERVICE_INTR)) {
240                         /* readThreshold reached */
241                         ppc->ppc_rthr = i+1;
242                 }
243                 if (r_ecr(ppc) & PPC_FIFO_FULL) {
244                         ppc->ppc_fifo = i+1;
245                         break;
246                 }
247         }
248
249         if (i >= 1024) {
250                 LOG_PPC(__FUNCTION__, ppc, "can't fill FIFO");
251                 goto error;
252         }
253
254         w_ecr(ppc, 0xd4);               /* test mode, no interrupt, no DMA */
255         w_ctr(ppc, ctr & ~PCD);         /* set direction to 0 */
256         w_ecr(ppc, 0xd0);               /* enable interrupts */
257
258         /* determine writeIntrThreshold
259          * empty the FIFO until serviceIntr is set
260          */
261         for (i=ppc->ppc_fifo; i>0; i--) {
262                 if (r_fifo(ppc) != (char)(ppc->ppc_fifo-i)) {
263                         LOG_PPC(__FUNCTION__, ppc, "invalid data in FIFO");
264                         goto error;
265                 }
266                 if (r_ecr(ppc) & PPC_SERVICE_INTR) {
267                         /* writeIntrThreshold reached */
268                         ppc->ppc_wthr = ppc->ppc_fifo - i+1;
269                 }
270                 /* if FIFO empty before the last byte, error */
271                 if (i>1 && (r_ecr(ppc) & PPC_FIFO_EMPTY)) {
272                         LOG_PPC(__FUNCTION__, ppc, "data lost in FIFO");
273                         goto error;
274                 }
275         }
276
277         /* FIFO must be empty after the last byte */
278         if (!(r_ecr(ppc) & PPC_FIFO_EMPTY)) {
279                 LOG_PPC(__FUNCTION__, ppc, "can't empty the FIFO");
280                 goto error;
281         }
282         
283         w_ctr(ppc, ctr_sav);
284         w_ecr(ppc, ecr_sav);
285
286         return (0);
287
288 error:
289         w_ctr(ppc, ctr_sav);
290         w_ecr(ppc, ecr_sav);
291
292         return (EINVAL);
293 }
294
295 static int
296 ppc_detect_port(struct ppc_data *ppc)
297 {
298
299         w_ctr(ppc, 0x0c);       /* To avoid missing PS2 ports */
300         w_dtr(ppc, 0xaa);
301         if (r_dtr(ppc) != 0xaa)
302                 return (0);
303
304         return (1);
305 }
306
307 /*
308  * EPP timeout, according to the PC87332 manual
309  * Semantics of clearing EPP timeout bit.
310  * PC87332      - reading SPP_STR does it...
311  * SMC          - write 1 to EPP timeout bit                    XXX
312  * Others       - (?) write 0 to EPP timeout bit
313  */
314 static void
315 ppc_reset_epp_timeout(struct ppc_data *ppc)
316 {
317         register char r;
318
319         r = r_str(ppc);
320         w_str(ppc, r | 0x1);
321         w_str(ppc, r & 0xfe);
322
323         return;
324 }
325
326 static int
327 ppc_check_epp_timeout(struct ppc_data *ppc)
328 {
329         ppc_reset_epp_timeout(ppc);
330
331         return (!(r_str(ppc) & TIMEOUT));
332 }
333
334 /*
335  * Configure current operating mode
336  */
337 static int
338 ppc_generic_setmode(struct ppc_data *ppc, int mode)
339 {
340         u_char ecr = 0;
341
342         /* check if mode is available */
343         if (mode && !(ppc->ppc_avm & mode))
344                 return (EINVAL);
345
346         /* if ECP mode, configure ecr register */
347         if (ppc->ppc_avm & PPB_ECP) {
348                 /* return to byte mode (keeping direction bit),
349                  * no interrupt, no DMA to be able to change to
350                  * ECP
351                  */
352                 w_ecr(ppc, PPC_ECR_RESET);
353                 ecr = PPC_DISABLE_INTR;
354
355                 if (mode & PPB_EPP)
356                         return (EINVAL);
357                 else if (mode & PPB_ECP)
358                         /* select ECP mode */
359                         ecr |= PPC_ECR_ECP;
360                 else if (mode & PPB_PS2)
361                         /* select PS2 mode with ECP */
362                         ecr |= PPC_ECR_PS2;
363                 else
364                         /* select COMPATIBLE/NIBBLE mode */
365                         ecr |= PPC_ECR_STD;
366
367                 w_ecr(ppc, ecr);
368         }
369
370         ppc->ppc_mode = mode;
371
372         return (0);
373 }
374
375 /*
376  * The ppc driver is free to choose options like FIFO or DMA
377  * if ECP mode is available.
378  *
379  * The 'RAW' option allows the upper drivers to force the ppc mode
380  * even with FIFO, DMA available.
381  */
382 static int
383 ppc_smclike_setmode(struct ppc_data *ppc, int mode)
384 {
385         u_char ecr = 0;
386
387         /* check if mode is available */
388         if (mode && !(ppc->ppc_avm & mode))
389                 return (EINVAL);
390
391         /* if ECP mode, configure ecr register */
392         if (ppc->ppc_avm & PPB_ECP) {
393                 /* return to byte mode (keeping direction bit),
394                  * no interrupt, no DMA to be able to change to
395                  * ECP or EPP mode
396                  */
397                 w_ecr(ppc, PPC_ECR_RESET);
398                 ecr = PPC_DISABLE_INTR;
399
400                 if (mode & PPB_EPP)
401                         /* select EPP mode */
402                         ecr |= PPC_ECR_EPP;
403                 else if (mode & PPB_ECP)
404                         /* select ECP mode */
405                         ecr |= PPC_ECR_ECP;
406                 else if (mode & PPB_PS2)
407                         /* select PS2 mode with ECP */
408                         ecr |= PPC_ECR_PS2;
409                 else
410                         /* select COMPATIBLE/NIBBLE mode */
411                         ecr |= PPC_ECR_STD;
412
413                 w_ecr(ppc, ecr);
414         }
415
416         ppc->ppc_mode = mode;
417
418         return (0);
419 }
420
421 #ifdef PPC_PROBE_CHIPSET
422 /*
423  * ppc_pc873xx_detect
424  *
425  * Probe for a Natsemi PC873xx-family part.
426  *
427  * References in this function are to the National Semiconductor
428  * PC87332 datasheet TL/C/11930, May 1995 revision.
429  */
430 static int pc873xx_basetab[] = {0x0398, 0x026e, 0x015c, 0x002e, 0};
431 static int pc873xx_porttab[] = {0x0378, 0x03bc, 0x0278, 0};
432 static int pc873xx_irqtab[] = {5, 7, 5, 0};
433
434 static int pc873xx_regstab[] = {
435         PC873_FER, PC873_FAR, PC873_PTR,
436         PC873_FCR, PC873_PCR, PC873_PMC,
437         PC873_TUP, PC873_SID, PC873_PNP0,
438         PC873_PNP1, PC873_LPTBA, -1
439 };
440
441 static char *pc873xx_rnametab[] = {
442         "FER", "FAR", "PTR", "FCR", "PCR",
443         "PMC", "TUP", "SID", "PNP0", "PNP1",
444         "LPTBA", NULL
445 };
446
447 static int
448 ppc_pc873xx_detect(struct ppc_data *ppc, int chipset_mode)      /* XXX mode never forced */
449 {
450     static int  index = 0;
451     int         idport, irq;
452     int         ptr, pcr, val, i;
453     
454     while ((idport = pc873xx_basetab[index++])) {
455         
456         /* XXX should check first to see if this location is already claimed */
457
458         /*
459          * Pull the 873xx through the power-on ID cycle (2.2,1.).
460          * We can't use this to locate the chip as it may already have
461          * been used by the BIOS.
462          */
463         (void)inb(idport); (void)inb(idport);
464         (void)inb(idport); (void)inb(idport);
465
466         /*
467          * Read the SID byte.  Possible values are :
468          *
469          * 01010xxx     PC87334
470          * 0001xxxx     PC87332
471          * 01110xxx     PC87306
472          */
473         outb(idport, PC873_SID);
474         val = inb(idport + 1);
475         if ((val & 0xf0) == 0x10) {
476             ppc->ppc_model = NS_PC87332;
477         } else if ((val & 0xf8) == 0x70) {
478             ppc->ppc_model = NS_PC87306;
479         } else if ((val & 0xf8) == 0x50) {
480             ppc->ppc_model = NS_PC87334;
481         } else {
482             if (bootverbose && (val != 0xff))
483                 printf("PC873xx probe at 0x%x got unknown ID 0x%x\n", idport, val);
484             continue ;          /* not recognised */
485         }
486
487         /* print registers */
488         if (bootverbose) {
489                 printf("PC873xx");
490                 for (i=0; pc873xx_regstab[i] != -1; i++) {
491                         outb(idport, pc873xx_regstab[i]);
492                         printf(" %s=0x%x", pc873xx_rnametab[i],
493                                                 inb(idport + 1) & 0xff);
494                 }
495                 printf("\n");
496         }
497         
498         /*
499          * We think we have one.  Is it enabled and where we want it to be?
500          */
501         outb(idport, PC873_FER);
502         val = inb(idport + 1);
503         if (!(val & PC873_PPENABLE)) {
504             if (bootverbose)
505                 printf("PC873xx parallel port disabled\n");
506             continue;
507         }
508         outb(idport, PC873_FAR);
509         val = inb(idport + 1) & 0x3;
510         /* XXX we should create a driver instance for every port found */
511         if (pc873xx_porttab[val] != ppc->ppc_base) {
512             if (bootverbose)
513                 printf("PC873xx at 0x%x not for driver at port 0x%x\n",
514                        pc873xx_porttab[val], ppc->ppc_base);
515             continue;
516         }
517
518         outb(idport, PC873_PTR);
519         ptr = inb(idport + 1);
520
521         /* get irq settings */
522         if (ppc->ppc_base == 0x378)
523                 irq = (ptr & PC873_LPTBIRQ7) ? 7 : 5;
524         else
525                 irq = pc873xx_irqtab[val];
526
527         if (bootverbose)
528                 printf("PC873xx irq %d at 0x%x\n", irq, ppc->ppc_base);
529         
530         /*
531          * Check if irq settings are correct
532          */
533         if (irq != ppc->ppc_irq) {
534                 /*
535                  * If the chipset is not locked and base address is 0x378,
536                  * we have another chance
537                  */
538                 if (ppc->ppc_base == 0x378 && !(ptr & PC873_CFGLOCK)) {
539                         if (ppc->ppc_irq == 7) {
540                                 outb(idport + 1, (ptr | PC873_LPTBIRQ7));
541                                 outb(idport + 1, (ptr | PC873_LPTBIRQ7));
542                         } else {
543                                 outb(idport + 1, (ptr & ~PC873_LPTBIRQ7));
544                                 outb(idport + 1, (ptr & ~PC873_LPTBIRQ7));
545                         }
546                         if (bootverbose)
547                            printf("PC873xx irq set to %d\n", ppc->ppc_irq);
548                 } else {
549                         if (bootverbose)
550                            printf("PC873xx sorry, can't change irq setting\n");
551                 }
552         } else {
553                 if (bootverbose)
554                         printf("PC873xx irq settings are correct\n");
555         }
556
557         outb(idport, PC873_PCR);
558         pcr = inb(idport + 1);
559         
560         if ((ptr & PC873_CFGLOCK) || !chipset_mode) {
561             if (bootverbose)
562                 printf("PC873xx %s", (ptr & PC873_CFGLOCK)?"locked":"unlocked");
563
564             ppc->ppc_avm |= PPB_NIBBLE;
565             if (bootverbose)
566                 printf(", NIBBLE");
567
568             if (pcr & PC873_EPPEN) {
569                 ppc->ppc_avm |= PPB_EPP;
570
571                 if (bootverbose)
572                         printf(", EPP");
573
574                 if (pcr & PC873_EPP19)
575                         ppc->ppc_epp = EPP_1_9;
576                 else
577                         ppc->ppc_epp = EPP_1_7;
578
579                 if ((ppc->ppc_model == NS_PC87332) && bootverbose) {
580                         outb(idport, PC873_PTR);
581                         ptr = inb(idport + 1);
582                         if (ptr & PC873_EPPRDIR)
583                                 printf(", Regular mode");
584                         else
585                                 printf(", Automatic mode");
586                 }
587             } else if (pcr & PC873_ECPEN) {
588                 ppc->ppc_avm |= PPB_ECP;
589                 if (bootverbose)
590                         printf(", ECP");
591
592                 if (pcr & PC873_ECPCLK) {               /* XXX */
593                         ppc->ppc_avm |= PPB_PS2;
594                         if (bootverbose)
595                                 printf(", PS/2");
596                 }
597             } else {
598                 outb(idport, PC873_PTR);
599                 ptr = inb(idport + 1);
600                 if (ptr & PC873_EXTENDED) {
601                         ppc->ppc_avm |= PPB_SPP;
602                         if (bootverbose)
603                                 printf(", SPP");
604                 }
605             }
606         } else {
607                 if (bootverbose)
608                         printf("PC873xx unlocked");
609
610                 if (chipset_mode & PPB_ECP) {
611                         if ((chipset_mode & PPB_EPP) && bootverbose)
612                                 printf(", ECP+EPP not supported");
613
614                         pcr &= ~PC873_EPPEN;
615                         pcr |= (PC873_ECPEN | PC873_ECPCLK);    /* XXX */
616                         outb(idport + 1, pcr);
617                         outb(idport + 1, pcr);
618
619                         if (bootverbose)
620                                 printf(", ECP");
621
622                 } else if (chipset_mode & PPB_EPP) {
623                         pcr &= ~(PC873_ECPEN | PC873_ECPCLK);
624                         pcr |= (PC873_EPPEN | PC873_EPP19);
625                         outb(idport + 1, pcr);
626                         outb(idport + 1, pcr);
627
628                         ppc->ppc_epp = EPP_1_9;                 /* XXX */
629
630                         if (bootverbose)
631                                 printf(", EPP1.9");
632
633                         /* enable automatic direction turnover */
634                         if (ppc->ppc_model == NS_PC87332) {
635                                 outb(idport, PC873_PTR);
636                                 ptr = inb(idport + 1);
637                                 ptr &= ~PC873_EPPRDIR;
638                                 outb(idport + 1, ptr);
639                                 outb(idport + 1, ptr);
640
641                                 if (bootverbose)
642                                         printf(", Automatic mode");
643                         }
644                 } else {
645                         pcr &= ~(PC873_ECPEN | PC873_ECPCLK | PC873_EPPEN);
646                         outb(idport + 1, pcr);
647                         outb(idport + 1, pcr);
648
649                         /* configure extended bit in PTR */
650                         outb(idport, PC873_PTR);
651                         ptr = inb(idport + 1);
652
653                         if (chipset_mode & PPB_PS2) {
654                                 ptr |= PC873_EXTENDED;
655
656                                 if (bootverbose)
657                                         printf(", PS/2");
658                         
659                         } else {
660                                 /* default to NIBBLE mode */
661                                 ptr &= ~PC873_EXTENDED;
662
663                                 if (bootverbose)
664                                         printf(", NIBBLE");
665                         }
666                         outb(idport + 1, ptr);
667                         outb(idport + 1, ptr);
668                 }
669
670                 ppc->ppc_avm = chipset_mode;
671         }
672
673         if (bootverbose)
674                 printf("\n");
675
676         ppc->ppc_type = PPC_TYPE_GENERIC;
677         ppc_generic_setmode(ppc, chipset_mode);
678
679         return(chipset_mode);
680     }
681     return(-1);
682 }
683
684 /*
685  * ppc_smc37c66xgt_detect
686  *
687  * SMC FDC37C66xGT configuration.
688  */
689 static int
690 ppc_smc37c66xgt_detect(struct ppc_data *ppc, int chipset_mode)
691 {
692         int s, i;
693         u_char r;
694         int type = -1;
695         int csr = SMC66x_CSR;   /* initial value is 0x3F0 */
696
697         int port_address[] = { -1 /* disabled */ , 0x3bc, 0x378, 0x278 };
698
699
700 #define cio csr+1       /* config IO port is either 0x3F1 or 0x371 */
701
702         /*
703          * Detection: enter configuration mode and read CRD register.
704          */
705          
706         s = splhigh();
707         outb(csr, SMC665_iCODE);
708         outb(csr, SMC665_iCODE);
709         splx(s);
710
711         outb(csr, 0xd);
712         if (inb(cio) == 0x65) {
713                 type = SMC_37C665GT;
714                 goto config;
715         }
716
717         for (i = 0; i < 2; i++) {
718                 s = splhigh();
719                 outb(csr, SMC666_iCODE);
720                 outb(csr, SMC666_iCODE);
721                 splx(s);
722
723                 outb(csr, 0xd);
724                 if (inb(cio) == 0x66) {
725                         type = SMC_37C666GT;
726                         break;
727                 }
728
729                 /* Another chance, CSR may be hard-configured to be at 0x370 */
730                 csr = SMC666_CSR;
731         }
732
733 config:
734         /*
735          * If chipset not found, do not continue.
736          */
737         if (type == -1)
738                 return (-1);
739
740         /* select CR1 */
741         outb(csr, 0x1);
742
743         /* read the port's address: bits 0 and 1 of CR1 */
744         r = inb(cio) & SMC_CR1_ADDR;
745         if (port_address[(int)r] != ppc->ppc_base)
746                 return (-1);
747
748         ppc->ppc_model = type;
749
750         /*
751          * CR1 and CR4 registers bits 3 and 0/1 for mode configuration
752          * If SPP mode is detected, try to set ECP+EPP mode
753          */
754
755         if (bootverbose) {
756                 outb(csr, 0x1);
757                 printf("ppc%d: SMC registers CR1=0x%x", ppc->ppc_unit,
758                         inb(cio) & 0xff);
759
760                 outb(csr, 0x4);
761                 printf(" CR4=0x%x", inb(cio) & 0xff);
762         }
763
764         /* select CR1 */
765         outb(csr, 0x1);
766
767         if (!chipset_mode) {
768                 /* autodetect mode */
769
770                 /* 666GT is ~certainly~ hardwired to an extended ECP+EPP mode */
771                 if (type == SMC_37C666GT) {
772                         ppc->ppc_avm |= PPB_ECP | PPB_EPP | PPB_SPP;
773                         if (bootverbose)
774                                 printf(" configuration hardwired, supposing " \
775                                         "ECP+EPP SPP");
776
777                 } else
778                    if ((inb(cio) & SMC_CR1_MODE) == 0) {
779                         /* already in extended parallel port mode, read CR4 */
780                         outb(csr, 0x4);
781                         r = (inb(cio) & SMC_CR4_EMODE);
782
783                         switch (r) {
784                         case SMC_SPP:
785                                 ppc->ppc_avm |= PPB_SPP;
786                                 if (bootverbose)
787                                         printf(" SPP");
788                                 break;
789
790                         case SMC_EPPSPP:
791                                 ppc->ppc_avm |= PPB_EPP | PPB_SPP;
792                                 if (bootverbose)
793                                         printf(" EPP SPP");
794                                 break;
795
796                         case SMC_ECP:
797                                 ppc->ppc_avm |= PPB_ECP | PPB_SPP;
798                                 if (bootverbose)
799                                         printf(" ECP SPP");
800                                 break;
801
802                         case SMC_ECPEPP:
803                                 ppc->ppc_avm |= PPB_ECP | PPB_EPP | PPB_SPP;
804                                 if (bootverbose)
805                                         printf(" ECP+EPP SPP");
806                                 break;
807                         }
808                    } else {
809                         /* not an extended port mode */
810                         ppc->ppc_avm |= PPB_SPP;
811                         if (bootverbose)
812                                 printf(" SPP");
813                    }
814
815         } else {
816                 /* mode forced */
817                 ppc->ppc_avm = chipset_mode;
818
819                 /* 666GT is ~certainly~ hardwired to an extended ECP+EPP mode */
820                 if (type == SMC_37C666GT)
821                         goto end_detect;
822
823                 r = inb(cio);
824                 if ((chipset_mode & (PPB_ECP | PPB_EPP)) == 0) {
825                         /* do not use ECP when the mode is not forced to */
826                         outb(cio, r | SMC_CR1_MODE);
827                         if (bootverbose)
828                                 printf(" SPP");
829                 } else {
830                         /* an extended mode is selected */
831                         outb(cio, r & ~SMC_CR1_MODE);
832
833                         /* read CR4 register and reset mode field */
834                         outb(csr, 0x4);
835                         r = inb(cio) & ~SMC_CR4_EMODE;
836
837                         if (chipset_mode & PPB_ECP) {
838                                 if (chipset_mode & PPB_EPP) {
839                                         outb(cio, r | SMC_ECPEPP);
840                                         if (bootverbose)
841                                                 printf(" ECP+EPP");
842                                 } else {
843                                         outb(cio, r | SMC_ECP);
844                                         if (bootverbose)
845                                                 printf(" ECP");
846                                 }
847                         } else {
848                                 /* PPB_EPP is set */
849                                 outb(cio, r | SMC_EPPSPP);
850                                 if (bootverbose)
851                                         printf(" EPP SPP");
852                         }
853                 }
854                 ppc->ppc_avm = chipset_mode;
855         }
856
857         /* set FIFO threshold to 16 */
858         if (ppc->ppc_avm & PPB_ECP) {
859                 /* select CRA */
860                 outb(csr, 0xa);
861                 outb(cio, 16);
862         }
863
864 end_detect:
865
866         if (bootverbose)
867                 printf ("\n");
868
869         if (ppc->ppc_avm & PPB_EPP) {
870                 /* select CR4 */
871                 outb(csr, 0x4);
872                 r = inb(cio);
873
874                 /*
875                  * Set the EPP protocol...
876                  * Low=EPP 1.9 (1284 standard) and High=EPP 1.7
877                  */
878                 if (ppc->ppc_epp == EPP_1_9)
879                         outb(cio, (r & ~SMC_CR4_EPPTYPE));
880                 else
881                         outb(cio, (r | SMC_CR4_EPPTYPE));
882         }
883
884         /* end config mode */
885         outb(csr, 0xaa);
886
887         ppc->ppc_type = PPC_TYPE_SMCLIKE;
888         ppc_smclike_setmode(ppc, chipset_mode);
889
890         return (chipset_mode);
891 }
892
893 /*
894  * Winbond W83877F stuff
895  *
896  * EFER: extended function enable register
897  * EFIR: extended function index register
898  * EFDR: extended function data register
899  */
900 #define efir ((efer == 0x250) ? 0x251 : 0x3f0)
901 #define efdr ((efer == 0x250) ? 0x252 : 0x3f1)
902
903 static int w83877f_efers[] = { 0x250, 0x3f0, 0x3f0, 0x250 };
904 static int w83877f_keys[] = { 0x89, 0x86, 0x87, 0x88 }; 
905 static int w83877f_keyiter[] = { 1, 2, 2, 1 };
906 static int w83877f_hefs[] = { WINB_HEFERE, WINB_HEFRAS, WINB_HEFERE | WINB_HEFRAS, 0 };
907
908 static int
909 ppc_w83877f_detect(struct ppc_data *ppc, int chipset_mode)
910 {
911         int i, j, efer;
912         unsigned char r, hefere, hefras;
913
914         for (i = 0; i < 4; i ++) {
915                 /* first try to enable configuration registers */
916                 efer = w83877f_efers[i];
917
918                 /* write the key to the EFER */
919                 for (j = 0; j < w83877f_keyiter[i]; j ++)
920                         outb (efer, w83877f_keys[i]);
921
922                 /* then check HEFERE and HEFRAS bits */
923                 outb (efir, 0x0c);
924                 hefere = inb(efdr) & WINB_HEFERE;
925
926                 outb (efir, 0x16);
927                 hefras = inb(efdr) & WINB_HEFRAS;
928
929                 /*
930                  * HEFRAS       HEFERE
931                  *   0             1    write 89h to 250h (power-on default)
932                  *   1             0    write 86h twice to 3f0h
933                  *   1             1    write 87h twice to 3f0h
934                  *   0             0    write 88h to 250h
935                  */
936                 if ((hefere | hefras) == w83877f_hefs[i])
937                         goto found;
938         }
939
940         return (-1);    /* failed */
941
942 found:
943         /* check base port address - read from CR23 */
944         outb(efir, 0x23);
945         if (ppc->ppc_base != inb(efdr) * 4)             /* 4 bytes boundaries */
946                 return (-1);
947
948         /* read CHIP ID from CR9/bits0-3 */
949         outb(efir, 0x9);
950
951         switch (inb(efdr) & WINB_CHIPID) {
952                 case WINB_W83877F_ID:
953                         ppc->ppc_model = WINB_W83877F;
954                         break;
955
956                 case WINB_W83877AF_ID:
957                         ppc->ppc_model = WINB_W83877AF;
958                         break;
959
960                 default:
961                         ppc->ppc_model = WINB_UNKNOWN;
962         }
963
964         if (bootverbose) {
965                 /* dump of registers */
966                 printf("ppc%d: 0x%x - ", ppc->ppc_unit, w83877f_keys[i]);
967                 for (i = 0; i <= 0xd; i ++) {
968                         outb(efir, i);
969                         printf("0x%x ", inb(efdr));
970                 }
971                 for (i = 0x10; i <= 0x17; i ++) {
972                         outb(efir, i);
973                         printf("0x%x ", inb(efdr));
974                 }
975                 outb(efir, 0x1e);
976                 printf("0x%x ", inb(efdr));
977                 for (i = 0x20; i <= 0x29; i ++) {
978                         outb(efir, i);
979                         printf("0x%x ", inb(efdr));
980                 }
981                 printf("\n");
982                 printf("ppc%d:", ppc->ppc_unit);
983         }
984
985         ppc->ppc_type = PPC_TYPE_GENERIC;
986
987         if (!chipset_mode) {
988                 /* autodetect mode */
989
990                 /* select CR0 */
991                 outb(efir, 0x0);
992                 r = inb(efdr) & (WINB_PRTMODS0 | WINB_PRTMODS1);
993
994                 /* select CR9 */
995                 outb(efir, 0x9);
996                 r |= (inb(efdr) & WINB_PRTMODS2);
997
998                 switch (r) {
999                 case WINB_W83757:
1000                         if (bootverbose)
1001                                 printf("ppc%d: W83757 compatible mode\n",
1002                                         ppc->ppc_unit);
1003                         return (-1);    /* generic or SMC-like */
1004
1005                 case WINB_EXTFDC:
1006                 case WINB_EXTADP:
1007                 case WINB_EXT2FDD:
1008                 case WINB_JOYSTICK:
1009                         if (bootverbose)
1010                                 printf(" not in parallel port mode\n");
1011                         return (-1);
1012
1013                 case (WINB_PARALLEL | WINB_EPP_SPP):
1014                         ppc->ppc_avm |= PPB_EPP | PPB_SPP;
1015                         if (bootverbose)
1016                                 printf(" EPP SPP");
1017                         break;
1018
1019                 case (WINB_PARALLEL | WINB_ECP):
1020                         ppc->ppc_avm |= PPB_ECP | PPB_SPP;
1021                         if (bootverbose)
1022                                 printf(" ECP SPP");
1023                         break;
1024
1025                 case (WINB_PARALLEL | WINB_ECP_EPP):
1026                         ppc->ppc_avm |= PPB_ECP | PPB_EPP | PPB_SPP;
1027                         ppc->ppc_type = PPC_TYPE_SMCLIKE;
1028
1029                         if (bootverbose)
1030                                 printf(" ECP+EPP SPP");
1031                         break;
1032                 default:
1033                         printf("%s: unknown case (0x%x)!\n", __FUNCTION__, r);
1034                 }
1035
1036         } else {
1037                 /* mode forced */
1038
1039                 /* select CR9 and set PRTMODS2 bit */
1040                 outb(efir, 0x9);
1041                 outb(efdr, inb(efdr) & ~WINB_PRTMODS2);
1042
1043                 /* select CR0 and reset PRTMODSx bits */
1044                 outb(efir, 0x0);
1045                 outb(efdr, inb(efdr) & ~(WINB_PRTMODS0 | WINB_PRTMODS1));
1046
1047                 if (chipset_mode & PPB_ECP) {
1048                         if (chipset_mode & PPB_EPP) {
1049                                 outb(efdr, inb(efdr) | WINB_ECP_EPP);
1050                                 if (bootverbose)
1051                                         printf(" ECP+EPP");
1052
1053                                 ppc->ppc_type = PPC_TYPE_SMCLIKE;
1054
1055                         } else {
1056                                 outb(efdr, inb(efdr) | WINB_ECP);
1057                                 if (bootverbose)
1058                                         printf(" ECP");
1059                         }
1060                 } else {
1061                         /* select EPP_SPP otherwise */
1062                         outb(efdr, inb(efdr) | WINB_EPP_SPP);
1063                         if (bootverbose)
1064                                 printf(" EPP SPP");
1065                 }
1066                 ppc->ppc_avm = chipset_mode;
1067         }
1068
1069         if (bootverbose)
1070                 printf("\n");
1071         
1072         /* exit configuration mode */
1073         outb(efer, 0xaa);
1074
1075         switch (ppc->ppc_type) {
1076         case PPC_TYPE_SMCLIKE:
1077                 ppc_smclike_setmode(ppc, chipset_mode);
1078                 break;
1079         default:
1080                 ppc_generic_setmode(ppc, chipset_mode);
1081                 break;
1082         }
1083
1084         return (chipset_mode);
1085 }
1086 #endif
1087
1088 /*
1089  * ppc_generic_detect
1090  */
1091 static int
1092 ppc_generic_detect(struct ppc_data *ppc, int chipset_mode)
1093 {
1094         /* default to generic */
1095         ppc->ppc_type = PPC_TYPE_GENERIC;
1096
1097         if (bootverbose)
1098                 printf("ppc%d:", ppc->ppc_unit);
1099
1100         if (!chipset_mode) {
1101                 /* first, check for ECP */
1102                 w_ecr(ppc, PPC_ECR_PS2);
1103                 if ((r_ecr(ppc) & 0xe0) == PPC_ECR_PS2) {
1104                         ppc->ppc_avm |= PPB_ECP | PPB_SPP;
1105                         if (bootverbose)
1106                                 printf(" ECP SPP");
1107
1108                         /* search for SMC style ECP+EPP mode */
1109                         w_ecr(ppc, PPC_ECR_EPP);
1110                 }
1111
1112                 /* try to reset EPP timeout bit */
1113                 if (ppc_check_epp_timeout(ppc)) {
1114                         ppc->ppc_avm |= PPB_EPP;
1115
1116                         if (ppc->ppc_avm & PPB_ECP) {
1117                                 /* SMC like chipset found */
1118                                 ppc->ppc_model = SMC_LIKE;
1119                                 ppc->ppc_type = PPC_TYPE_SMCLIKE;
1120
1121                                 if (bootverbose)
1122                                         printf(" ECP+EPP");
1123                         } else {
1124                                 if (bootverbose)
1125                                         printf(" EPP");
1126                         }
1127                 } else {
1128                         /* restore to standard mode */
1129                         w_ecr(ppc, PPC_ECR_STD);
1130                 }
1131
1132                 /* XXX try to detect NIBBLE and PS2 modes */
1133                 ppc->ppc_avm |= PPB_NIBBLE;
1134
1135                 if (bootverbose)
1136                         printf(" SPP");
1137
1138         } else {
1139                 ppc->ppc_avm = chipset_mode;
1140         }
1141
1142         if (bootverbose)
1143                 printf("\n");
1144
1145         switch (ppc->ppc_type) {
1146         case PPC_TYPE_SMCLIKE:
1147                 ppc_smclike_setmode(ppc, chipset_mode);
1148                 break;
1149         default:
1150                 ppc_generic_setmode(ppc, chipset_mode);
1151                 break;
1152         }
1153
1154         return (chipset_mode);
1155 }
1156
1157 /*
1158  * ppc_detect()
1159  *
1160  * mode is the mode suggested at boot
1161  */
1162 static int
1163 ppc_detect(struct ppc_data *ppc, int chipset_mode) {
1164
1165 #ifdef PPC_PROBE_CHIPSET
1166         int i, mode;
1167
1168         /* list of supported chipsets */
1169         int (*chipset_detect[])(struct ppc_data *, int) = {
1170                 ppc_pc873xx_detect,
1171                 ppc_smc37c66xgt_detect,
1172                 ppc_w83877f_detect,
1173                 ppc_generic_detect,
1174                 NULL
1175         };
1176 #endif
1177
1178         /* if can't find the port and mode not forced return error */
1179         if (!ppc_detect_port(ppc) && chipset_mode == 0)
1180                 return (EIO);                   /* failed, port not present */
1181
1182         /* assume centronics compatible mode is supported */
1183         ppc->ppc_avm = PPB_COMPATIBLE;
1184
1185 #ifdef PPC_PROBE_CHIPSET
1186         /* we have to differenciate available chipset modes,
1187          * chipset running modes and IEEE-1284 operating modes
1188          *
1189          * after detection, the port must support running in compatible mode
1190          */
1191         if (ppc->ppc_flags & 0x40) {
1192                 if (bootverbose)
1193                         printf("ppc: chipset forced to generic\n");
1194 #endif
1195
1196                 ppc->ppc_mode = ppc_generic_detect(ppc, chipset_mode);
1197
1198 #ifdef PPC_PROBE_CHIPSET
1199         } else {
1200                 for (i=0; chipset_detect[i] != NULL; i++) {
1201                         if ((mode = chipset_detect[i](ppc, chipset_mode)) != -1) {
1202                                 ppc->ppc_mode = mode;
1203                                 break;
1204                         }
1205                 }
1206         }
1207 #endif
1208
1209         /* configure/detect ECP FIFO */
1210         if ((ppc->ppc_avm & PPB_ECP) && !(ppc->ppc_flags & 0x80))
1211                 ppc_detect_fifo(ppc);
1212
1213         return (0);
1214 }
1215
1216 /*
1217  * ppc_exec_microseq()
1218  *
1219  * Execute a microsequence.
1220  * Microsequence mechanism is supposed to handle fast I/O operations.
1221  */
1222 static int
1223 ppc_exec_microseq(device_t dev, struct ppb_microseq **p_msq)
1224 {
1225         struct ppc_data *ppc = DEVTOSOFTC(dev);
1226         struct ppb_microseq *mi;
1227         char cc, *p;
1228         int i, iter, len;
1229         int error;
1230
1231         register int reg;
1232         register char mask;
1233         register int accum = 0;
1234         register char *ptr = 0;
1235
1236         struct ppb_microseq *stack = 0;
1237
1238 /* microsequence registers are equivalent to PC-like port registers */
1239 #define r_reg(register,ppc) (inb((ppc)->ppc_base + register))
1240 #define w_reg(register,ppc,byte) outb((ppc)->ppc_base + register, byte)
1241
1242 #define INCR_PC (mi ++)         /* increment program counter */
1243
1244         mi = *p_msq;
1245         for (;;) {
1246                 switch (mi->opcode) {                                           
1247                 case MS_OP_RSET:
1248                         cc = r_reg(mi->arg[0].i, ppc);
1249                         cc &= (char)mi->arg[2].i;       /* clear mask */
1250                         cc |= (char)mi->arg[1].i;       /* assert mask */
1251                         w_reg(mi->arg[0].i, ppc, cc);
1252                         INCR_PC;
1253                         break;
1254
1255                 case MS_OP_RASSERT_P:
1256                         reg = mi->arg[1].i;
1257                         ptr = ppc->ppc_ptr;
1258
1259                         if ((len = mi->arg[0].i) == MS_ACCUM) {
1260                                 accum = ppc->ppc_accum;
1261                                 for (; accum; accum--)
1262                                         w_reg(reg, ppc, *ptr++);
1263                                 ppc->ppc_accum = accum;
1264                         } else
1265                                 for (i=0; i<len; i++)
1266                                         w_reg(reg, ppc, *ptr++);
1267                         ppc->ppc_ptr = ptr;
1268
1269                         INCR_PC;
1270                         break;
1271
1272                 case MS_OP_RFETCH_P:
1273                         reg = mi->arg[1].i;
1274                         mask = (char)mi->arg[2].i;
1275                         ptr = ppc->ppc_ptr;
1276
1277                         if ((len = mi->arg[0].i) == MS_ACCUM) {
1278                                 accum = ppc->ppc_accum;
1279                                 for (; accum; accum--)
1280                                         *ptr++ = r_reg(reg, ppc) & mask;
1281                                 ppc->ppc_accum = accum;
1282                         } else
1283                                 for (i=0; i<len; i++)
1284                                         *ptr++ = r_reg(reg, ppc) & mask;
1285                         ppc->ppc_ptr = ptr;
1286
1287                         INCR_PC;
1288                         break;                                        
1289
1290                 case MS_OP_RFETCH:
1291                         *((char *) mi->arg[2].p) = r_reg(mi->arg[0].i, ppc) &
1292                                                         (char)mi->arg[1].i;
1293                         INCR_PC;
1294                         break;                                        
1295
1296                 case MS_OP_RASSERT:
1297                 case MS_OP_DELAY:
1298                 
1299                 /* let's suppose the next instr. is the same */
1300                 prefetch:
1301                         for (;mi->opcode == MS_OP_RASSERT; INCR_PC)
1302                                 w_reg(mi->arg[0].i, ppc, (char)mi->arg[1].i);
1303
1304                         if (mi->opcode == MS_OP_DELAY) {
1305                                 DELAY(mi->arg[0].i);
1306                                 INCR_PC;
1307                                 goto prefetch;
1308                         }
1309                         break;
1310
1311                 case MS_OP_ADELAY:
1312                         if (mi->arg[0].i)
1313                                 tsleep(NULL, PPBPRI, "ppbdelay",
1314                                                 mi->arg[0].i * (hz/1000));
1315                         INCR_PC;
1316                         break;
1317
1318                 case MS_OP_TRIG:
1319                         reg = mi->arg[0].i;
1320                         iter = mi->arg[1].i;
1321                         p = (char *)mi->arg[2].p;
1322
1323                         /* XXX delay limited to 255 us */
1324                         for (i=0; i<iter; i++) {
1325                                 w_reg(reg, ppc, *p++);
1326                                 DELAY((unsigned char)*p++);
1327                         }
1328                         INCR_PC;
1329                         break;
1330
1331                 case MS_OP_SET:
1332                         ppc->ppc_accum = mi->arg[0].i;
1333                         INCR_PC;
1334                         break;                                         
1335
1336                 case MS_OP_DBRA:
1337                         if (--ppc->ppc_accum > 0)
1338                                 mi += mi->arg[0].i;
1339                         INCR_PC;
1340                         break;                                        
1341
1342                 case MS_OP_BRSET:
1343                         cc = r_str(ppc);
1344                         if ((cc & (char)mi->arg[0].i) == (char)mi->arg[0].i) 
1345                                 mi += mi->arg[1].i;                      
1346                         INCR_PC;
1347                         break;
1348
1349                 case MS_OP_BRCLEAR:
1350                         cc = r_str(ppc);
1351                         if ((cc & (char)mi->arg[0].i) == 0)    
1352                                 mi += mi->arg[1].i;                             
1353                         INCR_PC;
1354                         break;                                
1355
1356                 case MS_OP_BRSTAT:
1357                         cc = r_str(ppc);
1358                         if ((cc & ((char)mi->arg[0].i | (char)mi->arg[1].i)) ==
1359                                                         (char)mi->arg[0].i)
1360                                 mi += mi->arg[2].i;
1361                         INCR_PC;
1362                         break;
1363
1364                 case MS_OP_C_CALL:
1365                         /*
1366                          * If the C call returns !0 then end the microseq.
1367                          * The current state of ptr is passed to the C function
1368                          */
1369                         if ((error = mi->arg[0].f(mi->arg[1].p, ppc->ppc_ptr)))
1370                                 return (error);
1371
1372                         INCR_PC;
1373                         break;
1374
1375                 case MS_OP_PTR:
1376                         ppc->ppc_ptr = (char *)mi->arg[0].p;
1377                         INCR_PC;
1378                         break;
1379
1380                 case MS_OP_CALL:
1381                         if (stack)
1382                                 panic("%s: too much calls", __FUNCTION__);
1383
1384                         if (mi->arg[0].p) {
1385                                 /* store the state of the actual
1386                                  * microsequence
1387                                  */
1388                                 stack = mi;
1389
1390                                 /* jump to the new microsequence */
1391                                 mi = (struct ppb_microseq *)mi->arg[0].p;
1392                         } else
1393                                 INCR_PC;
1394
1395                         break;
1396
1397                 case MS_OP_SUBRET:
1398                         /* retrieve microseq and pc state before the call */
1399                         mi = stack;
1400
1401                         /* reset the stack */
1402                         stack = 0;
1403
1404                         /* XXX return code */
1405
1406                         INCR_PC;
1407                         break;
1408
1409                 case MS_OP_PUT:
1410                 case MS_OP_GET:
1411                 case MS_OP_RET:
1412                         /* can't return to ppb level during the execution
1413                          * of a submicrosequence */
1414                         if (stack)
1415                                 panic("%s: can't return to ppb level",
1416                                                                 __FUNCTION__);
1417
1418                         /* update pc for ppb level of execution */
1419                         *p_msq = mi;
1420
1421                         /* return to ppb level of execution */
1422                         return (0);
1423
1424                 default:                         
1425                         panic("%s: unknown microsequence opcode 0x%x",
1426                                 __FUNCTION__, mi->opcode);        
1427                 }
1428         }
1429
1430         /* unreached */
1431 }
1432
1433 static void
1434 ppcintr(void *arg)
1435 {
1436         device_t dev = (device_t)arg;
1437         struct ppc_data *ppc = (struct ppc_data *)device_get_softc(dev);
1438         u_char ctr, ecr, str;
1439
1440         str = r_str(ppc);
1441         ctr = r_ctr(ppc);
1442         ecr = r_ecr(ppc);
1443
1444 #if PPC_DEBUG > 1
1445                 printf("![%x/%x/%x]", ctr, ecr, str);
1446 #endif
1447
1448         /* don't use ecp mode with IRQENABLE set */
1449         if (ctr & IRQENABLE) {
1450                 return;
1451         }
1452
1453         /* interrupts are generated by nFault signal
1454          * only in ECP mode */
1455         if ((str & nFAULT) && (ppc->ppc_mode & PPB_ECP)) {
1456                 /* check if ppc driver has programmed the
1457                  * nFault interrupt */
1458                 if  (ppc->ppc_irqstat & PPC_IRQ_nFAULT) {
1459
1460                         w_ecr(ppc, ecr | PPC_nFAULT_INTR);
1461                         ppc->ppc_irqstat &= ~PPC_IRQ_nFAULT;
1462                 } else {
1463                         /* shall be handled by underlying layers XXX */
1464                         return;
1465                 }
1466         }
1467
1468         if (ppc->ppc_irqstat & PPC_IRQ_DMA) {
1469                 /* disable interrupts (should be done by hardware though) */
1470                 w_ecr(ppc, ecr | PPC_SERVICE_INTR);
1471                 ppc->ppc_irqstat &= ~PPC_IRQ_DMA;
1472                 ecr = r_ecr(ppc);
1473
1474                 /* check if DMA completed */
1475                 if ((ppc->ppc_avm & PPB_ECP) && (ecr & PPC_ENABLE_DMA)) {
1476 #ifdef PPC_DEBUG
1477                         printf("a");
1478 #endif
1479                         /* stop DMA */
1480                         w_ecr(ppc, ecr & ~PPC_ENABLE_DMA);
1481                         ecr = r_ecr(ppc);
1482
1483                         if (ppc->ppc_dmastat == PPC_DMA_STARTED) {
1484 #ifdef PPC_DEBUG
1485                                 printf("d");
1486 #endif
1487                                 isa_dmadone(
1488                                         ppc->ppc_dmaflags,
1489                                         ppc->ppc_dmaddr,
1490                                         ppc->ppc_dmacnt,
1491                                         ppc->ppc_dmachan);
1492
1493                                 ppc->ppc_dmastat = PPC_DMA_COMPLETE;
1494
1495                                 /* wakeup the waiting process */
1496                                 wakeup((caddr_t)ppc);
1497                         }
1498                 }
1499         } else if (ppc->ppc_irqstat & PPC_IRQ_FIFO) {
1500
1501                 /* classic interrupt I/O */
1502                 ppc->ppc_irqstat &= ~PPC_IRQ_FIFO;
1503         }
1504
1505         return;
1506 }
1507
1508 static int
1509 ppc_read(device_t dev, char *buf, int len, int mode)
1510 {
1511         return (EINVAL);
1512 }
1513
1514 /*
1515  * Call this function if you want to send data in any advanced mode
1516  * of your parallel port: FIFO, DMA
1517  *
1518  * If what you want is not possible (no ECP, no DMA...),
1519  * EINVAL is returned
1520  */
1521 static int
1522 ppc_write(device_t dev, char *buf, int len, int how)
1523 {
1524         struct ppc_data *ppc = DEVTOSOFTC(dev);
1525         char ecr, ecr_sav, ctr, ctr_sav;
1526         int s, error = 0;
1527         int spin;
1528
1529 #ifdef PPC_DEBUG
1530         printf("w");
1531 #endif
1532
1533         ecr_sav = r_ecr(ppc);
1534         ctr_sav = r_ctr(ppc);
1535
1536         /*
1537          * Send buffer with DMA, FIFO and interrupts
1538          */
1539         if ((ppc->ppc_avm & PPB_ECP) && (ppc->ppc_registered)) {
1540
1541             if (ppc->ppc_dmachan >= 0) {
1542
1543                 /* byte mode, no intr, no DMA, dir=0, flush fifo
1544                  */
1545                 ecr = PPC_ECR_STD | PPC_DISABLE_INTR;
1546                 w_ecr(ppc, ecr);
1547
1548                 /* disable nAck interrupts */
1549                 ctr = r_ctr(ppc);
1550                 ctr &= ~IRQENABLE;
1551                 w_ctr(ppc, ctr);
1552
1553                 ppc->ppc_dmaflags = 0;
1554                 ppc->ppc_dmaddr = (caddr_t)buf;
1555                 ppc->ppc_dmacnt = (u_int)len;
1556
1557                 switch (ppc->ppc_mode) {
1558                 case PPB_COMPATIBLE:
1559                         /* compatible mode with FIFO, no intr, DMA, dir=0 */
1560                         ecr = PPC_ECR_FIFO | PPC_DISABLE_INTR | PPC_ENABLE_DMA;
1561                         break;
1562                 case PPB_ECP:
1563                         ecr = PPC_ECR_ECP | PPC_DISABLE_INTR | PPC_ENABLE_DMA;
1564                         break;
1565                 default:
1566                         error = EINVAL;
1567                         goto error;
1568                 }
1569
1570                 w_ecr(ppc, ecr);
1571                 ecr = r_ecr(ppc);
1572
1573                 /* enter splhigh() not to be preempted
1574                  * by the dma interrupt, we may miss
1575                  * the wakeup otherwise
1576                  */
1577                 s = splhigh();
1578
1579                 ppc->ppc_dmastat = PPC_DMA_INIT;
1580
1581                 /* enable interrupts */
1582                 ecr &= ~PPC_SERVICE_INTR;
1583                 ppc->ppc_irqstat = PPC_IRQ_DMA;
1584                 w_ecr(ppc, ecr);
1585
1586                 isa_dmastart(
1587                         ppc->ppc_dmaflags,
1588                         ppc->ppc_dmaddr,
1589                         ppc->ppc_dmacnt,
1590                         ppc->ppc_dmachan);
1591 #ifdef PPC_DEBUG
1592                 printf("s%d", ppc->ppc_dmacnt);
1593 #endif
1594                 ppc->ppc_dmastat = PPC_DMA_STARTED;
1595
1596                 /* Wait for the DMA completed interrupt. We hope we won't
1597                  * miss it, otherwise a signal will be necessary to unlock the
1598                  * process.
1599                  */
1600                 do {
1601                         /* release CPU */
1602                         error = tsleep((caddr_t)ppc,
1603                                 PPBPRI | PCATCH, "ppcdma", 0);
1604
1605                 } while (error == EWOULDBLOCK);
1606
1607                 splx(s);
1608
1609                 if (error) {
1610 #ifdef PPC_DEBUG
1611                         printf("i");
1612 #endif
1613                         /* stop DMA */
1614                         isa_dmadone(
1615                                 ppc->ppc_dmaflags, ppc->ppc_dmaddr,
1616                                 ppc->ppc_dmacnt, ppc->ppc_dmachan);
1617
1618                         /* no dma, no interrupt, flush the fifo */
1619                         w_ecr(ppc, PPC_ECR_RESET);
1620
1621                         ppc->ppc_dmastat = PPC_DMA_INTERRUPTED;
1622                         goto error;
1623                 }
1624
1625                 /* wait for an empty fifo */
1626                 while (!(r_ecr(ppc) & PPC_FIFO_EMPTY)) {
1627
1628                         for (spin=100; spin; spin--)
1629                                 if (r_ecr(ppc) & PPC_FIFO_EMPTY)
1630                                         goto fifo_empty;
1631 #ifdef PPC_DEBUG
1632                         printf("Z");
1633 #endif
1634                         error = tsleep((caddr_t)ppc, PPBPRI | PCATCH, "ppcfifo", hz/100);
1635                         if (error != EWOULDBLOCK) {
1636 #ifdef PPC_DEBUG
1637                                 printf("I");
1638 #endif
1639                                 /* no dma, no interrupt, flush the fifo */
1640                                 w_ecr(ppc, PPC_ECR_RESET);
1641
1642                                 ppc->ppc_dmastat = PPC_DMA_INTERRUPTED;
1643                                 error = EINTR;
1644                                 goto error;
1645                         }
1646                 }
1647
1648 fifo_empty:
1649                 /* no dma, no interrupt, flush the fifo */
1650                 w_ecr(ppc, PPC_ECR_RESET);
1651
1652             } else
1653                 error = EINVAL;                 /* XXX we should FIFO and
1654                                                  * interrupts */
1655         } else
1656                 error = EINVAL;
1657
1658 error:
1659
1660         /* PDRQ must be kept unasserted until nPDACK is
1661          * deasserted for a minimum of 350ns (SMC datasheet)
1662          *
1663          * Consequence may be a FIFO that never empty
1664          */
1665         DELAY(1);
1666
1667         w_ecr(ppc, ecr_sav);
1668         w_ctr(ppc, ctr_sav);
1669
1670         return (error);
1671 }
1672
1673 static void
1674 ppc_reset_epp(device_t dev)
1675 {
1676         struct ppc_data *ppc = DEVTOSOFTC(dev);
1677         
1678         ppc_reset_epp_timeout(ppc);
1679
1680         return;
1681 }
1682
1683 static int
1684 ppc_setmode(device_t dev, int mode)
1685 {
1686         struct ppc_data *ppc = DEVTOSOFTC(dev);
1687
1688         switch (ppc->ppc_type) {
1689         case PPC_TYPE_SMCLIKE:
1690                 return (ppc_smclike_setmode(ppc, mode));
1691                 break;
1692
1693         case PPC_TYPE_GENERIC:
1694         default:
1695                 return (ppc_generic_setmode(ppc, mode));
1696                 break;
1697         }
1698
1699         /* not reached */
1700         return (ENXIO);
1701 }
1702
1703 static int
1704 ppc_probe(device_t dev)
1705 {
1706 #ifdef __i386__
1707         static short next_bios_ppc = 0;
1708 #endif
1709         struct ppc_data *ppc;
1710         device_t parent;
1711         int error;
1712         u_long port;
1713 #ifdef PC98
1714 #define PC98_IEEE_1284_DISABLE 0x100
1715 #define PC98_IEEE_1284_PORT    0x140
1716
1717         unsigned int pc98_ieee_mode = 0x00;
1718         unsigned int tmp;
1719 #endif
1720
1721         /* If we are a PNP device, abort.  Otherwise we attach to *everthing* */
1722         if (isa_get_logicalid(dev))
1723                 return ENXIO;
1724
1725         parent = device_get_parent(dev);
1726
1727         /* XXX shall be set after detection */
1728         device_set_desc(dev, "Parallel port");
1729
1730         /*
1731          * Allocate the ppc_data structure.
1732          */
1733         ppc = DEVTOSOFTC(dev);
1734         bzero(ppc, sizeof(struct ppc_data));
1735
1736         ppc->rid_irq = ppc->rid_drq = ppc->rid_ioport = 0;
1737         ppc->res_irq = ppc->res_drq = ppc->res_ioport = 0;
1738
1739         /* retrieve ISA parameters */
1740         error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &port, NULL);
1741
1742 #ifdef __i386__
1743         /*
1744          * If port not specified, use bios list.
1745          */
1746         if (error) {
1747 #ifndef PC98
1748                 if((next_bios_ppc < BIOS_MAX_PPC) &&
1749                                 (*(BIOS_PORTS+next_bios_ppc) != 0) ) {
1750                         port = *(BIOS_PORTS+next_bios_ppc++);
1751                         if (bootverbose)
1752                           device_printf(dev, "parallel port found at 0x%x\n",
1753                                         (int) port);
1754                 } else {
1755                         device_printf(dev, "parallel port not found.\n");
1756                         return ENXIO;
1757                 }
1758 #else
1759                 if (next_bios_ppc == 0) {
1760                         /* Use default IEEE-1284 port of NEC PC-98x1 */
1761                         port = PC98_IEEE_1284_PORT;
1762                         next_bios_ppc += 1;
1763                         if (bootverbose)
1764                                 device_printf(dev, "parallel port found at 0x%x\n",
1765                                     (int) port);
1766                 }
1767 #endif
1768                 bus_set_resource(dev, SYS_RES_IOPORT, 0, port, IO_LPTSIZE);
1769         }
1770 #endif
1771 #ifdef __alpha__
1772         /*
1773          * There isn't a bios list on alpha. Put it in the usual place.
1774          */
1775         if (error) {
1776                 bus_set_resource(dev, SYS_RES_IOPORT, 0, 0x3bc, IO_LPTSIZE);
1777         }
1778 #endif
1779
1780         /* IO port is mandatory */
1781         ppc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
1782                                              &ppc->rid_ioport, 0, ~0,
1783                                              IO_LPTSIZE, RF_ACTIVE);
1784         if (ppc->res_ioport == 0) {
1785                 device_printf(dev, "cannot reserve I/O port range\n");
1786                 goto error;
1787         }
1788         ppc->ppc_base = rman_get_start(ppc->res_ioport);
1789
1790         ppc->ppc_flags = device_get_flags(dev);
1791
1792         if (!(ppc->ppc_flags & 0x20)) {
1793                 ppc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &ppc->rid_irq,
1794                                                   0ul, ~0ul, 1, RF_SHAREABLE);
1795                 ppc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ, &ppc->rid_drq,
1796                                                   0ul, ~0ul, 1, RF_ACTIVE);
1797         }
1798
1799         if (ppc->res_irq)
1800                 ppc->ppc_irq = rman_get_start(ppc->res_irq);
1801         if (ppc->res_drq)
1802                 ppc->ppc_dmachan = rman_get_start(ppc->res_drq);
1803
1804         ppc->ppc_unit = device_get_unit(dev);
1805         ppc->ppc_model = GENERIC;
1806
1807         ppc->ppc_mode = PPB_COMPATIBLE;
1808         ppc->ppc_epp = (ppc->ppc_flags & 0x10) >> 4;
1809
1810         ppc->ppc_type = PPC_TYPE_GENERIC;
1811
1812 #ifdef PC98
1813         /*
1814          * IEEE STD 1284 Function Check and Enable
1815          * for default IEEE-1284 port of NEC PC-98x1
1816          */
1817         if ((ppc->ppc_base == PC98_IEEE_1284_PORT) &&
1818                         !(ppc->ppc_flags & PC98_IEEE_1284_DISABLE)) {
1819                 tmp = inb(ppc->ppc_base + PPC_1284_ENABLE);
1820                 pc98_ieee_mode = tmp;
1821                 if ((tmp & 0x10) == 0x10) {
1822                         outb(ppc->ppc_base + PPC_1284_ENABLE, tmp & ~0x10);
1823                         tmp = inb(ppc->ppc_base + PPC_1284_ENABLE);
1824                         if ((tmp & 0x10) == 0x10)
1825                                 goto error;
1826                 } else {
1827                         outb(ppc->ppc_base + PPC_1284_ENABLE, tmp |  0x10);
1828                         tmp = inb(ppc->ppc_base + PPC_1284_ENABLE);
1829                         if ((tmp & 0x10) != 0x10)
1830                                 goto error;
1831                 }
1832                 outb(ppc->ppc_base + PPC_1284_ENABLE, pc98_ieee_mode | 0x10);
1833         }
1834 #endif
1835
1836         /*
1837          * Try to detect the chipset and its mode.
1838          */
1839         if (ppc_detect(ppc, ppc->ppc_flags & 0xf))
1840                 goto error;
1841
1842         return (0);
1843
1844 error:
1845 #ifdef PC98
1846         if ((ppc->ppc_base == PC98_IEEE_1284_PORT) &&
1847                         !(ppc->ppc_flags & PC98_IEEE_1284_DISABLE)) {
1848                 outb(ppc->ppc_base + PPC_1284_ENABLE, pc98_ieee_mode);
1849         }
1850 #endif
1851         if (ppc->res_irq != 0) {
1852                 bus_release_resource(dev, SYS_RES_IRQ, ppc->rid_irq,
1853                                      ppc->res_irq);
1854         }
1855         if (ppc->res_ioport != 0) {
1856                 bus_deactivate_resource(dev, SYS_RES_IOPORT, ppc->rid_ioport,
1857                                         ppc->res_ioport);
1858                 bus_release_resource(dev, SYS_RES_IOPORT, ppc->rid_ioport,
1859                                      ppc->res_ioport);
1860         }
1861         if (ppc->res_drq != 0) {
1862                 bus_deactivate_resource(dev, SYS_RES_DRQ, ppc->rid_drq,
1863                                         ppc->res_drq);
1864                 bus_release_resource(dev, SYS_RES_DRQ, ppc->rid_drq,
1865                                      ppc->res_drq);
1866         }
1867         return (ENXIO);
1868 }
1869
1870 static int
1871 ppc_attach(device_t dev)
1872 {
1873         struct ppc_data *ppc = DEVTOSOFTC(dev);
1874
1875         device_t ppbus;
1876         device_t parent = device_get_parent(dev);
1877
1878         device_printf(dev, "%s chipset (%s) in %s mode%s\n",
1879                       ppc_models[ppc->ppc_model], ppc_avms[ppc->ppc_avm],
1880                       ppc_modes[ppc->ppc_mode], (PPB_IS_EPP(ppc->ppc_mode)) ?
1881                       ppc_epp_protocol[ppc->ppc_epp] : "");
1882         
1883         if (ppc->ppc_fifo)
1884                 device_printf(dev, "FIFO with %d/%d/%d bytes threshold\n",
1885                               ppc->ppc_fifo, ppc->ppc_wthr, ppc->ppc_rthr);
1886
1887         if ((ppc->ppc_avm & PPB_ECP) && (ppc->ppc_dmachan > 0)) {
1888                 /* acquire the DMA channel forever */   /* XXX */
1889                 isa_dma_acquire(ppc->ppc_dmachan);
1890                 isa_dmainit(ppc->ppc_dmachan, 1024); /* nlpt.BUFSIZE */
1891         }
1892
1893         /* add ppbus as a child of this isa to parallel bridge */
1894         ppbus = device_add_child(dev, "ppbus", -1);
1895
1896         /*
1897          * Probe the ppbus and attach devices found.
1898          */
1899         device_probe_and_attach(ppbus);
1900
1901         /* register the ppc interrupt handler as default */
1902         if (ppc->res_irq) {
1903                 /* default to the tty mask for registration */  /* XXX */
1904                 if (BUS_SETUP_INTR(parent, dev, ppc->res_irq, INTR_TYPE_TTY,
1905                                             ppcintr, dev, &ppc->intr_cookie) == 0) {
1906
1907                         /* remember the ppcintr is registered */
1908                         ppc->ppc_registered = 1;
1909                 }
1910         }
1911
1912         return (0);
1913 }
1914
1915 static u_char
1916 ppc_io(device_t ppcdev, int iop, u_char *addr, int cnt, u_char byte)
1917 {
1918         struct ppc_data *ppc = DEVTOSOFTC(ppcdev);
1919         switch (iop) {
1920         case PPB_OUTSB_EPP:
1921                 outsb(ppc->ppc_base + PPC_EPP_DATA, addr, cnt);
1922                 break;
1923         case PPB_OUTSW_EPP:
1924                 outsw(ppc->ppc_base + PPC_EPP_DATA, addr, cnt);
1925                 break;
1926         case PPB_OUTSL_EPP:
1927                 outsl(ppc->ppc_base + PPC_EPP_DATA, addr, cnt);
1928                 break;
1929         case PPB_INSB_EPP:
1930                 insb(ppc->ppc_base + PPC_EPP_DATA, addr, cnt);
1931                 break;
1932         case PPB_INSW_EPP:
1933                 insw(ppc->ppc_base + PPC_EPP_DATA, addr, cnt);
1934                 break;
1935         case PPB_INSL_EPP:
1936                 insl(ppc->ppc_base + PPC_EPP_DATA, addr, cnt);
1937                 break;
1938         case PPB_RDTR:
1939                 return (r_dtr(ppc));
1940                 break;
1941         case PPB_RSTR:
1942                 return (r_str(ppc));
1943                 break;
1944         case PPB_RCTR:
1945                 return (r_ctr(ppc));
1946                 break;
1947         case PPB_REPP_A:
1948                 return (r_epp_A(ppc));
1949                 break;
1950         case PPB_REPP_D:
1951                 return (r_epp_D(ppc));
1952                 break;
1953         case PPB_RECR:
1954                 return (r_ecr(ppc));
1955                 break;
1956         case PPB_RFIFO:
1957                 return (r_fifo(ppc));
1958                 break;
1959         case PPB_WDTR:
1960                 w_dtr(ppc, byte);
1961                 break;
1962         case PPB_WSTR:
1963                 w_str(ppc, byte);
1964                 break;
1965         case PPB_WCTR:
1966                 w_ctr(ppc, byte);
1967                 break;
1968         case PPB_WEPP_A:
1969                 w_epp_A(ppc, byte);
1970                 break;
1971         case PPB_WEPP_D:
1972                 w_epp_D(ppc, byte);
1973                 break;
1974         case PPB_WECR:
1975                 w_ecr(ppc, byte);
1976                 break;
1977         case PPB_WFIFO:
1978                 w_fifo(ppc, byte);
1979                 break;
1980         default:
1981                 panic("%s: unknown I/O operation", __FUNCTION__);
1982                 break;
1983         }
1984
1985         return (0);     /* not significative */
1986 }
1987
1988 static int
1989 ppc_read_ivar(device_t bus, device_t dev, int index, uintptr_t *val)
1990 {
1991         struct ppc_data *ppc = (struct ppc_data *)device_get_softc(bus);
1992
1993         switch (index) {
1994         case PPC_IVAR_EPP_PROTO:
1995                 *val = (u_long)ppc->ppc_epp;
1996                 break;
1997         case PPC_IVAR_IRQ:
1998                 *val = (u_long)ppc->ppc_irq;
1999                 break;
2000         default:
2001                 return (ENOENT);
2002         }
2003
2004         return (0);
2005 }
2006
2007 /*
2008  * Resource is useless here since ppbus devices' interrupt handlers are
2009  * multiplexed to the same resource initially allocated by ppc
2010  */
2011 static int
2012 ppc_setup_intr(device_t bus, device_t child, struct resource *r, int flags,
2013                         void (*ihand)(void *), void *arg, void **cookiep)
2014 {
2015         int error;
2016         struct ppc_data *ppc = DEVTOSOFTC(bus);
2017
2018         if (ppc->ppc_registered) {
2019                 /* XXX refuse registration if DMA is in progress */
2020
2021                 /* first, unregister the default interrupt handler */
2022                 if ((error = BUS_TEARDOWN_INTR(device_get_parent(bus),
2023                                 bus, ppc->res_irq, ppc->intr_cookie)))
2024                         return (error);
2025
2026 /*              bus_deactivate_resource(bus, SYS_RES_IRQ, ppc->rid_irq, */
2027 /*                                      ppc->res_irq); */
2028
2029                 /* DMA/FIFO operation won't be possible anymore */
2030                 ppc->ppc_registered = 0;
2031         }
2032
2033         /* pass registration to the upper layer, ignore the incoming resource */
2034         return (BUS_SETUP_INTR(device_get_parent(bus), child,
2035                                r, flags, ihand, arg, cookiep));
2036 }
2037
2038 /*
2039  * When no underlying device has a registered interrupt, register the ppc
2040  * layer one
2041  */
2042 static int
2043 ppc_teardown_intr(device_t bus, device_t child, struct resource *r, void *ih)
2044 {
2045         int error;
2046         struct ppc_data *ppc = DEVTOSOFTC(bus);
2047         device_t parent = device_get_parent(bus);
2048
2049         /* pass unregistration to the upper layer */
2050         if ((error = BUS_TEARDOWN_INTR(parent, child, r, ih)))
2051                 return (error);
2052
2053         /* default to the tty mask for registration */          /* XXX */
2054         if (ppc->ppc_irq &&
2055                 !(error = BUS_SETUP_INTR(parent, bus, ppc->res_irq,
2056                         INTR_TYPE_TTY, ppcintr, bus, &ppc->intr_cookie))) {
2057
2058                 /* remember the ppcintr is registered */
2059                 ppc->ppc_registered = 1;
2060         }
2061
2062         return (error);
2063 }
2064
2065 DRIVER_MODULE(ppc, isa, ppc_driver, ppc_devclass, 0, 0);