wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2004 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <serial.h> |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 26 | #include <stdio_dev.h> |
Mike Frysinger | 078f2f1 | 2011-05-14 06:56:15 +0000 | [diff] [blame] | 27 | #include <post.h> |
| 28 | #include <linux/compiler.h> |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 29 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 32 | static struct serial_device *serial_devices; |
| 33 | static struct serial_device *serial_current; |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 34 | |
Marek Vasut | f6af048 | 2012-09-12 17:49:58 +0200 | [diff] [blame^] | 35 | static void serial_null(void) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | #define serial_initfunc(name) \ |
| 40 | void name(void) \ |
| 41 | __attribute__((weak, alias("serial_null"))); |
| 42 | |
Mike Frysinger | ffadc82 | 2011-04-29 18:03:30 +0000 | [diff] [blame] | 43 | void serial_register(struct serial_device *dev) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 44 | { |
Wolfgang Denk | d0813e5 | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 45 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
Marek Vasut | b46931d | 2012-09-07 14:35:31 +0200 | [diff] [blame] | 46 | dev->start += gd->reloc_off; |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 47 | dev->setbrg += gd->reloc_off; |
| 48 | dev->getc += gd->reloc_off; |
| 49 | dev->tstc += gd->reloc_off; |
| 50 | dev->putc += gd->reloc_off; |
| 51 | dev->puts += gd->reloc_off; |
Peter Tyser | 9057cbf | 2009-09-21 11:20:36 -0500 | [diff] [blame] | 52 | #endif |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 53 | |
| 54 | dev->next = serial_devices; |
| 55 | serial_devices = dev; |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 58 | void serial_initialize(void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 59 | { |
| 60 | #if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2) |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 61 | serial_register(&serial_smc_device); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 62 | #endif |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 63 | #if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) || \ |
| 64 | defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4) |
| 65 | serial_register(&serial_scc_device); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 66 | #endif |
| 67 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 68 | #if defined(CONFIG_SYS_NS16550_SERIAL) |
| 69 | #if defined(CONFIG_SYS_NS16550_COM1) |
Wolfgang Denk | 44df561 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 70 | serial_register(&eserial1_device); |
| 71 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 72 | #if defined(CONFIG_SYS_NS16550_COM2) |
Wolfgang Denk | 44df561 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 73 | serial_register(&eserial2_device); |
| 74 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 75 | #if defined(CONFIG_SYS_NS16550_COM3) |
Wolfgang Denk | 44df561 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 76 | serial_register(&eserial3_device); |
| 77 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 78 | #if defined(CONFIG_SYS_NS16550_COM4) |
Wolfgang Denk | 44df561 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 79 | serial_register(&eserial4_device); |
| 80 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 81 | #endif /* CONFIG_SYS_NS16550_SERIAL */ |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 82 | #if defined(CONFIG_FFUART) |
stefano babic | 5ab4f03 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 83 | serial_register(&serial_ffuart_device); |
| 84 | #endif |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 85 | #if defined(CONFIG_BTUART) |
stefano babic | 5ab4f03 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 86 | serial_register(&serial_btuart_device); |
| 87 | #endif |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 88 | #if defined(CONFIG_STUART) |
stefano babic | 5ab4f03 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 89 | serial_register(&serial_stuart_device); |
| 90 | #endif |
Harald Welte | da41262 | 2007-12-19 14:24:40 +0100 | [diff] [blame] | 91 | #if defined(CONFIG_S3C2410) |
| 92 | serial_register(&s3c24xx_serial0_device); |
| 93 | serial_register(&s3c24xx_serial1_device); |
| 94 | serial_register(&s3c24xx_serial2_device); |
| 95 | #endif |
Minkyu Kang | 485feb6 | 2010-08-23 19:52:03 +0900 | [diff] [blame] | 96 | #if defined(CONFIG_S5P) |
Minkyu Kang | baa3688 | 2010-03-24 16:59:30 +0900 | [diff] [blame] | 97 | serial_register(&s5p_serial0_device); |
| 98 | serial_register(&s5p_serial1_device); |
| 99 | serial_register(&s5p_serial2_device); |
| 100 | serial_register(&s5p_serial3_device); |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 101 | #endif |
Anatolij Gustschin | ee4aac8 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 102 | #if defined(CONFIG_MPC512X) |
| 103 | #if defined(CONFIG_SYS_PSC1) |
| 104 | serial_register(&serial1_device); |
| 105 | #endif |
| 106 | #if defined(CONFIG_SYS_PSC3) |
| 107 | serial_register(&serial3_device); |
| 108 | #endif |
| 109 | #if defined(CONFIG_SYS_PSC4) |
| 110 | serial_register(&serial4_device); |
| 111 | #endif |
| 112 | #if defined(CONFIG_SYS_PSC6) |
| 113 | serial_register(&serial6_device); |
| 114 | #endif |
| 115 | #endif |
Mike Frysinger | 53ba322 | 2011-04-29 23:23:28 -0400 | [diff] [blame] | 116 | #if defined(CONFIG_SYS_BFIN_UART) |
| 117 | serial_register_bfin_uart(); |
| 118 | #endif |
Michal Simek | 75348da | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 119 | #if defined(CONFIG_XILINX_UARTLITE) |
| 120 | # ifdef XILINX_UARTLITE_BASEADDR |
| 121 | serial_register(&uartlite_serial0_device); |
| 122 | # endif /* XILINX_UARTLITE_BASEADDR */ |
| 123 | # ifdef XILINX_UARTLITE_BASEADDR1 |
| 124 | serial_register(&uartlite_serial1_device); |
| 125 | # endif /* XILINX_UARTLITE_BASEADDR1 */ |
| 126 | # ifdef XILINX_UARTLITE_BASEADDR2 |
| 127 | serial_register(&uartlite_serial2_device); |
| 128 | # endif /* XILINX_UARTLITE_BASEADDR2 */ |
| 129 | # ifdef XILINX_UARTLITE_BASEADDR3 |
| 130 | serial_register(&uartlite_serial3_device); |
| 131 | # endif /* XILINX_UARTLITE_BASEADDR3 */ |
| 132 | #endif /* CONFIG_XILINX_UARTLITE */ |
Michal Simek | 76bed83 | 2012-09-14 00:55:24 +0000 | [diff] [blame] | 133 | #if defined(CONFIG_ZYNQ_SERIAL) |
| 134 | # ifdef CONFIG_ZYNQ_SERIAL_BASEADDR0 |
| 135 | serial_register(&uart_zynq_serial0_device); |
| 136 | # endif |
| 137 | # ifdef CONFIG_ZYNQ_SERIAL_BASEADDR1 |
| 138 | serial_register(&uart_zynq_serial1_device); |
| 139 | # endif |
| 140 | #endif |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 141 | serial_assign(default_serial_console()->name); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 144 | void serial_stdio_init(void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 145 | { |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 146 | struct stdio_dev dev; |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 147 | struct serial_device *s = serial_devices; |
| 148 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 149 | while (s) { |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 150 | memset(&dev, 0, sizeof(dev)); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 151 | |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 152 | strcpy(dev.name, s->name); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 153 | dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT; |
| 154 | |
Marek Vasut | b46931d | 2012-09-07 14:35:31 +0200 | [diff] [blame] | 155 | dev.start = s->start; |
| 156 | dev.stop = s->stop; |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 157 | dev.putc = s->putc; |
| 158 | dev.puts = s->puts; |
| 159 | dev.getc = s->getc; |
| 160 | dev.tstc = s->tstc; |
| 161 | |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 162 | stdio_register(&dev); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 163 | |
| 164 | s = s->next; |
| 165 | } |
| 166 | } |
| 167 | |
Gerlando Falauto | 9299958 | 2011-11-18 06:49:12 +0000 | [diff] [blame] | 168 | int serial_assign(const char *name) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 169 | { |
| 170 | struct serial_device *s; |
| 171 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 172 | for (s = serial_devices; s; s = s->next) { |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 173 | if (strcmp(s->name, name) == 0) { |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 174 | serial_current = s; |
| 175 | return 0; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return 1; |
| 180 | } |
| 181 | |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 182 | void serial_reinit_all(void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 183 | { |
| 184 | struct serial_device *s; |
| 185 | |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 186 | for (s = serial_devices; s; s = s->next) |
Marek Vasut | b46931d | 2012-09-07 14:35:31 +0200 | [diff] [blame] | 187 | s->start(); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Simon Glass | 8f4e6f1 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 190 | static struct serial_device *get_current(void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 191 | { |
Simon Glass | 8f4e6f1 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 192 | struct serial_device *dev; |
| 193 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 194 | if (!(gd->flags & GD_FLG_RELOC) || !serial_current) { |
Simon Glass | 8f4e6f1 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 195 | dev = default_serial_console(); |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 196 | |
Simon Glass | 8f4e6f1 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 197 | /* We must have a console device */ |
| 198 | if (!dev) |
| 199 | panic("Cannot find console"); |
| 200 | } else |
| 201 | dev = serial_current; |
| 202 | return dev; |
| 203 | } |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 204 | |
Simon Glass | 8f4e6f1 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 205 | int serial_init(void) |
| 206 | { |
Marek Vasut | b46931d | 2012-09-07 14:35:31 +0200 | [diff] [blame] | 207 | return get_current()->start(); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 210 | void serial_setbrg(void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 211 | { |
Simon Glass | 8f4e6f1 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 212 | get_current()->setbrg(); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 215 | int serial_getc(void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 216 | { |
Simon Glass | 8f4e6f1 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 217 | return get_current()->getc(); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 220 | int serial_tstc(void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 221 | { |
Simon Glass | 8f4e6f1 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 222 | return get_current()->tstc(); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 225 | void serial_putc(const char c) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 226 | { |
Simon Glass | 8f4e6f1 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 227 | get_current()->putc(c); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Gerlando Falauto | 34148d6 | 2011-11-18 06:49:11 +0000 | [diff] [blame] | 230 | void serial_puts(const char *s) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 231 | { |
Simon Glass | 8f4e6f1 | 2011-08-19 11:09:43 +0000 | [diff] [blame] | 232 | get_current()->puts(s); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 233 | } |
Mike Frysinger | 078f2f1 | 2011-05-14 06:56:15 +0000 | [diff] [blame] | 234 | |
| 235 | #if CONFIG_POST & CONFIG_SYS_POST_UART |
| 236 | static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE; |
| 237 | |
| 238 | /* Mark weak until post/cpu/.../uart.c migrate over */ |
| 239 | __weak |
| 240 | int uart_post_test(int flags) |
| 241 | { |
| 242 | unsigned char c; |
| 243 | int ret, saved_baud, b; |
| 244 | struct serial_device *saved_dev, *s; |
| 245 | bd_t *bd = gd->bd; |
| 246 | |
| 247 | /* Save current serial state */ |
| 248 | ret = 0; |
| 249 | saved_dev = serial_current; |
| 250 | saved_baud = bd->bi_baudrate; |
| 251 | |
| 252 | for (s = serial_devices; s; s = s->next) { |
| 253 | /* If this driver doesn't support loop back, skip it */ |
| 254 | if (!s->loop) |
| 255 | continue; |
| 256 | |
| 257 | /* Test the next device */ |
| 258 | serial_current = s; |
| 259 | |
| 260 | ret = serial_init(); |
| 261 | if (ret) |
| 262 | goto done; |
| 263 | |
| 264 | /* Consume anything that happens to be queued */ |
| 265 | while (serial_tstc()) |
| 266 | serial_getc(); |
| 267 | |
| 268 | /* Enable loop back */ |
| 269 | s->loop(1); |
| 270 | |
| 271 | /* Test every available baud rate */ |
| 272 | for (b = 0; b < ARRAY_SIZE(bauds); ++b) { |
| 273 | bd->bi_baudrate = bauds[b]; |
| 274 | serial_setbrg(); |
| 275 | |
| 276 | /* |
| 277 | * Stick to printable chars to avoid issues: |
| 278 | * - terminal corruption |
| 279 | * - serial program reacting to sequences and sending |
| 280 | * back random extra data |
| 281 | * - most serial drivers add in extra chars (like \r\n) |
| 282 | */ |
| 283 | for (c = 0x20; c < 0x7f; ++c) { |
| 284 | /* Send it out */ |
| 285 | serial_putc(c); |
| 286 | |
| 287 | /* Make sure it's the same one */ |
| 288 | ret = (c != serial_getc()); |
| 289 | if (ret) { |
| 290 | s->loop(0); |
| 291 | goto done; |
| 292 | } |
| 293 | |
| 294 | /* Clean up the output in case it was sent */ |
| 295 | serial_putc('\b'); |
| 296 | ret = ('\b' != serial_getc()); |
| 297 | if (ret) { |
| 298 | s->loop(0); |
| 299 | goto done; |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /* Disable loop back */ |
| 305 | s->loop(0); |
| 306 | |
Marek Vasut | b46931d | 2012-09-07 14:35:31 +0200 | [diff] [blame] | 307 | /* XXX: There is no serial_stop() !? */ |
| 308 | if (s->stop) |
| 309 | s->stop(); |
Mike Frysinger | 078f2f1 | 2011-05-14 06:56:15 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | done: |
| 313 | /* Restore previous serial state */ |
| 314 | serial_current = saved_dev; |
| 315 | bd->bi_baudrate = saved_baud; |
| 316 | serial_reinit_all(); |
| 317 | serial_setbrg(); |
| 318 | |
| 319 | return ret; |
| 320 | } |
| 321 | #endif |