xref: /netbsd-src/sys/arch/sparc64/dev/central.c (revision 3944ff70a48a86bb622139705a33f8ef461e24f7)
1 /*	$NetBSD: central.c,v 1.9 2022/01/22 11:49:17 thorpej Exp $	*/
2 /*	$OpenBSD: central.c,v 1.7 2010/11/11 17:58:23 miod Exp $	*/
3 
4 /*
5  * Copyright (c) 2004 Jason L. Wright (jason@thought.net)
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: central.c,v 1.9 2022/01/22 11:49:17 thorpej Exp $");
32 
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/device.h>
38 #include <sys/conf.h>
39 #include <sys/malloc.h>
40 #include <sys/kmem.h>
41 #include <sys/bus.h>
42 
43 #include <machine/autoconf.h>
44 #include <machine/openfirm.h>
45 
46 #include <sparc64/dev/centralvar.h>
47 
48 struct central_softc {
49 	bus_space_tag_t sc_bt;
50 	bus_space_tag_t sc_cbt;
51 	int sc_node;
52 	int sc_nrange;
53 	struct central_range *sc_range;
54 };
55 
56 static int central_match(device_t, cfdata_t, void *);
57 static void central_attach(device_t, device_t, void *);
58 
59 CFATTACH_DECL_NEW(central, sizeof(struct central_softc),
60     central_match, central_attach, NULL, NULL);
61 
62 static int central_print(void *, const char *);
63 static int central_get_string(int, const char *, char **);
64 
65 static bus_space_tag_t central_alloc_bus_tag(struct central_softc *);
66 static int _central_bus_map(
67 		bus_space_tag_t,
68 		bus_addr_t,		/*offset*/
69 		bus_size_t,		/*size*/
70 		int,			/*flags*/
71 		vaddr_t,		/* XXX unused -- compat w/sparc */
72 		bus_space_handle_t *);
73 
74 static int
central_match(device_t parent,cfdata_t match,void * aux)75 central_match(device_t parent, cfdata_t match, void *aux)
76 {
77 	struct mainbus_attach_args *ma = aux;
78 
79 	if (strcmp(ma->ma_name, "central") == 0)
80 		return (1);
81 	return (0);
82 }
83 
84 static void
central_attach(device_t parent,device_t self,void * aux)85 central_attach(device_t parent, device_t self, void *aux)
86 {
87 	struct central_softc *sc = device_private(self);
88 	struct mainbus_attach_args *ma = aux;
89 	int node0, node;
90 
91 	sc->sc_bt = ma->ma_bustag;
92 	sc->sc_node = ma->ma_node;
93 	sc->sc_cbt = central_alloc_bus_tag(sc);
94 
95 	prom_getprop(sc->sc_node, "ranges", sizeof(struct central_range),
96 	    &sc->sc_nrange, (void **)&sc->sc_range);
97 
98 	printf("\n");
99 
100 	devhandle_t selfh = device_handle(self);
101 	node0 = firstchild(sc->sc_node);
102 	for (node = node0; node; node = nextsibling(node)) {
103 		struct central_attach_args ca;
104 
105 		bzero(&ca, sizeof(ca));
106 		ca.ca_node = node;
107 		ca.ca_bustag = sc->sc_cbt;
108 		if (central_get_string(ca.ca_node, "name", &ca.ca_name)) {
109 			printf("can't fetch name for node 0x%x\n", node);
110 			continue;
111 		}
112 
113 		prom_getprop(node, "reg", sizeof(struct central_reg),
114 		    &ca.ca_nreg, (void **)&ca.ca_reg);
115 
116 		(void)config_found(self, (void *)&ca, central_print,
117 		    CFARGS(.devhandle = prom_node_to_devhandle(selfh,
118 							       ca.ca_node)));
119 
120 		if (ca.ca_name != NULL)
121 			free(ca.ca_name, M_DEVBUF);
122 	}
123 }
124 
125 static int
central_get_string(int node,const char * name,char ** buf)126 central_get_string(int node, const char *name, char **buf)
127 {
128 	int len;
129 
130 	len = prom_getproplen(node, name);
131 	if (len < 0)
132 		return (len);
133 	*buf = malloc(len + 1, M_DEVBUF, M_WAITOK);
134 	if (len != 0)
135 		prom_getpropstringA(node, name, *buf, len + 1);
136 	(*buf)[len] = '\0';
137 	return (0);
138 }
139 
140 static int
central_print(void * args,const char * busname)141 central_print(void *args, const char *busname)
142 {
143 	struct central_attach_args *ca = args;
144 	char *class;
145 
146 	if (busname != NULL) {
147 		printf("\"%s\" at %s", ca->ca_name, busname);
148 		class = prom_getpropstring(ca->ca_node, "device_type");
149 		if (*class != '\0')
150 			printf(" class %s", class);
151 	}
152 	return (UNCONF);
153 }
154 
155 static bus_space_tag_t
central_alloc_bus_tag(struct central_softc * sc)156 central_alloc_bus_tag(struct central_softc *sc)
157 {
158 	struct sparc_bus_space_tag *bt;
159 
160 	bt = kmem_zalloc(sizeof(*bt), KM_SLEEP);
161 	bt->cookie = sc;
162 	bt->parent = sc->sc_bt;
163 #if 0
164 	bt->asi = bt->parent->asi;
165 	bt->sasi = bt->parent->sasi;
166 #endif
167 	bt->sparc_bus_map = _central_bus_map;
168 	/* XXX bt->sparc_bus_mmap = central_bus_mmap; */
169 	/* XXX bt->sparc_intr_establish = upa_intr_establish; */
170 	return (bt);
171 }
172 
173 static int
_central_bus_map(bus_space_tag_t t,bus_addr_t addr,bus_size_t size,int flags,vaddr_t v,bus_space_handle_t * hp)174 _central_bus_map(bus_space_tag_t t, bus_addr_t addr, bus_size_t size, int flags,
175 	vaddr_t v, bus_space_handle_t *hp)
176 {
177 	struct central_softc *sc = t->cookie;
178 	int64_t slot = BUS_ADDR_IOSPACE(addr);
179 	int64_t offset = BUS_ADDR_PADDR(addr);
180 	int i;
181 
182 	if (t->parent == NULL || t->parent->sparc_bus_map == NULL) {
183 		printf("\ncentral_bus_map: invalid parent");
184 		return (EINVAL);
185 	}
186 
187 
188 	for (i = 0; i < sc->sc_nrange; i++) {
189 		bus_addr_t paddr;
190 
191 		if (sc->sc_range[i].cspace != slot)
192 			continue;
193 
194 		paddr = offset - sc->sc_range[i].coffset;
195 		paddr += sc->sc_range[i].poffset;
196 		paddr |= ((bus_addr_t)sc->sc_range[i].pspace << 32);
197 
198 		return bus_space_map(t->parent, paddr, size, flags, hp);
199 	}
200 
201 	return (EINVAL);
202 }
203