Skip to contentSkip to conversation

add rtc support

RFC Superseded Antony Pavlov 2 participants 17 messages Jul 10, 2014 – Jul 11, 2014

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover add rtc support

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
Signature · 7 lines
2.0.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

RFC 1/5 lib: import 'bcd' from linux-3.15

---
 include/linux/bcd.h | 22 ++++++++++++++++++++++
 lib/Makefile        |  1 +
 lib/bcd.c           | 14 ++++++++++++++
 3 files changed, 37 insertions(+)
include/linux/bcd.h new file +22 -0
new file mode 100644
index 0000000..18fff11
@@ -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 */
lib/Makefile +1 -0
index e8769a9..14442a7 100644
@@ -1,3 +1,4 @@
+obj-y			+= bcd.o
 obj-$(CONFIG_BOOTSTRAP)	+= bootstrap/
 obj-y			+= ctype.o
 obj-y			+= rbtree.o
lib/bcd.c new file +14 -0
new file mode 100644
index 0000000..b072d50
@@ -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);
Signature · 7 lines
2.0.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

RFC 2/5 add rtc support

In reply to Antony Pavlov
---
 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(+)
drivers/Kconfig +1 -0
index 12a9d8c..d38032c 100644
@@ -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
drivers/Makefile +1 -0
index 1990e86..4591f9a 100644
@@ -27,3 +27,4 @@ obj-y += bus/
 obj-$(CONFIG_REGULATOR) += regulator/
 obj-$(CONFIG_RESET_CONTROLLER) += reset/
 obj-$(CONFIG_PCI) += pci/
+obj-y += rtc/
drivers/rtc/Kconfig new file +16 -0
new file mode 100644
index 0000000..f148266
@@ -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.
drivers/rtc/Makefile new file +6 -0
new file mode 100644
index 0000000..7d1b5bc
@@ -0,0 +1,6 @@
+#
+# Makefile for RTC class/drivers.
+#
+
+obj-$(CONFIG_RTC_LIB)           += rtc-lib.o
+obj-$(CONFIG_RTC_CLASS)         += class.o
drivers/rtc/class.c new file +62 -0

62 lines not on this page · raw message

drivers/rtc/rtc-lib.c new file +64 -0

64 lines not on this page · raw message

include/linux/rtc.h new file +46 -0

46 lines not on this page · raw message

include/rtc.h +2 -0
index c2d8bce..e2414fb 100644
@@ -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_ */
Signature · 7 lines
2.0.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#5 lore raw

RFC 3/5 i2c: import SMBus stuff from linux

In reply to Antony Pavlov
---
 drivers/i2c/i2c.c | 368 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/i2c/i2c.h |  64 ++++++++++
 2 files changed, 432 insertions(+)
drivers/i2c/i2c.c +368 -0
index 9873957..167a5e4 100644
@@ -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];
include/i2c/i2c.h +64 -0

70 lines not on this page · raw message

Signature · 7 lines
2.0.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#6 lore raw

RFC 4/5 rtc: add ds1307 support

In reply to Antony Pavlov
---
 drivers/rtc/Kconfig      |  29 +++++
 drivers/rtc/Makefile     |   4 +
 drivers/rtc/rtc-ds1307.c | 315 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 348 insertions(+)
drivers/rtc/Kconfig +29 -0
index f148266..8771933 100644
@@ -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
drivers/rtc/Makefile +4 -0
index 7d1b5bc..2c5a50e 100644
@@ -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
drivers/rtc/rtc-ds1307.c new file +315 -0

315 lines not on this page · raw message

Signature · 7 lines
2.0.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#7 lore raw

RFC 5/5 commands: add hwclock

In reply to Antony Pavlov
---
 commands/Kconfig   |  8 ++++++++
 commands/Makefile  |  1 +
 commands/hwclock.c | 37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+)
commands/Kconfig +8 -0
index 61816f5..a594f7c 100644
@@ -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
commands/Makefile +1 -0
index d42aca5..44dd9d4 100644
@@ -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
commands/hwclock.c new file +37 -0
new file mode 100644
index 0000000..4671fee
@@ -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
Signature · 7 lines
2.0.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw
In reply to Antony Pavlov
Quoted 100 lines · Antony Pavlov
---
 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(+)
drivers/Kconfig +1 -0
index 12a9d8c..d38032c 100644
@@ -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
drivers/Makefile +1 -0
index 1990e86..4591f9a 100644
@@ -27,3 +27,4 @@ obj-y += bus/
 obj-$(CONFIG_REGULATOR) += regulator/
 obj-$(CONFIG_RESET_CONTROLLER) += reset/
 obj-$(CONFIG_PCI) += pci/
+obj-y += rtc/
drivers/rtc/Kconfig new file +16 -0
new file mode 100644
index 0000000..f148266
@@ -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.
drivers/rtc/Makefile new file +6 -0
new file mode 100644
index 0000000..7d1b5bc
@@ -0,0 +1,6 @@
+#
+# Makefile for RTC class/drivers.
+#
+
+obj-$(CONFIG_RTC_LIB)           += rtc-lib.o
+obj-$(CONFIG_RTC_CLASS)         += class.o
drivers/rtc/class.c new file +28 -0
new file mode 100644
index 0000000..b808f3d
@@ -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);

drivers/rtc/class.c line 38

Quoted 10 lines
drivers/rtc/class.c new file +10 -0
+
+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.

Quoted 14 lines
drivers/rtc/class.c new file +14 -0
+
+	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);

drivers/rtc/class.c line 62

Quoted 10 lines
drivers/rtc/class.c new file +10 -0
+	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

Signature · 9 lines
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
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#8 lore raw

Re: [RFC 5/5] commands: add hwclock

In reply to Antony Pavlov
Quoted 12 lines · Antony Pavlov
---
 commands/Kconfig   |  8 ++++++++
 commands/Makefile  |  1 +
 commands/hwclock.c | 37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+)
commands/Kconfig +0 -0
index 61816f5..a594f7c 100644

commands/Kconfig config CMD_GPIO · line 1700

Quoted 10 lines
commands/Kconfig +7 -0
@@ -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? ;)

Quoted 17 lines
commands/Kconfig +17 -0
+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);
+

commands/Kconfig config CMD_GPIO

Quoted 10 lines
commands/Kconfig +10 -0
+	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

Signature · 9 lines
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
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#4 lore raw
In reply to Sascha Hauer
Quoted 42 lines · On Thu, 10 Jul 2014 23:40:30 +0200 Sascha Hauer <[email protected]> wrote:
Quoted 41 lines · Antony Pavlov
---
 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(+)
drivers/Kconfig +1 -0
index 12a9d8c..d38032c 100644
@@ -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
drivers/Makefile +1 -0
index 1990e86..4591f9a 100644
@@ -27,3 +27,4 @@ obj-y += bus/
 obj-$(CONFIG_REGULATOR) += regulator/
 obj-$(CONFIG_RESET_CONTROLLER) += reset/
 obj-$(CONFIG_PCI) += pci/
+obj-y += rtc/
drivers/rtc/Kconfig new file +3 -0
new file mode 100644
index 0000000..f148266
@@ -0,0 +1,16 @@
+#
+# RTC class/drivers configuration
+#
Quoted 8 lines

drivers/rtc/Kconfig line 11

Quoted 8 lines
drivers/rtc/Kconfig new file +8 -0
+
+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.

Quoted 83 lines
Quoted 51 lines
drivers/rtc/Kconfig new file +5 -0
+        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.
drivers/rtc/Makefile new file +6 -0
new file mode 100644
index 0000000..7d1b5bc
@@ -0,0 +1,6 @@
+#
+# Makefile for RTC class/drivers.
+#
+
+obj-$(CONFIG_RTC_LIB)           += rtc-lib.o
+obj-$(CONFIG_RTC_CLASS)         += class.o
drivers/rtc/class.c new file +28 -0
new file mode 100644
index 0000000..b808f3d
@@ -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);

drivers/rtc/class.c line 38

Quoted 10 lines
drivers/rtc/class.c new file +10 -0
+
+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.

Quoted 19 lines
drivers/rtc/class.c new file +19 -0
+
+	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)
Quoted 8 lines

drivers/rtc/class.c line 62

Quoted 5 lines
drivers/rtc/class.c new file +5 -0
+		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
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#11 lore raw
In reply to Antony Pavlov
Quoted 4 lines · Antony Pavlov

This patchseries imports RTC support from linux-3.15.

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

Quoted 8 lines

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.

Quoted 2 lines

* 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

Signature · 9 lines
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
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#9 lore raw

Re: [RFC 5/5] commands: add hwclock

In reply to Sascha Hauer
Quoted 16 lines · On Thu, 10 Jul 2014 23:45:47 +0200 Sascha Hauer <[email protected]> wrote:
Quoted 15 lines · Antony Pavlov
---
 commands/Kconfig   |  8 ++++++++
 commands/Makefile  |  1 +
 commands/hwclock.c | 37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+)
commands/Kconfig +0 -0
index 61816f5..a594f7c 100644
@@ -1691,6 +1691,14 @@ config CMD_GPIO
 
 	  Usage: gpio_set_value GPIO VALUE
 
Quoted 9 lines

commands/Kconfig config CMD_GPIO · line 1700

Quoted 7 lines
commands/Kconfig +7 -0
+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!

Quoted 21 lines
Quoted 21 lines
commands/Kconfig +21 -0
+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);
+
Quoted 8 lines

commands/Kconfig

Quoted 6 lines
commands/Kconfig +6 -0
+	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
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#12 lore raw
In reply to Sascha Hauer
Quoted 9 lines · On Fri, 11 Jul 2014 07:37:38 +0200 Sascha Hauer <[email protected]> wrote:
Quoted 8 lines · Antony Pavlov

This patchseries imports RTC support from linux-3.15.

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

Tested with DS1307.

TODOs:

Quoted 7 lines
Quoted 4 lines

* 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.

Quoted 6 lines
Quoted 3 lines

* 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
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#10 lore raw

Re: [RFC 5/5] commands: add hwclock

In reply to Antony Pavlov
Quoted 9 lines · Antony Pavlov
Quoted 5 lines · On Thu, 10 Jul 2014 23:45:47 +0200 Sascha Hauer <[email protected]> wrote:

commands/hwclock.c line 34

Quoted 3 lines
commands/hwclock.c new file +3 -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.

Quoted 7 lines

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.

Quoted 4 lines

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

Signature · 9 lines
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
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#13 lore raw
In reply to Antony Pavlov
Quoted 35 lines · Antony Pavlov
Quoted 9 lines · On Fri, 11 Jul 2014 07:37:38 +0200 Sascha Hauer <[email protected]> wrote:
Quoted 8 lines · Antony Pavlov

This patchseries imports RTC support from linux-3.15.

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

Tested with DS1307.

TODOs:

Quoted 7 lines
Quoted 4 lines

* 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

(quoted diff) +4 -0
+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.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

* 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.

Quoted 4 lines

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

Signature · 9 lines
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
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#14 lore raw
Quoted 49 lines · On Fri, 11 Jul 2014 08:10:26 +0200 Sascha Hauer <[email protected]> wrote:
Quoted 35 lines · Antony Pavlov
Quoted 9 lines · On Fri, 11 Jul 2014 07:37:38 +0200 Sascha Hauer <[email protected]> wrote:
Quoted 8 lines · Antony Pavlov

This patchseries imports RTC support from linux-3.15.

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

Tested with DS1307.

TODOs:

Quoted 7 lines
Quoted 4 lines

* 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

(quoted diff) +4 -0
+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.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

* 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.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

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
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#15 lore raw
Quoted 19 lines · Antony Pavlov
Quoted 6 lines · On Fri, 11 Jul 2014 08:10:26 +0200 Sascha Hauer <[email protected]> wrote:
Quoted 3 lines

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.

Quoted 8 lines

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

Signature · 9 lines
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
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#16 lore raw
In reply to Antony Pavlov
Quoted 17 lines · On Fri, 11 Jul 2014 10:12:36 +0400 Antony Pavlov <[email protected]>…
Quoted 14 lines · On Fri, 11 Jul 2014 07:37:38 +0200 Sascha Hauer <[email protected]> wrote:
Quoted 12 lines · Antony Pavlov

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;

...

Quoted 3 lines

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
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox