1/*	$NetBSD: sa11x0.c,v 1.14 2003/07/15 00:24:50 lukem Exp $	*/
2
3/*-
4 * Copyright (c) 2001, The NetBSD Foundation, Inc.  All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by IWAMOTO Toshihiro and Ichiro FUKUHARA.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 */
18/*-
19 * Copyright (c) 1999
20 *         Shin Takemura and PocketBSD Project. All rights reserved.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 *    notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 *    notice, this list of conditions and the following disclaimer in the
29 *    documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 *    must display the following acknowledgement:
32 *	This product includes software developed by the PocketBSD project
33 *	and its contributors.
34 * 4. Neither the name of the project nor the names of its contributors
35 *    may be used to endorse or promote products derived from this software
36 *    without specific prior written permission.
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
39 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 *
50 */
51
52#include <sys/cdefs.h>
53__FBSDID("$FreeBSD$");
54
55#include <sys/param.h>
56#include <sys/systm.h>
57#include <sys/bus.h>
58#include <sys/kernel.h>
59#include <sys/reboot.h>
60#include <sys/malloc.h>
61#include <sys/interrupt.h>
62#include <sys/module.h>
63#include <sys/rman.h>
64
65#include <vm/vm.h>
66#include <vm/vm_extern.h>
67
68#include <machine/cpu.h>
69#include <machine/bus.h>
70#include <machine/intr.h>
71#include <arm/sa11x0/sa11x0_reg.h>
72#include <arm/sa11x0/sa11x0_var.h>
73#include <arm/sa11x0/sa11x0_dmacreg.h>
74#include <arm/sa11x0/sa11x0_ppcreg.h>
75#include <arm/sa11x0/sa11x0_gpioreg.h>
76
77extern void sa11x0_activateirqs(void);
78
79static struct resource *sa1110_alloc_resource(device_t, device_t, int, int *,
80        u_long, u_long, u_long, u_int);
81
82static int sa1110_activate_resource(device_t, device_t, int, int,
83        struct resource *);
84static int sa1110_setup_intr(device_t, device_t, struct resource *, int,
85        driver_filter_t *, driver_intr_t *, void *, void **);
86
87struct sa11x0_softc *sa11x0_softc; /* There can be only one. */
88
89static int
90sa1110_setup_intr(device_t dev, device_t child,
91        struct resource *ires,  int flags, driver_filter_t *filt,
92	driver_intr_t *intr, void *arg, void **cookiep)
93{
94	int saved_cpsr;
95
96	if (flags & INTR_TYPE_TTY)
97		rman_set_start(ires, 15);
98	else if (flags & INTR_TYPE_CLK) {
99		if (rman_get_start(ires) == 0)
100			rman_set_start(ires, 26);
101		else
102			rman_set_start(ires, 27);
103	}
104	saved_cpsr = SetCPSR(I32_bit, I32_bit);
105
106	SetCPSR(I32_bit, saved_cpsr & I32_bit);
107	BUS_SETUP_INTR(device_get_parent(dev), child, ires, flags, filt,
108	    intr, arg, cookiep);
109	return (0);
110}
111
112static struct resource *
113sa1110_alloc_resource(device_t bus, device_t child, int type, int *rid,
114        u_long start, u_long end, u_long count, u_int flags)
115{
116	struct resource *res;
117
118	res = rman_reserve_resource(&sa11x0_softc->sa11x0_rman, *rid, *rid,
119	    count, flags, child);
120	if (res != NULL)
121		rman_set_rid(res, *rid);
122
123	return (res);
124}
125static int
126sa1110_activate_resource(device_t bus, device_t child, int type, int rid,
127        struct resource *r)
128{
129	return (0);
130}
131/* prototypes */
132static int	sa11x0_probe(device_t);
133static int	sa11x0_attach(device_t);
134static void	sa11x0_identify(driver_t *, device_t);
135
136extern vm_offset_t saipic_base;
137
138
139int
140sa11x0_probe(device_t dev)
141{
142	return 0;
143}
144
145void
146sa11x0_identify(driver_t *driver, device_t parent)
147{
148
149	BUS_ADD_CHILD(parent, 0, "saip", 0);
150}
151
152int
153sa11x0_attach(device_t dev)
154{
155	struct sa11x0_softc *sc = device_get_softc(dev);
156	int unit = device_get_unit(dev);
157	sc->sc_iot = &sa11x0_bs_tag;
158
159	sa11x0_softc = sc;
160
161	/* Map the SAIP */
162
163	if (bus_space_map(sc->sc_iot, SAIPIC_BASE, SAIPIC_NPORTS,
164			0, &sc->sc_ioh))
165		panic("saip%d: Cannot map registers", unit);
166	saipic_base = sc->sc_ioh;
167
168	/* Map the GPIO registers */
169	if (bus_space_map(sc->sc_iot, SAGPIO_BASE, SAGPIO_NPORTS,
170			  0, &sc->sc_gpioh))
171		panic("saip%d: unable to map GPIO registers", unit);
172	bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_EDR, 0xffffffff);
173
174	/* Map the PPC registers */
175	if (bus_space_map(sc->sc_iot, SAPPC_BASE, SAPPC_NPORTS,
176			  0, &sc->sc_ppch))
177		panic("saip%d: unable to map PPC registers", unit);
178
179#if 0
180	/* Map the DMA controller registers */
181	if (bus_space_map(sc->sc_iot, SADMAC_BASE, SADMAC_NPORTS,
182			  0, &sc->sc_dmach))
183		panic("saip%d: unable to map DMAC registers", unit);
184#endif
185	/* Map the reset controller registers */
186	if (bus_space_map(sc->sc_iot, SARCR_BASE, PAGE_SIZE,
187			  0, &sc->sc_reseth))
188		panic("saip%d: unable to map reset registers", unit);
189	printf("\n");
190
191
192	/*
193	 *  Mask all interrupts.
194	 *  They are later unmasked at each device's attach routine.
195	 */
196	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAIPIC_MR, 0);
197
198	/* Route all bits to IRQ */
199	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAIPIC_LR, 0);
200
201	/* Exit idle mode only when unmasked intr is received */
202	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAIPIC_CR, 1);
203#if 0
204	/* disable all DMAC channels */
205	bus_space_write_4(sc->sc_iot, sc->sc_dmach, SADMAC_DCR0_CLR, 1);
206	bus_space_write_4(sc->sc_iot, sc->sc_dmach, SADMAC_DCR1_CLR, 1);
207	bus_space_write_4(sc->sc_iot, sc->sc_dmach, SADMAC_DCR2_CLR, 1);
208	bus_space_write_4(sc->sc_iot, sc->sc_dmach, SADMAC_DCR3_CLR, 1);
209	bus_space_write_4(sc->sc_iot, sc->sc_dmach, SADMAC_DCR4_CLR, 1);
210	bus_space_write_4(sc->sc_iot, sc->sc_dmach, SADMAC_DCR5_CLR, 1);
211#endif
212	/*
213	 * XXX this is probably a bad place, but intr bit shouldn't be
214	 * XXX enabled before intr mask is set.
215	 * XXX Having sane imask[] suffice??
216	 */
217#if 0
218	SetCPSR(I32_bit, 0);
219#endif
220	/*
221	 *  Attach each devices
222	 */
223	sc->sa11x0_rman.rm_type = RMAN_ARRAY;
224	sc->sa11x0_rman.rm_descr = "SA11x0 IRQs";
225	if (rman_init(&sc->sa11x0_rman) != 0 ||
226	    rman_manage_region(&sc->sa11x0_rman, 0, 32) != 0)
227		panic("sa11x0_attach: failed to set up rman");
228	device_add_child(dev, "uart", 0);
229	device_add_child(dev, "saost", 0);
230	bus_generic_probe(dev);
231	bus_generic_attach(dev);
232	sa11x0_activateirqs();
233	return (0);
234}
235
236static device_method_t saip_methods[] = {
237	DEVMETHOD(device_probe, sa11x0_probe),
238	DEVMETHOD(device_attach, sa11x0_attach),
239	DEVMETHOD(device_identify, sa11x0_identify),
240	DEVMETHOD(bus_alloc_resource, sa1110_alloc_resource),
241	DEVMETHOD(bus_activate_resource, sa1110_activate_resource),
242	DEVMETHOD(bus_setup_intr, sa1110_setup_intr),
243	{0, 0},
244};
245
246static driver_t saip_driver = {
247	"saip",
248	saip_methods,
249	sizeof(struct sa11x0_softc),
250};
251static devclass_t saip_devclass;
252
253DRIVER_MODULE(saip, nexus, saip_driver, saip_devclass, 0, 0);
254