Replace BCD2BIN and BIN2BCD macros with inline functions
authorAlbin Tonnerre <albin.tonnerre@free-electrons.com>
Thu, 13 Aug 2009 13:31:11 +0000 (15:31 +0200)
committerWolfgang Denk <wd@denx.de>
Tue, 25 Aug 2009 10:57:55 +0000 (12:57 +0200)
In the process, also remove backward-compatiblity macros BIN_TO_BCD and
BCD_TO_BIN and update the sole board using them to use the new bin2bcd
and bcd2bin instead

Signed-off-by: Albin Tonnerre <albin.tonnerre@free-electrons.com>
Acked-by: Stefan Roese <sr@denx.de>
Acked-by: Detlev Zundel <dzu@denx.de>
board/rsdproto/rsdproto.c
drivers/rtc/m41t62.c
drivers/rtc/rtc4543.c
drivers/rtc/s3c44b0_rtc.c
drivers/rtc/x1205.c
include/bcd.h
include/linux/mc146818rtc.h

index 26edb2e4f8cfbf3696058f01365748f735bffc9c..10759b79ca398034d57bd499e61d788aa540f527 100644 (file)
@@ -26,6 +26,7 @@
 #include <ioports.h>
 #include <mpc8260.h>
 #include <i2c.h>
+#include <bcd.h>
 
 /* define to initialise the SDRAM on the local bus */
 #undef INIT_LOCAL_BUS_SDRAM
@@ -208,16 +209,14 @@ void read_RS5C372_time (struct tm *timedate)
 {
        unsigned char buffer[8];
 
-#define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10)
-
        if (! i2c_read (RS5C372_PPC_I2C_ADR, 0, 1, buffer, sizeof (buffer))) {
-               timedate->tm_sec = BCD_TO_BIN (buffer[0]);
-               timedate->tm_min = BCD_TO_BIN (buffer[1]);
-               timedate->tm_hour = BCD_TO_BIN (buffer[2]);
-               timedate->tm_wday = BCD_TO_BIN (buffer[3]);
-               timedate->tm_mday = BCD_TO_BIN (buffer[4]);
-               timedate->tm_mon = BCD_TO_BIN (buffer[5]);
-               timedate->tm_year = BCD_TO_BIN (buffer[6]) + 2000;
+               timedate->tm_sec = bcd2bin (buffer[0]);
+               timedate->tm_min = bcd2bin (buffer[1]);
+               timedate->tm_hour = bcd2bin (buffer[2]);
+               timedate->tm_wday = bcd2bin (buffer[3]);
+               timedate->tm_mday = bcd2bin (buffer[4]);
+               timedate->tm_mon = bcd2bin (buffer[5]);
+               timedate->tm_year = bcd2bin (buffer[6]) + 2000;
        } else {
                /*printf("i2c error %02x\n", rc); */
                memset (timedate, 0, sizeof (struct tm));
index cfe84f926be3e4f51170c0ae3db43093043f15d5..3d7bb46b0e63d02d611e3b618d69dca01bba4599 100644 (file)
@@ -76,16 +76,16 @@ int rtc_get(struct rtc_time *tm)
              buf[0], buf[1], buf[2], buf[3],
              buf[4], buf[5], buf[6], buf[7]);
 
-       tm->tm_sec = BCD2BIN(buf[M41T62_REG_SEC] & 0x7f);
-       tm->tm_min = BCD2BIN(buf[M41T62_REG_MIN] & 0x7f);
-       tm->tm_hour = BCD2BIN(buf[M41T62_REG_HOUR] & 0x3f);
-       tm->tm_mday = BCD2BIN(buf[M41T62_REG_DAY] & 0x3f);
+       tm->tm_sec = bcd2bin(buf[M41T62_REG_SEC] & 0x7f);
+       tm->tm_min = bcd2bin(buf[M41T62_REG_MIN] & 0x7f);
+       tm->tm_hour = bcd2bin(buf[M41T62_REG_HOUR] & 0x3f);
+       tm->tm_mday = bcd2bin(buf[M41T62_REG_DAY] & 0x3f);
        tm->tm_wday = buf[M41T62_REG_WDAY] & 0x07;
-       tm->tm_mon = BCD2BIN(buf[M41T62_REG_MON] & 0x1f);
+       tm->tm_mon = bcd2bin(buf[M41T62_REG_MON] & 0x1f);
 
        /* assume 20YY not 19YY, and ignore the Century Bit */
        /* U-Boot needs to add 1900 here */
-       tm->tm_year = BCD2BIN(buf[M41T62_REG_YEAR]) + 100 + 1900;
+       tm->tm_year = bcd2bin(buf[M41T62_REG_YEAR]) + 100 + 1900;
 
        debug("%s: tm is secs=%d, mins=%d, hours=%d, "
              "mday=%d, mon=%d, year=%d, wday=%d\n",
@@ -109,19 +109,19 @@ int rtc_set(struct rtc_time *tm)
        /* Merge time-data and register flags into buf[0..7] */
        buf[M41T62_REG_SSEC] = 0;
        buf[M41T62_REG_SEC] =
-               BIN2BCD(tm->tm_sec) | (buf[M41T62_REG_SEC] & ~0x7f);
+               bin2bcd(tm->tm_sec) | (buf[M41T62_REG_SEC] & ~0x7f);
        buf[M41T62_REG_MIN] =
-               BIN2BCD(tm->tm_min) | (buf[M41T62_REG_MIN] & ~0x7f);
+               bin2bcd(tm->tm_min) | (buf[M41T62_REG_MIN] & ~0x7f);
        buf[M41T62_REG_HOUR] =
-               BIN2BCD(tm->tm_hour) | (buf[M41T62_REG_HOUR] & ~0x3f) ;
+               bin2bcd(tm->tm_hour) | (buf[M41T62_REG_HOUR] & ~0x3f) ;
        buf[M41T62_REG_WDAY] =
                (tm->tm_wday & 0x07) | (buf[M41T62_REG_WDAY] & ~0x07);
        buf[M41T62_REG_DAY] =
-               BIN2BCD(tm->tm_mday) | (buf[M41T62_REG_DAY] & ~0x3f);
+               bin2bcd(tm->tm_mday) | (buf[M41T62_REG_DAY] & ~0x3f);
        buf[M41T62_REG_MON] =
-               BIN2BCD(tm->tm_mon) | (buf[M41T62_REG_MON] & ~0x1f);
+               bin2bcd(tm->tm_mon) | (buf[M41T62_REG_MON] & ~0x1f);
        /* assume 20YY not 19YY */
-       buf[M41T62_REG_YEAR] = BIN2BCD(tm->tm_year % 100);
+       buf[M41T62_REG_YEAR] = bin2bcd(tm->tm_year % 100);
 
        if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, buf, M41T62_DATETIME_REG_SIZE)) {
                printf("I2C write failed in %s()\n", __func__);
index 242d9bcad79b09c9d8639d5761f2a8efca5b4af8..b60e37d5be7a9cb10b3d9e1959f2de06fbf9573b 100644 (file)
@@ -49,13 +49,13 @@ int rtc_get(struct rtc_time *tm)
        /* Read 52 bits into our buffer */
        tws_read(buffer, 52);
 
-       tm->tm_sec  = BCD2BIN( buffer[0] & 0x7F);
-       tm->tm_min  = BCD2BIN( buffer[1] & 0x7F);
-       tm->tm_hour = BCD2BIN( buffer[2] & 0x3F);
-       tm->tm_wday = BCD2BIN( buffer[3] & 0x07);
-       tm->tm_mday = BCD2BIN((buffer[3] & 0xF0) >> 4 | (buffer[4] & 0x0F) << 4);
-       tm->tm_mon  = BCD2BIN((buffer[4] & 0x30) >> 4 | (buffer[5] & 0x0F) << 4);
-       tm->tm_year = BCD2BIN((buffer[5] & 0xF0) >> 4 | (buffer[6] & 0x0F) << 4) + 2000;
+       tm->tm_sec  = bcd2bin( buffer[0] & 0x7F);
+       tm->tm_min  = bcd2bin( buffer[1] & 0x7F);
+       tm->tm_hour = bcd2bin( buffer[2] & 0x3F);
+       tm->tm_wday = bcd2bin( buffer[3] & 0x07);
+       tm->tm_mday = bcd2bin((buffer[3] & 0xF0) >> 4 | (buffer[4] & 0x0F) << 4);
+       tm->tm_mon  = bcd2bin((buffer[4] & 0x30) >> 4 | (buffer[5] & 0x0F) << 4);
+       tm->tm_year = bcd2bin((buffer[5] & 0xF0) >> 4 | (buffer[6] & 0x0F) << 4) + 2000;
        tm->tm_yday = 0;
        tm->tm_isdst = 0;
 
@@ -81,17 +81,17 @@ int rtc_set(struct rtc_time *tm)
                tm->tm_hour, tm->tm_min, tm->tm_sec);
 
        memset(buffer, 0, 7);
-       buffer[0] = BIN2BCD(tm->tm_sec);
-       buffer[1] = BIN2BCD(tm->tm_min);
-       buffer[2] = BIN2BCD(tm->tm_hour);
-       buffer[3] = BIN2BCD(tm->tm_wday);
-       tmp = BIN2BCD(tm->tm_mday);
+       buffer[0] = bin2bcd(tm->tm_sec);
+       buffer[1] = bin2bcd(tm->tm_min);
+       buffer[2] = bin2bcd(tm->tm_hour);
+       buffer[3] = bin2bcd(tm->tm_wday);
+       tmp = bin2bcd(tm->tm_mday);
        buffer[3] |= (tmp & 0x0F) << 4;
        buffer[4] =  (tmp & 0xF0) >> 4;
-       tmp = BIN2BCD(tm->tm_mon);
+       tmp = bin2bcd(tm->tm_mon);
        buffer[4] |= (tmp & 0x0F) << 4;
        buffer[5] =  (tmp & 0xF0) >> 4;
-       tmp = BIN2BCD(tm->tm_year  % 100);
+       tmp = bin2bcd(tm->tm_year  % 100);
        buffer[5] |= (tmp & 0x0F) << 4;
        buffer[6] =  (tmp & 0xF0) >> 4;
 
index bfb744aef14ba0653b82c139858b750dd0c52c38..a027fb1263bd309d9093ee40936bde4a8d0f41ea 100644 (file)
 int rtc_get (struct rtc_time* tm)
 {
        RTCCON |= 1;
-       tm->tm_year  = BCD2BIN(BCDYEAR);
-       tm->tm_mon   = BCD2BIN(BCDMON);
-       tm->tm_wday   = BCD2BIN(BCDDATE);
-       tm->tm_mday   = BCD2BIN(BCDDAY);
-       tm->tm_hour  = BCD2BIN(BCDHOUR);
-       tm->tm_min  = BCD2BIN(BCDMIN);
-       tm->tm_sec  = BCD2BIN(BCDSEC);
+       tm->tm_year  = bcd2bin(BCDYEAR);
+       tm->tm_mon   = bcd2bin(BCDMON);
+       tm->tm_wday   = bcd2bin(BCDDATE);
+       tm->tm_mday   = bcd2bin(BCDDAY);
+       tm->tm_hour  = bcd2bin(BCDHOUR);
+       tm->tm_min  = bcd2bin(BCDMIN);
+       tm->tm_sec  = bcd2bin(BCDSEC);
 
        if (tm->tm_sec==0) {
                /* we have to re-read the rtc data because of the "one second deviation" problem */
                /* see RTC datasheet for more info about it */
-               tm->tm_year  = BCD2BIN(BCDYEAR);
-               tm->tm_mon   = BCD2BIN(BCDMON);
-               tm->tm_mday   = BCD2BIN(BCDDAY);
-               tm->tm_wday   = BCD2BIN(BCDDATE);
-               tm->tm_hour  = BCD2BIN(BCDHOUR);
-               tm->tm_min  = BCD2BIN(BCDMIN);
-               tm->tm_sec  = BCD2BIN(BCDSEC);
+               tm->tm_year  = bcd2bin(BCDYEAR);
+               tm->tm_mon   = bcd2bin(BCDMON);
+               tm->tm_mday   = bcd2bin(BCDDAY);
+               tm->tm_wday   = bcd2bin(BCDDATE);
+               tm->tm_hour  = bcd2bin(BCDHOUR);
+               tm->tm_min  = bcd2bin(BCDMIN);
+               tm->tm_sec  = bcd2bin(BCDSEC);
        }
 
        RTCCON &= ~1;
@@ -75,13 +75,13 @@ int rtc_set (struct rtc_time* tm)
                tm->tm_year -= 2000;
 
        RTCCON |= 1;
-       BCDYEAR = BIN2BCD(tm->tm_year);
-       BCDMON = BIN2BCD(tm->tm_mon);
-       BCDDAY = BIN2BCD(tm->tm_mday);
-       BCDDATE = BIN2BCD(tm->tm_wday);
-       BCDHOUR = BIN2BCD(tm->tm_hour);
-       BCDMIN = BIN2BCD(tm->tm_min);
-       BCDSEC = BIN2BCD(tm->tm_sec);
+       BCDYEAR = bin2bcd(tm->tm_year);
+       BCDMON = bin2bcd(tm->tm_mon);
+       BCDDAY = bin2bcd(tm->tm_mday);
+       BCDDATE = bin2bcd(tm->tm_wday);
+       BCDHOUR = bin2bcd(tm->tm_hour);
+       BCDMIN = bin2bcd(tm->tm_min);
+       BCDSEC = bin2bcd(tm->tm_sec);
        RTCCON &= 1;
 
        return 0;
index 56115b032ac4cada55da46f76e4038d646805172..ceba7c34aca3884de2fbf08f541a0a4615716078 100644 (file)
@@ -116,13 +116,13 @@ int rtc_get(struct rtc_time *tm)
              buf[0], buf[1], buf[2], buf[3],
              buf[4], buf[5], buf[6], buf[7]);
 
-       tm->tm_sec = BCD2BIN(buf[CCR_SEC]);
-       tm->tm_min = BCD2BIN(buf[CCR_MIN]);
-       tm->tm_hour = BCD2BIN(buf[CCR_HOUR] & 0x3F); /* hr is 0-23 */
-       tm->tm_mday = BCD2BIN(buf[CCR_MDAY]);
-       tm->tm_mon = BCD2BIN(buf[CCR_MONTH]); /* mon is 0-11 */
-       tm->tm_year = BCD2BIN(buf[CCR_YEAR])
-               + (BCD2BIN(buf[CCR_Y2K]) * 100);
+       tm->tm_sec = bcd2bin(buf[CCR_SEC]);
+       tm->tm_min = bcd2bin(buf[CCR_MIN]);
+       tm->tm_hour = bcd2bin(buf[CCR_HOUR] & 0x3F); /* hr is 0-23 */
+       tm->tm_mday = bcd2bin(buf[CCR_MDAY]);
+       tm->tm_mon = bcd2bin(buf[CCR_MONTH]); /* mon is 0-11 */
+       tm->tm_year = bcd2bin(buf[CCR_YEAR])
+               + (bcd2bin(buf[CCR_Y2K]) * 100);
        tm->tm_wday = buf[CCR_WDAY];
 
        debug("%s: tm is secs=%d, mins=%d, hours=%d, "
@@ -143,21 +143,21 @@ int rtc_set(struct rtc_time *tm)
              tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
              tm->tm_hour, tm->tm_min, tm->tm_sec);
 
-       buf[CCR_SEC] = BIN2BCD(tm->tm_sec);
-       buf[CCR_MIN] = BIN2BCD(tm->tm_min);
+       buf[CCR_SEC] = bin2bcd(tm->tm_sec);
+       buf[CCR_MIN] = bin2bcd(tm->tm_min);
 
        /* set hour and 24hr bit */
-       buf[CCR_HOUR] = BIN2BCD(tm->tm_hour) | X1205_HR_MIL;
+       buf[CCR_HOUR] = bin2bcd(tm->tm_hour) | X1205_HR_MIL;
 
-       buf[CCR_MDAY] = BIN2BCD(tm->tm_mday);
+       buf[CCR_MDAY] = bin2bcd(tm->tm_mday);
 
        /* month, 1 - 12 */
-       buf[CCR_MONTH] = BIN2BCD(tm->tm_mon);
+       buf[CCR_MONTH] = bin2bcd(tm->tm_mon);
 
        /* year, since the rtc epoch*/
-       buf[CCR_YEAR] = BIN2BCD(tm->tm_year % 100);
+       buf[CCR_YEAR] = bin2bcd(tm->tm_year % 100);
        buf[CCR_WDAY] = tm->tm_wday & 0x07;
-       buf[CCR_Y2K] = BIN2BCD(tm->tm_year / 100);
+       buf[CCR_Y2K] = bin2bcd(tm->tm_year / 100);
 
        /* this sequence is required to unlock the chip */
        rtc_write(X1205_REG_SR, X1205_SR_WEL);
index c545308125b0904a070e3cccd611f83ff37b71e1..af4aa9c7baf8ace2f80e0a0ecb043d40d8b3f144 100644 (file)
@@ -3,18 +3,23 @@
  * at your option.
  */
 
-/* macros to translate to/from binary and binary-coded decimal (frequently
- * found in RTC chips).
+/* inline functions to translate to/from binary and binary-coded decimal
+ * (frequently found in RTC chips).
  */
 
 #ifndef _BCD_H
 #define _BCD_H
 
-#define BCD2BIN(val)   (((val) & 0x0f) + ((val)>>4)*10)
-#define BIN2BCD(val)   ((((val)/10)<<4) + (val)%10)
+#include <linux/types.h>
 
-/* backwards compat */
-#define BCD_TO_BIN(val) ((val)=BCD2BIN(val))
-#define BIN_TO_BCD(val) ((val)=BIN2BCD(val))
+static inline unsigned int bcd2bin(u8 val)
+{
+       return ((val) & 0x0f) + ((val) >> 4) * 10;
+}
+
+static inline u8 bin2bcd (unsigned int val)
+{
+       return (((val / 10) << 4) | (val % 10));
+}
 
 #endif /* _BCD_H */
index 227feeb0cd2f072ef66073023da73c54b3c77018..0644d92b3ca206b2bcd24a6eecb93791b033a7a4 100644 (file)
 #define RTC_VALID   RTC_REG_D
 # define RTC_VRT 0x80       /* valid RAM and time */
 /**********************************************************************/
-
-/* example: !(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
- * determines if the following two #defines are needed
- */
-#ifndef BCD_TO_BIN
-#define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10)
-#endif
-
-#ifndef BIN_TO_BCD
-#define BIN_TO_BCD(val) ((val)=(((val)/10)<<4) + (val)%10)
-#endif
-
 #endif /* _MC146818RTC_H */