No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / rs6000 / mca / mcadma_machdep.c
blobbc2a033eb94b488e6bde5f86a3a29c288e8ad52e
1 /* $NetBSD: mcadma_machdep.c,v 1.1 2007/12/17 19:09:40 garbled Exp $ */
3 /*-
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tim Rightnour
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.
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 <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: mcadma_machdep.c,v 1.1 2007/12/17 19:09:40 garbled Exp $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/syslog.h>
38 #include <sys/device.h>
39 #include <sys/malloc.h>
40 #include <sys/proc.h>
41 #include <sys/mbuf.h>
43 #define _POWERPC_BUS_DMA_PRIVATE
44 #include <machine/bus.h>
46 #include <machine/pio.h>
48 #include <dev/mca/mcavar.h>
49 #include <dev/mca/mcareg.h>
51 struct rs6000_dma_cookie {
52 int id_flags;
53 uint32_t bid; /* the BID of this IOCC */
54 uint32_t tce_off; /* offset of the TCE */
55 uint32_t nroftce;
56 uint32_t nrofslaves; /* nrof slave channels */
57 uint32_t slave_regs; /* allocation bitmask */
58 uint32_t dma_regs; /* allocation bitmask */
62 * Entry points for MCA DMA. These are mostly wrappers around
63 * the generic functions that understand how to deal with bounce
64 * buffers, if necessary.
67 struct powerpc_bus_dma_tag mca_bus_dma_tag = {
68 0, /* _bounce_thresh */
69 _mca_bus_dmamap_create,
70 _bus_dmamap_destroy,
71 _bus_dmamap_load,
72 _bus_dmamap_load_mbuf,
73 _bus_dmamap_load_uio,
74 _bus_dmamap_load_raw,
75 _bus_dmamap_unload,
76 NULL, /* _dmamap_sync */
77 _bus_dmamem_alloc,
78 _bus_dmamem_free,
79 _bus_dmamem_map,
80 _bus_dmamem_unmap,
81 _bus_dmamem_mmap,
84 /*
85 * Allocate a DMA map, and set up DMA channel.
86 */
87 static int
88 _mca_bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int flags,
89 bus_dmamap_t *dmamp, int dmach)
91 int error;
92 struct rs6000_dma_cookie *cookie;
94 #ifdef DEBUG
95 /* Sanity check */
96 if (dmach < 0 || dmach >= MAX_DMA_CHANNELS) {
97 printf("mcadma_create: invalid DMA channel %d\n", dmach);
98 return EINVAL;
101 if (size > 65536) {
102 panic("mca_dmamap_create: dmamap sz %ld > 65536", (long) size);
104 #endif
107 * MCA DMA transfer can be maximum 65536 bytes long and must
108 * be in one chunk. No specific boundary constraints are present.
110 if ((error = _bus_dmamap_create(t, size, 1, 65536, 0, flags, dmamp)))
111 return error;
113 cookie = (struct rs6000_dma_cookie *) (*dmamp)->_dm_cookie;
115 if (cookie == NULL) {
117 * Allocate our cookie if not yet done.
119 cookie = malloc(sizeof(struct rs6000_dma_cookie), M_DMAMAP,
120 ((flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK) | M_ZERO);
121 if (cookie == NULL) {
123 return ENOMEM;
125 (*dmamp)->_dm_cookie = cookie;
128 /* Encode DMA channel */
129 cookie->id_flags &= 0x0f;
130 cookie->id_flags |= dmach << 4;
132 /* Mark the dmamap as using DMA controller. Some devices
133 * drive DMA themselves, and don't need the MCA DMA controller.
134 * To distinguish the two, use a flag for dmamaps which use the DMA
135 * controller.
137 (*dmamp)->_dm_flags |= _MCABUS_DMA_USEDMACTRL;
138 return 0;
141 static void
142 _mca_bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
144 struct rs6000_dma_cookie *cookie = map->_dm_cookie;
146 free(cookie, M_DMAMAP);
147 _bus_dmamap_destroy(t, map);
151 * Load an MCA DMA map with a linear buffer.
153 static int
154 _mca_bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
155 bus_size_t buflen, struct proc *p, int flags)
157 struct rs6000_dma_cookie *cookie = map->_dm_cookie;
158 int error;
160 /* Make sure that on error condition we return "no valid mappings." */
161 map->dm_mapsize = 0;
162 map->dm_nsegs = 0;
164 error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
165 return error;
169 * Like _mca_bus_dmamap_load(), but for mbufs.
171 static int
172 _mca_bus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
173 int flags)
175 struct rs6000_dma_cookie *cookie = map->_dm_cookie;
176 int error;
178 /* Make sure that on error condition we return "no valid mappings." */
179 map->dm_mapsize = 0;
180 map->dm_nsegs = 0;
182 #ifdef DIAGNOSTIC
183 if ((m0->m_flags & M_PKTHDR) == 0)
184 panic("_mca_bus_dmamap_load_mbuf: no packet header");
185 #endif
187 if (m0->m_pkthdr.len > map->_dm_size)
188 return EINVAL;
190 error = _bus_dmamap_load_mbuf(t, map, m0, flags);
191 return error;
195 * Like _mca_bus_dmamap_load(), but for uios.
197 static int
198 _mca_bus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
199 int flags)
202 panic("_mca_bus_dmamap_load_uio: not implemented");
206 * Like _mca_bus_dmamap_load(), but for raw memory allocated with
207 * bus_dmamem_alloc().
209 static int
210 _mca_bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
211 bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
214 panic("_mca_bus_dmamap_load_raw: not implemented");
218 * Unload an MCA DMA map.
220 static void
221 _mca_bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
224 _bus_dmamap_unload(t, map);
228 * Allocate memory safe for MCA DMA.
230 static int
231 _mca_bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
232 bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
233 int flags)
235 paddr_t high;
237 high = trunc_page(avail_end);
239 return _bus_dmamem_alloc_range(t, size, alignment, boundary,
240 segs, nsegs, rsegs, flags, 0, high);