xref: /freebsd/sys/powerpc/mpc85xx/ds1553_bus_fdt.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
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  * From: FreeBSD: src/sys/powerpc/mpc85xx/ds1553_bus_lbc.c,v 1.2 2009/06/24 15:48:20 raj
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/clock.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/resource.h>
42 #include <sys/rman.h>
43 
44 #include <machine/bus.h>
45 #include <machine/resource.h>
46 
47 #include <dev/ofw/ofw_bus_subr.h>
48 
49 #include "ds1553_reg.h"
50 #include "clock_if.h"
51 
52 static devclass_t rtc_devclass;
53 
54 static int rtc_attach(device_t dev);
55 static int rtc_probe(device_t dev);
56 
57 static device_method_t rtc_methods[] = {
58 	/* Device interface */
59 	DEVMETHOD(device_probe,		rtc_probe),
60 	DEVMETHOD(device_attach,	rtc_attach),
61 
62 	/* clock interface */
63 	DEVMETHOD(clock_gettime,	ds1553_gettime),
64 	DEVMETHOD(clock_settime,	ds1553_settime),
65 
66 	{ 0, 0 }
67 };
68 
69 static driver_t rtc_driver = {
70 	"rtc",
71 	rtc_methods,
72 	sizeof(struct ds1553_softc),
73 };
74 
75 DRIVER_MODULE(rtc, lbc, rtc_driver, rtc_devclass, 0, 0);
76 
77 static int
78 rtc_probe(device_t dev)
79 {
80 
81 	if (!ofw_bus_is_compatible(dev, "dallas,ds1553"))
82 		return (ENXIO);
83 
84 	device_set_desc(dev, "Dallas Semiconductor DS1553 RTC");
85 	return (BUS_PROBE_DEFAULT);
86 }
87 
88 static int
89 rtc_attach(device_t dev)
90 {
91 	struct timespec ts;
92 	struct ds1553_softc *sc;
93 	int error;
94 
95 	sc = device_get_softc(dev);
96 	bzero(sc, sizeof(struct ds1553_softc));
97 
98 	mtx_init(&sc->sc_mtx, "rtc_mtx", NULL, MTX_SPIN);
99 
100 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
101 	    RF_ACTIVE);
102 	if (sc->res == NULL) {
103 		device_printf(dev, "cannot allocate resources\n");
104 		mtx_destroy(&sc->sc_mtx);
105 		return (ENXIO);
106 	}
107 
108 	sc->sc_bst = rman_get_bustag(sc->res);
109 	sc->sc_bsh = rman_get_bushandle(sc->res);
110 
111 	if ((error = ds1553_attach(dev)) != 0) {
112 		device_printf(dev, "cannot attach time of day clock\n");
113 		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
114 		mtx_destroy(&sc->sc_mtx);
115 		return (error);
116 	}
117 
118 	clock_register(dev, 1000000);
119 
120 	if (bootverbose) {
121 		ds1553_gettime(dev, &ts);
122 		device_printf(dev, "current time: %ld.%09ld\n",
123 		    (long)ts.tv_sec, ts.tv_nsec);
124 	}
125 
126 	return (0);
127 }
128