isa.c revision 159289
1/*-
2 * Copyright (c) 1998 Doug Rabson
3 * Copyright (c) 2001 Thomas Moestl <tmm@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 *	from: FreeBSD: src/sys/alpha/isa/isa.c,v 1.26 2001/07/11
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sys/sparc64/isa/isa.c 159289 2006-06-05 17:48:54Z marius $");
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/bus.h>
36
37#include <machine/bus.h>
38
39#include <sys/rman.h>
40
41#include <isa/isareg.h>
42#include <isa/isavar.h>
43#include <isa/isa_common.h>
44
45#include <dev/ofw/ofw_bus.h>
46#include <dev/ofw/openfirm.h>
47
48#include <machine/resource.h>
49
50#include <dev/pci/pcireg.h>
51#include <dev/pci/pcivar.h>
52
53#include <sparc64/pci/ofw_pci.h>
54#include <sparc64/isa/ofw_isa.h>
55
56/* There can be only one ISA bus, so it is safe to use globals. */
57bus_space_tag_t isa_io_bt = NULL;
58bus_space_handle_t isa_io_hdl;
59bus_space_tag_t isa_mem_bt = NULL;
60bus_space_handle_t isa_mem_hdl;
61
62static u_int64_t isa_io_base;
63static u_int64_t isa_io_limit;
64static u_int64_t isa_mem_base;
65static u_int64_t isa_mem_limit;
66
67device_t isa_bus_device;
68
69static phandle_t isab_node;
70static struct isa_ranges *isab_ranges;
71static int isab_nrange;
72static ofw_pci_intr_t isa_ino[8];
73static struct ofw_bus_iinfo isa_iinfo;
74
75/*
76 * XXX: This is really partly PCI-specific, but unfortunately is
77 * differently enough to have to duplicate it here...
78 */
79#define	ISAB_RANGE_PHYS(r)						\
80	(((u_int64_t)(r)->phys_mid << 32) | (u_int64_t)(r)->phys_lo)
81#define	ISAB_RANGE_SPACE(r)	(((r)->phys_hi >> 24) & 0x03)
82
83#define	ISAR_SPACE_IO		0x01
84#define	ISAR_SPACE_MEM		0x02
85
86#define INRANGE(x, start, end)	((x) >= (start) && (x) <= (end))
87
88static void	isa_setup_children(device_t, phandle_t);
89
90intrmask_t
91isa_irq_pending(void)
92{
93	intrmask_t pending;
94	int i;
95
96	/* XXX: Is this correct? */
97	for (i = 7, pending = 0; i >= 0; i--) {
98		pending <<= 1;
99		if (isa_ino[i] != PCI_INVALID_IRQ) {
100			pending |= (OFW_PCI_INTR_PENDING(isa_bus_device,
101			    isa_ino[i]) == 0) ? 0 : 1;
102		}
103	}
104	return (pending);
105}
106
107void
108isa_init(device_t dev)
109{
110	device_t bridge;
111	int i;
112
113	/* The parent of the bus must be a PCI-ISA bridge. */
114	bridge = device_get_parent(dev);
115	isab_node = ofw_bus_get_node(bridge);
116	isab_nrange = OF_getprop_alloc(isab_node, "ranges",
117	    sizeof(*isab_ranges), (void **)&isab_ranges);
118	if (isab_nrange <= 0)
119		panic("isa_init: cannot get bridge range property");
120
121	ofw_bus_setup_iinfo(isab_node, &isa_iinfo, sizeof(ofw_isa_intr_t));
122
123	/*
124	 * This is really a bad kludge; however, it is needed to provide
125	 * isa_irq_pending(), which is unfortunately still used by some
126	 * drivers.
127	 * XXX:	The only driver still using isa_irq_pending() is sio(4)
128	 *	which we don't use on sparc64. Should we just drop support
129	 *	for isa_irq_pending()?
130	 */
131	for (i = 0; i < 8; i++)
132		isa_ino[i] = PCI_INVALID_IRQ;
133
134	isa_setup_children(dev, isab_node);
135
136	for (i = isab_nrange - 1; i >= 0; i--) {
137		switch(ISAB_RANGE_SPACE(&isab_ranges[i])) {
138		case ISAR_SPACE_IO:
139			/* This is probably always 0. */
140			isa_io_base = ISAB_RANGE_PHYS(&isab_ranges[i]);
141			isa_io_limit = isab_ranges[i].size;
142			isa_io_hdl = OFW_PCI_GET_BUS_HANDLE(bridge,
143			    SYS_RES_IOPORT, isa_io_base, &isa_io_bt);
144			break;
145		case ISAR_SPACE_MEM:
146			/* This is probably always 0. */
147			isa_mem_base = ISAB_RANGE_PHYS(&isab_ranges[i]);
148			isa_mem_limit = isab_ranges[i].size;
149			isa_mem_hdl = OFW_PCI_GET_BUS_HANDLE(bridge,
150			    SYS_RES_MEMORY, isa_mem_base, &isa_mem_bt);
151			break;
152		}
153	}
154}
155
156static const struct {
157	const char	*name;
158	uint32_t	id;
159} ofw_isa_pnp_map[] = {
160	{ "SUNW,lomh",	0x0000ae4e }, /* SUN0000 */
161	{ "dma",	0x0002d041 }, /* PNP0200 */
162	{ "floppy",	0x0007d041 }, /* PNP0700 */
163	{ "rtc",	0x000bd041 }, /* PNP0B00 */
164	{ "flashprom",	0x0100ae4e }, /* SUN0001 */
165	{ "parallel",	0x0104d041 }, /* PNP0401 */
166	{ "serial",	0x0105d041 }, /* PNP0501 */
167	{ "i2c",	0x0200ae4e }, /* SUN0002 */
168	{ "rmc-comm",	0x0300ae4e }, /* SUN0003 */
169	{ "kb_ps2",	0x0303d041 }, /* PNP0303 */
170	{ "kdmouse",	0x030fd041 }, /* PNP0F03 */
171	{ "power",	0x0c0cd041 }, /* PNP0C0C */
172	{ NULL,		0x0 }
173};
174
175static void
176isa_setup_children(device_t dev, phandle_t parent)
177{
178	struct isa_regs *regs;
179	struct resource_list *rl;
180	device_t cdev;
181	u_int64_t end, start;
182	ofw_isa_intr_t *intrs, rintr;
183	phandle_t node;
184	uint32_t *drqs, *regidx;
185	int i, ndrq, nintr, nreg, nregidx, rid, rtype;
186	char *name;
187
188	/*
189	 * Loop through children and fake up PnP devices for them.
190	 * Their resources are added as fully mapped and specified because
191	 * adjusting the resources and the resource list entries respectively
192	 * in isa_alloc_resource() causes trouble with drivers which use
193	 * rman_get_start(), pass-through or allocate and release resources
194	 * multiple times, etc. Adjusting the resources might be better off
195	 * in a bus_activate_resource method but the common ISA code doesn't
196	 * allow for an isa_activate_resource().
197	 */
198	for (node = OF_child(parent); node != 0; node = OF_peer(node)) {
199		if ((OF_getprop_alloc(node, "name", 1, (void **)&name)) == -1)
200			continue;
201
202		/*
203		 * Keyboard and mouse controllers hang off of the `8042'
204		 * node but we have no real use for the `8042' itself.
205		 */
206		if (strcmp(name, "8042") == 0) {
207			isa_setup_children(dev, node);
208			free(name, M_OFWPROP);
209			continue;
210		}
211
212		for (i = 0; ofw_isa_pnp_map[i].name != NULL; i++)
213			if (strcmp(ofw_isa_pnp_map[i].name, name) == 0)
214				break;
215		if (ofw_isa_pnp_map[i].name == NULL) {
216			printf("isa_setup_children: no PnP map entry for node "
217			    "0x%lx: %s\n", (unsigned long)node, name);
218			free(name, M_OFWPROP);
219			continue;
220		}
221
222		if ((cdev = BUS_ADD_CHILD(dev, ISA_ORDER_PNPBIOS, NULL, -1)) ==
223		    NULL)
224			panic("isa_setup_children: BUS_ADD_CHILD failed");
225		isa_set_logicalid(cdev, ofw_isa_pnp_map[i].id);
226		isa_set_vendorid(cdev, ofw_isa_pnp_map[i].id);
227
228		rl = BUS_GET_RESOURCE_LIST(dev, cdev);
229		nreg = OF_getprop_alloc(node, "reg", sizeof(*regs),
230		    (void **)&regs);
231		for (i = 0; i < nreg; i++) {
232			start = ISA_REG_PHYS(&regs[i]);
233			end = start + regs[i].size - 1;
234			rtype = ofw_isa_range_map(isab_ranges, isab_nrange,
235			    &start, &end, NULL);
236			rid = 0;
237			while (resource_list_find(rl, rtype, rid) != NULL)
238				rid++;
239			bus_set_resource(cdev, rtype, rid, start,
240			    end - start + 1);
241		}
242		if (nreg == -1 && parent != isab_node) {
243			/*
244			 * The "reg" property still might be an index into
245			 * the set of registers of the parent device like
246			 * with the nodes hanging off of the `8042' node.
247			 */
248			nregidx = OF_getprop_alloc(node, "reg", sizeof(*regidx),
249			    (void **)&regidx);
250			if (nregidx > 2)
251				panic("isa_setup_children: impossible number "
252				    "of register indices");
253			if (nregidx != -1 && (nreg = OF_getprop_alloc(parent,
254			    "reg", sizeof(*regs), (void **)&regs)) >= nregidx) {
255				for (i = 0; i < nregidx; i++) {
256					start = ISA_REG_PHYS(&regs[regidx[i]]);
257					end = start + regs[regidx[i]].size - 1;
258					rtype = ofw_isa_range_map(isab_ranges,
259					    isab_nrange, &start, &end, NULL);
260					rid = 0;
261					while (resource_list_find(rl, rtype,
262					    rid) != NULL)
263						rid++;
264					bus_set_resource(cdev, rtype, rid,
265					    start, end - start + 1);
266				}
267			}
268			if (regidx != NULL)
269				free(regidx, M_OFWPROP);
270		}
271		if (regs != NULL)
272			free(regs, M_OFWPROP);
273
274		nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intrs),
275		    (void **)&intrs);
276		for (i = 0; i < nintr; i++) {
277			if (intrs[i] > 7)
278				panic("isa_setup_children: intr too large");
279			rintr = ofw_isa_route_intr(device_get_parent(dev), node,
280			    &isa_iinfo, intrs[i]);
281			if (rintr == PCI_INVALID_IRQ)
282				panic("isa_setup_children: could not map ISA "
283				    "interrupt %d", intrs[i]);
284			isa_ino[intrs[i]] = rintr;
285			bus_set_resource(cdev, SYS_RES_IRQ, i, rintr, 1);
286		}
287		if (intrs != NULL)
288			free(intrs, M_OFWPROP);
289
290		ndrq = OF_getprop_alloc(node, "dma-channel", sizeof(*drqs),
291		    (void **)&drqs);
292		for (i = 0; i < ndrq; i++)
293			bus_set_resource(cdev, SYS_RES_DRQ, i, drqs[i], 1);
294		if (drqs != NULL)
295			free(drqs, M_OFWPROP);
296
297		/*
298		 * Devices using DMA hang off of the `dma' node instead of
299		 * directly from the ISA bridge node.
300		 */
301		if (strcmp(name, "dma") == 0)
302			isa_setup_children(dev, node);
303
304		free(name, M_OFWPROP);
305	}
306}
307
308struct resource *
309isa_alloc_resource(device_t bus, device_t child, int type, int *rid,
310		   u_long start, u_long end, u_long count, u_int flags)
311{
312	/*
313	 * Consider adding a resource definition.
314	 */
315	int passthrough = (device_get_parent(child) != bus);
316	int isdefault = (start == 0UL && end == ~0UL);
317	struct resource_list *rl;
318	struct resource_list_entry *rle;
319	u_long base, limit;
320
321	rl = BUS_GET_RESOURCE_LIST(bus, child);
322	if (!passthrough && !isdefault) {
323		rle = resource_list_find(rl, type, *rid);
324		if (!rle) {
325			if (*rid < 0)
326				return (NULL);
327			switch (type) {
328			case SYS_RES_IRQ:
329				if (*rid >= ISA_NIRQ)
330					return (NULL);
331				break;
332			case SYS_RES_DRQ:
333				if (*rid >= ISA_NDRQ)
334					return (NULL);
335				break;
336			case SYS_RES_MEMORY:
337				if (*rid >= ISA_NMEM)
338					return (NULL);
339				break;
340			case SYS_RES_IOPORT:
341				if (*rid >= ISA_NPORT)
342					return (NULL);
343				break;
344			default:
345				return (NULL);
346			}
347			resource_list_add(rl, type, *rid, start, end, count);
348		}
349	}
350
351	/*
352	 * Sanity check if the resource in the respective entry is fully
353	 * mapped and specified and its type allocable. A driver could
354	 * have added an out of range resource on its own.
355	 */
356	if (!passthrough) {
357		if ((rle = resource_list_find(rl, type, *rid)) == NULL)
358			return (NULL);
359		base = limit = 0;
360		switch (type) {
361		case SYS_RES_MEMORY:
362			if (isa_mem_bt == NULL)
363				return (NULL);
364			base = isa_mem_base;
365			limit = base + isa_mem_limit;
366			break;
367		case SYS_RES_IOPORT:
368			if (isa_io_bt == NULL)
369				return (NULL);
370			base = isa_io_base;
371			limit = base + isa_io_limit;
372			break;
373		case SYS_RES_IRQ:
374			if (rle->start != rle->end || rle->start <= 7)
375				return (NULL);
376			break;
377		case SYS_RES_DRQ:
378			break;
379		default:
380			return (NULL);
381		}
382		if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
383			if (!INRANGE(rle->start, base, limit) ||
384			    !INRANGE(rle->end, base, limit))
385				return (NULL);
386		}
387	}
388
389	return (resource_list_alloc(rl, bus, child, type, rid, start, end,
390	    count, flags));
391}
392
393int
394isa_release_resource(device_t bus, device_t child, int type, int rid,
395		     struct resource *res)
396{
397
398	return (bus_generic_rl_release_resource(bus, child, type, rid, res));
399}
400
401int
402isa_setup_intr(device_t dev, device_t child,
403	       struct resource *irq, int flags,
404	       driver_intr_t *intr, void *arg, void **cookiep)
405{
406
407	/*
408	 * Just pass through. This is going to be handled by either
409	 * one of the parent PCI buses or the nexus device.
410	 * The interrupt had been routed before it was added to the
411	 * resource list of the child.
412	 */
413	return (BUS_SETUP_INTR(device_get_parent(dev), child, irq, flags, intr,
414	    arg, cookiep));
415}
416
417int
418isa_teardown_intr(device_t dev, device_t child,
419		  struct resource *irq, void *cookie)
420{
421
422	return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie));
423}
424