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
|
/* $NetBSD: ofw_autoconf.c,v 1.25 2022/12/14 13:19:04 macallan Exp $ */
/*
* Copyright (C) 1995, 1996 Wolfgang Solfrank.
* Copyright (C) 1995, 1996 TooLs GmbH.
* 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 TooLs GmbH.
* 4. The name of TooLs GmbH may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``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 TOOLS GMBH 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/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ofw_autoconf.c,v 1.25 2022/12/14 13:19:04 macallan Exp $");
#ifdef ofppc
#include "gtpci.h"
#endif
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/reboot.h>
#include <sys/systm.h>
#include <uvm/uvm_extern.h>
#include <machine/autoconf.h>
#include <sys/bus.h>
#include <dev/ofw/openfirm.h>
#include <dev/marvell/marvellvar.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#if NGTPCI > 0
#include <dev/marvell/gtpcivar.h>
#endif
#include <dev/scsipi/scsi_all.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsiconf.h>
#include <dev/ata/atavar.h>
#include <dev/ic/wdcvar.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsdisplayvar.h>
#include <dev/rasops/rasops.h>
#include <powerpc/oea/ofw_rasconsvar.h>
#include <machine/pci_machdep.h>
#include <prop/proplib.h>
extern char bootpath[256];
char cbootpath[256];
static int boot_node = 0; /* points at boot device if we netboot */
static void canonicalize_bootpath(void);
/*
* Determine device configuration for a machine.
*/
void
cpu_configure(void)
{
rascons_add_rom_font();
init_interrupt();
canonicalize_bootpath();
if (config_rootfound("mainbus", NULL) == NULL)
panic("configure: mainbus not configured");
genppc_cpu_configure();
}
static void
canonicalize_bootpath(void)
{
int node, len;
char *p, *lastp;
char last[32], type[32];
/*
* If the bootpath doesn't start with a / then it isn't
* an OFW path and probably is an alias, so look up the alias
* and regenerate the full bootpath so device_register will work.
*/
if (bootpath[0] != '/' && bootpath[0] != '\0') {
int aliases = OF_finddevice("/aliases");
char tmpbuf[100];
char aliasbuf[256];
if (aliases != 0) {
char *cp1, *cp2, *cp;
char saved_ch = '\0';
cp1 = strchr(bootpath, ':');
cp2 = strchr(bootpath, ',');
cp = cp1;
if (cp1 == NULL || (cp2 != NULL && cp2 < cp1))
cp = cp2;
tmpbuf[0] = '\0';
if (cp != NULL) {
strcpy(tmpbuf, cp);
saved_ch = *cp;
*cp = '\0';
}
len = OF_getprop(aliases, bootpath, aliasbuf,
sizeof(aliasbuf));
if (len > 0) {
if (aliasbuf[len-1] == '\0')
len--;
memcpy(bootpath, aliasbuf, len);
strcpy(&bootpath[len], tmpbuf);
} else {
*cp = saved_ch;
}
}
}
/*
* Strip kernel name. bootpath contains "OF-path"/"kernel".
*
* for example:
* /bandit@F2000000/gc@10/53c94@10000/sd@0,0/netbsd (OF-1.x)
* /pci/mac-io/ata-3@2000/disk@0:0/netbsd.new (OF-3.x)
*/
strcpy(cbootpath, bootpath);
if ((node = OF_finddevice("/options")) == -1 ||
OF_getprop(node, "qemu_boot_hack", type, sizeof(type) - 1) == -1 ||
type[0] != 'y') {
while ((node = OF_finddevice(cbootpath)) == -1) {
if ((p = strrchr(cbootpath, '/')) == NULL)
break;
*p = '\0';
}
} else {
node = -1;
}
printf("bootpath: %s\n", bootpath);
if (node == -1) {
/* Cannot canonicalize... use bootpath anyway. */
strcpy(cbootpath, bootpath);
return;
}
/* see if we netbooted */
len = OF_getprop(node, "device_type", type, sizeof(type) - 1);
if (len > -1) {
type[len] = 0;
if (strcmp(type, "network") == 0) {
boot_node = node;
}
}
/*
* cbootpath is a valid OF path. Use package-to-path to
* canonicalize pathname.
*/
/* Back up the last component for later use. */
if ((p = strrchr(cbootpath, '/')) != NULL)
strcpy(last, p + 1);
else
last[0] = '\0';
memset(cbootpath, 0, sizeof(cbootpath));
OF_package_to_path(node, cbootpath, sizeof(cbootpath) - 1);
/*
* OF_1.x (at least) always returns addr == 0 for
* SCSI disks (i.e. "/bandit@.../.../sd@0,0").
* also check for .../disk@ which some Adaptec firmware uses
*/
lastp = strrchr(cbootpath, '/');
if (lastp != NULL) {
lastp++;
if ((strncmp(lastp, "sd@", 3) == 0
&& strncmp(last, "sd@", 3) == 0) ||
(strncmp(lastp, "disk@", 5) == 0
&& strncmp(last, "disk@", 5) == 0))
strcpy(lastp, last);
} else {
lastp = cbootpath;
}
/*
* At this point, cbootpath contains like:
* "/pci@80000000/mac-io@10/ata-3@20000/disk"
*
* The last component may have no address... so append it.
*/
if (strchr(lastp, '@') == NULL) {
/* Append it. */
if ((p = strrchr(last, '@')) != NULL)
strcat(cbootpath, p);
}
if ((p = strrchr(lastp, ':')) != NULL) {
*p++ = '\0';
/* booted_partition = *p - '0'; XXX correct? */
}
}
/*
* device_register is called from config_attach as each device is
* attached. We use it to find the NetBSD device corresponding to the
* known OF boot device.
*/
void
device_register(device_t dev, void *aux)
{
static device_t parent;
static char *bp = bootpath + 1, *cp = cbootpath;
unsigned long addr, addr2;
char *p;
#if NGTPCI > 0
struct powerpc_bus_space *gtpci_mem_bs_tag = NULL;
#endif
/* Skip over devices not represented in the OF tree. */
if (device_is_a(dev, "mainbus")) {
parent = dev;
return;
}
/* skip over CPUs */
if (device_is_a(dev, "cpu")) {
return;
}
if (device_is_a(dev, "valkyriefb")) {
struct confargs *ca = aux;
prop_dictionary_t dict;
dict = device_properties(dev);
copy_disp_props(dev, ca->ca_node, dict);
}
/* cannot read useful display properties for platinum */
if (device_is_a(dev, "platinumfb")) {
return;
}
#if NGTPCI > 0
if (device_is_a(dev, "gtpci")) {
extern struct gtpci_prot gtpci0_prot, gtpci1_prot;
extern struct powerpc_bus_space
gtpci0_io_bs_tag, gtpci0_mem_bs_tag,
gtpci1_io_bs_tag, gtpci1_mem_bs_tag;
extern struct genppc_pci_chipset
genppc_gtpci0_chipset, genppc_gtpci1_chipset;
struct marvell_attach_args *mva = aux;
struct gtpci_prot *gtpci_prot;
struct powerpc_bus_space *gtpci_io_bs_tag;
struct genppc_pci_chipset *genppc_gtpci_chipset;
prop_dictionary_t dict = device_properties(dev);
prop_data_t prot, io_bs_tag, mem_bs_tag, pc;
int iostart, ioend;
if (mva->mva_unit == 0) {
gtpci_prot = >pci0_prot;
gtpci_io_bs_tag = >pci0_io_bs_tag;
gtpci_mem_bs_tag = >pci0_mem_bs_tag;
genppc_gtpci_chipset = &genppc_gtpci0_chipset;
iostart = 0;
ioend = 0;
} else {
gtpci_prot = >pci1_prot;
gtpci_io_bs_tag = >pci1_io_bs_tag;
gtpci_mem_bs_tag = >pci1_mem_bs_tag;
genppc_gtpci_chipset = &genppc_gtpci1_chipset;
iostart = 0x1400;
ioend = 0xffff;
}
prot = prop_data_create_data_nocopy(
gtpci_prot, sizeof(struct gtpci_prot));
KASSERT(prot != NULL);
prop_dictionary_set(dict, "prot", prot);
prop_object_release(prot);
io_bs_tag = prop_data_create_data_nocopy(
gtpci_io_bs_tag, sizeof(struct powerpc_bus_space));
KASSERT(io_bs_tag != NULL);
prop_dictionary_set(dict, "io-bus-tag", io_bs_tag);
prop_object_release(io_bs_tag);
mem_bs_tag = prop_data_create_data_nocopy(
gtpci_mem_bs_tag, sizeof(struct powerpc_bus_space));
KASSERT(mem_bs_tag != NULL);
prop_dictionary_set(dict, "mem-bus-tag", mem_bs_tag);
prop_object_release(mem_bs_tag);
genppc_gtpci_chipset->pc_conf_v = device_private(dev);
pc = prop_data_create_data_nocopy(genppc_gtpci_chipset,
sizeof(struct genppc_pci_chipset));
KASSERT(pc != NULL);
prop_dictionary_set(dict, "pci-chipset", pc);
prop_object_release(pc);
prop_dictionary_set_uint64(dict, "iostart", iostart);
prop_dictionary_set_uint64(dict, "ioend", ioend);
prop_dictionary_set_uint64(dict, "memstart",
gtpci_mem_bs_tag->pbs_base);
prop_dictionary_set_uint64(dict, "memend",
gtpci_mem_bs_tag->pbs_limit - 1);
prop_dictionary_set_uint32(dict, "cache-line-size",
CACHELINESIZE);
}
#endif
if (device_is_a(dev, "atapibus") ||
#ifndef PMAC_G5
device_is_a(dev, "pci") ||
#endif
device_is_a(dev, "scsibus") || device_is_a(dev, "atabus"))
return;
if (device_is_a(device_parent(dev), "pci")) {
struct pci_attach_args *pa = aux;
prop_dictionary_t dict;
prop_bool_t b;
int node;
char name[32];
dict = device_properties(dev);
node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
/* enable configuration of irq 14/15 for VIA native IDE */
if (device_is_a(dev, "viaide") &&
strncmp(model_name, "Pegasos", 7) == 0) {
b = prop_bool_create(true);
KASSERT(b != NULL);
(void)prop_dictionary_set(dict,
"use-compat-native-irq", b);
prop_object_release(b);
}
if (node != 0) {
int pci_class = 0;
prop_dictionary_set_uint32(dict, "device_node", node);
if (node == boot_node) {
/* we netbooted from whatever this is */
booted_device = dev;
}
/* see if this is going to be console */
memset(name, 0, sizeof(name));
OF_getprop(node, "device_type", name, sizeof(name));
OF_getprop(node, "class-code", &pci_class,
sizeof pci_class);
pci_class = (pci_class >> 16) & 0xff;
if (strcmp(name, "display") == 0 ||
strcmp(name, "ATY,DDParent") == 0 ||
pci_class == PCI_CLASS_DISPLAY) {
/* setup display properties for fb driver */
prop_dictionary_set_bool(dict, "is_console", 0);
copy_disp_props(dev, node, dict);
}
if (pci_class == PCI_CLASS_NETWORK) {
of_to_dataprop(dict, node, "local-mac-address",
"mac-address");
of_to_dataprop(dict, node, "shared-pins",
"shared-pins");
}
}
#ifdef macppc
/*
* XXX
* some macppc boxes have onboard devices where parts or all of
* the PCI_INTERRUPT register are hardwired to 0
*/
if (pa->pa_intrpin == 0)
pa->pa_intrpin = 1;
#endif
}
if (booted_device)
return;
/*
* Skip over devices that are really just layers of NetBSD
* autoconf(9) we should just skip as they do not have any
* OFW devices.
* XXX except on G5, where we have /ht/pci* instead of /pci*
*/
if (device_is_a(device_parent(dev), "atapibus") ||
device_is_a(device_parent(dev), "atabus") ||
#ifndef PMAC_G5
device_is_a(device_parent(dev), "pci") ||
#endif
device_is_a(device_parent(dev), "scsibus")) {
if (device_parent(device_parent(dev)) != parent) {
return;
}
} else {
if (device_parent(dev) != parent)
return;
}
/*
* Get the address part of the current path component. The
* last component of the canonical bootpath may have no
* address (eg, "disk"), in which case we need to get the
* address from the original bootpath instead.
*/
p = strchr(cp, '@');
if (!p) {
if (bp)
p = strchr(bp, '@');
if (!p)
addr = 0;
else
addr = strtoul(p + 1, &p, 16);
} else
addr = strtoul(p + 1, &p, 16);
/* if the current path has more address, grab that too */
if (p && *p == ',')
addr2 = strtoul(p + 1, &p, 16);
else
addr2 = 0;
if (device_is_a(device_parent(dev), "mainbus")) {
struct confargs *ca = aux;
if (strcmp(ca->ca_name, "ofw") == 0) /* XXX */
return;
if (strcmp(ca->ca_name, "gt") == 0)
parent = dev;
if (addr != ca->ca_reg[0])
return;
if (addr2 != 0 && addr2 != ca->ca_reg[1])
return;
} else if (device_is_a(device_parent(dev), "gt")) {
/*
* Special handle for MV64361 on PegasosII(ofppc).
*/
if (device_is_a(dev, "mvgbec")) {
/*
* Fix cp to /port@N from /ethernet/portN. (N is 0...2)
*/
static char fix_cp[8] = "/port@N";
if (strlen(cp) != 15 ||
strncmp(cp, "/ethernet/port", 14) != 0)
return;
fix_cp[7] = *(cp + 15);
p = fix_cp;
#if NGTPCI > 0
} else if (device_is_a(dev, "gtpci")) {
if (gtpci_mem_bs_tag != NULL &&
addr != gtpci_mem_bs_tag->pbs_base)
return;
#endif
} else
return;
} else if (device_is_a(device_parent(dev), "pci")) {
struct pci_attach_args *pa = aux;
if (addr != pa->pa_device ||
addr2 != pa->pa_function)
return;
} else if (device_is_a(device_parent(dev), "obio")) {
struct confargs *ca = aux;
if (addr != ca->ca_reg[0])
return;
} else if (device_is_a(device_parent(dev), "scsibus") ||
device_is_a(device_parent(dev), "atapibus")) {
struct scsipibus_attach_args *sa = aux;
/* periph_target is target for scsi, drive # for atapi */
if (addr != sa->sa_periph->periph_target)
return;
} else if (device_is_a(device_parent(device_parent(dev)), "pciide") ||
device_is_a(device_parent(device_parent(dev)), "viaide") ||
device_is_a(device_parent(device_parent(dev)), "slide")) {
struct ata_device *adev = aux;
if (addr != adev->adev_channel ||
addr2 != adev->adev_drv_data->drive)
return;
} else if (device_is_a(device_parent(device_parent(dev)), "wdc")) {
struct ata_device *adev = aux;
if (addr != adev->adev_drv_data->drive)
return;
} else if (device_is_a(dev, "pci")) {
if (addr != device_unit(dev))
return;
} else if (device_is_a(device_parent(dev), "atabus")) {
/*
* XXX
* on svwsata this is the channel number and we ignore the
* drive number which is always 0 anyway
* needs to be revisited for other (S)ATA cards
*/
struct ata_device *adev = aux;
if (addr != adev->adev_channel)
return;
/* we have our match, cut off the rest */
if (p) *p = 0;
} else
return;
/* If we reach this point, then dev is a match for the current
* path component.
*/
if (p && *p) {
parent = dev;
cp = p;
bp = strchr(bp, '/');
if (bp)
bp++;
return;
} else {
booted_device = dev;
booted_partition = 0; /* XXX -- should be extracted from bootpath */
return;
}
}
/*
* Setup root device.
* Configure swap area.
*/
void
cpu_rootconf(void)
{
printf("boot device: %s\n",
booted_device ? device_xname(booted_device) : "<unknown>");
rootconf();
}
/*
* Find OF-device corresponding to the PCI device.
*/
int
pcidev_to_ofdev(pci_chipset_tag_t pc, pcitag_t tag)
{
int bus, dev, func;
u_int reg[5];
int p, q;
int l, b, d, f;
pci_decompose_tag(pc, tag, &bus, &dev, &func);
for (q = OF_peer(0); q; q = p) {
l = OF_getprop(q, "assigned-addresses", reg, sizeof(reg));
if (l > 4) {
b = (reg[0] >> 16) & 0xff;
d = (reg[0] >> 11) & 0x1f;
f = (reg[0] >> 8) & 0x07;
if (b == bus && d == dev && f == func)
return q;
}
if ((p = OF_child(q)))
continue;
while (q) {
if ((p = OF_peer(q)))
break;
q = OF_parent(q);
}
}
return 0;
}
|