11.18Sthorpej/*	$NetBSD: mainbus.c,v 1.18 2023/12/20 14:12:25 thorpej Exp $	*/
21.1Ssimonb
31.1Ssimonb/*
41.1Ssimonb * Copyright 2002 Wasabi Systems, Inc.
51.1Ssimonb * All rights reserved.
61.1Ssimonb *
71.1Ssimonb * Written by Simon Burge for Wasabi Systems, Inc.
81.1Ssimonb *
91.1Ssimonb * Redistribution and use in source and binary forms, with or without
101.1Ssimonb * modification, are permitted provided that the following conditions
111.1Ssimonb * are met:
121.1Ssimonb * 1. Redistributions of source code must retain the above copyright
131.1Ssimonb *    notice, this list of conditions and the following disclaimer.
141.1Ssimonb * 2. Redistributions in binary form must reproduce the above copyright
151.1Ssimonb *    notice, this list of conditions and the following disclaimer in the
161.1Ssimonb *    documentation and/or other materials provided with the distribution.
171.1Ssimonb * 3. All advertising materials mentioning features or use of this software
181.1Ssimonb *    must display the following acknowledgement:
191.1Ssimonb *      This product includes software developed for the NetBSD Project by
201.1Ssimonb *      Wasabi Systems, Inc.
211.1Ssimonb * 4. The name of Wasabi Systems, Inc. may not be used to endorse
221.1Ssimonb *    or promote products derived from this software without specific prior
231.1Ssimonb *    written permission.
241.1Ssimonb *
251.1Ssimonb * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
261.1Ssimonb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
271.1Ssimonb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
281.1Ssimonb * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
291.1Ssimonb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
301.1Ssimonb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
311.1Ssimonb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
321.1Ssimonb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
331.1Ssimonb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
341.1Ssimonb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
351.1Ssimonb * POSSIBILITY OF SUCH DAMAGE.
361.1Ssimonb */
371.7Slukem
381.7Slukem#include <sys/cdefs.h>
391.18Sthorpej__KERNEL_RCSID(0, "$NetBSD: mainbus.c,v 1.18 2023/12/20 14:12:25 thorpej Exp $");
401.1Ssimonb
411.1Ssimonb#include "opt_pci.h"
421.1Ssimonb
431.1Ssimonb#include <sys/param.h>
441.1Ssimonb#include <sys/systm.h>
451.1Ssimonb#include <sys/device.h>
461.1Ssimonb
471.1Ssimonb#include <dev/pci/pcivar.h>
481.1Ssimonb#if defined(PCI_NETBSD_CONFIGURE)
491.1Ssimonb#include <dev/pci/pciconf.h>
501.1Ssimonb#endif
511.1Ssimonb
521.1Ssimonb#include <mips/cache.h>
531.1Ssimonb#include <mips/cpuregs.h>
541.1Ssimonb
551.1Ssimonb#include <evbmips/malta/autoconf.h>
561.1Ssimonb#include <evbmips/malta/maltareg.h>
571.1Ssimonb#include <evbmips/malta/maltavar.h>
581.1Ssimonb
591.1Ssimonb#if defined(PCI_NETBSD_ENABLE_IDE)
601.1Ssimonb#include <dev/pci/pciide_piix_reg.h>
611.1Ssimonb#endif /* PCI_NETBSD_ENABLE_IDE */
621.1Ssimonb
631.1Ssimonb#include "locators.h"
641.1Ssimonb#include "pci.h"
651.1Ssimonb
661.12Smattstatic int	mainbus_match(device_t, cfdata_t, void *);
671.12Smattstatic void	mainbus_attach(device_t, device_t, void *);
681.12Smattstatic int	mainbus_submatch(device_t, cfdata_t, const int *, void *);
691.1Ssimonbstatic int	mainbus_print(void *, const char *);
701.1Ssimonb
711.12SmattCFATTACH_DECL_NEW(mainbus, 0,
721.5Sthorpej    mainbus_match, mainbus_attach, NULL, NULL);
731.1Ssimonb
741.1Ssimonb/* There can be only one. */
751.12Smattbool mainbus_found;
761.1Ssimonb
771.1Ssimonbstruct mainbusdev {
781.1Ssimonb	const char *md_name;
791.1Ssimonb	bus_addr_t md_addr;
801.1Ssimonb	int md_intr;
811.1Ssimonb};
821.1Ssimonb
831.12Smattconst struct mainbusdev mainbusdevs[] = {
841.1Ssimonb	{ "cpu",		-1,			-1 },
851.1Ssimonb	{ "gt",			MALTA_CORECTRL_BASE,	-1 },
861.1Ssimonb	{ "com",		MALTA_CBUSUART,		MALTA_CBUSUART_INTR },
871.1Ssimonb	{ "i2c",		MALTA_I2C_BASE,		-1 },
881.1Ssimonb	{ "gpio",		MALTA_GPIO_BASE,	-1 },
891.1Ssimonb	{ NULL,			0,			0 },
901.1Ssimonb};
911.1Ssimonb
921.15Sthorpej#define	PCI_IO_START	0x00001000
931.15Sthorpej#define	PCI_IO_END	0x0000efff
941.15Sthorpej#define	PCI_IO_SIZE	((PCI_IO_END - PCI_IO_START) + 1)
951.15Sthorpej
961.15Sthorpej#define	PCI_MEM_START	MALTA_PCIMEM1_BASE
971.15Sthorpej#define	PCI_MEM_SIZE	MALTA_PCIMEM1_SIZE
981.15Sthorpej
991.1Ssimonbstatic int
1001.12Smattmainbus_match(device_t parent, cfdata_t match, void *aux)
1011.1Ssimonb{
1021.1Ssimonb
1031.1Ssimonb	if (mainbus_found)
1041.1Ssimonb		return (0);
1051.1Ssimonb
1061.1Ssimonb	return (1);
1071.1Ssimonb}
1081.1Ssimonb
1091.1Ssimonbstatic void
1101.12Smattmainbus_attach(device_t parent, device_t self, void *aux)
1111.1Ssimonb{
1121.1Ssimonb	struct mainbus_attach_args ma;
1131.12Smatt	const struct mainbusdev *md;
1141.13Sskrll#if defined(PCI_NETBSD_ENABLE_IDE) || defined(PCI_NETBSD_CONFIGURE)
1151.13Sskrll	struct malta_config *mcp = &malta_configuration;
1161.13Sskrll	pci_chipset_tag_t pc = &mcp->mc_pc;
1171.1Ssimonb#endif
1181.1Ssimonb
1191.12Smatt	mainbus_found = true;
1201.1Ssimonb	printf("\n");
1211.1Ssimonb
1221.1Ssimonb#if defined(PCI_NETBSD_CONFIGURE)
1231.13Sskrll	struct mips_cache_info * const mci = &mips_cache_info;
1241.15Sthorpej	struct pciconf_resources *pcires = pciconf_resource_init();
1251.15Sthorpej
1261.15Sthorpej	pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
1271.15Sthorpej	    PCI_IO_START, PCI_IO_SIZE);
1281.15Sthorpej	pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
1291.15Sthorpej	    PCI_MEM_START, PCI_MEM_SIZE);
1301.13Sskrll
1311.15Sthorpej	pci_configure_bus(pc, pcires, 0, mci->mci_dcache_align);
1321.15Sthorpej	pciconf_resource_fini(pcires);
1331.1Ssimonb#endif /* PCI_NETBSD_CONFIGURE */
1341.1Ssimonb
1351.1Ssimonb#if defined(PCI_NETBSD_ENABLE_IDE)
1361.1Ssimonb	/*
1371.1Ssimonb	 * Perhaps PMON has not enabled the IDE controller.  Easy to
1381.1Ssimonb	 * fix -- just set the ENABLE bits for each channel in the
1391.1Ssimonb	 * IDETIM register.  Just clear all the bits for the channel
1401.1Ssimonb	 * except for the ENABLE bits -- the `pciide' driver will
1411.1Ssimonb	 * properly configure it later.
1421.1Ssimonb	 */
1431.15Sthorpej	pcireg_t idetim = 0;
1441.1Ssimonb	if (PCI_NETBSD_ENABLE_IDE & 0x01)
1451.1Ssimonb		idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE, 0);
1461.1Ssimonb	if (PCI_NETBSD_ENABLE_IDE & 0x02)
1471.1Ssimonb		idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE, 1);
1481.1Ssimonb
1491.1Ssimonb	/* pciide0 is pci device 10, function 1 */
1501.15Sthorpej	pcitag_t idetag = pci_make_tag(pc, 0, 10, 1);
1511.1Ssimonb	pci_conf_write(pc, idetag, PIIX_IDETIM, idetim);
1521.1Ssimonb#endif
1531.1Ssimonb	for (md = mainbusdevs; md->md_name != NULL; md++) {
1541.1Ssimonb		ma.ma_name = md->md_name;
1551.1Ssimonb		ma.ma_addr = md->md_addr;
1561.1Ssimonb		ma.ma_intr = md->md_intr;
1571.16Sthorpej		config_found(self, &ma, mainbus_print,
1581.17Sthorpej		    CFARGS(.submatch = mainbus_submatch));
1591.1Ssimonb	}
1601.1Ssimonb}
1611.1Ssimonb
1621.1Ssimonbstatic int
1631.12Smattmainbus_submatch(device_t parent, cfdata_t cf,
1641.9Sdrochner		 const int *ldesc, void *aux)
1651.1Ssimonb{
1661.1Ssimonb	struct mainbus_attach_args *ma = aux;
1671.1Ssimonb
1681.1Ssimonb	if (cf->cf_loc[MAINBUSCF_ADDR] != MAINBUSCF_ADDR_DEFAULT &&
1691.1Ssimonb	    cf->cf_loc[MAINBUSCF_ADDR] != ma->ma_addr)
1701.1Ssimonb		return (0);
1711.1Ssimonb
1721.2Sthorpej	return (config_match(parent, cf, aux));
1731.1Ssimonb}
1741.1Ssimonb
1751.1Ssimonbstatic int
1761.1Ssimonbmainbus_print(void *aux, const char *pnp)
1771.1Ssimonb{
1781.1Ssimonb	struct mainbus_attach_args *ma = aux;
1791.1Ssimonb
1801.1Ssimonb	if (pnp != 0)
1811.1Ssimonb		return QUIET;
1821.1Ssimonb
1831.1Ssimonb	if (pnp)
1841.6Sthorpej		aprint_normal("%s at %s", ma->ma_name, pnp);
1851.1Ssimonb	if (ma->ma_addr != MAINBUSCF_ADDR_DEFAULT)
1861.6Sthorpej		aprint_normal(" addr 0x%lx", ma->ma_addr);
1871.1Ssimonb
1881.1Ssimonb	return (UNCONF);
1891.1Ssimonb}
190