1 1.3 thorpej /* $NetBSD: rtcsram.c,v 1.3 2025/09/07 21:45:13 thorpej 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.3 thorpej __KERNEL_RCSID(0, "$NetBSD: rtcsram.c,v 1.3 2025/09/07 21:45:13 thorpej 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.2 jmcneill #define WII_RTCSRAM_FREQ EXI_FREQ_8MHZ 44 1.1 jmcneill 45 1.1 jmcneill #define RTC_BASE 0x20000000 46 1.1 jmcneill #define SRAM_BASE 0x20000100 47 1.1 jmcneill 48 1.1 jmcneill #define WRITE_OFFSET 0x80000000 49 1.1 jmcneill 50 1.1 jmcneill struct rtcsram_sram { 51 1.1 jmcneill uint16_t checksum[2]; 52 1.1 jmcneill uint16_t ead[2]; 53 1.1 jmcneill int32_t counter_bias; 54 1.1 jmcneill int8_t display_offset_h; 55 1.1 jmcneill uint8_t ntd; 56 1.1 jmcneill uint8_t language; 57 1.1 jmcneill uint8_t flags; 58 1.1 jmcneill uint16_t flash_id[12]; 59 1.1 jmcneill uint32_t wireless_keyboard_id; 60 1.1 jmcneill uint32_t wireless_pad_id[2]; 61 1.1 jmcneill uint8_t last_dvd_errorcode; 62 1.1 jmcneill uint8_t padding1; 63 1.1 jmcneill uint16_t flash_id_checksum[2]; 64 1.1 jmcneill uint16_t padding2; 65 1.1 jmcneill } __aligned(32); 66 1.1 jmcneill CTASSERT(sizeof(struct rtcsram_sram) == 64); 67 1.1 jmcneill 68 1.1 jmcneill struct rtcsram_softc { 69 1.3 thorpej device_t sc_dev; 70 1.1 jmcneill struct todr_chip_handle sc_todr; 71 1.1 jmcneill 72 1.1 jmcneill uint8_t sc_chan; 73 1.1 jmcneill uint8_t sc_device; 74 1.1 jmcneill 75 1.1 jmcneill struct rtcsram_sram sc_sram; 76 1.1 jmcneill }; 77 1.1 jmcneill 78 1.1 jmcneill static int rtcsram_match(device_t, cfdata_t, void *); 79 1.1 jmcneill static void rtcsram_attach(device_t, device_t, void *); 80 1.1 jmcneill 81 1.1 jmcneill static uint32_t rtcsram_read_4(struct rtcsram_softc *, uint32_t); 82 1.1 jmcneill static void rtcsram_write_4(struct rtcsram_softc *, uint32_t, uint32_t); 83 1.1 jmcneill static void rtcsram_read_buf(struct rtcsram_softc *, uint32_t, void *, 84 1.1 jmcneill size_t); 85 1.1 jmcneill 86 1.1 jmcneill static int rtcsram_gettime(todr_chip_handle_t, struct timeval *); 87 1.1 jmcneill static int rtcsram_settime(todr_chip_handle_t, struct timeval *); 88 1.1 jmcneill 89 1.1 jmcneill CFATTACH_DECL_NEW(rtcsram, sizeof(struct rtcsram_softc), 90 1.1 jmcneill rtcsram_match, rtcsram_attach, NULL, NULL); 91 1.1 jmcneill 92 1.1 jmcneill static int 93 1.1 jmcneill rtcsram_match(device_t parent, cfdata_t cf, void *aux) 94 1.1 jmcneill { 95 1.1 jmcneill struct exi_attach_args * const eaa = aux; 96 1.1 jmcneill 97 1.1 jmcneill return eaa->eaa_id == WII_RTCSRAM_ID; 98 1.1 jmcneill } 99 1.1 jmcneill 100 1.1 jmcneill static void 101 1.1 jmcneill rtcsram_attach(device_t parent, device_t self, void *aux) 102 1.1 jmcneill { 103 1.1 jmcneill struct rtcsram_softc * const sc = device_private(self); 104 1.1 jmcneill struct exi_attach_args * const eaa = aux; 105 1.1 jmcneill 106 1.1 jmcneill aprint_naive("\n"); 107 1.1 jmcneill aprint_normal(": RTC/SRAM\n"); 108 1.1 jmcneill 109 1.3 thorpej sc->sc_dev = self; 110 1.1 jmcneill sc->sc_chan = eaa->eaa_chan; 111 1.1 jmcneill sc->sc_device = eaa->eaa_device; 112 1.1 jmcneill 113 1.1 jmcneill /* Read RTC counter bias from SRAM. */ 114 1.1 jmcneill rtcsram_read_buf(sc, SRAM_BASE, &sc->sc_sram, sizeof(sc->sc_sram)); 115 1.1 jmcneill aprint_debug_dev(self, "counter bias %d\n", sc->sc_sram.counter_bias); 116 1.1 jmcneill hexdump(aprint_debug, device_xname(self), &sc->sc_sram, 117 1.1 jmcneill sizeof(sc->sc_sram)); 118 1.1 jmcneill 119 1.3 thorpej sc->sc_todr.todr_dev = self; 120 1.1 jmcneill sc->sc_todr.todr_gettime = rtcsram_gettime; 121 1.1 jmcneill sc->sc_todr.todr_settime = rtcsram_settime; 122 1.1 jmcneill todr_attach(&sc->sc_todr); 123 1.1 jmcneill } 124 1.1 jmcneill 125 1.1 jmcneill static uint32_t 126 1.1 jmcneill rtcsram_read_4(struct rtcsram_softc *sc, uint32_t offset) 127 1.1 jmcneill { 128 1.1 jmcneill uint32_t val; 129 1.1 jmcneill 130 1.2 jmcneill exi_select(sc->sc_chan, sc->sc_device, WII_RTCSRAM_FREQ); 131 1.1 jmcneill exi_send_imm(sc->sc_chan, sc->sc_device, &offset, sizeof(offset)); 132 1.1 jmcneill exi_recv_imm(sc->sc_chan, sc->sc_device, &val, sizeof(val)); 133 1.1 jmcneill exi_unselect(sc->sc_chan); 134 1.1 jmcneill 135 1.1 jmcneill return val; 136 1.1 jmcneill } 137 1.1 jmcneill 138 1.1 jmcneill static void 139 1.1 jmcneill rtcsram_write_4(struct rtcsram_softc *sc, uint32_t offset, uint32_t val) 140 1.1 jmcneill { 141 1.1 jmcneill offset |= WRITE_OFFSET; 142 1.1 jmcneill 143 1.2 jmcneill exi_select(sc->sc_chan, sc->sc_device, WII_RTCSRAM_FREQ); 144 1.1 jmcneill exi_send_imm(sc->sc_chan, sc->sc_device, &offset, sizeof(offset)); 145 1.1 jmcneill exi_send_imm(sc->sc_chan, sc->sc_device, &val, sizeof(val)); 146 1.1 jmcneill exi_unselect(sc->sc_chan); 147 1.1 jmcneill } 148 1.1 jmcneill 149 1.1 jmcneill static void 150 1.1 jmcneill rtcsram_read_buf(struct rtcsram_softc *sc, uint32_t offset, void *data, 151 1.1 jmcneill size_t datalen) 152 1.1 jmcneill { 153 1.2 jmcneill exi_select(sc->sc_chan, sc->sc_device, WII_RTCSRAM_FREQ); 154 1.1 jmcneill exi_send_imm(sc->sc_chan, sc->sc_device, &offset, sizeof(offset)); 155 1.1 jmcneill exi_recv_dma(sc->sc_chan, sc->sc_device, data, datalen); 156 1.1 jmcneill exi_unselect(sc->sc_chan); 157 1.1 jmcneill } 158 1.1 jmcneill 159 1.1 jmcneill static int 160 1.1 jmcneill rtcsram_gettime(todr_chip_handle_t ch, struct timeval *tv) 161 1.1 jmcneill { 162 1.3 thorpej struct rtcsram_softc * const sc = device_private(ch->todr_dev); 163 1.1 jmcneill uint32_t val; 164 1.1 jmcneill 165 1.1 jmcneill val = rtcsram_read_4(sc, RTC_BASE); 166 1.1 jmcneill tv->tv_sec = (uint64_t)val + sc->sc_sram.counter_bias; 167 1.1 jmcneill tv->tv_usec = 0; 168 1.1 jmcneill 169 1.1 jmcneill return 0; 170 1.1 jmcneill } 171 1.1 jmcneill 172 1.1 jmcneill static int 173 1.1 jmcneill rtcsram_settime(todr_chip_handle_t ch, struct timeval *tv) 174 1.1 jmcneill { 175 1.3 thorpej struct rtcsram_softc * const sc = device_private(ch->todr_dev); 176 1.1 jmcneill 177 1.1 jmcneill rtcsram_write_4(sc, RTC_BASE, tv->tv_sec - sc->sc_sram.counter_bias); 178 1.1 jmcneill 179 1.1 jmcneill return 0; 180 1.1 jmcneill } 181