xref: /netbsd-src/sys/dev/sbus/lebuffer.c (revision 06be8101a16cc95f40783b3cb7afd12112103a9a)
1 /*	$NetBSD: lebuffer.c,v 1.9 2001/11/13 06:58:17 lukem Exp $ */
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Paul Kranenburg.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: lebuffer.c,v 1.9 2001/11/13 06:58:17 lukem Exp $");
41 
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/errno.h>
47 #include <sys/device.h>
48 #include <sys/malloc.h>
49 
50 #include <machine/bus.h>
51 #include <machine/autoconf.h>
52 #include <machine/cpu.h>
53 
54 #include <dev/sbus/sbusvar.h>
55 #include <dev/sbus/lebuffervar.h>
56 
57 int	lebufprint	__P((void *, const char *));
58 int	lebufmatch	__P((struct device *, struct cfdata *, void *));
59 void	lebufattach	__P((struct device *, struct device *, void *));
60 
61 struct cfattach lebuffer_ca = {
62 	sizeof(struct lebuf_softc), lebufmatch, lebufattach
63 };
64 
65 int
66 lebufprint(aux, busname)
67 	void *aux;
68 	const char *busname;
69 {
70 	struct sbus_attach_args *sa = aux;
71 	bus_space_tag_t t = sa->sa_bustag;
72 	struct lebuf_softc *sc = t->cookie;
73 
74 	sa->sa_bustag = sc->sc_bustag;	/* XXX */
75 	sbus_print(aux, busname);	/* XXX */
76 	sa->sa_bustag = t;		/* XXX */
77 	return (UNCONF);
78 }
79 
80 int
81 lebufmatch(parent, cf, aux)
82 	struct device *parent;
83 	struct cfdata *cf;
84 	void *aux;
85 {
86 	struct sbus_attach_args *sa = aux;
87 
88 	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
89 }
90 
91 /*
92  * Attach all the sub-devices we can find
93  */
94 void
95 lebufattach(parent, self, aux)
96 	struct device *parent, *self;
97 	void *aux;
98 {
99 	struct sbus_attach_args *sa = aux;
100 	struct lebuf_softc *sc = (void *)self;
101 	int node;
102 	int sbusburst;
103 	bus_space_tag_t sbt;
104 	bus_space_handle_t bh;
105 
106 	sc->sc_bustag = sa->sa_bustag;
107 	sc->sc_dmatag = sa->sa_dmatag;
108 
109 	if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
110 			 sa->sa_offset,
111 			 sa->sa_size,
112 			 0, 0, &bh) != 0) {
113 		printf("%s: attach: cannot map registers\n", self->dv_xname);
114 		return;
115 	}
116 
117 	/*
118 	 * This device's "register space" is just a buffer where the
119 	 * Lance ring-buffers can be stored. Note the buffer's location
120 	 * and size, so the `le' driver can pick them up.
121 	 */
122 	sc->sc_buffer = (caddr_t)(u_long)bh;
123 	sc->sc_bufsiz = sa->sa_size;
124 
125 	node = sc->sc_node = sa->sa_node;
126 
127 	/*
128 	 * Get transfer burst size from PROM
129 	 */
130 	sbusburst = ((struct sbus_softc *)parent)->sc_burst;
131 	if (sbusburst == 0)
132 		sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
133 
134 	sc->sc_burst = PROM_getpropint(node, "burst-sizes", -1);
135 	if (sc->sc_burst == -1)
136 		/* take SBus burst sizes */
137 		sc->sc_burst = sbusburst;
138 
139 	/* Clamp at parent's burst sizes */
140 	sc->sc_burst &= sbusburst;
141 
142 	sbus_establish(&sc->sc_sd, &sc->sc_dev);
143 
144 	/* Allocate a bus tag */
145 	sbt = (bus_space_tag_t)
146 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
147 	if (sbt == NULL) {
148 		printf("%s: attach: out of memory\n", self->dv_xname);
149 		return;
150 	}
151 
152 	printf(": %dK memory\n", sc->sc_bufsiz / 1024);
153 
154 	bzero(sbt, sizeof *sbt);
155 	sbt->cookie = sc;
156 	sbt->parent = sc->sc_bustag;
157 
158 	/* search through children */
159 	for (node = firstchild(node); node; node = nextsibling(node)) {
160 		struct sbus_attach_args sa;
161 		sbus_setup_attach_args((struct sbus_softc *)parent,
162 				       sbt, sc->sc_dmatag, node, &sa);
163 		(void)config_found(&sc->sc_dev, (void *)&sa, lebufprint);
164 		sbus_destroy_attach_args(&sa);
165 	}
166 }
167