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