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