Loading...
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 | /*
* Macintosh Nubus Interface Code
*/
#include <linux/config.h>
#include <linux/ptrace.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/string.h>
#include <linux/nubus.h>
#include <linux/errno.h>
#include <asm/setup.h>
#include <asm/system.h>
#include <asm/page.h>
#include <asm/hwtest.h>
/* for LCIII stuff; better find a general way like MACH_HAS_NUBUS */
#include <asm/macintosh.h>
#include <linux/proc_fs.h>
#undef LCIII_WEIRDNESS
static struct nubus_slot nubus_slots[16];
/*
* Please skip to the bottom of this file if you ate lunch recently
* -- Alan
*/
/*
* Yes this sucks. The ROM can appear on arbitary bytes of the long
* word. We are not amused.
*/
extern __inline__ int not_useful(void *p, int map)
{
unsigned long pv=(unsigned long)p;
pv&=3;
if(map&(1<<pv))
return 0;
return 1;
}
static unsigned long nubus_get_rom(unsigned char **ptr, int len, int map)
{
unsigned long v=0;
unsigned char *p=*ptr; /* as v|=*((*ptr)++) upset someone */
while(len)
{
v<<=8;
while(not_useful(p,map))
p++;
v|=*p++;
len--;
}
*ptr=p;
return v;
}
static void nubus_rewind(unsigned char **ptr, int len, int map)
{
unsigned char *p=*ptr;
if(len>65536)
printk("rewind of %d!\n", len);
while(len)
{
do
{
p--;
}
while(not_useful(p,map));
len--;
}
*ptr=p;
}
static void nubus_advance(unsigned char **ptr, int len, int map)
{
unsigned char *p=*ptr;
if(len>65536)
printk("advance of %d!\n", len);
while(len)
{
while(not_useful(p,map))
p++;
p++;
len--;
}
*ptr=p;
}
/*
* 24bit signed offset to 32bit
*/
static unsigned long nubus_expand32(unsigned long foo)
{
if(foo&0x00800000) /* 24bit negative */
foo|=0xFF000000;
return foo;
}
static void nubus_move(unsigned char **ptr, int len, int map)
{
if(len>0)
nubus_advance(ptr,len,map);
else if(len<0)
nubus_rewind(ptr,-len,map);
}
static void *nubus_rom_addr(int slot)
{
/*
* Returns the first byte after the card. We then walk
* backwards to get the lane register and the config
*/
return (void *)(0xF1000000+(slot<<24));
}
void nubus_memcpy(int slot, void *to, unsigned char *p, int len)
{
unsigned char *t=(unsigned char *)to;
while(len)
{
*t++=nubus_get_rom(&p,1, nubus_slots[slot].slot_lanes);
len--;
}
}
void nubus_strncpy(int slot, void *to, unsigned char *p, int len)
{
unsigned char *t=(unsigned char *)to;
while(len)
{
*t=nubus_get_rom(&p,1, nubus_slots[slot].slot_lanes);
if(!*t++)
break;
len--;
}
}
unsigned char *nubus_dirptr(struct nubus_dirent *nd)
{
unsigned char *p=(unsigned char *)(nd->base);
nubus_move(&p, nubus_expand32(nd->value), nd->mask);
return p;
}
struct nubus_dir *nubus_openrootdir(int slot)
{
static struct nubus_dir nbdir;
unsigned char *rp=nubus_rom_addr(slot);
nubus_rewind(&rp,20, nubus_slots[slot].slot_lanes);
nubus_move(&rp, nubus_expand32(nubus_slots[slot].slot_directory),
nubus_slots[slot].slot_lanes);
nbdir.base=rp;
nbdir.length=nubus_slots[slot].slot_dlength;
nbdir.count=0;
nbdir.mask=nubus_slots[slot].slot_lanes;
return &nbdir;
}
struct nubus_dir *nubus_opensubdir(struct nubus_dirent *d)
{
static struct nubus_dir nbdir;
unsigned char *rp=nubus_dirptr(d);
nbdir.base=rp;
nbdir.length=99999;/*slots[i].slot_dlength;*/
nbdir.count=0;
nbdir.mask=d->mask;
return &nbdir;
}
void nubus_closedir(struct nubus_dir *nd)
{
;
}
struct nubus_dirent *nubus_readdir(struct nubus_dir *nd)
{
u32 resid;
u8 rescode;
static struct nubus_dirent d;
if(nd->count==nd->length)
return NULL;
d.base=(unsigned long)nd->base;
resid=nubus_get_rom(&nd->base, 4, nd->mask);
nd->count++;
rescode=resid>>24;
if(rescode==0xFF)
{
nd->count=nd->length;
return NULL;
}
d.type=rescode;
d.value=resid&0xFFFFFF;
d.mask=nd->mask;
return &d;
}
/*
* MAC video handling irritations
*/
static unsigned char nubus_vid_byte[16];
static unsigned long nubus_vid_offset[16];
static void nubus_irqsplat(int slot, void *dev_id, struct pt_regs *regs)
{
unsigned char *p=((unsigned char *)nubus_slot_addr(slot))+
nubus_vid_offset[slot];
*p=nubus_vid_byte[slot];
}
static int nubus_add_irqsplatter(int slot, unsigned long ptr, unsigned char v)
{
nubus_vid_byte[slot]=v;
nubus_vid_offset[slot]=ptr;
nubus_request_irq(slot, NULL, nubus_irqsplat);
return 0;
}
void nubus_video_shutup(int slot, struct nubus_type *nt)
{
if(nt->category!=3 /* Display */ || nt->type!=1 /* Video */
|| nt->DrSW!=1 /* Quickdraw device */)
return;
switch(nt->DrHW)
{
/*
* Toby and MacII Hires cards. These behave in a MacII
* anyway but not on an RBV box
*/
case 0x0001:
case 0x0013:
nubus_add_irqsplatter(slot, 0xA0000, 0);
break;
/*
* Apple workstation video card.
*/
case 0x0006:
nubus_add_irqsplatter(slot, 0xA00000, 0);
break;
/*
* Futura cards
*/
case 0x0417:
case 0x042F:
nubus_add_irqsplatter(slot, 0xF05000, 0x80);
break;
/*
* Fingers crossed 8)
*
* If you have another card and an RBV based mac you'll
* almost certainly have to add it here to make it work.
*/
default:
break;
}
}
/*
* Device list
*/
static struct nubus_device_specifier *nubus_device_list=NULL;
void register_nubus_device(struct nubus_device_specifier *d)
{
d->next=nubus_device_list;
nubus_device_list=d;
}
void unregister_nubus_device(struct nubus_device_specifier *nb)
{
struct nubus_device_specifier **t=&nubus_device_list;
while(*t!=nb && *t)
t=&((*t)->next);
*t=nb->next;
}
static struct nubus_device_specifier *find_nubus_device(int slot, struct nubus_type *nt)
{
struct nubus_device_specifier *t=nubus_device_list;
while(t!=NULL)
{
if(t->setup(t,slot, nt)==0)
return t;
t=t->next;
}
printk("No driver for device [%d %d %d %d]\n",
nt->category, nt->type, nt->DrHW, nt->DrSW);
return NULL;
}
/*
* Probe a nubus slot
*/
void nubus_probe_slot(int slot, int mode)
{
unsigned char *rp;
unsigned char dp;
int lanes;
int i;
unsigned long dpat;
struct nubus_dir *dir;
struct nubus_dirent *nd;
struct nubus_type type_info;
/*
* Ok see whats cooking in the bytelanes
*/
rp=nubus_rom_addr(slot);
for(i=4;i;i--)
{
rp--;
if(!hwreg_present(rp))
continue;
dp=*rp;
if(dp==0)
continue;
/*
* Valid ?
*/
if((((dp>>4)^dp)&0x0F)!=0x0F)
continue;
if((dp&0x0F) >= 1<<i)
continue;
/*
* Looks promising
*/
nubus_slots[slot].slot_flags|=NUBUS_DEVICE_PRESENT;
lanes=dp;
if (mode==0)
printk("nubus%c: ",
"0123456789abcdef"[slot]);
/*
* Time to dig deeper. Find the ROM base
* and read it
*/
rp=nubus_rom_addr(slot);
/*
* Now to make this more fun the ROM is only visible
* on its bytelanes - that is smeared across the address
* space.
*/
nubus_rewind(&rp,20,lanes);
nubus_slots[slot].slot_directory= nubus_get_rom(&rp,4,lanes);
nubus_slots[slot].slot_dlength = nubus_get_rom(&rp,4,lanes);
nubus_slots[slot].slot_crc = nubus_get_rom(&rp,4,lanes);
nubus_slots[slot].slot_rev = nubus_get_rom(&rp,1,lanes);
nubus_slots[slot].slot_format = nubus_get_rom(&rp,1,lanes);
nubus_slots[slot].slot_lanes = lanes;
dpat=nubus_get_rom(&rp,4,lanes);
/*
* Ok now check what we got
*/
if(!(nubus_slots[slot].slot_directory&0x00FF0000))
printk("Dodgy doffset ??\n");
if(dpat!=0x5A932BC7)
printk("Wrong test pattern %lx\n",dpat);
/*
* I wonder how the CRC is meant to work -
* any takers ?
*/
/*
* Now parse the directories on the card
*/
dir=nubus_openrootdir(slot);
/*
* Find the board resource
*/
while((nd=nubus_readdir(dir))!=NULL)
{
/*
* This ought to be 1. 1 doesn't work, 0x80
* does. Seems the Apple docs are wrong.
*/
if(nd->type==0x80/*RES_ID_BOARD_DIR*/)
break;
}
nubus_closedir(dir);
if(nd==NULL)
{
printk("board resource not found!\n");
return;
}
dir=nubus_opensubdir(nd);
/*
* Walk the board resource
*/
while((nd=nubus_readdir(dir))!=NULL)
{
switch(nd->type)
{
case RES_ID_TYPE:
{
unsigned short nbtdata[4];
nubus_memcpy(slot, nbtdata, nubus_dirptr(nd), 8);
type_info.category=nbtdata[0];
type_info.type=nbtdata[1];
type_info.DrHW=nbtdata[2];
type_info.DrSW=nbtdata[3];
break;
}
case RES_ID_NAME:
nubus_strncpy(slot, nubus_slots[slot].slot_cardname,nubus_dirptr(nd),64);
break;
default:
;
}
}
nubus_closedir(dir);
/*
* Attempt to bind a driver to this slot
*/
if (mode==0) {
printk("%s\n",
nubus_slots[slot].slot_cardname);
find_nubus_device(slot,&type_info);
}
if (mode==1)
nubus_video_shutup(slot, &type_info);
return;
}
}
void nubus_probe_bus(void)
{
int i;
for(i=9;i<15;i++)
{
/* printk("nubus: probing slot %d !\n", i); */
nubus_probe_slot(i, 0);
}
}
/*
* RBV machines have level triggered video interrupts, and a VIA
* emulation that doesn't always seem to include being able to disable
* an interrupt. Totally lusing hardware. Before we can init irq's we
* have to install a handler to shut the bloody things up.
*/
void nubus_sweep_video(void)
{
int i;
return; /* XXX why ?? */
for(i=9;i<15;i++)
{
nubus_probe_slot(i,1);
}
}
/*
* Support functions
*/
int nubus_ethernet_addr(int slot, unsigned char *addr)
{
struct nubus_dir *nb;
struct nubus_dirent *d;
int ng=-ENOENT;
nb=nubus_openrootdir(slot);
if(nb==NULL)
return -ENOENT;
while((d=nubus_readdir(nb))!=NULL)
{
if(d->type==0x80) /* First private resource */
break;
}
if(d==NULL)
return -ENOENT;
nb=nubus_opensubdir(d);
while((d=nubus_readdir(nb))!=NULL)
{
if(d->type==0x80) /* First private field is the mac */
{
int i;
nubus_memcpy(slot, addr, nubus_dirptr(d), 6);
/* printk("d.base=%lX, d.value=%lX\n",
d->base,d->value);
memcpy(addr,"\xC0\xC1\xC2\xC3\xC4\xC5",6);*/
printk("MAC address: ");
for(i=0;i<6;i++)
{
printk("%s%02X", i?":":"", addr[i]);
}
ng=0;
break;
}
else
printk("ID=%d val=%x\n",
d->type, d->value);
}
return ng;
}
#ifdef CONFIG_PROC_FS
/*
* /proc for Nubus devices
*/
static int sprint_nubus_config(int slot, char *ptr, int len)
{
if(len<150)
return -1;
sprintf(ptr, "Device: %s %s\n", nubus_slots[slot].slot_cardname,
(nubus_slots[slot].slot_flags&NUBUS_DEVICE_ACTIVE)?
"[active]":"[unused]");
return strlen(ptr);
}
int get_nubus_list(char *buf)
{
int nprinted, len, size;
int slot;
#define MSG "\nwarning: page-size limit reached!\n"
/* reserve same for truncation warning message: */
size = PAGE_SIZE - (strlen(MSG) + 1);
len = sprintf(buf, "Nubus devices found:\n");
for (slot=0; slot< 16; slot++)
{
if(!(nubus_slots[slot].slot_flags&NUBUS_DEVICE_PRESENT))
continue;
nprinted = sprint_nubus_config(slot, buf + len, size - len);
if (nprinted < 0) {
return len + sprintf(buf + len, MSG);
}
len += nprinted;
}
return len;
}
static struct proc_dir_entry proc_nubus = {
PROC_NUBUS, 5, "nubus",
S_IFREG | S_IRUGO, 1, 0, 0,
0, &proc_array_inode_operations
};
#endif
void nubus_init(void)
{
/*
* Register cards
*/
#ifdef CONFIG_DAYNAPORT
extern struct nubus_device_specifier nubus_8390;
#endif
if (!MACH_IS_MAC)
return;
#ifdef LCIII_WEIRDNESS
if (macintosh_config->ident == MAC_MODEL_LCIII) {
printk("nubus init: LCIII has no nubus!\n");
return;
}
#endif
#ifdef CONFIG_DAYNAPORT
register_nubus_device(&nubus_8390);
#endif
/*
* And probe
*/
nubus_init_via();
printk("Scanning nubus slots.\n");
nubus_probe_bus();
#ifdef CONFIG_PROC_FS
proc_register(&proc_root, &proc_nubus);
#endif
}
|