riscv_platform.c revision 1.4
11.4Sthorpej/* $NetBSD: riscv_platform.c,v 1.4 2025/09/06 22:53:48 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.4Sthorpej__KERNEL_RCSID(0, "$NetBSD: riscv_platform.c,v 1.4 2025/09/06 22:53:48 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.4Sthorpej#include <dev/fdt/fdt_console.h>
471.3Sthorpej#include <dev/fdt/fdt_platform.h>
481.1Sskrll
491.1Sskrll#include <uvm/uvm_extern.h>
501.1Sskrll#include <uvm/pmap/pmap_devmap.h>
511.1Sskrll
521.2Sskrll#include <riscv/fdt/riscv_fdtvar.h>
531.1Sskrll
541.1Sskrllstatic const struct pmap_devmap *
551.1Sskrllriscv_platform_devmap(void)
561.1Sskrll{
571.1Sskrll	static const struct pmap_devmap devmap_empty[] = {
581.1Sskrll		DEVMAP_ENTRY_END
591.1Sskrll	};
601.1Sskrll
611.1Sskrll	static struct pmap_devmap devmap_uart[] = {
621.1Sskrll		DEVMAP_ENTRY(VM_KERNEL_IO_BASE, 0, PAGE_SIZE),
631.1Sskrll		DEVMAP_ENTRY_END
641.1Sskrll	};
651.1Sskrll	bus_addr_t uart_base;
661.1Sskrll
671.1Sskrll	const int phandle = fdtbus_get_stdout_phandle();
681.1Sskrll	if (phandle <= 0)
691.1Sskrll		return devmap_empty;
701.1Sskrll
711.1Sskrll	if (fdtbus_get_reg(phandle, 0, &uart_base, NULL) != 0)
721.1Sskrll		return devmap_empty;
731.1Sskrll
741.1Sskrll	devmap_uart[0].pd_pa = DEVMAP_ALIGN(uart_base);
751.1Sskrll
761.1Sskrll	return devmap_uart;
771.1Sskrll}
781.1Sskrll
791.1Sskrll
801.1Sskrllstatic u_int
811.1Sskrllriscv_platform_uart_freq(void)
821.1Sskrll{
831.1Sskrll	return 0;
841.1Sskrll}
851.1Sskrll
861.1Sskrllstatic const struct fdt_platform riscv_platform = {
871.1Sskrll	.fp_devmap = riscv_platform_devmap,
881.2Sskrll	.fp_bootstrap = riscv_fdt_cpu_bootstrap,
891.1Sskrll	.fp_uart_freq = riscv_platform_uart_freq,
901.2Sskrll	.fp_mpstart = riscv_fdt_cpu_mpstart,
911.1Sskrll};
921.1Sskrll
931.1SskrllFDT_PLATFORM(rv, FDT_PLATFORM_DEFAULT, &riscv_platform);
94