machfb.c revision 146482
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 146482 2005-05-21 20:47:38Z 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 *v);
295static void machfb_shutdown_reset(void *v);
296
297static int machfb_configure(int flags);
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	sc = &machfb_softc;
455
456	if ((chosen = OF_finddevice("/chosen")) == -1)	/* Quis contra nos? */
457		return (0);
458	if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1)
459		return (0);
460	if ((output = OF_instance_to_package(stdout)) == -1)
461		return (0);
462	if ((OF_getprop(output, "vendor-id", &id, sizeof(id)) == -1) ||
463	    id != ATI_VENDOR)
464		return (0);
465	if (OF_getprop(output, "device-id", &id, sizeof(id)) == -1)
466		return (0);
467	for (i = 0; i < sizeof(machfb_info) / sizeof(machfb_info[0]); i++) {
468		if (id == machfb_info[i].chip_id) {
469			/*
470			 * When being called a second time, i.e. during
471			 * sc_probe_unit(), just return at this point.
472			 * Note that the polarity of the VIO_PROBE_ONLY
473			 * flag is somewhat non-intuitive.
474			 */
475			if (!(flags & VIO_PROBE_ONLY))
476				goto found;
477			sc->sc_flags = MACHFB_CONSOLE;
478			sc->sc_node = output;
479			sc->sc_chip_id = id;
480			break;
481		}
482	}
483	if (!(sc->sc_flags & MACHFB_CONSOLE))
484		return (0);
485
486	if (OF_getprop(output, "revision-id", &sc->sc_chip_rev,
487	    sizeof(sc->sc_chip_rev)) == -1)
488		return (0);
489	if (OF_decode_addr(output, 0, &space, &addr) != 0)
490		return (0);
491	sc->sc_memt = &machfb_bst_store[0];
492	sc->sc_memh = sparc64_fake_bustag(space, addr, sc->sc_memt);
493	sc->sc_regt = sc->sc_memt;
494	bus_space_subregion(sc->sc_regt, sc->sc_memh, MACH64_REG_OFF,
495	    MACH64_REG_SIZE, &sc->sc_regh);
496
497	if (machfb_init(0, &sc->sc_va, 0) < 0)
498		 return (0);
499
500 found:
501	/* Return number of found adapters. */
502	return (1);
503}
504
505static int
506machfb_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
507{
508
509	return (0);
510}
511
512static int
513machfb_init(int unit, video_adapter_t *adp, int flags)
514{
515	struct machfb_softc *sc;
516	phandle_t options;
517	video_info_t *vi;
518	char buf[32];
519	int i, j;
520
521	sc = (struct machfb_softc *)adp;
522	vi = &adp->va_info;
523
524	if ((regr(sc, CONFIG_CHIP_ID) & 0xffff) != sc->sc_chip_id)
525		return (ENXIO);
526
527	sc->sc_ramdac_freq = 0;
528	for (i = 0; i < sizeof(machfb_info) / sizeof(machfb_info[0]); i++) {
529		if (sc->sc_chip_id == machfb_info[i].chip_id) {
530			sc->sc_ramdac_freq = machfb_info[i].ramdac_freq;
531			break;
532		}
533	}
534	if (sc->sc_ramdac_freq == 0)
535		return (ENXIO);
536	if (sc->sc_chip_id == ATI_RAGE_II && sc->sc_chip_rev & 0x07)
537		sc->sc_ramdac_freq = 170000;
538
539	vid_init_struct(adp, MACHFB_DRIVER_NAME, -1, unit);
540
541 	if (OF_getprop(sc->sc_node, "height", &sc->sc_height,
542	    sizeof(sc->sc_height)) == -1)
543		return (ENXIO);
544	if (OF_getprop(sc->sc_node, "width", &sc->sc_width,
545	    sizeof(sc->sc_width)) == -1)
546		return (ENXIO);
547	if (OF_getprop(sc->sc_node, "depth", &sc->sc_depth,
548	    sizeof(sc->sc_depth)) == -1)
549		return (ENXIO);
550	if ((options = OF_finddevice("/options")) == -1)
551		return (ENXIO);
552	if (OF_getprop(options, "screen-#rows", buf, sizeof(buf)) == -1)
553		return (ENXIO);
554	vi->vi_height = strtol(buf, NULL, 10);
555	if (OF_getprop(options, "screen-#columns", buf, sizeof(buf)) == -1)
556		return (ENXIO);
557	vi->vi_width = strtol(buf, NULL, 10);
558	vi->vi_cwidth = 12;
559	vi->vi_cheight = 22;
560	vi->vi_flags = V_INFO_COLOR;
561	vi->vi_mem_model = V_INFO_MM_OTHER;
562
563	sc->sc_draw_cache = -1;
564	sc->sc_font = gallant12x22_data;
565	sc->sc_cbwidth = howmany(vi->vi_cwidth, 8);	/* width in bytes */
566	sc->sc_xmargin = (sc->sc_width - (vi->vi_width * vi->vi_cwidth)) / 2;
567	sc->sc_ymargin = (sc->sc_height - (vi->vi_height * vi->vi_cheight)) / 2;
568
569	if (sc->sc_chip_id != ATI_MACH64_CT &&
570	    !((sc->sc_chip_id == ATI_MACH64_VT ||
571	    sc->sc_chip_id == ATI_RAGE_II) &&
572	    (sc->sc_chip_rev & 0x07) == 0))
573		sc->sc_flags |= MACHFB_DSP;
574
575	sc->sc_memsize = machfb_get_memsize(sc);
576	if (sc->sc_memsize == 8192)
577		/* The last page is used as register aperture. */
578		sc->sc_memsize -= 4;
579	sc->sc_memtype = regr(sc, CONFIG_STAT0) & 0x07;
580
581	if ((sc->sc_chip_id >= ATI_RAGE_XC_PCI66 &&
582	    sc->sc_chip_id <= ATI_RAGE_XL_PCI66) ||
583	    (sc->sc_chip_id >= ATI_RAGE_XL_PCI &&
584	    sc->sc_chip_id <= ATI_RAGE_XC_PCI))
585		sc->sc_ref_freq = 29498;
586	else
587		sc->sc_ref_freq = 14318;
588
589	regwb(sc, CLOCK_CNTL + 1, PLL_REF_DIV << 2);
590	sc->sc_ref_div = regrb(sc, CLOCK_CNTL + 2);
591	regwb(sc, CLOCK_CNTL + 1, MCLK_FB_DIV << 2);
592	sc->sc_mclk_fb_div = regrb(sc, CLOCK_CNTL + 2);
593	sc->sc_mem_freq = (2 * sc->sc_ref_freq * sc->sc_mclk_fb_div) /
594	    (sc->sc_ref_div * 2);
595	sc->sc_mclk_post_div = (sc->sc_mclk_fb_div * 2 * sc->sc_ref_freq) /
596	    (sc->sc_mem_freq * sc->sc_ref_div);
597
598	machfb_init_engine(sc);
599#if 0
600	mach64_adjust_frame(0, 0);
601#endif
602
603	sc->sc_dacw = -1;
604	for (i = 0; i < 16; i++)
605		for (j = 0; j < 16; j++)
606			machfb_putpalreg(sc, (i * 16) + j,
607			    machfb_default_cmap[j].red,
608			    machfb_default_cmap[j].green,
609			    machfb_default_cmap[j].blue);
610
611	machfb_blank_display(adp, V_DISPLAY_BLANK);
612
613	adp->va_flags |= V_ADP_COLOR | V_ADP_BORDER | V_ADP_INITIALIZED;
614	if (vid_register(adp) < 0)
615		return (ENXIO);
616	adp->va_flags |= V_ADP_REGISTERED;
617
618	return (0);
619}
620
621static int
622machfb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
623{
624
625	bcopy(&adp->va_info, info, sizeof(*info));
626
627	return (0);
628}
629
630static int
631machfb_query_mode(video_adapter_t *adp, video_info_t *info)
632{
633
634	return (ENODEV);
635}
636
637static int
638machfb_set_mode(video_adapter_t *adp, int mode)
639{
640
641	return (ENODEV);
642}
643
644static int
645machfb_save_font(video_adapter_t *adp, int page, int size, u_char *data,
646    int c, int count)
647{
648
649	return (ENODEV);
650}
651
652static int
653machfb_load_font(video_adapter_t *adp, int page, int size, u_char *data,
654    int c, int count)
655{
656
657	return (ENODEV);
658}
659
660static int
661machfb_show_font(video_adapter_t *adp, int page)
662{
663
664	return (ENODEV);
665}
666
667static int
668machfb_save_palette(video_adapter_t *adp, u_char *palette)
669{
670
671	return (ENODEV);
672}
673
674static int
675machfb_load_palette(video_adapter_t *adp, u_char *palette)
676{
677
678	return (ENODEV);
679}
680
681static int
682machfb_set_border(video_adapter_t *adp, int border)
683{
684	struct machfb_softc *sc;
685
686	sc = (struct machfb_softc *)adp;
687
688	machfb_fill_rect(adp, border, 0, 0, sc->sc_width, sc->sc_ymargin);
689	machfb_fill_rect(adp, border, 0, sc->sc_height - sc->sc_ymargin,
690	    sc->sc_width, sc->sc_ymargin);
691	machfb_fill_rect(adp, border, 0, 0, sc->sc_xmargin, sc->sc_height);
692	machfb_fill_rect(adp, border, sc->sc_width - sc->sc_xmargin, 0,
693	    sc->sc_xmargin, sc->sc_height);
694
695	return (0);
696}
697
698static int
699machfb_save_state(video_adapter_t *adp, void *p, size_t size)
700{
701
702	return (ENODEV);
703}
704
705static int
706machfb_load_state(video_adapter_t *adp, void *p)
707{
708
709	return (ENODEV);
710}
711
712static int
713machfb_set_win_org(video_adapter_t *adp, off_t offset)
714{
715
716	return (ENODEV);
717}
718
719static int
720machfb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
721{
722
723	*col = 0;
724	*row = 0;
725
726	return (0);
727}
728
729static int
730machfb_set_hw_cursor(video_adapter_t *adp, int col, int row)
731{
732
733	return (ENODEV);
734}
735
736static int
737machfb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
738    int celsize, int blink)
739{
740
741	return (ENODEV);
742}
743
744static int
745machfb_blank_display(video_adapter_t *adp, int mode)
746{
747	struct machfb_softc *sc;
748
749	sc = (struct machfb_softc *)adp;
750
751	if (mode == V_DISPLAY_ON || mode == V_DISPLAY_BLANK)
752		regw(sc, CRTC_GEN_CNTL, (regr(sc, CRTC_GEN_CNTL) | CRTC_EN |
753		    CRTC_EXT_DISP_EN) & ~CRTC_DISPLAY_DIS);
754	else
755		regw(sc, CRTC_GEN_CNTL, regr(sc, CRTC_GEN_CNTL) & ~CRTC_EN);
756	if (mode == V_DISPLAY_BLANK)
757		machfb_fill_rect(adp, (SC_NORM_ATTR >> 4) & 0xf, 0, 0,
758		    sc->sc_width, sc->sc_height);
759
760	return (0);
761}
762
763static int
764machfb_mmap(video_adapter_t *adp, vm_offset_t offset, vm_paddr_t *paddr,
765    int prot)
766{
767	struct machfb_softc *sc;
768
769	sc = (struct machfb_softc *)adp;
770
771	if (adp->va_io_base != 0 && offset >= adp->va_io_base &&
772	    offset < adp->va_io_base + adp->va_io_size) {
773		*paddr = sc->sc_vioh + offset - adp->va_io_size;
774		return (0);
775	}
776
777	if (adp->va_mem_base != 0 && offset >= adp->va_mem_base &&
778	    offset < adp->va_mem_base + adp->va_mem_size) {
779	    	*paddr = sc->sc_vmemh + offset - adp->va_mem_base;
780		return (0);
781	}
782
783	if (offset >= adp->va_registers &&
784	    offset < adp->va_registers + adp->va_registers_size) {
785	    	*paddr = sc->sc_memh + offset - adp->va_registers;
786		return (0);
787	}
788
789	/* 'regular' framebuffer mmap()ing */
790	if (offset < adp->va_window_size) {
791		*paddr = adp->va_window + offset;
792		return (0);
793	}
794
795	return (EINVAL);
796}
797
798static int
799machfb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
800{
801	struct machfb_softc *sc;
802	struct fbcursor *fbc;
803	struct fbtype *fb;
804
805	sc = (struct machfb_softc *)adp;
806
807	switch (cmd) {
808	case FBIOGTYPE:
809		fb = (struct fbtype *)data;
810		fb->fb_type = FBTYPE_PCIMISC;
811		fb->fb_height = sc->sc_height;
812		fb->fb_width = sc->sc_width;
813		fb->fb_depth = sc->sc_depth;
814		if (sc->sc_depth <= 1 || sc->sc_depth > 8)
815			fb->fb_cmsize = 0;
816		else
817			fb->fb_cmsize = 1 << sc->sc_depth;
818		fb->fb_size = adp->va_buffer_size;
819		break;
820	case FBIOSCURSOR:
821		fbc = (struct fbcursor *)data;
822		if (fbc->set & FB_CUR_SETCUR) {
823			if (fbc->enable == 0) {
824				machfb_cursor_enable(sc, 0);
825				sc->sc_flags &= ~MACHFB_CUREN;
826			} else
827				return (ENODEV);
828		}
829		break;
830	default:
831		return (fb_commonioctl(adp, cmd, data));
832	}
833
834	return (0);
835}
836
837static int
838machfb_clear(video_adapter_t *adp)
839{
840
841	return (ENODEV);
842}
843
844static int
845machfb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
846{
847	struct machfb_softc *sc;
848
849	sc = (struct machfb_softc *)adp;
850
851	if (sc->sc_draw_cache != MACHFB_DRAW_FILLRECT) {
852		wait_for_fifo(sc, 7);
853		regw(sc, DP_WRITE_MASK, 0xff);
854		regw(sc, DP_PIX_WIDTH, DST_8BPP | SRC_8BPP | HOST_8BPP);
855		regw(sc, DP_SRC, FRGD_SRC_FRGD_CLR);
856		regw(sc, DP_MIX, MIX_SRC << 16);
857		regw(sc, CLR_CMP_CNTL, 0);	/* no transparency */
858		regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
859		regw(sc, DST_CNTL, DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM);
860		sc->sc_draw_cache = MACHFB_DRAW_FILLRECT;
861	}
862	wait_for_fifo(sc, 5);
863	regw(sc, DP_FRGD_CLR, val);
864	regw(sc, SRC_Y_X, (x << 16) | y);
865	regw(sc, SRC_WIDTH1, cx);
866	regw(sc, DST_Y_X, (x << 16) | y);
867	regw(sc, DST_HEIGHT_WIDTH, (cx << 16) | cy);
868
869	return (0);
870}
871
872static int
873machfb_bitblt(video_adapter_t *adp, ...)
874{
875
876	return (ENODEV);
877}
878
879static int
880machfb_diag(video_adapter_t *adp, int level)
881{
882	video_info_t info;
883
884	fb_dump_adp_info(adp->va_name, adp, level);
885	machfb_get_info(adp, 0, &info);
886	fb_dump_mode_info(adp->va_name, adp, &info, level);
887
888	return (0);
889}
890
891static int
892machfb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
893{
894
895	return (ENODEV);
896}
897
898static int
899machfb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
900{
901
902	return (ENODEV);
903}
904
905static int
906machfb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
907{
908
909	return (ENODEV);
910}
911
912static int
913machfb_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p, uint32_t a,
914    int size, int bpp, int bit_ltor, int byte_ltor)
915{
916
917	return (ENODEV);
918}
919
920static int
921machfb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
922{
923	struct machfb_softc *sc;
924	uint8_t *p;
925	int i;
926
927	sc = (struct machfb_softc *)adp;
928
929	if (sc->sc_draw_cache != MACHFB_DRAW_CHAR) {
930		wait_for_fifo(sc, 8);
931		regw(sc, DP_WRITE_MASK, 0xff);	/* XXX only good for 8 bit */
932		regw(sc, DP_PIX_WIDTH, DST_8BPP | SRC_1BPP | HOST_1BPP);
933		regw(sc, DP_SRC, MONO_SRC_HOST | BKGD_SRC_BKGD_CLR |
934		    FRGD_SRC_FRGD_CLR);
935		regw(sc, DP_MIX ,((MIX_SRC & 0xffff) << 16) | MIX_SRC);
936		regw(sc, CLR_CMP_CNTL, 0);	/* no transparency */
937		regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
938		regw(sc, DST_CNTL, DST_Y_TOP_TO_BOTTOM | DST_X_LEFT_TO_RIGHT);
939		regw(sc, HOST_CNTL, HOST_BYTE_ALIGN);
940		sc->sc_draw_cache = MACHFB_DRAW_CHAR;
941	}
942	p = sc->sc_font + (c * adp->va_info.vi_cheight * sc->sc_cbwidth);
943	wait_for_fifo(sc, 6 + (adp->va_info.vi_cheight / sc->sc_cbwidth));
944	regw(sc, DP_BKGD_CLR, (a >> 4) & 0xf);
945	regw(sc, DP_FRGD_CLR, a & 0xf);
946	regw(sc, SRC_Y_X, 0);
947	regw(sc, SRC_WIDTH1, adp->va_info.vi_cwidth);
948	regw(sc, DST_Y_X, ((((off % adp->va_info.vi_width) *
949	    adp->va_info.vi_cwidth) + sc->sc_xmargin) << 16) |
950	    (((off / adp->va_info.vi_width) * adp->va_info.vi_cheight) +
951	    sc->sc_ymargin));
952	regw(sc, DST_HEIGHT_WIDTH, (adp->va_info.vi_cwidth << 16) |
953	    adp->va_info.vi_cheight);
954	for (i = 0; i < adp->va_info.vi_cheight * sc->sc_cbwidth; i += 4)
955		regw(sc, HOST_DATA0 + i, (p[i + 3] << 24 | p[i + 2] << 16 |
956		    p[i + 1] << 8 | p[i]));
957
958	return (0);
959}
960
961static int
962machfb_puts(video_adapter_t *adp, vm_offset_t off, uint16_t *s, int len)
963{
964	struct machfb_softc *sc;
965	int blanks, i, x1, x2, y1, y2;
966	uint8_t a, c, color1, color2;
967
968	sc = (struct machfb_softc *)adp;
969
970#define	MACHFB_BLANK	machfb_fill_rect(adp, color1, x1, y1,		\
971			    blanks * adp->va_info.vi_cwidth,		\
972			    adp->va_info.vi_cheight)
973
974	blanks = color1 = x1 = y1 = 0;
975	for (i = 0; i < len; i++) {
976		/*
977		 * Accelerate continuous blanks by drawing a respective
978		 * rectangle instead. Drawing a rectangle of any size
979		 * takes about the same number of operations as drawing
980		 * a single character.
981		 */
982		c = s[i] & 0xff;
983		a = (s[i] & 0xff00) >> 8;
984		if (c == 0x00 || c == 0x20 || c == 0xdb || c == 0xff) {
985			color2 = (a >> (c == 0xdb ? 0 : 4) & 0xf);
986	    		x2 = (((off + i) % adp->va_info.vi_width) *
987			    adp->va_info.vi_cwidth) + sc->sc_xmargin;
988			y2 = (((off + i) / adp->va_info.vi_width) *
989			    adp->va_info.vi_cheight) + sc->sc_ymargin;
990			if (blanks == 0) {
991				color1 = color2;
992				x1 = x2;
993				y1 = y2;
994				blanks++;
995			} else if (color1 != color2 || y1 != y2) {
996				MACHFB_BLANK;
997				color1 = color2;
998				x1 = x2;
999				y1 = y2;
1000				blanks = 1;
1001			} else
1002				blanks++;
1003		} else {
1004			if (blanks != 0) {
1005				MACHFB_BLANK;
1006				blanks = 0;
1007			}
1008			(*vidsw[adp->va_index]->putc)(adp, off + i, c, a);
1009		}
1010	}
1011	if (blanks != 0)
1012		MACHFB_BLANK;
1013
1014#undef MACHFB_BLANK
1015
1016	return (0);
1017}
1018
1019static int
1020machfb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
1021    uint32_t pixel_mask, int size)
1022{
1023	struct machfb_softc *sc;
1024	int error;
1025
1026	sc = (struct machfb_softc *)adp;
1027
1028	if ((!(sc->sc_flags & MACHFB_CUREN)) &&
1029	    (error = machfb_cursor_install(sc)) < 0)
1030	    	return (error);
1031	else {
1032		/*
1033		 * The hardware cursor always must be disabled when
1034		 * fiddling with its bits otherwise some artifacts
1035		 * may appear on the screen.
1036		 */
1037		machfb_cursor_enable(sc, 0);
1038	}
1039
1040	regw(sc, CUR_HORZ_VERT_OFF, 0);
1041	if ((regr(sc, GEN_TEST_CNTL) & CRTC_DBL_SCAN_EN) != 0)
1042		y <<= 1;
1043	regw(sc, CUR_HORZ_VERT_POSN, ((y + sc->sc_ymargin) << 16) |
1044	    (x + sc->sc_xmargin));
1045	machfb_cursor_enable(sc, 1);
1046	sc->sc_flags |= MACHFB_CUREN;
1047
1048	return (0);
1049}
1050
1051/*
1052 * PCI bus interface
1053 */
1054static int
1055machfb_pci_probe(device_t dev)
1056{
1057	int i;
1058
1059	if (pci_get_class(dev) != PCIC_DISPLAY ||
1060	    pci_get_subclass(dev) != PCIS_DISPLAY_VGA)
1061		return (ENXIO);
1062
1063	for (i = 0; i < sizeof(machfb_info) / sizeof(machfb_info[0]); i++) {
1064		if (pci_get_device(dev) == machfb_info[i].chip_id) {
1065			device_set_desc(dev, machfb_info[i].name);
1066			return (BUS_PROBE_DEFAULT);
1067		}
1068	}
1069
1070	return (ENXIO);
1071}
1072
1073static int
1074machfb_pci_attach(device_t dev)
1075{
1076	struct machfb_softc *sc;
1077	video_adapter_t *adp;
1078	video_switch_t *sw;
1079	phandle_t node;
1080	uint32_t *p32, saved_value;
1081	uint8_t *p;
1082	int error, i;
1083
1084	node = ofw_bus_get_node(dev);
1085	if ((sc = (struct machfb_softc *)vid_get_adapter(vid_find_adapter(
1086	    MACHFB_DRIVER_NAME, 0))) != NULL && sc->sc_node == node) {
1087	    	device_printf(dev, "console\n");
1088	    	device_set_softc(dev, sc);
1089	} else {
1090		sc = device_get_softc(dev);
1091		bzero(sc, sizeof(struct machfb_softc));
1092
1093		sc->sc_node = node;
1094		sc->sc_chip_id = pci_get_device(dev);
1095		sc->sc_chip_rev = pci_get_revid(dev);
1096	}
1097	adp = &sc->sc_va;
1098
1099	/*
1100	 * Regardless whether we are the console and already allocated
1101	 * resources in machfb_configure() or not we have to allocate
1102	 * them here (again) in order for rman_get_virtual() to work.
1103	 */
1104
1105	/* Enable memory and IO access. */
1106	pci_write_config(dev, PCIR_COMMAND,
1107	    pci_read_config(dev, PCIR_COMMAND, 2) | PCIM_CMD_PORTEN |
1108	    PCIM_CMD_MEMEN, 2);
1109
1110	sc->sc_memrid = PCIR_BAR(0);
1111	if ((sc->sc_memres = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
1112	    &sc->sc_memrid, RF_ACTIVE)) == NULL) {
1113		device_printf(dev, "cannot allocate memory resources\n");
1114		return (ENXIO);
1115	}
1116	sc->sc_memt = rman_get_bustag(sc->sc_memres);
1117	sc->sc_memh = rman_get_bushandle(sc->sc_memres);
1118	adp->va_registers = rman_get_start(sc->sc_memres);
1119	adp->va_registers_size = rman_get_size(sc->sc_memres);
1120	sc->sc_regt = sc->sc_memt;
1121	bus_space_subregion(sc->sc_regt, sc->sc_memh, MACH64_REG_OFF,
1122	    MACH64_REG_SIZE, &sc->sc_regh);
1123    	adp->va_buffer = (vm_offset_t)rman_get_virtual(sc->sc_memres);
1124	adp->va_buffer_size = rman_get_size(sc->sc_memres);
1125
1126	/*
1127	 * Depending on the firmware version the VGA I/O and/or memory
1128	 * resources of the Mach64 chips come up disabled. We generally
1129	 * enable them above (pci(4) actually already did this unless
1130	 * pci_enable_io_modes is not set) but this doesn't necessarily
1131	 * mean that we get valid ones. Invalid resources seem to have
1132	 * in common that they start at address 0. We don't allocate
1133	 * them in this case in order to avoid warnings in apb(4) and
1134	 * crashes when using these invalid resources. Xorg is aware
1135	 * of this and doesn't use the VGA resources in this case (but
1136	 * demands them if they are valid).
1137	 */
1138	sc->sc_viorid = PCIR_BAR(1);
1139	if (bus_get_resource_start(dev, SYS_RES_IOPORT, sc->sc_viorid) != 0) {
1140		if ((sc->sc_viores = bus_alloc_resource_any(dev,
1141		    SYS_RES_IOPORT, &sc->sc_viorid, RF_ACTIVE)) == NULL) {
1142			device_printf(dev,
1143			    "cannot allocate VGA I/O resources\n");
1144			error = ENXIO;
1145			goto fail_memres;
1146		}
1147		sc->sc_viot = rman_get_bustag(sc->sc_viores);
1148		sc->sc_vioh = rman_get_bushandle(sc->sc_viores);
1149		adp->va_io_base = rman_get_start(sc->sc_viores);
1150		adp->va_io_size = rman_get_size(sc->sc_viores);
1151	}
1152
1153	sc->sc_vmemrid = PCIR_BAR(2);
1154	if (bus_get_resource_start(dev, SYS_RES_MEMORY, sc->sc_vmemrid) != 0) {
1155		if ((sc->sc_vmemres = bus_alloc_resource_any(dev,
1156		    SYS_RES_MEMORY, &sc->sc_vmemrid, RF_ACTIVE)) == NULL) {
1157			device_printf(dev,
1158			    "cannot allocate VGA memory resources\n");
1159			error = ENXIO;
1160			goto fail_viores;
1161		}
1162		sc->sc_vmemt = rman_get_bustag(sc->sc_vmemres);
1163		sc->sc_vmemh = rman_get_bushandle(sc->sc_vmemres);
1164		adp->va_mem_base = rman_get_start(sc->sc_vmemres);
1165		adp->va_mem_size = rman_get_size(sc->sc_vmemres);
1166	}
1167
1168	device_printf(dev,
1169	    "%d MB aperture at 0x%08x, %d KB registers at 0x%08x\n",
1170	    (u_int)(adp->va_buffer_size / (1024 * 1024)),
1171	    (u_int)adp->va_buffer, MACH64_REG_SIZE / 1024,
1172	    (u_int)sc->sc_regh);
1173
1174	if (!(sc->sc_flags & MACHFB_CONSOLE)) {
1175		if ((sw = vid_get_switch(MACHFB_DRIVER_NAME)) == NULL) {
1176			device_printf(dev, "cannot get video switch\n");
1177			error = ENODEV;
1178			goto fail_vmemres;
1179		}
1180		/*
1181		 * During device configuration we don't necessarily probe
1182		 * the adapter which is the console first so we can't use
1183		 * the device unit number for the video adapter unit. The
1184		 * worst case would be that we use the video adapter unit
1185		 * 0 twice. As it doesn't really matter which unit number
1186		 * the corresponding video adapter has just use the next
1187		 * unused one.
1188		 */
1189		for (i = 0; i < devclass_get_maxunit(machfb_devclass); i++)
1190			if (vid_find_adapter(MACHFB_DRIVER_NAME, i) < 0)
1191				break;
1192		if ((error = sw->init(i, adp, 0)) != 0) {
1193			device_printf(dev, "cannot initialize adapter\n");
1194		    	goto fail_vmemres;
1195		}
1196	}
1197
1198	device_printf(dev,
1199	    "%ld KB %s %d.%d MHz, maximum RAMDAC clock %d MHz, %sDSP\n",
1200	    (u_long)sc->sc_memsize, machfb_memtype_names[sc->sc_memtype],
1201	    sc->sc_mem_freq / 1000, sc->sc_mem_freq % 1000,
1202	    sc->sc_ramdac_freq / 1000,
1203	    (sc->sc_flags & MACHFB_DSP) ? "" : "no ");
1204	device_printf(dev, "resolution %dx%d at %d bpp\n",
1205	    sc->sc_width, sc->sc_height, sc->sc_depth);
1206
1207	/*
1208	 * Test whether the aperture is byte swapped or not, set
1209	 * va_window and va_window_size as appropriate.
1210	 */
1211	p32 = (uint32_t *)adp->va_buffer;
1212	saved_value = *p32;
1213	p = (uint8_t *)adp->va_buffer;
1214	*p32 = 0x12345678;
1215	if (!(p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78)) {
1216		adp->va_window = adp->va_buffer + 0x800000;
1217		adp->va_window_size = adp->va_buffer_size - 0x800000;
1218	} else {
1219		adp->va_window = adp->va_buffer;
1220		adp->va_window_size = adp->va_buffer_size;
1221	}
1222	*p32 = saved_value;
1223	adp->va_window_gran = adp->va_window_size;
1224
1225	/*
1226	 * Allocate one page for the mouse pointer image at the end of
1227	 * the little endian aperture, right before the memory mapped
1228	 * registers that might also reside there. Must be done after
1229	 * sc_memsize was set and possibly adjusted to account for the
1230	 * memory mapped registers.
1231	 */
1232	sc->sc_curoff = (sc->sc_memsize * 1024) - PAGE_SIZE;
1233	sc->sc_memsize -= PAGE_SIZE / 1024;
1234	machfb_cursor_enable(sc, 0);
1235	/* Initialize with an all transparent image. */
1236	memset((void *)(adp->va_buffer + sc->sc_curoff), 0xaa, PAGE_SIZE);
1237
1238	/*
1239	 * For cosmetics register a handler that turns off the mouse
1240	 * pointer on halt. Register a second handler that turns off
1241	 * the CRTC when resetting, otherwise the OFW boot command
1242	 * issued by cpu_reset() just doesn't work.
1243	 */
1244	EVENTHANDLER_REGISTER(shutdown_final, machfb_shutdown_final, sc,
1245	    SHUTDOWN_PRI_DEFAULT);
1246	EVENTHANDLER_REGISTER(shutdown_reset, machfb_shutdown_reset, sc,
1247	    SHUTDOWN_PRI_DEFAULT);
1248
1249	return (0);
1250
1251 fail_vmemres:
1252 	if (sc->sc_vmemres != NULL)
1253	 	bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_vmemrid,
1254		    sc->sc_vmemres);
1255 fail_viores:
1256 	if (sc->sc_viores != NULL)
1257	 	bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_viorid,
1258		    sc->sc_viores);
1259 fail_memres:
1260	bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_memrid, sc->sc_memres);
1261
1262	return (error);
1263}
1264
1265static int
1266machfb_pci_detach(device_t dev)
1267{
1268
1269	return (EINVAL);
1270}
1271
1272/*
1273 * internal functions
1274 */
1275static void
1276machfb_cursor_enable(struct machfb_softc *sc, int onoff)
1277{
1278
1279	if (onoff)
1280		regw(sc, GEN_TEST_CNTL,
1281		    regr(sc, GEN_TEST_CNTL) | HWCURSOR_ENABLE);
1282	else
1283		regw(sc, GEN_TEST_CNTL,
1284		    regr(sc, GEN_TEST_CNTL) &~ HWCURSOR_ENABLE);
1285}
1286
1287static int
1288machfb_cursor_install(struct machfb_softc *sc)
1289{
1290	uint16_t *p;
1291	uint8_t fg;
1292	int i, j;
1293
1294	if (sc->sc_curoff == 0)
1295		return (ENODEV);
1296
1297	machfb_cursor_enable(sc, 0);
1298	regw(sc, CUR_OFFSET, sc->sc_curoff >> 3);
1299	fg = SC_NORM_ATTR & 0xf;
1300	regw(sc, CUR_CLR0, sc->sc_cmap_red[fg] << 24 |
1301	    sc->sc_cmap_green[fg] << 16 | sc->sc_cmap_blue[fg] << 8 | fg);
1302	p = (uint16_t *)(sc->sc_va.va_buffer + sc->sc_curoff);
1303	for (i = 0; i < 64; i++)
1304		for (j = 0; j < 8; j++)
1305			*(p++) = machfb_mouse_pointer_lut[
1306			    machfb_mouse_pointer_bits[i][j] >> 4] |
1307			    machfb_mouse_pointer_lut[
1308			    machfb_mouse_pointer_bits[i][j] & 0x0f] << 8;
1309
1310	return (0);
1311}
1312
1313static int
1314machfb_get_memsize(struct machfb_softc *sc)
1315{
1316	int tmp, memsize;
1317	int mem_tab[] = {
1318		512, 1024, 2048, 4096, 6144, 8192, 12288, 16384
1319	};
1320
1321	tmp = regr(sc, MEM_CNTL);
1322#ifdef MACHFB_DEBUG
1323	printf("memcntl=0x%08x\n", tmp);
1324#endif
1325	if (sc->sc_flags & MACHFB_DSP) {
1326		tmp &= 0x0000000f;
1327		if (tmp < 8)
1328			memsize = (tmp + 1) * 512;
1329		else if (tmp < 12)
1330			memsize = (tmp - 3) * 1024;
1331		else
1332			memsize = (tmp - 7) * 2048;
1333	} else
1334		memsize = mem_tab[tmp & 0x07];
1335
1336	return (memsize);
1337}
1338
1339static void
1340machfb_reset_engine(struct machfb_softc *sc)
1341{
1342
1343	/* Reset engine.*/
1344	regw(sc, GEN_TEST_CNTL, regr(sc, GEN_TEST_CNTL) & ~GUI_ENGINE_ENABLE);
1345
1346	/* Enable engine. */
1347	regw(sc, GEN_TEST_CNTL, regr(sc, GEN_TEST_CNTL) | GUI_ENGINE_ENABLE);
1348
1349	/*
1350	 * Ensure engine is not locked up by clearing any FIFO or
1351	 * host errors.
1352	 */
1353	regw(sc, BUS_CNTL, regr(sc, BUS_CNTL) | BUS_HOST_ERR_ACK |
1354	    BUS_FIFO_ERR_ACK);
1355}
1356
1357static void
1358machfb_init_engine(struct machfb_softc *sc)
1359{
1360	uint32_t pitch_value;
1361
1362	pitch_value = sc->sc_width;
1363
1364	if (sc->sc_depth == 24)
1365		pitch_value *= 3;
1366
1367	machfb_reset_engine(sc);
1368
1369	wait_for_fifo(sc, 14);
1370
1371	regw(sc, CONTEXT_MASK, 0xffffffff);
1372
1373	regw(sc, DST_OFF_PITCH, (pitch_value / 8) << 22);
1374
1375	regw(sc, DST_Y_X, 0);
1376	regw(sc, DST_HEIGHT, 0);
1377	regw(sc, DST_BRES_ERR, 0);
1378	regw(sc, DST_BRES_INC, 0);
1379	regw(sc, DST_BRES_DEC, 0);
1380
1381	regw(sc, DST_CNTL, DST_LAST_PEL | DST_X_LEFT_TO_RIGHT |
1382	    DST_Y_TOP_TO_BOTTOM);
1383
1384	regw(sc, SRC_OFF_PITCH, (pitch_value / 8) << 22);
1385
1386	regw(sc, SRC_Y_X, 0);
1387	regw(sc, SRC_HEIGHT1_WIDTH1, 1);
1388	regw(sc, SRC_Y_X_START, 0);
1389	regw(sc, SRC_HEIGHT2_WIDTH2, 1);
1390
1391	regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
1392
1393	wait_for_fifo(sc, 13);
1394	regw(sc, HOST_CNTL, 0);
1395
1396	regw(sc, PAT_REG0, 0);
1397	regw(sc, PAT_REG1, 0);
1398	regw(sc, PAT_CNTL, 0);
1399
1400	regw(sc, SC_LEFT, 0);
1401	regw(sc, SC_TOP, 0);
1402	regw(sc, SC_BOTTOM, sc->sc_height - 1);
1403	regw(sc, SC_RIGHT, pitch_value - 1);
1404
1405	regw(sc, DP_BKGD_CLR, 0);
1406	regw(sc, DP_FRGD_CLR, 0xffffffff);
1407	regw(sc, DP_WRITE_MASK, 0xffffffff);
1408	regw(sc, DP_MIX, (MIX_SRC << 16) | MIX_DST);
1409
1410	regw(sc, DP_SRC, FRGD_SRC_FRGD_CLR);
1411
1412	wait_for_fifo(sc, 3);
1413	regw(sc, CLR_CMP_CLR, 0);
1414	regw(sc, CLR_CMP_MASK, 0xffffffff);
1415	regw(sc, CLR_CMP_CNTL, 0);
1416
1417	wait_for_fifo(sc, 2);
1418	switch (sc->sc_depth) {
1419	case 8:
1420		regw(sc, DP_PIX_WIDTH, HOST_8BPP | SRC_8BPP | DST_8BPP);
1421		regw(sc, DP_CHAIN_MASK, DP_CHAIN_8BPP);
1422		regw(sc, DAC_CNTL, regr(sc, DAC_CNTL) | DAC_8BIT_EN);
1423		break;
1424#if 0
1425	case 32:
1426		regw(sc, DP_PIX_WIDTH, HOST_32BPP | SRC_32BPP | DST_32BPP);
1427		regw(sc, DP_CHAIN_MASK, DP_CHAIN_32BPP);
1428		regw(sc, DAC_CNTL, regr(sc, DAC_CNTL) | DAC_8BIT_EN);
1429		break;
1430#endif
1431	}
1432
1433	wait_for_fifo(sc, 5);
1434	regw(sc, CRTC_INT_CNTL, regr(sc, CRTC_INT_CNTL) & ~0x20);
1435	regw(sc, GUI_TRAJ_CNTL, DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM);
1436
1437	wait_for_idle(sc);
1438}
1439
1440#if 0
1441static void
1442machfb_adjust_frame(struct machfb_softc *sc, int x, int y)
1443{
1444	int offset;
1445
1446	offset = ((x + y * sc->sc_width) * (sc->sc_depth >> 3)) >> 3;
1447
1448	regw(sc, CRTC_OFF_PITCH, (regr(sc, CRTC_OFF_PITCH) & 0xfff00000) |
1449	     offset);
1450}
1451#endif
1452
1453static void
1454machfb_putpalreg(struct machfb_softc *sc, uint8_t index, uint8_t r, uint8_t g,
1455    uint8_t b)
1456{
1457
1458	sc->sc_cmap_red[index] = r;
1459	sc->sc_cmap_green[index] = g;
1460	sc->sc_cmap_blue[index] = b;
1461	/*
1462	 * Writing the DAC index takes a while, in theory we can poll some
1463	 * register to see when it's ready - but we better avoid writing it
1464	 * unnecessarily.
1465	 */
1466	if (index != sc->sc_dacw) {
1467		regwb(sc, DAC_MASK, 0xff);
1468		regwb(sc, DAC_WINDEX, index);
1469	}
1470	sc->sc_dacw = index + 1;
1471	regwb(sc, DAC_DATA, r);
1472	regwb(sc, DAC_DATA, g);
1473	regwb(sc, DAC_DATA, b);
1474}
1475
1476static void
1477machfb_shutdown_final(void *v)
1478{
1479	struct machfb_softc *sc = v;
1480
1481	machfb_cursor_enable(sc, 0);
1482}
1483
1484static void
1485machfb_shutdown_reset(void *v)
1486{
1487	struct machfb_softc *sc = v;
1488
1489	machfb_blank_display(&sc->sc_va, V_DISPLAY_STAND_BY);
1490}
1491