hpcdma.c revision 1.1.2.2 1 1.1.2.2 thorpej /* $NetBSD: hpcdma.c,v 1.1.2.2 2001/08/25 06:15:49 thorpej Exp $ */
2 1.1.2.2 thorpej
3 1.1.2.2 thorpej /*
4 1.1.2.2 thorpej * Copyright (c) 2001 Wayne Knowles
5 1.1.2.2 thorpej * All rights reserved.
6 1.1.2.2 thorpej *
7 1.1.2.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.2 thorpej * by Wayne Knowles
9 1.1.2.2 thorpej *
10 1.1.2.2 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1.2.2 thorpej * modification, are permitted provided that the following conditions
12 1.1.2.2 thorpej * are met:
13 1.1.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1.2.2 thorpej * documentation and/or other materials provided with the distribution.
18 1.1.2.2 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.1.2.2 thorpej * must display the following acknowledgement:
20 1.1.2.2 thorpej * This product includes software developed by the NetBSD
21 1.1.2.2 thorpej * Foundation, Inc. and its contributors.
22 1.1.2.2 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.2.2 thorpej * contributors may be used to endorse or promote products derived
24 1.1.2.2 thorpej * from this software without specific prior written permission.
25 1.1.2.2 thorpej *
26 1.1.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.2.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.2.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.2.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.2.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.2.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.2.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.2.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.2.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.2.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.2.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.1.2.2 thorpej */
38 1.1.2.2 thorpej
39 1.1.2.2 thorpej #include <sys/param.h>
40 1.1.2.2 thorpej #include <sys/systm.h>
41 1.1.2.2 thorpej #include <sys/device.h>
42 1.1.2.2 thorpej #include <sys/buf.h>
43 1.1.2.2 thorpej
44 1.1.2.2 thorpej #include <machine/bus.h>
45 1.1.2.2 thorpej
46 1.1.2.2 thorpej #include <sgimips/hpc/hpcvar.h>
47 1.1.2.2 thorpej #include <sgimips/hpc/hpcreg.h>
48 1.1.2.2 thorpej #include <sgimips/hpc/hpcdma.h>
49 1.1.2.2 thorpej
50 1.1.2.2 thorpej /*
51 1.1.2.2 thorpej * Allocate DMA Chain descriptor list
52 1.1.2.2 thorpej *
53 1.1.2.2 thorpej * XXX: Need to place in device tree
54 1.1.2.2 thorpej */
55 1.1.2.2 thorpej void
56 1.1.2.2 thorpej hpcdma_init(haa, sc, ndesc)
57 1.1.2.2 thorpej struct hpc_attach_args *haa;
58 1.1.2.2 thorpej struct hpc_dma_softc *sc;
59 1.1.2.2 thorpej int ndesc;
60 1.1.2.2 thorpej {
61 1.1.2.2 thorpej bus_dma_segment_t seg;
62 1.1.2.2 thorpej struct hpc_dma_desc *hdd;
63 1.1.2.2 thorpej int rseg, allocsz;
64 1.1.2.2 thorpej
65 1.1.2.2 thorpej sc->sc_bst = haa->ha_iot;
66 1.1.2.2 thorpej sc->sc_bsh = haa->ha_ioh;
67 1.1.2.2 thorpej sc->sc_dmat = haa->ha_dmat;
68 1.1.2.2 thorpej sc->sc_ndesc = ndesc;
69 1.1.2.2 thorpej sc->sc_flags = 0;
70 1.1.2.2 thorpej
71 1.1.2.2 thorpej /* Alloc 1 additional descriptor - needed for DMA bug fix */
72 1.1.2.2 thorpej allocsz = sizeof(struct hpc_dma_desc) * (ndesc + 1);
73 1.1.2.2 thorpej KASSERT(allocsz <= NBPG);
74 1.1.2.2 thorpej
75 1.1.2.2 thorpej if (bus_dmamap_create(sc->sc_dmat, NBPG, 1 /*seg*/,
76 1.1.2.2 thorpej NBPG, 0, BUS_DMA_WAITOK,
77 1.1.2.2 thorpej &sc->sc_dmamap) != 0) {
78 1.1.2.2 thorpej printf(": failed to create dmamap\n");
79 1.1.2.2 thorpej return;
80 1.1.2.2 thorpej }
81 1.1.2.2 thorpej
82 1.1.2.2 thorpej /*
83 1.1.2.2 thorpej * Allocate a block of memory for dma chaining pointers
84 1.1.2.2 thorpej */
85 1.1.2.2 thorpej if (bus_dmamem_alloc(sc->sc_dmat, allocsz, 0, 0,
86 1.1.2.2 thorpej &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
87 1.1.2.2 thorpej printf(": can't allocate sglist\n");
88 1.1.2.2 thorpej return;
89 1.1.2.2 thorpej }
90 1.1.2.2 thorpej /* Map pages into kernel memory */
91 1.1.2.2 thorpej if (bus_dmamem_map(sc->sc_dmat, &seg, rseg, allocsz,
92 1.1.2.2 thorpej (caddr_t *)&hdd, BUS_DMA_NOWAIT)) {
93 1.1.2.2 thorpej printf(": can't map sglist\n");
94 1.1.2.2 thorpej bus_dmamem_free(sc->sc_dmat, &seg, rseg);
95 1.1.2.2 thorpej return;
96 1.1.2.2 thorpej }
97 1.1.2.2 thorpej
98 1.1.2.2 thorpej if (bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, hdd,
99 1.1.2.2 thorpej allocsz, NULL, BUS_DMA_NOWAIT)) {
100 1.1.2.2 thorpej printf(": can't load sglist\n");
101 1.1.2.2 thorpej return;
102 1.1.2.2 thorpej }
103 1.1.2.2 thorpej
104 1.1.2.2 thorpej sc->sc_desc_kva = hdd;
105 1.1.2.2 thorpej sc->sc_desc_pa = (void *) sc->sc_dmamap->dm_segs[0].ds_addr;
106 1.1.2.2 thorpej }
107 1.1.2.2 thorpej
108 1.1.2.2 thorpej
109 1.1.2.2 thorpej void
110 1.1.2.2 thorpej hpcdma_sglist_create(sc, dmamap)
111 1.1.2.2 thorpej struct hpc_dma_softc *sc;
112 1.1.2.2 thorpej bus_dmamap_t dmamap;
113 1.1.2.2 thorpej {
114 1.1.2.2 thorpej struct hpc_dma_desc *hva, *hpa;
115 1.1.2.2 thorpej bus_dma_segment_t *segp;
116 1.1.2.2 thorpej int i;
117 1.1.2.2 thorpej
118 1.1.2.2 thorpej KASSERT(dmamap->dm_nsegs <= sc->sc_ndesc);
119 1.1.2.2 thorpej
120 1.1.2.2 thorpej hva = sc->sc_desc_kva;
121 1.1.2.2 thorpej hpa = sc->sc_desc_pa;
122 1.1.2.2 thorpej segp = dmamap->dm_segs;
123 1.1.2.2 thorpej
124 1.1.2.2 thorpej #ifdef DMA_DEBUG
125 1.1.2.2 thorpej printf("DMA_SGLIST<");
126 1.1.2.2 thorpej #endif
127 1.1.2.2 thorpej for (i = dmamap->dm_nsegs; i; i--) {
128 1.1.2.2 thorpej #ifdef DMA_DEBUG
129 1.1.2.2 thorpej printf("%p:%ld, ", (void *)segp->ds_addr, segp->ds_len);
130 1.1.2.2 thorpej #endif
131 1.1.2.2 thorpej hva->hdd_bufptr = segp->ds_addr;
132 1.1.2.2 thorpej hva->hdd_ctl = segp->ds_len;
133 1.1.2.2 thorpej hva->hdd_descptr = (u_int32_t) ++hpa;
134 1.1.2.2 thorpej ++hva; ++segp;
135 1.1.2.2 thorpej }
136 1.1.2.2 thorpej /* Work around HPC3 DMA bug */
137 1.1.2.2 thorpej hva->hdd_bufptr = 0;
138 1.1.2.2 thorpej hva->hdd_ctl = HDD_CTL_EOCHAIN;
139 1.1.2.2 thorpej hva->hdd_descptr = 0;
140 1.1.2.2 thorpej hva++;
141 1.1.2.2 thorpej #ifdef DMA_DEBUG
142 1.1.2.2 thorpej printf(">\n");
143 1.1.2.2 thorpej #endif
144 1.1.2.2 thorpej bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
145 1.1.2.2 thorpej 0, sc->sc_dmamap->dm_mapsize,
146 1.1.2.2 thorpej BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
147 1.1.2.2 thorpej
148 1.1.2.2 thorpej /* Load DMA Descriptor list */
149 1.1.2.2 thorpej bus_space_write_4(sc->sc_bst, sc->sc_bsh, HPC_SCSI0_NDBP,
150 1.1.2.2 thorpej (u_int32_t)sc->sc_desc_pa);
151 1.1.2.2 thorpej }
152 1.1.2.2 thorpej
153 1.1.2.2 thorpej void
154 1.1.2.2 thorpej hpcdma_cntl(sc, mode)
155 1.1.2.2 thorpej struct hpc_dma_softc *sc;
156 1.1.2.2 thorpej u_int32_t mode;
157 1.1.2.2 thorpej {
158 1.1.2.2 thorpej bus_space_write_4(sc->sc_bst, sc->sc_bsh, HPC_SCSI0_CTL, mode);
159 1.1.2.2 thorpej }
160 1.1.2.2 thorpej
161 1.1.2.2 thorpej void
162 1.1.2.2 thorpej hpcdma_flush(sc)
163 1.1.2.2 thorpej struct hpc_dma_softc *sc;
164 1.1.2.2 thorpej {
165 1.1.2.2 thorpej u_int32_t mode;
166 1.1.2.2 thorpej
167 1.1.2.2 thorpej mode = bus_space_read_4(sc->sc_bst, sc->sc_bsh, HPC_SCSI0_CTL);
168 1.1.2.2 thorpej bus_space_write_4(sc->sc_bst, sc->sc_bsh, HPC_SCSI0_CTL,
169 1.1.2.2 thorpej mode | HPC_DMACTL_FLUSH);
170 1.1.2.2 thorpej
171 1.1.2.2 thorpej /* Wait for Active bit to drop */
172 1.1.2.2 thorpej while (bus_space_read_4(sc->sc_bst, sc->sc_bsh, HPC_SCSI0_CTL) &
173 1.1.2.2 thorpej HPC_DMACTL_ACTIVE) {
174 1.1.2.2 thorpej bus_space_barrier(sc->sc_bst, sc->sc_bsh, HPC_SCSI0_CTL, 4,
175 1.1.2.2 thorpej BUS_SPACE_BARRIER_READ);
176 1.1.2.2 thorpej }
177 1.1.2.2 thorpej }
178