1 /* $OpenBSD: dma_sbus.c,v 1.12 2005/03/03 01:41:45 miod Exp $ */
2 /* $NetBSD: dma_sbus.c,v 1.5 2000/07/09 20:57:42 pk Exp $ */
3
4 /*-
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Paul Kranenburg.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*-
41 * Copyright (c) 1994 Peter Galbavy. All rights reserved.
42 * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>. All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
54 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
55 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
56 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
57 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
58 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
62 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 */
64
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
67
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/bus.h>
71 #include <sys/kernel.h>
72 #include <sys/module.h>
73 #include <sys/resource.h>
74 #include <sys/rman.h>
75
76 #include <dev/ofw/ofw_bus.h>
77 #include <dev/ofw/openfirm.h>
78
79 #include <machine/bus.h>
80 #include <machine/bus_common.h>
81 #include <machine/resource.h>
82
83 #include <sparc64/sbus/lsi64854reg.h>
84 #include <sparc64/sbus/lsi64854var.h>
85 #include <sparc64/sbus/ofw_sbus.h>
86 #include <sparc64/sbus/sbusreg.h>
87 #include <sparc64/sbus/sbusvar.h>
88
89 struct dma_devinfo {
90 char *ddi_compat; /* PROM compatible */
91 char *ddi_model; /* PROM model */
92 char *ddi_name; /* PROM name */
93 phandle_t ddi_node; /* PROM node */
94 char *ddi_type; /* PROM device_type */
95
96 struct resource_list ddi_rl;
97 };
98
99 struct dma_softc {
100 struct lsi64854_softc sc_lsi64854; /* base device */
101 int sc_ign;
102 int sc_slot;
103 };
104
105 static devclass_t dma_devclass;
106
107 static device_probe_t dma_probe;
108 static device_attach_t dma_attach;
109 static bus_print_child_t dma_print_child;
110 static bus_probe_nomatch_t dma_probe_nomatch;
111 static bus_get_resource_list_t dma_get_resource_list;
112 static ofw_bus_get_compat_t dma_get_compat;
113 static ofw_bus_get_model_t dma_get_model;
114 static ofw_bus_get_name_t dma_get_name;
115 static ofw_bus_get_node_t dma_get_node;
116 static ofw_bus_get_type_t dma_get_type;
117
118 static struct dma_devinfo *dma_setup_dinfo(device_t, phandle_t, char *);
119 static void dma_destroy_dinfo(struct dma_devinfo *);
120
121 static device_method_t dma_methods[] = {
122 /* Device interface */
123 DEVMETHOD(device_probe, dma_probe),
124 DEVMETHOD(device_attach, dma_attach),
125 DEVMETHOD(device_shutdown, bus_generic_shutdown),
126 DEVMETHOD(device_suspend, bus_generic_suspend),
127 DEVMETHOD(device_resume, bus_generic_resume),
128
129 /* Bus interface */
130 DEVMETHOD(bus_print_child, dma_print_child),
131 DEVMETHOD(bus_probe_nomatch, dma_probe_nomatch),
132 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
133 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
134 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
135 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
136 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
137 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
138 DEVMETHOD(bus_get_resource_list, dma_get_resource_list),
139 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
140
141 /* ofw_bus interface */
142 DEVMETHOD(ofw_bus_get_compat, dma_get_compat),
143 DEVMETHOD(ofw_bus_get_model, dma_get_model),
144 DEVMETHOD(ofw_bus_get_name, dma_get_name),
145 DEVMETHOD(ofw_bus_get_node, dma_get_node),
146 DEVMETHOD(ofw_bus_get_type, dma_get_type),
147
148 { 0, 0 }
149 };
150
151 static driver_t dma_driver = {
152 "dma",
153 dma_methods,
154 sizeof(struct dma_softc),
155 };
156
157 DRIVER_MODULE(dma, sbus, dma_driver, dma_devclass, 0, 0);
158 MODULE_DEPEND(dma, sbus, 1, 1, 1);
159 MODULE_VERSION(dma, 1);
160
161 static int
162 dma_probe(device_t dev)
163 {
164 const char *name;
165
166 name = ofw_bus_get_name(dev);
167 if (strcmp(name, "espdma") == 0 || strcmp(name, "dma") == 0 ||
168 strcmp(name, "ledma") == 0) {
169 device_set_desc_copy(dev, name);
170 return (0);
171 }
172 return (ENXIO);
173 }
174
175 static int
176 dma_attach(device_t dev)
177 {
178 struct dma_softc *dsc;
179 struct lsi64854_softc *lsc;
180 struct dma_devinfo *ddi;
181 device_t cdev;
182 const char *name;
183 char *cabletype, *cname;
184 uint32_t csr;
185 phandle_t child, node;
186 int error, burst, children;
187
188 dsc = device_get_softc(dev);
189 lsc = &dsc->sc_lsi64854;
190
191 name = ofw_bus_get_name(dev);
192 node = ofw_bus_get_node(dev);
193 dsc->sc_ign = sbus_get_ign(dev);
194 dsc->sc_slot = sbus_get_slot(dev);
195
196 lsc->sc_rid = 0;
197 lsc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &lsc->sc_rid,
198 RF_ACTIVE);
199 if (lsc->sc_res == NULL) {
200 device_printf(dev, "cannot allocate resources\n");
201 return (ENXIO);
202 }
203 lsc->sc_regt = rman_get_bustag(lsc->sc_res);
204 lsc->sc_regh = rman_get_bushandle(lsc->sc_res);
205
206 if (strcmp(name, "espdma") == 0 || strcmp(name, "dma") == 0)
207 lsc->sc_channel = L64854_CHANNEL_SCSI;
208 else if (strcmp(name, "ledma") == 0) {
209 /*
210 * Check to see which cable type is currently active and
211 * set the appropriate bit in the ledma csr so that it
212 * gets used. If we didn't netboot, the PROM won't have
213 * the "cable-selection" property; default to TP and then
214 * the user can change it via a "media" option to ifconfig.
215 */
216 csr = L64854_GCSR(lsc);
217 if ((OF_getprop_alloc(node, "cable-selection", 1,
218 (void **)&cabletype)) == -1) {
219 /* assume TP if nothing there */
220 csr |= E_TP_AUI;
221 } else {
222 if (strcmp(cabletype, "aui") == 0)
223 csr &= ~E_TP_AUI;
224 else
225 csr |= E_TP_AUI;
226 free(cabletype, M_OFWPROP);
227 }
228 L64854_SCSR(lsc, csr);
229 DELAY(20000); /* manual says we need a 20ms delay */
230 lsc->sc_channel = L64854_CHANNEL_ENET;
231 } else {
232 device_printf(dev, "unsupported DMA channel\n");
233 error = ENXIO;
234 goto fail_lres;
235 }
236
237 error = bus_dma_tag_create(
238 NULL, /* parent */
239 PAGE_SIZE, 0, /* alignment, boundary */
240 BUS_SPACE_MAXADDR, /* lowaddr */
241 BUS_SPACE_MAXADDR, /* highaddr */
242 NULL, NULL, /* filter, filterarg */
243 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
244 0, /* nsegments */
245 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
246 0, /* flags */
247 NULL, NULL, /* no locking */
248 &lsc->sc_parent_dmat);
249 if (error != 0) {
250 device_printf(dev, "cannot allocate parent DMA tag\n");
251 goto fail_lres;
252 }
253
254 burst = sbus_get_burstsz(dev);
255 lsc->sc_burst = (burst & SBUS_BURST_32) ? 32 :
256 (burst & SBUS_BURST_16) ? 16 : 0;
257 lsc->sc_dev = dev;
258
259 error = lsi64854_attach(lsc);
260 if (error != 0) {
261 device_printf(dev, "lsi64854_attach failed\n");
262 goto fail_lpdma;
263 }
264
265 /* Attach children. */
266 children = 0;
267 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
268 if ((OF_getprop_alloc(child, "name", 1, (void **)&cname)) == -1)
269 continue;
270 if ((ddi = dma_setup_dinfo(dev, child, cname)) == NULL) {
271 device_printf(dev, "<%s>: incomplete\n", cname);
272 free(cname, M_OFWPROP);
273 continue;
274 }
275 if (children != 0) {
276 device_printf(dev, "<%s>: only one child per DMA "
277 "channel supported\n", cname);
278 dma_destroy_dinfo(ddi);
279 free(cname, M_OFWPROP);
280 continue;
281 }
282 if ((cdev = device_add_child(dev, NULL, -1)) == NULL) {
283 device_printf(dev, "<%s>: device_add_child failed\n",
284 cname);
285 dma_destroy_dinfo(ddi);
286 free(cname, M_OFWPROP);
287 continue;
288 }
289 device_set_ivars(cdev, ddi);
290 children++;
291 }
292 error = bus_generic_attach(dev);
293 if (error != 0) {
294 device_printf(dev, "bus_generic_attach failed\n");
295 goto fail_lsi;
296 }
297
298 return (0);
299
300 fail_lsi:
301 lsi64854_detach(lsc);
302 fail_lpdma:
303 bus_dma_tag_destroy(lsc->sc_parent_dmat);
304 fail_lres:
305 bus_release_resource(dev, SYS_RES_MEMORY, lsc->sc_rid, lsc->sc_res);
306 return (error);
307 }
308
309 static struct dma_devinfo *
310 dma_setup_dinfo(device_t dev, phandle_t node, char *name)
311 {
312 struct dma_softc *dsc;
313 struct dma_devinfo *ddi;
314 struct sbus_regs *reg;
315 uint32_t base, iv, *intr;
316 int i, nreg, nintr, slot, rslot;
317
318 dsc = device_get_softc(dev);
319
320 ddi = malloc(sizeof(*ddi), M_DEVBUF, M_WAITOK | M_ZERO);
321 if (ddi == NULL)
322 return (NULL);
323 resource_list_init(&ddi->ddi_rl);
324 ddi->ddi_name = name;
325 ddi->ddi_node = node;
326 OF_getprop_alloc(node, "compatible", 1, (void **)&ddi->ddi_compat);
327 OF_getprop_alloc(node, "device_type", 1, (void **)&ddi->ddi_type);
328 OF_getprop_alloc(node, "model", 1, (void **)&ddi->ddi_model);
329 slot = -1;
330 nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)®);
331 if (nreg == -1) {
332 dma_destroy_dinfo(ddi);
333 return (NULL);
334 }
335 for (i = 0; i < nreg; i++) {
336 base = reg[i].sbr_offset;
337 if (SBUS_ABS(base)) {
338 rslot = SBUS_ABS_TO_SLOT(base);
339 base = SBUS_ABS_TO_OFFSET(base);
340 } else
341 rslot = reg[i].sbr_slot;
342 if (slot != -1 && slot != rslot) {
343 device_printf(dev, "<%s>: multiple slots\n", name);
344 free(reg, M_OFWPROP);
345 dma_destroy_dinfo(ddi);
346 return (NULL);
347 }
348 slot = rslot;
349
350 resource_list_add(&ddi->ddi_rl, SYS_RES_MEMORY, i, base,
351 base + reg[i].sbr_size, reg[i].sbr_size);
352 }
353 free(reg, M_OFWPROP);
354 if (slot != dsc->sc_slot) {
355 device_printf(dev, "<%s>: parent and child slot do not match\n",
356 name);
357 dma_destroy_dinfo(ddi);
358 return (NULL);
359 }
360
361 /*
362 * The `interrupts' property contains the SBus interrupt level.
363 */
364 nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intr),
365 (void **)&intr);
366 if (nintr != -1) {
367 for (i = 0; i < nintr; i++) {
368 iv = intr[i];
369 /*
370 * SBus card devices need the slot number encoded into
371 * the vector as this is generally not done.
372 */
373 if ((iv & INTMAP_OBIO_MASK) == 0)
374 iv |= slot << 3;
375 /* Set the IGN as appropriate. */
376 iv |= dsc->sc_ign << INTMAP_IGN_SHIFT;
377 resource_list_add(&ddi->ddi_rl, SYS_RES_IRQ, i,
378 iv, iv, 1);
379 }
380 free(intr, M_OFWPROP);
381 }
382 return (ddi);
383 }
384
385 static void
386 dma_destroy_dinfo(struct dma_devinfo *dinfo)
387 {
388
389 resource_list_free(&dinfo->ddi_rl);
390 if (dinfo->ddi_compat != NULL)
391 free(dinfo->ddi_compat, M_OFWPROP);
392 if (dinfo->ddi_model != NULL)
393 free(dinfo->ddi_model, M_OFWPROP);
394 if (dinfo->ddi_type != NULL)
395 free(dinfo->ddi_type, M_OFWPROP);
396 free(dinfo, M_DEVBUF);
397 }
398
399 static int
400 dma_print_child(device_t dev, device_t child)
401 {
402 struct dma_devinfo *ddi;
403 struct resource_list *rl;
404 int rv;
405
406 ddi = device_get_ivars(child);
407 rl = &ddi->ddi_rl;
408 rv = bus_print_child_header(dev, child);
409 rv += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
410 rv += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
411 rv += bus_print_child_footer(dev, child);
412 return (rv);
413 }
414
415 static void
416 dma_probe_nomatch(device_t dev, device_t child)
417 {
418 struct dma_devinfo *ddi;
419 struct resource_list *rl;
420
421 ddi = device_get_ivars(child);
422 rl = &ddi->ddi_rl;
423 device_printf(dev, "<%s>", ddi->ddi_name);
424 resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
425 resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
426 printf(" type %s (no driver attached)\n",
427 ddi->ddi_type != NULL ? ddi->ddi_type : "unknown");
428 }
429
430 static struct resource_list *
431 dma_get_resource_list(device_t dev, device_t child)
432 {
433 struct dma_devinfo *ddi;
434
435 ddi = device_get_ivars(child);
436 return (&ddi->ddi_rl);
437 }
438
439 static const char *
440 dma_get_compat(device_t bus, device_t dev)
441 {
442 struct dma_devinfo *dinfo;
443
444 dinfo = device_get_ivars(dev);
445 return (dinfo->ddi_compat);
446 }
447
448 static const char *
449 dma_get_model(device_t bus, device_t dev)
450 {
451 struct dma_devinfo *dinfo;
452
453 dinfo = device_get_ivars(dev);
454 return (dinfo->ddi_model);
455 }
456
457 static const char *
458 dma_get_name(device_t bus, device_t dev)
459 {
460 struct dma_devinfo *dinfo;
461
462 dinfo = device_get_ivars(dev);
463 return (dinfo->ddi_name);
464 }
465
466 static phandle_t
467 dma_get_node(device_t bus, device_t dev)
468 {
469 struct dma_devinfo *dinfo;
470
471 dinfo = device_get_ivars(dev);
472 return (dinfo->ddi_node);
473 }
474
475 static const char *
476 dma_get_type(device_t bus, device_t dev)
477 {
478 struct dma_devinfo *dinfo;
479
480 dinfo = device_get_ivars(dev);
481 return (dinfo->ddi_type);
482 }
Cache object: 9a3fb676943b7258b326b078bbd61ecf
|