xref: /netbsd-src/sys/arch/sgimips/hpc/pckbc_hpc.c (revision 61a804956d7d0e375a57722c6e8a9e9922d17294)
1*61a80495Sthorpej /* $NetBSD: pckbc_hpc.c,v 1.11 2020/11/21 17:18:31 thorpej Exp $	 */
2b26a57f6Ssekiya 
3b26a57f6Ssekiya /*
4b26a57f6Ssekiya  * Copyright (c) 2003 Christopher SEKIYA
5b26a57f6Ssekiya  * All rights reserved.
6b26a57f6Ssekiya  *
7b26a57f6Ssekiya  * Redistribution and use in source and binary forms, with or without
8b26a57f6Ssekiya  * modification, are permitted provided that the following conditions
9b26a57f6Ssekiya  * are met:
10b26a57f6Ssekiya  * 1. Redistributions of source code must retain the above copyright
11b26a57f6Ssekiya  *    notice, this list of conditions and the following disclaimer.
12b26a57f6Ssekiya  * 2. Redistributions in binary form must reproduce the above copyright
13b26a57f6Ssekiya  *    notice, this list of conditions and the following disclaimer in the
14b26a57f6Ssekiya  *    documentation and/or other materials provided with the distribution.
15b26a57f6Ssekiya  * 3. All advertising materials mentioning features or use of this software
16b26a57f6Ssekiya  *    must display the following acknowledgement:
17b26a57f6Ssekiya  *          This product includes software developed for the
1829c72c57Skeihan  *          NetBSD Project.  See http://www.NetBSD.org/ for
19b26a57f6Ssekiya  *          information about NetBSD.
20b26a57f6Ssekiya  * 4. The name of the author may not be used to endorse or promote products
21b26a57f6Ssekiya  *    derived from this software without specific prior written permission.
22b26a57f6Ssekiya  *
23b26a57f6Ssekiya  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24b26a57f6Ssekiya  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25b26a57f6Ssekiya  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26b26a57f6Ssekiya  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27b26a57f6Ssekiya  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28b26a57f6Ssekiya  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29b26a57f6Ssekiya  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30b26a57f6Ssekiya  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31b26a57f6Ssekiya  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32b26a57f6Ssekiya  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33b26a57f6Ssekiya  */
34b26a57f6Ssekiya 
35b26a57f6Ssekiya #include <sys/cdefs.h>
36*61a80495Sthorpej __KERNEL_RCSID(0, "$NetBSD: pckbc_hpc.c,v 1.11 2020/11/21 17:18:31 thorpej Exp $");
37b26a57f6Ssekiya 
38b26a57f6Ssekiya #include <sys/param.h>
39b26a57f6Ssekiya #include <sys/systm.h>
40b26a57f6Ssekiya #include <sys/kernel.h>
41b26a57f6Ssekiya #include <sys/proc.h>
42b26a57f6Ssekiya #include <sys/device.h>
43*61a80495Sthorpej #include <sys/kmem.h>
44b26a57f6Ssekiya #include <sys/errno.h>
45b26a57f6Ssekiya #include <sys/queue.h>
46c29520cbSad #include <sys/bus.h>
47b26a57f6Ssekiya 
48b26a57f6Ssekiya #include <machine/autoconf.h>
49b26a57f6Ssekiya #include <machine/machtype.h>
50b26a57f6Ssekiya 
51b26a57f6Ssekiya #include <dev/ic/i8042reg.h>
52b26a57f6Ssekiya #include <dev/ic/pckbcvar.h>
53b26a57f6Ssekiya 
54b26a57f6Ssekiya #include <sgimips/hpc/hpcreg.h>
55b26a57f6Ssekiya #include <sgimips/hpc/hpcvar.h>
56b26a57f6Ssekiya 
57b26a57f6Ssekiya struct pckbc_hpc_softc {
58b26a57f6Ssekiya 	struct pckbc_softc sc_pckbc;
59b26a57f6Ssekiya 
60b57c3a57Slonewolf 	int	sc_irq;
61b57c3a57Slonewolf 	int	sc_hasintr;
62b26a57f6Ssekiya };
63b26a57f6Ssekiya 
64a22d8d4aScube static int      pckbc_hpc_match(device_t, cfdata_t, void *);
65a22d8d4aScube static void     pckbc_hpc_attach(device_t, device_t, void *);
66b57c3a57Slonewolf static void     pckbc_hpc_intr_establish(struct pckbc_softc *, pckbc_slot_t);
67b26a57f6Ssekiya 
68a22d8d4aScube CFATTACH_DECL_NEW(pckbc_hpc, sizeof(struct pckbc_hpc_softc),
69b26a57f6Ssekiya 	      pckbc_hpc_match, pckbc_hpc_attach, NULL, NULL);
70b26a57f6Ssekiya 
71b26a57f6Ssekiya static int
pckbc_hpc_match(device_t parent,cfdata_t cf,void * aux)72a22d8d4aScube pckbc_hpc_match(device_t parent, cfdata_t cf, void *aux)
73b26a57f6Ssekiya {
74b26a57f6Ssekiya 	struct hpc_attach_args *ha = aux;
75b26a57f6Ssekiya 
76b26a57f6Ssekiya 	if (strcmp(ha->ha_name, cf->cf_name) == 0)
77b26a57f6Ssekiya 		return (1);
78b26a57f6Ssekiya 
79b26a57f6Ssekiya 	return (0);
80b26a57f6Ssekiya }
81b26a57f6Ssekiya 
82b26a57f6Ssekiya static void
pckbc_hpc_attach(device_t parent,device_t self,void * aux)83a22d8d4aScube pckbc_hpc_attach(device_t parent, device_t self, void *aux)
84b26a57f6Ssekiya {
85a22d8d4aScube 	struct pckbc_hpc_softc *msc = device_private(self);
86b26a57f6Ssekiya 	struct pckbc_softc *sc = &msc->sc_pckbc;
87b26a57f6Ssekiya 	struct hpc_attach_args *haa = aux;
88b26a57f6Ssekiya 	struct pckbc_internal *t;
89b26a57f6Ssekiya 	bus_space_handle_t ioh_d, ioh_c;
90b26a57f6Ssekiya 
91a22d8d4aScube 	sc->sc_dv = self;
92a22d8d4aScube 
93b57c3a57Slonewolf 	msc->sc_irq = haa->ha_irq;
94b57c3a57Slonewolf 
95b57c3a57Slonewolf 	msc->sc_hasintr = 0;
96b26a57f6Ssekiya 
97b26a57f6Ssekiya 	sc->intr_establish = pckbc_hpc_intr_establish;
98b26a57f6Ssekiya 
99b57c3a57Slonewolf 	/* XXX Ugly hack & kludge XXX */
100b57c3a57Slonewolf 	if (pckbc_is_console(haa->ha_st, MIPS_KSEG1_TO_PHYS(haa->ha_sh +
101b57c3a57Slonewolf 	    haa->ha_devoff))) {
102b57c3a57Slonewolf 		t = &pckbc_consdata;
103b57c3a57Slonewolf 		pckbc_console_attached = 1;
104b57c3a57Slonewolf 	} else {
105b26a57f6Ssekiya 		/* XXX should be bus_space_map() */
106b26a57f6Ssekiya 		if (bus_space_subregion(haa->ha_st, haa->ha_sh,
107b26a57f6Ssekiya 					haa->ha_devoff + KBDATAP, 1, &ioh_d) ||
108b26a57f6Ssekiya 		    bus_space_subregion(haa->ha_st, haa->ha_sh,
109b26a57f6Ssekiya 					haa->ha_devoff + KBCMDP, 1, &ioh_c))
110b57c3a57Slonewolf 			panic("pckbc_hpc_attach: couldn't map");
111b26a57f6Ssekiya 
112*61a80495Sthorpej 		t = kmem_zalloc(sizeof(struct pckbc_internal), KM_SLEEP);
113eb488f67Smacallan 		t->t_iot = hpc_memt;
114b26a57f6Ssekiya 		t->t_ioh_d = ioh_d;
115b26a57f6Ssekiya 		t->t_ioh_c = ioh_c;
116b26a57f6Ssekiya 		t->t_addr = haa->ha_sh;
117b26a57f6Ssekiya 		t->t_cmdbyte = KC8_CPU;	/* Enable ports */
11888ab7da9Sad 		callout_init(&t->t_cleanup, 0);
119b57c3a57Slonewolf 	}
120b26a57f6Ssekiya 
121b26a57f6Ssekiya 	t->t_sc = sc;
122b26a57f6Ssekiya 	sc->id = t;
123b26a57f6Ssekiya 
124a22d8d4aScube 	aprint_normal("\n");
125b26a57f6Ssekiya 
126b26a57f6Ssekiya 	/* Finish off the attach. */
127b26a57f6Ssekiya 	pckbc_attach(sc);
128b26a57f6Ssekiya }
129b26a57f6Ssekiya 
130b57c3a57Slonewolf static void
pckbc_hpc_intr_establish(struct pckbc_softc * sc,pckbc_slot_t slot)131b26a57f6Ssekiya pckbc_hpc_intr_establish(struct pckbc_softc * sc, pckbc_slot_t slot)
132b26a57f6Ssekiya {
133b57c3a57Slonewolf 	struct pckbc_hpc_softc *msc = (void *) sc;
134b26a57f6Ssekiya 
135b57c3a57Slonewolf 	if (msc->sc_hasintr)
136b57c3a57Slonewolf 		return;
137b57c3a57Slonewolf 
138b57c3a57Slonewolf 	if (cpu_intr_establish(msc->sc_irq, IPL_TTY, pckbcintr, sc) == NULL) {
139a22d8d4aScube 		aprint_error_dev(sc->sc_dv,
140a22d8d4aScube 		    "unable to establish interrupt for %s slot\n",
141a22d8d4aScube 		    pckbc_slot_names[slot]);
142b57c3a57Slonewolf 	} else {
143b57c3a57Slonewolf 		msc->sc_hasintr = 1;
144b57c3a57Slonewolf 	}
145b26a57f6Ssekiya }
146