2 * Copyright (c) 2015-2016 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>
71 static struct resource_spec rcons_spec[] = {
72 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE},
78 struct resource *res[1];
83 /* CN Console interface */
85 static tsw_outwakeup_t riscvtty_outwakeup;
87 static struct ttydevsw riscv_ttydevsw = {
88 .tsw_flags = TF_NOPREFIX,
89 .tsw_outwakeup = riscvtty_outwakeup,
93 static struct callout riscv_callout;
94 static struct tty *tp = NULL;
97 static int alt_break_state;
100 static void riscv_timeout(void *);
102 static cn_probe_t riscv_cnprobe;
103 static cn_init_t riscv_cninit;
104 static cn_term_t riscv_cnterm;
105 static cn_getc_t riscv_cngetc;
106 static cn_putc_t riscv_cnputc;
107 static cn_grab_t riscv_cngrab;
108 static cn_ungrab_t riscv_cnungrab;
110 CONSOLE_DRIVER(riscv);
112 #define MAX_BURST_LEN 1
113 #define QUEUE_SIZE 256
118 struct queue_entry *next;
121 struct queue_entry cnqueue[QUEUE_SIZE];
122 struct queue_entry *entry_last;
123 struct queue_entry *entry_served;
129 sbi_console_putchar(c);
133 early_putc_t *early_putc = riscv_putc;
137 cn_drvinit(void *unused)
140 if (riscv_consdev.cn_pri != CN_DEAD &&
141 riscv_consdev.cn_name[0] != '\0') {
142 tp = tty_alloc(&riscv_ttydevsw, NULL);
143 tty_init_console(tp, 0);
144 tty_makedev(tp, NULL, "%s", "rcons");
148 callout_init(&riscv_callout, 1);
149 callout_reset(&riscv_callout, polltime, riscv_timeout, NULL);
153 SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL);
156 riscvtty_outwakeup(struct tty *tp)
158 u_char buf[MAX_BURST_LEN];
163 len = ttydisc_getc(tp, buf, sizeof(buf));
167 KASSERT(len == 1, ("tty error"));
169 for (i = 0; i < len; i++)
175 riscv_timeout(void *v)
180 while ((c = riscv_cngetc(NULL)) != -1)
181 ttydisc_rint(tp, c, 0);
182 ttydisc_rint_done(tp);
185 callout_reset(&riscv_callout, polltime, riscv_timeout, NULL);
189 riscv_cnprobe(struct consdev *cp)
192 cp->cn_pri = CN_NORMAL;
196 riscv_cninit(struct consdev *cp)
200 strcpy(cp->cn_name, "rcons");
202 for (i = 0; i < QUEUE_SIZE; i++) {
203 if (i == (QUEUE_SIZE - 1))
204 cnqueue[i].next = &cnqueue[0];
206 cnqueue[i].next = &cnqueue[i+1];
211 entry_last = &cnqueue[0];
212 entry_served = &cnqueue[0];
216 riscv_cnterm(struct consdev *cp)
222 riscv_cngrab(struct consdev *cp)
228 riscv_cnungrab(struct consdev *cp)
234 riscv_cngetc(struct consdev *cp)
241 * RISCVTODO: BBL polls for console data on timer interrupt,
242 * but interrupts are turned off in KDB.
243 * So we currently do not have console in KDB.
246 ch = sbi_console_getchar();
248 entry_last->data = ch;
249 entry_last->used = 1;
250 entry_last = entry_last->next;
252 ch = sbi_console_getchar();
257 if (entry_served->used == 1) {
258 data = entry_served->data;
259 entry_served->used = 0;
260 entry_served = entry_served->next;
262 if (ch > 0 && ch < 0xff) {
264 kdb_alt_break(ch, &alt_break_state);
274 riscv_cnputc(struct consdev *cp, int c)
283 rcons_intr(void *arg)
287 c = sbi_console_getchar();
288 if (c > 0 && c < 0xff) {
289 entry_last->data = c;
290 entry_last->used = 1;
291 entry_last = entry_last->next;
294 csr_clear(sip, SIP_SSIP);
296 return (FILTER_HANDLED);
300 rcons_probe(device_t dev)
303 if (!ofw_bus_status_okay(dev))
306 if (!ofw_bus_is_compatible(dev, "riscv,console"))
309 device_set_desc(dev, "RISC-V console");
310 return (BUS_PROBE_DEFAULT);
314 rcons_attach(device_t dev)
316 struct rcons_softc *sc;
319 sc = device_get_softc(dev);
322 if (bus_alloc_resources(dev, rcons_spec, sc->res)) {
323 device_printf(dev, "could not allocate resources\n");
327 /* Setup IRQs handler */
328 error = bus_setup_intr(dev, sc->res[0], INTR_TYPE_CLK,
329 rcons_intr, NULL, sc, &sc->ihl[0]);
331 device_printf(dev, "Unable to alloc int resource.\n");
335 csr_set(sie, SIE_SSIE);
337 bus_generic_attach(sc->dev);
339 sbi_console_getchar();
344 static device_method_t rcons_methods[] = {
345 DEVMETHOD(device_probe, rcons_probe),
346 DEVMETHOD(device_attach, rcons_attach),
351 static driver_t rcons_driver = {
354 sizeof(struct rcons_softc)
357 static devclass_t rcons_devclass;
359 DRIVER_MODULE(rcons, simplebus, rcons_driver, rcons_devclass, 0, 0);