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
|
/* $NetBSD: ebus.c,v 1.42 2022/01/22 11:49:16 thorpej Exp $ */
/*
* Copyright (c) 1999, 2000 Matthew R. Green
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
*/
/*
* EBus support for PCI based SPARC systems (ms-IIep, Ultra).
* EBus is documented in PCIO manual (Sun Part#: 802-7837-01).
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ebus.c,v 1.42 2022/01/22 11:49:16 thorpej Exp $");
#if defined(DEBUG) && !defined(EBUS_DEBUG)
#define EBUS_DEBUG
#endif
#ifdef EBUS_DEBUG
#define EDB_PROM 0x01
#define EDB_CHILD 0x02
#define EDB_INTRMAP 0x04
#define EDB_BUSMAP 0x08
#define EDB_BUSDMA 0x10
#define EDB_INTR 0x20
int ebus_debug = 0;
#define DPRINTF(l, s) do { if (ebus_debug & l) printf s; } while (0)
#else
#define DPRINTF(l, s)
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/errno.h>
#include <sys/malloc.h>
#include <sys/kmem.h>
#include <sys/callout.h>
#include <sys/kernel.h>
#define _SPARC_BUS_DMA_PRIVATE
#include <sys/bus.h>
#include <machine/autoconf.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcidevs.h>
#include <dev/ofw/ofw_pci.h>
#include <dev/ebus/ebusreg.h>
#include <dev/ebus/ebusvar.h>
#include "opt_blink.h"
volatile uint32_t *ebus_LED = NULL;
#ifdef BLINK
static callout_t ebus_blink_ch;
static void ebus_blink(void *);
#endif
struct ebus_softc {
device_t sc_dev;
device_t sc_parent; /* PCI bus */
int sc_node; /* PROM node */
bus_space_tag_t sc_bustag; /* mem tag from pci */
/*
* "reg" contains exactly the info we'd get by processing
* "ranges", so don't bother with "ranges" and use "reg" directly.
*/
struct ofw_pci_register *sc_reg;
int sc_nreg;
};
static int ebus_match(device_t, cfdata_t, void *);
static void ebus_attach(device_t, device_t, void *);
CFATTACH_DECL_NEW(ebus, sizeof(struct ebus_softc),
ebus_match, ebus_attach, NULL, NULL);
static int ebus_setup_attach_args(struct ebus_softc *, bus_space_tag_t,
bus_dma_tag_t, int, struct ebus_attach_args *);
static void ebus_destroy_attach_args(struct ebus_attach_args *);
static int ebus_print(void *, const char *);
/*
* here are our bus space and bus DMA routines.
*/
static paddr_t ebus_bus_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
static int _ebus_bus_map(bus_space_tag_t, bus_addr_t, bus_size_t, int,
vaddr_t, bus_space_handle_t *);
static void *ebus_intr_establish(bus_space_tag_t, int, int,
int (*)(void *), void *, void (*)(void));
static bus_dma_tag_t ebus_alloc_dma_tag(struct ebus_softc *, bus_dma_tag_t);
/*
* Working around PROM bogosity.
*
* EBus doesn't have official OFW binding. sparc64 has a de-facto
* standard but patching it in in prompatch.c and then decoding it
* here would be an overkill for ms-IIep.
*
* So we assume that all ms-IIep based systems use PCIO chip only in
* "motherboard mode" with interrupt lines wired directly to ms-IIep
* interrupt inputs.
*
* Note that this is ineligible for prompatch.c, as we are not
* correcting PROM to conform to some established standard, this hack
* is tied to this version of ebus driver and as such it's better stay
* private to the driver.
*/
struct msiiep_ebus_intr_wiring {
const char *name; /* PROM node */
int line; /* ms-IIep interrupt input */
};
static const struct msiiep_ebus_intr_wiring krups_ebus_intr_wiring[] = {
{ "su", 0 }, { "8042", 0 }, { "sound", 3 }
};
static const struct msiiep_ebus_intr_wiring espresso_ebus_intr_wiring[] = {
{ "su", 0 }, { "8042", 0 }, { "sound", 3 }, { "parallel", 4 }
};
struct msiiep_known_ebus_wiring {
const char *model;
const struct msiiep_ebus_intr_wiring *map;
int mapsize;
};
#define MSIIEP_MODEL_WIRING(name, map) \
{ name, map, sizeof(map)/sizeof(map[0]) }
static const struct msiiep_known_ebus_wiring known_models[] = {
MSIIEP_MODEL_WIRING("SUNW,501-4267", krups_ebus_intr_wiring),
MSIIEP_MODEL_WIRING("SUNW,375-0059", espresso_ebus_intr_wiring),
{ NULL, NULL, 0}
};
/*
* XXX: This assumes single EBus. However I don't think any ms-IIep
* system ever used more than one. In any case, without looking at a
* system with multiple PCIO chips I don't know how to correctly
* program the driver to handle PROM glitches in them, so for the time
* being just use globals.
*/
static const struct msiiep_ebus_intr_wiring *wiring_map;
static int wiring_map_size;
static int ebus_init_wiring_table(struct ebus_softc *);
static int
ebus_match(device_t parent, cfdata_t cf, void *aux)
{
struct pci_attach_args *pa = aux;
char name[10];
int node;
/* Only attach if there's a PROM node. */
node = PCITAG_NODE(pa->pa_tag);
if (node == -1)
return (0);
prom_getpropstringA(node, "name", name, sizeof name);
if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE
&& PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN
&& PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUS
&& strcmp(name, "ebus") == 0)
return (1);
return (0);
}
static int
ebus_init_wiring_table(struct ebus_softc *sc)
{
const struct msiiep_known_ebus_wiring *p;
char buf[32];
char *model;
if (wiring_map != NULL) {
printf("%s: global ebus wiring map already initialized\n",
device_xname(sc->sc_dev));
return (0);
}
model = prom_getpropstringA(prom_findroot(), "model",
buf, sizeof(buf));
if (model == NULL)
panic("ebus_init_wiring_table: no \"model\" property");
for (p = known_models; p->model != NULL; ++p)
if (strcmp(model, p->model) == 0) {
wiring_map = p->map;
wiring_map_size = p->mapsize;
return (1);
}
/* not found? we should have failed in pci_attach_hook then. */
panic("ebus_init_wiring_table: unknown model %s", model);
}
/*
* attach an ebus and all its children. this code is modeled
* after the sbus code which does similar things.
*/
static void
ebus_attach(device_t parent, device_t self, void *aux)
{
struct ebus_softc *sc = device_private(self);
struct pci_attach_args *pa = aux;
struct ebus_attach_args ea;
bus_space_tag_t sbt;
bus_dma_tag_t dmatag;
bus_space_handle_t hLED;
pcireg_t base14;
int node, error;
char devinfo[256];
sc->sc_dev = self;
#ifdef BLINK
callout_init(&ebus_blink_ch, 0);
#endif
pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
printf(": %s, revision 0x%02x\n",
devinfo, PCI_REVISION(pa->pa_class));
node = PCITAG_NODE(pa->pa_tag);
if (node == -1)
panic("%s: unable to find ebus node", device_xname(self));
if (ebus_init_wiring_table(sc) == 0)
return;
/* map the LED register */
base14 = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x14);
if (bus_space_map(pa->pa_memt, base14 + 0x726000, 4, 0, &hLED) == 0) {
ebus_LED = bus_space_vaddr(pa->pa_memt, hLED);
#ifdef BLINK
ebus_blink((void *)0);
#endif
} else {
printf("unable to map the LED register\n");
}
sc->sc_node = node;
sc->sc_parent = parent; /* XXX: unused so far */
sc->sc_bustag = pa->pa_memt; /* EBus only does PCI MEM32 space */
if ((sbt = bus_space_tag_alloc(sc->sc_bustag, sc)) == NULL)
panic("unable to allocate ebus bus tag");
sbt->sparc_bus_map = _ebus_bus_map;
sbt->sparc_bus_mmap = ebus_bus_mmap;
sbt->sparc_intr_establish = ebus_intr_establish;
dmatag = ebus_alloc_dma_tag(sc, pa->pa_dmat);
/*
* Setup ranges. The interesting thing is that we use "reg"
* not "ranges", since "reg" on ebus has exactly the data we'd
* get by processing "ranges".
*/
error = prom_getprop(node, "reg", sizeof(struct ofw_pci_register),
&sc->sc_nreg, &sc->sc_reg);
if (error)
panic("%s: unable to read ebus registers (error %d)",
device_xname(self), error);
/*
* now attach all our children
*/
DPRINTF(EDB_CHILD, ("ebus node %08x, searching children...\n", node));
devhandle_t selfh = device_handle(self);
for (node = firstchild(node); node; node = nextsibling(node)) {
char *name = prom_getpropstring(node, "name");
if (ebus_setup_attach_args(sc, sbt, dmatag, node, &ea) != 0) {
printf("ebus_attach: %s: incomplete\n", name);
continue;
}
DPRINTF(EDB_CHILD,
("- found child `%s', attaching\n", ea.ea_name));
(void)config_found(self, &ea, ebus_print,
CFARGS(.devhandle = prom_node_to_devhandle(selfh, node)));
ebus_destroy_attach_args(&ea);
}
}
static int
ebus_setup_attach_args(struct ebus_softc *sc,
bus_space_tag_t bustag, bus_dma_tag_t dmatag, int node,
struct ebus_attach_args *ea)
{
int n, err;
memset(ea, 0, sizeof(struct ebus_attach_args));
err = prom_getprop(node, "name", 1, &n, &ea->ea_name);
if (err != 0)
return (err);
KASSERT(ea->ea_name[n-1] == '\0');
ea->ea_node = node;
ea->ea_bustag = bustag;
ea->ea_dmatag = dmatag;
err = prom_getprop(node, "reg", sizeof(struct ebus_regs),
&ea->ea_nreg, &ea->ea_reg);
if (err != 0)
return (err);
/*
* On Ultra the bar is the _offset_ of the BAR in PCI config
* space but in (some?) ms-IIep systems (e.g. Krups) it's the
* _number_ of the BAR - e.g. BAR1 is represented by 1 in
* Krups PROM, while on Ultra it's 0x14. Fix it here.
*/
for (n = 0; n < ea->ea_nreg; ++n)
if (ea->ea_reg[n].hi < PCI_MAPREG_START) {
ea->ea_reg[n].hi = PCI_MAPREG_START
+ ea->ea_reg[n].hi * sizeof(pcireg_t);
}
err = prom_getprop(node, "address", sizeof(uint32_t),
&ea->ea_nvaddr, &ea->ea_vaddr);
if (err != ENOENT) {
if (err != 0)
return (err);
if (ea->ea_nreg != ea->ea_nvaddr)
printf("ebus loses: device %s: %d regs and %d addrs\n",
ea->ea_name, ea->ea_nreg, ea->ea_nvaddr);
} else
ea->ea_nvaddr = 0;
/* XXX: "interrupts" hack */
for (n = 0; n < wiring_map_size; ++n) {
const struct msiiep_ebus_intr_wiring *w = &wiring_map[n];
if (strcmp(w->name, ea->ea_name) == 0) {
ea->ea_intr = malloc(sizeof(uint32_t),
M_DEVBUF, M_WAITOK);
ea->ea_intr[0] = w->line;
ea->ea_nintr = 1;
break;
}
}
return (0);
}
static void
ebus_destroy_attach_args(struct ebus_attach_args *ea)
{
if (ea->ea_name)
free((void *)ea->ea_name, M_DEVBUF);
if (ea->ea_reg)
free((void *)ea->ea_reg, M_DEVBUF);
if (ea->ea_intr)
free((void *)ea->ea_intr, M_DEVBUF);
if (ea->ea_vaddr)
free((void *)ea->ea_vaddr, M_DEVBUF);
}
static int
ebus_print(void *aux, const char *p)
{
struct ebus_attach_args *ea = aux;
int i;
if (p)
aprint_normal("%s at %s", ea->ea_name, p);
for (i = 0; i < ea->ea_nreg; ++i)
aprint_normal("%s bar %x offset 0x%x", i == 0 ? "" : ",",
ea->ea_reg[i].hi, ea->ea_reg[i].lo);
for (i = 0; i < ea->ea_nintr; ++i)
aprint_normal(" line %d", ea->ea_intr[i]);
return (UNCONF);
}
/*
* bus space and bus DMA methods below here
*/
static bus_dma_tag_t
ebus_alloc_dma_tag(struct ebus_softc *sc, bus_dma_tag_t pdt)
{
bus_dma_tag_t dt;
dt = kmem_zalloc(sizeof(*dt), KM_SLEEP);
dt->_cookie = sc;
#define PCOPY(x) dt->x = pdt->x
PCOPY(_dmamap_create);
PCOPY(_dmamap_destroy);
PCOPY(_dmamap_load);
PCOPY(_dmamap_load_mbuf);
PCOPY(_dmamap_load_uio);
PCOPY(_dmamap_load_raw);
PCOPY(_dmamap_unload);
PCOPY(_dmamap_sync);
PCOPY(_dmamem_alloc);
PCOPY(_dmamem_free);
PCOPY(_dmamem_map);
PCOPY(_dmamem_unmap);
PCOPY(_dmamem_mmap);
#undef PCOPY
return (dt);
}
/*
* bus space support. <sparc64/dev/psychoreg.h> has a discussion
* about PCI physical addresses, which also applies to ebus.
*/
static int
_ebus_bus_map(bus_space_tag_t t, bus_addr_t ba, bus_size_t size, int flags,
vaddr_t va, bus_space_handle_t *hp)
{
struct ebus_softc *sc = t->cookie;
u_int bar;
paddr_t offset;
int i;
bar = BUS_ADDR_IOSPACE(ba);
offset = BUS_ADDR_PADDR(ba);
DPRINTF(EDB_BUSMAP,
("\n_ebus_bus_map: bar %d offset %08x sz %x flags %x va %p\n",
(int)bar, (uint32_t)offset, (uint32_t)size,
flags, (void *)va));
/* EBus has only two BARs */
if (PCI_MAPREG_NUM(bar) > 1) {
DPRINTF(EDB_BUSMAP,
("\n_ebus_bus_map: impossible bar\n"));
return (EINVAL);
}
/*
* Almost all of the interesting ebus children are mapped by
* BAR1, the last entry in sc_reg[], so work our way backwards.
*/
for (i = sc->sc_nreg - 1; i >= 0; --i) {
bus_addr_t pciaddr;
uint32_t ss;
/* EBus only does MEM32 */
ss = sc->sc_reg[i].phys_hi & OFW_PCI_PHYS_HI_SPACEMASK;
if (ss != OFW_PCI_PHYS_HI_SPACE_MEM32)
continue;
if (bar != (sc->sc_reg[i].phys_hi
& OFW_PCI_PHYS_HI_REGISTERMASK))
continue;
pciaddr = (bus_addr_t)sc->sc_reg[i].phys_lo + offset;
if (pciaddr + size > sc->sc_reg[i].phys_lo
+ sc->sc_reg[i].size_lo)
continue;
DPRINTF(EDB_BUSMAP,
("_ebus_bus_map: mapping to PCI addr %x\n",
(uint32_t)pciaddr));
/* pass it onto the pci controller */
return (bus_space_map2(t->parent, pciaddr, size,
flags, va, hp));
}
DPRINTF(EDB_BUSMAP, (": FAILED\n"));
return (EINVAL);
}
static paddr_t
ebus_bus_mmap(bus_space_tag_t t, bus_addr_t ba, off_t off, int prot, int flags)
{
/* XXX: not implemented yet */
return (-1);
}
/*
* Install an interrupt handler for a EBus device.
*/
static void *
ebus_intr_establish(bus_space_tag_t t, int pri, int level,
int (*handler)(void *), void *arg,
void (*fastvec)(void))
{
return (bus_intr_establish(t->parent, pri, level, handler, arg));
}
#ifdef BLINK
static void
ebus_blink(void *zero)
{
register int s;
s = splhigh();
*ebus_LED = ~*ebus_LED;
splx(s);
/*
* Blink rate is:
* full cycle every second if completely idle (loadav = 0)
* full cycle every 2 seconds if loadav = 1
* full cycle every 3 seconds if loadav = 2
* etc.
*/
s = (((averunnable.ldavg[0] + FSCALE) * hz) >> (FSHIFT + 1));
callout_reset(&ebus_blink_ch, s, ebus_blink, NULL);
}
#endif
|