xref: /freebsd/sys/arm/freescale/imx/imx_i2c.c (revision d1e99670ae56b733bfffe627047eb0a552b5ac7e)
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  *
38d2c05e20SIan Lepore  * This driver supports multi-master i2c busses, 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/fdt/fdt_common.h>
67484b4fd4SRuslan Bukin #include <dev/ofw/openfirm.h>
68484b4fd4SRuslan Bukin #include <dev/ofw/ofw_bus.h>
69484b4fd4SRuslan Bukin #include <dev/ofw/ofw_bus_subr.h>
70484b4fd4SRuslan Bukin 
71484b4fd4SRuslan Bukin #define I2C_ADDR_REG		0x00 /* I2C slave address register */
72484b4fd4SRuslan Bukin #define I2C_FDR_REG		0x04 /* I2C frequency divider register */
73484b4fd4SRuslan Bukin #define I2C_CONTROL_REG		0x08 /* I2C control register */
74484b4fd4SRuslan Bukin #define I2C_STATUS_REG		0x0C /* I2C status register */
75484b4fd4SRuslan Bukin #define I2C_DATA_REG		0x10 /* I2C data register */
76484b4fd4SRuslan Bukin #define I2C_DFSRR_REG		0x14 /* I2C Digital Filter Sampling rate */
77484b4fd4SRuslan Bukin 
78484b4fd4SRuslan Bukin #define I2CCR_MEN		(1 << 7) /* Module enable */
79484b4fd4SRuslan Bukin #define I2CCR_MSTA		(1 << 5) /* Master/slave mode */
80484b4fd4SRuslan Bukin #define I2CCR_MTX		(1 << 4) /* Transmit/receive mode */
81484b4fd4SRuslan Bukin #define I2CCR_TXAK		(1 << 3) /* Transfer acknowledge */
82484b4fd4SRuslan Bukin #define I2CCR_RSTA		(1 << 2) /* Repeated START */
83484b4fd4SRuslan Bukin 
84484b4fd4SRuslan Bukin #define I2CSR_MCF		(1 << 7) /* Data transfer */
85484b4fd4SRuslan Bukin #define I2CSR_MASS		(1 << 6) /* Addressed as a slave */
86484b4fd4SRuslan Bukin #define I2CSR_MBB		(1 << 5) /* Bus busy */
87484b4fd4SRuslan Bukin #define I2CSR_MAL		(1 << 4) /* Arbitration lost */
88484b4fd4SRuslan Bukin #define I2CSR_SRW		(1 << 2) /* Slave read/write */
89484b4fd4SRuslan Bukin #define I2CSR_MIF		(1 << 1) /* Module interrupt */
90484b4fd4SRuslan Bukin #define I2CSR_RXAK		(1 << 0) /* Received acknowledge */
91484b4fd4SRuslan Bukin 
92484b4fd4SRuslan Bukin #define I2C_BAUD_RATE_FAST	0x31
93484b4fd4SRuslan Bukin #define I2C_BAUD_RATE_DEF	0x3F
94484b4fd4SRuslan Bukin #define I2C_DFSSR_DIV		0x10
95484b4fd4SRuslan Bukin 
96844aff82SIan Lepore /*
97844aff82SIan Lepore  * A table of available divisors and the associated coded values to put in the
98844aff82SIan Lepore  * FDR register to achieve that divisor.. There is no algorithmic relationship I
99844aff82SIan Lepore  * can see between divisors and the codes that go into the register.  The table
100844aff82SIan Lepore  * begins and ends with entries that handle insane configuration values.
101844aff82SIan Lepore  */
102844aff82SIan Lepore struct clkdiv {
103844aff82SIan Lepore 	u_int divisor;
104844aff82SIan Lepore 	u_int regcode;
105844aff82SIan Lepore };
106844aff82SIan Lepore static struct clkdiv clkdiv_table[] = {
107844aff82SIan Lepore         {    0, 0x20 }, {   22, 0x20 }, {   24, 0x21 }, {   26, 0x22 },
108844aff82SIan Lepore         {   28, 0x23 }, {   30, 0x00 }, {   32, 0x24 }, {   36, 0x25 },
109844aff82SIan Lepore         {   40, 0x26 }, {   42, 0x03 }, {   44, 0x27 }, {   48, 0x28 },
110844aff82SIan Lepore         {   52, 0x05 }, {   56, 0x29 }, {   60, 0x06 }, {   64, 0x2a },
111844aff82SIan Lepore         {   72, 0x2b }, {   80, 0x2c }, {   88, 0x09 }, {   96, 0x2d },
112844aff82SIan Lepore         {  104, 0x0a }, {  112, 0x2e }, {  128, 0x2f }, {  144, 0x0c },
113844aff82SIan Lepore         {  160, 0x30 }, {  192, 0x31 }, {  224, 0x32 }, {  240, 0x0f },
114844aff82SIan Lepore         {  256, 0x33 }, {  288, 0x10 }, {  320, 0x34 }, {  384, 0x35 },
115844aff82SIan Lepore         {  448, 0x36 }, {  480, 0x13 }, {  512, 0x37 }, {  576, 0x14 },
116844aff82SIan Lepore         {  640, 0x38 }, {  768, 0x39 }, {  896, 0x3a }, {  960, 0x17 },
117844aff82SIan Lepore         { 1024, 0x3b }, { 1152, 0x18 }, { 1280, 0x3c }, { 1536, 0x3d },
118844aff82SIan Lepore         { 1792, 0x3e }, { 1920, 0x1b }, { 2048, 0x3f }, { 2304, 0x1c },
119844aff82SIan Lepore         { 2560, 0x1d }, { 3072, 0x1e }, { 3840, 0x1f }, {UINT_MAX, 0x1f}
120844aff82SIan Lepore };
121844aff82SIan Lepore 
12240d7d632SRuslan Bukin static struct ofw_compat_data compat_data[] = {
12340d7d632SRuslan Bukin 	{"fsl,imx6q-i2c",  1},
12440d7d632SRuslan Bukin 	{"fsl,imx-i2c",	   1},
12540d7d632SRuslan Bukin 	{NULL,             0}
12640d7d632SRuslan Bukin };
12740d7d632SRuslan Bukin 
128484b4fd4SRuslan Bukin struct i2c_softc {
129484b4fd4SRuslan Bukin 	device_t		dev;
130484b4fd4SRuslan Bukin 	device_t		iicbus;
131484b4fd4SRuslan Bukin 	struct resource		*res;
132484b4fd4SRuslan Bukin 	int			rid;
133d2c05e20SIan Lepore 	sbintime_t		byte_time_sbt;
134484b4fd4SRuslan Bukin };
135484b4fd4SRuslan Bukin 
136484b4fd4SRuslan Bukin static phandle_t i2c_get_node(device_t, device_t);
137484b4fd4SRuslan Bukin static int i2c_probe(device_t);
138484b4fd4SRuslan Bukin static int i2c_attach(device_t);
139484b4fd4SRuslan Bukin 
140484b4fd4SRuslan Bukin static int i2c_repeated_start(device_t, u_char, int);
141484b4fd4SRuslan Bukin static int i2c_start(device_t, u_char, int);
142484b4fd4SRuslan Bukin static int i2c_stop(device_t);
143484b4fd4SRuslan Bukin static int i2c_reset(device_t, u_char, u_char, u_char *);
144484b4fd4SRuslan Bukin static int i2c_read(device_t, char *, int, int *, int, int);
145484b4fd4SRuslan Bukin static int i2c_write(device_t, const char *, int, int *, int);
146484b4fd4SRuslan Bukin 
147484b4fd4SRuslan Bukin static device_method_t i2c_methods[] = {
148484b4fd4SRuslan Bukin 	DEVMETHOD(device_probe,			i2c_probe),
149484b4fd4SRuslan Bukin 	DEVMETHOD(device_attach,		i2c_attach),
150484b4fd4SRuslan Bukin 
151484b4fd4SRuslan Bukin 	/* OFW methods */
152484b4fd4SRuslan Bukin 	DEVMETHOD(ofw_bus_get_node,		i2c_get_node),
153484b4fd4SRuslan Bukin 
154484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_callback,		iicbus_null_callback),
155484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_repeated_start,	i2c_repeated_start),
156484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_start,			i2c_start),
157484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_stop,			i2c_stop),
158484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_reset,			i2c_reset),
159484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_read,			i2c_read),
160484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_write,			i2c_write),
161484b4fd4SRuslan Bukin 	DEVMETHOD(iicbus_transfer,		iicbus_transfer_gen),
162484b4fd4SRuslan Bukin 
163d2c05e20SIan Lepore 	DEVMETHOD_END
164484b4fd4SRuslan Bukin };
165484b4fd4SRuslan Bukin 
166484b4fd4SRuslan Bukin static driver_t i2c_driver = {
167484b4fd4SRuslan Bukin 	"iichb",
168484b4fd4SRuslan Bukin 	i2c_methods,
169484b4fd4SRuslan Bukin 	sizeof(struct i2c_softc),
170484b4fd4SRuslan Bukin };
171484b4fd4SRuslan Bukin static devclass_t  i2c_devclass;
172484b4fd4SRuslan Bukin 
173484b4fd4SRuslan Bukin DRIVER_MODULE(i2c, simplebus, i2c_driver, i2c_devclass, 0, 0);
174484b4fd4SRuslan Bukin DRIVER_MODULE(iicbus, i2c, iicbus_driver, iicbus_devclass, 0, 0);
175484b4fd4SRuslan Bukin 
176484b4fd4SRuslan Bukin static phandle_t
177484b4fd4SRuslan Bukin i2c_get_node(device_t bus, device_t dev)
178484b4fd4SRuslan Bukin {
179484b4fd4SRuslan Bukin 	/*
180484b4fd4SRuslan Bukin 	 * Share controller node with iicbus device
181484b4fd4SRuslan Bukin 	 */
182484b4fd4SRuslan Bukin 	return ofw_bus_get_node(bus);
183484b4fd4SRuslan Bukin }
184484b4fd4SRuslan Bukin 
185484b4fd4SRuslan Bukin static __inline void
186484b4fd4SRuslan Bukin i2c_write_reg(struct i2c_softc *sc, bus_size_t off, uint8_t val)
187484b4fd4SRuslan Bukin {
188484b4fd4SRuslan Bukin 
189d2c05e20SIan Lepore 	bus_write_1(sc->res, off, val);
190484b4fd4SRuslan Bukin }
191484b4fd4SRuslan Bukin 
192484b4fd4SRuslan Bukin static __inline uint8_t
193484b4fd4SRuslan Bukin i2c_read_reg(struct i2c_softc *sc, bus_size_t off)
194484b4fd4SRuslan Bukin {
195484b4fd4SRuslan Bukin 
196d2c05e20SIan Lepore 	return (bus_read_1(sc->res, off));
197484b4fd4SRuslan Bukin }
198484b4fd4SRuslan Bukin 
199484b4fd4SRuslan Bukin static __inline void
200484b4fd4SRuslan Bukin i2c_flag_set(struct i2c_softc *sc, bus_size_t off, uint8_t mask)
201484b4fd4SRuslan Bukin {
202484b4fd4SRuslan Bukin 	uint8_t status;
203484b4fd4SRuslan Bukin 
204484b4fd4SRuslan Bukin 	status = i2c_read_reg(sc, off);
205484b4fd4SRuslan Bukin 	status |= mask;
206484b4fd4SRuslan Bukin 	i2c_write_reg(sc, off, status);
207484b4fd4SRuslan Bukin }
208484b4fd4SRuslan Bukin 
209d2c05e20SIan Lepore /* Wait for bus to become busy or not-busy. */
210484b4fd4SRuslan Bukin static int
211d2c05e20SIan Lepore wait_for_busbusy(struct i2c_softc *sc, int wantbusy)
212484b4fd4SRuslan Bukin {
213d2c05e20SIan Lepore 	int retry, srb;
214484b4fd4SRuslan Bukin 
215484b4fd4SRuslan Bukin 	retry = 1000;
216484b4fd4SRuslan Bukin 	while (retry --) {
217d2c05e20SIan Lepore 		srb = i2c_read_reg(sc, I2C_STATUS_REG) & I2CSR_MBB;
218d2c05e20SIan Lepore 		if ((srb && wantbusy) || (!srb && !wantbusy))
219484b4fd4SRuslan Bukin 			return (IIC_NOERR);
220d2c05e20SIan Lepore 		DELAY(1);
221484b4fd4SRuslan Bukin 	}
222484b4fd4SRuslan Bukin 	return (IIC_ETIMEOUT);
223484b4fd4SRuslan Bukin }
224484b4fd4SRuslan Bukin 
225d2c05e20SIan Lepore /* Wait for transfer to complete, optionally check RXAK. */
226484b4fd4SRuslan Bukin static int
227d2c05e20SIan Lepore wait_for_xfer(struct i2c_softc *sc, int checkack)
228484b4fd4SRuslan Bukin {
229d2c05e20SIan Lepore 	int retry, sr;
230484b4fd4SRuslan Bukin 
231d2c05e20SIan Lepore 	/*
232d2c05e20SIan Lepore 	 * Sleep for about the time it takes to transfer a byte (with precision
233d2c05e20SIan Lepore 	 * set to tolerate 5% oversleep).  We calculate the approximate byte
234d2c05e20SIan Lepore 	 * transfer time when we set the bus speed divisor.  Slaves are allowed
235d2c05e20SIan Lepore 	 * to do clock-stretching so the actual transfer time can be larger, but
236d2c05e20SIan Lepore 	 * this gets the bulk of the waiting out of the way without tying up the
237d2c05e20SIan Lepore 	 * processor the whole time.
238d2c05e20SIan Lepore 	 */
239d2c05e20SIan Lepore 	pause_sbt("imxi2c", sc->byte_time_sbt, sc->byte_time_sbt / 20, 0);
240d2c05e20SIan Lepore 
241d2c05e20SIan Lepore 	retry = 10000;
242484b4fd4SRuslan Bukin 	while (retry --) {
243d2c05e20SIan Lepore 		sr = i2c_read_reg(sc, I2C_STATUS_REG);
244d2c05e20SIan Lepore 		if (sr & I2CSR_MIF) {
245d2c05e20SIan Lepore                         if (sr & I2CSR_MAL)
246*d1e99670SIan Lepore 				return (IIC_EBUSERR);
247d2c05e20SIan Lepore 			else if (checkack && (sr & I2CSR_RXAK))
248d2c05e20SIan Lepore 				return (IIC_ENOACK);
249d2c05e20SIan Lepore 			else
250484b4fd4SRuslan Bukin 				return (IIC_NOERR);
251484b4fd4SRuslan Bukin 		}
252d2c05e20SIan Lepore 		DELAY(1);
253d2c05e20SIan Lepore 	}
254484b4fd4SRuslan Bukin 	return (IIC_ETIMEOUT);
255484b4fd4SRuslan Bukin }
256484b4fd4SRuslan Bukin 
257d2c05e20SIan Lepore /*
258d2c05e20SIan Lepore  * Implement the error handling shown in the state diagram of the imx6 reference
259d2c05e20SIan Lepore  * manual.  If there was an error, then:
260d2c05e20SIan Lepore  *  - Clear master mode (MSTA and MTX).
261d2c05e20SIan Lepore  *  - Wait for the bus to become free or for a timeout to happen.
262d2c05e20SIan Lepore  *  - Disable the controller.
263d2c05e20SIan Lepore  */
264484b4fd4SRuslan Bukin static int
265d2c05e20SIan Lepore i2c_error_handler(struct i2c_softc *sc, int error)
266484b4fd4SRuslan Bukin {
267484b4fd4SRuslan Bukin 
268d2c05e20SIan Lepore 	if (error != 0) {
269d2c05e20SIan Lepore 		i2c_write_reg(sc, I2C_STATUS_REG, 0);
270d2c05e20SIan Lepore 		i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN);
271d2c05e20SIan Lepore 		wait_for_busbusy(sc, false);
272d2c05e20SIan Lepore 		i2c_write_reg(sc, I2C_CONTROL_REG, 0);
273484b4fd4SRuslan Bukin 	}
274d2c05e20SIan Lepore 	return (error);
275484b4fd4SRuslan Bukin }
276484b4fd4SRuslan Bukin 
277484b4fd4SRuslan Bukin static int
278484b4fd4SRuslan Bukin i2c_probe(device_t dev)
279484b4fd4SRuslan Bukin {
280484b4fd4SRuslan Bukin 
281484b4fd4SRuslan Bukin 	if (!ofw_bus_status_okay(dev))
282484b4fd4SRuslan Bukin 		return (ENXIO);
283484b4fd4SRuslan Bukin 
28440d7d632SRuslan Bukin 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
285484b4fd4SRuslan Bukin 		return (ENXIO);
286484b4fd4SRuslan Bukin 
287d2c05e20SIan Lepore 	device_set_desc(dev, "Freescale i.MX I2C");
288484b4fd4SRuslan Bukin 
289484b4fd4SRuslan Bukin 	return (BUS_PROBE_DEFAULT);
290484b4fd4SRuslan Bukin }
291484b4fd4SRuslan Bukin 
292484b4fd4SRuslan Bukin static int
293484b4fd4SRuslan Bukin i2c_attach(device_t dev)
294484b4fd4SRuslan Bukin {
295484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
296484b4fd4SRuslan Bukin 
297484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
298484b4fd4SRuslan Bukin 	sc->dev = dev;
299484b4fd4SRuslan Bukin 	sc->rid = 0;
300484b4fd4SRuslan Bukin 
301484b4fd4SRuslan Bukin 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
302484b4fd4SRuslan Bukin 	    RF_ACTIVE);
303484b4fd4SRuslan Bukin 	if (sc->res == NULL) {
304484b4fd4SRuslan Bukin 		device_printf(dev, "could not allocate resources");
305484b4fd4SRuslan Bukin 		return (ENXIO);
306484b4fd4SRuslan Bukin 	}
307484b4fd4SRuslan Bukin 
308484b4fd4SRuslan Bukin 	sc->iicbus = device_add_child(dev, "iicbus", -1);
309484b4fd4SRuslan Bukin 	if (sc->iicbus == NULL) {
310484b4fd4SRuslan Bukin 		device_printf(dev, "could not add iicbus child");
311484b4fd4SRuslan Bukin 		return (ENXIO);
312484b4fd4SRuslan Bukin 	}
313484b4fd4SRuslan Bukin 
314484b4fd4SRuslan Bukin 	bus_generic_attach(dev);
315d2c05e20SIan Lepore 	return (0);
316484b4fd4SRuslan Bukin }
317484b4fd4SRuslan Bukin 
318484b4fd4SRuslan Bukin static int
319484b4fd4SRuslan Bukin i2c_repeated_start(device_t dev, u_char slave, int timeout)
320484b4fd4SRuslan Bukin {
321484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
322484b4fd4SRuslan Bukin 	int error;
323484b4fd4SRuslan Bukin 
324484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
325484b4fd4SRuslan Bukin 
326484b4fd4SRuslan Bukin 	if ((i2c_read_reg(sc, I2C_STATUS_REG) & I2CSR_MBB) == 0) {
327d2c05e20SIan Lepore 		return (IIC_EBUSERR);
328484b4fd4SRuslan Bukin 	}
329484b4fd4SRuslan Bukin 
330d2c05e20SIan Lepore 	/*
331d2c05e20SIan Lepore 	 * Set repeated start condition, delay (per reference manual, min 156nS)
332d2c05e20SIan Lepore 	 * before writing slave address, wait for ack after write.
333d2c05e20SIan Lepore 	 */
334484b4fd4SRuslan Bukin 	i2c_flag_set(sc, I2C_CONTROL_REG, I2CCR_RSTA);
335d2c05e20SIan Lepore 	DELAY(1);
336484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
337484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_DATA_REG, slave);
338d2c05e20SIan Lepore 	error = wait_for_xfer(sc, true);
339d2c05e20SIan Lepore 	return (i2c_error_handler(sc, error));
340484b4fd4SRuslan Bukin }
341484b4fd4SRuslan Bukin 
342484b4fd4SRuslan Bukin static int
343484b4fd4SRuslan Bukin i2c_start(device_t dev, u_char slave, int timeout)
344484b4fd4SRuslan Bukin {
345484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
346484b4fd4SRuslan Bukin 	int error;
347484b4fd4SRuslan Bukin 
348484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
349484b4fd4SRuslan Bukin 
350d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN);
351d2c05e20SIan Lepore 	DELAY(10); /* Delay for controller to sample bus state. */
352484b4fd4SRuslan Bukin 	if (i2c_read_reg(sc, I2C_STATUS_REG) & I2CSR_MBB) {
353*d1e99670SIan Lepore 		return (i2c_error_handler(sc, IIC_EBUSERR));
354484b4fd4SRuslan Bukin 	}
355d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN | I2CCR_MSTA | I2CCR_MTX);
356d2c05e20SIan Lepore 	if ((error = wait_for_busbusy(sc, true)) != IIC_NOERR)
357d2c05e20SIan Lepore 		return (i2c_error_handler(sc, error));
358d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_STATUS_REG, 0);
359484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_DATA_REG, slave);
360d2c05e20SIan Lepore 	error = wait_for_xfer(sc, true);
361d2c05e20SIan Lepore 	return (i2c_error_handler(sc, error));
362484b4fd4SRuslan Bukin }
363484b4fd4SRuslan Bukin 
364484b4fd4SRuslan Bukin static int
365484b4fd4SRuslan Bukin i2c_stop(device_t dev)
366484b4fd4SRuslan Bukin {
367484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
368484b4fd4SRuslan Bukin 
369484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
370d2c05e20SIan Lepore 
371d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN);
372d2c05e20SIan Lepore 	wait_for_busbusy(sc, false);
373484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_CONTROL_REG, 0);
374484b4fd4SRuslan Bukin 	return (IIC_NOERR);
375484b4fd4SRuslan Bukin }
376484b4fd4SRuslan Bukin 
377484b4fd4SRuslan Bukin static int
378484b4fd4SRuslan Bukin i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldadr)
379484b4fd4SRuslan Bukin {
380484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
381844aff82SIan Lepore 	u_int busfreq, div, i, ipgfreq;
382484b4fd4SRuslan Bukin 
383484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
384484b4fd4SRuslan Bukin 
385844aff82SIan Lepore 	/*
386844aff82SIan Lepore 	 * Look up the divisor that gives the nearest speed that doesn't exceed
387844aff82SIan Lepore 	 * the configured value for the bus.
388844aff82SIan Lepore 	 */
389844aff82SIan Lepore 	ipgfreq = imx_ccm_ipg_hz();
390844aff82SIan Lepore 	busfreq = IICBUS_GET_FREQUENCY(sc->iicbus, speed);
391844aff82SIan Lepore 	div = (ipgfreq + busfreq - 1) / busfreq;
392844aff82SIan Lepore 	for (i = 0; i < nitems(clkdiv_table); i++) {
393844aff82SIan Lepore 		if (clkdiv_table[i].divisor >= div)
394484b4fd4SRuslan Bukin 			break;
395484b4fd4SRuslan Bukin 	}
396484b4fd4SRuslan Bukin 
397d2c05e20SIan Lepore 	/*
398d2c05e20SIan Lepore 	 * Calculate roughly how long it will take to transfer a byte (which
399d2c05e20SIan Lepore 	 * requires 9 clock cycles) at the new bus speed.  This value is used to
400d2c05e20SIan Lepore 	 * pause() while waiting for transfer-complete.  With a 66MHz IPG clock
401d2c05e20SIan Lepore 	 * and the actual i2c bus speeds that leads to, for nominal 100KHz and
402d2c05e20SIan Lepore 	 * 400KHz bus speeds the transfer times are roughly 104uS and 22uS.
403d2c05e20SIan Lepore 	 */
404d2c05e20SIan Lepore 	busfreq = ipgfreq / clkdiv_table[i].divisor;
405d2c05e20SIan Lepore 	sc->byte_time_sbt = SBT_1US * (9000000 / busfreq);
406d2c05e20SIan Lepore 
407d2c05e20SIan Lepore 	/*
408d2c05e20SIan Lepore 	 * Disable the controller (do the reset), and set the new clock divisor.
409d2c05e20SIan Lepore 	 */
410d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
411484b4fd4SRuslan Bukin 	i2c_write_reg(sc, I2C_CONTROL_REG, 0x0);
412d2c05e20SIan Lepore 	i2c_write_reg(sc, I2C_FDR_REG, (uint8_t)clkdiv_table[i].regcode);
413484b4fd4SRuslan Bukin 	return (IIC_NOERR);
414484b4fd4SRuslan Bukin }
415484b4fd4SRuslan Bukin 
416484b4fd4SRuslan Bukin static int
417484b4fd4SRuslan Bukin i2c_read(device_t dev, char *buf, int len, int *read, int last, int delay)
418484b4fd4SRuslan Bukin {
419484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
420484b4fd4SRuslan Bukin 	int error, reg;
421484b4fd4SRuslan Bukin 
422484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
423484b4fd4SRuslan Bukin 	*read = 0;
424484b4fd4SRuslan Bukin 
425484b4fd4SRuslan Bukin 	if (len) {
426484b4fd4SRuslan Bukin 		if (len == 1)
427484b4fd4SRuslan Bukin 			i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
428484b4fd4SRuslan Bukin 			    I2CCR_MSTA | I2CCR_TXAK);
429484b4fd4SRuslan Bukin 		else
430484b4fd4SRuslan Bukin 			i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
431484b4fd4SRuslan Bukin 			    I2CCR_MSTA);
432d2c05e20SIan Lepore                 /* Dummy read to prime the receiver. */
433484b4fd4SRuslan Bukin 		i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
434d2c05e20SIan Lepore 		i2c_read_reg(sc, I2C_DATA_REG);
435d2c05e20SIan Lepore 	}
436d2c05e20SIan Lepore 
437d2c05e20SIan Lepore 	error = 0;
438d2c05e20SIan Lepore 	*read = 0;
439d2c05e20SIan Lepore 	while (*read < len) {
440d2c05e20SIan Lepore 		if ((error = wait_for_xfer(sc, false)) != IIC_NOERR)
441d2c05e20SIan Lepore 			break;
442d2c05e20SIan Lepore 		i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
443d2c05e20SIan Lepore 		if (last) {
444d2c05e20SIan Lepore 			if (*read == len - 2) {
445484b4fd4SRuslan Bukin 				/* NO ACK on last byte */
446484b4fd4SRuslan Bukin 				i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
447484b4fd4SRuslan Bukin 				    I2CCR_MSTA | I2CCR_TXAK);
448d2c05e20SIan Lepore 			} else if (*read == len - 1) {
449d2c05e20SIan Lepore 				/* Transfer done, signal stop. */
450484b4fd4SRuslan Bukin 				i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
451484b4fd4SRuslan Bukin 				    I2CCR_TXAK);
452d2c05e20SIan Lepore 				wait_for_busbusy(sc, false);
453484b4fd4SRuslan Bukin 			}
454d2c05e20SIan Lepore 		}
455484b4fd4SRuslan Bukin 		reg = i2c_read_reg(sc, I2C_DATA_REG);
456484b4fd4SRuslan Bukin 		*buf++ = reg;
457484b4fd4SRuslan Bukin 		(*read)++;
458484b4fd4SRuslan Bukin 	}
459484b4fd4SRuslan Bukin 
460d2c05e20SIan Lepore 	return (i2c_error_handler(sc, error));
461484b4fd4SRuslan Bukin }
462484b4fd4SRuslan Bukin 
463484b4fd4SRuslan Bukin static int
464484b4fd4SRuslan Bukin i2c_write(device_t dev, const char *buf, int len, int *sent, int timeout)
465484b4fd4SRuslan Bukin {
466484b4fd4SRuslan Bukin 	struct i2c_softc *sc;
467484b4fd4SRuslan Bukin 	int error;
468484b4fd4SRuslan Bukin 
469484b4fd4SRuslan Bukin 	sc = device_get_softc(dev);
470484b4fd4SRuslan Bukin 
471d2c05e20SIan Lepore 	error = 0;
472d2c05e20SIan Lepore 	*sent = 0;
473484b4fd4SRuslan Bukin 	while (*sent < len) {
474484b4fd4SRuslan Bukin 		i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
475484b4fd4SRuslan Bukin 		i2c_write_reg(sc, I2C_DATA_REG, *buf++);
476d2c05e20SIan Lepore 		if ((error = wait_for_xfer(sc, true)) != IIC_NOERR)
477d2c05e20SIan Lepore 			break;
478484b4fd4SRuslan Bukin 		(*sent)++;
479484b4fd4SRuslan Bukin 	}
480484b4fd4SRuslan Bukin 
481d2c05e20SIan Lepore 	return (i2c_error_handler(sc, error));
482484b4fd4SRuslan Bukin }
483