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