ofw_rascons.c revision 1.18 1 1.18 andvar /* $NetBSD: ofw_rascons.c,v 1.18 2021/08/17 22:00:30 andvar Exp $ */
2 1.1 garbled
3 1.1 garbled /*
4 1.1 garbled * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5 1.1 garbled * All rights reserved.
6 1.1 garbled *
7 1.1 garbled * Author: Chris G. Demetriou
8 1.4 kiyohara *
9 1.1 garbled * Permission to use, copy, modify and distribute this software and
10 1.1 garbled * its documentation is hereby granted, provided that both the copyright
11 1.1 garbled * notice and this permission notice appear in all copies of the
12 1.1 garbled * software, derivative works or modified versions, and any portions
13 1.1 garbled * thereof, and that both notices appear in supporting documentation.
14 1.4 kiyohara *
15 1.4 kiyohara * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 1.4 kiyohara * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 1.1 garbled * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 1.4 kiyohara *
19 1.1 garbled * Carnegie Mellon requests users of this software to return to
20 1.1 garbled *
21 1.1 garbled * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 1.1 garbled * School of Computer Science
23 1.1 garbled * Carnegie Mellon University
24 1.1 garbled * Pittsburgh PA 15213-3890
25 1.1 garbled *
26 1.1 garbled * any improvements or extensions that they make and grant Carnegie the
27 1.1 garbled * rights to redistribute these changes.
28 1.1 garbled */
29 1.1 garbled
30 1.1 garbled #include <sys/cdefs.h>
31 1.18 andvar __KERNEL_RCSID(0, "$NetBSD: ofw_rascons.c,v 1.18 2021/08/17 22:00:30 andvar Exp $");
32 1.8 matt
33 1.8 matt #include "wsdisplay.h"
34 1.1 garbled
35 1.1 garbled #include <sys/param.h>
36 1.1 garbled #include <sys/buf.h>
37 1.8 matt #include <sys/bus.h>
38 1.1 garbled #include <sys/conf.h>
39 1.1 garbled #include <sys/device.h>
40 1.1 garbled #include <sys/ioctl.h>
41 1.1 garbled #include <sys/kernel.h>
42 1.1 garbled #include <sys/systm.h>
43 1.1 garbled
44 1.1 garbled #include <dev/ofw/openfirm.h>
45 1.1 garbled #include <uvm/uvm_extern.h>
46 1.1 garbled
47 1.1 garbled #include <machine/autoconf.h>
48 1.1 garbled
49 1.1 garbled #include <dev/wscons/wsconsio.h>
50 1.1 garbled #include <dev/wscons/wsdisplayvar.h>
51 1.1 garbled #include <dev/rasops/rasops.h>
52 1.8 matt #include <dev/wscons/wsdisplay_vconsvar.h>
53 1.1 garbled #include <dev/wsfont/wsfont.h>
54 1.1 garbled
55 1.8 matt #include <powerpc/oea/bat.h>
56 1.11 macallan #include <powerpc/oea/cpufeat.h>
57 1.1 garbled #include <powerpc/oea/ofw_rasconsvar.h>
58 1.1 garbled
59 1.1 garbled /* we need a wsdisplay to do anything halfway useful */
60 1.1 garbled #if NWSDISPLAY > 0
61 1.1 garbled
62 1.1 garbled static int copy_rom_font(void);
63 1.1 garbled static struct wsdisplay_font openfirm6x11;
64 1.1 garbled static vaddr_t fbaddr;
65 1.1 garbled static int romfont_loaded = 0;
66 1.11 macallan static int needs_finalize = 0;
67 1.1 garbled
68 1.14 macallan #define FONTBUFSIZE (2048) /* enough for 96 6x11 bitmap characters */
69 1.14 macallan static uint8_t fontbuf[FONTBUFSIZE];
70 1.14 macallan
71 1.1 garbled struct vcons_screen rascons_console_screen;
72 1.1 garbled
73 1.1 garbled struct wsscreen_descr rascons_stdscreen = {
74 1.1 garbled "std",
75 1.1 garbled 0, 0, /* will be filled in -- XXX shouldn't, it's global */
76 1.1 garbled 0,
77 1.1 garbled 0, 0,
78 1.1 garbled WSSCREEN_REVERSE
79 1.1 garbled };
80 1.1 garbled
81 1.1 garbled int
82 1.1 garbled rascons_cnattach(void)
83 1.1 garbled {
84 1.1 garbled struct rasops_info *ri = &rascons_console_screen.scr_ri;
85 1.1 garbled long defattr;
86 1.1 garbled int crow = 0;
87 1.1 garbled
88 1.1 garbled /* get current cursor position */
89 1.1 garbled OF_interpret("line#", 0, 1, &crow);
90 1.15 rin if (crow < 0)
91 1.15 rin crow = 0;
92 1.1 garbled
93 1.1 garbled /* move (rom monitor) cursor to the lowest line - 1 */
94 1.14 macallan /* XXXX - Why? */
95 1.14 macallan #if 0
96 1.1 garbled OF_interpret("#lines 2 - to line#", 0, 0);
97 1.14 macallan #endif
98 1.1 garbled wsfont_init();
99 1.1 garbled if (copy_rom_font() == 0) {
100 1.9 macallan #if !defined(OFWOEA_WSCONS_NO_ROM_FONT)
101 1.1 garbled romfont_loaded = 1;
102 1.9 macallan #endif /* !OFWOEA_WSCONS_NO_ROM_FONT */
103 1.1 garbled }
104 1.4 kiyohara
105 1.1 garbled /* set up rasops */
106 1.1 garbled rascons_init_rasops(console_node, ri);
107 1.1 garbled
108 1.1 garbled /*
109 1.1 garbled * no need to clear the screen here when we're mimicing firmware
110 1.1 garbled * output anyway
111 1.1 garbled */
112 1.1 garbled #if 0
113 1.1 garbled if (ri->ri_width >= 1024 && ri->ri_height >= 768) {
114 1.1 garbled int i, screenbytes = ri->ri_stride * ri->ri_height;
115 1.1 garbled
116 1.1 garbled for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
117 1.1 garbled *(u_int32_t *)(fbaddr + i) = 0xffffffff;
118 1.1 garbled crow = 0;
119 1.1 garbled }
120 1.1 garbled #endif
121 1.4 kiyohara
122 1.1 garbled rascons_stdscreen.nrows = ri->ri_rows;
123 1.1 garbled rascons_stdscreen.ncols = ri->ri_cols;
124 1.1 garbled rascons_stdscreen.textops = &ri->ri_ops;
125 1.1 garbled rascons_stdscreen.capabilities = ri->ri_caps;
126 1.1 garbled
127 1.12 macallan /*
128 1.12 macallan * XXX
129 1.12 macallan * On some G5 models ( so far, 970FX but not 970MP ) we can't seem to
130 1.12 macallan * access video memory in real mode, but a lot of code relies on rasops
131 1.12 macallan * data structures being set up early so we can't just push the whole
132 1.12 macallan * thing further down. Instead set things up but don't actually attach
133 1.12 macallan * the console until later.
134 1.12 macallan * This needs a better trigger but for now I can't reliably tell which
135 1.12 macallan * exact models / CPUs / other hardware actually need it.
136 1.12 macallan */
137 1.11 macallan if ((oeacpufeat & OEACPU_64_BRIDGE) != 0) {
138 1.11 macallan needs_finalize = 1;
139 1.11 macallan } else {
140 1.11 macallan ri->ri_ops.allocattr(ri, 0, 0, 0, &defattr);
141 1.13 riastrad wsdisplay_preattach(&rascons_stdscreen, ri, 0, uimax(0,
142 1.13 riastrad uimin(crow, ri->ri_rows - 1)), defattr);
143 1.11 macallan }
144 1.1 garbled #if notyet
145 1.1 garbled rascons_init_cmap(NULL);
146 1.1 garbled #endif
147 1.1 garbled
148 1.1 garbled return 0;
149 1.1 garbled }
150 1.1 garbled
151 1.11 macallan void
152 1.14 macallan rascons_add_rom_font(void)
153 1.14 macallan {
154 1.14 macallan wsfont_init();
155 1.14 macallan if (romfont_loaded) {
156 1.14 macallan wsfont_add(&openfirm6x11, 0);
157 1.14 macallan }
158 1.14 macallan }
159 1.14 macallan
160 1.14 macallan void
161 1.11 macallan rascons_finalize(void)
162 1.11 macallan {
163 1.11 macallan struct rasops_info *ri = &rascons_console_screen.scr_ri;
164 1.11 macallan long defattr;
165 1.14 macallan int crow = 0;
166 1.11 macallan
167 1.11 macallan if (needs_finalize == 0) return;
168 1.14 macallan
169 1.14 macallan /* get current cursor position */
170 1.15 rin if (romfont_loaded) {
171 1.15 rin OF_interpret("line#", 0, 1, &crow);
172 1.15 rin if (crow < 0)
173 1.15 rin crow = 0;
174 1.15 rin }
175 1.14 macallan
176 1.11 macallan ri->ri_ops.allocattr(ri, 0, 0, 0, &defattr);
177 1.14 macallan wsdisplay_preattach(&rascons_stdscreen, ri, 0, uimax(0,
178 1.14 macallan uimin(crow, ri->ri_rows - 1)), defattr);
179 1.11 macallan }
180 1.11 macallan
181 1.1 garbled static int
182 1.3 cegger copy_rom_font(void)
183 1.1 garbled {
184 1.1 garbled u_char *romfont;
185 1.14 macallan int char_width, char_height, stride;
186 1.14 macallan int chosen, mmu, m, e, size;
187 1.1 garbled
188 1.15 rin /*
189 1.15 rin * Get ROM FONT address.
190 1.15 rin *
191 1.15 rin * For some machines like ``PowerMac11,2'', Open Firmware does not
192 1.15 rin * initialize console-related variables when auto-boot? is true;
193 1.15 rin * -1 is returned instead of correct value. Fall back to wsfont
194 1.15 rin * embedded in kernel in this case.
195 1.15 rin */
196 1.1 garbled OF_interpret("font-adr", 0, 1, &romfont);
197 1.15 rin if (romfont == NULL || romfont == (u_char *)-1)
198 1.1 garbled return -1;
199 1.1 garbled
200 1.1 garbled chosen = OF_finddevice("/chosen");
201 1.1 garbled OF_getprop(chosen, "mmu", &mmu, 4);
202 1.1 garbled
203 1.1 garbled /*
204 1.18 andvar * Convert to physical address. We cannot access to Open Firmware's
205 1.1 garbled * virtual address space.
206 1.1 garbled */
207 1.1 garbled OF_call_method("translate", mmu, 1, 3, romfont, &romfont, &m, &e);
208 1.4 kiyohara
209 1.1 garbled /* Get character size */
210 1.1 garbled OF_interpret("char-width", 0, 1, &char_width);
211 1.1 garbled OF_interpret("char-height", 0, 1, &char_height);
212 1.1 garbled
213 1.14 macallan stride = (char_width + 7) >> 3;
214 1.14 macallan size = stride * char_height * 96;
215 1.14 macallan if (size > FONTBUFSIZE) return -1;
216 1.14 macallan
217 1.14 macallan memcpy(fontbuf, romfont, size);
218 1.14 macallan
219 1.1 garbled openfirm6x11.name = "Open Firmware";
220 1.1 garbled openfirm6x11.firstchar = 32;
221 1.1 garbled openfirm6x11.numchars = 96;
222 1.1 garbled openfirm6x11.encoding = WSDISPLAY_FONTENC_ISO;
223 1.1 garbled openfirm6x11.fontwidth = char_width;
224 1.1 garbled openfirm6x11.fontheight = char_height;
225 1.14 macallan openfirm6x11.stride = stride;
226 1.1 garbled openfirm6x11.bitorder = WSDISPLAY_FONTORDER_L2R;
227 1.1 garbled openfirm6x11.byteorder = WSDISPLAY_FONTORDER_L2R;
228 1.14 macallan openfirm6x11.data = fontbuf;
229 1.1 garbled
230 1.1 garbled return 0;
231 1.1 garbled }
232 1.1 garbled
233 1.1 garbled int
234 1.1 garbled rascons_init_rasops(int node, struct rasops_info *ri)
235 1.1 garbled {
236 1.1 garbled int32_t width, height, linebytes, depth;
237 1.1 garbled
238 1.1 garbled /* XXX /chaos/control doesn't have "width", "height", ... */
239 1.1 garbled width = height = -1;
240 1.1 garbled if (OF_getprop(node, "width", &width, 4) != 4)
241 1.1 garbled OF_interpret("screen-width", 0, 1, &width);
242 1.1 garbled if (OF_getprop(node, "height", &height, 4) != 4)
243 1.1 garbled OF_interpret("screen-height", 0, 1, &height);
244 1.1 garbled if (OF_getprop(node, "linebytes", &linebytes, 4) != 4)
245 1.1 garbled linebytes = width; /* XXX */
246 1.1 garbled if (OF_getprop(node, "depth", &depth, 4) != 4)
247 1.1 garbled depth = 8; /* XXX */
248 1.1 garbled if (OF_getprop(node, "address", &fbaddr, 4) != 4)
249 1.1 garbled OF_interpret("frame-buffer-adr", 0, 1, &fbaddr);
250 1.1 garbled
251 1.1 garbled if (width == -1 || height == -1 || fbaddr == 0 || fbaddr == -1)
252 1.1 garbled return false;
253 1.1 garbled
254 1.1 garbled /* initialize rasops */
255 1.1 garbled ri->ri_width = width;
256 1.1 garbled ri->ri_height = height;
257 1.1 garbled ri->ri_depth = depth;
258 1.1 garbled ri->ri_stride = linebytes;
259 1.1 garbled ri->ri_bits = (char *)fbaddr;
260 1.5 macallan ri->ri_flg = RI_CENTER | RI_FULLCLEAR | RI_NO_AUTO;
261 1.1 garbled
262 1.1 garbled /* mimic firmware output if we can find the ROM font */
263 1.1 garbled if (romfont_loaded) {
264 1.14 macallan int cols = 0, rows = 0;
265 1.1 garbled
266 1.4 kiyohara /*
267 1.4 kiyohara * XXX this assumes we're the console which may or may not
268 1.4 kiyohara * be the case
269 1.1 garbled */
270 1.1 garbled OF_interpret("#lines", 0, 1, &rows);
271 1.1 garbled OF_interpret("#columns", 0, 1, &cols);
272 1.1 garbled ri->ri_font = &openfirm6x11;
273 1.1 garbled ri->ri_wsfcookie = -1; /* not using wsfont */
274 1.1 garbled rasops_init(ri, rows, cols);
275 1.14 macallan #ifdef RASCONS_DEBUG
276 1.14 macallan char buffer[128];
277 1.14 macallan snprintf(buffer, 128, "bits %08x c %d w %d -> %d %d\n",
278 1.14 macallan (uint32_t)ri->ri_bits, cols, width, ri->ri_xorigin, ri->ri_yorigin);
279 1.14 macallan OF_write(console_instance, buffer, strlen(buffer));
280 1.14 macallan #endif
281 1.1 garbled } else {
282 1.1 garbled /* use as much of the screen as the font permits */
283 1.1 garbled rasops_init(ri, height/8, width/8);
284 1.1 garbled ri->ri_caps = WSSCREEN_WSCOLORS;
285 1.1 garbled rasops_reconfig(ri, height / ri->ri_font->fontheight,
286 1.1 garbled width / ri->ri_font->fontwidth);
287 1.1 garbled }
288 1.1 garbled
289 1.17 rin #ifdef macppc
290 1.16 rin if (depth == 8 && ofw_quiesce) {
291 1.16 rin /*
292 1.16 rin * Open Firmware will be quiesced. This is last chance to
293 1.16 rin * set color palette via ``color!'' method.
294 1.16 rin */
295 1.16 rin for (int i = 0; i < 256; i++) {
296 1.16 rin OF_call_method_1("color!", console_instance, 4,
297 1.16 rin rasops_cmap[3 * i], rasops_cmap[3 * i + 1],
298 1.16 rin rasops_cmap[3 * i + 2], i);
299 1.16 rin }
300 1.16 rin }
301 1.17 rin #endif
302 1.16 rin
303 1.1 garbled return true;
304 1.1 garbled }
305 1.1 garbled #else /* NWSDISPLAY > 0 */
306 1.1 garbled int
307 1.3 cegger rascons_cnattach(void)
308 1.1 garbled {
309 1.1 garbled return -1;
310 1.1 garbled }
311 1.1 garbled #endif
312