xref: /netbsd/sys/arch/acorn32/mainbus/atppc_pioc.c (revision 6550d01e)
1 /* $NetBSD: atppc_pioc.c,v 1.5 2009/05/12 07:06:53 cegger Exp $ */
2 
3 /*-
4  * Copyright (c) 2001 Alcove - Nicolas Souchu
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * FreeBSD: src/sys/isa/ppc.c,v 1.26.2.5 2001/10/02 05:21:45 nsouch Exp
29  *
30  */
31 
32 #include "opt_atppc.h"
33 
34 #include <sys/param.h>
35 __KERNEL_RCSID(0, "$NetBSD: atppc_pioc.c,v 1.5 2009/05/12 07:06:53 cegger Exp $");
36 
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/device.h>
41 
42 #include <machine/intr.h>
43 #include <machine/bus.h>
44 
45 #include <arch/acorn32/mainbus/piocvar.h>
46 
47 #include <dev/ic/atppcreg.h>
48 #include <dev/ic/atppcvar.h>
49 
50 #include "locators.h"
51 
52 /* Probe and attach functions for a atppc device on the PIOC. */
53 static int atppc_pioc_probe(device_t, cfdata_t, void *);
54 static void atppc_pioc_attach(device_t, device_t, void *);
55 
56 CFATTACH_DECL_NEW(atppc_pioc, sizeof(struct atppc_softc), atppc_pioc_probe,
57 	atppc_pioc_attach, NULL, NULL);
58 
59 #define IO_LPTSIZE 8
60 
61 /*
62  * Probe function: find parallel port controller on isa bus. Combined from
63  * lpt_isa_probe() in lpt.c and atppc_detect_port() from FreeBSD's ppc.c.
64  */
65 static int
66 atppc_pioc_probe(device_t parent, cfdata_t cf, void *aux)
67 {
68 	bus_space_handle_t ioh;
69 	struct pioc_attach_args *pa = aux;
70 	bus_space_tag_t iot = pa->pa_iot;
71 	int addr = pa->pa_iobase + pa->pa_offset;
72 	int rval = 0;
73 
74 	if (pa->pa_name && strcmp(pa->pa_name, "lpt") != 0)
75 		return 0;
76 
77 	if (pa->pa_offset == PIOCCF_OFFSET_DEFAULT) {
78 		aprint_error_dev(parent, "(%s): io port unknown.\n", __func__);
79 	} else if (bus_space_map(iot, addr, IO_LPTSIZE, 0, &ioh) == 0) {
80 		if (atppc_detect_port(iot, ioh) == 0)
81 			rval = 1;
82 		else
83 			aprint_error_dev(parent,
84 			    "(%s): unable to write/read I/O port.\n",
85 			    __func__);
86 		bus_space_unmap(iot, ioh, IO_LPTSIZE);
87 	} else {
88 		aprint_error_dev(parent, "(%s): attempt to map bus space failed.\n",
89 		    __func__);
90 	}
91 
92 	return rval;
93 }
94 
95 /* Attach function: attach and configure parallel port controller on isa bus. */
96 static void
97 atppc_pioc_attach(device_t parent, device_t self, void *aux)
98 {
99 	struct atppc_softc *sc = device_private(self);
100 	struct pioc_attach_args *pa = aux;
101 	bus_addr_t iobase;
102 
103 	sc->sc_dev = self;
104 	sc->sc_iot = pa->pa_iot;
105 	sc->sc_has = 0;
106 	iobase = pa->pa_iobase + pa->pa_offset;
107 
108 	if (bus_space_map(sc->sc_iot, iobase, IO_LPTSIZE, 0,
109 		&sc->sc_ioh) != 0) {
110 		aprint_error(": attempt to map bus space failed, device not "
111 			"properly attached.\n");
112 		sc->sc_dev_ok = ATPPC_NOATTACH;
113 		return;
114 	}
115 	sc->sc_dev_ok = ATPPC_ATTACHED;
116 
117 	/* Assign interrupt handler */
118 	if (!(device_cfdata(self)->cf_flags & ATPPC_FLAG_DISABLE_INTR)) {
119 		if (pa->pa_irq != MAINBUSCF_IRQ_DEFAULT) {
120 			/* Establish interrupt handler. */
121 			sc->sc_ieh = intr_claim(pa->pa_irq, IPL_TTY, "atppc",
122 			    atppcintr, sc);
123 			sc->sc_has |= ATPPC_HAS_INTR;
124 		} else
125 			ATPPC_DPRINTF(("%s: IRQ not assigned or bad number of "
126 				"IRQs.\n", device_xname(self)));
127 	} else
128 		ATPPC_VPRINTF(("%s: interrupts not configured due to flags.\n",
129 			device_xname(self)));
130 
131 	/* Run soft configuration attach */
132 	atppc_sc_attach(sc);
133 }
134