gftty.c revision 1.2 1 1.2 thorpej /* $NetBSD: gftty.c,v 1.2 2024/01/02 07:30:29 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2023 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej /*
33 1.2 thorpej * Support for the Goldfish virtual TTY.
34 1.1 thorpej */
35 1.1 thorpej
36 1.1 thorpej #include <sys/cdefs.h>
37 1.2 thorpej __KERNEL_RCSID(0, "$NetBSD: gftty.c,v 1.2 2024/01/02 07:30:29 thorpej Exp $");
38 1.1 thorpej
39 1.1 thorpej #include <sys/param.h>
40 1.1 thorpej #include <sys/systm.h>
41 1.1 thorpej #include <sys/bus.h>
42 1.1 thorpej #include <sys/device.h>
43 1.1 thorpej #include <sys/kmem.h>
44 1.1 thorpej
45 1.1 thorpej #include <uvm/uvm_extern.h>
46 1.1 thorpej
47 1.1 thorpej #include <dev/cons.h>
48 1.1 thorpej
49 1.1 thorpej #include <dev/goldfish/gfttyvar.h>
50 1.1 thorpej
51 1.1 thorpej /*
52 1.1 thorpej * Goldfish TTY registers.
53 1.1 thorpej */
54 1.1 thorpej #define GFTTY_PUT_CHAR 0x00 /* 8 bit output value */
55 1.1 thorpej #define GFTTY_BYTES_READY 0x04 /* number of input bytes available */
56 1.1 thorpej #define GFTTY_CMD 0x08 /* command */
57 1.1 thorpej #define GFTTY_DATA_PTR 0x10 /* DMA pointer */
58 1.1 thorpej #define GFTTY_DATA_LEN 0x14 /* DMA length */
59 1.1 thorpej #define GFTTY_DATA_PTR_HIGH 0x18 /* DMA pointer (64-bit) */
60 1.1 thorpej #define GFTTY_VERSION 0x20 /* TTY version */
61 1.1 thorpej
62 1.1 thorpej #define CMD_INT_DISABLE 0x00
63 1.1 thorpej #define CMD_INT_ENABLE 0x01
64 1.1 thorpej #define CMD_WRITE_BUFFER 0x02
65 1.1 thorpej #define CMD_READ_BUFFER 0x03
66 1.1 thorpej
67 1.1 thorpej #define REG_READ0(c, r) \
68 1.1 thorpej bus_space_read_4((c)->c_bst, (c)->c_bsh, (r))
69 1.1 thorpej #define REG_WRITE0(c, r, v) \
70 1.1 thorpej bus_space_write_4((c)->c_bst, (c)->c_bsh, (r), (v))
71 1.1 thorpej
72 1.1 thorpej #define REG_READ(sc, r) REG_READ0((sc)->sc_config, (r))
73 1.1 thorpej #define REG_WRITE(sc, r, v) REG_WRITE0((sc)->sc_config, (r), (v))
74 1.1 thorpej
75 1.1 thorpej static int gftty_cngetc(dev_t);
76 1.1 thorpej static void gftty_cnputc(dev_t, int);
77 1.1 thorpej static void gftty_cnpollc(dev_t, int);
78 1.1 thorpej
79 1.1 thorpej static struct gftty_config gftty_cnconfig;
80 1.1 thorpej static struct cnm_state gftty_cnmagic_state;
81 1.1 thorpej static struct consdev gftty_consdev = {
82 1.1 thorpej .cn_getc = gftty_cngetc,
83 1.1 thorpej .cn_putc = gftty_cnputc,
84 1.1 thorpej .cn_pollc = gftty_cnpollc,
85 1.1 thorpej .cn_dev = NODEV,
86 1.1 thorpej .cn_pri = CN_NORMAL,
87 1.1 thorpej };
88 1.1 thorpej
89 1.1 thorpej /*
90 1.1 thorpej * gftty_attach --
91 1.1 thorpej * Attach a Goldfish virual TTY.
92 1.1 thorpej */
93 1.1 thorpej void
94 1.1 thorpej gftty_attach(struct gftty_softc *sc)
95 1.1 thorpej {
96 1.1 thorpej
97 1.1 thorpej aprint_naive("\n");
98 1.1 thorpej aprint_normal(": Google Goldfish TTY\n");
99 1.1 thorpej
100 1.1 thorpej /* If we got here without a config, we're the console. */
101 1.1 thorpej if (sc->sc_config == NULL) {
102 1.1 thorpej KASSERT(gftty_is_console(sc));
103 1.1 thorpej sc->sc_config = &gftty_cnconfig;
104 1.1 thorpej aprint_normal_dev(sc->sc_dev, "console\n");
105 1.1 thorpej }
106 1.1 thorpej }
107 1.1 thorpej
108 1.1 thorpej /*
109 1.1 thorpej * gftty_is_console --
110 1.1 thorpej * Returns true if the specified gftty instance is currently
111 1.1 thorpej * the console.
112 1.1 thorpej */
113 1.1 thorpej bool
114 1.1 thorpej gftty_is_console(struct gftty_softc *sc)
115 1.1 thorpej {
116 1.1 thorpej if (cn_tab == &gftty_consdev) {
117 1.1 thorpej bool val;
118 1.1 thorpej
119 1.1 thorpej if (prop_dictionary_get_bool(device_properties(sc->sc_dev),
120 1.1 thorpej "is-console", &val)) {
121 1.1 thorpej return val;
122 1.1 thorpej }
123 1.1 thorpej }
124 1.1 thorpej return false;
125 1.1 thorpej }
126 1.1 thorpej
127 1.1 thorpej /*
128 1.1 thorpej * gftty_init_config --
129 1.1 thorpej * Initialize a config structure.
130 1.1 thorpej */
131 1.1 thorpej static void
132 1.1 thorpej gftty_init_config(struct gftty_config *c, bus_space_tag_t bst,
133 1.1 thorpej bus_space_handle_t bsh)
134 1.1 thorpej {
135 1.1 thorpej c->c_bst = bst;
136 1.1 thorpej c->c_bsh = bsh;
137 1.1 thorpej c->c_version = REG_READ0(c, GFTTY_VERSION);
138 1.1 thorpej }
139 1.1 thorpej
140 1.1 thorpej /*
141 1.1 thorpej * gftty_alloc_config --
142 1.1 thorpej * Allocate a config structure, initialize it, and assign
143 1.1 thorpej * it to this device.
144 1.1 thorpej */
145 1.1 thorpej void
146 1.1 thorpej gftty_alloc_config(struct gftty_softc *sc, bus_space_tag_t bst,
147 1.1 thorpej bus_space_handle_t bsh)
148 1.1 thorpej {
149 1.1 thorpej struct gftty_config *c = kmem_zalloc(sizeof(*c), KM_SLEEP);
150 1.1 thorpej
151 1.1 thorpej gftty_init_config(c, bst, bsh);
152 1.1 thorpej sc->sc_config = c;
153 1.1 thorpej }
154 1.1 thorpej
155 1.1 thorpej /*
156 1.1 thorpej * gftty_set_buffer --
157 1.1 thorpej * Set the buffer address / length for an I/O operation.
158 1.1 thorpej */
159 1.1 thorpej static void
160 1.1 thorpej gftty_set_buffer(struct gftty_config *c, bus_addr_t addr, bus_size_t size)
161 1.1 thorpej {
162 1.1 thorpej REG_WRITE0(c, GFTTY_DATA_PTR, BUS_ADDR_LO32(addr));
163 1.1 thorpej if (sizeof(bus_addr_t) == 8) {
164 1.1 thorpej REG_WRITE0(c, GFTTY_DATA_PTR_HIGH, BUS_ADDR_HI32(addr));
165 1.1 thorpej }
166 1.1 thorpej REG_WRITE0(c, GFTTY_DATA_LEN, (uint32_t)size);
167 1.1 thorpej }
168 1.1 thorpej
169 1.1 thorpej /*
170 1.1 thorpej * gftty console routines.
171 1.1 thorpej */
172 1.1 thorpej static int
173 1.1 thorpej gftty_cngetc(dev_t dev)
174 1.1 thorpej {
175 1.1 thorpej struct gftty_config * const c = &gftty_cnconfig;
176 1.1 thorpej
177 1.1 thorpej if (REG_READ0(c, GFTTY_BYTES_READY) == 0) {
178 1.1 thorpej return -1;
179 1.1 thorpej }
180 1.1 thorpej
181 1.1 thorpej /*
182 1.1 thorpej * XXX This is all terrible and should burn to the ground.
183 1.1 thorpej * XXX This device desperately needs to be improved with
184 1.1 thorpej * XXX a GET_CHAR register.
185 1.1 thorpej */
186 1.1 thorpej bus_addr_t addr;
187 1.1 thorpej uint8_t buf[1];
188 1.1 thorpej
189 1.1 thorpej if (c->c_version == 0) {
190 1.1 thorpej addr = (bus_addr_t)buf;
191 1.1 thorpej } else {
192 1.1 thorpej addr = vtophys((vaddr_t)buf);
193 1.1 thorpej }
194 1.1 thorpej
195 1.1 thorpej gftty_set_buffer(c, addr, sizeof(buf));
196 1.1 thorpej REG_WRITE0(c, GFTTY_CMD, CMD_READ_BUFFER);
197 1.1 thorpej
198 1.1 thorpej return buf[0];
199 1.1 thorpej }
200 1.1 thorpej
201 1.1 thorpej static void
202 1.1 thorpej gftty_cnputc(dev_t dev, int ch)
203 1.1 thorpej {
204 1.1 thorpej REG_WRITE0(&gftty_cnconfig, GFTTY_PUT_CHAR, (unsigned char)ch);
205 1.1 thorpej }
206 1.1 thorpej
207 1.1 thorpej static void
208 1.1 thorpej gftty_cnpollc(dev_t dev, int on)
209 1.1 thorpej {
210 1.1 thorpej /* XXX */
211 1.1 thorpej }
212 1.1 thorpej
213 1.1 thorpej /*
214 1.1 thorpej * gftty_cnattach --
215 1.1 thorpej * Attach a Goldfish virtual TTY console.
216 1.1 thorpej */
217 1.1 thorpej void
218 1.1 thorpej gftty_cnattach(bus_space_tag_t bst, bus_space_handle_t bsh)
219 1.1 thorpej {
220 1.1 thorpej gftty_init_config(&gftty_cnconfig, bst, bsh);
221 1.1 thorpej
222 1.1 thorpej cn_tab = &gftty_consdev;
223 1.1 thorpej cn_init_magic(&gftty_cnmagic_state);
224 1.1 thorpej cn_set_magic("+++++");
225 1.1 thorpej }
226