xref: /netbsd-src/sys/arch/sun2/dev/bwtwo_any.c (revision cf80ca28cb9ef8022317ff1af66f5a815972f8ad)
1 /*	$NetBSD: bwtwo_any.c,v 1.18 2023/12/20 05:13:35 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe 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  *
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 
32 /*
33  * Copyright (c) 1992, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * This software was developed by the Computer Systems Engineering group
37  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
38  * contributed to Berkeley.
39  *
40  * All advertising materials mentioning features or use of this software
41  * must display the following acknowledgement:
42  *	This product includes software developed by the University of
43  *	California, Lawrence Berkeley Laboratory.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. Neither the name of the University nor the names of its contributors
54  *    may be used to endorse or promote products derived from this software
55  *    without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  *
69  *	@(#)bwtwo.c	8.1 (Berkeley) 6/11/93
70  */
71 
72 /*
73  * black&white display (bwtwo) driver.
74  *
75  * Does not handle interrupts, even though they can occur.
76  *
77  * P4 and overlay plane support by Jason R. Thorpe <thorpej@NetBSD.org>.
78  * Overlay plane handling hints and ideas provided by Brad Spencer.
79  */
80 
81 #include <sys/cdefs.h>
82 __KERNEL_RCSID(0, "$NetBSD: bwtwo_any.c,v 1.18 2023/12/20 05:13:35 thorpej Exp $");
83 
84 #include <sys/param.h>
85 #include <sys/systm.h>
86 #include <sys/device.h>
87 #include <sys/ioctl.h>
88 #include <sys/mman.h>
89 #include <sys/tty.h>
90 #include <sys/conf.h>
91 
92 #include <machine/autoconf.h>
93 #include <sun2/sun2/control.h>
94 
95 #include <dev/sun/fbio.h>
96 #include <dev/sun/fbvar.h>
97 #include <dev/sun/btreg.h>
98 #include <sun2/dev/bwtworeg.h>
99 #include <dev/sun/bwtwovar.h>
100 
101 /* autoconfiguration driver */
102 static int	bwtwomatch_any(device_t, cfdata_t, void *);
103 static void	bwtwoattach_any(device_t, device_t, void *);
104 
105 struct bwtwosun2_softc {
106 	struct bwtwo_softc sc;
107 	bus_space_handle_t bh;
108 };
109 
110 CFATTACH_DECL_NEW(bwtwo_obio, sizeof(struct bwtwosun2_softc),
111     bwtwomatch_any, bwtwoattach_any, NULL, NULL);
112 
113 CFATTACH_DECL_NEW(bwtwo_obmem, sizeof(struct bwtwosun2_softc),
114     bwtwomatch_any, bwtwoattach_any, NULL, NULL);
115 
116 static int	bwtwo_get_video_sun2(struct bwtwo_softc *);
117 static void	bwtwo_set_video_sun2(struct bwtwo_softc *, int);
118 
119 extern int fbnode;
120 
121 static int
bwtwomatch_any(device_t parent,cfdata_t cf,void * aux)122 bwtwomatch_any(device_t parent, cfdata_t cf, void *aux)
123 {
124 	struct mainbus_attach_args *ma = aux;
125 	bus_space_handle_t bh;
126 	int matched;
127 
128 	/* Make sure there is something there... */
129 	if (bus_space_map(ma->ma_bustag, ma->ma_paddr + BWREG_REG,
130 			  sizeof(struct bwtworeg), 0, &bh))
131 		return (0);
132 	matched = (bus_space_peek_1(ma->ma_bustag, bh, 0, NULL) == 0);
133 	bus_space_unmap(ma->ma_bustag, bh, sizeof(struct bwtworeg));
134 	return (matched);
135 }
136 
137 static void
bwtwoattach_any(device_t parent,device_t self,void * aux)138 bwtwoattach_any(device_t parent, device_t self, void *aux)
139 {
140 	struct bwtwosun2_softc *scsun2 = device_private(self);
141 	struct bwtwo_softc *sc = &scsun2->sc;
142 	struct mainbus_attach_args *ma = aux;
143 	struct fbdevice *fb = &sc->sc_fb;
144 	bus_space_handle_t bh;
145 	int isconsole;
146 	const char *name;
147 
148 	sc->sc_dev = self;
149 
150 	/* Remember cookies for bwtwo_mmap() */
151 	sc->sc_bustag = ma->ma_bustag;
152 	sc->sc_paddr = ma->ma_paddr;
153 
154 	fb->fb_flags = device_cfdata(self)->cf_flags;
155 	fb->fb_type.fb_depth = 1;
156 	fb_setsize_eeprom(fb, fb->fb_type.fb_depth, 1152, 900);
157 
158 	isconsole = fb_is_console(0);
159 
160 	/* A plain bwtwo */
161 	sc->sc_reg = NULL;
162 	fb->fb_pfour = NULL;
163 	name = "bwtwo";
164 	sc->sc_pixeloffset = BWREG_MEM;
165 	sc->sc_get_video = bwtwo_get_video_sun2;
166 	sc->sc_set_video = bwtwo_set_video_sun2;
167 
168 	/* Map the registers. */
169 	if (bus_space_map(ma->ma_bustag, ma->ma_paddr + BWREG_REG,
170 			  sizeof(struct bwtworeg), 0, &scsun2->bh)) {
171 		printf("%s: cannot map regs\n", device_xname(self));
172 		return;
173 	}
174 
175 	if (isconsole) {
176 		int ramsize = fb->fb_type.fb_height * fb->fb_linebytes;
177 		if (bus_space_map(ma->ma_bustag,
178 				  ma->ma_paddr + sc->sc_pixeloffset,
179 				  ramsize,
180 				  BUS_SPACE_MAP_LINEAR, &bh) != 0) {
181 			printf("%s: cannot map pixels\n", device_xname(self));
182 			return;
183 		}
184 		sc->sc_fb.fb_pixels = (char *)bh;
185 	}
186 
187 	bwtwoattach(sc, name, isconsole);
188 }
189 
190 static void
bwtwo_set_video_sun2(struct bwtwo_softc * sc,int enable)191 bwtwo_set_video_sun2(struct bwtwo_softc *sc, int enable)
192 {
193 	struct bwtwosun2_softc *scsun2 = (struct bwtwosun2_softc *)sc;
194 	unsigned char cr;
195 
196 	cr = bus_space_read_1(sc->sc_bustag, scsun2->bh, 0);
197 	bus_space_write_1(sc->sc_bustag, scsun2->bh, 0,
198 			  (enable ?
199 			   (cr | BW2_CR_ENABLE_VIDEO) :
200 			   (cr & ~BW2_CR_ENABLE_VIDEO)));
201 	return;
202 }
203 
204 static int
bwtwo_get_video_sun2(struct bwtwo_softc * sc)205 bwtwo_get_video_sun2(struct bwtwo_softc *sc)
206 {
207 	struct bwtwosun2_softc *scsun2 = (struct bwtwosun2_softc *)sc;
208 
209 	return ((bus_space_read_1(sc->sc_bustag, scsun2->bh, 0) &
210 		 BW2_CR_ENABLE_VIDEO) != 0);
211 }
212