1/*	$NetBSD: com_vrip.c,v 1.24 2022/07/08 07:02:47 skrll Exp $	*/
2
3/*-
4 * Copyright (c) 1999 SASAKI Takesi. All rights reserved.
5 * Copyright (c) 1999, 2002 PocketBSD Project. 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. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by the PocketBSD project
18 *	and its contributors.
19 * 4. Neither the name of the project nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 */
36
37#include <sys/cdefs.h>
38__KERNEL_RCSID(0, "$NetBSD: com_vrip.c,v 1.24 2022/07/08 07:02:47 skrll Exp $");
39
40#include "opt_kgdb.h"
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/device.h>
45#include <sys/reboot.h>
46
47#include <sys/termios.h>
48
49#include <machine/intr.h>
50#include <machine/bus.h>
51
52#include <machine/platid.h>
53#include <machine/platid_mask.h>
54#include <machine/config_hook.h>
55
56#include <hpcmips/vr/vr.h>
57#include <hpcmips/vr/vrcpudef.h>
58#include <hpcmips/vr/vripif.h>
59#include <hpcmips/vr/vripvar.h>
60#include <hpcmips/vr/cmureg.h>
61#include <hpcmips/vr/siureg.h>
62
63#include <dev/ic/comvar.h>
64#include <dev/ic/comreg.h>
65
66#include "opt_vr41xx.h"
67#include <hpcmips/vr/com_vripvar.h>
68
69#include "locators.h"
70
71#define COMVRIPDEBUG
72#ifdef COMVRIPDEBUG
73int	com_vrip_debug = 0;
74#define	DPRINTF(arg) if (com_vrip_debug) printf arg;
75#define	VPRINTF(arg) if (com_vrip_debug || bootverbose) printf arg;
76#else
77#define	DPRINTF(arg)
78#define	VPRINTF(arg) if (bootverbose) printf arg;
79#endif
80
81struct com_vrip_softc {
82	struct com_softc	sc_com;
83	int sc_pwctl;
84};
85
86static int com_vrip_probe(device_t, cfdata_t , void *);
87static void com_vrip_attach(device_t, device_t, void *);
88static int com_vrip_common_probe(bus_space_tag_t, int);
89
90void vrcmu_init(void);
91void vrcmu_supply(int);
92void vrcmu_mask(int);
93
94CFATTACH_DECL_NEW(com_vrip, sizeof(struct com_vrip_softc),
95    com_vrip_probe, com_vrip_attach, NULL, NULL);
96
97int
98com_vrip_cndb_attach(bus_space_tag_t iot, int iobase, int rate, int frequency,
99    tcflag_t cflag, int kgdb)
100{
101
102	if (!com_vrip_common_probe(iot, iobase))
103		return (EIO);	/* I can't find appropriate error number. */
104#ifdef KGDB
105	if (kgdb)
106		return (com_kgdb_attach(iot, iobase, rate, frequency,
107		    COM_TYPE_NORMAL, cflag));
108	else
109#endif
110		return (comcnattach(iot, iobase, rate, frequency,
111		    COM_TYPE_NORMAL, cflag));
112}
113
114static int
115com_vrip_common_probe(bus_space_tag_t iot, int iobase)
116{
117	bus_space_handle_t ioh;
118	int rv;
119
120	if (bus_space_map(iot, iobase, 1, 0, &ioh)) {
121		aprint_error(": can't map i/o space\n");
122		return 0;
123	}
124	rv = comprobe1(iot, ioh);
125	bus_space_unmap(iot, ioh, 1);
126	return (rv);
127}
128
129static int
130com_vrip_probe(device_t parent, cfdata_t cf, void *aux)
131{
132	struct vrip_attach_args *va = aux;
133	bus_space_tag_t iot = va->va_iot;
134	int rv;
135
136	DPRINTF(("==com_vrip_probe"));
137
138	if (va->va_addr == VRIPIFCF_ADDR_DEFAULT ||
139	    va->va_unit == VRIPIFCF_UNIT_DEFAULT) {
140		aprint_error(": need addr and intr.\n");
141		return (0);
142	}
143
144	vrip_power(va->va_vc, va->va_unit, 1);
145
146	if (com_is_console(iot, va->va_addr, 0)) {
147		/*
148		 *  We have already probed.
149		 */
150		rv = 1;
151	} else {
152		rv = com_vrip_common_probe(iot, va->va_addr);
153	}
154
155	DPRINTF((rv ? ": found COM ports\n" : ": can't probe COM device\n"));
156
157	if (rv) {
158		va->va_size = COM_NPORTS;
159	}
160	return (rv);
161}
162
163
164static void
165com_vrip_attach(device_t parent, device_t self, void *aux)
166{
167	struct com_vrip_softc *vsc = device_private(self);
168	struct com_softc *sc = &vsc->sc_com;
169	struct vrip_attach_args *va = aux;
170
171	bus_space_tag_t iot = va->va_iot;
172	bus_space_handle_t ioh;
173
174	sc->sc_dev = self;
175	vsc->sc_pwctl = device_cfdata(self)->cf_loc[VRIPIFCF_PWCTL];
176
177	DPRINTF(("==com_vrip_attach"));
178
179	if (bus_space_map(iot, va->va_addr, 1, 0, &ioh)) {
180		aprint_error(": can't map bus space\n");
181		return;
182	}
183
184	com_init_regs(&sc->sc_regs, iot, ioh, va->va_addr);
185
186	sc->enable = NULL; /* XXX: CMU control */
187	sc->disable = NULL;
188
189	sc->sc_frequency = VRCOM_FREQ;
190	/* Power management */
191	vrip_power(va->va_vc, va->va_unit, 1);
192	/* XXX, locale 'ID' must be need */
193	config_hook_call(CONFIG_HOOK_POWERCONTROL, vsc->sc_pwctl, (void*)1);
194
195	DPRINTF(("Try to attach com.\n"));
196	com_attach_subr(sc);
197
198	DPRINTF(("Establish intr"));
199	if (!vrip_intr_establish(va->va_vc, va->va_unit, 0, IPL_TTY,
200	    comintr, sc)) {
201		aprint_error_dev(self, "can't map interrupt line.\n");
202	}
203
204	DPRINTF((":return()"));
205	VPRINTF(("%s: pwctl %d\n", device_xname(self), vsc->sc_pwctl));
206}
207