xref: /netbsd/sys/dev/pci/atppc_puc.c (revision 79fb1406)
1 /* $NetBSD: atppc_puc.c,v 1.16 2018/12/09 11:14:01 jdolecek 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.16 2018/12/09 11:14:01 jdolecek 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
atppc_puc_match(device_t parent,cfdata_t match,void * aux)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
atppc_puc_attach(device_t parent,device_t self,void * aux)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 	char intrbuf[PCI_INTRSTR_LEN];
101 
102 	sc->sc_dev_ok = ATPPC_NOATTACH;
103 
104 	if (aa->poll) {
105 		aprint_error(": polling not supported\n");
106 		return;
107 	}
108 
109 	printf(": AT Parallel Port\n");
110 
111 	/* Attach */
112 	sc->sc_dev = self;
113 	sc->sc_iot = aa->t;
114 	sc->sc_ioh = aa->h;
115 	sc->sc_dmat = aa->dmat;
116 	sc->sc_has = 0;
117 
118 	intrstr = pci_intr_string(aa->pc, aa->intrhandle, intrbuf,
119 	    sizeof(intrbuf));
120 	sc->sc_ieh = pci_intr_establish_xname(aa->pc, aa->intrhandle, IPL_TTY,
121 	    atppcintr, sc, device_xname(self));
122 	if (sc->sc_ieh == NULL) {
123 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
124 		if (intrstr != NULL)
125 			aprint_error(" at %s", intrstr);
126 		aprint_error("\n");
127 		return;
128 	}
129 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
130 	sc->sc_has |= ATPPC_HAS_INTR;
131 
132 	/* setup DMA hooks */
133 	if (atppc_puc_dma_setup(psc) == 0) {
134 		sc->sc_has |= ATPPC_HAS_DMA;
135 		sc->sc_dma_start = atppc_puc_dma_start;
136 		sc->sc_dma_finish = atppc_puc_dma_finish;
137 		sc->sc_dma_abort = atppc_puc_dma_abort;
138 		sc->sc_dma_malloc = atppc_puc_dma_malloc;
139 		sc->sc_dma_free = atppc_puc_dma_free;
140 	}
141 
142 	/* Finished attach */
143 	sc->sc_dev_ok = ATPPC_ATTACHED;
144 
145 	/* Run soft configuration attach */
146 	atppc_sc_attach(sc);
147 }
148 
149 /* Setup DMA structures */
150 static int
atppc_puc_dma_setup(struct atppc_puc_softc * psc)151 atppc_puc_dma_setup(struct atppc_puc_softc *psc)
152 {
153 	return EOPNOTSUPP;	/* XXX DMA not tested yet */
154 #if 0
155 	struct atppc_softc *sc = (struct atppc_softc *)psc;
156 	int error;
157 
158 #define BUFSIZE	PAGE_SIZE	/* XXX see lptvar.h */
159 	if ((error = bus_dmamap_create(sc->sc_dmat, BUFSIZE, 1, BUFSIZE, 0,
160 	    BUS_DMA_NOWAIT, &psc->sc_dmamap)))
161 		return error;
162 
163 	return (0);
164 #endif
165 }
166 
167 /* Start DMA operation over PCI bus */
168 static int
atppc_puc_dma_start(struct atppc_softc * sc,void * buf,u_int nbytes,u_int8_t mode)169 atppc_puc_dma_start(struct atppc_softc *sc, void *buf, u_int nbytes,
170 	u_int8_t mode)
171 {
172 	struct atppc_puc_softc *psc = (struct atppc_puc_softc *)sc;
173 
174 	bus_dmamap_sync(sc->sc_dmat, psc->sc_dmamap, 0, nbytes,
175 	    (mode == ATPPC_DMA_MODE_WRITE) ? BUS_DMASYNC_PREWRITE
176 			: BUS_DMASYNC_PREREAD);
177 
178 	return (0);
179 }
180 
181 /* Stop DMA operation over PCI bus */
182 static int
atppc_puc_dma_finish(struct atppc_softc * sc)183 atppc_puc_dma_finish(struct atppc_softc *sc)
184 {
185 
186 	struct atppc_puc_softc *psc = (struct atppc_puc_softc *)sc;
187 
188 	/*
189 	 * We don't know direction of DMA, so sync both. We can safely
190 	 *  assume the dma map is loaded.
191 	 */
192 	bus_dmamap_sync(sc->sc_dmat, psc->sc_dmamap, 0,
193 	    psc->sc_dmamap->dm_segs[0].ds_len,
194 	    BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
195 
196 	return (0);
197 }
198 
199 /* Abort DMA operation over PCI bus */
200 int
atppc_puc_dma_abort(struct atppc_softc * lsc)201 atppc_puc_dma_abort(struct atppc_softc * lsc)
202 {
203 
204 	/* Nothing to do - we do not need to sync, op is aborted */
205 	return (0);
206 }
207 
208 /* Allocate memory for DMA over PCI bus */
209 int
atppc_puc_dma_malloc(device_t dev,void ** buf,bus_addr_t * bus_addr,bus_size_t size)210 atppc_puc_dma_malloc(device_t dev, void **buf, bus_addr_t *bus_addr,
211 	bus_size_t size)
212 {
213 	struct atppc_puc_softc *psc = device_private(dev);
214 	struct atppc_softc *sc = &psc->sc_atppc;
215 	int error;
216 
217 	error = bus_dmamap_load(sc->sc_dmat, psc->sc_dmamap, *buf, size,
218 	    NULL /* kernel address */, BUS_DMA_WAITOK|BUS_DMA_STREAMING);
219 	if (error)
220 		return (error);
221 
222 	*bus_addr = psc->sc_dmamap->dm_segs[0].ds_addr;
223 	return (0);
224 }
225 
226 /* Free memory allocated by atppc_isa_dma_malloc() */
227 void
atppc_puc_dma_free(device_t dev,void ** buf,bus_addr_t * bus_addr,bus_size_t size)228 atppc_puc_dma_free(device_t dev, void **buf, bus_addr_t *bus_addr,
229 	bus_size_t size)
230 {
231 	struct atppc_puc_softc *psc = device_private(dev);
232 	struct atppc_softc *sc = &psc->sc_atppc;
233 
234 	return (bus_dmamap_unload(sc->sc_dmat, psc->sc_dmamap));
235 }
236