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