1 /* $NetBSD: atppc_isadma.c,v 1.8 2022/09/25 17:09:36 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2001 Alcove - Nicolas Souchu
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * FreeBSD: src/sys/isa/ppc.c,v 1.26.2.5 2001/10/02 05:21:45 nsouch Exp
29 *
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: atppc_isadma.c,v 1.8 2022/09/25 17:09:36 thorpej Exp $");
34
35 #include "opt_atppc.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41
42 #include <sys/intr.h>
43 #include <sys/bus.h>
44
45 #include <dev/ic/atppcreg.h>
46 #include <dev/ic/atppcvar.h>
47
48 #include <dev/isa/isadmavar.h>
49 #include <dev/isa/isareg.h>
50 #include <dev/isa/isavar.h>
51
52 #include <dev/isa/atppc_isadma.h>
53
54 /* Enable DMA */
55 int
atppc_isadma_setup(struct atppc_softc * lsc,isa_chipset_tag_t ic,int drq)56 atppc_isadma_setup(struct atppc_softc * lsc, isa_chipset_tag_t ic, int drq)
57 {
58 int error = 1;
59
60 /* Reserve DRQ */
61 if (isa_drq_alloc(ic, drq)) {
62 ATPPC_DPRINTF(("%s(%s): cannot reserve DRQ line.\n", __func__,
63 device_xname(lsc->sc_dev)));
64 return error;
65 }
66
67 /* Get maximum DMA size for isa bus */
68 lsc->sc_dma_maxsize = isa_dmamaxsize(ic, drq);
69
70 /* Create dma mapping */
71 error = isa_dmamap_create(ic, drq, lsc->sc_dma_maxsize,
72 BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW);
73
74 return error;
75 }
76
77 /* Start DMA operation over ISA bus */
78 int
atppc_isadma_start(isa_chipset_tag_t ic,int drq,void * buf,u_int nbytes,u_int8_t mode)79 atppc_isadma_start(isa_chipset_tag_t ic, int drq, void *buf, u_int nbytes,
80 u_int8_t mode)
81 {
82 return (isa_dmastart(ic, drq, buf, nbytes, NULL,
83 ((mode == ATPPC_DMA_MODE_WRITE) ? DMAMODE_WRITE :
84 DMAMODE_READ) | DMAMODE_DEMAND, ((mode == ATPPC_DMA_MODE_WRITE)
85 ? BUS_DMA_WRITE : BUS_DMA_READ) | BUS_DMA_NOWAIT));
86 }
87
88 /* Stop DMA operation over ISA bus */
89 int
atppc_isadma_finish(isa_chipset_tag_t ic,int drq)90 atppc_isadma_finish(isa_chipset_tag_t ic, int drq)
91 {
92 isa_dmadone(ic, drq);
93 return 0;
94 }
95
96 /* Abort DMA operation over ISA bus */
97 int
atppc_isadma_abort(isa_chipset_tag_t ic,int drq)98 atppc_isadma_abort(isa_chipset_tag_t ic, int drq)
99 {
100 isa_dmaabort(ic, drq);
101 return 0;
102 }
103
104 /* Allocate memory for DMA over ISA bus */
105 int
atppc_isadma_malloc(isa_chipset_tag_t ic,int drq,void ** buf,bus_addr_t * bus_addr,bus_size_t size)106 atppc_isadma_malloc(isa_chipset_tag_t ic, int drq, void **buf, bus_addr_t *bus_addr, bus_size_t size)
107 {
108 int error;
109
110 error = isa_dmamem_alloc(ic, drq, size, bus_addr, BUS_DMA_WAITOK);
111 if (error)
112 return error;
113
114 error = isa_dmamem_map(ic, drq, *bus_addr, size, buf, BUS_DMA_WAITOK);
115 if (error)
116 isa_dmamem_free(ic, drq, *bus_addr, size);
117
118 return error;
119 }
120
121 /* Free memory allocated by atppc_isadma_malloc() */
122 void
atppc_isadma_free(isa_chipset_tag_t ic,int drq,void ** buf,bus_addr_t * bus_addr,bus_size_t size)123 atppc_isadma_free(isa_chipset_tag_t ic, int drq, void ** buf, bus_addr_t * bus_addr, bus_size_t size)
124 {
125 isa_dmamem_unmap(ic, drq, *buf, size);
126 isa_dmamem_free(ic, drq, *bus_addr, size);
127 }
128