iopi2c.c revision 1.12 1 1.12 thorpej /* $NetBSD: iopi2c.c,v 1.12 2025/09/15 13:23:01 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 /*
39 1.1 thorpej * Intel i80321 I/O Processor I2C Controller Unit support.
40 1.1 thorpej */
41 1.1 thorpej
42 1.1 thorpej #include <sys/cdefs.h>
43 1.12 thorpej __KERNEL_RCSID(0, "$NetBSD: iopi2c.c,v 1.12 2025/09/15 13:23:01 thorpej Exp $");
44 1.1 thorpej
45 1.1 thorpej #include <sys/param.h>
46 1.5 ad #include <sys/mutex.h>
47 1.1 thorpej #include <sys/systm.h>
48 1.1 thorpej #include <sys/device.h>
49 1.1 thorpej #include <sys/kernel.h>
50 1.1 thorpej
51 1.6 dyoung #include <sys/bus.h>
52 1.1 thorpej #include <machine/intr.h>
53 1.1 thorpej
54 1.1 thorpej #include <dev/i2c/i2cvar.h>
55 1.1 thorpej
56 1.1 thorpej #include <arm/xscale/iopi2creg.h>
57 1.1 thorpej #include <arm/xscale/iopi2cvar.h>
58 1.1 thorpej
59 1.1 thorpej static int iopiic_send_start(void *, int);
60 1.1 thorpej static int iopiic_send_stop(void *, int);
61 1.1 thorpej static int iopiic_initiate_xfer(void *, uint16_t, int);
62 1.1 thorpej static int iopiic_read_byte(void *, uint8_t *, int);
63 1.1 thorpej static int iopiic_write_byte(void *, uint8_t, int);
64 1.1 thorpej
65 1.1 thorpej void
66 1.1 thorpej iopiic_attach(struct iopiic_softc *sc)
67 1.1 thorpej {
68 1.1 thorpej
69 1.9 thorpej iic_tag_init(&sc->sc_i2c);
70 1.1 thorpej sc->sc_i2c.ic_cookie = sc;
71 1.1 thorpej sc->sc_i2c.ic_send_start = iopiic_send_start;
72 1.1 thorpej sc->sc_i2c.ic_send_stop = iopiic_send_stop;
73 1.1 thorpej sc->sc_i2c.ic_initiate_xfer = iopiic_initiate_xfer;
74 1.1 thorpej sc->sc_i2c.ic_read_byte = iopiic_read_byte;
75 1.1 thorpej sc->sc_i2c.ic_write_byte = iopiic_write_byte;
76 1.1 thorpej
77 1.12 thorpej iicbus_attach(sc->sc_dev, &sc->sc_i2c);
78 1.1 thorpej }
79 1.1 thorpej
80 1.1 thorpej #define IOPIIC_TIMEOUT 100 /* protocol timeout, in uSecs */
81 1.1 thorpej
82 1.1 thorpej static int
83 1.1 thorpej iopiic_wait(struct iopiic_softc *sc, int bit, int flags)
84 1.1 thorpej {
85 1.1 thorpej uint32_t isr;
86 1.1 thorpej int timeout, error=0;
87 1.1 thorpej
88 1.1 thorpej /* XXX We never sleep, we always poll. Fix me. */
89 1.1 thorpej
90 1.1 thorpej /*
91 1.1 thorpej * For some reason, we seem to run into problems if we poll
92 1.1 thorpej * the ISR while the transfer is in progress--at least on the
93 1.1 thorpej * i80312. The condition that we're looking for never seems
94 1.1 thorpej * to appear on a read, and it's not clear why; perhaps reads
95 1.1 thorpej * of the I2C register file interfere with its proper operation?
96 1.1 thorpej * For now, just delay for a while up front.
97 1.1 thorpej *
98 1.1 thorpej * We _really_ need this to be interrupt-driven, but a problem
99 1.1 thorpej * with that is that the i80312 has no way to mask interrupts...
100 1.1 thorpej * So we need to deal with that. For DMA and AAU, too, for that
101 1.1 thorpej * matter.
102 1.1 thorpej * Note that delay(100) doesn't quite work on the npwr w/ m41t00.
103 1.1 thorpej */
104 1.1 thorpej delay(110);
105 1.1 thorpej for (timeout = IOPIIC_TIMEOUT; timeout != 0; timeout--) {
106 1.1 thorpej isr = bus_space_read_4(sc->sc_st, sc->sc_sh, IIC_ISR);
107 1.1 thorpej if (isr & (bit | IIC_ISR_BED))
108 1.1 thorpej break;
109 1.1 thorpej delay(1);
110 1.1 thorpej }
111 1.1 thorpej
112 1.1 thorpej if (isr & (IIC_ISR_BED | (bit & IIC_ISR_ALD)))
113 1.1 thorpej error = EIO;
114 1.1 thorpej else if (isr & (bit & ~IIC_ISR_ALD))
115 1.1 thorpej error = 0;
116 1.1 thorpej else
117 1.1 thorpej error = ETIMEDOUT;
118 1.1 thorpej
119 1.1 thorpej if (error)
120 1.7 jakllsch device_printf(sc->sc_dev,
121 1.7 jakllsch "iopiic_wait, (%08x) error %d: ISR = 0x%08x\n",
122 1.7 jakllsch bit, error, isr);
123 1.1 thorpej
124 1.2 scw /*
125 1.2 scw * The IIC_ISR is Read/Clear apart from the bottom 4 bits, which are
126 1.2 scw * read-only. So simply write back our copy of the ISR to clear any
127 1.2 scw * latched status.
128 1.2 scw */
129 1.2 scw bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ISR, isr);
130 1.1 thorpej
131 1.1 thorpej return (error);
132 1.1 thorpej }
133 1.1 thorpej
134 1.1 thorpej static int
135 1.1 thorpej iopiic_send_start(void *cookie, int flags)
136 1.1 thorpej {
137 1.1 thorpej struct iopiic_softc *sc = cookie;
138 1.1 thorpej
139 1.1 thorpej /*
140 1.1 thorpej * This may only work in conjunction with a data transfer;
141 1.1 thorpej * we might need to un-export the "start" primitive.
142 1.1 thorpej */
143 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
144 1.1 thorpej sc->sc_icr | IIC_ICR_START);
145 1.1 thorpej delay(IOPIIC_TIMEOUT);
146 1.1 thorpej
147 1.1 thorpej return (0);
148 1.1 thorpej }
149 1.1 thorpej
150 1.1 thorpej static int
151 1.1 thorpej iopiic_send_stop(void *cookie, int flags)
152 1.1 thorpej {
153 1.1 thorpej struct iopiic_softc *sc = cookie;
154 1.1 thorpej
155 1.1 thorpej /*
156 1.1 thorpej * The STOP bit is only used in conjunction with
157 1.1 thorpej * a data transfer, so we need to use MA in this
158 1.1 thorpej * case.
159 1.1 thorpej *
160 1.1 thorpej * Consider adding an I2C_F_STOP so we can
161 1.1 thorpej * do a read-with-STOP and write-with-STOP.
162 1.1 thorpej */
163 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
164 1.1 thorpej sc->sc_icr | IIC_ICR_MA);
165 1.1 thorpej delay(IOPIIC_TIMEOUT);
166 1.1 thorpej
167 1.1 thorpej return (0);
168 1.1 thorpej }
169 1.1 thorpej
170 1.1 thorpej static int
171 1.1 thorpej iopiic_initiate_xfer(void *cookie, uint16_t addr, int flags)
172 1.1 thorpej {
173 1.1 thorpej struct iopiic_softc *sc = cookie;
174 1.1 thorpej int error, rd_req = (flags & I2C_F_READ) != 0;
175 1.1 thorpej uint32_t idbr;
176 1.1 thorpej
177 1.1 thorpej /* We only support 7-bit addressing. */
178 1.1 thorpej if ((addr & 0x78) == 0x78)
179 1.1 thorpej return (EINVAL);
180 1.1 thorpej
181 1.1 thorpej idbr = (addr << 1) | (rd_req ? 1 : 0);
182 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_IDBR, idbr);
183 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
184 1.1 thorpej sc->sc_icr | IIC_ICR_START | IIC_ICR_TB);
185 1.1 thorpej
186 1.1 thorpej error = iopiic_wait(sc, IIC_ISR_ITE, flags);
187 1.1 thorpej #if 0
188 1.1 thorpej if (error)
189 1.7 jakllsch device_printf(sc->sc_dev, "failed to initiate %s xfer\n",
190 1.1 thorpej rd_req ? "read" : "write");
191 1.1 thorpej #endif
192 1.1 thorpej return (error);
193 1.1 thorpej }
194 1.1 thorpej
195 1.1 thorpej static int
196 1.1 thorpej iopiic_read_byte(void *cookie, uint8_t *bytep, int flags)
197 1.1 thorpej {
198 1.1 thorpej struct iopiic_softc *sc = cookie;
199 1.1 thorpej int error, last_byte = (flags & I2C_F_LAST) != 0,
200 1.1 thorpej send_stop = (flags & I2C_F_STOP) != 0;
201 1.1 thorpej
202 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
203 1.1 thorpej sc->sc_icr | IIC_ICR_TB | (last_byte ? IIC_ICR_NACK : 0) |
204 1.1 thorpej (send_stop ? IIC_ICR_STOP : 0));
205 1.1 thorpej if ((error = iopiic_wait(sc, IIC_ISR_IRF | IIC_ISR_ALD, flags)) == 0)
206 1.1 thorpej *bytep = bus_space_read_4(sc->sc_st, sc->sc_sh, IIC_IDBR);
207 1.1 thorpej #if 0
208 1.1 thorpej if (error)
209 1.7 jakllsch device_printf(sc->sc_dev, "read byte failed\n");
210 1.1 thorpej #endif
211 1.1 thorpej
212 1.1 thorpej return (error);
213 1.1 thorpej }
214 1.1 thorpej
215 1.1 thorpej static int
216 1.1 thorpej iopiic_write_byte(void *cookie, uint8_t byte, int flags)
217 1.1 thorpej {
218 1.1 thorpej struct iopiic_softc *sc = cookie;
219 1.1 thorpej int error, send_stop = (flags & I2C_F_STOP) != 0;
220 1.1 thorpej
221 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_IDBR, byte);
222 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
223 1.1 thorpej sc->sc_icr | IIC_ICR_TB | (send_stop ? IIC_ICR_STOP : 0));
224 1.1 thorpej error = iopiic_wait(sc, IIC_ISR_ITE | IIC_ISR_ALD, flags);
225 1.1 thorpej
226 1.1 thorpej #if 0
227 1.1 thorpej if (error)
228 1.7 jakllsch device_printf(sc->sc_dev, "write byte failed\n");
229 1.1 thorpej #endif
230 1.1 thorpej
231 1.1 thorpej return (error);
232 1.1 thorpej }
233