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