xref: /netbsd-src/sys/arch/x68k/dev/mfp.c (revision 21e37cc72a480a47828990a439cde7ac9ffaf0c6)
1 /*	$NetBSD: mfp.c,v 1.11 2004/01/04 16:19:44 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 controller, which may be used before
41  * ordinary initialization.
42  */
43 
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: mfp.c,v 1.11 2004/01/04 16:19:44 wiz Exp $");
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/device.h>
50 #include <sys/malloc.h>
51 
52 #include <machine/bus.h>
53 #include <machine/cpu.h>
54 
55 #include <arch/x68k/dev/intiovar.h>
56 #include <arch/x68k/dev/mfp.h>
57 
58 static int mfp_match __P((struct device *, struct cfdata *, void *));
59 static void mfp_attach __P((struct device *, struct device *, void *));
60 static void mfp_init __P((void));
61 static void mfp_calibrate_delay __P((void));
62 
63 CFATTACH_DECL(mfp, sizeof(struct mfp_softc),
64     mfp_match, mfp_attach, NULL, NULL);
65 
66 static int
67 mfp_match(parent, cf, aux)
68 	struct device *parent;
69 	struct cfdata *cf;
70 	void *aux;
71 {
72 	struct intio_attach_args *ia = aux;
73 
74 	/* mfp0 */
75 	if (strcmp (ia->ia_name, "mfp") != 0)
76 		return 0;
77 	if (cf->cf_unit != 0)
78 		return (0);
79 
80 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
81 		ia->ia_addr = MFP_ADDR;
82 	if (ia->ia_intr == INTIOCF_INTR_DEFAULT)
83 		ia->ia_addr = MFP_INTR;
84 
85 	/* fixed address */
86 	if (ia->ia_addr != MFP_ADDR)
87 		return (0);
88 	if (ia->ia_intr != MFP_INTR)
89 		return (0);
90 
91 	return (1);
92 }
93 
94 
95 static void
96 mfp_attach(parent, self, aux)
97 	struct device *parent, *self;
98 	void *aux;
99 {
100 	struct mfp_softc *sc = (struct mfp_softc *)self;
101 	struct intio_attach_args *ia = aux;
102 
103 	mfp_init ();
104 
105 	if (sc != NULL) {
106 		/* realconfig */
107 		int r;
108 
109 		printf ("\n");
110 
111 		sc->sc_bst = ia->ia_bst;
112 		sc->sc_intr = ia->ia_intr;
113 		ia->ia_size = 0x30;
114 		r = intio_map_allocate_region (parent, ia, INTIO_MAP_ALLOCATE);
115 #ifdef DIAGNOSTIC
116 		if (r)
117 			panic ("IO map for MFP corruption??");
118 #endif
119 		bus_space_map(ia->ia_bst, ia->ia_addr, 0x2000, 0, &sc->sc_bht);
120 		config_found (self, "kbd", NULL);
121 		config_found (self, "clock", NULL);
122 		config_found (self, "pow", NULL);
123 	} else {
124 		/*
125 		 * Called from config_console;
126 		 * calibrate the DELAY loop counter
127 		 */
128 		mfp_calibrate_delay();
129 	}
130 }
131 
132 static void
133 mfp_init (void)
134 {
135 #if 0				/* done in x68k_init.c::intr_reset() */
136 	mfp_set_vr(MFP_INTR);
137 
138 	/* stop all interrupts */
139 	mfp_set_iera(0);
140 	mfp_set_ierb(0);
141 #endif
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 __P((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 (command)
205 	int command;
206 {
207 	while (!(mfp_get_tsr() & MFP_TSR_BE));
208 	mfp_set_udr(command);
209 
210 	return 0;
211 }
212 
213 int
214 mfp_receive_usart(void)
215 {
216 	while (!(mfp_get_rsr() & MFP_RSR_BF))
217 		asm("nop");
218 	return mfp_get_udr();
219 }
220