1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2011 Nathan Whitehorn
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/module.h>
33 #include <sys/bus.h>
34 #include <sys/conf.h>
35 #include <sys/clock.h>
36 #include <sys/cpu.h>
37 #include <sys/eventhandler.h>
38 #include <sys/kernel.h>
39 #include <sys/reboot.h>
40 #include <sys/sysctl.h>
41
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/openfirm.h>
44
45 #include <machine/rtas.h>
46
47 #include "clock_if.h"
48
49 static int rtasdev_probe(device_t);
50 static int rtasdev_attach(device_t);
51 /* clock interface */
52 static int rtas_gettime(device_t dev, struct timespec *ts);
53 static int rtas_settime(device_t dev, struct timespec *ts);
54
55 static void rtas_shutdown(void *arg, int howto);
56
57 static device_method_t rtasdev_methods[] = {
58 /* Device interface */
59 DEVMETHOD(device_probe, rtasdev_probe),
60 DEVMETHOD(device_attach, rtasdev_attach),
61
62 /* clock interface */
63 DEVMETHOD(clock_gettime, rtas_gettime),
64 DEVMETHOD(clock_settime, rtas_settime),
65
66 { 0, 0 },
67 };
68
69 static driver_t rtasdev_driver = {
70 "rtas",
71 rtasdev_methods,
72 0
73 };
74
75 DRIVER_MODULE(rtasdev, ofwbus, rtasdev_driver, 0, 0);
76
77 static int
rtasdev_probe(device_t dev)78 rtasdev_probe(device_t dev)
79 {
80 const char *name = ofw_bus_get_name(dev);
81
82 if (strcmp(name, "rtas") != 0)
83 return (ENXIO);
84 if (!rtas_exists())
85 return (ENXIO);
86
87 device_set_desc(dev, "Run-Time Abstraction Services");
88 return (0);
89 }
90
91 static int
rtasdev_attach(device_t dev)92 rtasdev_attach(device_t dev)
93 {
94 if (rtas_token_lookup("get-time-of-day") != -1)
95 clock_register(dev, 2000);
96
97 EVENTHANDLER_REGISTER(shutdown_final, rtas_shutdown, NULL,
98 SHUTDOWN_PRI_LAST);
99
100 return (0);
101 }
102
103 static int
rtas_gettime(device_t dev,struct timespec * ts)104 rtas_gettime(device_t dev, struct timespec *ts) {
105 struct clocktime ct;
106 cell_t tod[8];
107 cell_t token;
108 int error;
109
110 token = rtas_token_lookup("get-time-of-day");
111 if (token == -1)
112 return (ENXIO);
113 error = rtas_call_method(token, 0, 8, &tod[0], &tod[1], &tod[2],
114 &tod[3], &tod[4], &tod[5], &tod[6], &tod[7]);
115 if (error < 0)
116 return (ENXIO);
117 if (tod[0] != 0)
118 return ((tod[0] == -1) ? ENXIO : EAGAIN);
119
120 ct.year = tod[1];
121 ct.mon = tod[2];
122 ct.day = tod[3];
123 ct.hour = tod[4];
124 ct.min = tod[5];
125 ct.sec = tod[6];
126 ct.nsec = tod[7];
127
128 return (clock_ct_to_ts(&ct, ts));
129 }
130
131 static int
rtas_settime(device_t dev,struct timespec * ts)132 rtas_settime(device_t dev, struct timespec *ts)
133 {
134 struct clocktime ct;
135 cell_t token, status;
136 int error;
137
138 token = rtas_token_lookup("set-time-of-day");
139 if (token == -1)
140 return (ENXIO);
141
142 clock_ts_to_ct(ts, &ct);
143 error = rtas_call_method(token, 7, 1, ct.year, ct.mon, ct.day, ct.hour,
144 ct.min, ct.sec, ct.nsec, &status);
145 if (error < 0)
146 return (ENXIO);
147 if (status != 0)
148 return (((int)status < 0) ? ENXIO : EAGAIN);
149
150 return (0);
151 }
152
153 static void
rtas_shutdown(void * arg,int howto)154 rtas_shutdown(void *arg, int howto)
155 {
156 cell_t token, status;
157
158 if ((howto & RB_POWEROFF) != 0) {
159 token = rtas_token_lookup("power-off");
160 if (token == -1)
161 return;
162
163 rtas_call_method(token, 2, 1, 0, 0, &status);
164 } else if ((howto & RB_HALT) == 0) {
165 token = rtas_token_lookup("system-reboot");
166 if (token == -1)
167 return;
168
169 rtas_call_method(token, 0, 1, &status);
170 }
171 }
172