[U-Boot] [PATCH 10/33] rtc: mc146818: Set up RTC at start of day

Bin Meng bmeng.cn at gmail.com
Wed Nov 12 08:02:03 CET 2014


Hi Simon,

On Wed, Nov 12, 2014 at 8:17 AM, Simon Glass <sjg at chromium.org> wrote:
> If the RTC needs to be cleared, write the U-Boot build date to it. In any
> case make sure the settings are correct.
>
> Signed-off-by: Simon Glass <sjg at chromium.org>
> ---
>
>  drivers/rtc/mc146818.c | 49 +++++++++++++++++++++++++++++++++++++++++++++----
>  include/rtc.h          |  7 +++++++
>  2 files changed, 52 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/rtc/mc146818.c b/drivers/rtc/mc146818.c
> index f7cf106..7528044 100644
> --- a/drivers/rtc/mc146818.c
> +++ b/drivers/rtc/mc146818.c
> @@ -14,6 +14,7 @@
>  #include <common.h>
>  #include <command.h>
>  #include <rtc.h>
> +#include <version.h>
>
>  #if defined(__I386__) || defined(CONFIG_MALTA)
>  #include <asm/io.h>
> @@ -42,6 +43,12 @@ static void  rtc_write (uchar reg, uchar val);
>  #define RTC_CONFIG_C           0x0C
>  #define RTC_CONFIG_D           0x0D
>
> +#define RTC_CONFIG_A_REF_CLCK_32KHZ    (1 << 5)
> +#define RTC_CONFIG_A_RATE_1024HZ       6
> +
> +#define RTC_CONFIG_B_24H               (1 << 1)
> +
> +#define RTC_CONFIG_D_VALID_RAM_AND_TIME        0x80
>
>  /* ------------------------------------------------------------------------- */
>
> @@ -128,25 +135,59 @@ void rtc_reset (void)
>   */
>  static uchar rtc_read (uchar reg)
>  {
> -       return(in8(CONFIG_SYS_RTC_REG_BASE_ADDR+reg));
> +       return in8(CONFIG_SYS_RTC_REG_BASE_ADDR + reg);
>  }
>
>  static void rtc_write (uchar reg, uchar val)
>  {
> -       out8(CONFIG_SYS_RTC_REG_BASE_ADDR+reg, val);
> +       out8(CONFIG_SYS_RTC_REG_BASE_ADDR + reg, val);
>  }
>  #else
>  static uchar rtc_read (uchar reg)
>  {
>         out8(RTC_PORT_MC146818,reg);
> -       return(in8(RTC_PORT_MC146818+1));
> +       return in8(RTC_PORT_MC146818 + 1);
>  }
>
>  static void rtc_write (uchar reg, uchar val)
>  {
>         out8(RTC_PORT_MC146818,reg);
> -       out8(RTC_PORT_MC146818+1,val);
> +       out8(RTC_PORT_MC146818+1, val);
>  }
>  #endif
>
> +void rtc_init(bool invalid)
> +{
> +       debug("RTC Init\n");
> +
> +#define CLEAR_CMOS 0
> +#else
> +#define CLEAR_CMOS 1
>  #endif

It would be better if we change this #if #else like below which should
help better understanding.

#if defined(CONFIG_CMD_DATE)
#define CLEAR_CMOS 0
#else
#define CLEAR_CMOS 1
#endif

And put the above logic to the head of this file, or maybe
include/rtc.h if we want to make it generic for other RTC drivers as
well.

> +
> +       if (invalid) {
> +#if CLEAR_CMOS
> +               int i;
> +
> +               rtc_write(0x01, 0);
> +               rtc_write(0x03, 0);
> +               rtc_write(0x05, 0);

Please replace these 01/03/05 with macros.

> +               for (i = 10; i < 128; i++)
> +                       rtc_write(i, 0);
> +#endif
> +               printf("RTC:%s%s\n",
> +                      invalid ? " Clear requested" : "",
> +                      CLEAR_CMOS ? " zeroing cmos" : "");
> +       }
> +
> +       /* Setup the real time clock */
> +       rtc_write(RTC_CONFIG_B, RTC_CONFIG_B_24H);
> +       /* Setup the frequency it operates at */
> +       rtc_write(RTC_CONFIG_A, RTC_CONFIG_A_REF_CLCK_32KHZ |
> +                 RTC_CONFIG_A_RATE_1024HZ);
> +       /* Ensure all reserved bits are 0 in register D */
> +       rtc_write(RTC_CONFIG_D, RTC_CONFIG_D_VALID_RAM_AND_TIME);
> +
> +       /* Clear any pending interrupts */
> +       rtc_read(RTC_CONFIG_C);
> +}
> diff --git a/include/rtc.h b/include/rtc.h
> index c034966..8be06bb 100644
> --- a/include/rtc.h
> +++ b/include/rtc.h
> @@ -50,4 +50,11 @@ void to_tm (int, struct rtc_time *);
>  unsigned long mktime (unsigned int, unsigned int, unsigned int,
>                       unsigned int, unsigned int, unsigned int);
>
> +/**
> + * rtc_init() - Set up the real time clock ready for use
> + *
> + * @rtc_failed:        true if time was lost (e.g. flat battery), else false
> + */
> +void rtc_init(bool rtc_failed);
> +
>  #endif /* _RTC_H_ */
> --

This adds an API in the generic RTC driver header, however there are
some RTC drivers which already have the API but without any
parameters.

arch/mips/include/asm/jz4740.h:1147:extern void rtc_init(void);
board/cogent/mb.c:268:  rtc_init ();
drivers/rtc/bfin_rtc.c:30:static void rtc_init(void)
drivers/rtc/pl031.c:42:void rtc_init(void)
drivers/rtc/ds1302.c:192:rtc_init(void)

Do you plan to update other RTC drivers as well? Or if we want to keep
this x86 specific, maybe a CONFIG_X86 is needed?

Regards,
Bin


More information about the U-Boot mailing list