FreeBSD/Linux Kernel Cross Reference
sys/dev/fb/machfb.c
1 /*-
2 * Copyright (c) 2002 Bang Jun-Young
3 * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
16 *
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.
27 *
28 * from: NetBSD: machfb.c,v 1.23 2005/03/07 21:45:24 martin Exp
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: releng/10.4/sys/dev/fb/machfb.c 263763 2014-03-26 07:31:57Z dim $");
33
34 /*
35 * Driver for ATI Mach64 graphics chips. Some code is derived from the
36 * ATI Rage Pro and Derivatives Programmer's Guide.
37 */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/bus.h>
42 #include <sys/consio.h>
43 #include <sys/endian.h>
44 #include <sys/eventhandler.h>
45 #include <sys/fbio.h>
46 #include <sys/kernel.h>
47 #include <sys/module.h>
48 #include <sys/resource.h>
49
50 #include <vm/vm.h>
51 #include <vm/pmap.h>
52
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/openfirm.h>
55
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>
62
63 #include <sys/rman.h>
64
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>
71
72 /* #define MACHFB_DEBUG */
73
74 #define MACHFB_DRIVER_NAME "machfb"
75
76 #define MACH64_REG_OFF 0x7ffc00
77 #define MACH64_REG_SIZE 1024
78
79 struct machfb_softc {
80 video_adapter_t sc_va; /* must be first */
81
82 phandle_t sc_node;
83 uint16_t sc_chip_id;
84 uint8_t sc_chip_rev;
85
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;
94 u_long sc_mem;
95 u_long sc_vmem;
96
97 u_int sc_height;
98 u_int sc_width;
99 u_int sc_depth;
100 u_int sc_xmargin;
101 u_int sc_ymargin;
102
103 size_t sc_memsize;
104 u_int sc_memtype;
105 u_int sc_mem_freq;
106 u_int sc_ramdac_freq;
107 u_int sc_ref_freq;
108
109 u_int sc_ref_div;
110 u_int sc_mclk_post_div;
111 u_int sc_mclk_fb_div;
112
113 const u_char *sc_font;
114 u_int sc_cbwidth;
115 vm_offset_t sc_curoff;
116
117 int sc_bg_cache;
118 int sc_fg_cache;
119 u_int sc_draw_cache;
120 #define MACHFB_DRAW_CHAR (1 << 0)
121 #define MACHFB_DRAW_FILLRECT (1 << 1)
122
123 u_int sc_flags;
124 #define MACHFB_CONSOLE (1 << 0)
125 #define MACHFB_CUREN (1 << 1)
126 #define MACHFB_DSP (1 << 2)
127 #define MACHFB_SWAP (1 << 3)
128 };
129
130 static const struct {
131 uint16_t chip_id;
132 const char *name;
133 uint32_t ramdac_freq;
134 } machfb_info[] = {
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 }
166 };
167
168 static const struct machfb_cmap {
169 uint8_t red;
170 uint8_t green;
171 uint8_t blue;
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 */
189 };
190
191 #define MACHFB_CMAP_OFF 16
192
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, }, /* ............ */
216 };
217
218 /*
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.
221 */
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
225 };
226
227 static const char *const machfb_memtype_names[] = {
228 "(N/A)", "DRAM", "EDO DRAM", "EDO DRAM", "SDRAM", "SGRAM", "WRAM",
229 "(unknown type)"
230 };
231
232 extern const struct gfb_font gallant12x22;
233
234 static struct machfb_softc machfb_softc;
235 static struct bus_space_tag machfb_bst_store[1];
236
237 static device_probe_t machfb_pci_probe;
238 static device_attach_t machfb_pci_attach;
239 static device_detach_t machfb_pci_detach;
240
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),
246
247 { 0, 0 }
248 };
249
250 static driver_t machfb_pci_driver = {
251 MACHFB_DRIVER_NAME,
252 machfb_methods,
253 sizeof(struct machfb_softc),
254 };
255
256 static devclass_t machfb_devclass;
257
258 DRIVER_MODULE(machfb, pci, machfb_pci_driver, machfb_devclass, 0, 0);
259 MODULE_DEPEND(machfb, pci, 1, 1, 1);
260
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 *);
266 #if 0
267 static void machfb_adjust_frame(struct machfb_softc *, int, int);
268 #endif
269 static void machfb_shutdown_final(void *);
270 static void machfb_shutdown_reset(void *);
271
272 static int machfb_configure(int);
273
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;
305
306 static video_switch_t machfbvidsw = {
307 .probe = machfb_probe,
308 .init = machfb_init,
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,
325 .mmap = machfb_mmap,
326 .ioctl = machfb_ioctl,
327 .clear = machfb_clear,
328 .fill_rect = machfb_fill_rect,
329 .bitblt = machfb_bitblt,
330 .diag = machfb_diag,
331 .save_cursor_palette = machfb_save_cursor_palette,
332 .load_cursor_palette = machfb_load_cursor_palette,
333 .copy = machfb_copy,
334 .putp = machfb_putp,
335 .putc = machfb_putc,
336 .puts = machfb_puts,
337 .putm = machfb_putm
338 };
339
340 VIDEO_DRIVER(machfb, machfbvidsw, machfb_configure);
341
342 extern sc_rndr_sw_t txtrndrsw;
343 RENDERER(machfb, 0, txtrndrsw, gfb_set);
344
345 RENDERER_MODULE(machfb, gfb_set);
346
347 /*
348 * Inline functions for getting access to register aperture.
349 */
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);
355
356 static inline uint32_t
357 regr(struct machfb_softc *sc, uint32_t index)
358 {
359
360 return bus_space_read_4(sc->sc_regt, sc->sc_regh, index);
361 }
362
363 static inline uint8_t
364 regrb(struct machfb_softc *sc, uint32_t index)
365 {
366
367 return bus_space_read_1(sc->sc_regt, sc->sc_regh, index);
368 }
369
370 static inline void
371 regw(struct machfb_softc *sc, uint32_t index, uint32_t data)
372 {
373
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);
377 }
378
379 static inline void
380 regwb(struct machfb_softc *sc, uint32_t index, uint8_t data)
381 {
382
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);
386 }
387
388 static inline void
389 regwb_pll(struct machfb_softc *sc, uint32_t index, uint8_t data)
390 {
391
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);
395 }
396
397 static inline void
398 wait_for_fifo(struct machfb_softc *sc, uint8_t v)
399 {
400
401 while ((regr(sc, FIFO_STAT) & 0xffff) > (0x8000 >> v))
402 ;
403 }
404
405 static inline void
406 wait_for_idle(struct machfb_softc *sc)
407 {
408
409 wait_for_fifo(sc, 16);
410 while ((regr(sc, GUI_STAT) & 1) != 0)
411 ;
412 }
413
414 /*
415 * Inline functions for setting the background and foreground colors.
416 */
417 static inline void machfb_setbg(struct machfb_softc *sc, int bg);
418 static inline void machfb_setfg(struct machfb_softc *sc, int fg);
419
420 static inline void
421 machfb_setbg(struct machfb_softc *sc, int bg)
422 {
423
424 if (bg == sc->sc_bg_cache)
425 return;
426 sc->sc_bg_cache = bg;
427 wait_for_fifo(sc, 1);
428 regw(sc, DP_BKGD_CLR, bg + MACHFB_CMAP_OFF);
429 }
430
431 static inline void
432 machfb_setfg(struct machfb_softc *sc, int fg)
433 {
434
435 if (fg == sc->sc_fg_cache)
436 return;
437 sc->sc_fg_cache = fg;
438 wait_for_fifo(sc, 1);
439 regw(sc, DP_FRGD_CLR, fg + MACHFB_CMAP_OFF);
440 }
441
442 /*
443 * video driver interface
444 */
445 static int
446 machfb_configure(int flags)
447 {
448 struct machfb_softc *sc;
449 phandle_t chosen, output;
450 ihandle_t stdout;
451 bus_addr_t addr;
452 uint32_t id;
453 int i, space;
454
455 /*
456 * For the high-level console probing return the number of
457 * registered adapters.
458 */
459 if (!(flags & VIO_PROBE_ONLY)) {
460 for (i = 0; vid_find_adapter(MACHFB_DRIVER_NAME, i) >= 0; i++)
461 ;
462 return (i);
463 }
464
465 /* Low-level console probing and initialization. */
466
467 sc = &machfb_softc;
468 if (sc->sc_va.va_flags & V_ADP_REGISTERED)
469 goto found;
470
471 if ((chosen = OF_finddevice("/chosen")) == -1) /* Quis contra nos? */
472 return (0);
473 if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1)
474 return (0);
475 if ((output = OF_instance_to_package(stdout)) == -1)
476 return (0);
477 if ((OF_getprop(output, "vendor-id", &id, sizeof(id)) == -1) ||
478 id != ATI_VENDOR)
479 return (0);
480 if (OF_getprop(output, "device-id", &id, sizeof(id)) == -1)
481 return (0);
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;
486 sc->sc_chip_id = id;
487 break;
488 }
489 }
490 if (!(sc->sc_flags & MACHFB_CONSOLE))
491 return (0);
492
493 if (OF_getprop(output, "revision-id", &sc->sc_chip_rev,
494 sizeof(sc->sc_chip_rev)) == -1)
495 return (0);
496 if (OF_decode_addr(output, 0, &space, &addr) != 0)
497 return (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);
503
504 if (machfb_init(0, &sc->sc_va, 0) < 0)
505 return (0);
506
507 found:
508 /* Return number of found adapters. */
509 return (1);
510 }
511
512 static int
513 machfb_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
514 {
515
516 return (0);
517 }
518
519 static int
520 machfb_init(int unit, video_adapter_t *adp, int flags)
521 {
522 struct machfb_softc *sc;
523 phandle_t options;
524 video_info_t *vi;
525 char buf[32];
526 int i;
527 uint8_t dac_mask, dac_rindex, dac_windex;
528
529 sc = (struct machfb_softc *)adp;
530 vi = &adp->va_info;
531
532 if ((regr(sc, CONFIG_CHIP_ID) & 0xffff) != sc->sc_chip_id)
533 return (ENXIO);
534
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;
539 break;
540 }
541 }
542 if (sc->sc_ramdac_freq == 0)
543 return (ENXIO);
544 if (sc->sc_chip_id == ATI_RAGE_II && sc->sc_chip_rev & 0x07)
545 sc->sc_ramdac_freq = 170000;
546
547 vid_init_struct(adp, MACHFB_DRIVER_NAME, -1, unit);
548
549 if (OF_getprop(sc->sc_node, "height", &sc->sc_height,
550 sizeof(sc->sc_height)) == -1)
551 return (ENXIO);
552 if (OF_getprop(sc->sc_node, "width", &sc->sc_width,
553 sizeof(sc->sc_width)) == -1)
554 return (ENXIO);
555 if (OF_getprop(sc->sc_node, "depth", &sc->sc_depth,
556 sizeof(sc->sc_depth)) == -1)
557 return (ENXIO);
558 if ((options = OF_finddevice("/options")) == -1)
559 return (ENXIO);
560 if (OF_getprop(options, "screen-#rows", buf, sizeof(buf)) == -1)
561 return (ENXIO);
562 vi->vi_height = strtol(buf, NULL, 10);
563 if (OF_getprop(options, "screen-#columns", buf, sizeof(buf)) == -1)
564 return (ENXIO);
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;
570
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;
575
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;
581
582 sc->sc_memsize = machfb_get_memsize(sc);
583 if (sc->sc_memsize == 8192)
584 /* The last page is used as register aperture. */
585 sc->sc_memsize -= 4;
586 sc->sc_memtype = regr(sc, CONFIG_STAT0) & 0x07;
587
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;
593 else
594 sc->sc_ref_freq = 14318;
595
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);
604
605 machfb_init_engine(sc);
606 #if 0
607 machfb_adjust_frame(0, 0);
608 #endif
609 machfb_set_mode(adp, 0);
610
611 /*
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.
618 */
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);
628 }
629 regwb(sc, DAC_MASK, dac_mask);
630 regwb(sc, DAC_RINDEX, dac_rindex);
631 regwb(sc, DAC_WINDEX, dac_windex);
632
633 machfb_blank_display(adp, V_DISPLAY_ON);
634 machfb_clear(adp);
635
636 /*
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.
640 */
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)
644 return (ENXIO);
645 adp->va_flags |= V_ADP_REGISTERED;
646
647 return (0);
648 }
649
650 static int
651 machfb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
652 {
653
654 bcopy(&adp->va_info, info, sizeof(*info));
655
656 return (0);
657 }
658
659 static int
660 machfb_query_mode(video_adapter_t *adp, video_info_t *info)
661 {
662
663 return (ENODEV);
664 }
665
666 static int
667 machfb_set_mode(video_adapter_t *adp, int mode)
668 {
669 struct machfb_softc *sc;
670
671 sc = (struct machfb_softc *)adp;
672
673 sc->sc_bg_cache = -1;
674 sc->sc_fg_cache = -1;
675 sc->sc_draw_cache = 0;
676
677 return (0);
678 }
679
680 static int
681 machfb_save_font(video_adapter_t *adp, int page, int size, int width,
682 u_char *data, int c, int count)
683 {
684
685 return (ENODEV);
686 }
687
688 static int
689 machfb_load_font(video_adapter_t *adp, int page, int size, int width,
690 u_char *data, int c, int count)
691 {
692
693 return (ENODEV);
694 }
695
696 static int
697 machfb_show_font(video_adapter_t *adp, int page)
698 {
699
700 return (ENODEV);
701 }
702
703 static int
704 machfb_save_palette(video_adapter_t *adp, u_char *palette)
705 {
706 struct machfb_softc *sc;
707 int i;
708 uint8_t dac_mask, dac_rindex, dac_windex;
709
710 sc = (struct machfb_softc *)adp;
711
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);
722
723 return (0);
724 }
725
726 static int
727 machfb_load_palette(video_adapter_t *adp, u_char *palette)
728 {
729 struct machfb_softc *sc;
730 int i;
731 uint8_t dac_mask, dac_rindex, dac_windex;
732
733 sc = (struct machfb_softc *)adp;
734
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);
745
746 return (0);
747 }
748
749 static int
750 machfb_set_border(video_adapter_t *adp, int border)
751 {
752 struct machfb_softc *sc;
753
754 sc = (struct machfb_softc *)adp;
755
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);
762
763 return (0);
764 }
765
766 static int
767 machfb_save_state(video_adapter_t *adp, void *p, size_t size)
768 {
769
770 return (ENODEV);
771 }
772
773 static int
774 machfb_load_state(video_adapter_t *adp, void *p)
775 {
776
777 return (ENODEV);
778 }
779
780 static int
781 machfb_set_win_org(video_adapter_t *adp, off_t offset)
782 {
783
784 return (ENODEV);
785 }
786
787 static int
788 machfb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
789 {
790
791 *col = 0;
792 *row = 0;
793
794 return (0);
795 }
796
797 static int
798 machfb_set_hw_cursor(video_adapter_t *adp, int col, int row)
799 {
800
801 return (ENODEV);
802 }
803
804 static int
805 machfb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
806 int celsize, int blink)
807 {
808
809 return (ENODEV);
810 }
811
812 static int
813 machfb_blank_display(video_adapter_t *adp, int mode)
814 {
815 struct machfb_softc *sc;
816 uint32_t crtc_gen_cntl;
817
818 sc = (struct machfb_softc *)adp;
819
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);
822 switch (mode) {
823 case V_DISPLAY_ON:
824 break;
825 case V_DISPLAY_BLANK:
826 crtc_gen_cntl |= CRTC_HSYNC_DIS | CRTC_VSYNC_DIS |
827 CRTC_DISPLAY_DIS;
828 break;
829 case V_DISPLAY_STAND_BY:
830 crtc_gen_cntl |= CRTC_HSYNC_DIS | CRTC_DISPLAY_DIS;
831 break;
832 case V_DISPLAY_SUSPEND:
833 crtc_gen_cntl |= CRTC_VSYNC_DIS | CRTC_DISPLAY_DIS;
834 break;
835 }
836 regw(sc, CRTC_GEN_CNTL, crtc_gen_cntl);
837
838 return (0);
839 }
840
841 static int
842 machfb_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
843 int prot, vm_memattr_t *memattr)
844 {
845 struct machfb_softc *sc;
846 video_info_t *vi;
847
848 sc = (struct machfb_softc *)adp;
849 vi = &adp->va_info;
850
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;
855 return (0);
856 }
857
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;
862 return (0);
863 }
864
865 /* 'regular' framebuffer mmap()ing */
866 if (offset < adp->va_window_size) {
867 *paddr = vi->vi_window + offset;
868 return (0);
869 }
870
871 return (EINVAL);
872 }
873
874 static int
875 machfb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
876 {
877 struct machfb_softc *sc;
878 struct fbcursor *fbc;
879 struct fbtype *fb;
880
881 sc = (struct machfb_softc *)adp;
882
883 switch (cmd) {
884 case FBIOGTYPE:
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)
891 fb->fb_cmsize = 0;
892 else
893 fb->fb_cmsize = 1 << sc->sc_depth;
894 fb->fb_size = adp->va_buffer_size;
895 break;
896 case FBIOSCURSOR:
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;
901 } else
902 return (ENODEV);
903 break;
904 default:
905 return (fb_commonioctl(adp, cmd, data));
906 }
907
908 return (0);
909 }
910
911 static int
912 machfb_clear(video_adapter_t *adp)
913 {
914 struct machfb_softc *sc;
915
916 sc = (struct machfb_softc *)adp;
917
918 machfb_fill_rect(adp, (SC_NORM_ATTR >> 4) & 0xf, 0, 0, sc->sc_width,
919 sc->sc_height);
920
921 return (0);
922 }
923
924 static int
925 machfb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
926 {
927 struct machfb_softc *sc;
928
929 sc = (struct machfb_softc *)adp;
930
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;
941 }
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);
948
949 return (0);
950 }
951
952 static int
953 machfb_bitblt(video_adapter_t *adp, ...)
954 {
955
956 return (ENODEV);
957 }
958
959 static int
960 machfb_diag(video_adapter_t *adp, int level)
961 {
962 video_info_t info;
963
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);
967
968 return (0);
969 }
970
971 static int
972 machfb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
973 {
974
975 return (ENODEV);
976 }
977
978 static int
979 machfb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
980 {
981
982 return (ENODEV);
983 }
984
985 static int
986 machfb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
987 {
988
989 return (ENODEV);
990 }
991
992 static int
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)
995 {
996
997 return (ENODEV);
998 }
999
1000 static int
1001 machfb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
1002 {
1003 struct machfb_softc *sc;
1004 const uint8_t *p;
1005 int i;
1006
1007 sc = (struct machfb_softc *)adp;
1008
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 |
1014 FRGD_SRC_FRGD_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;
1021 }
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) +
1030 sc->sc_ymargin));
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]));
1037
1038 return (0);
1039 }
1040
1041 static int
1042 machfb_puts(video_adapter_t *adp, vm_offset_t off, uint16_t *s, int len)
1043 {
1044 struct machfb_softc *sc;
1045 int blanks, i, x1, x2, y1, y2;
1046 uint8_t a, c, color1, color2;
1047
1048 sc = (struct machfb_softc *)adp;
1049
1050 #define MACHFB_BLANK machfb_fill_rect(adp, color1, x1, y1, \
1051 blanks * adp->va_info.vi_cwidth, \
1052 adp->va_info.vi_cheight)
1053
1054 blanks = color1 = x1 = y1 = 0;
1055 for (i = 0; i < len; i++) {
1056 /*
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.
1061 */
1062 c = s[i] & 0xff;
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;
1070 if (blanks == 0) {
1071 color1 = color2;
1072 x1 = x2;
1073 y1 = y2;
1074 blanks++;
1075 } else if (color1 != color2 || y1 != y2) {
1076 MACHFB_BLANK;
1077 color1 = color2;
1078 x1 = x2;
1079 y1 = y2;
1080 blanks = 1;
1081 } else
1082 blanks++;
1083 } else {
1084 if (blanks != 0) {
1085 MACHFB_BLANK;
1086 blanks = 0;
1087 }
1088 vidd_putc(adp, off + i, c, a);
1089 }
1090 }
1091 if (blanks != 0)
1092 MACHFB_BLANK;
1093
1094 #undef MACHFB_BLANK
1095
1096 return (0);
1097 }
1098
1099 static int
1100 machfb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
1101 uint32_t pixel_mask, int size, int width)
1102 {
1103 struct machfb_softc *sc;
1104 int error;
1105
1106 sc = (struct machfb_softc *)adp;
1107
1108 if ((!(sc->sc_flags & MACHFB_CUREN)) &&
1109 (error = machfb_cursor_install(sc)) < 0)
1110 return (error);
1111 else {
1112 /*
1113 * The hardware cursor always must be disabled when
1114 * fiddling with its bits otherwise some artifacts
1115 * may appear on the screen.
1116 */
1117 machfb_cursor_enable(sc, 0);
1118 }
1119
1120 regw(sc, CUR_HORZ_VERT_OFF, 0);
1121 if ((regr(sc, GEN_TEST_CNTL) & CRTC_DBL_SCAN_EN) != 0)
1122 y <<= 1;
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;
1127
1128 return (0);
1129 }
1130
1131 /*
1132 * PCI bus interface
1133 */
1134 static int
1135 machfb_pci_probe(device_t dev)
1136 {
1137 int i;
1138
1139 if (pci_get_class(dev) != PCIC_DISPLAY ||
1140 pci_get_subclass(dev) != PCIS_DISPLAY_VGA)
1141 return (ENXIO);
1142
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);
1147 }
1148 }
1149
1150 return (ENXIO);
1151 }
1152
1153 static int
1154 machfb_pci_attach(device_t dev)
1155 {
1156 struct machfb_softc *sc;
1157 video_adapter_t *adp;
1158 video_switch_t *sw;
1159 video_info_t *vi;
1160 phandle_t node;
1161 int error, i, rid;
1162 uint32_t *p32, u32;
1163 uint8_t *p;
1164
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);
1170 } else {
1171 sc = device_get_softc(dev);
1172
1173 sc->sc_node = node;
1174 sc->sc_chip_id = pci_get_device(dev);
1175 sc->sc_chip_rev = pci_get_revid(dev);
1176 }
1177 adp = &sc->sc_va;
1178 vi = &adp->va_info;
1179
1180 rid = PCIR_BAR(0);
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");
1184 return (ENXIO);
1185 }
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;
1194 else {
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");
1199 error = ENXIO;
1200 goto fail_memres;
1201 }
1202 adp->va_mem_base =
1203 (vm_offset_t)rman_get_virtual(sc->sc_memres);
1204 }
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");
1212 error = ENXIO;
1213 goto fail_memmap;
1214 }
1215
1216 /*
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).
1226 */
1227 rid = PCIR_BAR(2);
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) {
1231 device_printf(dev,
1232 "cannot allocate VGA memory resources\n");
1233 error = ENXIO;
1234 goto fail_memmap;
1235 }
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) {
1244 device_printf(dev,
1245 "cannot map VGA memory resources\n");
1246 error = ENXIO;
1247 goto fail_vmemres;
1248 }
1249 adp->va_registers =
1250 (vm_offset_t)rman_get_virtual(sc->sc_vmemres);
1251 adp->va_registers_size = vi->vi_registers_size;
1252 }
1253
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");
1257 error = ENODEV;
1258 goto fail_vmemmap;
1259 }
1260 /*
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
1267 * unused one.
1268 */
1269 for (i = 0; i < devclass_get_maxunit(machfb_devclass); i++)
1270 if (vid_find_adapter(MACHFB_DRIVER_NAME, i) < 0)
1271 break;
1272 if ((error = sw->init(i, adp, 0)) != 0) {
1273 device_printf(dev, "cannot initialize adapter\n");
1274 goto fail_vmemmap;
1275 }
1276 }
1277
1278 /*
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.
1284 */
1285 p32 = (uint32_t *)adp->va_buffer;
1286 u32 = *p32;
1287 p = (uint8_t *)adp->va_buffer;
1288 *p32 = 0x12345678;
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;
1295 } else {
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;
1300 }
1301 *p32 = u32;
1302 adp->va_window_gran = adp->va_window_size;
1303
1304 device_printf(dev,
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) ?
1308 "" : "not ");
1309 device_printf(dev,
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);
1317
1318 /*
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.
1324 */
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);
1330
1331 /*
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.
1337 */
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);
1342
1343 return (0);
1344
1345 fail_vmemmap:
1346 if (adp->va_registers != 0)
1347 bus_space_unmap(sc->sc_vmemt, sc->sc_vmemh,
1348 vi->vi_registers_size);
1349 fail_vmemres:
1350 if (sc->sc_vmemres != NULL)
1351 bus_release_resource(dev, SYS_RES_MEMORY,
1352 rman_get_rid(sc->sc_vmemres), sc->sc_vmemres);
1353 fail_memmap:
1354 bus_space_unmap(sc->sc_memt, sc->sc_memh, vi->vi_buffer_size);
1355 fail_memres:
1356 bus_release_resource(dev, SYS_RES_MEMORY,
1357 rman_get_rid(sc->sc_memres), sc->sc_memres);
1358
1359 return (error);
1360 }
1361
1362 static int
1363 machfb_pci_detach(device_t dev)
1364 {
1365
1366 return (EINVAL);
1367 }
1368
1369 /*
1370 * internal functions
1371 */
1372 static void
1373 machfb_cursor_enable(struct machfb_softc *sc, int onoff)
1374 {
1375
1376 if (onoff)
1377 regw(sc, GEN_TEST_CNTL,
1378 regr(sc, GEN_TEST_CNTL) | HWCURSOR_ENABLE);
1379 else
1380 regw(sc, GEN_TEST_CNTL,
1381 regr(sc, GEN_TEST_CNTL) &~ HWCURSOR_ENABLE);
1382 }
1383
1384 static int
1385 machfb_cursor_install(struct machfb_softc *sc)
1386 {
1387 uint16_t *p, v;
1388 uint8_t fg;
1389 int i, j;
1390
1391 if (sc->sc_curoff == 0)
1392 return (ENODEV);
1393
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);
1409 else
1410 *(p++) = v;
1411 }
1412 }
1413
1414 return (0);
1415 }
1416
1417 static int
1418 machfb_get_memsize(struct machfb_softc *sc)
1419 {
1420 int tmp, memsize;
1421 const int mem_tab[] = {
1422 512, 1024, 2048, 4096, 6144, 8192, 12288, 16384
1423 };
1424
1425 tmp = regr(sc, MEM_CNTL);
1426 #ifdef MACHFB_DEBUG
1427 printf("memcntl=0x%08x\n", tmp);
1428 #endif
1429 if (sc->sc_flags & MACHFB_DSP) {
1430 tmp &= 0x0000000f;
1431 if (tmp < 8)
1432 memsize = (tmp + 1) * 512;
1433 else if (tmp < 12)
1434 memsize = (tmp - 3) * 1024;
1435 else
1436 memsize = (tmp - 7) * 2048;
1437 } else
1438 memsize = mem_tab[tmp & 0x07];
1439
1440 return (memsize);
1441 }
1442
1443 static void
1444 machfb_reset_engine(struct machfb_softc *sc)
1445 {
1446
1447 /* Reset engine.*/
1448 regw(sc, GEN_TEST_CNTL, regr(sc, GEN_TEST_CNTL) & ~GUI_ENGINE_ENABLE);
1449
1450 /* Enable engine. */
1451 regw(sc, GEN_TEST_CNTL, regr(sc, GEN_TEST_CNTL) | GUI_ENGINE_ENABLE);
1452
1453 /*
1454 * Ensure engine is not locked up by clearing any FIFO or
1455 * host errors.
1456 */
1457 regw(sc, BUS_CNTL, regr(sc, BUS_CNTL) | BUS_HOST_ERR_ACK |
1458 BUS_FIFO_ERR_ACK);
1459 }
1460
1461 static void
1462 machfb_init_engine(struct machfb_softc *sc)
1463 {
1464 uint32_t pitch_value;
1465
1466 pitch_value = sc->sc_width;
1467
1468 if (sc->sc_depth == 24)
1469 pitch_value *= 3;
1470
1471 machfb_reset_engine(sc);
1472
1473 wait_for_fifo(sc, 14);
1474
1475 regw(sc, CONTEXT_MASK, 0xffffffff);
1476
1477 regw(sc, DST_OFF_PITCH, (pitch_value / 8) << 22);
1478
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);
1484
1485 regw(sc, DST_CNTL, DST_LAST_PEL | DST_X_LEFT_TO_RIGHT |
1486 DST_Y_TOP_TO_BOTTOM);
1487
1488 regw(sc, SRC_OFF_PITCH, (pitch_value / 8) << 22);
1489
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);
1494
1495 regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
1496
1497 wait_for_fifo(sc, 13);
1498 regw(sc, HOST_CNTL, 0);
1499
1500 regw(sc, PAT_REG0, 0);
1501 regw(sc, PAT_REG1, 0);
1502 regw(sc, PAT_CNTL, 0);
1503
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);
1508
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);
1513
1514 regw(sc, DP_SRC, FRGD_SRC_FRGD_CLR);
1515
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);
1520
1521 wait_for_fifo(sc, 2);
1522 switch (sc->sc_depth) {
1523 case 8:
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);
1527 break;
1528 #if 0
1529 case 32:
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);
1533 break;
1534 #endif
1535 }
1536
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);
1540
1541 wait_for_idle(sc);
1542 }
1543
1544 #if 0
1545 static void
1546 machfb_adjust_frame(struct machfb_softc *sc, int x, int y)
1547 {
1548 int offset;
1549
1550 offset = ((x + y * sc->sc_width) * (sc->sc_depth >> 3)) >> 3;
1551
1552 regw(sc, CRTC_OFF_PITCH, (regr(sc, CRTC_OFF_PITCH) & 0xfff00000) |
1553 offset);
1554 }
1555 #endif
1556
1557 static void
1558 machfb_shutdown_final(void *v)
1559 {
1560 struct machfb_softc *sc = v;
1561
1562 machfb_cursor_enable(sc, 0);
1563 /*
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.
1567 */
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);
1571 }
1572 }
1573
1574 static void
1575 machfb_shutdown_reset(void *v)
1576 {
1577 struct machfb_softc *sc = v;
1578
1579 machfb_blank_display(&sc->sc_va, V_DISPLAY_STAND_BY);
1580 }
Cache object: 44ebfcfd0c911a6ed151d295777af669
|