]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - common/cmd_date.c
sniper: Serial number support, obtained from die ID
[karo-tx-uboot.git] / common / cmd_date.c
index 7b7f547ddb31b44a9b460673fb8dc3e0ebf4c69f..87146996216a6561c8927d5cfed19cfb724a7c1e 100644 (file)
@@ -2,23 +2,7 @@
  * (C) Copyright 2001
  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
 /*
  */
 #include <common.h>
 #include <command.h>
+#include <dm.h>
 #include <rtc.h>
 #include <i2c.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
-#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_CMD_DATE)
-
-const char *weekdays[] = {
+static const char * const weekdays[] = {
        "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
 };
 
+#ifdef CONFIG_NEEDS_MANUAL_RELOC
 #define RELOC(a)       ((typeof(a))((unsigned long)(a) + gd->reloc_off))
+#else
+#define RELOC(a)       a
+#endif
+
+int mk_date (const char *, struct rtc_time *);
 
-int mk_date (char *, struct rtc_time *);
+static struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 };
 
-int do_date (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
+static int do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
        struct rtc_time tm;
        int rcode = 0;
-       int old_bus;
+       int old_bus __maybe_unused;
 
        /* switch to correct I2C bus */
+#ifdef CONFIG_DM_RTC
+       struct udevice *dev;
+
+       rcode = uclass_get_device(UCLASS_RTC, 0, &dev);
+       if (rcode) {
+               printf("Cannot find RTC: err=%d\n", rcode);
+               return CMD_RET_FAILURE;
+       }
+#elif defined(CONFIG_SYS_I2C)
+       old_bus = i2c_get_bus_num();
+       i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM);
+#else
        old_bus = I2C_GET_BUS();
-       I2C_SET_BUS(CFG_RTC_BUS_NUM);
+       I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
+#endif
 
        switch (argc) {
        case 2:                 /* set date & time */
                if (strcmp(argv[1],"reset") == 0) {
                        puts ("Reset RTC...\n");
-                       rtc_reset ();
+#ifdef CONFIG_DM_RTC
+                       rcode = dm_rtc_reset(dev);
+                       if (!rcode)
+                               rcode = dm_rtc_set(dev, &default_tm);
+#else
+                       rtc_reset();
+                       rcode = rtc_set(&default_tm);
+#endif
+                       if (rcode)
+                               puts("## Failed to set date after RTC reset\n");
                } else {
                        /* initialize tm with current time */
-                       rtc_get (&tm);
-                       /* insert new date & time */
-                       if (mk_date (argv[1], &tm) != 0) {
-                               puts ("## Bad date format\n");
-                               break;
+#ifdef CONFIG_DM_RTC
+                       rcode = dm_rtc_get(dev, &tm);
+#else
+                       rcode = rtc_get(&tm);
+#endif
+                       if (!rcode) {
+                               /* insert new date & time */
+                               if (mk_date(argv[1], &tm) != 0) {
+                                       puts ("## Bad date format\n");
+                                       break;
+                               }
+                               /* and write to RTC */
+#ifdef CONFIG_DM_RTC
+                               rcode = dm_rtc_set(dev, &tm);
+#else
+                               rcode = rtc_set(&tm);
+#endif
+                               if (rcode) {
+                                       printf("## Set date failed: err=%d\n",
+                                              rcode);
+                               }
+                       } else {
+                               puts("## Get date failed\n");
                        }
-                       /* and write to RTC */
-                       rtc_set (&tm);
                }
                /* FALL TROUGH */
        case 1:                 /* get date & time */
-               rtc_get (&tm);
+#ifdef CONFIG_DM_RTC
+               rcode = dm_rtc_get(dev, &tm);
+#else
+               rcode = rtc_get(&tm);
+#endif
+               if (rcode) {
+                       puts("## Get date failed\n");
+                       break;
+               }
 
                printf ("Date: %4d-%02d-%02d (%sday)    Time: %2d:%02d:%02d\n",
                        tm.tm_year, tm.tm_mon, tm.tm_mday,
@@ -79,20 +114,23 @@ int do_date (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 
                break;
        default:
-               printf ("Usage:\n%s\n", cmdtp->usage);
-               rcode = 1;
+               rcode = CMD_RET_USAGE;
        }
 
        /* switch back to original I2C bus */
+#ifdef CONFIG_SYS_I2C
+       i2c_set_bus_num(old_bus);
+#elif !defined(CONFIG_DM_RTC)
        I2C_SET_BUS(old_bus);
+#endif
 
-       return rcode;
+       return rcode ? CMD_RET_FAILURE : 0;
 }
 
 /*
  * simple conversion of two-digit string with error checking
  */
-static int cnvrt2 (char *str, int *valp)
+static int cnvrt2 (const char *str, int *valp)
 {
        int val;
 
@@ -117,7 +155,7 @@ static int cnvrt2 (char *str, int *valp)
  * Some basic checking for valid values is done, but this will not catch
  * all possible error conditions.
  */
-int mk_date (char *datestr, struct rtc_time *tmp)
+int mk_date (const char *datestr, struct rtc_time *tmp)
 {
        int len, val;
        char *ptr;
@@ -190,7 +228,7 @@ int mk_date (char *datestr, struct rtc_time *tmp)
                tmp->tm_min  = val;
 
                /* calculate day of week */
-               GregorianDay (tmp);
+               rtc_calc_weekday(tmp);
 
                return (0);
        default:
@@ -204,11 +242,9 @@ int mk_date (char *datestr, struct rtc_time *tmp)
 
 U_BOOT_CMD(
        date,   2,      1,      do_date,
-       "date    - get/set/reset date & time\n",
+       "get/set/reset date & time",
        "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
        "  - without arguments: print date & time\n"
        "  - with numeric argument: set the system date & time\n"
-       "  - with 'reset' argument: reset the RTC\n"
+       "  - with 'reset' argument: reset the RTC"
 );
-
-#endif /* CFG_CMD_DATE */