2 * Copyright (c) 2002 Bang Jun-Young
3 * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * from: NetBSD: machfb.c,v 1.23 2005/03/07 21:45:24 martin Exp
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 * Driver for ATI Mach64 graphics chips. Some code is derived from the
36 * ATI Rage Pro and Derivatives Programmer's Guide.
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/consio.h>
43 #include <sys/endian.h>
44 #include <sys/eventhandler.h>
46 #include <sys/kernel.h>
47 #include <sys/module.h>
48 #include <sys/resource.h>
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/openfirm.h>
56 #include <machine/bus.h>
57 #include <machine/bus_private.h>
58 #include <machine/ofw_machdep.h>
59 #include <machine/pmap.h>
60 #include <machine/resource.h>
61 #include <machine/sc_machdep.h>
65 #include <dev/fb/fbreg.h>
66 #include <dev/fb/gfb.h>
67 #include <dev/fb/machfbreg.h>
68 #include <dev/pci/pcivar.h>
69 #include <dev/pci/pcireg.h>
70 #include <dev/syscons/syscons.h>
72 /* #define MACHFB_DEBUG */
74 #define MACHFB_DRIVER_NAME "machfb"
76 #define MACH64_REG_OFF 0x7ffc00
77 #define MACH64_REG_SIZE 1024
80 video_adapter_t sc_va; /* must be first */
86 struct resource *sc_memres;
87 struct resource *sc_vmemres;
88 bus_space_tag_t sc_memt;
89 bus_space_tag_t sc_regt;
90 bus_space_tag_t sc_vmemt;
91 bus_space_handle_t sc_memh;
92 bus_space_handle_t sc_vmemh;
93 bus_space_handle_t sc_regh;
106 u_int sc_ramdac_freq;
110 u_int sc_mclk_post_div;
111 u_int sc_mclk_fb_div;
113 const u_char *sc_font;
115 vm_offset_t sc_curoff;
120 #define MACHFB_DRAW_CHAR (1 << 0)
121 #define MACHFB_DRAW_FILLRECT (1 << 1)
124 #define MACHFB_CONSOLE (1 << 0)
125 #define MACHFB_CUREN (1 << 1)
126 #define MACHFB_DSP (1 << 2)
127 #define MACHFB_SWAP (1 << 3)
130 static const struct {
133 uint32_t ramdac_freq;
135 { ATI_MACH64_CT, "ATI Mach64 CT", 135000 },
136 { ATI_RAGE_PRO_AGP, "ATI 3D Rage Pro (AGP)", 230000 },
137 { ATI_RAGE_PRO_AGP1X, "ATI 3D Rage Pro (AGP 1x)", 230000 },
138 { ATI_RAGE_PRO_PCI_B, "ATI 3D Rage Pro Turbo", 230000 },
139 { ATI_RAGE_XC_PCI66, "ATI Rage XL (PCI66)", 230000 },
140 { ATI_RAGE_XL_AGP, "ATI Rage XL (AGP)", 230000 },
141 { ATI_RAGE_XC_AGP, "ATI Rage XC (AGP)", 230000 },
142 { ATI_RAGE_XL_PCI66, "ATI Rage XL (PCI66)", 230000 },
143 { ATI_RAGE_PRO_PCI_P, "ATI 3D Rage Pro", 230000 },
144 { ATI_RAGE_PRO_PCI_L, "ATI 3D Rage Pro (limited 3D)", 230000 },
145 { ATI_RAGE_XL_PCI, "ATI Rage XL", 230000 },
146 { ATI_RAGE_XC_PCI, "ATI Rage XC", 230000 },
147 { ATI_RAGE_II, "ATI 3D Rage I/II", 135000 },
148 { ATI_RAGE_IIP, "ATI 3D Rage II+", 200000 },
149 { ATI_RAGE_IIC_PCI, "ATI 3D Rage IIC", 230000 },
150 { ATI_RAGE_IIC_AGP_B, "ATI 3D Rage IIC (AGP)", 230000 },
151 { ATI_RAGE_IIC_AGP_P, "ATI 3D Rage IIC (AGP)", 230000 },
152 { ATI_RAGE_LT_PRO_AGP, "ATI 3D Rage LT Pro (AGP 133MHz)", 230000 },
153 { ATI_RAGE_MOB_M3_PCI, "ATI Rage Mobility M3", 230000 },
154 { ATI_RAGE_MOB_M3_AGP, "ATI Rage Mobility M3 (AGP)", 230000 },
155 { ATI_RAGE_LT, "ATI 3D Rage LT", 230000 },
156 { ATI_RAGE_LT_PRO_PCI, "ATI 3D Rage LT Pro", 230000 },
157 { ATI_RAGE_MOBILITY, "ATI Rage Mobility", 230000 },
158 { ATI_RAGE_L_MOBILITY, "ATI Rage L Mobility", 230000 },
159 { ATI_RAGE_LT_PRO, "ATI 3D Rage LT Pro", 230000 },
160 { ATI_RAGE_LT_PRO2, "ATI 3D Rage LT Pro", 230000 },
161 { ATI_RAGE_MOB_M1_PCI, "ATI Rage Mobility M1 (PCI)", 230000 },
162 { ATI_RAGE_L_MOB_M1_PCI, "ATI Rage L Mobility (PCI)", 230000 },
163 { ATI_MACH64_VT, "ATI Mach64 VT", 170000 },
164 { ATI_MACH64_VTB, "ATI Mach64 VTB", 200000 },
165 { ATI_MACH64_VT4, "ATI Mach64 VT4", 230000 }
168 static const struct machfb_cmap {
172 } machfb_default_cmap[16] = {
173 {0x00, 0x00, 0x00}, /* black */
174 {0x00, 0x00, 0xff}, /* blue */
175 {0x00, 0xff, 0x00}, /* green */
176 {0x00, 0xc0, 0xc0}, /* cyan */
177 {0xff, 0x00, 0x00}, /* red */
178 {0xc0, 0x00, 0xc0}, /* magenta */
179 {0xc0, 0xc0, 0x00}, /* brown */
180 {0xc0, 0xc0, 0xc0}, /* light grey */
181 {0x80, 0x80, 0x80}, /* dark grey */
182 {0x80, 0x80, 0xff}, /* light blue */
183 {0x80, 0xff, 0x80}, /* light green */
184 {0x80, 0xff, 0xff}, /* light cyan */
185 {0xff, 0x80, 0x80}, /* light red */
186 {0xff, 0x80, 0xff}, /* light magenta */
187 {0xff, 0xff, 0x80}, /* yellow */
188 {0xff, 0xff, 0xff} /* white */
191 #define MACHFB_CMAP_OFF 16
193 static const u_char machfb_mouse_pointer_bits[64][8] = {
194 { 0x00, 0x00, }, /* ............ */
195 { 0x80, 0x00, }, /* *........... */
196 { 0xc0, 0x00, }, /* **.......... */
197 { 0xe0, 0x00, }, /* ***......... */
198 { 0xf0, 0x00, }, /* ****........ */
199 { 0xf8, 0x00, }, /* *****....... */
200 { 0xfc, 0x00, }, /* ******...... */
201 { 0xfe, 0x00, }, /* *******..... */
202 { 0xff, 0x00, }, /* ********.... */
203 { 0xff, 0x80, }, /* *********... */
204 { 0xfc, 0xc0, }, /* ******..**.. */
205 { 0xdc, 0x00, }, /* **.***...... */
206 { 0x8e, 0x00, }, /* *...***..... */
207 { 0x0e, 0x00, }, /* ....***..... */
208 { 0x07, 0x00, }, /* .....***.... */
209 { 0x04, 0x00, }, /* .....*...... */
210 { 0x00, 0x00, }, /* ............ */
211 { 0x00, 0x00, }, /* ............ */
212 { 0x00, 0x00, }, /* ............ */
213 { 0x00, 0x00, }, /* ............ */
214 { 0x00, 0x00, }, /* ............ */
215 { 0x00, 0x00, }, /* ............ */
219 * Lookup table to perform a bit-swap of the mouse pointer bits,
220 * map set bits to CUR_CLR0 and unset bits to transparent.
222 static const u_char machfb_mouse_pointer_lut[] = {
223 0xaa, 0x2a, 0x8a, 0x0a, 0xa2, 0x22, 0x82, 0x02,
224 0xa8, 0x28, 0x88, 0x08, 0xa0, 0x20, 0x80, 0x00
227 static const char *const machfb_memtype_names[] = {
228 "(N/A)", "DRAM", "EDO DRAM", "EDO DRAM", "SDRAM", "SGRAM", "WRAM",
232 extern const struct gfb_font gallant12x22;
234 static struct machfb_softc machfb_softc;
235 static struct bus_space_tag machfb_bst_store[1];
237 static device_probe_t machfb_pci_probe;
238 static device_attach_t machfb_pci_attach;
239 static device_detach_t machfb_pci_detach;
241 static device_method_t machfb_methods[] = {
242 /* Device interface */
243 DEVMETHOD(device_probe, machfb_pci_probe),
244 DEVMETHOD(device_attach, machfb_pci_attach),
245 DEVMETHOD(device_detach, machfb_pci_detach),
250 static driver_t machfb_pci_driver = {
253 sizeof(struct machfb_softc),
256 static devclass_t machfb_devclass;
258 DRIVER_MODULE(machfb, pci, machfb_pci_driver, machfb_devclass, 0, 0);
259 MODULE_DEPEND(machfb, pci, 1, 1, 1);
261 static void machfb_cursor_enable(struct machfb_softc *, int);
262 static int machfb_cursor_install(struct machfb_softc *);
263 static int machfb_get_memsize(struct machfb_softc *);
264 static void machfb_reset_engine(struct machfb_softc *);
265 static void machfb_init_engine(struct machfb_softc *);
267 static void machfb_adjust_frame(struct machfb_softc *, int, int);
269 static void machfb_shutdown_final(void *);
270 static void machfb_shutdown_reset(void *);
272 static int machfb_configure(int);
274 static vi_probe_t machfb_probe;
275 static vi_init_t machfb_init;
276 static vi_get_info_t machfb_get_info;
277 static vi_query_mode_t machfb_query_mode;
278 static vi_set_mode_t machfb_set_mode;
279 static vi_save_font_t machfb_save_font;
280 static vi_load_font_t machfb_load_font;
281 static vi_show_font_t machfb_show_font;
282 static vi_save_palette_t machfb_save_palette;
283 static vi_load_palette_t machfb_load_palette;
284 static vi_set_border_t machfb_set_border;
285 static vi_save_state_t machfb_save_state;
286 static vi_load_state_t machfb_load_state;
287 static vi_set_win_org_t machfb_set_win_org;
288 static vi_read_hw_cursor_t machfb_read_hw_cursor;
289 static vi_set_hw_cursor_t machfb_set_hw_cursor;
290 static vi_set_hw_cursor_shape_t machfb_set_hw_cursor_shape;
291 static vi_blank_display_t machfb_blank_display;
292 static vi_mmap_t machfb_mmap;
293 static vi_ioctl_t machfb_ioctl;
294 static vi_clear_t machfb_clear;
295 static vi_fill_rect_t machfb_fill_rect;
296 static vi_bitblt_t machfb_bitblt;
297 static vi_diag_t machfb_diag;
298 static vi_save_cursor_palette_t machfb_save_cursor_palette;
299 static vi_load_cursor_palette_t machfb_load_cursor_palette;
300 static vi_copy_t machfb_copy;
301 static vi_putp_t machfb_putp;
302 static vi_putc_t machfb_putc;
303 static vi_puts_t machfb_puts;
304 static vi_putm_t machfb_putm;
306 static video_switch_t machfbvidsw = {
307 .probe = machfb_probe,
309 .get_info = machfb_get_info,
310 .query_mode = machfb_query_mode,
311 .set_mode = machfb_set_mode,
312 .save_font = machfb_save_font,
313 .load_font = machfb_load_font,
314 .show_font = machfb_show_font,
315 .save_palette = machfb_save_palette,
316 .load_palette = machfb_load_palette,
317 .set_border = machfb_set_border,
318 .save_state = machfb_save_state,
319 .load_state = machfb_load_state,
320 .set_win_org = machfb_set_win_org,
321 .read_hw_cursor = machfb_read_hw_cursor,
322 .set_hw_cursor = machfb_set_hw_cursor,
323 .set_hw_cursor_shape = machfb_set_hw_cursor_shape,
324 .blank_display = machfb_blank_display,
326 .ioctl = machfb_ioctl,
327 .clear = machfb_clear,
328 .fill_rect = machfb_fill_rect,
329 .bitblt = machfb_bitblt,
331 .save_cursor_palette = machfb_save_cursor_palette,
332 .load_cursor_palette = machfb_load_cursor_palette,
340 VIDEO_DRIVER(machfb, machfbvidsw, machfb_configure);
342 extern sc_rndr_sw_t txtrndrsw;
343 RENDERER(machfb, 0, txtrndrsw, gfb_set);
345 RENDERER_MODULE(machfb, gfb_set);
348 * Inline functions for getting access to register aperture.
350 static inline uint32_t regr(struct machfb_softc *, uint32_t);
351 static inline uint8_t regrb(struct machfb_softc *, uint32_t);
352 static inline void regw(struct machfb_softc *, uint32_t, uint32_t);
353 static inline void regwb(struct machfb_softc *, uint32_t, uint8_t);
354 static inline void regwb_pll(struct machfb_softc *, uint32_t, uint8_t);
356 static inline uint32_t
357 regr(struct machfb_softc *sc, uint32_t index)
360 return bus_space_read_4(sc->sc_regt, sc->sc_regh, index);
363 static inline uint8_t
364 regrb(struct machfb_softc *sc, uint32_t index)
367 return bus_space_read_1(sc->sc_regt, sc->sc_regh, index);
371 regw(struct machfb_softc *sc, uint32_t index, uint32_t data)
374 bus_space_write_4(sc->sc_regt, sc->sc_regh, index, data);
375 bus_space_barrier(sc->sc_regt, sc->sc_regh, index, 4,
376 BUS_SPACE_BARRIER_WRITE);
380 regwb(struct machfb_softc *sc, uint32_t index, uint8_t data)
383 bus_space_write_1(sc->sc_regt, sc->sc_regh, index, data);
384 bus_space_barrier(sc->sc_regt, sc->sc_regh, index, 1,
385 BUS_SPACE_BARRIER_WRITE);
389 regwb_pll(struct machfb_softc *sc, uint32_t index, uint8_t data)
392 regwb(sc, CLOCK_CNTL + 1, (index << 2) | PLL_WR_EN);
393 regwb(sc, CLOCK_CNTL + 2, data);
394 regwb(sc, CLOCK_CNTL + 1, (index << 2) & ~PLL_WR_EN);
398 wait_for_fifo(struct machfb_softc *sc, uint8_t v)
401 while ((regr(sc, FIFO_STAT) & 0xffff) > (0x8000 >> v))
406 wait_for_idle(struct machfb_softc *sc)
409 wait_for_fifo(sc, 16);
410 while ((regr(sc, GUI_STAT) & 1) != 0)
415 * Inline functions for setting the background and foreground colors.
417 static inline void machfb_setbg(struct machfb_softc *sc, int bg);
418 static inline void machfb_setfg(struct machfb_softc *sc, int fg);
421 machfb_setbg(struct machfb_softc *sc, int bg)
424 if (bg == sc->sc_bg_cache)
426 sc->sc_bg_cache = bg;
427 wait_for_fifo(sc, 1);
428 regw(sc, DP_BKGD_CLR, bg + MACHFB_CMAP_OFF);
432 machfb_setfg(struct machfb_softc *sc, int fg)
435 if (fg == sc->sc_fg_cache)
437 sc->sc_fg_cache = fg;
438 wait_for_fifo(sc, 1);
439 regw(sc, DP_FRGD_CLR, fg + MACHFB_CMAP_OFF);
443 * video driver interface
446 machfb_configure(int flags)
448 struct machfb_softc *sc;
449 phandle_t chosen, output;
456 * For the high-level console probing return the number of
457 * registered adapters.
459 if (!(flags & VIO_PROBE_ONLY)) {
460 for (i = 0; vid_find_adapter(MACHFB_DRIVER_NAME, i) >= 0; i++)
465 /* Low-level console probing and initialization. */
468 if (sc->sc_va.va_flags & V_ADP_REGISTERED)
471 if ((chosen = OF_finddevice("/chosen")) == -1) /* Quis contra nos? */
473 if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1)
475 if ((output = OF_instance_to_package(stdout)) == -1)
477 if ((OF_getprop(output, "vendor-id", &id, sizeof(id)) == -1) ||
480 if (OF_getprop(output, "device-id", &id, sizeof(id)) == -1)
482 for (i = 0; i < sizeof(machfb_info) / sizeof(machfb_info[0]); i++) {
483 if (id == machfb_info[i].chip_id) {
484 sc->sc_flags = MACHFB_CONSOLE;
485 sc->sc_node = output;
490 if (!(sc->sc_flags & MACHFB_CONSOLE))
493 if (OF_getprop(output, "revision-id", &sc->sc_chip_rev,
494 sizeof(sc->sc_chip_rev)) == -1)
496 if (OF_decode_addr(output, 0, &space, &addr) != 0)
498 sc->sc_memt = &machfb_bst_store[0];
499 sc->sc_memh = sparc64_fake_bustag(space, addr, sc->sc_memt);
500 sc->sc_regt = sc->sc_memt;
501 bus_space_subregion(sc->sc_regt, sc->sc_memh, MACH64_REG_OFF,
502 MACH64_REG_SIZE, &sc->sc_regh);
504 if (machfb_init(0, &sc->sc_va, 0) < 0)
508 /* Return number of found adapters. */
513 machfb_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
520 machfb_init(int unit, video_adapter_t *adp, int flags)
522 struct machfb_softc *sc;
527 uint8_t dac_mask, dac_rindex, dac_windex;
529 sc = (struct machfb_softc *)adp;
532 if ((regr(sc, CONFIG_CHIP_ID) & 0xffff) != sc->sc_chip_id)
535 sc->sc_ramdac_freq = 0;
536 for (i = 0; i < sizeof(machfb_info) / sizeof(machfb_info[0]); i++) {
537 if (sc->sc_chip_id == machfb_info[i].chip_id) {
538 sc->sc_ramdac_freq = machfb_info[i].ramdac_freq;
542 if (sc->sc_ramdac_freq == 0)
544 if (sc->sc_chip_id == ATI_RAGE_II && sc->sc_chip_rev & 0x07)
545 sc->sc_ramdac_freq = 170000;
547 vid_init_struct(adp, MACHFB_DRIVER_NAME, -1, unit);
549 if (OF_getprop(sc->sc_node, "height", &sc->sc_height,
550 sizeof(sc->sc_height)) == -1)
552 if (OF_getprop(sc->sc_node, "width", &sc->sc_width,
553 sizeof(sc->sc_width)) == -1)
555 if (OF_getprop(sc->sc_node, "depth", &sc->sc_depth,
556 sizeof(sc->sc_depth)) == -1)
558 if ((options = OF_finddevice("/options")) == -1)
560 if (OF_getprop(options, "screen-#rows", buf, sizeof(buf)) == -1)
562 vi->vi_height = strtol(buf, NULL, 10);
563 if (OF_getprop(options, "screen-#columns", buf, sizeof(buf)) == -1)
565 vi->vi_width = strtol(buf, NULL, 10);
566 vi->vi_cwidth = gallant12x22.width;
567 vi->vi_cheight = gallant12x22.height;
568 vi->vi_flags = V_INFO_COLOR;
569 vi->vi_mem_model = V_INFO_MM_OTHER;
571 sc->sc_font = gallant12x22.data;
572 sc->sc_cbwidth = howmany(vi->vi_cwidth, NBBY); /* width in bytes */
573 sc->sc_xmargin = (sc->sc_width - (vi->vi_width * vi->vi_cwidth)) / 2;
574 sc->sc_ymargin = (sc->sc_height - (vi->vi_height * vi->vi_cheight)) / 2;
576 if (sc->sc_chip_id != ATI_MACH64_CT &&
577 !((sc->sc_chip_id == ATI_MACH64_VT ||
578 sc->sc_chip_id == ATI_RAGE_II) &&
579 (sc->sc_chip_rev & 0x07) == 0))
580 sc->sc_flags |= MACHFB_DSP;
582 sc->sc_memsize = machfb_get_memsize(sc);
583 if (sc->sc_memsize == 8192)
584 /* The last page is used as register aperture. */
586 sc->sc_memtype = regr(sc, CONFIG_STAT0) & 0x07;
588 if ((sc->sc_chip_id >= ATI_RAGE_XC_PCI66 &&
589 sc->sc_chip_id <= ATI_RAGE_XL_PCI66) ||
590 (sc->sc_chip_id >= ATI_RAGE_XL_PCI &&
591 sc->sc_chip_id <= ATI_RAGE_XC_PCI))
592 sc->sc_ref_freq = 29498;
594 sc->sc_ref_freq = 14318;
596 regwb(sc, CLOCK_CNTL + 1, PLL_REF_DIV << 2);
597 sc->sc_ref_div = regrb(sc, CLOCK_CNTL + 2);
598 regwb(sc, CLOCK_CNTL + 1, MCLK_FB_DIV << 2);
599 sc->sc_mclk_fb_div = regrb(sc, CLOCK_CNTL + 2);
600 sc->sc_mem_freq = (2 * sc->sc_ref_freq * sc->sc_mclk_fb_div) /
601 (sc->sc_ref_div * 2);
602 sc->sc_mclk_post_div = (sc->sc_mclk_fb_div * 2 * sc->sc_ref_freq) /
603 (sc->sc_mem_freq * sc->sc_ref_div);
605 machfb_init_engine(sc);
607 machfb_adjust_frame(0, 0);
609 machfb_set_mode(adp, 0);
612 * Install our 16-color color map. This is done only once and with
613 * an offset of 16 on sparc64 as there the OBP driver expects white
614 * to be at index 0 and black at 255 (some versions also use 1 - 8
615 * for color text support or the full palette for the boot banner
616 * logo but no versions seems to use the ISO 6429-1983 color map).
617 * Otherwise the colors are inverted when back in the OFW.
619 dac_rindex = regrb(sc, DAC_RINDEX);
620 dac_windex = regrb(sc, DAC_WINDEX);
621 dac_mask = regrb(sc, DAC_MASK);
622 regwb(sc, DAC_MASK, 0xff);
623 regwb(sc, DAC_WINDEX, MACHFB_CMAP_OFF);
624 for (i = 0; i < 16; i++) {
625 regwb(sc, DAC_DATA, machfb_default_cmap[i].red);
626 regwb(sc, DAC_DATA, machfb_default_cmap[i].green);
627 regwb(sc, DAC_DATA, machfb_default_cmap[i].blue);
629 regwb(sc, DAC_MASK, dac_mask);
630 regwb(sc, DAC_RINDEX, dac_rindex);
631 regwb(sc, DAC_WINDEX, dac_windex);
633 machfb_blank_display(adp, V_DISPLAY_ON);
637 * Setting V_ADP_MODECHANGE serves as hack so machfb_set_mode()
638 * (which will invalidate our caches) is called as a precaution
639 * when the X server shuts down.
641 adp->va_flags |= V_ADP_COLOR | V_ADP_MODECHANGE | V_ADP_PALETTE |
642 V_ADP_BORDER | V_ADP_INITIALIZED;
643 if (vid_register(adp) < 0)
645 adp->va_flags |= V_ADP_REGISTERED;
651 machfb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
654 bcopy(&adp->va_info, info, sizeof(*info));
660 machfb_query_mode(video_adapter_t *adp, video_info_t *info)
667 machfb_set_mode(video_adapter_t *adp, int mode)
669 struct machfb_softc *sc;
671 sc = (struct machfb_softc *)adp;
673 sc->sc_bg_cache = -1;
674 sc->sc_fg_cache = -1;
675 sc->sc_draw_cache = 0;
681 machfb_save_font(video_adapter_t *adp, int page, int size, int width,
682 u_char *data, int c, int count)
689 machfb_load_font(video_adapter_t *adp, int page, int size, int width,
690 u_char *data, int c, int count)
697 machfb_show_font(video_adapter_t *adp, int page)
704 machfb_save_palette(video_adapter_t *adp, u_char *palette)
706 struct machfb_softc *sc;
708 uint8_t dac_mask, dac_rindex, dac_windex;
710 sc = (struct machfb_softc *)adp;
712 dac_rindex = regrb(sc, DAC_RINDEX);
713 dac_windex = regrb(sc, DAC_WINDEX);
714 dac_mask = regrb(sc, DAC_MASK);
715 regwb(sc, DAC_MASK, 0xff);
716 regwb(sc, DAC_RINDEX, 0x0);
717 for (i = 0; i < 256 * 3; i++)
718 palette[i] = regrb(sc, DAC_DATA);
719 regwb(sc, DAC_MASK, dac_mask);
720 regwb(sc, DAC_RINDEX, dac_rindex);
721 regwb(sc, DAC_WINDEX, dac_windex);
727 machfb_load_palette(video_adapter_t *adp, u_char *palette)
729 struct machfb_softc *sc;
731 uint8_t dac_mask, dac_rindex, dac_windex;
733 sc = (struct machfb_softc *)adp;
735 dac_rindex = regrb(sc, DAC_RINDEX);
736 dac_windex = regrb(sc, DAC_WINDEX);
737 dac_mask = regrb(sc, DAC_MASK);
738 regwb(sc, DAC_MASK, 0xff);
739 regwb(sc, DAC_WINDEX, 0x0);
740 for (i = 0; i < 256 * 3; i++)
741 regwb(sc, DAC_DATA, palette[i]);
742 regwb(sc, DAC_MASK, dac_mask);
743 regwb(sc, DAC_RINDEX, dac_rindex);
744 regwb(sc, DAC_WINDEX, dac_windex);
750 machfb_set_border(video_adapter_t *adp, int border)
752 struct machfb_softc *sc;
754 sc = (struct machfb_softc *)adp;
756 machfb_fill_rect(adp, border, 0, 0, sc->sc_width, sc->sc_ymargin);
757 machfb_fill_rect(adp, border, 0, sc->sc_height - sc->sc_ymargin,
758 sc->sc_width, sc->sc_ymargin);
759 machfb_fill_rect(adp, border, 0, 0, sc->sc_xmargin, sc->sc_height);
760 machfb_fill_rect(adp, border, sc->sc_width - sc->sc_xmargin, 0,
761 sc->sc_xmargin, sc->sc_height);
767 machfb_save_state(video_adapter_t *adp, void *p, size_t size)
774 machfb_load_state(video_adapter_t *adp, void *p)
781 machfb_set_win_org(video_adapter_t *adp, off_t offset)
788 machfb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
798 machfb_set_hw_cursor(video_adapter_t *adp, int col, int row)
805 machfb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
806 int celsize, int blink)
813 machfb_blank_display(video_adapter_t *adp, int mode)
815 struct machfb_softc *sc;
816 uint32_t crtc_gen_cntl;
818 sc = (struct machfb_softc *)adp;
820 crtc_gen_cntl = (regr(sc, CRTC_GEN_CNTL) | CRTC_EXT_DISP_EN | CRTC_EN) &
821 ~(CRTC_HSYNC_DIS | CRTC_VSYNC_DIS | CRTC_DISPLAY_DIS);
825 case V_DISPLAY_BLANK:
826 crtc_gen_cntl |= CRTC_HSYNC_DIS | CRTC_VSYNC_DIS |
829 case V_DISPLAY_STAND_BY:
830 crtc_gen_cntl |= CRTC_HSYNC_DIS | CRTC_DISPLAY_DIS;
832 case V_DISPLAY_SUSPEND:
833 crtc_gen_cntl |= CRTC_VSYNC_DIS | CRTC_DISPLAY_DIS;
836 regw(sc, CRTC_GEN_CNTL, crtc_gen_cntl);
842 machfb_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
843 int prot, vm_memattr_t *memattr)
845 struct machfb_softc *sc;
848 sc = (struct machfb_softc *)adp;
851 /* BAR 2 - VGA memory */
852 if (sc->sc_vmem != 0 && offset >= sc->sc_vmem &&
853 offset < sc->sc_vmem + vi->vi_registers_size) {
854 *paddr = vi->vi_registers + offset - sc->sc_vmem;
858 /* BAR 0 - framebuffer */
859 if (offset >= sc->sc_mem &&
860 offset < sc->sc_mem + vi->vi_buffer_size) {
861 *paddr = vi->vi_buffer + offset - sc->sc_mem;
865 /* 'regular' framebuffer mmap()ing */
866 if (offset < adp->va_window_size) {
867 *paddr = vi->vi_window + offset;
875 machfb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
877 struct machfb_softc *sc;
878 struct fbcursor *fbc;
881 sc = (struct machfb_softc *)adp;
885 fb = (struct fbtype *)data;
886 fb->fb_type = FBTYPE_PCIMISC;
887 fb->fb_height = sc->sc_height;
888 fb->fb_width = sc->sc_width;
889 fb->fb_depth = sc->sc_depth;
890 if (sc->sc_depth <= 1 || sc->sc_depth > 8)
893 fb->fb_cmsize = 1 << sc->sc_depth;
894 fb->fb_size = adp->va_buffer_size;
897 fbc = (struct fbcursor *)data;
898 if (fbc->set & FB_CUR_SETCUR && fbc->enable == 0) {
899 machfb_cursor_enable(sc, 0);
900 sc->sc_flags &= ~MACHFB_CUREN;
905 return (fb_commonioctl(adp, cmd, data));
912 machfb_clear(video_adapter_t *adp)
914 struct machfb_softc *sc;
916 sc = (struct machfb_softc *)adp;
918 machfb_fill_rect(adp, (SC_NORM_ATTR >> 4) & 0xf, 0, 0, sc->sc_width,
925 machfb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
927 struct machfb_softc *sc;
929 sc = (struct machfb_softc *)adp;
931 if (sc->sc_draw_cache != MACHFB_DRAW_FILLRECT) {
932 wait_for_fifo(sc, 7);
933 regw(sc, DP_WRITE_MASK, 0xff);
934 regw(sc, DP_PIX_WIDTH, DST_8BPP | SRC_8BPP | HOST_8BPP);
935 regw(sc, DP_SRC, FRGD_SRC_FRGD_CLR);
936 regw(sc, DP_MIX, MIX_SRC << 16);
937 regw(sc, CLR_CMP_CNTL, 0); /* no transparency */
938 regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
939 regw(sc, DST_CNTL, DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM);
940 sc->sc_draw_cache = MACHFB_DRAW_FILLRECT;
942 machfb_setfg(sc, val);
943 wait_for_fifo(sc, 4);
944 regw(sc, SRC_Y_X, (x << 16) | y);
945 regw(sc, SRC_WIDTH1, cx);
946 regw(sc, DST_Y_X, (x << 16) | y);
947 regw(sc, DST_HEIGHT_WIDTH, (cx << 16) | cy);
953 machfb_bitblt(video_adapter_t *adp, ...)
960 machfb_diag(video_adapter_t *adp, int level)
964 fb_dump_adp_info(adp->va_name, adp, level);
965 machfb_get_info(adp, 0, &info);
966 fb_dump_mode_info(adp->va_name, adp, &info, level);
972 machfb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
979 machfb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
986 machfb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
993 machfb_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p, uint32_t a,
994 int size, int bpp, int bit_ltor, int byte_ltor)
1001 machfb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
1003 struct machfb_softc *sc;
1007 sc = (struct machfb_softc *)adp;
1009 if (sc->sc_draw_cache != MACHFB_DRAW_CHAR) {
1010 wait_for_fifo(sc, 8);
1011 regw(sc, DP_WRITE_MASK, 0xff); /* XXX only good for 8 bit */
1012 regw(sc, DP_PIX_WIDTH, DST_8BPP | SRC_1BPP | HOST_1BPP);
1013 regw(sc, DP_SRC, MONO_SRC_HOST | BKGD_SRC_BKGD_CLR |
1015 regw(sc, DP_MIX ,((MIX_SRC & 0xffff) << 16) | MIX_SRC);
1016 regw(sc, CLR_CMP_CNTL, 0); /* no transparency */
1017 regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
1018 regw(sc, DST_CNTL, DST_Y_TOP_TO_BOTTOM | DST_X_LEFT_TO_RIGHT);
1019 regw(sc, HOST_CNTL, HOST_BYTE_ALIGN);
1020 sc->sc_draw_cache = MACHFB_DRAW_CHAR;
1022 machfb_setbg(sc, (a >> 4) & 0xf);
1023 machfb_setfg(sc, a & 0xf);
1024 wait_for_fifo(sc, 4 + (adp->va_info.vi_cheight / sc->sc_cbwidth));
1025 regw(sc, SRC_Y_X, 0);
1026 regw(sc, SRC_WIDTH1, adp->va_info.vi_cwidth);
1027 regw(sc, DST_Y_X, ((((off % adp->va_info.vi_width) *
1028 adp->va_info.vi_cwidth) + sc->sc_xmargin) << 16) |
1029 (((off / adp->va_info.vi_width) * adp->va_info.vi_cheight) +
1031 regw(sc, DST_HEIGHT_WIDTH, (adp->va_info.vi_cwidth << 16) |
1032 adp->va_info.vi_cheight);
1033 p = sc->sc_font + (c * adp->va_info.vi_cheight * sc->sc_cbwidth);
1034 for (i = 0; i < adp->va_info.vi_cheight * sc->sc_cbwidth; i += 4)
1035 regw(sc, HOST_DATA0 + i, (p[i + 3] << 24 | p[i + 2] << 16 |
1036 p[i + 1] << 8 | p[i]));
1042 machfb_puts(video_adapter_t *adp, vm_offset_t off, uint16_t *s, int len)
1044 struct machfb_softc *sc;
1045 int blanks, i, x1, x2, y1, y2;
1046 uint8_t a, c, color1, color2;
1048 sc = (struct machfb_softc *)adp;
1050 #define MACHFB_BLANK machfb_fill_rect(adp, color1, x1, y1, \
1051 blanks * adp->va_info.vi_cwidth, \
1052 adp->va_info.vi_cheight)
1054 blanks = color1 = x1 = y1 = 0;
1055 for (i = 0; i < len; i++) {
1057 * Accelerate continuous blanks by drawing a respective
1058 * rectangle instead. Drawing a rectangle of any size
1059 * takes about the same number of operations as drawing
1060 * a single character.
1063 a = (s[i] & 0xff00) >> 8;
1064 if (c == 0x00 || c == 0x20 || c == 0xdb || c == 0xff) {
1065 color2 = (a >> (c == 0xdb ? 0 : 4) & 0xf);
1066 x2 = (((off + i) % adp->va_info.vi_width) *
1067 adp->va_info.vi_cwidth) + sc->sc_xmargin;
1068 y2 = (((off + i) / adp->va_info.vi_width) *
1069 adp->va_info.vi_cheight) + sc->sc_ymargin;
1075 } else if (color1 != color2 || y1 != y2) {
1088 vidd_putc(adp, off + i, c, a);
1100 machfb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
1101 uint32_t pixel_mask, int size, int width)
1103 struct machfb_softc *sc;
1106 sc = (struct machfb_softc *)adp;
1108 if ((!(sc->sc_flags & MACHFB_CUREN)) &&
1109 (error = machfb_cursor_install(sc)) < 0)
1113 * The hardware cursor always must be disabled when
1114 * fiddling with its bits otherwise some artifacts
1115 * may appear on the screen.
1117 machfb_cursor_enable(sc, 0);
1120 regw(sc, CUR_HORZ_VERT_OFF, 0);
1121 if ((regr(sc, GEN_TEST_CNTL) & CRTC_DBL_SCAN_EN) != 0)
1123 regw(sc, CUR_HORZ_VERT_POSN, ((y + sc->sc_ymargin) << 16) |
1124 (x + sc->sc_xmargin));
1125 machfb_cursor_enable(sc, 1);
1126 sc->sc_flags |= MACHFB_CUREN;
1135 machfb_pci_probe(device_t dev)
1139 if (pci_get_class(dev) != PCIC_DISPLAY ||
1140 pci_get_subclass(dev) != PCIS_DISPLAY_VGA)
1143 for (i = 0; i < sizeof(machfb_info) / sizeof(machfb_info[0]); i++) {
1144 if (pci_get_device(dev) == machfb_info[i].chip_id) {
1145 device_set_desc(dev, machfb_info[i].name);
1146 return (BUS_PROBE_DEFAULT);
1154 machfb_pci_attach(device_t dev)
1156 struct machfb_softc *sc;
1157 video_adapter_t *adp;
1165 node = ofw_bus_get_node(dev);
1166 if ((sc = (struct machfb_softc *)vid_get_adapter(vid_find_adapter(
1167 MACHFB_DRIVER_NAME, 0))) != NULL && sc->sc_node == node) {
1168 device_printf(dev, "console\n");
1169 device_set_softc(dev, sc);
1171 sc = device_get_softc(dev);
1174 sc->sc_chip_id = pci_get_device(dev);
1175 sc->sc_chip_rev = pci_get_revid(dev);
1181 if ((sc->sc_memres = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1182 RF_ACTIVE)) == NULL) {
1183 device_printf(dev, "cannot allocate memory resources\n");
1186 sc->sc_memt = rman_get_bustag(sc->sc_memres);
1187 sc->sc_memh = rman_get_bushandle(sc->sc_memres);
1188 sc->sc_mem = rman_get_start(sc->sc_memres);
1189 vi->vi_buffer = sc->sc_memh;
1190 vi->vi_buffer_size = rman_get_size(sc->sc_memres);
1191 if (OF_getprop(sc->sc_node, "address", &u32, sizeof(u32)) > 0 &&
1192 vtophys(u32) == sc->sc_memh)
1193 adp->va_mem_base = u32;
1195 if (bus_space_map(sc->sc_memt, vi->vi_buffer,
1196 vi->vi_buffer_size, BUS_SPACE_MAP_LINEAR,
1197 &sc->sc_memh) != 0) {
1198 device_printf(dev, "cannot map memory resources\n");
1203 (vm_offset_t)rman_get_virtual(sc->sc_memres);
1205 adp->va_mem_size = vi->vi_buffer_size;
1206 adp->va_buffer = adp->va_mem_base;
1207 adp->va_buffer_size = adp->va_mem_size;
1208 sc->sc_regt = sc->sc_memt;
1209 if (bus_space_subregion(sc->sc_regt, sc->sc_memh, MACH64_REG_OFF,
1210 MACH64_REG_SIZE, &sc->sc_regh) != 0) {
1211 device_printf(dev, "cannot allocate register resources\n");
1217 * Depending on the firmware version the VGA I/O and/or memory
1218 * resources of the Mach64 chips come up disabled. These will be
1219 * enabled by pci(4) when activating the resource in question but
1220 * this doesn't necessarily mean that the resource is valid.
1221 * Invalid resources seem to have in common that they start at
1222 * address 0. We don't allocate the VGA memory in this case in
1223 * order to avoid warnings in apb(4) and crashes when using this
1224 * invalid resources. X.Org is aware of this and doesn't use the
1225 * VGA memory resource in this case (but demands it if it's valid).
1228 if (bus_get_resource_start(dev, SYS_RES_MEMORY, rid) != 0) {
1229 if ((sc->sc_vmemres = bus_alloc_resource_any(dev,
1230 SYS_RES_MEMORY, &rid, RF_ACTIVE)) == NULL) {
1232 "cannot allocate VGA memory resources\n");
1236 sc->sc_vmemt = rman_get_bustag(sc->sc_vmemres);
1237 sc->sc_vmemh = rman_get_bushandle(sc->sc_vmemres);
1238 sc->sc_vmem = rman_get_start(sc->sc_vmemres);
1239 vi->vi_registers = sc->sc_vmemh;
1240 vi->vi_registers_size = rman_get_size(sc->sc_vmemres);
1241 if (bus_space_map(sc->sc_vmemt, vi->vi_registers,
1242 vi->vi_registers_size, BUS_SPACE_MAP_LINEAR,
1243 &sc->sc_vmemh) != 0) {
1245 "cannot map VGA memory resources\n");
1250 (vm_offset_t)rman_get_virtual(sc->sc_vmemres);
1251 adp->va_registers_size = vi->vi_registers_size;
1254 if (!(sc->sc_flags & MACHFB_CONSOLE)) {
1255 if ((sw = vid_get_switch(MACHFB_DRIVER_NAME)) == NULL) {
1256 device_printf(dev, "cannot get video switch\n");
1261 * During device configuration we don't necessarily probe
1262 * the adapter which is the console first so we can't use
1263 * the device unit number for the video adapter unit. The
1264 * worst case would be that we use the video adapter unit
1265 * 0 twice. As it doesn't really matter which unit number
1266 * the corresponding video adapter has just use the next
1269 for (i = 0; i < devclass_get_maxunit(machfb_devclass); i++)
1270 if (vid_find_adapter(MACHFB_DRIVER_NAME, i) < 0)
1272 if ((error = sw->init(i, adp, 0)) != 0) {
1273 device_printf(dev, "cannot initialize adapter\n");
1279 * Test whether the aperture is byte swapped or not, set
1280 * va_window and va_window_size as appropriate. Note that
1281 * the aperture could be mapped either big or little endian
1282 * independently of the endianess of the host so this has
1283 * to be a runtime test.
1285 p32 = (uint32_t *)adp->va_buffer;
1287 p = (uint8_t *)adp->va_buffer;
1289 if (!(p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78)) {
1290 adp->va_window = adp->va_buffer + 0x800000;
1291 adp->va_window_size = adp->va_buffer_size - 0x800000;
1292 vi->vi_window = vi->vi_buffer + 0x800000;
1293 vi->vi_window_size = vi->vi_buffer_size - 0x800000;
1294 sc->sc_flags |= MACHFB_SWAP;
1296 adp->va_window = adp->va_buffer;
1297 adp->va_window_size = adp->va_buffer_size;
1298 vi->vi_window = vi->vi_buffer;
1299 vi->vi_window_size = vi->vi_buffer_size;
1302 adp->va_window_gran = adp->va_window_size;
1305 "%d MB aperture at %p %sswapped\n",
1306 (u_int)(adp->va_window_size / (1024 * 1024)),
1307 (void *)adp->va_window, (sc->sc_flags & MACHFB_SWAP) ?
1310 "%ld KB %s %d.%d MHz, maximum RAMDAC clock %d MHz, %sDSP\n",
1311 (u_long)sc->sc_memsize, machfb_memtype_names[sc->sc_memtype],
1312 sc->sc_mem_freq / 1000, sc->sc_mem_freq % 1000,
1313 sc->sc_ramdac_freq / 1000,
1314 (sc->sc_flags & MACHFB_DSP) ? "" : "no ");
1315 device_printf(dev, "resolution %dx%d at %d bpp\n",
1316 sc->sc_width, sc->sc_height, sc->sc_depth);
1319 * Allocate one page for the mouse pointer image at the end of
1320 * the little endian aperture, right before the memory mapped
1321 * registers that might also reside there. Must be done after
1322 * sc_memsize was set and possibly adjusted to account for the
1323 * memory mapped registers.
1325 sc->sc_curoff = (sc->sc_memsize * 1024) - PAGE_SIZE;
1326 sc->sc_memsize -= PAGE_SIZE / 1024;
1327 machfb_cursor_enable(sc, 0);
1328 /* Initialize with an all transparent image. */
1329 memset((void *)(adp->va_buffer + sc->sc_curoff), 0xaa, PAGE_SIZE);
1332 * Register a handler that performs some cosmetic surgery like
1333 * turning off the mouse pointer on halt in preparation for
1334 * handing the screen over to the OFW. Register another handler
1335 * that turns off the CRTC when resetting, otherwise the OFW
1336 * boot command issued by cpu_reset() just doesn't work.
1338 EVENTHANDLER_REGISTER(shutdown_final, machfb_shutdown_final, sc,
1339 SHUTDOWN_PRI_DEFAULT);
1340 EVENTHANDLER_REGISTER(shutdown_reset, machfb_shutdown_reset, sc,
1341 SHUTDOWN_PRI_DEFAULT);
1346 if (adp->va_registers != 0)
1347 bus_space_unmap(sc->sc_vmemt, sc->sc_vmemh,
1348 vi->vi_registers_size);
1350 if (sc->sc_vmemres != NULL)
1351 bus_release_resource(dev, SYS_RES_MEMORY,
1352 rman_get_rid(sc->sc_vmemres), sc->sc_vmemres);
1354 bus_space_unmap(sc->sc_memt, sc->sc_memh, vi->vi_buffer_size);
1356 bus_release_resource(dev, SYS_RES_MEMORY,
1357 rman_get_rid(sc->sc_memres), sc->sc_memres);
1363 machfb_pci_detach(device_t dev)
1370 * internal functions
1373 machfb_cursor_enable(struct machfb_softc *sc, int onoff)
1377 regw(sc, GEN_TEST_CNTL,
1378 regr(sc, GEN_TEST_CNTL) | HWCURSOR_ENABLE);
1380 regw(sc, GEN_TEST_CNTL,
1381 regr(sc, GEN_TEST_CNTL) &~ HWCURSOR_ENABLE);
1385 machfb_cursor_install(struct machfb_softc *sc)
1391 if (sc->sc_curoff == 0)
1394 machfb_cursor_enable(sc, 0);
1395 regw(sc, CUR_OFFSET, sc->sc_curoff >> 3);
1396 fg = SC_NORM_ATTR & 0xf;
1397 regw(sc, CUR_CLR0, machfb_default_cmap[fg].red << 24 |
1398 machfb_default_cmap[fg].green << 16 |
1399 machfb_default_cmap[fg].blue << 8);
1400 p = (uint16_t *)(sc->sc_va.va_buffer + sc->sc_curoff);
1401 for (i = 0; i < 64; i++) {
1402 for (j = 0; j < 8; j++) {
1403 v = machfb_mouse_pointer_lut[
1404 machfb_mouse_pointer_bits[i][j] >> 4] << 8 |
1405 machfb_mouse_pointer_lut[
1406 machfb_mouse_pointer_bits[i][j] & 0x0f];
1407 if (sc->sc_flags & MACHFB_SWAP)
1408 *(p++) = bswap16(v);
1418 machfb_get_memsize(struct machfb_softc *sc)
1421 const int mem_tab[] = {
1422 512, 1024, 2048, 4096, 6144, 8192, 12288, 16384
1425 tmp = regr(sc, MEM_CNTL);
1427 printf("memcntl=0x%08x\n", tmp);
1429 if (sc->sc_flags & MACHFB_DSP) {
1432 memsize = (tmp + 1) * 512;
1434 memsize = (tmp - 3) * 1024;
1436 memsize = (tmp - 7) * 2048;
1438 memsize = mem_tab[tmp & 0x07];
1444 machfb_reset_engine(struct machfb_softc *sc)
1448 regw(sc, GEN_TEST_CNTL, regr(sc, GEN_TEST_CNTL) & ~GUI_ENGINE_ENABLE);
1450 /* Enable engine. */
1451 regw(sc, GEN_TEST_CNTL, regr(sc, GEN_TEST_CNTL) | GUI_ENGINE_ENABLE);
1454 * Ensure engine is not locked up by clearing any FIFO or
1457 regw(sc, BUS_CNTL, regr(sc, BUS_CNTL) | BUS_HOST_ERR_ACK |
1462 machfb_init_engine(struct machfb_softc *sc)
1464 uint32_t pitch_value;
1466 pitch_value = sc->sc_width;
1468 if (sc->sc_depth == 24)
1471 machfb_reset_engine(sc);
1473 wait_for_fifo(sc, 14);
1475 regw(sc, CONTEXT_MASK, 0xffffffff);
1477 regw(sc, DST_OFF_PITCH, (pitch_value / 8) << 22);
1479 regw(sc, DST_Y_X, 0);
1480 regw(sc, DST_HEIGHT, 0);
1481 regw(sc, DST_BRES_ERR, 0);
1482 regw(sc, DST_BRES_INC, 0);
1483 regw(sc, DST_BRES_DEC, 0);
1485 regw(sc, DST_CNTL, DST_LAST_PEL | DST_X_LEFT_TO_RIGHT |
1486 DST_Y_TOP_TO_BOTTOM);
1488 regw(sc, SRC_OFF_PITCH, (pitch_value / 8) << 22);
1490 regw(sc, SRC_Y_X, 0);
1491 regw(sc, SRC_HEIGHT1_WIDTH1, 1);
1492 regw(sc, SRC_Y_X_START, 0);
1493 regw(sc, SRC_HEIGHT2_WIDTH2, 1);
1495 regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
1497 wait_for_fifo(sc, 13);
1498 regw(sc, HOST_CNTL, 0);
1500 regw(sc, PAT_REG0, 0);
1501 regw(sc, PAT_REG1, 0);
1502 regw(sc, PAT_CNTL, 0);
1504 regw(sc, SC_LEFT, 0);
1505 regw(sc, SC_TOP, 0);
1506 regw(sc, SC_BOTTOM, sc->sc_height - 1);
1507 regw(sc, SC_RIGHT, pitch_value - 1);
1509 regw(sc, DP_BKGD_CLR, 0);
1510 regw(sc, DP_FRGD_CLR, 0xffffffff);
1511 regw(sc, DP_WRITE_MASK, 0xffffffff);
1512 regw(sc, DP_MIX, (MIX_SRC << 16) | MIX_DST);
1514 regw(sc, DP_SRC, FRGD_SRC_FRGD_CLR);
1516 wait_for_fifo(sc, 3);
1517 regw(sc, CLR_CMP_CLR, 0);
1518 regw(sc, CLR_CMP_MASK, 0xffffffff);
1519 regw(sc, CLR_CMP_CNTL, 0);
1521 wait_for_fifo(sc, 2);
1522 switch (sc->sc_depth) {
1524 regw(sc, DP_PIX_WIDTH, HOST_8BPP | SRC_8BPP | DST_8BPP);
1525 regw(sc, DP_CHAIN_MASK, DP_CHAIN_8BPP);
1526 regw(sc, DAC_CNTL, regr(sc, DAC_CNTL) | DAC_8BIT_EN);
1530 regw(sc, DP_PIX_WIDTH, HOST_32BPP | SRC_32BPP | DST_32BPP);
1531 regw(sc, DP_CHAIN_MASK, DP_CHAIN_32BPP);
1532 regw(sc, DAC_CNTL, regr(sc, DAC_CNTL) | DAC_8BIT_EN);
1537 wait_for_fifo(sc, 2);
1538 regw(sc, CRTC_INT_CNTL, regr(sc, CRTC_INT_CNTL) & ~0x20);
1539 regw(sc, GUI_TRAJ_CNTL, DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM);
1546 machfb_adjust_frame(struct machfb_softc *sc, int x, int y)
1550 offset = ((x + y * sc->sc_width) * (sc->sc_depth >> 3)) >> 3;
1552 regw(sc, CRTC_OFF_PITCH, (regr(sc, CRTC_OFF_PITCH) & 0xfff00000) |
1558 machfb_shutdown_final(void *v)
1560 struct machfb_softc *sc = v;
1562 machfb_cursor_enable(sc, 0);
1564 * In case this is the console set the cursor of the stdout
1565 * instance to the start of the last line so OFW output ends
1566 * up beneath what FreeBSD left on the screen.
1568 if (sc->sc_flags & MACHFB_CONSOLE) {
1569 OF_interpret("stdout @ is my-self 0 to column#", 0);
1570 OF_interpret("stdout @ is my-self #lines 1 - to line#", 0);
1575 machfb_shutdown_reset(void *v)
1577 struct machfb_softc *sc = v;
1579 machfb_blank_display(&sc->sc_va, V_DISPLAY_STAND_BY);