Index: projects/powernv/powerpc/powernv/opal.h =================================================================== --- projects/powernv/powerpc/powernv/opal.h (revision 290871) +++ projects/powernv/powerpc/powernv/opal.h (revision 290872) @@ -1,65 +1,68 @@ /*- * Copyright (c) 2015 Nathan Whitehorn * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _POWERNV_OPAL_H #define _POWERNV_OPAL_H #include #include /* Check if OPAL is correctly instantiated. Will try to instantiate it. */ int opal_check(void); /* Call an OPAL method. Any pointers passed must be real-mode accessible! */ int opal_call(uint64_t token, ...); #define OPAL_CONSOLE_WRITE 1 #define OPAL_CONSOLE_READ 2 +#define OPAL_RTC_READ 3 +#define OPAL_RTC_WRITE 4 #define OPAL_CEC_POWER_DOWN 5 #define OPAL_CEC_REBOOT 6 +#define OPAL_POLL_EVENTS 10 #define OPAL_PCI_CONFIG_READ_BYTE 13 #define OPAL_PCI_CONFIG_READ_HALF_WORD 14 #define OPAL_PCI_CONFIG_READ_WORD 15 #define OPAL_PCI_CONFIG_WRITE_BYTE 16 #define OPAL_PCI_CONFIG_WRITE_HALF_WORD 17 #define OPAL_PCI_CONFIG_WRITE_WORD 18 #define OPAL_SET_XIVE 19 #define OPAL_GET_XIVE 20 #define OPAL_PCI_SET_PE 31 -#define OPAL_START_CPU 41 +#define OPAL_START_CPU 41 #define OPAL_PCI_MAP_PE_DMA_WINDOW_REAL 45 /* For OPAL_PCI_SET_PE */ #define OPAL_UNMAP_PE 0 #define OPAL_MAP_PE 1 #define OPAL_SUCCESS 0 #define OPAL_PARAMETER -1 #define OPAL_BUSY_EVENT -12 #endif Index: projects/powernv/powerpc/powernv/opal_dev.c =================================================================== --- projects/powernv/powerpc/powernv/opal_dev.c (revision 290871) +++ projects/powernv/powerpc/powernv/opal_dev.c (revision 290872) @@ -1,162 +1,219 @@ /*- * Copyright (c) 2015 Nathan Whitehorn * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include +#include +#include +#include + #include #include #include #include "clock_if.h" #include "opal.h" static int opaldev_probe(device_t); static int opaldev_attach(device_t); /* clock interface */ static int opal_gettime(device_t dev, struct timespec *ts); static int opal_settime(device_t dev, struct timespec *ts); /* ofw bus interface */ static const struct ofw_bus_devinfo *opaldev_get_devinfo(device_t dev, device_t child); static void opal_shutdown(void *arg, int howto); static device_method_t opaldev_methods[] = { /* Device interface */ DEVMETHOD(device_probe, opaldev_probe), DEVMETHOD(device_attach, opaldev_attach), /* clock interface */ DEVMETHOD(clock_gettime, opal_gettime), DEVMETHOD(clock_settime, opal_settime), /* ofw_bus interface */ DEVMETHOD(ofw_bus_get_devinfo, opaldev_get_devinfo), DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), DEVMETHOD_END }; static driver_t opaldev_driver = { "opal", opaldev_methods, 0 }; static devclass_t opaldev_devclass; DRIVER_MODULE(opaldev, ofwbus, opaldev_driver, opaldev_devclass, 0, 0); static int opaldev_probe(device_t dev) { if (!ofw_bus_is_compatible(dev, "ibm,opal-v3")) return (ENXIO); if (opal_check() != 0) return (ENXIO); device_set_desc(dev, "OPAL Abstraction Firmware"); return (BUS_PROBE_SPECIFIC); } static int opaldev_attach(device_t dev) { phandle_t child; device_t cdev; + uint64_t junk; + int rv; struct ofw_bus_devinfo *dinfo; - if (0 /* XXX NOT YET TEST FOR RTC */) + /* Test for RTC support and register clock if it works */ + rv = opal_call(OPAL_RTC_READ, vtophys(&junk), vtophys(&junk)); + do { + rv = opal_call(OPAL_RTC_READ, vtophys(&junk), vtophys(&junk)); + if (rv == OPAL_BUSY_EVENT) + rv = opal_call(OPAL_POLL_EVENTS, 0); + } while (rv == OPAL_BUSY_EVENT); + + if (rv == OPAL_SUCCESS) clock_register(dev, 2000); EVENTHANDLER_REGISTER(shutdown_final, opal_shutdown, NULL, SHUTDOWN_PRI_LAST); for (child = OF_child(ofw_bus_get_node(dev)); child != 0; child = OF_peer(child)) { dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO); if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) { free(dinfo, M_DEVBUF); continue; } cdev = device_add_child(dev, NULL, -1); if (cdev == NULL) { device_printf(dev, "<%s>: device_add_child failed\n", dinfo->obd_name); ofw_bus_gen_destroy_devinfo(dinfo); free(dinfo, M_DEVBUF); continue; } device_set_ivars(cdev, dinfo); } return (bus_generic_attach(dev)); } static int -opal_gettime(device_t dev, struct timespec *ts) { - return (ENXIO); +bcd2bin32(int bcd) +{ + int out = 0; + + out += bcd2bin(bcd & 0xff); + out += 100*bcd2bin((bcd & 0x0000ff00) >> 8); + out += 10000*bcd2bin((bcd & 0x00ff0000) >> 16); + out += 1000000*bcd2bin((bcd & 0xffff0000) >> 24); + + return (out); } static int +opal_gettime(device_t dev, struct timespec *ts) +{ + int rv; + struct clocktime ct; + uint32_t ymd; + uint64_t hmsm; + + do { + rv = opal_call(OPAL_RTC_READ, vtophys(&ymd), vtophys(&hmsm)); + if (rv == OPAL_BUSY_EVENT) { + rv = opal_call(OPAL_POLL_EVENTS, 0); + pause("opalrtc", 1); + } + } while (rv == OPAL_BUSY_EVENT); + + if (rv != OPAL_SUCCESS) + return (ENXIO); + + hmsm = be64toh(hmsm); + ymd = be32toh(ymd); + + ct.nsec = bcd2bin32((hmsm & 0x000000ffffff0000) >> 16) * 1000; + ct.sec = bcd2bin((hmsm & 0x0000ff0000000000) >> 40); + ct.min = bcd2bin((hmsm & 0x00ff000000000000) >> 48); + ct.hour = bcd2bin((hmsm & 0xff00000000000000) >> 56); + + ct.day = bcd2bin((ymd & 0x000000ff) >> 0); + ct.mon = bcd2bin((ymd & 0x0000ff00) >> 8); + ct.year = bcd2bin32((ymd & 0xffff0000) >> 16); + + return (clock_ct_to_ts(&ct, ts)); +} + +static int opal_settime(device_t dev, struct timespec *ts) { + return (0); } static const struct ofw_bus_devinfo * opaldev_get_devinfo(device_t dev, device_t child) { return (device_get_ivars(child)); } static void opal_shutdown(void *arg, int howto) { if (howto & RB_HALT) opal_call(OPAL_CEC_POWER_DOWN, 0 /* Normal power off */); else opal_call(OPAL_CEC_REBOOT); }