gpiic_opb.c revision 1.10 1 1.10 thorpej /* $NetBSD: gpiic_opb.c,v 1.10 2019/12/22 23:23:31 thorpej 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.9 matt #include <powerpc/ibm4xx/cpu.h>
51 1.1 scw #include <powerpc/ibm4xx/dev/opbvar.h>
52 1.1 scw #include <powerpc/ibm4xx/dev/gpiicreg.h>
53 1.1 scw
54 1.1 scw struct gpiic_softc {
55 1.8 matt device_t sc_dev;
56 1.1 scw bus_space_tag_t sc_bust;
57 1.1 scw bus_space_handle_t sc_bush;
58 1.1 scw uint8_t sc_txen;
59 1.6 tsutsui uint8_t sc_tx;
60 1.1 scw struct i2c_controller sc_i2c;
61 1.1 scw struct i2c_bitbang_ops sc_bops;
62 1.1 scw };
63 1.1 scw
64 1.8 matt static int gpiic_match(device_t, cfdata_t, void *);
65 1.8 matt static void gpiic_attach(device_t, device_t, void *);
66 1.1 scw
67 1.8 matt CFATTACH_DECL_NEW(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_send_start(void *, int);
71 1.1 scw static int gpiic_send_stop(void *, int);
72 1.1 scw static int gpiic_initiate_xfer(void *, i2c_addr_t, int);
73 1.1 scw static int gpiic_read_byte(void *, uint8_t *, int);
74 1.1 scw static int gpiic_write_byte(void *, uint8_t, int);
75 1.1 scw static void gpiic_set_dir(void *, uint32_t);
76 1.1 scw static void gpiic_set_bits(void *, uint32_t);
77 1.1 scw static uint32_t gpiic_read_bits(void *);
78 1.1 scw
79 1.1 scw static int
80 1.8 matt gpiic_match(device_t parent, cfdata_t cf, void *args)
81 1.1 scw {
82 1.8 matt struct opb_attach_args * const oaa = args;
83 1.1 scw
84 1.1 scw if (strcmp(oaa->opb_name, cf->cf_name) != 0)
85 1.1 scw return 0;
86 1.1 scw
87 1.1 scw return (1);
88 1.1 scw }
89 1.1 scw
90 1.1 scw static void
91 1.8 matt gpiic_attach(device_t parent, device_t self, void *args)
92 1.1 scw {
93 1.8 matt struct gpiic_softc * const sc = device_private(self);
94 1.8 matt struct opb_attach_args * const oaa = args;
95 1.1 scw struct i2cbus_attach_args iba;
96 1.1 scw
97 1.1 scw aprint_naive(": IIC controller\n");
98 1.1 scw aprint_normal(": On-Chip IIC controller\n");
99 1.1 scw
100 1.8 matt sc->sc_dev = self;
101 1.1 scw sc->sc_bust = oaa->opb_bt;
102 1.1 scw
103 1.1 scw bus_space_map(sc->sc_bust, oaa->opb_addr, IIC_NREG, 0, &sc->sc_bush);
104 1.1 scw
105 1.1 scw sc->sc_txen = 0;
106 1.6 tsutsui sc->sc_tx = IIC_DIRECTCNTL_SCC | IIC_DIRECTCNTL_SDAC;
107 1.10 thorpej iic_tag_init(&sc->sc_i2c);
108 1.1 scw sc->sc_i2c.ic_cookie = sc;
109 1.1 scw sc->sc_i2c.ic_send_start = gpiic_send_start;
110 1.1 scw sc->sc_i2c.ic_send_stop = gpiic_send_stop;
111 1.1 scw sc->sc_i2c.ic_initiate_xfer = gpiic_initiate_xfer;
112 1.1 scw sc->sc_i2c.ic_read_byte = gpiic_read_byte;
113 1.1 scw sc->sc_i2c.ic_write_byte = gpiic_write_byte;
114 1.1 scw
115 1.1 scw sc->sc_bops.ibo_set_dir = gpiic_set_dir;
116 1.1 scw sc->sc_bops.ibo_set_bits = gpiic_set_bits;
117 1.1 scw sc->sc_bops.ibo_read_bits = gpiic_read_bits;
118 1.1 scw sc->sc_bops.ibo_bits[I2C_BIT_SDA] = IIC_DIRECTCNTL_SDAC;
119 1.1 scw sc->sc_bops.ibo_bits[I2C_BIT_SCL] = IIC_DIRECTCNTL_SCC;
120 1.1 scw sc->sc_bops.ibo_bits[I2C_BIT_OUTPUT] = 1;
121 1.1 scw sc->sc_bops.ibo_bits[I2C_BIT_INPUT] = 0;
122 1.1 scw
123 1.1 scw /*
124 1.1 scw * Put the controller into Soft Reset. This allows us to
125 1.1 scw * manually bit-bang the I2C clock/data lines.
126 1.1 scw */
127 1.1 scw bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_XTCNTLSS,
128 1.1 scw IIC_XTCNTLSS_SRST);
129 1.1 scw delay(10);
130 1.1 scw bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL,
131 1.1 scw IIC_DIRECTCNTL_SCC | IIC_DIRECTCNTL_SDAC);
132 1.1 scw
133 1.7 kiyohara memset(&iba, 0, sizeof(iba));
134 1.1 scw iba.iba_tag = &sc->sc_i2c;
135 1.8 matt (void) config_found_ia(self, "i2cbus", &iba, iicbus_print);
136 1.1 scw }
137 1.1 scw
138 1.1 scw static int
139 1.1 scw gpiic_send_start(void *arg, int flags)
140 1.1 scw {
141 1.8 matt struct gpiic_softc * const sc = arg;
142 1.1 scw
143 1.1 scw return (i2c_bitbang_send_start(sc, flags, &sc->sc_bops));
144 1.1 scw }
145 1.1 scw
146 1.1 scw static int
147 1.1 scw gpiic_send_stop(void *arg, int flags)
148 1.1 scw {
149 1.8 matt struct gpiic_softc * const sc = arg;
150 1.1 scw
151 1.1 scw return (i2c_bitbang_send_stop(sc, flags, &sc->sc_bops));
152 1.1 scw }
153 1.1 scw
154 1.1 scw static int
155 1.1 scw gpiic_initiate_xfer(void *arg, i2c_addr_t addr, int flags)
156 1.1 scw {
157 1.8 matt struct gpiic_softc * const sc = arg;
158 1.1 scw
159 1.1 scw return (i2c_bitbang_initiate_xfer(sc, addr, flags, &sc->sc_bops));
160 1.1 scw }
161 1.1 scw
162 1.1 scw static int
163 1.1 scw gpiic_read_byte(void *arg, uint8_t *vp, int flags)
164 1.1 scw {
165 1.8 matt struct gpiic_softc * const sc = arg;
166 1.1 scw
167 1.1 scw return (i2c_bitbang_read_byte(sc, vp, flags, &sc->sc_bops));
168 1.1 scw }
169 1.1 scw
170 1.1 scw static int
171 1.1 scw gpiic_write_byte(void *arg, uint8_t v, int flags)
172 1.1 scw {
173 1.8 matt struct gpiic_softc * const sc = arg;
174 1.1 scw
175 1.1 scw return (i2c_bitbang_write_byte(sc, v, flags, &sc->sc_bops));
176 1.1 scw }
177 1.1 scw
178 1.1 scw static void
179 1.1 scw gpiic_set_dir(void *arg, uint32_t bits)
180 1.1 scw {
181 1.8 matt struct gpiic_softc * const sc = arg;
182 1.6 tsutsui uint8_t tx, txen;
183 1.1 scw
184 1.6 tsutsui txen = (uint8_t)bits;
185 1.6 tsutsui if (sc->sc_txen == txen)
186 1.6 tsutsui return;
187 1.6 tsutsui
188 1.6 tsutsui sc->sc_txen = txen;
189 1.6 tsutsui
190 1.6 tsutsui tx = sc->sc_tx;
191 1.6 tsutsui if (sc->sc_txen == 0)
192 1.6 tsutsui tx |= IIC_DIRECTCNTL_SDAC;
193 1.6 tsutsui bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL, tx);
194 1.1 scw }
195 1.1 scw
196 1.1 scw static void
197 1.1 scw gpiic_set_bits(void *arg, uint32_t bits)
198 1.1 scw {
199 1.8 matt struct gpiic_softc * const sc = arg;
200 1.1 scw
201 1.6 tsutsui sc->sc_tx = (uint8_t)bits;
202 1.6 tsutsui if (sc->sc_txen == 0)
203 1.1 scw bits |= IIC_DIRECTCNTL_SDAC;
204 1.1 scw
205 1.1 scw bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL, bits);
206 1.1 scw }
207 1.1 scw
208 1.1 scw static uint32_t
209 1.1 scw gpiic_read_bits(void *arg)
210 1.1 scw {
211 1.8 matt struct gpiic_softc * const sc = arg;
212 1.1 scw uint8_t rv;
213 1.1 scw
214 1.1 scw rv = bus_space_read_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL) << 2;
215 1.1 scw rv &= (IIC_DIRECTCNTL_SCC | IIC_DIRECTCNTL_SDAC);
216 1.1 scw
217 1.1 scw return ((uint32_t)rv);
218 1.1 scw }
219