amps.c revision 1.3
1/*	$NetBSD: amps.c,v 1.3 2002/05/22 22:43:17 bjh21 Exp $	*/
2
3/*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Mark Brinicombe of Causality Limited.
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 * Card driver and probe and attach functions to use generic 16550 com
39 * driver for the Atomwide multiport serial podule
40 */
41
42/*
43 * Thanks to Martin Coulson, Atomwide, for providing the hardware
44 */
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/ioctl.h>
49#include <sys/select.h>
50#include <sys/tty.h>
51#include <sys/proc.h>
52#include <sys/user.h>
53#include <sys/conf.h>
54#include <sys/file.h>
55#include <sys/uio.h>
56#include <sys/kernel.h>
57#include <sys/syslog.h>
58#include <sys/types.h>
59#include <sys/device.h>
60
61#include <machine/intr.h>
62#include <machine/io.h>
63#include <machine/bus.h>
64#include <acorn32/podulebus/podulebus.h>
65#include <acorn32/podulebus/ampsreg.h>
66#include <dev/ic/comreg.h>
67#include <dev/ic/comvar.h>
68#include <dev/podulebus/podules.h>
69
70#include "locators.h"
71
72/*
73 * Atomwide Mulit-port serial podule device.
74 *
75 * This probes and attaches the top level multi-port serial device to the
76 * podulebus. It then configures the child com devices.
77 */
78
79/*
80 * Atomwide Multi-port serial card softc structure.
81 *
82 * Contains the device node, podule information and global information
83 * required by the driver such as the card version and the interrupt mask.
84 */
85
86struct amps_softc {
87	struct device		sc_dev;			/* device node */
88	podule_t 		*sc_podule;		/* Our podule info */
89	int 			sc_podule_number;	/* Our podule number */
90	bus_space_tag_t		sc_iot;			/* Bus tag */
91};
92
93int	amps_probe	__P((struct device *, struct cfdata *, void *));
94void	amps_attach	__P((struct device *, struct device *, void *));
95void	amps_shutdown	__P((void *arg));
96
97struct cfattach amps_ca = {
98	sizeof(struct amps_softc), amps_probe, amps_attach
99};
100
101/*
102 * Attach arguments for child devices.
103 * Pass the podule details, the parent softc and the channel
104 */
105
106struct amps_attach_args {
107	bus_space_tag_t aa_iot;			/* bus space tag */
108	unsigned int aa_base;			/* base address for port */
109	int aa_port;      			/* serial port number */
110	podulebus_intr_handle_t aa_irq;		/* IRQ number */
111};
112
113/*
114 * Define prototypes for custom bus space functions.
115 */
116
117/* Print function used during child config */
118
119int
120amps_print(aux, name)
121	void *aux;
122	const char *name;
123{
124	struct amps_attach_args *aa = aux;
125
126	if (!name)
127		printf(", port %d", aa->aa_port);
128
129	return(QUIET);
130}
131
132/*
133 * Card probe function
134 *
135 * Just match the manufacturer and podule ID's
136 */
137
138int
139amps_probe(parent, cf, aux)
140	struct device *parent;
141	struct cfdata *cf;
142	void *aux;
143{
144	struct podule_attach_args *pa = (void *)aux;
145
146	return (pa->pa_product == PODULE_ATOMWIDE_SERIAL);
147}
148
149/*
150 * Card attach function
151 *
152 * Identify the card version and configure any children.
153 * Install a shutdown handler to kill interrupts on shutdown
154 */
155
156void
157amps_attach(parent, self, aux)
158	struct device *parent, *self;
159	void *aux;
160{
161	struct amps_softc *sc = (void *)self;
162	struct podule_attach_args *pa = (void *)aux;
163	struct amps_attach_args aa;
164
165	/* Note the podule number and validate */
166
167	if (pa->pa_podule_number == -1)
168		panic("Podule has disappeared !");
169
170	sc->sc_podule_number = pa->pa_podule_number;
171	sc->sc_podule = pa->pa_podule;
172	podules[sc->sc_podule_number].attached = 1;
173
174	sc->sc_iot = pa->pa_iot;
175
176	/* Install a clean up handler to make sure IRQ's are disabled */
177/*	if (shutdownhook_establish(amps_shutdown, (void *)sc) == NULL)
178		panic("%s: Cannot install shutdown handler", self->dv_xname);*/
179
180	/* Set the interrupt info for this podule */
181
182/*	sc->sc_podule->irq_addr = pa->pa_podule->slow_base +;*/	/* XXX */
183/*	sc->sc_podule->irq_mask = ;*/
184
185	printf("\n");
186
187	/* Configure the children */
188
189	aa.aa_iot = sc->sc_iot;
190	aa.aa_base = pa->pa_podule->slow_base + AMPS_BASE_OFFSET;
191	aa.aa_base += MAX_AMPS_PORTS * AMPS_PORT_SPACING;
192	aa.aa_irq = pa->pa_podule->interrupt;
193	for (aa.aa_port = 0; aa.aa_port < MAX_AMPS_PORTS; ++aa.aa_port) {
194		aa.aa_base -= AMPS_PORT_SPACING;
195		config_found_sm(self, &aa, amps_print, NULL);
196	}
197}
198
199/*
200 * Card shutdown function
201 *
202 * Called via do_shutdown_hooks() during kernel shutdown.
203 * Clear the cards's interrupt mask to stop any podule interrupts.
204 */
205
206/*void
207amps_shutdown(arg)
208	void *arg;
209{
210}*/
211
212/*
213 * Atomwide Multi-Port Serial probe and attach code for the com device.
214 *
215 * This provides a different pair of probe and attach functions
216 * for attaching the com device (dev/ic/com.c) to the Atomwide serial card.
217 */
218
219struct com_amps_softc {
220	struct	com_softc sc_com;	/* real "com" softc */
221	void	*sc_ih;			/* interrupt handler */
222	struct	evcnt sc_intrcnt;	/* interrupt count */
223};
224
225/* Prototypes for functions */
226
227static int  com_amps_probe   __P((struct device *, struct cfdata *, void *));
228static void com_amps_attach  __P((struct device *, struct device *, void *));
229
230/* device attach structure */
231
232struct cfattach com_amps_ca = {
233	sizeof(struct com_amps_softc), com_amps_probe, com_amps_attach
234};
235
236/*
237 * Controller probe function
238 *
239 * Map all the required I/O space for this channel, make sure interrupts
240 * are disabled and probe the bus.
241 */
242
243int
244com_amps_probe(parent, cf, aux)
245	struct device *parent;
246	struct cfdata *cf;
247	void *aux;
248{
249	bus_space_tag_t iot;
250	bus_space_handle_t ioh;
251	int iobase;
252	int rv = 1;
253	struct amps_attach_args *aa = aux;
254
255	iot = aa->aa_iot;
256	iobase = aa->aa_base;
257
258	/* if it's in use as console, it's there. */
259	if (!com_is_console(iot, iobase, 0)) {
260		if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
261			return 0;
262		}
263		/*
264		 * We don't use the generic comprobe1() function as it
265		 * is not good enough to identify which ports are present.
266		 *
267		 * Instead test for the presence of the scratch register
268		 */
269
270		bus_space_write_1(iot, ioh, com_scratch, 0x55);
271		bus_space_write_1(iot, ioh, com_ier, 0);
272		(void)bus_space_read_1(iot, ioh, com_data);
273		if (bus_space_read_1(iot, ioh, com_scratch) != 0x55)
274			rv = 0;
275		bus_space_write_1(iot, ioh, com_scratch, 0xaa);
276		bus_space_write_1(iot, ioh, com_ier, 0);
277		(void)bus_space_read_1(iot, ioh, com_data);
278		if (bus_space_read_1(iot, ioh, com_scratch) != 0xaa)
279			rv = 0;
280
281		bus_space_unmap(iot, ioh, COM_NPORTS);
282	}
283	return (rv);
284}
285
286/*
287 * Controller attach function
288 *
289 * Map all the required I/O space for this port and attach the driver
290 * The generic attach will probe and attach any device.
291 * Install an interrupt handler and we are ready to rock.
292 */
293
294void
295com_amps_attach(parent, self, aux)
296	struct device *parent, *self;
297	void *aux;
298{
299	struct com_amps_softc *asc = (void *)self;
300	struct com_softc *sc = &asc->sc_com;
301	u_int iobase;
302	bus_space_tag_t iot;
303	struct amps_attach_args *aa = aux;
304
305	iot = sc->sc_iot = aa->aa_iot;
306	iobase = sc->sc_iobase = aa->aa_base;
307
308	if (!com_is_console(iot, iobase, &sc->sc_ioh)
309		&& bus_space_map(iot, iobase, COM_NPORTS, 0, &sc->sc_ioh))
310			panic("comattach: io mapping failed");
311
312	sc->sc_frequency = AMPS_FREQ;
313	com_attach_subr(sc);
314
315	evcnt_attach_dynamic(&asc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
316	    self->dv_xname, "intr");
317	asc->sc_ih = podulebus_irq_establish(aa->aa_irq, IPL_SERIAL, comintr,
318	    sc, &asc->sc_intrcnt);
319}
320