isa.c revision 173121
186232Stmm/*-
286232Stmm * Copyright (c) 1998 Doug Rabson
386232Stmm * Copyright (c) 2001 Thomas Moestl <tmm@FreeBSD.org>
486232Stmm * All rights reserved.
586232Stmm *
686232Stmm * Redistribution and use in source and binary forms, with or without
786232Stmm * modification, are permitted provided that the following conditions
886232Stmm * are met:
986232Stmm * 1. Redistributions of source code must retain the above copyright
1086232Stmm *    notice, this list of conditions and the following disclaimer.
1186232Stmm * 2. Redistributions in binary form must reproduce the above copyright
1286232Stmm *    notice, this list of conditions and the following disclaimer in the
1386232Stmm *    documentation and/or other materials provided with the distribution.
1486232Stmm *
1586232Stmm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1686232Stmm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1786232Stmm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1886232Stmm * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1986232Stmm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2086232Stmm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2186232Stmm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2286232Stmm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2386232Stmm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2486232Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2586232Stmm * SUCH DAMAGE.
2686232Stmm *
2786232Stmm *	from: FreeBSD: src/sys/alpha/isa/isa.c,v 1.26 2001/07/11
2886232Stmm */
2986232Stmm
30137819Smarius#include <sys/cdefs.h>
31137819Smarius__FBSDID("$FreeBSD: head/sys/sparc64/isa/isa.c 173121 2007-10-28 22:08:37Z marius $");
32137819Smarius
3386232Stmm#include <sys/param.h>
3486232Stmm#include <sys/systm.h>
3586232Stmm#include <sys/bus.h>
36129051Smarius
3786232Stmm#include <machine/bus.h>
38129051Smarius
3986232Stmm#include <sys/rman.h>
4086232Stmm
4186232Stmm#include <isa/isareg.h>
4286232Stmm#include <isa/isavar.h>
4386232Stmm#include <isa/isa_common.h>
4486232Stmm
45133589Smarius#include <dev/ofw/ofw_bus.h>
46119338Simp#include <dev/ofw/openfirm.h>
4786232Stmm
4886232Stmm#include <machine/resource.h>
4986232Stmm
50129051Smarius#include <dev/pci/pcireg.h>
51129051Smarius#include <dev/pci/pcivar.h>
52129051Smarius
5386232Stmm#include <sparc64/pci/ofw_pci.h>
5486232Stmm#include <sparc64/isa/ofw_isa.h>
5586232Stmm
5686232Stmm/* There can be only one ISA bus, so it is safe to use globals. */
57129051Smariusstatic u_int64_t isa_io_base;
58129051Smariusstatic u_int64_t isa_io_limit;
59129051Smariusstatic u_int64_t isa_mem_base;
60129051Smariusstatic u_int64_t isa_mem_limit;
6186232Stmm
6286232Stmmdevice_t isa_bus_device;
6386232Stmm
6486232Stmmstatic phandle_t isab_node;
65137819Smariusstatic struct isa_ranges *isab_ranges;
66137819Smariusstatic int isab_nrange;
67117119Stmmstatic ofw_pci_intr_t isa_ino[8];
68137819Smariusstatic struct ofw_bus_iinfo isa_iinfo;
69117119Stmm
7086232Stmm/*
71137819Smarius * XXX: This is really partly PCI-specific, but unfortunately is
7286232Stmm * differently enough to have to duplicate it here...
7386232Stmm */
7486232Stmm#define	ISAB_RANGE_PHYS(r)						\
7586232Stmm	(((u_int64_t)(r)->phys_mid << 32) | (u_int64_t)(r)->phys_lo)
7686232Stmm#define	ISAB_RANGE_SPACE(r)	(((r)->phys_hi >> 24) & 0x03)
7786232Stmm
7886232Stmm#define	ISAR_SPACE_IO		0x01
7986232Stmm#define	ISAR_SPACE_MEM		0x02
8086232Stmm
8186232Stmm#define INRANGE(x, start, end)	((x) >= (start) && (x) <= (end))
8286232Stmm
83137819Smariusstatic void	isa_setup_children(device_t, phandle_t);
8486232Stmm
8586232Stmmintrmask_t
8686232Stmmisa_irq_pending(void)
8786232Stmm{
8886232Stmm	intrmask_t pending;
8986232Stmm	int i;
9086232Stmm
9186232Stmm	/* XXX: Is this correct? */
9286232Stmm	for (i = 7, pending = 0; i >= 0; i--) {
93117119Stmm		pending <<= 1;
94117119Stmm		if (isa_ino[i] != PCI_INVALID_IRQ) {
95117119Stmm			pending |= (OFW_PCI_INTR_PENDING(isa_bus_device,
9686232Stmm			    isa_ino[i]) == 0) ? 0 : 1;
9786232Stmm		}
9886232Stmm	}
9986232Stmm	return (pending);
10086232Stmm}
10186232Stmm
10286232Stmmvoid
10386232Stmmisa_init(device_t dev)
10486232Stmm{
10586232Stmm	device_t bridge;
106137819Smarius	int i;
10786232Stmm
10886232Stmm	/* The parent of the bus must be a PCI-ISA bridge. */
10986232Stmm	bridge = device_get_parent(dev);
110133589Smarius	isab_node = ofw_bus_get_node(bridge);
111137819Smarius	isab_nrange = OF_getprop_alloc(isab_node, "ranges",
112137819Smarius	    sizeof(*isab_ranges), (void **)&isab_ranges);
113137819Smarius	if (isab_nrange <= 0)
11486232Stmm		panic("isa_init: cannot get bridge range property");
115117119Stmm
116117119Stmm	ofw_bus_setup_iinfo(isab_node, &isa_iinfo, sizeof(ofw_isa_intr_t));
117117119Stmm
11893067Stmm	/*
119117119Stmm	 * This is really a bad kludge; however, it is needed to provide
120117119Stmm	 * isa_irq_pending(), which is unfortunately still used by some
121117119Stmm	 * drivers.
122137819Smarius	 * XXX:	The only driver still using isa_irq_pending() is sio(4)
123137819Smarius	 *	which we don't use on sparc64. Should we just drop support
124137819Smarius	 *	for isa_irq_pending()?
12593067Stmm	 */
12693067Stmm	for (i = 0; i < 8; i++)
127117119Stmm		isa_ino[i] = PCI_INVALID_IRQ;
12886232Stmm
129137819Smarius	isa_setup_children(dev, isab_node);
130137819Smarius
131137819Smarius	for (i = isab_nrange - 1; i >= 0; i--) {
132137819Smarius		switch(ISAB_RANGE_SPACE(&isab_ranges[i])) {
13386232Stmm		case ISAR_SPACE_IO:
13486232Stmm			/* This is probably always 0. */
135137819Smarius			isa_io_base = ISAB_RANGE_PHYS(&isab_ranges[i]);
136137819Smarius			isa_io_limit = isab_ranges[i].size;
13786232Stmm			break;
13886232Stmm		case ISAR_SPACE_MEM:
13986232Stmm			/* This is probably always 0. */
140137819Smarius			isa_mem_base = ISAB_RANGE_PHYS(&isab_ranges[i]);
141137819Smarius			isa_mem_limit = isab_ranges[i].size;
14286232Stmm			break;
14386232Stmm		}
14486232Stmm	}
14586232Stmm}
14686232Stmm
147159289Smariusstatic const struct {
148137819Smarius	const char	*name;
149137819Smarius	uint32_t	id;
150146407Smarius} ofw_isa_pnp_map[] = {
151137819Smarius	{ "SUNW,lomh",	0x0000ae4e }, /* SUN0000 */
152137819Smarius	{ "dma",	0x0002d041 }, /* PNP0200 */
153137819Smarius	{ "floppy",	0x0007d041 }, /* PNP0700 */
154137819Smarius	{ "rtc",	0x000bd041 }, /* PNP0B00 */
155137819Smarius	{ "flashprom",	0x0100ae4e }, /* SUN0001 */
156137819Smarius	{ "parallel",	0x0104d041 }, /* PNP0401 */
157137819Smarius	{ "serial",	0x0105d041 }, /* PNP0501 */
158159289Smarius	{ "i2c",	0x0200ae4e }, /* SUN0002 */
159159289Smarius	{ "rmc-comm",	0x0300ae4e }, /* SUN0003 */
160137819Smarius	{ "kb_ps2",	0x0303d041 }, /* PNP0303 */
161137819Smarius	{ "kdmouse",	0x030fd041 }, /* PNP0F03 */
162137819Smarius	{ "power",	0x0c0cd041 }, /* PNP0C0C */
163137819Smarius	{ NULL,		0x0 }
164137819Smarius};
165137819Smarius
166137819Smariusstatic void
167137819Smariusisa_setup_children(device_t dev, phandle_t parent)
16886232Stmm{
169137819Smarius	struct isa_regs *regs;
170146407Smarius	struct resource_list *rl;
171137819Smarius	device_t cdev;
172137819Smarius	u_int64_t end, start;
173137819Smarius	ofw_isa_intr_t *intrs, rintr;
174137819Smarius	phandle_t node;
175137819Smarius	uint32_t *drqs, *regidx;
176146407Smarius	int i, ndrq, nintr, nreg, nregidx, rid, rtype;
177137819Smarius	char *name;
17886232Stmm
179137819Smarius	/*
180137819Smarius	 * Loop through children and fake up PnP devices for them.
181137819Smarius	 * Their resources are added as fully mapped and specified because
182137819Smarius	 * adjusting the resources and the resource list entries respectively
183137819Smarius	 * in isa_alloc_resource() causes trouble with drivers which use
184137819Smarius	 * rman_get_start(), pass-through or allocate and release resources
185137819Smarius	 * multiple times, etc. Adjusting the resources might be better off
186137819Smarius	 * in a bus_activate_resource method but the common ISA code doesn't
187137819Smarius	 * allow for an isa_activate_resource().
188137819Smarius	 */
189137819Smarius	for (node = OF_child(parent); node != 0; node = OF_peer(node)) {
190137819Smarius		if ((OF_getprop_alloc(node, "name", 1, (void **)&name)) == -1)
191137819Smarius			continue;
192137819Smarius
193137819Smarius		/*
194137819Smarius		 * Keyboard and mouse controllers hang off of the `8042'
195137819Smarius		 * node but we have no real use for the `8042' itself.
196137819Smarius		 */
197137819Smarius		if (strcmp(name, "8042") == 0) {
198137819Smarius			isa_setup_children(dev, node);
199137819Smarius			free(name, M_OFWPROP);
200137819Smarius			continue;
201137819Smarius		}
202137819Smarius
203146407Smarius		for (i = 0; ofw_isa_pnp_map[i].name != NULL; i++)
204146407Smarius			if (strcmp(ofw_isa_pnp_map[i].name, name) == 0)
205137819Smarius				break;
206146407Smarius		if (ofw_isa_pnp_map[i].name == NULL) {
207173121Smarius			device_printf(dev, "no PnP map entry for node "
208137819Smarius			    "0x%lx: %s\n", (unsigned long)node, name);
209159289Smarius			free(name, M_OFWPROP);
210137819Smarius			continue;
211137819Smarius		}
212137819Smarius
213138506Simp		if ((cdev = BUS_ADD_CHILD(dev, ISA_ORDER_PNPBIOS, NULL, -1)) ==
214137819Smarius		    NULL)
215137819Smarius			panic("isa_setup_children: BUS_ADD_CHILD failed");
216146407Smarius		isa_set_logicalid(cdev, ofw_isa_pnp_map[i].id);
217146407Smarius		isa_set_vendorid(cdev, ofw_isa_pnp_map[i].id);
218137819Smarius
219146407Smarius		rl = BUS_GET_RESOURCE_LIST(dev, cdev);
220137819Smarius		nreg = OF_getprop_alloc(node, "reg", sizeof(*regs),
221137819Smarius		    (void **)&regs);
222137819Smarius		for (i = 0; i < nreg; i++) {
223137819Smarius			start = ISA_REG_PHYS(&regs[i]);
224137819Smarius			end = start + regs[i].size - 1;
225137819Smarius			rtype = ofw_isa_range_map(isab_ranges, isab_nrange,
226137819Smarius			    &start, &end, NULL);
227146407Smarius			rid = 0;
228146407Smarius			while (resource_list_find(rl, rtype, rid) != NULL)
229146407Smarius				rid++;
230146407Smarius			bus_set_resource(cdev, rtype, rid, start,
231137819Smarius			    end - start + 1);
232137819Smarius		}
233137819Smarius		if (nreg == -1 && parent != isab_node) {
234137819Smarius			/*
235137819Smarius			 * The "reg" property still might be an index into
236137819Smarius			 * the set of registers of the parent device like
237137819Smarius			 * with the nodes hanging off of the `8042' node.
238137819Smarius			 */
239137819Smarius			nregidx = OF_getprop_alloc(node, "reg", sizeof(*regidx),
240137819Smarius			    (void **)&regidx);
241137819Smarius			if (nregidx > 2)
242137819Smarius				panic("isa_setup_children: impossible number "
243137819Smarius				    "of register indices");
244137819Smarius			if (nregidx != -1 && (nreg = OF_getprop_alloc(parent,
245137819Smarius			    "reg", sizeof(*regs), (void **)&regs)) >= nregidx) {
246137819Smarius				for (i = 0; i < nregidx; i++) {
247137819Smarius					start = ISA_REG_PHYS(&regs[regidx[i]]);
248137819Smarius					end = start + regs[regidx[i]].size - 1;
249137819Smarius					rtype = ofw_isa_range_map(isab_ranges,
250137819Smarius					    isab_nrange, &start, &end, NULL);
251146407Smarius					rid = 0;
252146407Smarius					while (resource_list_find(rl, rtype,
253146407Smarius					    rid) != NULL)
254146407Smarius						rid++;
255146407Smarius					bus_set_resource(cdev, rtype, rid,
256146407Smarius					    start, end - start + 1);
257137819Smarius				}
258137819Smarius			}
259137819Smarius			if (regidx != NULL)
260137819Smarius				free(regidx, M_OFWPROP);
261137819Smarius		}
262137819Smarius		if (regs != NULL)
263137819Smarius			free(regs, M_OFWPROP);
264137819Smarius
265137819Smarius		nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intrs),
266137819Smarius		    (void **)&intrs);
267137819Smarius		for (i = 0; i < nintr; i++) {
268137819Smarius			if (intrs[i] > 7)
269137819Smarius				panic("isa_setup_children: intr too large");
270137819Smarius			rintr = ofw_isa_route_intr(device_get_parent(dev), node,
271137819Smarius			    &isa_iinfo, intrs[i]);
272173121Smarius			if (rintr == PCI_INVALID_IRQ) {
273173121Smarius				device_printf(dev, "could not map ISA "
274173121Smarius				    "interrupt %d for node 0x%lx: %s\n",
275173121Smarius				    intrs[i], (unsigned long)node, name);
276173121Smarius				continue;
277173121Smarius			}
278137819Smarius			isa_ino[intrs[i]] = rintr;
279137819Smarius			bus_set_resource(cdev, SYS_RES_IRQ, i, rintr, 1);
280137819Smarius		}
281137819Smarius		if (intrs != NULL)
282137819Smarius			free(intrs, M_OFWPROP);
283137819Smarius
284137819Smarius		ndrq = OF_getprop_alloc(node, "dma-channel", sizeof(*drqs),
285137819Smarius		    (void **)&drqs);
286137819Smarius		for (i = 0; i < ndrq; i++)
287137819Smarius			bus_set_resource(cdev, SYS_RES_DRQ, i, drqs[i], 1);
288137819Smarius		if (drqs != NULL)
289137819Smarius			free(drqs, M_OFWPROP);
290137819Smarius
291137819Smarius		/*
292137819Smarius		 * Devices using DMA hang off of the `dma' node instead of
293137819Smarius		 * directly from the ISA bridge node.
294137819Smarius		 */
295137819Smarius		if (strcmp(name, "dma") == 0)
296137819Smarius			isa_setup_children(dev, node);
297137819Smarius
298137819Smarius		free(name, M_OFWPROP);
29986232Stmm	}
30086232Stmm}
30186232Stmm
30286232Stmmstruct resource *
30386232Stmmisa_alloc_resource(device_t bus, device_t child, int type, int *rid,
304173121Smarius    u_long start, u_long end, u_long count, u_int flags)
30586232Stmm{
30686232Stmm	/*
307127146Sjmg	 * Consider adding a resource definition.
30886232Stmm	 */
30986232Stmm	int passthrough = (device_get_parent(child) != bus);
31086232Stmm	int isdefault = (start == 0UL && end == ~0UL);
311146407Smarius	struct resource_list *rl;
31286232Stmm	struct resource_list_entry *rle;
31386232Stmm	u_long base, limit;
31486232Stmm
315146407Smarius	rl = BUS_GET_RESOURCE_LIST(bus, child);
31686232Stmm	if (!passthrough && !isdefault) {
31786232Stmm		rle = resource_list_find(rl, type, *rid);
31886232Stmm		if (!rle) {
31986232Stmm			if (*rid < 0)
320146407Smarius				return (NULL);
32186232Stmm			switch (type) {
32286232Stmm			case SYS_RES_IRQ:
32386232Stmm				if (*rid >= ISA_NIRQ)
324146407Smarius					return (NULL);
32586232Stmm				break;
32686232Stmm			case SYS_RES_DRQ:
32786232Stmm				if (*rid >= ISA_NDRQ)
328146407Smarius					return (NULL);
32986232Stmm				break;
33086232Stmm			case SYS_RES_MEMORY:
33186232Stmm				if (*rid >= ISA_NMEM)
332146407Smarius					return (NULL);
33386232Stmm				break;
33486232Stmm			case SYS_RES_IOPORT:
33586232Stmm				if (*rid >= ISA_NPORT)
336146407Smarius					return (NULL);
33786232Stmm				break;
33886232Stmm			default:
339146407Smarius				return (NULL);
34086232Stmm			}
34186232Stmm			resource_list_add(rl, type, *rid, start, end, count);
34286232Stmm		}
34386232Stmm	}
34486232Stmm
34586232Stmm	/*
346137819Smarius	 * Sanity check if the resource in the respective entry is fully
347137819Smarius	 * mapped and specified and its type allocable. A driver could
348137819Smarius	 * have added an out of range resource on its own.
34986232Stmm	 */
350137819Smarius	if (!passthrough) {
351137819Smarius		if ((rle = resource_list_find(rl, type, *rid)) == NULL)
35286232Stmm			return (NULL);
353137819Smarius		base = limit = 0;
35486232Stmm		switch (type) {
35586232Stmm		case SYS_RES_MEMORY:
356137819Smarius			base = isa_mem_base;
357137819Smarius			limit = base + isa_mem_limit;
358137819Smarius			break;
35986232Stmm		case SYS_RES_IOPORT:
360137819Smarius			base = isa_io_base;
361137819Smarius			limit = base + isa_io_limit;
36286232Stmm			break;
36386232Stmm		case SYS_RES_IRQ:
364137819Smarius			if (rle->start != rle->end || rle->start <= 7)
36586232Stmm				return (NULL);
36686232Stmm			break;
367137819Smarius		case SYS_RES_DRQ:
368137819Smarius			break;
369137819Smarius		default:
370137819Smarius			return (NULL);
37186232Stmm		}
372137819Smarius		if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
373137819Smarius			if (!INRANGE(rle->start, base, limit) ||
374137819Smarius			    !INRANGE(rle->end, base, limit))
375137819Smarius				return (NULL);
376137819Smarius		}
37786232Stmm	}
37886232Stmm
379137819Smarius	return (resource_list_alloc(rl, bus, child, type, rid, start, end,
380137819Smarius	    count, flags));
38186232Stmm}
38286232Stmm
38386232Stmmint
38486232Stmmisa_release_resource(device_t bus, device_t child, int type, int rid,
385173121Smarius    struct resource *res)
38686232Stmm{
38786232Stmm
388146407Smarius	return (bus_generic_rl_release_resource(bus, child, type, rid, res));
38986232Stmm}
39086232Stmm
39186232Stmmint
392173121Smariusisa_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
393173121Smarius    driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
39486232Stmm{
39586232Stmm
39686232Stmm	/*
397146407Smarius	 * Just pass through. This is going to be handled by either
398146407Smarius	 * one of the parent PCI buses or the nexus device.
399146407Smarius	 * The interrupt had been routed before it was added to the
400146407Smarius	 * resource list of the child.
40186232Stmm	 */
402173121Smarius	return (bus_generic_setup_intr(dev, child, irq, flags, filter, intr,
403173121Smarius	    arg, cookiep));
40486232Stmm}
40586232Stmm
40686232Stmmint
407173121Smariusisa_teardown_intr(device_t dev, device_t child, struct resource *irq,
408173121Smarius    void *cookie)
40986232Stmm{
41086232Stmm
411173121Smarius	return (bus_generic_teardown_intr(dev, child, irq, cookie));
41286232Stmm}
413