summaryrefslogtreecommitdiff
path: root/sys/arch/sparc64/dev/jbus-i2c.c
blob: 25e52e53b7f97021a5330f22b461dc30e1234968 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*	$NetBSD: jbus-i2c.c,v 1.7 2021/08/07 16:19:05 thorpej Exp $	*/

/*
 * Copyright (c) 2018 Michael Lorenz
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: jbus-i2c.c,v 1.7 2021/08/07 16:19:05 thorpej Exp $");

#include <sys/param.h>
#include <sys/device.h>
#include <sys/errno.h>

#include <sys/bus.h>
#include <machine/autoconf.h>

#include <dev/i2c/i2cvar.h>
#include <dev/i2c/i2c_bitbang.h>

#include <machine/openfirm.h>

#ifdef JBUSI2C_DEBUG
#define DPRINTF printf
#else
#define DPRINTF if (0) printf
#endif

/* I2C glue */
static int jbusi2c_i2c_send_start(void *, int);
static int jbusi2c_i2c_send_stop(void *, int);
static int jbusi2c_i2c_initiate_xfer(void *, i2c_addr_t, int);
static int jbusi2c_i2c_read_byte(void *, uint8_t *, int);
static int jbusi2c_i2c_write_byte(void *, uint8_t, int);

/* I2C bitbang glue */
static void jbusi2c_i2cbb_set_bits(void *, uint32_t);
static void jbusi2c_i2cbb_set_dir(void *, uint32_t);
static uint32_t jbusi2c_i2cbb_read(void *);

static const struct i2c_bitbang_ops jbusi2c_i2cbb_ops = {
	jbusi2c_i2cbb_set_bits,
	jbusi2c_i2cbb_set_dir,
	jbusi2c_i2cbb_read,
	{
		2,	/* bit 1 is data */
		1,	/* bit 0 is clock */
		3,	/* direction register for both out */
		1	/* data in, clock out */
	}
};

static	int	jbusi2c_match(device_t, cfdata_t, void *);
static	void	jbusi2c_attach(device_t, device_t, void *);

struct jbusi2c_softc {
	device_t sc_dev;
	struct i2c_controller sc_i2c;
	bus_space_tag_t sc_bustag;
	bus_space_handle_t sc_regh;
	int sc_node;
};

static void jbusi2c_setup_i2c(struct jbusi2c_softc *);

CFATTACH_DECL_NEW(jbusi2c, sizeof(struct jbusi2c_softc),
    jbusi2c_match, jbusi2c_attach, NULL, NULL);

/* schizo GPIO registers */
#define DATA	0
#define DIR	8

int
jbusi2c_match(device_t parent, cfdata_t match, void *aux)
{
	struct mainbus_attach_args *ma = aux;
	char *str;

	if (strcmp(ma->ma_name, "i2c") != 0)
		return (0);

	str = prom_getpropstring(ma->ma_node, "compatible");
	if (strcmp(str, "jbus-i2c") == 0)
		return (1);

	return (0);
}

void
jbusi2c_attach(device_t parent, device_t self, void *aux)
{
	struct jbusi2c_softc *sc = device_private(self);
	struct mainbus_attach_args *ma = aux;

	aprint_normal(": addr %" PRIx64 "\n", ma->ma_reg[0].ur_paddr);

	sc->sc_dev = self;
	sc->sc_node = ma->ma_node;
	sc->sc_bustag = ma->ma_bustag;

	if (bus_space_map(sc->sc_bustag, ma->ma_reg[0].ur_paddr, 16, 0,
	    &sc->sc_regh)) {
		aprint_error(": failed to map registers\n");
		return;
	}

	jbusi2c_setup_i2c(sc);
}



static void
jbusi2c_setup_i2c(struct jbusi2c_softc *sc)
{
	struct i2cbus_attach_args iba;
	prop_array_t cfg;
	prop_dictionary_t dev;
	prop_dictionary_t dict = device_properties(sc->sc_dev);
	int devs, regs[2], addr;
	char name[64], compat[256];

	iic_tag_init(&sc->sc_i2c);
	sc->sc_i2c.ic_cookie = sc;
	sc->sc_i2c.ic_send_start = jbusi2c_i2c_send_start;
	sc->sc_i2c.ic_send_stop = jbusi2c_i2c_send_stop;
	sc->sc_i2c.ic_initiate_xfer = jbusi2c_i2c_initiate_xfer;
	sc->sc_i2c.ic_read_byte = jbusi2c_i2c_read_byte;
	sc->sc_i2c.ic_write_byte = jbusi2c_i2c_write_byte;

	/* round up i2c devices */
	devs = OF_child(sc->sc_node);
	cfg = prop_array_create();
	prop_dictionary_set(dict, "i2c-child-devices", cfg);
	prop_object_release(cfg);
	while (devs != 0) {
		if (OF_getprop(devs, "name", name, 256) <= 0)
			goto skip;
		memset(compat, 0, sizeof(compat));
		if (OF_getprop(devs, "compatible",
		    compat, 255) <= 0)
			goto skip;
		if (OF_getprop(devs, "reg", regs, 8) <= 0)
			goto skip;
		if (regs[0] != 0) goto skip;
		addr = (regs[1] & 0xff) >> 1;
		DPRINTF("-> %s@%d,%x\n", name, regs[0], addr);
		dev = prop_dictionary_create();
		prop_dictionary_set_string(dev, "name", name);
		prop_dictionary_set_data(dev, "compatible", compat,
		    strlen(compat)+1);
		prop_dictionary_set_uint32(dev, "addr", addr);
		prop_dictionary_set_uint64(dev, "cookie", devs);
		prop_array_add(cfg, dev);
		prop_object_release(dev);
	skip:
		devs = OF_peer(devs);
	}
	memset(&iba, 0, sizeof(iba));
	iba.iba_tag = &sc->sc_i2c;
	config_found(sc->sc_dev, &iba, iicbus_print, CFARGS_NONE);
}

static inline void
jbusi2c_write(struct jbusi2c_softc *sc, int reg, uint64_t bits)
{
	bus_space_write_8(sc->sc_bustag, sc->sc_regh, reg, bits);
}

static inline uint64_t
jbusi2c_read(struct jbusi2c_softc *sc, int reg)
{
	return bus_space_read_8(sc->sc_bustag, sc->sc_regh, reg);
}

/* I2C bitbanging */
static void
jbusi2c_i2cbb_set_bits(void *cookie, uint32_t bits)
{
	struct jbusi2c_softc *sc = cookie;

	jbusi2c_write(sc, DATA, bits);
}

static void
jbusi2c_i2cbb_set_dir(void *cookie, uint32_t dir)
{
	struct jbusi2c_softc *sc = cookie;

	jbusi2c_write(sc, DIR, dir);
}

static uint32_t
jbusi2c_i2cbb_read(void *cookie)
{
	struct jbusi2c_softc *sc = cookie;

	return jbusi2c_read(sc, DATA);
}

/* higher level I2C stuff */
static int
jbusi2c_i2c_send_start(void *cookie, int flags)
{

	return i2c_bitbang_send_start(cookie, flags, &jbusi2c_i2cbb_ops);
}

static int
jbusi2c_i2c_send_stop(void *cookie, int flags)
{

	return i2c_bitbang_send_stop(cookie, flags, &jbusi2c_i2cbb_ops);
}

static int
jbusi2c_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
{

	return i2c_bitbang_initiate_xfer(cookie, addr, flags, 
	    &jbusi2c_i2cbb_ops);
}

static int
jbusi2c_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
{

	return i2c_bitbang_read_byte(cookie, valp, flags, &jbusi2c_i2cbb_ops);
}

static int
jbusi2c_i2c_write_byte(void *cookie, uint8_t val, int flags)
{

	return i2c_bitbang_write_byte(cookie, val, flags, &jbusi2c_i2cbb_ops);
}