jbus-i2c.c revision 1.7.2.1 1 /* $NetBSD: jbus-i2c.c,v 1.7.2.1 2021/08/09 00:30:08 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2018 Michael Lorenz
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: jbus-i2c.c,v 1.7.2.1 2021/08/09 00:30:08 thorpej Exp $");
31
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/errno.h>
35
36 #include <sys/bus.h>
37 #include <machine/autoconf.h>
38
39 #include <dev/i2c/i2cvar.h>
40 #include <dev/i2c/i2c_bitbang.h>
41
42 #include <machine/openfirm.h>
43
44 #ifdef JBUSI2C_DEBUG
45 #define DPRINTF printf
46 #else
47 #define DPRINTF if (0) printf
48 #endif
49
50 /* I2C glue */
51 static int jbusi2c_i2c_send_start(void *, int);
52 static int jbusi2c_i2c_send_stop(void *, int);
53 static int jbusi2c_i2c_initiate_xfer(void *, i2c_addr_t, int);
54 static int jbusi2c_i2c_read_byte(void *, uint8_t *, int);
55 static int jbusi2c_i2c_write_byte(void *, uint8_t, int);
56
57 /* I2C bitbang glue */
58 static void jbusi2c_i2cbb_set_bits(void *, uint32_t);
59 static void jbusi2c_i2cbb_set_dir(void *, uint32_t);
60 static uint32_t jbusi2c_i2cbb_read(void *);
61
62 static const struct i2c_bitbang_ops jbusi2c_i2cbb_ops = {
63 jbusi2c_i2cbb_set_bits,
64 jbusi2c_i2cbb_set_dir,
65 jbusi2c_i2cbb_read,
66 {
67 2, /* bit 1 is data */
68 1, /* bit 0 is clock */
69 3, /* direction register for both out */
70 1 /* data in, clock out */
71 }
72 };
73
74 static int jbusi2c_match(device_t, cfdata_t, void *);
75 static void jbusi2c_attach(device_t, device_t, void *);
76
77 struct jbusi2c_softc {
78 device_t sc_dev;
79 struct i2c_controller sc_i2c;
80 bus_space_tag_t sc_bustag;
81 bus_space_handle_t sc_regh;
82 };
83
84 CFATTACH_DECL_NEW(jbusi2c, sizeof(struct jbusi2c_softc),
85 jbusi2c_match, jbusi2c_attach, NULL, NULL);
86
87 /* schizo GPIO registers */
88 #define DATA 0
89 #define DIR 8
90
91 int
92 jbusi2c_match(device_t parent, cfdata_t match, void *aux)
93 {
94 struct mainbus_attach_args *ma = aux;
95 char *str;
96
97 if (strcmp(ma->ma_name, "i2c") != 0)
98 return (0);
99
100 str = prom_getpropstring(ma->ma_node, "compatible");
101 if (strcmp(str, "jbus-i2c") == 0)
102 return (1);
103
104 return (0);
105 }
106
107 void
108 jbusi2c_attach(device_t parent, device_t self, void *aux)
109 {
110 struct jbusi2c_softc *sc = device_private(self);
111 struct mainbus_attach_args *ma = aux;
112 struct i2cbus_attach_args iba;
113
114 aprint_normal(": addr %" PRIx64 "\n", ma->ma_reg[0].ur_paddr);
115
116 sc->sc_dev = self;
117 sc->sc_bustag = ma->ma_bustag;
118
119 if (bus_space_map(sc->sc_bustag, ma->ma_reg[0].ur_paddr, 16, 0,
120 &sc->sc_regh)) {
121 aprint_error(": failed to map registers\n");
122 return;
123 }
124
125 iic_tag_init(&sc->sc_i2c);
126 sc->sc_i2c.ic_cookie = sc;
127 sc->sc_i2c.ic_send_start = jbusi2c_i2c_send_start;
128 sc->sc_i2c.ic_send_stop = jbusi2c_i2c_send_stop;
129 sc->sc_i2c.ic_initiate_xfer = jbusi2c_i2c_initiate_xfer;
130 sc->sc_i2c.ic_read_byte = jbusi2c_i2c_read_byte;
131 sc->sc_i2c.ic_write_byte = jbusi2c_i2c_write_byte;
132
133 memset(&iba, 0, sizeof(iba));
134 iba.iba_tag = &sc->sc_i2c;
135 config_found(sc->sc_dev, &iba, iicbus_print,
136 CFARGS(.devhandle = device_handle(self)));
137 }
138
139 static inline void
140 jbusi2c_write(struct jbusi2c_softc *sc, int reg, uint64_t bits)
141 {
142 bus_space_write_8(sc->sc_bustag, sc->sc_regh, reg, bits);
143 }
144
145 static inline uint64_t
146 jbusi2c_read(struct jbusi2c_softc *sc, int reg)
147 {
148 return bus_space_read_8(sc->sc_bustag, sc->sc_regh, reg);
149 }
150
151 /* I2C bitbanging */
152 static void
153 jbusi2c_i2cbb_set_bits(void *cookie, uint32_t bits)
154 {
155 struct jbusi2c_softc *sc = cookie;
156
157 jbusi2c_write(sc, DATA, bits);
158 }
159
160 static void
161 jbusi2c_i2cbb_set_dir(void *cookie, uint32_t dir)
162 {
163 struct jbusi2c_softc *sc = cookie;
164
165 jbusi2c_write(sc, DIR, dir);
166 }
167
168 static uint32_t
169 jbusi2c_i2cbb_read(void *cookie)
170 {
171 struct jbusi2c_softc *sc = cookie;
172
173 return jbusi2c_read(sc, DATA);
174 }
175
176 /* higher level I2C stuff */
177 static int
178 jbusi2c_i2c_send_start(void *cookie, int flags)
179 {
180
181 return i2c_bitbang_send_start(cookie, flags, &jbusi2c_i2cbb_ops);
182 }
183
184 static int
185 jbusi2c_i2c_send_stop(void *cookie, int flags)
186 {
187
188 return i2c_bitbang_send_stop(cookie, flags, &jbusi2c_i2cbb_ops);
189 }
190
191 static int
192 jbusi2c_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
193 {
194
195 return i2c_bitbang_initiate_xfer(cookie, addr, flags,
196 &jbusi2c_i2cbb_ops);
197 }
198
199 static int
200 jbusi2c_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
201 {
202
203 return i2c_bitbang_read_byte(cookie, valp, flags, &jbusi2c_i2cbb_ops);
204 }
205
206 static int
207 jbusi2c_i2c_write_byte(void *cookie, uint8_t val, int flags)
208 {
209
210 return i2c_bitbang_write_byte(cookie, val, flags, &jbusi2c_i2cbb_ops);
211 }
212