xref: /openbsd/sys/dev/sbus/lebuffer.c (revision 404b540a)
1 /*	$OpenBSD: lebuffer.c,v 1.9 2008/06/26 05:42:18 ray Exp $	*/
2 /*	$NetBSD: lebuffer.c,v 1.12 2002/03/11 16:00:57 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 #include <sys/cdefs.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/errno.h>
38 #include <sys/device.h>
39 #include <sys/malloc.h>
40 
41 #include <machine/bus.h>
42 #include <machine/autoconf.h>
43 #include <machine/cpu.h>
44 
45 #include <dev/sbus/sbusvar.h>
46 #include <dev/sbus/lebuffervar.h>
47 
48 int	lebufprint(void *, const char *);
49 int	lebufmatch(struct device *, void *, void *);
50 void	lebufattach(struct device *, struct device *, void *);
51 
52 struct cfattach lebuffer_ca = {
53 	sizeof(struct lebuf_softc), lebufmatch, lebufattach
54 };
55 
56 int
57 lebufprint(void *aux, const char *busname)
58 {
59 	struct sbus_attach_args *sa = aux;
60 	bus_space_tag_t t = sa->sa_bustag;
61 	struct lebuf_softc *sc = t->cookie;
62 
63 	sa->sa_bustag = sc->sc_bustag;	/* XXX */
64 	sbus_print(aux, busname);	/* XXX */
65 	sa->sa_bustag = t;		/* XXX */
66 	return (UNCONF);
67 }
68 
69 int
70 lebufmatch(struct device *parent, void *vcf, void *aux)
71 {
72 	struct sbus_attach_args *sa = aux;
73 	struct cfdata *cf = vcf;
74 
75 	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
76 }
77 
78 /*
79  * Attach all the sub-devices we can find
80  */
81 void
82 lebufattach(struct device *parent, struct device *self, void *aux)
83 {
84 	struct sbus_attach_args *sa = aux;
85 	struct lebuf_softc *sc = (void *)self;
86 	int node;
87 	int sbusburst;
88 	struct sparc_bus_space_tag *sbt;
89 	bus_space_handle_t bh;
90 
91 	sc->sc_bustag = sa->sa_bustag;
92 	sc->sc_dmatag = sa->sa_dmatag;
93 
94 	if (sbus_bus_map(sa->sa_bustag,
95 	    sa->sa_slot, sa->sa_offset, sa->sa_size,
96 	    BUS_SPACE_MAP_LINEAR, 0, &bh) != 0) {
97 		printf("%s: attach: cannot map registers\n", self->dv_xname);
98 		return;
99 	}
100 
101 	/*
102 	 * This device's "register space" is just a buffer where the
103 	 * Lance ring-buffers can be stored. Note the buffer's location
104 	 * and size, so the `le' driver can pick them up.
105 	 */
106 	sc->sc_buffer = (void *)bus_space_vaddr(sa->sa_bustag, bh);
107 	sc->sc_bufsiz = sa->sa_size;
108 
109 	node = sc->sc_node = sa->sa_node;
110 
111 	/*
112 	 * Get transfer burst size from PROM
113 	 */
114 	sbusburst = ((struct sbus_softc *)parent)->sc_burst;
115 	if (sbusburst == 0)
116 		sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
117 
118 	sc->sc_burst = getpropint(node, "burst-sizes", -1);
119 	if (sc->sc_burst == -1)
120 		/* take SBus burst sizes */
121 		sc->sc_burst = sbusburst;
122 
123 	/* Clamp at parent's burst sizes */
124 	sc->sc_burst &= sbusburst;
125 
126 	/* Allocate a bus tag */
127 	sbt = malloc(sizeof(*sbt), M_DEVBUF, M_NOWAIT | M_ZERO);
128 	if (sbt == NULL) {
129 		printf("%s: attach: out of memory\n", self->dv_xname);
130 		return;
131 	}
132 
133 	printf(": %dK memory\n", sc->sc_bufsiz / 1024);
134 
135 	sbt->cookie = sc;
136 	sbt->parent = sc->sc_bustag;
137 	sbt->asi = sbt->parent->asi;
138 	sbt->sasi = sbt->parent->sasi;
139 
140 	/* search through children */
141 	for (node = firstchild(node); node; node = nextsibling(node)) {
142 		struct sbus_attach_args sa;
143 		sbus_setup_attach_args((struct sbus_softc *)parent,
144 		    sbt, sc->sc_dmatag, node, &sa);
145 		(void)config_found(&sc->sc_dev, (void *)&sa, lebufprint);
146 		sbus_destroy_attach_args(&sa);
147 	}
148 }
149