public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Eugen Hristev <eugen.hristev@microchip.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v4 01/34] w1: Add 1-Wire uclass
Date: Tue, 18 Sep 2018 10:35:24 +0300	[thread overview]
Message-ID: <1537256157-18001-2-git-send-email-eugen.hristev@microchip.com> (raw)
In-Reply-To: <1537256157-18001-1-git-send-email-eugen.hristev@microchip.com>

From: Maxime Ripard <maxime.ripard@free-electrons.com>

We might want to use 1-Wire devices connected on boards such as EEPROMs in
U-Boot.

Provide a framework to be able to do that.

Signed-off-by: Maxime Ripard <maxime.ripard@bootlin.com>
[eugen.hristev at microchip.com: reworked]
Signed-off-by: Eugen Hristev <eugen.hristev@microchip.com>
---
 drivers/Kconfig        |   2 +
 drivers/Makefile       |   1 +
 drivers/w1/Kconfig     |  18 ++++
 drivers/w1/Makefile    |   1 +
 drivers/w1/w1-uclass.c | 236 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/dm/uclass-id.h |   1 +
 include/w1.h           |  36 ++++++++
 7 files changed, 295 insertions(+)
 create mode 100644 drivers/w1/Kconfig
 create mode 100644 drivers/w1/Makefile
 create mode 100644 drivers/w1/w1-uclass.c
 create mode 100644 include/w1.h

diff --git a/drivers/Kconfig b/drivers/Kconfig
index 56536c4..6f91eac 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -106,6 +106,8 @@ source "drivers/usb/Kconfig"
 
 source "drivers/video/Kconfig"
 
+source "drivers/w1/Kconfig"
+
 source "drivers/watchdog/Kconfig"
 
 config PHYS_TO_BUS
diff --git a/drivers/Makefile b/drivers/Makefile
index 23ea609..4ca07c3 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -105,6 +105,7 @@ obj-y += smem/
 obj-y += soc/
 obj-y += thermal/
 obj-y += axi/
+obj-$(CONFIG_W1) += w1/
 
 obj-$(CONFIG_MACH_PIC32) += ddr/microchip/
 endif
diff --git a/drivers/w1/Kconfig b/drivers/w1/Kconfig
new file mode 100644
index 0000000..64b27c6
--- /dev/null
+++ b/drivers/w1/Kconfig
@@ -0,0 +1,18 @@
+#
+# W1 subsystem configuration
+#
+
+menu "1-Wire support"
+
+config W1
+	bool "Enable 1-wire controllers support"
+	default no
+	depends on DM
+	help
+	  Support for the Dallas 1-Wire bus.
+
+if W1
+
+endif
+
+endmenu
diff --git a/drivers/w1/Makefile b/drivers/w1/Makefile
new file mode 100644
index 0000000..f81693b
--- /dev/null
+++ b/drivers/w1/Makefile
@@ -0,0 +1 @@
+obj-$(CONFIG_W1) += w1-uclass.o
diff --git a/drivers/w1/w1-uclass.c b/drivers/w1/w1-uclass.c
new file mode 100644
index 0000000..44759fe
--- /dev/null
+++ b/drivers/w1/w1-uclass.c
@@ -0,0 +1,236 @@
+// SPDX-License-Identifier:	GPL-2.0+
+/*
+ *
+ * Copyright (c) 2015 Free Electrons
+ * Copyright (c) 2015 NextThing Co.
+ * Copyright (c) 2018 Microchip Technology, Inc.
+ *
+ * Maxime Ripard <maxime.ripard@free-electrons.com>
+ * Eugen Hristev <eugen.hristev@microchip.com>
+ *
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <w1.h>
+
+#include <dm/device-internal.h>
+
+#define W1_MATCH_ROM	0x55
+#define W1_SKIP_ROM	0xcc
+#define W1_SEARCH	0xf0
+
+struct w1_bus {
+	u64	search_id;
+};
+
+static int w1_enumerate(struct udevice *bus)
+{
+	const struct w1_ops *ops = device_get_ops(bus);
+	struct w1_bus *w1 = dev_get_uclass_priv(bus);
+	u64 last_rn, rn = w1->search_id, tmp64;
+	bool last_device = false;
+	int search_bit, desc_bit = 64;
+	int last_zero = -1;
+	u8 triplet_ret = 0;
+	int i;
+
+	if (!ops->reset || !ops->write_byte || !ops->triplet)
+		return -ENOSYS;
+
+	while (!last_device) {
+		last_rn = rn;
+		rn = 0;
+
+		/*
+		 * Reset bus and all 1-wire device state machines
+		 * so they can respond to our requests.
+		 *
+		 * Return 0 - device(s) present, 1 - no devices present.
+		 */
+		if (ops->reset(bus)) {
+			debug("%s: No devices present on the wire.\n",
+			      __func__);
+			break;
+		}
+
+		/* Start the search */
+		ops->write_byte(bus, W1_SEARCH);
+		for (i = 0; i < 64; ++i) {
+			/* Determine the direction/search bit */
+			if (i == desc_bit)
+				/* took the 0 path last time, so take the 1 path */
+				search_bit = 1;
+			else if (i > desc_bit)
+				/* take the 0 path on the next branch */
+				search_bit = 0;
+			else
+				search_bit = ((last_rn >> i) & 0x1);
+
+			/* Read two bits and write one bit */
+			triplet_ret = ops->triplet(bus, search_bit);
+
+			/* quit if no device responded */
+			if ((triplet_ret & 0x03) == 0x03)
+				break;
+
+			/* If both directions were valid, and we took the 0 path... */
+			if (triplet_ret == 0)
+				last_zero = i;
+
+			/* extract the direction taken & update the device number */
+			tmp64 = (triplet_ret >> 2);
+			rn |= (tmp64 << i);
+		}
+
+		/* last device or error, aborting here */
+		if ((triplet_ret & 0x03) == 0x03)
+			last_device = true;
+
+		if ((triplet_ret & 0x03) != 0x03) {
+			if (desc_bit == last_zero || last_zero < 0) {
+				last_device = 1;
+				w1->search_id = 0;
+			} else {
+				w1->search_id = rn;
+			}
+			desc_bit = last_zero;
+
+			debug("%s: Detected new device 0x%llx (family 0x%x)\n",
+			      bus->name, rn, (u8)(rn & 0xff));
+		}
+	}
+
+	return 0;
+}
+
+int w1_get_bus(int busnum, struct udevice **busp)
+{
+	int ret, i = 0;
+
+	struct udevice *dev;
+
+	for (ret = uclass_first_device(UCLASS_W1, &dev);
+	     !ret;
+	     uclass_next_device(&dev), i++) {
+		if (ret) {
+			debug("Cannot find w1 bus %d\n", busnum);
+			return ret;
+		}
+		if (i == busnum) {
+			*busp = dev;
+			return 0;
+		}
+	}
+	return ret;
+}
+
+u8 w1_get_device_family(struct udevice *dev)
+{
+	struct w1_device *w1 = dev_get_parent_platdata(dev);
+
+	return w1->id & 0xff;
+}
+
+int w1_reset_select(struct udevice *dev)
+{
+	struct w1_device *w1 = dev_get_parent_platdata(dev);
+	struct udevice *bus = dev_get_parent(dev);
+	const struct w1_ops *ops = device_get_ops(bus);
+	int i;
+
+	if (!ops->reset || !ops->write_byte)
+		return -ENOSYS;
+
+	ops->reset(bus);
+
+	ops->write_byte(bus, W1_MATCH_ROM);
+
+	for (i = 0; i < sizeof(w1->id); i++)
+		ops->write_byte(bus, (w1->id >> (i * 8)) & 0xff);
+
+	return 0;
+}
+
+int w1_read_byte(struct udevice *dev)
+{
+	struct udevice *bus = dev_get_parent(dev);
+	const struct w1_ops *ops = device_get_ops(bus);
+
+	if (!ops->read_byte)
+		return -ENOSYS;
+
+	return ops->read_byte(bus);
+}
+
+int w1_read_buf(struct udevice *dev, u8 *buf, unsigned int count)
+{
+	int i, ret;
+
+	for (i = 0; i < count; i++) {
+		ret = w1_read_byte(dev);
+		if (ret < 0)
+			return ret;
+
+		buf[i] = ret & 0xff;
+	}
+
+	return 0;
+}
+
+int w1_write_byte(struct udevice *dev, u8 byte)
+{
+	struct udevice *bus = dev_get_parent(dev);
+	const struct w1_ops *ops = device_get_ops(bus);
+
+	if (!ops->write_byte)
+		return -ENOSYS;
+
+	ops->write_byte(bus, byte);
+
+	return 0;
+}
+
+static int w1_post_probe(struct udevice *bus)
+{
+	w1_enumerate(bus);
+
+	return 0;
+}
+
+int w1_init(void)
+{
+	struct udevice *bus;
+	struct uclass *uc;
+	int ret;
+
+	ret = uclass_get(UCLASS_W1, &uc);
+	if (ret)
+		return ret;
+
+	uclass_foreach_dev(bus, uc) {
+		ret = device_probe(bus);
+		if (ret == -ENODEV) {	/* No such device. */
+			printf("W1 controller not available.\n");
+			continue;
+		}
+
+		if (ret) {		/* Other error. */
+			printf("W1 controller probe failed.\n");
+			continue;
+		}
+	}
+	return 0;
+}
+
+UCLASS_DRIVER(w1) = {
+	.name		= "w1",
+	.id		= UCLASS_W1,
+	.flags		= DM_UC_FLAG_SEQ_ALIAS,
+	.per_device_auto_alloc_size	= sizeof(struct w1_bus),
+	.post_probe	= w1_post_probe,
+#if CONFIG_IS_ENABLED(OF_CONTROL)
+	.post_bind	= dm_scan_fdt_dev,
+#endif
+	.per_child_platdata_auto_alloc_size     = sizeof(struct w1_device),
+};
diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
index 7027ea0..fa72afb 100644
--- a/include/dm/uclass-id.h
+++ b/include/dm/uclass-id.h
@@ -92,6 +92,7 @@ enum uclass_id {
 	UCLASS_VIDEO,		/* Video or LCD device */
 	UCLASS_VIDEO_BRIDGE,	/* Video bridge, e.g. DisplayPort to LVDS */
 	UCLASS_VIDEO_CONSOLE,	/* Text console driver for video device */
+	UCLASS_W1,		/* Dallas 1-Wire bus */
 	UCLASS_WDT,		/* Watchdot Timer driver */
 
 	UCLASS_COUNT,
diff --git a/include/w1.h b/include/w1.h
new file mode 100644
index 0000000..b36e0f8
--- /dev/null
+++ b/include/w1.h
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier:	GPL-2.0+
+ *
+ * Copyright (c) 2015 Free Electrons
+ * Copyright (c) 2015 NextThing Co
+ *
+ */
+
+#ifndef __W1_H
+#define __W1_H
+
+#include <dm.h>
+
+#define W1_FAMILY_DS24B33	0x23
+#define W1_FAMILY_DS2431	0x2d
+
+struct w1_device {
+	u64	id;
+};
+
+struct w1_ops {
+	u8	(*read_byte)(struct udevice *dev);
+	bool	(*reset)(struct udevice *dev);
+	u8	(*triplet)(struct udevice *dev, bool bdir);
+	void	(*write_byte)(struct udevice *dev, u8 byte);
+};
+
+int w1_get_bus(int busnum, struct udevice **busp);
+u8 w1_get_device_family(struct udevice *dev);
+
+int w1_read_buf(struct udevice *dev, u8 *buf, unsigned int count);
+int w1_read_byte(struct udevice *dev);
+int w1_reset_select(struct udevice *dev);
+int w1_write_buf(struct udevice *dev, u8 *buf, unsigned int count);
+int w1_write_byte(struct udevice *dev, u8 byte);
+
+#endif
-- 
2.7.4

  reply	other threads:[~2018-09-18  7:35 UTC|newest]

Thread overview: 69+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-18  7:35 [U-Boot] [PATCH v4 00/34] Add support for 1wire protocol and 1wire eeproms Eugen Hristev
2018-09-18  7:35 ` Eugen Hristev [this message]
2018-09-29 15:44   ` [U-Boot] [U-Boot,v4,01/34] w1: Add 1-Wire uclass Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 02/34] w1: Add 1-Wire gpio driver Eugen Hristev
2018-09-29 15:44   ` [U-Boot] [U-Boot,v4,02/34] " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 03/34] dt-bindings: W1: w1-gpio: added bindings for w1-gpio Eugen Hristev
2018-09-29 15:44   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 04/34] W1-EEPROM: Add an W1-EEPROM uclass for 1 wire EEPROMs Eugen Hristev
2018-09-29 15:44   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 05/34] w1: identify devices with w1-eeprom uclass Eugen Hristev
2018-09-29 15:44   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 06/34] W1-EEPROM: add support for Maxim DS24 eeprom families Eugen Hristev
2018-09-29 15:44   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 07/34] dt-bindings: w1-eeprom: ds24xxx: create bindings Eugen Hristev
2018-09-29 15:44   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 08/34] W1-EEPROM: add sandbox driver Eugen Hristev
2018-09-29 15:45   ` [U-Boot] [U-Boot,v4,08/34] " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 09/34] dt-bindings: w1-eeprom: eep_sandbox: create bindings Eugen Hristev
2018-09-29 15:45   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 10/34] w1: add command for onewire protocol Eugen Hristev
2018-09-29 15:45   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 11/34] pinctrl: sandbox: add gpio onewire w1 group Eugen Hristev
2018-09-29 15:45   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 12/34] sandbox: DTS: w1: add node for one wire interface on GPIO Eugen Hristev
2018-09-29 15:45   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 13/34] configs: sandbox: add onewire w1 and sandbox eeprom Eugen Hristev
2018-09-29 15:45   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 14/34] configs: sama5d2_xplained: add onewire and eeprom drivers Eugen Hristev
2018-09-29 15:45   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 15/34] configs: sama5d3_xplained: " Eugen Hristev
2018-09-29 15:45   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 16/34] configs: sama5d27_som1_ek: " Eugen Hristev
2018-09-29 15:45   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 17/34] configs: sama5d2_ptc_ek: " Eugen Hristev
2018-09-29 15:45   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 18/34] configs: sama5d4_xplained: " Eugen Hristev
2018-09-29 15:45   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 19/34] board: atmel: add support for pda detection Eugen Hristev
2018-09-29 15:45   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 20/34] board: sama5d2_xplained: add pda detect call at init time Eugen Hristev
2018-09-29 15:46   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 21/34] board: sama5d3_xplained: " Eugen Hristev
2018-09-29 15:46   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 22/34] board: sama5d27_som1_ek: " Eugen Hristev
2018-09-29 15:46   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 23/34] board: sama5d2_ptc_ek: " Eugen Hristev
2018-09-29 15:46   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 24/34] board: sama5d4_xplained: " Eugen Hristev
2018-09-29 15:46   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 25/34] configs: sama5d2_xplained: add fdt overlay support Eugen Hristev
2018-09-29 15:46   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 26/34] configs: sama5d3_xplained: " Eugen Hristev
2018-09-29 15:46   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 27/34] configs: sama5d2_ptc_ek: " Eugen Hristev
2018-09-29 15:46   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 28/34] configs: sama5d27_som1_ek: " Eugen Hristev
2018-09-29 15:46   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 29/34] configs: sama5d4_xplained: " Eugen Hristev
2018-09-29 15:46   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 30/34] ARM: dts: at91: sama5d2_xplained: add onewire connector for LCD eeprom Eugen Hristev
2018-09-29 15:46   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 31/34] ARM: dts: at91: sama5d3_xplained: " Eugen Hristev
2018-09-29 15:46   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 32/34] ARM: dts: at91: sama5d27_som1_ek: " Eugen Hristev
2018-09-29 15:46   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 33/34] ARM: dts: at91: sama5d2_ptc: " Eugen Hristev
2018-09-29 15:47   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18  7:35 ` [U-Boot] [PATCH v4 34/34] ARM: dts: at91: sama5d4_xplained: " Eugen Hristev
2018-09-29 15:47   ` [U-Boot] [U-Boot, v4, " Tom Rini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1537256157-18001-2-git-send-email-eugen.hristev@microchip.com \
    --to=eugen.hristev@microchip.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox