public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: "Michał Barnaś" <barnas@google.com>
To: u-boot@lists.denx.de
Cc: "Michał Barnaś" <barnas@google.com>, "Tom Rini" <trini@konsulko.com>
Subject: [PATCH 3/3] misc: atsha204a: fix wakeup function
Date: Mon, 19 Feb 2024 16:32:04 +0000	[thread overview]
Message-ID: <20240219163213.855667-4-barnas@google.com> (raw)
In-Reply-To: <20240219163213.855667-1-barnas@google.com>

The ATSHA204A chip requires SDA line to go low for at least 60us to
wake up the chip. Previous implementation did not meet this requirement
due to the NAK received on bus and not sending the zeroes.
The function to ignore the NAK and send bytes regardless is not
supported in the u-boot making it impossible to wake up the chip
this way.
Instead, the bus speed, if needed, is set to lowest value and the
message is sent to the address 0x0. This way, the address of zero
makes the SDA line go low for about 80us, meeting the required time
to wake up the chip. The zero length packet is not sent by the i2c,
so the one byte is sent to the transfer function, but only the address
is sent anyway.
After sending the zero address, the bus speed is restored to the
previous value if it was slowed down to wake up the chip.

Signed-off-by: Michał Barnaś <barnas@google.com>
---

 drivers/misc/atsha204a-i2c.c | 49 ++++++++++++++++++++++++++++++++----
 1 file changed, 44 insertions(+), 5 deletions(-)

diff --git a/drivers/misc/atsha204a-i2c.c b/drivers/misc/atsha204a-i2c.c
index 29daefb2a5..707daa90bd 100644
--- a/drivers/misc/atsha204a-i2c.c
+++ b/drivers/misc/atsha204a-i2c.c
@@ -21,7 +21,6 @@
 #include <linux/bitrev.h>
 #include <u-boot/crc.h>
 
-#define ATSHA204A_TWLO_US		60
 #define ATSHA204A_TWHI_US		2500
 #define ATSHA204A_TRANSACTION_TIMEOUT	100000
 #define ATSHA204A_TRANSACTION_RETRY	5
@@ -34,6 +33,48 @@ static inline u16 atsha204a_crc16(const u8 *buffer, size_t len)
 	return bitrev16(crc16(0, buffer, len));
 }
 
+static int atsha204a_ping_bus(struct udevice *dev)
+{
+	struct udevice *bus = dev_get_parent(dev);
+	struct i2c_msg msg;
+	int speed;
+	int res;
+	u8 val = 0;
+
+	speed = dm_i2c_get_bus_speed(bus);
+	if (speed != I2C_SPEED_STANDARD_RATE) {
+		int rv;
+
+		rv = dm_i2c_set_bus_speed(bus, I2C_SPEED_STANDARD_RATE);
+		if (rv)
+			debug("Couldn't change the I2C bus speed\n");
+	}
+
+	/*
+	 * The I2C drivers don't support sending messages when NAK is received.
+	 * This chip requires wake up low signal on SDA for >= 60us.
+	 * To achieve this, we slow the bus to 100kHz and send an empty
+	 * message to address 0. This will hold the SDA line low for the
+	 * required time to wake up the chip.
+	 */
+	msg.addr = 0;
+	msg.flags = I2C_M_STOP;
+	msg.len = sizeof(val);
+	msg.buf = &val;
+
+	res = dm_i2c_xfer(dev, &msg, 1);
+
+	if (speed != I2C_SPEED_STANDARD_RATE) {
+		int rv;
+
+		rv = dm_i2c_set_bus_speed(bus, speed);
+		if (rv)
+			debug("Couldn't restore the I2C bus speed\n");
+	}
+
+	return res;
+}
+
 static int atsha204a_send(struct udevice *dev, const u8 *buf, u8 len)
 {
 	fdt_addr_t *priv = dev_get_priv(dev);
@@ -94,7 +135,6 @@ static int atsha204a_recv_resp(struct udevice *dev,
 
 int atsha204a_wakeup(struct udevice *dev)
 {
-	u8 req[4];
 	struct atsha204a_resp resp;
 	int res;
 
@@ -105,10 +145,9 @@ int atsha204a_wakeup(struct udevice *dev)
 	 * when the device is idle, asleep or during waking up.
 	 * Don't check for error when waking up the device.
 	 */
-	memset(req, 0, 4);
-	atsha204a_send(dev, req, 4);
+	atsha204a_ping_bus(dev);
 
-	udelay(ATSHA204A_TWLO_US + ATSHA204A_TWHI_US);
+	udelay(ATSHA204A_TWHI_US);
 
 	res = atsha204a_recv_resp(dev, &resp);
 	if (res) {
-- 
2.44.0.rc0.258.g7320e95886-goog


  parent reply	other threads:[~2024-02-19 16:37 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-19 16:32 [PATCH 0/3] Fix driver for misc/atsha204a Michał Barnaś
2024-02-19 16:32 ` [PATCH 1/3] misc: atsha204a: remove broken for loop Michał Barnaś
2024-02-19 16:32 ` [PATCH 2/3] misc: atsha204a: fix sleep function Michał Barnaś
2024-02-19 16:32 ` Michał Barnaś [this message]
2024-03-05 13:17 ` [PATCH 0/3] Fix driver for misc/atsha204a Tom Rini
2024-03-08 17:13   ` Michał Barnaś

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=20240219163213.855667-4-barnas@google.com \
    --to=barnas@google.com \
    --cc=trini@konsulko.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