]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/fb/machfb.c
rtwn_pci(4): add support for RTL8188EE chipset.
[FreeBSD/FreeBSD.git] / sys / dev / fb / machfb.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2002 Bang Jun-Young
5  * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  *      from: NetBSD: machfb.c,v 1.23 2005/03/07 21:45:24 martin Exp
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 /*
37  * Driver for ATI Mach64 graphics chips.  Some code is derived from the
38  * ATI Rage Pro and Derivatives Programmer's Guide.
39  */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/bus.h>
44 #include <sys/consio.h>
45 #include <sys/endian.h>
46 #include <sys/eventhandler.h>
47 #include <sys/fbio.h>
48 #include <sys/kernel.h>
49 #include <sys/module.h>
50 #include <sys/resource.h>
51
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54
55 #include <dev/ofw/ofw_bus.h>
56 #include <dev/ofw/openfirm.h>
57
58 #include <machine/bus.h>
59 #include <machine/bus_private.h>
60 #include <machine/ofw_machdep.h>
61 #include <machine/resource.h>
62 #include <machine/sc_machdep.h>
63
64 #include <sys/rman.h>
65
66 #include <dev/fb/fbreg.h>
67 #include <dev/fb/gfb.h>
68 #include <dev/fb/machfbreg.h>
69 #include <dev/pci/pcivar.h>
70 #include <dev/pci/pcireg.h>
71 #include <dev/syscons/syscons.h>
72
73 /* #define MACHFB_DEBUG */
74
75 #define MACHFB_DRIVER_NAME      "machfb"
76
77 #define MACH64_REG_OFF          0x7ffc00
78 #define MACH64_REG_SIZE         1024
79
80 struct machfb_softc {
81         video_adapter_t         sc_va;          /* must be first */
82
83         phandle_t               sc_node;
84         uint16_t                sc_chip_id;
85         uint8_t                 sc_chip_rev;
86
87         struct resource         *sc_memres;
88         struct resource         *sc_vmemres;
89         bus_space_tag_t         sc_memt;
90         bus_space_tag_t         sc_regt;
91         bus_space_tag_t         sc_vmemt;
92         bus_space_handle_t      sc_memh;
93         bus_space_handle_t      sc_vmemh;
94         bus_space_handle_t      sc_regh;
95         u_long                  sc_mem;
96         u_long                  sc_vmem;
97
98         u_int                   sc_height;
99         u_int                   sc_width;
100         u_int                   sc_depth;
101         u_int                   sc_xmargin;
102         u_int                   sc_ymargin;
103
104         size_t                  sc_memsize;
105         u_int                   sc_memtype;
106         u_int                   sc_mem_freq;
107         u_int                   sc_ramdac_freq;
108         u_int                   sc_ref_freq;
109
110         u_int                   sc_ref_div;
111         u_int                   sc_mclk_post_div;
112         u_int                   sc_mclk_fb_div;
113
114         const u_char            *sc_font;
115         u_int                   sc_cbwidth;
116         vm_offset_t             sc_curoff;
117
118         int                     sc_bg_cache;
119         int                     sc_fg_cache;
120         u_int                   sc_draw_cache;
121 #define MACHFB_DRAW_CHAR        (1 << 0)
122 #define MACHFB_DRAW_FILLRECT    (1 << 1)
123
124         u_int                   sc_flags;
125 #define MACHFB_CONSOLE          (1 << 0)
126 #define MACHFB_CUREN            (1 << 1)
127 #define MACHFB_DSP              (1 << 2)
128 #define MACHFB_SWAP             (1 << 3)
129 };
130
131 static const struct {
132         uint16_t        chip_id;
133         const char      *name;
134         uint32_t        ramdac_freq;
135 } machfb_info[] = {
136         { ATI_MACH64_CT, "ATI Mach64 CT", 135000 },
137         { ATI_RAGE_PRO_AGP, "ATI 3D Rage Pro (AGP)", 230000 },
138         { ATI_RAGE_PRO_AGP1X, "ATI 3D Rage Pro (AGP 1x)", 230000 },
139         { ATI_RAGE_PRO_PCI_B, "ATI 3D Rage Pro Turbo", 230000 },
140         { ATI_RAGE_XC_PCI66, "ATI Rage XL (PCI66)", 230000 },
141         { ATI_RAGE_XL_AGP, "ATI Rage XL (AGP)", 230000 },
142         { ATI_RAGE_XC_AGP, "ATI Rage XC (AGP)", 230000 },
143         { ATI_RAGE_XL_PCI66, "ATI Rage XL (PCI66)", 230000 },
144         { ATI_RAGE_PRO_PCI_P, "ATI 3D Rage Pro", 230000 },
145         { ATI_RAGE_PRO_PCI_L, "ATI 3D Rage Pro (limited 3D)", 230000 },
146         { ATI_RAGE_XL_PCI, "ATI Rage XL", 230000 },
147         { ATI_RAGE_XC_PCI, "ATI Rage XC", 230000 },
148         { ATI_RAGE_II, "ATI 3D Rage I/II", 135000 },
149         { ATI_RAGE_IIP, "ATI 3D Rage II+", 200000 },
150         { ATI_RAGE_IIC_PCI, "ATI 3D Rage IIC", 230000 },
151         { ATI_RAGE_IIC_AGP_B, "ATI 3D Rage IIC (AGP)", 230000 },
152         { ATI_RAGE_IIC_AGP_P, "ATI 3D Rage IIC (AGP)", 230000 },
153         { ATI_RAGE_LT_PRO_AGP, "ATI 3D Rage LT Pro (AGP 133MHz)", 230000 },
154         { ATI_RAGE_MOB_M3_PCI, "ATI Rage Mobility M3", 230000 },
155         { ATI_RAGE_MOB_M3_AGP, "ATI Rage Mobility M3 (AGP)", 230000 },
156         { ATI_RAGE_LT, "ATI 3D Rage LT", 230000 },
157         { ATI_RAGE_LT_PRO_PCI, "ATI 3D Rage LT Pro", 230000 },
158         { ATI_RAGE_MOBILITY, "ATI Rage Mobility", 230000 },
159         { ATI_RAGE_L_MOBILITY, "ATI Rage L Mobility", 230000 },
160         { ATI_RAGE_LT_PRO, "ATI 3D Rage LT Pro", 230000 },
161         { ATI_RAGE_LT_PRO2, "ATI 3D Rage LT Pro", 230000 },
162         { ATI_RAGE_MOB_M1_PCI, "ATI Rage Mobility M1 (PCI)", 230000 },
163         { ATI_RAGE_L_MOB_M1_PCI, "ATI Rage L Mobility (PCI)", 230000 },
164         { ATI_MACH64_VT, "ATI Mach64 VT", 170000 },
165         { ATI_MACH64_VTB, "ATI Mach64 VTB", 200000 },
166         { ATI_MACH64_VT4, "ATI Mach64 VT4", 230000 }
167 };
168
169 static const struct machfb_cmap {
170         uint8_t red;
171         uint8_t green;
172         uint8_t blue;
173 } machfb_default_cmap[16] = {
174         {0x00, 0x00, 0x00},     /* black */
175         {0x00, 0x00, 0xff},     /* blue */
176         {0x00, 0xff, 0x00},     /* green */
177         {0x00, 0xc0, 0xc0},     /* cyan */
178         {0xff, 0x00, 0x00},     /* red */
179         {0xc0, 0x00, 0xc0},     /* magenta */
180         {0xc0, 0xc0, 0x00},     /* brown */
181         {0xc0, 0xc0, 0xc0},     /* light grey */
182         {0x80, 0x80, 0x80},     /* dark grey */
183         {0x80, 0x80, 0xff},     /* light blue */
184         {0x80, 0xff, 0x80},     /* light green */
185         {0x80, 0xff, 0xff},     /* light cyan */
186         {0xff, 0x80, 0x80},     /* light red */
187         {0xff, 0x80, 0xff},     /* light magenta */
188         {0xff, 0xff, 0x80},     /* yellow */
189         {0xff, 0xff, 0xff}      /* white */
190 };
191
192 #define MACHFB_CMAP_OFF         16
193
194 static const u_char machfb_mouse_pointer_bits[64][8] = {
195         { 0x00, 0x00, },        /* ............ */
196         { 0x80, 0x00, },        /* *........... */
197         { 0xc0, 0x00, },        /* **.......... */
198         { 0xe0, 0x00, },        /* ***......... */
199         { 0xf0, 0x00, },        /* ****........ */
200         { 0xf8, 0x00, },        /* *****....... */
201         { 0xfc, 0x00, },        /* ******...... */
202         { 0xfe, 0x00, },        /* *******..... */
203         { 0xff, 0x00, },        /* ********.... */
204         { 0xff, 0x80, },        /* *********... */
205         { 0xfc, 0xc0, },        /* ******..**.. */
206         { 0xdc, 0x00, },        /* **.***...... */
207         { 0x8e, 0x00, },        /* *...***..... */
208         { 0x0e, 0x00, },        /* ....***..... */
209         { 0x07, 0x00, },        /* .....***.... */
210         { 0x04, 0x00, },        /* .....*...... */
211         { 0x00, 0x00, },        /* ............ */
212         { 0x00, 0x00, },        /* ............ */
213         { 0x00, 0x00, },        /* ............ */
214         { 0x00, 0x00, },        /* ............ */
215         { 0x00, 0x00, },        /* ............ */
216         { 0x00, 0x00, },        /* ............ */
217 };
218
219 /*
220  * Lookup table to perform a bit-swap of the mouse pointer bits,
221  * map set bits to CUR_CLR0 and unset bits to transparent.
222  */
223 static const u_char machfb_mouse_pointer_lut[] = {
224         0xaa, 0x2a, 0x8a, 0x0a, 0xa2, 0x22, 0x82, 0x02,
225         0xa8, 0x28, 0x88, 0x08, 0xa0, 0x20, 0x80, 0x00
226 };
227
228 static const char *const machfb_memtype_names[] = {
229         "(N/A)", "DRAM", "EDO DRAM", "EDO DRAM", "SDRAM", "SGRAM", "WRAM",
230         "(unknown type)"
231 };
232
233 extern const struct gfb_font gallant12x22;
234
235 static struct machfb_softc machfb_softc;
236 static struct bus_space_tag machfb_bst_store[1];
237
238 static device_probe_t machfb_pci_probe;
239 static device_attach_t machfb_pci_attach;
240 static device_detach_t machfb_pci_detach;
241
242 static device_method_t machfb_methods[] = {
243         /* Device interface */
244         DEVMETHOD(device_probe,         machfb_pci_probe),
245         DEVMETHOD(device_attach,        machfb_pci_attach),
246         DEVMETHOD(device_detach,        machfb_pci_detach),
247
248         { 0, 0 }
249 };
250
251 static driver_t machfb_pci_driver = {
252         MACHFB_DRIVER_NAME,
253         machfb_methods,
254         sizeof(struct machfb_softc),
255 };
256
257 static devclass_t machfb_devclass;
258
259 DRIVER_MODULE(machfb, pci, machfb_pci_driver, machfb_devclass, 0, 0);
260 MODULE_DEPEND(machfb, pci, 1, 1, 1);
261
262 static void machfb_cursor_enable(struct machfb_softc *, int);
263 static int machfb_cursor_install(struct machfb_softc *);
264 static int machfb_get_memsize(struct machfb_softc *);
265 static void machfb_reset_engine(struct machfb_softc *);
266 static void machfb_init_engine(struct machfb_softc *);
267 #if 0
268 static void machfb_adjust_frame(struct machfb_softc *, int, int);
269 #endif
270 static void machfb_shutdown_final(void *);
271 static void machfb_shutdown_reset(void *);
272
273 static int machfb_configure(int);
274
275 static vi_probe_t machfb_probe;
276 static vi_init_t machfb_init;
277 static vi_get_info_t machfb_get_info;
278 static vi_query_mode_t machfb_query_mode;
279 static vi_set_mode_t machfb_set_mode;
280 static vi_save_font_t machfb_save_font;
281 static vi_load_font_t machfb_load_font;
282 static vi_show_font_t machfb_show_font;
283 static vi_save_palette_t machfb_save_palette;
284 static vi_load_palette_t machfb_load_palette;
285 static vi_set_border_t machfb_set_border;
286 static vi_save_state_t machfb_save_state;
287 static vi_load_state_t machfb_load_state;
288 static vi_set_win_org_t machfb_set_win_org;
289 static vi_read_hw_cursor_t machfb_read_hw_cursor;
290 static vi_set_hw_cursor_t machfb_set_hw_cursor;
291 static vi_set_hw_cursor_shape_t machfb_set_hw_cursor_shape;
292 static vi_blank_display_t machfb_blank_display;
293 static vi_mmap_t machfb_mmap;
294 static vi_ioctl_t machfb_ioctl;
295 static vi_clear_t machfb_clear;
296 static vi_fill_rect_t machfb_fill_rect;
297 static vi_bitblt_t machfb_bitblt;
298 static vi_diag_t machfb_diag;
299 static vi_save_cursor_palette_t machfb_save_cursor_palette;
300 static vi_load_cursor_palette_t machfb_load_cursor_palette;
301 static vi_copy_t machfb_copy;
302 static vi_putp_t machfb_putp;
303 static vi_putc_t machfb_putc;
304 static vi_puts_t machfb_puts;
305 static vi_putm_t machfb_putm;
306
307 static video_switch_t machfbvidsw = {
308         .probe                  = machfb_probe,
309         .init                   = machfb_init,
310         .get_info               = machfb_get_info,
311         .query_mode             = machfb_query_mode,
312         .set_mode               = machfb_set_mode,
313         .save_font              = machfb_save_font,
314         .load_font              = machfb_load_font,
315         .show_font              = machfb_show_font,
316         .save_palette           = machfb_save_palette,
317         .load_palette           = machfb_load_palette,
318         .set_border             = machfb_set_border,
319         .save_state             = machfb_save_state,
320         .load_state             = machfb_load_state,
321         .set_win_org            = machfb_set_win_org,
322         .read_hw_cursor         = machfb_read_hw_cursor,
323         .set_hw_cursor          = machfb_set_hw_cursor,
324         .set_hw_cursor_shape    = machfb_set_hw_cursor_shape,
325         .blank_display          = machfb_blank_display,
326         .mmap                   = machfb_mmap,
327         .ioctl                  = machfb_ioctl,
328         .clear                  = machfb_clear,
329         .fill_rect              = machfb_fill_rect,
330         .bitblt                 = machfb_bitblt,
331         .diag                   = machfb_diag,
332         .save_cursor_palette    = machfb_save_cursor_palette,
333         .load_cursor_palette    = machfb_load_cursor_palette,
334         .copy                   = machfb_copy,
335         .putp                   = machfb_putp,
336         .putc                   = machfb_putc,
337         .puts                   = machfb_puts,
338         .putm                   = machfb_putm
339 };
340
341 VIDEO_DRIVER(machfb, machfbvidsw, machfb_configure);
342
343 extern sc_rndr_sw_t txtrndrsw;
344 RENDERER(machfb, 0, txtrndrsw, gfb_set);
345
346 RENDERER_MODULE(machfb, gfb_set);
347
348 /*
349  * Inline functions for getting access to register aperture.
350  */
351 static inline uint32_t regr(struct machfb_softc *, uint32_t);
352 static inline uint8_t regrb(struct machfb_softc *, uint32_t);
353 static inline void regw(struct machfb_softc *, uint32_t, uint32_t);
354 static inline void regwb(struct machfb_softc *, uint32_t, uint8_t);
355 static inline void regwb_pll(struct machfb_softc *, uint32_t, uint8_t);
356
357 static inline uint32_t
358 regr(struct machfb_softc *sc, uint32_t index)
359 {
360
361         return bus_space_read_4(sc->sc_regt, sc->sc_regh, index);
362 }
363
364 static inline uint8_t
365 regrb(struct machfb_softc *sc, uint32_t index)
366 {
367
368         return bus_space_read_1(sc->sc_regt, sc->sc_regh, index);
369 }
370
371 static inline void
372 regw(struct machfb_softc *sc, uint32_t index, uint32_t data)
373 {
374
375         bus_space_write_4(sc->sc_regt, sc->sc_regh, index, data);
376         bus_space_barrier(sc->sc_regt, sc->sc_regh, index, 4,
377             BUS_SPACE_BARRIER_WRITE);
378 }
379
380 static inline void
381 regwb(struct machfb_softc *sc, uint32_t index, uint8_t data)
382 {
383
384         bus_space_write_1(sc->sc_regt, sc->sc_regh, index, data);
385         bus_space_barrier(sc->sc_regt, sc->sc_regh, index, 1,
386             BUS_SPACE_BARRIER_WRITE);
387 }
388
389 static inline void
390 regwb_pll(struct machfb_softc *sc, uint32_t index, uint8_t data)
391 {
392
393         regwb(sc, CLOCK_CNTL + 1, (index << 2) | PLL_WR_EN);
394         regwb(sc, CLOCK_CNTL + 2, data);
395         regwb(sc, CLOCK_CNTL + 1, (index << 2) & ~PLL_WR_EN);
396 }
397
398 static inline void
399 wait_for_fifo(struct machfb_softc *sc, uint8_t v)
400 {
401
402         while ((regr(sc, FIFO_STAT) & 0xffff) > (0x8000 >> v))
403                 ;
404 }
405
406 static inline void
407 wait_for_idle(struct machfb_softc *sc)
408 {
409
410         wait_for_fifo(sc, 16);
411         while ((regr(sc, GUI_STAT) & 1) != 0)
412                 ;
413 }
414
415 /*
416  * Inline functions for setting the background and foreground colors.
417  */
418 static inline void machfb_setbg(struct machfb_softc *sc, int bg);
419 static inline void machfb_setfg(struct machfb_softc *sc, int fg);
420
421 static inline void
422 machfb_setbg(struct machfb_softc *sc, int bg)
423 {
424
425         if (bg == sc->sc_bg_cache)
426                 return;
427         sc->sc_bg_cache = bg;
428         wait_for_fifo(sc, 1);
429         regw(sc, DP_BKGD_CLR, bg + MACHFB_CMAP_OFF);
430 }
431
432 static inline void
433 machfb_setfg(struct machfb_softc *sc, int fg)
434 {
435
436         if (fg == sc->sc_fg_cache)
437                 return;
438         sc->sc_fg_cache = fg;
439         wait_for_fifo(sc, 1);
440         regw(sc, DP_FRGD_CLR, fg + MACHFB_CMAP_OFF);
441 }
442
443 /*
444  * video driver interface
445  */
446 static int
447 machfb_configure(int flags)
448 {
449         struct machfb_softc *sc;
450         phandle_t chosen, output;
451         ihandle_t stdout;
452         bus_addr_t addr;
453         uint32_t id;
454         int i, space;
455
456         /*
457          * For the high-level console probing return the number of
458          * registered adapters.
459          */
460         if (!(flags & VIO_PROBE_ONLY)) {
461                 for (i = 0; vid_find_adapter(MACHFB_DRIVER_NAME, i) >= 0; i++)
462                         ;
463                 return (i);
464         }
465
466         /* Low-level console probing and initialization. */
467
468         sc = &machfb_softc;
469         if (sc->sc_va.va_flags & V_ADP_REGISTERED)
470                 goto found;
471
472         if ((chosen = OF_finddevice("/chosen")) == -1)  /* Quis contra nos? */
473                 return (0);
474         if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1)
475                 return (0);
476         if ((output = OF_instance_to_package(stdout)) == -1)
477                 return (0);
478         if ((OF_getprop(output, "vendor-id", &id, sizeof(id)) == -1) ||
479             id != ATI_VENDOR)
480                 return (0);
481         if (OF_getprop(output, "device-id", &id, sizeof(id)) == -1)
482                 return (0);
483         for (i = 0; i < nitems(machfb_info); i++) {
484                 if (id == machfb_info[i].chip_id) {
485                         sc->sc_flags = MACHFB_CONSOLE;
486                         sc->sc_node = output;
487                         sc->sc_chip_id = id;
488                         break;
489                 }
490         }
491         if (!(sc->sc_flags & MACHFB_CONSOLE))
492                 return (0);
493
494         if (OF_getprop(output, "revision-id", &sc->sc_chip_rev,
495             sizeof(sc->sc_chip_rev)) == -1)
496                 return (0);
497         if (OF_decode_addr(output, 0, &space, &addr) != 0)
498                 return (0);
499         sc->sc_memt = &machfb_bst_store[0];
500         sc->sc_memh = sparc64_fake_bustag(space, addr, sc->sc_memt);
501         sc->sc_regt = sc->sc_memt;
502         bus_space_subregion(sc->sc_regt, sc->sc_memh, MACH64_REG_OFF,
503             MACH64_REG_SIZE, &sc->sc_regh);
504
505         if (machfb_init(0, &sc->sc_va, 0) < 0)
506                  return (0);
507
508  found:
509         /* Return number of found adapters. */
510         return (1);
511 }
512
513 static int
514 machfb_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
515 {
516
517         return (0);
518 }
519
520 static int
521 machfb_init(int unit, video_adapter_t *adp, int flags)
522 {
523         struct machfb_softc *sc;
524         phandle_t options;
525         video_info_t *vi;
526         char buf[32];
527         int i;
528         uint8_t dac_mask, dac_rindex, dac_windex;
529
530         sc = (struct machfb_softc *)adp;
531         vi = &adp->va_info;
532
533         if ((regr(sc, CONFIG_CHIP_ID) & 0xffff) != sc->sc_chip_id)
534                 return (ENXIO);
535
536         sc->sc_ramdac_freq = 0;
537         for (i = 0; i < nitems(machfb_info); i++) {
538                 if (sc->sc_chip_id == machfb_info[i].chip_id) {
539                         sc->sc_ramdac_freq = machfb_info[i].ramdac_freq;
540                         break;
541                 }
542         }
543         if (sc->sc_ramdac_freq == 0)
544                 return (ENXIO);
545         if (sc->sc_chip_id == ATI_RAGE_II && sc->sc_chip_rev & 0x07)
546                 sc->sc_ramdac_freq = 170000;
547
548         vid_init_struct(adp, MACHFB_DRIVER_NAME, -1, unit);
549
550         if (OF_getprop(sc->sc_node, "height", &sc->sc_height,
551             sizeof(sc->sc_height)) == -1)
552                 return (ENXIO);
553         if (OF_getprop(sc->sc_node, "width", &sc->sc_width,
554             sizeof(sc->sc_width)) == -1)
555                 return (ENXIO);
556         if (OF_getprop(sc->sc_node, "depth", &sc->sc_depth,
557             sizeof(sc->sc_depth)) == -1)
558                 return (ENXIO);
559         if ((options = OF_finddevice("/options")) == -1)
560                 return (ENXIO);
561         if (OF_getprop(options, "screen-#rows", buf, sizeof(buf)) == -1)
562                 return (ENXIO);
563         vi->vi_height = strtol(buf, NULL, 10);
564         if (OF_getprop(options, "screen-#columns", buf, sizeof(buf)) == -1)
565                 return (ENXIO);
566         vi->vi_width = strtol(buf, NULL, 10);
567         vi->vi_cwidth = gallant12x22.width;
568         vi->vi_cheight = gallant12x22.height;
569         vi->vi_flags = V_INFO_COLOR;
570         vi->vi_mem_model = V_INFO_MM_OTHER;
571
572         sc->sc_font = gallant12x22.data;
573         sc->sc_cbwidth = howmany(vi->vi_cwidth, NBBY);  /* width in bytes */
574         sc->sc_xmargin = (sc->sc_width - (vi->vi_width * vi->vi_cwidth)) / 2;
575         sc->sc_ymargin = (sc->sc_height - (vi->vi_height * vi->vi_cheight)) / 2;
576
577         if (sc->sc_chip_id != ATI_MACH64_CT &&
578             !((sc->sc_chip_id == ATI_MACH64_VT ||
579             sc->sc_chip_id == ATI_RAGE_II) &&
580             (sc->sc_chip_rev & 0x07) == 0))
581                 sc->sc_flags |= MACHFB_DSP;
582
583         sc->sc_memsize = machfb_get_memsize(sc);
584         if (sc->sc_memsize == 8192)
585                 /* The last page is used as register aperture. */
586                 sc->sc_memsize -= 4;
587         sc->sc_memtype = regr(sc, CONFIG_STAT0) & 0x07;
588
589         if ((sc->sc_chip_id >= ATI_RAGE_XC_PCI66 &&
590             sc->sc_chip_id <= ATI_RAGE_XL_PCI66) ||
591             (sc->sc_chip_id >= ATI_RAGE_XL_PCI &&
592             sc->sc_chip_id <= ATI_RAGE_XC_PCI))
593                 sc->sc_ref_freq = 29498;
594         else
595                 sc->sc_ref_freq = 14318;
596
597         regwb(sc, CLOCK_CNTL + 1, PLL_REF_DIV << 2);
598         sc->sc_ref_div = regrb(sc, CLOCK_CNTL + 2);
599         regwb(sc, CLOCK_CNTL + 1, MCLK_FB_DIV << 2);
600         sc->sc_mclk_fb_div = regrb(sc, CLOCK_CNTL + 2);
601         sc->sc_mem_freq = (2 * sc->sc_ref_freq * sc->sc_mclk_fb_div) /
602             (sc->sc_ref_div * 2);
603         sc->sc_mclk_post_div = (sc->sc_mclk_fb_div * 2 * sc->sc_ref_freq) /
604             (sc->sc_mem_freq * sc->sc_ref_div);
605
606         machfb_init_engine(sc);
607 #if 0
608         machfb_adjust_frame(0, 0);
609 #endif
610         machfb_set_mode(adp, 0);
611
612         /*
613          * Install our 16-color color map.  This is done only once and with
614          * an offset of 16 on sparc64 as there the OBP driver expects white
615          * to be at index 0 and black at 255 (some versions also use 1 - 8
616          * for color text support or the full palette for the boot banner
617          * logo but no versions seems to use the ISO 6429-1983 color map).
618          * Otherwise the colors are inverted when back in the OFW.
619          */
620         dac_rindex = regrb(sc, DAC_RINDEX);
621         dac_windex = regrb(sc, DAC_WINDEX);
622         dac_mask = regrb(sc, DAC_MASK);
623         regwb(sc, DAC_MASK, 0xff);
624         regwb(sc, DAC_WINDEX, MACHFB_CMAP_OFF);
625         for (i = 0; i < 16; i++) {
626                 regwb(sc, DAC_DATA, machfb_default_cmap[i].red);
627                 regwb(sc, DAC_DATA, machfb_default_cmap[i].green);
628                 regwb(sc, DAC_DATA, machfb_default_cmap[i].blue);
629         }
630         regwb(sc, DAC_MASK, dac_mask);
631         regwb(sc, DAC_RINDEX, dac_rindex);
632         regwb(sc, DAC_WINDEX, dac_windex);
633
634         machfb_blank_display(adp, V_DISPLAY_ON);
635         machfb_clear(adp);
636
637         /*
638          * Setting V_ADP_MODECHANGE serves as hack so machfb_set_mode()
639          * (which will invalidate our caches) is called as a precaution
640          * when the X server shuts down.
641          */
642         adp->va_flags |= V_ADP_COLOR | V_ADP_MODECHANGE | V_ADP_PALETTE |
643             V_ADP_BORDER | V_ADP_INITIALIZED;
644         if (vid_register(adp) < 0)
645                 return (ENXIO);
646         adp->va_flags |= V_ADP_REGISTERED;
647
648         return (0);
649 }
650
651 static int
652 machfb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
653 {
654
655         bcopy(&adp->va_info, info, sizeof(*info));
656
657         return (0);
658 }
659
660 static int
661 machfb_query_mode(video_adapter_t *adp, video_info_t *info)
662 {
663
664         return (ENODEV);
665 }
666
667 static int
668 machfb_set_mode(video_adapter_t *adp, int mode)
669 {
670         struct machfb_softc *sc;
671
672         sc = (struct machfb_softc *)adp;
673
674         sc->sc_bg_cache = -1;
675         sc->sc_fg_cache = -1;
676         sc->sc_draw_cache = 0;
677
678         return (0);
679 }
680
681 static int
682 machfb_save_font(video_adapter_t *adp, int page, int size, int width,
683     u_char *data, int c, int count)
684 {
685
686         return (ENODEV);
687 }
688
689 static int
690 machfb_load_font(video_adapter_t *adp, int page, int size, int width,
691     u_char *data, int c, int count)
692 {
693
694         return (ENODEV);
695 }
696
697 static int
698 machfb_show_font(video_adapter_t *adp, int page)
699 {
700
701         return (ENODEV);
702 }
703
704 static int
705 machfb_save_palette(video_adapter_t *adp, u_char *palette)
706 {
707         struct machfb_softc *sc;
708         int i;
709         uint8_t dac_mask, dac_rindex, dac_windex;
710
711         sc = (struct machfb_softc *)adp;
712
713         dac_rindex = regrb(sc, DAC_RINDEX);
714         dac_windex = regrb(sc, DAC_WINDEX);
715         dac_mask = regrb(sc, DAC_MASK);
716         regwb(sc, DAC_MASK, 0xff);
717         regwb(sc, DAC_RINDEX, 0x0);
718         for (i = 0; i < 256 * 3; i++)
719                 palette[i] = regrb(sc, DAC_DATA);
720         regwb(sc, DAC_MASK, dac_mask);
721         regwb(sc, DAC_RINDEX, dac_rindex);
722         regwb(sc, DAC_WINDEX, dac_windex);
723
724         return (0);
725 }
726
727 static int
728 machfb_load_palette(video_adapter_t *adp, u_char *palette)
729 {
730         struct machfb_softc *sc;
731         int i;
732         uint8_t dac_mask, dac_rindex, dac_windex;
733
734         sc = (struct machfb_softc *)adp;
735
736         dac_rindex = regrb(sc, DAC_RINDEX);
737         dac_windex = regrb(sc, DAC_WINDEX);
738         dac_mask = regrb(sc, DAC_MASK);
739         regwb(sc, DAC_MASK, 0xff);
740         regwb(sc, DAC_WINDEX, 0x0);
741         for (i = 0; i < 256 * 3; i++)
742                 regwb(sc, DAC_DATA, palette[i]);
743         regwb(sc, DAC_MASK, dac_mask);
744         regwb(sc, DAC_RINDEX, dac_rindex);
745         regwb(sc, DAC_WINDEX, dac_windex);
746
747         return (0);
748 }
749
750 static int
751 machfb_set_border(video_adapter_t *adp, int border)
752 {
753         struct machfb_softc *sc;
754
755         sc = (struct machfb_softc *)adp;
756
757         machfb_fill_rect(adp, border, 0, 0, sc->sc_width, sc->sc_ymargin);
758         machfb_fill_rect(adp, border, 0, sc->sc_height - sc->sc_ymargin,
759             sc->sc_width, sc->sc_ymargin);
760         machfb_fill_rect(adp, border, 0, 0, sc->sc_xmargin, sc->sc_height);
761         machfb_fill_rect(adp, border, sc->sc_width - sc->sc_xmargin, 0,
762             sc->sc_xmargin, sc->sc_height);
763
764         return (0);
765 }
766
767 static int
768 machfb_save_state(video_adapter_t *adp, void *p, size_t size)
769 {
770
771         return (ENODEV);
772 }
773
774 static int
775 machfb_load_state(video_adapter_t *adp, void *p)
776 {
777
778         return (ENODEV);
779 }
780
781 static int
782 machfb_set_win_org(video_adapter_t *adp, off_t offset)
783 {
784
785         return (ENODEV);
786 }
787
788 static int
789 machfb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
790 {
791
792         *col = 0;
793         *row = 0;
794
795         return (0);
796 }
797
798 static int
799 machfb_set_hw_cursor(video_adapter_t *adp, int col, int row)
800 {
801
802         return (ENODEV);
803 }
804
805 static int
806 machfb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
807     int celsize, int blink)
808 {
809
810         return (ENODEV);
811 }
812
813 static int
814 machfb_blank_display(video_adapter_t *adp, int mode)
815 {
816         struct machfb_softc *sc;
817         uint32_t crtc_gen_cntl;
818
819         sc = (struct machfb_softc *)adp;
820
821         crtc_gen_cntl = (regr(sc, CRTC_GEN_CNTL) | CRTC_EXT_DISP_EN | CRTC_EN) &
822             ~(CRTC_HSYNC_DIS | CRTC_VSYNC_DIS | CRTC_DISPLAY_DIS);
823         switch (mode) {
824         case V_DISPLAY_ON:
825                 break;
826         case V_DISPLAY_BLANK:
827                 crtc_gen_cntl |= CRTC_HSYNC_DIS | CRTC_VSYNC_DIS |
828                     CRTC_DISPLAY_DIS;
829                 break;
830         case V_DISPLAY_STAND_BY:
831                 crtc_gen_cntl |= CRTC_HSYNC_DIS | CRTC_DISPLAY_DIS;
832                 break;
833         case V_DISPLAY_SUSPEND:
834                 crtc_gen_cntl |= CRTC_VSYNC_DIS | CRTC_DISPLAY_DIS;
835                 break;
836         }
837         regw(sc, CRTC_GEN_CNTL, crtc_gen_cntl);
838
839         return (0);
840 }
841
842 static int
843 machfb_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
844     int prot, vm_memattr_t *memattr)
845 {
846         struct machfb_softc *sc;
847         video_info_t *vi;
848
849         sc = (struct machfb_softc *)adp;
850         vi = &adp->va_info;
851
852         /* BAR 2 - VGA memory */
853         if (sc->sc_vmem != 0 && offset >= sc->sc_vmem &&
854             offset < sc->sc_vmem + vi->vi_registers_size) {
855                 *paddr = vi->vi_registers + offset - sc->sc_vmem;
856                 return (0);
857         }
858
859         /* BAR 0 - framebuffer */
860         if (offset >= sc->sc_mem &&
861             offset < sc->sc_mem + vi->vi_buffer_size) {
862                 *paddr = vi->vi_buffer + offset - sc->sc_mem;
863                 return (0);
864         }
865
866         /* 'regular' framebuffer mmap()ing */
867         if (offset < adp->va_window_size) {
868                 *paddr = vi->vi_window + offset;
869                 return (0);
870         }
871
872         return (EINVAL);
873 }
874
875 static int
876 machfb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
877 {
878         struct machfb_softc *sc;
879         struct fbcursor *fbc;
880         struct fbtype *fb;
881
882         sc = (struct machfb_softc *)adp;
883
884         switch (cmd) {
885         case FBIOGTYPE:
886                 fb = (struct fbtype *)data;
887                 fb->fb_type = FBTYPE_PCIMISC;
888                 fb->fb_height = sc->sc_height;
889                 fb->fb_width = sc->sc_width;
890                 fb->fb_depth = sc->sc_depth;
891                 if (sc->sc_depth <= 1 || sc->sc_depth > 8)
892                         fb->fb_cmsize = 0;
893                 else
894                         fb->fb_cmsize = 1 << sc->sc_depth;
895                 fb->fb_size = adp->va_buffer_size;
896                 break;
897         case FBIOSCURSOR:
898                 fbc = (struct fbcursor *)data;
899                 if (fbc->set & FB_CUR_SETCUR && fbc->enable == 0) {
900                         machfb_cursor_enable(sc, 0);
901                         sc->sc_flags &= ~MACHFB_CUREN;
902                 } else
903                         return (ENODEV);
904                 break;
905         default:
906                 return (fb_commonioctl(adp, cmd, data));
907         }
908
909         return (0);
910 }
911
912 static int
913 machfb_clear(video_adapter_t *adp)
914 {
915         struct machfb_softc *sc;
916
917         sc = (struct machfb_softc *)adp;
918
919         machfb_fill_rect(adp, (SC_NORM_ATTR >> 4) & 0xf, 0, 0, sc->sc_width,
920             sc->sc_height);
921
922         return (0);
923 }
924
925 static int
926 machfb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
927 {
928         struct machfb_softc *sc;
929
930         sc = (struct machfb_softc *)adp;
931
932         if (sc->sc_draw_cache != MACHFB_DRAW_FILLRECT) {
933                 wait_for_fifo(sc, 7);
934                 regw(sc, DP_WRITE_MASK, 0xff);
935                 regw(sc, DP_PIX_WIDTH, DST_8BPP | SRC_8BPP | HOST_8BPP);
936                 regw(sc, DP_SRC, FRGD_SRC_FRGD_CLR);
937                 regw(sc, DP_MIX, MIX_SRC << 16);
938                 regw(sc, CLR_CMP_CNTL, 0);      /* no transparency */
939                 regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
940                 regw(sc, DST_CNTL, DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM);
941                 sc->sc_draw_cache = MACHFB_DRAW_FILLRECT;
942         }
943         machfb_setfg(sc, val);
944         wait_for_fifo(sc, 4);
945         regw(sc, SRC_Y_X, (x << 16) | y);
946         regw(sc, SRC_WIDTH1, cx);
947         regw(sc, DST_Y_X, (x << 16) | y);
948         regw(sc, DST_HEIGHT_WIDTH, (cx << 16) | cy);
949
950         return (0);
951 }
952
953 static int
954 machfb_bitblt(video_adapter_t *adp, ...)
955 {
956
957         return (ENODEV);
958 }
959
960 static int
961 machfb_diag(video_adapter_t *adp, int level)
962 {
963         video_info_t info;
964
965         fb_dump_adp_info(adp->va_name, adp, level);
966         machfb_get_info(adp, 0, &info);
967         fb_dump_mode_info(adp->va_name, adp, &info, level);
968
969         return (0);
970 }
971
972 static int
973 machfb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
974 {
975
976         return (ENODEV);
977 }
978
979 static int
980 machfb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
981 {
982
983         return (ENODEV);
984 }
985
986 static int
987 machfb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
988 {
989
990         return (ENODEV);
991 }
992
993 static int
994 machfb_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p, uint32_t a,
995     int size, int bpp, int bit_ltor, int byte_ltor)
996 {
997
998         return (ENODEV);
999 }
1000
1001 static int
1002 machfb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
1003 {
1004         struct machfb_softc *sc;
1005         const uint8_t *p;
1006         int i;
1007
1008         sc = (struct machfb_softc *)adp;
1009
1010         if (sc->sc_draw_cache != MACHFB_DRAW_CHAR) {
1011                 wait_for_fifo(sc, 8);
1012                 regw(sc, DP_WRITE_MASK, 0xff);  /* XXX only good for 8 bit */
1013                 regw(sc, DP_PIX_WIDTH, DST_8BPP | SRC_1BPP | HOST_1BPP);
1014                 regw(sc, DP_SRC, MONO_SRC_HOST | BKGD_SRC_BKGD_CLR |
1015                     FRGD_SRC_FRGD_CLR);
1016                 regw(sc, DP_MIX ,((MIX_SRC & 0xffff) << 16) | MIX_SRC);
1017                 regw(sc, CLR_CMP_CNTL, 0);      /* no transparency */
1018                 regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
1019                 regw(sc, DST_CNTL, DST_Y_TOP_TO_BOTTOM | DST_X_LEFT_TO_RIGHT);
1020                 regw(sc, HOST_CNTL, HOST_BYTE_ALIGN);
1021                 sc->sc_draw_cache = MACHFB_DRAW_CHAR;
1022         }
1023         machfb_setbg(sc, (a >> 4) & 0xf);
1024         machfb_setfg(sc, a & 0xf);
1025         wait_for_fifo(sc, 4 + (adp->va_info.vi_cheight / sc->sc_cbwidth));
1026         regw(sc, SRC_Y_X, 0);
1027         regw(sc, SRC_WIDTH1, adp->va_info.vi_cwidth);
1028         regw(sc, DST_Y_X, ((((off % adp->va_info.vi_width) *
1029             adp->va_info.vi_cwidth) + sc->sc_xmargin) << 16) |
1030             (((off / adp->va_info.vi_width) * adp->va_info.vi_cheight) +
1031             sc->sc_ymargin));
1032         regw(sc, DST_HEIGHT_WIDTH, (adp->va_info.vi_cwidth << 16) |
1033             adp->va_info.vi_cheight);
1034         p = sc->sc_font + (c * adp->va_info.vi_cheight * sc->sc_cbwidth);
1035         for (i = 0; i < adp->va_info.vi_cheight * sc->sc_cbwidth; i += 4)
1036                 regw(sc, HOST_DATA0 + i, (p[i + 3] << 24 | p[i + 2] << 16 |
1037                     p[i + 1] << 8 | p[i]));
1038
1039         return (0);
1040 }
1041
1042 static int
1043 machfb_puts(video_adapter_t *adp, vm_offset_t off, uint16_t *s, int len)
1044 {
1045         struct machfb_softc *sc;
1046         int blanks, i, x1, x2, y1, y2;
1047         uint8_t a, c, color1, color2;
1048
1049         sc = (struct machfb_softc *)adp;
1050
1051 #define MACHFB_BLANK    machfb_fill_rect(adp, color1, x1, y1,           \
1052                             blanks * adp->va_info.vi_cwidth,            \
1053                             adp->va_info.vi_cheight)
1054
1055         blanks = color1 = x1 = y1 = 0;
1056         for (i = 0; i < len; i++) {
1057                 /*
1058                  * Accelerate continuous blanks by drawing a respective
1059                  * rectangle instead.  Drawing a rectangle of any size
1060                  * takes about the same number of operations as drawing
1061                  * a single character.
1062                  */
1063                 c = s[i] & 0xff;
1064                 a = (s[i] & 0xff00) >> 8;
1065                 if (c == 0x00 || c == 0x20 || c == 0xdb || c == 0xff) {
1066                         color2 = (a >> (c == 0xdb ? 0 : 4) & 0xf);
1067                         x2 = (((off + i) % adp->va_info.vi_width) *
1068                             adp->va_info.vi_cwidth) + sc->sc_xmargin;
1069                         y2 = (((off + i) / adp->va_info.vi_width) *
1070                             adp->va_info.vi_cheight) + sc->sc_ymargin;
1071                         if (blanks == 0) {
1072                                 color1 = color2;
1073                                 x1 = x2;
1074                                 y1 = y2;
1075                                 blanks++;
1076                         } else if (color1 != color2 || y1 != y2) {
1077                                 MACHFB_BLANK;
1078                                 color1 = color2;
1079                                 x1 = x2;
1080                                 y1 = y2;
1081                                 blanks = 1;
1082                         } else
1083                                 blanks++;
1084                 } else {
1085                         if (blanks != 0) {
1086                                 MACHFB_BLANK;
1087                                 blanks = 0;
1088                         }
1089                         vidd_putc(adp, off + i, c, a);
1090                 }
1091         }
1092         if (blanks != 0)
1093                 MACHFB_BLANK;
1094
1095 #undef MACHFB_BLANK
1096
1097         return (0);
1098 }
1099
1100 static int
1101 machfb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
1102     uint32_t pixel_mask, int size, int width)
1103 {
1104         struct machfb_softc *sc;
1105         int error;
1106
1107         sc = (struct machfb_softc *)adp;
1108
1109         if ((!(sc->sc_flags & MACHFB_CUREN)) &&
1110             (error = machfb_cursor_install(sc)) < 0)
1111                 return (error);
1112         else {
1113                 /*
1114                  * The hardware cursor always must be disabled when
1115                  * fiddling with its bits otherwise some artifacts
1116                  * may appear on the screen.
1117                  */
1118                 machfb_cursor_enable(sc, 0);
1119         }
1120
1121         regw(sc, CUR_HORZ_VERT_OFF, 0);
1122         if ((regr(sc, GEN_TEST_CNTL) & CRTC_DBL_SCAN_EN) != 0)
1123                 y <<= 1;
1124         regw(sc, CUR_HORZ_VERT_POSN, ((y + sc->sc_ymargin) << 16) |
1125             (x + sc->sc_xmargin));
1126         machfb_cursor_enable(sc, 1);
1127         sc->sc_flags |= MACHFB_CUREN;
1128
1129         return (0);
1130 }
1131
1132 /*
1133  * PCI bus interface
1134  */
1135 static int
1136 machfb_pci_probe(device_t dev)
1137 {
1138         int i;
1139
1140         if (pci_get_class(dev) != PCIC_DISPLAY ||
1141             pci_get_subclass(dev) != PCIS_DISPLAY_VGA)
1142                 return (ENXIO);
1143
1144         for (i = 0; i < nitems(machfb_info); i++) {
1145                 if (pci_get_device(dev) == machfb_info[i].chip_id) {
1146                         device_set_desc(dev, machfb_info[i].name);
1147                         return (BUS_PROBE_DEFAULT);
1148                 }
1149         }
1150
1151         return (ENXIO);
1152 }
1153
1154 static int
1155 machfb_pci_attach(device_t dev)
1156 {
1157         struct machfb_softc *sc;
1158         video_adapter_t *adp;
1159         video_switch_t *sw;
1160         video_info_t *vi;
1161         phandle_t node;
1162         int error, i, rid;
1163         uint32_t *p32, u32;
1164         uint8_t *p;
1165
1166         node = ofw_bus_get_node(dev);
1167         if ((sc = (struct machfb_softc *)vid_get_adapter(vid_find_adapter(
1168             MACHFB_DRIVER_NAME, 0))) != NULL && sc->sc_node == node) {
1169                 device_printf(dev, "console\n");
1170                 device_set_softc(dev, sc);
1171         } else {
1172                 sc = device_get_softc(dev);
1173
1174                 sc->sc_node = node;
1175                 sc->sc_chip_id = pci_get_device(dev);
1176                 sc->sc_chip_rev = pci_get_revid(dev);
1177         }
1178         adp = &sc->sc_va;
1179         vi = &adp->va_info;
1180
1181         rid = PCIR_BAR(0);
1182         if ((sc->sc_memres = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1183             RF_ACTIVE)) == NULL) {
1184                 device_printf(dev, "cannot allocate memory resources\n");
1185                 return (ENXIO);
1186         }
1187         sc->sc_memt = rman_get_bustag(sc->sc_memres);
1188         sc->sc_memh = rman_get_bushandle(sc->sc_memres);
1189         sc->sc_mem = rman_get_start(sc->sc_memres);
1190         vi->vi_buffer = sc->sc_memh;
1191         vi->vi_buffer_size = rman_get_size(sc->sc_memres);
1192         if (OF_getprop(sc->sc_node, "address", &u32, sizeof(u32)) > 0 &&
1193                 vtophys(u32) == sc->sc_memh)
1194                 adp->va_mem_base = u32;
1195         else {
1196                 if (bus_space_map(sc->sc_memt, vi->vi_buffer,
1197                     vi->vi_buffer_size, BUS_SPACE_MAP_LINEAR,
1198                     &sc->sc_memh) != 0) {
1199                         device_printf(dev, "cannot map memory resources\n");
1200                         error = ENXIO;
1201                         goto fail_memres;
1202                 }
1203                 adp->va_mem_base =
1204                     (vm_offset_t)rman_get_virtual(sc->sc_memres);
1205         }
1206         adp->va_mem_size = vi->vi_buffer_size;
1207         adp->va_buffer = adp->va_mem_base;
1208         adp->va_buffer_size = adp->va_mem_size;
1209         sc->sc_regt = sc->sc_memt;
1210         if (bus_space_subregion(sc->sc_regt, sc->sc_memh, MACH64_REG_OFF,
1211             MACH64_REG_SIZE, &sc->sc_regh) != 0) {
1212                 device_printf(dev, "cannot allocate register resources\n");
1213                 error = ENXIO;
1214                 goto fail_memmap;
1215         }
1216
1217         /*
1218          * Depending on the firmware version the VGA I/O and/or memory
1219          * resources of the Mach64 chips come up disabled.  These will be
1220          * enabled by pci(4) when activating the resource in question but
1221          * this doesn't necessarily mean that the resource is valid.
1222          * Invalid resources seem to have in common that they start at
1223          * address 0.  We don't allocate the VGA memory in this case in
1224          * order to avoid warnings in apb(4) and crashes when using this
1225          * invalid resources.  X.Org is aware of this and doesn't use the
1226          * VGA memory resource in this case (but demands it if it's valid).
1227          */
1228         rid = PCIR_BAR(2);
1229         if (bus_get_resource_start(dev, SYS_RES_MEMORY, rid) != 0) {
1230                 if ((sc->sc_vmemres = bus_alloc_resource_any(dev,
1231                     SYS_RES_MEMORY, &rid, RF_ACTIVE)) == NULL) {
1232                         device_printf(dev,
1233                             "cannot allocate VGA memory resources\n");
1234                         error = ENXIO;
1235                         goto fail_memmap;
1236                 }
1237                 sc->sc_vmemt = rman_get_bustag(sc->sc_vmemres);
1238                 sc->sc_vmemh = rman_get_bushandle(sc->sc_vmemres);
1239                 sc->sc_vmem = rman_get_start(sc->sc_vmemres);
1240                 vi->vi_registers = sc->sc_vmemh;
1241                 vi->vi_registers_size = rman_get_size(sc->sc_vmemres);
1242                 if (bus_space_map(sc->sc_vmemt, vi->vi_registers,
1243                     vi->vi_registers_size, BUS_SPACE_MAP_LINEAR,
1244                     &sc->sc_vmemh) != 0) {
1245                         device_printf(dev,
1246                             "cannot map VGA memory resources\n");
1247                         error = ENXIO;
1248                         goto fail_vmemres;
1249                 }
1250                 adp->va_registers =
1251                     (vm_offset_t)rman_get_virtual(sc->sc_vmemres);
1252                 adp->va_registers_size = vi->vi_registers_size;
1253         }
1254
1255         if (!(sc->sc_flags & MACHFB_CONSOLE)) {
1256                 if ((sw = vid_get_switch(MACHFB_DRIVER_NAME)) == NULL) {
1257                         device_printf(dev, "cannot get video switch\n");
1258                         error = ENODEV;
1259                         goto fail_vmemmap;
1260                 }
1261                 /*
1262                  * During device configuration we don't necessarily probe
1263                  * the adapter which is the console first so we can't use
1264                  * the device unit number for the video adapter unit.  The
1265                  * worst case would be that we use the video adapter unit
1266                  * 0 twice.  As it doesn't really matter which unit number
1267                  * the corresponding video adapter has just use the next
1268                  * unused one.
1269                  */
1270                 for (i = 0; i < devclass_get_maxunit(machfb_devclass); i++)
1271                         if (vid_find_adapter(MACHFB_DRIVER_NAME, i) < 0)
1272                                 break;
1273                 if ((error = sw->init(i, adp, 0)) != 0) {
1274                         device_printf(dev, "cannot initialize adapter\n");
1275                         goto fail_vmemmap;
1276                 }
1277         }
1278
1279         /*
1280          * Test whether the aperture is byte swapped or not, set
1281          * va_window and va_window_size as appropriate.  Note that
1282          * the aperture could be mapped either big or little endian
1283          * independently of the endianness of the host so this has
1284          * to be a runtime test.
1285          */
1286         p32 = (uint32_t *)adp->va_buffer;
1287         u32 = *p32;
1288         p = (uint8_t *)adp->va_buffer;
1289         *p32 = 0x12345678;
1290         if (!(p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78)) {
1291                 adp->va_window = adp->va_buffer + 0x800000;
1292                 adp->va_window_size = adp->va_buffer_size - 0x800000;
1293                 vi->vi_window = vi->vi_buffer + 0x800000;
1294                 vi->vi_window_size = vi->vi_buffer_size - 0x800000;
1295                 sc->sc_flags |= MACHFB_SWAP;
1296         } else {
1297                 adp->va_window = adp->va_buffer;
1298                 adp->va_window_size = adp->va_buffer_size;
1299                 vi->vi_window = vi->vi_buffer;
1300                 vi->vi_window_size = vi->vi_buffer_size;
1301         }
1302         *p32 = u32;
1303         adp->va_window_gran = adp->va_window_size;
1304
1305         device_printf(dev,
1306             "%d MB aperture at %p %sswapped\n",
1307             (u_int)(adp->va_window_size / (1024 * 1024)),
1308             (void *)adp->va_window, (sc->sc_flags & MACHFB_SWAP) ?
1309             "" : "not ");
1310         device_printf(dev,
1311             "%ld KB %s %d.%d MHz, maximum RAMDAC clock %d MHz, %sDSP\n",
1312             (u_long)sc->sc_memsize, machfb_memtype_names[sc->sc_memtype],
1313             sc->sc_mem_freq / 1000, sc->sc_mem_freq % 1000,
1314             sc->sc_ramdac_freq / 1000,
1315             (sc->sc_flags & MACHFB_DSP) ? "" : "no ");
1316         device_printf(dev, "resolution %dx%d at %d bpp\n",
1317             sc->sc_width, sc->sc_height, sc->sc_depth);
1318
1319         /*
1320          * Allocate one page for the mouse pointer image at the end of
1321          * the little endian aperture, right before the memory mapped
1322          * registers that might also reside there.  Must be done after
1323          * sc_memsize was set and possibly adjusted to account for the
1324          * memory mapped registers.
1325          */
1326         sc->sc_curoff = (sc->sc_memsize * 1024) - PAGE_SIZE;
1327         sc->sc_memsize -= PAGE_SIZE / 1024;
1328         machfb_cursor_enable(sc, 0);
1329         /* Initialize with an all transparent image. */
1330         memset((void *)(adp->va_buffer + sc->sc_curoff), 0xaa, PAGE_SIZE);
1331
1332         /*
1333          * Register a handler that performs some cosmetic surgery like
1334          * turning off the mouse pointer on halt in preparation for
1335          * handing the screen over to the OFW.  Register another handler
1336          * that turns off the CRTC when resetting, otherwise the OFW
1337          * boot command issued by cpu_reset() just doesn't work.
1338          */
1339         EVENTHANDLER_REGISTER(shutdown_final, machfb_shutdown_final, sc,
1340             SHUTDOWN_PRI_DEFAULT);
1341         EVENTHANDLER_REGISTER(shutdown_reset, machfb_shutdown_reset, sc,
1342             SHUTDOWN_PRI_DEFAULT);
1343
1344         return (0);
1345
1346  fail_vmemmap:
1347         if (adp->va_registers != 0)
1348                 bus_space_unmap(sc->sc_vmemt, sc->sc_vmemh,
1349                     vi->vi_registers_size);
1350  fail_vmemres:
1351         if (sc->sc_vmemres != NULL)
1352                 bus_release_resource(dev, SYS_RES_MEMORY,
1353                     rman_get_rid(sc->sc_vmemres), sc->sc_vmemres);
1354  fail_memmap:
1355         bus_space_unmap(sc->sc_memt, sc->sc_memh, vi->vi_buffer_size);
1356  fail_memres:
1357         bus_release_resource(dev, SYS_RES_MEMORY,
1358             rman_get_rid(sc->sc_memres), sc->sc_memres);
1359
1360         return (error);
1361 }
1362
1363 static int
1364 machfb_pci_detach(device_t dev)
1365 {
1366
1367         return (EINVAL);
1368 }
1369
1370 /*
1371  * internal functions
1372  */
1373 static void
1374 machfb_cursor_enable(struct machfb_softc *sc, int onoff)
1375 {
1376
1377         if (onoff)
1378                 regw(sc, GEN_TEST_CNTL,
1379                     regr(sc, GEN_TEST_CNTL) | HWCURSOR_ENABLE);
1380         else
1381                 regw(sc, GEN_TEST_CNTL,
1382                     regr(sc, GEN_TEST_CNTL) &~ HWCURSOR_ENABLE);
1383 }
1384
1385 static int
1386 machfb_cursor_install(struct machfb_softc *sc)
1387 {
1388         uint16_t *p, v;
1389         uint8_t fg;
1390         int i, j;
1391
1392         if (sc->sc_curoff == 0)
1393                 return (ENODEV);
1394
1395         machfb_cursor_enable(sc, 0);
1396         regw(sc, CUR_OFFSET, sc->sc_curoff >> 3);
1397         fg = SC_NORM_ATTR & 0xf;
1398         regw(sc, CUR_CLR0, machfb_default_cmap[fg].red << 24 |
1399             machfb_default_cmap[fg].green << 16 |
1400             machfb_default_cmap[fg].blue << 8);
1401         p = (uint16_t *)(sc->sc_va.va_buffer + sc->sc_curoff);
1402         for (i = 0; i < 64; i++) {
1403                 for (j = 0; j < 8; j++) {
1404                         v = machfb_mouse_pointer_lut[
1405                             machfb_mouse_pointer_bits[i][j] >> 4] << 8 |
1406                             machfb_mouse_pointer_lut[
1407                             machfb_mouse_pointer_bits[i][j] & 0x0f];
1408                         if (sc->sc_flags & MACHFB_SWAP)
1409                                 *(p++) = bswap16(v);
1410                         else
1411                                 *(p++) = v;
1412                 }
1413         }
1414
1415         return (0);
1416 }
1417
1418 static int
1419 machfb_get_memsize(struct machfb_softc *sc)
1420 {
1421         int tmp, memsize;
1422         const int mem_tab[] = {
1423                 512, 1024, 2048, 4096, 6144, 8192, 12288, 16384
1424         };
1425
1426         tmp = regr(sc, MEM_CNTL);
1427 #ifdef MACHFB_DEBUG
1428         printf("memcntl=0x%08x\n", tmp);
1429 #endif
1430         if (sc->sc_flags & MACHFB_DSP) {
1431                 tmp &= 0x0000000f;
1432                 if (tmp < 8)
1433                         memsize = (tmp + 1) * 512;
1434                 else if (tmp < 12)
1435                         memsize = (tmp - 3) * 1024;
1436                 else
1437                         memsize = (tmp - 7) * 2048;
1438         } else
1439                 memsize = mem_tab[tmp & 0x07];
1440
1441         return (memsize);
1442 }
1443
1444 static void
1445 machfb_reset_engine(struct machfb_softc *sc)
1446 {
1447
1448         /* Reset engine.*/
1449         regw(sc, GEN_TEST_CNTL, regr(sc, GEN_TEST_CNTL) & ~GUI_ENGINE_ENABLE);
1450
1451         /* Enable engine. */
1452         regw(sc, GEN_TEST_CNTL, regr(sc, GEN_TEST_CNTL) | GUI_ENGINE_ENABLE);
1453
1454         /*
1455          * Ensure engine is not locked up by clearing any FIFO or
1456          * host errors.
1457          */
1458         regw(sc, BUS_CNTL, regr(sc, BUS_CNTL) | BUS_HOST_ERR_ACK |
1459             BUS_FIFO_ERR_ACK);
1460 }
1461
1462 static void
1463 machfb_init_engine(struct machfb_softc *sc)
1464 {
1465         uint32_t pitch_value;
1466
1467         pitch_value = sc->sc_width;
1468
1469         if (sc->sc_depth == 24)
1470                 pitch_value *= 3;
1471
1472         machfb_reset_engine(sc);
1473
1474         wait_for_fifo(sc, 14);
1475
1476         regw(sc, CONTEXT_MASK, 0xffffffff);
1477
1478         regw(sc, DST_OFF_PITCH, (pitch_value / 8) << 22);
1479
1480         regw(sc, DST_Y_X, 0);
1481         regw(sc, DST_HEIGHT, 0);
1482         regw(sc, DST_BRES_ERR, 0);
1483         regw(sc, DST_BRES_INC, 0);
1484         regw(sc, DST_BRES_DEC, 0);
1485
1486         regw(sc, DST_CNTL, DST_LAST_PEL | DST_X_LEFT_TO_RIGHT |
1487             DST_Y_TOP_TO_BOTTOM);
1488
1489         regw(sc, SRC_OFF_PITCH, (pitch_value / 8) << 22);
1490
1491         regw(sc, SRC_Y_X, 0);
1492         regw(sc, SRC_HEIGHT1_WIDTH1, 1);
1493         regw(sc, SRC_Y_X_START, 0);
1494         regw(sc, SRC_HEIGHT2_WIDTH2, 1);
1495
1496         regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
1497
1498         wait_for_fifo(sc, 13);
1499         regw(sc, HOST_CNTL, 0);
1500
1501         regw(sc, PAT_REG0, 0);
1502         regw(sc, PAT_REG1, 0);
1503         regw(sc, PAT_CNTL, 0);
1504
1505         regw(sc, SC_LEFT, 0);
1506         regw(sc, SC_TOP, 0);
1507         regw(sc, SC_BOTTOM, sc->sc_height - 1);
1508         regw(sc, SC_RIGHT, pitch_value - 1);
1509
1510         regw(sc, DP_BKGD_CLR, 0);
1511         regw(sc, DP_FRGD_CLR, 0xffffffff);
1512         regw(sc, DP_WRITE_MASK, 0xffffffff);
1513         regw(sc, DP_MIX, (MIX_SRC << 16) | MIX_DST);
1514
1515         regw(sc, DP_SRC, FRGD_SRC_FRGD_CLR);
1516
1517         wait_for_fifo(sc, 3);
1518         regw(sc, CLR_CMP_CLR, 0);
1519         regw(sc, CLR_CMP_MASK, 0xffffffff);
1520         regw(sc, CLR_CMP_CNTL, 0);
1521
1522         wait_for_fifo(sc, 2);
1523         switch (sc->sc_depth) {
1524         case 8:
1525                 regw(sc, DP_PIX_WIDTH, HOST_8BPP | SRC_8BPP | DST_8BPP);
1526                 regw(sc, DP_CHAIN_MASK, DP_CHAIN_8BPP);
1527                 regw(sc, DAC_CNTL, regr(sc, DAC_CNTL) | DAC_8BIT_EN);
1528                 break;
1529 #if 0
1530         case 32:
1531                 regw(sc, DP_PIX_WIDTH, HOST_32BPP | SRC_32BPP | DST_32BPP);
1532                 regw(sc, DP_CHAIN_MASK, DP_CHAIN_32BPP);
1533                 regw(sc, DAC_CNTL, regr(sc, DAC_CNTL) | DAC_8BIT_EN);
1534                 break;
1535 #endif
1536         }
1537
1538         wait_for_fifo(sc, 2);
1539         regw(sc, CRTC_INT_CNTL, regr(sc, CRTC_INT_CNTL) & ~0x20);
1540         regw(sc, GUI_TRAJ_CNTL, DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM);
1541
1542         wait_for_idle(sc);
1543 }
1544
1545 #if 0
1546 static void
1547 machfb_adjust_frame(struct machfb_softc *sc, int x, int y)
1548 {
1549         int offset;
1550
1551         offset = ((x + y * sc->sc_width) * (sc->sc_depth >> 3)) >> 3;
1552
1553         regw(sc, CRTC_OFF_PITCH, (regr(sc, CRTC_OFF_PITCH) & 0xfff00000) |
1554             offset);
1555 }
1556 #endif
1557
1558 static void
1559 machfb_shutdown_final(void *v)
1560 {
1561         struct machfb_softc *sc = v;
1562
1563         machfb_cursor_enable(sc, 0);
1564         /*
1565          * In case this is the console set the cursor of the stdout
1566          * instance to the start of the last line so OFW output ends
1567          * up beneath what FreeBSD left on the screen.
1568          */
1569         if (sc->sc_flags & MACHFB_CONSOLE) {
1570                 OF_interpret("stdout @ is my-self 0 to column#", 0);
1571                 OF_interpret("stdout @ is my-self #lines 1 - to line#", 0);
1572         }
1573 }
1574
1575 static void
1576 machfb_shutdown_reset(void *v)
1577 {
1578         struct machfb_softc *sc = v;
1579
1580         machfb_blank_display(&sc->sc_va, V_DISPLAY_STAND_BY);
1581 }