atppc_isa.c revision 1.1 1 /*-
2 * Copyright (c) 2001 Alcove - Nicolas Souchu
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: src/sys/isa/ppc.c,v 1.26.2.5 2001/10/02 05:21:45 nsouch Exp $
27 *
28 */
29
30 #include "opt_atppc.h"
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/device.h>
37
38 #include <machine/intr.h>
39 #include <machine/bus.h>
40
41 #include <dev/isa/isareg.h>
42 #include <dev/isa/isavar.h>
43 #include <dev/isa/isadmavar.h>
44 #include <dev/isa/atppc_isa_subr.h>
45
46 #include <dev/ic/atppcreg.h>
47 #include <dev/ic/atppcvar.h>
48
49 /*
50 * ISA bus attach code for atppc driver.
51 * Note on capabilities: capabilites may exist in the chipset but may not
52 * necessarily be useable. I.e. you may specify an IRQ in the autoconfig, but
53 * will the port actually have an IRQ assigned to it at the hardware level?
54 * How can you test if the capabilites can be used? For interrupts, see if a
55 * handler exists (sc_intr != NULL). For DMA, see if the sc_dma_start() and
56 * sc_dma_finish() function pointers are not NULL.
57 */
58
59 /* Probe, attach, and detach functions for a atppc device on the ISA bus. */
60 static int atppc_isa_probe __P((struct device *, struct cfdata *, void *));
61 static void atppc_isa_attach __P((struct device *, struct device *, void *));
62 static int atppc_isa_detach __P((struct device *, int));
63
64 CFATTACH_DECL(atppc_isa, sizeof(struct atppc_isa_softc), atppc_isa_probe,
65 atppc_isa_attach, atppc_isa_detach, NULL);
66
67 /*
68 * Probe function: find parallel port controller on isa bus. Combined from
69 * lpt_isa_probe() in lpt.c and atppc_detect_port() from FreeBSD's ppc.c.
70 */
71 static int
72 atppc_isa_probe(struct device * parent, struct cfdata * cf, void * aux)
73 {
74 bus_space_handle_t ioh;
75 struct isa_attach_args * ia = aux;
76 bus_space_tag_t iot = ia->ia_iot;
77 int addr = ia->ia_io->ir_addr;
78 int rval = 0;
79
80 if(ia->ia_nio < 1 || addr == ISACF_PORT_DEFAULT) {
81 printf("%s(%s): io port unknown.\n", __func__,
82 parent->dv_xname);
83 }
84 else if(bus_space_map(iot, addr, IO_LPTSIZE, 0, &ioh) == 0) {
85 if (atppc_detect_port(iot, ioh) == 0)
86 rval = 1;
87 else
88 printf("%s(%s): unable to write/read I/O port.\n",
89 __func__, parent->dv_xname);
90 bus_space_unmap(iot, ioh, IO_LPTSIZE);
91 }
92 else {
93 printf("%s(%s): attempt to map bus space failed.\n", __func__,
94 parent->dv_xname);
95 }
96
97 if(rval) {
98 ia->ia_nio = 1;
99 ia->ia_io[0].ir_size = IO_LPTSIZE;
100 ia->ia_nirq = 1;
101 ia->ia_ndrq = 1;
102 ia->ia_niomem = 0;
103 }
104
105 return rval;
106 }
107
108 /* Attach function: attach and configure parallel port controller on isa bus. */
109 static void
110 atppc_isa_attach(struct device * parent, struct device * self, void * aux)
111 {
112 struct atppc_isa_softc * sc = (struct atppc_isa_softc *)self;
113 struct atppc_softc * lsc = (struct atppc_softc *)self;
114 struct isa_attach_args * ia = aux;
115
116 lsc->sc_iot = ia->ia_iot;
117 lsc->sc_dmat = ia->ia_dmat;
118 lsc->sc_has = 0;
119 sc->sc_ic = ia->ia_ic;
120 sc->sc_iobase = ia->ia_io->ir_addr;
121
122 if(bus_space_map(lsc->sc_iot, sc->sc_iobase, IO_LPTSIZE, 0,
123 &lsc->sc_ioh) != 0) {
124 printf("%s: attempt to map bus space failed, device not "
125 "properly attached.\n", self->dv_xname);
126 lsc->sc_dev_ok = ATPPC_NOATTACH;
127 return;
128 }
129 else {
130 lsc->sc_dev_ok = ATPPC_ATTACHED;
131 }
132
133 /* Assign interrupt handler */
134 if(!(self->dv_cfdata->cf_flags & ATPPC_FLAG_DISABLE_INTR)) {
135 if((ia->ia_irq->ir_irq != ISACF_IRQ_DEFAULT) &&
136 (ia->ia_nirq > 0)) {
137
138 sc->sc_irq = ia->ia_irq->ir_irq;
139 /* Establish interrupt handler. */
140 if(!(atppc_isa_intr_establish(lsc))) {
141 lsc->sc_has |= ATPPC_HAS_INTR;
142 }
143 }
144 else {
145 ATPPC_DPRINTF(("%s: IRQ not assigned or bad number of "
146 "IRQs.\n", self->dv_xname));
147 }
148 }
149 else {
150 ATPPC_VPRINTF(("%s: interrupts not configured due to flags.\n",
151 self->dv_xname));
152 }
153
154 /* Configure DMA */
155 if(!(self->dv_cfdata->cf_flags & ATPPC_FLAG_DISABLE_DMA)) {
156 if((ia->ia_drq->ir_drq != ISACF_DRQ_DEFAULT) &&
157 (ia->ia_ndrq > 0)) {
158
159 sc->sc_drq = ia->ia_drq->ir_drq;
160 if(!(atppc_isa_dma_setup(lsc))) {
161 lsc->sc_has |= ATPPC_HAS_DMA;
162 }
163 }
164 else {
165 ATPPC_DPRINTF(("%s: DRQ not assigned or bad number of "
166 "DRQs.\n", self->dv_xname));
167 }
168 }
169 else {
170 ATPPC_VPRINTF(("%s: dma not configured due to flags.\n",
171 self->dv_xname));
172 }
173
174 /* Run soft configuration attach */
175 atppc_sc_attach(lsc);
176
177 return;
178 }
179
180 /* Detach function: used to detach atppc driver at run time. */
181 static int
182 atppc_isa_detach(struct device * dev, int flag)
183 {
184 struct atppc_softc * lsc = (struct atppc_softc *) dev;
185 int rval;
186
187 if(lsc->sc_dev_ok == ATPPC_ATTACHED) {
188 /* Run soft configuration detach first */
189 rval = atppc_sc_detach(lsc, flag);
190
191 /* Disable DMA */
192 atppc_isa_dma_remove(lsc);
193
194 /* Disestablish interrupt handler */
195 atppc_isa_intr_disestablish(lsc);
196
197 /* Unmap bus space */
198 bus_space_unmap(lsc->sc_iot, lsc->sc_ioh, IO_LPTSIZE);
199
200 /* Mark config data */
201 lsc->sc_dev_ok = ATPPC_NOATTACH;
202 }
203 else {
204 rval = 0;
205 if(!(flag & DETACH_QUIET)) {
206 printf("%s not properly attached! Detach routines "
207 "skipped.\n", dev->dv_xname);
208 }
209 }
210
211 if(!(flag & DETACH_QUIET)) {
212 printf("%s: detached.", dev->dv_xname);
213 }
214
215 return rval;
216 }
217