rtc.c revision 170845
1/*- 2 * Copyright (c) 2004 Marius Strobl <marius@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/sparc64/sparc64/rtc.c 170845 2007-06-16 23:17:23Z marius $"); 29 30/* 31 * The `rtc' device is a MC146818 compatible clock found on the ISA 32 * bus and EBus. The EBus variant actually is the Real-Time Clock 33 * function of a National Semiconductor PC87317/PC97317 which also 34 * provides Advanced Power Control functionality. 35 */ 36 37#include "opt_isa.h" 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/bus.h> 42#include <sys/kernel.h> 43#include <sys/lock.h> 44#include <sys/module.h> 45#include <sys/mutex.h> 46#include <sys/resource.h> 47 48#include <dev/ofw/ofw_bus.h> 49 50#include <machine/bus.h> 51#include <machine/resource.h> 52 53#include <sys/rman.h> 54 55#include <isa/isavar.h> 56 57#include <dev/mc146818/mc146818reg.h> 58#include <dev/mc146818/mc146818var.h> 59 60#include "clock_if.h" 61 62#define RTC_DESC "Real-Time Clock" 63 64#define RTC_READ mc146818_def_read 65#define RTC_WRITE mc146818_def_write 66 67#define PC87317_COMMON MC_REGA_DV0 /* bank 0 */ 68#define PC87317_RTC (MC_REGA_DV1 | MC_REGA_DV0) /* bank 1 */ 69#define PC87317_RTC_CR 0x48 /* Century Register */ 70#define PC87317_APC MC_REGA_DV2 /* bank 2 */ 71#define PC87317_APC_CADDR 0x51 /* Century Address Register */ 72#define PC87317_APC_CADDR_BANK0 0x00 /* locate CR in bank 0 */ 73#define PC87317_APC_CADDR_BANK1 0x80 /* locate CR in bank 1 */ 74 75static devclass_t rtc_devclass; 76 77static device_attach_t rtc_attach; 78static device_probe_t rtc_ebus_probe; 79#ifdef DEV_ISA 80static device_probe_t rtc_isa_probe; 81#endif 82 83static device_method_t rtc_ebus_methods[] = { 84 /* Device interface */ 85 DEVMETHOD(device_probe, rtc_ebus_probe), 86 DEVMETHOD(device_attach, rtc_attach), 87 88 /* clock interface */ 89 DEVMETHOD(clock_gettime, mc146818_gettime), 90 DEVMETHOD(clock_settime, mc146818_settime), 91 92 { 0, 0 } 93}; 94 95static driver_t rtc_ebus_driver = { 96 "rtc", 97 rtc_ebus_methods, 98 sizeof(struct mc146818_softc), 99}; 100 101DRIVER_MODULE(rtc, ebus, rtc_ebus_driver, rtc_devclass, 0, 0); 102 103#ifdef DEV_ISA 104static device_method_t rtc_isa_methods[] = { 105 /* Device interface */ 106 DEVMETHOD(device_probe, rtc_isa_probe), 107 DEVMETHOD(device_attach, rtc_attach), 108 109 /* clock interface */ 110 DEVMETHOD(clock_gettime, mc146818_gettime), 111 DEVMETHOD(clock_settime, mc146818_settime), 112 113 { 0, 0 } 114}; 115 116static driver_t rtc_isa_driver = { 117 "rtc", 118 rtc_isa_methods, 119 sizeof(struct mc146818_softc), 120}; 121 122DRIVER_MODULE(rtc, isa, rtc_isa_driver, rtc_devclass, 0, 0); 123#endif 124 125static u_int pc87317_getcent(device_t); 126static void pc87317_setcent(device_t, u_int); 127 128static int 129rtc_ebus_probe(device_t dev) 130{ 131 132 if (strcmp(ofw_bus_get_name(dev), "rtc") == 0) { 133 device_set_desc(dev, RTC_DESC); 134 return (0); 135 } 136 137 return (ENXIO); 138} 139 140#ifdef DEV_ISA 141static struct isa_pnp_id rtc_isa_ids[] = { 142 { 0x000bd041, RTC_DESC }, /* PNP0B00 */ 143 { 0 } 144}; 145 146static int 147rtc_isa_probe(device_t dev) 148{ 149 150 if (ISA_PNP_PROBE(device_get_parent(dev), dev, rtc_isa_ids) == 0) 151 return (0); 152 153 return (ENXIO); 154} 155#endif 156 157static int 158rtc_attach(device_t dev) 159{ 160 struct timespec ts; 161 struct mc146818_softc *sc; 162 struct resource *res; 163 int ebus, error, rid; 164 165 sc = device_get_softc(dev); 166 167 mtx_init(&sc->sc_mtx, "rtc_mtx", NULL, MTX_SPIN); 168 169 ebus = 0; 170 if (strcmp(device_get_name(device_get_parent(dev)), "ebus") == 0) 171 ebus = 1; 172 173 rid = 0; 174 res = bus_alloc_resource_any(dev, ebus ? SYS_RES_MEMORY : 175 SYS_RES_IOPORT, &rid, RF_ACTIVE); 176 if (res == NULL) { 177 device_printf(dev, "cannot allocate resources\n"); 178 error = ENXIO; 179 goto fail_mtx; 180 } 181 sc->sc_bst = rman_get_bustag(res); 182 sc->sc_bsh = rman_get_bushandle(res); 183 184 sc->sc_mcread = RTC_READ; 185 sc->sc_mcwrite = RTC_WRITE; 186 /* The TOD clock year 0 is 0. */ 187 sc->sc_year0 = 0; 188 /* 189 * For ISA use the default century get/set functions, for EBus we 190 * provide our own versions. 191 */ 192 sc->sc_flag = MC146818_NO_CENT_ADJUST; 193 if (ebus) { 194 /* 195 * Make sure the CR is at the default location (also used 196 * by Solaris). 197 */ 198 RTC_WRITE(dev, MC_REGA, PC87317_APC); 199 RTC_WRITE(dev, PC87317_APC_CADDR, PC87317_APC_CADDR_BANK1 | 200 PC87317_RTC_CR); 201 RTC_WRITE(dev, MC_REGA, PC87317_COMMON); 202 sc->sc_getcent = pc87317_getcent; 203 sc->sc_setcent = pc87317_setcent; 204 } 205 if ((error = mc146818_attach(dev)) != 0) { 206 device_printf(dev, "cannot attach time of day clock\n"); 207 goto fail_res; 208 } 209 210 if (bootverbose) { 211 mc146818_gettime(dev, &ts); 212 device_printf(dev, "current time: %ld.%09ld\n", (long)ts.tv_sec, 213 ts.tv_nsec); 214 } 215 216 return (0); 217 218 fail_res: 219 bus_release_resource(dev, ebus ? SYS_RES_MEMORY : SYS_RES_IOPORT, rid, 220 res); 221 fail_mtx: 222 mtx_destroy(&sc->sc_mtx); 223 224 return (error); 225} 226 227static u_int 228pc87317_getcent(device_t dev) 229{ 230 u_int cent; 231 232 RTC_WRITE(dev, MC_REGA, PC87317_RTC); 233 cent = RTC_READ(dev, PC87317_RTC_CR); 234 RTC_WRITE(dev, MC_REGA, PC87317_COMMON); 235 return (cent); 236} 237 238static void 239pc87317_setcent(device_t dev, u_int cent) 240{ 241 242 RTC_WRITE(dev, MC_REGA, PC87317_RTC); 243 RTC_WRITE(dev, PC87317_RTC_CR, cent); 244 RTC_WRITE(dev, MC_REGA, PC87317_COMMON); 245} 246