uart_cpu_sparc64.c revision 127825
1119815Smarcel/*
2127215Smarcel * Copyright (c) 2003, 2004 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 127825 2004-04-04 05:06:26Z marcel $");
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
42127215Smarcelbus_space_tag_t uart_bus_space_io;
43127215Smarcelbus_space_tag_t uart_bus_space_mem;
44127215Smarcel
45119815Smarcelstatic struct bus_space_tag bst_store[3];
46119815Smarcel
47119815Smarcelstatic int
48119815Smarceluart_cpu_channel(char *dev)
49119815Smarcel{
50119815Smarcel	char alias[64];
51119815Smarcel	phandle_t aliases;
52119815Smarcel	int len;
53119815Smarcel
54119815Smarcel	strcpy(alias, dev);
55119815Smarcel	if ((aliases = OF_finddevice("/aliases")) != -1)
56119815Smarcel		OF_getprop(aliases, dev, alias, sizeof(alias));
57119815Smarcel	len = strlen(alias);
58119815Smarcel	if (len < 2 || alias[len - 2] != ':' || alias[len - 1] < 'a' ||
59119815Smarcel	    alias[len - 1] > 'b')
60119815Smarcel		return (0);
61120452Smarcel	return (alias[len - 1] - 'a' + 1);
62119815Smarcel}
63119815Smarcel
64119815Smarcelint
65119866Smarceluart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
66119866Smarcel{
67119866Smarcel
68119866Smarcel	return ((b1->bsh == b2->bsh) ? 1 : 0);
69119866Smarcel}
70119866Smarcel
71127741Smarcel/*
72127741Smarcel * Get the address of the UART that is selected as the console, if the
73127741Smarcel * console is an UART of course. Note that we enforce that both stdin and
74127825Smarcel * stdout are selected.
75127741Smarcel * Note that the currently active console (i.e. /chosen/stdout and
76127741Smarcel * /chosen/stdin) may not be the same as the device selected in the
77127741Smarcel * environment (ie /options/output-device and /options/input-device) because
78127825Smarcel * keyboard and screen were selected but the keyboard was unplugged or the
79127825Smarcel * user has changed the environment. In the latter case I would assume that
80127825Smarcel * the user expects that FreeBSD uses the new console setting.
81127825Smarcel * For weirder configurations, use ofw_console(4).
82127741Smarcel */
83122466Sjakestatic phandle_t
84127741Smarceluart_cpu_getdev_console(phandle_t options, char *dev, size_t devsz)
85122466Sjake{
86122466Sjake	char buf[32];
87127741Smarcel	phandle_t input;
88122466Sjake
89127741Smarcel	if (OF_getprop(options, "input-device", dev, devsz) == -1)
90127741Smarcel		return (-1);
91127741Smarcel	if ((input = OF_finddevice(dev)) == -1)
92127741Smarcel		return (-1);
93127825Smarcel	if (OF_getprop(options, "output-device", buf, sizeof(buf)) == -1)
94127825Smarcel		return (-1);
95127825Smarcel	if (!strcmp(dev, "keyboard") && !strcmp(buf, "screen")) {
96127825Smarcel		phandle_t chosen;
97127825Smarcel		ihandle_t stdin, stdout;
98127825Smarcel
99127825Smarcel		if ((chosen = OF_finddevice("/chosen")) == -1)
100127825Smarcel			return (-1);
101127825Smarcel		if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1)
102127825Smarcel			return (-1);
103127825Smarcel		if ((input = OF_instance_to_package(stdin)) == -1)
104127825Smarcel			return (-1);
105127825Smarcel		if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1)
106127825Smarcel			return (-1);
107127825Smarcel		if (OF_instance_to_package(stdout) != input)
108127825Smarcel			return (-1);
109127825Smarcel		snprintf(dev, devsz, "ttya");
110127825Smarcel	} else if (OF_finddevice(buf) != input)
111127825Smarcel		return (-1);
112127741Smarcel	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
113127741Smarcel		return (-1);
114127741Smarcel	if (strcmp(buf, "serial") != 0)
115127741Smarcel		return (-1);
116127741Smarcel	return (input);
117127741Smarcel}
118127741Smarcel
119127741Smarcel/*
120127741Smarcel * Get the address of the UART that's selected as the debug port. Since
121127741Smarcel * there's no place for this in the OF, we use the kernel environment
122127741Smarcel * variable "hw.uart.dbgport". Note however that the variable is not a
123127741Smarcel * list of attributes. It's single device name or alias, as known by
124127741Smarcel * the OF.
125127741Smarcel */
126127741Smarcelstatic phandle_t
127127741Smarceluart_cpu_getdev_dbgport(phandle_t options, char *dev, size_t devsz)
128127741Smarcel{
129127741Smarcel	char buf[32];
130127741Smarcel	phandle_t input;
131127741Smarcel
132127741Smarcel	if (!getenv_string("hw.uart.dbgport", dev, devsz))
133127741Smarcel		return (-1);
134127741Smarcel	if ((input = OF_finddevice(dev)) == -1)
135127741Smarcel		return (-1);
136127741Smarcel	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
137127741Smarcel		return (-1);
138127741Smarcel	if (strcmp(buf, "serial") != 0)
139127741Smarcel		return (-1);
140127741Smarcel	return (input);
141127741Smarcel}
142127741Smarcel
143127741Smarcelstatic phandle_t
144127741Smarceluart_cpu_getdev_keyboard(phandle_t root, char *dev, size_t devsz)
145127741Smarcel{
146127741Smarcel	char buf[32];
147127741Smarcel	phandle_t child, node;
148127741Smarcel
149127741Smarcel	child = OF_child(root);
150127741Smarcel	while (child != 0 && child != -1) {
151122466Sjake		if (OF_getprop(child, "device_type", buf, sizeof(buf)) != -1 &&
152122466Sjake		    !strcmp(buf, "serial") &&
153127741Smarcel		    OF_getprop(child, "keyboard", buf, sizeof(buf)) != -1) {
154127741Smarcel			OF_getprop(child, "name", dev, devsz);
155122466Sjake			return (child);
156127741Smarcel		}
157127741Smarcel		if ((node = uart_cpu_getdev_keyboard(child, dev, devsz)) != -1)
158122466Sjake			return (node);
159127741Smarcel		child = OF_peer(child);
160122466Sjake	}
161122466Sjake	return (-1);
162122466Sjake}
163122466Sjake
164119866Smarcelint
165119815Smarceluart_cpu_getdev(int devtype, struct uart_devinfo *di)
166119815Smarcel{
167120009Stmm	char buf[32], dev[32], compat[32];
168127741Smarcel	phandle_t input, options;
169119815Smarcel	bus_addr_t addr;
170120452Smarcel	int baud, bits, error, space, stop;
171119815Smarcel	char flag, par;
172119815Smarcel
173127741Smarcel	if ((options = OF_finddevice("/options")) == -1)
174127741Smarcel		return (ENXIO);
175127741Smarcel	switch (devtype) {
176127741Smarcel	case UART_DEV_CONSOLE:
177127741Smarcel		input = uart_cpu_getdev_console(options, dev, sizeof(dev));
178127741Smarcel		break;
179127741Smarcel	case UART_DEV_DBGPORT:
180127741Smarcel		input = uart_cpu_getdev_dbgport(options, dev, sizeof(dev));
181127741Smarcel		break;
182127741Smarcel	case UART_DEV_KEYBOARD:
183127741Smarcel		input = uart_cpu_getdev_keyboard(OF_peer(0), dev, sizeof(dev));
184127741Smarcel		break;
185127741Smarcel	default:
186127741Smarcel		input = -1;
187127741Smarcel		break;
188127741Smarcel	}
189127741Smarcel	if (input == -1)
190127741Smarcel		return (ENXIO);
191119815Smarcel	error = OF_decode_addr(input, &space, &addr);
192119815Smarcel	if (error)
193119815Smarcel		return (error);
194119815Smarcel
195119815Smarcel	/* Get the device class. */
196119815Smarcel	if (OF_getprop(input, "name", buf, sizeof(buf)) == -1)
197119815Smarcel		return (ENXIO);
198120009Stmm	if (OF_getprop(input, "compatible", compat, sizeof(compat)) == -1)
199120009Stmm		compat[0] = '\0';
200119815Smarcel	di->bas.regshft = 0;
201119815Smarcel	di->bas.rclk = 0;
202119815Smarcel	if (!strcmp(buf, "se")) {
203119815Smarcel		di->ops = uart_sab82532_ops;
204120452Smarcel		di->bas.chan = uart_cpu_channel(dev);
205120452Smarcel		addr += 64 * (di->bas.chan - 1);
206119815Smarcel	} else if (!strcmp(buf, "zs")) {
207119815Smarcel		di->ops = uart_z8530_ops;
208120452Smarcel		di->bas.chan = uart_cpu_channel(dev);
209119815Smarcel		di->bas.regshft = 1;
210120452Smarcel		addr += 4 - 4 * (di->bas.chan - 1);
211120009Stmm	} else if (!strcmp(buf, "su") || !strcmp(buf, "su_pnp") ||
212120452Smarcel	    !strcmp(compat, "su") || !strcmp(compat, "su16550")) {
213119815Smarcel		di->ops = uart_ns8250_ops;
214120452Smarcel		di->bas.chan = 0;
215120452Smarcel	} else
216119815Smarcel		return (ENXIO);
217119815Smarcel
218119815Smarcel	/* Fill in the device info. */
219119815Smarcel	di->bas.bst = &bst_store[devtype];
220119815Smarcel	di->bas.bsh = sparc64_fake_bustag(space, addr, di->bas.bst);
221119815Smarcel
222119815Smarcel	/* Get the line settings. */
223120545Sjake	if (devtype == UART_DEV_KEYBOARD)
224120545Sjake		di->baudrate = 1200;
225120545Sjake	else
226120545Sjake		di->baudrate = 9600;
227119815Smarcel	di->databits = 8;
228119815Smarcel	di->stopbits = 1;
229119815Smarcel	di->parity = UART_PARITY_NONE;
230119815Smarcel	snprintf(buf, sizeof(buf), "%s-mode", dev);
231119815Smarcel	if (OF_getprop(options, buf, buf, sizeof(buf)) == -1)
232119815Smarcel		return (0);
233119815Smarcel	if (sscanf(buf, "%d,%d,%c,%d,%c", &baud, &bits, &par, &stop, &flag)
234119815Smarcel	    != 5)
235119815Smarcel		return (0);
236119815Smarcel	di->baudrate = baud;
237119815Smarcel	di->databits = bits;
238119815Smarcel	di->stopbits = stop;
239119815Smarcel	di->parity = (par == 'n') ? UART_PARITY_NONE :
240119815Smarcel	    (par == 'o') ? UART_PARITY_ODD : UART_PARITY_EVEN;
241119815Smarcel	return (0);
242119815Smarcel}
243