2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2008-2009 Semihalf, Michal Hajduk
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/resource.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
44 #include <sys/mutex.h>
46 #include <dev/iicbus/iiconf.h>
47 #include <dev/iicbus/iicbus.h>
48 #include "iicbus_if.h"
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
53 #define I2C_ADDR_REG 0x00 /* I2C slave address register */
54 #define I2C_FDR_REG 0x04 /* I2C frequency divider register */
55 #define I2C_CONTROL_REG 0x08 /* I2C control register */
56 #define I2C_STATUS_REG 0x0C /* I2C status register */
57 #define I2C_DATA_REG 0x10 /* I2C data register */
58 #define I2C_DFSRR_REG 0x14 /* I2C Digital Filter Sampling rate */
59 #define I2C_ENABLE 0x80 /* Module enable - interrupt disable */
60 #define I2CSR_RXAK 0x01 /* Received acknowledge */
61 #define I2CSR_MCF (1<<7) /* Data transfer */
62 #define I2CSR_MASS (1<<6) /* Addressed as a slave */
63 #define I2CSR_MBB (1<<5) /* Bus busy */
64 #define I2CSR_MAL (1<<4) /* Arbitration lost */
65 #define I2CSR_SRW (1<<2) /* Slave read/write */
66 #define I2CSR_MIF (1<<1) /* Module interrupt */
67 #define I2CCR_MEN (1<<7) /* Module enable */
68 #define I2CCR_MSTA (1<<5) /* Master/slave mode */
69 #define I2CCR_MTX (1<<4) /* Transmit/receive mode */
70 #define I2CCR_TXAK (1<<3) /* Transfer acknowledge */
71 #define I2CCR_RSTA (1<<2) /* Repeated START */
73 #define I2C_BAUD_RATE_FAST 0x31
74 #define I2C_BAUD_RATE_DEF 0x3F
75 #define I2C_DFSSR_DIV 0x10
78 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt,##args); } while (0)
80 #define debugf(fmt, args...)
89 bus_space_handle_t bsh;
93 static int i2c_probe(device_t);
94 static int i2c_attach(device_t);
96 static int i2c_repeated_start(device_t dev, u_char slave, int timeout);
97 static int i2c_start(device_t dev, u_char slave, int timeout);
98 static int i2c_stop(device_t dev);
99 static int i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr);
100 static int i2c_read(device_t dev, char *buf, int len, int *read, int last, int delay);
101 static int i2c_write(device_t dev, const char *buf, int len, int *sent, int timeout);
102 static phandle_t i2c_get_node(device_t bus, device_t dev);
104 static device_method_t i2c_methods[] = {
105 DEVMETHOD(device_probe, i2c_probe),
106 DEVMETHOD(device_attach, i2c_attach),
108 DEVMETHOD(iicbus_callback, iicbus_null_callback),
109 DEVMETHOD(iicbus_repeated_start, i2c_repeated_start),
110 DEVMETHOD(iicbus_start, i2c_start),
111 DEVMETHOD(iicbus_stop, i2c_stop),
112 DEVMETHOD(iicbus_reset, i2c_reset),
113 DEVMETHOD(iicbus_read, i2c_read),
114 DEVMETHOD(iicbus_write, i2c_write),
115 DEVMETHOD(iicbus_transfer, iicbus_transfer_gen),
116 DEVMETHOD(ofw_bus_get_node, i2c_get_node),
120 static driver_t i2c_driver = {
123 sizeof(struct i2c_softc),
126 DRIVER_MODULE(i2c, simplebus, i2c_driver, 0, 0);
127 DRIVER_MODULE(iicbus, i2c, iicbus_driver, 0, 0);
130 i2c_write_reg(struct i2c_softc *sc, bus_size_t off, uint8_t val)
133 bus_space_write_1(sc->bst, sc->bsh, off, val);
136 static __inline uint8_t
137 i2c_read_reg(struct i2c_softc *sc, bus_size_t off)
140 return (bus_space_read_1(sc->bst, sc->bsh, off));
144 i2c_flag_set(struct i2c_softc *sc, bus_size_t off, uint8_t mask)
148 status = i2c_read_reg(sc, off);
150 i2c_write_reg(sc, off, status);
154 i2c_do_wait(device_t dev, struct i2c_softc *sc, int write, int start)
159 status = i2c_read_reg(sc, I2C_STATUS_REG);
160 if (status & I2CSR_MIF) {
161 if (write && start && (status & I2CSR_RXAK)) {
162 debugf("no ack %s", start ?
163 "after sending slave address" : "");
167 if (status & I2CSR_MAL) {
168 debugf("arbitration lost");
172 if (!write && !(status & I2CSR_MCF)) {
173 debugf("transfer unfinished");
182 i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
183 i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN | I2CCR_TXAK);
188 i2c_probe(device_t dev)
190 struct i2c_softc *sc;
192 if (!ofw_bus_is_compatible(dev, "fsl-i2c"))
195 sc = device_get_softc(dev);
198 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
200 if (sc->res == NULL) {
201 device_printf(dev, "could not allocate resources\n");
205 sc->bst = rman_get_bustag(sc->res);
206 sc->bsh = rman_get_bushandle(sc->res);
209 i2c_write_reg(sc, I2C_CONTROL_REG, I2C_ENABLE);
210 bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
211 device_set_desc(dev, "I2C bus controller");
213 return (BUS_PROBE_DEFAULT);
217 i2c_attach(device_t dev)
219 struct i2c_softc *sc;
220 sc = device_get_softc(dev);
225 mtx_init(&sc->mutex, device_get_nameunit(dev), "I2C", MTX_DEF);
227 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
229 if (sc->res == NULL) {
230 device_printf(dev, "could not allocate resources");
231 mtx_destroy(&sc->mutex);
235 sc->bst = rman_get_bustag(sc->res);
236 sc->bsh = rman_get_bushandle(sc->res);
238 sc->iicbus = device_add_child(dev, "iicbus", -1);
239 if (sc->iicbus == NULL) {
240 device_printf(dev, "could not add iicbus child");
241 mtx_destroy(&sc->mutex);
245 bus_generic_attach(dev);
249 i2c_repeated_start(device_t dev, u_char slave, int timeout)
251 struct i2c_softc *sc;
254 sc = device_get_softc(dev);
256 mtx_lock(&sc->mutex);
257 /* Set repeated start condition */
258 i2c_flag_set(sc, I2C_CONTROL_REG ,I2CCR_RSTA);
259 /* Write target address - LSB is R/W bit */
260 i2c_write_reg(sc, I2C_DATA_REG, slave);
263 error = i2c_do_wait(dev, sc, 1, 1);
264 mtx_unlock(&sc->mutex);
273 i2c_start(device_t dev, u_char slave, int timeout)
275 struct i2c_softc *sc;
279 sc = device_get_softc(dev);
282 mtx_lock(&sc->mutex);
283 status = i2c_read_reg(sc, I2C_STATUS_REG);
284 /* Check if bus is idle or busy */
285 if (status & I2CSR_MBB) {
287 mtx_unlock(&sc->mutex);
289 return (IIC_EBUSERR);
292 /* Set start condition */
293 i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN | I2CCR_MSTA | I2CCR_MTX);
294 /* Write target address - LSB is R/W bit */
295 i2c_write_reg(sc, I2C_DATA_REG, slave);
298 error = i2c_do_wait(dev, sc, 1, 1);
300 mtx_unlock(&sc->mutex);
308 i2c_stop(device_t dev)
310 struct i2c_softc *sc;
312 sc = device_get_softc(dev);
313 mtx_lock(&sc->mutex);
314 i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN | I2CCR_TXAK);
316 mtx_unlock(&sc->mutex);
322 i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldadr)
324 struct i2c_softc *sc;
327 sc = device_get_softc(dev);
331 baud_rate = I2C_BAUD_RATE_FAST;
337 baud_rate = I2C_BAUD_RATE_DEF;
341 mtx_lock(&sc->mutex);
342 i2c_write_reg(sc, I2C_CONTROL_REG, 0x0);
343 i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
345 i2c_write_reg(sc, I2C_FDR_REG, baud_rate);
346 i2c_write_reg(sc, I2C_DFSRR_REG, I2C_DFSSR_DIV);
347 i2c_write_reg(sc, I2C_CONTROL_REG, I2C_ENABLE);
349 mtx_unlock(&sc->mutex);
355 i2c_read(device_t dev, char *buf, int len, int *read, int last, int delay)
357 struct i2c_softc *sc;
360 sc = device_get_softc(dev);
363 mtx_lock(&sc->mutex);
366 i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
367 I2CCR_MSTA | I2CCR_TXAK);
370 i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
374 i2c_read_reg(sc, I2C_DATA_REG);
378 while (*read < len) {
380 error = i2c_do_wait(dev, sc, 0, 0);
382 mtx_unlock(&sc->mutex);
385 if ((*read == len - 2) && last) {
386 i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
387 I2CCR_MSTA | I2CCR_TXAK);
390 if ((*read == len - 1) && last) {
391 i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
395 *buf++ = i2c_read_reg(sc, I2C_DATA_REG);
399 mtx_unlock(&sc->mutex);
405 i2c_write(device_t dev, const char *buf, int len, int *sent, int timeout)
407 struct i2c_softc *sc;
410 sc = device_get_softc(dev);
413 mtx_lock(&sc->mutex);
414 while (*sent < len) {
415 i2c_write_reg(sc, I2C_DATA_REG, *buf++);
418 error = i2c_do_wait(dev, sc, 1, 0);
420 mtx_unlock(&sc->mutex);
426 mtx_unlock(&sc->mutex);
432 i2c_get_node(device_t bus, device_t dev)
435 /* Share controller node with iibus device. */
436 return (ofw_bus_get_node(bus));