Home | History | Annotate | Line # | Download | only in fdt
fdt_console.c revision 1.1
      1  1.1  thorpej /* $NetBSD: fdt_console.c,v 1.1 2025/09/06 22:53:48 thorpej Exp $ */
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.1  thorpej  * modification, are permitted provided that the following conditions
      9  1.1  thorpej  * are met:
     10  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     12  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     15  1.1  thorpej  *
     16  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  thorpej  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  thorpej  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  thorpej  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  thorpej  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  thorpej  * SUCH DAMAGE.
     27  1.1  thorpej  */
     28  1.1  thorpej 
     29  1.1  thorpej #include <sys/cdefs.h>
     30  1.1  thorpej __KERNEL_RCSID(0, "$NetBSD: fdt_console.c,v 1.1 2025/09/06 22:53:48 thorpej Exp $");
     31  1.1  thorpej 
     32  1.1  thorpej #include "opt_fdt.h"
     33  1.1  thorpej 
     34  1.1  thorpej #include <sys/param.h>
     35  1.1  thorpej #include <sys/bus.h>
     36  1.1  thorpej 
     37  1.1  thorpej #include <libfdt.h>
     38  1.1  thorpej #include <dev/fdt/fdtvar.h>
     39  1.1  thorpej #include <dev/fdt/fdt_console.h>
     40  1.1  thorpej 
     41  1.1  thorpej #ifndef FDT_DEFAULT_STDOUT_PATH
     42  1.1  thorpej #define	FDT_DEFAULT_STDOUT_PATH		"serial0:115200n8"
     43  1.1  thorpej #endif
     44  1.1  thorpej 
     45  1.1  thorpej const struct fdt_console *
     46  1.1  thorpej fdtbus_get_console(void)
     47  1.1  thorpej {
     48  1.1  thorpej 	static const struct fdt_console_info *booted_console = NULL;
     49  1.1  thorpej 
     50  1.1  thorpej 	if (booted_console == NULL) {
     51  1.1  thorpej 		__link_set_decl(fdt_consoles, struct fdt_console_info);
     52  1.1  thorpej 		struct fdt_console_info * const *info;
     53  1.1  thorpej 		const struct fdt_console_info *best_info = NULL;
     54  1.1  thorpej 		const int phandle = fdtbus_get_stdout_phandle();
     55  1.1  thorpej 		int best_match = 0;
     56  1.1  thorpej 
     57  1.1  thorpej 		if (phandle == -1) {
     58  1.1  thorpej 			printf("WARNING: no console device\n");
     59  1.1  thorpej 			return NULL;
     60  1.1  thorpej 		}
     61  1.1  thorpej 
     62  1.1  thorpej 		__link_set_foreach(info, fdt_consoles) {
     63  1.1  thorpej 			const int match = (*info)->ops->match(phandle);
     64  1.1  thorpej 			if (match > best_match) {
     65  1.1  thorpej 				best_match = match;
     66  1.1  thorpej 				best_info = *info;
     67  1.1  thorpej 			}
     68  1.1  thorpej 		}
     69  1.1  thorpej 
     70  1.1  thorpej 		booted_console = best_info;
     71  1.1  thorpej 	}
     72  1.1  thorpej 
     73  1.1  thorpej 	return booted_console == NULL ? NULL : booted_console->ops;
     74  1.1  thorpej }
     75  1.1  thorpej 
     76  1.1  thorpej const char *
     77  1.1  thorpej fdtbus_get_stdout_path(void)
     78  1.1  thorpej {
     79  1.1  thorpej 	const char *prop;
     80  1.1  thorpej 
     81  1.1  thorpej 	const int off = fdt_path_offset(fdtbus_get_data(), "/chosen");
     82  1.1  thorpej 	if (off >= 0) {
     83  1.1  thorpej 		prop = fdt_getprop(fdtbus_get_data(), off, "stdout-path", NULL);
     84  1.1  thorpej 		if (prop != NULL)
     85  1.1  thorpej 			return prop;
     86  1.1  thorpej 	}
     87  1.1  thorpej 
     88  1.1  thorpej 	/* If the stdout-path property is not found, return the default */
     89  1.1  thorpej 	return FDT_DEFAULT_STDOUT_PATH;
     90  1.1  thorpej }
     91  1.1  thorpej 
     92  1.1  thorpej int
     93  1.1  thorpej fdtbus_get_stdout_phandle(void)
     94  1.1  thorpej {
     95  1.1  thorpej 	const char *prop, *p;
     96  1.1  thorpej 	int off, len;
     97  1.1  thorpej 
     98  1.1  thorpej 	prop = fdtbus_get_stdout_path();
     99  1.1  thorpej 	if (prop == NULL)
    100  1.1  thorpej 		return -1;
    101  1.1  thorpej 
    102  1.1  thorpej 	p = strchr(prop, ':');
    103  1.1  thorpej 	len = p == NULL ? strlen(prop) : (p - prop);
    104  1.1  thorpej 	if (*prop != '/') {
    105  1.1  thorpej 		/* Alias */
    106  1.1  thorpej 		prop = fdt_get_alias_namelen(fdtbus_get_data(), prop, len);
    107  1.1  thorpej 		if (prop == NULL)
    108  1.1  thorpej 			return -1;
    109  1.1  thorpej 		len = strlen(prop);
    110  1.1  thorpej 	}
    111  1.1  thorpej 	off = fdt_path_offset_namelen(fdtbus_get_data(), prop, len);
    112  1.1  thorpej 	if (off < 0)
    113  1.1  thorpej 		return -1;
    114  1.1  thorpej 
    115  1.1  thorpej 	return fdtbus_offset2phandle(off);
    116  1.1  thorpej }
    117  1.1  thorpej 
    118  1.1  thorpej int
    119  1.1  thorpej fdtbus_get_stdout_speed(void)
    120  1.1  thorpej {
    121  1.1  thorpej 	const char *prop, *p;
    122  1.1  thorpej 
    123  1.1  thorpej 	prop = fdtbus_get_stdout_path();
    124  1.1  thorpej 	if (prop == NULL)
    125  1.1  thorpej 		return -1;
    126  1.1  thorpej 
    127  1.1  thorpej 	p = strchr(prop, ':');
    128  1.1  thorpej 	if (p == NULL)
    129  1.1  thorpej 		return -1;
    130  1.1  thorpej 
    131  1.1  thorpej 	return (int)strtoul(p + 1, NULL, 10);
    132  1.1  thorpej }
    133  1.1  thorpej 
    134  1.1  thorpej tcflag_t
    135  1.1  thorpej fdtbus_get_stdout_flags(void)
    136  1.1  thorpej {
    137  1.1  thorpej 	const char *prop, *p;
    138  1.1  thorpej 	tcflag_t flags = TTYDEF_CFLAG;
    139  1.1  thorpej 	char *ep;
    140  1.1  thorpej 
    141  1.1  thorpej 	prop = fdtbus_get_stdout_path();
    142  1.1  thorpej 	if (prop == NULL)
    143  1.1  thorpej 		return flags;
    144  1.1  thorpej 
    145  1.1  thorpej 	p = strchr(prop, ':');
    146  1.1  thorpej 	if (p == NULL)
    147  1.1  thorpej 		return flags;
    148  1.1  thorpej 
    149  1.1  thorpej 	ep = NULL;
    150  1.1  thorpej 	(void)strtoul(p + 1, &ep, 10);
    151  1.1  thorpej 	if (ep == NULL)
    152  1.1  thorpej 		return flags;
    153  1.1  thorpej 
    154  1.1  thorpej 	/* <baud>{<parity>{<bits>{<flow>}}} */
    155  1.1  thorpej 	while (*ep) {
    156  1.1  thorpej 		switch (*ep) {
    157  1.1  thorpej 		/* parity */
    158  1.1  thorpej 		case 'n':	flags &= ~(PARENB|PARODD); break;
    159  1.1  thorpej 		case 'e':	flags &= ~PARODD; flags |= PARENB; break;
    160  1.1  thorpej 		case 'o':	flags |= (PARENB|PARODD); break;
    161  1.1  thorpej 		/* bits */
    162  1.1  thorpej 		case '5':	flags &= ~CSIZE; flags |= CS5; break;
    163  1.1  thorpej 		case '6':	flags &= ~CSIZE; flags |= CS6; break;
    164  1.1  thorpej 		case '7':	flags &= ~CSIZE; flags |= CS7; break;
    165  1.1  thorpej 		case '8':	flags &= ~CSIZE; flags |= CS8; break;
    166  1.1  thorpej 		/* flow */
    167  1.1  thorpej 		case 'r':	flags |= CRTSCTS; break;
    168  1.1  thorpej 		}
    169  1.1  thorpej 		ep++;
    170  1.1  thorpej 	}
    171  1.1  thorpej 
    172  1.1  thorpej 	return flags;
    173  1.1  thorpej }
    174