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