gpiic_opb.c revision 1.7 1 1.7 kiyohara /* $NetBSD: gpiic_opb.c,v 1.7 2011/06/12 07:19:49 kiyohara Exp $ */
2 1.1 scw
3 1.1 scw /*
4 1.1 scw * Copyright 2002, 2003 Wasabi Systems, Inc.
5 1.1 scw * All rights reserved.
6 1.1 scw *
7 1.1 scw * Written by Steve C. Woodford for Wasabi Systems, Inc.
8 1.1 scw *
9 1.1 scw * Redistribution and use in source and binary forms, with or without
10 1.1 scw * modification, are permitted provided that the following conditions
11 1.1 scw * are met:
12 1.1 scw * 1. Redistributions of source code must retain the above copyright
13 1.1 scw * notice, this list of conditions and the following disclaimer.
14 1.1 scw * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 scw * notice, this list of conditions and the following disclaimer in the
16 1.1 scw * documentation and/or other materials provided with the distribution.
17 1.1 scw * 3. All advertising materials mentioning features or use of this software
18 1.1 scw * must display the following acknowledgement:
19 1.1 scw * This product includes software developed for the NetBSD Project by
20 1.1 scw * Wasabi Systems, Inc.
21 1.1 scw * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 scw * or promote products derived from this software without specific prior
23 1.1 scw * written permission.
24 1.1 scw *
25 1.1 scw * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 scw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 scw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 scw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 scw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 scw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 scw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 scw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 scw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 scw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 scw * POSSIBILITY OF SUCH DAMAGE.
36 1.1 scw */
37 1.1 scw
38 1.1 scw #include "locators.h"
39 1.1 scw
40 1.1 scw #include <sys/param.h>
41 1.1 scw #include <sys/systm.h>
42 1.1 scw #include <sys/device.h>
43 1.1 scw #include <sys/errno.h>
44 1.5 ad #include <sys/mutex.h>
45 1.5 ad #include <sys/cpu.h>
46 1.1 scw
47 1.2 scw #include <dev/i2c/i2cvar.h>
48 1.2 scw #include <dev/i2c/i2c_bitbang.h>
49 1.1 scw
50 1.1 scw #include <powerpc/ibm4xx/dev/opbvar.h>
51 1.1 scw #include <powerpc/ibm4xx/dev/gpiicreg.h>
52 1.1 scw
53 1.1 scw struct gpiic_softc {
54 1.1 scw struct device sc_dev;
55 1.1 scw bus_space_tag_t sc_bust;
56 1.1 scw bus_space_handle_t sc_bush;
57 1.1 scw uint8_t sc_txen;
58 1.6 tsutsui uint8_t sc_tx;
59 1.1 scw struct i2c_controller sc_i2c;
60 1.1 scw struct i2c_bitbang_ops sc_bops;
61 1.5 ad kmutex_t sc_buslock;
62 1.1 scw };
63 1.1 scw
64 1.1 scw static int gpiic_match(struct device *, struct cfdata *, void *);
65 1.1 scw static void gpiic_attach(struct device *, struct device *, void *);
66 1.1 scw
67 1.1 scw CFATTACH_DECL(gpiic, sizeof(struct gpiic_softc),
68 1.1 scw gpiic_match, gpiic_attach, NULL, NULL);
69 1.1 scw
70 1.1 scw static int gpiic_acquire_bus(void *, int);
71 1.1 scw static void gpiic_release_bus(void *, int);
72 1.1 scw static int gpiic_send_start(void *, int);
73 1.1 scw static int gpiic_send_stop(void *, int);
74 1.1 scw static int gpiic_initiate_xfer(void *, i2c_addr_t, int);
75 1.1 scw static int gpiic_read_byte(void *, uint8_t *, int);
76 1.1 scw static int gpiic_write_byte(void *, uint8_t, int);
77 1.1 scw static void gpiic_set_dir(void *, uint32_t);
78 1.1 scw static void gpiic_set_bits(void *, uint32_t);
79 1.1 scw static uint32_t gpiic_read_bits(void *);
80 1.1 scw
81 1.1 scw static int
82 1.1 scw gpiic_match(struct device *parent, struct cfdata *cf, void *args)
83 1.1 scw {
84 1.1 scw struct opb_attach_args *oaa = args;
85 1.1 scw
86 1.1 scw if (strcmp(oaa->opb_name, cf->cf_name) != 0)
87 1.1 scw return 0;
88 1.1 scw
89 1.1 scw return (1);
90 1.1 scw }
91 1.1 scw
92 1.1 scw static void
93 1.1 scw gpiic_attach(struct device *parent, struct device *self, void *args)
94 1.1 scw {
95 1.1 scw struct gpiic_softc *sc = (struct gpiic_softc *)self;
96 1.1 scw struct opb_attach_args *oaa = args;
97 1.1 scw struct i2cbus_attach_args iba;
98 1.1 scw
99 1.1 scw aprint_naive(": IIC controller\n");
100 1.1 scw aprint_normal(": On-Chip IIC controller\n");
101 1.1 scw
102 1.1 scw sc->sc_bust = oaa->opb_bt;
103 1.1 scw
104 1.1 scw bus_space_map(sc->sc_bust, oaa->opb_addr, IIC_NREG, 0, &sc->sc_bush);
105 1.1 scw
106 1.5 ad mutex_init(&sc->sc_buslock, MUTEX_DEFAULT, IPL_NONE);
107 1.1 scw
108 1.1 scw sc->sc_txen = 0;
109 1.6 tsutsui sc->sc_tx = IIC_DIRECTCNTL_SCC | IIC_DIRECTCNTL_SDAC;
110 1.1 scw sc->sc_i2c.ic_cookie = sc;
111 1.1 scw sc->sc_i2c.ic_acquire_bus = gpiic_acquire_bus;
112 1.1 scw sc->sc_i2c.ic_release_bus = gpiic_release_bus;
113 1.1 scw sc->sc_i2c.ic_exec = NULL;
114 1.1 scw sc->sc_i2c.ic_send_start = gpiic_send_start;
115 1.1 scw sc->sc_i2c.ic_send_stop = gpiic_send_stop;
116 1.1 scw sc->sc_i2c.ic_initiate_xfer = gpiic_initiate_xfer;
117 1.1 scw sc->sc_i2c.ic_read_byte = gpiic_read_byte;
118 1.1 scw sc->sc_i2c.ic_write_byte = gpiic_write_byte;
119 1.1 scw
120 1.1 scw sc->sc_bops.ibo_set_dir = gpiic_set_dir;
121 1.1 scw sc->sc_bops.ibo_set_bits = gpiic_set_bits;
122 1.1 scw sc->sc_bops.ibo_read_bits = gpiic_read_bits;
123 1.1 scw sc->sc_bops.ibo_bits[I2C_BIT_SDA] = IIC_DIRECTCNTL_SDAC;
124 1.1 scw sc->sc_bops.ibo_bits[I2C_BIT_SCL] = IIC_DIRECTCNTL_SCC;
125 1.1 scw sc->sc_bops.ibo_bits[I2C_BIT_OUTPUT] = 1;
126 1.1 scw sc->sc_bops.ibo_bits[I2C_BIT_INPUT] = 0;
127 1.1 scw
128 1.1 scw /*
129 1.1 scw * Put the controller into Soft Reset. This allows us to
130 1.1 scw * manually bit-bang the I2C clock/data lines.
131 1.1 scw */
132 1.1 scw bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_XTCNTLSS,
133 1.1 scw IIC_XTCNTLSS_SRST);
134 1.1 scw delay(10);
135 1.1 scw bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL,
136 1.1 scw IIC_DIRECTCNTL_SCC | IIC_DIRECTCNTL_SDAC);
137 1.1 scw
138 1.7 kiyohara memset(&iba, 0, sizeof(iba));
139 1.1 scw iba.iba_tag = &sc->sc_i2c;
140 1.4 drochner (void) config_found_ia(&sc->sc_dev, "i2cbus", &iba, iicbus_print);
141 1.1 scw }
142 1.1 scw
143 1.1 scw static int
144 1.1 scw gpiic_acquire_bus(void *arg, int flags)
145 1.1 scw {
146 1.1 scw struct gpiic_softc *sc = arg;
147 1.1 scw
148 1.1 scw if (flags & I2C_F_POLL)
149 1.1 scw return (0);
150 1.1 scw
151 1.5 ad mutex_enter(&sc->sc_buslock);
152 1.5 ad return (0);
153 1.1 scw }
154 1.1 scw
155 1.1 scw static void
156 1.1 scw gpiic_release_bus(void *arg, int flags)
157 1.1 scw {
158 1.1 scw struct gpiic_softc *sc = arg;
159 1.1 scw
160 1.1 scw if (flags & I2C_F_POLL)
161 1.1 scw return;
162 1.1 scw
163 1.5 ad mutex_exit(&sc->sc_buslock);
164 1.1 scw }
165 1.1 scw
166 1.1 scw static int
167 1.1 scw gpiic_send_start(void *arg, int flags)
168 1.1 scw {
169 1.1 scw struct gpiic_softc *sc = arg;
170 1.1 scw
171 1.1 scw return (i2c_bitbang_send_start(sc, flags, &sc->sc_bops));
172 1.1 scw }
173 1.1 scw
174 1.1 scw static int
175 1.1 scw gpiic_send_stop(void *arg, int flags)
176 1.1 scw {
177 1.1 scw struct gpiic_softc *sc = arg;
178 1.1 scw
179 1.1 scw return (i2c_bitbang_send_stop(sc, flags, &sc->sc_bops));
180 1.1 scw }
181 1.1 scw
182 1.1 scw static int
183 1.1 scw gpiic_initiate_xfer(void *arg, i2c_addr_t addr, int flags)
184 1.1 scw {
185 1.1 scw struct gpiic_softc *sc = arg;
186 1.1 scw
187 1.1 scw return (i2c_bitbang_initiate_xfer(sc, addr, flags, &sc->sc_bops));
188 1.1 scw }
189 1.1 scw
190 1.1 scw static int
191 1.1 scw gpiic_read_byte(void *arg, uint8_t *vp, int flags)
192 1.1 scw {
193 1.1 scw struct gpiic_softc *sc = arg;
194 1.1 scw
195 1.1 scw return (i2c_bitbang_read_byte(sc, vp, flags, &sc->sc_bops));
196 1.1 scw }
197 1.1 scw
198 1.1 scw static int
199 1.1 scw gpiic_write_byte(void *arg, uint8_t v, int flags)
200 1.1 scw {
201 1.1 scw struct gpiic_softc *sc = arg;
202 1.1 scw
203 1.1 scw return (i2c_bitbang_write_byte(sc, v, flags, &sc->sc_bops));
204 1.1 scw }
205 1.1 scw
206 1.1 scw static void
207 1.1 scw gpiic_set_dir(void *arg, uint32_t bits)
208 1.1 scw {
209 1.1 scw struct gpiic_softc *sc = arg;
210 1.6 tsutsui uint8_t tx, txen;
211 1.1 scw
212 1.6 tsutsui txen = (uint8_t)bits;
213 1.6 tsutsui if (sc->sc_txen == txen)
214 1.6 tsutsui return;
215 1.6 tsutsui
216 1.6 tsutsui sc->sc_txen = txen;
217 1.6 tsutsui
218 1.6 tsutsui tx = sc->sc_tx;
219 1.6 tsutsui if (sc->sc_txen == 0)
220 1.6 tsutsui tx |= IIC_DIRECTCNTL_SDAC;
221 1.6 tsutsui bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL, tx);
222 1.1 scw }
223 1.1 scw
224 1.1 scw static void
225 1.1 scw gpiic_set_bits(void *arg, uint32_t bits)
226 1.1 scw {
227 1.1 scw struct gpiic_softc *sc = arg;
228 1.1 scw
229 1.6 tsutsui sc->sc_tx = (uint8_t)bits;
230 1.6 tsutsui if (sc->sc_txen == 0)
231 1.1 scw bits |= IIC_DIRECTCNTL_SDAC;
232 1.1 scw
233 1.1 scw bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL, bits);
234 1.1 scw }
235 1.1 scw
236 1.1 scw static uint32_t
237 1.1 scw gpiic_read_bits(void *arg)
238 1.1 scw {
239 1.1 scw struct gpiic_softc *sc = arg;
240 1.1 scw uint8_t rv;
241 1.1 scw
242 1.1 scw rv = bus_space_read_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL) << 2;
243 1.1 scw rv &= (IIC_DIRECTCNTL_SCC | IIC_DIRECTCNTL_SDAC);
244 1.1 scw
245 1.1 scw return ((uint32_t)rv);
246 1.1 scw }
247