Home | History | Annotate | Line # | Download | only in ic
igsfb_subr.c revision 1.10
      1  1.10  macallan /*	$NetBSD: igsfb_subr.c,v 1.10 2009/11/11 17:01:17 macallan Exp $ */
      2   1.1       uwe 
      3   1.1       uwe /*
      4   1.1       uwe  * Copyright (c) 2002 Valeriy E. Ushakov
      5   1.1       uwe  * All rights reserved.
      6   1.1       uwe  *
      7   1.1       uwe  * Redistribution and use in source and binary forms, with or without
      8   1.1       uwe  * modification, are permitted provided that the following conditions
      9   1.1       uwe  * are met:
     10   1.1       uwe  * 1. Redistributions of source code must retain the above copyright
     11   1.1       uwe  *    notice, this list of conditions and the following disclaimer.
     12   1.1       uwe  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       uwe  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       uwe  *    documentation and/or other materials provided with the distribution.
     15   1.1       uwe  * 3. The name of the author may not be used to endorse or promote products
     16   1.1       uwe  *    derived from this software without specific prior written permission
     17   1.1       uwe  *
     18   1.1       uwe  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19   1.1       uwe  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20   1.1       uwe  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21   1.1       uwe  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22   1.1       uwe  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23   1.1       uwe  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24   1.1       uwe  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25   1.1       uwe  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26   1.1       uwe  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27   1.1       uwe  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28   1.1       uwe  */
     29   1.1       uwe 
     30   1.1       uwe /*
     31   1.1       uwe  * Integraphics Systems IGA 168x and CyberPro series.
     32   1.1       uwe  */
     33   1.1       uwe #include <sys/cdefs.h>
     34  1.10  macallan __KERNEL_RCSID(0, "$NetBSD: igsfb_subr.c,v 1.10 2009/11/11 17:01:17 macallan Exp $");
     35   1.1       uwe 
     36   1.1       uwe #include <sys/param.h>
     37   1.1       uwe #include <sys/systm.h>
     38   1.1       uwe #include <sys/kernel.h>
     39   1.1       uwe #include <sys/device.h>
     40   1.1       uwe 
     41   1.9        ad #include <sys/bus.h>
     42   1.1       uwe 
     43   1.2       uwe #include <dev/wscons/wsdisplayvar.h>
     44   1.1       uwe #include <dev/wscons/wsconsio.h>
     45   1.2       uwe #include <dev/rasops/rasops.h>
     46   1.6  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     47   1.1       uwe 
     48   1.1       uwe #include <dev/ic/igsfbreg.h>
     49   1.1       uwe #include <dev/ic/igsfbvar.h>
     50   1.1       uwe 
     51  1.10  macallan #ifdef IGSFB_DEBUG
     52  1.10  macallan #define DPRINTF printf
     53  1.10  macallan #else
     54  1.10  macallan #define DPRINTF while (0) printf
     55  1.10  macallan #endif
     56   1.1       uwe 
     57   1.2       uwe static void	igsfb_init_seq(struct igsfb_devconfig *);
     58   1.2       uwe static void	igsfb_init_crtc(struct igsfb_devconfig *);
     59   1.2       uwe static void	igsfb_init_grfx(struct igsfb_devconfig *);
     60   1.2       uwe static void	igsfb_init_attr(struct igsfb_devconfig *);
     61   1.2       uwe static void	igsfb_init_ext(struct igsfb_devconfig *);
     62   1.2       uwe static void	igsfb_init_dac(struct igsfb_devconfig *);
     63   1.1       uwe 
     64   1.2       uwe static void	igsfb_freq_latch(struct igsfb_devconfig *);
     65   1.2       uwe static void	igsfb_video_on(struct igsfb_devconfig *);
     66  1.10  macallan static void	igsfb_calc_pll(int, int *, int *, int *, int, int, int, int);
     67   1.1       uwe 
     68   1.1       uwe 
     69   1.1       uwe 
     70   1.1       uwe /*
     71   1.1       uwe  * Enable chip.
     72   1.1       uwe  */
     73   1.1       uwe int
     74   1.7       uwe igsfb_enable(bus_space_tag_t iot, bus_addr_t iobase, int ioflags)
     75   1.1       uwe {
     76   1.1       uwe 	bus_space_handle_t vdoh;
     77   1.1       uwe 	bus_space_handle_t vseh;
     78   1.1       uwe 	bus_space_handle_t regh;
     79   1.1       uwe 	int ret;
     80   1.1       uwe 
     81   1.2       uwe 	ret = bus_space_map(iot, iobase + IGS_VDO, 1, ioflags, &vdoh);
     82   1.1       uwe 	if (ret != 0) {
     83   1.1       uwe 		printf("unable to map VDO register\n");
     84   1.1       uwe 		goto out0;
     85   1.1       uwe 	}
     86   1.1       uwe 
     87   1.2       uwe 	ret = bus_space_map(iot, iobase + IGS_VSE, 1, ioflags, &vseh);
     88   1.1       uwe 	if (ret != 0) {
     89   1.1       uwe 		printf("unable to map VSE register\n");
     90   1.1       uwe 		goto out1;
     91   1.1       uwe 	}
     92   1.1       uwe 
     93   1.2       uwe 	ret = bus_space_map(iot, iobase + IGS_REG_BASE, IGS_REG_SIZE, ioflags,
     94   1.2       uwe 			    &regh);
     95   1.1       uwe 	if (ret != 0) {
     96   1.1       uwe 		printf("unable to map I/O registers\n");
     97   1.1       uwe 		goto out2;
     98   1.1       uwe 	}
     99   1.1       uwe 
    100   1.1       uwe 	/*
    101   1.1       uwe 	 * Start decoding i/o space accesses.
    102   1.1       uwe 	 */
    103   1.1       uwe 	bus_space_write_1(iot, vdoh, 0, IGS_VDO_ENABLE | IGS_VDO_SETUP);
    104   1.1       uwe 	bus_space_write_1(iot, vseh, 0, IGS_VSE_ENABLE);
    105   1.1       uwe 	bus_space_write_1(iot, vdoh, 0, IGS_VDO_ENABLE);
    106   1.1       uwe 
    107   1.1       uwe 	/*
    108   1.1       uwe 	 * Start decoding memory space accesses (XXX: move out of here?
    109   1.1       uwe 	 * we program this register in igsfb_init_ext).
    110   1.1       uwe 	 * While here, enable coprocessor and select IGS_COP_BASE_B.
    111   1.1       uwe 	 */
    112   1.1       uwe 	igs_ext_write(iot, regh, IGS_EXT_BIU_MISC_CTL,
    113   1.1       uwe 		      (IGS_EXT_BIU_LINEAREN
    114   1.1       uwe 		       | IGS_EXT_BIU_COPREN | IGS_EXT_BIU_COPASELB));
    115   1.1       uwe 
    116   1.1       uwe 	bus_space_unmap(iot, regh, IGS_REG_SIZE);
    117   1.1       uwe   out2:	bus_space_unmap(iot, vseh, 1);
    118   1.1       uwe   out1:	bus_space_unmap(iot, vdoh, 1);
    119   1.8       uwe   out0: return ret;
    120   1.1       uwe }
    121   1.1       uwe 
    122   1.1       uwe 
    123   1.1       uwe /*
    124   1.1       uwe  * Init sequencer.
    125   1.1       uwe  * This is common for all video modes.
    126   1.1       uwe  */
    127   1.1       uwe static void
    128   1.7       uwe igsfb_init_seq(struct igsfb_devconfig *dc)
    129   1.1       uwe {
    130   1.2       uwe 	bus_space_tag_t iot = dc->dc_iot;
    131   1.2       uwe 	bus_space_handle_t ioh = dc->dc_ioh;
    132   1.1       uwe 
    133   1.1       uwe 	/* start messing with sequencer */
    134   1.1       uwe 	igs_seq_write(iot, ioh, IGS_SEQ_RESET, 0);
    135   1.1       uwe 
    136   1.1       uwe 	igs_seq_write(iot, ioh, 1, 0x01); /* 8 dot clock */
    137   1.1       uwe 	igs_seq_write(iot, ioh, 2, 0x0f); /* enable all maps */
    138   1.1       uwe 	igs_seq_write(iot, ioh, 3, 0x00); /* character generator */
    139   1.1       uwe 	igs_seq_write(iot, ioh, 4, 0x0e); /* memory mode */
    140   1.1       uwe 
    141   1.1       uwe 	/* this selects color mode among other things */
    142   1.1       uwe 	bus_space_write_1(iot, ioh, IGS_MISC_OUTPUT_W, 0xef);
    143   1.1       uwe 
    144   1.1       uwe 	/* normal sequencer operation */
    145   1.1       uwe 	igs_seq_write(iot, ioh, IGS_SEQ_RESET,
    146   1.1       uwe 		      IGS_SEQ_RESET_SYNC | IGS_SEQ_RESET_ASYNC);
    147   1.1       uwe }
    148   1.1       uwe 
    149   1.7       uwe 
    150   1.1       uwe /*
    151   1.1       uwe  * Init CRTC to 640x480 8bpp at 60Hz
    152   1.1       uwe  */
    153   1.1       uwe static void
    154   1.7       uwe igsfb_init_crtc(struct igsfb_devconfig *dc)
    155   1.1       uwe {
    156   1.2       uwe 	bus_space_tag_t iot = dc->dc_iot;
    157   1.2       uwe 	bus_space_handle_t ioh = dc->dc_ioh;
    158   1.1       uwe 
    159   1.1       uwe 	igs_crtc_write(iot, ioh, 0x00, 0x5f);
    160   1.1       uwe 	igs_crtc_write(iot, ioh, 0x01, 0x4f);
    161   1.1       uwe 	igs_crtc_write(iot, ioh, 0x02, 0x50);
    162   1.1       uwe 	igs_crtc_write(iot, ioh, 0x03, 0x80);
    163   1.1       uwe 	igs_crtc_write(iot, ioh, 0x04, 0x52);
    164   1.1       uwe 	igs_crtc_write(iot, ioh, 0x05, 0x9d);
    165   1.1       uwe 	igs_crtc_write(iot, ioh, 0x06, 0x0b);
    166   1.1       uwe 	igs_crtc_write(iot, ioh, 0x07, 0x3e);
    167   1.1       uwe 
    168   1.1       uwe 	/* next block is almost constant, only bit 6 in reg 9 differs */
    169   1.1       uwe 	igs_crtc_write(iot, ioh, 0x08, 0x00);
    170   1.1       uwe 	igs_crtc_write(iot, ioh, 0x09, 0x40); /* <- either 0x40 or 0x60 */
    171   1.1       uwe 	igs_crtc_write(iot, ioh, 0x0a, 0x00);
    172   1.1       uwe 	igs_crtc_write(iot, ioh, 0x0b, 0x00);
    173   1.1       uwe 	igs_crtc_write(iot, ioh, 0x0c, 0x00);
    174   1.1       uwe 	igs_crtc_write(iot, ioh, 0x0d, 0x00);
    175   1.1       uwe 	igs_crtc_write(iot, ioh, 0x0e, 0x00);
    176   1.1       uwe 	igs_crtc_write(iot, ioh, 0x0f, 0x00);
    177   1.1       uwe 
    178   1.1       uwe 	igs_crtc_write(iot, ioh, 0x10, 0xe9);
    179   1.1       uwe 	igs_crtc_write(iot, ioh, 0x11, 0x8b);
    180   1.1       uwe 	igs_crtc_write(iot, ioh, 0x12, 0xdf);
    181   1.1       uwe 	igs_crtc_write(iot, ioh, 0x13, 0x50);
    182   1.1       uwe 	igs_crtc_write(iot, ioh, 0x14, 0x00);
    183   1.1       uwe 	igs_crtc_write(iot, ioh, 0x15, 0xe6);
    184   1.1       uwe 	igs_crtc_write(iot, ioh, 0x16, 0x04);
    185   1.1       uwe 	igs_crtc_write(iot, ioh, 0x17, 0xc3);
    186   1.1       uwe 
    187   1.1       uwe 	igs_crtc_write(iot, ioh, 0x18, 0xff);
    188   1.1       uwe }
    189   1.1       uwe 
    190   1.1       uwe 
    191   1.1       uwe /*
    192   1.1       uwe  * Init graphics controller.
    193   1.1       uwe  * This is common for all video modes.
    194   1.1       uwe  */
    195   1.1       uwe static void
    196   1.7       uwe igsfb_init_grfx(struct igsfb_devconfig *dc)
    197   1.1       uwe {
    198   1.2       uwe 	bus_space_tag_t iot = dc->dc_iot;
    199   1.2       uwe 	bus_space_handle_t ioh = dc->dc_ioh;
    200   1.1       uwe 
    201   1.1       uwe 	igs_grfx_write(iot, ioh, 0, 0x00);
    202   1.1       uwe 	igs_grfx_write(iot, ioh, 1, 0x00);
    203   1.1       uwe 	igs_grfx_write(iot, ioh, 2, 0x00);
    204   1.1       uwe 	igs_grfx_write(iot, ioh, 3, 0x00);
    205   1.1       uwe 	igs_grfx_write(iot, ioh, 4, 0x00);
    206   1.1       uwe 	igs_grfx_write(iot, ioh, 5, 0x60); /* SRMODE, MODE256 */
    207   1.1       uwe 	igs_grfx_write(iot, ioh, 6, 0x05); /* 64k @ a0000, GRAPHICS */
    208   1.1       uwe 	igs_grfx_write(iot, ioh, 7, 0x0f); /* color compare all */
    209   1.1       uwe 	igs_grfx_write(iot, ioh, 8, 0xff); /* bitmask = all bits mutable */
    210   1.1       uwe }
    211   1.1       uwe 
    212   1.1       uwe 
    213   1.1       uwe /*
    214   1.1       uwe  * Init attribute controller.
    215   1.1       uwe  * This is common for all video modes.
    216   1.1       uwe  */
    217   1.1       uwe static void
    218   1.7       uwe igsfb_init_attr(struct igsfb_devconfig *dc)
    219   1.1       uwe {
    220   1.2       uwe 	bus_space_tag_t iot = dc->dc_iot;
    221   1.2       uwe 	bus_space_handle_t ioh = dc->dc_ioh;
    222   1.1       uwe 	int i;
    223   1.1       uwe 
    224   1.1       uwe 	igs_attr_flip_flop(iot, ioh);	/* reset attr flip-flop to address */
    225   1.1       uwe 
    226   1.1       uwe 	for (i = 0; i < 16; ++i)	/* crt palette */
    227   1.1       uwe 		igs_attr_write(iot, ioh, i, i);
    228   1.1       uwe 
    229   1.1       uwe 	igs_attr_write(iot, ioh, 0x10, 0x01); /* select graphic mode */
    230   1.1       uwe 	igs_attr_write(iot, ioh, 0x11, 0x00); /* crt overscan color */
    231   1.1       uwe 	igs_attr_write(iot, ioh, 0x12, 0x0f); /* color plane enable */
    232   1.1       uwe 	igs_attr_write(iot, ioh, 0x13, 0x00);
    233   1.1       uwe 	igs_attr_write(iot, ioh, 0x14, 0x00);
    234   1.1       uwe }
    235   1.1       uwe 
    236   1.1       uwe 
    237   1.1       uwe /*
    238   1.1       uwe  * When done with ATTR controller, call this to unblank the screen.
    239   1.1       uwe  */
    240   1.1       uwe static void
    241   1.7       uwe igsfb_video_on(struct igsfb_devconfig *dc)
    242   1.1       uwe {
    243   1.2       uwe 	bus_space_tag_t iot = dc->dc_iot;
    244   1.2       uwe 	bus_space_handle_t ioh = dc->dc_ioh;
    245   1.1       uwe 
    246   1.1       uwe 	igs_attr_flip_flop(iot, ioh);
    247   1.1       uwe 	bus_space_write_1(iot, ioh, IGS_ATTR_IDX, 0x20);
    248   1.1       uwe 	bus_space_write_1(iot, ioh, IGS_ATTR_IDX, 0x20);
    249   1.1       uwe }
    250   1.1       uwe 
    251   1.1       uwe 
    252   1.1       uwe /*
    253   1.1       uwe  * Latch VCLK (b0/b1) and MCLK (b2/b3) values.
    254   1.1       uwe  */
    255   1.1       uwe static void
    256   1.7       uwe igsfb_freq_latch(struct igsfb_devconfig *dc)
    257   1.1       uwe {
    258   1.2       uwe 	bus_space_tag_t iot = dc->dc_iot;
    259   1.2       uwe 	bus_space_handle_t ioh = dc->dc_ioh;
    260   1.1       uwe 
    261   1.1       uwe 	bus_space_write_1(iot, ioh, IGS_EXT_IDX, 0xb9);
    262   1.1       uwe 	bus_space_write_1(iot, ioh, IGS_EXT_PORT, 0x80);
    263   1.1       uwe 	bus_space_write_1(iot, ioh, IGS_EXT_PORT, 0x00);
    264   1.1       uwe }
    265   1.1       uwe 
    266   1.1       uwe 
    267   1.1       uwe static void
    268   1.7       uwe igsfb_init_ext(struct igsfb_devconfig *dc)
    269   1.1       uwe {
    270   1.2       uwe 	bus_space_tag_t iot = dc->dc_iot;
    271   1.2       uwe 	bus_space_handle_t ioh = dc->dc_ioh;
    272   1.2       uwe 	int is_cyberpro = (dc->dc_id >= 0x2000);
    273   1.1       uwe 
    274   1.1       uwe 	igs_ext_write(iot, ioh, 0x10, 0x10); /* IGS_EXT_START_ADDR enable */
    275   1.1       uwe 	igs_ext_write(iot, ioh, 0x12, 0x00); /* IGS_EXT_IRQ_CTL disable  */
    276   1.1       uwe 	igs_ext_write(iot, ioh, 0x13, 0x00); /* MBZ for normal operation */
    277   1.1       uwe 
    278   1.1       uwe 	igs_ext_write(iot, ioh, 0x31, 0x00); /* segment write ptr */
    279   1.1       uwe 	igs_ext_write(iot, ioh, 0x32, 0x00); /* segment read ptr */
    280   1.1       uwe 
    281   1.3       uwe 	/* IGS_EXT_BIU_MISC_CTL: linearen, copren, copaselb, segon */
    282   1.3       uwe 	igs_ext_write(iot, ioh, 0x33, 0x1d);
    283   1.1       uwe 
    284   1.1       uwe 	/* sprite location */
    285   1.1       uwe 	igs_ext_write(iot, ioh, 0x50, 0x00);
    286   1.1       uwe 	igs_ext_write(iot, ioh, 0x51, 0x00);
    287   1.1       uwe 	igs_ext_write(iot, ioh, 0x52, 0x00);
    288   1.1       uwe 	igs_ext_write(iot, ioh, 0x53, 0x00);
    289   1.1       uwe 	igs_ext_write(iot, ioh, 0x54, 0x00);
    290   1.1       uwe 	igs_ext_write(iot, ioh, 0x55, 0x00);
    291   1.1       uwe 	igs_ext_write(iot, ioh, 0x56, 0x00); /* sprite control */
    292   1.1       uwe 
    293   1.1       uwe 	/* IGS_EXT_GRFX_MODE */
    294   1.1       uwe 	igs_ext_write(iot, ioh, 0x57, 0x01); /* raster fb */
    295   1.1       uwe 
    296   1.1       uwe 	/* overscan R/G/B */
    297   1.1       uwe 	igs_ext_write(iot, ioh, 0x58, 0x00);
    298   1.1       uwe 	igs_ext_write(iot, ioh, 0x59, 0x00);
    299   1.1       uwe 	igs_ext_write(iot, ioh, 0x5A, 0x00);
    300   1.1       uwe 
    301   1.1       uwe 	/*
    302   1.1       uwe 	 * Video memory size &c.  We rely on firmware to program
    303   1.1       uwe 	 * BUS_CTL(30), MEM_CTL1(71), MEM_CTL2(72) appropriately.
    304   1.1       uwe 	 */
    305   1.1       uwe 
    306   1.1       uwe 	/* ext memory ctl0 */
    307   1.1       uwe 	igs_ext_write(iot, ioh, 0x70, 0x0B); /* enable fifo, seq */
    308   1.1       uwe 
    309   1.1       uwe 	/* ext hidden ctl1 */
    310   1.1       uwe 	igs_ext_write(iot, ioh, 0x73, 0x30); /* XXX: krups: 0x20 */
    311   1.1       uwe 
    312   1.1       uwe 	/* ext fifo control */
    313   1.1       uwe 	igs_ext_write(iot, ioh, 0x74, 0x10); /* XXX: krups: 0x1b */
    314   1.1       uwe 	igs_ext_write(iot, ioh, 0x75, 0x10); /* XXX: krups: 0x1e */
    315   1.1       uwe 
    316   1.1       uwe 	igs_ext_write(iot, ioh, 0x76, 0x00); /* ext seq. */
    317   1.1       uwe 	igs_ext_write(iot, ioh, 0x7A, 0xC8); /* ext. hidden ctl */
    318   1.1       uwe 
    319   1.1       uwe 	/* ext graphics ctl: GCEXTPATH.  krups 1, nettrom 1, docs 3 */
    320   1.1       uwe 	igs_ext_write(iot, ioh, 0x90, 0x01);
    321   1.1       uwe 
    322   1.2       uwe 	if (is_cyberpro)	/* select normal vclk/mclk registers */
    323   1.1       uwe 	    igs_ext_write(iot, ioh, 0xBF, 0x00);
    324   1.1       uwe 
    325   1.1       uwe 	igs_ext_write(iot, ioh, 0xB0, 0xD2); /* VCLK = 25.175MHz */
    326   1.1       uwe 	igs_ext_write(iot, ioh, 0xB1, 0xD3);
    327   1.1       uwe 	igs_ext_write(iot, ioh, 0xB2, 0xDB); /* MCLK = 75MHz*/
    328   1.1       uwe 	igs_ext_write(iot, ioh, 0xB3, 0x54);
    329   1.2       uwe 	igsfb_freq_latch(dc);
    330   1.1       uwe 
    331   1.2       uwe 	if (is_cyberpro)
    332   1.1       uwe 	    igs_ext_write(iot, ioh, 0xF8, 0x04); /* XXX: ??? */
    333   1.1       uwe 
    334   1.1       uwe 	/* 640x480 8bpp at 60Hz */
    335   1.1       uwe 	igs_ext_write(iot, ioh, 0x11, 0x00);
    336   1.1       uwe 	igs_ext_write(iot, ioh, 0x77, 0x01); /* 8bpp, indexed */
    337   1.1       uwe 	igs_ext_write(iot, ioh, 0x14, 0x51);
    338   1.1       uwe 	igs_ext_write(iot, ioh, 0x15, 0x00);
    339   1.1       uwe }
    340   1.1       uwe 
    341   1.1       uwe 
    342   1.1       uwe static void
    343   1.7       uwe igsfb_init_dac(struct igsfb_devconfig *dc)
    344   1.1       uwe {
    345   1.2       uwe 	bus_space_tag_t iot = dc->dc_iot;
    346   1.2       uwe 	bus_space_handle_t ioh = dc->dc_ioh;
    347   1.4       uwe 	uint8_t reg;
    348   1.1       uwe 
    349   1.1       uwe 	/* RAMDAC address 2 select */
    350   1.1       uwe 	reg = igs_ext_read(iot, ioh, IGS_EXT_SPRITE_CTL);
    351   1.1       uwe 	igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
    352   1.1       uwe 		      reg | IGS_EXT_SPRITE_DAC_PEL);
    353   1.1       uwe 
    354   1.1       uwe 	/* VREFEN, DAC8 */
    355   1.1       uwe 	bus_space_write_1(iot, ioh, IGS_DAC_CMD, 0x06);
    356   1.1       uwe 
    357   1.1       uwe 	/* restore */
    358   1.1       uwe 	igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL, reg);
    359   1.1       uwe 
    360   1.1       uwe 	bus_space_write_1(iot, ioh, IGS_PEL_MASK, 0xff);
    361   1.1       uwe }
    362   1.1       uwe 
    363   1.1       uwe 
    364   1.1       uwe void
    365   1.7       uwe igsfb_1024x768_8bpp_60Hz(struct igsfb_devconfig *dc)
    366   1.1       uwe {
    367   1.2       uwe 	bus_space_tag_t iot = dc->dc_iot;
    368   1.2       uwe 	bus_space_handle_t ioh = dc->dc_ioh;
    369   1.1       uwe 
    370   1.1       uwe 	igs_crtc_write(iot, ioh, 0x11, 0x00); /* write enable CRTC 0..7 */
    371   1.1       uwe 
    372   1.1       uwe 	igs_crtc_write(iot, ioh, 0x00, 0xa3);
    373   1.1       uwe 	igs_crtc_write(iot, ioh, 0x01, 0x7f);
    374   1.1       uwe 	igs_crtc_write(iot, ioh, 0x02, 0x7f); /* krups: 80 */
    375   1.1       uwe 	igs_crtc_write(iot, ioh, 0x03, 0x85); /* krups: 84 */
    376   1.1       uwe 	igs_crtc_write(iot, ioh, 0x04, 0x84); /* krups: 88 */
    377   1.1       uwe 	igs_crtc_write(iot, ioh, 0x05, 0x95); /* krups: 99 */
    378   1.1       uwe 	igs_crtc_write(iot, ioh, 0x06, 0x24);
    379   1.1       uwe 	igs_crtc_write(iot, ioh, 0x07, 0xfd);
    380   1.1       uwe 
    381   1.1       uwe 	/* next block is almost constant, only bit 6 in reg 9 differs */
    382   1.1       uwe 	igs_crtc_write(iot, ioh, 0x08, 0x00);
    383   1.1       uwe 	igs_crtc_write(iot, ioh, 0x09, 0x60); /* <- either 0x40 or 0x60 */
    384   1.1       uwe 	igs_crtc_write(iot, ioh, 0x0a, 0x00);
    385   1.1       uwe 	igs_crtc_write(iot, ioh, 0x0b, 0x00);
    386   1.1       uwe 	igs_crtc_write(iot, ioh, 0x0c, 0x00);
    387   1.1       uwe 	igs_crtc_write(iot, ioh, 0x0d, 0x00);
    388   1.1       uwe 	igs_crtc_write(iot, ioh, 0x0e, 0x00);
    389   1.1       uwe 	igs_crtc_write(iot, ioh, 0x0f, 0x00);
    390   1.1       uwe 
    391   1.1       uwe 	igs_crtc_write(iot, ioh, 0x10, 0x06);
    392   1.1       uwe 	igs_crtc_write(iot, ioh, 0x11, 0x8c);
    393   1.1       uwe 	igs_crtc_write(iot, ioh, 0x12, 0xff);
    394   1.1       uwe 	igs_crtc_write(iot, ioh, 0x13, 0x80); /* depends on BPP */
    395   1.1       uwe 	igs_crtc_write(iot, ioh, 0x14, 0x0f);
    396   1.1       uwe 	igs_crtc_write(iot, ioh, 0x15, 0x02);
    397   1.1       uwe 	igs_crtc_write(iot, ioh, 0x16, 0x21);
    398   1.1       uwe 	igs_crtc_write(iot, ioh, 0x17, 0xe3);
    399   1.1       uwe 	igs_crtc_write(iot, ioh, 0x18, 0xff);
    400   1.1       uwe 
    401   1.1       uwe 	igs_ext_write(iot, ioh, 0xB0, 0xE2); /* VCLK */
    402   1.1       uwe 	igs_ext_write(iot, ioh, 0xB1, 0x58);
    403   1.1       uwe #if 1
    404   1.1       uwe 	/* XXX: hmm, krups does this */
    405   1.1       uwe 	igs_ext_write(iot, ioh, 0xB2, 0xE2); /* MCLK */
    406   1.1       uwe 	igs_ext_write(iot, ioh, 0xB3, 0x58);
    407   1.1       uwe #endif
    408   1.2       uwe 	igsfb_freq_latch(dc);
    409   1.1       uwe 
    410   1.1       uwe 	igs_ext_write(iot, ioh, 0x11, 0x00);
    411   1.1       uwe 	igs_ext_write(iot, ioh, 0x77, 0x01); /* 8bpp, indexed */
    412   1.1       uwe 	igs_ext_write(iot, ioh, 0x14, 0x81);
    413   1.1       uwe 	igs_ext_write(iot, ioh, 0x15, 0x00);
    414  1.10  macallan 
    415  1.10  macallan 	dc->dc_width = 1024;
    416  1.10  macallan 	dc->dc_height = 768;
    417  1.10  macallan 	dc->dc_depth = 8;
    418  1.10  macallan 	dc->dc_stride = dc->dc_width;
    419   1.1       uwe }
    420   1.1       uwe 
    421   1.1       uwe 
    422   1.1       uwe /*
    423   1.1       uwe  * igs-video-init from krups prom
    424   1.1       uwe  */
    425   1.1       uwe void
    426   1.7       uwe igsfb_hw_setup(struct igsfb_devconfig *dc)
    427   1.1       uwe {
    428  1.10  macallan 	const struct videomode *mode = NULL;
    429  1.10  macallan 	int i;
    430   1.1       uwe 
    431   1.2       uwe 	igsfb_init_seq(dc);
    432   1.2       uwe 	igsfb_init_crtc(dc);
    433   1.2       uwe 	igsfb_init_attr(dc);
    434   1.2       uwe 	igsfb_init_grfx(dc);
    435   1.2       uwe 	igsfb_init_ext(dc);
    436   1.2       uwe 	igsfb_init_dac(dc);
    437   1.1       uwe 
    438  1.10  macallan 	i = 0;
    439  1.10  macallan 	while ((strcmp(dc->dc_modestring, videomode_list[i].name) != 0) &&
    440  1.10  macallan 	       ( i < videomode_count)) {
    441  1.10  macallan 		i++;
    442  1.10  macallan 	}
    443  1.10  macallan 
    444  1.10  macallan 	if (i < videomode_count) {
    445  1.10  macallan 		/* found a mode, now let's see if we can display it */
    446  1.10  macallan 		if ((videomode_list[i].dot_clock <= IGS_MAX_CLOCK) &&
    447  1.10  macallan 		    (videomode_list[i].hdisplay <= 2048) &&
    448  1.10  macallan 		    (videomode_list[i].hdisplay >= 320) &&
    449  1.10  macallan 		    (videomode_list[i].vdisplay <= 2048) &&
    450  1.10  macallan 		    (videomode_list[i].vdisplay >= 200)) {
    451  1.10  macallan 		 	mode = &videomode_list[i];
    452  1.10  macallan 		}
    453  1.10  macallan 	}
    454  1.10  macallan 
    455  1.10  macallan 	if (mode != NULL) {
    456  1.10  macallan 		igsfb_set_mode(dc, mode, 8);
    457  1.10  macallan 	} else
    458  1.10  macallan 		igsfb_1024x768_8bpp_60Hz(dc);
    459  1.10  macallan 
    460   1.2       uwe 	igsfb_video_on(dc);
    461   1.1       uwe }
    462  1.10  macallan 
    463  1.10  macallan void
    464  1.10  macallan igsfb_set_mode(struct igsfb_devconfig *dc, const struct videomode *mode,
    465  1.10  macallan     int depth)
    466  1.10  macallan {
    467  1.10  macallan 	bus_space_tag_t iot = dc->dc_iot;
    468  1.10  macallan 	bus_space_handle_t ioh = dc->dc_ioh;
    469  1.10  macallan 	int i, m, n, p, hoffset, bytes_per_pixel, memfetch;
    470  1.10  macallan 	int vsync_start, hsync_start, vsync_end, hsync_end;
    471  1.10  macallan 	int vblank_start, vblank_end, hblank_start, hblank_end;
    472  1.10  macallan 	uint8_t vclk1, vclk2, vclk3, overflow;
    473  1.10  macallan 
    474  1.10  macallan 	bytes_per_pixel = depth >> 3;
    475  1.10  macallan 	hoffset = (mode->hdisplay >> 3) * bytes_per_pixel;
    476  1.10  macallan 	memfetch = hoffset + 1;
    477  1.10  macallan 	overflow = (((mode->vtotal - 2) & 0x400) >> 10) |
    478  1.10  macallan 	    (((mode->vdisplay -1) & 0x400) >> 9) |
    479  1.10  macallan 	    ((mode->vsync_start & 0x400) >> 8) |
    480  1.10  macallan 	    ((mode->vsync_start & 0x400) >> 7) |
    481  1.10  macallan 	    0x10;
    482  1.10  macallan 
    483  1.10  macallan 	if (depth == 8) {
    484  1.10  macallan 		/* palette mode */
    485  1.10  macallan 		bus_space_write_1(dc->dc_iot, dc->dc_ioh, IGS_DAC_CMD, 0x06);
    486  1.10  macallan 	} else {
    487  1.10  macallan 		/* bypass palette */
    488  1.10  macallan 		bus_space_write_1(dc->dc_iot, dc->dc_ioh, IGS_DAC_CMD, 0x16);
    489  1.10  macallan 	}
    490  1.10  macallan 
    491  1.10  macallan 	igs_crtc_write(iot, ioh, 0x11, 0x00); /* write enable CRTC 0..7 */
    492  1.10  macallan 
    493  1.10  macallan 	hsync_start = mode->hsync_start;
    494  1.10  macallan 	hsync_end = mode->hsync_end;
    495  1.10  macallan 
    496  1.10  macallan 	hblank_start = min(mode->hsync_start, mode->hdisplay);
    497  1.10  macallan 	hblank_end = hsync_end;
    498  1.10  macallan 	if ((hblank_end - hblank_start) >= 63 * 8) {
    499  1.10  macallan 
    500  1.10  macallan 		/*
    501  1.10  macallan 		 * H Blanking size must be < 63*8. Same remark as above.
    502  1.10  macallan 		 */
    503  1.10  macallan 		hblank_start = hblank_end - 63 * 8;
    504  1.10  macallan 	}
    505  1.10  macallan 
    506  1.10  macallan 	vblank_start = min(mode->vsync_start, mode->vdisplay);
    507  1.10  macallan 	vblank_end = mode->vsync_end;
    508  1.10  macallan 
    509  1.10  macallan 	vsync_start = mode->vsync_start;
    510  1.10  macallan 	vsync_end = mode->vsync_end;
    511  1.10  macallan 	igs_crtc_write(iot, ioh, 0x00, (mode->htotal >> 3) - 5);
    512  1.10  macallan 	igs_crtc_write(iot, ioh, 0x01, (mode->hdisplay >> 3) - 1);
    513  1.10  macallan 	igs_crtc_write(iot, ioh, 0x02, (hblank_start >> 3) - 1);
    514  1.10  macallan 	igs_crtc_write(iot, ioh, 0x03, 0x80 | (((hblank_end >> 3) - 1) & 0x1f));
    515  1.10  macallan 	igs_crtc_write(iot, ioh, 0x04, hsync_start >> 3);
    516  1.10  macallan 	igs_crtc_write(iot, ioh, 0x05, ((((hblank_end >> 3) - 1)  & 0x20) << 2)
    517  1.10  macallan 	    | ((hsync_end >> 3) & 0x1f));
    518  1.10  macallan 	igs_crtc_write(iot, ioh, 0x06, (mode->vtotal - 2) & 0xff);
    519  1.10  macallan 	igs_crtc_write(iot, ioh, 0x07,
    520  1.10  macallan 	    ((vsync_start & 0x200) >> 2) |
    521  1.10  macallan 	    (((mode->vdisplay - 1) & 0x200) >> 3) |
    522  1.10  macallan 	    (((mode->vtotal - 2) & 0x200) >> 4) |
    523  1.10  macallan 	    0x10 |
    524  1.10  macallan 	    (((vblank_start - 1) & 0x100) >> 5) |
    525  1.10  macallan 	    ((vsync_start  & 0x100) >> 6) |
    526  1.10  macallan 	    (((mode->vdisplay - 1)  & 0x100) >> 7) |
    527  1.10  macallan 	    ((mode->vtotal  & 0x100) >> 8));
    528  1.10  macallan 
    529  1.10  macallan 	igs_crtc_write(iot, ioh, 0x08, 0x00);
    530  1.10  macallan 	igs_crtc_write(iot, ioh, 0x09, 0x40 |
    531  1.10  macallan 	    (((vblank_start - 1) & 0x200) >> 4));
    532  1.10  macallan 	igs_crtc_write(iot, ioh, 0x0a, 0x00);
    533  1.10  macallan 	igs_crtc_write(iot, ioh, 0x0b, 0x00);
    534  1.10  macallan 	igs_crtc_write(iot, ioh, 0x0c, 0x00);
    535  1.10  macallan 	igs_crtc_write(iot, ioh, 0x0d, 0x00);
    536  1.10  macallan 	igs_crtc_write(iot, ioh, 0x0e, 0x00);
    537  1.10  macallan 	igs_crtc_write(iot, ioh, 0x0f, 0x00);
    538  1.10  macallan 
    539  1.10  macallan 	igs_crtc_write(iot, ioh, 0x10, vsync_start & 0xff);
    540  1.10  macallan 	igs_crtc_write(iot, ioh, 0x11, (vsync_end & 0x0f) | 0x20);
    541  1.10  macallan 	igs_crtc_write(iot, ioh, 0x12, (mode->vdisplay - 1) & 0xff);
    542  1.10  macallan 	igs_crtc_write(iot, ioh, 0x13, hoffset & 0xff);
    543  1.10  macallan 	igs_crtc_write(iot, ioh, 0x14, 0x0f);
    544  1.10  macallan 	igs_crtc_write(iot, ioh, 0x15, (vblank_start - 1) & 0xff);
    545  1.10  macallan 	igs_crtc_write(iot, ioh, 0x16, (vblank_end - 1) & 0xff);
    546  1.10  macallan 	igs_crtc_write(iot, ioh, 0x17, 0xe3);
    547  1.10  macallan 	igs_crtc_write(iot, ioh, 0x18, 0xff);
    548  1.10  macallan 
    549  1.10  macallan 	for (i = 0; i < 0x10; i++)
    550  1.10  macallan 		igs_attr_write(iot, ioh, i, i);
    551  1.10  macallan 
    552  1.10  macallan 	igs_attr_write(iot, ioh, 0x10, 0x01);
    553  1.10  macallan 	igs_attr_write(iot, ioh, 0x11, 0x00);
    554  1.10  macallan 	igs_attr_write(iot, ioh, 0x12, 0x0f);
    555  1.10  macallan 	igs_attr_write(iot, ioh, 0x13, 0x00);
    556  1.10  macallan 
    557  1.10  macallan 	igs_grfx_write(iot, ioh, 0x00, 0x00);
    558  1.10  macallan 	igs_grfx_write(iot, ioh, 0x01, 0x00);
    559  1.10  macallan 	igs_grfx_write(iot, ioh, 0x02, 0x00);
    560  1.10  macallan 	igs_grfx_write(iot, ioh, 0x03, 0x00);
    561  1.10  macallan 	igs_grfx_write(iot, ioh, 0x04, 0x00);
    562  1.10  macallan 	igs_grfx_write(iot, ioh, 0x05, 0x60);
    563  1.10  macallan 	igs_grfx_write(iot, ioh, 0x06, 0x05);
    564  1.10  macallan 	igs_grfx_write(iot, ioh, 0x07, 0x0f);
    565  1.10  macallan 	igs_grfx_write(iot, ioh, 0x08, 0xff);
    566  1.10  macallan 
    567  1.10  macallan 	/* crank up memory clock to 95MHz - needed for higher resolutions */
    568  1.10  macallan 	igs_ext_write(iot, ioh, 0xB2, 0x91);
    569  1.10  macallan 	igs_ext_write(iot, ioh, 0xB3, 0x6a);
    570  1.10  macallan 	igsfb_freq_latch(dc);
    571  1.10  macallan 
    572  1.10  macallan 	igs_ext_write(iot, ioh, 0x11, overflow);
    573  1.10  macallan 	igs_ext_write(iot, ioh, 0x77, bytes_per_pixel);
    574  1.10  macallan 	igs_ext_write(iot, ioh, 0x14, memfetch & 0xff);
    575  1.10  macallan 	igs_ext_write(iot, ioh, 0x15,
    576  1.10  macallan 	    ((memfetch & 0x300) >> 8) | ((hoffset & 0x300) >> 4));
    577  1.10  macallan 	igs_ext_write(iot, ioh, 0x56, 0x00);
    578  1.10  macallan 
    579  1.10  macallan 	/* finally set the dot clock */
    580  1.10  macallan 	igsfb_calc_pll(mode->dot_clock, &m, &n, &p, 2047, 255, 7, IGS_MIN_VCO);
    581  1.10  macallan 	DPRINTF("m: %x, n: %x, p: %x\n", m, n, p);
    582  1.10  macallan 	vclk1 = m & 0xff;
    583  1.10  macallan 	vclk2 = (n & 0x1f) | ((p << 6) & 0xc0) |
    584  1.10  macallan 	    (mode->dot_clock > 180000 ? 0x20 : 0);
    585  1.10  macallan 	vclk3 = ((m >> 8) & 0x7) | ((n >> 2) & 0x38) | ((p << 4) & 0x40);
    586  1.10  macallan 	DPRINTF("clk: %02x %02x %02x\n", vclk1, vclk2, vclk3);
    587  1.10  macallan 	igs_ext_write(iot, ioh, 0xB0, vclk1);
    588  1.10  macallan 	igs_ext_write(iot, ioh, 0xB1, vclk2);
    589  1.10  macallan 	igs_ext_write(iot, ioh, 0xBA, vclk3);
    590  1.10  macallan 	igsfb_freq_latch(dc);
    591  1.10  macallan 	DPRINTF("clock: %d\n", IGS_CLOCK(m, n, p));
    592  1.10  macallan 
    593  1.10  macallan 	dc->dc_width = mode->hdisplay;
    594  1.10  macallan 	dc->dc_height = mode->vdisplay;
    595  1.10  macallan 	dc->dc_depth = depth;
    596  1.10  macallan 	dc->dc_stride = dc->dc_width * bytes_per_pixel;
    597  1.10  macallan }
    598  1.10  macallan 
    599  1.10  macallan 
    600  1.10  macallan static void
    601  1.10  macallan igsfb_calc_pll(int target, int *Mp, int *Np, int *Pp, int maxM, int maxN,
    602  1.10  macallan     int maxP, int minVco)
    603  1.10  macallan {
    604  1.10  macallan     int	    M, N, P, bestM = 0, bestN = 0;
    605  1.10  macallan     int	    f_vco, f_out;
    606  1.10  macallan     int	    err, besterr;
    607  1.10  macallan 
    608  1.10  macallan     /*
    609  1.10  macallan      * Compute correct P value to keep VCO in range
    610  1.10  macallan      */
    611  1.10  macallan     for (P = 0; P <= maxP; P++)
    612  1.10  macallan     {
    613  1.10  macallan 	f_vco = target * IGS_SCALE(P);
    614  1.10  macallan 	if (f_vco >= minVco)
    615  1.10  macallan 	    break;
    616  1.10  macallan     }
    617  1.10  macallan 
    618  1.10  macallan     /* M = f_out / f_ref * ((N + 1) * IGS_SCALE(P)); */
    619  1.10  macallan     besterr = target;
    620  1.10  macallan     for (N = 1; N <= maxN; N++)
    621  1.10  macallan     {
    622  1.10  macallan 	M = ((target * (N + 1) * IGS_SCALE(P) + (IGS_CLOCK_REF/2)) +
    623  1.10  macallan 	    IGS_CLOCK_REF/2) / IGS_CLOCK_REF - 1;
    624  1.10  macallan 	if (0 <= M && M <= maxM)
    625  1.10  macallan 	{
    626  1.10  macallan 	    f_out = IGS_CLOCK(M,N,P);
    627  1.10  macallan 	    err = target - f_out;
    628  1.10  macallan 	    if (err < 0)
    629  1.10  macallan 		err = -err;
    630  1.10  macallan 	    if (err < besterr)
    631  1.10  macallan 	    {
    632  1.10  macallan 		besterr = err;
    633  1.10  macallan 		bestM = M;
    634  1.10  macallan 		bestN = N;
    635  1.10  macallan 	    }
    636  1.10  macallan 	}
    637  1.10  macallan     }
    638  1.10  macallan     *Mp = bestM;
    639  1.10  macallan     *Np = bestN;
    640  1.10  macallan     *Pp = P;
    641  1.10  macallan }
    642