atppc_ofisa.c revision 1.9
1/* $NetBSD: atppc_ofisa.c,v 1.9 2008/04/16 09:39:01 cegger Exp $ */
2
3/*-
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jaromir Dolecek.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *        This product includes software developed by the NetBSD
21 *        Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 *    contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <sys/cdefs.h>
40__KERNEL_RCSID(0, "$NetBSD: atppc_ofisa.c,v 1.9 2008/04/16 09:39:01 cegger Exp $");
41
42#include "opt_atppc.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/errno.h>
47#include <sys/ioctl.h>
48#include <sys/syslog.h>
49#include <sys/device.h>
50#include <sys/proc.h>
51#include <sys/termios.h>
52
53#include <sys/bus.h>
54
55#include <dev/ofw/openfirm.h>
56#include <dev/isa/isavar.h>
57#include <dev/ofisa/ofisavar.h>
58
59#include <dev/ic/atppcvar.h>
60#include <dev/isa/atppc_isadma.h>
61
62static int	atppc_ofisa_match(device_t, cfdata_t, void *);
63static void	atppc_ofisa_attach(device_t, device_t, void *);
64
65struct atppc_ofisa_softc {
66	struct atppc_softc sc_atppc;
67
68	isa_chipset_tag_t sc_ic;
69	int sc_drq;
70};
71
72CFATTACH_DECL_NEW(atppc_ofisa, sizeof(struct atppc_ofisa_softc), atppc_ofisa_match,
73    atppc_ofisa_attach, NULL, NULL);
74
75static int atppc_ofisa_dma_start(struct atppc_softc *, void *, u_int,
76	u_int8_t);
77static int atppc_ofisa_dma_finish(struct atppc_softc *);
78static int atppc_ofisa_dma_abort(struct atppc_softc *);
79static int atppc_ofisa_dma_malloc(device_t, void **, bus_addr_t *,
80	bus_size_t);
81static void atppc_ofisa_dma_free(device_t, void **, bus_addr_t *,
82	bus_size_t);
83/*
84 * atppc_ofisa_match: autoconf(9) match routine
85 */
86static int
87atppc_ofisa_match(device_t parent, cfdata_t match, void *aux)
88{
89	struct ofisa_attach_args *aa = aux;
90	static const char *const compatible_strings[] = { "pnpPNP,401", NULL };
91	int rv = 0;
92
93	if (of_compatible(aa->oba.oba_phandle, compatible_strings) != -1)
94		rv = 5;
95#ifdef _LPT_OFISA_MD_MATCH
96	if (!rv)
97		rv = lpt_ofisa_md_match(parent, match, aux);
98#endif
99	return (rv);
100}
101
102static void
103atppc_ofisa_attach(device_t parent, device_t self, void *aux)
104{
105	struct atppc_softc *sc = device_private(self);
106	struct atppc_ofisa_softc *asc = device_private(self);
107	struct ofisa_attach_args *aa = aux;
108	struct ofisa_reg_desc reg;
109	struct ofisa_intr_desc intr;
110	struct ofisa_dma_desc dma;
111	int n;
112
113	sc->sc_dev_ok = ATPPC_NOATTACH;
114
115	printf(": AT Parallel Port\n");
116
117	/* find our I/O space */
118	n = ofisa_reg_get(aa->oba.oba_phandle, &reg, 1);
119#ifdef _LPT_OFISA_MD_REG_FIXUP
120	n = lpt_ofisa_md_reg_fixup(parent, self, aux, &reg, 1, n);
121#endif
122	if (n != 1) {
123		aprint_error_dev(sc->sc_dev, "unable to find i/o register resource\n");
124		return;
125	}
126
127	/* find our IRQ */
128	n = ofisa_intr_get(aa->oba.oba_phandle, &intr, 1);
129#ifdef _LPT_OFISA_MD_INTR_FIXUP
130	n = lpt_ofisa_md_intr_fixup(parent, self, aux, &intr, 1, n);
131#endif
132	if (n != 1) {
133		aprint_error_dev(sc->sc_dev, "unable to find irq resource\n");
134		return;
135	}
136
137	/* find our DRQ */
138	if (ofisa_dma_get(aa->oba.oba_phandle, &dma, 1) != 1) {
139		aprint_error_dev(sc->sc_dev, "unable to find DMA data\n");
140		return;
141	}
142	asc->sc_drq = dma.drq;
143
144	/* Attach */
145	sc->sc_dev = self;
146	sc->sc_iot = (reg.type == OFISA_REG_TYPE_IO) ? aa->iot : aa->memt;
147	sc->sc_has = 0;
148	asc->sc_ic = aa->ic;
149
150	sc->sc_dev_ok = ATPPC_ATTACHED;
151
152	if (bus_space_map(sc->sc_iot, reg.addr, reg.len, 0,
153		&sc->sc_ioh) != 0) {
154		aprint_error_dev(self, "attempt to map bus space failed, device not "
155			"properly attached.\n");
156		return;
157	}
158
159	sc->sc_ieh = isa_intr_establish(aa->ic, intr.irq, intr.share,
160	    IPL_TTY, atppcintr, sc);
161	sc->sc_has |= ATPPC_HAS_INTR;
162
163	/* setup DMA hooks */
164	if (atppc_isadma_setup(sc, asc->sc_ic, asc->sc_drq) == 0) {
165		sc->sc_has |= ATPPC_HAS_DMA;
166		sc->sc_dma_start = atppc_ofisa_dma_start;
167		sc->sc_dma_finish = atppc_ofisa_dma_finish;
168		sc->sc_dma_abort = atppc_ofisa_dma_abort;
169		sc->sc_dma_malloc = atppc_ofisa_dma_malloc;
170		sc->sc_dma_free = atppc_ofisa_dma_free;
171	}
172
173	/* Run soft configuration attach */
174	atppc_sc_attach(sc);
175}
176
177/* Start DMA operation over ISA bus */
178static int
179atppc_ofisa_dma_start(struct atppc_softc *lsc, void *buf, u_int nbytes,
180	u_int8_t mode)
181{
182	struct atppc_ofisa_softc * sc = (struct atppc_ofisa_softc *) lsc;
183
184	return atppc_isadma_start(sc->sc_ic, sc->sc_drq, buf, nbytes, mode);
185}
186
187/* Stop DMA operation over ISA bus */
188static int
189atppc_ofisa_dma_finish(struct atppc_softc * lsc)
190{
191	struct atppc_ofisa_softc * sc = (struct atppc_ofisa_softc *) lsc;
192
193	return atppc_isadma_finish(sc->sc_ic, sc->sc_drq);
194}
195
196/* Abort DMA operation over ISA bus */
197int
198atppc_ofisa_dma_abort(struct atppc_softc * lsc)
199{
200	struct atppc_ofisa_softc * sc = (struct atppc_ofisa_softc *) lsc;
201
202	return atppc_isadma_abort(sc->sc_ic, sc->sc_drq);
203}
204
205/* Allocate memory for DMA over ISA bus */
206int
207atppc_ofisa_dma_malloc(device_t dev, void ** buf, bus_addr_t * bus_addr,
208	bus_size_t size)
209{
210	struct atppc_ofisa_softc * sc = device_private(dev);
211
212	return atppc_isadma_malloc(sc->sc_ic, sc->sc_drq, buf, bus_addr, size);
213}
214
215/* Free memory allocated by atppc_isa_dma_malloc() */
216void
217atppc_ofisa_dma_free(device_t dev, void ** buf, bus_addr_t * bus_addr,
218	bus_size_t size)
219{
220	struct atppc_ofisa_softc * sc = device_private(dev);
221
222	return atppc_isadma_free(sc->sc_ic, sc->sc_drq, buf, bus_addr, size);
223}
224