Home | History | Annotate | Line # | Download | only in dev
rtcsram.c revision 1.2
      1  1.2  jmcneill /* $NetBSD: rtcsram.c,v 1.2 2024/02/10 11:00:15 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.2  jmcneill __KERNEL_RCSID(0, "$NetBSD: rtcsram.c,v 1.2 2024/02/10 11:00:15 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.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.1  jmcneill 	struct todr_chip_handle	sc_todr;
     70  1.1  jmcneill 
     71  1.1  jmcneill 	uint8_t			sc_chan;
     72  1.1  jmcneill 	uint8_t			sc_device;
     73  1.1  jmcneill 
     74  1.1  jmcneill 	struct rtcsram_sram	sc_sram;
     75  1.1  jmcneill };
     76  1.1  jmcneill 
     77  1.1  jmcneill static int	rtcsram_match(device_t, cfdata_t, void *);
     78  1.1  jmcneill static void	rtcsram_attach(device_t, device_t, void *);
     79  1.1  jmcneill 
     80  1.1  jmcneill static uint32_t	rtcsram_read_4(struct rtcsram_softc *, uint32_t);
     81  1.1  jmcneill static void	rtcsram_write_4(struct rtcsram_softc *, uint32_t, uint32_t);
     82  1.1  jmcneill static void	rtcsram_read_buf(struct rtcsram_softc *, uint32_t, void *,
     83  1.1  jmcneill 				 size_t);
     84  1.1  jmcneill 
     85  1.1  jmcneill static int	rtcsram_gettime(todr_chip_handle_t, struct timeval *);
     86  1.1  jmcneill static int	rtcsram_settime(todr_chip_handle_t, struct timeval *);
     87  1.1  jmcneill 
     88  1.1  jmcneill CFATTACH_DECL_NEW(rtcsram, sizeof(struct rtcsram_softc),
     89  1.1  jmcneill 	rtcsram_match, rtcsram_attach, NULL, NULL);
     90  1.1  jmcneill 
     91  1.1  jmcneill static int
     92  1.1  jmcneill rtcsram_match(device_t parent, cfdata_t cf, void *aux)
     93  1.1  jmcneill {
     94  1.1  jmcneill 	struct exi_attach_args * const eaa = aux;
     95  1.1  jmcneill 
     96  1.1  jmcneill 	return eaa->eaa_id == WII_RTCSRAM_ID;
     97  1.1  jmcneill }
     98  1.1  jmcneill 
     99  1.1  jmcneill static void
    100  1.1  jmcneill rtcsram_attach(device_t parent, device_t self, void *aux)
    101  1.1  jmcneill {
    102  1.1  jmcneill 	struct rtcsram_softc * const sc = device_private(self);
    103  1.1  jmcneill 	struct exi_attach_args * const eaa = aux;
    104  1.1  jmcneill 
    105  1.1  jmcneill 	aprint_naive("\n");
    106  1.1  jmcneill 	aprint_normal(": RTC/SRAM\n");
    107  1.1  jmcneill 
    108  1.1  jmcneill 	sc->sc_chan = eaa->eaa_chan;
    109  1.1  jmcneill 	sc->sc_device = eaa->eaa_device;
    110  1.1  jmcneill 
    111  1.1  jmcneill 	/* Read RTC counter bias from SRAM. */
    112  1.1  jmcneill 	rtcsram_read_buf(sc, SRAM_BASE, &sc->sc_sram, sizeof(sc->sc_sram));
    113  1.1  jmcneill 	aprint_debug_dev(self, "counter bias %d\n", sc->sc_sram.counter_bias);
    114  1.1  jmcneill 	hexdump(aprint_debug, device_xname(self), &sc->sc_sram,
    115  1.1  jmcneill 	    sizeof(sc->sc_sram));
    116  1.1  jmcneill 
    117  1.1  jmcneill 	sc->sc_todr.cookie = sc;
    118  1.1  jmcneill 	sc->sc_todr.todr_gettime = rtcsram_gettime;
    119  1.1  jmcneill 	sc->sc_todr.todr_settime = rtcsram_settime;
    120  1.1  jmcneill 	todr_attach(&sc->sc_todr);
    121  1.1  jmcneill }
    122  1.1  jmcneill 
    123  1.1  jmcneill static uint32_t
    124  1.1  jmcneill rtcsram_read_4(struct rtcsram_softc *sc, uint32_t offset)
    125  1.1  jmcneill {
    126  1.1  jmcneill 	uint32_t val;
    127  1.1  jmcneill 
    128  1.2  jmcneill 	exi_select(sc->sc_chan, sc->sc_device, WII_RTCSRAM_FREQ);
    129  1.1  jmcneill 	exi_send_imm(sc->sc_chan, sc->sc_device, &offset, sizeof(offset));
    130  1.1  jmcneill 	exi_recv_imm(sc->sc_chan, sc->sc_device, &val, sizeof(val));
    131  1.1  jmcneill 	exi_unselect(sc->sc_chan);
    132  1.1  jmcneill 
    133  1.1  jmcneill 	return val;
    134  1.1  jmcneill }
    135  1.1  jmcneill 
    136  1.1  jmcneill static void
    137  1.1  jmcneill rtcsram_write_4(struct rtcsram_softc *sc, uint32_t offset, uint32_t val)
    138  1.1  jmcneill {
    139  1.1  jmcneill 	offset |= WRITE_OFFSET;
    140  1.1  jmcneill 
    141  1.2  jmcneill 	exi_select(sc->sc_chan, sc->sc_device, WII_RTCSRAM_FREQ);
    142  1.1  jmcneill 	exi_send_imm(sc->sc_chan, sc->sc_device, &offset, sizeof(offset));
    143  1.1  jmcneill 	exi_send_imm(sc->sc_chan, sc->sc_device, &val, sizeof(val));
    144  1.1  jmcneill 	exi_unselect(sc->sc_chan);
    145  1.1  jmcneill }
    146  1.1  jmcneill 
    147  1.1  jmcneill static void
    148  1.1  jmcneill rtcsram_read_buf(struct rtcsram_softc *sc, uint32_t offset, void *data,
    149  1.1  jmcneill     size_t datalen)
    150  1.1  jmcneill {
    151  1.2  jmcneill 	exi_select(sc->sc_chan, sc->sc_device, WII_RTCSRAM_FREQ);
    152  1.1  jmcneill 	exi_send_imm(sc->sc_chan, sc->sc_device, &offset, sizeof(offset));
    153  1.1  jmcneill 	exi_recv_dma(sc->sc_chan, sc->sc_device, data, datalen);
    154  1.1  jmcneill 	exi_unselect(sc->sc_chan);
    155  1.1  jmcneill }
    156  1.1  jmcneill 
    157  1.1  jmcneill static int
    158  1.1  jmcneill rtcsram_gettime(todr_chip_handle_t ch, struct timeval *tv)
    159  1.1  jmcneill {
    160  1.1  jmcneill 	struct rtcsram_softc * const sc = ch->cookie;
    161  1.1  jmcneill 	uint32_t val;
    162  1.1  jmcneill 
    163  1.1  jmcneill 	val = rtcsram_read_4(sc, RTC_BASE);
    164  1.1  jmcneill 	tv->tv_sec = (uint64_t)val + sc->sc_sram.counter_bias;
    165  1.1  jmcneill 	tv->tv_usec = 0;
    166  1.1  jmcneill 
    167  1.1  jmcneill 	return 0;
    168  1.1  jmcneill }
    169  1.1  jmcneill 
    170  1.1  jmcneill static int
    171  1.1  jmcneill rtcsram_settime(todr_chip_handle_t ch, struct timeval *tv)
    172  1.1  jmcneill {
    173  1.1  jmcneill 	struct rtcsram_softc * const sc = ch->cookie;
    174  1.1  jmcneill 
    175  1.1  jmcneill 	rtcsram_write_4(sc, RTC_BASE, tv->tv_sec - sc->sc_sram.counter_bias);
    176  1.1  jmcneill 
    177  1.1  jmcneill 	return 0;
    178  1.1  jmcneill }
    179