atppc_isa.c revision 1.2 1 /* $NetBSD: atppc_isa.c,v 1.2 2004/01/21 00:33:37 bjh21 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 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/device.h>
39
40 #include <machine/intr.h>
41 #include <machine/bus.h>
42
43 #include <dev/isa/isareg.h>
44 #include <dev/isa/isavar.h>
45 #include <dev/isa/isadmavar.h>
46 #include <dev/isa/atppc_isa_subr.h>
47
48 #include <dev/ic/atppcreg.h>
49 #include <dev/ic/atppcvar.h>
50
51 /*
52 * ISA bus attach code for atppc driver.
53 * Note on capabilities: capabilites may exist in the chipset but may not
54 * necessarily be useable. I.e. you may specify an IRQ in the autoconfig, but
55 * will the port actually have an IRQ assigned to it at the hardware level?
56 * How can you test if the capabilites can be used? For interrupts, see if a
57 * handler exists (sc_intr != NULL). For DMA, see if the sc_dma_start() and
58 * sc_dma_finish() function pointers are not NULL.
59 */
60
61 /* Probe, attach, and detach functions for a atppc device on the ISA bus. */
62 static int atppc_isa_probe __P((struct device *, struct cfdata *, void *));
63 static void atppc_isa_attach __P((struct device *, struct device *, void *));
64 static int atppc_isa_detach __P((struct device *, int));
65
66 CFATTACH_DECL(atppc_isa, sizeof(struct atppc_isa_softc), atppc_isa_probe,
67 atppc_isa_attach, atppc_isa_detach, NULL);
68
69 /*
70 * Probe function: find parallel port controller on isa bus. Combined from
71 * lpt_isa_probe() in lpt.c and atppc_detect_port() from FreeBSD's ppc.c.
72 */
73 static int
74 atppc_isa_probe(struct device * parent, struct cfdata * cf, void * aux)
75 {
76 bus_space_handle_t ioh;
77 struct isa_attach_args * ia = aux;
78 bus_space_tag_t iot = ia->ia_iot;
79 int addr = ia->ia_io->ir_addr;
80 int rval = 0;
81
82 if(ia->ia_nio < 1 || addr == ISACF_PORT_DEFAULT) {
83 printf("%s(%s): io port unknown.\n", __func__,
84 parent->dv_xname);
85 }
86 else if(bus_space_map(iot, addr, IO_LPTSIZE, 0, &ioh) == 0) {
87 if (atppc_detect_port(iot, ioh) == 0)
88 rval = 1;
89 else
90 printf("%s(%s): unable to write/read I/O port.\n",
91 __func__, parent->dv_xname);
92 bus_space_unmap(iot, ioh, IO_LPTSIZE);
93 }
94 else {
95 printf("%s(%s): attempt to map bus space failed.\n", __func__,
96 parent->dv_xname);
97 }
98
99 if(rval) {
100 ia->ia_nio = 1;
101 ia->ia_io[0].ir_size = IO_LPTSIZE;
102 ia->ia_nirq = 1;
103 ia->ia_ndrq = 1;
104 ia->ia_niomem = 0;
105 }
106
107 return rval;
108 }
109
110 /* Attach function: attach and configure parallel port controller on isa bus. */
111 static void
112 atppc_isa_attach(struct device * parent, struct device * self, void * aux)
113 {
114 struct atppc_isa_softc * sc = (struct atppc_isa_softc *)self;
115 struct atppc_softc * lsc = (struct atppc_softc *)self;
116 struct isa_attach_args * ia = aux;
117
118 lsc->sc_iot = ia->ia_iot;
119 lsc->sc_dmat = ia->ia_dmat;
120 lsc->sc_has = 0;
121 sc->sc_ic = ia->ia_ic;
122 sc->sc_iobase = ia->ia_io->ir_addr;
123
124 if(bus_space_map(lsc->sc_iot, sc->sc_iobase, IO_LPTSIZE, 0,
125 &lsc->sc_ioh) != 0) {
126 printf("%s: attempt to map bus space failed, device not "
127 "properly attached.\n", self->dv_xname);
128 lsc->sc_dev_ok = ATPPC_NOATTACH;
129 return;
130 }
131 else {
132 lsc->sc_dev_ok = ATPPC_ATTACHED;
133 }
134
135 /* Assign interrupt handler */
136 if(!(self->dv_cfdata->cf_flags & ATPPC_FLAG_DISABLE_INTR)) {
137 if((ia->ia_irq->ir_irq != ISACF_IRQ_DEFAULT) &&
138 (ia->ia_nirq > 0)) {
139
140 sc->sc_irq = ia->ia_irq->ir_irq;
141 /* Establish interrupt handler. */
142 if(!(atppc_isa_intr_establish(lsc))) {
143 lsc->sc_has |= ATPPC_HAS_INTR;
144 }
145 }
146 else {
147 ATPPC_DPRINTF(("%s: IRQ not assigned or bad number of "
148 "IRQs.\n", self->dv_xname));
149 }
150 }
151 else {
152 ATPPC_VPRINTF(("%s: interrupts not configured due to flags.\n",
153 self->dv_xname));
154 }
155
156 /* Configure DMA */
157 if(!(self->dv_cfdata->cf_flags & ATPPC_FLAG_DISABLE_DMA)) {
158 if((ia->ia_drq->ir_drq != ISACF_DRQ_DEFAULT) &&
159 (ia->ia_ndrq > 0)) {
160
161 sc->sc_drq = ia->ia_drq->ir_drq;
162 if(!(atppc_isa_dma_setup(lsc))) {
163 lsc->sc_has |= ATPPC_HAS_DMA;
164 }
165 }
166 else {
167 ATPPC_DPRINTF(("%s: DRQ not assigned or bad number of "
168 "DRQs.\n", self->dv_xname));
169 }
170 }
171 else {
172 ATPPC_VPRINTF(("%s: dma not configured due to flags.\n",
173 self->dv_xname));
174 }
175
176 /* Run soft configuration attach */
177 atppc_sc_attach(lsc);
178
179 return;
180 }
181
182 /* Detach function: used to detach atppc driver at run time. */
183 static int
184 atppc_isa_detach(struct device * dev, int flag)
185 {
186 struct atppc_softc * lsc = (struct atppc_softc *) dev;
187 int rval;
188
189 if(lsc->sc_dev_ok == ATPPC_ATTACHED) {
190 /* Run soft configuration detach first */
191 rval = atppc_sc_detach(lsc, flag);
192
193 /* Disable DMA */
194 atppc_isa_dma_remove(lsc);
195
196 /* Disestablish interrupt handler */
197 atppc_isa_intr_disestablish(lsc);
198
199 /* Unmap bus space */
200 bus_space_unmap(lsc->sc_iot, lsc->sc_ioh, IO_LPTSIZE);
201
202 /* Mark config data */
203 lsc->sc_dev_ok = ATPPC_NOATTACH;
204 }
205 else {
206 rval = 0;
207 if(!(flag & DETACH_QUIET)) {
208 printf("%s not properly attached! Detach routines "
209 "skipped.\n", dev->dv_xname);
210 }
211 }
212
213 if(!(flag & DETACH_QUIET)) {
214 printf("%s: detached.", dev->dv_xname);
215 }
216
217 return rval;
218 }
219