Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / x68k / dev / mfp.c
blobf04d1db5daa5fd22453655dfb6384a7cb7c4e5c8
1 /* $NetBSD: mfp.c,v 1.22 2009/01/17 09:20:46 isaki Exp $ */
3 /*-
4 * Copyright (c) 1998 NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
36 * MC68901 MFP (multi function periferal) driver for NetBSD/x68k
40 * MFP is used as keyboard controller, which may be used before
41 * ordinary initialization.
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: mfp.c,v 1.22 2009/01/17 09:20:46 isaki Exp $");
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/device.h>
50 #include <sys/malloc.h>
52 #include <machine/bus.h>
53 #include <machine/cpu.h>
54 #include <machine/autoconf.h>
56 #include <arch/x68k/dev/intiovar.h>
57 #include <arch/x68k/dev/mfp.h>
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);
65 CFATTACH_DECL_NEW(mfp, sizeof(struct mfp_softc),
66 mfp_match, mfp_attach, NULL, NULL);
68 static int mfp_attached;
70 static int
71 mfp_match(device_t parent, cfdata_t cf, void *aux)
73 struct intio_attach_args *ia = aux;
75 /* mfp0 */
76 if (strcmp(ia->ia_name, "mfp") != 0)
77 return 0;
78 if (mfp_attached)
79 return (0);
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;
86 /* fixed address */
87 if (ia->ia_addr != MFP_ADDR)
88 return (0);
89 if (ia->ia_intr != MFP_INTR)
90 return (0);
92 return (1);
96 static void
97 mfp_attach(device_t parent, device_t self, void *aux)
99 struct mfp_softc *sc = device_private(self);
100 struct intio_attach_args *ia = aux;
101 int r;
103 aprint_normal("\n");
104 mfp_attached = 1;
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);
119 static int
120 mfp_search(device_t parent, cfdata_t cf, const int *loc, void *aux)
122 if (config_match(parent, cf, __UNCONST(cf->cf_name)) > 0)
123 config_attach(parent, cf, __UNCONST(cf->cf_name), NULL);
124 return 0;
127 void
128 mfp_config_console(void)
130 mfp_init();
131 mfp_calibrate_delay();
134 static void
135 mfp_init(void)
137 mfp_set_vr(MFP_INTR);
139 /* stop all interrupts */
140 mfp_set_iera(0);
141 mfp_set_ierb(0);
143 /* make MSCTRL 'High', XXX where should I do it? */
144 mfp_send_usart(0x41);
146 /* Timer A settings */
147 mfp_set_tacr(MFP_TIMERA_RESET | MFP_TIMERA_STOP);
149 /* Timer B settings: used for USART clock */
150 mfp_set_tbcr(MFP_TIMERB_RESET | MFP_TIMERB_STOP);
152 /* Timer C/D settings */
153 mfp_set_tcdcr(0);
156 extern int delay_divisor;
157 void _delay(u_int);
159 static void
160 mfp_calibrate_delay(void)
163 * Stolen from mvme68k.
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.
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! */
183 * MFP utility functions
187 * wait for built-in display hsync.
188 * should be called before writing to frame buffer.
189 * might be called before realconfig.
191 void
192 mfp_wait_for_hsync(void)
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");
202 * send COMMAND to the MFP USART.
203 * USART is attached to the keyboard.
204 * might be called before realconfig.
207 mfp_send_usart(int command)
209 while (!(mfp_get_tsr() & MFP_TSR_BE));
210 mfp_set_udr(command);
212 return 0;
216 mfp_receive_usart(void)
218 while (!(mfp_get_rsr() & MFP_RSR_BF))
219 __asm("nop");
220 return mfp_get_udr();