sa1111.c revision 1.21
1/*      $NetBSD: sa1111.c,v 1.21 2008/01/08 02:07:52 matt Exp $	*/
2
3/*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by IWAMOTO Toshihiro.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *        This product includes software developed by the NetBSD
21 *        Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 *    contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39/*
40 * TODO:
41 *   - introduce bus abstraction to support SA-1101
42 */
43
44#include <sys/cdefs.h>
45__KERNEL_RCSID(0, "$NetBSD: sa1111.c,v 1.21 2008/01/08 02:07:52 matt Exp $");
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/types.h>
50#include <sys/conf.h>
51#include <sys/device.h>
52#include <sys/kernel.h>
53#include <sys/malloc.h>
54#include <sys/uio.h>
55
56#include <machine/bus.h>
57#include <machine/intr.h>
58
59#include <arm/sa11x0/sa11x0_reg.h>
60#include <arm/sa11x0/sa11x0_var.h>
61#include <arm/sa11x0/sa11x0_gpioreg.h>
62#include <arm/sa11x0/sa1111_reg.h>
63#include <arm/sa11x0/sa1111_var.h>
64
65#include "locators.h"
66
67static int	sa1111_print(void *, const char *);
68
69static void	sacc_intr_calculatemasks(struct sacc_softc *);
70static void	sacc_intr_setpolarity(sacc_chipset_tag_t *, int , int);
71
72#ifdef INTR_DEBUG
73#define DPRINTF(arg)	printf arg
74#else
75#define DPRINTF(arg)
76#endif
77
78int
79sacc_probe(struct device *parent, struct cfdata *match, void *aux)
80{
81	struct sa11x0_attach_args *sa = aux;
82	bus_space_handle_t ioh;
83	uint32_t skid;
84
85	if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0, &ioh))
86		return 0;
87
88	skid = bus_space_read_4(sa->sa_iot, ioh, SACCSBI_SKID);
89	bus_space_unmap(sa->sa_iot, ioh, sa->sa_size);
90
91	if ((skid & 0xffffff00) != 0x690cc200)
92		return 0;
93
94	return 1;
95}
96
97
98int
99sa1111_search(struct device *parent, struct cfdata *cf, const int *ldesc,
100    void *aux)
101{
102	struct sa1111_attach_args aa;
103
104	aa.sa_addr = cf->cf_loc[SACCCF_ADDR];
105	aa.sa_size = cf->cf_loc[SACCCF_SIZE];
106	aa.sa_intr = cf->cf_loc[SACCCF_INTR];
107#if 0
108	aa.sa_membase = cf->cf_loc[SACCCF_MEMBASE];
109	aa.sa_memsize = cf->cf_loc[SACCCF_MEMSIZE];
110#endif
111
112        if (config_match(parent, cf, &aa) > 0)
113                config_attach(parent, cf, &aa, sa1111_print);
114
115        return 0;
116}
117
118static int
119sa1111_print(void *aux, const char *name)
120{
121
122	return UNCONF;
123}
124
125
126void *
127sacc_intr_establish(sacc_chipset_tag_t *ic, int irq, int type, int level,
128    int (*ih_fun)(void *), void *ih_arg)
129{
130	int s;
131	struct sacc_softc *sc = (struct sacc_softc *)ic;
132	struct sacc_intrhand **p, *ih;
133
134	/* no point in sleeping unless someone can free memory. */
135	ih = malloc(sizeof *ih, M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
136	if (ih == NULL)
137		panic("sacc_intr_establish: can't malloc handler info");
138
139	if (irq < 0 || irq > SACCIC_LEN ||
140	    !(type == IST_EDGE_RAISE || type == IST_EDGE_FALL))
141		panic("sacc_intr_establish: bogus irq or type");
142
143	if (sc->sc_intrhand[irq] == NULL) {
144		sacc_intr_setpolarity(ic, irq, type);
145		sc->sc_intrtype[irq] = type;
146	} else if (sc->sc_intrtype[irq] != type)
147		/* XXX we should be able to share raising and
148		 * falling edge intrs */
149		panic("sacc_intr_establish: type must be unique");
150
151	/* install intr handler */
152	/* map interrupt level to appropriate softinterrupt level */
153	level = SOFTINT_SERIAL;
154	ih->ih_soft = softint_establish(level, (void (*)(void *)) ih_fun,
155					 ih_arg);
156	ih->ih_irq = irq;
157	ih->ih_next = NULL;
158
159	s = splhigh();
160	for (p = &sc->sc_intrhand[irq]; *p; p = &(*p)->ih_next)
161		continue;
162
163	*p = ih;
164
165	sacc_intr_calculatemasks(sc);
166	splx(s);
167
168	return ih;
169}
170
171void
172sacc_intr_disestablish(sacc_chipset_tag_t *ic, void *arg)
173{
174	int irq, s;
175	struct sacc_softc *sc = (struct sacc_softc *)ic;
176	struct sacc_intrhand *ih, **p;
177
178	ih = (struct sacc_intrhand *)arg;
179	irq = ih->ih_irq;
180
181#ifdef DIAGNOSTIC
182	if (irq < 0 || irq > SACCIC_LEN)
183		panic("sacc_intr_disestablish: bogus irq");
184#endif
185
186	s = splhigh();
187
188	for (p = &sc->sc_intrhand[irq];; p = &(*p)->ih_next) {
189		if (*p == NULL)
190			panic("sacc_intr_disestablish: handler not registered");
191		if (*p == ih)
192			break;
193	}
194	*p = (*p)->ih_next;
195
196	sacc_intr_calculatemasks(sc);
197	splx(s);
198
199	free(ih, M_DEVBUF);
200}
201
202static void
203sacc_intr_setpolarity(sacc_chipset_tag_t *ic, int irq, int type)
204{
205	struct sacc_softc *sc = (struct sacc_softc *)ic;
206	int s;
207	uint32_t pol, mask;
208	int addr;
209
210	if (irq >= 32) {
211		addr = SACCIC_INTPOL1;
212		irq -= 32;
213	} else
214		addr = SACCIC_INTPOL0;
215
216	mask = (1 << irq);
217
218	s = splhigh();
219	pol = bus_space_read_4(sc->sc_iot, sc->sc_ioh, addr);
220	if (type == IST_EDGE_RAISE)
221		pol &= ~mask;
222	else
223		pol |= mask;
224	bus_space_write_4(sc->sc_iot, sc->sc_ioh, addr, pol);
225	splx(s);
226}
227
228static void
229sacc_intr_calculatemasks(struct sacc_softc *sc)
230{
231	int irq;
232
233	sc->sc_imask.lo = 0;
234	sc->sc_imask.hi = 0;
235	for (irq = 0; irq < 32; irq++)
236		if (sc->sc_intrhand[irq])
237			sc->sc_imask.lo |= (1 << irq);
238	for (irq = 0; irq < SACCIC_LEN - 32; irq++)
239		if (sc->sc_intrhand[irq + 32])
240			sc->sc_imask.hi |= (1 << irq);
241
242
243	/* XXX this should not be done here */
244	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACCIC_INTEN0,
245			  sc->sc_imask.lo);
246	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACCIC_INTEN1,
247			  sc->sc_imask.hi);
248	DPRINTF(("sacc_intr_calculatemasks: %x %x\n", sc->sc_imask.lo,
249	    sc->sc_imask.hi));
250}
251