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