iopi2c.c revision 1.1 1 1.1 thorpej /* $NetBSD: iopi2c.c,v 1.1 2003/10/06 16:06:06 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.1 thorpej __KERNEL_RCSID(0, "$NetBSD: iopi2c.c,v 1.1 2003/10/06 16:06:06 thorpej Exp $");
44 1.1 thorpej
45 1.1 thorpej #include <sys/param.h>
46 1.1 thorpej #include <sys/lock.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.1 thorpej #include <machine/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_acquire_bus(void *, int);
60 1.1 thorpej static void iopiic_release_bus(void *, int);
61 1.1 thorpej
62 1.1 thorpej static int iopiic_send_start(void *, int);
63 1.1 thorpej static int iopiic_send_stop(void *, int);
64 1.1 thorpej static int iopiic_initiate_xfer(void *, uint16_t, int);
65 1.1 thorpej static int iopiic_read_byte(void *, uint8_t *, int);
66 1.1 thorpej static int iopiic_write_byte(void *, uint8_t, int);
67 1.1 thorpej
68 1.1 thorpej void
69 1.1 thorpej iopiic_attach(struct iopiic_softc *sc)
70 1.1 thorpej {
71 1.1 thorpej struct i2cbus_attach_args iba;
72 1.1 thorpej
73 1.1 thorpej sc->sc_i2c.ic_cookie = sc;
74 1.1 thorpej sc->sc_i2c.ic_acquire_bus = iopiic_acquire_bus;
75 1.1 thorpej sc->sc_i2c.ic_release_bus = iopiic_release_bus;
76 1.1 thorpej sc->sc_i2c.ic_send_start = iopiic_send_start;
77 1.1 thorpej sc->sc_i2c.ic_send_stop = iopiic_send_stop;
78 1.1 thorpej sc->sc_i2c.ic_initiate_xfer = iopiic_initiate_xfer;
79 1.1 thorpej sc->sc_i2c.ic_read_byte = iopiic_read_byte;
80 1.1 thorpej sc->sc_i2c.ic_write_byte = iopiic_write_byte;
81 1.1 thorpej
82 1.1 thorpej iba.iba_name = "iic";
83 1.1 thorpej iba.iba_tag = &sc->sc_i2c;
84 1.1 thorpej (void) config_found(&sc->sc_dev, &iba, iicbus_print);
85 1.1 thorpej }
86 1.1 thorpej
87 1.1 thorpej static int
88 1.1 thorpej iopiic_acquire_bus(void *cookie, int flags)
89 1.1 thorpej {
90 1.1 thorpej struct iopiic_softc *sc = cookie;
91 1.1 thorpej
92 1.1 thorpej /* XXX What should we do for the polling case? */
93 1.1 thorpej if (flags & I2C_F_POLL)
94 1.1 thorpej return (0);
95 1.1 thorpej
96 1.1 thorpej return (lockmgr(&sc->sc_buslock, LK_EXCLUSIVE, NULL));
97 1.1 thorpej }
98 1.1 thorpej
99 1.1 thorpej static void
100 1.1 thorpej iopiic_release_bus(void *cookie, int flags)
101 1.1 thorpej {
102 1.1 thorpej struct iopiic_softc *sc = cookie;
103 1.1 thorpej
104 1.1 thorpej /* XXX See above. */
105 1.1 thorpej if (flags & I2C_F_POLL)
106 1.1 thorpej return;
107 1.1 thorpej
108 1.1 thorpej (void) lockmgr(&sc->sc_buslock, LK_RELEASE, NULL);
109 1.1 thorpej }
110 1.1 thorpej
111 1.1 thorpej #define IOPIIC_TIMEOUT 100 /* protocol timeout, in uSecs */
112 1.1 thorpej
113 1.1 thorpej static int
114 1.1 thorpej iopiic_wait(struct iopiic_softc *sc, int bit, int flags)
115 1.1 thorpej {
116 1.1 thorpej uint32_t isr;
117 1.1 thorpej int timeout, error=0;
118 1.1 thorpej
119 1.1 thorpej /* XXX We never sleep, we always poll. Fix me. */
120 1.1 thorpej
121 1.1 thorpej /*
122 1.1 thorpej * For some reason, we seem to run into problems if we poll
123 1.1 thorpej * the ISR while the transfer is in progress--at least on the
124 1.1 thorpej * i80312. The condition that we're looking for never seems
125 1.1 thorpej * to appear on a read, and it's not clear why; perhaps reads
126 1.1 thorpej * of the I2C register file interfere with its proper operation?
127 1.1 thorpej * For now, just delay for a while up front.
128 1.1 thorpej *
129 1.1 thorpej * We _really_ need this to be interrupt-driven, but a problem
130 1.1 thorpej * with that is that the i80312 has no way to mask interrupts...
131 1.1 thorpej * So we need to deal with that. For DMA and AAU, too, for that
132 1.1 thorpej * matter.
133 1.1 thorpej * Note that delay(100) doesn't quite work on the npwr w/ m41t00.
134 1.1 thorpej */
135 1.1 thorpej delay(110);
136 1.1 thorpej for (timeout = IOPIIC_TIMEOUT; timeout != 0; timeout--) {
137 1.1 thorpej isr = bus_space_read_4(sc->sc_st, sc->sc_sh, IIC_ISR);
138 1.1 thorpej if (isr & (bit | IIC_ISR_BED))
139 1.1 thorpej break;
140 1.1 thorpej delay(1);
141 1.1 thorpej }
142 1.1 thorpej
143 1.1 thorpej if (isr & (IIC_ISR_BED | (bit & IIC_ISR_ALD)))
144 1.1 thorpej error = EIO;
145 1.1 thorpej else if (isr & (bit & ~IIC_ISR_ALD))
146 1.1 thorpej error = 0;
147 1.1 thorpej else
148 1.1 thorpej error = ETIMEDOUT;
149 1.1 thorpej
150 1.1 thorpej if (error)
151 1.1 thorpej printf("%s: iopiic_wait, (%08x) error %d: ISR = 0x%08x\n",
152 1.1 thorpej sc->sc_dev.dv_xname, bit, error, isr);
153 1.1 thorpej
154 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ISR, 0);
155 1.1 thorpej
156 1.1 thorpej return (error);
157 1.1 thorpej }
158 1.1 thorpej
159 1.1 thorpej static int
160 1.1 thorpej iopiic_send_start(void *cookie, int flags)
161 1.1 thorpej {
162 1.1 thorpej struct iopiic_softc *sc = cookie;
163 1.1 thorpej
164 1.1 thorpej /*
165 1.1 thorpej * This may only work in conjunction with a data transfer;
166 1.1 thorpej * we might need to un-export the "start" primitive.
167 1.1 thorpej */
168 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
169 1.1 thorpej sc->sc_icr | IIC_ICR_START);
170 1.1 thorpej delay(IOPIIC_TIMEOUT);
171 1.1 thorpej
172 1.1 thorpej return (0);
173 1.1 thorpej }
174 1.1 thorpej
175 1.1 thorpej static int
176 1.1 thorpej iopiic_send_stop(void *cookie, int flags)
177 1.1 thorpej {
178 1.1 thorpej struct iopiic_softc *sc = cookie;
179 1.1 thorpej
180 1.1 thorpej /*
181 1.1 thorpej * The STOP bit is only used in conjunction with
182 1.1 thorpej * a data transfer, so we need to use MA in this
183 1.1 thorpej * case.
184 1.1 thorpej *
185 1.1 thorpej * Consider adding an I2C_F_STOP so we can
186 1.1 thorpej * do a read-with-STOP and write-with-STOP.
187 1.1 thorpej */
188 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
189 1.1 thorpej sc->sc_icr | IIC_ICR_MA);
190 1.1 thorpej delay(IOPIIC_TIMEOUT);
191 1.1 thorpej
192 1.1 thorpej return (0);
193 1.1 thorpej }
194 1.1 thorpej
195 1.1 thorpej static int
196 1.1 thorpej iopiic_initiate_xfer(void *cookie, uint16_t addr, int flags)
197 1.1 thorpej {
198 1.1 thorpej struct iopiic_softc *sc = cookie;
199 1.1 thorpej int error, rd_req = (flags & I2C_F_READ) != 0;
200 1.1 thorpej uint32_t idbr;
201 1.1 thorpej
202 1.1 thorpej /* We only support 7-bit addressing. */
203 1.1 thorpej if ((addr & 0x78) == 0x78)
204 1.1 thorpej return (EINVAL);
205 1.1 thorpej
206 1.1 thorpej idbr = (addr << 1) | (rd_req ? 1 : 0);
207 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_IDBR, idbr);
208 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
209 1.1 thorpej sc->sc_icr | IIC_ICR_START | IIC_ICR_TB);
210 1.1 thorpej
211 1.1 thorpej error = iopiic_wait(sc, IIC_ISR_ITE, flags);
212 1.1 thorpej #if 0
213 1.1 thorpej if (error)
214 1.1 thorpej printf("%s: failed to initiate %s xfer\n", sc->sc_dev.dv_xname,
215 1.1 thorpej rd_req ? "read" : "write");
216 1.1 thorpej #endif
217 1.1 thorpej return (error);
218 1.1 thorpej }
219 1.1 thorpej
220 1.1 thorpej static int
221 1.1 thorpej iopiic_read_byte(void *cookie, uint8_t *bytep, int flags)
222 1.1 thorpej {
223 1.1 thorpej struct iopiic_softc *sc = cookie;
224 1.1 thorpej int error, last_byte = (flags & I2C_F_LAST) != 0,
225 1.1 thorpej send_stop = (flags & I2C_F_STOP) != 0;
226 1.1 thorpej
227 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
228 1.1 thorpej sc->sc_icr | IIC_ICR_TB | (last_byte ? IIC_ICR_NACK : 0) |
229 1.1 thorpej (send_stop ? IIC_ICR_STOP : 0));
230 1.1 thorpej if ((error = iopiic_wait(sc, IIC_ISR_IRF | IIC_ISR_ALD, flags)) == 0)
231 1.1 thorpej *bytep = bus_space_read_4(sc->sc_st, sc->sc_sh, IIC_IDBR);
232 1.1 thorpej #if 0
233 1.1 thorpej if (error)
234 1.1 thorpej printf("%s: read byte failed\n", sc->sc_dev.dv_xname);
235 1.1 thorpej #endif
236 1.1 thorpej
237 1.1 thorpej return (error);
238 1.1 thorpej }
239 1.1 thorpej
240 1.1 thorpej static int
241 1.1 thorpej iopiic_write_byte(void *cookie, uint8_t byte, int flags)
242 1.1 thorpej {
243 1.1 thorpej struct iopiic_softc *sc = cookie;
244 1.1 thorpej int error, send_stop = (flags & I2C_F_STOP) != 0;
245 1.1 thorpej
246 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_IDBR, byte);
247 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
248 1.1 thorpej sc->sc_icr | IIC_ICR_TB | (send_stop ? IIC_ICR_STOP : 0));
249 1.1 thorpej error = iopiic_wait(sc, IIC_ISR_ITE | IIC_ISR_ALD, flags);
250 1.1 thorpej
251 1.1 thorpej #if 0
252 1.1 thorpej if (error)
253 1.1 thorpej printf("%s: write byte failed\n", sc->sc_dev.dv_xname);
254 1.1 thorpej #endif
255 1.1 thorpej
256 1.1 thorpej return (error);
257 1.1 thorpej }
258