iomdiic.c revision 1.7 1 1.7 skrll /* $NetBSD: iomdiic.c,v 1.7 2012/05/14 10:38:08 skrll Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 2003 Wasabi Systems, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.1 thorpej *
9 1.1 thorpej * Redistribution and use in source and binary forms, with or without
10 1.1 thorpej * modification, are permitted provided that the following conditions
11 1.1 thorpej * are met:
12 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer.
14 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.1 thorpej * documentation and/or other materials provided with the distribution.
17 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.1 thorpej * must display the following acknowledgement:
19 1.1 thorpej * This product includes software developed for the NetBSD Project by
20 1.1 thorpej * Wasabi Systems, Inc.
21 1.1 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 thorpej * or promote products derived from this software without specific prior
23 1.1 thorpej * written permission.
24 1.1 thorpej *
25 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
36 1.1 thorpej */
37 1.1 thorpej
38 1.1 thorpej #include <sys/param.h>
39 1.1 thorpej #include <sys/device.h>
40 1.1 thorpej #include <sys/kernel.h>
41 1.1 thorpej #include <sys/systm.h>
42 1.6 ad #include <sys/mutex.h>
43 1.6 ad #include <sys/bus.h>
44 1.6 ad #include <sys/cpu.h>
45 1.1 thorpej
46 1.1 thorpej #include <arm/iomd/iomdreg.h>
47 1.1 thorpej #include <arm/iomd/iomdvar.h>
48 1.1 thorpej
49 1.1 thorpej #include <dev/i2c/i2cvar.h>
50 1.1 thorpej #include <dev/i2c/i2c_bitbang.h>
51 1.1 thorpej
52 1.1 thorpej #include <arm/iomd/iomdiicvar.h>
53 1.1 thorpej
54 1.1 thorpej struct iomdiic_softc {
55 1.7 skrll device_t sc_dev;
56 1.1 thorpej bus_space_tag_t sc_st;
57 1.1 thorpej bus_space_handle_t sc_sh;
58 1.1 thorpej
59 1.1 thorpej struct i2c_controller sc_i2c;
60 1.6 ad kmutex_t sc_buslock;
61 1.1 thorpej
62 1.1 thorpej /*
63 1.1 thorpej * The SDA pin is open-drain, so we make it an input by
64 1.1 thorpej * writing a 1 to it.
65 1.1 thorpej */
66 1.1 thorpej uint8_t sc_iomd_iocr;
67 1.1 thorpej };
68 1.1 thorpej
69 1.1 thorpej static int iomdiic_acquire_bus(void *, int);
70 1.1 thorpej static void iomdiic_release_bus(void *, int);
71 1.1 thorpej
72 1.1 thorpej static int iomdiic_send_start(void *, int);
73 1.1 thorpej static int iomdiic_send_stop(void *, int);
74 1.1 thorpej static int iomdiic_initiate_xfer(void *, i2c_addr_t, int);
75 1.1 thorpej static int iomdiic_read_byte(void *, uint8_t *, int);
76 1.1 thorpej static int iomdiic_write_byte(void *, uint8_t, int);
77 1.1 thorpej
78 1.4 perry #define IOMDIIC_READ *(volatile uint8_t *)(IOMD_ADDRESS(IOMD_IOCR))
79 1.4 perry #define IOMDIIC_WRITE(x) *(volatile uint8_t *)(IOMD_ADDRESS(IOMD_IOCR)) = (x)
80 1.1 thorpej
81 1.1 thorpej #define IOMD_IOCR_SDA 0x01
82 1.1 thorpej #define IOMD_IOCR_SCL 0x02
83 1.1 thorpej
84 1.1 thorpej static void
85 1.1 thorpej iomdiic_bb_set_bits(void *cookie, uint32_t bits)
86 1.1 thorpej {
87 1.1 thorpej struct iomdiic_softc *sc = cookie;
88 1.1 thorpej
89 1.1 thorpej IOMDIIC_WRITE((IOMDIIC_READ & ~(IOMD_IOCR_SDA|IOMD_IOCR_SCL)) |
90 1.1 thorpej sc->sc_iomd_iocr | bits);
91 1.1 thorpej }
92 1.1 thorpej
93 1.1 thorpej static void
94 1.1 thorpej iomdiic_bb_set_dir(void *cookie, uint32_t bits)
95 1.1 thorpej {
96 1.1 thorpej struct iomdiic_softc *sc = cookie;
97 1.1 thorpej
98 1.1 thorpej sc->sc_iomd_iocr = bits;
99 1.1 thorpej }
100 1.1 thorpej
101 1.1 thorpej static uint32_t
102 1.1 thorpej iomdiic_bb_read_bits(void *cookie)
103 1.1 thorpej {
104 1.1 thorpej
105 1.1 thorpej return (IOMDIIC_READ);
106 1.1 thorpej }
107 1.1 thorpej
108 1.1 thorpej static const struct i2c_bitbang_ops iomdiic_bbops = {
109 1.1 thorpej iomdiic_bb_set_bits,
110 1.1 thorpej iomdiic_bb_set_dir,
111 1.1 thorpej iomdiic_bb_read_bits,
112 1.1 thorpej {
113 1.1 thorpej IOMD_IOCR_SDA, /* SDA */
114 1.1 thorpej IOMD_IOCR_SCL, /* SCL */
115 1.1 thorpej 0, /* SDA is output */
116 1.1 thorpej IOMD_IOCR_SDA, /* SDA is input */
117 1.1 thorpej }
118 1.1 thorpej };
119 1.1 thorpej
120 1.1 thorpej static int
121 1.7 skrll iomdiic_match(device_t parent, cfdata_t cf, void *aux)
122 1.1 thorpej {
123 1.2 reinoud struct iic_attach_args *ia = aux;
124 1.1 thorpej
125 1.2 reinoud if (strcmp(ia->ia_name, "iic") == 0) return 1;
126 1.2 reinoud return 0;
127 1.1 thorpej }
128 1.1 thorpej
129 1.1 thorpej static void
130 1.7 skrll iomdiic_attach(device_t parent, device_t self, void *aux)
131 1.1 thorpej {
132 1.7 skrll struct iomdiic_softc *sc = device_private(self);
133 1.1 thorpej struct i2cbus_attach_args iba;
134 1.1 thorpej
135 1.7 skrll aprint_normal("\n");
136 1.7 skrll
137 1.7 skrll sc->sc_dev = self;
138 1.1 thorpej
139 1.6 ad mutex_init(&sc->sc_buslock, MUTEX_DEFAULT, IPL_NONE);
140 1.1 thorpej
141 1.1 thorpej sc->sc_i2c.ic_cookie = sc;
142 1.1 thorpej sc->sc_i2c.ic_acquire_bus = iomdiic_acquire_bus;
143 1.1 thorpej sc->sc_i2c.ic_release_bus = iomdiic_release_bus;
144 1.1 thorpej sc->sc_i2c.ic_send_start = iomdiic_send_start;
145 1.1 thorpej sc->sc_i2c.ic_send_stop = iomdiic_send_stop;
146 1.1 thorpej sc->sc_i2c.ic_initiate_xfer = iomdiic_initiate_xfer;
147 1.1 thorpej sc->sc_i2c.ic_read_byte = iomdiic_read_byte;
148 1.1 thorpej sc->sc_i2c.ic_write_byte = iomdiic_write_byte;
149 1.1 thorpej
150 1.1 thorpej iba.iba_tag = &sc->sc_i2c;
151 1.7 skrll (void) config_found_ia(sc->sc_dev, "i2cbus", &iba, iicbus_print);
152 1.1 thorpej }
153 1.1 thorpej
154 1.7 skrll CFATTACH_DECL_NEW(iomdiic, sizeof(struct iomdiic_softc),
155 1.1 thorpej iomdiic_match, iomdiic_attach, NULL, NULL);
156 1.1 thorpej
157 1.1 thorpej i2c_tag_t
158 1.1 thorpej iomdiic_bootstrap_cookie(void)
159 1.1 thorpej {
160 1.1 thorpej static struct iomdiic_softc sc;
161 1.7 skrll static struct device dev;
162 1.1 thorpej
163 1.1 thorpej /* XXX Yuck. */
164 1.7 skrll strcpy(dev.dv_xname, "iomdiicboot");
165 1.1 thorpej
166 1.7 skrll sc.sc_dev = &dev;
167 1.1 thorpej sc.sc_i2c.ic_cookie = ≻
168 1.1 thorpej sc.sc_i2c.ic_acquire_bus = iomdiic_acquire_bus;
169 1.1 thorpej sc.sc_i2c.ic_release_bus = iomdiic_release_bus;
170 1.1 thorpej sc.sc_i2c.ic_send_start = iomdiic_send_start;
171 1.1 thorpej sc.sc_i2c.ic_send_stop = iomdiic_send_stop;
172 1.1 thorpej sc.sc_i2c.ic_initiate_xfer = iomdiic_initiate_xfer;
173 1.1 thorpej sc.sc_i2c.ic_read_byte = iomdiic_read_byte;
174 1.1 thorpej sc.sc_i2c.ic_write_byte = iomdiic_write_byte;
175 1.1 thorpej
176 1.1 thorpej return ((void *) &sc.sc_i2c);
177 1.1 thorpej }
178 1.1 thorpej
179 1.1 thorpej static int
180 1.1 thorpej iomdiic_acquire_bus(void *cookie, int flags)
181 1.1 thorpej {
182 1.1 thorpej struct iomdiic_softc *sc = cookie;
183 1.1 thorpej
184 1.1 thorpej /* XXX What should we do for the polling case? */
185 1.1 thorpej if (flags & I2C_F_POLL)
186 1.1 thorpej return (0);
187 1.1 thorpej
188 1.6 ad mutex_enter(&sc->sc_buslock);
189 1.6 ad return (0);
190 1.1 thorpej }
191 1.1 thorpej
192 1.1 thorpej static void
193 1.1 thorpej iomdiic_release_bus(void *cookie, int flags)
194 1.1 thorpej {
195 1.1 thorpej struct iomdiic_softc *sc = cookie;
196 1.1 thorpej
197 1.1 thorpej /* XXX See above. */
198 1.1 thorpej if (flags & I2C_F_POLL)
199 1.1 thorpej return;
200 1.1 thorpej
201 1.6 ad mutex_exit(&sc->sc_buslock);
202 1.1 thorpej }
203 1.1 thorpej
204 1.1 thorpej static int
205 1.1 thorpej iomdiic_send_start(void *cookie, int flags)
206 1.1 thorpej {
207 1.1 thorpej
208 1.1 thorpej return (i2c_bitbang_send_start(cookie, flags, &iomdiic_bbops));
209 1.1 thorpej }
210 1.1 thorpej
211 1.1 thorpej static int
212 1.1 thorpej iomdiic_send_stop(void *cookie, int flags)
213 1.1 thorpej {
214 1.1 thorpej
215 1.1 thorpej return (i2c_bitbang_send_stop(cookie, flags, &iomdiic_bbops));
216 1.1 thorpej }
217 1.1 thorpej
218 1.1 thorpej static int
219 1.1 thorpej iomdiic_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
220 1.1 thorpej {
221 1.1 thorpej
222 1.1 thorpej return (i2c_bitbang_initiate_xfer(cookie, addr, flags, &iomdiic_bbops));
223 1.1 thorpej }
224 1.1 thorpej
225 1.1 thorpej static int
226 1.1 thorpej iomdiic_read_byte(void *cookie, uint8_t *bytep, int flags)
227 1.1 thorpej {
228 1.1 thorpej
229 1.1 thorpej return (i2c_bitbang_read_byte(cookie, bytep, flags, &iomdiic_bbops));
230 1.1 thorpej }
231 1.1 thorpej
232 1.1 thorpej static int
233 1.1 thorpej iomdiic_write_byte(void *cookie, uint8_t byte, int flags)
234 1.1 thorpej {
235 1.1 thorpej
236 1.1 thorpej return (i2c_bitbang_write_byte(cookie, byte, flags, &iomdiic_bbops));
237 1.1 thorpej }
238