Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:05

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  This file interfaces with the real-time clock found in
0005  *  a Harris ICM7170
0006  *
0007  *  Year 2K Notes:
0008  *
0009  *  This chip only uses a two digit field to store the year.  This
0010  *  code uses the RTEMS Epoch as a pivot year.  This lets us map the
0011  *  two digit year field as follows:
0012  *
0013  *    + two digit years 0-87 are mapped to 2000-2087.
0014  *    + two digit years 88-99 are mapped to 1988-1999.
0015  *
0016  *  This is less than the time span supported by RTEMS.
0017  *
0018  *  COPYRIGHT (c) 1989-1999.
0019  *  On-Line Applications Research Corporation (OAR).
0020  *
0021  * Redistribution and use in source and binary forms, with or without
0022  * modification, are permitted provided that the following conditions
0023  * are met:
0024  * 1. Redistributions of source code must retain the above copyright
0025  *    notice, this list of conditions and the following disclaimer.
0026  * 2. Redistributions in binary form must reproduce the above copyright
0027  *    notice, this list of conditions and the following disclaimer in the
0028  *    documentation and/or other materials provided with the distribution.
0029  *
0030  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0031  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0032  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0033  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0034  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0035  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0036  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0037  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0038  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0039  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0040  * POSSIBILITY OF SUCH DAMAGE.
0041  */
0042 
0043 #include <rtems.h>
0044 #include <libchip/rtc.h>
0045 #include <libchip/icm7170.h>
0046 
0047 /*
0048  *  Control register bits
0049  */
0050 
0051 /* XXX */
0052 
0053 /*
0054  *  icm7170_initialize
0055  */
0056 
0057 static void icm7170_initialize(
0058   int minor
0059 )
0060 {
0061   uintptr_t      icm7170;
0062   setRegister_f  setReg;
0063   uintptr_t      clock;
0064 
0065   icm7170 = RTC_Table[ minor ].ulCtrlPort1;
0066   setReg = RTC_Table[ minor ].setRegister;
0067 
0068   /*
0069    *  Initialize the RTC with the proper clock frequency
0070    */
0071 
0072   clock = (uintptr_t) RTC_Table[ minor ].pDeviceParams;
0073   (*setReg)( icm7170, ICM7170_CONTROL, 0x0c | clock );
0074 }
0075 
0076 /*
0077  *  icm7170_get_time
0078  */
0079 
0080 static int icm7170_get_time(
0081   int                minor,
0082   rtems_time_of_day *time
0083 )
0084 {
0085   uint32_t       icm7170;
0086   getRegister_f  getReg;
0087   uint32_t       year;
0088 
0089   icm7170 = RTC_Table[ minor ].ulCtrlPort1;
0090   getReg = RTC_Table[ minor ].getRegister;
0091 
0092   /*
0093    *  Put the RTC into read mode
0094    */
0095 
0096   (void) (*getReg)( icm7170, ICM7170_COUNTER_HUNDREDTHS );
0097 
0098   /*
0099    *  Now get the time
0100    */
0101 
0102 
0103   year = (*getReg)( icm7170, ICM7170_YEAR );
0104   if ( year < 88 )
0105     year += 2000;
0106   else
0107     year += 1900;
0108 
0109   time->year   = year;
0110   time->month  = (*getReg)( icm7170, ICM7170_MONTH );
0111   time->day    = (*getReg)( icm7170, ICM7170_DATE );
0112   time->hour   = (*getReg)( icm7170, ICM7170_HOUR );
0113   time->minute = (*getReg)( icm7170, ICM7170_MINUTE );
0114   time->second = (*getReg)( icm7170, ICM7170_SECOND );
0115 
0116   time->ticks  = 0;
0117 
0118   /*
0119    *  Put the RTC back into normal mode.
0120    */
0121 
0122   (void) (*getReg)( icm7170, ICM7170_COUNTER_HUNDREDTHS );
0123 
0124   return 0;
0125 }
0126 
0127 /*
0128  *  icm7170_set_time
0129  */
0130 
0131 static int icm7170_set_time(
0132   int                minor,
0133   const rtems_time_of_day *time
0134 )
0135 {
0136   uintptr_t      icm7170;
0137   setRegister_f  setReg;
0138   uint32_t       year;
0139   uintptr_t      clock;
0140 
0141   icm7170 = RTC_Table[ minor ].ulCtrlPort1;
0142   setReg = RTC_Table[ minor ].setRegister;
0143   clock = (uintptr_t) RTC_Table[ minor ].pDeviceParams;
0144 
0145   year = time->year;
0146 
0147   if ( year >= 2088 )
0148     rtems_fatal_error_occurred( RTEMS_INVALID_NUMBER );
0149 
0150   if ( year >= 2000 )
0151     year -= 2000;
0152   else
0153     year -= 1900;
0154 
0155   (*setReg)( icm7170, ICM7170_CONTROL, 0x04 | clock );
0156 
0157   (*setReg)( icm7170, ICM7170_YEAR,    year );
0158   (*setReg)( icm7170, ICM7170_MONTH,   time->month );
0159   (*setReg)( icm7170, ICM7170_DATE,    time->day );
0160   (*setReg)( icm7170, ICM7170_HOUR,    time->hour );
0161   (*setReg)( icm7170, ICM7170_MINUTE,  time->minute );
0162   (*setReg)( icm7170, ICM7170_SECOND,  time->second );
0163 
0164   /*
0165    *  This is not really right.
0166    */
0167 
0168   (*setReg)( icm7170, ICM7170_DAY_OF_WEEK,  1 );
0169 
0170   /*
0171    *  Put the RTC back into normal mode.
0172    */
0173 
0174   (*setReg)( icm7170, ICM7170_CONTROL, 0x0c | clock );
0175 
0176   return 0;
0177 }
0178 
0179 /*
0180  *  Driver function table
0181  */
0182 
0183 rtc_fns icm7170_fns = {
0184   icm7170_initialize,
0185   icm7170_get_time,
0186   icm7170_set_time
0187 };