1 /* $NetBSD: atppc_puc.c,v 1.10 2008/04/28 20:23:54 martin Exp $ */
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include "opt_atppc.h"
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: atppc_puc.c,v 1.10 2008/04/28 20:23:54 martin Exp $");
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>
44 #include <sys/termios.h>
47 #include <uvm/uvm_extern.h>
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pucvar.h>
52 #include <dev/ic/atppcvar.h>
54 static int atppc_puc_match(device_t
, cfdata_t
, void *);
55 static void atppc_puc_attach(device_t
, device_t
, void *);
57 struct atppc_puc_softc
{
58 /* Machine independent device data */
59 struct atppc_softc sc_atppc
;
61 bus_dmamap_t sc_dmamap
;
64 CFATTACH_DECL_NEW(atppc_puc
, sizeof(struct atppc_puc_softc
), atppc_puc_match
,
65 atppc_puc_attach
, NULL
, NULL
);
67 static int atppc_puc_dma_setup(struct atppc_puc_softc
*);
68 static int atppc_puc_dma_start(struct atppc_softc
*, void *, u_int
,
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
*,
74 static void atppc_puc_dma_free(device_t
, void **, bus_addr_t
*,
78 * atppc_acpi_match: autoconf(9) match routine
81 atppc_puc_match(device_t parent
, cfdata_t match
, void *aux
)
83 struct puc_attach_args
*aa
= aux
;
86 * Locators already matched, just check the type.
88 if (aa
->type
!= PUC_PORT_TYPE_LPT
)
95 atppc_puc_attach(device_t parent
, device_t self
, void *aux
)
97 struct atppc_softc
*sc
= device_private(self
);
98 struct atppc_puc_softc
*psc
= device_private(self
);
99 struct puc_attach_args
*aa
= aux
;
102 sc
->sc_dev_ok
= ATPPC_NOATTACH
;
104 printf(": AT Parallel Port\n");
110 sc
->sc_dmat
= aa
->dmat
;
113 intrstr
= pci_intr_string(aa
->pc
, aa
->intrhandle
);
114 sc
->sc_ieh
= pci_intr_establish(aa
->pc
, aa
->intrhandle
, IPL_TTY
,
116 if (sc
->sc_ieh
== NULL
) {
117 aprint_error_dev(sc
->sc_dev
, "couldn't establish interrupt");
119 aprint_error(" at %s", intrstr
);
123 aprint_normal_dev(sc
->sc_dev
, "interrupting at %s\n", intrstr
);
124 sc
->sc_has
|= ATPPC_HAS_INTR
;
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
;
136 /* Finished attach */
137 sc
->sc_dev_ok
= ATPPC_ATTACHED
;
139 /* Run soft configuration attach */
143 /* Setup DMA structures */
145 atppc_puc_dma_setup(struct atppc_puc_softc
*psc
)
147 return EOPNOTSUPP
; /* XXX DMA not tested yet */
149 struct atppc_softc
*sc
= (struct atppc_softc
*)psc
;
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
)))
161 /* Start DMA operation over PCI bus */
163 atppc_puc_dma_start(struct atppc_softc
*dev
, void *buf
, u_int nbytes
,
166 struct atppc_puc_softc
*psc
= (struct atppc_puc_softc
*) dev
;
167 struct atppc_softc
*sc
= &psc
->sc_atppc
;
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
);
176 /* Stop DMA operation over PCI bus */
178 atppc_puc_dma_finish(struct atppc_softc
*dev
)
181 struct atppc_puc_softc
*psc
= (struct atppc_puc_softc
*) dev
;
182 struct atppc_softc
*sc
= &psc
->sc_atppc
;
185 * We don't know direction of DMA, so sync both. We can safely
186 * assume the dma map is loaded.
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
);
195 /* Abort DMA operation over PCI bus */
197 atppc_puc_dma_abort(struct atppc_softc
* lsc
)
200 /* Nothing to do - we do not need to sync, op is aborted */
204 /* Allocate memory for DMA over PCI bus */
206 atppc_puc_dma_malloc(device_t dev
, void **buf
, bus_addr_t
*bus_addr
,
209 struct atppc_puc_softc
*psc
= device_private(dev
);
210 struct atppc_softc
*sc
= &psc
->sc_atppc
;
213 error
= bus_dmamap_load(sc
->sc_dmat
, psc
->sc_dmamap
, *buf
, size
,
214 NULL
/* kernel address */, BUS_DMA_WAITOK
|BUS_DMA_STREAMING
);
218 *bus_addr
= psc
->sc_dmamap
->dm_segs
[0].ds_addr
;
222 /* Free memory allocated by atppc_isa_dma_malloc() */
224 atppc_puc_dma_free(device_t dev
, void **buf
, bus_addr_t
*bus_addr
,
227 struct atppc_puc_softc
*psc
= device_private(dev
);
228 struct atppc_softc
*sc
= &psc
->sc_atppc
;
230 return (bus_dmamap_unload(sc
->sc_dmat
, psc
->sc_dmamap
));