1 /*-
2  * Copyright (c) 2003-2009 RMI Corporation
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  * 3. Neither the name of RMI Corporation, nor the names of its contributors,
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * RMI_BSD */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: stable/9/sys/mips/rmi/dev/iic/ds1374u.c 216410 2010-12-13 17:53:38Z jchandra $");
33 /*
34  *  RTC chip sitting on the I2C bus.
35  */
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/bus.h>
41 #include <sys/clock.h>
42 #include <sys/time.h>
43 #include <sys/resource.h>
44 #include <sys/rman.h>
45 
46 #include <mips/include/bus.h>
47 #include <mips/include/cpu.h>
48 #include <mips/include/cpufunc.h>
49 #include <mips/include/frame.h>
50 #include <mips/include/resource.h>
51 
52 #include <dev/iicbus/iiconf.h>
53 #include <dev/iicbus/iicbus.h>
54 
55 #include "iicbus_if.h"
56 #include "clock_if.h"
57 
58 #define	DS1374_RTC_COUNTER	0	/* counter (bytes 0-3) */
59 
60 struct ds1374u_softc {
61 	uint32_t	sc_addr;
62 	device_t	sc_dev;
63 };
64 
65 static int
ds1374u_probe(device_t dev)66 ds1374u_probe(device_t dev)
67 {
68 	device_set_desc(dev, "DS1374U-33 RTC");
69 	return (0);
70 }
71 
72 static int
ds1374u_attach(device_t dev)73 ds1374u_attach(device_t dev)
74 {
75 	struct ds1374u_softc *sc = device_get_softc(dev);
76 
77 	if(sc==NULL) {
78 		printf("ds1374u_attach device_get_softc failed\n");
79 		return (0);
80 	}
81 	sc->sc_dev = dev;
82 	sc->sc_addr = iicbus_get_addr(dev);
83 
84 	clock_register(dev, 1000);
85 	return (0);
86 }
87 
88 static int
ds1374u_settime(device_t dev,struct timespec * ts)89 ds1374u_settime(device_t dev, struct timespec *ts)
90 {
91 	/* NB: register pointer precedes actual data */
92 	uint8_t data[5] = { DS1374_RTC_COUNTER };
93 	struct ds1374u_softc *sc = device_get_softc(dev);
94 	struct iic_msg msgs[1] = {
95 	     { sc->sc_addr, IIC_M_WR, 5, data },
96 	};
97 
98 	data[1] = (ts->tv_sec >> 0) & 0xff;
99 	data[2] = (ts->tv_sec >> 8) & 0xff;
100 	data[3] = (ts->tv_sec >> 16) & 0xff;
101 	data[4] = (ts->tv_sec >> 24) & 0xff;
102 
103 	return iicbus_transfer(dev, msgs, 1);
104 }
105 
106 static int
ds1374u_gettime(device_t dev,struct timespec * ts)107 ds1374u_gettime(device_t dev, struct timespec *ts)
108 {
109 	struct ds1374u_softc *sc = device_get_softc(dev);
110 	uint8_t addr[1] = { DS1374_RTC_COUNTER };
111 	uint8_t secs[4];
112 	struct iic_msg msgs[2] = {
113 	     { sc->sc_addr, IIC_M_WR, 1, addr },
114 	     { sc->sc_addr, IIC_M_RD, 4, secs },
115 	};
116 	int error;
117 
118 	error = iicbus_transfer(dev, msgs, 2);
119 	if (error == 0) {
120 		/* counter has seconds since epoch */
121 		ts->tv_sec = (secs[3] << 24) | (secs[2] << 16)
122 			   | (secs[1] <<  8) | (secs[0] <<  0);
123 		ts->tv_nsec = 0;
124 	}
125 	return error;
126 }
127 
128 static device_method_t ds1374u_methods[] = {
129 	DEVMETHOD(device_probe,		ds1374u_probe),
130 	DEVMETHOD(device_attach,	ds1374u_attach),
131 
132 	DEVMETHOD(clock_gettime,	ds1374u_gettime),
133 	DEVMETHOD(clock_settime,	ds1374u_settime),
134 
135 	{0, 0},
136 };
137 
138 static driver_t ds1374u_driver = {
139 	"ds1374u",
140 	ds1374u_methods,
141 	sizeof(struct ds1374u_softc),
142 };
143 static devclass_t ds1374u_devclass;
144 
145 DRIVER_MODULE(ds1374u, iicbus, ds1374u_driver, ds1374u_devclass, 0, 0);
146 MODULE_VERSION(ds1374u, 1);
147 MODULE_DEPEND(ds1374u, iicbus, 1, 1, 1);
148