xref: /netbsd-src/sys/arch/sparc64/dev/central.c (revision 3944ff70a48a86bb622139705a33f8ef461e24f7)
1*3944ff70Sthorpej /*	$NetBSD: central.c,v 1.9 2022/01/22 11:49:17 thorpej Exp $	*/
239a57b1cSmrg /*	$OpenBSD: central.c,v 1.7 2010/11/11 17:58:23 miod Exp $	*/
339a57b1cSmrg 
439a57b1cSmrg /*
539a57b1cSmrg  * Copyright (c) 2004 Jason L. Wright (jason@thought.net)
639a57b1cSmrg  * All rights reserved.
739a57b1cSmrg  *
839a57b1cSmrg  * Redistribution and use in source and binary forms, with or without
939a57b1cSmrg  * modification, are permitted provided that the following conditions
1039a57b1cSmrg  * are met:
1139a57b1cSmrg  * 1. Redistributions of source code must retain the above copyright
1239a57b1cSmrg  *    notice, this list of conditions and the following disclaimer.
1339a57b1cSmrg  * 2. Redistributions in binary form must reproduce the above copyright
1439a57b1cSmrg  *    notice, this list of conditions and the following disclaimer in the
1539a57b1cSmrg  *    documentation and/or other materials provided with the distribution.
1639a57b1cSmrg  *
1739a57b1cSmrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1839a57b1cSmrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1939a57b1cSmrg  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2039a57b1cSmrg  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
2139a57b1cSmrg  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2239a57b1cSmrg  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2339a57b1cSmrg  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2439a57b1cSmrg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2539a57b1cSmrg  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
2639a57b1cSmrg  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2739a57b1cSmrg  * POSSIBILITY OF SUCH DAMAGE.
2839a57b1cSmrg  */
2939a57b1cSmrg 
30f72d1cdaSmrg #include <sys/cdefs.h>
31*3944ff70Sthorpej __KERNEL_RCSID(0, "$NetBSD: central.c,v 1.9 2022/01/22 11:49:17 thorpej Exp $");
32f72d1cdaSmrg 
3339a57b1cSmrg #include <sys/types.h>
3439a57b1cSmrg #include <sys/param.h>
3539a57b1cSmrg #include <sys/systm.h>
3639a57b1cSmrg #include <sys/kernel.h>
3739a57b1cSmrg #include <sys/device.h>
3839a57b1cSmrg #include <sys/conf.h>
3939a57b1cSmrg #include <sys/malloc.h>
40fc256c4aSthorpej #include <sys/kmem.h>
4139a57b1cSmrg #include <sys/bus.h>
4239a57b1cSmrg 
4339a57b1cSmrg #include <machine/autoconf.h>
4439a57b1cSmrg #include <machine/openfirm.h>
4539a57b1cSmrg 
4639a57b1cSmrg #include <sparc64/dev/centralvar.h>
4739a57b1cSmrg 
4839a57b1cSmrg struct central_softc {
4939a57b1cSmrg 	bus_space_tag_t sc_bt;
5039a57b1cSmrg 	bus_space_tag_t sc_cbt;
5139a57b1cSmrg 	int sc_node;
5239a57b1cSmrg 	int sc_nrange;
5339a57b1cSmrg 	struct central_range *sc_range;
5439a57b1cSmrg };
5539a57b1cSmrg 
5639a57b1cSmrg static int central_match(device_t, cfdata_t, void *);
5739a57b1cSmrg static void central_attach(device_t, device_t, void *);
5839a57b1cSmrg 
5939a57b1cSmrg CFATTACH_DECL_NEW(central, sizeof(struct central_softc),
6039a57b1cSmrg     central_match, central_attach, NULL, NULL);
6139a57b1cSmrg 
6239a57b1cSmrg static int central_print(void *, const char *);
6339a57b1cSmrg static int central_get_string(int, const char *, char **);
6439a57b1cSmrg 
6539a57b1cSmrg static bus_space_tag_t central_alloc_bus_tag(struct central_softc *);
6639a57b1cSmrg static int _central_bus_map(
6739a57b1cSmrg 		bus_space_tag_t,
6839a57b1cSmrg 		bus_addr_t,		/*offset*/
6939a57b1cSmrg 		bus_size_t,		/*size*/
7039a57b1cSmrg 		int,			/*flags*/
7139a57b1cSmrg 		vaddr_t,		/* XXX unused -- compat w/sparc */
7239a57b1cSmrg 		bus_space_handle_t *);
7339a57b1cSmrg 
7439a57b1cSmrg static int
central_match(device_t parent,cfdata_t match,void * aux)7539a57b1cSmrg central_match(device_t parent, cfdata_t match, void *aux)
7639a57b1cSmrg {
7739a57b1cSmrg 	struct mainbus_attach_args *ma = aux;
7839a57b1cSmrg 
7939a57b1cSmrg 	if (strcmp(ma->ma_name, "central") == 0)
8039a57b1cSmrg 		return (1);
8139a57b1cSmrg 	return (0);
8239a57b1cSmrg }
8339a57b1cSmrg 
8439a57b1cSmrg static void
central_attach(device_t parent,device_t self,void * aux)8539a57b1cSmrg central_attach(device_t parent, device_t self, void *aux)
8639a57b1cSmrg {
87a14d5855Smrg 	struct central_softc *sc = device_private(self);
8839a57b1cSmrg 	struct mainbus_attach_args *ma = aux;
8939a57b1cSmrg 	int node0, node;
9039a57b1cSmrg 
9139a57b1cSmrg 	sc->sc_bt = ma->ma_bustag;
9239a57b1cSmrg 	sc->sc_node = ma->ma_node;
9339a57b1cSmrg 	sc->sc_cbt = central_alloc_bus_tag(sc);
9439a57b1cSmrg 
9539a57b1cSmrg 	prom_getprop(sc->sc_node, "ranges", sizeof(struct central_range),
9639a57b1cSmrg 	    &sc->sc_nrange, (void **)&sc->sc_range);
9739a57b1cSmrg 
9839a57b1cSmrg 	printf("\n");
9939a57b1cSmrg 
100*3944ff70Sthorpej 	devhandle_t selfh = device_handle(self);
10139a57b1cSmrg 	node0 = firstchild(sc->sc_node);
10239a57b1cSmrg 	for (node = node0; node; node = nextsibling(node)) {
10339a57b1cSmrg 		struct central_attach_args ca;
10439a57b1cSmrg 
10539a57b1cSmrg 		bzero(&ca, sizeof(ca));
10639a57b1cSmrg 		ca.ca_node = node;
10739a57b1cSmrg 		ca.ca_bustag = sc->sc_cbt;
10839a57b1cSmrg 		if (central_get_string(ca.ca_node, "name", &ca.ca_name)) {
10939a57b1cSmrg 			printf("can't fetch name for node 0x%x\n", node);
11039a57b1cSmrg 			continue;
11139a57b1cSmrg 		}
11239a57b1cSmrg 
11339a57b1cSmrg 		prom_getprop(node, "reg", sizeof(struct central_reg),
11439a57b1cSmrg 		    &ca.ca_nreg, (void **)&ca.ca_reg);
11539a57b1cSmrg 
11665c738d1Sthorpej 		(void)config_found(self, (void *)&ca, central_print,
117*3944ff70Sthorpej 		    CFARGS(.devhandle = prom_node_to_devhandle(selfh,
118*3944ff70Sthorpej 							       ca.ca_node)));
11939a57b1cSmrg 
12039a57b1cSmrg 		if (ca.ca_name != NULL)
12139a57b1cSmrg 			free(ca.ca_name, M_DEVBUF);
12239a57b1cSmrg 	}
12339a57b1cSmrg }
12439a57b1cSmrg 
12539a57b1cSmrg static int
central_get_string(int node,const char * name,char ** buf)12639a57b1cSmrg central_get_string(int node, const char *name, char **buf)
12739a57b1cSmrg {
12839a57b1cSmrg 	int len;
12939a57b1cSmrg 
13039a57b1cSmrg 	len = prom_getproplen(node, name);
13139a57b1cSmrg 	if (len < 0)
13239a57b1cSmrg 		return (len);
133d47bcd29Schs 	*buf = malloc(len + 1, M_DEVBUF, M_WAITOK);
13439a57b1cSmrg 	if (len != 0)
13539a57b1cSmrg 		prom_getpropstringA(node, name, *buf, len + 1);
13639a57b1cSmrg 	(*buf)[len] = '\0';
13739a57b1cSmrg 	return (0);
13839a57b1cSmrg }
13939a57b1cSmrg 
14039a57b1cSmrg static int
central_print(void * args,const char * busname)14139a57b1cSmrg central_print(void *args, const char *busname)
14239a57b1cSmrg {
14339a57b1cSmrg 	struct central_attach_args *ca = args;
14439a57b1cSmrg 	char *class;
14539a57b1cSmrg 
14639a57b1cSmrg 	if (busname != NULL) {
14739a57b1cSmrg 		printf("\"%s\" at %s", ca->ca_name, busname);
14839a57b1cSmrg 		class = prom_getpropstring(ca->ca_node, "device_type");
14939a57b1cSmrg 		if (*class != '\0')
15039a57b1cSmrg 			printf(" class %s", class);
15139a57b1cSmrg 	}
15239a57b1cSmrg 	return (UNCONF);
15339a57b1cSmrg }
15439a57b1cSmrg 
15539a57b1cSmrg static bus_space_tag_t
central_alloc_bus_tag(struct central_softc * sc)15639a57b1cSmrg central_alloc_bus_tag(struct central_softc *sc)
15739a57b1cSmrg {
15839a57b1cSmrg 	struct sparc_bus_space_tag *bt;
15939a57b1cSmrg 
160fc256c4aSthorpej 	bt = kmem_zalloc(sizeof(*bt), KM_SLEEP);
16139a57b1cSmrg 	bt->cookie = sc;
16239a57b1cSmrg 	bt->parent = sc->sc_bt;
16339a57b1cSmrg #if 0
16439a57b1cSmrg 	bt->asi = bt->parent->asi;
16539a57b1cSmrg 	bt->sasi = bt->parent->sasi;
16639a57b1cSmrg #endif
16739a57b1cSmrg 	bt->sparc_bus_map = _central_bus_map;
16839a57b1cSmrg 	/* XXX bt->sparc_bus_mmap = central_bus_mmap; */
16939a57b1cSmrg 	/* XXX bt->sparc_intr_establish = upa_intr_establish; */
17039a57b1cSmrg 	return (bt);
17139a57b1cSmrg }
17239a57b1cSmrg 
17339a57b1cSmrg 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)17439a57b1cSmrg _central_bus_map(bus_space_tag_t t, bus_addr_t addr, bus_size_t size, int flags,
17539a57b1cSmrg 	vaddr_t v, bus_space_handle_t *hp)
17639a57b1cSmrg {
17739a57b1cSmrg 	struct central_softc *sc = t->cookie;
17839a57b1cSmrg 	int64_t slot = BUS_ADDR_IOSPACE(addr);
17939a57b1cSmrg 	int64_t offset = BUS_ADDR_PADDR(addr);
18039a57b1cSmrg 	int i;
18139a57b1cSmrg 
18239a57b1cSmrg 	if (t->parent == NULL || t->parent->sparc_bus_map == NULL) {
18339a57b1cSmrg 		printf("\ncentral_bus_map: invalid parent");
18439a57b1cSmrg 		return (EINVAL);
18539a57b1cSmrg 	}
18639a57b1cSmrg 
18739a57b1cSmrg 
18839a57b1cSmrg 	for (i = 0; i < sc->sc_nrange; i++) {
18939a57b1cSmrg 		bus_addr_t paddr;
19039a57b1cSmrg 
19139a57b1cSmrg 		if (sc->sc_range[i].cspace != slot)
19239a57b1cSmrg 			continue;
19339a57b1cSmrg 
19439a57b1cSmrg 		paddr = offset - sc->sc_range[i].coffset;
19539a57b1cSmrg 		paddr += sc->sc_range[i].poffset;
19639a57b1cSmrg 		paddr |= ((bus_addr_t)sc->sc_range[i].pspace << 32);
19739a57b1cSmrg 
19839a57b1cSmrg 		return bus_space_map(t->parent, paddr, size, flags, hp);
19939a57b1cSmrg 	}
20039a57b1cSmrg 
20139a57b1cSmrg 	return (EINVAL);
20239a57b1cSmrg }
203