xref: /netbsd-src/sys/arch/sparc64/dev/vrtc.c (revision fc256c4a39aa30e04e0a83bb1e40829857c1d023)
1 /*	$NetBSD: vrtc.c,v 1.3 2021/01/04 14:48:52 thorpej Exp $	*/
2 /*	$OpenBSD: vrtc.c,v 1.1 2008/03/08 19:19:43 kettenis Exp $	*/
3 /*
4  * Copyright (c) 2008 Mark Kettenis
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/device.h>
21 #include <sys/kmem.h>
22 #include <sys/systm.h>
23 
24 #include <machine/autoconf.h>
25 #include <machine/hypervisor.h>
26 #include <machine/openfirm.h>
27 
28 #include <dev/clock_subr.h>
29 #include <sparc64/dev/vbusvar.h>
30 
31 extern todr_chip_handle_t todr_handle;
32 
33 int	vrtc_match(device_t, cfdata_t, void *);
34 void	vrtc_attach(device_t, device_t, void *);
35 
36 CFATTACH_DECL_NEW(vrtc, sizeof(device_t),
37     vrtc_match, vrtc_attach, NULL, NULL);
38 
39 int	vrtc_gettime(todr_chip_handle_t, struct timeval *);
40 int	vrtc_settime(todr_chip_handle_t, struct timeval *);
41 
42 int
vrtc_match(device_t parent,cfdata_t match,void * aux)43 vrtc_match(device_t parent, cfdata_t match, void *aux)
44 {
45 	struct vbus_attach_args *va = aux;
46 
47 	if (strcmp(va->va_name, "rtc") == 0)
48 		return (1);
49 
50 	return (0);
51 }
52 
53 void
vrtc_attach(device_t parent,device_t self,void * aux)54 vrtc_attach(device_t parent, device_t self, void *aux)
55 {
56 	todr_chip_handle_t handle;
57 
58 	printf("\n");
59 
60 	handle = kmem_alloc(sizeof(struct todr_chip_handle), KM_SLEEP);
61 	handle->cookie = self;
62 	handle->todr_gettime = vrtc_gettime;
63 	handle->todr_settime = vrtc_settime;
64 
65 	handle->bus_cookie = NULL;
66 	handle->todr_setwen = NULL;
67 
68 	todr_attach(handle);
69 }
70 
71 int
vrtc_gettime(todr_chip_handle_t handle,struct timeval * tv)72 vrtc_gettime(todr_chip_handle_t handle, struct timeval *tv)
73 {
74 	u_int64_t tod;
75 
76 	if (hv_tod_get(&tod) != H_EOK)
77 		return (1);
78 
79 	tv->tv_sec = tod;
80 	tv->tv_usec = 0;
81 	return (0);
82 }
83 
84 int
vrtc_settime(todr_chip_handle_t handle,struct timeval * tv)85 vrtc_settime(todr_chip_handle_t handle, struct timeval *tv)
86 {
87 	if (hv_tod_set(tv->tv_sec) != H_EOK)
88 		return (1);
89 
90 	return (0);
91 }
92