1 /* $NetBSD: ebus.c,v 1.30 2008/05/29 14:51:26 mrg Exp $ */
4 * Copyright (c) 1999, 2000 Matthew R. Green
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * EBus support for PCI based SPARC systems (ms-IIep, Ultra).
31 * EBus is documented in PCIO manual (Sun Part#: 802-7837-01).
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ebus.c,v 1.30 2008/05/29 14:51:26 mrg Exp $");
37 #if defined(DEBUG) && !defined(EBUS_DEBUG)
43 #define EDB_CHILD 0x02
44 #define EDB_INTRMAP 0x04
45 #define EDB_BUSMAP 0x08
46 #define EDB_BUSDMA 0x10
49 #define DPRINTF(l, s) do { if (ebus_debug & l) printf s; } while (0)
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/device.h>
57 #include <sys/errno.h>
58 #include <sys/malloc.h>
59 #include <sys/callout.h>
60 #include <sys/kernel.h>
62 #define _SPARC_BUS_DMA_PRIVATE
63 #include <machine/bus.h>
64 #include <machine/autoconf.h>
66 #include <dev/pci/pcivar.h>
67 #include <dev/pci/pcireg.h>
68 #include <dev/pci/pcidevs.h>
70 #include <dev/ofw/ofw_pci.h>
72 #include <dev/ebus/ebusreg.h>
73 #include <dev/ebus/ebusvar.h>
75 #include "opt_blink.h"
77 volatile uint32_t *ebus_LED
= NULL
;
80 static callout_t ebus_blink_ch
;
81 static void ebus_blink(void *);
86 device_t sc_parent
; /* PCI bus */
88 int sc_node
; /* PROM node */
90 bus_space_tag_t sc_bustag
; /* mem tag from pci */
93 * "reg" contains exactly the info we'd get by processing
94 * "ranges", so don't bother with "ranges" and use "reg" directly.
96 struct ofw_pci_register
*sc_reg
;
100 static int ebus_match(device_t
, cfdata_t
, void *);
101 static void ebus_attach(device_t
, device_t
, void *);
103 CFATTACH_DECL(ebus
, sizeof(struct ebus_softc
),
104 ebus_match
, ebus_attach
, NULL
, NULL
);
106 static int ebus_setup_attach_args(struct ebus_softc
*, bus_space_tag_t
,
107 bus_dma_tag_t
, int, struct ebus_attach_args
*);
108 static void ebus_destroy_attach_args(struct ebus_attach_args
*);
109 static int ebus_print(void *, const char *);
112 * here are our bus space and bus DMA routines.
114 static paddr_t
ebus_bus_mmap(bus_space_tag_t
, bus_addr_t
, off_t
, int, int);
115 static int _ebus_bus_map(bus_space_tag_t
, bus_addr_t
, bus_size_t
, int,
116 vaddr_t
, bus_space_handle_t
*);
117 static void *ebus_intr_establish(bus_space_tag_t
, int, int,
118 int (*)(void *), void *, void (*)(void));
120 static bus_dma_tag_t
ebus_alloc_dma_tag(struct ebus_softc
*, bus_dma_tag_t
);
124 * Working around PROM bogosity.
126 * EBus doesn't have official OFW binding. sparc64 has a de-facto
127 * standard but patching it in in prompatch.c and then decoding it
128 * here would be an overkill for ms-IIep.
130 * So we assume that all ms-IIep based systems use PCIO chip only in
131 * "motherboard mode" with interrupt lines wired directly to ms-IIep
134 * Note that this is ineligible for prompatch.c, as we are not
135 * correcting PROM to conform to some established standard, this hack
136 * is tied to this version of ebus driver and as such it's better stay
137 * private to the driver.
140 struct msiiep_ebus_intr_wiring
{
141 const char *name
; /* PROM node */
142 int line
; /* ms-IIep interrupt input */
145 static const struct msiiep_ebus_intr_wiring krups_ebus_intr_wiring
[] = {
146 { "su", 0 }, { "8042", 0 }, { "sound", 3 }
149 static const struct msiiep_ebus_intr_wiring espresso_ebus_intr_wiring
[] = {
150 { "su", 0 }, { "8042", 0 }, { "sound", 3 }, { "parallel", 4 }
154 struct msiiep_known_ebus_wiring
{
156 const struct msiiep_ebus_intr_wiring
*map
;
160 #define MSIIEP_MODEL_WIRING(name, map) \
161 { name, map, sizeof(map)/sizeof(map[0]) }
163 static const struct msiiep_known_ebus_wiring known_models
[] = {
164 MSIIEP_MODEL_WIRING("SUNW,501-4267", krups_ebus_intr_wiring
),
165 MSIIEP_MODEL_WIRING("SUNW,375-0059", espresso_ebus_intr_wiring
),
171 * XXX: This assumes single EBus. However I don't think any ms-IIep
172 * system ever used more than one. In any case, without looking at a
173 * system with multiple PCIO chips I don't know how to correctly
174 * program the driver to handle PROM glitches in them, so for the time
175 * being just use globals.
177 static const struct msiiep_ebus_intr_wiring
*wiring_map
;
178 static int wiring_map_size
;
180 static int ebus_init_wiring_table(struct ebus_softc
*);
184 ebus_match(device_t parent
, cfdata_t cf
, void *aux
)
186 struct pci_attach_args
*pa
= aux
;
190 /* Only attach if there's a PROM node. */
191 node
= PCITAG_NODE(pa
->pa_tag
);
195 prom_getpropstringA(node
, "name", name
, sizeof name
);
196 if (PCI_CLASS(pa
->pa_class
) == PCI_CLASS_BRIDGE
197 && PCI_VENDOR(pa
->pa_id
) == PCI_VENDOR_SUN
198 && PCI_PRODUCT(pa
->pa_id
) == PCI_PRODUCT_SUN_EBUS
199 && strcmp(name
, "ebus") == 0)
207 ebus_init_wiring_table(struct ebus_softc
*sc
)
209 const struct msiiep_known_ebus_wiring
*p
;
213 if (wiring_map
!= NULL
) {
214 printf("%s: global ebus wiring map already initalized\n",
215 device_xname(&sc
->sc_dev
));
219 model
= prom_getpropstringA(prom_findroot(), "model",
222 panic("ebus_init_wiring_table: no \"model\" property");
224 for (p
= known_models
; p
->model
!= NULL
; ++p
)
225 if (strcmp(model
, p
->model
) == 0) {
227 wiring_map_size
= p
->mapsize
;
231 /* not found? we should have failed in pci_attach_hook then. */
232 panic("ebus_init_wiring_table: unknown model %s", model
);
237 * attach an ebus and all it's children. this code is modeled
238 * after the sbus code which does similar things.
241 ebus_attach(device_t parent
, device_t self
, void *aux
)
243 struct ebus_softc
*sc
= device_private(self
);
244 struct pci_attach_args
*pa
= aux
;
245 struct ebus_attach_args ea
;
247 bus_dma_tag_t dmatag
;
248 bus_space_handle_t hLED
;
254 callout_init(&ebus_blink_ch
, 0);
257 pci_devinfo(pa
->pa_id
, pa
->pa_class
, 0, devinfo
, sizeof(devinfo
));
258 printf(": %s, revision 0x%02x\n",
259 devinfo
, PCI_REVISION(pa
->pa_class
));
261 node
= PCITAG_NODE(pa
->pa_tag
);
263 panic("%s: unable to find ebus node", device_xname(self
));
265 if (ebus_init_wiring_table(sc
) == 0)
268 /* map the LED register */
269 base14
= pci_conf_read(pa
->pa_pc
, pa
->pa_tag
, 0x14);
270 if (bus_space_map(pa
->pa_memt
, base14
+ 0x726000, 4, 0, &hLED
) == 0) {
271 ebus_LED
= bus_space_vaddr(pa
->pa_memt
, hLED
);
273 ebus_blink((void *)0);
276 printf("unable to map the LED register\n");
280 sc
->sc_parent
= parent
; /* XXX: unused so far */
281 sc
->sc_bustag
= pa
->pa_memt
; /* EBus only does PCI MEM32 space */
283 if ((sbt
= bus_space_tag_alloc(sc
->sc_bustag
, sc
)) == NULL
)
284 panic("unable to allocate ebus bus tag");
286 sbt
->sparc_bus_map
= _ebus_bus_map
;
287 sbt
->sparc_bus_mmap
= ebus_bus_mmap
;
288 sbt
->sparc_intr_establish
= ebus_intr_establish
;
290 dmatag
= ebus_alloc_dma_tag(sc
, pa
->pa_dmat
);
293 * Setup ranges. The interesting thing is that we use "reg"
294 * not "ranges", since "reg" on ebus has exactly the data we'd
295 * get by processing "ranges".
297 error
= prom_getprop(node
, "reg", sizeof(struct ofw_pci_register
),
298 &sc
->sc_nreg
, &sc
->sc_reg
);
300 panic("%s: unable to read ebus registers (error %d)",
301 device_xname(self
), error
);
304 * now attach all our children
306 DPRINTF(EDB_CHILD
, ("ebus node %08x, searching children...\n", node
));
307 for (node
= firstchild(node
); node
; node
= nextsibling(node
)) {
308 char *name
= prom_getpropstring(node
, "name");
310 if (ebus_setup_attach_args(sc
, sbt
, dmatag
, node
, &ea
) != 0) {
311 printf("ebus_attach: %s: incomplete\n", name
);
315 ("- found child `%s', attaching\n", ea
.ea_name
));
316 (void)config_found(self
, &ea
, ebus_print
);
317 ebus_destroy_attach_args(&ea
);
322 ebus_setup_attach_args(struct ebus_softc
*sc
,
323 bus_space_tag_t bustag
, bus_dma_tag_t dmatag
, int node
,
324 struct ebus_attach_args
*ea
)
328 memset(ea
, 0, sizeof(struct ebus_attach_args
));
330 err
= prom_getprop(node
, "name", 1, &n
, &ea
->ea_name
);
333 ea
->ea_name
[n
] = '\0';
336 ea
->ea_bustag
= bustag
;
337 ea
->ea_dmatag
= dmatag
;
339 err
= prom_getprop(node
, "reg", sizeof(struct ebus_regs
),
340 &ea
->ea_nreg
, &ea
->ea_reg
);
345 * On Ultra the bar is the _offset_ of the BAR in PCI config
346 * space but in (some?) ms-IIep systems (e.g. Krups) it's the
347 * _number_ of the BAR - e.g. BAR1 is represented by 1 in
348 * Krups PROM, while on Ultra it's 0x14. Fix it here.
350 for (n
= 0; n
< ea
->ea_nreg
; ++n
)
351 if (ea
->ea_reg
[n
].hi
< PCI_MAPREG_START
) {
352 ea
->ea_reg
[n
].hi
= PCI_MAPREG_START
353 + ea
->ea_reg
[n
].hi
* sizeof(pcireg_t
);
356 err
= prom_getprop(node
, "address", sizeof(uint32_t),
357 &ea
->ea_nvaddr
, &ea
->ea_vaddr
);
362 if (ea
->ea_nreg
!= ea
->ea_nvaddr
)
363 printf("ebus loses: device %s: %d regs and %d addrs\n",
364 ea
->ea_name
, ea
->ea_nreg
, ea
->ea_nvaddr
);
368 /* XXX: "interrupts" hack */
369 for (n
= 0; n
< wiring_map_size
; ++n
) {
370 const struct msiiep_ebus_intr_wiring
*w
= &wiring_map
[n
];
371 if (strcmp(w
->name
, ea
->ea_name
) == 0) {
372 ea
->ea_intr
= malloc(sizeof(uint32_t),
374 ea
->ea_intr
[0] = w
->line
;
384 ebus_destroy_attach_args(struct ebus_attach_args
*ea
)
388 free((void *)ea
->ea_name
, M_DEVBUF
);
390 free((void *)ea
->ea_reg
, M_DEVBUF
);
392 free((void *)ea
->ea_intr
, M_DEVBUF
);
394 free((void *)ea
->ea_vaddr
, M_DEVBUF
);
398 ebus_print(void *aux
, const char *p
)
400 struct ebus_attach_args
*ea
= aux
;
404 aprint_normal("%s at %s", ea
->ea_name
, p
);
405 for (i
= 0; i
< ea
->ea_nreg
; ++i
)
406 aprint_normal("%s bar %x offset 0x%x", i
== 0 ? "" : ",",
407 ea
->ea_reg
[i
].hi
, ea
->ea_reg
[i
].lo
);
408 for (i
= 0; i
< ea
->ea_nintr
; ++i
)
409 aprint_normal(" line %d", ea
->ea_intr
[i
]);
415 * bus space and bus DMA methods below here
418 ebus_alloc_dma_tag(struct ebus_softc
*sc
, bus_dma_tag_t pdt
)
423 malloc(sizeof(struct sparc_bus_dma_tag
), M_DEVBUF
, M_NOWAIT
);
425 panic("unable to allocate ebus DMA tag");
427 memset(dt
, 0, sizeof *dt
);
429 #define PCOPY(x) dt->x = pdt->x
430 PCOPY(_dmamap_create
);
431 PCOPY(_dmamap_destroy
);
433 PCOPY(_dmamap_load_mbuf
);
434 PCOPY(_dmamap_load_uio
);
435 PCOPY(_dmamap_load_raw
);
436 PCOPY(_dmamap_unload
);
438 PCOPY(_dmamem_alloc
);
441 PCOPY(_dmamem_unmap
);
448 * bus space support. <sparc64/dev/psychoreg.h> has a discussion
449 * about PCI physical addresses, which also applies to ebus.
452 _ebus_bus_map(bus_space_tag_t t
, bus_addr_t ba
, bus_size_t size
, int flags
,
453 vaddr_t va
, bus_space_handle_t
*hp
)
455 struct ebus_softc
*sc
= t
->cookie
;
460 bar
= BUS_ADDR_IOSPACE(ba
);
461 offset
= BUS_ADDR_PADDR(ba
);
464 ("\n_ebus_bus_map: bar %d offset %08x sz %x flags %x va %p\n",
465 (int)bar
, (uint32_t)offset
, (uint32_t)size
,
468 /* EBus has only two BARs */
469 if (PCI_MAPREG_NUM(bar
) > 1) {
471 ("\n_ebus_bus_map: impossible bar\n"));
476 * Almost all of the interesting ebus children are mapped by
477 * BAR1, the last entry in sc_reg[], so work our way backwards.
479 for (i
= sc
->sc_nreg
- 1; i
>= 0; --i
) {
483 /* EBus only does MEM32 */
484 ss
= sc
->sc_reg
[i
].phys_hi
& OFW_PCI_PHYS_HI_SPACEMASK
;
485 if (ss
!= OFW_PCI_PHYS_HI_SPACE_MEM32
)
488 if (bar
!= (sc
->sc_reg
[i
].phys_hi
489 & OFW_PCI_PHYS_HI_REGISTERMASK
))
492 pciaddr
= (bus_addr_t
)sc
->sc_reg
[i
].phys_lo
+ offset
;
494 if (pciaddr
+ size
> sc
->sc_reg
[i
].phys_lo
495 + sc
->sc_reg
[i
].size_lo
)
499 ("_ebus_bus_map: mapping to PCI addr %x\n",
502 /* pass it onto the pci controller */
503 return (bus_space_map2(t
->parent
, pciaddr
, size
,
507 DPRINTF(EDB_BUSMAP
, (": FAILED\n"));
512 ebus_bus_mmap(bus_space_tag_t t
, bus_addr_t ba
, off_t off
, int prot
, int flags
)
515 /* XXX: not implemented yet */
520 * Install an interrupt handler for a EBus device.
523 ebus_intr_establish(bus_space_tag_t t
, int pri
, int level
,
524 int (*handler
)(void *), void *arg
,
525 void (*fastvec
)(void))
528 return (bus_intr_establish(t
->parent
, pri
, level
, handler
, arg
));
534 ebus_blink(void *zero
)
539 *ebus_LED
= ~*ebus_LED
;
543 * full cycle every second if completely idle (loadav = 0)
544 * full cycle every 2 seconds if loadav = 1
545 * full cycle every 3 seconds if loadav = 2
548 s
= (((averunnable
.ldavg
[0] + FSCALE
) * hz
) >> (FSHIFT
+ 1));
549 callout_reset(&ebus_blink_ch
, s
, ebus_blink
, NULL
);