2 * Copyright (C) 2008 MARVELL INTERNATIONAL LTD.
5 * Developed by Semihalf.
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.
15 * 3. Neither the name of MARVELL nor the names of contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Driver for the TWSI (aka I2C, aka IIC) bus controller found on Marvell
34 * and Allwinner SoCs. Supports master operation only, and works in polling mode.
36 * Calls to DELAY() are needed per Application Note AN-179 "TWSI Software
37 * Guidelines for Discovery(TM), Horizon (TM) and Feroceon(TM) Devices".
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/module.h>
48 #include <sys/resource.h>
50 #include <machine/_inttypes.h>
51 #include <machine/bus.h>
52 #include <machine/resource.h>
57 #include <sys/mutex.h>
59 #include <dev/iicbus/iiconf.h>
60 #include <dev/iicbus/iicbus.h>
61 #include <dev/ofw/ofw_bus.h>
62 #include <dev/ofw/ofw_bus_subr.h>
64 #include <arm/mv/mvreg.h>
65 #include <arm/mv/mvvar.h>
66 #include <dev/iicbus/twsi/twsi.h>
68 #include "iicbus_if.h"
70 #define MV_TWSI_NAME "twsi"
71 #define IICBUS_DEVNAME "iicbus"
73 #define TWSI_ADDR 0x00
74 #define TWSI_DATA 0x04
75 #define TWSI_CNTR 0x08
76 #define TWSI_XADDR 0x10
77 #define TWSI_STAT 0x0c
78 #define TWSI_BAUD_RATE 0x0c
79 #define TWSI_SRST 0x1c
81 #define TWSI_BAUD_RATE_RAW(C,M,N) ((C)/((10*(M+1))<<(N+1)))
82 #define TWSI_BAUD_RATE_SLOW 50000 /* 50kHz */
83 #define TWSI_BAUD_RATE_FAST 100000 /* 100kHz */
89 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt,##args); } while (0)
91 #define debugf(fmt, args...)
94 static phandle_t mv_twsi_get_node(device_t, device_t);
95 static int mv_twsi_probe(device_t);
96 static int mv_twsi_attach(device_t);
98 static struct ofw_compat_data compat_data[] = {
99 { "mrvl,twsi", true },
100 { "marvell,mv64xxx-i2c", true },
104 static device_method_t mv_twsi_methods[] = {
105 /* device interface */
106 DEVMETHOD(device_probe, mv_twsi_probe),
107 DEVMETHOD(device_attach, mv_twsi_attach),
109 /* ofw_bus interface */
110 DEVMETHOD(ofw_bus_get_node, mv_twsi_get_node),
115 DEFINE_CLASS_1(twsi, mv_twsi_driver, mv_twsi_methods,
116 sizeof(struct twsi_softc), twsi_driver);
118 static devclass_t mv_twsi_devclass;
120 DRIVER_MODULE(twsi, simplebus, mv_twsi_driver, mv_twsi_devclass, 0, 0);
121 DRIVER_MODULE(iicbus, twsi, iicbus_driver, iicbus_devclass, 0, 0);
122 MODULE_DEPEND(twsi, iicbus, 1, 1, 1);
125 mv_twsi_get_node(device_t bus, device_t dev)
128 /* Used by ofw_iicbus. */
129 return (ofw_bus_get_node(bus));
133 mv_twsi_probe(device_t dev)
135 struct twsi_softc *sc;
137 sc = device_get_softc(dev);
138 if (!ofw_bus_status_okay(dev))
141 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
144 sc->reg_data = TWSI_DATA;
145 sc->reg_slave_addr = TWSI_ADDR;
146 sc->reg_slave_ext_addr = TWSI_XADDR;
147 sc->reg_control = TWSI_CNTR;
148 sc->reg_status = TWSI_STAT;
149 sc->reg_baud_rate = TWSI_BAUD_RATE;
150 sc->reg_soft_reset = TWSI_SRST;
152 device_set_desc(dev, "Marvell Integrated I2C Bus Controller");
153 return (BUS_PROBE_DEFAULT);
156 #define ABSSUB(a,b) (((a) > (b)) ? (a) - (b) : (b) - (a))
158 mv_twsi_cal_baud_rate(const uint32_t target, struct twsi_baud_rate *rate)
160 uint32_t clk, cur, diff, diff0;
163 /* Calculate baud rate. */
164 m0 = n0 = 4; /* Default values on reset */
168 for (n = 0; n < 8; n++) {
169 for (m = 0; m < 16; m++) {
170 cur = TWSI_BAUD_RATE_RAW(clk,m,n);
171 diff = ABSSUB(target, cur);
179 rate->raw = TWSI_BAUD_RATE_RAW(clk, m0, n0);
180 rate->param = TWSI_BAUD_RATE_PARAM(m0, n0);
186 mv_twsi_attach(device_t dev)
188 struct twsi_softc *sc;
189 phandle_t child, iicbusnode;
191 struct iicbus_ivar *devi;
192 char dname[32]; /* 32 is taken from struct u_device */
196 sc = device_get_softc(dev);
198 mv_twsi_cal_baud_rate(TWSI_BAUD_RATE_SLOW, &sc->baud_rate[IIC_SLOW]);
199 mv_twsi_cal_baud_rate(TWSI_BAUD_RATE_FAST, &sc->baud_rate[IIC_FAST]);
201 device_printf(dev, "calculated baud rates are:\n"
202 " %" PRIu32 " kHz (M=%d, N=%d) for slow,\n"
203 " %" PRIu32 " kHz (M=%d, N=%d) for fast.\n",
204 sc->baud_rate[IIC_SLOW].raw / 1000,
205 sc->baud_rate[IIC_SLOW].m,
206 sc->baud_rate[IIC_SLOW].n,
207 sc->baud_rate[IIC_FAST].raw / 1000,
208 sc->baud_rate[IIC_FAST].m,
209 sc->baud_rate[IIC_FAST].n);
212 ret = twsi_attach(dev);
217 /* Find iicbus as the child devices in the device tree. */
218 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
219 child = OF_peer(child)) {
220 len = OF_getproplen(child, "model");
221 if (len <= 0 || len > sizeof(dname))
223 error = OF_getprop(child, "model", &dname, len);
227 if (len == strlen(IICBUS_DEVNAME) &&
228 strncasecmp(dname, IICBUS_DEVNAME, len) == 0) {
236 /* Attach child devices onto iicbus. */
237 for (child = OF_child(iicbusnode); child != 0; child = OF_peer(child)) {
238 /* Get slave address. */
239 error = OF_getencprop(child, "i2c-address", &paddr, sizeof(paddr));
241 error = OF_getencprop(child, "reg", &paddr, sizeof(paddr));
245 /* Get device driver name. */
246 len = OF_getproplen(child, "model");
247 if (len <= 0 || len > sizeof(dname))
249 OF_getprop(child, "model", &dname, len);
252 device_printf(dev, "adding a device %s at %d.\n",
254 childdev = BUS_ADD_CHILD(sc->iicbus, 0, dname, -1);
255 devi = IICBUS_IVAR(childdev);
260 bus_generic_attach(sc->iicbus);