xref: /netbsd-src/sys/dev/pci/atppc_puc.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /* $NetBSD: atppc_puc.c,v 1.12 2010/11/13 13:52:05 uebayasi 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.12 2010/11/13 13:52:05 uebayasi 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 
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/pucvar.h>
50 
51 #include <dev/ic/atppcvar.h>
52 
53 static int	atppc_puc_match(device_t, cfdata_t, void *);
54 static void	atppc_puc_attach(device_t, device_t, void *);
55 
56 struct atppc_puc_softc {
57 	/* Machine independent device data */
58 	struct atppc_softc sc_atppc;
59 
60 	bus_dmamap_t sc_dmamap;
61 };
62 
63 CFATTACH_DECL_NEW(atppc_puc, sizeof(struct atppc_puc_softc), atppc_puc_match,
64     atppc_puc_attach, NULL, NULL);
65 
66 static int atppc_puc_dma_setup(struct atppc_puc_softc *);
67 static int atppc_puc_dma_start(struct atppc_softc *, void *, u_int,
68 	u_int8_t);
69 static int atppc_puc_dma_finish(struct atppc_softc *);
70 static int atppc_puc_dma_abort(struct atppc_softc *);
71 static int atppc_puc_dma_malloc(device_t, void **, bus_addr_t *,
72 	bus_size_t);
73 static void atppc_puc_dma_free(device_t, void **, bus_addr_t *,
74 	bus_size_t);
75 
76 /*
77  * atppc_acpi_match: autoconf(9) match routine
78  */
79 static int
80 atppc_puc_match(device_t parent, cfdata_t match, void *aux)
81 {
82 	struct puc_attach_args *aa = aux;
83 
84 	/*
85 	 * Locators already matched, just check the type.
86 	 */
87 	if (aa->type != PUC_PORT_TYPE_LPT)
88 		return (0);
89 
90 	return (1);
91 }
92 
93 static void
94 atppc_puc_attach(device_t parent, device_t self, void *aux)
95 {
96 	struct atppc_softc *sc = device_private(self);
97 	struct atppc_puc_softc *psc = device_private(self);
98 	struct puc_attach_args *aa = aux;
99 	const char *intrstr;
100 
101 	sc->sc_dev_ok = ATPPC_NOATTACH;
102 
103 	printf(": AT Parallel Port\n");
104 
105 	/* Attach */
106 	sc->sc_dev = self;
107 	sc->sc_iot = aa->t;
108 	sc->sc_ioh = aa->h;
109 	sc->sc_dmat = aa->dmat;
110 	sc->sc_has = 0;
111 
112 	intrstr = pci_intr_string(aa->pc, aa->intrhandle);
113 	sc->sc_ieh = pci_intr_establish(aa->pc, aa->intrhandle, IPL_TTY,
114 	    atppcintr, sc);
115 	if (sc->sc_ieh == NULL) {
116 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
117 		if (intrstr != NULL)
118 			aprint_error(" at %s", intrstr);
119 		aprint_error("\n");
120 		return;
121 	}
122 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
123 	sc->sc_has |= ATPPC_HAS_INTR;
124 
125 	/* setup DMA hooks */
126 	if (atppc_puc_dma_setup(psc) == 0) {
127 		sc->sc_has |= ATPPC_HAS_DMA;
128 		sc->sc_dma_start = atppc_puc_dma_start;
129 		sc->sc_dma_finish = atppc_puc_dma_finish;
130 		sc->sc_dma_abort = atppc_puc_dma_abort;
131 		sc->sc_dma_malloc = atppc_puc_dma_malloc;
132 		sc->sc_dma_free = atppc_puc_dma_free;
133 	}
134 
135 	/* Finished attach */
136 	sc->sc_dev_ok = ATPPC_ATTACHED;
137 
138 	/* Run soft configuration attach */
139 	atppc_sc_attach(sc);
140 }
141 
142 /* Setup DMA structures */
143 static int
144 atppc_puc_dma_setup(struct atppc_puc_softc *psc)
145 {
146 	return EOPNOTSUPP;	/* XXX DMA not tested yet */
147 #if 0
148 	struct atppc_softc *sc = (struct atppc_softc *)psc;
149 	int error;
150 
151 #define BUFSIZE	PAGE_SIZE	/* XXX see lptvar.h */
152 	if ((error = bus_dmamap_create(sc->sc_dmat, BUFSIZE, 1, BUFSIZE, 0,
153 	    BUS_DMA_NOWAIT, &psc->sc_dmamap)))
154 		return error;
155 
156 	return (0);
157 #endif
158 }
159 
160 /* Start DMA operation over PCI bus */
161 static int
162 atppc_puc_dma_start(struct atppc_softc *dev, void *buf, u_int nbytes,
163 	u_int8_t mode)
164 {
165 	struct atppc_puc_softc *psc = (struct atppc_puc_softc *) dev;
166 	struct atppc_softc *sc = &psc->sc_atppc;
167 
168 	bus_dmamap_sync(sc->sc_dmat, psc->sc_dmamap, 0, nbytes,
169 	    (mode == ATPPC_DMA_MODE_WRITE) ? BUS_DMASYNC_PREWRITE
170 			: BUS_DMASYNC_PREREAD);
171 
172 	return (0);
173 }
174 
175 /* Stop DMA operation over PCI bus */
176 static int
177 atppc_puc_dma_finish(struct atppc_softc *dev)
178 {
179 
180 	struct atppc_puc_softc *psc = (struct atppc_puc_softc *) dev;
181 	struct atppc_softc *sc = &psc->sc_atppc;
182 
183 	/*
184 	 * We don't know direction of DMA, so sync both. We can safely
185 	 *  assume the dma map is loaded.
186 	 */
187 	bus_dmamap_sync(sc->sc_dmat, psc->sc_dmamap, 0,
188 	    psc->sc_dmamap->dm_segs[0].ds_len,
189 	    BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
190 
191 	return (0);
192 }
193 
194 /* Abort DMA operation over PCI bus */
195 int
196 atppc_puc_dma_abort(struct atppc_softc * lsc)
197 {
198 
199 	/* Nothing to do - we do not need to sync, op is aborted */
200 	return (0);
201 }
202 
203 /* Allocate memory for DMA over PCI bus */
204 int
205 atppc_puc_dma_malloc(device_t dev, void **buf, bus_addr_t *bus_addr,
206 	bus_size_t size)
207 {
208 	struct atppc_puc_softc *psc = device_private(dev);
209 	struct atppc_softc *sc = &psc->sc_atppc;
210 	int error;
211 
212 	error = bus_dmamap_load(sc->sc_dmat, psc->sc_dmamap, *buf, size,
213 	    NULL /* kernel address */, BUS_DMA_WAITOK|BUS_DMA_STREAMING);
214 	if (error)
215 		return (error);
216 
217 	*bus_addr = psc->sc_dmamap->dm_segs[0].ds_addr;
218 	return (0);
219 }
220 
221 /* Free memory allocated by atppc_isa_dma_malloc() */
222 void
223 atppc_puc_dma_free(device_t dev, void **buf, bus_addr_t *bus_addr,
224 	bus_size_t size)
225 {
226 	struct atppc_puc_softc *psc = device_private(dev);
227 	struct atppc_softc *sc = &psc->sc_atppc;
228 
229 	return (bus_dmamap_unload(sc->sc_dmat, psc->sc_dmamap));
230 }
231