ph1_machdep.c revision 1.1 1 /* $NetBSD: ph1_machdep.c,v 1.1 2026/07/19 01:48:24 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.1 2026/07/19 01:48:24 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_machine_init --
50 * Perform any machine-specific initialization. "nextpa" is
51 * the next free physical memory page, from which any memory
52 * can be stolen on a whole-page basis as needed. Returns the
53 * next free physical memory page.
54 */
55 static paddr_t
56 ph1_plat_machine_init(paddr_t nextpa)
57 {
58 return nextpa;
59 }
60
61 /*
62 * plat_device_register --
63 * device_register() hook -- boot device detection and an
64 * opportunity to set any device properties as devices are
65 * attached during autoconfiguration.
66 */
67 static void
68 ph1_plat_device_register(device_t dev, void *aux)
69 {
70 extern device_t machine_booted_device; /* XXX extern */
71 device_t parent = device_parent(dev);
72
73 static device_t booted_controller;
74 static paddr_t booted_controller_phys = (paddr_t)-1;
75 static int booted_unit = -1;
76 static bool initialized;
77
78 /*
79 * The Phaethon 1 firmware provides several properties
80 * in /chosen to help us find the boot device:
81 *
82 * pg68k,booted-controller-type -- e.g. "ata"
83 * pg68k,booted-controller-phys -- phys addr of device
84 * pg68k,booted-unit -- unit (i.e. drive) #
85 *
86 * We'll be concerned with pg68k,booted-controller-phys and
87 * pg68k,booted-unit.
88 */
89 if (! initialized) {
90 int chosen = OF_finddevice("/chosen");
91 uint32_t val;
92
93 if (chosen != -1) {
94 if (of_getprop_uint32(chosen,
95 "pg68k,booted-controller-phys",
96 &val) == 0) {
97 booted_controller_phys = val;
98 }
99 if (of_getprop_uint32(chosen,
100 "pg68k,booted-unit",
101 &val) == 0) {
102 booted_unit = val;
103 }
104 }
105 initialized = true;
106 }
107
108 if (booted_controller == NULL) {
109 if (device_is_a(parent, "simplebus")) {
110 struct fdt_attach_args *faa = aux;
111 bus_addr_t addr;
112 bus_size_t size;
113
114 if (fdtbus_get_reg(faa->faa_phandle,
115 0, &addr, &size) != 0) {
116 return;
117 }
118
119 if (addr == booted_controller_phys) {
120 booted_controller = dev;
121 }
122 }
123 return;
124 }
125
126 if (machine_booted_device == NULL) {
127 if (device_is_a(dev, "wd") &&
128 device_is_a(parent, "atabus") &&
129 device_parent(parent) == booted_controller) {
130 struct ata_device *adev = aux;
131
132 if (adev->adev_drv_data->drive == booted_unit) {
133 machine_booted_device = dev;
134 }
135 }
136 }
137 }
138
139 /*
140 * plat_halt --
141 * Halt the machine, returning back to the firmware monitor,
142 * if possible.
143 */
144 static void
145 ph1_plat_halt(void)
146 {
147 printf("Transferring control back to firmware...\n");
148 pgromcall_halt();
149 }
150
151 /*
152 * plat_powerdown --
153 * Power-down the machine, if possible. If not, then halt.
154 */
155 static void
156 ph1_plat_powerdown(void)
157 {
158 /*
159 * The Phaethon 1 doesn't have software power control. It
160 * provides a poweroff vector, but it just halts.
161 */
162 ph1_plat_halt();
163 }
164
165 /*
166 * plat_reboot --
167 * Reboot the system according to "howto". Attempt to
168 * use "bootstr" as the arguments for the resulting system
169 * boot.
170 */
171 static void
172 ph1_plat_reboot(int howto, char *bootstr)
173 {
174 printf("Requesting reboot from firmware...\n");
175 pgromcall_reboot();
176 }
177
178 /*
179 * plat_uart_freq --
180 * Return the console UART clock frequency in Hz. This is
181 * called if the information is not available in the device
182 * tree.
183 */
184 static u_int
185 ph1_plat_uart_freq(void)
186 {
187 return 1843200;
188 }
189
190 static const struct fdt_platform phaethon1_platform = {
191 .fp_machine_init = ph1_plat_machine_init,
192 .fp_device_register = ph1_plat_device_register,
193 .fp_powerdown = ph1_plat_powerdown,
194 .fp_halt = ph1_plat_halt,
195 .fp_reboot = ph1_plat_reboot,
196 .fp_uart_freq = ph1_plat_uart_freq,
197 };
198 FDT_PLATFORM(phaethon1, "pg68k,cpu010-mk1", &phaethon1_platform);
199