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