ph1_machdep.c revision 1.2 1 /* $NetBSD: ph1_machdep.c,v 1.2 2026/08/02 16:45:28 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2026 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
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 <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: ph1_machdep.c,v 1.2 2026/08/02 16:45:28 thorpej Exp $");
34
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/device.h>
38
39 #include <hb68k/pg68k/pgromcalls.h>
40
41 #include <dev/ata/atavar.h>
42
43 #include <dev/fdt/fdtvar.h>
44 #include <dev/fdt/fdt_platform.h>
45
46 #include <dev/ofw/openfirm.h>
47
48 /*
49 * plat_device_register --
50 * device_register() hook -- boot device detection and an
51 * opportunity to set any device properties as devices are
52 * attached during autoconfiguration.
53 */
54 static void
55 ph1_plat_device_register(device_t dev, void *aux)
56 {
57 extern device_t machine_booted_device; /* XXX extern */
58 device_t parent = device_parent(dev);
59
60 static device_t booted_controller;
61 static paddr_t booted_controller_phys = (paddr_t)-1;
62 static int booted_unit = -1;
63 static bool initialized;
64
65 /*
66 * The Phaethon 1 firmware provides several properties
67 * in /chosen to help us find the boot device:
68 *
69 * pg68k,booted-controller-type -- e.g. "ata"
70 * pg68k,booted-controller-phys -- phys addr of device
71 * pg68k,booted-unit -- unit (i.e. drive) #
72 *
73 * We'll be concerned with pg68k,booted-controller-phys and
74 * pg68k,booted-unit.
75 */
76 if (! initialized) {
77 int chosen = OF_finddevice("/chosen");
78 uint32_t val;
79
80 if (chosen != -1) {
81 if (of_getprop_uint32(chosen,
82 "pg68k,booted-controller-phys",
83 &val) == 0) {
84 booted_controller_phys = val;
85 }
86 if (of_getprop_uint32(chosen,
87 "pg68k,booted-unit",
88 &val) == 0) {
89 booted_unit = val;
90 }
91 }
92 initialized = true;
93 }
94
95 if (booted_controller == NULL) {
96 if (device_is_a(parent, "simplebus")) {
97 struct fdt_attach_args *faa = aux;
98 bus_addr_t addr;
99 bus_size_t size;
100
101 if (fdtbus_get_reg(faa->faa_phandle,
102 0, &addr, &size) != 0) {
103 return;
104 }
105
106 if (addr == booted_controller_phys) {
107 booted_controller = dev;
108 }
109 }
110 return;
111 }
112
113 if (machine_booted_device == NULL) {
114 if (device_is_a(dev, "wd") &&
115 device_is_a(parent, "atabus") &&
116 device_parent(parent) == booted_controller) {
117 struct ata_device *adev = aux;
118
119 if (adev->adev_drv_data->drive == booted_unit) {
120 machine_booted_device = dev;
121 }
122 }
123 }
124 }
125
126 /*
127 * plat_halt --
128 * Halt the machine, returning back to the firmware monitor,
129 * if possible.
130 */
131 static void
132 ph1_plat_halt(void)
133 {
134 printf("Transferring control back to firmware...\n");
135 pgromcall_halt();
136 }
137
138 /*
139 * plat_reboot --
140 * Reboot the system according to "howto". Attempt to
141 * use "bootstr" as the arguments for the resulting system
142 * boot.
143 */
144 static void
145 ph1_plat_reboot(int howto, char *bootstr)
146 {
147 printf("Requesting reboot from firmware...\n");
148 pgromcall_reboot();
149 }
150
151 /*
152 * plat_uart_freq --
153 * Return the console UART clock frequency in Hz. This is
154 * called if the information is not available in the device
155 * tree.
156 */
157 static u_int
158 ph1_plat_uart_freq(void)
159 {
160 return 1843200;
161 }
162
163 static const struct fdt_platform phaethon1_platform = {
164 .fp_device_register = ph1_plat_device_register,
165 .fp_halt = ph1_plat_halt,
166 .fp_reboot = ph1_plat_reboot,
167 .fp_uart_freq = ph1_plat_uart_freq,
168 };
169 FDT_PLATFORM(phaethon1, "pg68k,cpu010-mk1", &phaethon1_platform);
170