1 /* $OpenBSD: dma_sbus.c,v 1.16 2008/06/26 05:42:18 ray Exp $ */
2 /* $NetBSD: dma_sbus.c,v 1.32 2008/04/28 20:23:57 martin Exp $ */
5 * SPDX-License-Identifier: BSD-2-Clause-NetBSD AND BSD-2-Clause-FreeBSD
7 * Copyright (c) 1998 The NetBSD Foundation, Inc.
10 * This code is derived from software contributed to The NetBSD Foundation
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
36 * Copyright (c) 1994 Peter Galbavy. All rights reserved.
37 * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>. All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
48 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
52 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
57 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
63 #include <sys/param.h>
64 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/module.h>
70 #include <dev/ofw/ofw_bus.h>
71 #include <dev/ofw/ofw_bus_subr.h>
72 #include <dev/ofw/openfirm.h>
74 #include <machine/bus.h>
75 #include <machine/bus_common.h>
76 #include <machine/resource.h>
78 #include <sparc64/sbus/lsi64854reg.h>
79 #include <sparc64/sbus/lsi64854var.h>
80 #include <sparc64/sbus/ofw_sbus.h>
81 #include <sparc64/sbus/sbusreg.h>
82 #include <sparc64/sbus/sbusvar.h>
85 struct ofw_bus_devinfo ddi_obdinfo;
86 struct resource_list ddi_rl;
90 struct lsi64854_softc sc_lsi64854; /* base device */
95 static devclass_t dma_devclass;
97 static device_probe_t dma_probe;
98 static device_attach_t dma_attach;
99 static bus_print_child_t dma_print_child;
100 static bus_probe_nomatch_t dma_probe_nomatch;
101 static bus_get_resource_list_t dma_get_resource_list;
102 static ofw_bus_get_devinfo_t dma_get_devinfo;
104 static struct dma_devinfo *dma_setup_dinfo(device_t, struct dma_softc *,
106 static void dma_destroy_dinfo(struct dma_devinfo *);
107 static int dma_print_res(struct dma_devinfo *);
109 static device_method_t dma_methods[] = {
110 /* Device interface */
111 DEVMETHOD(device_probe, dma_probe),
112 DEVMETHOD(device_attach, dma_attach),
113 DEVMETHOD(device_shutdown, bus_generic_shutdown),
114 DEVMETHOD(device_suspend, bus_generic_suspend),
115 DEVMETHOD(device_resume, bus_generic_resume),
118 DEVMETHOD(bus_print_child, dma_print_child),
119 DEVMETHOD(bus_probe_nomatch, dma_probe_nomatch),
120 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
121 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
122 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
123 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
124 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
125 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
126 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
127 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
128 DEVMETHOD(bus_get_resource_list, dma_get_resource_list),
129 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
131 /* ofw_bus interface */
132 DEVMETHOD(ofw_bus_get_devinfo, dma_get_devinfo),
133 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
134 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
135 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
136 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
137 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
142 static driver_t dma_driver = {
145 sizeof(struct dma_softc),
149 * The probe order is handled by sbus(4) as we don't want the variants
150 * with children to be attached earlier than the stand-alone controllers
151 * in order to generally preserve the OFW device tree order.
153 EARLY_DRIVER_MODULE(dma, sbus, dma_driver, dma_devclass, 0, 0,
155 MODULE_DEPEND(dma, sbus, 1, 1, 1);
156 MODULE_VERSION(dma, 1);
159 dma_probe(device_t dev)
163 name = ofw_bus_get_name(dev);
164 if (strcmp(name, "espdma") == 0 || strcmp(name, "dma") == 0 ||
165 strcmp(name, "ledma") == 0) {
166 device_set_desc_copy(dev, name);
173 dma_attach(device_t dev)
175 struct dma_softc *dsc;
176 struct lsi64854_softc *lsc;
177 struct dma_devinfo *ddi;
182 phandle_t child, node;
185 dsc = device_get_softc(dev);
186 lsc = &dsc->sc_lsi64854;
188 name = ofw_bus_get_name(dev);
189 node = ofw_bus_get_node(dev);
190 dsc->sc_ign = sbus_get_ign(dev);
191 dsc->sc_slot = sbus_get_slot(dev);
194 lsc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i,
196 if (lsc->sc_res == NULL) {
197 device_printf(dev, "cannot allocate resources\n");
201 if (strcmp(name, "espdma") == 0 || strcmp(name, "dma") == 0)
202 lsc->sc_channel = L64854_CHANNEL_SCSI;
203 else if (strcmp(name, "ledma") == 0) {
205 * Check to see which cable type is currently active and
206 * set the appropriate bit in the ledma csr so that it
207 * gets used. If we didn't netboot, the PROM won't have
208 * the "cable-selection" property; default to TP and then
209 * the user can change it via a "media" option to ifconfig.
211 csr = L64854_GCSR(lsc);
212 if ((OF_getprop_alloc(node, "cable-selection",
213 (void **)&cabletype)) == -1) {
214 /* assume TP if nothing there */
217 if (strcmp(cabletype, "aui") == 0)
221 OF_prop_free(cabletype);
223 L64854_SCSR(lsc, csr);
224 DELAY(20000); /* manual says we need a 20ms delay */
225 lsc->sc_channel = L64854_CHANNEL_ENET;
227 device_printf(dev, "unsupported DMA channel\n");
232 error = bus_dma_tag_create(
233 bus_get_dma_tag(dev), /* parent */
234 1, 0, /* alignment, boundary */
235 BUS_SPACE_MAXADDR, /* lowaddr */
236 BUS_SPACE_MAXADDR, /* highaddr */
237 NULL, NULL, /* filter, filterarg */
238 BUS_SPACE_MAXSIZE, /* maxsize */
239 BUS_SPACE_UNRESTRICTED, /* nsegments */
240 BUS_SPACE_MAXSIZE, /* maxsegsize */
242 NULL, NULL, /* no locking */
243 &lsc->sc_parent_dmat);
245 device_printf(dev, "cannot allocate parent DMA tag\n");
249 i = sbus_get_burstsz(dev);
250 lsc->sc_burst = (i & SBUS_BURST_32) ? 32 :
251 (i & SBUS_BURST_16) ? 16 : 0;
254 /* Attach children. */
256 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
257 if ((ddi = dma_setup_dinfo(dev, dsc, child)) == NULL)
261 "<%s>: only one child per DMA channel supported\n",
262 ddi->ddi_obdinfo.obd_name);
263 dma_destroy_dinfo(ddi);
266 if ((cdev = device_add_child(dev, NULL, -1)) == NULL) {
267 device_printf(dev, "<%s>: device_add_child failed\n",
268 ddi->ddi_obdinfo.obd_name);
269 dma_destroy_dinfo(ddi);
272 device_set_ivars(cdev, ddi);
275 return (bus_generic_attach(dev));
278 bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(lsc->sc_res),
283 static struct dma_devinfo *
284 dma_setup_dinfo(device_t dev, struct dma_softc *dsc, phandle_t node)
286 struct dma_devinfo *ddi;
287 struct sbus_regs *reg;
288 uint32_t base, iv, *intr;
289 int i, nreg, nintr, slot, rslot;
291 ddi = malloc(sizeof(*ddi), M_DEVBUF, M_WAITOK | M_ZERO);
292 if (ofw_bus_gen_setup_devinfo(&ddi->ddi_obdinfo, node) != 0) {
296 resource_list_init(&ddi->ddi_rl);
298 nreg = OF_getprop_alloc_multi(node, "reg", sizeof(*reg), (void **)®);
300 device_printf(dev, "<%s>: incomplete\n",
301 ddi->ddi_obdinfo.obd_name);
304 for (i = 0; i < nreg; i++) {
305 base = reg[i].sbr_offset;
306 if (SBUS_ABS(base)) {
307 rslot = SBUS_ABS_TO_SLOT(base);
308 base = SBUS_ABS_TO_OFFSET(base);
310 rslot = reg[i].sbr_slot;
311 if (slot != -1 && slot != rslot) {
312 device_printf(dev, "<%s>: multiple slots\n",
313 ddi->ddi_obdinfo.obd_name);
319 resource_list_add(&ddi->ddi_rl, SYS_RES_MEMORY, i, base,
320 base + reg[i].sbr_size, reg[i].sbr_size);
323 if (slot != dsc->sc_slot) {
324 device_printf(dev, "<%s>: parent and child slot do not match\n",
325 ddi->ddi_obdinfo.obd_name);
330 * The `interrupts' property contains the SBus interrupt level.
332 nintr = OF_getprop_alloc_multi(node, "interrupts", sizeof(*intr),
335 for (i = 0; i < nintr; i++) {
338 * SBus card devices need the slot number encoded into
339 * the vector as this is generally not done.
341 if ((iv & INTMAP_OBIO_MASK) == 0)
343 /* Set the IGN as appropriate. */
344 iv |= dsc->sc_ign << INTMAP_IGN_SHIFT;
345 resource_list_add(&ddi->ddi_rl, SYS_RES_IRQ, i,
353 dma_destroy_dinfo(ddi);
358 dma_destroy_dinfo(struct dma_devinfo *dinfo)
361 resource_list_free(&dinfo->ddi_rl);
362 ofw_bus_gen_destroy_devinfo(&dinfo->ddi_obdinfo);
363 free(dinfo, M_DEVBUF);
367 dma_print_child(device_t dev, device_t child)
371 rv = bus_print_child_header(dev, child);
372 rv += dma_print_res(device_get_ivars(child));
373 rv += bus_print_child_footer(dev, child);
378 dma_probe_nomatch(device_t dev, device_t child)
382 device_printf(dev, "<%s>", ofw_bus_get_name(child));
383 dma_print_res(device_get_ivars(child));
384 type = ofw_bus_get_type(child);
385 printf(" type %s (no driver attached)\n",
386 type != NULL ? type : "unknown");
389 static struct resource_list *
390 dma_get_resource_list(device_t dev, device_t child)
392 struct dma_devinfo *ddi;
394 ddi = device_get_ivars(child);
395 return (&ddi->ddi_rl);
398 static const struct ofw_bus_devinfo *
399 dma_get_devinfo(device_t bus, device_t child)
401 struct dma_devinfo *ddi;
403 ddi = device_get_ivars(child);
404 return (&ddi->ddi_obdinfo);
408 dma_print_res(struct dma_devinfo *ddi)
413 rv += resource_list_print_type(&ddi->ddi_rl, "mem", SYS_RES_MEMORY,
415 rv += resource_list_print_type(&ddi->ddi_rl, "irq", SYS_RES_IRQ, "%jd");