1250840Smarcel/*-
2250840Smarcel * Copyright (c) 2013 Juniper Networks, Inc.
3250840Smarcel * All rights reserved.
4250840Smarcel *
5250840Smarcel * Redistribution and use in source and binary forms, with or without
6250840Smarcel * modification, are permitted provided that the following conditions
7250840Smarcel * are met:
8250840Smarcel * 1. Redistributions of source code must retain the above copyright
9250840Smarcel *    notice, this list of conditions and the following disclaimer.
10250840Smarcel * 2. Redistributions in binary form must reproduce the above copyright
11250840Smarcel *    notice, this list of conditions and the following disclaimer in the
12250840Smarcel *    documentation and/or other materials provided with the distribution.
13250840Smarcel *
14250840Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15250840Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16250840Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17250840Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18250840Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19250840Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20250840Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21250840Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22250840Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23250840Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24250840Smarcel * SUCH DAMAGE.
25250840Smarcel */
26250840Smarcel
27250840Smarcel#include <sys/cdefs.h>
28250840Smarcel__FBSDID("$FreeBSD$");
29250840Smarcel
30250840Smarcel#include "opt_platform.h"
31250840Smarcel
32250840Smarcel#include <sys/param.h>
33250840Smarcel#include <sys/kernel.h>
34250840Smarcel#include <sys/bus.h>
35250840Smarcel#include <sys/linker.h>
36250840Smarcel
37250840Smarcel#include <machine/metadata.h>
38250840Smarcel#include <x86/fdt.h>
39250840Smarcel
40250840Smarcel#include <dev/fdt/fdt_common.h>
41250840Smarcel#include <dev/ofw/openfirm.h>
42250840Smarcel
43250840Smarcelint
44250840Smarcelx86_init_fdt(void)
45250840Smarcel{
46250840Smarcel	void *dtbp, *mdp;
47250840Smarcel	int error;
48250840Smarcel
49250840Smarcel	if (OF_install(OFW_FDT, 0) == FALSE) {
50250840Smarcel		error = ENXIO;
51250840Smarcel		goto out;
52250840Smarcel	}
53250840Smarcel
54250840Smarcel	mdp = preload_search_by_type("elf kernel");
55250840Smarcel	if (mdp == NULL)
56250840Smarcel		mdp = preload_search_by_type("elf32 kernel");
57287000Sroyger	dtbp = MD_FETCH(mdp, MODINFOMD_DTBP, void *);
58250840Smarcel
59250840Smarcel#if defined(FDT_DTB_STATIC)
60250840Smarcel	/*
61250840Smarcel	 * In case the device tree blob was not retrieved (from metadata) try
62250840Smarcel	 * to use the statically embedded one.
63250840Smarcel	 */
64250840Smarcel	if (dtbp == NULL)
65250840Smarcel		dtbp = &fdt_static_dtb;
66250840Smarcel#endif
67250840Smarcel
68250840Smarcel	if (dtbp == NULL) {
69250840Smarcel		error = ENOENT;
70250840Smarcel		goto out;
71250840Smarcel	}
72250840Smarcel
73250840Smarcel	error = OF_init(dtbp) ? ENXIO : 0;
74250840Smarcel
75250840Smarcel out:
76250840Smarcel	return (error);
77250840Smarcel}
78