]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/zs/zs_sbus.c
Use __FBSDID().
[FreeBSD/FreeBSD.git] / sys / dev / zs / zs_sbus.c
1 /*-
2  * Copyright (c) 2003 Jake Burkholder.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/interrupt.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <machine/bus.h>
37 #include <machine/resource.h>
38 #include <sys/rman.h>
39
40 #include <dev/ofw/openfirm.h>
41
42 #include <sparc64/fhc/fhcreg.h>
43 #include <sparc64/fhc/fhcvar.h>
44
45 #include <sparc64/sbus/sbusvar.h>
46
47 #include <dev/zs/z8530reg.h>
48 #include <dev/zs/z8530var.h>
49
50 #define ZS_SBUS_CHAN_A          4
51 #define ZS_SBUS_CHAN_B          0
52
53 #define ZS_SBUS_CSR             0
54 #define ZS_SBUS_DATA            2
55
56 #define ZS_SBUS_CLOCK           (9600 * 512)
57 #define ZS_SBUS_CLOCK_DIV       (16)
58
59 #define ZS_SBUS_DEF_SPEED       (9600)
60
61 enum zs_device_ivars {
62         ZS_IVAR_NODE
63 };
64
65 __BUS_ACCESSOR(zs, node, ZS, NODE, phandle_t)
66
67 struct zs_sbus_softc {
68         struct zs_softc         sc_zs;
69         struct resource         *sc_irqres;
70         int                     sc_irqrid;
71         void                    *sc_ih;
72         struct resource         *sc_memres;
73         int                     sc_memrid;
74 };
75
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,
79     uintptr_t *result);
80
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,
84     uintptr_t *result);
85
86 static int zs_sbus_detach(device_t dev);
87
88 static int zstty_sbus_attach(device_t dev);
89 static int zstty_sbus_probe(device_t dev);
90
91 static int zstty_keyboard(device_t dev);
92
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),
97
98         DEVMETHOD(bus_print_child,      bus_generic_print_child),
99         DEVMETHOD(bus_read_ivar,        zs_fhc_read_ivar),
100
101         { 0, 0 }
102 };
103
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),
108
109         DEVMETHOD(bus_print_child,      bus_generic_print_child),
110         DEVMETHOD(bus_read_ivar,        zs_sbus_read_ivar),
111
112         { 0, 0 }
113 };
114
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),
119
120         { 0, 0 }
121 };
122
123 static driver_t zs_fhc_driver = {
124         "zs",
125         zs_fhc_methods,
126         sizeof(struct zs_sbus_softc),
127 };
128
129 static driver_t zs_sbus_driver = {
130         "zs",
131         zs_sbus_methods,
132         sizeof(struct zs_sbus_softc),
133 };
134
135 static driver_t zstty_sbus_driver = {
136         "zstty",
137         zstty_sbus_methods,
138         sizeof(struct zstty_softc),
139 };
140
141 static devclass_t zs_fhc_devclass;
142 static devclass_t zs_sbus_devclass;
143
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);
146
147 static devclass_t zstty_sbus_devclass;
148
149 DRIVER_MODULE(zstty, zs, zstty_sbus_driver, zstty_sbus_devclass, 0, 0);
150
151 static uint8_t zs_sbus_init_reg[16] = {
152         0,      /* 0: CMD (reset, etc.) */
153         0,      /* 1: No interrupts yet. */
154         0,      /* 2: IVECT */
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,
165         0,
166         ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
167         ZSWR15_BREAK_IE,
168 };
169
170 static int
171 zs_fhc_probe(device_t dev)
172 {
173
174         if (strcmp(fhc_get_name(dev), "zs") != 0 ||
175             device_get_unit(dev) != 0)
176                 return (ENXIO);
177         return (zs_probe(dev));
178 }
179
180 static int
181 zs_sbus_probe(device_t dev)
182 {
183
184         if (strcmp(sbus_get_name(dev), "zs") != 0 ||
185             device_get_unit(dev) != 0)
186                 return (ENXIO);
187         return (zs_probe(dev));
188 }
189
190 static int
191 zs_fhc_attach(device_t dev)
192 {
193         struct zs_sbus_softc *sc;
194
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)
199                 goto error;
200         sc->sc_irqrid = FHC_UART;
201         sc->sc_irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->sc_irqrid, 0,
202             ~0, 1, RF_ACTIVE);
203         if (sc->sc_irqres == NULL)
204                 goto error;
205         if (bus_setup_intr(dev, sc->sc_irqres, INTR_TYPE_TTY | INTR_FAST,
206             zs_intr, sc, &sc->sc_ih) != 0)
207                 goto error;
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));
211
212 error:
213         zs_sbus_detach(dev);
214         return (ENXIO);
215 }
216
217 static int
218 zs_sbus_attach(device_t dev)
219 {
220         struct zs_sbus_softc *sc;
221
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)
226                 goto error;
227         sc->sc_irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->sc_irqrid, 0,
228             ~0, 1, RF_ACTIVE);
229         if (sc->sc_irqres == NULL)
230                 goto error;
231         if (bus_setup_intr(dev, sc->sc_irqres, INTR_TYPE_TTY | INTR_FAST,
232             zs_intr, sc, &sc->sc_ih) != 0)
233                 goto error;
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));
237
238 error:
239         zs_sbus_detach(dev);
240         return (ENXIO);
241 }
242
243 static int
244 zs_sbus_detach(device_t dev)
245 {
246         struct zs_sbus_softc *sc;
247
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,
253                     sc->sc_irqres);
254         }
255         if (sc->sc_memres != NULL)
256                 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_memrid,
257                     sc->sc_memres);
258         return (0);
259 }
260
261 static int
262 zs_fhc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
263 {
264
265         switch (which) {
266         case ZS_IVAR_NODE:
267                 *result = fhc_get_node(dev);
268                 break;
269         default:
270                 return (ENOENT);
271         }
272         return (0);
273 }
274
275 static int
276 zs_sbus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
277 {
278
279         switch (which) {
280         case ZS_IVAR_NODE:
281                 *result = sbus_get_node(dev);
282                 break;
283         default:
284                 return (ENOENT);
285         }
286         return (0);
287 }
288
289 static int
290 zstty_sbus_probe(device_t dev)
291 {
292
293         if (zstty_keyboard(dev)) {
294                 if ((device_get_unit(dev) & 1) == 0)
295                         device_set_desc(dev, "keyboard");
296                 else
297                         device_set_desc(dev, "mouse");
298         } else {
299                 if ((device_get_unit(dev) & 1) == 0)
300                         device_set_desc(dev, "ttya");
301                 else
302                         device_set_desc(dev, "ttyb");
303         }
304         return (zstty_probe(dev));
305 }
306
307 static int
308 zstty_sbus_attach(device_t dev)
309 {
310         struct zstty_softc *sc;
311
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;
316
317         switch (device_get_unit(dev) & 1) {
318         case 0:
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);
323                 break;
324         case 1:
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);
329                 break;
330         }
331
332         bcopy(zs_sbus_init_reg, sc->sc_creg, 16);
333         bcopy(zs_sbus_init_reg, sc->sc_preg, 16);
334
335         return (zstty_attach(dev));
336 }
337
338 void
339 zstty_set_speed(struct zstty_softc *sc, int ospeed)
340 {
341
342         sc->sc_preg[12] = ospeed;
343         sc->sc_preg[13] = ospeed >> 8;
344 }
345
346 int
347 zstty_console(device_t dev, char *mode, int len)
348 {
349         phandle_t chosen;
350         phandle_t options;
351         ihandle_t stdin;
352         ihandle_t stdout;
353         char output[32];
354         char input[32];
355         char name[32];
356
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)
363                 return (0);
364         if (zs_get_node(dev) != OF_instance_to_package(stdin) ||
365             zs_get_node(dev) != OF_instance_to_package(stdout))
366                 return (0);
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))) {
371                 if (mode != NULL) {
372                         sprintf(name, "%s-mode", device_get_desc(dev));
373                         return (OF_getprop(options, name, mode, len) != -1);
374                 } else
375                         return (1);
376         }
377         return (0);
378 }
379
380 static int
381 zstty_keyboard(device_t dev)
382 {
383
384         return (OF_getproplen(zs_get_node(dev), "keyboard") == 0);
385 }