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