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