Skip to contentSkip to conversation

Fix driver for misc/atsha204a

PATCH In review Michał Barnaś 2 participants 6 messages Feb 19, 2024 – Mar 8, 2024 Char and misc drivers

lore
Share & tools
lore
#0 lore raw

cover Fix driver for misc/atsha204a

Fix the driver to behave like the chip datasheet requires. Improve wake up function to send low signal on SDA line for at least 60us as chip requires to wake up. Fix sleep function to move the chip into sleep mode, not into idle mode. Remove unnecessary for loop, which would never run for more than one iteration.

Michał Barnaś (3):
misc: atsha204a: remove broken for loop
misc: atsha204a: fix sleep function
misc: atsha204a: fix wakeup function

 drivers/misc/atsha204a-i2c.c | 90 ++++++++++++++++++++++++------------
 1 file changed, 61 insertions(+), 29 deletions(-)
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
#1 lore raw

PATCH 1/3 misc: atsha204a: remove broken for loop

Some previous commit changed the continue statement to return, making the for loop used to retry waking up the chip to always return after one iteration. This commit removes the loop, cleaning the code a little.

---

 drivers/misc/atsha204a-i2c.c | 45 +++++++++++++++---------------------
 1 file changed, 19 insertions(+), 26 deletions(-)
drivers/misc/atsha204a-i2c.c +19 -26
index d3c515828f..ab83bbc3e9 100644
@@ -96,40 +96,33 @@ int atsha204a_wakeup(struct udevice *dev)
 {
 	u8 req[4];
 	struct atsha204a_resp resp;
-	int try, res;
+	int res;
 
 	debug("Waking up ATSHA204A\n");
 
-	for (try = 1; try <= 10; ++try) {
-		debug("Try %i... ", try);
-
-		/*
-		 * The device ignores any levels or transitions on the SCL pin
-		 * 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);
+	/*
+	 * The device ignores any levels or transitions on the SCL pin
+	 * 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);
 
-		udelay(ATSHA204A_TWLO_US + ATSHA204A_TWHI_US);
+	udelay(ATSHA204A_TWLO_US + ATSHA204A_TWHI_US);
 
-		res = atsha204a_recv_resp(dev, &resp);
-		if (res) {
-			debug("failed on receiving response, ending\n");
-			return res;
-		}
-
-		if (resp.code != ATSHA204A_STATUS_AFTER_WAKE) {
-			debug ("failed (responce code = %02x), ending\n",
-			       resp.code);
-			return -EBADMSG;
-		}
+	res = atsha204a_recv_resp(dev, &resp);
+	if (res) {
+		debug("failed on receiving response, ending\n");
+		return res;
+	}
 
-		debug("success\n");
-		return 0;
+	if (resp.code != ATSHA204A_STATUS_AFTER_WAKE) {
+		debug("failed (response code = %02x), ending\n", resp.code);
+		return -EBADMSG;
 	}
 
-	return -ETIMEDOUT;
+	debug("success\n");
+	return 0;
 }
 
 int atsha204a_idle(struct udevice *dev)
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
#2 lore raw

PATCH 2/3 misc: atsha204a: fix sleep function

In reply to Michał Barnaś

Fix the sleep function to issue the sleep command instead of idle one.

---

 drivers/misc/atsha204a-i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/misc/atsha204a-i2c.c +1 -1
index ab83bbc3e9..29daefb2a5 100644
@@ -139,7 +139,7 @@ int atsha204a_idle(struct udevice *dev)
 int atsha204a_sleep(struct udevice *dev)
 {
 	int res;
-	u8 req = ATSHA204A_FUNC_IDLE;
+	u8 req = ATSHA204A_FUNC_SLEEP;
 
 	res = atsha204a_send(dev, &req, 1);
 	if (res)
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
#3 lore raw

PATCH 3/3 misc: atsha204a: fix wakeup function

In reply to Michał Barnaś

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.

---

 drivers/misc/atsha204a-i2c.c | 49 ++++++++++++++++++++++++++++++++----
 1 file changed, 44 insertions(+), 5 deletions(-)
drivers/misc/atsha204a-i2c.c +44 -5
index 29daefb2a5..707daa90bd 100644
@@ -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) {
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
#4 lore raw
In reply to Michał Barnaś
Quoted 8 lines · Michał Barnaś

Fix the driver to behave like the chip datasheet requires. Improve wake up function to send low signal on SDA line for at least 60us as chip requires to wake up. Fix sleep function to move the chip into sleep mode, not into idle mode. Remove unnecessary for loop, which would never run for more than one iteration.

[...]

Applied to u-boot/next, thanks!

Signature · 1 line
Tom
#5 lore raw

Thank you very much!

Quoted 18 lines · Tom Rini
Quoted 8 lines · Michał Barnaś

Fix the driver to behave like the chip datasheet requires. Improve wake up function to send low signal on SDA line for at least 60us as chip requires to wake up. Fix sleep function to move the chip into sleep mode, not into idle mode. Remove unnecessary for loop, which would never run for more than one iteration.

[...]

Applied to u-boot/next, thanks!

--
Tom