amps.c revision 1.5
1/*	$NetBSD: amps.c,v 1.5 2002/10/02 02:23:51 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
97CFATTACH_DECL(amps, sizeof(struct amps_softc),
98	amps_probe, amps_attach, NULL, NULL);
99
100/*
101 * Attach arguments for child devices.
102 * Pass the podule details, the parent softc and the channel
103 */
104
105struct amps_attach_args {
106	bus_space_tag_t aa_iot;			/* bus space tag */
107	unsigned int aa_base;			/* base address for port */
108	int aa_port;      			/* serial port number */
109	podulebus_intr_handle_t aa_irq;		/* IRQ number */
110};
111
112/*
113 * Define prototypes for custom bus space functions.
114 */
115
116/* Print function used during child config */
117
118int
119amps_print(aux, name)
120	void *aux;
121	const char *name;
122{
123	struct amps_attach_args *aa = aux;
124
125	if (!name)
126		printf(", port %d", aa->aa_port);
127
128	return(QUIET);
129}
130
131/*
132 * Card probe function
133 *
134 * Just match the manufacturer and podule ID's
135 */
136
137int
138amps_probe(parent, cf, aux)
139	struct device *parent;
140	struct cfdata *cf;
141	void *aux;
142{
143	struct podule_attach_args *pa = (void *)aux;
144
145	return (pa->pa_product == PODULE_ATOMWIDE_SERIAL);
146}
147
148/*
149 * Card attach function
150 *
151 * Identify the card version and configure any children.
152 * Install a shutdown handler to kill interrupts on shutdown
153 */
154
155void
156amps_attach(parent, self, aux)
157	struct device *parent, *self;
158	void *aux;
159{
160	struct amps_softc *sc = (void *)self;
161	struct podule_attach_args *pa = (void *)aux;
162	struct amps_attach_args aa;
163
164	/* Note the podule number and validate */
165
166	if (pa->pa_podule_number == -1)
167		panic("Podule has disappeared !");
168
169	sc->sc_podule_number = pa->pa_podule_number;
170	sc->sc_podule = pa->pa_podule;
171	podules[sc->sc_podule_number].attached = 1;
172
173	sc->sc_iot = pa->pa_iot;
174
175	/* Install a clean up handler to make sure IRQ's are disabled */
176/*	if (shutdownhook_establish(amps_shutdown, (void *)sc) == NULL)
177		panic("%s: Cannot install shutdown handler", self->dv_xname);*/
178
179	/* Set the interrupt info for this podule */
180
181/*	sc->sc_podule->irq_addr = pa->pa_podule->slow_base +;*/	/* XXX */
182/*	sc->sc_podule->irq_mask = ;*/
183
184	printf("\n");
185
186	/* Configure the children */
187
188	aa.aa_iot = sc->sc_iot;
189	aa.aa_base = pa->pa_podule->slow_base + AMPS_BASE_OFFSET;
190	aa.aa_base += MAX_AMPS_PORTS * AMPS_PORT_SPACING;
191	aa.aa_irq = pa->pa_podule->interrupt;
192	for (aa.aa_port = 0; aa.aa_port < MAX_AMPS_PORTS; ++aa.aa_port) {
193		aa.aa_base -= AMPS_PORT_SPACING;
194		config_found_sm(self, &aa, amps_print, NULL);
195	}
196}
197
198/*
199 * Card shutdown function
200 *
201 * Called via do_shutdown_hooks() during kernel shutdown.
202 * Clear the cards's interrupt mask to stop any podule interrupts.
203 */
204
205/*void
206amps_shutdown(arg)
207	void *arg;
208{
209}*/
210
211/*
212 * Atomwide Multi-Port Serial probe and attach code for the com device.
213 *
214 * This provides a different pair of probe and attach functions
215 * for attaching the com device (dev/ic/com.c) to the Atomwide serial card.
216 */
217
218struct com_amps_softc {
219	struct	com_softc sc_com;	/* real "com" softc */
220	void	*sc_ih;			/* interrupt handler */
221	struct	evcnt sc_intrcnt;	/* interrupt count */
222};
223
224/* Prototypes for functions */
225
226static int  com_amps_probe   __P((struct device *, struct cfdata *, void *));
227static void com_amps_attach  __P((struct device *, struct device *, void *));
228
229/* device attach structure */
230
231CFATTACH_DECL(com_amps, sizeof(struct com_amps_softc),
232	com_amps_probe, com_amps_attach, NULL, NULL);
233
234/*
235 * Controller probe function
236 *
237 * Map all the required I/O space for this channel, make sure interrupts
238 * are disabled and probe the bus.
239 */
240
241int
242com_amps_probe(parent, cf, aux)
243	struct device *parent;
244	struct cfdata *cf;
245	void *aux;
246{
247	bus_space_tag_t iot;
248	bus_space_handle_t ioh;
249	int iobase;
250	int rv = 1;
251	struct amps_attach_args *aa = aux;
252
253	iot = aa->aa_iot;
254	iobase = aa->aa_base;
255
256	/* if it's in use as console, it's there. */
257	if (!com_is_console(iot, iobase, 0)) {
258		if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
259			return 0;
260		}
261		/*
262		 * We don't use the generic comprobe1() function as it
263		 * is not good enough to identify which ports are present.
264		 *
265		 * Instead test for the presence of the scratch register
266		 */
267
268		bus_space_write_1(iot, ioh, com_scratch, 0x55);
269		bus_space_write_1(iot, ioh, com_ier, 0);
270		(void)bus_space_read_1(iot, ioh, com_data);
271		if (bus_space_read_1(iot, ioh, com_scratch) != 0x55)
272			rv = 0;
273		bus_space_write_1(iot, ioh, com_scratch, 0xaa);
274		bus_space_write_1(iot, ioh, com_ier, 0);
275		(void)bus_space_read_1(iot, ioh, com_data);
276		if (bus_space_read_1(iot, ioh, com_scratch) != 0xaa)
277			rv = 0;
278
279		bus_space_unmap(iot, ioh, COM_NPORTS);
280	}
281	return (rv);
282}
283
284/*
285 * Controller attach function
286 *
287 * Map all the required I/O space for this port and attach the driver
288 * The generic attach will probe and attach any device.
289 * Install an interrupt handler and we are ready to rock.
290 */
291
292void
293com_amps_attach(parent, self, aux)
294	struct device *parent, *self;
295	void *aux;
296{
297	struct com_amps_softc *asc = (void *)self;
298	struct com_softc *sc = &asc->sc_com;
299	u_int iobase;
300	bus_space_tag_t iot;
301	struct amps_attach_args *aa = aux;
302
303	iot = sc->sc_iot = aa->aa_iot;
304	iobase = sc->sc_iobase = aa->aa_base;
305
306	if (!com_is_console(iot, iobase, &sc->sc_ioh)
307		&& bus_space_map(iot, iobase, COM_NPORTS, 0, &sc->sc_ioh))
308			panic("comattach: io mapping failed");
309
310	sc->sc_frequency = AMPS_FREQ;
311	com_attach_subr(sc);
312
313	evcnt_attach_dynamic(&asc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
314	    self->dv_xname, "intr");
315	asc->sc_ih = podulebus_irq_establish(aa->aa_irq, IPL_SERIAL, comintr,
316	    sc, &asc->sc_intrcnt);
317}
318