iodi.c revision 211893
1/*-
2 * Copyright (c) 2003-2009 RMI Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of RMI Corporation, nor the names of its contributors,
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * RMI_BSD
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/mips/rmi/iodi.c 211893 2010-08-27 19:53:57Z jchandra $");
34
35#define __RMAN_RESOURCE_VISIBLE
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/bus.h>
39#include <sys/kernel.h>
40#include <sys/lock.h>
41#include <sys/mutex.h>
42#include <sys/reboot.h>
43#include <sys/types.h>
44#include <sys/malloc.h>
45#include <sys/bus.h>
46#include <sys/interrupt.h>
47#include <sys/module.h>
48
49#include <machine/cpu.h>
50#include <machine/bus.h>
51#include <machine/bus.h>
52#include <machine/intr_machdep.h>
53#include <mips/rmi/iomap.h>
54#include <mips/rmi/pic.h>
55#include <mips/rmi/shared_structs.h>
56#include <mips/rmi/board.h>
57#include <sys/rman.h>
58
59
60#include <machine/param.h>
61#include <machine/intr_machdep.h>
62#include <machine/clock.h>	/* for DELAY */
63#include <machine/bus.h>
64#include <machine/resource.h>
65#include <mips/rmi/interrupt.h>
66#include <mips/rmi/msgring.h>
67#include <mips/rmi/iomap.h>
68#include <mips/rmi/debug.h>
69#include <mips/rmi/pic.h>
70#include <mips/rmi/xlrconfig.h>
71#include <mips/rmi/shared_structs.h>
72#include <mips/rmi/board.h>
73
74#include <mips/rmi/dev/xlr/atx_cpld.h>
75#include <mips/rmi/dev/xlr/xgmac_mdio.h>
76
77extern bus_space_tag_t uart_bus_space_mem;
78
79static struct resource *
80iodi_alloc_resource(device_t, device_t, int, int *,
81    u_long, u_long, u_long, u_int);
82
83static int
84iodi_activate_resource(device_t, device_t, int, int,
85    struct resource *);
86static int
87iodi_setup_intr(device_t, device_t, struct resource *, int,
88    driver_filter_t *, driver_intr_t *, void *, void **);
89
90struct iodi_softc *iodi_softc;	/* There can be only one. */
91
92static int
93iodi_setup_intr(device_t dev, device_t child,
94    struct resource *ires, int flags, driver_filter_t * filt,
95    driver_intr_t *intr, void *arg, void **cookiep)
96{
97
98	if (strcmp(device_get_name(child), "uart") == 0) {
99		/* FIXME uart 1? */
100		cpu_establish_hardintr("uart", filt, intr, arg,
101		    PIC_UART_0_IRQ, flags, cookiep);
102		pic_setup_intr(PIC_IRT_UART_0_INDEX, PIC_UART_0_IRQ, 0x1, 0);
103	} else if (strcmp(device_get_name(child), "rge") == 0) {
104		int irq;
105
106		/* This is a hack to pass in the irq */
107		irq = (intptr_t)ires->__r_i;
108		cpu_establish_hardintr("rge", filt, intr, arg, irq, flags,
109		    cookiep);
110		pic_setup_intr(irq - PIC_IRQ_BASE, irq, 0x1, 0);
111	} else if (strcmp(device_get_name(child), "ehci") == 0) {
112		cpu_establish_hardintr("ehci", filt, intr, arg, PIC_USB_IRQ, flags,
113		    cookiep);
114		pic_setup_intr(PIC_USB_IRQ - PIC_IRQ_BASE, PIC_USB_IRQ, 0x1, 0);
115	}
116
117	return (0);
118}
119
120static struct resource *
121iodi_alloc_resource(device_t bus, device_t child, int type, int *rid,
122    u_long start, u_long end, u_long count, u_int flags)
123{
124	struct resource *res = malloc(sizeof(*res), M_DEVBUF, M_WAITOK);
125	int unit;
126
127#ifdef DEBUG
128	switch (type) {
129	case SYS_RES_IRQ:
130		device_printf(bus, "IRQ resource - for %s %lx-%lx\n",
131		    device_get_nameunit(child), start, end);
132		break;
133
134	case SYS_RES_IOPORT:
135		device_printf(bus, "IOPORT resource - for %s %lx-%lx\n",
136		    device_get_nameunit(child), start, end);
137		break;
138
139	case SYS_RES_MEMORY:
140		device_printf(bus, "MEMORY resource - for %s %lx-%lx\n",
141		    device_get_nameunit(child), start, end);
142		break;
143	}
144#endif
145
146	if (strcmp(device_get_name(child), "uart") == 0) {
147		if ((unit = device_get_unit(child)) == 0) {	/* uart 0 */
148			res->r_bushandle = (xlr_io_base + XLR_IO_UART_0_OFFSET);
149		} else if (unit == 1) {
150			res->r_bushandle = (xlr_io_base + XLR_IO_UART_1_OFFSET);
151		} else
152			printf("%s: Unknown uart unit\n", __FUNCTION__);
153
154		res->r_bustag = uart_bus_space_mem;
155	} else if (strcmp(device_get_name(child), "ehci") == 0) {
156		res->r_bushandle = MIPS_PHYS_TO_KSEG1(0x1ef24000);
157		res->r_bustag = rmi_pci_bus_space;
158	} else if (strcmp(device_get_name(child), "cfi") == 0) {
159		res->r_bushandle = MIPS_PHYS_TO_KSEG1(0x1c000000);
160		res->r_bustag = 0;
161	}
162	/* res->r_start = *rid; */
163	return (res);
164}
165
166static int
167iodi_activate_resource(device_t bus, device_t child, int type, int rid,
168    struct resource *r)
169{
170	return (0);
171}
172
173/* prototypes */
174static int iodi_probe(device_t);
175static int iodi_attach(device_t);
176static void iodi_identify(driver_t *, device_t);
177
178int
179iodi_probe(device_t dev)
180{
181	return 0;
182}
183
184void
185iodi_identify(driver_t * driver, device_t parent)
186{
187
188	BUS_ADD_CHILD(parent, 0, "iodi", 0);
189}
190
191int
192iodi_attach(device_t dev)
193{
194	device_t tmpd;
195
196	/*
197	 * Attach each devices
198	 */
199	device_add_child(dev, "uart", 0);
200	device_add_child(dev, "xlr_i2c", 0);
201	device_add_child(dev, "pcib", 0);
202
203	if (xlr_board_info.usb)
204		device_add_child(dev, "ehci", 0);
205
206	if (xlr_board_info.cfi)
207		device_add_child(dev, "cfi", 0);
208
209	if (xlr_board_info.gmac_block[0].enabled) {
210		tmpd = device_add_child(dev, "rge", 0);
211		device_set_ivars(tmpd, &xlr_board_info.gmac_block[0]);
212
213		tmpd = device_add_child(dev, "rge", 1);
214		device_set_ivars(tmpd, &xlr_board_info.gmac_block[0]);
215
216		tmpd = device_add_child(dev, "rge", 2);
217		device_set_ivars(tmpd, &xlr_board_info.gmac_block[0]);
218
219		tmpd = device_add_child(dev, "rge", 3);
220		device_set_ivars(tmpd, &xlr_board_info.gmac_block[0]);
221	}
222	if (xlr_board_info.gmac_block[1].enabled) {
223		if (xlr_board_info.gmac_block[1].type == XLR_GMAC) {
224			tmpd = device_add_child(dev, "rge", 4);
225			device_set_ivars(tmpd, &xlr_board_info.gmac_block[1]);
226
227			tmpd = device_add_child(dev, "rge", 5);
228			device_set_ivars(tmpd, &xlr_board_info.gmac_block[1]);
229
230			if (xlr_board_info.gmac_block[1].enabled & 0x4) {
231				tmpd = device_add_child(dev, "rge", 6);
232				device_set_ivars(tmpd, &xlr_board_info.gmac_block[1]);
233			}
234
235			if (xlr_board_info.gmac_block[1].enabled & 0x8) {
236				tmpd = device_add_child(dev, "rge", 7);
237				device_set_ivars(tmpd, &xlr_board_info.gmac_block[1]);
238			}
239		} else if (xlr_board_info.gmac_block[1].type == XLR_XGMAC) {
240#if 0				/* XGMAC not yet */
241			tmpd = device_add_child(dev, "rge", 4);
242			device_set_ivars(tmpd, &xlr_board_info.gmac_block[1]);
243
244			tmpd = device_add_child(dev, "rge", 5);
245			device_set_ivars(tmpd, &xlr_board_info.gmac_block[1]);
246#endif
247		} else
248			device_printf(dev, "Unknown type of gmac 1\n");
249	}
250	bus_generic_probe(dev);
251	bus_generic_attach(dev);
252	return 0;
253}
254
255static device_method_t iodi_methods[] = {
256	DEVMETHOD(device_probe, iodi_probe),
257	DEVMETHOD(device_attach, iodi_attach),
258	DEVMETHOD(device_identify, iodi_identify),
259	DEVMETHOD(bus_alloc_resource, iodi_alloc_resource),
260	DEVMETHOD(bus_activate_resource, iodi_activate_resource),
261	DEVMETHOD(bus_setup_intr, iodi_setup_intr),
262	{0, 0},
263};
264
265static driver_t iodi_driver = {
266	"iodi",
267	iodi_methods,
268	1			/* no softc */
269};
270static devclass_t iodi_devclass;
271
272DRIVER_MODULE(iodi, nexus, iodi_driver, iodi_devclass, 0, 0);
273