Path: blob/main/sys/powerpc/mpc85xx/ds1553_bus_fdt.c
39507 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (C) 2006-2008 Semihalf, Grzegorz Bernacki4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*27* From: FreeBSD: src/sys/powerpc/mpc85xx/ds1553_bus_lbc.c,v 1.2 2009/06/24 15:48:20 raj28*/2930#include <sys/cdefs.h>3132#include <sys/param.h>33#include <sys/systm.h>34#include <sys/bus.h>35#include <sys/clock.h>36#include <sys/kernel.h>37#include <sys/lock.h>38#include <sys/module.h>39#include <sys/mutex.h>40#include <sys/resource.h>41#include <sys/rman.h>4243#include <machine/bus.h>44#include <machine/resource.h>4546#include <dev/ofw/ofw_bus_subr.h>4748#include "ds1553_reg.h"49#include "clock_if.h"5051static int rtc_attach(device_t dev);52static int rtc_probe(device_t dev);5354static device_method_t rtc_methods[] = {55/* Device interface */56DEVMETHOD(device_probe, rtc_probe),57DEVMETHOD(device_attach, rtc_attach),5859/* clock interface */60DEVMETHOD(clock_gettime, ds1553_gettime),61DEVMETHOD(clock_settime, ds1553_settime),62{ 0, 0 }63};6465static driver_t rtc_driver = {66"rtc",67rtc_methods,68sizeof(struct ds1553_softc),69};7071DRIVER_MODULE(rtc, lbc, rtc_driver, 0, 0);7273static int74rtc_probe(device_t dev)75{7677if (!ofw_bus_is_compatible(dev, "dallas,ds1553"))78return (ENXIO);7980device_set_desc(dev, "Dallas Semiconductor DS1553 RTC");81return (BUS_PROBE_DEFAULT);82}8384static int85rtc_attach(device_t dev)86{87struct timespec ts;88struct ds1553_softc *sc;89int error;9091sc = device_get_softc(dev);92bzero(sc, sizeof(struct ds1553_softc));9394mtx_init(&sc->sc_mtx, "rtc_mtx", NULL, MTX_SPIN);9596sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,97RF_ACTIVE);98if (sc->res == NULL) {99device_printf(dev, "cannot allocate resources\n");100mtx_destroy(&sc->sc_mtx);101return (ENXIO);102}103104sc->sc_bst = rman_get_bustag(sc->res);105sc->sc_bsh = rman_get_bushandle(sc->res);106107if ((error = ds1553_attach(dev)) != 0) {108device_printf(dev, "cannot attach time of day clock\n");109bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);110mtx_destroy(&sc->sc_mtx);111return (error);112}113114clock_register(dev, 1000000);115116if (bootverbose) {117ds1553_gettime(dev, &ts);118device_printf(dev, "current time: %ld.%09ld\n",119(long)ts.tv_sec, ts.tv_nsec);120}121122return (0);123}124125126