1757cb6dbSRafal Jaworowski /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
371e3c308SPedro F. Giffuni *
4757cb6dbSRafal Jaworowski * Copyright (C) 2008-2009 Semihalf, Michal Hajduk
5757cb6dbSRafal Jaworowski * All rights reserved.
6757cb6dbSRafal Jaworowski *
7757cb6dbSRafal Jaworowski * Redistribution and use in source and binary forms, with or without
8757cb6dbSRafal Jaworowski * modification, are permitted provided that the following conditions
9757cb6dbSRafal Jaworowski * are met:
10757cb6dbSRafal Jaworowski * 1. Redistributions of source code must retain the above copyright
11757cb6dbSRafal Jaworowski * notice, this list of conditions and the following disclaimer.
12757cb6dbSRafal Jaworowski * 2. Redistributions in binary form must reproduce the above copyright
13757cb6dbSRafal Jaworowski * notice, this list of conditions and the following disclaimer in the
14757cb6dbSRafal Jaworowski * documentation and/or other materials provided with the distribution.
15757cb6dbSRafal Jaworowski *
16757cb6dbSRafal Jaworowski * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17757cb6dbSRafal Jaworowski * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18757cb6dbSRafal Jaworowski * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19757cb6dbSRafal Jaworowski * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20757cb6dbSRafal Jaworowski * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21757cb6dbSRafal Jaworowski * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22757cb6dbSRafal Jaworowski * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23757cb6dbSRafal Jaworowski * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24757cb6dbSRafal Jaworowski * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25757cb6dbSRafal Jaworowski * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26757cb6dbSRafal Jaworowski * SUCH DAMAGE.
27757cb6dbSRafal Jaworowski */
28757cb6dbSRafal Jaworowski
29757cb6dbSRafal Jaworowski #include <sys/param.h>
30757cb6dbSRafal Jaworowski #include <sys/systm.h>
31757cb6dbSRafal Jaworowski #include <sys/bus.h>
32757cb6dbSRafal Jaworowski #include <sys/kernel.h>
33757cb6dbSRafal Jaworowski #include <sys/module.h>
34757cb6dbSRafal Jaworowski #include <sys/resource.h>
35757cb6dbSRafal Jaworowski
36757cb6dbSRafal Jaworowski #include <machine/bus.h>
37757cb6dbSRafal Jaworowski #include <machine/resource.h>
38757cb6dbSRafal Jaworowski #include <sys/rman.h>
39757cb6dbSRafal Jaworowski
40757cb6dbSRafal Jaworowski #include <sys/lock.h>
41757cb6dbSRafal Jaworowski #include <sys/mutex.h>
42757cb6dbSRafal Jaworowski
43757cb6dbSRafal Jaworowski #include <dev/iicbus/iiconf.h>
44757cb6dbSRafal Jaworowski #include <dev/iicbus/iicbus.h>
45757cb6dbSRafal Jaworowski #include "iicbus_if.h"
46757cb6dbSRafal Jaworowski
47d1d3233eSRafal Jaworowski #include <dev/ofw/ofw_bus.h>
48d1d3233eSRafal Jaworowski #include <dev/ofw/ofw_bus_subr.h>
49757cb6dbSRafal Jaworowski
50757cb6dbSRafal Jaworowski #define I2C_ADDR_REG 0x00 /* I2C slave address register */
51757cb6dbSRafal Jaworowski #define I2C_FDR_REG 0x04 /* I2C frequency divider register */
52757cb6dbSRafal Jaworowski #define I2C_CONTROL_REG 0x08 /* I2C control register */
53757cb6dbSRafal Jaworowski #define I2C_STATUS_REG 0x0C /* I2C status register */
54757cb6dbSRafal Jaworowski #define I2C_DATA_REG 0x10 /* I2C data register */
55757cb6dbSRafal Jaworowski #define I2C_DFSRR_REG 0x14 /* I2C Digital Filter Sampling rate */
56757cb6dbSRafal Jaworowski #define I2C_ENABLE 0x80 /* Module enable - interrupt disable */
57757cb6dbSRafal Jaworowski #define I2CSR_RXAK 0x01 /* Received acknowledge */
58757cb6dbSRafal Jaworowski #define I2CSR_MCF (1<<7) /* Data transfer */
59757cb6dbSRafal Jaworowski #define I2CSR_MASS (1<<6) /* Addressed as a slave */
60757cb6dbSRafal Jaworowski #define I2CSR_MBB (1<<5) /* Bus busy */
61757cb6dbSRafal Jaworowski #define I2CSR_MAL (1<<4) /* Arbitration lost */
62757cb6dbSRafal Jaworowski #define I2CSR_SRW (1<<2) /* Slave read/write */
63757cb6dbSRafal Jaworowski #define I2CSR_MIF (1<<1) /* Module interrupt */
64757cb6dbSRafal Jaworowski #define I2CCR_MEN (1<<7) /* Module enable */
65757cb6dbSRafal Jaworowski #define I2CCR_MSTA (1<<5) /* Master/slave mode */
66757cb6dbSRafal Jaworowski #define I2CCR_MTX (1<<4) /* Transmit/receive mode */
67757cb6dbSRafal Jaworowski #define I2CCR_TXAK (1<<3) /* Transfer acknowledge */
68757cb6dbSRafal Jaworowski #define I2CCR_RSTA (1<<2) /* Repeated START */
69757cb6dbSRafal Jaworowski
70757cb6dbSRafal Jaworowski #define I2C_BAUD_RATE_FAST 0x31
71757cb6dbSRafal Jaworowski #define I2C_BAUD_RATE_DEF 0x3F
72757cb6dbSRafal Jaworowski #define I2C_DFSSR_DIV 0x10
73757cb6dbSRafal Jaworowski
74757cb6dbSRafal Jaworowski #ifdef DEBUG
75757cb6dbSRafal Jaworowski #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt,##args); } while (0)
76757cb6dbSRafal Jaworowski #else
77757cb6dbSRafal Jaworowski #define debugf(fmt, args...)
78757cb6dbSRafal Jaworowski #endif
79757cb6dbSRafal Jaworowski
80757cb6dbSRafal Jaworowski struct i2c_softc {
81757cb6dbSRafal Jaworowski device_t dev;
82757cb6dbSRafal Jaworowski device_t iicbus;
83757cb6dbSRafal Jaworowski struct resource *res;
84757cb6dbSRafal Jaworowski struct mtx mutex;
85757cb6dbSRafal Jaworowski int rid;
86757cb6dbSRafal Jaworowski bus_space_handle_t bsh;
87757cb6dbSRafal Jaworowski bus_space_tag_t bst;
88757cb6dbSRafal Jaworowski };
89757cb6dbSRafal Jaworowski
90757cb6dbSRafal Jaworowski static int i2c_probe(device_t);
91757cb6dbSRafal Jaworowski static int i2c_attach(device_t);
92757cb6dbSRafal Jaworowski
93757cb6dbSRafal Jaworowski static int i2c_repeated_start(device_t dev, u_char slave, int timeout);
94757cb6dbSRafal Jaworowski static int i2c_start(device_t dev, u_char slave, int timeout);
95757cb6dbSRafal Jaworowski static int i2c_stop(device_t dev);
96757cb6dbSRafal Jaworowski static int i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr);
97757cb6dbSRafal Jaworowski static int i2c_read(device_t dev, char *buf, int len, int *read, int last, int delay);
98757cb6dbSRafal Jaworowski static int i2c_write(device_t dev, const char *buf, int len, int *sent, int timeout);
992db664d7SJustin Hibbits static phandle_t i2c_get_node(device_t bus, device_t dev);
100757cb6dbSRafal Jaworowski
101757cb6dbSRafal Jaworowski static device_method_t i2c_methods[] = {
102757cb6dbSRafal Jaworowski DEVMETHOD(device_probe, i2c_probe),
103757cb6dbSRafal Jaworowski DEVMETHOD(device_attach, i2c_attach),
104757cb6dbSRafal Jaworowski
105757cb6dbSRafal Jaworowski DEVMETHOD(iicbus_callback, iicbus_null_callback),
106757cb6dbSRafal Jaworowski DEVMETHOD(iicbus_repeated_start, i2c_repeated_start),
107757cb6dbSRafal Jaworowski DEVMETHOD(iicbus_start, i2c_start),
108757cb6dbSRafal Jaworowski DEVMETHOD(iicbus_stop, i2c_stop),
109757cb6dbSRafal Jaworowski DEVMETHOD(iicbus_reset, i2c_reset),
110757cb6dbSRafal Jaworowski DEVMETHOD(iicbus_read, i2c_read),
111757cb6dbSRafal Jaworowski DEVMETHOD(iicbus_write, i2c_write),
112757cb6dbSRafal Jaworowski DEVMETHOD(iicbus_transfer, iicbus_transfer_gen),
1132db664d7SJustin Hibbits DEVMETHOD(ofw_bus_get_node, i2c_get_node),
114757cb6dbSRafal Jaworowski { 0, 0 }
115757cb6dbSRafal Jaworowski };
116757cb6dbSRafal Jaworowski
117757cb6dbSRafal Jaworowski static driver_t i2c_driver = {
1182db664d7SJustin Hibbits "iichb",
119757cb6dbSRafal Jaworowski i2c_methods,
120757cb6dbSRafal Jaworowski sizeof(struct i2c_softc),
121757cb6dbSRafal Jaworowski };
122757cb6dbSRafal Jaworowski
1235d7d6129SJohn Baldwin DRIVER_MODULE(i2c, simplebus, i2c_driver, 0, 0);
124676ea8e1SJohn Baldwin DRIVER_MODULE(iicbus, i2c, iicbus_driver, 0, 0);
125757cb6dbSRafal Jaworowski
126757cb6dbSRafal Jaworowski static __inline void
i2c_write_reg(struct i2c_softc * sc,bus_size_t off,uint8_t val)127757cb6dbSRafal Jaworowski i2c_write_reg(struct i2c_softc *sc, bus_size_t off, uint8_t val)
128757cb6dbSRafal Jaworowski {
129757cb6dbSRafal Jaworowski
130757cb6dbSRafal Jaworowski bus_space_write_1(sc->bst, sc->bsh, off, val);
131757cb6dbSRafal Jaworowski }
132757cb6dbSRafal Jaworowski
133757cb6dbSRafal Jaworowski static __inline uint8_t
i2c_read_reg(struct i2c_softc * sc,bus_size_t off)134757cb6dbSRafal Jaworowski i2c_read_reg(struct i2c_softc *sc, bus_size_t off)
135757cb6dbSRafal Jaworowski {
136757cb6dbSRafal Jaworowski
137757cb6dbSRafal Jaworowski return (bus_space_read_1(sc->bst, sc->bsh, off));
138757cb6dbSRafal Jaworowski }
139757cb6dbSRafal Jaworowski
140757cb6dbSRafal Jaworowski static __inline void
i2c_flag_set(struct i2c_softc * sc,bus_size_t off,uint8_t mask)141757cb6dbSRafal Jaworowski i2c_flag_set(struct i2c_softc *sc, bus_size_t off, uint8_t mask)
142757cb6dbSRafal Jaworowski {
143757cb6dbSRafal Jaworowski uint8_t status;
144757cb6dbSRafal Jaworowski
145757cb6dbSRafal Jaworowski status = i2c_read_reg(sc, off);
146757cb6dbSRafal Jaworowski status |= mask;
147757cb6dbSRafal Jaworowski i2c_write_reg(sc, off, status);
148757cb6dbSRafal Jaworowski }
149757cb6dbSRafal Jaworowski
150757cb6dbSRafal Jaworowski static int
i2c_do_wait(device_t dev,struct i2c_softc * sc,int write,int start)151757cb6dbSRafal Jaworowski i2c_do_wait(device_t dev, struct i2c_softc *sc, int write, int start)
152757cb6dbSRafal Jaworowski {
153757cb6dbSRafal Jaworowski int err;
154757cb6dbSRafal Jaworowski uint8_t status;
155757cb6dbSRafal Jaworowski
156757cb6dbSRafal Jaworowski status = i2c_read_reg(sc, I2C_STATUS_REG);
157757cb6dbSRafal Jaworowski if (status & I2CSR_MIF) {
158757cb6dbSRafal Jaworowski if (write && start && (status & I2CSR_RXAK)) {
159757cb6dbSRafal Jaworowski debugf("no ack %s", start ?
160757cb6dbSRafal Jaworowski "after sending slave address" : "");
161757cb6dbSRafal Jaworowski err = IIC_ENOACK;
162757cb6dbSRafal Jaworowski goto error;
163757cb6dbSRafal Jaworowski }
164757cb6dbSRafal Jaworowski if (status & I2CSR_MAL) {
165757cb6dbSRafal Jaworowski debugf("arbitration lost");
166757cb6dbSRafal Jaworowski err = IIC_EBUSERR;
167757cb6dbSRafal Jaworowski goto error;
168757cb6dbSRafal Jaworowski }
169757cb6dbSRafal Jaworowski if (!write && !(status & I2CSR_MCF)) {
170757cb6dbSRafal Jaworowski debugf("transfer unfinished");
171757cb6dbSRafal Jaworowski err = IIC_EBUSERR;
172757cb6dbSRafal Jaworowski goto error;
173757cb6dbSRafal Jaworowski }
174757cb6dbSRafal Jaworowski }
175757cb6dbSRafal Jaworowski
176757cb6dbSRafal Jaworowski return (IIC_NOERR);
177757cb6dbSRafal Jaworowski
178757cb6dbSRafal Jaworowski error:
179757cb6dbSRafal Jaworowski i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
180757cb6dbSRafal Jaworowski i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN | I2CCR_TXAK);
181757cb6dbSRafal Jaworowski return (err);
182757cb6dbSRafal Jaworowski }
183757cb6dbSRafal Jaworowski
184757cb6dbSRafal Jaworowski static int
i2c_probe(device_t dev)185757cb6dbSRafal Jaworowski i2c_probe(device_t dev)
186757cb6dbSRafal Jaworowski {
187757cb6dbSRafal Jaworowski struct i2c_softc *sc;
188757cb6dbSRafal Jaworowski
189d1d3233eSRafal Jaworowski if (!ofw_bus_is_compatible(dev, "fsl-i2c"))
190757cb6dbSRafal Jaworowski return (ENXIO);
191757cb6dbSRafal Jaworowski
192757cb6dbSRafal Jaworowski sc = device_get_softc(dev);
193757cb6dbSRafal Jaworowski sc->rid = 0;
194757cb6dbSRafal Jaworowski
195757cb6dbSRafal Jaworowski sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
196757cb6dbSRafal Jaworowski RF_ACTIVE);
197757cb6dbSRafal Jaworowski if (sc->res == NULL) {
198d1d3233eSRafal Jaworowski device_printf(dev, "could not allocate resources\n");
199757cb6dbSRafal Jaworowski return (ENXIO);
200757cb6dbSRafal Jaworowski }
201757cb6dbSRafal Jaworowski
202757cb6dbSRafal Jaworowski sc->bst = rman_get_bustag(sc->res);
203757cb6dbSRafal Jaworowski sc->bsh = rman_get_bushandle(sc->res);
204757cb6dbSRafal Jaworowski
205757cb6dbSRafal Jaworowski /* Enable I2C */
206757cb6dbSRafal Jaworowski i2c_write_reg(sc, I2C_CONTROL_REG, I2C_ENABLE);
207757cb6dbSRafal Jaworowski bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
208757cb6dbSRafal Jaworowski device_set_desc(dev, "I2C bus controller");
209757cb6dbSRafal Jaworowski
210757cb6dbSRafal Jaworowski return (BUS_PROBE_DEFAULT);
211757cb6dbSRafal Jaworowski }
212757cb6dbSRafal Jaworowski
213757cb6dbSRafal Jaworowski static int
i2c_attach(device_t dev)214757cb6dbSRafal Jaworowski i2c_attach(device_t dev)
215757cb6dbSRafal Jaworowski {
216757cb6dbSRafal Jaworowski struct i2c_softc *sc;
217757cb6dbSRafal Jaworowski sc = device_get_softc(dev);
218757cb6dbSRafal Jaworowski
219757cb6dbSRafal Jaworowski sc->dev = dev;
220757cb6dbSRafal Jaworowski sc->rid = 0;
221757cb6dbSRafal Jaworowski
222757cb6dbSRafal Jaworowski mtx_init(&sc->mutex, device_get_nameunit(dev), "I2C", MTX_DEF);
223757cb6dbSRafal Jaworowski
224757cb6dbSRafal Jaworowski sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
225757cb6dbSRafal Jaworowski RF_ACTIVE);
226757cb6dbSRafal Jaworowski if (sc->res == NULL) {
227757cb6dbSRafal Jaworowski device_printf(dev, "could not allocate resources");
228757cb6dbSRafal Jaworowski mtx_destroy(&sc->mutex);
229757cb6dbSRafal Jaworowski return (ENXIO);
230757cb6dbSRafal Jaworowski }
231757cb6dbSRafal Jaworowski
232757cb6dbSRafal Jaworowski sc->bst = rman_get_bustag(sc->res);
233757cb6dbSRafal Jaworowski sc->bsh = rman_get_bushandle(sc->res);
234757cb6dbSRafal Jaworowski
2355b56413dSWarner Losh sc->iicbus = device_add_child(dev, "iicbus", DEVICE_UNIT_ANY);
236757cb6dbSRafal Jaworowski if (sc->iicbus == NULL) {
237757cb6dbSRafal Jaworowski device_printf(dev, "could not add iicbus child");
238757cb6dbSRafal Jaworowski mtx_destroy(&sc->mutex);
239757cb6dbSRafal Jaworowski return (ENXIO);
240757cb6dbSRafal Jaworowski }
241757cb6dbSRafal Jaworowski
242*18250ec6SJohn Baldwin bus_attach_children(dev);
243757cb6dbSRafal Jaworowski return (IIC_NOERR);
244757cb6dbSRafal Jaworowski }
245757cb6dbSRafal Jaworowski static int
i2c_repeated_start(device_t dev,u_char slave,int timeout)246757cb6dbSRafal Jaworowski i2c_repeated_start(device_t dev, u_char slave, int timeout)
247757cb6dbSRafal Jaworowski {
248757cb6dbSRafal Jaworowski struct i2c_softc *sc;
249757cb6dbSRafal Jaworowski int error;
250757cb6dbSRafal Jaworowski
251757cb6dbSRafal Jaworowski sc = device_get_softc(dev);
252757cb6dbSRafal Jaworowski
253757cb6dbSRafal Jaworowski mtx_lock(&sc->mutex);
254757cb6dbSRafal Jaworowski /* Set repeated start condition */
255757cb6dbSRafal Jaworowski i2c_flag_set(sc, I2C_CONTROL_REG ,I2CCR_RSTA);
256757cb6dbSRafal Jaworowski /* Write target address - LSB is R/W bit */
257757cb6dbSRafal Jaworowski i2c_write_reg(sc, I2C_DATA_REG, slave);
258757cb6dbSRafal Jaworowski DELAY(1250);
259757cb6dbSRafal Jaworowski
260757cb6dbSRafal Jaworowski error = i2c_do_wait(dev, sc, 1, 1);
261757cb6dbSRafal Jaworowski mtx_unlock(&sc->mutex);
262757cb6dbSRafal Jaworowski
263757cb6dbSRafal Jaworowski if (error)
264757cb6dbSRafal Jaworowski return (error);
265757cb6dbSRafal Jaworowski
266757cb6dbSRafal Jaworowski return (IIC_NOERR);
267757cb6dbSRafal Jaworowski }
268757cb6dbSRafal Jaworowski
269757cb6dbSRafal Jaworowski static int
i2c_start(device_t dev,u_char slave,int timeout)270757cb6dbSRafal Jaworowski i2c_start(device_t dev, u_char slave, int timeout)
271757cb6dbSRafal Jaworowski {
272757cb6dbSRafal Jaworowski struct i2c_softc *sc;
273757cb6dbSRafal Jaworowski uint8_t status;
274757cb6dbSRafal Jaworowski int error;
275757cb6dbSRafal Jaworowski
276757cb6dbSRafal Jaworowski sc = device_get_softc(dev);
277757cb6dbSRafal Jaworowski DELAY(1000);
278757cb6dbSRafal Jaworowski
279757cb6dbSRafal Jaworowski mtx_lock(&sc->mutex);
280757cb6dbSRafal Jaworowski status = i2c_read_reg(sc, I2C_STATUS_REG);
281757cb6dbSRafal Jaworowski /* Check if bus is idle or busy */
282757cb6dbSRafal Jaworowski if (status & I2CSR_MBB) {
283757cb6dbSRafal Jaworowski debugf("bus busy");
284757cb6dbSRafal Jaworowski mtx_unlock(&sc->mutex);
285757cb6dbSRafal Jaworowski i2c_stop(dev);
286d1e99670SIan Lepore return (IIC_EBUSERR);
287757cb6dbSRafal Jaworowski }
288757cb6dbSRafal Jaworowski
289757cb6dbSRafal Jaworowski /* Set start condition */
290757cb6dbSRafal Jaworowski i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN | I2CCR_MSTA | I2CCR_MTX);
291757cb6dbSRafal Jaworowski /* Write target address - LSB is R/W bit */
292757cb6dbSRafal Jaworowski i2c_write_reg(sc, I2C_DATA_REG, slave);
293757cb6dbSRafal Jaworowski DELAY(1250);
294757cb6dbSRafal Jaworowski
295757cb6dbSRafal Jaworowski error = i2c_do_wait(dev, sc, 1, 1);
296757cb6dbSRafal Jaworowski
297757cb6dbSRafal Jaworowski mtx_unlock(&sc->mutex);
298757cb6dbSRafal Jaworowski if (error)
299757cb6dbSRafal Jaworowski return (error);
300757cb6dbSRafal Jaworowski
301757cb6dbSRafal Jaworowski return (IIC_NOERR);
302757cb6dbSRafal Jaworowski }
303757cb6dbSRafal Jaworowski
304757cb6dbSRafal Jaworowski static int
i2c_stop(device_t dev)305757cb6dbSRafal Jaworowski i2c_stop(device_t dev)
306757cb6dbSRafal Jaworowski {
307757cb6dbSRafal Jaworowski struct i2c_softc *sc;
308757cb6dbSRafal Jaworowski
309757cb6dbSRafal Jaworowski sc = device_get_softc(dev);
310757cb6dbSRafal Jaworowski mtx_lock(&sc->mutex);
311757cb6dbSRafal Jaworowski i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN | I2CCR_TXAK);
312757cb6dbSRafal Jaworowski DELAY(1000);
313757cb6dbSRafal Jaworowski mtx_unlock(&sc->mutex);
314757cb6dbSRafal Jaworowski
315757cb6dbSRafal Jaworowski return (IIC_NOERR);
316757cb6dbSRafal Jaworowski }
317757cb6dbSRafal Jaworowski
318757cb6dbSRafal Jaworowski static int
i2c_reset(device_t dev,u_char speed,u_char addr,u_char * oldadr)319757cb6dbSRafal Jaworowski i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldadr)
320757cb6dbSRafal Jaworowski {
321757cb6dbSRafal Jaworowski struct i2c_softc *sc;
322757cb6dbSRafal Jaworowski uint8_t baud_rate;
323757cb6dbSRafal Jaworowski
324757cb6dbSRafal Jaworowski sc = device_get_softc(dev);
325757cb6dbSRafal Jaworowski
326757cb6dbSRafal Jaworowski switch (speed) {
327757cb6dbSRafal Jaworowski case IIC_FAST:
328757cb6dbSRafal Jaworowski baud_rate = I2C_BAUD_RATE_FAST;
329757cb6dbSRafal Jaworowski break;
330757cb6dbSRafal Jaworowski case IIC_SLOW:
331757cb6dbSRafal Jaworowski case IIC_UNKNOWN:
332757cb6dbSRafal Jaworowski case IIC_FASTEST:
333757cb6dbSRafal Jaworowski default:
334757cb6dbSRafal Jaworowski baud_rate = I2C_BAUD_RATE_DEF;
335757cb6dbSRafal Jaworowski break;
336757cb6dbSRafal Jaworowski }
337757cb6dbSRafal Jaworowski
338757cb6dbSRafal Jaworowski mtx_lock(&sc->mutex);
339757cb6dbSRafal Jaworowski i2c_write_reg(sc, I2C_CONTROL_REG, 0x0);
340757cb6dbSRafal Jaworowski i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
341757cb6dbSRafal Jaworowski DELAY(1000);
342757cb6dbSRafal Jaworowski i2c_write_reg(sc, I2C_FDR_REG, baud_rate);
343757cb6dbSRafal Jaworowski i2c_write_reg(sc, I2C_DFSRR_REG, I2C_DFSSR_DIV);
344757cb6dbSRafal Jaworowski i2c_write_reg(sc, I2C_CONTROL_REG, I2C_ENABLE);
345757cb6dbSRafal Jaworowski DELAY(1000);
346757cb6dbSRafal Jaworowski mtx_unlock(&sc->mutex);
347757cb6dbSRafal Jaworowski
348757cb6dbSRafal Jaworowski return (IIC_NOERR);
349757cb6dbSRafal Jaworowski }
350757cb6dbSRafal Jaworowski
351757cb6dbSRafal Jaworowski static int
i2c_read(device_t dev,char * buf,int len,int * read,int last,int delay)352757cb6dbSRafal Jaworowski i2c_read(device_t dev, char *buf, int len, int *read, int last, int delay)
353757cb6dbSRafal Jaworowski {
354757cb6dbSRafal Jaworowski struct i2c_softc *sc;
355757cb6dbSRafal Jaworowski int error;
356757cb6dbSRafal Jaworowski
357757cb6dbSRafal Jaworowski sc = device_get_softc(dev);
358757cb6dbSRafal Jaworowski *read = 0;
359757cb6dbSRafal Jaworowski
360757cb6dbSRafal Jaworowski mtx_lock(&sc->mutex);
361757cb6dbSRafal Jaworowski if (len) {
362757cb6dbSRafal Jaworowski if (len == 1)
363757cb6dbSRafal Jaworowski i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
364757cb6dbSRafal Jaworowski I2CCR_MSTA | I2CCR_TXAK);
365757cb6dbSRafal Jaworowski
366757cb6dbSRafal Jaworowski else
367757cb6dbSRafal Jaworowski i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
368757cb6dbSRafal Jaworowski I2CCR_MSTA);
369757cb6dbSRafal Jaworowski
370757cb6dbSRafal Jaworowski /* dummy read */
371757cb6dbSRafal Jaworowski i2c_read_reg(sc, I2C_DATA_REG);
372757cb6dbSRafal Jaworowski DELAY(1000);
373757cb6dbSRafal Jaworowski }
374757cb6dbSRafal Jaworowski
375757cb6dbSRafal Jaworowski while (*read < len) {
376757cb6dbSRafal Jaworowski DELAY(1000);
377757cb6dbSRafal Jaworowski error = i2c_do_wait(dev, sc, 0, 0);
378757cb6dbSRafal Jaworowski if (error) {
379757cb6dbSRafal Jaworowski mtx_unlock(&sc->mutex);
380757cb6dbSRafal Jaworowski return (error);
381757cb6dbSRafal Jaworowski }
382757cb6dbSRafal Jaworowski if ((*read == len - 2) && last) {
383757cb6dbSRafal Jaworowski i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
384757cb6dbSRafal Jaworowski I2CCR_MSTA | I2CCR_TXAK);
385757cb6dbSRafal Jaworowski }
386757cb6dbSRafal Jaworowski
387757cb6dbSRafal Jaworowski if ((*read == len - 1) && last) {
388757cb6dbSRafal Jaworowski i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
389757cb6dbSRafal Jaworowski I2CCR_TXAK);
390757cb6dbSRafal Jaworowski }
391757cb6dbSRafal Jaworowski
392757cb6dbSRafal Jaworowski *buf++ = i2c_read_reg(sc, I2C_DATA_REG);
393757cb6dbSRafal Jaworowski (*read)++;
394757cb6dbSRafal Jaworowski DELAY(1250);
395757cb6dbSRafal Jaworowski }
396757cb6dbSRafal Jaworowski mtx_unlock(&sc->mutex);
397757cb6dbSRafal Jaworowski
398757cb6dbSRafal Jaworowski return (IIC_NOERR);
399757cb6dbSRafal Jaworowski }
400757cb6dbSRafal Jaworowski
401757cb6dbSRafal Jaworowski static int
i2c_write(device_t dev,const char * buf,int len,int * sent,int timeout)402757cb6dbSRafal Jaworowski i2c_write(device_t dev, const char *buf, int len, int *sent, int timeout)
403757cb6dbSRafal Jaworowski {
404757cb6dbSRafal Jaworowski struct i2c_softc *sc;
405757cb6dbSRafal Jaworowski int error;
406757cb6dbSRafal Jaworowski
407757cb6dbSRafal Jaworowski sc = device_get_softc(dev);
408757cb6dbSRafal Jaworowski *sent = 0;
409757cb6dbSRafal Jaworowski
410757cb6dbSRafal Jaworowski mtx_lock(&sc->mutex);
411757cb6dbSRafal Jaworowski while (*sent < len) {
412757cb6dbSRafal Jaworowski i2c_write_reg(sc, I2C_DATA_REG, *buf++);
413757cb6dbSRafal Jaworowski DELAY(1250);
414757cb6dbSRafal Jaworowski
415757cb6dbSRafal Jaworowski error = i2c_do_wait(dev, sc, 1, 0);
416757cb6dbSRafal Jaworowski if (error) {
417757cb6dbSRafal Jaworowski mtx_unlock(&sc->mutex);
418757cb6dbSRafal Jaworowski return (error);
419757cb6dbSRafal Jaworowski }
420757cb6dbSRafal Jaworowski
421757cb6dbSRafal Jaworowski (*sent)++;
422757cb6dbSRafal Jaworowski }
423757cb6dbSRafal Jaworowski mtx_unlock(&sc->mutex);
424757cb6dbSRafal Jaworowski
425757cb6dbSRafal Jaworowski return (IIC_NOERR);
426757cb6dbSRafal Jaworowski }
4272db664d7SJustin Hibbits
4282db664d7SJustin Hibbits static phandle_t
i2c_get_node(device_t bus,device_t dev)4292db664d7SJustin Hibbits i2c_get_node(device_t bus, device_t dev)
4302db664d7SJustin Hibbits {
4312db664d7SJustin Hibbits
4322db664d7SJustin Hibbits /* Share controller node with iibus device. */
4332db664d7SJustin Hibbits return (ofw_bus_get_node(bus));
4342db664d7SJustin Hibbits }
435