Home | History | Annotate | Line # | Download | only in dev
mfp.c revision 1.19
      1  1.19    isaki /*	$NetBSD: mfp.c,v 1.19 2007/03/11 08:09:24 isaki Exp $	*/
      2   1.2  minoura 
      3   1.5  minoura /*-
      4   1.2  minoura  * Copyright (c) 1998 NetBSD Foundation, Inc.
      5   1.2  minoura  * All rights reserved.
      6   1.2  minoura  *
      7   1.2  minoura  * Redistribution and use in source and binary forms, with or without
      8   1.2  minoura  * modification, are permitted provided that the following conditions
      9   1.2  minoura  * are met:
     10   1.2  minoura  * 1. Redistributions of source code must retain the above copyright
     11   1.2  minoura  *    notice, this list of conditions and the following disclaimer.
     12   1.2  minoura  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.2  minoura  *    notice, this list of conditions and the following disclaimer in the
     14   1.2  minoura  *    documentation and/or other materials provided with the distribution.
     15   1.2  minoura  * 3. All advertising materials mentioning features or use of this software
     16   1.2  minoura  *    must display the following acknowledgement:
     17   1.5  minoura  *        This product includes software developed by the NetBSD
     18   1.5  minoura  *        Foundation, Inc. and its contributors.
     19   1.2  minoura  * 4. The name of the author may not be used to endorse or promote products
     20   1.2  minoura  *    derived from this software without specific prior written permission.
     21   1.2  minoura  *
     22   1.5  minoura  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23   1.5  minoura  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24   1.5  minoura  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25   1.5  minoura  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26   1.5  minoura  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27   1.5  minoura  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28   1.5  minoura  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29   1.5  minoura  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30   1.5  minoura  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31   1.5  minoura  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32   1.5  minoura  * POSSIBILITY OF SUCH DAMAGE.
     33   1.2  minoura  */
     34   1.2  minoura 
     35   1.2  minoura /*
     36   1.2  minoura  * MC68901 MFP (multi function periferal) driver for NetBSD/x68k
     37   1.2  minoura  */
     38   1.2  minoura 
     39   1.2  minoura /*
     40  1.11      wiz  * MFP is used as keyboard controller, which may be used before
     41   1.2  minoura  * ordinary initialization.
     42   1.2  minoura  */
     43  1.10    lukem 
     44  1.10    lukem #include <sys/cdefs.h>
     45  1.19    isaki __KERNEL_RCSID(0, "$NetBSD: mfp.c,v 1.19 2007/03/11 08:09:24 isaki Exp $");
     46   1.2  minoura 
     47   1.2  minoura #include <sys/param.h>
     48   1.2  minoura #include <sys/systm.h>
     49   1.2  minoura #include <sys/device.h>
     50   1.2  minoura #include <sys/malloc.h>
     51   1.2  minoura 
     52   1.2  minoura #include <machine/bus.h>
     53   1.2  minoura #include <machine/cpu.h>
     54   1.2  minoura 
     55   1.2  minoura #include <arch/x68k/dev/intiovar.h>
     56   1.2  minoura #include <arch/x68k/dev/mfp.h>
     57   1.2  minoura 
     58  1.14      chs static int mfp_match(struct device *, struct cfdata *, void *);
     59  1.14      chs static void mfp_attach(struct device *, struct device *, void *);
     60  1.14      chs static void mfp_init(void);
     61  1.14      chs static void mfp_calibrate_delay(void);
     62   1.2  minoura 
     63   1.8  thorpej CFATTACH_DECL(mfp, sizeof(struct mfp_softc),
     64   1.9  thorpej     mfp_match, mfp_attach, NULL, NULL);
     65   1.2  minoura 
     66  1.12      chs static int mfp_attached;
     67  1.12      chs 
     68  1.18    isaki static int
     69  1.14      chs mfp_match(struct device *parent, struct cfdata *cf, void *aux)
     70   1.2  minoura {
     71   1.2  minoura 	struct intio_attach_args *ia = aux;
     72   1.2  minoura 
     73   1.2  minoura 	/* mfp0 */
     74  1.19    isaki 	if (strcmp(ia->ia_name, "mfp") != 0)
     75   1.2  minoura 		return 0;
     76  1.12      chs 	if (mfp_attached)
     77   1.2  minoura 		return (0);
     78   1.2  minoura 
     79   1.2  minoura 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
     80   1.2  minoura 		ia->ia_addr = MFP_ADDR;
     81   1.2  minoura 	if (ia->ia_intr == INTIOCF_INTR_DEFAULT)
     82   1.2  minoura 		ia->ia_addr = MFP_INTR;
     83   1.2  minoura 
     84   1.2  minoura 	/* fixed address */
     85   1.2  minoura 	if (ia->ia_addr != MFP_ADDR)
     86   1.2  minoura 		return (0);
     87   1.2  minoura 	if (ia->ia_intr != MFP_INTR)
     88   1.2  minoura 		return (0);
     89   1.2  minoura 
     90   1.2  minoura 	return (1);
     91   1.2  minoura }
     92   1.2  minoura 
     93   1.2  minoura 
     94  1.18    isaki static void
     95  1.14      chs mfp_attach(struct device *parent, struct device *self, void *aux)
     96   1.2  minoura {
     97   1.2  minoura 	struct mfp_softc *sc = (struct mfp_softc *)self;
     98   1.2  minoura 	struct intio_attach_args *ia = aux;
     99   1.2  minoura 
    100  1.19    isaki 	mfp_init();
    101   1.2  minoura 
    102   1.2  minoura 	if (sc != NULL) {
    103   1.2  minoura 		/* realconfig */
    104   1.2  minoura 		int r;
    105   1.2  minoura 
    106  1.19    isaki 		printf("\n");
    107   1.2  minoura 
    108  1.13  minoura 		mfp_attached = 1;
    109   1.2  minoura 		sc->sc_bst = ia->ia_bst;
    110   1.2  minoura 		sc->sc_intr = ia->ia_intr;
    111   1.2  minoura 		ia->ia_size = 0x30;
    112  1.19    isaki 		r = intio_map_allocate_region(parent, ia, INTIO_MAP_ALLOCATE);
    113   1.2  minoura #ifdef DIAGNOSTIC
    114   1.2  minoura 		if (r)
    115  1.19    isaki 			panic("IO map for MFP corruption??");
    116   1.2  minoura #endif
    117   1.2  minoura 		bus_space_map(ia->ia_bst, ia->ia_addr, 0x2000, 0, &sc->sc_bht);
    118  1.19    isaki 		config_found(self, __UNCONST("kbd"), NULL);
    119  1.19    isaki 		config_found(self, __UNCONST("clock"), NULL);
    120  1.19    isaki 		config_found(self, __UNCONST("pow"), NULL);
    121   1.4  minoura 	} else {
    122   1.4  minoura 		/*
    123   1.4  minoura 		 * Called from config_console;
    124   1.4  minoura 		 * calibrate the DELAY loop counter
    125   1.4  minoura 		 */
    126   1.4  minoura 		mfp_calibrate_delay();
    127   1.2  minoura 	}
    128   1.2  minoura }
    129   1.2  minoura 
    130   1.2  minoura static void
    131  1.14      chs mfp_init(void)
    132   1.2  minoura {
    133   1.2  minoura #if 0				/* done in x68k_init.c::intr_reset() */
    134   1.2  minoura 	mfp_set_vr(MFP_INTR);
    135   1.2  minoura 
    136   1.2  minoura 	/* stop all interrupts */
    137   1.2  minoura 	mfp_set_iera(0);
    138   1.2  minoura 	mfp_set_ierb(0);
    139   1.2  minoura #endif
    140   1.2  minoura 
    141   1.2  minoura 	/* Timer A settings */
    142   1.2  minoura 	mfp_set_tacr(MFP_TIMERA_RESET | MFP_TIMERA_STOP);
    143   1.2  minoura 
    144   1.2  minoura 	/* Timer B settings: used for USART clock */
    145   1.2  minoura 	mfp_set_tbcr(MFP_TIMERB_RESET | MFP_TIMERB_STOP);
    146   1.2  minoura 
    147   1.2  minoura 	/* Timer C/D settings */
    148   1.2  minoura 	mfp_set_tcdcr(0);
    149   1.4  minoura }
    150   1.4  minoura 
    151   1.4  minoura extern int delay_divisor;
    152  1.14      chs void	_delay(u_int);
    153   1.4  minoura 
    154   1.4  minoura static void
    155   1.4  minoura mfp_calibrate_delay(void)
    156   1.4  minoura {
    157   1.4  minoura 	/*
    158   1.4  minoura 	 * Stolen from mvme68k.
    159   1.4  minoura 	 */
    160   1.4  minoura 	/*
    161   1.4  minoura 	 * X68k provides 4MHz clock (= 0.25usec) for MFP timer C.
    162   1.4  minoura 	 * 10000usec = 0.25usec * 200 * 200
    163   1.4  minoura 	 * Our slowest clock is 20MHz (?).  Its delay_divisor value
    164   1.4  minoura 	 * should be about 102.  Start from 140 here.
    165   1.4  minoura 	 */
    166   1.4  minoura 	for (delay_divisor = 140; delay_divisor > 0; delay_divisor--) {
    167   1.4  minoura 		mfp_set_tcdr(255-0);
    168   1.4  minoura 		mfp_set_tcdcr(0x70); /* 1/200 delay mode */
    169   1.4  minoura 		_delay(10000 << 8);
    170   1.4  minoura 		mfp_set_tcdcr(0); /* stop timer */
    171   1.4  minoura 		if ((255 - mfp_get_tcdr()) > 200)
    172   1.4  minoura 			break;	/* got it! */
    173   1.4  minoura 		/* retry! */
    174   1.4  minoura 	}
    175   1.2  minoura }
    176   1.2  minoura 
    177   1.2  minoura /*
    178   1.2  minoura  * MFP utility functions
    179   1.2  minoura  */
    180   1.2  minoura 
    181   1.2  minoura /*
    182   1.2  minoura  * wait for built-in display hsync.
    183   1.2  minoura  * should be called before writing to frame buffer.
    184   1.2  minoura  * might be called before realconfig.
    185   1.2  minoura  */
    186   1.2  minoura void
    187  1.14      chs mfp_wait_for_hsync(void)
    188   1.2  minoura {
    189   1.2  minoura 	/* wait for CRT HSYNC */
    190   1.2  minoura 	while (mfp_get_gpip() & MFP_GPIP_HSYNC)
    191  1.17    perry 		__asm("nop");
    192   1.2  minoura 	while (!(mfp_get_gpip() & MFP_GPIP_HSYNC))
    193  1.17    perry 		__asm("nop");
    194   1.2  minoura }
    195   1.2  minoura 
    196   1.2  minoura /*
    197   1.2  minoura  * send COMMAND to the MFP USART.
    198   1.2  minoura  * USART is attached to the keyboard.
    199   1.2  minoura  * might be called before realconfig.
    200   1.2  minoura  */
    201  1.18    isaki int
    202  1.14      chs mfp_send_usart(int command)
    203   1.2  minoura {
    204   1.2  minoura 	while (!(mfp_get_tsr() & MFP_TSR_BE));
    205   1.2  minoura 	mfp_set_udr(command);
    206   1.2  minoura 
    207   1.2  minoura 	return 0;
    208   1.2  minoura }
    209   1.2  minoura 
    210   1.2  minoura int
    211   1.6      wiz mfp_receive_usart(void)
    212   1.2  minoura {
    213   1.2  minoura 	while (!(mfp_get_rsr() & MFP_RSR_BF))
    214  1.17    perry 		__asm("nop");
    215   1.2  minoura 	return mfp_get_udr();
    216   1.2  minoura }
    217