Module Name:    src
Committed By:   macallan
Date:           Tue Jan 24 04:33:11 UTC 2012

Modified Files:
        src/sys/arch/macppc/conf: files.macppc
Added Files:
        src/sys/arch/macppc/dev: valkyriefb.c valkyriefbreg.h videopll.c
            videopllvar.h

Log Message:
drivers to support the onboard video circuitry found in for example the
Performa 6360


To generate a diff of this commit:
cvs rdiff -u -r1.98 -r1.99 src/sys/arch/macppc/conf/files.macppc
cvs rdiff -u -r0 -r1.1 src/sys/arch/macppc/dev/valkyriefb.c \
    src/sys/arch/macppc/dev/valkyriefbreg.h \
    src/sys/arch/macppc/dev/videopll.c src/sys/arch/macppc/dev/videopllvar.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/macppc/conf/files.macppc
diff -u src/sys/arch/macppc/conf/files.macppc:1.98 src/sys/arch/macppc/conf/files.macppc:1.99
--- src/sys/arch/macppc/conf/files.macppc:1.98	Wed Jul 13 22:54:33 2011
+++ src/sys/arch/macppc/conf/files.macppc	Tue Jan 24 04:33:11 2012
@@ -1,4 +1,4 @@
-#	$NetBSD: files.macppc,v 1.98 2011/07/13 22:54:33 macallan Exp $
+#	$NetBSD: files.macppc,v 1.99 2012/01/24 04:33:11 macallan Exp $
 #
 # macppc-specific configuration info
 
@@ -295,4 +295,12 @@ device deq
 attach deq at ki2c
 file arch/macppc/dev/deq.c				deq
 
-
+device videopll
+attach videopll at iic
+file arch/macppc/dev/videopll.c				videopll needs-flag
+defflag opt_videopll.h VIDEOPLL_DEBUG
+
+device valkyriefb: wsemuldisplaydev, rasops8, vcons, videomode
+attach valkyriefb at mainbus
+file arch/macppc/dev/valkyriefb.c			valkyriefb
+defflag opt_valkyriefb.h VALKYRIEFB_DEBUG

Added files:

Index: src/sys/arch/macppc/dev/valkyriefb.c
diff -u /dev/null src/sys/arch/macppc/dev/valkyriefb.c:1.1
--- /dev/null	Tue Jan 24 04:33:11 2012
+++ src/sys/arch/macppc/dev/valkyriefb.c	Tue Jan 24 04:33:11 2012
@@ -0,0 +1,439 @@
+/*	$NetBSD: valkyriefb.c,v 1.1 2012/01/24 04:33:11 macallan Exp $	*/
+
+/*
+ * Copyright (c) 2012 Michael Lorenz
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/* 
+ * A console driver for Apple's Valkyrie onboard video controller, found in
+ * for example the Performa 6360
+ * This should be easy enough to adapt to mac68k but I don't have the hardware
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: valkyriefb.c,v 1.1 2012/01/24 04:33:11 macallan Exp $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/device.h>
+
+#include <dev/ofw/openfirm.h>
+
+#include <machine/autoconf.h>
+
+#include <dev/wscons/wsdisplayvar.h>
+#include <dev/wscons/wsconsio.h>
+#include <dev/wsfont/wsfont.h>
+#include <dev/rasops/rasops.h>
+#include <dev/wscons/wsdisplay_vconsvar.h>
+
+#include <dev/videomode/videomode.h>
+
+#include <arch/macppc/dev/valkyriefbreg.h>
+#include <arch/macppc/dev/videopllvar.h>
+
+#include "opt_wsemul.h"
+#include "opt_valkyriefb.h"
+
+#include "videopll.h"
+#if NVIDEOPLL < 1
+#error "valkyriefb requires videopll at iic"
+#endif
+
+struct valkyriefb_softc {
+	device_t sc_dev;
+	int sc_node;
+	uint8_t *sc_base;
+
+	int sc_depth;
+	int sc_width, sc_height, sc_linebytes;
+	const struct videomode *sc_videomode;
+	uint8_t sc_modereg;
+
+	int sc_mode;
+	uint32_t sc_bg;
+		
+	u_char sc_cmap_red[256];
+	u_char sc_cmap_green[256];
+	u_char sc_cmap_blue[256];	
+
+	struct vcons_data vd;
+};
+
+struct valkyriefb_mode {
+	int width;
+	int height;
+	uint8_t mode;
+};
+
+/*
+ * Translate screen resolutions to values for Valkyrie's mode register.
+ * These numbers seem to roughly correspond to MacOS video mode numbers
+ * there are many numbers that result in the same resolution which in MacOS
+ * only differ by the display refresh rate, which isn't set by Valkyrie at
+ * all, instead there's a PLL chip hooked to cuda's i2c bus. It doesn't seem
+ * to matter which number we use as long as the resolution is right and we
+ * program the PLL for the right pixel clock
+ */
+static struct valkyriefb_mode modetab[] = {
+	{  640, 480, 6 },
+	{  800, 600, 12 },
+	{  832, 624, 9 },
+	{ 1024, 768, 14},
+};
+	
+static struct vcons_screen valkyriefb_console_screen;
+
+static int	valkyriefb_match(device_t, cfdata_t, void *);
+static void	valkyriefb_attach(device_t, device_t, void *);
+static int	valkyriefb_init(device_t);
+static int 	valkyriefb_set_mode(struct valkyriefb_softc *,
+		    const struct videomode *, int);
+
+CFATTACH_DECL_NEW(valkyriefb, sizeof(struct valkyriefb_softc),
+    valkyriefb_match, valkyriefb_attach, NULL, NULL);
+
+struct wsscreen_descr valkyriefb_defaultscreen = {
+	"default",
+	0, 0,
+	NULL,
+	8, 16,
+	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
+	NULL,
+};
+
+const struct wsscreen_descr *_valkyriefb_scrlist[] = {
+	&valkyriefb_defaultscreen,
+	/* XXX other formats, graphics screen? */
+};
+
+struct wsscreen_list valkyriefb_screenlist = {
+	sizeof(_valkyriefb_scrlist) / sizeof(struct wsscreen_descr *), _valkyriefb_scrlist
+};
+
+static int	valkyriefb_ioctl(void *, void *, u_long, void *, int,
+		    struct lwp *);
+static paddr_t	valkyriefb_mmap(void *, void *, off_t, int);
+
+static void	valkyriefb_init_screen(void *, struct vcons_screen *, int,
+			    long *);
+
+
+struct wsdisplay_accessops valkyriefb_accessops = {
+	valkyriefb_ioctl,
+	valkyriefb_mmap,
+	NULL,
+	NULL,
+	NULL,
+	NULL,	/* load_font */
+	NULL,	/* polls */
+	NULL,	/* scroll */
+};
+
+static inline void
+valkyriefb_write_reg(struct valkyriefb_softc *sc, int reg, uint8_t val)
+{
+	*(sc->sc_base + VAL_REGS_OFFSET + reg) = val;
+	__asm("eieio; sync;");
+}
+
+static inline uint8_t
+valkyriefb_read_reg(struct valkyriefb_softc *sc, int reg)
+{
+	return *(sc->sc_base + VAL_REGS_OFFSET + reg);
+}
+
+static void
+valkyriefb_write_cmap(struct valkyriefb_softc *sc,
+    int reg, uint8_t r, uint8_t g, uint8_t b)
+{
+	*(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_ADDR) = reg;
+	__asm("eieio; sync;");
+	*(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_LUT) = r;
+	__asm("eieio; sync;");
+	*(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_LUT) = g;
+	__asm("eieio; sync;");
+	*(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_LUT) = b;
+	__asm("eieio; sync;");
+}
+
+static int
+valkyriefb_match(device_t parent, cfdata_t cf, void *aux)
+{
+	struct confargs *ca = aux;
+
+	if (strcmp(ca->ca_name, "valkyrie") != 0)
+		return 0;
+	return 1;
+}
+
+static void
+valkyriefb_attach(device_t parent, device_t self, void *aux)
+{
+	struct valkyriefb_softc *sc = device_private(self);
+	struct confargs *ca = aux;
+	int i;
+	volatile uint32_t *fb;
+
+	sc->sc_dev = self;
+	sc->sc_node = ca->ca_node;
+
+	aprint_normal(" address 0x%08x\n", ca->ca_reg[0]);
+	aprint_verbose_dev(sc->sc_dev, "waiting for videopll...\n");
+	sc->sc_base = (uint8_t *)ca->ca_reg[0];
+#ifdef VALKYRIEFB_DEBUG
+	for (i = 0; i < 0x40; i += 8) {
+		aprint_error_dev(sc->sc_dev, "%02x: %02x\n", i, valkyriefb_read_reg(sc, i));
+	}
+#endif
+	config_finalize_register(sc->sc_dev, valkyriefb_init);
+	fb = (volatile uint32_t *)(sc->sc_base + 0x1000);
+	for (i = 0; i < 256; i++)
+		fb[i] = 0xffffffff;
+	for (i = 256; i < 0x10000; i++)
+		fb[i] = 0x80808080;
+}
+
+static int
+valkyriefb_init(device_t self)
+{
+	struct valkyriefb_softc *sc = device_private(self);
+	const struct videomode *mode;
+	struct rasops_info *ri;
+	prop_dictionary_t dict;
+	struct wsemuldisplaydev_attach_args aa;
+	bool console = FALSE;
+	long defattr;
+
+	mode = pick_mode_by_ref(800, 600, 60);
+	if (mode == NULL)
+		return 0;
+
+	valkyriefb_set_mode(sc, mode, 8);
+
+	vcons_init(&sc->vd, sc, &valkyriefb_defaultscreen, &valkyriefb_accessops);
+	sc->vd.init_screen = valkyriefb_init_screen;
+
+	dict = device_properties(sc->sc_dev);
+	prop_dictionary_get_bool(dict, "is_console", &console);
+
+	ri = &valkyriefb_console_screen.scr_ri;
+	vcons_init_screen(&sc->vd, &valkyriefb_console_screen, 1, &defattr);
+	valkyriefb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
+
+	valkyriefb_defaultscreen.textops = &ri->ri_ops;
+	valkyriefb_defaultscreen.capabilities = ri->ri_caps;
+	valkyriefb_defaultscreen.nrows = ri->ri_rows;
+	valkyriefb_defaultscreen.ncols = ri->ri_cols;
+	if (console) {
+		wsdisplay_cnattach(&valkyriefb_defaultscreen, ri, 0, 0, defattr);
+	}
+
+	memset(sc->sc_base + 0x1000, ri->ri_devcmap[(defattr >> 16) & 0xf],
+	    sc->sc_width * sc->sc_linebytes);
+
+	if (console)
+		vcons_replay_msgbuf(&valkyriefb_console_screen);
+
+	aa.console = console;
+	aa.scrdata = &valkyriefb_screenlist;
+	aa.accessops = &valkyriefb_accessops;
+	aa.accesscookie = &sc->vd;
+
+	config_found(self, &aa, wsemuldisplaydevprint);
+
+	return 0;
+}
+
+static int
+valkyriefb_set_mode(struct valkyriefb_softc *sc,
+    const struct videomode *mode, int depth)
+{
+	int i;
+	uint8_t modereg, tmp;
+
+	/* first find the parameter for the mode register */
+	i = 0;
+	while ((i < __arraycount(modetab)) && 
+	       (modetab[i].width != mode->hdisplay) &&
+	       (modetab[i].height != mode->vdisplay))
+		i++;
+	if ((modetab[i].width == mode->hdisplay) &&
+	       (modetab[i].height == mode->vdisplay)) {
+		modereg = modetab[i].mode;
+	} else {
+		aprint_error_dev(sc->sc_dev, "Can't find a mode register value for %s\n", mode->name);
+		return EINVAL;
+	}
+
+	/* check if we have enough video memory */
+	if ((mode->hdisplay * mode->vdisplay * (depth >> 3)) > 0x100000) {
+		aprint_error_dev(sc->sc_dev, "Not enough video RAM for %s\n", mode->name);
+		return EINVAL;
+	}
+
+	/* reject depths other than 8 or 16 */
+	if ((depth != 8) && (depth != 16)) {
+		aprint_error_dev(sc->sc_dev, "Depth [%d] is unsupported for %s\n", depth, mode->name);
+		return EINVAL;
+	}
+
+	/* now start programming the chip */
+	valkyriefb_write_reg(sc, VAL_STATUS, 0);
+	delay(100);
+	valkyriefb_write_reg(sc, VAL_MODE, modereg | VAL_MODE_STOP);
+
+	if (depth == 8) {
+		sc->sc_depth = 8;
+		valkyriefb_write_reg(sc, VAL_DEPTH, VAL_DEPTH_8);
+		for (i = 0; i < 256; i++) {
+			tmp = i & 0xe0;
+			/*
+			 * replicate bits so 0xe0 maps to a red value of 0xff
+			 * in order to make white look actually white
+			 */
+			tmp |= (tmp >> 3) | (tmp >> 6);
+			sc->sc_cmap_red[i] = tmp;
+
+			tmp = (i & 0x1c) << 3;
+			tmp |= (tmp >> 3) | (tmp >> 6);
+			sc->sc_cmap_green[i] = tmp;
+
+			tmp = (i & 0x03) << 6;
+			tmp |= tmp >> 2;
+			tmp |= tmp >> 4;
+			sc->sc_cmap_blue[i] = tmp;
+
+			valkyriefb_write_cmap(sc, i, sc->sc_cmap_red[i],
+			    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
+		}
+	} else if (depth == 16) {
+		sc->sc_depth = 16;
+		valkyriefb_write_reg(sc, VAL_DEPTH, VAL_DEPTH_16);
+		/* does the palette have any effect in 16bit? */
+	}
+
+	videopll_set_freq(mode->dot_clock);
+	delay(100);
+	valkyriefb_write_reg(sc, VAL_MODE, modereg);
+
+	sc->sc_modereg = modereg;
+	sc->sc_videomode = mode;
+	sc->sc_width = mode->hdisplay;
+	sc->sc_height = mode->vdisplay;
+	sc->sc_linebytes = mode->hdisplay * (sc->sc_depth >> 3);
+	aprint_normal_dev(sc->sc_dev, "switched to %d x %d in %d bit colour\n",
+	    sc->sc_width, sc->sc_height, sc->sc_depth);
+	return 0;
+}
+
+static int
+valkyriefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
+	struct lwp *l)
+{
+	struct vcons_data *vd = v;
+	struct valkyriefb_softc *sc = vd->cookie;
+	struct wsdisplay_fbinfo *wdf;
+	struct vcons_screen *ms = vd->active;
+
+	switch (cmd) {
+	case WSDISPLAYIO_GTYPE:
+		*(u_int *)data = WSDISPLAY_TYPE_VALKYRIE;
+		return 0;
+
+	case WSDISPLAYIO_GINFO:
+		wdf = (void *)data;
+		wdf->height = ms->scr_ri.ri_height;
+		wdf->width = ms->scr_ri.ri_width;
+		wdf->depth = ms->scr_ri.ri_depth;
+		wdf->cmsize = 256;
+		return 0;
+#if 0	
+	case WSDISPLAYIO_GETCMAP:
+		return valkyriefb_getcmap(sc,
+		    (struct wsdisplay_cmap *)data);
+
+	case WSDISPLAYIO_PUTCMAP:
+		return valkyriefb_putcmap(sc,
+		    (struct wsdisplay_cmap *)data);
+#endif
+
+	case WSDISPLAYIO_SMODE: {
+		int new_mode = *(int*)data;
+		if (new_mode != sc->sc_mode) {
+			sc->sc_mode = new_mode;
+			if (new_mode == WSDISPLAYIO_MODE_EMUL) {
+				vcons_redraw_screen(ms);
+			}
+		}
+		}
+		return 0;
+	}
+	return EPASSTHROUGH;
+}
+
+static paddr_t
+valkyriefb_mmap(void *v, void *vs, off_t offset, int prot)
+{
+	struct vcons_data *vd = v;
+	struct valkyriefb_softc *sc = vd->cookie;
+	paddr_t pa;
+		
+	/* 'regular' framebuffer mmap()ing */
+	if (offset < 0x100000) {
+		pa = (paddr_t)(sc->sc_base + 0x1000 + offset);	
+		return pa;
+	}
+	return -1;
+}
+
+static void
+valkyriefb_init_screen(void *cookie, struct vcons_screen *scr,
+    int existing, long *defattr)
+{
+	struct valkyriefb_softc *sc = cookie;
+	struct rasops_info *ri = &scr->scr_ri;
+	
+	ri->ri_depth = sc->sc_depth;
+	ri->ri_width = sc->sc_width;
+	ri->ri_height = sc->sc_height;
+	ri->ri_stride = sc->sc_linebytes;
+	ri->ri_flg = RI_CENTER | RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
+	ri->ri_bits = sc->sc_base + 0x1000;
+	/*
+	 * We probably shouldn't set this flag together with RI_ENABLE_ALPHA
+	 * since the CPU is likely slow enough to make scrolling using
+	 * framebuffer reads faster than redrawing
+	 */
+	scr->scr_flags |= VCONS_DONT_READ;
+	
+	rasops_init(ri, 0, 0);
+	ri->ri_caps = WSSCREEN_WSCOLORS;
+
+	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
+		    sc->sc_width / ri->ri_font->fontwidth);
+
+	ri->ri_hw = scr;
+}
Index: src/sys/arch/macppc/dev/valkyriefbreg.h
diff -u /dev/null src/sys/arch/macppc/dev/valkyriefbreg.h:1.1
--- /dev/null	Tue Jan 24 04:33:11 2012
+++ src/sys/arch/macppc/dev/valkyriefbreg.h	Tue Jan 24 04:33:11 2012
@@ -0,0 +1,52 @@
+/*-
+ * Copyright (c) 2012 Michael Lorenz
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: valkyriefbreg.h,v 1.1 2012/01/24 04:33:11 macallan Exp $");
+
+#ifndef VALKYRIEFBREG_H
+#define VALKYRIEFBREG_H
+
+#define VAL_FB_OFFSET		0x00000000
+#define VAL_CMAP_OFFSET		0x00304000
+#define VAL_CMAP_ADDR	0x00000000
+#define VAL_CMAP_LUT	0x00000008
+#define VAL_REGS_OFFSET		0x0030a000
+#define VAL_MODE	0x00000000
+	#define VAL_MODE_STOP	0x80
+	#define VAL_MODE_NOSYNC	0x40
+	
+#define VAL_DEPTH	0x00000008
+	#define VAL_DEPTH_8	3
+	#define VAL_DEPTH_16	4
+#define VAL_STATUS	0x00000010
+#define VAL_REG3	0x00000018
+#define VAL_INTR	0x00000020
+#define VAL_REG5	0x00000028
+#define VAL_INTR_EN	0x00000030
+#define VAL_MPSENSE	0x00000038
+
+#endif /* VALKYRIEFBREG_H */
Index: src/sys/arch/macppc/dev/videopll.c
diff -u /dev/null src/sys/arch/macppc/dev/videopll.c:1.1
--- /dev/null	Tue Jan 24 04:33:11 2012
+++ src/sys/arch/macppc/dev/videopll.c	Tue Jan 24 04:33:11 2012
@@ -0,0 +1,157 @@
+/*	$NetBSD: videopll.c,v 1.1 2012/01/24 04:33:11 macallan Exp $	*/
+
+/*
+ * Copyright (c) 2012 Michael Lorenz
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * A driver for the iic-controlled PLLs used in early Apple onboard video 
+ * hardware. For now we support /valkyrie only but others use vary similar
+ * schemes to program their pixel clock so adding support for those should
+ * be simple enough.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: videopll.c,v 1.1 2012/01/24 04:33:11 macallan Exp $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/device.h>
+#include <sys/malloc.h>
+
+#include <dev/ofw/openfirm.h>
+#include <dev/i2c/i2cvar.h>
+#include <arch/macppc/dev/videopllvar.h>
+
+#include "opt_videopll.h"
+#ifdef VIDEOPLL_DEBUG
+#define DPRINTF printf
+#else
+#define DPRINTF while (0) printf
+#endif
+
+struct videopll_softc {
+	device_t sc_dev;
+	i2c_tag_t sc_tag;
+	int sc_addr;
+};
+
+static int  videopll_match(device_t, cfdata_t, void *);
+static void videopll_attach(device_t, device_t, void *);
+
+CFATTACH_DECL_NEW(videopll, sizeof(struct videopll_softc),
+	videopll_match, videopll_attach, NULL, NULL);
+
+static void *glob = NULL;
+
+static int
+videopll_match(device_t parent, cfdata_t cfdata, void *aux)
+{
+	struct i2c_attach_args *ia = aux;
+	int node;
+
+	/* we support only /valkyrie so far */
+	node = OF_finddevice("/valkyrie");
+	if (node == -1)
+		return 0;
+	/* make sure we only attach to cuda's iic bus */
+	if (!device_is_a(device_parent(parent), "cuda"))
+		return 0;
+	if (ia->ia_addr == 0x50)
+		return 100;
+	return 0;
+}
+
+static void
+videopll_attach(device_t parent, device_t self, void *aux)
+{
+	struct i2c_attach_args *ia = aux;
+	struct videopll_softc *sc = device_private(self);
+
+	sc->sc_dev = self;
+	sc->sc_tag = ia->ia_tag;
+	sc->sc_addr = ia->ia_addr;
+	aprint_normal(": Apple onboard video PLL\n");
+	glob = sc;
+}
+
+/*
+ * pixel clock:
+ * 3.9064MHz * 2^p2 * p1 / p0
+ */
+int
+videopll_set_freq(int freq)
+{
+	struct videopll_softc *sc = glob;
+	int p0, p1, p2;
+	int freq_out, diff, diff_b = 100000000;
+	int b0 = 0, b1 = 0, b2 = 0, freq_b = 0;
+	uint8_t cmdbuf[4];
+
+	if (glob == NULL)
+		return EIO;
+	/*
+	 * XXX
+	 * The parameter ranges were taken from Linux' valkyriefb.c mode list.
+	 * We don't really know what exact parameters the PLL supports but
+	 * this should be enough for the modes we can actually support
+	 */
+	for (p2 = 2; p2 < 4; p2++) {
+		for (p1 = 27; p1 < 43; p1++) {
+			for (p0 = 11; p0 < 26; p0++) {
+				freq_out = (3906400 * (1 << p2) * p1) / (p0 * 1000);
+				diff = abs(freq - freq_out);
+				if (diff < diff_b) {
+					diff_b = diff;
+					b0 = p0;
+					b1 = p1;
+					b2 = p2;
+					freq_b = freq_out;
+				}
+			}
+		}
+	}
+	if (freq_b == 0)
+		return EINVAL;
+	DPRINTF("param: %d %d %d -> %d\n", b0, b1, b2, freq_b);
+	iic_acquire_bus(sc->sc_tag, 0);
+	cmdbuf[0] = 1;
+	cmdbuf[1] = b0;
+	iic_exec(sc->sc_tag, I2C_OP_WRITE,
+	    sc->sc_addr, cmdbuf, 2, NULL, 0, 0);
+	cmdbuf[0] = 2;
+	cmdbuf[1] = b1;
+	iic_exec(sc->sc_tag, I2C_OP_WRITE,
+	    sc->sc_addr, cmdbuf, 2, NULL, 0, 0);
+	cmdbuf[0] = 3;
+	cmdbuf[1] = b2;
+	iic_exec(sc->sc_tag, I2C_OP_WRITE,
+	    sc->sc_addr, cmdbuf, 2, NULL, 0, 0);
+	iic_release_bus(sc->sc_tag, 0);
+	return 0;
+}
+	
+
+
Index: src/sys/arch/macppc/dev/videopllvar.h
diff -u /dev/null src/sys/arch/macppc/dev/videopllvar.h:1.1
--- /dev/null	Tue Jan 24 04:33:11 2012
+++ src/sys/arch/macppc/dev/videopllvar.h	Tue Jan 24 04:33:11 2012
@@ -0,0 +1,35 @@
+/*-
+ * Copyright (c) 2012 Michael Lorenz
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: videopllvar.h,v 1.1 2012/01/24 04:33:11 macallan Exp $");
+
+#ifndef VIDEOPLLVAR_H
+#define VIDEOPLLVAR_H
+
+int videopll_set_freq(int);	/* in kHz */
+
+#endif /* VIDEOPLLVAR_H */

Reply via email to