xref: /freebsd/sys/dev/ppc/ppc.c (revision 0f210c922b80eef92f841effdded7996a621a9da)
167646539SMike Smith /*-
20f210c92SNicolas Souchu  * Copyright (c) 1997-2000 Nicolas Souchu
367646539SMike Smith  * All rights reserved.
467646539SMike Smith  *
567646539SMike Smith  * Redistribution and use in source and binary forms, with or without
667646539SMike Smith  * modification, are permitted provided that the following conditions
767646539SMike Smith  * are met:
867646539SMike Smith  * 1. Redistributions of source code must retain the above copyright
967646539SMike Smith  *    notice, this list of conditions and the following disclaimer.
1067646539SMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
1167646539SMike Smith  *    notice, this list of conditions and the following disclaimer in the
1267646539SMike Smith  *    documentation and/or other materials provided with the distribution.
1367646539SMike Smith  *
1467646539SMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1567646539SMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1667646539SMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1767646539SMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1867646539SMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1967646539SMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2067646539SMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2167646539SMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2267646539SMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2367646539SMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2467646539SMike Smith  * SUCH DAMAGE.
2567646539SMike Smith  *
26c3aac50fSPeter Wemm  * $FreeBSD$
2767646539SMike Smith  *
2867646539SMike Smith  */
2967646539SMike Smith #include "ppc.h"
3067646539SMike Smith 
3167646539SMike Smith #if NPPC > 0
3267646539SMike Smith 
330f210c92SNicolas Souchu #include "opt_ppc.h"
340f210c92SNicolas Souchu 
3567646539SMike Smith #include <sys/param.h>
3667646539SMike Smith #include <sys/systm.h>
3754ad6085SNicolas Souchu #include <sys/kernel.h>
380f210c92SNicolas Souchu #include <sys/bus.h>
390f210c92SNicolas Souchu #include <sys/malloc.h>
4067646539SMike Smith 
4167646539SMike Smith #include <vm/vm.h>
4267646539SMike Smith #include <vm/pmap.h>
430f210c92SNicolas Souchu #include <machine/clock.h>
440f210c92SNicolas Souchu #include <machine/bus.h>
450f210c92SNicolas Souchu #include <machine/resource.h>
460f210c92SNicolas Souchu #include <machine/vmparam.h>
470f210c92SNicolas Souchu #include <sys/rman.h>
4867646539SMike Smith 
490f210c92SNicolas Souchu #include <isa/isareg.h>
500f210c92SNicolas Souchu #include <isa/isavar.h>
5167646539SMike Smith 
5267646539SMike Smith #include <dev/ppbus/ppbconf.h>
5346f3ff79SMike Smith #include <dev/ppbus/ppb_msq.h>
5446f3ff79SMike Smith 
5567646539SMike Smith #include <i386/isa/ppcreg.h>
5667646539SMike Smith 
570f210c92SNicolas Souchu #include "ppbus_if.h"
58bc35c174SNicolas Souchu 
59bc35c174SNicolas Souchu #define LOG_PPC(function, ppc, string) \
60bc35c174SNicolas Souchu 		if (bootverbose) printf("%s: %s\n", function, string)
61bc35c174SNicolas Souchu 
6267646539SMike Smith 
630f210c92SNicolas Souchu #define DEVTOSOFTC(dev) ((struct ppc_data *)device_get_softc(dev))
640f210c92SNicolas Souchu 
650f210c92SNicolas Souchu devclass_t ppc_devclass;
660f210c92SNicolas Souchu 
670f210c92SNicolas Souchu static int ppc_probe(device_t dev);
680f210c92SNicolas Souchu static int ppc_attach(device_t dev);
690f210c92SNicolas Souchu static int ppc_read_ivar(device_t bus, device_t dev, int index, uintptr_t *val);
700f210c92SNicolas Souchu 
710f210c92SNicolas Souchu static void ppc_reset_epp(device_t);
720f210c92SNicolas Souchu static void ppc_ecp_sync(device_t);
730f210c92SNicolas Souchu static void ppcintr(void *arg);
740f210c92SNicolas Souchu 
750f210c92SNicolas Souchu static int ppc_exec_microseq(device_t, struct ppb_microseq **);
760f210c92SNicolas Souchu static int ppc_setmode(device_t, int);
770f210c92SNicolas Souchu 
780f210c92SNicolas Souchu static int ppc_read(device_t, char *, int, int);
790f210c92SNicolas Souchu static int ppc_write(device_t, char *, int, int);
800f210c92SNicolas Souchu 
810f210c92SNicolas Souchu static u_char ppc_io(device_t, int, u_char *, int, u_char);
820f210c92SNicolas Souchu 
830f210c92SNicolas Souchu static int ppc_setup_intr(device_t, device_t, struct resource *, int,
840f210c92SNicolas Souchu 		void (*)(void *), void *, void **);
850f210c92SNicolas Souchu static int ppc_teardown_intr(device_t, device_t, struct resource *, void *);
860f210c92SNicolas Souchu 
870f210c92SNicolas Souchu static device_method_t ppc_methods[] = {
880f210c92SNicolas Souchu 	/* device interface */
890f210c92SNicolas Souchu 	DEVMETHOD(device_probe,         ppc_probe),
900f210c92SNicolas Souchu 	DEVMETHOD(device_attach,        ppc_attach),
910f210c92SNicolas Souchu 
920f210c92SNicolas Souchu 	/* bus interface */
930f210c92SNicolas Souchu 	DEVMETHOD(bus_read_ivar,	ppc_read_ivar),
940f210c92SNicolas Souchu 	DEVMETHOD(bus_setup_intr,	ppc_setup_intr),
950f210c92SNicolas Souchu 	DEVMETHOD(bus_teardown_intr,	ppc_teardown_intr),
960f210c92SNicolas Souchu 	DEVMETHOD(bus_alloc_resource,   bus_generic_alloc_resource),
970f210c92SNicolas Souchu 
980f210c92SNicolas Souchu 	/* ppbus interface */
990f210c92SNicolas Souchu 	DEVMETHOD(ppbus_io,		ppc_io),
1000f210c92SNicolas Souchu 	DEVMETHOD(ppbus_exec_microseq,	ppc_exec_microseq),
1010f210c92SNicolas Souchu 	DEVMETHOD(ppbus_reset_epp,	ppc_reset_epp),
1020f210c92SNicolas Souchu 	DEVMETHOD(ppbus_setmode,	ppc_setmode),
1030f210c92SNicolas Souchu 	DEVMETHOD(ppbus_ecp_sync,	ppc_ecp_sync),
1040f210c92SNicolas Souchu 	DEVMETHOD(ppbus_read,		ppc_read),
1050f210c92SNicolas Souchu 	DEVMETHOD(ppbus_write,		ppc_write),
1060f210c92SNicolas Souchu 
1070f210c92SNicolas Souchu         { 0, 0 }
10867646539SMike Smith   };
10967646539SMike Smith 
1100f210c92SNicolas Souchu static driver_t ppc_driver = {
1110f210c92SNicolas Souchu 	"ppc",
1120f210c92SNicolas Souchu 	ppc_methods,
1130f210c92SNicolas Souchu 	sizeof(struct ppc_data),
1140f210c92SNicolas Souchu };
11567646539SMike Smith 
1160f210c92SNicolas Souchu static char *ppc_models[] = {
11746f3ff79SMike Smith 	"SMC-like", "SMC FDC37C665GT", "SMC FDC37C666GT", "PC87332", "PC87306",
118af548787SNicolas Souchu 	"82091AA", "Generic", "W83877F", "W83877AF", "Winbond", "PC87334", 0
11967646539SMike Smith };
12067646539SMike Smith 
12146f3ff79SMike Smith /* list of available modes */
12246f3ff79SMike Smith static char *ppc_avms[] = {
12346f3ff79SMike Smith 	"COMPATIBLE", "NIBBLE-only", "PS2-only", "PS2/NIBBLE", "EPP-only",
12446f3ff79SMike Smith 	"EPP/NIBBLE", "EPP/PS2", "EPP/PS2/NIBBLE", "ECP-only",
12546f3ff79SMike Smith 	"ECP/NIBBLE", "ECP/PS2", "ECP/PS2/NIBBLE", "ECP/EPP",
12646f3ff79SMike Smith 	"ECP/EPP/NIBBLE", "ECP/EPP/PS2", "ECP/EPP/PS2/NIBBLE", 0
12746f3ff79SMike Smith };
12846f3ff79SMike Smith 
12946f3ff79SMike Smith /* list of current executing modes
13046f3ff79SMike Smith  * Note that few modes do not actually exist.
13146f3ff79SMike Smith  */
13267646539SMike Smith static char *ppc_modes[] = {
13346f3ff79SMike Smith 	"COMPATIBLE", "NIBBLE", "PS/2", "PS/2", "EPP",
13446f3ff79SMike Smith 	"EPP", "EPP", "EPP", "ECP",
13546f3ff79SMike Smith 	"ECP", "ECP+PS2", "ECP+PS2", "ECP+EPP",
13646f3ff79SMike Smith 	"ECP+EPP", "ECP+EPP", "ECP+EPP", 0
13767646539SMike Smith };
13867646539SMike Smith 
13967646539SMike Smith static char *ppc_epp_protocol[] = { " (EPP 1.9)", " (EPP 1.7)", 0 };
14067646539SMike Smith 
14167646539SMike Smith /*
14267646539SMike Smith  * BIOS printer list - used by BIOS probe.
14367646539SMike Smith  */
14467646539SMike Smith #define	BIOS_PPC_PORTS	0x408
14567646539SMike Smith #define	BIOS_PORTS	(short *)(KERNBASE+BIOS_PPC_PORTS)
14667646539SMike Smith #define	BIOS_MAX_PPC	4
14767646539SMike Smith 
14867646539SMike Smith /*
14967646539SMike Smith  * ppc_ecp_sync()		XXX
15067646539SMike Smith  */
15167646539SMike Smith static void
1520f210c92SNicolas Souchu ppc_ecp_sync(device_t dev) {
15367646539SMike Smith 
15467646539SMike Smith 	int i, r;
1550f210c92SNicolas Souchu 	struct ppc_data *ppc = DEVTOSOFTC(dev);
15667646539SMike Smith 
157bc35c174SNicolas Souchu 	if (!(ppc->ppc_avm & PPB_ECP))
158bc35c174SNicolas Souchu 		return;
159bc35c174SNicolas Souchu 
16067646539SMike Smith 	r = r_ecr(ppc);
161bc35c174SNicolas Souchu 	if ((r & 0xe0) != PPC_ECR_EPP)
16267646539SMike Smith 		return;
16367646539SMike Smith 
16467646539SMike Smith 	for (i = 0; i < 100; i++) {
16567646539SMike Smith 		r = r_ecr(ppc);
16667646539SMike Smith 		if (r & 0x1)
16767646539SMike Smith 			return;
16867646539SMike Smith 		DELAY(100);
16967646539SMike Smith 	}
17067646539SMike Smith 
17146f3ff79SMike Smith 	printf("ppc%d: ECP sync failed as data still " \
1720f210c92SNicolas Souchu 		"present in FIFO.\n", ppc->ppc_unit);
17367646539SMike Smith 
17467646539SMike Smith 	return;
17567646539SMike Smith }
17667646539SMike Smith 
177bc35c174SNicolas Souchu /*
178bc35c174SNicolas Souchu  * ppc_detect_fifo()
179bc35c174SNicolas Souchu  *
180bc35c174SNicolas Souchu  * Detect parallel port FIFO
181bc35c174SNicolas Souchu  */
182bc35c174SNicolas Souchu static int
183bc35c174SNicolas Souchu ppc_detect_fifo(struct ppc_data *ppc)
18467646539SMike Smith {
185bc35c174SNicolas Souchu 	char ecr_sav;
186bc35c174SNicolas Souchu 	char ctr_sav, ctr, cc;
187bc35c174SNicolas Souchu 	short i;
18867646539SMike Smith 
189bc35c174SNicolas Souchu 	/* save registers */
190bc35c174SNicolas Souchu 	ecr_sav = r_ecr(ppc);
191bc35c174SNicolas Souchu 	ctr_sav = r_ctr(ppc);
192bc35c174SNicolas Souchu 
193bc35c174SNicolas Souchu 	/* enter ECP configuration mode, no interrupt, no DMA */
194bc35c174SNicolas Souchu 	w_ecr(ppc, 0xf4);
195bc35c174SNicolas Souchu 
196bc35c174SNicolas Souchu 	/* read PWord size - transfers in FIFO mode must be PWord aligned */
197bc35c174SNicolas Souchu 	ppc->ppc_pword = (r_cnfgA(ppc) & PPC_PWORD_MASK);
198bc35c174SNicolas Souchu 
199bc35c174SNicolas Souchu 	/* XXX 16 and 32 bits implementations not supported */
200bc35c174SNicolas Souchu 	if (ppc->ppc_pword != PPC_PWORD_8) {
201bc35c174SNicolas Souchu 		LOG_PPC(__FUNCTION__, ppc, "PWord not supported");
202bc35c174SNicolas Souchu 		goto error;
203bc35c174SNicolas Souchu 	}
204bc35c174SNicolas Souchu 
205bc35c174SNicolas Souchu 	w_ecr(ppc, 0x34);		/* byte mode, no interrupt, no DMA */
206bc35c174SNicolas Souchu 	ctr = r_ctr(ppc);
207bc35c174SNicolas Souchu 	w_ctr(ppc, ctr | PCD);		/* set direction to 1 */
208bc35c174SNicolas Souchu 
209bc35c174SNicolas Souchu 	/* enter ECP test mode, no interrupt, no DMA */
210bc35c174SNicolas Souchu 	w_ecr(ppc, 0xd4);
211bc35c174SNicolas Souchu 
212bc35c174SNicolas Souchu 	/* flush the FIFO */
213bc35c174SNicolas Souchu 	for (i=0; i<1024; i++) {
214bc35c174SNicolas Souchu 		if (r_ecr(ppc) & PPC_FIFO_EMPTY)
215bc35c174SNicolas Souchu 			break;
216bc35c174SNicolas Souchu 		cc = r_fifo(ppc);
217bc35c174SNicolas Souchu 	}
218bc35c174SNicolas Souchu 
219bc35c174SNicolas Souchu 	if (i >= 1024) {
220bc35c174SNicolas Souchu 		LOG_PPC(__FUNCTION__, ppc, "can't flush FIFO");
221bc35c174SNicolas Souchu 		goto error;
222bc35c174SNicolas Souchu 	}
223bc35c174SNicolas Souchu 
224bc35c174SNicolas Souchu 	/* enable interrupts, no DMA */
225bc35c174SNicolas Souchu 	w_ecr(ppc, 0xd0);
226bc35c174SNicolas Souchu 
227bc35c174SNicolas Souchu 	/* determine readIntrThreshold
228bc35c174SNicolas Souchu 	 * fill the FIFO until serviceIntr is set
229bc35c174SNicolas Souchu 	 */
230bc35c174SNicolas Souchu 	for (i=0; i<1024; i++) {
231bc35c174SNicolas Souchu 		w_fifo(ppc, (char)i);
232bc35c174SNicolas Souchu 		if (!ppc->ppc_rthr && (r_ecr(ppc) & PPC_SERVICE_INTR)) {
233bc35c174SNicolas Souchu 			/* readThreshold reached */
234bc35c174SNicolas Souchu 			ppc->ppc_rthr = i+1;
235bc35c174SNicolas Souchu 		}
236bc35c174SNicolas Souchu 		if (r_ecr(ppc) & PPC_FIFO_FULL) {
237bc35c174SNicolas Souchu 			ppc->ppc_fifo = i+1;
238bc35c174SNicolas Souchu 			break;
239bc35c174SNicolas Souchu 		}
240bc35c174SNicolas Souchu 	}
241bc35c174SNicolas Souchu 
242bc35c174SNicolas Souchu 	if (i >= 1024) {
243bc35c174SNicolas Souchu 		LOG_PPC(__FUNCTION__, ppc, "can't fill FIFO");
244bc35c174SNicolas Souchu 		goto error;
245bc35c174SNicolas Souchu 	}
246bc35c174SNicolas Souchu 
247bc35c174SNicolas Souchu 	w_ecr(ppc, 0xd4);		/* test mode, no interrupt, no DMA */
248bc35c174SNicolas Souchu 	w_ctr(ppc, ctr & ~PCD);		/* set direction to 0 */
249bc35c174SNicolas Souchu 	w_ecr(ppc, 0xd0);		/* enable interrupts */
250bc35c174SNicolas Souchu 
251bc35c174SNicolas Souchu 	/* determine writeIntrThreshold
252bc35c174SNicolas Souchu 	 * empty the FIFO until serviceIntr is set
253bc35c174SNicolas Souchu 	 */
254bc35c174SNicolas Souchu 	for (i=ppc->ppc_fifo; i>0; i--) {
255bc35c174SNicolas Souchu 		if (r_fifo(ppc) != (char)(ppc->ppc_fifo-i)) {
256bc35c174SNicolas Souchu 			LOG_PPC(__FUNCTION__, ppc, "invalid data in FIFO");
257bc35c174SNicolas Souchu 			goto error;
258bc35c174SNicolas Souchu 		}
259bc35c174SNicolas Souchu 		if (r_ecr(ppc) & PPC_SERVICE_INTR) {
260bc35c174SNicolas Souchu 			/* writeIntrThreshold reached */
261bc35c174SNicolas Souchu 			ppc->ppc_wthr = ppc->ppc_fifo - i+1;
262bc35c174SNicolas Souchu 		}
263bc35c174SNicolas Souchu 		/* if FIFO empty before the last byte, error */
264bc35c174SNicolas Souchu 		if (i>1 && (r_ecr(ppc) & PPC_FIFO_EMPTY)) {
265bc35c174SNicolas Souchu 			LOG_PPC(__FUNCTION__, ppc, "data lost in FIFO");
266bc35c174SNicolas Souchu 			goto error;
267bc35c174SNicolas Souchu 		}
268bc35c174SNicolas Souchu 	}
269bc35c174SNicolas Souchu 
270bc35c174SNicolas Souchu 	/* FIFO must be empty after the last byte */
271bc35c174SNicolas Souchu 	if (!(r_ecr(ppc) & PPC_FIFO_EMPTY)) {
272bc35c174SNicolas Souchu 		LOG_PPC(__FUNCTION__, ppc, "can't empty the FIFO");
273bc35c174SNicolas Souchu 		goto error;
274bc35c174SNicolas Souchu 	}
275bc35c174SNicolas Souchu 
276bc35c174SNicolas Souchu 	w_ctr(ppc, ctr_sav);
277bc35c174SNicolas Souchu 	w_ecr(ppc, ecr_sav);
278bc35c174SNicolas Souchu 
279bc35c174SNicolas Souchu 	return (0);
280bc35c174SNicolas Souchu 
281bc35c174SNicolas Souchu error:
282bc35c174SNicolas Souchu 	w_ctr(ppc, ctr_sav);
283bc35c174SNicolas Souchu 	w_ecr(ppc, ecr_sav);
284bc35c174SNicolas Souchu 
285bc35c174SNicolas Souchu 	return (EINVAL);
28667646539SMike Smith }
28767646539SMike Smith 
28846f3ff79SMike Smith static int
28946f3ff79SMike Smith ppc_detect_port(struct ppc_data *ppc)
29046f3ff79SMike Smith {
29146f3ff79SMike Smith 
29246f3ff79SMike Smith 	w_ctr(ppc, 0x0c);	/* To avoid missing PS2 ports */
29346f3ff79SMike Smith 	w_dtr(ppc, 0xaa);
294a7006f89SNicolas Souchu 	if (r_dtr(ppc) != 0xaa)
29546f3ff79SMike Smith 		return (0);
29646f3ff79SMike Smith 
29746f3ff79SMike Smith 	return (1);
29846f3ff79SMike Smith }
29946f3ff79SMike Smith 
30067646539SMike Smith /*
3010f210c92SNicolas Souchu  * EPP timeout, according to the PC87332 manual
3020f210c92SNicolas Souchu  * Semantics of clearing EPP timeout bit.
3030f210c92SNicolas Souchu  * PC87332	- reading SPP_STR does it...
3040f210c92SNicolas Souchu  * SMC		- write 1 to EPP timeout bit			XXX
3050f210c92SNicolas Souchu  * Others	- (?) write 0 to EPP timeout bit
3060f210c92SNicolas Souchu  */
3070f210c92SNicolas Souchu static void
3080f210c92SNicolas Souchu ppc_reset_epp_timeout(struct ppc_data *ppc)
3090f210c92SNicolas Souchu {
3100f210c92SNicolas Souchu 	register char r;
3110f210c92SNicolas Souchu 
3120f210c92SNicolas Souchu 	r = r_str(ppc);
3130f210c92SNicolas Souchu 	w_str(ppc, r | 0x1);
3140f210c92SNicolas Souchu 	w_str(ppc, r & 0xfe);
3150f210c92SNicolas Souchu 
3160f210c92SNicolas Souchu 	return;
3170f210c92SNicolas Souchu }
3180f210c92SNicolas Souchu 
3190f210c92SNicolas Souchu static int
3200f210c92SNicolas Souchu ppc_check_epp_timeout(struct ppc_data *ppc)
3210f210c92SNicolas Souchu {
3220f210c92SNicolas Souchu 	ppc_reset_epp_timeout(ppc);
3230f210c92SNicolas Souchu 
3240f210c92SNicolas Souchu 	return (!(r_str(ppc) & TIMEOUT));
3250f210c92SNicolas Souchu }
3260f210c92SNicolas Souchu 
3270f210c92SNicolas Souchu /*
3280f210c92SNicolas Souchu  * Configure current operating mode
3290f210c92SNicolas Souchu  */
3300f210c92SNicolas Souchu static int
3310f210c92SNicolas Souchu ppc_generic_setmode(struct ppc_data *ppc, int mode)
3320f210c92SNicolas Souchu {
3330f210c92SNicolas Souchu 	u_char ecr = 0;
3340f210c92SNicolas Souchu 
3350f210c92SNicolas Souchu 	/* check if mode is available */
3360f210c92SNicolas Souchu 	if (mode && !(ppc->ppc_avm & mode))
3370f210c92SNicolas Souchu 		return (EINVAL);
3380f210c92SNicolas Souchu 
3390f210c92SNicolas Souchu 	/* if ECP mode, configure ecr register */
3400f210c92SNicolas Souchu 	if (ppc->ppc_avm & PPB_ECP) {
3410f210c92SNicolas Souchu 		/* return to byte mode (keeping direction bit),
3420f210c92SNicolas Souchu 		 * no interrupt, no DMA to be able to change to
3430f210c92SNicolas Souchu 		 * ECP
3440f210c92SNicolas Souchu 		 */
3450f210c92SNicolas Souchu 		w_ecr(ppc, PPC_ECR_RESET);
3460f210c92SNicolas Souchu 		ecr = PPC_DISABLE_INTR;
3470f210c92SNicolas Souchu 
3480f210c92SNicolas Souchu 		if (mode & PPB_EPP)
3490f210c92SNicolas Souchu 			return (EINVAL);
3500f210c92SNicolas Souchu 		else if (mode & PPB_ECP)
3510f210c92SNicolas Souchu 			/* select ECP mode */
3520f210c92SNicolas Souchu 			ecr |= PPC_ECR_ECP;
3530f210c92SNicolas Souchu 		else if (mode & PPB_PS2)
3540f210c92SNicolas Souchu 			/* select PS2 mode with ECP */
3550f210c92SNicolas Souchu 			ecr |= PPC_ECR_PS2;
3560f210c92SNicolas Souchu 		else
3570f210c92SNicolas Souchu 			/* select COMPATIBLE/NIBBLE mode */
3580f210c92SNicolas Souchu 			ecr |= PPC_ECR_STD;
3590f210c92SNicolas Souchu 
3600f210c92SNicolas Souchu 		w_ecr(ppc, ecr);
3610f210c92SNicolas Souchu 	}
3620f210c92SNicolas Souchu 
3630f210c92SNicolas Souchu 	ppc->ppc_mode = mode;
3640f210c92SNicolas Souchu 
3650f210c92SNicolas Souchu 	return (0);
3660f210c92SNicolas Souchu }
3670f210c92SNicolas Souchu 
3680f210c92SNicolas Souchu /*
3690f210c92SNicolas Souchu  * The ppc driver is free to choose options like FIFO or DMA
3700f210c92SNicolas Souchu  * if ECP mode is available.
3710f210c92SNicolas Souchu  *
3720f210c92SNicolas Souchu  * The 'RAW' option allows the upper drivers to force the ppc mode
3730f210c92SNicolas Souchu  * even with FIFO, DMA available.
3740f210c92SNicolas Souchu  */
3750f210c92SNicolas Souchu static int
3760f210c92SNicolas Souchu ppc_smclike_setmode(struct ppc_data *ppc, int mode)
3770f210c92SNicolas Souchu {
3780f210c92SNicolas Souchu 	u_char ecr = 0;
3790f210c92SNicolas Souchu 
3800f210c92SNicolas Souchu 	/* check if mode is available */
3810f210c92SNicolas Souchu 	if (mode && !(ppc->ppc_avm & mode))
3820f210c92SNicolas Souchu 		return (EINVAL);
3830f210c92SNicolas Souchu 
3840f210c92SNicolas Souchu 	/* if ECP mode, configure ecr register */
3850f210c92SNicolas Souchu 	if (ppc->ppc_avm & PPB_ECP) {
3860f210c92SNicolas Souchu 		/* return to byte mode (keeping direction bit),
3870f210c92SNicolas Souchu 		 * no interrupt, no DMA to be able to change to
3880f210c92SNicolas Souchu 		 * ECP or EPP mode
3890f210c92SNicolas Souchu 		 */
3900f210c92SNicolas Souchu 		w_ecr(ppc, PPC_ECR_RESET);
3910f210c92SNicolas Souchu 		ecr = PPC_DISABLE_INTR;
3920f210c92SNicolas Souchu 
3930f210c92SNicolas Souchu 		if (mode & PPB_EPP)
3940f210c92SNicolas Souchu 			/* select EPP mode */
3950f210c92SNicolas Souchu 			ecr |= PPC_ECR_EPP;
3960f210c92SNicolas Souchu 		else if (mode & PPB_ECP)
3970f210c92SNicolas Souchu 			/* select ECP mode */
3980f210c92SNicolas Souchu 			ecr |= PPC_ECR_ECP;
3990f210c92SNicolas Souchu 		else if (mode & PPB_PS2)
4000f210c92SNicolas Souchu 			/* select PS2 mode with ECP */
4010f210c92SNicolas Souchu 			ecr |= PPC_ECR_PS2;
4020f210c92SNicolas Souchu 		else
4030f210c92SNicolas Souchu 			/* select COMPATIBLE/NIBBLE mode */
4040f210c92SNicolas Souchu 			ecr |= PPC_ECR_STD;
4050f210c92SNicolas Souchu 
4060f210c92SNicolas Souchu 		w_ecr(ppc, ecr);
4070f210c92SNicolas Souchu 	}
4080f210c92SNicolas Souchu 
4090f210c92SNicolas Souchu 	ppc->ppc_mode = mode;
4100f210c92SNicolas Souchu 
4110f210c92SNicolas Souchu 	return (0);
4120f210c92SNicolas Souchu }
4130f210c92SNicolas Souchu 
4140f210c92SNicolas Souchu #ifdef PPC_PROBE_CHIPSET
4150f210c92SNicolas Souchu /*
41667646539SMike Smith  * ppc_pc873xx_detect
41767646539SMike Smith  *
41867646539SMike Smith  * Probe for a Natsemi PC873xx-family part.
41967646539SMike Smith  *
42067646539SMike Smith  * References in this function are to the National Semiconductor
42167646539SMike Smith  * PC87332 datasheet TL/C/11930, May 1995 revision.
42267646539SMike Smith  */
42367646539SMike Smith static int pc873xx_basetab[] = {0x0398, 0x026e, 0x015c, 0x002e, 0};
42467646539SMike Smith static int pc873xx_porttab[] = {0x0378, 0x03bc, 0x0278, 0};
425af548787SNicolas Souchu static int pc873xx_irqtab[] = {5, 7, 5, 0};
426af548787SNicolas Souchu 
427af548787SNicolas Souchu static int pc873xx_regstab[] = {
428af548787SNicolas Souchu 	PC873_FER, PC873_FAR, PC873_PTR,
429af548787SNicolas Souchu 	PC873_FCR, PC873_PCR, PC873_PMC,
430af548787SNicolas Souchu 	PC873_TUP, PC873_SID, PC873_PNP0,
431af548787SNicolas Souchu 	PC873_PNP1, PC873_LPTBA, -1
432af548787SNicolas Souchu };
433af548787SNicolas Souchu 
434af548787SNicolas Souchu static char *pc873xx_rnametab[] = {
435af548787SNicolas Souchu 	"FER", "FAR", "PTR", "FCR", "PCR",
436af548787SNicolas Souchu 	"PMC", "TUP", "SID", "PNP0", "PNP1",
437af548787SNicolas Souchu 	"LPTBA", NULL
438af548787SNicolas Souchu };
43967646539SMike Smith 
44067646539SMike Smith static int
44146f3ff79SMike Smith ppc_pc873xx_detect(struct ppc_data *ppc, int chipset_mode)	/* XXX mode never forced */
44267646539SMike Smith {
44367646539SMike Smith     static int	index = 0;
444f1d19042SArchie Cobbs     int		idport, irq;
445af548787SNicolas Souchu     int		ptr, pcr, val, i;
44667646539SMike Smith 
44767646539SMike Smith     while ((idport = pc873xx_basetab[index++])) {
44867646539SMike Smith 
44967646539SMike Smith 	/* XXX should check first to see if this location is already claimed */
45067646539SMike Smith 
45167646539SMike Smith 	/*
452af548787SNicolas Souchu 	 * Pull the 873xx through the power-on ID cycle (2.2,1.).
453af548787SNicolas Souchu 	 * We can't use this to locate the chip as it may already have
454af548787SNicolas Souchu 	 * been used by the BIOS.
45567646539SMike Smith 	 */
456af548787SNicolas Souchu 	(void)inb(idport); (void)inb(idport);
457af548787SNicolas Souchu 	(void)inb(idport); (void)inb(idport);
45867646539SMike Smith 
45967646539SMike Smith 	/*
46067646539SMike Smith 	 * Read the SID byte.  Possible values are :
46167646539SMike Smith 	 *
462af548787SNicolas Souchu 	 * 01010xxx	PC87334
46367646539SMike Smith 	 * 0001xxxx	PC87332
46467646539SMike Smith 	 * 01110xxx	PC87306
46567646539SMike Smith 	 */
46667646539SMike Smith 	outb(idport, PC873_SID);
46767646539SMike Smith 	val = inb(idport + 1);
46867646539SMike Smith 	if ((val & 0xf0) == 0x10) {
4690f210c92SNicolas Souchu 	    ppc->ppc_model = NS_PC87332;
47067646539SMike Smith 	} else if ((val & 0xf8) == 0x70) {
4710f210c92SNicolas Souchu 	    ppc->ppc_model = NS_PC87306;
472af548787SNicolas Souchu 	} else if ((val & 0xf8) == 0x50) {
4730f210c92SNicolas Souchu 	    ppc->ppc_model = NS_PC87334;
47467646539SMike Smith 	} else {
47567646539SMike Smith 	    if (bootverbose && (val != 0xff))
47667646539SMike Smith 		printf("PC873xx probe at 0x%x got unknown ID 0x%x\n", idport, val);
47767646539SMike Smith 	    continue ;		/* not recognised */
47867646539SMike Smith 	}
47967646539SMike Smith 
480af548787SNicolas Souchu 	/* print registers */
481af548787SNicolas Souchu 	if (bootverbose) {
482af548787SNicolas Souchu 		printf("PC873xx");
483af548787SNicolas Souchu 		for (i=0; pc873xx_regstab[i] != -1; i++) {
484af548787SNicolas Souchu 			outb(idport, pc873xx_regstab[i]);
485af548787SNicolas Souchu 			printf(" %s=0x%x", pc873xx_rnametab[i],
486af548787SNicolas Souchu 						inb(idport + 1) & 0xff);
487af548787SNicolas Souchu 		}
488af548787SNicolas Souchu 		printf("\n");
489af548787SNicolas Souchu 	}
490af548787SNicolas Souchu 
49167646539SMike Smith 	/*
49267646539SMike Smith 	 * We think we have one.  Is it enabled and where we want it to be?
49367646539SMike Smith 	 */
49467646539SMike Smith 	outb(idport, PC873_FER);
49567646539SMike Smith 	val = inb(idport + 1);
49667646539SMike Smith 	if (!(val & PC873_PPENABLE)) {
49767646539SMike Smith 	    if (bootverbose)
49867646539SMike Smith 		printf("PC873xx parallel port disabled\n");
49967646539SMike Smith 	    continue;
50067646539SMike Smith 	}
50167646539SMike Smith 	outb(idport, PC873_FAR);
50267646539SMike Smith 	val = inb(idport + 1) & 0x3;
50367646539SMike Smith 	/* XXX we should create a driver instance for every port found */
50467646539SMike Smith 	if (pc873xx_porttab[val] != ppc->ppc_base) {
50567646539SMike Smith 	    if (bootverbose)
50667646539SMike Smith 		printf("PC873xx at 0x%x not for driver at port 0x%x\n",
50767646539SMike Smith 		       pc873xx_porttab[val], ppc->ppc_base);
50867646539SMike Smith 	    continue;
50967646539SMike Smith 	}
51067646539SMike Smith 
51167646539SMike Smith 	outb(idport, PC873_PTR);
512af548787SNicolas Souchu         ptr = inb(idport + 1);
513af548787SNicolas Souchu 
514af548787SNicolas Souchu 	/* get irq settings */
515af548787SNicolas Souchu 	if (ppc->ppc_base == 0x378)
516af548787SNicolas Souchu 		irq = (ptr & PC873_LPTBIRQ7) ? 7 : 5;
517af548787SNicolas Souchu 	else
518af548787SNicolas Souchu 		irq = pc873xx_irqtab[val];
519af548787SNicolas Souchu 
52067646539SMike Smith 	if (bootverbose)
521af548787SNicolas Souchu 		printf("PC873xx irq %d at 0x%x\n", irq, ppc->ppc_base);
52267646539SMike Smith 
523af548787SNicolas Souchu 	/*
524af548787SNicolas Souchu 	 * Check if irq settings are correct
525af548787SNicolas Souchu 	 */
526af548787SNicolas Souchu 	if (irq != ppc->ppc_irq) {
527af548787SNicolas Souchu 		/*
528af548787SNicolas Souchu 		 * If the chipset is not locked and base address is 0x378,
529af548787SNicolas Souchu 		 * we have another chance
530af548787SNicolas Souchu 		 */
531af548787SNicolas Souchu 		if (ppc->ppc_base == 0x378 && !(ptr & PC873_CFGLOCK)) {
532af548787SNicolas Souchu 			if (ppc->ppc_irq == 7) {
533af548787SNicolas Souchu 				outb(idport + 1, (ptr | PC873_LPTBIRQ7));
534af548787SNicolas Souchu 				outb(idport + 1, (ptr | PC873_LPTBIRQ7));
535af548787SNicolas Souchu 			} else {
536af548787SNicolas Souchu 				outb(idport + 1, (ptr & ~PC873_LPTBIRQ7));
537af548787SNicolas Souchu 				outb(idport + 1, (ptr & ~PC873_LPTBIRQ7));
53867646539SMike Smith 			}
539af548787SNicolas Souchu 			if (bootverbose)
540af548787SNicolas Souchu 			   printf("PC873xx irq set to %d\n", ppc->ppc_irq);
541af548787SNicolas Souchu 		} else {
542af548787SNicolas Souchu 			if (bootverbose)
543af548787SNicolas Souchu 			   printf("PC873xx sorry, can't change irq setting\n");
54467646539SMike Smith 		}
54567646539SMike Smith 	} else {
54667646539SMike Smith 		if (bootverbose)
547af548787SNicolas Souchu 			printf("PC873xx irq settings are correct\n");
54867646539SMike Smith 	}
54967646539SMike Smith 
55067646539SMike Smith 	outb(idport, PC873_PCR);
551af548787SNicolas Souchu 	pcr = inb(idport + 1);
552af548787SNicolas Souchu 
553af548787SNicolas Souchu 	if ((ptr & PC873_CFGLOCK) || !chipset_mode) {
554af548787SNicolas Souchu 	    if (bootverbose)
555af548787SNicolas Souchu 		printf("PC873xx %s", (ptr & PC873_CFGLOCK)?"locked":"unlocked");
556af548787SNicolas Souchu 
557af548787SNicolas Souchu 	    ppc->ppc_avm |= PPB_NIBBLE;
558af548787SNicolas Souchu 	    if (bootverbose)
559af548787SNicolas Souchu 		printf(", NIBBLE");
560af548787SNicolas Souchu 
561af548787SNicolas Souchu 	    if (pcr & PC873_EPPEN) {
562af548787SNicolas Souchu 	        ppc->ppc_avm |= PPB_EPP;
563af548787SNicolas Souchu 
564af548787SNicolas Souchu 		if (bootverbose)
565af548787SNicolas Souchu 			printf(", EPP");
566af548787SNicolas Souchu 
567af548787SNicolas Souchu 		if (pcr & PC873_EPP19)
568af548787SNicolas Souchu 			ppc->ppc_epp = EPP_1_9;
569af548787SNicolas Souchu 		else
570af548787SNicolas Souchu 			ppc->ppc_epp = EPP_1_7;
571af548787SNicolas Souchu 
5720f210c92SNicolas Souchu 		if ((ppc->ppc_model == NS_PC87332) && bootverbose) {
573af548787SNicolas Souchu 			outb(idport, PC873_PTR);
574af548787SNicolas Souchu 			ptr = inb(idport + 1);
575af548787SNicolas Souchu 			if (ptr & PC873_EPPRDIR)
576af548787SNicolas Souchu 				printf(", Regular mode");
577af548787SNicolas Souchu 			else
578af548787SNicolas Souchu 				printf(", Automatic mode");
579af548787SNicolas Souchu 		}
580af548787SNicolas Souchu 	    } else if (pcr & PC873_ECPEN) {
581af548787SNicolas Souchu 		ppc->ppc_avm |= PPB_ECP;
582af548787SNicolas Souchu 		if (bootverbose)
583af548787SNicolas Souchu 			printf(", ECP");
584af548787SNicolas Souchu 
585af548787SNicolas Souchu 		if (pcr & PC873_ECPCLK)	{		/* XXX */
586af548787SNicolas Souchu 			ppc->ppc_avm |= PPB_PS2;
587af548787SNicolas Souchu 			if (bootverbose)
588af548787SNicolas Souchu 				printf(", PS/2");
589af548787SNicolas Souchu 		}
590af548787SNicolas Souchu 	    } else {
591af548787SNicolas Souchu 		outb(idport, PC873_PTR);
592af548787SNicolas Souchu 		ptr = inb(idport + 1);
593af548787SNicolas Souchu 		if (ptr & PC873_EXTENDED) {
594af548787SNicolas Souchu 			ppc->ppc_avm |= PPB_SPP;
595af548787SNicolas Souchu                         if (bootverbose)
596af548787SNicolas Souchu                                 printf(", SPP");
597af548787SNicolas Souchu 		}
598af548787SNicolas Souchu 	    }
599af548787SNicolas Souchu 	} else {
600af548787SNicolas Souchu 		if (bootverbose)
601af548787SNicolas Souchu 			printf("PC873xx unlocked");
602af548787SNicolas Souchu 
603af548787SNicolas Souchu 		if (chipset_mode & PPB_ECP) {
604af548787SNicolas Souchu 			if ((chipset_mode & PPB_EPP) && bootverbose)
605af548787SNicolas Souchu 				printf(", ECP+EPP not supported");
606af548787SNicolas Souchu 
607af548787SNicolas Souchu 			pcr &= ~PC873_EPPEN;
608af548787SNicolas Souchu 			pcr |= (PC873_ECPEN | PC873_ECPCLK);	/* XXX */
609af548787SNicolas Souchu 			outb(idport + 1, pcr);
610af548787SNicolas Souchu 			outb(idport + 1, pcr);
611af548787SNicolas Souchu 
612af548787SNicolas Souchu 			if (bootverbose)
613af548787SNicolas Souchu 				printf(", ECP");
614af548787SNicolas Souchu 
615af548787SNicolas Souchu 		} else if (chipset_mode & PPB_EPP) {
616af548787SNicolas Souchu 			pcr &= ~(PC873_ECPEN | PC873_ECPCLK);
617af548787SNicolas Souchu 			pcr |= (PC873_EPPEN | PC873_EPP19);
618af548787SNicolas Souchu 			outb(idport + 1, pcr);
619af548787SNicolas Souchu 			outb(idport + 1, pcr);
620af548787SNicolas Souchu 
621af548787SNicolas Souchu 			ppc->ppc_epp = EPP_1_9;			/* XXX */
622af548787SNicolas Souchu 
623af548787SNicolas Souchu 			if (bootverbose)
624af548787SNicolas Souchu 				printf(", EPP1.9");
62567646539SMike Smith 
62667646539SMike Smith 			/* enable automatic direction turnover */
6270f210c92SNicolas Souchu 			if (ppc->ppc_model == NS_PC87332) {
62867646539SMike Smith 				outb(idport, PC873_PTR);
629af548787SNicolas Souchu 				ptr = inb(idport + 1);
630af548787SNicolas Souchu 				ptr &= ~PC873_EPPRDIR;
631af548787SNicolas Souchu 				outb(idport + 1, ptr);
632af548787SNicolas Souchu 				outb(idport + 1, ptr);
63367646539SMike Smith 
63467646539SMike Smith 				if (bootverbose)
635af548787SNicolas Souchu 					printf(", Automatic mode");
63667646539SMike Smith 			}
637af548787SNicolas Souchu 		} else {
638af548787SNicolas Souchu 			pcr &= ~(PC873_ECPEN | PC873_ECPCLK | PC873_EPPEN);
639af548787SNicolas Souchu 			outb(idport + 1, pcr);
640af548787SNicolas Souchu 			outb(idport + 1, pcr);
641af548787SNicolas Souchu 
642af548787SNicolas Souchu 			/* configure extended bit in PTR */
643af548787SNicolas Souchu 			outb(idport, PC873_PTR);
644af548787SNicolas Souchu 			ptr = inb(idport + 1);
645af548787SNicolas Souchu 
646af548787SNicolas Souchu 			if (chipset_mode & PPB_PS2) {
647af548787SNicolas Souchu 				ptr |= PC873_EXTENDED;
648af548787SNicolas Souchu 
649af548787SNicolas Souchu 				if (bootverbose)
650af548787SNicolas Souchu 					printf(", PS/2");
651af548787SNicolas Souchu 
652af548787SNicolas Souchu 			} else {
653af548787SNicolas Souchu 				/* default to NIBBLE mode */
654af548787SNicolas Souchu 				ptr &= ~PC873_EXTENDED;
655af548787SNicolas Souchu 
656af548787SNicolas Souchu 				if (bootverbose)
657af548787SNicolas Souchu 					printf(", NIBBLE");
65867646539SMike Smith 			}
659af548787SNicolas Souchu 			outb(idport + 1, ptr);
660af548787SNicolas Souchu 			outb(idport + 1, ptr);
661af548787SNicolas Souchu 		}
662af548787SNicolas Souchu 
663af548787SNicolas Souchu 		ppc->ppc_avm = chipset_mode;
664af548787SNicolas Souchu 	}
665af548787SNicolas Souchu 
666af548787SNicolas Souchu 	if (bootverbose)
667af548787SNicolas Souchu 		printf("\n");
668af548787SNicolas Souchu 
6690f210c92SNicolas Souchu 	ppc->ppc_type = PPC_TYPE_GENERIC;
6700f210c92SNicolas Souchu 	ppc_generic_setmode(ppc, chipset_mode);
67146f3ff79SMike Smith 
67246f3ff79SMike Smith 	return(chipset_mode);
67367646539SMike Smith     }
67446f3ff79SMike Smith     return(-1);
67567646539SMike Smith }
67667646539SMike Smith 
67767646539SMike Smith /*
67867646539SMike Smith  * ppc_smc37c66xgt_detect
67967646539SMike Smith  *
68067646539SMike Smith  * SMC FDC37C66xGT configuration.
68167646539SMike Smith  */
68267646539SMike Smith static int
68346f3ff79SMike Smith ppc_smc37c66xgt_detect(struct ppc_data *ppc, int chipset_mode)
68467646539SMike Smith {
68567646539SMike Smith 	int s, i;
686c9ab0738SNicolas Souchu 	u_char r;
68767646539SMike Smith 	int type = -1;
68867646539SMike Smith 	int csr = SMC66x_CSR;	/* initial value is 0x3F0 */
68967646539SMike Smith 
69067646539SMike Smith 	int port_address[] = { -1 /* disabled */ , 0x3bc, 0x378, 0x278 };
69167646539SMike Smith 
69267646539SMike Smith 
69367646539SMike Smith #define cio csr+1	/* config IO port is either 0x3F1 or 0x371 */
69467646539SMike Smith 
69567646539SMike Smith 	/*
69667646539SMike Smith 	 * Detection: enter configuration mode and read CRD register.
69767646539SMike Smith 	 */
69867646539SMike Smith 
69967646539SMike Smith 	s = splhigh();
70067646539SMike Smith 	outb(csr, SMC665_iCODE);
70167646539SMike Smith 	outb(csr, SMC665_iCODE);
70267646539SMike Smith 	splx(s);
70367646539SMike Smith 
70467646539SMike Smith 	outb(csr, 0xd);
70567646539SMike Smith 	if (inb(cio) == 0x65) {
70667646539SMike Smith 		type = SMC_37C665GT;
70767646539SMike Smith 		goto config;
70867646539SMike Smith 	}
70967646539SMike Smith 
71067646539SMike Smith 	for (i = 0; i < 2; i++) {
71167646539SMike Smith 		s = splhigh();
71267646539SMike Smith 		outb(csr, SMC666_iCODE);
71367646539SMike Smith 		outb(csr, SMC666_iCODE);
71467646539SMike Smith 		splx(s);
71567646539SMike Smith 
71667646539SMike Smith 		outb(csr, 0xd);
71767646539SMike Smith 		if (inb(cio) == 0x66) {
71867646539SMike Smith 			type = SMC_37C666GT;
71967646539SMike Smith 			break;
72067646539SMike Smith 		}
72167646539SMike Smith 
72267646539SMike Smith 		/* Another chance, CSR may be hard-configured to be at 0x370 */
72367646539SMike Smith 		csr = SMC666_CSR;
72467646539SMike Smith 	}
72567646539SMike Smith 
72667646539SMike Smith config:
72767646539SMike Smith 	/*
72867646539SMike Smith 	 * If chipset not found, do not continue.
72967646539SMike Smith 	 */
73067646539SMike Smith 	if (type == -1)
73146f3ff79SMike Smith 		return (-1);
73267646539SMike Smith 
73367646539SMike Smith 	/* select CR1 */
73467646539SMike Smith 	outb(csr, 0x1);
73567646539SMike Smith 
73667646539SMike Smith 	/* read the port's address: bits 0 and 1 of CR1 */
73767646539SMike Smith 	r = inb(cio) & SMC_CR1_ADDR;
738c9ab0738SNicolas Souchu 	if (port_address[(int)r] != ppc->ppc_base)
73946f3ff79SMike Smith 		return (-1);
74067646539SMike Smith 
7410f210c92SNicolas Souchu 	ppc->ppc_model = type;
74267646539SMike Smith 
74367646539SMike Smith 	/*
74467646539SMike Smith 	 * CR1 and CR4 registers bits 3 and 0/1 for mode configuration
74546f3ff79SMike Smith 	 * If SPP mode is detected, try to set ECP+EPP mode
74667646539SMike Smith 	 */
74767646539SMike Smith 
74846f3ff79SMike Smith 	if (bootverbose) {
74946f3ff79SMike Smith 		outb(csr, 0x1);
75041990851SNicolas Souchu 		printf("ppc%d: SMC registers CR1=0x%x", ppc->ppc_unit,
75154ad6085SNicolas Souchu 			inb(cio) & 0xff);
75246f3ff79SMike Smith 
75346f3ff79SMike Smith 		outb(csr, 0x4);
75446f3ff79SMike Smith 		printf(" CR4=0x%x", inb(cio) & 0xff);
75546f3ff79SMike Smith 	}
75646f3ff79SMike Smith 
75746f3ff79SMike Smith 	/* select CR1 */
75867646539SMike Smith 	outb(csr, 0x1);
75967646539SMike Smith 
76046f3ff79SMike Smith 	if (!chipset_mode) {
76167646539SMike Smith 		/* autodetect mode */
76267646539SMike Smith 
76346f3ff79SMike Smith 		/* 666GT is ~certainly~ hardwired to an extended ECP+EPP mode */
76446f3ff79SMike Smith 		if (type == SMC_37C666GT) {
76546f3ff79SMike Smith 			ppc->ppc_avm |= PPB_ECP | PPB_EPP | PPB_SPP;
766edcfcf27SNicolas Souchu 			if (bootverbose)
767edcfcf27SNicolas Souchu 				printf(" configuration hardwired, supposing " \
768edcfcf27SNicolas Souchu 					"ECP+EPP SPP");
76967646539SMike Smith 
77046f3ff79SMike Smith 		} else
77146f3ff79SMike Smith 		   if ((inb(cio) & SMC_CR1_MODE) == 0) {
77267646539SMike Smith 			/* already in extended parallel port mode, read CR4 */
77367646539SMike Smith 			outb(csr, 0x4);
77467646539SMike Smith 			r = (inb(cio) & SMC_CR4_EMODE);
77567646539SMike Smith 
77667646539SMike Smith 			switch (r) {
77767646539SMike Smith 			case SMC_SPP:
77846f3ff79SMike Smith 				ppc->ppc_avm |= PPB_SPP;
779edcfcf27SNicolas Souchu 				if (bootverbose)
780edcfcf27SNicolas Souchu 					printf(" SPP");
78167646539SMike Smith 				break;
78267646539SMike Smith 
78367646539SMike Smith 			case SMC_EPPSPP:
78446f3ff79SMike Smith 				ppc->ppc_avm |= PPB_EPP | PPB_SPP;
785edcfcf27SNicolas Souchu 				if (bootverbose)
786edcfcf27SNicolas Souchu 					printf(" EPP SPP");
78767646539SMike Smith 				break;
78867646539SMike Smith 
78967646539SMike Smith 			case SMC_ECP:
79046f3ff79SMike Smith 				ppc->ppc_avm |= PPB_ECP | PPB_SPP;
791edcfcf27SNicolas Souchu 				if (bootverbose)
792edcfcf27SNicolas Souchu 					printf(" ECP SPP");
79367646539SMike Smith 				break;
79467646539SMike Smith 
79567646539SMike Smith 			case SMC_ECPEPP:
79646f3ff79SMike Smith 				ppc->ppc_avm |= PPB_ECP | PPB_EPP | PPB_SPP;
797edcfcf27SNicolas Souchu 				if (bootverbose)
798edcfcf27SNicolas Souchu 					printf(" ECP+EPP SPP");
79967646539SMike Smith 				break;
80067646539SMike Smith 			}
80146f3ff79SMike Smith 		   } else {
80246f3ff79SMike Smith 			/* not an extended port mode */
80346f3ff79SMike Smith 			ppc->ppc_avm |= PPB_SPP;
804edcfcf27SNicolas Souchu 			if (bootverbose)
805edcfcf27SNicolas Souchu 				printf(" SPP");
80667646539SMike Smith 		   }
80746f3ff79SMike Smith 
80867646539SMike Smith 	} else {
80967646539SMike Smith 		/* mode forced */
81054ad6085SNicolas Souchu 		ppc->ppc_avm = chipset_mode;
81167646539SMike Smith 
81246f3ff79SMike Smith 		/* 666GT is ~certainly~ hardwired to an extended ECP+EPP mode */
81367646539SMike Smith 		if (type == SMC_37C666GT)
81467646539SMike Smith 			goto end_detect;
81567646539SMike Smith 
81667646539SMike Smith 		r = inb(cio);
81746f3ff79SMike Smith 		if ((chipset_mode & (PPB_ECP | PPB_EPP)) == 0) {
81846f3ff79SMike Smith 			/* do not use ECP when the mode is not forced to */
81967646539SMike Smith 			outb(cio, r | SMC_CR1_MODE);
820edcfcf27SNicolas Souchu 			if (bootverbose)
821edcfcf27SNicolas Souchu 				printf(" SPP");
82267646539SMike Smith 		} else {
82367646539SMike Smith 			/* an extended mode is selected */
82467646539SMike Smith 			outb(cio, r & ~SMC_CR1_MODE);
82567646539SMike Smith 
82667646539SMike Smith 			/* read CR4 register and reset mode field */
82767646539SMike Smith 			outb(csr, 0x4);
82867646539SMike Smith 			r = inb(cio) & ~SMC_CR4_EMODE;
82967646539SMike Smith 
83046f3ff79SMike Smith 			if (chipset_mode & PPB_ECP) {
83146f3ff79SMike Smith 				if (chipset_mode & PPB_EPP) {
83267646539SMike Smith 					outb(cio, r | SMC_ECPEPP);
833edcfcf27SNicolas Souchu 					if (bootverbose)
834edcfcf27SNicolas Souchu 						printf(" ECP+EPP");
83546f3ff79SMike Smith 				} else {
83646f3ff79SMike Smith 					outb(cio, r | SMC_ECP);
837edcfcf27SNicolas Souchu 					if (bootverbose)
838edcfcf27SNicolas Souchu 						printf(" ECP");
83946f3ff79SMike Smith 				}
84046f3ff79SMike Smith 			} else {
84146f3ff79SMike Smith 				/* PPB_EPP is set */
84246f3ff79SMike Smith 				outb(cio, r | SMC_EPPSPP);
843edcfcf27SNicolas Souchu 				if (bootverbose)
844edcfcf27SNicolas Souchu 					printf(" EPP SPP");
84567646539SMike Smith 			}
84667646539SMike Smith 		}
84746f3ff79SMike Smith 		ppc->ppc_avm = chipset_mode;
84867646539SMike Smith 	}
84967646539SMike Smith 
850bc35c174SNicolas Souchu 	/* set FIFO threshold to 16 */
851bc35c174SNicolas Souchu 	if (ppc->ppc_avm & PPB_ECP) {
852bc35c174SNicolas Souchu 		/* select CRA */
853bc35c174SNicolas Souchu 		outb(csr, 0xa);
854bc35c174SNicolas Souchu 		outb(cio, 16);
855bc35c174SNicolas Souchu 	}
856bc35c174SNicolas Souchu 
85767646539SMike Smith end_detect:
85846f3ff79SMike Smith 
85946f3ff79SMike Smith 	if (bootverbose)
86046f3ff79SMike Smith 		printf ("\n");
86146f3ff79SMike Smith 
86254ad6085SNicolas Souchu 	if (ppc->ppc_avm & PPB_EPP) {
86367646539SMike Smith 		/* select CR4 */
86467646539SMike Smith 		outb(csr, 0x4);
86567646539SMike Smith 		r = inb(cio);
86667646539SMike Smith 
86767646539SMike Smith 		/*
86867646539SMike Smith 		 * Set the EPP protocol...
86967646539SMike Smith 		 * Low=EPP 1.9 (1284 standard) and High=EPP 1.7
87067646539SMike Smith 		 */
87167646539SMike Smith 		if (ppc->ppc_epp == EPP_1_9)
87267646539SMike Smith 			outb(cio, (r & ~SMC_CR4_EPPTYPE));
87367646539SMike Smith 		else
87467646539SMike Smith 			outb(cio, (r | SMC_CR4_EPPTYPE));
87567646539SMike Smith 	}
87667646539SMike Smith 
87767646539SMike Smith 	/* end config mode */
87867646539SMike Smith 	outb(csr, 0xaa);
87967646539SMike Smith 
8800f210c92SNicolas Souchu 	ppc->ppc_type = PPC_TYPE_SMCLIKE;
8810f210c92SNicolas Souchu 	ppc_smclike_setmode(ppc, chipset_mode);
88267646539SMike Smith 
88346f3ff79SMike Smith 	return (chipset_mode);
88467646539SMike Smith }
88567646539SMike Smith 
88646f3ff79SMike Smith /*
88746f3ff79SMike Smith  * Winbond W83877F stuff
88846f3ff79SMike Smith  *
88946f3ff79SMike Smith  * EFER: extended function enable register
89046f3ff79SMike Smith  * EFIR: extended function index register
89146f3ff79SMike Smith  * EFDR: extended function data register
89246f3ff79SMike Smith  */
89346f3ff79SMike Smith #define efir ((efer == 0x250) ? 0x251 : 0x3f0)
89446f3ff79SMike Smith #define efdr ((efer == 0x250) ? 0x252 : 0x3f1)
89546f3ff79SMike Smith 
89646f3ff79SMike Smith static int w83877f_efers[] = { 0x250, 0x3f0, 0x3f0, 0x250 };
89746f3ff79SMike Smith static int w83877f_keys[] = { 0x89, 0x86, 0x87, 0x88 };
89846f3ff79SMike Smith static int w83877f_keyiter[] = { 1, 2, 2, 1 };
89946f3ff79SMike Smith static int w83877f_hefs[] = { WINB_HEFERE, WINB_HEFRAS, WINB_HEFERE | WINB_HEFRAS, 0 };
90067646539SMike Smith 
90167646539SMike Smith static int
90246f3ff79SMike Smith ppc_w83877f_detect(struct ppc_data *ppc, int chipset_mode)
90367646539SMike Smith {
904f1d19042SArchie Cobbs 	int i, j, efer;
90546f3ff79SMike Smith 	unsigned char r, hefere, hefras;
90667646539SMike Smith 
90746f3ff79SMike Smith 	for (i = 0; i < 4; i ++) {
90846f3ff79SMike Smith 		/* first try to enable configuration registers */
90946f3ff79SMike Smith 		efer = w83877f_efers[i];
91067646539SMike Smith 
91146f3ff79SMike Smith 		/* write the key to the EFER */
91246f3ff79SMike Smith 		for (j = 0; j < w83877f_keyiter[i]; j ++)
91346f3ff79SMike Smith 			outb (efer, w83877f_keys[i]);
91446f3ff79SMike Smith 
91546f3ff79SMike Smith 		/* then check HEFERE and HEFRAS bits */
91646f3ff79SMike Smith 		outb (efir, 0x0c);
91746f3ff79SMike Smith 		hefere = inb(efdr) & WINB_HEFERE;
91846f3ff79SMike Smith 
91946f3ff79SMike Smith 		outb (efir, 0x16);
92046f3ff79SMike Smith 		hefras = inb(efdr) & WINB_HEFRAS;
92146f3ff79SMike Smith 
92246f3ff79SMike Smith 		/*
92346f3ff79SMike Smith 		 * HEFRAS	HEFERE
92446f3ff79SMike Smith 		 *   0		   1	write 89h to 250h (power-on default)
92546f3ff79SMike Smith 		 *   1		   0	write 86h twice to 3f0h
92646f3ff79SMike Smith 		 *   1		   1	write 87h twice to 3f0h
92746f3ff79SMike Smith 		 *   0		   0	write 88h to 250h
92846f3ff79SMike Smith 		 */
92946f3ff79SMike Smith 		if ((hefere | hefras) == w83877f_hefs[i])
93046f3ff79SMike Smith 			goto found;
93167646539SMike Smith 	}
93267646539SMike Smith 
93346f3ff79SMike Smith 	return (-1);	/* failed */
93467646539SMike Smith 
93546f3ff79SMike Smith found:
93646f3ff79SMike Smith 	/* check base port address - read from CR23 */
93746f3ff79SMike Smith 	outb(efir, 0x23);
93846f3ff79SMike Smith 	if (ppc->ppc_base != inb(efdr) * 4)		/* 4 bytes boundaries */
93946f3ff79SMike Smith 		return (-1);
94046f3ff79SMike Smith 
94146f3ff79SMike Smith 	/* read CHIP ID from CR9/bits0-3 */
94246f3ff79SMike Smith 	outb(efir, 0x9);
94346f3ff79SMike Smith 
94446f3ff79SMike Smith 	switch (inb(efdr) & WINB_CHIPID) {
94546f3ff79SMike Smith 		case WINB_W83877F_ID:
9460f210c92SNicolas Souchu 			ppc->ppc_model = WINB_W83877F;
94746f3ff79SMike Smith 			break;
94846f3ff79SMike Smith 
94946f3ff79SMike Smith 		case WINB_W83877AF_ID:
9500f210c92SNicolas Souchu 			ppc->ppc_model = WINB_W83877AF;
95146f3ff79SMike Smith 			break;
95246f3ff79SMike Smith 
95346f3ff79SMike Smith 		default:
9540f210c92SNicolas Souchu 			ppc->ppc_model = WINB_UNKNOWN;
95546f3ff79SMike Smith 	}
95646f3ff79SMike Smith 
95746f3ff79SMike Smith 	if (bootverbose) {
95846f3ff79SMike Smith 		/* dump of registers */
95946f3ff79SMike Smith 		printf("ppc%d: 0x%x - ", ppc->ppc_unit, w83877f_keys[i]);
96046f3ff79SMike Smith 		for (i = 0; i <= 0xd; i ++) {
96146f3ff79SMike Smith 			outb(efir, i);
96246f3ff79SMike Smith 			printf("0x%x ", inb(efdr));
96346f3ff79SMike Smith 		}
96446f3ff79SMike Smith 		for (i = 0x10; i <= 0x17; i ++) {
96546f3ff79SMike Smith 			outb(efir, i);
96646f3ff79SMike Smith 			printf("0x%x ", inb(efdr));
96746f3ff79SMike Smith 		}
96846f3ff79SMike Smith 		outb(efir, 0x1e);
96946f3ff79SMike Smith 		printf("0x%x ", inb(efdr));
97046f3ff79SMike Smith 		for (i = 0x20; i <= 0x29; i ++) {
97146f3ff79SMike Smith 			outb(efir, i);
97246f3ff79SMike Smith 			printf("0x%x ", inb(efdr));
97346f3ff79SMike Smith 		}
97446f3ff79SMike Smith 		printf("\n");
975edcfcf27SNicolas Souchu 		printf("ppc%d:", ppc->ppc_unit);
97646f3ff79SMike Smith 	}
97746f3ff79SMike Smith 
9780f210c92SNicolas Souchu 	ppc->ppc_type = PPC_TYPE_GENERIC;
979edcfcf27SNicolas Souchu 
98046f3ff79SMike Smith 	if (!chipset_mode) {
98146f3ff79SMike Smith 		/* autodetect mode */
98246f3ff79SMike Smith 
98346f3ff79SMike Smith 		/* select CR0 */
98446f3ff79SMike Smith 		outb(efir, 0x0);
98546f3ff79SMike Smith 		r = inb(efdr) & (WINB_PRTMODS0 | WINB_PRTMODS1);
98646f3ff79SMike Smith 
98746f3ff79SMike Smith 		/* select CR9 */
98846f3ff79SMike Smith 		outb(efir, 0x9);
98946f3ff79SMike Smith 		r |= (inb(efdr) & WINB_PRTMODS2);
99046f3ff79SMike Smith 
99146f3ff79SMike Smith 		switch (r) {
99246f3ff79SMike Smith 		case WINB_W83757:
99346f3ff79SMike Smith 			if (bootverbose)
99446f3ff79SMike Smith 				printf("ppc%d: W83757 compatible mode\n",
99546f3ff79SMike Smith 					ppc->ppc_unit);
99646f3ff79SMike Smith 			return (-1);	/* generic or SMC-like */
99746f3ff79SMike Smith 
99846f3ff79SMike Smith 		case WINB_EXTFDC:
99946f3ff79SMike Smith 		case WINB_EXTADP:
100046f3ff79SMike Smith 		case WINB_EXT2FDD:
100146f3ff79SMike Smith 		case WINB_JOYSTICK:
100246f3ff79SMike Smith 			if (bootverbose)
1003edcfcf27SNicolas Souchu 				printf(" not in parallel port mode\n");
100446f3ff79SMike Smith 			return (-1);
100546f3ff79SMike Smith 
100646f3ff79SMike Smith 		case (WINB_PARALLEL | WINB_EPP_SPP):
100746f3ff79SMike Smith 			ppc->ppc_avm |= PPB_EPP | PPB_SPP;
1008edcfcf27SNicolas Souchu 			if (bootverbose)
1009edcfcf27SNicolas Souchu 				printf(" EPP SPP");
101046f3ff79SMike Smith 			break;
101146f3ff79SMike Smith 
101246f3ff79SMike Smith 		case (WINB_PARALLEL | WINB_ECP):
101346f3ff79SMike Smith 			ppc->ppc_avm |= PPB_ECP | PPB_SPP;
1014edcfcf27SNicolas Souchu 			if (bootverbose)
1015edcfcf27SNicolas Souchu 				printf(" ECP SPP");
101646f3ff79SMike Smith 			break;
101746f3ff79SMike Smith 
101846f3ff79SMike Smith 		case (WINB_PARALLEL | WINB_ECP_EPP):
101946f3ff79SMike Smith 			ppc->ppc_avm |= PPB_ECP | PPB_EPP | PPB_SPP;
10200f210c92SNicolas Souchu 			ppc->ppc_type = PPC_TYPE_SMCLIKE;
1021edcfcf27SNicolas Souchu 
1022edcfcf27SNicolas Souchu 			if (bootverbose)
1023edcfcf27SNicolas Souchu 				printf(" ECP+EPP SPP");
102446f3ff79SMike Smith 			break;
102546f3ff79SMike Smith 		default:
102646f3ff79SMike Smith 			printf("%s: unknown case (0x%x)!\n", __FUNCTION__, r);
102746f3ff79SMike Smith 		}
102846f3ff79SMike Smith 
102946f3ff79SMike Smith 	} else {
103046f3ff79SMike Smith 		/* mode forced */
103146f3ff79SMike Smith 
103246f3ff79SMike Smith 		/* select CR9 and set PRTMODS2 bit */
103346f3ff79SMike Smith 		outb(efir, 0x9);
103446f3ff79SMike Smith 		outb(efdr, inb(efdr) & ~WINB_PRTMODS2);
103546f3ff79SMike Smith 
103646f3ff79SMike Smith 		/* select CR0 and reset PRTMODSx bits */
103746f3ff79SMike Smith 		outb(efir, 0x0);
103846f3ff79SMike Smith 		outb(efdr, inb(efdr) & ~(WINB_PRTMODS0 | WINB_PRTMODS1));
103946f3ff79SMike Smith 
104046f3ff79SMike Smith 		if (chipset_mode & PPB_ECP) {
1041edcfcf27SNicolas Souchu 			if (chipset_mode & PPB_EPP) {
104246f3ff79SMike Smith 				outb(efdr, inb(efdr) | WINB_ECP_EPP);
1043edcfcf27SNicolas Souchu 				if (bootverbose)
1044edcfcf27SNicolas Souchu 					printf(" ECP+EPP");
1045edcfcf27SNicolas Souchu 
10460f210c92SNicolas Souchu 				ppc->ppc_type = PPC_TYPE_SMCLIKE;
1047edcfcf27SNicolas Souchu 
1048edcfcf27SNicolas Souchu 			} else {
104946f3ff79SMike Smith 				outb(efdr, inb(efdr) | WINB_ECP);
1050edcfcf27SNicolas Souchu 				if (bootverbose)
1051edcfcf27SNicolas Souchu 					printf(" ECP");
1052edcfcf27SNicolas Souchu 			}
105346f3ff79SMike Smith 		} else {
105446f3ff79SMike Smith 			/* select EPP_SPP otherwise */
105546f3ff79SMike Smith 			outb(efdr, inb(efdr) | WINB_EPP_SPP);
1056edcfcf27SNicolas Souchu 			if (bootverbose)
1057edcfcf27SNicolas Souchu 				printf(" EPP SPP");
105846f3ff79SMike Smith 		}
105946f3ff79SMike Smith 		ppc->ppc_avm = chipset_mode;
106046f3ff79SMike Smith 	}
106146f3ff79SMike Smith 
1062edcfcf27SNicolas Souchu 	if (bootverbose)
1063edcfcf27SNicolas Souchu 		printf("\n");
1064edcfcf27SNicolas Souchu 
106546f3ff79SMike Smith 	/* exit configuration mode */
106646f3ff79SMike Smith 	outb(efer, 0xaa);
106746f3ff79SMike Smith 
10680f210c92SNicolas Souchu 	switch (ppc->ppc_type) {
10690f210c92SNicolas Souchu 	case PPC_TYPE_SMCLIKE:
10700f210c92SNicolas Souchu 		ppc_smclike_setmode(ppc, chipset_mode);
10710f210c92SNicolas Souchu 		break;
10720f210c92SNicolas Souchu 	default:
10730f210c92SNicolas Souchu 		ppc_generic_setmode(ppc, chipset_mode);
10740f210c92SNicolas Souchu 		break;
10750f210c92SNicolas Souchu 	}
107646f3ff79SMike Smith 
107746f3ff79SMike Smith 	return (chipset_mode);
107867646539SMike Smith }
10790f210c92SNicolas Souchu #endif
108067646539SMike Smith 
108167646539SMike Smith /*
108267646539SMike Smith  * ppc_generic_detect
108367646539SMike Smith  */
108467646539SMike Smith static int
108546f3ff79SMike Smith ppc_generic_detect(struct ppc_data *ppc, int chipset_mode)
108667646539SMike Smith {
1087edcfcf27SNicolas Souchu 	/* default to generic */
10880f210c92SNicolas Souchu 	ppc->ppc_type = PPC_TYPE_GENERIC;
1089edcfcf27SNicolas Souchu 
1090edcfcf27SNicolas Souchu 	if (bootverbose)
1091edcfcf27SNicolas Souchu 		printf("ppc%d:", ppc->ppc_unit);
1092edcfcf27SNicolas Souchu 
109346f3ff79SMike Smith 	if (!chipset_mode) {
109446f3ff79SMike Smith 		/* first, check for ECP */
1095bc35c174SNicolas Souchu 		w_ecr(ppc, PPC_ECR_PS2);
1096bc35c174SNicolas Souchu 		if ((r_ecr(ppc) & 0xe0) == PPC_ECR_PS2) {
109746f3ff79SMike Smith 			ppc->ppc_avm |= PPB_ECP | PPB_SPP;
1098edcfcf27SNicolas Souchu 			if (bootverbose)
1099edcfcf27SNicolas Souchu 				printf(" ECP SPP");
110046f3ff79SMike Smith 
110146f3ff79SMike Smith 			/* search for SMC style ECP+EPP mode */
1102bc35c174SNicolas Souchu 			w_ecr(ppc, PPC_ECR_EPP);
110346f3ff79SMike Smith 		}
110467646539SMike Smith 
110567646539SMike Smith 		/* try to reset EPP timeout bit */
110646f3ff79SMike Smith 		if (ppc_check_epp_timeout(ppc)) {
110746f3ff79SMike Smith 			ppc->ppc_avm |= PPB_EPP;
110867646539SMike Smith 
1109edcfcf27SNicolas Souchu 			if (ppc->ppc_avm & PPB_ECP) {
111046f3ff79SMike Smith 				/* SMC like chipset found */
11110f210c92SNicolas Souchu 				ppc->ppc_model = SMC_LIKE;
11120f210c92SNicolas Souchu 				ppc->ppc_type = PPC_TYPE_SMCLIKE;
1113edcfcf27SNicolas Souchu 
1114edcfcf27SNicolas Souchu 				if (bootverbose)
1115edcfcf27SNicolas Souchu 					printf(" ECP+EPP");
1116edcfcf27SNicolas Souchu 			} else {
1117edcfcf27SNicolas Souchu 				if (bootverbose)
1118edcfcf27SNicolas Souchu 					printf(" EPP");
1119edcfcf27SNicolas Souchu 			}
1120edcfcf27SNicolas Souchu 		} else {
1121edcfcf27SNicolas Souchu 			/* restore to standard mode */
1122bc35c174SNicolas Souchu 			w_ecr(ppc, PPC_ECR_STD);
112367646539SMike Smith 		}
112467646539SMike Smith 
1125edcfcf27SNicolas Souchu 		/* XXX try to detect NIBBLE and PS2 modes */
112646f3ff79SMike Smith 		ppc->ppc_avm |= PPB_NIBBLE;
112767646539SMike Smith 
1128edcfcf27SNicolas Souchu 		if (bootverbose)
1129edcfcf27SNicolas Souchu 			printf(" SPP");
113067646539SMike Smith 
1131edcfcf27SNicolas Souchu 	} else {
1132edcfcf27SNicolas Souchu 		ppc->ppc_avm = chipset_mode;
1133edcfcf27SNicolas Souchu 	}
1134edcfcf27SNicolas Souchu 
1135edcfcf27SNicolas Souchu 	if (bootverbose)
1136edcfcf27SNicolas Souchu 		printf("\n");
1137edcfcf27SNicolas Souchu 
11380f210c92SNicolas Souchu 	switch (ppc->ppc_type) {
11390f210c92SNicolas Souchu 	case PPC_TYPE_SMCLIKE:
11400f210c92SNicolas Souchu 		ppc_smclike_setmode(ppc, chipset_mode);
11410f210c92SNicolas Souchu 		break;
11420f210c92SNicolas Souchu 	default:
11430f210c92SNicolas Souchu 		ppc_generic_setmode(ppc, chipset_mode);
11440f210c92SNicolas Souchu 		break;
11450f210c92SNicolas Souchu 	}
114646f3ff79SMike Smith 
114746f3ff79SMike Smith 	return (chipset_mode);
114867646539SMike Smith }
114967646539SMike Smith 
115067646539SMike Smith /*
115167646539SMike Smith  * ppc_detect()
115267646539SMike Smith  *
115367646539SMike Smith  * mode is the mode suggested at boot
115467646539SMike Smith  */
115567646539SMike Smith static int
115646f3ff79SMike Smith ppc_detect(struct ppc_data *ppc, int chipset_mode) {
115767646539SMike Smith 
11580f210c92SNicolas Souchu #ifdef PPC_PROBE_CHIPSET
115946f3ff79SMike Smith 	int i, mode;
116067646539SMike Smith 
116146f3ff79SMike Smith 	/* list of supported chipsets */
116246f3ff79SMike Smith 	int (*chipset_detect[])(struct ppc_data *, int) = {
116346f3ff79SMike Smith 		ppc_pc873xx_detect,
116446f3ff79SMike Smith 		ppc_smc37c66xgt_detect,
116546f3ff79SMike Smith 		ppc_w83877f_detect,
116646f3ff79SMike Smith 		ppc_generic_detect,
116746f3ff79SMike Smith 		NULL
116846f3ff79SMike Smith 	};
11690f210c92SNicolas Souchu #endif
117067646539SMike Smith 
117146f3ff79SMike Smith 	/* if can't find the port and mode not forced return error */
117246f3ff79SMike Smith 	if (!ppc_detect_port(ppc) && chipset_mode == 0)
117346f3ff79SMike Smith 		return (EIO);			/* failed, port not present */
117467646539SMike Smith 
117546f3ff79SMike Smith 	/* assume centronics compatible mode is supported */
117646f3ff79SMike Smith 	ppc->ppc_avm = PPB_COMPATIBLE;
117767646539SMike Smith 
11780f210c92SNicolas Souchu #ifdef PPC_PROBE_CHIPSET
117946f3ff79SMike Smith 	/* we have to differenciate available chipset modes,
118046f3ff79SMike Smith 	 * chipset running modes and IEEE-1284 operating modes
118146f3ff79SMike Smith 	 *
118246f3ff79SMike Smith 	 * after detection, the port must support running in compatible mode
118346f3ff79SMike Smith 	 */
1184af548787SNicolas Souchu 	if (ppc->ppc_flags & 0x40) {
1185af548787SNicolas Souchu 		if (bootverbose)
1186af548787SNicolas Souchu 			printf("ppc: chipset forced to generic\n");
11870f210c92SNicolas Souchu #endif
1188af548787SNicolas Souchu 
1189af548787SNicolas Souchu 		ppc->ppc_mode = ppc_generic_detect(ppc, chipset_mode);
1190af548787SNicolas Souchu 
11910f210c92SNicolas Souchu #ifdef PPC_PROBE_CHIPSET
1192af548787SNicolas Souchu 	} else {
119346f3ff79SMike Smith 		for (i=0; chipset_detect[i] != NULL; i++) {
119446f3ff79SMike Smith 			if ((mode = chipset_detect[i](ppc, chipset_mode)) != -1) {
119546f3ff79SMike Smith 				ppc->ppc_mode = mode;
119646f3ff79SMike Smith 				break;
119746f3ff79SMike Smith 			}
119846f3ff79SMike Smith 		}
1199af548787SNicolas Souchu 	}
12000f210c92SNicolas Souchu #endif
120146f3ff79SMike Smith 
1202bc35c174SNicolas Souchu 	/* configure/detect ECP FIFO */
1203bc35c174SNicolas Souchu 	if ((ppc->ppc_avm & PPB_ECP) && !(ppc->ppc_flags & 0x80))
1204bc35c174SNicolas Souchu 		ppc_detect_fifo(ppc);
1205bc35c174SNicolas Souchu 
120646f3ff79SMike Smith 	return (0);
120746f3ff79SMike Smith }
120846f3ff79SMike Smith 
120946f3ff79SMike Smith /*
121046f3ff79SMike Smith  * ppc_exec_microseq()
121146f3ff79SMike Smith  *
121246f3ff79SMike Smith  * Execute a microsequence.
121346f3ff79SMike Smith  * Microsequence mechanism is supposed to handle fast I/O operations.
121446f3ff79SMike Smith  */
121546f3ff79SMike Smith static int
12160f210c92SNicolas Souchu ppc_exec_microseq(device_t dev, struct ppb_microseq **p_msq)
121746f3ff79SMike Smith {
12180f210c92SNicolas Souchu 	struct ppc_data *ppc = DEVTOSOFTC(dev);
12190a40e22aSNicolas Souchu 	struct ppb_microseq *mi;
122046f3ff79SMike Smith 	char cc, *p;
122154ad6085SNicolas Souchu 	int i, iter, len;
122246f3ff79SMike Smith 	int error;
122346f3ff79SMike Smith 
122454ad6085SNicolas Souchu 	register int reg;
122554ad6085SNicolas Souchu 	register char mask;
122654ad6085SNicolas Souchu 	register int accum = 0;
122754ad6085SNicolas Souchu 	register char *ptr = 0;
122846f3ff79SMike Smith 
12290a40e22aSNicolas Souchu 	struct ppb_microseq *stack = 0;
123046f3ff79SMike Smith 
123146f3ff79SMike Smith /* microsequence registers are equivalent to PC-like port registers */
1232a7006f89SNicolas Souchu #define r_reg(register,ppc) (inb((ppc)->ppc_base + register))
123346f3ff79SMike Smith #define w_reg(register,ppc,byte) outb((ppc)->ppc_base + register, byte)
123446f3ff79SMike Smith 
12350a40e22aSNicolas Souchu #define INCR_PC (mi ++)		/* increment program counter */
123646f3ff79SMike Smith 
12370a40e22aSNicolas Souchu 	mi = *p_msq;
123846f3ff79SMike Smith 	for (;;) {
123946f3ff79SMike Smith 		switch (mi->opcode) {
124046f3ff79SMike Smith 		case MS_OP_RSET:
124146f3ff79SMike Smith 			cc = r_reg(mi->arg[0].i, ppc);
124254ad6085SNicolas Souchu 			cc &= (char)mi->arg[2].i;	/* clear mask */
124354ad6085SNicolas Souchu 			cc |= (char)mi->arg[1].i;	/* assert mask */
124446f3ff79SMike Smith                         w_reg(mi->arg[0].i, ppc, cc);
124546f3ff79SMike Smith 			INCR_PC;
124646f3ff79SMike Smith                         break;
124746f3ff79SMike Smith 
124846f3ff79SMike Smith 		case MS_OP_RASSERT_P:
124954ad6085SNicolas Souchu 			reg = mi->arg[1].i;
125054ad6085SNicolas Souchu 			ptr = ppc->ppc_ptr;
125154ad6085SNicolas Souchu 
125254ad6085SNicolas Souchu 			if ((len = mi->arg[0].i) == MS_ACCUM) {
125354ad6085SNicolas Souchu 				accum = ppc->ppc_accum;
125454ad6085SNicolas Souchu 				for (; accum; accum--)
125554ad6085SNicolas Souchu 					w_reg(reg, ppc, *ptr++);
125654ad6085SNicolas Souchu 				ppc->ppc_accum = accum;
125754ad6085SNicolas Souchu 			} else
125854ad6085SNicolas Souchu 				for (i=0; i<len; i++)
125954ad6085SNicolas Souchu 					w_reg(reg, ppc, *ptr++);
126054ad6085SNicolas Souchu 			ppc->ppc_ptr = ptr;
126154ad6085SNicolas Souchu 
126246f3ff79SMike Smith 			INCR_PC;
126346f3ff79SMike Smith 			break;
126446f3ff79SMike Smith 
126546f3ff79SMike Smith                 case MS_OP_RFETCH_P:
126654ad6085SNicolas Souchu 			reg = mi->arg[1].i;
126754ad6085SNicolas Souchu 			mask = (char)mi->arg[2].i;
126854ad6085SNicolas Souchu 			ptr = ppc->ppc_ptr;
126954ad6085SNicolas Souchu 
127054ad6085SNicolas Souchu 			if ((len = mi->arg[0].i) == MS_ACCUM) {
127154ad6085SNicolas Souchu 				accum = ppc->ppc_accum;
127254ad6085SNicolas Souchu 				for (; accum; accum--)
127354ad6085SNicolas Souchu 					*ptr++ = r_reg(reg, ppc) & mask;
127454ad6085SNicolas Souchu 				ppc->ppc_accum = accum;
127554ad6085SNicolas Souchu 			} else
127654ad6085SNicolas Souchu 				for (i=0; i<len; i++)
127754ad6085SNicolas Souchu 					*ptr++ = r_reg(reg, ppc) & mask;
127854ad6085SNicolas Souchu 			ppc->ppc_ptr = ptr;
127954ad6085SNicolas Souchu 
128046f3ff79SMike Smith 			INCR_PC;
128146f3ff79SMike Smith                         break;
128246f3ff79SMike Smith 
128346f3ff79SMike Smith                 case MS_OP_RFETCH:
128446f3ff79SMike Smith 			*((char *) mi->arg[2].p) = r_reg(mi->arg[0].i, ppc) &
128554ad6085SNicolas Souchu 							(char)mi->arg[1].i;
128646f3ff79SMike Smith 			INCR_PC;
128746f3ff79SMike Smith                         break;
128846f3ff79SMike Smith 
128946f3ff79SMike Smith 		case MS_OP_RASSERT:
129054ad6085SNicolas Souchu                 case MS_OP_DELAY:
129146f3ff79SMike Smith 
129246f3ff79SMike Smith 		/* let's suppose the next instr. is the same */
129346f3ff79SMike Smith 		prefetch:
129446f3ff79SMike Smith 			for (;mi->opcode == MS_OP_RASSERT; INCR_PC)
129554ad6085SNicolas Souchu 				w_reg(mi->arg[0].i, ppc, (char)mi->arg[1].i);
129646f3ff79SMike Smith 
129746f3ff79SMike Smith 			if (mi->opcode == MS_OP_DELAY) {
129846f3ff79SMike Smith 				DELAY(mi->arg[0].i);
129946f3ff79SMike Smith 				INCR_PC;
130046f3ff79SMike Smith 				goto prefetch;
130146f3ff79SMike Smith 			}
130246f3ff79SMike Smith 			break;
130346f3ff79SMike Smith 
130454ad6085SNicolas Souchu 		case MS_OP_ADELAY:
130554ad6085SNicolas Souchu 			if (mi->arg[0].i)
130654ad6085SNicolas Souchu 				tsleep(NULL, PPBPRI, "ppbdelay",
130754ad6085SNicolas Souchu 						mi->arg[0].i * (hz/1000));
130846f3ff79SMike Smith 			INCR_PC;
130946f3ff79SMike Smith 			break;
131046f3ff79SMike Smith 
131146f3ff79SMike Smith 		case MS_OP_TRIG:
131246f3ff79SMike Smith 			reg = mi->arg[0].i;
131346f3ff79SMike Smith 			iter = mi->arg[1].i;
131446f3ff79SMike Smith 			p = (char *)mi->arg[2].p;
131546f3ff79SMike Smith 
131654ad6085SNicolas Souchu 			/* XXX delay limited to 255 us */
131746f3ff79SMike Smith 			for (i=0; i<iter; i++) {
131846f3ff79SMike Smith 				w_reg(reg, ppc, *p++);
131946f3ff79SMike Smith 				DELAY((unsigned char)*p++);
132046f3ff79SMike Smith 			}
132146f3ff79SMike Smith 			INCR_PC;
132246f3ff79SMike Smith 			break;
132346f3ff79SMike Smith 
132446f3ff79SMike Smith                 case MS_OP_SET:
132554ad6085SNicolas Souchu                         ppc->ppc_accum = mi->arg[0].i;
132646f3ff79SMike Smith 			INCR_PC;
132746f3ff79SMike Smith                         break;
132846f3ff79SMike Smith 
132946f3ff79SMike Smith                 case MS_OP_DBRA:
133054ad6085SNicolas Souchu                         if (--ppc->ppc_accum > 0)
13310a40e22aSNicolas Souchu                                 mi += mi->arg[0].i;
133246f3ff79SMike Smith 			INCR_PC;
133346f3ff79SMike Smith                         break;
133446f3ff79SMike Smith 
133546f3ff79SMike Smith                 case MS_OP_BRSET:
133646f3ff79SMike Smith                         cc = r_str(ppc);
133754ad6085SNicolas Souchu                         if ((cc & (char)mi->arg[0].i) == (char)mi->arg[0].i)
13380a40e22aSNicolas Souchu                                 mi += mi->arg[1].i;
133946f3ff79SMike Smith 			INCR_PC;
134046f3ff79SMike Smith                         break;
134146f3ff79SMike Smith 
134246f3ff79SMike Smith                 case MS_OP_BRCLEAR:
134346f3ff79SMike Smith                         cc = r_str(ppc);
134454ad6085SNicolas Souchu                         if ((cc & (char)mi->arg[0].i) == 0)
13450a40e22aSNicolas Souchu                                 mi += mi->arg[1].i;
134646f3ff79SMike Smith 			INCR_PC;
134746f3ff79SMike Smith                         break;
134846f3ff79SMike Smith 
134954ad6085SNicolas Souchu 		case MS_OP_BRSTAT:
135054ad6085SNicolas Souchu 			cc = r_str(ppc);
135154ad6085SNicolas Souchu 			if ((cc & ((char)mi->arg[0].i | (char)mi->arg[1].i)) ==
135254ad6085SNicolas Souchu 							(char)mi->arg[0].i)
13530a40e22aSNicolas Souchu 				mi += mi->arg[2].i;
135454ad6085SNicolas Souchu 			INCR_PC;
135554ad6085SNicolas Souchu 			break;
135654ad6085SNicolas Souchu 
135746f3ff79SMike Smith 		case MS_OP_C_CALL:
135846f3ff79SMike Smith 			/*
135946f3ff79SMike Smith 			 * If the C call returns !0 then end the microseq.
136046f3ff79SMike Smith 			 * The current state of ptr is passed to the C function
136146f3ff79SMike Smith 			 */
136254ad6085SNicolas Souchu 			if ((error = mi->arg[0].f(mi->arg[1].p, ppc->ppc_ptr)))
136346f3ff79SMike Smith 				return (error);
136446f3ff79SMike Smith 
136546f3ff79SMike Smith 			INCR_PC;
136646f3ff79SMike Smith 			break;
136746f3ff79SMike Smith 
136846f3ff79SMike Smith 		case MS_OP_PTR:
136954ad6085SNicolas Souchu 			ppc->ppc_ptr = (char *)mi->arg[0].p;
137046f3ff79SMike Smith 			INCR_PC;
137146f3ff79SMike Smith 			break;
137246f3ff79SMike Smith 
137346f3ff79SMike Smith 		case MS_OP_CALL:
13740a40e22aSNicolas Souchu 			if (stack)
137546f3ff79SMike Smith 				panic("%s: too much calls", __FUNCTION__);
137646f3ff79SMike Smith 
137746f3ff79SMike Smith 			if (mi->arg[0].p) {
137846f3ff79SMike Smith 				/* store the state of the actual
137946f3ff79SMike Smith 				 * microsequence
138046f3ff79SMike Smith 				 */
13810a40e22aSNicolas Souchu 				stack = mi;
138246f3ff79SMike Smith 
138346f3ff79SMike Smith 				/* jump to the new microsequence */
13840a40e22aSNicolas Souchu 				mi = (struct ppb_microseq *)mi->arg[0].p;
138546f3ff79SMike Smith 			} else
138646f3ff79SMike Smith 				INCR_PC;
138746f3ff79SMike Smith 
138846f3ff79SMike Smith 			break;
138946f3ff79SMike Smith 
139046f3ff79SMike Smith 		case MS_OP_SUBRET:
139146f3ff79SMike Smith 			/* retrieve microseq and pc state before the call */
13920a40e22aSNicolas Souchu 			mi = stack;
139346f3ff79SMike Smith 
139446f3ff79SMike Smith 			/* reset the stack */
13950a40e22aSNicolas Souchu 			stack = 0;
139646f3ff79SMike Smith 
139746f3ff79SMike Smith 			/* XXX return code */
139846f3ff79SMike Smith 
139946f3ff79SMike Smith 			INCR_PC;
140046f3ff79SMike Smith 			break;
140146f3ff79SMike Smith 
140246f3ff79SMike Smith                 case MS_OP_PUT:
140346f3ff79SMike Smith                 case MS_OP_GET:
140446f3ff79SMike Smith                 case MS_OP_RET:
140546f3ff79SMike Smith 			/* can't return to ppb level during the execution
140646f3ff79SMike Smith 			 * of a submicrosequence */
14070a40e22aSNicolas Souchu 			if (stack)
140846f3ff79SMike Smith 				panic("%s: can't return to ppb level",
140946f3ff79SMike Smith 								__FUNCTION__);
141046f3ff79SMike Smith 
141146f3ff79SMike Smith 			/* update pc for ppb level of execution */
14120a40e22aSNicolas Souchu 			*p_msq = mi;
141346f3ff79SMike Smith 
141446f3ff79SMike Smith 			/* return to ppb level of execution */
141546f3ff79SMike Smith 			return (0);
141646f3ff79SMike Smith 
141746f3ff79SMike Smith                 default:
141846f3ff79SMike Smith                         panic("%s: unknown microsequence opcode 0x%x",
141946f3ff79SMike Smith                                 __FUNCTION__, mi->opcode);
142046f3ff79SMike Smith                 }
142146f3ff79SMike Smith 	}
142246f3ff79SMike Smith 
142346f3ff79SMike Smith 	/* unreached */
142446f3ff79SMike Smith }
142546f3ff79SMike Smith 
1426bc35c174SNicolas Souchu static void
14270f210c92SNicolas Souchu ppcintr(void *arg)
1428bc35c174SNicolas Souchu {
14290f210c92SNicolas Souchu 	device_t dev = (device_t)arg;
14300f210c92SNicolas Souchu 	struct ppc_data *ppc = (struct ppc_data *)device_get_softc(dev);
14313ab971c1SNicolas Souchu 	u_char ctr, ecr, str;
1432bc35c174SNicolas Souchu 
14333ab971c1SNicolas Souchu 	str = r_str(ppc);
1434bc35c174SNicolas Souchu 	ctr = r_ctr(ppc);
1435bc35c174SNicolas Souchu 	ecr = r_ecr(ppc);
1436bc35c174SNicolas Souchu 
14373ab971c1SNicolas Souchu #if PPC_DEBUG > 1
14383ab971c1SNicolas Souchu 		printf("![%x/%x/%x]", ctr, ecr, str);
1439bc35c174SNicolas Souchu #endif
1440bc35c174SNicolas Souchu 
1441bc35c174SNicolas Souchu 	/* don't use ecp mode with IRQENABLE set */
1442bc35c174SNicolas Souchu 	if (ctr & IRQENABLE) {
1443bc35c174SNicolas Souchu 		return;
1444bc35c174SNicolas Souchu 	}
1445bc35c174SNicolas Souchu 
14463ab971c1SNicolas Souchu 	/* interrupts are generated by nFault signal
14473ab971c1SNicolas Souchu 	 * only in ECP mode */
14483ab971c1SNicolas Souchu 	if ((str & nFAULT) && (ppc->ppc_mode & PPB_ECP)) {
14493ab971c1SNicolas Souchu 		/* check if ppc driver has programmed the
14503ab971c1SNicolas Souchu 		 * nFault interrupt */
1451bc35c174SNicolas Souchu 		if  (ppc->ppc_irqstat & PPC_IRQ_nFAULT) {
1452bc35c174SNicolas Souchu 
1453bc35c174SNicolas Souchu 			w_ecr(ppc, ecr | PPC_nFAULT_INTR);
1454bc35c174SNicolas Souchu 			ppc->ppc_irqstat &= ~PPC_IRQ_nFAULT;
1455bc35c174SNicolas Souchu 		} else {
14560f210c92SNicolas Souchu 			/* shall be handled by underlying layers XXX */
1457bc35c174SNicolas Souchu 			return;
1458bc35c174SNicolas Souchu 		}
1459bc35c174SNicolas Souchu 	}
1460bc35c174SNicolas Souchu 
1461bc35c174SNicolas Souchu 	if (ppc->ppc_irqstat & PPC_IRQ_DMA) {
1462bc35c174SNicolas Souchu 		/* disable interrupts (should be done by hardware though) */
1463bc35c174SNicolas Souchu 		w_ecr(ppc, ecr | PPC_SERVICE_INTR);
1464bc35c174SNicolas Souchu 		ppc->ppc_irqstat &= ~PPC_IRQ_DMA;
1465bc35c174SNicolas Souchu 		ecr = r_ecr(ppc);
1466bc35c174SNicolas Souchu 
1467bc35c174SNicolas Souchu 		/* check if DMA completed */
1468bc35c174SNicolas Souchu 		if ((ppc->ppc_avm & PPB_ECP) && (ecr & PPC_ENABLE_DMA)) {
1469bc35c174SNicolas Souchu #ifdef PPC_DEBUG
1470bc35c174SNicolas Souchu 			printf("a");
1471bc35c174SNicolas Souchu #endif
1472bc35c174SNicolas Souchu 			/* stop DMA */
1473bc35c174SNicolas Souchu 			w_ecr(ppc, ecr & ~PPC_ENABLE_DMA);
1474bc35c174SNicolas Souchu 			ecr = r_ecr(ppc);
1475bc35c174SNicolas Souchu 
1476bc35c174SNicolas Souchu 			if (ppc->ppc_dmastat == PPC_DMA_STARTED) {
1477bc35c174SNicolas Souchu #ifdef PPC_DEBUG
1478bc35c174SNicolas Souchu 				printf("d");
1479bc35c174SNicolas Souchu #endif
1480bc35c174SNicolas Souchu 				isa_dmadone(
1481bc35c174SNicolas Souchu 					ppc->ppc_dmaflags,
1482bc35c174SNicolas Souchu 					ppc->ppc_dmaddr,
1483bc35c174SNicolas Souchu 					ppc->ppc_dmacnt,
1484bc35c174SNicolas Souchu 					ppc->ppc_dmachan);
1485bc35c174SNicolas Souchu 
1486bc35c174SNicolas Souchu 				ppc->ppc_dmastat = PPC_DMA_COMPLETE;
1487bc35c174SNicolas Souchu 
1488bc35c174SNicolas Souchu 				/* wakeup the waiting process */
1489bc35c174SNicolas Souchu 				wakeup((caddr_t)ppc);
1490bc35c174SNicolas Souchu 			}
1491bc35c174SNicolas Souchu 		}
1492bc35c174SNicolas Souchu 	} else if (ppc->ppc_irqstat & PPC_IRQ_FIFO) {
1493bc35c174SNicolas Souchu 
1494bc35c174SNicolas Souchu 		/* classic interrupt I/O */
1495bc35c174SNicolas Souchu 		ppc->ppc_irqstat &= ~PPC_IRQ_FIFO;
1496bc35c174SNicolas Souchu 	}
1497bc35c174SNicolas Souchu 
1498bc35c174SNicolas Souchu 	return;
1499bc35c174SNicolas Souchu }
1500bc35c174SNicolas Souchu 
1501bc35c174SNicolas Souchu static int
15020f210c92SNicolas Souchu ppc_read(device_t dev, char *buf, int len, int mode)
1503bc35c174SNicolas Souchu {
1504bc35c174SNicolas Souchu 	return (EINVAL);
1505bc35c174SNicolas Souchu }
1506bc35c174SNicolas Souchu 
1507bc35c174SNicolas Souchu /*
1508bc35c174SNicolas Souchu  * Call this function if you want to send data in any advanced mode
1509bc35c174SNicolas Souchu  * of your parallel port: FIFO, DMA
1510bc35c174SNicolas Souchu  *
1511bc35c174SNicolas Souchu  * If what you want is not possible (no ECP, no DMA...),
1512bc35c174SNicolas Souchu  * EINVAL is returned
1513bc35c174SNicolas Souchu  */
1514bc35c174SNicolas Souchu static int
15150f210c92SNicolas Souchu ppc_write(device_t dev, char *buf, int len, int how)
1516bc35c174SNicolas Souchu {
15170f210c92SNicolas Souchu 	struct ppc_data *ppc = DEVTOSOFTC(dev);
1518bc35c174SNicolas Souchu 	char ecr, ecr_sav, ctr, ctr_sav;
1519bc35c174SNicolas Souchu 	int s, error = 0;
1520bc35c174SNicolas Souchu 	int spin;
1521bc35c174SNicolas Souchu 
1522bc35c174SNicolas Souchu #ifdef PPC_DEBUG
1523bc35c174SNicolas Souchu 	printf("w");
1524bc35c174SNicolas Souchu #endif
1525bc35c174SNicolas Souchu 
1526bc35c174SNicolas Souchu 	ecr_sav = r_ecr(ppc);
1527bc35c174SNicolas Souchu 	ctr_sav = r_ctr(ppc);
1528bc35c174SNicolas Souchu 
1529bc35c174SNicolas Souchu 	/*
1530bc35c174SNicolas Souchu 	 * Send buffer with DMA, FIFO and interrupts
1531bc35c174SNicolas Souchu 	 */
15320f210c92SNicolas Souchu 	if ((ppc->ppc_avm & PPB_ECP) && (ppc->ppc_registered)) {
1533bc35c174SNicolas Souchu 
1534bc35c174SNicolas Souchu 	    if (ppc->ppc_dmachan >= 0) {
1535bc35c174SNicolas Souchu 
1536bc35c174SNicolas Souchu 		/* byte mode, no intr, no DMA, dir=0, flush fifo
1537bc35c174SNicolas Souchu 		 */
1538bc35c174SNicolas Souchu 		ecr = PPC_ECR_STD | PPC_DISABLE_INTR;
1539bc35c174SNicolas Souchu 		w_ecr(ppc, ecr);
1540bc35c174SNicolas Souchu 
1541bc35c174SNicolas Souchu 		/* disable nAck interrupts */
1542bc35c174SNicolas Souchu 		ctr = r_ctr(ppc);
1543bc35c174SNicolas Souchu 		ctr &= ~IRQENABLE;
1544bc35c174SNicolas Souchu 		w_ctr(ppc, ctr);
1545bc35c174SNicolas Souchu 
1546bc35c174SNicolas Souchu 		ppc->ppc_dmaflags = 0;
1547bc35c174SNicolas Souchu 		ppc->ppc_dmaddr = (caddr_t)buf;
1548bc35c174SNicolas Souchu 		ppc->ppc_dmacnt = (u_int)len;
1549bc35c174SNicolas Souchu 
1550bc35c174SNicolas Souchu 		switch (ppc->ppc_mode) {
1551bc35c174SNicolas Souchu 		case PPB_COMPATIBLE:
1552bc35c174SNicolas Souchu 			/* compatible mode with FIFO, no intr, DMA, dir=0 */
1553bc35c174SNicolas Souchu 			ecr = PPC_ECR_FIFO | PPC_DISABLE_INTR | PPC_ENABLE_DMA;
1554bc35c174SNicolas Souchu 			break;
1555bc35c174SNicolas Souchu 		case PPB_ECP:
1556bc35c174SNicolas Souchu 			ecr = PPC_ECR_ECP | PPC_DISABLE_INTR | PPC_ENABLE_DMA;
1557bc35c174SNicolas Souchu 			break;
1558bc35c174SNicolas Souchu 		default:
1559bc35c174SNicolas Souchu 			error = EINVAL;
1560bc35c174SNicolas Souchu 			goto error;
1561bc35c174SNicolas Souchu 		}
1562bc35c174SNicolas Souchu 
1563bc35c174SNicolas Souchu 		w_ecr(ppc, ecr);
1564bc35c174SNicolas Souchu 		ecr = r_ecr(ppc);
1565bc35c174SNicolas Souchu 
1566bc35c174SNicolas Souchu 		/* enter splhigh() not to be preempted
1567bc35c174SNicolas Souchu 		 * by the dma interrupt, we may miss
1568bc35c174SNicolas Souchu 		 * the wakeup otherwise
1569bc35c174SNicolas Souchu 		 */
1570bc35c174SNicolas Souchu 		s = splhigh();
1571bc35c174SNicolas Souchu 
1572bc35c174SNicolas Souchu 		ppc->ppc_dmastat = PPC_DMA_INIT;
1573bc35c174SNicolas Souchu 
1574bc35c174SNicolas Souchu 		/* enable interrupts */
1575bc35c174SNicolas Souchu 		ecr &= ~PPC_SERVICE_INTR;
1576bc35c174SNicolas Souchu 		ppc->ppc_irqstat = PPC_IRQ_DMA;
1577bc35c174SNicolas Souchu 		w_ecr(ppc, ecr);
1578bc35c174SNicolas Souchu 
1579bc35c174SNicolas Souchu 		isa_dmastart(
1580bc35c174SNicolas Souchu 			ppc->ppc_dmaflags,
1581bc35c174SNicolas Souchu 			ppc->ppc_dmaddr,
1582bc35c174SNicolas Souchu 			ppc->ppc_dmacnt,
1583bc35c174SNicolas Souchu 			ppc->ppc_dmachan);
1584bc35c174SNicolas Souchu #ifdef PPC_DEBUG
1585bc35c174SNicolas Souchu 		printf("s%d", ppc->ppc_dmacnt);
1586bc35c174SNicolas Souchu #endif
1587bc35c174SNicolas Souchu 		ppc->ppc_dmastat = PPC_DMA_STARTED;
1588bc35c174SNicolas Souchu 
1589bc35c174SNicolas Souchu 		/* Wait for the DMA completed interrupt. We hope we won't
1590bc35c174SNicolas Souchu 		 * miss it, otherwise a signal will be necessary to unlock the
1591bc35c174SNicolas Souchu 		 * process.
1592bc35c174SNicolas Souchu 		 */
1593bc35c174SNicolas Souchu 		do {
1594bc35c174SNicolas Souchu 			/* release CPU */
1595bc35c174SNicolas Souchu 			error = tsleep((caddr_t)ppc,
1596bc35c174SNicolas Souchu 				PPBPRI | PCATCH, "ppcdma", 0);
1597bc35c174SNicolas Souchu 
1598bc35c174SNicolas Souchu 		} while (error == EWOULDBLOCK);
1599bc35c174SNicolas Souchu 
1600bc35c174SNicolas Souchu 		splx(s);
1601bc35c174SNicolas Souchu 
1602bc35c174SNicolas Souchu 		if (error) {
1603bc35c174SNicolas Souchu #ifdef PPC_DEBUG
1604bc35c174SNicolas Souchu 			printf("i");
1605bc35c174SNicolas Souchu #endif
1606bc35c174SNicolas Souchu 			/* stop DMA */
1607bc35c174SNicolas Souchu 			isa_dmadone(
1608bc35c174SNicolas Souchu 				ppc->ppc_dmaflags, ppc->ppc_dmaddr,
1609bc35c174SNicolas Souchu 				ppc->ppc_dmacnt, ppc->ppc_dmachan);
1610bc35c174SNicolas Souchu 
1611bc35c174SNicolas Souchu 			/* no dma, no interrupt, flush the fifo */
1612bc35c174SNicolas Souchu 			w_ecr(ppc, PPC_ECR_RESET);
1613bc35c174SNicolas Souchu 
1614bc35c174SNicolas Souchu 			ppc->ppc_dmastat = PPC_DMA_INTERRUPTED;
1615bc35c174SNicolas Souchu 			goto error;
1616bc35c174SNicolas Souchu 		}
1617bc35c174SNicolas Souchu 
1618bc35c174SNicolas Souchu 		/* wait for an empty fifo */
1619bc35c174SNicolas Souchu 		while (!(r_ecr(ppc) & PPC_FIFO_EMPTY)) {
1620bc35c174SNicolas Souchu 
1621bc35c174SNicolas Souchu 			for (spin=100; spin; spin--)
1622bc35c174SNicolas Souchu 				if (r_ecr(ppc) & PPC_FIFO_EMPTY)
1623bc35c174SNicolas Souchu 					goto fifo_empty;
1624bc35c174SNicolas Souchu #ifdef PPC_DEBUG
1625bc35c174SNicolas Souchu 			printf("Z");
1626bc35c174SNicolas Souchu #endif
1627bc35c174SNicolas Souchu 			error = tsleep((caddr_t)ppc, PPBPRI | PCATCH, "ppcfifo", hz/100);
1628bc35c174SNicolas Souchu 			if (error != EWOULDBLOCK) {
1629bc35c174SNicolas Souchu #ifdef PPC_DEBUG
1630bc35c174SNicolas Souchu 				printf("I");
1631bc35c174SNicolas Souchu #endif
1632bc35c174SNicolas Souchu 				/* no dma, no interrupt, flush the fifo */
1633bc35c174SNicolas Souchu 				w_ecr(ppc, PPC_ECR_RESET);
1634bc35c174SNicolas Souchu 
1635bc35c174SNicolas Souchu 				ppc->ppc_dmastat = PPC_DMA_INTERRUPTED;
1636bc35c174SNicolas Souchu 				error = EINTR;
1637bc35c174SNicolas Souchu 				goto error;
1638bc35c174SNicolas Souchu 			}
1639bc35c174SNicolas Souchu 		}
1640bc35c174SNicolas Souchu 
1641bc35c174SNicolas Souchu fifo_empty:
1642bc35c174SNicolas Souchu 		/* no dma, no interrupt, flush the fifo */
1643bc35c174SNicolas Souchu 		w_ecr(ppc, PPC_ECR_RESET);
1644bc35c174SNicolas Souchu 
1645bc35c174SNicolas Souchu 	    } else
1646bc35c174SNicolas Souchu 		error = EINVAL;			/* XXX we should FIFO and
1647bc35c174SNicolas Souchu 						 * interrupts */
1648bc35c174SNicolas Souchu 	} else
1649bc35c174SNicolas Souchu 		error = EINVAL;
1650bc35c174SNicolas Souchu 
1651bc35c174SNicolas Souchu error:
1652bc35c174SNicolas Souchu 
1653bc35c174SNicolas Souchu 	/* PDRQ must be kept unasserted until nPDACK is
1654bc35c174SNicolas Souchu 	 * deasserted for a minimum of 350ns (SMC datasheet)
1655bc35c174SNicolas Souchu 	 *
1656bc35c174SNicolas Souchu 	 * Consequence may be a FIFO that never empty
1657bc35c174SNicolas Souchu 	 */
1658bc35c174SNicolas Souchu 	DELAY(1);
1659bc35c174SNicolas Souchu 
1660bc35c174SNicolas Souchu 	w_ecr(ppc, ecr_sav);
1661bc35c174SNicolas Souchu 	w_ctr(ppc, ctr_sav);
1662bc35c174SNicolas Souchu 
1663bc35c174SNicolas Souchu 	return (error);
1664bc35c174SNicolas Souchu }
1665bc35c174SNicolas Souchu 
166667646539SMike Smith static void
16670f210c92SNicolas Souchu ppc_reset_epp(device_t dev)
166867646539SMike Smith {
16690f210c92SNicolas Souchu 	struct ppc_data *ppc = DEVTOSOFTC(dev);
167067646539SMike Smith 
16710f210c92SNicolas Souchu 	ppc_reset_epp_timeout(ppc);
167267646539SMike Smith 
167367646539SMike Smith 	return;
167467646539SMike Smith }
167567646539SMike Smith 
167667646539SMike Smith static int
16770f210c92SNicolas Souchu ppc_setmode(device_t dev, int mode)
16780f210c92SNicolas Souchu {
16790f210c92SNicolas Souchu 	struct ppc_data *ppc = DEVTOSOFTC(dev);
16800f210c92SNicolas Souchu 
16810f210c92SNicolas Souchu 	switch (ppc->ppc_type) {
16820f210c92SNicolas Souchu 	case PPC_TYPE_SMCLIKE:
16830f210c92SNicolas Souchu 		return (ppc_smclike_setmode(ppc, mode));
16840f210c92SNicolas Souchu 		break;
16850f210c92SNicolas Souchu 
16860f210c92SNicolas Souchu 	case PPC_TYPE_GENERIC:
16870f210c92SNicolas Souchu 	default:
16880f210c92SNicolas Souchu 		return (ppc_generic_setmode(ppc, mode));
16890f210c92SNicolas Souchu 		break;
16900f210c92SNicolas Souchu 	}
16910f210c92SNicolas Souchu 
16920f210c92SNicolas Souchu 	/* not reached */
16930f210c92SNicolas Souchu 	return (ENXIO);
16940f210c92SNicolas Souchu }
16950f210c92SNicolas Souchu 
16960f210c92SNicolas Souchu static int
16970f210c92SNicolas Souchu ppc_probe(device_t dev)
169867646539SMike Smith {
169967646539SMike Smith 	static short next_bios_ppc = 0;
170067646539SMike Smith 	struct ppc_data *ppc;
17010f210c92SNicolas Souchu 	device_t parent;
17020f210c92SNicolas Souchu 	int port;
170367646539SMike Smith 
17040f210c92SNicolas Souchu 	device_set_desc(dev, "Parallel port");
170567646539SMike Smith 
17060f210c92SNicolas Souchu 	/* XXX shall be connected to pnpbios - from Peter Wemm */
17070f210c92SNicolas Souchu 	if (isa_get_logicalid(dev))
17080f210c92SNicolas Souchu                 return ENXIO;
17090f210c92SNicolas Souchu 
17100f210c92SNicolas Souchu 	parent = device_get_parent(dev);
171167646539SMike Smith 
171267646539SMike Smith 	/*
171367646539SMike Smith 	 * Allocate the ppc_data structure.
171467646539SMike Smith 	 */
17150f210c92SNicolas Souchu 	ppc = DEVTOSOFTC(dev);
171667646539SMike Smith 	bzero(ppc, sizeof(struct ppc_data));
171767646539SMike Smith 
17180f210c92SNicolas Souchu 	ppc->rid_irq = ppc->rid_drq = ppc->rid_ioport = 0;
17190f210c92SNicolas Souchu 	ppc->res_irq = ppc->res_drq = ppc->res_ioport = 0;
172067646539SMike Smith 
17210f210c92SNicolas Souchu 	/* retrieve ISA parameters */
17220f210c92SNicolas Souchu 	BUS_READ_IVAR(parent, dev, ISA_IVAR_PORT, &port);
17230f210c92SNicolas Souchu 
17240f210c92SNicolas Souchu 	/*
17250f210c92SNicolas Souchu 	 * If port not specified, use bios list.
17260f210c92SNicolas Souchu 	 */
17270f210c92SNicolas Souchu 	if (port < 0) {
17280f210c92SNicolas Souchu 		if((next_bios_ppc < BIOS_MAX_PPC) &&
17290f210c92SNicolas Souchu 				(*(BIOS_PORTS+next_bios_ppc) != 0) ) {
17300f210c92SNicolas Souchu 			port = *(BIOS_PORTS+next_bios_ppc++);
17310f210c92SNicolas Souchu 			if (bootverbose)
17320f210c92SNicolas Souchu 			  device_printf(dev, "parallel port found at 0x%x\n",
17330f210c92SNicolas Souchu 					port);
17340f210c92SNicolas Souchu 		} else {
17350f210c92SNicolas Souchu 			device_printf(dev, "parallel port not found.\n");
17360f210c92SNicolas Souchu 			return ENXIO;
17370f210c92SNicolas Souchu 		}
17380f210c92SNicolas Souchu 	}
17390f210c92SNicolas Souchu 	ppc->ppc_base = port;
17400f210c92SNicolas Souchu 
17410f210c92SNicolas Souchu 	/* IO port is mandatory */
17420f210c92SNicolas Souchu 	ppc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
17430f210c92SNicolas Souchu 					     &ppc->rid_ioport, port, port,
17440f210c92SNicolas Souchu 					     IO_LPTSIZE, RF_ACTIVE);
17450f210c92SNicolas Souchu 	if (ppc->res_ioport == 0) {
17460f210c92SNicolas Souchu 		device_printf(dev, "cannot reserve I/O port range\n");
17470f210c92SNicolas Souchu 		goto error;
17480f210c92SNicolas Souchu 	}
17490f210c92SNicolas Souchu 
17500f210c92SNicolas Souchu 	ppc->ppc_flags = device_get_flags(dev);
17510f210c92SNicolas Souchu 
17520f210c92SNicolas Souchu 	if (!(ppc->ppc_flags & 0x20)) {
17530f210c92SNicolas Souchu 		ppc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &ppc->rid_irq,
17540f210c92SNicolas Souchu 						  0ul, ~0ul, 1, RF_SHAREABLE);
17550f210c92SNicolas Souchu 		ppc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ, &ppc->rid_drq,
17560f210c92SNicolas Souchu 						  0ul, ~0ul, 1, RF_ACTIVE);
17570f210c92SNicolas Souchu 	}
17580f210c92SNicolas Souchu 
17590f210c92SNicolas Souchu 	if (ppc->res_irq)
17600f210c92SNicolas Souchu 		BUS_READ_IVAR(parent, dev, ISA_IVAR_IRQ, &ppc->ppc_irq);
17610f210c92SNicolas Souchu 	if (ppc->res_drq)
17620f210c92SNicolas Souchu 		BUS_READ_IVAR(parent, dev, ISA_IVAR_DRQ, &ppc->ppc_dmachan);
17630f210c92SNicolas Souchu 
17640f210c92SNicolas Souchu 	ppc->ppc_unit = device_get_unit(dev);
17650f210c92SNicolas Souchu 	ppc->ppc_model = GENERIC;
1766af548787SNicolas Souchu 
176746f3ff79SMike Smith 	ppc->ppc_mode = PPB_COMPATIBLE;
17680f210c92SNicolas Souchu 	ppc->ppc_epp = (ppc->ppc_flags & 0x10) >> 4;
176967646539SMike Smith 
17700f210c92SNicolas Souchu 	ppc->ppc_type = PPC_TYPE_GENERIC;
1771edcfcf27SNicolas Souchu 
1772edcfcf27SNicolas Souchu 	/*
1773dc733423SDag-Erling Smørgrav 	 * Try to detect the chipset and its mode.
177467646539SMike Smith 	 */
17750f210c92SNicolas Souchu 	if (ppc_detect(ppc, ppc->ppc_flags & 0xf))
177667646539SMike Smith 		goto error;
177767646539SMike Smith 
17780f210c92SNicolas Souchu 	return (0);
177967646539SMike Smith 
178067646539SMike Smith error:
17810f210c92SNicolas Souchu 	if (ppc->res_irq != 0) {
17820f210c92SNicolas Souchu 		bus_release_resource(dev, SYS_RES_IRQ, ppc->rid_irq,
17830f210c92SNicolas Souchu 				     ppc->res_irq);
17840f210c92SNicolas Souchu 	}
17850f210c92SNicolas Souchu 	if (ppc->res_ioport != 0) {
17860f210c92SNicolas Souchu 		bus_deactivate_resource(dev, SYS_RES_IOPORT, ppc->rid_ioport,
17870f210c92SNicolas Souchu 					ppc->res_ioport);
17880f210c92SNicolas Souchu 		bus_release_resource(dev, SYS_RES_IOPORT, ppc->rid_ioport,
17890f210c92SNicolas Souchu 				     ppc->res_ioport);
17900f210c92SNicolas Souchu 	}
17910f210c92SNicolas Souchu 	if (ppc->res_drq != 0) {
17920f210c92SNicolas Souchu 		bus_deactivate_resource(dev, SYS_RES_DRQ, ppc->rid_drq,
17930f210c92SNicolas Souchu 					ppc->res_drq);
17940f210c92SNicolas Souchu 		bus_release_resource(dev, SYS_RES_DRQ, ppc->rid_drq,
17950f210c92SNicolas Souchu 				     ppc->res_drq);
17960f210c92SNicolas Souchu 	}
17970f210c92SNicolas Souchu 	return (ENXIO);
179867646539SMike Smith }
179967646539SMike Smith 
180067646539SMike Smith static int
18010f210c92SNicolas Souchu ppc_attach(device_t dev)
180267646539SMike Smith {
18030f210c92SNicolas Souchu 	struct ppc_data *ppc = DEVTOSOFTC(dev);
180467646539SMike Smith 
18050f210c92SNicolas Souchu 	device_t ppbus;
18060f210c92SNicolas Souchu 	device_t parent = device_get_parent(dev);
18070f210c92SNicolas Souchu 
18080f210c92SNicolas Souchu 	device_printf(dev, "%s chipset (%s) in %s mode%s\n",
18090f210c92SNicolas Souchu 		      ppc_models[ppc->ppc_model], ppc_avms[ppc->ppc_avm],
181046f3ff79SMike Smith 		      ppc_modes[ppc->ppc_mode], (PPB_IS_EPP(ppc->ppc_mode)) ?
181167646539SMike Smith 		      ppc_epp_protocol[ppc->ppc_epp] : "");
181267646539SMike Smith 
1813bc35c174SNicolas Souchu 	if (ppc->ppc_fifo)
18140f210c92SNicolas Souchu 		device_printf(dev, "FIFO with %d/%d/%d bytes threshold\n",
18150f210c92SNicolas Souchu 			      ppc->ppc_fifo, ppc->ppc_wthr, ppc->ppc_rthr);
181667646539SMike Smith 
1817bc35c174SNicolas Souchu 	if ((ppc->ppc_avm & PPB_ECP) && (ppc->ppc_dmachan > 0)) {
18180f210c92SNicolas Souchu 		/* acquire the DMA channel forever */	/* XXX */
1819bc35c174SNicolas Souchu 		isa_dma_acquire(ppc->ppc_dmachan);
1820bc35c174SNicolas Souchu 		isa_dmainit(ppc->ppc_dmachan, 1024); /* nlpt.BUFSIZE */
1821bc35c174SNicolas Souchu 	}
1822bc35c174SNicolas Souchu 
18230f210c92SNicolas Souchu 	/* add ppbus as a child of this isa to parallel bridge */
18240f210c92SNicolas Souchu 	ppbus = device_add_child(dev, "ppbus", -1);
18250f210c92SNicolas Souchu 
182667646539SMike Smith 	/*
182767646539SMike Smith 	 * Probe the ppbus and attach devices found.
182867646539SMike Smith 	 */
18290f210c92SNicolas Souchu 	device_probe_and_attach(ppbus);
183067646539SMike Smith 
18310f210c92SNicolas Souchu 	/* register the ppc interrupt handler as default */
18320f210c92SNicolas Souchu 	if (ppc->res_irq) {
18330f210c92SNicolas Souchu 		/* default to the tty mask for registration */	/* XXX */
18340f210c92SNicolas Souchu 		if (BUS_SETUP_INTR(parent, dev, ppc->res_irq, INTR_TYPE_TTY,
18350f210c92SNicolas Souchu 					    ppcintr, dev, &ppc->intr_cookie) == 0) {
18360f210c92SNicolas Souchu 
18370f210c92SNicolas Souchu 			/* remember the ppcintr is registered */
18380f210c92SNicolas Souchu 			ppc->ppc_registered = 1;
183967646539SMike Smith 		}
18400f210c92SNicolas Souchu 	}
18410f210c92SNicolas Souchu 
18420f210c92SNicolas Souchu 	return (0);
18430f210c92SNicolas Souchu }
18440f210c92SNicolas Souchu 
18450f210c92SNicolas Souchu static u_char
18460f210c92SNicolas Souchu ppc_io(device_t ppcdev, int iop, u_char *addr, int cnt, u_char byte)
18470f210c92SNicolas Souchu {
18480f210c92SNicolas Souchu 	struct ppc_data *ppc = DEVTOSOFTC(ppcdev);
18490f210c92SNicolas Souchu 	switch (iop) {
18500f210c92SNicolas Souchu 	case PPB_OUTSB_EPP:
18510f210c92SNicolas Souchu 		outsb(ppc->ppc_base + PPC_EPP_DATA, addr, cnt);
18520f210c92SNicolas Souchu 		break;
18530f210c92SNicolas Souchu 	case PPB_OUTSW_EPP:
18540f210c92SNicolas Souchu 		outsw(ppc->ppc_base + PPC_EPP_DATA, addr, cnt);
18550f210c92SNicolas Souchu 		break;
18560f210c92SNicolas Souchu 	case PPB_OUTSL_EPP:
18570f210c92SNicolas Souchu 		outsl(ppc->ppc_base + PPC_EPP_DATA, addr, cnt);
18580f210c92SNicolas Souchu 		break;
18590f210c92SNicolas Souchu 	case PPB_INSB_EPP:
18600f210c92SNicolas Souchu 		insb(ppc->ppc_base + PPC_EPP_DATA, addr, cnt);
18610f210c92SNicolas Souchu 		break;
18620f210c92SNicolas Souchu 	case PPB_INSW_EPP:
18630f210c92SNicolas Souchu 		insw(ppc->ppc_base + PPC_EPP_DATA, addr, cnt);
18640f210c92SNicolas Souchu 		break;
18650f210c92SNicolas Souchu 	case PPB_INSL_EPP:
18660f210c92SNicolas Souchu 		insl(ppc->ppc_base + PPC_EPP_DATA, addr, cnt);
18670f210c92SNicolas Souchu 		break;
18680f210c92SNicolas Souchu 	case PPB_RDTR:
18690f210c92SNicolas Souchu 		return (r_dtr(ppc));
18700f210c92SNicolas Souchu 		break;
18710f210c92SNicolas Souchu 	case PPB_RSTR:
18720f210c92SNicolas Souchu 		return (r_str(ppc));
18730f210c92SNicolas Souchu 		break;
18740f210c92SNicolas Souchu 	case PPB_RCTR:
18750f210c92SNicolas Souchu 		return (r_ctr(ppc));
18760f210c92SNicolas Souchu 		break;
18770f210c92SNicolas Souchu 	case PPB_REPP_A:
18780f210c92SNicolas Souchu 		return (r_epp_A(ppc));
18790f210c92SNicolas Souchu 		break;
18800f210c92SNicolas Souchu 	case PPB_REPP_D:
18810f210c92SNicolas Souchu 		return (r_epp_D(ppc));
18820f210c92SNicolas Souchu 		break;
18830f210c92SNicolas Souchu 	case PPB_RECR:
18840f210c92SNicolas Souchu 		return (r_ecr(ppc));
18850f210c92SNicolas Souchu 		break;
18860f210c92SNicolas Souchu 	case PPB_RFIFO:
18870f210c92SNicolas Souchu 		return (r_fifo(ppc));
18880f210c92SNicolas Souchu 		break;
18890f210c92SNicolas Souchu 	case PPB_WDTR:
18900f210c92SNicolas Souchu 		w_dtr(ppc, byte);
18910f210c92SNicolas Souchu 		break;
18920f210c92SNicolas Souchu 	case PPB_WSTR:
18930f210c92SNicolas Souchu 		w_str(ppc, byte);
18940f210c92SNicolas Souchu 		break;
18950f210c92SNicolas Souchu 	case PPB_WCTR:
18960f210c92SNicolas Souchu 		w_ctr(ppc, byte);
18970f210c92SNicolas Souchu 		break;
18980f210c92SNicolas Souchu 	case PPB_WEPP_A:
18990f210c92SNicolas Souchu 		w_epp_A(ppc, byte);
19000f210c92SNicolas Souchu 		break;
19010f210c92SNicolas Souchu 	case PPB_WEPP_D:
19020f210c92SNicolas Souchu 		w_epp_D(ppc, byte);
19030f210c92SNicolas Souchu 		break;
19040f210c92SNicolas Souchu 	case PPB_WECR:
19050f210c92SNicolas Souchu 		w_ecr(ppc, byte);
19060f210c92SNicolas Souchu 		break;
19070f210c92SNicolas Souchu 	case PPB_WFIFO:
19080f210c92SNicolas Souchu 		w_fifo(ppc, byte);
19090f210c92SNicolas Souchu 		break;
19100f210c92SNicolas Souchu 	default:
19110f210c92SNicolas Souchu 		panic("%s: unknown I/O operation", __FUNCTION__);
19120f210c92SNicolas Souchu 		break;
19130f210c92SNicolas Souchu 	}
19140f210c92SNicolas Souchu 
19150f210c92SNicolas Souchu 	return (0);	/* not significative */
19160f210c92SNicolas Souchu }
19170f210c92SNicolas Souchu 
19180f210c92SNicolas Souchu static int
19190f210c92SNicolas Souchu ppc_read_ivar(device_t bus, device_t dev, int index, uintptr_t *val)
19200f210c92SNicolas Souchu {
19210f210c92SNicolas Souchu 	struct ppc_data *ppc = (struct ppc_data *)device_get_softc(bus);
19220f210c92SNicolas Souchu 
19230f210c92SNicolas Souchu 	switch (index) {
19240f210c92SNicolas Souchu 	case PPC_IVAR_EPP_PROTO:
19250f210c92SNicolas Souchu 		*val = (u_long)ppc->ppc_epp;
19260f210c92SNicolas Souchu 		break;
19270f210c92SNicolas Souchu 	case PPC_IVAR_IRQ:
19280f210c92SNicolas Souchu 		BUS_READ_IVAR(device_get_parent(bus), bus, ISA_IVAR_IRQ, val);
19290f210c92SNicolas Souchu 		break;
19300f210c92SNicolas Souchu 	default:
19310f210c92SNicolas Souchu 		return (ENOENT);
19320f210c92SNicolas Souchu 	}
19330f210c92SNicolas Souchu 
19340f210c92SNicolas Souchu 	return (0);
19350f210c92SNicolas Souchu }
19360f210c92SNicolas Souchu 
19370f210c92SNicolas Souchu /*
19380f210c92SNicolas Souchu  * Resource is useless here since ppbus devices' interrupt handlers are
19390f210c92SNicolas Souchu  * multiplexed to the same resource initially allocated by ppc
19400f210c92SNicolas Souchu  */
19410f210c92SNicolas Souchu static int
19420f210c92SNicolas Souchu ppc_setup_intr(device_t bus, device_t child, struct resource *r, int flags,
19430f210c92SNicolas Souchu 			void (*ihand)(void *), void *arg, void **cookiep)
19440f210c92SNicolas Souchu {
19450f210c92SNicolas Souchu 	int error;
19460f210c92SNicolas Souchu 	struct ppc_data *ppc = DEVTOSOFTC(bus);
19470f210c92SNicolas Souchu 
19480f210c92SNicolas Souchu 	if (ppc->ppc_registered) {
19490f210c92SNicolas Souchu 		/* XXX refuse registration if DMA is in progress */
19500f210c92SNicolas Souchu 
19510f210c92SNicolas Souchu 		/* first, unregister the default interrupt handler */
19520f210c92SNicolas Souchu 		if ((error = BUS_TEARDOWN_INTR(device_get_parent(bus),
19530f210c92SNicolas Souchu 				bus, ppc->res_irq, ppc->intr_cookie)))
19540f210c92SNicolas Souchu 			return (error);
19550f210c92SNicolas Souchu 
19560f210c92SNicolas Souchu /* 		bus_deactivate_resource(bus, SYS_RES_IRQ, ppc->rid_irq, */
19570f210c92SNicolas Souchu /* 					ppc->res_irq); */
19580f210c92SNicolas Souchu 
19590f210c92SNicolas Souchu 		/* DMA/FIFO operation won't be possible anymore */
19600f210c92SNicolas Souchu 		ppc->ppc_registered = 0;
19610f210c92SNicolas Souchu 	}
19620f210c92SNicolas Souchu 
19630f210c92SNicolas Souchu 	/* pass registration to the upper layer, ignore the incoming resource */
19640f210c92SNicolas Souchu 	return (BUS_SETUP_INTR(device_get_parent(bus), child,
19650f210c92SNicolas Souchu 			       r, flags, ihand, arg, cookiep));
19660f210c92SNicolas Souchu }
19670f210c92SNicolas Souchu 
19680f210c92SNicolas Souchu /*
19690f210c92SNicolas Souchu  * When no underlying device has a registered interrupt, register the ppc
19700f210c92SNicolas Souchu  * layer one
19710f210c92SNicolas Souchu  */
19720f210c92SNicolas Souchu static int
19730f210c92SNicolas Souchu ppc_teardown_intr(device_t bus, device_t child, struct resource *r, void *ih)
19740f210c92SNicolas Souchu {
19750f210c92SNicolas Souchu 	int error;
19760f210c92SNicolas Souchu 	struct ppc_data *ppc = DEVTOSOFTC(bus);
19770f210c92SNicolas Souchu 	device_t parent = device_get_parent(bus);
19780f210c92SNicolas Souchu 
19790f210c92SNicolas Souchu 	/* pass unregistration to the upper layer */
19800f210c92SNicolas Souchu 	if ((error = BUS_TEARDOWN_INTR(parent, child, r, ih)))
19810f210c92SNicolas Souchu 		return (error);
19820f210c92SNicolas Souchu 
19830f210c92SNicolas Souchu 	/* default to the tty mask for registration */		/* XXX */
19840f210c92SNicolas Souchu 	if (ppc->ppc_irq &&
19850f210c92SNicolas Souchu 		!(error = BUS_SETUP_INTR(parent, bus, ppc->res_irq,
19860f210c92SNicolas Souchu 			INTR_TYPE_TTY, ppcintr, bus, &ppc->intr_cookie))) {
19870f210c92SNicolas Souchu 
19880f210c92SNicolas Souchu 		/* remember the ppcintr is registered */
19890f210c92SNicolas Souchu 		ppc->ppc_registered = 1;
19900f210c92SNicolas Souchu 	}
19910f210c92SNicolas Souchu 
19920f210c92SNicolas Souchu 	return (error);
19930f210c92SNicolas Souchu }
19940f210c92SNicolas Souchu 
19950f210c92SNicolas Souchu DRIVER_MODULE(ppc, isa, ppc_driver, ppc_devclass, 0, 0);
199667646539SMike Smith #endif
1997