riscv_platform.c revision 1.3
11.3Sthorpej/* $NetBSD: riscv_platform.c,v 1.3 2025/09/06 21:02:41 thorpej Exp $ */
21.1Sskrll
31.1Sskrll/*-
41.1Sskrll * Copyright (c) 2023 The NetBSD Foundation, Inc.
51.1Sskrll * All rights reserved.
61.1Sskrll *
71.1Sskrll * This code is derived from software contributed to The NetBSD Foundation
81.1Sskrll * by Nick Hudson
91.1Sskrll *
101.1Sskrll * Redistribution and use in source and binary forms, with or without
111.1Sskrll * modification, are permitted provided that the following conditions
121.1Sskrll * are met:
131.1Sskrll * 1. Redistributions of source code must retain the above copyright
141.1Sskrll *    notice, this list of conditions and the following disclaimer.
151.1Sskrll * 2. Redistributions in binary form must reproduce the above copyright
161.1Sskrll *    notice, this list of conditions and the following disclaimer in the
171.1Sskrll *    documentation and/or other materials provided with the distribution.
181.1Sskrll *
191.1Sskrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Sskrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Sskrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Sskrll * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Sskrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Sskrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Sskrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Sskrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Sskrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Sskrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Sskrll * POSSIBILITY OF SUCH DAMAGE.
301.1Sskrll */
311.1Sskrll
321.1Sskrll/*
331.1Sskrll * This is the default RISC-V FDT platform implementation. It assumes the
341.1Sskrll * following:
351.1Sskrll *
361.1Sskrll *  - Console UART is pre-configured by firmware
371.1Sskrll */
381.1Sskrll
391.1Sskrll#include <sys/cdefs.h>
401.3Sthorpej__KERNEL_RCSID(0, "$NetBSD: riscv_platform.c,v 1.3 2025/09/06 21:02:41 thorpej Exp $");
411.1Sskrll
421.1Sskrll#include <sys/param.h>
431.1Sskrll#include <sys/bus.h>
441.1Sskrll
451.1Sskrll#include <dev/fdt/fdtvar.h>
461.3Sthorpej#include <dev/fdt/fdt_platform.h>
471.1Sskrll
481.1Sskrll#include <uvm/uvm_extern.h>
491.1Sskrll#include <uvm/pmap/pmap_devmap.h>
501.1Sskrll
511.2Sskrll#include <riscv/fdt/riscv_fdtvar.h>
521.1Sskrll
531.1Sskrllstatic const struct pmap_devmap *
541.1Sskrllriscv_platform_devmap(void)
551.1Sskrll{
561.1Sskrll	static const struct pmap_devmap devmap_empty[] = {
571.1Sskrll		DEVMAP_ENTRY_END
581.1Sskrll	};
591.1Sskrll
601.1Sskrll	static struct pmap_devmap devmap_uart[] = {
611.1Sskrll		DEVMAP_ENTRY(VM_KERNEL_IO_BASE, 0, PAGE_SIZE),
621.1Sskrll		DEVMAP_ENTRY_END
631.1Sskrll	};
641.1Sskrll	bus_addr_t uart_base;
651.1Sskrll
661.1Sskrll	const int phandle = fdtbus_get_stdout_phandle();
671.1Sskrll	if (phandle <= 0)
681.1Sskrll		return devmap_empty;
691.1Sskrll
701.1Sskrll	if (fdtbus_get_reg(phandle, 0, &uart_base, NULL) != 0)
711.1Sskrll		return devmap_empty;
721.1Sskrll
731.1Sskrll	devmap_uart[0].pd_pa = DEVMAP_ALIGN(uart_base);
741.1Sskrll
751.1Sskrll	return devmap_uart;
761.1Sskrll}
771.1Sskrll
781.1Sskrll
791.1Sskrllstatic u_int
801.1Sskrllriscv_platform_uart_freq(void)
811.1Sskrll{
821.1Sskrll	return 0;
831.1Sskrll}
841.1Sskrll
851.1Sskrllstatic const struct fdt_platform riscv_platform = {
861.1Sskrll	.fp_devmap = riscv_platform_devmap,
871.2Sskrll	.fp_bootstrap = riscv_fdt_cpu_bootstrap,
881.1Sskrll	.fp_uart_freq = riscv_platform_uart_freq,
891.2Sskrll	.fp_mpstart = riscv_fdt_cpu_mpstart,
901.1Sskrll};
911.1Sskrll
921.1SskrllFDT_PLATFORM(rv, FDT_PLATFORM_DEFAULT, &riscv_platform);
93