2 * Copyright (C) 2006-2008 Semihalf, Grzegorz Bernacki
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 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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/clock.h>
36 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/resource.h>
43 #include <machine/bus.h>
44 #include <machine/resource.h>
46 #include <powerpc/mpc85xx/ds1553_reg.h>
47 #include <powerpc/mpc85xx/lbc.h>
51 static devclass_t rtc_devclass;
53 static int rtc_attach(device_t dev);
54 static int rtc_probe(device_t dev);
56 static device_method_t rtc_methods[] = {
57 /* Device interface */
58 DEVMETHOD(device_probe, rtc_probe),
59 DEVMETHOD(device_attach, rtc_attach),
62 DEVMETHOD(clock_gettime, ds1553_gettime),
63 DEVMETHOD(clock_settime, ds1553_settime),
68 static driver_t rtc_driver = {
71 sizeof(struct ds1553_softc),
74 DRIVER_MODULE(rtc, lbc, rtc_driver, rtc_devclass, 0, 0);
77 rtc_probe(device_t dev)
82 error = BUS_READ_IVAR(device_get_parent(dev), dev, LBC_IVAR_DEVTYPE,
87 if (devtype != LBC_DEVTYPE_RTC)
90 device_set_desc(dev, "Dallas Semiconductor DS1553 RTC");
96 rtc_attach(device_t dev)
99 struct ds1553_softc *sc;
102 sc = device_get_softc(dev);
103 bzero(sc, sizeof(struct ds1553_softc));
105 mtx_init(&sc->sc_mtx, "rtc_mtx", NULL, MTX_SPIN);
107 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
109 if (sc->res == NULL) {
110 device_printf(dev, "cannot allocate resources\n");
111 mtx_destroy(&sc->sc_mtx);
115 sc->sc_bst = rman_get_bustag(sc->res);
116 sc->sc_bsh = rman_get_bushandle(sc->res);
118 if ((error = ds1553_attach(dev)) != 0) {
119 device_printf(dev, "cannot attach time of day clock\n");
120 bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
121 mtx_destroy(&sc->sc_mtx);
125 clock_register(dev, 1000000);
128 ds1553_gettime(dev, &ts);
129 device_printf(dev, "current time: %ld.%09ld\n",
130 (long)ts.tv_sec, ts.tv_nsec);