[U-Boot-Users] [PATCH 1/5] Add Epson RX8025 RTC support

Matthias Fuchs matthias.fuchs at esd-electronics.com
Sun Nov 11 13:40:39 CET 2007


Signed-off-by: Matthias Fuchs <matthias.fuchs at esd-electronics.com>
---
 rtc/Makefile               |    6 +-
 rtc/{ds1307.c => rx8025.c} |  131 ++++++++++++++++++++++++++------------------
 2 files changed, 80 insertions(+), 57 deletions(-)
 copy rtc/{ds1307.c => rx8025.c} (60%)

diff --git a/rtc/Makefile b/rtc/Makefile
index 5a0cf11..16e9fde 100644
--- a/rtc/Makefile
+++ b/rtc/Makefile
@@ -30,9 +30,9 @@ LIB	= $(obj)librtc.a
 COBJS	= date.o   \
 	  bf5xx_rtc.o ds12887.o ds1302.o ds1306.o ds1307.o \
 	  ds1337.o ds1374.o ds1556.o ds164x.o ds174x.o ds3231.o \
-	  m41t11.o max6900.o m48t35ax.o mc146818.o mk48t59.o \
-	  mpc5xxx.o mpc8xx.o pcf8563.o s3c24x0_rtc.o rs5c372.o \
-	  mcfrtc.o x1205.o
+	  m41t11.o m48t35ax.o max6900.o  mc146818.o mcfrtc.o mk48t59.o \
+	  mpc5xxx.o mpc8xx.o pcf8563.o rs5c372.o rx8025.o s3c24x0_rtc.o \
+	  x1205.o
 
 SRCS	:= $(COBJS:.o=.c)
 OBJS	:= $(addprefix $(obj),$(COBJS))
diff --git a/rtc/ds1307.c b/rtc/rx8025.c
similarity index 60%
copy from rtc/ds1307.c
copy to rtc/rx8025.c
index c882d79..6c94ae1 100644
--- a/rtc/ds1307.c
+++ b/rtc/rx8025.c
@@ -1,8 +1,6 @@
 /*
- * (C) Copyright 2001, 2002, 2003
- * Wolfgang Denk, DENX Software Engineering, wd at denx.de.
- * Keith Outwater, keith_outwater at mvis.com`
- * Steven Scholz, steven.scholz at imc-berlin.de
+ * (C) Copyright 2007
+ * Matthias Fuchs, esd gmbh, matthias.fuchs at esd-electronics.com.
  *
  * See file CREDITS for list of people who contributed to this
  * project.
@@ -24,10 +22,7 @@
  */
 
 /*
- * Date & Time support (no alarms) for Dallas Semiconductor (now Maxim)
- * DS1307 and DS1338 Real Time Clock (RTC).
- *
- * based on ds1337.c
+ * Epson RX8025 RTC driver.
  */
 
 #include <common.h>
@@ -35,8 +30,7 @@
 #include <rtc.h>
 #include <i2c.h>
 
-#if (defined(CONFIG_RTC_DS1307) || defined(CONFIG_RTC_DS1338) ) && \
-    defined(CONFIG_CMD_DATE)
+#if defined(CONFIG_RTC_RX8025) && defined(CONFIG_CMD_DATE)
 
 /*---------------------------------------------------------------------*/
 #undef DEBUG_RTC
@@ -49,11 +43,7 @@
 /*---------------------------------------------------------------------*/
 
 #ifndef CFG_I2C_RTC_ADDR
-# define CFG_I2C_RTC_ADDR	0x68
-#endif
-
-#if defined(CONFIG_RTC_DS1307) && (CFG_I2C_SPEED > 100000)
-# error The DS1307 is specified only up to 100kHz!
+# define CFG_I2C_RTC_ADDR	0x32
 #endif
 
 /*
@@ -66,16 +56,39 @@
 #define RTC_DATE_REG_ADDR	0x04
 #define RTC_MON_REG_ADDR	0x05
 #define RTC_YR_REG_ADDR		0x06
-#define RTC_CTL_REG_ADDR	0x07
 
-#define RTC_SEC_BIT_CH		0x80	/* Clock Halt (in Register 0)   */
+#define RTC_CTL1_REG_ADDR	0x0e
+#define RTC_CTL2_REG_ADDR	0x0f
+
+/*
+ * Control register 1 bits
+ */
+#define RTC_CTL1_BIT_2412	0x20
+
+/*
+ * Control register 2 bits
+ */
+#define RTC_CTL2_BIT_PON	0x10
+#define RTC_CTL2_BIT_VDET	0x40
+#define RTC_CTL2_BIT_XST	0x20
+#define RTC_CTL2_BIT_VDSL	0x80
+
+/*
+ * Note: the RX8025 I2C RTC requires register
+ * reads and write to consist of a single bus
+ * cycle. It is not allowed to write the register
+ * address in a first cycle that is terminated by
+ * a STOP condition. The chips needs a 'restart'
+ * sequence (start sequence without a prior stop).
+ * This driver has been written for a 4xx board.
+ * U-Boot's 4xx i2c driver is currently not capable
+ * to generate such cycles to some work arounds
+ * are used.
+ */
 
-#define RTC_CTL_BIT_RS0		0x01	/* Rate select 0                */
-#define RTC_CTL_BIT_RS1		0x02	/* Rate select 1                */
-#define RTC_CTL_BIT_SQWE	0x10	/* Square Wave Enable           */
-#define RTC_CTL_BIT_OUT		0x80	/* Output Control               */
+/* static uchar rtc_read (uchar reg); */
+#define rtc_read(reg) buf[((reg) + 1) & 0xf]
 
-static uchar rtc_read (uchar reg);
 static void rtc_write (uchar reg, uchar val);
 static uchar bin2bcd (unsigned int n);
 static unsigned bcd2bin (uchar c);
@@ -85,26 +98,34 @@ static unsigned bcd2bin (uchar c);
  */
 void rtc_get (struct rtc_time *tmp)
 {
-	uchar sec, min, hour, mday, wday, mon, year;
+	uchar sec, min, hour, mday, wday, mon, year, ctl2;
+	uchar buf[16];
 
-	sec = rtc_read (RTC_SEC_REG_ADDR);
-	min = rtc_read (RTC_MIN_REG_ADDR);
-	hour = rtc_read (RTC_HR_REG_ADDR);
-	wday = rtc_read (RTC_DAY_REG_ADDR);
-	mday = rtc_read (RTC_DATE_REG_ADDR);
-	mon = rtc_read (RTC_MON_REG_ADDR);
-	year = rtc_read (RTC_YR_REG_ADDR);
+	if (i2c_read(CFG_I2C_RTC_ADDR, 0, 0, buf, 16))
+		printf("Error reading from RTC\n");
+
+	sec = rtc_read(RTC_SEC_REG_ADDR);
+	min = rtc_read(RTC_MIN_REG_ADDR);
+	hour = rtc_read(RTC_HR_REG_ADDR);
+	wday = rtc_read(RTC_DAY_REG_ADDR);
+	mday = rtc_read(RTC_DATE_REG_ADDR);
+	mon = rtc_read(RTC_MON_REG_ADDR);
+	year = rtc_read(RTC_YR_REG_ADDR);
 
 	DEBUGR ("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
 		"hr: %02x min: %02x sec: %02x\n",
 		year, mon, mday, wday, hour, min, sec);
 
-	if (sec & RTC_SEC_BIT_CH) {
-		printf ("### Warning: RTC oscillator has stopped\n");
-		/* clear the CH flag */
-		rtc_write (RTC_SEC_REG_ADDR,
-			   rtc_read (RTC_SEC_REG_ADDR) & ~RTC_SEC_BIT_CH);
-	}
+	/* dump status */
+	ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
+	if (ctl2 & RTC_CTL2_BIT_PON)
+		printf("RTC: power-on detected\n");
+
+	if (ctl2 & RTC_CTL2_BIT_VDET)
+		printf("RTC: voltage drop detected\n");
+
+	if (!(ctl2 & RTC_CTL2_BIT_XST))
+		printf("RTC: oscillator stop detected\n");
 
 	tmp->tm_sec  = bcd2bin (sec & 0x7F);
 	tmp->tm_min  = bcd2bin (min & 0x7F);
@@ -112,7 +133,7 @@ void rtc_get (struct rtc_time *tmp)
 	tmp->tm_mday = bcd2bin (mday & 0x3F);
 	tmp->tm_mon  = bcd2bin (mon & 0x1F);
 	tmp->tm_year = bcd2bin (year) + ( bcd2bin (year) >= 70 ? 1900 : 2000);
-	tmp->tm_wday = bcd2bin ((wday - 1) & 0x07);
+	tmp->tm_wday = bcd2bin (wday & 0x07);
 	tmp->tm_yday = 0;
 	tmp->tm_isdst= 0;
 
@@ -136,27 +157,32 @@ void rtc_set (struct rtc_time *tmp)
 
 	rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year % 100));
 	rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_mon));
-	rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday + 1));
+	rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday));
 	rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_mday));
 	rtc_write (RTC_HR_REG_ADDR, bin2bcd (tmp->tm_hour));
 	rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min));
 	rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec));
+
+	rtc_write (RTC_CTL1_REG_ADDR, RTC_CTL1_BIT_2412);
 }
 
 
 /*
  * Reset the RTC. We setting the date back to 1970-01-01.
- * We also enable the oscillator output on the SQW/OUT pin and program
- * it for 32,768 Hz output. Note that according to the datasheet, turning
- * on the square wave output increases the current drain on the backup
- * battery to something between 480nA and 800nA.
  */
 void rtc_reset (void)
 {
 	struct rtc_time tmp;
+	uchar buf[16];
+	uchar ctl2;
+
+	if (i2c_read(CFG_I2C_RTC_ADDR, 0,    0,   buf, 16))
+		printf("Error reading from RTC\n");
 
-	rtc_write (RTC_SEC_REG_ADDR, 0x00);	/* clearing Clock Halt	*/
-	rtc_write (RTC_CTL_REG_ADDR, RTC_CTL_BIT_SQWE | RTC_CTL_BIT_RS1 | RTC_CTL_BIT_RS0);
+	ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
+	ctl2 &= ~(RTC_CTL2_BIT_PON | RTC_CTL2_BIT_VDET);
+	ctl2 |= RTC_CTL2_BIT_XST | RTC_CTL2_BIT_VDSL;
+	rtc_write (RTC_CTL2_REG_ADDR, ctl2);
 
 	tmp.tm_year = 1970;
 	tmp.tm_mon = 1;
@@ -178,17 +204,14 @@ void rtc_reset (void)
 /*
  * Helper functions
  */
-
-static
-uchar rtc_read (uchar reg)
-{
-	return (i2c_reg_read (CFG_I2C_RTC_ADDR, reg));
-}
-
-
 static void rtc_write (uchar reg, uchar val)
 {
-	i2c_reg_write (CFG_I2C_RTC_ADDR, reg, val);
+	uchar buf[2];
+	buf[0] = reg << 4;
+	buf[1] = val;
+	if (i2c_write(CFG_I2C_RTC_ADDR, 0, 0, buf, 2) != 0)
+		printf("Error writing to RTC\n");
+
 }
 
 static unsigned bcd2bin (uchar n)
@@ -201,4 +224,4 @@ static unsigned char bin2bcd (unsigned int n)
 	return (((n / 10) << 4) | (n % 10));
 }
 
-#endif
+#endif /* CONFIG_RTC_RX8025 && (CFG_COMMANDS & CFG_CMD_DATE) */
-- 
1.5.3





More information about the U-Boot mailing list