maple.c revision 1.12 1 1.12 uch /* $NetBSD: maple.c,v 1.12 2002/03/25 18:59:40 uch Exp $ */
2 1.1 marcus
3 1.3 marcus /*-
4 1.1 marcus * Copyright (c) 2001 Marcus Comstedt
5 1.1 marcus * All rights reserved.
6 1.1 marcus *
7 1.1 marcus * Redistribution and use in source and binary forms, with or without
8 1.1 marcus * modification, are permitted provided that the following conditions
9 1.1 marcus * are met:
10 1.1 marcus * 1. Redistributions of source code must retain the above copyright
11 1.1 marcus * notice, this list of conditions and the following disclaimer.
12 1.1 marcus * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 marcus * notice, this list of conditions and the following disclaimer in the
14 1.1 marcus * documentation and/or other materials provided with the distribution.
15 1.1 marcus * 3. All advertising materials mentioning features or use of this software
16 1.1 marcus * must display the following acknowledgement:
17 1.3 marcus * This product includes software developed by Marcus Comstedt.
18 1.3 marcus * 4. Neither the name of The NetBSD Foundation nor the names of its
19 1.3 marcus * contributors may be used to endorse or promote products derived
20 1.3 marcus * from this software without specific prior written permission.
21 1.1 marcus *
22 1.3 marcus * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 1.3 marcus * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 1.3 marcus * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 1.3 marcus * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 1.3 marcus * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.3 marcus * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.3 marcus * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.3 marcus * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.3 marcus * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.3 marcus * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 1.3 marcus * POSSIBILITY OF SUCH DAMAGE.
33 1.1 marcus */
34 1.1 marcus
35 1.1 marcus #include <sys/param.h>
36 1.1 marcus #include <sys/device.h>
37 1.1 marcus #include <sys/fcntl.h>
38 1.1 marcus #include <sys/poll.h>
39 1.1 marcus #include <sys/select.h>
40 1.1 marcus #include <sys/proc.h>
41 1.1 marcus #include <sys/signalvar.h>
42 1.1 marcus #include <sys/systm.h>
43 1.1 marcus
44 1.1 marcus #include <uvm/uvm_extern.h>
45 1.1 marcus
46 1.1 marcus #include <machine/cpu.h>
47 1.1 marcus #include <machine/bus.h>
48 1.1 marcus #include <sh3/pmap.h>
49 1.1 marcus
50 1.1 marcus #include <dreamcast/dev/maple/maple.h>
51 1.1 marcus #include <dreamcast/dev/maple/mapleconf.h>
52 1.1 marcus #include <dreamcast/dev/maple/maplevar.h>
53 1.1 marcus #include <dreamcast/dev/maple/maplereg.h>
54 1.7 marcus #include <dreamcast/dev/maple/mapleio.h>
55 1.1 marcus
56 1.11 uch #include "locators.h"
57 1.1 marcus
58 1.7 marcus /* Internal macros, functions, and variables. */
59 1.7 marcus
60 1.5 marcus #define MAPLE_CALLOUT_TICKS 2
61 1.1 marcus
62 1.7 marcus #define MAPLEBUSUNIT(dev) (minor(dev)>>5)
63 1.7 marcus #define MAPLEPORT(dev) ((minor(dev) & 0x18) >> 3)
64 1.7 marcus #define MAPLESUBUNIT(dev) (minor(dev) & 0x7)
65 1.7 marcus
66 1.1 marcus /*
67 1.1 marcus * Function declarations.
68 1.1 marcus */
69 1.12 uch static int maplematch(struct device *, struct cfdata *, void *);
70 1.12 uch static void mapleattach(struct device *, struct device *, void *);
71 1.12 uch static int mapleprint(void *, const char *);
72 1.12 uch static int maplesubmatch(struct device *, struct cfdata *, void *);
73 1.12 uch static void maple_attach_dev(struct maple_softc *, int, int);
74 1.12 uch static void maple_begin_txbuf(struct maple_softc *);
75 1.12 uch static int maple_end_txbuf(struct maple_softc *);
76 1.12 uch static void maple_write_command(struct maple_softc *, int, int,
77 1.12 uch int, int, void *);
78 1.12 uch static void maple_scanbus(struct maple_softc *);
79 1.12 uch static void maple_callout(void *);
80 1.12 uch static void maple_send_commands(struct maple_softc *);
81 1.12 uch static void maple_check_responses(struct maple_softc *);
82 1.12 uch
83 1.12 uch int maple_alloc_dma(size_t, vaddr_t *, paddr_t *);
84 1.12 uch void maple_free_dma(paddr_t, size_t);
85 1.12 uch
86 1.12 uch int mapleopen(dev_t, int, int, struct proc *);
87 1.12 uch int mapleclose(dev_t, int, int, struct proc *);
88 1.12 uch int maple_internal_ioctl(struct maple_softc *, int, int, u_long, caddr_t,
89 1.12 uch int, struct proc *);
90 1.12 uch int mapleioctl(dev_t, u_long, caddr_t, int, struct proc *);
91 1.1 marcus
92 1.1 marcus /*
93 1.1 marcus * Global variables.
94 1.1 marcus */
95 1.1 marcus int maple_polling = 0; /* Are we polling? (Debugger mode) */
96 1.1 marcus
97 1.1 marcus /*
98 1.1 marcus * Driver definition.
99 1.1 marcus */
100 1.1 marcus struct cfattach maple_ca = {
101 1.1 marcus sizeof(struct maple_softc), maplematch, mapleattach
102 1.1 marcus };
103 1.1 marcus
104 1.7 marcus extern struct cfdriver maple_cd;
105 1.7 marcus
106 1.1 marcus static int
107 1.12 uch maplematch(struct device *parent, struct cfdata *cf, void *aux)
108 1.1 marcus {
109 1.1 marcus
110 1.1 marcus if (strcmp("maple", cf->cf_driver->cd_name))
111 1.12 uch return (0);
112 1.1 marcus
113 1.1 marcus return (1);
114 1.1 marcus }
115 1.1 marcus
116 1.1 marcus static void
117 1.12 uch maple_attach_dev(struct maple_softc *sc, int port, int subunit)
118 1.1 marcus {
119 1.1 marcus struct maple_attach_args ma;
120 1.7 marcus u_int32_t func;
121 1.7 marcus int f;
122 1.7 marcus char oldxname[16];
123 1.7 marcus
124 1.1 marcus ma.ma_port = port;
125 1.1 marcus ma.ma_subunit = subunit;
126 1.1 marcus ma.ma_devinfo = &sc->sc_unit[port][subunit].devinfo;
127 1.7 marcus ma.ma_function = 1;
128 1.7 marcus func = ma.ma_devinfo->di_func;
129 1.4 thorpej
130 1.7 marcus mapleprint(&ma, sc->sc_dev.dv_xname);
131 1.7 marcus printf("\n");
132 1.7 marcus strcpy(oldxname, sc->sc_dev.dv_xname);
133 1.7 marcus sprintf(sc->sc_dev.dv_xname, "maple%c", port+'A');
134 1.12 uch if (subunit)
135 1.12 uch sprintf(sc->sc_dev.dv_xname+6, "%d", subunit);
136 1.12 uch
137 1.12 uch for (f = 0; f < 32; f++, ma.ma_function <<= 1)
138 1.12 uch if (func & ma.ma_function)
139 1.12 uch (void)config_found_sm(&sc->sc_dev, &ma,
140 1.12 uch NULL, maplesubmatch);
141 1.7 marcus strcpy(sc->sc_dev.dv_xname, oldxname);
142 1.1 marcus }
143 1.1 marcus
144 1.1 marcus static void
145 1.12 uch maple_begin_txbuf(struct maple_softc *sc)
146 1.1 marcus {
147 1.12 uch
148 1.1 marcus sc->sc_txlink = sc->sc_txpos = sc->sc_txbuf;
149 1.1 marcus }
150 1.1 marcus
151 1.1 marcus static int
152 1.12 uch maple_end_txbuf(struct maple_softc *sc)
153 1.1 marcus {
154 1.1 marcus /* if no frame have been written, we can't mark the
155 1.1 marcus list end, and so the DMA must not be activated */
156 1.1 marcus if (sc->sc_txpos == sc->sc_txbuf)
157 1.12 uch return (0);
158 1.1 marcus
159 1.1 marcus *sc->sc_txlink |= 0x80000000;
160 1.1 marcus
161 1.1 marcus return (1);
162 1.1 marcus }
163 1.1 marcus
164 1.1 marcus static int8_t subunit_code[] = { 0x20, 0x01, 0x02, 0x04, 0x08, 0x10 };
165 1.1 marcus
166 1.1 marcus static void
167 1.12 uch maple_write_command(struct maple_softc *sc, int port, int subunit, int command,
168 1.12 uch int datalen, void *dataaddr)
169 1.1 marcus {
170 1.1 marcus int to, from;
171 1.1 marcus u_int32_t *p = sc->sc_txpos;
172 1.1 marcus
173 1.1 marcus if ((port & ~(MAPLE_PORTS-1)) != 0 ||
174 1.1 marcus subunit < 0 || subunit >= MAPLE_SUBUNITS)
175 1.12 uch return;
176 1.1 marcus
177 1.1 marcus /* Compute sender and recipient address */
178 1.1 marcus from = port << 6;
179 1.1 marcus to = from | subunit_code[subunit];
180 1.1 marcus
181 1.1 marcus /* Max data length = 255 longs = 1020 bytes */
182 1.12 uch if (datalen > 255)
183 1.12 uch datalen = 255;
184 1.12 uch else if (datalen < 0)
185 1.12 uch datalen = 0;
186 1.1 marcus
187 1.1 marcus sc->sc_txlink = p;
188 1.1 marcus
189 1.1 marcus /* Set length of packet and destination port (A-D) */
190 1.1 marcus *p++ = datalen | (port << 16);
191 1.1 marcus
192 1.1 marcus /* Write address to receive buffer where the response
193 1.1 marcus frame should be put */
194 1.1 marcus *p++ = sc->sc_rxbuf_phys[port][subunit];
195 1.1 marcus
196 1.1 marcus /* Create the frame header. The fields are assembled "backwards"
197 1.1 marcus because of the Maple Bus big-endianness. */
198 1.1 marcus *p++ = (command & 0xff) | (to << 8) | (from << 16) | (datalen << 24);
199 1.1 marcus
200 1.1 marcus /* Copy parameter data, if any */
201 1.1 marcus if (datalen > 0) {
202 1.12 uch u_int32_t *param = dataaddr;
203 1.12 uch int i;
204 1.12 uch for (i = 0; i < datalen; i++)
205 1.12 uch *p++ = *param++;
206 1.1 marcus }
207 1.1 marcus
208 1.1 marcus sc->sc_txpos = p;
209 1.1 marcus }
210 1.1 marcus
211 1.1 marcus static void
212 1.12 uch maple_scanbus(struct maple_softc *sc)
213 1.1 marcus {
214 1.5 marcus int p, s;
215 1.1 marcus
216 1.1 marcus maple_polling = 1;
217 1.1 marcus
218 1.1 marcus maple_begin_txbuf(sc);
219 1.1 marcus
220 1.1 marcus for (p = 0; p < MAPLE_PORTS; p++) {
221 1.12 uch maple_write_command(sc, p, 0, MAPLE_COMMAND_DEVINFO, 0, NULL);
222 1.1 marcus }
223 1.1 marcus
224 1.1 marcus if (maple_end_txbuf(sc)) {
225 1.1 marcus
226 1.12 uch MAPLE_DMAADDR = sc->sc_txbuf_phys;
227 1.12 uch MAPLE_STATE = 1;
228 1.12 uch while (MAPLE_STATE != 0)
229 1.12 uch ;
230 1.12 uch
231 1.12 uch for (p = 0; p < MAPLE_PORTS; p++)
232 1.12 uch if ((sc->sc_rxbuf[p][0][0] & 0xff) ==
233 1.12 uch MAPLE_RESPONSE_DEVINFO)
234 1.12 uch sc->sc_port_units[p] =
235 1.12 uch ((sc->sc_rxbuf[p][0][0] >> 15) & 0x3e) | 1;
236 1.12 uch else
237 1.12 uch sc->sc_port_units[p] = 0;
238 1.12 uch
239 1.12 uch
240 1.12 uch maple_begin_txbuf(sc);
241 1.12 uch
242 1.12 uch for (p = 0; p < MAPLE_PORTS; p++) {
243 1.12 uch for (s = 0; s < MAPLE_SUBUNITS; s++) {
244 1.12 uch if (sc->sc_port_units[p] & (1 << s))
245 1.12 uch maple_write_command(sc, p, s,
246 1.12 uch MAPLE_COMMAND_DEVINFO, 0, NULL);
247 1.12 uch }
248 1.12 uch }
249 1.1 marcus
250 1.12 uch if (maple_end_txbuf(sc)) {
251 1.12 uch MAPLE_DMAADDR = sc->sc_txbuf_phys;
252 1.12 uch MAPLE_STATE = 1;
253 1.12 uch while (MAPLE_STATE != 0)
254 1.12 uch ;
255 1.12 uch
256 1.12 uch for (p = 0; p < MAPLE_PORTS; p++)
257 1.12 uch for (s = 0; s < MAPLE_SUBUNITS; s++)
258 1.12 uch if (sc->sc_port_units[p] & (1 << s)) {
259 1.12 uch
260 1.12 uch if ((sc->sc_rxbuf[p][s][0] &
261 1.12 uch 0xff) ==
262 1.12 uch MAPLE_RESPONSE_DEVINFO) {
263 1.1 marcus
264 1.12 uch u_int32_t *to_swap;
265 1.12 uch int i;
266 1.5 marcus
267 1.12 uch memcpy((to_swap =
268 1.12 uch &sc->sc_unit[p][s].
269 1.12 uch devinfo.di_func),
270 1.12 uch sc->sc_rxbuf[p][s] +
271 1.12 uch 1,
272 1.12 uch sizeof(struct
273 1.12 uch maple_devinfo));
274 1.5 marcus
275 1.12 uch for (i = 0; i < 4; i++,
276 1.12 uch to_swap++)
277 1.12 uch *to_swap =
278 1.12 uch ntohl
279 1.12 uch (*to_swap);
280 1.12 uch maple_attach_dev(sc, p,
281 1.12 uch s);
282 1.12 uch } else {
283 1.12 uch
284 1.12 uch printf("%s: no response"
285 1.12 uch " from port %d "
286 1.12 uch " subunit %d\n",
287 1.12 uch sc->sc_dev.dv_xname,
288 1.12 uch p, s);
289 1.12 uch sc->sc_port_units[p] &=
290 1.12 uch ~(1 << s);
291 1.12 uch }
292 1.12 uch }
293 1.12 uch
294 1.5 marcus }
295 1.1 marcus
296 1.1 marcus }
297 1.1 marcus
298 1.1 marcus maple_polling = 0;
299 1.1 marcus
300 1.1 marcus }
301 1.1 marcus
302 1.1 marcus static void
303 1.12 uch maple_send_commands(struct maple_softc *sc)
304 1.1 marcus {
305 1.5 marcus int p, s;
306 1.1 marcus
307 1.1 marcus if (sc->maple_commands_pending || MAPLE_STATE != 0)
308 1.12 uch return;
309 1.1 marcus
310 1.1 marcus maple_begin_txbuf(sc);
311 1.1 marcus
312 1.1 marcus for (p = 0; p < MAPLE_PORTS; p++) {
313 1.12 uch for (s = 0; s < MAPLE_SUBUNITS; s++) {
314 1.12 uch if (sc->sc_unit[p][s].getcond_callback != NULL) {
315 1.12 uch u_int32_t func =
316 1.12 uch ntohl(sc->sc_unit[p][s].getcond_func);
317 1.5 marcus
318 1.12 uch maple_write_command(sc, p, s,
319 1.12 uch MAPLE_COMMAND_GETCOND, 1, &func);
320 1.12 uch }
321 1.12 uch }
322 1.1 marcus }
323 1.1 marcus
324 1.1 marcus if (!maple_end_txbuf(sc))
325 1.12 uch return;
326 1.1 marcus
327 1.1 marcus MAPLE_DMAADDR = sc->sc_txbuf_phys;
328 1.1 marcus MAPLE_STATE = 1;
329 1.1 marcus
330 1.1 marcus sc->maple_commands_pending = 1;
331 1.1 marcus }
332 1.1 marcus
333 1.1 marcus static void
334 1.12 uch maple_check_responses(struct maple_softc *sc)
335 1.1 marcus {
336 1.5 marcus int p, s;
337 1.1 marcus
338 1.1 marcus if (!sc->maple_commands_pending || MAPLE_STATE != 0)
339 1.12 uch return;
340 1.1 marcus
341 1.1 marcus for (p = 0; p < MAPLE_PORTS; p++) {
342 1.12 uch for (s = 0; s < MAPLE_SUBUNITS; s++) {
343 1.12 uch struct maple_unit *u = &sc->sc_unit[p][s];
344 1.12 uch if (u->getcond_callback != NULL &&
345 1.12 uch (sc->sc_rxbuf[p][s][0] & 0xff) ==
346 1.12 uch MAPLE_RESPONSE_DATATRF &&
347 1.12 uch (sc->sc_rxbuf[p][s][0] >> 24) >= 1 &&
348 1.12 uch htonl(sc->sc_rxbuf[p][s][1]) == u->getcond_func) {
349 1.12 uch (*u->getcond_callback)(u->getcond_data,
350 1.12 uch (void *)(sc->sc_rxbuf[p][s] + 2),
351 1.12 uch ((sc->sc_rxbuf[p][s][0] >> 22) & 1020) - 4);
352 1.12 uch }
353 1.12 uch }
354 1.1 marcus }
355 1.1 marcus
356 1.1 marcus sc->maple_commands_pending = 0;
357 1.1 marcus }
358 1.1 marcus
359 1.1 marcus void
360 1.12 uch maple_run_polling(struct device *dev)
361 1.1 marcus {
362 1.1 marcus struct maple_softc *sc;
363 1.1 marcus
364 1.1 marcus sc = (struct maple_softc *)dev;
365 1.1 marcus
366 1.1 marcus if (MAPLE_STATE != 0)
367 1.12 uch return;
368 1.1 marcus
369 1.1 marcus maple_send_commands(sc);
370 1.1 marcus
371 1.1 marcus while (MAPLE_STATE != 0)
372 1.12 uch ;
373 1.1 marcus
374 1.1 marcus maple_check_responses(sc);
375 1.1 marcus }
376 1.1 marcus
377 1.1 marcus void
378 1.12 uch maple_set_condition_callback(struct device *dev, int port, int subunit,
379 1.12 uch u_int32_t func, void (*callback)(void *, void *, int), void *data)
380 1.1 marcus {
381 1.1 marcus struct maple_softc *sc;
382 1.1 marcus
383 1.1 marcus sc = (struct maple_softc *)dev;
384 1.1 marcus
385 1.1 marcus if ((port & ~(MAPLE_PORTS-1)) != 0 ||
386 1.1 marcus subunit < 0 || subunit >= MAPLE_SUBUNITS)
387 1.12 uch return;
388 1.1 marcus
389 1.1 marcus sc->sc_unit[port][subunit].getcond_func = func;
390 1.1 marcus sc->sc_unit[port][subunit].getcond_callback = callback;
391 1.1 marcus sc->sc_unit[port][subunit].getcond_data = data;
392 1.1 marcus }
393 1.1 marcus
394 1.1 marcus static void
395 1.12 uch maple_callout(void *ctx)
396 1.1 marcus {
397 1.1 marcus struct maple_softc *sc = ctx;
398 1.1 marcus
399 1.12 uch if (!maple_polling) {
400 1.12 uch maple_check_responses(sc);
401 1.12 uch maple_send_commands(sc);
402 1.1 marcus }
403 1.1 marcus
404 1.1 marcus callout_reset(&sc->maple_callout_ch, MAPLE_CALLOUT_TICKS,
405 1.12 uch (void *)maple_callout, sc);
406 1.1 marcus }
407 1.1 marcus
408 1.2 marcus int
409 1.12 uch maple_alloc_dma(size_t size, vaddr_t *vap, paddr_t *pap)
410 1.2 marcus {
411 1.2 marcus extern paddr_t avail_start, avail_end; /* from pmap.c */
412 1.2 marcus struct pglist mlist;
413 1.8 chs struct vm_page *m;
414 1.2 marcus int error;
415 1.2 marcus
416 1.2 marcus size = round_page(size);
417 1.2 marcus
418 1.2 marcus TAILQ_INIT(&mlist);
419 1.2 marcus error = uvm_pglistalloc(size, avail_start, avail_end - PAGE_SIZE,
420 1.2 marcus 0, 0, &mlist, 1, 0);
421 1.2 marcus if (error)
422 1.2 marcus return (error);
423 1.2 marcus
424 1.2 marcus m = TAILQ_FIRST(&mlist);
425 1.2 marcus *pap = VM_PAGE_TO_PHYS(m);
426 1.2 marcus *vap = SH3_PHYS_TO_P2SEG(VM_PAGE_TO_PHYS(m));
427 1.2 marcus
428 1.2 marcus return (0);
429 1.2 marcus }
430 1.2 marcus
431 1.2 marcus void
432 1.12 uch maple_free_dma(paddr_t paddr, size_t size)
433 1.2 marcus {
434 1.2 marcus struct pglist mlist;
435 1.8 chs struct vm_page *m;
436 1.2 marcus bus_addr_t addr;
437 1.2 marcus
438 1.2 marcus TAILQ_INIT(&mlist);
439 1.2 marcus for (addr = paddr; addr < paddr + size; addr += PAGE_SIZE) {
440 1.2 marcus m = PHYS_TO_VM_PAGE(addr);
441 1.2 marcus TAILQ_INSERT_TAIL(&mlist, m, pageq);
442 1.2 marcus }
443 1.2 marcus uvm_pglistfree(&mlist);
444 1.2 marcus }
445 1.2 marcus
446 1.1 marcus static void
447 1.12 uch mapleattach(struct device *parent, struct device *self, void *aux)
448 1.1 marcus {
449 1.1 marcus struct maple_softc *sc;
450 1.1 marcus vaddr_t dmabuffer;
451 1.2 marcus paddr_t dmabuffer_phys;
452 1.1 marcus u_int32_t *p;
453 1.1 marcus int i, j;
454 1.1 marcus
455 1.1 marcus sc = (struct maple_softc *)self;
456 1.1 marcus
457 1.1 marcus printf("\n");
458 1.1 marcus
459 1.2 marcus if (maple_alloc_dma(MAPLE_DMABUF_SIZE, &dmabuffer, &dmabuffer_phys)) {
460 1.12 uch printf("%s: unable to allocate DMA buffers.\n",
461 1.12 uch sc->sc_dev.dv_xname);
462 1.12 uch return;
463 1.2 marcus }
464 1.1 marcus
465 1.12 uch p = (u_int32_t *)dmabuffer;
466 1.1 marcus
467 1.1 marcus for (i = 0; i < MAPLE_PORTS; i++)
468 1.12 uch for (j = 0; j < MAPLE_SUBUNITS; j++) {
469 1.12 uch sc->sc_rxbuf[i][j] = p;
470 1.12 uch sc->sc_rxbuf_phys[i][j] = SH3_P2SEG_TO_PHYS(p);
471 1.12 uch p += 256;
472 1.12 uch }
473 1.1 marcus
474 1.1 marcus sc->sc_txbuf = p;
475 1.1 marcus sc->sc_txbuf_phys = SH3_P2SEG_TO_PHYS(p);
476 1.1 marcus
477 1.1 marcus sc->maple_commands_pending = 0;
478 1.1 marcus
479 1.1 marcus MAPLE_RESET = RESET_MAGIC;
480 1.1 marcus MAPLE_RESET2 = 0;
481 1.1 marcus
482 1.1 marcus MAPLE_SPEED = SPEED_2MBPS | TIMEOUT(50000);
483 1.1 marcus
484 1.1 marcus MAPLE_ENABLE = 1;
485 1.1 marcus
486 1.1 marcus maple_scanbus(sc);
487 1.1 marcus
488 1.9 wiz memset(&sc->maple_callout_ch, 0, sizeof(sc->maple_callout_ch));
489 1.1 marcus
490 1.1 marcus callout_reset(&sc->maple_callout_ch, MAPLE_CALLOUT_TICKS,
491 1.12 uch (void *)maple_callout, sc);
492 1.1 marcus }
493 1.1 marcus
494 1.1 marcus int
495 1.12 uch mapleprint(void *aux, const char *pnp)
496 1.1 marcus {
497 1.1 marcus struct maple_attach_args *ma = aux;
498 1.1 marcus
499 1.7 marcus if (pnp != NULL) {
500 1.7 marcus printf("maple%c", 'A'+ma->ma_port);
501 1.7 marcus if (ma->ma_subunit != 0)
502 1.7 marcus printf("%d", ma->ma_subunit);
503 1.7 marcus printf(" at %s", pnp);
504 1.7 marcus }
505 1.1 marcus
506 1.4 thorpej printf(" port %d", ma->ma_port);
507 1.1 marcus
508 1.1 marcus if (ma->ma_subunit != 0)
509 1.4 thorpej printf(" subunit %d", ma->ma_subunit);
510 1.1 marcus
511 1.7 marcus printf(": %.*s",
512 1.12 uch (int)sizeof(ma->ma_devinfo->di_product_name),
513 1.12 uch ma->ma_devinfo->di_product_name);
514 1.7 marcus
515 1.7 marcus return (0);
516 1.1 marcus }
517 1.1 marcus
518 1.4 thorpej static int
519 1.12 uch maplesubmatch(struct device *parent, struct cfdata *match, void *aux)
520 1.1 marcus {
521 1.4 thorpej struct maple_attach_args *ma = aux;
522 1.1 marcus
523 1.4 thorpej if (match->cf_loc[MAPLECF_PORT] != MAPLECF_PORT_DEFAULT &&
524 1.4 thorpej match->cf_loc[MAPLECF_PORT] != ma->ma_port)
525 1.4 thorpej return (0);
526 1.4 thorpej
527 1.4 thorpej if (match->cf_loc[MAPLECF_SUBUNIT] != MAPLECF_SUBUNIT_DEFAULT &&
528 1.4 thorpej match->cf_loc[MAPLECF_SUBUNIT] != ma->ma_subunit)
529 1.4 thorpej return (0);
530 1.4 thorpej
531 1.4 thorpej return ((*match->cf_attach->ca_match)(parent, match, aux));
532 1.1 marcus }
533 1.1 marcus
534 1.1 marcus u_int32_t
535 1.12 uch maple_get_function_data(struct maple_devinfo *devinfo, u_int32_t function_code)
536 1.1 marcus {
537 1.1 marcus int i, p = 0;
538 1.1 marcus
539 1.1 marcus for (i = 31; i >= 0; --i)
540 1.12 uch if (devinfo->di_func & (1U << i)) {
541 1.12 uch if (function_code & (1U << i))
542 1.12 uch return devinfo->di_function_data[p];
543 1.12 uch else
544 1.12 uch if (++p >= 3)
545 1.12 uch break;
546 1.12 uch }
547 1.12 uch
548 1.1 marcus return (0);
549 1.7 marcus }
550 1.7 marcus
551 1.7 marcus /* Generic maple device interface */
552 1.7 marcus
553 1.7 marcus int
554 1.12 uch mapleopen(dev_t dev, int flag, int mode, struct proc *p)
555 1.7 marcus {
556 1.7 marcus struct maple_softc *sc;
557 1.7 marcus
558 1.7 marcus sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev));
559 1.7 marcus if (sc == NULL) /* make sure it was attached */
560 1.7 marcus return (ENXIO);
561 1.7 marcus
562 1.7 marcus if (MAPLEPORT(dev) >= MAPLE_PORTS)
563 1.7 marcus return (ENXIO);
564 1.7 marcus
565 1.7 marcus if (MAPLESUBUNIT(dev) >= MAPLE_SUBUNITS)
566 1.7 marcus return (ENXIO);
567 1.7 marcus
568 1.12 uch if (!(sc->sc_port_units[MAPLEPORT(dev)] & (1 << MAPLESUBUNIT(dev))))
569 1.7 marcus return (ENXIO);
570 1.7 marcus
571 1.12 uch sc->sc_port_units_open[MAPLEPORT(dev)] |= 1 << MAPLESUBUNIT(dev);
572 1.7 marcus
573 1.7 marcus return (0);
574 1.7 marcus }
575 1.7 marcus
576 1.7 marcus int
577 1.12 uch mapleclose(dev_t dev, int flag, int mode, struct proc *p)
578 1.7 marcus {
579 1.7 marcus struct maple_softc *sc;
580 1.7 marcus
581 1.7 marcus sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev));
582 1.7 marcus
583 1.12 uch sc->sc_port_units_open[MAPLEPORT(dev)] &= ~(1 << MAPLESUBUNIT(dev));
584 1.7 marcus
585 1.7 marcus return (0);
586 1.7 marcus }
587 1.7 marcus
588 1.7 marcus int
589 1.12 uch maple_internal_ioctl(struct maple_softc *sc, int port, int subunit, u_long cmd,
590 1.12 uch caddr_t data, int flag, struct proc *p)
591 1.7 marcus {
592 1.7 marcus struct maple_unit *u = &sc->sc_unit[port][subunit];
593 1.7 marcus
594 1.12 uch if (!(sc->sc_port_units[port] & (1 << subunit)))
595 1.7 marcus return (ENXIO);
596 1.7 marcus
597 1.7 marcus switch(cmd) {
598 1.7 marcus case MAPLEIO_GDEVINFO:
599 1.9 wiz memcpy(data, &u->devinfo, sizeof(struct maple_devinfo));
600 1.7 marcus return (0);
601 1.7 marcus default:
602 1.7 marcus return (EINVAL);
603 1.7 marcus }
604 1.7 marcus }
605 1.7 marcus
606 1.7 marcus int
607 1.12 uch mapleioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
608 1.7 marcus {
609 1.7 marcus struct maple_softc *sc;
610 1.7 marcus
611 1.7 marcus sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev));
612 1.7 marcus
613 1.12 uch return (maple_internal_ioctl(sc, MAPLEPORT(dev), MAPLESUBUNIT(dev),
614 1.12 uch cmd, data, flag, p));
615 1.1 marcus }
616