xref: /freebsd/sys/arm/freescale/imx/imx_i2c.c (revision 8928c2e4f598189529c2c9f27c1641808676fa73)
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>
51*8928c2e4SIan 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>
56484b4fd4SRuslan Bukin 
57484b4fd4SRuslan Bukin #include <machine/bus.h>
58484b4fd4SRuslan Bukin #include <machine/resource.h>
59484b4fd4SRuslan Bukin #include <sys/rman.h>
60484b4fd4SRuslan Bukin 
61844aff82SIan Lepore #include <arm/freescale/imx/imx_ccmvar.h>
62844aff82SIan Lepore 
63484b4fd4SRuslan Bukin #include <dev/iicbus/iiconf.h>
64484b4fd4SRuslan Bukin #include <dev/iicbus/iicbus.h>
65*8928c2e4SIan Lepore #include <dev/iicbus/iic_recover_bus.h>
66484b4fd4SRuslan Bukin #include "iicbus_if.h"
67484b4fd4SRuslan Bukin 
68484b4fd4SRuslan Bukin #include <dev/ofw/openfirm.h>
69484b4fd4SRuslan Bukin #include <dev/ofw/ofw_bus.h>
70484b4fd4SRuslan Bukin #include <dev/ofw/ofw_bus_subr.h>
71484b4fd4SRuslan Bukin 
72*8928c2e4SIan Lepore #include <dev/fdt/fdt_pinctrl.h>
73*8928c2e4SIan Lepore #include <dev/gpio/gpiobusvar.h>
74*8928c2e4SIan Lepore 
75484b4fd4SRuslan Bukin #define I2C_ADDR_REG		0x00 /* I2C slave address register */
76484b4fd4SRuslan Bukin #define I2C_FDR_REG		0x04 /* I2C frequency divider register */
77484b4fd4SRuslan Bukin #define I2C_CONTROL_REG		0x08 /* I2C control register */
78484b4fd4SRuslan Bukin #define I2C_STATUS_REG		0x0C /* I2C status register */
79484b4fd4SRuslan Bukin #define I2C_DATA_REG		0x10 /* I2C data register */
80484b4fd4SRuslan Bukin #define I2C_DFSRR_REG		0x14 /* I2C Digital Filter Sampling rate */
81484b4fd4SRuslan Bukin 
82484b4fd4SRuslan Bukin #define I2CCR_MEN		(1 << 7) /* Module enable */
83484b4fd4SRuslan Bukin #define I2CCR_MSTA		(1 << 5) /* Master/slave mode */
84484b4fd4SRuslan Bukin #define I2CCR_MTX		(1 << 4) /* Transmit/receive mode */
85484b4fd4SRuslan Bukin #define I2CCR_TXAK		(1 << 3) /* Transfer acknowledge */
86484b4fd4SRuslan Bukin #define I2CCR_RSTA		(1 << 2) /* Repeated START */
87484b4fd4SRuslan Bukin 
88484b4fd4SRuslan Bukin #define I2CSR_MCF		(1 << 7) /* Data transfer */
89484b4fd4SRuslan Bukin #define I2CSR_MASS		(1 << 6) /* Addressed as a slave */
90484b4fd4SRuslan Bukin #define I2CSR_MBB		(1 << 5) /* Bus busy */
91484b4fd4SRuslan Bukin #define I2CSR_MAL		(1 << 4) /* Arbitration lost */
92484b4fd4SRuslan Bukin #define I2CSR_SRW		(1 << 2) /* Slave read/write */
93484b4fd4SRuslan Bukin #define I2CSR_MIF		(1 << 1) /* Module interrupt */
94484b4fd4SRuslan Bukin #define I2CSR_RXAK		(1 << 0) /* Received acknowledge */
95484b4fd4SRuslan Bukin 
96484b4fd4SRuslan Bukin #define I2C_BAUD_RATE_FAST	0x31
97484b4fd4SRuslan Bukin #define I2C_BAUD_RATE_DEF	0x3F
98484b4fd4SRuslan Bukin #define I2C_DFSSR_DIV		0x10
99484b4fd4SRuslan Bukin 
100844aff82SIan Lepore /*
101844aff82SIan Lepore  * A table of available divisors and the associated coded values to put in the
102844aff82SIan Lepore  * FDR register to achieve that divisor.. There is no algorithmic relationship I
103844aff82SIan Lepore  * can see between divisors and the codes that go into the register.  The table
104844aff82SIan Lepore  * begins and ends with entries that handle insane configuration values.
105844aff82SIan Lepore  */
106844aff82SIan Lepore struct clkdiv {
107844aff82SIan Lepore 	u_int divisor;
108844aff82SIan Lepore 	u_int regcode;
109844aff82SIan Lepore };
110844aff82SIan Lepore static struct clkdiv clkdiv_table[] = {
111844aff82SIan Lepore         {    0, 0x20 }, {   22, 0x20 }, {   24, 0x21 }, {   26, 0x22 },
112844aff82SIan Lepore         {   28, 0x23 }, {   30, 0x00 }, {   32, 0x24 }, {   36, 0x25 },
113844aff82SIan Lepore         {   40, 0x26 }, {   42, 0x03 }, {   44, 0x27 }, {   48, 0x28 },
114844aff82SIan Lepore         {   52, 0x05 }, {   56, 0x29 }, {   60, 0x06 }, {   64, 0x2a },
115844aff82SIan Lepore         {   72, 0x2b }, {   80, 0x2c }, {   88, 0x09 }, {   96, 0x2d },
116844aff82SIan Lepore         {  104, 0x0a }, {  112, 0x2e }, {  128, 0x2f }, {  144, 0x0c },
117844aff82SIan Lepore         {  160, 0x30 }, {  192, 0x31 }, {  224, 0x32 }, {  240, 0x0f },
118844aff82SIan Lepore         {  256, 0x33 }, {  288, 0x10 }, {  320, 0x34 }, {  384, 0x35 },
119844aff82SIan Lepore         {  448, 0x36 }, {  480, 0x13 }, {  512, 0x37 }, {  576, 0x14 },
120844aff82SIan Lepore         {  640, 0x38 }, {  768, 0x39 }, {  896, 0x3a }, {  960, 0x17 },
121844aff82SIan Lepore         { 1024, 0x3b }, { 1152, 0x18 }, { 1280, 0x3c }, { 1536, 0x3d },
122844aff82SIan Lepore         { 1792, 0x3e }, { 1920, 0x1b }, { 2048, 0x3f }, { 2304, 0x1c },
123844aff82SIan Lepore         { 2560, 0x1d }, { 3072, 0x1e }, { 3840, 0x1f }, {UINT_MAX, 0x1f}
124844aff82SIan Lepore };
125844aff82SIan Lepore 
12640d7d632SRuslan Bukin static struct ofw_compat_data compat_data[] = {
12740d7d632SRuslan Bukin 	{"fsl,imx6q-i2c",  1},
12840d7d632SRuslan Bukin 	{"fsl,imx-i2c",	   1},
12940d7d632SRuslan Bukin 	{NULL,             0}
13040d7d632SRuslan Bukin };
13140d7d632SRuslan Bukin 
132484b4fd4SRuslan Bukin struct i2c_softc {
133484b4fd4SRuslan Bukin 	device_t		dev;
134484b4fd4SRuslan Bukin 	device_t		iicbus;
135484b4fd4SRuslan Bukin 	struct resource		*res;
136484b4fd4SRuslan Bukin 	int			rid;
137d2c05e20SIan Lepore 	sbintime_t		byte_time_sbt;
138*8928c2e4SIan Lepore 	int			rb_pinctl_idx;
139*8928c2e4SIan Lepore 	gpio_pin_t		rb_sclpin;
140*8928c2e4SIan Lepore 	gpio_pin_t 		rb_sdapin;
141484b4fd4SRuslan Bukin };
142484b4fd4SRuslan Bukin 
143484b4fd4SRuslan Bukin static phandle_t i2c_get_node(device_t, device_t);
144484b4fd4SRuslan Bukin static int i2c_probe(device_t);
145484b4fd4SRuslan Bukin static int i2c_attach(device_t);
146484b4fd4SRuslan Bukin 
147484b4fd4SRuslan Bukin static int i2c_repeated_start(device_t, u_char, int);
148484b4fd4SRuslan Bukin static int i2c_start(device_t, u_char, int);
149484b4fd4SRuslan Bukin static int i2c_stop(device_t);
150484b4fd4SRuslan Bukin static int i2c_reset(device_t, u_char, u_char, u_char *);
151484b4fd4SRuslan Bukin static int i2c_read(device_t, char *, int, int *, int, int);
152484b4fd4SRuslan Bukin static int i2c_write(device_t, const char *, int, int *, int);
153484b4fd4SRuslan Bukin 
154484b4fd4SRuslan Bukin static device_method_t i2c_methods[] = {
155484b4fd4SRuslan Bukin 	DEVMETHOD(device_probe,			i2c_probe),
156484b4fd4SRuslan Bukin 	DEVMETHOD(device_attach,		i2c_attach),
157484b4fd4SRuslan Bukin 
158484b4fd4SRuslan Bukin 	/* OFW methods */
159484b4fd4SRuslan Bukin 	DEVMETHOD(ofw_bus_get_node,		i2c_get_node),
160484b4fd4SRuslan Bukin 
161484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_callback,		iicbus_null_callback),
162484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_repeated_start,	i2c_repeated_start),
163484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_start,			i2c_start),
164484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_stop,			i2c_stop),
165484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_reset,			i2c_reset),
166484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_read,			i2c_read),
167484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_write,			i2c_write),
168484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_transfer,		iicbus_transfer_gen),
169484b4fd4SRuslan Bukin 
170d2c05e20SIan Lepore 	DEVMETHOD_END
171484b4fd4SRuslan Bukin };
172484b4fd4SRuslan Bukin 
173484b4fd4SRuslan Bukin static driver_t i2c_driver = {
174484b4fd4SRuslan Bukin 	"iichb",
175484b4fd4SRuslan Bukin 	i2c_methods,
176484b4fd4SRuslan Bukin 	sizeof(struct i2c_softc),
177484b4fd4SRuslan Bukin };
178484b4fd4SRuslan Bukin static devclass_t  i2c_devclass;
179484b4fd4SRuslan Bukin 
180484b4fd4SRuslan Bukin DRIVER_MODULE(i2c, simplebus, i2c_driver, i2c_devclass, 0, 0);
181484b4fd4SRuslan Bukin DRIVER_MODULE(iicbus, i2c, iicbus_driver, iicbus_devclass, 0, 0);
182484b4fd4SRuslan Bukin 
183484b4fd4SRuslan Bukin static phandle_t
184484b4fd4SRuslan Bukin i2c_get_node(device_t bus, device_t dev)
185484b4fd4SRuslan Bukin {
186484b4fd4SRuslan Bukin 	/*
187484b4fd4SRuslan Bukin 	 * Share controller node with iicbus device
188484b4fd4SRuslan Bukin 	 */
189484b4fd4SRuslan Bukin 	return ofw_bus_get_node(bus);
190484b4fd4SRuslan Bukin }
191484b4fd4SRuslan Bukin 
192484b4fd4SRuslan Bukin static __inline void
193484b4fd4SRuslan Bukin i2c_write_reg(struct i2c_softc *sc, bus_size_t off, uint8_t val)
194484b4fd4SRuslan Bukin {
195484b4fd4SRuslan Bukin 
196d2c05e20SIan Lepore 	bus_write_1(sc->res, off, val);
197484b4fd4SRuslan Bukin }
198484b4fd4SRuslan Bukin 
199484b4fd4SRuslan Bukin static __inline uint8_t
200484b4fd4SRuslan Bukin i2c_read_reg(struct i2c_softc *sc, bus_size_t off)
201484b4fd4SRuslan Bukin {
202484b4fd4SRuslan Bukin 
203d2c05e20SIan Lepore 	return (bus_read_1(sc->res, off));
204484b4fd4SRuslan Bukin }
205484b4fd4SRuslan Bukin 
206484b4fd4SRuslan Bukin static __inline void
207484b4fd4SRuslan Bukin i2c_flag_set(struct i2c_softc *sc, bus_size_t off, uint8_t mask)
208484b4fd4SRuslan Bukin {
209484b4fd4SRuslan Bukin 	uint8_t status;
210484b4fd4SRuslan Bukin 
211484b4fd4SRuslan Bukin 	status = i2c_read_reg(sc, off);
212484b4fd4SRuslan Bukin 	status |= mask;
213484b4fd4SRuslan Bukin 	i2c_write_reg(sc, off, status);
214484b4fd4SRuslan Bukin }
215484b4fd4SRuslan Bukin 
216d2c05e20SIan Lepore /* Wait for bus to become busy or not-busy. */
217484b4fd4SRuslan Bukin static int
218d2c05e20SIan Lepore wait_for_busbusy(struct i2c_softc *sc, int wantbusy)
219484b4fd4SRuslan Bukin {
220d2c05e20SIan Lepore 	int retry, srb;
221484b4fd4SRuslan Bukin 
222484b4fd4SRuslan Bukin 	retry = 1000;
223484b4fd4SRuslan Bukin 	while (retry --) {
224d2c05e20SIan Lepore 		srb = i2c_read_reg(sc, I2C_STATUS_REG) & I2CSR_MBB;
225d2c05e20SIan Lepore 		if ((srb && wantbusy) || (!srb && !wantbusy))
226484b4fd4SRuslan Bukin 			return (IIC_NOERR);
227d2c05e20SIan Lepore 		DELAY(1);
228484b4fd4SRuslan Bukin 	}
229484b4fd4SRuslan Bukin 	return (IIC_ETIMEOUT);
230484b4fd4SRuslan Bukin }
231484b4fd4SRuslan Bukin 
232d2c05e20SIan Lepore /* Wait for transfer to complete, optionally check RXAK. */
233484b4fd4SRuslan Bukin static int
234d2c05e20SIan Lepore wait_for_xfer(struct i2c_softc *sc, int checkack)
235484b4fd4SRuslan Bukin {
236d2c05e20SIan Lepore 	int retry, sr;
237484b4fd4SRuslan Bukin 
238d2c05e20SIan Lepore 	/*
239d2c05e20SIan Lepore 	 * Sleep for about the time it takes to transfer a byte (with precision
240d2c05e20SIan Lepore 	 * set to tolerate 5% oversleep).  We calculate the approximate byte
241d2c05e20SIan Lepore 	 * transfer time when we set the bus speed divisor.  Slaves are allowed
242d2c05e20SIan Lepore 	 * to do clock-stretching so the actual transfer time can be larger, but
243d2c05e20SIan Lepore 	 * this gets the bulk of the waiting out of the way without tying up the
244d2c05e20SIan Lepore 	 * processor the whole time.
245d2c05e20SIan Lepore 	 */
246d2c05e20SIan Lepore 	pause_sbt("imxi2c", sc->byte_time_sbt, sc->byte_time_sbt / 20, 0);
247d2c05e20SIan Lepore 
248d2c05e20SIan Lepore 	retry = 10000;
249484b4fd4SRuslan Bukin 	while (retry --) {
250d2c05e20SIan Lepore 		sr = i2c_read_reg(sc, I2C_STATUS_REG);
251d2c05e20SIan Lepore 		if (sr & I2CSR_MIF) {
252d2c05e20SIan Lepore                         if (sr & I2CSR_MAL)
253d1e99670SIan Lepore 				return (IIC_EBUSERR);
254d2c05e20SIan Lepore 			else if (checkack && (sr & I2CSR_RXAK))
255d2c05e20SIan Lepore 				return (IIC_ENOACK);
256d2c05e20SIan Lepore 			else
257484b4fd4SRuslan Bukin 				return (IIC_NOERR);
258484b4fd4SRuslan Bukin 		}
259d2c05e20SIan Lepore 		DELAY(1);
260d2c05e20SIan Lepore 	}
261484b4fd4SRuslan Bukin 	return (IIC_ETIMEOUT);
262484b4fd4SRuslan Bukin }
263484b4fd4SRuslan Bukin 
264d2c05e20SIan Lepore /*
265d2c05e20SIan Lepore  * Implement the error handling shown in the state diagram of the imx6 reference
266d2c05e20SIan Lepore  * manual.  If there was an error, then:
267d2c05e20SIan Lepore  *  - Clear master mode (MSTA and MTX).
268d2c05e20SIan Lepore  *  - Wait for the bus to become free or for a timeout to happen.
269d2c05e20SIan Lepore  *  - Disable the controller.
270d2c05e20SIan Lepore  */
271484b4fd4SRuslan Bukin static int
272d2c05e20SIan Lepore i2c_error_handler(struct i2c_softc *sc, int error)
273484b4fd4SRuslan Bukin {
274484b4fd4SRuslan Bukin 
275d2c05e20SIan Lepore 	if (error != 0) {
276d2c05e20SIan Lepore 		i2c_write_reg(sc, I2C_STATUS_REG, 0);
277d2c05e20SIan Lepore 		i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN);
278d2c05e20SIan Lepore 		wait_for_busbusy(sc, false);
279d2c05e20SIan Lepore 		i2c_write_reg(sc, I2C_CONTROL_REG, 0);
280484b4fd4SRuslan Bukin 	}
281d2c05e20SIan Lepore 	return (error);
282484b4fd4SRuslan Bukin }
283484b4fd4SRuslan Bukin 
284484b4fd4SRuslan Bukin static int
285*8928c2e4SIan Lepore i2c_recover_getsda(void *ctx)
286*8928c2e4SIan Lepore {
287*8928c2e4SIan Lepore 	bool active;
288*8928c2e4SIan Lepore 
289*8928c2e4SIan Lepore 	gpio_pin_is_active(((struct i2c_softc *)ctx)->rb_sdapin, &active);
290*8928c2e4SIan Lepore 	return (active);
291*8928c2e4SIan Lepore }
292*8928c2e4SIan Lepore 
293*8928c2e4SIan Lepore static void
294*8928c2e4SIan Lepore i2c_recover_setsda(void *ctx, int value)
295*8928c2e4SIan Lepore {
296*8928c2e4SIan Lepore 
297*8928c2e4SIan Lepore 	gpio_pin_set_active(((struct i2c_softc *)ctx)->rb_sdapin, value);
298*8928c2e4SIan Lepore }
299*8928c2e4SIan Lepore 
300*8928c2e4SIan Lepore static int
301*8928c2e4SIan Lepore i2c_recover_getscl(void *ctx)
302*8928c2e4SIan Lepore {
303*8928c2e4SIan Lepore 	bool active;
304*8928c2e4SIan Lepore 
305*8928c2e4SIan Lepore 	gpio_pin_is_active(((struct i2c_softc *)ctx)->rb_sclpin, &active);
306*8928c2e4SIan Lepore 	return (active);
307*8928c2e4SIan Lepore 
308*8928c2e4SIan Lepore }
309*8928c2e4SIan Lepore 
310*8928c2e4SIan Lepore static void
311*8928c2e4SIan Lepore i2c_recover_setscl(void *ctx, int value)
312*8928c2e4SIan Lepore {
313*8928c2e4SIan Lepore 
314*8928c2e4SIan Lepore 	gpio_pin_set_active(((struct i2c_softc *)ctx)->rb_sclpin, value);
315*8928c2e4SIan Lepore }
316*8928c2e4SIan Lepore 
317*8928c2e4SIan Lepore static int
318*8928c2e4SIan Lepore i2c_recover_bus(struct i2c_softc *sc)
319*8928c2e4SIan Lepore {
320*8928c2e4SIan Lepore 	struct iicrb_pin_access pins;
321*8928c2e4SIan Lepore 	int err;
322*8928c2e4SIan Lepore 
323*8928c2e4SIan Lepore 	/*
324*8928c2e4SIan Lepore 	 * If we have gpio pinmux config, reconfigure the pins to gpio mode,
325*8928c2e4SIan Lepore 	 * invoke iic_recover_bus which checks for a hung bus and bitbangs a
326*8928c2e4SIan Lepore 	 * recovery sequence if necessary, then configure the pins back to i2c
327*8928c2e4SIan Lepore 	 * mode (idx 0).
328*8928c2e4SIan Lepore 	 */
329*8928c2e4SIan Lepore 	if (sc->rb_pinctl_idx == 0)
330*8928c2e4SIan Lepore 		return (0);
331*8928c2e4SIan Lepore 
332*8928c2e4SIan Lepore 	fdt_pinctrl_configure(sc->dev, sc->rb_pinctl_idx);
333*8928c2e4SIan Lepore 
334*8928c2e4SIan Lepore 	pins.ctx = sc;
335*8928c2e4SIan Lepore 	pins.getsda = i2c_recover_getsda;
336*8928c2e4SIan Lepore 	pins.setsda = i2c_recover_setsda;
337*8928c2e4SIan Lepore 	pins.getscl = i2c_recover_getscl;
338*8928c2e4SIan Lepore 	pins.setscl = i2c_recover_setscl;
339*8928c2e4SIan Lepore 	err = iic_recover_bus(&pins);
340*8928c2e4SIan Lepore 
341*8928c2e4SIan Lepore 	fdt_pinctrl_configure(sc->dev, 0);
342*8928c2e4SIan Lepore 
343*8928c2e4SIan Lepore 	return (err);
344*8928c2e4SIan Lepore }
345*8928c2e4SIan Lepore 
346*8928c2e4SIan Lepore static int
347484b4fd4SRuslan Bukin i2c_probe(device_t dev)
348484b4fd4SRuslan Bukin {
349484b4fd4SRuslan Bukin 
350484b4fd4SRuslan Bukin 	if (!ofw_bus_status_okay(dev))
351484b4fd4SRuslan Bukin 		return (ENXIO);
352484b4fd4SRuslan Bukin 
35340d7d632SRuslan Bukin 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
354484b4fd4SRuslan Bukin 		return (ENXIO);
355484b4fd4SRuslan Bukin 
356d2c05e20SIan Lepore 	device_set_desc(dev, "Freescale i.MX I2C");
357484b4fd4SRuslan Bukin 
358484b4fd4SRuslan Bukin 	return (BUS_PROBE_DEFAULT);
359484b4fd4SRuslan Bukin }
360484b4fd4SRuslan Bukin 
361484b4fd4SRuslan Bukin static int
362484b4fd4SRuslan Bukin i2c_attach(device_t dev)
363484b4fd4SRuslan Bukin {
364*8928c2e4SIan Lepore 	char wrkstr[16];
365484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
366*8928c2e4SIan Lepore 	phandle_t node;
367*8928c2e4SIan Lepore 	int err, cfgidx;
368484b4fd4SRuslan Bukin 
369484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
370484b4fd4SRuslan Bukin 	sc->dev = dev;
371484b4fd4SRuslan Bukin 	sc->rid = 0;
372484b4fd4SRuslan Bukin 
373484b4fd4SRuslan Bukin 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
374484b4fd4SRuslan Bukin 	    RF_ACTIVE);
375484b4fd4SRuslan Bukin 	if (sc->res == NULL) {
376484b4fd4SRuslan Bukin 		device_printf(dev, "could not allocate resources");
377484b4fd4SRuslan Bukin 		return (ENXIO);
378484b4fd4SRuslan Bukin 	}
379484b4fd4SRuslan Bukin 
380484b4fd4SRuslan Bukin 	sc->iicbus = device_add_child(dev, "iicbus", -1);
381484b4fd4SRuslan Bukin 	if (sc->iicbus == NULL) {
382484b4fd4SRuslan Bukin 		device_printf(dev, "could not add iicbus child");
383484b4fd4SRuslan Bukin 		return (ENXIO);
384484b4fd4SRuslan Bukin 	}
385484b4fd4SRuslan Bukin 
386*8928c2e4SIan Lepore 	/*
387*8928c2e4SIan Lepore 	 * Set up for bus recovery using gpio pins, if the pinctrl and gpio
388*8928c2e4SIan Lepore 	 * properties are present.  This is optional.  If all the config data is
389*8928c2e4SIan Lepore 	 * not in place, we just don't do gpio bitbang bus recovery.
390*8928c2e4SIan Lepore 	 */
391*8928c2e4SIan Lepore 	node = ofw_bus_get_node(sc->dev);
392*8928c2e4SIan Lepore 
393*8928c2e4SIan Lepore 	err = gpio_pin_get_by_ofw_property(dev, node, "scl-gpios",
394*8928c2e4SIan Lepore 	    &sc->rb_sclpin);
395*8928c2e4SIan Lepore 	if (err != 0)
396*8928c2e4SIan Lepore 		goto no_recovery;
397*8928c2e4SIan Lepore 	err = gpio_pin_get_by_ofw_property(dev, node, "sda-gpios",
398*8928c2e4SIan Lepore 	    &sc->rb_sdapin);
399*8928c2e4SIan Lepore 	if (err != 0)
400*8928c2e4SIan Lepore 		goto no_recovery;
401*8928c2e4SIan Lepore 
402*8928c2e4SIan Lepore 	/*
403*8928c2e4SIan Lepore 	 * Preset the gpio pins to output high (idle bus state).  The signal
404*8928c2e4SIan Lepore 	 * won't actually appear on the pins until the bus recovery code changes
405*8928c2e4SIan Lepore 	 * the pinmux config from i2c to gpio.
406*8928c2e4SIan Lepore 	 */
407*8928c2e4SIan Lepore 	gpio_pin_setflags(sc->rb_sclpin, GPIO_PIN_OUTPUT);
408*8928c2e4SIan Lepore 	gpio_pin_setflags(sc->rb_sdapin, GPIO_PIN_OUTPUT);
409*8928c2e4SIan Lepore 	gpio_pin_set_active(sc->rb_sclpin, true);
410*8928c2e4SIan Lepore 	gpio_pin_set_active(sc->rb_sdapin, true);
411*8928c2e4SIan Lepore 
412*8928c2e4SIan Lepore 	/*
413*8928c2e4SIan Lepore 	 * Obtain the index of pinctrl node for bus recovery using gpio pins,
414*8928c2e4SIan Lepore 	 * then confirm that pinctrl properties exist for that index and for the
415*8928c2e4SIan Lepore 	 * default pinctrl-0.  If sc->rb_pinctl_idx is non-zero, the reset code
416*8928c2e4SIan Lepore 	 * will also do a bus recovery, so setting this value must be last.
417*8928c2e4SIan Lepore 	 */
418*8928c2e4SIan Lepore 	err = ofw_bus_find_string_index(node, "pinctrl-names", "gpio", &cfgidx);
419*8928c2e4SIan Lepore 	if (err == 0) {
420*8928c2e4SIan Lepore 		snprintf(wrkstr, sizeof(wrkstr), "pinctrl-%d", cfgidx);
421*8928c2e4SIan Lepore 		if (OF_hasprop(node, "pinctrl-0") && OF_hasprop(node, wrkstr))
422*8928c2e4SIan Lepore 			sc->rb_pinctl_idx = cfgidx;
423*8928c2e4SIan Lepore 	}
424*8928c2e4SIan Lepore 
425*8928c2e4SIan Lepore no_recovery:
426*8928c2e4SIan Lepore 
427*8928c2e4SIan Lepore 	/* We don't do a hardware reset here because iicbus_attach() does it. */
428*8928c2e4SIan Lepore 
429484b4fd4SRuslan Bukin 	bus_generic_attach(dev);
430d2c05e20SIan Lepore 	return (0);
431484b4fd4SRuslan Bukin }
432484b4fd4SRuslan Bukin 
433484b4fd4SRuslan Bukin static int
434484b4fd4SRuslan Bukin i2c_repeated_start(device_t dev, u_char slave, int timeout)
435484b4fd4SRuslan Bukin {
436484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
437484b4fd4SRuslan Bukin 	int error;
438484b4fd4SRuslan Bukin 
439484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
440484b4fd4SRuslan Bukin 
441484b4fd4SRuslan Bukin 	if ((i2c_read_reg(sc, I2C_STATUS_REG) & I2CSR_MBB) == 0) {
442d2c05e20SIan Lepore 		return (IIC_EBUSERR);
443484b4fd4SRuslan Bukin 	}
444484b4fd4SRuslan Bukin 
445d2c05e20SIan Lepore 	/*
446d2c05e20SIan Lepore 	 * Set repeated start condition, delay (per reference manual, min 156nS)
447d2c05e20SIan Lepore 	 * before writing slave address, wait for ack after write.
448d2c05e20SIan Lepore 	 */
449484b4fd4SRuslan Bukin 	i2c_flag_set(sc, I2C_CONTROL_REG, I2CCR_RSTA);
450d2c05e20SIan Lepore 	DELAY(1);
451484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
452484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_DATA_REG, slave);
453d2c05e20SIan Lepore 	error = wait_for_xfer(sc, true);
454d2c05e20SIan Lepore 	return (i2c_error_handler(sc, error));
455484b4fd4SRuslan Bukin }
456484b4fd4SRuslan Bukin 
457484b4fd4SRuslan Bukin static int
458*8928c2e4SIan Lepore i2c_start_ll(device_t dev, u_char slave, int timeout)
459484b4fd4SRuslan Bukin {
460484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
461484b4fd4SRuslan Bukin 	int error;
462484b4fd4SRuslan Bukin 
463484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
464484b4fd4SRuslan Bukin 
465d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN);
466d2c05e20SIan Lepore 	DELAY(10); /* Delay for controller to sample bus state. */
467484b4fd4SRuslan Bukin 	if (i2c_read_reg(sc, I2C_STATUS_REG) & I2CSR_MBB) {
468d1e99670SIan Lepore 		return (i2c_error_handler(sc, IIC_EBUSERR));
469484b4fd4SRuslan Bukin 	}
470d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN | I2CCR_MSTA | I2CCR_MTX);
471d2c05e20SIan Lepore 	if ((error = wait_for_busbusy(sc, true)) != IIC_NOERR)
472d2c05e20SIan Lepore 		return (i2c_error_handler(sc, error));
473d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_STATUS_REG, 0);
474484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_DATA_REG, slave);
475d2c05e20SIan Lepore 	error = wait_for_xfer(sc, true);
476d2c05e20SIan Lepore 	return (i2c_error_handler(sc, error));
477484b4fd4SRuslan Bukin }
478484b4fd4SRuslan Bukin 
479484b4fd4SRuslan Bukin static int
480*8928c2e4SIan Lepore i2c_start(device_t dev, u_char slave, int timeout)
481*8928c2e4SIan Lepore {
482*8928c2e4SIan Lepore 	struct i2c_softc *sc;
483*8928c2e4SIan Lepore 	int error;
484*8928c2e4SIan Lepore 
485*8928c2e4SIan Lepore 	sc = device_get_softc(dev);
486*8928c2e4SIan Lepore 
487*8928c2e4SIan Lepore 	/*
488*8928c2e4SIan Lepore 	 * Invoke the low-level code to put the bus into master mode and address
489*8928c2e4SIan Lepore 	 * the given slave.  If that fails, idle the controller and attempt a
490*8928c2e4SIan Lepore 	 * bus recovery, and then try again one time.  Signaling a start and
491*8928c2e4SIan Lepore 	 * addressing the slave is the only operation that a low-level driver
492*8928c2e4SIan Lepore 	 * can safely retry without any help from the upper layers that know
493*8928c2e4SIan Lepore 	 * more about the slave device.
494*8928c2e4SIan Lepore 	 */
495*8928c2e4SIan Lepore 	if ((error = i2c_start_ll(dev, slave, timeout)) != 0) {
496*8928c2e4SIan Lepore 		i2c_write_reg(sc, I2C_CONTROL_REG, 0x0);
497*8928c2e4SIan Lepore 		if ((error = i2c_recover_bus(sc)) != 0)
498*8928c2e4SIan Lepore 			return (error);
499*8928c2e4SIan Lepore 		error = i2c_start_ll(dev, slave, timeout);
500*8928c2e4SIan Lepore 	}
501*8928c2e4SIan Lepore 	return (error);
502*8928c2e4SIan Lepore }
503*8928c2e4SIan Lepore 
504*8928c2e4SIan Lepore static int
505484b4fd4SRuslan Bukin i2c_stop(device_t dev)
506484b4fd4SRuslan Bukin {
507484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
508484b4fd4SRuslan Bukin 
509484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
510d2c05e20SIan Lepore 
511d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN);
512d2c05e20SIan Lepore 	wait_for_busbusy(sc, false);
513484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_CONTROL_REG, 0);
514484b4fd4SRuslan Bukin 	return (IIC_NOERR);
515484b4fd4SRuslan Bukin }
516484b4fd4SRuslan Bukin 
517484b4fd4SRuslan Bukin static int
518484b4fd4SRuslan Bukin i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldadr)
519484b4fd4SRuslan Bukin {
520484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
521844aff82SIan Lepore 	u_int busfreq, div, i, ipgfreq;
522484b4fd4SRuslan Bukin 
523484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
524484b4fd4SRuslan Bukin 
525844aff82SIan Lepore 	/*
526844aff82SIan Lepore 	 * Look up the divisor that gives the nearest speed that doesn't exceed
527844aff82SIan Lepore 	 * the configured value for the bus.
528844aff82SIan Lepore 	 */
529844aff82SIan Lepore 	ipgfreq = imx_ccm_ipg_hz();
530844aff82SIan Lepore 	busfreq = IICBUS_GET_FREQUENCY(sc->iicbus, speed);
531f0e56111SPedro F. Giffuni 	div = howmany(ipgfreq, busfreq);
532844aff82SIan Lepore 	for (i = 0; i < nitems(clkdiv_table); i++) {
533844aff82SIan Lepore 		if (clkdiv_table[i].divisor >= div)
534484b4fd4SRuslan Bukin 			break;
535484b4fd4SRuslan Bukin 	}
536484b4fd4SRuslan Bukin 
537d2c05e20SIan Lepore 	/*
538d2c05e20SIan Lepore 	 * Calculate roughly how long it will take to transfer a byte (which
539d2c05e20SIan Lepore 	 * requires 9 clock cycles) at the new bus speed.  This value is used to
540d2c05e20SIan Lepore 	 * pause() while waiting for transfer-complete.  With a 66MHz IPG clock
541d2c05e20SIan Lepore 	 * and the actual i2c bus speeds that leads to, for nominal 100KHz and
542d2c05e20SIan Lepore 	 * 400KHz bus speeds the transfer times are roughly 104uS and 22uS.
543d2c05e20SIan Lepore 	 */
544d2c05e20SIan Lepore 	busfreq = ipgfreq / clkdiv_table[i].divisor;
545d2c05e20SIan Lepore 	sc->byte_time_sbt = SBT_1US * (9000000 / busfreq);
546d2c05e20SIan Lepore 
547d2c05e20SIan Lepore 	/*
548d2c05e20SIan Lepore 	 * Disable the controller (do the reset), and set the new clock divisor.
549d2c05e20SIan Lepore 	 */
550d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
551484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_CONTROL_REG, 0x0);
552d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_FDR_REG, (uint8_t)clkdiv_table[i].regcode);
553*8928c2e4SIan Lepore 
554*8928c2e4SIan Lepore 	/*
555*8928c2e4SIan Lepore 	 * Now that the controller is idle, perform bus recovery.  If the bus
556*8928c2e4SIan Lepore 	 * isn't hung, this a fairly fast no-op.
557*8928c2e4SIan Lepore 	 */
558*8928c2e4SIan Lepore 	return (i2c_recover_bus(sc));
559484b4fd4SRuslan Bukin }
560484b4fd4SRuslan Bukin 
561484b4fd4SRuslan Bukin static int
562484b4fd4SRuslan Bukin i2c_read(device_t dev, char *buf, int len, int *read, int last, int delay)
563484b4fd4SRuslan Bukin {
564484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
565484b4fd4SRuslan Bukin 	int error, reg;
566484b4fd4SRuslan Bukin 
567484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
568484b4fd4SRuslan Bukin 	*read = 0;
569484b4fd4SRuslan Bukin 
570484b4fd4SRuslan Bukin 	if (len) {
571484b4fd4SRuslan Bukin 		if (len == 1)
572484b4fd4SRuslan Bukin 			i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
573484b4fd4SRuslan Bukin 			    I2CCR_MSTA | I2CCR_TXAK);
574484b4fd4SRuslan Bukin 		else
575484b4fd4SRuslan Bukin 			i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
576484b4fd4SRuslan Bukin 			    I2CCR_MSTA);
577d2c05e20SIan Lepore                 /* Dummy read to prime the receiver. */
578484b4fd4SRuslan Bukin 		i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
579d2c05e20SIan Lepore 		i2c_read_reg(sc, I2C_DATA_REG);
580d2c05e20SIan Lepore 	}
581d2c05e20SIan Lepore 
582d2c05e20SIan Lepore 	error = 0;
583d2c05e20SIan Lepore 	*read = 0;
584d2c05e20SIan Lepore 	while (*read < len) {
585d2c05e20SIan Lepore 		if ((error = wait_for_xfer(sc, false)) != IIC_NOERR)
586d2c05e20SIan Lepore 			break;
587d2c05e20SIan Lepore 		i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
588d2c05e20SIan Lepore 		if (last) {
589d2c05e20SIan Lepore 			if (*read == len - 2) {
590484b4fd4SRuslan Bukin 				/* NO ACK on last byte */
591484b4fd4SRuslan Bukin 				i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
592484b4fd4SRuslan Bukin 				    I2CCR_MSTA | I2CCR_TXAK);
593d2c05e20SIan Lepore 			} else if (*read == len - 1) {
594d2c05e20SIan Lepore 				/* Transfer done, signal stop. */
595484b4fd4SRuslan Bukin 				i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
596484b4fd4SRuslan Bukin 				    I2CCR_TXAK);
597d2c05e20SIan Lepore 				wait_for_busbusy(sc, false);
598484b4fd4SRuslan Bukin 			}
599d2c05e20SIan Lepore 		}
600484b4fd4SRuslan Bukin 		reg = i2c_read_reg(sc, I2C_DATA_REG);
601484b4fd4SRuslan Bukin 		*buf++ = reg;
602484b4fd4SRuslan Bukin 		(*read)++;
603484b4fd4SRuslan Bukin 	}
604484b4fd4SRuslan Bukin 
605d2c05e20SIan Lepore 	return (i2c_error_handler(sc, error));
606484b4fd4SRuslan Bukin }
607484b4fd4SRuslan Bukin 
608484b4fd4SRuslan Bukin static int
609484b4fd4SRuslan Bukin i2c_write(device_t dev, const char *buf, int len, int *sent, int timeout)
610484b4fd4SRuslan Bukin {
611484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
612484b4fd4SRuslan Bukin 	int error;
613484b4fd4SRuslan Bukin 
614484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
615484b4fd4SRuslan Bukin 
616d2c05e20SIan Lepore 	error = 0;
617d2c05e20SIan Lepore 	*sent = 0;
618484b4fd4SRuslan Bukin 	while (*sent < len) {
619484b4fd4SRuslan Bukin 		i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
620484b4fd4SRuslan Bukin 		i2c_write_reg(sc, I2C_DATA_REG, *buf++);
621d2c05e20SIan Lepore 		if ((error = wait_for_xfer(sc, true)) != IIC_NOERR)
622d2c05e20SIan Lepore 			break;
623484b4fd4SRuslan Bukin 		(*sent)++;
624484b4fd4SRuslan Bukin 	}
625484b4fd4SRuslan Bukin 
626d2c05e20SIan Lepore 	return (i2c_error_handler(sc, error));
627484b4fd4SRuslan Bukin }
628