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