lebuffer.c revision 1.38
1/*	$NetBSD: lebuffer.c,v 1.38 2021/05/10 23:53:44 thorpej 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: lebuffer.c,v 1.38 2021/05/10 23:53:44 thorpej Exp $");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/errno.h>
39#include <sys/device.h>
40#include <sys/malloc.h>
41
42#include <sys/bus.h>
43#include <machine/autoconf.h>
44#include <sys/cpu.h>
45
46#include <dev/sbus/sbusvar.h>
47#include <dev/sbus/lebuffervar.h>
48
49int	lebufprint(void *, const char *);
50int	lebufmatch(device_t, cfdata_t, void *);
51void	lebufattach(device_t, device_t, void *);
52
53CFATTACH_DECL_NEW(lebuffer, sizeof(struct lebuf_softc),
54    lebufmatch, lebufattach, NULL, NULL);
55
56int
57lebufprint(void *aux, const char *busname)
58{
59
60	sbus_print(aux, busname);
61	return (UNCONF);
62}
63
64int
65lebufmatch(device_t parent, cfdata_t cf, void *aux)
66{
67	struct sbus_attach_args *sa = aux;
68
69	return (strcmp(cf->cf_name, sa->sa_name) == 0);
70}
71
72/*
73 * Attach all the sub-devices we can find
74 */
75void
76lebufattach(device_t parent, device_t self, void *aux)
77{
78	struct sbus_attach_args *sa = aux;
79	struct lebuf_softc *sc = device_private(self);
80	struct sbus_softc *sbsc = device_private(parent);
81	int node;
82	int sbusburst;
83	bus_space_tag_t bt = sa->sa_bustag;
84	bus_dma_tag_t	dt = sa->sa_dmatag;
85	bus_space_handle_t bh;
86
87	sc->sc_dev = self;
88
89	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset, sa->sa_size,
90			 BUS_SPACE_MAP_LINEAR, &bh) != 0) {
91		aprint_error(": cannot map registers\n");
92		return;
93	}
94
95	/*
96	 * This device's "register space" is just a buffer where the
97	 * Lance ring-buffers can be stored. Note the buffer's location
98	 * and size, so the `le' driver can pick them up.
99	 */
100	sc->sc_buffer = bus_space_vaddr(bt, bh);
101	sc->sc_bufsiz = sa->sa_size;
102
103	node = sc->sc_node = sa->sa_node;
104
105	/*
106	 * Get transfer burst size from PROM
107	 */
108	sbusburst = sbsc->sc_burst;
109	if (sbusburst == 0)
110		sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
111
112	sc->sc_burst = prom_getpropint(node, "burst-sizes", -1);
113	if (sc->sc_burst == -1)
114		/* take SBus burst sizes */
115		sc->sc_burst = sbusburst;
116
117	/* Clamp at parent's burst sizes */
118	sc->sc_burst &= sbusburst;
119
120	printf(": %dK memory\n", sc->sc_bufsiz / 1024);
121
122	/* search through children */
123	for (node = firstchild(node); node; node = nextsibling(node)) {
124		struct sbus_attach_args sax;
125		sbus_setup_attach_args(sbsc,
126				       bt, dt, node, &sax);
127		(void)config_found(self, (void *)&sax, lebufprint,
128		    CFARG_DEVHANDLE, prom_node_to_devhandle(node),
129		    CFARG_EOL);
130		sbus_destroy_attach_args(&sax);
131	}
132}
133