1*8d564c5dSskrll /* $NetBSD: fdt_platform.c,v 1.1 2023/04/07 08:55:31 skrll Exp $ */
2*8d564c5dSskrll
3*8d564c5dSskrll /*-
4*8d564c5dSskrll * Copyright (c) 2017 Jared D. McNeill <jmcneill@invisible.ca>
5*8d564c5dSskrll * All rights reserved.
6*8d564c5dSskrll *
7*8d564c5dSskrll * Redistribution and use in source and binary forms, with or without
8*8d564c5dSskrll * modification, are permitted provided that the following conditions
9*8d564c5dSskrll * are met:
10*8d564c5dSskrll * 1. Redistributions of source code must retain the above copyright
11*8d564c5dSskrll * notice, this list of conditions and the following disclaimer.
12*8d564c5dSskrll * 2. Redistributions in binary form must reproduce the above copyright
13*8d564c5dSskrll * notice, this list of conditions and the following disclaimer in the
14*8d564c5dSskrll * documentation and/or other materials provided with the distribution.
15*8d564c5dSskrll *
16*8d564c5dSskrll * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17*8d564c5dSskrll * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*8d564c5dSskrll * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*8d564c5dSskrll * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20*8d564c5dSskrll * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*8d564c5dSskrll * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22*8d564c5dSskrll * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*8d564c5dSskrll * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*8d564c5dSskrll * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8d564c5dSskrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8d564c5dSskrll * SUCH DAMAGE.
27*8d564c5dSskrll */
28*8d564c5dSskrll
29*8d564c5dSskrll #include <sys/cdefs.h>
30*8d564c5dSskrll __KERNEL_RCSID(0, "$NetBSD: fdt_platform.c,v 1.1 2023/04/07 08:55:31 skrll Exp $");
31*8d564c5dSskrll
32*8d564c5dSskrll #include <sys/param.h>
33*8d564c5dSskrll
34*8d564c5dSskrll #include <dev/ofw/openfirm.h>
35*8d564c5dSskrll
36*8d564c5dSskrll #include <dev/fdt/fdtvar.h>
37*8d564c5dSskrll
38*8d564c5dSskrll const struct fdt_platform *
fdt_platform_find(void)39*8d564c5dSskrll fdt_platform_find(void)
40*8d564c5dSskrll {
41*8d564c5dSskrll static const struct fdt_platform_info *booted_platform = NULL;
42*8d564c5dSskrll __link_set_decl(fdt_platforms, struct fdt_platform_info);
43*8d564c5dSskrll struct fdt_platform_info * const *info;
44*8d564c5dSskrll
45*8d564c5dSskrll if (booted_platform == NULL) {
46*8d564c5dSskrll const struct fdt_platform_info *best_info = NULL;
47*8d564c5dSskrll const int phandle = OF_peer(0);
48*8d564c5dSskrll int match, best_match = 0;
49*8d564c5dSskrll
50*8d564c5dSskrll __link_set_foreach(info, fdt_platforms) {
51*8d564c5dSskrll const struct device_compatible_entry compat_data[] = {
52*8d564c5dSskrll { .compat = (*info)->fpi_compat },
53*8d564c5dSskrll DEVICE_COMPAT_EOL
54*8d564c5dSskrll };
55*8d564c5dSskrll
56*8d564c5dSskrll match = of_compatible_match(phandle, compat_data);
57*8d564c5dSskrll if (match > best_match) {
58*8d564c5dSskrll best_match = match;
59*8d564c5dSskrll best_info = *info;
60*8d564c5dSskrll }
61*8d564c5dSskrll }
62*8d564c5dSskrll
63*8d564c5dSskrll booted_platform = best_info;
64*8d564c5dSskrll }
65*8d564c5dSskrll
66*8d564c5dSskrll /*
67*8d564c5dSskrll * No SoC specific platform was found. Try to find a generic
68*8d564c5dSskrll * platform definition and use that if available.
69*8d564c5dSskrll */
70*8d564c5dSskrll if (booted_platform == NULL) {
71*8d564c5dSskrll __link_set_foreach(info, fdt_platforms) {
72*8d564c5dSskrll if (strcmp((*info)->fpi_compat, FDT_PLATFORM_DEFAULT) == 0) {
73*8d564c5dSskrll booted_platform = *info;
74*8d564c5dSskrll break;
75*8d564c5dSskrll }
76*8d564c5dSskrll }
77*8d564c5dSskrll }
78*8d564c5dSskrll
79*8d564c5dSskrll return booted_platform == NULL ? NULL : booted_platform->fpi_ops;
80*8d564c5dSskrll }
81*8d564c5dSskrll
82*8d564c5dSskrll // XXXNH remove this and rely on a default
83*8d564c5dSskrll FDT_PLATFORM(dummy, NULL, NULL);
84