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