atppc_ofisa.c revision 1.8
1/* $NetBSD: atppc_ofisa.c,v 1.8 2008/04/15 15:02:28 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.8 2008/04/15 15:02:28 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(struct device *, struct cfdata *, void *);
63static void	atppc_ofisa_attach(struct device *, struct device *, 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(struct device *, void **, bus_addr_t *,
80	bus_size_t);
81static void atppc_ofisa_dma_free(struct device *, void **, bus_addr_t *,
82	bus_size_t);
83/*
84 * atppc_ofisa_match: autoconf(9) match routine
85 */
86static int
87atppc_ofisa_match(struct device *parent, struct cfdata *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(struct device *parent, struct device *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_iot = (reg.type == OFISA_REG_TYPE_IO) ? aa->iot : aa->memt;
146	sc->sc_has = 0;
147	asc->sc_ic = aa->ic;
148
149	sc->sc_dev_ok = ATPPC_ATTACHED;
150
151	if (bus_space_map(sc->sc_iot, reg.addr, reg.len, 0,
152		&sc->sc_ioh) != 0) {
153		aprint_error_dev(self, "attempt to map bus space failed, device not "
154			"properly attached.\n");
155		return;
156	}
157
158	sc->sc_ieh = isa_intr_establish(aa->ic, intr.irq, intr.share,
159	    IPL_TTY, atppcintr, sc);
160	sc->sc_has |= ATPPC_HAS_INTR;
161
162	/* setup DMA hooks */
163	if (atppc_isadma_setup(sc, asc->sc_ic, asc->sc_drq) == 0) {
164		sc->sc_has |= ATPPC_HAS_DMA;
165		sc->sc_dma_start = atppc_ofisa_dma_start;
166		sc->sc_dma_finish = atppc_ofisa_dma_finish;
167		sc->sc_dma_abort = atppc_ofisa_dma_abort;
168		sc->sc_dma_malloc = atppc_ofisa_dma_malloc;
169		sc->sc_dma_free = atppc_ofisa_dma_free;
170	}
171
172	/* Run soft configuration attach */
173	atppc_sc_attach(sc);
174}
175
176/* Start DMA operation over ISA bus */
177static int
178atppc_ofisa_dma_start(struct atppc_softc *lsc, void *buf, u_int nbytes,
179	u_int8_t mode)
180{
181	struct atppc_ofisa_softc * sc = (struct atppc_ofisa_softc *) lsc;
182
183	return atppc_isadma_start(sc->sc_ic, sc->sc_drq, buf, nbytes, mode);
184}
185
186/* Stop DMA operation over ISA bus */
187static int
188atppc_ofisa_dma_finish(struct atppc_softc * lsc)
189{
190	struct atppc_ofisa_softc * sc = (struct atppc_ofisa_softc *) lsc;
191
192	return atppc_isadma_finish(sc->sc_ic, sc->sc_drq);
193}
194
195/* Abort DMA operation over ISA bus */
196int
197atppc_ofisa_dma_abort(struct atppc_softc * lsc)
198{
199	struct atppc_ofisa_softc * sc = (struct atppc_ofisa_softc *) lsc;
200
201	return atppc_isadma_abort(sc->sc_ic, sc->sc_drq);
202}
203
204/* Allocate memory for DMA over ISA bus */
205int
206atppc_ofisa_dma_malloc(struct device * dev, void ** buf, bus_addr_t * bus_addr,
207	bus_size_t size)
208{
209	struct atppc_ofisa_softc * sc = (struct atppc_ofisa_softc *) dev;
210
211	return atppc_isadma_malloc(sc->sc_ic, sc->sc_drq, buf, bus_addr, size);
212}
213
214/* Free memory allocated by atppc_isa_dma_malloc() */
215void
216atppc_ofisa_dma_free(struct device * dev, void ** buf, bus_addr_t * bus_addr,
217	bus_size_t size)
218{
219	struct atppc_ofisa_softc * sc = (struct atppc_ofisa_softc *) dev;
220
221	return atppc_isadma_free(sc->sc_ic, sc->sc_drq, buf, bus_addr, size);
222}
223