mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [RFC 0/5] add rtc support
@ 2014-07-10  8:33 Antony Pavlov
  2014-07-10  8:33 ` [RFC 1/5] lib: import 'bcd' from linux-3.15 Antony Pavlov
                   ` (5 more replies)
  0 siblings, 6 replies; 17+ messages in thread
From: Antony Pavlov @ 2014-07-10  8:33 UTC (permalink / raw)
  To: barebox

This patchseries imports RTC support from linux-3.15.

User can use 'hwclock' command to see realtime clock readout.

Tested with DS1307.

TODOs:

 * rtc_set_time() is not realized;
 * ds1307_set_time() is not tested;
 * bcd lib is compiled in even if rtc support is disabled (as linux does);
   we can make it optional;
 * adjust include/linux/rtc.h and include/rtc.h;
 * rtc_unregister() is not realized.


Antony Pavlov (5):
  lib: import 'bcd' from linux-3.15
  add rtc support
  i2c: import SMBus stuff from linux
  rtc: add ds1307 support
  commands: add hwclock

 commands/Kconfig         |   8 ++
 commands/Makefile        |   1 +
 commands/hwclock.c       |  37 +++++
 drivers/Kconfig          |   1 +
 drivers/Makefile         |   1 +
 drivers/i2c/i2c.c        | 368 +++++++++++++++++++++++++++++++++++++++++++++++
 drivers/rtc/Kconfig      |  45 ++++++
 drivers/rtc/Makefile     |  10 ++
 drivers/rtc/class.c      |  62 ++++++++
 drivers/rtc/rtc-ds1307.c | 315 ++++++++++++++++++++++++++++++++++++++++
 drivers/rtc/rtc-lib.c    |  64 +++++++++
 include/i2c/i2c.h        |  64 +++++++++
 include/linux/bcd.h      |  22 +++
 include/linux/rtc.h      |  46 ++++++
 include/rtc.h            |   2 +
 lib/Makefile             |   1 +
 lib/bcd.c                |  14 ++
 17 files changed, 1061 insertions(+)
 create mode 100644 commands/hwclock.c
 create mode 100644 drivers/rtc/Kconfig
 create mode 100644 drivers/rtc/Makefile
 create mode 100644 drivers/rtc/class.c
 create mode 100644 drivers/rtc/rtc-ds1307.c
 create mode 100644 drivers/rtc/rtc-lib.c
 create mode 100644 include/linux/bcd.h
 create mode 100644 include/linux/rtc.h
 create mode 100644 lib/bcd.c

-- 
2.0.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [RFC 1/5] lib: import 'bcd' from linux-3.15
  2014-07-10  8:33 [RFC 0/5] add rtc support Antony Pavlov
@ 2014-07-10  8:33 ` Antony Pavlov
  2014-07-10  8:33 ` [RFC 2/5] add rtc support Antony Pavlov
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 17+ messages in thread
From: Antony Pavlov @ 2014-07-10  8:33 UTC (permalink / raw)
  To: barebox

Signed-off-by: Antony Pavlov <antonynpavlov@gmail.com>
---
 include/linux/bcd.h | 22 ++++++++++++++++++++++
 lib/Makefile        |  1 +
 lib/bcd.c           | 14 ++++++++++++++
 3 files changed, 37 insertions(+)

diff --git a/include/linux/bcd.h b/include/linux/bcd.h
new file mode 100644
index 0000000..18fff11
--- /dev/null
+++ b/include/linux/bcd.h
@@ -0,0 +1,22 @@
+#ifndef _BCD_H
+#define _BCD_H
+
+#include <linux/compiler.h>
+
+#define bcd2bin(x)					\
+		(__builtin_constant_p((u8 )(x)) ?	\
+		const_bcd2bin(x) :			\
+		_bcd2bin(x))
+
+#define bin2bcd(x)					\
+		(__builtin_constant_p((u8 )(x)) ?	\
+		const_bin2bcd(x) :			\
+		_bin2bcd(x))
+
+#define const_bcd2bin(x)	(((x) & 0x0f) + ((x) >> 4) * 10)
+#define const_bin2bcd(x)	((((x) / 10) << 4) + (x) % 10)
+
+unsigned _bcd2bin(unsigned char val) __attribute_const__;
+unsigned char _bin2bcd(unsigned val) __attribute_const__;
+
+#endif /* _BCD_H */
diff --git a/lib/Makefile b/lib/Makefile
index e8769a9..14442a7 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -1,3 +1,4 @@
+obj-y			+= bcd.o
 obj-$(CONFIG_BOOTSTRAP)	+= bootstrap/
 obj-y			+= ctype.o
 obj-y			+= rbtree.o
diff --git a/lib/bcd.c b/lib/bcd.c
new file mode 100644
index 0000000..b072d50
--- /dev/null
+++ b/lib/bcd.c
@@ -0,0 +1,14 @@
+#include <linux/bcd.h>
+#include <module.h>
+
+unsigned _bcd2bin(unsigned char val)
+{
+	return (val & 0x0f) + (val >> 4) * 10;
+}
+EXPORT_SYMBOL(_bcd2bin);
+
+unsigned char _bin2bcd(unsigned val)
+{
+	return ((val / 10) << 4) + val % 10;
+}
+EXPORT_SYMBOL(_bin2bcd);
-- 
2.0.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [RFC 2/5] add rtc support
  2014-07-10  8:33 [RFC 0/5] add rtc support Antony Pavlov
  2014-07-10  8:33 ` [RFC 1/5] lib: import 'bcd' from linux-3.15 Antony Pavlov
@ 2014-07-10  8:33 ` Antony Pavlov
  2014-07-10 21:40   ` Sascha Hauer
  2014-07-10  8:33 ` [RFC 3/5] i2c: import SMBus stuff from linux Antony Pavlov
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 17+ messages in thread
From: Antony Pavlov @ 2014-07-10  8:33 UTC (permalink / raw)
  To: barebox

Signed-off-by: Antony Pavlov <antonynpavlov@gmail.com>
---
 drivers/Kconfig       |  1 +
 drivers/Makefile      |  1 +
 drivers/rtc/Kconfig   | 16 +++++++++++++
 drivers/rtc/Makefile  |  6 +++++
 drivers/rtc/class.c   | 62 +++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/rtc/rtc-lib.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/rtc.h   | 46 ++++++++++++++++++++++++++++++++++++
 include/rtc.h         |  2 ++
 8 files changed, 198 insertions(+)

diff --git a/drivers/Kconfig b/drivers/Kconfig
index 12a9d8c..d38032c 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -28,5 +28,6 @@ source "drivers/bus/Kconfig"
 source "drivers/regulator/Kconfig"
 source "drivers/reset/Kconfig"
 source "drivers/pci/Kconfig"
+source "drivers/rtc/Kconfig"
 
 endmenu
diff --git a/drivers/Makefile b/drivers/Makefile
index 1990e86..4591f9a 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -27,3 +27,4 @@ obj-y += bus/
 obj-$(CONFIG_REGULATOR) += regulator/
 obj-$(CONFIG_RESET_CONTROLLER) += reset/
 obj-$(CONFIG_PCI) += pci/
+obj-y += rtc/
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
new file mode 100644
index 0000000..f148266
--- /dev/null
+++ b/drivers/rtc/Kconfig
@@ -0,0 +1,16 @@
+#
+# RTC class/drivers configuration
+#
+
+config RTC_LIB
+	bool
+
+menuconfig RTC_CLASS
+        bool "Real Time Clock"
+        default n
+        depends on !S390 && !UML
+        select RTC_LIB
+        help
+          Generic RTC class support. If you say yes here, you will
+          be allowed to plug one or more RTCs to your system. You will
+          probably want to enable one or more of the interfaces below.
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
new file mode 100644
index 0000000..7d1b5bc
--- /dev/null
+++ b/drivers/rtc/Makefile
@@ -0,0 +1,6 @@
+#
+# Makefile for RTC class/drivers.
+#
+
+obj-$(CONFIG_RTC_LIB)           += rtc-lib.o
+obj-$(CONFIG_RTC_CLASS)         += class.o
diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c
new file mode 100644
index 0000000..b808f3d
--- /dev/null
+++ b/drivers/rtc/class.c
@@ -0,0 +1,62 @@
+#include <linux/err.h>
+#include <rtc.h>
+#include <linux/rtc.h>
+
+LIST_HEAD(rtc_list);
+EXPORT_SYMBOL(rtc_list);
+
+struct rtc_device *rtc_lookup(const char *name)
+{
+	struct rtc_device *r;
+
+	if (!name)
+		return ERR_PTR(-ENODEV);
+
+	list_for_each_entry(r, &rtc_list, list) {
+		if (!strcmp(dev_name(&r->class_dev), name))
+			return r;
+	}
+
+	return ERR_PTR(-ENODEV);
+}
+EXPORT_SYMBOL(rtc_lookup);
+
+int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
+{
+	return rtc->ops->read_time(rtc, tm);
+}
+EXPORT_SYMBOL(rtc_read_time);
+
+int rtc_register(struct rtc_device *rtcdev)
+{
+	struct device_d *dev = &rtcdev->class_dev;
+
+	dev->id = DEVICE_ID_DYNAMIC;
+	strcpy(dev->name, "rtc");
+	if (rtcdev->dev)
+		dev->parent = rtcdev->dev;
+	platform_device_register(dev);
+
+	list_add_tail(&rtcdev->list, &rtc_list);
+
+	return 0;
+}
+EXPORT_SYMBOL(rtc_register);
+
+#if 0
+int rtc_unregister(struct rtc_device *cdev)
+{
+	struct device_d *dev = &cdev->class_dev;
+	int status;
+
+	list_del(&cdev->list);
+	if (list_empty(&rtc_list))
+		initialized = CONSOLE_UNINITIALIZED;
+
+	status = unregister_device(dev);
+	if (!status)
+		memset(cdev, 0, sizeof(*cdev));
+	return status;
+}
+EXPORT_SYMBOL(rtc_unregister);
+#endif
diff --git a/drivers/rtc/rtc-lib.c b/drivers/rtc/rtc-lib.c
new file mode 100644
index 0000000..44c0a06
--- /dev/null
+++ b/drivers/rtc/rtc-lib.c
@@ -0,0 +1,64 @@
+/*
+ * rtc and date/time utility functions
+ *
+ * Copyright (C) 2005-06 Tower Technologies
+ * Author: Alessandro Zummo <a.zummo@towertech.it>
+ *
+ * based on arch/arm/common/rtctime.c and other bits
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+*/
+
+#include <rtc.h>
+#include <linux/rtc.h>
+
+static const unsigned char rtc_days_in_month[] = {
+	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
+};
+
+static const unsigned short rtc_ydays[2][13] = {
+	/* Normal years */
+	{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
+	/* Leap years */
+	{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
+};
+
+#define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
+
+/*
+ * The number of days in the month.
+ */
+int rtc_month_days(unsigned int month, unsigned int year)
+{
+	return rtc_days_in_month[month] + (is_leap_year(year) && month == 1);
+}
+EXPORT_SYMBOL(rtc_month_days);
+
+/*
+ * The number of days since January 1. (0 to 365)
+ */
+int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
+{
+	return rtc_ydays[is_leap_year(year)][month] + day-1;
+}
+EXPORT_SYMBOL(rtc_year_days);
+
+/*
+ * Does the rtc_time represent a valid date/time?
+ */
+int rtc_valid_tm(struct rtc_time *tm)
+{
+	if (tm->tm_year < 70
+		|| ((unsigned)tm->tm_mon) >= 12
+		|| tm->tm_mday < 1
+		|| tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year + 1900)
+		|| ((unsigned)tm->tm_hour) >= 24
+		|| ((unsigned)tm->tm_min) >= 60
+		|| ((unsigned)tm->tm_sec) >= 60)
+		return -EINVAL;
+
+	return 0;
+}
+EXPORT_SYMBOL(rtc_valid_tm);
diff --git a/include/linux/rtc.h b/include/linux/rtc.h
new file mode 100644
index 0000000..fc94a57
--- /dev/null
+++ b/include/linux/rtc.h
@@ -0,0 +1,46 @@
+/*
+ * Generic RTC interface.
+ * This version contains the part of the user interface to the Real Time Clock
+ * service. It is used with both the legacy mc146818 and also  EFI
+ * Struct rtc_time and first 12 ioctl by Paul Gortmaker, 1996 - separated out
+ * from <linux/mc146818rtc.h> to this file for 2.4 kernels.
+ *
+ * Copyright (C) 1999 Hewlett-Packard Co.
+ * Copyright (C) 1999 Stephane Eranian <eranian@hpl.hp.com>
+ */
+#ifndef _LINUX_RTC_H_
+#define _LINUX_RTC_H_
+
+#include <common.h>
+#include <linux/types.h>
+
+extern int rtc_month_days(unsigned int month, unsigned int year);
+extern int rtc_year_days(unsigned int day, unsigned int month, unsigned int year);
+extern int rtc_valid_tm(struct rtc_time *tm);
+
+struct rtc_class_ops;
+
+struct rtc_device {
+	struct device_d *dev;
+	struct device_d class_dev;
+	struct list_head list;
+
+	const struct rtc_class_ops *ops;
+};
+
+struct rtc_class_ops {
+	int (*read_time)(struct rtc_device *, struct rtc_time *);
+	int (*set_time)(struct rtc_device *, struct rtc_time *);
+};
+
+extern int rtc_register(struct rtc_device *rtcdev);
+
+extern int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm);
+extern int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm);
+
+static inline bool is_leap_year(unsigned int year)
+{
+	return (!(year % 4) && (year % 100)) || !(year % 400);
+}
+
+#endif /* _LINUX_RTC_H_ */
diff --git a/include/rtc.h b/include/rtc.h
index c2d8bce..e2414fb 100644
--- a/include/rtc.h
+++ b/include/rtc.h
@@ -53,4 +53,6 @@ void to_tm (int, struct rtc_time *);
 unsigned long mktime (unsigned int, unsigned int, unsigned int,
 		      unsigned int, unsigned int, unsigned int);
 
+extern struct rtc_device *rtc_lookup(const char *name);
+
 #endif	/* _RTC_H_ */
-- 
2.0.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [RFC 3/5] i2c: import SMBus stuff from linux
  2014-07-10  8:33 [RFC 0/5] add rtc support Antony Pavlov
  2014-07-10  8:33 ` [RFC 1/5] lib: import 'bcd' from linux-3.15 Antony Pavlov
  2014-07-10  8:33 ` [RFC 2/5] add rtc support Antony Pavlov
@ 2014-07-10  8:33 ` Antony Pavlov
  2014-07-10  8:33 ` [RFC 4/5] rtc: add ds1307 support Antony Pavlov
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 17+ messages in thread
From: Antony Pavlov @ 2014-07-10  8:33 UTC (permalink / raw)
  To: barebox

Signed-off-by: Antony Pavlov <antonynpavlov@gmail.com>
---
 drivers/i2c/i2c.c | 368 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/i2c/i2c.h |  64 ++++++++++
 2 files changed, 432 insertions(+)

diff --git a/drivers/i2c/i2c.c b/drivers/i2c/i2c.c
index 9873957..167a5e4 100644
--- a/drivers/i2c/i2c.c
+++ b/drivers/i2c/i2c.c
@@ -159,6 +159,374 @@ int i2c_master_recv(struct i2c_client *client, char *buf, int count)
 }
 EXPORT_SYMBOL(i2c_master_recv);
 
+/* The SMBus parts */
+
+#define POLY    (0x1070U << 3)
+static u8 crc8(u16 data)
+{
+	int i;
+
+	for (i = 0; i < 8; i++) {
+		if (data & 0x8000)
+			data = data ^ POLY;
+		data = data << 1;
+	}
+	return (u8)(data >> 8);
+}
+
+/* Incremental CRC8 over count bytes in the array pointed to by p */
+static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
+{
+	int i;
+
+	for (i = 0; i < count; i++)
+		crc = crc8((crc ^ p[i]) << 8);
+	return crc;
+}
+
+/* Assume a 7-bit address, which is reasonable for SMBus */
+static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
+{
+	/* The address will be sent first */
+	u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
+	pec = i2c_smbus_pec(pec, &addr, 1);
+
+	/* The data buffer follows */
+	return i2c_smbus_pec(pec, msg->buf, msg->len);
+}
+
+/* Used for write only transactions */
+static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
+{
+	msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
+	msg->len++;
+}
+
+/* Return <0 on CRC error
+   If there was a write before this read (most cases) we need to take the
+   partial CRC from the write part into account.
+   Note that this function does modify the message (we need to decrease the
+   message length to hide the CRC byte from the caller). */
+static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
+{
+	u8 rpec = msg->buf[--msg->len];
+	cpec = i2c_smbus_msg_pec(cpec, msg);
+
+	if (rpec != cpec) {
+		pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
+			rpec, cpec);
+		return -EBADMSG;
+	}
+	return 0;
+}
+
+/**
+ * i2c_smbus_read_byte - SMBus "receive byte" protocol
+ * @client: Handle to slave device
+ *
+ * This executes the SMBus "receive byte" protocol, returning negative errno
+ * else the byte received from the device.
+ */
+s32 i2c_smbus_read_byte(const struct i2c_client *client)
+{
+	union i2c_smbus_data data;
+	int status;
+
+	status = i2c_smbus_xfer(client->adapter, client->addr, 0 /* client->flags */,
+				I2C_SMBUS_READ, 0,
+				I2C_SMBUS_BYTE, &data);
+	return (status < 0) ? status : data.byte;
+}
+EXPORT_SYMBOL(i2c_smbus_read_byte);
+
+/**
+ * i2c_smbus_write_byte - SMBus "send byte" protocol
+ * @client: Handle to slave device
+ * @value: Byte to be sent
+ *
+ * This executes the SMBus "send byte" protocol, returning negative errno
+ * else zero on success.
+ */
+s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
+{
+	return i2c_smbus_xfer(client->adapter, client->addr, 0 /* client->flags */,
+	                      I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
+}
+EXPORT_SYMBOL(i2c_smbus_write_byte);
+
+/**
+ * i2c_smbus_read_byte_data - SMBus "read byte" protocol
+ * @client: Handle to slave device
+ * @command: Byte interpreted by slave
+ *
+ * This executes the SMBus "read byte" protocol, returning negative errno
+ * else a data byte received from the device.
+ */
+s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
+{
+	union i2c_smbus_data data;
+	int status;
+
+	status = i2c_smbus_xfer(client->adapter, client->addr, 0 /* client->flags */,
+				I2C_SMBUS_READ, command,
+				I2C_SMBUS_BYTE_DATA, &data);
+	return (status < 0) ? status : data.byte;
+}
+EXPORT_SYMBOL(i2c_smbus_read_byte_data);
+
+/**
+ * i2c_smbus_write_byte_data - SMBus "write byte" protocol
+ * @client: Handle to slave device
+ * @command: Byte interpreted by slave
+ * @value: Byte being written
+ *
+ * This executes the SMBus "write byte" protocol, returning negative errno
+ * else zero on success.
+ */
+s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
+			      u8 value)
+{
+	union i2c_smbus_data data;
+	data.byte = value;
+	return i2c_smbus_xfer(client->adapter, client->addr, 0 /* client->flags */,
+			      I2C_SMBUS_WRITE, command,
+			      I2C_SMBUS_BYTE_DATA, &data);
+}
+EXPORT_SYMBOL(i2c_smbus_write_byte_data);
+
+/**
+ * i2c_smbus_read_word_data - SMBus "read word" protocol
+ * @client: Handle to slave device
+ * @command: Byte interpreted by slave
+ *
+ * This executes the SMBus "read word" protocol, returning negative errno
+ * else a 16-bit unsigned "word" received from the device.
+ */
+s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
+{
+	union i2c_smbus_data data;
+	int status;
+
+	status = i2c_smbus_xfer(client->adapter, client->addr, 0 /* client->flags */,
+				I2C_SMBUS_READ, command,
+				I2C_SMBUS_WORD_DATA, &data);
+	return (status < 0) ? status : data.word;
+}
+EXPORT_SYMBOL(i2c_smbus_read_word_data);
+
+/**
+ * i2c_smbus_write_word_data - SMBus "write word" protocol
+ * @client: Handle to slave device
+ * @command: Byte interpreted by slave
+ * @value: 16-bit "word" being written
+ *
+ * This executes the SMBus "write word" protocol, returning negative errno
+ * else zero on success.
+ */
+s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
+			      u16 value)
+{
+	union i2c_smbus_data data;
+	data.word = value;
+	return i2c_smbus_xfer(client->adapter, client->addr, 0 /* client->flags */,
+			      I2C_SMBUS_WRITE, command,
+			      I2C_SMBUS_WORD_DATA, &data);
+}
+EXPORT_SYMBOL(i2c_smbus_write_word_data);
+
+/* Returns the number of read bytes */
+s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
+				  u8 length, u8 *values)
+{
+	union i2c_smbus_data data;
+	int status;
+
+	if (length > I2C_SMBUS_BLOCK_MAX)
+		length = I2C_SMBUS_BLOCK_MAX;
+	data.block[0] = length;
+	status = i2c_smbus_xfer(client->adapter, client->addr, 0 /* client->flags */,
+				I2C_SMBUS_READ, command,
+				I2C_SMBUS_I2C_BLOCK_DATA, &data);
+	if (status < 0)
+		return status;
+
+	memcpy(values, &data.block[1], data.block[0]);
+	return data.block[0];
+}
+EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
+
+s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
+				   u8 length, const u8 *values)
+{
+	union i2c_smbus_data data;
+
+	if (length > I2C_SMBUS_BLOCK_MAX)
+		length = I2C_SMBUS_BLOCK_MAX;
+	data.block[0] = length;
+	memcpy(data.block + 1, values, length);
+	return i2c_smbus_xfer(client->adapter, client->addr, 0 /* client->flags */,
+			      I2C_SMBUS_WRITE, command,
+			      I2C_SMBUS_I2C_BLOCK_DATA, &data);
+}
+EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
+
+/* Simulate a SMBus command using the i2c protocol
+   No checking of parameters is done!  */
+static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
+				   unsigned short flags,
+				   char read_write, u8 command, int size,
+				   union i2c_smbus_data *data)
+{
+	/* So we need to generate a series of msgs. In the case of writing, we
+	  need to use only one message; when reading, we need two. We initialize
+	  most things with sane defaults, to keep the code below somewhat
+	  simpler. */
+	unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
+	unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
+	int num = read_write == I2C_SMBUS_READ ? 2 : 1;
+	int i;
+	u8 partial_pec = 0;
+	int status;
+	struct i2c_msg msg[2] = {
+		{
+			.addr = addr,
+			.flags = flags,
+			.len = 1,
+			.buf = msgbuf0,
+		}, {
+			.addr = addr,
+			.flags = flags | I2C_M_RD,
+			.len = 0,
+			.buf = msgbuf1,
+		},
+	};
+
+	msgbuf0[0] = command;
+	switch (size) {
+	case I2C_SMBUS_QUICK:
+		msg[0].len = 0;
+		/* Special case: The read/write field is used as data */
+		msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
+					I2C_M_RD : 0);
+		num = 1;
+		break;
+	case I2C_SMBUS_BYTE:
+		if (read_write == I2C_SMBUS_READ) {
+			/* Special case: only a read! */
+			msg[0].flags = I2C_M_RD | flags;
+			num = 1;
+		}
+		break;
+	case I2C_SMBUS_BYTE_DATA:
+		if (read_write == I2C_SMBUS_READ)
+			msg[1].len = 1;
+		else {
+			msg[0].len = 2;
+			msgbuf0[1] = data->byte;
+		}
+		break;
+	case I2C_SMBUS_WORD_DATA:
+		if (read_write == I2C_SMBUS_READ)
+			msg[1].len = 2;
+		else {
+			msg[0].len = 3;
+			msgbuf0[1] = data->word & 0xff;
+			msgbuf0[2] = data->word >> 8;
+		}
+		break;
+	case I2C_SMBUS_I2C_BLOCK_DATA:
+		if (read_write == I2C_SMBUS_READ) {
+			msg[1].len = data->block[0];
+		} else {
+			msg[0].len = data->block[0] + 1;
+			if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
+				dev_err(&adapter->dev,
+					"Invalid block write size %d\n",
+					data->block[0]);
+				return -EINVAL;
+			}
+			for (i = 1; i <= data->block[0]; i++)
+				msgbuf0[i] = data->block[i];
+		}
+		break;
+	default:
+		dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
+		return -EOPNOTSUPP;
+	}
+
+	i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
+				      && size != I2C_SMBUS_I2C_BLOCK_DATA);
+	if (i) {
+		/* Compute PEC if first message is a write */
+		if (!(msg[0].flags & I2C_M_RD)) {
+			if (num == 1) /* Write only */
+				i2c_smbus_add_pec(&msg[0]);
+			else /* Write followed by read */
+				partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
+		}
+		/* Ask for PEC if last message is a read */
+		if (msg[num-1].flags & I2C_M_RD)
+			msg[num-1].len++;
+	}
+
+	status = i2c_transfer(adapter, msg, num);
+	if (status < 0)
+		return status;
+
+	/* Check PEC if last message is a read */
+	if (i && (msg[num-1].flags & I2C_M_RD)) {
+		status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
+		if (status < 0)
+			return status;
+	}
+
+	if (read_write == I2C_SMBUS_READ)
+		switch (size) {
+		case I2C_SMBUS_BYTE:
+			data->byte = msgbuf0[0];
+			break;
+		case I2C_SMBUS_BYTE_DATA:
+			data->byte = msgbuf1[0];
+			break;
+		case I2C_SMBUS_WORD_DATA:
+			data->word = msgbuf1[0] | (msgbuf1[1] << 8);
+			break;
+		case I2C_SMBUS_I2C_BLOCK_DATA:
+			for (i = 0; i < data->block[0]; i++)
+				data->block[i+1] = msgbuf1[i];
+			break;
+		}
+	return 0;
+}
+
+/**
+ * i2c_smbus_xfer - execute SMBus protocol operations
+ * @adapter: Handle to I2C bus
+ * @addr: Address of SMBus slave on that bus
+ * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
+ * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
+ * @command: Byte interpreted by slave, for protocols which use such bytes
+ * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
+ * @data: Data to be read or written
+ *
+ * This executes an SMBus protocol operation, and returns a negative
+ * errno code else zero on success.
+ */
+s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
+		   char read_write, u8 command, int protocol,
+		   union i2c_smbus_data *data)
+{
+	s32 res;
+
+	flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
+
+	res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
+				      command, protocol, data);
+
+	return res;
+}
+EXPORT_SYMBOL(i2c_smbus_xfer);
+
 int i2c_read_reg(struct i2c_client *client, u32 addr, u8 *buf, u16 count)
 {
 	u8 msgbuf[2];
diff --git a/include/i2c/i2c.h b/include/i2c/i2c.h
index a107f5e..43038ec 100644
--- a/include/i2c/i2c.h
+++ b/include/i2c/i2c.h
@@ -85,6 +85,70 @@ struct i2c_client {
 
 #define to_i2c_client(a)	container_of(a, struct i2c_client, dev)
 
+/*flags for the client struct: */
+#define I2C_CLIENT_PEC	0x04		/* Use Packet Error Checking */
+#define I2C_CLIENT_TEN	0x10		/* we have a ten bit chip address */
+					/* Must equal I2C_M_TEN below */
+#define I2C_CLIENT_WAKE	0x80		/* for board_info; true iff can wake */
+#define I2C_CLIENT_SCCB	0x9000		/* Use Omnivision SCCB protocol */
+					/* Must match I2C_M_STOP|IGNORE_NAK */
+
+/*
+ * Data for SMBus Messages
+ */
+#define I2C_SMBUS_BLOCK_MAX	32	/* As specified in SMBus standard */
+union i2c_smbus_data {
+	__u8 byte;
+	__u16 word;
+	__u8 block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */
+			       /* and one more for user-space compatibility */
+};
+
+/* i2c_smbus_xfer read or write markers */
+#define I2C_SMBUS_READ	1
+#define I2C_SMBUS_WRITE	0
+
+/* SMBus transaction types (size parameter in the above functions)
+   Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */
+#define I2C_SMBUS_QUICK		    0
+#define I2C_SMBUS_BYTE		    1
+#define I2C_SMBUS_BYTE_DATA	    2
+#define I2C_SMBUS_WORD_DATA	    3
+#define I2C_SMBUS_PROC_CALL	    4
+#define I2C_SMBUS_BLOCK_DATA	    5
+#define I2C_SMBUS_I2C_BLOCK_BROKEN  6
+#define I2C_SMBUS_BLOCK_PROC_CALL   7		/* SMBus 2.0 */
+#define I2C_SMBUS_I2C_BLOCK_DATA    8
+
+/* This is the very generalized SMBus access routine. You probably do not
+   want to use this, though; one of the functions below may be much easier,
+   and probably just as fast.
+   Note that we use i2c_adapter here, because you do not need a specific
+   smbus adapter to call this function. */
+extern s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
+			  unsigned short flags, char read_write, u8 command,
+			  int size, union i2c_smbus_data *data);
+
+/* Now follow the 'nice' access routines. These also document the calling
+   conventions of i2c_smbus_xfer. */
+
+extern s32 i2c_smbus_read_byte(const struct i2c_client *client);
+extern s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value);
+extern s32 i2c_smbus_read_byte_data(const struct i2c_client *client,
+				    u8 command);
+extern s32 i2c_smbus_write_byte_data(const struct i2c_client *client,
+				     u8 command, u8 value);
+extern s32 i2c_smbus_read_word_data(const struct i2c_client *client,
+				    u8 command);
+extern s32 i2c_smbus_write_word_data(const struct i2c_client *client,
+				     u8 command, u16 value);
+
+/* Returns the number of read bytes */
+extern s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client,
+					 u8 command, u8 length, u8 *values);
+extern s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client,
+					  u8 command, u8 length,
+					  const u8 *values);
 
 /**
  * struct i2c_board_info - template for device creation
-- 
2.0.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [RFC 4/5] rtc: add ds1307 support
  2014-07-10  8:33 [RFC 0/5] add rtc support Antony Pavlov
                   ` (2 preceding siblings ...)
  2014-07-10  8:33 ` [RFC 3/5] i2c: import SMBus stuff from linux Antony Pavlov
@ 2014-07-10  8:33 ` Antony Pavlov
  2014-07-10  8:33 ` [RFC 5/5] commands: add hwclock Antony Pavlov
  2014-07-11  5:37 ` [RFC 0/5] add rtc support Sascha Hauer
  5 siblings, 0 replies; 17+ messages in thread
From: Antony Pavlov @ 2014-07-10  8:33 UTC (permalink / raw)
  To: barebox

Signed-off-by: Antony Pavlov <antonynpavlov@gmail.com>
---
 drivers/rtc/Kconfig      |  29 +++++
 drivers/rtc/Makefile     |   4 +
 drivers/rtc/rtc-ds1307.c | 315 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 348 insertions(+)

diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index f148266..8771933 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -14,3 +14,32 @@ menuconfig RTC_CLASS
           Generic RTC class support. If you say yes here, you will
           be allowed to plug one or more RTCs to your system. You will
           probably want to enable one or more of the interfaces below.
+
+if RTC_CLASS
+
+comment "I2C RTC drivers"
+        depends on I2C
+
+if I2C
+
+config RTC_DRV_DS1307
+        tristate "Dallas/Maxim DS1307/37/38/39/40, ST M41T00, EPSON RX-8025"
+        help
+          If you say yes here you get support for various compatible RTC
+          chips (often with battery backup) connected with I2C. This driver
+          should handle DS1307, DS1337, DS1338, DS1339, DS1340, ST M41T00,
+          EPSON RX-8025 and probably other chips. In some cases the RTC
+          must already have been initialized (by manufacturing or a
+          bootloader).
+
+          The first seven registers on these chips hold an RTC, and other
+          registers may add features such as NVRAM, a trickle charger for
+          the RTC/NVRAM backup power, and alarms. NVRAM is visible in
+          sysfs, but other chip features may not be available.
+
+          This driver can also be built as a module. If so, the module
+          will be called rtc-ds1307.
+
+endif # I2C
+
+endif # RTC_CLASS
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index 7d1b5bc..2c5a50e 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -4,3 +4,7 @@
 
 obj-$(CONFIG_RTC_LIB)           += rtc-lib.o
 obj-$(CONFIG_RTC_CLASS)         += class.o
+
+# Keep the list ordered.
+
+obj-$(CONFIG_RTC_DRV_DS1307)    += rtc-ds1307.o
diff --git a/drivers/rtc/rtc-ds1307.c b/drivers/rtc/rtc-ds1307.c
new file mode 100644
index 0000000..0c918f1
--- /dev/null
+++ b/drivers/rtc/rtc-ds1307.c
@@ -0,0 +1,315 @@
+/*
+ * rtc-ds1307.c - RTC driver for some mostly-compatible I2C chips.
+ *
+ *  Copyright (C) 2005 James Chapman (ds1337 core)
+ *  Copyright (C) 2006 David Brownell
+ *  Copyright (C) 2009 Matthias Fuchs (rx8025 support)
+ *  Copyright (C) 2012 Bertrand Achard (nvram access fixes)
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <common.h>
+#include <init.h>
+#include <driver.h>
+#include <xfuncs.h>
+#include <errno.h>
+#include <i2c/i2c.h>
+#include <rtc.h>
+#include <linux/rtc.h>
+#include <linux/bcd.h>
+
+/*
+ * We can't determine type by probing, but if we expect pre-Linux code
+ * to have set the chip up as a clock (turning on the oscillator and
+ * setting the date and time), Linux can ignore the non-clock features.
+ * That's a natural job for a factory or repair bench.
+ */
+enum ds_type {
+	ds_1307,
+	last_ds_type /* always last */
+};
+
+/* RTC registers don't differ much, except for the century flag */
+#define DS1307_REG_SECS		0x00	/* 00-59 */
+#	define DS1307_BIT_CH		0x80
+#	define DS1340_BIT_nEOSC		0x80
+#define DS1307_REG_MIN		0x01	/* 00-59 */
+#define DS1307_REG_HOUR		0x02	/* 00-23, or 1-12{am,pm} */
+#	define DS1307_BIT_12HR		0x40	/* in REG_HOUR */
+#	define DS1307_BIT_PM		0x20	/* in REG_HOUR */
+#	define DS1340_BIT_CENTURY_EN	0x80	/* in REG_HOUR */
+#	define DS1340_BIT_CENTURY	0x40	/* in REG_HOUR */
+#define DS1307_REG_WDAY		0x03	/* 01-07 */
+#define DS1307_REG_MDAY		0x04	/* 01-31 */
+#define DS1307_REG_MONTH	0x05	/* 01-12 */
+#	define DS1337_BIT_CENTURY	0x80	/* in REG_MONTH */
+#define DS1307_REG_YEAR		0x06	/* 00-99 */
+
+/*
+ * Other registers (control, status, alarms, trickle charge, NVRAM, etc)
+ * start at 7, and they differ a LOT. Only control and status matter for
+ * basic RTC date and time functionality; be careful using them.
+ */
+#define DS1307_REG_CONTROL	0x07		/* or ds1338 */
+#	define DS1307_BIT_OUT		0x80
+#	define DS1338_BIT_OSF		0x20
+#	define DS1307_BIT_SQWE		0x10
+#	define DS1307_BIT_RS1		0x02
+#	define DS1307_BIT_RS0		0x01
+
+struct ds1307 {
+	struct rtc_device	rtc;
+	u8			offset; /* register's offset */
+	u8			regs[11];
+	enum ds_type		type;
+	unsigned long		flags;
+	struct i2c_client	*client;
+	s32 (*read_block_data)(const struct i2c_client *client, u8 command,
+			       u8 length, u8 *values);
+	s32 (*write_block_data)(const struct i2c_client *client, u8 command,
+				u8 length, const u8 *values);
+};
+
+static struct platform_device_id ds1307_id[] = {
+	{ "ds1307", ds_1307 },
+	{ "pt7c4338", ds_1307 },
+	{ }
+};
+
+/*----------------------------------------------------------------------*/
+
+#define BLOCK_DATA_MAX_TRIES 10
+
+static s32 ds1307_read_block_data_once(const struct i2c_client *client,
+				       u8 command, u8 length, u8 *values)
+{
+	s32 i, data;
+
+	for (i = 0; i < length; i++) {
+		data = i2c_smbus_read_byte_data(client, command + i);
+		if (data < 0)
+			return data;
+		values[i] = data;
+	}
+	return i;
+}
+
+static s32 ds1307_read_block_data(const struct i2c_client *client, u8 command,
+				  u8 length, u8 *values)
+{
+	u8 oldvalues[255];
+	s32 ret;
+	int tries = 0;
+
+	dev_dbg(&client->dev, "ds1307_read_block_data (length=%d)\n", length);
+	ret = ds1307_read_block_data_once(client, command, length, values);
+	if (ret < 0)
+		return ret;
+	do {
+		if (++tries > BLOCK_DATA_MAX_TRIES) {
+			dev_err(&client->dev,
+				"ds1307_read_block_data failed\n");
+			return -EIO;
+		}
+		memcpy(oldvalues, values, length);
+		ret = ds1307_read_block_data_once(client, command, length,
+						  values);
+		if (ret < 0)
+			return ret;
+	} while (memcmp(oldvalues, values, length));
+	return length;
+}
+
+static s32 ds1307_write_block_data(const struct i2c_client *client, u8 command,
+				   u8 length, const u8 *values)
+{
+	u8 currvalues[255];
+	int tries = 0;
+
+	dev_dbg(&client->dev, "ds1307_write_block_data (length=%d)\n", length);
+	do {
+		s32 i, ret;
+
+		if (++tries > BLOCK_DATA_MAX_TRIES) {
+			dev_err(&client->dev,
+				"ds1307_write_block_data failed\n");
+			return -EIO;
+		}
+		for (i = 0; i < length; i++) {
+			ret = i2c_smbus_write_byte_data(client, command + i,
+							values[i]);
+			if (ret < 0)
+				return ret;
+		}
+		ret = ds1307_read_block_data_once(client, command, length,
+						  currvalues);
+		if (ret < 0)
+			return ret;
+	} while (memcmp(currvalues, values, length));
+	return length;
+}
+
+static inline struct ds1307 *to_ds1307_priv(struct rtc_device *rtcdev)
+{
+	return container_of(rtcdev, struct ds1307, rtc);
+}
+
+static int ds1307_get_time(struct rtc_device *rtcdev, struct rtc_time *t)
+{
+	struct device_d *dev = rtcdev->dev;
+	struct ds1307 *ds1307 = to_ds1307_priv(rtcdev);
+	int		tmp;
+
+	/* read the RTC date and time registers all at once */
+	tmp = ds1307->read_block_data(ds1307->client,
+		ds1307->offset, 7, ds1307->regs);
+	if (tmp != 7) {
+		dev_err(dev, "%s error %d\n", "read", tmp);
+		return -EIO;
+	}
+
+	dev_dbg(dev, "%s: %7ph\n", "read", ds1307->regs);
+
+	t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f);
+	t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
+	tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
+	t->tm_hour = bcd2bin(tmp);
+	t->tm_wday = bcd2bin(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
+	t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
+	tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
+	t->tm_mon = bcd2bin(tmp) - 1;
+
+	/* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */
+	t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
+
+	dev_dbg(dev, "%s secs=%d, mins=%d, "
+		"hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
+		"read", t->tm_sec, t->tm_min,
+		t->tm_hour, t->tm_mday,
+		t->tm_mon, t->tm_year, t->tm_wday);
+
+	/* initial clock setting can be undefined */
+	return rtc_valid_tm(t);
+}
+
+static int ds1307_set_time(struct rtc_device *rtcdev, struct rtc_time *t)
+{
+	struct device_d *dev = rtcdev->dev;
+	struct ds1307 *ds1307 = to_ds1307_priv(rtcdev);
+	int		result;
+	int		tmp;
+	u8		*buf = ds1307->regs;
+
+	dev_dbg(dev, "%s secs=%d, mins=%d, "
+		"hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
+		"write", t->tm_sec, t->tm_min,
+		t->tm_hour, t->tm_mday,
+		t->tm_mon, t->tm_year, t->tm_wday);
+
+	buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
+	buf[DS1307_REG_MIN] = bin2bcd(t->tm_min);
+	buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
+	buf[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
+	buf[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
+	buf[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
+
+	/* assume 20YY not 19YY */
+	tmp = t->tm_year - 100;
+	buf[DS1307_REG_YEAR] = bin2bcd(tmp);
+
+	dev_dbg(dev, "%s: %7ph\n", "write", buf);
+
+	result = ds1307->write_block_data(ds1307->client,
+		ds1307->offset, 7, buf);
+	if (result < 0) {
+		dev_err(dev, "%s error %d\n", "write", result);
+		return result;
+	}
+	return 0;
+}
+
+static const struct rtc_class_ops ds13xx_rtc_ops = {
+	.read_time	= ds1307_get_time,
+	.set_time	= ds1307_set_time,
+};
+
+static int ds1307_probe(struct device_d *dev)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct ds1307		*ds1307;
+	int			err = -ENODEV;
+	int			tmp;
+	unsigned char		*buf;
+	unsigned long driver_data;
+
+	ds1307 = xzalloc(sizeof(struct ds1307));
+
+	ds1307->client= client;
+
+	err = dev_get_drvdata(dev, &driver_data);
+	if (err)
+		goto exit;
+
+	ds1307->type = driver_data;
+
+	buf = ds1307->regs;
+
+	ds1307->read_block_data = ds1307_read_block_data;
+	ds1307->write_block_data = ds1307_write_block_data;
+
+	/* read RTC registers */
+	tmp = ds1307->read_block_data(ds1307->client, ds1307->offset, 8, buf);
+	if (tmp != 8) {
+		dev_dbg(&client->dev, "read error %d\n", tmp);
+		err = -EIO;
+		goto exit;
+	}
+
+	/*
+	 * minimal sanity checking; some chips (like DS1340) don't
+	 * specify the extra bits as must-be-zero, but there are
+	 * still a few values that are clearly out-of-range.
+	 */
+	tmp = ds1307->regs[DS1307_REG_HOUR];
+	switch (ds1307->type) {
+	default:
+		if (!(tmp & DS1307_BIT_12HR))
+			break;
+
+		/*
+		 * Be sure we're in 24 hour mode.  Multi-master systems
+		 * take note...
+		 */
+		tmp = bcd2bin(tmp & 0x1f);
+		if (tmp == 12)
+			tmp = 0;
+		if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
+			tmp += 12;
+		i2c_smbus_write_byte_data(client,
+				ds1307->offset + DS1307_REG_HOUR,
+				bin2bcd(tmp));
+	}
+
+	ds1307->rtc.ops = &ds13xx_rtc_ops;
+	ds1307->rtc.dev = dev;
+
+	err = rtc_register(&ds1307->rtc);
+
+exit:
+	return err;
+}
+
+static struct driver_d ds1307_driver = {
+	.name	= "rtc-ds1307",
+	.probe		= ds1307_probe,
+	.id_table	= ds1307_id,
+};
+
+static int __init ds1307_init(void)
+{
+	return i2c_driver_register(&ds1307_driver);
+}
+device_initcall(ds1307_init);
-- 
2.0.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [RFC 5/5] commands: add hwclock
  2014-07-10  8:33 [RFC 0/5] add rtc support Antony Pavlov
                   ` (3 preceding siblings ...)
  2014-07-10  8:33 ` [RFC 4/5] rtc: add ds1307 support Antony Pavlov
@ 2014-07-10  8:33 ` Antony Pavlov
  2014-07-10 21:45   ` Sascha Hauer
  2014-07-11  5:37 ` [RFC 0/5] add rtc support Sascha Hauer
  5 siblings, 1 reply; 17+ messages in thread
From: Antony Pavlov @ 2014-07-10  8:33 UTC (permalink / raw)
  To: barebox

Signed-off-by: Antony Pavlov <antonynpavlov@gmail.com>
---
 commands/Kconfig   |  8 ++++++++
 commands/Makefile  |  1 +
 commands/hwclock.c | 37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+)

diff --git a/commands/Kconfig b/commands/Kconfig
index 61816f5..a594f7c 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -1691,6 +1691,14 @@ config CMD_GPIO
 
 	  Usage: gpio_set_value GPIO VALUE
 
+config CMD_HWCLOCK
+	bool
+	depends on RTC_CLASS
+	prompt "hwclock command"
+	default y
+	help
+	  The lspci command allows to query or set the hardware clock (RTC).
+
 config CMD_I2C
 	bool
 	depends on I2C
diff --git a/commands/Makefile b/commands/Makefile
index d42aca5..44dd9d4 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -100,3 +100,4 @@ obj-$(CONFIG_CMD_MENUTREE)	+= menutree.o
 obj-$(CONFIG_CMD_2048)		+= 2048.o
 obj-$(CONFIG_CMD_REGULATOR)	+= regulator.o
 obj-$(CONFIG_CMD_LSPCI)		+= lspci.o
+obj-$(CONFIG_CMD_HWCLOCK)	+= hwclock.o
diff --git a/commands/hwclock.c b/commands/hwclock.c
new file mode 100644
index 0000000..4671fee
--- /dev/null
+++ b/commands/hwclock.c
@@ -0,0 +1,37 @@
+#include <common.h>
+#include <command.h>
+#include <getopt.h>
+#include <linux/err.h>
+#include <rtc.h>
+#include <linux/rtc.h>
+
+static int do_hwclock(int argc, char *argv[])
+{
+	struct rtc_device *r;
+	struct rtc_time tm;
+	char *rtc_name;
+
+	rtc_name = "rtc0";
+
+	if (argc > 1)
+		rtc_name = argv[1];
+
+	r = rtc_lookup(rtc_name);
+	if (IS_ERR(r))
+		return PTR_ERR(r);
+
+	rtc_read_time(r, &tm);
+
+	printf("%02d:%02d:%02d %02d-%02d-%04d\n",
+		tm.tm_hour, tm.tm_min, tm.tm_sec,
+		tm.tm_mday, tm.tm_mon, tm.tm_year);
+
+	return 0;
+}
+
+BAREBOX_CMD_START(hwclock)
+	.cmd		= do_hwclock,
+	BAREBOX_CMD_DESC("query or set the hardware clock (RTC)")
+	BAREBOX_CMD_OPTS("[RTC]")
+	BAREBOX_CMD_GROUP(CMD_GRP_HWMANIP)
+BAREBOX_CMD_END
-- 
2.0.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC 2/5] add rtc support
  2014-07-10  8:33 ` [RFC 2/5] add rtc support Antony Pavlov
@ 2014-07-10 21:40   ` Sascha Hauer
  2014-07-11  5:31     ` Antony Pavlov
  0 siblings, 1 reply; 17+ messages in thread
From: Sascha Hauer @ 2014-07-10 21:40 UTC (permalink / raw)
  To: Antony Pavlov; +Cc: barebox

On Thu, Jul 10, 2014 at 12:33:16PM +0400, Antony Pavlov wrote:
> Signed-off-by: Antony Pavlov <antonynpavlov@gmail.com>
> ---
>  drivers/Kconfig       |  1 +
>  drivers/Makefile      |  1 +
>  drivers/rtc/Kconfig   | 16 +++++++++++++
>  drivers/rtc/Makefile  |  6 +++++
>  drivers/rtc/class.c   | 62 +++++++++++++++++++++++++++++++++++++++++++++++++
>  drivers/rtc/rtc-lib.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  include/linux/rtc.h   | 46 ++++++++++++++++++++++++++++++++++++
>  include/rtc.h         |  2 ++
>  8 files changed, 198 insertions(+)
> 
> diff --git a/drivers/Kconfig b/drivers/Kconfig
> index 12a9d8c..d38032c 100644
> --- a/drivers/Kconfig
> +++ b/drivers/Kconfig
> @@ -28,5 +28,6 @@ source "drivers/bus/Kconfig"
>  source "drivers/regulator/Kconfig"
>  source "drivers/reset/Kconfig"
>  source "drivers/pci/Kconfig"
> +source "drivers/rtc/Kconfig"
>  
>  endmenu
> diff --git a/drivers/Makefile b/drivers/Makefile
> index 1990e86..4591f9a 100644
> --- a/drivers/Makefile
> +++ b/drivers/Makefile
> @@ -27,3 +27,4 @@ obj-y += bus/
>  obj-$(CONFIG_REGULATOR) += regulator/
>  obj-$(CONFIG_RESET_CONTROLLER) += reset/
>  obj-$(CONFIG_PCI) += pci/
> +obj-y += rtc/
> diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
> new file mode 100644
> index 0000000..f148266
> --- /dev/null
> +++ b/drivers/rtc/Kconfig
> @@ -0,0 +1,16 @@
> +#
> +# RTC class/drivers configuration
> +#
> +
> +config RTC_LIB
> +	bool
> +
> +menuconfig RTC_CLASS
> +        bool "Real Time Clock"
> +        default n
> +        depends on !S390 && !UML
> +        select RTC_LIB
> +        help
> +          Generic RTC class support. If you say yes here, you will
> +          be allowed to plug one or more RTCs to your system. You will
> +          probably want to enable one or more of the interfaces below.
> diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
> new file mode 100644
> index 0000000..7d1b5bc
> --- /dev/null
> +++ b/drivers/rtc/Makefile
> @@ -0,0 +1,6 @@
> +#
> +# Makefile for RTC class/drivers.
> +#
> +
> +obj-$(CONFIG_RTC_LIB)           += rtc-lib.o
> +obj-$(CONFIG_RTC_CLASS)         += class.o
> diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c
> new file mode 100644
> index 0000000..b808f3d
> --- /dev/null
> +++ b/drivers/rtc/class.c
> @@ -0,0 +1,62 @@
> +#include <linux/err.h>
> +#include <rtc.h>
> +#include <linux/rtc.h>
> +
> +LIST_HEAD(rtc_list);
> +EXPORT_SYMBOL(rtc_list);
> +
> +struct rtc_device *rtc_lookup(const char *name)
> +{
> +	struct rtc_device *r;
> +
> +	if (!name)
> +		return ERR_PTR(-ENODEV);
> +
> +	list_for_each_entry(r, &rtc_list, list) {
> +		if (!strcmp(dev_name(&r->class_dev), name))
> +			return r;
> +	}
> +
> +	return ERR_PTR(-ENODEV);
> +}
> +EXPORT_SYMBOL(rtc_lookup);
> +
> +int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
> +{
> +	return rtc->ops->read_time(rtc, tm);
> +}
> +EXPORT_SYMBOL(rtc_read_time);
> +
> +int rtc_register(struct rtc_device *rtcdev)
> +{
> +	struct device_d *dev = &rtcdev->class_dev;
> +
> +	dev->id = DEVICE_ID_DYNAMIC;
> +	strcpy(dev->name, "rtc");
> +	if (rtcdev->dev)
> +		dev->parent = rtcdev->dev;
> +	platform_device_register(dev);

Please check the return value.

> +
> +	list_add_tail(&rtcdev->list, &rtc_list);
> +
> +	return 0;
> +}
> +EXPORT_SYMBOL(rtc_register);
> +
> +#if 0
> +int rtc_unregister(struct rtc_device *cdev)
> +{
> +	struct device_d *dev = &cdev->class_dev;
> +	int status;
> +
> +	list_del(&cdev->list);
> +	if (list_empty(&rtc_list))
> +		initialized = CONSOLE_UNINITIALIZED;
> +
> +	status = unregister_device(dev);
> +	if (!status)
> +		memset(cdev, 0, sizeof(*cdev));
> +	return status;
> +}
> +EXPORT_SYMBOL(rtc_unregister);
> +#endif

CONSOLE_UNINITIALIZED? This probably wouldn't compile if it wasn't
ifdeffed. I think we can just remove this.

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC 5/5] commands: add hwclock
  2014-07-10  8:33 ` [RFC 5/5] commands: add hwclock Antony Pavlov
@ 2014-07-10 21:45   ` Sascha Hauer
  2014-07-11  5:59     ` Antony Pavlov
  0 siblings, 1 reply; 17+ messages in thread
From: Sascha Hauer @ 2014-07-10 21:45 UTC (permalink / raw)
  To: Antony Pavlov; +Cc: barebox

On Thu, Jul 10, 2014 at 12:33:19PM +0400, Antony Pavlov wrote:
> Signed-off-by: Antony Pavlov <antonynpavlov@gmail.com>
> ---
>  commands/Kconfig   |  8 ++++++++
>  commands/Makefile  |  1 +
>  commands/hwclock.c | 37 +++++++++++++++++++++++++++++++++++++
>  3 files changed, 46 insertions(+)
> 
> diff --git a/commands/Kconfig b/commands/Kconfig
> index 61816f5..a594f7c 100644
> --- a/commands/Kconfig
> +++ b/commands/Kconfig
> @@ -1691,6 +1691,14 @@ config CMD_GPIO
>  
>  	  Usage: gpio_set_value GPIO VALUE
>  
> +config CMD_HWCLOCK
> +	bool
> +	depends on RTC_CLASS
> +	prompt "hwclock command"
> +	default y
> +	help
> +	  The lspci command allows to query or set the hardware clock (RTC).

lspci? ;)

> +static int do_hwclock(int argc, char *argv[])
> +{
> +	struct rtc_device *r;
> +	struct rtc_time tm;
> +	char *rtc_name;
> +
> +	rtc_name = "rtc0";
> +
> +	if (argc > 1)
> +		rtc_name = argv[1];
> +
> +	r = rtc_lookup(rtc_name);
> +	if (IS_ERR(r))
> +		return PTR_ERR(r);
> +
> +	rtc_read_time(r, &tm);
> +
> +	printf("%02d:%02d:%02d %02d-%02d-%04d\n",
> +		tm.tm_hour, tm.tm_min, tm.tm_sec,
> +		tm.tm_mday, tm.tm_mon, tm.tm_year);
> +
> +	return 0;
> +}
> +
> +BAREBOX_CMD_START(hwclock)
> +	.cmd		= do_hwclock,
> +	BAREBOX_CMD_DESC("query or set the hardware clock (RTC)")

This initial version doesn't allow to set the time.

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC 2/5] add rtc support
  2014-07-10 21:40   ` Sascha Hauer
@ 2014-07-11  5:31     ` Antony Pavlov
  0 siblings, 0 replies; 17+ messages in thread
From: Antony Pavlov @ 2014-07-11  5:31 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On Thu, 10 Jul 2014 23:40:30 +0200
Sascha Hauer <s.hauer@pengutronix.de> wrote:

> On Thu, Jul 10, 2014 at 12:33:16PM +0400, Antony Pavlov wrote:
> > Signed-off-by: Antony Pavlov <antonynpavlov@gmail.com>
> > ---
> >  drivers/Kconfig       |  1 +
> >  drivers/Makefile      |  1 +
> >  drivers/rtc/Kconfig   | 16 +++++++++++++
> >  drivers/rtc/Makefile  |  6 +++++
> >  drivers/rtc/class.c   | 62 +++++++++++++++++++++++++++++++++++++++++++++++++
> >  drivers/rtc/rtc-lib.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++
> >  include/linux/rtc.h   | 46 ++++++++++++++++++++++++++++++++++++
> >  include/rtc.h         |  2 ++
> >  8 files changed, 198 insertions(+)
> > 
> > diff --git a/drivers/Kconfig b/drivers/Kconfig
> > index 12a9d8c..d38032c 100644
> > --- a/drivers/Kconfig
> > +++ b/drivers/Kconfig
> > @@ -28,5 +28,6 @@ source "drivers/bus/Kconfig"
> >  source "drivers/regulator/Kconfig"
> >  source "drivers/reset/Kconfig"
> >  source "drivers/pci/Kconfig"
> > +source "drivers/rtc/Kconfig"
> >  
> >  endmenu
> > diff --git a/drivers/Makefile b/drivers/Makefile
> > index 1990e86..4591f9a 100644
> > --- a/drivers/Makefile
> > +++ b/drivers/Makefile
> > @@ -27,3 +27,4 @@ obj-y += bus/
> >  obj-$(CONFIG_REGULATOR) += regulator/
> >  obj-$(CONFIG_RESET_CONTROLLER) += reset/
> >  obj-$(CONFIG_PCI) += pci/
> > +obj-y += rtc/
> > diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
> > new file mode 100644
> > index 0000000..f148266
> > --- /dev/null
> > +++ b/drivers/rtc/Kconfig
> > @@ -0,0 +1,16 @@
> > +#
> > +# RTC class/drivers configuration
> > +#
> > +
> > +config RTC_LIB
> > +	bool
> > +
> > +menuconfig RTC_CLASS
> > +        bool "Real Time Clock"
> > +        default n
> > +        depends on !S390 && !UML

I suppose i can drop thess two lines.

> > +        select RTC_LIB
> > +        help
> > +          Generic RTC class support. If you say yes here, you will
> > +          be allowed to plug one or more RTCs to your system. You will
> > +          probably want to enable one or more of the interfaces below.
> > diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
> > new file mode 100644
> > index 0000000..7d1b5bc
> > --- /dev/null
> > +++ b/drivers/rtc/Makefile
> > @@ -0,0 +1,6 @@
> > +#
> > +# Makefile for RTC class/drivers.
> > +#
> > +
> > +obj-$(CONFIG_RTC_LIB)           += rtc-lib.o
> > +obj-$(CONFIG_RTC_CLASS)         += class.o
> > diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c
> > new file mode 100644
> > index 0000000..b808f3d
> > --- /dev/null
> > +++ b/drivers/rtc/class.c
> > @@ -0,0 +1,62 @@
> > +#include <linux/err.h>
> > +#include <rtc.h>
> > +#include <linux/rtc.h>
> > +
> > +LIST_HEAD(rtc_list);
> > +EXPORT_SYMBOL(rtc_list);
> > +
> > +struct rtc_device *rtc_lookup(const char *name)
> > +{
> > +	struct rtc_device *r;
> > +
> > +	if (!name)
> > +		return ERR_PTR(-ENODEV);
> > +
> > +	list_for_each_entry(r, &rtc_list, list) {
> > +		if (!strcmp(dev_name(&r->class_dev), name))
> > +			return r;
> > +	}
> > +
> > +	return ERR_PTR(-ENODEV);
> > +}
> > +EXPORT_SYMBOL(rtc_lookup);
> > +
> > +int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
> > +{
> > +	return rtc->ops->read_time(rtc, tm);
> > +}
> > +EXPORT_SYMBOL(rtc_read_time);
> > +
> > +int rtc_register(struct rtc_device *rtcdev)
> > +{
> > +	struct device_d *dev = &rtcdev->class_dev;
> > +
> > +	dev->id = DEVICE_ID_DYNAMIC;
> > +	strcpy(dev->name, "rtc");
> > +	if (rtcdev->dev)
> > +		dev->parent = rtcdev->dev;
> > +	platform_device_register(dev);
> 
> Please check the return value.
> 
> > +
> > +	list_add_tail(&rtcdev->list, &rtc_list);
> > +
> > +	return 0;
> > +}
> > +EXPORT_SYMBOL(rtc_register);
> > +
> > +#if 0
> > +int rtc_unregister(struct rtc_device *cdev)
> > +{
> > +	struct device_d *dev = &cdev->class_dev;
> > +	int status;
> > +
> > +	list_del(&cdev->list);
> > +	if (list_empty(&rtc_list))
> > +		initialized = CONSOLE_UNINITIALIZED;
> > +
> > +	status = unregister_device(dev);
> > +	if (!status)
> > +		memset(cdev, 0, sizeof(*cdev));
> > +	return status;
> > +}
> > +EXPORT_SYMBOL(rtc_unregister);
> > +#endif
> 
> CONSOLE_UNINITIALIZED? This probably wouldn't compile if it wasn't
> ifdeffed. I think we can just remove this.

Please see TODOs in the '[RFC 0/5] add rtc support' message:

 * rtc_unregister() is not realized.

I suppose that most (but not all) of RTC chips does not need any deinitialization routines.
So I have not realized the rtc_unregister() routine.


-- 
Best regards,
  Antony Pavlov

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC 0/5] add rtc support
  2014-07-10  8:33 [RFC 0/5] add rtc support Antony Pavlov
                   ` (4 preceding siblings ...)
  2014-07-10  8:33 ` [RFC 5/5] commands: add hwclock Antony Pavlov
@ 2014-07-11  5:37 ` Sascha Hauer
  2014-07-11  6:12   ` Antony Pavlov
  5 siblings, 1 reply; 17+ messages in thread
From: Sascha Hauer @ 2014-07-11  5:37 UTC (permalink / raw)
  To: Antony Pavlov; +Cc: barebox

On Thu, Jul 10, 2014 at 12:33:14PM +0400, Antony Pavlov wrote:
> This patchseries imports RTC support from linux-3.15.
> 
> User can use 'hwclock' command to see realtime clock readout.
> 
> Tested with DS1307.
> 
> TODOs:
> 
>  * rtc_set_time() is not realized;
>  * ds1307_set_time() is not tested;
>  * bcd lib is compiled in even if rtc support is disabled (as linux does);
>    we can make it optional;

It's probably not worth it. The linker will throw it away anyway if
unused.

>  * adjust include/linux/rtc.h and include/rtc.h;
>  * rtc_unregister() is not realized.

Maybe an environment variable which holds the seconds since epoch would
be useful. This way scripts could make use of the current RTC time.

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC 5/5] commands: add hwclock
  2014-07-10 21:45   ` Sascha Hauer
@ 2014-07-11  5:59     ` Antony Pavlov
  2014-07-11  6:07       ` Sascha Hauer
  0 siblings, 1 reply; 17+ messages in thread
From: Antony Pavlov @ 2014-07-11  5:59 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On Thu, 10 Jul 2014 23:45:47 +0200
Sascha Hauer <s.hauer@pengutronix.de> wrote:

> On Thu, Jul 10, 2014 at 12:33:19PM +0400, Antony Pavlov wrote:
> > Signed-off-by: Antony Pavlov <antonynpavlov@gmail.com>
> > ---
> >  commands/Kconfig   |  8 ++++++++
> >  commands/Makefile  |  1 +
> >  commands/hwclock.c | 37 +++++++++++++++++++++++++++++++++++++
> >  3 files changed, 46 insertions(+)
> > 
> > diff --git a/commands/Kconfig b/commands/Kconfig
> > index 61816f5..a594f7c 100644
> > --- a/commands/Kconfig
> > +++ b/commands/Kconfig
> > @@ -1691,6 +1691,14 @@ config CMD_GPIO
> >  
> >  	  Usage: gpio_set_value GPIO VALUE
> >  
> > +config CMD_HWCLOCK
> > +	bool
> > +	depends on RTC_CLASS
> > +	prompt "hwclock command"
> > +	default y
> > +	help
> > +	  The lspci command allows to query or set the hardware clock (RTC).
> 
> lspci? ;)

No!
copy & paste!

> > +static int do_hwclock(int argc, char *argv[])
> > +{
> > +	struct rtc_device *r;
> > +	struct rtc_time tm;
> > +	char *rtc_name;
> > +
> > +	rtc_name = "rtc0";
> > +
> > +	if (argc > 1)
> > +		rtc_name = argv[1];
> > +
> > +	r = rtc_lookup(rtc_name);
> > +	if (IS_ERR(r))
> > +		return PTR_ERR(r);
> > +
> > +	rtc_read_time(r, &tm);
> > +
> > +	printf("%02d:%02d:%02d %02d-%02d-%04d\n",
> > +		tm.tm_hour, tm.tm_min, tm.tm_sec,
> > +		tm.tm_mday, tm.tm_mon, tm.tm_year);
> > +
> > +	return 0;
> > +}
> > +
> > +BAREBOX_CMD_START(hwclock)
> > +	.cmd		= do_hwclock,
> > +	BAREBOX_CMD_DESC("query or set the hardware clock (RTC)")
> 
> This initial version doesn't allow to set the time.

I have no idea about simple and elegant solution for setting clock :(

My hwclock man page show me an example:

 hwclock --set --date="2011-08-14 16:45:05"

I'll try to find some ready-to-use strptime-like function (e.g. in uClibc).

Also there is no epoch handling.

May be it's better to drop hwclock clock setting in the inital rtc support series?

-- 
Best regards,
  Antony Pavlov

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC 5/5] commands: add hwclock
  2014-07-11  5:59     ` Antony Pavlov
@ 2014-07-11  6:07       ` Sascha Hauer
  0 siblings, 0 replies; 17+ messages in thread
From: Sascha Hauer @ 2014-07-11  6:07 UTC (permalink / raw)
  To: Antony Pavlov; +Cc: barebox

On Fri, Jul 11, 2014 at 09:59:36AM +0400, Antony Pavlov wrote:
> On Thu, 10 Jul 2014 23:45:47 +0200
> Sascha Hauer <s.hauer@pengutronix.de> wrote:
> 
> > > +BAREBOX_CMD_START(hwclock)
> > > +	.cmd		= do_hwclock,
> > > +	BAREBOX_CMD_DESC("query or set the hardware clock (RTC)")
> > 
> > This initial version doesn't allow to set the time.
> 
> I have no idea about simple and elegant solution for setting clock :(
> 
> My hwclock man page show me an example:
> 
>  hwclock --set --date="2011-08-14 16:45:05"
> 
> I'll try to find some ready-to-use strptime-like function (e.g. in uClibc).

The format which the date man page describes as "coordinated universal
time" seems quite easy to parse. It's [MMDDhhmm[[CC]YY][.ss]], so
071107572014 for now.

> 
> Also there is no epoch handling.
> 
> May be it's better to drop hwclock clock setting in the inital rtc support series?

I think we should have set time support from the beginning. I mean
setting the time is an important part of testing the drivers before
merging them.

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC 0/5] add rtc support
  2014-07-11  6:12   ` Antony Pavlov
@ 2014-07-11  6:10     ` Sascha Hauer
  2014-07-11  6:32       ` Antony Pavlov
  2014-07-11 11:27     ` Antony Pavlov
  1 sibling, 1 reply; 17+ messages in thread
From: Sascha Hauer @ 2014-07-11  6:10 UTC (permalink / raw)
  To: Antony Pavlov; +Cc: barebox

On Fri, Jul 11, 2014 at 10:12:36AM +0400, Antony Pavlov wrote:
> On Fri, 11 Jul 2014 07:37:38 +0200
> Sascha Hauer <s.hauer@pengutronix.de> wrote:
> 
> > On Thu, Jul 10, 2014 at 12:33:14PM +0400, Antony Pavlov wrote:
> > > This patchseries imports RTC support from linux-3.15.
> > > 
> > > User can use 'hwclock' command to see realtime clock readout.
> > > 
> > > Tested with DS1307.
> > > 
> > > TODOs:
> > > 
> > >  * rtc_set_time() is not realized;
> > >  * ds1307_set_time() is not tested;
> > >  * bcd lib is compiled in even if rtc support is disabled (as linux does);
> > >    we can make it optional;
> > 
> > It's probably not worth it. The linker will throw it away anyway if
> > unused.
> 
> Hmm, I have missed this.
> 
> In this case can we drop I2C_SMBUS Kconfig stuff
> 
> +config I2C_SMBUS
> +	bool
> +
> 
> +obj-$(CONFIG_I2C_SMBUS) += i2c-smbus.o
> 
> from 'add driver for PCA95[357]x, ...' series?
> 
> Anyway CONFIG_I2C_SMBUS has another meaning in linux.
> 
> > 
> > >  * adjust include/linux/rtc.h and include/rtc.h;
> > >  * rtc_unregister() is not realized.
> > 
> > Maybe an environment variable which holds the seconds since epoch would
> > be useful. This way scripts could make use of the current RTC time.
> 
> So you propose to add an option to hwclock command to store RTC readout into variable,
> isn't it?

Yes. This would allow scripts to compare times or to store the current
time in some boot log.

> 
> I have an idea to put epoch offset (number of years into AD to which a zero year value
> in the Hardware Clock refers) into "global.epoch" variable and add corresponding
> Kconfig option for default epoch value.

Does Linux support such a mechanism aswell? How can we make sure Linux
uses the same epoch offset?

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC 0/5] add rtc support
  2014-07-11  5:37 ` [RFC 0/5] add rtc support Sascha Hauer
@ 2014-07-11  6:12   ` Antony Pavlov
  2014-07-11  6:10     ` Sascha Hauer
  2014-07-11 11:27     ` Antony Pavlov
  0 siblings, 2 replies; 17+ messages in thread
From: Antony Pavlov @ 2014-07-11  6:12 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On Fri, 11 Jul 2014 07:37:38 +0200
Sascha Hauer <s.hauer@pengutronix.de> wrote:

> On Thu, Jul 10, 2014 at 12:33:14PM +0400, Antony Pavlov wrote:
> > This patchseries imports RTC support from linux-3.15.
> > 
> > User can use 'hwclock' command to see realtime clock readout.
> > 
> > Tested with DS1307.
> > 
> > TODOs:
> > 
> >  * rtc_set_time() is not realized;
> >  * ds1307_set_time() is not tested;
> >  * bcd lib is compiled in even if rtc support is disabled (as linux does);
> >    we can make it optional;
> 
> It's probably not worth it. The linker will throw it away anyway if
> unused.

Hmm, I have missed this.

In this case can we drop I2C_SMBUS Kconfig stuff

+config I2C_SMBUS
+	bool
+

+obj-$(CONFIG_I2C_SMBUS) += i2c-smbus.o

from 'add driver for PCA95[357]x, ...' series?

Anyway CONFIG_I2C_SMBUS has another meaning in linux.

> 
> >  * adjust include/linux/rtc.h and include/rtc.h;
> >  * rtc_unregister() is not realized.
> 
> Maybe an environment variable which holds the seconds since epoch would
> be useful. This way scripts could make use of the current RTC time.

So you propose to add an option to hwclock command to store RTC readout into variable,
isn't it?

I have an idea to put epoch offset (number of years into AD to which a zero year value
in the Hardware Clock refers) into "global.epoch" variable and add corresponding
Kconfig option for default epoch value.

-- 
Best regards,
  Antony Pavlov

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC 0/5] add rtc support
  2014-07-11  6:10     ` Sascha Hauer
@ 2014-07-11  6:32       ` Antony Pavlov
  2014-07-11  6:37         ` Sascha Hauer
  0 siblings, 1 reply; 17+ messages in thread
From: Antony Pavlov @ 2014-07-11  6:32 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On Fri, 11 Jul 2014 08:10:26 +0200
Sascha Hauer <s.hauer@pengutronix.de> wrote:

> On Fri, Jul 11, 2014 at 10:12:36AM +0400, Antony Pavlov wrote:
> > On Fri, 11 Jul 2014 07:37:38 +0200
> > Sascha Hauer <s.hauer@pengutronix.de> wrote:
> > 
> > > On Thu, Jul 10, 2014 at 12:33:14PM +0400, Antony Pavlov wrote:
> > > > This patchseries imports RTC support from linux-3.15.
> > > > 
> > > > User can use 'hwclock' command to see realtime clock readout.
> > > > 
> > > > Tested with DS1307.
> > > > 
> > > > TODOs:
> > > > 
> > > >  * rtc_set_time() is not realized;
> > > >  * ds1307_set_time() is not tested;
> > > >  * bcd lib is compiled in even if rtc support is disabled (as linux does);
> > > >    we can make it optional;
> > > 
> > > It's probably not worth it. The linker will throw it away anyway if
> > > unused.
> > 
> > Hmm, I have missed this.
> > 
> > In this case can we drop I2C_SMBUS Kconfig stuff
> > 
> > +config I2C_SMBUS
> > +	bool
> > +
> > 
> > +obj-$(CONFIG_I2C_SMBUS) += i2c-smbus.o
> > 
> > from 'add driver for PCA95[357]x, ...' series?
> > 
> > Anyway CONFIG_I2C_SMBUS has another meaning in linux.
> > 
> > > 
> > > >  * adjust include/linux/rtc.h and include/rtc.h;
> > > >  * rtc_unregister() is not realized.
> > > 
> > > Maybe an environment variable which holds the seconds since epoch would
> > > be useful. This way scripts could make use of the current RTC time.
> > 
> > So you propose to add an option to hwclock command to store RTC readout into variable,
> > isn't it?
> 
> Yes. This would allow scripts to compare times or to store the current
> time in some boot log.
> 
> > 
> > I have an idea to put epoch offset (number of years into AD to which a zero year value
> > in the Hardware Clock refers) into "global.epoch" variable and add corresponding
> > Kconfig option for default epoch value.
> 
> Does Linux support such a mechanism aswell? How can we make sure Linux
> uses the same epoch offset?

Here is a quote from my hwclock(8) manpage:

       --getepoch
              Print  the  kernel's Hardware Clock epoch value to standard output.  This is the
              number of years into AD to which a zero year value in the Hardware Clock refers.
              For example, if you are using the convention that the year counter in your Hard‐
              ware Clock contains the number of full years since 1952, then the kernel's Hard‐
              ware Clock epoch value must be 1952.

              This epoch value is used whenever hwclock reads or sets the Hardware Clock.

       --setepoch
              Set  the  kernel's  Hardware  Clock  epoch  value  to the value specified by the
              --epoch option.  See the --getepoch option for details.

I suppose that Hardware Clock epoch depends on RTC-chip. I see no automatic machanism to guarantee the same epoch
for linux and barebox.


-- 
Best regards,
  Antony Pavlov

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC 0/5] add rtc support
  2014-07-11  6:32       ` Antony Pavlov
@ 2014-07-11  6:37         ` Sascha Hauer
  0 siblings, 0 replies; 17+ messages in thread
From: Sascha Hauer @ 2014-07-11  6:37 UTC (permalink / raw)
  To: Antony Pavlov; +Cc: barebox

On Fri, Jul 11, 2014 at 10:32:57AM +0400, Antony Pavlov wrote:
> On Fri, 11 Jul 2014 08:10:26 +0200
> Sascha Hauer <s.hauer@pengutronix.de> wrote:
> 
> > > I have an idea to put epoch offset (number of years into AD to which a zero year value
> > > in the Hardware Clock refers) into "global.epoch" variable and add corresponding
> > > Kconfig option for default epoch value.
> > 
> > Does Linux support such a mechanism aswell? How can we make sure Linux
> > uses the same epoch offset?
> 
> Here is a quote from my hwclock(8) manpage:
> 
>        --getepoch
>               Print  the  kernel's Hardware Clock epoch value to standard output.  This is the
>               number of years into AD to which a zero year value in the Hardware Clock refers.
>               For example, if you are using the convention that the year counter in your Hard‐
>               ware Clock contains the number of full years since 1952, then the kernel's Hard‐
>               ware Clock epoch value must be 1952.
> 
>               This epoch value is used whenever hwclock reads or sets the Hardware Clock.
> 
>        --setepoch
>               Set  the  kernel's  Hardware  Clock  epoch  value  to the value specified by the
>               --epoch option.  See the --getepoch option for details.
> 
> I suppose that Hardware Clock epoch depends on RTC-chip. I see no automatic machanism to guarantee the same epoch
> for linux and barebox.

Is this used anywhere? Running a hwclock --getepoch on my desktop PC
answers with:

| hwclock: The kernel keeps an epoch value for the Hardware Clock only on an Alpha machine.
| This copy of hwclock was built for a machine other than Alpha
| (and thus is presumably not running on an Alpha now).  No action taken.

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC 0/5] add rtc support
  2014-07-11  6:12   ` Antony Pavlov
  2014-07-11  6:10     ` Sascha Hauer
@ 2014-07-11 11:27     ` Antony Pavlov
  1 sibling, 0 replies; 17+ messages in thread
From: Antony Pavlov @ 2014-07-11 11:27 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On Fri, 11 Jul 2014 10:12:36 +0400
Antony Pavlov <antonynpavlov@gmail.com> wrote:

> On Fri, 11 Jul 2014 07:37:38 +0200
> Sascha Hauer <s.hauer@pengutronix.de> wrote:
> 
> > On Thu, Jul 10, 2014 at 12:33:14PM +0400, Antony Pavlov wrote:
> > > This patchseries imports RTC support from linux-3.15.
> > > 
> > > User can use 'hwclock' command to see realtime clock readout.
> > > 
> > > Tested with DS1307.
> > > 
> > > TODOs:
> > > 
> > >  * rtc_set_time() is not realized;
> > >  * ds1307_set_time() is not tested;
> > >  * bcd lib is compiled in even if rtc support is disabled (as linux does);
> > >    we can make it optional;
> > 
...
> I have an idea to put epoch offset (number of years into AD to which a zero year value
> in the Hardware Clock refers) into "global.epoch" variable and add corresponding
> Kconfig option for default epoch value.

I was wrong.

Hmm hwclock receives time value in the 'struct tm'
(e.g. see linux.git/include/linux/time.h)

/*
 * Similar to the struct tm in userspace <time.h>, but it needs to be here so
 * that the kernel source is self contained.
 */
struct tm {
	/*
	 * the number of seconds after the minute, normally in the range
	 * 0 to 59, but can be up to 60 to allow for leap seconds
	 */
	int tm_sec;
	/* the number of minutes after the hour, in the range 0 to 59*/
	int tm_min;
	/* the number of hours past midnight, in the range 0 to 23 */
	int tm_hour;
	/* the day of the month, in the range 1 to 31 */
	int tm_mday;
	/* the number of months since January, in the range 0 to 11 */
	int tm_mon;
	/* the number of years since 1900 */
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

	long tm_year;
	/* the number of days since Sunday, in the range 0 to 6 */
	int tm_wday;
	/* the number of days since January 1, in the range 0 to 365 */
	int tm_yday;
};

Here is a quote from read_hardware_clock()
(see util-linux-2.20.1/hwclock/hwclock.c)

         printf(_
             ("Hw clock time : %4d/%.2d/%.2d %.2d:%.2d:%.2d = "
             "%ld seconds since 1969\n"), tm.tm_year + 1900,
             tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min,
             tm.tm_sec, (long)*systime_p);

So in linux hardware clock epoch is always "1900".

-- 
Best regards,
  Antony Pavlov

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2014-07-11 11:15 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-10  8:33 [RFC 0/5] add rtc support Antony Pavlov
2014-07-10  8:33 ` [RFC 1/5] lib: import 'bcd' from linux-3.15 Antony Pavlov
2014-07-10  8:33 ` [RFC 2/5] add rtc support Antony Pavlov
2014-07-10 21:40   ` Sascha Hauer
2014-07-11  5:31     ` Antony Pavlov
2014-07-10  8:33 ` [RFC 3/5] i2c: import SMBus stuff from linux Antony Pavlov
2014-07-10  8:33 ` [RFC 4/5] rtc: add ds1307 support Antony Pavlov
2014-07-10  8:33 ` [RFC 5/5] commands: add hwclock Antony Pavlov
2014-07-10 21:45   ` Sascha Hauer
2014-07-11  5:59     ` Antony Pavlov
2014-07-11  6:07       ` Sascha Hauer
2014-07-11  5:37 ` [RFC 0/5] add rtc support Sascha Hauer
2014-07-11  6:12   ` Antony Pavlov
2014-07-11  6:10     ` Sascha Hauer
2014-07-11  6:32       ` Antony Pavlov
2014-07-11  6:37         ` Sascha Hauer
2014-07-11 11:27     ` Antony Pavlov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox