uart_cpu_sparc64.c revision 120545
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 120545 2003-09-28 07:06:34Z 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
42119815Smarcelstatic struct bus_space_tag bst_store[3];
43119815Smarcel
44119815Smarcelstatic int
45119815Smarceluart_cpu_channel(char *dev)
46119815Smarcel{
47119815Smarcel	char alias[64];
48119815Smarcel	phandle_t aliases;
49119815Smarcel	int len;
50119815Smarcel
51119815Smarcel	strcpy(alias, dev);
52119815Smarcel	if ((aliases = OF_finddevice("/aliases")) != -1)
53119815Smarcel		OF_getprop(aliases, dev, alias, sizeof(alias));
54119815Smarcel	len = strlen(alias);
55119815Smarcel	if (len < 2 || alias[len - 2] != ':' || alias[len - 1] < 'a' ||
56119815Smarcel	    alias[len - 1] > 'b')
57119815Smarcel		return (0);
58120452Smarcel	return (alias[len - 1] - 'a' + 1);
59119815Smarcel}
60119815Smarcel
61119815Smarcelint
62119866Smarceluart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
63119866Smarcel{
64119866Smarcel
65119866Smarcel	return ((b1->bsh == b2->bsh) ? 1 : 0);
66119866Smarcel}
67119866Smarcel
68119866Smarcelint
69119815Smarceluart_cpu_getdev(int devtype, struct uart_devinfo *di)
70119815Smarcel{
71120009Stmm	char buf[32], dev[32], compat[32];
72119815Smarcel	phandle_t input, options, output;
73119815Smarcel	bus_addr_t addr;
74120452Smarcel	int baud, bits, error, space, stop;
75119815Smarcel	char flag, par;
76119815Smarcel
77119815Smarcel	/*
78119815Smarcel	 * Get the address of the UART that is selected as the console, if
79119815Smarcel	 * the console is an UART of course. Note that we enforce that both
80119815Smarcel	 * stdin and stdout are selected. For weird configurations, use
81119815Smarcel	 * ofw_console(4).
82119815Smarcel	 * Note that the currently active console (ie /chosen/stdout and
83119815Smarcel	 * /chosen/stdin) may not be the same as the device selected in the
84119815Smarcel	 * environment (ie /options/output-device and /options/input-device)
85119815Smarcel	 * because the user may have changed the environment. In that case
86119815Smarcel	 * I would assume that the user expects that FreeBSD uses the new
87119815Smarcel	 * console setting. There's choice choice, really.
88119815Smarcel	 */
89119815Smarcel	if ((options = OF_finddevice("/options")) == -1)
90119815Smarcel		return (ENXIO);
91119815Smarcel	if (OF_getprop(options, "input-device", dev, sizeof(dev)) == -1)
92119815Smarcel		return (ENXIO);
93119815Smarcel	if ((input = OF_finddevice(dev)) == -1)
94119815Smarcel		return (ENXIO);
95119815Smarcel	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
96119815Smarcel		return (ENXIO);
97119815Smarcel	if (strcmp(buf, "serial"))
98119815Smarcel		return (ENODEV);
99119815Smarcel	if (devtype == UART_DEV_KEYBOARD) {
100119815Smarcel		if (OF_getprop(input, "keyboard", buf, sizeof(buf)) == -1)
101119815Smarcel			return (ENXIO);
102119815Smarcel	} else if (devtype == UART_DEV_CONSOLE) {
103119815Smarcel		if (OF_getprop(options, "output-device", buf, sizeof(buf))
104119815Smarcel		    == -1)
105119815Smarcel			return (ENXIO);
106119815Smarcel		if ((output = OF_finddevice(buf)) == -1)
107119815Smarcel			return (ENXIO);
108119815Smarcel		if (input != output)
109119815Smarcel			return (ENXIO);
110119815Smarcel	} else
111119815Smarcel		return (ENODEV);
112119815Smarcel
113119815Smarcel	error = OF_decode_addr(input, &space, &addr);
114119815Smarcel	if (error)
115119815Smarcel		return (error);
116119815Smarcel
117119815Smarcel	/* Get the device class. */
118119815Smarcel	if (OF_getprop(input, "name", buf, sizeof(buf)) == -1)
119119815Smarcel		return (ENXIO);
120120009Stmm	if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1)
121120009Stmm		compat[0] = '\0';
122119815Smarcel	di->bas.regshft = 0;
123119815Smarcel	di->bas.rclk = 0;
124119815Smarcel	if (!strcmp(buf, "se")) {
125119815Smarcel		di->ops = uart_sab82532_ops;
126120452Smarcel		di->bas.chan = uart_cpu_channel(dev);
127120452Smarcel		addr += 64 * (di->bas.chan - 1);
128119815Smarcel	} else if (!strcmp(buf, "zs")) {
129119815Smarcel		di->ops = uart_z8530_ops;
130120452Smarcel		di->bas.chan = uart_cpu_channel(dev);
131119815Smarcel		di->bas.regshft = 1;
132120452Smarcel		addr += 4 - 4 * (di->bas.chan - 1);
133120009Stmm	} else if (!strcmp(buf, "su") || !strcmp(buf, "su_pnp") ||
134120452Smarcel	    !strcmp(compat, "su") || !strcmp(compat, "su16550")) {
135119815Smarcel		di->ops = uart_ns8250_ops;
136120452Smarcel		di->bas.chan = 0;
137120452Smarcel	} else
138119815Smarcel		return (ENXIO);
139119815Smarcel
140119815Smarcel	/* Fill in the device info. */
141119815Smarcel	di->bas.bst = &bst_store[devtype];
142119815Smarcel	di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst);
143119815Smarcel
144119815Smarcel	/* Get the line settings. */
145120545Sjake	if (devtype == UART_DEV_KEYBOARD)
146120545Sjake		di->baudrate = 1200;
147120545Sjake	else
148120545Sjake		di->baudrate = 9600;
149119815Smarcel	di->databits = 8;
150119815Smarcel	di->stopbits = 1;
151119815Smarcel	di->parity = UART_PARITY_NONE;
152119815Smarcel	snprintf(buf, sizeof(buf), "%s-mode", dev);
153119815Smarcel	if (OF_getprop(options, buf, buf, sizeof(buf)) == -1)
154119815Smarcel		return (0);
155119815Smarcel	if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag)
156119815Smarcel	    != 5)
157119815Smarcel		return (0);
158119815Smarcel	di->baudrate = baud;
159119815Smarcel	di->databits = bits;
160119815Smarcel	di->stopbits = stop;
161119815Smarcel	di->parity = (par == 'n') ? UART_PARITY_NONE :
162119815Smarcel	    (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN;
163119815Smarcel	return (0);
164119815Smarcel}
165