1 /*        $NetBSD: lebuffer.c,v 1.41 2022/09/25 18:03:04 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.41 2022/09/25 18:03:04 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 
41 #include <sys/bus.h>
42 #include <machine/autoconf.h>
43 #include <sys/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(device_t, cfdata_t, void *);
50 void      lebufattach(device_t, device_t, void *);
51 
52 CFATTACH_DECL_NEW(lebuffer, sizeof(struct lebuf_softc),
53     lebufmatch, lebufattach, NULL, NULL);
54 
55 int
lebufprint(void * aux,const char * busname)56 lebufprint(void *aux, const char *busname)
57 {
58 
59           sbus_print(aux, busname);
60           return (UNCONF);
61 }
62 
63 int
lebufmatch(device_t parent,cfdata_t cf,void * aux)64 lebufmatch(device_t parent, cfdata_t cf, void *aux)
65 {
66           struct sbus_attach_args *sa = aux;
67 
68           return (strcmp(cf->cf_name, sa->sa_name) == 0);
69 }
70 
71 /*
72  * Attach all the sub-devices we can find
73  */
74 void
lebufattach(device_t parent,device_t self,void * aux)75 lebufattach(device_t parent, device_t self, void *aux)
76 {
77           struct sbus_attach_args *sa = aux;
78           struct lebuf_softc *sc = device_private(self);
79           struct sbus_softc *sbsc = device_private(parent);
80           int node;
81           int sbusburst;
82           bus_space_tag_t bt = sa->sa_bustag;
83           bus_dma_tag_t       dt = sa->sa_dmatag;
84           bus_space_handle_t bh;
85 
86           sc->sc_dev = self;
87 
88           if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset, sa->sa_size,
89                                BUS_SPACE_MAP_LINEAR, &bh) != 0) {
90                     aprint_error(": cannot map registers\n");
91                     return;
92           }
93 
94           /*
95            * This device's "register space" is just a buffer where the
96            * Lance ring-buffers can be stored. Note the buffer's location
97            * and size, so the `le' driver can pick them up.
98            */
99           sc->sc_buffer = bus_space_vaddr(bt, bh);
100           sc->sc_bufsiz = sa->sa_size;
101 
102           node = sc->sc_node = sa->sa_node;
103 
104           /*
105            * Get transfer burst size from PROM
106            */
107           sbusburst = sbsc->sc_burst;
108           if (sbusburst == 0)
109                     sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
110 
111           sc->sc_burst = prom_getpropint(node, "burst-sizes", -1);
112           if (sc->sc_burst == -1)
113                     /* take SBus burst sizes */
114                     sc->sc_burst = sbusburst;
115 
116           /* Clamp at parent's burst sizes */
117           sc->sc_burst &= sbusburst;
118 
119           printf(": %dK memory\n", sc->sc_bufsiz / 1024);
120 
121           /* search through children */
122           devhandle_t selfh = device_handle(self);
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                         CFARGS(.devhandle = prom_node_to_devhandle(selfh, node)));
129                     sbus_destroy_attach_args(&sax);
130           }
131 }
132