dma_sbus.c revision 200874
1146392Smarius/*	$OpenBSD: dma_sbus.c,v 1.12 2005/03/03 01:41:45 miod Exp $	*/
2146392Smarius/*	$NetBSD: dma_sbus.c,v 1.5 2000/07/09 20:57:42 pk Exp $ */
3146392Smarius
4146392Smarius/*-
5146392Smarius * Copyright (c) 1998 The NetBSD Foundation, Inc.
6146392Smarius * All rights reserved.
7146392Smarius *
8146392Smarius * This code is derived from software contributed to The NetBSD Foundation
9146392Smarius * by Paul Kranenburg.
10146392Smarius *
11146392Smarius * Redistribution and use in source and binary forms, with or without
12146392Smarius * modification, are permitted provided that the following conditions
13146392Smarius * are met:
14146392Smarius * 1. Redistributions of source code must retain the above copyright
15146392Smarius *    notice, this list of conditions and the following disclaimer.
16146392Smarius * 2. Redistributions in binary form must reproduce the above copyright
17146392Smarius *    notice, this list of conditions and the following disclaimer in the
18146392Smarius *    documentation and/or other materials provided with the distribution.
19146392Smarius * 3. All advertising materials mentioning features or use of this software
20146392Smarius *    must display the following acknowledgement:
21146392Smarius *        This product includes software developed by the NetBSD
22146392Smarius *        Foundation, Inc. and its contributors.
23146392Smarius * 4. Neither the name of The NetBSD Foundation nor the names of its
24146392Smarius *    contributors may be used to endorse or promote products derived
25146392Smarius *    from this software without specific prior written permission.
26146392Smarius *
27146392Smarius * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28146392Smarius * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29146392Smarius * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30146392Smarius * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31146392Smarius * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32146392Smarius * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33146392Smarius * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34146392Smarius * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35146392Smarius * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36146392Smarius * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37146392Smarius * POSSIBILITY OF SUCH DAMAGE.
38146392Smarius */
39146392Smarius
40146392Smarius/*-
41146392Smarius * Copyright (c) 1994 Peter Galbavy.  All rights reserved.
42146392Smarius * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>. All rights reserved.
43146392Smarius *
44146392Smarius * Redistribution and use in source and binary forms, with or without
45146392Smarius * modification, are permitted provided that the following conditions
46146392Smarius * are met:
47146392Smarius * 1. Redistributions of source code must retain the above copyright
48146392Smarius *    notice, this list of conditions and the following disclaimer.
49146392Smarius * 2. Redistributions in binary form must reproduce the above copyright
50146392Smarius *    notice, this list of conditions and the following disclaimer in the
51146392Smarius *    documentation and/or other materials provided with the distribution.
52146392Smarius *
53146392Smarius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
54146392Smarius * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
55146392Smarius * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
56146392Smarius * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
57146392Smarius * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
58146392Smarius * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59146392Smarius * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60146392Smarius * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61146392Smarius * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
62146392Smarius * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63146392Smarius */
64146392Smarius
65146392Smarius#include <sys/cdefs.h>
66146392Smarius__FBSDID("$FreeBSD: head/sys/sparc64/sbus/dma_sbus.c 200874 2009-12-22 21:02:46Z marius $");
67146392Smarius
68146392Smarius#include <sys/param.h>
69146392Smarius#include <sys/systm.h>
70146392Smarius#include <sys/bus.h>
71146392Smarius#include <sys/kernel.h>
72146392Smarius#include <sys/module.h>
73146392Smarius#include <sys/rman.h>
74146392Smarius
75146392Smarius#include <dev/ofw/ofw_bus.h>
76152684Smarius#include <dev/ofw/ofw_bus_subr.h>
77146392Smarius#include <dev/ofw/openfirm.h>
78146392Smarius
79146392Smarius#include <machine/bus.h>
80146392Smarius#include <machine/bus_common.h>
81146392Smarius#include <machine/resource.h>
82146392Smarius
83146392Smarius#include <sparc64/sbus/lsi64854reg.h>
84146392Smarius#include <sparc64/sbus/lsi64854var.h>
85146392Smarius#include <sparc64/sbus/ofw_sbus.h>
86146392Smarius#include <sparc64/sbus/sbusreg.h>
87146392Smarius#include <sparc64/sbus/sbusvar.h>
88146392Smarius
89146392Smariusstruct dma_devinfo {
90152684Smarius	struct ofw_bus_devinfo	ddi_obdinfo;
91146392Smarius	struct resource_list	ddi_rl;
92146392Smarius};
93146392Smarius
94146392Smariusstruct dma_softc {
95146392Smarius	struct lsi64854_softc	sc_lsi64854;	/* base device */
96146392Smarius	int			sc_ign;
97146392Smarius	int			sc_slot;
98146392Smarius};
99146392Smarius
100146392Smariusstatic devclass_t dma_devclass;
101146392Smarius
102146392Smariusstatic device_probe_t dma_probe;
103146392Smariusstatic device_attach_t dma_attach;
104146392Smariusstatic bus_print_child_t dma_print_child;
105146392Smariusstatic bus_probe_nomatch_t dma_probe_nomatch;
106146392Smariusstatic bus_get_resource_list_t dma_get_resource_list;
107152684Smariusstatic ofw_bus_get_devinfo_t dma_get_devinfo;
108146392Smarius
109152684Smariusstatic struct dma_devinfo *dma_setup_dinfo(device_t, struct dma_softc *,
110152684Smarius    phandle_t);
111146392Smariusstatic void dma_destroy_dinfo(struct dma_devinfo *);
112152684Smariusstatic int dma_print_res(struct dma_devinfo *);
113146392Smarius
114146392Smariusstatic device_method_t dma_methods[] = {
115166147Smarius	/* Device interface */
116146392Smarius	DEVMETHOD(device_probe,		dma_probe),
117146392Smarius	DEVMETHOD(device_attach,	dma_attach),
118154870Smarius	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
119154870Smarius	DEVMETHOD(device_suspend,	bus_generic_suspend),
120154870Smarius	DEVMETHOD(device_resume,	bus_generic_resume),
121146392Smarius
122146392Smarius	/* Bus interface */
123146392Smarius	DEVMETHOD(bus_print_child,	dma_print_child),
124146392Smarius	DEVMETHOD(bus_probe_nomatch,	dma_probe_nomatch),
125146392Smarius	DEVMETHOD(bus_alloc_resource,	bus_generic_rl_alloc_resource),
126146392Smarius	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
127146392Smarius	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
128190099Smarius	DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
129190099Smarius	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
130190099Smarius	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
131190099Smarius	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
132146392Smarius	DEVMETHOD(bus_get_resource_list, dma_get_resource_list),
133190114Smarius	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
134146392Smarius
135146392Smarius	/* ofw_bus interface */
136152684Smarius	DEVMETHOD(ofw_bus_get_devinfo,	dma_get_devinfo),
137152684Smarius	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
138152684Smarius	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
139152684Smarius	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
140152684Smarius	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
141152684Smarius	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
142146392Smarius
143190099Smarius	KOBJMETHOD_END
144146392Smarius};
145146392Smarius
146146392Smariusstatic driver_t dma_driver = {
147146392Smarius	"dma",
148146392Smarius	dma_methods,
149146392Smarius	sizeof(struct dma_softc),
150146392Smarius};
151146392Smarius
152200874Smarius/*
153200874Smarius * The probe order is handled by sbus(4) as we don't want the variants
154200874Smarius * with children to be attached earlier than the stand-alone controllers
155200874Smarius * in order to generally preserve the OFW device tree order.
156200874Smarius */
157200874SmariusEARLY_DRIVER_MODULE(dma, sbus, dma_driver, dma_devclass, 0, 0,
158200874Smarius    BUS_PASS_DEFAULT);
159182062SmariusMODULE_DEPEND(dma, sbus, 1, 1, 1);
160182062SmariusMODULE_VERSION(dma, 1);
161146392Smarius
162146392Smariusstatic int
163146392Smariusdma_probe(device_t dev)
164146392Smarius{
165146392Smarius	const char *name;
166146392Smarius
167146392Smarius	name = ofw_bus_get_name(dev);
168146392Smarius	if (strcmp(name, "espdma") == 0 || strcmp(name, "dma") == 0 ||
169146392Smarius	    strcmp(name, "ledma") == 0) {
170166147Smarius		device_set_desc_copy(dev, name);
171166147Smarius		return (0);
172146392Smarius	}
173166147Smarius	return (ENXIO);
174146392Smarius}
175146392Smarius
176146392Smariusstatic int
177146392Smariusdma_attach(device_t dev)
178146392Smarius{
179146392Smarius	struct dma_softc *dsc;
180146392Smarius	struct lsi64854_softc *lsc;
181146392Smarius	struct dma_devinfo *ddi;
182146392Smarius	device_t cdev;
183146392Smarius	const char *name;
184152684Smarius	char *cabletype;
185146392Smarius	uint32_t csr;
186146392Smarius	phandle_t child, node;
187182876Smarius	int error, i;
188146392Smarius
189146392Smarius	dsc = device_get_softc(dev);
190146392Smarius	lsc = &dsc->sc_lsi64854;
191146392Smarius
192146392Smarius	name = ofw_bus_get_name(dev);
193146392Smarius	node = ofw_bus_get_node(dev);
194146392Smarius	dsc->sc_ign = sbus_get_ign(dev);
195146392Smarius	dsc->sc_slot = sbus_get_slot(dev);
196146392Smarius
197182876Smarius	i = 0;
198182876Smarius	lsc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i,
199146392Smarius	    RF_ACTIVE);
200146392Smarius	if (lsc->sc_res == NULL) {
201146392Smarius		device_printf(dev, "cannot allocate resources\n");
202146392Smarius		return (ENXIO);
203146392Smarius	}
204146392Smarius
205146392Smarius	if (strcmp(name, "espdma") == 0 || strcmp(name, "dma") == 0)
206146392Smarius		lsc->sc_channel = L64854_CHANNEL_SCSI;
207146392Smarius	else if (strcmp(name, "ledma") == 0) {
208146392Smarius		/*
209146392Smarius		 * Check to see which cable type is currently active and
210146392Smarius		 * set the appropriate bit in the ledma csr so that it
211146392Smarius		 * gets used. If we didn't netboot, the PROM won't have
212146392Smarius		 * the "cable-selection" property; default to TP and then
213146392Smarius		 * the user can change it via a "media" option to ifconfig.
214146392Smarius		 */
215146392Smarius		csr = L64854_GCSR(lsc);
216146392Smarius		if ((OF_getprop_alloc(node, "cable-selection", 1,
217146392Smarius		    (void **)&cabletype)) == -1) {
218146392Smarius			/* assume TP if nothing there */
219146392Smarius			csr |= E_TP_AUI;
220146392Smarius		} else {
221146392Smarius			if (strcmp(cabletype, "aui") == 0)
222146392Smarius				csr &= ~E_TP_AUI;
223146392Smarius			else
224146392Smarius				csr |= E_TP_AUI;
225146392Smarius			free(cabletype, M_OFWPROP);
226146392Smarius		}
227146392Smarius		L64854_SCSR(lsc, csr);
228146392Smarius		DELAY(20000);	/* manual says we need a 20ms delay */
229146392Smarius		lsc->sc_channel = L64854_CHANNEL_ENET;
230146392Smarius	} else {
231146392Smarius		device_printf(dev, "unsupported DMA channel\n");
232146392Smarius		error = ENXIO;
233146392Smarius		goto fail_lres;
234146392Smarius	}
235146392Smarius
236146392Smarius	error = bus_dma_tag_create(
237166147Smarius	    bus_get_dma_tag(dev),	/* parent */
238166147Smarius	    1, 0,			/* alignment, boundary */
239146392Smarius	    BUS_SPACE_MAXADDR,		/* lowaddr */
240146392Smarius	    BUS_SPACE_MAXADDR,		/* highaddr */
241146392Smarius	    NULL, NULL,			/* filter, filterarg */
242146392Smarius	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
243146392Smarius	    0,				/* nsegments */
244146392Smarius	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
245146392Smarius	    0,				/* flags */
246146392Smarius	    NULL, NULL,			/* no locking */
247146392Smarius	    &lsc->sc_parent_dmat);
248146392Smarius	if (error != 0) {
249146392Smarius		device_printf(dev, "cannot allocate parent DMA tag\n");
250146392Smarius		goto fail_lres;
251146392Smarius	}
252146392Smarius
253182876Smarius	i = sbus_get_burstsz(dev);
254182876Smarius	lsc->sc_burst = (i & SBUS_BURST_32) ? 32 :
255182876Smarius	    (i & SBUS_BURST_16) ? 16 : 0;
256146392Smarius	lsc->sc_dev = dev;
257146392Smarius
258146392Smarius	/* Attach children. */
259182876Smarius	i = 0;
260146392Smarius	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
261152684Smarius		if ((ddi = dma_setup_dinfo(dev, dsc, child)) == NULL)
262146392Smarius			continue;
263182876Smarius		if (i != 0) {
264152684Smarius			device_printf(dev,
265152684Smarius			    "<%s>: only one child per DMA channel supported\n",
266152684Smarius			    ddi->ddi_obdinfo.obd_name);
267146392Smarius			dma_destroy_dinfo(ddi);
268146392Smarius			continue;
269146392Smarius		}
270146392Smarius		if ((cdev = device_add_child(dev, NULL, -1)) == NULL) {
271146392Smarius			device_printf(dev, "<%s>: device_add_child failed\n",
272152684Smarius			    ddi->ddi_obdinfo.obd_name);
273146392Smarius			dma_destroy_dinfo(ddi);
274146392Smarius			continue;
275146392Smarius		}
276146392Smarius		device_set_ivars(cdev, ddi);
277182876Smarius		i++;
278146392Smarius	}
279152684Smarius	return (bus_generic_attach(dev));
280146392Smarius
281146392Smarius fail_lres:
282182876Smarius	bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(lsc->sc_res),
283182876Smarius	    lsc->sc_res);
284146392Smarius	return (error);
285146392Smarius}
286146392Smarius
287146392Smariusstatic struct dma_devinfo *
288152684Smariusdma_setup_dinfo(device_t dev, struct dma_softc *dsc, phandle_t node)
289146392Smarius{
290146392Smarius	struct dma_devinfo *ddi;
291146392Smarius	struct sbus_regs *reg;
292146392Smarius	uint32_t base, iv, *intr;
293146392Smarius	int i, nreg, nintr, slot, rslot;
294146392Smarius
295146392Smarius	ddi = malloc(sizeof(*ddi), M_DEVBUF, M_WAITOK | M_ZERO);
296152684Smarius	if (ofw_bus_gen_setup_devinfo(&ddi->ddi_obdinfo, node) != 0) {
297152684Smarius		free(ddi, M_DEVBUF);
298146392Smarius		return (NULL);
299152684Smarius	}
300146392Smarius	resource_list_init(&ddi->ddi_rl);
301146392Smarius	slot = -1;
302146392Smarius	nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)&reg);
303146392Smarius	if (nreg == -1) {
304152684Smarius		device_printf(dev, "<%s>: incomplete\n",
305152684Smarius		    ddi->ddi_obdinfo.obd_name);
306152684Smarius		goto fail;
307146392Smarius	}
308146392Smarius	for (i = 0; i < nreg; i++) {
309146392Smarius		base = reg[i].sbr_offset;
310146392Smarius		if (SBUS_ABS(base)) {
311146392Smarius			rslot = SBUS_ABS_TO_SLOT(base);
312146392Smarius			base = SBUS_ABS_TO_OFFSET(base);
313146392Smarius		} else
314146392Smarius			rslot = reg[i].sbr_slot;
315146392Smarius		if (slot != -1 && slot != rslot) {
316152684Smarius			device_printf(dev, "<%s>: multiple slots\n",
317152684Smarius			    ddi->ddi_obdinfo.obd_name);
318146392Smarius			free(reg, M_OFWPROP);
319152684Smarius			goto fail;
320146392Smarius		}
321146392Smarius		slot = rslot;
322146392Smarius
323146392Smarius		resource_list_add(&ddi->ddi_rl, SYS_RES_MEMORY, i, base,
324146392Smarius		    base + reg[i].sbr_size, reg[i].sbr_size);
325146392Smarius	}
326146392Smarius	free(reg, M_OFWPROP);
327146392Smarius	if (slot != dsc->sc_slot) {
328146392Smarius		device_printf(dev, "<%s>: parent and child slot do not match\n",
329152684Smarius		    ddi->ddi_obdinfo.obd_name);
330152684Smarius		goto fail;
331146392Smarius	}
332146392Smarius
333146392Smarius	/*
334146392Smarius	 * The `interrupts' property contains the SBus interrupt level.
335146392Smarius	 */
336146392Smarius	nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intr),
337146392Smarius	    (void **)&intr);
338146392Smarius	if (nintr != -1) {
339146392Smarius		for (i = 0; i < nintr; i++) {
340146392Smarius			iv = intr[i];
341146392Smarius			/*
342146392Smarius			 * SBus card devices need the slot number encoded into
343146392Smarius			 * the vector as this is generally not done.
344146392Smarius			 */
345146392Smarius			if ((iv & INTMAP_OBIO_MASK) == 0)
346146392Smarius				iv |= slot << 3;
347146392Smarius			/* Set the IGN as appropriate. */
348146392Smarius			iv |= dsc->sc_ign << INTMAP_IGN_SHIFT;
349146392Smarius			resource_list_add(&ddi->ddi_rl, SYS_RES_IRQ, i,
350146392Smarius			    iv, iv, 1);
351146392Smarius		}
352146392Smarius		free(intr, M_OFWPROP);
353146392Smarius	}
354146392Smarius	return (ddi);
355152684Smarius
356152684Smarius fail:
357152684Smarius	dma_destroy_dinfo(ddi);
358152684Smarius	return (NULL);
359146392Smarius}
360146392Smarius
361146392Smariusstatic void
362146392Smariusdma_destroy_dinfo(struct dma_devinfo *dinfo)
363146392Smarius{
364146392Smarius
365146392Smarius	resource_list_free(&dinfo->ddi_rl);
366152684Smarius	ofw_bus_gen_destroy_devinfo(&dinfo->ddi_obdinfo);
367146392Smarius	free(dinfo, M_DEVBUF);
368146392Smarius}
369146392Smarius
370146392Smariusstatic int
371146392Smariusdma_print_child(device_t dev, device_t child)
372146392Smarius{
373146392Smarius	int rv;
374146392Smarius
375146392Smarius	rv = bus_print_child_header(dev, child);
376152684Smarius	rv += dma_print_res(device_get_ivars(child));
377146392Smarius	rv += bus_print_child_footer(dev, child);
378146392Smarius	return (rv);
379146392Smarius}
380146392Smarius
381146392Smariusstatic void
382146392Smariusdma_probe_nomatch(device_t dev, device_t child)
383146392Smarius{
384152684Smarius	const char *type;
385146392Smarius
386152684Smarius	device_printf(dev, "<%s>", ofw_bus_get_name(child));
387152684Smarius	dma_print_res(device_get_ivars(child));
388152684Smarius	type = ofw_bus_get_type(child);
389152684Smarius	printf(" type %s (no driver attached)\n",
390152684Smarius	    type != NULL ? type : "unknown");
391146392Smarius}
392146392Smarius
393146392Smariusstatic struct resource_list *
394146392Smariusdma_get_resource_list(device_t dev, device_t child)
395146392Smarius{
396146392Smarius	struct dma_devinfo *ddi;
397146392Smarius
398146392Smarius	ddi = device_get_ivars(child);
399146392Smarius	return (&ddi->ddi_rl);
400146392Smarius}
401146392Smarius
402152684Smariusstatic const struct ofw_bus_devinfo *
403152684Smariusdma_get_devinfo(device_t bus, device_t child)
404146392Smarius{
405152684Smarius	struct dma_devinfo *ddi;
406146392Smarius
407152684Smarius	ddi = device_get_ivars(child);
408152684Smarius	return (&ddi->ddi_obdinfo);
409146392Smarius}
410146392Smarius
411152684Smariusstatic int
412152684Smariusdma_print_res(struct dma_devinfo *ddi)
413146392Smarius{
414152684Smarius	int rv;
415146392Smarius
416152684Smarius	rv = 0;
417152684Smarius	rv += resource_list_print_type(&ddi->ddi_rl, "mem", SYS_RES_MEMORY,
418152684Smarius	    "%#lx");
419152684Smarius	rv += resource_list_print_type(&ddi->ddi_rl, "irq", SYS_RES_IRQ, "%ld");
420152684Smarius	return (rv);
421146392Smarius}
422