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