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