summaryrefslogtreecommitdiffstats
path: root/sys/dev/uart/uart_subr.c
blob: 0277b2137179e4603c202e4a0166ac350bf31911 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*-
 * Copyright (c) 2004 Marcel Moolenaar
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>

#include <machine/bus.h>
#include <machine/vmparam.h>

#include <dev/uart/uart.h>
#include <dev/uart/uart_cpu.h>

#define	UART_TAG_BR	0
#define	UART_TAG_CH	1
#define	UART_TAG_DB	2
#define	UART_TAG_DT	3
#define	UART_TAG_IO	4
#define	UART_TAG_MM	5
#define	UART_TAG_PA	6
#define	UART_TAG_RS	7
#define	UART_TAG_SB	8
#define	UART_TAG_XO	9

static struct uart_class *uart_classes[] = {
	&uart_ns8250_class,
	&uart_sab82532_class,
	&uart_z8530_class,
#if defined(__arm__)
	&uart_lpc_class,
	&uart_s3c2410_class,
#endif
};
static size_t uart_nclasses = sizeof(uart_classes) / sizeof(uart_classes[0]);

static bus_addr_t
uart_parse_addr(const char **p)
{
	return (strtoul(*p, (char**)(uintptr_t)p, 0));
}

static struct uart_class *
uart_parse_class(struct uart_class *class, const char **p)
{
	struct uart_class *uc;
	const char *nm;
	size_t len;
	u_int i;

	for (i = 0; i < uart_nclasses; i++) {
		uc = uart_classes[i];
		nm = uart_getname(uc);
		if (nm == NULL || *nm == '\0')
			continue;
		len = strlen(nm);
		if (strncmp(nm, *p, len) == 0) {
			*p += len;
			return (uc);
		}
	}
	return (class);
}

static long
uart_parse_long(const char **p)
{
	return (strtol(*p, (char**)(uintptr_t)p, 0));
}

static int
uart_parse_parity(const char **p)
{
	if (!strncmp(*p, "even", 4)) {
		*p += 4;
		return UART_PARITY_EVEN;
	}
	if (!strncmp(*p, "mark", 4)) {
		*p += 4;
		return UART_PARITY_MARK;
	}
	if (!strncmp(*p, "none", 4)) {
		*p += 4;
		return UART_PARITY_NONE;
	}
	if (!strncmp(*p, "odd", 3)) {
		*p += 3;
		return UART_PARITY_ODD;
	}
	if (!strncmp(*p, "space", 5)) {
		*p += 5;
		return UART_PARITY_SPACE;
	}
	return (-1);
}

static int
uart_parse_tag(const char **p)
{
	int tag;

	if ((*p)[0] == 'b' && (*p)[1] == 'r') {
		tag = UART_TAG_BR;
		goto out;
	}
	if ((*p)[0] == 'c' && (*p)[1] == 'h') {
		tag = UART_TAG_CH;
		goto out;
	}
	if ((*p)[0] == 'd' && (*p)[1] == 'b') {
		tag = UART_TAG_DB;
		goto out;
	}
	if ((*p)[0] == 'd' && (*p)[1] == 't') {
		tag = UART_TAG_DT;
		goto out;
	}
	if ((*p)[0] == 'i' && (*p)[1] == 'o') {
		tag = UART_TAG_IO;
		goto out;
	}
	if ((*p)[0] == 'm' && (*p)[1] == 'm') {
		tag = UART_TAG_MM;
		goto out;
	}
	if ((*p)[0] == 'p' && (*p)[1] == 'a') {
		tag = UART_TAG_PA;
		goto out;
	}
	if ((*p)[0] == 'r' && (*p)[1] == 's') {
		tag = UART_TAG_RS;
		goto out;
	}
	if ((*p)[0] == 's' && (*p)[1] == 'b') {
		tag = UART_TAG_SB;
		goto out;
	}
	if ((*p)[0] == 'x' && (*p)[1] == 'o') {
		tag = UART_TAG_XO;
		goto out;
	}
	return (-1);

out:
	*p += 2;
	if ((*p)[0] != ':')
		return (-1);
	(*p)++;
	return (tag);
}

/*
 * Parse a device specification. The specification is a list of attributes
 * separated by commas. Each attribute is a tag-value pair with the tag and
 * value separated by a colon. Supported tags are:
 *
 *	br = Baudrate
 *	ch = Channel
 *	db = Data bits
 *	dt = Device type
 *	io = I/O port address
 *	mm = Memory mapped I/O address
 *	pa = Parity
 *	rs = Register shift
 *	sb = Stopbits
 *	xo = Device clock (xtal oscillator)
 *
 * The io and mm tags are mutually exclusive.
 */

int
uart_getenv(int devtype, struct uart_devinfo *di, struct uart_class *class)
{
	const char *spec;
	bus_addr_t addr = ~0U;
	int error;

	/*
	 * All uart_class references are weak. Make sure the default
	 * device class has been compiled-in.
	 */
	if (class == NULL)
		return (ENXIO);

	/*
	 * Check the environment variables "hw.uart.console" and
	 * "hw.uart.dbgport". These variables, when present, specify
	 * which UART port is to be used as serial console or debug
	 * port (resp).
	 */
	if (devtype == UART_DEV_CONSOLE)
		spec = getenv("hw.uart.console");
	else if (devtype == UART_DEV_DBGPORT)
		spec = getenv("hw.uart.dbgport");
	else
		spec = NULL;
	if (spec == NULL)
		return (ENXIO);

	/* Set defaults. */
	di->bas.chan = 0;
	di->bas.regshft = 0;
	di->bas.rclk = 0;
	di->baudrate = 0;
	di->databits = 8;
	di->stopbits = 1;
	di->parity = UART_PARITY_NONE;

	/* Parse the attributes. */
	while (1) {
		switch (uart_parse_tag(&spec)) {
		case UART_TAG_BR:
			di->baudrate = uart_parse_long(&spec);
			break;
		case UART_TAG_CH:
			di->bas.chan = uart_parse_long(&spec);
			break;
		case UART_TAG_DB:
			di->databits = uart_parse_long(&spec);
			break;
		case UART_TAG_DT:
			class = uart_parse_class(class, &spec);
			break;
		case UART_TAG_IO:
			di->bas.bst = uart_bus_space_io;
			addr = uart_parse_addr(&spec);
			break;
		case UART_TAG_MM:
			di->bas.bst = uart_bus_space_mem;
			addr = uart_parse_addr(&spec);
			break;
		case UART_TAG_PA:
			di->parity = uart_parse_parity(&spec);
			break;
		case UART_TAG_RS:
			di->bas.regshft = uart_parse_long(&spec);
			break;
		case UART_TAG_SB:
			di->stopbits = uart_parse_long(&spec);
			break;
		case UART_TAG_XO:
			di->bas.rclk = uart_parse_long(&spec);
			break;
		default:
			return (EINVAL);
		}
		if (*spec == '\0')
			break;
		if (*spec != ',')
			return (EINVAL);
		spec++;
	}

	/*
	 * If we still have an invalid address, the specification must be
	 * missing an I/O port or memory address. We don't like that.
	 */
	if (addr == ~0U)
		return (EINVAL);

	/*
	 * Accept only the well-known baudrates. Any invalid baudrate
	 * is silently replaced with a 0-valued baudrate. The 0 baudrate
	 * has special meaning. It means that we're not supposed to
	 * program the baudrate and simply communicate with whatever
	 * speed the hardware is currently programmed for.
	 */
	if (di->baudrate >= 19200) {
		if (di->baudrate % 19200)
			di->baudrate = 0;
	} else if (di->baudrate >= 1200) {
		if (di->baudrate % 1200)
			di->baudrate = 0;
	} else if (di->baudrate > 0) {
		if (di->baudrate % 75)
			di->baudrate = 0;
	} else
		di->baudrate = 0;

	/* Set the ops and create a bus space handle. */
	di->ops = uart_getops(class);
	error = bus_space_map(di->bas.bst, addr, uart_getrange(class), 0,
	    &di->bas.bsh);
	return (error);
}
OpenPOWER on IntegriCloud