amps.c revision 1.19
1/*	$NetBSD: amps.c,v 1.19 2011/07/19 15:59:54 dyoung 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * Card driver and probe and attach functions to use generic 16550 com
32 * driver for the Atomwide multiport serial podule
33 */
34
35/*
36 * Thanks to Martin Coulson, Atomwide, for providing the hardware
37 */
38
39#include <sys/cdefs.h>
40__KERNEL_RCSID(0, "$NetBSD: amps.c,v 1.19 2011/07/19 15:59:54 dyoung Exp $");
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/ioctl.h>
45#include <sys/select.h>
46#include <sys/tty.h>
47#include <sys/proc.h>
48#include <sys/conf.h>
49#include <sys/file.h>
50#include <sys/uio.h>
51#include <sys/kernel.h>
52#include <sys/syslog.h>
53#include <sys/types.h>
54#include <sys/device.h>
55#include <sys/bus.h>
56
57#include <machine/intr.h>
58#include <machine/io.h>
59#include <acorn32/podulebus/podulebus.h>
60#include <acorn32/podulebus/ampsreg.h>
61#include <dev/ic/comreg.h>
62#include <dev/ic/comvar.h>
63#include <dev/podulebus/podules.h>
64
65#include "locators.h"
66
67/*
68 * Atomwide Mulit-port serial podule device.
69 *
70 * This probes and attaches the top level multi-port serial device to the
71 * podulebus. It then configures the child com devices.
72 */
73
74/*
75 * Atomwide Multi-port serial card softc structure.
76 *
77 * Contains the device node, podule information and global information
78 * required by the driver such as the card version and the interrupt mask.
79 */
80
81struct amps_softc {
82	struct device		sc_dev;			/* device node */
83	podule_t 		*sc_podule;		/* Our podule info */
84	int 			sc_podule_number;	/* Our podule number */
85	bus_space_tag_t		sc_iot;			/* Bus tag */
86};
87
88int	amps_probe(device_t, cfdata_t, void *);
89void	amps_attach(device_t, device_t, void *);
90
91CFATTACH_DECL(amps, sizeof(struct amps_softc),
92    amps_probe, amps_attach, NULL, NULL);
93
94int	amps_print(void *, const char *);
95void	amps_shutdown(void *);
96
97/*
98 * Attach arguments for child devices.
99 * Pass the podule details, the parent softc and the channel
100 */
101
102struct amps_attach_args {
103	bus_space_tag_t aa_iot;			/* bus space tag */
104	unsigned int aa_base;			/* base address for port */
105	int aa_port;      			/* serial port number */
106	podulebus_intr_handle_t aa_irq;		/* IRQ number */
107};
108
109/*
110 * Define prototypes for custom bus space functions.
111 */
112
113/* Print function used during child config */
114
115int
116amps_print(void *aux, const char *name)
117{
118	struct amps_attach_args *aa = aux;
119
120	if (!name)
121		aprint_normal(", port %d", aa->aa_port);
122
123	return(QUIET);
124}
125
126/*
127 * Card probe function
128 *
129 * Just match the manufacturer and podule ID's
130 */
131
132int
133amps_probe(device_t parent, cfdata_t cf, void *aux)
134{
135	struct podule_attach_args *pa = aux;
136
137	return (pa->pa_product == PODULE_ATOMWIDE_SERIAL);
138}
139
140/*
141 * Card attach function
142 *
143 * Identify the card version and configure any children.
144 * Install a shutdown handler to kill interrupts on shutdown
145 */
146
147void
148amps_attach(device_t parent, device_t self, void *aux)
149{
150	struct amps_softc *sc = device_private(self);
151	struct podule_attach_args *pa = aux;
152	struct amps_attach_args aa;
153
154	/* Note the podule number and validate */
155
156	if (pa->pa_podule_number == -1)
157		panic("Podule has disappeared !");
158
159	sc->sc_podule_number = pa->pa_podule_number;
160	sc->sc_podule = pa->pa_podule;
161	podules[sc->sc_podule_number].attached = 1;
162
163	sc->sc_iot = pa->pa_iot;
164
165	/* Install a clean up handler to make sure IRQ's are disabled */
166/*	if (shutdownhook_establish(amps_shutdown, (void *)sc) == NULL)
167		panic("%s: Cannot install shutdown handler", self->dv_xname);*/
168
169	/* Set the interrupt info for this podule */
170
171/*	sc->sc_podule->irq_addr = pa->pa_podule->slow_base +;*/	/* XXX */
172/*	sc->sc_podule->irq_mask = ;*/
173
174	printf("\n");
175
176	/* Configure the children */
177
178	aa.aa_iot = sc->sc_iot;
179	aa.aa_base = pa->pa_podule->slow_base + AMPS_BASE_OFFSET;
180	aa.aa_base += MAX_AMPS_PORTS * AMPS_PORT_SPACING;
181	aa.aa_irq = pa->pa_podule->interrupt;
182	for (aa.aa_port = 0; aa.aa_port < MAX_AMPS_PORTS; ++aa.aa_port) {
183		aa.aa_base -= AMPS_PORT_SPACING;
184		config_found(self, &aa, amps_print);
185	}
186}
187
188/*
189 * Card shutdown function
190 *
191 * Called via do_shutdown_hooks() during kernel shutdown.
192 * Clear the cards's interrupt mask to stop any podule interrupts.
193 */
194
195/*void
196amps_shutdown(void *arg)
197{
198}*/
199
200/*
201 * Atomwide Multi-Port Serial probe and attach code for the com device.
202 *
203 * This provides a different pair of probe and attach functions
204 * for attaching the com device (dev/ic/com.c) to the Atomwide serial card.
205 */
206
207struct com_amps_softc {
208	struct	com_softc sc_com;	/* real "com" softc */
209	void	*sc_ih;			/* interrupt handler */
210	struct	evcnt sc_intrcnt;	/* interrupt count */
211};
212
213/* Prototypes for functions */
214
215static int  com_amps_probe   (device_t, cfdata_t , void *);
216static void com_amps_attach  (device_t, device_t, void *);
217
218/* device attach structure */
219
220CFATTACH_DECL_NEW(com_amps, sizeof(struct com_amps_softc),
221	com_amps_probe, com_amps_attach, NULL, NULL);
222
223/*
224 * Controller probe function
225 *
226 * Map all the required I/O space for this channel, make sure interrupts
227 * are disabled and probe the bus.
228 */
229
230int
231com_amps_probe(device_t parent, cfdata_t cf, void *aux)
232{
233	bus_space_tag_t iot;
234	bus_space_handle_t ioh;
235	int iobase;
236	int rv = 1;
237	struct amps_attach_args *aa = aux;
238
239	iot = aa->aa_iot;
240	iobase = aa->aa_base;
241
242	/* if it's in use as console, it's there. */
243	if (!com_is_console(iot, iobase, 0)) {
244		if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
245			return 0;
246		}
247		/*
248		 * We don't use the generic comprobe1() function as it
249		 * is not good enough to identify which ports are present.
250		 *
251		 * Instead test for the presence of the scratch register
252		 */
253
254		bus_space_write_1(iot, ioh, com_scratch, 0x55);
255		bus_space_write_1(iot, ioh, com_ier, 0);
256		(void)bus_space_read_1(iot, ioh, com_data);
257		if (bus_space_read_1(iot, ioh, com_scratch) != 0x55)
258			rv = 0;
259		bus_space_write_1(iot, ioh, com_scratch, 0xaa);
260		bus_space_write_1(iot, ioh, com_ier, 0);
261		(void)bus_space_read_1(iot, ioh, com_data);
262		if (bus_space_read_1(iot, ioh, com_scratch) != 0xaa)
263			rv = 0;
264
265		bus_space_unmap(iot, ioh, COM_NPORTS);
266	}
267	return (rv);
268}
269
270/*
271 * Controller attach function
272 *
273 * Map all the required I/O space for this port and attach the driver
274 * The generic attach will probe and attach any device.
275 * Install an interrupt handler and we are ready to rock.
276 */
277
278void
279com_amps_attach(device_t parent, device_t self, void *aux)
280{
281	struct com_amps_softc *asc = device_private(self);
282	struct com_softc *sc = &asc->sc_com;
283	u_int iobase;
284	bus_space_tag_t iot;
285	bus_space_handle_t ioh;
286	struct amps_attach_args *aa = aux;
287
288	sc->sc_dev = self;
289	iot = aa->aa_iot;
290	iobase = aa->aa_base;
291
292	if (!com_is_console(iot, iobase, &ioh)
293	    && bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh))
294		panic("comattach: io mapping failed");
295	COM_INIT_REGS(sc->sc_regs, iot, ioh, iobase);
296
297	sc->sc_frequency = AMPS_FREQ;
298	com_attach_subr(sc);
299
300	evcnt_attach_dynamic(&asc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
301	    device_xname(self), "intr");
302	asc->sc_ih = podulebus_irq_establish(aa->aa_irq, IPL_SERIAL, comintr,
303	    sc, &asc->sc_intrcnt);
304}
305