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 $ */
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
41 * Copyright (c) 1994 Peter Galbavy. All rights reserved.
42 * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>. All rights reserved.
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
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.
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.
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
68 #include <sys/param.h>
69 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/module.h>
75 #include <dev/ofw/ofw_bus.h>
76 #include <dev/ofw/ofw_bus_subr.h>
77 #include <dev/ofw/openfirm.h>
79 #include <machine/bus.h>
80 #include <machine/bus_common.h>
81 #include <machine/resource.h>
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>
90 struct ofw_bus_devinfo ddi_obdinfo;
91 struct resource_list ddi_rl;
95 struct lsi64854_softc sc_lsi64854; /* base device */
100 static devclass_t dma_devclass;
102 static device_probe_t dma_probe;
103 static device_attach_t dma_attach;
104 static bus_print_child_t dma_print_child;
105 static bus_probe_nomatch_t dma_probe_nomatch;
106 static bus_get_resource_list_t dma_get_resource_list;
107 static ofw_bus_get_devinfo_t dma_get_devinfo;
109 static struct dma_devinfo *dma_setup_dinfo(device_t, struct dma_softc *,
111 static void dma_destroy_dinfo(struct dma_devinfo *);
112 static int dma_print_res(struct dma_devinfo *);
114 static device_method_t dma_methods[] = {
115 /* Device interface */
116 DEVMETHOD(device_probe, dma_probe),
117 DEVMETHOD(device_attach, dma_attach),
118 DEVMETHOD(device_shutdown, bus_generic_shutdown),
119 DEVMETHOD(device_suspend, bus_generic_suspend),
120 DEVMETHOD(device_resume, bus_generic_resume),
123 DEVMETHOD(bus_print_child, dma_print_child),
124 DEVMETHOD(bus_probe_nomatch, dma_probe_nomatch),
125 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
126 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
127 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
128 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
129 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
130 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
131 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
132 DEVMETHOD(bus_get_resource_list, dma_get_resource_list),
133 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
135 /* ofw_bus interface */
136 DEVMETHOD(ofw_bus_get_devinfo, dma_get_devinfo),
137 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
138 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
139 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
140 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
141 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
146 static driver_t dma_driver = {
149 sizeof(struct dma_softc),
153 * The probe order is handled by sbus(4) as we don't want the variants
154 * with children to be attached earlier than the stand-alone controllers
155 * in order to generally preserve the OFW device tree order.
157 EARLY_DRIVER_MODULE(dma, sbus, dma_driver, dma_devclass, 0, 0,
159 MODULE_DEPEND(dma, sbus, 1, 1, 1);
160 MODULE_VERSION(dma, 1);
163 dma_probe(device_t dev)
167 name = ofw_bus_get_name(dev);
168 if (strcmp(name, "espdma") == 0 || strcmp(name, "dma") == 0 ||
169 strcmp(name, "ledma") == 0) {
170 device_set_desc_copy(dev, name);
177 dma_attach(device_t dev)
179 struct dma_softc *dsc;
180 struct lsi64854_softc *lsc;
181 struct dma_devinfo *ddi;
186 phandle_t child, node;
189 dsc = device_get_softc(dev);
190 lsc = &dsc->sc_lsi64854;
192 name = ofw_bus_get_name(dev);
193 node = ofw_bus_get_node(dev);
194 dsc->sc_ign = sbus_get_ign(dev);
195 dsc->sc_slot = sbus_get_slot(dev);
198 lsc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i,
200 if (lsc->sc_res == NULL) {
201 device_printf(dev, "cannot allocate resources\n");
205 if (strcmp(name, "espdma") == 0 || strcmp(name, "dma") == 0)
206 lsc->sc_channel = L64854_CHANNEL_SCSI;
207 else if (strcmp(name, "ledma") == 0) {
209 * Check to see which cable type is currently active and
210 * set the appropriate bit in the ledma csr so that it
211 * gets used. If we didn't netboot, the PROM won't have
212 * the "cable-selection" property; default to TP and then
213 * the user can change it via a "media" option to ifconfig.
215 csr = L64854_GCSR(lsc);
216 if ((OF_getprop_alloc(node, "cable-selection", 1,
217 (void **)&cabletype)) == -1) {
218 /* assume TP if nothing there */
221 if (strcmp(cabletype, "aui") == 0)
225 free(cabletype, M_OFWPROP);
227 L64854_SCSR(lsc, csr);
228 DELAY(20000); /* manual says we need a 20ms delay */
229 lsc->sc_channel = L64854_CHANNEL_ENET;
231 device_printf(dev, "unsupported DMA channel\n");
236 error = bus_dma_tag_create(
237 bus_get_dma_tag(dev), /* parent */
238 1, 0, /* alignment, boundary */
239 BUS_SPACE_MAXADDR, /* lowaddr */
240 BUS_SPACE_MAXADDR, /* highaddr */
241 NULL, NULL, /* filter, filterarg */
242 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
244 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
246 NULL, NULL, /* no locking */
247 &lsc->sc_parent_dmat);
249 device_printf(dev, "cannot allocate parent DMA tag\n");
253 i = sbus_get_burstsz(dev);
254 lsc->sc_burst = (i & SBUS_BURST_32) ? 32 :
255 (i & SBUS_BURST_16) ? 16 : 0;
258 /* Attach children. */
260 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
261 if ((ddi = dma_setup_dinfo(dev, dsc, child)) == NULL)
265 "<%s>: only one child per DMA channel supported\n",
266 ddi->ddi_obdinfo.obd_name);
267 dma_destroy_dinfo(ddi);
270 if ((cdev = device_add_child(dev, NULL, -1)) == NULL) {
271 device_printf(dev, "<%s>: device_add_child failed\n",
272 ddi->ddi_obdinfo.obd_name);
273 dma_destroy_dinfo(ddi);
276 device_set_ivars(cdev, ddi);
279 return (bus_generic_attach(dev));
282 bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(lsc->sc_res),
287 static struct dma_devinfo *
288 dma_setup_dinfo(device_t dev, struct dma_softc *dsc, phandle_t node)
290 struct dma_devinfo *ddi;
291 struct sbus_regs *reg;
292 uint32_t base, iv, *intr;
293 int i, nreg, nintr, slot, rslot;
295 ddi = malloc(sizeof(*ddi), M_DEVBUF, M_WAITOK | M_ZERO);
296 if (ofw_bus_gen_setup_devinfo(&ddi->ddi_obdinfo, node) != 0) {
300 resource_list_init(&ddi->ddi_rl);
302 nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)®);
304 device_printf(dev, "<%s>: incomplete\n",
305 ddi->ddi_obdinfo.obd_name);
308 for (i = 0; i < nreg; i++) {
309 base = reg[i].sbr_offset;
310 if (SBUS_ABS(base)) {
311 rslot = SBUS_ABS_TO_SLOT(base);
312 base = SBUS_ABS_TO_OFFSET(base);
314 rslot = reg[i].sbr_slot;
315 if (slot != -1 && slot != rslot) {
316 device_printf(dev, "<%s>: multiple slots\n",
317 ddi->ddi_obdinfo.obd_name);
318 free(reg, M_OFWPROP);
323 resource_list_add(&ddi->ddi_rl, SYS_RES_MEMORY, i, base,
324 base + reg[i].sbr_size, reg[i].sbr_size);
326 free(reg, M_OFWPROP);
327 if (slot != dsc->sc_slot) {
328 device_printf(dev, "<%s>: parent and child slot do not match\n",
329 ddi->ddi_obdinfo.obd_name);
334 * The `interrupts' property contains the SBus interrupt level.
336 nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intr),
339 for (i = 0; i < nintr; i++) {
342 * SBus card devices need the slot number encoded into
343 * the vector as this is generally not done.
345 if ((iv & INTMAP_OBIO_MASK) == 0)
347 /* Set the IGN as appropriate. */
348 iv |= dsc->sc_ign << INTMAP_IGN_SHIFT;
349 resource_list_add(&ddi->ddi_rl, SYS_RES_IRQ, i,
352 free(intr, M_OFWPROP);
357 dma_destroy_dinfo(ddi);
362 dma_destroy_dinfo(struct dma_devinfo *dinfo)
365 resource_list_free(&dinfo->ddi_rl);
366 ofw_bus_gen_destroy_devinfo(&dinfo->ddi_obdinfo);
367 free(dinfo, M_DEVBUF);
371 dma_print_child(device_t dev, device_t child)
375 rv = bus_print_child_header(dev, child);
376 rv += dma_print_res(device_get_ivars(child));
377 rv += bus_print_child_footer(dev, child);
382 dma_probe_nomatch(device_t dev, device_t child)
386 device_printf(dev, "<%s>", ofw_bus_get_name(child));
387 dma_print_res(device_get_ivars(child));
388 type = ofw_bus_get_type(child);
389 printf(" type %s (no driver attached)\n",
390 type != NULL ? type : "unknown");
393 static struct resource_list *
394 dma_get_resource_list(device_t dev, device_t child)
396 struct dma_devinfo *ddi;
398 ddi = device_get_ivars(child);
399 return (&ddi->ddi_rl);
402 static const struct ofw_bus_devinfo *
403 dma_get_devinfo(device_t bus, device_t child)
405 struct dma_devinfo *ddi;
407 ddi = device_get_ivars(child);
408 return (&ddi->ddi_obdinfo);
412 dma_print_res(struct dma_devinfo *ddi)
417 rv += resource_list_print_type(&ddi->ddi_rl, "mem", SYS_RES_MEMORY,
419 rv += resource_list_print_type(&ddi->ddi_rl, "irq", SYS_RES_IRQ, "%ld");