lpc_rtc.c revision 285830
1/*- 2 * Copyright (c) 2011 Jakub Wojciech Klama <jceel@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: releng/10.2/sys/arm/lpc/lpc_rtc.c 266152 2014-05-15 16:11:06Z ian $"); 29 30#include <sys/param.h> 31#include <sys/bus.h> 32#include <sys/time.h> 33#include <sys/clock.h> 34#include <sys/resource.h> 35#include <sys/systm.h> 36#include <sys/rman.h> 37#include <sys/kernel.h> 38#include <sys/module.h> 39 40#include <machine/bus.h> 41#include <machine/resource.h> 42 43#include <dev/ofw/ofw_bus.h> 44#include <dev/ofw/ofw_bus_subr.h> 45 46#include <arm/lpc/lpcreg.h> 47 48#include "clock_if.h" 49 50struct lpc_rtc_softc { 51 device_t lr_dev; 52 struct resource * lr_mem_res; 53 bus_space_tag_t lr_bst; 54 bus_space_handle_t lr_bsh; 55}; 56 57static int lpc_rtc_probe(device_t dev); 58static int lpc_rtc_attach(device_t dev); 59static int lpc_rtc_gettime(device_t dev, struct timespec *ts); 60static int lpc_rtc_settime(device_t, struct timespec *); 61 62static int 63lpc_rtc_probe(device_t dev) 64{ 65 66 if (!ofw_bus_status_okay(dev)) 67 return (ENXIO); 68 69 if (!ofw_bus_is_compatible(dev, "lpc,rtc")) 70 return (ENXIO); 71 72 device_set_desc(dev, "LPC32x0 real time clock"); 73 return (BUS_PROBE_DEFAULT); 74} 75 76static int 77lpc_rtc_attach(device_t dev) 78{ 79 struct lpc_rtc_softc *sc = device_get_softc(dev); 80 int rid = 0; 81 82 sc->lr_dev = dev; 83 84 clock_register(dev, 1000000); 85 86 sc->lr_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 87 RF_ACTIVE); 88 if (!sc->lr_mem_res) { 89 device_printf(dev, "cannot allocate memory window\n"); 90 return (ENXIO); 91 } 92 93 sc->lr_bst = rman_get_bustag(sc->lr_mem_res); 94 sc->lr_bsh = rman_get_bushandle(sc->lr_mem_res); 95 96 return (0); 97} 98 99static int 100lpc_rtc_gettime(device_t dev, struct timespec *ts) 101{ 102 struct lpc_rtc_softc *sc = device_get_softc(dev); 103 104 ts->tv_sec = bus_space_read_4(sc->lr_bst, sc->lr_bsh, LPC_RTC_UCOUNT); 105 ts->tv_nsec = 0; 106 107 return (0); 108} 109 110static int 111lpc_rtc_settime(device_t dev, struct timespec *ts) 112{ 113 struct lpc_rtc_softc *sc = device_get_softc(dev); 114 uint32_t ctrl; 115 116 /* Stop RTC */ 117 ctrl = bus_space_read_4(sc->lr_bst, sc->lr_bsh, LPC_RTC_CTRL); 118 bus_space_write_4(sc->lr_bst, sc->lr_bsh, LPC_RTC_CTRL, ctrl | LPC_RTC_CTRL_DISABLE); 119 120 /* Write actual value */ 121 bus_space_write_4(sc->lr_bst, sc->lr_bsh, LPC_RTC_UCOUNT, ts->tv_sec); 122 123 /* Start RTC */ 124 ctrl = bus_space_read_4(sc->lr_bst, sc->lr_bsh, LPC_RTC_CTRL); 125 bus_space_write_4(sc->lr_bst, sc->lr_bsh, LPC_RTC_CTRL, ctrl & ~LPC_RTC_CTRL_DISABLE); 126 127 return (0); 128} 129 130static device_method_t lpc_rtc_methods[] = { 131 /* Device interface */ 132 DEVMETHOD(device_probe, lpc_rtc_probe), 133 DEVMETHOD(device_attach, lpc_rtc_attach), 134 135 /* Clock interface */ 136 DEVMETHOD(clock_gettime, lpc_rtc_gettime), 137 DEVMETHOD(clock_settime, lpc_rtc_settime), 138 139 { 0, 0 }, 140}; 141 142static driver_t lpc_rtc_driver = { 143 "rtc", 144 lpc_rtc_methods, 145 sizeof(struct lpc_rtc_softc), 146}; 147 148static devclass_t lpc_rtc_devclass; 149 150DRIVER_MODULE(rtc, simplebus, lpc_rtc_driver, lpc_rtc_devclass, 0, 0); 151