1*f7864055Sthorpej /* $NetBSD: gftty_mainbus.c,v 1.2 2024/01/06 17:52:43 thorpej Exp $ */
2f83db12cSthorpej
3f83db12cSthorpej /*-
4*f7864055Sthorpej * Copyright (c) 2023, 2024 The NetBSD Foundation, Inc.
5f83db12cSthorpej * All rights reserved.
6f83db12cSthorpej *
7f83db12cSthorpej * This code is derived from software contributed to The NetBSD Foundation
8f83db12cSthorpej * by Jason R. Thorpe.
9f83db12cSthorpej *
10f83db12cSthorpej * Redistribution and use in source and binary forms, with or without
11f83db12cSthorpej * modification, are permitted provided that the following conditions
12f83db12cSthorpej * are met:
13f83db12cSthorpej * 1. Redistributions of source code must retain the above copyright
14f83db12cSthorpej * notice, this list of conditions and the following disclaimer.
15f83db12cSthorpej * 2. Redistributions in binary form must reproduce the above copyright
16f83db12cSthorpej * notice, this list of conditions and the following disclaimer in the
17f83db12cSthorpej * documentation and/or other materials provided with the distribution.
18f83db12cSthorpej *
19f83db12cSthorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20f83db12cSthorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21f83db12cSthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22f83db12cSthorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23f83db12cSthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24f83db12cSthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25f83db12cSthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26f83db12cSthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27f83db12cSthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28f83db12cSthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29f83db12cSthorpej * POSSIBILITY OF SUCH DAMAGE.
30f83db12cSthorpej */
31f83db12cSthorpej
32f83db12cSthorpej #include <sys/cdefs.h>
33*f7864055Sthorpej __KERNEL_RCSID(0, "$NetBSD: gftty_mainbus.c,v 1.2 2024/01/06 17:52:43 thorpej Exp $");
34f83db12cSthorpej
35f83db12cSthorpej #include <sys/types.h>
36f83db12cSthorpej #include <sys/bus.h>
37f83db12cSthorpej #include <sys/device.h>
38f83db12cSthorpej #include <sys/intr.h>
39f83db12cSthorpej #include <sys/systm.h>
40f83db12cSthorpej
41f83db12cSthorpej #include <virt68k/dev/mainbusvar.h>
42f83db12cSthorpej
43f83db12cSthorpej #include <dev/goldfish/gfttyvar.h>
44f83db12cSthorpej
45f83db12cSthorpej static const struct device_compatible_entry compat_data[] = {
46f83db12cSthorpej { .compat = "google,goldfish-tty" },
47f83db12cSthorpej DEVICE_COMPAT_EOL
48f83db12cSthorpej };
49f83db12cSthorpej
50f83db12cSthorpej
51f83db12cSthorpej static int
gftty_mainbus_match(device_t parent,cfdata_t cf,void * aux)52f83db12cSthorpej gftty_mainbus_match(device_t parent, cfdata_t cf, void *aux)
53f83db12cSthorpej {
54f83db12cSthorpej struct mainbus_attach_args * const ma = aux;
55f83db12cSthorpej
56f83db12cSthorpej return mainbus_compatible_match(ma, compat_data);
57f83db12cSthorpej }
58f83db12cSthorpej
59f83db12cSthorpej static void
gftty_mainbus_attach(device_t parent,device_t self,void * aux)60f83db12cSthorpej gftty_mainbus_attach(device_t parent, device_t self, void *aux)
61f83db12cSthorpej {
62f83db12cSthorpej struct gftty_softc * const sc = device_private(self);
63f83db12cSthorpej struct mainbus_attach_args * const ma = aux;
64*f7864055Sthorpej char strbuf[INTR_STRING_BUFSIZE];
65f83db12cSthorpej
66f83db12cSthorpej sc->sc_dev = self;
67f83db12cSthorpej
68f83db12cSthorpej if (! gftty_is_console(sc)) {
69f83db12cSthorpej bus_space_handle_t bsh;
70f83db12cSthorpej
71f83db12cSthorpej if (bus_space_map(ma->ma_st, ma->ma_addr, ma->ma_size, 0,
72f83db12cSthorpej &bsh) != 0) {
73f83db12cSthorpej aprint_error(": couldn't map registers\n");
74f83db12cSthorpej return;
75f83db12cSthorpej }
76f83db12cSthorpej gftty_alloc_config(sc, ma->ma_st, bsh);
77f83db12cSthorpej }
78f83db12cSthorpej
79*f7864055Sthorpej sc->sc_dmat = ma->ma_dmat;
80*f7864055Sthorpej
81f83db12cSthorpej gftty_attach(sc);
82*f7864055Sthorpej if (sc->sc_tty != NULL) {
83*f7864055Sthorpej /* Attach was successful. Set up interrupt. */
84*f7864055Sthorpej sc->sc_ih = intr_establish(gftty_intr, sc,
85*f7864055Sthorpej ma->ma_irq, IPL_TTY, 0);
86*f7864055Sthorpej if (sc->sc_ih == NULL) {
87*f7864055Sthorpej aprint_error_dev(self,
88*f7864055Sthorpej "couldn't install interrupt handler\n");
89*f7864055Sthorpej return;
90*f7864055Sthorpej }
91*f7864055Sthorpej aprint_normal_dev(self, "interrupting at %s\n",
92*f7864055Sthorpej intr_string(sc->sc_ih, strbuf, sizeof(strbuf)));
93*f7864055Sthorpej }
94f83db12cSthorpej }
95f83db12cSthorpej
96f83db12cSthorpej CFATTACH_DECL_NEW(gftty_mainbus, sizeof(struct gftty_softc),
97f83db12cSthorpej gftty_mainbus_match, gftty_mainbus_attach, NULL, NULL);
98