1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2004 Marcel Moolenaar 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/bus.h> 35 36 #include <machine/bus.h> 37 #include <machine/vmparam.h> 38 39 #include <dev/uart/uart.h> 40 #include <dev/uart/uart_cpu.h> 41 42 #define UART_TAG_BR 0 43 #define UART_TAG_CH 1 44 #define UART_TAG_DB 2 45 #define UART_TAG_DT 3 46 #define UART_TAG_IO 4 47 #define UART_TAG_MM 5 48 #define UART_TAG_PA 6 49 #define UART_TAG_RS 7 50 #define UART_TAG_SB 8 51 #define UART_TAG_XO 9 52 #define UART_TAG_BD 10 53 54 static struct uart_class *uart_classes[] = { 55 &uart_ns8250_class, 56 &uart_z8530_class, 57 #if defined(__arm__) 58 &uart_s3c2410_class, 59 #endif 60 }; 61 62 static bus_addr_t 63 uart_parse_addr(const char **p) 64 { 65 return (strtoul(*p, (char**)(uintptr_t)p, 0)); 66 } 67 68 static struct uart_class * 69 uart_parse_class(struct uart_class *class, const char **p) 70 { 71 struct uart_class *uc; 72 const char *nm; 73 size_t len; 74 u_int i; 75 76 for (i = 0; i < nitems(uart_classes); i++) { 77 uc = uart_classes[i]; 78 nm = uart_getname(uc); 79 if (nm == NULL || *nm == '\0') 80 continue; 81 len = strlen(nm); 82 if (strncmp(nm, *p, len) == 0) { 83 *p += len; 84 return (uc); 85 } 86 } 87 return (class); 88 } 89 90 static long 91 uart_parse_long(const char **p) 92 { 93 return (strtol(*p, (char**)(uintptr_t)p, 0)); 94 } 95 96 static int 97 uart_parse_parity(const char **p) 98 { 99 if (!strncmp(*p, "even", 4)) { 100 *p += 4; 101 return UART_PARITY_EVEN; 102 } 103 if (!strncmp(*p, "mark", 4)) { 104 *p += 4; 105 return UART_PARITY_MARK; 106 } 107 if (!strncmp(*p, "none", 4)) { 108 *p += 4; 109 return UART_PARITY_NONE; 110 } 111 if (!strncmp(*p, "odd", 3)) { 112 *p += 3; 113 return UART_PARITY_ODD; 114 } 115 if (!strncmp(*p, "space", 5)) { 116 *p += 5; 117 return UART_PARITY_SPACE; 118 } 119 return (-1); 120 } 121 122 static int 123 uart_parse_tag(const char **p) 124 { 125 int tag; 126 127 if ((*p)[0] == 'b' && (*p)[1] == 'd') { 128 tag = UART_TAG_BD; 129 goto out; 130 } 131 if ((*p)[0] == 'b' && (*p)[1] == 'r') { 132 tag = UART_TAG_BR; 133 goto out; 134 } 135 if ((*p)[0] == 'c' && (*p)[1] == 'h') { 136 tag = UART_TAG_CH; 137 goto out; 138 } 139 if ((*p)[0] == 'd' && (*p)[1] == 'b') { 140 tag = UART_TAG_DB; 141 goto out; 142 } 143 if ((*p)[0] == 'd' && (*p)[1] == 't') { 144 tag = UART_TAG_DT; 145 goto out; 146 } 147 if ((*p)[0] == 'i' && (*p)[1] == 'o') { 148 tag = UART_TAG_IO; 149 goto out; 150 } 151 if ((*p)[0] == 'm' && (*p)[1] == 'm') { 152 tag = UART_TAG_MM; 153 goto out; 154 } 155 if ((*p)[0] == 'p' && (*p)[1] == 'a') { 156 tag = UART_TAG_PA; 157 goto out; 158 } 159 if ((*p)[0] == 'r' && (*p)[1] == 's') { 160 tag = UART_TAG_RS; 161 goto out; 162 } 163 if ((*p)[0] == 's' && (*p)[1] == 'b') { 164 tag = UART_TAG_SB; 165 goto out; 166 } 167 if ((*p)[0] == 'x' && (*p)[1] == 'o') { 168 tag = UART_TAG_XO; 169 goto out; 170 } 171 return (-1); 172 173 out: 174 *p += 2; 175 if ((*p)[0] != ':' && (*p)[0] != '=') 176 return (-1); 177 (*p)++; 178 return (tag); 179 } 180 181 /* 182 * Parse a device specification. The specification is a list of attributes 183 * separated by commas. Each attribute is a tag-value pair with the tag and 184 * value separated by a colon. Supported tags are: 185 * 186 * bd = Busy Detect 187 * br = Baudrate 188 * ch = Channel 189 * db = Data bits 190 * dt = Device type 191 * io = I/O port address 192 * mm = Memory mapped I/O address 193 * pa = Parity 194 * rs = Register shift 195 * sb = Stopbits 196 * xo = Device clock (xtal oscillator) 197 * 198 * The io and mm tags are mutually exclusive. 199 */ 200 201 int 202 uart_getenv(int devtype, struct uart_devinfo *di, struct uart_class *class) 203 { 204 const char *spec; 205 char *cp; 206 bus_addr_t addr = ~0U; 207 int error; 208 209 /* 210 * All uart_class references are weak. Make sure the default 211 * device class has been compiled-in. 212 */ 213 if (class == NULL) 214 return (ENXIO); 215 216 /* 217 * Check the environment variables "hw.uart.console" and 218 * "hw.uart.dbgport". These variables, when present, specify 219 * which UART port is to be used as serial console or debug 220 * port (resp). 221 */ 222 switch (devtype) { 223 case UART_DEV_CONSOLE: 224 cp = kern_getenv("hw.uart.console"); 225 break; 226 case UART_DEV_DBGPORT: 227 cp = kern_getenv("hw.uart.dbgport"); 228 break; 229 default: 230 cp = NULL; 231 break; 232 } 233 234 if (cp == NULL) 235 return (ENXIO); 236 237 /* Set defaults. */ 238 di->bas.chan = 0; 239 di->bas.regshft = 0; 240 di->bas.rclk = 0; 241 di->baudrate = 0; 242 di->databits = 8; 243 di->stopbits = 1; 244 di->parity = UART_PARITY_NONE; 245 246 /* Parse the attributes. */ 247 spec = cp; 248 for (;;) { 249 switch (uart_parse_tag(&spec)) { 250 case UART_TAG_BD: 251 di->bas.busy_detect = uart_parse_long(&spec); 252 break; 253 case UART_TAG_BR: 254 di->baudrate = uart_parse_long(&spec); 255 break; 256 case UART_TAG_CH: 257 di->bas.chan = uart_parse_long(&spec); 258 break; 259 case UART_TAG_DB: 260 di->databits = uart_parse_long(&spec); 261 break; 262 case UART_TAG_DT: 263 class = uart_parse_class(class, &spec); 264 break; 265 case UART_TAG_IO: 266 di->bas.bst = uart_bus_space_io; 267 addr = uart_parse_addr(&spec); 268 break; 269 case UART_TAG_MM: 270 di->bas.bst = uart_bus_space_mem; 271 addr = uart_parse_addr(&spec); 272 break; 273 case UART_TAG_PA: 274 di->parity = uart_parse_parity(&spec); 275 break; 276 case UART_TAG_RS: 277 di->bas.regshft = uart_parse_long(&spec); 278 break; 279 case UART_TAG_SB: 280 di->stopbits = uart_parse_long(&spec); 281 break; 282 case UART_TAG_XO: 283 di->bas.rclk = uart_parse_long(&spec); 284 break; 285 default: 286 goto inval; 287 } 288 if (*spec == '\0') 289 break; 290 if (*spec != ',') 291 goto inval; 292 spec++; 293 } 294 295 /* 296 * If we still have an invalid address, the specification must be 297 * missing an I/O port or memory address. We don't like that. 298 */ 299 if (addr == ~0U) 300 goto inval; 301 freeenv(cp); 302 303 /* 304 * Accept only the well-known baudrates. Any invalid baudrate 305 * is silently replaced with a 0-valued baudrate. The 0 baudrate 306 * has special meaning. It means that we're not supposed to 307 * program the baudrate and simply communicate with whatever 308 * speed the hardware is currently programmed for. 309 */ 310 if (di->baudrate >= 19200) { 311 if (di->baudrate % 19200) 312 di->baudrate = 0; 313 } else if (di->baudrate >= 1200) { 314 if (di->baudrate % 1200) 315 di->baudrate = 0; 316 } else if (di->baudrate > 0) { 317 if (di->baudrate % 75) 318 di->baudrate = 0; 319 } else 320 di->baudrate = 0; 321 322 /* Set the ops and create a bus space handle. */ 323 di->ops = uart_getops(class); 324 error = bus_space_map(di->bas.bst, addr, uart_getrange(class), 0, 325 &di->bas.bsh); 326 return (error); 327 inval: 328 printf("warning: bad uart specification: %s\n", cp); 329 freeenv(cp); 330 return (EINVAL); 331 } 332