xref: /freebsd/sys/arm/freescale/imx/imx_i2c.c (revision 94bc2117b4adaf7101cc412f4277e5812ad61ae2)
1484b4fd4SRuslan Bukin /*-
2484b4fd4SRuslan Bukin  * Copyright (C) 2008-2009 Semihalf, Michal Hajduk
3484b4fd4SRuslan Bukin  * Copyright (c) 2012, 2013 The FreeBSD Foundation
4d2c05e20SIan Lepore  * Copyright (c) 2015 Ian Lepore <ian@FreeBSD.org>
5484b4fd4SRuslan Bukin  * All rights reserved.
6484b4fd4SRuslan Bukin  *
7484b4fd4SRuslan Bukin  * Portions of this software were developed by Oleksandr Rybalko
8484b4fd4SRuslan Bukin  * under sponsorship from the FreeBSD Foundation.
9484b4fd4SRuslan Bukin  *
10484b4fd4SRuslan Bukin  * Redistribution and use in source and binary forms, with or without
11484b4fd4SRuslan Bukin  * modification, are permitted provided that the following conditions
12484b4fd4SRuslan Bukin  * are met:
13484b4fd4SRuslan Bukin  * 1. Redistributions of source code must retain the above copyright
14484b4fd4SRuslan Bukin  *    notice, this list of conditions and the following disclaimer.
15484b4fd4SRuslan Bukin  * 2. Redistributions in binary form must reproduce the above copyright
16484b4fd4SRuslan Bukin  *    notice, this list of conditions and the following disclaimer in the
17484b4fd4SRuslan Bukin  *    documentation and/or other materials provided with the distribution.
18484b4fd4SRuslan Bukin  *
19484b4fd4SRuslan Bukin  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20484b4fd4SRuslan Bukin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21484b4fd4SRuslan Bukin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22484b4fd4SRuslan Bukin  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23484b4fd4SRuslan Bukin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24484b4fd4SRuslan Bukin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25484b4fd4SRuslan Bukin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26484b4fd4SRuslan Bukin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27484b4fd4SRuslan Bukin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28484b4fd4SRuslan Bukin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29484b4fd4SRuslan Bukin  * SUCH DAMAGE.
30484b4fd4SRuslan Bukin  */
31484b4fd4SRuslan Bukin 
32d2c05e20SIan Lepore /*
33d2c05e20SIan Lepore  * I2C driver for Freescale i.MX hardware.
34d2c05e20SIan Lepore  *
35d2c05e20SIan Lepore  * Note that the hardware is capable of running as both a master and a slave.
36d2c05e20SIan Lepore  * This driver currently implements only master-mode operations.
37d2c05e20SIan Lepore  *
38db4fcadfSConrad Meyer  * This driver supports multi-master i2c buses, by detecting bus arbitration
39d2c05e20SIan Lepore  * loss and returning IIC_EBUSBSY status.  Notably, it does not do any kind of
40d2c05e20SIan Lepore  * retries if some other master jumps onto the bus and interrupts one of our
41d2c05e20SIan Lepore  * transfer cycles resulting in arbitration loss in mid-transfer.  The caller
42d2c05e20SIan Lepore  * must handle retries in a way that makes sense for the slave being addressed.
43d2c05e20SIan Lepore  */
44d2c05e20SIan Lepore 
45484b4fd4SRuslan Bukin #include <sys/cdefs.h>
46484b4fd4SRuslan Bukin __FBSDID("$FreeBSD$");
47484b4fd4SRuslan Bukin 
48484b4fd4SRuslan Bukin #include <sys/param.h>
49484b4fd4SRuslan Bukin #include <sys/systm.h>
50484b4fd4SRuslan Bukin #include <sys/bus.h>
518928c2e4SIan Lepore #include <sys/gpio.h>
52484b4fd4SRuslan Bukin #include <sys/kernel.h>
53844aff82SIan Lepore #include <sys/limits.h>
54484b4fd4SRuslan Bukin #include <sys/module.h>
55484b4fd4SRuslan Bukin #include <sys/resource.h>
56900fb59eSIan Lepore #include <sys/sysctl.h>
57484b4fd4SRuslan Bukin 
58484b4fd4SRuslan Bukin #include <machine/bus.h>
59484b4fd4SRuslan Bukin #include <machine/resource.h>
60484b4fd4SRuslan Bukin #include <sys/rman.h>
61484b4fd4SRuslan Bukin 
62844aff82SIan Lepore #include <arm/freescale/imx/imx_ccmvar.h>
63844aff82SIan Lepore 
64484b4fd4SRuslan Bukin #include <dev/iicbus/iiconf.h>
65484b4fd4SRuslan Bukin #include <dev/iicbus/iicbus.h>
668928c2e4SIan Lepore #include <dev/iicbus/iic_recover_bus.h>
67484b4fd4SRuslan Bukin #include "iicbus_if.h"
68484b4fd4SRuslan Bukin 
69484b4fd4SRuslan Bukin #include <dev/ofw/openfirm.h>
70484b4fd4SRuslan Bukin #include <dev/ofw/ofw_bus.h>
71484b4fd4SRuslan Bukin #include <dev/ofw/ofw_bus_subr.h>
72484b4fd4SRuslan Bukin 
738928c2e4SIan Lepore #include <dev/fdt/fdt_pinctrl.h>
748928c2e4SIan Lepore #include <dev/gpio/gpiobusvar.h>
758928c2e4SIan Lepore 
76*94bc2117SOleksandr Tymoshenko #if defined(EXT_RESOURCES) && defined(__aarch64__)
77*94bc2117SOleksandr Tymoshenko #define	IMX_ENABLE_CLOCKS
78*94bc2117SOleksandr Tymoshenko #endif
79*94bc2117SOleksandr Tymoshenko 
80*94bc2117SOleksandr Tymoshenko #ifdef IMX_ENABLE_CLOCKS
81*94bc2117SOleksandr Tymoshenko #include <dev/extres/clk/clk.h>
82*94bc2117SOleksandr Tymoshenko #endif
83*94bc2117SOleksandr Tymoshenko 
84484b4fd4SRuslan Bukin #define I2C_ADDR_REG		0x00 /* I2C slave address register */
85484b4fd4SRuslan Bukin #define I2C_FDR_REG		0x04 /* I2C frequency divider register */
86484b4fd4SRuslan Bukin #define I2C_CONTROL_REG		0x08 /* I2C control register */
87484b4fd4SRuslan Bukin #define I2C_STATUS_REG		0x0C /* I2C status register */
88484b4fd4SRuslan Bukin #define I2C_DATA_REG		0x10 /* I2C data register */
89484b4fd4SRuslan Bukin #define I2C_DFSRR_REG		0x14 /* I2C Digital Filter Sampling rate */
90484b4fd4SRuslan Bukin 
91484b4fd4SRuslan Bukin #define I2CCR_MEN		(1 << 7) /* Module enable */
92484b4fd4SRuslan Bukin #define I2CCR_MSTA		(1 << 5) /* Master/slave mode */
93484b4fd4SRuslan Bukin #define I2CCR_MTX		(1 << 4) /* Transmit/receive mode */
94484b4fd4SRuslan Bukin #define I2CCR_TXAK		(1 << 3) /* Transfer acknowledge */
95484b4fd4SRuslan Bukin #define I2CCR_RSTA		(1 << 2) /* Repeated START */
96484b4fd4SRuslan Bukin 
97484b4fd4SRuslan Bukin #define I2CSR_MCF		(1 << 7) /* Data transfer */
98484b4fd4SRuslan Bukin #define I2CSR_MASS		(1 << 6) /* Addressed as a slave */
99484b4fd4SRuslan Bukin #define I2CSR_MBB		(1 << 5) /* Bus busy */
100484b4fd4SRuslan Bukin #define I2CSR_MAL		(1 << 4) /* Arbitration lost */
101484b4fd4SRuslan Bukin #define I2CSR_SRW		(1 << 2) /* Slave read/write */
102484b4fd4SRuslan Bukin #define I2CSR_MIF		(1 << 1) /* Module interrupt */
103484b4fd4SRuslan Bukin #define I2CSR_RXAK		(1 << 0) /* Received acknowledge */
104484b4fd4SRuslan Bukin 
105484b4fd4SRuslan Bukin #define I2C_BAUD_RATE_FAST	0x31
106484b4fd4SRuslan Bukin #define I2C_BAUD_RATE_DEF	0x3F
107484b4fd4SRuslan Bukin #define I2C_DFSSR_DIV		0x10
108484b4fd4SRuslan Bukin 
109844aff82SIan Lepore /*
110844aff82SIan Lepore  * A table of available divisors and the associated coded values to put in the
111844aff82SIan Lepore  * FDR register to achieve that divisor.. There is no algorithmic relationship I
112844aff82SIan Lepore  * can see between divisors and the codes that go into the register.  The table
113844aff82SIan Lepore  * begins and ends with entries that handle insane configuration values.
114844aff82SIan Lepore  */
115844aff82SIan Lepore struct clkdiv {
116844aff82SIan Lepore 	u_int divisor;
117844aff82SIan Lepore 	u_int regcode;
118844aff82SIan Lepore };
119844aff82SIan Lepore static struct clkdiv clkdiv_table[] = {
120844aff82SIan Lepore         {    0, 0x20 }, {   22, 0x20 }, {   24, 0x21 }, {   26, 0x22 },
121844aff82SIan Lepore         {   28, 0x23 }, {   30, 0x00 }, {   32, 0x24 }, {   36, 0x25 },
122844aff82SIan Lepore         {   40, 0x26 }, {   42, 0x03 }, {   44, 0x27 }, {   48, 0x28 },
123844aff82SIan Lepore         {   52, 0x05 }, {   56, 0x29 }, {   60, 0x06 }, {   64, 0x2a },
124844aff82SIan Lepore         {   72, 0x2b }, {   80, 0x2c }, {   88, 0x09 }, {   96, 0x2d },
125844aff82SIan Lepore         {  104, 0x0a }, {  112, 0x2e }, {  128, 0x2f }, {  144, 0x0c },
126844aff82SIan Lepore         {  160, 0x30 }, {  192, 0x31 }, {  224, 0x32 }, {  240, 0x0f },
127844aff82SIan Lepore         {  256, 0x33 }, {  288, 0x10 }, {  320, 0x34 }, {  384, 0x35 },
128844aff82SIan Lepore         {  448, 0x36 }, {  480, 0x13 }, {  512, 0x37 }, {  576, 0x14 },
129844aff82SIan Lepore         {  640, 0x38 }, {  768, 0x39 }, {  896, 0x3a }, {  960, 0x17 },
130844aff82SIan Lepore         { 1024, 0x3b }, { 1152, 0x18 }, { 1280, 0x3c }, { 1536, 0x3d },
131844aff82SIan Lepore         { 1792, 0x3e }, { 1920, 0x1b }, { 2048, 0x3f }, { 2304, 0x1c },
132844aff82SIan Lepore         { 2560, 0x1d }, { 3072, 0x1e }, { 3840, 0x1f }, {UINT_MAX, 0x1f}
133844aff82SIan Lepore };
134844aff82SIan Lepore 
13540d7d632SRuslan Bukin static struct ofw_compat_data compat_data[] = {
136*94bc2117SOleksandr Tymoshenko 	{"fsl,imx21-i2c",  1},
13740d7d632SRuslan Bukin 	{"fsl,imx6q-i2c",  1},
13840d7d632SRuslan Bukin 	{"fsl,imx-i2c",	   1},
13940d7d632SRuslan Bukin 	{NULL,             0}
14040d7d632SRuslan Bukin };
14140d7d632SRuslan Bukin 
142484b4fd4SRuslan Bukin struct i2c_softc {
143484b4fd4SRuslan Bukin 	device_t		dev;
144484b4fd4SRuslan Bukin 	device_t		iicbus;
145484b4fd4SRuslan Bukin 	struct resource		*res;
146484b4fd4SRuslan Bukin 	int			rid;
147d2c05e20SIan Lepore 	sbintime_t		byte_time_sbt;
1488928c2e4SIan Lepore 	int			rb_pinctl_idx;
1498928c2e4SIan Lepore 	gpio_pin_t		rb_sclpin;
1508928c2e4SIan Lepore 	gpio_pin_t 		rb_sdapin;
151900fb59eSIan Lepore 	u_int			debug;
152900fb59eSIan Lepore 	u_int			slave;
153*94bc2117SOleksandr Tymoshenko #ifdef IMX_ENABLE_CLOCKS
154*94bc2117SOleksandr Tymoshenko 	clk_t			ipgclk;
155*94bc2117SOleksandr Tymoshenko #endif
156484b4fd4SRuslan Bukin };
157484b4fd4SRuslan Bukin 
158900fb59eSIan Lepore #define DEVICE_DEBUGF(sc, lvl, fmt, args...) \
159900fb59eSIan Lepore     if ((lvl) <= (sc)->debug) \
160900fb59eSIan Lepore         device_printf((sc)->dev, fmt, ##args)
161900fb59eSIan Lepore 
162900fb59eSIan Lepore #define DEBUGF(sc, lvl, fmt, args...) \
163900fb59eSIan Lepore     if ((lvl) <= (sc)->debug) \
164900fb59eSIan Lepore         printf(fmt, ##args)
165900fb59eSIan Lepore 
166484b4fd4SRuslan Bukin static phandle_t i2c_get_node(device_t, device_t);
167484b4fd4SRuslan Bukin static int i2c_probe(device_t);
168484b4fd4SRuslan Bukin static int i2c_attach(device_t);
169b107b904SIan Lepore static int i2c_detach(device_t);
170484b4fd4SRuslan Bukin 
171484b4fd4SRuslan Bukin static int i2c_repeated_start(device_t, u_char, int);
172484b4fd4SRuslan Bukin static int i2c_start(device_t, u_char, int);
173484b4fd4SRuslan Bukin static int i2c_stop(device_t);
174484b4fd4SRuslan Bukin static int i2c_reset(device_t, u_char, u_char, u_char *);
175484b4fd4SRuslan Bukin static int i2c_read(device_t, char *, int, int *, int, int);
176484b4fd4SRuslan Bukin static int i2c_write(device_t, const char *, int, int *, int);
177484b4fd4SRuslan Bukin 
178484b4fd4SRuslan Bukin static device_method_t i2c_methods[] = {
179484b4fd4SRuslan Bukin 	DEVMETHOD(device_probe,			i2c_probe),
180484b4fd4SRuslan Bukin 	DEVMETHOD(device_attach,		i2c_attach),
181b107b904SIan Lepore 	DEVMETHOD(device_detach,		i2c_detach),
182484b4fd4SRuslan Bukin 
183484b4fd4SRuslan Bukin 	/* OFW methods */
184484b4fd4SRuslan Bukin 	DEVMETHOD(ofw_bus_get_node,		i2c_get_node),
185484b4fd4SRuslan Bukin 
186484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_callback,		iicbus_null_callback),
187484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_repeated_start,	i2c_repeated_start),
188484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_start,			i2c_start),
189484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_stop,			i2c_stop),
190484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_reset,			i2c_reset),
191484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_read,			i2c_read),
192484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_write,			i2c_write),
193484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_transfer,		iicbus_transfer_gen),
194484b4fd4SRuslan Bukin 
195d2c05e20SIan Lepore 	DEVMETHOD_END
196484b4fd4SRuslan Bukin };
197484b4fd4SRuslan Bukin 
198484b4fd4SRuslan Bukin static driver_t i2c_driver = {
199af85a3d1SIan Lepore 	"imx_i2c",
200484b4fd4SRuslan Bukin 	i2c_methods,
201484b4fd4SRuslan Bukin 	sizeof(struct i2c_softc),
202484b4fd4SRuslan Bukin };
203484b4fd4SRuslan Bukin static devclass_t  i2c_devclass;
204484b4fd4SRuslan Bukin 
205af85a3d1SIan Lepore DRIVER_MODULE(imx_i2c, simplebus, i2c_driver, i2c_devclass, 0, 0);
206af85a3d1SIan Lepore DRIVER_MODULE(ofw_iicbus, imx_i2c, ofw_iicbus_driver, ofw_iicbus_devclass, 0, 0);
2075e2d7489SIan Lepore MODULE_DEPEND(imx_i2c, iicbus, 1, 1, 1);
208134399fcSIan Lepore SIMPLEBUS_PNP_INFO(compat_data);
209484b4fd4SRuslan Bukin 
210484b4fd4SRuslan Bukin static phandle_t
211484b4fd4SRuslan Bukin i2c_get_node(device_t bus, device_t dev)
212484b4fd4SRuslan Bukin {
213484b4fd4SRuslan Bukin 	/*
214484b4fd4SRuslan Bukin 	 * Share controller node with iicbus device
215484b4fd4SRuslan Bukin 	 */
216484b4fd4SRuslan Bukin 	return ofw_bus_get_node(bus);
217484b4fd4SRuslan Bukin }
218484b4fd4SRuslan Bukin 
219484b4fd4SRuslan Bukin static __inline void
220484b4fd4SRuslan Bukin i2c_write_reg(struct i2c_softc *sc, bus_size_t off, uint8_t val)
221484b4fd4SRuslan Bukin {
222484b4fd4SRuslan Bukin 
223d2c05e20SIan Lepore 	bus_write_1(sc->res, off, val);
224484b4fd4SRuslan Bukin }
225484b4fd4SRuslan Bukin 
226484b4fd4SRuslan Bukin static __inline uint8_t
227484b4fd4SRuslan Bukin i2c_read_reg(struct i2c_softc *sc, bus_size_t off)
228484b4fd4SRuslan Bukin {
229484b4fd4SRuslan Bukin 
230d2c05e20SIan Lepore 	return (bus_read_1(sc->res, off));
231484b4fd4SRuslan Bukin }
232484b4fd4SRuslan Bukin 
233484b4fd4SRuslan Bukin static __inline void
234484b4fd4SRuslan Bukin i2c_flag_set(struct i2c_softc *sc, bus_size_t off, uint8_t mask)
235484b4fd4SRuslan Bukin {
236484b4fd4SRuslan Bukin 	uint8_t status;
237484b4fd4SRuslan Bukin 
238484b4fd4SRuslan Bukin 	status = i2c_read_reg(sc, off);
239484b4fd4SRuslan Bukin 	status |= mask;
240484b4fd4SRuslan Bukin 	i2c_write_reg(sc, off, status);
241484b4fd4SRuslan Bukin }
242484b4fd4SRuslan Bukin 
243d2c05e20SIan Lepore /* Wait for bus to become busy or not-busy. */
244484b4fd4SRuslan Bukin static int
245d2c05e20SIan Lepore wait_for_busbusy(struct i2c_softc *sc, int wantbusy)
246484b4fd4SRuslan Bukin {
247d2c05e20SIan Lepore 	int retry, srb;
248484b4fd4SRuslan Bukin 
249484b4fd4SRuslan Bukin 	retry = 1000;
250484b4fd4SRuslan Bukin 	while (retry --) {
251d2c05e20SIan Lepore 		srb = i2c_read_reg(sc, I2C_STATUS_REG) & I2CSR_MBB;
252d2c05e20SIan Lepore 		if ((srb && wantbusy) || (!srb && !wantbusy))
253484b4fd4SRuslan Bukin 			return (IIC_NOERR);
254d2c05e20SIan Lepore 		DELAY(1);
255484b4fd4SRuslan Bukin 	}
256484b4fd4SRuslan Bukin 	return (IIC_ETIMEOUT);
257484b4fd4SRuslan Bukin }
258484b4fd4SRuslan Bukin 
259d2c05e20SIan Lepore /* Wait for transfer to complete, optionally check RXAK. */
260484b4fd4SRuslan Bukin static int
261d2c05e20SIan Lepore wait_for_xfer(struct i2c_softc *sc, int checkack)
262484b4fd4SRuslan Bukin {
263d2c05e20SIan Lepore 	int retry, sr;
264484b4fd4SRuslan Bukin 
265d2c05e20SIan Lepore 	/*
266d2c05e20SIan Lepore 	 * Sleep for about the time it takes to transfer a byte (with precision
267d2c05e20SIan Lepore 	 * set to tolerate 5% oversleep).  We calculate the approximate byte
268d2c05e20SIan Lepore 	 * transfer time when we set the bus speed divisor.  Slaves are allowed
269d2c05e20SIan Lepore 	 * to do clock-stretching so the actual transfer time can be larger, but
270d2c05e20SIan Lepore 	 * this gets the bulk of the waiting out of the way without tying up the
271d2c05e20SIan Lepore 	 * processor the whole time.
272d2c05e20SIan Lepore 	 */
273d2c05e20SIan Lepore 	pause_sbt("imxi2c", sc->byte_time_sbt, sc->byte_time_sbt / 20, 0);
274d2c05e20SIan Lepore 
275d2c05e20SIan Lepore 	retry = 10000;
276484b4fd4SRuslan Bukin 	while (retry --) {
277d2c05e20SIan Lepore 		sr = i2c_read_reg(sc, I2C_STATUS_REG);
278d2c05e20SIan Lepore 		if (sr & I2CSR_MIF) {
279d2c05e20SIan Lepore                         if (sr & I2CSR_MAL)
280d1e99670SIan Lepore 				return (IIC_EBUSERR);
281d2c05e20SIan Lepore 			else if (checkack && (sr & I2CSR_RXAK))
282d2c05e20SIan Lepore 				return (IIC_ENOACK);
283d2c05e20SIan Lepore 			else
284484b4fd4SRuslan Bukin 				return (IIC_NOERR);
285484b4fd4SRuslan Bukin 		}
286d2c05e20SIan Lepore 		DELAY(1);
287d2c05e20SIan Lepore 	}
288484b4fd4SRuslan Bukin 	return (IIC_ETIMEOUT);
289484b4fd4SRuslan Bukin }
290484b4fd4SRuslan Bukin 
291d2c05e20SIan Lepore /*
292d2c05e20SIan Lepore  * Implement the error handling shown in the state diagram of the imx6 reference
293d2c05e20SIan Lepore  * manual.  If there was an error, then:
294d2c05e20SIan Lepore  *  - Clear master mode (MSTA and MTX).
295d2c05e20SIan Lepore  *  - Wait for the bus to become free or for a timeout to happen.
296d2c05e20SIan Lepore  *  - Disable the controller.
297d2c05e20SIan Lepore  */
298484b4fd4SRuslan Bukin static int
299d2c05e20SIan Lepore i2c_error_handler(struct i2c_softc *sc, int error)
300484b4fd4SRuslan Bukin {
301484b4fd4SRuslan Bukin 
302d2c05e20SIan Lepore 	if (error != 0) {
303d2c05e20SIan Lepore 		i2c_write_reg(sc, I2C_STATUS_REG, 0);
304d2c05e20SIan Lepore 		i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN);
305d2c05e20SIan Lepore 		wait_for_busbusy(sc, false);
306d2c05e20SIan Lepore 		i2c_write_reg(sc, I2C_CONTROL_REG, 0);
307484b4fd4SRuslan Bukin 	}
308d2c05e20SIan Lepore 	return (error);
309484b4fd4SRuslan Bukin }
310484b4fd4SRuslan Bukin 
311484b4fd4SRuslan Bukin static int
3128928c2e4SIan Lepore i2c_recover_getsda(void *ctx)
3138928c2e4SIan Lepore {
3148928c2e4SIan Lepore 	bool active;
3158928c2e4SIan Lepore 
3168928c2e4SIan Lepore 	gpio_pin_is_active(((struct i2c_softc *)ctx)->rb_sdapin, &active);
3178928c2e4SIan Lepore 	return (active);
3188928c2e4SIan Lepore }
3198928c2e4SIan Lepore 
3208928c2e4SIan Lepore static void
3218928c2e4SIan Lepore i2c_recover_setsda(void *ctx, int value)
3228928c2e4SIan Lepore {
3238928c2e4SIan Lepore 
3248928c2e4SIan Lepore 	gpio_pin_set_active(((struct i2c_softc *)ctx)->rb_sdapin, value);
3258928c2e4SIan Lepore }
3268928c2e4SIan Lepore 
3278928c2e4SIan Lepore static int
3288928c2e4SIan Lepore i2c_recover_getscl(void *ctx)
3298928c2e4SIan Lepore {
3308928c2e4SIan Lepore 	bool active;
3318928c2e4SIan Lepore 
3328928c2e4SIan Lepore 	gpio_pin_is_active(((struct i2c_softc *)ctx)->rb_sclpin, &active);
3338928c2e4SIan Lepore 	return (active);
3348928c2e4SIan Lepore 
3358928c2e4SIan Lepore }
3368928c2e4SIan Lepore 
3378928c2e4SIan Lepore static void
3388928c2e4SIan Lepore i2c_recover_setscl(void *ctx, int value)
3398928c2e4SIan Lepore {
3408928c2e4SIan Lepore 
3418928c2e4SIan Lepore 	gpio_pin_set_active(((struct i2c_softc *)ctx)->rb_sclpin, value);
3428928c2e4SIan Lepore }
3438928c2e4SIan Lepore 
3448928c2e4SIan Lepore static int
3458928c2e4SIan Lepore i2c_recover_bus(struct i2c_softc *sc)
3468928c2e4SIan Lepore {
3478928c2e4SIan Lepore 	struct iicrb_pin_access pins;
3488928c2e4SIan Lepore 	int err;
3498928c2e4SIan Lepore 
3508928c2e4SIan Lepore 	/*
3518928c2e4SIan Lepore 	 * If we have gpio pinmux config, reconfigure the pins to gpio mode,
3528928c2e4SIan Lepore 	 * invoke iic_recover_bus which checks for a hung bus and bitbangs a
3538928c2e4SIan Lepore 	 * recovery sequence if necessary, then configure the pins back to i2c
3548928c2e4SIan Lepore 	 * mode (idx 0).
3558928c2e4SIan Lepore 	 */
3568928c2e4SIan Lepore 	if (sc->rb_pinctl_idx == 0)
3578928c2e4SIan Lepore 		return (0);
3588928c2e4SIan Lepore 
3598928c2e4SIan Lepore 	fdt_pinctrl_configure(sc->dev, sc->rb_pinctl_idx);
3608928c2e4SIan Lepore 
3618928c2e4SIan Lepore 	pins.ctx = sc;
3628928c2e4SIan Lepore 	pins.getsda = i2c_recover_getsda;
3638928c2e4SIan Lepore 	pins.setsda = i2c_recover_setsda;
3648928c2e4SIan Lepore 	pins.getscl = i2c_recover_getscl;
3658928c2e4SIan Lepore 	pins.setscl = i2c_recover_setscl;
3668928c2e4SIan Lepore 	err = iic_recover_bus(&pins);
3678928c2e4SIan Lepore 
3688928c2e4SIan Lepore 	fdt_pinctrl_configure(sc->dev, 0);
3698928c2e4SIan Lepore 
3708928c2e4SIan Lepore 	return (err);
3718928c2e4SIan Lepore }
3728928c2e4SIan Lepore 
3738928c2e4SIan Lepore static int
374484b4fd4SRuslan Bukin i2c_probe(device_t dev)
375484b4fd4SRuslan Bukin {
376484b4fd4SRuslan Bukin 
377484b4fd4SRuslan Bukin 	if (!ofw_bus_status_okay(dev))
378484b4fd4SRuslan Bukin 		return (ENXIO);
379484b4fd4SRuslan Bukin 
38040d7d632SRuslan Bukin 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
381484b4fd4SRuslan Bukin 		return (ENXIO);
382484b4fd4SRuslan Bukin 
383d2c05e20SIan Lepore 	device_set_desc(dev, "Freescale i.MX I2C");
384484b4fd4SRuslan Bukin 
385484b4fd4SRuslan Bukin 	return (BUS_PROBE_DEFAULT);
386484b4fd4SRuslan Bukin }
387484b4fd4SRuslan Bukin 
388484b4fd4SRuslan Bukin static int
389484b4fd4SRuslan Bukin i2c_attach(device_t dev)
390484b4fd4SRuslan Bukin {
3918928c2e4SIan Lepore 	char wrkstr[16];
392484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
3938928c2e4SIan Lepore 	phandle_t node;
3948928c2e4SIan Lepore 	int err, cfgidx;
395484b4fd4SRuslan Bukin 
396484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
397484b4fd4SRuslan Bukin 	sc->dev = dev;
398484b4fd4SRuslan Bukin 	sc->rid = 0;
399484b4fd4SRuslan Bukin 
400*94bc2117SOleksandr Tymoshenko #ifdef IMX_ENABLE_CLOCKS
401*94bc2117SOleksandr Tymoshenko 	if (clk_get_by_ofw_index(sc->dev, 0, 0, &sc->ipgclk) != 0) {
402*94bc2117SOleksandr Tymoshenko 		device_printf(dev, "could not get ipg clock");
403*94bc2117SOleksandr Tymoshenko 		return (ENOENT);
404*94bc2117SOleksandr Tymoshenko 	}
405*94bc2117SOleksandr Tymoshenko 
406*94bc2117SOleksandr Tymoshenko 	err = clk_enable(sc->ipgclk);
407*94bc2117SOleksandr Tymoshenko 	if (err != 0) {
408*94bc2117SOleksandr Tymoshenko 		device_printf(sc->dev, "could not enable ipg clock\n");
409*94bc2117SOleksandr Tymoshenko 		return (err);
410*94bc2117SOleksandr Tymoshenko 	}
411*94bc2117SOleksandr Tymoshenko #endif
412*94bc2117SOleksandr Tymoshenko 
413484b4fd4SRuslan Bukin 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
414484b4fd4SRuslan Bukin 	    RF_ACTIVE);
415484b4fd4SRuslan Bukin 	if (sc->res == NULL) {
416484b4fd4SRuslan Bukin 		device_printf(dev, "could not allocate resources");
417484b4fd4SRuslan Bukin 		return (ENXIO);
418484b4fd4SRuslan Bukin 	}
419484b4fd4SRuslan Bukin 
420484b4fd4SRuslan Bukin 	sc->iicbus = device_add_child(dev, "iicbus", -1);
421484b4fd4SRuslan Bukin 	if (sc->iicbus == NULL) {
422484b4fd4SRuslan Bukin 		device_printf(dev, "could not add iicbus child");
423484b4fd4SRuslan Bukin 		return (ENXIO);
424484b4fd4SRuslan Bukin 	}
425484b4fd4SRuslan Bukin 
426900fb59eSIan Lepore 	/* Set up debug-enable sysctl. */
427900fb59eSIan Lepore 	SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->dev),
428900fb59eSIan Lepore 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
429900fb59eSIan Lepore 	    OID_AUTO, "debug", CTLFLAG_RWTUN, &sc->debug, 0,
430900fb59eSIan Lepore 	    "Enable debug; 1=reads/writes, 2=add starts/stops");
431900fb59eSIan Lepore 
4328928c2e4SIan Lepore 	/*
4338928c2e4SIan Lepore 	 * Set up for bus recovery using gpio pins, if the pinctrl and gpio
4348928c2e4SIan Lepore 	 * properties are present.  This is optional.  If all the config data is
4358928c2e4SIan Lepore 	 * not in place, we just don't do gpio bitbang bus recovery.
4368928c2e4SIan Lepore 	 */
4378928c2e4SIan Lepore 	node = ofw_bus_get_node(sc->dev);
4388928c2e4SIan Lepore 
4398928c2e4SIan Lepore 	err = gpio_pin_get_by_ofw_property(dev, node, "scl-gpios",
4408928c2e4SIan Lepore 	    &sc->rb_sclpin);
4418928c2e4SIan Lepore 	if (err != 0)
4428928c2e4SIan Lepore 		goto no_recovery;
4438928c2e4SIan Lepore 	err = gpio_pin_get_by_ofw_property(dev, node, "sda-gpios",
4448928c2e4SIan Lepore 	    &sc->rb_sdapin);
4458928c2e4SIan Lepore 	if (err != 0)
4468928c2e4SIan Lepore 		goto no_recovery;
4478928c2e4SIan Lepore 
4488928c2e4SIan Lepore 	/*
4498928c2e4SIan Lepore 	 * Preset the gpio pins to output high (idle bus state).  The signal
4508928c2e4SIan Lepore 	 * won't actually appear on the pins until the bus recovery code changes
4518928c2e4SIan Lepore 	 * the pinmux config from i2c to gpio.
4528928c2e4SIan Lepore 	 */
4538928c2e4SIan Lepore 	gpio_pin_setflags(sc->rb_sclpin, GPIO_PIN_OUTPUT);
4548928c2e4SIan Lepore 	gpio_pin_setflags(sc->rb_sdapin, GPIO_PIN_OUTPUT);
4558928c2e4SIan Lepore 	gpio_pin_set_active(sc->rb_sclpin, true);
4568928c2e4SIan Lepore 	gpio_pin_set_active(sc->rb_sdapin, true);
4578928c2e4SIan Lepore 
4588928c2e4SIan Lepore 	/*
4598928c2e4SIan Lepore 	 * Obtain the index of pinctrl node for bus recovery using gpio pins,
4608928c2e4SIan Lepore 	 * then confirm that pinctrl properties exist for that index and for the
4618928c2e4SIan Lepore 	 * default pinctrl-0.  If sc->rb_pinctl_idx is non-zero, the reset code
4628928c2e4SIan Lepore 	 * will also do a bus recovery, so setting this value must be last.
4638928c2e4SIan Lepore 	 */
4648928c2e4SIan Lepore 	err = ofw_bus_find_string_index(node, "pinctrl-names", "gpio", &cfgidx);
4658928c2e4SIan Lepore 	if (err == 0) {
4668928c2e4SIan Lepore 		snprintf(wrkstr, sizeof(wrkstr), "pinctrl-%d", cfgidx);
4678928c2e4SIan Lepore 		if (OF_hasprop(node, "pinctrl-0") && OF_hasprop(node, wrkstr))
4688928c2e4SIan Lepore 			sc->rb_pinctl_idx = cfgidx;
4698928c2e4SIan Lepore 	}
4708928c2e4SIan Lepore 
4718928c2e4SIan Lepore no_recovery:
4728928c2e4SIan Lepore 
4738928c2e4SIan Lepore 	/* We don't do a hardware reset here because iicbus_attach() does it. */
4748928c2e4SIan Lepore 
4751e4042d4SIan Lepore 	/* Probe and attach the iicbus when interrupts are available. */
476b832a7e5SWarner Losh 	return (bus_delayed_attach_children(dev));
477484b4fd4SRuslan Bukin }
478484b4fd4SRuslan Bukin 
479484b4fd4SRuslan Bukin static int
480b107b904SIan Lepore i2c_detach(device_t dev)
481b107b904SIan Lepore {
482b107b904SIan Lepore 	struct i2c_softc *sc;
483b107b904SIan Lepore 	int error;
484b107b904SIan Lepore 
485b107b904SIan Lepore 	sc = device_get_softc(dev);
486b107b904SIan Lepore 
487*94bc2117SOleksandr Tymoshenko #ifdef IMX_ENABLE_CLOCKS
488*94bc2117SOleksandr Tymoshenko 	error = clk_disable(sc->ipgclk);
489*94bc2117SOleksandr Tymoshenko 	if (error != 0) {
490*94bc2117SOleksandr Tymoshenko 		device_printf(sc->dev, "could not disable ipg clock\n");
491*94bc2117SOleksandr Tymoshenko 		return (error);
492*94bc2117SOleksandr Tymoshenko 	}
493*94bc2117SOleksandr Tymoshenko #endif
494*94bc2117SOleksandr Tymoshenko 
495b107b904SIan Lepore 	if ((error = bus_generic_detach(sc->dev)) != 0) {
496b107b904SIan Lepore 		device_printf(sc->dev, "cannot detach child devices\n");
497b107b904SIan Lepore 		return (error);
498b107b904SIan Lepore 	}
499b107b904SIan Lepore 
500b107b904SIan Lepore 	if (sc->iicbus != NULL)
501b107b904SIan Lepore 		device_delete_child(dev, sc->iicbus);
502b107b904SIan Lepore 
503ecb53c09SIan Lepore 	/* Release bus-recover pins; gpio_pin_release() handles NULL args. */
504ecb53c09SIan Lepore 	gpio_pin_release(sc->rb_sclpin);
505ecb53c09SIan Lepore 	gpio_pin_release(sc->rb_sdapin);
506ecb53c09SIan Lepore 
507b107b904SIan Lepore 	if (sc->res != NULL)
508b107b904SIan Lepore 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->res);
509b107b904SIan Lepore 
510b107b904SIan Lepore 	return (0);
511b107b904SIan Lepore }
512b107b904SIan Lepore 
513b107b904SIan Lepore static int
514484b4fd4SRuslan Bukin i2c_repeated_start(device_t dev, u_char slave, int timeout)
515484b4fd4SRuslan Bukin {
516484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
517484b4fd4SRuslan Bukin 	int error;
518484b4fd4SRuslan Bukin 
519484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
520484b4fd4SRuslan Bukin 
521484b4fd4SRuslan Bukin 	if ((i2c_read_reg(sc, I2C_STATUS_REG) & I2CSR_MBB) == 0) {
522d2c05e20SIan Lepore 		return (IIC_EBUSERR);
523484b4fd4SRuslan Bukin 	}
524484b4fd4SRuslan Bukin 
525d2c05e20SIan Lepore 	/*
526d2c05e20SIan Lepore 	 * Set repeated start condition, delay (per reference manual, min 156nS)
527d2c05e20SIan Lepore 	 * before writing slave address, wait for ack after write.
528d2c05e20SIan Lepore 	 */
529484b4fd4SRuslan Bukin 	i2c_flag_set(sc, I2C_CONTROL_REG, I2CCR_RSTA);
530d2c05e20SIan Lepore 	DELAY(1);
531484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
532484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_DATA_REG, slave);
533900fb59eSIan Lepore 	sc->slave = slave;
534900fb59eSIan Lepore 	DEVICE_DEBUGF(sc, 2, "rstart 0x%02x\n", sc->slave);
535d2c05e20SIan Lepore 	error = wait_for_xfer(sc, true);
536d2c05e20SIan Lepore 	return (i2c_error_handler(sc, error));
537484b4fd4SRuslan Bukin }
538484b4fd4SRuslan Bukin 
539484b4fd4SRuslan Bukin static int
5408928c2e4SIan Lepore i2c_start_ll(device_t dev, u_char slave, int timeout)
541484b4fd4SRuslan Bukin {
542484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
543484b4fd4SRuslan Bukin 	int error;
544484b4fd4SRuslan Bukin 
545484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
546484b4fd4SRuslan Bukin 
547d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN);
548d2c05e20SIan Lepore 	DELAY(10); /* Delay for controller to sample bus state. */
549484b4fd4SRuslan Bukin 	if (i2c_read_reg(sc, I2C_STATUS_REG) & I2CSR_MBB) {
550d1e99670SIan Lepore 		return (i2c_error_handler(sc, IIC_EBUSERR));
551484b4fd4SRuslan Bukin 	}
552d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN | I2CCR_MSTA | I2CCR_MTX);
553d2c05e20SIan Lepore 	if ((error = wait_for_busbusy(sc, true)) != IIC_NOERR)
554d2c05e20SIan Lepore 		return (i2c_error_handler(sc, error));
555d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_STATUS_REG, 0);
556484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_DATA_REG, slave);
557900fb59eSIan Lepore 	sc->slave = slave;
558900fb59eSIan Lepore 	DEVICE_DEBUGF(sc, 2, "start  0x%02x\n", sc->slave);
559d2c05e20SIan Lepore 	error = wait_for_xfer(sc, true);
560d2c05e20SIan Lepore 	return (i2c_error_handler(sc, error));
561484b4fd4SRuslan Bukin }
562484b4fd4SRuslan Bukin 
563484b4fd4SRuslan Bukin static int
5648928c2e4SIan Lepore i2c_start(device_t dev, u_char slave, int timeout)
5658928c2e4SIan Lepore {
5668928c2e4SIan Lepore 	struct i2c_softc *sc;
5678928c2e4SIan Lepore 	int error;
5688928c2e4SIan Lepore 
5698928c2e4SIan Lepore 	sc = device_get_softc(dev);
5708928c2e4SIan Lepore 
5718928c2e4SIan Lepore 	/*
5728928c2e4SIan Lepore 	 * Invoke the low-level code to put the bus into master mode and address
5738928c2e4SIan Lepore 	 * the given slave.  If that fails, idle the controller and attempt a
5748928c2e4SIan Lepore 	 * bus recovery, and then try again one time.  Signaling a start and
5758928c2e4SIan Lepore 	 * addressing the slave is the only operation that a low-level driver
5768928c2e4SIan Lepore 	 * can safely retry without any help from the upper layers that know
5778928c2e4SIan Lepore 	 * more about the slave device.
5788928c2e4SIan Lepore 	 */
5798928c2e4SIan Lepore 	if ((error = i2c_start_ll(dev, slave, timeout)) != 0) {
5808928c2e4SIan Lepore 		i2c_write_reg(sc, I2C_CONTROL_REG, 0x0);
5818928c2e4SIan Lepore 		if ((error = i2c_recover_bus(sc)) != 0)
5828928c2e4SIan Lepore 			return (error);
5838928c2e4SIan Lepore 		error = i2c_start_ll(dev, slave, timeout);
5848928c2e4SIan Lepore 	}
5858928c2e4SIan Lepore 	return (error);
5868928c2e4SIan Lepore }
5878928c2e4SIan Lepore 
5888928c2e4SIan Lepore static int
589484b4fd4SRuslan Bukin i2c_stop(device_t dev)
590484b4fd4SRuslan Bukin {
591484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
592484b4fd4SRuslan Bukin 
593484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
594d2c05e20SIan Lepore 
595d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN);
596d2c05e20SIan Lepore 	wait_for_busbusy(sc, false);
597484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_CONTROL_REG, 0);
598900fb59eSIan Lepore 	DEVICE_DEBUGF(sc, 2, "stop   0x%02x\n", sc->slave);
599484b4fd4SRuslan Bukin 	return (IIC_NOERR);
600484b4fd4SRuslan Bukin }
601484b4fd4SRuslan Bukin 
602484b4fd4SRuslan Bukin static int
603484b4fd4SRuslan Bukin i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldadr)
604484b4fd4SRuslan Bukin {
605484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
606844aff82SIan Lepore 	u_int busfreq, div, i, ipgfreq;
607*94bc2117SOleksandr Tymoshenko #ifdef IMX_ENABLE_CLOCKS
608*94bc2117SOleksandr Tymoshenko 	int err;
609*94bc2117SOleksandr Tymoshenko 	uint64_t freq;
610*94bc2117SOleksandr Tymoshenko #endif
611484b4fd4SRuslan Bukin 
612484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
613484b4fd4SRuslan Bukin 
614900fb59eSIan Lepore 	DEVICE_DEBUGF(sc, 1, "reset\n");
615900fb59eSIan Lepore 
616844aff82SIan Lepore 	/*
617844aff82SIan Lepore 	 * Look up the divisor that gives the nearest speed that doesn't exceed
618844aff82SIan Lepore 	 * the configured value for the bus.
619844aff82SIan Lepore 	 */
620*94bc2117SOleksandr Tymoshenko #ifdef IMX_ENABLE_CLOCKS
621*94bc2117SOleksandr Tymoshenko 	err = clk_get_freq(sc->ipgclk, &freq);
622*94bc2117SOleksandr Tymoshenko 	if (err != 0) {
623*94bc2117SOleksandr Tymoshenko 		device_printf(sc->dev, "cannot get frequency\n");
624*94bc2117SOleksandr Tymoshenko 		return (err);
625*94bc2117SOleksandr Tymoshenko 	}
626*94bc2117SOleksandr Tymoshenko 	ipgfreq = (int32_t)freq;
627*94bc2117SOleksandr Tymoshenko #else
628844aff82SIan Lepore 	ipgfreq = imx_ccm_ipg_hz();
629*94bc2117SOleksandr Tymoshenko #endif
630844aff82SIan Lepore 	busfreq = IICBUS_GET_FREQUENCY(sc->iicbus, speed);
631f0e56111SPedro F. Giffuni 	div = howmany(ipgfreq, busfreq);
632844aff82SIan Lepore 	for (i = 0; i < nitems(clkdiv_table); i++) {
633844aff82SIan Lepore 		if (clkdiv_table[i].divisor >= div)
634484b4fd4SRuslan Bukin 			break;
635484b4fd4SRuslan Bukin 	}
636484b4fd4SRuslan Bukin 
637d2c05e20SIan Lepore 	/*
638d2c05e20SIan Lepore 	 * Calculate roughly how long it will take to transfer a byte (which
639d2c05e20SIan Lepore 	 * requires 9 clock cycles) at the new bus speed.  This value is used to
640d2c05e20SIan Lepore 	 * pause() while waiting for transfer-complete.  With a 66MHz IPG clock
641d2c05e20SIan Lepore 	 * and the actual i2c bus speeds that leads to, for nominal 100KHz and
642d2c05e20SIan Lepore 	 * 400KHz bus speeds the transfer times are roughly 104uS and 22uS.
643d2c05e20SIan Lepore 	 */
644d2c05e20SIan Lepore 	busfreq = ipgfreq / clkdiv_table[i].divisor;
645d2c05e20SIan Lepore 	sc->byte_time_sbt = SBT_1US * (9000000 / busfreq);
646d2c05e20SIan Lepore 
647d2c05e20SIan Lepore 	/*
648d2c05e20SIan Lepore 	 * Disable the controller (do the reset), and set the new clock divisor.
649d2c05e20SIan Lepore 	 */
650d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
651484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_CONTROL_REG, 0x0);
652d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_FDR_REG, (uint8_t)clkdiv_table[i].regcode);
6538928c2e4SIan Lepore 
6548928c2e4SIan Lepore 	/*
6558928c2e4SIan Lepore 	 * Now that the controller is idle, perform bus recovery.  If the bus
6568928c2e4SIan Lepore 	 * isn't hung, this a fairly fast no-op.
6578928c2e4SIan Lepore 	 */
6588928c2e4SIan Lepore 	return (i2c_recover_bus(sc));
659484b4fd4SRuslan Bukin }
660484b4fd4SRuslan Bukin 
661484b4fd4SRuslan Bukin static int
662484b4fd4SRuslan Bukin i2c_read(device_t dev, char *buf, int len, int *read, int last, int delay)
663484b4fd4SRuslan Bukin {
664484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
665484b4fd4SRuslan Bukin 	int error, reg;
666484b4fd4SRuslan Bukin 
667484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
668484b4fd4SRuslan Bukin 	*read = 0;
669484b4fd4SRuslan Bukin 
670900fb59eSIan Lepore 	DEVICE_DEBUGF(sc, 1, "read   0x%02x len %d: ", sc->slave, len);
671484b4fd4SRuslan Bukin 	if (len) {
672484b4fd4SRuslan Bukin 		if (len == 1)
673484b4fd4SRuslan Bukin 			i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
674484b4fd4SRuslan Bukin 			    I2CCR_MSTA | I2CCR_TXAK);
675484b4fd4SRuslan Bukin 		else
676484b4fd4SRuslan Bukin 			i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
677484b4fd4SRuslan Bukin 			    I2CCR_MSTA);
678d2c05e20SIan Lepore                 /* Dummy read to prime the receiver. */
679484b4fd4SRuslan Bukin 		i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
680d2c05e20SIan Lepore 		i2c_read_reg(sc, I2C_DATA_REG);
681d2c05e20SIan Lepore 	}
682d2c05e20SIan Lepore 
683d2c05e20SIan Lepore 	error = 0;
684d2c05e20SIan Lepore 	*read = 0;
685d2c05e20SIan Lepore 	while (*read < len) {
686d2c05e20SIan Lepore 		if ((error = wait_for_xfer(sc, false)) != IIC_NOERR)
687d2c05e20SIan Lepore 			break;
688d2c05e20SIan Lepore 		i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
689d2c05e20SIan Lepore 		if (last) {
690d2c05e20SIan Lepore 			if (*read == len - 2) {
691484b4fd4SRuslan Bukin 				/* NO ACK on last byte */
692484b4fd4SRuslan Bukin 				i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
693484b4fd4SRuslan Bukin 				    I2CCR_MSTA | I2CCR_TXAK);
694d2c05e20SIan Lepore 			} else if (*read == len - 1) {
695d2c05e20SIan Lepore 				/* Transfer done, signal stop. */
696484b4fd4SRuslan Bukin 				i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
697484b4fd4SRuslan Bukin 				    I2CCR_TXAK);
698d2c05e20SIan Lepore 				wait_for_busbusy(sc, false);
699484b4fd4SRuslan Bukin 			}
700d2c05e20SIan Lepore 		}
701484b4fd4SRuslan Bukin 		reg = i2c_read_reg(sc, I2C_DATA_REG);
702900fb59eSIan Lepore 		DEBUGF(sc, 1, "0x%02x ", reg);
703484b4fd4SRuslan Bukin 		*buf++ = reg;
704484b4fd4SRuslan Bukin 		(*read)++;
705484b4fd4SRuslan Bukin 	}
706900fb59eSIan Lepore 	DEBUGF(sc, 1, "\n");
707484b4fd4SRuslan Bukin 
708d2c05e20SIan Lepore 	return (i2c_error_handler(sc, error));
709484b4fd4SRuslan Bukin }
710484b4fd4SRuslan Bukin 
711484b4fd4SRuslan Bukin static int
712484b4fd4SRuslan Bukin i2c_write(device_t dev, const char *buf, int len, int *sent, int timeout)
713484b4fd4SRuslan Bukin {
714484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
715484b4fd4SRuslan Bukin 	int error;
716484b4fd4SRuslan Bukin 
717484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
718484b4fd4SRuslan Bukin 
719d2c05e20SIan Lepore 	error = 0;
720d2c05e20SIan Lepore 	*sent = 0;
721900fb59eSIan Lepore 	DEVICE_DEBUGF(sc, 1, "write  0x%02x len %d: ", sc->slave, len);
722484b4fd4SRuslan Bukin 	while (*sent < len) {
723900fb59eSIan Lepore 		DEBUGF(sc, 1, "0x%02x ", *buf);
724484b4fd4SRuslan Bukin 		i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
725484b4fd4SRuslan Bukin 		i2c_write_reg(sc, I2C_DATA_REG, *buf++);
726d2c05e20SIan Lepore 		if ((error = wait_for_xfer(sc, true)) != IIC_NOERR)
727d2c05e20SIan Lepore 			break;
728484b4fd4SRuslan Bukin 		(*sent)++;
729484b4fd4SRuslan Bukin 	}
730900fb59eSIan Lepore 	DEBUGF(sc, 1, "\n");
731d2c05e20SIan Lepore 	return (i2c_error_handler(sc, error));
732484b4fd4SRuslan Bukin }
733