xref: /freebsd/sys/dev/iicbus/iic_recover_bus.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1930b3123SIan Lepore /*-
2*dbb77490SIan Lepore  * SPDX-License-Identifier: BSD-2-Clause
3*dbb77490SIan Lepore  *
4930b3123SIan Lepore  * Copyright (c) 2017 Ian Lepore <ian@freebsd.org>
5930b3123SIan Lepore  *
6930b3123SIan Lepore  * Development sponsored by Microsemi, Inc.
7930b3123SIan Lepore  *
8930b3123SIan Lepore  * Redistribution and use in source and binary forms, with or without
9930b3123SIan Lepore  * modification, are permitted provided that the following conditions
10930b3123SIan Lepore  * are met:
11930b3123SIan Lepore  * 1. Redistributions of source code must retain the above copyright
12930b3123SIan Lepore  *    notice, this list of conditions and the following disclaimer.
13930b3123SIan Lepore  * 2. Redistributions in binary form must reproduce the above copyright
14930b3123SIan Lepore  *    notice, this list of conditions and the following disclaimer in the
15930b3123SIan Lepore  *    documentation and/or other materials provided with the distribution.
16930b3123SIan Lepore  *
17930b3123SIan Lepore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18930b3123SIan Lepore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19930b3123SIan Lepore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20930b3123SIan Lepore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21930b3123SIan Lepore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22930b3123SIan Lepore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23930b3123SIan Lepore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24930b3123SIan Lepore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25930b3123SIan Lepore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26930b3123SIan Lepore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27930b3123SIan Lepore  * SUCH DAMAGE.
28930b3123SIan Lepore  */
29930b3123SIan Lepore 
30930b3123SIan Lepore #include <sys/cdefs.h>
31930b3123SIan Lepore /*
32930b3123SIan Lepore  * Helper code to recover a hung i2c bus by bit-banging a recovery sequence.
33930b3123SIan Lepore  *
34930b3123SIan Lepore  * An i2c bus can be hung by a slave driving the clock (rare) or data lines low.
35930b3123SIan Lepore  * The most common cause is a partially-completed transaction such as rebooting
36930b3123SIan Lepore  * while a slave is sending a byte of data.  Because i2c allows the clock to
37930b3123SIan Lepore  * freeze for any amount of time, the slave device will continue driving the
38930b3123SIan Lepore  * data line until power is removed, or the clock cycles enough times to
39930b3123SIan Lepore  * complete the current byte.  After completing any partial byte, a START/STOP
40930b3123SIan Lepore  * sequence resets the slave and the bus is recovered.
41930b3123SIan Lepore  *
42930b3123SIan Lepore  * Any i2c driver which is able to manually set the level of the clock and data
43930b3123SIan Lepore  * lines can use this common code for bus recovery.  On many SOCs that have
44930b3123SIan Lepore  * embedded i2c controllers, the i2c pins can be temporarily reassigned as gpio
45930b3123SIan Lepore  * pins to do the bus recovery, then can be assigned back to the i2c hardware.
46930b3123SIan Lepore  */
47930b3123SIan Lepore 
48930b3123SIan Lepore #include "opt_platform.h"
49930b3123SIan Lepore 
50930b3123SIan Lepore #include <sys/param.h>
51930b3123SIan Lepore #include <sys/systm.h>
52930b3123SIan Lepore #include <sys/bus.h>
53930b3123SIan Lepore 
54930b3123SIan Lepore #include <dev/iicbus/iic_recover_bus.h>
55930b3123SIan Lepore #include <dev/iicbus/iiconf.h>
56930b3123SIan Lepore 
57930b3123SIan Lepore int
iic_recover_bus(struct iicrb_pin_access * pins)58930b3123SIan Lepore iic_recover_bus(struct iicrb_pin_access *pins)
59930b3123SIan Lepore {
60930b3123SIan Lepore 	const u_int timeout_us = 40000;
61930b3123SIan Lepore 	const u_int delay_us = 500;
62930b3123SIan Lepore 	int i;
63930b3123SIan Lepore 
64930b3123SIan Lepore 	/*
65930b3123SIan Lepore 	 * Start with clock and data high.
66930b3123SIan Lepore 	 */
67930b3123SIan Lepore 	pins->setsda(pins->ctx, 1);
68930b3123SIan Lepore 	pins->setscl(pins->ctx, 1);
69930b3123SIan Lepore 
70930b3123SIan Lepore 	/*
71930b3123SIan Lepore 	 * At this point, SCL should be high.  If it's not, some slave on the
72930b3123SIan Lepore 	 * bus is doing clock-stretching and we should wait a while.  If that
73930b3123SIan Lepore 	 * slave is completely locked up there may be no way to recover at all.
74930b3123SIan Lepore 	 * We wait up to 40 milliseconds, a seriously pessimistic time (even a
75930b3123SIan Lepore 	 * cheap eeprom has a max post-write delay of only 10ms), and also long
76930b3123SIan Lepore 	 * enough to allow SMB slaves to timeout normally after 35ms.
77930b3123SIan Lepore 	 */
78930b3123SIan Lepore 	for (i = 0; i < timeout_us; i += delay_us) {
79930b3123SIan Lepore 		if (pins->getscl(pins->ctx))
80930b3123SIan Lepore 			break;
81930b3123SIan Lepore 		DELAY(delay_us);
82930b3123SIan Lepore 	}
83930b3123SIan Lepore 	if (i >= timeout_us)
84930b3123SIan Lepore 		return (IIC_EBUSERR);
85930b3123SIan Lepore 
86930b3123SIan Lepore 	/*
87930b3123SIan Lepore 	 * At this point we should be able to control the clock line.  Some
88930b3123SIan Lepore 	 * slave may be part way through a byte transfer, and could be holding
89930b3123SIan Lepore 	 * the data line low waiting for more clock pulses to finish the byte.
90930b3123SIan Lepore 	 * Cycle the clock until we see the data line go high, but only up to 9
91930b3123SIan Lepore 	 * times because if it's not free after 9 clocks we're never going to
92930b3123SIan Lepore 	 * win this battle.  We do 9 max because that's a byte plus an ack/nack
93930b3123SIan Lepore 	 * bit, after which the slave must not be driving the data line anymore.
94930b3123SIan Lepore 	 */
95930b3123SIan Lepore 	for (i = 0; ; ++i) {
96930b3123SIan Lepore 		if (pins->getsda(pins->ctx))
97930b3123SIan Lepore 			break;
98930b3123SIan Lepore 		if (i == 9)
99930b3123SIan Lepore 			return (IIC_EBUSERR);
100930b3123SIan Lepore 		pins->setscl(pins->ctx, 0);
101930b3123SIan Lepore 		DELAY(5);
102930b3123SIan Lepore 		pins->setscl(pins->ctx, 1);
103930b3123SIan Lepore 		DELAY(5);
104930b3123SIan Lepore 	}
105930b3123SIan Lepore 
106930b3123SIan Lepore 	/*
107930b3123SIan Lepore 	 * At this point we should be in control of both the clock and data
108930b3123SIan Lepore 	 * lines, and both lines should be high.  To complete the reset of a
109930b3123SIan Lepore 	 * slave that was part way through a transaction, we need to do a
110930b3123SIan Lepore 	 * START/STOP sequence, which leaves both lines high at the end.
111930b3123SIan Lepore 	 *  - START: SDA transitions high->low while SCL remains high.
112930b3123SIan Lepore 	 *  - STOP:  SDA transitions low->high while SCL remains high.
113930b3123SIan Lepore 	 * Note that even though the clock line remains high, we transition the
114930b3123SIan Lepore 	 * data line no faster than it would change state with a 100khz clock.
115930b3123SIan Lepore 	 */
116930b3123SIan Lepore 	pins->setsda(pins->ctx, 0);
117930b3123SIan Lepore 	DELAY(5);
118930b3123SIan Lepore 	pins->setsda(pins->ctx, 1);
119930b3123SIan Lepore 	DELAY(5);
120930b3123SIan Lepore 
121930b3123SIan Lepore 	return (0);
122930b3123SIan Lepore }
123930b3123SIan Lepore 
124