rtcsram.c revision 1.1 1 1.1 jmcneill /* $NetBSD: rtcsram.c,v 1.1 2024/01/25 11:47:53 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2024 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: rtcsram.c,v 1.1 2024/01/25 11:47:53 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/systm.h>
36 1.1 jmcneill #include <dev/clock_subr.h>
37 1.1 jmcneill
38 1.1 jmcneill #include <lib/libkern/libkern.h>
39 1.1 jmcneill
40 1.1 jmcneill #include "exi.h"
41 1.1 jmcneill
42 1.1 jmcneill #define WII_RTCSRAM_ID 0xfffff308
43 1.1 jmcneill
44 1.1 jmcneill #define RTC_BASE 0x20000000
45 1.1 jmcneill #define SRAM_BASE 0x20000100
46 1.1 jmcneill
47 1.1 jmcneill #define WRITE_OFFSET 0x80000000
48 1.1 jmcneill
49 1.1 jmcneill struct rtcsram_sram {
50 1.1 jmcneill uint16_t checksum[2];
51 1.1 jmcneill uint16_t ead[2];
52 1.1 jmcneill int32_t counter_bias;
53 1.1 jmcneill int8_t display_offset_h;
54 1.1 jmcneill uint8_t ntd;
55 1.1 jmcneill uint8_t language;
56 1.1 jmcneill uint8_t flags;
57 1.1 jmcneill uint16_t flash_id[12];
58 1.1 jmcneill uint32_t wireless_keyboard_id;
59 1.1 jmcneill uint32_t wireless_pad_id[2];
60 1.1 jmcneill uint8_t last_dvd_errorcode;
61 1.1 jmcneill uint8_t padding1;
62 1.1 jmcneill uint16_t flash_id_checksum[2];
63 1.1 jmcneill uint16_t padding2;
64 1.1 jmcneill } __aligned(32);
65 1.1 jmcneill CTASSERT(sizeof(struct rtcsram_sram) == 64);
66 1.1 jmcneill
67 1.1 jmcneill struct rtcsram_softc {
68 1.1 jmcneill struct todr_chip_handle sc_todr;
69 1.1 jmcneill
70 1.1 jmcneill uint8_t sc_chan;
71 1.1 jmcneill uint8_t sc_device;
72 1.1 jmcneill
73 1.1 jmcneill struct rtcsram_sram sc_sram;
74 1.1 jmcneill };
75 1.1 jmcneill
76 1.1 jmcneill static int rtcsram_match(device_t, cfdata_t, void *);
77 1.1 jmcneill static void rtcsram_attach(device_t, device_t, void *);
78 1.1 jmcneill
79 1.1 jmcneill static uint32_t rtcsram_read_4(struct rtcsram_softc *, uint32_t);
80 1.1 jmcneill static void rtcsram_write_4(struct rtcsram_softc *, uint32_t, uint32_t);
81 1.1 jmcneill static void rtcsram_read_buf(struct rtcsram_softc *, uint32_t, void *,
82 1.1 jmcneill size_t);
83 1.1 jmcneill
84 1.1 jmcneill static int rtcsram_gettime(todr_chip_handle_t, struct timeval *);
85 1.1 jmcneill static int rtcsram_settime(todr_chip_handle_t, struct timeval *);
86 1.1 jmcneill
87 1.1 jmcneill CFATTACH_DECL_NEW(rtcsram, sizeof(struct rtcsram_softc),
88 1.1 jmcneill rtcsram_match, rtcsram_attach, NULL, NULL);
89 1.1 jmcneill
90 1.1 jmcneill static int
91 1.1 jmcneill rtcsram_match(device_t parent, cfdata_t cf, void *aux)
92 1.1 jmcneill {
93 1.1 jmcneill struct exi_attach_args * const eaa = aux;
94 1.1 jmcneill
95 1.1 jmcneill return eaa->eaa_id == WII_RTCSRAM_ID;
96 1.1 jmcneill }
97 1.1 jmcneill
98 1.1 jmcneill static void
99 1.1 jmcneill rtcsram_attach(device_t parent, device_t self, void *aux)
100 1.1 jmcneill {
101 1.1 jmcneill struct rtcsram_softc * const sc = device_private(self);
102 1.1 jmcneill struct exi_attach_args * const eaa = aux;
103 1.1 jmcneill
104 1.1 jmcneill aprint_naive("\n");
105 1.1 jmcneill aprint_normal(": RTC/SRAM\n");
106 1.1 jmcneill
107 1.1 jmcneill sc->sc_chan = eaa->eaa_chan;
108 1.1 jmcneill sc->sc_device = eaa->eaa_device;
109 1.1 jmcneill
110 1.1 jmcneill /* Read RTC counter bias from SRAM. */
111 1.1 jmcneill rtcsram_read_buf(sc, SRAM_BASE, &sc->sc_sram, sizeof(sc->sc_sram));
112 1.1 jmcneill aprint_debug_dev(self, "counter bias %d\n", sc->sc_sram.counter_bias);
113 1.1 jmcneill hexdump(aprint_debug, device_xname(self), &sc->sc_sram,
114 1.1 jmcneill sizeof(sc->sc_sram));
115 1.1 jmcneill
116 1.1 jmcneill sc->sc_todr.cookie = sc;
117 1.1 jmcneill sc->sc_todr.todr_gettime = rtcsram_gettime;
118 1.1 jmcneill sc->sc_todr.todr_settime = rtcsram_settime;
119 1.1 jmcneill todr_attach(&sc->sc_todr);
120 1.1 jmcneill }
121 1.1 jmcneill
122 1.1 jmcneill static uint32_t
123 1.1 jmcneill rtcsram_read_4(struct rtcsram_softc *sc, uint32_t offset)
124 1.1 jmcneill {
125 1.1 jmcneill uint32_t val;
126 1.1 jmcneill
127 1.1 jmcneill exi_select(sc->sc_chan, sc->sc_device);
128 1.1 jmcneill exi_send_imm(sc->sc_chan, sc->sc_device, &offset, sizeof(offset));
129 1.1 jmcneill exi_recv_imm(sc->sc_chan, sc->sc_device, &val, sizeof(val));
130 1.1 jmcneill exi_unselect(sc->sc_chan);
131 1.1 jmcneill
132 1.1 jmcneill return val;
133 1.1 jmcneill }
134 1.1 jmcneill
135 1.1 jmcneill static void
136 1.1 jmcneill rtcsram_write_4(struct rtcsram_softc *sc, uint32_t offset, uint32_t val)
137 1.1 jmcneill {
138 1.1 jmcneill offset |= WRITE_OFFSET;
139 1.1 jmcneill
140 1.1 jmcneill exi_select(sc->sc_chan, sc->sc_device);
141 1.1 jmcneill exi_send_imm(sc->sc_chan, sc->sc_device, &offset, sizeof(offset));
142 1.1 jmcneill exi_send_imm(sc->sc_chan, sc->sc_device, &val, sizeof(val));
143 1.1 jmcneill exi_unselect(sc->sc_chan);
144 1.1 jmcneill }
145 1.1 jmcneill
146 1.1 jmcneill static void
147 1.1 jmcneill rtcsram_read_buf(struct rtcsram_softc *sc, uint32_t offset, void *data,
148 1.1 jmcneill size_t datalen)
149 1.1 jmcneill {
150 1.1 jmcneill exi_select(sc->sc_chan, sc->sc_device);
151 1.1 jmcneill exi_send_imm(sc->sc_chan, sc->sc_device, &offset, sizeof(offset));
152 1.1 jmcneill exi_recv_dma(sc->sc_chan, sc->sc_device, data, datalen);
153 1.1 jmcneill exi_unselect(sc->sc_chan);
154 1.1 jmcneill }
155 1.1 jmcneill
156 1.1 jmcneill static int
157 1.1 jmcneill rtcsram_gettime(todr_chip_handle_t ch, struct timeval *tv)
158 1.1 jmcneill {
159 1.1 jmcneill struct rtcsram_softc * const sc = ch->cookie;
160 1.1 jmcneill uint32_t val;
161 1.1 jmcneill
162 1.1 jmcneill val = rtcsram_read_4(sc, RTC_BASE);
163 1.1 jmcneill tv->tv_sec = (uint64_t)val + sc->sc_sram.counter_bias;
164 1.1 jmcneill tv->tv_usec = 0;
165 1.1 jmcneill
166 1.1 jmcneill return 0;
167 1.1 jmcneill }
168 1.1 jmcneill
169 1.1 jmcneill static int
170 1.1 jmcneill rtcsram_settime(todr_chip_handle_t ch, struct timeval *tv)
171 1.1 jmcneill {
172 1.1 jmcneill struct rtcsram_softc * const sc = ch->cookie;
173 1.1 jmcneill
174 1.1 jmcneill rtcsram_write_4(sc, RTC_BASE, tv->tv_sec - sc->sc_sram.counter_bias);
175 1.1 jmcneill
176 1.1 jmcneill return 0;
177 1.1 jmcneill }
178