cuda.c revision 1.3.4.4 1 1.3.4.4 yamt /* $NetBSD: cuda.c,v 1.3.4.4 2007/11/15 11:43:05 yamt Exp $ */
2 1.3.4.2 yamt
3 1.3.4.2 yamt /*-
4 1.3.4.2 yamt * Copyright (c) 2006 Michael Lorenz
5 1.3.4.2 yamt * All rights reserved.
6 1.3.4.2 yamt *
7 1.3.4.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.3.4.2 yamt * modification, are permitted provided that the following conditions
9 1.3.4.2 yamt * are met:
10 1.3.4.2 yamt * 1. Redistributions of source code must retain the above copyright
11 1.3.4.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.3.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.3.4.2 yamt * notice, this list of conditions and the following disclaimer in the
14 1.3.4.2 yamt * documentation and/or other materials provided with the distribution.
15 1.3.4.2 yamt * 3. Neither the name of The NetBSD Foundation nor the names of its
16 1.3.4.2 yamt * contributors may be used to endorse or promote products derived
17 1.3.4.2 yamt * from this software without specific prior written permission.
18 1.3.4.2 yamt *
19 1.3.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.3.4.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.3.4.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.3.4.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.3.4.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.3.4.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.3.4.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.3.4.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.3.4.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.3.4.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.3.4.2 yamt * POSSIBILITY OF SUCH DAMAGE.
30 1.3.4.2 yamt */
31 1.3.4.2 yamt
32 1.3.4.2 yamt #include <sys/cdefs.h>
33 1.3.4.4 yamt __KERNEL_RCSID(0, "$NetBSD: cuda.c,v 1.3.4.4 2007/11/15 11:43:05 yamt Exp $");
34 1.3.4.2 yamt
35 1.3.4.2 yamt #include <sys/param.h>
36 1.3.4.2 yamt #include <sys/systm.h>
37 1.3.4.2 yamt #include <sys/kernel.h>
38 1.3.4.2 yamt #include <sys/device.h>
39 1.3.4.2 yamt #include <sys/proc.h>
40 1.3.4.2 yamt
41 1.3.4.2 yamt #include <machine/bus.h>
42 1.3.4.2 yamt #include <machine/autoconf.h>
43 1.3.4.3 yamt #include <machine/pio.h>
44 1.3.4.2 yamt #include <dev/clock_subr.h>
45 1.3.4.2 yamt #include <dev/i2c/i2cvar.h>
46 1.3.4.2 yamt
47 1.3.4.2 yamt #include <macppc/dev/viareg.h>
48 1.3.4.2 yamt #include <macppc/dev/cudavar.h>
49 1.3.4.2 yamt
50 1.3.4.2 yamt #include <dev/ofw/openfirm.h>
51 1.3.4.2 yamt #include <dev/adb/adbvar.h>
52 1.3.4.2 yamt #include "opt_cuda.h"
53 1.3.4.2 yamt
54 1.3.4.2 yamt #ifdef CUDA_DEBUG
55 1.3.4.2 yamt #define DPRINTF printf
56 1.3.4.2 yamt #else
57 1.3.4.2 yamt #define DPRINTF while (0) printf
58 1.3.4.2 yamt #endif
59 1.3.4.2 yamt
60 1.3.4.2 yamt #define CUDA_NOTREADY 0x1 /* has not been initialized yet */
61 1.3.4.2 yamt #define CUDA_IDLE 0x2 /* the bus is currently idle */
62 1.3.4.2 yamt #define CUDA_OUT 0x3 /* sending out a command */
63 1.3.4.2 yamt #define CUDA_IN 0x4 /* receiving data */
64 1.3.4.2 yamt #define CUDA_POLLING 0x5 /* polling - II only */
65 1.3.4.2 yamt
66 1.3.4.2 yamt static void cuda_attach(struct device *, struct device *, void *);
67 1.3.4.2 yamt static int cuda_match(struct device *, struct cfdata *, void *);
68 1.3.4.2 yamt static void cuda_autopoll(void *, int);
69 1.3.4.2 yamt
70 1.3.4.2 yamt static int cuda_intr(void *);
71 1.3.4.2 yamt
72 1.3.4.2 yamt typedef struct _cuda_handler {
73 1.3.4.2 yamt int (*handler)(void *, int, uint8_t *);
74 1.3.4.2 yamt void *cookie;
75 1.3.4.2 yamt } CudaHandler;
76 1.3.4.2 yamt
77 1.3.4.2 yamt struct cuda_softc {
78 1.3.4.2 yamt struct device sc_dev;
79 1.3.4.2 yamt void *sc_ih;
80 1.3.4.2 yamt CudaHandler sc_handlers[16];
81 1.3.4.2 yamt struct todr_chip_handle sc_todr;
82 1.3.4.2 yamt struct adb_bus_accessops sc_adbops;
83 1.3.4.2 yamt struct i2c_controller sc_i2c;
84 1.3.4.2 yamt struct lock sc_buslock;
85 1.3.4.2 yamt bus_space_tag_t sc_memt;
86 1.3.4.2 yamt bus_space_handle_t sc_memh;
87 1.3.4.2 yamt int sc_node;
88 1.3.4.2 yamt int sc_state;
89 1.3.4.2 yamt int sc_waiting;
90 1.3.4.2 yamt int sc_polling;
91 1.3.4.2 yamt int sc_sent;
92 1.3.4.2 yamt int sc_out_length;
93 1.3.4.2 yamt int sc_received;
94 1.3.4.2 yamt int sc_iic_done;
95 1.3.4.2 yamt int sc_error;
96 1.3.4.2 yamt /* time */
97 1.3.4.2 yamt uint32_t sc_tod;
98 1.3.4.2 yamt uint32_t sc_autopoll;
99 1.3.4.2 yamt uint32_t sc_todev;
100 1.3.4.2 yamt /* ADB */
101 1.3.4.2 yamt void (*sc_adb_handler)(void *, int, uint8_t *);
102 1.3.4.2 yamt void *sc_adb_cookie;
103 1.3.4.2 yamt uint32_t sc_i2c_read_len;
104 1.3.4.2 yamt /* internal buffers */
105 1.3.4.2 yamt uint8_t sc_in[256];
106 1.3.4.2 yamt uint8_t sc_out[256];
107 1.3.4.2 yamt };
108 1.3.4.2 yamt
109 1.3.4.2 yamt CFATTACH_DECL(cuda, sizeof(struct cuda_softc),
110 1.3.4.2 yamt cuda_match, cuda_attach, NULL, NULL);
111 1.3.4.2 yamt
112 1.3.4.2 yamt static inline void cuda_write_reg(struct cuda_softc *, int, uint8_t);
113 1.3.4.2 yamt static inline uint8_t cuda_read_reg(struct cuda_softc *, int);
114 1.3.4.2 yamt static void cuda_idle(struct cuda_softc *);
115 1.3.4.2 yamt static void cuda_tip(struct cuda_softc *);
116 1.3.4.2 yamt static void cuda_clear_tip(struct cuda_softc *);
117 1.3.4.2 yamt static void cuda_in(struct cuda_softc *);
118 1.3.4.2 yamt static void cuda_out(struct cuda_softc *);
119 1.3.4.2 yamt static void cuda_toggle_ack(struct cuda_softc *);
120 1.3.4.2 yamt static void cuda_ack_off(struct cuda_softc *);
121 1.3.4.2 yamt static int cuda_intr_state(struct cuda_softc *);
122 1.3.4.2 yamt
123 1.3.4.2 yamt static void cuda_init(struct cuda_softc *);
124 1.3.4.2 yamt
125 1.3.4.2 yamt /*
126 1.3.4.2 yamt * send a message to Cuda.
127 1.3.4.2 yamt */
128 1.3.4.2 yamt /* cookie, flags, length, data */
129 1.3.4.2 yamt static int cuda_send(void *, int, int, uint8_t *);
130 1.3.4.2 yamt static void cuda_poll(void *);
131 1.3.4.2 yamt static void cuda_adb_poll(void *);
132 1.3.4.2 yamt static int cuda_set_handler(void *, int, int (*)(void *, int, uint8_t *), void *);
133 1.3.4.2 yamt
134 1.3.4.2 yamt static int cuda_error_handler(void *, int, uint8_t *);
135 1.3.4.2 yamt
136 1.3.4.2 yamt static int cuda_todr_handler(void *, int, uint8_t *);
137 1.3.4.2 yamt static int cuda_todr_set(todr_chip_handle_t, volatile struct timeval *);
138 1.3.4.2 yamt static int cuda_todr_get(todr_chip_handle_t, volatile struct timeval *);
139 1.3.4.2 yamt
140 1.3.4.2 yamt static int cuda_adb_handler(void *, int, uint8_t *);
141 1.3.4.2 yamt static void cuda_final(struct device *);
142 1.3.4.2 yamt
143 1.3.4.2 yamt static struct cuda_attach_args *cuda0 = NULL;
144 1.3.4.2 yamt
145 1.3.4.2 yamt /* ADB bus attachment stuff */
146 1.3.4.2 yamt static int cuda_adb_send(void *, int, int, int, uint8_t *);
147 1.3.4.2 yamt static int cuda_adb_set_handler(void *, void (*)(void *, int, uint8_t *), void *);
148 1.3.4.2 yamt
149 1.3.4.2 yamt /* i2c stuff */
150 1.3.4.2 yamt static int cuda_i2c_acquire_bus(void *, int);
151 1.3.4.2 yamt static void cuda_i2c_release_bus(void *, int);
152 1.3.4.2 yamt static int cuda_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
153 1.3.4.2 yamt void *, size_t, int);
154 1.3.4.2 yamt
155 1.3.4.2 yamt static int
156 1.3.4.2 yamt cuda_match(struct device *parent, struct cfdata *cf, void *aux)
157 1.3.4.2 yamt {
158 1.3.4.2 yamt struct confargs *ca = aux;
159 1.3.4.2 yamt
160 1.3.4.2 yamt if (ca->ca_nreg < 8)
161 1.3.4.2 yamt return 0;
162 1.3.4.2 yamt
163 1.3.4.2 yamt if (ca->ca_nintr < 4)
164 1.3.4.2 yamt return 0;
165 1.3.4.2 yamt
166 1.3.4.2 yamt if (strcmp(ca->ca_name, "via-cuda") == 0) {
167 1.3.4.2 yamt return 10; /* beat adb* at obio? */
168 1.3.4.2 yamt }
169 1.3.4.2 yamt
170 1.3.4.2 yamt return 0;
171 1.3.4.2 yamt }
172 1.3.4.2 yamt
173 1.3.4.2 yamt static void
174 1.3.4.2 yamt cuda_attach(struct device *parent, struct device *dev, void *aux)
175 1.3.4.2 yamt {
176 1.3.4.2 yamt struct confargs *ca = aux;
177 1.3.4.2 yamt struct cuda_softc *sc = (struct cuda_softc *)dev;
178 1.3.4.2 yamt struct i2cbus_attach_args iba;
179 1.3.4.2 yamt static struct cuda_attach_args caa;
180 1.3.4.2 yamt int irq = ca->ca_intr[0];
181 1.3.4.2 yamt int node, i, child;
182 1.3.4.2 yamt char name[32];
183 1.3.4.2 yamt
184 1.3.4.4 yamt node = of_getnode_byname(OF_parent(ca->ca_node), "extint-gpio1");
185 1.3.4.2 yamt if (node)
186 1.3.4.2 yamt OF_getprop(node, "interrupts", &irq, 4);
187 1.3.4.2 yamt
188 1.3.4.2 yamt printf(" irq %d: ", irq);
189 1.3.4.2 yamt
190 1.3.4.2 yamt sc->sc_node = ca->ca_node;
191 1.3.4.2 yamt sc->sc_memt = ca->ca_tag;
192 1.3.4.2 yamt
193 1.3.4.2 yamt sc->sc_sent = 0;
194 1.3.4.2 yamt sc->sc_received = 0;
195 1.3.4.2 yamt sc->sc_waiting = 0;
196 1.3.4.2 yamt sc->sc_polling = 0;
197 1.3.4.2 yamt sc->sc_state = CUDA_NOTREADY;
198 1.3.4.2 yamt sc->sc_error = 0;
199 1.3.4.2 yamt sc->sc_i2c_read_len = 0;
200 1.3.4.2 yamt
201 1.3.4.2 yamt if (bus_space_map(sc->sc_memt, ca->ca_reg[0] + ca->ca_baseaddr,
202 1.3.4.2 yamt ca->ca_reg[1], 0, &sc->sc_memh) != 0) {
203 1.3.4.2 yamt
204 1.3.4.2 yamt printf("%s: unable to map registers\n", dev->dv_xname);
205 1.3.4.2 yamt return;
206 1.3.4.2 yamt }
207 1.3.4.3 yamt sc->sc_ih = intr_establish(irq, IST_EDGE, IPL_TTY, cuda_intr, sc);
208 1.3.4.2 yamt printf("\n");
209 1.3.4.2 yamt
210 1.3.4.2 yamt for (i = 0; i < 16; i++) {
211 1.3.4.2 yamt sc->sc_handlers[i].handler = NULL;
212 1.3.4.2 yamt sc->sc_handlers[i].cookie = NULL;
213 1.3.4.2 yamt }
214 1.3.4.2 yamt
215 1.3.4.2 yamt cuda_init(sc);
216 1.3.4.2 yamt
217 1.3.4.2 yamt /* now attach children */
218 1.3.4.2 yamt config_interrupts(dev, cuda_final);
219 1.3.4.2 yamt cuda_set_handler(sc, CUDA_ERROR, cuda_error_handler, sc);
220 1.3.4.2 yamt cuda_set_handler(sc, CUDA_PSEUDO, cuda_todr_handler, sc);
221 1.3.4.2 yamt
222 1.3.4.2 yamt child = OF_child(ca->ca_node);
223 1.3.4.2 yamt while (child != 0) {
224 1.3.4.2 yamt
225 1.3.4.2 yamt if (OF_getprop(child, "name", name, 32) == 0)
226 1.3.4.2 yamt continue;
227 1.3.4.2 yamt if (strncmp(name, "adb", 4) == 0) {
228 1.3.4.2 yamt
229 1.3.4.2 yamt cuda_set_handler(sc, CUDA_ADB, cuda_adb_handler, sc);
230 1.3.4.2 yamt sc->sc_adbops.cookie = sc;
231 1.3.4.2 yamt sc->sc_adbops.send = cuda_adb_send;
232 1.3.4.2 yamt sc->sc_adbops.poll = cuda_adb_poll;
233 1.3.4.2 yamt sc->sc_adbops.autopoll = cuda_autopoll;
234 1.3.4.2 yamt sc->sc_adbops.set_handler = cuda_adb_set_handler;
235 1.3.4.2 yamt config_found_ia(dev, "adb_bus", &sc->sc_adbops,
236 1.3.4.2 yamt nadb_print);
237 1.3.4.2 yamt } else if (strncmp(name, "rtc", 4) == 0) {
238 1.3.4.2 yamt
239 1.3.4.2 yamt sc->sc_todr.todr_gettime = cuda_todr_get;
240 1.3.4.2 yamt sc->sc_todr.todr_settime = cuda_todr_set;
241 1.3.4.2 yamt sc->sc_todr.cookie = sc;
242 1.3.4.2 yamt todr_attach(&sc->sc_todr);
243 1.3.4.2 yamt }
244 1.3.4.2 yamt child = OF_peer(child);
245 1.3.4.2 yamt }
246 1.3.4.2 yamt
247 1.3.4.2 yamt caa.cookie = sc;
248 1.3.4.2 yamt caa.set_handler = cuda_set_handler;
249 1.3.4.2 yamt caa.send = cuda_send;
250 1.3.4.2 yamt caa.poll = cuda_poll;
251 1.3.4.2 yamt #if notyet
252 1.3.4.2 yamt config_found(dev, &caa, cuda_print);
253 1.3.4.2 yamt #endif
254 1.3.4.2 yamt
255 1.3.4.2 yamt iba.iba_tag = &sc->sc_i2c;
256 1.3.4.2 yamt sc->sc_i2c.ic_cookie = sc;
257 1.3.4.2 yamt sc->sc_i2c.ic_acquire_bus = cuda_i2c_acquire_bus;
258 1.3.4.2 yamt sc->sc_i2c.ic_release_bus = cuda_i2c_release_bus;
259 1.3.4.2 yamt sc->sc_i2c.ic_send_start = NULL;
260 1.3.4.2 yamt sc->sc_i2c.ic_send_stop = NULL;
261 1.3.4.2 yamt sc->sc_i2c.ic_initiate_xfer = NULL;
262 1.3.4.2 yamt sc->sc_i2c.ic_read_byte = NULL;
263 1.3.4.2 yamt sc->sc_i2c.ic_write_byte = NULL;
264 1.3.4.2 yamt sc->sc_i2c.ic_exec = cuda_i2c_exec;
265 1.3.4.2 yamt config_found_ia(&sc->sc_dev, "i2cbus", &iba, iicbus_print);
266 1.3.4.2 yamt
267 1.3.4.2 yamt if (cuda0 == NULL)
268 1.3.4.2 yamt cuda0 = &caa;
269 1.3.4.2 yamt }
270 1.3.4.2 yamt
271 1.3.4.2 yamt static void
272 1.3.4.2 yamt cuda_init(struct cuda_softc *sc)
273 1.3.4.2 yamt {
274 1.3.4.2 yamt volatile int i;
275 1.3.4.2 yamt uint8_t reg;
276 1.3.4.2 yamt
277 1.3.4.2 yamt reg = cuda_read_reg(sc, vDirB);
278 1.3.4.2 yamt reg |= 0x30; /* register B bits 4 and 5: outputs */
279 1.3.4.2 yamt cuda_write_reg(sc, vDirB, reg);
280 1.3.4.2 yamt
281 1.3.4.2 yamt reg = cuda_read_reg(sc, vDirB);
282 1.3.4.2 yamt reg &= 0xf7; /* register B bit 3: input */
283 1.3.4.2 yamt cuda_write_reg(sc, vDirB, reg);
284 1.3.4.2 yamt
285 1.3.4.2 yamt reg = cuda_read_reg(sc, vACR);
286 1.3.4.2 yamt reg &= ~vSR_OUT; /* make sure SR is set to IN */
287 1.3.4.2 yamt cuda_write_reg(sc, vACR, reg);
288 1.3.4.2 yamt
289 1.3.4.2 yamt cuda_write_reg(sc, vACR, (cuda_read_reg(sc, vACR) | 0x0c) & ~0x10);
290 1.3.4.2 yamt
291 1.3.4.2 yamt sc->sc_state = CUDA_IDLE; /* used by all types of hardware */
292 1.3.4.2 yamt
293 1.3.4.2 yamt cuda_write_reg(sc, vIER, 0x84); /* make sure VIA interrupts are on */
294 1.3.4.2 yamt cuda_idle(sc); /* set ADB bus state to idle */
295 1.3.4.2 yamt
296 1.3.4.2 yamt /* sort of a device reset */
297 1.3.4.2 yamt i = cuda_read_reg(sc, vSR); /* clear interrupt */
298 1.3.4.2 yamt cuda_write_reg(sc, vIER, 0x04); /* no interrupts while clearing */
299 1.3.4.2 yamt cuda_idle(sc); /* reset state to idle */
300 1.3.4.2 yamt delay(150);
301 1.3.4.2 yamt cuda_tip(sc); /* signal start of frame */
302 1.3.4.2 yamt delay(150);
303 1.3.4.2 yamt cuda_toggle_ack(sc);
304 1.3.4.2 yamt delay(150);
305 1.3.4.2 yamt cuda_clear_tip(sc);
306 1.3.4.2 yamt delay(150);
307 1.3.4.2 yamt cuda_idle(sc); /* back to idle state */
308 1.3.4.2 yamt i = cuda_read_reg(sc, vSR); /* clear interrupt */
309 1.3.4.2 yamt cuda_write_reg(sc, vIER, 0x84); /* ints ok now */
310 1.3.4.2 yamt }
311 1.3.4.2 yamt
312 1.3.4.2 yamt static void
313 1.3.4.2 yamt cuda_final(struct device *dev)
314 1.3.4.2 yamt {
315 1.3.4.2 yamt struct cuda_softc *sc = (struct cuda_softc *)dev;
316 1.3.4.2 yamt
317 1.3.4.2 yamt sc->sc_polling = 0;
318 1.3.4.2 yamt #if 0
319 1.3.4.2 yamt {
320 1.3.4.2 yamt int err;
321 1.3.4.2 yamt uint8_t buffer[2], buf2[2];
322 1.3.4.2 yamt
323 1.3.4.2 yamt /* trying to read */
324 1.3.4.2 yamt printf("reading\n");
325 1.3.4.2 yamt buffer[0] = 0;
326 1.3.4.2 yamt buffer[1] = 1;
327 1.3.4.2 yamt buf2[0] = 0;
328 1.3.4.2 yamt err = cuda_i2c_exec(sc, I2C_OP_WRITE, 0x8a, buffer, 2, buf2, 0, 0);
329 1.3.4.2 yamt buf2[0] = 0;
330 1.3.4.2 yamt err = cuda_i2c_exec(sc, I2C_OP_WRITE | I2C_OP_READ, 0x8a, buffer, 1, buf2, 2, 0);
331 1.3.4.2 yamt printf("buf2: %02x\n", buf2[0]);
332 1.3.4.2 yamt }
333 1.3.4.2 yamt #endif
334 1.3.4.2 yamt }
335 1.3.4.2 yamt
336 1.3.4.2 yamt static inline void
337 1.3.4.2 yamt cuda_write_reg(struct cuda_softc *sc, int offset, uint8_t value)
338 1.3.4.2 yamt {
339 1.3.4.2 yamt
340 1.3.4.2 yamt bus_space_write_1(sc->sc_memt, sc->sc_memh, offset, value);
341 1.3.4.2 yamt }
342 1.3.4.2 yamt
343 1.3.4.2 yamt static inline uint8_t
344 1.3.4.2 yamt cuda_read_reg(struct cuda_softc *sc, int offset)
345 1.3.4.2 yamt {
346 1.3.4.2 yamt
347 1.3.4.2 yamt return bus_space_read_1(sc->sc_memt, sc->sc_memh, offset);
348 1.3.4.2 yamt }
349 1.3.4.2 yamt
350 1.3.4.2 yamt static int
351 1.3.4.2 yamt cuda_set_handler(void *cookie, int type,
352 1.3.4.2 yamt int (*handler)(void *, int, uint8_t *), void *hcookie)
353 1.3.4.2 yamt {
354 1.3.4.2 yamt struct cuda_softc *sc = cookie;
355 1.3.4.2 yamt CudaHandler *me;
356 1.3.4.2 yamt
357 1.3.4.2 yamt if ((type >= 0) && (type < 16)) {
358 1.3.4.2 yamt me = &sc->sc_handlers[type];
359 1.3.4.2 yamt me->handler = handler;
360 1.3.4.2 yamt me->cookie = hcookie;
361 1.3.4.2 yamt return 0;
362 1.3.4.2 yamt }
363 1.3.4.2 yamt return -1;
364 1.3.4.2 yamt }
365 1.3.4.2 yamt
366 1.3.4.2 yamt static int
367 1.3.4.2 yamt cuda_send(void *cookie, int poll, int length, uint8_t *msg)
368 1.3.4.2 yamt {
369 1.3.4.2 yamt struct cuda_softc *sc = cookie;
370 1.3.4.2 yamt int s;
371 1.3.4.2 yamt
372 1.3.4.2 yamt DPRINTF("cuda_send %08x\n", (uint32_t)cookie);
373 1.3.4.2 yamt if (sc->sc_state == CUDA_NOTREADY)
374 1.3.4.2 yamt return -1;
375 1.3.4.2 yamt
376 1.3.4.2 yamt s = splhigh();
377 1.3.4.2 yamt
378 1.3.4.2 yamt if ((sc->sc_state == CUDA_IDLE) /*&&
379 1.3.4.2 yamt ((cuda_read_reg(sc, vBufB) & vPB3) == vPB3)*/) {
380 1.3.4.2 yamt /* fine */
381 1.3.4.2 yamt DPRINTF("chip is idle\n");
382 1.3.4.2 yamt } else {
383 1.3.4.2 yamt DPRINTF("cuda state is %d\n", sc->sc_state);
384 1.3.4.2 yamt if (sc->sc_waiting == 0) {
385 1.3.4.2 yamt sc->sc_waiting = 1;
386 1.3.4.2 yamt } else {
387 1.3.4.2 yamt splx(s);
388 1.3.4.2 yamt return -1;
389 1.3.4.2 yamt }
390 1.3.4.2 yamt }
391 1.3.4.2 yamt
392 1.3.4.2 yamt sc->sc_error = 0;
393 1.3.4.2 yamt memcpy(sc->sc_out, msg, length);
394 1.3.4.2 yamt sc->sc_out_length = length;
395 1.3.4.2 yamt sc->sc_sent = 0;
396 1.3.4.2 yamt
397 1.3.4.2 yamt if (sc->sc_waiting != 1) {
398 1.3.4.2 yamt
399 1.3.4.2 yamt delay(150);
400 1.3.4.2 yamt sc->sc_state = CUDA_OUT;
401 1.3.4.2 yamt cuda_out(sc);
402 1.3.4.2 yamt cuda_write_reg(sc, vSR, sc->sc_out[0]);
403 1.3.4.2 yamt cuda_ack_off(sc);
404 1.3.4.2 yamt cuda_tip(sc);
405 1.3.4.2 yamt }
406 1.3.4.2 yamt sc->sc_waiting = 1;
407 1.3.4.2 yamt
408 1.3.4.2 yamt if (sc->sc_polling || poll || cold) {
409 1.3.4.2 yamt cuda_poll(sc);
410 1.3.4.2 yamt }
411 1.3.4.2 yamt
412 1.3.4.2 yamt splx(s);
413 1.3.4.2 yamt
414 1.3.4.2 yamt return 0;
415 1.3.4.2 yamt }
416 1.3.4.2 yamt
417 1.3.4.2 yamt static void
418 1.3.4.2 yamt cuda_poll(void *cookie)
419 1.3.4.2 yamt {
420 1.3.4.2 yamt struct cuda_softc *sc = cookie;
421 1.3.4.2 yamt int s;
422 1.3.4.2 yamt
423 1.3.4.2 yamt DPRINTF("polling\n");
424 1.3.4.2 yamt while ((sc->sc_state != CUDA_IDLE) ||
425 1.3.4.2 yamt (cuda_intr_state(sc)) ||
426 1.3.4.2 yamt (sc->sc_waiting == 1)) {
427 1.3.4.2 yamt if ((cuda_read_reg(sc, vIFR) & vSR_INT) == vSR_INT) {
428 1.3.4.2 yamt s = splhigh();
429 1.3.4.2 yamt cuda_intr(sc);
430 1.3.4.2 yamt splx(s);
431 1.3.4.2 yamt }
432 1.3.4.2 yamt }
433 1.3.4.2 yamt }
434 1.3.4.2 yamt
435 1.3.4.2 yamt static void
436 1.3.4.2 yamt cuda_adb_poll(void *cookie)
437 1.3.4.2 yamt {
438 1.3.4.2 yamt struct cuda_softc *sc = cookie;
439 1.3.4.2 yamt int s;
440 1.3.4.2 yamt
441 1.3.4.2 yamt s = splhigh();
442 1.3.4.2 yamt cuda_intr(sc);
443 1.3.4.2 yamt splx(s);
444 1.3.4.2 yamt }
445 1.3.4.2 yamt
446 1.3.4.2 yamt static void
447 1.3.4.2 yamt cuda_idle(struct cuda_softc *sc)
448 1.3.4.2 yamt {
449 1.3.4.2 yamt uint8_t reg;
450 1.3.4.2 yamt
451 1.3.4.2 yamt reg = cuda_read_reg(sc, vBufB);
452 1.3.4.2 yamt reg |= (vPB4 | vPB5);
453 1.3.4.2 yamt cuda_write_reg(sc, vBufB, reg);
454 1.3.4.2 yamt }
455 1.3.4.2 yamt
456 1.3.4.2 yamt static void
457 1.3.4.2 yamt cuda_tip(struct cuda_softc *sc)
458 1.3.4.2 yamt {
459 1.3.4.2 yamt uint8_t reg;
460 1.3.4.2 yamt
461 1.3.4.2 yamt reg = cuda_read_reg(sc, vBufB);
462 1.3.4.2 yamt reg &= ~vPB5;
463 1.3.4.2 yamt cuda_write_reg(sc, vBufB, reg);
464 1.3.4.2 yamt }
465 1.3.4.2 yamt
466 1.3.4.2 yamt static void
467 1.3.4.2 yamt cuda_clear_tip(struct cuda_softc *sc)
468 1.3.4.2 yamt {
469 1.3.4.2 yamt uint8_t reg;
470 1.3.4.2 yamt
471 1.3.4.2 yamt reg = cuda_read_reg(sc, vBufB);
472 1.3.4.2 yamt reg |= vPB5;
473 1.3.4.2 yamt cuda_write_reg(sc, vBufB, reg);
474 1.3.4.2 yamt }
475 1.3.4.2 yamt
476 1.3.4.2 yamt static void
477 1.3.4.2 yamt cuda_in(struct cuda_softc *sc)
478 1.3.4.2 yamt {
479 1.3.4.2 yamt uint8_t reg;
480 1.3.4.2 yamt
481 1.3.4.2 yamt reg = cuda_read_reg(sc, vACR);
482 1.3.4.2 yamt reg &= ~vSR_OUT;
483 1.3.4.2 yamt cuda_write_reg(sc, vACR, reg);
484 1.3.4.2 yamt }
485 1.3.4.2 yamt
486 1.3.4.2 yamt static void
487 1.3.4.2 yamt cuda_out(struct cuda_softc *sc)
488 1.3.4.2 yamt {
489 1.3.4.2 yamt uint8_t reg;
490 1.3.4.2 yamt
491 1.3.4.2 yamt reg = cuda_read_reg(sc, vACR);
492 1.3.4.2 yamt reg |= vSR_OUT;
493 1.3.4.2 yamt cuda_write_reg(sc, vACR, reg);
494 1.3.4.2 yamt }
495 1.3.4.2 yamt
496 1.3.4.2 yamt static void
497 1.3.4.2 yamt cuda_toggle_ack(struct cuda_softc *sc)
498 1.3.4.2 yamt {
499 1.3.4.2 yamt uint8_t reg;
500 1.3.4.2 yamt
501 1.3.4.2 yamt reg = cuda_read_reg(sc, vBufB);
502 1.3.4.2 yamt reg ^= vPB4;
503 1.3.4.2 yamt cuda_write_reg(sc, vBufB, reg);
504 1.3.4.2 yamt }
505 1.3.4.2 yamt
506 1.3.4.2 yamt static void
507 1.3.4.2 yamt cuda_ack_off(struct cuda_softc *sc)
508 1.3.4.2 yamt {
509 1.3.4.2 yamt uint8_t reg;
510 1.3.4.2 yamt
511 1.3.4.2 yamt reg = cuda_read_reg(sc, vBufB);
512 1.3.4.2 yamt reg |= vPB4;
513 1.3.4.2 yamt cuda_write_reg(sc, vBufB, reg);
514 1.3.4.2 yamt }
515 1.3.4.2 yamt
516 1.3.4.2 yamt static int
517 1.3.4.2 yamt cuda_intr_state(struct cuda_softc *sc)
518 1.3.4.2 yamt {
519 1.3.4.2 yamt return ((cuda_read_reg(sc, vBufB) & vPB3) == 0);
520 1.3.4.2 yamt }
521 1.3.4.2 yamt
522 1.3.4.2 yamt static int
523 1.3.4.2 yamt cuda_intr(void *arg)
524 1.3.4.2 yamt {
525 1.3.4.2 yamt struct cuda_softc *sc = arg;
526 1.3.4.2 yamt int i, ending, type;
527 1.3.4.2 yamt uint8_t reg;
528 1.3.4.2 yamt
529 1.3.4.2 yamt reg = cuda_read_reg(sc, vIFR); /* Read the interrupts */
530 1.3.4.2 yamt DPRINTF("[");
531 1.3.4.2 yamt if ((reg & 0x80) == 0) {
532 1.3.4.2 yamt DPRINTF("irq %02x]", reg);
533 1.3.4.2 yamt return 0; /* No interrupts to process */
534 1.3.4.2 yamt }
535 1.3.4.2 yamt DPRINTF(":");
536 1.3.4.2 yamt
537 1.3.4.2 yamt cuda_write_reg(sc, vIFR, 0x7f); /* Clear 'em */
538 1.3.4.2 yamt
539 1.3.4.2 yamt switch_start:
540 1.3.4.2 yamt switch (sc->sc_state) {
541 1.3.4.2 yamt case CUDA_IDLE:
542 1.3.4.2 yamt /*
543 1.3.4.2 yamt * This is an unexpected packet, so grab the first (dummy)
544 1.3.4.2 yamt * byte, set up the proper vars, and tell the chip we are
545 1.3.4.2 yamt * starting to receive the packet by setting the TIP bit.
546 1.3.4.2 yamt */
547 1.3.4.2 yamt sc->sc_in[1] = cuda_read_reg(sc, vSR);
548 1.3.4.2 yamt DPRINTF("start: %02x", sc->sc_in[1]);
549 1.3.4.2 yamt if (cuda_intr_state(sc) == 0) {
550 1.3.4.2 yamt /* must have been a fake start */
551 1.3.4.2 yamt DPRINTF(" ... fake start\n");
552 1.3.4.2 yamt if (sc->sc_waiting) {
553 1.3.4.2 yamt /* start over */
554 1.3.4.2 yamt delay(150);
555 1.3.4.2 yamt sc->sc_state = CUDA_OUT;
556 1.3.4.2 yamt sc->sc_sent = 0;
557 1.3.4.2 yamt cuda_out(sc);
558 1.3.4.2 yamt cuda_write_reg(sc, vSR, sc->sc_out[1]);
559 1.3.4.2 yamt cuda_ack_off(sc);
560 1.3.4.2 yamt cuda_tip(sc);
561 1.3.4.2 yamt }
562 1.3.4.2 yamt break;
563 1.3.4.2 yamt }
564 1.3.4.2 yamt
565 1.3.4.2 yamt cuda_in(sc);
566 1.3.4.2 yamt cuda_tip(sc);
567 1.3.4.2 yamt
568 1.3.4.2 yamt sc->sc_received = 1;
569 1.3.4.2 yamt sc->sc_state = CUDA_IN;
570 1.3.4.2 yamt DPRINTF(" CUDA_IN");
571 1.3.4.2 yamt break;
572 1.3.4.2 yamt
573 1.3.4.2 yamt case CUDA_IN:
574 1.3.4.2 yamt sc->sc_in[sc->sc_received] = cuda_read_reg(sc, vSR);
575 1.3.4.2 yamt DPRINTF(" %02x", sc->sc_in[sc->sc_received]);
576 1.3.4.2 yamt ending = 0;
577 1.3.4.2 yamt if (sc->sc_received > 255) {
578 1.3.4.2 yamt /* bitch only once */
579 1.3.4.2 yamt if (sc->sc_received == 256) {
580 1.3.4.2 yamt printf("%s: input overflow\n",
581 1.3.4.2 yamt sc->sc_dev.dv_xname);
582 1.3.4.2 yamt ending = 1;
583 1.3.4.2 yamt }
584 1.3.4.2 yamt } else
585 1.3.4.2 yamt sc->sc_received++;
586 1.3.4.2 yamt if (sc->sc_received > 3) {
587 1.3.4.2 yamt if ((sc->sc_in[3] == CMD_IIC) &&
588 1.3.4.2 yamt (sc->sc_received > (sc->sc_i2c_read_len + 4))) {
589 1.3.4.2 yamt ending = 1;
590 1.3.4.2 yamt }
591 1.3.4.2 yamt }
592 1.3.4.2 yamt
593 1.3.4.2 yamt /* intr off means this is the last byte (end of frame) */
594 1.3.4.2 yamt if (cuda_intr_state(sc) == 0) {
595 1.3.4.2 yamt ending = 1;
596 1.3.4.2 yamt DPRINTF(".\n");
597 1.3.4.2 yamt } else {
598 1.3.4.2 yamt cuda_toggle_ack(sc);
599 1.3.4.2 yamt }
600 1.3.4.2 yamt
601 1.3.4.2 yamt if (ending == 1) { /* end of message? */
602 1.3.4.2 yamt
603 1.3.4.2 yamt sc->sc_in[0] = sc->sc_received - 1;
604 1.3.4.2 yamt
605 1.3.4.2 yamt /* reset vars and signal the end of this frame */
606 1.3.4.2 yamt cuda_idle(sc);
607 1.3.4.2 yamt
608 1.3.4.2 yamt /* check if we have a handler for this message */
609 1.3.4.2 yamt type = sc->sc_in[1];
610 1.3.4.2 yamt if ((type >= 0) && (type < 16)) {
611 1.3.4.2 yamt CudaHandler *me = &sc->sc_handlers[type];
612 1.3.4.2 yamt
613 1.3.4.2 yamt if (me->handler != NULL) {
614 1.3.4.2 yamt me->handler(me->cookie,
615 1.3.4.2 yamt sc->sc_received - 1, &sc->sc_in[1]);
616 1.3.4.2 yamt } else {
617 1.3.4.2 yamt printf("no handler for type %02x\n", type);
618 1.3.4.2 yamt panic("barf");
619 1.3.4.2 yamt }
620 1.3.4.2 yamt }
621 1.3.4.2 yamt
622 1.3.4.2 yamt DPRINTF("CUDA_IDLE");
623 1.3.4.2 yamt sc->sc_state = CUDA_IDLE;
624 1.3.4.2 yamt
625 1.3.4.2 yamt sc->sc_received = 0;
626 1.3.4.2 yamt
627 1.3.4.2 yamt /*
628 1.3.4.2 yamt * If there is something waiting to be sent out,
629 1.3.4.2 yamt * set everything up and send the first byte.
630 1.3.4.2 yamt */
631 1.3.4.2 yamt if (sc->sc_waiting == 1) {
632 1.3.4.2 yamt
633 1.3.4.2 yamt DPRINTF("pending write\n");
634 1.3.4.2 yamt delay(1500); /* required */
635 1.3.4.2 yamt sc->sc_sent = 0;
636 1.3.4.2 yamt sc->sc_state = CUDA_OUT;
637 1.3.4.2 yamt
638 1.3.4.2 yamt /*
639 1.3.4.2 yamt * If the interrupt is on, we were too slow
640 1.3.4.2 yamt * and the chip has already started to send
641 1.3.4.2 yamt * something to us, so back out of the write
642 1.3.4.2 yamt * and start a read cycle.
643 1.3.4.2 yamt */
644 1.3.4.2 yamt if (cuda_intr_state(sc)) {
645 1.3.4.2 yamt cuda_in(sc);
646 1.3.4.2 yamt cuda_idle(sc);
647 1.3.4.2 yamt sc->sc_sent = 0;
648 1.3.4.2 yamt sc->sc_state = CUDA_IDLE;
649 1.3.4.2 yamt sc->sc_received = 0;
650 1.3.4.2 yamt delay(150);
651 1.3.4.2 yamt DPRINTF("too slow - incoming message\n");
652 1.3.4.2 yamt goto switch_start;
653 1.3.4.2 yamt }
654 1.3.4.2 yamt /*
655 1.3.4.2 yamt * If we got here, it's ok to start sending
656 1.3.4.2 yamt * so load the first byte and tell the chip
657 1.3.4.2 yamt * we want to send.
658 1.3.4.2 yamt */
659 1.3.4.2 yamt DPRINTF("sending ");
660 1.3.4.2 yamt
661 1.3.4.2 yamt cuda_out(sc);
662 1.3.4.2 yamt cuda_write_reg(sc, vSR,
663 1.3.4.2 yamt sc->sc_out[sc->sc_sent]);
664 1.3.4.2 yamt cuda_ack_off(sc);
665 1.3.4.2 yamt cuda_tip(sc);
666 1.3.4.2 yamt }
667 1.3.4.2 yamt }
668 1.3.4.2 yamt break;
669 1.3.4.2 yamt
670 1.3.4.2 yamt case CUDA_OUT:
671 1.3.4.2 yamt i = cuda_read_reg(sc, vSR); /* reset SR-intr in IFR */
672 1.3.4.2 yamt
673 1.3.4.2 yamt sc->sc_sent++;
674 1.3.4.2 yamt if (cuda_intr_state(sc)) { /* ADB intr low during write */
675 1.3.4.2 yamt
676 1.3.4.2 yamt DPRINTF("incoming msg during send\n");
677 1.3.4.2 yamt cuda_in(sc); /* make sure SR is set to IN */
678 1.3.4.2 yamt cuda_idle(sc);
679 1.3.4.2 yamt sc->sc_sent = 0; /* must start all over */
680 1.3.4.2 yamt sc->sc_state = CUDA_IDLE; /* new state */
681 1.3.4.2 yamt sc->sc_received = 0;
682 1.3.4.2 yamt sc->sc_waiting = 1; /* must retry when done with
683 1.3.4.2 yamt * read */
684 1.3.4.2 yamt delay(150);
685 1.3.4.2 yamt goto switch_start; /* process next state right
686 1.3.4.2 yamt * now */
687 1.3.4.2 yamt break;
688 1.3.4.2 yamt }
689 1.3.4.2 yamt if (sc->sc_out_length == sc->sc_sent) { /* check for done */
690 1.3.4.2 yamt
691 1.3.4.2 yamt sc->sc_waiting = 0; /* done writing */
692 1.3.4.2 yamt sc->sc_state = CUDA_IDLE; /* signal bus is idle */
693 1.3.4.2 yamt cuda_in(sc);
694 1.3.4.2 yamt cuda_idle(sc);
695 1.3.4.2 yamt DPRINTF("done sending\n");
696 1.3.4.2 yamt } else {
697 1.3.4.2 yamt /* send next byte */
698 1.3.4.2 yamt cuda_write_reg(sc, vSR, sc->sc_out[sc->sc_sent]);
699 1.3.4.2 yamt cuda_toggle_ack(sc); /* signal byte ready to
700 1.3.4.2 yamt * shift */
701 1.3.4.2 yamt }
702 1.3.4.2 yamt break;
703 1.3.4.2 yamt
704 1.3.4.2 yamt case CUDA_NOTREADY:
705 1.3.4.2 yamt DPRINTF("adb: not yet initialized\n");
706 1.3.4.2 yamt break;
707 1.3.4.2 yamt
708 1.3.4.2 yamt default:
709 1.3.4.2 yamt DPRINTF("intr: unknown ADB state\n");
710 1.3.4.2 yamt break;
711 1.3.4.2 yamt }
712 1.3.4.2 yamt
713 1.3.4.2 yamt DPRINTF("]");
714 1.3.4.2 yamt return 1;
715 1.3.4.2 yamt }
716 1.3.4.2 yamt
717 1.3.4.2 yamt static int
718 1.3.4.2 yamt cuda_error_handler(void *cookie, int len, uint8_t *data)
719 1.3.4.2 yamt {
720 1.3.4.2 yamt struct cuda_softc *sc = cookie;
721 1.3.4.2 yamt
722 1.3.4.2 yamt /*
723 1.3.4.2 yamt * something went wrong
724 1.3.4.2 yamt * byte 3 seems to be the failed command
725 1.3.4.2 yamt */
726 1.3.4.2 yamt sc->sc_error = 1;
727 1.3.4.2 yamt wakeup(&sc->sc_todev);
728 1.3.4.2 yamt return 0;
729 1.3.4.2 yamt }
730 1.3.4.2 yamt
731 1.3.4.2 yamt
732 1.3.4.2 yamt /* real time clock */
733 1.3.4.2 yamt
734 1.3.4.2 yamt static int
735 1.3.4.2 yamt cuda_todr_handler(void *cookie, int len, uint8_t *data)
736 1.3.4.2 yamt {
737 1.3.4.2 yamt struct cuda_softc *sc = cookie;
738 1.3.4.2 yamt
739 1.3.4.2 yamt #ifdef CUDA_DEBUG
740 1.3.4.2 yamt int i;
741 1.3.4.2 yamt printf("msg: %02x", data[0]);
742 1.3.4.2 yamt for (i = 1; i < len; i++) {
743 1.3.4.2 yamt printf(" %02x", data[i]);
744 1.3.4.2 yamt }
745 1.3.4.2 yamt printf("\n");
746 1.3.4.2 yamt #endif
747 1.3.4.2 yamt
748 1.3.4.2 yamt switch(data[2]) {
749 1.3.4.2 yamt case CMD_READ_RTC:
750 1.3.4.2 yamt memcpy(&sc->sc_tod, &data[3], 4);
751 1.3.4.2 yamt break;
752 1.3.4.2 yamt case CMD_WRITE_RTC:
753 1.3.4.2 yamt sc->sc_tod = 0xffffffff;
754 1.3.4.2 yamt break;
755 1.3.4.2 yamt case CMD_AUTOPOLL:
756 1.3.4.2 yamt sc->sc_autopoll = 1;
757 1.3.4.2 yamt break;
758 1.3.4.2 yamt case CMD_IIC:
759 1.3.4.2 yamt sc->sc_iic_done = len;
760 1.3.4.2 yamt break;
761 1.3.4.2 yamt }
762 1.3.4.2 yamt wakeup(&sc->sc_todev);
763 1.3.4.2 yamt return 0;
764 1.3.4.2 yamt }
765 1.3.4.2 yamt
766 1.3.4.2 yamt #define DIFF19041970 2082844800
767 1.3.4.2 yamt
768 1.3.4.2 yamt static int
769 1.3.4.2 yamt cuda_todr_get(todr_chip_handle_t tch, volatile struct timeval *tvp)
770 1.3.4.2 yamt {
771 1.3.4.2 yamt struct cuda_softc *sc = tch->cookie;
772 1.3.4.2 yamt int cnt = 0;
773 1.3.4.2 yamt uint8_t cmd[] = { CUDA_PSEUDO, CMD_READ_RTC};
774 1.3.4.2 yamt
775 1.3.4.2 yamt sc->sc_tod = 0;
776 1.3.4.2 yamt cuda_send(sc, 0, 2, cmd);
777 1.3.4.2 yamt
778 1.3.4.2 yamt while ((sc->sc_tod == 0) && (cnt < 10)) {
779 1.3.4.2 yamt tsleep(&sc->sc_todev, 0, "todr", 10);
780 1.3.4.2 yamt cnt++;
781 1.3.4.2 yamt }
782 1.3.4.2 yamt
783 1.3.4.2 yamt if (sc->sc_tod == 0)
784 1.3.4.2 yamt return EIO;
785 1.3.4.2 yamt
786 1.3.4.2 yamt tvp->tv_sec = sc->sc_tod - DIFF19041970;
787 1.3.4.2 yamt DPRINTF("tod: %ld\n", tvp->tv_sec);
788 1.3.4.2 yamt tvp->tv_usec = 0;
789 1.3.4.2 yamt return 0;
790 1.3.4.2 yamt }
791 1.3.4.2 yamt
792 1.3.4.2 yamt static int
793 1.3.4.2 yamt cuda_todr_set(todr_chip_handle_t tch, volatile struct timeval *tvp)
794 1.3.4.2 yamt {
795 1.3.4.2 yamt struct cuda_softc *sc = tch->cookie;
796 1.3.4.2 yamt uint32_t sec;
797 1.3.4.2 yamt uint8_t cmd[] = {CUDA_PSEUDO, CMD_WRITE_RTC, 0, 0, 0, 0};
798 1.3.4.2 yamt
799 1.3.4.2 yamt sec = tvp->tv_sec + DIFF19041970;
800 1.3.4.2 yamt memcpy(&cmd[2], &sec, 4);
801 1.3.4.2 yamt sc->sc_tod = 0;
802 1.3.4.2 yamt if (cuda_send(sc, 0, 6, cmd) == 0) {
803 1.3.4.2 yamt while (sc->sc_tod == 0) {
804 1.3.4.2 yamt tsleep(&sc->sc_todev, 0, "todr", 10);
805 1.3.4.2 yamt }
806 1.3.4.2 yamt return 0;
807 1.3.4.2 yamt }
808 1.3.4.2 yamt return -1;
809 1.3.4.2 yamt
810 1.3.4.2 yamt }
811 1.3.4.2 yamt
812 1.3.4.2 yamt /* poweroff and reboot */
813 1.3.4.2 yamt
814 1.3.4.2 yamt void
815 1.3.4.2 yamt cuda_poweroff()
816 1.3.4.2 yamt {
817 1.3.4.2 yamt struct cuda_softc *sc;
818 1.3.4.2 yamt uint8_t cmd[] = {CUDA_PSEUDO, CMD_POWEROFF};
819 1.3.4.2 yamt
820 1.3.4.2 yamt if (cuda0 == NULL)
821 1.3.4.2 yamt return;
822 1.3.4.2 yamt sc = cuda0->cookie;
823 1.3.4.2 yamt sc->sc_polling = 1;
824 1.3.4.2 yamt cuda0->poll(sc);
825 1.3.4.2 yamt if (cuda0->send(sc, 1, 2, cmd) == 0)
826 1.3.4.2 yamt while (1);
827 1.3.4.2 yamt }
828 1.3.4.2 yamt
829 1.3.4.2 yamt void
830 1.3.4.2 yamt cuda_restart()
831 1.3.4.2 yamt {
832 1.3.4.2 yamt struct cuda_softc *sc;
833 1.3.4.2 yamt uint8_t cmd[] = {CUDA_PSEUDO, CMD_RESET};
834 1.3.4.2 yamt
835 1.3.4.2 yamt if (cuda0 == NULL)
836 1.3.4.2 yamt return;
837 1.3.4.2 yamt sc = cuda0->cookie;
838 1.3.4.2 yamt sc->sc_polling = 1;
839 1.3.4.2 yamt cuda0->poll(sc);
840 1.3.4.2 yamt if (cuda0->send(sc, 1, 2, cmd) == 0)
841 1.3.4.2 yamt while (1);
842 1.3.4.2 yamt }
843 1.3.4.2 yamt
844 1.3.4.2 yamt /* ADB message handling */
845 1.3.4.2 yamt
846 1.3.4.2 yamt static void
847 1.3.4.2 yamt cuda_autopoll(void *cookie, int flag)
848 1.3.4.2 yamt {
849 1.3.4.2 yamt struct cuda_softc *sc = cookie;
850 1.3.4.2 yamt uint8_t cmd[] = {CUDA_PSEUDO, CMD_AUTOPOLL, (flag != 0)};
851 1.3.4.2 yamt
852 1.3.4.2 yamt if (cmd[2] == sc->sc_autopoll)
853 1.3.4.2 yamt return;
854 1.3.4.2 yamt
855 1.3.4.2 yamt sc->sc_autopoll = -1;
856 1.3.4.2 yamt cuda_send(sc, 0, 3, cmd);
857 1.3.4.2 yamt while(sc->sc_autopoll == -1) {
858 1.3.4.2 yamt if (sc->sc_polling || cold) {
859 1.3.4.2 yamt cuda_poll(sc);
860 1.3.4.2 yamt } else
861 1.3.4.2 yamt tsleep(&sc->sc_todev, 0, "autopoll", 100);
862 1.3.4.2 yamt }
863 1.3.4.2 yamt }
864 1.3.4.2 yamt
865 1.3.4.2 yamt static int
866 1.3.4.2 yamt cuda_adb_handler(void *cookie, int len, uint8_t *data)
867 1.3.4.2 yamt {
868 1.3.4.2 yamt struct cuda_softc *sc = cookie;
869 1.3.4.2 yamt
870 1.3.4.2 yamt if (sc->sc_adb_handler != NULL) {
871 1.3.4.2 yamt sc->sc_adb_handler(sc->sc_adb_cookie, len - 1,
872 1.3.4.2 yamt &data[1]);
873 1.3.4.2 yamt return 0;
874 1.3.4.2 yamt }
875 1.3.4.2 yamt return -1;
876 1.3.4.2 yamt }
877 1.3.4.2 yamt
878 1.3.4.2 yamt static int
879 1.3.4.2 yamt cuda_adb_send(void *cookie, int poll, int command, int len, uint8_t *data)
880 1.3.4.2 yamt {
881 1.3.4.2 yamt struct cuda_softc *sc = cookie;
882 1.3.4.2 yamt int i, s = 0;
883 1.3.4.2 yamt uint8_t packet[16];
884 1.3.4.2 yamt
885 1.3.4.2 yamt /* construct an ADB command packet and send it */
886 1.3.4.2 yamt packet[0] = CUDA_ADB;
887 1.3.4.2 yamt packet[1] = command;
888 1.3.4.2 yamt for (i = 0; i < len; i++)
889 1.3.4.2 yamt packet[i + 2] = data[i];
890 1.3.4.2 yamt if (poll || cold) {
891 1.3.4.2 yamt s = splhigh();
892 1.3.4.2 yamt cuda_poll(sc);
893 1.3.4.2 yamt }
894 1.3.4.2 yamt cuda_send(sc, poll, len + 2, packet);
895 1.3.4.2 yamt if (poll || cold) {
896 1.3.4.2 yamt cuda_poll(sc);
897 1.3.4.2 yamt splx(s);
898 1.3.4.2 yamt }
899 1.3.4.2 yamt return 0;
900 1.3.4.2 yamt }
901 1.3.4.2 yamt
902 1.3.4.2 yamt static int
903 1.3.4.2 yamt cuda_adb_set_handler(void *cookie, void (*handler)(void *, int, uint8_t *),
904 1.3.4.2 yamt void *hcookie)
905 1.3.4.2 yamt {
906 1.3.4.2 yamt struct cuda_softc *sc = cookie;
907 1.3.4.2 yamt
908 1.3.4.2 yamt /* register a callback for incoming ADB messages */
909 1.3.4.2 yamt sc->sc_adb_handler = handler;
910 1.3.4.2 yamt sc->sc_adb_cookie = hcookie;
911 1.3.4.2 yamt return 0;
912 1.3.4.2 yamt }
913 1.3.4.2 yamt
914 1.3.4.2 yamt /* i2c message handling */
915 1.3.4.2 yamt
916 1.3.4.2 yamt static int
917 1.3.4.2 yamt cuda_i2c_acquire_bus(void *cookie, int flags)
918 1.3.4.2 yamt {
919 1.3.4.2 yamt /* nothing yet */
920 1.3.4.2 yamt return 0;
921 1.3.4.2 yamt }
922 1.3.4.2 yamt
923 1.3.4.2 yamt static void
924 1.3.4.2 yamt cuda_i2c_release_bus(void *cookie, int flags)
925 1.3.4.2 yamt {
926 1.3.4.2 yamt /* nothing here either */
927 1.3.4.2 yamt }
928 1.3.4.2 yamt
929 1.3.4.2 yamt static int
930 1.3.4.2 yamt cuda_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *_send,
931 1.3.4.2 yamt size_t send_len, void *_recv, size_t recv_len, int flags)
932 1.3.4.2 yamt {
933 1.3.4.2 yamt struct cuda_softc *sc = cookie;
934 1.3.4.2 yamt const uint8_t *send = _send;
935 1.3.4.2 yamt uint8_t *recv = _recv;
936 1.3.4.2 yamt uint8_t command[16] = {CUDA_PSEUDO, CMD_IIC};
937 1.3.4.2 yamt
938 1.3.4.2 yamt DPRINTF("cuda_i2c_exec(%02x)\n", addr);
939 1.3.4.2 yamt command[2] = addr;
940 1.3.4.2 yamt
941 1.3.4.2 yamt memcpy(&command[3], send, min((int)send_len, 12));
942 1.3.4.2 yamt
943 1.3.4.2 yamt sc->sc_iic_done = 0;
944 1.3.4.2 yamt cuda_send(sc, sc->sc_polling, send_len + 3, command);
945 1.3.4.2 yamt
946 1.3.4.2 yamt while ((sc->sc_iic_done == 0) && (sc->sc_error == 0)) {
947 1.3.4.2 yamt if (sc->sc_polling || cold) {
948 1.3.4.2 yamt cuda_poll(sc);
949 1.3.4.2 yamt } else
950 1.3.4.2 yamt tsleep(&sc->sc_todev, 0, "i2c", 1000);
951 1.3.4.2 yamt }
952 1.3.4.2 yamt
953 1.3.4.2 yamt if (sc->sc_error) {
954 1.3.4.2 yamt sc->sc_error = 0;
955 1.3.4.2 yamt return -1;
956 1.3.4.2 yamt }
957 1.3.4.2 yamt
958 1.3.4.2 yamt /* see if we're supposed to do a read */
959 1.3.4.2 yamt if (recv_len > 0) {
960 1.3.4.2 yamt sc->sc_iic_done = 0;
961 1.3.4.2 yamt command[2] |= 1;
962 1.3.4.2 yamt command[3] = 0;
963 1.3.4.2 yamt
964 1.3.4.2 yamt /*
965 1.3.4.2 yamt * XXX we need to do something to limit the size of the answer
966 1.3.4.2 yamt * - apparently the chip keeps sending until we tell it to stop
967 1.3.4.2 yamt */
968 1.3.4.2 yamt sc->sc_i2c_read_len = recv_len;
969 1.3.4.2 yamt DPRINTF("rcv_len: %d\n", recv_len);
970 1.3.4.2 yamt cuda_send(sc, sc->sc_polling, 3, command);
971 1.3.4.2 yamt while ((sc->sc_iic_done == 0) && (sc->sc_error == 0)) {
972 1.3.4.2 yamt if (sc->sc_polling || cold) {
973 1.3.4.2 yamt cuda_poll(sc);
974 1.3.4.2 yamt } else
975 1.3.4.2 yamt tsleep(&sc->sc_todev, 0, "i2c", 1000);
976 1.3.4.2 yamt }
977 1.3.4.2 yamt
978 1.3.4.2 yamt if (sc->sc_error) {
979 1.3.4.2 yamt printf("error trying to read\n");
980 1.3.4.2 yamt sc->sc_error = 0;
981 1.3.4.2 yamt return -1;
982 1.3.4.2 yamt }
983 1.3.4.2 yamt }
984 1.3.4.2 yamt
985 1.3.4.2 yamt DPRINTF("received: %d\n", sc->sc_iic_done);
986 1.3.4.2 yamt if ((sc->sc_iic_done > 3) && (recv_len > 0)) {
987 1.3.4.2 yamt int rlen;
988 1.3.4.2 yamt
989 1.3.4.2 yamt /* we got an answer */
990 1.3.4.2 yamt rlen = min(sc->sc_iic_done - 3, recv_len);
991 1.3.4.2 yamt memcpy(recv, &sc->sc_in[4], rlen);
992 1.3.4.2 yamt #ifdef CUDA_DEBUG
993 1.3.4.2 yamt {
994 1.3.4.2 yamt int i;
995 1.3.4.2 yamt printf("ret:");
996 1.3.4.2 yamt for (i = 0; i < rlen; i++)
997 1.3.4.2 yamt printf(" %02x", recv[i]);
998 1.3.4.2 yamt printf("\n");
999 1.3.4.2 yamt }
1000 1.3.4.2 yamt #endif
1001 1.3.4.2 yamt return rlen;
1002 1.3.4.2 yamt }
1003 1.3.4.2 yamt return 0;
1004 1.3.4.2 yamt }
1005