lpc_rtc.c revision 239278
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: head/sys/arm/lpc/lpc_rtc.c 239278 2012-08-15 05:37:10Z gonzo $"); 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_is_compatible(dev, "lpc,rtc")) 67 return (ENXIO); 68 69 device_set_desc(dev, "LPC32x0 real time clock"); 70 return (BUS_PROBE_DEFAULT); 71} 72 73static int 74lpc_rtc_attach(device_t dev) 75{ 76 struct lpc_rtc_softc *sc = device_get_softc(dev); 77 int rid = 0; 78 79 sc->lr_dev = dev; 80 81 clock_register(dev, 1000000); 82 83 sc->lr_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 84 RF_ACTIVE); 85 if (!sc->lr_mem_res) { 86 device_printf(dev, "cannot allocate memory window\n"); 87 return (ENXIO); 88 } 89 90 sc->lr_bst = rman_get_bustag(sc->lr_mem_res); 91 sc->lr_bsh = rman_get_bushandle(sc->lr_mem_res); 92 93 return (0); 94} 95 96static int 97lpc_rtc_gettime(device_t dev, struct timespec *ts) 98{ 99 struct lpc_rtc_softc *sc = device_get_softc(dev); 100 101 ts->tv_sec = bus_space_read_4(sc->lr_bst, sc->lr_bsh, LPC_RTC_UCOUNT); 102 ts->tv_nsec = 0; 103 104 return (0); 105} 106 107static int 108lpc_rtc_settime(device_t dev, struct timespec *ts) 109{ 110 struct lpc_rtc_softc *sc = device_get_softc(dev); 111 uint32_t ctrl; 112 113 /* Stop RTC */ 114 ctrl = bus_space_read_4(sc->lr_bst, sc->lr_bsh, LPC_RTC_CTRL); 115 bus_space_write_4(sc->lr_bst, sc->lr_bsh, LPC_RTC_CTRL, ctrl | LPC_RTC_CTRL_DISABLE); 116 117 /* Write actual value */ 118 bus_space_write_4(sc->lr_bst, sc->lr_bsh, LPC_RTC_UCOUNT, ts->tv_sec); 119 120 /* Start RTC */ 121 ctrl = bus_space_read_4(sc->lr_bst, sc->lr_bsh, LPC_RTC_CTRL); 122 bus_space_write_4(sc->lr_bst, sc->lr_bsh, LPC_RTC_CTRL, ctrl & ~LPC_RTC_CTRL_DISABLE); 123 124 return (0); 125} 126 127static device_method_t lpc_rtc_methods[] = { 128 /* Device interface */ 129 DEVMETHOD(device_probe, lpc_rtc_probe), 130 DEVMETHOD(device_attach, lpc_rtc_attach), 131 132 /* Clock interface */ 133 DEVMETHOD(clock_gettime, lpc_rtc_gettime), 134 DEVMETHOD(clock_settime, lpc_rtc_settime), 135 136 { 0, 0 }, 137}; 138 139static driver_t lpc_rtc_driver = { 140 "rtc", 141 lpc_rtc_methods, 142 sizeof(struct lpc_rtc_softc), 143}; 144 145static devclass_t lpc_rtc_devclass; 146 147DRIVER_MODULE(rtc, simplebus, lpc_rtc_driver, lpc_rtc_devclass, 0, 0); 148