xref: /freebsd/sys/arm/freescale/imx/imx_i2c.c (revision db4fcadf52c891afffded7fc72b9d9298bc39e77)
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  *
38*db4fcadfSConrad 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>
51484b4fd4SRuslan Bukin #include <sys/kernel.h>
52844aff82SIan Lepore #include <sys/limits.h>
53484b4fd4SRuslan Bukin #include <sys/module.h>
54484b4fd4SRuslan Bukin #include <sys/resource.h>
55484b4fd4SRuslan Bukin 
56484b4fd4SRuslan Bukin #include <machine/bus.h>
57484b4fd4SRuslan Bukin #include <machine/resource.h>
58484b4fd4SRuslan Bukin #include <sys/rman.h>
59484b4fd4SRuslan Bukin 
60844aff82SIan Lepore #include <arm/freescale/imx/imx_ccmvar.h>
61844aff82SIan Lepore 
62484b4fd4SRuslan Bukin #include <dev/iicbus/iiconf.h>
63484b4fd4SRuslan Bukin #include <dev/iicbus/iicbus.h>
64484b4fd4SRuslan Bukin #include "iicbus_if.h"
65484b4fd4SRuslan Bukin 
66484b4fd4SRuslan Bukin #include <dev/ofw/openfirm.h>
67484b4fd4SRuslan Bukin #include <dev/ofw/ofw_bus.h>
68484b4fd4SRuslan Bukin #include <dev/ofw/ofw_bus_subr.h>
69484b4fd4SRuslan Bukin 
70484b4fd4SRuslan Bukin #define I2C_ADDR_REG		0x00 /* I2C slave address register */
71484b4fd4SRuslan Bukin #define I2C_FDR_REG		0x04 /* I2C frequency divider register */
72484b4fd4SRuslan Bukin #define I2C_CONTROL_REG		0x08 /* I2C control register */
73484b4fd4SRuslan Bukin #define I2C_STATUS_REG		0x0C /* I2C status register */
74484b4fd4SRuslan Bukin #define I2C_DATA_REG		0x10 /* I2C data register */
75484b4fd4SRuslan Bukin #define I2C_DFSRR_REG		0x14 /* I2C Digital Filter Sampling rate */
76484b4fd4SRuslan Bukin 
77484b4fd4SRuslan Bukin #define I2CCR_MEN		(1 << 7) /* Module enable */
78484b4fd4SRuslan Bukin #define I2CCR_MSTA		(1 << 5) /* Master/slave mode */
79484b4fd4SRuslan Bukin #define I2CCR_MTX		(1 << 4) /* Transmit/receive mode */
80484b4fd4SRuslan Bukin #define I2CCR_TXAK		(1 << 3) /* Transfer acknowledge */
81484b4fd4SRuslan Bukin #define I2CCR_RSTA		(1 << 2) /* Repeated START */
82484b4fd4SRuslan Bukin 
83484b4fd4SRuslan Bukin #define I2CSR_MCF		(1 << 7) /* Data transfer */
84484b4fd4SRuslan Bukin #define I2CSR_MASS		(1 << 6) /* Addressed as a slave */
85484b4fd4SRuslan Bukin #define I2CSR_MBB		(1 << 5) /* Bus busy */
86484b4fd4SRuslan Bukin #define I2CSR_MAL		(1 << 4) /* Arbitration lost */
87484b4fd4SRuslan Bukin #define I2CSR_SRW		(1 << 2) /* Slave read/write */
88484b4fd4SRuslan Bukin #define I2CSR_MIF		(1 << 1) /* Module interrupt */
89484b4fd4SRuslan Bukin #define I2CSR_RXAK		(1 << 0) /* Received acknowledge */
90484b4fd4SRuslan Bukin 
91484b4fd4SRuslan Bukin #define I2C_BAUD_RATE_FAST	0x31
92484b4fd4SRuslan Bukin #define I2C_BAUD_RATE_DEF	0x3F
93484b4fd4SRuslan Bukin #define I2C_DFSSR_DIV		0x10
94484b4fd4SRuslan Bukin 
95844aff82SIan Lepore /*
96844aff82SIan Lepore  * A table of available divisors and the associated coded values to put in the
97844aff82SIan Lepore  * FDR register to achieve that divisor.. There is no algorithmic relationship I
98844aff82SIan Lepore  * can see between divisors and the codes that go into the register.  The table
99844aff82SIan Lepore  * begins and ends with entries that handle insane configuration values.
100844aff82SIan Lepore  */
101844aff82SIan Lepore struct clkdiv {
102844aff82SIan Lepore 	u_int divisor;
103844aff82SIan Lepore 	u_int regcode;
104844aff82SIan Lepore };
105844aff82SIan Lepore static struct clkdiv clkdiv_table[] = {
106844aff82SIan Lepore         {    0, 0x20 }, {   22, 0x20 }, {   24, 0x21 }, {   26, 0x22 },
107844aff82SIan Lepore         {   28, 0x23 }, {   30, 0x00 }, {   32, 0x24 }, {   36, 0x25 },
108844aff82SIan Lepore         {   40, 0x26 }, {   42, 0x03 }, {   44, 0x27 }, {   48, 0x28 },
109844aff82SIan Lepore         {   52, 0x05 }, {   56, 0x29 }, {   60, 0x06 }, {   64, 0x2a },
110844aff82SIan Lepore         {   72, 0x2b }, {   80, 0x2c }, {   88, 0x09 }, {   96, 0x2d },
111844aff82SIan Lepore         {  104, 0x0a }, {  112, 0x2e }, {  128, 0x2f }, {  144, 0x0c },
112844aff82SIan Lepore         {  160, 0x30 }, {  192, 0x31 }, {  224, 0x32 }, {  240, 0x0f },
113844aff82SIan Lepore         {  256, 0x33 }, {  288, 0x10 }, {  320, 0x34 }, {  384, 0x35 },
114844aff82SIan Lepore         {  448, 0x36 }, {  480, 0x13 }, {  512, 0x37 }, {  576, 0x14 },
115844aff82SIan Lepore         {  640, 0x38 }, {  768, 0x39 }, {  896, 0x3a }, {  960, 0x17 },
116844aff82SIan Lepore         { 1024, 0x3b }, { 1152, 0x18 }, { 1280, 0x3c }, { 1536, 0x3d },
117844aff82SIan Lepore         { 1792, 0x3e }, { 1920, 0x1b }, { 2048, 0x3f }, { 2304, 0x1c },
118844aff82SIan Lepore         { 2560, 0x1d }, { 3072, 0x1e }, { 3840, 0x1f }, {UINT_MAX, 0x1f}
119844aff82SIan Lepore };
120844aff82SIan Lepore 
12140d7d632SRuslan Bukin static struct ofw_compat_data compat_data[] = {
12240d7d632SRuslan Bukin 	{"fsl,imx6q-i2c",  1},
12340d7d632SRuslan Bukin 	{"fsl,imx-i2c",	   1},
12440d7d632SRuslan Bukin 	{NULL,             0}
12540d7d632SRuslan Bukin };
12640d7d632SRuslan Bukin 
127484b4fd4SRuslan Bukin struct i2c_softc {
128484b4fd4SRuslan Bukin 	device_t		dev;
129484b4fd4SRuslan Bukin 	device_t		iicbus;
130484b4fd4SRuslan Bukin 	struct resource		*res;
131484b4fd4SRuslan Bukin 	int			rid;
132d2c05e20SIan Lepore 	sbintime_t		byte_time_sbt;
133484b4fd4SRuslan Bukin };
134484b4fd4SRuslan Bukin 
135484b4fd4SRuslan Bukin static phandle_t i2c_get_node(device_t, device_t);
136484b4fd4SRuslan Bukin static int i2c_probe(device_t);
137484b4fd4SRuslan Bukin static int i2c_attach(device_t);
138484b4fd4SRuslan Bukin 
139484b4fd4SRuslan Bukin static int i2c_repeated_start(device_t, u_char, int);
140484b4fd4SRuslan Bukin static int i2c_start(device_t, u_char, int);
141484b4fd4SRuslan Bukin static int i2c_stop(device_t);
142484b4fd4SRuslan Bukin static int i2c_reset(device_t, u_char, u_char, u_char *);
143484b4fd4SRuslan Bukin static int i2c_read(device_t, char *, int, int *, int, int);
144484b4fd4SRuslan Bukin static int i2c_write(device_t, const char *, int, int *, int);
145484b4fd4SRuslan Bukin 
146484b4fd4SRuslan Bukin static device_method_t i2c_methods[] = {
147484b4fd4SRuslan Bukin 	DEVMETHOD(device_probe,			i2c_probe),
148484b4fd4SRuslan Bukin 	DEVMETHOD(device_attach,		i2c_attach),
149484b4fd4SRuslan Bukin 
150484b4fd4SRuslan Bukin 	/* OFW methods */
151484b4fd4SRuslan Bukin 	DEVMETHOD(ofw_bus_get_node,		i2c_get_node),
152484b4fd4SRuslan Bukin 
153484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_callback,		iicbus_null_callback),
154484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_repeated_start,	i2c_repeated_start),
155484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_start,			i2c_start),
156484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_stop,			i2c_stop),
157484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_reset,			i2c_reset),
158484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_read,			i2c_read),
159484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_write,			i2c_write),
160484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_transfer,		iicbus_transfer_gen),
161484b4fd4SRuslan Bukin 
162d2c05e20SIan Lepore 	DEVMETHOD_END
163484b4fd4SRuslan Bukin };
164484b4fd4SRuslan Bukin 
165484b4fd4SRuslan Bukin static driver_t i2c_driver = {
166484b4fd4SRuslan Bukin 	"iichb",
167484b4fd4SRuslan Bukin 	i2c_methods,
168484b4fd4SRuslan Bukin 	sizeof(struct i2c_softc),
169484b4fd4SRuslan Bukin };
170484b4fd4SRuslan Bukin static devclass_t  i2c_devclass;
171484b4fd4SRuslan Bukin 
172484b4fd4SRuslan Bukin DRIVER_MODULE(i2c, simplebus, i2c_driver, i2c_devclass, 0, 0);
173484b4fd4SRuslan Bukin DRIVER_MODULE(iicbus, i2c, iicbus_driver, iicbus_devclass, 0, 0);
174484b4fd4SRuslan Bukin 
175484b4fd4SRuslan Bukin static phandle_t
176484b4fd4SRuslan Bukin i2c_get_node(device_t bus, device_t dev)
177484b4fd4SRuslan Bukin {
178484b4fd4SRuslan Bukin 	/*
179484b4fd4SRuslan Bukin 	 * Share controller node with iicbus device
180484b4fd4SRuslan Bukin 	 */
181484b4fd4SRuslan Bukin 	return ofw_bus_get_node(bus);
182484b4fd4SRuslan Bukin }
183484b4fd4SRuslan Bukin 
184484b4fd4SRuslan Bukin static __inline void
185484b4fd4SRuslan Bukin i2c_write_reg(struct i2c_softc *sc, bus_size_t off, uint8_t val)
186484b4fd4SRuslan Bukin {
187484b4fd4SRuslan Bukin 
188d2c05e20SIan Lepore 	bus_write_1(sc->res, off, val);
189484b4fd4SRuslan Bukin }
190484b4fd4SRuslan Bukin 
191484b4fd4SRuslan Bukin static __inline uint8_t
192484b4fd4SRuslan Bukin i2c_read_reg(struct i2c_softc *sc, bus_size_t off)
193484b4fd4SRuslan Bukin {
194484b4fd4SRuslan Bukin 
195d2c05e20SIan Lepore 	return (bus_read_1(sc->res, off));
196484b4fd4SRuslan Bukin }
197484b4fd4SRuslan Bukin 
198484b4fd4SRuslan Bukin static __inline void
199484b4fd4SRuslan Bukin i2c_flag_set(struct i2c_softc *sc, bus_size_t off, uint8_t mask)
200484b4fd4SRuslan Bukin {
201484b4fd4SRuslan Bukin 	uint8_t status;
202484b4fd4SRuslan Bukin 
203484b4fd4SRuslan Bukin 	status = i2c_read_reg(sc, off);
204484b4fd4SRuslan Bukin 	status |= mask;
205484b4fd4SRuslan Bukin 	i2c_write_reg(sc, off, status);
206484b4fd4SRuslan Bukin }
207484b4fd4SRuslan Bukin 
208d2c05e20SIan Lepore /* Wait for bus to become busy or not-busy. */
209484b4fd4SRuslan Bukin static int
210d2c05e20SIan Lepore wait_for_busbusy(struct i2c_softc *sc, int wantbusy)
211484b4fd4SRuslan Bukin {
212d2c05e20SIan Lepore 	int retry, srb;
213484b4fd4SRuslan Bukin 
214484b4fd4SRuslan Bukin 	retry = 1000;
215484b4fd4SRuslan Bukin 	while (retry --) {
216d2c05e20SIan Lepore 		srb = i2c_read_reg(sc, I2C_STATUS_REG) & I2CSR_MBB;
217d2c05e20SIan Lepore 		if ((srb && wantbusy) || (!srb && !wantbusy))
218484b4fd4SRuslan Bukin 			return (IIC_NOERR);
219d2c05e20SIan Lepore 		DELAY(1);
220484b4fd4SRuslan Bukin 	}
221484b4fd4SRuslan Bukin 	return (IIC_ETIMEOUT);
222484b4fd4SRuslan Bukin }
223484b4fd4SRuslan Bukin 
224d2c05e20SIan Lepore /* Wait for transfer to complete, optionally check RXAK. */
225484b4fd4SRuslan Bukin static int
226d2c05e20SIan Lepore wait_for_xfer(struct i2c_softc *sc, int checkack)
227484b4fd4SRuslan Bukin {
228d2c05e20SIan Lepore 	int retry, sr;
229484b4fd4SRuslan Bukin 
230d2c05e20SIan Lepore 	/*
231d2c05e20SIan Lepore 	 * Sleep for about the time it takes to transfer a byte (with precision
232d2c05e20SIan Lepore 	 * set to tolerate 5% oversleep).  We calculate the approximate byte
233d2c05e20SIan Lepore 	 * transfer time when we set the bus speed divisor.  Slaves are allowed
234d2c05e20SIan Lepore 	 * to do clock-stretching so the actual transfer time can be larger, but
235d2c05e20SIan Lepore 	 * this gets the bulk of the waiting out of the way without tying up the
236d2c05e20SIan Lepore 	 * processor the whole time.
237d2c05e20SIan Lepore 	 */
238d2c05e20SIan Lepore 	pause_sbt("imxi2c", sc->byte_time_sbt, sc->byte_time_sbt / 20, 0);
239d2c05e20SIan Lepore 
240d2c05e20SIan Lepore 	retry = 10000;
241484b4fd4SRuslan Bukin 	while (retry --) {
242d2c05e20SIan Lepore 		sr = i2c_read_reg(sc, I2C_STATUS_REG);
243d2c05e20SIan Lepore 		if (sr & I2CSR_MIF) {
244d2c05e20SIan Lepore                         if (sr & I2CSR_MAL)
245d1e99670SIan Lepore 				return (IIC_EBUSERR);
246d2c05e20SIan Lepore 			else if (checkack && (sr & I2CSR_RXAK))
247d2c05e20SIan Lepore 				return (IIC_ENOACK);
248d2c05e20SIan Lepore 			else
249484b4fd4SRuslan Bukin 				return (IIC_NOERR);
250484b4fd4SRuslan Bukin 		}
251d2c05e20SIan Lepore 		DELAY(1);
252d2c05e20SIan Lepore 	}
253484b4fd4SRuslan Bukin 	return (IIC_ETIMEOUT);
254484b4fd4SRuslan Bukin }
255484b4fd4SRuslan Bukin 
256d2c05e20SIan Lepore /*
257d2c05e20SIan Lepore  * Implement the error handling shown in the state diagram of the imx6 reference
258d2c05e20SIan Lepore  * manual.  If there was an error, then:
259d2c05e20SIan Lepore  *  - Clear master mode (MSTA and MTX).
260d2c05e20SIan Lepore  *  - Wait for the bus to become free or for a timeout to happen.
261d2c05e20SIan Lepore  *  - Disable the controller.
262d2c05e20SIan Lepore  */
263484b4fd4SRuslan Bukin static int
264d2c05e20SIan Lepore i2c_error_handler(struct i2c_softc *sc, int error)
265484b4fd4SRuslan Bukin {
266484b4fd4SRuslan Bukin 
267d2c05e20SIan Lepore 	if (error != 0) {
268d2c05e20SIan Lepore 		i2c_write_reg(sc, I2C_STATUS_REG, 0);
269d2c05e20SIan Lepore 		i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN);
270d2c05e20SIan Lepore 		wait_for_busbusy(sc, false);
271d2c05e20SIan Lepore 		i2c_write_reg(sc, I2C_CONTROL_REG, 0);
272484b4fd4SRuslan Bukin 	}
273d2c05e20SIan Lepore 	return (error);
274484b4fd4SRuslan Bukin }
275484b4fd4SRuslan Bukin 
276484b4fd4SRuslan Bukin static int
277484b4fd4SRuslan Bukin i2c_probe(device_t dev)
278484b4fd4SRuslan Bukin {
279484b4fd4SRuslan Bukin 
280484b4fd4SRuslan Bukin 	if (!ofw_bus_status_okay(dev))
281484b4fd4SRuslan Bukin 		return (ENXIO);
282484b4fd4SRuslan Bukin 
28340d7d632SRuslan Bukin 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
284484b4fd4SRuslan Bukin 		return (ENXIO);
285484b4fd4SRuslan Bukin 
286d2c05e20SIan Lepore 	device_set_desc(dev, "Freescale i.MX I2C");
287484b4fd4SRuslan Bukin 
288484b4fd4SRuslan Bukin 	return (BUS_PROBE_DEFAULT);
289484b4fd4SRuslan Bukin }
290484b4fd4SRuslan Bukin 
291484b4fd4SRuslan Bukin static int
292484b4fd4SRuslan Bukin i2c_attach(device_t dev)
293484b4fd4SRuslan Bukin {
294484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
295484b4fd4SRuslan Bukin 
296484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
297484b4fd4SRuslan Bukin 	sc->dev = dev;
298484b4fd4SRuslan Bukin 	sc->rid = 0;
299484b4fd4SRuslan Bukin 
300484b4fd4SRuslan Bukin 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
301484b4fd4SRuslan Bukin 	    RF_ACTIVE);
302484b4fd4SRuslan Bukin 	if (sc->res == NULL) {
303484b4fd4SRuslan Bukin 		device_printf(dev, "could not allocate resources");
304484b4fd4SRuslan Bukin 		return (ENXIO);
305484b4fd4SRuslan Bukin 	}
306484b4fd4SRuslan Bukin 
307484b4fd4SRuslan Bukin 	sc->iicbus = device_add_child(dev, "iicbus", -1);
308484b4fd4SRuslan Bukin 	if (sc->iicbus == NULL) {
309484b4fd4SRuslan Bukin 		device_printf(dev, "could not add iicbus child");
310484b4fd4SRuslan Bukin 		return (ENXIO);
311484b4fd4SRuslan Bukin 	}
312484b4fd4SRuslan Bukin 
313484b4fd4SRuslan Bukin 	bus_generic_attach(dev);
314d2c05e20SIan Lepore 	return (0);
315484b4fd4SRuslan Bukin }
316484b4fd4SRuslan Bukin 
317484b4fd4SRuslan Bukin static int
318484b4fd4SRuslan Bukin i2c_repeated_start(device_t dev, u_char slave, int timeout)
319484b4fd4SRuslan Bukin {
320484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
321484b4fd4SRuslan Bukin 	int error;
322484b4fd4SRuslan Bukin 
323484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
324484b4fd4SRuslan Bukin 
325484b4fd4SRuslan Bukin 	if ((i2c_read_reg(sc, I2C_STATUS_REG) & I2CSR_MBB) == 0) {
326d2c05e20SIan Lepore 		return (IIC_EBUSERR);
327484b4fd4SRuslan Bukin 	}
328484b4fd4SRuslan Bukin 
329d2c05e20SIan Lepore 	/*
330d2c05e20SIan Lepore 	 * Set repeated start condition, delay (per reference manual, min 156nS)
331d2c05e20SIan Lepore 	 * before writing slave address, wait for ack after write.
332d2c05e20SIan Lepore 	 */
333484b4fd4SRuslan Bukin 	i2c_flag_set(sc, I2C_CONTROL_REG, I2CCR_RSTA);
334d2c05e20SIan Lepore 	DELAY(1);
335484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
336484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_DATA_REG, slave);
337d2c05e20SIan Lepore 	error = wait_for_xfer(sc, true);
338d2c05e20SIan Lepore 	return (i2c_error_handler(sc, error));
339484b4fd4SRuslan Bukin }
340484b4fd4SRuslan Bukin 
341484b4fd4SRuslan Bukin static int
342484b4fd4SRuslan Bukin i2c_start(device_t dev, u_char slave, int timeout)
343484b4fd4SRuslan Bukin {
344484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
345484b4fd4SRuslan Bukin 	int error;
346484b4fd4SRuslan Bukin 
347484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
348484b4fd4SRuslan Bukin 
349d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN);
350d2c05e20SIan Lepore 	DELAY(10); /* Delay for controller to sample bus state. */
351484b4fd4SRuslan Bukin 	if (i2c_read_reg(sc, I2C_STATUS_REG) & I2CSR_MBB) {
352d1e99670SIan Lepore 		return (i2c_error_handler(sc, IIC_EBUSERR));
353484b4fd4SRuslan Bukin 	}
354d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN | I2CCR_MSTA | I2CCR_MTX);
355d2c05e20SIan Lepore 	if ((error = wait_for_busbusy(sc, true)) != IIC_NOERR)
356d2c05e20SIan Lepore 		return (i2c_error_handler(sc, error));
357d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_STATUS_REG, 0);
358484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_DATA_REG, slave);
359d2c05e20SIan Lepore 	error = wait_for_xfer(sc, true);
360d2c05e20SIan Lepore 	return (i2c_error_handler(sc, error));
361484b4fd4SRuslan Bukin }
362484b4fd4SRuslan Bukin 
363484b4fd4SRuslan Bukin static int
364484b4fd4SRuslan Bukin i2c_stop(device_t dev)
365484b4fd4SRuslan Bukin {
366484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
367484b4fd4SRuslan Bukin 
368484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
369d2c05e20SIan Lepore 
370d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN);
371d2c05e20SIan Lepore 	wait_for_busbusy(sc, false);
372484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_CONTROL_REG, 0);
373484b4fd4SRuslan Bukin 	return (IIC_NOERR);
374484b4fd4SRuslan Bukin }
375484b4fd4SRuslan Bukin 
376484b4fd4SRuslan Bukin static int
377484b4fd4SRuslan Bukin i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldadr)
378484b4fd4SRuslan Bukin {
379484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
380844aff82SIan Lepore 	u_int busfreq, div, i, ipgfreq;
381484b4fd4SRuslan Bukin 
382484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
383484b4fd4SRuslan Bukin 
384844aff82SIan Lepore 	/*
385844aff82SIan Lepore 	 * Look up the divisor that gives the nearest speed that doesn't exceed
386844aff82SIan Lepore 	 * the configured value for the bus.
387844aff82SIan Lepore 	 */
388844aff82SIan Lepore 	ipgfreq = imx_ccm_ipg_hz();
389844aff82SIan Lepore 	busfreq = IICBUS_GET_FREQUENCY(sc->iicbus, speed);
390f0e56111SPedro F. Giffuni 	div = howmany(ipgfreq, busfreq);
391844aff82SIan Lepore 	for (i = 0; i < nitems(clkdiv_table); i++) {
392844aff82SIan Lepore 		if (clkdiv_table[i].divisor >= div)
393484b4fd4SRuslan Bukin 			break;
394484b4fd4SRuslan Bukin 	}
395484b4fd4SRuslan Bukin 
396d2c05e20SIan Lepore 	/*
397d2c05e20SIan Lepore 	 * Calculate roughly how long it will take to transfer a byte (which
398d2c05e20SIan Lepore 	 * requires 9 clock cycles) at the new bus speed.  This value is used to
399d2c05e20SIan Lepore 	 * pause() while waiting for transfer-complete.  With a 66MHz IPG clock
400d2c05e20SIan Lepore 	 * and the actual i2c bus speeds that leads to, for nominal 100KHz and
401d2c05e20SIan Lepore 	 * 400KHz bus speeds the transfer times are roughly 104uS and 22uS.
402d2c05e20SIan Lepore 	 */
403d2c05e20SIan Lepore 	busfreq = ipgfreq / clkdiv_table[i].divisor;
404d2c05e20SIan Lepore 	sc->byte_time_sbt = SBT_1US * (9000000 / busfreq);
405d2c05e20SIan Lepore 
406d2c05e20SIan Lepore 	/*
407d2c05e20SIan Lepore 	 * Disable the controller (do the reset), and set the new clock divisor.
408d2c05e20SIan Lepore 	 */
409d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
410484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_CONTROL_REG, 0x0);
411d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_FDR_REG, (uint8_t)clkdiv_table[i].regcode);
412484b4fd4SRuslan Bukin 	return (IIC_NOERR);
413484b4fd4SRuslan Bukin }
414484b4fd4SRuslan Bukin 
415484b4fd4SRuslan Bukin static int
416484b4fd4SRuslan Bukin i2c_read(device_t dev, char *buf, int len, int *read, int last, int delay)
417484b4fd4SRuslan Bukin {
418484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
419484b4fd4SRuslan Bukin 	int error, reg;
420484b4fd4SRuslan Bukin 
421484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
422484b4fd4SRuslan Bukin 	*read = 0;
423484b4fd4SRuslan Bukin 
424484b4fd4SRuslan Bukin 	if (len) {
425484b4fd4SRuslan Bukin 		if (len == 1)
426484b4fd4SRuslan Bukin 			i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
427484b4fd4SRuslan Bukin 			    I2CCR_MSTA | I2CCR_TXAK);
428484b4fd4SRuslan Bukin 		else
429484b4fd4SRuslan Bukin 			i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
430484b4fd4SRuslan Bukin 			    I2CCR_MSTA);
431d2c05e20SIan Lepore                 /* Dummy read to prime the receiver. */
432484b4fd4SRuslan Bukin 		i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
433d2c05e20SIan Lepore 		i2c_read_reg(sc, I2C_DATA_REG);
434d2c05e20SIan Lepore 	}
435d2c05e20SIan Lepore 
436d2c05e20SIan Lepore 	error = 0;
437d2c05e20SIan Lepore 	*read = 0;
438d2c05e20SIan Lepore 	while (*read < len) {
439d2c05e20SIan Lepore 		if ((error = wait_for_xfer(sc, false)) != IIC_NOERR)
440d2c05e20SIan Lepore 			break;
441d2c05e20SIan Lepore 		i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
442d2c05e20SIan Lepore 		if (last) {
443d2c05e20SIan Lepore 			if (*read == len - 2) {
444484b4fd4SRuslan Bukin 				/* NO ACK on last byte */
445484b4fd4SRuslan Bukin 				i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
446484b4fd4SRuslan Bukin 				    I2CCR_MSTA | I2CCR_TXAK);
447d2c05e20SIan Lepore 			} else if (*read == len - 1) {
448d2c05e20SIan Lepore 				/* Transfer done, signal stop. */
449484b4fd4SRuslan Bukin 				i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
450484b4fd4SRuslan Bukin 				    I2CCR_TXAK);
451d2c05e20SIan Lepore 				wait_for_busbusy(sc, false);
452484b4fd4SRuslan Bukin 			}
453d2c05e20SIan Lepore 		}
454484b4fd4SRuslan Bukin 		reg = i2c_read_reg(sc, I2C_DATA_REG);
455484b4fd4SRuslan Bukin 		*buf++ = reg;
456484b4fd4SRuslan Bukin 		(*read)++;
457484b4fd4SRuslan Bukin 	}
458484b4fd4SRuslan Bukin 
459d2c05e20SIan Lepore 	return (i2c_error_handler(sc, error));
460484b4fd4SRuslan Bukin }
461484b4fd4SRuslan Bukin 
462484b4fd4SRuslan Bukin static int
463484b4fd4SRuslan Bukin i2c_write(device_t dev, const char *buf, int len, int *sent, int timeout)
464484b4fd4SRuslan Bukin {
465484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
466484b4fd4SRuslan Bukin 	int error;
467484b4fd4SRuslan Bukin 
468484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
469484b4fd4SRuslan Bukin 
470d2c05e20SIan Lepore 	error = 0;
471d2c05e20SIan Lepore 	*sent = 0;
472484b4fd4SRuslan Bukin 	while (*sent < len) {
473484b4fd4SRuslan Bukin 		i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
474484b4fd4SRuslan Bukin 		i2c_write_reg(sc, I2C_DATA_REG, *buf++);
475d2c05e20SIan Lepore 		if ((error = wait_for_xfer(sc, true)) != IIC_NOERR)
476d2c05e20SIan Lepore 			break;
477484b4fd4SRuslan Bukin 		(*sent)++;
478484b4fd4SRuslan Bukin 	}
479484b4fd4SRuslan Bukin 
480d2c05e20SIan Lepore 	return (i2c_error_handler(sc, error));
481484b4fd4SRuslan Bukin }
482