xref: /netbsd-src/sys/arch/sparc64/dev/pckbc_ebus.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: pckbc_ebus.c,v 1.1 2012/10/09 20:55:04 jdc Exp $ */
2 
3 /*
4  * Copyright (c) 2002 Valeriy E. Ushakov
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. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: pckbc_ebus.c,v 1.1 2012/10/09 20:55:04 jdc Exp $");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/device.h>
37 #include <sys/malloc.h>
38 #include <sys/bus.h>
39 #include <sys/intr.h>
40 
41 #include <machine/autoconf.h>
42 
43 #include <dev/ic/i8042reg.h>
44 #include <dev/ic/pckbcvar.h>
45 #include <dev/pckbport/pckbportvar.h>
46 
47 #include <dev/ebus/ebusreg.h>
48 #include <dev/ebus/ebusvar.h>
49 
50 struct pckbc_ebus_softc {
51 	struct pckbc_softc psc_pckbc;	/* real "pckbc" softc */
52 	uint32_t psc_intr[PCKBC_NSLOTS];
53 };
54 
55 static int	pckbc_ebus_match(device_t, cfdata_t, void *);
56 static void	pckbc_ebus_attach(device_t, device_t, void *);
57 
58 static void	pckbc_ebus_intr_establish(struct pckbc_softc *, pckbport_slot_t);
59 
60 #define PCKBC_PROM_DEVICE_NAME "8042"
61 
62 CFATTACH_DECL_NEW(pckbc_ebus, sizeof(struct pckbc_ebus_softc),
63     pckbc_ebus_match, pckbc_ebus_attach, NULL, NULL);
64 
65 
66 static int
67 pckbc_ebus_match(device_t parent, cfdata_t cf, void *aux)
68 {
69 	struct ebus_attach_args *ea = aux;
70 
71 	return (strcmp(ea->ea_name, PCKBC_PROM_DEVICE_NAME) == 0);
72 }
73 
74 static void
75 pckbc_ebus_attach(device_t parent, device_t self, void *aux)
76 {
77 	struct pckbc_ebus_softc *sc = device_private(self);
78 	struct pckbc_softc *psc = (struct pckbc_softc *) sc;
79 	struct ebus_attach_args *ea = aux;
80 	struct pckbc_internal *t;
81 	bus_space_tag_t iot;
82 	bus_addr_t ioaddr;
83 	int stdin_node, node;
84 	int isconsole, i;
85 
86 	psc->sc_dv = self;
87 	iot = ea->ea_bustag;
88 	ioaddr = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
89 
90 	stdin_node = prom_instance_to_package(prom_stdin());
91 	isconsole = 0;
92 	for (node = prom_firstchild(ea->ea_node);
93 	     node != 0; node = prom_nextsibling(node))
94 		if (node == stdin_node) {
95 			isconsole = 1;
96 			break;
97 		}
98 
99 	psc->intr_establish = pckbc_ebus_intr_establish;
100 
101 	for (i = 0; i < PCKBC_NSLOTS; i++)
102 		sc->psc_intr[i] = ea->ea_intr[i];
103 
104 	if (isconsole) {
105 		int status;
106 
107 		status = pckbc_cnattach(iot, ioaddr, KBCMDP, 0,
108 		    PCKBC_NEED_AUXWRITE | PCKBC_CANT_TRANSLATE);
109 		if (status == 0)
110 			aprint_normal(": cnattach ok");
111 		else
112 			aprint_error(": cnattach %d", status);
113 	}
114 
115 	if (pckbc_is_console(iot, ioaddr)) {
116 		t = &pckbc_consdata;
117 		pckbc_console_attached = 1;
118 	} else {
119 		bus_space_handle_t ioh_d, ioh_c;
120 
121 		if (bus_space_map(iot, ioaddr + KBDATAP, 1, 0, &ioh_d) != 0) {
122 			aprint_error(": unable to map data register\n");
123 			return;
124 		}
125 
126 		if (bus_space_map(iot, ioaddr + KBCMDP,  1, 0, &ioh_c) != 0) {
127 			bus_space_unmap(iot, ioh_d, 1);
128 			aprint_error(": unable to map cmd register\n");
129 			return;
130 		}
131 
132 		t = malloc(sizeof(struct pckbc_internal), M_DEVBUF, M_WAITOK);
133 		memset(t, 0, sizeof(struct pckbc_internal));
134 		t->t_iot = iot;
135 		t->t_ioh_d = ioh_d;
136 		t->t_ioh_c = ioh_c;
137 		t->t_addr = ioaddr;
138 		t->t_cmdbyte = KC8_CPU; /* initial command: enable ports */
139 		callout_init(&t->t_cleanup, 0);
140 
141 		(void) pckbc_poll_data1(t, PCKBC_KBD_SLOT); /* flush */
142 
143 		if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0)
144 			aprint_error(": unable to request self test");
145 		else {
146 			int response;
147 
148 			response = pckbc_poll_data1(t, PCKBC_KBD_SLOT);
149 			if (response == 0x55)
150 				aprint_normal(": selftest ok");
151 			else
152 				aprint_error(": selftest failed (0x%02x)",
153 				    response);
154 		}
155 	}
156 
157 	/* crosslink */
158 	t->t_sc = psc;
159 	psc->id = t;
160 
161 	/* finish off the attach */
162 	aprint_normal("\n");
163 	pckbc_attach(psc);
164 }
165 
166 
167 static void
168 pckbc_ebus_intr_establish(struct pckbc_softc *sc, pckbport_slot_t slot)
169 {
170 	struct pckbc_ebus_softc *psc = (struct pckbc_ebus_softc *)sc;
171 	void *res;
172 
173 	/* We assume that interrupt order is the same as slot order. */
174 	res = bus_intr_establish(sc->id->t_iot, psc->psc_intr[slot],
175 				 IPL_TTY, pckbcintr, sc);
176 	if (res == NULL)
177 		aprint_error_dev(sc->sc_dv,
178 		    "unable to establish %s slot interrupt\n",
179 		    pckbc_slot_names[slot]);
180 
181 	return;
182 }
183