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