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