1 /* $NetBSD: atppc_isa.c,v 1.17 2022/09/25 17:09:36 thorpej 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 <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: atppc_isa.c,v 1.17 2022/09/25 17:09:36 thorpej Exp $");
34 
35 #include "opt_atppc.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41 
42 #include <sys/intr.h>
43 #include <sys/bus.h>
44 
45 #include <dev/isa/isareg.h>
46 #include <dev/isa/isavar.h>
47 #include <dev/isa/isadmavar.h>
48 
49 #include <dev/ic/atppcreg.h>
50 #include <dev/ic/atppcvar.h>
51 #include <dev/isa/atppc_isadma.h>
52 
53 /*
54  * ISA bus attach code for atppc driver.
55  * Note on capabilities: capabilities may exist in the chipset but may not
56  * necessarily be useable. I.e. you may specify an IRQ in the autoconfig, but
57  * will the port actually have an IRQ assigned to it at the hardware level?
58  * How can you test if the capabilities can be used? For interrupts, see if a
59  * handler exists (sc_intr != NULL). For DMA, see if the sc_dma_start() and
60  * sc_dma_finish() function pointers are not NULL.
61  */
62 
63 /* Configuration data for atppc on isa bus. */
64 struct atppc_isa_softc {
65           /* Machine independent device data */
66           struct atppc_softc sc_atppc;
67 
68           /* IRQ/DRQ/IO Port assignments on ISA bus */
69           int sc_irq;
70           int sc_drq;
71           int sc_iobase;
72 
73           /* ISA chipset tag */
74           isa_chipset_tag_t sc_ic;
75 };
76 
77 /* Probe and attach functions for a atppc device on the ISA bus. */
78 static int atppc_isa_probe(device_t, cfdata_t, void *);
79 static void atppc_isa_attach(device_t, device_t, void *);
80 
81 static int atppc_isa_dma_start(struct atppc_softc *, void *, u_int,
82           u_int8_t);
83 static int atppc_isa_dma_finish(struct atppc_softc *);
84 static int atppc_isa_dma_abort(struct atppc_softc *);
85 static int atppc_isa_dma_malloc(device_t, void **, bus_addr_t *,
86           bus_size_t);
87 static void atppc_isa_dma_free(device_t, void **, bus_addr_t *,
88           bus_size_t);
89 
90 CFATTACH_DECL_NEW(atppc_isa, sizeof(struct atppc_isa_softc), atppc_isa_probe,
91           atppc_isa_attach, NULL, NULL);
92 
93 /*
94  * Probe function: find parallel port controller on isa bus. Combined from
95  * lpt_isa_probe() in lpt.c and atppc_detect_port() from FreeBSD's ppc.c.
96  */
97 static int
atppc_isa_probe(device_t parent,cfdata_t cf,void * aux)98 atppc_isa_probe(device_t parent, cfdata_t cf, void *aux)
99 {
100           bus_space_handle_t ioh;
101           struct isa_attach_args *ia = aux;
102           bus_space_tag_t iot = ia->ia_iot;
103           int rval = 0;
104 
105           if (ia->ia_nio < 1)
106                     return (0);
107 
108           /* Disallow wildcarded i/o address */
109           if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
110                     return (0);
111 
112           if (bus_space_map(iot, ia->ia_io[0].ir_addr, IO_LPTSIZE, 0, &ioh))
113                     return (0);
114 
115           if (atppc_detect_port(iot, ioh) == 0)
116                     rval = 1;
117 
118           bus_space_unmap(iot, ioh, IO_LPTSIZE);
119 
120           if (rval) {
121                     ia->ia_nio = 1;
122                     ia->ia_io[0].ir_size = IO_LPTSIZE;
123                     ia->ia_nirq = 1;
124                     ia->ia_ndrq = 1;
125                     ia->ia_niomem = 0;
126           }
127           return (rval);
128 }
129 
130 /* Attach function: attach and configure parallel port controller on isa bus. */
131 static void
atppc_isa_attach(device_t parent,device_t self,void * aux)132 atppc_isa_attach(device_t parent, device_t self, void *aux)
133 {
134           struct atppc_isa_softc *sc = device_private(self);
135           struct atppc_softc *lsc = &sc->sc_atppc;
136           struct isa_attach_args *ia = aux;
137 
138           printf(": AT Parallel Port\n");
139 
140           lsc->sc_iot = ia->ia_iot;
141           lsc->sc_dmat = ia->ia_dmat;
142           lsc->sc_has = 0;
143           sc->sc_ic = ia->ia_ic;
144           sc->sc_iobase = ia->ia_io->ir_addr;
145 
146           if (bus_space_map(lsc->sc_iot, sc->sc_iobase, IO_LPTSIZE, 0,
147                     &lsc->sc_ioh) != 0) {
148                     aprint_error_dev(self, "attempt to map bus space failed, device not "
149                               "properly attached.\n");
150                     lsc->sc_dev_ok = ATPPC_NOATTACH;
151                     return;
152           }
153 
154           lsc->sc_dev = self;
155           lsc->sc_dev_ok = ATPPC_ATTACHED;
156 
157           /* Assign interrupt handler */
158           if (!(device_cfdata(self)->cf_flags & ATPPC_FLAG_DISABLE_INTR)
159              && ia->ia_irq->ir_irq != ISA_UNKNOWN_IRQ
160              && ia->ia_nirq >= 1) {
161                     sc->sc_irq = ia->ia_irq[0].ir_irq;
162           } else
163                     sc->sc_irq = -1;
164 
165           if (sc->sc_irq > 0) {
166                     /* Establish interrupt handler. */
167                     lsc->sc_ieh = isa_intr_establish(sc->sc_ic, sc->sc_irq,
168                               IST_EDGE, IPL_TTY, atppcintr, lsc->sc_dev);
169 
170                     lsc->sc_has |= ATPPC_HAS_INTR;
171           }
172 
173           /* Configure DMA */
174           if (!(device_cfdata(self)->cf_flags & ATPPC_FLAG_DISABLE_DMA)
175               && ia->ia_drq->ir_drq != ISA_UNKNOWN_DRQ
176               && ia->ia_ndrq >= 1)
177                     sc->sc_drq = ia->ia_drq[0].ir_drq;
178           else
179                     sc->sc_drq = -1;
180 
181           if (sc->sc_drq != -1
182               && atppc_isadma_setup(lsc, sc->sc_ic, sc->sc_drq) == 0) {
183                     lsc->sc_has |= ATPPC_HAS_DMA;
184 
185                     /* setup DMA hooks */
186                     lsc->sc_dma_start = atppc_isa_dma_start;
187                     lsc->sc_dma_finish = atppc_isa_dma_finish;
188                     lsc->sc_dma_abort = atppc_isa_dma_abort;
189                     lsc->sc_dma_malloc = atppc_isa_dma_malloc;
190                     lsc->sc_dma_free = atppc_isa_dma_free;
191           }
192 
193           /* Run soft configuration attach */
194           atppc_sc_attach(lsc);
195 
196           return;
197 }
198 
199 /* Start DMA operation over ISA bus */
200 static int
atppc_isa_dma_start(struct atppc_softc * lsc,void * buf,u_int nbytes,u_int8_t mode)201 atppc_isa_dma_start(struct atppc_softc *lsc, void *buf, u_int nbytes,
202           u_int8_t mode)
203 {
204           struct atppc_isa_softc *sc = (struct atppc_isa_softc *) lsc;
205 
206           return atppc_isadma_start(sc->sc_ic, sc->sc_drq, buf, nbytes, mode);
207 }
208 
209 /* Stop DMA operation over ISA bus */
210 static int
atppc_isa_dma_finish(struct atppc_softc * lsc)211 atppc_isa_dma_finish(struct atppc_softc *lsc)
212 {
213           struct atppc_isa_softc *sc = (struct atppc_isa_softc *) lsc;
214 
215           return atppc_isadma_finish(sc->sc_ic, sc->sc_drq);
216 }
217 
218 /* Abort DMA operation over ISA bus */
219 static int
atppc_isa_dma_abort(struct atppc_softc * lsc)220 atppc_isa_dma_abort(struct atppc_softc *lsc)
221 {
222           struct atppc_isa_softc *sc = (struct atppc_isa_softc *) lsc;
223 
224           return atppc_isadma_abort(sc->sc_ic, sc->sc_drq);
225 }
226 
227 /* Allocate memory for DMA over ISA bus */
228 static int
atppc_isa_dma_malloc(device_t dev,void ** buf,bus_addr_t * bus_addr,bus_size_t size)229 atppc_isa_dma_malloc(device_t dev, void **buf, bus_addr_t *bus_addr,
230           bus_size_t size)
231 {
232           struct atppc_isa_softc *sc = device_private(dev);
233 
234           return atppc_isadma_malloc(sc->sc_ic, sc->sc_drq, buf, bus_addr, size);
235 }
236 
237 /* Free memory allocated by atppc_isa_dma_malloc() */
238 static void
atppc_isa_dma_free(device_t dev,void ** buf,bus_addr_t * bus_addr,bus_size_t size)239 atppc_isa_dma_free(device_t dev, void **buf, bus_addr_t *bus_addr,
240           bus_size_t size)
241 {
242           struct atppc_isa_softc *sc = device_private(dev);
243 
244           return atppc_isadma_free(sc->sc_ic, sc->sc_drq, buf, bus_addr, size);
245 }
246