Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: mfp.c,v 1.34 2026/05/19 21:18:11 andvar 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 peripheral) 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.34 2026/05/19 21:18:11 andvar Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/device.h>
     44 
     45 #include <machine/bus.h>
     46 #include <machine/cpu.h>
     47 #include <machine/autoconf.h>
     48 
     49 #include <arch/x68k/dev/intiovar.h>
     50 #include <arch/x68k/dev/mfp.h>
     51 
     52 static int  mfp_match(device_t, cfdata_t, void *);
     53 static void mfp_attach(device_t, device_t, void *);
     54 static int  mfp_search(device_t, cfdata_t, const int *, void *);
     55 static void mfp_init(void);
     56 static void mfp_calibrate_delay(void);
     57 
     58 CFATTACH_DECL_NEW(mfp, sizeof(struct mfp_softc),
     59     mfp_match, mfp_attach, NULL, NULL);
     60 
     61 static int mfp_attached;
     62 
     63 static int
     64 mfp_match(device_t parent, cfdata_t cf, void *aux)
     65 {
     66 	struct intio_attach_args *ia = aux;
     67 
     68 	/* mfp0 */
     69 	if (strcmp(ia->ia_name, "mfp") != 0)
     70 		return 0;
     71 	if (mfp_attached)
     72 		return (0);
     73 
     74 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
     75 		ia->ia_addr = MFP_ADDR;
     76 	if (ia->ia_intr == INTIOCF_INTR_DEFAULT)
     77 		ia->ia_addr = MFP_INTR;
     78 
     79 	/* fixed address */
     80 	if (ia->ia_addr != MFP_ADDR)
     81 		return (0);
     82 	if (ia->ia_intr != MFP_INTR)
     83 		return (0);
     84 
     85 	return (1);
     86 }
     87 
     88 
     89 static void
     90 mfp_attach(device_t parent, device_t self, void *aux)
     91 {
     92 	struct mfp_softc *sc = device_private(self);
     93 	struct intio_attach_args *ia = aux;
     94 	int r __diagused;
     95 
     96 	aprint_normal("\n");
     97 	mfp_attached = 1;
     98 
     99 	/* mfp_init() is already called via early config_console() */
    100 	sc->sc_bst = ia->ia_bst;
    101 	sc->sc_intr = ia->ia_intr;
    102 	ia->ia_size = 0x30;
    103 	r = intio_map_allocate_region(parent, ia, INTIO_MAP_ALLOCATE);
    104 #ifdef DIAGNOSTIC
    105 	if (r)
    106 		panic("IO map for MFP corruption??");
    107 #endif
    108 	bus_space_map(ia->ia_bst, ia->ia_addr, 0x2000, 0, &sc->sc_bht);
    109 	config_search(self, NULL,
    110 	    CFARGS(.search = mfp_search));
    111 }
    112 
    113 static int
    114 mfp_search(device_t parent, cfdata_t cf, const int *loc, void *aux)
    115 {
    116 	if (config_probe(parent, cf, __UNCONST(cf->cf_name)))
    117 		config_attach(parent, cf, __UNCONST(cf->cf_name), NULL,
    118 		    CFARGS_NONE);
    119 	return 0;
    120 }
    121 
    122 void
    123 mfp_config_console(void)
    124 {
    125 	mfp_init();
    126 	mfp_calibrate_delay();
    127 }
    128 
    129 static void
    130 mfp_init(void)
    131 {
    132 	mfp_set_vr(MFP_INTR);
    133 
    134 	/* stop all interrupts */
    135 	mfp_set_iera(0);
    136 	mfp_set_ierb(0);
    137 
    138 	/* make MSCTRL 'High', XXX where should I do it? */
    139 	mfp_send_usart(0x41);
    140 
    141 	/* Timer A settings */
    142 	mfp_set_tacr(MFP_TIMERA_RESET | MFP_TIMERA_STOP);
    143 
    144 	/* Timer B settings: used for USART clock */
    145 	mfp_set_tbcr(MFP_TIMERB_RESET | MFP_TIMERB_STOP);
    146 
    147 	/* Timer C/D settings */
    148 	mfp_set_tcdcr(0);
    149 }
    150 
    151 extern int delay_divisor;
    152 
    153 static void
    154 mfp_calibrate_delay(void)
    155 {
    156 	/*
    157 	 * See delay_divisor_est() definition and recommendation to
    158 	 * assume a bit slower than you'll actually see.
    159 	 */
    160 	for (delay_divisor = delay_divisor_est(delay_calibration_weight(16));
    161 	     delay_divisor > 0; delay_divisor--) {
    162 		mfp_set_tcdr(255-0);
    163 		mfp_set_tcdcr(0x70); /* 1/200 delay mode */
    164 		delay(10000);
    165 		mfp_set_tcdcr(0); /* stop timer */
    166 		if ((255 - mfp_get_tcdr()) > 200)
    167 			break;	/* got it! */
    168 		/* retry! */
    169 	}
    170 }
    171 
    172 /*
    173  * MFP utility functions
    174  */
    175 
    176 /*
    177  * wait for built-in display hsync.
    178  * should be called before writing to frame buffer.
    179  * might be called before realconfig.
    180  */
    181 void
    182 mfp_wait_for_hsync(void)
    183 {
    184 	/* wait for CRT HSYNC */
    185 	while (mfp_get_gpip() & MFP_GPIP_HSYNC)
    186 		__asm("nop");
    187 	while (!(mfp_get_gpip() & MFP_GPIP_HSYNC))
    188 		__asm("nop");
    189 }
    190 
    191 /*
    192  * send COMMAND to the MFP USART.
    193  * USART is attached to the keyboard.
    194  * might be called before realconfig.
    195  */
    196 int
    197 mfp_send_usart(int command)
    198 {
    199 	while (!(mfp_get_tsr() & MFP_TSR_BE));
    200 	mfp_set_udr(command);
    201 
    202 	return 0;
    203 }
    204 
    205 int
    206 mfp_receive_usart(void)
    207 {
    208 	while (!(mfp_get_rsr() & MFP_RSR_BF))
    209 		__asm("nop");
    210 	return mfp_get_udr();
    211 }
    212