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