mainbus.c revision 1.11
11.11Sthorpej/*	$NetBSD: mainbus.c,v 1.11 2025/09/06 22:53:48 thorpej Exp $	*/
21.1Shikaru
31.1Shikaru/*
41.1Shikaru * Copyright (c) 2007
51.1Shikaru *      Internet Initiative Japan, Inc.  All rights reserved.
61.1Shikaru *
71.1Shikaru * Redistribution and use in source and binary forms, with or without
81.1Shikaru * modification, are permitted provided that the following conditions
91.1Shikaru * are met:
101.1Shikaru * 1. Redistributions of source code must retain the above copyright
111.1Shikaru *    notice, this list of conditions and the following disclaimer.
121.1Shikaru * 2. Redistributions in binary form must reproduce the above copyright
131.1Shikaru *    notice, this list of conditions and the following disclaimer in the
141.1Shikaru *    documentation and/or other materials provided with the distribution.
151.1Shikaru *
161.1Shikaru * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
171.1Shikaru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
181.1Shikaru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191.1Shikaru * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
201.1Shikaru * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
211.1Shikaru * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
221.1Shikaru * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
231.1Shikaru * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241.1Shikaru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Shikaru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Shikaru * SUCH DAMAGE.
271.1Shikaru */
281.1Shikaru
291.1Shikaru#include <sys/cdefs.h>
301.11Sthorpej__KERNEL_RCSID(0, "$NetBSD: mainbus.c,v 1.11 2025/09/06 22:53:48 thorpej Exp $");
311.2Sjmcneill
321.2Sjmcneill#define	_MIPS_BUS_DMA_PRIVATE
331.1Shikaru
341.1Shikaru#include <sys/param.h>
351.1Shikaru#include <sys/systm.h>
361.1Shikaru#include <sys/device.h>
371.2Sjmcneill#include <sys/bus.h>
381.1Shikaru
391.1Shikaru#include <mips/cavium/include/mainbusvar.h>
401.3Sjmcneill#include <mips/cavium/octeonvar.h>
411.1Shikaru
421.2Sjmcneill#include <dev/fdt/fdtvar.h>
431.11Sthorpej#include <dev/fdt/fdt_console.h>
441.2Sjmcneill
451.1Shikarustatic int	mainbus_match(device_t, struct cfdata *, void *);
461.1Shikarustatic void	mainbus_attach(device_t, device_t, void *);
471.2Sjmcneillstatic void	mainbus_attach_static(device_t);
481.2Sjmcneillstatic void	mainbus_attach_devicetree(device_t);
491.1Shikarustatic int	mainbus_submatch(device_t, cfdata_t, const int *, void *);
501.1Shikarustatic int	mainbus_print(void *, const char *);
511.1Shikaru
521.2Sjmcneillstatic void	simplebus_bus_io_init(bus_space_tag_t, void *);
531.2Sjmcneill
541.1ShikaruCFATTACH_DECL_NEW(mainbus, sizeof(device_t), mainbus_match, mainbus_attach,
551.1Shikaru    NULL, NULL);
561.1Shikaru
571.2Sjmcneillstatic struct mips_bus_space simplebus_bus_tag;
581.2Sjmcneill
591.2Sjmcneillstatic struct mips_bus_dma_tag simplebus_dma_tag = {
601.2Sjmcneill	._cookie = NULL,
611.2Sjmcneill	._wbase = 0,
621.2Sjmcneill	._bounce_alloc_lo = 0,
631.2Sjmcneill	._bounce_alloc_hi = 0,
641.2Sjmcneill	._dmamap_ops = _BUS_DMAMAP_OPS_INITIALIZER,
651.2Sjmcneill	._dmamem_ops = _BUS_DMAMEM_OPS_INITIALIZER,
661.2Sjmcneill	._dmatag_ops = _BUS_DMATAG_OPS_INITIALIZER,
671.2Sjmcneill};
681.2Sjmcneill
691.1Shikarustatic int
701.1Shikarumainbus_match(device_t parent, struct cfdata *match, void *aux)
711.1Shikaru{
721.1Shikaru	static int once = 0;
731.1Shikaru
741.1Shikaru	if (once != 0)
751.1Shikaru		return 0;
761.1Shikaru	once = 1;
771.1Shikaru
781.1Shikaru	return 1;
791.1Shikaru}
801.1Shikaru
811.1Shikarustatic void
821.1Shikarumainbus_attach(device_t parent, device_t self, void *aux)
831.1Shikaru{
841.2Sjmcneill	aprint_normal("\n");
851.2Sjmcneill
861.2Sjmcneill	if (fdtbus_get_data() != NULL) {
871.2Sjmcneill		mainbus_attach_devicetree(self);
881.2Sjmcneill	} else {
891.2Sjmcneill		mainbus_attach_static(self);
901.2Sjmcneill	}
911.2Sjmcneill}
921.2Sjmcneill
931.2Sjmcneillstatic void
941.2Sjmcneillmainbus_attach_static(device_t self)
951.2Sjmcneill{
961.2Sjmcneill	struct mainbus_attach_args aa;
971.1Shikaru	int i;
981.1Shikaru
991.1Shikaru	for (i = 0; i < (int)mainbus_ndevs; i++) {
1001.1Shikaru		aa.aa_name = mainbus_devs[i];
1011.7Sthorpej		config_found(self, &aa, mainbus_print,
1021.9Sthorpej		    CFARGS(.submatch = mainbus_submatch,
1031.9Sthorpej			   .iattr = "mainbus"));
1041.1Shikaru	}
1051.1Shikaru}
1061.1Shikaru
1071.2Sjmcneillextern struct octeon_config octeon_configuration;
1081.2Sjmcneillextern void octpow_bootstrap(struct octeon_config *);
1091.2Sjmcneillextern void octfpa_bootstrap(struct octeon_config *);
1101.2Sjmcneill
1111.2Sjmcneillstatic void
1121.2Sjmcneillmainbus_attach_devicetree(device_t self)
1131.2Sjmcneill{
1141.3Sjmcneill	const struct fdt_console *cons = fdtbus_get_console();
1151.2Sjmcneill	struct mainbus_attach_args aa;
1161.2Sjmcneill	struct fdt_attach_args faa;
1171.3Sjmcneill	u_int uart_freq;
1181.2Sjmcneill
1191.2Sjmcneill	aa.aa_name = "cpunode";
1201.7Sthorpej	config_found(self, &aa, mainbus_print,
1211.9Sthorpej	    CFARGS(.submatch = mainbus_submatch,
1221.9Sthorpej		   .iattr = "mainbus"));
1231.2Sjmcneill
1241.4Sjmcneill	aa.aa_name = "iobus";
1251.7Sthorpej	config_found(self, &aa, mainbus_print,
1261.9Sthorpej	    CFARGS(.submatch = mainbus_submatch,
1271.9Sthorpej		   .iattr = "mainbus"));
1281.4Sjmcneill
1291.2Sjmcneill	simplebus_bus_io_init(&simplebus_bus_tag, NULL);
1301.2Sjmcneill
1311.2Sjmcneill	faa.faa_bst = &simplebus_bus_tag;
1321.2Sjmcneill	faa.faa_dmat = &simplebus_dma_tag;
1331.2Sjmcneill	faa.faa_name = "";
1341.3Sjmcneill
1351.3Sjmcneill	if (cons != NULL) {
1361.3Sjmcneill		faa.faa_phandle = fdtbus_get_stdout_phandle();
1371.3Sjmcneill
1381.3Sjmcneill		if (of_getprop_uint32(faa.faa_phandle, "clock-frequency",
1391.3Sjmcneill		    &uart_freq) != 0) {
1401.3Sjmcneill			uart_freq = octeon_ioclock_speed();
1411.3Sjmcneill		}
1421.3Sjmcneill
1431.3Sjmcneill		if (uart_freq > 0)
1441.3Sjmcneill			delay(640000000 / uart_freq);
1451.3Sjmcneill
1461.3Sjmcneill		cons->consinit(&faa, uart_freq);
1471.3Sjmcneill	}
1481.3Sjmcneill
1491.2Sjmcneill	faa.faa_phandle = OF_peer(0);
1501.8Sthorpej	config_found(self, &faa, NULL,
1511.9Sthorpej	    CFARGS(.iattr = "fdt"));
1521.2Sjmcneill}
1531.2Sjmcneill
1541.1Shikarustatic int
1551.1Shikarumainbus_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
1561.1Shikaru{
1571.1Shikaru	struct mainbus_attach_args *aa = aux;
1581.1Shikaru
1591.1Shikaru	if (strcmp(cf->cf_name, aa->aa_name) != 0)
1601.1Shikaru		return 0;
1611.1Shikaru
1621.1Shikaru	return config_match(parent, cf, aux);
1631.1Shikaru}
1641.1Shikaru
1651.1Shikarustatic int
1661.1Shikarumainbus_print(void *aux, const char *pnp)
1671.1Shikaru{
1681.1Shikaru	struct mainbus_attach_args *aa = aux;
1691.1Shikaru
1701.1Shikaru	if (pnp != 0)
1711.1Shikaru		return QUIET;
1721.1Shikaru
1731.1Shikaru	if (pnp)
1741.1Shikaru		aprint_normal("%s at %s", aa->aa_name, pnp);
1751.1Shikaru
1761.1Shikaru	return UNCONF;
1771.1Shikaru}
1781.2Sjmcneill
1791.2Sjmcneill/* ---- bus_space(9) */
1801.2Sjmcneill#define	CHIP			simplebus
1811.2Sjmcneill#define	CHIP_IO
1821.2Sjmcneill#define	CHIP_ACCESS_SIZE	8
1831.2Sjmcneill
1841.2Sjmcneill#define	CHIP_W1_BUS_START(v)	0x0000000000000000ULL
1851.2Sjmcneill#define	CHIP_W1_BUS_END(v)	0x7fffffffffffffffULL
1861.2Sjmcneill#define	CHIP_W1_SYS_START(v)	0x8000000000000000ULL
1871.2Sjmcneill#define	CHIP_W1_SYS_END(v)	0xffffffffffffffffULL
1881.2Sjmcneill
1891.2Sjmcneill#include <mips/mips/bus_space_alignstride_chipdep.c>
1901.10Sjmcneill
1911.10Sjmcneillbus_space_tag_t
1921.10Sjmcneillfdtbus_bus_tag_create(int phandle, uint32_t flags)
1931.10Sjmcneill{
1941.10Sjmcneill	return &simplebus_bus_tag;
1951.10Sjmcneill}
196