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