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