armadillo9_iic.c revision 1.7 1 1.7 chs /* $NetBSD: armadillo9_iic.c,v 1.7 2012/10/27 17:17:46 chs Exp $ */
2 1.1 hamajima
3 1.1 hamajima /*
4 1.1 hamajima * Copyright (c) 2005 HAMAJIMA Katsuomi. All rights reserved.
5 1.1 hamajima *
6 1.1 hamajima * Redistribution and use in source and binary forms, with or without
7 1.1 hamajima * modification, are permitted provided that the following conditions
8 1.1 hamajima * are met:
9 1.1 hamajima * 1. Redistributions of source code must retain the above copyright
10 1.1 hamajima * notice, this list of conditions and the following disclaimer.
11 1.1 hamajima * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 hamajima * notice, this list of conditions and the following disclaimer in the
13 1.1 hamajima * documentation and/or other materials provided with the distribution.
14 1.1 hamajima *
15 1.1 hamajima * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 1.1 hamajima * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.1 hamajima * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.1 hamajima * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 hamajima * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 hamajima * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.1 hamajima * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 hamajima * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 hamajima * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 hamajima * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 hamajima * SUCH DAMAGE.
26 1.1 hamajima */
27 1.1 hamajima
28 1.1 hamajima #include <sys/cdefs.h>
29 1.7 chs __KERNEL_RCSID(0, "$NetBSD: armadillo9_iic.c,v 1.7 2012/10/27 17:17:46 chs Exp $");
30 1.1 hamajima
31 1.1 hamajima #include <sys/param.h>
32 1.1 hamajima #include <sys/systm.h>
33 1.1 hamajima #include <sys/kernel.h>
34 1.1 hamajima #include <sys/device.h>
35 1.4 ad #include <sys/mutex.h>
36 1.1 hamajima #include <dev/i2c/i2cvar.h>
37 1.1 hamajima #include <dev/i2c/i2c_bitbang.h>
38 1.1 hamajima #include <arm/ep93xx/epsocvar.h>
39 1.1 hamajima #include <arm/ep93xx/epgpiovar.h>
40 1.2 thorpej #include <evbarm/armadillo/armadillo9var.h>
41 1.1 hamajima
42 1.1 hamajima #include "seeprom.h"
43 1.1 hamajima #if NSEEPROM > 0
44 1.1 hamajima #include <net/if.h>
45 1.1 hamajima #include <net/if_ether.h>
46 1.1 hamajima #include <dev/i2c/at24cxxvar.h>
47 1.1 hamajima #endif
48 1.1 hamajima
49 1.1 hamajima struct armadillo9iic_softc {
50 1.1 hamajima struct i2c_controller sc_i2c;
51 1.4 ad kmutex_t sc_buslock;
52 1.1 hamajima int sc_port;
53 1.1 hamajima int sc_sda;
54 1.1 hamajima int sc_scl;
55 1.1 hamajima struct epgpio_softc *sc_gpio;
56 1.1 hamajima };
57 1.1 hamajima
58 1.7 chs static int armadillo9iic_match(device_t, cfdata_t, void *);
59 1.7 chs static void armadillo9iic_attach(device_t, device_t, void *);
60 1.1 hamajima
61 1.1 hamajima static int armadillo9iic_acquire_bus(void *, int);
62 1.1 hamajima static void armadillo9iic_release_bus(void *, int);
63 1.1 hamajima static int armadillo9iic_send_start(void *, int);
64 1.1 hamajima static int armadillo9iic_send_stop(void *, int);
65 1.1 hamajima static int armadillo9iic_initiate_xfer(void *, uint16_t, int);
66 1.1 hamajima static int armadillo9iic_read_byte(void *, uint8_t *, int);
67 1.1 hamajima static int armadillo9iic_write_byte(void *, uint8_t, int);
68 1.1 hamajima
69 1.1 hamajima static void armadillo9iic_bb_set_bits(void *, uint32_t);
70 1.1 hamajima static void armadillo9iic_bb_set_dir(void *, uint32_t);
71 1.1 hamajima static uint32_t armadillo9iic_bb_read_bits(void *);
72 1.1 hamajima
73 1.7 chs CFATTACH_DECL_NEW(armadillo9iic, sizeof(struct armadillo9iic_softc),
74 1.1 hamajima armadillo9iic_match, armadillo9iic_attach, NULL, NULL);
75 1.1 hamajima
76 1.1 hamajima static struct i2c_bitbang_ops armadillo9iic_bbops = {
77 1.1 hamajima armadillo9iic_bb_set_bits,
78 1.1 hamajima armadillo9iic_bb_set_dir,
79 1.1 hamajima armadillo9iic_bb_read_bits,
80 1.1 hamajima { 0, 0, 0, 0 } /* set in attach() */
81 1.1 hamajima };
82 1.1 hamajima
83 1.1 hamajima int
84 1.7 chs armadillo9iic_match(device_t parent, cfdata_t cf, void *aux)
85 1.1 hamajima {
86 1.1 hamajima return 2;
87 1.1 hamajima }
88 1.1 hamajima
89 1.1 hamajima void
90 1.7 chs armadillo9iic_attach(device_t parent, device_t self, void *aux)
91 1.1 hamajima {
92 1.7 chs struct armadillo9iic_softc *sc = device_private(self);
93 1.1 hamajima struct i2cbus_attach_args iba;
94 1.1 hamajima #if NSEEPROM > 0
95 1.1 hamajima struct epgpio_attach_args *ga = aux;
96 1.1 hamajima #endif
97 1.4 ad mutex_init(&sc->sc_buslock, MUTEX_DEFAULT, IPL_NONE);
98 1.1 hamajima
99 1.1 hamajima sc->sc_port = ga->ga_port;
100 1.1 hamajima sc->sc_sda = ga->ga_bit1;
101 1.1 hamajima sc->sc_scl = ga->ga_bit2;
102 1.1 hamajima sc->sc_gpio = (struct epgpio_softc *)parent;
103 1.1 hamajima
104 1.1 hamajima armadillo9iic_bbops.ibo_bits[I2C_BIT_SDA] = (1<<sc->sc_sda);
105 1.1 hamajima armadillo9iic_bbops.ibo_bits[I2C_BIT_SCL] = (1<<sc->sc_scl);
106 1.1 hamajima armadillo9iic_bbops.ibo_bits[I2C_BIT_OUTPUT] = sc->sc_sda;
107 1.1 hamajima armadillo9iic_bbops.ibo_bits[I2C_BIT_INPUT] = 0;
108 1.1 hamajima
109 1.1 hamajima sc->sc_i2c.ic_cookie = sc;
110 1.1 hamajima sc->sc_i2c.ic_acquire_bus = armadillo9iic_acquire_bus;
111 1.1 hamajima sc->sc_i2c.ic_release_bus = armadillo9iic_release_bus;
112 1.1 hamajima sc->sc_i2c.ic_send_start = armadillo9iic_send_start;
113 1.1 hamajima sc->sc_i2c.ic_send_stop = armadillo9iic_send_stop;
114 1.1 hamajima sc->sc_i2c.ic_initiate_xfer = armadillo9iic_initiate_xfer;
115 1.1 hamajima sc->sc_i2c.ic_read_byte = armadillo9iic_read_byte;
116 1.1 hamajima sc->sc_i2c.ic_write_byte = armadillo9iic_write_byte;
117 1.1 hamajima
118 1.1 hamajima iba.iba_tag = &sc->sc_i2c;
119 1.1 hamajima
120 1.6 hamajima epgpio_in(sc->sc_gpio, sc->sc_port, sc->sc_sda);
121 1.1 hamajima epgpio_out(sc->sc_gpio, sc->sc_port, sc->sc_scl);
122 1.1 hamajima epgpio_set(sc->sc_gpio, sc->sc_port, sc->sc_scl);
123 1.1 hamajima
124 1.1 hamajima printf("\n");
125 1.1 hamajima
126 1.7 chs config_found_ia(self, "i2cbus", &iba, iicbus_print);
127 1.1 hamajima
128 1.1 hamajima #if NSEEPROM > 0
129 1.2 thorpej /* read mac address */
130 1.2 thorpej /* XXX This should probably be done elsewhere, earlier in bootstrap. */
131 1.2 thorpej if (seeprom_bootstrap_read(&sc->sc_i2c, 0x50, 0x00, 128,
132 1.2 thorpej armadillo9_ethaddr, ETHER_ADDR_LEN) != 0) {
133 1.2 thorpej printf("%s: WARNING: unable to read MAC address from SEEPROM\n",
134 1.7 chs device_xname(self));
135 1.1 hamajima }
136 1.1 hamajima #endif
137 1.1 hamajima }
138 1.1 hamajima
139 1.1 hamajima int
140 1.1 hamajima armadillo9iic_acquire_bus(void *cookie, int flags)
141 1.1 hamajima {
142 1.1 hamajima struct armadillo9iic_softc *sc = cookie;
143 1.1 hamajima
144 1.1 hamajima /* XXX What should we do for the polling case? */
145 1.1 hamajima if (flags & I2C_F_POLL)
146 1.1 hamajima return 0;
147 1.1 hamajima
148 1.4 ad mutex_enter(&sc->sc_buslock);
149 1.4 ad return 0;
150 1.1 hamajima }
151 1.1 hamajima
152 1.1 hamajima void
153 1.1 hamajima armadillo9iic_release_bus(void *cookie, int flags)
154 1.1 hamajima {
155 1.1 hamajima struct armadillo9iic_softc *sc = cookie;
156 1.1 hamajima
157 1.1 hamajima /* XXX See above. */
158 1.1 hamajima if (flags & I2C_F_POLL)
159 1.1 hamajima return;
160 1.1 hamajima
161 1.4 ad mutex_exit(&sc->sc_buslock);
162 1.1 hamajima }
163 1.1 hamajima
164 1.1 hamajima int
165 1.1 hamajima armadillo9iic_send_start(void *cookie, int flags)
166 1.1 hamajima {
167 1.1 hamajima return i2c_bitbang_send_start(cookie, flags, &armadillo9iic_bbops);
168 1.1 hamajima }
169 1.1 hamajima
170 1.1 hamajima int
171 1.1 hamajima armadillo9iic_send_stop(void *cookie, int flags)
172 1.1 hamajima {
173 1.1 hamajima return i2c_bitbang_send_stop(cookie, flags, &armadillo9iic_bbops);
174 1.1 hamajima }
175 1.1 hamajima
176 1.1 hamajima int
177 1.1 hamajima armadillo9iic_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
178 1.1 hamajima {
179 1.1 hamajima return i2c_bitbang_initiate_xfer(cookie, addr, flags,
180 1.1 hamajima &armadillo9iic_bbops);
181 1.1 hamajima }
182 1.1 hamajima
183 1.1 hamajima int
184 1.1 hamajima armadillo9iic_read_byte(void *cookie, uint8_t *bytep, int flags)
185 1.1 hamajima {
186 1.1 hamajima return i2c_bitbang_read_byte(cookie, bytep, flags,
187 1.1 hamajima &armadillo9iic_bbops);
188 1.1 hamajima }
189 1.1 hamajima
190 1.1 hamajima int
191 1.1 hamajima armadillo9iic_write_byte(void *cookie, uint8_t byte, int flags)
192 1.1 hamajima {
193 1.1 hamajima return i2c_bitbang_write_byte(cookie, byte, flags,
194 1.1 hamajima &armadillo9iic_bbops);
195 1.1 hamajima }
196 1.1 hamajima
197 1.1 hamajima void
198 1.1 hamajima armadillo9iic_bb_set_bits(void *cookie, uint32_t bits)
199 1.1 hamajima {
200 1.1 hamajima struct armadillo9iic_softc *sc = cookie;
201 1.1 hamajima
202 1.1 hamajima if (bits & (1 << sc->sc_sda))
203 1.1 hamajima epgpio_set(sc->sc_gpio, sc->sc_port, sc->sc_sda);
204 1.1 hamajima else
205 1.1 hamajima epgpio_clear(sc->sc_gpio, sc->sc_port, sc->sc_sda);
206 1.1 hamajima
207 1.1 hamajima if (bits & (1 << sc->sc_scl))
208 1.1 hamajima epgpio_set(sc->sc_gpio, sc->sc_port, sc->sc_scl);
209 1.1 hamajima else
210 1.1 hamajima epgpio_clear(sc->sc_gpio, sc->sc_port, sc->sc_scl);
211 1.1 hamajima }
212 1.1 hamajima
213 1.1 hamajima void
214 1.1 hamajima armadillo9iic_bb_set_dir(void *cookie, uint32_t bits)
215 1.1 hamajima {
216 1.6 hamajima struct armadillo9iic_softc *sc = cookie;
217 1.6 hamajima
218 1.6 hamajima if(bits)
219 1.6 hamajima epgpio_out(sc->sc_gpio, sc->sc_port, sc->sc_sda);
220 1.6 hamajima else
221 1.6 hamajima epgpio_in(sc->sc_gpio, sc->sc_port, sc->sc_sda);
222 1.1 hamajima }
223 1.1 hamajima
224 1.1 hamajima uint32_t
225 1.1 hamajima armadillo9iic_bb_read_bits(void *cookie)
226 1.1 hamajima {
227 1.1 hamajima struct armadillo9iic_softc *sc = cookie;
228 1.5 hamajima uint32_t bits = 0;
229 1.5 hamajima
230 1.5 hamajima bits |= epgpio_read(sc->sc_gpio, sc->sc_port, sc->sc_sda) << sc->sc_sda;
231 1.5 hamajima bits |= epgpio_read(sc->sc_gpio, sc->sc_port, sc->sc_scl) << sc->sc_scl;
232 1.1 hamajima
233 1.5 hamajima return bits;
234 1.1 hamajima }
235