1178173Simp/*	$NetBSD: obio.c,v 1.11 2003/07/15 00:25:05 lukem Exp $	*/
2178173Simp
3178173Simp/*-
4178173Simp * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
5178173Simp * All rights reserved.
6178173Simp *
7178173Simp * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8178173Simp *
9178173Simp * Redistribution and use in source and binary forms, with or without
10178173Simp * modification, are permitted provided that the following conditions
11178173Simp * are met:
12178173Simp * 1. Redistributions of source code must retain the above copyright
13178173Simp *    notice, this list of conditions and the following disclaimer.
14178173Simp * 2. Redistributions in binary form must reproduce the above copyright
15178173Simp *    notice, this list of conditions and the following disclaimer in the
16178173Simp *    documentation and/or other materials provided with the distribution.
17178173Simp * 3. All advertising materials mentioning features or use of this software
18178173Simp *    must display the following acknowledgement:
19178173Simp *	This product includes software developed for the NetBSD Project by
20178173Simp *	Wasabi Systems, Inc.
21178173Simp * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22178173Simp *    or promote products derived from this software without specific prior
23178173Simp *    written permission.
24178173Simp *
25178173Simp * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26178173Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27178173Simp * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28178173Simp * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29178173Simp * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30178173Simp * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31178173Simp * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32178173Simp * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33178173Simp * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34178173Simp * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35178173Simp * POSSIBILITY OF SUCH DAMAGE.
36178173Simp */
37178173Simp
38178173Simp/*
39178173Simp * On-board device autoconfiguration support for Broadcom Sentry5
40178173Simp * based boards.
41178173Simp * XXX This is totally bogus and is just enough to get the console hopefully
42178173Simp * running on the sentry5.
43178173Simp */
44178173Simp
45178173Simp#include <sys/cdefs.h>
46178173Simp__FBSDID("$FreeBSD$");
47178173Simp
48178173Simp#include <sys/param.h>
49178173Simp#include <sys/systm.h>
50178173Simp#include <sys/bus.h>
51178173Simp#include <sys/kernel.h>
52178173Simp#include <sys/module.h>
53178173Simp#include <sys/rman.h>
54178173Simp#include <sys/malloc.h>
55178173Simp
56178173Simp#include <machine/bus.h>
57178173Simp
58182901Sgonzo#include <mips/sentry5/obiovar.h>
59182901Sgonzo#include <mips/sentry5/sentry5reg.h>
60178173Simp
61178173Simpint	obio_probe(device_t);
62178173Simpint	obio_attach(device_t);
63178173Simp
64178173Simp/*
65178173Simp * A bit tricky and hackish. Since we need OBIO to rely
66178173Simp * on PCI we make it pseudo-pci device. But there should
67178173Simp * be only one such device, so we use this static flag
68178173Simp * to prevent false positives on every realPCI device probe.
69178173Simp */
70178173Simpstatic int have_one = 0;
71178173Simp
72178173Simpint
73178173Simpobio_probe(device_t dev)
74178173Simp{
75202036Simp	if (!have_one) {
76178173Simp		have_one = 1;
77178173Simp		return 0;
78178173Simp	}
79202036Simp	return (ENXIO);
80178173Simp}
81178173Simp
82178173Simpint
83178173Simpobio_attach(device_t dev)
84178173Simp{
85178173Simp	struct obio_softc *sc = device_get_softc(dev);
86178173Simp
87178173Simp	sc->oba_st = MIPS_BUS_SPACE_IO;
88178173Simp	sc->oba_addr = MIPS_PHYS_TO_KSEG1(SENTRY5_UART1ADR);
89178173Simp	sc->oba_size = 0x03FFFFFF;	/* XXX sb pci bus 0 aperture size? */
90178173Simp	sc->oba_rman.rm_type = RMAN_ARRAY;
91178173Simp	sc->oba_rman.rm_descr = "OBIO I/O";
92178173Simp	if (rman_init(&sc->oba_rman) != 0 ||
93178173Simp	    rman_manage_region(&sc->oba_rman,
94178173Simp	    sc->oba_addr, sc->oba_addr + sc->oba_size) != 0)
95178173Simp		panic("obio_attach: failed to set up I/O rman");
96178173Simp	sc->oba_irq_rman.rm_type = RMAN_ARRAY;
97178173Simp	sc->oba_irq_rman.rm_descr = "OBIO IRQ";
98178173Simp
99178173Simp	/*
100178173Simp	 * This module is intended for UART purposes only and
101178173Simp	 * it's IRQ is 4
102178173Simp	 */
103178173Simp	if (rman_init(&sc->oba_irq_rman) != 0 ||
104178173Simp	    rman_manage_region(&sc->oba_irq_rman, 4, 4) != 0)
105178173Simp		panic("obio_attach: failed to set up IRQ rman");
106178173Simp
107178173Simp	device_add_child(dev, "uart", 0);
108178173Simp	bus_generic_probe(dev);
109178173Simp	bus_generic_attach(dev);
110178173Simp
111178173Simp	return (0);
112178173Simp}
113178173Simp
114178173Simpstatic struct resource *
115178173Simpobio_alloc_resource(device_t bus, device_t child, int type, int *rid,
116178173Simp    u_long start, u_long end, u_long count, u_int flags)
117178173Simp{
118178173Simp	struct resource *rv;
119178173Simp	struct rman *rm;
120178173Simp	bus_space_handle_t bh = 0;
121178173Simp	struct obio_softc *sc = device_get_softc(bus);
122178173Simp
123178173Simp	switch (type) {
124178173Simp	case SYS_RES_IRQ:
125178173Simp		rm = &sc->oba_irq_rman;
126178173Simp		break;
127178173Simp	case SYS_RES_MEMORY:
128178173Simp		return (NULL);
129178173Simp	case SYS_RES_IOPORT:
130178173Simp		rm = &sc->oba_rman;
131178173Simp		bh = sc->oba_addr;
132178173Simp		start = bh;
133178173Simp		break;
134178173Simp	default:
135178173Simp		return (NULL);
136178173Simp	}
137178173Simp
138178173Simp
139178173Simp	rv = rman_reserve_resource(rm, start, end, count, flags, child);
140178173Simp	if (rv == NULL)
141178173Simp		return (NULL);
142178173Simp	if (type == SYS_RES_IRQ)
143178173Simp		return (rv);
144178173Simp	rman_set_rid(rv, *rid);
145202036Simp	rman_set_bustag(rv, mips_bus_space_generic);
146178173Simp	rman_set_bushandle(rv, bh);
147178173Simp
148178173Simp	if (0) {
149178173Simp		if (bus_activate_resource(child, type, *rid, rv)) {
150178173Simp			rman_release_resource(rv);
151178173Simp			return (NULL);
152178173Simp		}
153178173Simp	}
154178173Simp	return (rv);
155178173Simp
156178173Simp}
157178173Simp
158178173Simpstatic int
159178173Simpobio_activate_resource(device_t bus, device_t child, int type, int rid,
160178173Simp    struct resource *r)
161178173Simp{
162178173Simp	return (0);
163178173Simp}
164178173Simpstatic device_method_t obio_methods[] = {
165178173Simp	DEVMETHOD(device_probe, obio_probe),
166178173Simp	DEVMETHOD(device_attach, obio_attach),
167178173Simp
168178173Simp	DEVMETHOD(bus_alloc_resource, obio_alloc_resource),
169178173Simp	DEVMETHOD(bus_activate_resource, obio_activate_resource),
170178173Simp	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
171178173Simp	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
172178173Simp
173178173Simp	{0, 0},
174178173Simp};
175178173Simp
176178173Simpstatic driver_t obio_driver = {
177178173Simp	"obio",
178178173Simp	obio_methods,
179178173Simp	sizeof(struct obio_softc),
180178173Simp};
181178173Simpstatic devclass_t obio_devclass;
182178173Simp
183178173SimpDRIVER_MODULE(obio, pci, obio_driver, obio_devclass, 0, 0);
184