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