150397Sobrien/*	$NetBSD: obio.c,v 1.11 2003/07/15 00:25:05 lukem Exp $	*/
2132718Skan
350397Sobrien/*-
4132718Skan * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
550397Sobrien * All rights reserved.
6132718Skan *
750397Sobrien * Written by Jason R. Thorpe for Wasabi Systems, Inc.
850397Sobrien *
950397Sobrien * Redistribution and use in source and binary forms, with or without
1050397Sobrien * modification, are permitted provided that the following conditions
11132718Skan * are met:
1250397Sobrien * 1. Redistributions of source code must retain the above copyright
1350397Sobrien *    notice, this list of conditions and the following disclaimer.
1450397Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1550397Sobrien *    notice, this list of conditions and the following disclaimer in the
1650397Sobrien *    documentation and/or other materials provided with the distribution.
17132718Skan * 3. All advertising materials mentioning features or use of this software
18169689Skan *    must display the following acknowledgement:
19169689Skan *	This product includes software developed for the NetBSD Project by
2050397Sobrien *	Wasabi Systems, Inc.
2150397Sobrien * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22117395Skan *    or promote products derived from this software without specific prior
2350397Sobrien *    written permission.
2452284Sobrien *
2590075Sobrien * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
2690075Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2752284Sobrien * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2852284Sobrien * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29117395Skan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30117395Skan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31117395Skan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32117395Skan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33117395Skan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34117395Skan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35117395Skan * POSSIBILITY OF SUCH DAMAGE.
36117395Skan */
37117395Skan
38117395Skan/*
3950397Sobrien * On-board device autoconfiguration support for Intel IQ80321
4050397Sobrien * evaluation boards.
4150397Sobrien */
4250397Sobrien
4350397Sobrien#include <sys/cdefs.h>
4450397Sobrien__FBSDID("$FreeBSD: releng/11.0/sys/mips/malta/obio.c 294883 2016-01-27 02:23:54Z jhibbits $");
4550397Sobrien
4650397Sobrien#include <sys/param.h>
4750397Sobrien#include <sys/systm.h>
4850397Sobrien#include <sys/bus.h>
4950397Sobrien#include <sys/kernel.h>
5050397Sobrien#include <sys/module.h>
5150397Sobrien#include <sys/rman.h>
5250397Sobrien#include <sys/malloc.h>
5350397Sobrien
5450397Sobrien#include <machine/bus.h>
5550397Sobrien
5650397Sobrien#include <mips/malta/maltareg.h>
5750397Sobrien#include <mips/malta/obiovar.h>
5850397Sobrien
5950397Sobrienint	obio_probe(device_t);
6050397Sobrienint	obio_attach(device_t);
6150397Sobrien
6250397Sobrien/*
6350397Sobrien * A bit tricky and hackish. Since we need OBIO to rely
6450397Sobrien * on PCI we make it pseudo-pci device. But there should
6550397Sobrien * be only one such device, so we use this static flag
6650397Sobrien * to prevent false positives on every real PCI device probe.
6750397Sobrien */
6850397Sobrienstatic int have_one = 0;
6950397Sobrien
7050397Sobrienint
7150397Sobrienobio_probe(device_t dev)
7250397Sobrien{
7350397Sobrien	if (!have_one) {
7450397Sobrien		have_one = 1;
7550397Sobrien		return 0;
7650397Sobrien	}
7750397Sobrien	return (ENXIO);
7850397Sobrien}
7950397Sobrien
8050397Sobrienint
8150397Sobrienobio_attach(device_t dev)
8250397Sobrien{
8350397Sobrien	struct obio_softc *sc = device_get_softc(dev);
8450397Sobrien
8550397Sobrien	sc->oba_st = mips_bus_space_generic;
8650397Sobrien	sc->oba_addr = MIPS_PHYS_TO_KSEG1(MALTA_UART0ADR);
8750397Sobrien	sc->oba_size = MALTA_PCIMEM3_SIZE;
8850397Sobrien	sc->oba_rman.rm_type = RMAN_ARRAY;
8950397Sobrien	sc->oba_rman.rm_descr = "OBIO I/O";
9050397Sobrien	if (rman_init(&sc->oba_rman) != 0 ||
9150397Sobrien	    rman_manage_region(&sc->oba_rman,
9250397Sobrien	    sc->oba_addr, sc->oba_addr + sc->oba_size) != 0)
93132718Skan		panic("obio_attach: failed to set up I/O rman");
9450397Sobrien	sc->oba_irq_rman.rm_type = RMAN_ARRAY;
9550397Sobrien	sc->oba_irq_rman.rm_descr = "OBIO IRQ";
9690075Sobrien
9790075Sobrien	/*
9890075Sobrien	 * This module is intended for UART purposes only and
9990075Sobrien	 * it's IRQ is 4
10090075Sobrien	 */
101102780Skan	if (rman_init(&sc->oba_irq_rman) != 0 ||
102102780Skan	    rman_manage_region(&sc->oba_irq_rman, 4, 4) != 0)
103		panic("obio_attach: failed to set up IRQ rman");
104
105	device_add_child(dev, "uart", 0);
106	bus_generic_probe(dev);
107	bus_generic_attach(dev);
108
109	return (0);
110}
111
112static struct resource *
113obio_alloc_resource(device_t bus, device_t child, int type, int *rid,
114    rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
115{
116	struct resource *rv;
117	struct rman *rm;
118	bus_space_tag_t bt = 0;
119	bus_space_handle_t bh = 0;
120	struct obio_softc *sc = device_get_softc(bus);
121
122	switch (type) {
123	case SYS_RES_IRQ:
124		rm = &sc->oba_irq_rman;
125		break;
126	case SYS_RES_MEMORY:
127		return (NULL);
128	case SYS_RES_IOPORT:
129		rm = &sc->oba_rman;
130		bt = sc->oba_st;
131		bh = sc->oba_addr;
132		start = bh;
133		break;
134	default:
135		return (NULL);
136	}
137
138
139	rv = rman_reserve_resource(rm, start, end, count, flags, child);
140	if (rv == NULL)
141		return (NULL);
142	if (type == SYS_RES_IRQ)
143		return (rv);
144	rman_set_rid(rv, *rid);
145	rman_set_bustag(rv, bt);
146	rman_set_bushandle(rv, bh);
147
148	if (0) {
149		if (bus_activate_resource(child, type, *rid, rv)) {
150			rman_release_resource(rv);
151			return (NULL);
152		}
153	}
154	return (rv);
155
156}
157
158static int
159obio_activate_resource(device_t bus, device_t child, int type, int rid,
160    struct resource *r)
161{
162	return (0);
163}
164static device_method_t obio_methods[] = {
165	DEVMETHOD(device_probe, obio_probe),
166	DEVMETHOD(device_attach, obio_attach),
167
168	DEVMETHOD(bus_alloc_resource, obio_alloc_resource),
169	DEVMETHOD(bus_activate_resource, obio_activate_resource),
170	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
171	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
172
173	{0, 0},
174};
175
176static driver_t obio_driver = {
177	"obio",
178	obio_methods,
179	sizeof(struct obio_softc),
180};
181static devclass_t obio_devclass;
182
183DRIVER_MODULE(obio, pci, obio_driver, obio_devclass, 0, 0);
184