1238730Sdelphij/*	$NetBSD: mainbus.c,v 1.7 2023/12/20 14:12:25 thorpej Exp $	*/
2294286Sdelphij
3238730Sdelphij/*
4238730Sdelphij * Copyright 2002 Wasabi Systems, Inc.
5238730Sdelphij * All rights reserved.
6238730Sdelphij *
7238730Sdelphij * Written by Simon Burge for Wasabi Systems, Inc.
8238730Sdelphij *
960786Sps * Redistribution and use in source and binary forms, with or without
1060786Sps * modification, are permitted provided that the following conditions
1160786Sps * are met:
1260786Sps * 1. Redistributions of source code must retain the above copyright
1360786Sps *    notice, this list of conditions and the following disclaimer.
1460786Sps * 2. Redistributions in binary form must reproduce the above copyright
1560786Sps *    notice, this list of conditions and the following disclaimer in the
1660786Sps *    documentation and/or other materials provided with the distribution.
1760786Sps * 3. All advertising materials mentioning features or use of this software
1860786Sps *    must display the following acknowledgement:
1960786Sps *      This product includes software developed for the NetBSD Project by
2060786Sps *      Wasabi Systems, Inc.
2160786Sps * 4. The name of Wasabi Systems, Inc. may not be used to endorse
2260786Sps *    or promote products derived from this software without specific prior
2360786Sps *    written permission.
2460786Sps *
2560786Sps * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
2660786Sps * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2760786Sps * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2860786Sps * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
2960786Sps * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3060786Sps * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3160786Sps * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3260786Sps * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3360786Sps * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3460786Sps * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3560786Sps * POSSIBILITY OF SUCH DAMAGE.
3660786Sps */
3760786Sps
3860786Sps#include <sys/cdefs.h>
3960786Sps__KERNEL_RCSID(0, "$NetBSD: mainbus.c,v 1.7 2023/12/20 14:12:25 thorpej Exp $");
4060786Sps
4160786Sps#include "opt_pci.h"
4260786Sps
4360786Sps#include <sys/param.h>
4460786Sps#include <sys/systm.h>
4560786Sps#include <sys/device.h>
4660786Sps
4760786Sps#include <dev/pci/pcivar.h>
4860786Sps#if defined(PCI_NETBSD_CONFIGURE)
4960786Sps#include <dev/pci/pciconf.h>
5060786Sps#endif
5160786Sps
5260786Sps#include <mips/cache.h>
5360786Sps#include <mips/cpuregs.h>
5460786Sps
5560786Sps#include <mips/bonito/bonitoreg.h>
5660786Sps
5760786Sps#include <evbmips/loongson/autoconf.h>
5860786Sps#if defined(PCI_NETBSD_CONFIGURE)
5960786Sps#include <evbmips/loongson/loongson_bus_defs.h>
6060786Sps#endif
6160786Sps
6260786Sps#include "locators.h"
6360786Sps#include "pci.h"
6460786Sps
6560786Spsstatic int	mainbus_match(device_t, cfdata_t, void *);
6660786Spsstatic void	mainbus_attach(device_t, device_t, void *);
6760786Spsstatic int	mainbus_print(void *, const char *);
6860786Sps
6960786SpsCFATTACH_DECL_NEW(mainbus, 0,
7060786Sps    mainbus_match, mainbus_attach, NULL, NULL);
7160786Sps
7260786Sps/* There can be only one. */
7360786Spsstatic bool mainbus_found;
7460786Sps
7560786Spsconst char * const mainbusdevs[] = {
7660786Sps	"cpu",
7760786Sps	"bonito",
7860786Sps#if 0
7960786Sps	"i2c",
8060786Sps	"gpio",
8160786Sps#endif
8260786Sps};
8360786Sps
8460786Sps#define	PCI_IO_START	0x00001000
8560786Sps#define	PCI_IO_END	0x00003fff
8660786Sps#define	PCI_IO_SIZE	((PCI_IO_END - PCI_IO_START) + 1)
8760786Sps
8860786Sps#define	PCI_MEM_START	0
8960786Sps#define	PCI_MEM_SIZE	BONITO_PCILO_SIZE
9060786Sps
9160786Spsstatic int
9260786Spsmainbus_match(device_t parent, cfdata_t match, void *aux)
9360786Sps{
9460786Sps	if (mainbus_found)
9560786Sps		return (0);
9660786Sps
9760786Sps	return (1);
9860786Sps}
9960786Sps
10060786Spsstatic void
101mainbus_attach(device_t parent, device_t self, void *aux)
102{
103	size_t i;
104
105	mainbus_found = true;
106	aprint_normal("\n");
107
108#if defined(PCI_NETBSD_CONFIGURE)
109	struct mips_cache_info * const mci = &mips_cache_info;
110	struct pciconf_resources *pcires = pciconf_resource_init();
111
112	pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
113	    PCI_IO_START, PCI_IO_SIZE);
114	pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
115	    PCI_MEM_START, PCI_MEM_SIZE);
116
117	pci_configure_bus(&bonito_pc, pcires, 0, mci->mci_dcache_align);
118	pciconf_resource_fini(pcires);
119#endif /* PCI_NETBSD_CONFIGURE */
120
121	for (i = 0; i < __arraycount(mainbusdevs); i++) {
122		struct mainbus_attach_args maa;
123		maa.maa_name = mainbusdevs[i];
124		(void) config_found(self, &maa, mainbus_print, CFARGS_NONE);
125	}
126}
127
128static int
129mainbus_print(void *aux, const char *pnp)
130{
131	struct mainbus_attach_args *maa = aux;
132
133	if (pnp)
134		aprint_normal("%s at %s", maa->maa_name, pnp);
135
136	return UNCONF;
137}
138