amps.c revision 1.2
1/*	$NetBSD: amps.c,v 1.2 2001/11/27 00:53:12 thorpej 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	if (matchpodule(pa, MANUFACTURER_ATOMWIDE2, PODULE_ATOMWIDE2_SERIAL, -1) == 0)
147		return(0);
148	return(1);
149}
150
151/*
152 * Card attach function
153 *
154 * Identify the card version and configure any children.
155 * Install a shutdown handler to kill interrupts on shutdown
156 */
157
158void
159amps_attach(parent, self, aux)
160	struct device *parent, *self;
161	void *aux;
162{
163	struct amps_softc *sc = (void *)self;
164	struct podule_attach_args *pa = (void *)aux;
165	struct amps_attach_args aa;
166
167	/* Note the podule number and validate */
168
169	if (pa->pa_podule_number == -1)
170		panic("Podule has disappeared !");
171
172	sc->sc_podule_number = pa->pa_podule_number;
173	sc->sc_podule = pa->pa_podule;
174	podules[sc->sc_podule_number].attached = 1;
175
176	sc->sc_iot = pa->pa_iot;
177
178	/* Install a clean up handler to make sure IRQ's are disabled */
179/*	if (shutdownhook_establish(amps_shutdown, (void *)sc) == NULL)
180		panic("%s: Cannot install shutdown handler", self->dv_xname);*/
181
182	/* Set the interrupt info for this podule */
183
184/*	sc->sc_podule->irq_addr = pa->pa_podule->slow_base +;*/	/* XXX */
185/*	sc->sc_podule->irq_mask = ;*/
186
187	printf("\n");
188
189	/* Configure the children */
190
191	aa.aa_iot = sc->sc_iot;
192	aa.aa_base = pa->pa_podule->slow_base + AMPS_BASE_OFFSET;
193	aa.aa_base += MAX_AMPS_PORTS * AMPS_PORT_SPACING;
194	aa.aa_irq = pa->pa_podule->interrupt;
195	for (aa.aa_port = 0; aa.aa_port < MAX_AMPS_PORTS; ++aa.aa_port) {
196		aa.aa_base -= AMPS_PORT_SPACING;
197		config_found_sm(self, &aa, amps_print, NULL);
198	}
199}
200
201/*
202 * Card shutdown function
203 *
204 * Called via do_shutdown_hooks() during kernel shutdown.
205 * Clear the cards's interrupt mask to stop any podule interrupts.
206 */
207
208/*void
209amps_shutdown(arg)
210	void *arg;
211{
212}*/
213
214/*
215 * Atomwide Multi-Port Serial probe and attach code for the com device.
216 *
217 * This provides a different pair of probe and attach functions
218 * for attaching the com device (dev/ic/com.c) to the Atomwide serial card.
219 */
220
221struct com_amps_softc {
222	struct	com_softc sc_com;	/* real "com" softc */
223	void	*sc_ih;			/* interrupt handler */
224	struct	evcnt sc_intrcnt;	/* interrupt count */
225};
226
227/* Prototypes for functions */
228
229static int  com_amps_probe   __P((struct device *, struct cfdata *, void *));
230static void com_amps_attach  __P((struct device *, struct device *, void *));
231
232/* device attach structure */
233
234struct cfattach com_amps_ca = {
235	sizeof(struct com_amps_softc), com_amps_probe, com_amps_attach
236};
237
238/*
239 * Controller probe function
240 *
241 * Map all the required I/O space for this channel, make sure interrupts
242 * are disabled and probe the bus.
243 */
244
245int
246com_amps_probe(parent, cf, aux)
247	struct device *parent;
248	struct cfdata *cf;
249	void *aux;
250{
251	bus_space_tag_t iot;
252	bus_space_handle_t ioh;
253	int iobase;
254	int rv = 1;
255	struct amps_attach_args *aa = aux;
256
257	iot = aa->aa_iot;
258	iobase = aa->aa_base;
259
260	/* if it's in use as console, it's there. */
261	if (!com_is_console(iot, iobase, 0)) {
262		if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
263			return 0;
264		}
265		/*
266		 * We don't use the generic comprobe1() function as it
267		 * is not good enough to identify which ports are present.
268		 *
269		 * Instead test for the presence of the scratch register
270		 */
271
272		bus_space_write_1(iot, ioh, com_scratch, 0x55);
273		bus_space_write_1(iot, ioh, com_ier, 0);
274		(void)bus_space_read_1(iot, ioh, com_data);
275		if (bus_space_read_1(iot, ioh, com_scratch) != 0x55)
276			rv = 0;
277		bus_space_write_1(iot, ioh, com_scratch, 0xaa);
278		bus_space_write_1(iot, ioh, com_ier, 0);
279		(void)bus_space_read_1(iot, ioh, com_data);
280		if (bus_space_read_1(iot, ioh, com_scratch) != 0xaa)
281			rv = 0;
282
283		bus_space_unmap(iot, ioh, COM_NPORTS);
284	}
285	return (rv);
286}
287
288/*
289 * Controller attach function
290 *
291 * Map all the required I/O space for this port and attach the driver
292 * The generic attach will probe and attach any device.
293 * Install an interrupt handler and we are ready to rock.
294 */
295
296void
297com_amps_attach(parent, self, aux)
298	struct device *parent, *self;
299	void *aux;
300{
301	struct com_amps_softc *asc = (void *)self;
302	struct com_softc *sc = &asc->sc_com;
303	u_int iobase;
304	bus_space_tag_t iot;
305	struct amps_attach_args *aa = aux;
306
307	iot = sc->sc_iot = aa->aa_iot;
308	iobase = sc->sc_iobase = aa->aa_base;
309
310	if (!com_is_console(iot, iobase, &sc->sc_ioh)
311		&& bus_space_map(iot, iobase, COM_NPORTS, 0, &sc->sc_ioh))
312			panic("comattach: io mapping failed");
313
314	sc->sc_frequency = AMPS_FREQ;
315	com_attach_subr(sc);
316
317	evcnt_attach_dynamic(&asc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
318	    self->dv_xname, "intr");
319	asc->sc_ih = podulebus_irq_establish(aa->aa_irq, IPL_SERIAL, comintr,
320	    sc, &asc->sc_intrcnt);
321}
322