sa1111.c revision 1.3
1/*      $NetBSD: sa1111.c,v 1.3 2001/09/24 14:29:30 takemura 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 *   - separate machine specific attach code
42 *   - introduce bus abstraction to support SA1101
43 */
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/types.h>
48#include <sys/conf.h>
49#include <sys/device.h>
50#include <sys/kernel.h>
51#include <sys/malloc.h>
52#include <sys/uio.h>
53
54#include <machine/bus.h>
55#ifdef hpcarm
56#include <machine/platid.h>
57#include <machine/platid_mask.h>
58#endif
59
60#include <arm/sa11x0/sa11x0_reg.h>
61#include <arm/sa11x0/sa11x0_var.h>
62#include <arm/sa11x0/sa11x0_gpioreg.h>
63#include <arm/sa11x0/sa1111_reg.h>
64#include <arm/sa11x0/sa1111_var.h>
65
66static	int	sacc_probe(struct device *, struct cfdata *, void *);
67static	void	sacc_attach(struct device *, struct device *, void *);
68static	int	sa1111_search(struct device *, struct cfdata *, void *);
69static	int	sa1111_print(void *, const char *);
70
71static void	sacc_intr_calculatemasks(struct sacc_softc *);
72static void	sacc_intr_setpolarity(sacc_chipset_tag_t *, int , int);
73int		sacc_intr(void *);
74
75#if hpcarm
76void *softintr_establish(int, int (*)(void *), void *);
77void softintr_schedule(void *);
78#endif
79
80#ifdef hpcarm
81struct platid_data sacc_platid_table[] = {
82	{ &platid_mask_MACH_HP_JORNADA_720, (void *)1 },
83	{ &platid_mask_MACH_HP_JORNADA_720JP, (void *)1 },
84	{ NULL, NULL }
85};
86#endif
87
88struct cfattach sacc_ca = {
89	sizeof(struct sacc_softc), sacc_probe, sacc_attach
90};
91
92#ifdef INTR_DEBUG
93#define DPRINTF(arg)	printf arg
94#else
95#define DPRINTF(arg)
96#endif
97
98static int
99sacc_probe(parent, match, aux)
100	struct device *parent;
101	struct cfdata *match;
102	void *aux;
103{
104	struct sa11x0_attach_args *sa = aux;
105	bus_space_handle_t ioh;
106	u_int32_t skid;
107
108	if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0, &ioh))
109		return (0);
110
111	skid = bus_space_read_4(sa->sa_iot, ioh, SACCSBI_SKID);
112	bus_space_unmap(sa->sa_iot, ioh, sa->sa_size);
113
114	if ((skid & 0xffffff00) != 0x690cc200)
115		return (0);
116
117	return (1);
118}
119
120static void
121sacc_attach(parent, self, aux)
122	struct device *parent;
123	struct device *self;
124	void *aux;
125{
126	int i, gpiopin;
127	u_int32_t skid;
128	struct sacc_softc *sc = (struct sacc_softc *)self;
129	struct sa11x0_softc *psc = (struct sa11x0_softc *)parent;
130	struct sa11x0_attach_args *sa = aux;
131#ifdef hpcarm
132	struct platid_data *p;
133#endif
134
135	printf("\n");
136
137	sc->sc_iot = sa->sa_iot;
138	sc->sc_piot = psc->sc_iot;
139	sc->sc_gpioh = psc->sc_gpioh;
140#ifdef hpcarm
141	if ((p = platid_search_data(&platid, sacc_platid_table)) == NULL)
142		return;
143
144	gpiopin = (int) p->data;
145#else
146	gpiopin = sa->sa_gpio;
147#endif
148	sc->sc_gpiomask = 1 << gpiopin;
149
150	if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0,
151			  &sc->sc_ioh)) {
152		printf("%s: unable to map registers\n", sc->sc_dev.dv_xname);
153		return;
154	}
155
156	skid = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACCSBI_SKID);
157
158	printf("%s: SA1111 rev %d.%d\n", sc->sc_dev.dv_xname,
159	       (skid & 0xf0) >> 3, skid & 0xf);
160
161	for(i = 0; i < SACCIC_LEN; i++)
162		sc->sc_intrhand[i] = NULL;
163
164	/* initialize SA1111 interrupt controller */
165	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACCIC_INTEN0, 0);
166	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACCIC_INTEN1, 0);
167	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACCIC_INTTSTSEL, 0);
168	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
169			  SACCIC_INTSTATCLR0, 0xffffffff);
170	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
171			  SACCIC_INTSTATCLR1, 0xffffffff);
172
173	/* connect to SA1110's GPIO intr */
174	sa11x0_intr_establish(0, gpiopin, 1, IPL_SERIAL, sacc_intr, sc);
175
176	/*
177	 *  Attach each devices
178	 */
179	config_search(sa1111_search, self, NULL);
180}
181
182static int
183sa1111_search(parent, cf, aux)
184	struct device *parent;
185	struct cfdata *cf;
186	void *aux;
187{
188        if ((*cf->cf_attach->ca_match)(parent, cf, NULL) > 0)
189                config_attach(parent, cf, NULL, sa1111_print);
190
191        return 0;
192}
193
194static int
195sa1111_print(aux, name)
196	void *aux;
197	const char *name;
198{
199	return (UNCONF);
200}
201
202int
203sacc_intr(arg)
204	void *arg;
205{
206	int i;
207	u_int32_t mask;
208	struct sacc_intrvec intstat;
209	struct sacc_softc *sc = arg;
210#ifdef hpcarm
211	struct sacc_intrhand *ih;
212#endif
213
214	intstat.lo =
215	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACCIC_INTSTATCLR0);
216	intstat.hi =
217	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACCIC_INTSTATCLR1);
218	DPRINTF(("sacc_intr_dispatch: %x %x\n", intstat.lo, intstat.hi));
219
220	for(i = 0, mask = 1; i < 32; i++, mask <<= 1)
221		if (intstat.lo & mask) {
222			/* clear SA1110's GPIO intr status */
223			bus_space_write_4(sc->sc_piot, sc->sc_gpioh,
224					  SAGPIO_EDR, sc->sc_gpiomask);
225			/*
226			 * Clear intr status before calling intr handlers.
227			 * This cause stray interrupts, but clearing
228			 * after calling intr handlers cause intr lossage.
229			 */
230			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
231					  SACCIC_INTSTATCLR0, 1 << i);
232
233#ifdef hpcarm
234			for(ih = sc->sc_intrhand[i]; ih; ih = ih->ih_next)
235				softintr_schedule(ih->ih_soft);
236#endif
237		}
238	for(i = 0, mask = 1; i < SACCIC_LEN - 32; i++, mask <<= 1)
239		if (intstat.hi & mask) {
240			/* clear SA1110's GPIO intr status */
241			bus_space_write_4(sc->sc_piot, sc->sc_gpioh,
242					  SAGPIO_EDR, sc->sc_gpiomask);
243			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
244					  SACCIC_INTSTATCLR1, 1 << i);
245#ifdef hpcarm
246			for(ih = sc->sc_intrhand[i + 32]; ih; ih = ih->ih_next)
247				softintr_schedule(ih->ih_soft);
248#endif
249		}
250	return 1;
251}
252
253void *
254sacc_intr_establish(ic, irq, type, level, ih_fun, ih_arg)
255	sacc_chipset_tag_t *ic;
256	int irq, type, level;
257	int (*ih_fun)(void *);
258	void *ih_arg;
259{
260	int s;
261	struct sacc_softc *sc = (struct sacc_softc *)ic;
262	struct sacc_intrhand **p, *ih;
263
264	/* no point in sleeping unless someone can free memory. */
265	ih = malloc(sizeof *ih, M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
266	if (ih == NULL)
267		panic("sacc_intr_establish: can't malloc handler info");
268
269	if (irq < 0 || irq > SACCIC_LEN ||
270	    ! (type == IST_EDGE_RAISE || type == IST_EDGE_FALL))
271		panic("sacc_intr_establish: bogus irq or type");
272
273	if (sc->sc_intrhand[irq] == NULL) {
274		sacc_intr_setpolarity(ic, irq, type);
275		sc->sc_intrtype[irq] = type;
276	} else if (sc->sc_intrtype[irq] != type)
277		/* XXX we should be able to share raising and
278		 * falling edge intrs */
279		panic("sacc_intr_establish: type must be unique\n");
280
281	/* install intr handler */
282#ifdef hpcarm
283	ih->ih_soft = softintr_establish(level, (void (*)(void *)) ih_fun,
284					 ih_arg);
285#endif
286	ih->ih_irq = irq;
287	ih->ih_next = NULL;
288
289	s = splhigh();
290	for(p = &sc->sc_intrhand[irq]; *p; p = &(*p)->ih_next)
291		;
292
293	*p = ih;
294
295	sacc_intr_calculatemasks(sc);
296	splx(s);
297
298	return(ih);
299}
300
301void
302sacc_intr_disestablish(ic, arg)
303	sacc_chipset_tag_t *ic;
304	void *arg;
305{
306	int irq, s;
307	struct sacc_softc *sc = (struct sacc_softc *)ic;
308	struct sacc_intrhand *ih, **p;
309
310	ih = (struct sacc_intrhand *)arg;
311	irq = ih->ih_irq;
312
313#ifdef DIAGNOSTIC
314	if (irq < 0 || irq > SACCIC_LEN)
315		panic("sacc_intr_disestablish: bogus irq");
316#endif
317
318	s = splhigh();
319
320	for(p = &sc->sc_intrhand[irq];; p = &(*p)->ih_next) {
321		if (*p == NULL)
322			panic("sacc_intr_disestablish: handler not registered");
323		if (*p == ih)
324			break;
325	}
326	*p = (*p)->ih_next;
327
328	sacc_intr_calculatemasks(sc);
329	splx(s);
330
331	free(ih, M_DEVBUF);
332}
333
334void
335sacc_intr_setpolarity(ic, irq, type)
336	sacc_chipset_tag_t *ic;
337	int irq;
338	int type;
339{
340	struct sacc_softc *sc = (struct sacc_softc *)ic;
341	int s;
342	u_int32_t pol, mask;
343	int addr;
344
345	if (irq >= 32) {
346		addr = SACCIC_INTPOL1;
347		irq -= 32;
348	} else
349		addr = SACCIC_INTPOL0;
350
351	mask = (1 << irq);
352
353	s = splhigh();
354	pol = bus_space_read_4(sc->sc_iot, sc->sc_ioh, addr);
355	if (type == IST_EDGE_RAISE)
356		pol &= ~mask;
357	else
358		pol |= mask;
359	bus_space_write_4(sc->sc_iot, sc->sc_ioh, addr, pol);
360	splx(s);
361}
362
363void
364sacc_intr_calculatemasks(sc)
365	struct sacc_softc *sc;
366{
367	int irq;
368
369	sc->sc_imask.lo = 0;
370	sc->sc_imask.hi = 0;
371	for(irq = 0; irq < 32; irq++)
372		if (sc->sc_intrhand[irq])
373			sc->sc_imask.lo |= (1 << irq);
374	for(irq = 0; irq < SACCIC_LEN - 32; irq++)
375		if (sc->sc_intrhand[irq + 32])
376			sc->sc_imask.hi |= (1 << irq);
377
378
379	/* XXX this should not be done here */
380	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACCIC_INTEN0,
381			  sc->sc_imask.lo);
382	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACCIC_INTEN1,
383			  sc->sc_imask.hi);
384	DPRINTF(("sacc_intr_calculatemasks: %x %x\n", sc->sc_imask.lo,
385	    sc->sc_imask.hi));
386}
387