The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/arm/at91/at91_rtc.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
    3  *
    4  * Redistribution and use in source and binary forms, with or without
    5  * modification, are permitted provided that the following conditions
    6  * are met:
    7  * 1. Redistributions of source code must retain the above copyright
    8  *    notice, this list of conditions and the following disclaimer.
    9  * 2. Redistributions in binary form must reproduce the above copyright
   10  *    notice, this list of conditions and the following disclaimer in the
   11  *    documentation and/or other materials provided with the distribution.
   12  *
   13  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   16  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
   17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   23  * SUCH DAMAGE.
   24  */
   25 
   26 #include <sys/cdefs.h>
   27 __FBSDID("$FreeBSD: releng/8.1/sys/arm/at91/at91_rtc.c 185265 2008-11-25 00:13:26Z imp $");
   28 
   29 #include <sys/param.h>
   30 #include <sys/systm.h>
   31 #include <sys/bus.h>
   32 #include <sys/clock.h>
   33 #include <sys/conf.h>
   34 #include <sys/kernel.h>
   35 #include <sys/lock.h>
   36 #include <sys/mbuf.h>
   37 #include <sys/malloc.h>
   38 #include <sys/module.h>
   39 #include <sys/mutex.h>
   40 #include <sys/rman.h>
   41 #include <machine/bus.h>
   42 
   43 #include <arm/at91/at91_rtcreg.h>
   44 
   45 #include "clock_if.h"
   46 
   47 struct at91_rtc_softc
   48 {
   49         device_t dev;                   /* Myself */
   50         void *intrhand;                 /* Interrupt handle */
   51         struct resource *irq_res;       /* IRQ resource */
   52         struct resource *mem_res;       /* Memory resource */
   53         struct mtx sc_mtx;              /* basically a perimeter lock */
   54 };
   55 
   56 static inline uint32_t
   57 RD4(struct at91_rtc_softc *sc, bus_size_t off)
   58 {
   59         return bus_read_4(sc->mem_res, off);
   60 }
   61 
   62 static inline void
   63 WR4(struct at91_rtc_softc *sc, bus_size_t off, uint32_t val)
   64 {
   65         bus_write_4(sc->mem_res, off, val);
   66 }
   67 
   68 #define AT91_RTC_LOCK(_sc)              mtx_lock_spin(&(_sc)->sc_mtx)
   69 #define AT91_RTC_UNLOCK(_sc)            mtx_unlock_spin(&(_sc)->sc_mtx)
   70 #define AT91_RTC_LOCK_INIT(_sc) \
   71         mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
   72             "rtc", MTX_SPIN)
   73 #define AT91_RTC_LOCK_DESTROY(_sc)      mtx_destroy(&_sc->sc_mtx);
   74 #define AT91_RTC_ASSERT_LOCKED(_sc)     mtx_assert(&_sc->sc_mtx, MA_OWNED);
   75 #define AT91_RTC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
   76 
   77 static devclass_t at91_rtc_devclass;
   78 
   79 /* bus entry points */
   80 
   81 static int at91_rtc_probe(device_t dev);
   82 static int at91_rtc_attach(device_t dev);
   83 static int at91_rtc_detach(device_t dev);
   84 static int at91_rtc_intr(void *);
   85 
   86 /* helper routines */
   87 static int at91_rtc_activate(device_t dev);
   88 static void at91_rtc_deactivate(device_t dev);
   89 
   90 static int
   91 at91_rtc_probe(device_t dev)
   92 {
   93         device_set_desc(dev, "RTC");
   94         return (0);
   95 }
   96 
   97 static int
   98 at91_rtc_attach(device_t dev)
   99 {
  100         struct at91_rtc_softc *sc = device_get_softc(dev);
  101         int err;
  102 
  103         sc->dev = dev;
  104         err = at91_rtc_activate(dev);
  105         if (err)
  106                 goto out;
  107 
  108         AT91_RTC_LOCK_INIT(sc);
  109 
  110         /*
  111          * Activate the interrupt, but disable all interrupts in the hardware
  112          */
  113         WR4(sc, RTC_IDR, 0xffffffff);
  114         err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC,
  115             at91_rtc_intr, NULL, sc, &sc->intrhand);
  116         if (err) {
  117                 AT91_RTC_LOCK_DESTROY(sc);
  118                 goto out;
  119         }
  120         clock_register(dev, 1000000);
  121 out:;
  122         if (err)
  123                 at91_rtc_deactivate(dev);
  124         return (err);
  125 }
  126 
  127 static int
  128 at91_rtc_detach(device_t dev)
  129 {
  130         return (EBUSY); /* XXX */
  131 }
  132 
  133 static int
  134 at91_rtc_activate(device_t dev)
  135 {
  136         struct at91_rtc_softc *sc;
  137         int rid;
  138 
  139         sc = device_get_softc(dev);
  140         rid = 0;
  141         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
  142             RF_ACTIVE);
  143         if (sc->mem_res == NULL)
  144                 goto errout;
  145         rid = 0;
  146         sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
  147             RF_ACTIVE | RF_SHAREABLE);
  148         if (sc->irq_res == NULL)
  149                 goto errout;
  150         return (0);
  151 errout:
  152         at91_rtc_deactivate(dev);
  153         return (ENOMEM);
  154 }
  155 
  156 static void
  157 at91_rtc_deactivate(device_t dev)
  158 {
  159         struct at91_rtc_softc *sc;
  160 
  161         sc = device_get_softc(dev);
  162         if (sc->intrhand)
  163                 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
  164         sc->intrhand = 0;
  165         bus_generic_detach(sc->dev);
  166         if (sc->mem_res)
  167                 bus_release_resource(dev, SYS_RES_IOPORT,
  168                     rman_get_rid(sc->mem_res), sc->mem_res);
  169         sc->mem_res = 0;
  170         if (sc->irq_res)
  171                 bus_release_resource(dev, SYS_RES_IRQ,
  172                     rman_get_rid(sc->irq_res), sc->irq_res);
  173         sc->irq_res = 0;
  174         return;
  175 }
  176 
  177 static int
  178 at91_rtc_intr(void *xsc)
  179 {
  180         struct at91_rtc_softc *sc = xsc;
  181 #if 0
  182         uint32_t status;
  183 
  184         /* Reading the status also clears the interrupt */
  185         status = RD4(sc, RTC_SR);
  186         if (status == 0)
  187                 return;
  188         AT91_RTC_LOCK(sc);
  189         AT91_RTC_UNLOCK(sc);
  190 #endif
  191         wakeup(sc);
  192         return (FILTER_HANDLED);
  193 }
  194 
  195 /*
  196  * Get the time of day clock and return it in ts.
  197  * Return 0 on success, an error number otherwise.
  198  */
  199 static int
  200 at91_rtc_gettime(device_t dev, struct timespec *ts)
  201 {
  202         struct clocktime ct;
  203         uint32_t timr, calr;
  204         struct at91_rtc_softc *sc;
  205 
  206         sc = device_get_softc(dev);
  207         timr = RD4(sc, RTC_TIMR);
  208         calr = RD4(sc, RTC_CALR);
  209         ct.nsec = 0;
  210         ct.sec = RTC_TIMR_SEC(timr);
  211         ct.min = RTC_TIMR_MIN(timr);
  212         ct.hour = RTC_TIMR_HR(timr);
  213         ct.year = RTC_CALR_CEN(calr) * 100 + RTC_CALR_YEAR(calr);
  214         ct.mon = RTC_CALR_MON(calr);
  215         ct.day = RTC_CALR_DAY(calr);
  216         ct.dow = -1;
  217         return clock_ct_to_ts(&ct, ts);
  218 }
  219 
  220 /*
  221  * Set the time of day clock based on the value of the struct timespec arg.
  222  * Return 0 on success, an error number otherwise.
  223  */
  224 static int
  225 at91_rtc_settime(device_t dev, struct timespec *ts)
  226 {
  227         struct at91_rtc_softc *sc;
  228         struct clocktime ct;
  229 
  230         sc = device_get_softc(dev);
  231         clock_ts_to_ct(ts, &ct);
  232         WR4(sc, RTC_TIMR, RTC_TIMR_MK(ct.hour, ct.min, ct.sec));
  233         WR4(sc, RTC_CALR, RTC_CALR_MK(ct.year, ct.mon, ct.day, ct.dow));
  234         return (0);
  235 }
  236 
  237 static device_method_t at91_rtc_methods[] = {
  238         /* Device interface */
  239         DEVMETHOD(device_probe,         at91_rtc_probe),
  240         DEVMETHOD(device_attach,        at91_rtc_attach),
  241         DEVMETHOD(device_detach,        at91_rtc_detach),
  242 
  243         /* clock interface */
  244         DEVMETHOD(clock_gettime,        at91_rtc_gettime),
  245         DEVMETHOD(clock_settime,        at91_rtc_settime),
  246 
  247         { 0, 0 }
  248 };
  249 
  250 static driver_t at91_rtc_driver = {
  251         "at91_rtc",
  252         at91_rtc_methods,
  253         sizeof(struct at91_rtc_softc),
  254 };
  255 
  256 DRIVER_MODULE(at91_rtc, atmelarm, at91_rtc_driver, at91_rtc_devclass, 0, 0);

Cache object: a656cf3b5ecb3be9e45d3f07b90d8a36


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.