1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/module.h> 35 #include <sys/bus.h> 36 #include <sys/conf.h> 37 #include <sys/clock.h> 38 #include <sys/cpu.h> 39 #include <sys/eventhandler.h> 40 #include <sys/kernel.h> 41 #include <sys/reboot.h> 42 #include <sys/sysctl.h> 43 44 #include <dev/ofw/ofw_bus.h> 45 #include <dev/ofw/openfirm.h> 46 47 #include <machine/rtas.h> 48 49 #include "clock_if.h" 50 51 static int rtasdev_probe(device_t); 52 static int rtasdev_attach(device_t); 53 /* clock interface */ 54 static int rtas_gettime(device_t dev, struct timespec *ts); 55 static int rtas_settime(device_t dev, struct timespec *ts); 56 57 static void rtas_shutdown(void *arg, int howto); 58 59 static device_method_t rtasdev_methods[] = { 60 /* Device interface */ 61 DEVMETHOD(device_probe, rtasdev_probe), 62 DEVMETHOD(device_attach, rtasdev_attach), 63 64 /* clock interface */ 65 DEVMETHOD(clock_gettime, rtas_gettime), 66 DEVMETHOD(clock_settime, rtas_settime), 67 68 { 0, 0 }, 69 }; 70 71 static driver_t rtasdev_driver = { 72 "rtas", 73 rtasdev_methods, 74 0 75 }; 76 77 static devclass_t rtasdev_devclass; 78 79 DRIVER_MODULE(rtasdev, ofwbus, rtasdev_driver, rtasdev_devclass, 0, 0); 80 81 static int 82 rtasdev_probe(device_t dev) 83 { 84 const char *name = ofw_bus_get_name(dev); 85 86 if (strcmp(name, "rtas") != 0) 87 return (ENXIO); 88 if (!rtas_exists()) 89 return (ENXIO); 90 91 device_set_desc(dev, "Run-Time Abstraction Services"); 92 return (0); 93 } 94 95 static int 96 rtasdev_attach(device_t dev) 97 { 98 if (rtas_token_lookup("get-time-of-day") != -1) 99 clock_register(dev, 2000); 100 101 EVENTHANDLER_REGISTER(shutdown_final, rtas_shutdown, NULL, 102 SHUTDOWN_PRI_LAST); 103 104 return (0); 105 } 106 107 static int 108 rtas_gettime(device_t dev, struct timespec *ts) { 109 struct clocktime ct; 110 cell_t tod[8]; 111 cell_t token; 112 int error; 113 114 token = rtas_token_lookup("get-time-of-day"); 115 if (token == -1) 116 return (ENXIO); 117 error = rtas_call_method(token, 0, 8, &tod[0], &tod[1], &tod[2], 118 &tod[3], &tod[4], &tod[5], &tod[6], &tod[7]); 119 if (error < 0) 120 return (ENXIO); 121 if (tod[0] != 0) 122 return ((tod[0] == -1) ? ENXIO : EAGAIN); 123 124 ct.year = tod[1]; 125 ct.mon = tod[2]; 126 ct.day = tod[3]; 127 ct.hour = tod[4]; 128 ct.min = tod[5]; 129 ct.sec = tod[6]; 130 ct.nsec = tod[7]; 131 132 return (clock_ct_to_ts(&ct, ts)); 133 } 134 135 static int 136 rtas_settime(device_t dev, struct timespec *ts) 137 { 138 struct clocktime ct; 139 cell_t token, status; 140 int error; 141 142 token = rtas_token_lookup("set-time-of-day"); 143 if (token == -1) 144 return (ENXIO); 145 146 clock_ts_to_ct(ts, &ct); 147 error = rtas_call_method(token, 7, 1, ct.year, ct.mon, ct.day, ct.hour, 148 ct.min, ct.sec, ct.nsec, &status); 149 if (error < 0) 150 return (ENXIO); 151 if (status != 0) 152 return (((int)status < 0) ? ENXIO : EAGAIN); 153 154 return (0); 155 } 156 157 static void 158 rtas_shutdown(void *arg, int howto) 159 { 160 cell_t token, status; 161 162 if (howto & RB_HALT) { 163 token = rtas_token_lookup("power-off"); 164 if (token == -1) 165 return; 166 167 rtas_call_method(token, 2, 1, 0, 0, &status); 168 } else { 169 token = rtas_token_lookup("system-reboot"); 170 if (token == -1) 171 return; 172 173 rtas_call_method(token, 0, 1, &status); 174 } 175 } 176