xref: /freebsd/sys/dev/iicbus/controller/twsi/mv_twsi.c (revision 580d00f42fdd94ce43583cc45fe3f1d9fdff47d4)
1*580d00f4SEmmanuel Vadot /*-
2*580d00f4SEmmanuel Vadot  * Copyright (C) 2008 MARVELL INTERNATIONAL LTD.
3*580d00f4SEmmanuel Vadot  * All rights reserved.
4*580d00f4SEmmanuel Vadot  *
5*580d00f4SEmmanuel Vadot  * Developed by Semihalf.
6*580d00f4SEmmanuel Vadot  *
7*580d00f4SEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
8*580d00f4SEmmanuel Vadot  * modification, are permitted provided that the following conditions
9*580d00f4SEmmanuel Vadot  * are met:
10*580d00f4SEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
11*580d00f4SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
12*580d00f4SEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
13*580d00f4SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
14*580d00f4SEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
15*580d00f4SEmmanuel Vadot  * 3. Neither the name of MARVELL nor the names of contributors
16*580d00f4SEmmanuel Vadot  *    may be used to endorse or promote products derived from this software
17*580d00f4SEmmanuel Vadot  *    without specific prior written permission.
18*580d00f4SEmmanuel Vadot  *
19*580d00f4SEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20*580d00f4SEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*580d00f4SEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*580d00f4SEmmanuel Vadot  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23*580d00f4SEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*580d00f4SEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*580d00f4SEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*580d00f4SEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*580d00f4SEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*580d00f4SEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*580d00f4SEmmanuel Vadot  * SUCH DAMAGE.
30*580d00f4SEmmanuel Vadot  */
31*580d00f4SEmmanuel Vadot 
32*580d00f4SEmmanuel Vadot /*
33*580d00f4SEmmanuel Vadot  * Driver for the TWSI (aka I2C, aka IIC) bus controller found on Marvell
34*580d00f4SEmmanuel Vadot  * and Allwinner SoCs. Supports master operation only, and works in polling mode.
35*580d00f4SEmmanuel Vadot  *
36*580d00f4SEmmanuel Vadot  * Calls to DELAY() are needed per Application Note AN-179 "TWSI Software
37*580d00f4SEmmanuel Vadot  * Guidelines for Discovery(TM), Horizon (TM) and Feroceon(TM) Devices".
38*580d00f4SEmmanuel Vadot  */
39*580d00f4SEmmanuel Vadot 
40*580d00f4SEmmanuel Vadot #include <sys/cdefs.h>
41*580d00f4SEmmanuel Vadot #include <sys/param.h>
42*580d00f4SEmmanuel Vadot #include <sys/systm.h>
43*580d00f4SEmmanuel Vadot #include <sys/bus.h>
44*580d00f4SEmmanuel Vadot #include <sys/kernel.h>
45*580d00f4SEmmanuel Vadot #include <sys/module.h>
46*580d00f4SEmmanuel Vadot #include <sys/resource.h>
47*580d00f4SEmmanuel Vadot 
48*580d00f4SEmmanuel Vadot #include <machine/_inttypes.h>
49*580d00f4SEmmanuel Vadot #include <machine/bus.h>
50*580d00f4SEmmanuel Vadot #include <machine/resource.h>
51*580d00f4SEmmanuel Vadot 
52*580d00f4SEmmanuel Vadot #include <sys/rman.h>
53*580d00f4SEmmanuel Vadot 
54*580d00f4SEmmanuel Vadot #include <sys/lock.h>
55*580d00f4SEmmanuel Vadot #include <sys/mutex.h>
56*580d00f4SEmmanuel Vadot 
57*580d00f4SEmmanuel Vadot #include <dev/iicbus/iiconf.h>
58*580d00f4SEmmanuel Vadot #include <dev/iicbus/iicbus.h>
59*580d00f4SEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
60*580d00f4SEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
61*580d00f4SEmmanuel Vadot 
62*580d00f4SEmmanuel Vadot #include <dev/extres/clk/clk.h>
63*580d00f4SEmmanuel Vadot 
64*580d00f4SEmmanuel Vadot #include <arm/mv/mvreg.h>
65*580d00f4SEmmanuel Vadot #include <arm/mv/mvvar.h>
66*580d00f4SEmmanuel Vadot #include <dev/iicbus/controller/twsi/twsi.h>
67*580d00f4SEmmanuel Vadot 
68*580d00f4SEmmanuel Vadot #include "iicbus_if.h"
69*580d00f4SEmmanuel Vadot 
70*580d00f4SEmmanuel Vadot #define MV_TWSI_NAME		"twsi"
71*580d00f4SEmmanuel Vadot #define	IICBUS_DEVNAME		"iicbus"
72*580d00f4SEmmanuel Vadot 
73*580d00f4SEmmanuel Vadot #define TWSI_ADDR	0x00
74*580d00f4SEmmanuel Vadot #define TWSI_DATA	0x04
75*580d00f4SEmmanuel Vadot #define TWSI_CNTR	0x08
76*580d00f4SEmmanuel Vadot #define TWSI_XADDR	0x10
77*580d00f4SEmmanuel Vadot #define TWSI_STAT	0x0c
78*580d00f4SEmmanuel Vadot #define TWSI_BAUD_RATE	0x0c
79*580d00f4SEmmanuel Vadot #define TWSI_SRST	0x1c
80*580d00f4SEmmanuel Vadot 
81*580d00f4SEmmanuel Vadot #define	TWSI_BAUD_RATE_RAW(C,M,N)	((C)/((10*(M+1))<<(N+1)))
82*580d00f4SEmmanuel Vadot #define	TWSI_BAUD_RATE_SLOW		50000	/* 50kHz */
83*580d00f4SEmmanuel Vadot #define	TWSI_BAUD_RATE_FAST		100000	/* 100kHz */
84*580d00f4SEmmanuel Vadot 
85*580d00f4SEmmanuel Vadot #define TWSI_DEBUG
86*580d00f4SEmmanuel Vadot #undef TWSI_DEBUG
87*580d00f4SEmmanuel Vadot 
88*580d00f4SEmmanuel Vadot #ifdef  TWSI_DEBUG
89*580d00f4SEmmanuel Vadot #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt,##args); } while (0)
90*580d00f4SEmmanuel Vadot #else
91*580d00f4SEmmanuel Vadot #define debugf(fmt, args...)
92*580d00f4SEmmanuel Vadot #endif
93*580d00f4SEmmanuel Vadot 
94*580d00f4SEmmanuel Vadot static phandle_t mv_twsi_get_node(device_t, device_t);
95*580d00f4SEmmanuel Vadot static int mv_twsi_probe(device_t);
96*580d00f4SEmmanuel Vadot static int mv_twsi_attach(device_t);
97*580d00f4SEmmanuel Vadot 
98*580d00f4SEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
99*580d00f4SEmmanuel Vadot 	{ "mrvl,twsi",			true },
100*580d00f4SEmmanuel Vadot 	{ "marvell,mv64xxx-i2c",	true },
101*580d00f4SEmmanuel Vadot 	{ "marvell,mv78230-i2c",	true },
102*580d00f4SEmmanuel Vadot 	{ NULL,				false }
103*580d00f4SEmmanuel Vadot };
104*580d00f4SEmmanuel Vadot 
105*580d00f4SEmmanuel Vadot static device_method_t mv_twsi_methods[] = {
106*580d00f4SEmmanuel Vadot 	/* device interface */
107*580d00f4SEmmanuel Vadot 	DEVMETHOD(device_probe,		mv_twsi_probe),
108*580d00f4SEmmanuel Vadot 	DEVMETHOD(device_attach,	mv_twsi_attach),
109*580d00f4SEmmanuel Vadot 
110*580d00f4SEmmanuel Vadot 	/* ofw_bus interface */
111*580d00f4SEmmanuel Vadot 	DEVMETHOD(ofw_bus_get_node,	mv_twsi_get_node),
112*580d00f4SEmmanuel Vadot 
113*580d00f4SEmmanuel Vadot 	DEVMETHOD_END
114*580d00f4SEmmanuel Vadot };
115*580d00f4SEmmanuel Vadot 
116*580d00f4SEmmanuel Vadot DEFINE_CLASS_1(twsi, mv_twsi_driver, mv_twsi_methods,
117*580d00f4SEmmanuel Vadot     sizeof(struct twsi_softc), twsi_driver);
118*580d00f4SEmmanuel Vadot 
119*580d00f4SEmmanuel Vadot DRIVER_MODULE(twsi, simplebus, mv_twsi_driver, 0, 0);
120*580d00f4SEmmanuel Vadot DRIVER_MODULE(iicbus, twsi, iicbus_driver, 0, 0);
121*580d00f4SEmmanuel Vadot MODULE_DEPEND(twsi, iicbus, 1, 1, 1);
122*580d00f4SEmmanuel Vadot SIMPLEBUS_PNP_INFO(compat_data);
123*580d00f4SEmmanuel Vadot 
124*580d00f4SEmmanuel Vadot static phandle_t
125*580d00f4SEmmanuel Vadot mv_twsi_get_node(device_t bus, device_t dev)
126*580d00f4SEmmanuel Vadot {
127*580d00f4SEmmanuel Vadot 
128*580d00f4SEmmanuel Vadot 	/* Used by ofw_iicbus. */
129*580d00f4SEmmanuel Vadot 	return (ofw_bus_get_node(bus));
130*580d00f4SEmmanuel Vadot }
131*580d00f4SEmmanuel Vadot 
132*580d00f4SEmmanuel Vadot static int
133*580d00f4SEmmanuel Vadot mv_twsi_probe(device_t dev)
134*580d00f4SEmmanuel Vadot {
135*580d00f4SEmmanuel Vadot 
136*580d00f4SEmmanuel Vadot 	if (!ofw_bus_status_okay(dev))
137*580d00f4SEmmanuel Vadot 		return (ENXIO);
138*580d00f4SEmmanuel Vadot 
139*580d00f4SEmmanuel Vadot 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
140*580d00f4SEmmanuel Vadot 		return (ENXIO);
141*580d00f4SEmmanuel Vadot 
142*580d00f4SEmmanuel Vadot 	device_set_desc(dev, "Marvell Integrated I2C Bus Controller");
143*580d00f4SEmmanuel Vadot 	return (BUS_PROBE_DEFAULT);
144*580d00f4SEmmanuel Vadot }
145*580d00f4SEmmanuel Vadot 
146*580d00f4SEmmanuel Vadot #define	ABSSUB(a,b)	(((a) > (b)) ? (a) - (b) : (b) - (a))
147*580d00f4SEmmanuel Vadot static void
148*580d00f4SEmmanuel Vadot mv_twsi_cal_baud_rate(struct twsi_softc *sc, const uint32_t target,
149*580d00f4SEmmanuel Vadot     struct twsi_baud_rate *rate)
150*580d00f4SEmmanuel Vadot {
151*580d00f4SEmmanuel Vadot 	uint64_t clk;
152*580d00f4SEmmanuel Vadot 	uint32_t cur, diff, diff0;
153*580d00f4SEmmanuel Vadot 	int m, n, m0, n0;
154*580d00f4SEmmanuel Vadot 
155*580d00f4SEmmanuel Vadot 	/* Calculate baud rate. */
156*580d00f4SEmmanuel Vadot 	m0 = n0 = 4;	/* Default values on reset */
157*580d00f4SEmmanuel Vadot 	diff0 = 0xffffffff;
158*580d00f4SEmmanuel Vadot 	clk_get_freq(sc->clk_core, &clk);
159*580d00f4SEmmanuel Vadot 
160*580d00f4SEmmanuel Vadot 	for (n = 0; n < 8; n++) {
161*580d00f4SEmmanuel Vadot 		for (m = 0; m < 16; m++) {
162*580d00f4SEmmanuel Vadot 			cur = TWSI_BAUD_RATE_RAW(clk,m,n);
163*580d00f4SEmmanuel Vadot 			diff = ABSSUB(target, cur);
164*580d00f4SEmmanuel Vadot 			if (diff < diff0) {
165*580d00f4SEmmanuel Vadot 				m0 = m;
166*580d00f4SEmmanuel Vadot 				n0 = n;
167*580d00f4SEmmanuel Vadot 				diff0 = diff;
168*580d00f4SEmmanuel Vadot 			}
169*580d00f4SEmmanuel Vadot 		}
170*580d00f4SEmmanuel Vadot 	}
171*580d00f4SEmmanuel Vadot 	rate->raw = TWSI_BAUD_RATE_RAW(clk, m0, n0);
172*580d00f4SEmmanuel Vadot 	rate->param = TWSI_BAUD_RATE_PARAM(m0, n0);
173*580d00f4SEmmanuel Vadot 	rate->m = m0;
174*580d00f4SEmmanuel Vadot 	rate->n = n0;
175*580d00f4SEmmanuel Vadot }
176*580d00f4SEmmanuel Vadot 
177*580d00f4SEmmanuel Vadot static int
178*580d00f4SEmmanuel Vadot mv_twsi_attach(device_t dev)
179*580d00f4SEmmanuel Vadot {
180*580d00f4SEmmanuel Vadot 	struct twsi_softc *sc;
181*580d00f4SEmmanuel Vadot 	int error;
182*580d00f4SEmmanuel Vadot 
183*580d00f4SEmmanuel Vadot 	sc = device_get_softc(dev);
184*580d00f4SEmmanuel Vadot 	sc->dev = dev;
185*580d00f4SEmmanuel Vadot 
186*580d00f4SEmmanuel Vadot 	/* Activate clock */
187*580d00f4SEmmanuel Vadot 	error = clk_get_by_ofw_index(dev, 0, 0, &sc->clk_core);
188*580d00f4SEmmanuel Vadot 	if (error != 0) {
189*580d00f4SEmmanuel Vadot 		device_printf(dev, "could not find core clock\n");
190*580d00f4SEmmanuel Vadot 		return (error);
191*580d00f4SEmmanuel Vadot 	}
192*580d00f4SEmmanuel Vadot 	error = clk_enable(sc->clk_core);
193*580d00f4SEmmanuel Vadot 	if (error != 0) {
194*580d00f4SEmmanuel Vadot 		device_printf(dev, "could not enable core clock\n");
195*580d00f4SEmmanuel Vadot 		return (error);
196*580d00f4SEmmanuel Vadot 	}
197*580d00f4SEmmanuel Vadot 
198*580d00f4SEmmanuel Vadot 	if (clk_get_by_ofw_index(dev, 0, 1, &sc->clk_reg) == 0) {
199*580d00f4SEmmanuel Vadot 		error = clk_enable(sc->clk_reg);
200*580d00f4SEmmanuel Vadot 		if (error != 0) {
201*580d00f4SEmmanuel Vadot 			device_printf(dev, "could not enable core clock\n");
202*580d00f4SEmmanuel Vadot 			return (error);
203*580d00f4SEmmanuel Vadot 		}
204*580d00f4SEmmanuel Vadot 	}
205*580d00f4SEmmanuel Vadot 
206*580d00f4SEmmanuel Vadot 	mv_twsi_cal_baud_rate(sc, TWSI_BAUD_RATE_SLOW, &sc->baud_rate[IIC_SLOW]);
207*580d00f4SEmmanuel Vadot 	mv_twsi_cal_baud_rate(sc, TWSI_BAUD_RATE_FAST, &sc->baud_rate[IIC_FAST]);
208*580d00f4SEmmanuel Vadot 	if (bootverbose)
209*580d00f4SEmmanuel Vadot 		device_printf(dev, "calculated baud rates are:\n"
210*580d00f4SEmmanuel Vadot 		    " %" PRIu32 " kHz (M=%d, N=%d) for slow,\n"
211*580d00f4SEmmanuel Vadot 		    " %" PRIu32 " kHz (M=%d, N=%d) for fast.\n",
212*580d00f4SEmmanuel Vadot 		    sc->baud_rate[IIC_SLOW].raw / 1000,
213*580d00f4SEmmanuel Vadot 		    sc->baud_rate[IIC_SLOW].m,
214*580d00f4SEmmanuel Vadot 		    sc->baud_rate[IIC_SLOW].n,
215*580d00f4SEmmanuel Vadot 		    sc->baud_rate[IIC_FAST].raw / 1000,
216*580d00f4SEmmanuel Vadot 		    sc->baud_rate[IIC_FAST].m,
217*580d00f4SEmmanuel Vadot 		    sc->baud_rate[IIC_FAST].n);
218*580d00f4SEmmanuel Vadot 
219*580d00f4SEmmanuel Vadot 	sc->reg_data = TWSI_DATA;
220*580d00f4SEmmanuel Vadot 	sc->reg_slave_addr = TWSI_ADDR;
221*580d00f4SEmmanuel Vadot 	sc->reg_slave_ext_addr = TWSI_XADDR;
222*580d00f4SEmmanuel Vadot 	sc->reg_control = TWSI_CNTR;
223*580d00f4SEmmanuel Vadot 	sc->reg_status = TWSI_STAT;
224*580d00f4SEmmanuel Vadot 	sc->reg_baud_rate = TWSI_BAUD_RATE;
225*580d00f4SEmmanuel Vadot 	sc->reg_soft_reset = TWSI_SRST;
226*580d00f4SEmmanuel Vadot 
227*580d00f4SEmmanuel Vadot 	return (twsi_attach(dev));
228*580d00f4SEmmanuel Vadot }
229