xref: /netbsd-src/sys/arch/sparc/dev/ebus.c (revision 3944ff70a48a86bb622139705a33f8ef461e24f7)
1*3944ff70Sthorpej /*	$NetBSD: ebus.c,v 1.42 2022/01/22 11:49:16 thorpej Exp $ */
2255d046fSuwe 
3255d046fSuwe /*
4255d046fSuwe  * Copyright (c) 1999, 2000 Matthew R. Green
5255d046fSuwe  * All rights reserved.
6255d046fSuwe  *
7255d046fSuwe  * Redistribution and use in source and binary forms, with or without
8255d046fSuwe  * modification, are permitted provided that the following conditions
9255d046fSuwe  * are met:
10255d046fSuwe  * 1. Redistributions of source code must retain the above copyright
11255d046fSuwe  *    notice, this list of conditions and the following disclaimer.
12255d046fSuwe  * 2. Redistributions in binary form must reproduce the above copyright
13255d046fSuwe  *    notice, this list of conditions and the following disclaimer in the
14255d046fSuwe  *    documentation and/or other materials provided with the distribution.
15255d046fSuwe  *
16255d046fSuwe  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17255d046fSuwe  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18255d046fSuwe  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19255d046fSuwe  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20255d046fSuwe  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21255d046fSuwe  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22255d046fSuwe  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23255d046fSuwe  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24255d046fSuwe  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25255d046fSuwe  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26255d046fSuwe  * SUCH DAMAGE.
27255d046fSuwe  */
28255d046fSuwe 
29255d046fSuwe /*
30255d046fSuwe  * EBus support for PCI based SPARC systems (ms-IIep, Ultra).
31255d046fSuwe  * EBus is documented in PCIO manual (Sun Part#: 802-7837-01).
32255d046fSuwe  */
33255d046fSuwe 
34a4183603Slukem #include <sys/cdefs.h>
35*3944ff70Sthorpej __KERNEL_RCSID(0, "$NetBSD: ebus.c,v 1.42 2022/01/22 11:49:16 thorpej Exp $");
36a4183603Slukem 
377948c194Suwe #if defined(DEBUG) && !defined(EBUS_DEBUG)
387948c194Suwe #define EBUS_DEBUG
397948c194Suwe #endif
40255d046fSuwe 
417948c194Suwe #ifdef EBUS_DEBUG
42255d046fSuwe #define	EDB_PROM	0x01
43255d046fSuwe #define EDB_CHILD	0x02
44255d046fSuwe #define	EDB_INTRMAP	0x04
45255d046fSuwe #define EDB_BUSMAP	0x08
46255d046fSuwe #define EDB_BUSDMA	0x10
47255d046fSuwe #define EDB_INTR	0x20
48255d046fSuwe int ebus_debug = 0;
49255d046fSuwe #define DPRINTF(l, s)   do { if (ebus_debug & l) printf s; } while (0)
50255d046fSuwe #else
51255d046fSuwe #define DPRINTF(l, s)
52255d046fSuwe #endif
53255d046fSuwe 
54255d046fSuwe #include <sys/param.h>
55ec05898dSuwe #include <sys/systm.h>
56255d046fSuwe #include <sys/device.h>
57255d046fSuwe #include <sys/errno.h>
58255d046fSuwe #include <sys/malloc.h>
5938e52ce4Sthorpej #include <sys/kmem.h>
60321e7799Smacallan #include <sys/callout.h>
61321e7799Smacallan #include <sys/kernel.h>
62255d046fSuwe 
63255d046fSuwe #define _SPARC_BUS_DMA_PRIVATE
64b6584574Sdyoung #include <sys/bus.h>
65255d046fSuwe #include <machine/autoconf.h>
66255d046fSuwe 
67255d046fSuwe #include <dev/pci/pcivar.h>
68255d046fSuwe #include <dev/pci/pcireg.h>
69255d046fSuwe #include <dev/pci/pcidevs.h>
70255d046fSuwe 
71255d046fSuwe #include <dev/ofw/ofw_pci.h>
72255d046fSuwe 
738598481fSuwe #include <dev/ebus/ebusreg.h>
748598481fSuwe #include <dev/ebus/ebusvar.h>
75255d046fSuwe 
76321e7799Smacallan #include "opt_blink.h"
77321e7799Smacallan 
78321e7799Smacallan volatile uint32_t *ebus_LED = NULL;
79321e7799Smacallan 
80321e7799Smacallan #ifdef BLINK
8188ab7da9Sad static callout_t ebus_blink_ch;
82321e7799Smacallan static void ebus_blink(void *);
83321e7799Smacallan #endif
84255d046fSuwe 
858598481fSuwe struct ebus_softc {
86142a0210Smrg 	device_t			sc_dev;
87102f0440Stsutsui 	device_t			sc_parent;	/* PCI bus */
888598481fSuwe 
898598481fSuwe 	int				sc_node;	/* PROM node */
908598481fSuwe 
918598481fSuwe 	bus_space_tag_t			sc_bustag;	/* mem tag from pci */
928598481fSuwe 
938598481fSuwe 	/*
948598481fSuwe 	 * "reg" contains exactly the info we'd get by processing
958598481fSuwe 	 * "ranges", so don't bother with "ranges" and use "reg" directly.
968598481fSuwe 	 */
978598481fSuwe 	struct ofw_pci_register		*sc_reg;
988598481fSuwe 	int				sc_nreg;
998598481fSuwe };
1008598481fSuwe 
101102f0440Stsutsui static int	ebus_match(device_t, cfdata_t, void *);
102102f0440Stsutsui static void	ebus_attach(device_t, device_t, void *);
103255d046fSuwe 
104142a0210Smrg CFATTACH_DECL_NEW(ebus, sizeof(struct ebus_softc),
1054bf871a7Sthorpej     ebus_match, ebus_attach, NULL, NULL);
106255d046fSuwe 
107ec05898dSuwe static int	ebus_setup_attach_args(struct ebus_softc *, bus_space_tag_t,
108ea303077Spk 				bus_dma_tag_t, int, struct ebus_attach_args *);
109ec05898dSuwe static void	ebus_destroy_attach_args(struct ebus_attach_args *);
110ec05898dSuwe static int	ebus_print(void *, const char *);
111255d046fSuwe 
112255d046fSuwe /*
1131ffa7b76Swiz  * here are our bus space and bus DMA routines.
114255d046fSuwe  */
115255d046fSuwe static paddr_t	ebus_bus_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
116ec05898dSuwe static int	_ebus_bus_map(bus_space_tag_t, bus_addr_t, bus_size_t, int,
117ec05898dSuwe 			      vaddr_t, bus_space_handle_t *);
118725a6aebSpk static void	*ebus_intr_establish(bus_space_tag_t, int, int,
119725a6aebSpk 				     int (*)(void *), void *, void (*)(void));
120255d046fSuwe 
1218598481fSuwe static bus_dma_tag_t	ebus_alloc_dma_tag(struct ebus_softc *, bus_dma_tag_t);
1228598481fSuwe 
1238598481fSuwe 
124255d046fSuwe /*
125255d046fSuwe  * Working around PROM bogosity.
126255d046fSuwe  *
127255d046fSuwe  * EBus doesn't have official OFW binding.  sparc64 has a de-facto
128255d046fSuwe  * standard but patching it in in prompatch.c and then decoding it
129255d046fSuwe  * here would be an overkill for ms-IIep.
130255d046fSuwe  *
131255d046fSuwe  * So we assume that all ms-IIep based systems use PCIO chip only in
132255d046fSuwe  * "motherboard mode" with interrupt lines wired directly to ms-IIep
133255d046fSuwe  * interrupt inputs.
134255d046fSuwe  *
135255d046fSuwe  * Note that this is ineligible for prompatch.c, as we are not
136255d046fSuwe  * correcting PROM to conform to some established standard, this hack
137255d046fSuwe  * is tied to this version of ebus driver and as such it's better stay
138255d046fSuwe  * private to the driver.
139255d046fSuwe  */
140255d046fSuwe 
141255d046fSuwe struct msiiep_ebus_intr_wiring {
142255d046fSuwe 	const char *name;	/* PROM node */
143255d046fSuwe 	int line;		/* ms-IIep interrupt input */
144255d046fSuwe };
145255d046fSuwe 
146ec05898dSuwe static const struct msiiep_ebus_intr_wiring krups_ebus_intr_wiring[] = {
147255d046fSuwe 	{ "su", 0 }, { "8042", 0 }, { "sound", 3 }
148255d046fSuwe };
149255d046fSuwe 
15094f3432cSjdc static const struct msiiep_ebus_intr_wiring espresso_ebus_intr_wiring[] = {
15194f3432cSjdc 	{ "su", 0 }, { "8042", 0 }, { "sound", 3 }, { "parallel", 4 }
15294f3432cSjdc };
15394f3432cSjdc 
154255d046fSuwe 
155255d046fSuwe struct msiiep_known_ebus_wiring {
156255d046fSuwe 	const char *model;
157ec05898dSuwe 	const struct msiiep_ebus_intr_wiring *map;
158255d046fSuwe 	int mapsize;
159255d046fSuwe };
160255d046fSuwe 
161255d046fSuwe #define MSIIEP_MODEL_WIRING(name, map) \
162255d046fSuwe 	{ name, map, sizeof(map)/sizeof(map[0]) }
163255d046fSuwe 
164ec05898dSuwe static const struct msiiep_known_ebus_wiring known_models[] = {
165255d046fSuwe 	MSIIEP_MODEL_WIRING("SUNW,501-4267", krups_ebus_intr_wiring),
16694f3432cSjdc 	MSIIEP_MODEL_WIRING("SUNW,375-0059", espresso_ebus_intr_wiring),
167255d046fSuwe 	{ NULL, NULL, 0}
168255d046fSuwe };
169255d046fSuwe 
170255d046fSuwe 
171255d046fSuwe /*
172255d046fSuwe  * XXX: This assumes single EBus.  However I don't think any ms-IIep
173255d046fSuwe  * system ever used more than one.  In any case, without looking at a
174255d046fSuwe  * system with multiple PCIO chips I don't know how to correctly
175255d046fSuwe  * program the driver to handle PROM glitches in them, so for the time
176255d046fSuwe  * being just use globals.
177255d046fSuwe  */
178ec05898dSuwe static const struct msiiep_ebus_intr_wiring *wiring_map;
179255d046fSuwe static int wiring_map_size;
180255d046fSuwe 
181255d046fSuwe static int ebus_init_wiring_table(struct ebus_softc *);
182255d046fSuwe 
183255d046fSuwe 
184ec05898dSuwe static int
ebus_match(device_t parent,cfdata_t cf,void * aux)185102f0440Stsutsui ebus_match(device_t parent, cfdata_t cf, void *aux)
186255d046fSuwe {
187255d046fSuwe 	struct pci_attach_args *pa = aux;
188255d046fSuwe 	char name[10];
189255d046fSuwe 	int node;
190255d046fSuwe 
191255d046fSuwe 	/* Only attach if there's a PROM node. */
192255d046fSuwe 	node = PCITAG_NODE(pa->pa_tag);
193255d046fSuwe 	if (node == -1)
194255d046fSuwe 		return (0);
195255d046fSuwe 
196ea53363eSpk 	prom_getpropstringA(node, "name", name, sizeof name);
197255d046fSuwe 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE
198255d046fSuwe 	    && PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN
199255d046fSuwe 	    && PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUS
200255d046fSuwe 	    && strcmp(name, "ebus") == 0)
201255d046fSuwe 		return (1);
202255d046fSuwe 
203255d046fSuwe 	return (0);
204255d046fSuwe }
205255d046fSuwe 
206255d046fSuwe 
207255d046fSuwe static int
ebus_init_wiring_table(struct ebus_softc * sc)208ec05898dSuwe ebus_init_wiring_table(struct ebus_softc *sc)
209255d046fSuwe {
210ec05898dSuwe 	const struct msiiep_known_ebus_wiring *p;
211255d046fSuwe 	char buf[32];
212255d046fSuwe 	char *model;
213255d046fSuwe 
214255d046fSuwe 	if (wiring_map != NULL) {
215c56890eeSmsaitoh 		printf("%s: global ebus wiring map already initialized\n",
216142a0210Smrg 		    device_xname(sc->sc_dev));
217255d046fSuwe 		return (0);
218255d046fSuwe 	}
219255d046fSuwe 
220ea53363eSpk 	model = prom_getpropstringA(prom_findroot(), "model",
221255d046fSuwe 				    buf, sizeof(buf));
222255d046fSuwe 	if (model == NULL)
223255d046fSuwe 		panic("ebus_init_wiring_table: no \"model\" property");
224255d046fSuwe 
225255d046fSuwe 	for (p = known_models; p->model != NULL; ++p)
226255d046fSuwe 		if (strcmp(model, p->model) == 0) {
227255d046fSuwe 			wiring_map = p->map;
228255d046fSuwe 			wiring_map_size = p->mapsize;
229255d046fSuwe 			return (1);
230255d046fSuwe 		}
231255d046fSuwe 
232255d046fSuwe 	/* not found?  we should have failed in pci_attach_hook then. */
233255d046fSuwe 	panic("ebus_init_wiring_table: unknown model %s", model);
234255d046fSuwe }
235255d046fSuwe 
236255d046fSuwe 
237255d046fSuwe /*
238f0a7346dSsnj  * attach an ebus and all its children.  this code is modeled
239255d046fSuwe  * after the sbus code which does similar things.
240255d046fSuwe  */
241ec05898dSuwe static void
ebus_attach(device_t parent,device_t self,void * aux)242102f0440Stsutsui ebus_attach(device_t parent, device_t self, void *aux)
243255d046fSuwe {
244102f0440Stsutsui 	struct ebus_softc *sc = device_private(self);
245255d046fSuwe 	struct pci_attach_args *pa = aux;
246255d046fSuwe 	struct ebus_attach_args ea;
247ea303077Spk 	bus_space_tag_t sbt;
248ea303077Spk 	bus_dma_tag_t dmatag;
249321e7799Smacallan 	bus_space_handle_t hLED;
2503c33ccf0She 	pcireg_t base14;
251255d046fSuwe 	int node, error;
252255d046fSuwe 	char devinfo[256];
253255d046fSuwe 
254142a0210Smrg 	sc->sc_dev = self;
255142a0210Smrg 
25688ab7da9Sad #ifdef BLINK
25788ab7da9Sad 	callout_init(&ebus_blink_ch, 0);
25888ab7da9Sad #endif
25988ab7da9Sad 
2600e5d2423Skleink 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
26139f110f6Suwe 	printf(": %s, revision 0x%02x\n",
26239f110f6Suwe 	       devinfo, PCI_REVISION(pa->pa_class));
263255d046fSuwe 
264255d046fSuwe 	node = PCITAG_NODE(pa->pa_tag);
265255d046fSuwe 	if (node == -1)
266102f0440Stsutsui 		panic("%s: unable to find ebus node", device_xname(self));
267255d046fSuwe 
268255d046fSuwe 	if (ebus_init_wiring_table(sc) == 0)
269255d046fSuwe 		return;
270255d046fSuwe 
271321e7799Smacallan 	/* map the LED register */
272321e7799Smacallan 	base14 = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x14);
273321e7799Smacallan 	if (bus_space_map(pa->pa_memt, base14 + 0x726000, 4, 0, &hLED) == 0) {
274321e7799Smacallan 		ebus_LED = bus_space_vaddr(pa->pa_memt, hLED);
275321e7799Smacallan #ifdef BLINK
27653524e44Schristos 		ebus_blink((void *)0);
277321e7799Smacallan #endif
278321e7799Smacallan 	} else {
279321e7799Smacallan 		printf("unable to map the LED register\n");
280321e7799Smacallan 	}
281321e7799Smacallan 
282255d046fSuwe 	sc->sc_node = node;
283255d046fSuwe 	sc->sc_parent = parent;	/* XXX: unused so far */
284255d046fSuwe 	sc->sc_bustag = pa->pa_memt; /* EBus only does PCI MEM32 space */
285ea303077Spk 
286ea303077Spk 	if ((sbt = bus_space_tag_alloc(sc->sc_bustag, sc)) == NULL)
287ea303077Spk 		panic("unable to allocate ebus bus tag");
288ea303077Spk 
289ea303077Spk 	sbt->sparc_bus_map = _ebus_bus_map;
290ea303077Spk 	sbt->sparc_bus_mmap = ebus_bus_mmap;
291ea303077Spk 	sbt->sparc_intr_establish = ebus_intr_establish;
292ea303077Spk 
293ea303077Spk 	dmatag = ebus_alloc_dma_tag(sc, pa->pa_dmat);
294255d046fSuwe 
295255d046fSuwe 	/*
296255d046fSuwe 	 * Setup ranges.  The interesting thing is that we use "reg"
297255d046fSuwe 	 * not "ranges", since "reg" on ebus has exactly the data we'd
298255d046fSuwe 	 * get by processing "ranges".
299255d046fSuwe 	 */
300ea53363eSpk 	error = prom_getprop(node, "reg", sizeof(struct ofw_pci_register),
3019e599bdbSmrg 			     &sc->sc_nreg, &sc->sc_reg);
302255d046fSuwe 	if (error)
303255d046fSuwe 		panic("%s: unable to read ebus registers (error %d)",
304102f0440Stsutsui 		    device_xname(self), error);
305255d046fSuwe 
306255d046fSuwe 	/*
307255d046fSuwe 	 * now attach all our children
308255d046fSuwe 	 */
309255d046fSuwe 	DPRINTF(EDB_CHILD, ("ebus node %08x, searching children...\n", node));
310*3944ff70Sthorpej 	devhandle_t selfh = device_handle(self);
311255d046fSuwe 	for (node = firstchild(node); node; node = nextsibling(node)) {
312ea53363eSpk 		char *name = prom_getpropstring(node, "name");
313255d046fSuwe 
314ea303077Spk 		if (ebus_setup_attach_args(sc, sbt, dmatag, node, &ea) != 0) {
315255d046fSuwe 			printf("ebus_attach: %s: incomplete\n", name);
316255d046fSuwe 			continue;
317255d046fSuwe 		}
318255d046fSuwe 		DPRINTF(EDB_CHILD,
319255d046fSuwe 			("- found child `%s', attaching\n", ea.ea_name));
32065c738d1Sthorpej 		(void)config_found(self, &ea, ebus_print,
321*3944ff70Sthorpej 		    CFARGS(.devhandle = prom_node_to_devhandle(selfh, node)));
322255d046fSuwe 		ebus_destroy_attach_args(&ea);
323255d046fSuwe 	}
324255d046fSuwe }
325255d046fSuwe 
326ec05898dSuwe 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)327ec05898dSuwe ebus_setup_attach_args(struct ebus_softc *sc,
328ec05898dSuwe 		       bus_space_tag_t bustag, bus_dma_tag_t dmatag, int node,
329ec05898dSuwe 		       struct ebus_attach_args	*ea)
330255d046fSuwe {
331255d046fSuwe 	int n, err;
332255d046fSuwe 
333255d046fSuwe 	memset(ea, 0, sizeof(struct ebus_attach_args));
334255d046fSuwe 
335ea53363eSpk 	err = prom_getprop(node, "name", 1, &n, &ea->ea_name);
336255d046fSuwe 	if (err != 0)
337255d046fSuwe 		return (err);
3384f67d478Smrg 	KASSERT(ea->ea_name[n-1] == '\0');
339255d046fSuwe 
340255d046fSuwe 	ea->ea_node = node;
341ea303077Spk 	ea->ea_bustag = bustag;
342ea303077Spk 	ea->ea_dmatag = dmatag;
343255d046fSuwe 
344ea53363eSpk 	err = prom_getprop(node, "reg", sizeof(struct ebus_regs),
3459e599bdbSmrg 			   &ea->ea_nreg, &ea->ea_reg);
346255d046fSuwe 	if (err != 0)
347255d046fSuwe 		return (err);
348255d046fSuwe 
3498598481fSuwe 	/*
3508598481fSuwe 	 * On Ultra the bar is the _offset_ of the BAR in PCI config
3518598481fSuwe 	 * space but in (some?) ms-IIep systems (e.g. Krups) it's the
3528598481fSuwe 	 * _number_ of the BAR - e.g. BAR1 is represented by 1 in
3538598481fSuwe 	 * Krups PROM, while on Ultra it's 0x14.  Fix it here.
3548598481fSuwe 	 */
35587c196d0Smacallan 	for (n = 0; n < ea->ea_nreg; ++n)
3568598481fSuwe 	    if (ea->ea_reg[n].hi < PCI_MAPREG_START) {
3578598481fSuwe 		ea->ea_reg[n].hi = PCI_MAPREG_START
3588598481fSuwe 		    + ea->ea_reg[n].hi * sizeof(pcireg_t);
3598598481fSuwe 	    }
3608598481fSuwe 
361ec05898dSuwe 	err = prom_getprop(node, "address", sizeof(uint32_t),
3629e599bdbSmrg 			   &ea->ea_nvaddr, &ea->ea_vaddr);
363255d046fSuwe 	if (err != ENOENT) {
364255d046fSuwe 		if (err != 0)
365255d046fSuwe 			return (err);
366255d046fSuwe 
367255d046fSuwe 		if (ea->ea_nreg != ea->ea_nvaddr)
368255d046fSuwe 			printf("ebus loses: device %s: %d regs and %d addrs\n",
369255d046fSuwe 			       ea->ea_name, ea->ea_nreg, ea->ea_nvaddr);
370255d046fSuwe 	} else
371255d046fSuwe 		ea->ea_nvaddr = 0;
372255d046fSuwe 
373255d046fSuwe 	/* XXX: "interrupts" hack */
374255d046fSuwe 	for (n = 0; n < wiring_map_size; ++n) {
375ec05898dSuwe 		const struct msiiep_ebus_intr_wiring *w = &wiring_map[n];
376255d046fSuwe 		if (strcmp(w->name, ea->ea_name) == 0) {
377ec05898dSuwe 			ea->ea_intr = malloc(sizeof(uint32_t),
378d47bcd29Schs 					     M_DEVBUF, M_WAITOK);
379255d046fSuwe 			ea->ea_intr[0] = w->line;
380255d046fSuwe 			ea->ea_nintr = 1;
381255d046fSuwe 			break;
382255d046fSuwe 		}
383255d046fSuwe 	}
384255d046fSuwe 
385255d046fSuwe 	return (0);
386255d046fSuwe }
387255d046fSuwe 
388ec05898dSuwe static void
ebus_destroy_attach_args(struct ebus_attach_args * ea)389ec05898dSuwe ebus_destroy_attach_args(struct ebus_attach_args *ea)
390255d046fSuwe {
391255d046fSuwe 
392255d046fSuwe 	if (ea->ea_name)
393255d046fSuwe 		free((void *)ea->ea_name, M_DEVBUF);
394255d046fSuwe 	if (ea->ea_reg)
395255d046fSuwe 		free((void *)ea->ea_reg, M_DEVBUF);
396255d046fSuwe 	if (ea->ea_intr)
397255d046fSuwe 		free((void *)ea->ea_intr, M_DEVBUF);
398255d046fSuwe 	if (ea->ea_vaddr)
399255d046fSuwe 		free((void *)ea->ea_vaddr, M_DEVBUF);
400255d046fSuwe }
401255d046fSuwe 
402ec05898dSuwe static int
ebus_print(void * aux,const char * p)403ec05898dSuwe ebus_print(void *aux, const char *p)
404255d046fSuwe {
405255d046fSuwe 	struct ebus_attach_args *ea = aux;
406255d046fSuwe 	int i;
407255d046fSuwe 
408255d046fSuwe 	if (p)
4099c121415Sthorpej 		aprint_normal("%s at %s", ea->ea_name, p);
410255d046fSuwe 	for (i = 0; i < ea->ea_nreg; ++i)
4119c121415Sthorpej 		aprint_normal("%s bar %x offset 0x%x", i == 0 ? "" : ",",
4128598481fSuwe 		       ea->ea_reg[i].hi, ea->ea_reg[i].lo);
413255d046fSuwe 	for (i = 0; i < ea->ea_nintr; ++i)
4149c121415Sthorpej 		aprint_normal(" line %d", ea->ea_intr[i]);
415255d046fSuwe 	return (UNCONF);
416255d046fSuwe }
417255d046fSuwe 
418255d046fSuwe 
419255d046fSuwe /*
4201ffa7b76Swiz  * bus space and bus DMA methods below here
421255d046fSuwe  */
422ec05898dSuwe static bus_dma_tag_t
ebus_alloc_dma_tag(struct ebus_softc * sc,bus_dma_tag_t pdt)423ec05898dSuwe ebus_alloc_dma_tag(struct ebus_softc *sc, bus_dma_tag_t pdt)
424255d046fSuwe {
425255d046fSuwe 	bus_dma_tag_t dt;
426255d046fSuwe 
42738e52ce4Sthorpej 	dt = kmem_zalloc(sizeof(*dt), KM_SLEEP);
428255d046fSuwe 	dt->_cookie = sc;
429255d046fSuwe #define PCOPY(x)	dt->x = pdt->x
430255d046fSuwe 	PCOPY(_dmamap_create);
431255d046fSuwe 	PCOPY(_dmamap_destroy);
432255d046fSuwe 	PCOPY(_dmamap_load);
433255d046fSuwe 	PCOPY(_dmamap_load_mbuf);
434255d046fSuwe 	PCOPY(_dmamap_load_uio);
435255d046fSuwe 	PCOPY(_dmamap_load_raw);
436255d046fSuwe 	PCOPY(_dmamap_unload);
437255d046fSuwe 	PCOPY(_dmamap_sync);
438255d046fSuwe 	PCOPY(_dmamem_alloc);
439255d046fSuwe 	PCOPY(_dmamem_free);
440255d046fSuwe 	PCOPY(_dmamem_map);
441255d046fSuwe 	PCOPY(_dmamem_unmap);
442255d046fSuwe 	PCOPY(_dmamem_mmap);
443255d046fSuwe #undef	PCOPY
444255d046fSuwe 	return (dt);
445255d046fSuwe }
446255d046fSuwe 
447255d046fSuwe /*
448255d046fSuwe  * bus space support.  <sparc64/dev/psychoreg.h> has a discussion
449255d046fSuwe  * about PCI physical addresses, which also applies to ebus.
450255d046fSuwe  */
451255d046fSuwe 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)452ec05898dSuwe _ebus_bus_map(bus_space_tag_t t, bus_addr_t ba, bus_size_t size, int flags,
453ec05898dSuwe 	      vaddr_t va, bus_space_handle_t *hp)
454255d046fSuwe {
455255d046fSuwe 	struct ebus_softc *sc = t->cookie;
456255d046fSuwe 	u_int bar;
457255d046fSuwe 	paddr_t offset;
458255d046fSuwe 	int i;
459255d046fSuwe 
4607e8becd6Spk 	bar = BUS_ADDR_IOSPACE(ba);
4617e8becd6Spk 	offset = BUS_ADDR_PADDR(ba);
462255d046fSuwe 
463255d046fSuwe 	DPRINTF(EDB_BUSMAP,
464255d046fSuwe 		("\n_ebus_bus_map: bar %d offset %08x sz %x flags %x va %p\n",
465ec05898dSuwe 		 (int)bar, (uint32_t)offset, (uint32_t)size,
4667e8becd6Spk 		 flags, (void *)va));
467255d046fSuwe 
4688598481fSuwe 	/* EBus has only two BARs */
4698598481fSuwe 	if (PCI_MAPREG_NUM(bar) > 1) {
470255d046fSuwe 		DPRINTF(EDB_BUSMAP,
471255d046fSuwe 			("\n_ebus_bus_map: impossible bar\n"));
472255d046fSuwe 		return (EINVAL);
473255d046fSuwe 	}
474255d046fSuwe 
475255d046fSuwe 	/*
476255d046fSuwe 	 * Almost all of the interesting ebus children are mapped by
477255d046fSuwe 	 * BAR1, the last entry in sc_reg[], so work our way backwards.
478255d046fSuwe 	 */
479255d046fSuwe 	for (i = sc->sc_nreg - 1; i >= 0; --i) {
480255d046fSuwe 		bus_addr_t pciaddr;
481ec05898dSuwe 		uint32_t ss;
482255d046fSuwe 
483255d046fSuwe 		/* EBus only does MEM32 */
484255d046fSuwe 		ss  = sc->sc_reg[i].phys_hi & OFW_PCI_PHYS_HI_SPACEMASK;
485255d046fSuwe 		if (ss != OFW_PCI_PHYS_HI_SPACE_MEM32)
486255d046fSuwe 			continue;
487255d046fSuwe 
488255d046fSuwe 		if (bar != (sc->sc_reg[i].phys_hi
489255d046fSuwe 			    & OFW_PCI_PHYS_HI_REGISTERMASK))
490255d046fSuwe 			continue;
491255d046fSuwe 
492255d046fSuwe 		pciaddr = (bus_addr_t)sc->sc_reg[i].phys_lo + offset;
493255d046fSuwe 
494255d046fSuwe 		if (pciaddr + size > sc->sc_reg[i].phys_lo
495255d046fSuwe 					+ sc->sc_reg[i].size_lo)
496255d046fSuwe 			continue;
497255d046fSuwe 
498255d046fSuwe 		DPRINTF(EDB_BUSMAP,
499255d046fSuwe 			("_ebus_bus_map: mapping to PCI addr %x\n",
500ec05898dSuwe 			 (uint32_t)pciaddr));
501255d046fSuwe 
502255d046fSuwe 		/* pass it onto the pci controller */
503ea303077Spk 		return (bus_space_map2(t->parent, pciaddr, size,
5047e8becd6Spk 				       flags, va, hp));
505255d046fSuwe 	}
506255d046fSuwe 
507255d046fSuwe 	DPRINTF(EDB_BUSMAP, (": FAILED\n"));
508255d046fSuwe 	return (EINVAL);
509255d046fSuwe }
510255d046fSuwe 
511255d046fSuwe static paddr_t
ebus_bus_mmap(bus_space_tag_t t,bus_addr_t ba,off_t off,int prot,int flags)512ec05898dSuwe ebus_bus_mmap(bus_space_tag_t t, bus_addr_t ba, off_t off, int prot, int flags)
513255d046fSuwe {
514255d046fSuwe 
515ea303077Spk 	/* XXX: not implemented yet */
516255d046fSuwe 	return (-1);
517255d046fSuwe }
518255d046fSuwe 
519255d046fSuwe /*
520255d046fSuwe  * Install an interrupt handler for a EBus device.
521255d046fSuwe  */
522ec05898dSuwe static void *
ebus_intr_establish(bus_space_tag_t t,int pri,int level,int (* handler)(void *),void * arg,void (* fastvec)(void))523ec05898dSuwe ebus_intr_establish(bus_space_tag_t t, int pri, int level,
524ec05898dSuwe 		    int (*handler)(void *), void *arg,
525ec05898dSuwe 		    void (*fastvec)(void))
526255d046fSuwe {
527ec05898dSuwe 
528725a6aebSpk 	return (bus_intr_establish(t->parent, pri, level, handler, arg));
529255d046fSuwe }
530321e7799Smacallan 
531321e7799Smacallan #ifdef BLINK
532321e7799Smacallan 
533321e7799Smacallan static void
ebus_blink(void * zero)534321e7799Smacallan ebus_blink(void *zero)
535321e7799Smacallan {
536321e7799Smacallan 	register int s;
537321e7799Smacallan 
538321e7799Smacallan 	s = splhigh();
539321e7799Smacallan 	*ebus_LED = ~*ebus_LED;
540321e7799Smacallan 	splx(s);
541321e7799Smacallan 	/*
542321e7799Smacallan 	 * Blink rate is:
543321e7799Smacallan 	 *	full cycle every second if completely idle (loadav = 0)
544321e7799Smacallan 	 *	full cycle every 2 seconds if loadav = 1
545321e7799Smacallan 	 *	full cycle every 3 seconds if loadav = 2
546321e7799Smacallan 	 * etc.
547321e7799Smacallan 	 */
548321e7799Smacallan 	s = (((averunnable.ldavg[0] + FSCALE) * hz) >> (FSHIFT + 1));
549321e7799Smacallan 	callout_reset(&ebus_blink_ch, s, ebus_blink, NULL);
550321e7799Smacallan }
551321e7799Smacallan #endif
552