xref: /netbsd-src/sys/arch/sun2/dev/zs_any.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: zs_any.c,v 1.13 2005/12/11 12:19:16 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Gordon W. Ross and Matthew Fredette.
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  * Zilog Z8530 Dual UART driver (machine-dependent part)
41  *
42  * Runs two serial lines per chip using slave drivers.
43  * Plain tty/async lines use the zs_async slave.
44  * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
45  */
46 
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: zs_any.c,v 1.13 2005/12/11 12:19:16 christos Exp $");
49 
50 #include "opt_kgdb.h"
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/conf.h>
55 #include <sys/device.h>
56 #include <sys/file.h>
57 #include <sys/ioctl.h>
58 #include <sys/kernel.h>
59 #include <sys/proc.h>
60 #include <sys/tty.h>
61 #include <sys/time.h>
62 #include <sys/syslog.h>
63 
64 #include <machine/autoconf.h>
65 #include <machine/bus.h>
66 #include <machine/z8530var.h>
67 
68 #ifdef	KGDB
69 #include <uvm/uvm_extern.h>
70 
71 #include <machine/idprom.h>
72 #include <machine/pmap.h>
73 #include <sun2/dev/cons.h>
74 #endif
75 
76 #include <sun2/sun2/machdep.h>
77 #include <dev/ic/z8530reg.h>
78 #include <dev/sun/kbd_reg.h>
79 
80 /****************************************************************
81  * Autoconfig
82  ****************************************************************/
83 
84 /* Definition of the driver for autoconfig. */
85 static int	zs_any_match(struct device *, struct cfdata *, void *);
86 static void	zs_any_attach(struct device *, struct device *, void *);
87 
88 CFATTACH_DECL(zs_obio, sizeof(struct zsc_softc),
89     zs_any_match, zs_any_attach, NULL, NULL);
90 
91 CFATTACH_DECL(zs_obmem, sizeof(struct zsc_softc),
92     zs_any_match, zs_any_attach, NULL, NULL);
93 
94 CFATTACH_DECL(zs_mbmem, sizeof(struct zsc_softc),
95     zs_any_match, zs_any_attach, NULL, NULL);
96 
97 /*
98  * Is the zs chip present?
99  */
100 static int
101 zs_any_match(struct device *parent, struct cfdata *cf, void *aux)
102 {
103 	struct mainbus_attach_args *ma = aux;
104 	bus_space_handle_t bh;
105 	int matched;
106 
107 	/* Make sure there is something there... */
108 	if (bus_space_map(ma->ma_bustag, ma->ma_paddr, sizeof(struct zsdevice),
109 			  0, &bh))
110 		return (0);
111 	matched = (bus_space_peek_1(ma->ma_bustag, bh, 0, NULL) == 0);
112 	bus_space_unmap(ma->ma_bustag, bh, sizeof(struct zsdevice));
113 	if (!matched)
114 		return (0);
115 
116 	/* Default interrupt priority (always splbio==2) */
117 	if (ma->ma_pri == -1)
118 		ma->ma_pri = ZSHARD_PRI;
119 
120 	return (1);
121 }
122 
123 /*
124  * Attach a found zs.
125  */
126 static void
127 zs_any_attach(struct device *parent, struct device *self, void *aux)
128 {
129 	struct zsc_softc *zsc = (void *) self;
130 	struct mainbus_attach_args *ma = aux;
131 	bus_space_handle_t bh;
132 
133         zsc->zsc_bustag = ma->ma_bustag;
134         zsc->zsc_dmatag = ma->ma_dmatag;
135         zsc->zsc_promunit = self->dv_unit;
136         zsc->zsc_node = 0;
137 
138 	/* Map in the device. */
139 	if (bus_space_map(ma->ma_bustag, ma->ma_paddr, sizeof(struct zsdevice),
140 			  BUS_SPACE_MAP_LINEAR, &bh))
141 		panic("zs_any_attach: can't map");
142 
143 	/* This is where we break the bus_space(9) abstraction: */
144 	zs_attach(zsc, (void *)bh, ma->ma_pri);
145 }
146 
147 int
148 zs_console_flags(int promunit, int node, int channel)
149 {
150 	int cookie, flags = 0;
151 
152 	/*
153 	 * Use `promunit' and `channel' to derive the PROM
154 	 * stdio handles that correspond to this device.
155 	 */
156 	if (promunit == 0)
157 		cookie = PROMDEV_TTYA + channel;
158 	else if (promunit == 1 && channel == 0)
159 		cookie = PROMDEV_KBD;
160 	else
161 		cookie = -1;
162 
163 	/*
164 	 * We have the console keyboard only if it's a Sun-2
165 	 * keyboard or better. (i.e., not a Sun-1 parallel kbd).
166 	 */
167 	if (cookie == prom_stdin() &&
168 	    (cookie != PROMDEV_KBD || prom_kbdid() >= KB_SUN2))
169 		flags |= ZS_HWFLAG_CONSOLE_INPUT;
170 
171 	/*
172 	 * Prevent the keyboard from matching the output device
173 	 * (note that PROMDEV_KBD == PROMDEV_SCREEN == 0!).
174 	 */
175 	if (cookie != PROMDEV_KBD && cookie == prom_stdout())
176 		flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
177 
178 	return (flags);
179 }
180 
181 #ifdef	KGDB
182 extern	int sun68k_find_prom_map(bus_addr_t, bus_type_t, int,
183 	    bus_space_handle_t *);
184 
185 /*
186  * Find a zs mapped by the PROM.  Currently this only works to find
187  * zs0 on obio.
188  */
189 void *
190 zs_find_prom(int unit)
191 {
192 	bus_addr_t zs0_phys;
193 	bus_space_handle_t bh;
194 
195 	if (unit != 0)
196 		return (NULL);
197 
198 	/*
199 	 * The physical address of zs0 is model-dependent.
200 	 */
201 	zs0_phys = (cpu_machine_id == ID_SUN2_120 ? 0x002000 : 0x7f2000);
202 	if (sun68k_find_prom_map(zs0_phys, PMAP_OBIO, sizeof(struct zsdevice),
203 	    &bh))
204 		return (NULL);
205 
206 	return (bh);
207 }
208 #endif	/* KGDB */
209