iomdiic.c revision 1.9 1 1.9 thorpej /* $NetBSD: iomdiic.c,v 1.9 2019/12/22 23:23:29 thorpej 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.1 thorpej
61 1.1 thorpej /*
62 1.1 thorpej * The SDA pin is open-drain, so we make it an input by
63 1.1 thorpej * writing a 1 to it.
64 1.1 thorpej */
65 1.1 thorpej uint8_t sc_iomd_iocr;
66 1.1 thorpej };
67 1.1 thorpej
68 1.1 thorpej static int iomdiic_send_start(void *, int);
69 1.1 thorpej static int iomdiic_send_stop(void *, int);
70 1.1 thorpej static int iomdiic_initiate_xfer(void *, i2c_addr_t, int);
71 1.1 thorpej static int iomdiic_read_byte(void *, uint8_t *, int);
72 1.1 thorpej static int iomdiic_write_byte(void *, uint8_t, int);
73 1.1 thorpej
74 1.4 perry #define IOMDIIC_READ *(volatile uint8_t *)(IOMD_ADDRESS(IOMD_IOCR))
75 1.4 perry #define IOMDIIC_WRITE(x) *(volatile uint8_t *)(IOMD_ADDRESS(IOMD_IOCR)) = (x)
76 1.1 thorpej
77 1.1 thorpej #define IOMD_IOCR_SDA 0x01
78 1.1 thorpej #define IOMD_IOCR_SCL 0x02
79 1.1 thorpej
80 1.1 thorpej static void
81 1.1 thorpej iomdiic_bb_set_bits(void *cookie, uint32_t bits)
82 1.1 thorpej {
83 1.1 thorpej struct iomdiic_softc *sc = cookie;
84 1.1 thorpej
85 1.1 thorpej IOMDIIC_WRITE((IOMDIIC_READ & ~(IOMD_IOCR_SDA|IOMD_IOCR_SCL)) |
86 1.1 thorpej sc->sc_iomd_iocr | bits);
87 1.1 thorpej }
88 1.1 thorpej
89 1.1 thorpej static void
90 1.1 thorpej iomdiic_bb_set_dir(void *cookie, uint32_t bits)
91 1.1 thorpej {
92 1.1 thorpej struct iomdiic_softc *sc = cookie;
93 1.1 thorpej
94 1.1 thorpej sc->sc_iomd_iocr = bits;
95 1.1 thorpej }
96 1.1 thorpej
97 1.1 thorpej static uint32_t
98 1.1 thorpej iomdiic_bb_read_bits(void *cookie)
99 1.1 thorpej {
100 1.1 thorpej
101 1.1 thorpej return (IOMDIIC_READ);
102 1.1 thorpej }
103 1.1 thorpej
104 1.1 thorpej static const struct i2c_bitbang_ops iomdiic_bbops = {
105 1.1 thorpej iomdiic_bb_set_bits,
106 1.1 thorpej iomdiic_bb_set_dir,
107 1.1 thorpej iomdiic_bb_read_bits,
108 1.1 thorpej {
109 1.1 thorpej IOMD_IOCR_SDA, /* SDA */
110 1.1 thorpej IOMD_IOCR_SCL, /* SCL */
111 1.1 thorpej 0, /* SDA is output */
112 1.1 thorpej IOMD_IOCR_SDA, /* SDA is input */
113 1.1 thorpej }
114 1.1 thorpej };
115 1.1 thorpej
116 1.1 thorpej static int
117 1.7 skrll iomdiic_match(device_t parent, cfdata_t cf, void *aux)
118 1.1 thorpej {
119 1.2 reinoud struct iic_attach_args *ia = aux;
120 1.1 thorpej
121 1.2 reinoud if (strcmp(ia->ia_name, "iic") == 0) return 1;
122 1.2 reinoud return 0;
123 1.1 thorpej }
124 1.1 thorpej
125 1.1 thorpej static void
126 1.7 skrll iomdiic_attach(device_t parent, device_t self, void *aux)
127 1.1 thorpej {
128 1.7 skrll struct iomdiic_softc *sc = device_private(self);
129 1.1 thorpej struct i2cbus_attach_args iba;
130 1.1 thorpej
131 1.7 skrll aprint_normal("\n");
132 1.7 skrll
133 1.7 skrll sc->sc_dev = self;
134 1.1 thorpej
135 1.9 thorpej iic_tag_init(&sc->sc_i2c);
136 1.1 thorpej sc->sc_i2c.ic_cookie = sc;
137 1.1 thorpej sc->sc_i2c.ic_send_start = iomdiic_send_start;
138 1.1 thorpej sc->sc_i2c.ic_send_stop = iomdiic_send_stop;
139 1.1 thorpej sc->sc_i2c.ic_initiate_xfer = iomdiic_initiate_xfer;
140 1.1 thorpej sc->sc_i2c.ic_read_byte = iomdiic_read_byte;
141 1.1 thorpej sc->sc_i2c.ic_write_byte = iomdiic_write_byte;
142 1.1 thorpej
143 1.8 chs memset(&iba, 0, sizeof(iba));
144 1.1 thorpej iba.iba_tag = &sc->sc_i2c;
145 1.7 skrll (void) config_found_ia(sc->sc_dev, "i2cbus", &iba, iicbus_print);
146 1.1 thorpej }
147 1.1 thorpej
148 1.7 skrll CFATTACH_DECL_NEW(iomdiic, sizeof(struct iomdiic_softc),
149 1.1 thorpej iomdiic_match, iomdiic_attach, NULL, NULL);
150 1.1 thorpej
151 1.1 thorpej i2c_tag_t
152 1.1 thorpej iomdiic_bootstrap_cookie(void)
153 1.1 thorpej {
154 1.1 thorpej static struct iomdiic_softc sc;
155 1.7 skrll static struct device dev;
156 1.1 thorpej
157 1.1 thorpej /* XXX Yuck. */
158 1.7 skrll strcpy(dev.dv_xname, "iomdiicboot");
159 1.1 thorpej
160 1.7 skrll sc.sc_dev = &dev;
161 1.9 thorpej
162 1.9 thorpej iic_tag_init(&sc.sc_i2c);
163 1.1 thorpej sc.sc_i2c.ic_cookie = ≻
164 1.1 thorpej sc.sc_i2c.ic_send_start = iomdiic_send_start;
165 1.1 thorpej sc.sc_i2c.ic_send_stop = iomdiic_send_stop;
166 1.1 thorpej sc.sc_i2c.ic_initiate_xfer = iomdiic_initiate_xfer;
167 1.1 thorpej sc.sc_i2c.ic_read_byte = iomdiic_read_byte;
168 1.1 thorpej sc.sc_i2c.ic_write_byte = iomdiic_write_byte;
169 1.1 thorpej
170 1.1 thorpej return ((void *) &sc.sc_i2c);
171 1.1 thorpej }
172 1.1 thorpej
173 1.1 thorpej static int
174 1.1 thorpej iomdiic_send_start(void *cookie, int flags)
175 1.1 thorpej {
176 1.1 thorpej
177 1.1 thorpej return (i2c_bitbang_send_start(cookie, flags, &iomdiic_bbops));
178 1.1 thorpej }
179 1.1 thorpej
180 1.1 thorpej static int
181 1.1 thorpej iomdiic_send_stop(void *cookie, int flags)
182 1.1 thorpej {
183 1.1 thorpej
184 1.1 thorpej return (i2c_bitbang_send_stop(cookie, flags, &iomdiic_bbops));
185 1.1 thorpej }
186 1.1 thorpej
187 1.1 thorpej static int
188 1.1 thorpej iomdiic_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
189 1.1 thorpej {
190 1.1 thorpej
191 1.1 thorpej return (i2c_bitbang_initiate_xfer(cookie, addr, flags, &iomdiic_bbops));
192 1.1 thorpej }
193 1.1 thorpej
194 1.1 thorpej static int
195 1.1 thorpej iomdiic_read_byte(void *cookie, uint8_t *bytep, int flags)
196 1.1 thorpej {
197 1.1 thorpej
198 1.1 thorpej return (i2c_bitbang_read_byte(cookie, bytep, flags, &iomdiic_bbops));
199 1.1 thorpej }
200 1.1 thorpej
201 1.1 thorpej static int
202 1.1 thorpej iomdiic_write_byte(void *cookie, uint8_t byte, int flags)
203 1.1 thorpej {
204 1.1 thorpej
205 1.1 thorpej return (i2c_bitbang_write_byte(cookie, byte, flags, &iomdiic_bbops));
206 1.1 thorpej }
207