iodi.c revision 198956
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", NULL,
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		irq = rman_get_rid(ires);
129   	    if (rmi_spin_mutex_safe)
130		  mtx_lock_spin(&xlr_pic_lock);
131		reg = xlr_read_reg(mmio, PIC_IRT_1_BASE + irq - PIC_IRQ_BASE);
132		xlr_write_reg(mmio, PIC_IRT_1_BASE + irq - PIC_IRQ_BASE, reg | (1 << 6) | (1 << 30) | (1 << 31));
133   	    if (rmi_spin_mutex_safe)
134		  mtx_unlock_spin(&xlr_pic_lock);
135		cpu_establish_hardintr("rge", NULL, (driver_intr_t *) intr, (void *)arg, irq, flags, cookiep);
136
137	} else if (strcmp(device_get_name(child), "ehci") == 0) {
138   	    if (rmi_spin_mutex_safe)
139		mtx_lock_spin(&xlr_pic_lock);
140		reg = xlr_read_reg(mmio, PIC_IRT_1_BASE + PIC_USB_IRQ - PIC_IRQ_BASE);
141		xlr_write_reg(mmio, PIC_IRT_1_BASE + PIC_USB_IRQ - PIC_IRQ_BASE, reg | (1 << 6) | (1 << 30) | (1 << 31));
142   	    if (rmi_spin_mutex_safe)
143		mtx_unlock_spin(&xlr_pic_lock);
144		cpu_establish_hardintr("ehci", NULL, (driver_intr_t *) intr, (void *)arg, PIC_USB_IRQ, flags, cookiep);
145	}
146	BUS_SETUP_INTR(device_get_parent(dev),
147	    child, ires, flags, filt, intr, arg, cookiep);
148
149
150	return (0);
151}
152
153/* Strange hook found in mips/include/bus.h */
154#ifndef MIPS_BUS_SPACE_PCI
155#define MIPS_BUS_SPACE_PCI	10
156#endif
157
158static struct resource *
159iodi_alloc_resource(device_t bus, device_t child, int type, int *rid,
160    u_long start, u_long end, u_long count, u_int flags)
161{
162	struct resource *res = malloc(sizeof(*res), M_DEVBUF, M_WAITOK);
163	int unit;
164
165#ifdef DEBUG
166	switch (type) {
167	case SYS_RES_IRQ:
168		device_printf(bus, "IRQ resource - for %s %lx-%lx\n",
169		    device_get_nameunit(child), start, end);
170		break;
171
172	case SYS_RES_IOPORT:
173		device_printf(bus, "IOPORT resource - for %s %lx-%lx\n",
174		    device_get_nameunit(child), start, end);
175		break;
176
177	case SYS_RES_MEMORY:
178		device_printf(bus, "MEMORY resource - for %s %lx-%lx\n",
179		    device_get_nameunit(child), start, end);
180		break;
181	}
182#endif
183
184	if (strcmp(device_get_name(child), "uart") == 0) {
185		if ((unit = device_get_unit(child)) == 0) {	/* uart 0 */
186			res->r_bushandle = (xlr_io_base + XLR_IO_UART_0_OFFSET);
187		} else if (unit == 1) {
188			res->r_bushandle = (xlr_io_base + XLR_IO_UART_1_OFFSET);
189		} else
190			printf("%s: Unknown uart unit\n", __FUNCTION__);
191
192		res->r_bustag = uart_bus_space_mem;
193	} else if (strcmp(device_get_name(child), "ehci") == 0) {
194		res->r_bushandle = 0xbef24000;
195		res->r_bustag = (bus_space_tag_t) MIPS_BUS_SPACE_PCI;
196	} else if (strcmp(device_get_name(child), "cfi") == 0) {
197		res->r_bushandle = 0xbc000000;
198		res->r_bustag = 0;
199	}
200	/* res->r_start = *rid; */
201	return (res);
202}
203
204static int
205iodi_activate_resource(device_t bus, device_t child, int type, int rid,
206    struct resource *r)
207{
208	return (0);
209}
210
211/* prototypes */
212static int iodi_probe(device_t);
213static int iodi_attach(device_t);
214static void iodi_identify(driver_t *, device_t);
215
216int
217iodi_probe(device_t dev)
218{
219	return 0;
220}
221
222void
223iodi_identify(driver_t * driver, device_t parent)
224{
225
226	BUS_ADD_CHILD(parent, 0, "iodi", 0);
227}
228
229int
230iodi_attach(device_t dev)
231{
232	device_t tmpd;
233
234	/*
235	 * Attach each devices
236	 */
237	device_add_child(dev, "uart", 0);
238	device_add_child(dev, "xlr_i2c", 0);
239
240	if (xlr_board_info.usb)
241		device_add_child(dev, "ehci", 0);
242
243	if (xlr_board_info.cfi)
244		device_add_child(dev, "cfi", 0);
245
246	if (xlr_board_info.gmac_block[0].enabled) {
247		tmpd = device_add_child(dev, "rge", 0);
248		device_set_ivars(tmpd, &xlr_board_info.gmac_block[0]);
249
250		tmpd = device_add_child(dev, "rge", 1);
251		device_set_ivars(tmpd, &xlr_board_info.gmac_block[0]);
252
253		tmpd = device_add_child(dev, "rge", 2);
254		device_set_ivars(tmpd, &xlr_board_info.gmac_block[0]);
255
256		tmpd = device_add_child(dev, "rge", 3);
257		device_set_ivars(tmpd, &xlr_board_info.gmac_block[0]);
258	}
259	if (xlr_board_info.gmac_block[1].enabled) {
260		if (xlr_board_info.gmac_block[1].type == XLR_GMAC) {
261			tmpd = device_add_child(dev, "rge", 4);
262			device_set_ivars(tmpd, &xlr_board_info.gmac_block[1]);
263
264			tmpd = device_add_child(dev, "rge", 5);
265			device_set_ivars(tmpd, &xlr_board_info.gmac_block[1]);
266
267			tmpd = device_add_child(dev, "rge", 6);
268			device_set_ivars(tmpd, &xlr_board_info.gmac_block[1]);
269
270			tmpd = device_add_child(dev, "rge", 7);
271			device_set_ivars(tmpd, &xlr_board_info.gmac_block[1]);
272		} else if (xlr_board_info.gmac_block[1].type == XLR_XGMAC) {
273#if 0				/* XGMAC not yet */
274			tmpd = device_add_child(dev, "rge", 4);
275			device_set_ivars(tmpd, &xlr_board_info.gmac_block[1]);
276
277			tmpd = device_add_child(dev, "rge", 5);
278			device_set_ivars(tmpd, &xlr_board_info.gmac_block[1]);
279#endif
280		} else
281			device_printf(dev, "Unknown type of gmac 1\n");
282	}
283	bus_generic_probe(dev);
284	bus_generic_attach(dev);
285	return 0;
286}
287
288static device_method_t iodi_methods[] = {
289	DEVMETHOD(device_probe, iodi_probe),
290	DEVMETHOD(device_attach, iodi_attach),
291	DEVMETHOD(device_identify, iodi_identify),
292	DEVMETHOD(bus_alloc_resource, iodi_alloc_resource),
293	DEVMETHOD(bus_activate_resource, iodi_activate_resource),
294	DEVMETHOD(bus_setup_intr, iodi_setup_intr),
295	{0, 0},
296};
297
298static driver_t iodi_driver = {
299	"iodi",
300	iodi_methods,
301	1			/* no softc */
302};
303static devclass_t iodi_devclass;
304
305DRIVER_MODULE(iodi, nexus, iodi_driver, iodi_devclass, 0, 0);
306