1/*	$OpenBSD: dma_sbus.c,v 1.19 2022/10/16 01:22:40 jsg 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Copyright (c) 1994 Peter Galbavy.  All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 *    notice, this list of conditions and the following disclaimer in the
43 *    documentation and/or other materials provided with the distribution.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 */
56
57#include <sys/param.h>
58#include <sys/systm.h>
59#include <sys/kernel.h>
60#include <sys/errno.h>
61#include <sys/device.h>
62#include <sys/malloc.h>
63
64#include <machine/bus.h>
65#include <machine/intr.h>
66#include <machine/autoconf.h>
67
68#include <dev/sbus/sbusvar.h>
69
70#include <dev/ic/lsi64854reg.h>
71#include <dev/ic/lsi64854var.h>
72
73#include <scsi/scsi_all.h>
74#include <scsi/scsiconf.h>
75
76#include <dev/ic/ncr53c9xreg.h>
77#include <dev/ic/ncr53c9xvar.h>
78
79struct dma_softc {
80	struct lsi64854_softc	sc_lsi64854;	/* base device */
81};
82
83int	dmamatch_sbus(struct device *, void *, void *);
84void	dmaattach_sbus(struct device *, struct device *, void *);
85
86int	dmaprint_sbus(void *, const char *);
87
88void	*dmabus_intr_establish(
89		bus_space_tag_t,
90		bus_space_tag_t,
91		int,			/*bus interrupt priority*/
92		int,			/*`device class' level*/
93		int,			/*flags*/
94		int (*)(void *),	/*handler*/
95		void *,			/*handler arg*/
96		const char *);		/*what*/
97
98static	bus_space_tag_t dma_alloc_bustag(struct dma_softc *sc);
99
100const struct cfattach dma_sbus_ca = {
101	sizeof(struct dma_softc), dmamatch_sbus, dmaattach_sbus
102};
103
104const struct cfattach ledma_ca = {
105	sizeof(struct dma_softc), dmamatch_sbus, dmaattach_sbus
106};
107
108struct cfdriver ledma_cd = {
109	NULL, "ledma", DV_DULL
110};
111
112struct cfdriver dma_cd = {
113	NULL, "dma", DV_DULL
114};
115
116int
117dmaprint_sbus(void *aux, const char *busname)
118{
119	struct sbus_attach_args *sa = aux;
120	bus_space_tag_t t = sa->sa_bustag;
121	struct dma_softc *sc = t->cookie;
122
123	sa->sa_bustag = sc->sc_lsi64854.sc_bustag;	/* XXX */
124	sbus_print(aux, busname);	/* XXX */
125	sa->sa_bustag = t;		/* XXX */
126	return (UNCONF);
127}
128
129int
130dmamatch_sbus(struct device *parent, void *vcf, void *aux)
131{
132	struct cfdata *cf = vcf;
133	struct sbus_attach_args *sa = aux;
134
135	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0 ||
136		strcmp("espdma", sa->sa_name) == 0);
137}
138
139void
140dmaattach_sbus(struct device *parent, struct device *self, void *aux)
141{
142	struct sbus_attach_args *sa = aux;
143	struct dma_softc *dsc = (void *)self;
144	struct lsi64854_softc *sc = &dsc->sc_lsi64854;
145	bus_space_handle_t bh;
146	bus_space_tag_t sbt;
147	int sbusburst, burst;
148	int node;
149
150	node = sa->sa_node;
151
152	sc->sc_bustag = sa->sa_bustag;
153	sc->sc_dmatag = sa->sa_dmatag;
154
155	/* Map registers */
156	if (sa->sa_npromvaddrs != 0) {
157		if (sbus_bus_map(sa->sa_bustag, 0,
158		    sa->sa_promvaddrs[0],
159		    sa->sa_size,		/* ???? */
160		    BUS_SPACE_MAP_PROMADDRESS,
161		    0, &bh) != 0) {
162			printf("%s: cannot map registers\n", self->dv_xname);
163			return;
164		}
165	} else if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
166	    sa->sa_offset,
167	    sa->sa_size,
168	    0, 0, &bh) != 0) {
169		printf("%s: cannot map registers\n", self->dv_xname);
170		return;
171	}
172	sc->sc_regs = bh;
173
174	/*
175	 * Get transfer burst size from PROM and plug it into the
176	 * controller registers. This is needed on the Sun4m; do
177	 * others need it too?
178	 */
179	sbusburst = ((struct sbus_softc *)parent)->sc_burst;
180	if (sbusburst == 0)
181		sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
182
183	burst = getpropint(node,"burst-sizes", -1);
184	if (burst == -1)
185		/* take SBus burst sizes */
186		burst = sbusburst;
187
188	/* Clamp at parent's burst sizes */
189	burst &= sbusburst;
190	sc->sc_burst = (burst & SBUS_BURST_32) ? 32 :
191		       (burst & SBUS_BURST_16) ? 16 : 0;
192
193	if (sc->sc_dev.dv_cfdata->cf_attach == &ledma_ca) {
194		char *cabletype;
195		u_int32_t csr;
196		/*
197		 * Check to see which cable type is currently active and
198		 * set the appropriate bit in the ledma csr so that it
199		 * gets used. If we didn't netboot, the PROM won't have
200		 * the "cable-selection" property; default to TP and then
201		 * the user can change it via a "media" option to ifconfig.
202		 */
203		cabletype = getpropstring(node, "cable-selection");
204		csr = L64854_GCSR(sc);
205		if (strcmp(cabletype, "tpe") == 0) {
206			csr |= E_TP_AUI;
207		} else if (strcmp(cabletype, "aui") == 0) {
208			csr &= ~E_TP_AUI;
209		} else {
210			/* assume TP if nothing there */
211			csr |= E_TP_AUI;
212		}
213		L64854_SCSR(sc, csr);
214		delay(20000);	/* manual says we need a 20ms delay */
215		sc->sc_channel = L64854_CHANNEL_ENET;
216	} else {
217		sc->sc_channel = L64854_CHANNEL_SCSI;
218	}
219
220	sbt = dma_alloc_bustag(dsc);
221	if (lsi64854_attach(sc) != 0)
222		return;
223
224	/* Attach children */
225	for (node = firstchild(sa->sa_node); node; node = nextsibling(node)) {
226		struct sbus_attach_args sa;
227		sbus_setup_attach_args((struct sbus_softc *)parent,
228				       sbt, sc->sc_dmatag, node, &sa);
229		(void) config_found(&sc->sc_dev, (void *)&sa, dmaprint_sbus);
230		sbus_destroy_attach_args(&sa);
231	}
232}
233
234void *
235dmabus_intr_establish(
236	bus_space_tag_t t,
237	bus_space_tag_t t0,
238	int pri,
239	int level,
240	int flags,
241	int (*handler)(void *),
242	void *arg,
243	const char *what)
244{
245	struct lsi64854_softc *sc = t->cookie;
246
247	/* XXX - for now only le; do esp later */
248	if (sc->sc_channel == L64854_CHANNEL_ENET) {
249		sc->sc_intrchain = handler;
250		sc->sc_intrchainarg = arg;
251		handler = lsi64854_enet_intr;
252		arg = sc;
253	}
254
255	for (t = t->parent; t; t = t->parent) {
256		if (t->sparc_intr_establish != NULL)
257			return ((*t->sparc_intr_establish)
258				(t, t0, pri, level, flags, handler, arg, what));
259
260	}
261
262	panic("dmabus_intr_establish: no handler found");
263
264	return (NULL);
265}
266
267bus_space_tag_t
268dma_alloc_bustag(struct dma_softc *sc)
269{
270	struct sparc_bus_space_tag *sbt;
271
272	sbt = malloc(sizeof(*sbt), M_DEVBUF, M_NOWAIT | M_ZERO);
273	if (sbt == NULL)
274		return (NULL);
275
276	sbt->cookie = sc;
277	sbt->parent = sc->sc_lsi64854.sc_bustag;
278	sbt->asi = sbt->parent->asi;
279	sbt->sasi = sbt->parent->sasi;
280	sbt->sparc_intr_establish = dmabus_intr_establish;
281	return (sbt);
282}
283