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 Mostek M48T08 or M48T18 or compatibles.
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/m48t08.h>
0046 
0047 /*
0048  *  Control register bits
0049  */
0050 
0051 #define M48T08_CONTROL_WRITE  0x80
0052 #define M48T08_CONTROL_READ   0x40
0053 #define M48T08_CONTROL_SIGN   0x20
0054 
0055 /*
0056  *  m48t08_initialize
0057  */
0058 
0059 static void m48t08_initialize(
0060   int minor
0061 )
0062 {
0063 }
0064 
0065 /*
0066  *  m48t08_get_time
0067  */
0068 
0069 #define From_BCD( _x ) ((((_x) >> 4) * 10) + ((_x) & 0x0F))
0070 #define To_BCD( _x )   ((((_x) / 10) << 4) + ((_x) % 10))
0071 
0072 static int m48t08_get_time(
0073   int                minor,
0074   rtems_time_of_day *time
0075 )
0076 {
0077   uint32_t       m48t08;
0078   getRegister_f  getReg;
0079   setRegister_f  setReg;
0080   uint8_t        controlReg;
0081   uint32_t       value1;
0082   uint32_t       value2;
0083 
0084   m48t08 = RTC_Table[ minor ].ulCtrlPort1;
0085   getReg = RTC_Table[ minor ].getRegister;
0086   setReg = RTC_Table[ minor ].setRegister;
0087 
0088   /*
0089    *  Put the RTC into read mode
0090    */
0091 
0092   controlReg = (*getReg)( m48t08, M48T08_CONTROL );
0093   (*setReg)( m48t08, M48T08_CONTROL, controlReg | M48T08_CONTROL_READ );
0094 
0095   value1 = (*getReg)( m48t08, M48T08_YEAR );
0096   value2 = From_BCD( value1 );
0097   if ( value2 < 88 )
0098     time->year = 2000 + value2;
0099   else
0100     time->year = 1900 + value2;
0101 
0102   value1 = (*getReg)( m48t08, M48T08_MONTH );
0103   time->month = From_BCD( value1 );
0104 
0105   value1 = (*getReg)( m48t08, M48T08_DATE );
0106   time->day = From_BCD( value1 );
0107 
0108   value1 = (*getReg)( m48t08, M48T08_HOUR );
0109   time->hour = From_BCD( value1 );
0110 
0111   value1 = (*getReg)( m48t08, M48T08_MINUTE );
0112   time->minute = From_BCD( value1 );
0113 
0114   value1 = (*getReg)( m48t08, M48T08_SECOND );
0115   time->second = From_BCD( value1 );
0116 
0117   time->ticks  = 0;
0118 
0119   /*
0120    *  Put the RTC back into normal mode.
0121    */
0122 
0123   (*setReg)( m48t08, M48T08_CONTROL, controlReg );
0124 
0125   return 0;
0126 }
0127 
0128 /*
0129  *  m48t08_set_time
0130  */
0131 
0132 static int m48t08_set_time(
0133   int                minor,
0134   const rtems_time_of_day *time
0135 )
0136 {
0137   uint32_t       m48t08;
0138   getRegister_f  getReg;
0139   setRegister_f  setReg;
0140   uint8_t        controlReg;
0141 
0142   m48t08 = RTC_Table[ minor ].ulCtrlPort1;
0143   getReg = RTC_Table[ minor ].getRegister;
0144   setReg = RTC_Table[ minor ].setRegister;
0145 
0146   /*
0147    *  Put the RTC into read mode
0148    */
0149 
0150   controlReg = (*getReg)( m48t08, M48T08_CONTROL );
0151   (*setReg)( m48t08, M48T08_CONTROL, controlReg | M48T08_CONTROL_WRITE );
0152 
0153   if ( time->year >= 2088 )
0154     rtems_fatal_error_occurred( RTEMS_INVALID_NUMBER );
0155 
0156   (*setReg)( m48t08, M48T08_YEAR,    To_BCD(time->year % 100) );
0157   (*setReg)( m48t08, M48T08_MONTH,   To_BCD(time->month) );
0158   (*setReg)( m48t08, M48T08_DATE,    To_BCD(time->day) );
0159   (*setReg)( m48t08, M48T08_HOUR,    To_BCD(time->hour) );
0160   (*setReg)( m48t08, M48T08_MINUTE,  To_BCD(time->minute) );
0161   (*setReg)( m48t08, M48T08_SECOND,  To_BCD(time->second) );
0162 
0163   /*
0164    *  Put the RTC back into normal mode.
0165    */
0166 
0167   (*setReg)( m48t08, M48T08_CONTROL, controlReg );
0168 
0169   return 0;
0170 }
0171 
0172 /*
0173  *  Driver function table
0174  */
0175 
0176 rtc_fns m48t08_fns = {
0177   m48t08_initialize,
0178   m48t08_get_time,
0179   m48t08_set_time
0180 };