mfp.c revision 1.13 1 /* $NetBSD: mfp.c,v 1.13 2005/01/17 14:06:14 minoura 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.13 2005/01/17 14:06:14 minoura 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_init ();
106
107 if (sc != NULL) {
108 /* realconfig */
109 int r;
110
111 printf ("\n");
112
113 mfp_attached = 1;
114 sc->sc_bst = ia->ia_bst;
115 sc->sc_intr = ia->ia_intr;
116 ia->ia_size = 0x30;
117 r = intio_map_allocate_region (parent, ia, INTIO_MAP_ALLOCATE);
118 #ifdef DIAGNOSTIC
119 if (r)
120 panic ("IO map for MFP corruption??");
121 #endif
122 bus_space_map(ia->ia_bst, ia->ia_addr, 0x2000, 0, &sc->sc_bht);
123 config_found (self, "kbd", NULL);
124 config_found (self, "clock", NULL);
125 config_found (self, "pow", NULL);
126 } else {
127 /*
128 * Called from config_console;
129 * calibrate the DELAY loop counter
130 */
131 mfp_calibrate_delay();
132 }
133 }
134
135 static void
136 mfp_init (void)
137 {
138 #if 0 /* done in x68k_init.c::intr_reset() */
139 mfp_set_vr(MFP_INTR);
140
141 /* stop all interrupts */
142 mfp_set_iera(0);
143 mfp_set_ierb(0);
144 #endif
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 __P((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 (command)
208 int command;
209 {
210 while (!(mfp_get_tsr() & MFP_TSR_BE));
211 mfp_set_udr(command);
212
213 return 0;
214 }
215
216 int
217 mfp_receive_usart(void)
218 {
219 while (!(mfp_get_rsr() & MFP_RSR_BF))
220 asm("nop");
221 return mfp_get_udr();
222 }
223