xref: /netbsd-src/sys/arch/evbarm/nslu2/nslu2_iic.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: nslu2_iic.c,v 1.8 2012/10/14 14:20:58 msaitoh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Steve C. Woodford.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/device.h>
36 #include <sys/mutex.h>
37 #include <sys/bus.h>
38 
39 #include <dev/i2c/i2cvar.h>
40 #include <dev/i2c/i2c_bitbang.h>
41 
42 #include <arm/xscale/ixp425reg.h>
43 #include <arm/xscale/ixp425var.h>
44 
45 #include <evbarm/nslu2/nslu2reg.h>
46 
47 struct slugiic_softc {
48 	struct i2c_controller sc_ic;
49 	struct i2c_bitbang_ops sc_ibo;
50 	kmutex_t sc_lock;
51 	uint32_t sc_dirout;
52 };
53 
54 static int
55 slugiic_acquire_bus(void *arg, int flags)
56 {
57 	struct slugiic_softc *sc = arg;
58 
59 	if (flags & I2C_F_POLL)
60 		return (0);
61 
62 	mutex_enter(&sc->sc_lock);
63 	return (0);
64 }
65 
66 static void
67 slugiic_release_bus(void *arg, int flags)
68 {
69 	struct slugiic_softc *sc = arg;
70 
71 	if (flags & I2C_F_POLL)
72 		return;
73 
74 	mutex_exit(&sc->sc_lock);
75 }
76 
77 static int
78 slugiic_send_start(void *arg, int flags)
79 {
80 	struct slugiic_softc *sc = arg;
81 
82 	return (i2c_bitbang_send_start(sc, flags, &sc->sc_ibo));
83 }
84 
85 static int
86 slugiic_send_stop(void *arg, int flags)
87 {
88 	struct slugiic_softc *sc = arg;
89 
90 	return (i2c_bitbang_send_stop(sc, flags, &sc->sc_ibo));
91 }
92 
93 static int
94 slugiic_initiate_xfer(void *arg, i2c_addr_t addr, int flags)
95 {
96 	struct slugiic_softc *sc = arg;
97 
98 	return (i2c_bitbang_initiate_xfer(sc, addr, flags, &sc->sc_ibo));
99 }
100 
101 static int
102 slugiic_read_byte(void *arg, uint8_t *vp, int flags)
103 {
104 	struct slugiic_softc *sc = arg;
105 
106 	return (i2c_bitbang_read_byte(sc, vp, flags, &sc->sc_ibo));
107 }
108 
109 static int
110 slugiic_write_byte(void *arg, uint8_t v, int flags)
111 {
112 	struct slugiic_softc *sc = arg;
113 
114 	return (i2c_bitbang_write_byte(sc, v, flags, &sc->sc_ibo));
115 }
116 
117 static void
118 slugiic_set_dir(void *arg, uint32_t bits)
119 {
120 	struct slugiic_softc *sc = arg;
121 	uint32_t reg;
122 	int s;
123 
124 	if (sc->sc_dirout == bits)
125 		return;
126 
127 	s = splhigh();
128 
129 	sc->sc_dirout = bits;
130 
131 	if (sc->sc_dirout) {
132 		/* SDA is output; enable SDA output if SDA OUTR is low */
133 		reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
134 		if ((reg & GPIO_I2C_SDA_BIT) == 0) {
135 			reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOER);
136 			reg &= ~GPIO_I2C_SDA_BIT;
137 			GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOER, reg);
138 		}
139 	} else {
140 		/* SDA is input; disable SDA output */
141 		reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOER);
142 		reg |= GPIO_I2C_SDA_BIT;
143 		GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOER, reg);
144 	}
145 
146 	splx(s);
147 }
148 
149 static void
150 slugiic_set_bits(void *arg, uint32_t bits)
151 {
152 	struct slugiic_softc *sc = arg;
153 	uint32_t oer, outr;
154 	int s;
155 
156 	s = splhigh();
157 
158 	/*
159 	 * Enable SCL output if the SCL line is to be driven low.
160 	 * Enable SDA output if the SDA line is to be driven low and
161 	 * SDA direction is output.
162 	 * Otherwise switch them to input even if directions are output
163 	 * so that we can emulate open collector output with the pullup
164 	 * resistors.
165 	 * If lines are to be set to high, disable OER first then set OUTR.
166 	 * If lines are to be set to low, set OUTR first then enable OER.
167 	 */
168 	oer = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOER);
169 	if ((bits & GPIO_I2C_SCL_BIT) != 0)
170 		oer |= GPIO_I2C_SCL_BIT;
171 	if ((bits & GPIO_I2C_SDA_BIT) != 0)
172 		oer |= GPIO_I2C_SDA_BIT;
173 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOER, oer);
174 
175 	outr = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
176 	outr &= ~(GPIO_I2C_SDA_BIT | GPIO_I2C_SCL_BIT);
177 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, outr | bits);
178 
179 	if ((bits & GPIO_I2C_SCL_BIT) == 0)
180 		oer &= ~GPIO_I2C_SCL_BIT;
181 	if ((bits & GPIO_I2C_SDA_BIT) == 0 && sc->sc_dirout)
182 		oer &= ~GPIO_I2C_SDA_BIT;
183 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOER, oer);
184 
185 	splx(s);
186 }
187 
188 static uint32_t
189 slugiic_read_bits(void *arg)
190 {
191 	uint32_t reg;
192 
193 	reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPINR);
194 	return (reg & (GPIO_I2C_SDA_BIT | GPIO_I2C_SCL_BIT));
195 }
196 
197 static void
198 slugiic_deferred_attach(device_t self)
199 {
200 	struct slugiic_softc *sc = device_private(self);
201 	struct i2cbus_attach_args iba;
202 	uint32_t reg;
203 
204 	reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
205 	reg |= GPIO_I2C_SDA_BIT | GPIO_I2C_SCL_BIT;
206 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg);
207 
208 	reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOER);
209 	reg &= ~GPIO_I2C_SCL_BIT;
210 	reg |= GPIO_I2C_SDA_BIT;
211 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOER, reg);
212 
213 	iba.iba_tag = &sc->sc_ic;
214 	(void) config_found_ia(self, "i2cbus", &iba, iicbus_print);
215 }
216 
217 static int
218 slugiic_match(device_t parent, cfdata_t cf, void *aux)
219 {
220 
221 	return (1);
222 }
223 
224 static void
225 slugiic_attach(device_t parent, device_t self, void *aux)
226 {
227 	struct slugiic_softc *sc = device_private(self);
228 
229 	aprint_naive("\n");
230 	aprint_normal(": I2C bus\n");
231 
232 	sc->sc_ic.ic_cookie = sc;
233 	sc->sc_ic.ic_acquire_bus = slugiic_acquire_bus;
234 	sc->sc_ic.ic_release_bus = slugiic_release_bus;
235 	sc->sc_ic.ic_exec = NULL;
236 	sc->sc_ic.ic_send_start = slugiic_send_start;
237 	sc->sc_ic.ic_send_stop = slugiic_send_stop;
238 	sc->sc_ic.ic_initiate_xfer = slugiic_initiate_xfer;
239 	sc->sc_ic.ic_read_byte = slugiic_read_byte;
240 	sc->sc_ic.ic_write_byte = slugiic_write_byte;
241 
242 	sc->sc_ibo.ibo_set_dir = slugiic_set_dir;
243 	sc->sc_ibo.ibo_set_bits = slugiic_set_bits;
244 	sc->sc_ibo.ibo_read_bits = slugiic_read_bits;
245 	sc->sc_ibo.ibo_bits[I2C_BIT_SDA] = GPIO_I2C_SDA_BIT;
246 	sc->sc_ibo.ibo_bits[I2C_BIT_SCL] = GPIO_I2C_SCL_BIT;
247 	sc->sc_ibo.ibo_bits[I2C_BIT_OUTPUT] = 1;
248 	sc->sc_ibo.ibo_bits[I2C_BIT_INPUT] = 0;
249 
250 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
251 
252 	sc->sc_dirout = 0;
253 
254 	/*
255 	 * Defer until ixp425_softc has been initialised
256 	 */
257 	config_interrupts(self, slugiic_deferred_attach);
258 }
259 
260 CFATTACH_DECL_NEW(slugiic, sizeof(struct slugiic_softc),
261     slugiic_match, slugiic_attach, NULL, NULL);
262