2 * Copyright (c) 2015-2017 Ruslan Bukin <br@bsdpad.com>
5 * Portions of this software were developed by SRI International and the
6 * University of Cambridge Computer Laboratory under DARPA/AFRL contract
7 * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Portions of this software were developed by the University of Cambridge
10 * Computer Laboratory as part of the CTSRD Project, with support from the
11 * UK Higher Education Innovation Fund (HEIF).
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
40 #include <sys/kernel.h>
42 #include <sys/systm.h>
43 #include <sys/types.h>
46 #include <sys/consio.h>
49 #include <sys/module.h>
52 #include <dev/ofw/openfirm.h>
58 #include <dev/fdt/fdt_common.h>
59 #include <dev/ofw/openfirm.h>
60 #include <dev/ofw/ofw_bus.h>
61 #include <dev/ofw/ofw_bus_subr.h>
63 #include <machine/bus.h>
64 #include <machine/cpu.h>
65 #include <machine/intr.h>
66 #include <machine/asm.h>
67 #include <machine/trap.h>
68 #include <machine/vmparam.h>
69 #include <machine/sbi.h>
73 struct resource *res[1];
78 /* CN Console interface */
80 static tsw_outwakeup_t riscvtty_outwakeup;
82 static struct ttydevsw riscv_ttydevsw = {
83 .tsw_flags = TF_NOPREFIX,
84 .tsw_outwakeup = riscvtty_outwakeup,
88 static struct callout riscv_callout;
89 static struct tty *tp = NULL;
92 static int alt_break_state;
95 static void riscv_timeout(void *);
97 static cn_probe_t riscv_cnprobe;
98 static cn_init_t riscv_cninit;
99 static cn_term_t riscv_cnterm;
100 static cn_getc_t riscv_cngetc;
101 static cn_putc_t riscv_cnputc;
102 static cn_grab_t riscv_cngrab;
103 static cn_ungrab_t riscv_cnungrab;
105 CONSOLE_DRIVER(riscv);
107 #define MAX_BURST_LEN 1
113 sbi_console_putchar(c);
117 early_putc_t *early_putc = riscv_putc;
121 cn_drvinit(void *unused)
124 if (riscv_consdev.cn_pri != CN_DEAD &&
125 riscv_consdev.cn_name[0] != '\0') {
126 tp = tty_alloc(&riscv_ttydevsw, NULL);
127 tty_init_console(tp, 0);
128 tty_makedev(tp, NULL, "%s", "rcons");
132 callout_init(&riscv_callout, 1);
133 callout_reset(&riscv_callout, polltime, riscv_timeout, NULL);
137 SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL);
140 riscvtty_outwakeup(struct tty *tp)
142 u_char buf[MAX_BURST_LEN];
147 len = ttydisc_getc(tp, buf, sizeof(buf));
151 KASSERT(len == 1, ("tty error"));
153 for (i = 0; i < len; i++)
159 riscv_timeout(void *v)
164 while ((c = riscv_cngetc(NULL)) != -1)
165 ttydisc_rint(tp, c, 0);
166 ttydisc_rint_done(tp);
169 callout_reset(&riscv_callout, polltime, riscv_timeout, NULL);
173 riscv_cnprobe(struct consdev *cp)
176 cp->cn_pri = CN_NORMAL;
180 riscv_cninit(struct consdev *cp)
183 strcpy(cp->cn_name, "rcons");
187 riscv_cnterm(struct consdev *cp)
193 riscv_cngrab(struct consdev *cp)
199 riscv_cnungrab(struct consdev *cp)
205 riscv_cngetc(struct consdev *cp)
211 * RISCVTODO: BBL polls for console data on timer interrupt,
212 * but interrupts are turned off in KDB.
213 * So we currently do not have console in KDB.
216 ch = sbi_console_getchar();
218 ch = sbi_console_getchar();
223 ch = sbi_console_getchar();
224 if (ch > 0 && ch < 0xff) {
226 kdb_alt_break(ch, &alt_break_state);
235 riscv_cnputc(struct consdev *cp, int c)
244 rcons_probe(device_t dev)
247 device_set_desc(dev, "RISC-V console");
249 return (BUS_PROBE_DEFAULT);
253 rcons_attach(device_t dev)
255 struct rcons_softc *sc;
257 if (device_get_unit(dev) != 0)
260 sc = device_get_softc(dev);
263 csr_set(sie, SIE_SSIE);
265 bus_generic_attach(sc->dev);
270 static device_method_t rcons_methods[] = {
271 DEVMETHOD(device_probe, rcons_probe),
272 DEVMETHOD(device_attach, rcons_attach),
277 static driver_t rcons_driver = {
280 sizeof(struct rcons_softc)
283 static devclass_t rcons_devclass;
285 DRIVER_MODULE(rcons, nexus, rcons_driver, rcons_devclass, 0, 0);