xref: /freebsd/sys/dev/iicbus/controller/twsi/mv_twsi.c (revision be82b3a0bf72ed3b5f01ac9fcd8dcd3802e3c742)
1580d00f4SEmmanuel Vadot /*-
2580d00f4SEmmanuel Vadot  * Copyright (C) 2008 MARVELL INTERNATIONAL LTD.
3580d00f4SEmmanuel Vadot  * All rights reserved.
4580d00f4SEmmanuel Vadot  *
5580d00f4SEmmanuel Vadot  * Developed by Semihalf.
6580d00f4SEmmanuel Vadot  *
7580d00f4SEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
8580d00f4SEmmanuel Vadot  * modification, are permitted provided that the following conditions
9580d00f4SEmmanuel Vadot  * are met:
10580d00f4SEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
11580d00f4SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
12580d00f4SEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
13580d00f4SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
14580d00f4SEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
15580d00f4SEmmanuel Vadot  * 3. Neither the name of MARVELL nor the names of contributors
16580d00f4SEmmanuel Vadot  *    may be used to endorse or promote products derived from this software
17580d00f4SEmmanuel Vadot  *    without specific prior written permission.
18580d00f4SEmmanuel Vadot  *
19580d00f4SEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20580d00f4SEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21580d00f4SEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22580d00f4SEmmanuel Vadot  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23580d00f4SEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24580d00f4SEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25580d00f4SEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26580d00f4SEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27580d00f4SEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28580d00f4SEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29580d00f4SEmmanuel Vadot  * SUCH DAMAGE.
30580d00f4SEmmanuel Vadot  */
31580d00f4SEmmanuel Vadot 
32580d00f4SEmmanuel Vadot /*
33580d00f4SEmmanuel Vadot  * Driver for the TWSI (aka I2C, aka IIC) bus controller found on Marvell
34580d00f4SEmmanuel Vadot  * and Allwinner SoCs. Supports master operation only, and works in polling mode.
35580d00f4SEmmanuel Vadot  *
36580d00f4SEmmanuel Vadot  * Calls to DELAY() are needed per Application Note AN-179 "TWSI Software
37580d00f4SEmmanuel Vadot  * Guidelines for Discovery(TM), Horizon (TM) and Feroceon(TM) Devices".
38580d00f4SEmmanuel Vadot  */
39580d00f4SEmmanuel Vadot 
40580d00f4SEmmanuel Vadot #include <sys/param.h>
41580d00f4SEmmanuel Vadot #include <sys/systm.h>
42580d00f4SEmmanuel Vadot #include <sys/bus.h>
43580d00f4SEmmanuel Vadot #include <sys/kernel.h>
44580d00f4SEmmanuel Vadot #include <sys/module.h>
45580d00f4SEmmanuel Vadot #include <sys/resource.h>
46580d00f4SEmmanuel Vadot 
47580d00f4SEmmanuel Vadot #include <machine/_inttypes.h>
48580d00f4SEmmanuel Vadot #include <machine/bus.h>
49580d00f4SEmmanuel Vadot #include <machine/resource.h>
50580d00f4SEmmanuel Vadot 
51580d00f4SEmmanuel Vadot #include <sys/rman.h>
52580d00f4SEmmanuel Vadot 
53580d00f4SEmmanuel Vadot #include <sys/lock.h>
54580d00f4SEmmanuel Vadot #include <sys/mutex.h>
55580d00f4SEmmanuel Vadot 
56580d00f4SEmmanuel Vadot #include <dev/iicbus/iiconf.h>
57580d00f4SEmmanuel Vadot #include <dev/iicbus/iicbus.h>
58580d00f4SEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
59580d00f4SEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
60580d00f4SEmmanuel Vadot 
61*be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
62580d00f4SEmmanuel Vadot 
63580d00f4SEmmanuel Vadot #include <arm/mv/mvreg.h>
64580d00f4SEmmanuel Vadot #include <arm/mv/mvvar.h>
65580d00f4SEmmanuel Vadot #include <dev/iicbus/controller/twsi/twsi.h>
66580d00f4SEmmanuel Vadot 
67580d00f4SEmmanuel Vadot #include "iicbus_if.h"
68580d00f4SEmmanuel Vadot 
69580d00f4SEmmanuel Vadot #define MV_TWSI_NAME		"twsi"
70580d00f4SEmmanuel Vadot #define	IICBUS_DEVNAME		"iicbus"
71580d00f4SEmmanuel Vadot 
72580d00f4SEmmanuel Vadot #define TWSI_ADDR	0x00
73580d00f4SEmmanuel Vadot #define TWSI_DATA	0x04
74580d00f4SEmmanuel Vadot #define TWSI_CNTR	0x08
75580d00f4SEmmanuel Vadot #define TWSI_XADDR	0x10
76580d00f4SEmmanuel Vadot #define TWSI_STAT	0x0c
77580d00f4SEmmanuel Vadot #define TWSI_BAUD_RATE	0x0c
78580d00f4SEmmanuel Vadot #define TWSI_SRST	0x1c
79580d00f4SEmmanuel Vadot 
80580d00f4SEmmanuel Vadot #define	TWSI_BAUD_RATE_RAW(C,M,N)	((C)/((10*(M+1))<<(N+1)))
81580d00f4SEmmanuel Vadot #define	TWSI_BAUD_RATE_SLOW		50000	/* 50kHz */
82580d00f4SEmmanuel Vadot #define	TWSI_BAUD_RATE_FAST		100000	/* 100kHz */
83580d00f4SEmmanuel Vadot 
84580d00f4SEmmanuel Vadot #define TWSI_DEBUG
85580d00f4SEmmanuel Vadot #undef TWSI_DEBUG
86580d00f4SEmmanuel Vadot 
87580d00f4SEmmanuel Vadot #ifdef  TWSI_DEBUG
88580d00f4SEmmanuel Vadot #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt,##args); } while (0)
89580d00f4SEmmanuel Vadot #else
90580d00f4SEmmanuel Vadot #define debugf(fmt, args...)
91580d00f4SEmmanuel Vadot #endif
92580d00f4SEmmanuel Vadot 
93580d00f4SEmmanuel Vadot static phandle_t mv_twsi_get_node(device_t, device_t);
94580d00f4SEmmanuel Vadot static int mv_twsi_probe(device_t);
95580d00f4SEmmanuel Vadot static int mv_twsi_attach(device_t);
96580d00f4SEmmanuel Vadot 
97580d00f4SEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
98580d00f4SEmmanuel Vadot 	{ "mrvl,twsi",			true },
99580d00f4SEmmanuel Vadot 	{ "marvell,mv64xxx-i2c",	true },
100580d00f4SEmmanuel Vadot 	{ "marvell,mv78230-i2c",	true },
101580d00f4SEmmanuel Vadot 	{ NULL,				false }
102580d00f4SEmmanuel Vadot };
103580d00f4SEmmanuel Vadot 
104580d00f4SEmmanuel Vadot static device_method_t mv_twsi_methods[] = {
105580d00f4SEmmanuel Vadot 	/* device interface */
106580d00f4SEmmanuel Vadot 	DEVMETHOD(device_probe,		mv_twsi_probe),
107580d00f4SEmmanuel Vadot 	DEVMETHOD(device_attach,	mv_twsi_attach),
108580d00f4SEmmanuel Vadot 
109580d00f4SEmmanuel Vadot 	/* ofw_bus interface */
110580d00f4SEmmanuel Vadot 	DEVMETHOD(ofw_bus_get_node,	mv_twsi_get_node),
111580d00f4SEmmanuel Vadot 
112580d00f4SEmmanuel Vadot 	DEVMETHOD_END
113580d00f4SEmmanuel Vadot };
114580d00f4SEmmanuel Vadot 
115580d00f4SEmmanuel Vadot DEFINE_CLASS_1(twsi, mv_twsi_driver, mv_twsi_methods,
116580d00f4SEmmanuel Vadot     sizeof(struct twsi_softc), twsi_driver);
117580d00f4SEmmanuel Vadot 
118580d00f4SEmmanuel Vadot DRIVER_MODULE(twsi, simplebus, mv_twsi_driver, 0, 0);
119580d00f4SEmmanuel Vadot DRIVER_MODULE(iicbus, twsi, iicbus_driver, 0, 0);
120580d00f4SEmmanuel Vadot MODULE_DEPEND(twsi, iicbus, 1, 1, 1);
121580d00f4SEmmanuel Vadot SIMPLEBUS_PNP_INFO(compat_data);
122580d00f4SEmmanuel Vadot 
123580d00f4SEmmanuel Vadot static phandle_t
mv_twsi_get_node(device_t bus,device_t dev)124580d00f4SEmmanuel Vadot mv_twsi_get_node(device_t bus, device_t dev)
125580d00f4SEmmanuel Vadot {
126580d00f4SEmmanuel Vadot 
127580d00f4SEmmanuel Vadot 	/* Used by ofw_iicbus. */
128580d00f4SEmmanuel Vadot 	return (ofw_bus_get_node(bus));
129580d00f4SEmmanuel Vadot }
130580d00f4SEmmanuel Vadot 
131580d00f4SEmmanuel Vadot static int
mv_twsi_probe(device_t dev)132580d00f4SEmmanuel Vadot mv_twsi_probe(device_t dev)
133580d00f4SEmmanuel Vadot {
134580d00f4SEmmanuel Vadot 
135580d00f4SEmmanuel Vadot 	if (!ofw_bus_status_okay(dev))
136580d00f4SEmmanuel Vadot 		return (ENXIO);
137580d00f4SEmmanuel Vadot 
138580d00f4SEmmanuel Vadot 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
139580d00f4SEmmanuel Vadot 		return (ENXIO);
140580d00f4SEmmanuel Vadot 
141580d00f4SEmmanuel Vadot 	device_set_desc(dev, "Marvell Integrated I2C Bus Controller");
142580d00f4SEmmanuel Vadot 	return (BUS_PROBE_DEFAULT);
143580d00f4SEmmanuel Vadot }
144580d00f4SEmmanuel Vadot 
145580d00f4SEmmanuel Vadot #define	ABSSUB(a,b)	(((a) > (b)) ? (a) - (b) : (b) - (a))
146580d00f4SEmmanuel Vadot static void
mv_twsi_cal_baud_rate(struct twsi_softc * sc,const uint32_t target,struct twsi_baud_rate * rate)147580d00f4SEmmanuel Vadot mv_twsi_cal_baud_rate(struct twsi_softc *sc, const uint32_t target,
148580d00f4SEmmanuel Vadot     struct twsi_baud_rate *rate)
149580d00f4SEmmanuel Vadot {
150580d00f4SEmmanuel Vadot 	uint64_t clk;
151580d00f4SEmmanuel Vadot 	uint32_t cur, diff, diff0;
152580d00f4SEmmanuel Vadot 	int m, n, m0, n0;
153580d00f4SEmmanuel Vadot 
154580d00f4SEmmanuel Vadot 	/* Calculate baud rate. */
155580d00f4SEmmanuel Vadot 	m0 = n0 = 4;	/* Default values on reset */
156580d00f4SEmmanuel Vadot 	diff0 = 0xffffffff;
157580d00f4SEmmanuel Vadot 	clk_get_freq(sc->clk_core, &clk);
158580d00f4SEmmanuel Vadot 
159580d00f4SEmmanuel Vadot 	for (n = 0; n < 8; n++) {
160580d00f4SEmmanuel Vadot 		for (m = 0; m < 16; m++) {
161580d00f4SEmmanuel Vadot 			cur = TWSI_BAUD_RATE_RAW(clk,m,n);
162580d00f4SEmmanuel Vadot 			diff = ABSSUB(target, cur);
163580d00f4SEmmanuel Vadot 			if (diff < diff0) {
164580d00f4SEmmanuel Vadot 				m0 = m;
165580d00f4SEmmanuel Vadot 				n0 = n;
166580d00f4SEmmanuel Vadot 				diff0 = diff;
167580d00f4SEmmanuel Vadot 			}
168580d00f4SEmmanuel Vadot 		}
169580d00f4SEmmanuel Vadot 	}
170580d00f4SEmmanuel Vadot 	rate->raw = TWSI_BAUD_RATE_RAW(clk, m0, n0);
171580d00f4SEmmanuel Vadot 	rate->param = TWSI_BAUD_RATE_PARAM(m0, n0);
172580d00f4SEmmanuel Vadot 	rate->m = m0;
173580d00f4SEmmanuel Vadot 	rate->n = n0;
174580d00f4SEmmanuel Vadot }
175580d00f4SEmmanuel Vadot 
176580d00f4SEmmanuel Vadot static int
mv_twsi_attach(device_t dev)177580d00f4SEmmanuel Vadot mv_twsi_attach(device_t dev)
178580d00f4SEmmanuel Vadot {
179580d00f4SEmmanuel Vadot 	struct twsi_softc *sc;
180580d00f4SEmmanuel Vadot 	int error;
181580d00f4SEmmanuel Vadot 
182580d00f4SEmmanuel Vadot 	sc = device_get_softc(dev);
183580d00f4SEmmanuel Vadot 	sc->dev = dev;
184580d00f4SEmmanuel Vadot 
185580d00f4SEmmanuel Vadot 	/* Activate clock */
186580d00f4SEmmanuel Vadot 	error = clk_get_by_ofw_index(dev, 0, 0, &sc->clk_core);
187580d00f4SEmmanuel Vadot 	if (error != 0) {
188580d00f4SEmmanuel Vadot 		device_printf(dev, "could not find core clock\n");
189580d00f4SEmmanuel Vadot 		return (error);
190580d00f4SEmmanuel Vadot 	}
191580d00f4SEmmanuel Vadot 	error = clk_enable(sc->clk_core);
192580d00f4SEmmanuel Vadot 	if (error != 0) {
193580d00f4SEmmanuel Vadot 		device_printf(dev, "could not enable core clock\n");
194580d00f4SEmmanuel Vadot 		return (error);
195580d00f4SEmmanuel Vadot 	}
196580d00f4SEmmanuel Vadot 
197580d00f4SEmmanuel Vadot 	if (clk_get_by_ofw_index(dev, 0, 1, &sc->clk_reg) == 0) {
198580d00f4SEmmanuel Vadot 		error = clk_enable(sc->clk_reg);
199580d00f4SEmmanuel Vadot 		if (error != 0) {
200580d00f4SEmmanuel Vadot 			device_printf(dev, "could not enable core clock\n");
201580d00f4SEmmanuel Vadot 			return (error);
202580d00f4SEmmanuel Vadot 		}
203580d00f4SEmmanuel Vadot 	}
204580d00f4SEmmanuel Vadot 
205580d00f4SEmmanuel Vadot 	mv_twsi_cal_baud_rate(sc, TWSI_BAUD_RATE_SLOW, &sc->baud_rate[IIC_SLOW]);
206580d00f4SEmmanuel Vadot 	mv_twsi_cal_baud_rate(sc, TWSI_BAUD_RATE_FAST, &sc->baud_rate[IIC_FAST]);
207580d00f4SEmmanuel Vadot 	if (bootverbose)
208580d00f4SEmmanuel Vadot 		device_printf(dev, "calculated baud rates are:\n"
209580d00f4SEmmanuel Vadot 		    " %" PRIu32 " kHz (M=%d, N=%d) for slow,\n"
210580d00f4SEmmanuel Vadot 		    " %" PRIu32 " kHz (M=%d, N=%d) for fast.\n",
211580d00f4SEmmanuel Vadot 		    sc->baud_rate[IIC_SLOW].raw / 1000,
212580d00f4SEmmanuel Vadot 		    sc->baud_rate[IIC_SLOW].m,
213580d00f4SEmmanuel Vadot 		    sc->baud_rate[IIC_SLOW].n,
214580d00f4SEmmanuel Vadot 		    sc->baud_rate[IIC_FAST].raw / 1000,
215580d00f4SEmmanuel Vadot 		    sc->baud_rate[IIC_FAST].m,
216580d00f4SEmmanuel Vadot 		    sc->baud_rate[IIC_FAST].n);
217580d00f4SEmmanuel Vadot 
218580d00f4SEmmanuel Vadot 	sc->reg_data = TWSI_DATA;
219580d00f4SEmmanuel Vadot 	sc->reg_slave_addr = TWSI_ADDR;
220580d00f4SEmmanuel Vadot 	sc->reg_slave_ext_addr = TWSI_XADDR;
221580d00f4SEmmanuel Vadot 	sc->reg_control = TWSI_CNTR;
222580d00f4SEmmanuel Vadot 	sc->reg_status = TWSI_STAT;
223580d00f4SEmmanuel Vadot 	sc->reg_baud_rate = TWSI_BAUD_RATE;
224580d00f4SEmmanuel Vadot 	sc->reg_soft_reset = TWSI_SRST;
225580d00f4SEmmanuel Vadot 
226580d00f4SEmmanuel Vadot 	return (twsi_attach(dev));
227580d00f4SEmmanuel Vadot }
228