2 * Copyright (c) 2003 Jake Burkholder.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/interrupt.h>
34 #include <sys/kernel.h>
36 #include <machine/bus.h>
37 #include <machine/resource.h>
40 #include <dev/ofw/openfirm.h>
42 #include <sparc64/fhc/fhcreg.h>
43 #include <sparc64/fhc/fhcvar.h>
45 #include <sparc64/sbus/sbusvar.h>
47 #include <dev/zs/z8530reg.h>
48 #include <dev/zs/z8530var.h>
50 #define ZS_SBUS_CHAN_A 4
51 #define ZS_SBUS_CHAN_B 0
54 #define ZS_SBUS_DATA 2
56 #define ZS_SBUS_CLOCK (9600 * 512)
57 #define ZS_SBUS_CLOCK_DIV (16)
59 #define ZS_SBUS_DEF_SPEED (9600)
61 enum zs_device_ivars {
65 __BUS_ACCESSOR(zs, node, ZS, NODE, phandle_t)
67 struct zs_sbus_softc {
68 struct zs_softc sc_zs;
69 struct resource *sc_irqres;
72 struct resource *sc_memres;
76 static int zs_fhc_probe(device_t dev);
77 static int zs_fhc_attach(device_t dev);
78 static int zs_fhc_read_ivar(device_t dev, device_t child, int which,
81 static int zs_sbus_probe(device_t dev);
82 static int zs_sbus_attach(device_t dev);
83 static int zs_sbus_read_ivar(device_t dev, device_t child, int which,
86 static int zs_sbus_detach(device_t dev);
88 static int zstty_sbus_attach(device_t dev);
89 static int zstty_sbus_probe(device_t dev);
91 static int zstty_keyboard(device_t dev);
93 static device_method_t zs_fhc_methods[] = {
94 DEVMETHOD(device_probe, zs_fhc_probe),
95 DEVMETHOD(device_attach, zs_fhc_attach),
96 DEVMETHOD(device_detach, zs_sbus_detach),
98 DEVMETHOD(bus_print_child, bus_generic_print_child),
99 DEVMETHOD(bus_read_ivar, zs_fhc_read_ivar),
104 static device_method_t zs_sbus_methods[] = {
105 DEVMETHOD(device_probe, zs_sbus_probe),
106 DEVMETHOD(device_attach, zs_sbus_attach),
107 DEVMETHOD(device_detach, zs_sbus_detach),
109 DEVMETHOD(bus_print_child, bus_generic_print_child),
110 DEVMETHOD(bus_read_ivar, zs_sbus_read_ivar),
115 static device_method_t zstty_sbus_methods[] = {
116 DEVMETHOD(device_probe, zstty_sbus_probe),
117 DEVMETHOD(device_attach, zstty_sbus_attach),
118 DEVMETHOD(device_detach, bus_generic_detach),
123 static driver_t zs_fhc_driver = {
126 sizeof(struct zs_sbus_softc),
129 static driver_t zs_sbus_driver = {
132 sizeof(struct zs_sbus_softc),
135 static driver_t zstty_sbus_driver = {
138 sizeof(struct zstty_softc),
141 static devclass_t zs_fhc_devclass;
142 static devclass_t zs_sbus_devclass;
144 DRIVER_MODULE(zs, fhc, zs_fhc_driver, zs_fhc_devclass, 0, 0);
145 DRIVER_MODULE(zs, sbus, zs_sbus_driver, zs_sbus_devclass, 0, 0);
147 static devclass_t zstty_sbus_devclass;
149 DRIVER_MODULE(zstty, zs, zstty_sbus_driver, zstty_sbus_devclass, 0, 0);
151 static uint8_t zs_sbus_init_reg[16] = {
152 0, /* 0: CMD (reset, etc.) */
153 0, /* 1: No interrupts yet. */
155 ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
156 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
157 ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
158 0, /* 6: TXSYNC/SYNCLO */
159 0, /* 7: RXSYNC/SYNCHI */
160 0, /* 8: alias for data port */
161 ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
162 0, /* 10: Misc. TX/RX control bits */
163 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
164 ((ZS_SBUS_CLOCK/32)/ZS_SBUS_DEF_SPEED)-2,
166 ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
171 zs_fhc_probe(device_t dev)
174 if (strcmp(fhc_get_name(dev), "zs") != 0 ||
175 device_get_unit(dev) != 0)
177 return (zs_probe(dev));
181 zs_sbus_probe(device_t dev)
184 if (strcmp(sbus_get_name(dev), "zs") != 0 ||
185 device_get_unit(dev) != 0)
187 return (zs_probe(dev));
191 zs_fhc_attach(device_t dev)
193 struct zs_sbus_softc *sc;
195 sc = device_get_softc(dev);
196 sc->sc_memres = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->sc_memrid,
197 0, ~0, 1, RF_ACTIVE);
198 if (sc->sc_memres == NULL)
200 sc->sc_irqrid = FHC_UART;
201 sc->sc_irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->sc_irqrid, 0,
203 if (sc->sc_irqres == NULL)
205 if (bus_setup_intr(dev, sc->sc_irqres, INTR_TYPE_TTY | INTR_FAST,
206 zs_intr, sc, &sc->sc_ih) != 0)
208 sc->sc_zs.sc_bt = rman_get_bustag(sc->sc_memres);
209 sc->sc_zs.sc_bh = rman_get_bushandle(sc->sc_memres);
210 return (zs_attach(dev));
218 zs_sbus_attach(device_t dev)
220 struct zs_sbus_softc *sc;
222 sc = device_get_softc(dev);
223 sc->sc_memres = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->sc_memrid,
224 0, ~0, 1, RF_ACTIVE);
225 if (sc->sc_memres == NULL)
227 sc->sc_irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->sc_irqrid, 0,
229 if (sc->sc_irqres == NULL)
231 if (bus_setup_intr(dev, sc->sc_irqres, INTR_TYPE_TTY | INTR_FAST,
232 zs_intr, sc, &sc->sc_ih) != 0)
234 sc->sc_zs.sc_bt = rman_get_bustag(sc->sc_memres);
235 sc->sc_zs.sc_bh = rman_get_bushandle(sc->sc_memres);
236 return (zs_attach(dev));
244 zs_sbus_detach(device_t dev)
246 struct zs_sbus_softc *sc;
248 sc = device_get_softc(dev);
249 if (sc->sc_irqres != NULL) {
250 if (sc->sc_ih != NULL)
251 bus_teardown_intr(dev, sc->sc_irqres, sc->sc_ih);
252 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid,
255 if (sc->sc_memres != NULL)
256 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_memrid,
262 zs_fhc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
267 *result = fhc_get_node(dev);
276 zs_sbus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
281 *result = sbus_get_node(dev);
290 zstty_sbus_probe(device_t dev)
293 if (zstty_keyboard(dev)) {
294 if ((device_get_unit(dev) & 1) == 0)
295 device_set_desc(dev, "keyboard");
297 device_set_desc(dev, "mouse");
299 if ((device_get_unit(dev) & 1) == 0)
300 device_set_desc(dev, "ttya");
302 device_set_desc(dev, "ttyb");
304 return (zstty_probe(dev));
308 zstty_sbus_attach(device_t dev)
310 struct zstty_softc *sc;
312 sc = device_get_softc(dev);
313 sc->sc_parent = device_get_softc(device_get_parent(dev));
314 sc->sc_bt = sc->sc_parent->sc_bt;
315 sc->sc_brg_clk = ZS_SBUS_CLOCK / ZS_SBUS_CLOCK_DIV;
317 switch (device_get_unit(dev) & 1) {
319 bus_space_subregion(sc->sc_bt, sc->sc_parent->sc_bh,
320 ZS_SBUS_CHAN_A + ZS_SBUS_CSR, 1, &sc->sc_csr);
321 bus_space_subregion(sc->sc_bt, sc->sc_parent->sc_bh,
322 ZS_SBUS_CHAN_A + ZS_SBUS_DATA, 1, &sc->sc_data);
325 bus_space_subregion(sc->sc_bt, sc->sc_parent->sc_bh,
326 ZS_SBUS_CHAN_B + ZS_SBUS_CSR, 1, &sc->sc_csr);
327 bus_space_subregion(sc->sc_bt, sc->sc_parent->sc_bh,
328 ZS_SBUS_CHAN_B + ZS_SBUS_DATA, 1, &sc->sc_data);
332 bcopy(zs_sbus_init_reg, sc->sc_creg, 16);
333 bcopy(zs_sbus_init_reg, sc->sc_preg, 16);
335 return (zstty_attach(dev));
339 zstty_set_speed(struct zstty_softc *sc, int ospeed)
342 sc->sc_preg[12] = ospeed;
343 sc->sc_preg[13] = ospeed >> 8;
347 zstty_console(device_t dev, char *mode, int len)
357 chosen = OF_finddevice("/chosen");
358 options = OF_finddevice("/options");
359 if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1 ||
360 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1 ||
361 OF_getprop(options, "input-device", input, sizeof(input)) == -1 ||
362 OF_getprop(options, "output-device", output, sizeof(output)) == -1)
364 if (zs_get_node(dev) != OF_instance_to_package(stdin) ||
365 zs_get_node(dev) != OF_instance_to_package(stdout))
367 if ((strcmp(input, device_get_desc(dev)) == 0 &&
368 strcmp(output, device_get_desc(dev)) == 0) ||
369 (strcmp(input, "keyboard") == 0 && strcmp(output, "screen") == 0 &&
370 (device_get_unit(dev) & 1) == 0 && !zstty_keyboard(dev))) {
372 sprintf(name, "%s-mode", device_get_desc(dev));
373 return (OF_getprop(options, name, mode, len) != -1);
381 zstty_keyboard(device_t dev)
384 return (OF_getproplen(zs_get_node(dev), "keyboard") == 0);