xref: /netbsd-src/sys/dev/pci/atppc_puc.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /* $NetBSD: atppc_puc.c,v 1.10 2008/04/28 20:23:54 martin 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "opt_atppc.h"
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: atppc_puc.c,v 1.10 2008/04/28 20:23:54 martin Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/errno.h>
40 #include <sys/ioctl.h>
41 #include <sys/syslog.h>
42 #include <sys/device.h>
43 #include <sys/proc.h>
44 #include <sys/termios.h>
45 
46 #include <sys/bus.h>
47 #include <uvm/uvm_extern.h>
48 
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pucvar.h>
51 
52 #include <dev/ic/atppcvar.h>
53 
54 static int	atppc_puc_match(device_t, cfdata_t, void *);
55 static void	atppc_puc_attach(device_t, device_t, void *);
56 
57 struct atppc_puc_softc {
58 	/* Machine independent device data */
59 	struct atppc_softc sc_atppc;
60 
61 	bus_dmamap_t sc_dmamap;
62 };
63 
64 CFATTACH_DECL_NEW(atppc_puc, sizeof(struct atppc_puc_softc), atppc_puc_match,
65     atppc_puc_attach, NULL, NULL);
66 
67 static int atppc_puc_dma_setup(struct atppc_puc_softc *);
68 static int atppc_puc_dma_start(struct atppc_softc *, void *, u_int,
69 	u_int8_t);
70 static int atppc_puc_dma_finish(struct atppc_softc *);
71 static int atppc_puc_dma_abort(struct atppc_softc *);
72 static int atppc_puc_dma_malloc(device_t, void **, bus_addr_t *,
73 	bus_size_t);
74 static void atppc_puc_dma_free(device_t, void **, bus_addr_t *,
75 	bus_size_t);
76 
77 /*
78  * atppc_acpi_match: autoconf(9) match routine
79  */
80 static int
81 atppc_puc_match(device_t parent, cfdata_t match, void *aux)
82 {
83 	struct puc_attach_args *aa = aux;
84 
85 	/*
86 	 * Locators already matched, just check the type.
87 	 */
88 	if (aa->type != PUC_PORT_TYPE_LPT)
89 		return (0);
90 
91 	return (1);
92 }
93 
94 static void
95 atppc_puc_attach(device_t parent, device_t self, void *aux)
96 {
97 	struct atppc_softc *sc = device_private(self);
98 	struct atppc_puc_softc *psc = device_private(self);
99 	struct puc_attach_args *aa = aux;
100 	const char *intrstr;
101 
102 	sc->sc_dev_ok = ATPPC_NOATTACH;
103 
104 	printf(": AT Parallel Port\n");
105 
106 	/* Attach */
107 	sc->sc_dev = self;
108 	sc->sc_iot = aa->t;
109 	sc->sc_ioh = aa->h;
110 	sc->sc_dmat = aa->dmat;
111 	sc->sc_has = 0;
112 
113 	intrstr = pci_intr_string(aa->pc, aa->intrhandle);
114 	sc->sc_ieh = pci_intr_establish(aa->pc, aa->intrhandle, IPL_TTY,
115 	    atppcintr, sc);
116 	if (sc->sc_ieh == NULL) {
117 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
118 		if (intrstr != NULL)
119 			printf(" at %s", intrstr);
120 		printf("\n");
121 		return;
122 	}
123 	printf("%s: interrupting at %s\n", device_xname(sc->sc_dev), intrstr);
124 	sc->sc_has |= ATPPC_HAS_INTR;
125 
126 	/* setup DMA hooks */
127 	if (atppc_puc_dma_setup(psc) == 0) {
128 		sc->sc_has |= ATPPC_HAS_DMA;
129 		sc->sc_dma_start = atppc_puc_dma_start;
130 		sc->sc_dma_finish = atppc_puc_dma_finish;
131 		sc->sc_dma_abort = atppc_puc_dma_abort;
132 		sc->sc_dma_malloc = atppc_puc_dma_malloc;
133 		sc->sc_dma_free = atppc_puc_dma_free;
134 	}
135 
136 	/* Finished attach */
137 	sc->sc_dev_ok = ATPPC_ATTACHED;
138 
139 	/* Run soft configuration attach */
140 	atppc_sc_attach(sc);
141 }
142 
143 /* Setup DMA structures */
144 static int
145 atppc_puc_dma_setup(struct atppc_puc_softc *psc)
146 {
147 	return EOPNOTSUPP;	/* XXX DMA not tested yet */
148 #if 0
149 	struct atppc_softc *sc = (struct atppc_softc *)psc;
150 	int error;
151 
152 #define BUFSIZE	PAGE_SIZE	/* XXX see lptvar.h */
153 	if ((error = bus_dmamap_create(sc->sc_dmat, BUFSIZE, 1, BUFSIZE, 0,
154 	    BUS_DMA_NOWAIT, &psc->sc_dmamap)))
155 		return error;
156 
157 	return (0);
158 #endif
159 }
160 
161 /* Start DMA operation over PCI bus */
162 static int
163 atppc_puc_dma_start(struct atppc_softc *dev, void *buf, u_int nbytes,
164 	u_int8_t mode)
165 {
166 	struct atppc_puc_softc *psc = (struct atppc_puc_softc *) dev;
167 	struct atppc_softc *sc = &psc->sc_atppc;
168 
169 	bus_dmamap_sync(sc->sc_dmat, psc->sc_dmamap, 0, nbytes,
170 	    (mode == ATPPC_DMA_MODE_WRITE) ? BUS_DMASYNC_PREWRITE
171 			: BUS_DMASYNC_PREREAD);
172 
173 	return (0);
174 }
175 
176 /* Stop DMA operation over PCI bus */
177 static int
178 atppc_puc_dma_finish(struct atppc_softc *dev)
179 {
180 
181 	struct atppc_puc_softc *psc = (struct atppc_puc_softc *) dev;
182 	struct atppc_softc *sc = &psc->sc_atppc;
183 
184 	/*
185 	 * We don't know direction of DMA, so sync both. We can safely
186 	 *  assume the dma map is loaded.
187 	 */
188 	bus_dmamap_sync(sc->sc_dmat, psc->sc_dmamap, 0,
189 	    psc->sc_dmamap->dm_segs[0].ds_len,
190 	    BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
191 
192 	return (0);
193 }
194 
195 /* Abort DMA operation over PCI bus */
196 int
197 atppc_puc_dma_abort(struct atppc_softc * lsc)
198 {
199 
200 	/* Nothing to do - we do not need to sync, op is aborted */
201 	return (0);
202 }
203 
204 /* Allocate memory for DMA over PCI bus */
205 int
206 atppc_puc_dma_malloc(device_t dev, void **buf, bus_addr_t *bus_addr,
207 	bus_size_t size)
208 {
209 	struct atppc_puc_softc *psc = device_private(dev);
210 	struct atppc_softc *sc = &psc->sc_atppc;
211 	int error;
212 
213 	error = bus_dmamap_load(sc->sc_dmat, psc->sc_dmamap, *buf, size,
214 	    NULL /* kernel address */, BUS_DMA_WAITOK|BUS_DMA_STREAMING);
215 	if (error)
216 		return (error);
217 
218 	*bus_addr = psc->sc_dmamap->dm_segs[0].ds_addr;
219 	return (0);
220 }
221 
222 /* Free memory allocated by atppc_isa_dma_malloc() */
223 void
224 atppc_puc_dma_free(device_t dev, void **buf, bus_addr_t *bus_addr,
225 	bus_size_t size)
226 {
227 	struct atppc_puc_softc *psc = device_private(dev);
228 	struct atppc_softc *sc = &psc->sc_atppc;
229 
230 	return (bus_dmamap_unload(sc->sc_dmat, psc->sc_dmamap));
231 }
232