obio.c revision 202175
1239278Sgonzo/*	$NetBSD: obio.c,v 1.11 2003/07/15 00:25:05 lukem Exp $	*/
2239278Sgonzo
3239278Sgonzo/*-
4239278Sgonzo * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
5239278Sgonzo * All rights reserved.
6239278Sgonzo *
7239278Sgonzo * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8239278Sgonzo *
9239278Sgonzo * Redistribution and use in source and binary forms, with or without
10239278Sgonzo * modification, are permitted provided that the following conditions
11239278Sgonzo * are met:
12239278Sgonzo * 1. Redistributions of source code must retain the above copyright
13239278Sgonzo *    notice, this list of conditions and the following disclaimer.
14239278Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
15239278Sgonzo *    notice, this list of conditions and the following disclaimer in the
16239278Sgonzo *    documentation and/or other materials provided with the distribution.
17239278Sgonzo * 3. All advertising materials mentioning features or use of this software
18239278Sgonzo *    must display the following acknowledgement:
19239278Sgonzo *	This product includes software developed for the NetBSD Project by
20239278Sgonzo *	Wasabi Systems, Inc.
21239278Sgonzo * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22239278Sgonzo *    or promote products derived from this software without specific prior
23239278Sgonzo *    written permission.
24239278Sgonzo *
25239278Sgonzo * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26239278Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27239278Sgonzo * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28239278Sgonzo * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29239278Sgonzo * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30239278Sgonzo * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31239278Sgonzo * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32239278Sgonzo * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33239278Sgonzo * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34239278Sgonzo * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35239278Sgonzo * POSSIBILITY OF SUCH DAMAGE.
36239278Sgonzo */
37239278Sgonzo
38239278Sgonzo/*
39239278Sgonzo * On-board device autoconfiguration support for Intel IQ80321
40239278Sgonzo * evaluation boards.
41239278Sgonzo */
42239278Sgonzo
43239278Sgonzo#include <sys/cdefs.h>
44239278Sgonzo__FBSDID("$FreeBSD: head/sys/mips/malta/obio.c 202175 2010-01-12 21:36:08Z imp $");
45239278Sgonzo
46239278Sgonzo#include <sys/param.h>
47239278Sgonzo#include <sys/systm.h>
48239278Sgonzo#include <sys/bus.h>
49239278Sgonzo#include <sys/kernel.h>
50239278Sgonzo#include <sys/module.h>
51239278Sgonzo#include <sys/rman.h>
52239278Sgonzo#include <sys/malloc.h>
53239278Sgonzo
54239278Sgonzo#include <machine/bus.h>
55239278Sgonzo
56239278Sgonzo#include <mips/malta/maltareg.h>
57239278Sgonzo#include <mips/malta/obiovar.h>
58239278Sgonzo
59239278Sgonzoint	obio_probe(device_t);
60239278Sgonzoint	obio_attach(device_t);
61239278Sgonzo
62239278Sgonzo/*
63239278Sgonzo * A bit tricky and hackish. Since we need OBIO to rely
64239278Sgonzo * on PCI we make it pseudo-pci device. But there should
65239278Sgonzo * be only one such device, so we use this static flag
66239278Sgonzo * to prevent false positives on every real PCI device probe.
67239278Sgonzo */
68239278Sgonzostatic int have_one = 0;
69239278Sgonzo
70239278Sgonzoint
71239278Sgonzoobio_probe(device_t dev)
72239278Sgonzo{
73239278Sgonzo	if (!have_one) {
74239278Sgonzo		have_one = 1;
75239278Sgonzo		return 0;
76239278Sgonzo	}
77239278Sgonzo	return (ENXIO);
78239278Sgonzo}
79239278Sgonzo
80239278Sgonzoint
81239278Sgonzoobio_attach(device_t dev)
82239278Sgonzo{
83239278Sgonzo	struct obio_softc *sc = device_get_softc(dev);
84239278Sgonzo
85239278Sgonzo	sc->oba_st = mips_bus_space_generic;
86239278Sgonzo	sc->oba_addr = MIPS_PHYS_TO_KSEG1(MALTA_UART0ADR);
87239278Sgonzo	sc->oba_size = MALTA_PCIMEM3_SIZE;
88266152Sian	sc->oba_rman.rm_type = RMAN_ARRAY;
89266152Sian	sc->oba_rman.rm_descr = "OBIO I/O";
90266152Sian	if (rman_init(&sc->oba_rman) != 0 ||
91266152Sian	    rman_manage_region(&sc->oba_rman,
92239278Sgonzo	    sc->oba_addr, sc->oba_addr + sc->oba_size) != 0)
93239278Sgonzo		panic("obio_attach: failed to set up I/O rman");
94239278Sgonzo	sc->oba_irq_rman.rm_type = RMAN_ARRAY;
95239278Sgonzo	sc->oba_irq_rman.rm_descr = "OBIO IRQ";
96239278Sgonzo
97239278Sgonzo	/*
98239278Sgonzo	 * This module is intended for UART purposes only and
99239278Sgonzo	 * it's IRQ is 4
100239278Sgonzo	 */
101239278Sgonzo	if (rman_init(&sc->oba_irq_rman) != 0 ||
102239278Sgonzo	    rman_manage_region(&sc->oba_irq_rman, 4, 4) != 0)
103239278Sgonzo		panic("obio_attach: failed to set up IRQ rman");
104239278Sgonzo
105239278Sgonzo	device_add_child(dev, "uart", 0);
106239278Sgonzo	bus_generic_probe(dev);
107239278Sgonzo	bus_generic_attach(dev);
108239278Sgonzo
109239278Sgonzo	return (0);
110239278Sgonzo}
111239278Sgonzo
112239278Sgonzostatic struct resource *
113239278Sgonzoobio_alloc_resource(device_t bus, device_t child, int type, int *rid,
114239278Sgonzo    u_long start, u_long end, u_long count, u_int flags)
115239278Sgonzo{
116239278Sgonzo	struct resource *rv;
117239278Sgonzo	struct rman *rm;
118239278Sgonzo	bus_space_tag_t bt = 0;
119239278Sgonzo	bus_space_handle_t bh = 0;
120239278Sgonzo	struct obio_softc *sc = device_get_softc(bus);
121239278Sgonzo
122239278Sgonzo	switch (type) {
123239278Sgonzo	case SYS_RES_IRQ:
124239278Sgonzo		rm = &sc->oba_irq_rman;
125239278Sgonzo		break;
126239278Sgonzo	case SYS_RES_MEMORY:
127239278Sgonzo		return (NULL);
128239278Sgonzo	case SYS_RES_IOPORT:
129239278Sgonzo		rm = &sc->oba_rman;
130239278Sgonzo		bt = sc->oba_st;
131239278Sgonzo		bh = sc->oba_addr;
132239278Sgonzo		start = bh;
133239278Sgonzo		break;
134239278Sgonzo	default:
135239278Sgonzo		return (NULL);
136239278Sgonzo	}
137239278Sgonzo
138239278Sgonzo
139239278Sgonzo	rv = rman_reserve_resource(rm, start, end, count, flags, child);
140239278Sgonzo	if (rv == NULL)
141239278Sgonzo		return (NULL);
142239278Sgonzo	if (type == SYS_RES_IRQ)
143239278Sgonzo		return (rv);
144239278Sgonzo	rman_set_rid(rv, *rid);
145239278Sgonzo	rman_set_bustag(rv, bt);
146239278Sgonzo	rman_set_bushandle(rv, bh);
147239278Sgonzo
148239278Sgonzo	if (0) {
149239278Sgonzo		if (bus_activate_resource(child, type, *rid, rv)) {
150239278Sgonzo			rman_release_resource(rv);
151239278Sgonzo			return (NULL);
152239278Sgonzo		}
153239278Sgonzo	}
154239278Sgonzo	return (rv);
155239278Sgonzo
156239278Sgonzo}
157239278Sgonzo
158239278Sgonzostatic int
159239278Sgonzoobio_activate_resource(device_t bus, device_t child, int type, int rid,
160239278Sgonzo    struct resource *r)
161239278Sgonzo{
162239278Sgonzo	return (0);
163239278Sgonzo}
164239278Sgonzostatic device_method_t obio_methods[] = {
165239278Sgonzo	DEVMETHOD(device_probe, obio_probe),
166239278Sgonzo	DEVMETHOD(device_attach, obio_attach),
167239278Sgonzo
168239278Sgonzo	DEVMETHOD(bus_alloc_resource, obio_alloc_resource),
169239278Sgonzo	DEVMETHOD(bus_activate_resource, obio_activate_resource),
170239278Sgonzo	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
171239278Sgonzo	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
172239278Sgonzo
173239278Sgonzo	{0, 0},
174239278Sgonzo};
175239278Sgonzo
176239278Sgonzostatic driver_t obio_driver = {
177239278Sgonzo	"obio",
178239278Sgonzo	obio_methods,
179239278Sgonzo	sizeof(struct obio_softc),
180239278Sgonzo};
181239278Sgonzostatic devclass_t obio_devclass;
182239278Sgonzo
183239278SgonzoDRIVER_MODULE(obio, pci, obio_driver, obio_devclass, 0, 0);
184239278Sgonzo