mv_twsi.c revision 295630
1/*-
2 * Copyright (C) 2008 MARVELL INTERNATIONAL LTD.
3 * All rights reserved.
4 *
5 * Developed by Semihalf.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32/*
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.
35 *
36 * Calls to DELAY() are needed per Application Note AN-179 "TWSI Software
37 * Guidelines for Discovery(TM), Horizon (TM) and Feroceon(TM) Devices".
38 */
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: head/sys/dev/iicbus/twsi/mv_twsi.c 295630 2016-02-15 17:14:10Z andrew $");
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/bus.h>
46#include <sys/kernel.h>
47#include <sys/module.h>
48#include <sys/resource.h>
49
50#include <machine/_inttypes.h>
51#include <machine/bus.h>
52#include <machine/resource.h>
53
54#include <sys/rman.h>
55
56#include <sys/lock.h>
57#include <sys/mutex.h>
58
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>
63
64#include <arm/mv/mvreg.h>
65#include <arm/mv/mvvar.h>
66#include <dev/iicbus/twsi/twsi.h>
67
68#include "iicbus_if.h"
69
70#define MV_TWSI_NAME		"twsi"
71#define	IICBUS_DEVNAME		"iicbus"
72
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
80
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 */
84
85#define TWSI_DEBUG
86#undef TWSI_DEBUG
87
88#ifdef  TWSI_DEBUG
89#define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt,##args); } while (0)
90#else
91#define debugf(fmt, args...)
92#endif
93
94static int mv_twsi_probe(device_t);
95static int mv_twsi_attach(device_t);
96
97static struct ofw_compat_data compat_data[] = {
98	{ "mrvl,twsi",			true },
99	{ "marvell,mv64xxx-i2c",	true },
100	{ NULL,				false }
101};
102
103static device_method_t mv_twsi_methods[] = {
104	/* device interface */
105	DEVMETHOD(device_probe,		mv_twsi_probe),
106	DEVMETHOD(device_attach,	mv_twsi_attach),
107
108	{ 0, 0 }
109};
110
111DEFINE_CLASS_1(twsi, mv_twsi_driver, mv_twsi_methods,
112    sizeof(struct twsi_softc), twsi_driver);
113
114static devclass_t mv_twsi_devclass;
115
116DRIVER_MODULE(twsi, simplebus, mv_twsi_driver, mv_twsi_devclass, 0, 0);
117DRIVER_MODULE(iicbus, twsi, iicbus_driver, iicbus_devclass, 0, 0);
118MODULE_DEPEND(twsi, iicbus, 1, 1, 1);
119
120static int
121mv_twsi_probe(device_t dev)
122{
123	struct twsi_softc *sc;
124
125	sc = device_get_softc(dev);
126	if (!ofw_bus_status_okay(dev))
127		return (ENXIO);
128
129	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
130		return (ENXIO);
131
132	sc->reg_data = TWSI_DATA;
133	sc->reg_slave_addr = TWSI_ADDR;
134	sc->reg_slave_ext_addr = TWSI_XADDR;
135	sc->reg_control = TWSI_CNTR;
136	sc->reg_status = TWSI_STAT;
137	sc->reg_baud_rate = TWSI_BAUD_RATE;
138	sc->reg_soft_reset = TWSI_SRST;
139
140	device_set_desc(dev, "Marvell Integrated I2C Bus Controller");
141	return (BUS_PROBE_DEFAULT);
142}
143
144#define	ABSSUB(a,b)	(((a) > (b)) ? (a) - (b) : (b) - (a))
145static void
146mv_twsi_cal_baud_rate(const uint32_t target, struct twsi_baud_rate *rate)
147{
148	uint32_t clk, cur, diff, diff0;
149	int m, n, m0, n0;
150
151	/* Calculate baud rate. */
152	m0 = n0 = 4;	/* Default values on reset */
153	diff0 = 0xffffffff;
154	clk = get_tclk();
155
156	for (n = 0; n < 8; n++) {
157		for (m = 0; m < 16; m++) {
158			cur = TWSI_BAUD_RATE_RAW(clk,m,n);
159			diff = ABSSUB(target, cur);
160			if (diff < diff0) {
161				m0 = m;
162				n0 = n;
163				diff0 = diff;
164			}
165		}
166	}
167	rate->raw = TWSI_BAUD_RATE_RAW(clk, m0, n0);
168	rate->param = TWSI_BAUD_RATE_PARAM(m0, n0);
169	rate->m = m0;
170	rate->n = n0;
171}
172
173static int
174mv_twsi_attach(device_t dev)
175{
176	struct twsi_softc *sc;
177	phandle_t child, iicbusnode;
178	device_t childdev;
179	struct iicbus_ivar *devi;
180	char dname[32];	/* 32 is taken from struct u_device */
181	uint32_t paddr;
182	int len, error, ret;
183
184	sc = device_get_softc(dev);
185
186	mv_twsi_cal_baud_rate(TWSI_BAUD_RATE_SLOW, &sc->baud_rate[IIC_SLOW]);
187	mv_twsi_cal_baud_rate(TWSI_BAUD_RATE_FAST, &sc->baud_rate[IIC_FAST]);
188	if (bootverbose)
189		device_printf(dev, "calculated baud rates are:\n"
190		    " %" PRIu32 " kHz (M=%d, N=%d) for slow,\n"
191		    " %" PRIu32 " kHz (M=%d, N=%d) for fast.\n",
192		    sc->baud_rate[IIC_SLOW].raw / 1000,
193		    sc->baud_rate[IIC_SLOW].m,
194		    sc->baud_rate[IIC_SLOW].n,
195		    sc->baud_rate[IIC_FAST].raw / 1000,
196		    sc->baud_rate[IIC_FAST].m,
197		    sc->baud_rate[IIC_FAST].n);
198
199
200	ret = twsi_attach(dev);
201	if (ret != 0)
202		return (ret);
203
204	iicbusnode = 0;
205	/* Find iicbus as the child devices in the device tree. */
206	for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
207	    child = OF_peer(child)) {
208		len = OF_getproplen(child, "model");
209		if (len <= 0 || len > sizeof(dname))
210			continue;
211		error = OF_getprop(child, "model", &dname, len);
212		if (error == -1)
213			continue;
214		len = strlen(dname);
215		if (len == strlen(IICBUS_DEVNAME) &&
216		    strncasecmp(dname, IICBUS_DEVNAME, len) == 0) {
217			iicbusnode = child;
218			break;
219		}
220	}
221	if (iicbusnode == 0)
222		goto attach_end;
223
224	/* Attach child devices onto iicbus. */
225	for (child = OF_child(iicbusnode); child != 0; child = OF_peer(child)) {
226		/* Get slave address. */
227		error = OF_getencprop(child, "i2c-address", &paddr, sizeof(paddr));
228		if (error == -1)
229			error = OF_getencprop(child, "reg", &paddr, sizeof(paddr));
230		if (error == -1)
231			continue;
232
233		/* Get device driver name. */
234		len = OF_getproplen(child, "model");
235		if (len <= 0 || len > sizeof(dname))
236			continue;
237		OF_getprop(child, "model", &dname, len);
238
239		if (bootverbose)
240			device_printf(dev, "adding a device %s at %d.\n",
241			    dname, paddr);
242		childdev = BUS_ADD_CHILD(sc->iicbus, 0, dname, -1);
243		devi = IICBUS_IVAR(childdev);
244		devi->addr = paddr;
245	}
246
247attach_end:
248	bus_generic_attach(sc->iicbus);
249
250	return (0);
251}
252