1 /* $NetBSD: iociic.c,v 1.7 2009/01/07 22:58:38 bjh21 Exp $ */
4 * Copyright (c) 2003 Wasabi Systems, Inc.
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/param.h>
39 #include <sys/device.h>
40 #include <sys/kernel.h>
41 #include <sys/systm.h>
42 #include <sys/mutex.h>
44 #include <machine/bus.h>
45 #include <machine/cpu.h>
47 #include <acorn26/iobus/iocreg.h>
48 #include <acorn26/iobus/iocvar.h>
50 #include <dev/i2c/i2cvar.h>
51 #include <dev/i2c/i2c_bitbang.h>
53 #include <acorn26/ioc/iociicvar.h>
57 bus_space_tag_t sc_st
;
58 bus_space_handle_t sc_sh
;
60 struct i2c_controller sc_i2c
;
64 * The SDA pin is open-drain, so we make it an input by
70 static struct iociic_softc
*the_iociic
;
72 static int iociic_acquire_bus(void *, int);
73 static void iociic_release_bus(void *, int);
75 static int iociic_send_start(void *, int);
76 static int iociic_send_stop(void *, int);
77 static int iociic_initiate_xfer(void *, i2c_addr_t
, int);
78 static int iociic_read_byte(void *, uint8_t *, int);
79 static int iociic_write_byte(void *, uint8_t, int);
82 iociic_bb_set_bits(void *cookie
, uint32_t bits
)
84 struct iociic_softc
*sc
= cookie
;
86 ioc_ctl_write(device_parent(sc
->sc_dev
), sc
->sc_ioc_ctl
| bits
,
87 IOC_CTL_SDA
| IOC_CTL_SCL
);
91 iociic_bb_set_dir(void *cookie
, uint32_t bits
)
93 struct iociic_softc
*sc
= cookie
;
95 sc
->sc_ioc_ctl
= bits
;
99 iociic_bb_read_bits(void *cookie
)
101 struct iociic_softc
*sc
= cookie
;
103 return ioc_ctl_read(device_parent(sc
->sc_dev
));
106 static const struct i2c_bitbang_ops iociic_bbops
= {
111 IOC_CTL_SDA
, /* SDA */
112 IOC_CTL_SCL
, /* SCL */
113 0, /* SDA is output */
114 IOC_CTL_SDA
, /* SDA is input */
119 iociic_match(device_t parent
, cfdata_t cf
, void *aux
)
126 iociic_attach(device_t parent
, device_t self
, void *aux
)
128 struct iociic_softc
*sc
= device_private(self
);
129 struct i2cbus_attach_args iba
;
132 if (the_iociic
== NULL
)
137 mutex_init(&sc
->sc_buslock
, MUTEX_DEFAULT
, IPL_NONE
);
139 sc
->sc_i2c
.ic_cookie
= sc
;
140 sc
->sc_i2c
.ic_acquire_bus
= iociic_acquire_bus
;
141 sc
->sc_i2c
.ic_release_bus
= iociic_release_bus
;
142 sc
->sc_i2c
.ic_send_start
= iociic_send_start
;
143 sc
->sc_i2c
.ic_send_stop
= iociic_send_stop
;
144 sc
->sc_i2c
.ic_initiate_xfer
= iociic_initiate_xfer
;
145 sc
->sc_i2c
.ic_read_byte
= iociic_read_byte
;
146 sc
->sc_i2c
.ic_write_byte
= iociic_write_byte
;
148 iba
.iba_tag
= &sc
->sc_i2c
;
149 (void) config_found_ia(self
, "i2cbus", &iba
, iicbus_print
);
152 CFATTACH_DECL_NEW(iociic
, sizeof(struct iociic_softc
),
153 iociic_match
, iociic_attach
, NULL
, NULL
);
156 iociic_bootstrap_cookie(void)
159 if (the_iociic
== NULL
)
161 return &the_iociic
->sc_i2c
;
165 iociic_acquire_bus(void *cookie
, int flags
)
167 struct iociic_softc
*sc
= cookie
;
169 /* XXX What should we do for the polling case? */
170 if (flags
& I2C_F_POLL
)
173 mutex_enter(&sc
->sc_buslock
);
178 iociic_release_bus(void *cookie
, int flags
)
180 struct iociic_softc
*sc
= cookie
;
183 if (flags
& I2C_F_POLL
)
186 mutex_exit(&sc
->sc_buslock
);
190 iociic_send_start(void *cookie
, int flags
)
193 return i2c_bitbang_send_start(cookie
, flags
, &iociic_bbops
);
197 iociic_send_stop(void *cookie
, int flags
)
200 return (i2c_bitbang_send_stop(cookie
, flags
, &iociic_bbops
));
204 iociic_initiate_xfer(void *cookie
, i2c_addr_t addr
, int flags
)
207 return i2c_bitbang_initiate_xfer(cookie
, addr
, flags
, &iociic_bbops
);
211 iociic_read_byte(void *cookie
, uint8_t *bytep
, int flags
)
214 return i2c_bitbang_read_byte(cookie
, bytep
, flags
, &iociic_bbops
);
218 iociic_write_byte(void *cookie
, uint8_t byte
, int flags
)
221 return i2c_bitbang_write_byte(cookie
, byte
, flags
, &iociic_bbops
);