atppc_isa.c revision 1.16 1 1.16 andvar /* $NetBSD: atppc_isa.c,v 1.16 2021/12/03 13:27:38 andvar Exp $ */
2 1.2 bjh21
3 1.1 jdolecek /*-
4 1.1 jdolecek * Copyright (c) 2001 Alcove - Nicolas Souchu
5 1.1 jdolecek * All rights reserved.
6 1.1 jdolecek *
7 1.1 jdolecek * Redistribution and use in source and binary forms, with or without
8 1.1 jdolecek * modification, are permitted provided that the following conditions
9 1.1 jdolecek * are met:
10 1.1 jdolecek * 1. Redistributions of source code must retain the above copyright
11 1.1 jdolecek * notice, this list of conditions and the following disclaimer.
12 1.1 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jdolecek * notice, this list of conditions and the following disclaimer in the
14 1.1 jdolecek * documentation and/or other materials provided with the distribution.
15 1.1 jdolecek *
16 1.1 jdolecek * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 jdolecek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 jdolecek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 jdolecek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 jdolecek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 jdolecek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 jdolecek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 jdolecek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 jdolecek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jdolecek * SUCH DAMAGE.
27 1.1 jdolecek *
28 1.3 bjh21 * FreeBSD: src/sys/isa/ppc.c,v 1.26.2.5 2001/10/02 05:21:45 nsouch Exp
29 1.1 jdolecek *
30 1.1 jdolecek */
31 1.1 jdolecek
32 1.3 bjh21 #include <sys/cdefs.h>
33 1.16 andvar __KERNEL_RCSID(0, "$NetBSD: atppc_isa.c,v 1.16 2021/12/03 13:27:38 andvar Exp $");
34 1.3 bjh21
35 1.1 jdolecek #include "opt_atppc.h"
36 1.1 jdolecek
37 1.1 jdolecek #include <sys/param.h>
38 1.1 jdolecek #include <sys/systm.h>
39 1.1 jdolecek #include <sys/kernel.h>
40 1.1 jdolecek #include <sys/malloc.h>
41 1.1 jdolecek #include <sys/device.h>
42 1.1 jdolecek
43 1.11 ad #include <sys/intr.h>
44 1.11 ad #include <sys/bus.h>
45 1.1 jdolecek
46 1.1 jdolecek #include <dev/isa/isareg.h>
47 1.1 jdolecek #include <dev/isa/isavar.h>
48 1.1 jdolecek #include <dev/isa/isadmavar.h>
49 1.1 jdolecek
50 1.1 jdolecek #include <dev/ic/atppcreg.h>
51 1.1 jdolecek #include <dev/ic/atppcvar.h>
52 1.4 jdolecek #include <dev/isa/atppc_isadma.h>
53 1.1 jdolecek
54 1.1 jdolecek /*
55 1.1 jdolecek * ISA bus attach code for atppc driver.
56 1.16 andvar * Note on capabilities: capabilities may exist in the chipset but may not
57 1.5 drochner * necessarily be useable. I.e. you may specify an IRQ in the autoconfig, but
58 1.1 jdolecek * will the port actually have an IRQ assigned to it at the hardware level?
59 1.16 andvar * How can you test if the capabilities can be used? For interrupts, see if a
60 1.5 drochner * handler exists (sc_intr != NULL). For DMA, see if the sc_dma_start() and
61 1.1 jdolecek * sc_dma_finish() function pointers are not NULL.
62 1.1 jdolecek */
63 1.1 jdolecek
64 1.4 jdolecek /* Configuration data for atppc on isa bus. */
65 1.4 jdolecek struct atppc_isa_softc {
66 1.4 jdolecek /* Machine independent device data */
67 1.4 jdolecek struct atppc_softc sc_atppc;
68 1.4 jdolecek
69 1.4 jdolecek /* IRQ/DRQ/IO Port assignments on ISA bus */
70 1.4 jdolecek int sc_irq;
71 1.4 jdolecek int sc_drq;
72 1.4 jdolecek int sc_iobase;
73 1.4 jdolecek
74 1.4 jdolecek /* ISA chipset tag */
75 1.4 jdolecek isa_chipset_tag_t sc_ic;
76 1.4 jdolecek };
77 1.4 jdolecek
78 1.4 jdolecek /* Probe and attach functions for a atppc device on the ISA bus. */
79 1.14 cegger static int atppc_isa_probe(device_t, cfdata_t, void *);
80 1.14 cegger static void atppc_isa_attach(device_t, device_t, void *);
81 1.4 jdolecek
82 1.5 drochner static int atppc_isa_dma_start(struct atppc_softc *, void *, u_int,
83 1.4 jdolecek u_int8_t);
84 1.4 jdolecek static int atppc_isa_dma_finish(struct atppc_softc *);
85 1.4 jdolecek static int atppc_isa_dma_abort(struct atppc_softc *);
86 1.14 cegger static int atppc_isa_dma_malloc(device_t, void **, bus_addr_t *,
87 1.4 jdolecek bus_size_t);
88 1.14 cegger static void atppc_isa_dma_free(device_t, void **, bus_addr_t *,
89 1.4 jdolecek bus_size_t);
90 1.1 jdolecek
91 1.13 cegger CFATTACH_DECL_NEW(atppc_isa, sizeof(struct atppc_isa_softc), atppc_isa_probe,
92 1.4 jdolecek atppc_isa_attach, NULL, NULL);
93 1.1 jdolecek
94 1.5 drochner /*
95 1.5 drochner * Probe function: find parallel port controller on isa bus. Combined from
96 1.5 drochner * lpt_isa_probe() in lpt.c and atppc_detect_port() from FreeBSD's ppc.c.
97 1.1 jdolecek */
98 1.1 jdolecek static int
99 1.14 cegger atppc_isa_probe(device_t parent, cfdata_t cf, void *aux)
100 1.1 jdolecek {
101 1.1 jdolecek bus_space_handle_t ioh;
102 1.5 drochner struct isa_attach_args *ia = aux;
103 1.1 jdolecek bus_space_tag_t iot = ia->ia_iot;
104 1.5 drochner int rval = 0;
105 1.1 jdolecek
106 1.4 jdolecek if (ia->ia_nio < 1)
107 1.4 jdolecek return (0);
108 1.1 jdolecek
109 1.4 jdolecek /* Disallow wildcarded i/o address */
110 1.6 drochner if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
111 1.4 jdolecek return (0);
112 1.4 jdolecek
113 1.4 jdolecek if (bus_space_map(iot, ia->ia_io[0].ir_addr, IO_LPTSIZE, 0, &ioh))
114 1.4 jdolecek return (0);
115 1.4 jdolecek
116 1.5 drochner if (atppc_detect_port(iot, ioh) == 0)
117 1.5 drochner rval = 1;
118 1.4 jdolecek
119 1.5 drochner bus_space_unmap(iot, ioh, IO_LPTSIZE);
120 1.1 jdolecek
121 1.5 drochner if (rval) {
122 1.5 drochner ia->ia_nio = 1;
123 1.5 drochner ia->ia_io[0].ir_size = IO_LPTSIZE;
124 1.5 drochner ia->ia_nirq = 1;
125 1.5 drochner ia->ia_ndrq = 1;
126 1.5 drochner ia->ia_niomem = 0;
127 1.5 drochner }
128 1.5 drochner return (rval);
129 1.1 jdolecek }
130 1.1 jdolecek
131 1.1 jdolecek /* Attach function: attach and configure parallel port controller on isa bus. */
132 1.5 drochner static void
133 1.14 cegger atppc_isa_attach(device_t parent, device_t self, void *aux)
134 1.1 jdolecek {
135 1.14 cegger struct atppc_isa_softc *sc = device_private(self);
136 1.5 drochner struct atppc_softc *lsc = &sc->sc_atppc;
137 1.5 drochner struct isa_attach_args *ia = aux;
138 1.1 jdolecek
139 1.4 jdolecek printf(": AT Parallel Port\n");
140 1.4 jdolecek
141 1.1 jdolecek lsc->sc_iot = ia->ia_iot;
142 1.5 drochner lsc->sc_dmat = ia->ia_dmat;
143 1.1 jdolecek lsc->sc_has = 0;
144 1.1 jdolecek sc->sc_ic = ia->ia_ic;
145 1.1 jdolecek sc->sc_iobase = ia->ia_io->ir_addr;
146 1.1 jdolecek
147 1.5 drochner if (bus_space_map(lsc->sc_iot, sc->sc_iobase, IO_LPTSIZE, 0,
148 1.1 jdolecek &lsc->sc_ioh) != 0) {
149 1.12 cegger aprint_error_dev(self, "attempt to map bus space failed, device not "
150 1.12 cegger "properly attached.\n");
151 1.1 jdolecek lsc->sc_dev_ok = ATPPC_NOATTACH;
152 1.1 jdolecek return;
153 1.1 jdolecek }
154 1.4 jdolecek
155 1.14 cegger lsc->sc_dev = self;
156 1.4 jdolecek lsc->sc_dev_ok = ATPPC_ATTACHED;
157 1.1 jdolecek
158 1.1 jdolecek /* Assign interrupt handler */
159 1.9 thorpej if (!(device_cfdata(self)->cf_flags & ATPPC_FLAG_DISABLE_INTR)
160 1.6 drochner && ia->ia_irq->ir_irq != ISA_UNKNOWN_IRQ
161 1.4 jdolecek && ia->ia_nirq >= 1) {
162 1.4 jdolecek sc->sc_irq = ia->ia_irq[0].ir_irq;
163 1.4 jdolecek } else
164 1.4 jdolecek sc->sc_irq = -1;
165 1.4 jdolecek
166 1.4 jdolecek if (sc->sc_irq > 0) {
167 1.4 jdolecek /* Establish interrupt handler. */
168 1.4 jdolecek lsc->sc_ieh = isa_intr_establish(sc->sc_ic, sc->sc_irq,
169 1.15 jakllsch IST_EDGE, IPL_TTY, atppcintr, lsc->sc_dev);
170 1.4 jdolecek
171 1.4 jdolecek lsc->sc_has |= ATPPC_HAS_INTR;
172 1.1 jdolecek }
173 1.1 jdolecek
174 1.1 jdolecek /* Configure DMA */
175 1.9 thorpej if (!(device_cfdata(self)->cf_flags & ATPPC_FLAG_DISABLE_DMA)
176 1.6 drochner && ia->ia_drq->ir_drq != ISA_UNKNOWN_DRQ
177 1.4 jdolecek && ia->ia_ndrq >= 1)
178 1.4 jdolecek sc->sc_drq = ia->ia_drq[0].ir_drq;
179 1.4 jdolecek else
180 1.4 jdolecek sc->sc_drq = -1;
181 1.4 jdolecek
182 1.4 jdolecek if (sc->sc_drq != -1
183 1.4 jdolecek && atppc_isadma_setup(lsc, sc->sc_ic, sc->sc_drq) == 0) {
184 1.4 jdolecek lsc->sc_has |= ATPPC_HAS_DMA;
185 1.4 jdolecek
186 1.4 jdolecek /* setup DMA hooks */
187 1.4 jdolecek lsc->sc_dma_start = atppc_isa_dma_start;
188 1.4 jdolecek lsc->sc_dma_finish = atppc_isa_dma_finish;
189 1.4 jdolecek lsc->sc_dma_abort = atppc_isa_dma_abort;
190 1.4 jdolecek lsc->sc_dma_malloc = atppc_isa_dma_malloc;
191 1.4 jdolecek lsc->sc_dma_free = atppc_isa_dma_free;
192 1.1 jdolecek }
193 1.1 jdolecek
194 1.1 jdolecek /* Run soft configuration attach */
195 1.1 jdolecek atppc_sc_attach(lsc);
196 1.1 jdolecek
197 1.1 jdolecek return;
198 1.1 jdolecek }
199 1.1 jdolecek
200 1.4 jdolecek /* Start DMA operation over ISA bus */
201 1.5 drochner static int
202 1.4 jdolecek atppc_isa_dma_start(struct atppc_softc *lsc, void *buf, u_int nbytes,
203 1.4 jdolecek u_int8_t mode)
204 1.4 jdolecek {
205 1.14 cegger struct atppc_isa_softc *sc = (struct atppc_isa_softc *) lsc;
206 1.5 drochner
207 1.4 jdolecek return atppc_isadma_start(sc->sc_ic, sc->sc_drq, buf, nbytes, mode);
208 1.4 jdolecek }
209 1.4 jdolecek
210 1.4 jdolecek /* Stop DMA operation over ISA bus */
211 1.5 drochner static int
212 1.5 drochner atppc_isa_dma_finish(struct atppc_softc *lsc)
213 1.1 jdolecek {
214 1.14 cegger struct atppc_isa_softc *sc = (struct atppc_isa_softc *) lsc;
215 1.5 drochner
216 1.4 jdolecek return atppc_isadma_finish(sc->sc_ic, sc->sc_drq);
217 1.4 jdolecek }
218 1.1 jdolecek
219 1.4 jdolecek /* Abort DMA operation over ISA bus */
220 1.5 drochner static int
221 1.5 drochner atppc_isa_dma_abort(struct atppc_softc *lsc)
222 1.4 jdolecek {
223 1.14 cegger struct atppc_isa_softc *sc = (struct atppc_isa_softc *) lsc;
224 1.5 drochner
225 1.4 jdolecek return atppc_isadma_abort(sc->sc_ic, sc->sc_drq);
226 1.4 jdolecek }
227 1.1 jdolecek
228 1.5 drochner /* Allocate memory for DMA over ISA bus */
229 1.4 jdolecek static int
230 1.14 cegger atppc_isa_dma_malloc(device_t dev, void **buf, bus_addr_t *bus_addr,
231 1.4 jdolecek bus_size_t size)
232 1.4 jdolecek {
233 1.14 cegger struct atppc_isa_softc *sc = device_private(dev);
234 1.1 jdolecek
235 1.4 jdolecek return atppc_isadma_malloc(sc->sc_ic, sc->sc_drq, buf, bus_addr, size);
236 1.4 jdolecek }
237 1.1 jdolecek
238 1.5 drochner /* Free memory allocated by atppc_isa_dma_malloc() */
239 1.5 drochner static void
240 1.14 cegger atppc_isa_dma_free(device_t dev, void **buf, bus_addr_t *bus_addr,
241 1.4 jdolecek bus_size_t size)
242 1.4 jdolecek {
243 1.14 cegger struct atppc_isa_softc *sc = device_private(dev);
244 1.1 jdolecek
245 1.4 jdolecek return atppc_isadma_free(sc->sc_ic, sc->sc_drq, buf, bus_addr, size);
246 1.1 jdolecek }
247