tegra_rtc.c revision 1.1 1 /* $NetBSD: tegra_rtc.c,v 1.1 2015/05/05 00:25:44 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
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 ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * 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
29 #include "locators.h"
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: tegra_rtc.c,v 1.1 2015/05/05 00:25:44 jmcneill Exp $");
33
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/device.h>
37 #include <sys/intr.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/kmem.h>
41
42 #include <dev/clock_subr.h>
43
44 #include <arm/nvidia/tegra_reg.h>
45 #include <arm/nvidia/tegra_rtcreg.h>
46 #include <arm/nvidia/tegra_var.h>
47
48 static int tegra_rtc_match(device_t, cfdata_t, void *);
49 static void tegra_rtc_attach(device_t, device_t, void *);
50
51 struct tegra_rtc_softc {
52 device_t sc_dev;
53 bus_space_tag_t sc_bst;
54 bus_space_handle_t sc_bsh;
55
56 struct todr_chip_handle sc_todr;
57 };
58
59 static int tegra_rtc_gettime(todr_chip_handle_t, struct timeval *);
60 static int tegra_rtc_settime(todr_chip_handle_t, struct timeval *);
61
62 CFATTACH_DECL_NEW(tegra_rtc, sizeof(struct tegra_rtc_softc),
63 tegra_rtc_match, tegra_rtc_attach, NULL, NULL);
64
65 #define RTC_READ(sc, reg) \
66 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
67 #define RTC_WRITE(sc, reg, val) \
68 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
69
70 static int
71 tegra_rtc_match(device_t parent, cfdata_t cf, void *aux)
72 {
73 return 1;
74 }
75
76 static void
77 tegra_rtc_attach(device_t parent, device_t self, void *aux)
78 {
79 struct tegra_rtc_softc * const sc = device_private(self);
80 struct tegraio_attach_args * const tio = aux;
81 const struct tegra_locators * const loc = &tio->tio_loc;
82
83 sc->sc_dev = self;
84 sc->sc_bst = tio->tio_bst;
85 bus_space_subregion(tio->tio_bst, tio->tio_bsh,
86 loc->loc_offset, loc->loc_size, &sc->sc_bsh);
87
88 aprint_naive("\n");
89 aprint_normal(": RTC\n");
90
91 sc->sc_todr.todr_gettime = tegra_rtc_gettime;
92 sc->sc_todr.todr_settime = tegra_rtc_settime;
93 sc->sc_todr.cookie = sc;
94 todr_attach(&sc->sc_todr);
95 }
96
97 static int
98 tegra_rtc_gettime(todr_chip_handle_t tch, struct timeval *tv)
99 {
100 struct tegra_rtc_softc * const sc = tch->cookie;
101
102 tv->tv_sec = RTC_READ(sc, RTC_SECONDS_REG);
103 tv->tv_usec = 0;
104
105 return 0;
106 }
107
108 static int
109 tegra_rtc_settime(todr_chip_handle_t tch, struct timeval *tv)
110 {
111 struct tegra_rtc_softc * const sc = tch->cookie;
112 int retry = 500;
113
114 while (--retry > 0) {
115 if ((RTC_READ(sc, RTC_BUSY_REG) & RTC_BUSY_STATUS) == 0)
116 break;
117 delay(1);
118 }
119 if (retry == 0) {
120 device_printf(sc->sc_dev, "RTC write failed (BUSY)\n");
121 return ETIMEDOUT;
122 }
123
124 RTC_WRITE(sc, RTC_SECONDS_REG, tv->tv_sec);
125
126 return 0;
127 }
128