1*fc256c4aSthorpej /* $NetBSD: pckbc_ebus.c,v 1.4 2021/01/04 14:48:51 thorpej Exp $ */
28803ac04Sjdc
38803ac04Sjdc /*
48803ac04Sjdc * Copyright (c) 2002 Valeriy E. Ushakov
58803ac04Sjdc * All rights reserved.
68803ac04Sjdc *
78803ac04Sjdc * Redistribution and use in source and binary forms, with or without
88803ac04Sjdc * modification, are permitted provided that the following conditions
98803ac04Sjdc * are met:
108803ac04Sjdc * 1. Redistributions of source code must retain the above copyright
118803ac04Sjdc * notice, this list of conditions and the following disclaimer.
128803ac04Sjdc * 2. Redistributions in binary form must reproduce the above copyright
138803ac04Sjdc * notice, this list of conditions and the following disclaimer in the
148803ac04Sjdc * documentation and/or other materials provided with the distribution.
158803ac04Sjdc * 3. The name of the author may not be used to endorse or promote products
168803ac04Sjdc * derived from this software without specific prior written permission
178803ac04Sjdc *
188803ac04Sjdc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
198803ac04Sjdc * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
208803ac04Sjdc * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
218803ac04Sjdc * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
228803ac04Sjdc * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
238803ac04Sjdc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
248803ac04Sjdc * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
258803ac04Sjdc * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
268803ac04Sjdc * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
278803ac04Sjdc * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
288803ac04Sjdc */
298803ac04Sjdc
308803ac04Sjdc #include <sys/cdefs.h>
31*fc256c4aSthorpej __KERNEL_RCSID(0, "$NetBSD: pckbc_ebus.c,v 1.4 2021/01/04 14:48:51 thorpej Exp $");
328803ac04Sjdc
338803ac04Sjdc #include <sys/param.h>
348803ac04Sjdc #include <sys/systm.h>
358803ac04Sjdc #include <sys/kernel.h>
368803ac04Sjdc #include <sys/device.h>
37*fc256c4aSthorpej #include <sys/kmem.h>
388803ac04Sjdc #include <sys/bus.h>
398803ac04Sjdc #include <sys/intr.h>
408803ac04Sjdc
418803ac04Sjdc #include <machine/autoconf.h>
428803ac04Sjdc
438803ac04Sjdc #include <dev/ic/i8042reg.h>
448803ac04Sjdc #include <dev/ic/pckbcvar.h>
458803ac04Sjdc #include <dev/pckbport/pckbportvar.h>
468803ac04Sjdc
478803ac04Sjdc #include <dev/ebus/ebusreg.h>
488803ac04Sjdc #include <dev/ebus/ebusvar.h>
498803ac04Sjdc
50e35ff9bdSmacallan #include "opt_tadpmu.h"
51e35ff9bdSmacallan #include <sparc64/dev/tadpmureg.h>
52e35ff9bdSmacallan #include <sparc64/dev/tadpmuvar.h>
53e35ff9bdSmacallan
548803ac04Sjdc struct pckbc_ebus_softc {
558803ac04Sjdc struct pckbc_softc psc_pckbc; /* real "pckbc" softc */
56e35ff9bdSmacallan uint32_t psc_intr[5]; /* Tadpole Viper's pckbc has 3 slots */
578803ac04Sjdc };
588803ac04Sjdc
598803ac04Sjdc static int pckbc_ebus_match(device_t, cfdata_t, void *);
608803ac04Sjdc static void pckbc_ebus_attach(device_t, device_t, void *);
618803ac04Sjdc
628803ac04Sjdc static void pckbc_ebus_intr_establish(struct pckbc_softc *, pckbport_slot_t);
638803ac04Sjdc
648803ac04Sjdc #define PCKBC_PROM_DEVICE_NAME "8042"
653a8c4b6cSnakayama #define PCKBC_PROM_DEVICE_NAME2 "kb_ps2"
668803ac04Sjdc
678803ac04Sjdc CFATTACH_DECL_NEW(pckbc_ebus, sizeof(struct pckbc_ebus_softc),
688803ac04Sjdc pckbc_ebus_match, pckbc_ebus_attach, NULL, NULL);
698803ac04Sjdc
708803ac04Sjdc
718803ac04Sjdc static int
pckbc_ebus_match(device_t parent,cfdata_t cf,void * aux)728803ac04Sjdc pckbc_ebus_match(device_t parent, cfdata_t cf, void *aux)
738803ac04Sjdc {
748803ac04Sjdc struct ebus_attach_args *ea = aux;
758803ac04Sjdc
763a8c4b6cSnakayama if (strcmp(ea->ea_name, PCKBC_PROM_DEVICE_NAME) == 0 ||
773a8c4b6cSnakayama strcmp(ea->ea_name, PCKBC_PROM_DEVICE_NAME2) == 0)
783a8c4b6cSnakayama return 1;
793a8c4b6cSnakayama return 0;
808803ac04Sjdc }
818803ac04Sjdc
828803ac04Sjdc static void
pckbc_ebus_attach(device_t parent,device_t self,void * aux)838803ac04Sjdc pckbc_ebus_attach(device_t parent, device_t self, void *aux)
848803ac04Sjdc {
858803ac04Sjdc struct pckbc_ebus_softc *sc = device_private(self);
868803ac04Sjdc struct pckbc_softc *psc = (struct pckbc_softc *) sc;
878803ac04Sjdc struct ebus_attach_args *ea = aux;
888803ac04Sjdc struct pckbc_internal *t;
898803ac04Sjdc bus_space_tag_t iot;
908803ac04Sjdc bus_addr_t ioaddr;
918803ac04Sjdc int stdin_node, node;
928803ac04Sjdc int isconsole, i;
938803ac04Sjdc
948803ac04Sjdc psc->sc_dv = self;
958803ac04Sjdc iot = ea->ea_bustag;
968803ac04Sjdc ioaddr = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
978803ac04Sjdc
988803ac04Sjdc stdin_node = prom_instance_to_package(prom_stdin());
998803ac04Sjdc isconsole = 0;
1008803ac04Sjdc for (node = prom_firstchild(ea->ea_node);
1018803ac04Sjdc node != 0; node = prom_nextsibling(node))
1028803ac04Sjdc if (node == stdin_node) {
1038803ac04Sjdc isconsole = 1;
1048803ac04Sjdc break;
1058803ac04Sjdc }
1068803ac04Sjdc
1078803ac04Sjdc psc->intr_establish = pckbc_ebus_intr_establish;
1088803ac04Sjdc
1093a8c4b6cSnakayama if (ea->ea_nintr < PCKBC_NSLOTS) {
1103a8c4b6cSnakayama aprint_error(": no intr %d", ea->ea_nintr);
1113a8c4b6cSnakayama
1123a8c4b6cSnakayama /*
1133a8c4b6cSnakayama * XXX OpenBIOS doesn't provide interrupts for pckbc
1143a8c4b6cSnakayama * currently, so use the interrupt numbers described in
1153a8c4b6cSnakayama * QEMU's hw/sparc64/sun4u.c::isa_irq_handler.
1163a8c4b6cSnakayama */
1173a8c4b6cSnakayama if (strcmp(machine_model, "OpenBiosTeam,OpenBIOS") == 0) {
1183a8c4b6cSnakayama sc->psc_intr[PCKBC_KBD_SLOT] = 0x29;
1193a8c4b6cSnakayama sc->psc_intr[PCKBC_AUX_SLOT] = 0x2a;
1203a8c4b6cSnakayama } else {
1213a8c4b6cSnakayama aprint_error("\n");
1223a8c4b6cSnakayama return;
1233a8c4b6cSnakayama }
1243a8c4b6cSnakayama } else {
125e35ff9bdSmacallan for (i = 0; i < ea->ea_nintr; i++)
1268803ac04Sjdc sc->psc_intr[i] = ea->ea_intr[i];
1273a8c4b6cSnakayama }
1288803ac04Sjdc
1298803ac04Sjdc if (isconsole) {
1308803ac04Sjdc int status;
1318803ac04Sjdc
1328803ac04Sjdc status = pckbc_cnattach(iot, ioaddr, KBCMDP, 0,
1338803ac04Sjdc PCKBC_NEED_AUXWRITE | PCKBC_CANT_TRANSLATE);
1348803ac04Sjdc if (status == 0)
1358803ac04Sjdc aprint_normal(": cnattach ok");
1368803ac04Sjdc else
1378803ac04Sjdc aprint_error(": cnattach %d", status);
1388803ac04Sjdc }
1398803ac04Sjdc
1408803ac04Sjdc if (pckbc_is_console(iot, ioaddr)) {
1418803ac04Sjdc t = &pckbc_consdata;
1428803ac04Sjdc pckbc_console_attached = 1;
1438803ac04Sjdc } else {
1448803ac04Sjdc bus_space_handle_t ioh_d, ioh_c;
1458803ac04Sjdc
1468803ac04Sjdc if (bus_space_map(iot, ioaddr + KBDATAP, 1, 0, &ioh_d) != 0) {
1478803ac04Sjdc aprint_error(": unable to map data register\n");
1488803ac04Sjdc return;
1498803ac04Sjdc }
1508803ac04Sjdc
1518803ac04Sjdc if (bus_space_map(iot, ioaddr + KBCMDP, 1, 0, &ioh_c) != 0) {
1528803ac04Sjdc bus_space_unmap(iot, ioh_d, 1);
1538803ac04Sjdc aprint_error(": unable to map cmd register\n");
1548803ac04Sjdc return;
1558803ac04Sjdc }
1568803ac04Sjdc
157*fc256c4aSthorpej t = kmem_zalloc(sizeof(struct pckbc_internal), KM_SLEEP);
1588803ac04Sjdc t->t_iot = iot;
1598803ac04Sjdc t->t_ioh_d = ioh_d;
1608803ac04Sjdc t->t_ioh_c = ioh_c;
1618803ac04Sjdc t->t_addr = ioaddr;
1628803ac04Sjdc t->t_cmdbyte = KC8_CPU; /* initial command: enable ports */
1638803ac04Sjdc callout_init(&t->t_cleanup, 0);
1648803ac04Sjdc
1658803ac04Sjdc (void) pckbc_poll_data1(t, PCKBC_KBD_SLOT); /* flush */
1668803ac04Sjdc
1678803ac04Sjdc if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0)
1688803ac04Sjdc aprint_error(": unable to request self test");
1698803ac04Sjdc else {
1708803ac04Sjdc int response;
1718803ac04Sjdc
1728803ac04Sjdc response = pckbc_poll_data1(t, PCKBC_KBD_SLOT);
1738803ac04Sjdc if (response == 0x55)
1748803ac04Sjdc aprint_normal(": selftest ok");
1758803ac04Sjdc else
1768803ac04Sjdc aprint_error(": selftest failed (0x%02x)",
1778803ac04Sjdc response);
1788803ac04Sjdc }
1798803ac04Sjdc }
1808803ac04Sjdc
1818803ac04Sjdc /* crosslink */
1828803ac04Sjdc t->t_sc = psc;
1838803ac04Sjdc psc->id = t;
1848803ac04Sjdc
1858803ac04Sjdc /* finish off the attach */
1868803ac04Sjdc aprint_normal("\n");
1878803ac04Sjdc pckbc_attach(psc);
188e35ff9bdSmacallan #ifdef HAVE_TADPMU
189e35ff9bdSmacallan /* now look for a tadpmu child device */
190e35ff9bdSmacallan char name[64], *p;
191e35ff9bdSmacallan int pmu = 0;
192e35ff9bdSmacallan for (node = prom_firstchild(ea->ea_node);
193e35ff9bdSmacallan node != 0; node = prom_nextsibling(node)) {
194e35ff9bdSmacallan if((p = prom_getpropstringA(node, "name", name, 64)) != NULL) {
195e35ff9bdSmacallan if (strcmp(name, "tadpmu") == 0) {
196e35ff9bdSmacallan pmu = node;
197e35ff9bdSmacallan break;
1988803ac04Sjdc }
199e35ff9bdSmacallan }
200e35ff9bdSmacallan }
201e35ff9bdSmacallan if (pmu != 0) {
202e35ff9bdSmacallan void *irq;
2038803ac04Sjdc
204e35ff9bdSmacallan bus_space_handle_t hcmd, hdata;
205e35ff9bdSmacallan if (bus_space_map(iot, ioaddr + TADPMU_CMD, 1, 0, &hcmd) != 0) {
206e35ff9bdSmacallan bus_space_unmap(iot, hcmd, 1);
207e35ff9bdSmacallan aprint_error(": unable to map PMU cmd register\n");
208e35ff9bdSmacallan return;
209e35ff9bdSmacallan }
210e35ff9bdSmacallan if (bus_space_map(iot, ioaddr + TADPMU_DATA, 1, 0, &hdata) != 0) {
211e35ff9bdSmacallan bus_space_unmap(iot, hdata, 1);
212e35ff9bdSmacallan aprint_error(": unable to map PMU data register\n");
213e35ff9bdSmacallan return;
214e35ff9bdSmacallan }
215e35ff9bdSmacallan tadpmu_init(iot, hcmd, hdata);
216e35ff9bdSmacallan irq = bus_intr_establish(iot, sc->psc_intr[2], IPL_TTY,
217e35ff9bdSmacallan tadpmu_intr, sc);
218e35ff9bdSmacallan if (irq == NULL) {
219e35ff9bdSmacallan aprint_error("failed to establish tadpmu interrupt\n");
220e35ff9bdSmacallan }
221e35ff9bdSmacallan }
222e35ff9bdSmacallan #endif
223e35ff9bdSmacallan }
2248803ac04Sjdc
2258803ac04Sjdc static void
pckbc_ebus_intr_establish(struct pckbc_softc * sc,pckbport_slot_t slot)2268803ac04Sjdc pckbc_ebus_intr_establish(struct pckbc_softc *sc, pckbport_slot_t slot)
2278803ac04Sjdc {
2288803ac04Sjdc struct pckbc_ebus_softc *psc = (struct pckbc_ebus_softc *)sc;
2298803ac04Sjdc void *res;
2308803ac04Sjdc
2318803ac04Sjdc /* We assume that interrupt order is the same as slot order. */
2328803ac04Sjdc res = bus_intr_establish(sc->id->t_iot, psc->psc_intr[slot],
2338803ac04Sjdc IPL_TTY, pckbcintr, sc);
2348803ac04Sjdc if (res == NULL)
2358803ac04Sjdc aprint_error_dev(sc->sc_dv,
2368803ac04Sjdc "unable to establish %s slot interrupt\n",
2378803ac04Sjdc pckbc_slot_names[slot]);
2388803ac04Sjdc
2398803ac04Sjdc return;
2408803ac04Sjdc }
241