uart_cpu_sparc64.c revision 122466
1119815Smarcel/*
2119815Smarcel * Copyright (c) 2003 Marcel Moolenaar
3119815Smarcel * All rights reserved.
4119815Smarcel *
5119815Smarcel * Redistribution and use in source and binary forms, with or without
6119815Smarcel * modification, are permitted provided that the following conditions
7119815Smarcel * are met:
8119815Smarcel *
9119815Smarcel * 1. Redistributions of source code must retain the above copyright
10119815Smarcel *    notice, this list of conditions and the following disclaimer.
11119815Smarcel * 2. Redistributions in binary form must reproduce the above copyright
12119815Smarcel *    notice, this list of conditions and the following disclaimer in the
13119815Smarcel *    documentation and/or other materials provided with the distribution.
14119815Smarcel *
15119815Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16119815Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17119815Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18119815Smarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19119815Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20119815Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21119815Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22119815Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23119815Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24119815Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25119815Smarcel */
26119815Smarcel
27119815Smarcel#include <sys/cdefs.h>
28119815Smarcel__FBSDID("$FreeBSD: head/sys/dev/uart/uart_cpu_sparc64.c 122466 2003-11-11 06:52:04Z jake $");
29119815Smarcel
30119815Smarcel#include <sys/param.h>
31119815Smarcel#include <sys/systm.h>
32119815Smarcel
33119815Smarcel#include <machine/bus.h>
34119815Smarcel#include <machine/bus_private.h>
35119815Smarcel
36119815Smarcel#include <dev/ofw/openfirm.h>
37119815Smarcel#include <machine/ofw_machdep.h>
38119815Smarcel
39119815Smarcel#include <dev/uart/uart.h>
40119815Smarcel#include <dev/uart/uart_cpu.h>
41119815Smarcel
42122466Sjakestatic phandle_t uart_cpu_getdev_keyboard(phandle_t root);
43122466Sjake
44119815Smarcelstatic struct bus_space_tag bst_store[3];
45119815Smarcel
46119815Smarcelstatic int
47119815Smarceluart_cpu_channel(char *dev)
48119815Smarcel{
49119815Smarcel	char alias[64];
50119815Smarcel	phandle_t aliases;
51119815Smarcel	int len;
52119815Smarcel
53119815Smarcel	strcpy(alias, dev);
54119815Smarcel	if ((aliases = OF_finddevice("/aliases")) != -1)
55119815Smarcel		OF_getprop(aliases, dev, alias, sizeof(alias));
56119815Smarcel	len = strlen(alias);
57119815Smarcel	if (len < 2 || alias[len - 2] != ':' || alias[len - 1] < 'a' ||
58119815Smarcel	    alias[len - 1] > 'b')
59119815Smarcel		return (0);
60120452Smarcel	return (alias[len - 1] - 'a' + 1);
61119815Smarcel}
62119815Smarcel
63119815Smarcelint
64119866Smarceluart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
65119866Smarcel{
66119866Smarcel
67119866Smarcel	return ((b1->bsh == b2->bsh) ? 1 : 0);
68119866Smarcel}
69119866Smarcel
70122466Sjakestatic phandle_t
71122466Sjakeuart_cpu_getdev_keyboard(phandle_t root)
72122466Sjake{
73122466Sjake	phandle_t child;
74122466Sjake	phandle_t node;
75122466Sjake	char buf[32];
76122466Sjake
77122466Sjake	for (child = OF_child(root); child != 0 && child != -1;
78122466Sjake	    child = OF_peer(child)) {
79122466Sjake		if (OF_getprop(child, "device_type", buf, sizeof(buf)) != -1 &&
80122466Sjake		    !strcmp(buf, "serial") &&
81122466Sjake		    OF_getprop(child, "keyboard", buf, sizeof(buf)) != -1)
82122466Sjake			return (child);
83122466Sjake		if ((node = uart_cpu_getdev_keyboard(child)) != -1)
84122466Sjake			return (node);
85122466Sjake	}
86122466Sjake	return (-1);
87122466Sjake}
88122466Sjake
89119866Smarcelint
90119815Smarceluart_cpu_getdev(int devtype, struct uart_devinfo *di)
91119815Smarcel{
92120009Stmm	char buf[32], dev[32], compat[32];
93119815Smarcel	phandle_t input, options, output;
94119815Smarcel	bus_addr_t addr;
95120452Smarcel	int baud, bits, error, space, stop;
96119815Smarcel	char flag, par;
97119815Smarcel
98119815Smarcel	/*
99119815Smarcel	 * Get the address of the UART that is selected as the console, if
100119815Smarcel	 * the console is an UART of course. Note that we enforce that both
101119815Smarcel	 * stdin and stdout are selected. For weird configurations, use
102119815Smarcel	 * ofw_console(4).
103119815Smarcel	 * Note that the currently active console (ie /chosen/stdout and
104119815Smarcel	 * /chosen/stdin) may not be the same as the device selected in the
105119815Smarcel	 * environment (ie /options/output-device and /options/input-device)
106119815Smarcel	 * because the user may have changed the environment. In that case
107119815Smarcel	 * I would assume that the user expects that FreeBSD uses the new
108119815Smarcel	 * console setting. There's choice choice, really.
109119815Smarcel	 */
110122466Sjake	 if ((options = OF_finddevice("/options")) == -1)
111122466Sjake		 return (ENXIO);
112122466Sjake	if (devtype == UART_DEV_CONSOLE) {
113122466Sjake		if (OF_getprop(options, "input-device", dev, sizeof(dev)) == -1)
114119815Smarcel			return (ENXIO);
115122466Sjake		if ((input = OF_finddevice(dev)) == -1)
116122466Sjake			return (ENXIO);
117122466Sjake		if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
118122466Sjake			return (ENXIO);
119122466Sjake		if (strcmp(buf, "serial"))
120122466Sjake			return (ENODEV);
121119815Smarcel		if (OF_getprop(options, "output-device", buf, sizeof(buf))
122119815Smarcel		    == -1)
123119815Smarcel			return (ENXIO);
124119815Smarcel		if ((output = OF_finddevice(buf)) == -1)
125119815Smarcel			return (ENXIO);
126119815Smarcel		if (input != output)
127119815Smarcel			return (ENXIO);
128122466Sjake	} else if (devtype == UART_DEV_KEYBOARD) {
129122466Sjake		if ((input = uart_cpu_getdev_keyboard(OF_peer(0))) == -1)
130122466Sjake			return (ENXIO);
131119815Smarcel	} else
132119815Smarcel		return (ENODEV);
133119815Smarcel
134119815Smarcel	error = OF_decode_addr(input, &space, &addr);
135119815Smarcel	if (error)
136119815Smarcel		return (error);
137119815Smarcel
138119815Smarcel	/* Get the device class. */
139119815Smarcel	if (OF_getprop(input, "name", buf, sizeof(buf)) == -1)
140119815Smarcel		return (ENXIO);
141120009Stmm	if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1)
142120009Stmm		compat[0] = '\0';
143119815Smarcel	di->bas.regshft = 0;
144119815Smarcel	di->bas.rclk = 0;
145119815Smarcel	if (!strcmp(buf, "se")) {
146119815Smarcel		di->ops = uart_sab82532_ops;
147120452Smarcel		di->bas.chan = uart_cpu_channel(dev);
148120452Smarcel		addr += 64 * (di->bas.chan - 1);
149119815Smarcel	} else if (!strcmp(buf, "zs")) {
150119815Smarcel		di->ops = uart_z8530_ops;
151120452Smarcel		di->bas.chan = uart_cpu_channel(dev);
152119815Smarcel		di->bas.regshft = 1;
153120452Smarcel		addr += 4 - 4 * (di->bas.chan - 1);
154120009Stmm	} else if (!strcmp(buf, "su") || !strcmp(buf, "su_pnp") ||
155120452Smarcel	    !strcmp(compat, "su") || !strcmp(compat, "su16550")) {
156119815Smarcel		di->ops = uart_ns8250_ops;
157120452Smarcel		di->bas.chan = 0;
158120452Smarcel	} else
159119815Smarcel		return (ENXIO);
160119815Smarcel
161119815Smarcel	/* Fill in the device info. */
162119815Smarcel	di->bas.bst = &bst_store[devtype];
163119815Smarcel	di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst);
164119815Smarcel
165119815Smarcel	/* Get the line settings. */
166120545Sjake	if (devtype == UART_DEV_KEYBOARD)
167120545Sjake		di->baudrate = 1200;
168120545Sjake	else
169120545Sjake		di->baudrate = 9600;
170119815Smarcel	di->databits = 8;
171119815Smarcel	di->stopbits = 1;
172119815Smarcel	di->parity = UART_PARITY_NONE;
173119815Smarcel	snprintf(buf, sizeof(buf), "%s-mode", dev);
174119815Smarcel	if (OF_getprop(options, buf, buf, sizeof(buf)) == -1)
175119815Smarcel		return (0);
176119815Smarcel	if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag)
177119815Smarcel	    != 5)
178119815Smarcel		return (0);
179119815Smarcel	di->baudrate = baud;
180119815Smarcel	di->databits = bits;
181119815Smarcel	di->stopbits = stop;
182119815Smarcel	di->parity = (par == 'n') ? UART_PARITY_NONE :
183119815Smarcel	    (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN;
184119815Smarcel	return (0);
185119815Smarcel}
186