1 /* $NetBSD: com_ebus.c,v 1.17 2018/12/08 17:46:13 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: com_ebus.c,v 1.17 2018/12/08 17:46:13 thorpej Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 #include <sys/termios.h>
39
40 #include <sys/bus.h>
41 #include <machine/autoconf.h>
42 #include <machine/intr.h>
43
44 #include <dev/ebus/ebusreg.h>
45 #include <dev/ebus/ebusvar.h>
46
47 #include <dev/ic/comreg.h>
48 #include <dev/ic/comvar.h>
49
50 struct com_ebus_softc {
51 struct com_softc ebsc_com; /* real "com" softc */
52
53 /* this space for rent */
54 };
55
56 static int com_ebus_match(device_t, cfdata_t , void *);
57 static void com_ebus_attach(device_t, device_t, void *);
58
59 CFATTACH_DECL_NEW(com_ebus, sizeof(struct com_ebus_softc),
60 com_ebus_match, com_ebus_attach, NULL, NULL);
61
62 static int
com_ebus_match(device_t parent,cfdata_t cf,void * aux)63 com_ebus_match(device_t parent, cfdata_t cf, void *aux)
64 {
65 struct ebus_attach_args *ea = aux;
66 bus_space_handle_t ioh;
67 int match;
68
69 if (strcmp(ea->ea_name, "su") != 0)
70 return (0);
71
72 match = 0;
73 if (bus_space_map(ea->ea_bustag, EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
74 ea->ea_reg[0].size, 0, &ioh) == 0)
75 {
76 match = comprobe1(ea->ea_bustag, ioh);
77 bus_space_unmap(ea->ea_bustag, ioh, ea->ea_reg[0].size);
78 }
79
80 return (match);
81 }
82
83 static void
com_ebus_attach(device_t parent,device_t self,void * aux)84 com_ebus_attach(device_t parent, device_t self, void *aux)
85 {
86 struct com_ebus_softc *ebsc = device_private(self);
87 struct com_softc *sc = &ebsc->ebsc_com;
88 struct ebus_attach_args *ea = aux;
89 bus_space_tag_t iot;
90 bus_space_handle_t ioh;
91 bus_addr_t iobase;
92
93 sc->sc_dev = self;
94 iot = ea->ea_bustag;
95 iobase = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
96 sc->sc_frequency = COM_FREQ;
97 sc->sc_hwflags = COM_HW_NO_TXPRELOAD;
98
99 /*
100 * XXX: It would be nice to be able to split console input and
101 * output to different devices. For now switch to serial
102 * console if PROM stdin is on serial (so that we can use DDB).
103 */
104 if (prom_instance_to_package(prom_stdin()) == ea->ea_node)
105 comcnattach(iot, iobase, B9600, sc->sc_frequency,
106 COM_TYPE_NORMAL, (CLOCAL | CREAD | CS8));
107
108 if (!com_is_console(iot, iobase, &ioh)
109 && bus_space_map(iot, iobase, ea->ea_reg[0].size,
110 0, &ioh) != 0)
111 {
112 aprint_error(": unable to map device registers\n");
113 return;
114 }
115
116 com_init_regs(&sc->sc_regs, iot, ioh, iobase);
117
118 com_attach_subr(sc);
119
120 if (ea->ea_nintr != 0)
121 (void)bus_intr_establish(iot, ea->ea_intr[0], IPL_SERIAL,
122 comintr, sc);
123 }
124