xref: /netbsd-src/sys/arch/alpha/pci/tsciic.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: tsciic.c,v 1.1 2014/02/21 12:23:30 jdc Exp $	*/
2 
3 /*
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Julian Coleman.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 
34 __KERNEL_RCSID(0, "$NetBSD: tsciic.c,v 1.1 2014/02/21 12:23:30 jdc Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 
40 #include <alpha/pci/tsreg.h>
41 #include <alpha/pci/tsvar.h>
42 
43 #include <dev/i2c/i2cvar.h>
44 #include <dev/i2c/i2c_bitbang.h>
45 #include <dev/i2c/ddcvar.h>
46 
47 /* I2C glue */
48 static int tsciic_acquire_bus(void *, int);
49 static void tsciic_release_bus(void *, int);
50 static int tsciic_send_start(void *, int);
51 static int tsciic_send_stop(void *, int);
52 static int tsciic_initiate_xfer(void *, i2c_addr_t, int);
53 static int tsciic_read_byte(void *, uint8_t *, int);
54 static int tsciic_write_byte(void *, uint8_t, int);
55 
56 /* I2C bitbang glue */
57 static void tsciicbb_set_bits(void *, uint32_t);
58 static void tsciicbb_set_dir(void *, uint32_t);
59 static uint32_t tsciicbb_read(void *);
60 
61 #define MPD_BIT_SDA 0x01
62 #define MPD_BIT_SCL 0x02
63 static const struct i2c_bitbang_ops tsciicbb_ops = {
64 	tsciicbb_set_bits,
65 	tsciicbb_set_dir,
66 	tsciicbb_read,
67 	{
68 		MPD_BIT_SDA,
69 		MPD_BIT_SCL,
70 		0,
71 		0
72 	}
73 };
74 
75 void
76 tsciic_init(device_t self) {
77 	struct tsciic_softc *sc = device_private(self);
78 	struct i2cbus_attach_args iba;
79 
80 	mutex_init(&sc->sc_buslock, MUTEX_DEFAULT, IPL_NONE);
81 
82 	sc->sc_i2c.ic_cookie = sc;
83 	sc->sc_i2c.ic_acquire_bus = tsciic_acquire_bus;
84 	sc->sc_i2c.ic_release_bus = tsciic_release_bus;
85 	sc->sc_i2c.ic_send_start = tsciic_send_start;
86 	sc->sc_i2c.ic_send_stop = tsciic_send_stop;
87 	sc->sc_i2c.ic_initiate_xfer = tsciic_initiate_xfer;
88 	sc->sc_i2c.ic_read_byte = tsciic_read_byte;
89 	sc->sc_i2c.ic_write_byte = tsciic_write_byte;
90 	sc->sc_i2c.ic_exec = NULL;
91 
92 	memset(&iba, 0, sizeof(iba));
93 	iba.iba_tag = &sc->sc_i2c;
94 
95 	config_found_ia(self, "i2cbus", &iba, iicbus_print);
96 
97 }
98 
99 /* I2C bitbanging */
100 static void
101 tsciicbb_set_bits(void *cookie, uint32_t bits)
102 {
103 	uint64_t val;
104 
105 	val = (bits & MPD_BIT_SDA ? MPD_DS : 0) |
106 	    (bits & MPD_BIT_SCL ? MPD_CKS : 0);
107 	alpha_mb();
108 	STQP(TS_C_MPD) = val;
109 	alpha_mb();
110 }
111 
112 static void
113 tsciicbb_set_dir(void *cookie, uint32_t dir)
114 {
115 	/* Nothing to do */
116 }
117 
118 static uint32_t
119 tsciicbb_read(void *cookie)
120 {
121 	uint64_t val;
122 	uint32_t bits;
123 
124 	val = LDQP(TS_C_MPD);
125 	bits = (val & MPD_DR ? MPD_BIT_SDA : 0) |
126 	    (val & MPD_CKR ? MPD_BIT_SCL : 0);
127 	return bits;
128 }
129 
130 /* higher level I2C stuff */
131 static int
132 tsciic_acquire_bus(void *cookie, int flags)
133 {
134 	struct tsciic_softc *sc = cookie;
135 
136 	mutex_enter(&sc->sc_buslock);
137 	return 0;
138 }
139 
140 static void
141 tsciic_release_bus(void *cookie, int flags)
142 {
143 	struct tsciic_softc *sc = cookie;
144 
145 	mutex_exit(&sc->sc_buslock);
146 }
147 
148 static int
149 tsciic_send_start(void *cookie, int flags)
150 {
151 	return (i2c_bitbang_send_start(cookie, flags, &tsciicbb_ops));
152 }
153 
154 static int
155 tsciic_send_stop(void *cookie, int flags)
156 {
157 	return (i2c_bitbang_send_stop(cookie, flags, &tsciicbb_ops));
158 }
159 
160 static int
161 tsciic_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
162 {
163 	return (i2c_bitbang_initiate_xfer(cookie, addr, flags,
164 	    &tsciicbb_ops));
165 }
166 
167 static int
168 tsciic_read_byte(void *cookie, uint8_t *valp, int flags)
169 {
170 	return (i2c_bitbang_read_byte(cookie, valp, flags, &tsciicbb_ops));
171 }
172 
173 static int
174 tsciic_write_byte(void *cookie, uint8_t val, int flags)
175 {
176 	return (i2c_bitbang_write_byte(cookie, val, flags, &tsciicbb_ops));
177 }
178