1178172Simp/*- 2178172Simp * Copyright (c) 2004 Marius Strobl <marius@FreeBSD.org> 3178172Simp * All rights reserved. 4178172Simp * 5178172Simp * Redistribution and use in source and binary forms, with or without 6178172Simp * modification, are permitted provided that the following conditions 7178172Simp * are met: 8178172Simp * 1. Redistributions of source code must retain the above copyright 9178172Simp * notice, this list of conditions and the following disclaimer. 10178172Simp * 2. Redistributions in binary form must reproduce the above copyright 11178172Simp * notice, this list of conditions and the following disclaimer in the 12178172Simp * documentation and/or other materials provided with the distribution. 13178172Simp * 14178172Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15178172Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16178172Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17178172Simp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18178172Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19178172Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20178172Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21178172Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22178172Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23178172Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24178172Simp * SUCH DAMAGE. 25178172Simp */ 26178172Simp 27178172Simp#include <sys/cdefs.h> 28178172Simp__FBSDID("$FreeBSD: releng/10.3/sys/sparc64/sparc64/rtc.c 227848 2011-11-22 21:55:40Z marius $"); 29178172Simp 30178172Simp/* 31178172Simp * The `rtc' device is found on the ISA bus and the EBus. The ISA version 32178172Simp * always is a MC146818 compatible clock while the EBus variant either is the 33178172Simp * MC146818 compatible Real-Time Clock function of a National Semiconductor 34178172Simp * PC87317/PC97317 which also provides Advanced Power Control functionality 35178172Simp * or a Texas Instruments bq4802. 36178172Simp */ 37178172Simp 38178172Simp#include "opt_isa.h" 39178172Simp 40178172Simp#include <sys/param.h> 41178172Simp#include <sys/systm.h> 42229677Sgonzo#include <sys/bus.h> 43229677Sgonzo#include <sys/kernel.h> 44229677Sgonzo#include <sys/lock.h> 45229677Sgonzo#include <sys/module.h> 46178172Simp#include <sys/mutex.h> 47178172Simp#include <sys/resource.h> 48178172Simp 49178172Simp#include <dev/ofw/ofw_bus.h> 50217354Sjchandra 51217354Sjchandra#include <machine/bus.h> 52217354Sjchandra#include <machine/resource.h> 53217354Sjchandra 54217354Sjchandra#include <sys/rman.h> 55217354Sjchandra 56217354Sjchandra#include <isa/isavar.h> 57217354Sjchandra 58178172Simp#include <dev/mc146818/mc146818reg.h> 59217354Sjchandra#include <dev/mc146818/mc146818var.h> 60178172Simp 61217354Sjchandra#include "clock_if.h" 62217354Sjchandra 63217354Sjchandra#define RTC_DESC "Real-Time Clock" 64217354Sjchandra 65229677Sgonzo#define RTC_READ mc146818_def_read 66229677Sgonzo#define RTC_WRITE mc146818_def_write 67229677Sgonzo 68229677Sgonzo#define PC87317_COMMON MC_REGA_DV0 /* bank 0 */ 69229677Sgonzo#define PC87317_RTC (MC_REGA_DV1 | MC_REGA_DV0) /* bank 1 */ 70229677Sgonzo#define PC87317_RTC_CR 0x48 /* Century Register */ 71229677Sgonzo#define PC87317_APC MC_REGA_DV2 /* bank 2 */ 72178172Simp#define PC87317_APC_CADDR 0x51 /* Century Address Register */ 73178172Simp#define PC87317_APC_CADDR_BANK0 0x00 /* locate CR in bank 0 */ 74178172Simp#define PC87317_APC_CADDR_BANK1 0x80 /* locate CR in bank 1 */ 75178172Simp 76229677Sgonzostatic devclass_t rtc_devclass; 77178172Simp 78178172Simpstatic device_attach_t rtc_attach; 79178172Simpstatic device_probe_t rtc_ebus_probe; 80178172Simp#ifdef DEV_ISA 81178172Simpstatic device_probe_t rtc_isa_probe; 82226065Skib#endif 83178172Simp 84178172Simpstatic device_method_t rtc_ebus_methods[] = { 85178172Simp /* Device interface */ 86178172Simp DEVMETHOD(device_probe, rtc_ebus_probe), 87178172Simp DEVMETHOD(device_attach, rtc_attach), 88226065Skib 89226065Skib /* clock interface */ 90226065Skib DEVMETHOD(clock_gettime, mc146818_gettime), 91226065Skib DEVMETHOD(clock_settime, mc146818_settime), 92226065Skib 93226065Skib DEVMETHOD_END 94226065Skib}; 95226065Skib 96226065Skibstatic driver_t rtc_ebus_driver = { 97207152Skib "rtc", 98207269Skib rtc_ebus_methods, 99207152Skib sizeof(struct mc146818_softc), 100207269Skib}; 101207152Skib 102207152SkibDRIVER_MODULE(rtc, ebus, rtc_ebus_driver, rtc_devclass, 0, 0); 103178172Simp 104#ifdef DEV_ISA 105static device_method_t rtc_isa_methods[] = { 106 /* Device interface */ 107 DEVMETHOD(device_probe, rtc_isa_probe), 108 DEVMETHOD(device_attach, rtc_attach), 109 110 /* clock interface */ 111 DEVMETHOD(clock_gettime, mc146818_gettime), 112 DEVMETHOD(clock_settime, mc146818_settime), 113 114 DEVMETHOD_END 115}; 116 117static driver_t rtc_isa_driver = { 118 "rtc", 119 rtc_isa_methods, 120 sizeof(struct mc146818_softc), 121}; 122 123DRIVER_MODULE(rtc, isa, rtc_isa_driver, rtc_devclass, 0, 0); 124#endif 125 126static u_int pc87317_getcent(device_t dev); 127static void pc87317_setcent(device_t dev, u_int cent); 128 129static int 130rtc_ebus_probe(device_t dev) 131{ 132 133 if (strcmp(ofw_bus_get_name(dev), "rtc") == 0) { 134 /* The bq4802 is not supported, yet. */ 135 if (ofw_bus_get_compat(dev) != NULL && 136 strcmp(ofw_bus_get_compat(dev), "bq4802") == 0) 137 return (ENXIO); 138 device_set_desc(dev, RTC_DESC); 139 return (0); 140 } 141 142 return (ENXIO); 143} 144 145#ifdef DEV_ISA 146static struct isa_pnp_id rtc_isa_ids[] = { 147 { 0x000bd041, RTC_DESC }, /* PNP0B00 */ 148 { 0 } 149}; 150 151static int 152rtc_isa_probe(device_t dev) 153{ 154 155 if (ISA_PNP_PROBE(device_get_parent(dev), dev, rtc_isa_ids) == 0) 156 return (0); 157 158 return (ENXIO); 159} 160#endif 161 162static int 163rtc_attach(device_t dev) 164{ 165 struct timespec ts; 166 struct mc146818_softc *sc; 167 struct resource *res; 168 int ebus, error, rid; 169 170 sc = device_get_softc(dev); 171 172 mtx_init(&sc->sc_mtx, "rtc_mtx", NULL, MTX_SPIN); 173 174 ebus = 0; 175 if (strcmp(device_get_name(device_get_parent(dev)), "ebus") == 0) 176 ebus = 1; 177 178 rid = 0; 179 res = bus_alloc_resource_any(dev, ebus ? SYS_RES_MEMORY : 180 SYS_RES_IOPORT, &rid, RF_ACTIVE); 181 if (res == NULL) { 182 device_printf(dev, "cannot allocate resources\n"); 183 error = ENXIO; 184 goto fail_mtx; 185 } 186 sc->sc_bst = rman_get_bustag(res); 187 sc->sc_bsh = rman_get_bushandle(res); 188 189 sc->sc_mcread = RTC_READ; 190 sc->sc_mcwrite = RTC_WRITE; 191 /* The TOD clock year 0 is 0. */ 192 sc->sc_year0 = 0; 193 /* 194 * For ISA use the default century get/set functions, for EBus we 195 * provide our own versions. 196 */ 197 sc->sc_flag = MC146818_NO_CENT_ADJUST; 198 if (ebus) { 199 /* 200 * Make sure the CR is at the default location (also used 201 * by Solaris). 202 */ 203 RTC_WRITE(dev, MC_REGA, PC87317_APC); 204 RTC_WRITE(dev, PC87317_APC_CADDR, PC87317_APC_CADDR_BANK1 | 205 PC87317_RTC_CR); 206 RTC_WRITE(dev, MC_REGA, PC87317_COMMON); 207 sc->sc_getcent = pc87317_getcent; 208 sc->sc_setcent = pc87317_setcent; 209 } 210 if ((error = mc146818_attach(dev)) != 0) { 211 device_printf(dev, "cannot attach time of day clock\n"); 212 goto fail_res; 213 } 214 215 if (bootverbose) { 216 if (mc146818_gettime(dev, &ts) != 0) 217 device_printf(dev, "invalid time"); 218 else 219 device_printf(dev, "current time: %ld.%09ld\n", 220 (long)ts.tv_sec, ts.tv_nsec); 221 } 222 223 return (0); 224 225 fail_res: 226 bus_release_resource(dev, ebus ? SYS_RES_MEMORY : SYS_RES_IOPORT, rid, 227 res); 228 fail_mtx: 229 mtx_destroy(&sc->sc_mtx); 230 231 return (error); 232} 233 234static u_int 235pc87317_getcent(device_t dev) 236{ 237 u_int cent; 238 239 RTC_WRITE(dev, MC_REGA, PC87317_RTC); 240 cent = RTC_READ(dev, PC87317_RTC_CR); 241 RTC_WRITE(dev, MC_REGA, PC87317_COMMON); 242 return (cent); 243} 244 245static void 246pc87317_setcent(device_t dev, u_int cent) 247{ 248 249 RTC_WRITE(dev, MC_REGA, PC87317_RTC); 250 RTC_WRITE(dev, PC87317_RTC_CR, cent); 251 RTC_WRITE(dev, MC_REGA, PC87317_COMMON); 252} 253