iodi.c revision 208165
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 208165 2010-05-16 19:43:48Z rrs $");
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, driver_intr_t * intr, void *arg,
95    void **cookiep)
96{
97	int level;
98	xlr_reg_t *mmio = xlr_io_mmio(XLR_IO_PIC_OFFSET);
99	xlr_reg_t reg;
100
101	/* FIXME is this the right place to fiddle with PIC? */
102	if (strcmp(device_get_name(child), "uart") == 0) {
103		/* FIXME uart 1? */
104		if (rmi_spin_mutex_safe)
105			mtx_lock_spin(&xlr_pic_lock);
106		level = PIC_IRQ_IS_EDGE_TRIGGERED(PIC_IRT_UART_0_INDEX);
107		xlr_write_reg(mmio, PIC_IRT_0_UART_0, 0x01);
108		xlr_write_reg(mmio, PIC_IRT_1_UART_0, ((1 << 31) | (level << 30) | (1 << 6) | (PIC_UART_0_IRQ)));
109		if (rmi_spin_mutex_safe)
110			mtx_unlock_spin(&xlr_pic_lock);
111		cpu_establish_hardintr("uart", filt,
112		    (driver_intr_t *) intr, (void *)arg, PIC_UART_0_IRQ, flags, cookiep);
113
114	} else if (strcmp(device_get_name(child), "rge") == 0) {
115		int irq;
116
117		/* This is a hack to pass in the irq */
118		irq = (int)ires->__r_i;
119		if (rmi_spin_mutex_safe)
120			mtx_lock_spin(&xlr_pic_lock);
121		reg = xlr_read_reg(mmio, PIC_IRT_1_BASE + irq - PIC_IRQ_BASE);
122		xlr_write_reg(mmio, PIC_IRT_1_BASE + irq - PIC_IRQ_BASE, reg | (1 << 6) | (1 << 30) | (1 << 31));
123		if (rmi_spin_mutex_safe)
124			mtx_unlock_spin(&xlr_pic_lock);
125		cpu_establish_hardintr("rge", filt, (driver_intr_t *) intr, (void *)arg, irq, flags, cookiep);
126
127	} else if (strcmp(device_get_name(child), "ehci") == 0) {
128		if (rmi_spin_mutex_safe)
129			mtx_lock_spin(&xlr_pic_lock);
130		reg = xlr_read_reg(mmio, PIC_IRT_1_BASE + PIC_USB_IRQ - PIC_IRQ_BASE);
131		xlr_write_reg(mmio, PIC_IRT_1_BASE + PIC_USB_IRQ - PIC_IRQ_BASE, reg | (1 << 6) | (1 << 30) | (1 << 31));
132		if (rmi_spin_mutex_safe)
133			mtx_unlock_spin(&xlr_pic_lock);
134		cpu_establish_hardintr("ehci", filt, (driver_intr_t *) intr, (void *)arg, PIC_USB_IRQ, flags, cookiep);
135	}
136	/*
137	 * This causes a panic and looks recursive to me (RRS).
138	 * BUS_SETUP_INTR(device_get_parent(dev), child, ires, flags, filt,
139	 * intr, arg, cookiep);
140	 */
141
142	return (0);
143}
144
145static struct resource *
146iodi_alloc_resource(device_t bus, device_t child, int type, int *rid,
147    u_long start, u_long end, u_long count, u_int flags)
148{
149	struct resource *res = malloc(sizeof(*res), M_DEVBUF, M_WAITOK);
150	int unit;
151
152#ifdef DEBUG
153	switch (type) {
154	case SYS_RES_IRQ:
155		device_printf(bus, "IRQ resource - for %s %lx-%lx\n",
156		    device_get_nameunit(child), start, end);
157		break;
158
159	case SYS_RES_IOPORT:
160		device_printf(bus, "IOPORT resource - for %s %lx-%lx\n",
161		    device_get_nameunit(child), start, end);
162		break;
163
164	case SYS_RES_MEMORY:
165		device_printf(bus, "MEMORY resource - for %s %lx-%lx\n",
166		    device_get_nameunit(child), start, end);
167		break;
168	}
169#endif
170
171	if (strcmp(device_get_name(child), "uart") == 0) {
172		if ((unit = device_get_unit(child)) == 0) {	/* uart 0 */
173			res->r_bushandle = (xlr_io_base + XLR_IO_UART_0_OFFSET);
174		} else if (unit == 1) {
175			res->r_bushandle = (xlr_io_base + XLR_IO_UART_1_OFFSET);
176		} else
177			printf("%s: Unknown uart unit\n", __FUNCTION__);
178
179		res->r_bustag = uart_bus_space_mem;
180	} else if (strcmp(device_get_name(child), "ehci") == 0) {
181		res->r_bushandle = 0xbef24000;
182		res->r_bustag = rmi_pci_bus_space;
183	} else if (strcmp(device_get_name(child), "cfi") == 0) {
184		res->r_bushandle = 0xbc000000;
185		res->r_bustag = 0;
186	}
187	/* res->r_start = *rid; */
188	return (res);
189}
190
191static int
192iodi_activate_resource(device_t bus, device_t child, int type, int rid,
193    struct resource *r)
194{
195	return (0);
196}
197
198/* prototypes */
199static int iodi_probe(device_t);
200static int iodi_attach(device_t);
201static void iodi_identify(driver_t *, device_t);
202
203int
204iodi_probe(device_t dev)
205{
206	return 0;
207}
208
209void
210iodi_identify(driver_t * driver, device_t parent)
211{
212
213	BUS_ADD_CHILD(parent, 0, "iodi", 0);
214}
215
216int
217iodi_attach(device_t dev)
218{
219	device_t tmpd;
220
221	/*
222	 * Attach each devices
223	 */
224	device_add_child(dev, "uart", 0);
225	device_add_child(dev, "xlr_i2c", 0);
226	device_add_child(dev, "pcib", 0);
227
228	if (xlr_board_info.usb)
229		device_add_child(dev, "ehci", 0);
230
231	if (xlr_board_info.cfi)
232		device_add_child(dev, "cfi", 0);
233
234	if (xlr_board_info.gmac_block[0].enabled) {
235		tmpd = device_add_child(dev, "rge", 0);
236		device_set_ivars(tmpd, &xlr_board_info.gmac_block[0]);
237
238		tmpd = device_add_child(dev, "rge", 1);
239		device_set_ivars(tmpd, &xlr_board_info.gmac_block[0]);
240
241		tmpd = device_add_child(dev, "rge", 2);
242		device_set_ivars(tmpd, &xlr_board_info.gmac_block[0]);
243
244		tmpd = device_add_child(dev, "rge", 3);
245		device_set_ivars(tmpd, &xlr_board_info.gmac_block[0]);
246	}
247	if (xlr_board_info.gmac_block[1].enabled) {
248		if (xlr_board_info.gmac_block[1].type == XLR_GMAC) {
249			tmpd = device_add_child(dev, "rge", 4);
250			device_set_ivars(tmpd, &xlr_board_info.gmac_block[1]);
251
252			tmpd = device_add_child(dev, "rge", 5);
253			device_set_ivars(tmpd, &xlr_board_info.gmac_block[1]);
254
255			if (xlr_board_info.gmac_block[1].enabled & 0x4) {
256				tmpd = device_add_child(dev, "rge", 6);
257				device_set_ivars(tmpd, &xlr_board_info.gmac_block[1]);
258			}
259
260			if (xlr_board_info.gmac_block[1].enabled & 0x8) {
261				tmpd = device_add_child(dev, "rge", 7);
262				device_set_ivars(tmpd, &xlr_board_info.gmac_block[1]);
263			}
264		} else if (xlr_board_info.gmac_block[1].type == XLR_XGMAC) {
265#if 0				/* XGMAC not yet */
266			tmpd = device_add_child(dev, "rge", 4);
267			device_set_ivars(tmpd, &xlr_board_info.gmac_block[1]);
268
269			tmpd = device_add_child(dev, "rge", 5);
270			device_set_ivars(tmpd, &xlr_board_info.gmac_block[1]);
271#endif
272		} else
273			device_printf(dev, "Unknown type of gmac 1\n");
274	}
275	bus_generic_probe(dev);
276	bus_generic_attach(dev);
277	return 0;
278}
279
280static device_method_t iodi_methods[] = {
281	DEVMETHOD(device_probe, iodi_probe),
282	DEVMETHOD(device_attach, iodi_attach),
283	DEVMETHOD(device_identify, iodi_identify),
284	DEVMETHOD(bus_alloc_resource, iodi_alloc_resource),
285	DEVMETHOD(bus_activate_resource, iodi_activate_resource),
286	DEVMETHOD(bus_setup_intr, iodi_setup_intr),
287	{0, 0},
288};
289
290static driver_t iodi_driver = {
291	"iodi",
292	iodi_methods,
293	1			/* no softc */
294};
295static devclass_t iodi_devclass;
296
297DRIVER_MODULE(iodi, nexus, iodi_driver, iodi_devclass, 0, 0);
298