1 1.16 thorpej /* $NetBSD: aurtc.c,v 1.16 2025/09/07 21:45:14 thorpej Exp $ */ 2 1.9 gdamore 3 1.9 gdamore /*- 4 1.9 gdamore * Copyright (c) 2006 Itronix Inc. 5 1.9 gdamore * All rights reserved. 6 1.9 gdamore * 7 1.9 gdamore * Written by Garrett D'Amore for Itronix Inc. 8 1.9 gdamore * 9 1.9 gdamore * Redistribution and use in source and binary forms, with or without 10 1.9 gdamore * modification, are permitted provided that the following conditions 11 1.9 gdamore * are met: 12 1.9 gdamore * 1. Redistributions of source code must retain the above copyright 13 1.9 gdamore * notice, this list of conditions and the following disclaimer. 14 1.9 gdamore * 2. Redistributions in binary form must reproduce the above copyright 15 1.9 gdamore * notice, this list of conditions and the following disclaimer in the 16 1.9 gdamore * documentation and/or other materials provided with the distribution. 17 1.9 gdamore * 3. The name of Itronix Inc. may not be used to endorse 18 1.9 gdamore * or promote products derived from this software without specific 19 1.9 gdamore * prior written permission. 20 1.9 gdamore * 21 1.9 gdamore * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND 22 1.9 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 1.9 gdamore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 1.9 gdamore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY 25 1.9 gdamore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26 1.9 gdamore * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 1.9 gdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 28 1.9 gdamore * ON ANY THEORY OF LIABILITY, WHETHER IN 29 1.9 gdamore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 1.9 gdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 1.9 gdamore * POSSIBILITY OF SUCH DAMAGE. 32 1.9 gdamore */ 33 1.1 simonb 34 1.1 simonb /* 35 1.1 simonb * Copyright 2002 Wasabi Systems, Inc. 36 1.1 simonb * All rights reserved. 37 1.1 simonb * 38 1.1 simonb * Written by Simon Burge for Wasabi Systems, Inc. 39 1.1 simonb * 40 1.1 simonb * Redistribution and use in source and binary forms, with or without 41 1.1 simonb * modification, are permitted provided that the following conditions 42 1.1 simonb * are met: 43 1.1 simonb * 1. Redistributions of source code must retain the above copyright 44 1.1 simonb * notice, this list of conditions and the following disclaimer. 45 1.1 simonb * 2. Redistributions in binary form must reproduce the above copyright 46 1.1 simonb * notice, this list of conditions and the following disclaimer in the 47 1.1 simonb * documentation and/or other materials provided with the distribution. 48 1.1 simonb * 3. All advertising materials mentioning features or use of this software 49 1.1 simonb * must display the following acknowledgement: 50 1.1 simonb * This product includes software developed for the NetBSD Project by 51 1.1 simonb * Wasabi Systems, Inc. 52 1.1 simonb * 4. The name of Wasabi Systems, Inc. may not be used to endorse 53 1.1 simonb * or promote products derived from this software without specific prior 54 1.1 simonb * written permission. 55 1.1 simonb * 56 1.1 simonb * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 57 1.1 simonb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 58 1.1 simonb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 59 1.1 simonb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 60 1.1 simonb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 61 1.1 simonb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 62 1.1 simonb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 63 1.1 simonb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 64 1.1 simonb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 65 1.1 simonb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 66 1.1 simonb * POSSIBILITY OF SUCH DAMAGE. 67 1.1 simonb */ 68 1.6 lukem 69 1.9 gdamore 70 1.6 lukem #include <sys/cdefs.h> 71 1.16 thorpej __KERNEL_RCSID(0, "$NetBSD: aurtc.c,v 1.16 2025/09/07 21:45:14 thorpej Exp $"); 72 1.1 simonb 73 1.1 simonb #include <sys/param.h> 74 1.1 simonb #include <sys/systm.h> 75 1.9 gdamore #include <sys/errno.h> 76 1.1 simonb #include <sys/device.h> 77 1.9 gdamore #include <sys/proc.h> 78 1.1 simonb 79 1.1 simonb #include <dev/clock_subr.h> 80 1.1 simonb 81 1.13 dyoung #include <sys/bus.h> 82 1.9 gdamore 83 1.1 simonb #include <mips/alchemy/include/aureg.h> 84 1.9 gdamore #include <mips/alchemy/include/auvar.h> 85 1.1 simonb #include <mips/alchemy/include/aubusvar.h> 86 1.1 simonb 87 1.9 gdamore #define REGVAL(x) (*(volatile uint32_t *)(MIPS_PHYS_TO_KSEG1(PC_BASE + (x)))) 88 1.9 gdamore #define GETREG(x) (REGVAL(x)) 89 1.9 gdamore #define PUTREG(x,v) (REGVAL(x) = (v)) 90 1.9 gdamore 91 1.9 gdamore struct aurtc_softc { 92 1.14 kiyohara device_t sc_dev; 93 1.9 gdamore struct todr_chip_handle sc_tch; 94 1.9 gdamore void *sc_shutdownhook; 95 1.9 gdamore }; 96 1.9 gdamore 97 1.14 kiyohara static int aurtc_match(device_t, struct cfdata *, void *); 98 1.14 kiyohara static void aurtc_attach(device_t, device_t, void *); 99 1.12 tsutsui static int aurtc_gettime(todr_chip_handle_t, struct timeval *); 100 1.12 tsutsui static int aurtc_settime(todr_chip_handle_t, struct timeval *); 101 1.9 gdamore static void aurtc_shutdown(void *); 102 1.1 simonb 103 1.14 kiyohara CFATTACH_DECL_NEW(aurtc, sizeof (struct aurtc_softc), 104 1.5 thorpej aurtc_match, aurtc_attach, NULL, NULL); 105 1.1 simonb 106 1.1 simonb int 107 1.14 kiyohara aurtc_match(device_t parent, struct cfdata *match, void *aux) 108 1.1 simonb { 109 1.1 simonb struct aubus_attach_args *aa = aux; 110 1.1 simonb 111 1.2 thorpej if (strcmp(aa->aa_name, match->cf_name) == 0) 112 1.1 simonb return (1); 113 1.1 simonb 114 1.1 simonb return (0); 115 1.1 simonb } 116 1.1 simonb 117 1.1 simonb void 118 1.14 kiyohara aurtc_attach(device_t parent, device_t self, void *aux) 119 1.1 simonb { 120 1.14 kiyohara struct aurtc_softc *sc = device_private(self); 121 1.1 simonb 122 1.9 gdamore printf(": Au1X00 programmable clock\n"); 123 1.9 gdamore 124 1.14 kiyohara sc->sc_dev = self; 125 1.16 thorpej sc->sc_tch.todr_dev = self; 126 1.9 gdamore sc->sc_tch.todr_gettime = aurtc_gettime; 127 1.9 gdamore sc->sc_tch.todr_settime = aurtc_settime; 128 1.9 gdamore 129 1.9 gdamore sc->sc_shutdownhook = shutdownhook_establish(aurtc_shutdown, NULL); 130 1.10 gdamore 131 1.10 gdamore todr_attach(&sc->sc_tch); 132 1.1 simonb } 133 1.1 simonb 134 1.9 gdamore /* 135 1.9 gdamore * Note that our RTC only has second resolution. 136 1.9 gdamore */ 137 1.9 gdamore 138 1.9 gdamore int 139 1.12 tsutsui aurtc_gettime(todr_chip_handle_t tch, struct timeval *tv) 140 1.1 simonb { 141 1.9 gdamore int s; 142 1.1 simonb 143 1.9 gdamore s = splclock(); 144 1.9 gdamore tv->tv_sec = GETREG(PC_COUNTER_READ_0); 145 1.9 gdamore splx(s); 146 1.9 gdamore return 0; 147 1.1 simonb } 148 1.1 simonb 149 1.9 gdamore int 150 1.12 tsutsui aurtc_settime(todr_chip_handle_t tch, struct timeval *tvp) 151 1.1 simonb { 152 1.9 gdamore int s; 153 1.9 gdamore struct timeval tv; 154 1.1 simonb 155 1.9 gdamore s = splclock(); 156 1.9 gdamore tv = *tvp; 157 1.9 gdamore splx(s); 158 1.1 simonb 159 1.9 gdamore /* wait for the clock register to be idle */ 160 1.9 gdamore while (GETREG(PC_COUNTER_CONTROL) & CC_C0S) { 161 1.9 gdamore continue; 162 1.9 gdamore } 163 1.1 simonb 164 1.9 gdamore PUTREG(PC_COUNTER_WRITE0, tv.tv_sec); 165 1.9 gdamore 166 1.9 gdamore /* 167 1.9 gdamore * It could take a second or two for the clock change to take effect. 168 1.9 gdamore * We don't want to make settimeofday() take that long, so we don't 169 1.9 gdamore * wait here, but instead wait in flush. This can have a bad effect 170 1.9 gdamore * for settimeofday() calls with a short window between them. 171 1.9 gdamore */ 172 1.9 gdamore return 0; 173 1.1 simonb } 174 1.1 simonb 175 1.9 gdamore void 176 1.9 gdamore aurtc_shutdown(void *arg) 177 1.9 gdamore { 178 1.1 simonb 179 1.9 gdamore /* wait for the clock register to be idle */ 180 1.9 gdamore while (GETREG(PC_COUNTER_CONTROL) & CC_C0S) { 181 1.8 gdamore continue; 182 1.9 gdamore } 183 1.1 simonb } 184