1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2006-2008 Semihalf, Grzegorz Bernacki
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * From: FreeBSD: src/sys/powerpc/mpc85xx/ds1553_bus_lbc.c,v 1.2 2009/06/24 15:48:20 raj
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <sys/clock.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/resource.h>
44 #include <sys/rman.h>
45 
46 #include <machine/bus.h>
47 #include <machine/resource.h>
48 
49 #include <dev/ofw/ofw_bus_subr.h>
50 
51 #include "ds1553_reg.h"
52 #include "clock_if.h"
53 
54 static devclass_t rtc_devclass;
55 
56 static int rtc_attach(device_t dev);
57 static int rtc_probe(device_t dev);
58 
59 static device_method_t rtc_methods[] = {
60 	/* Device interface */
61 	DEVMETHOD(device_probe,		rtc_probe),
62 	DEVMETHOD(device_attach,	rtc_attach),
63 
64 	/* clock interface */
65 	DEVMETHOD(clock_gettime,	ds1553_gettime),
66 	DEVMETHOD(clock_settime,	ds1553_settime),
67 	{ 0, 0 }
68 };
69 
70 static driver_t rtc_driver = {
71 	"rtc",
72 	rtc_methods,
73 	sizeof(struct ds1553_softc),
74 };
75 
76 DRIVER_MODULE(rtc, lbc, rtc_driver, rtc_devclass, 0, 0);
77 
78 static int
79 rtc_probe(device_t dev)
80 {
81 
82 	if (!ofw_bus_is_compatible(dev, "dallas,ds1553"))
83 		return (ENXIO);
84 
85 	device_set_desc(dev, "Dallas Semiconductor DS1553 RTC");
86 	return (BUS_PROBE_DEFAULT);
87 }
88 
89 static int
90 rtc_attach(device_t dev)
91 {
92 	struct timespec ts;
93 	struct ds1553_softc *sc;
94 	int error;
95 
96 	sc = device_get_softc(dev);
97 	bzero(sc, sizeof(struct ds1553_softc));
98 
99 	mtx_init(&sc->sc_mtx, "rtc_mtx", NULL, MTX_SPIN);
100 
101 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
102 	    RF_ACTIVE);
103 	if (sc->res == NULL) {
104 		device_printf(dev, "cannot allocate resources\n");
105 		mtx_destroy(&sc->sc_mtx);
106 		return (ENXIO);
107 	}
108 
109 	sc->sc_bst = rman_get_bustag(sc->res);
110 	sc->sc_bsh = rman_get_bushandle(sc->res);
111 
112 	if ((error = ds1553_attach(dev)) != 0) {
113 		device_printf(dev, "cannot attach time of day clock\n");
114 		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
115 		mtx_destroy(&sc->sc_mtx);
116 		return (error);
117 	}
118 
119 	clock_register(dev, 1000000);
120 
121 	if (bootverbose) {
122 		ds1553_gettime(dev, &ts);
123 		device_printf(dev, "current time: %ld.%09ld\n",
124 		    (long)ts.tv_sec, ts.tv_nsec);
125 	}
126 
127 	return (0);
128 }
129