xref: /netbsd-src/sys/arch/x68k/dev/mfp.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: mfp.c,v 1.21 2008/12/31 08:00:31 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.21 2008/12/31 08:00:31 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 
55 #include <arch/x68k/dev/intiovar.h>
56 #include <arch/x68k/dev/mfp.h>
57 
58 static int mfp_match(struct device *, struct cfdata *, void *);
59 static void mfp_attach(struct device *, struct device *, void *);
60 static int  mfp_search(device_t, cfdata_t, const int *, void *);
61 static void mfp_init(void);
62 static void mfp_calibrate_delay(void);
63 
64 CFATTACH_DECL(mfp, sizeof(struct mfp_softc),
65     mfp_match, mfp_attach, NULL, NULL);
66 
67 static int mfp_attached;
68 
69 static int
70 mfp_match(struct device *parent, struct cfdata *cf, 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 (mfp_attached)
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(struct device *parent, struct device *self, void *aux)
97 {
98 	struct mfp_softc *sc = (struct mfp_softc *)self;
99 	struct intio_attach_args *ia = aux;
100 
101 	mfp_init();
102 
103 	if (sc != NULL) {
104 		/* realconfig */
105 		int r;
106 
107 		printf("\n");
108 
109 		mfp_attached = 1;
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_search_ia(mfp_search, self, "mfp", 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 int
130 mfp_search(device_t parent, cfdata_t cf, const int *loc, void *aux)
131 {
132 	if (config_match(parent, cf, __UNCONST(cf->cf_name)) > 0)
133 		config_attach(parent, cf, __UNCONST(cf->cf_name), NULL);
134 	return 0;
135 }
136 
137 static void
138 mfp_init(void)
139 {
140 	mfp_set_vr(MFP_INTR);
141 
142 	/* stop all interrupts */
143 	mfp_set_iera(0);
144 	mfp_set_ierb(0);
145 
146 	/* make MSCTRL 'High', XXX where should I do it? */
147 	mfp_send_usart(0x41);
148 
149 	/* Timer A settings */
150 	mfp_set_tacr(MFP_TIMERA_RESET | MFP_TIMERA_STOP);
151 
152 	/* Timer B settings: used for USART clock */
153 	mfp_set_tbcr(MFP_TIMERB_RESET | MFP_TIMERB_STOP);
154 
155 	/* Timer C/D settings */
156 	mfp_set_tcdcr(0);
157 }
158 
159 extern int delay_divisor;
160 void	_delay(u_int);
161 
162 static void
163 mfp_calibrate_delay(void)
164 {
165 	/*
166 	 * Stolen from mvme68k.
167 	 */
168 	/*
169 	 * X68k provides 4MHz clock (= 0.25usec) for MFP timer C.
170 	 * 10000usec = 0.25usec * 200 * 200
171 	 * Our slowest clock is 20MHz (?).  Its delay_divisor value
172 	 * should be about 102.  Start from 140 here.
173 	 */
174 	for (delay_divisor = 140; delay_divisor > 0; delay_divisor--) {
175 		mfp_set_tcdr(255-0);
176 		mfp_set_tcdcr(0x70); /* 1/200 delay mode */
177 		_delay(10000 << 8);
178 		mfp_set_tcdcr(0); /* stop timer */
179 		if ((255 - mfp_get_tcdr()) > 200)
180 			break;	/* got it! */
181 		/* retry! */
182 	}
183 }
184 
185 /*
186  * MFP utility functions
187  */
188 
189 /*
190  * wait for built-in display hsync.
191  * should be called before writing to frame buffer.
192  * might be called before realconfig.
193  */
194 void
195 mfp_wait_for_hsync(void)
196 {
197 	/* wait for CRT HSYNC */
198 	while (mfp_get_gpip() & MFP_GPIP_HSYNC)
199 		__asm("nop");
200 	while (!(mfp_get_gpip() & MFP_GPIP_HSYNC))
201 		__asm("nop");
202 }
203 
204 /*
205  * send COMMAND to the MFP USART.
206  * USART is attached to the keyboard.
207  * might be called before realconfig.
208  */
209 int
210 mfp_send_usart(int command)
211 {
212 	while (!(mfp_get_tsr() & MFP_TSR_BE));
213 	mfp_set_udr(command);
214 
215 	return 0;
216 }
217 
218 int
219 mfp_receive_usart(void)
220 {
221 	while (!(mfp_get_rsr() & MFP_RSR_BF))
222 		__asm("nop");
223 	return mfp_get_udr();
224 }
225