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