2 * Copyright (c) 2015 Nathan Whitehorn
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/module.h>
35 #include <sys/clock.h>
37 #include <sys/kernel.h>
38 #include <sys/reboot.h>
39 #include <sys/sysctl.h>
40 #include <sys/endian.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 #include <dev/ofw/openfirm.h>
52 static int opaldev_probe(device_t);
53 static int opaldev_attach(device_t);
55 static int opal_gettime(device_t dev, struct timespec *ts);
56 static int opal_settime(device_t dev, struct timespec *ts);
57 /* ofw bus interface */
58 static const struct ofw_bus_devinfo *opaldev_get_devinfo(device_t dev,
61 static void opal_shutdown(void *arg, int howto);
62 static void opal_intr(void *);
64 static device_method_t opaldev_methods[] = {
65 /* Device interface */
66 DEVMETHOD(device_probe, opaldev_probe),
67 DEVMETHOD(device_attach, opaldev_attach),
70 DEVMETHOD(clock_gettime, opal_gettime),
71 DEVMETHOD(clock_settime, opal_settime),
74 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
76 /* ofw_bus interface */
77 DEVMETHOD(ofw_bus_get_devinfo, opaldev_get_devinfo),
78 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
79 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
80 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
81 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
82 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
87 static driver_t opaldev_driver = {
93 static devclass_t opaldev_devclass;
95 DRIVER_MODULE(opaldev, ofwbus, opaldev_driver, opaldev_devclass, 0, 0);
98 opaldev_probe(device_t dev)
104 if (!ofw_bus_is_compatible(dev, "ibm,opal-v3"))
106 if (opal_check() != 0)
109 device_set_desc(dev, "OPAL Abstraction Firmware");
111 /* Manually add IRQs before attaching */
112 if (OF_hasprop(ofw_bus_get_node(dev), "opal-interrupts")) {
113 iparent = OF_finddevice("/interrupt-controller@0");
114 iparent = OF_xref_from_node(iparent);
116 n_irqs = OF_getproplen(ofw_bus_get_node(dev),
117 "opal-interrupts") / sizeof(*irqs);
118 irqs = malloc(n_irqs * sizeof(*irqs), M_DEVBUF, M_WAITOK);
119 OF_getencprop(ofw_bus_get_node(dev), "opal-interrupts", irqs,
120 n_irqs * sizeof(*irqs));
121 for (i = 0; i < n_irqs; i++)
122 bus_set_resource(dev, SYS_RES_IRQ, i,
123 ofw_bus_map_intr(dev, iparent, 1, &irqs[i]), 1);
124 free(irqs, M_DEVBUF);
128 return (BUS_PROBE_SPECIFIC);
132 opaldev_attach(device_t dev)
138 struct ofw_bus_devinfo *dinfo;
139 struct resource *irq;
141 /* Test for RTC support and register clock if it works */
142 rv = opal_call(OPAL_RTC_READ, vtophys(&junk), vtophys(&junk));
144 rv = opal_call(OPAL_RTC_READ, vtophys(&junk), vtophys(&junk));
145 if (rv == OPAL_BUSY_EVENT)
146 rv = opal_call(OPAL_POLL_EVENTS, 0);
147 } while (rv == OPAL_BUSY_EVENT);
149 if (rv == OPAL_SUCCESS)
150 clock_register(dev, 2000);
152 EVENTHANDLER_REGISTER(shutdown_final, opal_shutdown, NULL,
155 /* Bind to interrupts */
156 for (i = 0; (irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
157 RF_ACTIVE)) != NULL; i++)
158 bus_setup_intr(dev, irq, INTR_TYPE_TTY | INTR_MPSAFE |
159 INTR_ENTROPY, NULL, opal_intr, (void *)rman_get_start(irq),
162 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
163 child = OF_peer(child)) {
164 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
165 if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
166 free(dinfo, M_DEVBUF);
169 cdev = device_add_child(dev, NULL, -1);
171 device_printf(dev, "<%s>: device_add_child failed\n",
173 ofw_bus_gen_destroy_devinfo(dinfo);
174 free(dinfo, M_DEVBUF);
177 device_set_ivars(cdev, dinfo);
180 return (bus_generic_attach(dev));
188 out += bcd2bin(bcd & 0xff);
189 out += 100*bcd2bin((bcd & 0x0000ff00) >> 8);
190 out += 10000*bcd2bin((bcd & 0x00ff0000) >> 16);
191 out += 1000000*bcd2bin((bcd & 0xffff0000) >> 24);
203 out += bin2bcd(tmp) * 1;
207 out += bin2bcd(tmp) * 100;
211 out += bin2bcd(tmp) * 10000;
217 opal_gettime(device_t dev, struct timespec *ts)
224 rv = opal_call(OPAL_RTC_READ, vtophys(&ymd), vtophys(&hmsm));
225 while (rv == OPAL_BUSY_EVENT) {
226 opal_call(OPAL_POLL_EVENTS, 0);
228 rv = opal_call(OPAL_RTC_READ, vtophys(&ymd), vtophys(&hmsm));
231 if (rv != OPAL_SUCCESS)
234 hmsm = be64toh(hmsm);
237 ct.nsec = bcd2bin32((hmsm & 0x000000ffffff0000) >> 16) * 1000;
238 ct.sec = bcd2bin((hmsm & 0x0000ff0000000000) >> 40);
239 ct.min = bcd2bin((hmsm & 0x00ff000000000000) >> 48);
240 ct.hour = bcd2bin((hmsm & 0xff00000000000000) >> 56);
242 ct.day = bcd2bin((ymd & 0x000000ff) >> 0);
243 ct.mon = bcd2bin((ymd & 0x0000ff00) >> 8);
244 ct.year = bcd2bin32((ymd & 0xffff0000) >> 16);
246 return (clock_ct_to_ts(&ct, ts));
250 opal_settime(device_t dev, struct timespec *ts)
257 clock_ts_to_ct(ts, &ct);
259 ymd |= (uint32_t)bin2bcd(ct.day);
260 ymd |= ((uint32_t)bin2bcd(ct.mon) << 8);
261 ymd |= ((uint32_t)bin2bcd32(ct.year) << 16);
263 hmsm |= ((uint64_t)bin2bcd32(ct.nsec/1000) << 16);
264 hmsm |= ((uint64_t)bin2bcd(ct.sec) << 40);
265 hmsm |= ((uint64_t)bin2bcd(ct.min) << 48);
266 hmsm |= ((uint64_t)bin2bcd(ct.hour) << 56);
268 hmsm = htobe64(hmsm);
272 rv = opal_call(OPAL_RTC_WRITE, vtophys(&ymd), vtophys(&hmsm));
273 if (rv == OPAL_BUSY_EVENT) {
274 rv = opal_call(OPAL_POLL_EVENTS, 0);
277 } while (rv == OPAL_BUSY_EVENT);
279 if (rv != OPAL_SUCCESS)
285 static const struct ofw_bus_devinfo *
286 opaldev_get_devinfo(device_t dev, device_t child)
288 return (device_get_ivars(child));
292 opal_shutdown(void *arg, int howto)
296 opal_call(OPAL_CEC_POWER_DOWN, 0 /* Normal power off */);
298 opal_call(OPAL_CEC_REBOOT);
300 opal_call(OPAL_RETURN_CPU);
304 opal_intr(void *xintr)
308 opal_call(OPAL_HANDLE_INTERRUPT, (uint32_t)(uint64_t)xintr,
310 /* XXX: do something useful with this information */