Home | History | Annotate | Line # | Download | only in riscv
      1 /*	$NetBSD: mainbus.c,v 1.7 2024/08/04 08:16:25 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2022 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Nick Hudson
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include "opt_console.h"
     33 #include "opt_efi.h"
     34 #include "opt_modular.h"
     35 
     36 #include <sys/cdefs.h>
     37 
     38 __RCSID("$NetBSD: mainbus.c,v 1.7 2024/08/04 08:16:25 skrll Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/types.h>
     42 
     43 #include <sys/bus.h>
     44 #include <sys/device.h>
     45 #include <sys/systm.h>
     46 
     47 #include <uvm/uvm_extern.h>
     48 
     49 #include <dev/fdt/fdtvar.h>
     50 
     51 #ifdef CONSADDR
     52 #include <dev/ic/comreg.h>
     53 #include <dev/ic/comvar.h>
     54 #endif
     55 
     56 #include <machine/sysreg.h>
     57 
     58 extern struct bus_space riscv_generic_bs_tag;
     59 
     60 bus_space_tag_t
     61 fdtbus_bus_tag_create(int phandle, uint32_t flags)
     62 {
     63 	return &riscv_generic_bs_tag;
     64 }
     65 
     66 static inline bool
     67 cpu_earlydevice_va_p(void)
     68 {
     69 
     70 	return __SHIFTOUT(csr_satp_read(), SATP_MODE);
     71 }
     72 
     73 void com_platform_early_putchar(char);
     74 
     75 void __noasan
     76 com_platform_early_putchar(char c)
     77 {
     78 #ifdef CONSADDR
     79 #define CONSADDR_VA	(VM_KERNEL_IO_BASE + (CONSADDR & SEGOFSET))
     80 
     81 	volatile uint8_t *uartaddr = cpu_earlydevice_va_p() ?
     82 	    (volatile uint8_t *)CONSADDR_VA :
     83 	    (volatile uint8_t *)CONSADDR;
     84 
     85 	while ((uartaddr[com_lsr] & LSR_TXRDY) == 0)
     86 		;
     87 
     88 	uartaddr[com_data] = c;
     89 #endif
     90 }
     91 
     92 static int
     93 mainbus_match(device_t parent, cfdata_t cf, void *aux)
     94 {
     95 	static int once = 0;
     96 
     97 	if (once != 0)
     98 		return 0;
     99 	once = 1;
    100 
    101 	return 1;
    102 }
    103 
    104 
    105 static void
    106 mainbus_attach_devicetree(device_t self)
    107 {
    108 	struct fdt_attach_args faa = {
    109 		.faa_name = "",
    110 		.faa_phandle = OF_peer(0),
    111 		.faa_bst = &riscv_generic_bs_tag,
    112 	};
    113 
    114 	aprint_normal("\n");
    115 
    116 	config_found(self, &faa, NULL, CFARGS(.iattr = "fdt"));
    117 }
    118 
    119 static void
    120 mainbus_attach(device_t parent, device_t self, void *aux)
    121 {
    122 	if (fdtbus_get_data() != NULL) {
    123 		mainbus_attach_devicetree(self);
    124 	}
    125 }
    126 
    127 CFATTACH_DECL_NEW(mainbus, 0, mainbus_match, mainbus_attach, NULL, NULL);
    128