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