qec.c revision 1.2 1 1.2 pk /* $NetBSD: qec.c,v 1.2 1998/07/28 00:44:39 pk Exp $ */
2 1.1 pk
3 1.1 pk /*-
4 1.1 pk * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 pk * All rights reserved.
6 1.1 pk *
7 1.1 pk * This code is derived from software contributed to The NetBSD Foundation
8 1.1 pk * by Paul Kranenburg.
9 1.1 pk *
10 1.1 pk * Redistribution and use in source and binary forms, with or without
11 1.1 pk * modification, are permitted provided that the following conditions
12 1.1 pk * are met:
13 1.1 pk * 1. Redistributions of source code must retain the above copyright
14 1.1 pk * notice, this list of conditions and the following disclaimer.
15 1.1 pk * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 pk * notice, this list of conditions and the following disclaimer in the
17 1.1 pk * documentation and/or other materials provided with the distribution.
18 1.1 pk * 3. All advertising materials mentioning features or use of this software
19 1.1 pk * must display the following acknowledgement:
20 1.1 pk * This product includes software developed by the NetBSD
21 1.1 pk * Foundation, Inc. and its contributors.
22 1.1 pk * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 pk * contributors may be used to endorse or promote products derived
24 1.1 pk * from this software without specific prior written permission.
25 1.1 pk *
26 1.1 pk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 pk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 pk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 pk * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 pk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 pk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 pk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 pk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 pk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 pk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 pk * POSSIBILITY OF SUCH DAMAGE.
37 1.1 pk */
38 1.1 pk
39 1.1 pk #include <sys/types.h>
40 1.1 pk #include <sys/param.h>
41 1.1 pk #include <sys/systm.h>
42 1.1 pk #include <sys/kernel.h>
43 1.1 pk #include <sys/errno.h>
44 1.1 pk #include <sys/ioctl.h>
45 1.1 pk #include <sys/device.h>
46 1.1 pk #include <sys/malloc.h>
47 1.1 pk #include <sys/buf.h>
48 1.1 pk #include <sys/proc.h>
49 1.1 pk #include <sys/user.h>
50 1.1 pk
51 1.2 pk #include <machine/bus.h>
52 1.2 pk #include <machine/autoconf.h>
53 1.1 pk
54 1.1 pk #include <dev/sbus/sbusvar.h>
55 1.1 pk #include <dev/sbus/qecreg.h>
56 1.1 pk #include <dev/sbus/qecvar.h>
57 1.1 pk
58 1.1 pk static int qecprint __P((void *, const char *));
59 1.1 pk static int qecmatch __P((struct device *, struct cfdata *, void *));
60 1.1 pk static void qecattach __P((struct device *, struct device *, void *));
61 1.1 pk
62 1.1 pk static int qec_bus_map __P((
63 1.1 pk bus_space_tag_t,
64 1.1 pk bus_type_t, /*slot*/
65 1.1 pk bus_addr_t, /*offset*/
66 1.1 pk bus_size_t, /*size*/
67 1.1 pk int, /*flags*/
68 1.1 pk vm_offset_t, /*preferred virtual address */
69 1.1 pk bus_space_handle_t *));
70 1.1 pk
71 1.1 pk struct cfattach qec_ca = {
72 1.1 pk sizeof(struct qec_softc), qecmatch, qecattach
73 1.1 pk };
74 1.1 pk
75 1.1 pk int
76 1.1 pk qecprint(aux, busname)
77 1.1 pk void *aux;
78 1.1 pk const char *busname;
79 1.1 pk {
80 1.1 pk struct sbus_attach_args *sa = aux;
81 1.1 pk bus_space_tag_t t = sa->sa_bustag;
82 1.1 pk struct qec_softc *sc = t->cookie;
83 1.1 pk
84 1.1 pk sa->sa_bustag = sc->sc_bustag; /* XXX */
85 1.1 pk sbus_print(aux, busname); /* XXX */
86 1.1 pk sa->sa_bustag = t; /* XXX */
87 1.1 pk return (UNCONF);
88 1.1 pk }
89 1.1 pk
90 1.1 pk int
91 1.1 pk qecmatch(parent, cf, aux)
92 1.1 pk struct device *parent;
93 1.1 pk struct cfdata *cf;
94 1.1 pk void *aux;
95 1.1 pk {
96 1.1 pk struct sbus_attach_args *sa = aux;
97 1.1 pk
98 1.1 pk return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
99 1.1 pk }
100 1.1 pk
101 1.1 pk /*
102 1.1 pk * Attach all the sub-devices we can find
103 1.1 pk */
104 1.1 pk void
105 1.1 pk qecattach(parent, self, aux)
106 1.1 pk struct device *parent, *self;
107 1.1 pk void *aux;
108 1.1 pk {
109 1.1 pk struct sbus_attach_args *sa = aux;
110 1.1 pk struct qec_softc *sc = (void *)self;
111 1.1 pk int node;
112 1.1 pk int sbusburst;
113 1.1 pk bus_space_tag_t sbt;
114 1.1 pk bus_space_handle_t bh;
115 1.1 pk struct bootpath *bp;
116 1.1 pk int error;
117 1.1 pk int nreg;
118 1.1 pk struct rom_reg *rr;
119 1.1 pk
120 1.1 pk sc->sc_bustag = sa->sa_bustag;
121 1.1 pk sc->sc_dmatag = sa->sa_dmatag;
122 1.1 pk node = sa->sa_node;
123 1.1 pk
124 1.1 pk rr = NULL;
125 1.1 pk if (getpropA(node, "reg", sizeof(struct rom_reg),
126 1.1 pk &nreg, (void **)&rr) != 0) {
127 1.1 pk printf("%s: cannot get register property\n", self->dv_xname);
128 1.1 pk return;
129 1.1 pk }
130 1.1 pk if (nreg < 2) {
131 1.1 pk printf("%s: only %d register sets\n", self->dv_xname, nreg);
132 1.1 pk return;
133 1.1 pk }
134 1.1 pk
135 1.1 pk if (sbus_bus_map(sa->sa_bustag,
136 1.1 pk (bus_type_t)rr[0].rr_iospace,
137 1.1 pk (bus_addr_t)rr[0].rr_paddr,
138 1.1 pk (bus_size_t)rr[0].rr_len,
139 1.1 pk BUS_SPACE_MAP_LINEAR, 0, &bh) != 0) {
140 1.1 pk printf("%s: attach: cannot map registers\n", self->dv_xname);
141 1.1 pk return;
142 1.1 pk }
143 1.1 pk sc->sc_regs = (void *)bh;
144 1.1 pk
145 1.1 pk /*
146 1.1 pk * This device's "register space 1" is just a buffer where the
147 1.1 pk * Lance ring-buffers can be stored. Note the buffer's location
148 1.1 pk * and size, so the child driver can pick them up.
149 1.1 pk */
150 1.1 pk if (sbus_bus_map(sa->sa_bustag,
151 1.1 pk (bus_type_t)rr[1].rr_iospace,
152 1.1 pk (bus_addr_t)rr[1].rr_paddr,
153 1.1 pk (bus_size_t)rr[1].rr_len,
154 1.1 pk BUS_SPACE_MAP_LINEAR, 0, &bh) != 0) {
155 1.1 pk printf("%s: attach: cannot map registers\n", self->dv_xname);
156 1.1 pk return;
157 1.1 pk }
158 1.1 pk sc->sc_buffer = (caddr_t)bh;
159 1.1 pk sc->sc_bufsiz = (bus_size_t)rr[1].rr_len;
160 1.1 pk
161 1.1 pk /*
162 1.1 pk * Get transfer burst size from PROM
163 1.1 pk */
164 1.1 pk sbusburst = ((struct sbus_softc *)parent)->sc_burst;
165 1.1 pk if (sbusburst == 0)
166 1.1 pk sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
167 1.1 pk
168 1.1 pk sc->sc_burst = getpropint(node, "burst-sizes", -1);
169 1.1 pk if (sc->sc_burst == -1)
170 1.1 pk /* take SBus burst sizes */
171 1.1 pk sc->sc_burst = sbusburst;
172 1.1 pk
173 1.1 pk /* Clamp at parent's burst sizes */
174 1.1 pk sc->sc_burst &= sbusburst;
175 1.1 pk
176 1.1 pk sbus_establish(&sc->sc_sd, &sc->sc_dev);
177 1.1 pk
178 1.1 pk
179 1.1 pk /*
180 1.1 pk * Collect address translations from the OBP.
181 1.1 pk */
182 1.1 pk error = getpropA(node, "ranges", sizeof(struct rom_range),
183 1.1 pk &sc->sc_nrange, (void **)&sc->sc_range);
184 1.1 pk switch (error) {
185 1.1 pk case 0:
186 1.1 pk break;
187 1.1 pk case ENOENT:
188 1.1 pk default:
189 1.1 pk panic("%s: error getting ranges property", self->dv_xname);
190 1.1 pk }
191 1.1 pk
192 1.1 pk /* Propagate bootpath */
193 1.1 pk if (sa->sa_bp != NULL)
194 1.1 pk bp = sa->sa_bp + 1;
195 1.1 pk else
196 1.1 pk bp = NULL;
197 1.1 pk
198 1.1 pk /* Allocate a bus tag */
199 1.1 pk sbt = (bus_space_tag_t)
200 1.1 pk malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
201 1.1 pk if (sbt == NULL) {
202 1.1 pk printf("%s: attach: out of memory\n", self->dv_xname);
203 1.1 pk return;
204 1.1 pk }
205 1.1 pk
206 1.1 pk bzero(sbt, sizeof *sbt);
207 1.1 pk sbt->cookie = sc;
208 1.1 pk sbt->parent = sc->sc_bustag;
209 1.1 pk sbt->sparc_bus_map = qec_bus_map;
210 1.1 pk
211 1.1 pk printf(": %dK memory\n", sc->sc_bufsiz / 1024);
212 1.1 pk
213 1.1 pk /* search through children */
214 1.1 pk for (node = firstchild(node); node; node = nextsibling(node)) {
215 1.1 pk struct sbus_attach_args sa;
216 1.1 pk sbus_setup_attach_args((struct sbus_softc *)parent,
217 1.1 pk sbt, sc->sc_dmatag, node, bp, &sa);
218 1.1 pk (void)config_found(&sc->sc_dev, (void *)&sa, qecprint);
219 1.1 pk }
220 1.1 pk free(rr, M_DEVBUF);
221 1.1 pk }
222 1.1 pk
223 1.1 pk int
224 1.1 pk qec_bus_map(t, btype, offset, size, flags, vaddr, hp)
225 1.1 pk bus_space_tag_t t;
226 1.1 pk bus_type_t btype;
227 1.1 pk bus_addr_t offset;
228 1.1 pk bus_size_t size;
229 1.1 pk int flags;
230 1.1 pk vm_offset_t vaddr;
231 1.1 pk bus_space_handle_t *hp;
232 1.1 pk {
233 1.1 pk struct qec_softc *sc = t->cookie;
234 1.1 pk int slot = btype;
235 1.1 pk int i;
236 1.1 pk
237 1.1 pk for (i = 0; i < sc->sc_nrange; i++) {
238 1.1 pk bus_addr_t paddr;
239 1.1 pk bus_type_t iospace;
240 1.1 pk
241 1.1 pk if (sc->sc_range[i].cspace != slot)
242 1.1 pk continue;
243 1.1 pk
244 1.1 pk /* We've found the connection to the parent bus */
245 1.1 pk paddr = sc->sc_range[i].poffset + offset;
246 1.1 pk iospace = sc->sc_range[i].pspace;
247 1.1 pk return (bus_space_map2(sc->sc_bustag, iospace, paddr,
248 1.1 pk size, flags, vaddr, hp));
249 1.1 pk }
250 1.1 pk
251 1.1 pk return (EINVAL);
252 1.1 pk }
253