ph1_machdep.c revision 1.2 1 1.2 thorpej /* $NetBSD: ph1_machdep.c,v 1.2 2026/08/02 16:45:28 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2026 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej #include <sys/cdefs.h>
33 1.2 thorpej __KERNEL_RCSID(0, "$NetBSD: ph1_machdep.c,v 1.2 2026/08/02 16:45:28 thorpej Exp $");
34 1.1 thorpej
35 1.1 thorpej #include <sys/param.h>
36 1.1 thorpej #include <sys/bus.h>
37 1.1 thorpej #include <sys/device.h>
38 1.1 thorpej
39 1.1 thorpej #include <hb68k/pg68k/pgromcalls.h>
40 1.1 thorpej
41 1.1 thorpej #include <dev/ata/atavar.h>
42 1.1 thorpej
43 1.1 thorpej #include <dev/fdt/fdtvar.h>
44 1.1 thorpej #include <dev/fdt/fdt_platform.h>
45 1.1 thorpej
46 1.1 thorpej #include <dev/ofw/openfirm.h>
47 1.1 thorpej
48 1.1 thorpej /*
49 1.1 thorpej * plat_device_register --
50 1.1 thorpej * device_register() hook -- boot device detection and an
51 1.1 thorpej * opportunity to set any device properties as devices are
52 1.1 thorpej * attached during autoconfiguration.
53 1.1 thorpej */
54 1.1 thorpej static void
55 1.1 thorpej ph1_plat_device_register(device_t dev, void *aux)
56 1.1 thorpej {
57 1.1 thorpej extern device_t machine_booted_device; /* XXX extern */
58 1.1 thorpej device_t parent = device_parent(dev);
59 1.1 thorpej
60 1.1 thorpej static device_t booted_controller;
61 1.1 thorpej static paddr_t booted_controller_phys = (paddr_t)-1;
62 1.1 thorpej static int booted_unit = -1;
63 1.1 thorpej static bool initialized;
64 1.1 thorpej
65 1.1 thorpej /*
66 1.1 thorpej * The Phaethon 1 firmware provides several properties
67 1.1 thorpej * in /chosen to help us find the boot device:
68 1.1 thorpej *
69 1.1 thorpej * pg68k,booted-controller-type -- e.g. "ata"
70 1.1 thorpej * pg68k,booted-controller-phys -- phys addr of device
71 1.1 thorpej * pg68k,booted-unit -- unit (i.e. drive) #
72 1.1 thorpej *
73 1.1 thorpej * We'll be concerned with pg68k,booted-controller-phys and
74 1.1 thorpej * pg68k,booted-unit.
75 1.1 thorpej */
76 1.1 thorpej if (! initialized) {
77 1.1 thorpej int chosen = OF_finddevice("/chosen");
78 1.1 thorpej uint32_t val;
79 1.1 thorpej
80 1.1 thorpej if (chosen != -1) {
81 1.1 thorpej if (of_getprop_uint32(chosen,
82 1.1 thorpej "pg68k,booted-controller-phys",
83 1.1 thorpej &val) == 0) {
84 1.1 thorpej booted_controller_phys = val;
85 1.1 thorpej }
86 1.1 thorpej if (of_getprop_uint32(chosen,
87 1.1 thorpej "pg68k,booted-unit",
88 1.1 thorpej &val) == 0) {
89 1.1 thorpej booted_unit = val;
90 1.1 thorpej }
91 1.1 thorpej }
92 1.1 thorpej initialized = true;
93 1.1 thorpej }
94 1.1 thorpej
95 1.1 thorpej if (booted_controller == NULL) {
96 1.1 thorpej if (device_is_a(parent, "simplebus")) {
97 1.1 thorpej struct fdt_attach_args *faa = aux;
98 1.1 thorpej bus_addr_t addr;
99 1.1 thorpej bus_size_t size;
100 1.1 thorpej
101 1.1 thorpej if (fdtbus_get_reg(faa->faa_phandle,
102 1.1 thorpej 0, &addr, &size) != 0) {
103 1.1 thorpej return;
104 1.1 thorpej }
105 1.1 thorpej
106 1.1 thorpej if (addr == booted_controller_phys) {
107 1.1 thorpej booted_controller = dev;
108 1.1 thorpej }
109 1.1 thorpej }
110 1.1 thorpej return;
111 1.1 thorpej }
112 1.1 thorpej
113 1.1 thorpej if (machine_booted_device == NULL) {
114 1.1 thorpej if (device_is_a(dev, "wd") &&
115 1.1 thorpej device_is_a(parent, "atabus") &&
116 1.1 thorpej device_parent(parent) == booted_controller) {
117 1.1 thorpej struct ata_device *adev = aux;
118 1.1 thorpej
119 1.1 thorpej if (adev->adev_drv_data->drive == booted_unit) {
120 1.1 thorpej machine_booted_device = dev;
121 1.1 thorpej }
122 1.1 thorpej }
123 1.1 thorpej }
124 1.1 thorpej }
125 1.1 thorpej
126 1.1 thorpej /*
127 1.1 thorpej * plat_halt --
128 1.1 thorpej * Halt the machine, returning back to the firmware monitor,
129 1.1 thorpej * if possible.
130 1.1 thorpej */
131 1.1 thorpej static void
132 1.1 thorpej ph1_plat_halt(void)
133 1.1 thorpej {
134 1.1 thorpej printf("Transferring control back to firmware...\n");
135 1.1 thorpej pgromcall_halt();
136 1.1 thorpej }
137 1.1 thorpej
138 1.1 thorpej /*
139 1.1 thorpej * plat_reboot --
140 1.1 thorpej * Reboot the system according to "howto". Attempt to
141 1.1 thorpej * use "bootstr" as the arguments for the resulting system
142 1.1 thorpej * boot.
143 1.1 thorpej */
144 1.1 thorpej static void
145 1.1 thorpej ph1_plat_reboot(int howto, char *bootstr)
146 1.1 thorpej {
147 1.1 thorpej printf("Requesting reboot from firmware...\n");
148 1.1 thorpej pgromcall_reboot();
149 1.1 thorpej }
150 1.1 thorpej
151 1.1 thorpej /*
152 1.1 thorpej * plat_uart_freq --
153 1.1 thorpej * Return the console UART clock frequency in Hz. This is
154 1.1 thorpej * called if the information is not available in the device
155 1.1 thorpej * tree.
156 1.1 thorpej */
157 1.1 thorpej static u_int
158 1.1 thorpej ph1_plat_uart_freq(void)
159 1.1 thorpej {
160 1.1 thorpej return 1843200;
161 1.1 thorpej }
162 1.1 thorpej
163 1.1 thorpej static const struct fdt_platform phaethon1_platform = {
164 1.1 thorpej .fp_device_register = ph1_plat_device_register,
165 1.1 thorpej .fp_halt = ph1_plat_halt,
166 1.1 thorpej .fp_reboot = ph1_plat_reboot,
167 1.1 thorpej .fp_uart_freq = ph1_plat_uart_freq,
168 1.1 thorpej };
169 1.1 thorpej FDT_PLATFORM(phaethon1, "pg68k,cpu010-mk1", &phaethon1_platform);
170