lpc_rtc.c revision 266152
1239278Sgonzo/*- 2239278Sgonzo * Copyright (c) 2011 Jakub Wojciech Klama <jceel@FreeBSD.org> 3239278Sgonzo * All rights reserved. 4239278Sgonzo * 5239278Sgonzo * Redistribution and use in source and binary forms, with or without 6239278Sgonzo * modification, are permitted provided that the following conditions 7239278Sgonzo * are met: 8239278Sgonzo * 1. Redistributions of source code must retain the above copyright 9239278Sgonzo * notice, this list of conditions and the following disclaimer. 10239278Sgonzo * 2. Redistributions in binary form must reproduce the above copyright 11239278Sgonzo * notice, this list of conditions and the following disclaimer in the 12239278Sgonzo * documentation and/or other materials provided with the distribution. 13239278Sgonzo * 14239278Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15239278Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16239278Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17239278Sgonzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18239278Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19239278Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20239278Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21239278Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22239278Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23239278Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24239278Sgonzo * SUCH DAMAGE. 25239278Sgonzo * 26239278Sgonzo */ 27239278Sgonzo#include <sys/cdefs.h> 28239278Sgonzo__FBSDID("$FreeBSD: stable/10/sys/arm/lpc/lpc_rtc.c 266152 2014-05-15 16:11:06Z ian $"); 29239278Sgonzo 30239278Sgonzo#include <sys/param.h> 31239278Sgonzo#include <sys/bus.h> 32239278Sgonzo#include <sys/time.h> 33239278Sgonzo#include <sys/clock.h> 34239278Sgonzo#include <sys/resource.h> 35239278Sgonzo#include <sys/systm.h> 36239278Sgonzo#include <sys/rman.h> 37239278Sgonzo#include <sys/kernel.h> 38239278Sgonzo#include <sys/module.h> 39239278Sgonzo 40239278Sgonzo#include <machine/bus.h> 41239278Sgonzo#include <machine/resource.h> 42239278Sgonzo 43239278Sgonzo#include <dev/ofw/ofw_bus.h> 44239278Sgonzo#include <dev/ofw/ofw_bus_subr.h> 45239278Sgonzo 46239278Sgonzo#include <arm/lpc/lpcreg.h> 47239278Sgonzo 48239278Sgonzo#include "clock_if.h" 49239278Sgonzo 50239278Sgonzostruct lpc_rtc_softc { 51239278Sgonzo device_t lr_dev; 52239278Sgonzo struct resource * lr_mem_res; 53239278Sgonzo bus_space_tag_t lr_bst; 54239278Sgonzo bus_space_handle_t lr_bsh; 55239278Sgonzo}; 56239278Sgonzo 57239278Sgonzostatic int lpc_rtc_probe(device_t dev); 58239278Sgonzostatic int lpc_rtc_attach(device_t dev); 59239278Sgonzostatic int lpc_rtc_gettime(device_t dev, struct timespec *ts); 60239278Sgonzostatic int lpc_rtc_settime(device_t, struct timespec *); 61239278Sgonzo 62239278Sgonzostatic int 63239278Sgonzolpc_rtc_probe(device_t dev) 64239278Sgonzo{ 65239278Sgonzo 66266152Sian if (!ofw_bus_status_okay(dev)) 67266152Sian return (ENXIO); 68266152Sian 69239278Sgonzo if (!ofw_bus_is_compatible(dev, "lpc,rtc")) 70239278Sgonzo return (ENXIO); 71239278Sgonzo 72239278Sgonzo device_set_desc(dev, "LPC32x0 real time clock"); 73239278Sgonzo return (BUS_PROBE_DEFAULT); 74239278Sgonzo} 75239278Sgonzo 76239278Sgonzostatic int 77239278Sgonzolpc_rtc_attach(device_t dev) 78239278Sgonzo{ 79239278Sgonzo struct lpc_rtc_softc *sc = device_get_softc(dev); 80239278Sgonzo int rid = 0; 81239278Sgonzo 82239278Sgonzo sc->lr_dev = dev; 83239278Sgonzo 84239278Sgonzo clock_register(dev, 1000000); 85239278Sgonzo 86239278Sgonzo sc->lr_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 87239278Sgonzo RF_ACTIVE); 88239278Sgonzo if (!sc->lr_mem_res) { 89239278Sgonzo device_printf(dev, "cannot allocate memory window\n"); 90239278Sgonzo return (ENXIO); 91239278Sgonzo } 92239278Sgonzo 93239278Sgonzo sc->lr_bst = rman_get_bustag(sc->lr_mem_res); 94239278Sgonzo sc->lr_bsh = rman_get_bushandle(sc->lr_mem_res); 95239278Sgonzo 96239278Sgonzo return (0); 97239278Sgonzo} 98239278Sgonzo 99239278Sgonzostatic int 100239278Sgonzolpc_rtc_gettime(device_t dev, struct timespec *ts) 101239278Sgonzo{ 102239278Sgonzo struct lpc_rtc_softc *sc = device_get_softc(dev); 103239278Sgonzo 104239278Sgonzo ts->tv_sec = bus_space_read_4(sc->lr_bst, sc->lr_bsh, LPC_RTC_UCOUNT); 105239278Sgonzo ts->tv_nsec = 0; 106239278Sgonzo 107239278Sgonzo return (0); 108239278Sgonzo} 109239278Sgonzo 110239278Sgonzostatic int 111239278Sgonzolpc_rtc_settime(device_t dev, struct timespec *ts) 112239278Sgonzo{ 113239278Sgonzo struct lpc_rtc_softc *sc = device_get_softc(dev); 114239278Sgonzo uint32_t ctrl; 115239278Sgonzo 116239278Sgonzo /* Stop RTC */ 117239278Sgonzo ctrl = bus_space_read_4(sc->lr_bst, sc->lr_bsh, LPC_RTC_CTRL); 118239278Sgonzo bus_space_write_4(sc->lr_bst, sc->lr_bsh, LPC_RTC_CTRL, ctrl | LPC_RTC_CTRL_DISABLE); 119239278Sgonzo 120239278Sgonzo /* Write actual value */ 121239278Sgonzo bus_space_write_4(sc->lr_bst, sc->lr_bsh, LPC_RTC_UCOUNT, ts->tv_sec); 122239278Sgonzo 123239278Sgonzo /* Start RTC */ 124239278Sgonzo ctrl = bus_space_read_4(sc->lr_bst, sc->lr_bsh, LPC_RTC_CTRL); 125239278Sgonzo bus_space_write_4(sc->lr_bst, sc->lr_bsh, LPC_RTC_CTRL, ctrl & ~LPC_RTC_CTRL_DISABLE); 126239278Sgonzo 127239278Sgonzo return (0); 128239278Sgonzo} 129239278Sgonzo 130239278Sgonzostatic device_method_t lpc_rtc_methods[] = { 131239278Sgonzo /* Device interface */ 132239278Sgonzo DEVMETHOD(device_probe, lpc_rtc_probe), 133239278Sgonzo DEVMETHOD(device_attach, lpc_rtc_attach), 134239278Sgonzo 135239278Sgonzo /* Clock interface */ 136239278Sgonzo DEVMETHOD(clock_gettime, lpc_rtc_gettime), 137239278Sgonzo DEVMETHOD(clock_settime, lpc_rtc_settime), 138239278Sgonzo 139239278Sgonzo { 0, 0 }, 140239278Sgonzo}; 141239278Sgonzo 142239278Sgonzostatic driver_t lpc_rtc_driver = { 143239278Sgonzo "rtc", 144239278Sgonzo lpc_rtc_methods, 145239278Sgonzo sizeof(struct lpc_rtc_softc), 146239278Sgonzo}; 147239278Sgonzo 148239278Sgonzostatic devclass_t lpc_rtc_devclass; 149239278Sgonzo 150239278SgonzoDRIVER_MODULE(rtc, simplebus, lpc_rtc_driver, lpc_rtc_devclass, 0, 0); 151