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