xref: /netbsd-src/sys/arch/sparc64/dev/upa.c (revision 3944ff70a48a86bb622139705a33f8ef461e24f7)
1*3944ff70Sthorpej /*	$NetBSD: upa.c,v 1.24 2022/01/22 11:49:17 thorpej Exp $	*/
299585750Smartin /*	$OpenBSD: upa.c,v 1.8 2008/01/17 22:53:18 kettenis Exp $	*/
399585750Smartin 
499585750Smartin /*
599585750Smartin  * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
699585750Smartin  * All rights reserved.
799585750Smartin  *
899585750Smartin  * Redistribution and use in source and binary forms, with or without
999585750Smartin  * modification, are permitted provided that the following conditions
1099585750Smartin  * are met:
1199585750Smartin  * 1. Redistributions of source code must retain the above copyright
1299585750Smartin  *    notice, this list of conditions and the following disclaimer.
1399585750Smartin  * 2. Redistributions in binary form must reproduce the above copyright
1499585750Smartin  *    notice, this list of conditions and the following disclaimer in the
1599585750Smartin  *    documentation and/or other materials provided with the distribution.
1699585750Smartin  *
1799585750Smartin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1899585750Smartin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1999585750Smartin  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2099585750Smartin  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
2199585750Smartin  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2299585750Smartin  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2399585750Smartin  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2499585750Smartin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2599585750Smartin  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
2699585750Smartin  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2799585750Smartin  * POSSIBILITY OF SUCH DAMAGE.
2899585750Smartin  *
2999585750Smartin  * Effort sponsored in part by the Defense Advanced Research Projects
3099585750Smartin  * Agency (DARPA) and Air Force Research Laboratory, Air Force
3199585750Smartin  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
3299585750Smartin  */
3399585750Smartin 
34f72d1cdaSmrg #include <sys/cdefs.h>
35*3944ff70Sthorpej __KERNEL_RCSID(0, "$NetBSD: upa.c,v 1.24 2022/01/22 11:49:17 thorpej Exp $");
36f72d1cdaSmrg 
3799585750Smartin #include <sys/types.h>
3899585750Smartin #include <sys/param.h>
3999585750Smartin #include <sys/systm.h>
4099585750Smartin #include <sys/kernel.h>
4199585750Smartin #include <sys/device.h>
4299585750Smartin #include <sys/conf.h>
43fc256c4aSthorpej #include <sys/kmem.h>
4499585750Smartin 
45b6584574Sdyoung #include <sys/bus.h>
4699585750Smartin #include <machine/autoconf.h>
4799585750Smartin #include <machine/openfirm.h>
4899585750Smartin 
4999585750Smartin struct upa_range {
5099585750Smartin 	u_int64_t	ur_space;
5199585750Smartin 	u_int64_t	ur_addr;
5299585750Smartin 	u_int64_t	ur_len;
5399585750Smartin };
5499585750Smartin 
5599585750Smartin struct upa_softc {
5688655ce2Schristos 	device_t		sc_dev;
5799585750Smartin 	bus_space_tag_t		sc_bt;
5899585750Smartin 	bus_space_handle_t	sc_reg[3];
5999585750Smartin 	struct upa_range	*sc_range;
6099585750Smartin 	int			sc_node;
6199585750Smartin 	int			sc_nrange;
6299585750Smartin 	bus_space_tag_t		sc_cbt;
6399585750Smartin };
6499585750Smartin 
6588655ce2Schristos int	upa_match(device_t, cfdata_t, void *);
6688655ce2Schristos void	upa_attach(device_t, device_t, void *);
6799585750Smartin 
6888655ce2Schristos CFATTACH_DECL_NEW(upa, sizeof(struct upa_softc),
6999585750Smartin     upa_match, upa_attach, NULL, NULL);
7099585750Smartin 
7199585750Smartin int upa_print(void *, const char *);
7299585750Smartin bus_space_tag_t upa_alloc_bus_tag(struct upa_softc *);
7399585750Smartin int upa_bus_map(bus_space_tag_t, bus_addr_t,
7499585750Smartin     bus_size_t, int, vaddr_t, bus_space_handle_t *);
7599585750Smartin paddr_t upa_bus_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
7699585750Smartin 
7799585750Smartin int
upa_match(device_t parent,cfdata_t match,void * aux)7888655ce2Schristos upa_match(device_t parent, cfdata_t match, void *aux)
7999585750Smartin {
8099585750Smartin 	struct mainbus_attach_args *ma = aux;
8199585750Smartin 
8299585750Smartin 	if (strcmp(ma->ma_name, "upa") == 0)
8399585750Smartin 		return (1);
8499585750Smartin 
8599585750Smartin 	return (0);
8699585750Smartin }
8799585750Smartin 
8899585750Smartin void
upa_attach(device_t parent,device_t self,void * aux)8988655ce2Schristos upa_attach(device_t parent, device_t self, void *aux)
9099585750Smartin {
9199585750Smartin 	struct upa_softc *sc = device_private(self);
9299585750Smartin 	struct mainbus_attach_args *ma = aux;
9399585750Smartin 	int i, node;
9499585750Smartin 
9588655ce2Schristos 	sc->sc_dev = self;
9699585750Smartin 	sc->sc_bt = ma->ma_bustag;
9799585750Smartin 	sc->sc_node = ma->ma_node;
9899585750Smartin 
9999585750Smartin 	for (i = 0; i < 3; i++) {
10099585750Smartin 		if (i >= ma->ma_nreg) {
10199585750Smartin 			printf(": no register %d\n", i);
10299585750Smartin 			return;
10399585750Smartin 		}
10499585750Smartin 		if (bus_space_map(sc->sc_bt, ma->ma_reg[i].ur_paddr,
10599585750Smartin 		    ma->ma_reg[i].ur_len, 0, &sc->sc_reg[i])) {
10699585750Smartin 			printf(": failed to map reg%d\n", i);
10799585750Smartin 			return;
10899585750Smartin 		}
10999585750Smartin 	}
11099585750Smartin 
11199585750Smartin 	if (prom_getprop(sc->sc_node, "ranges", sizeof(struct upa_range),
11299585750Smartin 	    &sc->sc_nrange, (void **)&sc->sc_range))
11399585750Smartin 		panic("upa: can't get ranges");
11499585750Smartin 
11599585750Smartin 	printf("\n");
11699585750Smartin 
11799585750Smartin 	sc->sc_cbt = upa_alloc_bus_tag(sc);
11899585750Smartin 
119*3944ff70Sthorpej 	devhandle_t selfh = device_handle(sc->sc_dev);
12099585750Smartin 	for (node = OF_child(sc->sc_node); node; node = OF_peer(node)) {
12199585750Smartin 		char buf[32];
12299585750Smartin 		struct mainbus_attach_args map;
12399585750Smartin 
12499585750Smartin 		bzero(&map, sizeof(map));
12599585750Smartin 		if (OF_getprop(node, "device_type", buf, sizeof(buf)) <= 0)
12699585750Smartin 			continue;
12799585750Smartin 		if (prom_getprop(node, "reg", sizeof(*map.ma_reg),
12899585750Smartin 		    &map.ma_nreg, (void **)&map.ma_reg) != 0)
12999585750Smartin 			continue;
13099585750Smartin 		if (OF_getprop(node, "name", buf, sizeof(buf)) <= 0)
13199585750Smartin 			continue;
13299585750Smartin 		map.ma_node = node;
13399585750Smartin 		map.ma_name = buf;
13499585750Smartin 		map.ma_bustag = sc->sc_cbt;
13599585750Smartin 		map.ma_dmatag = ma->ma_dmatag;
13665c738d1Sthorpej 		config_found(sc->sc_dev, &map, upa_print,
137*3944ff70Sthorpej 		    CFARGS(.devhandle = prom_node_to_devhandle(selfh, node)));
13899585750Smartin 	}
13999585750Smartin }
14099585750Smartin 
14199585750Smartin int
upa_print(void * args,const char * name)14299585750Smartin upa_print(void *args, const char *name)
14399585750Smartin {
14499585750Smartin 	struct mainbus_attach_args *ma = args;
14599585750Smartin 
14699585750Smartin 	if (name)
14799585750Smartin 		printf("\"%s\" at %s", ma->ma_name, name);
14899585750Smartin 	return (UNCONF);
14999585750Smartin }
15099585750Smartin 
15199585750Smartin bus_space_tag_t
upa_alloc_bus_tag(struct upa_softc * sc)15299585750Smartin upa_alloc_bus_tag(struct upa_softc *sc)
15399585750Smartin {
15499585750Smartin 	struct sparc_bus_space_tag *bt;
15599585750Smartin 
156fc256c4aSthorpej 	bt = kmem_zalloc(sizeof(*bt), KM_SLEEP);
15799585750Smartin 	*bt = *sc->sc_bt;
15899585750Smartin 	bt->cookie = sc;
15999585750Smartin 	bt->parent = sc->sc_bt;
16099585750Smartin 	bt->sparc_bus_map = upa_bus_map;
16199585750Smartin 	bt->sparc_bus_mmap = upa_bus_mmap;
16299585750Smartin 
16399585750Smartin 	return bt;
16499585750Smartin }
16599585750Smartin 
16699585750Smartin int
upa_bus_map(bus_space_tag_t t,bus_addr_t offset,bus_size_t size,int flags,vaddr_t v,bus_space_handle_t * hp)16799585750Smartin upa_bus_map(bus_space_tag_t t, bus_addr_t offset,
16899585750Smartin     bus_size_t size, int flags, vaddr_t v, bus_space_handle_t *hp)
16999585750Smartin {
17099585750Smartin 	struct upa_softc *sc = t->cookie;
17199585750Smartin 	int i;
17299585750Smartin 
17399585750Smartin 	if (t->parent == 0 || t->parent->sparc_bus_map == 0) {
17499585750Smartin 		printf("\n__upa_bus_map: invalid parent");
17599585750Smartin 		return (EINVAL);
17699585750Smartin 	}
17799585750Smartin 
17899585750Smartin 	t = t->parent;
17999585750Smartin 
18099585750Smartin 	for (i = 0; i < sc->sc_nrange; i++) {
18199585750Smartin 		if (offset < sc->sc_range[i].ur_space)
18299585750Smartin 			continue;
18399585750Smartin 		if (offset >= (sc->sc_range[i].ur_space +
18499585750Smartin 		    sc->sc_range[i].ur_space))
18599585750Smartin 			continue;
18699585750Smartin 		break;
18799585750Smartin 	}
18899585750Smartin 	if (i == sc->sc_nrange)
18999585750Smartin 		return (EINVAL);
19099585750Smartin 
19199585750Smartin 	offset -= sc->sc_range[i].ur_space;
19299585750Smartin 	offset += sc->sc_range[i].ur_addr;
19399585750Smartin 
19499585750Smartin 	return ((*t->sparc_bus_map)(t, offset, size, flags, v, hp));
19599585750Smartin }
19699585750Smartin 
19799585750Smartin paddr_t
upa_bus_mmap(bus_space_tag_t t,bus_addr_t paddr,off_t off,int prot,int flags)19899585750Smartin upa_bus_mmap(bus_space_tag_t t, bus_addr_t paddr, off_t off, int prot,
19999585750Smartin     int flags)
20099585750Smartin {
20199585750Smartin 	struct upa_softc *sc = t->cookie;
20299585750Smartin 	int i;
20399585750Smartin 
20499585750Smartin 	if (t->parent == 0 || t->parent->sparc_bus_map == 0) {
20599585750Smartin 		printf("\n__upa_bus_map: invalid parent");
20699585750Smartin 		return (EINVAL);
20799585750Smartin 	}
20899585750Smartin 
20999585750Smartin 	t = t->parent;
21099585750Smartin 
21199585750Smartin 	for (i = 0; i < sc->sc_nrange; i++) {
21299585750Smartin 		if (paddr + off < sc->sc_range[i].ur_space)
21399585750Smartin 			continue;
21499585750Smartin 		if (paddr + off >= (sc->sc_range[i].ur_space +
21599585750Smartin 		    sc->sc_range[i].ur_space))
21699585750Smartin 			continue;
21799585750Smartin 		break;
21899585750Smartin 	}
21999585750Smartin 	if (i == sc->sc_nrange)
22099585750Smartin 		return (EINVAL);
22199585750Smartin 
22299585750Smartin 	paddr -= sc->sc_range[i].ur_space;
22399585750Smartin 	paddr += sc->sc_range[i].ur_addr;
22499585750Smartin 
22599585750Smartin 	return ((*t->sparc_bus_mmap)(t, paddr, off, prot, flags));
22699585750Smartin }
227