11.8Sskrll/*	$NetBSD: rmixl_mainbus.c,v 1.8 2022/09/29 07:00:47 skrll Exp $	*/
21.2Smatt
31.2Smatt/*
41.2Smatt * Copyright (c) 1994,1995 Mark Brinicombe.
51.2Smatt * Copyright (c) 1994 Brini.
61.2Smatt * All rights reserved.
71.2Smatt *
81.2Smatt * Redistribution and use in source and binary forms, with or without
91.2Smatt * modification, are permitted provided that the following conditions
101.2Smatt * are met:
111.2Smatt * 1. Redistributions of source code must retain the above copyright
121.2Smatt *    notice, this list of conditions and the following disclaimer.
131.2Smatt * 2. Redistributions in binary form must reproduce the above copyright
141.2Smatt *    notice, this list of conditions and the following disclaimer in the
151.2Smatt *    documentation and/or other materials provided with the distribution.
161.2Smatt * 3. All advertising materials mentioning features or use of this software
171.2Smatt *    must display the following acknowledgement:
181.2Smatt *	This product includes software developed by Brini.
191.2Smatt * 4. The name of the company nor the name of the author may be used to
201.2Smatt *    endorse or promote products derived from this software without specific
211.2Smatt *    prior written permission.
221.2Smatt *
231.2Smatt * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
241.2Smatt * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
251.2Smatt * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
261.2Smatt * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
271.2Smatt * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
281.2Smatt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
291.2Smatt * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
301.2Smatt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
311.2Smatt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
321.2Smatt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
331.2Smatt * SUCH DAMAGE.
341.2Smatt *
351.2Smatt * RiscBSD kernel project
361.2Smatt *
371.2Smatt * mainbus.c
381.2Smatt *
391.2Smatt * mainbus configuration
401.2Smatt *
411.2Smatt * Created      : 15/12/94
421.2Smatt */
431.2Smatt
441.2Smatt#include <sys/cdefs.h>
451.8Sskrll__KERNEL_RCSID(0, "$NetBSD: rmixl_mainbus.c,v 1.8 2022/09/29 07:00:47 skrll Exp $");
461.2Smatt
471.2Smatt#include <sys/param.h>
481.2Smatt#include <sys/systm.h>
491.2Smatt#include <sys/kernel.h>
501.2Smatt#include <sys/conf.h>
511.2Smatt#include <sys/device.h>
521.2Smatt
531.3Smatt#include <evbmips/rmixl/autoconf.h>
541.4Sdyoung#include <sys/bus.h>
551.2Smatt#include "locators.h"
561.2Smatt
571.2Smattstatic int  mainbusmatch(device_t,  cfdata_t, void *);
581.2Smattstatic void mainbusattach(device_t,  device_t,  void *);
591.3Smattstatic int  mainbus_node_alloc(struct mainbus_softc *, int);
601.3Smattstatic int  mainbus_search(device_t, cfdata_t, const int *, void *);
611.3Smattstatic int  mainbus_print(void *, const char *);
621.2Smatt
631.3SmattCFATTACH_DECL_NEW(mainbus, sizeof(struct mainbus_softc),
641.3Smatt	mainbusmatch, mainbusattach, NULL, NULL);
651.2Smatt
661.2Smattstatic int mainbus_found;
671.2Smatt
681.2Smattstatic int
691.2Smattmainbusmatch(device_t parent, cfdata_t cf, void *aux)
701.2Smatt{
711.2Smatt	if (mainbus_found)
721.2Smatt		return 0;
731.2Smatt	return 1;
741.2Smatt}
751.2Smatt
761.3Smattstatic void
771.3Smattmainbusattach(device_t parent, device_t self, void *aux)
781.3Smatt{
791.3Smatt	struct mainbus_softc *sc = device_private(self);
801.3Smatt
811.3Smatt	aprint_naive("\n");
821.3Smatt	aprint_normal("\n");
831.3Smatt
841.3Smatt	sc->sc_dev = self;
851.3Smatt	sc->sc_node_next = 0;
861.3Smatt	sc->sc_node_mask = 0;
871.3Smatt
881.3Smatt	mainbus_found = 1;
891.3Smatt
901.3Smatt	/*
911.7Sskrll	 * attach mainbus devices
921.3Smatt	 */
931.5Sthorpej	config_search(self, NULL,
941.6Sthorpej	    CFARGS(.search = mainbus_search));
951.3Smatt}
961.3Smatt
971.3Smattstatic int
981.3Smattmainbus_print(void *aux, const char *pnp)
991.3Smatt{
1001.3Smatt	struct mainbus_attach_args *ma = aux;
1011.3Smatt
1021.3Smatt	if (pnp != NULL)
1031.3Smatt		aprint_normal("%s:", pnp);
1041.3Smatt	aprint_normal(" node %d", ma->ma_node);
1051.3Smatt
1061.3Smatt	return (UNCONF);
1071.3Smatt}
1081.3Smatt
1091.2Smattstatic int
1101.3Smattmainbus_node_alloc(struct mainbus_softc *sc, int node)
1111.2Smatt{
1121.3Smatt	uint64_t bit;
1131.3Smatt
1141.3Smatt	if (node == MAINBUSCF_NODE_DEFAULT) {
1151.3Smatt		for (node=sc->sc_node_next; node < 64; node++) {
1161.3Smatt			bit = 1 << node;
1171.3Smatt			if ((sc->sc_node_mask & bit) == 0) {
1181.3Smatt				sc->sc_node_mask |= bit;
1191.3Smatt				sc->sc_node_next = node + 1;
1201.3Smatt				return node;
1211.3Smatt			}
1221.3Smatt		}
1231.7Sskrll		panic("%s: node mask underflow", __func__);
1241.3Smatt	} else {
1251.7Sskrll		if (node >= 64)
1261.7Sskrll			panic("%s: node >= 64", __func__);
1271.3Smatt		if (node < 0)
1281.7Sskrll			panic("%s: bad node %d", __func__, node);
1291.3Smatt		bit = 1 << node;
1301.3Smatt		if ((sc->sc_node_mask & bit) == 0) {
1311.3Smatt			sc->sc_node_mask |= bit;
1321.3Smatt			sc->sc_node_next = node + 1;
1331.3Smatt			return node;
1341.3Smatt		} else {
1351.3Smatt			panic("%s: node %d already used\n",
1361.3Smatt				__func__, node);
1371.3Smatt		}
1381.3Smatt	}
1391.2Smatt
1401.3Smatt	/*NOTREACHED*/
1411.3Smatt	return -1;	/* as if */
1421.2Smatt}
1431.2Smatt
1441.3Smattstatic int
1451.3Smattmainbus_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
1461.2Smatt{
1471.3Smatt	struct mainbus_softc *sc = device_private(parent);
1481.3Smatt	struct mainbus_attach_args ma;
1491.3Smatt
1501.3Smatt	ma.ma_node = mainbus_node_alloc(sc, cf->cf_loc[MAINBUSCF_NODE]);
1511.3Smatt
1521.5Sthorpej	if (config_probe(parent, cf, &ma))
1531.6Sthorpej		config_attach(parent, cf, &ma, mainbus_print, CFARGS_NONE);
1541.2Smatt
1551.3Smatt	return 0;
1561.2Smatt}
1571.3Smatt
158