1 /*-
2 * Copyright (C) 2006-2008 Semihalf, Grzegorz Bernacki
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 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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/clock.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/resource.h>
41 #include <sys/rman.h>
42
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45
46 #include <powerpc/mpc85xx/ds1553_reg.h>
47 #include <powerpc/mpc85xx/lbc.h>
48
49 #include "clock_if.h"
50
51 static devclass_t rtc_devclass;
52
53 static int rtc_attach(device_t dev);
54 static int rtc_probe(device_t dev);
55
56 static device_method_t rtc_methods[] = {
57 /* Device interface */
58 DEVMETHOD(device_probe, rtc_probe),
59 DEVMETHOD(device_attach, rtc_attach),
60
61 /* clock interface */
62 DEVMETHOD(clock_gettime, ds1553_gettime),
63 DEVMETHOD(clock_settime, ds1553_settime),
64
65 { 0, 0 }
66 };
67
68 static driver_t rtc_driver = {
69 "rtc",
70 rtc_methods,
71 sizeof(struct ds1553_softc),
72 };
73
74 DRIVER_MODULE(rtc, lbc, rtc_driver, rtc_devclass, 0, 0);
75
76 static int
77 rtc_probe(device_t dev)
78 {
79 uintptr_t devtype;
80 int error;
81
82 error = BUS_READ_IVAR(device_get_parent(dev), dev, LBC_IVAR_DEVTYPE,
83 &devtype);
84 if (error)
85 return (error);
86
87 if (devtype != LBC_DEVTYPE_RTC)
88 return (EINVAL);
89
90 device_set_desc(dev, "Dallas Semiconductor DS1553 RTC");
91
92 return (0);
93 }
94
95 static int
96 rtc_attach(device_t dev)
97 {
98 struct timespec ts;
99 struct ds1553_softc *sc;
100 int error;
101
102 sc = device_get_softc(dev);
103 bzero(sc, sizeof(struct ds1553_softc));
104
105 mtx_init(&sc->sc_mtx, "rtc_mtx", NULL, MTX_SPIN);
106
107 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
108 RF_ACTIVE);
109 if (sc->res == NULL) {
110 device_printf(dev, "cannot allocate resources\n");
111 mtx_destroy(&sc->sc_mtx);
112 return (ENXIO);
113 }
114
115 sc->sc_bst = rman_get_bustag(sc->res);
116 sc->sc_bsh = rman_get_bushandle(sc->res);
117
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);
122 return (error);
123 }
124
125 clock_register(dev, 1000000);
126
127 if (bootverbose) {
128 ds1553_gettime(dev, &ts);
129 device_printf(dev, "current time: %ld.%09ld\n",
130 (long)ts.tv_sec, ts.tv_nsec);
131 }
132
133 return (0);
134 }
Cache object: e123bee29d448ad22d77cef7769f1cf4
|