rmixl_mainbus.c revision 1.4
11.4Sdyoung/*	$NetBSD: rmixl_mainbus.c,v 1.4 2011/07/01 19:01:31 dyoung 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.4Sdyoung__KERNEL_RCSID(0, "$NetBSD: rmixl_mainbus.c,v 1.4 2011/07/01 19:01:31 dyoung 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/malloc.h>
521.2Smatt#include <sys/device.h>
531.2Smatt
541.3Smatt#include <evbmips/rmixl/autoconf.h>
551.4Sdyoung#include <sys/bus.h>
561.2Smatt#include "locators.h"
571.2Smatt
581.2Smattstatic int  mainbusmatch(device_t,  cfdata_t, void *);
591.2Smattstatic void mainbusattach(device_t,  device_t,  void *);
601.3Smattstatic int  mainbus_node_alloc(struct mainbus_softc *, int);
611.3Smattstatic int  mainbus_search(device_t, cfdata_t, const int *, void *);
621.3Smattstatic int  mainbus_print(void *, const char *);
631.2Smatt
641.3SmattCFATTACH_DECL_NEW(mainbus, sizeof(struct mainbus_softc),
651.3Smatt	mainbusmatch, mainbusattach, NULL, NULL);
661.2Smatt
671.2Smattstatic int mainbus_found;
681.2Smatt
691.2Smattstatic int
701.2Smattmainbusmatch(device_t parent, cfdata_t cf, void *aux)
711.2Smatt{
721.2Smatt	if (mainbus_found)
731.2Smatt		return 0;
741.2Smatt	return 1;
751.2Smatt}
761.2Smatt
771.3Smattstatic void
781.3Smattmainbusattach(device_t parent, device_t self, void *aux)
791.3Smatt{
801.3Smatt	struct mainbus_softc *sc = device_private(self);
811.3Smatt
821.3Smatt	aprint_naive("\n");
831.3Smatt	aprint_normal("\n");
841.3Smatt
851.3Smatt	sc->sc_dev = self;
861.3Smatt	sc->sc_node_next = 0;
871.3Smatt	sc->sc_node_mask = 0;
881.3Smatt
891.3Smatt	mainbus_found = 1;
901.3Smatt
911.3Smatt	/*
921.3Smatt	 * attach mainbus devices
931.3Smatt	 */
941.3Smatt        config_search_ia(mainbus_search, self, "mainbus", mainbus_print);
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.3Smatt		panic("%s: node mask underflow", __func__);
1241.3Smatt	} else {
1251.3Smatt		if (node >= 64)
1261.3Smatt			panic("%s: node >= 64", __func__);
1271.3Smatt		if (node < 0)
1281.3Smatt			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.3Smatt	if (config_match(parent, cf, &ma) > 0)
1531.3Smatt		config_attach(parent, cf, &ma, mainbus_print);
1541.2Smatt
1551.3Smatt	return 0;
1561.2Smatt}
1571.3Smatt
158