iomdiic.c revision 1.12 1 1.12 riastrad /* $NetBSD: iomdiic.c,v 1.12 2022/03/28 12:38:57 riastradh 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.12 riastrad #include <sys/device_impl.h> /* XXX autoconf abuse */
41 1.1 thorpej #include <sys/kernel.h>
42 1.1 thorpej #include <sys/systm.h>
43 1.6 ad #include <sys/mutex.h>
44 1.6 ad #include <sys/bus.h>
45 1.6 ad #include <sys/cpu.h>
46 1.1 thorpej
47 1.1 thorpej #include <arm/iomd/iomdreg.h>
48 1.1 thorpej #include <arm/iomd/iomdvar.h>
49 1.1 thorpej
50 1.1 thorpej #include <dev/i2c/i2cvar.h>
51 1.1 thorpej #include <dev/i2c/i2c_bitbang.h>
52 1.1 thorpej
53 1.1 thorpej #include <arm/iomd/iomdiicvar.h>
54 1.1 thorpej
55 1.1 thorpej struct iomdiic_softc {
56 1.7 skrll device_t sc_dev;
57 1.1 thorpej bus_space_tag_t sc_st;
58 1.1 thorpej bus_space_handle_t sc_sh;
59 1.1 thorpej
60 1.1 thorpej struct i2c_controller sc_i2c;
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_send_start(void *, int);
70 1.1 thorpej static int iomdiic_send_stop(void *, int);
71 1.1 thorpej static int iomdiic_initiate_xfer(void *, i2c_addr_t, int);
72 1.1 thorpej static int iomdiic_read_byte(void *, uint8_t *, int);
73 1.1 thorpej static int iomdiic_write_byte(void *, uint8_t, int);
74 1.1 thorpej
75 1.4 perry #define IOMDIIC_READ *(volatile uint8_t *)(IOMD_ADDRESS(IOMD_IOCR))
76 1.4 perry #define IOMDIIC_WRITE(x) *(volatile uint8_t *)(IOMD_ADDRESS(IOMD_IOCR)) = (x)
77 1.1 thorpej
78 1.1 thorpej #define IOMD_IOCR_SDA 0x01
79 1.1 thorpej #define IOMD_IOCR_SCL 0x02
80 1.1 thorpej
81 1.1 thorpej static void
82 1.1 thorpej iomdiic_bb_set_bits(void *cookie, uint32_t bits)
83 1.1 thorpej {
84 1.1 thorpej struct iomdiic_softc *sc = cookie;
85 1.1 thorpej
86 1.1 thorpej IOMDIIC_WRITE((IOMDIIC_READ & ~(IOMD_IOCR_SDA|IOMD_IOCR_SCL)) |
87 1.1 thorpej sc->sc_iomd_iocr | bits);
88 1.1 thorpej }
89 1.1 thorpej
90 1.1 thorpej static void
91 1.1 thorpej iomdiic_bb_set_dir(void *cookie, uint32_t bits)
92 1.1 thorpej {
93 1.1 thorpej struct iomdiic_softc *sc = cookie;
94 1.1 thorpej
95 1.1 thorpej sc->sc_iomd_iocr = bits;
96 1.1 thorpej }
97 1.1 thorpej
98 1.1 thorpej static uint32_t
99 1.1 thorpej iomdiic_bb_read_bits(void *cookie)
100 1.1 thorpej {
101 1.1 thorpej
102 1.1 thorpej return (IOMDIIC_READ);
103 1.1 thorpej }
104 1.1 thorpej
105 1.1 thorpej static const struct i2c_bitbang_ops iomdiic_bbops = {
106 1.1 thorpej iomdiic_bb_set_bits,
107 1.1 thorpej iomdiic_bb_set_dir,
108 1.1 thorpej iomdiic_bb_read_bits,
109 1.1 thorpej {
110 1.1 thorpej IOMD_IOCR_SDA, /* SDA */
111 1.1 thorpej IOMD_IOCR_SCL, /* SCL */
112 1.1 thorpej 0, /* SDA is output */
113 1.1 thorpej IOMD_IOCR_SDA, /* SDA is input */
114 1.1 thorpej }
115 1.1 thorpej };
116 1.1 thorpej
117 1.1 thorpej static int
118 1.7 skrll iomdiic_match(device_t parent, cfdata_t cf, void *aux)
119 1.1 thorpej {
120 1.2 reinoud struct iic_attach_args *ia = aux;
121 1.1 thorpej
122 1.2 reinoud if (strcmp(ia->ia_name, "iic") == 0) return 1;
123 1.2 reinoud return 0;
124 1.1 thorpej }
125 1.1 thorpej
126 1.1 thorpej static void
127 1.7 skrll iomdiic_attach(device_t parent, device_t self, void *aux)
128 1.1 thorpej {
129 1.7 skrll struct iomdiic_softc *sc = device_private(self);
130 1.1 thorpej struct i2cbus_attach_args iba;
131 1.1 thorpej
132 1.7 skrll aprint_normal("\n");
133 1.7 skrll
134 1.7 skrll sc->sc_dev = self;
135 1.1 thorpej
136 1.9 thorpej iic_tag_init(&sc->sc_i2c);
137 1.1 thorpej sc->sc_i2c.ic_cookie = sc;
138 1.1 thorpej sc->sc_i2c.ic_send_start = iomdiic_send_start;
139 1.1 thorpej sc->sc_i2c.ic_send_stop = iomdiic_send_stop;
140 1.1 thorpej sc->sc_i2c.ic_initiate_xfer = iomdiic_initiate_xfer;
141 1.1 thorpej sc->sc_i2c.ic_read_byte = iomdiic_read_byte;
142 1.1 thorpej sc->sc_i2c.ic_write_byte = iomdiic_write_byte;
143 1.1 thorpej
144 1.8 chs memset(&iba, 0, sizeof(iba));
145 1.1 thorpej iba.iba_tag = &sc->sc_i2c;
146 1.11 thorpej config_found(sc->sc_dev, &iba, iicbus_print, CFARGS_NONE);
147 1.1 thorpej }
148 1.1 thorpej
149 1.7 skrll CFATTACH_DECL_NEW(iomdiic, sizeof(struct iomdiic_softc),
150 1.1 thorpej iomdiic_match, iomdiic_attach, NULL, NULL);
151 1.1 thorpej
152 1.1 thorpej i2c_tag_t
153 1.1 thorpej iomdiic_bootstrap_cookie(void)
154 1.1 thorpej {
155 1.1 thorpej static struct iomdiic_softc sc;
156 1.7 skrll static struct device dev;
157 1.1 thorpej
158 1.1 thorpej /* XXX Yuck. */
159 1.7 skrll strcpy(dev.dv_xname, "iomdiicboot");
160 1.1 thorpej
161 1.7 skrll sc.sc_dev = &dev;
162 1.9 thorpej
163 1.9 thorpej iic_tag_init(&sc.sc_i2c);
164 1.1 thorpej sc.sc_i2c.ic_cookie = ≻
165 1.1 thorpej sc.sc_i2c.ic_send_start = iomdiic_send_start;
166 1.1 thorpej sc.sc_i2c.ic_send_stop = iomdiic_send_stop;
167 1.1 thorpej sc.sc_i2c.ic_initiate_xfer = iomdiic_initiate_xfer;
168 1.1 thorpej sc.sc_i2c.ic_read_byte = iomdiic_read_byte;
169 1.1 thorpej sc.sc_i2c.ic_write_byte = iomdiic_write_byte;
170 1.1 thorpej
171 1.1 thorpej return ((void *) &sc.sc_i2c);
172 1.1 thorpej }
173 1.1 thorpej
174 1.1 thorpej static int
175 1.1 thorpej iomdiic_send_start(void *cookie, int flags)
176 1.1 thorpej {
177 1.1 thorpej
178 1.1 thorpej return (i2c_bitbang_send_start(cookie, flags, &iomdiic_bbops));
179 1.1 thorpej }
180 1.1 thorpej
181 1.1 thorpej static int
182 1.1 thorpej iomdiic_send_stop(void *cookie, int flags)
183 1.1 thorpej {
184 1.1 thorpej
185 1.1 thorpej return (i2c_bitbang_send_stop(cookie, flags, &iomdiic_bbops));
186 1.1 thorpej }
187 1.1 thorpej
188 1.1 thorpej static int
189 1.1 thorpej iomdiic_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
190 1.1 thorpej {
191 1.1 thorpej
192 1.1 thorpej return (i2c_bitbang_initiate_xfer(cookie, addr, flags, &iomdiic_bbops));
193 1.1 thorpej }
194 1.1 thorpej
195 1.1 thorpej static int
196 1.1 thorpej iomdiic_read_byte(void *cookie, uint8_t *bytep, int flags)
197 1.1 thorpej {
198 1.1 thorpej
199 1.1 thorpej return (i2c_bitbang_read_byte(cookie, bytep, flags, &iomdiic_bbops));
200 1.1 thorpej }
201 1.1 thorpej
202 1.1 thorpej static int
203 1.1 thorpej iomdiic_write_byte(void *cookie, uint8_t byte, int flags)
204 1.1 thorpej {
205 1.1 thorpej
206 1.1 thorpej return (i2c_bitbang_write_byte(cookie, byte, flags, &iomdiic_bbops));
207 1.1 thorpej }
208