Commit db8f4c7e authored by Andrew Morton's avatar Andrew Morton Committed by Linus Torvalds

[PATCH] BIN_TO_BCD consolidation

Cleanup patch from Hollis Blanchard <hollis@austin.ibm.com>

We have a large number of private implementations of BIN_TO_BCD and
BCD_TO_BIN, which are all the same.  And a lot of them are inflexible because
they modify their arg:

	#define BIN_TO_BCD(val) ((val)=(((val)/10)<<4) + (val)%10)

- Create (in <linux/bcd.h> a generic BIN2BCD/BCD2BIN which does not modify
  its arg

- Create generic BIN_TO_BCD/BCD_TO_BIN which uses the above

- Update lots of callers to use the new generic version.
parent fe04e945
......@@ -37,6 +37,7 @@
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/bcd.h>
#include <asm/uaccess.h>
#include <asm/io.h>
......
......@@ -47,14 +47,6 @@ EXPORT_SYMBOL(rtc_lock);
/* change this if you have some constant time drift */
#define USECS_PER_JIFFY (1000000/HZ)
#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
static int dummy_set_rtc(void)
{
return 0;
......
......@@ -97,6 +97,7 @@
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/bcd.h>
#include <asm/uaccess.h>
#include <asm/system.h>
......
......@@ -32,6 +32,7 @@
#include <linux/interrupt.h>
#include <linux/time.h>
#include <linux/delay.h>
#include <linux/bcd.h>
#include <asm/segment.h>
#include <asm/io.h>
......
......@@ -43,6 +43,7 @@
#include <linux/smp.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/bcd.h>
#include <asm/io.h>
#include <asm/smp.h>
......
......@@ -15,6 +15,7 @@
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/rtc.h>
#include <linux/bcd.h>
#include <asm/rtc.h>
......
......@@ -11,6 +11,7 @@
#include <linux/kernel_stat.h>
#include <linux/interrupt.h>
#include <linux/rtc.h>
#include <linux/bcd.h>
#include <asm/irq.h>
#include <asm/io.h>
......@@ -36,9 +37,6 @@
#define C_SIGN 0x20
#define C_CALIB 0x1f
#define BCD_TO_BIN(val) (((val)&15) + ((val)>>4)*10)
#define BIN_TO_BCD(val) (((val/10) << 4) | (val % 10))
int sun3x_hwclk(int set, struct rtc_time *t)
{
volatile struct mostek_dt *h =
......@@ -49,23 +47,23 @@ int sun3x_hwclk(int set, struct rtc_time *t)
if(set) {
h->csr |= C_WRITE;
h->sec = BIN_TO_BCD(t->tm_sec);
h->min = BIN_TO_BCD(t->tm_min);
h->hour = BIN_TO_BCD(t->tm_hour);
h->wday = BIN_TO_BCD(t->tm_wday);
h->mday = BIN_TO_BCD(t->tm_mday);
h->month = BIN_TO_BCD(t->tm_mon);
h->year = BIN_TO_BCD(t->tm_year);
h->sec = BIN2BCD(t->tm_sec);
h->min = BIN2BCD(t->tm_min);
h->hour = BIN2BCD(t->tm_hour);
h->wday = BIN2BCD(t->tm_wday);
h->mday = BIN2BCD(t->tm_mday);
h->month = BIN2BCD(t->tm_mon);
h->year = BIN2BCD(t->tm_year);
h->csr &= ~C_WRITE;
} else {
h->csr |= C_READ;
t->tm_sec = BCD_TO_BIN(h->sec);
t->tm_min = BCD_TO_BIN(h->min);
t->tm_hour = BCD_TO_BIN(h->hour);
t->tm_wday = BCD_TO_BIN(h->wday);
t->tm_mday = BCD_TO_BIN(h->mday);
t->tm_mon = BCD_TO_BIN(h->month);
t->tm_year = BCD_TO_BIN(h->year);
t->tm_sec = BCD2BIN(h->sec);
t->tm_min = BCD2BIN(h->min);
t->tm_hour = BCD2BIN(h->hour);
t->tm_wday = BCD2BIN(h->wday);
t->tm_mday = BCD2BIN(h->mday);
t->tm_mon = BCD2BIN(h->month);
t->tm_year = BCD2BIN(h->year);
h->csr &= ~C_READ;
}
......
......@@ -20,6 +20,7 @@
#include <linux/types.h>
#include <linux/time.h>
#include <linux/bcd.h>
#include <asm/time.h>
#include <asm/addrspace.h>
......@@ -28,12 +29,6 @@
#define EPOCH 2000
#undef BCD_TO_BIN
#define BCD_TO_BIN(val) (((val)&15) + ((val)>>4)*10)
#undef BIN_TO_BCD
#define BIN_TO_BCD(val) ((((val)/10)<<4) + (val)%10)
#define READ_RTC(x) *(volatile unsigned char*)(rtc_base+x)
#define WRITE_RTC(x, y) *(volatile unsigned char*)(rtc_base+x) = y
......@@ -52,11 +47,11 @@ rtc_ds1386_get_time(void)
WRITE_RTC(0xB, byte);
/* read time data */
year = BCD_TO_BIN(READ_RTC(0xA)) + EPOCH;
month = BCD_TO_BIN(READ_RTC(0x9) & 0x1f);
day = BCD_TO_BIN(READ_RTC(0x8));
minute = BCD_TO_BIN(READ_RTC(0x2));
second = BCD_TO_BIN(READ_RTC(0x1));
year = BCD2BIN(READ_RTC(0xA)) + EPOCH;
month = BCD2BIN(READ_RTC(0x9) & 0x1f);
day = BCD2BIN(READ_RTC(0x8));
minute = BCD2BIN(READ_RTC(0x2));
second = BCD2BIN(READ_RTC(0x1));
/* hour is special - deal with it later */
temp = READ_RTC(0x4);
......@@ -68,11 +63,11 @@ rtc_ds1386_get_time(void)
/* calc hour */
if (temp & 0x40) {
/* 12 hour format */
hour = BCD_TO_BIN(temp & 0x1f);
hour = BCD2BIN(temp & 0x1f);
if (temp & 0x20) hour += 12; /* PM */
} else {
/* 24 hour format */
hour = BCD_TO_BIN(temp & 0x3f);
hour = BCD2BIN(temp & 0x3f);
}
return mktime(year, month, day, hour, minute, second);
......@@ -95,19 +90,19 @@ rtc_ds1386_set_time(unsigned long t)
to_tm(t, &tm);
/* check each field one by one */
year = BIN_TO_BCD(tm.tm_year - EPOCH);
year = BIN2BCD(tm.tm_year - EPOCH);
if (year != READ_RTC(0xA)) {
WRITE_RTC(0xA, year);
}
temp = READ_RTC(0x9);
month = BIN_TO_BCD(tm.tm_mon);
month = BIN2BCD(tm.tm_mon);
if (month != (temp & 0x1f)) {
WRITE_RTC( 0x9,
(month & 0x1f) | (temp & ~0x1f) );
}
day = BIN_TO_BCD(tm.tm_mday);
day = BIN2BCD(tm.tm_mday);
if (day != READ_RTC(0x8)) {
WRITE_RTC(0x8, day);
}
......@@ -117,22 +112,22 @@ rtc_ds1386_set_time(unsigned long t)
/* 12 hour format */
hour = 0x40;
if (tm.tm_hour > 12) {
hour |= 0x20 | (BIN_TO_BCD(hour-12) & 0x1f);
hour |= 0x20 | (BIN2BCD(hour-12) & 0x1f);
} else {
hour |= BIN_TO_BCD(tm.tm_hour);
hour |= BIN2BCD(tm.tm_hour);
}
} else {
/* 24 hour format */
hour = BIN_TO_BCD(tm.tm_hour) & 0x3f;
hour = BIN2BCD(tm.tm_hour) & 0x3f;
}
if (hour != temp) WRITE_RTC(0x4, hour);
minute = BIN_TO_BCD(tm.tm_min);
minute = BIN2BCD(tm.tm_min);
if (minute != READ_RTC(0x2)) {
WRITE_RTC(0x2, minute);
}
second = BIN_TO_BCD(tm.tm_sec);
second = BIN2BCD(tm.tm_sec);
if (second != READ_RTC(0x1)) {
WRITE_RTC(0x1, second);
}
......
......@@ -16,6 +16,7 @@
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/bcd.h>
#include <asm/cpu.h>
#include <asm/bootinfo.h>
......
......@@ -16,6 +16,7 @@
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/kernel_stat.h>
#include <linux/bcd.h>
#include <asm/bootinfo.h>
#include <asm/cpu.h>
......
......@@ -35,6 +35,7 @@
#include <linux/poll.h>
#include <linux/proc_fs.h>
#include <linux/smp_lock.h>
#include <linux/bcd.h>
#include <asm/m48t35.h>
#include <asm/sn/ioc3.h>
......
......@@ -11,6 +11,7 @@
#include <linux/param.h>
#include <linux/timex.h>
#include <linux/mm.h>
#include <linux/bcd.h>
#include <asm/pgtable.h>
#include <asm/sgialib.h>
......
......@@ -40,6 +40,7 @@
#include <asm/iSeries/iSeries_proc.h>
#include <asm/uaccess.h>
#include <linux/pci.h>
#include <linux/bcd.h>
/*
......
......@@ -20,6 +20,7 @@
#include <linux/kernel_stat.h>
#include <linux/mc146818rtc.h>
#include <linux/init.h>
#include <linux/bcd.h>
#include <asm/segment.h>
#include <asm/io.h>
......
......@@ -24,6 +24,7 @@
#include <linux/irq.h>
#include <linux/seq_file.h>
#include <linux/root_dev.h>
#include <linux/bcd.h>
#include <asm/system.h>
#include <asm/pgtable.h>
......
......@@ -19,6 +19,7 @@
#include <linux/timex.h>
#include <linux/kernel_stat.h>
#include <linux/init.h>
#include <linux/bcd.h>
#include <asm/sections.h>
#include <asm/segment.h>
......
......@@ -19,6 +19,7 @@
#include <linux/kernel.h>
#include <linux/time.h>
#include <linux/timex.h>
#include <linux/bcd.h>
#include <asm/machdep.h>
#include <asm/io.h>
......
......@@ -40,6 +40,7 @@
#include <asm/iSeries/iSeries_proc.h>
#include <asm/uaccess.h>
#include <linux/pci.h>
#include <linux/bcd.h>
extern struct pci_dev * iSeries_vio_dev;
......
......@@ -32,6 +32,7 @@
#include <linux/poll.h>
#include <linux/proc_fs.h>
#include <linux/spinlock.h>
#include <linux/bcd.h>
#include <asm/io.h>
#include <asm/uaccess.h>
......
......@@ -9,18 +9,11 @@
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/time.h>
#include <linux/bcd.h>
#include <asm/io.h>
#include <asm/rtc.h>
#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
void sh_rtc_gettimeofday(struct timeval *tv)
{
unsigned int sec128, sec, min, hr, wk, day, mon, yr, yr100;
......
......@@ -23,6 +23,7 @@
#include <linux/mc146818rtc.h>
#include <linux/delay.h>
#include <linux/profile.h>
#include <linux/bcd.h>
#include <asm/oplib.h>
#include <asm/mostek.h>
......@@ -330,14 +331,6 @@ static int __init has_low_battery(void)
return (data1 == data2); /* Was the write blocked? */
}
#ifndef BCD_TO_BIN
#define BCD_TO_BIN(val) (((val)&15) + ((val)>>4)*10)
#endif
#ifndef BIN_TO_BCD
#define BIN_TO_BCD(val) ((((val)/10)<<4) + (val)%10)
#endif
/* Probe for the real time clock chip. */
static void __init set_system_time(void)
{
......
......@@ -21,6 +21,7 @@
#include <linux/ioport.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/bcd.h>
#include <asm/vsyscall.h>
#include <asm/timex.h>
......
......@@ -14,6 +14,7 @@
#include <linux/string.h>
#include <linux/mc146818rtc.h>
#include <linux/init.h>
#include <linux/bcd.h>
#include "pcf8583.h"
......
......@@ -16,6 +16,7 @@
#include <linux/device.h>
#include <linux/suspend.h>
#include <linux/seq_file.h>
#include <linux/bcd.h>
#include <asm/uaccess.h>
#include <asm/acpi.h>
......
......@@ -72,6 +72,7 @@
#include <linux/spinlock.h>
#include <linux/sysctl.h>
#include <linux/wait.h>
#include <linux/bcd.h>
#include <asm/current.h>
#include <asm/uaccess.h>
......
......@@ -37,6 +37,7 @@
#include <linux/config.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/bcd.h>
#include <linux/blk.h>
#include "scsi.h"
......@@ -151,8 +152,6 @@ int sr_set_blocklength(Scsi_CD *cd, int blocklength)
/* This function gets called after a media change. Checks if the CD is
multisession, asks for offset etc. */
#define BCD_TO_BIN(x) ((((int)x & 0xf0) >> 4)*10 + ((int)x & 0x0f))
int sr_cd_check(struct cdrom_device_info *cdi)
{
Scsi_CD *cd = cdi->handle;
......@@ -223,9 +222,9 @@ int sr_cd_check(struct cdrom_device_info *cdi)
no_multi = 1;
break;
}
min = BCD_TO_BIN(buffer[15]);
sec = BCD_TO_BIN(buffer[16]);
frame = BCD_TO_BIN(buffer[17]);
min = BCD2BIN(buffer[15]);
sec = BCD2BIN(buffer[16]);
frame = BCD2BIN(buffer[17]);
sector = min * CD_SECS * CD_FRAMES + sec * CD_FRAMES + frame;
break;
}
......@@ -252,9 +251,9 @@ int sr_cd_check(struct cdrom_device_info *cdi)
}
if (rc != 0)
break;
min = BCD_TO_BIN(buffer[1]);
sec = BCD_TO_BIN(buffer[2]);
frame = BCD_TO_BIN(buffer[3]);
min = BCD2BIN(buffer[1]);
sec = BCD2BIN(buffer[2]);
frame = BCD2BIN(buffer[3]);
sector = min * CD_SECS * CD_FRAMES + sec * CD_FRAMES + frame;
if (sector)
sector -= CD_MSF_OFFSET;
......
......@@ -36,6 +36,7 @@
#include <linux/poll.h>
#include <linux/rtc.h>
#include <linux/spinlock.h>
#include <linux/bcd.h>
#include <asm/ds1286.h>
#include <asm/io.h>
......
......@@ -18,6 +18,7 @@
#define RTC_ALWAYS_BCD 0
#include <linux/mc146818rtc.h>
#include <linux/bcd.h>
#include <asm/hardware/dec21285.h>
#include <asm/leds.h>
......
......@@ -39,11 +39,6 @@
#define RTC_INIT() (-1)
#endif
/* conversions to and from the stupid RTC internal format */
#define BCD_TO_BIN(x) x = (((x & 0xf0) >> 3) * 5 + (x & 0xf))
#define BIN_TO_BCD(x) x = (x % 10) | ((x / 10) << 4)
/*
* The struct used to pass data via the following ioctl. Similar to the
* struct tm in <time.h>, but it needs to be here so that the kernel
......
......@@ -16,6 +16,7 @@
#include <linux/mc146818rtc.h>
#include <linux/rtc.h>
#include <linux/bcd.h>
#define RTC_PIE 0x40 /* periodic interrupt enable */
#define RTC_AIE 0x20 /* alarm interrupt enable */
......
......@@ -57,15 +57,4 @@
#define RTC_IPSW 0x40
#define RTC_TE 0x80
/*
* Conversion between binary and BCD.
*/
#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 /* _ASM_DS1286_h */
......@@ -56,15 +56,4 @@
#define RTC_IPSW 0x40
#define RTC_TE 0x80
/*
* Conversion between binary and BCD.
*/
#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 /* _ASM_DS1286_h */
......@@ -21,12 +21,4 @@ struct m48t35_rtc {
#define M48T35_RTC_STOPPED 0x80
#define M48T35_RTC_READ 0x40
#ifndef BCD_TO_BIN
#define BCD_TO_BIN(x) ((x)=((x)&15) + ((x)>>4)*10)
#endif
#ifndef BIN_TO_BCD
#define BIN_TO_BCD(x) ((x)=(((x)/10)<<4) + (x)%10)
#endif
#endif
......@@ -74,13 +74,4 @@
#define M48T35_RTC_READ 0x40
/* read/write conversions */
#ifndef BCD_TO_BIN
#define BCD_TO_BIN(x) ((x)=((x)&15) + ((x)>>4)*10)
#endif
#ifndef BIN_TO_BCD
#define BIN_TO_BCD(x) ((x)=(((x)/10)<<4) + (x)%10)
#endif
#endif
......@@ -24,12 +24,4 @@
#define MK48T59_RTC_CONTROLB 0x1FF9
#define MK48T59_RTC_CB_STOP 0x80
#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 /* _PPC_MK48T59_H */
......@@ -23,14 +23,6 @@
#define MOTO_RTC_CONTROLA 0x1FF8
#define MOTO_RTC_CONTROLB 0x1FF9
#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
/* PowerMac specific nvram stuffs */
enum {
......
......@@ -355,14 +355,6 @@ typedef struct {
todc_info->flags = clock_type ##_FLAGS; \
}
#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
extern todc_info_t *todc_info;
unsigned char todc_direct_read_val(int addr);
......
......@@ -28,12 +28,4 @@
#define MOTO_RTC_CONTROLA 0x1FF8
#define MOTO_RTC_CONTROLB 0x1FF9
#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 /* _PPC64_NVRAM_H */
/* Permission is hereby granted to copy, modify and redistribute this code
* in terms of the GNU Library General Public License, Version 2 or later,
* at your option.
*/
/* macros 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)
/* backwards compat */
#define BCD_TO_BIN(val) ((val)=BCD2BIN(val))
#define BIN_TO_BCD(val) ((val)=BIN2BCD(val))
#endif /* _BCD_H */
......@@ -87,15 +87,4 @@ extern spinlock_t rtc_lock; /* serialize CMOS RAM access */
# 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 */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment