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/mips/atheros/pcf2123_rtc.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2010, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice unmodified, this list of conditions, and the following
   12  *    disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  */
   29 
   30 #include <sys/cdefs.h>
   31 __FBSDID("$FreeBSD: stable/12/sys/mips/atheros/pcf2123_rtc.c 326259 2017-11-27 15:07:26Z pfg $");
   32 
   33 #include <sys/param.h>
   34 #include <sys/bus.h>
   35 #include <sys/lock.h>
   36 #include <sys/time.h>
   37 #include <sys/clock.h>
   38 #include <sys/resource.h>
   39 #include <sys/systm.h>
   40 #include <sys/rman.h>
   41 #include <sys/kernel.h>
   42 #include <sys/module.h>
   43 
   44 #include <mips/atheros/pcf2123reg.h>
   45 
   46 #include <dev/spibus/spi.h>
   47 #include "spibus_if.h"
   48 
   49 #include "clock_if.h"
   50 
   51 #define YEAR_BASE       1970
   52 #define PCF2123_DELAY   50
   53 
   54 struct pcf2123_rtc_softc {
   55         device_t dev;
   56 };
   57 
   58 static int pcf2123_rtc_probe(device_t dev);
   59 static int pcf2123_rtc_attach(device_t dev);
   60 
   61 static int pcf2123_rtc_gettime(device_t dev, struct timespec *ts);
   62 static int pcf2123_rtc_settime(device_t dev, struct timespec *ts);
   63 
   64 static int
   65 pcf2123_rtc_probe(device_t dev)
   66 {
   67 
   68         device_set_desc(dev, "PCF2123 SPI RTC");
   69         return (0);
   70 }
   71 
   72 static int
   73 pcf2123_rtc_attach(device_t dev)
   74 {
   75         struct pcf2123_rtc_softc *sc;
   76         struct spi_command cmd;
   77         unsigned char rxBuf[3];
   78         unsigned char txBuf[3];
   79         int err;
   80 
   81         sc = device_get_softc(dev);
   82         sc->dev = dev;
   83 
   84         clock_register(dev, 1000000);
   85 
   86         memset(&cmd, 0, sizeof(cmd));
   87         memset(rxBuf, 0, sizeof(rxBuf));
   88         memset(txBuf, 0, sizeof(txBuf));
   89 
   90         /* Make sure Ctrl1 and Ctrl2 are zeroes */
   91         txBuf[0] = PCF2123_WRITE(PCF2123_REG_CTRL1);
   92         cmd.rx_cmd = rxBuf;
   93         cmd.tx_cmd = txBuf;
   94         cmd.rx_cmd_sz = sizeof(rxBuf);
   95         cmd.tx_cmd_sz = sizeof(txBuf);
   96         err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
   97         DELAY(PCF2123_DELAY);
   98 
   99         return (0);
  100 }
  101 
  102 static int
  103 pcf2123_rtc_gettime(device_t dev, struct timespec *ts)
  104 {
  105         struct clocktime ct;
  106         struct spi_command cmd;
  107         unsigned char rxTimedate[8];
  108         unsigned char txTimedate[8];
  109         int err;
  110 
  111         memset(&cmd, 0, sizeof(cmd));
  112         memset(rxTimedate, 0, sizeof(rxTimedate));
  113         memset(txTimedate, 0, sizeof(txTimedate));
  114 
  115         /*
  116          * Counter is stopped when access to time registers is in progress
  117          * So there is no need to stop/start counter
  118          */
  119         /* Start reading from seconds */
  120         txTimedate[0] = PCF2123_READ(PCF2123_REG_SECONDS);
  121         cmd.rx_cmd = rxTimedate;
  122         cmd.tx_cmd = txTimedate;
  123         cmd.rx_cmd_sz = sizeof(rxTimedate);
  124         cmd.tx_cmd_sz = sizeof(txTimedate);
  125         err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
  126         DELAY(PCF2123_DELAY);
  127 
  128         ct.nsec = 0;
  129         ct.sec = FROMBCD(rxTimedate[1] & 0x7f);
  130         ct.min = FROMBCD(rxTimedate[2] & 0x7f);
  131         ct.hour = FROMBCD(rxTimedate[3] & 0x3f);
  132 
  133         ct.dow = FROMBCD(rxTimedate[5] & 0x3f);
  134 
  135         ct.day = FROMBCD(rxTimedate[4] & 0x3f);
  136         ct.mon = FROMBCD(rxTimedate[6] & 0x1f);
  137         ct.year = YEAR_BASE + FROMBCD(rxTimedate[7]);
  138 
  139         return (clock_ct_to_ts(&ct, ts));
  140 }
  141 
  142 static int
  143 pcf2123_rtc_settime(device_t dev, struct timespec *ts)
  144 {
  145         struct clocktime ct;
  146         struct pcf2123_rtc_softc *sc;
  147         struct spi_command cmd;
  148         unsigned char rxTimedate[8];
  149         unsigned char txTimedate[8];
  150         int err;
  151 
  152         sc = device_get_softc(dev);
  153 
  154         /* Resolution: 1 sec */
  155         if (ts->tv_nsec >= 500000000)
  156                 ts->tv_sec++;
  157         ts->tv_nsec = 0;
  158         clock_ts_to_ct(ts, &ct);
  159 
  160         memset(&cmd, 0, sizeof(cmd));
  161         memset(rxTimedate, 0, sizeof(rxTimedate));
  162         memset(txTimedate, 0, sizeof(txTimedate));
  163 
  164         /* Start reading from seconds */
  165         cmd.rx_cmd = rxTimedate;
  166         cmd.tx_cmd = txTimedate;
  167         cmd.rx_cmd_sz = sizeof(rxTimedate);
  168         cmd.tx_cmd_sz = sizeof(txTimedate);
  169 
  170         /*
  171          * Counter is stopped when access to time registers is in progress
  172          * So there is no need to stop/start counter
  173          */
  174         txTimedate[0] = PCF2123_WRITE(PCF2123_REG_SECONDS);
  175         txTimedate[1] = TOBCD(ct.sec);
  176         txTimedate[2] = TOBCD(ct.min);
  177         txTimedate[3] = TOBCD(ct.hour);
  178         txTimedate[4] = TOBCD(ct.day);
  179         txTimedate[5] = TOBCD(ct.dow);
  180         txTimedate[6] = TOBCD(ct.mon);
  181         txTimedate[7] = TOBCD(ct.year - YEAR_BASE);
  182 
  183         err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
  184         DELAY(PCF2123_DELAY);
  185 
  186         return (err);
  187 }
  188 
  189 static device_method_t pcf2123_rtc_methods[] = {
  190         DEVMETHOD(device_probe,         pcf2123_rtc_probe),
  191         DEVMETHOD(device_attach,        pcf2123_rtc_attach),
  192 
  193         DEVMETHOD(clock_gettime,        pcf2123_rtc_gettime),
  194         DEVMETHOD(clock_settime,        pcf2123_rtc_settime),
  195 
  196         { 0, 0 },
  197 };
  198 
  199 static driver_t pcf2123_rtc_driver = {
  200         "rtc",
  201         pcf2123_rtc_methods,
  202         sizeof(struct pcf2123_rtc_softc),
  203 };
  204 static devclass_t pcf2123_rtc_devclass;
  205 
  206 DRIVER_MODULE(pcf2123_rtc, spibus, pcf2123_rtc_driver, pcf2123_rtc_devclass, 0, 0);

Cache object: 87a40ce2e4757c1c9a64af4ce366c31e


[ 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.