uart_cpu_sparc64.c revision 120545
1155324Simp/*
2155324Simp * Copyright (c) 2003 Marcel Moolenaar
3155324Simp * All rights reserved.
4155324Simp *
5155324Simp * Redistribution and use in source and binary forms, with or without
6155324Simp * modification, are permitted provided that the following conditions
7155324Simp * are met:
8155324Simp *
9155324Simp * 1. Redistributions of source code must retain the above copyright
10155324Simp *    notice, this list of conditions and the following disclaimer.
11155324Simp * 2. Redistributions in binary form must reproduce the above copyright
12155324Simp *    notice, this list of conditions and the following disclaimer in the
13185265Simp *    documentation and/or other materials provided with the distribution.
14185265Simp *
15185265Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16185265Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17185265Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18185265Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19185265Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20185265Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21185265Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22185265Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23185265Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24155324Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25155324Simp */
26155324Simp
27155324Simp#include <sys/cdefs.h>
28155324Simp__FBSDID("$FreeBSD: head/sys/dev/uart/uart_cpu_sparc64.c 120545 2003-09-28 07:06:34Z jake $");
29234281Smarius
30155324Simp#include <sys/param.h>
31234281Smarius#include <sys/systm.h>
32234281Smarius
33234281Smarius#include <machine/bus.h>
34234281Smarius#include <machine/bus_private.h>
35234281Smarius
36155324Simp#include <dev/ofw/openfirm.h>
37155324Simp#include <machine/ofw_machdep.h>
38155324Simp
39234281Smarius#include <dev/uart/uart.h>
40234281Smarius#include <dev/uart/uart_cpu.h>
41234281Smarius
42234281Smariusstatic struct bus_space_tag bst_store[3];
43234281Smarius
44234281Smariusstatic int
45155324Simpuart_cpu_channel(char *dev)
46155324Simp{
47234281Smarius	char alias[64];
48234281Smarius	phandle_t aliases;
49234281Smarius	int len;
50234281Smarius
51234281Smarius	strcpy(alias, dev);
52234281Smarius	if ((aliases = OF_finddevice("/aliases")) != -1)
53234281Smarius		OF_getprop(aliases, dev, alias, sizeof(alias));
54155324Simp	len = strlen(alias);
55155324Simp	if (len < 2 || alias[len - 2] != ':' || alias[len - 1] < 'a' ||
56155324Simp	    alias[len - 1] > 'b')
57234281Smarius		return (0);
58234281Smarius	return (alias[len - 1] - 'a' + 1);
59234281Smarius}
60234281Smarius
61155324Simpint
62155324Simpuart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
63234281Smarius{
64234281Smarius
65234281Smarius	return ((b1->bsh == b2->bsh) ? 1 : 0);
66234281Smarius}
67234291Smarius
68234281Smariusint
69234281Smariusuart_cpu_getdev(int devtype, struct uart_devinfo *di)
70155324Simp{
71155324Simp	char buf[32], dev[32], compat[32];
72155324Simp	phandle_t input, options, output;
73155324Simp	bus_addr_t addr;
74155324Simp	int baud, bits, error, space, stop;
75234281Smarius	char flag, par;
76234281Smarius
77234281Smarius	/*
78234281Smarius	 * Get the address of the UART that is selected as the console, if
79234281Smarius	 * the console is an UART of course. Note that we enforce that both
80234281Smarius	 * stdin and stdout are selected. For weird configurations, use
81234281Smarius	 * ofw_console(4).
82234281Smarius	 * Note that the currently active console (ie /chosen/stdout and
83234281Smarius	 * /chosen/stdin) may not be the same as the device selected in the
84234281Smarius	 * environment (ie /options/output-device and /options/input-device)
85155324Simp	 * because the user may have changed the environment. In that case
86155324Simp	 * I would assume that the user expects that FreeBSD uses the new
87	 * console setting. There's choice choice, really.
88	 */
89	if ((options = OF_finddevice("/options")) == -1)
90		return (ENXIO);
91	if (OF_getprop(options, "input-device", dev, sizeof(dev)) == -1)
92		return (ENXIO);
93	if ((input = OF_finddevice(dev)) == -1)
94		return (ENXIO);
95	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
96		return (ENXIO);
97	if (strcmp(buf, "serial"))
98		return (ENODEV);
99	if (devtype == UART_DEV_KEYBOARD) {
100		if (OF_getprop(input, "keyboard", buf, sizeof(buf)) == -1)
101			return (ENXIO);
102	} else if (devtype == UART_DEV_CONSOLE) {
103		if (OF_getprop(options, "output-device", buf, sizeof(buf))
104		    == -1)
105			return (ENXIO);
106		if ((output = OF_finddevice(buf)) == -1)
107			return (ENXIO);
108		if (input != output)
109			return (ENXIO);
110	} else
111		return (ENODEV);
112
113	error = OF_decode_addr(input, &space, &addr);
114	if (error)
115		return (error);
116
117	/* Get the device class. */
118	if (OF_getprop(input, "name", buf, sizeof(buf)) == -1)
119		return (ENXIO);
120	if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1)
121		compat[0] = '\0';
122	di->bas.regshft = 0;
123	di->bas.rclk = 0;
124	if (!strcmp(buf, "se")) {
125		di->ops = uart_sab82532_ops;
126		di->bas.chan = uart_cpu_channel(dev);
127		addr += 64 * (di->bas.chan - 1);
128	} else if (!strcmp(buf, "zs")) {
129		di->ops = uart_z8530_ops;
130		di->bas.chan = uart_cpu_channel(dev);
131		di->bas.regshft = 1;
132		addr += 4 - 4 * (di->bas.chan - 1);
133	} else if (!strcmp(buf, "su") || !strcmp(buf, "su_pnp") ||
134	    !strcmp(compat, "su") || !strcmp(compat, "su16550")) {
135		di->ops = uart_ns8250_ops;
136		di->bas.chan = 0;
137	} else
138		return (ENXIO);
139
140	/* Fill in the device info. */
141	di->bas.bst = &bst_store[devtype];
142	di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst);
143
144	/* Get the line settings. */
145	if (devtype == UART_DEV_KEYBOARD)
146		di->baudrate = 1200;
147	else
148		di->baudrate = 9600;
149	di->databits = 8;
150	di->stopbits = 1;
151	di->parity = UART_PARITY_NONE;
152	snprintf(buf, sizeof(buf), "%s-mode", dev);
153	if (OF_getprop(options, buf, buf, sizeof(buf)) == -1)
154		return (0);
155	if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag)
156	    != 5)
157		return (0);
158	di->baudrate = baud;
159	di->databits = bits;
160	di->stopbits = stop;
161	di->parity = (par == 'n') ? UART_PARITY_NONE :
162	    (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN;
163	return (0);
164}
165