xref: /netbsd-src/sys/arch/x68k/dev/mfp.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: mfp.c,v 1.9 2002/10/02 16:02:41 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  * 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 CFATTACH_DECL(mfp, sizeof(struct mfp_softc),
61     mfp_match, mfp_attach, NULL, NULL);
62 
63 static int
64 mfp_match(parent, cf, aux)
65 	struct device *parent;
66 	struct cfdata *cf;
67 	void *aux;
68 {
69 	struct intio_attach_args *ia = aux;
70 
71 	/* mfp0 */
72 	if (strcmp (ia->ia_name, "mfp") != 0)
73 		return 0;
74 	if (cf->cf_unit != 0)
75 		return (0);
76 
77 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
78 		ia->ia_addr = MFP_ADDR;
79 	if (ia->ia_intr == INTIOCF_INTR_DEFAULT)
80 		ia->ia_addr = MFP_INTR;
81 
82 	/* fixed address */
83 	if (ia->ia_addr != MFP_ADDR)
84 		return (0);
85 	if (ia->ia_intr != MFP_INTR)
86 		return (0);
87 
88 	return (1);
89 }
90 
91 
92 static void
93 mfp_attach(parent, self, aux)
94 	struct device *parent, *self;
95 	void *aux;
96 {
97 	struct mfp_softc *sc = (struct mfp_softc *)self;
98 	struct intio_attach_args *ia = aux;
99 
100 	mfp_init ();
101 
102 	if (sc != NULL) {
103 		/* realconfig */
104 		int r;
105 
106 		printf ("\n");
107 
108 		sc->sc_bst = ia->ia_bst;
109 		sc->sc_intr = ia->ia_intr;
110 		ia->ia_size = 0x30;
111 		r = intio_map_allocate_region (parent, ia, INTIO_MAP_ALLOCATE);
112 #ifdef DIAGNOSTIC
113 		if (r)
114 			panic ("IO map for MFP corruption??");
115 #endif
116 		bus_space_map(ia->ia_bst, ia->ia_addr, 0x2000, 0, &sc->sc_bht);
117 		config_found (self, "kbd", NULL);
118 		config_found (self, "clock", NULL);
119 		config_found (self, "pow", NULL);
120 	} else {
121 		/*
122 		 * Called from config_console;
123 		 * calibrate the DELAY loop counter
124 		 */
125 		mfp_calibrate_delay();
126 	}
127 }
128 
129 static void
130 mfp_init (void)
131 {
132 #if 0				/* done in x68k_init.c::intr_reset() */
133 	mfp_set_vr(MFP_INTR);
134 
135 	/* stop all interrupts */
136 	mfp_set_iera(0);
137 	mfp_set_ierb(0);
138 #endif
139 
140 	/* Timer A settings */
141 	mfp_set_tacr(MFP_TIMERA_RESET | MFP_TIMERA_STOP);
142 
143 	/* Timer B settings: used for USART clock */
144 	mfp_set_tbcr(MFP_TIMERB_RESET | MFP_TIMERB_STOP);
145 
146 	/* Timer C/D settings */
147 	mfp_set_tcdcr(0);
148 }
149 
150 extern int delay_divisor;
151 void	_delay __P((u_int));
152 
153 static void
154 mfp_calibrate_delay(void)
155 {
156 	/*
157 	 * Stolen from mvme68k.
158 	 */
159 	/*
160 	 * X68k provides 4MHz clock (= 0.25usec) for MFP timer C.
161 	 * 10000usec = 0.25usec * 200 * 200
162 	 * Our slowest clock is 20MHz (?).  Its delay_divisor value
163 	 * should be about 102.  Start from 140 here.
164 	 */
165 	for (delay_divisor = 140; delay_divisor > 0; delay_divisor--) {
166 		mfp_set_tcdr(255-0);
167 		mfp_set_tcdcr(0x70); /* 1/200 delay mode */
168 		_delay(10000 << 8);
169 		mfp_set_tcdcr(0); /* stop timer */
170 		if ((255 - mfp_get_tcdr()) > 200)
171 			break;	/* got it! */
172 		/* retry! */
173 	}
174 }
175 
176 /*
177  * MFP utility functions
178  */
179 
180 /*
181  * wait for built-in display hsync.
182  * should be called before writing to frame buffer.
183  * might be called before realconfig.
184  */
185 void
186 mfp_wait_for_hsync (void)
187 {
188 	/* wait for CRT HSYNC */
189 	while (mfp_get_gpip() & MFP_GPIP_HSYNC)
190 		asm("nop");
191 	while (!(mfp_get_gpip() & MFP_GPIP_HSYNC))
192 		asm("nop");
193 }
194 
195 /*
196  * send COMMAND to the MFP USART.
197  * USART is attached to the keyboard.
198  * might be called before realconfig.
199  */
200 int
201 mfp_send_usart (command)
202 	int command;
203 {
204 	while (!(mfp_get_tsr() & MFP_TSR_BE));
205 	mfp_set_udr(command);
206 
207 	return 0;
208 }
209 
210 int
211 mfp_receive_usart(void)
212 {
213 	while (!(mfp_get_rsr() & MFP_RSR_BF))
214 		asm("nop");
215 	return mfp_get_udr();
216 }
217