1170101Ssimokawa/*-
2170101Ssimokawa * Copyright (c) 2004 Hidetoshi Shimokawa
3170101Ssimokawa *
4170101Ssimokawa * Redistribution and use in source and binary forms, with or without
5170101Ssimokawa * modification, are permitted provided that the following conditions
6170101Ssimokawa * are met:
7170101Ssimokawa * 1. Redistributions of source code must retain the above copyright
8170101Ssimokawa *    notice, this list of conditions and the following disclaimer.
9170101Ssimokawa * 2. Redistributions in binary form must reproduce the above copyright
10170101Ssimokawa *    notice, this list of conditions and the following disclaimer in the
11170101Ssimokawa *    documentation and/or other materials provided with the distribution.
12170101Ssimokawa *
13170101Ssimokawa * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14170101Ssimokawa * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15170101Ssimokawa * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16170101Ssimokawa * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17170101Ssimokawa * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18170101Ssimokawa * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19170101Ssimokawa * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20170101Ssimokawa * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21170101Ssimokawa * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22170101Ssimokawa * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23170101Ssimokawa * SUCH DAMAGE.
24170101Ssimokawa */
25170101Ssimokawa
26170101Ssimokawa#include <sys/cdefs.h>
27170101Ssimokawa__FBSDID("$FreeBSD: releng/10.2/sys/boot/i386/libfirewire/dconsole.c 170101 2007-05-29 14:35:57Z simokawa $");
28170101Ssimokawa
29170101Ssimokawa#include <stand.h>
30170101Ssimokawa#include <bootstrap.h>
31170101Ssimokawa#include <sys/param.h>
32170101Ssimokawa#include <btxv86.h>
33170101Ssimokawa#include <dev/dcons/dcons.h>
34170101Ssimokawa
35170101Ssimokawavoid fw_enable(void);
36170101Ssimokawavoid fw_poll(void);
37170101Ssimokawa
38170101Ssimokawastatic void	dconsole_probe(struct console *cp);
39170101Ssimokawastatic int	dconsole_init(int arg);
40170101Ssimokawastatic void	dconsole_putchar(int c);
41170101Ssimokawastatic int	dconsole_getchar(void);
42170101Ssimokawastatic int	dconsole_ischar(void);
43170101Ssimokawa
44170101Ssimokawastatic int	dcons_started = 0;
45170101Ssimokawa
46170101Ssimokawa#define DCONS_BUF_SIZE (64*1024)
47170101Ssimokawastatic struct dcons_softc sc[DCONS_NPORT];
48170101Ssimokawauint32_t dcons_paddr;
49170101Ssimokawa
50170101Ssimokawa/* The buffer must be allocated in BSS becase:
51170101Ssimokawa *    - The dcons driver in the kernel is initialized before VM/pmap is
52170101Ssimokawa *	initialized, so that the buffer must be allocate in the region
53170101Ssimokawa *	that is mapped at the very early boot state.
54170101Ssimokawa *    - We expect identiy map only for regions before KERNLOAD
55170101Ssimokawa *	(i386:4MB amd64:1MB).
56170101Ssimokawa *    - It seems that heap in conventional memory(640KB) is not sufficent
57170101Ssimokawa *	and we move it to high address as LOADER_SUPPORT_BZIP2.
58170101Ssimokawa *    - BSS is placed in conventional memory.
59170101Ssimokawa */
60170101Ssimokawastatic char dcons_buffer[DCONS_BUF_SIZE + PAGE_SIZE];
61170101Ssimokawa
62170101Ssimokawastruct console dconsole = {
63170101Ssimokawa    "dcons",
64170101Ssimokawa    "dumb console port",
65170101Ssimokawa    0,
66170101Ssimokawa    dconsole_probe,
67170101Ssimokawa    dconsole_init,
68170101Ssimokawa    dconsole_putchar,
69170101Ssimokawa    dconsole_getchar,
70170101Ssimokawa    dconsole_ischar
71170101Ssimokawa};
72170101Ssimokawa
73170101Ssimokawa#define DCONSOLE_AS_MULTI_CONSOLE	1
74170101Ssimokawa
75170101Ssimokawastatic void
76170101Ssimokawadconsole_probe(struct console *cp)
77170101Ssimokawa{
78170101Ssimokawa    /* XXX check the BIOS equipment list? */
79170101Ssimokawa    cp->c_flags |= (C_PRESENTIN | C_PRESENTOUT);
80170101Ssimokawa#if DCONSOLE_AS_MULTI_CONSOLE
81170101Ssimokawa    dconsole_init(0);
82170101Ssimokawa    cp->c_flags |= (C_ACTIVEIN | C_ACTIVEOUT);
83170101Ssimokawa#endif
84170101Ssimokawa}
85170101Ssimokawa
86170101Ssimokawastatic int
87170101Ssimokawadconsole_init(int arg)
88170101Ssimokawa{
89170101Ssimokawa    char buf[16], *dbuf;
90170101Ssimokawa    int size;
91170101Ssimokawa
92170101Ssimokawa    if (dcons_started && arg == 0)
93170101Ssimokawa	return 0;
94170101Ssimokawa    dcons_started = 1;
95170101Ssimokawa
96170101Ssimokawa    size = DCONS_BUF_SIZE;
97170101Ssimokawa    dbuf = (char *)round_page((vm_offset_t)&dcons_buffer[0]);
98170101Ssimokawa    dcons_paddr = VTOP(dbuf);
99170101Ssimokawa    sprintf(buf, "0x%08x", dcons_paddr);
100170101Ssimokawa    setenv("dcons.addr", buf, 1);
101170101Ssimokawa
102170101Ssimokawa    dcons_init((struct dcons_buf *)dbuf, size, sc);
103170101Ssimokawa    sprintf(buf, "%d", size);
104170101Ssimokawa    setenv("dcons.size", buf, 1);
105170101Ssimokawa    fw_enable();
106170101Ssimokawa    return(0);
107170101Ssimokawa}
108170101Ssimokawa
109170101Ssimokawastatic void
110170101Ssimokawadconsole_putchar(int c)
111170101Ssimokawa{
112170101Ssimokawa    dcons_putc(&sc[0], c);
113170101Ssimokawa}
114170101Ssimokawa
115170101Ssimokawastatic int
116170101Ssimokawadconsole_getchar(void)
117170101Ssimokawa{
118170101Ssimokawa    fw_poll();
119170101Ssimokawa    return (dcons_checkc(&sc[0]));
120170101Ssimokawa}
121170101Ssimokawa
122170101Ssimokawastatic int
123170101Ssimokawadconsole_ischar(void)
124170101Ssimokawa{
125170101Ssimokawa    fw_poll();
126170101Ssimokawa    return (dcons_ischar(&sc[0]));
127170101Ssimokawa}
128