armadillo9_iic.c revision 1.12 1 1.12 thorpej /* $NetBSD: armadillo9_iic.c,v 1.12 2025/09/15 13:23:01 thorpej 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.12 thorpej __KERNEL_RCSID(0, "$NetBSD: armadillo9_iic.c,v 1.12 2025/09/15 13:23:01 thorpej 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.1 hamajima int sc_port;
52 1.1 hamajima int sc_sda;
53 1.1 hamajima int sc_scl;
54 1.1 hamajima struct epgpio_softc *sc_gpio;
55 1.1 hamajima };
56 1.1 hamajima
57 1.7 chs static int armadillo9iic_match(device_t, cfdata_t, void *);
58 1.7 chs static void armadillo9iic_attach(device_t, device_t, void *);
59 1.1 hamajima
60 1.1 hamajima static int armadillo9iic_send_start(void *, int);
61 1.1 hamajima static int armadillo9iic_send_stop(void *, int);
62 1.1 hamajima static int armadillo9iic_initiate_xfer(void *, uint16_t, int);
63 1.1 hamajima static int armadillo9iic_read_byte(void *, uint8_t *, int);
64 1.1 hamajima static int armadillo9iic_write_byte(void *, uint8_t, int);
65 1.1 hamajima
66 1.1 hamajima static void armadillo9iic_bb_set_bits(void *, uint32_t);
67 1.1 hamajima static void armadillo9iic_bb_set_dir(void *, uint32_t);
68 1.1 hamajima static uint32_t armadillo9iic_bb_read_bits(void *);
69 1.1 hamajima
70 1.7 chs CFATTACH_DECL_NEW(armadillo9iic, sizeof(struct armadillo9iic_softc),
71 1.1 hamajima armadillo9iic_match, armadillo9iic_attach, NULL, NULL);
72 1.1 hamajima
73 1.1 hamajima static struct i2c_bitbang_ops armadillo9iic_bbops = {
74 1.1 hamajima armadillo9iic_bb_set_bits,
75 1.1 hamajima armadillo9iic_bb_set_dir,
76 1.1 hamajima armadillo9iic_bb_read_bits,
77 1.1 hamajima { 0, 0, 0, 0 } /* set in attach() */
78 1.1 hamajima };
79 1.1 hamajima
80 1.1 hamajima int
81 1.7 chs armadillo9iic_match(device_t parent, cfdata_t cf, void *aux)
82 1.1 hamajima {
83 1.1 hamajima return 2;
84 1.1 hamajima }
85 1.1 hamajima
86 1.1 hamajima void
87 1.7 chs armadillo9iic_attach(device_t parent, device_t self, void *aux)
88 1.1 hamajima {
89 1.7 chs struct armadillo9iic_softc *sc = device_private(self);
90 1.1 hamajima #if NSEEPROM > 0
91 1.1 hamajima struct epgpio_attach_args *ga = aux;
92 1.1 hamajima #endif
93 1.1 hamajima
94 1.1 hamajima sc->sc_port = ga->ga_port;
95 1.1 hamajima sc->sc_sda = ga->ga_bit1;
96 1.1 hamajima sc->sc_scl = ga->ga_bit2;
97 1.1 hamajima sc->sc_gpio = (struct epgpio_softc *)parent;
98 1.1 hamajima
99 1.1 hamajima armadillo9iic_bbops.ibo_bits[I2C_BIT_SDA] = (1<<sc->sc_sda);
100 1.1 hamajima armadillo9iic_bbops.ibo_bits[I2C_BIT_SCL] = (1<<sc->sc_scl);
101 1.1 hamajima armadillo9iic_bbops.ibo_bits[I2C_BIT_OUTPUT] = sc->sc_sda;
102 1.1 hamajima armadillo9iic_bbops.ibo_bits[I2C_BIT_INPUT] = 0;
103 1.1 hamajima
104 1.9 thorpej iic_tag_init(&sc->sc_i2c);
105 1.1 hamajima sc->sc_i2c.ic_cookie = sc;
106 1.1 hamajima sc->sc_i2c.ic_send_start = armadillo9iic_send_start;
107 1.1 hamajima sc->sc_i2c.ic_send_stop = armadillo9iic_send_stop;
108 1.1 hamajima sc->sc_i2c.ic_initiate_xfer = armadillo9iic_initiate_xfer;
109 1.1 hamajima sc->sc_i2c.ic_read_byte = armadillo9iic_read_byte;
110 1.1 hamajima sc->sc_i2c.ic_write_byte = armadillo9iic_write_byte;
111 1.1 hamajima
112 1.6 hamajima epgpio_in(sc->sc_gpio, sc->sc_port, sc->sc_sda);
113 1.1 hamajima epgpio_out(sc->sc_gpio, sc->sc_port, sc->sc_scl);
114 1.1 hamajima epgpio_set(sc->sc_gpio, sc->sc_port, sc->sc_scl);
115 1.1 hamajima
116 1.1 hamajima printf("\n");
117 1.1 hamajima
118 1.12 thorpej iicbus_attach(self, &sc->sc_i2c);
119 1.1 hamajima
120 1.1 hamajima #if NSEEPROM > 0
121 1.2 thorpej /* read mac address */
122 1.2 thorpej /* XXX This should probably be done elsewhere, earlier in bootstrap. */
123 1.2 thorpej if (seeprom_bootstrap_read(&sc->sc_i2c, 0x50, 0x00, 128,
124 1.2 thorpej armadillo9_ethaddr, ETHER_ADDR_LEN) != 0) {
125 1.2 thorpej printf("%s: WARNING: unable to read MAC address from SEEPROM\n",
126 1.7 chs device_xname(self));
127 1.1 hamajima }
128 1.1 hamajima #endif
129 1.1 hamajima }
130 1.1 hamajima
131 1.1 hamajima int
132 1.1 hamajima armadillo9iic_send_start(void *cookie, int flags)
133 1.1 hamajima {
134 1.1 hamajima return i2c_bitbang_send_start(cookie, flags, &armadillo9iic_bbops);
135 1.1 hamajima }
136 1.1 hamajima
137 1.1 hamajima int
138 1.1 hamajima armadillo9iic_send_stop(void *cookie, int flags)
139 1.1 hamajima {
140 1.1 hamajima return i2c_bitbang_send_stop(cookie, flags, &armadillo9iic_bbops);
141 1.1 hamajima }
142 1.1 hamajima
143 1.1 hamajima int
144 1.1 hamajima armadillo9iic_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
145 1.1 hamajima {
146 1.1 hamajima return i2c_bitbang_initiate_xfer(cookie, addr, flags,
147 1.1 hamajima &armadillo9iic_bbops);
148 1.1 hamajima }
149 1.1 hamajima
150 1.1 hamajima int
151 1.1 hamajima armadillo9iic_read_byte(void *cookie, uint8_t *bytep, int flags)
152 1.1 hamajima {
153 1.1 hamajima return i2c_bitbang_read_byte(cookie, bytep, flags,
154 1.1 hamajima &armadillo9iic_bbops);
155 1.1 hamajima }
156 1.1 hamajima
157 1.1 hamajima int
158 1.1 hamajima armadillo9iic_write_byte(void *cookie, uint8_t byte, int flags)
159 1.1 hamajima {
160 1.1 hamajima return i2c_bitbang_write_byte(cookie, byte, flags,
161 1.1 hamajima &armadillo9iic_bbops);
162 1.1 hamajima }
163 1.1 hamajima
164 1.1 hamajima void
165 1.1 hamajima armadillo9iic_bb_set_bits(void *cookie, uint32_t bits)
166 1.1 hamajima {
167 1.1 hamajima struct armadillo9iic_softc *sc = cookie;
168 1.1 hamajima
169 1.1 hamajima if (bits & (1 << sc->sc_sda))
170 1.1 hamajima epgpio_set(sc->sc_gpio, sc->sc_port, sc->sc_sda);
171 1.1 hamajima else
172 1.1 hamajima epgpio_clear(sc->sc_gpio, sc->sc_port, sc->sc_sda);
173 1.1 hamajima
174 1.1 hamajima if (bits & (1 << sc->sc_scl))
175 1.1 hamajima epgpio_set(sc->sc_gpio, sc->sc_port, sc->sc_scl);
176 1.1 hamajima else
177 1.1 hamajima epgpio_clear(sc->sc_gpio, sc->sc_port, sc->sc_scl);
178 1.1 hamajima }
179 1.1 hamajima
180 1.1 hamajima void
181 1.1 hamajima armadillo9iic_bb_set_dir(void *cookie, uint32_t bits)
182 1.1 hamajima {
183 1.6 hamajima struct armadillo9iic_softc *sc = cookie;
184 1.6 hamajima
185 1.6 hamajima if(bits)
186 1.6 hamajima epgpio_out(sc->sc_gpio, sc->sc_port, sc->sc_sda);
187 1.6 hamajima else
188 1.6 hamajima epgpio_in(sc->sc_gpio, sc->sc_port, sc->sc_sda);
189 1.1 hamajima }
190 1.1 hamajima
191 1.1 hamajima uint32_t
192 1.1 hamajima armadillo9iic_bb_read_bits(void *cookie)
193 1.1 hamajima {
194 1.1 hamajima struct armadillo9iic_softc *sc = cookie;
195 1.5 hamajima uint32_t bits = 0;
196 1.5 hamajima
197 1.5 hamajima bits |= epgpio_read(sc->sc_gpio, sc->sc_port, sc->sc_sda) << sc->sc_sda;
198 1.5 hamajima bits |= epgpio_read(sc->sc_gpio, sc->sc_port, sc->sc_scl) << sc->sc_scl;
199 1.1 hamajima
200 1.5 hamajima return bits;
201 1.1 hamajima }
202